xref: /linux/fs/ntfs3/file.c (revision a9b38767c607e0de219b66a2b1ba0cb37beaba08)
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 
18 #include "debug.h"
19 #include "ntfs.h"
20 #include "ntfs_fs.h"
21 
ntfs_ioctl_fitrim(struct ntfs_sb_info * sbi,unsigned long arg)22 static int ntfs_ioctl_fitrim(struct ntfs_sb_info *sbi, unsigned long arg)
23 {
24 	struct fstrim_range __user *user_range;
25 	struct fstrim_range range;
26 	struct block_device *dev;
27 	int err;
28 
29 	if (!capable(CAP_SYS_ADMIN))
30 		return -EPERM;
31 
32 	dev = sbi->sb->s_bdev;
33 	if (!bdev_max_discard_sectors(dev))
34 		return -EOPNOTSUPP;
35 
36 	user_range = (struct fstrim_range __user *)arg;
37 	if (copy_from_user(&range, user_range, sizeof(range)))
38 		return -EFAULT;
39 
40 	range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
41 
42 	err = ntfs_trim_fs(sbi, &range);
43 	if (err < 0)
44 		return err;
45 
46 	if (copy_to_user(user_range, &range, sizeof(range)))
47 		return -EFAULT;
48 
49 	return 0;
50 }
51 
ntfs_ioctl_get_volume_label(struct ntfs_sb_info * sbi,u8 __user * buf)52 static int ntfs_ioctl_get_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
53 {
54 	if (copy_to_user(buf, sbi->volume.label, FSLABEL_MAX))
55 		return -EFAULT;
56 
57 	return 0;
58 }
59 
ntfs_ioctl_set_volume_label(struct ntfs_sb_info * sbi,u8 __user * buf)60 static int ntfs_ioctl_set_volume_label(struct ntfs_sb_info *sbi, u8 __user *buf)
61 {
62 	u8 user[FSLABEL_MAX] = {0};
63 	int len;
64 
65 	if (!capable(CAP_SYS_ADMIN))
66 		return -EPERM;
67 
68 	if (copy_from_user(user, buf, FSLABEL_MAX))
69 		return -EFAULT;
70 
71 	len = strnlen(user, FSLABEL_MAX);
72 
73 	return ntfs_set_label(sbi, user, len);
74 }
75 
76 /*
77  * ntfs_ioctl - file_operations::unlocked_ioctl
78  */
ntfs_ioctl(struct file * filp,u32 cmd,unsigned long arg)79 long ntfs_ioctl(struct file *filp, u32 cmd, unsigned long arg)
80 {
81 	struct inode *inode = file_inode(filp);
82 	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
83 
84 	/* Avoid any operation if inode is bad. */
85 	if (unlikely(is_bad_ni(ntfs_i(inode))))
86 		return -EINVAL;
87 
88 	switch (cmd) {
89 	case FITRIM:
90 		return ntfs_ioctl_fitrim(sbi, arg);
91 	case FS_IOC_GETFSLABEL:
92 		return ntfs_ioctl_get_volume_label(sbi, (u8 __user *)arg);
93 	case FS_IOC_SETFSLABEL:
94 		return ntfs_ioctl_set_volume_label(sbi, (u8 __user *)arg);
95 	}
96 	return -ENOTTY; /* Inappropriate ioctl for device. */
97 }
98 
99 #ifdef CONFIG_COMPAT
ntfs_compat_ioctl(struct file * filp,u32 cmd,unsigned long arg)100 long ntfs_compat_ioctl(struct file *filp, u32 cmd, unsigned long arg)
101 
102 {
103 	return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
104 }
105 #endif
106 
107 /*
108  * ntfs_getattr - inode_operations::getattr
109  */
ntfs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,u32 flags)110 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
111 		 struct kstat *stat, u32 request_mask, u32 flags)
112 {
113 	struct inode *inode = d_inode(path->dentry);
114 	struct ntfs_inode *ni = ntfs_i(inode);
115 
116 	/* Avoid any operation if inode is bad. */
117 	if (unlikely(is_bad_ni(ni)))
118 		return -EINVAL;
119 
120 	stat->result_mask |= STATX_BTIME;
121 	stat->btime = ni->i_crtime;
122 	stat->blksize = ni->mi.sbi->cluster_size; /* 512, 1K, ..., 2M */
123 
124 	if (inode->i_flags & S_IMMUTABLE)
125 		stat->attributes |= STATX_ATTR_IMMUTABLE;
126 
127 	if (inode->i_flags & S_APPEND)
128 		stat->attributes |= STATX_ATTR_APPEND;
129 
130 	if (is_compressed(ni))
131 		stat->attributes |= STATX_ATTR_COMPRESSED;
132 
133 	if (is_encrypted(ni))
134 		stat->attributes |= STATX_ATTR_ENCRYPTED;
135 
136 	stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |
137 				 STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
138 
139 	generic_fillattr(idmap, request_mask, inode, stat);
140 
141 	return 0;
142 }
143 
ntfs_extend_initialized_size(struct file * file,struct ntfs_inode * ni,const loff_t valid,const loff_t new_valid)144 static int ntfs_extend_initialized_size(struct file *file,
145 					struct ntfs_inode *ni,
146 					const loff_t valid,
147 					const loff_t new_valid)
148 {
149 	struct inode *inode = &ni->vfs_inode;
150 	struct address_space *mapping = inode->i_mapping;
151 	struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
152 	loff_t pos = valid;
153 	int err;
154 
155 	if (valid >= new_valid)
156 		return 0;
157 
158 	if (is_resident(ni)) {
159 		ni->i_valid = new_valid;
160 		return 0;
161 	}
162 
163 	WARN_ON(is_compressed(ni));
164 
165 	for (;;) {
166 		u32 zerofrom, len;
167 		struct folio *folio;
168 		u8 bits;
169 		CLST vcn, lcn, clen;
170 
171 		if (is_sparsed(ni)) {
172 			bits = sbi->cluster_bits;
173 			vcn = pos >> bits;
174 
175 			err = attr_data_get_block(ni, vcn, 1, &lcn, &clen, NULL,
176 						  false);
177 			if (err)
178 				goto out;
179 
180 			if (lcn == SPARSE_LCN) {
181 				pos = ((loff_t)clen + vcn) << bits;
182 				ni->i_valid = pos;
183 				goto next;
184 			}
185 		}
186 
187 		zerofrom = pos & (PAGE_SIZE - 1);
188 		len = PAGE_SIZE - zerofrom;
189 
190 		if (pos + len > new_valid)
191 			len = new_valid - pos;
192 
193 		err = ntfs_write_begin(NULL, mapping, pos, len, &folio, NULL);
194 		if (err)
195 			goto out;
196 
197 		folio_zero_range(folio, zerofrom, folio_size(folio) - zerofrom);
198 
199 		err = ntfs_write_end(NULL, mapping, pos, len, len, folio, NULL);
200 		if (err < 0)
201 			goto out;
202 		pos += len;
203 
204 next:
205 		if (pos >= new_valid)
206 			break;
207 
208 		balance_dirty_pages_ratelimited(mapping);
209 		cond_resched();
210 	}
211 
212 	return 0;
213 
214 out:
215 	ni->i_valid = valid;
216 	ntfs_inode_warn(inode, "failed to extend initialized size to %llx.",
217 			new_valid);
218 	return err;
219 }
220 
221 /*
222  * ntfs_zero_range - Helper function for punch_hole.
223  *
224  * It zeroes a range [vbo, vbo_to).
225  */
ntfs_zero_range(struct inode * inode,u64 vbo,u64 vbo_to)226 static int ntfs_zero_range(struct inode *inode, u64 vbo, u64 vbo_to)
227 {
228 	int err = 0;
229 	struct address_space *mapping = inode->i_mapping;
230 	u32 blocksize = i_blocksize(inode);
231 	pgoff_t idx = vbo >> PAGE_SHIFT;
232 	u32 from = vbo & (PAGE_SIZE - 1);
233 	pgoff_t idx_end = (vbo_to + PAGE_SIZE - 1) >> PAGE_SHIFT;
234 	loff_t page_off;
235 	struct buffer_head *head, *bh;
236 	u32 bh_next, bh_off, to;
237 	sector_t iblock;
238 	struct folio *folio;
239 	bool dirty = false;
240 
241 	for (; idx < idx_end; idx += 1, from = 0) {
242 		page_off = (loff_t)idx << PAGE_SHIFT;
243 		to = (page_off + PAGE_SIZE) > vbo_to ? (vbo_to - page_off) :
244 						       PAGE_SIZE;
245 		iblock = page_off >> inode->i_blkbits;
246 
247 		folio = __filemap_get_folio(
248 			mapping, idx, FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
249 			mapping_gfp_constraint(mapping, ~__GFP_FS));
250 		if (IS_ERR(folio))
251 			return PTR_ERR(folio);
252 
253 		head = folio_buffers(folio);
254 		if (!head)
255 			head = create_empty_buffers(folio, blocksize, 0);
256 
257 		bh = head;
258 		bh_off = 0;
259 		do {
260 			bh_next = bh_off + blocksize;
261 
262 			if (bh_next <= from || bh_off >= to)
263 				continue;
264 
265 			if (!buffer_mapped(bh)) {
266 				ntfs_get_block(inode, iblock, bh, 0);
267 				/* Unmapped? It's a hole - nothing to do. */
268 				if (!buffer_mapped(bh))
269 					continue;
270 			}
271 
272 			/* Ok, it's mapped. Make sure it's up-to-date. */
273 			if (folio_test_uptodate(folio))
274 				set_buffer_uptodate(bh);
275 			else if (bh_read(bh, 0) < 0) {
276 				err = -EIO;
277 				folio_unlock(folio);
278 				folio_put(folio);
279 				goto out;
280 			}
281 
282 			mark_buffer_dirty(bh);
283 		} while (bh_off = bh_next, iblock += 1,
284 			 head != (bh = bh->b_this_page));
285 
286 		folio_zero_segment(folio, from, to);
287 		dirty = true;
288 
289 		folio_unlock(folio);
290 		folio_put(folio);
291 		cond_resched();
292 	}
293 out:
294 	if (dirty)
295 		mark_inode_dirty(inode);
296 	return err;
297 }
298 
299 /*
300  * ntfs_file_mmap_prepare - file_operations::mmap_prepare
301  */
ntfs_file_mmap_prepare(struct vm_area_desc * desc)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 	u64 from = ((u64)desc->pgoff << PAGE_SHIFT);
308 	bool rw = desc->vm_flags & VM_WRITE;
309 	int err;
310 
311 	/* Avoid any operation if inode is bad. */
312 	if (unlikely(is_bad_ni(ni)))
313 		return -EINVAL;
314 
315 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
316 		return -EIO;
317 
318 	if (is_encrypted(ni)) {
319 		ntfs_inode_warn(inode, "mmap encrypted not supported");
320 		return -EOPNOTSUPP;
321 	}
322 
323 	if (is_dedup(ni)) {
324 		ntfs_inode_warn(inode, "mmap deduplicated not supported");
325 		return -EOPNOTSUPP;
326 	}
327 
328 	if (is_compressed(ni) && rw) {
329 		ntfs_inode_warn(inode, "mmap(write) compressed not supported");
330 		return -EOPNOTSUPP;
331 	}
332 
333 	if (rw) {
334 		u64 to = min_t(loff_t, i_size_read(inode),
335 			       from + desc->end - desc->start);
336 
337 		if (is_sparsed(ni)) {
338 			/* Allocate clusters for rw map. */
339 			struct ntfs_sb_info *sbi = inode->i_sb->s_fs_info;
340 			CLST lcn, len;
341 			CLST vcn = from >> sbi->cluster_bits;
342 			CLST end = bytes_to_cluster(sbi, to);
343 			bool new;
344 
345 			for (; vcn < end; vcn += len) {
346 				err = attr_data_get_block(ni, vcn, 1, &lcn,
347 							  &len, &new, true);
348 				if (err)
349 					goto out;
350 			}
351 		}
352 
353 		if (ni->i_valid < to) {
354 			if (!inode_trylock(inode)) {
355 				err = -EAGAIN;
356 				goto out;
357 			}
358 			err = ntfs_extend_initialized_size(file, ni,
359 							   ni->i_valid, to);
360 			inode_unlock(inode);
361 			if (err)
362 				goto out;
363 		}
364 	}
365 
366 	err = generic_file_mmap_prepare(desc);
367 out:
368 	return err;
369 }
370 
ntfs_extend(struct inode * inode,loff_t pos,size_t count,struct file * file)371 static int ntfs_extend(struct inode *inode, loff_t pos, size_t count,
372 		       struct file *file)
373 {
374 	struct ntfs_inode *ni = ntfs_i(inode);
375 	struct address_space *mapping = inode->i_mapping;
376 	loff_t end = pos + count;
377 	bool extend_init = file && pos > ni->i_valid;
378 	int err;
379 
380 	if (end <= inode->i_size && !extend_init)
381 		return 0;
382 
383 	/* Mark rw ntfs as dirty. It will be cleared at umount. */
384 	ntfs_set_state(ni->mi.sbi, NTFS_DIRTY_DIRTY);
385 
386 	if (end > inode->i_size) {
387 		err = ntfs_set_size(inode, end);
388 		if (err)
389 			goto out;
390 	}
391 
392 	if (extend_init && !is_compressed(ni)) {
393 		err = ntfs_extend_initialized_size(file, ni, ni->i_valid, pos);
394 		if (err)
395 			goto out;
396 	} else {
397 		err = 0;
398 	}
399 
400 	if (file && is_sparsed(ni)) {
401 		/*
402 		 * This code optimizes large writes to sparse file.
403 		 * TODO: merge this fragment with fallocate fragment.
404 		 */
405 		struct ntfs_sb_info *sbi = ni->mi.sbi;
406 		CLST vcn = pos >> sbi->cluster_bits;
407 		CLST cend = bytes_to_cluster(sbi, end);
408 		CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
409 		CLST lcn, clen;
410 		bool new;
411 
412 		if (cend_v > cend)
413 			cend_v = cend;
414 
415 		/*
416 		 * Allocate and zero new clusters.
417 		 * Zeroing these clusters may be too long.
418 		 */
419 		for (; vcn < cend_v; vcn += clen) {
420 			err = attr_data_get_block(ni, vcn, cend_v - vcn, &lcn,
421 						  &clen, &new, true);
422 			if (err)
423 				goto out;
424 		}
425 		/*
426 		 * Allocate but not zero new clusters.
427 		 */
428 		for (; vcn < cend; vcn += clen) {
429 			err = attr_data_get_block(ni, vcn, cend - vcn, &lcn,
430 						  &clen, &new, false);
431 			if (err)
432 				goto out;
433 		}
434 	}
435 
436 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
437 	mark_inode_dirty(inode);
438 
439 	if (IS_SYNC(inode)) {
440 		int err2;
441 
442 		err = filemap_fdatawrite_range(mapping, pos, end - 1);
443 		err2 = sync_mapping_buffers(mapping);
444 		if (!err)
445 			err = err2;
446 		err2 = write_inode_now(inode, 1);
447 		if (!err)
448 			err = err2;
449 		if (!err)
450 			err = filemap_fdatawait_range(mapping, pos, end - 1);
451 	}
452 
453 out:
454 	return err;
455 }
456 
ntfs_truncate(struct inode * inode,loff_t new_size)457 static int ntfs_truncate(struct inode *inode, loff_t new_size)
458 {
459 	struct super_block *sb = inode->i_sb;
460 	struct ntfs_inode *ni = ntfs_i(inode);
461 	int err, dirty = 0;
462 	u64 new_valid;
463 
464 	if (!S_ISREG(inode->i_mode))
465 		return 0;
466 
467 	if (is_compressed(ni)) {
468 		if (ni->i_valid > new_size)
469 			ni->i_valid = new_size;
470 	} else {
471 		err = block_truncate_page(inode->i_mapping, new_size,
472 					  ntfs_get_block);
473 		if (err)
474 			return err;
475 	}
476 
477 	new_valid = ntfs_up_block(sb, min_t(u64, ni->i_valid, new_size));
478 
479 	truncate_setsize(inode, new_size);
480 
481 	ni_lock(ni);
482 
483 	down_write(&ni->file.run_lock);
484 	err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run, new_size,
485 			    &new_valid, ni->mi.sbi->options->prealloc, NULL);
486 	up_write(&ni->file.run_lock);
487 
488 	if (new_valid < ni->i_valid)
489 		ni->i_valid = new_valid;
490 
491 	ni_unlock(ni);
492 
493 	ni->std_fa |= FILE_ATTRIBUTE_ARCHIVE;
494 	inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
495 	if (!IS_DIRSYNC(inode)) {
496 		dirty = 1;
497 	} else {
498 		err = ntfs_sync_inode(inode);
499 		if (err)
500 			return err;
501 	}
502 
503 	if (dirty)
504 		mark_inode_dirty(inode);
505 
506 	/*ntfs_flush_inodes(inode->i_sb, inode, NULL);*/
507 
508 	return 0;
509 }
510 
511 /*
512  * ntfs_fallocate - file_operations::ntfs_fallocate
513  *
514  * Preallocate space for a file. This implements ntfs's fallocate file
515  * operation, which gets called from sys_fallocate system call. User
516  * space requests 'len' bytes at 'vbo'. If FALLOC_FL_KEEP_SIZE is set
517  * we just allocate clusters without zeroing them out. Otherwise we
518  * allocate and zero out clusters via an expanding truncate.
519  */
ntfs_fallocate(struct file * file,int mode,loff_t vbo,loff_t len)520 static long ntfs_fallocate(struct file *file, int mode, loff_t vbo, loff_t len)
521 {
522 	struct inode *inode = file_inode(file);
523 	struct address_space *mapping = inode->i_mapping;
524 	struct super_block *sb = inode->i_sb;
525 	struct ntfs_sb_info *sbi = sb->s_fs_info;
526 	struct ntfs_inode *ni = ntfs_i(inode);
527 	loff_t end = vbo + len;
528 	loff_t vbo_down = round_down(vbo, max_t(unsigned long,
529 						sbi->cluster_size, PAGE_SIZE));
530 	bool is_supported_holes = is_sparsed(ni) || is_compressed(ni);
531 	loff_t i_size, new_size;
532 	bool map_locked;
533 	int err;
534 
535 	/* No support for dir. */
536 	if (!S_ISREG(inode->i_mode))
537 		return -EOPNOTSUPP;
538 
539 	/*
540 	 * vfs_fallocate checks all possible combinations of mode.
541 	 * Do additional checks here before ntfs_set_state(dirty).
542 	 */
543 	if (mode & FALLOC_FL_PUNCH_HOLE) {
544 		if (!is_supported_holes)
545 			return -EOPNOTSUPP;
546 	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
547 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
548 		if (!is_supported_holes)
549 			return -EOPNOTSUPP;
550 	} else if (mode &
551 		   ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE |
552 		     FALLOC_FL_COLLAPSE_RANGE | FALLOC_FL_INSERT_RANGE)) {
553 		ntfs_inode_warn(inode, "fallocate(0x%x) is not supported",
554 				mode);
555 		return -EOPNOTSUPP;
556 	}
557 
558 	ntfs_set_state(sbi, NTFS_DIRTY_DIRTY);
559 
560 	inode_lock(inode);
561 	i_size = inode->i_size;
562 	new_size = max(end, i_size);
563 	map_locked = false;
564 
565 	if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
566 		/* Should never be here, see ntfs_file_open. */
567 		err = -EOPNOTSUPP;
568 		goto out;
569 	}
570 
571 	if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
572 		    FALLOC_FL_INSERT_RANGE)) {
573 		inode_dio_wait(inode);
574 		filemap_invalidate_lock(mapping);
575 		map_locked = true;
576 	}
577 
578 	if (mode & FALLOC_FL_PUNCH_HOLE) {
579 		u32 frame_size;
580 		loff_t mask, vbo_a, end_a, tmp;
581 
582 		err = filemap_write_and_wait_range(mapping, vbo_down,
583 						   LLONG_MAX);
584 		if (err)
585 			goto out;
586 
587 		truncate_pagecache(inode, vbo_down);
588 
589 		ni_lock(ni);
590 		err = attr_punch_hole(ni, vbo, len, &frame_size);
591 		ni_unlock(ni);
592 		if (!err)
593 			goto ok;
594 
595 		if (err != E_NTFS_NOTALIGNED)
596 			goto out;
597 
598 		/* Process not aligned punch. */
599 		err = 0;
600 		mask = frame_size - 1;
601 		vbo_a = (vbo + mask) & ~mask;
602 		end_a = end & ~mask;
603 
604 		tmp = min(vbo_a, end);
605 		if (tmp > vbo) {
606 			err = ntfs_zero_range(inode, vbo, tmp);
607 			if (err)
608 				goto out;
609 		}
610 
611 		if (vbo < end_a && end_a < end) {
612 			err = ntfs_zero_range(inode, end_a, end);
613 			if (err)
614 				goto out;
615 		}
616 
617 		/* Aligned punch_hole */
618 		if (end_a > vbo_a) {
619 			ni_lock(ni);
620 			err = attr_punch_hole(ni, vbo_a, end_a - vbo_a, NULL);
621 			ni_unlock(ni);
622 			if (err)
623 				goto out;
624 		}
625 	} else if (mode & FALLOC_FL_COLLAPSE_RANGE) {
626 		/*
627 		 * Write tail of the last page before removed range since
628 		 * it will get removed from the page cache below.
629 		 */
630 		err = filemap_write_and_wait_range(mapping, vbo_down, vbo);
631 		if (err)
632 			goto out;
633 
634 		/*
635 		 * Write data that will be shifted to preserve them
636 		 * when discarding page cache below.
637 		 */
638 		err = filemap_write_and_wait_range(mapping, end, LLONG_MAX);
639 		if (err)
640 			goto out;
641 
642 		truncate_pagecache(inode, vbo_down);
643 
644 		ni_lock(ni);
645 		err = attr_collapse_range(ni, vbo, len);
646 		ni_unlock(ni);
647 		if (err)
648 			goto out;
649 	} else if (mode & FALLOC_FL_INSERT_RANGE) {
650 		/* Check new size. */
651 		err = inode_newsize_ok(inode, new_size);
652 		if (err)
653 			goto out;
654 
655 		/* Write out all dirty pages. */
656 		err = filemap_write_and_wait_range(mapping, vbo_down,
657 						   LLONG_MAX);
658 		if (err)
659 			goto out;
660 		truncate_pagecache(inode, vbo_down);
661 
662 		ni_lock(ni);
663 		err = attr_insert_range(ni, vbo, len);
664 		ni_unlock(ni);
665 		if (err)
666 			goto out;
667 	} else {
668 		/* Check new size. */
669 		u8 cluster_bits = sbi->cluster_bits;
670 
671 		/* Be sure file is non resident. */
672 		if (is_resident(ni)) {
673 			ni_lock(ni);
674 			err = attr_force_nonresident(ni);
675 			ni_unlock(ni);
676 			if (err)
677 				goto out;
678 		}
679 
680 		/* generic/213: expected -ENOSPC instead of -EFBIG. */
681 		if (!is_supported_holes) {
682 			loff_t to_alloc = new_size - inode_get_bytes(inode);
683 
684 			if (to_alloc > 0 &&
685 			    (to_alloc >> cluster_bits) >
686 				    wnd_zeroes(&sbi->used.bitmap)) {
687 				err = -ENOSPC;
688 				goto out;
689 			}
690 		}
691 
692 		err = inode_newsize_ok(inode, new_size);
693 		if (err)
694 			goto out;
695 
696 		if (new_size > i_size) {
697 			/*
698 			 * Allocate clusters, do not change 'valid' size.
699 			 */
700 			err = ntfs_set_size(inode, new_size);
701 			if (err)
702 				goto out;
703 		}
704 
705 		if (is_supported_holes) {
706 			CLST vcn = vbo >> cluster_bits;
707 			CLST cend = bytes_to_cluster(sbi, end);
708 			CLST cend_v = bytes_to_cluster(sbi, ni->i_valid);
709 			CLST lcn, clen;
710 			bool new;
711 
712 			if (cend_v > cend)
713 				cend_v = cend;
714 
715 			/*
716 			 * Allocate and zero new clusters.
717 			 * Zeroing these clusters may be too long.
718 			 */
719 			for (; vcn < cend_v; vcn += clen) {
720 				err = attr_data_get_block(ni, vcn, cend_v - vcn,
721 							  &lcn, &clen, &new,
722 							  true);
723 				if (err)
724 					goto out;
725 			}
726 			/*
727 			 * Allocate but not zero new clusters.
728 			 */
729 			for (; vcn < cend; vcn += clen) {
730 				err = attr_data_get_block(ni, vcn, cend - vcn,
731 							  &lcn, &clen, &new,
732 							  false);
733 				if (err)
734 					goto out;
735 			}
736 		}
737 
738 		if (mode & FALLOC_FL_KEEP_SIZE) {
739 			ni_lock(ni);
740 			/* True - Keep preallocated. */
741 			err = attr_set_size(ni, ATTR_DATA, NULL, 0,
742 					    &ni->file.run, i_size, &ni->i_valid,
743 					    true, NULL);
744 			ni_unlock(ni);
745 			if (err)
746 				goto out;
747 		} else if (new_size > i_size) {
748 			i_size_write(inode, new_size);
749 		}
750 	}
751 
752 ok:
753 	err = file_modified(file);
754 	if (err)
755 		goto out;
756 
757 out:
758 	if (map_locked)
759 		filemap_invalidate_unlock(mapping);
760 
761 	if (!err) {
762 		inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
763 		mark_inode_dirty(inode);
764 	}
765 
766 	inode_unlock(inode);
767 	return err;
768 }
769 
770 /*
771  * ntfs_setattr - inode_operations::setattr
772  */
ntfs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * attr)773 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
774 		 struct iattr *attr)
775 {
776 	struct inode *inode = d_inode(dentry);
777 	struct ntfs_inode *ni = ntfs_i(inode);
778 	u32 ia_valid = attr->ia_valid;
779 	umode_t mode = inode->i_mode;
780 	int err;
781 
782 	/* Avoid any operation if inode is bad. */
783 	if (unlikely(is_bad_ni(ni)))
784 		return -EINVAL;
785 
786 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
787 		return -EIO;
788 
789 	err = setattr_prepare(idmap, dentry, attr);
790 	if (err)
791 		goto out;
792 
793 	if (ia_valid & ATTR_SIZE) {
794 		loff_t newsize, oldsize;
795 
796 		if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
797 			/* Should never be here, see ntfs_file_open(). */
798 			err = -EOPNOTSUPP;
799 			goto out;
800 		}
801 		inode_dio_wait(inode);
802 		oldsize = i_size_read(inode);
803 		newsize = attr->ia_size;
804 
805 		if (newsize <= oldsize)
806 			err = ntfs_truncate(inode, newsize);
807 		else
808 			err = ntfs_extend(inode, newsize, 0, NULL);
809 
810 		if (err)
811 			goto out;
812 
813 		ni->ni_flags |= NI_FLAG_UPDATE_PARENT;
814 		i_size_write(inode, newsize);
815 	}
816 
817 	setattr_copy(idmap, inode, attr);
818 
819 	if (mode != inode->i_mode) {
820 		err = ntfs_acl_chmod(idmap, dentry);
821 		if (err)
822 			goto out;
823 
824 		/* Linux 'w' -> Windows 'ro'. */
825 		if (0222 & inode->i_mode)
826 			ni->std_fa &= ~FILE_ATTRIBUTE_READONLY;
827 		else
828 			ni->std_fa |= FILE_ATTRIBUTE_READONLY;
829 	}
830 
831 	if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE))
832 		ntfs_save_wsl_perm(inode, NULL);
833 	mark_inode_dirty(inode);
834 out:
835 	return err;
836 }
837 
838 /*
839  * check_read_restriction:
840  * common code for ntfs_file_read_iter and ntfs_file_splice_read
841  */
check_read_restriction(struct inode * inode)842 static int check_read_restriction(struct inode *inode)
843 {
844 	struct ntfs_inode *ni = ntfs_i(inode);
845 
846 	/* Avoid any operation if inode is bad. */
847 	if (unlikely(is_bad_ni(ni)))
848 		return -EINVAL;
849 
850 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
851 		return -EIO;
852 
853 	if (is_encrypted(ni)) {
854 		ntfs_inode_warn(inode, "encrypted i/o not supported");
855 		return -EOPNOTSUPP;
856 	}
857 
858 #ifndef CONFIG_NTFS3_LZX_XPRESS
859 	if (ni->ni_flags & NI_FLAG_COMPRESSED_MASK) {
860 		ntfs_inode_warn(
861 			inode,
862 			"activate CONFIG_NTFS3_LZX_XPRESS to read external compressed files");
863 		return -EOPNOTSUPP;
864 	}
865 #endif
866 
867 	if (is_dedup(ni)) {
868 		ntfs_inode_warn(inode, "read deduplicated not supported");
869 		return -EOPNOTSUPP;
870 	}
871 
872 	return 0;
873 }
874 
875 /*
876  * ntfs_file_read_iter - file_operations::read_iter
877  */
ntfs_file_read_iter(struct kiocb * iocb,struct iov_iter * iter)878 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *iter)
879 {
880 	struct file *file = iocb->ki_filp;
881 	struct inode *inode = file_inode(file);
882 	struct ntfs_inode *ni = ntfs_i(inode);
883 	ssize_t err;
884 
885 	err = check_read_restriction(inode);
886 	if (err)
887 		return err;
888 
889 	if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
890 		ntfs_inode_warn(inode, "direct i/o + compressed not supported");
891 		return -EOPNOTSUPP;
892 	}
893 
894 	return generic_file_read_iter(iocb, iter);
895 }
896 
897 /*
898  * ntfs_file_splice_read - file_operations::splice_read
899  */
ntfs_file_splice_read(struct file * in,loff_t * ppos,struct pipe_inode_info * pipe,size_t len,unsigned int flags)900 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
901 				     struct pipe_inode_info *pipe, size_t len,
902 				     unsigned int flags)
903 {
904 	struct inode *inode = file_inode(in);
905 	ssize_t err;
906 
907 	err = check_read_restriction(inode);
908 	if (err)
909 		return err;
910 
911 	return filemap_splice_read(in, ppos, pipe, len, flags);
912 }
913 
914 /*
915  * ntfs_get_frame_pages
916  *
917  * Return: Array of locked pages.
918  */
ntfs_get_frame_pages(struct address_space * mapping,pgoff_t index,struct page ** pages,u32 pages_per_frame,bool * frame_uptodate)919 static int ntfs_get_frame_pages(struct address_space *mapping, pgoff_t index,
920 				struct page **pages, u32 pages_per_frame,
921 				bool *frame_uptodate)
922 {
923 	gfp_t gfp_mask = mapping_gfp_mask(mapping);
924 	u32 npages;
925 
926 	*frame_uptodate = true;
927 
928 	for (npages = 0; npages < pages_per_frame; npages++, index++) {
929 		struct folio *folio;
930 
931 		folio = __filemap_get_folio(mapping, index,
932 					    FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
933 					    gfp_mask);
934 		if (IS_ERR(folio)) {
935 			while (npages--) {
936 				folio = page_folio(pages[npages]);
937 				folio_unlock(folio);
938 				folio_put(folio);
939 			}
940 
941 			return -ENOMEM;
942 		}
943 
944 		if (!folio_test_uptodate(folio))
945 			*frame_uptodate = false;
946 
947 		pages[npages] = &folio->page;
948 	}
949 
950 	return 0;
951 }
952 
953 /*
954  * ntfs_compress_write - Helper for ntfs_file_write_iter() (compressed files).
955  */
ntfs_compress_write(struct kiocb * iocb,struct iov_iter * from)956 static ssize_t ntfs_compress_write(struct kiocb *iocb, struct iov_iter *from)
957 {
958 	int err;
959 	struct file *file = iocb->ki_filp;
960 	size_t count = iov_iter_count(from);
961 	loff_t pos = iocb->ki_pos;
962 	struct inode *inode = file_inode(file);
963 	loff_t i_size = i_size_read(inode);
964 	struct address_space *mapping = inode->i_mapping;
965 	struct ntfs_inode *ni = ntfs_i(inode);
966 	u64 valid = ni->i_valid;
967 	struct ntfs_sb_info *sbi = ni->mi.sbi;
968 	struct page **pages = NULL;
969 	struct folio *folio;
970 	size_t written = 0;
971 	u8 frame_bits = NTFS_LZNT_CUNIT + sbi->cluster_bits;
972 	u32 frame_size = 1u << frame_bits;
973 	u32 pages_per_frame = frame_size >> PAGE_SHIFT;
974 	u32 ip, off;
975 	CLST frame;
976 	u64 frame_vbo;
977 	pgoff_t index;
978 	bool frame_uptodate;
979 
980 	if (frame_size < PAGE_SIZE) {
981 		/*
982 		 * frame_size == 8K if cluster 512
983 		 * frame_size == 64K if cluster 4096
984 		 */
985 		ntfs_inode_warn(inode, "page size is bigger than frame size");
986 		return -EOPNOTSUPP;
987 	}
988 
989 	pages = kmalloc_array(pages_per_frame, sizeof(struct page *), GFP_NOFS);
990 	if (!pages)
991 		return -ENOMEM;
992 
993 	err = file_remove_privs(file);
994 	if (err)
995 		goto out;
996 
997 	err = file_update_time(file);
998 	if (err)
999 		goto out;
1000 
1001 	/* Zero range [valid : pos). */
1002 	while (valid < pos) {
1003 		CLST lcn, clen;
1004 
1005 		frame = valid >> frame_bits;
1006 		frame_vbo = valid & ~(frame_size - 1);
1007 		off = valid & (frame_size - 1);
1008 
1009 		err = attr_data_get_block(ni, frame << NTFS_LZNT_CUNIT, 1, &lcn,
1010 					  &clen, NULL, false);
1011 		if (err)
1012 			goto out;
1013 
1014 		if (lcn == SPARSE_LCN) {
1015 			ni->i_valid = valid =
1016 				frame_vbo + ((u64)clen << sbi->cluster_bits);
1017 			continue;
1018 		}
1019 
1020 		/* Load full frame. */
1021 		err = ntfs_get_frame_pages(mapping, frame_vbo >> PAGE_SHIFT,
1022 					   pages, pages_per_frame,
1023 					   &frame_uptodate);
1024 		if (err)
1025 			goto out;
1026 
1027 		if (!frame_uptodate && off) {
1028 			err = ni_read_frame(ni, frame_vbo, pages,
1029 					    pages_per_frame);
1030 			if (err) {
1031 				for (ip = 0; ip < pages_per_frame; ip++) {
1032 					folio = page_folio(pages[ip]);
1033 					folio_unlock(folio);
1034 					folio_put(folio);
1035 				}
1036 				goto out;
1037 			}
1038 		}
1039 
1040 		ip = off >> PAGE_SHIFT;
1041 		off = offset_in_page(valid);
1042 		for (; ip < pages_per_frame; ip++, off = 0) {
1043 			folio = page_folio(pages[ip]);
1044 			folio_zero_segment(folio, off, PAGE_SIZE);
1045 			flush_dcache_folio(folio);
1046 			folio_mark_uptodate(folio);
1047 		}
1048 
1049 		ni_lock(ni);
1050 		err = ni_write_frame(ni, pages, pages_per_frame);
1051 		ni_unlock(ni);
1052 
1053 		for (ip = 0; ip < pages_per_frame; ip++) {
1054 			folio = page_folio(pages[ip]);
1055 			folio_mark_uptodate(folio);
1056 			folio_unlock(folio);
1057 			folio_put(folio);
1058 		}
1059 
1060 		if (err)
1061 			goto out;
1062 
1063 		ni->i_valid = valid = frame_vbo + frame_size;
1064 	}
1065 
1066 	/* Copy user data [pos : pos + count). */
1067 	while (count) {
1068 		size_t copied, bytes;
1069 
1070 		off = pos & (frame_size - 1);
1071 		bytes = frame_size - off;
1072 		if (bytes > count)
1073 			bytes = count;
1074 
1075 		frame_vbo = pos & ~(frame_size - 1);
1076 		index = frame_vbo >> PAGE_SHIFT;
1077 
1078 		if (unlikely(fault_in_iov_iter_readable(from, bytes))) {
1079 			err = -EFAULT;
1080 			goto out;
1081 		}
1082 
1083 		/* Load full frame. */
1084 		err = ntfs_get_frame_pages(mapping, index, pages,
1085 					   pages_per_frame, &frame_uptodate);
1086 		if (err)
1087 			goto out;
1088 
1089 		if (!frame_uptodate) {
1090 			loff_t to = pos + bytes;
1091 
1092 			if (off || (to < i_size && (to & (frame_size - 1)))) {
1093 				err = ni_read_frame(ni, frame_vbo, pages,
1094 						    pages_per_frame);
1095 				if (err) {
1096 					for (ip = 0; ip < pages_per_frame;
1097 					     ip++) {
1098 						folio = page_folio(pages[ip]);
1099 						folio_unlock(folio);
1100 						folio_put(folio);
1101 					}
1102 					goto out;
1103 				}
1104 			}
1105 		}
1106 
1107 		WARN_ON(!bytes);
1108 		copied = 0;
1109 		ip = off >> PAGE_SHIFT;
1110 		off = offset_in_page(pos);
1111 
1112 		/* Copy user data to pages. */
1113 		for (;;) {
1114 			size_t cp, tail = PAGE_SIZE - off;
1115 
1116 			folio = page_folio(pages[ip]);
1117 			cp = copy_folio_from_iter_atomic(folio, off,
1118 							min(tail, bytes), from);
1119 			flush_dcache_folio(folio);
1120 
1121 			copied += cp;
1122 			bytes -= cp;
1123 			if (!bytes || !cp)
1124 				break;
1125 
1126 			if (cp < tail) {
1127 				off += cp;
1128 			} else {
1129 				ip++;
1130 				off = 0;
1131 			}
1132 		}
1133 
1134 		ni_lock(ni);
1135 		err = ni_write_frame(ni, pages, pages_per_frame);
1136 		ni_unlock(ni);
1137 
1138 		for (ip = 0; ip < pages_per_frame; ip++) {
1139 			folio = page_folio(pages[ip]);
1140 			folio_clear_dirty(folio);
1141 			folio_mark_uptodate(folio);
1142 			folio_unlock(folio);
1143 			folio_put(folio);
1144 		}
1145 
1146 		if (err)
1147 			goto out;
1148 
1149 		/*
1150 		 * We can loop for a long time in here. Be nice and allow
1151 		 * us to schedule out to avoid softlocking if preempt
1152 		 * is disabled.
1153 		 */
1154 		cond_resched();
1155 
1156 		pos += copied;
1157 		written += copied;
1158 
1159 		count = iov_iter_count(from);
1160 	}
1161 
1162 out:
1163 	kfree(pages);
1164 
1165 	if (err < 0)
1166 		return err;
1167 
1168 	iocb->ki_pos += written;
1169 	if (iocb->ki_pos > ni->i_valid)
1170 		ni->i_valid = iocb->ki_pos;
1171 	if (iocb->ki_pos > i_size)
1172 		i_size_write(inode, iocb->ki_pos);
1173 
1174 	return written;
1175 }
1176 
1177 /*
1178  * check_write_restriction:
1179  * common code for ntfs_file_write_iter and ntfs_file_splice_write
1180  */
check_write_restriction(struct inode * inode)1181 static int check_write_restriction(struct inode *inode)
1182 {
1183 	struct ntfs_inode *ni = ntfs_i(inode);
1184 
1185 	/* Avoid any operation if inode is bad. */
1186 	if (unlikely(is_bad_ni(ni)))
1187 		return -EINVAL;
1188 
1189 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1190 		return -EIO;
1191 
1192 	if (is_encrypted(ni)) {
1193 		ntfs_inode_warn(inode, "encrypted i/o not supported");
1194 		return -EOPNOTSUPP;
1195 	}
1196 
1197 	if (is_dedup(ni)) {
1198 		ntfs_inode_warn(inode, "write into deduplicated not supported");
1199 		return -EOPNOTSUPP;
1200 	}
1201 
1202 	return 0;
1203 }
1204 
1205 /*
1206  * ntfs_file_write_iter - file_operations::write_iter
1207  */
ntfs_file_write_iter(struct kiocb * iocb,struct iov_iter * from)1208 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
1209 {
1210 	struct file *file = iocb->ki_filp;
1211 	struct inode *inode = file_inode(file);
1212 	struct ntfs_inode *ni = ntfs_i(inode);
1213 	ssize_t ret;
1214 	int err;
1215 
1216 	if (!inode_trylock(inode)) {
1217 		if (iocb->ki_flags & IOCB_NOWAIT)
1218 			return -EAGAIN;
1219 		inode_lock(inode);
1220 	}
1221 
1222 	ret = check_write_restriction(inode);
1223 	if (ret)
1224 		goto out;
1225 
1226 	if (is_compressed(ni) && (iocb->ki_flags & IOCB_DIRECT)) {
1227 		ntfs_inode_warn(inode, "direct i/o + compressed not supported");
1228 		ret = -EOPNOTSUPP;
1229 		goto out;
1230 	}
1231 
1232 	ret = generic_write_checks(iocb, from);
1233 	if (ret <= 0)
1234 		goto out;
1235 
1236 	err = file_modified(iocb->ki_filp);
1237 	if (err) {
1238 		ret = err;
1239 		goto out;
1240 	}
1241 
1242 	if (WARN_ON(ni->ni_flags & NI_FLAG_COMPRESSED_MASK)) {
1243 		/* Should never be here, see ntfs_file_open(). */
1244 		ret = -EOPNOTSUPP;
1245 		goto out;
1246 	}
1247 
1248 	ret = ntfs_extend(inode, iocb->ki_pos, ret, file);
1249 	if (ret)
1250 		goto out;
1251 
1252 	ret = is_compressed(ni) ? ntfs_compress_write(iocb, from) :
1253 				  __generic_file_write_iter(iocb, from);
1254 
1255 out:
1256 	inode_unlock(inode);
1257 
1258 	if (ret > 0)
1259 		ret = generic_write_sync(iocb, ret);
1260 
1261 	return ret;
1262 }
1263 
1264 /*
1265  * ntfs_file_open - file_operations::open
1266  */
ntfs_file_open(struct inode * inode,struct file * file)1267 int ntfs_file_open(struct inode *inode, struct file *file)
1268 {
1269 	struct ntfs_inode *ni = ntfs_i(inode);
1270 
1271 	/* Avoid any operation if inode is bad. */
1272 	if (unlikely(is_bad_ni(ni)))
1273 		return -EINVAL;
1274 
1275 	if (unlikely(ntfs3_forced_shutdown(inode->i_sb)))
1276 		return -EIO;
1277 
1278 	if (unlikely((is_compressed(ni) || is_encrypted(ni)) &&
1279 		     (file->f_flags & O_DIRECT))) {
1280 		return -EOPNOTSUPP;
1281 	}
1282 
1283 	/* Decompress "external compressed" file if opened for rw. */
1284 	if ((ni->ni_flags & NI_FLAG_COMPRESSED_MASK) &&
1285 	    (file->f_flags & (O_WRONLY | O_RDWR | O_TRUNC))) {
1286 #ifdef CONFIG_NTFS3_LZX_XPRESS
1287 		int err = ni_decompress_file(ni);
1288 
1289 		if (err)
1290 			return err;
1291 #else
1292 		ntfs_inode_warn(
1293 			inode,
1294 			"activate CONFIG_NTFS3_LZX_XPRESS to write external compressed files");
1295 		return -EOPNOTSUPP;
1296 #endif
1297 	}
1298 
1299 	return generic_file_open(inode, file);
1300 }
1301 
1302 /*
1303  * ntfs_file_release - file_operations::release
1304  */
ntfs_file_release(struct inode * inode,struct file * file)1305 static int ntfs_file_release(struct inode *inode, struct file *file)
1306 {
1307 	struct ntfs_inode *ni = ntfs_i(inode);
1308 	struct ntfs_sb_info *sbi = ni->mi.sbi;
1309 	int err = 0;
1310 
1311 	/* If we are last writer on the inode, drop the block reservation. */
1312 	if (sbi->options->prealloc &&
1313 	    ((file->f_mode & FMODE_WRITE) &&
1314 	     atomic_read(&inode->i_writecount) == 1)
1315 	   /*
1316 	    * The only file when inode->i_fop = &ntfs_file_operations and
1317 	    * init_rwsem(&ni->file.run_lock) is not called explicitly is MFT.
1318 	    *
1319 	    * Add additional check here.
1320 	    */
1321 	    && inode->i_ino != MFT_REC_MFT) {
1322 		ni_lock(ni);
1323 		down_write(&ni->file.run_lock);
1324 
1325 		err = attr_set_size(ni, ATTR_DATA, NULL, 0, &ni->file.run,
1326 				    i_size_read(inode), &ni->i_valid, false,
1327 				    NULL);
1328 
1329 		up_write(&ni->file.run_lock);
1330 		ni_unlock(ni);
1331 	}
1332 	return err;
1333 }
1334 
1335 /*
1336  * ntfs_fiemap - inode_operations::fiemap
1337  */
ntfs_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,__u64 start,__u64 len)1338 int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
1339 		__u64 start, __u64 len)
1340 {
1341 	int err;
1342 	struct ntfs_inode *ni = ntfs_i(inode);
1343 
1344 	/* Avoid any operation if inode is bad. */
1345 	if (unlikely(is_bad_ni(ni)))
1346 		return -EINVAL;
1347 
1348 	err = fiemap_prep(inode, fieinfo, start, &len, ~FIEMAP_FLAG_XATTR);
1349 	if (err)
1350 		return err;
1351 
1352 	ni_lock(ni);
1353 
1354 	err = ni_fiemap(ni, fieinfo, start, len);
1355 
1356 	ni_unlock(ni);
1357 
1358 	return err;
1359 }
1360 
1361 /*
1362  * ntfs_file_splice_write - file_operations::splice_write
1363  */
ntfs_file_splice_write(struct pipe_inode_info * pipe,struct file * file,loff_t * ppos,size_t len,unsigned int flags)1364 static ssize_t ntfs_file_splice_write(struct pipe_inode_info *pipe,
1365 				      struct file *file, loff_t *ppos,
1366 				      size_t len, unsigned int flags)
1367 {
1368 	ssize_t err;
1369 	struct inode *inode = file_inode(file);
1370 
1371 	err = check_write_restriction(inode);
1372 	if (err)
1373 		return err;
1374 
1375 	return iter_file_splice_write(pipe, file, ppos, len, flags);
1376 }
1377 
1378 // clang-format off
1379 const struct inode_operations ntfs_file_inode_operations = {
1380 	.getattr	= ntfs_getattr,
1381 	.setattr	= ntfs_setattr,
1382 	.listxattr	= ntfs_listxattr,
1383 	.get_acl	= ntfs_get_acl,
1384 	.set_acl	= ntfs_set_acl,
1385 	.fiemap		= ntfs_fiemap,
1386 };
1387 
1388 const struct file_operations ntfs_file_operations = {
1389 	.llseek		= generic_file_llseek,
1390 	.read_iter	= ntfs_file_read_iter,
1391 	.write_iter	= ntfs_file_write_iter,
1392 	.unlocked_ioctl = ntfs_ioctl,
1393 #ifdef CONFIG_COMPAT
1394 	.compat_ioctl	= ntfs_compat_ioctl,
1395 #endif
1396 	.splice_read	= ntfs_file_splice_read,
1397 	.splice_write	= ntfs_file_splice_write,
1398 	.mmap_prepare	= ntfs_file_mmap_prepare,
1399 	.open		= ntfs_file_open,
1400 	.fsync		= generic_file_fsync,
1401 	.fallocate	= ntfs_fallocate,
1402 	.release	= ntfs_file_release,
1403 };
1404 
1405 #if IS_ENABLED(CONFIG_NTFS_FS)
1406 const struct file_operations ntfs_legacy_file_operations = {
1407 	.llseek		= generic_file_llseek,
1408 	.read_iter	= ntfs_file_read_iter,
1409 	.splice_read	= ntfs_file_splice_read,
1410 	.open		= ntfs_file_open,
1411 	.release	= ntfs_file_release,
1412 };
1413 #endif
1414 // clang-format on
1415