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(struct kiocb
*iocb
, struct iov_iter
*iter
,
220 struct file
*file
= iocb
->ki_filp
;
221 struct address_space
*mapping
= file
->f_mapping
;
222 struct inode
*inode
= mapping
->host
;
223 size_t count
= iov_iter_count(iter
);
226 ret
= blockdev_direct_IO(iocb
, inode
, iter
, offset
, udf_get_block
);
227 if (unlikely(ret
< 0 && iov_iter_rw(iter
) == WRITE
))
228 udf_write_failed(mapping
, offset
+ count
);
232 static sector_t
udf_bmap(struct address_space
*mapping
, sector_t block
)
234 return generic_block_bmap(mapping
, block
, udf_get_block
);
237 const struct address_space_operations udf_aops
= {
238 .readpage
= udf_readpage
,
239 .readpages
= udf_readpages
,
240 .writepage
= udf_writepage
,
241 .writepages
= udf_writepages
,
242 .write_begin
= udf_write_begin
,
243 .write_end
= generic_write_end
,
244 .direct_IO
= udf_direct_IO
,
249 * Expand file stored in ICB to a normal one-block-file
251 * This function requires i_data_sem for writing and releases it.
252 * This function requires i_mutex held
254 int udf_expand_file_adinicb(struct inode
*inode
)
258 struct udf_inode_info
*iinfo
= UDF_I(inode
);
260 struct writeback_control udf_wbc
= {
261 .sync_mode
= WB_SYNC_NONE
,
265 WARN_ON_ONCE(!mutex_is_locked(&inode
->i_mutex
));
266 if (!iinfo
->i_lenAlloc
) {
267 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
268 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_SHORT
;
270 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_LONG
;
271 /* from now on we have normal address_space methods */
272 inode
->i_data
.a_ops
= &udf_aops
;
273 up_write(&iinfo
->i_data_sem
);
274 mark_inode_dirty(inode
);
278 * Release i_data_sem so that we can lock a page - page lock ranks
279 * above i_data_sem. i_mutex still protects us against file changes.
281 up_write(&iinfo
->i_data_sem
);
283 page
= find_or_create_page(inode
->i_mapping
, 0, GFP_NOFS
);
287 if (!PageUptodate(page
)) {
289 memset(kaddr
+ iinfo
->i_lenAlloc
, 0x00,
290 PAGE_CACHE_SIZE
- iinfo
->i_lenAlloc
);
291 memcpy(kaddr
, iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
,
293 flush_dcache_page(page
);
294 SetPageUptodate(page
);
297 down_write(&iinfo
->i_data_sem
);
298 memset(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
, 0x00,
300 iinfo
->i_lenAlloc
= 0;
301 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
302 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_SHORT
;
304 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_LONG
;
305 /* from now on we have normal address_space methods */
306 inode
->i_data
.a_ops
= &udf_aops
;
307 up_write(&iinfo
->i_data_sem
);
308 err
= inode
->i_data
.a_ops
->writepage(page
, &udf_wbc
);
310 /* Restore everything back so that we don't lose data... */
313 down_write(&iinfo
->i_data_sem
);
314 memcpy(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
, kaddr
,
318 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_IN_ICB
;
319 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
320 up_write(&iinfo
->i_data_sem
);
322 page_cache_release(page
);
323 mark_inode_dirty(inode
);
328 struct buffer_head
*udf_expand_dir_adinicb(struct inode
*inode
, int *block
,
332 struct buffer_head
*dbh
= NULL
;
333 struct kernel_lb_addr eloc
;
335 struct extent_position epos
;
337 struct udf_fileident_bh sfibh
, dfibh
;
338 loff_t f_pos
= udf_ext0_offset(inode
);
339 int size
= udf_ext0_offset(inode
) + inode
->i_size
;
340 struct fileIdentDesc cfi
, *sfi
, *dfi
;
341 struct udf_inode_info
*iinfo
= UDF_I(inode
);
343 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
344 alloctype
= ICBTAG_FLAG_AD_SHORT
;
346 alloctype
= ICBTAG_FLAG_AD_LONG
;
348 if (!inode
->i_size
) {
349 iinfo
->i_alloc_type
= alloctype
;
350 mark_inode_dirty(inode
);
354 /* alloc block, and copy data to it */
355 *block
= udf_new_block(inode
->i_sb
, inode
,
356 iinfo
->i_location
.partitionReferenceNum
,
357 iinfo
->i_location
.logicalBlockNum
, err
);
360 newblock
= udf_get_pblock(inode
->i_sb
, *block
,
361 iinfo
->i_location
.partitionReferenceNum
,
365 dbh
= udf_tgetblk(inode
->i_sb
, newblock
);
369 memset(dbh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
370 set_buffer_uptodate(dbh
);
372 mark_buffer_dirty_inode(dbh
, inode
);
374 sfibh
.soffset
= sfibh
.eoffset
=
375 f_pos
& (inode
->i_sb
->s_blocksize
- 1);
376 sfibh
.sbh
= sfibh
.ebh
= NULL
;
377 dfibh
.soffset
= dfibh
.eoffset
= 0;
378 dfibh
.sbh
= dfibh
.ebh
= dbh
;
379 while (f_pos
< size
) {
380 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_IN_ICB
;
381 sfi
= udf_fileident_read(inode
, &f_pos
, &sfibh
, &cfi
, NULL
,
387 iinfo
->i_alloc_type
= alloctype
;
388 sfi
->descTag
.tagLocation
= cpu_to_le32(*block
);
389 dfibh
.soffset
= dfibh
.eoffset
;
390 dfibh
.eoffset
+= (sfibh
.eoffset
- sfibh
.soffset
);
391 dfi
= (struct fileIdentDesc
*)(dbh
->b_data
+ dfibh
.soffset
);
392 if (udf_write_fi(inode
, sfi
, dfi
, &dfibh
, sfi
->impUse
,
394 le16_to_cpu(sfi
->lengthOfImpUse
))) {
395 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_IN_ICB
;
400 mark_buffer_dirty_inode(dbh
, inode
);
402 memset(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
, 0,
404 iinfo
->i_lenAlloc
= 0;
405 eloc
.logicalBlockNum
= *block
;
406 eloc
.partitionReferenceNum
=
407 iinfo
->i_location
.partitionReferenceNum
;
408 iinfo
->i_lenExtents
= inode
->i_size
;
410 epos
.block
= iinfo
->i_location
;
411 epos
.offset
= udf_file_entry_alloc_offset(inode
);
412 udf_add_aext(inode
, &epos
, &eloc
, inode
->i_size
, 0);
416 mark_inode_dirty(inode
);
420 static int udf_get_block(struct inode
*inode
, sector_t block
,
421 struct buffer_head
*bh_result
, int create
)
425 struct udf_inode_info
*iinfo
;
428 phys
= udf_block_map(inode
, block
);
430 map_bh(bh_result
, inode
->i_sb
, phys
);
436 iinfo
= UDF_I(inode
);
438 down_write(&iinfo
->i_data_sem
);
439 if (block
== iinfo
->i_next_alloc_block
+ 1) {
440 iinfo
->i_next_alloc_block
++;
441 iinfo
->i_next_alloc_goal
++;
444 udf_clear_extent_cache(inode
);
445 phys
= inode_getblk(inode
, block
, &err
, &new);
450 set_buffer_new(bh_result
);
451 map_bh(bh_result
, inode
->i_sb
, phys
);
454 up_write(&iinfo
->i_data_sem
);
458 static struct buffer_head
*udf_getblk(struct inode
*inode
, long block
,
459 int create
, int *err
)
461 struct buffer_head
*bh
;
462 struct buffer_head dummy
;
465 dummy
.b_blocknr
= -1000;
466 *err
= udf_get_block(inode
, block
, &dummy
, create
);
467 if (!*err
&& buffer_mapped(&dummy
)) {
468 bh
= sb_getblk(inode
->i_sb
, dummy
.b_blocknr
);
469 if (buffer_new(&dummy
)) {
471 memset(bh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
472 set_buffer_uptodate(bh
);
474 mark_buffer_dirty_inode(bh
, inode
);
482 /* Extend the file with new blocks totaling 'new_block_bytes',
483 * return the number of extents added
485 static int udf_do_extend_file(struct inode
*inode
,
486 struct extent_position
*last_pos
,
487 struct kernel_long_ad
*last_ext
,
488 loff_t new_block_bytes
)
491 int count
= 0, fake
= !(last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
);
492 struct super_block
*sb
= inode
->i_sb
;
493 struct kernel_lb_addr prealloc_loc
= {};
494 int prealloc_len
= 0;
495 struct udf_inode_info
*iinfo
;
498 /* The previous extent is fake and we should not extend by anything
499 * - there's nothing to do... */
500 if (!new_block_bytes
&& fake
)
503 iinfo
= UDF_I(inode
);
504 /* Round the last extent up to a multiple of block size */
505 if (last_ext
->extLength
& (sb
->s_blocksize
- 1)) {
506 last_ext
->extLength
=
507 (last_ext
->extLength
& UDF_EXTENT_FLAG_MASK
) |
508 (((last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
509 sb
->s_blocksize
- 1) & ~(sb
->s_blocksize
- 1));
510 iinfo
->i_lenExtents
=
511 (iinfo
->i_lenExtents
+ sb
->s_blocksize
- 1) &
512 ~(sb
->s_blocksize
- 1);
515 /* Last extent are just preallocated blocks? */
516 if ((last_ext
->extLength
& UDF_EXTENT_FLAG_MASK
) ==
517 EXT_NOT_RECORDED_ALLOCATED
) {
518 /* Save the extent so that we can reattach it to the end */
519 prealloc_loc
= last_ext
->extLocation
;
520 prealloc_len
= last_ext
->extLength
;
521 /* Mark the extent as a hole */
522 last_ext
->extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
|
523 (last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
);
524 last_ext
->extLocation
.logicalBlockNum
= 0;
525 last_ext
->extLocation
.partitionReferenceNum
= 0;
528 /* Can we merge with the previous extent? */
529 if ((last_ext
->extLength
& UDF_EXTENT_FLAG_MASK
) ==
530 EXT_NOT_RECORDED_NOT_ALLOCATED
) {
531 add
= (1 << 30) - sb
->s_blocksize
-
532 (last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
);
533 if (add
> new_block_bytes
)
534 add
= new_block_bytes
;
535 new_block_bytes
-= add
;
536 last_ext
->extLength
+= add
;
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? */
548 if (!new_block_bytes
)
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
;
555 last_ext
->extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
| add
;
557 /* Create enough extents to cover the whole hole */
558 while (new_block_bytes
> add
) {
559 new_block_bytes
-= add
;
560 err
= udf_add_aext(inode
, last_pos
, &last_ext
->extLocation
,
561 last_ext
->extLength
, 1);
566 if (new_block_bytes
) {
567 last_ext
->extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
|
569 err
= udf_add_aext(inode
, last_pos
, &last_ext
->extLocation
,
570 last_ext
->extLength
, 1);
577 /* Do we have some preallocated blocks saved? */
579 err
= udf_add_aext(inode
, last_pos
, &prealloc_loc
,
583 last_ext
->extLocation
= prealloc_loc
;
584 last_ext
->extLength
= prealloc_len
;
588 /* last_pos should point to the last written extent... */
589 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
590 last_pos
->offset
-= sizeof(struct short_ad
);
591 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
592 last_pos
->offset
-= sizeof(struct long_ad
);
599 /* Extend the final block of the file to final_block_len bytes */
600 static void udf_do_extend_final_block(struct inode
*inode
,
601 struct extent_position
*last_pos
,
602 struct kernel_long_ad
*last_ext
,
603 uint32_t final_block_len
)
605 struct super_block
*sb
= inode
->i_sb
;
606 uint32_t added_bytes
;
608 added_bytes
= final_block_len
-
609 (last_ext
->extLength
& (sb
->s_blocksize
- 1));
610 last_ext
->extLength
+= added_bytes
;
611 UDF_I(inode
)->i_lenExtents
+= added_bytes
;
613 udf_write_aext(inode
, last_pos
, &last_ext
->extLocation
,
614 last_ext
->extLength
, 1);
617 static int udf_extend_file(struct inode
*inode
, loff_t newsize
)
620 struct extent_position epos
;
621 struct kernel_lb_addr eloc
;
624 struct super_block
*sb
= inode
->i_sb
;
625 sector_t first_block
= newsize
>> sb
->s_blocksize_bits
, offset
;
626 unsigned long partial_final_block
;
628 struct udf_inode_info
*iinfo
= UDF_I(inode
);
629 struct kernel_long_ad extent
;
631 int within_final_block
;
633 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
634 adsize
= sizeof(struct short_ad
);
635 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
636 adsize
= sizeof(struct long_ad
);
640 etype
= inode_bmap(inode
, first_block
, &epos
, &eloc
, &elen
, &offset
);
641 within_final_block
= (etype
!= -1);
643 if ((!epos
.bh
&& epos
.offset
== udf_file_entry_alloc_offset(inode
)) ||
644 (epos
.bh
&& epos
.offset
== sizeof(struct allocExtDesc
))) {
645 /* File has no extents at all or has empty last
646 * indirect extent! Create a fake extent... */
647 extent
.extLocation
.logicalBlockNum
= 0;
648 extent
.extLocation
.partitionReferenceNum
= 0;
649 extent
.extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
;
651 epos
.offset
-= adsize
;
652 etype
= udf_next_aext(inode
, &epos
, &extent
.extLocation
,
653 &extent
.extLength
, 0);
654 extent
.extLength
|= etype
<< 30;
657 partial_final_block
= newsize
& (sb
->s_blocksize
- 1);
659 /* File has extent covering the new size (could happen when extending
662 if (within_final_block
) {
663 /* Extending file within the last file block */
664 udf_do_extend_final_block(inode
, &epos
, &extent
,
665 partial_final_block
);
667 loff_t add
= ((loff_t
)offset
<< sb
->s_blocksize_bits
) |
669 err
= udf_do_extend_file(inode
, &epos
, &extent
, add
);
675 iinfo
->i_lenExtents
= newsize
;
681 static sector_t
inode_getblk(struct inode
*inode
, sector_t block
,
684 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
];
685 struct extent_position prev_epos
, cur_epos
, next_epos
;
686 int count
= 0, startnum
= 0, endnum
= 0;
687 uint32_t elen
= 0, tmpelen
;
688 struct kernel_lb_addr eloc
, tmpeloc
;
690 loff_t lbcount
= 0, b_off
= 0;
691 uint32_t newblocknum
, newblock
;
694 struct udf_inode_info
*iinfo
= UDF_I(inode
);
695 int goal
= 0, pgoal
= iinfo
->i_location
.logicalBlockNum
;
701 prev_epos
.offset
= udf_file_entry_alloc_offset(inode
);
702 prev_epos
.block
= iinfo
->i_location
;
704 cur_epos
= next_epos
= prev_epos
;
705 b_off
= (loff_t
)block
<< inode
->i_sb
->s_blocksize_bits
;
707 /* find the extent which contains the block we are looking for.
708 alternate between laarr[0] and laarr[1] for locations of the
709 current extent, and the previous extent */
711 if (prev_epos
.bh
!= cur_epos
.bh
) {
712 brelse(prev_epos
.bh
);
714 prev_epos
.bh
= cur_epos
.bh
;
716 if (cur_epos
.bh
!= next_epos
.bh
) {
718 get_bh(next_epos
.bh
);
719 cur_epos
.bh
= next_epos
.bh
;
724 prev_epos
.block
= cur_epos
.block
;
725 cur_epos
.block
= next_epos
.block
;
727 prev_epos
.offset
= cur_epos
.offset
;
728 cur_epos
.offset
= next_epos
.offset
;
730 etype
= udf_next_aext(inode
, &next_epos
, &eloc
, &elen
, 1);
736 laarr
[c
].extLength
= (etype
<< 30) | elen
;
737 laarr
[c
].extLocation
= eloc
;
739 if (etype
!= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
740 pgoal
= eloc
.logicalBlockNum
+
741 ((elen
+ inode
->i_sb
->s_blocksize
- 1) >>
742 inode
->i_sb
->s_blocksize_bits
);
745 } while (lbcount
+ elen
<= b_off
);
748 offset
= b_off
>> inode
->i_sb
->s_blocksize_bits
;
750 * Move prev_epos and cur_epos into indirect extent if we are at
753 udf_next_aext(inode
, &prev_epos
, &tmpeloc
, &tmpelen
, 0);
754 udf_next_aext(inode
, &cur_epos
, &tmpeloc
, &tmpelen
, 0);
756 /* if the extent is allocated and recorded, return the block
757 if the extent is not a multiple of the blocksize, round up */
759 if (etype
== (EXT_RECORDED_ALLOCATED
>> 30)) {
760 if (elen
& (inode
->i_sb
->s_blocksize
- 1)) {
761 elen
= EXT_RECORDED_ALLOCATED
|
762 ((elen
+ inode
->i_sb
->s_blocksize
- 1) &
763 ~(inode
->i_sb
->s_blocksize
- 1));
764 udf_write_aext(inode
, &cur_epos
, &eloc
, elen
, 1);
766 brelse(prev_epos
.bh
);
768 brelse(next_epos
.bh
);
769 newblock
= udf_get_lb_pblock(inode
->i_sb
, &eloc
, offset
);
773 /* Are we beyond EOF? */
783 /* Create a fake extent when there's not one */
784 memset(&laarr
[0].extLocation
, 0x00,
785 sizeof(struct kernel_lb_addr
));
786 laarr
[0].extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
;
787 /* Will udf_do_extend_file() create real extent from
789 startnum
= (offset
> 0);
791 /* Create extents for the hole between EOF and offset */
792 hole_len
= (loff_t
)offset
<< inode
->i_blkbits
;
793 ret
= udf_do_extend_file(inode
, &prev_epos
, laarr
, hole_len
);
795 brelse(prev_epos
.bh
);
797 brelse(next_epos
.bh
);
804 /* We are not covered by a preallocated extent? */
805 if ((laarr
[0].extLength
& UDF_EXTENT_FLAG_MASK
) !=
806 EXT_NOT_RECORDED_ALLOCATED
) {
807 /* Is there any real extent? - otherwise we overwrite
811 laarr
[c
].extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
|
812 inode
->i_sb
->s_blocksize
;
813 memset(&laarr
[c
].extLocation
, 0x00,
814 sizeof(struct kernel_lb_addr
));
821 endnum
= startnum
= ((count
> 2) ? 2 : count
);
823 /* if the current extent is in position 0,
824 swap it with the previous */
825 if (!c
&& count
!= 1) {
832 /* if the current block is located in an extent,
833 read the next extent */
834 etype
= udf_next_aext(inode
, &next_epos
, &eloc
, &elen
, 0);
836 laarr
[c
+ 1].extLength
= (etype
<< 30) | elen
;
837 laarr
[c
+ 1].extLocation
= eloc
;
845 /* if the current extent is not recorded but allocated, get the
846 * block in the extent corresponding to the requested block */
847 if ((laarr
[c
].extLength
>> 30) == (EXT_NOT_RECORDED_ALLOCATED
>> 30))
848 newblocknum
= laarr
[c
].extLocation
.logicalBlockNum
+ offset
;
849 else { /* otherwise, allocate a new block */
850 if (iinfo
->i_next_alloc_block
== block
)
851 goal
= iinfo
->i_next_alloc_goal
;
854 if (!(goal
= pgoal
)) /* XXX: what was intended here? */
855 goal
= iinfo
->i_location
.logicalBlockNum
+ 1;
858 newblocknum
= udf_new_block(inode
->i_sb
, inode
,
859 iinfo
->i_location
.partitionReferenceNum
,
862 brelse(prev_epos
.bh
);
864 brelse(next_epos
.bh
);
869 iinfo
->i_lenExtents
+= inode
->i_sb
->s_blocksize
;
872 /* if the extent the requsted block is located in contains multiple
873 * blocks, split the extent into at most three extents. blocks prior
874 * to requested block, requested block, and blocks after requested
876 udf_split_extents(inode
, &c
, offset
, newblocknum
, laarr
, &endnum
);
878 #ifdef UDF_PREALLOCATE
879 /* We preallocate blocks only for regular files. It also makes sense
880 * for directories but there's a problem when to drop the
881 * preallocation. We might use some delayed work for that but I feel
882 * it's overengineering for a filesystem like UDF. */
883 if (S_ISREG(inode
->i_mode
))
884 udf_prealloc_extents(inode
, c
, lastblock
, laarr
, &endnum
);
887 /* merge any continuous blocks in laarr */
888 udf_merge_extents(inode
, laarr
, &endnum
);
890 /* write back the new extents, inserting new extents if the new number
891 * of extents is greater than the old number, and deleting extents if
892 * the new number of extents is less than the old number */
893 udf_update_extents(inode
, laarr
, startnum
, endnum
, &prev_epos
);
895 brelse(prev_epos
.bh
);
897 brelse(next_epos
.bh
);
899 newblock
= udf_get_pblock(inode
->i_sb
, newblocknum
,
900 iinfo
->i_location
.partitionReferenceNum
, 0);
906 iinfo
->i_next_alloc_block
= block
;
907 iinfo
->i_next_alloc_goal
= newblocknum
;
908 inode
->i_ctime
= current_fs_time(inode
->i_sb
);
911 udf_sync_inode(inode
);
913 mark_inode_dirty(inode
);
918 static void udf_split_extents(struct inode
*inode
, int *c
, int offset
,
920 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
923 unsigned long blocksize
= inode
->i_sb
->s_blocksize
;
924 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
926 if ((laarr
[*c
].extLength
>> 30) == (EXT_NOT_RECORDED_ALLOCATED
>> 30) ||
927 (laarr
[*c
].extLength
>> 30) ==
928 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30)) {
930 int blen
= ((laarr
[curr
].extLength
& UDF_EXTENT_LENGTH_MASK
) +
931 blocksize
- 1) >> blocksize_bits
;
932 int8_t etype
= (laarr
[curr
].extLength
>> 30);
936 else if (!offset
|| blen
== offset
+ 1) {
937 laarr
[curr
+ 2] = laarr
[curr
+ 1];
938 laarr
[curr
+ 1] = laarr
[curr
];
940 laarr
[curr
+ 3] = laarr
[curr
+ 1];
941 laarr
[curr
+ 2] = laarr
[curr
+ 1] = laarr
[curr
];
945 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
946 udf_free_blocks(inode
->i_sb
, inode
,
947 &laarr
[curr
].extLocation
,
949 laarr
[curr
].extLength
=
950 EXT_NOT_RECORDED_NOT_ALLOCATED
|
951 (offset
<< blocksize_bits
);
952 laarr
[curr
].extLocation
.logicalBlockNum
= 0;
953 laarr
[curr
].extLocation
.
954 partitionReferenceNum
= 0;
956 laarr
[curr
].extLength
= (etype
<< 30) |
957 (offset
<< blocksize_bits
);
963 laarr
[curr
].extLocation
.logicalBlockNum
= newblocknum
;
964 if (etype
== (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
965 laarr
[curr
].extLocation
.partitionReferenceNum
=
966 UDF_I(inode
)->i_location
.partitionReferenceNum
;
967 laarr
[curr
].extLength
= EXT_RECORDED_ALLOCATED
|
971 if (blen
!= offset
+ 1) {
972 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30))
973 laarr
[curr
].extLocation
.logicalBlockNum
+=
975 laarr
[curr
].extLength
= (etype
<< 30) |
976 ((blen
- (offset
+ 1)) << blocksize_bits
);
983 static void udf_prealloc_extents(struct inode
*inode
, int c
, int lastblock
,
984 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
987 int start
, length
= 0, currlength
= 0, i
;
989 if (*endnum
>= (c
+ 1)) {
995 if ((laarr
[c
+ 1].extLength
>> 30) ==
996 (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
998 length
= currlength
=
999 (((laarr
[c
+ 1].extLength
&
1000 UDF_EXTENT_LENGTH_MASK
) +
1001 inode
->i_sb
->s_blocksize
- 1) >>
1002 inode
->i_sb
->s_blocksize_bits
);
1007 for (i
= start
+ 1; i
<= *endnum
; i
++) {
1010 length
+= UDF_DEFAULT_PREALLOC_BLOCKS
;
1011 } else if ((laarr
[i
].extLength
>> 30) ==
1012 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30)) {
1013 length
+= (((laarr
[i
].extLength
&
1014 UDF_EXTENT_LENGTH_MASK
) +
1015 inode
->i_sb
->s_blocksize
- 1) >>
1016 inode
->i_sb
->s_blocksize_bits
);
1022 int next
= laarr
[start
].extLocation
.logicalBlockNum
+
1023 (((laarr
[start
].extLength
& UDF_EXTENT_LENGTH_MASK
) +
1024 inode
->i_sb
->s_blocksize
- 1) >>
1025 inode
->i_sb
->s_blocksize_bits
);
1026 int numalloc
= udf_prealloc_blocks(inode
->i_sb
, inode
,
1027 laarr
[start
].extLocation
.partitionReferenceNum
,
1028 next
, (UDF_DEFAULT_PREALLOC_BLOCKS
> length
?
1029 length
: UDF_DEFAULT_PREALLOC_BLOCKS
) -
1032 if (start
== (c
+ 1))
1033 laarr
[start
].extLength
+=
1035 inode
->i_sb
->s_blocksize_bits
);
1037 memmove(&laarr
[c
+ 2], &laarr
[c
+ 1],
1038 sizeof(struct long_ad
) * (*endnum
- (c
+ 1)));
1040 laarr
[c
+ 1].extLocation
.logicalBlockNum
= next
;
1041 laarr
[c
+ 1].extLocation
.partitionReferenceNum
=
1042 laarr
[c
].extLocation
.
1043 partitionReferenceNum
;
1044 laarr
[c
+ 1].extLength
=
1045 EXT_NOT_RECORDED_ALLOCATED
|
1047 inode
->i_sb
->s_blocksize_bits
);
1051 for (i
= start
+ 1; numalloc
&& i
< *endnum
; i
++) {
1052 int elen
= ((laarr
[i
].extLength
&
1053 UDF_EXTENT_LENGTH_MASK
) +
1054 inode
->i_sb
->s_blocksize
- 1) >>
1055 inode
->i_sb
->s_blocksize_bits
;
1057 if (elen
> numalloc
) {
1058 laarr
[i
].extLength
-=
1060 inode
->i_sb
->s_blocksize_bits
);
1064 if (*endnum
> (i
+ 1))
1067 sizeof(struct long_ad
) *
1068 (*endnum
- (i
+ 1)));
1073 UDF_I(inode
)->i_lenExtents
+=
1074 numalloc
<< inode
->i_sb
->s_blocksize_bits
;
1079 static void udf_merge_extents(struct inode
*inode
,
1080 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
1084 unsigned long blocksize
= inode
->i_sb
->s_blocksize
;
1085 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
1087 for (i
= 0; i
< (*endnum
- 1); i
++) {
1088 struct kernel_long_ad
*li
/*l[i]*/ = &laarr
[i
];
1089 struct kernel_long_ad
*lip1
/*l[i plus 1]*/ = &laarr
[i
+ 1];
1091 if (((li
->extLength
>> 30) == (lip1
->extLength
>> 30)) &&
1092 (((li
->extLength
>> 30) ==
1093 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30)) ||
1094 ((lip1
->extLocation
.logicalBlockNum
-
1095 li
->extLocation
.logicalBlockNum
) ==
1096 (((li
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1097 blocksize
- 1) >> blocksize_bits
)))) {
1099 if (((li
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1100 (lip1
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1101 blocksize
- 1) & ~UDF_EXTENT_LENGTH_MASK
) {
1102 lip1
->extLength
= (lip1
->extLength
-
1104 UDF_EXTENT_LENGTH_MASK
) +
1105 UDF_EXTENT_LENGTH_MASK
) &
1107 li
->extLength
= (li
->extLength
&
1108 UDF_EXTENT_FLAG_MASK
) +
1109 (UDF_EXTENT_LENGTH_MASK
+ 1) -
1111 lip1
->extLocation
.logicalBlockNum
=
1112 li
->extLocation
.logicalBlockNum
+
1114 UDF_EXTENT_LENGTH_MASK
) >>
1117 li
->extLength
= lip1
->extLength
+
1119 UDF_EXTENT_LENGTH_MASK
) +
1120 blocksize
- 1) & ~(blocksize
- 1));
1121 if (*endnum
> (i
+ 2))
1122 memmove(&laarr
[i
+ 1], &laarr
[i
+ 2],
1123 sizeof(struct long_ad
) *
1124 (*endnum
- (i
+ 2)));
1128 } else if (((li
->extLength
>> 30) ==
1129 (EXT_NOT_RECORDED_ALLOCATED
>> 30)) &&
1130 ((lip1
->extLength
>> 30) ==
1131 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))) {
1132 udf_free_blocks(inode
->i_sb
, inode
, &li
->extLocation
, 0,
1134 UDF_EXTENT_LENGTH_MASK
) +
1135 blocksize
- 1) >> blocksize_bits
);
1136 li
->extLocation
.logicalBlockNum
= 0;
1137 li
->extLocation
.partitionReferenceNum
= 0;
1139 if (((li
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1140 (lip1
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1141 blocksize
- 1) & ~UDF_EXTENT_LENGTH_MASK
) {
1142 lip1
->extLength
= (lip1
->extLength
-
1144 UDF_EXTENT_LENGTH_MASK
) +
1145 UDF_EXTENT_LENGTH_MASK
) &
1147 li
->extLength
= (li
->extLength
&
1148 UDF_EXTENT_FLAG_MASK
) +
1149 (UDF_EXTENT_LENGTH_MASK
+ 1) -
1152 li
->extLength
= lip1
->extLength
+
1154 UDF_EXTENT_LENGTH_MASK
) +
1155 blocksize
- 1) & ~(blocksize
- 1));
1156 if (*endnum
> (i
+ 2))
1157 memmove(&laarr
[i
+ 1], &laarr
[i
+ 2],
1158 sizeof(struct long_ad
) *
1159 (*endnum
- (i
+ 2)));
1163 } else if ((li
->extLength
>> 30) ==
1164 (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
1165 udf_free_blocks(inode
->i_sb
, inode
,
1166 &li
->extLocation
, 0,
1168 UDF_EXTENT_LENGTH_MASK
) +
1169 blocksize
- 1) >> blocksize_bits
);
1170 li
->extLocation
.logicalBlockNum
= 0;
1171 li
->extLocation
.partitionReferenceNum
= 0;
1172 li
->extLength
= (li
->extLength
&
1173 UDF_EXTENT_LENGTH_MASK
) |
1174 EXT_NOT_RECORDED_NOT_ALLOCATED
;
1179 static void udf_update_extents(struct inode
*inode
,
1180 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
1181 int startnum
, int endnum
,
1182 struct extent_position
*epos
)
1185 struct kernel_lb_addr tmploc
;
1188 if (startnum
> endnum
) {
1189 for (i
= 0; i
< (startnum
- endnum
); i
++)
1190 udf_delete_aext(inode
, *epos
, laarr
[i
].extLocation
,
1191 laarr
[i
].extLength
);
1192 } else if (startnum
< endnum
) {
1193 for (i
= 0; i
< (endnum
- startnum
); i
++) {
1194 udf_insert_aext(inode
, *epos
, laarr
[i
].extLocation
,
1195 laarr
[i
].extLength
);
1196 udf_next_aext(inode
, epos
, &laarr
[i
].extLocation
,
1197 &laarr
[i
].extLength
, 1);
1202 for (i
= start
; i
< endnum
; i
++) {
1203 udf_next_aext(inode
, epos
, &tmploc
, &tmplen
, 0);
1204 udf_write_aext(inode
, epos
, &laarr
[i
].extLocation
,
1205 laarr
[i
].extLength
, 1);
1209 struct buffer_head
*udf_bread(struct inode
*inode
, int block
,
1210 int create
, int *err
)
1212 struct buffer_head
*bh
= NULL
;
1214 bh
= udf_getblk(inode
, block
, create
, err
);
1218 if (buffer_uptodate(bh
))
1221 ll_rw_block(READ
, 1, &bh
);
1224 if (buffer_uptodate(bh
))
1232 int udf_setsize(struct inode
*inode
, loff_t newsize
)
1235 struct udf_inode_info
*iinfo
;
1236 int bsize
= i_blocksize(inode
);
1238 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1239 S_ISLNK(inode
->i_mode
)))
1241 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1244 iinfo
= UDF_I(inode
);
1245 if (newsize
> inode
->i_size
) {
1246 down_write(&iinfo
->i_data_sem
);
1247 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1249 (udf_file_entry_alloc_offset(inode
) + newsize
)) {
1250 err
= udf_expand_file_adinicb(inode
);
1253 down_write(&iinfo
->i_data_sem
);
1255 iinfo
->i_lenAlloc
= newsize
;
1259 err
= udf_extend_file(inode
, newsize
);
1261 up_write(&iinfo
->i_data_sem
);
1265 up_write(&iinfo
->i_data_sem
);
1266 truncate_setsize(inode
, newsize
);
1268 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1269 down_write(&iinfo
->i_data_sem
);
1270 udf_clear_extent_cache(inode
);
1271 memset(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
+ newsize
,
1272 0x00, bsize
- newsize
-
1273 udf_file_entry_alloc_offset(inode
));
1274 iinfo
->i_lenAlloc
= newsize
;
1275 truncate_setsize(inode
, newsize
);
1276 up_write(&iinfo
->i_data_sem
);
1279 err
= block_truncate_page(inode
->i_mapping
, newsize
,
1283 truncate_setsize(inode
, newsize
);
1284 down_write(&iinfo
->i_data_sem
);
1285 udf_clear_extent_cache(inode
);
1286 udf_truncate_extents(inode
);
1287 up_write(&iinfo
->i_data_sem
);
1290 inode
->i_mtime
= inode
->i_ctime
= current_fs_time(inode
->i_sb
);
1292 udf_sync_inode(inode
);
1294 mark_inode_dirty(inode
);
1299 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1300 * arbitrary - just that we hopefully don't limit any real use of rewritten
1301 * inode on write-once media but avoid looping for too long on corrupted media.
1303 #define UDF_MAX_ICB_NESTING 1024
1305 static int udf_read_inode(struct inode
*inode
, bool hidden_inode
)
1307 struct buffer_head
*bh
= NULL
;
1308 struct fileEntry
*fe
;
1309 struct extendedFileEntry
*efe
;
1311 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1312 struct udf_sb_info
*sbi
= UDF_SB(inode
->i_sb
);
1313 struct kernel_lb_addr
*iloc
= &iinfo
->i_location
;
1314 unsigned int link_count
;
1315 unsigned int indirections
= 0;
1316 int bs
= inode
->i_sb
->s_blocksize
;
1320 if (iloc
->logicalBlockNum
>=
1321 sbi
->s_partmaps
[iloc
->partitionReferenceNum
].s_partition_len
) {
1322 udf_debug("block=%d, partition=%d out of range\n",
1323 iloc
->logicalBlockNum
, iloc
->partitionReferenceNum
);
1328 * Set defaults, but the inode is still incomplete!
1329 * Note: get_new_inode() sets the following on a new inode:
1332 * i_flags = sb->s_flags
1334 * clean_inode(): zero fills and sets
1339 bh
= udf_read_ptagged(inode
->i_sb
, iloc
, 0, &ident
);
1341 udf_err(inode
->i_sb
, "(ino %ld) failed !bh\n", inode
->i_ino
);
1345 if (ident
!= TAG_IDENT_FE
&& ident
!= TAG_IDENT_EFE
&&
1346 ident
!= TAG_IDENT_USE
) {
1347 udf_err(inode
->i_sb
, "(ino %ld) failed ident=%d\n",
1348 inode
->i_ino
, ident
);
1352 fe
= (struct fileEntry
*)bh
->b_data
;
1353 efe
= (struct extendedFileEntry
*)bh
->b_data
;
1355 if (fe
->icbTag
.strategyType
== cpu_to_le16(4096)) {
1356 struct buffer_head
*ibh
;
1358 ibh
= udf_read_ptagged(inode
->i_sb
, iloc
, 1, &ident
);
1359 if (ident
== TAG_IDENT_IE
&& ibh
) {
1360 struct kernel_lb_addr loc
;
1361 struct indirectEntry
*ie
;
1363 ie
= (struct indirectEntry
*)ibh
->b_data
;
1364 loc
= lelb_to_cpu(ie
->indirectICB
.extLocation
);
1366 if (ie
->indirectICB
.extLength
) {
1368 memcpy(&iinfo
->i_location
, &loc
,
1369 sizeof(struct kernel_lb_addr
));
1370 if (++indirections
> UDF_MAX_ICB_NESTING
) {
1371 udf_err(inode
->i_sb
,
1372 "too many ICBs in ICB hierarchy"
1373 " (max %d supported)\n",
1374 UDF_MAX_ICB_NESTING
);
1382 } else if (fe
->icbTag
.strategyType
!= cpu_to_le16(4)) {
1383 udf_err(inode
->i_sb
, "unsupported strategy type: %d\n",
1384 le16_to_cpu(fe
->icbTag
.strategyType
));
1387 if (fe
->icbTag
.strategyType
== cpu_to_le16(4))
1388 iinfo
->i_strat4096
= 0;
1389 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1390 iinfo
->i_strat4096
= 1;
1392 iinfo
->i_alloc_type
= le16_to_cpu(fe
->icbTag
.flags
) &
1393 ICBTAG_FLAG_AD_MASK
;
1394 if (iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_SHORT
&&
1395 iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_LONG
&&
1396 iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
1400 iinfo
->i_unique
= 0;
1401 iinfo
->i_lenEAttr
= 0;
1402 iinfo
->i_lenExtents
= 0;
1403 iinfo
->i_lenAlloc
= 0;
1404 iinfo
->i_next_alloc_block
= 0;
1405 iinfo
->i_next_alloc_goal
= 0;
1406 if (fe
->descTag
.tagIdent
== cpu_to_le16(TAG_IDENT_EFE
)) {
1409 ret
= udf_alloc_i_data(inode
, bs
-
1410 sizeof(struct extendedFileEntry
));
1413 memcpy(iinfo
->i_ext
.i_data
,
1414 bh
->b_data
+ sizeof(struct extendedFileEntry
),
1415 bs
- sizeof(struct extendedFileEntry
));
1416 } else if (fe
->descTag
.tagIdent
== cpu_to_le16(TAG_IDENT_FE
)) {
1419 ret
= udf_alloc_i_data(inode
, bs
- sizeof(struct fileEntry
));
1422 memcpy(iinfo
->i_ext
.i_data
,
1423 bh
->b_data
+ sizeof(struct fileEntry
),
1424 bs
- sizeof(struct fileEntry
));
1425 } else if (fe
->descTag
.tagIdent
== cpu_to_le16(TAG_IDENT_USE
)) {
1428 iinfo
->i_lenAlloc
= le32_to_cpu(
1429 ((struct unallocSpaceEntry
*)bh
->b_data
)->
1431 ret
= udf_alloc_i_data(inode
, bs
-
1432 sizeof(struct unallocSpaceEntry
));
1435 memcpy(iinfo
->i_ext
.i_data
,
1436 bh
->b_data
+ sizeof(struct unallocSpaceEntry
),
1437 bs
- sizeof(struct unallocSpaceEntry
));
1442 read_lock(&sbi
->s_cred_lock
);
1443 i_uid_write(inode
, le32_to_cpu(fe
->uid
));
1444 if (!uid_valid(inode
->i_uid
) ||
1445 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_UID_IGNORE
) ||
1446 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_UID_SET
))
1447 inode
->i_uid
= UDF_SB(inode
->i_sb
)->s_uid
;
1449 i_gid_write(inode
, le32_to_cpu(fe
->gid
));
1450 if (!gid_valid(inode
->i_gid
) ||
1451 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_GID_IGNORE
) ||
1452 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_GID_SET
))
1453 inode
->i_gid
= UDF_SB(inode
->i_sb
)->s_gid
;
1455 if (fe
->icbTag
.fileType
!= ICBTAG_FILE_TYPE_DIRECTORY
&&
1456 sbi
->s_fmode
!= UDF_INVALID_MODE
)
1457 inode
->i_mode
= sbi
->s_fmode
;
1458 else if (fe
->icbTag
.fileType
== ICBTAG_FILE_TYPE_DIRECTORY
&&
1459 sbi
->s_dmode
!= UDF_INVALID_MODE
)
1460 inode
->i_mode
= sbi
->s_dmode
;
1462 inode
->i_mode
= udf_convert_permissions(fe
);
1463 inode
->i_mode
&= ~sbi
->s_umask
;
1464 read_unlock(&sbi
->s_cred_lock
);
1466 link_count
= le16_to_cpu(fe
->fileLinkCount
);
1468 if (!hidden_inode
) {
1474 set_nlink(inode
, link_count
);
1476 inode
->i_size
= le64_to_cpu(fe
->informationLength
);
1477 iinfo
->i_lenExtents
= inode
->i_size
;
1479 if (iinfo
->i_efe
== 0) {
1480 inode
->i_blocks
= le64_to_cpu(fe
->logicalBlocksRecorded
) <<
1481 (inode
->i_sb
->s_blocksize_bits
- 9);
1483 if (!udf_disk_stamp_to_time(&inode
->i_atime
, fe
->accessTime
))
1484 inode
->i_atime
= sbi
->s_record_time
;
1486 if (!udf_disk_stamp_to_time(&inode
->i_mtime
,
1487 fe
->modificationTime
))
1488 inode
->i_mtime
= sbi
->s_record_time
;
1490 if (!udf_disk_stamp_to_time(&inode
->i_ctime
, fe
->attrTime
))
1491 inode
->i_ctime
= sbi
->s_record_time
;
1493 iinfo
->i_unique
= le64_to_cpu(fe
->uniqueID
);
1494 iinfo
->i_lenEAttr
= le32_to_cpu(fe
->lengthExtendedAttr
);
1495 iinfo
->i_lenAlloc
= le32_to_cpu(fe
->lengthAllocDescs
);
1496 iinfo
->i_checkpoint
= le32_to_cpu(fe
->checkpoint
);
1498 inode
->i_blocks
= le64_to_cpu(efe
->logicalBlocksRecorded
) <<
1499 (inode
->i_sb
->s_blocksize_bits
- 9);
1501 if (!udf_disk_stamp_to_time(&inode
->i_atime
, efe
->accessTime
))
1502 inode
->i_atime
= sbi
->s_record_time
;
1504 if (!udf_disk_stamp_to_time(&inode
->i_mtime
,
1505 efe
->modificationTime
))
1506 inode
->i_mtime
= sbi
->s_record_time
;
1508 if (!udf_disk_stamp_to_time(&iinfo
->i_crtime
, efe
->createTime
))
1509 iinfo
->i_crtime
= sbi
->s_record_time
;
1511 if (!udf_disk_stamp_to_time(&inode
->i_ctime
, efe
->attrTime
))
1512 inode
->i_ctime
= sbi
->s_record_time
;
1514 iinfo
->i_unique
= le64_to_cpu(efe
->uniqueID
);
1515 iinfo
->i_lenEAttr
= le32_to_cpu(efe
->lengthExtendedAttr
);
1516 iinfo
->i_lenAlloc
= le32_to_cpu(efe
->lengthAllocDescs
);
1517 iinfo
->i_checkpoint
= le32_to_cpu(efe
->checkpoint
);
1519 inode
->i_generation
= iinfo
->i_unique
;
1522 * Sanity check length of allocation descriptors and extended attrs to
1523 * avoid integer overflows
1525 if (iinfo
->i_lenEAttr
> bs
|| iinfo
->i_lenAlloc
> bs
)
1527 /* Now do exact checks */
1528 if (udf_file_entry_alloc_offset(inode
) + iinfo
->i_lenAlloc
> bs
)
1530 /* Sanity checks for files in ICB so that we don't get confused later */
1531 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1533 * For file in ICB data is stored in allocation descriptor
1534 * so sizes should match
1536 if (iinfo
->i_lenAlloc
!= inode
->i_size
)
1538 /* File in ICB has to fit in there... */
1539 if (inode
->i_size
> bs
- udf_file_entry_alloc_offset(inode
))
1543 switch (fe
->icbTag
.fileType
) {
1544 case ICBTAG_FILE_TYPE_DIRECTORY
:
1545 inode
->i_op
= &udf_dir_inode_operations
;
1546 inode
->i_fop
= &udf_dir_operations
;
1547 inode
->i_mode
|= S_IFDIR
;
1550 case ICBTAG_FILE_TYPE_REALTIME
:
1551 case ICBTAG_FILE_TYPE_REGULAR
:
1552 case ICBTAG_FILE_TYPE_UNDEF
:
1553 case ICBTAG_FILE_TYPE_VAT20
:
1554 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1555 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
1557 inode
->i_data
.a_ops
= &udf_aops
;
1558 inode
->i_op
= &udf_file_inode_operations
;
1559 inode
->i_fop
= &udf_file_operations
;
1560 inode
->i_mode
|= S_IFREG
;
1562 case ICBTAG_FILE_TYPE_BLOCK
:
1563 inode
->i_mode
|= S_IFBLK
;
1565 case ICBTAG_FILE_TYPE_CHAR
:
1566 inode
->i_mode
|= S_IFCHR
;
1568 case ICBTAG_FILE_TYPE_FIFO
:
1569 init_special_inode(inode
, inode
->i_mode
| S_IFIFO
, 0);
1571 case ICBTAG_FILE_TYPE_SOCKET
:
1572 init_special_inode(inode
, inode
->i_mode
| S_IFSOCK
, 0);
1574 case ICBTAG_FILE_TYPE_SYMLINK
:
1575 inode
->i_data
.a_ops
= &udf_symlink_aops
;
1576 inode
->i_op
= &udf_symlink_inode_operations
;
1577 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
1579 case ICBTAG_FILE_TYPE_MAIN
:
1580 udf_debug("METADATA FILE-----\n");
1582 case ICBTAG_FILE_TYPE_MIRROR
:
1583 udf_debug("METADATA MIRROR FILE-----\n");
1585 case ICBTAG_FILE_TYPE_BITMAP
:
1586 udf_debug("METADATA BITMAP FILE-----\n");
1589 udf_err(inode
->i_sb
, "(ino %ld) failed unknown file type=%d\n",
1590 inode
->i_ino
, fe
->icbTag
.fileType
);
1593 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
1594 struct deviceSpec
*dsea
=
1595 (struct deviceSpec
*)udf_get_extendedattr(inode
, 12, 1);
1597 init_special_inode(inode
, inode
->i_mode
,
1598 MKDEV(le32_to_cpu(dsea
->majorDeviceIdent
),
1599 le32_to_cpu(dsea
->minorDeviceIdent
)));
1600 /* Developer ID ??? */
1610 static int udf_alloc_i_data(struct inode
*inode
, size_t size
)
1612 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1613 iinfo
->i_ext
.i_data
= kmalloc(size
, GFP_KERNEL
);
1615 if (!iinfo
->i_ext
.i_data
) {
1616 udf_err(inode
->i_sb
, "(ino %ld) no free memory\n",
1624 static umode_t
udf_convert_permissions(struct fileEntry
*fe
)
1627 uint32_t permissions
;
1630 permissions
= le32_to_cpu(fe
->permissions
);
1631 flags
= le16_to_cpu(fe
->icbTag
.flags
);
1633 mode
= ((permissions
) & S_IRWXO
) |
1634 ((permissions
>> 2) & S_IRWXG
) |
1635 ((permissions
>> 4) & S_IRWXU
) |
1636 ((flags
& ICBTAG_FLAG_SETUID
) ? S_ISUID
: 0) |
1637 ((flags
& ICBTAG_FLAG_SETGID
) ? S_ISGID
: 0) |
1638 ((flags
& ICBTAG_FLAG_STICKY
) ? S_ISVTX
: 0);
1643 int udf_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1645 return udf_update_inode(inode
, wbc
->sync_mode
== WB_SYNC_ALL
);
1648 static int udf_sync_inode(struct inode
*inode
)
1650 return udf_update_inode(inode
, 1);
1653 static int udf_update_inode(struct inode
*inode
, int do_sync
)
1655 struct buffer_head
*bh
= NULL
;
1656 struct fileEntry
*fe
;
1657 struct extendedFileEntry
*efe
;
1658 uint64_t lb_recorded
;
1663 struct udf_sb_info
*sbi
= UDF_SB(inode
->i_sb
);
1664 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
1665 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1667 bh
= udf_tgetblk(inode
->i_sb
,
1668 udf_get_lb_pblock(inode
->i_sb
, &iinfo
->i_location
, 0));
1670 udf_debug("getblk failure\n");
1675 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
1676 fe
= (struct fileEntry
*)bh
->b_data
;
1677 efe
= (struct extendedFileEntry
*)bh
->b_data
;
1680 struct unallocSpaceEntry
*use
=
1681 (struct unallocSpaceEntry
*)bh
->b_data
;
1683 use
->lengthAllocDescs
= cpu_to_le32(iinfo
->i_lenAlloc
);
1684 memcpy(bh
->b_data
+ sizeof(struct unallocSpaceEntry
),
1685 iinfo
->i_ext
.i_data
, inode
->i_sb
->s_blocksize
-
1686 sizeof(struct unallocSpaceEntry
));
1687 use
->descTag
.tagIdent
= cpu_to_le16(TAG_IDENT_USE
);
1688 crclen
= sizeof(struct unallocSpaceEntry
);
1693 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_UID_FORGET
))
1694 fe
->uid
= cpu_to_le32(-1);
1696 fe
->uid
= cpu_to_le32(i_uid_read(inode
));
1698 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_GID_FORGET
))
1699 fe
->gid
= cpu_to_le32(-1);
1701 fe
->gid
= cpu_to_le32(i_gid_read(inode
));
1703 udfperms
= ((inode
->i_mode
& S_IRWXO
)) |
1704 ((inode
->i_mode
& S_IRWXG
) << 2) |
1705 ((inode
->i_mode
& S_IRWXU
) << 4);
1707 udfperms
|= (le32_to_cpu(fe
->permissions
) &
1708 (FE_PERM_O_DELETE
| FE_PERM_O_CHATTR
|
1709 FE_PERM_G_DELETE
| FE_PERM_G_CHATTR
|
1710 FE_PERM_U_DELETE
| FE_PERM_U_CHATTR
));
1711 fe
->permissions
= cpu_to_le32(udfperms
);
1713 if (S_ISDIR(inode
->i_mode
) && inode
->i_nlink
> 0)
1714 fe
->fileLinkCount
= cpu_to_le16(inode
->i_nlink
- 1);
1716 fe
->fileLinkCount
= cpu_to_le16(inode
->i_nlink
);
1718 fe
->informationLength
= cpu_to_le64(inode
->i_size
);
1720 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
1722 struct deviceSpec
*dsea
=
1723 (struct deviceSpec
*)udf_get_extendedattr(inode
, 12, 1);
1725 dsea
= (struct deviceSpec
*)
1726 udf_add_extendedattr(inode
,
1727 sizeof(struct deviceSpec
) +
1728 sizeof(struct regid
), 12, 0x3);
1729 dsea
->attrType
= cpu_to_le32(12);
1730 dsea
->attrSubtype
= 1;
1731 dsea
->attrLength
= cpu_to_le32(
1732 sizeof(struct deviceSpec
) +
1733 sizeof(struct regid
));
1734 dsea
->impUseLength
= cpu_to_le32(sizeof(struct regid
));
1736 eid
= (struct regid
*)dsea
->impUse
;
1737 memset(eid
, 0, sizeof(struct regid
));
1738 strcpy(eid
->ident
, UDF_ID_DEVELOPER
);
1739 eid
->identSuffix
[0] = UDF_OS_CLASS_UNIX
;
1740 eid
->identSuffix
[1] = UDF_OS_ID_LINUX
;
1741 dsea
->majorDeviceIdent
= cpu_to_le32(imajor(inode
));
1742 dsea
->minorDeviceIdent
= cpu_to_le32(iminor(inode
));
1745 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1746 lb_recorded
= 0; /* No extents => no blocks! */
1749 (inode
->i_blocks
+ (1 << (blocksize_bits
- 9)) - 1) >>
1750 (blocksize_bits
- 9);
1752 if (iinfo
->i_efe
== 0) {
1753 memcpy(bh
->b_data
+ sizeof(struct fileEntry
),
1754 iinfo
->i_ext
.i_data
,
1755 inode
->i_sb
->s_blocksize
- sizeof(struct fileEntry
));
1756 fe
->logicalBlocksRecorded
= cpu_to_le64(lb_recorded
);
1758 udf_time_to_disk_stamp(&fe
->accessTime
, inode
->i_atime
);
1759 udf_time_to_disk_stamp(&fe
->modificationTime
, inode
->i_mtime
);
1760 udf_time_to_disk_stamp(&fe
->attrTime
, inode
->i_ctime
);
1761 memset(&(fe
->impIdent
), 0, sizeof(struct regid
));
1762 strcpy(fe
->impIdent
.ident
, UDF_ID_DEVELOPER
);
1763 fe
->impIdent
.identSuffix
[0] = UDF_OS_CLASS_UNIX
;
1764 fe
->impIdent
.identSuffix
[1] = UDF_OS_ID_LINUX
;
1765 fe
->uniqueID
= cpu_to_le64(iinfo
->i_unique
);
1766 fe
->lengthExtendedAttr
= cpu_to_le32(iinfo
->i_lenEAttr
);
1767 fe
->lengthAllocDescs
= cpu_to_le32(iinfo
->i_lenAlloc
);
1768 fe
->checkpoint
= cpu_to_le32(iinfo
->i_checkpoint
);
1769 fe
->descTag
.tagIdent
= cpu_to_le16(TAG_IDENT_FE
);
1770 crclen
= sizeof(struct fileEntry
);
1772 memcpy(bh
->b_data
+ sizeof(struct extendedFileEntry
),
1773 iinfo
->i_ext
.i_data
,
1774 inode
->i_sb
->s_blocksize
-
1775 sizeof(struct extendedFileEntry
));
1776 efe
->objectSize
= cpu_to_le64(inode
->i_size
);
1777 efe
->logicalBlocksRecorded
= cpu_to_le64(lb_recorded
);
1779 if (iinfo
->i_crtime
.tv_sec
> inode
->i_atime
.tv_sec
||
1780 (iinfo
->i_crtime
.tv_sec
== inode
->i_atime
.tv_sec
&&
1781 iinfo
->i_crtime
.tv_nsec
> inode
->i_atime
.tv_nsec
))
1782 iinfo
->i_crtime
= inode
->i_atime
;
1784 if (iinfo
->i_crtime
.tv_sec
> inode
->i_mtime
.tv_sec
||
1785 (iinfo
->i_crtime
.tv_sec
== inode
->i_mtime
.tv_sec
&&
1786 iinfo
->i_crtime
.tv_nsec
> inode
->i_mtime
.tv_nsec
))
1787 iinfo
->i_crtime
= inode
->i_mtime
;
1789 if (iinfo
->i_crtime
.tv_sec
> inode
->i_ctime
.tv_sec
||
1790 (iinfo
->i_crtime
.tv_sec
== inode
->i_ctime
.tv_sec
&&
1791 iinfo
->i_crtime
.tv_nsec
> inode
->i_ctime
.tv_nsec
))
1792 iinfo
->i_crtime
= inode
->i_ctime
;
1794 udf_time_to_disk_stamp(&efe
->accessTime
, inode
->i_atime
);
1795 udf_time_to_disk_stamp(&efe
->modificationTime
, inode
->i_mtime
);
1796 udf_time_to_disk_stamp(&efe
->createTime
, iinfo
->i_crtime
);
1797 udf_time_to_disk_stamp(&efe
->attrTime
, inode
->i_ctime
);
1799 memset(&(efe
->impIdent
), 0, sizeof(struct regid
));
1800 strcpy(efe
->impIdent
.ident
, UDF_ID_DEVELOPER
);
1801 efe
->impIdent
.identSuffix
[0] = UDF_OS_CLASS_UNIX
;
1802 efe
->impIdent
.identSuffix
[1] = UDF_OS_ID_LINUX
;
1803 efe
->uniqueID
= cpu_to_le64(iinfo
->i_unique
);
1804 efe
->lengthExtendedAttr
= cpu_to_le32(iinfo
->i_lenEAttr
);
1805 efe
->lengthAllocDescs
= cpu_to_le32(iinfo
->i_lenAlloc
);
1806 efe
->checkpoint
= cpu_to_le32(iinfo
->i_checkpoint
);
1807 efe
->descTag
.tagIdent
= cpu_to_le16(TAG_IDENT_EFE
);
1808 crclen
= sizeof(struct extendedFileEntry
);
1812 if (iinfo
->i_strat4096
) {
1813 fe
->icbTag
.strategyType
= cpu_to_le16(4096);
1814 fe
->icbTag
.strategyParameter
= cpu_to_le16(1);
1815 fe
->icbTag
.numEntries
= cpu_to_le16(2);
1817 fe
->icbTag
.strategyType
= cpu_to_le16(4);
1818 fe
->icbTag
.numEntries
= cpu_to_le16(1);
1822 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_USE
;
1823 else if (S_ISDIR(inode
->i_mode
))
1824 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_DIRECTORY
;
1825 else if (S_ISREG(inode
->i_mode
))
1826 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_REGULAR
;
1827 else if (S_ISLNK(inode
->i_mode
))
1828 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_SYMLINK
;
1829 else if (S_ISBLK(inode
->i_mode
))
1830 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_BLOCK
;
1831 else if (S_ISCHR(inode
->i_mode
))
1832 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_CHAR
;
1833 else if (S_ISFIFO(inode
->i_mode
))
1834 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_FIFO
;
1835 else if (S_ISSOCK(inode
->i_mode
))
1836 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_SOCKET
;
1838 icbflags
= iinfo
->i_alloc_type
|
1839 ((inode
->i_mode
& S_ISUID
) ? ICBTAG_FLAG_SETUID
: 0) |
1840 ((inode
->i_mode
& S_ISGID
) ? ICBTAG_FLAG_SETGID
: 0) |
1841 ((inode
->i_mode
& S_ISVTX
) ? ICBTAG_FLAG_STICKY
: 0) |
1842 (le16_to_cpu(fe
->icbTag
.flags
) &
1843 ~(ICBTAG_FLAG_AD_MASK
| ICBTAG_FLAG_SETUID
|
1844 ICBTAG_FLAG_SETGID
| ICBTAG_FLAG_STICKY
));
1846 fe
->icbTag
.flags
= cpu_to_le16(icbflags
);
1847 if (sbi
->s_udfrev
>= 0x0200)
1848 fe
->descTag
.descVersion
= cpu_to_le16(3);
1850 fe
->descTag
.descVersion
= cpu_to_le16(2);
1851 fe
->descTag
.tagSerialNum
= cpu_to_le16(sbi
->s_serial_number
);
1852 fe
->descTag
.tagLocation
= cpu_to_le32(
1853 iinfo
->i_location
.logicalBlockNum
);
1854 crclen
+= iinfo
->i_lenEAttr
+ iinfo
->i_lenAlloc
- sizeof(struct tag
);
1855 fe
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
1856 fe
->descTag
.descCRC
= cpu_to_le16(crc_itu_t(0, (char *)fe
+ sizeof(struct tag
),
1858 fe
->descTag
.tagChecksum
= udf_tag_checksum(&fe
->descTag
);
1860 set_buffer_uptodate(bh
);
1863 /* write the data blocks */
1864 mark_buffer_dirty(bh
);
1866 sync_dirty_buffer(bh
);
1867 if (buffer_write_io_error(bh
)) {
1868 udf_warn(inode
->i_sb
, "IO error syncing udf inode [%08lx]\n",
1878 struct inode
*__udf_iget(struct super_block
*sb
, struct kernel_lb_addr
*ino
,
1881 unsigned long block
= udf_get_lb_pblock(sb
, ino
, 0);
1882 struct inode
*inode
= iget_locked(sb
, block
);
1886 return ERR_PTR(-ENOMEM
);
1888 if (!(inode
->i_state
& I_NEW
))
1891 memcpy(&UDF_I(inode
)->i_location
, ino
, sizeof(struct kernel_lb_addr
));
1892 err
= udf_read_inode(inode
, hidden_inode
);
1895 return ERR_PTR(err
);
1897 unlock_new_inode(inode
);
1902 int udf_add_aext(struct inode
*inode
, struct extent_position
*epos
,
1903 struct kernel_lb_addr
*eloc
, uint32_t elen
, int inc
)
1906 struct short_ad
*sad
= NULL
;
1907 struct long_ad
*lad
= NULL
;
1908 struct allocExtDesc
*aed
;
1910 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1913 ptr
= iinfo
->i_ext
.i_data
+ epos
->offset
-
1914 udf_file_entry_alloc_offset(inode
) +
1917 ptr
= epos
->bh
->b_data
+ epos
->offset
;
1919 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
1920 adsize
= sizeof(struct short_ad
);
1921 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
1922 adsize
= sizeof(struct long_ad
);
1926 if (epos
->offset
+ (2 * adsize
) > inode
->i_sb
->s_blocksize
) {
1927 unsigned char *sptr
, *dptr
;
1928 struct buffer_head
*nbh
;
1930 struct kernel_lb_addr obloc
= epos
->block
;
1932 epos
->block
.logicalBlockNum
= udf_new_block(inode
->i_sb
, NULL
,
1933 obloc
.partitionReferenceNum
,
1934 obloc
.logicalBlockNum
, &err
);
1935 if (!epos
->block
.logicalBlockNum
)
1937 nbh
= udf_tgetblk(inode
->i_sb
, udf_get_lb_pblock(inode
->i_sb
,
1943 memset(nbh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
1944 set_buffer_uptodate(nbh
);
1946 mark_buffer_dirty_inode(nbh
, inode
);
1948 aed
= (struct allocExtDesc
*)(nbh
->b_data
);
1949 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
))
1950 aed
->previousAllocExtLocation
=
1951 cpu_to_le32(obloc
.logicalBlockNum
);
1952 if (epos
->offset
+ adsize
> inode
->i_sb
->s_blocksize
) {
1953 loffset
= epos
->offset
;
1954 aed
->lengthAllocDescs
= cpu_to_le32(adsize
);
1955 sptr
= ptr
- adsize
;
1956 dptr
= nbh
->b_data
+ sizeof(struct allocExtDesc
);
1957 memcpy(dptr
, sptr
, adsize
);
1958 epos
->offset
= sizeof(struct allocExtDesc
) + adsize
;
1960 loffset
= epos
->offset
+ adsize
;
1961 aed
->lengthAllocDescs
= cpu_to_le32(0);
1963 epos
->offset
= sizeof(struct allocExtDesc
);
1966 aed
= (struct allocExtDesc
*)epos
->bh
->b_data
;
1967 le32_add_cpu(&aed
->lengthAllocDescs
, adsize
);
1969 iinfo
->i_lenAlloc
+= adsize
;
1970 mark_inode_dirty(inode
);
1973 if (UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0200)
1974 udf_new_tag(nbh
->b_data
, TAG_IDENT_AED
, 3, 1,
1975 epos
->block
.logicalBlockNum
, sizeof(struct tag
));
1977 udf_new_tag(nbh
->b_data
, TAG_IDENT_AED
, 2, 1,
1978 epos
->block
.logicalBlockNum
, sizeof(struct tag
));
1979 switch (iinfo
->i_alloc_type
) {
1980 case ICBTAG_FLAG_AD_SHORT
:
1981 sad
= (struct short_ad
*)sptr
;
1982 sad
->extLength
= cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS
|
1983 inode
->i_sb
->s_blocksize
);
1985 cpu_to_le32(epos
->block
.logicalBlockNum
);
1987 case ICBTAG_FLAG_AD_LONG
:
1988 lad
= (struct long_ad
*)sptr
;
1989 lad
->extLength
= cpu_to_le32(EXT_NEXT_EXTENT_ALLOCDECS
|
1990 inode
->i_sb
->s_blocksize
);
1991 lad
->extLocation
= cpu_to_lelb(epos
->block
);
1992 memset(lad
->impUse
, 0x00, sizeof(lad
->impUse
));
1996 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
1997 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
1998 udf_update_tag(epos
->bh
->b_data
, loffset
);
2000 udf_update_tag(epos
->bh
->b_data
,
2001 sizeof(struct allocExtDesc
));
2002 mark_buffer_dirty_inode(epos
->bh
, inode
);
2005 mark_inode_dirty(inode
);
2010 udf_write_aext(inode
, epos
, eloc
, elen
, inc
);
2013 iinfo
->i_lenAlloc
+= adsize
;
2014 mark_inode_dirty(inode
);
2016 aed
= (struct allocExtDesc
*)epos
->bh
->b_data
;
2017 le32_add_cpu(&aed
->lengthAllocDescs
, adsize
);
2018 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2019 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
2020 udf_update_tag(epos
->bh
->b_data
,
2021 epos
->offset
+ (inc
? 0 : adsize
));
2023 udf_update_tag(epos
->bh
->b_data
,
2024 sizeof(struct allocExtDesc
));
2025 mark_buffer_dirty_inode(epos
->bh
, inode
);
2031 void udf_write_aext(struct inode
*inode
, struct extent_position
*epos
,
2032 struct kernel_lb_addr
*eloc
, uint32_t elen
, int inc
)
2036 struct short_ad
*sad
;
2037 struct long_ad
*lad
;
2038 struct udf_inode_info
*iinfo
= UDF_I(inode
);
2041 ptr
= iinfo
->i_ext
.i_data
+ epos
->offset
-
2042 udf_file_entry_alloc_offset(inode
) +
2045 ptr
= epos
->bh
->b_data
+ epos
->offset
;
2047 switch (iinfo
->i_alloc_type
) {
2048 case ICBTAG_FLAG_AD_SHORT
:
2049 sad
= (struct short_ad
*)ptr
;
2050 sad
->extLength
= cpu_to_le32(elen
);
2051 sad
->extPosition
= cpu_to_le32(eloc
->logicalBlockNum
);
2052 adsize
= sizeof(struct short_ad
);
2054 case ICBTAG_FLAG_AD_LONG
:
2055 lad
= (struct long_ad
*)ptr
;
2056 lad
->extLength
= cpu_to_le32(elen
);
2057 lad
->extLocation
= cpu_to_lelb(*eloc
);
2058 memset(lad
->impUse
, 0x00, sizeof(lad
->impUse
));
2059 adsize
= sizeof(struct long_ad
);
2066 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2067 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201) {
2068 struct allocExtDesc
*aed
=
2069 (struct allocExtDesc
*)epos
->bh
->b_data
;
2070 udf_update_tag(epos
->bh
->b_data
,
2071 le32_to_cpu(aed
->lengthAllocDescs
) +
2072 sizeof(struct allocExtDesc
));
2074 mark_buffer_dirty_inode(epos
->bh
, inode
);
2076 mark_inode_dirty(inode
);
2080 epos
->offset
+= adsize
;
2084 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2085 * someone does some weird stuff.
2087 #define UDF_MAX_INDIR_EXTS 16
2089 int8_t udf_next_aext(struct inode
*inode
, struct extent_position
*epos
,
2090 struct kernel_lb_addr
*eloc
, uint32_t *elen
, int inc
)
2093 unsigned int indirections
= 0;
2095 while ((etype
= udf_current_aext(inode
, epos
, eloc
, elen
, inc
)) ==
2096 (EXT_NEXT_EXTENT_ALLOCDECS
>> 30)) {
2099 if (++indirections
> UDF_MAX_INDIR_EXTS
) {
2100 udf_err(inode
->i_sb
,
2101 "too many indirect extents in inode %lu\n",
2106 epos
->block
= *eloc
;
2107 epos
->offset
= sizeof(struct allocExtDesc
);
2109 block
= udf_get_lb_pblock(inode
->i_sb
, &epos
->block
, 0);
2110 epos
->bh
= udf_tread(inode
->i_sb
, block
);
2112 udf_debug("reading block %d failed!\n", block
);
2120 int8_t udf_current_aext(struct inode
*inode
, struct extent_position
*epos
,
2121 struct kernel_lb_addr
*eloc
, uint32_t *elen
, int inc
)
2126 struct short_ad
*sad
;
2127 struct long_ad
*lad
;
2128 struct udf_inode_info
*iinfo
= UDF_I(inode
);
2132 epos
->offset
= udf_file_entry_alloc_offset(inode
);
2133 ptr
= iinfo
->i_ext
.i_data
+ epos
->offset
-
2134 udf_file_entry_alloc_offset(inode
) +
2136 alen
= udf_file_entry_alloc_offset(inode
) +
2140 epos
->offset
= sizeof(struct allocExtDesc
);
2141 ptr
= epos
->bh
->b_data
+ epos
->offset
;
2142 alen
= sizeof(struct allocExtDesc
) +
2143 le32_to_cpu(((struct allocExtDesc
*)epos
->bh
->b_data
)->
2147 switch (iinfo
->i_alloc_type
) {
2148 case ICBTAG_FLAG_AD_SHORT
:
2149 sad
= udf_get_fileshortad(ptr
, alen
, &epos
->offset
, inc
);
2152 etype
= le32_to_cpu(sad
->extLength
) >> 30;
2153 eloc
->logicalBlockNum
= le32_to_cpu(sad
->extPosition
);
2154 eloc
->partitionReferenceNum
=
2155 iinfo
->i_location
.partitionReferenceNum
;
2156 *elen
= le32_to_cpu(sad
->extLength
) & UDF_EXTENT_LENGTH_MASK
;
2158 case ICBTAG_FLAG_AD_LONG
:
2159 lad
= udf_get_filelongad(ptr
, alen
, &epos
->offset
, inc
);
2162 etype
= le32_to_cpu(lad
->extLength
) >> 30;
2163 *eloc
= lelb_to_cpu(lad
->extLocation
);
2164 *elen
= le32_to_cpu(lad
->extLength
) & UDF_EXTENT_LENGTH_MASK
;
2167 udf_debug("alloc_type = %d unsupported\n", iinfo
->i_alloc_type
);
2174 static int8_t udf_insert_aext(struct inode
*inode
, struct extent_position epos
,
2175 struct kernel_lb_addr neloc
, uint32_t nelen
)
2177 struct kernel_lb_addr oeloc
;
2184 while ((etype
= udf_next_aext(inode
, &epos
, &oeloc
, &oelen
, 0)) != -1) {
2185 udf_write_aext(inode
, &epos
, &neloc
, nelen
, 1);
2187 nelen
= (etype
<< 30) | oelen
;
2189 udf_add_aext(inode
, &epos
, &neloc
, nelen
, 1);
2192 return (nelen
>> 30);
2195 int8_t udf_delete_aext(struct inode
*inode
, struct extent_position epos
,
2196 struct kernel_lb_addr eloc
, uint32_t elen
)
2198 struct extent_position oepos
;
2201 struct allocExtDesc
*aed
;
2202 struct udf_inode_info
*iinfo
;
2209 iinfo
= UDF_I(inode
);
2210 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
2211 adsize
= sizeof(struct short_ad
);
2212 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
2213 adsize
= sizeof(struct long_ad
);
2218 if (udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1) == -1)
2221 while ((etype
= udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1)) != -1) {
2222 udf_write_aext(inode
, &oepos
, &eloc
, (etype
<< 30) | elen
, 1);
2223 if (oepos
.bh
!= epos
.bh
) {
2224 oepos
.block
= epos
.block
;
2228 oepos
.offset
= epos
.offset
- adsize
;
2231 memset(&eloc
, 0x00, sizeof(struct kernel_lb_addr
));
2234 if (epos
.bh
!= oepos
.bh
) {
2235 udf_free_blocks(inode
->i_sb
, inode
, &epos
.block
, 0, 1);
2236 udf_write_aext(inode
, &oepos
, &eloc
, elen
, 1);
2237 udf_write_aext(inode
, &oepos
, &eloc
, elen
, 1);
2239 iinfo
->i_lenAlloc
-= (adsize
* 2);
2240 mark_inode_dirty(inode
);
2242 aed
= (struct allocExtDesc
*)oepos
.bh
->b_data
;
2243 le32_add_cpu(&aed
->lengthAllocDescs
, -(2 * adsize
));
2244 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2245 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
2246 udf_update_tag(oepos
.bh
->b_data
,
2247 oepos
.offset
- (2 * adsize
));
2249 udf_update_tag(oepos
.bh
->b_data
,
2250 sizeof(struct allocExtDesc
));
2251 mark_buffer_dirty_inode(oepos
.bh
, inode
);
2254 udf_write_aext(inode
, &oepos
, &eloc
, elen
, 1);
2256 iinfo
->i_lenAlloc
-= adsize
;
2257 mark_inode_dirty(inode
);
2259 aed
= (struct allocExtDesc
*)oepos
.bh
->b_data
;
2260 le32_add_cpu(&aed
->lengthAllocDescs
, -adsize
);
2261 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2262 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
2263 udf_update_tag(oepos
.bh
->b_data
,
2264 epos
.offset
- adsize
);
2266 udf_update_tag(oepos
.bh
->b_data
,
2267 sizeof(struct allocExtDesc
));
2268 mark_buffer_dirty_inode(oepos
.bh
, inode
);
2275 return (elen
>> 30);
2278 int8_t inode_bmap(struct inode
*inode
, sector_t block
,
2279 struct extent_position
*pos
, struct kernel_lb_addr
*eloc
,
2280 uint32_t *elen
, sector_t
*offset
)
2282 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
2283 loff_t lbcount
= 0, bcount
=
2284 (loff_t
) block
<< blocksize_bits
;
2286 struct udf_inode_info
*iinfo
;
2288 iinfo
= UDF_I(inode
);
2289 if (!udf_read_extent_cache(inode
, bcount
, &lbcount
, pos
)) {
2291 pos
->block
= iinfo
->i_location
;
2296 etype
= udf_next_aext(inode
, pos
, eloc
, elen
, 1);
2298 *offset
= (bcount
- lbcount
) >> blocksize_bits
;
2299 iinfo
->i_lenExtents
= lbcount
;
2303 } while (lbcount
<= bcount
);
2304 /* update extent cache */
2305 udf_update_extent_cache(inode
, lbcount
- *elen
, pos
, 1);
2306 *offset
= (bcount
+ *elen
- lbcount
) >> blocksize_bits
;
2311 long udf_block_map(struct inode
*inode
, sector_t block
)
2313 struct kernel_lb_addr eloc
;
2316 struct extent_position epos
= {};
2319 down_read(&UDF_I(inode
)->i_data_sem
);
2321 if (inode_bmap(inode
, block
, &epos
, &eloc
, &elen
, &offset
) ==
2322 (EXT_RECORDED_ALLOCATED
>> 30))
2323 ret
= udf_get_lb_pblock(inode
->i_sb
, &eloc
, offset
);
2327 up_read(&UDF_I(inode
)->i_data_sem
);
2330 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_VARCONV
))
2331 return udf_fixed_to_variable(ret
);