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