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