xref: /linux/fs/ntfs/file.c (revision cdd4dc3aebeab43a72ce0bc2b5bab6f0a80b97a5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * NTFS kernel file operations.
4  *
5  * Copyright (c) 2001-2015 Anton Altaparmakov and Tuxera Inc.
6  * Copyright (c) 2025 LG Electronics Co., Ltd.
7  */
8 
9 #include <linux/writeback.h>
10 #include <linux/blkdev.h>
11 #include <linux/fs.h>
12 #include <linux/iomap.h>
13 #include <linux/uio.h>
14 #include <linux/posix_acl.h>
15 #include <linux/posix_acl_xattr.h>
16 #include <linux/compat.h>
17 #include <linux/falloc.h>
18 
19 #include "lcnalloc.h"
20 #include "ntfs.h"
21 #include "reparse.h"
22 #include "ea.h"
23 #include "iomap.h"
24 #include "bitmap.h"
25 
26 #include <linux/filelock.h>
27 
28 /*
29  * ntfs_file_open - called when an inode is about to be opened
30  * @vi:		inode to be opened
31  * @filp:	file structure describing the inode
32  *
33  * Limit file size to the page cache limit on architectures where unsigned long
34  * is 32-bits. This is the most we can do for now without overflowing the page
35  * cache page index. Doing it this way means we don't run into problems because
36  * of existing too large files. It would be better to allow the user to read
37  * the beginning of the file but I doubt very much anyone is going to hit this
38  * check on a 32-bit architecture, so there is no point in adding the extra
39  * complexity required to support this.
40  *
41  * On 64-bit architectures, the check is hopefully optimized away by the
42  * compiler.
43  *
44  * After the check passes, just call generic_file_open() to do its work.
45  */
46 static int ntfs_file_open(struct inode *vi, struct file *filp)
47 {
48 	struct ntfs_inode *ni = NTFS_I(vi);
49 
50 	if (NVolShutdown(ni->vol))
51 		return -EIO;
52 
53 	if (sizeof(unsigned long) < 8) {
54 		if (i_size_read(vi) > MAX_LFS_FILESIZE)
55 			return -EOVERFLOW;
56 	}
57 
58 	filp->f_mode |= FMODE_NOWAIT | FMODE_CAN_ODIRECT;
59 
60 	return generic_file_open(vi, filp);
61 }
62 
63 /*
64  * Trim preallocated space on file release.
65  *
66  * When the preallo_size mount option is set (default 64KB), writes extend
67  * allocated_size and runlist in units of preallocated size to reduce
68  * runlist merge overhead for small writes. This can leave
69  * allocated_size > data_size if not all preallocated space is used.
70  *
71  * We perform the trim here because ->release() is called only when
72  * the file is no longer open. At this point, no further writes can occur,
73  * so it is safe to reclaim the unused preallocated space.
74  *
75  * Returns 0 on success, or negative error on failure.
76  */
77 static int ntfs_trim_prealloc(struct inode *vi)
78 {
79 	struct ntfs_inode *ni = NTFS_I(vi);
80 	struct ntfs_volume *vol = ni->vol;
81 	struct runlist_element *rl;
82 	s64 aligned_data_size;
83 	s64 vcn_ds, vcn_tr;
84 	ssize_t rc;
85 	int err = 0;
86 
87 	inode_lock(vi);
88 	mutex_lock(&ni->mrec_lock);
89 	down_write(&ni->runlist.lock);
90 
91 	aligned_data_size = round_up(ni->data_size, vol->cluster_size);
92 	if (aligned_data_size >= ni->allocated_size)
93 		goto out_unlock;
94 
95 	vcn_ds = ntfs_bytes_to_cluster(vol, aligned_data_size);
96 	vcn_tr = -1;
97 	rc = ni->runlist.count - 2;
98 	rl = ni->runlist.rl;
99 
100 	while (rc >= 0 && rl[rc].lcn == LCN_HOLE && vcn_ds <= rl[rc].vcn) {
101 		vcn_tr = rl[rc].vcn;
102 		rc--;
103 	}
104 
105 	if (vcn_tr >= 0) {
106 		err = ntfs_rl_truncate_nolock(vol, &ni->runlist, vcn_tr);
107 		if (err) {
108 			kvfree(ni->runlist.rl);
109 			ni->runlist.rl = NULL;
110 			ntfs_error(vol->sb, "Preallocated block rollback failed");
111 		} else {
112 			ni->allocated_size = ntfs_cluster_to_bytes(vol, vcn_tr);
113 			err = ntfs_attr_update_mapping_pairs(ni, 0);
114 			if (err)
115 				ntfs_error(vol->sb,
116 					   "Failed to rollback mapping pairs for prealloc");
117 		}
118 	}
119 
120 out_unlock:
121 	up_write(&ni->runlist.lock);
122 	mutex_unlock(&ni->mrec_lock);
123 	inode_unlock(vi);
124 
125 	return err;
126 }
127 
128 static int ntfs_file_release(struct inode *vi, struct file *filp)
129 {
130 	if (!NInoCompressed(NTFS_I(vi)))
131 		return ntfs_trim_prealloc(vi);
132 
133 	return 0;
134 }
135 
136 /*
137  * ntfs_file_fsync - sync a file to disk
138  * @filp:	file to be synced
139  * @start:	start offset to be synced
140  * @end:	end offset to be synced
141  * @datasync:	if non-zero only flush user data and not metadata
142  *
143  * Data integrity sync of a file to disk.  Used for fsync, fdatasync, and msync
144  * system calls.  This function is inspired by fs/buffer.c::file_fsync().
145  *
146  * If @datasync is false, write the mft record and all associated extent mft
147  * records as well as the $DATA attribute and then sync the block device.
148  *
149  * If @datasync is true and the attribute is non-resident, we skip the writing
150  * of the mft record and all associated extent mft records (this might still
151  * happen due to the write_inode_now() call).
152  *
153  * Also, if @datasync is true, we do not wait on the inode to be written out
154  * but we always wait on the page cache pages to be written out.
155  */
156 static int ntfs_file_fsync(struct file *filp, loff_t start, loff_t end,
157 			   int datasync)
158 {
159 	struct inode *vi = filp->f_mapping->host;
160 	struct ntfs_inode *ni = NTFS_I(vi);
161 	struct ntfs_volume *vol = ni->vol;
162 	int err, ret = 0;
163 	struct inode *parent_vi, *ia_vi;
164 	struct ntfs_attr_search_ctx *ctx;
165 
166 	ntfs_debug("Entering for inode 0x%llx.", ni->mft_no);
167 
168 	if (NVolShutdown(vol))
169 		return -EIO;
170 
171 	err = file_write_and_wait_range(filp, start, end);
172 	if (err)
173 		return err;
174 
175 	if (!datasync || !NInoNonResident(NTFS_I(vi)))
176 		ret = __ntfs_write_inode(vi, 1);
177 	write_inode_now(vi, !datasync);
178 
179 	ctx = ntfs_attr_get_search_ctx(ni, NULL);
180 	if (!ctx)
181 		return -ENOMEM;
182 
183 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL_CHILD);
184 	while (!(err = ntfs_attr_lookup(AT_UNUSED, NULL, 0, 0, 0, NULL, 0, ctx))) {
185 		if (ctx->attr->type == AT_FILE_NAME) {
186 			struct file_name_attr *fn = (struct file_name_attr *)((u8 *)ctx->attr +
187 					le16_to_cpu(ctx->attr->data.resident.value_offset));
188 
189 			parent_vi = ntfs_iget(vi->i_sb, MREF_LE(fn->parent_directory));
190 			if (IS_ERR(parent_vi))
191 				continue;
192 			mutex_lock_nested(&NTFS_I(parent_vi)->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
193 			ia_vi = ntfs_index_iget(parent_vi, I30, 4);
194 			mutex_unlock(&NTFS_I(parent_vi)->mrec_lock);
195 			if (IS_ERR(ia_vi)) {
196 				iput(parent_vi);
197 				continue;
198 			}
199 			write_inode_now(ia_vi, 1);
200 			iput(ia_vi);
201 			write_inode_now(parent_vi, 1);
202 			iput(parent_vi);
203 		} else if (ctx->attr->non_resident) {
204 			struct inode *attr_vi;
205 			__le16 *name;
206 
207 			name = (__le16 *)((u8 *)ctx->attr + le16_to_cpu(ctx->attr->name_offset));
208 			if (ctx->attr->type == AT_DATA && ctx->attr->name_length == 0)
209 				continue;
210 
211 			attr_vi = ntfs_attr_iget(vi, ctx->attr->type,
212 						 name, ctx->attr->name_length);
213 			if (IS_ERR(attr_vi))
214 				continue;
215 			spin_lock(&attr_vi->i_lock);
216 			if (inode_state_read_once(attr_vi) & I_DIRTY_PAGES) {
217 				spin_unlock(&attr_vi->i_lock);
218 				filemap_write_and_wait(attr_vi->i_mapping);
219 			} else
220 				spin_unlock(&attr_vi->i_lock);
221 			iput(attr_vi);
222 		}
223 	}
224 	mutex_unlock(&ni->mrec_lock);
225 	ntfs_attr_put_search_ctx(ctx);
226 
227 	write_inode_now(vol->mftbmp_ino, 1);
228 	down_write(&vol->lcnbmp_lock);
229 	write_inode_now(vol->lcnbmp_ino, 1);
230 	up_write(&vol->lcnbmp_lock);
231 	write_inode_now(vol->mft_ino, 1);
232 
233 	/*
234 	 * NOTE: If we were to use mapping->private_list (see ext2 and
235 	 * fs/buffer.c) for dirty blocks then we could optimize the below to be
236 	 * sync_mapping_buffers(vi->i_mapping).
237 	 */
238 	err = sync_blockdev(vi->i_sb->s_bdev);
239 	if (unlikely(err && !ret))
240 		ret = err;
241 	if (likely(!ret))
242 		ntfs_debug("Done.");
243 	else
244 		ntfs_warning(vi->i_sb,
245 				"Failed to f%ssync inode 0x%llx.  Error %u.",
246 				datasync ? "data" : "", ni->mft_no, -ret);
247 	if (!ret)
248 		blkdev_issue_flush(vi->i_sb->s_bdev);
249 	return ret;
250 }
251 
252 static int ntfs_setattr_size(struct inode *vi, struct iattr *attr)
253 {
254 	struct ntfs_inode *ni = NTFS_I(vi);
255 	int err;
256 	loff_t old_size = vi->i_size;
257 
258 	if (NInoCompressed(ni) || NInoEncrypted(ni)) {
259 		ntfs_warning(vi->i_sb,
260 			"Changes in inode size are not supported yet for %s files, ignoring.",
261 			NInoCompressed(ni) ? "compressed" : "encrypted");
262 		return -EOPNOTSUPP;
263 	}
264 
265 	err = inode_newsize_ok(vi, attr->ia_size);
266 	if (err)
267 		return err;
268 
269 	inode_dio_wait(vi);
270 	/* Serialize against page faults */
271 	if (NInoNonResident(NTFS_I(vi)) && attr->ia_size < old_size) {
272 		err = iomap_truncate_page(vi, attr->ia_size, NULL,
273 				&ntfs_read_iomap_ops,
274 				&ntfs_iomap_folio_ops, NULL);
275 		if (err)
276 			return err;
277 	}
278 
279 	truncate_setsize(vi, attr->ia_size);
280 	err = ntfs_truncate_vfs(vi, attr->ia_size, old_size);
281 	if (err) {
282 		i_size_write(vi, old_size);
283 		return err;
284 	}
285 
286 	if (NInoNonResident(ni) && attr->ia_size > old_size &&
287 	    old_size % PAGE_SIZE != 0) {
288 		loff_t len = min_t(loff_t,
289 				round_up(old_size, PAGE_SIZE) - old_size,
290 				attr->ia_size - old_size);
291 		err = iomap_zero_range(vi, old_size, len,
292 				NULL, &ntfs_seek_iomap_ops,
293 				&ntfs_iomap_folio_ops, NULL);
294 	}
295 
296 	return err;
297 }
298 
299 /*
300  * ntfs_setattr
301  *
302  * Called from notify_change() when an attribute is being changed.
303  *
304  * NOTE: Changes in inode size are not supported yet for compressed or
305  * encrypted files.
306  */
307 int ntfs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
308 		 struct iattr *attr)
309 {
310 	struct inode *vi = d_inode(dentry);
311 	int err;
312 	unsigned int ia_valid = attr->ia_valid;
313 	struct ntfs_inode *ni = NTFS_I(vi);
314 	struct ntfs_volume *vol = ni->vol;
315 
316 	if (NVolShutdown(vol))
317 		return -EIO;
318 
319 	err = setattr_prepare(idmap, dentry, attr);
320 	if (err)
321 		goto out;
322 
323 	if (!(vol->vol_flags & VOLUME_IS_DIRTY))
324 		ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
325 
326 	if (ia_valid & ATTR_SIZE) {
327 		err = ntfs_setattr_size(vi, attr);
328 		if (err)
329 			goto out;
330 
331 		ia_valid |= ATTR_MTIME | ATTR_CTIME;
332 	}
333 
334 	setattr_copy(idmap, vi, attr);
335 
336 	if (vol->sb->s_flags & SB_POSIXACL && !S_ISLNK(vi->i_mode)) {
337 		err = posix_acl_chmod(idmap, dentry, vi->i_mode);
338 		if (err)
339 			goto out;
340 	}
341 
342 	if (0222 & vi->i_mode)
343 		ni->flags &= ~FILE_ATTR_READONLY;
344 	else
345 		ni->flags |= FILE_ATTR_READONLY;
346 
347 	if (ia_valid & (ATTR_UID | ATTR_GID | ATTR_MODE)) {
348 		unsigned int flags = 0;
349 
350 		if (ia_valid & ATTR_UID)
351 			flags |= NTFS_EA_UID;
352 		if (ia_valid & ATTR_GID)
353 			flags |= NTFS_EA_GID;
354 		if (ia_valid & ATTR_MODE)
355 			flags |= NTFS_EA_MODE;
356 
357 		if (S_ISDIR(vi->i_mode))
358 			vi->i_mode &= ~vol->dmask;
359 		else
360 			vi->i_mode &= ~vol->fmask;
361 
362 		mutex_lock(&ni->mrec_lock);
363 		ntfs_ea_set_wsl_inode(vi, 0, NULL, flags);
364 		mutex_unlock(&ni->mrec_lock);
365 	}
366 
367 	mark_inode_dirty(vi);
368 out:
369 	return err;
370 }
371 
372 int ntfs_getattr(struct mnt_idmap *idmap, const struct path *path,
373 		struct kstat *stat, unsigned int request_mask,
374 		unsigned int query_flags)
375 {
376 	struct inode *inode = d_backing_inode(path->dentry);
377 	struct ntfs_inode *ni = NTFS_I(inode);
378 
379 	generic_fillattr(idmap, request_mask, inode, stat);
380 
381 	stat->blksize = NTFS_SB(inode->i_sb)->cluster_size;
382 	stat->blocks = (((u64)NTFS_I(inode)->i_dealloc_clusters <<
383 			NTFS_SB(inode->i_sb)->cluster_size_bits) >> 9) + inode->i_blocks;
384 	stat->result_mask |= STATX_BTIME;
385 	stat->btime = NTFS_I(inode)->i_crtime;
386 
387 	if (NInoCompressed(ni))
388 		stat->attributes |= STATX_ATTR_COMPRESSED;
389 
390 	if (NInoEncrypted(ni))
391 		stat->attributes |= STATX_ATTR_ENCRYPTED;
392 
393 	if (inode->i_flags & S_IMMUTABLE)
394 		stat->attributes |= STATX_ATTR_IMMUTABLE;
395 
396 	if (inode->i_flags & S_APPEND)
397 		stat->attributes |= STATX_ATTR_APPEND;
398 
399 	stat->attributes_mask |= STATX_ATTR_COMPRESSED | STATX_ATTR_ENCRYPTED |
400 				 STATX_ATTR_IMMUTABLE | STATX_ATTR_APPEND;
401 
402 	/*
403 	 * If it's a compressed or encrypted file, NTFS currently
404 	 * does not support DIO. For normal files, we report the bdev
405 	 * logical block size.
406 	 */
407 	if (request_mask & STATX_DIOALIGN && S_ISREG(inode->i_mode)) {
408 		unsigned int align =
409 			bdev_logical_block_size(inode->i_sb->s_bdev);
410 
411 		stat->result_mask |= STATX_DIOALIGN;
412 		if (!NInoCompressed(ni) && !NInoEncrypted(ni)) {
413 			stat->dio_mem_align = align;
414 			stat->dio_offset_align = align;
415 		}
416 	}
417 
418 	return 0;
419 }
420 
421 static loff_t ntfs_file_llseek(struct file *file, loff_t offset, int whence)
422 {
423 	struct inode *inode = file->f_mapping->host;
424 
425 	switch (whence) {
426 	case SEEK_HOLE:
427 		inode_lock_shared(inode);
428 		offset = iomap_seek_hole(inode, offset, &ntfs_seek_iomap_ops);
429 		inode_unlock_shared(inode);
430 		break;
431 	case SEEK_DATA:
432 		inode_lock_shared(inode);
433 		offset = iomap_seek_data(inode, offset, &ntfs_seek_iomap_ops);
434 		inode_unlock_shared(inode);
435 		break;
436 	default:
437 		return generic_file_llseek_size(file, offset, whence,
438 						inode->i_sb->s_maxbytes,
439 						i_size_read(inode));
440 	}
441 	if (offset < 0)
442 		return offset;
443 	return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
444 }
445 
446 static ssize_t ntfs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
447 {
448 	struct inode *vi = file_inode(iocb->ki_filp);
449 	struct super_block *sb = vi->i_sb;
450 	ssize_t ret;
451 
452 	if (NVolShutdown(NTFS_SB(sb)))
453 		return -EIO;
454 
455 	if (NInoCompressed(NTFS_I(vi)) && iocb->ki_flags & IOCB_DIRECT)
456 		return -EOPNOTSUPP;
457 
458 	inode_lock_shared(vi);
459 
460 	if (iocb->ki_flags & IOCB_DIRECT) {
461 		size_t count = iov_iter_count(to);
462 
463 		if ((iocb->ki_pos | count) & (sb->s_blocksize - 1)) {
464 			ret = -EINVAL;
465 			goto inode_unlock;
466 		}
467 
468 		file_accessed(iocb->ki_filp);
469 		ret = iomap_dio_rw(iocb, to, &ntfs_read_iomap_ops, NULL, 0,
470 				NULL, 0);
471 	} else {
472 		ret = generic_file_read_iter(iocb, to);
473 	}
474 
475 inode_unlock:
476 	inode_unlock_shared(vi);
477 
478 	return ret;
479 }
480 
481 static int ntfs_file_write_dio_end_io(struct kiocb *iocb, ssize_t size,
482 		int error, unsigned int flags)
483 {
484 	struct inode *inode = file_inode(iocb->ki_filp);
485 
486 	if (error)
487 		return error;
488 
489 	if (size) {
490 		if (i_size_read(inode) < iocb->ki_pos + size) {
491 			i_size_write(inode, iocb->ki_pos + size);
492 			mark_inode_dirty(inode);
493 		}
494 	}
495 
496 	return 0;
497 }
498 
499 static const struct iomap_dio_ops ntfs_write_dio_ops = {
500 	.end_io			= ntfs_file_write_dio_end_io,
501 };
502 
503 static ssize_t ntfs_dio_write_iter(struct kiocb *iocb, struct iov_iter *from)
504 {
505 	ssize_t ret;
506 
507 	ret = iomap_dio_rw(iocb, from, &ntfs_dio_iomap_ops,
508 			&ntfs_write_dio_ops, 0, NULL, 0);
509 	if (ret == -ENOTBLK)
510 		ret = 0;
511 	else if (ret < 0)
512 		goto out;
513 
514 	if (iov_iter_count(from)) {
515 		loff_t offset, end;
516 		ssize_t written;
517 		int ret2;
518 
519 		offset = iocb->ki_pos;
520 		iocb->ki_flags &= ~IOCB_DIRECT;
521 		written = iomap_file_buffered_write(iocb, from,
522 				&ntfs_write_iomap_ops, &ntfs_iomap_folio_ops,
523 				NULL);
524 		if (written < 0) {
525 			ret = written;
526 			goto out;
527 		}
528 
529 		ret += written;
530 		end = iocb->ki_pos + written - 1;
531 		ret2 = filemap_write_and_wait_range(iocb->ki_filp->f_mapping,
532 				offset, end);
533 		if (ret2) {
534 			ret = -EIO;
535 			goto out;
536 		}
537 		if (!ret2)
538 			invalidate_mapping_pages(iocb->ki_filp->f_mapping,
539 						 offset >> PAGE_SHIFT,
540 						 end >> PAGE_SHIFT);
541 	}
542 
543 out:
544 	return ret;
545 }
546 
547 static ssize_t ntfs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
548 {
549 	struct file *file = iocb->ki_filp;
550 	struct inode *vi = file->f_mapping->host;
551 	struct ntfs_inode *ni = NTFS_I(vi);
552 	struct ntfs_volume *vol = ni->vol;
553 	ssize_t ret;
554 	ssize_t count;
555 	loff_t pos;
556 	int err;
557 	loff_t old_data_size, old_init_size;
558 
559 	if (NVolShutdown(vol))
560 		return -EIO;
561 
562 	if (NInoEncrypted(ni)) {
563 		ntfs_error(vi->i_sb, "Writing for %s files is not supported yet",
564 			   NInoCompressed(ni) ? "Compressed" : "Encrypted");
565 		return -EOPNOTSUPP;
566 	}
567 
568 	if (NInoCompressed(ni) && iocb->ki_flags & IOCB_DIRECT)
569 		return -EOPNOTSUPP;
570 
571 	if (iocb->ki_flags & IOCB_NOWAIT) {
572 		if (!inode_trylock(vi))
573 			return -EAGAIN;
574 	} else
575 		inode_lock(vi);
576 
577 	ret = generic_write_checks(iocb, from);
578 	if (ret <= 0)
579 		goto out_lock;
580 
581 	err = file_modified(iocb->ki_filp);
582 	if (err) {
583 		ret = err;
584 		goto out_lock;
585 	}
586 
587 	if (!(vol->vol_flags & VOLUME_IS_DIRTY))
588 		ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
589 
590 	pos = iocb->ki_pos;
591 	count = ret;
592 
593 	old_data_size = ni->data_size;
594 	old_init_size = ni->initialized_size;
595 
596 	if (NInoNonResident(ni) && NInoCompressed(ni)) {
597 		ret = ntfs_compress_write(ni, pos, count, from);
598 		if (ret > 0)
599 			iocb->ki_pos += ret;
600 		goto out;
601 	}
602 
603 	if (NInoNonResident(ni) && iocb->ki_flags & IOCB_DIRECT)
604 		ret = ntfs_dio_write_iter(iocb, from);
605 	else
606 		ret = iomap_file_buffered_write(iocb, from, &ntfs_write_iomap_ops,
607 				&ntfs_iomap_folio_ops, NULL);
608 out:
609 	if (ret < 0 && ret != -EIOCBQUEUED) {
610 		if (ni->initialized_size != old_init_size) {
611 			mutex_lock(&ni->mrec_lock);
612 			ntfs_attr_set_initialized_size(ni, old_init_size);
613 			mutex_unlock(&ni->mrec_lock);
614 		}
615 		if (ni->data_size != old_data_size) {
616 			truncate_setsize(vi, old_data_size);
617 			ntfs_attr_truncate(ni, old_data_size);
618 		}
619 	}
620 out_lock:
621 	inode_unlock(vi);
622 	if (ret > 0)
623 		ret = generic_write_sync(iocb, ret);
624 	return ret;
625 }
626 
627 static vm_fault_t ntfs_filemap_page_mkwrite(struct vm_fault *vmf)
628 {
629 	struct inode *inode = file_inode(vmf->vma->vm_file);
630 	vm_fault_t ret;
631 
632 	sb_start_pagefault(inode->i_sb);
633 	file_update_time(vmf->vma->vm_file);
634 
635 	ret = iomap_page_mkwrite(vmf, &ntfs_page_mkwrite_iomap_ops, NULL);
636 	sb_end_pagefault(inode->i_sb);
637 	return ret;
638 }
639 
640 static const struct vm_operations_struct ntfs_file_vm_ops = {
641 	.fault		= filemap_fault,
642 	.map_pages	= filemap_map_pages,
643 	.page_mkwrite	= ntfs_filemap_page_mkwrite,
644 };
645 
646 static int ntfs_file_mmap_prepare(struct vm_area_desc *desc)
647 {
648 	struct file *file = desc->file;
649 	struct inode *inode = file_inode(file);
650 
651 	if (NVolShutdown(NTFS_SB(file->f_mapping->host->i_sb)))
652 		return -EIO;
653 
654 	if (NInoCompressed(NTFS_I(inode)))
655 		return -EOPNOTSUPP;
656 
657 	if (vma_desc_test(desc, VMA_WRITE_BIT)) {
658 		struct inode *inode = file_inode(file);
659 		loff_t from, to;
660 		int err;
661 
662 		from = ((loff_t)desc->pgoff << PAGE_SHIFT);
663 		to = min_t(loff_t, i_size_read(inode),
664 			   from + desc->end - desc->start);
665 
666 		if (NTFS_I(inode)->initialized_size < to) {
667 			err = ntfs_extend_initialized_size(inode, to, to, false);
668 			if (err)
669 				return err;
670 		}
671 	}
672 
673 
674 	file_accessed(file);
675 	desc->vm_ops = &ntfs_file_vm_ops;
676 	return 0;
677 }
678 
679 static int ntfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
680 		u64 start, u64 len)
681 {
682 	return iomap_fiemap(inode, fieinfo, start, len, &ntfs_read_iomap_ops);
683 }
684 
685 static const char *ntfs_get_link(struct dentry *dentry, struct inode *inode,
686 		struct delayed_call *done)
687 {
688 	if (!NTFS_I(inode)->target)
689 		return ERR_PTR(-EINVAL);
690 
691 	return NTFS_I(inode)->target;
692 }
693 
694 static ssize_t ntfs_file_splice_read(struct file *in, loff_t *ppos,
695 		struct pipe_inode_info *pipe, size_t len, unsigned int flags)
696 {
697 	if (NVolShutdown(NTFS_SB(in->f_mapping->host->i_sb)))
698 		return -EIO;
699 
700 	return filemap_splice_read(in, ppos, pipe, len, flags);
701 }
702 
703 static int ntfs_ioctl_shutdown(struct super_block *sb, unsigned long arg)
704 {
705 	u32 flags;
706 
707 	if (!capable(CAP_SYS_ADMIN))
708 		return -EPERM;
709 
710 	if (get_user(flags, (__u32 __user *)arg))
711 		return -EFAULT;
712 
713 	return ntfs_force_shutdown(sb, flags);
714 }
715 
716 static int ntfs_ioctl_get_volume_label(struct file *filp, unsigned long arg)
717 {
718 	struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb);
719 	char __user *buf = (char __user *)arg;
720 
721 	if (!vol->volume_label) {
722 		if (copy_to_user(buf, "", 1))
723 			return -EFAULT;
724 	} else if (copy_to_user(buf, vol->volume_label,
725 				MIN(FSLABEL_MAX, strlen(vol->volume_label) + 1)))
726 		return -EFAULT;
727 	return 0;
728 }
729 
730 static int ntfs_ioctl_set_volume_label(struct file *filp, unsigned long arg)
731 {
732 	struct ntfs_volume *vol = NTFS_SB(file_inode(filp)->i_sb);
733 	char *label;
734 	int ret;
735 
736 	if (!capable(CAP_SYS_ADMIN))
737 		return -EPERM;
738 
739 	label = strndup_user((const char __user *)arg, FSLABEL_MAX);
740 	if (IS_ERR(label))
741 		return PTR_ERR(label);
742 
743 	ret = mnt_want_write_file(filp);
744 	if (ret)
745 		goto out;
746 
747 	ret = ntfs_write_volume_label(vol, label);
748 	mnt_drop_write_file(filp);
749 out:
750 	kfree(label);
751 	return ret;
752 }
753 
754 static int ntfs_ioctl_fitrim(struct ntfs_volume *vol, unsigned long arg)
755 {
756 	struct fstrim_range __user *user_range;
757 	struct fstrim_range range;
758 	struct block_device *dev;
759 	int err;
760 
761 	if (!capable(CAP_SYS_ADMIN))
762 		return -EPERM;
763 
764 	dev = vol->sb->s_bdev;
765 	if (!bdev_max_discard_sectors(dev))
766 		return -EOPNOTSUPP;
767 
768 	user_range = (struct fstrim_range __user *)arg;
769 	if (copy_from_user(&range, user_range, sizeof(range)))
770 		return -EFAULT;
771 
772 	if (range.len == 0)
773 		return -EINVAL;
774 
775 	if (range.len < vol->cluster_size)
776 		return -EINVAL;
777 
778 	range.minlen = max_t(u32, range.minlen, bdev_discard_granularity(dev));
779 
780 	err = ntfs_trim_fs(vol, &range);
781 	if (err < 0)
782 		return err;
783 
784 	if (copy_to_user(user_range, &range, sizeof(range)))
785 		return -EFAULT;
786 
787 	return 0;
788 }
789 
790 long ntfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
791 {
792 	switch (cmd) {
793 	case FS_IOC_SHUTDOWN:
794 		return ntfs_ioctl_shutdown(file_inode(filp)->i_sb, arg);
795 	case FS_IOC_GETFSLABEL:
796 		return ntfs_ioctl_get_volume_label(filp, arg);
797 	case FS_IOC_SETFSLABEL:
798 		return ntfs_ioctl_set_volume_label(filp, arg);
799 	case FITRIM:
800 		return ntfs_ioctl_fitrim(NTFS_SB(file_inode(filp)->i_sb), arg);
801 	default:
802 		return -ENOTTY;
803 	}
804 }
805 
806 #ifdef CONFIG_COMPAT
807 long ntfs_compat_ioctl(struct file *filp, unsigned int cmd,
808 		unsigned long arg)
809 {
810 	return ntfs_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
811 }
812 #endif
813 
814 static int ntfs_allocate_range(struct ntfs_inode *ni, int mode, loff_t offset,
815 		loff_t len)
816 {
817 	struct inode *vi = VFS_I(ni);
818 	struct ntfs_volume *vol = ni->vol;
819 	s64 need_space;
820 	loff_t old_size, new_size;
821 	s64 start_vcn, end_vcn;
822 	int err;
823 
824 	old_size = i_size_read(vi);
825 	new_size = max_t(loff_t, old_size, offset + len);
826 	start_vcn = ntfs_bytes_to_cluster(vol, offset);
827 	end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1;
828 
829 	err = inode_newsize_ok(vi, new_size);
830 	if (err)
831 		goto out;
832 
833 	need_space = ntfs_bytes_to_cluster(vol, ni->allocated_size);
834 	if (need_space > start_vcn)
835 		need_space = end_vcn - need_space;
836 	else
837 		need_space = end_vcn - start_vcn;
838 	if (need_space > 0 &&
839 	    need_space > (atomic64_read(&vol->free_clusters) -
840 			  atomic64_read(&vol->dirty_clusters))) {
841 		err = -ENOSPC;
842 		goto out;
843 	}
844 
845 	err = ntfs_attr_fallocate(ni, offset, len,
846 			mode & FALLOC_FL_KEEP_SIZE ? true : false);
847 
848 	if (!(mode & FALLOC_FL_KEEP_SIZE) && new_size != old_size)
849 		i_size_write(vi, ni->data_size);
850 out:
851 	return err;
852 }
853 
854 static int ntfs_punch_hole(struct ntfs_inode *ni, int mode, loff_t offset,
855 		loff_t len)
856 {
857 	struct ntfs_volume *vol = ni->vol;
858 	struct inode *vi = VFS_I(ni);
859 	loff_t end_offset;
860 	s64 start_vcn, end_vcn;
861 	int err = 0;
862 
863 	loff_t offset_down = round_down(offset, max_t(unsigned int,
864 				vol->cluster_size, PAGE_SIZE));
865 
866 	if (NVolDisableSparse(vol)) {
867 		err = -EOPNOTSUPP;
868 		goto out;
869 	}
870 
871 	if (offset >= ni->data_size)
872 		goto out;
873 
874 	if (offset + len > ni->data_size)
875 		end_offset = ni->data_size;
876 	else
877 		end_offset = offset + len;
878 
879 	err = filemap_write_and_wait_range(vi->i_mapping, offset_down, LLONG_MAX);
880 	if (err)
881 		goto out;
882 	truncate_pagecache(vi, offset_down);
883 
884 	start_vcn = ntfs_bytes_to_cluster(vol, offset);
885 	end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1;
886 
887 	if (offset & vol->cluster_size_mask) {
888 		loff_t to;
889 
890 		to = min_t(loff_t, ntfs_cluster_to_bytes(vol, start_vcn + 1),
891 				end_offset);
892 		err = iomap_zero_range(vi, offset, to - offset, NULL,
893 				&ntfs_seek_iomap_ops,
894 				&ntfs_iomap_folio_ops, NULL);
895 		if (err < 0 || (end_vcn - start_vcn) == 1)
896 			goto out;
897 		start_vcn++;
898 	}
899 
900 	if (end_offset & vol->cluster_size_mask) {
901 		loff_t from;
902 
903 		from = ntfs_cluster_to_bytes(vol, end_vcn - 1);
904 		err = iomap_zero_range(vi, from, end_offset - from, NULL,
905 				&ntfs_seek_iomap_ops,
906 				&ntfs_iomap_folio_ops, NULL);
907 		if (err < 0 || (end_vcn - start_vcn) == 1)
908 			goto out;
909 		end_vcn--;
910 	}
911 
912 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
913 	err = ntfs_non_resident_attr_punch_hole(ni, start_vcn,
914 			end_vcn - start_vcn);
915 	mutex_unlock(&ni->mrec_lock);
916 out:
917 	return err;
918 }
919 
920 static int ntfs_collapse_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
921 {
922 	struct ntfs_volume *vol = ni->vol;
923 	struct inode *vi = VFS_I(ni);
924 	loff_t old_size, new_size;
925 	s64 start_vcn, end_vcn;
926 	int err;
927 
928 	loff_t offset_down = round_down(offset,
929 			max_t(unsigned long, vol->cluster_size, PAGE_SIZE));
930 
931 	if ((offset & vol->cluster_size_mask) ||
932 	    (len & vol->cluster_size_mask) ||
933 	    offset >= ni->allocated_size) {
934 		err = -EINVAL;
935 		goto out;
936 	}
937 
938 	old_size = i_size_read(vi);
939 	start_vcn = ntfs_bytes_to_cluster(vol, offset);
940 	end_vcn = ntfs_bytes_to_cluster(vol, offset + len - 1) + 1;
941 
942 	if (ntfs_cluster_to_bytes(vol, end_vcn) > ni->allocated_size)
943 		end_vcn = (round_up(ni->allocated_size - 1,
944 			   vol->cluster_size) >> vol->cluster_size_bits) + 1;
945 	new_size = old_size - ntfs_cluster_to_bytes(vol, end_vcn - start_vcn);
946 	if (new_size < 0)
947 		new_size = 0;
948 	err = filemap_write_and_wait_range(vi->i_mapping,
949 			offset_down, LLONG_MAX);
950 	if (err)
951 		goto out;
952 
953 	truncate_pagecache(vi, offset_down);
954 
955 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
956 	err = ntfs_non_resident_attr_collapse_range(ni, start_vcn,
957 			end_vcn - start_vcn);
958 	mutex_unlock(&ni->mrec_lock);
959 
960 	if (new_size != old_size)
961 		i_size_write(vi, ni->data_size);
962 out:
963 	return err;
964 }
965 
966 static int ntfs_insert_range(struct ntfs_inode *ni, loff_t offset, loff_t len)
967 {
968 	struct ntfs_volume *vol = ni->vol;
969 	struct inode *vi = VFS_I(ni);
970 	loff_t offset_down = round_down(offset,
971 			max_t(unsigned long, vol->cluster_size, PAGE_SIZE));
972 	loff_t alloc_size, end_offset = offset + len;
973 	loff_t old_size, new_size;
974 	s64 start_vcn, end_vcn;
975 	int err;
976 
977 	if (NVolDisableSparse(vol)) {
978 		err = -EOPNOTSUPP;
979 		goto out;
980 	}
981 
982 	if ((offset & vol->cluster_size_mask) ||
983 	    (len & vol->cluster_size_mask) ||
984 	     offset >= ni->allocated_size) {
985 		err = -EINVAL;
986 		goto out;
987 	}
988 
989 	old_size = i_size_read(vi);
990 	start_vcn = ntfs_bytes_to_cluster(vol, offset);
991 	end_vcn = ntfs_bytes_to_cluster(vol, end_offset - 1) + 1;
992 
993 	new_size = old_size + ntfs_cluster_to_bytes(vol, end_vcn - start_vcn);
994 	alloc_size = ni->allocated_size +
995 		ntfs_cluster_to_bytes(vol, end_vcn - start_vcn);
996 	if (alloc_size < 0) {
997 		err = -EFBIG;
998 		goto out;
999 	}
1000 	err = inode_newsize_ok(vi, alloc_size);
1001 	if (err)
1002 		goto out;
1003 
1004 	err = filemap_write_and_wait_range(vi->i_mapping,
1005 			offset_down, LLONG_MAX);
1006 	if (err)
1007 		goto out;
1008 
1009 	truncate_pagecache(vi, offset_down);
1010 
1011 	mutex_lock_nested(&ni->mrec_lock, NTFS_INODE_MUTEX_NORMAL);
1012 	err = ntfs_non_resident_attr_insert_range(ni, start_vcn,
1013 			end_vcn - start_vcn);
1014 	mutex_unlock(&ni->mrec_lock);
1015 
1016 	if (new_size != old_size)
1017 		i_size_write(vi, ni->data_size);
1018 out:
1019 	return err;
1020 }
1021 
1022 #define NTFS_FALLOC_FL_SUPPORTED					\
1023 		(FALLOC_FL_ALLOCATE_RANGE | FALLOC_FL_KEEP_SIZE |	\
1024 		 FALLOC_FL_INSERT_RANGE | FALLOC_FL_PUNCH_HOLE |	\
1025 		 FALLOC_FL_COLLAPSE_RANGE)
1026 
1027 static long ntfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
1028 {
1029 	struct inode *vi = file_inode(file);
1030 	struct ntfs_inode *ni = NTFS_I(vi);
1031 	struct ntfs_volume *vol = ni->vol;
1032 	int err = 0;
1033 	loff_t old_size;
1034 	bool map_locked = false;
1035 
1036 	if (mode & ~(NTFS_FALLOC_FL_SUPPORTED))
1037 		return -EOPNOTSUPP;
1038 
1039 	if (!NVolFreeClusterKnown(vol))
1040 		wait_event(vol->free_waitq, NVolFreeClusterKnown(vol));
1041 
1042 	if ((ni->vol->mft_zone_end - ni->vol->mft_zone_start) == 0)
1043 		return -ENOSPC;
1044 
1045 	if (NInoNonResident(ni) && !NInoFullyMapped(ni)) {
1046 		down_write(&ni->runlist.lock);
1047 		err = ntfs_attr_map_whole_runlist(ni);
1048 		up_write(&ni->runlist.lock);
1049 		if (err)
1050 			return err;
1051 	}
1052 
1053 	if (!(vol->vol_flags & VOLUME_IS_DIRTY)) {
1054 		err = ntfs_set_volume_flags(vol, VOLUME_IS_DIRTY);
1055 		if (err)
1056 			return err;
1057 	}
1058 
1059 	old_size = i_size_read(vi);
1060 
1061 	inode_lock(vi);
1062 	if (NInoCompressed(ni) || NInoEncrypted(ni)) {
1063 		err = -EOPNOTSUPP;
1064 		goto out;
1065 	}
1066 
1067 	inode_dio_wait(vi);
1068 	if (mode & (FALLOC_FL_PUNCH_HOLE | FALLOC_FL_COLLAPSE_RANGE |
1069 		    FALLOC_FL_INSERT_RANGE)) {
1070 		filemap_invalidate_lock(vi->i_mapping);
1071 		map_locked = true;
1072 	}
1073 
1074 	switch (mode & FALLOC_FL_MODE_MASK) {
1075 	case FALLOC_FL_ALLOCATE_RANGE:
1076 	case FALLOC_FL_KEEP_SIZE:
1077 		err = ntfs_allocate_range(ni, mode, offset, len);
1078 		break;
1079 	case FALLOC_FL_PUNCH_HOLE:
1080 		err = ntfs_punch_hole(ni, mode, offset, len);
1081 		break;
1082 	case FALLOC_FL_COLLAPSE_RANGE:
1083 		err = ntfs_collapse_range(ni, offset, len);
1084 		break;
1085 	case FALLOC_FL_INSERT_RANGE:
1086 		err = ntfs_insert_range(ni, offset, len);
1087 		break;
1088 	default:
1089 		err = -EOPNOTSUPP;
1090 	}
1091 
1092 	if (err)
1093 		goto out;
1094 
1095 	err = file_modified(file);
1096 out:
1097 	if (map_locked)
1098 		filemap_invalidate_unlock(vi->i_mapping);
1099 	if (!err) {
1100 		if (mode == 0 && NInoNonResident(ni) &&
1101 		    offset > old_size && old_size % PAGE_SIZE != 0) {
1102 			loff_t len = min_t(loff_t,
1103 					   round_up(old_size, PAGE_SIZE) - old_size,
1104 					   offset - old_size);
1105 			err = iomap_zero_range(vi, old_size, len, NULL,
1106 					       &ntfs_seek_iomap_ops,
1107 					       &ntfs_iomap_folio_ops, NULL);
1108 		}
1109 		NInoSetFileNameDirty(ni);
1110 		inode_set_mtime_to_ts(vi, inode_set_ctime_current(vi));
1111 		mark_inode_dirty(vi);
1112 	}
1113 
1114 	inode_unlock(vi);
1115 	return err;
1116 }
1117 
1118 const struct file_operations ntfs_file_ops = {
1119 	.llseek		= ntfs_file_llseek,
1120 	.read_iter	= ntfs_file_read_iter,
1121 	.write_iter	= ntfs_file_write_iter,
1122 	.fsync		= ntfs_file_fsync,
1123 	.mmap_prepare	= ntfs_file_mmap_prepare,
1124 	.open		= ntfs_file_open,
1125 	.release	= ntfs_file_release,
1126 	.splice_read	= ntfs_file_splice_read,
1127 	.splice_write	= iter_file_splice_write,
1128 	.unlocked_ioctl	= ntfs_ioctl,
1129 #ifdef CONFIG_COMPAT
1130 	.compat_ioctl	= ntfs_compat_ioctl,
1131 #endif
1132 	.fallocate	= ntfs_fallocate,
1133 	.setlease	= generic_setlease,
1134 };
1135 
1136 const struct inode_operations ntfs_file_inode_ops = {
1137 	.setattr	= ntfs_setattr,
1138 	.getattr	= ntfs_getattr,
1139 	.listxattr	= ntfs_listxattr,
1140 	.get_acl	= ntfs_get_acl,
1141 	.set_acl	= ntfs_set_acl,
1142 	.fiemap		= ntfs_fiemap,
1143 };
1144 
1145 const struct inode_operations ntfs_symlink_inode_operations = {
1146 	.get_link	= ntfs_get_link,
1147 	.setattr	= ntfs_setattr,
1148 	.listxattr	= ntfs_listxattr,
1149 };
1150 
1151 const struct inode_operations ntfs_special_inode_operations = {
1152 	.setattr	= ntfs_setattr,
1153 	.getattr	= ntfs_getattr,
1154 	.listxattr	= ntfs_listxattr,
1155 	.get_acl	= ntfs_get_acl,
1156 	.set_acl	= ntfs_set_acl,
1157 };
1158 
1159 const struct file_operations ntfs_empty_file_ops = {};
1160 
1161 const struct inode_operations ntfs_empty_inode_ops = {};
1162