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