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 int8_t 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 void 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 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
888 newblock = udf_get_pblock(inode->i_sb, newblocknum,
889 iinfo->i_location.partitionReferenceNum, 0);
895 iinfo->i_next_alloc_block = block;
896 iinfo->i_next_alloc_goal = newblocknum;
897 inode->i_ctime = current_time(inode);
900 udf_sync_inode(inode);
902 mark_inode_dirty(inode);
904 brelse(prev_epos.bh);
906 brelse(next_epos.bh);
910 static void udf_split_extents(struct inode *inode, int *c, int offset,
911 int newblocknum, struct kernel_long_ad *laarr,
914 unsigned long blocksize = inode->i_sb->s_blocksize;
915 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
917 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
918 (laarr[*c].extLength >> 30) ==
919 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
921 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
922 blocksize - 1) >> blocksize_bits;
923 int8_t etype = (laarr[curr].extLength >> 30);
927 else if (!offset || blen == offset + 1) {
928 laarr[curr + 2] = laarr[curr + 1];
929 laarr[curr + 1] = laarr[curr];
931 laarr[curr + 3] = laarr[curr + 1];
932 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
936 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
937 udf_free_blocks(inode->i_sb, inode,
938 &laarr[curr].extLocation,
940 laarr[curr].extLength =
941 EXT_NOT_RECORDED_NOT_ALLOCATED |
942 (offset << blocksize_bits);
943 laarr[curr].extLocation.logicalBlockNum = 0;
944 laarr[curr].extLocation.
945 partitionReferenceNum = 0;
947 laarr[curr].extLength = (etype << 30) |
948 (offset << blocksize_bits);
954 laarr[curr].extLocation.logicalBlockNum = newblocknum;
955 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
956 laarr[curr].extLocation.partitionReferenceNum =
957 UDF_I(inode)->i_location.partitionReferenceNum;
958 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
962 if (blen != offset + 1) {
963 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
964 laarr[curr].extLocation.logicalBlockNum +=
966 laarr[curr].extLength = (etype << 30) |
967 ((blen - (offset + 1)) << blocksize_bits);
974 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
975 struct kernel_long_ad *laarr,
978 int start, length = 0, currlength = 0, i;
980 if (*endnum >= (c + 1)) {
986 if ((laarr[c + 1].extLength >> 30) ==
987 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
989 length = currlength =
990 (((laarr[c + 1].extLength &
991 UDF_EXTENT_LENGTH_MASK) +
992 inode->i_sb->s_blocksize - 1) >>
993 inode->i_sb->s_blocksize_bits);
998 for (i = start + 1; i <= *endnum; i++) {
1001 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1002 } else if ((laarr[i].extLength >> 30) ==
1003 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1004 length += (((laarr[i].extLength &
1005 UDF_EXTENT_LENGTH_MASK) +
1006 inode->i_sb->s_blocksize - 1) >>
1007 inode->i_sb->s_blocksize_bits);
1013 int next = laarr[start].extLocation.logicalBlockNum +
1014 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1015 inode->i_sb->s_blocksize - 1) >>
1016 inode->i_sb->s_blocksize_bits);
1017 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1018 laarr[start].extLocation.partitionReferenceNum,
1019 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1020 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1023 if (start == (c + 1))
1024 laarr[start].extLength +=
1026 inode->i_sb->s_blocksize_bits);
1028 memmove(&laarr[c + 2], &laarr[c + 1],
1029 sizeof(struct long_ad) * (*endnum - (c + 1)));
1031 laarr[c + 1].extLocation.logicalBlockNum = next;
1032 laarr[c + 1].extLocation.partitionReferenceNum =
1033 laarr[c].extLocation.
1034 partitionReferenceNum;
1035 laarr[c + 1].extLength =
1036 EXT_NOT_RECORDED_ALLOCATED |
1038 inode->i_sb->s_blocksize_bits);
1042 for (i = start + 1; numalloc && i < *endnum; i++) {
1043 int elen = ((laarr[i].extLength &
1044 UDF_EXTENT_LENGTH_MASK) +
1045 inode->i_sb->s_blocksize - 1) >>
1046 inode->i_sb->s_blocksize_bits;
1048 if (elen > numalloc) {
1049 laarr[i].extLength -=
1051 inode->i_sb->s_blocksize_bits);
1055 if (*endnum > (i + 1))
1058 sizeof(struct long_ad) *
1059 (*endnum - (i + 1)));
1064 UDF_I(inode)->i_lenExtents +=
1065 numalloc << inode->i_sb->s_blocksize_bits;
1070 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1074 unsigned long blocksize = inode->i_sb->s_blocksize;
1075 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1077 for (i = 0; i < (*endnum - 1); i++) {
1078 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1079 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1081 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1082 (((li->extLength >> 30) ==
1083 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1084 ((lip1->extLocation.logicalBlockNum -
1085 li->extLocation.logicalBlockNum) ==
1086 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1087 blocksize - 1) >> blocksize_bits)))) {
1089 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1090 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1091 blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
1092 li->extLength = lip1->extLength +
1094 UDF_EXTENT_LENGTH_MASK) +
1095 blocksize - 1) & ~(blocksize - 1));
1096 if (*endnum > (i + 2))
1097 memmove(&laarr[i + 1], &laarr[i + 2],
1098 sizeof(struct long_ad) *
1099 (*endnum - (i + 2)));
1103 } else if (((li->extLength >> 30) ==
1104 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1105 ((lip1->extLength >> 30) ==
1106 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1107 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1109 UDF_EXTENT_LENGTH_MASK) +
1110 blocksize - 1) >> blocksize_bits);
1111 li->extLocation.logicalBlockNum = 0;
1112 li->extLocation.partitionReferenceNum = 0;
1114 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1115 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1116 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1117 lip1->extLength = (lip1->extLength -
1119 UDF_EXTENT_LENGTH_MASK) +
1120 UDF_EXTENT_LENGTH_MASK) &
1122 li->extLength = (li->extLength &
1123 UDF_EXTENT_FLAG_MASK) +
1124 (UDF_EXTENT_LENGTH_MASK + 1) -
1127 li->extLength = lip1->extLength +
1129 UDF_EXTENT_LENGTH_MASK) +
1130 blocksize - 1) & ~(blocksize - 1));
1131 if (*endnum > (i + 2))
1132 memmove(&laarr[i + 1], &laarr[i + 2],
1133 sizeof(struct long_ad) *
1134 (*endnum - (i + 2)));
1138 } else if ((li->extLength >> 30) ==
1139 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1140 udf_free_blocks(inode->i_sb, inode,
1141 &li->extLocation, 0,
1143 UDF_EXTENT_LENGTH_MASK) +
1144 blocksize - 1) >> blocksize_bits);
1145 li->extLocation.logicalBlockNum = 0;
1146 li->extLocation.partitionReferenceNum = 0;
1147 li->extLength = (li->extLength &
1148 UDF_EXTENT_LENGTH_MASK) |
1149 EXT_NOT_RECORDED_NOT_ALLOCATED;
1154 static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1155 int startnum, int endnum,
1156 struct extent_position *epos)
1159 struct kernel_lb_addr tmploc;
1162 if (startnum > endnum) {
1163 for (i = 0; i < (startnum - endnum); i++)
1164 udf_delete_aext(inode, *epos);
1165 } else if (startnum < endnum) {
1166 for (i = 0; i < (endnum - startnum); i++) {
1167 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1168 laarr[i].extLength);
1169 udf_next_aext(inode, epos, &laarr[i].extLocation,
1170 &laarr[i].extLength, 1);
1175 for (i = start; i < endnum; i++) {
1176 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1177 udf_write_aext(inode, epos, &laarr[i].extLocation,
1178 laarr[i].extLength, 1);
1182 struct buffer_head *udf_bread(struct inode *inode, int block,
1183 int create, int *err)
1185 struct buffer_head *bh = NULL;
1187 bh = udf_getblk(inode, block, create, err);
1191 if (buffer_uptodate(bh))
1194 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1197 if (buffer_uptodate(bh))
1205 int udf_setsize(struct inode *inode, loff_t newsize)
1208 struct udf_inode_info *iinfo;
1209 int bsize = i_blocksize(inode);
1211 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1212 S_ISLNK(inode->i_mode)))
1214 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1217 iinfo = UDF_I(inode);
1218 if (newsize > inode->i_size) {
1219 down_write(&iinfo->i_data_sem);
1220 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1222 (udf_file_entry_alloc_offset(inode) + newsize)) {
1223 err = udf_expand_file_adinicb(inode);
1226 down_write(&iinfo->i_data_sem);
1228 iinfo->i_lenAlloc = newsize;
1232 err = udf_extend_file(inode, newsize);
1234 up_write(&iinfo->i_data_sem);
1238 up_write(&iinfo->i_data_sem);
1239 truncate_setsize(inode, newsize);
1241 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1242 down_write(&iinfo->i_data_sem);
1243 udf_clear_extent_cache(inode);
1244 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1245 0x00, bsize - newsize -
1246 udf_file_entry_alloc_offset(inode));
1247 iinfo->i_lenAlloc = newsize;
1248 truncate_setsize(inode, newsize);
1249 up_write(&iinfo->i_data_sem);
1252 err = block_truncate_page(inode->i_mapping, newsize,
1256 truncate_setsize(inode, newsize);
1257 down_write(&iinfo->i_data_sem);
1258 udf_clear_extent_cache(inode);
1259 udf_truncate_extents(inode);
1260 up_write(&iinfo->i_data_sem);
1263 inode->i_mtime = inode->i_ctime = current_time(inode);
1265 udf_sync_inode(inode);
1267 mark_inode_dirty(inode);
1272 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1273 * arbitrary - just that we hopefully don't limit any real use of rewritten
1274 * inode on write-once media but avoid looping for too long on corrupted media.
1276 #define UDF_MAX_ICB_NESTING 1024
1278 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1280 struct buffer_head *bh = NULL;
1281 struct fileEntry *fe;
1282 struct extendedFileEntry *efe;
1284 struct udf_inode_info *iinfo = UDF_I(inode);
1285 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1286 struct kernel_lb_addr *iloc = &iinfo->i_location;
1287 unsigned int link_count;
1288 unsigned int indirections = 0;
1289 int bs = inode->i_sb->s_blocksize;
1293 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1294 udf_debug("partition reference: %d > logical volume partitions: %d\n",
1295 iloc->partitionReferenceNum, sbi->s_partitions);
1299 if (iloc->logicalBlockNum >=
1300 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1301 udf_debug("block=%d, partition=%d out of range\n",
1302 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1307 * Set defaults, but the inode is still incomplete!
1308 * Note: get_new_inode() sets the following on a new inode:
1311 * i_flags = sb->s_flags
1313 * clean_inode(): zero fills and sets
1318 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1320 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1324 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1325 ident != TAG_IDENT_USE) {
1326 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1327 inode->i_ino, ident);
1331 fe = (struct fileEntry *)bh->b_data;
1332 efe = (struct extendedFileEntry *)bh->b_data;
1334 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1335 struct buffer_head *ibh;
1337 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1338 if (ident == TAG_IDENT_IE && ibh) {
1339 struct kernel_lb_addr loc;
1340 struct indirectEntry *ie;
1342 ie = (struct indirectEntry *)ibh->b_data;
1343 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1345 if (ie->indirectICB.extLength) {
1347 memcpy(&iinfo->i_location, &loc,
1348 sizeof(struct kernel_lb_addr));
1349 if (++indirections > UDF_MAX_ICB_NESTING) {
1350 udf_err(inode->i_sb,
1351 "too many ICBs in ICB hierarchy"
1352 " (max %d supported)\n",
1353 UDF_MAX_ICB_NESTING);
1361 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1362 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1363 le16_to_cpu(fe->icbTag.strategyType));
1366 if (fe->icbTag.strategyType == cpu_to_le16(4))
1367 iinfo->i_strat4096 = 0;
1368 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1369 iinfo->i_strat4096 = 1;
1371 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1372 ICBTAG_FLAG_AD_MASK;
1373 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1374 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1375 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1379 iinfo->i_unique = 0;
1380 iinfo->i_lenEAttr = 0;
1381 iinfo->i_lenExtents = 0;
1382 iinfo->i_lenAlloc = 0;
1383 iinfo->i_next_alloc_block = 0;
1384 iinfo->i_next_alloc_goal = 0;
1385 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1388 ret = udf_alloc_i_data(inode, bs -
1389 sizeof(struct extendedFileEntry));
1392 memcpy(iinfo->i_ext.i_data,
1393 bh->b_data + sizeof(struct extendedFileEntry),
1394 bs - sizeof(struct extendedFileEntry));
1395 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1398 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1401 memcpy(iinfo->i_ext.i_data,
1402 bh->b_data + sizeof(struct fileEntry),
1403 bs - sizeof(struct fileEntry));
1404 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1407 iinfo->i_lenAlloc = le32_to_cpu(
1408 ((struct unallocSpaceEntry *)bh->b_data)->
1410 ret = udf_alloc_i_data(inode, bs -
1411 sizeof(struct unallocSpaceEntry));
1414 memcpy(iinfo->i_ext.i_data,
1415 bh->b_data + sizeof(struct unallocSpaceEntry),
1416 bs - sizeof(struct unallocSpaceEntry));
1421 read_lock(&sbi->s_cred_lock);
1422 i_uid_write(inode, le32_to_cpu(fe->uid));
1423 if (!uid_valid(inode->i_uid) ||
1424 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1425 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1426 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1428 i_gid_write(inode, le32_to_cpu(fe->gid));
1429 if (!gid_valid(inode->i_gid) ||
1430 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1431 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1432 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1434 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1435 sbi->s_fmode != UDF_INVALID_MODE)
1436 inode->i_mode = sbi->s_fmode;
1437 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1438 sbi->s_dmode != UDF_INVALID_MODE)
1439 inode->i_mode = sbi->s_dmode;
1441 inode->i_mode = udf_convert_permissions(fe);
1442 inode->i_mode &= ~sbi->s_umask;
1443 read_unlock(&sbi->s_cred_lock);
1445 link_count = le16_to_cpu(fe->fileLinkCount);
1447 if (!hidden_inode) {
1453 set_nlink(inode, link_count);
1455 inode->i_size = le64_to_cpu(fe->informationLength);
1456 iinfo->i_lenExtents = inode->i_size;
1458 if (iinfo->i_efe == 0) {
1459 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1460 (inode->i_sb->s_blocksize_bits - 9);
1462 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1463 inode->i_atime = sbi->s_record_time;
1465 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1466 fe->modificationTime))
1467 inode->i_mtime = sbi->s_record_time;
1469 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1470 inode->i_ctime = sbi->s_record_time;
1472 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1473 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1474 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1475 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1477 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1478 (inode->i_sb->s_blocksize_bits - 9);
1480 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1481 inode->i_atime = sbi->s_record_time;
1483 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1484 efe->modificationTime))
1485 inode->i_mtime = sbi->s_record_time;
1487 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1488 iinfo->i_crtime = sbi->s_record_time;
1490 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1491 inode->i_ctime = sbi->s_record_time;
1493 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1494 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1495 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1496 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1498 inode->i_generation = iinfo->i_unique;
1501 * Sanity check length of allocation descriptors and extended attrs to
1502 * avoid integer overflows
1504 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1506 /* Now do exact checks */
1507 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1509 /* Sanity checks for files in ICB so that we don't get confused later */
1510 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1512 * For file in ICB data is stored in allocation descriptor
1513 * so sizes should match
1515 if (iinfo->i_lenAlloc != inode->i_size)
1517 /* File in ICB has to fit in there... */
1518 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1522 switch (fe->icbTag.fileType) {
1523 case ICBTAG_FILE_TYPE_DIRECTORY:
1524 inode->i_op = &udf_dir_inode_operations;
1525 inode->i_fop = &udf_dir_operations;
1526 inode->i_mode |= S_IFDIR;
1529 case ICBTAG_FILE_TYPE_REALTIME:
1530 case ICBTAG_FILE_TYPE_REGULAR:
1531 case ICBTAG_FILE_TYPE_UNDEF:
1532 case ICBTAG_FILE_TYPE_VAT20:
1533 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1534 inode->i_data.a_ops = &udf_adinicb_aops;
1536 inode->i_data.a_ops = &udf_aops;
1537 inode->i_op = &udf_file_inode_operations;
1538 inode->i_fop = &udf_file_operations;
1539 inode->i_mode |= S_IFREG;
1541 case ICBTAG_FILE_TYPE_BLOCK:
1542 inode->i_mode |= S_IFBLK;
1544 case ICBTAG_FILE_TYPE_CHAR:
1545 inode->i_mode |= S_IFCHR;
1547 case ICBTAG_FILE_TYPE_FIFO:
1548 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1550 case ICBTAG_FILE_TYPE_SOCKET:
1551 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1553 case ICBTAG_FILE_TYPE_SYMLINK:
1554 inode->i_data.a_ops = &udf_symlink_aops;
1555 inode->i_op = &udf_symlink_inode_operations;
1556 inode_nohighmem(inode);
1557 inode->i_mode = S_IFLNK | 0777;
1559 case ICBTAG_FILE_TYPE_MAIN:
1560 udf_debug("METADATA FILE-----\n");
1562 case ICBTAG_FILE_TYPE_MIRROR:
1563 udf_debug("METADATA MIRROR FILE-----\n");
1565 case ICBTAG_FILE_TYPE_BITMAP:
1566 udf_debug("METADATA BITMAP FILE-----\n");
1569 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1570 inode->i_ino, fe->icbTag.fileType);
1573 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1574 struct deviceSpec *dsea =
1575 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1577 init_special_inode(inode, inode->i_mode,
1578 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1579 le32_to_cpu(dsea->minorDeviceIdent)));
1580 /* Developer ID ??? */
1590 static int udf_alloc_i_data(struct inode *inode, size_t size)
1592 struct udf_inode_info *iinfo = UDF_I(inode);
1593 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1594 if (!iinfo->i_ext.i_data)
1599 static umode_t udf_convert_permissions(struct fileEntry *fe)
1602 uint32_t permissions;
1605 permissions = le32_to_cpu(fe->permissions);
1606 flags = le16_to_cpu(fe->icbTag.flags);
1608 mode = ((permissions) & 0007) |
1609 ((permissions >> 2) & 0070) |
1610 ((permissions >> 4) & 0700) |
1611 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1612 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1613 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1618 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1620 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1623 static int udf_sync_inode(struct inode *inode)
1625 return udf_update_inode(inode, 1);
1628 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec time)
1630 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1631 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1632 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1633 iinfo->i_crtime = time;
1636 static int udf_update_inode(struct inode *inode, int do_sync)
1638 struct buffer_head *bh = NULL;
1639 struct fileEntry *fe;
1640 struct extendedFileEntry *efe;
1641 uint64_t lb_recorded;
1646 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1647 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1648 struct udf_inode_info *iinfo = UDF_I(inode);
1650 bh = udf_tgetblk(inode->i_sb,
1651 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1653 udf_debug("getblk failure\n");
1658 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1659 fe = (struct fileEntry *)bh->b_data;
1660 efe = (struct extendedFileEntry *)bh->b_data;
1663 struct unallocSpaceEntry *use =
1664 (struct unallocSpaceEntry *)bh->b_data;
1666 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1667 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1668 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1669 sizeof(struct unallocSpaceEntry));
1670 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1671 crclen = sizeof(struct unallocSpaceEntry);
1676 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1677 fe->uid = cpu_to_le32(-1);
1679 fe->uid = cpu_to_le32(i_uid_read(inode));
1681 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1682 fe->gid = cpu_to_le32(-1);
1684 fe->gid = cpu_to_le32(i_gid_read(inode));
1686 udfperms = ((inode->i_mode & 0007)) |
1687 ((inode->i_mode & 0070) << 2) |
1688 ((inode->i_mode & 0700) << 4);
1690 udfperms |= (le32_to_cpu(fe->permissions) &
1691 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1692 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1693 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1694 fe->permissions = cpu_to_le32(udfperms);
1696 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1697 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1699 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1701 fe->informationLength = cpu_to_le64(inode->i_size);
1703 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1705 struct deviceSpec *dsea =
1706 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1708 dsea = (struct deviceSpec *)
1709 udf_add_extendedattr(inode,
1710 sizeof(struct deviceSpec) +
1711 sizeof(struct regid), 12, 0x3);
1712 dsea->attrType = cpu_to_le32(12);
1713 dsea->attrSubtype = 1;
1714 dsea->attrLength = cpu_to_le32(
1715 sizeof(struct deviceSpec) +
1716 sizeof(struct regid));
1717 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1719 eid = (struct regid *)dsea->impUse;
1720 memset(eid, 0, sizeof(*eid));
1721 strcpy(eid->ident, UDF_ID_DEVELOPER);
1722 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1723 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1724 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1725 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1728 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1729 lb_recorded = 0; /* No extents => no blocks! */
1732 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1733 (blocksize_bits - 9);
1735 if (iinfo->i_efe == 0) {
1736 memcpy(bh->b_data + sizeof(struct fileEntry),
1737 iinfo->i_ext.i_data,
1738 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1739 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1741 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1742 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1743 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1744 memset(&(fe->impIdent), 0, sizeof(struct regid));
1745 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1746 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1747 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1748 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1749 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1750 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1751 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1752 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1753 crclen = sizeof(struct fileEntry);
1755 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1756 iinfo->i_ext.i_data,
1757 inode->i_sb->s_blocksize -
1758 sizeof(struct extendedFileEntry));
1759 efe->objectSize = cpu_to_le64(inode->i_size);
1760 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1762 udf_adjust_time(iinfo, inode->i_atime);
1763 udf_adjust_time(iinfo, inode->i_mtime);
1764 udf_adjust_time(iinfo, inode->i_ctime);
1766 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1767 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1768 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1769 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1771 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1772 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1773 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1774 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1775 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1776 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1777 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1778 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1779 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1780 crclen = sizeof(struct extendedFileEntry);
1784 if (iinfo->i_strat4096) {
1785 fe->icbTag.strategyType = cpu_to_le16(4096);
1786 fe->icbTag.strategyParameter = cpu_to_le16(1);
1787 fe->icbTag.numEntries = cpu_to_le16(2);
1789 fe->icbTag.strategyType = cpu_to_le16(4);
1790 fe->icbTag.numEntries = cpu_to_le16(1);
1794 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1795 else if (S_ISDIR(inode->i_mode))
1796 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1797 else if (S_ISREG(inode->i_mode))
1798 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1799 else if (S_ISLNK(inode->i_mode))
1800 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1801 else if (S_ISBLK(inode->i_mode))
1802 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1803 else if (S_ISCHR(inode->i_mode))
1804 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1805 else if (S_ISFIFO(inode->i_mode))
1806 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1807 else if (S_ISSOCK(inode->i_mode))
1808 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1810 icbflags = iinfo->i_alloc_type |
1811 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1812 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1813 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1814 (le16_to_cpu(fe->icbTag.flags) &
1815 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1816 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1818 fe->icbTag.flags = cpu_to_le16(icbflags);
1819 if (sbi->s_udfrev >= 0x0200)
1820 fe->descTag.descVersion = cpu_to_le16(3);
1822 fe->descTag.descVersion = cpu_to_le16(2);
1823 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1824 fe->descTag.tagLocation = cpu_to_le32(
1825 iinfo->i_location.logicalBlockNum);
1826 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1827 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1828 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1830 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1832 set_buffer_uptodate(bh);
1835 /* write the data blocks */
1836 mark_buffer_dirty(bh);
1838 sync_dirty_buffer(bh);
1839 if (buffer_write_io_error(bh)) {
1840 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1850 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1853 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1854 struct inode *inode = iget_locked(sb, block);
1858 return ERR_PTR(-ENOMEM);
1860 if (!(inode->i_state & I_NEW))
1863 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1864 err = udf_read_inode(inode, hidden_inode);
1867 return ERR_PTR(err);
1869 unlock_new_inode(inode);
1874 int udf_setup_indirect_aext(struct inode *inode, int block,
1875 struct extent_position *epos)
1877 struct super_block *sb = inode->i_sb;
1878 struct buffer_head *bh;
1879 struct allocExtDesc *aed;
1880 struct extent_position nepos;
1881 struct kernel_lb_addr neloc;
1884 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1885 adsize = sizeof(struct short_ad);
1886 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1887 adsize = sizeof(struct long_ad);
1891 neloc.logicalBlockNum = block;
1892 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1894 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1898 memset(bh->b_data, 0x00, sb->s_blocksize);
1899 set_buffer_uptodate(bh);
1901 mark_buffer_dirty_inode(bh, inode);
1903 aed = (struct allocExtDesc *)(bh->b_data);
1904 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1905 aed->previousAllocExtLocation =
1906 cpu_to_le32(epos->block.logicalBlockNum);
1908 aed->lengthAllocDescs = cpu_to_le32(0);
1909 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1913 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1914 sizeof(struct tag));
1916 nepos.block = neloc;
1917 nepos.offset = sizeof(struct allocExtDesc);
1921 * Do we have to copy current last extent to make space for indirect
1924 if (epos->offset + adsize > sb->s_blocksize) {
1925 struct kernel_lb_addr cp_loc;
1929 epos->offset -= adsize;
1930 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1931 cp_len |= ((uint32_t)cp_type) << 30;
1933 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1934 udf_write_aext(inode, epos, &nepos.block,
1935 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1937 __udf_add_aext(inode, epos, &nepos.block,
1938 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1948 * Append extent at the given position - should be the first free one in inode
1949 * / indirect extent. This function assumes there is enough space in the inode
1950 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1952 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1953 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1955 struct udf_inode_info *iinfo = UDF_I(inode);
1956 struct allocExtDesc *aed;
1959 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1960 adsize = sizeof(struct short_ad);
1961 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1962 adsize = sizeof(struct long_ad);
1967 WARN_ON(iinfo->i_lenAlloc !=
1968 epos->offset - udf_file_entry_alloc_offset(inode));
1970 aed = (struct allocExtDesc *)epos->bh->b_data;
1971 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1972 epos->offset - sizeof(struct allocExtDesc));
1973 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1976 udf_write_aext(inode, epos, eloc, elen, inc);
1979 iinfo->i_lenAlloc += adsize;
1980 mark_inode_dirty(inode);
1982 aed = (struct allocExtDesc *)epos->bh->b_data;
1983 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1984 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1985 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1986 udf_update_tag(epos->bh->b_data,
1987 epos->offset + (inc ? 0 : adsize));
1989 udf_update_tag(epos->bh->b_data,
1990 sizeof(struct allocExtDesc));
1991 mark_buffer_dirty_inode(epos->bh, inode);
1998 * Append extent at given position - should be the first free one in inode
1999 * / indirect extent. Takes care of allocating and linking indirect blocks.
2001 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2002 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2005 struct super_block *sb = inode->i_sb;
2007 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2008 adsize = sizeof(struct short_ad);
2009 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2010 adsize = sizeof(struct long_ad);
2014 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2018 new_block = udf_new_block(sb, NULL,
2019 epos->block.partitionReferenceNum,
2020 epos->block.logicalBlockNum, &err);
2024 err = udf_setup_indirect_aext(inode, new_block, epos);
2029 return __udf_add_aext(inode, epos, eloc, elen, inc);
2032 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2033 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2037 struct short_ad *sad;
2038 struct long_ad *lad;
2039 struct udf_inode_info *iinfo = UDF_I(inode);
2042 ptr = iinfo->i_ext.i_data + epos->offset -
2043 udf_file_entry_alloc_offset(inode) +
2046 ptr = epos->bh->b_data + epos->offset;
2048 switch (iinfo->i_alloc_type) {
2049 case ICBTAG_FLAG_AD_SHORT:
2050 sad = (struct short_ad *)ptr;
2051 sad->extLength = cpu_to_le32(elen);
2052 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2053 adsize = sizeof(struct short_ad);
2055 case ICBTAG_FLAG_AD_LONG:
2056 lad = (struct long_ad *)ptr;
2057 lad->extLength = cpu_to_le32(elen);
2058 lad->extLocation = cpu_to_lelb(*eloc);
2059 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2060 adsize = sizeof(struct long_ad);
2067 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2068 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2069 struct allocExtDesc *aed =
2070 (struct allocExtDesc *)epos->bh->b_data;
2071 udf_update_tag(epos->bh->b_data,
2072 le32_to_cpu(aed->lengthAllocDescs) +
2073 sizeof(struct allocExtDesc));
2075 mark_buffer_dirty_inode(epos->bh, inode);
2077 mark_inode_dirty(inode);
2081 epos->offset += adsize;
2085 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2086 * someone does some weird stuff.
2088 #define UDF_MAX_INDIR_EXTS 16
2090 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2091 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2094 unsigned int indirections = 0;
2096 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2097 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2100 if (++indirections > UDF_MAX_INDIR_EXTS) {
2101 udf_err(inode->i_sb,
2102 "too many indirect extents in inode %lu\n",
2107 epos->block = *eloc;
2108 epos->offset = sizeof(struct allocExtDesc);
2110 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2111 epos->bh = udf_tread(inode->i_sb, block);
2113 udf_debug("reading block %d failed!\n", block);
2121 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2122 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2127 struct short_ad *sad;
2128 struct long_ad *lad;
2129 struct udf_inode_info *iinfo = UDF_I(inode);
2133 epos->offset = udf_file_entry_alloc_offset(inode);
2134 ptr = iinfo->i_ext.i_data + epos->offset -
2135 udf_file_entry_alloc_offset(inode) +
2137 alen = udf_file_entry_alloc_offset(inode) +
2141 epos->offset = sizeof(struct allocExtDesc);
2142 ptr = epos->bh->b_data + epos->offset;
2143 alen = sizeof(struct allocExtDesc) +
2144 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2148 switch (iinfo->i_alloc_type) {
2149 case ICBTAG_FLAG_AD_SHORT:
2150 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2153 etype = le32_to_cpu(sad->extLength) >> 30;
2154 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2155 eloc->partitionReferenceNum =
2156 iinfo->i_location.partitionReferenceNum;
2157 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2159 case ICBTAG_FLAG_AD_LONG:
2160 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2163 etype = le32_to_cpu(lad->extLength) >> 30;
2164 *eloc = lelb_to_cpu(lad->extLocation);
2165 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2168 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2175 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2176 struct kernel_lb_addr neloc, uint32_t nelen)
2178 struct kernel_lb_addr oeloc;
2185 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2186 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2188 nelen = (etype << 30) | oelen;
2190 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2193 return (nelen >> 30);
2196 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
2198 struct extent_position oepos;
2201 struct allocExtDesc *aed;
2202 struct udf_inode_info *iinfo;
2203 struct kernel_lb_addr eloc;
2211 iinfo = UDF_I(inode);
2212 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2213 adsize = sizeof(struct short_ad);
2214 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2215 adsize = sizeof(struct long_ad);
2220 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2223 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2224 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2225 if (oepos.bh != epos.bh) {
2226 oepos.block = epos.block;
2230 oepos.offset = epos.offset - adsize;
2233 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2236 if (epos.bh != oepos.bh) {
2237 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2238 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2239 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2241 iinfo->i_lenAlloc -= (adsize * 2);
2242 mark_inode_dirty(inode);
2244 aed = (struct allocExtDesc *)oepos.bh->b_data;
2245 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2246 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2247 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2248 udf_update_tag(oepos.bh->b_data,
2249 oepos.offset - (2 * adsize));
2251 udf_update_tag(oepos.bh->b_data,
2252 sizeof(struct allocExtDesc));
2253 mark_buffer_dirty_inode(oepos.bh, inode);
2256 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2258 iinfo->i_lenAlloc -= adsize;
2259 mark_inode_dirty(inode);
2261 aed = (struct allocExtDesc *)oepos.bh->b_data;
2262 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2263 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2264 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2265 udf_update_tag(oepos.bh->b_data,
2266 epos.offset - adsize);
2268 udf_update_tag(oepos.bh->b_data,
2269 sizeof(struct allocExtDesc));
2270 mark_buffer_dirty_inode(oepos.bh, inode);
2277 return (elen >> 30);
2280 int8_t inode_bmap(struct inode *inode, sector_t block,
2281 struct extent_position *pos, struct kernel_lb_addr *eloc,
2282 uint32_t *elen, sector_t *offset)
2284 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2285 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2287 struct udf_inode_info *iinfo;
2289 iinfo = UDF_I(inode);
2290 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2292 pos->block = iinfo->i_location;
2297 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2299 *offset = (bcount - lbcount) >> blocksize_bits;
2300 iinfo->i_lenExtents = lbcount;
2304 } while (lbcount <= bcount);
2305 /* update extent cache */
2306 udf_update_extent_cache(inode, lbcount - *elen, pos);
2307 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2312 long udf_block_map(struct inode *inode, sector_t block)
2314 struct kernel_lb_addr eloc;
2317 struct extent_position epos = {};
2320 down_read(&UDF_I(inode)->i_data_sem);
2322 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2323 (EXT_RECORDED_ALLOCATED >> 30))
2324 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2328 up_read(&UDF_I(inode)->i_data_sem);
2331 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2332 return udf_fixed_to_variable(ret);