5 * Inode handling routines for the OSTA-UDF(tm) filesystem.
8 * This file is distributed under the terms of the GNU General Public
9 * License (GPL). Copies of the GPL can be obtained from:
10 * ftp://prep.ai.mit.edu/pub/gnu/GPL
11 * Each contributing author retains all rights to their own work.
13 * (C) 1998 Dave Boynton
14 * (C) 1998-2004 Ben Fennema
15 * (C) 1999-2000 Stelias Computing Inc
19 * 10/04/98 dgb Added rudimentary directory functions
20 * 10/07/98 Fully working udf_block_map! It works!
21 * 11/25/98 bmap altered to better support extents
22 * 12/06/98 blf partition support in udf_iget, udf_block_map
24 * 12/12/98 rewrote udf_block_map to handle next extents and descs across
25 * block boundaries (which is not actually allowed)
26 * 12/20/98 added support for strategy 4096
27 * 03/07/99 rewrote udf_block_map (again)
28 * New funcs, inode_bmap, udf_next_aext
29 * 04/19/99 Support for writing device EA's for major/minor #
34 #include <linux/module.h>
35 #include <linux/pagemap.h>
36 #include <linux/writeback.h>
37 #include <linux/slab.h>
38 #include <linux/crc-itu-t.h>
39 #include <linux/mpage.h>
40 #include <linux/uio.h>
45 MODULE_AUTHOR("Ben Fennema");
46 MODULE_DESCRIPTION("Universal Disk Format Filesystem");
47 MODULE_LICENSE("GPL");
49 #define EXTENT_MERGE_SIZE 5
51 static umode_t udf_convert_permissions(struct fileEntry *);
52 static int udf_update_inode(struct inode *, int);
53 static int udf_sync_inode(struct inode *inode);
54 static int udf_alloc_i_data(struct inode *inode, size_t size);
55 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
56 static int8_t udf_insert_aext(struct inode *, struct extent_position,
57 struct kernel_lb_addr, uint32_t);
58 static void udf_split_extents(struct inode *, int *, int, int,
59 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
60 static void udf_prealloc_extents(struct inode *, int, int,
61 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
62 static void udf_merge_extents(struct inode *,
63 struct kernel_long_ad[EXTENT_MERGE_SIZE], int *);
64 static void udf_update_extents(struct inode *,
65 struct kernel_long_ad[EXTENT_MERGE_SIZE], int, int,
66 struct extent_position *);
67 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
69 static void __udf_clear_extent_cache(struct inode *inode)
71 struct udf_inode_info *iinfo = UDF_I(inode);
73 if (iinfo->cached_extent.lstart != -1) {
74 brelse(iinfo->cached_extent.epos.bh);
75 iinfo->cached_extent.lstart = -1;
79 /* Invalidate extent cache */
80 static void udf_clear_extent_cache(struct inode *inode)
82 struct udf_inode_info *iinfo = UDF_I(inode);
84 spin_lock(&iinfo->i_extent_cache_lock);
85 __udf_clear_extent_cache(inode);
86 spin_unlock(&iinfo->i_extent_cache_lock);
89 /* Return contents of extent cache */
90 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
91 loff_t *lbcount, struct extent_position *pos)
93 struct udf_inode_info *iinfo = UDF_I(inode);
96 spin_lock(&iinfo->i_extent_cache_lock);
97 if ((iinfo->cached_extent.lstart <= bcount) &&
98 (iinfo->cached_extent.lstart != -1)) {
100 *lbcount = iinfo->cached_extent.lstart;
101 memcpy(pos, &iinfo->cached_extent.epos,
102 sizeof(struct extent_position));
107 spin_unlock(&iinfo->i_extent_cache_lock);
111 /* Add extent to extent cache */
112 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
113 struct extent_position *pos, int next_epos)
115 struct udf_inode_info *iinfo = UDF_I(inode);
117 spin_lock(&iinfo->i_extent_cache_lock);
118 /* Invalidate previously cached extent */
119 __udf_clear_extent_cache(inode);
122 memcpy(&iinfo->cached_extent.epos, pos,
123 sizeof(struct extent_position));
124 iinfo->cached_extent.lstart = estart;
126 switch (iinfo->i_alloc_type) {
127 case ICBTAG_FLAG_AD_SHORT:
128 iinfo->cached_extent.epos.offset -=
129 sizeof(struct short_ad);
131 case ICBTAG_FLAG_AD_LONG:
132 iinfo->cached_extent.epos.offset -=
133 sizeof(struct long_ad);
135 spin_unlock(&iinfo->i_extent_cache_lock);
138 void udf_evict_inode(struct inode *inode)
140 struct udf_inode_info *iinfo = UDF_I(inode);
143 if (!inode->i_nlink && !is_bad_inode(inode)) {
145 udf_setsize(inode, 0);
146 udf_update_inode(inode, IS_SYNC(inode));
148 truncate_inode_pages_final(&inode->i_data);
149 invalidate_inode_buffers(inode);
151 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
152 inode->i_size != iinfo->i_lenExtents) {
153 udf_warn(inode->i_sb, "Inode %lu (mode %o) has inode size %llu different from extent length %llu. Filesystem need not be standards compliant.\n",
154 inode->i_ino, inode->i_mode,
155 (unsigned long long)inode->i_size,
156 (unsigned long long)iinfo->i_lenExtents);
158 kfree(iinfo->i_ext.i_data);
159 iinfo->i_ext.i_data = NULL;
160 udf_clear_extent_cache(inode);
162 udf_free_inode(inode);
166 static void udf_write_failed(struct address_space *mapping, loff_t to)
168 struct inode *inode = mapping->host;
169 struct udf_inode_info *iinfo = UDF_I(inode);
170 loff_t isize = inode->i_size;
173 truncate_pagecache(inode, isize);
174 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
175 down_write(&iinfo->i_data_sem);
176 udf_clear_extent_cache(inode);
177 udf_truncate_extents(inode);
178 up_write(&iinfo->i_data_sem);
183 static int udf_writepage(struct page *page, struct writeback_control *wbc)
185 return block_write_full_page(page, udf_get_block, wbc);
188 static int udf_writepages(struct address_space *mapping,
189 struct writeback_control *wbc)
191 return mpage_writepages(mapping, wbc, udf_get_block);
194 static int udf_readpage(struct file *file, struct page *page)
196 return mpage_readpage(page, udf_get_block);
199 static int udf_readpages(struct file *file, struct address_space *mapping,
200 struct list_head *pages, unsigned nr_pages)
202 return mpage_readpages(mapping, pages, nr_pages, udf_get_block);
205 static int udf_write_begin(struct file *file, struct address_space *mapping,
206 loff_t pos, unsigned len, unsigned flags,
207 struct page **pagep, void **fsdata)
211 ret = block_write_begin(mapping, pos, len, flags, pagep, udf_get_block);
213 udf_write_failed(mapping, pos + len);
217 static ssize_t udf_direct_IO(int rw, struct kiocb *iocb,
218 struct iov_iter *iter,
221 struct file *file = iocb->ki_filp;
222 struct address_space *mapping = file->f_mapping;
223 struct inode *inode = mapping->host;
224 size_t count = iov_iter_count(iter);
227 ret = blockdev_direct_IO(rw, iocb, inode, iter, offset, udf_get_block);
228 if (unlikely(ret < 0 && (rw & WRITE)))
229 udf_write_failed(mapping, offset + count);
233 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
235 return generic_block_bmap(mapping, block, udf_get_block);
238 const struct address_space_operations udf_aops = {
239 .readpage = udf_readpage,
240 .readpages = udf_readpages,
241 .writepage = udf_writepage,
242 .writepages = udf_writepages,
243 .write_begin = udf_write_begin,
244 .write_end = generic_write_end,
245 .direct_IO = udf_direct_IO,
250 * Expand file stored in ICB to a normal one-block-file
252 * This function requires i_data_sem for writing and releases it.
253 * This function requires i_mutex held
255 int udf_expand_file_adinicb(struct inode *inode)
259 struct udf_inode_info *iinfo = UDF_I(inode);
261 struct writeback_control udf_wbc = {
262 .sync_mode = WB_SYNC_NONE,
266 WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex));
267 if (!iinfo->i_lenAlloc) {
268 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
269 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
271 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
272 /* from now on we have normal address_space methods */
273 inode->i_data.a_ops = &udf_aops;
274 up_write(&iinfo->i_data_sem);
275 mark_inode_dirty(inode);
279 * Release i_data_sem so that we can lock a page - page lock ranks
280 * above i_data_sem. i_mutex still protects us against file changes.
282 up_write(&iinfo->i_data_sem);
284 page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
288 if (!PageUptodate(page)) {
290 memset(kaddr + iinfo->i_lenAlloc, 0x00,
291 PAGE_CACHE_SIZE - iinfo->i_lenAlloc);
292 memcpy(kaddr, iinfo->i_ext.i_data + iinfo->i_lenEAttr,
294 flush_dcache_page(page);
295 SetPageUptodate(page);
298 down_write(&iinfo->i_data_sem);
299 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0x00,
301 iinfo->i_lenAlloc = 0;
302 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
303 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
305 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
306 /* from now on we have normal address_space methods */
307 inode->i_data.a_ops = &udf_aops;
308 up_write(&iinfo->i_data_sem);
309 err = inode->i_data.a_ops->writepage(page, &udf_wbc);
311 /* Restore everything back so that we don't lose data... */
314 down_write(&iinfo->i_data_sem);
315 memcpy(iinfo->i_ext.i_data + iinfo->i_lenEAttr, kaddr,
319 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
320 inode->i_data.a_ops = &udf_adinicb_aops;
321 up_write(&iinfo->i_data_sem);
323 page_cache_release(page);
324 mark_inode_dirty(inode);
329 struct buffer_head *udf_expand_dir_adinicb(struct inode *inode, int *block,
333 struct buffer_head *dbh = NULL;
334 struct kernel_lb_addr eloc;
336 struct extent_position epos;
338 struct udf_fileident_bh sfibh, dfibh;
339 loff_t f_pos = udf_ext0_offset(inode);
340 int size = udf_ext0_offset(inode) + inode->i_size;
341 struct fileIdentDesc cfi, *sfi, *dfi;
342 struct udf_inode_info *iinfo = UDF_I(inode);
344 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
345 alloctype = ICBTAG_FLAG_AD_SHORT;
347 alloctype = ICBTAG_FLAG_AD_LONG;
349 if (!inode->i_size) {
350 iinfo->i_alloc_type = alloctype;
351 mark_inode_dirty(inode);
355 /* alloc block, and copy data to it */
356 *block = udf_new_block(inode->i_sb, inode,
357 iinfo->i_location.partitionReferenceNum,
358 iinfo->i_location.logicalBlockNum, err);
361 newblock = udf_get_pblock(inode->i_sb, *block,
362 iinfo->i_location.partitionReferenceNum,
366 dbh = udf_tgetblk(inode->i_sb, newblock);
370 memset(dbh->b_data, 0x00, inode->i_sb->s_blocksize);
371 set_buffer_uptodate(dbh);
373 mark_buffer_dirty_inode(dbh, inode);
375 sfibh.soffset = sfibh.eoffset =
376 f_pos & (inode->i_sb->s_blocksize - 1);
377 sfibh.sbh = sfibh.ebh = NULL;
378 dfibh.soffset = dfibh.eoffset = 0;
379 dfibh.sbh = dfibh.ebh = dbh;
380 while (f_pos < size) {
381 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
382 sfi = udf_fileident_read(inode, &f_pos, &sfibh, &cfi, NULL,
388 iinfo->i_alloc_type = alloctype;
389 sfi->descTag.tagLocation = cpu_to_le32(*block);
390 dfibh.soffset = dfibh.eoffset;
391 dfibh.eoffset += (sfibh.eoffset - sfibh.soffset);
392 dfi = (struct fileIdentDesc *)(dbh->b_data + dfibh.soffset);
393 if (udf_write_fi(inode, sfi, dfi, &dfibh, sfi->impUse,
395 le16_to_cpu(sfi->lengthOfImpUse))) {
396 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
401 mark_buffer_dirty_inode(dbh, inode);
403 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr, 0,
405 iinfo->i_lenAlloc = 0;
406 eloc.logicalBlockNum = *block;
407 eloc.partitionReferenceNum =
408 iinfo->i_location.partitionReferenceNum;
409 iinfo->i_lenExtents = inode->i_size;
411 epos.block = iinfo->i_location;
412 epos.offset = udf_file_entry_alloc_offset(inode);
413 udf_add_aext(inode, &epos, &eloc, inode->i_size, 0);
417 mark_inode_dirty(inode);
421 static int udf_get_block(struct inode *inode, sector_t block,
422 struct buffer_head *bh_result, int create)
426 struct udf_inode_info *iinfo;
429 phys = udf_block_map(inode, block);
431 map_bh(bh_result, inode->i_sb, phys);
437 iinfo = UDF_I(inode);
439 down_write(&iinfo->i_data_sem);
440 if (block == iinfo->i_next_alloc_block + 1) {
441 iinfo->i_next_alloc_block++;
442 iinfo->i_next_alloc_goal++;
445 udf_clear_extent_cache(inode);
446 phys = inode_getblk(inode, block, &err, &new);
451 set_buffer_new(bh_result);
452 map_bh(bh_result, inode->i_sb, phys);
455 up_write(&iinfo->i_data_sem);
459 static struct buffer_head *udf_getblk(struct inode *inode, long block,
460 int create, int *err)
462 struct buffer_head *bh;
463 struct buffer_head dummy;
466 dummy.b_blocknr = -1000;
467 *err = udf_get_block(inode, block, &dummy, create);
468 if (!*err && buffer_mapped(&dummy)) {
469 bh = sb_getblk(inode->i_sb, dummy.b_blocknr);
470 if (buffer_new(&dummy)) {
472 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
473 set_buffer_uptodate(bh);
475 mark_buffer_dirty_inode(bh, inode);
483 /* Extend the file by 'blocks' blocks, 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,
490 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
491 struct super_block *sb = inode->i_sb;
492 struct kernel_lb_addr prealloc_loc = {};
493 int prealloc_len = 0;
494 struct udf_inode_info *iinfo;
497 /* The previous extent is fake and we should not extend by anything
498 * - there's nothing to do... */
502 iinfo = UDF_I(inode);
503 /* Round the last extent up to a multiple of block size */
504 if (last_ext->extLength & (sb->s_blocksize - 1)) {
505 last_ext->extLength =
506 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
507 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
508 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
509 iinfo->i_lenExtents =
510 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
511 ~(sb->s_blocksize - 1);
514 /* Last extent are just preallocated blocks? */
515 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
516 EXT_NOT_RECORDED_ALLOCATED) {
517 /* Save the extent so that we can reattach it to the end */
518 prealloc_loc = last_ext->extLocation;
519 prealloc_len = last_ext->extLength;
520 /* Mark the extent as a hole */
521 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
522 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
523 last_ext->extLocation.logicalBlockNum = 0;
524 last_ext->extLocation.partitionReferenceNum = 0;
527 /* Can we merge with the previous extent? */
528 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
529 EXT_NOT_RECORDED_NOT_ALLOCATED) {
530 add = ((1 << 30) - sb->s_blocksize -
531 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK)) >>
532 sb->s_blocksize_bits;
536 last_ext->extLength += add << sb->s_blocksize_bits;
540 udf_add_aext(inode, last_pos, &last_ext->extLocation,
541 last_ext->extLength, 1);
544 udf_write_aext(inode, last_pos, &last_ext->extLocation,
545 last_ext->extLength, 1);
547 /* Managed to do everything necessary? */
551 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
552 last_ext->extLocation.logicalBlockNum = 0;
553 last_ext->extLocation.partitionReferenceNum = 0;
554 add = (1 << (30-sb->s_blocksize_bits)) - 1;
555 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
556 (add << sb->s_blocksize_bits);
558 /* Create enough extents to cover the whole hole */
559 while (blocks > add) {
561 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
562 last_ext->extLength, 1);
568 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
569 (blocks << sb->s_blocksize_bits);
570 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
571 last_ext->extLength, 1);
578 /* Do we have some preallocated blocks saved? */
580 err = udf_add_aext(inode, last_pos, &prealloc_loc,
584 last_ext->extLocation = prealloc_loc;
585 last_ext->extLength = prealloc_len;
589 /* last_pos should point to the last written extent... */
590 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
591 last_pos->offset -= sizeof(struct short_ad);
592 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
593 last_pos->offset -= sizeof(struct long_ad);
600 static int udf_extend_file(struct inode *inode, loff_t newsize)
603 struct extent_position epos;
604 struct kernel_lb_addr eloc;
607 struct super_block *sb = inode->i_sb;
608 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
610 struct udf_inode_info *iinfo = UDF_I(inode);
611 struct kernel_long_ad extent;
614 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
615 adsize = sizeof(struct short_ad);
616 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
617 adsize = sizeof(struct long_ad);
621 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
623 /* File has extent covering the new size (could happen when extending
624 * inside a block)? */
627 if (newsize & (sb->s_blocksize - 1))
629 /* Extended file just to the boundary of the last file block? */
633 /* Truncate is extending the file by 'offset' blocks */
634 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
635 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
636 /* File has no extents at all or has empty last
637 * indirect extent! Create a fake extent... */
638 extent.extLocation.logicalBlockNum = 0;
639 extent.extLocation.partitionReferenceNum = 0;
640 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
642 epos.offset -= adsize;
643 etype = udf_next_aext(inode, &epos, &extent.extLocation,
644 &extent.extLength, 0);
645 extent.extLength |= etype << 30;
647 err = udf_do_extend_file(inode, &epos, &extent, offset);
651 iinfo->i_lenExtents = newsize;
657 static sector_t inode_getblk(struct inode *inode, sector_t block,
660 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
661 struct extent_position prev_epos, cur_epos, next_epos;
662 int count = 0, startnum = 0, endnum = 0;
663 uint32_t elen = 0, tmpelen;
664 struct kernel_lb_addr eloc, tmpeloc;
666 loff_t lbcount = 0, b_off = 0;
667 uint32_t newblocknum, newblock;
670 struct udf_inode_info *iinfo = UDF_I(inode);
671 int goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
677 prev_epos.offset = udf_file_entry_alloc_offset(inode);
678 prev_epos.block = iinfo->i_location;
680 cur_epos = next_epos = prev_epos;
681 b_off = (loff_t)block << inode->i_sb->s_blocksize_bits;
683 /* find the extent which contains the block we are looking for.
684 alternate between laarr[0] and laarr[1] for locations of the
685 current extent, and the previous extent */
687 if (prev_epos.bh != cur_epos.bh) {
688 brelse(prev_epos.bh);
690 prev_epos.bh = cur_epos.bh;
692 if (cur_epos.bh != next_epos.bh) {
694 get_bh(next_epos.bh);
695 cur_epos.bh = next_epos.bh;
700 prev_epos.block = cur_epos.block;
701 cur_epos.block = next_epos.block;
703 prev_epos.offset = cur_epos.offset;
704 cur_epos.offset = next_epos.offset;
706 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
712 laarr[c].extLength = (etype << 30) | elen;
713 laarr[c].extLocation = eloc;
715 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
716 pgoal = eloc.logicalBlockNum +
717 ((elen + inode->i_sb->s_blocksize - 1) >>
718 inode->i_sb->s_blocksize_bits);
721 } while (lbcount + elen <= b_off);
724 offset = b_off >> inode->i_sb->s_blocksize_bits;
726 * Move prev_epos and cur_epos into indirect extent if we are at
729 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
730 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
732 /* if the extent is allocated and recorded, return the block
733 if the extent is not a multiple of the blocksize, round up */
735 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
736 if (elen & (inode->i_sb->s_blocksize - 1)) {
737 elen = EXT_RECORDED_ALLOCATED |
738 ((elen + inode->i_sb->s_blocksize - 1) &
739 ~(inode->i_sb->s_blocksize - 1));
740 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
742 brelse(prev_epos.bh);
744 brelse(next_epos.bh);
745 newblock = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
749 /* Are we beyond EOF? */
758 /* Create a fake extent when there's not one */
759 memset(&laarr[0].extLocation, 0x00,
760 sizeof(struct kernel_lb_addr));
761 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
762 /* Will udf_do_extend_file() create real extent from
764 startnum = (offset > 0);
766 /* Create extents for the hole between EOF and offset */
767 ret = udf_do_extend_file(inode, &prev_epos, laarr, offset);
769 brelse(prev_epos.bh);
771 brelse(next_epos.bh);
778 /* We are not covered by a preallocated extent? */
779 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
780 EXT_NOT_RECORDED_ALLOCATED) {
781 /* Is there any real extent? - otherwise we overwrite
785 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
786 inode->i_sb->s_blocksize;
787 memset(&laarr[c].extLocation, 0x00,
788 sizeof(struct kernel_lb_addr));
795 endnum = startnum = ((count > 2) ? 2 : count);
797 /* if the current extent is in position 0,
798 swap it with the previous */
799 if (!c && count != 1) {
806 /* if the current block is located in an extent,
807 read the next extent */
808 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
810 laarr[c + 1].extLength = (etype << 30) | elen;
811 laarr[c + 1].extLocation = eloc;
819 /* if the current extent is not recorded but allocated, get the
820 * block in the extent corresponding to the requested block */
821 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
822 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
823 else { /* otherwise, allocate a new block */
824 if (iinfo->i_next_alloc_block == block)
825 goal = iinfo->i_next_alloc_goal;
828 if (!(goal = pgoal)) /* XXX: what was intended here? */
829 goal = iinfo->i_location.logicalBlockNum + 1;
832 newblocknum = udf_new_block(inode->i_sb, inode,
833 iinfo->i_location.partitionReferenceNum,
836 brelse(prev_epos.bh);
838 brelse(next_epos.bh);
843 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
846 /* if the extent the requsted block is located in contains multiple
847 * blocks, split the extent into at most three extents. blocks prior
848 * to requested block, requested block, and blocks after requested
850 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
852 #ifdef UDF_PREALLOCATE
853 /* We preallocate blocks only for regular files. It also makes sense
854 * for directories but there's a problem when to drop the
855 * preallocation. We might use some delayed work for that but I feel
856 * it's overengineering for a filesystem like UDF. */
857 if (S_ISREG(inode->i_mode))
858 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
861 /* merge any continuous blocks in laarr */
862 udf_merge_extents(inode, laarr, &endnum);
864 /* write back the new extents, inserting new extents if the new number
865 * of extents is greater than the old number, and deleting extents if
866 * the new number of extents is less than the old number */
867 udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
869 brelse(prev_epos.bh);
871 brelse(next_epos.bh);
873 newblock = udf_get_pblock(inode->i_sb, newblocknum,
874 iinfo->i_location.partitionReferenceNum, 0);
880 iinfo->i_next_alloc_block = block;
881 iinfo->i_next_alloc_goal = newblocknum;
882 inode->i_ctime = current_fs_time(inode->i_sb);
885 udf_sync_inode(inode);
887 mark_inode_dirty(inode);
892 static void udf_split_extents(struct inode *inode, int *c, int offset,
894 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
897 unsigned long blocksize = inode->i_sb->s_blocksize;
898 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
900 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
901 (laarr[*c].extLength >> 30) ==
902 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
904 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
905 blocksize - 1) >> blocksize_bits;
906 int8_t etype = (laarr[curr].extLength >> 30);
910 else if (!offset || blen == offset + 1) {
911 laarr[curr + 2] = laarr[curr + 1];
912 laarr[curr + 1] = laarr[curr];
914 laarr[curr + 3] = laarr[curr + 1];
915 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
919 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
920 udf_free_blocks(inode->i_sb, inode,
921 &laarr[curr].extLocation,
923 laarr[curr].extLength =
924 EXT_NOT_RECORDED_NOT_ALLOCATED |
925 (offset << blocksize_bits);
926 laarr[curr].extLocation.logicalBlockNum = 0;
927 laarr[curr].extLocation.
928 partitionReferenceNum = 0;
930 laarr[curr].extLength = (etype << 30) |
931 (offset << blocksize_bits);
937 laarr[curr].extLocation.logicalBlockNum = newblocknum;
938 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
939 laarr[curr].extLocation.partitionReferenceNum =
940 UDF_I(inode)->i_location.partitionReferenceNum;
941 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
945 if (blen != offset + 1) {
946 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
947 laarr[curr].extLocation.logicalBlockNum +=
949 laarr[curr].extLength = (etype << 30) |
950 ((blen - (offset + 1)) << blocksize_bits);
957 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
958 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
961 int start, length = 0, currlength = 0, i;
963 if (*endnum >= (c + 1)) {
969 if ((laarr[c + 1].extLength >> 30) ==
970 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
972 length = currlength =
973 (((laarr[c + 1].extLength &
974 UDF_EXTENT_LENGTH_MASK) +
975 inode->i_sb->s_blocksize - 1) >>
976 inode->i_sb->s_blocksize_bits);
981 for (i = start + 1; i <= *endnum; i++) {
984 length += UDF_DEFAULT_PREALLOC_BLOCKS;
985 } else if ((laarr[i].extLength >> 30) ==
986 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
987 length += (((laarr[i].extLength &
988 UDF_EXTENT_LENGTH_MASK) +
989 inode->i_sb->s_blocksize - 1) >>
990 inode->i_sb->s_blocksize_bits);
996 int next = laarr[start].extLocation.logicalBlockNum +
997 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
998 inode->i_sb->s_blocksize - 1) >>
999 inode->i_sb->s_blocksize_bits);
1000 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
1001 laarr[start].extLocation.partitionReferenceNum,
1002 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
1003 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
1006 if (start == (c + 1))
1007 laarr[start].extLength +=
1009 inode->i_sb->s_blocksize_bits);
1011 memmove(&laarr[c + 2], &laarr[c + 1],
1012 sizeof(struct long_ad) * (*endnum - (c + 1)));
1014 laarr[c + 1].extLocation.logicalBlockNum = next;
1015 laarr[c + 1].extLocation.partitionReferenceNum =
1016 laarr[c].extLocation.
1017 partitionReferenceNum;
1018 laarr[c + 1].extLength =
1019 EXT_NOT_RECORDED_ALLOCATED |
1021 inode->i_sb->s_blocksize_bits);
1025 for (i = start + 1; numalloc && i < *endnum; i++) {
1026 int elen = ((laarr[i].extLength &
1027 UDF_EXTENT_LENGTH_MASK) +
1028 inode->i_sb->s_blocksize - 1) >>
1029 inode->i_sb->s_blocksize_bits;
1031 if (elen > numalloc) {
1032 laarr[i].extLength -=
1034 inode->i_sb->s_blocksize_bits);
1038 if (*endnum > (i + 1))
1041 sizeof(struct long_ad) *
1042 (*endnum - (i + 1)));
1047 UDF_I(inode)->i_lenExtents +=
1048 numalloc << inode->i_sb->s_blocksize_bits;
1053 static void udf_merge_extents(struct inode *inode,
1054 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
1058 unsigned long blocksize = inode->i_sb->s_blocksize;
1059 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1061 for (i = 0; i < (*endnum - 1); i++) {
1062 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
1063 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
1065 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
1066 (((li->extLength >> 30) ==
1067 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1068 ((lip1->extLocation.logicalBlockNum -
1069 li->extLocation.logicalBlockNum) ==
1070 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1071 blocksize - 1) >> blocksize_bits)))) {
1073 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1074 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1075 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1076 lip1->extLength = (lip1->extLength -
1078 UDF_EXTENT_LENGTH_MASK) +
1079 UDF_EXTENT_LENGTH_MASK) &
1081 li->extLength = (li->extLength &
1082 UDF_EXTENT_FLAG_MASK) +
1083 (UDF_EXTENT_LENGTH_MASK + 1) -
1085 lip1->extLocation.logicalBlockNum =
1086 li->extLocation.logicalBlockNum +
1088 UDF_EXTENT_LENGTH_MASK) >>
1091 li->extLength = lip1->extLength +
1093 UDF_EXTENT_LENGTH_MASK) +
1094 blocksize - 1) & ~(blocksize - 1));
1095 if (*endnum > (i + 2))
1096 memmove(&laarr[i + 1], &laarr[i + 2],
1097 sizeof(struct long_ad) *
1098 (*endnum - (i + 2)));
1102 } else if (((li->extLength >> 30) ==
1103 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1104 ((lip1->extLength >> 30) ==
1105 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1106 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1108 UDF_EXTENT_LENGTH_MASK) +
1109 blocksize - 1) >> blocksize_bits);
1110 li->extLocation.logicalBlockNum = 0;
1111 li->extLocation.partitionReferenceNum = 0;
1113 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1114 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1115 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1116 lip1->extLength = (lip1->extLength -
1118 UDF_EXTENT_LENGTH_MASK) +
1119 UDF_EXTENT_LENGTH_MASK) &
1121 li->extLength = (li->extLength &
1122 UDF_EXTENT_FLAG_MASK) +
1123 (UDF_EXTENT_LENGTH_MASK + 1) -
1126 li->extLength = lip1->extLength +
1128 UDF_EXTENT_LENGTH_MASK) +
1129 blocksize - 1) & ~(blocksize - 1));
1130 if (*endnum > (i + 2))
1131 memmove(&laarr[i + 1], &laarr[i + 2],
1132 sizeof(struct long_ad) *
1133 (*endnum - (i + 2)));
1137 } else if ((li->extLength >> 30) ==
1138 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1139 udf_free_blocks(inode->i_sb, inode,
1140 &li->extLocation, 0,
1142 UDF_EXTENT_LENGTH_MASK) +
1143 blocksize - 1) >> blocksize_bits);
1144 li->extLocation.logicalBlockNum = 0;
1145 li->extLocation.partitionReferenceNum = 0;
1146 li->extLength = (li->extLength &
1147 UDF_EXTENT_LENGTH_MASK) |
1148 EXT_NOT_RECORDED_NOT_ALLOCATED;
1153 static void udf_update_extents(struct inode *inode,
1154 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE],
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, laarr[i].extLocation,
1165 laarr[i].extLength);
1166 } else if (startnum < endnum) {
1167 for (i = 0; i < (endnum - startnum); i++) {
1168 udf_insert_aext(inode, *epos, laarr[i].extLocation,
1169 laarr[i].extLength);
1170 udf_next_aext(inode, epos, &laarr[i].extLocation,
1171 &laarr[i].extLength, 1);
1176 for (i = start; i < endnum; i++) {
1177 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1178 udf_write_aext(inode, epos, &laarr[i].extLocation,
1179 laarr[i].extLength, 1);
1183 struct buffer_head *udf_bread(struct inode *inode, int block,
1184 int create, int *err)
1186 struct buffer_head *bh = NULL;
1188 bh = udf_getblk(inode, block, create, err);
1192 if (buffer_uptodate(bh))
1195 ll_rw_block(READ, 1, &bh);
1198 if (buffer_uptodate(bh))
1206 int udf_setsize(struct inode *inode, loff_t newsize)
1209 struct udf_inode_info *iinfo;
1210 int bsize = 1 << inode->i_blkbits;
1212 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1213 S_ISLNK(inode->i_mode)))
1215 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1218 iinfo = UDF_I(inode);
1219 if (newsize > inode->i_size) {
1220 down_write(&iinfo->i_data_sem);
1221 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1223 (udf_file_entry_alloc_offset(inode) + newsize)) {
1224 err = udf_expand_file_adinicb(inode);
1227 down_write(&iinfo->i_data_sem);
1229 iinfo->i_lenAlloc = newsize;
1233 err = udf_extend_file(inode, newsize);
1235 up_write(&iinfo->i_data_sem);
1239 truncate_setsize(inode, newsize);
1240 up_write(&iinfo->i_data_sem);
1242 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1243 down_write(&iinfo->i_data_sem);
1244 udf_clear_extent_cache(inode);
1245 memset(iinfo->i_ext.i_data + iinfo->i_lenEAttr + newsize,
1246 0x00, bsize - newsize -
1247 udf_file_entry_alloc_offset(inode));
1248 iinfo->i_lenAlloc = newsize;
1249 truncate_setsize(inode, newsize);
1250 up_write(&iinfo->i_data_sem);
1253 err = block_truncate_page(inode->i_mapping, newsize,
1257 down_write(&iinfo->i_data_sem);
1258 udf_clear_extent_cache(inode);
1259 truncate_setsize(inode, newsize);
1260 udf_truncate_extents(inode);
1261 up_write(&iinfo->i_data_sem);
1264 inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
1266 udf_sync_inode(inode);
1268 mark_inode_dirty(inode);
1273 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1274 * arbitrary - just that we hopefully don't limit any real use of rewritten
1275 * inode on write-once media but avoid looping for too long on corrupted media.
1277 #define UDF_MAX_ICB_NESTING 1024
1279 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1281 struct buffer_head *bh = NULL;
1282 struct fileEntry *fe;
1283 struct extendedFileEntry *efe;
1285 struct udf_inode_info *iinfo = UDF_I(inode);
1286 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1287 struct kernel_lb_addr *iloc = &iinfo->i_location;
1288 unsigned int link_count;
1289 unsigned int indirections = 0;
1290 int bs = inode->i_sb->s_blocksize;
1294 if (iloc->logicalBlockNum >=
1295 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1296 udf_debug("block=%d, partition=%d out of range\n",
1297 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1302 * Set defaults, but the inode is still incomplete!
1303 * Note: get_new_inode() sets the following on a new inode:
1306 * i_flags = sb->s_flags
1308 * clean_inode(): zero fills and sets
1313 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1315 udf_err(inode->i_sb, "(ino %ld) failed !bh\n", inode->i_ino);
1319 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1320 ident != TAG_IDENT_USE) {
1321 udf_err(inode->i_sb, "(ino %ld) failed ident=%d\n",
1322 inode->i_ino, ident);
1326 fe = (struct fileEntry *)bh->b_data;
1327 efe = (struct extendedFileEntry *)bh->b_data;
1329 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1330 struct buffer_head *ibh;
1332 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1333 if (ident == TAG_IDENT_IE && ibh) {
1334 struct kernel_lb_addr loc;
1335 struct indirectEntry *ie;
1337 ie = (struct indirectEntry *)ibh->b_data;
1338 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1340 if (ie->indirectICB.extLength) {
1342 memcpy(&iinfo->i_location, &loc,
1343 sizeof(struct kernel_lb_addr));
1344 if (++indirections > UDF_MAX_ICB_NESTING) {
1345 udf_err(inode->i_sb,
1346 "too many ICBs in ICB hierarchy"
1347 " (max %d supported)\n",
1348 UDF_MAX_ICB_NESTING);
1356 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1357 udf_err(inode->i_sb, "unsupported strategy type: %d\n",
1358 le16_to_cpu(fe->icbTag.strategyType));
1361 if (fe->icbTag.strategyType == cpu_to_le16(4))
1362 iinfo->i_strat4096 = 0;
1363 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1364 iinfo->i_strat4096 = 1;
1366 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1367 ICBTAG_FLAG_AD_MASK;
1368 iinfo->i_unique = 0;
1369 iinfo->i_lenEAttr = 0;
1370 iinfo->i_lenExtents = 0;
1371 iinfo->i_lenAlloc = 0;
1372 iinfo->i_next_alloc_block = 0;
1373 iinfo->i_next_alloc_goal = 0;
1374 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1377 ret = udf_alloc_i_data(inode, bs -
1378 sizeof(struct extendedFileEntry));
1381 memcpy(iinfo->i_ext.i_data,
1382 bh->b_data + sizeof(struct extendedFileEntry),
1383 bs - sizeof(struct extendedFileEntry));
1384 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1387 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1390 memcpy(iinfo->i_ext.i_data,
1391 bh->b_data + sizeof(struct fileEntry),
1392 bs - sizeof(struct fileEntry));
1393 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1396 iinfo->i_lenAlloc = le32_to_cpu(
1397 ((struct unallocSpaceEntry *)bh->b_data)->
1399 ret = udf_alloc_i_data(inode, bs -
1400 sizeof(struct unallocSpaceEntry));
1403 memcpy(iinfo->i_ext.i_data,
1404 bh->b_data + sizeof(struct unallocSpaceEntry),
1405 bs - sizeof(struct unallocSpaceEntry));
1410 read_lock(&sbi->s_cred_lock);
1411 i_uid_write(inode, le32_to_cpu(fe->uid));
1412 if (!uid_valid(inode->i_uid) ||
1413 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_IGNORE) ||
1414 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1415 inode->i_uid = UDF_SB(inode->i_sb)->s_uid;
1417 i_gid_write(inode, le32_to_cpu(fe->gid));
1418 if (!gid_valid(inode->i_gid) ||
1419 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_IGNORE) ||
1420 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1421 inode->i_gid = UDF_SB(inode->i_sb)->s_gid;
1423 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1424 sbi->s_fmode != UDF_INVALID_MODE)
1425 inode->i_mode = sbi->s_fmode;
1426 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1427 sbi->s_dmode != UDF_INVALID_MODE)
1428 inode->i_mode = sbi->s_dmode;
1430 inode->i_mode = udf_convert_permissions(fe);
1431 inode->i_mode &= ~sbi->s_umask;
1432 read_unlock(&sbi->s_cred_lock);
1434 link_count = le16_to_cpu(fe->fileLinkCount);
1436 if (!hidden_inode) {
1442 set_nlink(inode, link_count);
1444 inode->i_size = le64_to_cpu(fe->informationLength);
1445 iinfo->i_lenExtents = inode->i_size;
1447 if (iinfo->i_efe == 0) {
1448 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1449 (inode->i_sb->s_blocksize_bits - 9);
1451 if (!udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime))
1452 inode->i_atime = sbi->s_record_time;
1454 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1455 fe->modificationTime))
1456 inode->i_mtime = sbi->s_record_time;
1458 if (!udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime))
1459 inode->i_ctime = sbi->s_record_time;
1461 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1462 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1463 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1464 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1466 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1467 (inode->i_sb->s_blocksize_bits - 9);
1469 if (!udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime))
1470 inode->i_atime = sbi->s_record_time;
1472 if (!udf_disk_stamp_to_time(&inode->i_mtime,
1473 efe->modificationTime))
1474 inode->i_mtime = sbi->s_record_time;
1476 if (!udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime))
1477 iinfo->i_crtime = sbi->s_record_time;
1479 if (!udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime))
1480 inode->i_ctime = sbi->s_record_time;
1482 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1483 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1484 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1485 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1487 inode->i_generation = iinfo->i_unique;
1490 * Sanity check length of allocation descriptors and extended attrs to
1491 * avoid integer overflows
1493 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1495 /* Now do exact checks */
1496 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1498 /* Sanity checks for files in ICB so that we don't get confused later */
1499 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1501 * For file in ICB data is stored in allocation descriptor
1502 * so sizes should match
1504 if (iinfo->i_lenAlloc != inode->i_size)
1506 /* File in ICB has to fit in there... */
1507 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1511 switch (fe->icbTag.fileType) {
1512 case ICBTAG_FILE_TYPE_DIRECTORY:
1513 inode->i_op = &udf_dir_inode_operations;
1514 inode->i_fop = &udf_dir_operations;
1515 inode->i_mode |= S_IFDIR;
1518 case ICBTAG_FILE_TYPE_REALTIME:
1519 case ICBTAG_FILE_TYPE_REGULAR:
1520 case ICBTAG_FILE_TYPE_UNDEF:
1521 case ICBTAG_FILE_TYPE_VAT20:
1522 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1523 inode->i_data.a_ops = &udf_adinicb_aops;
1525 inode->i_data.a_ops = &udf_aops;
1526 inode->i_op = &udf_file_inode_operations;
1527 inode->i_fop = &udf_file_operations;
1528 inode->i_mode |= S_IFREG;
1530 case ICBTAG_FILE_TYPE_BLOCK:
1531 inode->i_mode |= S_IFBLK;
1533 case ICBTAG_FILE_TYPE_CHAR:
1534 inode->i_mode |= S_IFCHR;
1536 case ICBTAG_FILE_TYPE_FIFO:
1537 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1539 case ICBTAG_FILE_TYPE_SOCKET:
1540 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1542 case ICBTAG_FILE_TYPE_SYMLINK:
1543 inode->i_data.a_ops = &udf_symlink_aops;
1544 inode->i_op = &udf_symlink_inode_operations;
1545 inode->i_mode = S_IFLNK | S_IRWXUGO;
1547 case ICBTAG_FILE_TYPE_MAIN:
1548 udf_debug("METADATA FILE-----\n");
1550 case ICBTAG_FILE_TYPE_MIRROR:
1551 udf_debug("METADATA MIRROR FILE-----\n");
1553 case ICBTAG_FILE_TYPE_BITMAP:
1554 udf_debug("METADATA BITMAP FILE-----\n");
1557 udf_err(inode->i_sb, "(ino %ld) failed unknown file type=%d\n",
1558 inode->i_ino, fe->icbTag.fileType);
1561 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1562 struct deviceSpec *dsea =
1563 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1565 init_special_inode(inode, inode->i_mode,
1566 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1567 le32_to_cpu(dsea->minorDeviceIdent)));
1568 /* Developer ID ??? */
1578 static int udf_alloc_i_data(struct inode *inode, size_t size)
1580 struct udf_inode_info *iinfo = UDF_I(inode);
1581 iinfo->i_ext.i_data = kmalloc(size, GFP_KERNEL);
1583 if (!iinfo->i_ext.i_data) {
1584 udf_err(inode->i_sb, "(ino %ld) no free memory\n",
1592 static umode_t udf_convert_permissions(struct fileEntry *fe)
1595 uint32_t permissions;
1598 permissions = le32_to_cpu(fe->permissions);
1599 flags = le16_to_cpu(fe->icbTag.flags);
1601 mode = ((permissions) & S_IRWXO) |
1602 ((permissions >> 2) & S_IRWXG) |
1603 ((permissions >> 4) & S_IRWXU) |
1604 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1605 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1606 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1611 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1613 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1616 static int udf_sync_inode(struct inode *inode)
1618 return udf_update_inode(inode, 1);
1621 static int udf_update_inode(struct inode *inode, int do_sync)
1623 struct buffer_head *bh = NULL;
1624 struct fileEntry *fe;
1625 struct extendedFileEntry *efe;
1626 uint64_t lb_recorded;
1631 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1632 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1633 struct udf_inode_info *iinfo = UDF_I(inode);
1635 bh = udf_tgetblk(inode->i_sb,
1636 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1638 udf_debug("getblk failure\n");
1643 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1644 fe = (struct fileEntry *)bh->b_data;
1645 efe = (struct extendedFileEntry *)bh->b_data;
1648 struct unallocSpaceEntry *use =
1649 (struct unallocSpaceEntry *)bh->b_data;
1651 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1652 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1653 iinfo->i_ext.i_data, inode->i_sb->s_blocksize -
1654 sizeof(struct unallocSpaceEntry));
1655 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1656 use->descTag.tagLocation =
1657 cpu_to_le32(iinfo->i_location.logicalBlockNum);
1658 crclen = sizeof(struct unallocSpaceEntry) +
1659 iinfo->i_lenAlloc - sizeof(struct tag);
1660 use->descTag.descCRCLength = cpu_to_le16(crclen);
1661 use->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)use +
1664 use->descTag.tagChecksum = udf_tag_checksum(&use->descTag);
1669 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1670 fe->uid = cpu_to_le32(-1);
1672 fe->uid = cpu_to_le32(i_uid_read(inode));
1674 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1675 fe->gid = cpu_to_le32(-1);
1677 fe->gid = cpu_to_le32(i_gid_read(inode));
1679 udfperms = ((inode->i_mode & S_IRWXO)) |
1680 ((inode->i_mode & S_IRWXG) << 2) |
1681 ((inode->i_mode & S_IRWXU) << 4);
1683 udfperms |= (le32_to_cpu(fe->permissions) &
1684 (FE_PERM_O_DELETE | FE_PERM_O_CHATTR |
1685 FE_PERM_G_DELETE | FE_PERM_G_CHATTR |
1686 FE_PERM_U_DELETE | FE_PERM_U_CHATTR));
1687 fe->permissions = cpu_to_le32(udfperms);
1689 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1690 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1692 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1694 fe->informationLength = cpu_to_le64(inode->i_size);
1696 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1698 struct deviceSpec *dsea =
1699 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1701 dsea = (struct deviceSpec *)
1702 udf_add_extendedattr(inode,
1703 sizeof(struct deviceSpec) +
1704 sizeof(struct regid), 12, 0x3);
1705 dsea->attrType = cpu_to_le32(12);
1706 dsea->attrSubtype = 1;
1707 dsea->attrLength = cpu_to_le32(
1708 sizeof(struct deviceSpec) +
1709 sizeof(struct regid));
1710 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1712 eid = (struct regid *)dsea->impUse;
1713 memset(eid, 0, sizeof(struct regid));
1714 strcpy(eid->ident, UDF_ID_DEVELOPER);
1715 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1716 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1717 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1718 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1721 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1722 lb_recorded = 0; /* No extents => no blocks! */
1725 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1726 (blocksize_bits - 9);
1728 if (iinfo->i_efe == 0) {
1729 memcpy(bh->b_data + sizeof(struct fileEntry),
1730 iinfo->i_ext.i_data,
1731 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1732 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1734 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1735 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1736 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1737 memset(&(fe->impIdent), 0, sizeof(struct regid));
1738 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1739 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1740 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1741 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1742 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1743 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1744 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1745 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1746 crclen = sizeof(struct fileEntry);
1748 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1749 iinfo->i_ext.i_data,
1750 inode->i_sb->s_blocksize -
1751 sizeof(struct extendedFileEntry));
1752 efe->objectSize = cpu_to_le64(inode->i_size);
1753 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1755 if (iinfo->i_crtime.tv_sec > inode->i_atime.tv_sec ||
1756 (iinfo->i_crtime.tv_sec == inode->i_atime.tv_sec &&
1757 iinfo->i_crtime.tv_nsec > inode->i_atime.tv_nsec))
1758 iinfo->i_crtime = inode->i_atime;
1760 if (iinfo->i_crtime.tv_sec > inode->i_mtime.tv_sec ||
1761 (iinfo->i_crtime.tv_sec == inode->i_mtime.tv_sec &&
1762 iinfo->i_crtime.tv_nsec > inode->i_mtime.tv_nsec))
1763 iinfo->i_crtime = inode->i_mtime;
1765 if (iinfo->i_crtime.tv_sec > inode->i_ctime.tv_sec ||
1766 (iinfo->i_crtime.tv_sec == inode->i_ctime.tv_sec &&
1767 iinfo->i_crtime.tv_nsec > inode->i_ctime.tv_nsec))
1768 iinfo->i_crtime = inode->i_ctime;
1770 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1771 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1772 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1773 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1775 memset(&(efe->impIdent), 0, sizeof(struct regid));
1776 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1777 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1778 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1779 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1780 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1781 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1782 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1783 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1784 crclen = sizeof(struct extendedFileEntry);
1786 if (iinfo->i_strat4096) {
1787 fe->icbTag.strategyType = cpu_to_le16(4096);
1788 fe->icbTag.strategyParameter = cpu_to_le16(1);
1789 fe->icbTag.numEntries = cpu_to_le16(2);
1791 fe->icbTag.strategyType = cpu_to_le16(4);
1792 fe->icbTag.numEntries = cpu_to_le16(1);
1795 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);
1833 set_buffer_uptodate(bh);
1836 /* write the data blocks */
1837 mark_buffer_dirty(bh);
1839 sync_dirty_buffer(bh);
1840 if (buffer_write_io_error(bh)) {
1841 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1851 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1854 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1855 struct inode *inode = iget_locked(sb, block);
1859 return ERR_PTR(-ENOMEM);
1861 if (!(inode->i_state & I_NEW))
1864 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1865 err = udf_read_inode(inode, hidden_inode);
1868 return ERR_PTR(err);
1870 unlock_new_inode(inode);
1875 int udf_add_aext(struct inode *inode, struct extent_position *epos,
1876 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1879 struct short_ad *sad = NULL;
1880 struct long_ad *lad = NULL;
1881 struct allocExtDesc *aed;
1883 struct udf_inode_info *iinfo = UDF_I(inode);
1886 ptr = iinfo->i_ext.i_data + epos->offset -
1887 udf_file_entry_alloc_offset(inode) +
1890 ptr = epos->bh->b_data + epos->offset;
1892 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1893 adsize = sizeof(struct short_ad);
1894 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1895 adsize = sizeof(struct long_ad);
1899 if (epos->offset + (2 * adsize) > inode->i_sb->s_blocksize) {
1900 unsigned char *sptr, *dptr;
1901 struct buffer_head *nbh;
1903 struct kernel_lb_addr obloc = epos->block;
1905 epos->block.logicalBlockNum = udf_new_block(inode->i_sb, NULL,
1906 obloc.partitionReferenceNum,
1907 obloc.logicalBlockNum, &err);
1908 if (!epos->block.logicalBlockNum)
1910 nbh = udf_tgetblk(inode->i_sb, udf_get_lb_pblock(inode->i_sb,
1916 memset(nbh->b_data, 0x00, inode->i_sb->s_blocksize);
1917 set_buffer_uptodate(nbh);
1919 mark_buffer_dirty_inode(nbh, inode);
1921 aed = (struct allocExtDesc *)(nbh->b_data);
1922 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT))
1923 aed->previousAllocExtLocation =
1924 cpu_to_le32(obloc.logicalBlockNum);
1925 if (epos->offset + adsize > inode->i_sb->s_blocksize) {
1926 loffset = epos->offset;
1927 aed->lengthAllocDescs = cpu_to_le32(adsize);
1928 sptr = ptr - adsize;
1929 dptr = nbh->b_data + sizeof(struct allocExtDesc);
1930 memcpy(dptr, sptr, adsize);
1931 epos->offset = sizeof(struct allocExtDesc) + adsize;
1933 loffset = epos->offset + adsize;
1934 aed->lengthAllocDescs = cpu_to_le32(0);
1936 epos->offset = sizeof(struct allocExtDesc);
1939 aed = (struct allocExtDesc *)epos->bh->b_data;
1940 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1942 iinfo->i_lenAlloc += adsize;
1943 mark_inode_dirty(inode);
1946 if (UDF_SB(inode->i_sb)->s_udfrev >= 0x0200)
1947 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 3, 1,
1948 epos->block.logicalBlockNum, sizeof(struct tag));
1950 udf_new_tag(nbh->b_data, TAG_IDENT_AED, 2, 1,
1951 epos->block.logicalBlockNum, sizeof(struct tag));
1952 switch (iinfo->i_alloc_type) {
1953 case ICBTAG_FLAG_AD_SHORT:
1954 sad = (struct short_ad *)sptr;
1955 sad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1956 inode->i_sb->s_blocksize);
1958 cpu_to_le32(epos->block.logicalBlockNum);
1960 case ICBTAG_FLAG_AD_LONG:
1961 lad = (struct long_ad *)sptr;
1962 lad->extLength = cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS |
1963 inode->i_sb->s_blocksize);
1964 lad->extLocation = cpu_to_lelb(epos->block);
1965 memset(lad->impUse, 0x00, sizeof(lad->impUse));
1969 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1970 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1971 udf_update_tag(epos->bh->b_data, loffset);
1973 udf_update_tag(epos->bh->b_data,
1974 sizeof(struct allocExtDesc));
1975 mark_buffer_dirty_inode(epos->bh, inode);
1978 mark_inode_dirty(inode);
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);
2004 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2005 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2009 struct short_ad *sad;
2010 struct long_ad *lad;
2011 struct udf_inode_info *iinfo = UDF_I(inode);
2014 ptr = iinfo->i_ext.i_data + epos->offset -
2015 udf_file_entry_alloc_offset(inode) +
2018 ptr = epos->bh->b_data + epos->offset;
2020 switch (iinfo->i_alloc_type) {
2021 case ICBTAG_FLAG_AD_SHORT:
2022 sad = (struct short_ad *)ptr;
2023 sad->extLength = cpu_to_le32(elen);
2024 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2025 adsize = sizeof(struct short_ad);
2027 case ICBTAG_FLAG_AD_LONG:
2028 lad = (struct long_ad *)ptr;
2029 lad->extLength = cpu_to_le32(elen);
2030 lad->extLocation = cpu_to_lelb(*eloc);
2031 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2032 adsize = sizeof(struct long_ad);
2039 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2040 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2041 struct allocExtDesc *aed =
2042 (struct allocExtDesc *)epos->bh->b_data;
2043 udf_update_tag(epos->bh->b_data,
2044 le32_to_cpu(aed->lengthAllocDescs) +
2045 sizeof(struct allocExtDesc));
2047 mark_buffer_dirty_inode(epos->bh, inode);
2049 mark_inode_dirty(inode);
2053 epos->offset += adsize;
2056 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2057 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2061 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2062 (EXT_NEXT_EXTENT_ALLOCDECS >> 30)) {
2064 epos->block = *eloc;
2065 epos->offset = sizeof(struct allocExtDesc);
2067 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2068 epos->bh = udf_tread(inode->i_sb, block);
2070 udf_debug("reading block %d failed!\n", block);
2078 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2079 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2084 struct short_ad *sad;
2085 struct long_ad *lad;
2086 struct udf_inode_info *iinfo = UDF_I(inode);
2090 epos->offset = udf_file_entry_alloc_offset(inode);
2091 ptr = iinfo->i_ext.i_data + epos->offset -
2092 udf_file_entry_alloc_offset(inode) +
2094 alen = udf_file_entry_alloc_offset(inode) +
2098 epos->offset = sizeof(struct allocExtDesc);
2099 ptr = epos->bh->b_data + epos->offset;
2100 alen = sizeof(struct allocExtDesc) +
2101 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2105 switch (iinfo->i_alloc_type) {
2106 case ICBTAG_FLAG_AD_SHORT:
2107 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2110 etype = le32_to_cpu(sad->extLength) >> 30;
2111 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2112 eloc->partitionReferenceNum =
2113 iinfo->i_location.partitionReferenceNum;
2114 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2116 case ICBTAG_FLAG_AD_LONG:
2117 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2120 etype = le32_to_cpu(lad->extLength) >> 30;
2121 *eloc = lelb_to_cpu(lad->extLocation);
2122 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2125 udf_debug("alloc_type = %d unsupported\n", iinfo->i_alloc_type);
2132 static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2133 struct kernel_lb_addr neloc, uint32_t nelen)
2135 struct kernel_lb_addr oeloc;
2142 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2143 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2145 nelen = (etype << 30) | oelen;
2147 udf_add_aext(inode, &epos, &neloc, nelen, 1);
2150 return (nelen >> 30);
2153 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos,
2154 struct kernel_lb_addr eloc, uint32_t elen)
2156 struct extent_position oepos;
2159 struct allocExtDesc *aed;
2160 struct udf_inode_info *iinfo;
2167 iinfo = UDF_I(inode);
2168 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2169 adsize = sizeof(struct short_ad);
2170 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2171 adsize = sizeof(struct long_ad);
2176 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2179 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2180 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2181 if (oepos.bh != epos.bh) {
2182 oepos.block = epos.block;
2186 oepos.offset = epos.offset - adsize;
2189 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2192 if (epos.bh != oepos.bh) {
2193 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2194 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2195 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2197 iinfo->i_lenAlloc -= (adsize * 2);
2198 mark_inode_dirty(inode);
2200 aed = (struct allocExtDesc *)oepos.bh->b_data;
2201 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2202 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2203 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2204 udf_update_tag(oepos.bh->b_data,
2205 oepos.offset - (2 * adsize));
2207 udf_update_tag(oepos.bh->b_data,
2208 sizeof(struct allocExtDesc));
2209 mark_buffer_dirty_inode(oepos.bh, inode);
2212 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2214 iinfo->i_lenAlloc -= adsize;
2215 mark_inode_dirty(inode);
2217 aed = (struct allocExtDesc *)oepos.bh->b_data;
2218 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2219 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2220 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2221 udf_update_tag(oepos.bh->b_data,
2222 epos.offset - adsize);
2224 udf_update_tag(oepos.bh->b_data,
2225 sizeof(struct allocExtDesc));
2226 mark_buffer_dirty_inode(oepos.bh, inode);
2233 return (elen >> 30);
2236 int8_t inode_bmap(struct inode *inode, sector_t block,
2237 struct extent_position *pos, struct kernel_lb_addr *eloc,
2238 uint32_t *elen, sector_t *offset)
2240 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2241 loff_t lbcount = 0, bcount =
2242 (loff_t) block << blocksize_bits;
2244 struct udf_inode_info *iinfo;
2246 iinfo = UDF_I(inode);
2247 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2249 pos->block = iinfo->i_location;
2254 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2256 *offset = (bcount - lbcount) >> blocksize_bits;
2257 iinfo->i_lenExtents = lbcount;
2261 } while (lbcount <= bcount);
2262 /* update extent cache */
2263 udf_update_extent_cache(inode, lbcount - *elen, pos, 1);
2264 *offset = (bcount + *elen - lbcount) >> blocksize_bits;
2269 long udf_block_map(struct inode *inode, sector_t block)
2271 struct kernel_lb_addr eloc;
2274 struct extent_position epos = {};
2277 down_read(&UDF_I(inode)->i_data_sem);
2279 if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
2280 (EXT_RECORDED_ALLOCATED >> 30))
2281 ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
2285 up_read(&UDF_I(inode)->i_data_sem);
2288 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
2289 return udf_fixed_to_variable(ret);