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