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