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>
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 int udf_sync_inode(struct inode *inode);
54 static int udf_alloc_i_data(struct inode *inode, size_t size);
55 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
56 static int8_t udf_insert_aext(struct inode *, struct extent_position,
57 struct kernel_lb_addr, uint32_t);
58 static void udf_split_extents(struct inode *, int *, int, int,
59 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
60 static void udf_prealloc_extents(struct inode *, int, int,
61 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
62 static void udf_merge_extents(struct inode *,
63 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
64 static void udf_update_extents(struct inode *,
65 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 struct extent_position *);
67 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
69 static void __udf_clear_extent_cache(struct inode *inode)
71 struct udf_inode_info *iinfo = UDF_I(inode);
73 if (iinfo->cached_extent.lstart != -1) {
74 brelse(iinfo->cached_extent.epos.bh);
75 iinfo->cached_extent.lstart = -1;
79 /* Invalidate extent cache */
80 static void udf_clear_extent_cache(struct inode *inode)
82 struct udf_inode_info *iinfo = UDF_I(inode);
84 spin_lock(&iinfo->i_extent_cache_lock);
85 __udf_clear_extent_cache(inode);
86 spin_unlock(&iinfo->i_extent_cache_lock);
89 /* Return contents of extent cache */
90 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
91 loff_t *lbcount, struct extent_position *pos)
93 struct udf_inode_info *iinfo = UDF_I(inode);
96 spin_lock(&iinfo->i_extent_cache_lock);
97 if ((iinfo->cached_extent.lstart <= bcount) &&
98 (iinfo->cached_extent.lstart != -1)) {
100 *lbcount = iinfo->cached_extent.lstart;
101 memcpy(pos, &iinfo->cached_extent.epos,
102 sizeof(struct extent_position));
107 spin_unlock(&iinfo->i_extent_cache_lock);
111 /* Add extent to extent cache */
112 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
113 struct extent_position *pos, int next_epos)
115 struct udf_inode_info *iinfo = UDF_I(inode);
117 spin_lock(&iinfo->i_extent_cache_lock);
118 /* Invalidate previously cached extent */
119 __udf_clear_extent_cache(inode);
122 memcpy(&iinfo->cached_extent.epos, pos,
123 sizeof(struct extent_position));
124 iinfo->cached_extent.lstart = estart;
126 switch (iinfo->i_alloc_type) {
127 case ICBTAG_FLAG_AD_SHORT:
128 iinfo->cached_extent.epos.offset -=
129 sizeof(struct short_ad);
131 case ICBTAG_FLAG_AD_LONG:
132 iinfo->cached_extent.epos.offset -=
133 sizeof(struct long_ad);
135 spin_unlock(&iinfo->i_extent_cache_lock);
138 void udf_evict_inode(struct inode *inode)
140 struct udf_inode_info *iinfo = UDF_I(inode);
143 if (!is_bad_inode(inode)) {
144 if (!inode->i_nlink) {
146 udf_setsize(inode, 0);
147 udf_update_inode(inode, IS_SYNC(inode));
149 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
150 inode->i_size != iinfo->i_lenExtents) {
151 udf_warn(inode->i_sb,
152 "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);
158 truncate_inode_pages_final(&inode->i_data);
159 invalidate_inode_buffers(inode);
161 kfree(iinfo->i_ext.i_data);
162 iinfo->i_ext.i_data = NULL;
163 udf_clear_extent_cache(inode);
165 udf_free_inode(inode);
169 static void udf_write_failed(struct address_space *mapping, loff_t to)
171 struct inode *inode = mapping->host;
172 struct udf_inode_info *iinfo = UDF_I(inode);
173 loff_t isize = inode->i_size;
176 truncate_pagecache(inode, isize);
177 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
178 down_write(&iinfo->i_data_sem);
179 udf_clear_extent_cache(inode);
180 udf_truncate_extents(inode);
181 up_write(&iinfo->i_data_sem);
186 static int udf_writepage(struct page *page, struct writeback_control *wbc)
188 return block_write_full_page(page, udf_get_block, wbc);
191 static int udf_writepages(struct address_space *mapping,
192 struct writeback_control *wbc)
194 return mpage_writepages(mapping, wbc, udf_get_block);
197 static int udf_readpage(struct file *file, struct page *page)
199 return mpage_readpage(page, udf_get_block);
202 static int udf_readpages(struct file *file, struct address_space *mapping,
203 struct list_head *pages, unsigned nr_pages)
205 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
208 static int udf_write_begin(struct file *file, struct address_space *mapping,
209 loff_t pos, unsigned len, unsigned flags,
210 struct page **pagep, void **fsdata)
214 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
216 udf_write_failed(mapping, pos + len);
220 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
222 struct file *file = iocb->ki_filp;
223 struct address_space *mapping = file->f_mapping;
224 struct inode *inode = mapping->host;
225 size_t count = iov_iter_count(iter);
228 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
229 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
230 udf_write_failed(mapping, iocb->ki_pos + count);
234 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
236 return generic_block_bmap(mapping, block, udf_get_block);
239 const struct address_space_operations udf_aops = {
240 .readpage = udf_readpage,
241 .readpages = udf_readpages,
242 .writepage = udf_writepage,
243 .writepages = udf_writepages,
244 .write_begin = udf_write_begin,
245 .write_end = generic_write_end,
246 .direct_IO = udf_direct_IO,
251 * Expand file stored in ICB to a normal one-block-file
253 * This function requires i_data_sem for writing and releases it.
254 * This function requires i_mutex held
256 int udf_expand_file_adinicb(struct inode *inode)
260 struct udf_inode_info *iinfo = UDF_I(inode);
262 struct writeback_control udf_wbc = {
263 .sync_mode = WB_SYNC_NONE,
267 WARN_ON_ONCE(!inode_is_locked(inode));
268 if (!iinfo->i_lenAlloc) {
269 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
270 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
272 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
273 /* from now on we have normal address_space methods */
274 inode->i_data.a_ops = &udf_aops;
275 up_write(&iinfo->i_data_sem);
276 mark_inode_dirty(inode);
280 * Release i_data_sem so that we can lock a page - page lock ranks
281 * above i_data_sem. i_mutex still protects us against file changes.
283 up_write(&iinfo->i_data_sem);
285 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
289 if (!PageUptodate(page)) {
291 memset(kaddr + iinfo->i_lenAlloc, 0x00,
292 PAGE_SIZE - iinfo->i_lenAlloc);
293 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
295 flush_dcache_page(page);
296 SetPageUptodate(page);
299 down_write(&iinfo->i_data_sem);
300 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
302 iinfo->i_lenAlloc = 0;
303 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
304 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
306 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
307 /* from now on we have normal address_space methods */
308 inode->i_data.a_ops = &udf_aops;
309 up_write(&iinfo->i_data_sem);
310 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
312 /* Restore everything back so that we don't lose data... */
315 down_write(&iinfo->i_data_sem);
316 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
320 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
321 inode->i_data.a_ops = &udf_adinicb_aops;
322 up_write(&iinfo->i_data_sem);
325 mark_inode_dirty(inode);
330 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
334 struct buffer_head *dbh = NULL;
335 struct kernel_lb_addr eloc;
337 struct extent_position epos;
339 struct udf_fileident_bh sfibh, dfibh;
340 loff_t f_pos = udf_ext0_offset(inode);
341 int size = udf_ext0_offset(inode) + inode->i_size;
342 struct fileIdentDesc cfi, *sfi, *dfi;
343 struct udf_inode_info *iinfo = UDF_I(inode);
345 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
346 alloctype = ICBTAG_FLAG_AD_SHORT;
348 alloctype = ICBTAG_FLAG_AD_LONG;
350 if (!inode->i_size) {
351 iinfo->i_alloc_type = alloctype;
352 mark_inode_dirty(inode);
356 /* alloc block, and copy data to it */
357 *block = udf_new_block(inode->i_sb, inode,
358 iinfo->i_location.partitionReferenceNum,
359 iinfo->i_location.logicalBlockNum, err);
362 newblock = udf_get_pblock(inode->i_sb, *block,
363 iinfo->i_location.partitionReferenceNum,
367 dbh = udf_tgetblk(inode->i_sb, newblock);
371 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
372 set_buffer_uptodate(dbh);
374 mark_buffer_dirty_inode(dbh, inode);
376 sfibh.soffset = sfibh.eoffset =
377 f_pos & (inode->i_sb->s_blocksize - 1);
378 sfibh.sbh = sfibh.ebh = NULL;
379 dfibh.soffset = dfibh.eoffset = 0;
380 dfibh.sbh = dfibh.ebh = dbh;
381 while (f_pos < size) {
382 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
383 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
389 iinfo->i_alloc_type = alloctype;
390 sfi->descTag.tagLocation = cpu_to_le32(*block);
391 dfibh.soffset = dfibh.eoffset;
392 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
393 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
394 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
396 le16_to_cpu(sfi->lengthOfImpUse))) {
397 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
402 mark_buffer_dirty_inode(dbh, inode);
404 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
406 iinfo->i_lenAlloc = 0;
407 eloc.logicalBlockNum = *block;
408 eloc.partitionReferenceNum =
409 iinfo->i_location.partitionReferenceNum;
410 iinfo->i_lenExtents = inode->i_size;
412 epos.block = iinfo->i_location;
413 epos.offset = udf_file_entry_alloc_offset(inode);
414 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
418 mark_inode_dirty(inode);
422 static int udf_get_block(struct inode *inode, sector_t block,
423 struct buffer_head *bh_result, int create)
427 struct udf_inode_info *iinfo;
430 phys = udf_block_map(inode, block);
432 map_bh(bh_result, inode->i_sb, phys);
438 iinfo = UDF_I(inode);
440 down_write(&iinfo->i_data_sem);
441 if (block == iinfo->i_next_alloc_block + 1) {
442 iinfo->i_next_alloc_block++;
443 iinfo->i_next_alloc_goal++;
446 udf_clear_extent_cache(inode);
447 phys = inode_getblk(inode, block, &err, &new);
452 set_buffer_new(bh_result);
453 map_bh(bh_result, inode->i_sb, phys);
456 up_write(&iinfo->i_data_sem);
460 static struct buffer_head *udf_getblk(struct inode *inode, long block,
461 int create, int *err)
463 struct buffer_head *bh;
464 struct buffer_head dummy;
467 dummy.b_blocknr = -1000;
468 *err = udf_get_block(inode, block, &dummy, create);
469 if (!*err && buffer_mapped(&dummy)) {
470 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
471 if (buffer_new(&dummy)) {
473 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
474 set_buffer_uptodate(bh);
476 mark_buffer_dirty_inode(bh, inode);
484 /* Extend the file with new blocks totaling 'new_block_bytes',
485 * return the number of extents added
487 static int udf_do_extend_file(struct inode *inode,
488 struct extent_position *last_pos,
489 struct kernel_long_ad *last_ext,
490 loff_t new_block_bytes)
493 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
494 struct super_block *sb = inode->i_sb;
495 struct kernel_lb_addr prealloc_loc = {};
496 int prealloc_len = 0;
497 struct udf_inode_info *iinfo;
500 /* The previous extent is fake and we should not extend by anything
501 * - there's nothing to do... */
502 if (!new_block_bytes && fake)
505 iinfo = UDF_I(inode);
506 /* Round the last extent up to a multiple of block size */
507 if (last_ext->extLength & (sb->s_blocksize - 1)) {
508 last_ext->extLength =
509 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
510 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
511 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
512 iinfo->i_lenExtents =
513 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
514 ~(sb->s_blocksize - 1);
517 /* Last extent are just preallocated blocks? */
518 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
519 EXT_NOT_RECORDED_ALLOCATED) {
520 /* Save the extent so that we can reattach it to the end */
521 prealloc_loc = last_ext->extLocation;
522 prealloc_len = last_ext->extLength;
523 /* Mark the extent as a hole */
524 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
525 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
526 last_ext->extLocation.logicalBlockNum = 0;
527 last_ext->extLocation.partitionReferenceNum = 0;
530 /* Can we merge with the previous extent? */
531 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
532 EXT_NOT_RECORDED_NOT_ALLOCATED) {
533 add = (1 << 30) - sb->s_blocksize -
534 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
535 if (add > new_block_bytes)
536 add = new_block_bytes;
537 new_block_bytes -= add;
538 last_ext->extLength += add;
542 udf_add_aext(inode, last_pos, &last_ext->extLocation,
543 last_ext->extLength, 1);
546 struct kernel_lb_addr tmploc;
549 udf_write_aext(inode, last_pos, &last_ext->extLocation,
550 last_ext->extLength, 1);
553 * We've rewritten the last extent. If we are going to add
554 * more extents, we may need to enter possible following
555 * empty indirect extent.
557 if (new_block_bytes || prealloc_len)
558 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
561 /* Managed to do everything necessary? */
562 if (!new_block_bytes)
565 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
566 last_ext->extLocation.logicalBlockNum = 0;
567 last_ext->extLocation.partitionReferenceNum = 0;
568 add = (1 << 30) - sb->s_blocksize;
569 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
571 /* Create enough extents to cover the whole hole */
572 while (new_block_bytes > add) {
573 new_block_bytes -= add;
574 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
575 last_ext->extLength, 1);
580 if (new_block_bytes) {
581 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
583 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
584 last_ext->extLength, 1);
591 /* Do we have some preallocated blocks saved? */
593 err = udf_add_aext(inode, last_pos, &prealloc_loc,
597 last_ext->extLocation = prealloc_loc;
598 last_ext->extLength = prealloc_len;
602 /* last_pos should point to the last written extent... */
603 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
604 last_pos->offset -= sizeof(struct short_ad);
605 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
606 last_pos->offset -= sizeof(struct long_ad);
613 /* Extend the final block of the file to final_block_len bytes */
614 static void udf_do_extend_final_block(struct inode *inode,
615 struct extent_position *last_pos,
616 struct kernel_long_ad *last_ext,
617 uint32_t final_block_len)
619 struct super_block *sb = inode->i_sb;
620 uint32_t added_bytes;
622 added_bytes = final_block_len -
623 (last_ext->extLength & (sb->s_blocksize - 1));
624 last_ext->extLength += added_bytes;
625 UDF_I(inode)->i_lenExtents += added_bytes;
627 udf_write_aext(inode, last_pos, &last_ext->extLocation,
628 last_ext->extLength, 1);
631 static int udf_extend_file(struct inode *inode, loff_t newsize)
634 struct extent_position epos;
635 struct kernel_lb_addr eloc;
638 struct super_block *sb = inode->i_sb;
639 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
640 unsigned long partial_final_block;
642 struct udf_inode_info *iinfo = UDF_I(inode);
643 struct kernel_long_ad extent;
645 int within_final_block;
647 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
648 adsize = sizeof(struct short_ad);
649 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
650 adsize = sizeof(struct long_ad);
654 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
655 within_final_block = (etype != -1);
657 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
658 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
659 /* File has no extents at all or has empty last
660 * indirect extent! Create a fake extent... */
661 extent.extLocation.logicalBlockNum = 0;
662 extent.extLocation.partitionReferenceNum = 0;
663 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
665 epos.offset -= adsize;
666 etype = udf_next_aext(inode, &epos, &extent.extLocation,
667 &extent.extLength, 0);
668 extent.extLength |= etype << 30;
671 partial_final_block = newsize & (sb->s_blocksize - 1);
673 /* File has extent covering the new size (could happen when extending
676 if (within_final_block) {
677 /* Extending file within the last file block */
678 udf_do_extend_final_block(inode, &epos, &extent,
679 partial_final_block);
681 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
683 err = udf_do_extend_file(inode, &epos, &extent, add);
689 iinfo->i_lenExtents = newsize;
695 static sector_t inode_getblk(struct inode *inode, sector_t block,
698 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
699 struct extent_position prev_epos, cur_epos, next_epos;
700 int count = 0, startnum = 0, endnum = 0;
701 uint32_t elen = 0, tmpelen;
702 struct kernel_lb_addr eloc, tmpeloc;
704 loff_t lbcount = 0, b_off = 0;
705 uint32_t newblocknum, newblock;
708 struct udf_inode_info *iinfo = UDF_I(inode);
709 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
715 prev_epos.offset = udf_file_entry_alloc_offset(inode);
716 prev_epos.block = iinfo->i_location;
718 cur_epos = next_epos = prev_epos;
719 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
721 /* find the extent which contains the block we are looking for.
722 alternate between laarr[0] and laarr[1] for locations of the
723 current extent, and the previous extent */
725 if (prev_epos.bh != cur_epos.bh) {
726 brelse(prev_epos.bh);
728 prev_epos.bh = cur_epos.bh;
730 if (cur_epos.bh != next_epos.bh) {
732 get_bh(next_epos.bh);
733 cur_epos.bh = next_epos.bh;
738 prev_epos.block = cur_epos.block;
739 cur_epos.block = next_epos.block;
741 prev_epos.offset = cur_epos.offset;
742 cur_epos.offset = next_epos.offset;
744 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
750 laarr[c].extLength = (etype << 30) | elen;
751 laarr[c].extLocation = eloc;
753 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
754 pgoal = eloc.logicalBlockNum +
755 ((elen + inode->i_sb->s_blocksize - 1) >>
756 inode->i_sb->s_blocksize_bits);
759 } while (lbcount + elen <= b_off);
762 offset = b_off >> inode->i_sb->s_blocksize_bits;
764 * Move prev_epos and cur_epos into indirect extent if we are at
767 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
768 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
770 /* if the extent is allocated and recorded, return the block
771 if the extent is not a multiple of the blocksize, round up */
773 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
774 if (elen & (inode->i_sb->s_blocksize - 1)) {
775 elen = EXT_RECORDED_ALLOCATED |
776 ((elen + inode->i_sb->s_blocksize - 1) &
777 ~(inode->i_sb->s_blocksize - 1));
778 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
780 brelse(prev_epos.bh);
782 brelse(next_epos.bh);
783 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
787 /* Are we beyond EOF? */
797 /* Create a fake extent when there's not one */
798 memset(&laarr[0].extLocation, 0x00,
799 sizeof(struct kernel_lb_addr));
800 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
801 /* Will udf_do_extend_file() create real extent from
803 startnum = (offset > 0);
805 /* Create extents for the hole between EOF and offset */
806 hole_len = (loff_t)offset << inode->i_blkbits;
807 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
809 brelse(prev_epos.bh);
811 brelse(next_epos.bh);
818 /* We are not covered by a preallocated extent? */
819 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
820 EXT_NOT_RECORDED_ALLOCATED) {
821 /* Is there any real extent? - otherwise we overwrite
825 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
826 inode->i_sb->s_blocksize;
827 memset(&laarr[c].extLocation, 0x00,
828 sizeof(struct kernel_lb_addr));
835 endnum = startnum = ((count > 2) ? 2 : count);
837 /* if the current extent is in position 0,
838 swap it with the previous */
839 if (!c && count != 1) {
846 /* if the current block is located in an extent,
847 read the next extent */
848 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
850 laarr[c + 1].extLength = (etype << 30) | elen;
851 laarr[c + 1].extLocation = eloc;
859 /* if the current extent is not recorded but allocated, get the
860 * block in the extent corresponding to the requested block */
861 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
862 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
863 else { /* otherwise, allocate a new block */
864 if (iinfo->i_next_alloc_block == block)
865 goal = iinfo->i_next_alloc_goal;
868 if (!(goal = pgoal)) /* XXX: what was intended here? */
869 goal = iinfo->i_location.logicalBlockNum + 1;
872 newblocknum = udf_new_block(inode->i_sb, inode,
873 iinfo->i_location.partitionReferenceNum,
876 brelse(prev_epos.bh);
878 brelse(next_epos.bh);
883 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
886 /* if the extent the requsted block is located in contains multiple
887 * blocks, split the extent into at most three extents. blocks prior
888 * to requested block, requested block, and blocks after requested
890 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
892 #ifdef UDF_PREALLOCATE
893 /* We preallocate blocks only for regular files. It also makes sense
894 * for directories but there's a problem when to drop the
895 * preallocation. We might use some delayed work for that but I feel
896 * it's overengineering for a filesystem like UDF. */
897 if (S_ISREG(inode->i_mode))
898 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
901 /* merge any continuous blocks in laarr */
902 udf_merge_extents(inode, laarr, &endnum);
904 /* write back the new extents, inserting new extents if the new number
905 * of extents is greater than the old number, and deleting extents if
906 * the new number of extents is less than the old number */
907 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
909 brelse(prev_epos.bh);
911 brelse(next_epos.bh);
913 newblock = udf_get_pblock(inode->i_sb, newblocknum,
914 iinfo->i_location.partitionReferenceNum, 0);
920 iinfo->i_next_alloc_block = block;
921 iinfo->i_next_alloc_goal = newblocknum;
922 inode->i_ctime = current_time(inode);
925 udf_sync_inode(inode);
927 mark_inode_dirty(inode);
932 static void udf_split_extents(struct inode *inode, int *c, int offset,
934 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
937 unsigned long blocksize = inode->i_sb->s_blocksize;
938 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
940 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
941 (laarr[*c].extLength >> 30) ==
942 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
944 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
945 blocksize - 1) >> blocksize_bits;
946 int8_t etype = (laarr[curr].extLength >> 30);
950 else if (!offset || blen == offset + 1) {
951 laarr[curr + 2] = laarr[curr + 1];
952 laarr[curr + 1] = laarr[curr];
954 laarr[curr + 3] = laarr[curr + 1];
955 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
959 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
960 udf_free_blocks(inode->i_sb, inode,
961 &laarr[curr].extLocation,
963 laarr[curr].extLength =
964 EXT_NOT_RECORDED_NOT_ALLOCATED |
965 (offset << blocksize_bits);
966 laarr[curr].extLocation.logicalBlockNum = 0;
967 laarr[curr].extLocation.
968 partitionReferenceNum = 0;
970 laarr[curr].extLength = (etype << 30) |
971 (offset << blocksize_bits);
977 laarr[curr].extLocation.logicalBlockNum = newblocknum;
978 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
979 laarr[curr].extLocation.partitionReferenceNum =
980 UDF_I(inode)->i_location.partitionReferenceNum;
981 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
985 if (blen != offset + 1) {
986 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
987 laarr[curr].extLocation.logicalBlockNum +=
989 laarr[curr].extLength = (etype << 30) |
990 ((blen - (offset + 1)) << blocksize_bits);
997 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
998 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1001 int start, length = 0, currlength = 0, i;
1003 if (*endnum >= (c + 1)) {
1009 if ((laarr[c + 1].extLength >> 30) ==
1010 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1012 length = currlength =
1013 (((laarr[c + 1].extLength &
1014 UDF_EXTENT_LENGTH_MASK) +
1015 inode->i_sb->s_blocksize - 1) >>
1016 inode->i_sb->s_blocksize_bits);
1021 for (i = start + 1; i <= *endnum; i++) {
1024 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1025 } else if ((laarr[i].extLength >> 30) ==
1026 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1027 length += (((laarr[i].extLength &
1028 UDF_EXTENT_LENGTH_MASK) +
1029 inode->i_sb->s_blocksize - 1) >>
1030 inode->i_sb->s_blocksize_bits);
1036 int next = laarr[start].extLocation.logicalBlockNum +
1037 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1038 inode->i_sb->s_blocksize - 1) >>
1039 inode->i_sb->s_blocksize_bits);
1040 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1041 laarr[start].extLocation.partitionReferenceNum,
1042 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1043 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1046 if (start == (c + 1))
1047 laarr[start].extLength +=
1049 inode->i_sb->s_blocksize_bits);
1051 memmove(&laarr[c + 2], &laarr[c + 1],
1052 sizeof(struct long_ad) * (*endnum - (c + 1)));
1054 laarr[c + 1].extLocation.logicalBlockNum = next;
1055 laarr[c + 1].extLocation.partitionReferenceNum =
1056 laarr[c].extLocation.
1057 partitionReferenceNum;
1058 laarr[c + 1].extLength =
1059 EXT_NOT_RECORDED_ALLOCATED |
1061 inode->i_sb->s_blocksize_bits);
1065 for (i = start + 1; numalloc && i < *endnum; i++) {
1066 int elen = ((laarr[i].extLength &
1067 UDF_EXTENT_LENGTH_MASK) +
1068 inode->i_sb->s_blocksize - 1) >>
1069 inode->i_sb->s_blocksize_bits;
1071 if (elen > numalloc) {
1072 laarr[i].extLength -=
1074 inode->i_sb->s_blocksize_bits);
1078 if (*endnum > (i + 1))
1081 sizeof(struct long_ad) *
1082 (*endnum - (i + 1)));
1087 UDF_I(inode)->i_lenExtents +=
1088 numalloc << inode->i_sb->s_blocksize_bits;
1093 static void udf_merge_extents(struct inode *inode,
1094 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1098 unsigned long blocksize = inode->i_sb->s_blocksize;
1099 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1101 for (i = 0; i < (*endnum - 1); i++) {
1102 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1103 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1105 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1106 (((li->extLength >> 30) ==
1107 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1108 ((lip1->extLocation.logicalBlockNum -
1109 li->extLocation.logicalBlockNum) ==
1110 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1111 blocksize - 1) >> blocksize_bits)))) {
1113 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1114 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1115 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1116 lip1->extLength = (lip1->extLength -
1118 UDF_EXTENT_LENGTH_MASK) +
1119 UDF_EXTENT_LENGTH_MASK) &
1121 li->extLength = (li->extLength &
1122 UDF_EXTENT_FLAG_MASK) +
1123 (UDF_EXTENT_LENGTH_MASK + 1) -
1125 lip1->extLocation.logicalBlockNum =
1126 li->extLocation.logicalBlockNum +
1128 UDF_EXTENT_LENGTH_MASK) >>
1131 li->extLength = lip1->extLength +
1133 UDF_EXTENT_LENGTH_MASK) +
1134 blocksize - 1) & ~(blocksize - 1));
1135 if (*endnum > (i + 2))
1136 memmove(&laarr[i + 1], &laarr[i + 2],
1137 sizeof(struct long_ad) *
1138 (*endnum - (i + 2)));
1142 } else if (((li->extLength >> 30) ==
1143 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1144 ((lip1->extLength >> 30) ==
1145 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1146 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1148 UDF_EXTENT_LENGTH_MASK) +
1149 blocksize - 1) >> blocksize_bits);
1150 li->extLocation.logicalBlockNum = 0;
1151 li->extLocation.partitionReferenceNum = 0;
1153 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1154 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1155 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1156 lip1->extLength = (lip1->extLength -
1158 UDF_EXTENT_LENGTH_MASK) +
1159 UDF_EXTENT_LENGTH_MASK) &
1161 li->extLength = (li->extLength &
1162 UDF_EXTENT_FLAG_MASK) +
1163 (UDF_EXTENT_LENGTH_MASK + 1) -
1166 li->extLength = lip1->extLength +
1168 UDF_EXTENT_LENGTH_MASK) +
1169 blocksize - 1) & ~(blocksize - 1));
1170 if (*endnum > (i + 2))
1171 memmove(&laarr[i + 1], &laarr[i + 2],
1172 sizeof(struct long_ad) *
1173 (*endnum - (i + 2)));
1177 } else if ((li->extLength >> 30) ==
1178 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1179 udf_free_blocks(inode->i_sb, inode,
1180 &li->extLocation, 0,
1182 UDF_EXTENT_LENGTH_MASK) +
1183 blocksize - 1) >> blocksize_bits);
1184 li->extLocation.logicalBlockNum = 0;
1185 li->extLocation.partitionReferenceNum = 0;
1186 li->extLength = (li->extLength &
1187 UDF_EXTENT_LENGTH_MASK) |
1188 EXT_NOT_RECORDED_NOT_ALLOCATED;
1193 static void udf_update_extents(struct inode *inode,
1194 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1195 int startnum, int endnum,
1196 struct extent_position *epos)
1199 struct kernel_lb_addr tmploc;
1202 if (startnum > endnum) {
1203 for (i = 0; i < (startnum - endnum); i++)
1204 udf_delete_aext(inode, *epos, laarr[i].extLocation,
1205 laarr[i].extLength);
1206 } else if (startnum < endnum) {
1207 for (i = 0; i < (endnum - startnum); i++) {
1208 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1209 laarr[i].extLength);
1210 udf_next_aext(inode, epos, &laarr[i].extLocation,
1211 &laarr[i].extLength, 1);
1216 for (i = start; i < endnum; i++) {
1217 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1218 udf_write_aext(inode, epos, &laarr[i].extLocation,
1219 laarr[i].extLength, 1);
1223 struct buffer_head *udf_bread(struct inode *inode, int block,
1224 int create, int *err)
1226 struct buffer_head *bh = NULL;
1228 bh = udf_getblk(inode, block, create, err);
1232 if (buffer_uptodate(bh))
1235 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1238 if (buffer_uptodate(bh))
1246 int udf_setsize(struct inode *inode, loff_t newsize)
1249 struct udf_inode_info *iinfo;
1250 int bsize = i_blocksize(inode);
1252 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1253 S_ISLNK(inode->i_mode)))
1255 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1258 iinfo = UDF_I(inode);
1259 if (newsize > inode->i_size) {
1260 down_write(&iinfo->i_data_sem);
1261 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1263 (udf_file_entry_alloc_offset(inode) + newsize)) {
1264 err = udf_expand_file_adinicb(inode);
1267 down_write(&iinfo->i_data_sem);
1269 iinfo->i_lenAlloc = newsize;
1273 err = udf_extend_file(inode, newsize);
1275 up_write(&iinfo->i_data_sem);
1279 up_write(&iinfo->i_data_sem);
1280 truncate_setsize(inode, newsize);
1282 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1283 down_write(&iinfo->i_data_sem);
1284 udf_clear_extent_cache(inode);
1285 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1286 0x00, bsize - newsize -
1287 udf_file_entry_alloc_offset(inode));
1288 iinfo->i_lenAlloc = newsize;
1289 truncate_setsize(inode, newsize);
1290 up_write(&iinfo->i_data_sem);
1293 err = block_truncate_page(inode->i_mapping, newsize,
1297 truncate_setsize(inode, newsize);
1298 down_write(&iinfo->i_data_sem);
1299 udf_clear_extent_cache(inode);
1300 udf_truncate_extents(inode);
1301 up_write(&iinfo->i_data_sem);
1304 inode->i_mtime = inode->i_ctime = current_time(inode);
1306 udf_sync_inode(inode);
1308 mark_inode_dirty(inode);
1313 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1314 * arbitrary - just that we hopefully don't limit any real use of rewritten
1315 * inode on write-once media but avoid looping for too long on corrupted media.
1317 #define UDF_MAX_ICB_NESTING 1024
1319 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1321 struct buffer_head *bh = NULL;
1322 struct fileEntry *fe;
1323 struct extendedFileEntry *efe;
1325 struct udf_inode_info *iinfo = UDF_I(inode);
1326 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1327 struct kernel_lb_addr *iloc = &iinfo->i_location;
1328 unsigned int link_count;
1329 unsigned int indirections = 0;
1330 int bs = inode->i_sb->s_blocksize;
1334 if (iloc->logicalBlockNum >=
1335 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1336 udf_debug("block=%d, partition=%d out of range\n",
1337 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1342 * Set defaults, but the inode is still incomplete!
1343 * Note: get_new_inode() sets the following on a new inode:
1346 * i_flags = sb->s_flags
1348 * clean_inode(): zero fills and sets
1353 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1355 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1359 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1360 ident != TAG_IDENT_USE) {
1361 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1362 inode->i_ino, ident);
1366 fe = (struct fileEntry *)bh->b_data;
1367 efe = (struct extendedFileEntry *)bh->b_data;
1369 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1370 struct buffer_head *ibh;
1372 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1373 if (ident == TAG_IDENT_IE && ibh) {
1374 struct kernel_lb_addr loc;
1375 struct indirectEntry *ie;
1377 ie = (struct indirectEntry *)ibh->b_data;
1378 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1380 if (ie->indirectICB.extLength) {
1382 memcpy(&iinfo->i_location, &loc,
1383 sizeof(struct kernel_lb_addr));
1384 if (++indirections > UDF_MAX_ICB_NESTING) {
1385 udf_err(inode->i_sb,
1386 "too many ICBs in ICB hierarchy"
1387 " (max %d supported)\n",
1388 UDF_MAX_ICB_NESTING);
1396 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1397 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1398 le16_to_cpu(fe->icbTag.strategyType));
1401 if (fe->icbTag.strategyType == cpu_to_le16(4))
1402 iinfo->i_strat4096 = 0;
1403 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1404 iinfo->i_strat4096 = 1;
1406 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1407 ICBTAG_FLAG_AD_MASK;
1408 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1409 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1410 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1414 iinfo->i_unique = 0;
1415 iinfo->i_lenEAttr = 0;
1416 iinfo->i_lenExtents = 0;
1417 iinfo->i_lenAlloc = 0;
1418 iinfo->i_next_alloc_block = 0;
1419 iinfo->i_next_alloc_goal = 0;
1420 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1423 ret = udf_alloc_i_data(inode, bs -
1424 sizeof(struct extendedFileEntry));
1427 memcpy(iinfo->i_ext.i_data,
1428 bh->b_data + sizeof(struct extendedFileEntry),
1429 bs - sizeof(struct extendedFileEntry));
1430 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1433 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1436 memcpy(iinfo->i_ext.i_data,
1437 bh->b_data + sizeof(struct fileEntry),
1438 bs - sizeof(struct fileEntry));
1439 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1442 iinfo->i_lenAlloc = le32_to_cpu(
1443 ((struct unallocSpaceEntry *)bh->b_data)->
1445 ret = udf_alloc_i_data(inode, bs -
1446 sizeof(struct unallocSpaceEntry));
1449 memcpy(iinfo->i_ext.i_data,
1450 bh->b_data + sizeof(struct unallocSpaceEntry),
1451 bs - sizeof(struct unallocSpaceEntry));
1456 read_lock(&sbi->s_cred_lock);
1457 i_uid_write(inode, le32_to_cpu(fe->uid));
1458 if (!uid_valid(inode->i_uid) ||
1459 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1460 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1461 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1463 i_gid_write(inode, le32_to_cpu(fe->gid));
1464 if (!gid_valid(inode->i_gid) ||
1465 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1466 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1467 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1469 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1470 sbi->s_fmode != UDF_INVALID_MODE)
1471 inode->i_mode = sbi->s_fmode;
1472 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1473 sbi->s_dmode != UDF_INVALID_MODE)
1474 inode->i_mode = sbi->s_dmode;
1476 inode->i_mode = udf_convert_permissions(fe);
1477 inode->i_mode &= ~sbi->s_umask;
1478 read_unlock(&sbi->s_cred_lock);
1480 link_count = le16_to_cpu(fe->fileLinkCount);
1482 if (!hidden_inode) {
1488 set_nlink(inode, link_count);
1490 inode->i_size = le64_to_cpu(fe->informationLength);
1491 iinfo->i_lenExtents = inode->i_size;
1493 if (iinfo->i_efe == 0) {
1494 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1495 (inode->i_sb->s_blocksize_bits - 9);
1497 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1498 inode->i_atime = sbi->s_record_time;
1500 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1501 fe->modificationTime))
1502 inode->i_mtime = sbi->s_record_time;
1504 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1505 inode->i_ctime = sbi->s_record_time;
1507 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1508 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1509 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1510 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1512 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1513 (inode->i_sb->s_blocksize_bits - 9);
1515 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1516 inode->i_atime = sbi->s_record_time;
1518 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1519 efe->modificationTime))
1520 inode->i_mtime = sbi->s_record_time;
1522 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1523 iinfo->i_crtime = sbi->s_record_time;
1525 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1526 inode->i_ctime = sbi->s_record_time;
1528 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1529 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1530 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1531 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1533 inode->i_generation = iinfo->i_unique;
1536 * Sanity check length of allocation descriptors and extended attrs to
1537 * avoid integer overflows
1539 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1541 /* Now do exact checks */
1542 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1544 /* Sanity checks for files in ICB so that we don't get confused later */
1545 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1547 * For file in ICB data is stored in allocation descriptor
1548 * so sizes should match
1550 if (iinfo->i_lenAlloc != inode->i_size)
1552 /* File in ICB has to fit in there... */
1553 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1557 switch (fe->icbTag.fileType) {
1558 case ICBTAG_FILE_TYPE_DIRECTORY:
1559 inode->i_op = &udf_dir_inode_operations;
1560 inode->i_fop = &udf_dir_operations;
1561 inode->i_mode |= S_IFDIR;
1564 case ICBTAG_FILE_TYPE_REALTIME:
1565 case ICBTAG_FILE_TYPE_REGULAR:
1566 case ICBTAG_FILE_TYPE_UNDEF:
1567 case ICBTAG_FILE_TYPE_VAT20:
1568 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1569 inode->i_data.a_ops = &udf_adinicb_aops;
1571 inode->i_data.a_ops = &udf_aops;
1572 inode->i_op = &udf_file_inode_operations;
1573 inode->i_fop = &udf_file_operations;
1574 inode->i_mode |= S_IFREG;
1576 case ICBTAG_FILE_TYPE_BLOCK:
1577 inode->i_mode |= S_IFBLK;
1579 case ICBTAG_FILE_TYPE_CHAR:
1580 inode->i_mode |= S_IFCHR;
1582 case ICBTAG_FILE_TYPE_FIFO:
1583 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1585 case ICBTAG_FILE_TYPE_SOCKET:
1586 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1588 case ICBTAG_FILE_TYPE_SYMLINK:
1589 inode->i_data.a_ops = &udf_symlink_aops;
1590 inode->i_op = &page_symlink_inode_operations;
1591 inode_nohighmem(inode);
1592 inode->i_mode = S_IFLNK | S_IRWXUGO;
1594 case ICBTAG_FILE_TYPE_MAIN:
1595 udf_debug("METADATA FILE-----\n");
1597 case ICBTAG_FILE_TYPE_MIRROR:
1598 udf_debug("METADATA MIRROR FILE-----\n");
1600 case ICBTAG_FILE_TYPE_BITMAP:
1601 udf_debug("METADATA BITMAP FILE-----\n");
1604 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1605 inode->i_ino, fe->icbTag.fileType);
1608 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1609 struct deviceSpec *dsea =
1610 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1612 init_special_inode(inode, inode->i_mode,
1613 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1614 le32_to_cpu(dsea->minorDeviceIdent)));
1615 /* Developer ID ??? */
1625 static int udf_alloc_i_data(struct inode *inode, size_t size)
1627 struct udf_inode_info *iinfo = UDF_I(inode);
1628 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1630 if (!iinfo->i_ext.i_data) {
1631 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1639 static umode_t udf_convert_permissions(struct fileEntry *fe)
1642 uint32_t permissions;
1645 permissions = le32_to_cpu(fe->permissions);
1646 flags = le16_to_cpu(fe->icbTag.flags);
1648 mode = ((permissions) & S_IRWXO) |
1649 ((permissions >> 2) & S_IRWXG) |
1650 ((permissions >> 4) & S_IRWXU) |
1651 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1652 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1653 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1658 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1660 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1663 static int udf_sync_inode(struct inode *inode)
1665 return udf_update_inode(inode, 1);
1668 static int udf_update_inode(struct inode *inode, int do_sync)
1670 struct buffer_head *bh = NULL;
1671 struct fileEntry *fe;
1672 struct extendedFileEntry *efe;
1673 uint64_t lb_recorded;
1678 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1679 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1680 struct udf_inode_info *iinfo = UDF_I(inode);
1682 bh = udf_tgetblk(inode->i_sb,
1683 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1685 udf_debug("getblk failure\n");
1690 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1691 fe = (struct fileEntry *)bh->b_data;
1692 efe = (struct extendedFileEntry *)bh->b_data;
1695 struct unallocSpaceEntry *use =
1696 (struct unallocSpaceEntry *)bh->b_data;
1698 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1699 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1700 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1701 sizeof(struct unallocSpaceEntry));
1702 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1703 crclen = sizeof(struct unallocSpaceEntry);
1708 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1709 fe->uid = cpu_to_le32(-1);
1711 fe->uid = cpu_to_le32(i_uid_read(inode));
1713 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1714 fe->gid = cpu_to_le32(-1);
1716 fe->gid = cpu_to_le32(i_gid_read(inode));
1718 udfperms = ((inode->i_mode & S_IRWXO)) |
1719 ((inode->i_mode & S_IRWXG) << 2) |
1720 ((inode->i_mode & S_IRWXU) << 4);
1722 udfperms |= (le32_to_cpu(fe->permissions) &
1723 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1724 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1725 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1726 fe->permissions = cpu_to_le32(udfperms);
1728 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1729 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1731 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1733 fe->informationLength = cpu_to_le64(inode->i_size);
1735 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1737 struct deviceSpec *dsea =
1738 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1740 dsea = (struct deviceSpec *)
1741 udf_add_extendedattr(inode,
1742 sizeof(struct deviceSpec) +
1743 sizeof(struct regid), 12, 0x3);
1744 dsea->attrType = cpu_to_le32(12);
1745 dsea->attrSubtype = 1;
1746 dsea->attrLength = cpu_to_le32(
1747 sizeof(struct deviceSpec) +
1748 sizeof(struct regid));
1749 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1751 eid = (struct regid *)dsea->impUse;
1752 memset(eid, 0, sizeof(struct regid));
1753 strcpy(eid->ident, UDF_ID_DEVELOPER);
1754 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1755 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1756 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1757 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1760 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1761 lb_recorded = 0; /* No extents => no blocks! */
1764 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1765 (blocksize_bits - 9);
1767 if (iinfo->i_efe == 0) {
1768 memcpy(bh->b_data + sizeof(struct fileEntry),
1769 iinfo->i_ext.i_data,
1770 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1771 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1773 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1774 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1775 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1776 memset(&(fe->impIdent), 0, sizeof(struct regid));
1777 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1778 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1779 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1780 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1781 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1782 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1783 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1784 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1785 crclen = sizeof(struct fileEntry);
1787 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1788 iinfo->i_ext.i_data,
1789 inode->i_sb->s_blocksize -
1790 sizeof(struct extendedFileEntry));
1791 efe->objectSize = cpu_to_le64(inode->i_size);
1792 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1794 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1795 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1796 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1797 iinfo->i_crtime = inode->i_atime;
1799 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1800 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1801 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1802 iinfo->i_crtime = inode->i_mtime;
1804 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1805 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1806 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1807 iinfo->i_crtime = inode->i_ctime;
1809 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1810 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1811 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1812 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1814 memset(&(efe->impIdent), 0, sizeof(struct regid));
1815 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1816 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1817 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1818 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1819 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1820 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1821 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1822 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1823 crclen = sizeof(struct extendedFileEntry);
1827 if (iinfo->i_strat4096) {
1828 fe->icbTag.strategyType = cpu_to_le16(4096);
1829 fe->icbTag.strategyParameter = cpu_to_le16(1);
1830 fe->icbTag.numEntries = cpu_to_le16(2);
1832 fe->icbTag.strategyType = cpu_to_le16(4);
1833 fe->icbTag.numEntries = cpu_to_le16(1);
1837 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1838 else if (S_ISDIR(inode->i_mode))
1839 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1840 else if (S_ISREG(inode->i_mode))
1841 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1842 else if (S_ISLNK(inode->i_mode))
1843 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1844 else if (S_ISBLK(inode->i_mode))
1845 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1846 else if (S_ISCHR(inode->i_mode))
1847 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1848 else if (S_ISFIFO(inode->i_mode))
1849 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1850 else if (S_ISSOCK(inode->i_mode))
1851 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1853 icbflags = iinfo->i_alloc_type |
1854 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1855 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1856 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1857 (le16_to_cpu(fe->icbTag.flags) &
1858 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1859 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1861 fe->icbTag.flags = cpu_to_le16(icbflags);
1862 if (sbi->s_udfrev >= 0x0200)
1863 fe->descTag.descVersion = cpu_to_le16(3);
1865 fe->descTag.descVersion = cpu_to_le16(2);
1866 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1867 fe->descTag.tagLocation = cpu_to_le32(
1868 iinfo->i_location.logicalBlockNum);
1869 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1870 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1871 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1873 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1875 set_buffer_uptodate(bh);
1878 /* write the data blocks */
1879 mark_buffer_dirty(bh);
1881 sync_dirty_buffer(bh);
1882 if (buffer_write_io_error(bh)) {
1883 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1893 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1896 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1897 struct inode *inode = iget_locked(sb, block);
1901 return ERR_PTR(-ENOMEM);
1903 if (!(inode->i_state & I_NEW))
1906 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1907 err = udf_read_inode(inode, hidden_inode);
1910 return ERR_PTR(err);
1912 unlock_new_inode(inode);
1917 int udf_setup_indirect_aext(struct inode *inode, int block,
1918 struct extent_position *epos)
1920 struct super_block *sb = inode->i_sb;
1921 struct buffer_head *bh;
1922 struct allocExtDesc *aed;
1923 struct extent_position nepos;
1924 struct kernel_lb_addr neloc;
1927 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1928 adsize = sizeof(struct short_ad);
1929 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1930 adsize = sizeof(struct long_ad);
1934 neloc.logicalBlockNum = block;
1935 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1937 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1941 memset(bh->b_data, 0x00, sb->s_blocksize);
1942 set_buffer_uptodate(bh);
1944 mark_buffer_dirty_inode(bh, inode);
1946 aed = (struct allocExtDesc *)(bh->b_data);
1947 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1948 aed->previousAllocExtLocation =
1949 cpu_to_le32(epos->block.logicalBlockNum);
1951 aed->lengthAllocDescs = cpu_to_le32(0);
1952 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1956 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1957 sizeof(struct tag));
1959 nepos.block = neloc;
1960 nepos.offset = sizeof(struct allocExtDesc);
1964 * Do we have to copy current last extent to make space for indirect
1967 if (epos->offset + adsize > sb->s_blocksize) {
1968 struct kernel_lb_addr cp_loc;
1972 epos->offset -= adsize;
1973 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1974 cp_len |= ((uint32_t)cp_type) << 30;
1976 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1977 udf_write_aext(inode, epos, &nepos.block,
1978 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1980 __udf_add_aext(inode, epos, &nepos.block,
1981 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1991 * Append extent at the given position - should be the first free one in inode
1992 * / indirect extent. This function assumes there is enough space in the inode
1993 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1995 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1996 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1998 struct udf_inode_info *iinfo = UDF_I(inode);
1999 struct allocExtDesc *aed;
2002 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2003 adsize = sizeof(struct short_ad);
2004 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2005 adsize = sizeof(struct long_ad);
2010 WARN_ON(iinfo->i_lenAlloc !=
2011 epos->offset - udf_file_entry_alloc_offset(inode));
2013 aed = (struct allocExtDesc *)epos->bh->b_data;
2014 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
2015 epos->offset - sizeof(struct allocExtDesc));
2016 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
2019 udf_write_aext(inode, epos, eloc, elen, inc);
2022 iinfo->i_lenAlloc += adsize;
2023 mark_inode_dirty(inode);
2025 aed = (struct allocExtDesc *)epos->bh->b_data;
2026 le32_add_cpu(&aed->lengthAllocDescs, adsize);
2027 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2028 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2029 udf_update_tag(epos->bh->b_data,
2030 epos->offset + (inc ? 0 : adsize));
2032 udf_update_tag(epos->bh->b_data,
2033 sizeof(struct allocExtDesc));
2034 mark_buffer_dirty_inode(epos->bh, inode);
2041 * Append extent at given position - should be the first free one in inode
2042 * / indirect extent. Takes care of allocating and linking indirect blocks.
2044 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2045 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2048 struct super_block *sb = inode->i_sb;
2050 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2051 adsize = sizeof(struct short_ad);
2052 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2053 adsize = sizeof(struct long_ad);
2057 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2061 new_block = udf_new_block(sb, NULL,
2062 epos->block.partitionReferenceNum,
2063 epos->block.logicalBlockNum, &err);
2067 err = udf_setup_indirect_aext(inode, new_block, epos);
2072 return __udf_add_aext(inode, epos, eloc, elen, inc);
2075 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2076 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2080 struct short_ad *sad;
2081 struct long_ad *lad;
2082 struct udf_inode_info *iinfo = UDF_I(inode);
2085 ptr = iinfo->i_ext.i_data + epos->offset -
2086 udf_file_entry_alloc_offset(inode) +
2089 ptr = epos->bh->b_data + epos->offset;
2091 switch (iinfo->i_alloc_type) {
2092 case ICBTAG_FLAG_AD_SHORT:
2093 sad = (struct short_ad *)ptr;
2094 sad->extLength = cpu_to_le32(elen);
2095 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2096 adsize = sizeof(struct short_ad);
2098 case ICBTAG_FLAG_AD_LONG:
2099 lad = (struct long_ad *)ptr;
2100 lad->extLength = cpu_to_le32(elen);
2101 lad->extLocation = cpu_to_lelb(*eloc);
2102 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2103 adsize = sizeof(struct long_ad);
2110 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2111 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2112 struct allocExtDesc *aed =
2113 (struct allocExtDesc *)epos->bh->b_data;
2114 udf_update_tag(epos->bh->b_data,
2115 le32_to_cpu(aed->lengthAllocDescs) +
2116 sizeof(struct allocExtDesc));
2118 mark_buffer_dirty_inode(epos->bh, inode);
2120 mark_inode_dirty(inode);
2124 epos->offset += adsize;
2128 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2129 * someone does some weird stuff.
2131 #define UDF_MAX_INDIR_EXTS 16
2133 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2134 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2137 unsigned int indirections = 0;
2139 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2140 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2143 if (++indirections > UDF_MAX_INDIR_EXTS) {
2144 udf_err(inode->i_sb,
2145 "too many indirect extents in inode %lu\n",
2150 epos->block = *eloc;
2151 epos->offset = sizeof(struct allocExtDesc);
2153 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2154 epos->bh = udf_tread(inode->i_sb, block);
2156 udf_debug("reading block %d failed!\n", block);
2164 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2165 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2170 struct short_ad *sad;
2171 struct long_ad *lad;
2172 struct udf_inode_info *iinfo = UDF_I(inode);
2176 epos->offset = udf_file_entry_alloc_offset(inode);
2177 ptr = iinfo->i_ext.i_data + epos->offset -
2178 udf_file_entry_alloc_offset(inode) +
2180 alen = udf_file_entry_alloc_offset(inode) +
2184 epos->offset = sizeof(struct allocExtDesc);
2185 ptr = epos->bh->b_data + epos->offset;
2186 alen = sizeof(struct allocExtDesc) +
2187 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2191 switch (iinfo->i_alloc_type) {
2192 case ICBTAG_FLAG_AD_SHORT:
2193 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2196 etype = le32_to_cpu(sad->extLength) >> 30;
2197 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2198 eloc->partitionReferenceNum =
2199 iinfo->i_location.partitionReferenceNum;
2200 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2202 case ICBTAG_FLAG_AD_LONG:
2203 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2206 etype = le32_to_cpu(lad->extLength) >> 30;
2207 *eloc = lelb_to_cpu(lad->extLocation);
2208 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2211 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2218 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2219 struct kernel_lb_addr neloc, uint32_t nelen)
2221 struct kernel_lb_addr oeloc;
2228 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2229 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2231 nelen = (etype << 30) | oelen;
2233 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2236 return (nelen >> 30);
2239 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2240 struct kernel_lb_addr eloc, uint32_t elen)
2242 struct extent_position oepos;
2245 struct allocExtDesc *aed;
2246 struct udf_inode_info *iinfo;
2253 iinfo = UDF_I(inode);
2254 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2255 adsize = sizeof(struct short_ad);
2256 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2257 adsize = sizeof(struct long_ad);
2262 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2265 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2266 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2267 if (oepos.bh != epos.bh) {
2268 oepos.block = epos.block;
2272 oepos.offset = epos.offset - adsize;
2275 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2278 if (epos.bh != oepos.bh) {
2279 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2280 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2281 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2283 iinfo->i_lenAlloc -= (adsize * 2);
2284 mark_inode_dirty(inode);
2286 aed = (struct allocExtDesc *)oepos.bh->b_data;
2287 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2288 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2289 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2290 udf_update_tag(oepos.bh->b_data,
2291 oepos.offset - (2 * adsize));
2293 udf_update_tag(oepos.bh->b_data,
2294 sizeof(struct allocExtDesc));
2295 mark_buffer_dirty_inode(oepos.bh, inode);
2298 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2300 iinfo->i_lenAlloc -= adsize;
2301 mark_inode_dirty(inode);
2303 aed = (struct allocExtDesc *)oepos.bh->b_data;
2304 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2305 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2306 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2307 udf_update_tag(oepos.bh->b_data,
2308 epos.offset - adsize);
2310 udf_update_tag(oepos.bh->b_data,
2311 sizeof(struct allocExtDesc));
2312 mark_buffer_dirty_inode(oepos.bh, inode);
2319 return (elen >> 30);
2322 int8_t inode_bmap(struct inode *inode, sector_t block,
2323 struct extent_position *pos, struct kernel_lb_addr *eloc,
2324 uint32_t *elen, sector_t *offset)
2326 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2327 loff_t lbcount = 0, bcount =
2328 (loff_t) block << blocksize_bits;
2330 struct udf_inode_info *iinfo;
2332 iinfo = UDF_I(inode);
2333 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2335 pos->block = iinfo->i_location;
2340 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2342 *offset = (bcount - lbcount) >> blocksize_bits;
2343 iinfo->i_lenExtents = lbcount;
2347 } while (lbcount <= bcount);
2348 /* update extent cache */
2349 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2350 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2355 long udf_block_map(struct inode *inode, sector_t block)
2357 struct kernel_lb_addr eloc;
2360 struct extent_position epos = {};
2363 down_read(&UDF_I(inode)->i_data_sem);
2365 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2366 (EXT_RECORDED_ALLOCATED >> 30))
2367 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2371 up_read(&UDF_I(inode)->i_data_sem);
2374 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2375 return udf_fixed_to_variable(ret);