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,
223 struct file *file = iocb->ki_filp;
224 struct address_space *mapping = file->f_mapping;
225 struct inode *inode = mapping->host;
226 size_t count = iov_iter_count(iter);
229 ret = blockdev_direct_IO(iocb, inode, iter, offset, udf_get_block);
230 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
231 udf_write_failed(mapping, offset + count);
235 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
237 return generic_block_bmap(mapping, block, udf_get_block);
240 const struct address_space_operations udf_aops = {
241 .readpage = udf_readpage,
242 .readpages = udf_readpages,
243 .writepage = udf_writepage,
244 .writepages = udf_writepages,
245 .write_begin = udf_write_begin,
246 .write_end = generic_write_end,
247 .direct_IO = udf_direct_IO,
252 * Expand file stored in ICB to a normal one-block-file
254 * This function requires i_data_sem for writing and releases it.
255 * This function requires i_mutex held
257 int udf_expand_file_adinicb(struct inode *inode)
261 struct udf_inode_info *iinfo = UDF_I(inode);
263 struct writeback_control udf_wbc = {
264 .sync_mode = WB_SYNC_NONE,
268 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
269 if (!iinfo->i_lenAlloc) {
270 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
271 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
273 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
274 /* from now on we have normal address_space methods */
275 inode->i_data.a_ops = &udf_aops;
276 up_write(&iinfo->i_data_sem);
277 mark_inode_dirty(inode);
281 * Release i_data_sem so that we can lock a page - page lock ranks
282 * above i_data_sem. i_mutex still protects us against file changes.
284 up_write(&iinfo->i_data_sem);
286 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
290 if (!PageUptodate(page)) {
292 memset(kaddr + iinfo->i_lenAlloc, 0x00,
293 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
294 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
296 flush_dcache_page(page);
297 SetPageUptodate(page);
300 down_write(&iinfo->i_data_sem);
301 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
303 iinfo->i_lenAlloc = 0;
304 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
305 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
307 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
308 /* from now on we have normal address_space methods */
309 inode->i_data.a_ops = &udf_aops;
310 up_write(&iinfo->i_data_sem);
311 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
313 /* Restore everything back so that we don't lose data... */
316 down_write(&iinfo->i_data_sem);
317 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
321 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
322 inode->i_data.a_ops = &udf_adinicb_aops;
323 up_write(&iinfo->i_data_sem);
325 page_cache_release(page);
326 mark_inode_dirty(inode);
331 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
335 struct buffer_head *dbh = NULL;
336 struct kernel_lb_addr eloc;
338 struct extent_position epos;
340 struct udf_fileident_bh sfibh, dfibh;
341 loff_t f_pos = udf_ext0_offset(inode);
342 int size = udf_ext0_offset(inode) + inode->i_size;
343 struct fileIdentDesc cfi, *sfi, *dfi;
344 struct udf_inode_info *iinfo = UDF_I(inode);
346 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
347 alloctype = ICBTAG_FLAG_AD_SHORT;
349 alloctype = ICBTAG_FLAG_AD_LONG;
351 if (!inode->i_size) {
352 iinfo->i_alloc_type = alloctype;
353 mark_inode_dirty(inode);
357 /* alloc block, and copy data to it */
358 *block = udf_new_block(inode->i_sb, inode,
359 iinfo->i_location.partitionReferenceNum,
360 iinfo->i_location.logicalBlockNum, err);
363 newblock = udf_get_pblock(inode->i_sb, *block,
364 iinfo->i_location.partitionReferenceNum,
368 dbh = udf_tgetblk(inode->i_sb, newblock);
372 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
373 set_buffer_uptodate(dbh);
375 mark_buffer_dirty_inode(dbh, inode);
377 sfibh.soffset = sfibh.eoffset =
378 f_pos & (inode->i_sb->s_blocksize - 1);
379 sfibh.sbh = sfibh.ebh = NULL;
380 dfibh.soffset = dfibh.eoffset = 0;
381 dfibh.sbh = dfibh.ebh = dbh;
382 while (f_pos < size) {
383 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
384 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
390 iinfo->i_alloc_type = alloctype;
391 sfi->descTag.tagLocation = cpu_to_le32(*block);
392 dfibh.soffset = dfibh.eoffset;
393 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
394 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
395 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
397 le16_to_cpu(sfi->lengthOfImpUse))) {
398 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
403 mark_buffer_dirty_inode(dbh, inode);
405 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
407 iinfo->i_lenAlloc = 0;
408 eloc.logicalBlockNum = *block;
409 eloc.partitionReferenceNum =
410 iinfo->i_location.partitionReferenceNum;
411 iinfo->i_lenExtents = inode->i_size;
413 epos.block = iinfo->i_location;
414 epos.offset = udf_file_entry_alloc_offset(inode);
415 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
419 mark_inode_dirty(inode);
423 static int udf_get_block(struct inode *inode, sector_t block,
424 struct buffer_head *bh_result, int create)
428 struct udf_inode_info *iinfo;
431 phys = udf_block_map(inode, block);
433 map_bh(bh_result, inode->i_sb, phys);
439 iinfo = UDF_I(inode);
441 down_write(&iinfo->i_data_sem);
442 if (block == iinfo->i_next_alloc_block + 1) {
443 iinfo->i_next_alloc_block++;
444 iinfo->i_next_alloc_goal++;
447 udf_clear_extent_cache(inode);
448 phys = inode_getblk(inode, block, &err, &new);
453 set_buffer_new(bh_result);
454 map_bh(bh_result, inode->i_sb, phys);
457 up_write(&iinfo->i_data_sem);
461 static struct buffer_head *udf_getblk(struct inode *inode, long block,
462 int create, int *err)
464 struct buffer_head *bh;
465 struct buffer_head dummy;
468 dummy.b_blocknr = -1000;
469 *err = udf_get_block(inode, block, &dummy, create);
470 if (!*err && buffer_mapped(&dummy)) {
471 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
472 if (buffer_new(&dummy)) {
474 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
475 set_buffer_uptodate(bh);
477 mark_buffer_dirty_inode(bh, inode);
485 /* Extend the file with new blocks totaling 'new_block_bytes',
486 * return the number of extents added
488 static int udf_do_extend_file(struct inode *inode,
489 struct extent_position *last_pos,
490 struct kernel_long_ad *last_ext,
491 loff_t new_block_bytes)
494 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
495 struct super_block *sb = inode->i_sb;
496 struct kernel_lb_addr prealloc_loc = {};
497 int prealloc_len = 0;
498 struct udf_inode_info *iinfo;
501 /* The previous extent is fake and we should not extend by anything
502 * - there's nothing to do... */
503 if (!new_block_bytes && fake)
506 iinfo = UDF_I(inode);
507 /* Round the last extent up to a multiple of block size */
508 if (last_ext->extLength & (sb->s_blocksize - 1)) {
509 last_ext->extLength =
510 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
511 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
512 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
513 iinfo->i_lenExtents =
514 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
515 ~(sb->s_blocksize - 1);
518 /* Last extent are just preallocated blocks? */
519 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
520 EXT_NOT_RECORDED_ALLOCATED) {
521 /* Save the extent so that we can reattach it to the end */
522 prealloc_loc = last_ext->extLocation;
523 prealloc_len = last_ext->extLength;
524 /* Mark the extent as a hole */
525 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
526 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
527 last_ext->extLocation.logicalBlockNum = 0;
528 last_ext->extLocation.partitionReferenceNum = 0;
531 /* Can we merge with the previous extent? */
532 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
533 EXT_NOT_RECORDED_NOT_ALLOCATED) {
534 add = (1 << 30) - sb->s_blocksize -
535 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
536 if (add > new_block_bytes)
537 add = new_block_bytes;
538 new_block_bytes -= add;
539 last_ext->extLength += add;
543 udf_add_aext(inode, last_pos, &last_ext->extLocation,
544 last_ext->extLength, 1);
547 udf_write_aext(inode, last_pos, &last_ext->extLocation,
548 last_ext->extLength, 1);
550 /* Managed to do everything necessary? */
551 if (!new_block_bytes)
554 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
555 last_ext->extLocation.logicalBlockNum = 0;
556 last_ext->extLocation.partitionReferenceNum = 0;
557 add = (1 << 30) - sb->s_blocksize;
558 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
560 /* Create enough extents to cover the whole hole */
561 while (new_block_bytes > add) {
562 new_block_bytes -= add;
563 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
564 last_ext->extLength, 1);
569 if (new_block_bytes) {
570 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
572 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
573 last_ext->extLength, 1);
580 /* Do we have some preallocated blocks saved? */
582 err = udf_add_aext(inode, last_pos, &prealloc_loc,
586 last_ext->extLocation = prealloc_loc;
587 last_ext->extLength = prealloc_len;
591 /* last_pos should point to the last written extent... */
592 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
593 last_pos->offset -= sizeof(struct short_ad);
594 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
595 last_pos->offset -= sizeof(struct long_ad);
602 /* Extend the final block of the file to final_block_len bytes */
603 static void udf_do_extend_final_block(struct inode *inode,
604 struct extent_position *last_pos,
605 struct kernel_long_ad *last_ext,
606 uint32_t final_block_len)
608 struct super_block *sb = inode->i_sb;
609 uint32_t added_bytes;
611 added_bytes = final_block_len -
612 (last_ext->extLength & (sb->s_blocksize - 1));
613 last_ext->extLength += added_bytes;
614 UDF_I(inode)->i_lenExtents += added_bytes;
616 udf_write_aext(inode, last_pos, &last_ext->extLocation,
617 last_ext->extLength, 1);
620 static int udf_extend_file(struct inode *inode, loff_t newsize)
623 struct extent_position epos;
624 struct kernel_lb_addr eloc;
627 struct super_block *sb = inode->i_sb;
628 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
629 unsigned long partial_final_block;
631 struct udf_inode_info *iinfo = UDF_I(inode);
632 struct kernel_long_ad extent;
634 int within_final_block;
636 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
637 adsize = sizeof(struct short_ad);
638 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
639 adsize = sizeof(struct long_ad);
643 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
644 within_final_block = (etype != -1);
646 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
647 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
648 /* File has no extents at all or has empty last
649 * indirect extent! Create a fake extent... */
650 extent.extLocation.logicalBlockNum = 0;
651 extent.extLocation.partitionReferenceNum = 0;
652 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
654 epos.offset -= adsize;
655 etype = udf_next_aext(inode, &epos, &extent.extLocation,
656 &extent.extLength, 0);
657 extent.extLength |= etype << 30;
660 partial_final_block = newsize & (sb->s_blocksize - 1);
662 /* File has extent covering the new size (could happen when extending
665 if (within_final_block) {
666 /* Extending file within the last file block */
667 udf_do_extend_final_block(inode, &epos, &extent,
668 partial_final_block);
670 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
672 err = udf_do_extend_file(inode, &epos, &extent, add);
678 iinfo->i_lenExtents = newsize;
684 static sector_t inode_getblk(struct inode *inode, sector_t block,
687 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
688 struct extent_position prev_epos, cur_epos, next_epos;
689 int count = 0, startnum = 0, endnum = 0;
690 uint32_t elen = 0, tmpelen;
691 struct kernel_lb_addr eloc, tmpeloc;
693 loff_t lbcount = 0, b_off = 0;
694 uint32_t newblocknum, newblock;
697 struct udf_inode_info *iinfo = UDF_I(inode);
698 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
704 prev_epos.offset = udf_file_entry_alloc_offset(inode);
705 prev_epos.block = iinfo->i_location;
707 cur_epos = next_epos = prev_epos;
708 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
710 /* find the extent which contains the block we are looking for.
711 alternate between laarr[0] and laarr[1] for locations of the
712 current extent, and the previous extent */
714 if (prev_epos.bh != cur_epos.bh) {
715 brelse(prev_epos.bh);
717 prev_epos.bh = cur_epos.bh;
719 if (cur_epos.bh != next_epos.bh) {
721 get_bh(next_epos.bh);
722 cur_epos.bh = next_epos.bh;
727 prev_epos.block = cur_epos.block;
728 cur_epos.block = next_epos.block;
730 prev_epos.offset = cur_epos.offset;
731 cur_epos.offset = next_epos.offset;
733 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
739 laarr[c].extLength = (etype << 30) | elen;
740 laarr[c].extLocation = eloc;
742 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
743 pgoal = eloc.logicalBlockNum +
744 ((elen + inode->i_sb->s_blocksize - 1) >>
745 inode->i_sb->s_blocksize_bits);
748 } while (lbcount + elen <= b_off);
751 offset = b_off >> inode->i_sb->s_blocksize_bits;
753 * Move prev_epos and cur_epos into indirect extent if we are at
756 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
757 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
759 /* if the extent is allocated and recorded, return the block
760 if the extent is not a multiple of the blocksize, round up */
762 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
763 if (elen & (inode->i_sb->s_blocksize - 1)) {
764 elen = EXT_RECORDED_ALLOCATED |
765 ((elen + inode->i_sb->s_blocksize - 1) &
766 ~(inode->i_sb->s_blocksize - 1));
767 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
769 brelse(prev_epos.bh);
771 brelse(next_epos.bh);
772 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
776 /* Are we beyond EOF? */
786 /* Create a fake extent when there's not one */
787 memset(&laarr[0].extLocation, 0x00,
788 sizeof(struct kernel_lb_addr));
789 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
790 /* Will udf_do_extend_file() create real extent from
792 startnum = (offset > 0);
794 /* Create extents for the hole between EOF and offset */
795 hole_len = (loff_t)offset << inode->i_blkbits;
796 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
798 brelse(prev_epos.bh);
800 brelse(next_epos.bh);
807 /* We are not covered by a preallocated extent? */
808 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
809 EXT_NOT_RECORDED_ALLOCATED) {
810 /* Is there any real extent? - otherwise we overwrite
814 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
815 inode->i_sb->s_blocksize;
816 memset(&laarr[c].extLocation, 0x00,
817 sizeof(struct kernel_lb_addr));
824 endnum = startnum = ((count > 2) ? 2 : count);
826 /* if the current extent is in position 0,
827 swap it with the previous */
828 if (!c && count != 1) {
835 /* if the current block is located in an extent,
836 read the next extent */
837 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
839 laarr[c + 1].extLength = (etype << 30) | elen;
840 laarr[c + 1].extLocation = eloc;
848 /* if the current extent is not recorded but allocated, get the
849 * block in the extent corresponding to the requested block */
850 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
851 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
852 else { /* otherwise, allocate a new block */
853 if (iinfo->i_next_alloc_block == block)
854 goal = iinfo->i_next_alloc_goal;
857 if (!(goal = pgoal)) /* XXX: what was intended here? */
858 goal = iinfo->i_location.logicalBlockNum + 1;
861 newblocknum = udf_new_block(inode->i_sb, inode,
862 iinfo->i_location.partitionReferenceNum,
865 brelse(prev_epos.bh);
867 brelse(next_epos.bh);
872 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
875 /* if the extent the requsted block is located in contains multiple
876 * blocks, split the extent into at most three extents. blocks prior
877 * to requested block, requested block, and blocks after requested
879 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
881 #ifdef UDF_PREALLOCATE
882 /* We preallocate blocks only for regular files. It also makes sense
883 * for directories but there's a problem when to drop the
884 * preallocation. We might use some delayed work for that but I feel
885 * it's overengineering for a filesystem like UDF. */
886 if (S_ISREG(inode->i_mode))
887 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
890 /* merge any continuous blocks in laarr */
891 udf_merge_extents(inode, laarr, &endnum);
893 /* write back the new extents, inserting new extents if the new number
894 * of extents is greater than the old number, and deleting extents if
895 * the new number of extents is less than the old number */
896 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
898 brelse(prev_epos.bh);
900 brelse(next_epos.bh);
902 newblock = udf_get_pblock(inode->i_sb, newblocknum,
903 iinfo->i_location.partitionReferenceNum, 0);
909 iinfo->i_next_alloc_block = block;
910 iinfo->i_next_alloc_goal = newblocknum;
911 inode->i_ctime = current_fs_time(inode->i_sb);
914 udf_sync_inode(inode);
916 mark_inode_dirty(inode);
921 static void udf_split_extents(struct inode *inode, int *c, int offset,
923 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
926 unsigned long blocksize = inode->i_sb->s_blocksize;
927 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
929 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
930 (laarr[*c].extLength >> 30) ==
931 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
933 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
934 blocksize - 1) >> blocksize_bits;
935 int8_t etype = (laarr[curr].extLength >> 30);
939 else if (!offset || blen == offset + 1) {
940 laarr[curr + 2] = laarr[curr + 1];
941 laarr[curr + 1] = laarr[curr];
943 laarr[curr + 3] = laarr[curr + 1];
944 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
948 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
949 udf_free_blocks(inode->i_sb, inode,
950 &laarr[curr].extLocation,
952 laarr[curr].extLength =
953 EXT_NOT_RECORDED_NOT_ALLOCATED |
954 (offset << blocksize_bits);
955 laarr[curr].extLocation.logicalBlockNum = 0;
956 laarr[curr].extLocation.
957 partitionReferenceNum = 0;
959 laarr[curr].extLength = (etype << 30) |
960 (offset << blocksize_bits);
966 laarr[curr].extLocation.logicalBlockNum = newblocknum;
967 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
968 laarr[curr].extLocation.partitionReferenceNum =
969 UDF_I(inode)->i_location.partitionReferenceNum;
970 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
974 if (blen != offset + 1) {
975 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
976 laarr[curr].extLocation.logicalBlockNum +=
978 laarr[curr].extLength = (etype << 30) |
979 ((blen - (offset + 1)) << blocksize_bits);
986 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
987 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
990 int start, length = 0, currlength = 0, i;
992 if (*endnum >= (c + 1)) {
998 if ((laarr[c + 1].extLength >> 30) ==
999 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1001 length = currlength =
1002 (((laarr[c + 1].extLength &
1003 UDF_EXTENT_LENGTH_MASK) +
1004 inode->i_sb->s_blocksize - 1) >>
1005 inode->i_sb->s_blocksize_bits);
1010 for (i = start + 1; i <= *endnum; i++) {
1013 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1014 } else if ((laarr[i].extLength >> 30) ==
1015 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1016 length += (((laarr[i].extLength &
1017 UDF_EXTENT_LENGTH_MASK) +
1018 inode->i_sb->s_blocksize - 1) >>
1019 inode->i_sb->s_blocksize_bits);
1025 int next = laarr[start].extLocation.logicalBlockNum +
1026 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1027 inode->i_sb->s_blocksize - 1) >>
1028 inode->i_sb->s_blocksize_bits);
1029 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1030 laarr[start].extLocation.partitionReferenceNum,
1031 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1032 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1035 if (start == (c + 1))
1036 laarr[start].extLength +=
1038 inode->i_sb->s_blocksize_bits);
1040 memmove(&laarr[c + 2], &laarr[c + 1],
1041 sizeof(struct long_ad) * (*endnum - (c + 1)));
1043 laarr[c + 1].extLocation.logicalBlockNum = next;
1044 laarr[c + 1].extLocation.partitionReferenceNum =
1045 laarr[c].extLocation.
1046 partitionReferenceNum;
1047 laarr[c + 1].extLength =
1048 EXT_NOT_RECORDED_ALLOCATED |
1050 inode->i_sb->s_blocksize_bits);
1054 for (i = start + 1; numalloc && i < *endnum; i++) {
1055 int elen = ((laarr[i].extLength &
1056 UDF_EXTENT_LENGTH_MASK) +
1057 inode->i_sb->s_blocksize - 1) >>
1058 inode->i_sb->s_blocksize_bits;
1060 if (elen > numalloc) {
1061 laarr[i].extLength -=
1063 inode->i_sb->s_blocksize_bits);
1067 if (*endnum > (i + 1))
1070 sizeof(struct long_ad) *
1071 (*endnum - (i + 1)));
1076 UDF_I(inode)->i_lenExtents +=
1077 numalloc << inode->i_sb->s_blocksize_bits;
1082 static void udf_merge_extents(struct inode *inode,
1083 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1087 unsigned long blocksize = inode->i_sb->s_blocksize;
1088 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1090 for (i = 0; i < (*endnum - 1); i++) {
1091 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1092 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1094 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1095 (((li->extLength >> 30) ==
1096 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1097 ((lip1->extLocation.logicalBlockNum -
1098 li->extLocation.logicalBlockNum) ==
1099 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1100 blocksize - 1) >> blocksize_bits)))) {
1102 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1103 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1104 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1105 lip1->extLength = (lip1->extLength -
1107 UDF_EXTENT_LENGTH_MASK) +
1108 UDF_EXTENT_LENGTH_MASK) &
1110 li->extLength = (li->extLength &
1111 UDF_EXTENT_FLAG_MASK) +
1112 (UDF_EXTENT_LENGTH_MASK + 1) -
1114 lip1->extLocation.logicalBlockNum =
1115 li->extLocation.logicalBlockNum +
1117 UDF_EXTENT_LENGTH_MASK) >>
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 ((lip1->extLength >> 30) ==
1134 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1135 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1137 UDF_EXTENT_LENGTH_MASK) +
1138 blocksize - 1) >> blocksize_bits);
1139 li->extLocation.logicalBlockNum = 0;
1140 li->extLocation.partitionReferenceNum = 0;
1142 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1143 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1144 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1145 lip1->extLength = (lip1->extLength -
1147 UDF_EXTENT_LENGTH_MASK) +
1148 UDF_EXTENT_LENGTH_MASK) &
1150 li->extLength = (li->extLength &
1151 UDF_EXTENT_FLAG_MASK) +
1152 (UDF_EXTENT_LENGTH_MASK + 1) -
1155 li->extLength = lip1->extLength +
1157 UDF_EXTENT_LENGTH_MASK) +
1158 blocksize - 1) & ~(blocksize - 1));
1159 if (*endnum > (i + 2))
1160 memmove(&laarr[i + 1], &laarr[i + 2],
1161 sizeof(struct long_ad) *
1162 (*endnum - (i + 2)));
1166 } else if ((li->extLength >> 30) ==
1167 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1168 udf_free_blocks(inode->i_sb, inode,
1169 &li->extLocation, 0,
1171 UDF_EXTENT_LENGTH_MASK) +
1172 blocksize - 1) >> blocksize_bits);
1173 li->extLocation.logicalBlockNum = 0;
1174 li->extLocation.partitionReferenceNum = 0;
1175 li->extLength = (li->extLength &
1176 UDF_EXTENT_LENGTH_MASK) |
1177 EXT_NOT_RECORDED_NOT_ALLOCATED;
1182 static void udf_update_extents(struct inode *inode,
1183 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1184 int startnum, int endnum,
1185 struct extent_position *epos)
1188 struct kernel_lb_addr tmploc;
1191 if (startnum > endnum) {
1192 for (i = 0; i < (startnum - endnum); i++)
1193 udf_delete_aext(inode, *epos, laarr[i].extLocation,
1194 laarr[i].extLength);
1195 } else if (startnum < endnum) {
1196 for (i = 0; i < (endnum - startnum); i++) {
1197 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1198 laarr[i].extLength);
1199 udf_next_aext(inode, epos, &laarr[i].extLocation,
1200 &laarr[i].extLength, 1);
1205 for (i = start; i < endnum; i++) {
1206 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1207 udf_write_aext(inode, epos, &laarr[i].extLocation,
1208 laarr[i].extLength, 1);
1212 struct buffer_head *udf_bread(struct inode *inode, int block,
1213 int create, int *err)
1215 struct buffer_head *bh = NULL;
1217 bh = udf_getblk(inode, block, create, err);
1221 if (buffer_uptodate(bh))
1224 ll_rw_block(READ, 1, &bh);
1227 if (buffer_uptodate(bh))
1235 int udf_setsize(struct inode *inode, loff_t newsize)
1238 struct udf_inode_info *iinfo;
1239 int bsize = i_blocksize(inode);
1241 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1242 S_ISLNK(inode->i_mode)))
1244 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1247 iinfo = UDF_I(inode);
1248 if (newsize > inode->i_size) {
1249 down_write(&iinfo->i_data_sem);
1250 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1252 (udf_file_entry_alloc_offset(inode) + newsize)) {
1253 err = udf_expand_file_adinicb(inode);
1256 down_write(&iinfo->i_data_sem);
1258 iinfo->i_lenAlloc = newsize;
1262 err = udf_extend_file(inode, newsize);
1264 up_write(&iinfo->i_data_sem);
1268 up_write(&iinfo->i_data_sem);
1269 truncate_setsize(inode, newsize);
1271 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1272 down_write(&iinfo->i_data_sem);
1273 udf_clear_extent_cache(inode);
1274 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1275 0x00, bsize - newsize -
1276 udf_file_entry_alloc_offset(inode));
1277 iinfo->i_lenAlloc = newsize;
1278 truncate_setsize(inode, newsize);
1279 up_write(&iinfo->i_data_sem);
1282 err = block_truncate_page(inode->i_mapping, newsize,
1286 truncate_setsize(inode, newsize);
1287 down_write(&iinfo->i_data_sem);
1288 udf_clear_extent_cache(inode);
1289 udf_truncate_extents(inode);
1290 up_write(&iinfo->i_data_sem);
1293 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1295 udf_sync_inode(inode);
1297 mark_inode_dirty(inode);
1302 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1303 * arbitrary - just that we hopefully don't limit any real use of rewritten
1304 * inode on write-once media but avoid looping for too long on corrupted media.
1306 #define UDF_MAX_ICB_NESTING 1024
1308 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1310 struct buffer_head *bh = NULL;
1311 struct fileEntry *fe;
1312 struct extendedFileEntry *efe;
1314 struct udf_inode_info *iinfo = UDF_I(inode);
1315 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1316 struct kernel_lb_addr *iloc = &iinfo->i_location;
1317 unsigned int link_count;
1318 unsigned int indirections = 0;
1319 int bs = inode->i_sb->s_blocksize;
1323 if (iloc->logicalBlockNum >=
1324 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1325 udf_debug("block=%d, partition=%d out of range\n",
1326 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1331 * Set defaults, but the inode is still incomplete!
1332 * Note: get_new_inode() sets the following on a new inode:
1335 * i_flags = sb->s_flags
1337 * clean_inode(): zero fills and sets
1342 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1344 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1348 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1349 ident != TAG_IDENT_USE) {
1350 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1351 inode->i_ino, ident);
1355 fe = (struct fileEntry *)bh->b_data;
1356 efe = (struct extendedFileEntry *)bh->b_data;
1358 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1359 struct buffer_head *ibh;
1361 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1362 if (ident == TAG_IDENT_IE && ibh) {
1363 struct kernel_lb_addr loc;
1364 struct indirectEntry *ie;
1366 ie = (struct indirectEntry *)ibh->b_data;
1367 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1369 if (ie->indirectICB.extLength) {
1371 memcpy(&iinfo->i_location, &loc,
1372 sizeof(struct kernel_lb_addr));
1373 if (++indirections > UDF_MAX_ICB_NESTING) {
1374 udf_err(inode->i_sb,
1375 "too many ICBs in ICB hierarchy"
1376 " (max %d supported)\n",
1377 UDF_MAX_ICB_NESTING);
1385 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1386 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1387 le16_to_cpu(fe->icbTag.strategyType));
1390 if (fe->icbTag.strategyType == cpu_to_le16(4))
1391 iinfo->i_strat4096 = 0;
1392 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1393 iinfo->i_strat4096 = 1;
1395 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1396 ICBTAG_FLAG_AD_MASK;
1397 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1398 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1399 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1403 iinfo->i_unique = 0;
1404 iinfo->i_lenEAttr = 0;
1405 iinfo->i_lenExtents = 0;
1406 iinfo->i_lenAlloc = 0;
1407 iinfo->i_next_alloc_block = 0;
1408 iinfo->i_next_alloc_goal = 0;
1409 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1412 ret = udf_alloc_i_data(inode, bs -
1413 sizeof(struct extendedFileEntry));
1416 memcpy(iinfo->i_ext.i_data,
1417 bh->b_data + sizeof(struct extendedFileEntry),
1418 bs - sizeof(struct extendedFileEntry));
1419 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1422 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1425 memcpy(iinfo->i_ext.i_data,
1426 bh->b_data + sizeof(struct fileEntry),
1427 bs - sizeof(struct fileEntry));
1428 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1431 iinfo->i_lenAlloc = le32_to_cpu(
1432 ((struct unallocSpaceEntry *)bh->b_data)->
1434 ret = udf_alloc_i_data(inode, bs -
1435 sizeof(struct unallocSpaceEntry));
1438 memcpy(iinfo->i_ext.i_data,
1439 bh->b_data + sizeof(struct unallocSpaceEntry),
1440 bs - sizeof(struct unallocSpaceEntry));
1445 read_lock(&sbi->s_cred_lock);
1446 i_uid_write(inode, le32_to_cpu(fe->uid));
1447 if (!uid_valid(inode->i_uid) ||
1448 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1449 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1450 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1452 i_gid_write(inode, le32_to_cpu(fe->gid));
1453 if (!gid_valid(inode->i_gid) ||
1454 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1455 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1456 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1458 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1459 sbi->s_fmode != UDF_INVALID_MODE)
1460 inode->i_mode = sbi->s_fmode;
1461 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1462 sbi->s_dmode != UDF_INVALID_MODE)
1463 inode->i_mode = sbi->s_dmode;
1465 inode->i_mode = udf_convert_permissions(fe);
1466 inode->i_mode &= ~sbi->s_umask;
1467 read_unlock(&sbi->s_cred_lock);
1469 link_count = le16_to_cpu(fe->fileLinkCount);
1471 if (!hidden_inode) {
1477 set_nlink(inode, link_count);
1479 inode->i_size = le64_to_cpu(fe->informationLength);
1480 iinfo->i_lenExtents = inode->i_size;
1482 if (iinfo->i_efe == 0) {
1483 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1484 (inode->i_sb->s_blocksize_bits - 9);
1486 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1487 inode->i_atime = sbi->s_record_time;
1489 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1490 fe->modificationTime))
1491 inode->i_mtime = sbi->s_record_time;
1493 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1494 inode->i_ctime = sbi->s_record_time;
1496 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1497 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1498 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1499 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1501 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1502 (inode->i_sb->s_blocksize_bits - 9);
1504 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1505 inode->i_atime = sbi->s_record_time;
1507 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1508 efe->modificationTime))
1509 inode->i_mtime = sbi->s_record_time;
1511 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1512 iinfo->i_crtime = sbi->s_record_time;
1514 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1515 inode->i_ctime = sbi->s_record_time;
1517 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1518 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1519 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1520 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1522 inode->i_generation = iinfo->i_unique;
1525 * Sanity check length of allocation descriptors and extended attrs to
1526 * avoid integer overflows
1528 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1530 /* Now do exact checks */
1531 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1533 /* Sanity checks for files in ICB so that we don't get confused later */
1534 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1536 * For file in ICB data is stored in allocation descriptor
1537 * so sizes should match
1539 if (iinfo->i_lenAlloc != inode->i_size)
1541 /* File in ICB has to fit in there... */
1542 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1546 switch (fe->icbTag.fileType) {
1547 case ICBTAG_FILE_TYPE_DIRECTORY:
1548 inode->i_op = &udf_dir_inode_operations;
1549 inode->i_fop = &udf_dir_operations;
1550 inode->i_mode |= S_IFDIR;
1553 case ICBTAG_FILE_TYPE_REALTIME:
1554 case ICBTAG_FILE_TYPE_REGULAR:
1555 case ICBTAG_FILE_TYPE_UNDEF:
1556 case ICBTAG_FILE_TYPE_VAT20:
1557 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1558 inode->i_data.a_ops = &udf_adinicb_aops;
1560 inode->i_data.a_ops = &udf_aops;
1561 inode->i_op = &udf_file_inode_operations;
1562 inode->i_fop = &udf_file_operations;
1563 inode->i_mode |= S_IFREG;
1565 case ICBTAG_FILE_TYPE_BLOCK:
1566 inode->i_mode |= S_IFBLK;
1568 case ICBTAG_FILE_TYPE_CHAR:
1569 inode->i_mode |= S_IFCHR;
1571 case ICBTAG_FILE_TYPE_FIFO:
1572 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1574 case ICBTAG_FILE_TYPE_SOCKET:
1575 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1577 case ICBTAG_FILE_TYPE_SYMLINK:
1578 inode->i_data.a_ops = &udf_symlink_aops;
1579 inode->i_op = &udf_symlink_inode_operations;
1580 inode->i_mode = S_IFLNK | S_IRWXUGO;
1582 case ICBTAG_FILE_TYPE_MAIN:
1583 udf_debug("METADATA FILE-----\n");
1585 case ICBTAG_FILE_TYPE_MIRROR:
1586 udf_debug("METADATA MIRROR FILE-----\n");
1588 case ICBTAG_FILE_TYPE_BITMAP:
1589 udf_debug("METADATA BITMAP FILE-----\n");
1592 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1593 inode->i_ino, fe->icbTag.fileType);
1596 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1597 struct deviceSpec *dsea =
1598 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1600 init_special_inode(inode, inode->i_mode,
1601 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1602 le32_to_cpu(dsea->minorDeviceIdent)));
1603 /* Developer ID ??? */
1613 static int udf_alloc_i_data(struct inode *inode, size_t size)
1615 struct udf_inode_info *iinfo = UDF_I(inode);
1616 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1618 if (!iinfo->i_ext.i_data) {
1619 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1627 static umode_t udf_convert_permissions(struct fileEntry *fe)
1630 uint32_t permissions;
1633 permissions = le32_to_cpu(fe->permissions);
1634 flags = le16_to_cpu(fe->icbTag.flags);
1636 mode = ((permissions) & S_IRWXO) |
1637 ((permissions >> 2) & S_IRWXG) |
1638 ((permissions >> 4) & S_IRWXU) |
1639 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1640 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1641 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1646 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1648 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1651 static int udf_sync_inode(struct inode *inode)
1653 return udf_update_inode(inode, 1);
1656 static int udf_update_inode(struct inode *inode, int do_sync)
1658 struct buffer_head *bh = NULL;
1659 struct fileEntry *fe;
1660 struct extendedFileEntry *efe;
1661 uint64_t lb_recorded;
1666 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1667 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1668 struct udf_inode_info *iinfo = UDF_I(inode);
1670 bh = udf_tgetblk(inode->i_sb,
1671 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1673 udf_debug("getblk failure\n");
1678 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1679 fe = (struct fileEntry *)bh->b_data;
1680 efe = (struct extendedFileEntry *)bh->b_data;
1683 struct unallocSpaceEntry *use =
1684 (struct unallocSpaceEntry *)bh->b_data;
1686 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1687 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1688 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1689 sizeof(struct unallocSpaceEntry));
1690 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1691 crclen = sizeof(struct unallocSpaceEntry);
1696 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1697 fe->uid = cpu_to_le32(-1);
1699 fe->uid = cpu_to_le32(i_uid_read(inode));
1701 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1702 fe->gid = cpu_to_le32(-1);
1704 fe->gid = cpu_to_le32(i_gid_read(inode));
1706 udfperms = ((inode->i_mode & S_IRWXO)) |
1707 ((inode->i_mode & S_IRWXG) << 2) |
1708 ((inode->i_mode & S_IRWXU) << 4);
1710 udfperms |= (le32_to_cpu(fe->permissions) &
1711 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1712 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1713 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1714 fe->permissions = cpu_to_le32(udfperms);
1716 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1717 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1719 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1721 fe->informationLength = cpu_to_le64(inode->i_size);
1723 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1725 struct deviceSpec *dsea =
1726 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1728 dsea = (struct deviceSpec *)
1729 udf_add_extendedattr(inode,
1730 sizeof(struct deviceSpec) +
1731 sizeof(struct regid), 12, 0x3);
1732 dsea->attrType = cpu_to_le32(12);
1733 dsea->attrSubtype = 1;
1734 dsea->attrLength = cpu_to_le32(
1735 sizeof(struct deviceSpec) +
1736 sizeof(struct regid));
1737 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1739 eid = (struct regid *)dsea->impUse;
1740 memset(eid, 0, sizeof(struct regid));
1741 strcpy(eid->ident, UDF_ID_DEVELOPER);
1742 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1743 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1744 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1745 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1748 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1749 lb_recorded = 0; /* No extents => no blocks! */
1752 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1753 (blocksize_bits - 9);
1755 if (iinfo->i_efe == 0) {
1756 memcpy(bh->b_data + sizeof(struct fileEntry),
1757 iinfo->i_ext.i_data,
1758 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1759 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1761 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1762 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1763 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1764 memset(&(fe->impIdent), 0, sizeof(struct regid));
1765 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1766 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1767 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1768 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1769 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1770 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1771 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1772 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1773 crclen = sizeof(struct fileEntry);
1775 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1776 iinfo->i_ext.i_data,
1777 inode->i_sb->s_blocksize -
1778 sizeof(struct extendedFileEntry));
1779 efe->objectSize = cpu_to_le64(inode->i_size);
1780 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1782 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1783 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1784 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1785 iinfo->i_crtime = inode->i_atime;
1787 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1788 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1789 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1790 iinfo->i_crtime = inode->i_mtime;
1792 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1793 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1794 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1795 iinfo->i_crtime = inode->i_ctime;
1797 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1798 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1799 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1800 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1802 memset(&(efe->impIdent), 0, sizeof(struct regid));
1803 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1804 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1805 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1806 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1807 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1808 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1809 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1810 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1811 crclen = sizeof(struct extendedFileEntry);
1815 if (iinfo->i_strat4096) {
1816 fe->icbTag.strategyType = cpu_to_le16(4096);
1817 fe->icbTag.strategyParameter = cpu_to_le16(1);
1818 fe->icbTag.numEntries = cpu_to_le16(2);
1820 fe->icbTag.strategyType = cpu_to_le16(4);
1821 fe->icbTag.numEntries = cpu_to_le16(1);
1825 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1826 else if (S_ISDIR(inode->i_mode))
1827 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1828 else if (S_ISREG(inode->i_mode))
1829 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1830 else if (S_ISLNK(inode->i_mode))
1831 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1832 else if (S_ISBLK(inode->i_mode))
1833 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1834 else if (S_ISCHR(inode->i_mode))
1835 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1836 else if (S_ISFIFO(inode->i_mode))
1837 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1838 else if (S_ISSOCK(inode->i_mode))
1839 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1841 icbflags = iinfo->i_alloc_type |
1842 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1843 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1844 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1845 (le16_to_cpu(fe->icbTag.flags) &
1846 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1847 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1849 fe->icbTag.flags = cpu_to_le16(icbflags);
1850 if (sbi->s_udfrev >= 0x0200)
1851 fe->descTag.descVersion = cpu_to_le16(3);
1853 fe->descTag.descVersion = cpu_to_le16(2);
1854 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1855 fe->descTag.tagLocation = cpu_to_le32(
1856 iinfo->i_location.logicalBlockNum);
1857 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1858 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1859 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1861 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1863 set_buffer_uptodate(bh);
1866 /* write the data blocks */
1867 mark_buffer_dirty(bh);
1869 sync_dirty_buffer(bh);
1870 if (buffer_write_io_error(bh)) {
1871 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1881 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1884 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1885 struct inode *inode = iget_locked(sb, block);
1889 return ERR_PTR(-ENOMEM);
1891 if (!(inode->i_state & I_NEW))
1894 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1895 err = udf_read_inode(inode, hidden_inode);
1898 return ERR_PTR(err);
1900 unlock_new_inode(inode);
1905 int udf_add_aext(struct inode *inode, struct extent_position *epos,
1906 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1909 struct short_ad *sad = NULL;
1910 struct long_ad *lad = NULL;
1911 struct allocExtDesc *aed;
1913 struct udf_inode_info *iinfo = UDF_I(inode);
1916 ptr = iinfo->i_ext.i_data + epos->offset -
1917 udf_file_entry_alloc_offset(inode) +
1920 ptr = epos->bh->b_data + epos->offset;
1922 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1923 adsize = sizeof(struct short_ad);
1924 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1925 adsize = sizeof(struct long_ad);
1929 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1930 unsigned char *sptr, *dptr;
1931 struct buffer_head *nbh;
1933 struct kernel_lb_addr obloc = epos->block;
1935 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1936 obloc.partitionReferenceNum,
1937 obloc.logicalBlockNum, &err);
1938 if (!epos->block.logicalBlockNum)
1940 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1946 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1947 set_buffer_uptodate(nbh);
1949 mark_buffer_dirty_inode(nbh, inode);
1951 aed = (struct allocExtDesc *)(nbh->b_data);
1952 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1953 aed->previousAllocExtLocation =
1954 cpu_to_le32(obloc.logicalBlockNum);
1955 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1956 loffset = epos->offset;
1957 aed->lengthAllocDescs = cpu_to_le32(adsize);
1958 sptr = ptr - adsize;
1959 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1960 memcpy(dptr, sptr, adsize);
1961 epos->offset = sizeof(struct allocExtDesc) + adsize;
1963 loffset = epos->offset + adsize;
1964 aed->lengthAllocDescs = cpu_to_le32(0);
1966 epos->offset = sizeof(struct allocExtDesc);
1969 aed = (struct allocExtDesc *)epos->bh->b_data;
1970 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1972 iinfo->i_lenAlloc += adsize;
1973 mark_inode_dirty(inode);
1976 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1977 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1978 epos->block.logicalBlockNum, sizeof(struct tag));
1980 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1981 epos->block.logicalBlockNum, sizeof(struct tag));
1982 switch (iinfo->i_alloc_type) {
1983 case ICBTAG_FLAG_AD_SHORT:
1984 sad = (struct short_ad *)sptr;
1985 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1986 inode->i_sb->s_blocksize);
1988 cpu_to_le32(epos->block.logicalBlockNum);
1990 case ICBTAG_FLAG_AD_LONG:
1991 lad = (struct long_ad *)sptr;
1992 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1993 inode->i_sb->s_blocksize);
1994 lad->extLocation = cpu_to_lelb(epos->block);
1995 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1999 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2000 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2001 udf_update_tag(epos->bh->b_data, loffset);
2003 udf_update_tag(epos->bh->b_data,
2004 sizeof(struct allocExtDesc));
2005 mark_buffer_dirty_inode(epos->bh, inode);
2008 mark_inode_dirty(inode);
2013 udf_write_aext(inode, epos, eloc, elen, inc);
2016 iinfo->i_lenAlloc += adsize;
2017 mark_inode_dirty(inode);
2019 aed = (struct allocExtDesc *)epos->bh->b_data;
2020 le32_add_cpu(&aed->lengthAllocDescs, adsize);
2021 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2022 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2023 udf_update_tag(epos->bh->b_data,
2024 epos->offset + (inc ? 0 : adsize));
2026 udf_update_tag(epos->bh->b_data,
2027 sizeof(struct allocExtDesc));
2028 mark_buffer_dirty_inode(epos->bh, inode);
2034 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2035 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2039 struct short_ad *sad;
2040 struct long_ad *lad;
2041 struct udf_inode_info *iinfo = UDF_I(inode);
2044 ptr = iinfo->i_ext.i_data + epos->offset -
2045 udf_file_entry_alloc_offset(inode) +
2048 ptr = epos->bh->b_data + epos->offset;
2050 switch (iinfo->i_alloc_type) {
2051 case ICBTAG_FLAG_AD_SHORT:
2052 sad = (struct short_ad *)ptr;
2053 sad->extLength = cpu_to_le32(elen);
2054 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2055 adsize = sizeof(struct short_ad);
2057 case ICBTAG_FLAG_AD_LONG:
2058 lad = (struct long_ad *)ptr;
2059 lad->extLength = cpu_to_le32(elen);
2060 lad->extLocation = cpu_to_lelb(*eloc);
2061 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2062 adsize = sizeof(struct long_ad);
2069 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2070 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2071 struct allocExtDesc *aed =
2072 (struct allocExtDesc *)epos->bh->b_data;
2073 udf_update_tag(epos->bh->b_data,
2074 le32_to_cpu(aed->lengthAllocDescs) +
2075 sizeof(struct allocExtDesc));
2077 mark_buffer_dirty_inode(epos->bh, inode);
2079 mark_inode_dirty(inode);
2083 epos->offset += adsize;
2087 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2088 * someone does some weird stuff.
2090 #define UDF_MAX_INDIR_EXTS 16
2092 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2093 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2096 unsigned int indirections = 0;
2098 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2099 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2102 if (++indirections > UDF_MAX_INDIR_EXTS) {
2103 udf_err(inode->i_sb,
2104 "too many indirect extents in inode %lu\n",
2109 epos->block = *eloc;
2110 epos->offset = sizeof(struct allocExtDesc);
2112 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2113 epos->bh = udf_tread(inode->i_sb, block);
2115 udf_debug("reading block %d failed!\n", block);
2123 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2124 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2129 struct short_ad *sad;
2130 struct long_ad *lad;
2131 struct udf_inode_info *iinfo = UDF_I(inode);
2135 epos->offset = udf_file_entry_alloc_offset(inode);
2136 ptr = iinfo->i_ext.i_data + epos->offset -
2137 udf_file_entry_alloc_offset(inode) +
2139 alen = udf_file_entry_alloc_offset(inode) +
2143 epos->offset = sizeof(struct allocExtDesc);
2144 ptr = epos->bh->b_data + epos->offset;
2145 alen = sizeof(struct allocExtDesc) +
2146 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2150 switch (iinfo->i_alloc_type) {
2151 case ICBTAG_FLAG_AD_SHORT:
2152 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2155 etype = le32_to_cpu(sad->extLength) >> 30;
2156 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2157 eloc->partitionReferenceNum =
2158 iinfo->i_location.partitionReferenceNum;
2159 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2161 case ICBTAG_FLAG_AD_LONG:
2162 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2165 etype = le32_to_cpu(lad->extLength) >> 30;
2166 *eloc = lelb_to_cpu(lad->extLocation);
2167 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2170 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2177 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2178 struct kernel_lb_addr neloc, uint32_t nelen)
2180 struct kernel_lb_addr oeloc;
2187 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2188 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2190 nelen = (etype << 30) | oelen;
2192 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2195 return (nelen >> 30);
2198 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2199 struct kernel_lb_addr eloc, uint32_t elen)
2201 struct extent_position oepos;
2204 struct allocExtDesc *aed;
2205 struct udf_inode_info *iinfo;
2212 iinfo = UDF_I(inode);
2213 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2214 adsize = sizeof(struct short_ad);
2215 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2216 adsize = sizeof(struct long_ad);
2221 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2224 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2225 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2226 if (oepos.bh != epos.bh) {
2227 oepos.block = epos.block;
2231 oepos.offset = epos.offset - adsize;
2234 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2237 if (epos.bh != oepos.bh) {
2238 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2239 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2240 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2242 iinfo->i_lenAlloc -= (adsize * 2);
2243 mark_inode_dirty(inode);
2245 aed = (struct allocExtDesc *)oepos.bh->b_data;
2246 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2247 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2248 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2249 udf_update_tag(oepos.bh->b_data,
2250 oepos.offset - (2 * adsize));
2252 udf_update_tag(oepos.bh->b_data,
2253 sizeof(struct allocExtDesc));
2254 mark_buffer_dirty_inode(oepos.bh, inode);
2257 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2259 iinfo->i_lenAlloc -= adsize;
2260 mark_inode_dirty(inode);
2262 aed = (struct allocExtDesc *)oepos.bh->b_data;
2263 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2264 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2265 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2266 udf_update_tag(oepos.bh->b_data,
2267 epos.offset - adsize);
2269 udf_update_tag(oepos.bh->b_data,
2270 sizeof(struct allocExtDesc));
2271 mark_buffer_dirty_inode(oepos.bh, inode);
2278 return (elen >> 30);
2281 int8_t inode_bmap(struct inode *inode, sector_t block,
2282 struct extent_position *pos, struct kernel_lb_addr *eloc,
2283 uint32_t *elen, sector_t *offset)
2285 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2286 loff_t lbcount = 0, bcount =
2287 (loff_t) block << blocksize_bits;
2289 struct udf_inode_info *iinfo;
2291 iinfo = UDF_I(inode);
2292 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2294 pos->block = iinfo->i_location;
2299 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2301 *offset = (bcount - lbcount) >> blocksize_bits;
2302 iinfo->i_lenExtents = lbcount;
2306 } while (lbcount <= bcount);
2307 /* update extent cache */
2308 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2309 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2314 long udf_block_map(struct inode *inode, sector_t block)
2316 struct kernel_lb_addr eloc;
2319 struct extent_position epos = {};
2322 down_read(&UDF_I(inode)->i_data_sem);
2324 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2325 (EXT_RECORDED_ALLOCATED >> 30))
2326 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2330 up_read(&UDF_I(inode)->i_data_sem);
2333 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2334 return udf_fixed_to_variable(ret);