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
)
219 struct file
*file
= iocb
->ki_filp
;
220 struct address_space
*mapping
= file
->f_mapping
;
221 struct inode
*inode
= mapping
->host
;
222 size_t count
= iov_iter_count(iter
);
225 ret
= blockdev_direct_IO(iocb
, inode
, iter
, udf_get_block
);
226 if (unlikely(ret
< 0 && iov_iter_rw(iter
) == WRITE
))
227 udf_write_failed(mapping
, iocb
->ki_pos
+ count
);
231 static sector_t
udf_bmap(struct address_space
*mapping
, sector_t block
)
233 return generic_block_bmap(mapping
, block
, udf_get_block
);
236 const struct address_space_operations udf_aops
= {
237 .readpage
= udf_readpage
,
238 .readpages
= udf_readpages
,
239 .writepage
= udf_writepage
,
240 .writepages
= udf_writepages
,
241 .write_begin
= udf_write_begin
,
242 .write_end
= generic_write_end
,
243 .direct_IO
= udf_direct_IO
,
248 * Expand file stored in ICB to a normal one-block-file
250 * This function requires i_data_sem for writing and releases it.
251 * This function requires i_mutex held
253 int udf_expand_file_adinicb(struct inode
*inode
)
257 struct udf_inode_info
*iinfo
= UDF_I(inode
);
259 struct writeback_control udf_wbc
= {
260 .sync_mode
= WB_SYNC_NONE
,
264 WARN_ON_ONCE(!inode_is_locked(inode
));
265 if (!iinfo
->i_lenAlloc
) {
266 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
267 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_SHORT
;
269 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_LONG
;
270 /* from now on we have normal address_space methods */
271 inode
->i_data
.a_ops
= &udf_aops
;
272 up_write(&iinfo
->i_data_sem
);
273 mark_inode_dirty(inode
);
277 * Release i_data_sem so that we can lock a page - page lock ranks
278 * above i_data_sem. i_mutex still protects us against file changes.
280 up_write(&iinfo
->i_data_sem
);
282 page
= find_or_create_page(inode
->i_mapping
, 0, GFP_NOFS
);
286 if (!PageUptodate(page
)) {
288 memset(kaddr
+ iinfo
->i_lenAlloc
, 0x00,
289 PAGE_SIZE
- iinfo
->i_lenAlloc
);
290 memcpy(kaddr
, iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
,
292 flush_dcache_page(page
);
293 SetPageUptodate(page
);
296 down_write(&iinfo
->i_data_sem
);
297 memset(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
, 0x00,
299 iinfo
->i_lenAlloc
= 0;
300 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
301 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_SHORT
;
303 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_LONG
;
304 /* from now on we have normal address_space methods */
305 inode
->i_data
.a_ops
= &udf_aops
;
306 up_write(&iinfo
->i_data_sem
);
307 err
= inode
->i_data
.a_ops
->writepage(page
, &udf_wbc
);
309 /* Restore everything back so that we don't lose data... */
312 down_write(&iinfo
->i_data_sem
);
313 memcpy(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
, kaddr
,
317 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_IN_ICB
;
318 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
319 up_write(&iinfo
->i_data_sem
);
322 mark_inode_dirty(inode
);
327 struct buffer_head
*udf_expand_dir_adinicb(struct inode
*inode
, int *block
,
331 struct buffer_head
*dbh
= NULL
;
332 struct kernel_lb_addr eloc
;
334 struct extent_position epos
;
336 struct udf_fileident_bh sfibh
, dfibh
;
337 loff_t f_pos
= udf_ext0_offset(inode
);
338 int size
= udf_ext0_offset(inode
) + inode
->i_size
;
339 struct fileIdentDesc cfi
, *sfi
, *dfi
;
340 struct udf_inode_info
*iinfo
= UDF_I(inode
);
342 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_USE_SHORT_AD
))
343 alloctype
= ICBTAG_FLAG_AD_SHORT
;
345 alloctype
= ICBTAG_FLAG_AD_LONG
;
347 if (!inode
->i_size
) {
348 iinfo
->i_alloc_type
= alloctype
;
349 mark_inode_dirty(inode
);
353 /* alloc block, and copy data to it */
354 *block
= udf_new_block(inode
->i_sb
, inode
,
355 iinfo
->i_location
.partitionReferenceNum
,
356 iinfo
->i_location
.logicalBlockNum
, err
);
359 newblock
= udf_get_pblock(inode
->i_sb
, *block
,
360 iinfo
->i_location
.partitionReferenceNum
,
364 dbh
= udf_tgetblk(inode
->i_sb
, newblock
);
368 memset(dbh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
369 set_buffer_uptodate(dbh
);
371 mark_buffer_dirty_inode(dbh
, inode
);
373 sfibh
.soffset
= sfibh
.eoffset
=
374 f_pos
& (inode
->i_sb
->s_blocksize
- 1);
375 sfibh
.sbh
= sfibh
.ebh
= NULL
;
376 dfibh
.soffset
= dfibh
.eoffset
= 0;
377 dfibh
.sbh
= dfibh
.ebh
= dbh
;
378 while (f_pos
< size
) {
379 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_IN_ICB
;
380 sfi
= udf_fileident_read(inode
, &f_pos
, &sfibh
, &cfi
, NULL
,
386 iinfo
->i_alloc_type
= alloctype
;
387 sfi
->descTag
.tagLocation
= cpu_to_le32(*block
);
388 dfibh
.soffset
= dfibh
.eoffset
;
389 dfibh
.eoffset
+= (sfibh
.eoffset
- sfibh
.soffset
);
390 dfi
= (struct fileIdentDesc
*)(dbh
->b_data
+ dfibh
.soffset
);
391 if (udf_write_fi(inode
, sfi
, dfi
, &dfibh
, sfi
->impUse
,
393 le16_to_cpu(sfi
->lengthOfImpUse
))) {
394 iinfo
->i_alloc_type
= ICBTAG_FLAG_AD_IN_ICB
;
399 mark_buffer_dirty_inode(dbh
, inode
);
401 memset(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
, 0,
403 iinfo
->i_lenAlloc
= 0;
404 eloc
.logicalBlockNum
= *block
;
405 eloc
.partitionReferenceNum
=
406 iinfo
->i_location
.partitionReferenceNum
;
407 iinfo
->i_lenExtents
= inode
->i_size
;
409 epos
.block
= iinfo
->i_location
;
410 epos
.offset
= udf_file_entry_alloc_offset(inode
);
411 udf_add_aext(inode
, &epos
, &eloc
, inode
->i_size
, 0);
415 mark_inode_dirty(inode
);
419 static int udf_get_block(struct inode
*inode
, sector_t block
,
420 struct buffer_head
*bh_result
, int create
)
424 struct udf_inode_info
*iinfo
;
427 phys
= udf_block_map(inode
, block
);
429 map_bh(bh_result
, inode
->i_sb
, phys
);
435 iinfo
= UDF_I(inode
);
437 down_write(&iinfo
->i_data_sem
);
438 if (block
== iinfo
->i_next_alloc_block
+ 1) {
439 iinfo
->i_next_alloc_block
++;
440 iinfo
->i_next_alloc_goal
++;
443 udf_clear_extent_cache(inode
);
444 phys
= inode_getblk(inode
, block
, &err
, &new);
449 set_buffer_new(bh_result
);
450 map_bh(bh_result
, inode
->i_sb
, phys
);
453 up_write(&iinfo
->i_data_sem
);
457 static struct buffer_head
*udf_getblk(struct inode
*inode
, long block
,
458 int create
, int *err
)
460 struct buffer_head
*bh
;
461 struct buffer_head dummy
;
464 dummy
.b_blocknr
= -1000;
465 *err
= udf_get_block(inode
, block
, &dummy
, create
);
466 if (!*err
&& buffer_mapped(&dummy
)) {
467 bh
= sb_getblk(inode
->i_sb
, dummy
.b_blocknr
);
468 if (buffer_new(&dummy
)) {
470 memset(bh
->b_data
, 0x00, inode
->i_sb
->s_blocksize
);
471 set_buffer_uptodate(bh
);
473 mark_buffer_dirty_inode(bh
, inode
);
481 /* Extend the file with new blocks totaling 'new_block_bytes',
482 * return the number of extents added
484 static int udf_do_extend_file(struct inode
*inode
,
485 struct extent_position
*last_pos
,
486 struct kernel_long_ad
*last_ext
,
487 loff_t new_block_bytes
)
490 int count
= 0, fake
= !(last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
);
491 struct super_block
*sb
= inode
->i_sb
;
492 struct kernel_lb_addr prealloc_loc
= {};
493 int prealloc_len
= 0;
494 struct udf_inode_info
*iinfo
;
497 /* The previous extent is fake and we should not extend by anything
498 * - there's nothing to do... */
499 if (!new_block_bytes
&& fake
)
502 iinfo
= UDF_I(inode
);
503 /* Round the last extent up to a multiple of block size */
504 if (last_ext
->extLength
& (sb
->s_blocksize
- 1)) {
505 last_ext
->extLength
=
506 (last_ext
->extLength
& UDF_EXTENT_FLAG_MASK
) |
507 (((last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
508 sb
->s_blocksize
- 1) & ~(sb
->s_blocksize
- 1));
509 iinfo
->i_lenExtents
=
510 (iinfo
->i_lenExtents
+ sb
->s_blocksize
- 1) &
511 ~(sb
->s_blocksize
- 1);
514 /* Last extent are just preallocated blocks? */
515 if ((last_ext
->extLength
& UDF_EXTENT_FLAG_MASK
) ==
516 EXT_NOT_RECORDED_ALLOCATED
) {
517 /* Save the extent so that we can reattach it to the end */
518 prealloc_loc
= last_ext
->extLocation
;
519 prealloc_len
= last_ext
->extLength
;
520 /* Mark the extent as a hole */
521 last_ext
->extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
|
522 (last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
);
523 last_ext
->extLocation
.logicalBlockNum
= 0;
524 last_ext
->extLocation
.partitionReferenceNum
= 0;
527 /* Can we merge with the previous extent? */
528 if ((last_ext
->extLength
& UDF_EXTENT_FLAG_MASK
) ==
529 EXT_NOT_RECORDED_NOT_ALLOCATED
) {
530 add
= (1 << 30) - sb
->s_blocksize
-
531 (last_ext
->extLength
& UDF_EXTENT_LENGTH_MASK
);
532 if (add
> new_block_bytes
)
533 add
= new_block_bytes
;
534 new_block_bytes
-= add
;
535 last_ext
->extLength
+= add
;
539 udf_add_aext(inode
, last_pos
, &last_ext
->extLocation
,
540 last_ext
->extLength
, 1);
543 struct kernel_lb_addr tmploc
;
546 udf_write_aext(inode
, last_pos
, &last_ext
->extLocation
,
547 last_ext
->extLength
, 1);
549 * We've rewritten the last extent but there may be empty
550 * indirect extent after it - enter it.
552 udf_next_aext(inode
, last_pos
, &tmploc
, &tmplen
, 0);
555 /* Managed to do everything necessary? */
556 if (!new_block_bytes
)
559 /* All further extents will be NOT_RECORDED_NOT_ALLOCATED */
560 last_ext
->extLocation
.logicalBlockNum
= 0;
561 last_ext
->extLocation
.partitionReferenceNum
= 0;
562 add
= (1 << 30) - sb
->s_blocksize
;
563 last_ext
->extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
| add
;
565 /* Create enough extents to cover the whole hole */
566 while (new_block_bytes
> add
) {
567 new_block_bytes
-= add
;
568 err
= udf_add_aext(inode
, last_pos
, &last_ext
->extLocation
,
569 last_ext
->extLength
, 1);
574 if (new_block_bytes
) {
575 last_ext
->extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
|
577 err
= udf_add_aext(inode
, last_pos
, &last_ext
->extLocation
,
578 last_ext
->extLength
, 1);
585 /* Do we have some preallocated blocks saved? */
587 err
= udf_add_aext(inode
, last_pos
, &prealloc_loc
,
591 last_ext
->extLocation
= prealloc_loc
;
592 last_ext
->extLength
= prealloc_len
;
596 /* last_pos should point to the last written extent... */
597 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
598 last_pos
->offset
-= sizeof(struct short_ad
);
599 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
600 last_pos
->offset
-= sizeof(struct long_ad
);
607 /* Extend the final block of the file to final_block_len bytes */
608 static void udf_do_extend_final_block(struct inode
*inode
,
609 struct extent_position
*last_pos
,
610 struct kernel_long_ad
*last_ext
,
611 uint32_t final_block_len
)
613 struct super_block
*sb
= inode
->i_sb
;
614 uint32_t added_bytes
;
616 added_bytes
= final_block_len
-
617 (last_ext
->extLength
& (sb
->s_blocksize
- 1));
618 last_ext
->extLength
+= added_bytes
;
619 UDF_I(inode
)->i_lenExtents
+= added_bytes
;
621 udf_write_aext(inode
, last_pos
, &last_ext
->extLocation
,
622 last_ext
->extLength
, 1);
625 static int udf_extend_file(struct inode
*inode
, loff_t newsize
)
628 struct extent_position epos
;
629 struct kernel_lb_addr eloc
;
632 struct super_block
*sb
= inode
->i_sb
;
633 sector_t first_block
= newsize
>> sb
->s_blocksize_bits
, offset
;
634 unsigned long partial_final_block
;
636 struct udf_inode_info
*iinfo
= UDF_I(inode
);
637 struct kernel_long_ad extent
;
639 int within_final_block
;
641 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
642 adsize
= sizeof(struct short_ad
);
643 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
644 adsize
= sizeof(struct long_ad
);
648 etype
= inode_bmap(inode
, first_block
, &epos
, &eloc
, &elen
, &offset
);
649 within_final_block
= (etype
!= -1);
651 if ((!epos
.bh
&& epos
.offset
== udf_file_entry_alloc_offset(inode
)) ||
652 (epos
.bh
&& epos
.offset
== sizeof(struct allocExtDesc
))) {
653 /* File has no extents at all or has empty last
654 * indirect extent! Create a fake extent... */
655 extent
.extLocation
.logicalBlockNum
= 0;
656 extent
.extLocation
.partitionReferenceNum
= 0;
657 extent
.extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
;
659 epos
.offset
-= adsize
;
660 etype
= udf_next_aext(inode
, &epos
, &extent
.extLocation
,
661 &extent
.extLength
, 0);
662 extent
.extLength
|= etype
<< 30;
665 partial_final_block
= newsize
& (sb
->s_blocksize
- 1);
667 /* File has extent covering the new size (could happen when extending
670 if (within_final_block
) {
671 /* Extending file within the last file block */
672 udf_do_extend_final_block(inode
, &epos
, &extent
,
673 partial_final_block
);
675 loff_t add
= ((loff_t
)offset
<< sb
->s_blocksize_bits
) |
677 err
= udf_do_extend_file(inode
, &epos
, &extent
, add
);
683 iinfo
->i_lenExtents
= newsize
;
689 static sector_t
inode_getblk(struct inode
*inode
, sector_t block
,
692 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
];
693 struct extent_position prev_epos
, cur_epos
, next_epos
;
694 int count
= 0, startnum
= 0, endnum
= 0;
695 uint32_t elen
= 0, tmpelen
;
696 struct kernel_lb_addr eloc
, tmpeloc
;
698 loff_t lbcount
= 0, b_off
= 0;
699 uint32_t newblocknum
, newblock
;
702 struct udf_inode_info
*iinfo
= UDF_I(inode
);
703 int goal
= 0, pgoal
= iinfo
->i_location
.logicalBlockNum
;
709 prev_epos
.offset
= udf_file_entry_alloc_offset(inode
);
710 prev_epos
.block
= iinfo
->i_location
;
712 cur_epos
= next_epos
= prev_epos
;
713 b_off
= (loff_t
)block
<< inode
->i_sb
->s_blocksize_bits
;
715 /* find the extent which contains the block we are looking for.
716 alternate between laarr[0] and laarr[1] for locations of the
717 current extent, and the previous extent */
719 if (prev_epos
.bh
!= cur_epos
.bh
) {
720 brelse(prev_epos
.bh
);
722 prev_epos
.bh
= cur_epos
.bh
;
724 if (cur_epos
.bh
!= next_epos
.bh
) {
726 get_bh(next_epos
.bh
);
727 cur_epos
.bh
= next_epos
.bh
;
732 prev_epos
.block
= cur_epos
.block
;
733 cur_epos
.block
= next_epos
.block
;
735 prev_epos
.offset
= cur_epos
.offset
;
736 cur_epos
.offset
= next_epos
.offset
;
738 etype
= udf_next_aext(inode
, &next_epos
, &eloc
, &elen
, 1);
744 laarr
[c
].extLength
= (etype
<< 30) | elen
;
745 laarr
[c
].extLocation
= eloc
;
747 if (etype
!= (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
748 pgoal
= eloc
.logicalBlockNum
+
749 ((elen
+ inode
->i_sb
->s_blocksize
- 1) >>
750 inode
->i_sb
->s_blocksize_bits
);
753 } while (lbcount
+ elen
<= b_off
);
756 offset
= b_off
>> inode
->i_sb
->s_blocksize_bits
;
758 * Move prev_epos and cur_epos into indirect extent if we are at
761 udf_next_aext(inode
, &prev_epos
, &tmpeloc
, &tmpelen
, 0);
762 udf_next_aext(inode
, &cur_epos
, &tmpeloc
, &tmpelen
, 0);
764 /* if the extent is allocated and recorded, return the block
765 if the extent is not a multiple of the blocksize, round up */
767 if (etype
== (EXT_RECORDED_ALLOCATED
>> 30)) {
768 if (elen
& (inode
->i_sb
->s_blocksize
- 1)) {
769 elen
= EXT_RECORDED_ALLOCATED
|
770 ((elen
+ inode
->i_sb
->s_blocksize
- 1) &
771 ~(inode
->i_sb
->s_blocksize
- 1));
772 udf_write_aext(inode
, &cur_epos
, &eloc
, elen
, 1);
774 brelse(prev_epos
.bh
);
776 brelse(next_epos
.bh
);
777 newblock
= udf_get_lb_pblock(inode
->i_sb
, &eloc
, offset
);
781 /* Are we beyond EOF? */
791 /* Create a fake extent when there's not one */
792 memset(&laarr
[0].extLocation
, 0x00,
793 sizeof(struct kernel_lb_addr
));
794 laarr
[0].extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
;
795 /* Will udf_do_extend_file() create real extent from
797 startnum
= (offset
> 0);
799 /* Create extents for the hole between EOF and offset */
800 hole_len
= (loff_t
)offset
<< inode
->i_blkbits
;
801 ret
= udf_do_extend_file(inode
, &prev_epos
, laarr
, hole_len
);
803 brelse(prev_epos
.bh
);
805 brelse(next_epos
.bh
);
812 /* We are not covered by a preallocated extent? */
813 if ((laarr
[0].extLength
& UDF_EXTENT_FLAG_MASK
) !=
814 EXT_NOT_RECORDED_ALLOCATED
) {
815 /* Is there any real extent? - otherwise we overwrite
819 laarr
[c
].extLength
= EXT_NOT_RECORDED_NOT_ALLOCATED
|
820 inode
->i_sb
->s_blocksize
;
821 memset(&laarr
[c
].extLocation
, 0x00,
822 sizeof(struct kernel_lb_addr
));
829 endnum
= startnum
= ((count
> 2) ? 2 : count
);
831 /* if the current extent is in position 0,
832 swap it with the previous */
833 if (!c
&& count
!= 1) {
840 /* if the current block is located in an extent,
841 read the next extent */
842 etype
= udf_next_aext(inode
, &next_epos
, &eloc
, &elen
, 0);
844 laarr
[c
+ 1].extLength
= (etype
<< 30) | elen
;
845 laarr
[c
+ 1].extLocation
= eloc
;
853 /* if the current extent is not recorded but allocated, get the
854 * block in the extent corresponding to the requested block */
855 if ((laarr
[c
].extLength
>> 30) == (EXT_NOT_RECORDED_ALLOCATED
>> 30))
856 newblocknum
= laarr
[c
].extLocation
.logicalBlockNum
+ offset
;
857 else { /* otherwise, allocate a new block */
858 if (iinfo
->i_next_alloc_block
== block
)
859 goal
= iinfo
->i_next_alloc_goal
;
862 if (!(goal
= pgoal
)) /* XXX: what was intended here? */
863 goal
= iinfo
->i_location
.logicalBlockNum
+ 1;
866 newblocknum
= udf_new_block(inode
->i_sb
, inode
,
867 iinfo
->i_location
.partitionReferenceNum
,
870 brelse(prev_epos
.bh
);
872 brelse(next_epos
.bh
);
877 iinfo
->i_lenExtents
+= inode
->i_sb
->s_blocksize
;
880 /* if the extent the requsted block is located in contains multiple
881 * blocks, split the extent into at most three extents. blocks prior
882 * to requested block, requested block, and blocks after requested
884 udf_split_extents(inode
, &c
, offset
, newblocknum
, laarr
, &endnum
);
886 #ifdef UDF_PREALLOCATE
887 /* We preallocate blocks only for regular files. It also makes sense
888 * for directories but there's a problem when to drop the
889 * preallocation. We might use some delayed work for that but I feel
890 * it's overengineering for a filesystem like UDF. */
891 if (S_ISREG(inode
->i_mode
))
892 udf_prealloc_extents(inode
, c
, lastblock
, laarr
, &endnum
);
895 /* merge any continuous blocks in laarr */
896 udf_merge_extents(inode
, laarr
, &endnum
);
898 /* write back the new extents, inserting new extents if the new number
899 * of extents is greater than the old number, and deleting extents if
900 * the new number of extents is less than the old number */
901 udf_update_extents(inode
, laarr
, startnum
, endnum
, &prev_epos
);
903 brelse(prev_epos
.bh
);
905 brelse(next_epos
.bh
);
907 newblock
= udf_get_pblock(inode
->i_sb
, newblocknum
,
908 iinfo
->i_location
.partitionReferenceNum
, 0);
914 iinfo
->i_next_alloc_block
= block
;
915 iinfo
->i_next_alloc_goal
= newblocknum
;
916 inode
->i_ctime
= current_time(inode
);
919 udf_sync_inode(inode
);
921 mark_inode_dirty(inode
);
926 static void udf_split_extents(struct inode
*inode
, int *c
, int offset
,
928 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
931 unsigned long blocksize
= inode
->i_sb
->s_blocksize
;
932 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
934 if ((laarr
[*c
].extLength
>> 30) == (EXT_NOT_RECORDED_ALLOCATED
>> 30) ||
935 (laarr
[*c
].extLength
>> 30) ==
936 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30)) {
938 int blen
= ((laarr
[curr
].extLength
& UDF_EXTENT_LENGTH_MASK
) +
939 blocksize
- 1) >> blocksize_bits
;
940 int8_t etype
= (laarr
[curr
].extLength
>> 30);
944 else if (!offset
|| blen
== offset
+ 1) {
945 laarr
[curr
+ 2] = laarr
[curr
+ 1];
946 laarr
[curr
+ 1] = laarr
[curr
];
948 laarr
[curr
+ 3] = laarr
[curr
+ 1];
949 laarr
[curr
+ 2] = laarr
[curr
+ 1] = laarr
[curr
];
953 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
954 udf_free_blocks(inode
->i_sb
, inode
,
955 &laarr
[curr
].extLocation
,
957 laarr
[curr
].extLength
=
958 EXT_NOT_RECORDED_NOT_ALLOCATED
|
959 (offset
<< blocksize_bits
);
960 laarr
[curr
].extLocation
.logicalBlockNum
= 0;
961 laarr
[curr
].extLocation
.
962 partitionReferenceNum
= 0;
964 laarr
[curr
].extLength
= (etype
<< 30) |
965 (offset
<< blocksize_bits
);
971 laarr
[curr
].extLocation
.logicalBlockNum
= newblocknum
;
972 if (etype
== (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))
973 laarr
[curr
].extLocation
.partitionReferenceNum
=
974 UDF_I(inode
)->i_location
.partitionReferenceNum
;
975 laarr
[curr
].extLength
= EXT_RECORDED_ALLOCATED
|
979 if (blen
!= offset
+ 1) {
980 if (etype
== (EXT_NOT_RECORDED_ALLOCATED
>> 30))
981 laarr
[curr
].extLocation
.logicalBlockNum
+=
983 laarr
[curr
].extLength
= (etype
<< 30) |
984 ((blen
- (offset
+ 1)) << blocksize_bits
);
991 static void udf_prealloc_extents(struct inode
*inode
, int c
, int lastblock
,
992 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
995 int start
, length
= 0, currlength
= 0, i
;
997 if (*endnum
>= (c
+ 1)) {
1003 if ((laarr
[c
+ 1].extLength
>> 30) ==
1004 (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
1006 length
= currlength
=
1007 (((laarr
[c
+ 1].extLength
&
1008 UDF_EXTENT_LENGTH_MASK
) +
1009 inode
->i_sb
->s_blocksize
- 1) >>
1010 inode
->i_sb
->s_blocksize_bits
);
1015 for (i
= start
+ 1; i
<= *endnum
; i
++) {
1018 length
+= UDF_DEFAULT_PREALLOC_BLOCKS
;
1019 } else if ((laarr
[i
].extLength
>> 30) ==
1020 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30)) {
1021 length
+= (((laarr
[i
].extLength
&
1022 UDF_EXTENT_LENGTH_MASK
) +
1023 inode
->i_sb
->s_blocksize
- 1) >>
1024 inode
->i_sb
->s_blocksize_bits
);
1030 int next
= laarr
[start
].extLocation
.logicalBlockNum
+
1031 (((laarr
[start
].extLength
& UDF_EXTENT_LENGTH_MASK
) +
1032 inode
->i_sb
->s_blocksize
- 1) >>
1033 inode
->i_sb
->s_blocksize_bits
);
1034 int numalloc
= udf_prealloc_blocks(inode
->i_sb
, inode
,
1035 laarr
[start
].extLocation
.partitionReferenceNum
,
1036 next
, (UDF_DEFAULT_PREALLOC_BLOCKS
> length
?
1037 length
: UDF_DEFAULT_PREALLOC_BLOCKS
) -
1040 if (start
== (c
+ 1))
1041 laarr
[start
].extLength
+=
1043 inode
->i_sb
->s_blocksize_bits
);
1045 memmove(&laarr
[c
+ 2], &laarr
[c
+ 1],
1046 sizeof(struct long_ad
) * (*endnum
- (c
+ 1)));
1048 laarr
[c
+ 1].extLocation
.logicalBlockNum
= next
;
1049 laarr
[c
+ 1].extLocation
.partitionReferenceNum
=
1050 laarr
[c
].extLocation
.
1051 partitionReferenceNum
;
1052 laarr
[c
+ 1].extLength
=
1053 EXT_NOT_RECORDED_ALLOCATED
|
1055 inode
->i_sb
->s_blocksize_bits
);
1059 for (i
= start
+ 1; numalloc
&& i
< *endnum
; i
++) {
1060 int elen
= ((laarr
[i
].extLength
&
1061 UDF_EXTENT_LENGTH_MASK
) +
1062 inode
->i_sb
->s_blocksize
- 1) >>
1063 inode
->i_sb
->s_blocksize_bits
;
1065 if (elen
> numalloc
) {
1066 laarr
[i
].extLength
-=
1068 inode
->i_sb
->s_blocksize_bits
);
1072 if (*endnum
> (i
+ 1))
1075 sizeof(struct long_ad
) *
1076 (*endnum
- (i
+ 1)));
1081 UDF_I(inode
)->i_lenExtents
+=
1082 numalloc
<< inode
->i_sb
->s_blocksize_bits
;
1087 static void udf_merge_extents(struct inode
*inode
,
1088 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
1092 unsigned long blocksize
= inode
->i_sb
->s_blocksize
;
1093 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
1095 for (i
= 0; i
< (*endnum
- 1); i
++) {
1096 struct kernel_long_ad
*li
/*l[i]*/ = &laarr
[i
];
1097 struct kernel_long_ad
*lip1
/*l[i plus 1]*/ = &laarr
[i
+ 1];
1099 if (((li
->extLength
>> 30) == (lip1
->extLength
>> 30)) &&
1100 (((li
->extLength
>> 30) ==
1101 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30)) ||
1102 ((lip1
->extLocation
.logicalBlockNum
-
1103 li
->extLocation
.logicalBlockNum
) ==
1104 (((li
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1105 blocksize
- 1) >> blocksize_bits
)))) {
1107 if (((li
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1108 (lip1
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1109 blocksize
- 1) & ~UDF_EXTENT_LENGTH_MASK
) {
1110 lip1
->extLength
= (lip1
->extLength
-
1112 UDF_EXTENT_LENGTH_MASK
) +
1113 UDF_EXTENT_LENGTH_MASK
) &
1115 li
->extLength
= (li
->extLength
&
1116 UDF_EXTENT_FLAG_MASK
) +
1117 (UDF_EXTENT_LENGTH_MASK
+ 1) -
1119 lip1
->extLocation
.logicalBlockNum
=
1120 li
->extLocation
.logicalBlockNum
+
1122 UDF_EXTENT_LENGTH_MASK
) >>
1125 li
->extLength
= lip1
->extLength
+
1127 UDF_EXTENT_LENGTH_MASK
) +
1128 blocksize
- 1) & ~(blocksize
- 1));
1129 if (*endnum
> (i
+ 2))
1130 memmove(&laarr
[i
+ 1], &laarr
[i
+ 2],
1131 sizeof(struct long_ad
) *
1132 (*endnum
- (i
+ 2)));
1136 } else if (((li
->extLength
>> 30) ==
1137 (EXT_NOT_RECORDED_ALLOCATED
>> 30)) &&
1138 ((lip1
->extLength
>> 30) ==
1139 (EXT_NOT_RECORDED_NOT_ALLOCATED
>> 30))) {
1140 udf_free_blocks(inode
->i_sb
, inode
, &li
->extLocation
, 0,
1142 UDF_EXTENT_LENGTH_MASK
) +
1143 blocksize
- 1) >> blocksize_bits
);
1144 li
->extLocation
.logicalBlockNum
= 0;
1145 li
->extLocation
.partitionReferenceNum
= 0;
1147 if (((li
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1148 (lip1
->extLength
& UDF_EXTENT_LENGTH_MASK
) +
1149 blocksize
- 1) & ~UDF_EXTENT_LENGTH_MASK
) {
1150 lip1
->extLength
= (lip1
->extLength
-
1152 UDF_EXTENT_LENGTH_MASK
) +
1153 UDF_EXTENT_LENGTH_MASK
) &
1155 li
->extLength
= (li
->extLength
&
1156 UDF_EXTENT_FLAG_MASK
) +
1157 (UDF_EXTENT_LENGTH_MASK
+ 1) -
1160 li
->extLength
= lip1
->extLength
+
1162 UDF_EXTENT_LENGTH_MASK
) +
1163 blocksize
- 1) & ~(blocksize
- 1));
1164 if (*endnum
> (i
+ 2))
1165 memmove(&laarr
[i
+ 1], &laarr
[i
+ 2],
1166 sizeof(struct long_ad
) *
1167 (*endnum
- (i
+ 2)));
1171 } else if ((li
->extLength
>> 30) ==
1172 (EXT_NOT_RECORDED_ALLOCATED
>> 30)) {
1173 udf_free_blocks(inode
->i_sb
, inode
,
1174 &li
->extLocation
, 0,
1176 UDF_EXTENT_LENGTH_MASK
) +
1177 blocksize
- 1) >> blocksize_bits
);
1178 li
->extLocation
.logicalBlockNum
= 0;
1179 li
->extLocation
.partitionReferenceNum
= 0;
1180 li
->extLength
= (li
->extLength
&
1181 UDF_EXTENT_LENGTH_MASK
) |
1182 EXT_NOT_RECORDED_NOT_ALLOCATED
;
1187 static void udf_update_extents(struct inode
*inode
,
1188 struct kernel_long_ad laarr
[EXTENT_MERGE_SIZE
],
1189 int startnum
, int endnum
,
1190 struct extent_position
*epos
)
1193 struct kernel_lb_addr tmploc
;
1196 if (startnum
> endnum
) {
1197 for (i
= 0; i
< (startnum
- endnum
); i
++)
1198 udf_delete_aext(inode
, *epos
, laarr
[i
].extLocation
,
1199 laarr
[i
].extLength
);
1200 } else if (startnum
< endnum
) {
1201 for (i
= 0; i
< (endnum
- startnum
); i
++) {
1202 udf_insert_aext(inode
, *epos
, laarr
[i
].extLocation
,
1203 laarr
[i
].extLength
);
1204 udf_next_aext(inode
, epos
, &laarr
[i
].extLocation
,
1205 &laarr
[i
].extLength
, 1);
1210 for (i
= start
; i
< endnum
; i
++) {
1211 udf_next_aext(inode
, epos
, &tmploc
, &tmplen
, 0);
1212 udf_write_aext(inode
, epos
, &laarr
[i
].extLocation
,
1213 laarr
[i
].extLength
, 1);
1217 struct buffer_head
*udf_bread(struct inode
*inode
, int block
,
1218 int create
, int *err
)
1220 struct buffer_head
*bh
= NULL
;
1222 bh
= udf_getblk(inode
, block
, create
, err
);
1226 if (buffer_uptodate(bh
))
1229 ll_rw_block(REQ_OP_READ
, 0, 1, &bh
);
1232 if (buffer_uptodate(bh
))
1240 int udf_setsize(struct inode
*inode
, loff_t newsize
)
1243 struct udf_inode_info
*iinfo
;
1244 int bsize
= i_blocksize(inode
);
1246 if (!(S_ISREG(inode
->i_mode
) || S_ISDIR(inode
->i_mode
) ||
1247 S_ISLNK(inode
->i_mode
)))
1249 if (IS_APPEND(inode
) || IS_IMMUTABLE(inode
))
1252 iinfo
= UDF_I(inode
);
1253 if (newsize
> inode
->i_size
) {
1254 down_write(&iinfo
->i_data_sem
);
1255 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1257 (udf_file_entry_alloc_offset(inode
) + newsize
)) {
1258 err
= udf_expand_file_adinicb(inode
);
1261 down_write(&iinfo
->i_data_sem
);
1263 iinfo
->i_lenAlloc
= newsize
;
1267 err
= udf_extend_file(inode
, newsize
);
1269 up_write(&iinfo
->i_data_sem
);
1273 up_write(&iinfo
->i_data_sem
);
1274 truncate_setsize(inode
, newsize
);
1276 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1277 down_write(&iinfo
->i_data_sem
);
1278 udf_clear_extent_cache(inode
);
1279 memset(iinfo
->i_ext
.i_data
+ iinfo
->i_lenEAttr
+ newsize
,
1280 0x00, bsize
- newsize
-
1281 udf_file_entry_alloc_offset(inode
));
1282 iinfo
->i_lenAlloc
= newsize
;
1283 truncate_setsize(inode
, newsize
);
1284 up_write(&iinfo
->i_data_sem
);
1287 err
= block_truncate_page(inode
->i_mapping
, newsize
,
1291 truncate_setsize(inode
, newsize
);
1292 down_write(&iinfo
->i_data_sem
);
1293 udf_clear_extent_cache(inode
);
1294 udf_truncate_extents(inode
);
1295 up_write(&iinfo
->i_data_sem
);
1298 inode
->i_mtime
= inode
->i_ctime
= current_time(inode
);
1300 udf_sync_inode(inode
);
1302 mark_inode_dirty(inode
);
1307 * Maximum length of linked list formed by ICB hierarchy. The chosen number is
1308 * arbitrary - just that we hopefully don't limit any real use of rewritten
1309 * inode on write-once media but avoid looping for too long on corrupted media.
1311 #define UDF_MAX_ICB_NESTING 1024
1313 static int udf_read_inode(struct inode
*inode
, bool hidden_inode
)
1315 struct buffer_head
*bh
= NULL
;
1316 struct fileEntry
*fe
;
1317 struct extendedFileEntry
*efe
;
1319 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1320 struct udf_sb_info
*sbi
= UDF_SB(inode
->i_sb
);
1321 struct kernel_lb_addr
*iloc
= &iinfo
->i_location
;
1322 unsigned int link_count
;
1323 unsigned int indirections
= 0;
1324 int bs
= inode
->i_sb
->s_blocksize
;
1328 if (iloc
->logicalBlockNum
>=
1329 sbi
->s_partmaps
[iloc
->partitionReferenceNum
].s_partition_len
) {
1330 udf_debug("block=%d, partition=%d out of range\n",
1331 iloc
->logicalBlockNum
, iloc
->partitionReferenceNum
);
1336 * Set defaults, but the inode is still incomplete!
1337 * Note: get_new_inode() sets the following on a new inode:
1340 * i_flags = sb->s_flags
1342 * clean_inode(): zero fills and sets
1347 bh
= udf_read_ptagged(inode
->i_sb
, iloc
, 0, &ident
);
1349 udf_err(inode
->i_sb
, "(ino %ld) failed !bh\n", inode
->i_ino
);
1353 if (ident
!= TAG_IDENT_FE
&& ident
!= TAG_IDENT_EFE
&&
1354 ident
!= TAG_IDENT_USE
) {
1355 udf_err(inode
->i_sb
, "(ino %ld) failed ident=%d\n",
1356 inode
->i_ino
, ident
);
1360 fe
= (struct fileEntry
*)bh
->b_data
;
1361 efe
= (struct extendedFileEntry
*)bh
->b_data
;
1363 if (fe
->icbTag
.strategyType
== cpu_to_le16(4096)) {
1364 struct buffer_head
*ibh
;
1366 ibh
= udf_read_ptagged(inode
->i_sb
, iloc
, 1, &ident
);
1367 if (ident
== TAG_IDENT_IE
&& ibh
) {
1368 struct kernel_lb_addr loc
;
1369 struct indirectEntry
*ie
;
1371 ie
= (struct indirectEntry
*)ibh
->b_data
;
1372 loc
= lelb_to_cpu(ie
->indirectICB
.extLocation
);
1374 if (ie
->indirectICB
.extLength
) {
1376 memcpy(&iinfo
->i_location
, &loc
,
1377 sizeof(struct kernel_lb_addr
));
1378 if (++indirections
> UDF_MAX_ICB_NESTING
) {
1379 udf_err(inode
->i_sb
,
1380 "too many ICBs in ICB hierarchy"
1381 " (max %d supported)\n",
1382 UDF_MAX_ICB_NESTING
);
1390 } else if (fe
->icbTag
.strategyType
!= cpu_to_le16(4)) {
1391 udf_err(inode
->i_sb
, "unsupported strategy type: %d\n",
1392 le16_to_cpu(fe
->icbTag
.strategyType
));
1395 if (fe
->icbTag
.strategyType
== cpu_to_le16(4))
1396 iinfo
->i_strat4096
= 0;
1397 else /* if (fe->icbTag.strategyType == cpu_to_le16(4096)) */
1398 iinfo
->i_strat4096
= 1;
1400 iinfo
->i_alloc_type
= le16_to_cpu(fe
->icbTag
.flags
) &
1401 ICBTAG_FLAG_AD_MASK
;
1402 if (iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_SHORT
&&
1403 iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_LONG
&&
1404 iinfo
->i_alloc_type
!= ICBTAG_FLAG_AD_IN_ICB
) {
1408 iinfo
->i_unique
= 0;
1409 iinfo
->i_lenEAttr
= 0;
1410 iinfo
->i_lenExtents
= 0;
1411 iinfo
->i_lenAlloc
= 0;
1412 iinfo
->i_next_alloc_block
= 0;
1413 iinfo
->i_next_alloc_goal
= 0;
1414 if (fe
->descTag
.tagIdent
== cpu_to_le16(TAG_IDENT_EFE
)) {
1417 ret
= udf_alloc_i_data(inode
, bs
-
1418 sizeof(struct extendedFileEntry
));
1421 memcpy(iinfo
->i_ext
.i_data
,
1422 bh
->b_data
+ sizeof(struct extendedFileEntry
),
1423 bs
- sizeof(struct extendedFileEntry
));
1424 } else if (fe
->descTag
.tagIdent
== cpu_to_le16(TAG_IDENT_FE
)) {
1427 ret
= udf_alloc_i_data(inode
, bs
- sizeof(struct fileEntry
));
1430 memcpy(iinfo
->i_ext
.i_data
,
1431 bh
->b_data
+ sizeof(struct fileEntry
),
1432 bs
- sizeof(struct fileEntry
));
1433 } else if (fe
->descTag
.tagIdent
== cpu_to_le16(TAG_IDENT_USE
)) {
1436 iinfo
->i_lenAlloc
= le32_to_cpu(
1437 ((struct unallocSpaceEntry
*)bh
->b_data
)->
1439 ret
= udf_alloc_i_data(inode
, bs
-
1440 sizeof(struct unallocSpaceEntry
));
1443 memcpy(iinfo
->i_ext
.i_data
,
1444 bh
->b_data
+ sizeof(struct unallocSpaceEntry
),
1445 bs
- sizeof(struct unallocSpaceEntry
));
1450 read_lock(&sbi
->s_cred_lock
);
1451 i_uid_write(inode
, le32_to_cpu(fe
->uid
));
1452 if (!uid_valid(inode
->i_uid
) ||
1453 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_UID_IGNORE
) ||
1454 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_UID_SET
))
1455 inode
->i_uid
= UDF_SB(inode
->i_sb
)->s_uid
;
1457 i_gid_write(inode
, le32_to_cpu(fe
->gid
));
1458 if (!gid_valid(inode
->i_gid
) ||
1459 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_GID_IGNORE
) ||
1460 UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_GID_SET
))
1461 inode
->i_gid
= UDF_SB(inode
->i_sb
)->s_gid
;
1463 if (fe
->icbTag
.fileType
!= ICBTAG_FILE_TYPE_DIRECTORY
&&
1464 sbi
->s_fmode
!= UDF_INVALID_MODE
)
1465 inode
->i_mode
= sbi
->s_fmode
;
1466 else if (fe
->icbTag
.fileType
== ICBTAG_FILE_TYPE_DIRECTORY
&&
1467 sbi
->s_dmode
!= UDF_INVALID_MODE
)
1468 inode
->i_mode
= sbi
->s_dmode
;
1470 inode
->i_mode
= udf_convert_permissions(fe
);
1471 inode
->i_mode
&= ~sbi
->s_umask
;
1472 read_unlock(&sbi
->s_cred_lock
);
1474 link_count
= le16_to_cpu(fe
->fileLinkCount
);
1476 if (!hidden_inode
) {
1482 set_nlink(inode
, link_count
);
1484 inode
->i_size
= le64_to_cpu(fe
->informationLength
);
1485 iinfo
->i_lenExtents
= inode
->i_size
;
1487 if (iinfo
->i_efe
== 0) {
1488 inode
->i_blocks
= le64_to_cpu(fe
->logicalBlocksRecorded
) <<
1489 (inode
->i_sb
->s_blocksize_bits
- 9);
1491 if (!udf_disk_stamp_to_time(&inode
->i_atime
, fe
->accessTime
))
1492 inode
->i_atime
= sbi
->s_record_time
;
1494 if (!udf_disk_stamp_to_time(&inode
->i_mtime
,
1495 fe
->modificationTime
))
1496 inode
->i_mtime
= sbi
->s_record_time
;
1498 if (!udf_disk_stamp_to_time(&inode
->i_ctime
, fe
->attrTime
))
1499 inode
->i_ctime
= sbi
->s_record_time
;
1501 iinfo
->i_unique
= le64_to_cpu(fe
->uniqueID
);
1502 iinfo
->i_lenEAttr
= le32_to_cpu(fe
->lengthExtendedAttr
);
1503 iinfo
->i_lenAlloc
= le32_to_cpu(fe
->lengthAllocDescs
);
1504 iinfo
->i_checkpoint
= le32_to_cpu(fe
->checkpoint
);
1506 inode
->i_blocks
= le64_to_cpu(efe
->logicalBlocksRecorded
) <<
1507 (inode
->i_sb
->s_blocksize_bits
- 9);
1509 if (!udf_disk_stamp_to_time(&inode
->i_atime
, efe
->accessTime
))
1510 inode
->i_atime
= sbi
->s_record_time
;
1512 if (!udf_disk_stamp_to_time(&inode
->i_mtime
,
1513 efe
->modificationTime
))
1514 inode
->i_mtime
= sbi
->s_record_time
;
1516 if (!udf_disk_stamp_to_time(&iinfo
->i_crtime
, efe
->createTime
))
1517 iinfo
->i_crtime
= sbi
->s_record_time
;
1519 if (!udf_disk_stamp_to_time(&inode
->i_ctime
, efe
->attrTime
))
1520 inode
->i_ctime
= sbi
->s_record_time
;
1522 iinfo
->i_unique
= le64_to_cpu(efe
->uniqueID
);
1523 iinfo
->i_lenEAttr
= le32_to_cpu(efe
->lengthExtendedAttr
);
1524 iinfo
->i_lenAlloc
= le32_to_cpu(efe
->lengthAllocDescs
);
1525 iinfo
->i_checkpoint
= le32_to_cpu(efe
->checkpoint
);
1527 inode
->i_generation
= iinfo
->i_unique
;
1530 * Sanity check length of allocation descriptors and extended attrs to
1531 * avoid integer overflows
1533 if (iinfo
->i_lenEAttr
> bs
|| iinfo
->i_lenAlloc
> bs
)
1535 /* Now do exact checks */
1536 if (udf_file_entry_alloc_offset(inode
) + iinfo
->i_lenAlloc
> bs
)
1538 /* Sanity checks for files in ICB so that we don't get confused later */
1539 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
) {
1541 * For file in ICB data is stored in allocation descriptor
1542 * so sizes should match
1544 if (iinfo
->i_lenAlloc
!= inode
->i_size
)
1546 /* File in ICB has to fit in there... */
1547 if (inode
->i_size
> bs
- udf_file_entry_alloc_offset(inode
))
1551 switch (fe
->icbTag
.fileType
) {
1552 case ICBTAG_FILE_TYPE_DIRECTORY
:
1553 inode
->i_op
= &udf_dir_inode_operations
;
1554 inode
->i_fop
= &udf_dir_operations
;
1555 inode
->i_mode
|= S_IFDIR
;
1558 case ICBTAG_FILE_TYPE_REALTIME
:
1559 case ICBTAG_FILE_TYPE_REGULAR
:
1560 case ICBTAG_FILE_TYPE_UNDEF
:
1561 case ICBTAG_FILE_TYPE_VAT20
:
1562 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1563 inode
->i_data
.a_ops
= &udf_adinicb_aops
;
1565 inode
->i_data
.a_ops
= &udf_aops
;
1566 inode
->i_op
= &udf_file_inode_operations
;
1567 inode
->i_fop
= &udf_file_operations
;
1568 inode
->i_mode
|= S_IFREG
;
1570 case ICBTAG_FILE_TYPE_BLOCK
:
1571 inode
->i_mode
|= S_IFBLK
;
1573 case ICBTAG_FILE_TYPE_CHAR
:
1574 inode
->i_mode
|= S_IFCHR
;
1576 case ICBTAG_FILE_TYPE_FIFO
:
1577 init_special_inode(inode
, inode
->i_mode
| S_IFIFO
, 0);
1579 case ICBTAG_FILE_TYPE_SOCKET
:
1580 init_special_inode(inode
, inode
->i_mode
| S_IFSOCK
, 0);
1582 case ICBTAG_FILE_TYPE_SYMLINK
:
1583 inode
->i_data
.a_ops
= &udf_symlink_aops
;
1584 inode
->i_op
= &page_symlink_inode_operations
;
1585 inode_nohighmem(inode
);
1586 inode
->i_mode
= S_IFLNK
| S_IRWXUGO
;
1588 case ICBTAG_FILE_TYPE_MAIN
:
1589 udf_debug("METADATA FILE-----\n");
1591 case ICBTAG_FILE_TYPE_MIRROR
:
1592 udf_debug("METADATA MIRROR FILE-----\n");
1594 case ICBTAG_FILE_TYPE_BITMAP
:
1595 udf_debug("METADATA BITMAP FILE-----\n");
1598 udf_err(inode
->i_sb
, "(ino %ld) failed unknown file type=%d\n",
1599 inode
->i_ino
, fe
->icbTag
.fileType
);
1602 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
1603 struct deviceSpec
*dsea
=
1604 (struct deviceSpec
*)udf_get_extendedattr(inode
, 12, 1);
1606 init_special_inode(inode
, inode
->i_mode
,
1607 MKDEV(le32_to_cpu(dsea
->majorDeviceIdent
),
1608 le32_to_cpu(dsea
->minorDeviceIdent
)));
1609 /* Developer ID ??? */
1619 static int udf_alloc_i_data(struct inode
*inode
, size_t size
)
1621 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1622 iinfo
->i_ext
.i_data
= kmalloc(size
, GFP_KERNEL
);
1624 if (!iinfo
->i_ext
.i_data
) {
1625 udf_err(inode
->i_sb
, "(ino %ld) no free memory\n",
1633 static umode_t
udf_convert_permissions(struct fileEntry
*fe
)
1636 uint32_t permissions
;
1639 permissions
= le32_to_cpu(fe
->permissions
);
1640 flags
= le16_to_cpu(fe
->icbTag
.flags
);
1642 mode
= ((permissions
) & S_IRWXO
) |
1643 ((permissions
>> 2) & S_IRWXG
) |
1644 ((permissions
>> 4) & S_IRWXU
) |
1645 ((flags
& ICBTAG_FLAG_SETUID
) ? S_ISUID
: 0) |
1646 ((flags
& ICBTAG_FLAG_SETGID
) ? S_ISGID
: 0) |
1647 ((flags
& ICBTAG_FLAG_STICKY
) ? S_ISVTX
: 0);
1652 int udf_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1654 return udf_update_inode(inode
, wbc
->sync_mode
== WB_SYNC_ALL
);
1657 static int udf_sync_inode(struct inode
*inode
)
1659 return udf_update_inode(inode
, 1);
1662 static int udf_update_inode(struct inode
*inode
, int do_sync
)
1664 struct buffer_head
*bh
= NULL
;
1665 struct fileEntry
*fe
;
1666 struct extendedFileEntry
*efe
;
1667 uint64_t lb_recorded
;
1672 struct udf_sb_info
*sbi
= UDF_SB(inode
->i_sb
);
1673 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
1674 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1676 bh
= udf_tgetblk(inode
->i_sb
,
1677 udf_get_lb_pblock(inode
->i_sb
, &iinfo
->i_location
, 0));
1679 udf_debug("getblk failure\n");
1684 memset(bh
->b_data
, 0, inode
->i_sb
->s_blocksize
);
1685 fe
= (struct fileEntry
*)bh
->b_data
;
1686 efe
= (struct extendedFileEntry
*)bh
->b_data
;
1689 struct unallocSpaceEntry
*use
=
1690 (struct unallocSpaceEntry
*)bh
->b_data
;
1692 use
->lengthAllocDescs
= cpu_to_le32(iinfo
->i_lenAlloc
);
1693 memcpy(bh
->b_data
+ sizeof(struct unallocSpaceEntry
),
1694 iinfo
->i_ext
.i_data
, inode
->i_sb
->s_blocksize
-
1695 sizeof(struct unallocSpaceEntry
));
1696 use
->descTag
.tagIdent
= cpu_to_le16(TAG_IDENT_USE
);
1697 crclen
= sizeof(struct unallocSpaceEntry
);
1702 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_UID_FORGET
))
1703 fe
->uid
= cpu_to_le32(-1);
1705 fe
->uid
= cpu_to_le32(i_uid_read(inode
));
1707 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_GID_FORGET
))
1708 fe
->gid
= cpu_to_le32(-1);
1710 fe
->gid
= cpu_to_le32(i_gid_read(inode
));
1712 udfperms
= ((inode
->i_mode
& S_IRWXO
)) |
1713 ((inode
->i_mode
& S_IRWXG
) << 2) |
1714 ((inode
->i_mode
& S_IRWXU
) << 4);
1716 udfperms
|= (le32_to_cpu(fe
->permissions
) &
1717 (FE_PERM_O_DELETE
| FE_PERM_O_CHATTR
|
1718 FE_PERM_G_DELETE
| FE_PERM_G_CHATTR
|
1719 FE_PERM_U_DELETE
| FE_PERM_U_CHATTR
));
1720 fe
->permissions
= cpu_to_le32(udfperms
);
1722 if (S_ISDIR(inode
->i_mode
) && inode
->i_nlink
> 0)
1723 fe
->fileLinkCount
= cpu_to_le16(inode
->i_nlink
- 1);
1725 fe
->fileLinkCount
= cpu_to_le16(inode
->i_nlink
);
1727 fe
->informationLength
= cpu_to_le64(inode
->i_size
);
1729 if (S_ISCHR(inode
->i_mode
) || S_ISBLK(inode
->i_mode
)) {
1731 struct deviceSpec
*dsea
=
1732 (struct deviceSpec
*)udf_get_extendedattr(inode
, 12, 1);
1734 dsea
= (struct deviceSpec
*)
1735 udf_add_extendedattr(inode
,
1736 sizeof(struct deviceSpec
) +
1737 sizeof(struct regid
), 12, 0x3);
1738 dsea
->attrType
= cpu_to_le32(12);
1739 dsea
->attrSubtype
= 1;
1740 dsea
->attrLength
= cpu_to_le32(
1741 sizeof(struct deviceSpec
) +
1742 sizeof(struct regid
));
1743 dsea
->impUseLength
= cpu_to_le32(sizeof(struct regid
));
1745 eid
= (struct regid
*)dsea
->impUse
;
1746 memset(eid
, 0, sizeof(struct regid
));
1747 strcpy(eid
->ident
, UDF_ID_DEVELOPER
);
1748 eid
->identSuffix
[0] = UDF_OS_CLASS_UNIX
;
1749 eid
->identSuffix
[1] = UDF_OS_ID_LINUX
;
1750 dsea
->majorDeviceIdent
= cpu_to_le32(imajor(inode
));
1751 dsea
->minorDeviceIdent
= cpu_to_le32(iminor(inode
));
1754 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_IN_ICB
)
1755 lb_recorded
= 0; /* No extents => no blocks! */
1758 (inode
->i_blocks
+ (1 << (blocksize_bits
- 9)) - 1) >>
1759 (blocksize_bits
- 9);
1761 if (iinfo
->i_efe
== 0) {
1762 memcpy(bh
->b_data
+ sizeof(struct fileEntry
),
1763 iinfo
->i_ext
.i_data
,
1764 inode
->i_sb
->s_blocksize
- sizeof(struct fileEntry
));
1765 fe
->logicalBlocksRecorded
= cpu_to_le64(lb_recorded
);
1767 udf_time_to_disk_stamp(&fe
->accessTime
, inode
->i_atime
);
1768 udf_time_to_disk_stamp(&fe
->modificationTime
, inode
->i_mtime
);
1769 udf_time_to_disk_stamp(&fe
->attrTime
, inode
->i_ctime
);
1770 memset(&(fe
->impIdent
), 0, sizeof(struct regid
));
1771 strcpy(fe
->impIdent
.ident
, UDF_ID_DEVELOPER
);
1772 fe
->impIdent
.identSuffix
[0] = UDF_OS_CLASS_UNIX
;
1773 fe
->impIdent
.identSuffix
[1] = UDF_OS_ID_LINUX
;
1774 fe
->uniqueID
= cpu_to_le64(iinfo
->i_unique
);
1775 fe
->lengthExtendedAttr
= cpu_to_le32(iinfo
->i_lenEAttr
);
1776 fe
->lengthAllocDescs
= cpu_to_le32(iinfo
->i_lenAlloc
);
1777 fe
->checkpoint
= cpu_to_le32(iinfo
->i_checkpoint
);
1778 fe
->descTag
.tagIdent
= cpu_to_le16(TAG_IDENT_FE
);
1779 crclen
= sizeof(struct fileEntry
);
1781 memcpy(bh
->b_data
+ sizeof(struct extendedFileEntry
),
1782 iinfo
->i_ext
.i_data
,
1783 inode
->i_sb
->s_blocksize
-
1784 sizeof(struct extendedFileEntry
));
1785 efe
->objectSize
= cpu_to_le64(inode
->i_size
);
1786 efe
->logicalBlocksRecorded
= cpu_to_le64(lb_recorded
);
1788 if (iinfo
->i_crtime
.tv_sec
> inode
->i_atime
.tv_sec
||
1789 (iinfo
->i_crtime
.tv_sec
== inode
->i_atime
.tv_sec
&&
1790 iinfo
->i_crtime
.tv_nsec
> inode
->i_atime
.tv_nsec
))
1791 iinfo
->i_crtime
= inode
->i_atime
;
1793 if (iinfo
->i_crtime
.tv_sec
> inode
->i_mtime
.tv_sec
||
1794 (iinfo
->i_crtime
.tv_sec
== inode
->i_mtime
.tv_sec
&&
1795 iinfo
->i_crtime
.tv_nsec
> inode
->i_mtime
.tv_nsec
))
1796 iinfo
->i_crtime
= inode
->i_mtime
;
1798 if (iinfo
->i_crtime
.tv_sec
> inode
->i_ctime
.tv_sec
||
1799 (iinfo
->i_crtime
.tv_sec
== inode
->i_ctime
.tv_sec
&&
1800 iinfo
->i_crtime
.tv_nsec
> inode
->i_ctime
.tv_nsec
))
1801 iinfo
->i_crtime
= inode
->i_ctime
;
1803 udf_time_to_disk_stamp(&efe
->accessTime
, inode
->i_atime
);
1804 udf_time_to_disk_stamp(&efe
->modificationTime
, inode
->i_mtime
);
1805 udf_time_to_disk_stamp(&efe
->createTime
, iinfo
->i_crtime
);
1806 udf_time_to_disk_stamp(&efe
->attrTime
, inode
->i_ctime
);
1808 memset(&(efe
->impIdent
), 0, sizeof(struct regid
));
1809 strcpy(efe
->impIdent
.ident
, UDF_ID_DEVELOPER
);
1810 efe
->impIdent
.identSuffix
[0] = UDF_OS_CLASS_UNIX
;
1811 efe
->impIdent
.identSuffix
[1] = UDF_OS_ID_LINUX
;
1812 efe
->uniqueID
= cpu_to_le64(iinfo
->i_unique
);
1813 efe
->lengthExtendedAttr
= cpu_to_le32(iinfo
->i_lenEAttr
);
1814 efe
->lengthAllocDescs
= cpu_to_le32(iinfo
->i_lenAlloc
);
1815 efe
->checkpoint
= cpu_to_le32(iinfo
->i_checkpoint
);
1816 efe
->descTag
.tagIdent
= cpu_to_le16(TAG_IDENT_EFE
);
1817 crclen
= sizeof(struct extendedFileEntry
);
1821 if (iinfo
->i_strat4096
) {
1822 fe
->icbTag
.strategyType
= cpu_to_le16(4096);
1823 fe
->icbTag
.strategyParameter
= cpu_to_le16(1);
1824 fe
->icbTag
.numEntries
= cpu_to_le16(2);
1826 fe
->icbTag
.strategyType
= cpu_to_le16(4);
1827 fe
->icbTag
.numEntries
= cpu_to_le16(1);
1831 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_USE
;
1832 else if (S_ISDIR(inode
->i_mode
))
1833 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_DIRECTORY
;
1834 else if (S_ISREG(inode
->i_mode
))
1835 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_REGULAR
;
1836 else if (S_ISLNK(inode
->i_mode
))
1837 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_SYMLINK
;
1838 else if (S_ISBLK(inode
->i_mode
))
1839 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_BLOCK
;
1840 else if (S_ISCHR(inode
->i_mode
))
1841 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_CHAR
;
1842 else if (S_ISFIFO(inode
->i_mode
))
1843 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_FIFO
;
1844 else if (S_ISSOCK(inode
->i_mode
))
1845 fe
->icbTag
.fileType
= ICBTAG_FILE_TYPE_SOCKET
;
1847 icbflags
= iinfo
->i_alloc_type
|
1848 ((inode
->i_mode
& S_ISUID
) ? ICBTAG_FLAG_SETUID
: 0) |
1849 ((inode
->i_mode
& S_ISGID
) ? ICBTAG_FLAG_SETGID
: 0) |
1850 ((inode
->i_mode
& S_ISVTX
) ? ICBTAG_FLAG_STICKY
: 0) |
1851 (le16_to_cpu(fe
->icbTag
.flags
) &
1852 ~(ICBTAG_FLAG_AD_MASK
| ICBTAG_FLAG_SETUID
|
1853 ICBTAG_FLAG_SETGID
| ICBTAG_FLAG_STICKY
));
1855 fe
->icbTag
.flags
= cpu_to_le16(icbflags
);
1856 if (sbi
->s_udfrev
>= 0x0200)
1857 fe
->descTag
.descVersion
= cpu_to_le16(3);
1859 fe
->descTag
.descVersion
= cpu_to_le16(2);
1860 fe
->descTag
.tagSerialNum
= cpu_to_le16(sbi
->s_serial_number
);
1861 fe
->descTag
.tagLocation
= cpu_to_le32(
1862 iinfo
->i_location
.logicalBlockNum
);
1863 crclen
+= iinfo
->i_lenEAttr
+ iinfo
->i_lenAlloc
- sizeof(struct tag
);
1864 fe
->descTag
.descCRCLength
= cpu_to_le16(crclen
);
1865 fe
->descTag
.descCRC
= cpu_to_le16(crc_itu_t(0, (char *)fe
+ sizeof(struct tag
),
1867 fe
->descTag
.tagChecksum
= udf_tag_checksum(&fe
->descTag
);
1869 set_buffer_uptodate(bh
);
1872 /* write the data blocks */
1873 mark_buffer_dirty(bh
);
1875 sync_dirty_buffer(bh
);
1876 if (buffer_write_io_error(bh
)) {
1877 udf_warn(inode
->i_sb
, "IO error syncing udf inode [%08lx]\n",
1887 struct inode
*__udf_iget(struct super_block
*sb
, struct kernel_lb_addr
*ino
,
1890 unsigned long block
= udf_get_lb_pblock(sb
, ino
, 0);
1891 struct inode
*inode
= iget_locked(sb
, block
);
1895 return ERR_PTR(-ENOMEM
);
1897 if (!(inode
->i_state
& I_NEW
))
1900 memcpy(&UDF_I(inode
)->i_location
, ino
, sizeof(struct kernel_lb_addr
));
1901 err
= udf_read_inode(inode
, hidden_inode
);
1904 return ERR_PTR(err
);
1906 unlock_new_inode(inode
);
1911 int udf_setup_indirect_aext(struct inode
*inode
, int block
,
1912 struct extent_position
*epos
)
1914 struct super_block
*sb
= inode
->i_sb
;
1915 struct buffer_head
*bh
;
1916 struct allocExtDesc
*aed
;
1917 struct extent_position nepos
;
1918 struct kernel_lb_addr neloc
;
1921 if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
1922 adsize
= sizeof(struct short_ad
);
1923 else if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
1924 adsize
= sizeof(struct long_ad
);
1928 neloc
.logicalBlockNum
= block
;
1929 neloc
.partitionReferenceNum
= epos
->block
.partitionReferenceNum
;
1931 bh
= udf_tgetblk(sb
, udf_get_lb_pblock(sb
, &neloc
, 0));
1935 memset(bh
->b_data
, 0x00, sb
->s_blocksize
);
1936 set_buffer_uptodate(bh
);
1938 mark_buffer_dirty_inode(bh
, inode
);
1940 aed
= (struct allocExtDesc
*)(bh
->b_data
);
1941 if (!UDF_QUERY_FLAG(sb
, UDF_FLAG_STRICT
)) {
1942 aed
->previousAllocExtLocation
=
1943 cpu_to_le32(epos
->block
.logicalBlockNum
);
1945 aed
->lengthAllocDescs
= cpu_to_le32(0);
1946 if (UDF_SB(sb
)->s_udfrev
>= 0x0200)
1950 udf_new_tag(bh
->b_data
, TAG_IDENT_AED
, ver
, 1, block
,
1951 sizeof(struct tag
));
1953 nepos
.block
= neloc
;
1954 nepos
.offset
= sizeof(struct allocExtDesc
);
1958 * Do we have to copy current last extent to make space for indirect
1961 if (epos
->offset
+ adsize
> sb
->s_blocksize
) {
1962 struct kernel_lb_addr cp_loc
;
1966 epos
->offset
-= adsize
;
1967 cp_type
= udf_current_aext(inode
, epos
, &cp_loc
, &cp_len
, 0);
1968 cp_len
|= ((uint32_t)cp_type
) << 30;
1970 __udf_add_aext(inode
, &nepos
, &cp_loc
, cp_len
, 1);
1971 udf_write_aext(inode
, epos
, &nepos
.block
,
1972 sb
->s_blocksize
| EXT_NEXT_EXTENT_ALLOCDECS
, 0);
1974 __udf_add_aext(inode
, epos
, &nepos
.block
,
1975 sb
->s_blocksize
| EXT_NEXT_EXTENT_ALLOCDECS
, 0);
1985 * Append extent at the given position - should be the first free one in inode
1986 * / indirect extent. This function assumes there is enough space in the inode
1987 * or indirect extent. Use udf_add_aext() if you didn't check for this before.
1989 int __udf_add_aext(struct inode
*inode
, struct extent_position
*epos
,
1990 struct kernel_lb_addr
*eloc
, uint32_t elen
, int inc
)
1992 struct udf_inode_info
*iinfo
= UDF_I(inode
);
1993 struct allocExtDesc
*aed
;
1996 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
1997 adsize
= sizeof(struct short_ad
);
1998 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
1999 adsize
= sizeof(struct long_ad
);
2004 WARN_ON(iinfo
->i_lenAlloc
!=
2005 epos
->offset
- udf_file_entry_alloc_offset(inode
));
2007 aed
= (struct allocExtDesc
*)epos
->bh
->b_data
;
2008 WARN_ON(le32_to_cpu(aed
->lengthAllocDescs
) !=
2009 epos
->offset
- sizeof(struct allocExtDesc
));
2010 WARN_ON(epos
->offset
+ adsize
> inode
->i_sb
->s_blocksize
);
2013 udf_write_aext(inode
, epos
, eloc
, elen
, inc
);
2016 iinfo
->i_lenAlloc
+= adsize
;
2017 mark_inode_dirty(inode
);
2019 aed
= (struct allocExtDesc
*)epos
->bh
->b_data
;
2020 le32_add_cpu(&aed
->lengthAllocDescs
, adsize
);
2021 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2022 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
2023 udf_update_tag(epos
->bh
->b_data
,
2024 epos
->offset
+ (inc
? 0 : adsize
));
2026 udf_update_tag(epos
->bh
->b_data
,
2027 sizeof(struct allocExtDesc
));
2028 mark_buffer_dirty_inode(epos
->bh
, inode
);
2035 * Append extent at given position - should be the first free one in inode
2036 * / indirect extent. Takes care of allocating and linking indirect blocks.
2038 int udf_add_aext(struct inode
*inode
, struct extent_position
*epos
,
2039 struct kernel_lb_addr
*eloc
, uint32_t elen
, int inc
)
2042 struct super_block
*sb
= inode
->i_sb
;
2044 if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
2045 adsize
= sizeof(struct short_ad
);
2046 else if (UDF_I(inode
)->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
2047 adsize
= sizeof(struct long_ad
);
2051 if (epos
->offset
+ (2 * adsize
) > sb
->s_blocksize
) {
2055 new_block
= udf_new_block(sb
, NULL
,
2056 epos
->block
.partitionReferenceNum
,
2057 epos
->block
.logicalBlockNum
, &err
);
2061 err
= udf_setup_indirect_aext(inode
, new_block
, epos
);
2066 return __udf_add_aext(inode
, epos
, eloc
, elen
, inc
);
2069 void udf_write_aext(struct inode
*inode
, struct extent_position
*epos
,
2070 struct kernel_lb_addr
*eloc
, uint32_t elen
, int inc
)
2074 struct short_ad
*sad
;
2075 struct long_ad
*lad
;
2076 struct udf_inode_info
*iinfo
= UDF_I(inode
);
2079 ptr
= iinfo
->i_ext
.i_data
+ epos
->offset
-
2080 udf_file_entry_alloc_offset(inode
) +
2083 ptr
= epos
->bh
->b_data
+ epos
->offset
;
2085 switch (iinfo
->i_alloc_type
) {
2086 case ICBTAG_FLAG_AD_SHORT
:
2087 sad
= (struct short_ad
*)ptr
;
2088 sad
->extLength
= cpu_to_le32(elen
);
2089 sad
->extPosition
= cpu_to_le32(eloc
->logicalBlockNum
);
2090 adsize
= sizeof(struct short_ad
);
2092 case ICBTAG_FLAG_AD_LONG
:
2093 lad
= (struct long_ad
*)ptr
;
2094 lad
->extLength
= cpu_to_le32(elen
);
2095 lad
->extLocation
= cpu_to_lelb(*eloc
);
2096 memset(lad
->impUse
, 0x00, sizeof(lad
->impUse
));
2097 adsize
= sizeof(struct long_ad
);
2104 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2105 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201) {
2106 struct allocExtDesc
*aed
=
2107 (struct allocExtDesc
*)epos
->bh
->b_data
;
2108 udf_update_tag(epos
->bh
->b_data
,
2109 le32_to_cpu(aed
->lengthAllocDescs
) +
2110 sizeof(struct allocExtDesc
));
2112 mark_buffer_dirty_inode(epos
->bh
, inode
);
2114 mark_inode_dirty(inode
);
2118 epos
->offset
+= adsize
;
2122 * Only 1 indirect extent in a row really makes sense but allow upto 16 in case
2123 * someone does some weird stuff.
2125 #define UDF_MAX_INDIR_EXTS 16
2127 int8_t udf_next_aext(struct inode
*inode
, struct extent_position
*epos
,
2128 struct kernel_lb_addr
*eloc
, uint32_t *elen
, int inc
)
2131 unsigned int indirections
= 0;
2133 while ((etype
= udf_current_aext(inode
, epos
, eloc
, elen
, inc
)) ==
2134 (EXT_NEXT_EXTENT_ALLOCDECS
>> 30)) {
2137 if (++indirections
> UDF_MAX_INDIR_EXTS
) {
2138 udf_err(inode
->i_sb
,
2139 "too many indirect extents in inode %lu\n",
2144 epos
->block
= *eloc
;
2145 epos
->offset
= sizeof(struct allocExtDesc
);
2147 block
= udf_get_lb_pblock(inode
->i_sb
, &epos
->block
, 0);
2148 epos
->bh
= udf_tread(inode
->i_sb
, block
);
2150 udf_debug("reading block %d failed!\n", block
);
2158 int8_t udf_current_aext(struct inode
*inode
, struct extent_position
*epos
,
2159 struct kernel_lb_addr
*eloc
, uint32_t *elen
, int inc
)
2164 struct short_ad
*sad
;
2165 struct long_ad
*lad
;
2166 struct udf_inode_info
*iinfo
= UDF_I(inode
);
2170 epos
->offset
= udf_file_entry_alloc_offset(inode
);
2171 ptr
= iinfo
->i_ext
.i_data
+ epos
->offset
-
2172 udf_file_entry_alloc_offset(inode
) +
2174 alen
= udf_file_entry_alloc_offset(inode
) +
2178 epos
->offset
= sizeof(struct allocExtDesc
);
2179 ptr
= epos
->bh
->b_data
+ epos
->offset
;
2180 alen
= sizeof(struct allocExtDesc
) +
2181 le32_to_cpu(((struct allocExtDesc
*)epos
->bh
->b_data
)->
2185 switch (iinfo
->i_alloc_type
) {
2186 case ICBTAG_FLAG_AD_SHORT
:
2187 sad
= udf_get_fileshortad(ptr
, alen
, &epos
->offset
, inc
);
2190 etype
= le32_to_cpu(sad
->extLength
) >> 30;
2191 eloc
->logicalBlockNum
= le32_to_cpu(sad
->extPosition
);
2192 eloc
->partitionReferenceNum
=
2193 iinfo
->i_location
.partitionReferenceNum
;
2194 *elen
= le32_to_cpu(sad
->extLength
) & UDF_EXTENT_LENGTH_MASK
;
2196 case ICBTAG_FLAG_AD_LONG
:
2197 lad
= udf_get_filelongad(ptr
, alen
, &epos
->offset
, inc
);
2200 etype
= le32_to_cpu(lad
->extLength
) >> 30;
2201 *eloc
= lelb_to_cpu(lad
->extLocation
);
2202 *elen
= le32_to_cpu(lad
->extLength
) & UDF_EXTENT_LENGTH_MASK
;
2205 udf_debug("alloc_type = %d unsupported\n", iinfo
->i_alloc_type
);
2212 static int8_t udf_insert_aext(struct inode
*inode
, struct extent_position epos
,
2213 struct kernel_lb_addr neloc
, uint32_t nelen
)
2215 struct kernel_lb_addr oeloc
;
2222 while ((etype
= udf_next_aext(inode
, &epos
, &oeloc
, &oelen
, 0)) != -1) {
2223 udf_write_aext(inode
, &epos
, &neloc
, nelen
, 1);
2225 nelen
= (etype
<< 30) | oelen
;
2227 udf_add_aext(inode
, &epos
, &neloc
, nelen
, 1);
2230 return (nelen
>> 30);
2233 int8_t udf_delete_aext(struct inode
*inode
, struct extent_position epos
,
2234 struct kernel_lb_addr eloc
, uint32_t elen
)
2236 struct extent_position oepos
;
2239 struct allocExtDesc
*aed
;
2240 struct udf_inode_info
*iinfo
;
2247 iinfo
= UDF_I(inode
);
2248 if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_SHORT
)
2249 adsize
= sizeof(struct short_ad
);
2250 else if (iinfo
->i_alloc_type
== ICBTAG_FLAG_AD_LONG
)
2251 adsize
= sizeof(struct long_ad
);
2256 if (udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1) == -1)
2259 while ((etype
= udf_next_aext(inode
, &epos
, &eloc
, &elen
, 1)) != -1) {
2260 udf_write_aext(inode
, &oepos
, &eloc
, (etype
<< 30) | elen
, 1);
2261 if (oepos
.bh
!= epos
.bh
) {
2262 oepos
.block
= epos
.block
;
2266 oepos
.offset
= epos
.offset
- adsize
;
2269 memset(&eloc
, 0x00, sizeof(struct kernel_lb_addr
));
2272 if (epos
.bh
!= oepos
.bh
) {
2273 udf_free_blocks(inode
->i_sb
, inode
, &epos
.block
, 0, 1);
2274 udf_write_aext(inode
, &oepos
, &eloc
, elen
, 1);
2275 udf_write_aext(inode
, &oepos
, &eloc
, elen
, 1);
2277 iinfo
->i_lenAlloc
-= (adsize
* 2);
2278 mark_inode_dirty(inode
);
2280 aed
= (struct allocExtDesc
*)oepos
.bh
->b_data
;
2281 le32_add_cpu(&aed
->lengthAllocDescs
, -(2 * adsize
));
2282 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2283 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
2284 udf_update_tag(oepos
.bh
->b_data
,
2285 oepos
.offset
- (2 * adsize
));
2287 udf_update_tag(oepos
.bh
->b_data
,
2288 sizeof(struct allocExtDesc
));
2289 mark_buffer_dirty_inode(oepos
.bh
, inode
);
2292 udf_write_aext(inode
, &oepos
, &eloc
, elen
, 1);
2294 iinfo
->i_lenAlloc
-= adsize
;
2295 mark_inode_dirty(inode
);
2297 aed
= (struct allocExtDesc
*)oepos
.bh
->b_data
;
2298 le32_add_cpu(&aed
->lengthAllocDescs
, -adsize
);
2299 if (!UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_STRICT
) ||
2300 UDF_SB(inode
->i_sb
)->s_udfrev
>= 0x0201)
2301 udf_update_tag(oepos
.bh
->b_data
,
2302 epos
.offset
- adsize
);
2304 udf_update_tag(oepos
.bh
->b_data
,
2305 sizeof(struct allocExtDesc
));
2306 mark_buffer_dirty_inode(oepos
.bh
, inode
);
2313 return (elen
>> 30);
2316 int8_t inode_bmap(struct inode
*inode
, sector_t block
,
2317 struct extent_position
*pos
, struct kernel_lb_addr
*eloc
,
2318 uint32_t *elen
, sector_t
*offset
)
2320 unsigned char blocksize_bits
= inode
->i_sb
->s_blocksize_bits
;
2321 loff_t lbcount
= 0, bcount
=
2322 (loff_t
) block
<< blocksize_bits
;
2324 struct udf_inode_info
*iinfo
;
2326 iinfo
= UDF_I(inode
);
2327 if (!udf_read_extent_cache(inode
, bcount
, &lbcount
, pos
)) {
2329 pos
->block
= iinfo
->i_location
;
2334 etype
= udf_next_aext(inode
, pos
, eloc
, elen
, 1);
2336 *offset
= (bcount
- lbcount
) >> blocksize_bits
;
2337 iinfo
->i_lenExtents
= lbcount
;
2341 } while (lbcount
<= bcount
);
2342 /* update extent cache */
2343 udf_update_extent_cache(inode
, lbcount
- *elen
, pos
, 1);
2344 *offset
= (bcount
+ *elen
- lbcount
) >> blocksize_bits
;
2349 long udf_block_map(struct inode
*inode
, sector_t block
)
2351 struct kernel_lb_addr eloc
;
2354 struct extent_position epos
= {};
2357 down_read(&UDF_I(inode
)->i_data_sem
);
2359 if (inode_bmap(inode
, block
, &epos
, &eloc
, &elen
, &offset
) ==
2360 (EXT_RECORDED_ALLOCATED
>> 30))
2361 ret
= udf_get_lb_pblock(inode
->i_sb
, &eloc
, offset
);
2365 up_read(&UDF_I(inode
)->i_data_sem
);
2368 if (UDF_QUERY_FLAG(inode
->i_sb
, UDF_FLAG_VARCONV
))
2369 return udf_fixed_to_variable(ret
);