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, udf_pblk_t,
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,
322 udf_pblk_t *block, int *err)
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++;
437 udf_clear_extent_cache(inode);
438 phys = inode_getblk(inode, block, &err, &new);
443 set_buffer_new(bh_result);
444 map_bh(bh_result, inode->i_sb, phys);
447 up_write(&iinfo->i_data_sem);
451 static struct buffer_head *udf_getblk(struct inode *inode, udf_pblk_t block,
452 int create, int *err)
454 struct buffer_head *bh;
455 struct buffer_head dummy;
458 dummy.b_blocknr = -1000;
459 *err = udf_get_block(inode, block, &dummy, create);
460 if (!*err && buffer_mapped(&dummy)) {
461 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
462 if (buffer_new(&dummy)) {
464 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
465 set_buffer_uptodate(bh);
467 mark_buffer_dirty_inode(bh, inode);
475 /* Extend the file with new blocks totaling 'new_block_bytes',
476 * return the number of extents added
478 static int udf_do_extend_file(struct inode *inode,
479 struct extent_position *last_pos,
480 struct kernel_long_ad *last_ext,
481 loff_t new_block_bytes)
484 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
485 struct super_block *sb = inode->i_sb;
486 struct kernel_lb_addr prealloc_loc = {};
487 uint32_t prealloc_len = 0;
488 struct udf_inode_info *iinfo;
491 /* The previous extent is fake and we should not extend by anything
492 * - there's nothing to do... */
493 if (!new_block_bytes && fake)
496 iinfo = UDF_I(inode);
497 /* Round the last extent up to a multiple of block size */
498 if (last_ext->extLength & (sb->s_blocksize - 1)) {
499 last_ext->extLength =
500 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
501 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
502 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
503 iinfo->i_lenExtents =
504 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
505 ~(sb->s_blocksize - 1);
508 /* Last extent are just preallocated blocks? */
509 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
510 EXT_NOT_RECORDED_ALLOCATED) {
511 /* Save the extent so that we can reattach it to the end */
512 prealloc_loc = last_ext->extLocation;
513 prealloc_len = last_ext->extLength;
514 /* Mark the extent as a hole */
515 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
516 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
517 last_ext->extLocation.logicalBlockNum = 0;
518 last_ext->extLocation.partitionReferenceNum = 0;
521 /* Can we merge with the previous extent? */
522 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
523 EXT_NOT_RECORDED_NOT_ALLOCATED) {
524 add = (1 << 30) - sb->s_blocksize -
525 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
526 if (add > new_block_bytes)
527 add = new_block_bytes;
528 new_block_bytes -= add;
529 last_ext->extLength += add;
533 udf_add_aext(inode, last_pos, &last_ext->extLocation,
534 last_ext->extLength, 1);
537 struct kernel_lb_addr tmploc;
540 udf_write_aext(inode, last_pos, &last_ext->extLocation,
541 last_ext->extLength, 1);
544 * We've rewritten the last extent. If we are going to add
545 * more extents, we may need to enter possible following
546 * empty indirect extent.
548 if (new_block_bytes || prealloc_len)
549 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
552 /* Managed to do everything necessary? */
553 if (!new_block_bytes)
556 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
557 last_ext->extLocation.logicalBlockNum = 0;
558 last_ext->extLocation.partitionReferenceNum = 0;
559 add = (1 << 30) - sb->s_blocksize;
560 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
562 /* Create enough extents to cover the whole hole */
563 while (new_block_bytes > add) {
564 new_block_bytes -= add;
565 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
566 last_ext->extLength, 1);
571 if (new_block_bytes) {
572 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
574 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
575 last_ext->extLength, 1);
582 /* Do we have some preallocated blocks saved? */
584 err = udf_add_aext(inode, last_pos, &prealloc_loc,
588 last_ext->extLocation = prealloc_loc;
589 last_ext->extLength = prealloc_len;
593 /* last_pos should point to the last written extent... */
594 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
595 last_pos->offset -= sizeof(struct short_ad);
596 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
597 last_pos->offset -= sizeof(struct long_ad);
604 /* Extend the final block of the file to final_block_len bytes */
605 static void udf_do_extend_final_block(struct inode *inode,
606 struct extent_position *last_pos,
607 struct kernel_long_ad *last_ext,
608 uint32_t final_block_len)
610 struct super_block *sb = inode->i_sb;
611 uint32_t added_bytes;
613 added_bytes = final_block_len -
614 (last_ext->extLength & (sb->s_blocksize - 1));
615 last_ext->extLength += added_bytes;
616 UDF_I(inode)->i_lenExtents += added_bytes;
618 udf_write_aext(inode, last_pos, &last_ext->extLocation,
619 last_ext->extLength, 1);
622 static int udf_extend_file(struct inode *inode, loff_t newsize)
625 struct extent_position epos;
626 struct kernel_lb_addr eloc;
629 struct super_block *sb = inode->i_sb;
630 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
631 unsigned long partial_final_block;
633 struct udf_inode_info *iinfo = UDF_I(inode);
634 struct kernel_long_ad extent;
636 int within_final_block;
638 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
639 adsize = sizeof(struct short_ad);
640 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
641 adsize = sizeof(struct long_ad);
645 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
646 within_final_block = (etype != -1);
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 partial_final_block = newsize & (sb->s_blocksize - 1);
664 /* File has extent covering the new size (could happen when extending
667 if (within_final_block) {
668 /* Extending file within the last file block */
669 udf_do_extend_final_block(inode, &epos, &extent,
670 partial_final_block);
672 loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
674 err = udf_do_extend_file(inode, &epos, &extent, add);
680 iinfo->i_lenExtents = newsize;
686 static sector_t inode_getblk(struct inode *inode, sector_t block,
689 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
690 struct extent_position prev_epos, cur_epos, next_epos;
691 int count = 0, startnum = 0, endnum = 0;
692 uint32_t elen = 0, tmpelen;
693 struct kernel_lb_addr eloc, tmpeloc;
695 loff_t lbcount = 0, b_off = 0;
696 udf_pblk_t newblocknum, newblock;
699 struct udf_inode_info *iinfo = UDF_I(inode);
700 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
706 prev_epos.offset = udf_file_entry_alloc_offset(inode);
707 prev_epos.block = iinfo->i_location;
709 cur_epos = next_epos = prev_epos;
710 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
712 /* find the extent which contains the block we are looking for.
713 alternate between laarr[0] and laarr[1] for locations of the
714 current extent, and the previous extent */
716 if (prev_epos.bh != cur_epos.bh) {
717 brelse(prev_epos.bh);
719 prev_epos.bh = cur_epos.bh;
721 if (cur_epos.bh != next_epos.bh) {
723 get_bh(next_epos.bh);
724 cur_epos.bh = next_epos.bh;
729 prev_epos.block = cur_epos.block;
730 cur_epos.block = next_epos.block;
732 prev_epos.offset = cur_epos.offset;
733 cur_epos.offset = next_epos.offset;
735 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
741 laarr[c].extLength = (etype << 30) | elen;
742 laarr[c].extLocation = eloc;
744 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
745 pgoal = eloc.logicalBlockNum +
746 ((elen + inode->i_sb->s_blocksize - 1) >>
747 inode->i_sb->s_blocksize_bits);
750 } while (lbcount + elen <= b_off);
753 offset = b_off >> inode->i_sb->s_blocksize_bits;
755 * Move prev_epos and cur_epos into indirect extent if we are at
758 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
759 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
761 /* if the extent is allocated and recorded, return the block
762 if the extent is not a multiple of the blocksize, round up */
764 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
765 if (elen & (inode->i_sb->s_blocksize - 1)) {
766 elen = EXT_RECORDED_ALLOCATED |
767 ((elen + inode->i_sb->s_blocksize - 1) &
768 ~(inode->i_sb->s_blocksize - 1));
769 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
771 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
775 /* Are we beyond EOF? */
785 /* Create a fake extent when there's not one */
786 memset(&laarr[0].extLocation, 0x00,
787 sizeof(struct kernel_lb_addr));
788 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
789 /* Will udf_do_extend_file() create real extent from
791 startnum = (offset > 0);
793 /* Create extents for the hole between EOF and offset */
794 hole_len = (loff_t)offset << inode->i_blkbits;
795 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
804 /* We are not covered by a preallocated extent? */
805 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
806 EXT_NOT_RECORDED_ALLOCATED) {
807 /* Is there any real extent? - otherwise we overwrite
811 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
812 inode->i_sb->s_blocksize;
813 memset(&laarr[c].extLocation, 0x00,
814 sizeof(struct kernel_lb_addr));
821 endnum = startnum = ((count > 2) ? 2 : count);
823 /* if the current extent is in position 0,
824 swap it with the previous */
825 if (!c && count != 1) {
832 /* if the current block is located in an extent,
833 read the next extent */
834 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
836 laarr[c + 1].extLength = (etype << 30) | elen;
837 laarr[c + 1].extLocation = eloc;
845 /* if the current extent is not recorded but allocated, get the
846 * block in the extent corresponding to the requested block */
847 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
848 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
849 else { /* otherwise, allocate a new block */
850 if (iinfo->i_next_alloc_block == block)
851 goal = iinfo->i_next_alloc_goal;
854 if (!(goal = pgoal)) /* XXX: what was intended here? */
855 goal = iinfo->i_location.logicalBlockNum + 1;
858 newblocknum = udf_new_block(inode->i_sb, inode,
859 iinfo->i_location.partitionReferenceNum,
867 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
870 /* if the extent the requsted block is located in contains multiple
871 * blocks, split the extent into at most three extents. blocks prior
872 * to requested block, requested block, and blocks after requested
874 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
876 /* We preallocate blocks only for regular files. It also makes sense
877 * for directories but there's a problem when to drop the
878 * preallocation. We might use some delayed work for that but I feel
879 * it's overengineering for a filesystem like UDF. */
880 if (S_ISREG(inode->i_mode))
881 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
883 /* merge any continuous blocks in laarr */
884 udf_merge_extents(inode, laarr, &endnum);
886 /* write back the new extents, inserting new extents if the new number
887 * of extents is greater than the old number, and deleting extents if
888 * the new number of extents is less than the old number */
889 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
891 newblock = udf_get_pblock(inode->i_sb, newblocknum,
892 iinfo->i_location.partitionReferenceNum, 0);
898 iinfo->i_next_alloc_block = block;
899 iinfo->i_next_alloc_goal = newblocknum;
900 inode->i_ctime = current_time(inode);
903 udf_sync_inode(inode);
905 mark_inode_dirty(inode);
907 brelse(prev_epos.bh);
909 brelse(next_epos.bh);
913 static void udf_split_extents(struct inode *inode, int *c, int offset,
914 udf_pblk_t newblocknum,
915 struct kernel_long_ad *laarr, int *endnum)
917 unsigned long blocksize = inode->i_sb->s_blocksize;
918 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
920 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
921 (laarr[*c].extLength >> 30) ==
922 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
924 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
925 blocksize - 1) >> blocksize_bits;
926 int8_t etype = (laarr[curr].extLength >> 30);
930 else if (!offset || blen == offset + 1) {
931 laarr[curr + 2] = laarr[curr + 1];
932 laarr[curr + 1] = laarr[curr];
934 laarr[curr + 3] = laarr[curr + 1];
935 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
939 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
940 udf_free_blocks(inode->i_sb, inode,
941 &laarr[curr].extLocation,
943 laarr[curr].extLength =
944 EXT_NOT_RECORDED_NOT_ALLOCATED |
945 (offset << blocksize_bits);
946 laarr[curr].extLocation.logicalBlockNum = 0;
947 laarr[curr].extLocation.
948 partitionReferenceNum = 0;
950 laarr[curr].extLength = (etype << 30) |
951 (offset << blocksize_bits);
957 laarr[curr].extLocation.logicalBlockNum = newblocknum;
958 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
959 laarr[curr].extLocation.partitionReferenceNum =
960 UDF_I(inode)->i_location.partitionReferenceNum;
961 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
965 if (blen != offset + 1) {
966 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
967 laarr[curr].extLocation.logicalBlockNum +=
969 laarr[curr].extLength = (etype << 30) |
970 ((blen - (offset + 1)) << blocksize_bits);
977 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
978 struct kernel_long_ad *laarr,
981 int start, length = 0, currlength = 0, i;
983 if (*endnum >= (c + 1)) {
989 if ((laarr[c + 1].extLength >> 30) ==
990 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
992 length = currlength =
993 (((laarr[c + 1].extLength &
994 UDF_EXTENT_LENGTH_MASK) +
995 inode->i_sb->s_blocksize - 1) >>
996 inode->i_sb->s_blocksize_bits);
1001 for (i = start + 1; i <= *endnum; i++) {
1004 length += UDF_DEFAULT_PREALLOC_BLOCKS;
1005 } else if ((laarr[i].extLength >> 30) ==
1006 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
1007 length += (((laarr[i].extLength &
1008 UDF_EXTENT_LENGTH_MASK) +
1009 inode->i_sb->s_blocksize - 1) >>
1010 inode->i_sb->s_blocksize_bits);
1016 int next = laarr[start].extLocation.logicalBlockNum +
1017 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
1018 inode->i_sb->s_blocksize - 1) >>
1019 inode->i_sb->s_blocksize_bits);
1020 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1021 laarr[start].extLocation.partitionReferenceNum,
1022 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1023 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1026 if (start == (c + 1))
1027 laarr[start].extLength +=
1029 inode->i_sb->s_blocksize_bits);
1031 memmove(&laarr[c + 2], &laarr[c + 1],
1032 sizeof(struct long_ad) * (*endnum - (c + 1)));
1034 laarr[c + 1].extLocation.logicalBlockNum = next;
1035 laarr[c + 1].extLocation.partitionReferenceNum =
1036 laarr[c].extLocation.
1037 partitionReferenceNum;
1038 laarr[c + 1].extLength =
1039 EXT_NOT_RECORDED_ALLOCATED |
1041 inode->i_sb->s_blocksize_bits);
1045 for (i = start + 1; numalloc && i < *endnum; i++) {
1046 int elen = ((laarr[i].extLength &
1047 UDF_EXTENT_LENGTH_MASK) +
1048 inode->i_sb->s_blocksize - 1) >>
1049 inode->i_sb->s_blocksize_bits;
1051 if (elen > numalloc) {
1052 laarr[i].extLength -=
1054 inode->i_sb->s_blocksize_bits);
1058 if (*endnum > (i + 1))
1061 sizeof(struct long_ad) *
1062 (*endnum - (i + 1)));
1067 UDF_I(inode)->i_lenExtents +=
1068 numalloc << inode->i_sb->s_blocksize_bits;
1073 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
1077 unsigned long blocksize = inode->i_sb->s_blocksize;
1078 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1080 for (i = 0; i < (*endnum - 1); i++) {
1081 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1082 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1084 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1085 (((li->extLength >> 30) ==
1086 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1087 ((lip1->extLocation.logicalBlockNum -
1088 li->extLocation.logicalBlockNum) ==
1089 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1090 blocksize - 1) >> blocksize_bits)))) {
1092 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1093 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1094 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1095 lip1->extLength = (lip1->extLength -
1097 UDF_EXTENT_LENGTH_MASK) +
1098 UDF_EXTENT_LENGTH_MASK) &
1100 li->extLength = (li->extLength &
1101 UDF_EXTENT_FLAG_MASK) +
1102 (UDF_EXTENT_LENGTH_MASK + 1) -
1104 lip1->extLocation.logicalBlockNum =
1105 li->extLocation.logicalBlockNum +
1107 UDF_EXTENT_LENGTH_MASK) >>
1110 li->extLength = lip1->extLength +
1112 UDF_EXTENT_LENGTH_MASK) +
1113 blocksize - 1) & ~(blocksize - 1));
1114 if (*endnum > (i + 2))
1115 memmove(&laarr[i + 1], &laarr[i + 2],
1116 sizeof(struct long_ad) *
1117 (*endnum - (i + 2)));
1121 } else if (((li->extLength >> 30) ==
1122 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1123 ((lip1->extLength >> 30) ==
1124 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1125 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1127 UDF_EXTENT_LENGTH_MASK) +
1128 blocksize - 1) >> blocksize_bits);
1129 li->extLocation.logicalBlockNum = 0;
1130 li->extLocation.partitionReferenceNum = 0;
1132 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1133 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1134 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1135 lip1->extLength = (lip1->extLength -
1137 UDF_EXTENT_LENGTH_MASK) +
1138 UDF_EXTENT_LENGTH_MASK) &
1140 li->extLength = (li->extLength &
1141 UDF_EXTENT_FLAG_MASK) +
1142 (UDF_EXTENT_LENGTH_MASK + 1) -
1145 li->extLength = lip1->extLength +
1147 UDF_EXTENT_LENGTH_MASK) +
1148 blocksize - 1) & ~(blocksize - 1));
1149 if (*endnum > (i + 2))
1150 memmove(&laarr[i + 1], &laarr[i + 2],
1151 sizeof(struct long_ad) *
1152 (*endnum - (i + 2)));
1156 } else if ((li->extLength >> 30) ==
1157 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1158 udf_free_blocks(inode->i_sb, inode,
1159 &li->extLocation, 0,
1161 UDF_EXTENT_LENGTH_MASK) +
1162 blocksize - 1) >> blocksize_bits);
1163 li->extLocation.logicalBlockNum = 0;
1164 li->extLocation.partitionReferenceNum = 0;
1165 li->extLength = (li->extLength &
1166 UDF_EXTENT_LENGTH_MASK) |
1167 EXT_NOT_RECORDED_NOT_ALLOCATED;
1172 static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1173 int startnum, int endnum,
1174 struct extent_position *epos)
1177 struct kernel_lb_addr tmploc;
1180 if (startnum > endnum) {
1181 for (i = 0; i < (startnum - endnum); i++)
1182 udf_delete_aext(inode, *epos);
1183 } else if (startnum < endnum) {
1184 for (i = 0; i < (endnum - startnum); i++) {
1185 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1186 laarr[i].extLength);
1187 udf_next_aext(inode, epos, &laarr[i].extLocation,
1188 &laarr[i].extLength, 1);
1193 for (i = start; i < endnum; i++) {
1194 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1195 udf_write_aext(inode, epos, &laarr[i].extLocation,
1196 laarr[i].extLength, 1);
1200 struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
1201 int create, int *err)
1203 struct buffer_head *bh = NULL;
1205 bh = udf_getblk(inode, block, create, err);
1209 if (buffer_uptodate(bh))
1212 ll_rw_block(REQ_OP_READ, 0, 1, &bh);
1215 if (buffer_uptodate(bh))
1223 int udf_setsize(struct inode *inode, loff_t newsize)
1226 struct udf_inode_info *iinfo;
1227 unsigned int bsize = i_blocksize(inode);
1229 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1230 S_ISLNK(inode->i_mode)))
1232 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1235 iinfo = UDF_I(inode);
1236 if (newsize > inode->i_size) {
1237 down_write(&iinfo->i_data_sem);
1238 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1240 (udf_file_entry_alloc_offset(inode) + newsize)) {
1241 err = udf_expand_file_adinicb(inode);
1244 down_write(&iinfo->i_data_sem);
1246 iinfo->i_lenAlloc = newsize;
1250 err = udf_extend_file(inode, newsize);
1252 up_write(&iinfo->i_data_sem);
1256 up_write(&iinfo->i_data_sem);
1257 truncate_setsize(inode, newsize);
1259 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1260 down_write(&iinfo->i_data_sem);
1261 udf_clear_extent_cache(inode);
1262 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1263 0x00, bsize - newsize -
1264 udf_file_entry_alloc_offset(inode));
1265 iinfo->i_lenAlloc = newsize;
1266 truncate_setsize(inode, newsize);
1267 up_write(&iinfo->i_data_sem);
1270 err = block_truncate_page(inode->i_mapping, newsize,
1274 truncate_setsize(inode, newsize);
1275 down_write(&iinfo->i_data_sem);
1276 udf_clear_extent_cache(inode);
1277 udf_truncate_extents(inode);
1278 up_write(&iinfo->i_data_sem);
1281 inode->i_mtime = inode->i_ctime = current_time(inode);
1283 udf_sync_inode(inode);
1285 mark_inode_dirty(inode);
1290 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1291 * arbitrary - just that we hopefully don't limit any real use of rewritten
1292 * inode on write-once media but avoid looping for too long on corrupted media.
1294 #define UDF_MAX_ICB_NESTING 1024
1296 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1298 struct buffer_head *bh = NULL;
1299 struct fileEntry *fe;
1300 struct extendedFileEntry *efe;
1302 struct udf_inode_info *iinfo = UDF_I(inode);
1303 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1304 struct kernel_lb_addr *iloc = &iinfo->i_location;
1305 unsigned int link_count;
1306 unsigned int indirections = 0;
1307 int bs = inode->i_sb->s_blocksize;
1312 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1313 udf_debug("partition reference: %u > logical volume partitions: %u\n",
1314 iloc->partitionReferenceNum, sbi->s_partitions);
1318 if (iloc->logicalBlockNum >=
1319 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1320 udf_debug("block=%u, partition=%u out of range\n",
1321 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1326 * Set defaults, but the inode is still incomplete!
1327 * Note: get_new_inode() sets the following on a new inode:
1330 * i_flags = sb->s_flags
1332 * clean_inode(): zero fills and sets
1337 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1339 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
1343 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1344 ident != TAG_IDENT_USE) {
1345 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
1346 inode->i_ino, ident);
1350 fe = (struct fileEntry *)bh->b_data;
1351 efe = (struct extendedFileEntry *)bh->b_data;
1353 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1354 struct buffer_head *ibh;
1356 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1357 if (ident == TAG_IDENT_IE && ibh) {
1358 struct kernel_lb_addr loc;
1359 struct indirectEntry *ie;
1361 ie = (struct indirectEntry *)ibh->b_data;
1362 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1364 if (ie->indirectICB.extLength) {
1366 memcpy(&iinfo->i_location, &loc,
1367 sizeof(struct kernel_lb_addr));
1368 if (++indirections > UDF_MAX_ICB_NESTING) {
1369 udf_err(inode->i_sb,
1370 "too many ICBs in ICB hierarchy"
1371 " (max %d supported)\n",
1372 UDF_MAX_ICB_NESTING);
1380 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1381 udf_err(inode->i_sb, "unsupported strategy type: %u\n",
1382 le16_to_cpu(fe->icbTag.strategyType));
1385 if (fe->icbTag.strategyType == cpu_to_le16(4))
1386 iinfo->i_strat4096 = 0;
1387 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1388 iinfo->i_strat4096 = 1;
1390 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1391 ICBTAG_FLAG_AD_MASK;
1392 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1393 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1394 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1398 iinfo->i_unique = 0;
1399 iinfo->i_lenEAttr = 0;
1400 iinfo->i_lenExtents = 0;
1401 iinfo->i_lenAlloc = 0;
1402 iinfo->i_next_alloc_block = 0;
1403 iinfo->i_next_alloc_goal = 0;
1404 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1407 ret = udf_alloc_i_data(inode, bs -
1408 sizeof(struct extendedFileEntry));
1411 memcpy(iinfo->i_ext.i_data,
1412 bh->b_data + sizeof(struct extendedFileEntry),
1413 bs - sizeof(struct extendedFileEntry));
1414 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1417 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1420 memcpy(iinfo->i_ext.i_data,
1421 bh->b_data + sizeof(struct fileEntry),
1422 bs - sizeof(struct fileEntry));
1423 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1426 iinfo->i_lenAlloc = le32_to_cpu(
1427 ((struct unallocSpaceEntry *)bh->b_data)->
1429 ret = udf_alloc_i_data(inode, bs -
1430 sizeof(struct unallocSpaceEntry));
1433 memcpy(iinfo->i_ext.i_data,
1434 bh->b_data + sizeof(struct unallocSpaceEntry),
1435 bs - sizeof(struct unallocSpaceEntry));
1440 read_lock(&sbi->s_cred_lock);
1441 uid = le32_to_cpu(fe->uid);
1442 if (uid == UDF_INVALID_ID ||
1443 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1444 inode->i_uid = sbi->s_uid;
1446 i_uid_write(inode, uid);
1448 gid = le32_to_cpu(fe->gid);
1449 if (gid == UDF_INVALID_ID ||
1450 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1451 inode->i_gid = sbi->s_gid;
1453 i_gid_write(inode, gid);
1455 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1456 sbi->s_fmode != UDF_INVALID_MODE)
1457 inode->i_mode = sbi->s_fmode;
1458 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1459 sbi->s_dmode != UDF_INVALID_MODE)
1460 inode->i_mode = sbi->s_dmode;
1462 inode->i_mode = udf_convert_permissions(fe);
1463 inode->i_mode &= ~sbi->s_umask;
1464 read_unlock(&sbi->s_cred_lock);
1466 link_count = le16_to_cpu(fe->fileLinkCount);
1468 if (!hidden_inode) {
1474 set_nlink(inode, link_count);
1476 inode->i_size = le64_to_cpu(fe->informationLength);
1477 iinfo->i_lenExtents = inode->i_size;
1479 if (iinfo->i_efe == 0) {
1480 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1481 (inode->i_sb->s_blocksize_bits - 9);
1483 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1484 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1485 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
1487 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1488 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1489 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1490 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1492 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1493 (inode->i_sb->s_blocksize_bits - 9);
1495 udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1496 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
1497 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
1498 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
1500 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1501 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1502 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1503 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1505 inode->i_generation = iinfo->i_unique;
1508 * Sanity check length of allocation descriptors and extended attrs to
1509 * avoid integer overflows
1511 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1513 /* Now do exact checks */
1514 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1516 /* Sanity checks for files in ICB so that we don't get confused later */
1517 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1519 * For file in ICB data is stored in allocation descriptor
1520 * so sizes should match
1522 if (iinfo->i_lenAlloc != inode->i_size)
1524 /* File in ICB has to fit in there... */
1525 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1529 switch (fe->icbTag.fileType) {
1530 case ICBTAG_FILE_TYPE_DIRECTORY:
1531 inode->i_op = &udf_dir_inode_operations;
1532 inode->i_fop = &udf_dir_operations;
1533 inode->i_mode |= S_IFDIR;
1536 case ICBTAG_FILE_TYPE_REALTIME:
1537 case ICBTAG_FILE_TYPE_REGULAR:
1538 case ICBTAG_FILE_TYPE_UNDEF:
1539 case ICBTAG_FILE_TYPE_VAT20:
1540 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1541 inode->i_data.a_ops = &udf_adinicb_aops;
1543 inode->i_data.a_ops = &udf_aops;
1544 inode->i_op = &udf_file_inode_operations;
1545 inode->i_fop = &udf_file_operations;
1546 inode->i_mode |= S_IFREG;
1548 case ICBTAG_FILE_TYPE_BLOCK:
1549 inode->i_mode |= S_IFBLK;
1551 case ICBTAG_FILE_TYPE_CHAR:
1552 inode->i_mode |= S_IFCHR;
1554 case ICBTAG_FILE_TYPE_FIFO:
1555 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1557 case ICBTAG_FILE_TYPE_SOCKET:
1558 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1560 case ICBTAG_FILE_TYPE_SYMLINK:
1561 inode->i_data.a_ops = &udf_symlink_aops;
1562 inode->i_op = &udf_symlink_inode_operations;
1563 inode_nohighmem(inode);
1564 inode->i_mode = S_IFLNK | 0777;
1566 case ICBTAG_FILE_TYPE_MAIN:
1567 udf_debug("METADATA FILE-----\n");
1569 case ICBTAG_FILE_TYPE_MIRROR:
1570 udf_debug("METADATA MIRROR FILE-----\n");
1572 case ICBTAG_FILE_TYPE_BITMAP:
1573 udf_debug("METADATA BITMAP FILE-----\n");
1576 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
1577 inode->i_ino, fe->icbTag.fileType);
1580 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1581 struct deviceSpec *dsea =
1582 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1584 init_special_inode(inode, inode->i_mode,
1585 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1586 le32_to_cpu(dsea->minorDeviceIdent)));
1587 /* Developer ID ??? */
1597 static int udf_alloc_i_data(struct inode *inode, size_t size)
1599 struct udf_inode_info *iinfo = UDF_I(inode);
1600 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1601 if (!iinfo->i_ext.i_data)
1606 static umode_t udf_convert_permissions(struct fileEntry *fe)
1609 uint32_t permissions;
1612 permissions = le32_to_cpu(fe->permissions);
1613 flags = le16_to_cpu(fe->icbTag.flags);
1615 mode = ((permissions) & 0007) |
1616 ((permissions >> 2) & 0070) |
1617 ((permissions >> 4) & 0700) |
1618 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1619 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1620 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1625 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1627 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1630 static int udf_sync_inode(struct inode *inode)
1632 return udf_update_inode(inode, 1);
1635 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
1637 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1638 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1639 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1640 iinfo->i_crtime = time;
1643 static int udf_update_inode(struct inode *inode, int do_sync)
1645 struct buffer_head *bh = NULL;
1646 struct fileEntry *fe;
1647 struct extendedFileEntry *efe;
1648 uint64_t lb_recorded;
1653 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1654 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1655 struct udf_inode_info *iinfo = UDF_I(inode);
1657 bh = udf_tgetblk(inode->i_sb,
1658 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1660 udf_debug("getblk failure\n");
1665 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1666 fe = (struct fileEntry *)bh->b_data;
1667 efe = (struct extendedFileEntry *)bh->b_data;
1670 struct unallocSpaceEntry *use =
1671 (struct unallocSpaceEntry *)bh->b_data;
1673 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1674 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1675 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1676 sizeof(struct unallocSpaceEntry));
1677 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1678 crclen = sizeof(struct unallocSpaceEntry);
1683 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1684 fe->uid = cpu_to_le32(UDF_INVALID_ID);
1686 fe->uid = cpu_to_le32(i_uid_read(inode));
1688 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1689 fe->gid = cpu_to_le32(UDF_INVALID_ID);
1691 fe->gid = cpu_to_le32(i_gid_read(inode));
1693 udfperms = ((inode->i_mode & 0007)) |
1694 ((inode->i_mode & 0070) << 2) |
1695 ((inode->i_mode & 0700) << 4);
1697 udfperms |= (le32_to_cpu(fe->permissions) &
1698 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1699 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1700 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1701 fe->permissions = cpu_to_le32(udfperms);
1703 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1704 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1706 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1708 fe->informationLength = cpu_to_le64(inode->i_size);
1710 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1712 struct deviceSpec *dsea =
1713 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1715 dsea = (struct deviceSpec *)
1716 udf_add_extendedattr(inode,
1717 sizeof(struct deviceSpec) +
1718 sizeof(struct regid), 12, 0x3);
1719 dsea->attrType = cpu_to_le32(12);
1720 dsea->attrSubtype = 1;
1721 dsea->attrLength = cpu_to_le32(
1722 sizeof(struct deviceSpec) +
1723 sizeof(struct regid));
1724 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1726 eid = (struct regid *)dsea->impUse;
1727 memset(eid, 0, sizeof(*eid));
1728 strcpy(eid->ident, UDF_ID_DEVELOPER);
1729 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1730 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1731 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1732 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1735 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1736 lb_recorded = 0; /* No extents => no blocks! */
1739 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1740 (blocksize_bits - 9);
1742 if (iinfo->i_efe == 0) {
1743 memcpy(bh->b_data + sizeof(struct fileEntry),
1744 iinfo->i_ext.i_data,
1745 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1746 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1748 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1749 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1750 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1751 memset(&(fe->impIdent), 0, sizeof(struct regid));
1752 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1753 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1754 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1755 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1756 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1757 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1758 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1759 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1760 crclen = sizeof(struct fileEntry);
1762 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1763 iinfo->i_ext.i_data,
1764 inode->i_sb->s_blocksize -
1765 sizeof(struct extendedFileEntry));
1766 efe->objectSize = cpu_to_le64(inode->i_size);
1767 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1769 udf_adjust_time(iinfo, inode->i_atime);
1770 udf_adjust_time(iinfo, inode->i_mtime);
1771 udf_adjust_time(iinfo, inode->i_ctime);
1773 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1774 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1775 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1776 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1778 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1779 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1780 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1781 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1782 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1783 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1784 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1785 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1786 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1787 crclen = sizeof(struct extendedFileEntry);
1791 if (iinfo->i_strat4096) {
1792 fe->icbTag.strategyType = cpu_to_le16(4096);
1793 fe->icbTag.strategyParameter = cpu_to_le16(1);
1794 fe->icbTag.numEntries = cpu_to_le16(2);
1796 fe->icbTag.strategyType = cpu_to_le16(4);
1797 fe->icbTag.numEntries = cpu_to_le16(1);
1801 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1802 else if (S_ISDIR(inode->i_mode))
1803 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1804 else if (S_ISREG(inode->i_mode))
1805 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1806 else if (S_ISLNK(inode->i_mode))
1807 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1808 else if (S_ISBLK(inode->i_mode))
1809 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1810 else if (S_ISCHR(inode->i_mode))
1811 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1812 else if (S_ISFIFO(inode->i_mode))
1813 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1814 else if (S_ISSOCK(inode->i_mode))
1815 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1817 icbflags = iinfo->i_alloc_type |
1818 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1819 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1820 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1821 (le16_to_cpu(fe->icbTag.flags) &
1822 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1823 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1825 fe->icbTag.flags = cpu_to_le16(icbflags);
1826 if (sbi->s_udfrev >= 0x0200)
1827 fe->descTag.descVersion = cpu_to_le16(3);
1829 fe->descTag.descVersion = cpu_to_le16(2);
1830 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1831 fe->descTag.tagLocation = cpu_to_le32(
1832 iinfo->i_location.logicalBlockNum);
1833 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1834 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1835 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1837 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1839 set_buffer_uptodate(bh);
1842 /* write the data blocks */
1843 mark_buffer_dirty(bh);
1845 sync_dirty_buffer(bh);
1846 if (buffer_write_io_error(bh)) {
1847 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1857 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1860 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1861 struct inode *inode = iget_locked(sb, block);
1865 return ERR_PTR(-ENOMEM);
1867 if (!(inode->i_state & I_NEW))
1870 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1871 err = udf_read_inode(inode, hidden_inode);
1874 return ERR_PTR(err);
1876 unlock_new_inode(inode);
1881 int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
1882 struct extent_position *epos)
1884 struct super_block *sb = inode->i_sb;
1885 struct buffer_head *bh;
1886 struct allocExtDesc *aed;
1887 struct extent_position nepos;
1888 struct kernel_lb_addr neloc;
1891 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1892 adsize = sizeof(struct short_ad);
1893 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1894 adsize = sizeof(struct long_ad);
1898 neloc.logicalBlockNum = block;
1899 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1901 bh = udf_tgetblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1905 memset(bh->b_data, 0x00, sb->s_blocksize);
1906 set_buffer_uptodate(bh);
1908 mark_buffer_dirty_inode(bh, inode);
1910 aed = (struct allocExtDesc *)(bh->b_data);
1911 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1912 aed->previousAllocExtLocation =
1913 cpu_to_le32(epos->block.logicalBlockNum);
1915 aed->lengthAllocDescs = cpu_to_le32(0);
1916 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1920 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1921 sizeof(struct tag));
1923 nepos.block = neloc;
1924 nepos.offset = sizeof(struct allocExtDesc);
1928 * Do we have to copy current last extent to make space for indirect
1931 if (epos->offset + adsize > sb->s_blocksize) {
1932 struct kernel_lb_addr cp_loc;
1936 epos->offset -= adsize;
1937 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1938 cp_len |= ((uint32_t)cp_type) << 30;
1940 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1941 udf_write_aext(inode, epos, &nepos.block,
1942 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1944 __udf_add_aext(inode, epos, &nepos.block,
1945 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDECS, 0);
1955 * Append extent at the given position - should be the first free one in inode
1956 * / indirect extent. This function assumes there is enough space in the inode
1957 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1959 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1960 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1962 struct udf_inode_info *iinfo = UDF_I(inode);
1963 struct allocExtDesc *aed;
1966 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1967 adsize = sizeof(struct short_ad);
1968 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1969 adsize = sizeof(struct long_ad);
1974 WARN_ON(iinfo->i_lenAlloc !=
1975 epos->offset - udf_file_entry_alloc_offset(inode));
1977 aed = (struct allocExtDesc *)epos->bh->b_data;
1978 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1979 epos->offset - sizeof(struct allocExtDesc));
1980 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1983 udf_write_aext(inode, epos, eloc, elen, inc);
1986 iinfo->i_lenAlloc += adsize;
1987 mark_inode_dirty(inode);
1989 aed = (struct allocExtDesc *)epos->bh->b_data;
1990 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1991 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1992 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1993 udf_update_tag(epos->bh->b_data,
1994 epos->offset + (inc ? 0 : adsize));
1996 udf_update_tag(epos->bh->b_data,
1997 sizeof(struct allocExtDesc));
1998 mark_buffer_dirty_inode(epos->bh, inode);
2005 * Append extent at given position - should be the first free one in inode
2006 * / indirect extent. Takes care of allocating and linking indirect blocks.
2008 int udf_add_aext(struct inode *inode, struct extent_position *epos,
2009 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2012 struct super_block *sb = inode->i_sb;
2014 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2015 adsize = sizeof(struct short_ad);
2016 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2017 adsize = sizeof(struct long_ad);
2021 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
2023 udf_pblk_t new_block;
2025 new_block = udf_new_block(sb, NULL,
2026 epos->block.partitionReferenceNum,
2027 epos->block.logicalBlockNum, &err);
2031 err = udf_setup_indirect_aext(inode, new_block, epos);
2036 return __udf_add_aext(inode, epos, eloc, elen, inc);
2039 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2040 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2044 struct short_ad *sad;
2045 struct long_ad *lad;
2046 struct udf_inode_info *iinfo = UDF_I(inode);
2049 ptr = iinfo->i_ext.i_data + epos->offset -
2050 udf_file_entry_alloc_offset(inode) +
2053 ptr = epos->bh->b_data + epos->offset;
2055 switch (iinfo->i_alloc_type) {
2056 case ICBTAG_FLAG_AD_SHORT:
2057 sad = (struct short_ad *)ptr;
2058 sad->extLength = cpu_to_le32(elen);
2059 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2060 adsize = sizeof(struct short_ad);
2062 case ICBTAG_FLAG_AD_LONG:
2063 lad = (struct long_ad *)ptr;
2064 lad->extLength = cpu_to_le32(elen);
2065 lad->extLocation = cpu_to_lelb(*eloc);
2066 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2067 adsize = sizeof(struct long_ad);
2074 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2075 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2076 struct allocExtDesc *aed =
2077 (struct allocExtDesc *)epos->bh->b_data;
2078 udf_update_tag(epos->bh->b_data,
2079 le32_to_cpu(aed->lengthAllocDescs) +
2080 sizeof(struct allocExtDesc));
2082 mark_buffer_dirty_inode(epos->bh, inode);
2084 mark_inode_dirty(inode);
2088 epos->offset += adsize;
2092 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2093 * someone does some weird stuff.
2095 #define UDF_MAX_INDIR_EXTS 16
2097 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2098 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2101 unsigned int indirections = 0;
2103 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2104 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2107 if (++indirections > UDF_MAX_INDIR_EXTS) {
2108 udf_err(inode->i_sb,
2109 "too many indirect extents in inode %lu\n",
2114 epos->block = *eloc;
2115 epos->offset = sizeof(struct allocExtDesc);
2117 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2118 epos->bh = udf_tread(inode->i_sb, block);
2120 udf_debug("reading block %u failed!\n", block);
2128 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2129 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2134 struct short_ad *sad;
2135 struct long_ad *lad;
2136 struct udf_inode_info *iinfo = UDF_I(inode);
2140 epos->offset = udf_file_entry_alloc_offset(inode);
2141 ptr = iinfo->i_ext.i_data + epos->offset -
2142 udf_file_entry_alloc_offset(inode) +
2144 alen = udf_file_entry_alloc_offset(inode) +
2148 epos->offset = sizeof(struct allocExtDesc);
2149 ptr = epos->bh->b_data + epos->offset;
2150 alen = sizeof(struct allocExtDesc) +
2151 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2155 switch (iinfo->i_alloc_type) {
2156 case ICBTAG_FLAG_AD_SHORT:
2157 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2160 etype = le32_to_cpu(sad->extLength) >> 30;
2161 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2162 eloc->partitionReferenceNum =
2163 iinfo->i_location.partitionReferenceNum;
2164 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2166 case ICBTAG_FLAG_AD_LONG:
2167 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2170 etype = le32_to_cpu(lad->extLength) >> 30;
2171 *eloc = lelb_to_cpu(lad->extLocation);
2172 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2175 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
2182 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2183 struct kernel_lb_addr neloc, uint32_t nelen)
2185 struct kernel_lb_addr oeloc;
2192 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2193 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2195 nelen = (etype << 30) | oelen;
2197 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2200 return (nelen >> 30);
2203 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
2205 struct extent_position oepos;
2208 struct allocExtDesc *aed;
2209 struct udf_inode_info *iinfo;
2210 struct kernel_lb_addr eloc;
2218 iinfo = UDF_I(inode);
2219 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2220 adsize = sizeof(struct short_ad);
2221 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2222 adsize = sizeof(struct long_ad);
2227 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2230 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2231 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2232 if (oepos.bh != epos.bh) {
2233 oepos.block = epos.block;
2237 oepos.offset = epos.offset - adsize;
2240 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2243 if (epos.bh != oepos.bh) {
2244 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2245 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2246 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2248 iinfo->i_lenAlloc -= (adsize * 2);
2249 mark_inode_dirty(inode);
2251 aed = (struct allocExtDesc *)oepos.bh->b_data;
2252 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2253 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2254 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2255 udf_update_tag(oepos.bh->b_data,
2256 oepos.offset - (2 * adsize));
2258 udf_update_tag(oepos.bh->b_data,
2259 sizeof(struct allocExtDesc));
2260 mark_buffer_dirty_inode(oepos.bh, inode);
2263 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2265 iinfo->i_lenAlloc -= adsize;
2266 mark_inode_dirty(inode);
2268 aed = (struct allocExtDesc *)oepos.bh->b_data;
2269 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2270 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2271 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2272 udf_update_tag(oepos.bh->b_data,
2273 epos.offset - adsize);
2275 udf_update_tag(oepos.bh->b_data,
2276 sizeof(struct allocExtDesc));
2277 mark_buffer_dirty_inode(oepos.bh, inode);
2284 return (elen >> 30);
2287 int8_t inode_bmap(struct inode *inode, sector_t block,
2288 struct extent_position *pos, struct kernel_lb_addr *eloc,
2289 uint32_t *elen, sector_t *offset)
2291 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2292 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2294 struct udf_inode_info *iinfo;
2296 iinfo = UDF_I(inode);
2297 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2299 pos->block = iinfo->i_location;
2304 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2306 *offset = (bcount - lbcount) >> blocksize_bits;
2307 iinfo->i_lenExtents = lbcount;
2311 } while (lbcount <= bcount);
2312 /* update extent cache */
2313 udf_update_extent_cache(inode, lbcount - *elen, pos);
2314 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2319 udf_pblk_t udf_block_map(struct inode *inode, sector_t block)
2321 struct kernel_lb_addr eloc;
2324 struct extent_position epos = {};
2327 down_read(&UDF_I(inode)->i_data_sem);
2329 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2330 (EXT_RECORDED_ALLOCATED >> 30))
2331 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2335 up_read(&UDF_I(inode)->i_data_sem);
2338 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2339 return udf_fixed_to_variable(ret);