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