5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
22 * 12/06/98 blf partition support in udf_iget, udf_block_map
24 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/buffer_head.h>
37 #include <linux/writeback.h>
38 #include <linux/slab.h>
39 #include <linux/crc-itu-t.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_sync_inode(struct inode *inode);
54 static int udf_alloc_i_data(struct inode *inode, size_t size);
55 static struct buffer_head *inode_getblk(struct inode *, sector_t, int *,
57 static int8_t udf_insert_aext(struct inode *, struct extent_position,
58 struct kernel_lb_addr, uint32_t);
59 static void udf_split_extents(struct inode *, int *, int, int,
60 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
61 static void udf_prealloc_extents(struct inode *, int, int,
62 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
63 static void udf_merge_extents(struct inode *,
64 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
65 static void udf_update_extents(struct inode *,
66 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
67 struct extent_position *);
68 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
71 void udf_evict_inode(struct inode *inode)
73 struct udf_inode_info *iinfo = UDF_I(inode);
76 truncate_inode_pages(&inode->i_data, 0);
78 if (!inode->i_nlink && !is_bad_inode(inode)) {
82 udf_update_inode(inode, IS_SYNC(inode));
84 invalidate_inode_buffers(inode);
86 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
87 inode->i_size != iinfo->i_lenExtents) {
88 printk(KERN_WARNING "UDF-fs (%s): Inode %lu (mode %o) has "
89 "inode size %llu different from extent length %llu. "
90 "Filesystem need not be standards compliant.\n",
91 inode->i_sb->s_id, inode->i_ino, inode->i_mode,
92 (unsigned long long)inode->i_size,
93 (unsigned long long)iinfo->i_lenExtents);
95 kfree(iinfo->i_ext.i_data);
96 iinfo->i_ext.i_data = NULL;
98 udf_free_inode(inode);
102 static int udf_writepage(struct page *page, struct writeback_control *wbc)
104 return block_write_full_page(page, udf_get_block, wbc);
107 static int udf_readpage(struct file *file, struct page *page)
109 return block_read_full_page(page, udf_get_block);
112 static int udf_write_begin(struct file *file, struct address_space *mapping,
113 loff_t pos, unsigned len, unsigned flags,
114 struct page **pagep, void **fsdata)
118 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
120 loff_t isize = mapping->host->i_size;
121 if (pos + len > isize)
122 vmtruncate(mapping->host, isize);
128 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
130 return generic_block_bmap(mapping, block, udf_get_block);
133 const struct address_space_operations udf_aops = {
134 .readpage = udf_readpage,
135 .writepage = udf_writepage,
136 .write_begin = udf_write_begin,
137 .write_end = generic_write_end,
141 void udf_expand_file_adinicb(struct inode *inode, int newsize, int *err)
145 struct udf_inode_info *iinfo = UDF_I(inode);
146 struct writeback_control udf_wbc = {
147 .sync_mode = WB_SYNC_NONE,
151 /* from now on we have normal address_space methods */
152 inode->i_data.a_ops = &udf_aops;
154 if (!iinfo->i_lenAlloc) {
155 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
156 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
158 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
159 mark_inode_dirty(inode);
163 page = grab_cache_page(inode->i_mapping, 0);
164 BUG_ON(!PageLocked(page));
166 if (!PageUptodate(page)) {
168 memset(kaddr + iinfo->i_lenAlloc, 0x00,
169 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
170 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
172 flush_dcache_page(page);
173 SetPageUptodate(page);
176 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
178 iinfo->i_lenAlloc = 0;
179 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
180 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
182 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
184 inode->i_data.a_ops->writepage(page, &udf_wbc);
185 page_cache_release(page);
187 mark_inode_dirty(inode);
190 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
194 struct buffer_head *dbh = NULL;
195 struct kernel_lb_addr eloc;
197 struct extent_position epos;
199 struct udf_fileident_bh sfibh, dfibh;
200 loff_t f_pos = udf_ext0_offset(inode);
201 int size = udf_ext0_offset(inode) + inode->i_size;
202 struct fileIdentDesc cfi, *sfi, *dfi;
203 struct udf_inode_info *iinfo = UDF_I(inode);
205 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
206 alloctype = ICBTAG_FLAG_AD_SHORT;
208 alloctype = ICBTAG_FLAG_AD_LONG;
210 if (!inode->i_size) {
211 iinfo->i_alloc_type = alloctype;
212 mark_inode_dirty(inode);
216 /* alloc block, and copy data to it */
217 *block = udf_new_block(inode->i_sb, inode,
218 iinfo->i_location.partitionReferenceNum,
219 iinfo->i_location.logicalBlockNum, err);
222 newblock = udf_get_pblock(inode->i_sb, *block,
223 iinfo->i_location.partitionReferenceNum,
227 dbh = udf_tgetblk(inode->i_sb, newblock);
231 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
232 set_buffer_uptodate(dbh);
234 mark_buffer_dirty_inode(dbh, inode);
236 sfibh.soffset = sfibh.eoffset =
237 f_pos & (inode->i_sb->s_blocksize - 1);
238 sfibh.sbh = sfibh.ebh = NULL;
239 dfibh.soffset = dfibh.eoffset = 0;
240 dfibh.sbh = dfibh.ebh = dbh;
241 while (f_pos < size) {
242 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
243 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
249 iinfo->i_alloc_type = alloctype;
250 sfi->descTag.tagLocation = cpu_to_le32(*block);
251 dfibh.soffset = dfibh.eoffset;
252 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
253 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
254 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
256 le16_to_cpu(sfi->lengthOfImpUse))) {
257 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
262 mark_buffer_dirty_inode(dbh, inode);
264 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
266 iinfo->i_lenAlloc = 0;
267 eloc.logicalBlockNum = *block;
268 eloc.partitionReferenceNum =
269 iinfo->i_location.partitionReferenceNum;
270 iinfo->i_lenExtents = inode->i_size;
272 epos.block = iinfo->i_location;
273 epos.offset = udf_file_entry_alloc_offset(inode);
274 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
278 mark_inode_dirty(inode);
282 static int udf_get_block(struct inode *inode, sector_t block,
283 struct buffer_head *bh_result, int create)
286 struct buffer_head *bh;
288 struct udf_inode_info *iinfo;
291 phys = udf_block_map(inode, block);
293 map_bh(bh_result, inode->i_sb, phys);
300 iinfo = UDF_I(inode);
302 down_write(&iinfo->i_data_sem);
303 if (block == iinfo->i_next_alloc_block + 1) {
304 iinfo->i_next_alloc_block++;
305 iinfo->i_next_alloc_goal++;
310 bh = inode_getblk(inode, block, &err, &phys, &new);
317 set_buffer_new(bh_result);
318 map_bh(bh_result, inode->i_sb, phys);
321 up_write(&iinfo->i_data_sem);
325 static struct buffer_head *udf_getblk(struct inode *inode, long block,
326 int create, int *err)
328 struct buffer_head *bh;
329 struct buffer_head dummy;
332 dummy.b_blocknr = -1000;
333 *err = udf_get_block(inode, block, &dummy, create);
334 if (!*err && buffer_mapped(&dummy)) {
335 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
336 if (buffer_new(&dummy)) {
338 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
339 set_buffer_uptodate(bh);
341 mark_buffer_dirty_inode(bh, inode);
349 /* Extend the file by 'blocks' blocks, return the number of extents added */
350 int udf_extend_file(struct inode *inode, struct extent_position *last_pos,
351 struct kernel_long_ad *last_ext, sector_t blocks)
354 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
355 struct super_block *sb = inode->i_sb;
356 struct kernel_lb_addr prealloc_loc = {};
357 int prealloc_len = 0;
358 struct udf_inode_info *iinfo;
360 /* The previous extent is fake and we should not extend by anything
361 * - there's nothing to do... */
365 iinfo = UDF_I(inode);
366 /* Round the last extent up to a multiple of block size */
367 if (last_ext->extLength & (sb->s_blocksize - 1)) {
368 last_ext->extLength =
369 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
370 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
371 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
372 iinfo->i_lenExtents =
373 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
374 ~(sb->s_blocksize - 1);
377 /* Last extent are just preallocated blocks? */
378 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
379 EXT_NOT_RECORDED_ALLOCATED) {
380 /* Save the extent so that we can reattach it to the end */
381 prealloc_loc = last_ext->extLocation;
382 prealloc_len = last_ext->extLength;
383 /* Mark the extent as a hole */
384 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
385 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
386 last_ext->extLocation.logicalBlockNum = 0;
387 last_ext->extLocation.partitionReferenceNum = 0;
390 /* Can we merge with the previous extent? */
391 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
392 EXT_NOT_RECORDED_NOT_ALLOCATED) {
393 add = ((1 << 30) - sb->s_blocksize -
394 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
395 sb->s_blocksize_bits;
399 last_ext->extLength += add << sb->s_blocksize_bits;
403 udf_add_aext(inode, last_pos, &last_ext->extLocation,
404 last_ext->extLength, 1);
407 udf_write_aext(inode, last_pos, &last_ext->extLocation,
408 last_ext->extLength, 1);
410 /* Managed to do everything necessary? */
414 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
415 last_ext->extLocation.logicalBlockNum = 0;
416 last_ext->extLocation.partitionReferenceNum = 0;
417 add = (1 << (30-sb->s_blocksize_bits)) - 1;
418 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
419 (add << sb->s_blocksize_bits);
421 /* Create enough extents to cover the whole hole */
422 while (blocks > add) {
424 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
425 last_ext->extLength, 1) == -1)
430 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
431 (blocks << sb->s_blocksize_bits);
432 if (udf_add_aext(inode, last_pos, &last_ext->extLocation,
433 last_ext->extLength, 1) == -1)
439 /* Do we have some preallocated blocks saved? */
441 if (udf_add_aext(inode, last_pos, &prealloc_loc,
442 prealloc_len, 1) == -1)
444 last_ext->extLocation = prealloc_loc;
445 last_ext->extLength = prealloc_len;
449 /* last_pos should point to the last written extent... */
450 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
451 last_pos->offset -= sizeof(struct short_ad);
452 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
453 last_pos->offset -= sizeof(struct long_ad);
460 static struct buffer_head *inode_getblk(struct inode *inode, sector_t block,
461 int *err, sector_t *phys, int *new)
463 static sector_t last_block;
464 struct buffer_head *result = NULL;
465 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
466 struct extent_position prev_epos, cur_epos, next_epos;
467 int count = 0, startnum = 0, endnum = 0;
468 uint32_t elen = 0, tmpelen;
469 struct kernel_lb_addr eloc, tmpeloc;
471 loff_t lbcount = 0, b_off = 0;
472 uint32_t newblocknum, newblock;
475 struct udf_inode_info *iinfo = UDF_I(inode);
476 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
479 prev_epos.offset = udf_file_entry_alloc_offset(inode);
480 prev_epos.block = iinfo->i_location;
482 cur_epos = next_epos = prev_epos;
483 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
485 /* find the extent which contains the block we are looking for.
486 alternate between laarr[0] and laarr[1] for locations of the
487 current extent, and the previous extent */
489 if (prev_epos.bh != cur_epos.bh) {
490 brelse(prev_epos.bh);
492 prev_epos.bh = cur_epos.bh;
494 if (cur_epos.bh != next_epos.bh) {
496 get_bh(next_epos.bh);
497 cur_epos.bh = next_epos.bh;
502 prev_epos.block = cur_epos.block;
503 cur_epos.block = next_epos.block;
505 prev_epos.offset = cur_epos.offset;
506 cur_epos.offset = next_epos.offset;
508 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
514 laarr[c].extLength = (etype << 30) | elen;
515 laarr[c].extLocation = eloc;
517 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
518 pgoal = eloc.logicalBlockNum +
519 ((elen + inode->i_sb->s_blocksize - 1) >>
520 inode->i_sb->s_blocksize_bits);
523 } while (lbcount + elen <= b_off);
526 offset = b_off >> inode->i_sb->s_blocksize_bits;
528 * Move prev_epos and cur_epos into indirect extent if we are at
531 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
532 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
534 /* if the extent is allocated and recorded, return the block
535 if the extent is not a multiple of the blocksize, round up */
537 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
538 if (elen & (inode->i_sb->s_blocksize - 1)) {
539 elen = EXT_RECORDED_ALLOCATED |
540 ((elen + inode->i_sb->s_blocksize - 1) &
541 ~(inode->i_sb->s_blocksize - 1));
542 etype = udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
544 brelse(prev_epos.bh);
546 brelse(next_epos.bh);
547 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
553 /* Are we beyond EOF? */
562 /* Create a fake extent when there's not one */
563 memset(&laarr[0].extLocation, 0x00,
564 sizeof(struct kernel_lb_addr));
565 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
566 /* Will udf_extend_file() create real extent from
568 startnum = (offset > 0);
570 /* Create extents for the hole between EOF and offset */
571 ret = udf_extend_file(inode, &prev_epos, laarr, offset);
573 brelse(prev_epos.bh);
575 brelse(next_epos.bh);
576 /* We don't really know the error here so we just make
584 /* We are not covered by a preallocated extent? */
585 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
586 EXT_NOT_RECORDED_ALLOCATED) {
587 /* Is there any real extent? - otherwise we overwrite
591 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
592 inode->i_sb->s_blocksize;
593 memset(&laarr[c].extLocation, 0x00,
594 sizeof(struct kernel_lb_addr));
601 endnum = startnum = ((count > 2) ? 2 : count);
603 /* if the current extent is in position 0,
604 swap it with the previous */
605 if (!c && count != 1) {
612 /* if the current block is located in an extent,
613 read the next extent */
614 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
616 laarr[c + 1].extLength = (etype << 30) | elen;
617 laarr[c + 1].extLocation = eloc;
625 /* if the current extent is not recorded but allocated, get the
626 * block in the extent corresponding to the requested block */
627 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
628 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
629 else { /* otherwise, allocate a new block */
630 if (iinfo->i_next_alloc_block == block)
631 goal = iinfo->i_next_alloc_goal;
634 if (!(goal = pgoal)) /* XXX: what was intended here? */
635 goal = iinfo->i_location.logicalBlockNum + 1;
638 newblocknum = udf_new_block(inode->i_sb, inode,
639 iinfo->i_location.partitionReferenceNum,
642 brelse(prev_epos.bh);
646 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
649 /* if the extent the requsted block is located in contains multiple
650 * blocks, split the extent into at most three extents. blocks prior
651 * to requested block, requested block, and blocks after requested
653 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
655 #ifdef UDF_PREALLOCATE
656 /* We preallocate blocks only for regular files. It also makes sense
657 * for directories but there's a problem when to drop the
658 * preallocation. We might use some delayed work for that but I feel
659 * it's overengineering for a filesystem like UDF. */
660 if (S_ISREG(inode->i_mode))
661 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
664 /* merge any continuous blocks in laarr */
665 udf_merge_extents(inode, laarr, &endnum);
667 /* write back the new extents, inserting new extents if the new number
668 * of extents is greater than the old number, and deleting extents if
669 * the new number of extents is less than the old number */
670 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
672 brelse(prev_epos.bh);
674 newblock = udf_get_pblock(inode->i_sb, newblocknum,
675 iinfo->i_location.partitionReferenceNum, 0);
681 iinfo->i_next_alloc_block = block;
682 iinfo->i_next_alloc_goal = newblocknum;
683 inode->i_ctime = current_fs_time(inode->i_sb);
686 udf_sync_inode(inode);
688 mark_inode_dirty(inode);
693 static void udf_split_extents(struct inode *inode, int *c, int offset,
695 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
698 unsigned long blocksize = inode->i_sb->s_blocksize;
699 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
701 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
702 (laarr[*c].extLength >> 30) ==
703 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
705 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
706 blocksize - 1) >> blocksize_bits;
707 int8_t etype = (laarr[curr].extLength >> 30);
711 else if (!offset || blen == offset + 1) {
712 laarr[curr + 2] = laarr[curr + 1];
713 laarr[curr + 1] = laarr[curr];
715 laarr[curr + 3] = laarr[curr + 1];
716 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
720 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
721 udf_free_blocks(inode->i_sb, inode,
722 &laarr[curr].extLocation,
724 laarr[curr].extLength =
725 EXT_NOT_RECORDED_NOT_ALLOCATED |
726 (offset << blocksize_bits);
727 laarr[curr].extLocation.logicalBlockNum = 0;
728 laarr[curr].extLocation.
729 partitionReferenceNum = 0;
731 laarr[curr].extLength = (etype << 30) |
732 (offset << blocksize_bits);
738 laarr[curr].extLocation.logicalBlockNum = newblocknum;
739 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
740 laarr[curr].extLocation.partitionReferenceNum =
741 UDF_I(inode)->i_location.partitionReferenceNum;
742 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
746 if (blen != offset + 1) {
747 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
748 laarr[curr].extLocation.logicalBlockNum +=
750 laarr[curr].extLength = (etype << 30) |
751 ((blen - (offset + 1)) << blocksize_bits);
758 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
759 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
762 int start, length = 0, currlength = 0, i;
764 if (*endnum >= (c + 1)) {
770 if ((laarr[c + 1].extLength >> 30) ==
771 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
773 length = currlength =
774 (((laarr[c + 1].extLength &
775 UDF_EXTENT_LENGTH_MASK) +
776 inode->i_sb->s_blocksize - 1) >>
777 inode->i_sb->s_blocksize_bits);
782 for (i = start + 1; i <= *endnum; i++) {
785 length += UDF_DEFAULT_PREALLOC_BLOCKS;
786 } else if ((laarr[i].extLength >> 30) ==
787 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
788 length += (((laarr[i].extLength &
789 UDF_EXTENT_LENGTH_MASK) +
790 inode->i_sb->s_blocksize - 1) >>
791 inode->i_sb->s_blocksize_bits);
797 int next = laarr[start].extLocation.logicalBlockNum +
798 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
799 inode->i_sb->s_blocksize - 1) >>
800 inode->i_sb->s_blocksize_bits);
801 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
802 laarr[start].extLocation.partitionReferenceNum,
803 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
804 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
807 if (start == (c + 1))
808 laarr[start].extLength +=
810 inode->i_sb->s_blocksize_bits);
812 memmove(&laarr[c + 2], &laarr[c + 1],
813 sizeof(struct long_ad) * (*endnum - (c + 1)));
815 laarr[c + 1].extLocation.logicalBlockNum = next;
816 laarr[c + 1].extLocation.partitionReferenceNum =
817 laarr[c].extLocation.
818 partitionReferenceNum;
819 laarr[c + 1].extLength =
820 EXT_NOT_RECORDED_ALLOCATED |
822 inode->i_sb->s_blocksize_bits);
826 for (i = start + 1; numalloc && i < *endnum; i++) {
827 int elen = ((laarr[i].extLength &
828 UDF_EXTENT_LENGTH_MASK) +
829 inode->i_sb->s_blocksize - 1) >>
830 inode->i_sb->s_blocksize_bits;
832 if (elen > numalloc) {
833 laarr[i].extLength -=
835 inode->i_sb->s_blocksize_bits);
839 if (*endnum > (i + 1))
842 sizeof(struct long_ad) *
843 (*endnum - (i + 1)));
848 UDF_I(inode)->i_lenExtents +=
849 numalloc << inode->i_sb->s_blocksize_bits;
854 static void udf_merge_extents(struct inode *inode,
855 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
859 unsigned long blocksize = inode->i_sb->s_blocksize;
860 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
862 for (i = 0; i < (*endnum - 1); i++) {
863 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
864 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
866 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
867 (((li->extLength >> 30) ==
868 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
869 ((lip1->extLocation.logicalBlockNum -
870 li->extLocation.logicalBlockNum) ==
871 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
872 blocksize - 1) >> blocksize_bits)))) {
874 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
875 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
876 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
877 lip1->extLength = (lip1->extLength -
879 UDF_EXTENT_LENGTH_MASK) +
880 UDF_EXTENT_LENGTH_MASK) &
882 li->extLength = (li->extLength &
883 UDF_EXTENT_FLAG_MASK) +
884 (UDF_EXTENT_LENGTH_MASK + 1) -
886 lip1->extLocation.logicalBlockNum =
887 li->extLocation.logicalBlockNum +
889 UDF_EXTENT_LENGTH_MASK) >>
892 li->extLength = lip1->extLength +
894 UDF_EXTENT_LENGTH_MASK) +
895 blocksize - 1) & ~(blocksize - 1));
896 if (*endnum > (i + 2))
897 memmove(&laarr[i + 1], &laarr[i + 2],
898 sizeof(struct long_ad) *
899 (*endnum - (i + 2)));
903 } else if (((li->extLength >> 30) ==
904 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
905 ((lip1->extLength >> 30) ==
906 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
907 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
909 UDF_EXTENT_LENGTH_MASK) +
910 blocksize - 1) >> blocksize_bits);
911 li->extLocation.logicalBlockNum = 0;
912 li->extLocation.partitionReferenceNum = 0;
914 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
915 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
916 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
917 lip1->extLength = (lip1->extLength -
919 UDF_EXTENT_LENGTH_MASK) +
920 UDF_EXTENT_LENGTH_MASK) &
922 li->extLength = (li->extLength &
923 UDF_EXTENT_FLAG_MASK) +
924 (UDF_EXTENT_LENGTH_MASK + 1) -
927 li->extLength = lip1->extLength +
929 UDF_EXTENT_LENGTH_MASK) +
930 blocksize - 1) & ~(blocksize - 1));
931 if (*endnum > (i + 2))
932 memmove(&laarr[i + 1], &laarr[i + 2],
933 sizeof(struct long_ad) *
934 (*endnum - (i + 2)));
938 } else if ((li->extLength >> 30) ==
939 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
940 udf_free_blocks(inode->i_sb, inode,
943 UDF_EXTENT_LENGTH_MASK) +
944 blocksize - 1) >> blocksize_bits);
945 li->extLocation.logicalBlockNum = 0;
946 li->extLocation.partitionReferenceNum = 0;
947 li->extLength = (li->extLength &
948 UDF_EXTENT_LENGTH_MASK) |
949 EXT_NOT_RECORDED_NOT_ALLOCATED;
954 static void udf_update_extents(struct inode *inode,
955 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
956 int startnum, int endnum,
957 struct extent_position *epos)
960 struct kernel_lb_addr tmploc;
963 if (startnum > endnum) {
964 for (i = 0; i < (startnum - endnum); i++)
965 udf_delete_aext(inode, *epos, laarr[i].extLocation,
967 } else if (startnum < endnum) {
968 for (i = 0; i < (endnum - startnum); i++) {
969 udf_insert_aext(inode, *epos, laarr[i].extLocation,
971 udf_next_aext(inode, epos, &laarr[i].extLocation,
972 &laarr[i].extLength, 1);
977 for (i = start; i < endnum; i++) {
978 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
979 udf_write_aext(inode, epos, &laarr[i].extLocation,
980 laarr[i].extLength, 1);
984 struct buffer_head *udf_bread(struct inode *inode, int block,
985 int create, int *err)
987 struct buffer_head *bh = NULL;
989 bh = udf_getblk(inode, block, create, err);
993 if (buffer_uptodate(bh))
996 ll_rw_block(READ, 1, &bh);
999 if (buffer_uptodate(bh))
1007 void udf_truncate(struct inode *inode)
1011 struct udf_inode_info *iinfo;
1013 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1014 S_ISLNK(inode->i_mode)))
1016 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1019 iinfo = UDF_I(inode);
1020 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1021 down_write(&iinfo->i_data_sem);
1022 if (inode->i_sb->s_blocksize <
1023 (udf_file_entry_alloc_offset(inode) +
1025 udf_expand_file_adinicb(inode, inode->i_size, &err);
1026 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1027 inode->i_size = iinfo->i_lenAlloc;
1028 up_write(&iinfo->i_data_sem);
1031 udf_truncate_extents(inode);
1033 offset = inode->i_size & (inode->i_sb->s_blocksize - 1);
1034 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + offset,
1035 0x00, inode->i_sb->s_blocksize -
1036 offset - udf_file_entry_alloc_offset(inode));
1037 iinfo->i_lenAlloc = inode->i_size;
1039 up_write(&iinfo->i_data_sem);
1041 block_truncate_page(inode->i_mapping, inode->i_size,
1043 down_write(&iinfo->i_data_sem);
1044 udf_truncate_extents(inode);
1045 up_write(&iinfo->i_data_sem);
1048 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1050 udf_sync_inode(inode);
1052 mark_inode_dirty(inode);
1055 static void __udf_read_inode(struct inode *inode)
1057 struct buffer_head *bh = NULL;
1058 struct fileEntry *fe;
1060 struct udf_inode_info *iinfo = UDF_I(inode);
1063 * Set defaults, but the inode is still incomplete!
1064 * Note: get_new_inode() sets the following on a new inode:
1067 * i_flags = sb->s_flags
1069 * clean_inode(): zero fills and sets
1074 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
1076 printk(KERN_ERR "udf: udf_read_inode(ino %ld) failed !bh\n",
1078 make_bad_inode(inode);
1082 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1083 ident != TAG_IDENT_USE) {
1084 printk(KERN_ERR "udf: udf_read_inode(ino %ld) "
1085 "failed ident=%d\n", inode->i_ino, ident);
1087 make_bad_inode(inode);
1091 fe = (struct fileEntry *)bh->b_data;
1093 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1094 struct buffer_head *ibh;
1096 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
1098 if (ident == TAG_IDENT_IE && ibh) {
1099 struct buffer_head *nbh = NULL;
1100 struct kernel_lb_addr loc;
1101 struct indirectEntry *ie;
1103 ie = (struct indirectEntry *)ibh->b_data;
1104 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1106 if (ie->indirectICB.extLength &&
1107 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
1109 if (ident == TAG_IDENT_FE ||
1110 ident == TAG_IDENT_EFE) {
1111 memcpy(&iinfo->i_location,
1113 sizeof(struct kernel_lb_addr));
1117 __udf_read_inode(inode);
1124 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1125 printk(KERN_ERR "udf: unsupported strategy type: %d\n",
1126 le16_to_cpu(fe->icbTag.strategyType));
1128 make_bad_inode(inode);
1131 udf_fill_inode(inode, bh);
1136 static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1138 struct fileEntry *fe;
1139 struct extendedFileEntry *efe;
1141 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1142 struct udf_inode_info *iinfo = UDF_I(inode);
1144 fe = (struct fileEntry *)bh->b_data;
1145 efe = (struct extendedFileEntry *)bh->b_data;
1147 if (fe->icbTag.strategyType == cpu_to_le16(4))
1148 iinfo->i_strat4096 = 0;
1149 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1150 iinfo->i_strat4096 = 1;
1152 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1153 ICBTAG_FLAG_AD_MASK;
1154 iinfo->i_unique = 0;
1155 iinfo->i_lenEAttr = 0;
1156 iinfo->i_lenExtents = 0;
1157 iinfo->i_lenAlloc = 0;
1158 iinfo->i_next_alloc_block = 0;
1159 iinfo->i_next_alloc_goal = 0;
1160 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1163 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1164 sizeof(struct extendedFileEntry))) {
1165 make_bad_inode(inode);
1168 memcpy(iinfo->i_ext.i_data,
1169 bh->b_data + sizeof(struct extendedFileEntry),
1170 inode->i_sb->s_blocksize -
1171 sizeof(struct extendedFileEntry));
1172 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1175 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1176 sizeof(struct fileEntry))) {
1177 make_bad_inode(inode);
1180 memcpy(iinfo->i_ext.i_data,
1181 bh->b_data + sizeof(struct fileEntry),
1182 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1183 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1186 iinfo->i_lenAlloc = le32_to_cpu(
1187 ((struct unallocSpaceEntry *)bh->b_data)->
1189 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1190 sizeof(struct unallocSpaceEntry))) {
1191 make_bad_inode(inode);
1194 memcpy(iinfo->i_ext.i_data,
1195 bh->b_data + sizeof(struct unallocSpaceEntry),
1196 inode->i_sb->s_blocksize -
1197 sizeof(struct unallocSpaceEntry));
1201 read_lock(&sbi->s_cred_lock);
1202 inode->i_uid = le32_to_cpu(fe->uid);
1203 if (inode->i_uid == -1 ||
1204 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1205 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1206 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1208 inode->i_gid = le32_to_cpu(fe->gid);
1209 if (inode->i_gid == -1 ||
1210 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1211 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1212 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1214 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1215 sbi->s_fmode != UDF_INVALID_MODE)
1216 inode->i_mode = sbi->s_fmode;
1217 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1218 sbi->s_dmode != UDF_INVALID_MODE)
1219 inode->i_mode = sbi->s_dmode;
1221 inode->i_mode = udf_convert_permissions(fe);
1222 inode->i_mode &= ~sbi->s_umask;
1223 read_unlock(&sbi->s_cred_lock);
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 if (iinfo->i_efe == 0) {
1233 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1234 (inode->i_sb->s_blocksize_bits - 9);
1236 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1237 inode->i_atime = sbi->s_record_time;
1239 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1240 fe->modificationTime))
1241 inode->i_mtime = sbi->s_record_time;
1243 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1244 inode->i_ctime = sbi->s_record_time;
1246 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1247 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1248 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1249 offset = sizeof(struct fileEntry) + iinfo->i_lenEAttr;
1251 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1252 (inode->i_sb->s_blocksize_bits - 9);
1254 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1255 inode->i_atime = sbi->s_record_time;
1257 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1258 efe->modificationTime))
1259 inode->i_mtime = sbi->s_record_time;
1261 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1262 iinfo->i_crtime = sbi->s_record_time;
1264 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1265 inode->i_ctime = sbi->s_record_time;
1267 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1268 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1269 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1270 offset = sizeof(struct extendedFileEntry) +
1274 switch (fe->icbTag.fileType) {
1275 case ICBTAG_FILE_TYPE_DIRECTORY:
1276 inode->i_op = &udf_dir_inode_operations;
1277 inode->i_fop = &udf_dir_operations;
1278 inode->i_mode |= S_IFDIR;
1281 case ICBTAG_FILE_TYPE_REALTIME:
1282 case ICBTAG_FILE_TYPE_REGULAR:
1283 case ICBTAG_FILE_TYPE_UNDEF:
1284 case ICBTAG_FILE_TYPE_VAT20:
1285 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1286 inode->i_data.a_ops = &udf_adinicb_aops;
1288 inode->i_data.a_ops = &udf_aops;
1289 inode->i_op = &udf_file_inode_operations;
1290 inode->i_fop = &udf_file_operations;
1291 inode->i_mode |= S_IFREG;
1293 case ICBTAG_FILE_TYPE_BLOCK:
1294 inode->i_mode |= S_IFBLK;
1296 case ICBTAG_FILE_TYPE_CHAR:
1297 inode->i_mode |= S_IFCHR;
1299 case ICBTAG_FILE_TYPE_FIFO:
1300 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1302 case ICBTAG_FILE_TYPE_SOCKET:
1303 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1305 case ICBTAG_FILE_TYPE_SYMLINK:
1306 inode->i_data.a_ops = &udf_symlink_aops;
1307 inode->i_op = &udf_symlink_inode_operations;
1308 inode->i_mode = S_IFLNK | S_IRWXUGO;
1310 case ICBTAG_FILE_TYPE_MAIN:
1311 udf_debug("METADATA FILE-----\n");
1313 case ICBTAG_FILE_TYPE_MIRROR:
1314 udf_debug("METADATA MIRROR FILE-----\n");
1316 case ICBTAG_FILE_TYPE_BITMAP:
1317 udf_debug("METADATA BITMAP FILE-----\n");
1320 printk(KERN_ERR "udf: udf_fill_inode(ino %ld) failed unknown "
1321 "file type=%d\n", inode->i_ino,
1322 fe->icbTag.fileType);
1323 make_bad_inode(inode);
1326 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1327 struct deviceSpec *dsea =
1328 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1330 init_special_inode(inode, inode->i_mode,
1331 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1332 le32_to_cpu(dsea->minorDeviceIdent)));
1333 /* Developer ID ??? */
1335 make_bad_inode(inode);
1339 static int udf_alloc_i_data(struct inode *inode, size_t size)
1341 struct udf_inode_info *iinfo = UDF_I(inode);
1342 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1344 if (!iinfo->i_ext.i_data) {
1345 printk(KERN_ERR "udf:udf_alloc_i_data (ino %ld) "
1346 "no free memory\n", inode->i_ino);
1353 static mode_t udf_convert_permissions(struct fileEntry *fe)
1356 uint32_t permissions;
1359 permissions = le32_to_cpu(fe->permissions);
1360 flags = le16_to_cpu(fe->icbTag.flags);
1362 mode = ((permissions) & S_IRWXO) |
1363 ((permissions >> 2) & S_IRWXG) |
1364 ((permissions >> 4) & S_IRWXU) |
1365 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1366 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1367 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1372 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1374 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1377 static int udf_sync_inode(struct inode *inode)
1379 return udf_update_inode(inode, 1);
1382 static int udf_update_inode(struct inode *inode, int do_sync)
1384 struct buffer_head *bh = NULL;
1385 struct fileEntry *fe;
1386 struct extendedFileEntry *efe;
1391 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1392 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1393 struct udf_inode_info *iinfo = UDF_I(inode);
1395 bh = udf_tgetblk(inode->i_sb,
1396 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1398 udf_debug("getblk failure\n");
1403 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1404 fe = (struct fileEntry *)bh->b_data;
1405 efe = (struct extendedFileEntry *)bh->b_data;
1408 struct unallocSpaceEntry *use =
1409 (struct unallocSpaceEntry *)bh->b_data;
1411 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1412 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1413 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1414 sizeof(struct unallocSpaceEntry));
1415 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1416 use->descTag.tagLocation =
1417 cpu_to_le32(iinfo->i_location.logicalBlockNum);
1418 crclen = sizeof(struct unallocSpaceEntry) +
1419 iinfo->i_lenAlloc - sizeof(struct tag);
1420 use->descTag.descCRCLength = cpu_to_le16(crclen);
1421 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
1424 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1429 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1430 fe->uid = cpu_to_le32(-1);
1432 fe->uid = cpu_to_le32(inode->i_uid);
1434 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1435 fe->gid = cpu_to_le32(-1);
1437 fe->gid = cpu_to_le32(inode->i_gid);
1439 udfperms = ((inode->i_mode & S_IRWXO)) |
1440 ((inode->i_mode & S_IRWXG) << 2) |
1441 ((inode->i_mode & S_IRWXU) << 4);
1443 udfperms |= (le32_to_cpu(fe->permissions) &
1444 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1445 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1446 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1447 fe->permissions = cpu_to_le32(udfperms);
1449 if (S_ISDIR(inode->i_mode))
1450 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1452 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1454 fe->informationLength = cpu_to_le64(inode->i_size);
1456 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1458 struct deviceSpec *dsea =
1459 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1461 dsea = (struct deviceSpec *)
1462 udf_add_extendedattr(inode,
1463 sizeof(struct deviceSpec) +
1464 sizeof(struct regid), 12, 0x3);
1465 dsea->attrType = cpu_to_le32(12);
1466 dsea->attrSubtype = 1;
1467 dsea->attrLength = cpu_to_le32(
1468 sizeof(struct deviceSpec) +
1469 sizeof(struct regid));
1470 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1472 eid = (struct regid *)dsea->impUse;
1473 memset(eid, 0, sizeof(struct regid));
1474 strcpy(eid->ident, UDF_ID_DEVELOPER);
1475 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1476 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1477 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1478 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1481 if (iinfo->i_efe == 0) {
1482 memcpy(bh->b_data + sizeof(struct fileEntry),
1483 iinfo->i_ext.i_data,
1484 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1485 fe->logicalBlocksRecorded = cpu_to_le64(
1486 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1487 (blocksize_bits - 9));
1489 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1490 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1491 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1492 memset(&(fe->impIdent), 0, sizeof(struct regid));
1493 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1494 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1495 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1496 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1497 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1498 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1499 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1500 crclen = sizeof(struct fileEntry);
1502 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1503 iinfo->i_ext.i_data,
1504 inode->i_sb->s_blocksize -
1505 sizeof(struct extendedFileEntry));
1506 efe->objectSize = cpu_to_le64(inode->i_size);
1507 efe->logicalBlocksRecorded = cpu_to_le64(
1508 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1509 (blocksize_bits - 9));
1511 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1512 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1513 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1514 iinfo->i_crtime = inode->i_atime;
1516 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1517 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1518 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1519 iinfo->i_crtime = inode->i_mtime;
1521 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1522 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1523 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1524 iinfo->i_crtime = inode->i_ctime;
1526 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1527 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1528 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1529 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1531 memset(&(efe->impIdent), 0, sizeof(struct regid));
1532 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1533 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1534 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1535 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1536 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1537 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1538 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1539 crclen = sizeof(struct extendedFileEntry);
1541 if (iinfo->i_strat4096) {
1542 fe->icbTag.strategyType = cpu_to_le16(4096);
1543 fe->icbTag.strategyParameter = cpu_to_le16(1);
1544 fe->icbTag.numEntries = cpu_to_le16(2);
1546 fe->icbTag.strategyType = cpu_to_le16(4);
1547 fe->icbTag.numEntries = cpu_to_le16(1);
1550 if (S_ISDIR(inode->i_mode))
1551 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1552 else if (S_ISREG(inode->i_mode))
1553 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1554 else if (S_ISLNK(inode->i_mode))
1555 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1556 else if (S_ISBLK(inode->i_mode))
1557 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1558 else if (S_ISCHR(inode->i_mode))
1559 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1560 else if (S_ISFIFO(inode->i_mode))
1561 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1562 else if (S_ISSOCK(inode->i_mode))
1563 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1565 icbflags = iinfo->i_alloc_type |
1566 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1567 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1568 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1569 (le16_to_cpu(fe->icbTag.flags) &
1570 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1571 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1573 fe->icbTag.flags = cpu_to_le16(icbflags);
1574 if (sbi->s_udfrev >= 0x0200)
1575 fe->descTag.descVersion = cpu_to_le16(3);
1577 fe->descTag.descVersion = cpu_to_le16(2);
1578 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1579 fe->descTag.tagLocation = cpu_to_le32(
1580 iinfo->i_location.logicalBlockNum);
1581 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1582 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1583 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1585 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1588 set_buffer_uptodate(bh);
1591 /* write the data blocks */
1592 mark_buffer_dirty(bh);
1594 sync_dirty_buffer(bh);
1595 if (buffer_write_io_error(bh)) {
1596 printk(KERN_WARNING "IO error syncing udf inode "
1597 "[%s:%08lx]\n", inode->i_sb->s_id,
1607 struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
1609 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1610 struct inode *inode = iget_locked(sb, block);
1615 if (inode->i_state & I_NEW) {
1616 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1617 __udf_read_inode(inode);
1618 unlock_new_inode(inode);
1621 if (is_bad_inode(inode))
1624 if (ino->logicalBlockNum >= UDF_SB(sb)->
1625 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
1626 udf_debug("block=%d, partition=%d out of range\n",
1627 ino->logicalBlockNum, ino->partitionReferenceNum);
1628 make_bad_inode(inode);
1639 int8_t udf_add_aext(struct inode *inode, struct extent_position *epos,
1640 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1643 struct short_ad *sad = NULL;
1644 struct long_ad *lad = NULL;
1645 struct allocExtDesc *aed;
1648 struct udf_inode_info *iinfo = UDF_I(inode);
1651 ptr = iinfo->i_ext.i_data + epos->offset -
1652 udf_file_entry_alloc_offset(inode) +
1655 ptr = epos->bh->b_data + epos->offset;
1657 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1658 adsize = sizeof(struct short_ad);
1659 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1660 adsize = sizeof(struct long_ad);
1664 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1665 unsigned char *sptr, *dptr;
1666 struct buffer_head *nbh;
1668 struct kernel_lb_addr obloc = epos->block;
1670 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1671 obloc.partitionReferenceNum,
1672 obloc.logicalBlockNum, &err);
1673 if (!epos->block.logicalBlockNum)
1675 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1681 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1682 set_buffer_uptodate(nbh);
1684 mark_buffer_dirty_inode(nbh, inode);
1686 aed = (struct allocExtDesc *)(nbh->b_data);
1687 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1688 aed->previousAllocExtLocation =
1689 cpu_to_le32(obloc.logicalBlockNum);
1690 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1691 loffset = epos->offset;
1692 aed->lengthAllocDescs = cpu_to_le32(adsize);
1693 sptr = ptr - adsize;
1694 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1695 memcpy(dptr, sptr, adsize);
1696 epos->offset = sizeof(struct allocExtDesc) + adsize;
1698 loffset = epos->offset + adsize;
1699 aed->lengthAllocDescs = cpu_to_le32(0);
1701 epos->offset = sizeof(struct allocExtDesc);
1704 aed = (struct allocExtDesc *)epos->bh->b_data;
1705 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1707 iinfo->i_lenAlloc += adsize;
1708 mark_inode_dirty(inode);
1711 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1712 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1713 epos->block.logicalBlockNum, sizeof(struct tag));
1715 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1716 epos->block.logicalBlockNum, sizeof(struct tag));
1717 switch (iinfo->i_alloc_type) {
1718 case ICBTAG_FLAG_AD_SHORT:
1719 sad = (struct short_ad *)sptr;
1720 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1721 inode->i_sb->s_blocksize);
1723 cpu_to_le32(epos->block.logicalBlockNum);
1725 case ICBTAG_FLAG_AD_LONG:
1726 lad = (struct long_ad *)sptr;
1727 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1728 inode->i_sb->s_blocksize);
1729 lad->extLocation = cpu_to_lelb(epos->block);
1730 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1734 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1735 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1736 udf_update_tag(epos->bh->b_data, loffset);
1738 udf_update_tag(epos->bh->b_data,
1739 sizeof(struct allocExtDesc));
1740 mark_buffer_dirty_inode(epos->bh, inode);
1743 mark_inode_dirty(inode);
1748 etype = udf_write_aext(inode, epos, eloc, elen, inc);
1751 iinfo->i_lenAlloc += adsize;
1752 mark_inode_dirty(inode);
1754 aed = (struct allocExtDesc *)epos->bh->b_data;
1755 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1756 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1757 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1758 udf_update_tag(epos->bh->b_data,
1759 epos->offset + (inc ? 0 : adsize));
1761 udf_update_tag(epos->bh->b_data,
1762 sizeof(struct allocExtDesc));
1763 mark_buffer_dirty_inode(epos->bh, inode);
1769 int8_t udf_write_aext(struct inode *inode, struct extent_position *epos,
1770 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1774 struct short_ad *sad;
1775 struct long_ad *lad;
1776 struct udf_inode_info *iinfo = UDF_I(inode);
1779 ptr = iinfo->i_ext.i_data + epos->offset -
1780 udf_file_entry_alloc_offset(inode) +
1783 ptr = epos->bh->b_data + epos->offset;
1785 switch (iinfo->i_alloc_type) {
1786 case ICBTAG_FLAG_AD_SHORT:
1787 sad = (struct short_ad *)ptr;
1788 sad->extLength = cpu_to_le32(elen);
1789 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
1790 adsize = sizeof(struct short_ad);
1792 case ICBTAG_FLAG_AD_LONG:
1793 lad = (struct long_ad *)ptr;
1794 lad->extLength = cpu_to_le32(elen);
1795 lad->extLocation = cpu_to_lelb(*eloc);
1796 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1797 adsize = sizeof(struct long_ad);
1804 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1805 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
1806 struct allocExtDesc *aed =
1807 (struct allocExtDesc *)epos->bh->b_data;
1808 udf_update_tag(epos->bh->b_data,
1809 le32_to_cpu(aed->lengthAllocDescs) +
1810 sizeof(struct allocExtDesc));
1812 mark_buffer_dirty_inode(epos->bh, inode);
1814 mark_inode_dirty(inode);
1818 epos->offset += adsize;
1820 return (elen >> 30);
1823 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1824 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1828 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
1829 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
1831 epos->block = *eloc;
1832 epos->offset = sizeof(struct allocExtDesc);
1834 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
1835 epos->bh = udf_tread(inode->i_sb, block);
1837 udf_debug("reading block %d failed!\n", block);
1845 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1846 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1851 struct short_ad *sad;
1852 struct long_ad *lad;
1853 struct udf_inode_info *iinfo = UDF_I(inode);
1857 epos->offset = udf_file_entry_alloc_offset(inode);
1858 ptr = iinfo->i_ext.i_data + epos->offset -
1859 udf_file_entry_alloc_offset(inode) +
1861 alen = udf_file_entry_alloc_offset(inode) +
1865 epos->offset = sizeof(struct allocExtDesc);
1866 ptr = epos->bh->b_data + epos->offset;
1867 alen = sizeof(struct allocExtDesc) +
1868 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
1872 switch (iinfo->i_alloc_type) {
1873 case ICBTAG_FLAG_AD_SHORT:
1874 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
1877 etype = le32_to_cpu(sad->extLength) >> 30;
1878 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
1879 eloc->partitionReferenceNum =
1880 iinfo->i_location.partitionReferenceNum;
1881 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
1883 case ICBTAG_FLAG_AD_LONG:
1884 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
1887 etype = le32_to_cpu(lad->extLength) >> 30;
1888 *eloc = lelb_to_cpu(lad->extLocation);
1889 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
1892 udf_debug("alloc_type = %d unsupported\n",
1893 iinfo->i_alloc_type);
1900 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
1901 struct kernel_lb_addr neloc, uint32_t nelen)
1903 struct kernel_lb_addr oeloc;
1910 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
1911 udf_write_aext(inode, &epos, &neloc, nelen, 1);
1913 nelen = (etype << 30) | oelen;
1915 udf_add_aext(inode, &epos, &neloc, nelen, 1);
1918 return (nelen >> 30);
1921 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
1922 struct kernel_lb_addr eloc, uint32_t elen)
1924 struct extent_position oepos;
1927 struct allocExtDesc *aed;
1928 struct udf_inode_info *iinfo;
1935 iinfo = UDF_I(inode);
1936 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1937 adsize = sizeof(struct short_ad);
1938 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1939 adsize = sizeof(struct long_ad);
1944 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
1947 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
1948 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
1949 if (oepos.bh != epos.bh) {
1950 oepos.block = epos.block;
1954 oepos.offset = epos.offset - adsize;
1957 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
1960 if (epos.bh != oepos.bh) {
1961 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
1962 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1963 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1965 iinfo->i_lenAlloc -= (adsize * 2);
1966 mark_inode_dirty(inode);
1968 aed = (struct allocExtDesc *)oepos.bh->b_data;
1969 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
1970 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1971 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1972 udf_update_tag(oepos.bh->b_data,
1973 oepos.offset - (2 * adsize));
1975 udf_update_tag(oepos.bh->b_data,
1976 sizeof(struct allocExtDesc));
1977 mark_buffer_dirty_inode(oepos.bh, inode);
1980 udf_write_aext(inode, &oepos, &eloc, elen, 1);
1982 iinfo->i_lenAlloc -= adsize;
1983 mark_inode_dirty(inode);
1985 aed = (struct allocExtDesc *)oepos.bh->b_data;
1986 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
1987 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1988 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1989 udf_update_tag(oepos.bh->b_data,
1990 epos.offset - adsize);
1992 udf_update_tag(oepos.bh->b_data,
1993 sizeof(struct allocExtDesc));
1994 mark_buffer_dirty_inode(oepos.bh, inode);
2001 return (elen >> 30);
2004 int8_t inode_bmap(struct inode *inode, sector_t block,
2005 struct extent_position *pos, struct kernel_lb_addr *eloc,
2006 uint32_t *elen, sector_t *offset)
2008 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2009 loff_t lbcount = 0, bcount =
2010 (loff_t) block << blocksize_bits;
2012 struct udf_inode_info *iinfo;
2014 iinfo = UDF_I(inode);
2016 pos->block = iinfo->i_location;
2021 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2023 *offset = (bcount - lbcount) >> blocksize_bits;
2024 iinfo->i_lenExtents = lbcount;
2028 } while (lbcount <= bcount);
2030 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2035 long udf_block_map(struct inode *inode, sector_t block)
2037 struct kernel_lb_addr eloc;
2040 struct extent_position epos = {};
2043 down_read(&UDF_I(inode)->i_data_sem);
2045 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2046 (EXT_RECORDED_ALLOCATED >> 30))
2047 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2051 up_read(&UDF_I(inode)->i_data_sem);
2054 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2055 return udf_fixed_to_variable(ret);