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