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);
73 * Clean-up before the specified inode is destroyed.
76 * This routine is called when the kernel destroys an inode structure
77 * ie. when iput() finds i_count == 0.
80 * July 1, 1997 - Andrew E. Mileski
81 * Written, tested, and released.
83 * Called at the last iput() if i_nlink is zero.
85 void udf_delete_inode(struct inode *inode)
87 truncate_inode_pages(&inode->i_data, 0);
89 if (is_bad_inode(inode))
96 udf_update_inode(inode, IS_SYNC(inode));
97 udf_free_inode(inode);
107 * If we are going to release inode from memory, we discard preallocation and
108 * truncate last inode extent to proper length. We could use drop_inode() but
109 * it's called under inode_lock and thus we cannot mark inode dirty there. We
110 * use clear_inode() but we have to make sure to write inode as it's not written
113 void udf_clear_inode(struct inode *inode)
115 struct udf_inode_info *iinfo;
116 if (!(inode->i_sb->s_flags & MS_RDONLY)) {
118 /* Discard preallocation for directories, symlinks, etc. */
119 udf_discard_prealloc(inode);
120 udf_truncate_tail_extent(inode);
122 write_inode_now(inode, 0);
124 iinfo = UDF_I(inode);
125 kfree(iinfo->i_ext.i_data);
126 iinfo->i_ext.i_data = NULL;
129 static int udf_writepage(struct page *page, struct writeback_control *wbc)
131 return block_write_full_page(page, udf_get_block, wbc);
134 static int udf_readpage(struct file *file, struct page *page)
136 return block_read_full_page(page, udf_get_block);
139 static int udf_write_begin(struct file *file, struct address_space *mapping,
140 loff_t pos, unsigned len, unsigned flags,
141 struct page **pagep, void **fsdata)
144 return block_write_begin(file, mapping, pos, len, flags, pagep, fsdata,
148 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
150 return generic_block_bmap(mapping, block, udf_get_block);
153 const struct address_space_operations udf_aops = {
154 .readpage = udf_readpage,
155 .writepage = udf_writepage,
156 .sync_page = block_sync_page,
157 .write_begin = udf_write_begin,
158 .write_end = generic_write_end,
162 void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
166 struct udf_inode_info *iinfo = UDF_I(inode);
167 struct writeback_control udf_wbc = {
168 .sync_mode = WB_SYNC_NONE,
172 /* from now on we have normal address_space methods */
173 inode->i_data.a_ops = &udf_aops;
175 if (!iinfo->i_lenAlloc) {
176 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
177 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
179 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
180 mark_inode_dirty(inode);
184 page = grab_cache_page(inode->i_mapping, 0);
185 BUG_ON(!PageLocked(page));
187 if (!PageUptodate(page)) {
189 memset(kaddr + iinfo->i_lenAlloc, 0x00,
190 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
191 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
193 flush_dcache_page(page);
194 SetPageUptodate(page);
197 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
199 iinfo->i_lenAlloc = 0;
200 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
201 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
203 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
205 inode->i_data.a_ops->writepage(page, &udf_wbc);
206 page_cache_release(page);
208 mark_inode_dirty(inode);
211 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
215 struct buffer_head *dbh = NULL;
219 struct extent_position epos;
221 struct udf_fileident_bh sfibh, dfibh;
222 loff_t f_pos = udf_ext0_offset(inode);
223 int size = udf_ext0_offset(inode) + inode->i_size;
224 struct fileIdentDesc cfi, *sfi, *dfi;
225 struct udf_inode_info *iinfo = UDF_I(inode);
227 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
228 alloctype = ICBTAG_FLAG_AD_SHORT;
230 alloctype = ICBTAG_FLAG_AD_LONG;
232 if (!inode->i_size) {
233 iinfo->i_alloc_type = alloctype;
234 mark_inode_dirty(inode);
238 /* alloc block, and copy data to it */
239 *block = udf_new_block(inode->i_sb, inode,
240 iinfo->i_location.partitionReferenceNum,
241 iinfo->i_location.logicalBlockNum, err);
244 newblock = udf_get_pblock(inode->i_sb, *block,
245 iinfo->i_location.partitionReferenceNum,
249 dbh = udf_tgetblk(inode->i_sb, newblock);
253 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
254 set_buffer_uptodate(dbh);
256 mark_buffer_dirty_inode(dbh, inode);
258 sfibh.soffset = sfibh.eoffset =
259 f_pos & (inode->i_sb->s_blocksize - 1);
260 sfibh.sbh = sfibh.ebh = NULL;
261 dfibh.soffset = dfibh.eoffset = 0;
262 dfibh.sbh = dfibh.ebh = dbh;
263 while (f_pos < size) {
264 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
265 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
271 iinfo->i_alloc_type = alloctype;
272 sfi->descTag.tagLocation = cpu_to_le32(*block);
273 dfibh.soffset = dfibh.eoffset;
274 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
275 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
276 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
278 le16_to_cpu(sfi->lengthOfImpUse))) {
279 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
284 mark_buffer_dirty_inode(dbh, inode);
286 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
288 iinfo->i_lenAlloc = 0;
289 eloc.logicalBlockNum = *block;
290 eloc.partitionReferenceNum =
291 iinfo->i_location.partitionReferenceNum;
292 elen = inode->i_sb->s_blocksize;
293 iinfo->i_lenExtents = elen;
295 epos.block = iinfo->i_location;
296 epos.offset = udf_file_entry_alloc_offset(inode);
297 udf_add_aext(inode, &epos, eloc, elen, 0);
301 mark_inode_dirty(inode);
305 static int udf_get_block(struct inode *inode, sector_t block,
306 struct buffer_head *bh_result, int create)
309 struct buffer_head *bh;
311 struct udf_inode_info *iinfo;
314 phys = udf_block_map(inode, block);
316 map_bh(bh_result, inode->i_sb, phys);
329 iinfo = UDF_I(inode);
330 if (block == iinfo->i_next_alloc_block + 1) {
331 iinfo->i_next_alloc_block++;
332 iinfo->i_next_alloc_goal++;
337 bh = inode_getblk(inode, block, &err, &phys, &new);
344 set_buffer_new(bh_result);
345 map_bh(bh_result, inode->i_sb, phys);
352 udf_warning(inode->i_sb, "udf_get_block", "block < 0");
356 static struct buffer_head *udf_getblk(struct inode *inode, long block,
357 int create, int *err)
359 struct buffer_head *bh;
360 struct buffer_head dummy;
363 dummy.b_blocknr = -1000;
364 *err = udf_get_block(inode, block, &dummy, create);
365 if (!*err && buffer_mapped(&dummy)) {
366 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
367 if (buffer_new(&dummy)) {
369 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
370 set_buffer_uptodate(bh);
372 mark_buffer_dirty_inode(bh, inode);
380 /* Extend the file by 'blocks' blocks, return the number of extents added */
381 int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
382 kernel_long_ad *last_ext, sector_t blocks)
385 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
386 struct super_block *sb = inode->i_sb;
387 kernel_lb_addr prealloc_loc = {};
388 int prealloc_len = 0;
389 struct udf_inode_info *iinfo;
391 /* The previous extent is fake and we should not extend by anything
392 * - there's nothing to do... */
396 iinfo = UDF_I(inode);
397 /* Round the last extent up to a multiple of block size */
398 if (last_ext->extLength & (sb->s_blocksize - 1)) {
399 last_ext->extLength =
400 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
401 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
402 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
403 iinfo->i_lenExtents =
404 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
405 ~(sb->s_blocksize - 1);
408 /* Last extent are just preallocated blocks? */
409 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
410 EXT_NOT_RECORDED_ALLOCATED) {
411 /* Save the extent so that we can reattach it to the end */
412 prealloc_loc = last_ext->extLocation;
413 prealloc_len = last_ext->extLength;
414 /* Mark the extent as a hole */
415 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
416 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
417 last_ext->extLocation.logicalBlockNum = 0;
418 last_ext->extLocation.partitionReferenceNum = 0;
421 /* Can we merge with the previous extent? */
422 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
423 EXT_NOT_RECORDED_NOT_ALLOCATED) {
424 add = ((1 << 30) - sb->s_blocksize -
425 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
426 sb->s_blocksize_bits;
430 last_ext->extLength += add << sb->s_blocksize_bits;
434 udf_add_aext(inode, last_pos, last_ext->extLocation,
435 last_ext->extLength, 1);
438 udf_write_aext(inode, last_pos, last_ext->extLocation,
439 last_ext->extLength, 1);
441 /* Managed to do everything necessary? */
445 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
446 last_ext->extLocation.logicalBlockNum = 0;
447 last_ext->extLocation.partitionReferenceNum = 0;
448 add = (1 << (30-sb->s_blocksize_bits)) - 1;
449 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
450 (add << sb->s_blocksize_bits);
452 /* Create enough extents to cover the whole hole */
453 while (blocks > add) {
455 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
456 last_ext->extLength, 1) == -1)
461 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
462 (blocks << sb->s_blocksize_bits);
463 if (udf_add_aext(inode, last_pos, last_ext->extLocation,
464 last_ext->extLength, 1) == -1)
470 /* Do we have some preallocated blocks saved? */
472 if (udf_add_aext(inode, last_pos, prealloc_loc,
473 prealloc_len, 1) == -1)
475 last_ext->extLocation = prealloc_loc;
476 last_ext->extLength = prealloc_len;
480 /* last_pos should point to the last written extent... */
481 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
482 last_pos->offset -= sizeof(short_ad);
483 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
484 last_pos->offset -= sizeof(long_ad);
491 static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
492 int *err, sector_t *phys, int *new)
494 static sector_t last_block;
495 struct buffer_head *result = NULL;
496 kernel_long_ad laarr[EXTENT_MERGE_SIZE];
497 struct extent_position prev_epos, cur_epos, next_epos;
498 int count = 0, startnum = 0, endnum = 0;
499 uint32_t elen = 0, tmpelen;
500 kernel_lb_addr eloc, tmpeloc;
502 loff_t lbcount = 0, b_off = 0;
503 uint32_t newblocknum, newblock;
506 struct udf_inode_info *iinfo = UDF_I(inode);
507 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
510 prev_epos.offset = udf_file_entry_alloc_offset(inode);
511 prev_epos.block = iinfo->i_location;
513 cur_epos = next_epos = prev_epos;
514 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
516 /* find the extent which contains the block we are looking for.
517 alternate between laarr[0] and laarr[1] for locations of the
518 current extent, and the previous extent */
520 if (prev_epos.bh != cur_epos.bh) {
521 brelse(prev_epos.bh);
523 prev_epos.bh = cur_epos.bh;
525 if (cur_epos.bh != next_epos.bh) {
527 get_bh(next_epos.bh);
528 cur_epos.bh = next_epos.bh;
533 prev_epos.block = cur_epos.block;
534 cur_epos.block = next_epos.block;
536 prev_epos.offset = cur_epos.offset;
537 cur_epos.offset = next_epos.offset;
539 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
545 laarr[c].extLength = (etype << 30) | elen;
546 laarr[c].extLocation = eloc;
548 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
549 pgoal = eloc.logicalBlockNum +
550 ((elen + inode->i_sb->s_blocksize - 1) >>
551 inode->i_sb->s_blocksize_bits);
554 } while (lbcount + elen <= b_off);
557 offset = b_off >> inode->i_sb->s_blocksize_bits;
559 * Move prev_epos and cur_epos into indirect extent if we are at
562 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
563 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
565 /* if the extent is allocated and recorded, return the block
566 if the extent is not a multiple of the blocksize, round up */
568 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
569 if (elen & (inode->i_sb->s_blocksize - 1)) {
570 elen = EXT_RECORDED_ALLOCATED |
571 ((elen + inode->i_sb->s_blocksize - 1) &
572 ~(inode->i_sb->s_blocksize - 1));
573 etype = udf_write_aext(inode, &cur_epos, eloc, elen, 1);
575 brelse(prev_epos.bh);
577 brelse(next_epos.bh);
578 newblock = udf_get_lb_pblock(inode->i_sb, eloc, offset);
584 /* Are we beyond EOF? */
593 /* Create a fake extent when there's not one */
594 memset(&laarr[0].extLocation, 0x00,
595 sizeof(kernel_lb_addr));
596 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
597 /* Will udf_extend_file() create real extent from
599 startnum = (offset > 0);
601 /* Create extents for the hole between EOF and offset */
602 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
604 brelse(prev_epos.bh);
606 brelse(next_epos.bh);
607 /* We don't really know the error here so we just make
615 /* We are not covered by a preallocated extent? */
616 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
617 EXT_NOT_RECORDED_ALLOCATED) {
618 /* Is there any real extent? - otherwise we overwrite
622 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
623 inode->i_sb->s_blocksize;
624 memset(&laarr[c].extLocation, 0x00,
625 sizeof(kernel_lb_addr));
632 endnum = startnum = ((count > 2) ? 2 : count);
634 /* if the current extent is in position 0,
635 swap it with the previous */
636 if (!c && count != 1) {
643 /* if the current block is located in an extent,
644 read the next extent */
645 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
647 laarr[c + 1].extLength = (etype << 30) | elen;
648 laarr[c + 1].extLocation = eloc;
656 /* if the current extent is not recorded but allocated, get the
657 * block in the extent corresponding to the requested block */
658 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
659 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
660 else { /* otherwise, allocate a new block */
661 if (iinfo->i_next_alloc_block == block)
662 goal = iinfo->i_next_alloc_goal;
665 if (!(goal = pgoal)) /* XXX: what was intended here? */
666 goal = iinfo->i_location.logicalBlockNum + 1;
669 newblocknum = udf_new_block(inode->i_sb, inode,
670 iinfo->i_location.partitionReferenceNum,
673 brelse(prev_epos.bh);
677 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
680 /* if the extent the requsted block is located in contains multiple
681 * blocks, split the extent into at most three extents. blocks prior
682 * to requested block, requested block, and blocks after requested
684 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
686 #ifdef UDF_PREALLOCATE
687 /* preallocate blocks */
688 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
691 /* merge any continuous blocks in laarr */
692 udf_merge_extents(inode, laarr, &endnum);
694 /* write back the new extents, inserting new extents if the new number
695 * of extents is greater than the old number, and deleting extents if
696 * the new number of extents is less than the old number */
697 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
699 brelse(prev_epos.bh);
701 newblock = udf_get_pblock(inode->i_sb, newblocknum,
702 iinfo->i_location.partitionReferenceNum, 0);
708 iinfo->i_next_alloc_block = block;
709 iinfo->i_next_alloc_goal = newblocknum;
710 inode->i_ctime = current_fs_time(inode->i_sb);
713 udf_sync_inode(inode);
715 mark_inode_dirty(inode);
720 static void udf_split_extents(struct inode *inode, int *c, int offset,
722 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
725 unsigned long blocksize = inode->i_sb->s_blocksize;
726 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
728 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
729 (laarr[*c].extLength >> 30) ==
730 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
732 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
733 blocksize - 1) >> blocksize_bits;
734 int8_t etype = (laarr[curr].extLength >> 30);
738 else if (!offset || blen == offset + 1) {
739 laarr[curr + 2] = laarr[curr + 1];
740 laarr[curr + 1] = laarr[curr];
742 laarr[curr + 3] = laarr[curr + 1];
743 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
747 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
748 udf_free_blocks(inode->i_sb, inode,
749 laarr[curr].extLocation,
751 laarr[curr].extLength =
752 EXT_NOT_RECORDED_NOT_ALLOCATED |
753 (offset << blocksize_bits);
754 laarr[curr].extLocation.logicalBlockNum = 0;
755 laarr[curr].extLocation.
756 partitionReferenceNum = 0;
758 laarr[curr].extLength = (etype << 30) |
759 (offset << blocksize_bits);
765 laarr[curr].extLocation.logicalBlockNum = newblocknum;
766 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
767 laarr[curr].extLocation.partitionReferenceNum =
768 UDF_I(inode)->i_location.partitionReferenceNum;
769 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
773 if (blen != offset + 1) {
774 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
775 laarr[curr].extLocation.logicalBlockNum +=
777 laarr[curr].extLength = (etype << 30) |
778 ((blen - (offset + 1)) << blocksize_bits);
785 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
786 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
789 int start, length = 0, currlength = 0, i;
791 if (*endnum >= (c + 1)) {
797 if ((laarr[c + 1].extLength >> 30) ==
798 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
800 length = currlength =
801 (((laarr[c + 1].extLength &
802 UDF_EXTENT_LENGTH_MASK) +
803 inode->i_sb->s_blocksize - 1) >>
804 inode->i_sb->s_blocksize_bits);
809 for (i = start + 1; i <= *endnum; i++) {
812 length += UDF_DEFAULT_PREALLOC_BLOCKS;
813 } else if ((laarr[i].extLength >> 30) ==
814 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
815 length += (((laarr[i].extLength &
816 UDF_EXTENT_LENGTH_MASK) +
817 inode->i_sb->s_blocksize - 1) >>
818 inode->i_sb->s_blocksize_bits);
824 int next = laarr[start].extLocation.logicalBlockNum +
825 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
826 inode->i_sb->s_blocksize - 1) >>
827 inode->i_sb->s_blocksize_bits);
828 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
829 laarr[start].extLocation.partitionReferenceNum,
830 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
831 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
834 if (start == (c + 1))
835 laarr[start].extLength +=
837 inode->i_sb->s_blocksize_bits);
839 memmove(&laarr[c + 2], &laarr[c + 1],
840 sizeof(long_ad) * (*endnum - (c + 1)));
842 laarr[c + 1].extLocation.logicalBlockNum = next;
843 laarr[c + 1].extLocation.partitionReferenceNum =
844 laarr[c].extLocation.
845 partitionReferenceNum;
846 laarr[c + 1].extLength =
847 EXT_NOT_RECORDED_ALLOCATED |
849 inode->i_sb->s_blocksize_bits);
853 for (i = start + 1; numalloc && i < *endnum; i++) {
854 int elen = ((laarr[i].extLength &
855 UDF_EXTENT_LENGTH_MASK) +
856 inode->i_sb->s_blocksize - 1) >>
857 inode->i_sb->s_blocksize_bits;
859 if (elen > numalloc) {
860 laarr[i].extLength -=
862 inode->i_sb->s_blocksize_bits);
866 if (*endnum > (i + 1))
870 (*endnum - (i + 1)));
875 UDF_I(inode)->i_lenExtents +=
876 numalloc << inode->i_sb->s_blocksize_bits;
881 static void udf_merge_extents(struct inode *inode,
882 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
886 unsigned long blocksize = inode->i_sb->s_blocksize;
887 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
889 for (i = 0; i < (*endnum - 1); i++) {
890 kernel_long_ad *li /*l[i]*/ = &laarr[i];
891 kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
893 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
894 (((li->extLength >> 30) ==
895 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
896 ((lip1->extLocation.logicalBlockNum -
897 li->extLocation.logicalBlockNum) ==
898 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
899 blocksize - 1) >> blocksize_bits)))) {
901 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
902 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
903 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
904 lip1->extLength = (lip1->extLength -
906 UDF_EXTENT_LENGTH_MASK) +
907 UDF_EXTENT_LENGTH_MASK) &
909 li->extLength = (li->extLength &
910 UDF_EXTENT_FLAG_MASK) +
911 (UDF_EXTENT_LENGTH_MASK + 1) -
913 lip1->extLocation.logicalBlockNum =
914 li->extLocation.logicalBlockNum +
916 UDF_EXTENT_LENGTH_MASK) >>
919 li->extLength = lip1->extLength +
921 UDF_EXTENT_LENGTH_MASK) +
922 blocksize - 1) & ~(blocksize - 1));
923 if (*endnum > (i + 2))
924 memmove(&laarr[i + 1], &laarr[i + 2],
926 (*endnum - (i + 2)));
930 } else if (((li->extLength >> 30) ==
931 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
932 ((lip1->extLength >> 30) ==
933 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
934 udf_free_blocks(inode->i_sb, inode, li->extLocation, 0,
936 UDF_EXTENT_LENGTH_MASK) +
937 blocksize - 1) >> blocksize_bits);
938 li->extLocation.logicalBlockNum = 0;
939 li->extLocation.partitionReferenceNum = 0;
941 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
942 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
943 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
944 lip1->extLength = (lip1->extLength -
946 UDF_EXTENT_LENGTH_MASK) +
947 UDF_EXTENT_LENGTH_MASK) &
949 li->extLength = (li->extLength &
950 UDF_EXTENT_FLAG_MASK) +
951 (UDF_EXTENT_LENGTH_MASK + 1) -
954 li->extLength = lip1->extLength +
956 UDF_EXTENT_LENGTH_MASK) +
957 blocksize - 1) & ~(blocksize - 1));
958 if (*endnum > (i + 2))
959 memmove(&laarr[i + 1], &laarr[i + 2],
961 (*endnum - (i + 2)));
965 } else if ((li->extLength >> 30) ==
966 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
967 udf_free_blocks(inode->i_sb, inode,
970 UDF_EXTENT_LENGTH_MASK) +
971 blocksize - 1) >> blocksize_bits);
972 li->extLocation.logicalBlockNum = 0;
973 li->extLocation.partitionReferenceNum = 0;
974 li->extLength = (li->extLength &
975 UDF_EXTENT_LENGTH_MASK) |
976 EXT_NOT_RECORDED_NOT_ALLOCATED;
981 static void udf_update_extents(struct inode *inode,
982 kernel_long_ad laarr[EXTENT_MERGE_SIZE],
983 int startnum, int endnum,
984 struct extent_position *epos)
987 kernel_lb_addr tmploc;
990 if (startnum > endnum) {
991 for (i = 0; i < (startnum - endnum); i++)
992 udf_delete_aext(inode, *epos, laarr[i].extLocation,
994 } else if (startnum < endnum) {
995 for (i = 0; i < (endnum - startnum); i++) {
996 udf_insert_aext(inode, *epos, laarr[i].extLocation,
998 udf_next_aext(inode, epos, &laarr[i].extLocation,
999 &laarr[i].extLength, 1);
1004 for (i = start; i < endnum; i++) {
1005 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1006 udf_write_aext(inode, epos, laarr[i].extLocation,
1007 laarr[i].extLength, 1);
1011 struct buffer_head *udf_bread(struct inode *inode, int block,
1012 int create, int *err)
1014 struct buffer_head *bh = NULL;
1016 bh = udf_getblk(inode, block, create, err);
1020 if (buffer_uptodate(bh))
1023 ll_rw_block(READ, 1, &bh);
1026 if (buffer_uptodate(bh))
1034 void udf_truncate(struct inode *inode)
1038 struct udf_inode_info *iinfo;
1040 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1041 S_ISLNK(inode->i_mode)))
1043 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1047 iinfo = UDF_I(inode);
1048 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1049 if (inode->i_sb->s_blocksize <
1050 (udf_file_entry_alloc_offset(inode) +
1052 udf_expand_file_adinicb(inode, inode->i_size, &err);
1053 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1054 inode->i_size = iinfo->i_lenAlloc;
1058 udf_truncate_extents(inode);
1060 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
1061 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
1062 0x00, inode->i_sb->s_blocksize -
1063 offset - udf_file_entry_alloc_offset(inode));
1064 iinfo->i_lenAlloc = inode->i_size;
1067 block_truncate_page(inode->i_mapping, inode->i_size,
1069 udf_truncate_extents(inode);
1072 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1074 udf_sync_inode(inode);
1076 mark_inode_dirty(inode);
1080 static void __udf_read_inode(struct inode *inode)
1082 struct buffer_head *bh = NULL;
1083 struct fileEntry *fe;
1085 struct udf_inode_info *iinfo = UDF_I(inode);
1088 * Set defaults, but the inode is still incomplete!
1089 * Note: get_new_inode() sets the following on a new inode:
1092 * i_flags = sb->s_flags
1094 * clean_inode(): zero fills and sets
1099 bh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 0, &ident);
1101 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
1103 make_bad_inode(inode);
1107 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1108 ident != TAG_IDENT_USE) {
1109 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1110 "failed ident=%d\n", inode->i_ino, ident);
1112 make_bad_inode(inode);
1116 fe = (struct fileEntry *)bh->b_data;
1118 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1119 struct buffer_head *ibh = NULL, *nbh = NULL;
1120 struct indirectEntry *ie;
1122 ibh = udf_read_ptagged(inode->i_sb, iinfo->i_location, 1,
1124 if (ident == TAG_IDENT_IE) {
1127 ie = (struct indirectEntry *)ibh->b_data;
1129 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1131 if (ie->indirectICB.extLength &&
1132 (nbh = udf_read_ptagged(inode->i_sb, loc, 0,
1134 if (ident == TAG_IDENT_FE ||
1135 ident == TAG_IDENT_EFE) {
1136 memcpy(&iinfo->i_location,
1138 sizeof(kernel_lb_addr));
1142 __udf_read_inode(inode);
1155 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1156 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
1157 le16_to_cpu(fe->icbTag.strategyType));
1159 make_bad_inode(inode);
1162 udf_fill_inode(inode, bh);
1167 static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1169 struct fileEntry *fe;
1170 struct extendedFileEntry *efe;
1174 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1175 struct udf_inode_info *iinfo = UDF_I(inode);
1177 fe = (struct fileEntry *)bh->b_data;
1178 efe = (struct extendedFileEntry *)bh->b_data;
1180 if (fe->icbTag.strategyType == cpu_to_le16(4))
1181 iinfo->i_strat4096 = 0;
1182 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1183 iinfo->i_strat4096 = 1;
1185 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1186 ICBTAG_FLAG_AD_MASK;
1187 iinfo->i_unique = 0;
1188 iinfo->i_lenEAttr = 0;
1189 iinfo->i_lenExtents = 0;
1190 iinfo->i_lenAlloc = 0;
1191 iinfo->i_next_alloc_block = 0;
1192 iinfo->i_next_alloc_goal = 0;
1193 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1196 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1197 sizeof(struct extendedFileEntry))) {
1198 make_bad_inode(inode);
1201 memcpy(iinfo->i_ext.i_data,
1202 bh->b_data + sizeof(struct extendedFileEntry),
1203 inode->i_sb->s_blocksize -
1204 sizeof(struct extendedFileEntry));
1205 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1208 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1209 sizeof(struct fileEntry))) {
1210 make_bad_inode(inode);
1213 memcpy(iinfo->i_ext.i_data,
1214 bh->b_data + sizeof(struct fileEntry),
1215 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1216 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1219 iinfo->i_lenAlloc = le32_to_cpu(
1220 ((struct unallocSpaceEntry *)bh->b_data)->
1222 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1223 sizeof(struct unallocSpaceEntry))) {
1224 make_bad_inode(inode);
1227 memcpy(iinfo->i_ext.i_data,
1228 bh->b_data + sizeof(struct unallocSpaceEntry),
1229 inode->i_sb->s_blocksize -
1230 sizeof(struct unallocSpaceEntry));
1234 inode->i_uid = le32_to_cpu(fe->uid);
1235 if (inode->i_uid == -1 ||
1236 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1237 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1238 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1240 inode->i_gid = le32_to_cpu(fe->gid);
1241 if (inode->i_gid == -1 ||
1242 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1243 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1244 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1246 inode->i_nlink = le16_to_cpu(fe->fileLinkCount);
1247 if (!inode->i_nlink)
1250 inode->i_size = le64_to_cpu(fe->informationLength);
1251 iinfo->i_lenExtents = inode->i_size;
1253 inode->i_mode = udf_convert_permissions(fe);
1254 inode->i_mode &= ~UDF_SB(inode->i_sb)->s_umask;
1256 if (iinfo->i_efe == 0) {
1257 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1258 (inode->i_sb->s_blocksize_bits - 9);
1260 if (udf_stamp_to_time(&convtime, &convtime_usec,
1261 lets_to_cpu(fe->accessTime))) {
1262 inode->i_atime.tv_sec = convtime;
1263 inode->i_atime.tv_nsec = convtime_usec * 1000;
1265 inode->i_atime = sbi->s_record_time;
1268 if (udf_stamp_to_time(&convtime, &convtime_usec,
1269 lets_to_cpu(fe->modificationTime))) {
1270 inode->i_mtime.tv_sec = convtime;
1271 inode->i_mtime.tv_nsec = convtime_usec * 1000;
1273 inode->i_mtime = sbi->s_record_time;
1276 if (udf_stamp_to_time(&convtime, &convtime_usec,
1277 lets_to_cpu(fe->attrTime))) {
1278 inode->i_ctime.tv_sec = convtime;
1279 inode->i_ctime.tv_nsec = convtime_usec * 1000;
1281 inode->i_ctime = sbi->s_record_time;
1284 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1285 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1286 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1287 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
1289 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1290 (inode->i_sb->s_blocksize_bits - 9);
1292 if (udf_stamp_to_time(&convtime, &convtime_usec,
1293 lets_to_cpu(efe->accessTime))) {
1294 inode->i_atime.tv_sec = convtime;
1295 inode->i_atime.tv_nsec = convtime_usec * 1000;
1297 inode->i_atime = sbi->s_record_time;
1300 if (udf_stamp_to_time(&convtime, &convtime_usec,
1301 lets_to_cpu(efe->modificationTime))) {
1302 inode->i_mtime.tv_sec = convtime;
1303 inode->i_mtime.tv_nsec = convtime_usec * 1000;
1305 inode->i_mtime = sbi->s_record_time;
1308 if (udf_stamp_to_time(&convtime, &convtime_usec,
1309 lets_to_cpu(efe->createTime))) {
1310 iinfo->i_crtime.tv_sec = convtime;
1311 iinfo->i_crtime.tv_nsec = convtime_usec * 1000;
1313 iinfo->i_crtime = sbi->s_record_time;
1316 if (udf_stamp_to_time(&convtime, &convtime_usec,
1317 lets_to_cpu(efe->attrTime))) {
1318 inode->i_ctime.tv_sec = convtime;
1319 inode->i_ctime.tv_nsec = convtime_usec * 1000;
1321 inode->i_ctime = sbi->s_record_time;
1324 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1325 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1326 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1327 offset = sizeof(struct extendedFileEntry) +
1331 switch (fe->icbTag.fileType) {
1332 case ICBTAG_FILE_TYPE_DIRECTORY:
1333 inode->i_op = &udf_dir_inode_operations;
1334 inode->i_fop = &udf_dir_operations;
1335 inode->i_mode |= S_IFDIR;
1338 case ICBTAG_FILE_TYPE_REALTIME:
1339 case ICBTAG_FILE_TYPE_REGULAR:
1340 case ICBTAG_FILE_TYPE_UNDEF:
1341 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1342 inode->i_data.a_ops = &udf_adinicb_aops;
1344 inode->i_data.a_ops = &udf_aops;
1345 inode->i_op = &udf_file_inode_operations;
1346 inode->i_fop = &udf_file_operations;
1347 inode->i_mode |= S_IFREG;
1349 case ICBTAG_FILE_TYPE_BLOCK:
1350 inode->i_mode |= S_IFBLK;
1352 case ICBTAG_FILE_TYPE_CHAR:
1353 inode->i_mode |= S_IFCHR;
1355 case ICBTAG_FILE_TYPE_FIFO:
1356 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1358 case ICBTAG_FILE_TYPE_SOCKET:
1359 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1361 case ICBTAG_FILE_TYPE_SYMLINK:
1362 inode->i_data.a_ops = &udf_symlink_aops;
1363 inode->i_op = &page_symlink_inode_operations;
1364 inode->i_mode = S_IFLNK | S_IRWXUGO;
1367 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1368 "file type=%d\n", inode->i_ino,
1369 fe->icbTag.fileType);
1370 make_bad_inode(inode);
1373 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1374 struct deviceSpec *dsea =
1375 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1377 init_special_inode(inode, inode->i_mode,
1378 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1379 le32_to_cpu(dsea->minorDeviceIdent)));
1380 /* Developer ID ??? */
1382 make_bad_inode(inode);
1386 static int udf_alloc_i_data(struct inode *inode, size_t size)
1388 struct udf_inode_info *iinfo = UDF_I(inode);
1389 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1391 if (!iinfo->i_ext.i_data) {
1392 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1393 "no free memory\n", inode->i_ino);
1400 static mode_t udf_convert_permissions(struct fileEntry *fe)
1403 uint32_t permissions;
1406 permissions = le32_to_cpu(fe->permissions);
1407 flags = le16_to_cpu(fe->icbTag.flags);
1409 mode = ((permissions) & S_IRWXO) |
1410 ((permissions >> 2) & S_IRWXG) |
1411 ((permissions >> 4) & S_IRWXU) |
1412 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1413 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1414 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1423 * Write out the specified inode.
1426 * This routine is called whenever an inode is synced.
1427 * Currently this routine is just a placeholder.
1430 * July 1, 1997 - Andrew E. Mileski
1431 * Written, tested, and released.
1434 int udf_write_inode(struct inode *inode, int sync)
1439 ret = udf_update_inode(inode, sync);
1445 int udf_sync_inode(struct inode *inode)
1447 return udf_update_inode(inode, 1);
1450 static int udf_update_inode(struct inode *inode, int do_sync)
1452 struct buffer_head *bh = NULL;
1453 struct fileEntry *fe;
1454 struct extendedFileEntry *efe;
1458 kernel_timestamp cpu_time;
1460 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1461 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1462 struct udf_inode_info *iinfo = UDF_I(inode);
1464 bh = udf_tread(inode->i_sb,
1465 udf_get_lb_pblock(inode->i_sb,
1466 iinfo->i_location, 0));
1468 udf_debug("bread failure\n");
1472 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1474 fe = (struct fileEntry *)bh->b_data;
1475 efe = (struct extendedFileEntry *)bh->b_data;
1477 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1478 struct unallocSpaceEntry *use =
1479 (struct unallocSpaceEntry *)bh->b_data;
1481 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1482 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1483 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1484 sizeof(struct unallocSpaceEntry));
1485 crclen = sizeof(struct unallocSpaceEntry) +
1486 iinfo->i_lenAlloc - sizeof(tag);
1487 use->descTag.tagLocation = cpu_to_le32(
1490 use->descTag.descCRCLength = cpu_to_le16(crclen);
1491 use->descTag.descCRC = cpu_to_le16(udf_crc((char *)use +
1492 sizeof(tag), crclen,
1494 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1496 mark_buffer_dirty(bh);
1501 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1502 fe->uid = cpu_to_le32(-1);
1504 fe->uid = cpu_to_le32(inode->i_uid);
1506 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1507 fe->gid = cpu_to_le32(-1);
1509 fe->gid = cpu_to_le32(inode->i_gid);
1511 udfperms = ((inode->i_mode & S_IRWXO)) |
1512 ((inode->i_mode & S_IRWXG) << 2) |
1513 ((inode->i_mode & S_IRWXU) << 4);
1515 udfperms |= (le32_to_cpu(fe->permissions) &
1516 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1517 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1518 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1519 fe->permissions = cpu_to_le32(udfperms);
1521 if (S_ISDIR(inode->i_mode))
1522 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1524 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1526 fe->informationLength = cpu_to_le64(inode->i_size);
1528 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1530 struct deviceSpec *dsea =
1531 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1533 dsea = (struct deviceSpec *)
1534 udf_add_extendedattr(inode,
1535 sizeof(struct deviceSpec) +
1536 sizeof(regid), 12, 0x3);
1537 dsea->attrType = cpu_to_le32(12);
1538 dsea->attrSubtype = 1;
1539 dsea->attrLength = cpu_to_le32(
1540 sizeof(struct deviceSpec) +
1542 dsea->impUseLength = cpu_to_le32(sizeof(regid));
1544 eid = (regid *)dsea->impUse;
1545 memset(eid, 0, sizeof(regid));
1546 strcpy(eid->ident, UDF_ID_DEVELOPER);
1547 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1548 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1549 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1550 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1553 if (iinfo->i_efe == 0) {
1554 memcpy(bh->b_data + sizeof(struct fileEntry),
1555 iinfo->i_ext.i_data,
1556 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1557 fe->logicalBlocksRecorded = cpu_to_le64(
1558 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1559 (blocksize_bits - 9));
1561 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1562 fe->accessTime = cpu_to_lets(cpu_time);
1563 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1564 fe->modificationTime = cpu_to_lets(cpu_time);
1565 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1566 fe->attrTime = cpu_to_lets(cpu_time);
1567 memset(&(fe->impIdent), 0, sizeof(regid));
1568 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1569 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1570 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1571 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1572 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1573 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1574 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1575 crclen = sizeof(struct fileEntry);
1577 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1578 iinfo->i_ext.i_data,
1579 inode->i_sb->s_blocksize -
1580 sizeof(struct extendedFileEntry));
1581 efe->objectSize = cpu_to_le64(inode->i_size);
1582 efe->logicalBlocksRecorded = cpu_to_le64(
1583 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1584 (blocksize_bits - 9));
1586 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1587 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1588 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1589 iinfo->i_crtime = inode->i_atime;
1591 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1592 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1593 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1594 iinfo->i_crtime = inode->i_mtime;
1596 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1597 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1598 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1599 iinfo->i_crtime = inode->i_ctime;
1601 if (udf_time_to_stamp(&cpu_time, inode->i_atime))
1602 efe->accessTime = cpu_to_lets(cpu_time);
1603 if (udf_time_to_stamp(&cpu_time, inode->i_mtime))
1604 efe->modificationTime = cpu_to_lets(cpu_time);
1605 if (udf_time_to_stamp(&cpu_time, iinfo->i_crtime))
1606 efe->createTime = cpu_to_lets(cpu_time);
1607 if (udf_time_to_stamp(&cpu_time, inode->i_ctime))
1608 efe->attrTime = cpu_to_lets(cpu_time);
1610 memset(&(efe->impIdent), 0, sizeof(regid));
1611 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1612 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1613 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1614 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1615 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1616 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1617 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1618 crclen = sizeof(struct extendedFileEntry);
1620 if (iinfo->i_strat4096) {
1621 fe->icbTag.strategyType = cpu_to_le16(4096);
1622 fe->icbTag.strategyParameter = cpu_to_le16(1);
1623 fe->icbTag.numEntries = cpu_to_le16(2);
1625 fe->icbTag.strategyType = cpu_to_le16(4);
1626 fe->icbTag.numEntries = cpu_to_le16(1);
1629 if (S_ISDIR(inode->i_mode))
1630 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1631 else if (S_ISREG(inode->i_mode))
1632 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1633 else if (S_ISLNK(inode->i_mode))
1634 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1635 else if (S_ISBLK(inode->i_mode))
1636 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1637 else if (S_ISCHR(inode->i_mode))
1638 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1639 else if (S_ISFIFO(inode->i_mode))
1640 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1641 else if (S_ISSOCK(inode->i_mode))
1642 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1644 icbflags = iinfo->i_alloc_type |
1645 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1646 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1647 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1648 (le16_to_cpu(fe->icbTag.flags) &
1649 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1650 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1652 fe->icbTag.flags = cpu_to_le16(icbflags);
1653 if (sbi->s_udfrev >= 0x0200)
1654 fe->descTag.descVersion = cpu_to_le16(3);
1656 fe->descTag.descVersion = cpu_to_le16(2);
1657 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1658 fe->descTag.tagLocation = cpu_to_le32(
1659 iinfo->i_location.logicalBlockNum);
1660 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc -
1662 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1663 fe->descTag.descCRC = cpu_to_le16(udf_crc((char *)fe + sizeof(tag),
1665 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1667 /* write the data blocks */
1668 mark_buffer_dirty(bh);
1670 sync_dirty_buffer(bh);
1671 if (buffer_req(bh) && !buffer_uptodate(bh)) {
1672 printk(KERN_WARNING "IO error syncing udf inode "
1673 "[%s:%08lx]\n", inode->i_sb->s_id,
1683 struct inode *udf_iget(struct super_block *sb, kernel_lb_addr ino)
1685 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1686 struct inode *inode = iget_locked(sb, block);
1691 if (inode->i_state & I_NEW) {
1692 memcpy(&UDF_I(inode)->i_location, &ino, sizeof(kernel_lb_addr));
1693 __udf_read_inode(inode);
1694 unlock_new_inode(inode);
1697 if (is_bad_inode(inode))
1700 if (ino.logicalBlockNum >= UDF_SB(sb)->
1701 s_partmaps[ino.partitionReferenceNum].s_partition_len) {
1702 udf_debug("block=%d, partition=%d out of range\n",
1703 ino.logicalBlockNum, ino.partitionReferenceNum);
1704 make_bad_inode(inode);
1715 int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
1716 kernel_lb_addr eloc, uint32_t elen, int inc)
1719 short_ad *sad = NULL;
1720 long_ad *lad = NULL;
1721 struct allocExtDesc *aed;
1724 struct udf_inode_info *iinfo = UDF_I(inode);
1727 ptr = iinfo->i_ext.i_data + epos->offset -
1728 udf_file_entry_alloc_offset(inode) +
1731 ptr = epos->bh->b_data + epos->offset;
1733 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1734 adsize = sizeof(short_ad);
1735 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1736 adsize = sizeof(long_ad);
1740 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1742 struct buffer_head *nbh;
1744 kernel_lb_addr obloc = epos->block;
1746 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1747 obloc.partitionReferenceNum,
1748 obloc.logicalBlockNum, &err);
1749 if (!epos->block.logicalBlockNum)
1751 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1757 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1758 set_buffer_uptodate(nbh);
1760 mark_buffer_dirty_inode(nbh, inode);
1762 aed = (struct allocExtDesc *)(nbh->b_data);
1763 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1764 aed->previousAllocExtLocation =
1765 cpu_to_le32(obloc.logicalBlockNum);
1766 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1767 loffset = epos->offset;
1768 aed->lengthAllocDescs = cpu_to_le32(adsize);
1769 sptr = ptr - adsize;
1770 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1771 memcpy(dptr, sptr, adsize);
1772 epos->offset = sizeof(struct allocExtDesc) + adsize;
1774 loffset = epos->offset + adsize;
1775 aed->lengthAllocDescs = cpu_to_le32(0);
1777 epos->offset = sizeof(struct allocExtDesc);
1780 aed = (struct allocExtDesc *)epos->bh->b_data;
1781 aed->lengthAllocDescs =
1782 cpu_to_le32(le32_to_cpu(
1783 aed->lengthAllocDescs) + adsize);
1785 iinfo->i_lenAlloc += adsize;
1786 mark_inode_dirty(inode);
1789 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1790 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1791 epos->block.logicalBlockNum, sizeof(tag));
1793 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1794 epos->block.logicalBlockNum, sizeof(tag));
1795 switch (iinfo->i_alloc_type) {
1796 case ICBTAG_FLAG_AD_SHORT:
1797 sad = (short_ad *)sptr;
1798 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1799 inode->i_sb->s_blocksize);
1801 cpu_to_le32(epos->block.logicalBlockNum);
1803 case ICBTAG_FLAG_AD_LONG:
1804 lad = (long_ad *)sptr;
1805 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1806 inode->i_sb->s_blocksize);
1807 lad->extLocation = cpu_to_lelb(epos->block);
1808 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1812 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1813 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1814 udf_update_tag(epos->bh->b_data, loffset);
1816 udf_update_tag(epos->bh->b_data,
1817 sizeof(struct allocExtDesc));
1818 mark_buffer_dirty_inode(epos->bh, inode);
1821 mark_inode_dirty(inode);
1826 etype = udf_write_aext(inode, epos, eloc, elen, inc);
1829 iinfo->i_lenAlloc += adsize;
1830 mark_inode_dirty(inode);
1832 aed = (struct allocExtDesc *)epos->bh->b_data;
1833 aed->lengthAllocDescs =
1834 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) +
1836 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1837 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1838 udf_update_tag(epos->bh->b_data,
1839 epos->offset + (inc ? 0 : adsize));
1841 udf_update_tag(epos->bh->b_data,
1842 sizeof(struct allocExtDesc));
1843 mark_buffer_dirty_inode(epos->bh, inode);
1849 int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
1850 kernel_lb_addr eloc, uint32_t elen, int inc)
1856 struct udf_inode_info *iinfo = UDF_I(inode);
1859 ptr = iinfo->i_ext.i_data + epos->offset -
1860 udf_file_entry_alloc_offset(inode) +
1863 ptr = epos->bh->b_data + epos->offset;
1865 switch (iinfo->i_alloc_type) {
1866 case ICBTAG_FLAG_AD_SHORT:
1867 sad = (short_ad *)ptr;
1868 sad->extLength = cpu_to_le32(elen);
1869 sad->extPosition = cpu_to_le32(eloc.logicalBlockNum);
1870 adsize = sizeof(short_ad);
1872 case ICBTAG_FLAG_AD_LONG:
1873 lad = (long_ad *)ptr;
1874 lad->extLength = cpu_to_le32(elen);
1875 lad->extLocation = cpu_to_lelb(eloc);
1876 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1877 adsize = sizeof(long_ad);
1884 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1885 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
1886 struct allocExtDesc *aed =
1887 (struct allocExtDesc *)epos->bh->b_data;
1888 udf_update_tag(epos->bh->b_data,
1889 le32_to_cpu(aed->lengthAllocDescs) +
1890 sizeof(struct allocExtDesc));
1892 mark_buffer_dirty_inode(epos->bh, inode);
1894 mark_inode_dirty(inode);
1898 epos->offset += adsize;
1900 return (elen >> 30);
1903 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1904 kernel_lb_addr *eloc, uint32_t *elen, int inc)
1908 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
1909 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
1911 epos->block = *eloc;
1912 epos->offset = sizeof(struct allocExtDesc);
1914 block = udf_get_lb_pblock(inode->i_sb, epos->block, 0);
1915 epos->bh = udf_tread(inode->i_sb, block);
1917 udf_debug("reading block %d failed!\n", block);
1925 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1926 kernel_lb_addr *eloc, uint32_t *elen, int inc)
1933 struct udf_inode_info *iinfo = UDF_I(inode);
1937 epos->offset = udf_file_entry_alloc_offset(inode);
1938 ptr = iinfo->i_ext.i_data + epos->offset -
1939 udf_file_entry_alloc_offset(inode) +
1941 alen = udf_file_entry_alloc_offset(inode) +
1945 epos->offset = sizeof(struct allocExtDesc);
1946 ptr = epos->bh->b_data + epos->offset;
1947 alen = sizeof(struct allocExtDesc) +
1948 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1952 switch (iinfo->i_alloc_type) {
1953 case ICBTAG_FLAG_AD_SHORT:
1954 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1957 etype = le32_to_cpu(sad->extLength) >> 30;
1958 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
1959 eloc->partitionReferenceNum =
1960 iinfo->i_location.partitionReferenceNum;
1961 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1963 case ICBTAG_FLAG_AD_LONG:
1964 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1967 etype = le32_to_cpu(lad->extLength) >> 30;
1968 *eloc = lelb_to_cpu(lad->extLocation);
1969 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1972 udf_debug("alloc_type = %d unsupported\n",
1973 iinfo->i_alloc_type);
1980 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1981 kernel_lb_addr neloc, uint32_t nelen)
1983 kernel_lb_addr oeloc;
1990 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
1991 udf_write_aext(inode, &epos, neloc, nelen, 1);
1993 nelen = (etype << 30) | oelen;
1995 udf_add_aext(inode, &epos, neloc, nelen, 1);
1998 return (nelen >> 30);
2001 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2002 kernel_lb_addr eloc, uint32_t elen)
2004 struct extent_position oepos;
2007 struct allocExtDesc *aed;
2008 struct udf_inode_info *iinfo;
2015 iinfo = UDF_I(inode);
2016 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2017 adsize = sizeof(short_ad);
2018 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2019 adsize = sizeof(long_ad);
2024 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2027 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2028 udf_write_aext(inode, &oepos, eloc, (etype << 30) | elen, 1);
2029 if (oepos.bh != epos.bh) {
2030 oepos.block = epos.block;
2034 oepos.offset = epos.offset - adsize;
2037 memset(&eloc, 0x00, sizeof(kernel_lb_addr));
2040 if (epos.bh != oepos.bh) {
2041 udf_free_blocks(inode->i_sb, inode, epos.block, 0, 1);
2042 udf_write_aext(inode, &oepos, eloc, elen, 1);
2043 udf_write_aext(inode, &oepos, eloc, elen, 1);
2045 iinfo->i_lenAlloc -= (adsize * 2);
2046 mark_inode_dirty(inode);
2048 aed = (struct allocExtDesc *)oepos.bh->b_data;
2049 aed->lengthAllocDescs =
2050 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2052 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2053 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2054 udf_update_tag(oepos.bh->b_data,
2055 oepos.offset - (2 * adsize));
2057 udf_update_tag(oepos.bh->b_data,
2058 sizeof(struct allocExtDesc));
2059 mark_buffer_dirty_inode(oepos.bh, inode);
2062 udf_write_aext(inode, &oepos, eloc, elen, 1);
2064 iinfo->i_lenAlloc -= adsize;
2065 mark_inode_dirty(inode);
2067 aed = (struct allocExtDesc *)oepos.bh->b_data;
2068 aed->lengthAllocDescs =
2069 cpu_to_le32(le32_to_cpu(aed->lengthAllocDescs) -
2071 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2072 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2073 udf_update_tag(oepos.bh->b_data,
2074 epos.offset - adsize);
2076 udf_update_tag(oepos.bh->b_data,
2077 sizeof(struct allocExtDesc));
2078 mark_buffer_dirty_inode(oepos.bh, inode);
2085 return (elen >> 30);
2088 int8_t inode_bmap(struct inode *inode, sector_t block,
2089 struct extent_position *pos, kernel_lb_addr *eloc,
2090 uint32_t *elen, sector_t *offset)
2092 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2093 loff_t lbcount = 0, bcount =
2094 (loff_t) block << blocksize_bits;
2096 struct udf_inode_info *iinfo;
2099 printk(KERN_ERR "udf: inode_bmap: block < 0\n");
2103 iinfo = UDF_I(inode);
2105 pos->block = iinfo->i_location;
2110 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2112 *offset = (bcount - lbcount) >> blocksize_bits;
2113 iinfo->i_lenExtents = lbcount;
2117 } while (lbcount <= bcount);
2119 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2124 long udf_block_map(struct inode *inode, sector_t block)
2126 kernel_lb_addr eloc;
2129 struct extent_position epos = {};
2134 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2135 (EXT_RECORDED_ALLOCATED >> 30))
2136 ret = udf_get_lb_pblock(inode->i_sb, eloc, offset);
2143 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2144 return udf_fixed_to_variable(ret);