5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
22 * 12/06/98 blf partition support in udf_iget, udf_block_map
24 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/writeback.h>
37 #include <linux/slab.h>
38 #include <linux/crc-itu-t.h>
39 #include <linux/mpage.h>
40 #include <linux/uio.h>
41 #include <linux/bio.h>
46 #define EXTENT_MERGE_SIZE 5
48 static umode_t udf_convert_permissions(struct fileEntry *);
49 static int udf_update_inode(struct inode *, int);
50 static int udf_sync_inode(struct inode *inode);
51 static int udf_alloc_i_data(struct inode *inode, size_t size);
52 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
53 static int8_t udf_insert_aext(struct inode *, struct extent_position,
54 struct kernel_lb_addr, uint32_t);
55 static void udf_split_extents(struct inode *, int *, int, int,
56 struct kernel_long_ad *, int *);
57 static void udf_prealloc_extents(struct inode *, int, int,
58 struct kernel_long_ad *, int *);
59 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
60 static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
61 int, struct extent_position *);
62 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
64 static void __udf_clear_extent_cache(struct inode *inode)
66 struct udf_inode_info *iinfo = UDF_I(inode);
68 if (iinfo->cached_extent.lstart != -1) {
69 brelse(iinfo->cached_extent.epos.bh);
70 iinfo->cached_extent.lstart = -1;
74 /* Invalidate extent cache */
75 static void udf_clear_extent_cache(struct inode *inode)
77 struct udf_inode_info *iinfo = UDF_I(inode);
79 spin_lock(&iinfo->i_extent_cache_lock);
80 __udf_clear_extent_cache(inode);
81 spin_unlock(&iinfo->i_extent_cache_lock);
84 /* Return contents of extent cache */
85 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
86 loff_t *lbcount, struct extent_position *pos)
88 struct udf_inode_info *iinfo = UDF_I(inode);
91 spin_lock(&iinfo->i_extent_cache_lock);
92 if ((iinfo->cached_extent.lstart <= bcount) &&
93 (iinfo->cached_extent.lstart != -1)) {
95 *lbcount = iinfo->cached_extent.lstart;
96 memcpy(pos, &iinfo->cached_extent.epos,
97 sizeof(struct extent_position));
102 spin_unlock(&iinfo->i_extent_cache_lock);
106 /* Add extent to extent cache */
107 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
108 struct extent_position *pos)
110 struct udf_inode_info *iinfo = UDF_I(inode);
112 spin_lock(&iinfo->i_extent_cache_lock);
113 /* Invalidate previously cached extent */
114 __udf_clear_extent_cache(inode);
117 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
118 iinfo->cached_extent.lstart = estart;
119 switch (iinfo->i_alloc_type) {
120 case ICBTAG_FLAG_AD_SHORT:
121 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
123 case ICBTAG_FLAG_AD_LONG:
124 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
127 spin_unlock(&iinfo->i_extent_cache_lock);
130 void udf_evict_inode(struct inode *inode)
132 struct udf_inode_info *iinfo = UDF_I(inode);
135 if (!is_bad_inode(inode)) {
136 if (!inode->i_nlink) {
138 udf_setsize(inode, 0);
139 udf_update_inode(inode, IS_SYNC(inode));
141 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
142 inode->i_size != iinfo->i_lenExtents) {
143 udf_warn(inode->i_sb,
144 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
145 inode->i_ino, inode->i_mode,
146 (unsigned long long)inode->i_size,
147 (unsigned long long)iinfo->i_lenExtents);
150 truncate_inode_pages_final(&inode->i_data);
151 invalidate_inode_buffers(inode);
153 kfree(iinfo->i_ext.i_data);
154 iinfo->i_ext.i_data = NULL;
155 udf_clear_extent_cache(inode);
157 udf_free_inode(inode);
161 static void udf_write_failed(struct address_space *mapping, loff_t to)
163 struct inode *inode = mapping->host;
164 struct udf_inode_info *iinfo = UDF_I(inode);
165 loff_t isize = inode->i_size;
168 truncate_pagecache(inode, isize);
169 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
170 down_write(&iinfo->i_data_sem);
171 udf_clear_extent_cache(inode);
172 udf_truncate_extents(inode);
173 up_write(&iinfo->i_data_sem);
178 static int udf_writepage(struct page *page, struct writeback_control *wbc)
180 return block_write_full_page(page, udf_get_block, wbc);
183 static int udf_writepages(struct address_space *mapping,
184 struct writeback_control *wbc)
186 return mpage_writepages(mapping, wbc, udf_get_block);
189 static int udf_readpage(struct file *file, struct page *page)
191 return mpage_readpage(page, udf_get_block);
194 static int udf_readpages(struct file *file, struct address_space *mapping,
195 struct list_head *pages, unsigned nr_pages)
197 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
200 static int udf_write_begin(struct file *file, struct address_space *mapping,
201 loff_t pos, unsigned len, unsigned flags,
202 struct page **pagep, void **fsdata)
206 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
208 udf_write_failed(mapping, pos + len);
212 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
214 struct file *file = iocb->ki_filp;
215 struct address_space *mapping = file->f_mapping;
216 struct inode *inode = mapping->host;
217 size_t count = iov_iter_count(iter);
220 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
221 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
222 udf_write_failed(mapping, iocb->ki_pos + count);
226 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
228 return generic_block_bmap(mapping, block, udf_get_block);
231 const struct address_space_operations udf_aops = {
232 .readpage = udf_readpage,
233 .readpages = udf_readpages,
234 .writepage = udf_writepage,
235 .writepages = udf_writepages,
236 .write_begin = udf_write_begin,
237 .write_end = generic_write_end,
238 .direct_IO = udf_direct_IO,
243 * Expand file stored in ICB to a normal one-block-file
245 * This function requires i_data_sem for writing and releases it.
246 * This function requires i_mutex held
248 int udf_expand_file_adinicb(struct inode *inode)
252 struct udf_inode_info *iinfo = UDF_I(inode);
255 WARN_ON_ONCE(!inode_is_locked(inode));
256 if (!iinfo->i_lenAlloc) {
257 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
258 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
260 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
261 /* from now on we have normal address_space methods */
262 inode->i_data.a_ops = &udf_aops;
263 up_write(&iinfo->i_data_sem);
264 mark_inode_dirty(inode);
268 * Release i_data_sem so that we can lock a page - page lock ranks
269 * above i_data_sem. i_mutex still protects us against file changes.
271 up_write(&iinfo->i_data_sem);
273 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
277 if (!PageUptodate(page)) {
278 kaddr = kmap_atomic(page);
279 memset(kaddr + iinfo->i_lenAlloc, 0x00,
280 PAGE_SIZE - iinfo->i_lenAlloc);
281 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
283 flush_dcache_page(page);
284 SetPageUptodate(page);
285 kunmap_atomic(kaddr);
287 down_write(&iinfo->i_data_sem);
288 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
290 iinfo->i_lenAlloc = 0;
291 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
292 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
294 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
295 /* from now on we have normal address_space methods */
296 inode->i_data.a_ops = &udf_aops;
297 set_page_dirty(page);
299 up_write(&iinfo->i_data_sem);
300 err = filemap_fdatawrite(inode->i_mapping);
302 /* Restore everything back so that we don't lose data... */
304 down_write(&iinfo->i_data_sem);
305 kaddr = kmap_atomic(page);
306 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
308 kunmap_atomic(kaddr);
310 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
311 inode->i_data.a_ops = &udf_adinicb_aops;
312 iinfo->i_lenAlloc = inode->i_size;
313 up_write(&iinfo->i_data_sem);
316 mark_inode_dirty(inode);
321 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
325 struct buffer_head *dbh = NULL;
326 struct kernel_lb_addr eloc;
328 struct extent_position epos;
330 struct udf_fileident_bh sfibh, dfibh;
331 loff_t f_pos = udf_ext0_offset(inode);
332 int size = udf_ext0_offset(inode) + inode->i_size;
333 struct fileIdentDesc cfi, *sfi, *dfi;
334 struct udf_inode_info *iinfo = UDF_I(inode);
336 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
337 alloctype = ICBTAG_FLAG_AD_SHORT;
339 alloctype = ICBTAG_FLAG_AD_LONG;
341 if (!inode->i_size) {
342 iinfo->i_alloc_type = alloctype;
343 mark_inode_dirty(inode);
347 /* alloc block, and copy data to it */
348 *block = udf_new_block(inode->i_sb, inode,
349 iinfo->i_location.partitionReferenceNum,
350 iinfo->i_location.logicalBlockNum, err);
353 newblock = udf_get_pblock(inode->i_sb, *block,
354 iinfo->i_location.partitionReferenceNum,
358 dbh = udf_tgetblk(inode->i_sb, newblock);
362 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
363 set_buffer_uptodate(dbh);
365 mark_buffer_dirty_inode(dbh, inode);
367 sfibh.soffset = sfibh.eoffset =
368 f_pos & (inode->i_sb->s_blocksize - 1);
369 sfibh.sbh = sfibh.ebh = NULL;
370 dfibh.soffset = dfibh.eoffset = 0;
371 dfibh.sbh = dfibh.ebh = dbh;
372 while (f_pos < size) {
373 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
374 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
380 iinfo->i_alloc_type = alloctype;
381 sfi->descTag.tagLocation = cpu_to_le32(*block);
382 dfibh.soffset = dfibh.eoffset;
383 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
384 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
385 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
387 le16_to_cpu(sfi->lengthOfImpUse))) {
388 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
393 mark_buffer_dirty_inode(dbh, inode);
395 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
397 iinfo->i_lenAlloc = 0;
398 eloc.logicalBlockNum = *block;
399 eloc.partitionReferenceNum =
400 iinfo->i_location.partitionReferenceNum;
401 iinfo->i_lenExtents = inode->i_size;
403 epos.block = iinfo->i_location;
404 epos.offset = udf_file_entry_alloc_offset(inode);
405 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
409 mark_inode_dirty(inode);
413 static int udf_get_block(struct inode *inode, sector_t block,
414 struct buffer_head *bh_result, int create)
418 struct udf_inode_info *iinfo;
421 phys = udf_block_map(inode, block);
423 map_bh(bh_result, inode->i_sb, phys);
429 iinfo = UDF_I(inode);
431 down_write(&iinfo->i_data_sem);
432 if (block == iinfo->i_next_alloc_block + 1) {
433 iinfo->i_next_alloc_block++;
434 iinfo->i_next_alloc_goal++;
437 udf_clear_extent_cache(inode);
438 phys = inode_getblk(inode, block, &err, &new);
443 set_buffer_new(bh_result);
444 map_bh(bh_result, inode->i_sb, phys);
447 up_write(&iinfo->i_data_sem);
451 static struct buffer_head *udf_getblk(struct inode *inode, long block,
452 int create, int *err)
454 struct buffer_head *bh;
455 struct buffer_head dummy;
458 dummy.b_blocknr = -1000;
459 *err = udf_get_block(inode, block, &dummy, create);
460 if (!*err && buffer_mapped(&dummy)) {
461 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
462 if (buffer_new(&dummy)) {
464 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
465 set_buffer_uptodate(bh);
467 mark_buffer_dirty_inode(bh, inode);
475 /* Extend the file with new blocks totaling 'new_block_bytes',
476 * return the number of extents added
478 static int udf_do_extend_file(struct inode *inode,
479 struct extent_position *last_pos,
480 struct kernel_long_ad *last_ext,
481 loff_t new_block_bytes)
484 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
485 struct super_block *sb = inode->i_sb;
486 struct kernel_lb_addr prealloc_loc = {};
487 int prealloc_len = 0;
488 struct udf_inode_info *iinfo;
491 /* The previous extent is fake and we should not extend by anything
492 * - there's nothing to do... */
493 if (!new_block_bytes && fake)
496 iinfo = UDF_I(inode);
497 /* Round the last extent up to a multiple of block size */
498 if (last_ext->extLength & (sb->s_blocksize - 1)) {
499 last_ext->extLength =
500 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
501 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
502 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
503 iinfo->i_lenExtents =
504 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
505 ~(sb->s_blocksize - 1);
508 /* Last extent are just preallocated blocks? */
509 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
510 EXT_NOT_RECORDED_ALLOCATED) {
511 /* Save the extent so that we can reattach it to the end */
512 prealloc_loc = last_ext->extLocation;
513 prealloc_len = last_ext->extLength;
514 /* Mark the extent as a hole */
515 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
516 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
517 last_ext->extLocation.logicalBlockNum = 0;
518 last_ext->extLocation.partitionReferenceNum = 0;
521 /* Can we merge with the previous extent? */
522 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
523 EXT_NOT_RECORDED_NOT_ALLOCATED) {
524 add = (1 << 30) - sb->s_blocksize -
525 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
526 if (add > new_block_bytes)
527 add = new_block_bytes;
528 new_block_bytes -= add;
529 last_ext->extLength += add;
533 udf_add_aext(inode, last_pos, &last_ext->extLocation,
534 last_ext->extLength, 1);
537 struct kernel_lb_addr tmploc;
540 udf_write_aext(inode, last_pos, &last_ext->extLocation,
541 last_ext->extLength, 1);
544 * We've rewritten the last extent. If we are going to add
545 * more extents, we may need to enter possible following
546 * empty indirect extent.
548 if (new_block_bytes || prealloc_len)
549 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
552 /* Managed to do everything necessary? */
553 if (!new_block_bytes)
556 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
557 last_ext->extLocation.logicalBlockNum = 0;
558 last_ext->extLocation.partitionReferenceNum = 0;
559 add = (1 << 30) - sb->s_blocksize;
560 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
562 /* Create enough extents to cover the whole hole */
563 while (new_block_bytes > add) {
564 new_block_bytes -= add;
565 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
566 last_ext->extLength, 1);
571 if (new_block_bytes) {
572 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
574 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
575 last_ext->extLength, 1);
582 /* Do we have some preallocated blocks saved? */
584 err = udf_add_aext(inode, last_pos, &prealloc_loc,
588 last_ext->extLocation = prealloc_loc;
589 last_ext->extLength = prealloc_len;
593 /* last_pos should point to the last written extent... */
594 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
595 last_pos->offset -= sizeof(struct short_ad);
596 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
597 last_pos->offset -= sizeof(struct long_ad);
604 /* Extend the final block of the file to final_block_len bytes */
605 static void udf_do_extend_final_block(struct inode *inode,
606 struct extent_position *last_pos,
607 struct kernel_long_ad *last_ext,
608 uint32_t final_block_len)
610 struct super_block *sb = inode->i_sb;
611 uint32_t added_bytes;
613 added_bytes = final_block_len -
614 (last_ext->extLength & (sb->s_blocksize - 1));
615 last_ext->extLength += added_bytes;
616 UDF_I(inode)->i_lenExtents += added_bytes;
618 udf_write_aext(inode, last_pos, &last_ext->extLocation,
619 last_ext->extLength, 1);
622 static int udf_extend_file(struct inode *inode, loff_t newsize)
625 struct extent_position epos;
626 struct kernel_lb_addr eloc;
629 struct super_block *sb = inode->i_sb;
630 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
631 unsigned long partial_final_block;
633 struct udf_inode_info *iinfo = UDF_I(inode);
634 struct kernel_long_ad extent;
636 int within_final_block;
638 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
639 adsize = sizeof(struct short_ad);
640 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
641 adsize = sizeof(struct long_ad);
645 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
646 within_final_block = (etype != -1);
648 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
649 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
650 /* File has no extents at all or has empty last
651 * indirect extent! Create a fake extent... */
652 extent.extLocation.logicalBlockNum = 0;
653 extent.extLocation.partitionReferenceNum = 0;
654 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
656 epos.offset -= adsize;
657 etype = udf_next_aext(inode, &epos, &extent.extLocation,
658 &extent.extLength, 0);
659 extent.extLength |= etype << 30;
662 partial_final_block = newsize & (sb->s_blocksize - 1);
664 /* File has extent covering the new size (could happen when extending
667 if (within_final_block) {
668 /* Extending file within the last file block */
669 udf_do_extend_final_block(inode, &epos, &extent,
670 partial_final_block);
672 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
674 err = udf_do_extend_file(inode, &epos, &extent, add);
680 iinfo->i_lenExtents = newsize;
686 static sector_t inode_getblk(struct inode *inode, sector_t block,
689 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
690 struct extent_position prev_epos, cur_epos, next_epos;
691 int count = 0, startnum = 0, endnum = 0;
692 uint32_t elen = 0, tmpelen;
693 struct kernel_lb_addr eloc, tmpeloc;
695 loff_t lbcount = 0, b_off = 0;
696 uint32_t newblocknum, newblock;
699 struct udf_inode_info *iinfo = UDF_I(inode);
700 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
706 prev_epos.offset = udf_file_entry_alloc_offset(inode);
707 prev_epos.block = iinfo->i_location;
709 cur_epos = next_epos = prev_epos;
710 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
712 /* find the extent which contains the block we are looking for.
713 alternate between laarr[0] and laarr[1] for locations of the
714 current extent, and the previous extent */
716 if (prev_epos.bh != cur_epos.bh) {
717 brelse(prev_epos.bh);
719 prev_epos.bh = cur_epos.bh;
721 if (cur_epos.bh != next_epos.bh) {
723 get_bh(next_epos.bh);
724 cur_epos.bh = next_epos.bh;
729 prev_epos.block = cur_epos.block;
730 cur_epos.block = next_epos.block;
732 prev_epos.offset = cur_epos.offset;
733 cur_epos.offset = next_epos.offset;
735 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
741 laarr[c].extLength = (etype << 30) | elen;
742 laarr[c].extLocation = eloc;
744 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
745 pgoal = eloc.logicalBlockNum +
746 ((elen + inode->i_sb->s_blocksize - 1) >>
747 inode->i_sb->s_blocksize_bits);
750 } while (lbcount + elen <= b_off);
753 offset = b_off >> inode->i_sb->s_blocksize_bits;
755 * Move prev_epos and cur_epos into indirect extent if we are at
758 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
759 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
761 /* if the extent is allocated and recorded, return the block
762 if the extent is not a multiple of the blocksize, round up */
764 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
765 if (elen & (inode->i_sb->s_blocksize - 1)) {
766 elen = EXT_RECORDED_ALLOCATED |
767 ((elen + inode->i_sb->s_blocksize - 1) &
768 ~(inode->i_sb->s_blocksize - 1));
769 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
771 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
775 /* Are we beyond EOF? */
785 /* Create a fake extent when there's not one */
786 memset(&laarr[0].extLocation, 0x00,
787 sizeof(struct kernel_lb_addr));
788 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
789 /* Will udf_do_extend_file() create real extent from
791 startnum = (offset > 0);
793 /* Create extents for the hole between EOF and offset */
794 hole_len = (loff_t)offset << inode->i_blkbits;
795 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
804 /* We are not covered by a preallocated extent? */
805 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
806 EXT_NOT_RECORDED_ALLOCATED) {
807 /* Is there any real extent? - otherwise we overwrite
811 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
812 inode->i_sb->s_blocksize;
813 memset(&laarr[c].extLocation, 0x00,
814 sizeof(struct kernel_lb_addr));
821 endnum = startnum = ((count > 2) ? 2 : count);
823 /* if the current extent is in position 0,
824 swap it with the previous */
825 if (!c && count != 1) {
832 /* if the current block is located in an extent,
833 read the next extent */
834 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
836 laarr[c + 1].extLength = (etype << 30) | elen;
837 laarr[c + 1].extLocation = eloc;
845 /* if the current extent is not recorded but allocated, get the
846 * block in the extent corresponding to the requested block */
847 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
848 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
849 else { /* otherwise, allocate a new block */
850 if (iinfo->i_next_alloc_block == block)
851 goal = iinfo->i_next_alloc_goal;
854 if (!(goal = pgoal)) /* XXX: what was intended here? */
855 goal = iinfo->i_location.logicalBlockNum + 1;
858 newblocknum = udf_new_block(inode->i_sb, inode,
859 iinfo->i_location.partitionReferenceNum,
867 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
870 /* if the extent the requsted block is located in contains multiple
871 * blocks, split the extent into at most three extents. blocks prior
872 * to requested block, requested block, and blocks after requested
874 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
876 /* We preallocate blocks only for regular files. It also makes sense
877 * for directories but there's a problem when to drop the
878 * preallocation. We might use some delayed work for that but I feel
879 * it's overengineering for a filesystem like UDF. */
880 if (S_ISREG(inode->i_mode))
881 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
883 /* merge any continuous blocks in laarr */
884 udf_merge_extents(inode, laarr, &endnum);
886 /* write back the new extents, inserting new extents if the new number
887 * of extents is greater than the old number, and deleting extents if
888 * the new number of extents is less than the old number */
889 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
891 newblock = udf_get_pblock(inode->i_sb, newblocknum,
892 iinfo->i_location.partitionReferenceNum, 0);
898 iinfo->i_next_alloc_block = block;
899 iinfo->i_next_alloc_goal = newblocknum;
900 inode->i_ctime = current_time(inode);
903 udf_sync_inode(inode);
905 mark_inode_dirty(inode);
907 brelse(prev_epos.bh);
909 brelse(next_epos.bh);
913 static void udf_split_extents(struct inode *inode, int *c, int offset,
914 int newblocknum, struct kernel_long_ad *laarr,
917 unsigned long blocksize = inode->i_sb->s_blocksize;
918 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
920 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
921 (laarr[*c].extLength >> 30) ==
922 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
924 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
925 blocksize - 1) >> blocksize_bits;
926 int8_t etype = (laarr[curr].extLength >> 30);
930 else if (!offset || blen == offset + 1) {
931 laarr[curr + 2] = laarr[curr + 1];
932 laarr[curr + 1] = laarr[curr];
934 laarr[curr + 3] = laarr[curr + 1];
935 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
939 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
940 udf_free_blocks(inode->i_sb, inode,
941 &laarr[curr].extLocation,
943 laarr[curr].extLength =
944 EXT_NOT_RECORDED_NOT_ALLOCATED |
945 (offset << blocksize_bits);
946 laarr[curr].extLocation.logicalBlockNum = 0;
947 laarr[curr].extLocation.
948 partitionReferenceNum = 0;
950 laarr[curr].extLength = (etype << 30) |
951 (offset << blocksize_bits);
957 laarr[curr].extLocation.logicalBlockNum = newblocknum;
958 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
959 laarr[curr].extLocation.partitionReferenceNum =
960 UDF_I(inode)->i_location.partitionReferenceNum;
961 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
965 if (blen != offset + 1) {
966 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
967 laarr[curr].extLocation.logicalBlockNum +=
969 laarr[curr].extLength = (etype << 30) |
970 ((blen - (offset + 1)) << blocksize_bits);
977 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
978 struct kernel_long_ad *laarr,
981 int start, length = 0, currlength = 0, i;
983 if (*endnum >= (c + 1)) {
989 if ((laarr[c + 1].extLength >> 30) ==
990 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
992 length = currlength =
993 (((laarr[c + 1].extLength &
994 UDF_EXTENT_LENGTH_MASK) +
995 inode->i_sb->s_blocksize - 1) >>
996 inode->i_sb->s_blocksize_bits);
1001 for (i = start + 1; i <= *endnum; i++) {
1004 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1005 } else if ((laarr[i].extLength >> 30) ==
1006 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1007 length += (((laarr[i].extLength &
1008 UDF_EXTENT_LENGTH_MASK) +
1009 inode->i_sb->s_blocksize - 1) >>
1010 inode->i_sb->s_blocksize_bits);
1016 int next = laarr[start].extLocation.logicalBlockNum +
1017 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1018 inode->i_sb->s_blocksize - 1) >>
1019 inode->i_sb->s_blocksize_bits);
1020 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1021 laarr[start].extLocation.partitionReferenceNum,
1022 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1023 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1026 if (start == (c + 1))
1027 laarr[start].extLength +=
1029 inode->i_sb->s_blocksize_bits);
1031 memmove(&laarr[c + 2], &laarr[c + 1],
1032 sizeof(struct long_ad) * (*endnum - (c + 1)));
1034 laarr[c + 1].extLocation.logicalBlockNum = next;
1035 laarr[c + 1].extLocation.partitionReferenceNum =
1036 laarr[c].extLocation.
1037 partitionReferenceNum;
1038 laarr[c + 1].extLength =
1039 EXT_NOT_RECORDED_ALLOCATED |
1041 inode->i_sb->s_blocksize_bits);
1045 for (i = start + 1; numalloc && i < *endnum; i++) {
1046 int elen = ((laarr[i].extLength &
1047 UDF_EXTENT_LENGTH_MASK) +
1048 inode->i_sb->s_blocksize - 1) >>
1049 inode->i_sb->s_blocksize_bits;
1051 if (elen > numalloc) {
1052 laarr[i].extLength -=
1054 inode->i_sb->s_blocksize_bits);
1058 if (*endnum > (i + 1))
1061 sizeof(struct long_ad) *
1062 (*endnum - (i + 1)));
1067 UDF_I(inode)->i_lenExtents +=
1068 numalloc << inode->i_sb->s_blocksize_bits;
1073 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1077 unsigned long blocksize = inode->i_sb->s_blocksize;
1078 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1080 for (i = 0; i < (*endnum - 1); i++) {
1081 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1082 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1084 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1085 (((li->extLength >> 30) ==
1086 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1087 ((lip1->extLocation.logicalBlockNum -
1088 li->extLocation.logicalBlockNum) ==
1089 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1090 blocksize - 1) >> blocksize_bits)))) {
1092 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1093 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1094 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1095 lip1->extLength = (lip1->extLength -
1097 UDF_EXTENT_LENGTH_MASK) +
1098 UDF_EXTENT_LENGTH_MASK) &
1100 li->extLength = (li->extLength &
1101 UDF_EXTENT_FLAG_MASK) +
1102 (UDF_EXTENT_LENGTH_MASK + 1) -
1104 lip1->extLocation.logicalBlockNum =
1105 li->extLocation.logicalBlockNum +
1107 UDF_EXTENT_LENGTH_MASK) >>
1110 li->extLength = lip1->extLength +
1112 UDF_EXTENT_LENGTH_MASK) +
1113 blocksize - 1) & ~(blocksize - 1));
1114 if (*endnum > (i + 2))
1115 memmove(&laarr[i + 1], &laarr[i + 2],
1116 sizeof(struct long_ad) *
1117 (*endnum - (i + 2)));
1121 } else if (((li->extLength >> 30) ==
1122 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1123 ((lip1->extLength >> 30) ==
1124 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1125 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1127 UDF_EXTENT_LENGTH_MASK) +
1128 blocksize - 1) >> blocksize_bits);
1129 li->extLocation.logicalBlockNum = 0;
1130 li->extLocation.partitionReferenceNum = 0;
1132 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1133 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1134 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1135 lip1->extLength = (lip1->extLength -
1137 UDF_EXTENT_LENGTH_MASK) +
1138 UDF_EXTENT_LENGTH_MASK) &
1140 li->extLength = (li->extLength &
1141 UDF_EXTENT_FLAG_MASK) +
1142 (UDF_EXTENT_LENGTH_MASK + 1) -
1145 li->extLength = lip1->extLength +
1147 UDF_EXTENT_LENGTH_MASK) +
1148 blocksize - 1) & ~(blocksize - 1));
1149 if (*endnum > (i + 2))
1150 memmove(&laarr[i + 1], &laarr[i + 2],
1151 sizeof(struct long_ad) *
1152 (*endnum - (i + 2)));
1156 } else if ((li->extLength >> 30) ==
1157 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1158 udf_free_blocks(inode->i_sb, inode,
1159 &li->extLocation, 0,
1161 UDF_EXTENT_LENGTH_MASK) +
1162 blocksize - 1) >> blocksize_bits);
1163 li->extLocation.logicalBlockNum = 0;
1164 li->extLocation.partitionReferenceNum = 0;
1165 li->extLength = (li->extLength &
1166 UDF_EXTENT_LENGTH_MASK) |
1167 EXT_NOT_RECORDED_NOT_ALLOCATED;
1172 static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1173 int startnum, int endnum,
1174 struct extent_position *epos)
1177 struct kernel_lb_addr tmploc;
1180 if (startnum > endnum) {
1181 for (i = 0; i < (startnum - endnum); i++)
1182 udf_delete_aext(inode, *epos, laarr[i].extLocation,
1183 laarr[i].extLength);
1184 } else if (startnum < endnum) {
1185 for (i = 0; i < (endnum - startnum); i++) {
1186 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1187 laarr[i].extLength);
1188 udf_next_aext(inode, epos, &laarr[i].extLocation,
1189 &laarr[i].extLength, 1);
1194 for (i = start; i < endnum; i++) {
1195 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1196 udf_write_aext(inode, epos, &laarr[i].extLocation,
1197 laarr[i].extLength, 1);
1201 struct buffer_head *udf_bread(struct inode *inode, int block,
1202 int create, int *err)
1204 struct buffer_head *bh = NULL;
1206 bh = udf_getblk(inode, block, create, err);
1210 if (buffer_uptodate(bh))
1213 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1216 if (buffer_uptodate(bh))
1224 int udf_setsize(struct inode *inode, loff_t newsize)
1227 struct udf_inode_info *iinfo;
1228 int bsize = i_blocksize(inode);
1230 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1231 S_ISLNK(inode->i_mode)))
1233 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1236 iinfo = UDF_I(inode);
1237 if (newsize > inode->i_size) {
1238 down_write(&iinfo->i_data_sem);
1239 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1241 (udf_file_entry_alloc_offset(inode) + newsize)) {
1242 err = udf_expand_file_adinicb(inode);
1245 down_write(&iinfo->i_data_sem);
1247 iinfo->i_lenAlloc = newsize;
1251 err = udf_extend_file(inode, newsize);
1253 up_write(&iinfo->i_data_sem);
1257 up_write(&iinfo->i_data_sem);
1258 truncate_setsize(inode, newsize);
1260 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1261 down_write(&iinfo->i_data_sem);
1262 udf_clear_extent_cache(inode);
1263 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1264 0x00, bsize - newsize -
1265 udf_file_entry_alloc_offset(inode));
1266 iinfo->i_lenAlloc = newsize;
1267 truncate_setsize(inode, newsize);
1268 up_write(&iinfo->i_data_sem);
1271 err = block_truncate_page(inode->i_mapping, newsize,
1275 truncate_setsize(inode, newsize);
1276 down_write(&iinfo->i_data_sem);
1277 udf_clear_extent_cache(inode);
1278 udf_truncate_extents(inode);
1279 up_write(&iinfo->i_data_sem);
1282 inode->i_mtime = inode->i_ctime = current_time(inode);
1284 udf_sync_inode(inode);
1286 mark_inode_dirty(inode);
1291 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1292 * arbitrary - just that we hopefully don't limit any real use of rewritten
1293 * inode on write-once media but avoid looping for too long on corrupted media.
1295 #define UDF_MAX_ICB_NESTING 1024
1297 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1299 struct buffer_head *bh = NULL;
1300 struct fileEntry *fe;
1301 struct extendedFileEntry *efe;
1303 struct udf_inode_info *iinfo = UDF_I(inode);
1304 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1305 struct kernel_lb_addr *iloc = &iinfo->i_location;
1306 unsigned int link_count;
1307 unsigned int indirections = 0;
1308 int bs = inode->i_sb->s_blocksize;
1312 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1313 udf_debug("partition reference: %d > logical volume partitions: %d\n",
1314 iloc->partitionReferenceNum, sbi->s_partitions);
1318 if (iloc->logicalBlockNum >=
1319 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1320 udf_debug("block=%d, partition=%d out of range\n",
1321 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1326 * Set defaults, but the inode is still incomplete!
1327 * Note: get_new_inode() sets the following on a new inode:
1330 * i_flags = sb->s_flags
1332 * clean_inode(): zero fills and sets
1337 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1339 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1343 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1344 ident != TAG_IDENT_USE) {
1345 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1346 inode->i_ino, ident);
1350 fe = (struct fileEntry *)bh->b_data;
1351 efe = (struct extendedFileEntry *)bh->b_data;
1353 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1354 struct buffer_head *ibh;
1356 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1357 if (ident == TAG_IDENT_IE && ibh) {
1358 struct kernel_lb_addr loc;
1359 struct indirectEntry *ie;
1361 ie = (struct indirectEntry *)ibh->b_data;
1362 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1364 if (ie->indirectICB.extLength) {
1366 memcpy(&iinfo->i_location, &loc,
1367 sizeof(struct kernel_lb_addr));
1368 if (++indirections > UDF_MAX_ICB_NESTING) {
1369 udf_err(inode->i_sb,
1370 "too many ICBs in ICB hierarchy"
1371 " (max %d supported)\n",
1372 UDF_MAX_ICB_NESTING);
1380 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1381 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1382 le16_to_cpu(fe->icbTag.strategyType));
1385 if (fe->icbTag.strategyType == cpu_to_le16(4))
1386 iinfo->i_strat4096 = 0;
1387 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1388 iinfo->i_strat4096 = 1;
1390 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1391 ICBTAG_FLAG_AD_MASK;
1392 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1393 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1394 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1398 iinfo->i_unique = 0;
1399 iinfo->i_lenEAttr = 0;
1400 iinfo->i_lenExtents = 0;
1401 iinfo->i_lenAlloc = 0;
1402 iinfo->i_next_alloc_block = 0;
1403 iinfo->i_next_alloc_goal = 0;
1404 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1407 ret = udf_alloc_i_data(inode, bs -
1408 sizeof(struct extendedFileEntry));
1411 memcpy(iinfo->i_ext.i_data,
1412 bh->b_data + sizeof(struct extendedFileEntry),
1413 bs - sizeof(struct extendedFileEntry));
1414 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1417 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1420 memcpy(iinfo->i_ext.i_data,
1421 bh->b_data + sizeof(struct fileEntry),
1422 bs - sizeof(struct fileEntry));
1423 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1426 iinfo->i_lenAlloc = le32_to_cpu(
1427 ((struct unallocSpaceEntry *)bh->b_data)->
1429 ret = udf_alloc_i_data(inode, bs -
1430 sizeof(struct unallocSpaceEntry));
1433 memcpy(iinfo->i_ext.i_data,
1434 bh->b_data + sizeof(struct unallocSpaceEntry),
1435 bs - sizeof(struct unallocSpaceEntry));
1440 read_lock(&sbi->s_cred_lock);
1441 i_uid_write(inode, le32_to_cpu(fe->uid));
1442 if (!uid_valid(inode->i_uid) ||
1443 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1444 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1445 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1447 i_gid_write(inode, le32_to_cpu(fe->gid));
1448 if (!gid_valid(inode->i_gid) ||
1449 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1450 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1451 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1453 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1454 sbi->s_fmode != UDF_INVALID_MODE)
1455 inode->i_mode = sbi->s_fmode;
1456 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1457 sbi->s_dmode != UDF_INVALID_MODE)
1458 inode->i_mode = sbi->s_dmode;
1460 inode->i_mode = udf_convert_permissions(fe);
1461 inode->i_mode &= ~sbi->s_umask;
1462 read_unlock(&sbi->s_cred_lock);
1464 link_count = le16_to_cpu(fe->fileLinkCount);
1466 if (!hidden_inode) {
1472 set_nlink(inode, link_count);
1474 inode->i_size = le64_to_cpu(fe->informationLength);
1475 iinfo->i_lenExtents = inode->i_size;
1477 if (iinfo->i_efe == 0) {
1478 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1479 (inode->i_sb->s_blocksize_bits - 9);
1481 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1482 inode->i_atime = sbi->s_record_time;
1484 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1485 fe->modificationTime))
1486 inode->i_mtime = sbi->s_record_time;
1488 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1489 inode->i_ctime = sbi->s_record_time;
1491 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1492 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1493 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1494 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1496 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1497 (inode->i_sb->s_blocksize_bits - 9);
1499 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1500 inode->i_atime = sbi->s_record_time;
1502 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1503 efe->modificationTime))
1504 inode->i_mtime = sbi->s_record_time;
1506 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1507 iinfo->i_crtime = sbi->s_record_time;
1509 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1510 inode->i_ctime = sbi->s_record_time;
1512 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1513 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1514 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1515 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1517 inode->i_generation = iinfo->i_unique;
1520 * Sanity check length of allocation descriptors and extended attrs to
1521 * avoid integer overflows
1523 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1525 /* Now do exact checks */
1526 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1528 /* Sanity checks for files in ICB so that we don't get confused later */
1529 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1531 * For file in ICB data is stored in allocation descriptor
1532 * so sizes should match
1534 if (iinfo->i_lenAlloc != inode->i_size)
1536 /* File in ICB has to fit in there... */
1537 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1541 switch (fe->icbTag.fileType) {
1542 case ICBTAG_FILE_TYPE_DIRECTORY:
1543 inode->i_op = &udf_dir_inode_operations;
1544 inode->i_fop = &udf_dir_operations;
1545 inode->i_mode |= S_IFDIR;
1548 case ICBTAG_FILE_TYPE_REALTIME:
1549 case ICBTAG_FILE_TYPE_REGULAR:
1550 case ICBTAG_FILE_TYPE_UNDEF:
1551 case ICBTAG_FILE_TYPE_VAT20:
1552 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1553 inode->i_data.a_ops = &udf_adinicb_aops;
1555 inode->i_data.a_ops = &udf_aops;
1556 inode->i_op = &udf_file_inode_operations;
1557 inode->i_fop = &udf_file_operations;
1558 inode->i_mode |= S_IFREG;
1560 case ICBTAG_FILE_TYPE_BLOCK:
1561 inode->i_mode |= S_IFBLK;
1563 case ICBTAG_FILE_TYPE_CHAR:
1564 inode->i_mode |= S_IFCHR;
1566 case ICBTAG_FILE_TYPE_FIFO:
1567 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1569 case ICBTAG_FILE_TYPE_SOCKET:
1570 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1572 case ICBTAG_FILE_TYPE_SYMLINK:
1573 inode->i_data.a_ops = &udf_symlink_aops;
1574 inode->i_op = &udf_symlink_inode_operations;
1575 inode_nohighmem(inode);
1576 inode->i_mode = S_IFLNK | 0777;
1578 case ICBTAG_FILE_TYPE_MAIN:
1579 udf_debug("METADATA FILE-----\n");
1581 case ICBTAG_FILE_TYPE_MIRROR:
1582 udf_debug("METADATA MIRROR FILE-----\n");
1584 case ICBTAG_FILE_TYPE_BITMAP:
1585 udf_debug("METADATA BITMAP FILE-----\n");
1588 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1589 inode->i_ino, fe->icbTag.fileType);
1592 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1593 struct deviceSpec *dsea =
1594 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1596 init_special_inode(inode, inode->i_mode,
1597 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1598 le32_to_cpu(dsea->minorDeviceIdent)));
1599 /* Developer ID ??? */
1609 static int udf_alloc_i_data(struct inode *inode, size_t size)
1611 struct udf_inode_info *iinfo = UDF_I(inode);
1612 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1613 if (!iinfo->i_ext.i_data)
1618 static umode_t udf_convert_permissions(struct fileEntry *fe)
1621 uint32_t permissions;
1624 permissions = le32_to_cpu(fe->permissions);
1625 flags = le16_to_cpu(fe->icbTag.flags);
1627 mode = ((permissions) & 0007) |
1628 ((permissions >> 2) & 0070) |
1629 ((permissions >> 4) & 0700) |
1630 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1631 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1632 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1637 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1639 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1642 static int udf_sync_inode(struct inode *inode)
1644 return udf_update_inode(inode, 1);
1647 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
1649 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1650 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1651 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1652 iinfo->i_crtime = time;
1655 static int udf_update_inode(struct inode *inode, int do_sync)
1657 struct buffer_head *bh = NULL;
1658 struct fileEntry *fe;
1659 struct extendedFileEntry *efe;
1660 uint64_t lb_recorded;
1665 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1666 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1667 struct udf_inode_info *iinfo = UDF_I(inode);
1669 bh = udf_tgetblk(inode->i_sb,
1670 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1672 udf_debug("getblk failure\n");
1677 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1678 fe = (struct fileEntry *)bh->b_data;
1679 efe = (struct extendedFileEntry *)bh->b_data;
1682 struct unallocSpaceEntry *use =
1683 (struct unallocSpaceEntry *)bh->b_data;
1685 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1686 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1687 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1688 sizeof(struct unallocSpaceEntry));
1689 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1690 crclen = sizeof(struct unallocSpaceEntry);
1695 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1696 fe->uid = cpu_to_le32(-1);
1698 fe->uid = cpu_to_le32(i_uid_read(inode));
1700 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1701 fe->gid = cpu_to_le32(-1);
1703 fe->gid = cpu_to_le32(i_gid_read(inode));
1705 udfperms = ((inode->i_mode & 0007)) |
1706 ((inode->i_mode & 0070) << 2) |
1707 ((inode->i_mode & 0700) << 4);
1709 udfperms |= (le32_to_cpu(fe->permissions) &
1710 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1711 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1712 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1713 fe->permissions = cpu_to_le32(udfperms);
1715 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1716 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1718 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1720 fe->informationLength = cpu_to_le64(inode->i_size);
1722 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1724 struct deviceSpec *dsea =
1725 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1727 dsea = (struct deviceSpec *)
1728 udf_add_extendedattr(inode,
1729 sizeof(struct deviceSpec) +
1730 sizeof(struct regid), 12, 0x3);
1731 dsea->attrType = cpu_to_le32(12);
1732 dsea->attrSubtype = 1;
1733 dsea->attrLength = cpu_to_le32(
1734 sizeof(struct deviceSpec) +
1735 sizeof(struct regid));
1736 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1738 eid = (struct regid *)dsea->impUse;
1739 memset(eid, 0, sizeof(*eid));
1740 strcpy(eid->ident, UDF_ID_DEVELOPER);
1741 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1742 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1743 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1744 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1747 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1748 lb_recorded = 0; /* No extents => no blocks! */
1751 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1752 (blocksize_bits - 9);
1754 if (iinfo->i_efe == 0) {
1755 memcpy(bh->b_data + sizeof(struct fileEntry),
1756 iinfo->i_ext.i_data,
1757 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1758 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1760 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1761 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1762 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1763 memset(&(fe->impIdent), 0, sizeof(struct regid));
1764 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1765 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1766 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1767 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1768 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1769 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1770 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1771 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1772 crclen = sizeof(struct fileEntry);
1774 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1775 iinfo->i_ext.i_data,
1776 inode->i_sb->s_blocksize -
1777 sizeof(struct extendedFileEntry));
1778 efe->objectSize = cpu_to_le64(inode->i_size);
1779 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1781 udf_adjust_time(iinfo, inode->i_atime);
1782 udf_adjust_time(iinfo, inode->i_mtime);
1783 udf_adjust_time(iinfo, inode->i_ctime);
1785 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1786 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1787 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1788 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1790 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1791 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1792 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1793 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1794 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1795 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1796 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1797 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1798 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1799 crclen = sizeof(struct extendedFileEntry);
1803 if (iinfo->i_strat4096) {
1804 fe->icbTag.strategyType = cpu_to_le16(4096);
1805 fe->icbTag.strategyParameter = cpu_to_le16(1);
1806 fe->icbTag.numEntries = cpu_to_le16(2);
1808 fe->icbTag.strategyType = cpu_to_le16(4);
1809 fe->icbTag.numEntries = cpu_to_le16(1);
1813 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1814 else if (S_ISDIR(inode->i_mode))
1815 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1816 else if (S_ISREG(inode->i_mode))
1817 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1818 else if (S_ISLNK(inode->i_mode))
1819 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1820 else if (S_ISBLK(inode->i_mode))
1821 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1822 else if (S_ISCHR(inode->i_mode))
1823 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1824 else if (S_ISFIFO(inode->i_mode))
1825 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1826 else if (S_ISSOCK(inode->i_mode))
1827 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1829 icbflags = iinfo->i_alloc_type |
1830 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1831 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1832 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1833 (le16_to_cpu(fe->icbTag.flags) &
1834 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1835 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1837 fe->icbTag.flags = cpu_to_le16(icbflags);
1838 if (sbi->s_udfrev >= 0x0200)
1839 fe->descTag.descVersion = cpu_to_le16(3);
1841 fe->descTag.descVersion = cpu_to_le16(2);
1842 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1843 fe->descTag.tagLocation = cpu_to_le32(
1844 iinfo->i_location.logicalBlockNum);
1845 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1846 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1847 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1849 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1851 set_buffer_uptodate(bh);
1854 /* write the data blocks */
1855 mark_buffer_dirty(bh);
1857 sync_dirty_buffer(bh);
1858 if (buffer_write_io_error(bh)) {
1859 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1869 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1872 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1873 struct inode *inode = iget_locked(sb, block);
1877 return ERR_PTR(-ENOMEM);
1879 if (!(inode->i_state & I_NEW))
1882 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1883 err = udf_read_inode(inode, hidden_inode);
1886 return ERR_PTR(err);
1888 unlock_new_inode(inode);
1893 int udf_setup_indirect_aext(struct inode *inode, int block,
1894 struct extent_position *epos)
1896 struct super_block *sb = inode->i_sb;
1897 struct buffer_head *bh;
1898 struct allocExtDesc *aed;
1899 struct extent_position nepos;
1900 struct kernel_lb_addr neloc;
1903 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1904 adsize = sizeof(struct short_ad);
1905 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1906 adsize = sizeof(struct long_ad);
1910 neloc.logicalBlockNum = block;
1911 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1913 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1917 memset(bh->b_data, 0x00, sb->s_blocksize);
1918 set_buffer_uptodate(bh);
1920 mark_buffer_dirty_inode(bh, inode);
1922 aed = (struct allocExtDesc *)(bh->b_data);
1923 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1924 aed->previousAllocExtLocation =
1925 cpu_to_le32(epos->block.logicalBlockNum);
1927 aed->lengthAllocDescs = cpu_to_le32(0);
1928 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1932 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1933 sizeof(struct tag));
1935 nepos.block = neloc;
1936 nepos.offset = sizeof(struct allocExtDesc);
1940 * Do we have to copy current last extent to make space for indirect
1943 if (epos->offset + adsize > sb->s_blocksize) {
1944 struct kernel_lb_addr cp_loc;
1948 epos->offset -= adsize;
1949 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1950 cp_len |= ((uint32_t)cp_type) << 30;
1952 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1953 udf_write_aext(inode, epos, &nepos.block,
1954 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1956 __udf_add_aext(inode, epos, &nepos.block,
1957 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1967 * Append extent at the given position - should be the first free one in inode
1968 * / indirect extent. This function assumes there is enough space in the inode
1969 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1971 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1972 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1974 struct udf_inode_info *iinfo = UDF_I(inode);
1975 struct allocExtDesc *aed;
1978 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1979 adsize = sizeof(struct short_ad);
1980 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1981 adsize = sizeof(struct long_ad);
1986 WARN_ON(iinfo->i_lenAlloc !=
1987 epos->offset - udf_file_entry_alloc_offset(inode));
1989 aed = (struct allocExtDesc *)epos->bh->b_data;
1990 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1991 epos->offset - sizeof(struct allocExtDesc));
1992 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1995 udf_write_aext(inode, epos, eloc, elen, inc);
1998 iinfo->i_lenAlloc += adsize;
1999 mark_inode_dirty(inode);
2001 aed = (struct allocExtDesc *)epos->bh->b_data;
2002 le32_add_cpu(&aed->lengthAllocDescs, adsize);
2003 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2004 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2005 udf_update_tag(epos->bh->b_data,
2006 epos->offset + (inc ? 0 : adsize));
2008 udf_update_tag(epos->bh->b_data,
2009 sizeof(struct allocExtDesc));
2010 mark_buffer_dirty_inode(epos->bh, inode);
2017 * Append extent at given position - should be the first free one in inode
2018 * / indirect extent. Takes care of allocating and linking indirect blocks.
2020 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2021 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2024 struct super_block *sb = inode->i_sb;
2026 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2027 adsize = sizeof(struct short_ad);
2028 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2029 adsize = sizeof(struct long_ad);
2033 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2037 new_block = udf_new_block(sb, NULL,
2038 epos->block.partitionReferenceNum,
2039 epos->block.logicalBlockNum, &err);
2043 err = udf_setup_indirect_aext(inode, new_block, epos);
2048 return __udf_add_aext(inode, epos, eloc, elen, inc);
2051 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2052 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2056 struct short_ad *sad;
2057 struct long_ad *lad;
2058 struct udf_inode_info *iinfo = UDF_I(inode);
2061 ptr = iinfo->i_ext.i_data + epos->offset -
2062 udf_file_entry_alloc_offset(inode) +
2065 ptr = epos->bh->b_data + epos->offset;
2067 switch (iinfo->i_alloc_type) {
2068 case ICBTAG_FLAG_AD_SHORT:
2069 sad = (struct short_ad *)ptr;
2070 sad->extLength = cpu_to_le32(elen);
2071 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2072 adsize = sizeof(struct short_ad);
2074 case ICBTAG_FLAG_AD_LONG:
2075 lad = (struct long_ad *)ptr;
2076 lad->extLength = cpu_to_le32(elen);
2077 lad->extLocation = cpu_to_lelb(*eloc);
2078 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2079 adsize = sizeof(struct long_ad);
2086 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2087 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2088 struct allocExtDesc *aed =
2089 (struct allocExtDesc *)epos->bh->b_data;
2090 udf_update_tag(epos->bh->b_data,
2091 le32_to_cpu(aed->lengthAllocDescs) +
2092 sizeof(struct allocExtDesc));
2094 mark_buffer_dirty_inode(epos->bh, inode);
2096 mark_inode_dirty(inode);
2100 epos->offset += adsize;
2104 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2105 * someone does some weird stuff.
2107 #define UDF_MAX_INDIR_EXTS 16
2109 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2110 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2113 unsigned int indirections = 0;
2115 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2116 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2119 if (++indirections > UDF_MAX_INDIR_EXTS) {
2120 udf_err(inode->i_sb,
2121 "too many indirect extents in inode %lu\n",
2126 epos->block = *eloc;
2127 epos->offset = sizeof(struct allocExtDesc);
2129 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2130 epos->bh = udf_tread(inode->i_sb, block);
2132 udf_debug("reading block %d failed!\n", block);
2140 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2141 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2146 struct short_ad *sad;
2147 struct long_ad *lad;
2148 struct udf_inode_info *iinfo = UDF_I(inode);
2152 epos->offset = udf_file_entry_alloc_offset(inode);
2153 ptr = iinfo->i_ext.i_data + epos->offset -
2154 udf_file_entry_alloc_offset(inode) +
2156 alen = udf_file_entry_alloc_offset(inode) +
2160 epos->offset = sizeof(struct allocExtDesc);
2161 ptr = epos->bh->b_data + epos->offset;
2162 alen = sizeof(struct allocExtDesc) +
2163 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2167 switch (iinfo->i_alloc_type) {
2168 case ICBTAG_FLAG_AD_SHORT:
2169 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2172 etype = le32_to_cpu(sad->extLength) >> 30;
2173 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2174 eloc->partitionReferenceNum =
2175 iinfo->i_location.partitionReferenceNum;
2176 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2178 case ICBTAG_FLAG_AD_LONG:
2179 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2182 etype = le32_to_cpu(lad->extLength) >> 30;
2183 *eloc = lelb_to_cpu(lad->extLocation);
2184 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2187 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2194 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2195 struct kernel_lb_addr neloc, uint32_t nelen)
2197 struct kernel_lb_addr oeloc;
2204 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2205 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2207 nelen = (etype << 30) | oelen;
2209 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2212 return (nelen >> 30);
2215 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2216 struct kernel_lb_addr eloc, uint32_t elen)
2218 struct extent_position oepos;
2221 struct allocExtDesc *aed;
2222 struct udf_inode_info *iinfo;
2229 iinfo = UDF_I(inode);
2230 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2231 adsize = sizeof(struct short_ad);
2232 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2233 adsize = sizeof(struct long_ad);
2238 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2241 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2242 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2243 if (oepos.bh != epos.bh) {
2244 oepos.block = epos.block;
2248 oepos.offset = epos.offset - adsize;
2251 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2254 if (epos.bh != oepos.bh) {
2255 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2256 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2257 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2259 iinfo->i_lenAlloc -= (adsize * 2);
2260 mark_inode_dirty(inode);
2262 aed = (struct allocExtDesc *)oepos.bh->b_data;
2263 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2264 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2265 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2266 udf_update_tag(oepos.bh->b_data,
2267 oepos.offset - (2 * adsize));
2269 udf_update_tag(oepos.bh->b_data,
2270 sizeof(struct allocExtDesc));
2271 mark_buffer_dirty_inode(oepos.bh, inode);
2274 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2276 iinfo->i_lenAlloc -= adsize;
2277 mark_inode_dirty(inode);
2279 aed = (struct allocExtDesc *)oepos.bh->b_data;
2280 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2281 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2282 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2283 udf_update_tag(oepos.bh->b_data,
2284 epos.offset - adsize);
2286 udf_update_tag(oepos.bh->b_data,
2287 sizeof(struct allocExtDesc));
2288 mark_buffer_dirty_inode(oepos.bh, inode);
2295 return (elen >> 30);
2298 int8_t inode_bmap(struct inode *inode, sector_t block,
2299 struct extent_position *pos, struct kernel_lb_addr *eloc,
2300 uint32_t *elen, sector_t *offset)
2302 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2303 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2305 struct udf_inode_info *iinfo;
2307 iinfo = UDF_I(inode);
2308 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2310 pos->block = iinfo->i_location;
2315 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2317 *offset = (bcount - lbcount) >> blocksize_bits;
2318 iinfo->i_lenExtents = lbcount;
2322 } while (lbcount <= bcount);
2323 /* update extent cache */
2324 udf_update_extent_cache(inode, lbcount - *elen, pos);
2325 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2330 long udf_block_map(struct inode *inode, sector_t block)
2332 struct kernel_lb_addr eloc;
2335 struct extent_position epos = {};
2338 down_read(&UDF_I(inode)->i_data_sem);
2340 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2341 (EXT_RECORDED_ALLOCATED >> 30))
2342 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2346 up_read(&UDF_I(inode)->i_data_sem);
2349 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2350 return udf_fixed_to_variable(ret);