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>
40 #include <linux/mpage.h>
45 MODULE_AUTHOR("Ben Fennema");
46 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47 MODULE_LICENSE("GPL");
49 #define EXTENT_MERGE_SIZE 5
51 static umode_t udf_convert_permissions(struct fileEntry *);
52 static int udf_update_inode(struct inode *, int);
53 static void udf_fill_inode(struct inode *, struct buffer_head *);
54 static int udf_sync_inode(struct inode *inode);
55 static int udf_alloc_i_data(struct inode *inode, size_t size);
56 static sector_t inode_getblk(struct inode *, sector_t, int *, 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 if (!inode->i_nlink && !is_bad_inode(inode)) {
78 udf_setsize(inode, 0);
79 udf_update_inode(inode, IS_SYNC(inode));
81 truncate_inode_pages(&inode->i_data, 0);
82 invalidate_inode_buffers(inode);
84 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
85 inode->i_size != iinfo->i_lenExtents) {
86 udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
87 inode->i_ino, inode->i_mode,
88 (unsigned long long)inode->i_size,
89 (unsigned long long)iinfo->i_lenExtents);
91 kfree(iinfo->i_ext.i_data);
92 iinfo->i_ext.i_data = NULL;
94 udf_free_inode(inode);
98 static void udf_write_failed(struct address_space *mapping, loff_t to)
100 struct inode *inode = mapping->host;
101 struct udf_inode_info *iinfo = UDF_I(inode);
102 loff_t isize = inode->i_size;
105 truncate_pagecache(inode, to, isize);
106 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
107 down_write(&iinfo->i_data_sem);
108 udf_truncate_extents(inode);
109 up_write(&iinfo->i_data_sem);
114 static int udf_writepage(struct page *page, struct writeback_control *wbc)
116 return block_write_full_page(page, udf_get_block, wbc);
119 static int udf_writepages(struct address_space *mapping,
120 struct writeback_control *wbc)
122 return mpage_writepages(mapping, wbc, udf_get_block);
125 static int udf_readpage(struct file *file, struct page *page)
127 return mpage_readpage(page, udf_get_block);
130 static int udf_readpages(struct file *file, struct address_space *mapping,
131 struct list_head *pages, unsigned nr_pages)
133 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
136 static int udf_write_begin(struct file *file, struct address_space *mapping,
137 loff_t pos, unsigned len, unsigned flags,
138 struct page **pagep, void **fsdata)
142 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
144 udf_write_failed(mapping, pos + len);
148 static ssize_t udf_direct_IO(int rw, struct kiocb *iocb,
149 const struct iovec *iov,
150 loff_t offset, unsigned long nr_segs)
152 struct file *file = iocb->ki_filp;
153 struct address_space *mapping = file->f_mapping;
154 struct inode *inode = mapping->host;
157 ret = blockdev_direct_IO(rw, iocb, inode, iov, offset, nr_segs,
159 if (unlikely(ret < 0 && (rw & WRITE)))
160 udf_write_failed(mapping, offset + iov_length(iov, nr_segs));
164 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
166 return generic_block_bmap(mapping, block, udf_get_block);
169 const struct address_space_operations udf_aops = {
170 .readpage = udf_readpage,
171 .readpages = udf_readpages,
172 .writepage = udf_writepage,
173 .writepages = udf_writepages,
174 .write_begin = udf_write_begin,
175 .write_end = generic_write_end,
176 .direct_IO = udf_direct_IO,
181 * Expand file stored in ICB to a normal one-block-file
183 * This function requires i_data_sem for writing and releases it.
184 * This function requires i_mutex held
186 int udf_expand_file_adinicb(struct inode *inode)
190 struct udf_inode_info *iinfo = UDF_I(inode);
192 struct writeback_control udf_wbc = {
193 .sync_mode = WB_SYNC_NONE,
197 if (!iinfo->i_lenAlloc) {
198 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
199 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
201 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
202 /* from now on we have normal address_space methods */
203 inode->i_data.a_ops = &udf_aops;
204 up_write(&iinfo->i_data_sem);
205 mark_inode_dirty(inode);
209 * Release i_data_sem so that we can lock a page - page lock ranks
210 * above i_data_sem. i_mutex still protects us against file changes.
212 up_write(&iinfo->i_data_sem);
214 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
218 if (!PageUptodate(page)) {
220 memset(kaddr + iinfo->i_lenAlloc, 0x00,
221 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
222 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
224 flush_dcache_page(page);
225 SetPageUptodate(page);
228 down_write(&iinfo->i_data_sem);
229 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
231 iinfo->i_lenAlloc = 0;
232 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
233 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
235 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
236 /* from now on we have normal address_space methods */
237 inode->i_data.a_ops = &udf_aops;
238 up_write(&iinfo->i_data_sem);
239 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
241 /* Restore everything back so that we don't lose data... */
244 down_write(&iinfo->i_data_sem);
245 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
249 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
250 inode->i_data.a_ops = &udf_adinicb_aops;
251 up_write(&iinfo->i_data_sem);
253 page_cache_release(page);
254 mark_inode_dirty(inode);
259 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
263 struct buffer_head *dbh = NULL;
264 struct kernel_lb_addr eloc;
266 struct extent_position epos;
268 struct udf_fileident_bh sfibh, dfibh;
269 loff_t f_pos = udf_ext0_offset(inode);
270 int size = udf_ext0_offset(inode) + inode->i_size;
271 struct fileIdentDesc cfi, *sfi, *dfi;
272 struct udf_inode_info *iinfo = UDF_I(inode);
274 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
275 alloctype = ICBTAG_FLAG_AD_SHORT;
277 alloctype = ICBTAG_FLAG_AD_LONG;
279 if (!inode->i_size) {
280 iinfo->i_alloc_type = alloctype;
281 mark_inode_dirty(inode);
285 /* alloc block, and copy data to it */
286 *block = udf_new_block(inode->i_sb, inode,
287 iinfo->i_location.partitionReferenceNum,
288 iinfo->i_location.logicalBlockNum, err);
291 newblock = udf_get_pblock(inode->i_sb, *block,
292 iinfo->i_location.partitionReferenceNum,
296 dbh = udf_tgetblk(inode->i_sb, newblock);
300 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
301 set_buffer_uptodate(dbh);
303 mark_buffer_dirty_inode(dbh, inode);
305 sfibh.soffset = sfibh.eoffset =
306 f_pos & (inode->i_sb->s_blocksize - 1);
307 sfibh.sbh = sfibh.ebh = NULL;
308 dfibh.soffset = dfibh.eoffset = 0;
309 dfibh.sbh = dfibh.ebh = dbh;
310 while (f_pos < size) {
311 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
312 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
318 iinfo->i_alloc_type = alloctype;
319 sfi->descTag.tagLocation = cpu_to_le32(*block);
320 dfibh.soffset = dfibh.eoffset;
321 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
322 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
323 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
325 le16_to_cpu(sfi->lengthOfImpUse))) {
326 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
331 mark_buffer_dirty_inode(dbh, inode);
333 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
335 iinfo->i_lenAlloc = 0;
336 eloc.logicalBlockNum = *block;
337 eloc.partitionReferenceNum =
338 iinfo->i_location.partitionReferenceNum;
339 iinfo->i_lenExtents = inode->i_size;
341 epos.block = iinfo->i_location;
342 epos.offset = udf_file_entry_alloc_offset(inode);
343 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
347 mark_inode_dirty(inode);
351 static int udf_get_block(struct inode *inode, sector_t block,
352 struct buffer_head *bh_result, int create)
356 struct udf_inode_info *iinfo;
359 phys = udf_block_map(inode, block);
361 map_bh(bh_result, inode->i_sb, phys);
367 iinfo = UDF_I(inode);
369 down_write(&iinfo->i_data_sem);
370 if (block == iinfo->i_next_alloc_block + 1) {
371 iinfo->i_next_alloc_block++;
372 iinfo->i_next_alloc_goal++;
376 phys = inode_getblk(inode, block, &err, &new);
381 set_buffer_new(bh_result);
382 map_bh(bh_result, inode->i_sb, phys);
385 up_write(&iinfo->i_data_sem);
389 static struct buffer_head *udf_getblk(struct inode *inode, long block,
390 int create, int *err)
392 struct buffer_head *bh;
393 struct buffer_head dummy;
396 dummy.b_blocknr = -1000;
397 *err = udf_get_block(inode, block, &dummy, create);
398 if (!*err && buffer_mapped(&dummy)) {
399 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
400 if (buffer_new(&dummy)) {
402 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
403 set_buffer_uptodate(bh);
405 mark_buffer_dirty_inode(bh, inode);
413 /* Extend the file by 'blocks' blocks, return the number of extents added */
414 static int udf_do_extend_file(struct inode *inode,
415 struct extent_position *last_pos,
416 struct kernel_long_ad *last_ext,
420 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
421 struct super_block *sb = inode->i_sb;
422 struct kernel_lb_addr prealloc_loc = {};
423 int prealloc_len = 0;
424 struct udf_inode_info *iinfo;
427 /* The previous extent is fake and we should not extend by anything
428 * - there's nothing to do... */
432 iinfo = UDF_I(inode);
433 /* Round the last extent up to a multiple of block size */
434 if (last_ext->extLength & (sb->s_blocksize - 1)) {
435 last_ext->extLength =
436 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
437 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
438 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
439 iinfo->i_lenExtents =
440 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
441 ~(sb->s_blocksize - 1);
444 /* Last extent are just preallocated blocks? */
445 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
446 EXT_NOT_RECORDED_ALLOCATED) {
447 /* Save the extent so that we can reattach it to the end */
448 prealloc_loc = last_ext->extLocation;
449 prealloc_len = last_ext->extLength;
450 /* Mark the extent as a hole */
451 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
452 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
453 last_ext->extLocation.logicalBlockNum = 0;
454 last_ext->extLocation.partitionReferenceNum = 0;
457 /* Can we merge with the previous extent? */
458 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
459 EXT_NOT_RECORDED_NOT_ALLOCATED) {
460 add = ((1 << 30) - sb->s_blocksize -
461 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
462 sb->s_blocksize_bits;
466 last_ext->extLength += add << sb->s_blocksize_bits;
470 udf_add_aext(inode, last_pos, &last_ext->extLocation,
471 last_ext->extLength, 1);
474 udf_write_aext(inode, last_pos, &last_ext->extLocation,
475 last_ext->extLength, 1);
477 /* Managed to do everything necessary? */
481 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
482 last_ext->extLocation.logicalBlockNum = 0;
483 last_ext->extLocation.partitionReferenceNum = 0;
484 add = (1 << (30-sb->s_blocksize_bits)) - 1;
485 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
486 (add << sb->s_blocksize_bits);
488 /* Create enough extents to cover the whole hole */
489 while (blocks > add) {
491 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
492 last_ext->extLength, 1);
498 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
499 (blocks << sb->s_blocksize_bits);
500 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
501 last_ext->extLength, 1);
508 /* Do we have some preallocated blocks saved? */
510 err = udf_add_aext(inode, last_pos, &prealloc_loc,
514 last_ext->extLocation = prealloc_loc;
515 last_ext->extLength = prealloc_len;
519 /* last_pos should point to the last written extent... */
520 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
521 last_pos->offset -= sizeof(struct short_ad);
522 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
523 last_pos->offset -= sizeof(struct long_ad);
530 static int udf_extend_file(struct inode *inode, loff_t newsize)
533 struct extent_position epos;
534 struct kernel_lb_addr eloc;
537 struct super_block *sb = inode->i_sb;
538 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
540 struct udf_inode_info *iinfo = UDF_I(inode);
541 struct kernel_long_ad extent;
544 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
545 adsize = sizeof(struct short_ad);
546 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
547 adsize = sizeof(struct long_ad);
551 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
553 /* File has extent covering the new size (could happen when extending
554 * inside a block)? */
557 if (newsize & (sb->s_blocksize - 1))
559 /* Extended file just to the boundary of the last file block? */
563 /* Truncate is extending the file by 'offset' blocks */
564 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
565 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
566 /* File has no extents at all or has empty last
567 * indirect extent! Create a fake extent... */
568 extent.extLocation.logicalBlockNum = 0;
569 extent.extLocation.partitionReferenceNum = 0;
570 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
572 epos.offset -= adsize;
573 etype = udf_next_aext(inode, &epos, &extent.extLocation,
574 &extent.extLength, 0);
575 extent.extLength |= etype << 30;
577 err = udf_do_extend_file(inode, &epos, &extent, offset);
581 iinfo->i_lenExtents = newsize;
587 static sector_t inode_getblk(struct inode *inode, sector_t block,
590 static sector_t last_block;
591 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
592 struct extent_position prev_epos, cur_epos, next_epos;
593 int count = 0, startnum = 0, endnum = 0;
594 uint32_t elen = 0, tmpelen;
595 struct kernel_lb_addr eloc, tmpeloc;
597 loff_t lbcount = 0, b_off = 0;
598 uint32_t newblocknum, newblock;
601 struct udf_inode_info *iinfo = UDF_I(inode);
602 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
607 prev_epos.offset = udf_file_entry_alloc_offset(inode);
608 prev_epos.block = iinfo->i_location;
610 cur_epos = next_epos = prev_epos;
611 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
613 /* find the extent which contains the block we are looking for.
614 alternate between laarr[0] and laarr[1] for locations of the
615 current extent, and the previous extent */
617 if (prev_epos.bh != cur_epos.bh) {
618 brelse(prev_epos.bh);
620 prev_epos.bh = cur_epos.bh;
622 if (cur_epos.bh != next_epos.bh) {
624 get_bh(next_epos.bh);
625 cur_epos.bh = next_epos.bh;
630 prev_epos.block = cur_epos.block;
631 cur_epos.block = next_epos.block;
633 prev_epos.offset = cur_epos.offset;
634 cur_epos.offset = next_epos.offset;
636 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
642 laarr[c].extLength = (etype << 30) | elen;
643 laarr[c].extLocation = eloc;
645 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
646 pgoal = eloc.logicalBlockNum +
647 ((elen + inode->i_sb->s_blocksize - 1) >>
648 inode->i_sb->s_blocksize_bits);
651 } while (lbcount + elen <= b_off);
654 offset = b_off >> inode->i_sb->s_blocksize_bits;
656 * Move prev_epos and cur_epos into indirect extent if we are at
659 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
660 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
662 /* if the extent is allocated and recorded, return the block
663 if the extent is not a multiple of the blocksize, round up */
665 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
666 if (elen & (inode->i_sb->s_blocksize - 1)) {
667 elen = EXT_RECORDED_ALLOCATED |
668 ((elen + inode->i_sb->s_blocksize - 1) &
669 ~(inode->i_sb->s_blocksize - 1));
670 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
672 brelse(prev_epos.bh);
674 brelse(next_epos.bh);
675 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
680 /* Are we beyond EOF? */
689 /* Create a fake extent when there's not one */
690 memset(&laarr[0].extLocation, 0x00,
691 sizeof(struct kernel_lb_addr));
692 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
693 /* Will udf_do_extend_file() create real extent from
695 startnum = (offset > 0);
697 /* Create extents for the hole between EOF and offset */
698 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
700 brelse(prev_epos.bh);
702 brelse(next_epos.bh);
709 /* We are not covered by a preallocated extent? */
710 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
711 EXT_NOT_RECORDED_ALLOCATED) {
712 /* Is there any real extent? - otherwise we overwrite
716 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
717 inode->i_sb->s_blocksize;
718 memset(&laarr[c].extLocation, 0x00,
719 sizeof(struct kernel_lb_addr));
726 endnum = startnum = ((count > 2) ? 2 : count);
728 /* if the current extent is in position 0,
729 swap it with the previous */
730 if (!c && count != 1) {
737 /* if the current block is located in an extent,
738 read the next extent */
739 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
741 laarr[c + 1].extLength = (etype << 30) | elen;
742 laarr[c + 1].extLocation = eloc;
750 /* if the current extent is not recorded but allocated, get the
751 * block in the extent corresponding to the requested block */
752 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
753 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
754 else { /* otherwise, allocate a new block */
755 if (iinfo->i_next_alloc_block == block)
756 goal = iinfo->i_next_alloc_goal;
759 if (!(goal = pgoal)) /* XXX: what was intended here? */
760 goal = iinfo->i_location.logicalBlockNum + 1;
763 newblocknum = udf_new_block(inode->i_sb, inode,
764 iinfo->i_location.partitionReferenceNum,
767 brelse(prev_epos.bh);
771 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
774 /* if the extent the requsted block is located in contains multiple
775 * blocks, split the extent into at most three extents. blocks prior
776 * to requested block, requested block, and blocks after requested
778 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
780 #ifdef UDF_PREALLOCATE
781 /* We preallocate blocks only for regular files. It also makes sense
782 * for directories but there's a problem when to drop the
783 * preallocation. We might use some delayed work for that but I feel
784 * it's overengineering for a filesystem like UDF. */
785 if (S_ISREG(inode->i_mode))
786 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
789 /* merge any continuous blocks in laarr */
790 udf_merge_extents(inode, laarr, &endnum);
792 /* write back the new extents, inserting new extents if the new number
793 * of extents is greater than the old number, and deleting extents if
794 * the new number of extents is less than the old number */
795 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
797 brelse(prev_epos.bh);
799 newblock = udf_get_pblock(inode->i_sb, newblocknum,
800 iinfo->i_location.partitionReferenceNum, 0);
806 iinfo->i_next_alloc_block = block;
807 iinfo->i_next_alloc_goal = newblocknum;
808 inode->i_ctime = current_fs_time(inode->i_sb);
811 udf_sync_inode(inode);
813 mark_inode_dirty(inode);
818 static void udf_split_extents(struct inode *inode, int *c, int offset,
820 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
823 unsigned long blocksize = inode->i_sb->s_blocksize;
824 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
826 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
827 (laarr[*c].extLength >> 30) ==
828 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
830 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
831 blocksize - 1) >> blocksize_bits;
832 int8_t etype = (laarr[curr].extLength >> 30);
836 else if (!offset || blen == offset + 1) {
837 laarr[curr + 2] = laarr[curr + 1];
838 laarr[curr + 1] = laarr[curr];
840 laarr[curr + 3] = laarr[curr + 1];
841 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
845 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
846 udf_free_blocks(inode->i_sb, inode,
847 &laarr[curr].extLocation,
849 laarr[curr].extLength =
850 EXT_NOT_RECORDED_NOT_ALLOCATED |
851 (offset << blocksize_bits);
852 laarr[curr].extLocation.logicalBlockNum = 0;
853 laarr[curr].extLocation.
854 partitionReferenceNum = 0;
856 laarr[curr].extLength = (etype << 30) |
857 (offset << blocksize_bits);
863 laarr[curr].extLocation.logicalBlockNum = newblocknum;
864 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
865 laarr[curr].extLocation.partitionReferenceNum =
866 UDF_I(inode)->i_location.partitionReferenceNum;
867 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
871 if (blen != offset + 1) {
872 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
873 laarr[curr].extLocation.logicalBlockNum +=
875 laarr[curr].extLength = (etype << 30) |
876 ((blen - (offset + 1)) << blocksize_bits);
883 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
884 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
887 int start, length = 0, currlength = 0, i;
889 if (*endnum >= (c + 1)) {
895 if ((laarr[c + 1].extLength >> 30) ==
896 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
898 length = currlength =
899 (((laarr[c + 1].extLength &
900 UDF_EXTENT_LENGTH_MASK) +
901 inode->i_sb->s_blocksize - 1) >>
902 inode->i_sb->s_blocksize_bits);
907 for (i = start + 1; i <= *endnum; i++) {
910 length += UDF_DEFAULT_PREALLOC_BLOCKS;
911 } else if ((laarr[i].extLength >> 30) ==
912 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
913 length += (((laarr[i].extLength &
914 UDF_EXTENT_LENGTH_MASK) +
915 inode->i_sb->s_blocksize - 1) >>
916 inode->i_sb->s_blocksize_bits);
922 int next = laarr[start].extLocation.logicalBlockNum +
923 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
924 inode->i_sb->s_blocksize - 1) >>
925 inode->i_sb->s_blocksize_bits);
926 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
927 laarr[start].extLocation.partitionReferenceNum,
928 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
929 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
932 if (start == (c + 1))
933 laarr[start].extLength +=
935 inode->i_sb->s_blocksize_bits);
937 memmove(&laarr[c + 2], &laarr[c + 1],
938 sizeof(struct long_ad) * (*endnum - (c + 1)));
940 laarr[c + 1].extLocation.logicalBlockNum = next;
941 laarr[c + 1].extLocation.partitionReferenceNum =
942 laarr[c].extLocation.
943 partitionReferenceNum;
944 laarr[c + 1].extLength =
945 EXT_NOT_RECORDED_ALLOCATED |
947 inode->i_sb->s_blocksize_bits);
951 for (i = start + 1; numalloc && i < *endnum; i++) {
952 int elen = ((laarr[i].extLength &
953 UDF_EXTENT_LENGTH_MASK) +
954 inode->i_sb->s_blocksize - 1) >>
955 inode->i_sb->s_blocksize_bits;
957 if (elen > numalloc) {
958 laarr[i].extLength -=
960 inode->i_sb->s_blocksize_bits);
964 if (*endnum > (i + 1))
967 sizeof(struct long_ad) *
968 (*endnum - (i + 1)));
973 UDF_I(inode)->i_lenExtents +=
974 numalloc << inode->i_sb->s_blocksize_bits;
979 static void udf_merge_extents(struct inode *inode,
980 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
984 unsigned long blocksize = inode->i_sb->s_blocksize;
985 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
987 for (i = 0; i < (*endnum - 1); i++) {
988 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
989 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
991 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
992 (((li->extLength >> 30) ==
993 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
994 ((lip1->extLocation.logicalBlockNum -
995 li->extLocation.logicalBlockNum) ==
996 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
997 blocksize - 1) >> blocksize_bits)))) {
999 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1000 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1001 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1002 lip1->extLength = (lip1->extLength -
1004 UDF_EXTENT_LENGTH_MASK) +
1005 UDF_EXTENT_LENGTH_MASK) &
1007 li->extLength = (li->extLength &
1008 UDF_EXTENT_FLAG_MASK) +
1009 (UDF_EXTENT_LENGTH_MASK + 1) -
1011 lip1->extLocation.logicalBlockNum =
1012 li->extLocation.logicalBlockNum +
1014 UDF_EXTENT_LENGTH_MASK) >>
1017 li->extLength = lip1->extLength +
1019 UDF_EXTENT_LENGTH_MASK) +
1020 blocksize - 1) & ~(blocksize - 1));
1021 if (*endnum > (i + 2))
1022 memmove(&laarr[i + 1], &laarr[i + 2],
1023 sizeof(struct long_ad) *
1024 (*endnum - (i + 2)));
1028 } else if (((li->extLength >> 30) ==
1029 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1030 ((lip1->extLength >> 30) ==
1031 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1032 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1034 UDF_EXTENT_LENGTH_MASK) +
1035 blocksize - 1) >> blocksize_bits);
1036 li->extLocation.logicalBlockNum = 0;
1037 li->extLocation.partitionReferenceNum = 0;
1039 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1040 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1041 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1042 lip1->extLength = (lip1->extLength -
1044 UDF_EXTENT_LENGTH_MASK) +
1045 UDF_EXTENT_LENGTH_MASK) &
1047 li->extLength = (li->extLength &
1048 UDF_EXTENT_FLAG_MASK) +
1049 (UDF_EXTENT_LENGTH_MASK + 1) -
1052 li->extLength = lip1->extLength +
1054 UDF_EXTENT_LENGTH_MASK) +
1055 blocksize - 1) & ~(blocksize - 1));
1056 if (*endnum > (i + 2))
1057 memmove(&laarr[i + 1], &laarr[i + 2],
1058 sizeof(struct long_ad) *
1059 (*endnum - (i + 2)));
1063 } else if ((li->extLength >> 30) ==
1064 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1065 udf_free_blocks(inode->i_sb, inode,
1066 &li->extLocation, 0,
1068 UDF_EXTENT_LENGTH_MASK) +
1069 blocksize - 1) >> blocksize_bits);
1070 li->extLocation.logicalBlockNum = 0;
1071 li->extLocation.partitionReferenceNum = 0;
1072 li->extLength = (li->extLength &
1073 UDF_EXTENT_LENGTH_MASK) |
1074 EXT_NOT_RECORDED_NOT_ALLOCATED;
1079 static void udf_update_extents(struct inode *inode,
1080 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1081 int startnum, int endnum,
1082 struct extent_position *epos)
1085 struct kernel_lb_addr tmploc;
1088 if (startnum > endnum) {
1089 for (i = 0; i < (startnum - endnum); i++)
1090 udf_delete_aext(inode, *epos, laarr[i].extLocation,
1091 laarr[i].extLength);
1092 } else if (startnum < endnum) {
1093 for (i = 0; i < (endnum - startnum); i++) {
1094 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1095 laarr[i].extLength);
1096 udf_next_aext(inode, epos, &laarr[i].extLocation,
1097 &laarr[i].extLength, 1);
1102 for (i = start; i < endnum; i++) {
1103 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1104 udf_write_aext(inode, epos, &laarr[i].extLocation,
1105 laarr[i].extLength, 1);
1109 struct buffer_head *udf_bread(struct inode *inode, int block,
1110 int create, int *err)
1112 struct buffer_head *bh = NULL;
1114 bh = udf_getblk(inode, block, create, err);
1118 if (buffer_uptodate(bh))
1121 ll_rw_block(READ, 1, &bh);
1124 if (buffer_uptodate(bh))
1132 int udf_setsize(struct inode *inode, loff_t newsize)
1135 struct udf_inode_info *iinfo;
1136 int bsize = 1 << inode->i_blkbits;
1138 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1139 S_ISLNK(inode->i_mode)))
1141 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1144 iinfo = UDF_I(inode);
1145 if (newsize > inode->i_size) {
1146 down_write(&iinfo->i_data_sem);
1147 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1149 (udf_file_entry_alloc_offset(inode) + newsize)) {
1150 err = udf_expand_file_adinicb(inode);
1153 down_write(&iinfo->i_data_sem);
1155 iinfo->i_lenAlloc = newsize;
1159 err = udf_extend_file(inode, newsize);
1161 up_write(&iinfo->i_data_sem);
1165 truncate_setsize(inode, newsize);
1166 up_write(&iinfo->i_data_sem);
1168 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1169 down_write(&iinfo->i_data_sem);
1170 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1171 0x00, bsize - newsize -
1172 udf_file_entry_alloc_offset(inode));
1173 iinfo->i_lenAlloc = newsize;
1174 truncate_setsize(inode, newsize);
1175 up_write(&iinfo->i_data_sem);
1178 err = block_truncate_page(inode->i_mapping, newsize,
1182 down_write(&iinfo->i_data_sem);
1183 truncate_setsize(inode, newsize);
1184 udf_truncate_extents(inode);
1185 up_write(&iinfo->i_data_sem);
1188 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1190 udf_sync_inode(inode);
1192 mark_inode_dirty(inode);
1196 static void __udf_read_inode(struct inode *inode)
1198 struct buffer_head *bh = NULL;
1199 struct fileEntry *fe;
1201 struct udf_inode_info *iinfo = UDF_I(inode);
1204 * Set defaults, but the inode is still incomplete!
1205 * Note: get_new_inode() sets the following on a new inode:
1208 * i_flags = sb->s_flags
1210 * clean_inode(): zero fills and sets
1215 bh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 0, &ident);
1217 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1218 make_bad_inode(inode);
1222 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1223 ident != TAG_IDENT_USE) {
1224 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1225 inode->i_ino, ident);
1227 make_bad_inode(inode);
1231 fe = (struct fileEntry *)bh->b_data;
1233 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1234 struct buffer_head *ibh;
1236 ibh = udf_read_ptagged(inode->i_sb, &iinfo->i_location, 1,
1238 if (ident == TAG_IDENT_IE && ibh) {
1239 struct buffer_head *nbh = NULL;
1240 struct kernel_lb_addr loc;
1241 struct indirectEntry *ie;
1243 ie = (struct indirectEntry *)ibh->b_data;
1244 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1246 if (ie->indirectICB.extLength &&
1247 (nbh = udf_read_ptagged(inode->i_sb, &loc, 0,
1249 if (ident == TAG_IDENT_FE ||
1250 ident == TAG_IDENT_EFE) {
1251 memcpy(&iinfo->i_location,
1253 sizeof(struct kernel_lb_addr));
1257 __udf_read_inode(inode);
1264 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1265 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1266 le16_to_cpu(fe->icbTag.strategyType));
1268 make_bad_inode(inode);
1271 udf_fill_inode(inode, bh);
1276 static void udf_fill_inode(struct inode *inode, struct buffer_head *bh)
1278 struct fileEntry *fe;
1279 struct extendedFileEntry *efe;
1280 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1281 struct udf_inode_info *iinfo = UDF_I(inode);
1282 unsigned int link_count;
1284 fe = (struct fileEntry *)bh->b_data;
1285 efe = (struct extendedFileEntry *)bh->b_data;
1287 if (fe->icbTag.strategyType == cpu_to_le16(4))
1288 iinfo->i_strat4096 = 0;
1289 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1290 iinfo->i_strat4096 = 1;
1292 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1293 ICBTAG_FLAG_AD_MASK;
1294 iinfo->i_unique = 0;
1295 iinfo->i_lenEAttr = 0;
1296 iinfo->i_lenExtents = 0;
1297 iinfo->i_lenAlloc = 0;
1298 iinfo->i_next_alloc_block = 0;
1299 iinfo->i_next_alloc_goal = 0;
1300 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1303 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1304 sizeof(struct extendedFileEntry))) {
1305 make_bad_inode(inode);
1308 memcpy(iinfo->i_ext.i_data,
1309 bh->b_data + sizeof(struct extendedFileEntry),
1310 inode->i_sb->s_blocksize -
1311 sizeof(struct extendedFileEntry));
1312 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1315 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1316 sizeof(struct fileEntry))) {
1317 make_bad_inode(inode);
1320 memcpy(iinfo->i_ext.i_data,
1321 bh->b_data + sizeof(struct fileEntry),
1322 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1323 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1326 iinfo->i_lenAlloc = le32_to_cpu(
1327 ((struct unallocSpaceEntry *)bh->b_data)->
1329 if (udf_alloc_i_data(inode, inode->i_sb->s_blocksize -
1330 sizeof(struct unallocSpaceEntry))) {
1331 make_bad_inode(inode);
1334 memcpy(iinfo->i_ext.i_data,
1335 bh->b_data + sizeof(struct unallocSpaceEntry),
1336 inode->i_sb->s_blocksize -
1337 sizeof(struct unallocSpaceEntry));
1341 read_lock(&sbi->s_cred_lock);
1342 i_uid_write(inode, le32_to_cpu(fe->uid));
1343 if (!uid_valid(inode->i_uid) ||
1344 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1345 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1346 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1348 i_gid_write(inode, le32_to_cpu(fe->gid));
1349 if (!gid_valid(inode->i_gid) ||
1350 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1351 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1352 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1354 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1355 sbi->s_fmode != UDF_INVALID_MODE)
1356 inode->i_mode = sbi->s_fmode;
1357 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1358 sbi->s_dmode != UDF_INVALID_MODE)
1359 inode->i_mode = sbi->s_dmode;
1361 inode->i_mode = udf_convert_permissions(fe);
1362 inode->i_mode &= ~sbi->s_umask;
1363 read_unlock(&sbi->s_cred_lock);
1365 link_count = le16_to_cpu(fe->fileLinkCount);
1368 set_nlink(inode, link_count);
1370 inode->i_size = le64_to_cpu(fe->informationLength);
1371 iinfo->i_lenExtents = inode->i_size;
1373 if (iinfo->i_efe == 0) {
1374 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1375 (inode->i_sb->s_blocksize_bits - 9);
1377 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1378 inode->i_atime = sbi->s_record_time;
1380 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1381 fe->modificationTime))
1382 inode->i_mtime = sbi->s_record_time;
1384 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1385 inode->i_ctime = sbi->s_record_time;
1387 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1388 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1389 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1390 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1392 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1393 (inode->i_sb->s_blocksize_bits - 9);
1395 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1396 inode->i_atime = sbi->s_record_time;
1398 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1399 efe->modificationTime))
1400 inode->i_mtime = sbi->s_record_time;
1402 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1403 iinfo->i_crtime = sbi->s_record_time;
1405 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1406 inode->i_ctime = sbi->s_record_time;
1408 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1409 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1410 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1411 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1414 switch (fe->icbTag.fileType) {
1415 case ICBTAG_FILE_TYPE_DIRECTORY:
1416 inode->i_op = &udf_dir_inode_operations;
1417 inode->i_fop = &udf_dir_operations;
1418 inode->i_mode |= S_IFDIR;
1421 case ICBTAG_FILE_TYPE_REALTIME:
1422 case ICBTAG_FILE_TYPE_REGULAR:
1423 case ICBTAG_FILE_TYPE_UNDEF:
1424 case ICBTAG_FILE_TYPE_VAT20:
1425 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1426 inode->i_data.a_ops = &udf_adinicb_aops;
1428 inode->i_data.a_ops = &udf_aops;
1429 inode->i_op = &udf_file_inode_operations;
1430 inode->i_fop = &udf_file_operations;
1431 inode->i_mode |= S_IFREG;
1433 case ICBTAG_FILE_TYPE_BLOCK:
1434 inode->i_mode |= S_IFBLK;
1436 case ICBTAG_FILE_TYPE_CHAR:
1437 inode->i_mode |= S_IFCHR;
1439 case ICBTAG_FILE_TYPE_FIFO:
1440 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1442 case ICBTAG_FILE_TYPE_SOCKET:
1443 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1445 case ICBTAG_FILE_TYPE_SYMLINK:
1446 inode->i_data.a_ops = &udf_symlink_aops;
1447 inode->i_op = &udf_symlink_inode_operations;
1448 inode->i_mode = S_IFLNK | S_IRWXUGO;
1450 case ICBTAG_FILE_TYPE_MAIN:
1451 udf_debug("METADATA FILE-----\n");
1453 case ICBTAG_FILE_TYPE_MIRROR:
1454 udf_debug("METADATA MIRROR FILE-----\n");
1456 case ICBTAG_FILE_TYPE_BITMAP:
1457 udf_debug("METADATA BITMAP FILE-----\n");
1460 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1461 inode->i_ino, fe->icbTag.fileType);
1462 make_bad_inode(inode);
1465 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1466 struct deviceSpec *dsea =
1467 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1469 init_special_inode(inode, inode->i_mode,
1470 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1471 le32_to_cpu(dsea->minorDeviceIdent)));
1472 /* Developer ID ??? */
1474 make_bad_inode(inode);
1478 static int udf_alloc_i_data(struct inode *inode, size_t size)
1480 struct udf_inode_info *iinfo = UDF_I(inode);
1481 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1483 if (!iinfo->i_ext.i_data) {
1484 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1492 static umode_t udf_convert_permissions(struct fileEntry *fe)
1495 uint32_t permissions;
1498 permissions = le32_to_cpu(fe->permissions);
1499 flags = le16_to_cpu(fe->icbTag.flags);
1501 mode = ((permissions) & S_IRWXO) |
1502 ((permissions >> 2) & S_IRWXG) |
1503 ((permissions >> 4) & S_IRWXU) |
1504 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1505 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1506 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1511 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1513 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1516 static int udf_sync_inode(struct inode *inode)
1518 return udf_update_inode(inode, 1);
1521 static int udf_update_inode(struct inode *inode, int do_sync)
1523 struct buffer_head *bh = NULL;
1524 struct fileEntry *fe;
1525 struct extendedFileEntry *efe;
1526 uint64_t lb_recorded;
1531 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1532 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1533 struct udf_inode_info *iinfo = UDF_I(inode);
1535 bh = udf_tgetblk(inode->i_sb,
1536 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1538 udf_debug("getblk failure\n");
1543 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1544 fe = (struct fileEntry *)bh->b_data;
1545 efe = (struct extendedFileEntry *)bh->b_data;
1548 struct unallocSpaceEntry *use =
1549 (struct unallocSpaceEntry *)bh->b_data;
1551 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1552 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1553 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1554 sizeof(struct unallocSpaceEntry));
1555 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1556 use->descTag.tagLocation =
1557 cpu_to_le32(iinfo->i_location.logicalBlockNum);
1558 crclen = sizeof(struct unallocSpaceEntry) +
1559 iinfo->i_lenAlloc - sizeof(struct tag);
1560 use->descTag.descCRCLength = cpu_to_le16(crclen);
1561 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
1564 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1569 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1570 fe->uid = cpu_to_le32(-1);
1572 fe->uid = cpu_to_le32(i_uid_read(inode));
1574 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1575 fe->gid = cpu_to_le32(-1);
1577 fe->gid = cpu_to_le32(i_gid_read(inode));
1579 udfperms = ((inode->i_mode & S_IRWXO)) |
1580 ((inode->i_mode & S_IRWXG) << 2) |
1581 ((inode->i_mode & S_IRWXU) << 4);
1583 udfperms |= (le32_to_cpu(fe->permissions) &
1584 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1585 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1586 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1587 fe->permissions = cpu_to_le32(udfperms);
1589 if (S_ISDIR(inode->i_mode))
1590 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1592 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1594 fe->informationLength = cpu_to_le64(inode->i_size);
1596 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1598 struct deviceSpec *dsea =
1599 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1601 dsea = (struct deviceSpec *)
1602 udf_add_extendedattr(inode,
1603 sizeof(struct deviceSpec) +
1604 sizeof(struct regid), 12, 0x3);
1605 dsea->attrType = cpu_to_le32(12);
1606 dsea->attrSubtype = 1;
1607 dsea->attrLength = cpu_to_le32(
1608 sizeof(struct deviceSpec) +
1609 sizeof(struct regid));
1610 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1612 eid = (struct regid *)dsea->impUse;
1613 memset(eid, 0, sizeof(struct regid));
1614 strcpy(eid->ident, UDF_ID_DEVELOPER);
1615 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1616 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1617 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1618 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1621 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1622 lb_recorded = 0; /* No extents => no blocks! */
1625 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1626 (blocksize_bits - 9);
1628 if (iinfo->i_efe == 0) {
1629 memcpy(bh->b_data + sizeof(struct fileEntry),
1630 iinfo->i_ext.i_data,
1631 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1632 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1634 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1635 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1636 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1637 memset(&(fe->impIdent), 0, sizeof(struct regid));
1638 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1639 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1640 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1641 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1642 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1643 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1644 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1645 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1646 crclen = sizeof(struct fileEntry);
1648 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1649 iinfo->i_ext.i_data,
1650 inode->i_sb->s_blocksize -
1651 sizeof(struct extendedFileEntry));
1652 efe->objectSize = cpu_to_le64(inode->i_size);
1653 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1655 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1656 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1657 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1658 iinfo->i_crtime = inode->i_atime;
1660 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1661 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1662 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1663 iinfo->i_crtime = inode->i_mtime;
1665 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1666 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1667 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1668 iinfo->i_crtime = inode->i_ctime;
1670 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1671 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1672 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1673 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1675 memset(&(efe->impIdent), 0, sizeof(struct regid));
1676 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1677 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1678 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1679 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1680 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1681 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1682 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1683 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1684 crclen = sizeof(struct extendedFileEntry);
1686 if (iinfo->i_strat4096) {
1687 fe->icbTag.strategyType = cpu_to_le16(4096);
1688 fe->icbTag.strategyParameter = cpu_to_le16(1);
1689 fe->icbTag.numEntries = cpu_to_le16(2);
1691 fe->icbTag.strategyType = cpu_to_le16(4);
1692 fe->icbTag.numEntries = cpu_to_le16(1);
1695 if (S_ISDIR(inode->i_mode))
1696 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1697 else if (S_ISREG(inode->i_mode))
1698 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1699 else if (S_ISLNK(inode->i_mode))
1700 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1701 else if (S_ISBLK(inode->i_mode))
1702 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1703 else if (S_ISCHR(inode->i_mode))
1704 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1705 else if (S_ISFIFO(inode->i_mode))
1706 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1707 else if (S_ISSOCK(inode->i_mode))
1708 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1710 icbflags = iinfo->i_alloc_type |
1711 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1712 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1713 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1714 (le16_to_cpu(fe->icbTag.flags) &
1715 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1716 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1718 fe->icbTag.flags = cpu_to_le16(icbflags);
1719 if (sbi->s_udfrev >= 0x0200)
1720 fe->descTag.descVersion = cpu_to_le16(3);
1722 fe->descTag.descVersion = cpu_to_le16(2);
1723 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1724 fe->descTag.tagLocation = cpu_to_le32(
1725 iinfo->i_location.logicalBlockNum);
1726 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1727 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1728 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1730 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1733 set_buffer_uptodate(bh);
1736 /* write the data blocks */
1737 mark_buffer_dirty(bh);
1739 sync_dirty_buffer(bh);
1740 if (buffer_write_io_error(bh)) {
1741 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1751 struct inode *udf_iget(struct super_block *sb, struct kernel_lb_addr *ino)
1753 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1754 struct inode *inode = iget_locked(sb, block);
1759 if (inode->i_state & I_NEW) {
1760 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1761 __udf_read_inode(inode);
1762 unlock_new_inode(inode);
1765 if (is_bad_inode(inode))
1768 if (ino->logicalBlockNum >= UDF_SB(sb)->
1769 s_partmaps[ino->partitionReferenceNum].s_partition_len) {
1770 udf_debug("block=%d, partition=%d out of range\n",
1771 ino->logicalBlockNum, ino->partitionReferenceNum);
1772 make_bad_inode(inode);
1783 int udf_add_aext(struct inode *inode, struct extent_position *epos,
1784 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1787 struct short_ad *sad = NULL;
1788 struct long_ad *lad = NULL;
1789 struct allocExtDesc *aed;
1791 struct udf_inode_info *iinfo = UDF_I(inode);
1794 ptr = iinfo->i_ext.i_data + epos->offset -
1795 udf_file_entry_alloc_offset(inode) +
1798 ptr = epos->bh->b_data + epos->offset;
1800 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1801 adsize = sizeof(struct short_ad);
1802 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1803 adsize = sizeof(struct long_ad);
1807 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1808 unsigned char *sptr, *dptr;
1809 struct buffer_head *nbh;
1811 struct kernel_lb_addr obloc = epos->block;
1813 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1814 obloc.partitionReferenceNum,
1815 obloc.logicalBlockNum, &err);
1816 if (!epos->block.logicalBlockNum)
1818 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1824 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1825 set_buffer_uptodate(nbh);
1827 mark_buffer_dirty_inode(nbh, inode);
1829 aed = (struct allocExtDesc *)(nbh->b_data);
1830 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1831 aed->previousAllocExtLocation =
1832 cpu_to_le32(obloc.logicalBlockNum);
1833 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1834 loffset = epos->offset;
1835 aed->lengthAllocDescs = cpu_to_le32(adsize);
1836 sptr = ptr - adsize;
1837 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1838 memcpy(dptr, sptr, adsize);
1839 epos->offset = sizeof(struct allocExtDesc) + adsize;
1841 loffset = epos->offset + adsize;
1842 aed->lengthAllocDescs = cpu_to_le32(0);
1844 epos->offset = sizeof(struct allocExtDesc);
1847 aed = (struct allocExtDesc *)epos->bh->b_data;
1848 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1850 iinfo->i_lenAlloc += adsize;
1851 mark_inode_dirty(inode);
1854 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1855 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1856 epos->block.logicalBlockNum, sizeof(struct tag));
1858 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1859 epos->block.logicalBlockNum, sizeof(struct tag));
1860 switch (iinfo->i_alloc_type) {
1861 case ICBTAG_FLAG_AD_SHORT:
1862 sad = (struct short_ad *)sptr;
1863 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1864 inode->i_sb->s_blocksize);
1866 cpu_to_le32(epos->block.logicalBlockNum);
1868 case ICBTAG_FLAG_AD_LONG:
1869 lad = (struct long_ad *)sptr;
1870 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1871 inode->i_sb->s_blocksize);
1872 lad->extLocation = cpu_to_lelb(epos->block);
1873 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1877 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1878 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1879 udf_update_tag(epos->bh->b_data, loffset);
1881 udf_update_tag(epos->bh->b_data,
1882 sizeof(struct allocExtDesc));
1883 mark_buffer_dirty_inode(epos->bh, inode);
1886 mark_inode_dirty(inode);
1891 udf_write_aext(inode, epos, eloc, elen, inc);
1894 iinfo->i_lenAlloc += adsize;
1895 mark_inode_dirty(inode);
1897 aed = (struct allocExtDesc *)epos->bh->b_data;
1898 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1899 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1900 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1901 udf_update_tag(epos->bh->b_data,
1902 epos->offset + (inc ? 0 : adsize));
1904 udf_update_tag(epos->bh->b_data,
1905 sizeof(struct allocExtDesc));
1906 mark_buffer_dirty_inode(epos->bh, inode);
1912 void udf_write_aext(struct inode *inode, struct extent_position *epos,
1913 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1917 struct short_ad *sad;
1918 struct long_ad *lad;
1919 struct udf_inode_info *iinfo = UDF_I(inode);
1922 ptr = iinfo->i_ext.i_data + epos->offset -
1923 udf_file_entry_alloc_offset(inode) +
1926 ptr = epos->bh->b_data + epos->offset;
1928 switch (iinfo->i_alloc_type) {
1929 case ICBTAG_FLAG_AD_SHORT:
1930 sad = (struct short_ad *)ptr;
1931 sad->extLength = cpu_to_le32(elen);
1932 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
1933 adsize = sizeof(struct short_ad);
1935 case ICBTAG_FLAG_AD_LONG:
1936 lad = (struct long_ad *)ptr;
1937 lad->extLength = cpu_to_le32(elen);
1938 lad->extLocation = cpu_to_lelb(*eloc);
1939 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1940 adsize = sizeof(struct long_ad);
1947 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1948 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
1949 struct allocExtDesc *aed =
1950 (struct allocExtDesc *)epos->bh->b_data;
1951 udf_update_tag(epos->bh->b_data,
1952 le32_to_cpu(aed->lengthAllocDescs) +
1953 sizeof(struct allocExtDesc));
1955 mark_buffer_dirty_inode(epos->bh, inode);
1957 mark_inode_dirty(inode);
1961 epos->offset += adsize;
1964 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
1965 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1969 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
1970 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
1972 epos->block = *eloc;
1973 epos->offset = sizeof(struct allocExtDesc);
1975 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
1976 epos->bh = udf_tread(inode->i_sb, block);
1978 udf_debug("reading block %d failed!\n", block);
1986 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
1987 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
1992 struct short_ad *sad;
1993 struct long_ad *lad;
1994 struct udf_inode_info *iinfo = UDF_I(inode);
1998 epos->offset = udf_file_entry_alloc_offset(inode);
1999 ptr = iinfo->i_ext.i_data + epos->offset -
2000 udf_file_entry_alloc_offset(inode) +
2002 alen = udf_file_entry_alloc_offset(inode) +
2006 epos->offset = sizeof(struct allocExtDesc);
2007 ptr = epos->bh->b_data + epos->offset;
2008 alen = sizeof(struct allocExtDesc) +
2009 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2013 switch (iinfo->i_alloc_type) {
2014 case ICBTAG_FLAG_AD_SHORT:
2015 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2018 etype = le32_to_cpu(sad->extLength) >> 30;
2019 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2020 eloc->partitionReferenceNum =
2021 iinfo->i_location.partitionReferenceNum;
2022 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2024 case ICBTAG_FLAG_AD_LONG:
2025 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2028 etype = le32_to_cpu(lad->extLength) >> 30;
2029 *eloc = lelb_to_cpu(lad->extLocation);
2030 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2033 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2040 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2041 struct kernel_lb_addr neloc, uint32_t nelen)
2043 struct kernel_lb_addr oeloc;
2050 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2051 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2053 nelen = (etype << 30) | oelen;
2055 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2058 return (nelen >> 30);
2061 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2062 struct kernel_lb_addr eloc, uint32_t elen)
2064 struct extent_position oepos;
2067 struct allocExtDesc *aed;
2068 struct udf_inode_info *iinfo;
2075 iinfo = UDF_I(inode);
2076 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2077 adsize = sizeof(struct short_ad);
2078 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2079 adsize = sizeof(struct long_ad);
2084 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2087 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2088 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2089 if (oepos.bh != epos.bh) {
2090 oepos.block = epos.block;
2094 oepos.offset = epos.offset - adsize;
2097 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2100 if (epos.bh != oepos.bh) {
2101 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2102 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2103 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2105 iinfo->i_lenAlloc -= (adsize * 2);
2106 mark_inode_dirty(inode);
2108 aed = (struct allocExtDesc *)oepos.bh->b_data;
2109 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2110 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2111 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2112 udf_update_tag(oepos.bh->b_data,
2113 oepos.offset - (2 * adsize));
2115 udf_update_tag(oepos.bh->b_data,
2116 sizeof(struct allocExtDesc));
2117 mark_buffer_dirty_inode(oepos.bh, inode);
2120 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2122 iinfo->i_lenAlloc -= adsize;
2123 mark_inode_dirty(inode);
2125 aed = (struct allocExtDesc *)oepos.bh->b_data;
2126 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2127 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2128 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2129 udf_update_tag(oepos.bh->b_data,
2130 epos.offset - adsize);
2132 udf_update_tag(oepos.bh->b_data,
2133 sizeof(struct allocExtDesc));
2134 mark_buffer_dirty_inode(oepos.bh, inode);
2141 return (elen >> 30);
2144 int8_t inode_bmap(struct inode *inode, sector_t block,
2145 struct extent_position *pos, struct kernel_lb_addr *eloc,
2146 uint32_t *elen, sector_t *offset)
2148 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2149 loff_t lbcount = 0, bcount =
2150 (loff_t) block << blocksize_bits;
2152 struct udf_inode_info *iinfo;
2154 iinfo = UDF_I(inode);
2156 pos->block = iinfo->i_location;
2161 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2163 *offset = (bcount - lbcount) >> blocksize_bits;
2164 iinfo->i_lenExtents = lbcount;
2168 } while (lbcount <= bcount);
2170 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2175 long udf_block_map(struct inode *inode, sector_t block)
2177 struct kernel_lb_addr eloc;
2180 struct extent_position epos = {};
2183 down_read(&UDF_I(inode)->i_data_sem);
2185 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2186 (EXT_RECORDED_ALLOCATED >> 30))
2187 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2191 up_read(&UDF_I(inode)->i_data_sem);
2194 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2195 return udf_fixed_to_variable(ret);