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/smp_lock.h>
35 #include <linux/module.h>
36 #include <linux/pagemap.h>
37 #include <linux/buffer_head.h>
38 #include <linux/writeback.h>
39 #include <linux/slab.h>
44 MODULE_AUTHOR("Ben Fennema");
45 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
46 MODULE_LICENSE("GPL");
48 #define EXTENT_MERGE_SIZE 5
50 static mode_t udf_convert_permissions(struct fileEntry *);
51 static int udf_update_inode(struct inode *, int);
52 static void udf_fill_inode(struct inode *, struct buffer_head *);
53 static int udf_alloc_i_data(struct inode *inode, size_t size);
54 static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
56 static int8_t udf_insert_aext(struct inode *, struct extent_position,
57 kernel_lb_addr, uint32_t);
58 static void udf_split_extents(struct inode *, int *, int, int,
59 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
60 static void udf_prealloc_extents(struct inode *, int, int,
61 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
62 static void udf_merge_extents(struct inode *,
63 kernel_long_ad[EXTENT_MERGE_SIZE], int *);
64 static void udf_update_extents(struct inode *,
65 kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 struct extent_position *);
67 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
70 void udf_delete_inode(struct inode *inode)
72 truncate_inode_pages(&inode->i_data, 0);
74 if (is_bad_inode(inode))
81 udf_update_inode(inode, IS_SYNC(inode));
82 udf_free_inode(inode);
92 * If we are going to release inode from memory, we discard preallocation and
93 * truncate last inode extent to proper length. We could use drop_inode() but
94 * it's called under inode_lock and thus we cannot mark inode dirty there. We
95 * use clear_inode() but we have to make sure to write inode as it's not written
98 void udf_clear_inode(struct inode *inode)
100 struct udf_inode_info *iinfo;
101 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
103 /* Discard preallocation for directories, symlinks, etc. */
104 udf_discard_prealloc(inode);
105 udf_truncate_tail_extent(inode);
107 write_inode_now(inode, 0);
109 iinfo = UDF_I(inode);
110 kfree(iinfo->i_ext.i_data);
111 iinfo->i_ext.i_data = NULL;
114 static int udf_writepage(struct page *page, struct writeback_control *wbc)
116 return block_write_full_page(page, udf_get_block, wbc);
119 static int udf_readpage(struct file *file, struct page *page)
121 return block_read_full_page(page, udf_get_block);
124 static int udf_write_begin(struct file *file, struct address_space *mapping,
125 loff_t pos, unsigned len, unsigned flags,
126 struct page **pagep, void **fsdata)
129 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
133 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
135 return generic_block_bmap(mapping, block, udf_get_block);
138 const struct address_space_operations udf_aops = {
139 .readpage = udf_readpage,
140 .writepage = udf_writepage,
141 .sync_page = block_sync_page,
142 .write_begin = udf_write_begin,
143 .write_end = generic_write_end,
147 void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
151 struct udf_inode_info *iinfo = UDF_I(inode);
152 struct writeback_control udf_wbc = {
153 .sync_mode = WB_SYNC_NONE,
157 /* from now on we have normal address_space methods */
158 inode->i_data.a_ops = &udf_aops;
160 if (!iinfo->i_lenAlloc) {
161 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
162 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
164 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
165 mark_inode_dirty(inode);
169 page = grab_cache_page(inode->i_mapping, 0);
170 BUG_ON(!PageLocked(page));
172 if (!PageUptodate(page)) {
174 memset(kaddr + iinfo->i_lenAlloc, 0x00,
175 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
176 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
178 flush_dcache_page(page);
179 SetPageUptodate(page);
182 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
184 iinfo->i_lenAlloc = 0;
185 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
186 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
188 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
190 inode->i_data.a_ops->writepage(page, &udf_wbc);
191 page_cache_release(page);
193 mark_inode_dirty(inode);
196 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
200 struct buffer_head *dbh = NULL;
204 struct extent_position epos;
206 struct udf_fileident_bh sfibh, dfibh;
207 loff_t f_pos = udf_ext0_offset(inode);
208 int size = udf_ext0_offset(inode) + inode->i_size;
209 struct fileIdentDesc cfi, *sfi, *dfi;
210 struct udf_inode_info *iinfo = UDF_I(inode);
212 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
213 alloctype = ICBTAG_FLAG_AD_SHORT;
215 alloctype = ICBTAG_FLAG_AD_LONG;
217 if (!inode->i_size) {
218 iinfo->i_alloc_type = alloctype;
219 mark_inode_dirty(inode);
223 /* alloc block, and copy data to it */
224 *block = udf_new_block(inode->i_sb, inode,
225 iinfo->i_location.partitionReferenceNum,
226 iinfo->i_location.logicalBlockNum, err);
229 newblock = udf_get_pblock(inode->i_sb, *block,
230 iinfo->i_location.partitionReferenceNum,
234 dbh = udf_tgetblk(inode->i_sb, newblock);
238 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
239 set_buffer_uptodate(dbh);
241 mark_buffer_dirty_inode(dbh, inode);
243 sfibh.soffset = sfibh.eoffset =
244 f_pos & (inode->i_sb->s_blocksize - 1);
245 sfibh.sbh = sfibh.ebh = NULL;
246 dfibh.soffset = dfibh.eoffset = 0;
247 dfibh.sbh = dfibh.ebh = dbh;
248 while (f_pos < size) {
249 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
250 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
256 iinfo->i_alloc_type = alloctype;
257 sfi->descTag.tagLocation = cpu_to_le32(*block);
258 dfibh.soffset = dfibh.eoffset;
259 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
260 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
261 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
263 le16_to_cpu(sfi->lengthOfImpUse))) {
264 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
269 mark_buffer_dirty_inode(dbh, inode);
271 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
273 iinfo->i_lenAlloc = 0;
274 eloc.logicalBlockNum = *block;
275 eloc.partitionReferenceNum =
276 iinfo->i_location.partitionReferenceNum;
277 elen = inode->i_sb->s_blocksize;
278 iinfo->i_lenExtents = elen;
280 epos.block = iinfo->i_location;
281 epos.offset = udf_file_entry_alloc_offset(inode);
282 udf_add_aext(inode, &epos, eloc, elen, 0);
286 mark_inode_dirty(inode);
290 static int udf_get_block(struct inode *inode, sector_t block,
291 struct buffer_head *bh_result, int create)
294 struct buffer_head *bh;
296 struct udf_inode_info *iinfo;
299 phys = udf_block_map(inode, block);
301 map_bh(bh_result, inode->i_sb, phys);
314 iinfo = UDF_I(inode);
315 if (block == iinfo->i_next_alloc_block + 1) {
316 iinfo->i_next_alloc_block++;
317 iinfo->i_next_alloc_goal++;
322 bh = inode_getblk(inode, block, &err, &phys, &new);
329 set_buffer_new(bh_result);
330 map_bh(bh_result, inode->i_sb, phys);
337 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
341 static struct buffer_head *udf_getblk(struct inode *inode, long block,
342 int create, int *err)
344 struct buffer_head *bh;
345 struct buffer_head dummy;
348 dummy.b_blocknr = -1000;
349 *err = udf_get_block(inode, block, &dummy, create);
350 if (!*err && buffer_mapped(&dummy)) {
351 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
352 if (buffer_new(&dummy)) {
354 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
355 set_buffer_uptodate(bh);
357 mark_buffer_dirty_inode(bh, inode);
365 /* Extend the file by 'blocks' blocks, return the number of extents added */
366 int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
367 kernel_long_ad *last_ext, sector_t blocks)
370 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
371 struct super_block *sb = inode->i_sb;
372 kernel_lb_addr prealloc_loc = {};
373 int prealloc_len = 0;
374 struct udf_inode_info *iinfo;
376 /* The previous extent is fake and we should not extend by anything
377 * - there's nothing to do... */
381 iinfo = UDF_I(inode);
382 /* Round the last extent up to a multiple of block size */
383 if (last_ext->extLength & (sb->s_blocksize - 1)) {
384 last_ext->extLength =
385 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
386 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
387 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
388 iinfo->i_lenExtents =
389 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
390 ~(sb->s_blocksize - 1);
393 /* Last extent are just preallocated blocks? */
394 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
395 EXT_NOT_RECORDED_ALLOCATED) {
396 /* Save the extent so that we can reattach it to the end */
397 prealloc_loc = last_ext->extLocation;
398 prealloc_len = last_ext->extLength;
399 /* Mark the extent as a hole */
400 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
401 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
402 last_ext->extLocation.logicalBlockNum = 0;
403 last_ext->extLocation.partitionReferenceNum = 0;
406 /* Can we merge with the previous extent? */
407 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
408 EXT_NOT_RECORDED_NOT_ALLOCATED) {
409 add = ((1 << 30) - sb->s_blocksize -
410 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
411 sb->s_blocksize_bits;
415 last_ext->extLength += add << sb->s_blocksize_bits;
419 udf_add_aext(inode, last_pos, last_ext->extLocation,
420 last_ext->extLength, 1);
423 udf_write_aext(inode, last_pos, last_ext->extLocation,
424 last_ext->extLength, 1);
426 /* Managed to do everything necessary? */
430 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
431 last_ext->extLocation.logicalBlockNum = 0;
432 last_ext->extLocation.partitionReferenceNum = 0;
433 add = (1 << (30-sb->s_blocksize_bits)) - 1;
434 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
435 (add << sb->s_blocksize_bits);
437 /* Create enough extents to cover the whole hole */
438 while (blocks > add) {
440 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
441 last_ext->extLength, 1) == -1)
446 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
447 (blocks << sb->s_blocksize_bits);
448 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
449 last_ext->extLength, 1) == -1)
455 /* Do we have some preallocated blocks saved? */
457 if (udf_add_aext(inode, last_pos, prealloc_loc,
458 prealloc_len, 1) == -1)
460 last_ext->extLocation = prealloc_loc;
461 last_ext->extLength = prealloc_len;
465 /* last_pos should point to the last written extent... */
466 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
467 last_pos->offset -= sizeof(short_ad);
468 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
469 last_pos->offset -= sizeof(long_ad);
476 static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
477 int *err, sector_t *phys, int *new)
479 static sector_t last_block;
480 struct buffer_head *result = NULL;
481 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
482 struct extent_position prev_epos, cur_epos, next_epos;
483 int count = 0, startnum = 0, endnum = 0;
484 uint32_t elen = 0, tmpelen;
485 kernel_lb_addr eloc, tmpeloc;
487 loff_t lbcount = 0, b_off = 0;
488 uint32_t newblocknum, newblock;
491 struct udf_inode_info *iinfo = UDF_I(inode);
492 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
495 prev_epos.offset = udf_file_entry_alloc_offset(inode);
496 prev_epos.block = iinfo->i_location;
498 cur_epos = next_epos = prev_epos;
499 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
501 /* find the extent which contains the block we are looking for.
502 alternate between laarr[0] and laarr[1] for locations of the
503 current extent, and the previous extent */
505 if (prev_epos.bh != cur_epos.bh) {
506 brelse(prev_epos.bh);
508 prev_epos.bh = cur_epos.bh;
510 if (cur_epos.bh != next_epos.bh) {
512 get_bh(next_epos.bh);
513 cur_epos.bh = next_epos.bh;
518 prev_epos.block = cur_epos.block;
519 cur_epos.block = next_epos.block;
521 prev_epos.offset = cur_epos.offset;
522 cur_epos.offset = next_epos.offset;
524 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
530 laarr[c].extLength = (etype << 30) | elen;
531 laarr[c].extLocation = eloc;
533 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
534 pgoal = eloc.logicalBlockNum +
535 ((elen + inode->i_sb->s_blocksize - 1) >>
536 inode->i_sb->s_blocksize_bits);
539 } while (lbcount + elen <= b_off);
542 offset = b_off >> inode->i_sb->s_blocksize_bits;
544 * Move prev_epos and cur_epos into indirect extent if we are at
547 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
548 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
550 /* if the extent is allocated and recorded, return the block
551 if the extent is not a multiple of the blocksize, round up */
553 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
554 if (elen & (inode->i_sb->s_blocksize - 1)) {
555 elen = EXT_RECORDED_ALLOCATED |
556 ((elen + inode->i_sb->s_blocksize - 1) &
557 ~(inode->i_sb->s_blocksize - 1));
558 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
560 brelse(prev_epos.bh);
562 brelse(next_epos.bh);
563 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
569 /* Are we beyond EOF? */
578 /* Create a fake extent when there's not one */
579 memset(&laarr[0].extLocation, 0x00,
580 sizeof(kernel_lb_addr));
581 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
582 /* Will udf_extend_file() create real extent from
584 startnum = (offset > 0);
586 /* Create extents for the hole between EOF and offset */
587 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
589 brelse(prev_epos.bh);
591 brelse(next_epos.bh);
592 /* We don't really know the error here so we just make
600 /* We are not covered by a preallocated extent? */
601 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
602 EXT_NOT_RECORDED_ALLOCATED) {
603 /* Is there any real extent? - otherwise we overwrite
607 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
608 inode->i_sb->s_blocksize;
609 memset(&laarr[c].extLocation, 0x00,
610 sizeof(kernel_lb_addr));
617 endnum = startnum = ((count > 2) ? 2 : count);
619 /* if the current extent is in position 0,
620 swap it with the previous */
621 if (!c && count != 1) {
628 /* if the current block is located in an extent,
629 read the next extent */
630 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
632 laarr[c + 1].extLength = (etype << 30) | elen;
633 laarr[c + 1].extLocation = eloc;
641 /* if the current extent is not recorded but allocated, get the
642 * block in the extent corresponding to the requested block */
643 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
644 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
645 else { /* otherwise, allocate a new block */
646 if (iinfo->i_next_alloc_block == block)
647 goal = iinfo->i_next_alloc_goal;
650 if (!(goal = pgoal)) /* XXX: what was intended here? */
651 goal = iinfo->i_location.logicalBlockNum + 1;
654 newblocknum = udf_new_block(inode->i_sb, inode,
655 iinfo->i_location.partitionReferenceNum,
658 brelse(prev_epos.bh);
662 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
665 /* if the extent the requsted block is located in contains multiple
666 * blocks, split the extent into at most three extents. blocks prior
667 * to requested block, requested block, and blocks after requested
669 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
671 #ifdef UDF_PREALLOCATE
672 /* preallocate blocks */
673 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
676 /* merge any continuous blocks in laarr */
677 udf_merge_extents(inode, laarr, &endnum);
679 /* write back the new extents, inserting new extents if the new number
680 * of extents is greater than the old number, and deleting extents if
681 * the new number of extents is less than the old number */
682 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
684 brelse(prev_epos.bh);
686 newblock = udf_get_pblock(inode->i_sb, newblocknum,
687 iinfo->i_location.partitionReferenceNum, 0);
693 iinfo->i_next_alloc_block = block;
694 iinfo->i_next_alloc_goal = newblocknum;
695 inode->i_ctime = current_fs_time(inode->i_sb);
698 udf_sync_inode(inode);
700 mark_inode_dirty(inode);
705 static void udf_split_extents(struct inode *inode, int *c, int offset,
707 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
710 unsigned long blocksize = inode->i_sb->s_blocksize;
711 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
713 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
714 (laarr[*c].extLength >> 30) ==
715 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
717 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
718 blocksize - 1) >> blocksize_bits;
719 int8_t etype = (laarr[curr].extLength >> 30);
723 else if (!offset || blen == offset + 1) {
724 laarr[curr + 2] = laarr[curr + 1];
725 laarr[curr + 1] = laarr[curr];
727 laarr[curr + 3] = laarr[curr + 1];
728 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
732 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
733 udf_free_blocks(inode->i_sb, inode,
734 laarr[curr].extLocation,
736 laarr[curr].extLength =
737 EXT_NOT_RECORDED_NOT_ALLOCATED |
738 (offset << blocksize_bits);
739 laarr[curr].extLocation.logicalBlockNum = 0;
740 laarr[curr].extLocation.
741 partitionReferenceNum = 0;
743 laarr[curr].extLength = (etype << 30) |
744 (offset << blocksize_bits);
750 laarr[curr].extLocation.logicalBlockNum = newblocknum;
751 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
752 laarr[curr].extLocation.partitionReferenceNum =
753 UDF_I(inode)->i_location.partitionReferenceNum;
754 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
758 if (blen != offset + 1) {
759 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
760 laarr[curr].extLocation.logicalBlockNum +=
762 laarr[curr].extLength = (etype << 30) |
763 ((blen - (offset + 1)) << blocksize_bits);
770 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
771 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
774 int start, length = 0, currlength = 0, i;
776 if (*endnum >= (c + 1)) {
782 if ((laarr[c + 1].extLength >> 30) ==
783 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
785 length = currlength =
786 (((laarr[c + 1].extLength &
787 UDF_EXTENT_LENGTH_MASK) +
788 inode->i_sb->s_blocksize - 1) >>
789 inode->i_sb->s_blocksize_bits);
794 for (i = start + 1; i <= *endnum; i++) {
797 length += UDF_DEFAULT_PREALLOC_BLOCKS;
798 } else if ((laarr[i].extLength >> 30) ==
799 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
800 length += (((laarr[i].extLength &
801 UDF_EXTENT_LENGTH_MASK) +
802 inode->i_sb->s_blocksize - 1) >>
803 inode->i_sb->s_blocksize_bits);
809 int next = laarr[start].extLocation.logicalBlockNum +
810 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
811 inode->i_sb->s_blocksize - 1) >>
812 inode->i_sb->s_blocksize_bits);
813 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
814 laarr[start].extLocation.partitionReferenceNum,
815 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
816 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
819 if (start == (c + 1))
820 laarr[start].extLength +=
822 inode->i_sb->s_blocksize_bits);
824 memmove(&laarr[c + 2], &laarr[c + 1],
825 sizeof(long_ad) * (*endnum - (c + 1)));
827 laarr[c + 1].extLocation.logicalBlockNum = next;
828 laarr[c + 1].extLocation.partitionReferenceNum =
829 laarr[c].extLocation.
830 partitionReferenceNum;
831 laarr[c + 1].extLength =
832 EXT_NOT_RECORDED_ALLOCATED |
834 inode->i_sb->s_blocksize_bits);
838 for (i = start + 1; numalloc && i < *endnum; i++) {
839 int elen = ((laarr[i].extLength &
840 UDF_EXTENT_LENGTH_MASK) +
841 inode->i_sb->s_blocksize - 1) >>
842 inode->i_sb->s_blocksize_bits;
844 if (elen > numalloc) {
845 laarr[i].extLength -=
847 inode->i_sb->s_blocksize_bits);
851 if (*endnum > (i + 1))
855 (*endnum - (i + 1)));
860 UDF_I(inode)->i_lenExtents +=
861 numalloc << inode->i_sb->s_blocksize_bits;
866 static void udf_merge_extents(struct inode *inode,
867 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
871 unsigned long blocksize = inode->i_sb->s_blocksize;
872 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
874 for (i = 0; i < (*endnum - 1); i++) {
875 kernel_long_ad *li /*l[i]*/ = &laarr[i];
876 kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
878 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
879 (((li->extLength >> 30) ==
880 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
881 ((lip1->extLocation.logicalBlockNum -
882 li->extLocation.logicalBlockNum) ==
883 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
884 blocksize - 1) >> blocksize_bits)))) {
886 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
887 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
888 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
889 lip1->extLength = (lip1->extLength -
891 UDF_EXTENT_LENGTH_MASK) +
892 UDF_EXTENT_LENGTH_MASK) &
894 li->extLength = (li->extLength &
895 UDF_EXTENT_FLAG_MASK) +
896 (UDF_EXTENT_LENGTH_MASK + 1) -
898 lip1->extLocation.logicalBlockNum =
899 li->extLocation.logicalBlockNum +
901 UDF_EXTENT_LENGTH_MASK) >>
904 li->extLength = lip1->extLength +
906 UDF_EXTENT_LENGTH_MASK) +
907 blocksize - 1) & ~(blocksize - 1));
908 if (*endnum > (i + 2))
909 memmove(&laarr[i + 1], &laarr[i + 2],
911 (*endnum - (i + 2)));
915 } else if (((li->extLength >> 30) ==
916 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
917 ((lip1->extLength >> 30) ==
918 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
919 udf_free_blocks(inode->i_sb, inode, li->extLocation, 0,
921 UDF_EXTENT_LENGTH_MASK) +
922 blocksize - 1) >> blocksize_bits);
923 li->extLocation.logicalBlockNum = 0;
924 li->extLocation.partitionReferenceNum = 0;
926 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
927 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
928 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
929 lip1->extLength = (lip1->extLength -
931 UDF_EXTENT_LENGTH_MASK) +
932 UDF_EXTENT_LENGTH_MASK) &
934 li->extLength = (li->extLength &
935 UDF_EXTENT_FLAG_MASK) +
936 (UDF_EXTENT_LENGTH_MASK + 1) -
939 li->extLength = lip1->extLength +
941 UDF_EXTENT_LENGTH_MASK) +
942 blocksize - 1) & ~(blocksize - 1));
943 if (*endnum > (i + 2))
944 memmove(&laarr[i + 1], &laarr[i + 2],
946 (*endnum - (i + 2)));
950 } else if ((li->extLength >> 30) ==
951 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
952 udf_free_blocks(inode->i_sb, inode,
955 UDF_EXTENT_LENGTH_MASK) +
956 blocksize - 1) >> blocksize_bits);
957 li->extLocation.logicalBlockNum = 0;
958 li->extLocation.partitionReferenceNum = 0;
959 li->extLength = (li->extLength &
960 UDF_EXTENT_LENGTH_MASK) |
961 EXT_NOT_RECORDED_NOT_ALLOCATED;
966 static void udf_update_extents(struct inode *inode,
967 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
968 int startnum, int endnum,
969 struct extent_position *epos)
972 kernel_lb_addr tmploc;
975 if (startnum > endnum) {
976 for (i = 0; i < (startnum - endnum); i++)
977 udf_delete_aext(inode, *epos, laarr[i].extLocation,
979 } else if (startnum < endnum) {
980 for (i = 0; i < (endnum - startnum); i++) {
981 udf_insert_aext(inode, *epos, laarr[i].extLocation,
983 udf_next_aext(inode, epos, &laarr[i].extLocation,
984 &laarr[i].extLength, 1);
989 for (i = start; i < endnum; i++) {
990 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
991 udf_write_aext(inode, epos, laarr[i].extLocation,
992 laarr[i].extLength, 1);
996 struct buffer_head *udf_bread(struct inode *inode, int block,
997 int create, int *err)
999 struct buffer_head *bh = NULL;
1001 bh = udf_getblk(inode, block, create, err);
1005 if (buffer_uptodate(bh))
1008 ll_rw_block(READ, 1, &bh);
1011 if (buffer_uptodate(bh))
1019 void udf_truncate(struct inode *inode)
1023 struct udf_inode_info *iinfo;
1025 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1026 S_ISLNK(inode->i_mode)))
1028 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1032 iinfo = UDF_I(inode);
1033 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1034 if (inode->i_sb->s_blocksize <
1035 (udf_file_entry_alloc_offset(inode) +
1037 udf_expand_file_adinicb(inode, inode->i_size, &err);
1038 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1039 inode->i_size = iinfo->i_lenAlloc;
1043 udf_truncate_extents(inode);
1045 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
1046 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
1047 0x00, inode->i_sb->s_blocksize -
1048 offset - udf_file_entry_alloc_offset(inode));
1049 iinfo->i_lenAlloc = inode->i_size;
1052 block_truncate_page(inode->i_mapping, inode->i_size,
1054 udf_truncate_extents(inode);
1057 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1059 udf_sync_inode(inode);
1061 mark_inode_dirty(inode);
1065 static void __udf_read_inode(struct inode *inode)
1067 struct buffer_head *bh = NULL;
1068 struct fileEntry *fe;
1070 struct udf_inode_info *iinfo = UDF_I(inode);
1073 * Set defaults, but the inode is still incomplete!
1074 * Note: get_new_inode() sets the following on a new inode:
1077 * i_flags = sb->s_flags
1079 * clean_inode(): zero fills and sets
1084 bh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 0, &ident);
1086 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
1088 make_bad_inode(inode);
1092 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1093 ident != TAG_IDENT_USE) {
1094 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1095 "failed ident=%d\n", inode->i_ino, ident);
1097 make_bad_inode(inode);
1101 fe = (struct fileEntry *)bh->b_data;
1103 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1104 struct buffer_head *ibh;
1106 ibh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 1,
1108 if (ident == TAG_IDENT_IE && ibh) {
1109 struct buffer_head *nbh = NULL;
1111 struct indirectEntry *ie;
1113 ie = (struct indirectEntry *)ibh->b_data;
1114 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1116 if (ie->indirectICB.extLength &&
1117 (nbh = udf_read_ptagged(inode->i_sb, loc, 0,
1119 if (ident == TAG_IDENT_FE ||
1120 ident == TAG_IDENT_EFE) {
1121 memcpy(&iinfo->i_location,
1123 sizeof(kernel_lb_addr));
1127 __udf_read_inode(inode);
1134 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1135 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
1136 le16_to_cpu(fe->icbTag.strategyType));
1138 make_bad_inode(inode);
1141 udf_fill_inode(inode, bh);
1146 static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1148 struct fileEntry *fe;
1149 struct extendedFileEntry *efe;
1153 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1154 struct udf_inode_info *iinfo = UDF_I(inode);
1156 fe = (struct fileEntry *)bh->b_data;
1157 efe = (struct extendedFileEntry *)bh->b_data;
1159 if (fe->icbTag.strategyType == cpu_to_le16(4))
1160 iinfo->i_strat4096 = 0;
1161 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1162 iinfo->i_strat4096 = 1;
1164 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1165 ICBTAG_FLAG_AD_MASK;
1166 iinfo->i_unique = 0;
1167 iinfo->i_lenEAttr = 0;
1168 iinfo->i_lenExtents = 0;
1169 iinfo->i_lenAlloc = 0;
1170 iinfo->i_next_alloc_block = 0;
1171 iinfo->i_next_alloc_goal = 0;
1172 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1175 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1176 sizeof(struct extendedFileEntry))) {
1177 make_bad_inode(inode);
1180 memcpy(iinfo->i_ext.i_data,
1181 bh->b_data + sizeof(struct extendedFileEntry),
1182 inode->i_sb->s_blocksize -
1183 sizeof(struct extendedFileEntry));
1184 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1187 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1188 sizeof(struct fileEntry))) {
1189 make_bad_inode(inode);
1192 memcpy(iinfo->i_ext.i_data,
1193 bh->b_data + sizeof(struct fileEntry),
1194 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1195 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1198 iinfo->i_lenAlloc = le32_to_cpu(
1199 ((struct unallocSpaceEntry *)bh->b_data)->
1201 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1202 sizeof(struct unallocSpaceEntry))) {
1203 make_bad_inode(inode);
1206 memcpy(iinfo->i_ext.i_data,
1207 bh->b_data + sizeof(struct unallocSpaceEntry),
1208 inode->i_sb->s_blocksize -
1209 sizeof(struct unallocSpaceEntry));
1213 inode->i_uid = le32_to_cpu(fe->uid);
1214 if (inode->i_uid == -1 ||
1215 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1216 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1217 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1219 inode->i_gid = le32_to_cpu(fe->gid);
1220 if (inode->i_gid == -1 ||
1221 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1222 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1223 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1225 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1226 if (!inode->i_nlink)
1229 inode->i_size = le64_to_cpu(fe->informationLength);
1230 iinfo->i_lenExtents = inode->i_size;
1232 inode->i_mode = udf_convert_permissions(fe);
1233 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1235 if (iinfo->i_efe == 0) {
1236 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1237 (inode->i_sb->s_blocksize_bits - 9);
1239 if (udf_stamp_to_time(&convtime, &convtime_usec,
1240 lets_to_cpu(fe->accessTime))) {
1241 inode->i_atime.tv_sec = convtime;
1242 inode->i_atime.tv_nsec = convtime_usec * 1000;
1244 inode->i_atime = sbi->s_record_time;
1247 if (udf_stamp_to_time(&convtime, &convtime_usec,
1248 lets_to_cpu(fe->modificationTime))) {
1249 inode->i_mtime.tv_sec = convtime;
1250 inode->i_mtime.tv_nsec = convtime_usec * 1000;
1252 inode->i_mtime = sbi->s_record_time;
1255 if (udf_stamp_to_time(&convtime, &convtime_usec,
1256 lets_to_cpu(fe->attrTime))) {
1257 inode->i_ctime.tv_sec = convtime;
1258 inode->i_ctime.tv_nsec = convtime_usec * 1000;
1260 inode->i_ctime = sbi->s_record_time;
1263 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1264 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1265 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1266 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
1268 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1269 (inode->i_sb->s_blocksize_bits - 9);
1271 if (udf_stamp_to_time(&convtime, &convtime_usec,
1272 lets_to_cpu(efe->accessTime))) {
1273 inode->i_atime.tv_sec = convtime;
1274 inode->i_atime.tv_nsec = convtime_usec * 1000;
1276 inode->i_atime = sbi->s_record_time;
1279 if (udf_stamp_to_time(&convtime, &convtime_usec,
1280 lets_to_cpu(efe->modificationTime))) {
1281 inode->i_mtime.tv_sec = convtime;
1282 inode->i_mtime.tv_nsec = convtime_usec * 1000;
1284 inode->i_mtime = sbi->s_record_time;
1287 if (udf_stamp_to_time(&convtime, &convtime_usec,
1288 lets_to_cpu(efe->createTime))) {
1289 iinfo->i_crtime.tv_sec = convtime;
1290 iinfo->i_crtime.tv_nsec = convtime_usec * 1000;
1292 iinfo->i_crtime = sbi->s_record_time;
1295 if (udf_stamp_to_time(&convtime, &convtime_usec,
1296 lets_to_cpu(efe->attrTime))) {
1297 inode->i_ctime.tv_sec = convtime;
1298 inode->i_ctime.tv_nsec = convtime_usec * 1000;
1300 inode->i_ctime = sbi->s_record_time;
1303 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1304 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1305 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1306 offset = sizeof(struct extendedFileEntry) +
1310 switch (fe->icbTag.fileType) {
1311 case ICBTAG_FILE_TYPE_DIRECTORY:
1312 inode->i_op = &udf_dir_inode_operations;
1313 inode->i_fop = &udf_dir_operations;
1314 inode->i_mode |= S_IFDIR;
1317 case ICBTAG_FILE_TYPE_REALTIME:
1318 case ICBTAG_FILE_TYPE_REGULAR:
1319 case ICBTAG_FILE_TYPE_UNDEF:
1320 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1321 inode->i_data.a_ops = &udf_adinicb_aops;
1323 inode->i_data.a_ops = &udf_aops;
1324 inode->i_op = &udf_file_inode_operations;
1325 inode->i_fop = &udf_file_operations;
1326 inode->i_mode |= S_IFREG;
1328 case ICBTAG_FILE_TYPE_BLOCK:
1329 inode->i_mode |= S_IFBLK;
1331 case ICBTAG_FILE_TYPE_CHAR:
1332 inode->i_mode |= S_IFCHR;
1334 case ICBTAG_FILE_TYPE_FIFO:
1335 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1337 case ICBTAG_FILE_TYPE_SOCKET:
1338 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1340 case ICBTAG_FILE_TYPE_SYMLINK:
1341 inode->i_data.a_ops = &udf_symlink_aops;
1342 inode->i_op = &page_symlink_inode_operations;
1343 inode->i_mode = S_IFLNK | S_IRWXUGO;
1346 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1347 "file type=%d\n", inode->i_ino,
1348 fe->icbTag.fileType);
1349 make_bad_inode(inode);
1352 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1353 struct deviceSpec *dsea =
1354 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1356 init_special_inode(inode, inode->i_mode,
1357 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1358 le32_to_cpu(dsea->minorDeviceIdent)));
1359 /* Developer ID ??? */
1361 make_bad_inode(inode);
1365 static int udf_alloc_i_data(struct inode *inode, size_t size)
1367 struct udf_inode_info *iinfo = UDF_I(inode);
1368 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1370 if (!iinfo->i_ext.i_data) {
1371 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1372 "no free memory\n", inode->i_ino);
1379 static mode_t udf_convert_permissions(struct fileEntry *fe)
1382 uint32_t permissions;
1385 permissions = le32_to_cpu(fe->permissions);
1386 flags = le16_to_cpu(fe->icbTag.flags);
1388 mode = ((permissions) & S_IRWXO) |
1389 ((permissions >> 2) & S_IRWXG) |
1390 ((permissions >> 4) & S_IRWXU) |
1391 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1392 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1393 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1398 int udf_write_inode(struct inode *inode, int sync)
1403 ret = udf_update_inode(inode, sync);
1409 int udf_sync_inode(struct inode *inode)
1411 return udf_update_inode(inode, 1);
1414 static int udf_update_inode(struct inode *inode, int do_sync)
1416 struct buffer_head *bh = NULL;
1417 struct fileEntry *fe;
1418 struct extendedFileEntry *efe;
1422 kernel_timestamp cpu_time;
1424 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1425 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1426 struct udf_inode_info *iinfo = UDF_I(inode);
1428 bh = udf_tread(inode->i_sb,
1429 udf_get_lb_pblock(inode->i_sb,
1430 iinfo->i_location, 0));
1432 udf_debug("bread failure\n");
1436 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1438 fe = (struct fileEntry *)bh->b_data;
1439 efe = (struct extendedFileEntry *)bh->b_data;
1441 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1442 struct unallocSpaceEntry *use =
1443 (struct unallocSpaceEntry *)bh->b_data;
1445 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1446 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1447 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1448 sizeof(struct unallocSpaceEntry));
1449 crclen = sizeof(struct unallocSpaceEntry) +
1450 iinfo->i_lenAlloc - sizeof(tag);
1451 use->descTag.tagLocation = cpu_to_le32(
1454 use->descTag.descCRCLength = cpu_to_le16(crclen);
1455 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use +
1456 sizeof(tag), crclen,
1458 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1460 mark_buffer_dirty(bh);
1465 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1466 fe->uid = cpu_to_le32(-1);
1468 fe->uid = cpu_to_le32(inode->i_uid);
1470 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1471 fe->gid = cpu_to_le32(-1);
1473 fe->gid = cpu_to_le32(inode->i_gid);
1475 udfperms = ((inode->i_mode & S_IRWXO)) |
1476 ((inode->i_mode & S_IRWXG) << 2) |
1477 ((inode->i_mode & S_IRWXU) << 4);
1479 udfperms |= (le32_to_cpu(fe->permissions) &
1480 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1481 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1482 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1483 fe->permissions = cpu_to_le32(udfperms);
1485 if (S_ISDIR(inode->i_mode))
1486 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1488 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1490 fe->informationLength = cpu_to_le64(inode->i_size);
1492 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1494 struct deviceSpec *dsea =
1495 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1497 dsea = (struct deviceSpec *)
1498 udf_add_extendedattr(inode,
1499 sizeof(struct deviceSpec) +
1500 sizeof(regid), 12, 0x3);
1501 dsea->attrType = cpu_to_le32(12);
1502 dsea->attrSubtype = 1;
1503 dsea->attrLength = cpu_to_le32(
1504 sizeof(struct deviceSpec) +
1506 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1508 eid = (regid *)dsea->impUse;
1509 memset(eid, 0, sizeof(regid));
1510 strcpy(eid->ident, UDF_ID_DEVELOPER);
1511 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1512 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1513 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1514 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1517 if (iinfo->i_efe == 0) {
1518 memcpy(bh->b_data + sizeof(struct fileEntry),
1519 iinfo->i_ext.i_data,
1520 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1521 fe->logicalBlocksRecorded = cpu_to_le64(
1522 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1523 (blocksize_bits - 9));
1525 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1526 fe->accessTime = cpu_to_lets(cpu_time);
1527 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1528 fe->modificationTime = cpu_to_lets(cpu_time);
1529 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1530 fe->attrTime = cpu_to_lets(cpu_time);
1531 memset(&(fe->impIdent), 0, sizeof(regid));
1532 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1533 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1534 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1535 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1536 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1537 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1538 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1539 crclen = sizeof(struct fileEntry);
1541 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1542 iinfo->i_ext.i_data,
1543 inode->i_sb->s_blocksize -
1544 sizeof(struct extendedFileEntry));
1545 efe->objectSize = cpu_to_le64(inode->i_size);
1546 efe->logicalBlocksRecorded = cpu_to_le64(
1547 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1548 (blocksize_bits - 9));
1550 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1551 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1552 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1553 iinfo->i_crtime = inode->i_atime;
1555 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1556 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1557 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1558 iinfo->i_crtime = inode->i_mtime;
1560 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1561 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1562 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1563 iinfo->i_crtime = inode->i_ctime;
1565 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1566 efe->accessTime = cpu_to_lets(cpu_time);
1567 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1568 efe->modificationTime = cpu_to_lets(cpu_time);
1569 if (udf_time_to_stamp(&cpu_time, iinfo->i_crtime))
1570 efe->createTime = cpu_to_lets(cpu_time);
1571 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1572 efe->attrTime = cpu_to_lets(cpu_time);
1574 memset(&(efe->impIdent), 0, sizeof(regid));
1575 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1576 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1577 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1578 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1579 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1580 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1581 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1582 crclen = sizeof(struct extendedFileEntry);
1584 if (iinfo->i_strat4096) {
1585 fe->icbTag.strategyType = cpu_to_le16(4096);
1586 fe->icbTag.strategyParameter = cpu_to_le16(1);
1587 fe->icbTag.numEntries = cpu_to_le16(2);
1589 fe->icbTag.strategyType = cpu_to_le16(4);
1590 fe->icbTag.numEntries = cpu_to_le16(1);
1593 if (S_ISDIR(inode->i_mode))
1594 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1595 else if (S_ISREG(inode->i_mode))
1596 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1597 else if (S_ISLNK(inode->i_mode))
1598 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1599 else if (S_ISBLK(inode->i_mode))
1600 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1601 else if (S_ISCHR(inode->i_mode))
1602 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1603 else if (S_ISFIFO(inode->i_mode))
1604 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1605 else if (S_ISSOCK(inode->i_mode))
1606 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1608 icbflags = iinfo->i_alloc_type |
1609 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1610 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1611 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1612 (le16_to_cpu(fe->icbTag.flags) &
1613 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1614 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1616 fe->icbTag.flags = cpu_to_le16(icbflags);
1617 if (sbi->s_udfrev >= 0x0200)
1618 fe->descTag.descVersion = cpu_to_le16(3);
1620 fe->descTag.descVersion = cpu_to_le16(2);
1621 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1622 fe->descTag.tagLocation = cpu_to_le32(
1623 iinfo->i_location.logicalBlockNum);
1624 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
1626 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1627 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag),
1629 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1631 /* write the data blocks */
1632 mark_buffer_dirty(bh);
1634 sync_dirty_buffer(bh);
1635 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1636 printk(KERN_WARNING "IO error syncing udf inode "
1637 "[%s:%08lx]\n", inode->i_sb->s_id,
1647 struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
1649 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1650 struct inode *inode = iget_locked(sb, block);
1655 if (inode->i_state & I_NEW) {
1656 memcpy(&UDF_I(inode)->i_location, &ino, sizeof(kernel_lb_addr));
1657 __udf_read_inode(inode);
1658 unlock_new_inode(inode);
1661 if (is_bad_inode(inode))
1664 if (ino.logicalBlockNum >= UDF_SB(sb)->
1665 s_partmaps[ino.partitionReferenceNum].s_partition_len) {
1666 udf_debug("block=%d, partition=%d out of range\n",
1667 ino.logicalBlockNum, ino.partitionReferenceNum);
1668 make_bad_inode(inode);
1679 int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
1680 kernel_lb_addr eloc, uint32_t elen, int inc)
1683 short_ad *sad = NULL;
1684 long_ad *lad = NULL;
1685 struct allocExtDesc *aed;
1688 struct udf_inode_info *iinfo = UDF_I(inode);
1691 ptr = iinfo->i_ext.i_data + epos->offset -
1692 udf_file_entry_alloc_offset(inode) +
1695 ptr = epos->bh->b_data + epos->offset;
1697 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1698 adsize = sizeof(short_ad);
1699 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1700 adsize = sizeof(long_ad);
1704 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1706 struct buffer_head *nbh;
1708 kernel_lb_addr obloc = epos->block;
1710 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1711 obloc.partitionReferenceNum,
1712 obloc.logicalBlockNum, &err);
1713 if (!epos->block.logicalBlockNum)
1715 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1721 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1722 set_buffer_uptodate(nbh);
1724 mark_buffer_dirty_inode(nbh, inode);
1726 aed = (struct allocExtDesc *)(nbh->b_data);
1727 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1728 aed->previousAllocExtLocation =
1729 cpu_to_le32(obloc.logicalBlockNum);
1730 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1731 loffset = epos->offset;
1732 aed->lengthAllocDescs = cpu_to_le32(adsize);
1733 sptr = ptr - adsize;
1734 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1735 memcpy(dptr, sptr, adsize);
1736 epos->offset = sizeof(struct allocExtDesc) + adsize;
1738 loffset = epos->offset + adsize;
1739 aed->lengthAllocDescs = cpu_to_le32(0);
1741 epos->offset = sizeof(struct allocExtDesc);
1744 aed = (struct allocExtDesc *)epos->bh->b_data;
1745 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1747 iinfo->i_lenAlloc += adsize;
1748 mark_inode_dirty(inode);
1751 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1752 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1753 epos->block.logicalBlockNum, sizeof(tag));
1755 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1756 epos->block.logicalBlockNum, sizeof(tag));
1757 switch (iinfo->i_alloc_type) {
1758 case ICBTAG_FLAG_AD_SHORT:
1759 sad = (short_ad *)sptr;
1760 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1761 inode->i_sb->s_blocksize);
1763 cpu_to_le32(epos->block.logicalBlockNum);
1765 case ICBTAG_FLAG_AD_LONG:
1766 lad = (long_ad *)sptr;
1767 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1768 inode->i_sb->s_blocksize);
1769 lad->extLocation = cpu_to_lelb(epos->block);
1770 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1774 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1775 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1776 udf_update_tag(epos->bh->b_data, loffset);
1778 udf_update_tag(epos->bh->b_data,
1779 sizeof(struct allocExtDesc));
1780 mark_buffer_dirty_inode(epos->bh, inode);
1783 mark_inode_dirty(inode);
1788 etype = udf_write_aext(inode, epos, eloc, elen, inc);
1791 iinfo->i_lenAlloc += adsize;
1792 mark_inode_dirty(inode);
1794 aed = (struct allocExtDesc *)epos->bh->b_data;
1795 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1796 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1797 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1798 udf_update_tag(epos->bh->b_data,
1799 epos->offset + (inc ? 0 : adsize));
1801 udf_update_tag(epos->bh->b_data,
1802 sizeof(struct allocExtDesc));
1803 mark_buffer_dirty_inode(epos->bh, inode);
1809 int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
1810 kernel_lb_addr eloc, uint32_t elen, int inc)
1816 struct udf_inode_info *iinfo = UDF_I(inode);
1819 ptr = iinfo->i_ext.i_data + epos->offset -
1820 udf_file_entry_alloc_offset(inode) +
1823 ptr = epos->bh->b_data + epos->offset;
1825 switch (iinfo->i_alloc_type) {
1826 case ICBTAG_FLAG_AD_SHORT:
1827 sad = (short_ad *)ptr;
1828 sad->extLength = cpu_to_le32(elen);
1829 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1830 adsize = sizeof(short_ad);
1832 case ICBTAG_FLAG_AD_LONG:
1833 lad = (long_ad *)ptr;
1834 lad->extLength = cpu_to_le32(elen);
1835 lad->extLocation = cpu_to_lelb(eloc);
1836 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1837 adsize = sizeof(long_ad);
1844 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1845 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
1846 struct allocExtDesc *aed =
1847 (struct allocExtDesc *)epos->bh->b_data;
1848 udf_update_tag(epos->bh->b_data,
1849 le32_to_cpu(aed->lengthAllocDescs) +
1850 sizeof(struct allocExtDesc));
1852 mark_buffer_dirty_inode(epos->bh, inode);
1854 mark_inode_dirty(inode);
1858 epos->offset += adsize;
1860 return (elen >> 30);
1863 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1864 kernel_lb_addr *eloc, uint32_t *elen, int inc)
1868 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
1869 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
1871 epos->block = *eloc;
1872 epos->offset = sizeof(struct allocExtDesc);
1874 block = udf_get_lb_pblock(inode->i_sb, epos->block, 0);
1875 epos->bh = udf_tread(inode->i_sb, block);
1877 udf_debug("reading block %d failed!\n", block);
1885 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1886 kernel_lb_addr *eloc, uint32_t *elen, int inc)
1893 struct udf_inode_info *iinfo = UDF_I(inode);
1897 epos->offset = udf_file_entry_alloc_offset(inode);
1898 ptr = iinfo->i_ext.i_data + epos->offset -
1899 udf_file_entry_alloc_offset(inode) +
1901 alen = udf_file_entry_alloc_offset(inode) +
1905 epos->offset = sizeof(struct allocExtDesc);
1906 ptr = epos->bh->b_data + epos->offset;
1907 alen = sizeof(struct allocExtDesc) +
1908 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1912 switch (iinfo->i_alloc_type) {
1913 case ICBTAG_FLAG_AD_SHORT:
1914 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1917 etype = le32_to_cpu(sad->extLength) >> 30;
1918 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
1919 eloc->partitionReferenceNum =
1920 iinfo->i_location.partitionReferenceNum;
1921 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1923 case ICBTAG_FLAG_AD_LONG:
1924 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1927 etype = le32_to_cpu(lad->extLength) >> 30;
1928 *eloc = lelb_to_cpu(lad->extLocation);
1929 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1932 udf_debug("alloc_type = %d unsupported\n",
1933 iinfo->i_alloc_type);
1940 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1941 kernel_lb_addr neloc, uint32_t nelen)
1943 kernel_lb_addr oeloc;
1950 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
1951 udf_write_aext(inode, &epos, neloc, nelen, 1);
1953 nelen = (etype << 30) | oelen;
1955 udf_add_aext(inode, &epos, neloc, nelen, 1);
1958 return (nelen >> 30);
1961 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
1962 kernel_lb_addr eloc, uint32_t elen)
1964 struct extent_position oepos;
1967 struct allocExtDesc *aed;
1968 struct udf_inode_info *iinfo;
1975 iinfo = UDF_I(inode);
1976 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1977 adsize = sizeof(short_ad);
1978 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1979 adsize = sizeof(long_ad);
1984 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
1987 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
1988 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
1989 if (oepos.bh != epos.bh) {
1990 oepos.block = epos.block;
1994 oepos.offset = epos.offset - adsize;
1997 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
2000 if (epos.bh != oepos.bh) {
2001 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2002 udf_write_aext(inode, &oepos, eloc, elen, 1);
2003 udf_write_aext(inode, &oepos, eloc, elen, 1);
2005 iinfo->i_lenAlloc -= (adsize * 2);
2006 mark_inode_dirty(inode);
2008 aed = (struct allocExtDesc *)oepos.bh->b_data;
2009 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2010 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2011 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2012 udf_update_tag(oepos.bh->b_data,
2013 oepos.offset - (2 * adsize));
2015 udf_update_tag(oepos.bh->b_data,
2016 sizeof(struct allocExtDesc));
2017 mark_buffer_dirty_inode(oepos.bh, inode);
2020 udf_write_aext(inode, &oepos, eloc, elen, 1);
2022 iinfo->i_lenAlloc -= adsize;
2023 mark_inode_dirty(inode);
2025 aed = (struct allocExtDesc *)oepos.bh->b_data;
2026 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2027 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2028 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2029 udf_update_tag(oepos.bh->b_data,
2030 epos.offset - adsize);
2032 udf_update_tag(oepos.bh->b_data,
2033 sizeof(struct allocExtDesc));
2034 mark_buffer_dirty_inode(oepos.bh, inode);
2041 return (elen >> 30);
2044 int8_t inode_bmap(struct inode *inode, sector_t block,
2045 struct extent_position *pos, kernel_lb_addr *eloc,
2046 uint32_t *elen, sector_t *offset)
2048 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2049 loff_t lbcount = 0, bcount =
2050 (loff_t) block << blocksize_bits;
2052 struct udf_inode_info *iinfo;
2055 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2059 iinfo = UDF_I(inode);
2061 pos->block = iinfo->i_location;
2066 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2068 *offset = (bcount - lbcount) >> blocksize_bits;
2069 iinfo->i_lenExtents = lbcount;
2073 } while (lbcount <= bcount);
2075 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2080 long udf_block_map(struct inode *inode, sector_t block)
2082 kernel_lb_addr eloc;
2085 struct extent_position epos = {};
2090 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2091 (EXT_RECORDED_ALLOCATED >> 30))
2092 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
2099 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2100 return udf_fixed_to_variable(ret);