xref: /linux/fs/ntfs3/file.c (revision 056a5087d87ead77dedbe9cf5bde53b7cd4b4651)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *
4  * Copyright (C) 2019-2021 Paragon Software GmbH, All rights reserved.
5  *
6  *  Regular file handling primitives for NTFS-based filesystems.
7  *
8  */
9 
10 #include <linux/backing-dev.h>
11 #include <linux/blkdev.h>
12 #include <linux/buffer_head.h>
13 #include <linux/compat.h>
14 #include <linux/falloc.h>
15 #include <linux/fiemap.h>
16 #include <linux/fileattr.h>
17 #include <linux/filelock.h>
18 #include <linux/iomap.h>
19 
20 #include "debug.h"
21 #include "ntfs.h"
22 #include "ntfs_fs.h"
23 
24 /*
25  * cifx, btrfs, exfat, ext4, f2fs use this constant.
26  * Hope this value will become common to all fs.
27  */
28 #define NTFS3_IOC_SHUTDOWN _IOR('X', 125, __u32)
29 
30 /*
31  * Helper for ntfs_should_use_dio.
32  */
33 static u32 ntfs_dio_alignment(struct inode *inode)
34 {
35 	struct ntfs_inode *ni = ntfs_i(inode);
36 
37 	if (is_resident(ni)) {
38 		/* Check delalloc. */
39 		if (!ni->file.run_da.count)
40 			return 0;
41 	}
42 
43 	/* In most cases this is bdev_logical_block_size(bdev). */
44 	return ni->mi.sbi->bdev_blocksize;
45 }
46 
47 /*
48  * Returns %true if the given DIO request should be attempted with DIO, or
49  * %false if it should fall back to buffered I/O.
50  */
51 static bool ntfs_should_use_dio(struct kiocb *iocb, struct iov_iter *iter)
52 {
53 	struct inode *inode = file_inode(iocb->ki_filp);
54 	u32 dio_align = ntfs_dio_alignment(inode);
55 
56 	if (!dio_align)
57 		return false;
58 
59 	return IS_ALIGNED(iocb->ki_pos | iov_iter_alignment(iter), dio_align);
60 }
61 
62 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
63 {
64 	struct fstrim_range __user *user_range;
65 	struct fstrim_range range;
66 	struct block_device *dev;
67 	int err;
68 
69 	if (!capable(CAP_SYS_ADMIN))
70 		return -EPERM;
71 
72 	dev = sbi->sb->s_bdev;
73 	if (!bdev_max_discard_sectors(dev))
74 		return -EOPNOTSUPP;
75 
76 	user_range = (struct fstrim_range __user *)arg;
77 	if (copy_from_user(&range, user_range, sizeof(range)))
78 		return -EFAULT;
79 
80 	range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
81 
82 	err = ntfs_trim_fs(sbi, &range);
83 	if (err < 0)
84 		return err;
85 
86 	if (copy_to_user(user_range, &range, sizeof(range)))
87 		return -EFAULT;
88 
89 	return 0;
90 }
91 
92 static int ntfs_ioctl_get_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
93 {
94 	if (copy_to_user(buf, sbi->volume.label, FSLABEL_MAX))
95 		return -EFAULT;
96 
97 	return 0;
98 }
99 
100 static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
101 {
102 	u8 user[FSLABEL_MAX] = { 0 };
103 	int len;
104 
105 	if (!capable(CAP_SYS_ADMIN))
106 		return -EPERM;
107 
108 	if (copy_from_user(user, buf, FSLABEL_MAX))
109 		return -EFAULT;
110 
111 	len = strnlen(user, FSLABEL_MAX);
112 
113 	return ntfs_set_label(sbi, user, len);
114 }
115 
116 /*
117  * ntfs_force_shutdown - helper function. Called from ioctl
118  */
119 static int ntfs_force_shutdown(struct super_block *sb, u32 flags)
120 {
121 	int err;
122 	struct ntfs_sb_info *sbi = sb->s_fs_info;
123 
124 	if (unlikely(ntfs3_forced_shutdown(sb)))
125 		return 0;
126 
127 	/* No additional options yet (flags). */
128 	err = bdev_freeze(sb->s_bdev);
129 	if (err)
130 		return err;
131 	set_bit(NTFS_FLAGS_SHUTDOWN_BIT, &sbi->flags);
132 	bdev_thaw(sb->s_bdev);
133 	return 0;
134 }
135 
136 static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg)
137 {
138 	u32 flags;
139 
140 	if (!capable(CAP_SYS_ADMIN))
141 		return -EPERM;
142 
143 	if (get_user(flags, (__u32 __user *)arg))
144 		return -EFAULT;
145 
146 	return ntfs_force_shutdown(sb, flags);
147 }
148 
149 /*
150  * ntfs_ioctl - file_operations::unlocked_ioctl
151  */
152 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
153 {
154 	struct inode *inode = file_inode(filp);
155 	struct super_block *sb = inode->i_sb;
156 	struct ntfs_sb_info *sbi = sb->s_fs_info;
157 
158 	/* Avoid any operation if inode is bad. */
159 	if (unlikely(is_bad_ni(ntfs_i(inode))))
160 		return -EINVAL;
161 
162 	switch (cmd) {
163 	case FITRIM:
164 		return ntfs_ioctl_fitrim(sbi, arg);
165 	case FS_IOC_GETFSLABEL:
166 		return ntfs_ioctl_get_volume_label(sbi, (u8 __user *)arg);
167 	case FS_IOC_SETFSLABEL:
168 		return ntfs_ioctl_set_volume_label(sbi, (u8 __user *)arg);
169 	case NTFS3_IOC_SHUTDOWN:
170 		return ntfs_ioctl_shutdown(sb, arg);
171 	}
172 	return -ENOTTY; /* Inappropriate ioctl for device. */
173 }
174 
175 #ifdef CONFIG_COMPAT
176 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
177 
178 {
179 	return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
180 }
181 #endif
182 
183 /*
184  * ntfs_fileattr_get - inode_operations::fileattr_get
185  */
186 int ntfs_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
187 {
188 	struct inode *inode = d_inode(dentry);
189 	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
190 
191 	/* Avoid any operation if inode is bad. */
192 	if (unlikely(is_bad_ni(ntfs_i(inode))))
193 		return -EINVAL;
194 
195 	/*
196 	 * NTFS preserves case (the default). Case sensitivity depends on
197 	 * mount options: with "nocase", NTFS is case-insensitive;
198 	 * otherwise it is case-sensitive.
199 	 */
200 	if (sbi->options->nocase) {
201 		fa->fsx_xflags |= FS_XFLAG_CASEFOLD;
202 		fa->flags |= FS_CASEFOLD_FL;
203 	}
204 	if (inode->i_flags & S_IMMUTABLE) {
205 		fa->fsx_xflags |= FS_XFLAG_IMMUTABLE;
206 		fa->flags |= FS_IMMUTABLE_FL;
207 	}
208 	return 0;
209 }
210 
211 /*
212  * ntfs_getattr - inode_operations::getattr
213  */
214 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
215 		 struct kstat *stat, u32 request_mask, u32 flags)
216 {
217 	struct inode *inode = d_inode(path->dentry);
218 	struct ntfs_inode *ni = ntfs_i(inode);
219 
220 	/* Avoid any operation if inode is bad. */
221 	if (unlikely(is_bad_ni(ni)))
222 		return -EINVAL;
223 
224 	stat->result_mask |= STATX_BTIME;
225 	stat->btime = ni->i_crtime;
226 	stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
227 
228 	if (inode->i_flags & S_IMMUTABLE)
229 		stat->attributes |= STATX_ATTR_IMMUTABLE;
230 
231 	if (inode->i_flags & S_APPEND)
232 		stat->attributes |= STATX_ATTR_APPEND;
233 
234 	if (is_compressed(ni))
235 		stat->attributes |= STATX_ATTR_COMPRESSED;
236 
237 	if (is_encrypted(ni))
238 		stat->attributes |= STATX_ATTR_ENCRYPTED;
239 
240 	stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |
241 				 STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
242 
243 	generic_fillattr(idmap, request_mask, inode, stat);
244 
245 	return 0;
246 }
247 
248 static int ntfs_extend_initialized_size(struct file *file,
249 					struct ntfs_inode *ni,
250 					const loff_t new_valid)
251 {
252 	struct inode *inode = &ni->vfs_inode;
253 	const loff_t valid = ni->i_valid;
254 	int err;
255 
256 	if (valid >= new_valid)
257 		return 0;
258 
259 	if (is_resident(ni)) {
260 		ni->i_valid = new_valid;
261 		return 0;
262 	}
263 
264 	err = iomap_zero_range(inode, valid, new_valid - valid, NULL,
265 			       &ntfs_iomap_ops, &ntfs_iomap_folio_ops, NULL);
266 	if (err) {
267 		ni->i_valid = valid;
268 		ntfs_inode_warn(inode,
269 				"failed to extend initialized size to %llx.",
270 				new_valid);
271 		return err;
272 	}
273 
274 	return 0;
275 }
276 
277 static void ntfs_filemap_close(struct vm_area_struct *vma)
278 {
279 	struct inode *inode = file_inode(vma->vm_file);
280 	struct ntfs_inode *ni = ntfs_i(inode);
281 	u64 from = (u64)vma->vm_pgoff << PAGE_SHIFT;
282 	u64 to = min_t(u64, i_size_read(inode),
283 		       from + vma->vm_end - vma->vm_start);
284 
285 	if (ni->i_valid < to) {
286 		ni->i_valid = to;
287 		mark_inode_dirty(inode);
288 	}
289 }
290 
291 /* Copy of generic_file_vm_ops. */
292 static const struct vm_operations_struct ntfs_file_vm_ops = {
293 	.close = ntfs_filemap_close,
294 	.fault = filemap_fault,
295 	.map_pages = filemap_map_pages,
296 	.page_mkwrite = filemap_page_mkwrite,
297 };
298 
299 /*
300  * ntfs_file_mmap_prepare - file_operations::mmap_prepare
301  */
302 static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
303 {
304 	struct file *file = desc->file;
305 	struct inode *inode = file_inode(file);
306 	struct ntfs_inode *ni = ntfs_i(inode);
307 	const bool rw = vma_desc_test(desc, VMA_WRITE_BIT);
308 	int err;
309 
310 	/* Avoid any operation if inode is bad. */
311 	if (unlikely(is_bad_ni(ni)))
312 		return -EINVAL;
313 
314 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
315 		return -EIO;
316 
317 	if (is_encrypted(ni)) {
318 		ntfs_inode_warn(inode, "mmap encrypted not supported");
319 		return -EOPNOTSUPP;
320 	}
321 
322 	if (is_dedup(ni)) {
323 		ntfs_inode_warn(inode, "mmap deduplicated not supported");
324 		return -EOPNOTSUPP;
325 	}
326 
327 	if (is_compressed(ni)) {
328 		if (rw) {
329 			ntfs_inode_warn(inode,
330 					"mmap(write) compressed not supported");
331 			return -EOPNOTSUPP;
332 		}
333 		/* Turn off readahead for compressed files. */
334 		file->f_ra.ra_pages = 0;
335 	}
336 
337 	if (rw) {
338 		u64 from = (u64)desc->pgoff << PAGE_SHIFT;
339 		u64 to = min_t(u64, i_size_read(inode),
340 			       from + vma_desc_size(desc));
341 
342 		if (is_sparsed(ni)) {
343 			/* Allocate clusters for rw map. */
344 			struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
345 			CLST lcn, len;
346 			CLST vcn = from >> sbi->cluster_bits;
347 			CLST end = bytes_to_cluster(sbi, to);
348 			bool new;
349 
350 			for (; vcn < end; vcn += len) {
351 				err = attr_data_get_block(ni, vcn, 1, &lcn,
352 							  &len, &new, true,
353 							  NULL, false);
354 				if (err)
355 					goto out;
356 			}
357 		}
358 
359 		if (ni->i_valid < to) {
360 			if (!inode_trylock(inode)) {
361 				err = -EAGAIN;
362 				goto out;
363 			}
364 			err = ntfs_extend_initialized_size(file, ni, to);
365 			inode_unlock(inode);
366 			if (err)
367 				goto out;
368 		}
369 	}
370 
371 	err = generic_file_mmap_prepare(desc);
372 	if (!err && rw)
373 		desc->vm_ops = &ntfs_file_vm_ops;
374 out:
375 	return err;
376 }
377 
378 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
379 		       struct file *file)
380 {
381 	struct ntfs_inode *ni = ntfs_i(inode);
382 	struct address_space *mapping = inode->i_mapping;
383 	loff_t end = pos + count;
384 	bool extend_init = file && pos > ni->i_valid;
385 	int err;
386 
387 	if (end <= inode->i_size && !extend_init)
388 		return 0;
389 
390 	/* Mark rw ntfs as dirty. It will be cleared at umount. */
391 	ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
392 
393 	if (end > inode->i_size) {
394 		/*
395 		 * Normal files: increase file size, allocate space.
396 		 * Sparse/Compressed: increase file size. No space allocated.
397 		 */
398 		err = ntfs_set_size(inode, end);
399 		if (err)
400 			goto out;
401 	}
402 
403 	if (extend_init && !is_compressed(ni)) {
404 		err = ntfs_extend_initialized_size(file, ni, pos);
405 		if (err)
406 			goto out;
407 	} else {
408 		err = 0;
409 	}
410 
411 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
412 	mark_inode_dirty(inode);
413 
414 	if (IS_SYNC(inode)) {
415 		int err2;
416 
417 		err = filemap_fdatawrite_range(mapping, pos, end - 1);
418 		err2 = write_inode_now(inode, 1);
419 		if (!err)
420 			err = err2;
421 		if (!err)
422 			err = filemap_fdatawait_range(mapping, pos, end - 1);
423 	}
424 
425 out:
426 	return err;
427 }
428 
429 static int ntfs_truncate(struct inode *inode, loff_t new_size)
430 {
431 	int err;
432 	struct ntfs_inode *ni = ntfs_i(inode);
433 	u64 new_valid = min_t(u64, ni->i_valid, new_size);
434 
435 	truncate_setsize(inode, new_size);
436 
437 	ni_lock(ni);
438 
439 	down_write(&ni->file.run_lock);
440 	err = attr_set_size_ex(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
441 			       &new_valid, ni->mi.sbi->options->prealloc, NULL,
442 			       false);
443 	up_write(&ni->file.run_lock);
444 
445 	ni->i_valid = new_valid;
446 
447 	ni_unlock(ni);
448 
449 	if (err)
450 		return err;
451 
452 	ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
453 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
454 	if (!IS_DIRSYNC(inode)) {
455 		mark_inode_dirty(inode);
456 	} else {
457 		err = ntfs_sync_inode(inode);
458 		if (err)
459 			return err;
460 	}
461 
462 	return 0;
463 }
464 
465 /*
466  * ntfs_fallocate - file_operations::ntfs_fallocate
467  *
468  * Preallocate space for a file. This implements ntfs's fallocate file
469  * operation, which gets called from sys_fallocate system call. User
470  * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
471  * we just allocate clusters without zeroing them out. Otherwise we
472  * allocate and zero out clusters via an expanding truncate.
473  */
474 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
475 {
476 	struct inode *inode = file_inode(file);
477 	struct address_space *mapping = inode->i_mapping;
478 	struct super_block *sb = inode->i_sb;
479 	struct ntfs_sb_info *sbi = sb->s_fs_info;
480 	struct ntfs_inode *ni = ntfs_i(inode);
481 	loff_t end = vbo + len;
482 	loff_t vbo_down = round_down(vbo, max_t(unsigned long,
483 						sbi->cluster_size, PAGE_SIZE));
484 	bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
485 	loff_t i_size, new_size;
486 	bool map_locked;
487 	int err;
488 
489 	/* No support for dir. */
490 	if (!S_ISREG(inode->i_mode))
491 		return -EOPNOTSUPP;
492 
493 	/*
494 	 * vfs_fallocate checks all possible combinations of mode.
495 	 * Do additional checks here before ntfs_set_state(dirty).
496 	 */
497 	if (mode & FALLOC_FL_PUNCH_HOLE) {
498 		if (!is_supported_holes)
499 			return -EOPNOTSUPP;
500 	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
501 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
502 		if (!is_supported_holes)
503 			return -EOPNOTSUPP;
504 	} else if (mode &
505 		   ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
506 		     FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
507 		ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
508 				mode);
509 		return -EOPNOTSUPP;
510 	}
511 
512 	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
513 
514 	inode_lock(inode);
515 	i_size = inode->i_size;
516 	new_size = max(end, i_size);
517 	map_locked = false;
518 
519 	if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
520 		/* Should never be here, see ntfs_file_open. */
521 		err = -EOPNOTSUPP;
522 		goto out;
523 	}
524 
525 	if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
526 		    FALLOC_FL_INSERT_RANGE)) {
527 		inode_dio_wait(inode);
528 		filemap_invalidate_lock(mapping);
529 		map_locked = true;
530 	}
531 
532 	if (mode & FALLOC_FL_PUNCH_HOLE) {
533 		u32 frame_size;
534 		loff_t mask, vbo_a, end_a, tmp, from;
535 
536 		err = filemap_write_and_wait_range(mapping, vbo_down,
537 						   LLONG_MAX);
538 		if (err)
539 			goto out;
540 
541 		truncate_pagecache(inode, vbo_down);
542 
543 		ni_lock(ni);
544 		err = attr_punch_hole(ni, vbo, len, &frame_size);
545 		ni_unlock(ni);
546 		if (!err)
547 			goto ok;
548 
549 		if (err != E_NTFS_NOTALIGNED)
550 			goto out;
551 
552 		/* Process not aligned punch. */
553 		err = 0;
554 		if (end > i_size)
555 			end = i_size;
556 		mask = frame_size - 1;
557 		vbo_a = (vbo + mask) & ~mask;
558 		end_a = end & ~mask;
559 
560 		tmp = min(vbo_a, end);
561 		from = min_t(loff_t, ni->i_valid, vbo);
562 		/* Zero head of punch. */
563 		if (tmp > from) {
564 			err = iomap_zero_range(inode, from, tmp - from, NULL,
565 					       &ntfs_iomap_ops,
566 					       &ntfs_iomap_folio_ops, NULL);
567 			if (err)
568 				goto out;
569 		}
570 
571 		/* Aligned punch_hole. Deallocate clusters. */
572 		if (end_a > vbo_a) {
573 			ni_lock(ni);
574 			err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
575 			ni_unlock(ni);
576 			if (err)
577 				goto out;
578 		}
579 
580 		/* Zero tail of punch. */
581 		if (vbo < end_a && end_a < end) {
582 			err = iomap_zero_range(inode, end_a, end - end_a, NULL,
583 					       &ntfs_iomap_ops,
584 					       &ntfs_iomap_folio_ops, NULL);
585 			if (err)
586 				goto out;
587 		}
588 	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
589 		/*
590 		 * Write tail of the last page before removed range since
591 		 * it will get removed from the page cache below.
592 		 */
593 		err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
594 		if (err)
595 			goto out;
596 
597 		/*
598 		 * Write data that will be shifted to preserve them
599 		 * when discarding page cache below.
600 		 */
601 		err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
602 		if (err)
603 			goto out;
604 
605 		truncate_pagecache(inode, vbo_down);
606 
607 		ni_lock(ni);
608 		err = attr_collapse_range(ni, vbo, len);
609 		ni_unlock(ni);
610 		if (err)
611 			goto out;
612 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
613 		/* Check new size. */
614 		err = inode_newsize_ok(inode, new_size);
615 		if (err)
616 			goto out;
617 
618 		/* Write out all dirty pages. */
619 		err = filemap_write_and_wait_range(mapping, vbo_down,
620 						   LLONG_MAX);
621 		if (err)
622 			goto out;
623 		truncate_pagecache(inode, vbo_down);
624 
625 		ni_lock(ni);
626 		err = attr_insert_range(ni, vbo, len);
627 		ni_unlock(ni);
628 		if (err)
629 			goto out;
630 	} else {
631 		/* Check new size. */
632 		u8 cluster_bits = sbi->cluster_bits;
633 
634 		/* Be sure file is non resident. */
635 		if (is_resident(ni)) {
636 			ni_lock(ni);
637 			err = attr_force_nonresident(ni);
638 			ni_unlock(ni);
639 			if (err)
640 				goto out;
641 		}
642 
643 		/* generic/213: expected -ENOSPC instead of -EFBIG. */
644 		if (!is_supported_holes) {
645 			loff_t to_alloc = new_size - inode_get_bytes(inode);
646 
647 			if (to_alloc > 0 &&
648 			    (to_alloc >> cluster_bits) >
649 				    wnd_zeroes(&sbi->used.bitmap)) {
650 				err = -ENOSPC;
651 				goto out;
652 			}
653 		}
654 
655 		err = inode_newsize_ok(inode, new_size);
656 		if (err)
657 			goto out;
658 
659 		if (new_size > i_size) {
660 			/*
661 			 * Allocate clusters, do not change 'valid' size.
662 			 */
663 			err = ntfs_set_size(inode, new_size);
664 			if (err)
665 				goto out;
666 		}
667 
668 		if (is_supported_holes) {
669 			CLST vcn = vbo >> cluster_bits;
670 			CLST cend = bytes_to_cluster(sbi, end);
671 			CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
672 			CLST lcn, clen;
673 			bool new;
674 
675 			if (cend_v > cend)
676 				cend_v = cend;
677 
678 			/*
679 			 * Allocate and zero new clusters.
680 			 * Zeroing these clusters may be too long.
681 			 */
682 			for (; vcn < cend_v; vcn += clen) {
683 				err = attr_data_get_block(ni, vcn, cend_v - vcn,
684 							  &lcn, &clen, &new,
685 							  true, NULL, false);
686 				if (err)
687 					goto out;
688 			}
689 
690 			/*
691 			 * Moving up 'valid size'.
692 			 */
693 			err = ntfs_extend_initialized_size(
694 				file, ni, (u64)cend_v << cluster_bits);
695 			if (err)
696 				goto out;
697 
698 			/*
699 			 * Allocate but not zero new clusters.
700 			 */
701 			for (; vcn < cend; vcn += clen) {
702 				err = attr_data_get_block(ni, vcn, cend - vcn,
703 							  &lcn, &clen, &new,
704 							  false, NULL, false);
705 				if (err)
706 					goto out;
707 			}
708 		}
709 
710 		if (mode & FALLOC_FL_KEEP_SIZE) {
711 			ni_lock(ni);
712 			/* True - Keep preallocated. */
713 			err = attr_set_size(ni, ATTR_DATA, NULL, 0,
714 					    &ni->file.run, i_size, &ni->i_valid,
715 					    true);
716 			ni_unlock(ni);
717 			if (err)
718 				goto out;
719 			i_size_write(inode, i_size);
720 		} else if (new_size > i_size) {
721 			i_size_write(inode, new_size);
722 		}
723 	}
724 
725 ok:
726 	err = file_modified(file);
727 	if (err)
728 		goto out;
729 
730 out:
731 	if (map_locked)
732 		filemap_invalidate_unlock(mapping);
733 
734 	if (!err) {
735 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
736 		mark_inode_dirty(inode);
737 	}
738 
739 	inode_unlock(inode);
740 	return err;
741 }
742 
743 /*
744  * ntfs_setattr - inode_operations::setattr
745  */
746 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
747 		 struct iattr *attr)
748 {
749 	struct inode *inode = d_inode(dentry);
750 	struct ntfs_inode *ni = ntfs_i(inode);
751 	u32 ia_valid = attr->ia_valid;
752 	umode_t mode = inode->i_mode;
753 	int err;
754 
755 	/* Avoid any operation if inode is bad. */
756 	if (unlikely(is_bad_ni(ni)))
757 		return -EINVAL;
758 
759 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
760 		return -EIO;
761 
762 	err = setattr_prepare(idmap, dentry, attr);
763 	if (err)
764 		goto out;
765 
766 	if (ia_valid & ATTR_SIZE) {
767 		loff_t newsize, oldsize;
768 
769 		if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
770 			/* Should never be here, see ntfs_file_open(). */
771 			err = -EOPNOTSUPP;
772 			goto out;
773 		}
774 		inode_dio_wait(inode);
775 		oldsize = i_size_read(inode);
776 		newsize = attr->ia_size;
777 
778 		if (newsize <= oldsize)
779 			err = ntfs_truncate(inode, newsize);
780 		else
781 			err = ntfs_extend(inode, newsize, 0, NULL);
782 
783 		if (err)
784 			goto out;
785 
786 		ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
787 		i_size_write(inode, newsize);
788 	}
789 
790 	setattr_copy(idmap, inode, attr);
791 
792 	if (mode != inode->i_mode) {
793 		err = ntfs_acl_chmod(idmap, dentry);
794 		if (err)
795 			goto out;
796 
797 		/* Linux 'w' -> Windows 'ro'. */
798 		if (0222 & inode->i_mode)
799 			ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
800 		else
801 			ni->std_fa |= FILE_ATTRIBUTE_READONLY;
802 	}
803 
804 	if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
805 		ntfs_save_wsl_perm(inode, NULL);
806 	mark_inode_dirty(inode);
807 out:
808 	return err;
809 }
810 
811 /*
812  * check_read_restriction:
813  * common code for ntfs_file_read_iter and ntfs_file_splice_read
814  */
815 static int check_read_restriction(struct inode *inode)
816 {
817 	struct ntfs_inode *ni = ntfs_i(inode);
818 
819 	/* Avoid any operation if inode is bad. */
820 	if (unlikely(is_bad_ni(ni)))
821 		return -EINVAL;
822 
823 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
824 		return -EIO;
825 
826 	if (is_encrypted(ni)) {
827 		ntfs_inode_warn(inode, "encrypted i/o not supported");
828 		return -EOPNOTSUPP;
829 	}
830 
831 #ifndef CONFIG_NTFS3_LZX_XPRESS
832 	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
833 		ntfs_inode_warn(
834 			inode,
835 			"activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
836 		return -EOPNOTSUPP;
837 	}
838 #endif
839 
840 	if (is_dedup(ni)) {
841 		ntfs_inode_warn(inode, "read deduplicated not supported");
842 		return -EOPNOTSUPP;
843 	}
844 
845 	return 0;
846 }
847 
848 /*
849  * ntfs_file_read_iter - file_operations::read_iter
850  */
851 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
852 {
853 	struct file *file = iocb->ki_filp;
854 	struct inode *inode = file_inode(file);
855 	struct ntfs_inode *ni = ntfs_i(inode);
856 	size_t bytes = iov_iter_count(iter);
857 	loff_t valid, i_size, vbo, end;
858 	unsigned int dio_flags;
859 	ssize_t err;
860 
861 	err = check_read_restriction(inode);
862 	if (err)
863 		return err;
864 
865 	if (!bytes)
866 		return 0; /* skip atime */
867 
868 	if (is_compressed(ni)) {
869 		if (iocb->ki_flags & IOCB_DIRECT) {
870 			ntfs_inode_warn(
871 				inode, "direct i/o + compressed not supported");
872 			return -EOPNOTSUPP;
873 		}
874 		/* Turn off readahead for compressed files. */
875 		file->f_ra.ra_pages = 0;
876 	}
877 
878 	/* Fallback to buffered I/O if the inode does not support direct I/O. */
879 	if (!(iocb->ki_flags & IOCB_DIRECT) ||
880 	    !ntfs_should_use_dio(iocb, iter)) {
881 		iocb->ki_flags &= ~IOCB_DIRECT;
882 		return generic_file_read_iter(iocb, iter);
883 	}
884 
885 	if (iocb->ki_flags & IOCB_NOWAIT) {
886 		if (!inode_trylock_shared(inode))
887 			return -EAGAIN;
888 	} else {
889 		inode_lock_shared(inode);
890 	}
891 
892 	vbo = iocb->ki_pos;
893 	end = vbo + bytes;
894 	dio_flags = 0;
895 	valid = ni->i_valid;
896 	i_size = inode->i_size;
897 
898 	if (vbo < valid) {
899 		if (valid < end) {
900 			/* read cross 'valid' size. */
901 			dio_flags |= IOMAP_DIO_FORCE_WAIT;
902 		}
903 
904 		if (ni->file.run_da.count) {
905 			/* Direct I/O is not compatible with delalloc. */
906 			err = ni_allocate_da_blocks(ni);
907 			if (err)
908 				goto out;
909 		}
910 
911 		err = iomap_dio_rw(iocb, iter, &ntfs_iomap_ops, NULL, dio_flags,
912 				   NULL, 0);
913 
914 		if (err <= 0)
915 			goto out;
916 		end = vbo + err;
917 		if (valid < end) {
918 			size_t to_zero = end - valid;
919 			/* Fix iter. */
920 			iov_iter_revert(iter, to_zero);
921 			iov_iter_zero(to_zero, iter);
922 		}
923 	} else if (vbo < i_size) {
924 		if (end > i_size)
925 			bytes = i_size - vbo;
926 		iov_iter_zero(bytes, iter);
927 		iocb->ki_pos += bytes;
928 		err = bytes;
929 	}
930 
931 out:
932 	inode_unlock_shared(inode);
933 	file_accessed(iocb->ki_filp);
934 	return err;
935 }
936 
937 /*
938  * ntfs_file_splice_read - file_operations::splice_read
939  */
940 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
941 				     struct pipe_inode_info *pipe, size_t len,
942 				     unsigned int flags)
943 {
944 	struct inode *inode = file_inode(in);
945 	ssize_t err;
946 
947 	err = check_read_restriction(inode);
948 	if (err)
949 		return err;
950 
951 	if (is_compressed(ntfs_i(inode))) {
952 		/* Turn off readahead for compressed files. */
953 		in->f_ra.ra_pages = 0;
954 	}
955 
956 	return filemap_splice_read(in, ppos, pipe, len, flags);
957 }
958 
959 /*
960  * ntfs_get_frame_pages
961  *
962  * Return: Array of locked pages.
963  */
964 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
965 				struct page **pages, u32 pages_per_frame,
966 				bool *frame_uptodate)
967 {
968 	gfp_t gfp_mask = mapping_gfp_mask(mapping);
969 	u32 npages;
970 
971 	*frame_uptodate = true;
972 
973 	for (npages = 0; npages < pages_per_frame; npages++, index++) {
974 		struct folio *folio;
975 
976 		folio = __filemap_get_folio(mapping, index,
977 					    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
978 					    gfp_mask | __GFP_ZERO);
979 		if (IS_ERR(folio)) {
980 			while (npages--) {
981 				folio = page_folio(pages[npages]);
982 				folio_unlock(folio);
983 				folio_put(folio);
984 			}
985 
986 			return -ENOMEM;
987 		}
988 
989 		if (!folio_test_uptodate(folio))
990 			*frame_uptodate = false;
991 
992 		pages[npages] = &folio->page;
993 	}
994 
995 	return 0;
996 }
997 
998 /*
999  * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
1000  */
1001 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
1002 {
1003 	int err;
1004 	struct file *file = iocb->ki_filp;
1005 	size_t count = iov_iter_count(from);
1006 	loff_t pos = iocb->ki_pos;
1007 	struct inode *inode = file_inode(file);
1008 	loff_t i_size = i_size_read(inode);
1009 	struct address_space *mapping = inode->i_mapping;
1010 	struct ntfs_inode *ni = ntfs_i(inode);
1011 	u64 valid = ni->i_valid;
1012 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1013 	struct page **pages = NULL;
1014 	struct folio *folio;
1015 	size_t written = 0;
1016 	u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
1017 	u32 frame_size = 1u << frame_bits;
1018 	u32 pages_per_frame = frame_size >> PAGE_SHIFT;
1019 	u32 ip, off;
1020 	CLST frame;
1021 	u64 frame_vbo;
1022 	pgoff_t index;
1023 	bool frame_uptodate;
1024 
1025 	if (frame_size < PAGE_SIZE) {
1026 		/*
1027 		 * frame_size == 8K if cluster 512
1028 		 * frame_size == 64K if cluster 4096
1029 		 */
1030 		ntfs_inode_warn(inode, "page size is bigger than frame size");
1031 		return -EOPNOTSUPP;
1032 	}
1033 
1034 	pages = kmalloc_objs(struct page *, pages_per_frame, GFP_NOFS);
1035 	if (!pages)
1036 		return -ENOMEM;
1037 
1038 	err = file_remove_privs(file);
1039 	if (err)
1040 		goto out;
1041 
1042 	err = file_update_time(file);
1043 	if (err)
1044 		goto out;
1045 
1046 	/* Zero range [valid : pos). */
1047 	while (valid < pos) {
1048 		CLST lcn, clen;
1049 
1050 		frame = valid >> frame_bits;
1051 		frame_vbo = valid & ~(frame_size - 1);
1052 		off = valid & (frame_size - 1);
1053 
1054 		err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
1055 					  &clen, NULL, false, NULL, false);
1056 		if (err)
1057 			goto out;
1058 
1059 		if (lcn == SPARSE_LCN) {
1060 			ni->i_valid = valid =
1061 				frame_vbo + ((u64)clen << sbi->cluster_bits);
1062 			continue;
1063 		}
1064 
1065 		/* Load full frame. */
1066 		err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
1067 					   pages, pages_per_frame,
1068 					   &frame_uptodate);
1069 		if (err)
1070 			goto out;
1071 
1072 		if (!frame_uptodate && off) {
1073 			err = ni_read_frame(ni, frame_vbo, pages,
1074 					    pages_per_frame, 0);
1075 			if (err) {
1076 				for (ip = 0; ip < pages_per_frame; ip++) {
1077 					folio = page_folio(pages[ip]);
1078 					folio_unlock(folio);
1079 					folio_put(folio);
1080 				}
1081 				goto out;
1082 			}
1083 		}
1084 
1085 		ip = off >> PAGE_SHIFT;
1086 		off = offset_in_page(valid);
1087 		for (; ip < pages_per_frame; ip++, off = 0) {
1088 			folio = page_folio(pages[ip]);
1089 			folio_zero_segment(folio, off, PAGE_SIZE);
1090 			flush_dcache_folio(folio);
1091 			folio_mark_uptodate(folio);
1092 		}
1093 
1094 		ni_lock(ni);
1095 		err = ni_write_frame(ni, pages, pages_per_frame);
1096 		ni_unlock(ni);
1097 
1098 		for (ip = 0; ip < pages_per_frame; ip++) {
1099 			folio = page_folio(pages[ip]);
1100 			folio_mark_uptodate(folio);
1101 			folio_unlock(folio);
1102 			folio_put(folio);
1103 		}
1104 
1105 		if (err)
1106 			goto out;
1107 
1108 		ni->i_valid = valid = frame_vbo + frame_size;
1109 	}
1110 
1111 	/* Copy user data [pos : pos + count). */
1112 	while (count) {
1113 		size_t copied, bytes;
1114 
1115 		off = pos & (frame_size - 1);
1116 		bytes = frame_size - off;
1117 		if (bytes > count)
1118 			bytes = count;
1119 
1120 		frame_vbo = pos & ~(frame_size - 1);
1121 		index = frame_vbo >> PAGE_SHIFT;
1122 
1123 		if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
1124 			err = -EFAULT;
1125 			goto out;
1126 		}
1127 
1128 		/* Load full frame. */
1129 		err = ntfs_get_frame_pages(mapping, index, pages,
1130 					   pages_per_frame, &frame_uptodate);
1131 		if (err)
1132 			goto out;
1133 
1134 		if (!frame_uptodate) {
1135 			loff_t to = pos + bytes;
1136 
1137 			if (off || (to < i_size && (to & (frame_size - 1)))) {
1138 				err = ni_read_frame(ni, frame_vbo, pages,
1139 						    pages_per_frame, 0);
1140 				if (err) {
1141 					for (ip = 0; ip < pages_per_frame;
1142 					     ip++) {
1143 						folio = page_folio(pages[ip]);
1144 						folio_unlock(folio);
1145 						folio_put(folio);
1146 					}
1147 					goto out;
1148 				}
1149 			}
1150 		}
1151 
1152 		WARN_ON(!bytes);
1153 		copied = 0;
1154 		ip = off >> PAGE_SHIFT;
1155 		off = offset_in_page(pos);
1156 
1157 		/* Copy user data to pages. */
1158 		for (;;) {
1159 			size_t cp, tail = PAGE_SIZE - off;
1160 
1161 			folio = page_folio(pages[ip]);
1162 			cp = copy_folio_from_iter_atomic(
1163 				folio, off, min(tail, bytes), from);
1164 			flush_dcache_folio(folio);
1165 
1166 			copied += cp;
1167 			bytes -= cp;
1168 			if (!bytes || !cp)
1169 				break;
1170 
1171 			if (cp < tail) {
1172 				off += cp;
1173 			} else {
1174 				ip++;
1175 				off = 0;
1176 			}
1177 		}
1178 
1179 		ni_lock(ni);
1180 		err = ni_write_frame(ni, pages, pages_per_frame);
1181 		ni_unlock(ni);
1182 
1183 		for (ip = 0; ip < pages_per_frame; ip++) {
1184 			folio = page_folio(pages[ip]);
1185 			folio_clear_dirty(folio);
1186 			folio_mark_uptodate(folio);
1187 			folio_unlock(folio);
1188 			folio_put(folio);
1189 		}
1190 
1191 		if (err)
1192 			goto out;
1193 
1194 		/*
1195 		 * We can loop for a long time in here. Be nice and allow
1196 		 * us to schedule out to avoid softlocking if preempt
1197 		 * is disabled.
1198 		 */
1199 		cond_resched();
1200 
1201 		pos += copied;
1202 		written += copied;
1203 
1204 		count = iov_iter_count(from);
1205 	}
1206 
1207 out:
1208 	kfree(pages);
1209 
1210 	if (err < 0)
1211 		return err;
1212 
1213 	iocb->ki_pos += written;
1214 	if (iocb->ki_pos > ni->i_valid)
1215 		ni->i_valid = iocb->ki_pos;
1216 	if (iocb->ki_pos > i_size)
1217 		i_size_write(inode, iocb->ki_pos);
1218 
1219 	return written;
1220 }
1221 
1222 /*
1223  * check_write_restriction:
1224  * common code for ntfs_file_write_iter and ntfs_file_splice_write
1225  */
1226 static int check_write_restriction(struct inode *inode)
1227 {
1228 	struct ntfs_inode *ni = ntfs_i(inode);
1229 
1230 	/* Avoid any operation if inode is bad. */
1231 	if (unlikely(is_bad_ni(ni)))
1232 		return -EINVAL;
1233 
1234 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1235 		return -EIO;
1236 
1237 	if (is_encrypted(ni)) {
1238 		ntfs_inode_warn(inode, "encrypted i/o not supported");
1239 		return -EOPNOTSUPP;
1240 	}
1241 
1242 	if (is_dedup(ni)) {
1243 		ntfs_inode_warn(inode, "write into deduplicated not supported");
1244 		return -EOPNOTSUPP;
1245 	}
1246 
1247 	if (unlikely(IS_IMMUTABLE(inode)))
1248 		return -EPERM;
1249 
1250 	return 0;
1251 }
1252 
1253 /*
1254  * ntfs_file_write_iter - file_operations::write_iter
1255  */
1256 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1257 {
1258 	struct file *file = iocb->ki_filp;
1259 	struct inode *inode = file_inode(file);
1260 	struct ntfs_inode *ni = ntfs_i(inode);
1261 	ssize_t ret, err;
1262 
1263 	if (!inode_trylock(inode)) {
1264 		if (iocb->ki_flags & IOCB_NOWAIT)
1265 			return -EAGAIN;
1266 		inode_lock(inode);
1267 	}
1268 
1269 	ret = check_write_restriction(inode);
1270 	if (ret)
1271 		goto out;
1272 
1273 	if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1274 		ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1275 		ret = -EOPNOTSUPP;
1276 		goto out;
1277 	}
1278 
1279 	ret = generic_write_checks(iocb, from);
1280 	if (ret <= 0)
1281 		goto out;
1282 
1283 	err = file_modified(iocb->ki_filp);
1284 	if (err) {
1285 		ret = err;
1286 		goto out;
1287 	}
1288 
1289 	if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1290 		/* Should never be here, see ntfs_file_open(). */
1291 		ret = -EOPNOTSUPP;
1292 		goto out;
1293 	}
1294 
1295 	ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1296 	if (ret)
1297 		goto out;
1298 
1299 	if (is_compressed(ni)) {
1300 		ret = ntfs_compress_write(iocb, from);
1301 		goto out;
1302 	}
1303 
1304 	/* Fallback to buffered I/O if the inode does not support direct I/O. */
1305 	if (!(iocb->ki_flags & IOCB_DIRECT) ||
1306 	    !ntfs_should_use_dio(iocb, from)) {
1307 		iocb->ki_flags &= ~IOCB_DIRECT;
1308 
1309 		ret = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops,
1310 						&ntfs_iomap_folio_ops, NULL);
1311 		inode_unlock(inode);
1312 
1313 		if (likely(ret > 0))
1314 			ret = generic_write_sync(iocb, ret);
1315 
1316 		return ret;
1317 	}
1318 
1319 	if (ni->file.run_da.count) {
1320 		/* Direct I/O is not compatible with delalloc. */
1321 		ret = ni_allocate_da_blocks(ni);
1322 		if (ret)
1323 			goto out;
1324 	}
1325 
1326 	ret = iomap_dio_rw(iocb, from, &ntfs_iomap_ops, NULL, 0, NULL, 0);
1327 
1328 	if (ret == -ENOTBLK) {
1329 		/* Returns -ENOTBLK in case of a page invalidation failure for writes.*/
1330 		/* The callers needs to fall back to buffered I/O in this case. */
1331 		ret = 0;
1332 	}
1333 
1334 	if (ret >= 0 && iov_iter_count(from)) {
1335 		loff_t offset = iocb->ki_pos, endbyte;
1336 
1337 		iocb->ki_flags &= ~IOCB_DIRECT;
1338 		err = iomap_file_buffered_write(iocb, from, &ntfs_iomap_ops,
1339 						&ntfs_iomap_folio_ops, NULL);
1340 		if (err < 0) {
1341 			ret = err;
1342 			goto out;
1343 		}
1344 
1345 		/*
1346 		* We need to ensure that the pages within the page cache for
1347 		* the range covered by this I/O are written to disk and
1348 		* invalidated. This is in attempt to preserve the expected
1349 		* direct I/O semantics in the case we fallback to buffered I/O
1350 		* to complete off the I/O request.
1351 		*/
1352 		ret += err;
1353 		endbyte = offset + err - 1;
1354 		err = filemap_write_and_wait_range(inode->i_mapping, offset,
1355 						   endbyte);
1356 		if (err) {
1357 			ret = err;
1358 			goto out;
1359 		}
1360 
1361 		invalidate_mapping_pages(inode->i_mapping, offset >> PAGE_SHIFT,
1362 					 endbyte >> PAGE_SHIFT);
1363 	}
1364 
1365 out:
1366 	inode_unlock(inode);
1367 
1368 	return ret;
1369 }
1370 
1371 /*
1372  * ntfs_file_open - file_operations::open
1373  */
1374 int ntfs_file_open(struct inode *inode, struct file *file)
1375 {
1376 	struct ntfs_inode *ni = ntfs_i(inode);
1377 
1378 	/* Avoid any operation if inode is bad. */
1379 	if (unlikely(is_bad_ni(ni)))
1380 		return -EINVAL;
1381 
1382 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1383 		return -EIO;
1384 
1385 	if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1386 		     (file->f_flags & O_DIRECT))) {
1387 		return -EOPNOTSUPP;
1388 	}
1389 
1390 	/* Decompress "external compressed" file if opened for rw. */
1391 	if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1392 	    (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1393 #ifdef CONFIG_NTFS3_LZX_XPRESS
1394 		int err = ni_decompress_file(ni);
1395 
1396 		if (err)
1397 			return err;
1398 #else
1399 		ntfs_inode_warn(
1400 			inode,
1401 			"activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1402 		return -EOPNOTSUPP;
1403 #endif
1404 	}
1405 
1406 	file->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
1407 
1408 	return generic_file_open(inode, file);
1409 }
1410 
1411 /*
1412  * ntfs_file_release - file_operations::release
1413  *
1414  * Called when an inode is released. Note that this is different
1415  * from ntfs_file_open: open gets called at every open, but release
1416  * gets called only when /all/ the files are closed.
1417  */
1418 static int ntfs_file_release(struct inode *inode, struct file *file)
1419 {
1420 	int err;
1421 	struct ntfs_inode *ni;
1422 
1423 	if (!(file->f_mode & FMODE_WRITE) ||
1424 	    atomic_read(&inode->i_writecount) != 1 ||
1425 	    inode->i_ino == MFT_REC_MFT) {
1426 		return 0;
1427 	}
1428 
1429 	/* Close the last writer on the inode. */
1430 	ni = ntfs_i(inode);
1431 
1432 	/* Allocate delayed blocks (clusters). */
1433 	err = ni_allocate_da_blocks(ni);
1434 	if (err)
1435 		goto out;
1436 
1437 	if (ni->mi.sbi->options->prealloc) {
1438 		ni_lock(ni);
1439 		down_write(&ni->file.run_lock);
1440 
1441 		/* Deallocate preallocated. */
1442 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1443 				    inode->i_size, &ni->i_valid, false);
1444 
1445 		up_write(&ni->file.run_lock);
1446 		ni_unlock(ni);
1447 	}
1448 out:
1449 	return err;
1450 }
1451 
1452 /*
1453  * ntfs_fiemap - inode_operations::fiemap
1454  */
1455 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1456 		__u64 start, __u64 len)
1457 {
1458 	int err;
1459 	struct ntfs_inode *ni = ntfs_i(inode);
1460 
1461 	/* Avoid any operation if inode is bad. */
1462 	if (unlikely(is_bad_ni(ni)))
1463 		return -EINVAL;
1464 
1465 	if (is_compressed(ni)) {
1466 		/* Unfortunately cp -r incorrectly treats compressed clusters. */
1467 		ntfs_inode_warn(inode,
1468 				"fiemap is not supported for compressed file");
1469 		return -EOPNOTSUPP;
1470 	}
1471 
1472 	if (S_ISDIR(inode->i_mode)) {
1473 		/* TODO: add support for dirs (ATTR_ALLOC). */
1474 		ntfs_inode_warn(inode,
1475 				"fiemap is not supported for directories");
1476 		return -EOPNOTSUPP;
1477 	}
1478 
1479 	if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
1480 		ntfs_inode_warn(inode, "fiemap(xattr) is not supported");
1481 		return -EOPNOTSUPP;
1482 	}
1483 
1484 	inode_lock_shared(inode);
1485 
1486 	err = iomap_fiemap(inode, fieinfo, start, len, &ntfs_iomap_ops);
1487 
1488 	inode_unlock_shared(inode);
1489 	return err;
1490 }
1491 
1492 /*
1493  * ntfs_file_splice_write - file_operations::splice_write
1494  */
1495 static ssize_t ntfs_file_splice_write(struct pipe_inode_info *pipe,
1496 				      struct file *file, loff_t *ppos,
1497 				      size_t len, unsigned int flags)
1498 {
1499 	ssize_t err;
1500 	struct inode *inode = file_inode(file);
1501 
1502 	err = check_write_restriction(inode);
1503 	if (err)
1504 		return err;
1505 
1506 	return iter_file_splice_write(pipe, file, ppos, len, flags);
1507 }
1508 
1509 /*
1510  * ntfs_file_fsync - file_operations::fsync
1511  */
1512 int ntfs_file_fsync(struct file *file, loff_t start, loff_t end, int datasync)
1513 {
1514 	struct inode *inode = file_inode(file);
1515 	struct super_block *sb = inode->i_sb;
1516 	struct ntfs_sb_info *sbi = sb->s_fs_info;
1517 	int err, ret;
1518 
1519 	if (unlikely(ntfs3_forced_shutdown(sb)))
1520 		return -EIO;
1521 
1522 	ret = file_write_and_wait_range(file, start, end);
1523 	if (ret)
1524 		return ret;
1525 
1526 	ret = write_inode_now(inode, !datasync);
1527 
1528 	if (!ret) {
1529 		ret = ni_write_parents(ntfs_i(inode), !datasync);
1530 	}
1531 
1532 	if (!ret) {
1533 		ntfs_set_state(sbi, NTFS_DIRTY_CLEAR);
1534 		ntfs_update_mftmirr(sbi);
1535 	}
1536 
1537 	err = sync_blockdev(sb->s_bdev);
1538 	if (unlikely(err && !ret))
1539 		ret = err;
1540 	if (!ret)
1541 		blkdev_issue_flush(sb->s_bdev);
1542 	return ret;
1543 }
1544 
1545 /*
1546  * ntfs_llseek - file_operations::llseek
1547  */
1548 static loff_t ntfs_llseek(struct file *file, loff_t offset, int whence)
1549 {
1550 	struct inode *inode = file->f_mapping->host;
1551 	struct ntfs_inode *ni = ntfs_i(inode);
1552 	loff_t maxbytes = ntfs_get_maxbytes(ni);
1553 	loff_t ret;
1554 
1555 	if (whence == SEEK_DATA || whence == SEEK_HOLE) {
1556 		inode_lock_shared(inode);
1557 		/* Scan file for hole or data. */
1558 		ret = ni_seek_data_or_hole(ni, offset, whence == SEEK_DATA);
1559 		inode_unlock_shared(inode);
1560 
1561 		if (ret >= 0)
1562 			ret = vfs_setpos(file, ret, maxbytes);
1563 	} else {
1564 		ret = generic_file_llseek_size(file, offset, whence, maxbytes,
1565 					       i_size_read(inode));
1566 	}
1567 	return ret;
1568 }
1569 
1570 // clang-format off
1571 const struct inode_operations ntfs_file_inode_operations = {
1572 	.getattr	= ntfs_getattr,
1573 	.setattr	= ntfs_setattr,
1574 	.listxattr	= ntfs_listxattr,
1575 	.get_acl	= ntfs_get_acl,
1576 	.set_acl	= ntfs_set_acl,
1577 	.fiemap		= ntfs_fiemap,
1578 	.fileattr_get	= ntfs_fileattr_get,
1579 };
1580 
1581 const struct file_operations ntfs_file_operations = {
1582 	.llseek		= ntfs_llseek,
1583 	.read_iter	= ntfs_file_read_iter,
1584 	.write_iter	= ntfs_file_write_iter,
1585 	.unlocked_ioctl = ntfs_ioctl,
1586 #ifdef CONFIG_COMPAT
1587 	.compat_ioctl	= ntfs_compat_ioctl,
1588 #endif
1589 	.splice_read	= ntfs_file_splice_read,
1590 	.splice_write	= ntfs_file_splice_write,
1591 	.mmap_prepare	= ntfs_file_mmap_prepare,
1592 	.open		= ntfs_file_open,
1593 	.fsync		= ntfs_file_fsync,
1594 	.fallocate	= ntfs_fallocate,
1595 	.release	= ntfs_file_release,
1596 	.setlease	= generic_setlease,
1597 };
1598 // clang-format on
1599