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 #define FE_MAPPED_PERMS (FE_PERM_U_READ | FE_PERM_U_WRITE | FE_PERM_U_EXEC | \
49 FE_PERM_G_READ | FE_PERM_G_WRITE | FE_PERM_G_EXEC | \
50 FE_PERM_O_READ | FE_PERM_O_WRITE | FE_PERM_O_EXEC)
52 #define FE_DELETE_PERMS (FE_PERM_U_DELETE | FE_PERM_G_DELETE | \
57 static umode_t udf_convert_permissions(struct fileEntry *);
58 static int udf_update_inode(struct inode *, int);
59 static int udf_sync_inode(struct inode *inode);
60 static int udf_alloc_i_data(struct inode *inode, size_t size);
61 static int inode_getblk(struct inode *inode, struct udf_map_rq *map);
62 static int udf_insert_aext(struct inode *, struct extent_position,
63 struct kernel_lb_addr, uint32_t);
64 static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
65 struct kernel_long_ad *, int *);
66 static void udf_prealloc_extents(struct inode *, int, int,
67 struct kernel_long_ad *, int *);
68 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
69 static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
70 int, struct extent_position *);
71 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
73 static void __udf_clear_extent_cache(struct inode *inode)
75 struct udf_inode_info *iinfo = UDF_I(inode);
77 if (iinfo->cached_extent.lstart != -1) {
78 brelse(iinfo->cached_extent.epos.bh);
79 iinfo->cached_extent.lstart = -1;
83 /* Invalidate extent cache */
84 static void udf_clear_extent_cache(struct inode *inode)
86 struct udf_inode_info *iinfo = UDF_I(inode);
88 spin_lock(&iinfo->i_extent_cache_lock);
89 __udf_clear_extent_cache(inode);
90 spin_unlock(&iinfo->i_extent_cache_lock);
93 /* Return contents of extent cache */
94 static int udf_read_extent_cache(struct inode *inode, loff_t bcount,
95 loff_t *lbcount, struct extent_position *pos)
97 struct udf_inode_info *iinfo = UDF_I(inode);
100 spin_lock(&iinfo->i_extent_cache_lock);
101 if ((iinfo->cached_extent.lstart <= bcount) &&
102 (iinfo->cached_extent.lstart != -1)) {
104 *lbcount = iinfo->cached_extent.lstart;
105 memcpy(pos, &iinfo->cached_extent.epos,
106 sizeof(struct extent_position));
111 spin_unlock(&iinfo->i_extent_cache_lock);
115 /* Add extent to extent cache */
116 static void udf_update_extent_cache(struct inode *inode, loff_t estart,
117 struct extent_position *pos)
119 struct udf_inode_info *iinfo = UDF_I(inode);
121 spin_lock(&iinfo->i_extent_cache_lock);
122 /* Invalidate previously cached extent */
123 __udf_clear_extent_cache(inode);
126 memcpy(&iinfo->cached_extent.epos, pos, sizeof(*pos));
127 iinfo->cached_extent.lstart = estart;
128 switch (iinfo->i_alloc_type) {
129 case ICBTAG_FLAG_AD_SHORT:
130 iinfo->cached_extent.epos.offset -= sizeof(struct short_ad);
132 case ICBTAG_FLAG_AD_LONG:
133 iinfo->cached_extent.epos.offset -= sizeof(struct long_ad);
136 spin_unlock(&iinfo->i_extent_cache_lock);
139 void udf_evict_inode(struct inode *inode)
141 struct udf_inode_info *iinfo = UDF_I(inode);
144 if (!is_bad_inode(inode)) {
145 if (!inode->i_nlink) {
147 udf_setsize(inode, 0);
148 udf_update_inode(inode, IS_SYNC(inode));
150 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB &&
151 inode->i_size != iinfo->i_lenExtents) {
152 udf_warn(inode->i_sb,
153 "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);
159 truncate_inode_pages_final(&inode->i_data);
160 invalidate_inode_buffers(inode);
162 kfree(iinfo->i_data);
163 iinfo->i_data = NULL;
164 udf_clear_extent_cache(inode);
166 udf_free_inode(inode);
170 static void udf_write_failed(struct address_space *mapping, loff_t to)
172 struct inode *inode = mapping->host;
173 struct udf_inode_info *iinfo = UDF_I(inode);
174 loff_t isize = inode->i_size;
177 truncate_pagecache(inode, isize);
178 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
179 down_write(&iinfo->i_data_sem);
180 udf_clear_extent_cache(inode);
181 udf_truncate_extents(inode);
182 up_write(&iinfo->i_data_sem);
187 static int udf_writepages(struct address_space *mapping,
188 struct writeback_control *wbc)
190 return mpage_writepages(mapping, wbc, udf_get_block);
193 static int udf_read_folio(struct file *file, struct folio *folio)
195 return mpage_read_folio(folio, udf_get_block);
198 static void udf_readahead(struct readahead_control *rac)
200 mpage_readahead(rac, udf_get_block);
203 static int udf_write_begin(struct file *file, struct address_space *mapping,
204 loff_t pos, unsigned len,
205 struct page **pagep, void **fsdata)
209 ret = block_write_begin(mapping, pos, len, pagep, udf_get_block);
211 udf_write_failed(mapping, pos + len);
215 static ssize_t udf_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
217 struct file *file = iocb->ki_filp;
218 struct address_space *mapping = file->f_mapping;
219 struct inode *inode = mapping->host;
220 size_t count = iov_iter_count(iter);
223 ret = blockdev_direct_IO(iocb, inode, iter, udf_get_block);
224 if (unlikely(ret < 0 && iov_iter_rw(iter) == WRITE))
225 udf_write_failed(mapping, iocb->ki_pos + count);
229 static sector_t udf_bmap(struct address_space *mapping, sector_t block)
231 return generic_block_bmap(mapping, block, udf_get_block);
234 const struct address_space_operations udf_aops = {
235 .dirty_folio = block_dirty_folio,
236 .invalidate_folio = block_invalidate_folio,
237 .read_folio = udf_read_folio,
238 .readahead = udf_readahead,
239 .writepages = udf_writepages,
240 .write_begin = udf_write_begin,
241 .write_end = generic_write_end,
242 .direct_IO = udf_direct_IO,
244 .migrate_folio = buffer_migrate_folio,
248 * Expand file stored in ICB to a normal one-block-file
250 * This function requires i_mutex held
252 int udf_expand_file_adinicb(struct inode *inode)
256 struct udf_inode_info *iinfo = UDF_I(inode);
259 WARN_ON_ONCE(!inode_is_locked(inode));
260 if (!iinfo->i_lenAlloc) {
261 down_write(&iinfo->i_data_sem);
262 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
263 iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
265 iinfo->i_alloc_type = ICBTAG_FLAG_AD_LONG;
266 /* from now on we have normal address_space methods */
267 inode->i_data.a_ops = &udf_aops;
268 up_write(&iinfo->i_data_sem);
269 mark_inode_dirty(inode);
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_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_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_data + iinfo->i_lenEAttr, kaddr, inode->i_size);
307 kunmap_atomic(kaddr);
309 iinfo->i_alloc_type = ICBTAG_FLAG_AD_IN_ICB;
310 inode->i_data.a_ops = &udf_adinicb_aops;
311 iinfo->i_lenAlloc = inode->i_size;
312 up_write(&iinfo->i_data_sem);
315 mark_inode_dirty(inode);
320 #define UDF_MAP_CREATE 0x01 /* Mapping can allocate new blocks */
321 #define UDF_MAP_NOPREALLOC 0x02 /* Do not preallocate blocks */
323 #define UDF_BLK_MAPPED 0x01 /* Block was successfully mapped */
324 #define UDF_BLK_NEW 0x02 /* Block was freshly allocated */
329 int iflags; /* UDF_MAP_ flags determining behavior */
330 int oflags; /* UDF_BLK_ flags reporting results */
333 static int udf_map_block(struct inode *inode, struct udf_map_rq *map)
336 struct udf_inode_info *iinfo = UDF_I(inode);
339 if (!(map->iflags & UDF_MAP_CREATE)) {
340 struct kernel_lb_addr eloc;
343 struct extent_position epos = {};
345 down_read(&iinfo->i_data_sem);
346 if (inode_bmap(inode, map->lblk, &epos, &eloc, &elen, &offset)
347 == (EXT_RECORDED_ALLOCATED >> 30)) {
348 map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc,
350 map->oflags |= UDF_BLK_MAPPED;
352 up_read(&iinfo->i_data_sem);
358 down_write(&iinfo->i_data_sem);
360 * Block beyond EOF and prealloc extents? Just discard preallocation
361 * as it is not useful and complicates things.
363 if (((loff_t)map->lblk) << inode->i_blkbits > iinfo->i_lenExtents)
364 udf_discard_prealloc(inode);
365 udf_clear_extent_cache(inode);
366 err = inode_getblk(inode, map);
367 up_write(&iinfo->i_data_sem);
371 static int udf_get_block(struct inode *inode, sector_t block,
372 struct buffer_head *bh_result, int create)
375 struct udf_map_rq map = {
377 .iflags = create ? UDF_MAP_CREATE : 0,
381 * We preallocate blocks only for regular files. It also makes sense
382 * for directories but there's a problem when to drop the
383 * preallocation. We might use some delayed work for that but I feel
384 * it's overengineering for a filesystem like UDF.
386 if (!S_ISREG(inode->i_mode))
387 map.iflags |= UDF_MAP_NOPREALLOC;
388 err = udf_map_block(inode, &map);
391 if (map.oflags & UDF_BLK_MAPPED) {
392 map_bh(bh_result, inode->i_sb, map.pblk);
393 if (map.oflags & UDF_BLK_NEW)
394 set_buffer_new(bh_result);
399 /* Extend the file with new blocks totaling 'new_block_bytes',
400 * return the number of extents added
402 static int udf_do_extend_file(struct inode *inode,
403 struct extent_position *last_pos,
404 struct kernel_long_ad *last_ext,
405 loff_t new_block_bytes)
408 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
409 struct super_block *sb = inode->i_sb;
410 struct udf_inode_info *iinfo;
413 /* The previous extent is fake and we should not extend by anything
414 * - there's nothing to do... */
415 if (!new_block_bytes && fake)
418 iinfo = UDF_I(inode);
419 /* Round the last extent up to a multiple of block size */
420 if (last_ext->extLength & (sb->s_blocksize - 1)) {
421 last_ext->extLength =
422 (last_ext->extLength & UDF_EXTENT_FLAG_MASK) |
423 (((last_ext->extLength & UDF_EXTENT_LENGTH_MASK) +
424 sb->s_blocksize - 1) & ~(sb->s_blocksize - 1));
425 iinfo->i_lenExtents =
426 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
427 ~(sb->s_blocksize - 1);
431 /* Can we merge with the previous extent? */
432 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
433 EXT_NOT_RECORDED_NOT_ALLOCATED) {
434 add = (1 << 30) - sb->s_blocksize -
435 (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
436 if (add > new_block_bytes)
437 add = new_block_bytes;
438 new_block_bytes -= add;
439 last_ext->extLength += add;
443 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
444 last_ext->extLength, 1);
449 struct kernel_lb_addr tmploc;
452 udf_write_aext(inode, last_pos, &last_ext->extLocation,
453 last_ext->extLength, 1);
456 * We've rewritten the last extent. If we are going to add
457 * more extents, we may need to enter possible following
458 * empty indirect extent.
461 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
463 iinfo->i_lenExtents += add;
465 /* Managed to do everything necessary? */
466 if (!new_block_bytes)
469 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
470 last_ext->extLocation.logicalBlockNum = 0;
471 last_ext->extLocation.partitionReferenceNum = 0;
472 add = (1 << 30) - sb->s_blocksize;
473 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED | add;
475 /* Create enough extents to cover the whole hole */
476 while (new_block_bytes > add) {
477 new_block_bytes -= add;
478 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
479 last_ext->extLength, 1);
482 iinfo->i_lenExtents += add;
485 if (new_block_bytes) {
486 last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
488 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
489 last_ext->extLength, 1);
492 iinfo->i_lenExtents += new_block_bytes;
497 /* last_pos should point to the last written extent... */
498 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
499 last_pos->offset -= sizeof(struct short_ad);
500 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
501 last_pos->offset -= sizeof(struct long_ad);
507 /* Remove extents we've created so far */
508 udf_clear_extent_cache(inode);
509 udf_truncate_extents(inode);
513 /* Extend the final block of the file to final_block_len bytes */
514 static void udf_do_extend_final_block(struct inode *inode,
515 struct extent_position *last_pos,
516 struct kernel_long_ad *last_ext,
519 uint32_t added_bytes;
522 * Extent already large enough? It may be already rounded up to block
525 if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
527 added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
528 last_ext->extLength += added_bytes;
529 UDF_I(inode)->i_lenExtents += added_bytes;
531 udf_write_aext(inode, last_pos, &last_ext->extLocation,
532 last_ext->extLength, 1);
535 static int udf_extend_file(struct inode *inode, loff_t newsize)
538 struct extent_position epos;
539 struct kernel_lb_addr eloc;
542 struct super_block *sb = inode->i_sb;
543 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
546 struct udf_inode_info *iinfo = UDF_I(inode);
547 struct kernel_long_ad extent;
549 bool within_last_ext;
551 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
552 adsize = sizeof(struct short_ad);
553 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
554 adsize = sizeof(struct long_ad);
559 * When creating hole in file, just don't bother with preserving
560 * preallocation. It likely won't be very useful anyway.
562 udf_discard_prealloc(inode);
564 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
565 within_last_ext = (etype != -1);
566 /* We don't expect extents past EOF... */
567 WARN_ON_ONCE(within_last_ext &&
568 elen > ((loff_t)offset + 1) << inode->i_blkbits);
570 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
571 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
572 /* File has no extents at all or has empty last
573 * indirect extent! Create a fake extent... */
574 extent.extLocation.logicalBlockNum = 0;
575 extent.extLocation.partitionReferenceNum = 0;
576 extent.extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
578 epos.offset -= adsize;
579 etype = udf_next_aext(inode, &epos, &extent.extLocation,
580 &extent.extLength, 0);
581 extent.extLength |= etype << 30;
584 new_elen = ((loff_t)offset << inode->i_blkbits) |
585 (newsize & (sb->s_blocksize - 1));
587 /* File has extent covering the new size (could happen when extending
590 if (within_last_ext) {
591 /* Extending file within the last file block */
592 udf_do_extend_final_block(inode, &epos, &extent, new_elen);
594 err = udf_do_extend_file(inode, &epos, &extent, new_elen);
605 static int inode_getblk(struct inode *inode, struct udf_map_rq *map)
607 struct kernel_long_ad laarr[EXTENT_MERGE_SIZE];
608 struct extent_position prev_epos, cur_epos, next_epos;
609 int count = 0, startnum = 0, endnum = 0;
610 uint32_t elen = 0, tmpelen;
611 struct kernel_lb_addr eloc, tmpeloc;
613 loff_t lbcount = 0, b_off = 0;
614 udf_pblk_t newblocknum;
617 struct udf_inode_info *iinfo = UDF_I(inode);
618 udf_pblk_t goal = 0, pgoal = iinfo->i_location.logicalBlockNum;
623 prev_epos.offset = udf_file_entry_alloc_offset(inode);
624 prev_epos.block = iinfo->i_location;
626 cur_epos = next_epos = prev_epos;
627 b_off = (loff_t)map->lblk << inode->i_sb->s_blocksize_bits;
629 /* find the extent which contains the block we are looking for.
630 alternate between laarr[0] and laarr[1] for locations of the
631 current extent, and the previous extent */
633 if (prev_epos.bh != cur_epos.bh) {
634 brelse(prev_epos.bh);
636 prev_epos.bh = cur_epos.bh;
638 if (cur_epos.bh != next_epos.bh) {
640 get_bh(next_epos.bh);
641 cur_epos.bh = next_epos.bh;
646 prev_epos.block = cur_epos.block;
647 cur_epos.block = next_epos.block;
649 prev_epos.offset = cur_epos.offset;
650 cur_epos.offset = next_epos.offset;
652 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 1);
658 laarr[c].extLength = (etype << 30) | elen;
659 laarr[c].extLocation = eloc;
661 if (etype != (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
662 pgoal = eloc.logicalBlockNum +
663 ((elen + inode->i_sb->s_blocksize - 1) >>
664 inode->i_sb->s_blocksize_bits);
667 } while (lbcount + elen <= b_off);
670 offset = b_off >> inode->i_sb->s_blocksize_bits;
672 * Move prev_epos and cur_epos into indirect extent if we are at
675 udf_next_aext(inode, &prev_epos, &tmpeloc, &tmpelen, 0);
676 udf_next_aext(inode, &cur_epos, &tmpeloc, &tmpelen, 0);
678 /* if the extent is allocated and recorded, return the block
679 if the extent is not a multiple of the blocksize, round up */
681 if (etype == (EXT_RECORDED_ALLOCATED >> 30)) {
682 if (elen & (inode->i_sb->s_blocksize - 1)) {
683 elen = EXT_RECORDED_ALLOCATED |
684 ((elen + inode->i_sb->s_blocksize - 1) &
685 ~(inode->i_sb->s_blocksize - 1));
686 iinfo->i_lenExtents =
687 ALIGN(iinfo->i_lenExtents,
688 inode->i_sb->s_blocksize);
689 udf_write_aext(inode, &cur_epos, &eloc, elen, 1);
691 map->oflags = UDF_BLK_MAPPED;
692 map->pblk = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
696 /* Are we beyond EOF and preallocated extent? */
706 /* Create a fake extent when there's not one */
707 memset(&laarr[0].extLocation, 0x00,
708 sizeof(struct kernel_lb_addr));
709 laarr[0].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED;
710 /* Will udf_do_extend_file() create real extent from
712 startnum = (offset > 0);
714 /* Create extents for the hole between EOF and offset */
715 hole_len = (loff_t)offset << inode->i_blkbits;
716 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
722 /* We are not covered by a preallocated extent? */
723 if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
724 EXT_NOT_RECORDED_ALLOCATED) {
725 /* Is there any real extent? - otherwise we overwrite
729 laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
730 inode->i_sb->s_blocksize;
731 memset(&laarr[c].extLocation, 0x00,
732 sizeof(struct kernel_lb_addr));
739 endnum = startnum = ((count > 2) ? 2 : count);
741 /* if the current extent is in position 0,
742 swap it with the previous */
743 if (!c && count != 1) {
750 /* if the current block is located in an extent,
751 read the next extent */
752 etype = udf_next_aext(inode, &next_epos, &eloc, &elen, 0);
754 laarr[c + 1].extLength = (etype << 30) | elen;
755 laarr[c + 1].extLocation = eloc;
763 /* if the current extent is not recorded but allocated, get the
764 * block in the extent corresponding to the requested block */
765 if ((laarr[c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30))
766 newblocknum = laarr[c].extLocation.logicalBlockNum + offset;
767 else { /* otherwise, allocate a new block */
768 if (iinfo->i_next_alloc_block == map->lblk)
769 goal = iinfo->i_next_alloc_goal;
772 if (!(goal = pgoal)) /* XXX: what was intended here? */
773 goal = iinfo->i_location.logicalBlockNum + 1;
776 newblocknum = udf_new_block(inode->i_sb, inode,
777 iinfo->i_location.partitionReferenceNum,
782 iinfo->i_lenExtents += inode->i_sb->s_blocksize;
785 /* if the extent the requsted block is located in contains multiple
786 * blocks, split the extent into at most three extents. blocks prior
787 * to requested block, requested block, and blocks after requested
789 udf_split_extents(inode, &c, offset, newblocknum, laarr, &endnum);
791 if (!(map->iflags & UDF_MAP_NOPREALLOC))
792 udf_prealloc_extents(inode, c, lastblock, laarr, &endnum);
794 /* merge any continuous blocks in laarr */
795 udf_merge_extents(inode, laarr, &endnum);
797 /* write back the new extents, inserting new extents if the new number
798 * of extents is greater than the old number, and deleting extents if
799 * the new number of extents is less than the old number */
800 ret = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
804 map->pblk = udf_get_pblock(inode->i_sb, newblocknum,
805 iinfo->i_location.partitionReferenceNum, 0);
810 map->oflags = UDF_BLK_NEW | UDF_BLK_MAPPED;
811 iinfo->i_next_alloc_block = map->lblk + 1;
812 iinfo->i_next_alloc_goal = newblocknum + 1;
813 inode->i_ctime = current_time(inode);
816 udf_sync_inode(inode);
818 mark_inode_dirty(inode);
821 brelse(prev_epos.bh);
823 brelse(next_epos.bh);
827 static void udf_split_extents(struct inode *inode, int *c, int offset,
828 udf_pblk_t newblocknum,
829 struct kernel_long_ad *laarr, int *endnum)
831 unsigned long blocksize = inode->i_sb->s_blocksize;
832 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
834 if ((laarr[*c].extLength >> 30) == (EXT_NOT_RECORDED_ALLOCATED >> 30) ||
835 (laarr[*c].extLength >> 30) ==
836 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
838 int blen = ((laarr[curr].extLength & UDF_EXTENT_LENGTH_MASK) +
839 blocksize - 1) >> blocksize_bits;
840 int8_t etype = (laarr[curr].extLength >> 30);
844 else if (!offset || blen == offset + 1) {
845 laarr[curr + 2] = laarr[curr + 1];
846 laarr[curr + 1] = laarr[curr];
848 laarr[curr + 3] = laarr[curr + 1];
849 laarr[curr + 2] = laarr[curr + 1] = laarr[curr];
853 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
854 udf_free_blocks(inode->i_sb, inode,
855 &laarr[curr].extLocation,
857 laarr[curr].extLength =
858 EXT_NOT_RECORDED_NOT_ALLOCATED |
859 (offset << blocksize_bits);
860 laarr[curr].extLocation.logicalBlockNum = 0;
861 laarr[curr].extLocation.
862 partitionReferenceNum = 0;
864 laarr[curr].extLength = (etype << 30) |
865 (offset << blocksize_bits);
871 laarr[curr].extLocation.logicalBlockNum = newblocknum;
872 if (etype == (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))
873 laarr[curr].extLocation.partitionReferenceNum =
874 UDF_I(inode)->i_location.partitionReferenceNum;
875 laarr[curr].extLength = EXT_RECORDED_ALLOCATED |
879 if (blen != offset + 1) {
880 if (etype == (EXT_NOT_RECORDED_ALLOCATED >> 30))
881 laarr[curr].extLocation.logicalBlockNum +=
883 laarr[curr].extLength = (etype << 30) |
884 ((blen - (offset + 1)) << blocksize_bits);
891 static void udf_prealloc_extents(struct inode *inode, int c, int lastblock,
892 struct kernel_long_ad *laarr,
895 int start, length = 0, currlength = 0, i;
897 if (*endnum >= (c + 1)) {
903 if ((laarr[c + 1].extLength >> 30) ==
904 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
906 length = currlength =
907 (((laarr[c + 1].extLength &
908 UDF_EXTENT_LENGTH_MASK) +
909 inode->i_sb->s_blocksize - 1) >>
910 inode->i_sb->s_blocksize_bits);
915 for (i = start + 1; i <= *endnum; i++) {
918 length += UDF_DEFAULT_PREALLOC_BLOCKS;
919 } else if ((laarr[i].extLength >> 30) ==
920 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) {
921 length += (((laarr[i].extLength &
922 UDF_EXTENT_LENGTH_MASK) +
923 inode->i_sb->s_blocksize - 1) >>
924 inode->i_sb->s_blocksize_bits);
930 int next = laarr[start].extLocation.logicalBlockNum +
931 (((laarr[start].extLength & UDF_EXTENT_LENGTH_MASK) +
932 inode->i_sb->s_blocksize - 1) >>
933 inode->i_sb->s_blocksize_bits);
934 int numalloc = udf_prealloc_blocks(inode->i_sb, inode,
935 laarr[start].extLocation.partitionReferenceNum,
936 next, (UDF_DEFAULT_PREALLOC_BLOCKS > length ?
937 length : UDF_DEFAULT_PREALLOC_BLOCKS) -
940 if (start == (c + 1))
941 laarr[start].extLength +=
943 inode->i_sb->s_blocksize_bits);
945 memmove(&laarr[c + 2], &laarr[c + 1],
946 sizeof(struct long_ad) * (*endnum - (c + 1)));
948 laarr[c + 1].extLocation.logicalBlockNum = next;
949 laarr[c + 1].extLocation.partitionReferenceNum =
950 laarr[c].extLocation.
951 partitionReferenceNum;
952 laarr[c + 1].extLength =
953 EXT_NOT_RECORDED_ALLOCATED |
955 inode->i_sb->s_blocksize_bits);
959 for (i = start + 1; numalloc && i < *endnum; i++) {
960 int elen = ((laarr[i].extLength &
961 UDF_EXTENT_LENGTH_MASK) +
962 inode->i_sb->s_blocksize - 1) >>
963 inode->i_sb->s_blocksize_bits;
965 if (elen > numalloc) {
966 laarr[i].extLength -=
968 inode->i_sb->s_blocksize_bits);
972 if (*endnum > (i + 1))
975 sizeof(struct long_ad) *
976 (*endnum - (i + 1)));
981 UDF_I(inode)->i_lenExtents +=
982 numalloc << inode->i_sb->s_blocksize_bits;
987 static void udf_merge_extents(struct inode *inode, struct kernel_long_ad *laarr,
991 unsigned long blocksize = inode->i_sb->s_blocksize;
992 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
994 for (i = 0; i < (*endnum - 1); i++) {
995 struct kernel_long_ad *li /*l[i]*/ = &laarr[i];
996 struct kernel_long_ad *lip1 /*l[i plus 1]*/ = &laarr[i + 1];
998 if (((li->extLength >> 30) == (lip1->extLength >> 30)) &&
999 (((li->extLength >> 30) ==
1000 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30)) ||
1001 ((lip1->extLocation.logicalBlockNum -
1002 li->extLocation.logicalBlockNum) ==
1003 (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1004 blocksize - 1) >> blocksize_bits)))) {
1006 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1007 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1008 blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
1009 li->extLength = lip1->extLength +
1011 UDF_EXTENT_LENGTH_MASK) +
1012 blocksize - 1) & ~(blocksize - 1));
1013 if (*endnum > (i + 2))
1014 memmove(&laarr[i + 1], &laarr[i + 2],
1015 sizeof(struct long_ad) *
1016 (*endnum - (i + 2)));
1020 } else if (((li->extLength >> 30) ==
1021 (EXT_NOT_RECORDED_ALLOCATED >> 30)) &&
1022 ((lip1->extLength >> 30) ==
1023 (EXT_NOT_RECORDED_NOT_ALLOCATED >> 30))) {
1024 udf_free_blocks(inode->i_sb, inode, &li->extLocation, 0,
1026 UDF_EXTENT_LENGTH_MASK) +
1027 blocksize - 1) >> blocksize_bits);
1028 li->extLocation.logicalBlockNum = 0;
1029 li->extLocation.partitionReferenceNum = 0;
1031 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1032 (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1033 blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1034 lip1->extLength = (lip1->extLength -
1036 UDF_EXTENT_LENGTH_MASK) +
1037 UDF_EXTENT_LENGTH_MASK) &
1039 li->extLength = (li->extLength &
1040 UDF_EXTENT_FLAG_MASK) +
1041 (UDF_EXTENT_LENGTH_MASK + 1) -
1044 li->extLength = lip1->extLength +
1046 UDF_EXTENT_LENGTH_MASK) +
1047 blocksize - 1) & ~(blocksize - 1));
1048 if (*endnum > (i + 2))
1049 memmove(&laarr[i + 1], &laarr[i + 2],
1050 sizeof(struct long_ad) *
1051 (*endnum - (i + 2)));
1055 } else if ((li->extLength >> 30) ==
1056 (EXT_NOT_RECORDED_ALLOCATED >> 30)) {
1057 udf_free_blocks(inode->i_sb, inode,
1058 &li->extLocation, 0,
1060 UDF_EXTENT_LENGTH_MASK) +
1061 blocksize - 1) >> blocksize_bits);
1062 li->extLocation.logicalBlockNum = 0;
1063 li->extLocation.partitionReferenceNum = 0;
1064 li->extLength = (li->extLength &
1065 UDF_EXTENT_LENGTH_MASK) |
1066 EXT_NOT_RECORDED_NOT_ALLOCATED;
1071 static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1072 int startnum, int endnum,
1073 struct extent_position *epos)
1076 struct kernel_lb_addr tmploc;
1080 if (startnum > endnum) {
1081 for (i = 0; i < (startnum - endnum); i++)
1082 udf_delete_aext(inode, *epos);
1083 } else if (startnum < endnum) {
1084 for (i = 0; i < (endnum - startnum); i++) {
1085 err = udf_insert_aext(inode, *epos,
1086 laarr[i].extLocation,
1087 laarr[i].extLength);
1089 * If we fail here, we are likely corrupting the extent
1090 * list and leaking blocks. At least stop early to
1095 udf_next_aext(inode, epos, &laarr[i].extLocation,
1096 &laarr[i].extLength, 1);
1101 for (i = start; i < endnum; i++) {
1102 udf_next_aext(inode, epos, &tmploc, &tmplen, 0);
1103 udf_write_aext(inode, epos, &laarr[i].extLocation,
1104 laarr[i].extLength, 1);
1109 struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
1110 int create, int *err)
1112 struct buffer_head *bh = NULL;
1113 struct udf_map_rq map = {
1115 .iflags = UDF_MAP_NOPREALLOC | (create ? UDF_MAP_CREATE : 0),
1118 *err = udf_map_block(inode, &map);
1119 if (*err || !(map.oflags & UDF_BLK_MAPPED))
1122 bh = sb_getblk(inode->i_sb, map.pblk);
1127 if (map.oflags & UDF_BLK_NEW) {
1129 memset(bh->b_data, 0x00, inode->i_sb->s_blocksize);
1130 set_buffer_uptodate(bh);
1132 mark_buffer_dirty_inode(bh, inode);
1136 if (bh_read(bh, 0) >= 0)
1144 int udf_setsize(struct inode *inode, loff_t newsize)
1147 struct udf_inode_info *iinfo;
1148 unsigned int bsize = i_blocksize(inode);
1150 if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
1151 S_ISLNK(inode->i_mode)))
1153 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
1156 iinfo = UDF_I(inode);
1157 if (newsize > inode->i_size) {
1158 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1160 (udf_file_entry_alloc_offset(inode) + newsize)) {
1161 down_write(&iinfo->i_data_sem);
1162 iinfo->i_lenAlloc = newsize;
1165 err = udf_expand_file_adinicb(inode);
1169 down_write(&iinfo->i_data_sem);
1170 err = udf_extend_file(inode, newsize);
1172 up_write(&iinfo->i_data_sem);
1176 up_write(&iinfo->i_data_sem);
1177 truncate_setsize(inode, newsize);
1179 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1180 down_write(&iinfo->i_data_sem);
1181 udf_clear_extent_cache(inode);
1182 memset(iinfo->i_data + iinfo->i_lenEAttr + newsize,
1183 0x00, bsize - newsize -
1184 udf_file_entry_alloc_offset(inode));
1185 iinfo->i_lenAlloc = newsize;
1186 truncate_setsize(inode, newsize);
1187 up_write(&iinfo->i_data_sem);
1190 err = block_truncate_page(inode->i_mapping, newsize,
1194 truncate_setsize(inode, newsize);
1195 down_write(&iinfo->i_data_sem);
1196 udf_clear_extent_cache(inode);
1197 err = udf_truncate_extents(inode);
1198 up_write(&iinfo->i_data_sem);
1203 inode->i_mtime = inode->i_ctime = current_time(inode);
1205 udf_sync_inode(inode);
1207 mark_inode_dirty(inode);
1212 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1213 * arbitrary - just that we hopefully don't limit any real use of rewritten
1214 * inode on write-once media but avoid looping for too long on corrupted media.
1216 #define UDF_MAX_ICB_NESTING 1024
1218 static int udf_read_inode(struct inode *inode, bool hidden_inode)
1220 struct buffer_head *bh = NULL;
1221 struct fileEntry *fe;
1222 struct extendedFileEntry *efe;
1224 struct udf_inode_info *iinfo = UDF_I(inode);
1225 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1226 struct kernel_lb_addr *iloc = &iinfo->i_location;
1227 unsigned int link_count;
1228 unsigned int indirections = 0;
1229 int bs = inode->i_sb->s_blocksize;
1234 if (iloc->partitionReferenceNum >= sbi->s_partitions) {
1235 udf_debug("partition reference: %u > logical volume partitions: %u\n",
1236 iloc->partitionReferenceNum, sbi->s_partitions);
1240 if (iloc->logicalBlockNum >=
1241 sbi->s_partmaps[iloc->partitionReferenceNum].s_partition_len) {
1242 udf_debug("block=%u, partition=%u out of range\n",
1243 iloc->logicalBlockNum, iloc->partitionReferenceNum);
1248 * Set defaults, but the inode is still incomplete!
1249 * Note: get_new_inode() sets the following on a new inode:
1252 * i_flags = sb->s_flags
1254 * clean_inode(): zero fills and sets
1259 bh = udf_read_ptagged(inode->i_sb, iloc, 0, &ident);
1261 udf_err(inode->i_sb, "(ino %lu) failed !bh\n", inode->i_ino);
1265 if (ident != TAG_IDENT_FE && ident != TAG_IDENT_EFE &&
1266 ident != TAG_IDENT_USE) {
1267 udf_err(inode->i_sb, "(ino %lu) failed ident=%u\n",
1268 inode->i_ino, ident);
1272 fe = (struct fileEntry *)bh->b_data;
1273 efe = (struct extendedFileEntry *)bh->b_data;
1275 if (fe->icbTag.strategyType == cpu_to_le16(4096)) {
1276 struct buffer_head *ibh;
1278 ibh = udf_read_ptagged(inode->i_sb, iloc, 1, &ident);
1279 if (ident == TAG_IDENT_IE && ibh) {
1280 struct kernel_lb_addr loc;
1281 struct indirectEntry *ie;
1283 ie = (struct indirectEntry *)ibh->b_data;
1284 loc = lelb_to_cpu(ie->indirectICB.extLocation);
1286 if (ie->indirectICB.extLength) {
1288 memcpy(&iinfo->i_location, &loc,
1289 sizeof(struct kernel_lb_addr));
1290 if (++indirections > UDF_MAX_ICB_NESTING) {
1291 udf_err(inode->i_sb,
1292 "too many ICBs in ICB hierarchy"
1293 " (max %d supported)\n",
1294 UDF_MAX_ICB_NESTING);
1302 } else if (fe->icbTag.strategyType != cpu_to_le16(4)) {
1303 udf_err(inode->i_sb, "unsupported strategy type: %u\n",
1304 le16_to_cpu(fe->icbTag.strategyType));
1307 if (fe->icbTag.strategyType == cpu_to_le16(4))
1308 iinfo->i_strat4096 = 0;
1309 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1310 iinfo->i_strat4096 = 1;
1312 iinfo->i_alloc_type = le16_to_cpu(fe->icbTag.flags) &
1313 ICBTAG_FLAG_AD_MASK;
1314 if (iinfo->i_alloc_type != ICBTAG_FLAG_AD_SHORT &&
1315 iinfo->i_alloc_type != ICBTAG_FLAG_AD_LONG &&
1316 iinfo->i_alloc_type != ICBTAG_FLAG_AD_IN_ICB) {
1320 iinfo->i_hidden = hidden_inode;
1321 iinfo->i_unique = 0;
1322 iinfo->i_lenEAttr = 0;
1323 iinfo->i_lenExtents = 0;
1324 iinfo->i_lenAlloc = 0;
1325 iinfo->i_next_alloc_block = 0;
1326 iinfo->i_next_alloc_goal = 0;
1327 if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_EFE)) {
1330 ret = udf_alloc_i_data(inode, bs -
1331 sizeof(struct extendedFileEntry));
1334 memcpy(iinfo->i_data,
1335 bh->b_data + sizeof(struct extendedFileEntry),
1336 bs - sizeof(struct extendedFileEntry));
1337 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_FE)) {
1340 ret = udf_alloc_i_data(inode, bs - sizeof(struct fileEntry));
1343 memcpy(iinfo->i_data,
1344 bh->b_data + sizeof(struct fileEntry),
1345 bs - sizeof(struct fileEntry));
1346 } else if (fe->descTag.tagIdent == cpu_to_le16(TAG_IDENT_USE)) {
1349 iinfo->i_lenAlloc = le32_to_cpu(
1350 ((struct unallocSpaceEntry *)bh->b_data)->
1352 ret = udf_alloc_i_data(inode, bs -
1353 sizeof(struct unallocSpaceEntry));
1356 memcpy(iinfo->i_data,
1357 bh->b_data + sizeof(struct unallocSpaceEntry),
1358 bs - sizeof(struct unallocSpaceEntry));
1363 read_lock(&sbi->s_cred_lock);
1364 uid = le32_to_cpu(fe->uid);
1365 if (uid == UDF_INVALID_ID ||
1366 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_SET))
1367 inode->i_uid = sbi->s_uid;
1369 i_uid_write(inode, uid);
1371 gid = le32_to_cpu(fe->gid);
1372 if (gid == UDF_INVALID_ID ||
1373 UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_SET))
1374 inode->i_gid = sbi->s_gid;
1376 i_gid_write(inode, gid);
1378 if (fe->icbTag.fileType != ICBTAG_FILE_TYPE_DIRECTORY &&
1379 sbi->s_fmode != UDF_INVALID_MODE)
1380 inode->i_mode = sbi->s_fmode;
1381 else if (fe->icbTag.fileType == ICBTAG_FILE_TYPE_DIRECTORY &&
1382 sbi->s_dmode != UDF_INVALID_MODE)
1383 inode->i_mode = sbi->s_dmode;
1385 inode->i_mode = udf_convert_permissions(fe);
1386 inode->i_mode &= ~sbi->s_umask;
1387 iinfo->i_extraPerms = le32_to_cpu(fe->permissions) & ~FE_MAPPED_PERMS;
1389 read_unlock(&sbi->s_cred_lock);
1391 link_count = le16_to_cpu(fe->fileLinkCount);
1393 if (!hidden_inode) {
1399 set_nlink(inode, link_count);
1401 inode->i_size = le64_to_cpu(fe->informationLength);
1402 iinfo->i_lenExtents = inode->i_size;
1404 if (iinfo->i_efe == 0) {
1405 inode->i_blocks = le64_to_cpu(fe->logicalBlocksRecorded) <<
1406 (inode->i_sb->s_blocksize_bits - 9);
1408 udf_disk_stamp_to_time(&inode->i_atime, fe->accessTime);
1409 udf_disk_stamp_to_time(&inode->i_mtime, fe->modificationTime);
1410 udf_disk_stamp_to_time(&inode->i_ctime, fe->attrTime);
1412 iinfo->i_unique = le64_to_cpu(fe->uniqueID);
1413 iinfo->i_lenEAttr = le32_to_cpu(fe->lengthExtendedAttr);
1414 iinfo->i_lenAlloc = le32_to_cpu(fe->lengthAllocDescs);
1415 iinfo->i_checkpoint = le32_to_cpu(fe->checkpoint);
1416 iinfo->i_streamdir = 0;
1417 iinfo->i_lenStreams = 0;
1419 inode->i_blocks = le64_to_cpu(efe->logicalBlocksRecorded) <<
1420 (inode->i_sb->s_blocksize_bits - 9);
1422 udf_disk_stamp_to_time(&inode->i_atime, efe->accessTime);
1423 udf_disk_stamp_to_time(&inode->i_mtime, efe->modificationTime);
1424 udf_disk_stamp_to_time(&iinfo->i_crtime, efe->createTime);
1425 udf_disk_stamp_to_time(&inode->i_ctime, efe->attrTime);
1427 iinfo->i_unique = le64_to_cpu(efe->uniqueID);
1428 iinfo->i_lenEAttr = le32_to_cpu(efe->lengthExtendedAttr);
1429 iinfo->i_lenAlloc = le32_to_cpu(efe->lengthAllocDescs);
1430 iinfo->i_checkpoint = le32_to_cpu(efe->checkpoint);
1433 iinfo->i_streamdir = (efe->streamDirectoryICB.extLength != 0);
1434 iinfo->i_locStreamdir =
1435 lelb_to_cpu(efe->streamDirectoryICB.extLocation);
1436 iinfo->i_lenStreams = le64_to_cpu(efe->objectSize);
1437 if (iinfo->i_lenStreams >= inode->i_size)
1438 iinfo->i_lenStreams -= inode->i_size;
1440 iinfo->i_lenStreams = 0;
1442 inode->i_generation = iinfo->i_unique;
1445 * Sanity check length of allocation descriptors and extended attrs to
1446 * avoid integer overflows
1448 if (iinfo->i_lenEAttr > bs || iinfo->i_lenAlloc > bs)
1450 /* Now do exact checks */
1451 if (udf_file_entry_alloc_offset(inode) + iinfo->i_lenAlloc > bs)
1453 /* Sanity checks for files in ICB so that we don't get confused later */
1454 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
1456 * For file in ICB data is stored in allocation descriptor
1457 * so sizes should match
1459 if (iinfo->i_lenAlloc != inode->i_size)
1461 /* File in ICB has to fit in there... */
1462 if (inode->i_size > bs - udf_file_entry_alloc_offset(inode))
1466 switch (fe->icbTag.fileType) {
1467 case ICBTAG_FILE_TYPE_DIRECTORY:
1468 inode->i_op = &udf_dir_inode_operations;
1469 inode->i_fop = &udf_dir_operations;
1470 inode->i_mode |= S_IFDIR;
1473 case ICBTAG_FILE_TYPE_REALTIME:
1474 case ICBTAG_FILE_TYPE_REGULAR:
1475 case ICBTAG_FILE_TYPE_UNDEF:
1476 case ICBTAG_FILE_TYPE_VAT20:
1477 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1478 inode->i_data.a_ops = &udf_adinicb_aops;
1480 inode->i_data.a_ops = &udf_aops;
1481 inode->i_op = &udf_file_inode_operations;
1482 inode->i_fop = &udf_file_operations;
1483 inode->i_mode |= S_IFREG;
1485 case ICBTAG_FILE_TYPE_BLOCK:
1486 inode->i_mode |= S_IFBLK;
1488 case ICBTAG_FILE_TYPE_CHAR:
1489 inode->i_mode |= S_IFCHR;
1491 case ICBTAG_FILE_TYPE_FIFO:
1492 init_special_inode(inode, inode->i_mode | S_IFIFO, 0);
1494 case ICBTAG_FILE_TYPE_SOCKET:
1495 init_special_inode(inode, inode->i_mode | S_IFSOCK, 0);
1497 case ICBTAG_FILE_TYPE_SYMLINK:
1498 inode->i_data.a_ops = &udf_symlink_aops;
1499 inode->i_op = &udf_symlink_inode_operations;
1500 inode_nohighmem(inode);
1501 inode->i_mode = S_IFLNK | 0777;
1503 case ICBTAG_FILE_TYPE_MAIN:
1504 udf_debug("METADATA FILE-----\n");
1506 case ICBTAG_FILE_TYPE_MIRROR:
1507 udf_debug("METADATA MIRROR FILE-----\n");
1509 case ICBTAG_FILE_TYPE_BITMAP:
1510 udf_debug("METADATA BITMAP FILE-----\n");
1513 udf_err(inode->i_sb, "(ino %lu) failed unknown file type=%u\n",
1514 inode->i_ino, fe->icbTag.fileType);
1517 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1518 struct deviceSpec *dsea =
1519 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1521 init_special_inode(inode, inode->i_mode,
1522 MKDEV(le32_to_cpu(dsea->majorDeviceIdent),
1523 le32_to_cpu(dsea->minorDeviceIdent)));
1524 /* Developer ID ??? */
1534 static int udf_alloc_i_data(struct inode *inode, size_t size)
1536 struct udf_inode_info *iinfo = UDF_I(inode);
1537 iinfo->i_data = kmalloc(size, GFP_KERNEL);
1543 static umode_t udf_convert_permissions(struct fileEntry *fe)
1546 uint32_t permissions;
1549 permissions = le32_to_cpu(fe->permissions);
1550 flags = le16_to_cpu(fe->icbTag.flags);
1552 mode = ((permissions) & 0007) |
1553 ((permissions >> 2) & 0070) |
1554 ((permissions >> 4) & 0700) |
1555 ((flags & ICBTAG_FLAG_SETUID) ? S_ISUID : 0) |
1556 ((flags & ICBTAG_FLAG_SETGID) ? S_ISGID : 0) |
1557 ((flags & ICBTAG_FLAG_STICKY) ? S_ISVTX : 0);
1562 void udf_update_extra_perms(struct inode *inode, umode_t mode)
1564 struct udf_inode_info *iinfo = UDF_I(inode);
1567 * UDF 2.01 sec. 3.3.3.3 Note 2:
1568 * In Unix, delete permission tracks write
1570 iinfo->i_extraPerms &= ~FE_DELETE_PERMS;
1572 iinfo->i_extraPerms |= FE_PERM_U_DELETE;
1574 iinfo->i_extraPerms |= FE_PERM_G_DELETE;
1576 iinfo->i_extraPerms |= FE_PERM_O_DELETE;
1579 int udf_write_inode(struct inode *inode, struct writeback_control *wbc)
1581 return udf_update_inode(inode, wbc->sync_mode == WB_SYNC_ALL);
1584 static int udf_sync_inode(struct inode *inode)
1586 return udf_update_inode(inode, 1);
1589 static void udf_adjust_time(struct udf_inode_info *iinfo, struct timespec64 time)
1591 if (iinfo->i_crtime.tv_sec > time.tv_sec ||
1592 (iinfo->i_crtime.tv_sec == time.tv_sec &&
1593 iinfo->i_crtime.tv_nsec > time.tv_nsec))
1594 iinfo->i_crtime = time;
1597 static int udf_update_inode(struct inode *inode, int do_sync)
1599 struct buffer_head *bh = NULL;
1600 struct fileEntry *fe;
1601 struct extendedFileEntry *efe;
1602 uint64_t lb_recorded;
1607 struct udf_sb_info *sbi = UDF_SB(inode->i_sb);
1608 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
1609 struct udf_inode_info *iinfo = UDF_I(inode);
1611 bh = sb_getblk(inode->i_sb,
1612 udf_get_lb_pblock(inode->i_sb, &iinfo->i_location, 0));
1614 udf_debug("getblk failure\n");
1619 memset(bh->b_data, 0, inode->i_sb->s_blocksize);
1620 fe = (struct fileEntry *)bh->b_data;
1621 efe = (struct extendedFileEntry *)bh->b_data;
1624 struct unallocSpaceEntry *use =
1625 (struct unallocSpaceEntry *)bh->b_data;
1627 use->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1628 memcpy(bh->b_data + sizeof(struct unallocSpaceEntry),
1629 iinfo->i_data, inode->i_sb->s_blocksize -
1630 sizeof(struct unallocSpaceEntry));
1631 use->descTag.tagIdent = cpu_to_le16(TAG_IDENT_USE);
1632 crclen = sizeof(struct unallocSpaceEntry);
1637 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_UID_FORGET))
1638 fe->uid = cpu_to_le32(UDF_INVALID_ID);
1640 fe->uid = cpu_to_le32(i_uid_read(inode));
1642 if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_GID_FORGET))
1643 fe->gid = cpu_to_le32(UDF_INVALID_ID);
1645 fe->gid = cpu_to_le32(i_gid_read(inode));
1647 udfperms = ((inode->i_mode & 0007)) |
1648 ((inode->i_mode & 0070) << 2) |
1649 ((inode->i_mode & 0700) << 4);
1651 udfperms |= iinfo->i_extraPerms;
1652 fe->permissions = cpu_to_le32(udfperms);
1654 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
1655 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1657 if (iinfo->i_hidden)
1658 fe->fileLinkCount = cpu_to_le16(0);
1660 fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1663 fe->informationLength = cpu_to_le64(inode->i_size);
1665 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
1667 struct deviceSpec *dsea =
1668 (struct deviceSpec *)udf_get_extendedattr(inode, 12, 1);
1670 dsea = (struct deviceSpec *)
1671 udf_add_extendedattr(inode,
1672 sizeof(struct deviceSpec) +
1673 sizeof(struct regid), 12, 0x3);
1674 dsea->attrType = cpu_to_le32(12);
1675 dsea->attrSubtype = 1;
1676 dsea->attrLength = cpu_to_le32(
1677 sizeof(struct deviceSpec) +
1678 sizeof(struct regid));
1679 dsea->impUseLength = cpu_to_le32(sizeof(struct regid));
1681 eid = (struct regid *)dsea->impUse;
1682 memset(eid, 0, sizeof(*eid));
1683 strcpy(eid->ident, UDF_ID_DEVELOPER);
1684 eid->identSuffix[0] = UDF_OS_CLASS_UNIX;
1685 eid->identSuffix[1] = UDF_OS_ID_LINUX;
1686 dsea->majorDeviceIdent = cpu_to_le32(imajor(inode));
1687 dsea->minorDeviceIdent = cpu_to_le32(iminor(inode));
1690 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
1691 lb_recorded = 0; /* No extents => no blocks! */
1694 (inode->i_blocks + (1 << (blocksize_bits - 9)) - 1) >>
1695 (blocksize_bits - 9);
1697 if (iinfo->i_efe == 0) {
1698 memcpy(bh->b_data + sizeof(struct fileEntry),
1700 inode->i_sb->s_blocksize - sizeof(struct fileEntry));
1701 fe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1703 udf_time_to_disk_stamp(&fe->accessTime, inode->i_atime);
1704 udf_time_to_disk_stamp(&fe->modificationTime, inode->i_mtime);
1705 udf_time_to_disk_stamp(&fe->attrTime, inode->i_ctime);
1706 memset(&(fe->impIdent), 0, sizeof(struct regid));
1707 strcpy(fe->impIdent.ident, UDF_ID_DEVELOPER);
1708 fe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1709 fe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1710 fe->uniqueID = cpu_to_le64(iinfo->i_unique);
1711 fe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1712 fe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1713 fe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1714 fe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_FE);
1715 crclen = sizeof(struct fileEntry);
1717 memcpy(bh->b_data + sizeof(struct extendedFileEntry),
1719 inode->i_sb->s_blocksize -
1720 sizeof(struct extendedFileEntry));
1722 cpu_to_le64(inode->i_size + iinfo->i_lenStreams);
1723 efe->logicalBlocksRecorded = cpu_to_le64(lb_recorded);
1725 if (iinfo->i_streamdir) {
1726 struct long_ad *icb_lad = &efe->streamDirectoryICB;
1728 icb_lad->extLocation =
1729 cpu_to_lelb(iinfo->i_locStreamdir);
1730 icb_lad->extLength =
1731 cpu_to_le32(inode->i_sb->s_blocksize);
1734 udf_adjust_time(iinfo, inode->i_atime);
1735 udf_adjust_time(iinfo, inode->i_mtime);
1736 udf_adjust_time(iinfo, inode->i_ctime);
1738 udf_time_to_disk_stamp(&efe->accessTime, inode->i_atime);
1739 udf_time_to_disk_stamp(&efe->modificationTime, inode->i_mtime);
1740 udf_time_to_disk_stamp(&efe->createTime, iinfo->i_crtime);
1741 udf_time_to_disk_stamp(&efe->attrTime, inode->i_ctime);
1743 memset(&(efe->impIdent), 0, sizeof(efe->impIdent));
1744 strcpy(efe->impIdent.ident, UDF_ID_DEVELOPER);
1745 efe->impIdent.identSuffix[0] = UDF_OS_CLASS_UNIX;
1746 efe->impIdent.identSuffix[1] = UDF_OS_ID_LINUX;
1747 efe->uniqueID = cpu_to_le64(iinfo->i_unique);
1748 efe->lengthExtendedAttr = cpu_to_le32(iinfo->i_lenEAttr);
1749 efe->lengthAllocDescs = cpu_to_le32(iinfo->i_lenAlloc);
1750 efe->checkpoint = cpu_to_le32(iinfo->i_checkpoint);
1751 efe->descTag.tagIdent = cpu_to_le16(TAG_IDENT_EFE);
1752 crclen = sizeof(struct extendedFileEntry);
1756 if (iinfo->i_strat4096) {
1757 fe->icbTag.strategyType = cpu_to_le16(4096);
1758 fe->icbTag.strategyParameter = cpu_to_le16(1);
1759 fe->icbTag.numEntries = cpu_to_le16(2);
1761 fe->icbTag.strategyType = cpu_to_le16(4);
1762 fe->icbTag.numEntries = cpu_to_le16(1);
1766 fe->icbTag.fileType = ICBTAG_FILE_TYPE_USE;
1767 else if (S_ISDIR(inode->i_mode))
1768 fe->icbTag.fileType = ICBTAG_FILE_TYPE_DIRECTORY;
1769 else if (S_ISREG(inode->i_mode))
1770 fe->icbTag.fileType = ICBTAG_FILE_TYPE_REGULAR;
1771 else if (S_ISLNK(inode->i_mode))
1772 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SYMLINK;
1773 else if (S_ISBLK(inode->i_mode))
1774 fe->icbTag.fileType = ICBTAG_FILE_TYPE_BLOCK;
1775 else if (S_ISCHR(inode->i_mode))
1776 fe->icbTag.fileType = ICBTAG_FILE_TYPE_CHAR;
1777 else if (S_ISFIFO(inode->i_mode))
1778 fe->icbTag.fileType = ICBTAG_FILE_TYPE_FIFO;
1779 else if (S_ISSOCK(inode->i_mode))
1780 fe->icbTag.fileType = ICBTAG_FILE_TYPE_SOCKET;
1782 icbflags = iinfo->i_alloc_type |
1783 ((inode->i_mode & S_ISUID) ? ICBTAG_FLAG_SETUID : 0) |
1784 ((inode->i_mode & S_ISGID) ? ICBTAG_FLAG_SETGID : 0) |
1785 ((inode->i_mode & S_ISVTX) ? ICBTAG_FLAG_STICKY : 0) |
1786 (le16_to_cpu(fe->icbTag.flags) &
1787 ~(ICBTAG_FLAG_AD_MASK | ICBTAG_FLAG_SETUID |
1788 ICBTAG_FLAG_SETGID | ICBTAG_FLAG_STICKY));
1790 fe->icbTag.flags = cpu_to_le16(icbflags);
1791 if (sbi->s_udfrev >= 0x0200)
1792 fe->descTag.descVersion = cpu_to_le16(3);
1794 fe->descTag.descVersion = cpu_to_le16(2);
1795 fe->descTag.tagSerialNum = cpu_to_le16(sbi->s_serial_number);
1796 fe->descTag.tagLocation = cpu_to_le32(
1797 iinfo->i_location.logicalBlockNum);
1798 crclen += iinfo->i_lenEAttr + iinfo->i_lenAlloc - sizeof(struct tag);
1799 fe->descTag.descCRCLength = cpu_to_le16(crclen);
1800 fe->descTag.descCRC = cpu_to_le16(crc_itu_t(0, (char *)fe + sizeof(struct tag),
1802 fe->descTag.tagChecksum = udf_tag_checksum(&fe->descTag);
1804 set_buffer_uptodate(bh);
1807 /* write the data blocks */
1808 mark_buffer_dirty(bh);
1810 sync_dirty_buffer(bh);
1811 if (buffer_write_io_error(bh)) {
1812 udf_warn(inode->i_sb, "IO error syncing udf inode [%08lx]\n",
1822 struct inode *__udf_iget(struct super_block *sb, struct kernel_lb_addr *ino,
1825 unsigned long block = udf_get_lb_pblock(sb, ino, 0);
1826 struct inode *inode = iget_locked(sb, block);
1830 return ERR_PTR(-ENOMEM);
1832 if (!(inode->i_state & I_NEW)) {
1833 if (UDF_I(inode)->i_hidden != hidden_inode) {
1835 return ERR_PTR(-EFSCORRUPTED);
1840 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
1841 err = udf_read_inode(inode, hidden_inode);
1844 return ERR_PTR(err);
1846 unlock_new_inode(inode);
1851 int udf_setup_indirect_aext(struct inode *inode, udf_pblk_t block,
1852 struct extent_position *epos)
1854 struct super_block *sb = inode->i_sb;
1855 struct buffer_head *bh;
1856 struct allocExtDesc *aed;
1857 struct extent_position nepos;
1858 struct kernel_lb_addr neloc;
1861 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1862 adsize = sizeof(struct short_ad);
1863 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1864 adsize = sizeof(struct long_ad);
1868 neloc.logicalBlockNum = block;
1869 neloc.partitionReferenceNum = epos->block.partitionReferenceNum;
1871 bh = sb_getblk(sb, udf_get_lb_pblock(sb, &neloc, 0));
1875 memset(bh->b_data, 0x00, sb->s_blocksize);
1876 set_buffer_uptodate(bh);
1878 mark_buffer_dirty_inode(bh, inode);
1880 aed = (struct allocExtDesc *)(bh->b_data);
1881 if (!UDF_QUERY_FLAG(sb, UDF_FLAG_STRICT)) {
1882 aed->previousAllocExtLocation =
1883 cpu_to_le32(epos->block.logicalBlockNum);
1885 aed->lengthAllocDescs = cpu_to_le32(0);
1886 if (UDF_SB(sb)->s_udfrev >= 0x0200)
1890 udf_new_tag(bh->b_data, TAG_IDENT_AED, ver, 1, block,
1891 sizeof(struct tag));
1893 nepos.block = neloc;
1894 nepos.offset = sizeof(struct allocExtDesc);
1898 * Do we have to copy current last extent to make space for indirect
1901 if (epos->offset + adsize > sb->s_blocksize) {
1902 struct kernel_lb_addr cp_loc;
1906 epos->offset -= adsize;
1907 cp_type = udf_current_aext(inode, epos, &cp_loc, &cp_len, 0);
1908 cp_len |= ((uint32_t)cp_type) << 30;
1910 __udf_add_aext(inode, &nepos, &cp_loc, cp_len, 1);
1911 udf_write_aext(inode, epos, &nepos.block,
1912 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
1914 __udf_add_aext(inode, epos, &nepos.block,
1915 sb->s_blocksize | EXT_NEXT_EXTENT_ALLOCDESCS, 0);
1925 * Append extent at the given position - should be the first free one in inode
1926 * / indirect extent. This function assumes there is enough space in the inode
1927 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1929 int __udf_add_aext(struct inode *inode, struct extent_position *epos,
1930 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1932 struct udf_inode_info *iinfo = UDF_I(inode);
1933 struct allocExtDesc *aed;
1936 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1937 adsize = sizeof(struct short_ad);
1938 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1939 adsize = sizeof(struct long_ad);
1944 WARN_ON(iinfo->i_lenAlloc !=
1945 epos->offset - udf_file_entry_alloc_offset(inode));
1947 aed = (struct allocExtDesc *)epos->bh->b_data;
1948 WARN_ON(le32_to_cpu(aed->lengthAllocDescs) !=
1949 epos->offset - sizeof(struct allocExtDesc));
1950 WARN_ON(epos->offset + adsize > inode->i_sb->s_blocksize);
1953 udf_write_aext(inode, epos, eloc, elen, inc);
1956 iinfo->i_lenAlloc += adsize;
1957 mark_inode_dirty(inode);
1959 aed = (struct allocExtDesc *)epos->bh->b_data;
1960 le32_add_cpu(&aed->lengthAllocDescs, adsize);
1961 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
1962 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
1963 udf_update_tag(epos->bh->b_data,
1964 epos->offset + (inc ? 0 : adsize));
1966 udf_update_tag(epos->bh->b_data,
1967 sizeof(struct allocExtDesc));
1968 mark_buffer_dirty_inode(epos->bh, inode);
1975 * Append extent at given position - should be the first free one in inode
1976 * / indirect extent. Takes care of allocating and linking indirect blocks.
1978 int udf_add_aext(struct inode *inode, struct extent_position *epos,
1979 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
1982 struct super_block *sb = inode->i_sb;
1984 if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
1985 adsize = sizeof(struct short_ad);
1986 else if (UDF_I(inode)->i_alloc_type == ICBTAG_FLAG_AD_LONG)
1987 adsize = sizeof(struct long_ad);
1991 if (epos->offset + (2 * adsize) > sb->s_blocksize) {
1993 udf_pblk_t new_block;
1995 new_block = udf_new_block(sb, NULL,
1996 epos->block.partitionReferenceNum,
1997 epos->block.logicalBlockNum, &err);
2001 err = udf_setup_indirect_aext(inode, new_block, epos);
2006 return __udf_add_aext(inode, epos, eloc, elen, inc);
2009 void udf_write_aext(struct inode *inode, struct extent_position *epos,
2010 struct kernel_lb_addr *eloc, uint32_t elen, int inc)
2014 struct short_ad *sad;
2015 struct long_ad *lad;
2016 struct udf_inode_info *iinfo = UDF_I(inode);
2019 ptr = iinfo->i_data + epos->offset -
2020 udf_file_entry_alloc_offset(inode) +
2023 ptr = epos->bh->b_data + epos->offset;
2025 switch (iinfo->i_alloc_type) {
2026 case ICBTAG_FLAG_AD_SHORT:
2027 sad = (struct short_ad *)ptr;
2028 sad->extLength = cpu_to_le32(elen);
2029 sad->extPosition = cpu_to_le32(eloc->logicalBlockNum);
2030 adsize = sizeof(struct short_ad);
2032 case ICBTAG_FLAG_AD_LONG:
2033 lad = (struct long_ad *)ptr;
2034 lad->extLength = cpu_to_le32(elen);
2035 lad->extLocation = cpu_to_lelb(*eloc);
2036 memset(lad->impUse, 0x00, sizeof(lad->impUse));
2037 adsize = sizeof(struct long_ad);
2044 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2045 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201) {
2046 struct allocExtDesc *aed =
2047 (struct allocExtDesc *)epos->bh->b_data;
2048 udf_update_tag(epos->bh->b_data,
2049 le32_to_cpu(aed->lengthAllocDescs) +
2050 sizeof(struct allocExtDesc));
2052 mark_buffer_dirty_inode(epos->bh, inode);
2054 mark_inode_dirty(inode);
2058 epos->offset += adsize;
2062 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2063 * someone does some weird stuff.
2065 #define UDF_MAX_INDIR_EXTS 16
2067 int8_t udf_next_aext(struct inode *inode, struct extent_position *epos,
2068 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2071 unsigned int indirections = 0;
2073 while ((etype = udf_current_aext(inode, epos, eloc, elen, inc)) ==
2074 (EXT_NEXT_EXTENT_ALLOCDESCS >> 30)) {
2077 if (++indirections > UDF_MAX_INDIR_EXTS) {
2078 udf_err(inode->i_sb,
2079 "too many indirect extents in inode %lu\n",
2084 epos->block = *eloc;
2085 epos->offset = sizeof(struct allocExtDesc);
2087 block = udf_get_lb_pblock(inode->i_sb, &epos->block, 0);
2088 epos->bh = sb_bread(inode->i_sb, block);
2090 udf_debug("reading block %u failed!\n", block);
2098 int8_t udf_current_aext(struct inode *inode, struct extent_position *epos,
2099 struct kernel_lb_addr *eloc, uint32_t *elen, int inc)
2104 struct short_ad *sad;
2105 struct long_ad *lad;
2106 struct udf_inode_info *iinfo = UDF_I(inode);
2110 epos->offset = udf_file_entry_alloc_offset(inode);
2111 ptr = iinfo->i_data + epos->offset -
2112 udf_file_entry_alloc_offset(inode) +
2114 alen = udf_file_entry_alloc_offset(inode) +
2118 epos->offset = sizeof(struct allocExtDesc);
2119 ptr = epos->bh->b_data + epos->offset;
2120 alen = sizeof(struct allocExtDesc) +
2121 le32_to_cpu(((struct allocExtDesc *)epos->bh->b_data)->
2125 switch (iinfo->i_alloc_type) {
2126 case ICBTAG_FLAG_AD_SHORT:
2127 sad = udf_get_fileshortad(ptr, alen, &epos->offset, inc);
2130 etype = le32_to_cpu(sad->extLength) >> 30;
2131 eloc->logicalBlockNum = le32_to_cpu(sad->extPosition);
2132 eloc->partitionReferenceNum =
2133 iinfo->i_location.partitionReferenceNum;
2134 *elen = le32_to_cpu(sad->extLength) & UDF_EXTENT_LENGTH_MASK;
2136 case ICBTAG_FLAG_AD_LONG:
2137 lad = udf_get_filelongad(ptr, alen, &epos->offset, inc);
2140 etype = le32_to_cpu(lad->extLength) >> 30;
2141 *eloc = lelb_to_cpu(lad->extLocation);
2142 *elen = le32_to_cpu(lad->extLength) & UDF_EXTENT_LENGTH_MASK;
2145 udf_debug("alloc_type = %u unsupported\n", iinfo->i_alloc_type);
2152 static int udf_insert_aext(struct inode *inode, struct extent_position epos,
2153 struct kernel_lb_addr neloc, uint32_t nelen)
2155 struct kernel_lb_addr oeloc;
2163 while ((etype = udf_next_aext(inode, &epos, &oeloc, &oelen, 0)) != -1) {
2164 udf_write_aext(inode, &epos, &neloc, nelen, 1);
2166 nelen = (etype << 30) | oelen;
2168 err = udf_add_aext(inode, &epos, &neloc, nelen, 1);
2174 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)
2176 struct extent_position oepos;
2179 struct allocExtDesc *aed;
2180 struct udf_inode_info *iinfo;
2181 struct kernel_lb_addr eloc;
2189 iinfo = UDF_I(inode);
2190 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
2191 adsize = sizeof(struct short_ad);
2192 else if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_LONG)
2193 adsize = sizeof(struct long_ad);
2198 if (udf_next_aext(inode, &epos, &eloc, &elen, 1) == -1)
2201 while ((etype = udf_next_aext(inode, &epos, &eloc, &elen, 1)) != -1) {
2202 udf_write_aext(inode, &oepos, &eloc, (etype << 30) | elen, 1);
2203 if (oepos.bh != epos.bh) {
2204 oepos.block = epos.block;
2208 oepos.offset = epos.offset - adsize;
2211 memset(&eloc, 0x00, sizeof(struct kernel_lb_addr));
2214 if (epos.bh != oepos.bh) {
2215 udf_free_blocks(inode->i_sb, inode, &epos.block, 0, 1);
2216 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2217 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2219 iinfo->i_lenAlloc -= (adsize * 2);
2220 mark_inode_dirty(inode);
2222 aed = (struct allocExtDesc *)oepos.bh->b_data;
2223 le32_add_cpu(&aed->lengthAllocDescs, -(2 * adsize));
2224 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2225 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2226 udf_update_tag(oepos.bh->b_data,
2227 oepos.offset - (2 * adsize));
2229 udf_update_tag(oepos.bh->b_data,
2230 sizeof(struct allocExtDesc));
2231 mark_buffer_dirty_inode(oepos.bh, inode);
2234 udf_write_aext(inode, &oepos, &eloc, elen, 1);
2236 iinfo->i_lenAlloc -= adsize;
2237 mark_inode_dirty(inode);
2239 aed = (struct allocExtDesc *)oepos.bh->b_data;
2240 le32_add_cpu(&aed->lengthAllocDescs, -adsize);
2241 if (!UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_STRICT) ||
2242 UDF_SB(inode->i_sb)->s_udfrev >= 0x0201)
2243 udf_update_tag(oepos.bh->b_data,
2244 epos.offset - adsize);
2246 udf_update_tag(oepos.bh->b_data,
2247 sizeof(struct allocExtDesc));
2248 mark_buffer_dirty_inode(oepos.bh, inode);
2255 return (elen >> 30);
2258 int8_t inode_bmap(struct inode *inode, sector_t block,
2259 struct extent_position *pos, struct kernel_lb_addr *eloc,
2260 uint32_t *elen, sector_t *offset)
2262 unsigned char blocksize_bits = inode->i_sb->s_blocksize_bits;
2263 loff_t lbcount = 0, bcount = (loff_t) block << blocksize_bits;
2265 struct udf_inode_info *iinfo;
2267 iinfo = UDF_I(inode);
2268 if (!udf_read_extent_cache(inode, bcount, &lbcount, pos)) {
2270 pos->block = iinfo->i_location;
2275 etype = udf_next_aext(inode, pos, eloc, elen, 1);
2277 *offset = (bcount - lbcount) >> blocksize_bits;
2278 iinfo->i_lenExtents = lbcount;
2282 } while (lbcount <= bcount);
2283 /* update extent cache */
2284 udf_update_extent_cache(inode, lbcount - *elen, pos);
2285 *offset = (bcount + *elen - lbcount) >> blocksize_bits;