Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge tag 'ntfs3_for_7.0' of https://github.com/Paragon-Software-Group/linux-ntfs3

Pull ntfs3 updates from Konstantin Komarov:
"New code:
- improve readahead for bitmap initialization and large directory scans
- fsync files by syncing parent inodes
- drop of preallocated clusters for sparse and compressed files
- zero-fill folios beyond i_valid in ntfs_read_folio()
- implement llseek SEEK_DATA/SEEK_HOLE by scanning data runs
- implement iomap-based file operations
- allow explicit boolean acl/prealloc mount options
- fall-through between switch labels
- delayed-allocation (delalloc) support

Fixes:
- check return value of indx_find to avoid infinite loop
- initialize new folios before use
- infinite loop in attr_load_runs_range on inconsistent metadata
- infinite loop triggered by zero-sized ATTR_LIST
- ntfs_mount_options leak in ntfs_fill_super()
- deadlock in ni_read_folio_cmpr
- circular locking dependency in run_unpack_ex
- prevent infinite loops caused by the next valid being the same
- restore NULL folio initialization in ntfs_writepages()
- slab-out-of-bounds read in DeleteIndexEntryRoot

Updates:
- allow readdir() to finish after directory mutations without rewinddir()
- handle attr_set_size() errors when truncating files
- make ntfs_writeback_ops static
- refactor duplicate kmemdup pattern in do_action()
- avoid calling run_get_entry() when run == NULL in ntfs_read_run_nb_ra()

Replaced:
- use wait_on_buffer() directly
- rename ni_readpage_cmpr into ni_read_folio_cmpr"

* tag 'ntfs3_for_7.0' of https://github.com/Paragon-Software-Group/linux-ntfs3: (26 commits)
fs/ntfs3: add delayed-allocation (delalloc) support
fs/ntfs3: avoid calling run_get_entry() when run == NULL in ntfs_read_run_nb_ra()
fs/ntfs3: add fall-through between switch labels
fs/ntfs3: allow explicit boolean acl/prealloc mount options
fs/ntfs3: Fix slab-out-of-bounds read in DeleteIndexEntryRoot
ntfs3: Restore NULL folio initialization in ntfs_writepages()
ntfs3: Refactor duplicate kmemdup pattern in do_action()
fs/ntfs3: prevent infinite loops caused by the next valid being the same
fs/ntfs3: make ntfs_writeback_ops static
ntfs3: fix circular locking dependency in run_unpack_ex
fs/ntfs3: implement iomap-based file operations
fs/ntfs3: fix deadlock in ni_read_folio_cmpr
fs/ntfs3: implement llseek SEEK_DATA/SEEK_HOLE by scanning data runs
fs/ntfs3: zero-fill folios beyond i_valid in ntfs_read_folio()
fs/ntfs3: handle attr_set_size() errors when truncating files
fs/ntfs3: drop preallocated clusters for sparse and compressed files
fs/ntfs3: fsync files by syncing parent inodes
fs/ntfs3: fix ntfs_mount_options leak in ntfs_fill_super()
fs/ntfs3: allow readdir() to finish after directory mutations without rewinddir()
fs/ntfs3: improve readahead for bitmap initialization and large directory scans
...

+1844 -1156
+282 -130
fs/ntfs3/attrib.c
··· 91 91 * run_deallocate_ex - Deallocate clusters. 92 92 */ 93 93 static int run_deallocate_ex(struct ntfs_sb_info *sbi, struct runs_tree *run, 94 - CLST vcn, CLST len, CLST *done, bool trim) 94 + CLST vcn, CLST len, CLST *done, bool trim, 95 + struct runs_tree *run_da) 95 96 { 96 97 int err = 0; 97 98 CLST vcn_next, vcn0 = vcn, lcn, clen, dn = 0; ··· 121 120 if (sbi) { 122 121 /* mark bitmap range [lcn + clen) as free and trim clusters. */ 123 122 mark_as_free_ex(sbi, lcn, clen, trim); 123 + 124 + if (run_da) { 125 + CLST da_len; 126 + if (!run_remove_range(run_da, vcn, clen, 127 + &da_len)) { 128 + err = -ENOMEM; 129 + goto failed; 130 + } 131 + ntfs_sub_da(sbi, da_len); 132 + } 124 133 } 125 134 dn += clen; 126 135 } ··· 158 147 * attr_allocate_clusters - Find free space, mark it as used and store in @run. 159 148 */ 160 149 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run, 161 - CLST vcn, CLST lcn, CLST len, CLST *pre_alloc, 162 - enum ALLOCATE_OPT opt, CLST *alen, const size_t fr, 163 - CLST *new_lcn, CLST *new_len) 150 + struct runs_tree *run_da, CLST vcn, CLST lcn, 151 + CLST len, CLST *pre_alloc, enum ALLOCATE_OPT opt, 152 + CLST *alen, const size_t fr, CLST *new_lcn, 153 + CLST *new_len) 164 154 { 165 155 int err; 166 156 CLST flen, vcn0 = vcn, pre = pre_alloc ? *pre_alloc : 0; ··· 178 166 continue; 179 167 } 180 168 169 + if (err == -ENOSPC && new_len && vcn - vcn0) { 170 + /* Keep already allocated clusters. */ 171 + *alen = vcn - vcn0; 172 + return 0; 173 + } 174 + 181 175 if (err) 182 176 goto out; 183 177 ··· 197 179 198 180 /* Add new fragment into run storage. */ 199 181 if (!run_add_entry(run, vcn, lcn, flen, opt & ALLOCATE_MFT)) { 182 + undo_alloc: 200 183 /* Undo last 'ntfs_look_for_free_space' */ 201 184 mark_as_free_ex(sbi, lcn, len, false); 202 185 err = -ENOMEM; 203 186 goto out; 187 + } 188 + 189 + if (run_da) { 190 + CLST da_len; 191 + if (!run_remove_range(run_da, vcn, flen, &da_len)) { 192 + goto undo_alloc; 193 + } 194 + ntfs_sub_da(sbi, da_len); 204 195 } 205 196 206 197 if (opt & ALLOCATE_ZERO) { ··· 226 199 vcn += flen; 227 200 228 201 if (flen >= len || (opt & ALLOCATE_MFT) || 229 - (fr && run->count - cnt >= fr)) { 202 + (opt & ALLOCATE_ONE_FR) || (fr && run->count - cnt >= fr)) { 230 203 *alen = vcn - vcn0; 231 204 return 0; 232 205 } ··· 237 210 out: 238 211 /* Undo 'ntfs_look_for_free_space' */ 239 212 if (vcn - vcn0) { 240 - run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false); 213 + run_deallocate_ex(sbi, run, vcn0, vcn - vcn0, NULL, false, 214 + run_da); 241 215 run_truncate(run, vcn0); 242 216 } 243 217 ··· 303 275 } else { 304 276 const char *data = resident_data(attr); 305 277 306 - err = attr_allocate_clusters(sbi, run, 0, 0, len, NULL, 278 + err = attr_allocate_clusters(sbi, run, NULL, 0, 0, len, NULL, 307 279 ALLOCATE_DEF, &alen, 0, NULL, 308 280 NULL); 309 281 if (err) ··· 419 391 } 420 392 421 393 /* 422 - * attr_set_size - Change the size of attribute. 394 + * attr_set_size_ex - Change the size of attribute. 423 395 * 424 396 * Extend: 425 397 * - Sparse/compressed: No allocated clusters. ··· 427 399 * Shrink: 428 400 * - No deallocate if @keep_prealloc is set. 429 401 */ 430 - int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type, 431 - const __le16 *name, u8 name_len, struct runs_tree *run, 432 - u64 new_size, const u64 *new_valid, bool keep_prealloc, 433 - struct ATTRIB **ret) 402 + int attr_set_size_ex(struct ntfs_inode *ni, enum ATTR_TYPE type, 403 + const __le16 *name, u8 name_len, struct runs_tree *run, 404 + u64 new_size, const u64 *new_valid, bool keep_prealloc, 405 + struct ATTRIB **ret, bool no_da) 434 406 { 435 407 int err = 0; 436 408 struct ntfs_sb_info *sbi = ni->mi.sbi; 437 409 u8 cluster_bits = sbi->cluster_bits; 438 410 bool is_mft = ni->mi.rno == MFT_REC_MFT && type == ATTR_DATA && 439 411 !name_len; 440 - u64 old_valid, old_size, old_alloc, new_alloc, new_alloc_tmp; 412 + u64 old_valid, old_size, old_alloc, new_alloc_tmp; 413 + u64 new_alloc = 0; 441 414 struct ATTRIB *attr = NULL, *attr_b; 442 415 struct ATTR_LIST_ENTRY *le, *le_b; 443 416 struct mft_inode *mi, *mi_b; 444 417 CLST alen, vcn, lcn, new_alen, old_alen, svcn, evcn; 445 418 CLST next_svcn, pre_alloc = -1, done = 0; 446 - bool is_ext, is_bad = false; 419 + bool is_ext = false, is_bad = false; 447 420 bool dirty = false; 421 + struct runs_tree *run_da = run == &ni->file.run ? &ni->file.run_da : 422 + NULL; 423 + bool da = !is_mft && sbi->options->delalloc && run_da && !no_da; 448 424 u32 align; 449 425 struct MFT_REC *rec; 450 426 ··· 480 448 481 449 is_ext = is_attr_ext(attr_b); 482 450 align = sbi->cluster_size; 483 - if (is_ext) 451 + if (is_ext) { 484 452 align <<= attr_b->nres.c_unit; 453 + keep_prealloc = false; 454 + da = false; 455 + } 485 456 486 457 old_valid = le64_to_cpu(attr_b->nres.valid_size); 487 458 old_size = le64_to_cpu(attr_b->nres.data_size); ··· 500 465 attr_b->nres.data_size = cpu_to_le64(new_size); 501 466 mi_b->dirty = dirty = true; 502 467 goto ok; 468 + } 469 + 470 + if (da && 471 + (vcn = old_alen + run_len(&ni->file.run_da), new_alen > vcn)) { 472 + /* Resize up normal file. Delay new clusters allocation. */ 473 + alen = new_alen - vcn; 474 + 475 + if (ntfs_check_free_space(sbi, alen, 0, true)) { 476 + if (!run_add_entry(&ni->file.run_da, vcn, SPARSE_LCN, 477 + alen, false)) { 478 + err = -ENOMEM; 479 + goto out; 480 + } 481 + 482 + ntfs_add_da(sbi, alen); 483 + goto ok1; 484 + } 485 + } 486 + 487 + if (!keep_prealloc && run_da && run_da->count && 488 + (vcn = run_get_max_vcn(run_da), new_alen < vcn)) { 489 + /* Shrink delayed clusters. */ 490 + 491 + /* Try to remove fragment from delay allocated run. */ 492 + if (!run_remove_range(run_da, new_alen, vcn - new_alen, 493 + &alen)) { 494 + err = -ENOMEM; 495 + goto out; 496 + } 497 + 498 + ntfs_sub_da(sbi, alen); 503 499 } 504 500 505 501 vcn = old_alen - 1; ··· 638 572 } else { 639 573 /* ~3 bytes per fragment. */ 640 574 err = attr_allocate_clusters( 641 - sbi, run, vcn, lcn, to_allocate, &pre_alloc, 575 + sbi, run, run_da, vcn, lcn, to_allocate, 576 + &pre_alloc, 642 577 is_mft ? ALLOCATE_MFT : ALLOCATE_DEF, &alen, 643 578 is_mft ? 0 : 644 579 (sbi->record_size - ··· 818 751 mi_b->dirty = dirty = true; 819 752 820 753 err = run_deallocate_ex(sbi, run, vcn, evcn - vcn + 1, &dlen, 821 - true); 754 + true, run_da); 822 755 if (err) 823 756 goto out; 824 757 825 758 if (is_ext) { 826 759 /* dlen - really deallocated clusters. */ 827 760 le64_sub_cpu(&attr_b->nres.total_size, 828 - ((u64)dlen << cluster_bits)); 761 + (u64)dlen << cluster_bits); 829 762 } 830 763 831 764 run_truncate(run, vcn); ··· 880 813 if (((type == ATTR_DATA && !name_len) || 881 814 (type == ATTR_ALLOC && name == I30_NAME))) { 882 815 /* Update inode_set_bytes. */ 883 - if (attr_b->non_res) { 884 - new_alloc = le64_to_cpu(attr_b->nres.alloc_size); 885 - if (inode_get_bytes(&ni->vfs_inode) != new_alloc) { 886 - inode_set_bytes(&ni->vfs_inode, new_alloc); 887 - dirty = true; 888 - } 816 + if (attr_b->non_res && 817 + inode_get_bytes(&ni->vfs_inode) != new_alloc) { 818 + inode_set_bytes(&ni->vfs_inode, new_alloc); 819 + dirty = true; 889 820 } 821 + 822 + i_size_write(&ni->vfs_inode, new_size); 890 823 891 824 /* Don't forget to update duplicate information in parent. */ 892 825 if (dirty) { ··· 928 861 is_bad = true; 929 862 930 863 undo_1: 931 - run_deallocate_ex(sbi, run, vcn, alen, NULL, false); 864 + run_deallocate_ex(sbi, run, vcn, alen, NULL, false, run_da); 932 865 933 866 run_truncate(run, vcn); 934 867 out: ··· 951 884 * - new allocated clusters are zeroed via blkdev_issue_zeroout. 952 885 */ 953 886 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn, 954 - CLST *len, bool *new, bool zero) 887 + CLST *len, bool *new, bool zero, void **res, bool no_da) 955 888 { 956 - int err = 0; 957 - struct runs_tree *run = &ni->file.run; 958 - struct ntfs_sb_info *sbi; 959 - u8 cluster_bits; 960 - struct ATTRIB *attr, *attr_b; 961 - struct ATTR_LIST_ENTRY *le, *le_b; 962 - struct mft_inode *mi, *mi_b; 963 - CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0, alen; 964 - CLST alloc, evcn; 965 - unsigned fr; 966 - u64 total_size, total_size0; 967 - int step = 0; 889 + int err; 968 890 969 891 if (new) 970 892 *new = false; 893 + if (res) 894 + *res = NULL; 971 895 972 896 /* Try to find in cache. */ 973 897 down_read(&ni->file.run_lock); 974 - if (!run_lookup_entry(run, vcn, lcn, len, NULL)) 898 + if (!no_da && run_lookup_entry(&ni->file.run_da, vcn, lcn, len, NULL)) { 899 + /* The requested vcn is delay allocated. */ 900 + *lcn = DELALLOC_LCN; 901 + } else if (run_lookup_entry(&ni->file.run, vcn, lcn, len, NULL)) { 902 + /* The requested vcn is known in current run. */ 903 + } else { 975 904 *len = 0; 905 + } 976 906 up_read(&ni->file.run_lock); 977 907 978 908 if (*len && (*lcn != SPARSE_LCN || !new)) 979 909 return 0; /* Fast normal way without allocation. */ 980 910 981 911 /* No cluster in cache or we need to allocate cluster in hole. */ 982 - sbi = ni->mi.sbi; 983 - cluster_bits = sbi->cluster_bits; 984 - 985 912 ni_lock(ni); 986 913 down_write(&ni->file.run_lock); 987 914 988 - /* Repeat the code above (under write lock). */ 989 - if (!run_lookup_entry(run, vcn, lcn, len, NULL)) 915 + err = attr_data_get_block_locked(ni, vcn, clen, lcn, len, new, zero, 916 + res, no_da); 917 + 918 + up_write(&ni->file.run_lock); 919 + ni_unlock(ni); 920 + 921 + return err; 922 + } 923 + 924 + /* 925 + * attr_data_get_block_locked - Helper for attr_data_get_block. 926 + */ 927 + int attr_data_get_block_locked(struct ntfs_inode *ni, CLST vcn, CLST clen, 928 + CLST *lcn, CLST *len, bool *new, bool zero, 929 + void **res, bool no_da) 930 + { 931 + int err = 0; 932 + struct ntfs_sb_info *sbi = ni->mi.sbi; 933 + struct runs_tree *run = &ni->file.run; 934 + struct runs_tree *run_da = &ni->file.run_da; 935 + bool da = sbi->options->delalloc && !no_da; 936 + u8 cluster_bits; 937 + struct ATTRIB *attr, *attr_b; 938 + struct ATTR_LIST_ENTRY *le, *le_b; 939 + struct mft_inode *mi, *mi_b; 940 + CLST hint, svcn, to_alloc, evcn1, next_svcn, asize, end, vcn0; 941 + CLST alloc, evcn; 942 + unsigned fr; 943 + u64 total_size, total_size0; 944 + int step; 945 + 946 + again: 947 + if (da && run_lookup_entry(run_da, vcn, lcn, len, NULL)) { 948 + /* The requested vcn is delay allocated. */ 949 + *lcn = DELALLOC_LCN; 950 + } else if (run_lookup_entry(run, vcn, lcn, len, NULL)) { 951 + /* The requested vcn is known in current run. */ 952 + } else { 990 953 *len = 0; 954 + } 991 955 992 956 if (*len) { 993 957 if (*lcn != SPARSE_LCN || !new) ··· 1026 928 if (clen > *len) 1027 929 clen = *len; 1028 930 } 931 + 932 + cluster_bits = sbi->cluster_bits; 933 + step = 0; 1029 934 1030 935 le_b = NULL; 1031 936 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, &mi_b); ··· 1038 937 } 1039 938 1040 939 if (!attr_b->non_res) { 940 + u32 data_size = le32_to_cpu(attr_b->res.data_size); 1041 941 *lcn = RESIDENT_LCN; 1042 - *len = 1; 942 + *len = data_size; 943 + if (res && data_size) { 944 + *res = kmemdup(resident_data(attr_b), data_size, 945 + GFP_KERNEL); 946 + if (!*res) 947 + err = -ENOMEM; 948 + } 1043 949 goto out; 1044 950 } 1045 951 ··· 1056 948 err = -EINVAL; 1057 949 } else { 1058 950 *len = 1; 1059 - *lcn = SPARSE_LCN; 951 + *lcn = EOF_LCN; 1060 952 } 1061 953 goto out; 1062 954 } ··· 1134 1026 to_alloc = ((vcn0 + clen + clst_per_frame - 1) & cmask) - vcn; 1135 1027 if (fr < clst_per_frame) 1136 1028 fr = clst_per_frame; 1137 - zero = true; 1029 + if (vcn != vcn0) 1030 + zero = true; 1138 1031 1139 1032 /* Check if 'vcn' and 'vcn0' in different attribute segments. */ 1140 1033 if (vcn < svcn || evcn1 <= vcn) { ··· 1152 1043 if (err) 1153 1044 goto out; 1154 1045 } 1046 + da = false; /* no delalloc for compressed file. */ 1155 1047 } 1156 1048 1157 1049 if (vcn + to_alloc > asize) 1158 1050 to_alloc = asize - vcn; 1051 + 1052 + if (da) { 1053 + CLST rlen1, rlen2; 1054 + if (!ntfs_check_free_space(sbi, to_alloc, 0, true)) { 1055 + err = ni_allocate_da_blocks_locked(ni); 1056 + if (err) 1057 + goto out; 1058 + /* Layout of records may be changed. Start again without 'da'. */ 1059 + da = false; 1060 + goto again; 1061 + } 1062 + 1063 + /* run_add_entry consolidates existed ranges. */ 1064 + rlen1 = run_len(run_da); 1065 + if (!run_add_entry(run_da, vcn, SPARSE_LCN, to_alloc, false)) { 1066 + err = -ENOMEM; 1067 + goto out; 1068 + } 1069 + rlen2 = run_len(run_da); 1070 + 1071 + /* new added delay clusters = rlen2 - rlen1. */ 1072 + ntfs_add_da(sbi, rlen2 - rlen1); 1073 + *len = to_alloc; 1074 + *lcn = DELALLOC_LCN; 1075 + goto ok; 1076 + } 1159 1077 1160 1078 /* Get the last LCN to allocate from. */ 1161 1079 hint = 0; ··· 1198 1062 } 1199 1063 1200 1064 /* Allocate and zeroout new clusters. */ 1201 - err = attr_allocate_clusters(sbi, run, vcn, hint + 1, to_alloc, NULL, 1202 - zero ? ALLOCATE_ZERO : ALLOCATE_DEF, &alen, 1203 - fr, lcn, len); 1065 + err = attr_allocate_clusters(sbi, run, run_da, vcn, hint + 1, to_alloc, 1066 + NULL, 1067 + zero ? ALLOCATE_ZERO : ALLOCATE_ONE_FR, 1068 + len, fr, lcn, len); 1204 1069 if (err) 1205 1070 goto out; 1206 1071 *new = true; 1207 1072 step = 1; 1208 1073 1209 - end = vcn + alen; 1074 + end = vcn + *len; 1210 1075 /* Save 'total_size0' to restore if error. */ 1211 1076 total_size0 = le64_to_cpu(attr_b->nres.total_size); 1212 - total_size = total_size0 + ((u64)alen << cluster_bits); 1077 + total_size = total_size0 + ((u64)*len << cluster_bits); 1213 1078 1214 1079 if (vcn != vcn0) { 1215 1080 if (!run_lookup_entry(run, vcn0, lcn, len, NULL)) { ··· 1276 1139 * in 'ni_insert_nonresident'. 1277 1140 * Return in advance -ENOSPC here if there are no free cluster and no free MFT. 1278 1141 */ 1279 - if (!ntfs_check_for_free_space(sbi, 1, 1)) { 1142 + if (!ntfs_check_free_space(sbi, 1, 1, false)) { 1280 1143 /* Undo step 1. */ 1281 1144 err = -ENOSPC; 1282 1145 goto undo1; ··· 1361 1224 /* Too complex to restore. */ 1362 1225 _ntfs_bad_inode(&ni->vfs_inode); 1363 1226 } 1364 - up_write(&ni->file.run_lock); 1365 - ni_unlock(ni); 1366 1227 1367 1228 return err; 1368 1229 ··· 1369 1234 attr_b->nres.total_size = cpu_to_le64(total_size0); 1370 1235 inode_set_bytes(&ni->vfs_inode, total_size0); 1371 1236 1372 - if (run_deallocate_ex(sbi, run, vcn, alen, NULL, false) || 1373 - !run_add_entry(run, vcn, SPARSE_LCN, alen, false) || 1237 + if (run_deallocate_ex(sbi, run, vcn, *len, NULL, false, run_da) || 1238 + !run_add_entry(run, vcn, SPARSE_LCN, *len, false) || 1374 1239 mi_pack_runs(mi, attr, run, max(end, evcn1) - svcn)) { 1375 1240 _ntfs_bad_inode(&ni->vfs_inode); 1376 1241 } 1377 1242 goto out; 1378 - } 1379 - 1380 - int attr_data_read_resident(struct ntfs_inode *ni, struct folio *folio) 1381 - { 1382 - u64 vbo; 1383 - struct ATTRIB *attr; 1384 - u32 data_size; 1385 - size_t len; 1386 - 1387 - attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, NULL); 1388 - if (!attr) 1389 - return -EINVAL; 1390 - 1391 - if (attr->non_res) 1392 - return E_NTFS_NONRESIDENT; 1393 - 1394 - vbo = folio->index << PAGE_SHIFT; 1395 - data_size = le32_to_cpu(attr->res.data_size); 1396 - if (vbo > data_size) 1397 - len = 0; 1398 - else 1399 - len = min(data_size - vbo, folio_size(folio)); 1400 - 1401 - folio_fill_tail(folio, 0, resident_data(attr) + vbo, len); 1402 - folio_mark_uptodate(folio); 1403 - 1404 - return 0; 1405 1243 } 1406 1244 1407 1245 int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio) ··· 1393 1285 return E_NTFS_NONRESIDENT; 1394 1286 } 1395 1287 1396 - vbo = folio->index << PAGE_SHIFT; 1288 + vbo = folio_pos(folio); 1397 1289 data_size = le32_to_cpu(attr->res.data_size); 1398 1290 if (vbo < data_size) { 1399 1291 char *data = resident_data(attr); ··· 1462 1354 CLST vcn; 1463 1355 CLST vcn_last = (to - 1) >> cluster_bits; 1464 1356 CLST lcn, clen; 1465 - int err; 1357 + int err = 0; 1358 + int retry = 0; 1466 1359 1467 1360 for (vcn = from >> cluster_bits; vcn <= vcn_last; vcn += clen) { 1468 - if (!run_lookup_entry(run, vcn, &lcn, &clen, NULL)) { 1469 - err = attr_load_runs_vcn(ni, type, name, name_len, run, 1470 - vcn); 1471 - if (err) 1472 - return err; 1473 - clen = 0; /* Next run_lookup_entry(vcn) must be success. */ 1361 + if (run_lookup_entry(run, vcn, &lcn, &clen, NULL)) { 1362 + retry = 0; 1363 + continue; 1474 1364 } 1365 + if (retry) { 1366 + err = -EINVAL; 1367 + break; 1368 + } 1369 + err = attr_load_runs_vcn(ni, type, name, name_len, run, vcn); 1370 + if (err) 1371 + break; 1372 + 1373 + clen = 0; /* Next run_lookup_entry(vcn) must be success. */ 1374 + retry++; 1475 1375 } 1476 1376 1477 - return 0; 1377 + return err; 1478 1378 } 1479 1379 1480 1380 #ifdef CONFIG_NTFS3_LZX_XPRESS ··· 1805 1689 1806 1690 if (len < clst_data) { 1807 1691 err = run_deallocate_ex(sbi, run, vcn + len, clst_data - len, 1808 - NULL, true); 1692 + NULL, true, NULL); 1809 1693 if (err) 1810 1694 goto out; 1811 1695 ··· 1825 1709 hint = -1; 1826 1710 } 1827 1711 1828 - err = attr_allocate_clusters(sbi, run, vcn + clst_data, 1712 + err = attr_allocate_clusters(sbi, run, NULL, vcn + clst_data, 1829 1713 hint + 1, len - clst_data, NULL, 1830 1714 ALLOCATE_DEF, &alen, 0, NULL, 1831 1715 NULL); ··· 1980 1864 CLST vcn, end; 1981 1865 u64 valid_size, data_size, alloc_size, total_size; 1982 1866 u32 mask; 1867 + u64 i_size; 1983 1868 __le16 a_flags; 1984 1869 1985 1870 if (!bytes) ··· 1996 1879 return 0; 1997 1880 } 1998 1881 1999 - data_size = le64_to_cpu(attr_b->nres.data_size); 2000 - alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 2001 - a_flags = attr_b->flags; 2002 - 2003 - if (is_attr_ext(attr_b)) { 2004 - total_size = le64_to_cpu(attr_b->nres.total_size); 2005 - mask = (sbi->cluster_size << attr_b->nres.c_unit) - 1; 2006 - } else { 2007 - total_size = alloc_size; 2008 - mask = sbi->cluster_mask; 2009 - } 2010 - 2011 - if ((vbo & mask) || (bytes & mask)) { 1882 + mask = is_attr_ext(attr_b) ? 1883 + ((sbi->cluster_size << attr_b->nres.c_unit) - 1) : 1884 + sbi->cluster_mask; 1885 + if ((vbo | bytes) & mask) { 2012 1886 /* Allow to collapse only cluster aligned ranges. */ 2013 1887 return -EINVAL; 2014 1888 } 2015 1889 2016 - if (vbo > data_size) 1890 + /* i_size - size of file with delay allocated clusters. */ 1891 + i_size = ni->vfs_inode.i_size; 1892 + 1893 + if (vbo > i_size) 2017 1894 return -EINVAL; 2018 1895 2019 1896 down_write(&ni->file.run_lock); 2020 1897 2021 - if (vbo + bytes >= data_size) { 2022 - u64 new_valid = min(ni->i_valid, vbo); 1898 + if (vbo + bytes >= i_size) { 1899 + valid_size = min(ni->i_valid, vbo); 2023 1900 2024 1901 /* Simple truncate file at 'vbo'. */ 2025 1902 truncate_setsize(&ni->vfs_inode, vbo); 2026 1903 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, vbo, 2027 - &new_valid, true, NULL); 1904 + &valid_size, true); 2028 1905 2029 - if (!err && new_valid < ni->i_valid) 2030 - ni->i_valid = new_valid; 1906 + if (!err && valid_size < ni->i_valid) 1907 + ni->i_valid = valid_size; 2031 1908 2032 1909 goto out; 2033 1910 } 2034 1911 2035 - /* 2036 - * Enumerate all attribute segments and collapse. 2037 - */ 2038 - alen = alloc_size >> sbi->cluster_bits; 2039 1912 vcn = vbo >> sbi->cluster_bits; 2040 1913 len = bytes >> sbi->cluster_bits; 2041 1914 end = vcn + len; 2042 1915 dealloc = 0; 2043 1916 done = 0; 2044 1917 1918 + /* 1919 + * Check delayed clusters. 1920 + */ 1921 + if (ni->file.run_da.count) { 1922 + struct runs_tree *run_da = &ni->file.run_da; 1923 + if (run_is_mapped_full(run_da, vcn, end - 1)) { 1924 + /* 1925 + * The requested range is full in delayed clusters. 1926 + */ 1927 + err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, run, 1928 + i_size - bytes, NULL, false, 1929 + NULL, true); 1930 + goto out; 1931 + } 1932 + 1933 + /* Collapse request crosses real and delayed clusters. */ 1934 + err = ni_allocate_da_blocks_locked(ni); 1935 + if (err) 1936 + goto out; 1937 + 1938 + /* Layout of records maybe changed. */ 1939 + le_b = NULL; 1940 + attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, 1941 + &mi_b); 1942 + if (!attr_b || !attr_b->non_res) { 1943 + err = -ENOENT; 1944 + goto out; 1945 + } 1946 + } 1947 + 1948 + data_size = le64_to_cpu(attr_b->nres.data_size); 1949 + alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 1950 + total_size = is_attr_ext(attr_b) ? 1951 + le64_to_cpu(attr_b->nres.total_size) : 1952 + alloc_size; 1953 + alen = alloc_size >> sbi->cluster_bits; 1954 + a_flags = attr_b->flags; 2045 1955 svcn = le64_to_cpu(attr_b->nres.svcn); 2046 1956 evcn1 = le64_to_cpu(attr_b->nres.evcn) + 1; 2047 1957 ··· 2091 1947 goto out; 2092 1948 } 2093 1949 1950 + /* 1951 + * Enumerate all attribute segments and collapse. 1952 + */ 2094 1953 for (;;) { 2095 1954 CLST vcn1, eat, next_svcn; 2096 1955 ··· 2121 1974 vcn1 = vcn + done; /* original vcn in attr/run. */ 2122 1975 eat = min(end, evcn1) - vcn1; 2123 1976 2124 - err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc, true); 1977 + err = run_deallocate_ex(sbi, run, vcn1, eat, &dealloc, true, 1978 + NULL); 2125 1979 if (err) 2126 1980 goto out; 2127 1981 2128 1982 if (svcn + eat < evcn1) { 2129 1983 /* Collapse a part of this attribute segment. */ 2130 - 2131 1984 if (!run_collapse_range(run, vcn1, eat, done)) { 2132 1985 err = -ENOMEM; 2133 1986 goto out; ··· 2308 2161 bytes = alloc_size; 2309 2162 bytes -= vbo; 2310 2163 2311 - if ((vbo & mask) || (bytes & mask)) { 2164 + if ((vbo | bytes) & mask) { 2312 2165 /* We have to zero a range(s). */ 2313 - if (frame_size == NULL) { 2166 + if (!frame_size) { 2314 2167 /* Caller insists range is aligned. */ 2315 2168 return -EINVAL; 2316 2169 } ··· 2369 2222 * Calculate how many clusters there are. 2370 2223 * Don't do any destructive actions. 2371 2224 */ 2372 - err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false); 2225 + err = run_deallocate_ex(NULL, run, vcn1, zero, &hole2, false, 2226 + NULL); 2373 2227 if (err) 2374 2228 goto done; 2375 2229 ··· 2408 2260 } 2409 2261 2410 2262 /* Real deallocate. Should not fail. */ 2411 - run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true); 2263 + run_deallocate_ex(sbi, &run2, vcn1, zero, &hole, true, 2264 + &ni->file.run_da); 2412 2265 2413 2266 next_attr: 2414 2267 /* Free all allocated memory. */ ··· 2521 2372 return -EINVAL; 2522 2373 } 2523 2374 2524 - if ((vbo & mask) || (bytes & mask)) { 2375 + if ((vbo | bytes) & mask) { 2525 2376 /* Allow to insert only frame aligned ranges. */ 2526 2377 return -EINVAL; 2527 2378 } ··· 2540 2391 2541 2392 if (!attr_b->non_res) { 2542 2393 err = attr_set_size(ni, ATTR_DATA, NULL, 0, run, 2543 - data_size + bytes, NULL, false, NULL); 2394 + data_size + bytes, NULL, false); 2544 2395 2545 2396 le_b = NULL; 2546 2397 attr_b = ni_find_attr(ni, NULL, &le_b, ATTR_DATA, NULL, 0, NULL, ··· 2563 2414 goto done; 2564 2415 } 2565 2416 2566 - /* Resident files becomes nonresident. */ 2417 + /* Resident file becomes nonresident. */ 2567 2418 data_size = le64_to_cpu(attr_b->nres.data_size); 2568 2419 alloc_size = le64_to_cpu(attr_b->nres.alloc_size); 2569 2420 } ··· 2600 2451 if (err) 2601 2452 goto out; 2602 2453 2603 - if (!run_insert_range(run, vcn, len)) { 2604 - err = -ENOMEM; 2454 + err = run_insert_range(run, vcn, len); 2455 + if (err) 2605 2456 goto out; 2606 - } 2457 + 2458 + err = run_insert_range_da(&ni->file.run_da, vcn, len); 2459 + if (err) 2460 + goto out; 2607 2461 2608 2462 /* Try to pack in current record as much as possible. */ 2609 2463 err = mi_pack_runs(mi, attr, run, evcn1 + len - svcn);
+13 -4
fs/ntfs3/attrlist.c
··· 52 52 53 53 if (!attr->non_res) { 54 54 lsize = le32_to_cpu(attr->res.data_size); 55 + if (!lsize) { 56 + err = -EINVAL; 57 + goto out; 58 + } 59 + 55 60 /* attr is resident: lsize < record_size (1K or 4K) */ 56 61 le = kvmalloc(al_aligned(lsize), GFP_KERNEL); 57 62 if (!le) { ··· 71 66 u16 run_off = le16_to_cpu(attr->nres.run_off); 72 67 73 68 lsize = le64_to_cpu(attr->nres.data_size); 69 + if (!lsize) { 70 + err = -EINVAL; 71 + goto out; 72 + } 74 73 75 74 run_init(&ni->attr_list.run); 76 75 ··· 345 336 le->id = id; 346 337 memcpy(le->name, name, sizeof(short) * name_len); 347 338 348 - err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, new_size, 349 - &new_size, true, &attr); 339 + err = attr_set_size_ex(ni, ATTR_LIST, NULL, 0, &al->run, new_size, 340 + &new_size, true, &attr, false); 350 341 if (err) { 351 342 /* Undo memmove above. */ 352 343 memmove(le, Add2Ptr(le, sz), old_size - off); ··· 404 395 * Attribute list increased on demand in al_add_le. 405 396 * Attribute list decreased here. 406 397 */ 407 - err = attr_set_size(ni, ATTR_LIST, NULL, 0, &al->run, al->size, NULL, 408 - false, &attr); 398 + err = attr_set_size_ex(ni, ATTR_LIST, NULL, 0, &al->run, al->size, NULL, 399 + false, &attr, false); 409 400 if (err) 410 401 goto out; 411 402
+17
fs/ntfs3/bitmap.c
··· 508 508 size_t wpos, wbit, iw, vbo; 509 509 struct buffer_head *bh = NULL; 510 510 CLST lcn, clen; 511 + struct file_ra_state *ra; 512 + struct address_space *mapping = sb->s_bdev->bd_mapping; 511 513 512 514 wnd->uptodated = 0; 513 515 wnd->extent_max = 0; ··· 517 515 wnd->total_zeroes = 0; 518 516 519 517 vbo = 0; 518 + 519 + /* Allocate in memory instead of stack. Not critical if failed. */ 520 + ra = kzalloc(sizeof(*ra), GFP_NOFS); 521 + if (ra) { 522 + file_ra_state_init(ra, mapping); 523 + ra->ra_pages = (wnd->nbits / 8 + PAGE_SIZE - 1) >> PAGE_SHIFT; 524 + } 520 525 521 526 for (iw = 0; iw < wnd->nwnd; iw++) { 522 527 if (iw + 1 == wnd->nwnd) ··· 559 550 560 551 lbo = ((u64)lcn << cluster_bits) + off; 561 552 len = ((u64)clen << cluster_bits) - off; 553 + } 554 + 555 + if (ra) { 556 + pgoff_t idx = lbo >> PAGE_SHIFT; 557 + if (!ra_has_index(ra, idx)) 558 + page_cache_sync_readahead(mapping, ra, NULL, 559 + idx, 1); 562 560 } 563 561 564 562 bh = ntfs_bread(sb, lbo >> sb->s_blocksize_bits); ··· 654 638 } 655 639 656 640 out: 641 + kfree(ra); 657 642 return err; 658 643 } 659 644
+76 -32
fs/ntfs3/dir.c
··· 393 393 * ntfs_readdir - file_operations::iterate_shared 394 394 * 395 395 * Use non sorted enumeration. 396 - * We have an example of broken volume where sorted enumeration 397 - * counts each name twice. 396 + * Sorted enumeration may result infinite loop if names tree contains loop. 398 397 */ 399 398 static int ntfs_readdir(struct file *file, struct dir_context *ctx) 400 399 { 401 400 const struct INDEX_ROOT *root; 402 - u64 vbo; 403 401 size_t bit; 404 - loff_t eod; 405 402 int err = 0; 406 403 struct inode *dir = file_inode(file); 407 404 struct ntfs_inode *ni = ntfs_i(dir); 408 405 struct super_block *sb = dir->i_sb; 409 406 struct ntfs_sb_info *sbi = sb->s_fs_info; 410 407 loff_t i_size = i_size_read(dir); 411 - u32 pos = ctx->pos; 408 + u64 pos = ctx->pos; 412 409 u8 *name = NULL; 413 410 struct indx_node *node = NULL; 414 411 u8 index_bits = ni->dir.index_bits; 412 + size_t max_bit = i_size >> ni->dir.index_bits; 413 + loff_t eod = i_size + sbi->record_size; 415 414 416 415 /* Name is a buffer of PATH_MAX length. */ 417 416 static_assert(NTFS_NAME_LEN * 4 < PATH_MAX); 418 417 419 - eod = i_size + sbi->record_size; 418 + if (!pos) { 419 + /* 420 + * ni->dir.version increments each directory change. 421 + * Save the initial value of ni->dir.version. 422 + */ 423 + file->private_data = (void *)ni->dir.version; 424 + } 420 425 421 - if (pos >= eod) 422 - return 0; 426 + if (pos >= eod) { 427 + if (file->private_data == (void *)ni->dir.version) { 428 + /* No changes since first readdir. */ 429 + return 0; 430 + } 431 + 432 + /* 433 + * Handle directories that changed after the initial readdir(). 434 + * 435 + * Some user space code implements recursive removal like this instead 436 + * of calling rmdir(2) directly: 437 + * 438 + * fd = opendir(path); 439 + * while ((dent = readdir(fd))) 440 + * unlinkat(dirfd(fd), dent->d_name, 0); 441 + * closedir(fd); 442 + * 443 + * POSIX leaves unspecified what readdir() should return once the 444 + * directory has been modified after opendir()/rewinddir(), so this 445 + * pattern is not guaranteed to work on all filesystems or platforms. 446 + * 447 + * In ntfs3 the internal name tree may be reshaped while entries are 448 + * being removed, so there is no stable anchor for continuing a 449 + * single-pass walk based on the original readdir() order. 450 + * 451 + * In practice some widely used tools (for example certain rm(1) 452 + * implementations) have used this readdir()/unlink() loop, and some 453 + * filesystems behave in a way that effectively makes it work in the 454 + * common case. 455 + * 456 + * The code below follows that practice and tries to provide 457 + * "rmdir-like" behaviour for such callers on ntfs3, even though the 458 + * situation is not strictly defined by the APIs. 459 + * 460 + * Apple documents the same readdir()/unlink() issue and a workaround 461 + * for HFS file systems in: 462 + * https://web.archive.org/web/20220122122948/https:/support.apple.com/kb/TA21420?locale=en_US 463 + */ 464 + ctx->pos = pos = 3; 465 + file->private_data = (void *)ni->dir.version; 466 + } 423 467 424 468 if (!dir_emit_dots(file, ctx)) 425 469 return 0; ··· 498 454 if (pos >= sbi->record_size) { 499 455 bit = (pos - sbi->record_size) >> index_bits; 500 456 } else { 457 + /* 458 + * Add each name from root in 'ctx'. 459 + */ 501 460 err = ntfs_read_hdr(sbi, ni, &root->ihdr, 0, pos, name, ctx); 502 461 if (err) 503 462 goto out; 504 463 bit = 0; 505 464 } 506 465 507 - if (!i_size) { 508 - ctx->pos = eod; 509 - goto out; 510 - } 511 - 512 - for (;;) { 513 - vbo = (u64)bit << index_bits; 514 - if (vbo >= i_size) { 515 - ctx->pos = eod; 516 - goto out; 517 - } 518 - 466 + /* 467 + * Enumerate indexes until the end of dir. 468 + */ 469 + for (; bit < max_bit; bit += 1) { 470 + /* Get the next used index. */ 519 471 err = indx_used_bit(&ni->dir, ni, &bit); 520 472 if (err) 521 473 goto out; 522 474 523 475 if (bit == MINUS_ONE_T) { 524 - ctx->pos = eod; 525 - goto out; 476 + /* no more used indexes. end of dir. */ 477 + break; 526 478 } 527 479 528 - vbo = (u64)bit << index_bits; 529 - if (vbo >= i_size) { 480 + if (bit >= max_bit) { 481 + /* Corrupted directory. */ 530 482 err = -EINVAL; 531 483 goto out; 532 484 } 533 485 534 - err = indx_read(&ni->dir, ni, bit << ni->dir.idx2vbn_bits, 535 - &node); 486 + err = indx_read_ra(&ni->dir, ni, bit << ni->dir.idx2vbn_bits, 487 + &node, &file->f_ra); 536 488 if (err) 537 489 goto out; 538 490 491 + /* 492 + * Add each name from index in 'ctx'. 493 + */ 539 494 err = ntfs_read_hdr(sbi, ni, &node->index->ihdr, 540 - vbo + sbi->record_size, pos, name, ctx); 495 + ((u64)bit << index_bits) + sbi->record_size, 496 + pos, name, ctx); 541 497 if (err) 542 498 goto out; 543 - 544 - bit += 1; 545 499 } 546 500 547 501 out: 548 - 549 502 kfree(name); 550 503 put_indx_node(node); 551 504 552 - if (err == 1) { 505 + if (!err) { 506 + /* End of directory. */ 507 + ctx->pos = eod; 508 + } else if (err == 1) { 553 509 /* 'ctx' is full. */ 554 510 err = 0; 555 511 } else if (err == -ENOENT) { ··· 668 624 .llseek = generic_file_llseek, 669 625 .read = generic_read_dir, 670 626 .iterate_shared = ntfs_readdir, 671 - .fsync = generic_file_fsync, 627 + .fsync = ntfs_file_fsync, 672 628 .open = ntfs_file_open, 673 629 .unlocked_ioctl = ntfs_ioctl, 674 630 #ifdef CONFIG_COMPAT
+344 -255
fs/ntfs3/file.c
··· 15 15 #include <linux/fiemap.h> 16 16 #include <linux/fileattr.h> 17 17 #include <linux/filelock.h> 18 + #include <linux/iomap.h> 18 19 19 20 #include "debug.h" 20 21 #include "ntfs.h" ··· 26 25 * Hope this value will become common to all fs. 27 26 */ 28 27 #define NTFS3_IOC_SHUTDOWN _IOR('X', 125, __u32) 28 + 29 + /* 30 + * Helper for ntfs_should_use_dio. 31 + */ 32 + static u32 ntfs_dio_alignment(struct inode *inode) 33 + { 34 + struct ntfs_inode *ni = ntfs_i(inode); 35 + 36 + if (is_resident(ni)) { 37 + /* Check delalloc. */ 38 + if (!ni->file.run_da.count) 39 + return 0; 40 + } 41 + 42 + /* In most cases this is bdev_logical_block_size(bdev). */ 43 + return ni->mi.sbi->bdev_blocksize; 44 + } 45 + 46 + /* 47 + * Returns %true if the given DIO request should be attempted with DIO, or 48 + * %false if it should fall back to buffered I/O. 49 + */ 50 + static bool ntfs_should_use_dio(struct kiocb *iocb, struct iov_iter *iter) 51 + { 52 + struct inode *inode = file_inode(iocb->ki_filp); 53 + u32 dio_align = ntfs_dio_alignment(inode); 54 + 55 + if (!dio_align) 56 + return false; 57 + 58 + return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align); 59 + } 29 60 30 61 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg) 31 62 { ··· 219 186 220 187 static int ntfs_extend_initialized_size(struct file *file, 221 188 struct ntfs_inode *ni, 222 - const loff_t valid, 223 189 const loff_t new_valid) 224 190 { 225 191 struct inode *inode = &ni->vfs_inode; 226 - struct address_space *mapping = inode->i_mapping; 227 - struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info; 228 - loff_t pos = valid; 192 + const loff_t valid = ni->i_valid; 229 193 int err; 230 194 231 195 if (valid >= new_valid) ··· 233 203 return 0; 234 204 } 235 205 236 - WARN_ON(is_compressed(ni)); 237 - 238 - for (;;) { 239 - u32 zerofrom, len; 240 - struct folio *folio; 241 - u8 bits; 242 - CLST vcn, lcn, clen; 243 - 244 - if (is_sparsed(ni)) { 245 - bits = sbi->cluster_bits; 246 - vcn = pos >> bits; 247 - 248 - err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL, 249 - false); 250 - if (err) 251 - goto out; 252 - 253 - if (lcn == SPARSE_LCN) { 254 - pos = ((loff_t)clen + vcn) << bits; 255 - ni->i_valid = pos; 256 - goto next; 257 - } 258 - } 259 - 260 - zerofrom = pos & (PAGE_SIZE - 1); 261 - len = PAGE_SIZE - zerofrom; 262 - 263 - if (pos + len > new_valid) 264 - len = new_valid - pos; 265 - 266 - err = ntfs_write_begin(NULL, mapping, pos, len, &folio, NULL); 267 - if (err) 268 - goto out; 269 - 270 - folio_zero_range(folio, zerofrom, folio_size(folio) - zerofrom); 271 - 272 - err = ntfs_write_end(NULL, mapping, pos, len, len, folio, NULL); 273 - if (err < 0) 274 - goto out; 275 - pos += len; 276 - 277 - next: 278 - if (pos >= new_valid) 279 - break; 280 - 281 - balance_dirty_pages_ratelimited(mapping); 282 - cond_resched(); 206 + err = iomap_zero_range(inode, valid, new_valid - valid, NULL, 207 + &ntfs_iomap_ops, &ntfs_iomap_folio_ops, NULL); 208 + if (err) { 209 + ni->i_valid = valid; 210 + ntfs_inode_warn(inode, 211 + "failed to extend initialized size to %llx.", 212 + new_valid); 213 + return err; 283 214 } 284 215 285 216 return 0; 286 - 287 - out: 288 - ni->i_valid = valid; 289 - ntfs_inode_warn(inode, "failed to extend initialized size to %llx.", 290 - new_valid); 291 - return err; 292 217 } 293 218 294 - /* 295 - * ntfs_zero_range - Helper function for punch_hole. 296 - * 297 - * It zeroes a range [vbo, vbo_to). 298 - */ 299 - static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to) 219 + static void ntfs_filemap_close(struct vm_area_struct *vma) 300 220 { 301 - int err = 0; 302 - struct address_space *mapping = inode->i_mapping; 303 - u32 blocksize = i_blocksize(inode); 304 - pgoff_t idx = vbo >> PAGE_SHIFT; 305 - u32 from = vbo & (PAGE_SIZE - 1); 306 - pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT; 307 - loff_t page_off; 308 - struct buffer_head *head, *bh; 309 - u32 bh_next, bh_off, to; 310 - sector_t iblock; 311 - struct folio *folio; 312 - bool dirty = false; 221 + struct inode *inode = file_inode(vma->vm_file); 222 + struct ntfs_inode *ni = ntfs_i(inode); 223 + u64 from = (u64)vma->vm_pgoff << PAGE_SHIFT; 224 + u64 to = min_t(u64, i_size_read(inode), 225 + from + vma->vm_end - vma->vm_start); 313 226 314 - for (; idx < idx_end; idx += 1, from = 0) { 315 - page_off = (loff_t)idx << PAGE_SHIFT; 316 - to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) : 317 - PAGE_SIZE; 318 - iblock = page_off >> inode->i_blkbits; 319 - 320 - folio = __filemap_get_folio( 321 - mapping, idx, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, 322 - mapping_gfp_constraint(mapping, ~__GFP_FS)); 323 - if (IS_ERR(folio)) 324 - return PTR_ERR(folio); 325 - 326 - head = folio_buffers(folio); 327 - if (!head) 328 - head = create_empty_buffers(folio, blocksize, 0); 329 - 330 - bh = head; 331 - bh_off = 0; 332 - do { 333 - bh_next = bh_off + blocksize; 334 - 335 - if (bh_next <= from || bh_off >= to) 336 - continue; 337 - 338 - if (!buffer_mapped(bh)) { 339 - ntfs_get_block(inode, iblock, bh, 0); 340 - /* Unmapped? It's a hole - nothing to do. */ 341 - if (!buffer_mapped(bh)) 342 - continue; 343 - } 344 - 345 - /* Ok, it's mapped. Make sure it's up-to-date. */ 346 - if (folio_test_uptodate(folio)) 347 - set_buffer_uptodate(bh); 348 - else if (bh_read(bh, 0) < 0) { 349 - err = -EIO; 350 - folio_unlock(folio); 351 - folio_put(folio); 352 - goto out; 353 - } 354 - 355 - mark_buffer_dirty(bh); 356 - } while (bh_off = bh_next, iblock += 1, 357 - head != (bh = bh->b_this_page)); 358 - 359 - folio_zero_segment(folio, from, to); 360 - dirty = true; 361 - 362 - folio_unlock(folio); 363 - folio_put(folio); 364 - cond_resched(); 365 - } 366 - out: 367 - if (dirty) 227 + if (ni->i_valid < to) { 228 + ni->i_valid = to; 368 229 mark_inode_dirty(inode); 369 - return err; 230 + } 370 231 } 232 + 233 + /* Copy of generic_file_vm_ops. */ 234 + static const struct vm_operations_struct ntfs_file_vm_ops = { 235 + .close = ntfs_filemap_close, 236 + .fault = filemap_fault, 237 + .map_pages = filemap_map_pages, 238 + .page_mkwrite = filemap_page_mkwrite, 239 + }; 371 240 372 241 /* 373 242 * ntfs_file_mmap_prepare - file_operations::mmap_prepare ··· 276 347 struct file *file = desc->file; 277 348 struct inode *inode = file_inode(file); 278 349 struct ntfs_inode *ni = ntfs_i(inode); 279 - u64 from = ((u64)desc->pgoff << PAGE_SHIFT); 280 350 bool rw = desc->vm_flags & VM_WRITE; 281 351 int err; 282 352 ··· 307 379 } 308 380 309 381 if (rw) { 310 - u64 to = min_t(loff_t, i_size_read(inode), 382 + u64 from = (u64)desc->pgoff << PAGE_SHIFT; 383 + u64 to = min_t(u64, i_size_read(inode), 311 384 from + vma_desc_size(desc)); 312 385 313 386 if (is_sparsed(ni)) { ··· 321 392 322 393 for (; vcn < end; vcn += len) { 323 394 err = attr_data_get_block(ni, vcn, 1, &lcn, 324 - &len, &new, true); 395 + &len, &new, true, 396 + NULL, false); 325 397 if (err) 326 398 goto out; 327 399 } ··· 333 403 err = -EAGAIN; 334 404 goto out; 335 405 } 336 - err = ntfs_extend_initialized_size(file, ni, 337 - ni->i_valid, to); 406 + err = ntfs_extend_initialized_size(file, ni, to); 338 407 inode_unlock(inode); 339 408 if (err) 340 409 goto out; ··· 341 412 } 342 413 343 414 err = generic_file_mmap_prepare(desc); 415 + if (!err && rw) 416 + desc->vm_ops = &ntfs_file_vm_ops; 344 417 out: 345 418 return err; 346 419 } ··· 363 432 ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY); 364 433 365 434 if (end > inode->i_size) { 435 + /* 436 + * Normal files: increase file size, allocate space. 437 + * Sparse/Compressed: increase file size. No space allocated. 438 + */ 366 439 err = ntfs_set_size(inode, end); 367 440 if (err) 368 441 goto out; 369 442 } 370 443 371 444 if (extend_init && !is_compressed(ni)) { 372 - err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos); 445 + err = ntfs_extend_initialized_size(file, ni, pos); 373 446 if (err) 374 447 goto out; 375 448 } else { 376 449 err = 0; 377 - } 378 - 379 - if (file && is_sparsed(ni)) { 380 - /* 381 - * This code optimizes large writes to sparse file. 382 - * TODO: merge this fragment with fallocate fragment. 383 - */ 384 - struct ntfs_sb_info *sbi = ni->mi.sbi; 385 - CLST vcn = pos >> sbi->cluster_bits; 386 - CLST cend = bytes_to_cluster(sbi, end); 387 - CLST cend_v = bytes_to_cluster(sbi, ni->i_valid); 388 - CLST lcn, clen; 389 - bool new; 390 - 391 - if (cend_v > cend) 392 - cend_v = cend; 393 - 394 - /* 395 - * Allocate and zero new clusters. 396 - * Zeroing these clusters may be too long. 397 - */ 398 - for (; vcn < cend_v; vcn += clen) { 399 - err = attr_data_get_block(ni, vcn, cend_v - vcn, &lcn, 400 - &clen, &new, true); 401 - if (err) 402 - goto out; 403 - } 404 - /* 405 - * Allocate but not zero new clusters. 406 - */ 407 - for (; vcn < cend; vcn += clen) { 408 - err = attr_data_get_block(ni, vcn, cend - vcn, &lcn, 409 - &clen, &new, false); 410 - if (err) 411 - goto out; 412 - } 413 450 } 414 451 415 452 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); ··· 403 504 404 505 static int ntfs_truncate(struct inode *inode, loff_t new_size) 405 506 { 406 - struct super_block *sb = inode->i_sb; 507 + int err; 407 508 struct ntfs_inode *ni = ntfs_i(inode); 408 - int err, dirty = 0; 409 - u64 new_valid; 410 - 411 - if (!S_ISREG(inode->i_mode)) 412 - return 0; 413 - 414 - if (is_compressed(ni)) { 415 - if (ni->i_valid > new_size) 416 - ni->i_valid = new_size; 417 - } else { 418 - err = block_truncate_page(inode->i_mapping, new_size, 419 - ntfs_get_block); 420 - if (err) 421 - return err; 422 - } 423 - 424 - new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size)); 509 + u64 new_valid = min_t(u64, ni->i_valid, new_size); 425 510 426 511 truncate_setsize(inode, new_size); 427 512 428 513 ni_lock(ni); 429 514 430 515 down_write(&ni->file.run_lock); 431 - err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, 432 - &new_valid, ni->mi.sbi->options->prealloc, NULL); 516 + err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, 517 + &new_valid, ni->mi.sbi->options->prealloc, NULL, 518 + false); 433 519 up_write(&ni->file.run_lock); 434 520 435 - if (new_valid < ni->i_valid) 436 - ni->i_valid = new_valid; 521 + ni->i_valid = new_valid; 437 522 438 523 ni_unlock(ni); 524 + 525 + if (err) 526 + return err; 439 527 440 528 ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; 441 529 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode)); 442 530 if (!IS_DIRSYNC(inode)) { 443 - dirty = 1; 531 + mark_inode_dirty(inode); 444 532 } else { 445 533 err = ntfs_sync_inode(inode); 446 534 if (err) 447 535 return err; 448 536 } 449 - 450 - if (dirty) 451 - mark_inode_dirty(inode); 452 537 453 538 return 0; 454 539 } ··· 506 623 507 624 if (mode & FALLOC_FL_PUNCH_HOLE) { 508 625 u32 frame_size; 509 - loff_t mask, vbo_a, end_a, tmp; 626 + loff_t mask, vbo_a, end_a, tmp, from; 510 627 511 628 err = filemap_write_and_wait_range(mapping, vbo_down, 512 629 LLONG_MAX); ··· 526 643 527 644 /* Process not aligned punch. */ 528 645 err = 0; 646 + if (end > i_size) 647 + end = i_size; 529 648 mask = frame_size - 1; 530 649 vbo_a = (vbo + mask) & ~mask; 531 650 end_a = end & ~mask; 532 651 533 652 tmp = min(vbo_a, end); 534 - if (tmp > vbo) { 535 - err = ntfs_zero_range(inode, vbo, tmp); 653 + from = min_t(loff_t, ni->i_valid, vbo); 654 + /* Zero head of punch. */ 655 + if (tmp > from) { 656 + err = iomap_zero_range(inode, from, tmp - from, NULL, 657 + &ntfs_iomap_ops, 658 + &ntfs_iomap_folio_ops, NULL); 536 659 if (err) 537 660 goto out; 538 661 } 539 662 540 - if (vbo < end_a && end_a < end) { 541 - err = ntfs_zero_range(inode, end_a, end); 542 - if (err) 543 - goto out; 544 - } 545 - 546 - /* Aligned punch_hole */ 663 + /* Aligned punch_hole. Deallocate clusters. */ 547 664 if (end_a > vbo_a) { 548 665 ni_lock(ni); 549 666 err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL); 550 667 ni_unlock(ni); 668 + if (err) 669 + goto out; 670 + } 671 + 672 + /* Zero tail of punch. */ 673 + if (vbo < end_a && end_a < end) { 674 + err = iomap_zero_range(inode, end_a, end - end_a, NULL, 675 + &ntfs_iomap_ops, 676 + &ntfs_iomap_folio_ops, NULL); 551 677 if (err) 552 678 goto out; 553 679 } ··· 657 765 for (; vcn < cend_v; vcn += clen) { 658 766 err = attr_data_get_block(ni, vcn, cend_v - vcn, 659 767 &lcn, &clen, &new, 660 - true); 768 + true, NULL, false); 661 769 if (err) 662 770 goto out; 663 771 } 772 + 773 + /* 774 + * Moving up 'valid size'. 775 + */ 776 + err = ntfs_extend_initialized_size( 777 + file, ni, (u64)cend_v << cluster_bits); 778 + if (err) 779 + goto out; 780 + 664 781 /* 665 782 * Allocate but not zero new clusters. 666 783 */ 667 784 for (; vcn < cend; vcn += clen) { 668 785 err = attr_data_get_block(ni, vcn, cend - vcn, 669 786 &lcn, &clen, &new, 670 - false); 787 + false, NULL, false); 671 788 if (err) 672 789 goto out; 673 790 } ··· 687 786 /* True - Keep preallocated. */ 688 787 err = attr_set_size(ni, ATTR_DATA, NULL, 0, 689 788 &ni->file.run, i_size, &ni->i_valid, 690 - true, NULL); 789 + true); 691 790 ni_unlock(ni); 692 791 if (err) 693 792 goto out; 793 + i_size_write(inode, i_size); 694 794 } else if (new_size > i_size) { 695 795 i_size_write(inode, new_size); 696 796 } ··· 828 926 struct file *file = iocb->ki_filp; 829 927 struct inode *inode = file_inode(file); 830 928 struct ntfs_inode *ni = ntfs_i(inode); 929 + size_t bytes = iov_iter_count(iter); 930 + loff_t valid, i_size, vbo, end; 931 + unsigned int dio_flags; 831 932 ssize_t err; 832 933 833 934 err = check_read_restriction(inode); 834 935 if (err) 835 936 return err; 937 + 938 + if (!bytes) 939 + return 0; /* skip atime */ 836 940 837 941 if (is_compressed(ni)) { 838 942 if (iocb->ki_flags & IOCB_DIRECT) { ··· 850 942 file->f_ra.ra_pages = 0; 851 943 } 852 944 853 - /* Check minimum alignment for dio. */ 854 - if (iocb->ki_flags & IOCB_DIRECT) { 855 - struct super_block *sb = inode->i_sb; 856 - struct ntfs_sb_info *sbi = sb->s_fs_info; 857 - if ((iocb->ki_pos | iov_iter_alignment(iter)) & 858 - sbi->bdev_blocksize_mask) { 859 - iocb->ki_flags &= ~IOCB_DIRECT; 860 - } 945 + /* Fallback to buffered I/O if the inode does not support direct I/O. */ 946 + if (!(iocb->ki_flags & IOCB_DIRECT) || 947 + !ntfs_should_use_dio(iocb, iter)) { 948 + iocb->ki_flags &= ~IOCB_DIRECT; 949 + return generic_file_read_iter(iocb, iter); 861 950 } 862 951 863 - return generic_file_read_iter(iocb, iter); 952 + if (iocb->ki_flags & IOCB_NOWAIT) { 953 + if (!inode_trylock_shared(inode)) 954 + return -EAGAIN; 955 + } else { 956 + inode_lock_shared(inode); 957 + } 958 + 959 + vbo = iocb->ki_pos; 960 + end = vbo + bytes; 961 + dio_flags = 0; 962 + valid = ni->i_valid; 963 + i_size = inode->i_size; 964 + 965 + if (vbo < valid) { 966 + if (valid < end) { 967 + /* read cross 'valid' size. */ 968 + dio_flags |= IOMAP_DIO_FORCE_WAIT; 969 + } 970 + 971 + if (ni->file.run_da.count) { 972 + /* Direct I/O is not compatible with delalloc. */ 973 + err = ni_allocate_da_blocks(ni); 974 + if (err) 975 + goto out; 976 + } 977 + 978 + err = iomap_dio_rw(iocb, iter, &ntfs_iomap_ops, NULL, dio_flags, 979 + NULL, 0); 980 + 981 + if (err <= 0) 982 + goto out; 983 + end = vbo + err; 984 + if (valid < end) { 985 + size_t to_zero = end - valid; 986 + /* Fix iter. */ 987 + iov_iter_revert(iter, to_zero); 988 + iov_iter_zero(to_zero, iter); 989 + } 990 + } else if (vbo < i_size) { 991 + if (end > i_size) 992 + bytes = i_size - vbo; 993 + iov_iter_zero(bytes, iter); 994 + iocb->ki_pos += bytes; 995 + err = bytes; 996 + } 997 + 998 + out: 999 + inode_unlock_shared(inode); 1000 + file_accessed(iocb->ki_filp); 1001 + return err; 864 1002 } 865 1003 866 1004 /* ··· 950 996 951 997 folio = __filemap_get_folio(mapping, index, 952 998 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, 953 - gfp_mask); 999 + gfp_mask | __GFP_ZERO); 954 1000 if (IS_ERR(folio)) { 955 1001 while (npages--) { 956 1002 folio = page_folio(pages[npages]); ··· 1027 1073 off = valid & (frame_size - 1); 1028 1074 1029 1075 err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn, 1030 - &clen, NULL, false); 1076 + &clen, NULL, false, NULL, false); 1031 1077 if (err) 1032 1078 goto out; 1033 1079 ··· 1219 1265 return -EOPNOTSUPP; 1220 1266 } 1221 1267 1268 + if (unlikely(IS_IMMUTABLE(inode))) 1269 + return -EPERM; 1270 + 1222 1271 return 0; 1223 1272 } 1224 1273 ··· 1233 1276 struct file *file = iocb->ki_filp; 1234 1277 struct inode *inode = file_inode(file); 1235 1278 struct ntfs_inode *ni = ntfs_i(inode); 1236 - ssize_t ret; 1237 - int err; 1279 + ssize_t ret, err; 1238 1280 1239 1281 if (!inode_trylock(inode)) { 1240 1282 if (iocb->ki_flags & IOCB_NOWAIT) ··· 1271 1315 if (ret) 1272 1316 goto out; 1273 1317 1274 - ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) : 1275 - __generic_file_write_iter(iocb, from); 1318 + if (is_compressed(ni)) { 1319 + ret = ntfs_compress_write(iocb, from); 1320 + goto out; 1321 + } 1322 + 1323 + /* Fallback to buffered I/O if the inode does not support direct I/O. */ 1324 + if (!(iocb->ki_flags & IOCB_DIRECT) || 1325 + !ntfs_should_use_dio(iocb, from)) { 1326 + iocb->ki_flags &= ~IOCB_DIRECT; 1327 + 1328 + ret = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops, 1329 + &ntfs_iomap_folio_ops, NULL); 1330 + inode_unlock(inode); 1331 + 1332 + if (likely(ret > 0)) 1333 + ret = generic_write_sync(iocb, ret); 1334 + 1335 + return ret; 1336 + } 1337 + 1338 + if (ni->file.run_da.count) { 1339 + /* Direct I/O is not compatible with delalloc. */ 1340 + ret = ni_allocate_da_blocks(ni); 1341 + if (ret) 1342 + goto out; 1343 + } 1344 + 1345 + ret = iomap_dio_rw(iocb, from, &ntfs_iomap_ops, NULL, 0, NULL, 0); 1346 + 1347 + if (ret == -ENOTBLK) { 1348 + /* Returns -ENOTBLK in case of a page invalidation failure for writes.*/ 1349 + /* The callers needs to fall back to buffered I/O in this case. */ 1350 + ret = 0; 1351 + } 1352 + 1353 + if (ret >= 0 && iov_iter_count(from)) { 1354 + loff_t offset = iocb->ki_pos, endbyte; 1355 + 1356 + iocb->ki_flags &= ~IOCB_DIRECT; 1357 + err = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops, 1358 + &ntfs_iomap_folio_ops, NULL); 1359 + if (err < 0) { 1360 + ret = err; 1361 + goto out; 1362 + } 1363 + 1364 + /* 1365 + * We need to ensure that the pages within the page cache for 1366 + * the range covered by this I/O are written to disk and 1367 + * invalidated. This is in attempt to preserve the expected 1368 + * direct I/O semantics in the case we fallback to buffered I/O 1369 + * to complete off the I/O request. 1370 + */ 1371 + ret += err; 1372 + endbyte = offset + err - 1; 1373 + err = filemap_write_and_wait_range(inode->i_mapping, offset, 1374 + endbyte); 1375 + if (err) { 1376 + ret = err; 1377 + goto out; 1378 + } 1379 + 1380 + invalidate_mapping_pages(inode->i_mapping, offset >> PAGE_SHIFT, 1381 + endbyte >> PAGE_SHIFT); 1382 + } 1276 1383 1277 1384 out: 1278 1385 inode_unlock(inode); 1279 - 1280 - if (ret > 0) 1281 - ret = generic_write_sync(iocb, ret); 1282 1386 1283 1387 return ret; 1284 1388 } ··· 1378 1362 #endif 1379 1363 } 1380 1364 1365 + file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT; 1366 + 1381 1367 return generic_file_open(inode, file); 1382 1368 } 1383 1369 1384 1370 /* 1385 1371 * ntfs_file_release - file_operations::release 1372 + * 1373 + * Called when an inode is released. Note that this is different 1374 + * from ntfs_file_open: open gets called at every open, but release 1375 + * gets called only when /all/ the files are closed. 1386 1376 */ 1387 1377 static int ntfs_file_release(struct inode *inode, struct file *file) 1388 1378 { 1389 - struct ntfs_inode *ni = ntfs_i(inode); 1390 - struct ntfs_sb_info *sbi = ni->mi.sbi; 1391 - int err = 0; 1379 + int err; 1380 + struct ntfs_inode *ni; 1392 1381 1393 - /* If we are last writer on the inode, drop the block reservation. */ 1394 - if (sbi->options->prealloc && 1395 - ((file->f_mode & FMODE_WRITE) && 1396 - atomic_read(&inode->i_writecount) == 1) 1397 - /* 1398 - * The only file when inode->i_fop = &ntfs_file_operations and 1399 - * init_rwsem(&ni->file.run_lock) is not called explicitly is MFT. 1400 - * 1401 - * Add additional check here. 1402 - */ 1403 - && inode->i_ino != MFT_REC_MFT) { 1382 + if (!(file->f_mode & FMODE_WRITE) || 1383 + atomic_read(&inode->i_writecount) != 1 || 1384 + inode->i_ino == MFT_REC_MFT) { 1385 + return 0; 1386 + } 1387 + 1388 + /* Close the last writer on the inode. */ 1389 + ni = ntfs_i(inode); 1390 + 1391 + /* Allocate delayed blocks (clusters). */ 1392 + err = ni_allocate_da_blocks(ni); 1393 + if (err) 1394 + goto out; 1395 + 1396 + if (ni->mi.sbi->options->prealloc) { 1404 1397 ni_lock(ni); 1405 1398 down_write(&ni->file.run_lock); 1406 1399 1400 + /* Deallocate preallocated. */ 1407 1401 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, 1408 - i_size_read(inode), &ni->i_valid, false, 1409 - NULL); 1402 + inode->i_size, &ni->i_valid, false); 1410 1403 1411 1404 up_write(&ni->file.run_lock); 1412 1405 ni_unlock(ni); 1413 1406 } 1407 + out: 1414 1408 return err; 1415 1409 } 1416 1410 ··· 1437 1411 if (unlikely(is_bad_ni(ni))) 1438 1412 return -EINVAL; 1439 1413 1440 - err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR); 1441 - if (err) 1442 - return err; 1414 + if (is_compressed(ni)) { 1415 + /* Unfortunately cp -r incorrectly treats compressed clusters. */ 1416 + ntfs_inode_warn(inode, 1417 + "fiemap is not supported for compressed file"); 1418 + return -EOPNOTSUPP; 1419 + } 1443 1420 1444 - ni_lock(ni); 1421 + if (S_ISDIR(inode->i_mode)) { 1422 + /* TODO: add support for dirs (ATTR_ALLOC). */ 1423 + ntfs_inode_warn(inode, 1424 + "fiemap is not supported for directories"); 1425 + return -EOPNOTSUPP; 1426 + } 1445 1427 1446 - err = ni_fiemap(ni, fieinfo, start, len); 1428 + if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) { 1429 + ntfs_inode_warn(inode, "fiemap(xattr) is not supported"); 1430 + return -EOPNOTSUPP; 1431 + } 1447 1432 1448 - ni_unlock(ni); 1433 + inode_lock_shared(inode); 1449 1434 1435 + err = iomap_fiemap(inode, fieinfo, start, len, &ntfs_iomap_ops); 1436 + 1437 + inode_unlock_shared(inode); 1450 1438 return err; 1451 1439 } 1452 1440 ··· 1484 1444 /* 1485 1445 * ntfs_file_fsync - file_operations::fsync 1486 1446 */ 1487 - static int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 1447 + int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync) 1488 1448 { 1489 1449 struct inode *inode = file_inode(file); 1490 - if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1450 + struct super_block *sb = inode->i_sb; 1451 + struct ntfs_sb_info *sbi = sb->s_fs_info; 1452 + int err, ret; 1453 + 1454 + if (unlikely(ntfs3_forced_shutdown(sb))) 1491 1455 return -EIO; 1492 1456 1493 - return generic_file_fsync(file, start, end, datasync); 1457 + ret = file_write_and_wait_range(file, start, end); 1458 + if (ret) 1459 + return ret; 1460 + 1461 + ret = write_inode_now(inode, !datasync); 1462 + 1463 + if (!ret) { 1464 + ret = ni_write_parents(ntfs_i(inode), !datasync); 1465 + } 1466 + 1467 + if (!ret) { 1468 + ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); 1469 + ntfs_update_mftmirr(sbi); 1470 + } 1471 + 1472 + err = sync_blockdev(sb->s_bdev); 1473 + if (unlikely(err && !ret)) 1474 + ret = err; 1475 + if (!ret) 1476 + blkdev_issue_flush(sb->s_bdev); 1477 + return ret; 1478 + } 1479 + 1480 + /* 1481 + * ntfs_llseek - file_operations::llseek 1482 + */ 1483 + static loff_t ntfs_llseek(struct file *file, loff_t offset, int whence) 1484 + { 1485 + struct inode *inode = file->f_mapping->host; 1486 + struct ntfs_inode *ni = ntfs_i(inode); 1487 + loff_t maxbytes = ntfs_get_maxbytes(ni); 1488 + loff_t ret; 1489 + 1490 + if (whence == SEEK_DATA || whence == SEEK_HOLE) { 1491 + inode_lock_shared(inode); 1492 + /* Scan file for hole or data. */ 1493 + ret = ni_seek_data_or_hole(ni, offset, whence == SEEK_DATA); 1494 + inode_unlock_shared(inode); 1495 + 1496 + if (ret >= 0) 1497 + ret = vfs_setpos(file, ret, maxbytes); 1498 + } else { 1499 + ret = generic_file_llseek_size(file, offset, whence, maxbytes, 1500 + i_size_read(inode)); 1501 + } 1502 + return ret; 1494 1503 } 1495 1504 1496 1505 // clang-format off ··· 1553 1464 }; 1554 1465 1555 1466 const struct file_operations ntfs_file_operations = { 1556 - .llseek = generic_file_llseek, 1467 + .llseek = ntfs_llseek, 1557 1468 .read_iter = ntfs_file_read_iter, 1558 1469 .write_iter = ntfs_file_write_iter, 1559 1470 .unlocked_ioctl = ntfs_ioctl,
+201 -181
fs/ntfs3/frecord.c
··· 123 123 indx_clear(&ni->dir); 124 124 else { 125 125 run_close(&ni->file.run); 126 + ntfs_sub_da(ni->mi.sbi, run_len(&ni->file.run_da)); 127 + run_close(&ni->file.run_da); 126 128 #ifdef CONFIG_NTFS3_LZX_XPRESS 127 129 if (ni->file.offs_folio) { 128 130 /* On-demand allocated page for offsets. */ ··· 1852 1850 return REPARSE_LINK; 1853 1851 } 1854 1852 1855 - /* 1856 - * ni_fiemap - Helper for file_fiemap(). 1857 - * 1858 - * Assumed ni_lock. 1859 - * TODO: Less aggressive locks. 1860 - */ 1861 - int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo, 1862 - __u64 vbo, __u64 len) 1863 - { 1864 - int err = 0; 1865 - struct ntfs_sb_info *sbi = ni->mi.sbi; 1866 - u8 cluster_bits = sbi->cluster_bits; 1867 - struct runs_tree run; 1868 - struct ATTRIB *attr; 1869 - CLST vcn = vbo >> cluster_bits; 1870 - CLST lcn, clen; 1871 - u64 valid = ni->i_valid; 1872 - u64 lbo, bytes; 1873 - u64 end, alloc_size; 1874 - size_t idx = -1; 1875 - u32 flags; 1876 - bool ok; 1877 - 1878 - run_init(&run); 1879 - if (S_ISDIR(ni->vfs_inode.i_mode)) { 1880 - attr = ni_find_attr(ni, NULL, NULL, ATTR_ALLOC, I30_NAME, 1881 - ARRAY_SIZE(I30_NAME), NULL, NULL); 1882 - } else { 1883 - attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, NULL, 1884 - NULL); 1885 - if (!attr) { 1886 - err = -EINVAL; 1887 - goto out; 1888 - } 1889 - if (is_attr_compressed(attr)) { 1890 - /* Unfortunately cp -r incorrectly treats compressed clusters. */ 1891 - err = -EOPNOTSUPP; 1892 - ntfs_inode_warn( 1893 - &ni->vfs_inode, 1894 - "fiemap is not supported for compressed file (cp -r)"); 1895 - goto out; 1896 - } 1897 - } 1898 - 1899 - if (!attr || !attr->non_res) { 1900 - err = fiemap_fill_next_extent( 1901 - fieinfo, 0, 0, 1902 - attr ? le32_to_cpu(attr->res.data_size) : 0, 1903 - FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_LAST | 1904 - FIEMAP_EXTENT_MERGED); 1905 - goto out; 1906 - } 1907 - 1908 - end = vbo + len; 1909 - alloc_size = le64_to_cpu(attr->nres.alloc_size); 1910 - if (end > alloc_size) 1911 - end = alloc_size; 1912 - 1913 - while (vbo < end) { 1914 - if (idx == -1) { 1915 - ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx); 1916 - } else { 1917 - CLST vcn_next = vcn; 1918 - 1919 - ok = run_get_entry(&run, ++idx, &vcn, &lcn, &clen) && 1920 - vcn == vcn_next; 1921 - if (!ok) 1922 - vcn = vcn_next; 1923 - } 1924 - 1925 - if (!ok) { 1926 - err = attr_load_runs_vcn(ni, attr->type, 1927 - attr_name(attr), 1928 - attr->name_len, &run, vcn); 1929 - 1930 - if (err) 1931 - break; 1932 - 1933 - ok = run_lookup_entry(&run, vcn, &lcn, &clen, &idx); 1934 - 1935 - if (!ok) { 1936 - err = -EINVAL; 1937 - break; 1938 - } 1939 - } 1940 - 1941 - if (!clen) { 1942 - err = -EINVAL; // ? 1943 - break; 1944 - } 1945 - 1946 - if (lcn == SPARSE_LCN) { 1947 - vcn += clen; 1948 - vbo = (u64)vcn << cluster_bits; 1949 - continue; 1950 - } 1951 - 1952 - flags = FIEMAP_EXTENT_MERGED; 1953 - if (S_ISDIR(ni->vfs_inode.i_mode)) { 1954 - ; 1955 - } else if (is_attr_compressed(attr)) { 1956 - CLST clst_data; 1957 - 1958 - err = attr_is_frame_compressed(ni, attr, 1959 - vcn >> attr->nres.c_unit, 1960 - &clst_data, &run); 1961 - if (err) 1962 - break; 1963 - if (clst_data < NTFS_LZNT_CLUSTERS) 1964 - flags |= FIEMAP_EXTENT_ENCODED; 1965 - } else if (is_attr_encrypted(attr)) { 1966 - flags |= FIEMAP_EXTENT_DATA_ENCRYPTED; 1967 - } 1968 - 1969 - vbo = (u64)vcn << cluster_bits; 1970 - bytes = (u64)clen << cluster_bits; 1971 - lbo = (u64)lcn << cluster_bits; 1972 - 1973 - vcn += clen; 1974 - 1975 - if (vbo + bytes >= end) 1976 - bytes = end - vbo; 1977 - 1978 - if (vbo + bytes <= valid) { 1979 - ; 1980 - } else if (vbo >= valid) { 1981 - flags |= FIEMAP_EXTENT_UNWRITTEN; 1982 - } else { 1983 - /* vbo < valid && valid < vbo + bytes */ 1984 - u64 dlen = valid - vbo; 1985 - 1986 - if (vbo + dlen >= end) 1987 - flags |= FIEMAP_EXTENT_LAST; 1988 - 1989 - err = fiemap_fill_next_extent(fieinfo, vbo, lbo, dlen, 1990 - flags); 1991 - 1992 - if (err < 0) 1993 - break; 1994 - if (err == 1) { 1995 - err = 0; 1996 - break; 1997 - } 1998 - 1999 - vbo = valid; 2000 - bytes -= dlen; 2001 - if (!bytes) 2002 - continue; 2003 - 2004 - lbo += dlen; 2005 - flags |= FIEMAP_EXTENT_UNWRITTEN; 2006 - } 2007 - 2008 - if (vbo + bytes >= end) 2009 - flags |= FIEMAP_EXTENT_LAST; 2010 - 2011 - err = fiemap_fill_next_extent(fieinfo, vbo, lbo, bytes, flags); 2012 - if (err < 0) 2013 - break; 2014 - if (err == 1) { 2015 - err = 0; 2016 - break; 2017 - } 2018 - 2019 - vbo += bytes; 2020 - } 2021 - 2022 - out: 2023 - run_close(&run); 2024 - return err; 2025 - } 2026 - 2027 1853 static struct page *ntfs_lock_new_page(struct address_space *mapping, 2028 - pgoff_t index, gfp_t gfp) 1854 + pgoff_t index, gfp_t gfp) 2029 1855 { 2030 - struct folio *folio = __filemap_get_folio(mapping, index, 2031 - FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); 1856 + struct folio *folio = __filemap_get_folio( 1857 + mapping, index, FGP_LOCK | FGP_ACCESSED | FGP_CREAT, gfp); 2032 1858 struct page *page; 2033 1859 2034 1860 if (IS_ERR(folio)) ··· 1876 2046 } 1877 2047 1878 2048 /* 1879 - * ni_readpage_cmpr 2049 + * ni_read_folio_cmpr 1880 2050 * 1881 2051 * When decompressing, we typically obtain more than one page per reference. 1882 2052 * We inject the additional pages into the page cache. 1883 2053 */ 1884 - int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio) 2054 + int ni_read_folio_cmpr(struct ntfs_inode *ni, struct folio *folio) 1885 2055 { 1886 2056 int err; 1887 2057 struct ntfs_sb_info *sbi = ni->mi.sbi; 1888 2058 struct address_space *mapping = folio->mapping; 1889 - pgoff_t index = folio->index; 1890 - u64 frame_vbo, vbo = (u64)index << PAGE_SHIFT; 2059 + pgoff_t index; 2060 + u64 frame_vbo, vbo = folio_pos(folio); 1891 2061 struct page **pages = NULL; /* Array of at most 16 pages. stack? */ 1892 2062 u8 frame_bits; 1893 2063 CLST frame; ··· 1937 2107 pages[i] = pg; 1938 2108 } 1939 2109 2110 + ni_lock(ni); 1940 2111 err = ni_read_frame(ni, frame_vbo, pages, pages_per_frame, 0); 2112 + ni_unlock(ni); 1941 2113 1942 2114 out1: 1943 2115 for (i = 0; i < pages_per_frame; i++) { ··· 2016 2184 2017 2185 for (vcn = vbo >> sbi->cluster_bits; vcn < end; vcn += clen) { 2018 2186 err = attr_data_get_block(ni, vcn, cend - vcn, &lcn, 2019 - &clen, &new, false); 2187 + &clen, &new, false, NULL, 2188 + false); 2020 2189 if (err) 2021 2190 goto out; 2022 2191 } ··· 2238 2405 struct runs_tree *run = &ni->file.run; 2239 2406 u64 valid_size = ni->i_valid; 2240 2407 u64 vbo_disk; 2241 - size_t unc_size; 2408 + size_t unc_size = 0; 2242 2409 u32 frame_size, i, ondisk_size; 2243 2410 struct page *pg; 2244 2411 struct ATTRIB *attr; ··· 2835 3002 } 2836 3003 2837 3004 /* 3005 + * ni_seek_data_or_hole 3006 + * 3007 + * Helper function for ntfs_llseek( SEEK_DATA/SEEK_HOLE ) 3008 + */ 3009 + loff_t ni_seek_data_or_hole(struct ntfs_inode *ni, loff_t offset, bool data) 3010 + { 3011 + int err; 3012 + u8 cluster_bits = ni->mi.sbi->cluster_bits; 3013 + CLST vcn, lcn, clen; 3014 + loff_t vbo; 3015 + 3016 + /* Enumerate all fragments. */ 3017 + for (vcn = offset >> cluster_bits;; vcn += clen) { 3018 + err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL, false, 3019 + NULL, false); 3020 + if (err) { 3021 + return err; 3022 + } 3023 + 3024 + if (lcn == RESIDENT_LCN) { 3025 + /* clen - resident size in bytes. clen == ni->vfs_inode.i_size */ 3026 + if (offset >= clen) { 3027 + /* check eof. */ 3028 + return -ENXIO; 3029 + } 3030 + 3031 + if (data) { 3032 + return offset; 3033 + } 3034 + 3035 + return clen; 3036 + } 3037 + 3038 + if (lcn == EOF_LCN) { 3039 + if (data) { 3040 + return -ENXIO; 3041 + } 3042 + 3043 + /* implicit hole at the end of file. */ 3044 + return ni->vfs_inode.i_size; 3045 + } 3046 + 3047 + if (data) { 3048 + /* 3049 + * Adjust the file offset to the next location in the file greater than 3050 + * or equal to offset containing data. If offset points to data, then 3051 + * the file offset is set to offset. 3052 + */ 3053 + if (lcn != SPARSE_LCN) { 3054 + vbo = (u64)vcn << cluster_bits; 3055 + return max(vbo, offset); 3056 + } 3057 + } else { 3058 + /* 3059 + * Adjust the file offset to the next hole in the file greater than or 3060 + * equal to offset. If offset points into the middle of a hole, then the 3061 + * file offset is set to offset. If there is no hole past offset, then the 3062 + * file offset is adjusted to the end of the file 3063 + * (i.e., there is an implicit hole at the end of any file). 3064 + */ 3065 + if (lcn == SPARSE_LCN && 3066 + /* native compression hole begins at aligned vcn. */ 3067 + (!(ni->std_fa & FILE_ATTRIBUTE_COMPRESSED) || 3068 + !(vcn & (NTFS_LZNT_CLUSTERS - 1)))) { 3069 + vbo = (u64)vcn << cluster_bits; 3070 + return max(vbo, offset); 3071 + } 3072 + } 3073 + 3074 + if (!clen) { 3075 + /* Corrupted file. */ 3076 + return -EINVAL; 3077 + } 3078 + } 3079 + } 3080 + 3081 + /* 3082 + * ni_write_parents 3083 + * 3084 + * Helper function for ntfs_file_fsync. 3085 + */ 3086 + int ni_write_parents(struct ntfs_inode *ni, int sync) 3087 + { 3088 + int err = 0; 3089 + struct ATTRIB *attr = NULL; 3090 + struct ATTR_LIST_ENTRY *le = NULL; 3091 + struct ntfs_sb_info *sbi = ni->mi.sbi; 3092 + struct super_block *sb = sbi->sb; 3093 + 3094 + while ((attr = ni_find_attr(ni, attr, &le, ATTR_NAME, NULL, 0, NULL, 3095 + NULL))) { 3096 + struct inode *dir; 3097 + struct ATTR_FILE_NAME *fname; 3098 + 3099 + fname = resident_data_ex(attr, SIZEOF_ATTRIBUTE_FILENAME); 3100 + if (!fname) 3101 + continue; 3102 + 3103 + /* Check simple case when parent inode equals current inode. */ 3104 + if (ino_get(&fname->home) == ni->vfs_inode.i_ino) { 3105 + if (MFT_REC_ROOT != ni->vfs_inode.i_ino) { 3106 + ntfs_set_state(sbi, NTFS_DIRTY_ERROR); 3107 + err = -EINVAL; 3108 + } 3109 + continue; 3110 + } 3111 + 3112 + dir = ntfs_iget5(sb, &fname->home, NULL); 3113 + if (IS_ERR(dir)) { 3114 + ntfs_inode_warn( 3115 + &ni->vfs_inode, 3116 + "failed to open parent directory r=%lx to write", 3117 + (long)ino_get(&fname->home)); 3118 + continue; 3119 + } 3120 + 3121 + if (!is_bad_inode(dir)) { 3122 + int err2 = write_inode_now(dir, sync); 3123 + if (!err) 3124 + err = err2; 3125 + } 3126 + iput(dir); 3127 + } 3128 + 3129 + return err; 3130 + } 3131 + 3132 + /* 2838 3133 * ni_update_parent 2839 3134 * 2840 3135 * Update duplicate info of ATTR_FILE_NAME in MFT and in parent directories. ··· 3237 3276 mark_inode_dirty_sync(inode); 3238 3277 3239 3278 return 0; 3279 + } 3280 + 3281 + /* 3282 + * Force to allocate all delay allocated clusters. 3283 + */ 3284 + int ni_allocate_da_blocks(struct ntfs_inode *ni) 3285 + { 3286 + int err; 3287 + 3288 + ni_lock(ni); 3289 + down_write(&ni->file.run_lock); 3290 + 3291 + err = ni_allocate_da_blocks_locked(ni); 3292 + 3293 + up_write(&ni->file.run_lock); 3294 + ni_unlock(ni); 3295 + 3296 + return err; 3297 + } 3298 + 3299 + /* 3300 + * Force to allocate all delay allocated clusters. 3301 + */ 3302 + int ni_allocate_da_blocks_locked(struct ntfs_inode *ni) 3303 + { 3304 + int err; 3305 + 3306 + if (!ni->file.run_da.count) 3307 + return 0; 3308 + 3309 + if (is_sparsed(ni)) { 3310 + CLST vcn, lcn, clen, alen; 3311 + bool new; 3312 + 3313 + /* 3314 + * Sparse file allocates clusters in 'attr_data_get_block_locked' 3315 + */ 3316 + while (run_get_entry(&ni->file.run_da, 0, &vcn, &lcn, &clen)) { 3317 + /* TODO: zero=true? */ 3318 + err = attr_data_get_block_locked(ni, vcn, clen, &lcn, 3319 + &alen, &new, true, 3320 + NULL, true); 3321 + if (err) 3322 + break; 3323 + if (!new) { 3324 + err = -EINVAL; 3325 + break; 3326 + } 3327 + } 3328 + } else { 3329 + /* 3330 + * Normal file allocates clusters in 'attr_set_size' 3331 + */ 3332 + err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run, 3333 + ni->vfs_inode.i_size, &ni->i_valid, 3334 + false, NULL, true); 3335 + } 3336 + 3337 + return err; 3240 3338 }
+35 -30
fs/ntfs3/fslog.c
··· 1074 1074 u32 client_undo_commit; 1075 1075 1076 1076 struct restart_info rst_info, rst_info2; 1077 + 1078 + struct file_ra_state read_ahead; 1077 1079 }; 1078 1080 1079 1081 static inline u32 lsn_to_vbo(struct ntfs_log *log, const u64 lsn) ··· 1166 1164 1167 1165 page_buf = page_off ? log->one_page_buf : *buffer; 1168 1166 1169 - err = ntfs_read_run_nb(ni->mi.sbi, &ni->file.run, page_vbo, page_buf, 1170 - log->page_size, NULL); 1167 + err = ntfs_read_run_nb_ra(ni->mi.sbi, &ni->file.run, page_vbo, page_buf, 1168 + log->page_size, NULL, &log->read_ahead); 1171 1169 if (err) 1172 1170 goto out; 1173 1171 ··· 3031 3029 } 3032 3030 3033 3031 /* 3032 + * update_oa_attr - Synchronize OpenAttr's attribute pointer with modified attribute 3033 + * @oa2: OpenAttr structure in memory that needs to be updated 3034 + * @attr: Modified attribute from MFT record to duplicate 3035 + * 3036 + * Returns true on success, false on allocation failure. 3037 + */ 3038 + static bool update_oa_attr(struct OpenAttr *oa2, struct ATTRIB *attr) 3039 + { 3040 + void *p2; 3041 + 3042 + p2 = kmemdup(attr, le32_to_cpu(attr->size), GFP_NOFS); 3043 + if (p2) { 3044 + kfree(oa2->attr); 3045 + oa2->attr = p2; 3046 + return true; 3047 + } 3048 + return false; 3049 + } 3050 + 3051 + /* 3034 3052 * do_action - Common routine for the Redo and Undo Passes. 3035 3053 * @rlsn: If it is NULL then undo. 3036 3054 */ ··· 3273 3251 le16_add_cpu(&rec->hard_links, 1); 3274 3252 3275 3253 oa2 = find_loaded_attr(log, attr, rno_base); 3276 - if (oa2) { 3277 - void *p2 = kmemdup(attr, le32_to_cpu(attr->size), 3278 - GFP_NOFS); 3279 - if (p2) { 3280 - // run_close(oa2->run1); 3281 - kfree(oa2->attr); 3282 - oa2->attr = p2; 3283 - } 3284 - } 3254 + if (oa2) 3255 + update_oa_attr(oa2, attr); 3285 3256 3286 3257 mi->dirty = true; 3287 3258 break; ··· 3333 3318 memmove(Add2Ptr(attr, aoff), data, dlen); 3334 3319 3335 3320 oa2 = find_loaded_attr(log, attr, rno_base); 3336 - if (oa2) { 3337 - void *p2 = kmemdup(attr, le32_to_cpu(attr->size), 3338 - GFP_NOFS); 3339 - if (p2) { 3340 - // run_close(&oa2->run0); 3341 - oa2->run1 = &oa2->run0; 3342 - kfree(oa2->attr); 3343 - oa2->attr = p2; 3344 - } 3345 - } 3321 + if (oa2 && update_oa_attr(oa2, attr)) 3322 + oa2->run1 = &oa2->run0; 3346 3323 3347 3324 mi->dirty = true; 3348 3325 break; ··· 3384 3377 attr->nres.total_size = new_sz->total_size; 3385 3378 3386 3379 oa2 = find_loaded_attr(log, attr, rno_base); 3387 - if (oa2) { 3388 - void *p2 = kmemdup(attr, le32_to_cpu(attr->size), 3389 - GFP_NOFS); 3390 - if (p2) { 3391 - kfree(oa2->attr); 3392 - oa2->attr = p2; 3393 - } 3394 - } 3380 + if (oa2) 3381 + update_oa_attr(oa2, attr); 3382 + 3395 3383 mi->dirty = true; 3396 3384 break; 3397 3385 ··· 3431 3429 3432 3430 e1 = Add2Ptr(attr, le16_to_cpu(lrh->attr_off)); 3433 3431 esize = le16_to_cpu(e1->size); 3432 + if (PtrOffset(e1, Add2Ptr(hdr, used)) < esize) 3433 + goto dirty_vol; 3434 + 3434 3435 e2 = Add2Ptr(e1, esize); 3435 3436 3436 3437 memmove(e1, e2, PtrOffset(e2, Add2Ptr(hdr, used))); ··· 5133 5128 5134 5129 undo_action_done: 5135 5130 5136 - ntfs_update_mftmirr(sbi, 0); 5131 + ntfs_update_mftmirr(sbi); 5137 5132 5138 5133 sbi->flags &= ~NTFS_FLAGS_NEED_REPLAY; 5139 5134
+70 -42
fs/ntfs3/fsntfs.c
··· 445 445 } 446 446 447 447 /* 448 - * ntfs_check_for_free_space 448 + * ntfs_check_free_space 449 449 * 450 450 * Check if it is possible to allocate 'clen' clusters and 'mlen' Mft records 451 451 */ 452 - bool ntfs_check_for_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen) 452 + bool ntfs_check_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen, 453 + bool da) 453 454 { 454 455 size_t free, zlen, avail; 455 456 struct wnd_bitmap *wnd; 457 + CLST da_clusters = ntfs_get_da(sbi); 456 458 457 459 wnd = &sbi->used.bitmap; 458 460 down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_CLUSTERS); 459 461 free = wnd_zeroes(wnd); 462 + 463 + if (free >= da_clusters) { 464 + free -= da_clusters; 465 + } else { 466 + free = 0; 467 + } 468 + 460 469 zlen = min_t(size_t, NTFS_MIN_MFT_ZONE, wnd_zone_len(wnd)); 461 470 up_read(&wnd->rw_lock); 462 471 463 - if (free < zlen + clen) 472 + if (free < zlen + clen) { 464 473 return false; 474 + } 465 475 466 476 avail = free - (zlen + clen); 467 477 468 - wnd = &sbi->mft.bitmap; 469 - down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT); 470 - free = wnd_zeroes(wnd); 471 - zlen = wnd_zone_len(wnd); 472 - up_read(&wnd->rw_lock); 478 + /* 479 + * When delalloc is active then keep in mind some reserved space. 480 + * The worst case: 1 mft record per each ~500 clusters. 481 + */ 482 + if (da) { 483 + /* 1 mft record per each 1024 clusters. */ 484 + mlen += da_clusters >> 10; 485 + } 473 486 474 - if (free >= zlen + mlen) 475 - return true; 487 + if (mlen || !avail) { 488 + wnd = &sbi->mft.bitmap; 489 + down_read_nested(&wnd->rw_lock, BITMAP_MUTEX_MFT); 490 + free = wnd_zeroes(wnd); 491 + zlen = wnd_zone_len(wnd); 492 + up_read(&wnd->rw_lock); 476 493 477 - return avail >= bytes_to_cluster(sbi, mlen << sbi->record_bits); 494 + if (free < zlen + mlen && 495 + avail < bytes_to_cluster(sbi, mlen << sbi->record_bits)) { 496 + return false; 497 + } 498 + } 499 + 500 + return true; 478 501 } 479 502 480 503 /* ··· 532 509 533 510 /* Step 1: Resize $MFT::DATA. */ 534 511 down_write(&ni->file.run_lock); 535 - err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, 536 - new_mft_bytes, NULL, false, &attr); 512 + err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run, 513 + new_mft_bytes, NULL, false, &attr, false); 537 514 538 515 if (err) { 539 516 up_write(&ni->file.run_lock); ··· 548 525 new_bitmap_bytes = ntfs3_bitmap_size(new_mft_total); 549 526 550 527 err = attr_set_size(ni, ATTR_BITMAP, NULL, 0, &sbi->mft.bitmap.run, 551 - new_bitmap_bytes, &new_bitmap_bytes, true, NULL); 528 + new_bitmap_bytes, &new_bitmap_bytes, true); 552 529 553 530 /* Refresh MFT Zone if necessary. */ 554 531 down_write_nested(&sbi->used.bitmap.rw_lock, BITMAP_MUTEX_CLUSTERS); ··· 866 843 /* 867 844 * ntfs_update_mftmirr - Update $MFTMirr data. 868 845 */ 869 - void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait) 846 + void ntfs_update_mftmirr(struct ntfs_sb_info *sbi) 870 847 { 871 - int err; 872 848 struct super_block *sb = sbi->sb; 873 849 u32 blocksize, bytes; 874 850 sector_t block1, block2; ··· 897 875 return; 898 876 } 899 877 900 - if (buffer_locked(bh2)) 901 - __wait_on_buffer(bh2); 902 - 878 + wait_on_buffer(bh2); 903 879 lock_buffer(bh2); 904 880 memcpy(bh2->b_data, bh1->b_data, blocksize); 905 881 set_buffer_uptodate(bh2); ··· 906 886 907 887 put_bh(bh1); 908 888 bh1 = NULL; 909 - 910 - err = wait ? sync_dirty_buffer(bh2) : 0; 911 - 912 889 put_bh(bh2); 913 - if (err) 914 - return; 915 890 } 916 891 917 892 sbi->flags &= ~NTFS_FLAGS_MFTMIRR; ··· 1084 1069 return -ENOMEM; 1085 1070 } 1086 1071 1087 - if (buffer_locked(bh)) 1088 - __wait_on_buffer(bh); 1089 - 1072 + wait_on_buffer(bh); 1090 1073 lock_buffer(bh); 1091 1074 if (buf) { 1092 1075 memcpy(bh->b_data + off, buf, op); ··· 1181 1168 return ntfs_bread(sb, lbo >> sb->s_blocksize_bits); 1182 1169 } 1183 1170 1184 - int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run, 1185 - u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb) 1171 + int ntfs_read_run_nb_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run, 1172 + u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb, 1173 + struct file_ra_state *ra) 1186 1174 { 1187 1175 int err; 1188 1176 struct super_block *sb = sbi->sb; 1177 + struct address_space *mapping = sb->s_bdev->bd_mapping; 1189 1178 u32 blocksize = sb->s_blocksize; 1190 1179 u8 cluster_bits = sbi->cluster_bits; 1191 1180 u32 off = vbo & sbi->cluster_mask; ··· 1227 1212 nb->bytes = bytes; 1228 1213 } 1229 1214 1215 + if (ra && !ra->ra_pages) 1216 + file_ra_state_init(ra, mapping); 1217 + 1230 1218 for (;;) { 1231 1219 u32 len32 = len >= bytes ? bytes : len; 1232 1220 sector_t block = lbo >> sb->s_blocksize_bits; 1221 + 1222 + if (ra) { 1223 + pgoff_t index = lbo >> PAGE_SHIFT; 1224 + if (!ra_has_index(ra, index)) { 1225 + page_cache_sync_readahead(mapping, ra, NULL, 1226 + index, 1); 1227 + ra->prev_pos = (loff_t)index << PAGE_SHIFT; 1228 + } 1229 + } 1233 1230 1234 1231 do { 1235 1232 u32 op = blocksize - off; ··· 1279 1252 1280 1253 } while (len32); 1281 1254 1255 + if (!run) { 1256 + err = -EINVAL; 1257 + goto out; 1258 + } 1259 + 1260 + /* Get next fragment to read. */ 1282 1261 vcn_next = vcn + clen; 1283 1262 if (!run_get_entry(run, ++idx, &vcn, &lcn, &clen) || 1284 1263 vcn != vcn_next) { ··· 1319 1286 * 1320 1287 * Return: < 0 if error, 0 if ok, -E_NTFS_FIXUP if need to update fixups. 1321 1288 */ 1322 - int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo, 1323 - struct NTFS_RECORD_HEADER *rhdr, u32 bytes, 1324 - struct ntfs_buffers *nb) 1289 + int ntfs_read_bh_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run, 1290 + u64 vbo, struct NTFS_RECORD_HEADER *rhdr, u32 bytes, 1291 + struct ntfs_buffers *nb, struct file_ra_state *ra) 1325 1292 { 1326 - int err = ntfs_read_run_nb(sbi, run, vbo, rhdr, bytes, nb); 1293 + int err = ntfs_read_run_nb_ra(sbi, run, vbo, rhdr, bytes, nb, ra); 1327 1294 1328 1295 if (err) 1329 1296 return err; ··· 1380 1347 err = -ENOMEM; 1381 1348 goto out; 1382 1349 } 1383 - if (buffer_locked(bh)) 1384 - __wait_on_buffer(bh); 1385 - 1350 + wait_on_buffer(bh); 1386 1351 lock_buffer(bh); 1387 - if (!buffer_uptodate(bh)) 1388 - { 1352 + if (!buffer_uptodate(bh)) { 1389 1353 memset(bh->b_data, 0, blocksize); 1390 1354 set_buffer_uptodate(bh); 1391 1355 } ··· 1457 1427 if (op > bytes) 1458 1428 op = bytes; 1459 1429 1460 - if (buffer_locked(bh)) 1461 - __wait_on_buffer(bh); 1462 - 1430 + wait_on_buffer(bh); 1463 1431 lock_buffer(bh); 1464 1432 1465 1433 bh_data = bh->b_data + off; ··· 2214 2186 if (new_sds_size > ni->vfs_inode.i_size) { 2215 2187 err = attr_set_size(ni, ATTR_DATA, SDS_NAME, 2216 2188 ARRAY_SIZE(SDS_NAME), &ni->file.run, 2217 - new_sds_size, &new_sds_size, false, NULL); 2189 + new_sds_size, &new_sds_size, false); 2218 2190 if (err) 2219 2191 goto out; 2220 2192 }
+27 -22
fs/ntfs3/index.c
··· 252 252 253 253 bbuf->bh = bh; 254 254 255 - if (buffer_locked(bh)) 256 - __wait_on_buffer(bh); 257 - 255 + wait_on_buffer(bh); 258 256 lock_buffer(bh); 259 257 260 258 sb = sbi->sb; ··· 1026 1028 } 1027 1029 1028 1030 /* 1029 - * indx_read 1031 + * indx_read_ra 1030 1032 * 1031 1033 * If ntfs_readdir calls this function 1032 1034 * inode is shared locked and no ni_lock. 1033 1035 * Use rw_semaphore for read/write access to alloc_run. 1034 1036 */ 1035 - int indx_read(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn, 1036 - struct indx_node **node) 1037 + int indx_read_ra(struct ntfs_index *indx, struct ntfs_inode *ni, CLST vbn, 1038 + struct indx_node **node, struct file_ra_state *ra) 1037 1039 { 1038 1040 int err; 1039 1041 struct INDEX_BUFFER *ib; 1042 + struct ntfs_sb_info *sbi = ni->mi.sbi; 1040 1043 struct runs_tree *run = &indx->alloc_run; 1041 1044 struct rw_semaphore *lock = &indx->run_lock; 1042 1045 u64 vbo = (u64)vbn << indx->vbn2vbo_bits; ··· 1063 1064 } 1064 1065 1065 1066 down_read(lock); 1066 - err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb); 1067 + err = ntfs_read_bh_ra(sbi, run, vbo, &ib->rhdr, bytes, &in->nb, ra); 1067 1068 up_read(lock); 1068 1069 if (!err) 1069 1070 goto ok; ··· 1083 1084 goto out; 1084 1085 1085 1086 down_read(lock); 1086 - err = ntfs_read_bh(ni->mi.sbi, run, vbo, &ib->rhdr, bytes, &in->nb); 1087 + err = ntfs_read_bh_ra(sbi, run, vbo, &ib->rhdr, bytes, &in->nb, ra); 1087 1088 up_read(lock); 1088 1089 if (err == -E_NTFS_FIXUP) 1089 1090 goto ok; ··· 1099 1100 } 1100 1101 1101 1102 if (err == -E_NTFS_FIXUP) { 1102 - ntfs_write_bh(ni->mi.sbi, &ib->rhdr, &in->nb, 0); 1103 + ntfs_write_bh(sbi, &ib->rhdr, &in->nb, 0); 1103 1104 err = 0; 1104 1105 } 1105 1106 ··· 1189 1190 return -EINVAL; 1190 1191 } 1191 1192 1192 - fnd_push(fnd, node, e); 1193 + err = fnd_push(fnd, node, e); 1194 + 1195 + if (err) { 1196 + put_indx_node(node); 1197 + return err; 1198 + } 1193 1199 } 1194 1200 1195 1201 *entry = e; ··· 1446 1442 1447 1443 run_init(&run); 1448 1444 1449 - err = attr_allocate_clusters(sbi, &run, 0, 0, len, NULL, ALLOCATE_DEF, 1450 - &alen, 0, NULL, NULL); 1445 + err = attr_allocate_clusters(sbi, &run, NULL, 0, 0, len, NULL, 1446 + ALLOCATE_DEF, &alen, 0, NULL, NULL); 1451 1447 if (err) 1452 1448 goto out; 1453 1449 ··· 1531 1527 /* Increase bitmap. */ 1532 1528 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len, 1533 1529 &indx->bitmap_run, 1534 - ntfs3_bitmap_size(bit + 1), NULL, true, 1535 - NULL); 1530 + ntfs3_bitmap_size(bit + 1), NULL, true); 1536 1531 if (err) 1537 1532 goto out1; 1538 1533 } ··· 1552 1549 1553 1550 /* Increase allocation. */ 1554 1551 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len, 1555 - &indx->alloc_run, data_size, &data_size, true, 1556 - NULL); 1552 + &indx->alloc_run, data_size, &data_size, true); 1557 1553 if (err) { 1558 1554 if (bmp) 1559 1555 goto out2; ··· 1570 1568 out2: 1571 1569 /* Ops. No space? */ 1572 1570 attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len, 1573 - &indx->bitmap_run, bmp_size, &bmp_size_v, false, NULL); 1571 + &indx->bitmap_run, bmp_size, &bmp_size_v, false); 1574 1572 1575 1573 out1: 1576 1574 return err; ··· 2000 1998 fnd->level - 1, fnd); 2001 1999 } 2002 2000 2001 + indx->version += 1; 2003 2002 out: 2004 2003 fnd_put(fnd_a); 2005 2004 out1: ··· 2104 2101 new_data = (u64)bit << indx->index_bits; 2105 2102 2106 2103 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len, 2107 - &indx->alloc_run, new_data, &new_data, false, NULL); 2104 + &indx->alloc_run, new_data, &new_data, false); 2108 2105 if (err) 2109 2106 return err; 2110 2107 ··· 2116 2113 return 0; 2117 2114 2118 2115 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len, 2119 - &indx->bitmap_run, bpb, &bpb, false, NULL); 2116 + &indx->bitmap_run, bpb, &bpb, false); 2120 2117 2121 2118 return err; 2122 2119 } ··· 2331 2328 hdr = &root->ihdr; 2332 2329 e = fnd->root_de; 2333 2330 n = NULL; 2331 + ib = NULL; 2334 2332 } 2335 2333 2336 2334 e_size = le16_to_cpu(e->size); ··· 2354 2350 * Check to see if removing that entry made 2355 2351 * the leaf empty. 2356 2352 */ 2357 - if (ib_is_leaf(ib) && ib_is_empty(ib)) { 2353 + if (ib && ib_is_leaf(ib) && ib_is_empty(ib)) { 2358 2354 fnd_pop(fnd); 2359 2355 fnd_push(fnd2, n, e); 2360 2356 } ··· 2602 2598 in = &s_index_names[indx->type]; 2603 2599 2604 2600 err = attr_set_size(ni, ATTR_ALLOC, in->name, in->name_len, 2605 - &indx->alloc_run, 0, NULL, false, NULL); 2601 + &indx->alloc_run, 0, NULL, false); 2606 2602 if (in->name == I30_NAME) 2607 2603 i_size_write(&ni->vfs_inode, 0); 2608 2604 ··· 2611 2607 run_close(&indx->alloc_run); 2612 2608 2613 2609 err = attr_set_size(ni, ATTR_BITMAP, in->name, in->name_len, 2614 - &indx->bitmap_run, 0, NULL, false, NULL); 2610 + &indx->bitmap_run, 0, NULL, false); 2615 2611 err = ni_remove_attr(ni, ATTR_BITMAP, in->name, in->name_len, 2616 2612 false, NULL); 2617 2613 run_close(&indx->bitmap_run); ··· 2649 2645 mi->dirty = true; 2650 2646 } 2651 2647 2648 + indx->version += 1; 2652 2649 out: 2653 2650 fnd_put(fnd2); 2654 2651 out1:
+454 -390
fs/ntfs3/inode.c
··· 12 12 #include <linux/nls.h> 13 13 #include <linux/uio.h> 14 14 #include <linux/writeback.h> 15 + #include <linux/iomap.h> 15 16 16 17 #include "debug.h" 17 18 #include "ntfs.h" ··· 40 39 u32 rp_fa = 0, asize, t32; 41 40 u16 roff, rsize, names = 0, links = 0; 42 41 const struct ATTR_FILE_NAME *fname = NULL; 43 - const struct INDEX_ROOT *root; 42 + const struct INDEX_ROOT *root = NULL; 44 43 struct REPARSE_DATA_BUFFER rp; // 0x18 bytes 45 44 u64 t64; 46 45 struct MFT_REC *rec; ··· 167 166 168 167 std5 = Add2Ptr(attr, roff); 169 168 170 - #ifdef STATX_BTIME 171 169 nt2kernel(std5->cr_time, &ni->i_crtime); 172 - #endif 173 170 nt2kernel(std5->a_time, &ts); 174 171 inode_set_atime_to_ts(inode, ts); 175 172 nt2kernel(std5->c_time, &ts); ··· 554 555 return inode; 555 556 } 556 557 557 - enum get_block_ctx { 558 - GET_BLOCK_GENERAL = 0, 559 - GET_BLOCK_WRITE_BEGIN = 1, 560 - GET_BLOCK_DIRECT_IO_R = 2, 561 - GET_BLOCK_DIRECT_IO_W = 3, 562 - GET_BLOCK_BMAP = 4, 563 - }; 564 - 565 - static noinline int ntfs_get_block_vbo(struct inode *inode, u64 vbo, 566 - struct buffer_head *bh, int create, 567 - enum get_block_ctx ctx) 568 - { 569 - struct super_block *sb = inode->i_sb; 570 - struct ntfs_sb_info *sbi = sb->s_fs_info; 571 - struct ntfs_inode *ni = ntfs_i(inode); 572 - struct folio *folio = bh->b_folio; 573 - u8 cluster_bits = sbi->cluster_bits; 574 - u32 block_size = sb->s_blocksize; 575 - u64 bytes, lbo, valid; 576 - u32 off; 577 - int err; 578 - CLST vcn, lcn, len; 579 - bool new; 580 - 581 - /* Clear previous state. */ 582 - clear_buffer_new(bh); 583 - clear_buffer_uptodate(bh); 584 - 585 - if (is_resident(ni)) { 586 - bh->b_blocknr = RESIDENT_LCN; 587 - bh->b_size = block_size; 588 - if (!folio) { 589 - /* direct io (read) or bmap call */ 590 - err = 0; 591 - } else { 592 - ni_lock(ni); 593 - err = attr_data_read_resident(ni, folio); 594 - ni_unlock(ni); 595 - 596 - if (!err) 597 - set_buffer_uptodate(bh); 598 - } 599 - return err; 600 - } 601 - 602 - vcn = vbo >> cluster_bits; 603 - off = vbo & sbi->cluster_mask; 604 - new = false; 605 - 606 - err = attr_data_get_block(ni, vcn, 1, &lcn, &len, create ? &new : NULL, 607 - create && sbi->cluster_size > PAGE_SIZE); 608 - if (err) 609 - goto out; 610 - 611 - if (!len) 612 - return 0; 613 - 614 - bytes = ((u64)len << cluster_bits) - off; 615 - 616 - if (lcn >= sbi->used.bitmap.nbits) { 617 - /* This case includes resident/compressed/sparse. */ 618 - if (!create) { 619 - if (bh->b_size > bytes) 620 - bh->b_size = bytes; 621 - return 0; 622 - } 623 - WARN_ON(1); 624 - } 625 - 626 - if (new) 627 - set_buffer_new(bh); 628 - 629 - lbo = ((u64)lcn << cluster_bits) + off; 630 - 631 - set_buffer_mapped(bh); 632 - bh->b_bdev = sb->s_bdev; 633 - bh->b_blocknr = lbo >> sb->s_blocksize_bits; 634 - 635 - valid = ni->i_valid; 636 - 637 - if (ctx == GET_BLOCK_DIRECT_IO_W) { 638 - /* ntfs_direct_IO will update ni->i_valid. */ 639 - if (vbo >= valid) 640 - set_buffer_new(bh); 641 - } else if (create) { 642 - /* Normal write. */ 643 - if (bytes > bh->b_size) 644 - bytes = bh->b_size; 645 - 646 - if (vbo >= valid) 647 - set_buffer_new(bh); 648 - 649 - if (vbo + bytes > valid) { 650 - ni->i_valid = vbo + bytes; 651 - mark_inode_dirty(inode); 652 - } 653 - } else if (vbo >= valid) { 654 - /* Read out of valid data. */ 655 - clear_buffer_mapped(bh); 656 - } else if (vbo + bytes <= valid) { 657 - /* Normal read. */ 658 - } else if (vbo + block_size <= valid) { 659 - /* Normal short read. */ 660 - bytes = block_size; 661 - } else { 662 - /* 663 - * Read across valid size: vbo < valid && valid < vbo + block_size 664 - */ 665 - bytes = block_size; 666 - 667 - if (folio) { 668 - u32 voff = valid - vbo; 669 - 670 - bh->b_size = block_size; 671 - off = vbo & (PAGE_SIZE - 1); 672 - folio_set_bh(bh, folio, off); 673 - 674 - if (bh_read(bh, 0) < 0) { 675 - err = -EIO; 676 - goto out; 677 - } 678 - folio_zero_segment(folio, off + voff, off + block_size); 679 - } 680 - } 681 - 682 - if (bh->b_size > bytes) 683 - bh->b_size = bytes; 684 - 685 - #ifndef __LP64__ 686 - if (ctx == GET_BLOCK_DIRECT_IO_W || ctx == GET_BLOCK_DIRECT_IO_R) { 687 - static_assert(sizeof(size_t) < sizeof(loff_t)); 688 - if (bytes > 0x40000000u) 689 - bh->b_size = 0x40000000u; 690 - } 691 - #endif 692 - 693 - return 0; 694 - 695 - out: 696 - return err; 697 - } 698 - 699 - int ntfs_get_block(struct inode *inode, sector_t vbn, 700 - struct buffer_head *bh_result, int create) 701 - { 702 - return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits, 703 - bh_result, create, GET_BLOCK_GENERAL); 704 - } 705 - 706 - static int ntfs_get_block_bmap(struct inode *inode, sector_t vsn, 707 - struct buffer_head *bh_result, int create) 708 - { 709 - return ntfs_get_block_vbo(inode, 710 - (u64)vsn << inode->i_sb->s_blocksize_bits, 711 - bh_result, create, GET_BLOCK_BMAP); 712 - } 713 - 714 558 static sector_t ntfs_bmap(struct address_space *mapping, sector_t block) 715 559 { 716 - return generic_block_bmap(mapping, block, ntfs_get_block_bmap); 560 + struct inode *inode = mapping->host; 561 + struct ntfs_inode *ni = ntfs_i(inode); 562 + 563 + /* 564 + * We can get here for an inline file via the FIBMAP ioctl 565 + */ 566 + if (is_resident(ni)) 567 + return 0; 568 + 569 + if (mapping_tagged(mapping, PAGECACHE_TAG_DIRTY) && 570 + !run_is_empty(&ni->file.run_da)) { 571 + /* 572 + * With delalloc data we want to sync the file so 573 + * that we can make sure we allocate blocks for file and data 574 + * is in place for the user to see it 575 + */ 576 + ni_allocate_da_blocks(ni); 577 + } 578 + 579 + return iomap_bmap(mapping, block, &ntfs_iomap_ops); 717 580 } 581 + 582 + static void ntfs_iomap_read_end_io(struct bio *bio) 583 + { 584 + int error = blk_status_to_errno(bio->bi_status); 585 + struct folio_iter fi; 586 + 587 + bio_for_each_folio_all(fi, bio) { 588 + struct folio *folio = fi.folio; 589 + struct inode *inode = folio->mapping->host; 590 + struct ntfs_inode *ni = ntfs_i(inode); 591 + u64 valid = ni->i_valid; 592 + u32 f_size = folio_size(folio); 593 + loff_t f_pos = folio_pos(folio); 594 + 595 + 596 + if (valid < f_pos + f_size) { 597 + u32 z_from = valid <= f_pos ? 598 + 0 : 599 + offset_in_folio(folio, valid); 600 + /* The only thing ntfs_iomap_read_end_io used for. */ 601 + folio_zero_segment(folio, z_from, f_size); 602 + } 603 + 604 + iomap_finish_folio_read(folio, fi.offset, fi.length, error); 605 + } 606 + bio_put(bio); 607 + } 608 + 609 + /* 610 + * Copied from iomap/bio.c. 611 + */ 612 + static int ntfs_iomap_bio_read_folio_range(const struct iomap_iter *iter, 613 + struct iomap_read_folio_ctx *ctx, 614 + size_t plen) 615 + { 616 + struct folio *folio = ctx->cur_folio; 617 + const struct iomap *iomap = &iter->iomap; 618 + loff_t pos = iter->pos; 619 + size_t poff = offset_in_folio(folio, pos); 620 + loff_t length = iomap_length(iter); 621 + sector_t sector; 622 + struct bio *bio = ctx->read_ctx; 623 + 624 + sector = iomap_sector(iomap, pos); 625 + if (!bio || bio_end_sector(bio) != sector || 626 + !bio_add_folio(bio, folio, plen, poff)) { 627 + gfp_t gfp = mapping_gfp_constraint(folio->mapping, GFP_KERNEL); 628 + gfp_t orig_gfp = gfp; 629 + unsigned int nr_vecs = DIV_ROUND_UP(length, PAGE_SIZE); 630 + 631 + if (bio) 632 + submit_bio(bio); 633 + 634 + if (ctx->rac) /* same as readahead_gfp_mask */ 635 + gfp |= __GFP_NORETRY | __GFP_NOWARN; 636 + bio = bio_alloc(iomap->bdev, bio_max_segs(nr_vecs), REQ_OP_READ, 637 + gfp); 638 + /* 639 + * If the bio_alloc fails, try it again for a single page to 640 + * avoid having to deal with partial page reads. This emulates 641 + * what do_mpage_read_folio does. 642 + */ 643 + if (!bio) 644 + bio = bio_alloc(iomap->bdev, 1, REQ_OP_READ, orig_gfp); 645 + if (ctx->rac) 646 + bio->bi_opf |= REQ_RAHEAD; 647 + bio->bi_iter.bi_sector = sector; 648 + bio->bi_end_io = ntfs_iomap_read_end_io; 649 + bio_add_folio_nofail(bio, folio, plen, poff); 650 + ctx->read_ctx = bio; 651 + } 652 + return 0; 653 + } 654 + 655 + static void ntfs_iomap_bio_submit_read(struct iomap_read_folio_ctx *ctx) 656 + { 657 + struct bio *bio = ctx->read_ctx; 658 + 659 + if (bio) 660 + submit_bio(bio); 661 + } 662 + 663 + static const struct iomap_read_ops ntfs_iomap_bio_read_ops = { 664 + .read_folio_range = ntfs_iomap_bio_read_folio_range, 665 + .submit_read = ntfs_iomap_bio_submit_read, 666 + }; 718 667 719 668 static int ntfs_read_folio(struct file *file, struct folio *folio) 720 669 { ··· 670 723 struct address_space *mapping = folio->mapping; 671 724 struct inode *inode = mapping->host; 672 725 struct ntfs_inode *ni = ntfs_i(inode); 726 + loff_t vbo = folio_pos(folio); 727 + struct iomap_read_folio_ctx ctx = { 728 + .cur_folio = folio, 729 + .ops = &ntfs_iomap_bio_read_ops, 730 + }; 673 731 674 - if (is_resident(ni)) { 675 - ni_lock(ni); 676 - err = attr_data_read_resident(ni, folio); 677 - ni_unlock(ni); 678 - if (err != E_NTFS_NONRESIDENT) { 679 - folio_unlock(folio); 680 - return err; 681 - } 732 + if (unlikely(is_bad_ni(ni))) { 733 + folio_unlock(folio); 734 + return -EIO; 735 + } 736 + 737 + if (ni->i_valid <= vbo) { 738 + folio_zero_range(folio, 0, folio_size(folio)); 739 + folio_mark_uptodate(folio); 740 + folio_unlock(folio); 741 + return 0; 682 742 } 683 743 684 744 if (is_compressed(ni)) { 685 - ni_lock(ni); 686 - err = ni_readpage_cmpr(ni, folio); 687 - ni_unlock(ni); 745 + /* ni_lock is taken inside ni_read_folio_cmpr after page locks */ 746 + err = ni_read_folio_cmpr(ni, folio); 688 747 return err; 689 748 } 690 749 691 - /* Normal + sparse files. */ 692 - return mpage_read_folio(folio, ntfs_get_block); 750 + iomap_read_folio(&ntfs_iomap_ops, &ctx, NULL); 751 + return 0; 693 752 } 694 753 695 754 static void ntfs_readahead(struct readahead_control *rac) ··· 703 750 struct address_space *mapping = rac->mapping; 704 751 struct inode *inode = mapping->host; 705 752 struct ntfs_inode *ni = ntfs_i(inode); 706 - u64 valid; 707 - loff_t pos; 753 + struct iomap_read_folio_ctx ctx = { 754 + .ops = &ntfs_iomap_bio_read_ops, 755 + .rac = rac, 756 + }; 708 757 709 758 if (is_resident(ni)) { 710 759 /* No readahead for resident. */ ··· 718 763 return; 719 764 } 720 765 721 - valid = ni->i_valid; 722 - pos = readahead_pos(rac); 723 - 724 - if (valid < i_size_read(inode) && pos <= valid && 725 - valid < pos + readahead_length(rac)) { 726 - /* Range cross 'valid'. Read it page by page. */ 727 - return; 728 - } 729 - 730 - mpage_readahead(rac, ntfs_get_block); 731 - } 732 - 733 - static int ntfs_get_block_direct_IO_R(struct inode *inode, sector_t iblock, 734 - struct buffer_head *bh_result, int create) 735 - { 736 - return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits, 737 - bh_result, create, GET_BLOCK_DIRECT_IO_R); 738 - } 739 - 740 - static int ntfs_get_block_direct_IO_W(struct inode *inode, sector_t iblock, 741 - struct buffer_head *bh_result, int create) 742 - { 743 - return ntfs_get_block_vbo(inode, (u64)iblock << inode->i_blkbits, 744 - bh_result, create, GET_BLOCK_DIRECT_IO_W); 745 - } 746 - 747 - static ssize_t ntfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter) 748 - { 749 - struct file *file = iocb->ki_filp; 750 - struct address_space *mapping = file->f_mapping; 751 - struct inode *inode = mapping->host; 752 - struct ntfs_inode *ni = ntfs_i(inode); 753 - loff_t vbo = iocb->ki_pos; 754 - loff_t end; 755 - int wr = iov_iter_rw(iter) & WRITE; 756 - size_t iter_count = iov_iter_count(iter); 757 - loff_t valid; 758 - ssize_t ret; 759 - 760 - if (is_resident(ni)) { 761 - /* Switch to buffered write. */ 762 - ret = 0; 763 - goto out; 764 - } 765 - if (is_compressed(ni)) { 766 - ret = 0; 767 - goto out; 768 - } 769 - 770 - ret = blockdev_direct_IO(iocb, inode, iter, 771 - wr ? ntfs_get_block_direct_IO_W : 772 - ntfs_get_block_direct_IO_R); 773 - 774 - if (ret > 0) 775 - end = vbo + ret; 776 - else if (wr && ret == -EIOCBQUEUED) 777 - end = vbo + iter_count; 778 - else 779 - goto out; 780 - 781 - valid = ni->i_valid; 782 - if (wr) { 783 - if (end > valid && !S_ISBLK(inode->i_mode)) { 784 - ni->i_valid = end; 785 - mark_inode_dirty(inode); 786 - } 787 - } else if (vbo < valid && valid < end) { 788 - /* Fix page. */ 789 - iov_iter_revert(iter, end - valid); 790 - iov_iter_zero(end - valid, iter); 791 - } 792 - 793 - out: 794 - return ret; 766 + iomap_readahead(&ntfs_iomap_ops, &ctx, NULL); 795 767 } 796 768 797 769 int ntfs_set_size(struct inode *inode, u64 new_size) ··· 731 849 /* Check for maximum file size. */ 732 850 if (is_sparsed(ni) || is_compressed(ni)) { 733 851 if (new_size > sbi->maxbytes_sparse) { 734 - err = -EFBIG; 735 - goto out; 852 + return -EFBIG; 736 853 } 737 854 } else if (new_size > sbi->maxbytes) { 738 - err = -EFBIG; 739 - goto out; 855 + return -EFBIG; 740 856 } 741 857 742 858 ni_lock(ni); 743 859 down_write(&ni->file.run_lock); 744 860 745 861 err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size, 746 - &ni->i_valid, true, NULL); 862 + &ni->i_valid, true); 863 + 864 + if (!err) { 865 + i_size_write(inode, new_size); 866 + mark_inode_dirty(inode); 867 + } 747 868 748 869 up_write(&ni->file.run_lock); 749 870 ni_unlock(ni); 750 871 751 - mark_inode_dirty(inode); 752 - 753 - out: 754 872 return err; 755 873 } 874 + 875 + /* 876 + * Special value to detect ntfs_writeback_range call 877 + */ 878 + #define WB_NO_DA (struct iomap *)1 879 + /* 880 + * Function to get mapping vbo -> lbo. 881 + * used with: 882 + * - iomap_zero_range 883 + * - iomap_truncate_page 884 + * - iomap_dio_rw 885 + * - iomap_file_buffered_write 886 + * - iomap_bmap 887 + * - iomap_fiemap 888 + * - iomap_bio_read_folio 889 + * - iomap_bio_readahead 890 + */ 891 + static int ntfs_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 892 + unsigned int flags, struct iomap *iomap, 893 + struct iomap *srcmap) 894 + { 895 + struct ntfs_inode *ni = ntfs_i(inode); 896 + struct ntfs_sb_info *sbi = ni->mi.sbi; 897 + u8 cluster_bits = sbi->cluster_bits; 898 + CLST vcn = offset >> cluster_bits; 899 + u32 off = offset & sbi->cluster_mask; 900 + bool rw = flags & IOMAP_WRITE; 901 + loff_t endbyte = offset + length; 902 + void *res = NULL; 903 + int err; 904 + CLST lcn, clen, clen_max = 1; 905 + bool new_clst = false; 906 + bool no_da; 907 + bool zero = false; 908 + if (unlikely(ntfs3_forced_shutdown(sbi->sb))) 909 + return -EIO; 910 + 911 + if (flags & IOMAP_REPORT) { 912 + if (offset > ntfs_get_maxbytes(ni)) { 913 + /* called from fiemap/bmap. */ 914 + return -EINVAL; 915 + } 916 + 917 + if (offset >= inode->i_size) { 918 + /* special code for report. */ 919 + return -ENOENT; 920 + } 921 + } 922 + 923 + if (IOMAP_ZERO == flags && (endbyte & sbi->cluster_mask)) { 924 + rw = true; 925 + } else if (rw) { 926 + clen_max = bytes_to_cluster(sbi, endbyte) - vcn; 927 + } 928 + 929 + /* 930 + * Force to allocate clusters if directIO(write) or writeback_range. 931 + * NOTE: attr_data_get_block allocates clusters only for sparse file. 932 + * Normal file allocates clusters in attr_set_size. 933 + */ 934 + no_da = flags == (IOMAP_DIRECT | IOMAP_WRITE) || srcmap == WB_NO_DA; 935 + 936 + err = attr_data_get_block(ni, vcn, clen_max, &lcn, &clen, 937 + rw ? &new_clst : NULL, zero, &res, no_da); 938 + 939 + if (err) { 940 + return err; 941 + } 942 + 943 + if (lcn == EOF_LCN) { 944 + /* request out of file. */ 945 + if (flags & IOMAP_REPORT) { 946 + /* special code for report. */ 947 + return -ENOENT; 948 + } 949 + 950 + if (rw) { 951 + /* should never be here. */ 952 + return -EINVAL; 953 + } 954 + lcn = SPARSE_LCN; 955 + } 956 + 957 + iomap->flags = new_clst ? IOMAP_F_NEW : 0; 958 + 959 + if (lcn == RESIDENT_LCN) { 960 + if (offset >= clen) { 961 + kfree(res); 962 + if (flags & IOMAP_REPORT) { 963 + /* special code for report. */ 964 + return -ENOENT; 965 + } 966 + return -EFAULT; 967 + } 968 + 969 + iomap->private = iomap->inline_data = res; 970 + iomap->type = IOMAP_INLINE; 971 + iomap->offset = 0; 972 + iomap->length = clen; /* resident size in bytes. */ 973 + return 0; 974 + } 975 + 976 + if (!clen) { 977 + /* broken file? */ 978 + return -EINVAL; 979 + } 980 + 981 + iomap->bdev = inode->i_sb->s_bdev; 982 + iomap->offset = offset; 983 + iomap->length = ((loff_t)clen << cluster_bits) - off; 984 + 985 + if (lcn == COMPRESSED_LCN) { 986 + /* should never be here. */ 987 + return -EOPNOTSUPP; 988 + } 989 + 990 + if (lcn == DELALLOC_LCN) { 991 + iomap->type = IOMAP_DELALLOC; 992 + iomap->addr = IOMAP_NULL_ADDR; 993 + } else { 994 + 995 + /* Translate clusters into bytes. */ 996 + iomap->addr = ((loff_t)lcn << cluster_bits) + off; 997 + if (length && iomap->length > length) 998 + iomap->length = length; 999 + else 1000 + endbyte = offset + iomap->length; 1001 + 1002 + if (lcn == SPARSE_LCN) { 1003 + iomap->addr = IOMAP_NULL_ADDR; 1004 + iomap->type = IOMAP_HOLE; 1005 + // if (IOMAP_ZERO == flags && !off) { 1006 + // iomap->length = (endbyte - offset) & 1007 + // sbi->cluster_mask_inv; 1008 + // } 1009 + } else if (endbyte <= ni->i_valid) { 1010 + iomap->type = IOMAP_MAPPED; 1011 + } else if (offset < ni->i_valid) { 1012 + iomap->type = IOMAP_MAPPED; 1013 + if (flags & IOMAP_REPORT) 1014 + iomap->length = ni->i_valid - offset; 1015 + } else if (rw || (flags & IOMAP_ZERO)) { 1016 + iomap->type = IOMAP_MAPPED; 1017 + } else { 1018 + iomap->type = IOMAP_UNWRITTEN; 1019 + } 1020 + } 1021 + 1022 + if ((flags & IOMAP_ZERO) && 1023 + (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) { 1024 + /* Avoid too large requests. */ 1025 + u32 tail; 1026 + u32 off_a = offset & (PAGE_SIZE - 1); 1027 + if (off_a) 1028 + tail = PAGE_SIZE - off_a; 1029 + else 1030 + tail = PAGE_SIZE; 1031 + 1032 + if (iomap->length > tail) 1033 + iomap->length = tail; 1034 + } 1035 + 1036 + return 0; 1037 + } 1038 + 1039 + static int ntfs_iomap_end(struct inode *inode, loff_t pos, loff_t length, 1040 + ssize_t written, unsigned int flags, 1041 + struct iomap *iomap) 1042 + { 1043 + int err = 0; 1044 + struct ntfs_inode *ni = ntfs_i(inode); 1045 + loff_t endbyte = pos + written; 1046 + 1047 + if ((flags & IOMAP_WRITE) || (flags & IOMAP_ZERO)) { 1048 + if (iomap->type == IOMAP_INLINE) { 1049 + u32 data_size; 1050 + struct ATTRIB *attr; 1051 + struct mft_inode *mi; 1052 + 1053 + attr = ni_find_attr(ni, NULL, NULL, ATTR_DATA, NULL, 0, 1054 + NULL, &mi); 1055 + if (!attr || attr->non_res) { 1056 + err = -EINVAL; 1057 + goto out; 1058 + } 1059 + 1060 + data_size = le32_to_cpu(attr->res.data_size); 1061 + if (!(pos < data_size && endbyte <= data_size)) { 1062 + err = -EINVAL; 1063 + goto out; 1064 + } 1065 + 1066 + /* Update resident data. */ 1067 + memcpy(resident_data(attr) + pos, 1068 + iomap_inline_data(iomap, pos), written); 1069 + mi->dirty = true; 1070 + ni->i_valid = data_size; 1071 + } else if (ni->i_valid < endbyte) { 1072 + ni->i_valid = endbyte; 1073 + mark_inode_dirty(inode); 1074 + } 1075 + } 1076 + 1077 + if ((flags & IOMAP_ZERO) && 1078 + (iomap->type == IOMAP_MAPPED || iomap->type == IOMAP_DELALLOC)) { 1079 + /* Pair for code in ntfs_iomap_begin. */ 1080 + balance_dirty_pages_ratelimited(inode->i_mapping); 1081 + cond_resched(); 1082 + } 1083 + 1084 + out: 1085 + if (iomap->type == IOMAP_INLINE) { 1086 + kfree(iomap->private); 1087 + iomap->private = NULL; 1088 + } 1089 + 1090 + return err; 1091 + } 1092 + 1093 + /* 1094 + * write_begin + put_folio + write_end. 1095 + * iomap_zero_range 1096 + * iomap_truncate_page 1097 + * iomap_file_buffered_write 1098 + */ 1099 + static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos, 1100 + unsigned int len, struct folio *folio) 1101 + { 1102 + struct ntfs_inode *ni = ntfs_i(inode); 1103 + loff_t end = pos + len; 1104 + u32 f_size = folio_size(folio); 1105 + loff_t f_pos = folio_pos(folio); 1106 + loff_t f_end = f_pos + f_size; 1107 + 1108 + if (ni->i_valid <= end && end < f_end) { 1109 + /* zero range [end - f_end). */ 1110 + /* The only thing ntfs_iomap_put_folio used for. */ 1111 + folio_zero_segment(folio, offset_in_folio(folio, end), f_size); 1112 + } 1113 + folio_unlock(folio); 1114 + folio_put(folio); 1115 + } 1116 + 1117 + /* 1118 + * iomap_writeback_ops::writeback_range 1119 + */ 1120 + static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc, 1121 + struct folio *folio, u64 offset, 1122 + unsigned int len, u64 end_pos) 1123 + { 1124 + struct iomap *iomap = &wpc->iomap; 1125 + /* Check iomap position. */ 1126 + if (iomap->offset + iomap->length <= offset || offset < iomap->offset) { 1127 + int err; 1128 + struct inode *inode = wpc->inode; 1129 + struct ntfs_inode *ni = ntfs_i(inode); 1130 + struct ntfs_sb_info *sbi = ntfs_sb(inode->i_sb); 1131 + loff_t i_size_up = ntfs_up_cluster(sbi, inode->i_size); 1132 + loff_t len_max = i_size_up - offset; 1133 + 1134 + err = ni->file.run_da.count ? ni_allocate_da_blocks(ni) : 0; 1135 + 1136 + if (!err) { 1137 + /* Use local special value 'WB_NO_DA' to disable delalloc. */ 1138 + err = ntfs_iomap_begin(inode, offset, len_max, 1139 + IOMAP_WRITE, iomap, WB_NO_DA); 1140 + } 1141 + 1142 + if (err) { 1143 + ntfs_set_state(sbi, NTFS_DIRTY_DIRTY); 1144 + return err; 1145 + } 1146 + } 1147 + 1148 + return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 1149 + } 1150 + 1151 + 1152 + static const struct iomap_writeback_ops ntfs_writeback_ops = { 1153 + .writeback_range = ntfs_writeback_range, 1154 + .writeback_submit = iomap_ioend_writeback_submit, 1155 + }; 756 1156 757 1157 static int ntfs_resident_writepage(struct folio *folio, 758 1158 struct writeback_control *wbc) ··· 1064 900 static int ntfs_writepages(struct address_space *mapping, 1065 901 struct writeback_control *wbc) 1066 902 { 1067 - struct inode *inode = mapping->host; 1068 - 1069 - /* Avoid any operation if inode is bad. */ 1070 - if (unlikely(is_bad_ni(ntfs_i(inode)))) 1071 - return -EINVAL; 1072 - 1073 - if (unlikely(ntfs3_forced_shutdown(inode->i_sb))) 1074 - return -EIO; 1075 - 1076 - if (is_resident(ntfs_i(inode))) { 1077 - struct folio *folio = NULL; 1078 - int error; 1079 - 1080 - while ((folio = writeback_iter(mapping, wbc, folio, &error))) 1081 - error = ntfs_resident_writepage(folio, wbc); 1082 - return error; 1083 - } 1084 - return mpage_writepages(mapping, wbc, ntfs_get_block); 1085 - } 1086 - 1087 - static int ntfs_get_block_write_begin(struct inode *inode, sector_t vbn, 1088 - struct buffer_head *bh_result, int create) 1089 - { 1090 - return ntfs_get_block_vbo(inode, (u64)vbn << inode->i_blkbits, 1091 - bh_result, create, GET_BLOCK_WRITE_BEGIN); 1092 - } 1093 - 1094 - int ntfs_write_begin(const struct kiocb *iocb, struct address_space *mapping, 1095 - loff_t pos, u32 len, struct folio **foliop, void **fsdata) 1096 - { 1097 903 int err; 1098 904 struct inode *inode = mapping->host; 1099 905 struct ntfs_inode *ni = ntfs_i(inode); 906 + struct iomap_writepage_ctx wpc = { 907 + .inode = mapping->host, 908 + .wbc = wbc, 909 + .ops = &ntfs_writeback_ops, 910 + }; 1100 911 1101 912 /* Avoid any operation if inode is bad. */ 1102 913 if (unlikely(is_bad_ni(ni))) ··· 1081 942 return -EIO; 1082 943 1083 944 if (is_resident(ni)) { 1084 - struct folio *folio = __filemap_get_folio( 1085 - mapping, pos >> PAGE_SHIFT, FGP_WRITEBEGIN, 1086 - mapping_gfp_mask(mapping)); 945 + struct folio *folio = NULL; 1087 946 1088 - if (IS_ERR(folio)) { 1089 - err = PTR_ERR(folio); 1090 - goto out; 1091 - } 947 + while ((folio = writeback_iter(mapping, wbc, folio, &err))) 948 + err = ntfs_resident_writepage(folio, wbc); 1092 949 1093 - ni_lock(ni); 1094 - err = attr_data_read_resident(ni, folio); 1095 - ni_unlock(ni); 1096 - 1097 - if (!err) { 1098 - *foliop = folio; 1099 - goto out; 1100 - } 1101 - folio_unlock(folio); 1102 - folio_put(folio); 1103 - 1104 - if (err != E_NTFS_NONRESIDENT) 1105 - goto out; 950 + return err; 1106 951 } 1107 952 1108 - err = block_write_begin(mapping, pos, len, foliop, 1109 - ntfs_get_block_write_begin); 1110 - 1111 - out: 1112 - return err; 1113 - } 1114 - 1115 - /* 1116 - * ntfs_write_end - Address_space_operations::write_end. 1117 - */ 1118 - int ntfs_write_end(const struct kiocb *iocb, struct address_space *mapping, 1119 - loff_t pos, u32 len, u32 copied, struct folio *folio, 1120 - void *fsdata) 1121 - { 1122 - struct inode *inode = mapping->host; 1123 - struct ntfs_inode *ni = ntfs_i(inode); 1124 - u64 valid = ni->i_valid; 1125 - bool dirty = false; 1126 - int err; 1127 - 1128 - if (is_resident(ni)) { 1129 - ni_lock(ni); 1130 - err = attr_data_write_resident(ni, folio); 1131 - ni_unlock(ni); 1132 - if (!err) { 1133 - struct buffer_head *head = folio_buffers(folio); 1134 - dirty = true; 1135 - /* Clear any buffers in folio. */ 1136 - if (head) { 1137 - struct buffer_head *bh = head; 1138 - 1139 - do { 1140 - clear_buffer_dirty(bh); 1141 - clear_buffer_mapped(bh); 1142 - set_buffer_uptodate(bh); 1143 - } while (head != (bh = bh->b_this_page)); 1144 - } 1145 - folio_mark_uptodate(folio); 1146 - err = copied; 1147 - } 1148 - folio_unlock(folio); 1149 - folio_put(folio); 1150 - } else { 1151 - err = generic_write_end(iocb, mapping, pos, len, copied, folio, 1152 - fsdata); 1153 - } 1154 - 1155 - if (err >= 0) { 1156 - if (!(ni->std_fa & FILE_ATTRIBUTE_ARCHIVE)) { 1157 - inode_set_mtime_to_ts(inode, 1158 - inode_set_ctime_current(inode)); 1159 - ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE; 1160 - dirty = true; 1161 - } 1162 - 1163 - if (valid != ni->i_valid) { 1164 - /* ni->i_valid is changed in ntfs_get_block_vbo. */ 1165 - dirty = true; 1166 - } 1167 - 1168 - if (pos + err > inode->i_size) { 1169 - i_size_write(inode, pos + err); 1170 - dirty = true; 1171 - } 1172 - 1173 - if (dirty) 1174 - mark_inode_dirty(inode); 1175 - } 1176 - 1177 - return err; 953 + return iomap_writepages(&wpc); 1178 954 } 1179 955 1180 956 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc) ··· 1104 1050 1105 1051 /* 1106 1052 * Helper function to read file. 1053 + * Used to read $AttrDef and $UpCase 1107 1054 */ 1108 1055 int inode_read_data(struct inode *inode, void *data, size_t bytes) 1109 1056 { ··· 1594 1539 attr->nres.alloc_size = 1595 1540 cpu_to_le64(ntfs_up_cluster(sbi, nsize)); 1596 1541 1597 - err = attr_allocate_clusters(sbi, &ni->file.run, 0, 0, 1598 - clst, NULL, ALLOCATE_DEF, 1599 - &alen, 0, NULL, NULL); 1542 + err = attr_allocate_clusters(sbi, &ni->file.run, NULL, 1543 + 0, 0, clst, NULL, 1544 + ALLOCATE_DEF, &alen, 0, 1545 + NULL, NULL); 1600 1546 if (err) 1601 1547 goto out5; 1602 1548 ··· 1738 1682 /* Delete ATTR_EA, if non-resident. */ 1739 1683 struct runs_tree run; 1740 1684 run_init(&run); 1741 - attr_set_size(ni, ATTR_EA, NULL, 0, &run, 0, NULL, false, NULL); 1685 + attr_set_size(ni, ATTR_EA, NULL, 0, &run, 0, NULL, false); 1742 1686 run_close(&run); 1743 1687 } 1744 1688 ··· 2150 2094 .read_folio = ntfs_read_folio, 2151 2095 .readahead = ntfs_readahead, 2152 2096 .writepages = ntfs_writepages, 2153 - .write_begin = ntfs_write_begin, 2154 - .write_end = ntfs_write_end, 2155 - .direct_IO = ntfs_direct_IO, 2156 2097 .bmap = ntfs_bmap, 2157 - .dirty_folio = block_dirty_folio, 2158 - .migrate_folio = buffer_migrate_folio, 2159 - .invalidate_folio = block_invalidate_folio, 2098 + .dirty_folio = iomap_dirty_folio, 2099 + .migrate_folio = filemap_migrate_folio, 2100 + .release_folio = iomap_release_folio, 2101 + .invalidate_folio = iomap_invalidate_folio, 2160 2102 }; 2161 2103 2162 2104 const struct address_space_operations ntfs_aops_cmpr = { 2163 2105 .read_folio = ntfs_read_folio, 2164 - .dirty_folio = block_dirty_folio, 2165 - .direct_IO = ntfs_direct_IO, 2106 + .dirty_folio = iomap_dirty_folio, 2107 + .release_folio = iomap_release_folio, 2108 + .invalidate_folio = iomap_invalidate_folio, 2109 + }; 2110 + 2111 + const struct iomap_ops ntfs_iomap_ops = { 2112 + .iomap_begin = ntfs_iomap_begin, 2113 + .iomap_end = ntfs_iomap_end, 2114 + }; 2115 + 2116 + const struct iomap_write_ops ntfs_iomap_folio_ops = { 2117 + .put_folio = ntfs_iomap_put_folio, 2166 2118 }; 2167 2119 // clang-format on
+4
fs/ntfs3/ntfs.h
··· 77 77 typedef u32 CLST; 78 78 #endif 79 79 80 + /* On-disk sparsed cluster is marked as -1. */ 80 81 #define SPARSE_LCN64 ((u64)-1) 81 82 #define SPARSE_LCN ((CLST)-1) 83 + /* Below is virtual (not on-disk) values. */ 82 84 #define RESIDENT_LCN ((CLST)-2) 83 85 #define COMPRESSED_LCN ((CLST)-3) 86 + #define EOF_LCN ((CLST)-4) 87 + #define DELALLOC_LCN ((CLST)-5) 84 88 85 89 enum RECORD_NUM { 86 90 MFT_REC_MFT = 0,
+121 -32
fs/ntfs3/ntfs_fs.h
··· 109 109 unsigned force : 1; /* RW mount dirty volume. */ 110 110 unsigned prealloc : 1; /* Preallocate space when file is growing. */ 111 111 unsigned nocase : 1; /* case insensitive. */ 112 + unsigned delalloc : 1; /* delay allocation. */ 112 113 }; 113 114 114 115 /* Special value to unpack and deallocate. */ ··· 134 133 enum ALLOCATE_OPT { 135 134 ALLOCATE_DEF = 0, // Allocate all clusters. 136 135 ALLOCATE_MFT = 1, // Allocate for MFT. 137 - ALLOCATE_ZERO = 2, // Zeroout new allocated clusters 136 + ALLOCATE_ZERO = 2, // Zeroout new allocated clusters. 137 + ALLOCATE_ONE_FR = 4, // Allocate one fragment only. 138 138 }; 139 139 140 140 enum bitmap_mutex_classes { ··· 194 192 struct runs_tree alloc_run; 195 193 /* read/write access to 'bitmap_run'/'alloc_run' while ntfs_readdir */ 196 194 struct rw_semaphore run_lock; 195 + size_t version; /* increment each change */ 197 196 198 197 /*TODO: Remove 'cmp'. */ 199 198 NTFS_CMP_FUNC cmp; ··· 216 213 217 214 u32 discard_granularity; 218 215 u64 discard_granularity_mask_inv; // ~(discard_granularity_mask_inv-1) 219 - u32 bdev_blocksize_mask; // bdev_logical_block_size(bdev) - 1; 216 + u32 bdev_blocksize; // bdev_logical_block_size(bdev) 220 217 221 218 u32 cluster_size; // bytes per cluster 222 219 u32 cluster_mask; // == cluster_size - 1 ··· 275 272 struct { 276 273 struct wnd_bitmap bitmap; // $Bitmap::Data 277 274 CLST next_free_lcn; 275 + /* Total sum of delay allocated clusters in all files. */ 276 + #ifdef CONFIG_NTFS3_64BIT_CLUSTER 277 + atomic64_t da; 278 + #else 279 + atomic_t da; 280 + #endif 278 281 } used; 279 282 280 283 struct { ··· 388 379 */ 389 380 u8 mi_loaded; 390 381 391 - /* 382 + /* 392 383 * Use this field to avoid any write(s). 393 384 * If inode is bad during initialization - use make_bad_inode 394 385 * If inode is bad during operations - use this field ··· 399 390 struct ntfs_index dir; 400 391 struct { 401 392 struct rw_semaphore run_lock; 393 + /* Unpacked runs from just one record. */ 402 394 struct runs_tree run; 395 + /* 396 + * Pairs [vcn, len] for all delay allocated clusters. 397 + * Normal file always contains delayed clusters in one fragment. 398 + * TODO: use 2 CLST per pair instead of 3. 399 + */ 400 + struct runs_tree run_da; 403 401 #ifdef CONFIG_NTFS3_LZX_XPRESS 404 402 struct folio *offs_folio; 405 403 #endif ··· 446 430 447 431 /* Functions from attrib.c */ 448 432 int attr_allocate_clusters(struct ntfs_sb_info *sbi, struct runs_tree *run, 449 - CLST vcn, CLST lcn, CLST len, CLST *pre_alloc, 450 - enum ALLOCATE_OPT opt, CLST *alen, const size_t fr, 451 - CLST *new_lcn, CLST *new_len); 433 + struct runs_tree *run_da, CLST vcn, CLST lcn, 434 + CLST len, CLST *pre_alloc, enum ALLOCATE_OPT opt, 435 + CLST *alen, const size_t fr, CLST *new_lcn, 436 + CLST *new_len); 452 437 int attr_make_nonresident(struct ntfs_inode *ni, struct ATTRIB *attr, 453 438 struct ATTR_LIST_ENTRY *le, struct mft_inode *mi, 454 439 u64 new_size, struct runs_tree *run, 455 440 struct ATTRIB **ins_attr, struct page *page); 456 - int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type, 457 - const __le16 *name, u8 name_len, struct runs_tree *run, 458 - u64 new_size, const u64 *new_valid, bool keep_prealloc, 459 - struct ATTRIB **ret); 441 + int attr_set_size_ex(struct ntfs_inode *ni, enum ATTR_TYPE type, 442 + const __le16 *name, u8 name_len, struct runs_tree *run, 443 + u64 new_size, const u64 *new_valid, bool keep_prealloc, 444 + struct ATTRIB **ret, bool no_da); 445 + static inline int attr_set_size(struct ntfs_inode *ni, enum ATTR_TYPE type, 446 + const __le16 *name, u8 name_len, 447 + struct runs_tree *run, u64 new_size, 448 + const u64 *new_valid, bool keep_prealloc) 449 + { 450 + return attr_set_size_ex(ni, type, name, name_len, run, new_size, 451 + new_valid, keep_prealloc, NULL, false); 452 + } 460 453 int attr_data_get_block(struct ntfs_inode *ni, CLST vcn, CLST clen, CLST *lcn, 461 - CLST *len, bool *new, bool zero); 462 - int attr_data_read_resident(struct ntfs_inode *ni, struct folio *folio); 454 + CLST *len, bool *new, bool zero, void **res, 455 + bool no_da); 456 + int attr_data_get_block_locked(struct ntfs_inode *ni, CLST vcn, CLST clen, 457 + CLST *lcn, CLST *len, bool *new, bool zero, 458 + void **res, bool no_da); 463 459 int attr_data_write_resident(struct ntfs_inode *ni, struct folio *folio); 464 460 int attr_load_runs_vcn(struct ntfs_inode *ni, enum ATTR_TYPE type, 465 461 const __le16 *name, u8 name_len, struct runs_tree *run, ··· 540 512 int ntfs_file_open(struct inode *inode, struct file *file); 541 513 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo, 542 514 __u64 start, __u64 len); 515 + int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync); 543 516 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg); 544 517 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg); 545 518 extern const struct inode_operations ntfs_special_inode_operations; ··· 596 567 struct REPARSE_DATA_BUFFER *buffer); 597 568 int ni_write_inode(struct inode *inode, int sync, const char *hint); 598 569 #define _ni_write_inode(i, w) ni_write_inode(i, w, __func__) 599 - int ni_fiemap(struct ntfs_inode *ni, struct fiemap_extent_info *fieinfo, 600 - __u64 vbo, __u64 len); 601 - int ni_readpage_cmpr(struct ntfs_inode *ni, struct folio *folio); 570 + int ni_read_folio_cmpr(struct ntfs_inode *ni, struct folio *folio); 602 571 int ni_decompress_file(struct ntfs_inode *ni); 603 572 int ni_read_frame(struct ntfs_inode *ni, u64 frame_vbo, struct page **pages, 604 573 u32 pages_per_frame, int copy); ··· 617 590 struct NTFS_DE *new_de); 618 591 619 592 bool ni_is_dirty(struct inode *inode); 593 + loff_t ni_seek_data_or_hole(struct ntfs_inode *ni, loff_t offset, bool data); 594 + int ni_write_parents(struct ntfs_inode *ni, int sync); 595 + int ni_allocate_da_blocks(struct ntfs_inode *ni); 596 + int ni_allocate_da_blocks_locked(struct ntfs_inode *ni); 620 597 621 598 /* Globals from fslog.c */ 622 599 bool check_index_header(const struct INDEX_HDR *hdr, size_t bytes); ··· 636 605 int ntfs_look_for_free_space(struct ntfs_sb_info *sbi, CLST lcn, CLST len, 637 606 CLST *new_lcn, CLST *new_len, 638 607 enum ALLOCATE_OPT opt); 639 - bool ntfs_check_for_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen); 608 + bool ntfs_check_free_space(struct ntfs_sb_info *sbi, CLST clen, CLST mlen, 609 + bool da); 640 610 int ntfs_look_free_mft(struct ntfs_sb_info *sbi, CLST *rno, bool mft, 641 611 struct ntfs_inode *ni, struct mft_inode **mi); 642 612 void ntfs_mark_rec_free(struct ntfs_sb_info *sbi, CLST rno, bool is_mft); 643 613 int ntfs_clear_mft_tail(struct ntfs_sb_info *sbi, size_t from, size_t to); 644 614 int ntfs_refresh_zone(struct ntfs_sb_info *sbi); 645 - void ntfs_update_mftmirr(struct ntfs_sb_info *sbi, int wait); 615 + void ntfs_update_mftmirr(struct ntfs_sb_info *sbi); 646 616 void ntfs_bad_inode(struct inode *inode, const char *hint); 647 617 #define _ntfs_bad_inode(i) ntfs_bad_inode(i, __func__) 648 618 enum NTFS_DIRTY_FLAGS { ··· 658 626 u64 vbo, const void *buf, size_t bytes, int sync); 659 627 struct buffer_head *ntfs_bread_run(struct ntfs_sb_info *sbi, 660 628 const struct runs_tree *run, u64 vbo); 661 - int ntfs_read_run_nb(struct ntfs_sb_info *sbi, const struct runs_tree *run, 662 - u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb); 663 - int ntfs_read_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo, 664 - struct NTFS_RECORD_HEADER *rhdr, u32 bytes, 665 - struct ntfs_buffers *nb); 629 + int ntfs_read_run_nb_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run, 630 + u64 vbo, void *buf, u32 bytes, struct ntfs_buffers *nb, 631 + struct file_ra_state *ra); 632 + static inline int ntfs_read_run_nb(struct ntfs_sb_info *sbi, 633 + const struct runs_tree *run, u64 vbo, 634 + void *buf, u32 bytes, 635 + struct ntfs_buffers *nb) 636 + { 637 + return ntfs_read_run_nb_ra(sbi, run, vbo, buf, bytes, nb, NULL); 638 + } 639 + int ntfs_read_bh_ra(struct ntfs_sb_info *sbi, const struct runs_tree *run, 640 + u64 vbo, struct NTFS_RECORD_HEADER *rhdr, u32 bytes, 641 + struct ntfs_buffers *nb, struct file_ra_state *ra); 642 + static inline int ntfs_read_bh(struct ntfs_sb_info *sbi, 643 + const struct runs_tree *run, u64 vbo, 644 + struct NTFS_RECORD_HEADER *rhdr, u32 bytes, 645 + struct ntfs_buffers *nb) 646 + { 647 + return ntfs_read_bh_ra(sbi, run, vbo, rhdr, bytes, nb, NULL); 648 + } 649 + 666 650 int ntfs_get_bh(struct ntfs_sb_info *sbi, const struct runs_tree *run, u64 vbo, 667 651 u32 bytes, struct ntfs_buffers *nb); 668 652 int ntfs_write_bh(struct ntfs_sb_info *sbi, struct NTFS_RECORD_HEADER *rhdr, ··· 744 696 const struct ATTRIB *attr, enum index_mutex_classed type); 745 697 struct INDEX_ROOT *indx_get_root(struct ntfs_index *indx, struct ntfs_inode *ni, 746 698 struct ATTRIB **attr, struct mft_inode **mi); 747 - int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn, 748 - struct indx_node **node); 699 + int indx_read_ra(struct ntfs_index *idx, struct ntfs_inode *ni, CLST vbn, 700 + struct indx_node **node, struct file_ra_state *ra); 701 + static inline int indx_read(struct ntfs_index *idx, struct ntfs_inode *ni, 702 + CLST vbn, struct indx_node **node) 703 + { 704 + return indx_read_ra(idx, ni, vbn, node, NULL); 705 + } 749 706 int indx_find(struct ntfs_index *indx, struct ntfs_inode *dir, 750 707 const struct INDEX_ROOT *root, const void *Key, size_t KeyLen, 751 708 const void *param, int *diff, struct NTFS_DE **entry, ··· 774 721 struct inode *ntfs_iget5(struct super_block *sb, const struct MFT_REF *ref, 775 722 const struct cpu_str *name); 776 723 int ntfs_set_size(struct inode *inode, u64 new_size); 777 - int ntfs_get_block(struct inode *inode, sector_t vbn, 778 - struct buffer_head *bh_result, int create); 779 - int ntfs_write_begin(const struct kiocb *iocb, struct address_space *mapping, 780 - loff_t pos, u32 len, struct folio **foliop, void **fsdata); 781 - int ntfs_write_end(const struct kiocb *iocb, struct address_space *mapping, 782 - loff_t pos, u32 len, u32 copied, struct folio *folio, 783 - void *fsdata); 784 724 int ntfs3_write_inode(struct inode *inode, struct writeback_control *wbc); 785 725 int ntfs_sync_inode(struct inode *inode); 786 726 int inode_read_data(struct inode *inode, void *data, size_t bytes); ··· 784 738 int ntfs_link_inode(struct inode *inode, struct dentry *dentry); 785 739 int ntfs_unlink_inode(struct inode *dir, const struct dentry *dentry); 786 740 void ntfs_evict_inode(struct inode *inode); 741 + extern const struct iomap_ops ntfs_iomap_ops; 742 + extern const struct iomap_write_ops ntfs_iomap_folio_ops; 787 743 extern const struct inode_operations ntfs_link_inode_operations; 788 744 extern const struct address_space_operations ntfs_aops; 789 745 extern const struct address_space_operations ntfs_aops_cmpr; ··· 863 815 bool run_add_entry(struct runs_tree *run, CLST vcn, CLST lcn, CLST len, 864 816 bool is_mft); 865 817 bool run_collapse_range(struct runs_tree *run, CLST vcn, CLST len, CLST sub); 866 - bool run_insert_range(struct runs_tree *run, CLST vcn, CLST len); 818 + int run_insert_range(struct runs_tree *run, CLST vcn, CLST len); 819 + int run_insert_range_da(struct runs_tree *run, CLST vcn, CLST len); 867 820 bool run_get_entry(const struct runs_tree *run, size_t index, CLST *vcn, 868 821 CLST *lcn, CLST *len); 869 822 bool run_is_mapped_full(const struct runs_tree *run, CLST svcn, CLST evcn); ··· 884 835 #endif 885 836 int run_get_highest_vcn(CLST vcn, const u8 *run_buf, u64 *highest_vcn); 886 837 int run_clone(const struct runs_tree *run, struct runs_tree *new_run); 838 + bool run_remove_range(struct runs_tree *run, CLST vcn, CLST len, CLST *done); 839 + CLST run_len(const struct runs_tree *run); 840 + CLST run_get_max_vcn(const struct runs_tree *run); 887 841 888 842 /* Globals from super.c */ 889 843 void *ntfs_set_shared(void *ptr, u32 bytes); ··· 1063 1011 return test_bit(NTFS_FLAGS_SHUTDOWN_BIT, &ntfs_sb(sb)->flags); 1064 1012 } 1065 1013 1014 + /* Returns total sum of delay allocated clusters in all files. */ 1015 + static inline CLST ntfs_get_da(struct ntfs_sb_info *sbi) 1016 + { 1017 + #ifdef CONFIG_NTFS3_64BIT_CLUSTER 1018 + return atomic64_read(&sbi->used.da); 1019 + #else 1020 + return atomic_read(&sbi->used.da); 1021 + #endif 1022 + } 1023 + 1024 + /* Update total count of delay allocated clusters. */ 1025 + static inline void ntfs_add_da(struct ntfs_sb_info *sbi, CLST da) 1026 + { 1027 + #ifdef CONFIG_NTFS3_64BIT_CLUSTER 1028 + atomic64_add(da, &sbi->used.da); 1029 + #else 1030 + atomic_add(da, &sbi->used.da); 1031 + #endif 1032 + } 1033 + 1034 + /* Update total count of delay allocated clusters. */ 1035 + static inline void ntfs_sub_da(struct ntfs_sb_info *sbi, CLST da) 1036 + { 1037 + #ifdef CONFIG_NTFS3_64BIT_CLUSTER 1038 + atomic64_sub(da, &sbi->used.da); 1039 + #else 1040 + atomic_sub(da, &sbi->used.da); 1041 + #endif 1042 + } 1043 + 1066 1044 /* 1067 1045 * ntfs_up_cluster - Align up on cluster boundary. 1068 1046 */ ··· 1164 1082 static inline int is_resident(struct ntfs_inode *ni) 1165 1083 { 1166 1084 return ni->ni_flags & NI_FLAG_RESIDENT; 1085 + } 1086 + 1087 + static inline loff_t ntfs_get_maxbytes(struct ntfs_inode *ni) 1088 + { 1089 + struct ntfs_sb_info *sbi = ni->mi.sbi; 1090 + return is_sparsed(ni) || is_compressed(ni) ? sbi->maxbytes_sparse : 1091 + sbi->maxbytes; 1167 1092 } 1168 1093 1169 1094 static inline void le16_sub_cpu(__le16 *var, u16 val)
+149 -14
fs/ntfs3/run.c
··· 454 454 455 455 /* 456 456 * If existing range fits then were done. 457 - * Otherwise extend found one and fall back to range jocode. 457 + * Otherwise extend found one and fall back to range join code. 458 458 */ 459 459 if (r->vcn + r->len < vcn + len) 460 460 r->len += len - ((r->vcn + r->len) - vcn); ··· 482 482 return true; 483 483 } 484 484 485 - /* run_collapse_range 485 + /* 486 + * run_collapse_range 486 487 * 487 488 * Helper for attr_collapse_range(), 488 489 * which is helper for fallocate(collapse_range). ··· 494 493 struct ntfs_run *r, *e, *eat_start, *eat_end; 495 494 CLST end; 496 495 497 - if (WARN_ON(!run_lookup(run, vcn, &index))) 498 - return true; /* Should never be here. */ 496 + if (!run_lookup(run, vcn, &index) && index >= run->count) { 497 + return true; 498 + } 499 499 500 500 e = run->runs + run->count; 501 501 r = run->runs + index; ··· 562 560 * Helper for attr_insert_range(), 563 561 * which is helper for fallocate(insert_range). 564 562 */ 565 - bool run_insert_range(struct runs_tree *run, CLST vcn, CLST len) 563 + int run_insert_range(struct runs_tree *run, CLST vcn, CLST len) 566 564 { 567 565 size_t index; 568 566 struct ntfs_run *r, *e; 569 567 570 568 if (WARN_ON(!run_lookup(run, vcn, &index))) 571 - return false; /* Should never be here. */ 569 + return -EINVAL; /* Should never be here. */ 572 570 573 571 e = run->runs + run->count; 574 572 r = run->runs + index; ··· 590 588 r->len = len1; 591 589 592 590 if (!run_add_entry(run, vcn + len, lcn2, len2, false)) 593 - return false; 591 + return -ENOMEM; 594 592 } 595 593 596 594 if (!run_add_entry(run, vcn, SPARSE_LCN, len, false)) 597 - return false; 595 + return -ENOMEM; 598 596 599 - return true; 597 + return 0; 598 + } 599 + 600 + /* run_insert_range_da 601 + * 602 + * Helper for attr_insert_range(), 603 + * which is helper for fallocate(insert_range). 604 + */ 605 + int run_insert_range_da(struct runs_tree *run, CLST vcn, CLST len) 606 + { 607 + struct ntfs_run *r, *r0 = NULL, *e = run->runs + run->count; 608 + ; 609 + 610 + for (r = run->runs; r < e; r++) { 611 + CLST end = r->vcn + r->len; 612 + 613 + if (vcn >= end) 614 + continue; 615 + 616 + if (!r0 && r->vcn < vcn) { 617 + r0 = r; 618 + } else { 619 + r->vcn += len; 620 + } 621 + } 622 + 623 + if (r0) { 624 + /* split fragment. */ 625 + CLST len1 = vcn - r0->vcn; 626 + CLST len2 = r0->len - len1; 627 + 628 + r0->len = len1; 629 + if (!run_add_entry(run, vcn + len, SPARSE_LCN, len2, false)) 630 + return -ENOMEM; 631 + } 632 + 633 + return 0; 600 634 } 601 635 602 636 /* ··· 1169 1131 struct rw_semaphore *lock = 1170 1132 is_mounted(sbi) ? &sbi->mft.ni->file.run_lock : 1171 1133 NULL; 1172 - if (lock) 1173 - down_read(lock); 1174 - ntfs_refresh_zone(sbi); 1175 - if (lock) 1176 - up_read(lock); 1134 + if (lock) { 1135 + if (down_read_trylock(lock)) { 1136 + ntfs_refresh_zone(sbi); 1137 + up_read(lock); 1138 + } 1139 + } else { 1140 + ntfs_refresh_zone(sbi); 1141 + } 1177 1142 } 1178 1143 up_write(&wnd->rw_lock); 1179 1144 if (err) ··· 1246 1205 memcpy(new_run->runs, run->runs, bytes); 1247 1206 new_run->count = run->count; 1248 1207 return 0; 1208 + } 1209 + 1210 + /* 1211 + * run_remove_range 1212 + * 1213 + */ 1214 + bool run_remove_range(struct runs_tree *run, CLST vcn, CLST len, CLST *done) 1215 + { 1216 + size_t index, eat; 1217 + struct ntfs_run *r, *e, *eat_start, *eat_end; 1218 + CLST end, d; 1219 + 1220 + *done = 0; 1221 + 1222 + /* Fast check. */ 1223 + if (!run->count) 1224 + return true; 1225 + 1226 + if (!run_lookup(run, vcn, &index) && index >= run->count) { 1227 + /* No entries in this run. */ 1228 + return true; 1229 + } 1230 + 1231 + 1232 + e = run->runs + run->count; 1233 + r = run->runs + index; 1234 + end = vcn + len; 1235 + 1236 + if (vcn > r->vcn) { 1237 + CLST r_end = r->vcn + r->len; 1238 + d = vcn - r->vcn; 1239 + 1240 + if (r_end > end) { 1241 + /* Remove a middle part, split. */ 1242 + *done += len; 1243 + r->len = d; 1244 + return run_add_entry(run, end, r->lcn, r_end - end, 1245 + false); 1246 + } 1247 + /* Remove tail of run .*/ 1248 + *done += r->len - d; 1249 + r->len = d; 1250 + r += 1; 1251 + } 1252 + 1253 + eat_start = r; 1254 + eat_end = r; 1255 + 1256 + for (; r < e; r++) { 1257 + if (r->vcn >= end) 1258 + continue; 1259 + 1260 + if (r->vcn + r->len <= end) { 1261 + /* Eat this run. */ 1262 + *done += r->len; 1263 + eat_end = r + 1; 1264 + continue; 1265 + } 1266 + 1267 + d = end - r->vcn; 1268 + *done += d; 1269 + if (r->lcn != SPARSE_LCN) 1270 + r->lcn += d; 1271 + r->len -= d; 1272 + r->vcn = end; 1273 + } 1274 + 1275 + eat = eat_end - eat_start; 1276 + memmove(eat_start, eat_end, (e - eat_end) * sizeof(*r)); 1277 + run->count -= eat; 1278 + 1279 + return true; 1280 + } 1281 + 1282 + CLST run_len(const struct runs_tree *run) 1283 + { 1284 + const struct ntfs_run *r, *e; 1285 + CLST len = 0; 1286 + 1287 + for (r = run->runs, e = r + run->count; r < e; r++) { 1288 + len += r->len; 1289 + } 1290 + 1291 + return len; 1292 + } 1293 + 1294 + CLST run_get_max_vcn(const struct runs_tree *run) 1295 + { 1296 + const struct ntfs_run *r; 1297 + if (!run->count) 1298 + return 0; 1299 + 1300 + r = run->runs + run->count - 1; 1301 + return r->vcn + r->len; 1249 1302 }
+50 -23
fs/ntfs3/super.c
··· 58 58 #include <linux/buffer_head.h> 59 59 #include <linux/exportfs.h> 60 60 #include <linux/fs.h> 61 - #include <linux/fs_struct.h> 62 61 #include <linux/fs_context.h> 63 62 #include <linux/fs_parser.h> 63 + #include <linux/fs_struct.h> 64 64 #include <linux/log2.h> 65 65 #include <linux/minmax.h> 66 66 #include <linux/module.h> ··· 264 264 Opt_windows_names, 265 265 Opt_showmeta, 266 266 Opt_acl, 267 + Opt_acl_bool, 267 268 Opt_iocharset, 268 269 Opt_prealloc, 270 + Opt_prealloc_bool, 269 271 Opt_nocase, 272 + Opt_delalloc, 273 + Opt_delalloc_bool, 270 274 Opt_err, 271 275 }; 272 276 ··· 289 285 fsparam_flag("hide_dot_files", Opt_hide_dot_files), 290 286 fsparam_flag("windows_names", Opt_windows_names), 291 287 fsparam_flag("showmeta", Opt_showmeta), 292 - fsparam_flag_no("acl", Opt_acl), 288 + fsparam_flag("acl", Opt_acl), 289 + fsparam_bool("acl", Opt_acl_bool), 293 290 fsparam_string("iocharset", Opt_iocharset), 294 - fsparam_flag_no("prealloc", Opt_prealloc), 291 + fsparam_flag("prealloc", Opt_prealloc), 292 + fsparam_bool("prealloc", Opt_prealloc_bool), 295 293 fsparam_flag("nocase", Opt_nocase), 294 + fsparam_flag("delalloc", Opt_delalloc), 295 + fsparam_bool("delalloc", Opt_delalloc_bool), 296 296 {} 297 297 }; 298 298 // clang-format on ··· 387 379 case Opt_showmeta: 388 380 opts->showmeta = 1; 389 381 break; 390 - case Opt_acl: 391 - if (!result.negated) 382 + case Opt_acl_bool: 383 + if (result.boolean) { 384 + fallthrough; 385 + case Opt_acl: 392 386 #ifdef CONFIG_NTFS3_FS_POSIX_ACL 393 387 fc->sb_flags |= SB_POSIXACL; 394 388 #else 395 389 return invalf( 396 390 fc, "ntfs3: Support for ACL not compiled in!"); 397 391 #endif 398 - else 392 + } else 399 393 fc->sb_flags &= ~SB_POSIXACL; 400 394 break; 401 395 case Opt_iocharset: ··· 406 396 param->string = NULL; 407 397 break; 408 398 case Opt_prealloc: 409 - opts->prealloc = !result.negated; 399 + opts->prealloc = 1; 400 + break; 401 + case Opt_prealloc_bool: 402 + opts->prealloc = result.boolean; 410 403 break; 411 404 case Opt_nocase: 412 405 opts->nocase = 1; 406 + break; 407 + case Opt_delalloc: 408 + opts->delalloc = 1; 409 + break; 410 + case Opt_delalloc_bool: 411 + opts->delalloc = result.boolean; 413 412 break; 414 413 default: 415 414 /* Should not be here unless we forget add case. */ ··· 693 674 sbi->volume.ni = NULL; 694 675 } 695 676 696 - ntfs_update_mftmirr(sbi, 0); 677 + ntfs_update_mftmirr(sbi); 697 678 698 679 indx_clear(&sbi->security.index_sii); 699 680 indx_clear(&sbi->security.index_sdh); ··· 724 705 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); 725 706 726 707 if (sbi->options) { 727 - unload_nls(sbi->options->nls); 728 - kfree(sbi->options->nls_name); 729 - kfree(sbi->options); 708 + put_mount_options(sbi->options); 730 709 sbi->options = NULL; 731 710 } 732 711 ··· 736 719 struct super_block *sb = dentry->d_sb; 737 720 struct ntfs_sb_info *sbi = sb->s_fs_info; 738 721 struct wnd_bitmap *wnd = &sbi->used.bitmap; 722 + CLST da_clusters = ntfs_get_da(sbi); 739 723 740 724 buf->f_type = sb->s_magic; 741 - buf->f_bsize = sbi->cluster_size; 725 + buf->f_bsize = buf->f_frsize = sbi->cluster_size; 742 726 buf->f_blocks = wnd->nbits; 743 727 744 - buf->f_bfree = buf->f_bavail = wnd_zeroes(wnd); 728 + buf->f_bfree = wnd_zeroes(wnd); 729 + if (buf->f_bfree > da_clusters) { 730 + buf->f_bfree -= da_clusters; 731 + } else { 732 + buf->f_bfree = 0; 733 + } 734 + buf->f_bavail = buf->f_bfree; 735 + 745 736 buf->f_fsid.val[0] = sbi->volume.ser_num; 746 - buf->f_fsid.val[1] = (sbi->volume.ser_num >> 32); 737 + buf->f_fsid.val[1] = sbi->volume.ser_num >> 32; 747 738 buf->f_namelen = NTFS_NAME_LEN; 748 739 749 740 return 0; ··· 796 771 seq_puts(m, ",prealloc"); 797 772 if (opts->nocase) 798 773 seq_puts(m, ",nocase"); 774 + if (opts->delalloc) 775 + seq_puts(m, ",delalloc"); 799 776 800 777 return 0; 801 778 } ··· 850 823 if (!err) 851 824 ntfs_set_state(sbi, NTFS_DIRTY_CLEAR); 852 825 853 - ntfs_update_mftmirr(sbi, wait); 826 + ntfs_update_mftmirr(sbi); 827 + 828 + if (wait) { 829 + sync_blockdev(sb->s_bdev); 830 + blkdev_issue_flush(sb->s_bdev); 831 + } 854 832 855 833 return err; 856 834 } ··· 1108 1076 dev_size += sector_size - 1; 1109 1077 } 1110 1078 1111 - sbi->bdev_blocksize_mask = max(boot_sector_size, sector_size) - 1; 1079 + sbi->bdev_blocksize = max(boot_sector_size, sector_size); 1112 1080 sbi->mft.lbo = mlcn << cluster_bits; 1113 1081 sbi->mft.lbo2 = mlcn2 << cluster_bits; 1114 1082 ··· 1285 1253 } 1286 1254 } 1287 1255 sbi->options = options; 1288 - fc->fs_private = NULL; 1289 1256 sb->s_flags |= SB_NODIRATIME; 1290 1257 sb->s_magic = 0x7366746e; // "ntfs" 1291 1258 sb->s_op = &ntfs_sops; ··· 1683 1652 */ 1684 1653 struct buffer_head *bh0 = sb_getblk(sb, 0); 1685 1654 if (bh0) { 1686 - if (buffer_locked(bh0)) 1687 - __wait_on_buffer(bh0); 1688 - 1655 + wait_on_buffer(bh0); 1689 1656 lock_buffer(bh0); 1690 1657 memcpy(bh0->b_data, boot2, sizeof(*boot2)); 1691 1658 set_buffer_uptodate(bh0); ··· 1708 1679 out: 1709 1680 /* sbi->options == options */ 1710 1681 if (options) { 1711 - unload_nls(options->nls); 1712 - kfree(options->nls_name); 1713 - kfree(options); 1682 + put_mount_options(sbi->options); 1714 1683 sbi->options = NULL; 1715 1684 } 1716 1685
+1 -1
fs/ntfs3/xattr.c
··· 460 460 461 461 new_sz = size; 462 462 err = attr_set_size(ni, ATTR_EA, NULL, 0, &ea_run, new_sz, &new_sz, 463 - false, NULL); 463 + false); 464 464 if (err) 465 465 goto out; 466 466