1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * iomap callack functions 4 * 5 * Copyright (c) 2025 LG Electronics Co., Ltd. 6 */ 7 8 #include <linux/writeback.h> 9 10 #include "attrib.h" 11 #include "mft.h" 12 #include "ntfs.h" 13 #include "iomap.h" 14 15 static void ntfs_iomap_put_folio_non_resident(struct inode *inode, loff_t pos, 16 unsigned int len, struct folio *folio) 17 { 18 struct ntfs_inode *ni = NTFS_I(inode); 19 unsigned long sector_size = 1UL << inode->i_blkbits; 20 loff_t start_down, end_up, init; 21 22 start_down = round_down(pos, sector_size); 23 end_up = (pos + len - 1) | (sector_size - 1); 24 init = ni->initialized_size; 25 26 if (init >= start_down && init <= end_up) { 27 if (init < pos) { 28 loff_t offset = offset_in_folio(folio, pos + len); 29 30 if (offset == 0) 31 offset = folio_size(folio); 32 folio_zero_segments(folio, 33 offset_in_folio(folio, init), 34 offset_in_folio(folio, pos), 35 offset, 36 folio_size(folio)); 37 38 } else { 39 loff_t offset = max_t(loff_t, pos + len, init); 40 41 offset = offset_in_folio(folio, offset); 42 if (offset == 0) 43 offset = folio_size(folio); 44 folio_zero_segment(folio, 45 offset, 46 folio_size(folio)); 47 } 48 } else if (init <= pos) { 49 loff_t offset = 0, offset2 = offset_in_folio(folio, pos + len); 50 51 if ((init >> folio_shift(folio)) == (pos >> folio_shift(folio))) 52 offset = offset_in_folio(folio, init); 53 if (offset2 == 0) 54 offset2 = folio_size(folio); 55 folio_zero_segments(folio, 56 offset, 57 offset_in_folio(folio, pos), 58 offset2, 59 folio_size(folio)); 60 } 61 folio_unlock(folio); 62 folio_put(folio); 63 } 64 65 /* 66 * iomap_zero_range is called for an area beyond the initialized size, 67 * garbage values can be read, so zeroing out is needed. 68 */ 69 static void ntfs_iomap_put_folio(struct inode *inode, loff_t pos, 70 unsigned int len, struct folio *folio) 71 { 72 if (NInoNonResident(NTFS_I(inode))) 73 return ntfs_iomap_put_folio_non_resident(inode, pos, 74 len, folio); 75 folio_unlock(folio); 76 folio_put(folio); 77 } 78 79 const struct iomap_write_ops ntfs_iomap_folio_ops = { 80 .put_folio = ntfs_iomap_put_folio, 81 }; 82 83 static int ntfs_read_iomap_begin_resident(struct inode *inode, loff_t offset, loff_t length, 84 unsigned int flags, struct iomap *iomap, bool keep_mrec_lock) 85 { 86 struct ntfs_inode *base_ni, *ni = NTFS_I(inode); 87 struct ntfs_attr_search_ctx *ctx; 88 loff_t i_size; 89 u32 attr_len; 90 int err = 0; 91 char *kattr; 92 93 base_ni = ntfs_base_inode(ni); 94 mutex_lock(&base_ni->mrec_lock); 95 96 ctx = ntfs_attr_get_search_ctx(base_ni, NULL); 97 if (!ctx) { 98 err = -ENOMEM; 99 goto out; 100 } 101 102 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 103 CASE_SENSITIVE, 0, NULL, 0, ctx); 104 if (unlikely(err)) 105 goto out; 106 107 attr_len = le32_to_cpu(ctx->attr->data.resident.value_length); 108 if (unlikely(attr_len > ni->initialized_size)) 109 attr_len = ni->initialized_size; 110 i_size = i_size_read(inode); 111 112 if (unlikely(attr_len > i_size)) { 113 /* Race with shrinking truncate. */ 114 attr_len = i_size; 115 } 116 117 if (offset >= attr_len) { 118 if (flags & IOMAP_REPORT) 119 err = -ENOENT; 120 else { 121 iomap->type = IOMAP_HOLE; 122 iomap->offset = offset; 123 iomap->length = length; 124 } 125 goto out; 126 } 127 128 kattr = (u8 *)ctx->attr + le16_to_cpu(ctx->attr->data.resident.value_offset); 129 130 iomap->type = IOMAP_INLINE; 131 iomap->inline_data = kattr; 132 iomap->offset = 0; 133 iomap->length = attr_len; 134 135 out: 136 if (ctx) 137 ntfs_attr_put_search_ctx(ctx); 138 139 if (!err && keep_mrec_lock && 140 iomap->type == IOMAP_INLINE) { 141 iomap->private = base_ni; 142 return 0; 143 } 144 145 mutex_unlock(&base_ni->mrec_lock); 146 147 return err; 148 } 149 150 /* 151 * ntfs_read_iomap_begin_non_resident - map non-resident NTFS file data 152 * @inode: inode to map 153 * @offset: file offset to map 154 * @length: length of mapping 155 * @flags: IOMAP flags 156 * @iomap: iomap structure to fill 157 * @need_unwritten: true if UNWRITTEN extent type is needed 158 * 159 * Map a range of a non-resident NTFS file to an iomap extent. 160 * 161 * NTFS UNWRITTEN extent handling: 162 * ================================ 163 * The concept of an unwritten extent in NTFS is slightly different from 164 * that of other filesystems. NTFS conceptually manages only a single 165 * continuous unwritten region, which is strictly defined based on 166 * initialized_size. 167 * 168 * File offset layout: 169 * 0 initialized_size i_size(EOF) 170 * |----------#0----------|----------#1----------|----------#2----------| 171 * | Actual data | Pre-allocated | Pre-allocated | 172 * | (user written) | (within initialized) | (initialized ~ EOF) | 173 * |----------------------|----------------------|----------------------| 174 * MAPPED MAPPED UNWRITTEN (conditionally) 175 * 176 * Region #0: User-written data, initialized and valid. 177 * Region #1: Pre-allocated within initialized_size, must be zero-initialized 178 * by the filesystem before exposure to userspace. 179 * Region #2: Pre-allocated beyond initialized_size, does not need initialization. 180 * 181 * The @need_unwritten parameter controls whether region #2 is mapped as 182 * IOMAP_UNWRITTEN or IOMAP_MAPPED: 183 * - For seek operations (SEEK_DATA/SEEK_HOLE): IOMAP_MAPPED is needed to 184 * prevent iomap_seek_data from incorrectly interpreting pre-allocated 185 * space as a hole. Since NTFS does not support multiple unwritten extents, 186 * all pre-allocated regions should be treated as data, not holes. 187 * - For zero_range operations: IOMAP_MAPPED is needed to be zeroed out. 188 * 189 * Return: 0 on success, negative error code on failure. 190 */ 191 static int ntfs_read_iomap_begin_non_resident(struct inode *inode, loff_t offset, 192 loff_t length, unsigned int flags, struct iomap *iomap, 193 bool need_unwritten) 194 { 195 struct ntfs_inode *ni = NTFS_I(inode); 196 s64 vcn; 197 s64 lcn; 198 struct runlist_element *rl; 199 struct ntfs_volume *vol = ni->vol; 200 loff_t vcn_ofs; 201 loff_t rl_length; 202 203 vcn = ntfs_bytes_to_cluster(vol, offset); 204 vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset); 205 206 down_write(&ni->runlist.lock); 207 rl = ntfs_attr_vcn_to_rl(ni, vcn, &lcn); 208 if (IS_ERR(rl)) { 209 up_write(&ni->runlist.lock); 210 return PTR_ERR(rl); 211 } 212 213 if (flags & IOMAP_REPORT) { 214 if (lcn < LCN_HOLE) { 215 up_write(&ni->runlist.lock); 216 return -ENOENT; 217 } 218 } else if (lcn < LCN_ENOENT) { 219 up_write(&ni->runlist.lock); 220 return -EINVAL; 221 } 222 223 iomap->bdev = inode->i_sb->s_bdev; 224 iomap->offset = offset; 225 226 if (lcn <= LCN_DELALLOC) { 227 if (lcn == LCN_DELALLOC) 228 iomap->type = IOMAP_DELALLOC; 229 else 230 iomap->type = IOMAP_HOLE; 231 iomap->addr = IOMAP_NULL_ADDR; 232 } else { 233 if (need_unwritten && offset >= ni->initialized_size) 234 iomap->type = IOMAP_UNWRITTEN; 235 else 236 iomap->type = IOMAP_MAPPED; 237 iomap->addr = ntfs_cluster_to_bytes(vol, lcn) + vcn_ofs; 238 } 239 240 rl_length = ntfs_cluster_to_bytes(vol, rl->length - (vcn - rl->vcn)); 241 242 if (rl_length == 0 && rl->lcn > LCN_DELALLOC) { 243 ntfs_error(inode->i_sb, 244 "runlist(vcn : %lld, length : %lld, lcn : %lld) is corrupted\n", 245 rl->vcn, rl->length, rl->lcn); 246 up_write(&ni->runlist.lock); 247 return -EIO; 248 } 249 250 if (rl_length && length > rl_length - vcn_ofs) 251 iomap->length = rl_length - vcn_ofs; 252 else 253 iomap->length = length; 254 up_write(&ni->runlist.lock); 255 256 if (!(flags & IOMAP_ZERO) && 257 iomap->type == IOMAP_MAPPED && 258 iomap->offset < ni->initialized_size && 259 iomap->offset + iomap->length > ni->initialized_size) { 260 iomap->length = round_up(ni->initialized_size, 1 << inode->i_blkbits) - 261 iomap->offset; 262 } 263 iomap->flags |= IOMAP_F_MERGED; 264 265 return 0; 266 } 267 268 static int __ntfs_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 269 unsigned int flags, struct iomap *iomap, struct iomap *srcmap, 270 bool need_unwritten, bool keep_mrec_lock) 271 { 272 if (NInoNonResident(NTFS_I(inode))) 273 return ntfs_read_iomap_begin_non_resident(inode, offset, length, 274 flags, iomap, need_unwritten); 275 return ntfs_read_iomap_begin_resident(inode, offset, length, 276 flags, iomap, keep_mrec_lock); 277 } 278 279 static int ntfs_read_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 280 unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 281 { 282 return __ntfs_read_iomap_begin(inode, offset, length, flags, iomap, 283 srcmap, true, true); 284 } 285 286 static int ntfs_read_iomap_end(struct inode *inode, loff_t pos, loff_t length, 287 ssize_t written, unsigned int flags, struct iomap *iomap) 288 { 289 struct ntfs_inode *base_ni = iomap->private; 290 291 if (base_ni) 292 mutex_unlock(&base_ni->mrec_lock); 293 return written; 294 } 295 296 DEFINE_IOMAP_ITER_NEXT_END(ntfs_read_iomap_next, ntfs_read_iomap_begin, 297 ntfs_read_iomap_end); 298 299 /* 300 * Check that the cached iomap still matches the NTFS runlist before 301 * iomap_zero_range() is called. if the runlist changes while iomap is 302 * iterating a cached iomap, iomap_zero_range() may overwrite folios 303 * that have been already written with valid data. 304 */ 305 static bool ntfs_iomap_valid(struct inode *inode, const struct iomap *iomap) 306 { 307 struct ntfs_inode *ni = NTFS_I(inode); 308 struct runlist_element *rl; 309 s64 vcn, lcn; 310 311 if (!NInoNonResident(ni)) 312 return false; 313 314 vcn = iomap->offset >> ni->vol->cluster_size_bits; 315 316 down_read(&ni->runlist.lock); 317 rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); 318 if (IS_ERR(rl)) { 319 up_read(&ni->runlist.lock); 320 return false; 321 } 322 lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 323 up_read(&ni->runlist.lock); 324 return lcn == LCN_DELALLOC; 325 } 326 327 static const struct iomap_write_ops ntfs_zero_iomap_folio_ops = { 328 .put_folio = ntfs_iomap_put_folio, 329 .iomap_valid = ntfs_iomap_valid, 330 }; 331 332 static int ntfs_seek_iomap_begin(struct inode *inode, loff_t offset, loff_t length, 333 unsigned int flags, struct iomap *iomap, struct iomap *srcmap) 334 { 335 return __ntfs_read_iomap_begin(inode, offset, length, flags, iomap, 336 srcmap, false, false); 337 } 338 339 static int ntfs_zero_read_iomap_end(struct inode *inode, loff_t pos, loff_t length, 340 ssize_t written, unsigned int flags, struct iomap *iomap) 341 { 342 if ((flags & IOMAP_ZERO) && (iomap->flags & IOMAP_F_STALE)) 343 return -EPERM; 344 return written; 345 } 346 347 static DEFINE_IOMAP_ITER_NEXT_END(ntfs_zero_read_iomap_next, 348 ntfs_seek_iomap_begin, ntfs_zero_read_iomap_end); 349 350 DEFINE_IOMAP_ITER_NEXT(ntfs_seek_iomap_next, ntfs_seek_iomap_begin); 351 352 int ntfs_dio_zero_range(struct inode *inode, loff_t offset, loff_t length) 353 { 354 if ((offset | length) & (SECTOR_SIZE - 1)) 355 return -EINVAL; 356 357 return blkdev_issue_zeroout(inode->i_sb->s_bdev, 358 offset >> SECTOR_SHIFT, 359 length >> SECTOR_SHIFT, 360 GFP_NOFS, 361 BLKDEV_ZERO_NOUNMAP); 362 } 363 364 static int ntfs_zero_range(struct inode *inode, loff_t offset, loff_t length) 365 { 366 return iomap_zero_range(inode, 367 offset, length, 368 NULL, 369 ntfs_zero_read_iomap_next, 370 &ntfs_zero_iomap_folio_ops, 371 NULL); 372 } 373 374 static int ntfs_write_simple_iomap_begin_non_resident(struct inode *inode, loff_t offset, 375 loff_t length, struct iomap *iomap) 376 { 377 struct ntfs_inode *ni = NTFS_I(inode); 378 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 379 ntfs_base_inode(ni) : ni; 380 struct ntfs_volume *vol = ni->vol; 381 loff_t vcn_ofs, rl_length; 382 struct runlist_element *rl, *rlc; 383 bool is_retry = false; 384 int err = 0, map_err = 0; 385 s64 vcn, lcn; 386 s64 max_clu_count = 387 ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size)); 388 389 vcn = ntfs_bytes_to_cluster(vol, offset); 390 vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset); 391 392 down_read(&ni->runlist.lock); 393 rl = ni->runlist.rl; 394 if (!rl) { 395 up_read(&ni->runlist.lock); 396 err = ntfs_map_runlist(ni, vcn); 397 if (err) { 398 mutex_unlock(&mrec_ni->mrec_lock); 399 return -ENOENT; 400 } 401 down_read(&ni->runlist.lock); 402 rl = ni->runlist.rl; 403 } 404 up_read(&ni->runlist.lock); 405 406 down_write(&ni->runlist.lock); 407 remap_rl: 408 /* Seek to element containing target vcn. */ 409 rl = __ntfs_attr_find_vcn_nolock(&ni->runlist, vcn); 410 if (IS_ERR(rl)) { 411 up_write(&ni->runlist.lock); 412 mutex_unlock(&mrec_ni->mrec_lock); 413 return -EIO; 414 } 415 lcn = ntfs_rl_vcn_to_lcn(rl, vcn); 416 417 if (lcn <= LCN_RL_NOT_MAPPED && is_retry == false) { 418 is_retry = true; 419 map_err = ntfs_map_runlist_nolock(ni, vcn, NULL); 420 if (!map_err) { 421 rl = ni->runlist.rl; 422 goto remap_rl; 423 } 424 } 425 426 /* 427 * As in ntfs_attr_vcn_to_rl(): a runlist fragment that could not be 428 * mapped is not a hole. Treating it as one would put a delalloc 429 * extent over clusters that are allocated on disk but unknown to us. 430 */ 431 if (lcn == LCN_RL_NOT_MAPPED) { 432 up_write(&ni->runlist.lock); 433 mutex_unlock(&mrec_ni->mrec_lock); 434 return map_err == -ENOMEM ? -ENOMEM : -EIO; 435 } 436 437 max_clu_count = min(max_clu_count, rl->length - (vcn - rl->vcn)); 438 if (max_clu_count == 0) { 439 ntfs_error(inode->i_sb, 440 "runlist(vcn : %lld, length : %lld) is corrupted\n", 441 rl->vcn, rl->length); 442 up_write(&ni->runlist.lock); 443 mutex_unlock(&mrec_ni->mrec_lock); 444 return -EIO; 445 } 446 447 iomap->bdev = inode->i_sb->s_bdev; 448 iomap->offset = offset; 449 450 if (lcn <= LCN_DELALLOC) { 451 if (lcn < LCN_DELALLOC) { 452 max_clu_count = 453 ntfs_available_clusters_count(vol, max_clu_count); 454 if (max_clu_count < 0) { 455 err = max_clu_count; 456 up_write(&ni->runlist.lock); 457 mutex_unlock(&mrec_ni->mrec_lock); 458 return err; 459 } 460 } 461 462 iomap->type = IOMAP_DELALLOC; 463 iomap->addr = IOMAP_NULL_ADDR; 464 465 if (lcn <= LCN_HOLE) { 466 size_t new_rl_count; 467 468 rlc = kmalloc(sizeof(struct runlist_element) * 2, 469 GFP_NOFS); 470 if (!rlc) { 471 up_write(&ni->runlist.lock); 472 mutex_unlock(&mrec_ni->mrec_lock); 473 return -ENOMEM; 474 } 475 476 rlc->vcn = vcn; 477 rlc->lcn = LCN_DELALLOC; 478 rlc->length = max_clu_count; 479 480 rlc[1].vcn = vcn + max_clu_count; 481 rlc[1].lcn = LCN_RL_NOT_MAPPED; 482 rlc[1].length = 0; 483 484 rl = ntfs_runlists_merge(&ni->runlist, rlc, 0, 485 &new_rl_count); 486 if (IS_ERR(rl)) { 487 ntfs_error(vol->sb, "Failed to merge runlists"); 488 up_write(&ni->runlist.lock); 489 mutex_unlock(&mrec_ni->mrec_lock); 490 kvfree(rlc); 491 return PTR_ERR(rl); 492 } 493 494 ni->runlist.rl = rl; 495 ni->runlist.count = new_rl_count; 496 ni->i_dealloc_clusters += max_clu_count; 497 } 498 up_write(&ni->runlist.lock); 499 mutex_unlock(&mrec_ni->mrec_lock); 500 501 if (lcn < LCN_DELALLOC) 502 ntfs_hold_dirty_clusters(vol, max_clu_count); 503 504 rl_length = ntfs_cluster_to_bytes(vol, max_clu_count); 505 if (length > rl_length - vcn_ofs) 506 iomap->length = rl_length - vcn_ofs; 507 else 508 iomap->length = length; 509 510 iomap->flags = IOMAP_F_NEW; 511 if (lcn <= LCN_HOLE) { 512 loff_t end = offset + length; 513 514 if (vcn_ofs || ((vol->cluster_size > iomap->length) && 515 end < ni->initialized_size)) { 516 loff_t z_start, z_end; 517 518 z_start = vcn << vol->cluster_size_bits; 519 z_end = min_t(loff_t, z_start + vol->cluster_size, 520 i_size_read(inode)); 521 if (z_end > z_start) 522 err = ntfs_zero_range(inode, 523 z_start, 524 z_end - z_start); 525 } 526 if ((!err || err == -EPERM) && 527 max_clu_count > 1 && 528 (iomap->length & vol->cluster_size_mask && 529 end < ni->initialized_size)) { 530 loff_t z_start, z_end; 531 532 z_start = (vcn + max_clu_count - 1) << 533 vol->cluster_size_bits; 534 z_end = min_t(loff_t, z_start + vol->cluster_size, 535 i_size_read(inode)); 536 if (z_end > z_start) 537 err = ntfs_zero_range(inode, 538 z_start, 539 z_end - z_start); 540 } 541 542 if (err == -EPERM) 543 err = 0; 544 if (err) { 545 ntfs_release_dirty_clusters(vol, max_clu_count); 546 return err; 547 } 548 } 549 } else { 550 up_write(&ni->runlist.lock); 551 mutex_unlock(&mrec_ni->mrec_lock); 552 553 iomap->type = IOMAP_MAPPED; 554 iomap->addr = ntfs_cluster_to_bytes(vol, lcn) + vcn_ofs; 555 556 rl_length = ntfs_cluster_to_bytes(vol, max_clu_count); 557 if (length > rl_length - vcn_ofs) 558 iomap->length = rl_length - vcn_ofs; 559 else 560 iomap->length = length; 561 } 562 563 return 0; 564 } 565 566 #define NTFS_IOMAP_FLAGS_BEGIN BIT(1) 567 #define NTFS_IOMAP_FLAGS_DIO BIT(2) 568 #define NTFS_IOMAP_FLAGS_MKWRITE BIT(3) 569 #define NTFS_IOMAP_FLAGS_WRITEBACK BIT(4) 570 571 static int ntfs_write_da_iomap_begin_non_resident(struct inode *inode, 572 loff_t offset, loff_t length, unsigned int flags, 573 struct iomap *iomap, int ntfs_iomap_flags) 574 { 575 struct ntfs_inode *ni = NTFS_I(inode); 576 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 577 ntfs_base_inode(ni) : ni; 578 struct ntfs_volume *vol = ni->vol; 579 loff_t vcn_ofs, rl_length; 580 s64 vcn, start_lcn, lcn_count; 581 bool balloc = false, update_mp; 582 int err; 583 s64 max_clu_count = 584 ntfs_bytes_to_cluster(vol, round_up(length, vol->cluster_size)); 585 586 vcn = ntfs_bytes_to_cluster(vol, offset); 587 vcn_ofs = ntfs_bytes_to_cluster_off(vol, offset); 588 589 update_mp = ntfs_iomap_flags & (NTFS_IOMAP_FLAGS_DIO | NTFS_IOMAP_FLAGS_MKWRITE) || 590 NInoAttr(ni) || ni->mft_no < FILE_first_user; 591 down_write(&ni->runlist.lock); 592 err = ntfs_attr_map_cluster(ni, vcn, &start_lcn, &lcn_count, 593 max_clu_count, &balloc, update_mp, 594 ntfs_iomap_flags & NTFS_IOMAP_FLAGS_WRITEBACK); 595 up_write(&ni->runlist.lock); 596 mutex_unlock(&mrec_ni->mrec_lock); 597 if (err) { 598 ni->i_dealloc_clusters = 0; 599 return err; 600 } 601 602 iomap->bdev = inode->i_sb->s_bdev; 603 iomap->offset = offset; 604 605 rl_length = ntfs_cluster_to_bytes(vol, lcn_count); 606 if (length > rl_length - vcn_ofs) 607 iomap->length = rl_length - vcn_ofs; 608 else 609 iomap->length = length; 610 611 if (start_lcn == LCN_HOLE) 612 iomap->type = IOMAP_HOLE; 613 else 614 iomap->type = IOMAP_MAPPED; 615 if (balloc == true) 616 iomap->flags = IOMAP_F_NEW; 617 618 iomap->addr = ntfs_cluster_to_bytes(vol, start_lcn) + vcn_ofs; 619 620 if (balloc == true) { 621 if (flags & IOMAP_DIRECT || 622 ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE) { 623 loff_t end = offset + length; 624 625 if (vcn_ofs || ((vol->cluster_size > iomap->length) && 626 end < ni->initialized_size)) 627 err = ntfs_dio_zero_range(inode, 628 start_lcn << 629 vol->cluster_size_bits, 630 vol->cluster_size); 631 if (!err && lcn_count > 1 && 632 (iomap->length & vol->cluster_size_mask && 633 end < ni->initialized_size)) 634 err = ntfs_dio_zero_range(inode, 635 (start_lcn + lcn_count - 1) << 636 vol->cluster_size_bits, 637 vol->cluster_size); 638 } else { 639 if (lcn_count > ni->i_dealloc_clusters) 640 ni->i_dealloc_clusters = 0; 641 else 642 ni->i_dealloc_clusters -= lcn_count; 643 } 644 if (err < 0) 645 return err; 646 } 647 648 if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_MKWRITE && 649 iomap->offset + iomap->length > ni->initialized_size) { 650 err = ntfs_attr_set_initialized_size(ni, 651 iomap->offset + iomap->length); 652 } 653 654 return err; 655 } 656 657 static int ntfs_write_iomap_begin_resident(struct inode *inode, loff_t offset, 658 struct iomap *iomap) 659 { 660 struct ntfs_inode *ni = NTFS_I(inode); 661 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 662 ntfs_base_inode(ni) : ni; 663 struct attr_record *a; 664 struct ntfs_attr_search_ctx *ctx = NULL; 665 u32 attr_len; 666 int err = 0; 667 char *kattr; 668 669 ctx = ntfs_attr_get_search_ctx(mrec_ni, NULL); 670 if (!ctx) { 671 err = -ENOMEM; 672 goto out; 673 } 674 675 err = ntfs_attr_lookup(ni->type, ni->name, ni->name_len, 676 CASE_SENSITIVE, 0, NULL, 0, ctx); 677 if (err) { 678 if (err == -ENOENT) 679 err = -EIO; 680 goto out; 681 } 682 683 a = ctx->attr; 684 /* The total length of the attribute value. */ 685 attr_len = le32_to_cpu(a->data.resident.value_length); 686 kattr = (u8 *)a + le16_to_cpu(a->data.resident.value_offset); 687 688 iomap->type = IOMAP_INLINE; 689 iomap->inline_data = kattr; 690 iomap->offset = 0; 691 iomap->length = attr_len; 692 iomap->private = mrec_ni; 693 694 out: 695 if (ctx) 696 ntfs_attr_put_search_ctx(ctx); 697 698 if (err) 699 mutex_unlock(&mrec_ni->mrec_lock); 700 701 return err; 702 } 703 704 static int ntfs_write_iomap_begin_non_resident(struct inode *inode, loff_t offset, 705 loff_t length, unsigned int flags, 706 struct iomap *iomap, int ntfs_iomap_flags) 707 { 708 struct ntfs_inode *ni = NTFS_I(inode); 709 struct ntfs_inode *mrec_ni = ntfs_inode_is_named_stream(ni) ? 710 ntfs_base_inode(ni) : ni; 711 712 mutex_lock(&mrec_ni->mrec_lock); 713 714 if (ntfs_iomap_flags & NTFS_IOMAP_FLAGS_BEGIN) 715 return ntfs_write_simple_iomap_begin_non_resident(inode, offset, 716 length, iomap); 717 else 718 return ntfs_write_da_iomap_begin_non_resident(inode, 719 offset, length, 720 flags, iomap, 721 ntfs_iomap_flags); 722 } 723 724 static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset, 725 loff_t length, unsigned int flags, 726 struct iomap *iomap, int ntfs_iomap_flags) 727 { 728 struct ntfs_inode *ni = NTFS_I(inode); 729 struct ntfs_inode *mrec_ni; 730 731 if (NVolShutdown(ni->vol)) 732 return -EIO; 733 734 if (!NInoNonResident(ni)) { 735 mrec_ni = ntfs_inode_is_named_stream(ni) ? 736 ntfs_base_inode(ni) : ni; 737 mutex_lock(&mrec_ni->mrec_lock); 738 return ntfs_write_iomap_begin_resident(inode, offset, iomap); 739 } 740 return ntfs_write_iomap_begin_non_resident(inode, offset, length, flags, 741 iomap, ntfs_iomap_flags); 742 } 743 744 static int ntfs_write_iomap_begin(struct inode *inode, loff_t offset, 745 loff_t length, unsigned int flags, 746 struct iomap *iomap, struct iomap *srcmap) 747 { 748 return __ntfs_write_iomap_begin(inode, offset, length, flags, iomap, 749 NTFS_IOMAP_FLAGS_BEGIN); 750 } 751 752 static int ntfs_write_iomap_end_resident(struct inode *inode, loff_t pos, 753 loff_t length, ssize_t written, 754 unsigned int flags, struct iomap *iomap) 755 { 756 struct ntfs_inode *base_ni = iomap->private; 757 758 mark_mft_record_dirty(base_ni); 759 mutex_unlock(&base_ni->mrec_lock); 760 return written; 761 } 762 763 static int ntfs_write_iomap_end(struct inode *inode, loff_t pos, loff_t length, 764 ssize_t written, unsigned int flags, 765 struct iomap *iomap) 766 { 767 if (iomap->type == IOMAP_INLINE) 768 return ntfs_write_iomap_end_resident(inode, pos, length, 769 written, flags, iomap); 770 return written; 771 } 772 773 DEFINE_IOMAP_ITER_NEXT_END(ntfs_write_iomap_next, ntfs_write_iomap_begin, 774 ntfs_write_iomap_end); 775 776 static int ntfs_page_mkwrite_iomap_begin(struct inode *inode, loff_t offset, 777 loff_t length, unsigned int flags, 778 struct iomap *iomap, struct iomap *srcmap) 779 { 780 return __ntfs_write_iomap_begin(inode, offset, length, flags, iomap, 781 NTFS_IOMAP_FLAGS_MKWRITE); 782 } 783 784 DEFINE_IOMAP_ITER_NEXT_END(ntfs_page_mkwrite_iomap_next, 785 ntfs_page_mkwrite_iomap_begin, ntfs_write_iomap_end); 786 787 static int ntfs_dio_iomap_begin(struct inode *inode, loff_t offset, 788 loff_t length, unsigned int flags, 789 struct iomap *iomap, struct iomap *srcmap) 790 { 791 return __ntfs_write_iomap_begin(inode, offset, length, flags, iomap, 792 NTFS_IOMAP_FLAGS_DIO); 793 } 794 795 DEFINE_IOMAP_ITER_NEXT_END(ntfs_dio_iomap_next, ntfs_dio_iomap_begin, 796 ntfs_write_iomap_end); 797 798 static ssize_t ntfs_writeback_range(struct iomap_writepage_ctx *wpc, 799 struct folio *folio, u64 offset, unsigned int len, u64 end_pos) 800 { 801 if (offset < wpc->iomap.offset || 802 offset >= wpc->iomap.offset + wpc->iomap.length) { 803 int error; 804 805 error = __ntfs_write_iomap_begin(wpc->inode, offset, 806 NTFS_I(wpc->inode)->allocated_size - offset, 807 IOMAP_WRITE, &wpc->iomap, 808 NTFS_IOMAP_FLAGS_WRITEBACK); 809 if (error) 810 return error; 811 } 812 813 return iomap_add_to_ioend(wpc, folio, offset, end_pos, len); 814 } 815 816 const struct iomap_writeback_ops ntfs_writeback_ops = { 817 .writeback_range = ntfs_writeback_range, 818 .writeback_submit = iomap_ioend_writeback_submit, 819 }; 820