hc
2024-11-01 2f529f9b558ca1c1bd74be7437a84e4711743404
kernel/fs/udf/inode.c
....@@ -438,6 +438,12 @@
438438 iinfo->i_next_alloc_goal++;
439439 }
440440
441
+ /*
442
+ * Block beyond EOF and prealloc extents? Just discard preallocation
443
+ * as it is not useful and complicates things.
444
+ */
445
+ if (((loff_t)block) << inode->i_blkbits > iinfo->i_lenExtents)
446
+ udf_discard_prealloc(inode);
441447 udf_clear_extent_cache(inode);
442448 phys = inode_getblk(inode, block, &err, &new);
443449 if (!phys)
....@@ -487,8 +493,6 @@
487493 uint32_t add;
488494 int count = 0, fake = !(last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
489495 struct super_block *sb = inode->i_sb;
490
- struct kernel_lb_addr prealloc_loc = {};
491
- uint32_t prealloc_len = 0;
492496 struct udf_inode_info *iinfo;
493497 int err;
494498
....@@ -507,19 +511,6 @@
507511 iinfo->i_lenExtents =
508512 (iinfo->i_lenExtents + sb->s_blocksize - 1) &
509513 ~(sb->s_blocksize - 1);
510
- }
511
-
512
- /* Last extent are just preallocated blocks? */
513
- if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
514
- EXT_NOT_RECORDED_ALLOCATED) {
515
- /* Save the extent so that we can reattach it to the end */
516
- prealloc_loc = last_ext->extLocation;
517
- prealloc_len = last_ext->extLength;
518
- /* Mark the extent as a hole */
519
- last_ext->extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
520
- (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
521
- last_ext->extLocation.logicalBlockNum = 0;
522
- last_ext->extLocation.partitionReferenceNum = 0;
523514 }
524515
525516 /* Can we merge with the previous extent? */
....@@ -549,7 +540,7 @@
549540 * more extents, we may need to enter possible following
550541 * empty indirect extent.
551542 */
552
- if (new_block_bytes || prealloc_len)
543
+ if (new_block_bytes)
553544 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
554545 }
555546
....@@ -583,17 +574,6 @@
583574 }
584575
585576 out:
586
- /* Do we have some preallocated blocks saved? */
587
- if (prealloc_len) {
588
- err = udf_add_aext(inode, last_pos, &prealloc_loc,
589
- prealloc_len, 1);
590
- if (err)
591
- return err;
592
- last_ext->extLocation = prealloc_loc;
593
- last_ext->extLength = prealloc_len;
594
- count++;
595
- }
596
-
597577 /* last_pos should point to the last written extent... */
598578 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
599579 last_pos->offset -= sizeof(struct short_ad);
....@@ -609,13 +589,17 @@
609589 static void udf_do_extend_final_block(struct inode *inode,
610590 struct extent_position *last_pos,
611591 struct kernel_long_ad *last_ext,
612
- uint32_t final_block_len)
592
+ uint32_t new_elen)
613593 {
614
- struct super_block *sb = inode->i_sb;
615594 uint32_t added_bytes;
616595
617
- added_bytes = final_block_len -
618
- (last_ext->extLength & (sb->s_blocksize - 1));
596
+ /*
597
+ * Extent already large enough? It may be already rounded up to block
598
+ * size...
599
+ */
600
+ if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
601
+ return;
602
+ added_bytes = (last_ext->extLength & UDF_EXTENT_LENGTH_MASK) - new_elen;
619603 last_ext->extLength += added_bytes;
620604 UDF_I(inode)->i_lenExtents += added_bytes;
621605
....@@ -632,12 +616,12 @@
632616 int8_t etype;
633617 struct super_block *sb = inode->i_sb;
634618 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
635
- unsigned long partial_final_block;
619
+ loff_t new_elen;
636620 int adsize;
637621 struct udf_inode_info *iinfo = UDF_I(inode);
638622 struct kernel_long_ad extent;
639623 int err = 0;
640
- int within_final_block;
624
+ bool within_last_ext;
641625
642626 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
643627 adsize = sizeof(struct short_ad);
....@@ -646,8 +630,17 @@
646630 else
647631 BUG();
648632
633
+ /*
634
+ * When creating hole in file, just don't bother with preserving
635
+ * preallocation. It likely won't be very useful anyway.
636
+ */
637
+ udf_discard_prealloc(inode);
638
+
649639 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
650
- within_final_block = (etype != -1);
640
+ within_last_ext = (etype != -1);
641
+ /* We don't expect extents past EOF... */
642
+ WARN_ON_ONCE(within_last_ext &&
643
+ elen > ((loff_t)offset + 1) << inode->i_blkbits);
651644
652645 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
653646 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
....@@ -663,19 +656,17 @@
663656 extent.extLength |= etype << 30;
664657 }
665658
666
- partial_final_block = newsize & (sb->s_blocksize - 1);
659
+ new_elen = ((loff_t)offset << inode->i_blkbits) |
660
+ (newsize & (sb->s_blocksize - 1));
667661
668662 /* File has extent covering the new size (could happen when extending
669663 * inside a block)?
670664 */
671
- if (within_final_block) {
665
+ if (within_last_ext) {
672666 /* Extending file within the last file block */
673
- udf_do_extend_final_block(inode, &epos, &extent,
674
- partial_final_block);
667
+ udf_do_extend_final_block(inode, &epos, &extent, new_elen);
675668 } else {
676
- loff_t add = ((loff_t)offset << sb->s_blocksize_bits) |
677
- partial_final_block;
678
- err = udf_do_extend_file(inode, &epos, &extent, add);
669
+ err = udf_do_extend_file(inode, &epos, &extent, new_elen);
679670 }
680671
681672 if (err < 0)
....@@ -776,10 +767,11 @@
776767 goto out_free;
777768 }
778769
779
- /* Are we beyond EOF? */
770
+ /* Are we beyond EOF and preallocated extent? */
780771 if (etype == -1) {
781772 int ret;
782773 loff_t hole_len;
774
+
783775 isBeyondEOF = true;
784776 if (count) {
785777 if (c)