xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zpl_file.c (revision 2f10ffc003be396f3fc23cd2888023896560252b)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * This file and its contents are supplied under the terms of the
4  * Common Development and Distribution License ("CDDL"), version 1.0.
5  * You may only use this file in accordance with the terms of version
6  * 1.0 of the CDDL.
7  *
8  * A full copy of the text of the CDDL should have accompanied this
9  * source.  A copy of the CDDL is also available via the Internet at
10  * https://opensource.org/license/CDDL-1.0.
11  */
12 /*
13  * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
14  * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
15  * Copyright (c) 2025, Klara, Inc.
16  * Copyright (c) 2025, Rob Norris <robn@despairlabs.com>
17  */
18 
19 
20 #ifdef CONFIG_COMPAT
21 #include <linux/compat.h>
22 #endif
23 #include <linux/fs.h>
24 #include <linux/migrate.h>
25 #include <sys/file.h>
26 #include <sys/dmu_objset.h>
27 #include <sys/zfs_znode.h>
28 #include <sys/zfs_vfsops.h>
29 #include <sys/zfs_vnops.h>
30 #include <sys/zfs_project.h>
31 #include <linux/pagemap_compat.h>
32 #include <linux/fadvise.h>
33 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
34 #include <linux/writeback.h>
35 #endif
36 #ifdef HAVE_FILELOCK_HEADER
37 #include <linux/filelock.h>
38 #endif
39 
40 /*
41  * Per-open-file state, hung off file->private_data.  Allocated lazily the
42  * first time a Direct I/O read on this handle hits a benign checksum verify
43  * failure -- a recycled O_DIRECT buffer whose buffered re-read then succeeded.
44  * Its presence makes zpl_iter_read route subsequent reads through the uncached
45  * buffered path for the remaining lifetime of the handle, which stops the
46  * verify-failure / re-read storm without disabling the verify itself (so
47  * mirror/raidz self-heal for genuine corruption is unaffected).
48  */
49 typedef struct zpl_file_data {
50 	boolean_t	zfd_dio_read_declined;
51 } zpl_file_data_t;
52 
53 static void
zpl_dio_read_decline(struct file * filp)54 zpl_dio_read_decline(struct file *filp)
55 {
56 	if (atomic_load_ptr(&filp->private_data) != NULL)
57 		return;
58 
59 	zpl_file_data_t *zfd = kmem_zalloc(sizeof (*zfd), KM_SLEEP);
60 	zfd->zfd_dio_read_declined = B_TRUE;
61 	if (atomic_cas_ptr(&filp->private_data, NULL, zfd) != NULL)
62 		kmem_free(zfd, sizeof (*zfd));
63 }
64 
65 /*
66  * When using fallocate(2) to preallocate space, inflate the requested
67  * capacity check by 10% to account for the required metadata blocks.
68  */
69 static unsigned int zfs_fallocate_reserve_percent = 110;
70 
71 static int
zpl_open(struct inode * ip,struct file * filp)72 zpl_open(struct inode *ip, struct file *filp)
73 {
74 	cred_t *cr = CRED();
75 	int error;
76 	fstrans_cookie_t cookie;
77 
78 	error = generic_file_open(ip, filp);
79 	if (error)
80 		return (error);
81 
82 	crhold(cr);
83 	cookie = spl_fstrans_mark();
84 	error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr);
85 	spl_fstrans_unmark(cookie);
86 	crfree(cr);
87 	ASSERT3S(error, <=, 0);
88 
89 	return (error);
90 }
91 
92 static int
zpl_release(struct inode * ip,struct file * filp)93 zpl_release(struct inode *ip, struct file *filp)
94 {
95 	cred_t *cr = CRED();
96 	int error;
97 	fstrans_cookie_t cookie;
98 
99 	cookie = spl_fstrans_mark();
100 	if (ITOZ(ip)->z_atime_dirty)
101 		zfs_mark_inode_dirty(ip);
102 
103 	crhold(cr);
104 	error = -zfs_close(ip, filp->f_flags, cr);
105 	spl_fstrans_unmark(cookie);
106 	crfree(cr);
107 	ASSERT3S(error, <=, 0);
108 
109 	zpl_file_data_t *zfd = filp->private_data;
110 	if (zfd != NULL) {
111 		filp->private_data = NULL;
112 		kmem_free(zfd, sizeof (*zfd));
113 	}
114 
115 	return (error);
116 }
117 
118 static int
zpl_iterate(struct file * filp,struct dir_context * ctx)119 zpl_iterate(struct file *filp, struct dir_context *ctx)
120 {
121 	cred_t *cr = CRED();
122 	int error;
123 	fstrans_cookie_t cookie;
124 
125 	crhold(cr);
126 	cookie = spl_fstrans_mark();
127 	error = -zfs_readdir(file_inode(filp), ctx, cr);
128 	spl_fstrans_unmark(cookie);
129 	crfree(cr);
130 	ASSERT3S(error, <=, 0);
131 
132 	return (error);
133 }
134 
135 static inline int
136 zpl_write_cache_pages(struct address_space *mapping,
137     struct writeback_control *wbc, void *data);
138 
139 static int
zpl_fsync(struct file * filp,loff_t start,loff_t end,int datasync)140 zpl_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
141 {
142 	struct inode *inode = filp->f_mapping->host;
143 	znode_t *zp = ITOZ(inode);
144 	cred_t *cr = CRED();
145 	int error;
146 	fstrans_cookie_t cookie;
147 
148 	/*
149 	 * Force dirty pages in the range out to the DMU and the log, ready
150 	 * for zil_commit() to write down.
151 	 *
152 	 * We call write_cache_pages() directly to ensure that zpl_putpage() is
153 	 * called with the flags we need. We need WB_SYNC_NONE to avoid a call
154 	 * to zil_commit() (since we're doing this as a kind of pre-sync); but
155 	 * we do need for_sync so that the pages remain in writeback until
156 	 * they're on disk, and so that we get an error if the DMU write fails.
157 	 */
158 	if (filemap_range_has_page(inode->i_mapping, start, end)) {
159 		int for_sync = 1;
160 		struct writeback_control wbc = {
161 			.sync_mode = WB_SYNC_NONE,
162 			.nr_to_write = LONG_MAX,
163 			.range_start = start,
164 			.range_end = end,
165 		};
166 		error =
167 		    zpl_write_cache_pages(inode->i_mapping, &wbc, &for_sync);
168 		if (error != 0) {
169 			/*
170 			 * Unclear what state things are in. zfs_putpage() will
171 			 * ensure the pages remain dirty if they haven't been
172 			 * written down to the DMU, but because there may be
173 			 * nothing logged, we can't assume that zfs_sync() ->
174 			 * zil_commit() will give us a useful error. It's
175 			 * safest if we just error out here.
176 			 */
177 			return (error);
178 		}
179 	}
180 
181 	crhold(cr);
182 	cookie = spl_fstrans_mark();
183 	error = -zfs_fsync(zp, datasync, cr);
184 	spl_fstrans_unmark(cookie);
185 	crfree(cr);
186 	ASSERT3S(error, <=, 0);
187 
188 	return (error);
189 }
190 
191 static inline int
zfs_io_flags(struct kiocb * kiocb)192 zfs_io_flags(struct kiocb *kiocb)
193 {
194 	int flags = 0;
195 
196 #if defined(IOCB_DSYNC)
197 	if (kiocb->ki_flags & IOCB_DSYNC)
198 		flags |= O_DSYNC;
199 #endif
200 #if defined(IOCB_SYNC)
201 	if (kiocb->ki_flags & IOCB_SYNC)
202 		flags |= O_SYNC;
203 #endif
204 #if defined(IOCB_APPEND)
205 	if (kiocb->ki_flags & IOCB_APPEND)
206 		flags |= O_APPEND;
207 #endif
208 #if defined(IOCB_DIRECT)
209 	if (kiocb->ki_flags & IOCB_DIRECT)
210 		flags |= O_DIRECT;
211 #endif
212 	return (flags);
213 }
214 
215 static inline uint16_t
zfs_uio_flags(struct kiocb * kiocb)216 zfs_uio_flags(struct kiocb *kiocb)
217 {
218 	uint16_t flags = 0;
219 
220 	/*
221 	 * Both RWF_DONTCACHE and POSIX_FADV_NOREUSE say the caller does not
222 	 * intend to read the data after this.
223 	 */
224 #if defined(IOCB_DONTCACHE)
225 	if (kiocb->ki_flags & IOCB_DONTCACHE)
226 		flags |= UIO_UNCACHED;
227 #endif
228 #if defined(FMODE_NOREUSE)
229 	if (kiocb->ki_filp->f_mode & FMODE_NOREUSE)
230 		flags |= UIO_UNCACHED;
231 #endif
232 	return (flags);
233 }
234 
235 /*
236  * If relatime is enabled, call file_accessed() if zfs_relatime_need_update()
237  * is true.  This is needed since datasets with inherited "relatime" property
238  * aren't necessarily mounted with the MNT_RELATIME flag (e.g. after
239  * `zfs set relatime=...`), which is what relatime test in VFS by
240  * relatime_need_update() is based on.
241  */
242 static inline void
zpl_file_accessed(struct file * filp)243 zpl_file_accessed(struct file *filp)
244 {
245 	struct inode *ip = filp->f_mapping->host;
246 
247 	if (!IS_NOATIME(ip) && ITOZSB(ip)->z_relatime) {
248 		if (zfs_relatime_need_update(ip))
249 			file_accessed(filp);
250 	} else {
251 		file_accessed(filp);
252 	}
253 }
254 
255 static ssize_t
zpl_iter_read(struct kiocb * kiocb,struct iov_iter * to)256 zpl_iter_read(struct kiocb *kiocb, struct iov_iter *to)
257 {
258 	cred_t *cr = CRED();
259 	fstrans_cookie_t cookie;
260 	struct file *filp = kiocb->ki_filp;
261 	ssize_t count = iov_iter_count(to);
262 	zfs_uio_t uio;
263 
264 	zfs_uio_iov_iter_init(&uio, to, kiocb->ki_pos, count);
265 	uio.uio_extflg |= zfs_uio_flags(kiocb);
266 
267 	/*
268 	 * This handle previously declined Direct I/O after a benign read
269 	 * verify failure; keep taking the uncached buffered path.
270 	 */
271 	zpl_file_data_t *zfd = atomic_load_ptr(&filp->private_data);
272 	if (zfd != NULL && zfd->zfd_dio_read_declined)
273 		uio.uio_extflg |= UIO_DIO_DENY;
274 
275 	crhold(cr);
276 	cookie = spl_fstrans_mark();
277 
278 	ssize_t ret = -zfs_read(ITOZ(filp->f_mapping->host), &uio,
279 	    filp->f_flags | zfs_io_flags(kiocb), cr);
280 
281 	spl_fstrans_unmark(cookie);
282 	crfree(cr);
283 
284 	/*
285 	 * A Direct I/O read verify failed benignly (recycled O_DIRECT buffer)
286 	 * and the buffered re-read succeeded; decline Direct I/O reads on this
287 	 * handle from here on.
288 	 */
289 	if (uio.uio_extflg & UIO_DIO_CKSUM_RETRIED)
290 		zpl_dio_read_decline(filp);
291 
292 	if (ret < 0)
293 		return (ret);
294 
295 	ssize_t read = count - uio.uio_resid;
296 	kiocb->ki_pos += read;
297 
298 	zpl_file_accessed(filp);
299 
300 	return (read);
301 }
302 
303 static inline ssize_t
zpl_generic_write_checks(struct kiocb * kiocb,struct iov_iter * from,size_t * countp)304 zpl_generic_write_checks(struct kiocb *kiocb, struct iov_iter *from,
305     size_t *countp)
306 {
307 	ssize_t ret = generic_write_checks(kiocb, from);
308 	if (ret <= 0)
309 		return (ret);
310 
311 	*countp = ret;
312 
313 	return (0);
314 }
315 
316 static ssize_t
zpl_iter_write(struct kiocb * kiocb,struct iov_iter * from)317 zpl_iter_write(struct kiocb *kiocb, struct iov_iter *from)
318 {
319 	cred_t *cr = CRED();
320 	fstrans_cookie_t cookie;
321 	struct file *filp = kiocb->ki_filp;
322 	struct inode *ip = filp->f_mapping->host;
323 	zfs_uio_t uio;
324 	size_t count = 0;
325 	ssize_t ret;
326 
327 	ret = zpl_generic_write_checks(kiocb, from, &count);
328 	if (ret)
329 		return (ret);
330 
331 	zfs_uio_iov_iter_init(&uio, from, kiocb->ki_pos, count);
332 	uio.uio_extflg |= zfs_uio_flags(kiocb);
333 
334 	crhold(cr);
335 	cookie = spl_fstrans_mark();
336 
337 	ret = -zfs_write(ITOZ(ip), &uio,
338 	    filp->f_flags | zfs_io_flags(kiocb), cr);
339 
340 	spl_fstrans_unmark(cookie);
341 	crfree(cr);
342 
343 	if (ret < 0)
344 		return (ret);
345 
346 	ssize_t wrote = count - uio.uio_resid;
347 	kiocb->ki_pos += wrote;
348 
349 	return (wrote);
350 }
351 
352 static ssize_t
zpl_direct_IO(struct kiocb * kiocb,struct iov_iter * iter)353 zpl_direct_IO(struct kiocb *kiocb, struct iov_iter *iter)
354 {
355 	/*
356 	 * All O_DIRECT requests should be handled by
357 	 * zpl_iter_write/read}(). There is no way kernel generic code should
358 	 * call the direct_IO address_space_operations function. We set this
359 	 * code path to be fatal if it is executed.
360 	 */
361 	PANIC(0);
362 	return (0);
363 }
364 
365 static loff_t
zpl_llseek(struct file * filp,loff_t offset,int whence)366 zpl_llseek(struct file *filp, loff_t offset, int whence)
367 {
368 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
369 	fstrans_cookie_t cookie;
370 
371 	if (whence == SEEK_DATA || whence == SEEK_HOLE) {
372 		struct inode *ip = filp->f_mapping->host;
373 		loff_t maxbytes = ip->i_sb->s_maxbytes;
374 		loff_t error;
375 
376 		spl_inode_lock_shared(ip);
377 		cookie = spl_fstrans_mark();
378 		error = -zfs_holey(ITOZ(ip), whence, &offset);
379 		spl_fstrans_unmark(cookie);
380 		if (error == 0)
381 			error = lseek_execute(filp, ip, offset, maxbytes);
382 		spl_inode_unlock_shared(ip);
383 
384 		return (error);
385 	}
386 #endif /* SEEK_HOLE && SEEK_DATA */
387 
388 	return (generic_file_llseek(filp, offset, whence));
389 }
390 
391 /*
392  * It's worth taking a moment to describe how mmap is implemented
393  * for zfs because it differs considerably from other Linux filesystems.
394  * However, this issue is handled the same way under OpenSolaris.
395  *
396  * The issue is that by design zfs bypasses the Linux page cache and
397  * leaves all caching up to the ARC.  This has been shown to work
398  * well for the common read(2)/write(2) case.  However, mmap(2)
399  * is problem because it relies on being tightly integrated with the
400  * page cache.  To handle this we cache mmap'ed files twice, once in
401  * the ARC and a second time in the page cache.  The code is careful
402  * to keep both copies synchronized.
403  *
404  * When a file with an mmap'ed region is written to using write(2)
405  * both the data in the ARC and existing pages in the page cache
406  * are updated.  For a read(2) data will be read first from the page
407  * cache then the ARC if needed.  Neither a write(2) or read(2) will
408  * will ever result in new pages being added to the page cache.
409  *
410  * New pages are added to the page cache only via .readpage() which
411  * is called when the vfs needs to read a page off disk to back the
412  * virtual memory region.  These pages may be modified without
413  * notifying the ARC and will be written out periodically via
414  * .writepage().  This will occur due to either a sync or the usual
415  * page aging behavior.  Note because a read(2) of a mmap'ed file
416  * will always check the page cache first even when the ARC is out
417  * of date correct data will still be returned.
418  *
419  * While this implementation ensures correct behavior it does have
420  * have some drawbacks.  The most obvious of which is that it
421  * increases the required memory footprint when access mmap'ed
422  * files.  It also adds additional complexity to the code keeping
423  * both caches synchronized.
424  *
425  * Longer term it may be possible to cleanly resolve this wart by
426  * mapping page cache pages directly on to the ARC buffers.  The
427  * Linux address space operations are flexible enough to allow
428  * selection of which pages back a particular index.  The trick
429  * would be working out the details of which subsystem is in
430  * charge, the ARC, the page cache, or both.  It may also prove
431  * helpful to move the ARC buffers to a scatter-gather lists
432  * rather than a vmalloc'ed region.
433  */
434 /*
435  * Bump z_seq when a clean page first transitions to dirty via an mmap store.
436  * The default generic_file_vm_ops.page_mkwrite (filemap_page_mkwrite) updates
437  * mtime/ctime via file_update_time -> __mark_inode_dirty, but never tells the
438  * filesystem that the change cookie should advance. Without this hook NFSv4
439  * GETATTR between an mmap store and writeback returns a stale change_cookie
440  * alongside the newer mtime, violating monotonicity. zfs_dirty_inode persists
441  * the new value on the same dirty path.
442  */
443 static vm_fault_t
zpl_page_mkwrite(struct vm_fault * vmf)444 zpl_page_mkwrite(struct vm_fault *vmf)
445 {
446 	znode_t *zp = ITOZ(file_inode(vmf->vma->vm_file));
447 
448 	atomic_inc_64(&zp->z_seq);
449 
450 	return (filemap_page_mkwrite(vmf));
451 }
452 
453 static const struct vm_operations_struct zpl_vm_ops = {
454 	.fault		= filemap_fault,
455 	.map_pages	= filemap_map_pages,
456 	.page_mkwrite	= zpl_page_mkwrite,
457 };
458 
459 static int
zpl_mmap(struct file * filp,struct vm_area_struct * vma)460 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
461 {
462 	struct inode *ip = filp->f_mapping->host;
463 	int error;
464 	fstrans_cookie_t cookie;
465 
466 	cookie = spl_fstrans_mark();
467 	error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start,
468 	    (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags);
469 	spl_fstrans_unmark(cookie);
470 
471 	if (error)
472 		return (error);
473 
474 	error = generic_file_mmap(filp, vma);
475 	if (error)
476 		return (error);
477 
478 	vma->vm_ops = &zpl_vm_ops;
479 	return (error);
480 }
481 
482 /*
483  * Populate a page with data for the Linux page cache.  This function is
484  * only used to support mmap(2).  There will be an identical copy of the
485  * data in the ARC which is kept up to date via .write() and .writepage().
486  */
487 static inline int
zpl_readpage_common(struct page * pp)488 zpl_readpage_common(struct page *pp)
489 {
490 	fstrans_cookie_t cookie;
491 
492 	ASSERT(PageLocked(pp));
493 
494 	cookie = spl_fstrans_mark();
495 	int error = -zfs_getpage(pp->mapping->host, pp);
496 	spl_fstrans_unmark(cookie);
497 
498 	unlock_page(pp);
499 
500 	return (error);
501 }
502 
503 #ifdef HAVE_VFS_READ_FOLIO
504 static int
zpl_read_folio(struct file * filp,struct folio * folio)505 zpl_read_folio(struct file *filp, struct folio *folio)
506 {
507 	return (zpl_readpage_common(&folio->page));
508 }
509 #else
510 static int
zpl_readpage(struct file * filp,struct page * pp)511 zpl_readpage(struct file *filp, struct page *pp)
512 {
513 	return (zpl_readpage_common(pp));
514 }
515 #endif
516 
517 static int
zpl_readpage_filler(void * data,struct page * pp)518 zpl_readpage_filler(void *data, struct page *pp)
519 {
520 	return (zpl_readpage_common(pp));
521 }
522 
523 /*
524  * Populate a set of pages with data for the Linux page cache.  This
525  * function will only be called for read ahead and never for demand
526  * paging.  For simplicity, the code relies on read_cache_pages() to
527  * correctly lock each page for IO and call zpl_readpage().
528  */
529 #ifdef HAVE_VFS_READPAGES
530 static int
zpl_readpages(struct file * filp,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)531 zpl_readpages(struct file *filp, struct address_space *mapping,
532     struct list_head *pages, unsigned nr_pages)
533 {
534 	return (read_cache_pages(mapping, pages, zpl_readpage_filler, NULL));
535 }
536 #else
537 static void
zpl_readahead(struct readahead_control * ractl)538 zpl_readahead(struct readahead_control *ractl)
539 {
540 	struct page *page;
541 
542 	while ((page = readahead_page(ractl)) != NULL) {
543 		int ret;
544 
545 		ret = zpl_readpage_filler(NULL, page);
546 		put_page(page);
547 		if (ret)
548 			break;
549 	}
550 }
551 #endif
552 
553 static int
zpl_putpage(struct page * pp,struct writeback_control * wbc,void * data)554 zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data)
555 {
556 	boolean_t *for_sync = data;
557 	fstrans_cookie_t cookie;
558 	int ret;
559 
560 	ASSERT(PageLocked(pp));
561 	ASSERT(!PageWriteback(pp));
562 
563 	cookie = spl_fstrans_mark();
564 	ret = zfs_putpage(pp->mapping->host, pp, wbc, *for_sync);
565 	spl_fstrans_unmark(cookie);
566 
567 	return (ret);
568 }
569 
570 #ifdef HAVE_WRITE_CACHE_PAGES
571 #ifdef HAVE_WRITEPAGE_T_FOLIO
572 static int
zpl_putfolio(struct folio * pp,struct writeback_control * wbc,void * data)573 zpl_putfolio(struct folio *pp, struct writeback_control *wbc, void *data)
574 {
575 	return (zpl_putpage(&pp->page, wbc, data));
576 }
577 #endif
578 
579 static inline int
zpl_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,void * data)580 zpl_write_cache_pages(struct address_space *mapping,
581     struct writeback_control *wbc, void *data)
582 {
583 	int result;
584 
585 #ifdef HAVE_WRITEPAGE_T_FOLIO
586 	result = write_cache_pages(mapping, wbc, zpl_putfolio, data);
587 #else
588 	result = write_cache_pages(mapping, wbc, zpl_putpage, data);
589 #endif
590 	return (result);
591 }
592 #else
593 static inline int
zpl_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,void * data)594 zpl_write_cache_pages(struct address_space *mapping,
595     struct writeback_control *wbc, void *data)
596 {
597 	pgoff_t start = wbc->range_start >> PAGE_SHIFT;
598 	pgoff_t end = wbc->range_end >> PAGE_SHIFT;
599 
600 	struct folio_batch fbatch;
601 	folio_batch_init(&fbatch);
602 
603 	/*
604 	 * This atomically (-ish) tags all DIRTY pages in the range with
605 	 * TOWRITE, allowing users to continue dirtying or undirtying pages
606 	 * while we get on with writeback, without us treading on each other.
607 	 */
608 	tag_pages_for_writeback(mapping, start, end);
609 
610 	int err = 0;
611 	unsigned int npages;
612 
613 	/*
614 	 * Grab references to the TOWRITE pages just flagged. This may not get
615 	 * all of them, so we do it in a loop until there are none left.
616 	 */
617 	while ((npages = filemap_get_folios_tag(mapping, &start, end,
618 	    PAGECACHE_TAG_TOWRITE, &fbatch)) != 0) {
619 
620 		/* Loop over each page and write it out. */
621 		struct folio *folio;
622 		while ((folio = folio_batch_next(&fbatch)) != NULL) {
623 			folio_lock(folio);
624 
625 			/*
626 			 * If the folio has been remapped, or is no longer
627 			 * dirty, then there's nothing to do.
628 			 */
629 			if (folio->mapping != mapping ||
630 			    !folio_test_dirty(folio)) {
631 				folio_unlock(folio);
632 				continue;
633 			}
634 
635 			/*
636 			 * If writeback is already in progress, wait for it to
637 			 * finish. We continue after this even if the page
638 			 * ends up clean; zfs_putpage() will skip it if no
639 			 * further work is required.
640 			 */
641 			while (folio_test_writeback(folio))
642 				folio_wait_bit(folio, PG_writeback);
643 
644 			/*
645 			 * Write it out and collect any error. zfs_putpage()
646 			 * will clear the TOWRITE and DIRTY flags, and return
647 			 * with the page unlocked.
648 			 */
649 			int ferr = zpl_putpage(&folio->page, wbc, data);
650 			if (err == 0 && ferr != 0)
651 				err = ferr;
652 
653 			/* Housekeeping for the caller. */
654 			wbc->nr_to_write -= folio_nr_pages(folio);
655 		}
656 
657 		/* Release any remaining references on the batch. */
658 		folio_batch_release(&fbatch);
659 	}
660 
661 	return (err);
662 }
663 #endif
664 
665 static int
zpl_writepages(struct address_space * mapping,struct writeback_control * wbc)666 zpl_writepages(struct address_space *mapping, struct writeback_control *wbc)
667 {
668 	znode_t		*zp = ITOZ(mapping->host);
669 	zfsvfs_t	*zfsvfs = ITOZSB(mapping->host);
670 	enum writeback_sync_modes sync_mode;
671 	int result;
672 
673 	if ((result = zpl_enter(zfsvfs, FTAG)) != 0)
674 		return (result);
675 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
676 		wbc->sync_mode = WB_SYNC_ALL;
677 	zpl_exit(zfsvfs, FTAG);
678 	sync_mode = wbc->sync_mode;
679 
680 	/*
681 	 * We don't want to run write_cache_pages() in SYNC mode here, because
682 	 * that would make putpage() wait for a single page to be committed to
683 	 * disk every single time, resulting in atrocious performance. Instead
684 	 * we run it once in non-SYNC mode so that the ZIL gets all the data,
685 	 * and then we commit it all in one go.
686 	 */
687 	boolean_t for_sync = (sync_mode == WB_SYNC_ALL);
688 	wbc->sync_mode = WB_SYNC_NONE;
689 	result = zpl_write_cache_pages(mapping, wbc, &for_sync);
690 	if (sync_mode != wbc->sync_mode) {
691 		if ((result = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
692 			return (result);
693 
694 		if (zfsvfs->z_log != NULL) {
695 			/*
696 			 * We don't want to block here if the pool suspends,
697 			 * because this is not a syncing op by itself, but
698 			 * might be part of one that the caller will
699 			 * coordinate.
700 			 */
701 			result = -zil_commit_flags(zfsvfs->z_log, zp->z_id,
702 			    ZIL_COMMIT_NOW);
703 		}
704 
705 		zpl_exit(zfsvfs, FTAG);
706 
707 		/*
708 		 * If zil_commit_flags() failed, it's unclear what state things
709 		 * are currently in. putpage() has written back out what it can
710 		 * to the DMU, but it may not be on disk. We have little choice
711 		 * but to escape.
712 		 */
713 		if (result != 0)
714 			return (result);
715 
716 		/*
717 		 * We need to call write_cache_pages() again (we can't just
718 		 * return after the commit) because the previous call in
719 		 * non-SYNC mode does not guarantee that we got all the dirty
720 		 * pages (see the implementation of write_cache_pages() for
721 		 * details). That being said, this is a no-op in most cases.
722 		 */
723 		wbc->sync_mode = sync_mode;
724 		result = zpl_write_cache_pages(mapping, wbc, &for_sync);
725 	}
726 	return (result);
727 }
728 
729 #ifdef HAVE_VFS_WRITEPAGE
730 /*
731  * Write out dirty pages to the ARC, this function is only required to
732  * support mmap(2).  Mapped pages may be dirtied by memory operations
733  * which never call .write().  These dirty pages are kept in sync with
734  * the ARC buffers via this hook.
735  */
736 static int
zpl_writepage(struct page * pp,struct writeback_control * wbc)737 zpl_writepage(struct page *pp, struct writeback_control *wbc)
738 {
739 	if (ITOZSB(pp->mapping->host)->z_os->os_sync == ZFS_SYNC_ALWAYS)
740 		wbc->sync_mode = WB_SYNC_ALL;
741 
742 	boolean_t for_sync = (wbc->sync_mode == WB_SYNC_ALL);
743 
744 	return (zpl_putpage(pp, wbc, &for_sync));
745 }
746 #endif
747 
748 /*
749  * The flag combination which matches the behavior of zfs_space() is
750  * FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE.  The FALLOC_FL_PUNCH_HOLE
751  * flag was introduced in the 2.6.38 kernel.
752  *
753  * The original mode=0 (allocate space) behavior can be reasonably emulated
754  * by checking if enough space exists and creating a sparse file, as real
755  * persistent space reservation is not possible due to COW, snapshots, etc.
756  */
757 static long
zpl_fallocate_common(struct inode * ip,int mode,loff_t offset,loff_t len)758 zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len)
759 {
760 	cred_t *cr = CRED();
761 	znode_t *zp = ITOZ(ip);
762 	zfsvfs_t *zfsvfs = ITOZSB(ip);
763 	loff_t olen;
764 	fstrans_cookie_t cookie;
765 	int error = 0;
766 
767 	int test_mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE;
768 
769 	if ((mode & ~(FALLOC_FL_KEEP_SIZE | test_mode)) != 0)
770 		return (-EOPNOTSUPP);
771 
772 	if (offset < 0 || len <= 0)
773 		return (-EINVAL);
774 
775 	spl_inode_lock(ip);
776 	olen = i_size_read(ip);
777 
778 	crhold(cr);
779 	cookie = spl_fstrans_mark();
780 	if (mode & (test_mode)) {
781 		flock64_t bf;
782 
783 		if (mode & FALLOC_FL_KEEP_SIZE) {
784 			if (offset > olen)
785 				goto out_unmark;
786 
787 			if (offset + len > olen)
788 				len = olen - offset;
789 		}
790 		bf.l_type = F_WRLCK;
791 		bf.l_whence = SEEK_SET;
792 		bf.l_start = offset;
793 		bf.l_len = len;
794 		bf.l_pid = 0;
795 
796 		error = -zfs_space(zp, F_FREESP, &bf, O_RDWR, offset, cr);
797 	} else if ((mode & ~FALLOC_FL_KEEP_SIZE) == 0) {
798 		unsigned int percent = zfs_fallocate_reserve_percent;
799 		struct kstatfs statfs;
800 
801 		/* Legacy mode, disable fallocate compatibility. */
802 		if (percent == 0) {
803 			error = -EOPNOTSUPP;
804 			goto out_unmark;
805 		}
806 
807 		/*
808 		 * Use zfs_statvfs() instead of dmu_objset_space() since it
809 		 * also checks project quota limits, which are relevant here.
810 		 */
811 		error = -zfs_statvfs(ip, &statfs);
812 		if (error)
813 			goto out_unmark;
814 
815 		/*
816 		 * Shrink available space a bit to account for overhead/races.
817 		 * We know the product previously fit into availbytes from
818 		 * dmu_objset_space(), so the smaller product will also fit.
819 		 */
820 		if (len > statfs.f_bavail * (statfs.f_bsize * 100 / percent)) {
821 			error = -ENOSPC;
822 			goto out_unmark;
823 		}
824 		if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > olen) {
825 			error = zpl_enter_verify_zp(zfsvfs, zp, FTAG);
826 			if (error)
827 				goto out_unmark;
828 
829 			/*
830 			 * extend file: log=TRUE drives z_seq bump,
831 			 * mtime/ctime advance, and TX_TRUNCATE ZIL
832 			 * record; matches zfs_space().
833 			 */
834 			error = -zfs_freesp(zp, offset + len, 0, 0, TRUE);
835 			zfs_exit(zfsvfs, FTAG);
836 		}
837 	}
838 out_unmark:
839 	spl_fstrans_unmark(cookie);
840 	spl_inode_unlock(ip);
841 
842 	crfree(cr);
843 
844 	return (error);
845 }
846 
847 static long
zpl_fallocate(struct file * filp,int mode,loff_t offset,loff_t len)848 zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
849 {
850 	return zpl_fallocate_common(file_inode(filp),
851 	    mode, offset, len);
852 }
853 
854 static int
zpl_ioctl_getversion(struct file * filp,void __user * arg)855 zpl_ioctl_getversion(struct file *filp, void __user *arg)
856 {
857 	uint32_t generation = file_inode(filp)->i_generation;
858 
859 	return (copy_to_user(arg, &generation, sizeof (generation)));
860 }
861 
862 static int
zpl_fadvise(struct file * filp,loff_t offset,loff_t len,int advice)863 zpl_fadvise(struct file *filp, loff_t offset, loff_t len, int advice)
864 {
865 	struct inode *ip = file_inode(filp);
866 	znode_t *zp = ITOZ(ip);
867 	zfsvfs_t *zfsvfs = ITOZSB(ip);
868 	objset_t *os = zfsvfs->z_os;
869 	int error = 0;
870 
871 	if (S_ISFIFO(ip->i_mode))
872 		return (-ESPIPE);
873 
874 	if (offset < 0 || len < 0)
875 		return (-EINVAL);
876 
877 	if ((error = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
878 		return (error);
879 
880 	if (advice == POSIX_FADV_WILLNEED) {
881 		loff_t rlen = len ? len : i_size_read(ip) - offset;
882 		dmu_prefetch_user(os, zp->z_id, 0, offset, rlen,
883 		    ZIO_PRIORITY_ASYNC_READ);
884 		if (!zn_has_cached_data(zp, offset, offset + rlen - 1)) {
885 			zfs_exit(zfsvfs, FTAG);
886 			return (error);
887 		}
888 	}
889 
890 #ifdef HAVE_GENERIC_FADVISE
891 	error = generic_fadvise(filp, offset, len, advice);
892 #endif
893 
894 	if (error == 0 && advice == POSIX_FADV_DONTNEED) {
895 		loff_t rlen = len ? len : i_size_read(ip) - offset;
896 		dmu_evict_range(os, zp->z_id, offset, rlen);
897 	}
898 
899 	zfs_exit(zfsvfs, FTAG);
900 
901 	return (error);
902 }
903 
904 #define	ZFS_FL_USER_VISIBLE	(FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL)
905 #define	ZFS_FL_USER_MODIFIABLE	(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL)
906 
907 
908 static struct {
909 	uint64_t zfs_flag;
910 	uint32_t fs_flag;
911 	uint32_t xflag;
912 } flags_lookup[] = {
913 	{ZFS_IMMUTABLE, FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE},
914 	{ZFS_APPENDONLY, FS_APPEND_FL, FS_XFLAG_APPEND},
915 	{ZFS_NODUMP, FS_NODUMP_FL, FS_XFLAG_NODUMP},
916 	{ZFS_PROJINHERIT, FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT}
917 };
918 
919 static uint32_t
__zpl_ioctl_getflags(struct inode * ip)920 __zpl_ioctl_getflags(struct inode *ip)
921 {
922 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
923 	uint32_t ioctl_flags = 0;
924 	for (int i = 0; i < ARRAY_SIZE(flags_lookup); i++)
925 		if (zfs_flags & flags_lookup[i].zfs_flag)
926 			ioctl_flags |= flags_lookup[i].fs_flag;
927 
928 	return (ioctl_flags);
929 }
930 
931 static uint32_t
__zpl_ioctl_getxflags(struct inode * ip)932 __zpl_ioctl_getxflags(struct inode *ip)
933 {
934 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
935 	uint32_t ioctl_flags = 0;
936 
937 	for (int i = 0; i < ARRAY_SIZE(flags_lookup); i++)
938 		if (zfs_flags & flags_lookup[i].zfs_flag)
939 			ioctl_flags |= flags_lookup[i].xflag;
940 
941 	return (ioctl_flags);
942 }
943 
944 /*
945  * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
946  * attributes common to both Linux and Solaris are mapped.
947  */
948 static int
zpl_ioctl_getflags(struct file * filp,void __user * arg)949 zpl_ioctl_getflags(struct file *filp, void __user *arg)
950 {
951 	uint32_t flags;
952 	int err;
953 
954 	flags = __zpl_ioctl_getflags(file_inode(filp));
955 	flags = flags & ZFS_FL_USER_VISIBLE;
956 	err = copy_to_user(arg, &flags, sizeof (flags));
957 
958 	return (err);
959 }
960 
961 /*
962  * fchange() is a helper macro to detect if we have been asked to change a
963  * flag. This is ugly, but the requirement that we do this is a consequence of
964  * how the Linux file attribute interface was designed. Another consequence is
965  * that concurrent modification of files suffers from a TOCTOU race. Neither
966  * are things we can fix without modifying the kernel-userland interface, which
967  * is outside of our jurisdiction.
968  */
969 
970 #define	fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
971 
972 static int
__zpl_ioctl_setflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)973 __zpl_ioctl_setflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
974 {
975 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
976 	xoptattr_t *xoap;
977 
978 	if (ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL |
979 	    FS_PROJINHERIT_FL))
980 		return (-EOPNOTSUPP);
981 
982 	if (ioctl_flags & ~ZFS_FL_USER_MODIFIABLE)
983 		return (-EACCES);
984 
985 	if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) ||
986 	    fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) &&
987 	    !capable(CAP_LINUX_IMMUTABLE))
988 		return (-EPERM);
989 
990 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
991 		return (-EACCES);
992 
993 	xva_init(xva);
994 	xoap = xva_getxoptattr(xva);
995 
996 #define	FLAG_CHANGE(iflag, zflag, xflag, xfield)	do {	\
997 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) ||	\
998 	    ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) {	\
999 		XVA_SET_REQ(xva, (xflag));	\
1000 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
1001 	}	\
1002 } while (0)
1003 
1004 	FLAG_CHANGE(FS_IMMUTABLE_FL, ZFS_IMMUTABLE, XAT_IMMUTABLE,
1005 	    xoap->xoa_immutable);
1006 	FLAG_CHANGE(FS_APPEND_FL, ZFS_APPENDONLY, XAT_APPENDONLY,
1007 	    xoap->xoa_appendonly);
1008 	FLAG_CHANGE(FS_NODUMP_FL, ZFS_NODUMP, XAT_NODUMP,
1009 	    xoap->xoa_nodump);
1010 	FLAG_CHANGE(FS_PROJINHERIT_FL, ZFS_PROJINHERIT, XAT_PROJINHERIT,
1011 	    xoap->xoa_projinherit);
1012 
1013 #undef	FLAG_CHANGE
1014 
1015 	return (0);
1016 }
1017 
1018 static int
__zpl_ioctl_setxflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)1019 __zpl_ioctl_setxflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
1020 {
1021 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
1022 	xoptattr_t *xoap;
1023 
1024 	if (ioctl_flags & ~(FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND |
1025 	    FS_XFLAG_NODUMP | FS_XFLAG_PROJINHERIT))
1026 		return (-EOPNOTSUPP);
1027 
1028 	if ((fchange(ioctl_flags, zfs_flags, FS_XFLAG_IMMUTABLE,
1029 	    ZFS_IMMUTABLE) ||
1030 	    fchange(ioctl_flags, zfs_flags, FS_XFLAG_APPEND, ZFS_APPENDONLY)) &&
1031 	    !capable(CAP_LINUX_IMMUTABLE))
1032 		return (-EPERM);
1033 
1034 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
1035 		return (-EACCES);
1036 
1037 	xva_init(xva);
1038 	xoap = xva_getxoptattr(xva);
1039 
1040 #define	FLAG_CHANGE(iflag, zflag, xflag, xfield)	do {	\
1041 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) ||	\
1042 	    ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) {	\
1043 		XVA_SET_REQ(xva, (xflag));	\
1044 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
1045 	}	\
1046 } while (0)
1047 
1048 	FLAG_CHANGE(FS_XFLAG_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
1049 	    xoap->xoa_immutable);
1050 	FLAG_CHANGE(FS_XFLAG_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
1051 	    xoap->xoa_appendonly);
1052 	FLAG_CHANGE(FS_XFLAG_NODUMP, ZFS_NODUMP, XAT_NODUMP,
1053 	    xoap->xoa_nodump);
1054 	FLAG_CHANGE(FS_XFLAG_PROJINHERIT, ZFS_PROJINHERIT, XAT_PROJINHERIT,
1055 	    xoap->xoa_projinherit);
1056 
1057 #undef	FLAG_CHANGE
1058 
1059 	return (0);
1060 }
1061 
1062 static int
zpl_ioctl_setflags(struct file * filp,void __user * arg)1063 zpl_ioctl_setflags(struct file *filp, void __user *arg)
1064 {
1065 	struct inode *ip = file_inode(filp);
1066 	uint32_t flags;
1067 	cred_t *cr = CRED();
1068 	xvattr_t xva;
1069 	int err;
1070 	fstrans_cookie_t cookie;
1071 
1072 	if (copy_from_user(&flags, arg, sizeof (flags)))
1073 		return (-EFAULT);
1074 
1075 	err = __zpl_ioctl_setflags(ip, flags, &xva);
1076 	if (err)
1077 		return (err);
1078 
1079 	crhold(cr);
1080 	cookie = spl_fstrans_mark();
1081 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr);
1082 	spl_fstrans_unmark(cookie);
1083 	crfree(cr);
1084 
1085 	return (err);
1086 }
1087 
1088 static int
zpl_ioctl_getxattr(struct file * filp,void __user * arg)1089 zpl_ioctl_getxattr(struct file *filp, void __user *arg)
1090 {
1091 	zfsxattr_t fsx = { 0 };
1092 	struct inode *ip = file_inode(filp);
1093 	int err;
1094 
1095 	fsx.fsx_xflags = __zpl_ioctl_getxflags(ip);
1096 	fsx.fsx_projid = ITOZ(ip)->z_projid;
1097 	err = copy_to_user(arg, &fsx, sizeof (fsx));
1098 
1099 	return (err);
1100 }
1101 
1102 static int
zpl_ioctl_setxattr(struct file * filp,void __user * arg)1103 zpl_ioctl_setxattr(struct file *filp, void __user *arg)
1104 {
1105 	struct inode *ip = file_inode(filp);
1106 	zfsxattr_t fsx;
1107 	cred_t *cr = CRED();
1108 	xvattr_t xva;
1109 	xoptattr_t *xoap;
1110 	int err;
1111 	fstrans_cookie_t cookie;
1112 
1113 	if (copy_from_user(&fsx, arg, sizeof (fsx)))
1114 		return (-EFAULT);
1115 
1116 	if (!zpl_is_valid_projid(fsx.fsx_projid))
1117 		return (-EINVAL);
1118 
1119 	err = __zpl_ioctl_setxflags(ip, fsx.fsx_xflags, &xva);
1120 	if (err)
1121 		return (err);
1122 
1123 	xoap = xva_getxoptattr(&xva);
1124 	XVA_SET_REQ(&xva, XAT_PROJID);
1125 	xoap->xoa_projid = fsx.fsx_projid;
1126 
1127 	crhold(cr);
1128 	cookie = spl_fstrans_mark();
1129 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr);
1130 	spl_fstrans_unmark(cookie);
1131 	crfree(cr);
1132 
1133 	return (err);
1134 }
1135 
1136 /*
1137  * Expose Additional File Level Attributes of ZFS.
1138  */
1139 static int
zpl_ioctl_getdosflags(struct file * filp,void __user * arg)1140 zpl_ioctl_getdosflags(struct file *filp, void __user *arg)
1141 {
1142 	struct inode *ip = file_inode(filp);
1143 	uint64_t dosflags = ITOZ(ip)->z_pflags;
1144 	dosflags &= ZFS_DOS_FL_USER_VISIBLE;
1145 	int err = copy_to_user(arg, &dosflags, sizeof (dosflags));
1146 
1147 	return (err);
1148 }
1149 
1150 static int
__zpl_ioctl_setdosflags(struct inode * ip,uint64_t ioctl_flags,xvattr_t * xva)1151 __zpl_ioctl_setdosflags(struct inode *ip, uint64_t ioctl_flags, xvattr_t *xva)
1152 {
1153 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
1154 	xoptattr_t *xoap;
1155 
1156 	if (ioctl_flags & (~ZFS_DOS_FL_USER_VISIBLE))
1157 		return (-EOPNOTSUPP);
1158 
1159 	if ((fchange(ioctl_flags, zfs_flags, ZFS_IMMUTABLE, ZFS_IMMUTABLE) ||
1160 	    fchange(ioctl_flags, zfs_flags, ZFS_APPENDONLY, ZFS_APPENDONLY)) &&
1161 	    !capable(CAP_LINUX_IMMUTABLE))
1162 		return (-EPERM);
1163 
1164 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
1165 		return (-EACCES);
1166 
1167 	xva_init(xva);
1168 	xoap = xva_getxoptattr(xva);
1169 
1170 #define	FLAG_CHANGE(iflag, xflag, xfield)	do {	\
1171 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (iflag))) ||	\
1172 	    ((zfs_flags & (iflag)) && !(ioctl_flags & (iflag)))) {	\
1173 		XVA_SET_REQ(xva, (xflag));	\
1174 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
1175 	}	\
1176 } while (0)
1177 
1178 	FLAG_CHANGE(ZFS_IMMUTABLE, XAT_IMMUTABLE, xoap->xoa_immutable);
1179 	FLAG_CHANGE(ZFS_APPENDONLY, XAT_APPENDONLY, xoap->xoa_appendonly);
1180 	FLAG_CHANGE(ZFS_NODUMP, XAT_NODUMP, xoap->xoa_nodump);
1181 	FLAG_CHANGE(ZFS_READONLY, XAT_READONLY, xoap->xoa_readonly);
1182 	FLAG_CHANGE(ZFS_HIDDEN, XAT_HIDDEN, xoap->xoa_hidden);
1183 	FLAG_CHANGE(ZFS_SYSTEM, XAT_SYSTEM, xoap->xoa_system);
1184 	FLAG_CHANGE(ZFS_ARCHIVE, XAT_ARCHIVE, xoap->xoa_archive);
1185 	FLAG_CHANGE(ZFS_NOUNLINK, XAT_NOUNLINK, xoap->xoa_nounlink);
1186 	FLAG_CHANGE(ZFS_REPARSE, XAT_REPARSE, xoap->xoa_reparse);
1187 	FLAG_CHANGE(ZFS_OFFLINE, XAT_OFFLINE, xoap->xoa_offline);
1188 	FLAG_CHANGE(ZFS_SPARSE, XAT_SPARSE, xoap->xoa_sparse);
1189 
1190 #undef	FLAG_CHANGE
1191 
1192 	return (0);
1193 }
1194 
1195 /*
1196  * Set Additional File Level Attributes of ZFS.
1197  */
1198 static int
zpl_ioctl_setdosflags(struct file * filp,void __user * arg)1199 zpl_ioctl_setdosflags(struct file *filp, void __user *arg)
1200 {
1201 	struct inode *ip = file_inode(filp);
1202 	uint64_t dosflags;
1203 	cred_t *cr = CRED();
1204 	xvattr_t xva;
1205 	int err;
1206 	fstrans_cookie_t cookie;
1207 
1208 	if (copy_from_user(&dosflags, arg, sizeof (dosflags)))
1209 		return (-EFAULT);
1210 
1211 	err = __zpl_ioctl_setdosflags(ip, dosflags, &xva);
1212 	if (err)
1213 		return (err);
1214 
1215 	crhold(cr);
1216 	cookie = spl_fstrans_mark();
1217 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr);
1218 	spl_fstrans_unmark(cookie);
1219 	crfree(cr);
1220 
1221 	return (err);
1222 }
1223 
1224 static int
zpl_ioctl_rewrite(struct file * filp,void __user * arg)1225 zpl_ioctl_rewrite(struct file *filp, void __user *arg)
1226 {
1227 	struct inode *ip = file_inode(filp);
1228 	zfs_rewrite_args_t args;
1229 	fstrans_cookie_t cookie;
1230 	int err;
1231 
1232 	if (copy_from_user(&args, arg, sizeof (args)))
1233 		return (-EFAULT);
1234 
1235 	if (unlikely(!(filp->f_mode & FMODE_WRITE)))
1236 		return (-EBADF);
1237 
1238 	cookie = spl_fstrans_mark();
1239 	err = -zfs_rewrite(ITOZ(ip), args.off, args.len, args.flags, args.arg);
1240 	spl_fstrans_unmark(cookie);
1241 
1242 	return (err);
1243 }
1244 
1245 static long
zpl_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1246 zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1247 {
1248 	switch (cmd) {
1249 	case FS_IOC_GETVERSION:
1250 		return (zpl_ioctl_getversion(filp, (void *)arg));
1251 	case FS_IOC_GETFLAGS:
1252 		return (zpl_ioctl_getflags(filp, (void *)arg));
1253 	case FS_IOC_SETFLAGS:
1254 		return (zpl_ioctl_setflags(filp, (void *)arg));
1255 	case ZFS_IOC_FSGETXATTR:
1256 		return (zpl_ioctl_getxattr(filp, (void *)arg));
1257 	case ZFS_IOC_FSSETXATTR:
1258 		return (zpl_ioctl_setxattr(filp, (void *)arg));
1259 	case ZFS_IOC_GETDOSFLAGS:
1260 		return (zpl_ioctl_getdosflags(filp, (void *)arg));
1261 	case ZFS_IOC_SETDOSFLAGS:
1262 		return (zpl_ioctl_setdosflags(filp, (void *)arg));
1263 	case ZFS_IOC_REWRITE:
1264 		return (zpl_ioctl_rewrite(filp, (void *)arg));
1265 	default:
1266 		return (-ENOTTY);
1267 	}
1268 }
1269 
1270 #ifdef CONFIG_COMPAT
1271 static long
zpl_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1272 zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1273 {
1274 	switch (cmd) {
1275 	case FS_IOC32_GETVERSION:
1276 		cmd = FS_IOC_GETVERSION;
1277 		break;
1278 	case FS_IOC32_GETFLAGS:
1279 		cmd = FS_IOC_GETFLAGS;
1280 		break;
1281 	case FS_IOC32_SETFLAGS:
1282 		cmd = FS_IOC_SETFLAGS;
1283 		break;
1284 	default:
1285 		return (-ENOTTY);
1286 	}
1287 	return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)));
1288 }
1289 #endif /* CONFIG_COMPAT */
1290 
1291 const struct address_space_operations zpl_address_space_operations = {
1292 #ifdef HAVE_VFS_READPAGES
1293 	.readpages	= zpl_readpages,
1294 #else
1295 	.readahead	= zpl_readahead,
1296 #endif
1297 #ifdef HAVE_VFS_READ_FOLIO
1298 	.read_folio	= zpl_read_folio,
1299 #else
1300 	.readpage	= zpl_readpage,
1301 #endif
1302 #ifdef HAVE_VFS_WRITEPAGE
1303 	.writepage	= zpl_writepage,
1304 #endif
1305 	.writepages	= zpl_writepages,
1306 	.direct_IO	= zpl_direct_IO,
1307 #ifdef HAVE_VFS_SET_PAGE_DIRTY_NOBUFFERS
1308 	.set_page_dirty = __set_page_dirty_nobuffers,
1309 #endif
1310 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
1311 	.dirty_folio	= filemap_dirty_folio,
1312 #endif
1313 #ifdef HAVE_VFS_MIGRATE_FOLIO
1314 	.migrate_folio	= migrate_folio,
1315 #elif defined(HAVE_VFS_MIGRATEPAGE)
1316 	.migratepage	= migrate_page,
1317 #endif
1318 };
1319 
1320 const struct file_operations zpl_file_operations = {
1321 	.open		= zpl_open,
1322 	.release	= zpl_release,
1323 	.llseek		= zpl_llseek,
1324 	.read_iter	= zpl_iter_read,
1325 	.write_iter	= zpl_iter_write,
1326 #ifdef HAVE_COPY_SPLICE_READ
1327 	.splice_read	= copy_splice_read,
1328 #else
1329 	.splice_read	= generic_file_splice_read,
1330 #endif
1331 	.splice_write	= iter_file_splice_write,
1332 	.mmap		= zpl_mmap,
1333 	.fsync		= zpl_fsync,
1334 	.fallocate	= zpl_fallocate,
1335 	.setlease	= generic_setlease,
1336 	.copy_file_range	= zpl_copy_file_range,
1337 #ifdef HAVE_VFS_CLONE_FILE_RANGE
1338 	.clone_file_range	= zpl_clone_file_range,
1339 #endif
1340 #ifdef HAVE_VFS_REMAP_FILE_RANGE
1341 	.remap_file_range	= zpl_remap_file_range,
1342 #endif
1343 #ifdef HAVE_VFS_DEDUPE_FILE_RANGE
1344 	.dedupe_file_range	= zpl_dedupe_file_range,
1345 #endif
1346 	.fadvise	= zpl_fadvise,
1347 #ifdef HAVE_VFS_FOP_FLAGS
1348 	.fop_flags	=
1349 #ifdef FOP_DIO_PARALLEL_WRITE
1350 	/*
1351 	 * Writes are serialized by the znode's own per-range lock rather
1352 	 * than by i_rwsem, so non-overlapping O_DIRECT writes need no
1353 	 * further serialization from the VFS or from io_uring.
1354 	 */
1355 	    FOP_DIO_PARALLEL_WRITE |
1356 #endif
1357 #ifdef FOP_DONTCACHE
1358 	    FOP_DONTCACHE |
1359 #endif
1360 	    0,
1361 #endif
1362 	.unlocked_ioctl	= zpl_ioctl,
1363 #ifdef CONFIG_COMPAT
1364 	.compat_ioctl	= zpl_compat_ioctl,
1365 #endif
1366 };
1367 
1368 const struct file_operations zpl_dir_file_operations = {
1369 	.llseek		= generic_file_llseek,
1370 	.read		= generic_read_dir,
1371 	.iterate_shared	= zpl_iterate,
1372 	.fsync		= zpl_fsync,
1373 	.setlease	= generic_setlease,
1374 	.unlocked_ioctl = zpl_ioctl,
1375 #ifdef CONFIG_COMPAT
1376 	.compat_ioctl   = zpl_compat_ioctl,
1377 #endif
1378 };
1379 
1380 module_param(zfs_fallocate_reserve_percent, uint, 0644);
1381 MODULE_PARM_DESC(zfs_fallocate_reserve_percent,
1382 	"Percentage of length to use for the available capacity check");
1383