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