5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
22 * 12/06/98 blf partition support in udf_iget, udf_block_map
24 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/writeback.h>
37 #include <linux/slab.h>
38 #include <linux/crc-itu-t.h>
39 #include <linux/mpage.h>
40 #include <linux/uio.h>
41 #include <linux/bio.h>
46 MODULE_AUTHOR("Ben Fennema");
47 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
48 MODULE_LICENSE("GPL");
50 #define EXTENT_MERGE_SIZE 5
52 static umode_t udf_convert_permissions(struct fileEntry *);
53 static int udf_update_inode(struct inode *, int);
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 *, int *);
61 static void udf_prealloc_extents(struct inode *, int, int,
62 struct kernel_long_ad *, int *);
63 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
64 static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
65 int, struct extent_position *);
66 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
68 static void __udf_clear_extent_cache(struct inode *inode)
70 struct udf_inode_info *iinfo = UDF_I(inode);
72 if (iinfo->cached_extent.lstart != -1) {
73 brelse(iinfo->cached_extent.epos.bh);
74 iinfo->cached_extent.lstart = -1;
78 /* Invalidate extent cache */
79 static void udf_clear_extent_cache(struct inode *inode)
81 struct udf_inode_info *iinfo = UDF_I(inode);
83 spin_lock(&iinfo->i_extent_cache_lock);
84 __udf_clear_extent_cache(inode);
85 spin_unlock(&iinfo->i_extent_cache_lock);
88 /* Return contents of extent cache */
89 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
90 loff_t *lbcount, struct extent_position *pos)
92 struct udf_inode_info *iinfo = UDF_I(inode);
95 spin_lock(&iinfo->i_extent_cache_lock);
96 if ((iinfo->cached_extent.lstart <= bcount) &&
97 (iinfo->cached_extent.lstart != -1)) {
99 *lbcount = iinfo->cached_extent.lstart;
100 memcpy(pos, &iinfo->cached_extent.epos,
101 sizeof(struct extent_position));
106 spin_unlock(&iinfo->i_extent_cache_lock);
110 /* Add extent to extent cache */
111 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
112 struct extent_position *pos, int next_epos)
114 struct udf_inode_info *iinfo = UDF_I(inode);
116 spin_lock(&iinfo->i_extent_cache_lock);
117 /* Invalidate previously cached extent */
118 __udf_clear_extent_cache(inode);
121 memcpy(&iinfo->cached_extent.epos, pos,
122 sizeof(struct extent_position));
123 iinfo->cached_extent.lstart = estart;
125 switch (iinfo->i_alloc_type) {
126 case ICBTAG_FLAG_AD_SHORT:
127 iinfo->cached_extent.epos.offset -=
128 sizeof(struct short_ad);
130 case ICBTAG_FLAG_AD_LONG:
131 iinfo->cached_extent.epos.offset -=
132 sizeof(struct long_ad);
134 spin_unlock(&iinfo->i_extent_cache_lock);
137 void udf_evict_inode(struct inode *inode)
139 struct udf_inode_info *iinfo = UDF_I(inode);
142 if (!inode->i_nlink && !is_bad_inode(inode)) {
144 udf_setsize(inode, 0);
145 udf_update_inode(inode, IS_SYNC(inode));
147 truncate_inode_pages_final(&inode->i_data);
148 invalidate_inode_buffers(inode);
150 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
151 inode->i_size != iinfo->i_lenExtents) {
152 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",
153 inode->i_ino, inode->i_mode,
154 (unsigned long long)inode->i_size,
155 (unsigned long long)iinfo->i_lenExtents);
157 kfree(iinfo->i_ext.i_data);
158 iinfo->i_ext.i_data = NULL;
159 udf_clear_extent_cache(inode);
161 udf_free_inode(inode);
165 static void udf_write_failed(struct address_space *mapping, loff_t to)
167 struct inode *inode = mapping->host;
168 struct udf_inode_info *iinfo = UDF_I(inode);
169 loff_t isize = inode->i_size;
172 truncate_pagecache(inode, isize);
173 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
174 down_write(&iinfo->i_data_sem);
175 udf_clear_extent_cache(inode);
176 udf_truncate_extents(inode);
177 up_write(&iinfo->i_data_sem);
182 static int udf_writepage(struct page *page, struct writeback_control *wbc)
184 return block_write_full_page(page, udf_get_block, wbc);
187 static int udf_writepages(struct address_space *mapping,
188 struct writeback_control *wbc)
190 return mpage_writepages(mapping, wbc, udf_get_block);
193 static int udf_readpage(struct file *file, struct page *page)
195 return mpage_readpage(page, udf_get_block);
198 static int udf_readpages(struct file *file, struct address_space *mapping,
199 struct list_head *pages, unsigned nr_pages)
201 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
204 static int udf_write_begin(struct file *file, struct address_space *mapping,
205 loff_t pos, unsigned len, unsigned flags,
206 struct page **pagep, void **fsdata)
210 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
212 udf_write_failed(mapping, pos + len);
216 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
218 struct file *file = iocb->ki_filp;
219 struct address_space *mapping = file->f_mapping;
220 struct inode *inode = mapping->host;
221 size_t count = iov_iter_count(iter);
224 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
225 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
226 udf_write_failed(mapping, iocb->ki_pos + count);
230 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
232 return generic_block_bmap(mapping, block, udf_get_block);
235 const struct address_space_operations udf_aops = {
236 .readpage = udf_readpage,
237 .readpages = udf_readpages,
238 .writepage = udf_writepage,
239 .writepages = udf_writepages,
240 .write_begin = udf_write_begin,
241 .write_end = generic_write_end,
242 .direct_IO = udf_direct_IO,
247 * Expand file stored in ICB to a normal one-block-file
249 * This function requires i_data_sem for writing and releases it.
250 * This function requires i_mutex held
252 int udf_expand_file_adinicb(struct inode *inode)
256 struct udf_inode_info *iinfo = UDF_I(inode);
258 struct writeback_control udf_wbc = {
259 .sync_mode = WB_SYNC_NONE,
263 WARN_ON_ONCE(!inode_is_locked(inode));
264 if (!iinfo->i_lenAlloc) {
265 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
266 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
268 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
269 /* from now on we have normal address_space methods */
270 inode->i_data.a_ops = &udf_aops;
271 up_write(&iinfo->i_data_sem);
272 mark_inode_dirty(inode);
276 * Release i_data_sem so that we can lock a page - page lock ranks
277 * above i_data_sem. i_mutex still protects us against file changes.
279 up_write(&iinfo->i_data_sem);
281 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
285 if (!PageUptodate(page)) {
287 memset(kaddr + iinfo->i_lenAlloc, 0x00,
288 PAGE_SIZE - iinfo->i_lenAlloc);
289 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
291 flush_dcache_page(page);
292 SetPageUptodate(page);
295 down_write(&iinfo->i_data_sem);
296 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
298 iinfo->i_lenAlloc = 0;
299 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
300 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
302 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
303 /* from now on we have normal address_space methods */
304 inode->i_data.a_ops = &udf_aops;
305 up_write(&iinfo->i_data_sem);
306 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
308 /* Restore everything back so that we don't lose data... */
311 down_write(&iinfo->i_data_sem);
312 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
316 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
317 inode->i_data.a_ops = &udf_adinicb_aops;
318 up_write(&iinfo->i_data_sem);
321 mark_inode_dirty(inode);
326 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
330 struct buffer_head *dbh = NULL;
331 struct kernel_lb_addr eloc;
333 struct extent_position epos;
335 struct udf_fileident_bh sfibh, dfibh;
336 loff_t f_pos = udf_ext0_offset(inode);
337 int size = udf_ext0_offset(inode) + inode->i_size;
338 struct fileIdentDesc cfi, *sfi, *dfi;
339 struct udf_inode_info *iinfo = UDF_I(inode);
341 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
342 alloctype = ICBTAG_FLAG_AD_SHORT;
344 alloctype = ICBTAG_FLAG_AD_LONG;
346 if (!inode->i_size) {
347 iinfo->i_alloc_type = alloctype;
348 mark_inode_dirty(inode);
352 /* alloc block, and copy data to it */
353 *block = udf_new_block(inode->i_sb, inode,
354 iinfo->i_location.partitionReferenceNum,
355 iinfo->i_location.logicalBlockNum, err);
358 newblock = udf_get_pblock(inode->i_sb, *block,
359 iinfo->i_location.partitionReferenceNum,
363 dbh = udf_tgetblk(inode->i_sb, newblock);
367 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
368 set_buffer_uptodate(dbh);
370 mark_buffer_dirty_inode(dbh, inode);
372 sfibh.soffset = sfibh.eoffset =
373 f_pos & (inode->i_sb->s_blocksize - 1);
374 sfibh.sbh = sfibh.ebh = NULL;
375 dfibh.soffset = dfibh.eoffset = 0;
376 dfibh.sbh = dfibh.ebh = dbh;
377 while (f_pos < size) {
378 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
379 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
385 iinfo->i_alloc_type = alloctype;
386 sfi->descTag.tagLocation = cpu_to_le32(*block);
387 dfibh.soffset = dfibh.eoffset;
388 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
389 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
390 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
392 le16_to_cpu(sfi->lengthOfImpUse))) {
393 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
398 mark_buffer_dirty_inode(dbh, inode);
400 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
402 iinfo->i_lenAlloc = 0;
403 eloc.logicalBlockNum = *block;
404 eloc.partitionReferenceNum =
405 iinfo->i_location.partitionReferenceNum;
406 iinfo->i_lenExtents = inode->i_size;
408 epos.block = iinfo->i_location;
409 epos.offset = udf_file_entry_alloc_offset(inode);
410 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
414 mark_inode_dirty(inode);
418 static int udf_get_block(struct inode *inode, sector_t block,
419 struct buffer_head *bh_result, int create)
423 struct udf_inode_info *iinfo;
426 phys = udf_block_map(inode, block);
428 map_bh(bh_result, inode->i_sb, phys);
434 iinfo = UDF_I(inode);
436 down_write(&iinfo->i_data_sem);
437 if (block == iinfo->i_next_alloc_block + 1) {
438 iinfo->i_next_alloc_block++;
439 iinfo->i_next_alloc_goal++;
442 udf_clear_extent_cache(inode);
443 phys = inode_getblk(inode, block, &err, &new);
448 set_buffer_new(bh_result);
449 map_bh(bh_result, inode->i_sb, phys);
452 up_write(&iinfo->i_data_sem);
456 static struct buffer_head *udf_getblk(struct inode *inode, long block,
457 int create, int *err)
459 struct buffer_head *bh;
460 struct buffer_head dummy;
463 dummy.b_blocknr = -1000;
464 *err = udf_get_block(inode, block, &dummy, create);
465 if (!*err && buffer_mapped(&dummy)) {
466 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
467 if (buffer_new(&dummy)) {
469 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
470 set_buffer_uptodate(bh);
472 mark_buffer_dirty_inode(bh, inode);
480 /* Extend the file by 'blocks' blocks, return the number of extents added */
481 static int udf_do_extend_file(struct inode *inode,
482 struct extent_position *last_pos,
483 struct kernel_long_ad *last_ext,
487 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
488 struct super_block *sb = inode->i_sb;
489 struct kernel_lb_addr prealloc_loc = {};
490 int prealloc_len = 0;
491 struct udf_inode_info *iinfo;
494 /* The previous extent is fake and we should not extend by anything
495 * - there's nothing to do... */
499 iinfo = UDF_I(inode);
500 /* Round the last extent up to a multiple of block size */
501 if (last_ext->extLength & (sb->s_blocksize - 1)) {
502 last_ext->extLength =
503 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
504 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
505 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
506 iinfo->i_lenExtents =
507 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
508 ~(sb->s_blocksize - 1);
511 /* Last extent are just preallocated blocks? */
512 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
513 EXT_NOT_RECORDED_ALLOCATED) {
514 /* Save the extent so that we can reattach it to the end */
515 prealloc_loc = last_ext->extLocation;
516 prealloc_len = last_ext->extLength;
517 /* Mark the extent as a hole */
518 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
519 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
520 last_ext->extLocation.logicalBlockNum = 0;
521 last_ext->extLocation.partitionReferenceNum = 0;
524 /* Can we merge with the previous extent? */
525 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
526 EXT_NOT_RECORDED_NOT_ALLOCATED) {
527 add = ((1 << 30) - sb->s_blocksize -
528 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
529 sb->s_blocksize_bits;
533 last_ext->extLength += add << sb->s_blocksize_bits;
537 udf_add_aext(inode, last_pos, &last_ext->extLocation,
538 last_ext->extLength, 1);
541 struct kernel_lb_addr tmploc;
544 udf_write_aext(inode, last_pos, &last_ext->extLocation,
545 last_ext->extLength, 1);
547 * We've rewritten the last extent but there may be empty
548 * indirect extent after it - enter it.
550 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
553 /* Managed to do everything necessary? */
557 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
558 last_ext->extLocation.logicalBlockNum = 0;
559 last_ext->extLocation.partitionReferenceNum = 0;
560 add = (1 << (30-sb->s_blocksize_bits)) - 1;
561 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
562 (add << sb->s_blocksize_bits);
564 /* Create enough extents to cover the whole hole */
565 while (blocks > add) {
567 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
568 last_ext->extLength, 1);
574 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
575 (blocks << sb->s_blocksize_bits);
576 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
577 last_ext->extLength, 1);
584 /* Do we have some preallocated blocks saved? */
586 err = udf_add_aext(inode, last_pos, &prealloc_loc,
590 last_ext->extLocation = prealloc_loc;
591 last_ext->extLength = prealloc_len;
595 /* last_pos should point to the last written extent... */
596 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
597 last_pos->offset -= sizeof(struct short_ad);
598 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
599 last_pos->offset -= sizeof(struct long_ad);
606 static int udf_extend_file(struct inode *inode, loff_t newsize)
609 struct extent_position epos;
610 struct kernel_lb_addr eloc;
613 struct super_block *sb = inode->i_sb;
614 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
616 struct udf_inode_info *iinfo = UDF_I(inode);
617 struct kernel_long_ad extent;
620 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
621 adsize = sizeof(struct short_ad);
622 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
623 adsize = sizeof(struct long_ad);
627 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
629 /* File has extent covering the new size (could happen when extending
630 * inside a block)? */
633 if (newsize & (sb->s_blocksize - 1))
635 /* Extended file just to the boundary of the last file block? */
639 /* Truncate is extending the file by 'offset' blocks */
640 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
641 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
642 /* File has no extents at all or has empty last
643 * indirect extent! Create a fake extent... */
644 extent.extLocation.logicalBlockNum = 0;
645 extent.extLocation.partitionReferenceNum = 0;
646 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
648 epos.offset -= adsize;
649 etype = udf_next_aext(inode, &epos, &extent.extLocation,
650 &extent.extLength, 0);
651 extent.extLength |= etype << 30;
653 err = udf_do_extend_file(inode, &epos, &extent, offset);
657 iinfo->i_lenExtents = newsize;
663 static sector_t inode_getblk(struct inode *inode, sector_t block,
666 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
667 struct extent_position prev_epos, cur_epos, next_epos;
668 int count = 0, startnum = 0, endnum = 0;
669 uint32_t elen = 0, tmpelen;
670 struct kernel_lb_addr eloc, tmpeloc;
672 loff_t lbcount = 0, b_off = 0;
673 uint32_t newblocknum, newblock;
676 struct udf_inode_info *iinfo = UDF_I(inode);
677 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
683 prev_epos.offset = udf_file_entry_alloc_offset(inode);
684 prev_epos.block = iinfo->i_location;
686 cur_epos = next_epos = prev_epos;
687 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
689 /* find the extent which contains the block we are looking for.
690 alternate between laarr[0] and laarr[1] for locations of the
691 current extent, and the previous extent */
693 if (prev_epos.bh != cur_epos.bh) {
694 brelse(prev_epos.bh);
696 prev_epos.bh = cur_epos.bh;
698 if (cur_epos.bh != next_epos.bh) {
700 get_bh(next_epos.bh);
701 cur_epos.bh = next_epos.bh;
706 prev_epos.block = cur_epos.block;
707 cur_epos.block = next_epos.block;
709 prev_epos.offset = cur_epos.offset;
710 cur_epos.offset = next_epos.offset;
712 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
718 laarr[c].extLength = (etype << 30) | elen;
719 laarr[c].extLocation = eloc;
721 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
722 pgoal = eloc.logicalBlockNum +
723 ((elen + inode->i_sb->s_blocksize - 1) >>
724 inode->i_sb->s_blocksize_bits);
727 } while (lbcount + elen <= b_off);
730 offset = b_off >> inode->i_sb->s_blocksize_bits;
732 * Move prev_epos and cur_epos into indirect extent if we are at
735 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
736 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
738 /* if the extent is allocated and recorded, return the block
739 if the extent is not a multiple of the blocksize, round up */
741 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
742 if (elen & (inode->i_sb->s_blocksize - 1)) {
743 elen = EXT_RECORDED_ALLOCATED |
744 ((elen + inode->i_sb->s_blocksize - 1) &
745 ~(inode->i_sb->s_blocksize - 1));
746 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
748 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
752 /* Are we beyond EOF? */
761 /* Create a fake extent when there's not one */
762 memset(&laarr[0].extLocation, 0x00,
763 sizeof(struct kernel_lb_addr));
764 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
765 /* Will udf_do_extend_file() create real extent from
767 startnum = (offset > 0);
769 /* Create extents for the hole between EOF and offset */
770 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
779 /* We are not covered by a preallocated extent? */
780 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
781 EXT_NOT_RECORDED_ALLOCATED) {
782 /* Is there any real extent? - otherwise we overwrite
786 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
787 inode->i_sb->s_blocksize;
788 memset(&laarr[c].extLocation, 0x00,
789 sizeof(struct kernel_lb_addr));
796 endnum = startnum = ((count > 2) ? 2 : count);
798 /* if the current extent is in position 0,
799 swap it with the previous */
800 if (!c && count != 1) {
807 /* if the current block is located in an extent,
808 read the next extent */
809 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
811 laarr[c + 1].extLength = (etype << 30) | elen;
812 laarr[c + 1].extLocation = eloc;
820 /* if the current extent is not recorded but allocated, get the
821 * block in the extent corresponding to the requested block */
822 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
823 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
824 else { /* otherwise, allocate a new block */
825 if (iinfo->i_next_alloc_block == block)
826 goal = iinfo->i_next_alloc_goal;
829 if (!(goal = pgoal)) /* XXX: what was intended here? */
830 goal = iinfo->i_location.logicalBlockNum + 1;
833 newblocknum = udf_new_block(inode->i_sb, inode,
834 iinfo->i_location.partitionReferenceNum,
842 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
845 /* if the extent the requsted block is located in contains multiple
846 * blocks, split the extent into at most three extents. blocks prior
847 * to requested block, requested block, and blocks after requested
849 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
851 /* We preallocate blocks only for regular files. It also makes sense
852 * for directories but there's a problem when to drop the
853 * preallocation. We might use some delayed work for that but I feel
854 * it's overengineering for a filesystem like UDF. */
855 if (S_ISREG(inode->i_mode))
856 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
858 /* merge any continuous blocks in laarr */
859 udf_merge_extents(inode, laarr, &endnum);
861 /* write back the new extents, inserting new extents if the new number
862 * of extents is greater than the old number, and deleting extents if
863 * the new number of extents is less than the old number */
864 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
866 newblock = udf_get_pblock(inode->i_sb, newblocknum,
867 iinfo->i_location.partitionReferenceNum, 0);
873 iinfo->i_next_alloc_block = block;
874 iinfo->i_next_alloc_goal = newblocknum;
875 inode->i_ctime = current_time(inode);
878 udf_sync_inode(inode);
880 mark_inode_dirty(inode);
882 brelse(prev_epos.bh);
884 brelse(next_epos.bh);
888 static void udf_split_extents(struct inode *inode, int *c, int offset,
889 int newblocknum, struct kernel_long_ad *laarr,
892 unsigned long blocksize = inode->i_sb->s_blocksize;
893 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
895 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
896 (laarr[*c].extLength >> 30) ==
897 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
899 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
900 blocksize - 1) >> blocksize_bits;
901 int8_t etype = (laarr[curr].extLength >> 30);
905 else if (!offset || blen == offset + 1) {
906 laarr[curr + 2] = laarr[curr + 1];
907 laarr[curr + 1] = laarr[curr];
909 laarr[curr + 3] = laarr[curr + 1];
910 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
914 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
915 udf_free_blocks(inode->i_sb, inode,
916 &laarr[curr].extLocation,
918 laarr[curr].extLength =
919 EXT_NOT_RECORDED_NOT_ALLOCATED |
920 (offset << blocksize_bits);
921 laarr[curr].extLocation.logicalBlockNum = 0;
922 laarr[curr].extLocation.
923 partitionReferenceNum = 0;
925 laarr[curr].extLength = (etype << 30) |
926 (offset << blocksize_bits);
932 laarr[curr].extLocation.logicalBlockNum = newblocknum;
933 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
934 laarr[curr].extLocation.partitionReferenceNum =
935 UDF_I(inode)->i_location.partitionReferenceNum;
936 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
940 if (blen != offset + 1) {
941 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
942 laarr[curr].extLocation.logicalBlockNum +=
944 laarr[curr].extLength = (etype << 30) |
945 ((blen - (offset + 1)) << blocksize_bits);
952 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
953 struct kernel_long_ad *laarr,
956 int start, length = 0, currlength = 0, i;
958 if (*endnum >= (c + 1)) {
964 if ((laarr[c + 1].extLength >> 30) ==
965 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
967 length = currlength =
968 (((laarr[c + 1].extLength &
969 UDF_EXTENT_LENGTH_MASK) +
970 inode->i_sb->s_blocksize - 1) >>
971 inode->i_sb->s_blocksize_bits);
976 for (i = start + 1; i <= *endnum; i++) {
979 length += UDF_DEFAULT_PREALLOC_BLOCKS;
980 } else if ((laarr[i].extLength >> 30) ==
981 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
982 length += (((laarr[i].extLength &
983 UDF_EXTENT_LENGTH_MASK) +
984 inode->i_sb->s_blocksize - 1) >>
985 inode->i_sb->s_blocksize_bits);
991 int next = laarr[start].extLocation.logicalBlockNum +
992 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
993 inode->i_sb->s_blocksize - 1) >>
994 inode->i_sb->s_blocksize_bits);
995 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
996 laarr[start].extLocation.partitionReferenceNum,
997 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
998 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1001 if (start == (c + 1))
1002 laarr[start].extLength +=
1004 inode->i_sb->s_blocksize_bits);
1006 memmove(&laarr[c + 2], &laarr[c + 1],
1007 sizeof(struct long_ad) * (*endnum - (c + 1)));
1009 laarr[c + 1].extLocation.logicalBlockNum = next;
1010 laarr[c + 1].extLocation.partitionReferenceNum =
1011 laarr[c].extLocation.
1012 partitionReferenceNum;
1013 laarr[c + 1].extLength =
1014 EXT_NOT_RECORDED_ALLOCATED |
1016 inode->i_sb->s_blocksize_bits);
1020 for (i = start + 1; numalloc && i < *endnum; i++) {
1021 int elen = ((laarr[i].extLength &
1022 UDF_EXTENT_LENGTH_MASK) +
1023 inode->i_sb->s_blocksize - 1) >>
1024 inode->i_sb->s_blocksize_bits;
1026 if (elen > numalloc) {
1027 laarr[i].extLength -=
1029 inode->i_sb->s_blocksize_bits);
1033 if (*endnum > (i + 1))
1036 sizeof(struct long_ad) *
1037 (*endnum - (i + 1)));
1042 UDF_I(inode)->i_lenExtents +=
1043 numalloc << inode->i_sb->s_blocksize_bits;
1048 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1052 unsigned long blocksize = inode->i_sb->s_blocksize;
1053 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1055 for (i = 0; i < (*endnum - 1); i++) {
1056 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1057 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1059 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1060 (((li->extLength >> 30) ==
1061 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1062 ((lip1->extLocation.logicalBlockNum -
1063 li->extLocation.logicalBlockNum) ==
1064 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1065 blocksize - 1) >> blocksize_bits)))) {
1067 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1068 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1069 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1070 lip1->extLength = (lip1->extLength -
1072 UDF_EXTENT_LENGTH_MASK) +
1073 UDF_EXTENT_LENGTH_MASK) &
1075 li->extLength = (li->extLength &
1076 UDF_EXTENT_FLAG_MASK) +
1077 (UDF_EXTENT_LENGTH_MASK + 1) -
1079 lip1->extLocation.logicalBlockNum =
1080 li->extLocation.logicalBlockNum +
1082 UDF_EXTENT_LENGTH_MASK) >>
1085 li->extLength = lip1->extLength +
1087 UDF_EXTENT_LENGTH_MASK) +
1088 blocksize - 1) & ~(blocksize - 1));
1089 if (*endnum > (i + 2))
1090 memmove(&laarr[i + 1], &laarr[i + 2],
1091 sizeof(struct long_ad) *
1092 (*endnum - (i + 2)));
1096 } else if (((li->extLength >> 30) ==
1097 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1098 ((lip1->extLength >> 30) ==
1099 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1100 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1102 UDF_EXTENT_LENGTH_MASK) +
1103 blocksize - 1) >> blocksize_bits);
1104 li->extLocation.logicalBlockNum = 0;
1105 li->extLocation.partitionReferenceNum = 0;
1107 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1108 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1109 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1110 lip1->extLength = (lip1->extLength -
1112 UDF_EXTENT_LENGTH_MASK) +
1113 UDF_EXTENT_LENGTH_MASK) &
1115 li->extLength = (li->extLength &
1116 UDF_EXTENT_FLAG_MASK) +
1117 (UDF_EXTENT_LENGTH_MASK + 1) -
1120 li->extLength = lip1->extLength +
1122 UDF_EXTENT_LENGTH_MASK) +
1123 blocksize - 1) & ~(blocksize - 1));
1124 if (*endnum > (i + 2))
1125 memmove(&laarr[i + 1], &laarr[i + 2],
1126 sizeof(struct long_ad) *
1127 (*endnum - (i + 2)));
1131 } else if ((li->extLength >> 30) ==
1132 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1133 udf_free_blocks(inode->i_sb, inode,
1134 &li->extLocation, 0,
1136 UDF_EXTENT_LENGTH_MASK) +
1137 blocksize - 1) >> blocksize_bits);
1138 li->extLocation.logicalBlockNum = 0;
1139 li->extLocation.partitionReferenceNum = 0;
1140 li->extLength = (li->extLength &
1141 UDF_EXTENT_LENGTH_MASK) |
1142 EXT_NOT_RECORDED_NOT_ALLOCATED;
1147 static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1148 int startnum, int endnum,
1149 struct extent_position *epos)
1152 struct kernel_lb_addr tmploc;
1155 if (startnum > endnum) {
1156 for (i = 0; i < (startnum - endnum); i++)
1157 udf_delete_aext(inode, *epos, laarr[i].extLocation,
1158 laarr[i].extLength);
1159 } else if (startnum < endnum) {
1160 for (i = 0; i < (endnum - startnum); i++) {
1161 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1162 laarr[i].extLength);
1163 udf_next_aext(inode, epos, &laarr[i].extLocation,
1164 &laarr[i].extLength, 1);
1169 for (i = start; i < endnum; i++) {
1170 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1171 udf_write_aext(inode, epos, &laarr[i].extLocation,
1172 laarr[i].extLength, 1);
1176 struct buffer_head *udf_bread(struct inode *inode, int block,
1177 int create, int *err)
1179 struct buffer_head *bh = NULL;
1181 bh = udf_getblk(inode, block, create, err);
1185 if (buffer_uptodate(bh))
1188 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1191 if (buffer_uptodate(bh))
1199 int udf_setsize(struct inode *inode, loff_t newsize)
1202 struct udf_inode_info *iinfo;
1203 int bsize = 1 << inode->i_blkbits;
1205 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1206 S_ISLNK(inode->i_mode)))
1208 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1211 iinfo = UDF_I(inode);
1212 if (newsize > inode->i_size) {
1213 down_write(&iinfo->i_data_sem);
1214 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1216 (udf_file_entry_alloc_offset(inode) + newsize)) {
1217 err = udf_expand_file_adinicb(inode);
1220 down_write(&iinfo->i_data_sem);
1222 iinfo->i_lenAlloc = newsize;
1226 err = udf_extend_file(inode, newsize);
1228 up_write(&iinfo->i_data_sem);
1232 truncate_setsize(inode, newsize);
1233 up_write(&iinfo->i_data_sem);
1235 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1236 down_write(&iinfo->i_data_sem);
1237 udf_clear_extent_cache(inode);
1238 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1239 0x00, bsize - newsize -
1240 udf_file_entry_alloc_offset(inode));
1241 iinfo->i_lenAlloc = newsize;
1242 truncate_setsize(inode, newsize);
1243 up_write(&iinfo->i_data_sem);
1246 err = block_truncate_page(inode->i_mapping, newsize,
1250 down_write(&iinfo->i_data_sem);
1251 udf_clear_extent_cache(inode);
1252 truncate_setsize(inode, newsize);
1253 udf_truncate_extents(inode);
1254 up_write(&iinfo->i_data_sem);
1257 inode->i_mtime = inode->i_ctime = current_time(inode);
1259 udf_sync_inode(inode);
1261 mark_inode_dirty(inode);
1266 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1267 * arbitrary - just that we hopefully don't limit any real use of rewritten
1268 * inode on write-once media but avoid looping for too long on corrupted media.
1270 #define UDF_MAX_ICB_NESTING 1024
1272 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1274 struct buffer_head *bh = NULL;
1275 struct fileEntry *fe;
1276 struct extendedFileEntry *efe;
1278 struct udf_inode_info *iinfo = UDF_I(inode);
1279 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1280 struct kernel_lb_addr *iloc = &iinfo->i_location;
1281 unsigned int link_count;
1282 unsigned int indirections = 0;
1283 int bs = inode->i_sb->s_blocksize;
1287 if (iloc->logicalBlockNum >=
1288 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1289 udf_debug("block=%d, partition=%d out of range\n",
1290 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1295 * Set defaults, but the inode is still incomplete!
1296 * Note: get_new_inode() sets the following on a new inode:
1299 * i_flags = sb->s_flags
1301 * clean_inode(): zero fills and sets
1306 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1308 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1312 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1313 ident != TAG_IDENT_USE) {
1314 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1315 inode->i_ino, ident);
1319 fe = (struct fileEntry *)bh->b_data;
1320 efe = (struct extendedFileEntry *)bh->b_data;
1322 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1323 struct buffer_head *ibh;
1325 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1326 if (ident == TAG_IDENT_IE && ibh) {
1327 struct kernel_lb_addr loc;
1328 struct indirectEntry *ie;
1330 ie = (struct indirectEntry *)ibh->b_data;
1331 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1333 if (ie->indirectICB.extLength) {
1335 memcpy(&iinfo->i_location, &loc,
1336 sizeof(struct kernel_lb_addr));
1337 if (++indirections > UDF_MAX_ICB_NESTING) {
1338 udf_err(inode->i_sb,
1339 "too many ICBs in ICB hierarchy"
1340 " (max %d supported)\n",
1341 UDF_MAX_ICB_NESTING);
1349 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1350 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1351 le16_to_cpu(fe->icbTag.strategyType));
1354 if (fe->icbTag.strategyType == cpu_to_le16(4))
1355 iinfo->i_strat4096 = 0;
1356 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1357 iinfo->i_strat4096 = 1;
1359 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1360 ICBTAG_FLAG_AD_MASK;
1361 iinfo->i_unique = 0;
1362 iinfo->i_lenEAttr = 0;
1363 iinfo->i_lenExtents = 0;
1364 iinfo->i_lenAlloc = 0;
1365 iinfo->i_next_alloc_block = 0;
1366 iinfo->i_next_alloc_goal = 0;
1367 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1370 ret = udf_alloc_i_data(inode, bs -
1371 sizeof(struct extendedFileEntry));
1374 memcpy(iinfo->i_ext.i_data,
1375 bh->b_data + sizeof(struct extendedFileEntry),
1376 bs - sizeof(struct extendedFileEntry));
1377 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1380 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1383 memcpy(iinfo->i_ext.i_data,
1384 bh->b_data + sizeof(struct fileEntry),
1385 bs - sizeof(struct fileEntry));
1386 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1389 iinfo->i_lenAlloc = le32_to_cpu(
1390 ((struct unallocSpaceEntry *)bh->b_data)->
1392 ret = udf_alloc_i_data(inode, bs -
1393 sizeof(struct unallocSpaceEntry));
1396 memcpy(iinfo->i_ext.i_data,
1397 bh->b_data + sizeof(struct unallocSpaceEntry),
1398 bs - sizeof(struct unallocSpaceEntry));
1403 read_lock(&sbi->s_cred_lock);
1404 i_uid_write(inode, le32_to_cpu(fe->uid));
1405 if (!uid_valid(inode->i_uid) ||
1406 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1407 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1408 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1410 i_gid_write(inode, le32_to_cpu(fe->gid));
1411 if (!gid_valid(inode->i_gid) ||
1412 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1413 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1414 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1416 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1417 sbi->s_fmode != UDF_INVALID_MODE)
1418 inode->i_mode = sbi->s_fmode;
1419 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1420 sbi->s_dmode != UDF_INVALID_MODE)
1421 inode->i_mode = sbi->s_dmode;
1423 inode->i_mode = udf_convert_permissions(fe);
1424 inode->i_mode &= ~sbi->s_umask;
1425 read_unlock(&sbi->s_cred_lock);
1427 link_count = le16_to_cpu(fe->fileLinkCount);
1429 if (!hidden_inode) {
1435 set_nlink(inode, link_count);
1437 inode->i_size = le64_to_cpu(fe->informationLength);
1438 iinfo->i_lenExtents = inode->i_size;
1440 if (iinfo->i_efe == 0) {
1441 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1442 (inode->i_sb->s_blocksize_bits - 9);
1444 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1445 inode->i_atime = sbi->s_record_time;
1447 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1448 fe->modificationTime))
1449 inode->i_mtime = sbi->s_record_time;
1451 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1452 inode->i_ctime = sbi->s_record_time;
1454 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1455 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1456 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1457 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1459 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1460 (inode->i_sb->s_blocksize_bits - 9);
1462 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1463 inode->i_atime = sbi->s_record_time;
1465 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1466 efe->modificationTime))
1467 inode->i_mtime = sbi->s_record_time;
1469 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1470 iinfo->i_crtime = sbi->s_record_time;
1472 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1473 inode->i_ctime = sbi->s_record_time;
1475 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1476 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1477 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1478 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1480 inode->i_generation = iinfo->i_unique;
1483 * Sanity check length of allocation descriptors and extended attrs to
1484 * avoid integer overflows
1486 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1488 /* Now do exact checks */
1489 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1491 /* Sanity checks for files in ICB so that we don't get confused later */
1492 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1494 * For file in ICB data is stored in allocation descriptor
1495 * so sizes should match
1497 if (iinfo->i_lenAlloc != inode->i_size)
1499 /* File in ICB has to fit in there... */
1500 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1504 switch (fe->icbTag.fileType) {
1505 case ICBTAG_FILE_TYPE_DIRECTORY:
1506 inode->i_op = &udf_dir_inode_operations;
1507 inode->i_fop = &udf_dir_operations;
1508 inode->i_mode |= S_IFDIR;
1511 case ICBTAG_FILE_TYPE_REALTIME:
1512 case ICBTAG_FILE_TYPE_REGULAR:
1513 case ICBTAG_FILE_TYPE_UNDEF:
1514 case ICBTAG_FILE_TYPE_VAT20:
1515 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1516 inode->i_data.a_ops = &udf_adinicb_aops;
1518 inode->i_data.a_ops = &udf_aops;
1519 inode->i_op = &udf_file_inode_operations;
1520 inode->i_fop = &udf_file_operations;
1521 inode->i_mode |= S_IFREG;
1523 case ICBTAG_FILE_TYPE_BLOCK:
1524 inode->i_mode |= S_IFBLK;
1526 case ICBTAG_FILE_TYPE_CHAR:
1527 inode->i_mode |= S_IFCHR;
1529 case ICBTAG_FILE_TYPE_FIFO:
1530 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1532 case ICBTAG_FILE_TYPE_SOCKET:
1533 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1535 case ICBTAG_FILE_TYPE_SYMLINK:
1536 inode->i_data.a_ops = &udf_symlink_aops;
1537 inode->i_op = &udf_symlink_inode_operations;
1538 inode_nohighmem(inode);
1539 inode->i_mode = S_IFLNK | S_IRWXUGO;
1541 case ICBTAG_FILE_TYPE_MAIN:
1542 udf_debug("METADATA FILE-----\n");
1544 case ICBTAG_FILE_TYPE_MIRROR:
1545 udf_debug("METADATA MIRROR FILE-----\n");
1547 case ICBTAG_FILE_TYPE_BITMAP:
1548 udf_debug("METADATA BITMAP FILE-----\n");
1551 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1552 inode->i_ino, fe->icbTag.fileType);
1555 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1556 struct deviceSpec *dsea =
1557 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1559 init_special_inode(inode, inode->i_mode,
1560 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1561 le32_to_cpu(dsea->minorDeviceIdent)));
1562 /* Developer ID ??? */
1572 static int udf_alloc_i_data(struct inode *inode, size_t size)
1574 struct udf_inode_info *iinfo = UDF_I(inode);
1575 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1577 if (!iinfo->i_ext.i_data) {
1578 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1586 static umode_t udf_convert_permissions(struct fileEntry *fe)
1589 uint32_t permissions;
1592 permissions = le32_to_cpu(fe->permissions);
1593 flags = le16_to_cpu(fe->icbTag.flags);
1595 mode = ((permissions) & S_IRWXO) |
1596 ((permissions >> 2) & S_IRWXG) |
1597 ((permissions >> 4) & S_IRWXU) |
1598 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1599 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1600 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1605 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1607 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1610 static int udf_sync_inode(struct inode *inode)
1612 return udf_update_inode(inode, 1);
1615 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
1617 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1618 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1619 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1620 iinfo->i_crtime = time;
1623 static int udf_update_inode(struct inode *inode, int do_sync)
1625 struct buffer_head *bh = NULL;
1626 struct fileEntry *fe;
1627 struct extendedFileEntry *efe;
1628 uint64_t lb_recorded;
1633 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1634 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1635 struct udf_inode_info *iinfo = UDF_I(inode);
1637 bh = udf_tgetblk(inode->i_sb,
1638 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1640 udf_debug("getblk failure\n");
1645 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1646 fe = (struct fileEntry *)bh->b_data;
1647 efe = (struct extendedFileEntry *)bh->b_data;
1650 struct unallocSpaceEntry *use =
1651 (struct unallocSpaceEntry *)bh->b_data;
1653 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1654 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1655 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1656 sizeof(struct unallocSpaceEntry));
1657 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1658 crclen = sizeof(struct unallocSpaceEntry);
1663 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1664 fe->uid = cpu_to_le32(-1);
1666 fe->uid = cpu_to_le32(i_uid_read(inode));
1668 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1669 fe->gid = cpu_to_le32(-1);
1671 fe->gid = cpu_to_le32(i_gid_read(inode));
1673 udfperms = ((inode->i_mode & S_IRWXO)) |
1674 ((inode->i_mode & S_IRWXG) << 2) |
1675 ((inode->i_mode & S_IRWXU) << 4);
1677 udfperms |= (le32_to_cpu(fe->permissions) &
1678 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1679 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1680 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1681 fe->permissions = cpu_to_le32(udfperms);
1683 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1684 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1686 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1688 fe->informationLength = cpu_to_le64(inode->i_size);
1690 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1692 struct deviceSpec *dsea =
1693 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1695 dsea = (struct deviceSpec *)
1696 udf_add_extendedattr(inode,
1697 sizeof(struct deviceSpec) +
1698 sizeof(struct regid), 12, 0x3);
1699 dsea->attrType = cpu_to_le32(12);
1700 dsea->attrSubtype = 1;
1701 dsea->attrLength = cpu_to_le32(
1702 sizeof(struct deviceSpec) +
1703 sizeof(struct regid));
1704 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1706 eid = (struct regid *)dsea->impUse;
1707 memset(eid, 0, sizeof(struct regid));
1708 strcpy(eid->ident, UDF_ID_DEVELOPER);
1709 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1710 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1711 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1712 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1715 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1716 lb_recorded = 0; /* No extents => no blocks! */
1719 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1720 (blocksize_bits - 9);
1722 if (iinfo->i_efe == 0) {
1723 memcpy(bh->b_data + sizeof(struct fileEntry),
1724 iinfo->i_ext.i_data,
1725 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1726 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1728 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1729 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1730 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1731 memset(&(fe->impIdent), 0, sizeof(struct regid));
1732 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1733 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1734 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1735 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1736 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1737 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1738 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1739 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1740 crclen = sizeof(struct fileEntry);
1742 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1743 iinfo->i_ext.i_data,
1744 inode->i_sb->s_blocksize -
1745 sizeof(struct extendedFileEntry));
1746 efe->objectSize = cpu_to_le64(inode->i_size);
1747 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1749 udf_adjust_time(iinfo, inode->i_atime);
1750 udf_adjust_time(iinfo, inode->i_mtime);
1751 udf_adjust_time(iinfo, inode->i_ctime);
1753 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1754 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1755 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1756 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1758 memset(&(efe->impIdent), 0, sizeof(struct regid));
1759 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1760 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1761 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1762 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1763 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1764 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1765 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1766 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1767 crclen = sizeof(struct extendedFileEntry);
1771 if (iinfo->i_strat4096) {
1772 fe->icbTag.strategyType = cpu_to_le16(4096);
1773 fe->icbTag.strategyParameter = cpu_to_le16(1);
1774 fe->icbTag.numEntries = cpu_to_le16(2);
1776 fe->icbTag.strategyType = cpu_to_le16(4);
1777 fe->icbTag.numEntries = cpu_to_le16(1);
1781 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1782 else if (S_ISDIR(inode->i_mode))
1783 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1784 else if (S_ISREG(inode->i_mode))
1785 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1786 else if (S_ISLNK(inode->i_mode))
1787 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1788 else if (S_ISBLK(inode->i_mode))
1789 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1790 else if (S_ISCHR(inode->i_mode))
1791 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1792 else if (S_ISFIFO(inode->i_mode))
1793 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1794 else if (S_ISSOCK(inode->i_mode))
1795 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1797 icbflags = iinfo->i_alloc_type |
1798 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1799 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1800 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1801 (le16_to_cpu(fe->icbTag.flags) &
1802 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1803 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1805 fe->icbTag.flags = cpu_to_le16(icbflags);
1806 if (sbi->s_udfrev >= 0x0200)
1807 fe->descTag.descVersion = cpu_to_le16(3);
1809 fe->descTag.descVersion = cpu_to_le16(2);
1810 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1811 fe->descTag.tagLocation = cpu_to_le32(
1812 iinfo->i_location.logicalBlockNum);
1813 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1814 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1815 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1817 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1819 set_buffer_uptodate(bh);
1822 /* write the data blocks */
1823 mark_buffer_dirty(bh);
1825 sync_dirty_buffer(bh);
1826 if (buffer_write_io_error(bh)) {
1827 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1837 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1840 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1841 struct inode *inode = iget_locked(sb, block);
1845 return ERR_PTR(-ENOMEM);
1847 if (!(inode->i_state & I_NEW))
1850 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1851 err = udf_read_inode(inode, hidden_inode);
1854 return ERR_PTR(err);
1856 unlock_new_inode(inode);
1861 int udf_setup_indirect_aext(struct inode *inode, int block,
1862 struct extent_position *epos)
1864 struct super_block *sb = inode->i_sb;
1865 struct buffer_head *bh;
1866 struct allocExtDesc *aed;
1867 struct extent_position nepos;
1868 struct kernel_lb_addr neloc;
1871 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1872 adsize = sizeof(struct short_ad);
1873 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1874 adsize = sizeof(struct long_ad);
1878 neloc.logicalBlockNum = block;
1879 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1881 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1885 memset(bh->b_data, 0x00, sb->s_blocksize);
1886 set_buffer_uptodate(bh);
1888 mark_buffer_dirty_inode(bh, inode);
1890 aed = (struct allocExtDesc *)(bh->b_data);
1891 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1892 aed->previousAllocExtLocation =
1893 cpu_to_le32(epos->block.logicalBlockNum);
1895 aed->lengthAllocDescs = cpu_to_le32(0);
1896 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1900 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1901 sizeof(struct tag));
1903 nepos.block = neloc;
1904 nepos.offset = sizeof(struct allocExtDesc);
1908 * Do we have to copy current last extent to make space for indirect
1911 if (epos->offset + adsize > sb->s_blocksize) {
1912 struct kernel_lb_addr cp_loc;
1916 epos->offset -= adsize;
1917 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1918 cp_len |= ((uint32_t)cp_type) << 30;
1920 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1921 udf_write_aext(inode, epos, &nepos.block,
1922 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1924 __udf_add_aext(inode, epos, &nepos.block,
1925 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1935 * Append extent at the given position - should be the first free one in inode
1936 * / indirect extent. This function assumes there is enough space in the inode
1937 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1939 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1940 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1942 struct udf_inode_info *iinfo = UDF_I(inode);
1943 struct allocExtDesc *aed;
1946 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1947 adsize = sizeof(struct short_ad);
1948 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1949 adsize = sizeof(struct long_ad);
1954 WARN_ON(iinfo->i_lenAlloc !=
1955 epos->offset - udf_file_entry_alloc_offset(inode));
1957 aed = (struct allocExtDesc *)epos->bh->b_data;
1958 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1959 epos->offset - sizeof(struct allocExtDesc));
1960 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1963 udf_write_aext(inode, epos, eloc, elen, inc);
1966 iinfo->i_lenAlloc += adsize;
1967 mark_inode_dirty(inode);
1969 aed = (struct allocExtDesc *)epos->bh->b_data;
1970 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1971 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1972 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1973 udf_update_tag(epos->bh->b_data,
1974 epos->offset + (inc ? 0 : adsize));
1976 udf_update_tag(epos->bh->b_data,
1977 sizeof(struct allocExtDesc));
1978 mark_buffer_dirty_inode(epos->bh, inode);
1985 * Append extent at given position - should be the first free one in inode
1986 * / indirect extent. Takes care of allocating and linking indirect blocks.
1988 int udf_add_aext(struct inode *inode, struct extent_position *epos,
1989 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1992 struct super_block *sb = inode->i_sb;
1994 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1995 adsize = sizeof(struct short_ad);
1996 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1997 adsize = sizeof(struct long_ad);
2001 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2005 new_block = udf_new_block(sb, NULL,
2006 epos->block.partitionReferenceNum,
2007 epos->block.logicalBlockNum, &err);
2011 err = udf_setup_indirect_aext(inode, new_block, epos);
2016 return __udf_add_aext(inode, epos, eloc, elen, inc);
2019 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2020 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2024 struct short_ad *sad;
2025 struct long_ad *lad;
2026 struct udf_inode_info *iinfo = UDF_I(inode);
2029 ptr = iinfo->i_ext.i_data + epos->offset -
2030 udf_file_entry_alloc_offset(inode) +
2033 ptr = epos->bh->b_data + epos->offset;
2035 switch (iinfo->i_alloc_type) {
2036 case ICBTAG_FLAG_AD_SHORT:
2037 sad = (struct short_ad *)ptr;
2038 sad->extLength = cpu_to_le32(elen);
2039 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2040 adsize = sizeof(struct short_ad);
2042 case ICBTAG_FLAG_AD_LONG:
2043 lad = (struct long_ad *)ptr;
2044 lad->extLength = cpu_to_le32(elen);
2045 lad->extLocation = cpu_to_lelb(*eloc);
2046 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2047 adsize = sizeof(struct long_ad);
2054 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2055 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2056 struct allocExtDesc *aed =
2057 (struct allocExtDesc *)epos->bh->b_data;
2058 udf_update_tag(epos->bh->b_data,
2059 le32_to_cpu(aed->lengthAllocDescs) +
2060 sizeof(struct allocExtDesc));
2062 mark_buffer_dirty_inode(epos->bh, inode);
2064 mark_inode_dirty(inode);
2068 epos->offset += adsize;
2072 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2073 * someone does some weird stuff.
2075 #define UDF_MAX_INDIR_EXTS 16
2077 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2078 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2081 unsigned int indirections = 0;
2083 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2084 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2087 if (++indirections > UDF_MAX_INDIR_EXTS) {
2088 udf_err(inode->i_sb,
2089 "too many indirect extents in inode %lu\n",
2094 epos->block = *eloc;
2095 epos->offset = sizeof(struct allocExtDesc);
2097 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2098 epos->bh = udf_tread(inode->i_sb, block);
2100 udf_debug("reading block %d failed!\n", block);
2108 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2109 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2114 struct short_ad *sad;
2115 struct long_ad *lad;
2116 struct udf_inode_info *iinfo = UDF_I(inode);
2120 epos->offset = udf_file_entry_alloc_offset(inode);
2121 ptr = iinfo->i_ext.i_data + epos->offset -
2122 udf_file_entry_alloc_offset(inode) +
2124 alen = udf_file_entry_alloc_offset(inode) +
2128 epos->offset = sizeof(struct allocExtDesc);
2129 ptr = epos->bh->b_data + epos->offset;
2130 alen = sizeof(struct allocExtDesc) +
2131 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2135 switch (iinfo->i_alloc_type) {
2136 case ICBTAG_FLAG_AD_SHORT:
2137 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2140 etype = le32_to_cpu(sad->extLength) >> 30;
2141 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2142 eloc->partitionReferenceNum =
2143 iinfo->i_location.partitionReferenceNum;
2144 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2146 case ICBTAG_FLAG_AD_LONG:
2147 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2150 etype = le32_to_cpu(lad->extLength) >> 30;
2151 *eloc = lelb_to_cpu(lad->extLocation);
2152 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2155 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2162 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2163 struct kernel_lb_addr neloc, uint32_t nelen)
2165 struct kernel_lb_addr oeloc;
2172 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2173 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2175 nelen = (etype << 30) | oelen;
2177 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2180 return (nelen >> 30);
2183 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2184 struct kernel_lb_addr eloc, uint32_t elen)
2186 struct extent_position oepos;
2189 struct allocExtDesc *aed;
2190 struct udf_inode_info *iinfo;
2197 iinfo = UDF_I(inode);
2198 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2199 adsize = sizeof(struct short_ad);
2200 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2201 adsize = sizeof(struct long_ad);
2206 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2209 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2210 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2211 if (oepos.bh != epos.bh) {
2212 oepos.block = epos.block;
2216 oepos.offset = epos.offset - adsize;
2219 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2222 if (epos.bh != oepos.bh) {
2223 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2224 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2225 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2227 iinfo->i_lenAlloc -= (adsize * 2);
2228 mark_inode_dirty(inode);
2230 aed = (struct allocExtDesc *)oepos.bh->b_data;
2231 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2232 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2233 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2234 udf_update_tag(oepos.bh->b_data,
2235 oepos.offset - (2 * adsize));
2237 udf_update_tag(oepos.bh->b_data,
2238 sizeof(struct allocExtDesc));
2239 mark_buffer_dirty_inode(oepos.bh, inode);
2242 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2244 iinfo->i_lenAlloc -= adsize;
2245 mark_inode_dirty(inode);
2247 aed = (struct allocExtDesc *)oepos.bh->b_data;
2248 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2249 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2250 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2251 udf_update_tag(oepos.bh->b_data,
2252 epos.offset - adsize);
2254 udf_update_tag(oepos.bh->b_data,
2255 sizeof(struct allocExtDesc));
2256 mark_buffer_dirty_inode(oepos.bh, inode);
2263 return (elen >> 30);
2266 int8_t inode_bmap(struct inode *inode, sector_t block,
2267 struct extent_position *pos, struct kernel_lb_addr *eloc,
2268 uint32_t *elen, sector_t *offset)
2270 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2271 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2273 struct udf_inode_info *iinfo;
2275 iinfo = UDF_I(inode);
2276 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2278 pos->block = iinfo->i_location;
2283 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2285 *offset = (bcount - lbcount) >> blocksize_bits;
2286 iinfo->i_lenExtents = lbcount;
2290 } while (lbcount <= bcount);
2291 /* update extent cache */
2292 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2293 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2298 long udf_block_map(struct inode *inode, sector_t block)
2300 struct kernel_lb_addr eloc;
2303 struct extent_position epos = {};
2306 down_read(&UDF_I(inode)->i_data_sem);
2308 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2309 (EXT_RECORDED_ALLOCATED >> 30))
2310 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2314 up_read(&UDF_I(inode)->i_data_sem);
2317 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2318 return udf_fixed_to_variable(ret);