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