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