5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
22 * 12/06/98 blf partition support in udf_iget, udf_block_map
24 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/writeback.h>
37 #include <linux/slab.h>
38 #include <linux/crc-itu-t.h>
39 #include <linux/mpage.h>
40 #include <linux/uio.h>
41 #include <linux/bio.h>
46 #define EXTENT_MERGE_SIZE 5
48 static umode_t udf_convert_permissions(struct fileEntry *);
49 static int udf_update_inode(struct inode *, int);
50 static int udf_sync_inode(struct inode *inode);
51 static int udf_alloc_i_data(struct inode *inode, size_t size);
52 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
53 static int udf_insert_aext(struct inode *, struct extent_position,
54 struct kernel_lb_addr, uint32_t);
55 static void udf_split_extents(struct inode *, int *, int, int,
56 struct kernel_long_ad *, int *);
57 static void udf_prealloc_extents(struct inode *, int, int,
58 struct kernel_long_ad *, int *);
59 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
60 static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
61 int, struct extent_position *);
62 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
64 static void __udf_clear_extent_cache(struct inode *inode)
66 struct udf_inode_info *iinfo = UDF_I(inode);
68 if (iinfo->cached_extent.lstart != -1) {
69 brelse(iinfo->cached_extent.epos.bh);
70 iinfo->cached_extent.lstart = -1;
74 /* Invalidate extent cache */
75 static void udf_clear_extent_cache(struct inode *inode)
77 struct udf_inode_info *iinfo = UDF_I(inode);
79 spin_lock(&iinfo->i_extent_cache_lock);
80 __udf_clear_extent_cache(inode);
81 spin_unlock(&iinfo->i_extent_cache_lock);
84 /* Return contents of extent cache */
85 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
86 loff_t *lbcount, struct extent_position *pos)
88 struct udf_inode_info *iinfo = UDF_I(inode);
91 spin_lock(&iinfo->i_extent_cache_lock);
92 if ((iinfo->cached_extent.lstart <= bcount) &&
93 (iinfo->cached_extent.lstart != -1)) {
95 *lbcount = iinfo->cached_extent.lstart;
96 memcpy(pos, &iinfo->cached_extent.epos,
97 sizeof(struct extent_position));
102 spin_unlock(&iinfo->i_extent_cache_lock);
106 /* Add extent to extent cache */
107 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
108 struct extent_position *pos)
110 struct udf_inode_info *iinfo = UDF_I(inode);
112 spin_lock(&iinfo->i_extent_cache_lock);
113 /* Invalidate previously cached extent */
114 __udf_clear_extent_cache(inode);
117 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
118 iinfo->cached_extent.lstart = estart;
119 switch (iinfo->i_alloc_type) {
120 case ICBTAG_FLAG_AD_SHORT:
121 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
123 case ICBTAG_FLAG_AD_LONG:
124 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
127 spin_unlock(&iinfo->i_extent_cache_lock);
130 void udf_evict_inode(struct inode *inode)
132 struct udf_inode_info *iinfo = UDF_I(inode);
135 if (!is_bad_inode(inode)) {
136 if (!inode->i_nlink) {
138 udf_setsize(inode, 0);
139 udf_update_inode(inode, IS_SYNC(inode));
141 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
142 inode->i_size != iinfo->i_lenExtents) {
143 udf_warn(inode->i_sb,
144 "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
145 inode->i_ino, inode->i_mode,
146 (unsigned long long)inode->i_size,
147 (unsigned long long)iinfo->i_lenExtents);
150 truncate_inode_pages_final(&inode->i_data);
151 invalidate_inode_buffers(inode);
153 kfree(iinfo->i_ext.i_data);
154 iinfo->i_ext.i_data = NULL;
155 udf_clear_extent_cache(inode);
157 udf_free_inode(inode);
161 static void udf_write_failed(struct address_space *mapping, loff_t to)
163 struct inode *inode = mapping->host;
164 struct udf_inode_info *iinfo = UDF_I(inode);
165 loff_t isize = inode->i_size;
168 truncate_pagecache(inode, isize);
169 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
170 down_write(&iinfo->i_data_sem);
171 udf_clear_extent_cache(inode);
172 udf_truncate_extents(inode);
173 up_write(&iinfo->i_data_sem);
178 static int udf_writepage(struct page *page, struct writeback_control *wbc)
180 return block_write_full_page(page, udf_get_block, wbc);
183 static int udf_writepages(struct address_space *mapping,
184 struct writeback_control *wbc)
186 return mpage_writepages(mapping, wbc, udf_get_block);
189 static int udf_readpage(struct file *file, struct page *page)
191 return mpage_readpage(page, udf_get_block);
194 static int udf_readpages(struct file *file, struct address_space *mapping,
195 struct list_head *pages, unsigned nr_pages)
197 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
200 static int udf_write_begin(struct file *file, struct address_space *mapping,
201 loff_t pos, unsigned len, unsigned flags,
202 struct page **pagep, void **fsdata)
206 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
208 udf_write_failed(mapping, pos + len);
212 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
214 struct file *file = iocb->ki_filp;
215 struct address_space *mapping = file->f_mapping;
216 struct inode *inode = mapping->host;
217 size_t count = iov_iter_count(iter);
220 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
221 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
222 udf_write_failed(mapping, iocb->ki_pos + count);
226 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
228 return generic_block_bmap(mapping, block, udf_get_block);
231 const struct address_space_operations udf_aops = {
232 .readpage = udf_readpage,
233 .readpages = udf_readpages,
234 .writepage = udf_writepage,
235 .writepages = udf_writepages,
236 .write_begin = udf_write_begin,
237 .write_end = generic_write_end,
238 .direct_IO = udf_direct_IO,
243 * Expand file stored in ICB to a normal one-block-file
245 * This function requires i_data_sem for writing and releases it.
246 * This function requires i_mutex held
248 int udf_expand_file_adinicb(struct inode *inode)
252 struct udf_inode_info *iinfo = UDF_I(inode);
255 WARN_ON_ONCE(!inode_is_locked(inode));
256 if (!iinfo->i_lenAlloc) {
257 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
258 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
260 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
261 /* from now on we have normal address_space methods */
262 inode->i_data.a_ops = &udf_aops;
263 up_write(&iinfo->i_data_sem);
264 mark_inode_dirty(inode);
268 * Release i_data_sem so that we can lock a page - page lock ranks
269 * above i_data_sem. i_mutex still protects us against file changes.
271 up_write(&iinfo->i_data_sem);
273 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
277 if (!PageUptodate(page)) {
278 kaddr = kmap_atomic(page);
279 memset(kaddr + iinfo->i_lenAlloc, 0x00,
280 PAGE_SIZE - iinfo->i_lenAlloc);
281 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
283 flush_dcache_page(page);
284 SetPageUptodate(page);
285 kunmap_atomic(kaddr);
287 down_write(&iinfo->i_data_sem);
288 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
290 iinfo->i_lenAlloc = 0;
291 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
292 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
294 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
295 /* from now on we have normal address_space methods */
296 inode->i_data.a_ops = &udf_aops;
297 set_page_dirty(page);
299 up_write(&iinfo->i_data_sem);
300 err = filemap_fdatawrite(inode->i_mapping);
302 /* Restore everything back so that we don't lose data... */
304 down_write(&iinfo->i_data_sem);
305 kaddr = kmap_atomic(page);
306 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
308 kunmap_atomic(kaddr);
310 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
311 inode->i_data.a_ops = &udf_adinicb_aops;
312 iinfo->i_lenAlloc = inode->i_size;
313 up_write(&iinfo->i_data_sem);
316 mark_inode_dirty(inode);
321 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
325 struct buffer_head *dbh = NULL;
326 struct kernel_lb_addr eloc;
328 struct extent_position epos;
330 struct udf_fileident_bh sfibh, dfibh;
331 loff_t f_pos = udf_ext0_offset(inode);
332 int size = udf_ext0_offset(inode) + inode->i_size;
333 struct fileIdentDesc cfi, *sfi, *dfi;
334 struct udf_inode_info *iinfo = UDF_I(inode);
336 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
337 alloctype = ICBTAG_FLAG_AD_SHORT;
339 alloctype = ICBTAG_FLAG_AD_LONG;
341 if (!inode->i_size) {
342 iinfo->i_alloc_type = alloctype;
343 mark_inode_dirty(inode);
347 /* alloc block, and copy data to it */
348 *block = udf_new_block(inode->i_sb, inode,
349 iinfo->i_location.partitionReferenceNum,
350 iinfo->i_location.logicalBlockNum, err);
353 newblock = udf_get_pblock(inode->i_sb, *block,
354 iinfo->i_location.partitionReferenceNum,
358 dbh = udf_tgetblk(inode->i_sb, newblock);
362 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
363 set_buffer_uptodate(dbh);
365 mark_buffer_dirty_inode(dbh, inode);
367 sfibh.soffset = sfibh.eoffset =
368 f_pos & (inode->i_sb->s_blocksize - 1);
369 sfibh.sbh = sfibh.ebh = NULL;
370 dfibh.soffset = dfibh.eoffset = 0;
371 dfibh.sbh = dfibh.ebh = dbh;
372 while (f_pos < size) {
373 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
374 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
380 iinfo->i_alloc_type = alloctype;
381 sfi->descTag.tagLocation = cpu_to_le32(*block);
382 dfibh.soffset = dfibh.eoffset;
383 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
384 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
385 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
387 le16_to_cpu(sfi->lengthOfImpUse))) {
388 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
393 mark_buffer_dirty_inode(dbh, inode);
395 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
397 iinfo->i_lenAlloc = 0;
398 eloc.logicalBlockNum = *block;
399 eloc.partitionReferenceNum =
400 iinfo->i_location.partitionReferenceNum;
401 iinfo->i_lenExtents = inode->i_size;
403 epos.block = iinfo->i_location;
404 epos.offset = udf_file_entry_alloc_offset(inode);
405 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
409 mark_inode_dirty(inode);
413 static int udf_get_block(struct inode *inode, sector_t block,
414 struct buffer_head *bh_result, int create)
418 struct udf_inode_info *iinfo;
421 phys = udf_block_map(inode, block);
423 map_bh(bh_result, inode->i_sb, phys);
429 iinfo = UDF_I(inode);
431 down_write(&iinfo->i_data_sem);
432 if (block == iinfo->i_next_alloc_block + 1) {
433 iinfo->i_next_alloc_block++;
434 iinfo->i_next_alloc_goal++;
438 * Block beyond EOF and prealloc extents? Just discard preallocation
439 * as it is not useful and complicates things.
441 if (((loff_t)block) << inode->i_blkbits > iinfo->i_lenExtents)
442 udf_discard_prealloc(inode);
443 udf_clear_extent_cache(inode);
444 phys = inode_getblk(inode, block, &err, &new);
449 set_buffer_new(bh_result);
450 map_bh(bh_result, inode->i_sb, phys);
453 up_write(&iinfo->i_data_sem);
457 static struct buffer_head *udf_getblk(struct inode *inode, long block,
458 int create, int *err)
460 struct buffer_head *bh;
461 struct buffer_head dummy;
464 dummy.b_blocknr = -1000;
465 *err = udf_get_block(inode, block, &dummy, create);
466 if (!*err && buffer_mapped(&dummy)) {
467 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
468 if (buffer_new(&dummy)) {
470 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
471 set_buffer_uptodate(bh);
473 mark_buffer_dirty_inode(bh, inode);
481 /* Extend the file with new blocks totaling 'new_block_bytes',
482 * return the number of extents added
484 static int udf_do_extend_file(struct inode *inode,
485 struct extent_position *last_pos,
486 struct kernel_long_ad *last_ext,
487 loff_t new_block_bytes)
490 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
491 struct super_block *sb = inode->i_sb;
492 struct udf_inode_info *iinfo;
495 /* The previous extent is fake and we should not extend by anything
496 * - there's nothing to do... */
497 if (!new_block_bytes && fake)
500 iinfo = UDF_I(inode);
501 /* Round the last extent up to a multiple of block size */
502 if (last_ext->extLength & (sb->s_blocksize - 1)) {
503 last_ext->extLength =
504 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
505 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
506 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
507 iinfo->i_lenExtents =
508 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
509 ~(sb->s_blocksize - 1);
512 /* Can we merge with the previous extent? */
513 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
514 EXT_NOT_RECORDED_NOT_ALLOCATED) {
515 add = (1 << 30) - sb->s_blocksize -
516 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
517 if (add > new_block_bytes)
518 add = new_block_bytes;
519 new_block_bytes -= add;
520 last_ext->extLength += add;
524 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
525 last_ext->extLength, 1);
530 struct kernel_lb_addr tmploc;
533 udf_write_aext(inode, last_pos, &last_ext->extLocation,
534 last_ext->extLength, 1);
537 * We've rewritten the last extent. If we are going to add
538 * more extents, we may need to enter possible following
539 * empty indirect extent.
542 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
545 /* Managed to do everything necessary? */
546 if (!new_block_bytes)
549 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
550 last_ext->extLocation.logicalBlockNum = 0;
551 last_ext->extLocation.partitionReferenceNum = 0;
552 add = (1 << 30) - sb->s_blocksize;
553 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
555 /* Create enough extents to cover the whole hole */
556 while (new_block_bytes > add) {
557 new_block_bytes -= add;
558 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
559 last_ext->extLength, 1);
564 if (new_block_bytes) {
565 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
567 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
568 last_ext->extLength, 1);
575 /* last_pos should point to the last written extent... */
576 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
577 last_pos->offset -= sizeof(struct short_ad);
578 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
579 last_pos->offset -= sizeof(struct long_ad);
585 /* Remove extents we've created so far */
586 udf_clear_extent_cache(inode);
587 udf_truncate_extents(inode);
591 /* Extend the final block of the file to final_block_len bytes */
592 static void udf_do_extend_final_block(struct inode *inode,
593 struct extent_position *last_pos,
594 struct kernel_long_ad *last_ext,
597 uint32_t added_bytes;
600 * Extent already large enough? It may be already rounded up to block
603 if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
605 added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
606 last_ext->extLength += added_bytes;
607 UDF_I(inode)->i_lenExtents += added_bytes;
609 udf_write_aext(inode, last_pos, &last_ext->extLocation,
610 last_ext->extLength, 1);
613 static int udf_extend_file(struct inode *inode, loff_t newsize)
616 struct extent_position epos;
617 struct kernel_lb_addr eloc;
620 struct super_block *sb = inode->i_sb;
621 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
624 struct udf_inode_info *iinfo = UDF_I(inode);
625 struct kernel_long_ad extent;
627 bool within_last_ext;
629 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
630 adsize = sizeof(struct short_ad);
631 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
632 adsize = sizeof(struct long_ad);
637 * When creating hole in file, just don't bother with preserving
638 * preallocation. It likely won't be very useful anyway.
640 udf_discard_prealloc(inode);
642 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
643 within_last_ext = (etype != -1);
644 /* We don't expect extents past EOF... */
645 WARN_ON_ONCE(within_last_ext &&
646 elen > ((loff_t)offset + 1) << inode->i_blkbits);
648 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
649 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
650 /* File has no extents at all or has empty last
651 * indirect extent! Create a fake extent... */
652 extent.extLocation.logicalBlockNum = 0;
653 extent.extLocation.partitionReferenceNum = 0;
654 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
656 epos.offset -= adsize;
657 etype = udf_next_aext(inode, &epos, &extent.extLocation,
658 &extent.extLength, 0);
659 extent.extLength |= etype << 30;
662 new_elen = ((loff_t)offset << inode->i_blkbits) |
663 (newsize & (sb->s_blocksize - 1));
665 /* File has extent covering the new size (could happen when extending
668 if (within_last_ext) {
669 /* Extending file within the last file block */
670 udf_do_extend_final_block(inode, &epos, &extent, new_elen);
672 err = udf_do_extend_file(inode, &epos, &extent, new_elen);
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 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
773 /* Are we beyond EOF and preallocated extent? */
784 /* Create a fake extent when there's not one */
785 memset(&laarr[0].extLocation, 0x00,
786 sizeof(struct kernel_lb_addr));
787 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
788 /* Will udf_do_extend_file() create real extent from
790 startnum = (offset > 0);
792 /* Create extents for the hole between EOF and offset */
793 hole_len = (loff_t)offset << inode->i_blkbits;
794 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
804 * Is there any real extent? - otherwise we overwrite the fake
809 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
810 inode->i_sb->s_blocksize;
811 memset(&laarr[c].extLocation, 0x00,
812 sizeof(struct kernel_lb_addr));
818 endnum = startnum = ((count > 2) ? 2 : count);
820 /* if the current extent is in position 0,
821 swap it with the previous */
822 if (!c && count != 1) {
829 /* if the current block is located in an extent,
830 read the next extent */
831 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
833 laarr[c + 1].extLength = (etype << 30) | elen;
834 laarr[c + 1].extLocation = eloc;
842 /* if the current extent is not recorded but allocated, get the
843 * block in the extent corresponding to the requested block */
844 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
845 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
846 else { /* otherwise, allocate a new block */
847 if (iinfo->i_next_alloc_block == block)
848 goal = iinfo->i_next_alloc_goal;
851 if (!(goal = pgoal)) /* XXX: what was intended here? */
852 goal = iinfo->i_location.logicalBlockNum + 1;
855 newblocknum = udf_new_block(inode->i_sb, inode,
856 iinfo->i_location.partitionReferenceNum,
864 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
867 /* if the extent the requsted block is located in contains multiple
868 * blocks, split the extent into at most three extents. blocks prior
869 * to requested block, requested block, and blocks after requested
871 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
873 /* We preallocate blocks only for regular files. It also makes sense
874 * for directories but there's a problem when to drop the
875 * preallocation. We might use some delayed work for that but I feel
876 * it's overengineering for a filesystem like UDF. */
877 if (S_ISREG(inode->i_mode))
878 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
880 /* merge any continuous blocks in laarr */
881 udf_merge_extents(inode, laarr, &endnum);
883 /* write back the new extents, inserting new extents if the new number
884 * of extents is greater than the old number, and deleting extents if
885 * the new number of extents is less than the old number */
886 *err = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
890 newblock = udf_get_pblock(inode->i_sb, newblocknum,
891 iinfo->i_location.partitionReferenceNum, 0);
897 iinfo->i_next_alloc_block = block;
898 iinfo->i_next_alloc_goal = newblocknum;
899 inode->i_ctime = current_time(inode);
902 udf_sync_inode(inode);
904 mark_inode_dirty(inode);
906 brelse(prev_epos.bh);
908 brelse(next_epos.bh);
912 static void udf_split_extents(struct inode *inode, int *c, int offset,
913 int newblocknum, struct kernel_long_ad *laarr,
916 unsigned long blocksize = inode->i_sb->s_blocksize;
917 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
919 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
920 (laarr[*c].extLength >> 30) ==
921 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
923 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
924 blocksize - 1) >> blocksize_bits;
925 int8_t etype = (laarr[curr].extLength >> 30);
929 else if (!offset || blen == offset + 1) {
930 laarr[curr + 2] = laarr[curr + 1];
931 laarr[curr + 1] = laarr[curr];
933 laarr[curr + 3] = laarr[curr + 1];
934 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
938 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
939 udf_free_blocks(inode->i_sb, inode,
940 &laarr[curr].extLocation,
942 laarr[curr].extLength =
943 EXT_NOT_RECORDED_NOT_ALLOCATED |
944 (offset << blocksize_bits);
945 laarr[curr].extLocation.logicalBlockNum = 0;
946 laarr[curr].extLocation.
947 partitionReferenceNum = 0;
949 laarr[curr].extLength = (etype << 30) |
950 (offset << blocksize_bits);
956 laarr[curr].extLocation.logicalBlockNum = newblocknum;
957 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
958 laarr[curr].extLocation.partitionReferenceNum =
959 UDF_I(inode)->i_location.partitionReferenceNum;
960 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
964 if (blen != offset + 1) {
965 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
966 laarr[curr].extLocation.logicalBlockNum +=
968 laarr[curr].extLength = (etype << 30) |
969 ((blen - (offset + 1)) << blocksize_bits);
976 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
977 struct kernel_long_ad *laarr,
980 int start, length = 0, currlength = 0, i;
982 if (*endnum >= (c + 1)) {
988 if ((laarr[c + 1].extLength >> 30) ==
989 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
991 length = currlength =
992 (((laarr[c + 1].extLength &
993 UDF_EXTENT_LENGTH_MASK) +
994 inode->i_sb->s_blocksize - 1) >>
995 inode->i_sb->s_blocksize_bits);
1000 for (i = start + 1; i <= *endnum; i++) {
1003 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1004 } else if ((laarr[i].extLength >> 30) ==
1005 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1006 length += (((laarr[i].extLength &
1007 UDF_EXTENT_LENGTH_MASK) +
1008 inode->i_sb->s_blocksize - 1) >>
1009 inode->i_sb->s_blocksize_bits);
1015 int next = laarr[start].extLocation.logicalBlockNum +
1016 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1017 inode->i_sb->s_blocksize - 1) >>
1018 inode->i_sb->s_blocksize_bits);
1019 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1020 laarr[start].extLocation.partitionReferenceNum,
1021 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1022 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1025 if (start == (c + 1))
1026 laarr[start].extLength +=
1028 inode->i_sb->s_blocksize_bits);
1030 memmove(&laarr[c + 2], &laarr[c + 1],
1031 sizeof(struct long_ad) * (*endnum - (c + 1)));
1033 laarr[c + 1].extLocation.logicalBlockNum = next;
1034 laarr[c + 1].extLocation.partitionReferenceNum =
1035 laarr[c].extLocation.
1036 partitionReferenceNum;
1037 laarr[c + 1].extLength =
1038 EXT_NOT_RECORDED_ALLOCATED |
1040 inode->i_sb->s_blocksize_bits);
1044 for (i = start + 1; numalloc && i < *endnum; i++) {
1045 int elen = ((laarr[i].extLength &
1046 UDF_EXTENT_LENGTH_MASK) +
1047 inode->i_sb->s_blocksize - 1) >>
1048 inode->i_sb->s_blocksize_bits;
1050 if (elen > numalloc) {
1051 laarr[i].extLength -=
1053 inode->i_sb->s_blocksize_bits);
1057 if (*endnum > (i + 1))
1060 sizeof(struct long_ad) *
1061 (*endnum - (i + 1)));
1066 UDF_I(inode)->i_lenExtents +=
1067 numalloc << inode->i_sb->s_blocksize_bits;
1072 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1076 unsigned long blocksize = inode->i_sb->s_blocksize;
1077 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1079 for (i = 0; i < (*endnum - 1); i++) {
1080 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1081 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1083 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1084 (((li->extLength >> 30) ==
1085 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1086 ((lip1->extLocation.logicalBlockNum -
1087 li->extLocation.logicalBlockNum) ==
1088 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1089 blocksize - 1) >> blocksize_bits)))) {
1091 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1092 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1093 blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
1094 li->extLength = lip1->extLength +
1096 UDF_EXTENT_LENGTH_MASK) +
1097 blocksize - 1) & ~(blocksize - 1));
1098 if (*endnum > (i + 2))
1099 memmove(&laarr[i + 1], &laarr[i + 2],
1100 sizeof(struct long_ad) *
1101 (*endnum - (i + 2)));
1105 } else if (((li->extLength >> 30) ==
1106 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1107 ((lip1->extLength >> 30) ==
1108 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1109 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1111 UDF_EXTENT_LENGTH_MASK) +
1112 blocksize - 1) >> blocksize_bits);
1113 li->extLocation.logicalBlockNum = 0;
1114 li->extLocation.partitionReferenceNum = 0;
1116 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1117 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1118 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1119 lip1->extLength = (lip1->extLength -
1121 UDF_EXTENT_LENGTH_MASK) +
1122 UDF_EXTENT_LENGTH_MASK) &
1124 li->extLength = (li->extLength &
1125 UDF_EXTENT_FLAG_MASK) +
1126 (UDF_EXTENT_LENGTH_MASK + 1) -
1129 li->extLength = lip1->extLength +
1131 UDF_EXTENT_LENGTH_MASK) +
1132 blocksize - 1) & ~(blocksize - 1));
1133 if (*endnum > (i + 2))
1134 memmove(&laarr[i + 1], &laarr[i + 2],
1135 sizeof(struct long_ad) *
1136 (*endnum - (i + 2)));
1140 } else if ((li->extLength >> 30) ==
1141 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1142 udf_free_blocks(inode->i_sb, inode,
1143 &li->extLocation, 0,
1145 UDF_EXTENT_LENGTH_MASK) +
1146 blocksize - 1) >> blocksize_bits);
1147 li->extLocation.logicalBlockNum = 0;
1148 li->extLocation.partitionReferenceNum = 0;
1149 li->extLength = (li->extLength &
1150 UDF_EXTENT_LENGTH_MASK) |
1151 EXT_NOT_RECORDED_NOT_ALLOCATED;
1156 static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1157 int startnum, int endnum,
1158 struct extent_position *epos)
1161 struct kernel_lb_addr tmploc;
1165 if (startnum > endnum) {
1166 for (i = 0; i < (startnum - endnum); i++)
1167 udf_delete_aext(inode, *epos);
1168 } else if (startnum < endnum) {
1169 for (i = 0; i < (endnum - startnum); i++) {
1170 err = udf_insert_aext(inode, *epos,
1171 laarr[i].extLocation,
1172 laarr[i].extLength);
1174 * If we fail here, we are likely corrupting the extent
1175 * list and leaking blocks. At least stop early to
1180 udf_next_aext(inode, epos, &laarr[i].extLocation,
1181 &laarr[i].extLength, 1);
1186 for (i = start; i < endnum; i++) {
1187 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1188 udf_write_aext(inode, epos, &laarr[i].extLocation,
1189 laarr[i].extLength, 1);
1194 struct buffer_head *udf_bread(struct inode *inode, int block,
1195 int create, int *err)
1197 struct buffer_head *bh = NULL;
1199 bh = udf_getblk(inode, block, create, err);
1203 if (buffer_uptodate(bh))
1206 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1209 if (buffer_uptodate(bh))
1217 int udf_setsize(struct inode *inode, loff_t newsize)
1220 struct udf_inode_info *iinfo;
1221 int bsize = i_blocksize(inode);
1223 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1224 S_ISLNK(inode->i_mode)))
1226 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1229 iinfo = UDF_I(inode);
1230 if (newsize > inode->i_size) {
1231 down_write(&iinfo->i_data_sem);
1232 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1234 (udf_file_entry_alloc_offset(inode) + newsize)) {
1235 err = udf_expand_file_adinicb(inode);
1238 down_write(&iinfo->i_data_sem);
1240 iinfo->i_lenAlloc = newsize;
1244 err = udf_extend_file(inode, newsize);
1246 up_write(&iinfo->i_data_sem);
1250 up_write(&iinfo->i_data_sem);
1251 truncate_setsize(inode, newsize);
1253 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1254 down_write(&iinfo->i_data_sem);
1255 udf_clear_extent_cache(inode);
1256 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1257 0x00, bsize - newsize -
1258 udf_file_entry_alloc_offset(inode));
1259 iinfo->i_lenAlloc = newsize;
1260 truncate_setsize(inode, newsize);
1261 up_write(&iinfo->i_data_sem);
1264 err = block_truncate_page(inode->i_mapping, newsize,
1268 truncate_setsize(inode, newsize);
1269 down_write(&iinfo->i_data_sem);
1270 udf_clear_extent_cache(inode);
1271 udf_truncate_extents(inode);
1272 up_write(&iinfo->i_data_sem);
1275 inode->i_mtime = inode->i_ctime = current_time(inode);
1277 udf_sync_inode(inode);
1279 mark_inode_dirty(inode);
1284 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1285 * arbitrary - just that we hopefully don't limit any real use of rewritten
1286 * inode on write-once media but avoid looping for too long on corrupted media.
1288 #define UDF_MAX_ICB_NESTING 1024
1290 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1292 struct buffer_head *bh = NULL;
1293 struct fileEntry *fe;
1294 struct extendedFileEntry *efe;
1296 struct udf_inode_info *iinfo = UDF_I(inode);
1297 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1298 struct kernel_lb_addr *iloc = &iinfo->i_location;
1299 unsigned int link_count;
1300 unsigned int indirections = 0;
1301 int bs = inode->i_sb->s_blocksize;
1305 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1306 udf_debug("partition reference: %d > logical volume partitions: %d\n",
1307 iloc->partitionReferenceNum, sbi->s_partitions);
1311 if (iloc->logicalBlockNum >=
1312 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1313 udf_debug("block=%d, partition=%d out of range\n",
1314 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1319 * Set defaults, but the inode is still incomplete!
1320 * Note: get_new_inode() sets the following on a new inode:
1323 * i_flags = sb->s_flags
1325 * clean_inode(): zero fills and sets
1330 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1332 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1336 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1337 ident != TAG_IDENT_USE) {
1338 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1339 inode->i_ino, ident);
1343 fe = (struct fileEntry *)bh->b_data;
1344 efe = (struct extendedFileEntry *)bh->b_data;
1346 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1347 struct buffer_head *ibh;
1349 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1350 if (ident == TAG_IDENT_IE && ibh) {
1351 struct kernel_lb_addr loc;
1352 struct indirectEntry *ie;
1354 ie = (struct indirectEntry *)ibh->b_data;
1355 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1357 if (ie->indirectICB.extLength) {
1359 memcpy(&iinfo->i_location, &loc,
1360 sizeof(struct kernel_lb_addr));
1361 if (++indirections > UDF_MAX_ICB_NESTING) {
1362 udf_err(inode->i_sb,
1363 "too many ICBs in ICB hierarchy"
1364 " (max %d supported)\n",
1365 UDF_MAX_ICB_NESTING);
1373 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1374 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1375 le16_to_cpu(fe->icbTag.strategyType));
1378 if (fe->icbTag.strategyType == cpu_to_le16(4))
1379 iinfo->i_strat4096 = 0;
1380 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1381 iinfo->i_strat4096 = 1;
1383 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1384 ICBTAG_FLAG_AD_MASK;
1385 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1386 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1387 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1391 iinfo->i_unique = 0;
1392 iinfo->i_lenEAttr = 0;
1393 iinfo->i_lenExtents = 0;
1394 iinfo->i_lenAlloc = 0;
1395 iinfo->i_next_alloc_block = 0;
1396 iinfo->i_next_alloc_goal = 0;
1397 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1400 ret = udf_alloc_i_data(inode, bs -
1401 sizeof(struct extendedFileEntry));
1404 memcpy(iinfo->i_ext.i_data,
1405 bh->b_data + sizeof(struct extendedFileEntry),
1406 bs - sizeof(struct extendedFileEntry));
1407 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1410 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1413 memcpy(iinfo->i_ext.i_data,
1414 bh->b_data + sizeof(struct fileEntry),
1415 bs - sizeof(struct fileEntry));
1416 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1419 iinfo->i_lenAlloc = le32_to_cpu(
1420 ((struct unallocSpaceEntry *)bh->b_data)->
1422 ret = udf_alloc_i_data(inode, bs -
1423 sizeof(struct unallocSpaceEntry));
1426 memcpy(iinfo->i_ext.i_data,
1427 bh->b_data + sizeof(struct unallocSpaceEntry),
1428 bs - sizeof(struct unallocSpaceEntry));
1433 read_lock(&sbi->s_cred_lock);
1434 i_uid_write(inode, le32_to_cpu(fe->uid));
1435 if (!uid_valid(inode->i_uid) ||
1436 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1437 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1438 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1440 i_gid_write(inode, le32_to_cpu(fe->gid));
1441 if (!gid_valid(inode->i_gid) ||
1442 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1443 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1444 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1446 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1447 sbi->s_fmode != UDF_INVALID_MODE)
1448 inode->i_mode = sbi->s_fmode;
1449 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1450 sbi->s_dmode != UDF_INVALID_MODE)
1451 inode->i_mode = sbi->s_dmode;
1453 inode->i_mode = udf_convert_permissions(fe);
1454 inode->i_mode &= ~sbi->s_umask;
1455 read_unlock(&sbi->s_cred_lock);
1457 link_count = le16_to_cpu(fe->fileLinkCount);
1459 if (!hidden_inode) {
1465 set_nlink(inode, link_count);
1467 inode->i_size = le64_to_cpu(fe->informationLength);
1468 iinfo->i_lenExtents = inode->i_size;
1470 if (iinfo->i_efe == 0) {
1471 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1472 (inode->i_sb->s_blocksize_bits - 9);
1474 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1475 inode->i_atime = sbi->s_record_time;
1477 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1478 fe->modificationTime))
1479 inode->i_mtime = sbi->s_record_time;
1481 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1482 inode->i_ctime = sbi->s_record_time;
1484 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1485 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1486 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1487 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1489 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1490 (inode->i_sb->s_blocksize_bits - 9);
1492 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1493 inode->i_atime = sbi->s_record_time;
1495 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1496 efe->modificationTime))
1497 inode->i_mtime = sbi->s_record_time;
1499 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1500 iinfo->i_crtime = sbi->s_record_time;
1502 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1503 inode->i_ctime = sbi->s_record_time;
1505 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1506 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1507 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1508 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1510 inode->i_generation = iinfo->i_unique;
1513 * Sanity check length of allocation descriptors and extended attrs to
1514 * avoid integer overflows
1516 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1518 /* Now do exact checks */
1519 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1521 /* Sanity checks for files in ICB so that we don't get confused later */
1522 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1524 * For file in ICB data is stored in allocation descriptor
1525 * so sizes should match
1527 if (iinfo->i_lenAlloc != inode->i_size)
1529 /* File in ICB has to fit in there... */
1530 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1534 switch (fe->icbTag.fileType) {
1535 case ICBTAG_FILE_TYPE_DIRECTORY:
1536 inode->i_op = &udf_dir_inode_operations;
1537 inode->i_fop = &udf_dir_operations;
1538 inode->i_mode |= S_IFDIR;
1541 case ICBTAG_FILE_TYPE_REALTIME:
1542 case ICBTAG_FILE_TYPE_REGULAR:
1543 case ICBTAG_FILE_TYPE_UNDEF:
1544 case ICBTAG_FILE_TYPE_VAT20:
1545 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1546 inode->i_data.a_ops = &udf_adinicb_aops;
1548 inode->i_data.a_ops = &udf_aops;
1549 inode->i_op = &udf_file_inode_operations;
1550 inode->i_fop = &udf_file_operations;
1551 inode->i_mode |= S_IFREG;
1553 case ICBTAG_FILE_TYPE_BLOCK:
1554 inode->i_mode |= S_IFBLK;
1556 case ICBTAG_FILE_TYPE_CHAR:
1557 inode->i_mode |= S_IFCHR;
1559 case ICBTAG_FILE_TYPE_FIFO:
1560 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1562 case ICBTAG_FILE_TYPE_SOCKET:
1563 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1565 case ICBTAG_FILE_TYPE_SYMLINK:
1566 inode->i_data.a_ops = &udf_symlink_aops;
1567 inode->i_op = &udf_symlink_inode_operations;
1568 inode_nohighmem(inode);
1569 inode->i_mode = S_IFLNK | 0777;
1571 case ICBTAG_FILE_TYPE_MAIN:
1572 udf_debug("METADATA FILE-----\n");
1574 case ICBTAG_FILE_TYPE_MIRROR:
1575 udf_debug("METADATA MIRROR FILE-----\n");
1577 case ICBTAG_FILE_TYPE_BITMAP:
1578 udf_debug("METADATA BITMAP FILE-----\n");
1581 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1582 inode->i_ino, fe->icbTag.fileType);
1585 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1586 struct deviceSpec *dsea =
1587 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1589 init_special_inode(inode, inode->i_mode,
1590 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1591 le32_to_cpu(dsea->minorDeviceIdent)));
1592 /* Developer ID ??? */
1602 static int udf_alloc_i_data(struct inode *inode, size_t size)
1604 struct udf_inode_info *iinfo = UDF_I(inode);
1605 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1606 if (!iinfo->i_ext.i_data)
1611 static umode_t udf_convert_permissions(struct fileEntry *fe)
1614 uint32_t permissions;
1617 permissions = le32_to_cpu(fe->permissions);
1618 flags = le16_to_cpu(fe->icbTag.flags);
1620 mode = ((permissions) & 0007) |
1621 ((permissions >> 2) & 0070) |
1622 ((permissions >> 4) & 0700) |
1623 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1624 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1625 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1630 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1632 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1635 static int udf_sync_inode(struct inode *inode)
1637 return udf_update_inode(inode, 1);
1640 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
1642 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1643 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1644 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1645 iinfo->i_crtime = time;
1648 static int udf_update_inode(struct inode *inode, int do_sync)
1650 struct buffer_head *bh = NULL;
1651 struct fileEntry *fe;
1652 struct extendedFileEntry *efe;
1653 uint64_t lb_recorded;
1658 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1659 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1660 struct udf_inode_info *iinfo = UDF_I(inode);
1662 bh = udf_tgetblk(inode->i_sb,
1663 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1665 udf_debug("getblk failure\n");
1670 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1671 fe = (struct fileEntry *)bh->b_data;
1672 efe = (struct extendedFileEntry *)bh->b_data;
1675 struct unallocSpaceEntry *use =
1676 (struct unallocSpaceEntry *)bh->b_data;
1678 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1679 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1680 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1681 sizeof(struct unallocSpaceEntry));
1682 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1683 crclen = sizeof(struct unallocSpaceEntry);
1688 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1689 fe->uid = cpu_to_le32(-1);
1691 fe->uid = cpu_to_le32(i_uid_read(inode));
1693 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1694 fe->gid = cpu_to_le32(-1);
1696 fe->gid = cpu_to_le32(i_gid_read(inode));
1698 udfperms = ((inode->i_mode & 0007)) |
1699 ((inode->i_mode & 0070) << 2) |
1700 ((inode->i_mode & 0700) << 4);
1702 udfperms |= (le32_to_cpu(fe->permissions) &
1703 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1704 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1705 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1706 fe->permissions = cpu_to_le32(udfperms);
1708 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1709 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1711 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1713 fe->informationLength = cpu_to_le64(inode->i_size);
1715 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1717 struct deviceSpec *dsea =
1718 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1720 dsea = (struct deviceSpec *)
1721 udf_add_extendedattr(inode,
1722 sizeof(struct deviceSpec) +
1723 sizeof(struct regid), 12, 0x3);
1724 dsea->attrType = cpu_to_le32(12);
1725 dsea->attrSubtype = 1;
1726 dsea->attrLength = cpu_to_le32(
1727 sizeof(struct deviceSpec) +
1728 sizeof(struct regid));
1729 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1731 eid = (struct regid *)dsea->impUse;
1732 memset(eid, 0, sizeof(*eid));
1733 strcpy(eid->ident, UDF_ID_DEVELOPER);
1734 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1735 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1736 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1737 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1740 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1741 lb_recorded = 0; /* No extents => no blocks! */
1744 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1745 (blocksize_bits - 9);
1747 if (iinfo->i_efe == 0) {
1748 memcpy(bh->b_data + sizeof(struct fileEntry),
1749 iinfo->i_ext.i_data,
1750 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1751 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1753 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1754 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1755 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1756 memset(&(fe->impIdent), 0, sizeof(struct regid));
1757 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1758 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1759 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1760 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1761 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1762 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1763 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1764 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1765 crclen = sizeof(struct fileEntry);
1767 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1768 iinfo->i_ext.i_data,
1769 inode->i_sb->s_blocksize -
1770 sizeof(struct extendedFileEntry));
1771 efe->objectSize = cpu_to_le64(inode->i_size);
1772 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1774 udf_adjust_time(iinfo, inode->i_atime);
1775 udf_adjust_time(iinfo, inode->i_mtime);
1776 udf_adjust_time(iinfo, inode->i_ctime);
1778 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1779 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1780 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1781 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1783 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1784 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1785 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1786 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1787 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1788 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1789 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1790 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1791 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1792 crclen = sizeof(struct extendedFileEntry);
1796 if (iinfo->i_strat4096) {
1797 fe->icbTag.strategyType = cpu_to_le16(4096);
1798 fe->icbTag.strategyParameter = cpu_to_le16(1);
1799 fe->icbTag.numEntries = cpu_to_le16(2);
1801 fe->icbTag.strategyType = cpu_to_le16(4);
1802 fe->icbTag.numEntries = cpu_to_le16(1);
1806 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1807 else if (S_ISDIR(inode->i_mode))
1808 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1809 else if (S_ISREG(inode->i_mode))
1810 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1811 else if (S_ISLNK(inode->i_mode))
1812 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1813 else if (S_ISBLK(inode->i_mode))
1814 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1815 else if (S_ISCHR(inode->i_mode))
1816 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1817 else if (S_ISFIFO(inode->i_mode))
1818 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1819 else if (S_ISSOCK(inode->i_mode))
1820 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1822 icbflags = iinfo->i_alloc_type |
1823 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1824 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1825 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1826 (le16_to_cpu(fe->icbTag.flags) &
1827 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1828 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1830 fe->icbTag.flags = cpu_to_le16(icbflags);
1831 if (sbi->s_udfrev >= 0x0200)
1832 fe->descTag.descVersion = cpu_to_le16(3);
1834 fe->descTag.descVersion = cpu_to_le16(2);
1835 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1836 fe->descTag.tagLocation = cpu_to_le32(
1837 iinfo->i_location.logicalBlockNum);
1838 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1839 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1840 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1842 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1844 set_buffer_uptodate(bh);
1847 /* write the data blocks */
1848 mark_buffer_dirty(bh);
1850 sync_dirty_buffer(bh);
1851 if (buffer_write_io_error(bh)) {
1852 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1862 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1865 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1866 struct inode *inode = iget_locked(sb, block);
1870 return ERR_PTR(-ENOMEM);
1872 if (!(inode->i_state & I_NEW))
1875 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1876 err = udf_read_inode(inode, hidden_inode);
1879 return ERR_PTR(err);
1881 unlock_new_inode(inode);
1886 int udf_setup_indirect_aext(struct inode *inode, int block,
1887 struct extent_position *epos)
1889 struct super_block *sb = inode->i_sb;
1890 struct buffer_head *bh;
1891 struct allocExtDesc *aed;
1892 struct extent_position nepos;
1893 struct kernel_lb_addr neloc;
1896 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1897 adsize = sizeof(struct short_ad);
1898 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1899 adsize = sizeof(struct long_ad);
1903 neloc.logicalBlockNum = block;
1904 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1906 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1910 memset(bh->b_data, 0x00, sb->s_blocksize);
1911 set_buffer_uptodate(bh);
1913 mark_buffer_dirty_inode(bh, inode);
1915 aed = (struct allocExtDesc *)(bh->b_data);
1916 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1917 aed->previousAllocExtLocation =
1918 cpu_to_le32(epos->block.logicalBlockNum);
1920 aed->lengthAllocDescs = cpu_to_le32(0);
1921 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1925 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1926 sizeof(struct tag));
1928 nepos.block = neloc;
1929 nepos.offset = sizeof(struct allocExtDesc);
1933 * Do we have to copy current last extent to make space for indirect
1936 if (epos->offset + adsize > sb->s_blocksize) {
1937 struct kernel_lb_addr cp_loc;
1941 epos->offset -= adsize;
1942 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1943 cp_len |= ((uint32_t)cp_type) << 30;
1945 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1946 udf_write_aext(inode, epos, &nepos.block,
1947 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1949 __udf_add_aext(inode, epos, &nepos.block,
1950 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1960 * Append extent at the given position - should be the first free one in inode
1961 * / indirect extent. This function assumes there is enough space in the inode
1962 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1964 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1965 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1967 struct udf_inode_info *iinfo = UDF_I(inode);
1968 struct allocExtDesc *aed;
1971 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1972 adsize = sizeof(struct short_ad);
1973 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1974 adsize = sizeof(struct long_ad);
1979 WARN_ON(iinfo->i_lenAlloc !=
1980 epos->offset - udf_file_entry_alloc_offset(inode));
1982 aed = (struct allocExtDesc *)epos->bh->b_data;
1983 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1984 epos->offset - sizeof(struct allocExtDesc));
1985 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1988 udf_write_aext(inode, epos, eloc, elen, inc);
1991 iinfo->i_lenAlloc += adsize;
1992 mark_inode_dirty(inode);
1994 aed = (struct allocExtDesc *)epos->bh->b_data;
1995 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1996 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1997 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1998 udf_update_tag(epos->bh->b_data,
1999 epos->offset + (inc ? 0 : adsize));
2001 udf_update_tag(epos->bh->b_data,
2002 sizeof(struct allocExtDesc));
2003 mark_buffer_dirty_inode(epos->bh, inode);
2010 * Append extent at given position - should be the first free one in inode
2011 * / indirect extent. Takes care of allocating and linking indirect blocks.
2013 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2014 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2017 struct super_block *sb = inode->i_sb;
2019 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2020 adsize = sizeof(struct short_ad);
2021 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2022 adsize = sizeof(struct long_ad);
2026 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2030 new_block = udf_new_block(sb, NULL,
2031 epos->block.partitionReferenceNum,
2032 epos->block.logicalBlockNum, &err);
2036 err = udf_setup_indirect_aext(inode, new_block, epos);
2041 return __udf_add_aext(inode, epos, eloc, elen, inc);
2044 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2045 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2049 struct short_ad *sad;
2050 struct long_ad *lad;
2051 struct udf_inode_info *iinfo = UDF_I(inode);
2054 ptr = iinfo->i_ext.i_data + epos->offset -
2055 udf_file_entry_alloc_offset(inode) +
2058 ptr = epos->bh->b_data + epos->offset;
2060 switch (iinfo->i_alloc_type) {
2061 case ICBTAG_FLAG_AD_SHORT:
2062 sad = (struct short_ad *)ptr;
2063 sad->extLength = cpu_to_le32(elen);
2064 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2065 adsize = sizeof(struct short_ad);
2067 case ICBTAG_FLAG_AD_LONG:
2068 lad = (struct long_ad *)ptr;
2069 lad->extLength = cpu_to_le32(elen);
2070 lad->extLocation = cpu_to_lelb(*eloc);
2071 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2072 adsize = sizeof(struct long_ad);
2079 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2080 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2081 struct allocExtDesc *aed =
2082 (struct allocExtDesc *)epos->bh->b_data;
2083 udf_update_tag(epos->bh->b_data,
2084 le32_to_cpu(aed->lengthAllocDescs) +
2085 sizeof(struct allocExtDesc));
2087 mark_buffer_dirty_inode(epos->bh, inode);
2089 mark_inode_dirty(inode);
2093 epos->offset += adsize;
2097 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2098 * someone does some weird stuff.
2100 #define UDF_MAX_INDIR_EXTS 16
2102 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2103 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2106 unsigned int indirections = 0;
2108 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2109 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2112 if (++indirections > UDF_MAX_INDIR_EXTS) {
2113 udf_err(inode->i_sb,
2114 "too many indirect extents in inode %lu\n",
2119 epos->block = *eloc;
2120 epos->offset = sizeof(struct allocExtDesc);
2122 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2123 epos->bh = udf_tread(inode->i_sb, block);
2125 udf_debug("reading block %d failed!\n", block);
2133 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2134 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2139 struct short_ad *sad;
2140 struct long_ad *lad;
2141 struct udf_inode_info *iinfo = UDF_I(inode);
2145 epos->offset = udf_file_entry_alloc_offset(inode);
2146 ptr = iinfo->i_ext.i_data + epos->offset -
2147 udf_file_entry_alloc_offset(inode) +
2149 alen = udf_file_entry_alloc_offset(inode) +
2153 epos->offset = sizeof(struct allocExtDesc);
2154 ptr = epos->bh->b_data + epos->offset;
2155 alen = sizeof(struct allocExtDesc) +
2156 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2160 switch (iinfo->i_alloc_type) {
2161 case ICBTAG_FLAG_AD_SHORT:
2162 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2165 etype = le32_to_cpu(sad->extLength) >> 30;
2166 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2167 eloc->partitionReferenceNum =
2168 iinfo->i_location.partitionReferenceNum;
2169 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2171 case ICBTAG_FLAG_AD_LONG:
2172 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2175 etype = le32_to_cpu(lad->extLength) >> 30;
2176 *eloc = lelb_to_cpu(lad->extLocation);
2177 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2180 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2187 static int udf_insert_aext(struct inode *inode, struct extent_position epos,
2188 struct kernel_lb_addr neloc, uint32_t nelen)
2190 struct kernel_lb_addr oeloc;
2198 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2199 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2201 nelen = (etype << 30) | oelen;
2203 err = udf_add_aext(inode, &epos, &neloc, nelen, 1);
2209 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
2211 struct extent_position oepos;
2214 struct allocExtDesc *aed;
2215 struct udf_inode_info *iinfo;
2216 struct kernel_lb_addr eloc;
2224 iinfo = UDF_I(inode);
2225 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2226 adsize = sizeof(struct short_ad);
2227 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2228 adsize = sizeof(struct long_ad);
2233 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2236 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2237 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2238 if (oepos.bh != epos.bh) {
2239 oepos.block = epos.block;
2243 oepos.offset = epos.offset - adsize;
2246 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2249 if (epos.bh != oepos.bh) {
2250 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2251 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2252 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2254 iinfo->i_lenAlloc -= (adsize * 2);
2255 mark_inode_dirty(inode);
2257 aed = (struct allocExtDesc *)oepos.bh->b_data;
2258 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2259 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2260 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2261 udf_update_tag(oepos.bh->b_data,
2262 oepos.offset - (2 * adsize));
2264 udf_update_tag(oepos.bh->b_data,
2265 sizeof(struct allocExtDesc));
2266 mark_buffer_dirty_inode(oepos.bh, inode);
2269 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2271 iinfo->i_lenAlloc -= adsize;
2272 mark_inode_dirty(inode);
2274 aed = (struct allocExtDesc *)oepos.bh->b_data;
2275 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2276 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2277 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2278 udf_update_tag(oepos.bh->b_data,
2279 epos.offset - adsize);
2281 udf_update_tag(oepos.bh->b_data,
2282 sizeof(struct allocExtDesc));
2283 mark_buffer_dirty_inode(oepos.bh, inode);
2290 return (elen >> 30);
2293 int8_t inode_bmap(struct inode *inode, sector_t block,
2294 struct extent_position *pos, struct kernel_lb_addr *eloc,
2295 uint32_t *elen, sector_t *offset)
2297 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2298 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2300 struct udf_inode_info *iinfo;
2302 iinfo = UDF_I(inode);
2303 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2305 pos->block = iinfo->i_location;
2310 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2312 *offset = (bcount - lbcount) >> blocksize_bits;
2313 iinfo->i_lenExtents = lbcount;
2317 } while (lbcount <= bcount);
2318 /* update extent cache */
2319 udf_update_extent_cache(inode, lbcount - *elen, pos);
2320 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2325 long udf_block_map(struct inode *inode, sector_t block)
2327 struct kernel_lb_addr eloc;
2330 struct extent_position epos = {};
2333 down_read(&UDF_I(inode)->i_data_sem);
2335 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2336 (EXT_RECORDED_ALLOCATED >> 30))
2337 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2341 up_read(&UDF_I(inode)->i_data_sem);
2344 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2345 return udf_fixed_to_variable(ret);