hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
kernel/fs/udf/inode.c
....@@ -57,15 +57,15 @@
5757 static int udf_sync_inode(struct inode *inode);
5858 static int udf_alloc_i_data(struct inode *inode, size_t size);
5959 static sector_t inode_getblk(struct inode *, sector_t, int *, int *);
60
-static int8_t udf_insert_aext(struct inode *, struct extent_position,
61
- struct kernel_lb_addr, uint32_t);
60
+static int udf_insert_aext(struct inode *, struct extent_position,
61
+ struct kernel_lb_addr, uint32_t);
6262 static void udf_split_extents(struct inode *, int *, int, udf_pblk_t,
6363 struct kernel_long_ad *, int *);
6464 static void udf_prealloc_extents(struct inode *, int, int,
6565 struct kernel_long_ad *, int *);
6666 static void udf_merge_extents(struct inode *, struct kernel_long_ad *, int *);
67
-static void udf_update_extents(struct inode *, struct kernel_long_ad *, int,
68
- int, struct extent_position *);
67
+static int udf_update_extents(struct inode *, struct kernel_long_ad *, int,
68
+ int, struct extent_position *);
6969 static int udf_get_block(struct inode *, sector_t, struct buffer_head *, int);
7070
7171 static void __udf_clear_extent_cache(struct inode *inode)
....@@ -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
....@@ -509,19 +513,6 @@
509513 ~(sb->s_blocksize - 1);
510514 }
511515
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;
523
- }
524
-
525516 /* Can we merge with the previous extent? */
526517 if ((last_ext->extLength & UDF_EXTENT_FLAG_MASK) ==
527518 EXT_NOT_RECORDED_NOT_ALLOCATED) {
....@@ -534,8 +525,10 @@
534525 }
535526
536527 if (fake) {
537
- udf_add_aext(inode, last_pos, &last_ext->extLocation,
538
- last_ext->extLength, 1);
528
+ err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
529
+ last_ext->extLength, 1);
530
+ if (err < 0)
531
+ goto out_err;
539532 count++;
540533 } else {
541534 struct kernel_lb_addr tmploc;
....@@ -549,7 +542,7 @@
549542 * more extents, we may need to enter possible following
550543 * empty indirect extent.
551544 */
552
- if (new_block_bytes || prealloc_len)
545
+ if (new_block_bytes)
553546 udf_next_aext(inode, last_pos, &tmploc, &tmplen, 0);
554547 }
555548
....@@ -569,7 +562,7 @@
569562 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
570563 last_ext->extLength, 1);
571564 if (err)
572
- return err;
565
+ goto out_err;
573566 count++;
574567 }
575568 if (new_block_bytes) {
....@@ -578,22 +571,11 @@
578571 err = udf_add_aext(inode, last_pos, &last_ext->extLocation,
579572 last_ext->extLength, 1);
580573 if (err)
581
- return err;
574
+ goto out_err;
582575 count++;
583576 }
584577
585578 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
-
597579 /* last_pos should point to the last written extent... */
598580 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
599581 last_pos->offset -= sizeof(struct short_ad);
....@@ -603,19 +585,28 @@
603585 return -EIO;
604586
605587 return count;
588
+out_err:
589
+ /* Remove extents we've created so far */
590
+ udf_clear_extent_cache(inode);
591
+ udf_truncate_extents(inode);
592
+ return err;
606593 }
607594
608595 /* Extend the final block of the file to final_block_len bytes */
609596 static void udf_do_extend_final_block(struct inode *inode,
610597 struct extent_position *last_pos,
611598 struct kernel_long_ad *last_ext,
612
- uint32_t final_block_len)
599
+ uint32_t new_elen)
613600 {
614
- struct super_block *sb = inode->i_sb;
615601 uint32_t added_bytes;
616602
617
- added_bytes = final_block_len -
618
- (last_ext->extLength & (sb->s_blocksize - 1));
603
+ /*
604
+ * Extent already large enough? It may be already rounded up to block
605
+ * size...
606
+ */
607
+ if (new_elen <= (last_ext->extLength & UDF_EXTENT_LENGTH_MASK))
608
+ return;
609
+ added_bytes = new_elen - (last_ext->extLength & UDF_EXTENT_LENGTH_MASK);
619610 last_ext->extLength += added_bytes;
620611 UDF_I(inode)->i_lenExtents += added_bytes;
621612
....@@ -632,12 +623,12 @@
632623 int8_t etype;
633624 struct super_block *sb = inode->i_sb;
634625 sector_t first_block = newsize >> sb->s_blocksize_bits, offset;
635
- unsigned long partial_final_block;
626
+ loff_t new_elen;
636627 int adsize;
637628 struct udf_inode_info *iinfo = UDF_I(inode);
638629 struct kernel_long_ad extent;
639630 int err = 0;
640
- int within_final_block;
631
+ bool within_last_ext;
641632
642633 if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_SHORT)
643634 adsize = sizeof(struct short_ad);
....@@ -646,8 +637,17 @@
646637 else
647638 BUG();
648639
640
+ /*
641
+ * When creating hole in file, just don't bother with preserving
642
+ * preallocation. It likely won't be very useful anyway.
643
+ */
644
+ udf_discard_prealloc(inode);
645
+
649646 etype = inode_bmap(inode, first_block, &epos, &eloc, &elen, &offset);
650
- within_final_block = (etype != -1);
647
+ within_last_ext = (etype != -1);
648
+ /* We don't expect extents past EOF... */
649
+ WARN_ON_ONCE(within_last_ext &&
650
+ elen > ((loff_t)offset + 1) << inode->i_blkbits);
651651
652652 if ((!epos.bh && epos.offset == udf_file_entry_alloc_offset(inode)) ||
653653 (epos.bh && epos.offset == sizeof(struct allocExtDesc))) {
....@@ -663,19 +663,17 @@
663663 extent.extLength |= etype << 30;
664664 }
665665
666
- partial_final_block = newsize & (sb->s_blocksize - 1);
666
+ new_elen = ((loff_t)offset << inode->i_blkbits) |
667
+ (newsize & (sb->s_blocksize - 1));
667668
668669 /* File has extent covering the new size (could happen when extending
669670 * inside a block)?
670671 */
671
- if (within_final_block) {
672
+ if (within_last_ext) {
672673 /* Extending file within the last file block */
673
- udf_do_extend_final_block(inode, &epos, &extent,
674
- partial_final_block);
674
+ udf_do_extend_final_block(inode, &epos, &extent, new_elen);
675675 } 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);
676
+ err = udf_do_extend_file(inode, &epos, &extent, new_elen);
679677 }
680678
681679 if (err < 0)
....@@ -697,7 +695,7 @@
697695 struct kernel_lb_addr eloc, tmpeloc;
698696 int c = 1;
699697 loff_t lbcount = 0, b_off = 0;
700
- udf_pblk_t newblocknum, newblock;
698
+ udf_pblk_t newblocknum, newblock = 0;
701699 sector_t offset = 0;
702700 int8_t etype;
703701 struct udf_inode_info *iinfo = UDF_I(inode);
....@@ -776,10 +774,11 @@
776774 goto out_free;
777775 }
778776
779
- /* Are we beyond EOF? */
777
+ /* Are we beyond EOF and preallocated extent? */
780778 if (etype == -1) {
781779 int ret;
782780 loff_t hole_len;
781
+
783782 isBeyondEOF = true;
784783 if (count) {
785784 if (c)
....@@ -799,25 +798,22 @@
799798 ret = udf_do_extend_file(inode, &prev_epos, laarr, hole_len);
800799 if (ret < 0) {
801800 *err = ret;
802
- newblock = 0;
803801 goto out_free;
804802 }
805803 c = 0;
806804 offset = 0;
807805 count += ret;
808
- /* We are not covered by a preallocated extent? */
809
- if ((laarr[0].extLength & UDF_EXTENT_FLAG_MASK) !=
810
- EXT_NOT_RECORDED_ALLOCATED) {
811
- /* Is there any real extent? - otherwise we overwrite
812
- * the fake one... */
813
- if (count)
814
- c = !c;
815
- laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
816
- inode->i_sb->s_blocksize;
817
- memset(&laarr[c].extLocation, 0x00,
818
- sizeof(struct kernel_lb_addr));
819
- count++;
820
- }
806
+ /*
807
+ * Is there any real extent? - otherwise we overwrite the fake
808
+ * one...
809
+ */
810
+ if (count)
811
+ c = !c;
812
+ laarr[c].extLength = EXT_NOT_RECORDED_NOT_ALLOCATED |
813
+ inode->i_sb->s_blocksize;
814
+ memset(&laarr[c].extLocation, 0x00,
815
+ sizeof(struct kernel_lb_addr));
816
+ count++;
821817 endnum = c + 1;
822818 lastblock = 1;
823819 } else {
....@@ -864,7 +860,6 @@
864860 goal, err);
865861 if (!newblocknum) {
866862 *err = -ENOSPC;
867
- newblock = 0;
868863 goto out_free;
869864 }
870865 if (isBeyondEOF)
....@@ -890,7 +885,9 @@
890885 /* write back the new extents, inserting new extents if the new number
891886 * of extents is greater than the old number, and deleting extents if
892887 * the new number of extents is less than the old number */
893
- udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
888
+ *err = udf_update_extents(inode, laarr, startnum, endnum, &prev_epos);
889
+ if (*err < 0)
890
+ goto out_free;
894891
895892 newblock = udf_get_pblock(inode->i_sb, newblocknum,
896893 iinfo->i_location.partitionReferenceNum, 0);
....@@ -1094,23 +1091,8 @@
10941091 blocksize - 1) >> blocksize_bits)))) {
10951092
10961093 if (((li->extLength & UDF_EXTENT_LENGTH_MASK) +
1097
- (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1098
- blocksize - 1) & ~UDF_EXTENT_LENGTH_MASK) {
1099
- lip1->extLength = (lip1->extLength -
1100
- (li->extLength &
1101
- UDF_EXTENT_LENGTH_MASK) +
1102
- UDF_EXTENT_LENGTH_MASK) &
1103
- ~(blocksize - 1);
1104
- li->extLength = (li->extLength &
1105
- UDF_EXTENT_FLAG_MASK) +
1106
- (UDF_EXTENT_LENGTH_MASK + 1) -
1107
- blocksize;
1108
- lip1->extLocation.logicalBlockNum =
1109
- li->extLocation.logicalBlockNum +
1110
- ((li->extLength &
1111
- UDF_EXTENT_LENGTH_MASK) >>
1112
- blocksize_bits);
1113
- } else {
1094
+ (lip1->extLength & UDF_EXTENT_LENGTH_MASK) +
1095
+ blocksize - 1) <= UDF_EXTENT_LENGTH_MASK) {
11141096 li->extLength = lip1->extLength +
11151097 (((li->extLength &
11161098 UDF_EXTENT_LENGTH_MASK) +
....@@ -1173,21 +1155,30 @@
11731155 }
11741156 }
11751157
1176
-static void udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1177
- int startnum, int endnum,
1178
- struct extent_position *epos)
1158
+static int udf_update_extents(struct inode *inode, struct kernel_long_ad *laarr,
1159
+ int startnum, int endnum,
1160
+ struct extent_position *epos)
11791161 {
11801162 int start = 0, i;
11811163 struct kernel_lb_addr tmploc;
11821164 uint32_t tmplen;
1165
+ int err;
11831166
11841167 if (startnum > endnum) {
11851168 for (i = 0; i < (startnum - endnum); i++)
11861169 udf_delete_aext(inode, *epos);
11871170 } else if (startnum < endnum) {
11881171 for (i = 0; i < (endnum - startnum); i++) {
1189
- udf_insert_aext(inode, *epos, laarr[i].extLocation,
1190
- laarr[i].extLength);
1172
+ err = udf_insert_aext(inode, *epos,
1173
+ laarr[i].extLocation,
1174
+ laarr[i].extLength);
1175
+ /*
1176
+ * If we fail here, we are likely corrupting the extent
1177
+ * list and leaking blocks. At least stop early to
1178
+ * limit the damage.
1179
+ */
1180
+ if (err < 0)
1181
+ return err;
11911182 udf_next_aext(inode, epos, &laarr[i].extLocation,
11921183 &laarr[i].extLength, 1);
11931184 start++;
....@@ -1199,6 +1190,7 @@
11991190 udf_write_aext(inode, epos, &laarr[i].extLocation,
12001191 laarr[i].extLength, 1);
12011192 }
1193
+ return 0;
12021194 }
12031195
12041196 struct buffer_head *udf_bread(struct inode *inode, udf_pblk_t block,
....@@ -1401,6 +1393,7 @@
14011393 ret = -EIO;
14021394 goto out;
14031395 }
1396
+ iinfo->i_hidden = hidden_inode;
14041397 iinfo->i_unique = 0;
14051398 iinfo->i_lenEAttr = 0;
14061399 iinfo->i_lenExtents = 0;
....@@ -1736,8 +1729,12 @@
17361729
17371730 if (S_ISDIR(inode->i_mode) && inode->i_nlink > 0)
17381731 fe->fileLinkCount = cpu_to_le16(inode->i_nlink - 1);
1739
- else
1740
- fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1732
+ else {
1733
+ if (iinfo->i_hidden)
1734
+ fe->fileLinkCount = cpu_to_le16(0);
1735
+ else
1736
+ fe->fileLinkCount = cpu_to_le16(inode->i_nlink);
1737
+ }
17411738
17421739 fe->informationLength = cpu_to_le64(inode->i_size);
17431740
....@@ -1908,8 +1905,13 @@
19081905 if (!inode)
19091906 return ERR_PTR(-ENOMEM);
19101907
1911
- if (!(inode->i_state & I_NEW))
1908
+ if (!(inode->i_state & I_NEW)) {
1909
+ if (UDF_I(inode)->i_hidden != hidden_inode) {
1910
+ iput(inode);
1911
+ return ERR_PTR(-EFSCORRUPTED);
1912
+ }
19121913 return inode;
1914
+ }
19131915
19141916 memcpy(&UDF_I(inode)->i_location, ino, sizeof(struct kernel_lb_addr));
19151917 err = udf_read_inode(inode, hidden_inode);
....@@ -2223,12 +2225,13 @@
22232225 return etype;
22242226 }
22252227
2226
-static int8_t udf_insert_aext(struct inode *inode, struct extent_position epos,
2227
- struct kernel_lb_addr neloc, uint32_t nelen)
2228
+static int udf_insert_aext(struct inode *inode, struct extent_position epos,
2229
+ struct kernel_lb_addr neloc, uint32_t nelen)
22282230 {
22292231 struct kernel_lb_addr oeloc;
22302232 uint32_t oelen;
22312233 int8_t etype;
2234
+ int err;
22322235
22332236 if (epos.bh)
22342237 get_bh(epos.bh);
....@@ -2238,10 +2241,10 @@
22382241 neloc = oeloc;
22392242 nelen = (etype << 30) | oelen;
22402243 }
2241
- udf_add_aext(inode, &epos, &neloc, nelen, 1);
2244
+ err = udf_add_aext(inode, &epos, &neloc, nelen, 1);
22422245 brelse(epos.bh);
22432246
2244
- return (nelen >> 30);
2247
+ return err;
22452248 }
22462249
22472250 int8_t udf_delete_aext(struct inode *inode, struct extent_position epos)