xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zpl_file.c (revision efa8679e7f69c9cc225613827d9f75644cca5b3b)
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 	znode_t *zp = ITOZ(ip);
677 	zfsvfs_t *zfsvfs = ITOZSB(ip);
678 	loff_t olen;
679 	fstrans_cookie_t cookie;
680 	int error = 0;
681 
682 	int test_mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE;
683 
684 	if ((mode & ~(FALLOC_FL_KEEP_SIZE | test_mode)) != 0)
685 		return (-EOPNOTSUPP);
686 
687 	if (offset < 0 || len <= 0)
688 		return (-EINVAL);
689 
690 	spl_inode_lock(ip);
691 	olen = i_size_read(ip);
692 
693 	crhold(cr);
694 	cookie = spl_fstrans_mark();
695 	if (mode & (test_mode)) {
696 		flock64_t bf;
697 
698 		if (mode & FALLOC_FL_KEEP_SIZE) {
699 			if (offset > olen)
700 				goto out_unmark;
701 
702 			if (offset + len > olen)
703 				len = olen - offset;
704 		}
705 		bf.l_type = F_WRLCK;
706 		bf.l_whence = SEEK_SET;
707 		bf.l_start = offset;
708 		bf.l_len = len;
709 		bf.l_pid = 0;
710 
711 		error = -zfs_space(zp, F_FREESP, &bf, O_RDWR, offset, cr);
712 	} else if ((mode & ~FALLOC_FL_KEEP_SIZE) == 0) {
713 		unsigned int percent = zfs_fallocate_reserve_percent;
714 		struct kstatfs statfs;
715 
716 		/* Legacy mode, disable fallocate compatibility. */
717 		if (percent == 0) {
718 			error = -EOPNOTSUPP;
719 			goto out_unmark;
720 		}
721 
722 		/*
723 		 * Use zfs_statvfs() instead of dmu_objset_space() since it
724 		 * also checks project quota limits, which are relevant here.
725 		 */
726 		error = -zfs_statvfs(ip, &statfs);
727 		if (error)
728 			goto out_unmark;
729 
730 		/*
731 		 * Shrink available space a bit to account for overhead/races.
732 		 * We know the product previously fit into availbytes from
733 		 * dmu_objset_space(), so the smaller product will also fit.
734 		 */
735 		if (len > statfs.f_bavail * (statfs.f_bsize * 100 / percent)) {
736 			error = -ENOSPC;
737 			goto out_unmark;
738 		}
739 		if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > olen) {
740 			error = zpl_enter_verify_zp(zfsvfs, zp, FTAG);
741 			if (error)
742 				goto out_unmark;
743 
744 			error = -zfs_freesp(zp, offset + len, 0, 0, FALSE);
745 			zfs_exit(zfsvfs, FTAG);
746 		}
747 	}
748 out_unmark:
749 	spl_fstrans_unmark(cookie);
750 	spl_inode_unlock(ip);
751 
752 	crfree(cr);
753 
754 	return (error);
755 }
756 
757 static long
zpl_fallocate(struct file * filp,int mode,loff_t offset,loff_t len)758 zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
759 {
760 	return zpl_fallocate_common(file_inode(filp),
761 	    mode, offset, len);
762 }
763 
764 static int
zpl_ioctl_getversion(struct file * filp,void __user * arg)765 zpl_ioctl_getversion(struct file *filp, void __user *arg)
766 {
767 	uint32_t generation = file_inode(filp)->i_generation;
768 
769 	return (copy_to_user(arg, &generation, sizeof (generation)));
770 }
771 
772 static int
zpl_fadvise(struct file * filp,loff_t offset,loff_t len,int advice)773 zpl_fadvise(struct file *filp, loff_t offset, loff_t len, int advice)
774 {
775 	struct inode *ip = file_inode(filp);
776 	znode_t *zp = ITOZ(ip);
777 	zfsvfs_t *zfsvfs = ITOZSB(ip);
778 	objset_t *os = zfsvfs->z_os;
779 	int error = 0;
780 
781 	if (S_ISFIFO(ip->i_mode))
782 		return (-ESPIPE);
783 
784 	if (offset < 0 || len < 0)
785 		return (-EINVAL);
786 
787 	if ((error = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
788 		return (error);
789 
790 	if (advice == POSIX_FADV_WILLNEED) {
791 		loff_t rlen = len ? len : i_size_read(ip) - offset;
792 		dmu_prefetch(os, zp->z_id, 0, offset, rlen,
793 		    ZIO_PRIORITY_ASYNC_READ);
794 		if (!zn_has_cached_data(zp, offset, offset + rlen - 1)) {
795 			zfs_exit(zfsvfs, FTAG);
796 			return (error);
797 		}
798 	}
799 
800 #ifdef HAVE_GENERIC_FADVISE
801 	error = generic_fadvise(filp, offset, len, advice);
802 #endif
803 
804 	if (error == 0 && advice == POSIX_FADV_DONTNEED) {
805 		loff_t rlen = len ? len : i_size_read(ip) - offset;
806 		dmu_evict_range(os, zp->z_id, offset, rlen);
807 	}
808 
809 	zfs_exit(zfsvfs, FTAG);
810 
811 	return (error);
812 }
813 
814 #define	ZFS_FL_USER_VISIBLE	(FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL)
815 #define	ZFS_FL_USER_MODIFIABLE	(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL)
816 
817 
818 static struct {
819 	uint64_t zfs_flag;
820 	uint32_t fs_flag;
821 	uint32_t xflag;
822 } flags_lookup[] = {
823 	{ZFS_IMMUTABLE, FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE},
824 	{ZFS_APPENDONLY, FS_APPEND_FL, FS_XFLAG_APPEND},
825 	{ZFS_NODUMP, FS_NODUMP_FL, FS_XFLAG_NODUMP},
826 	{ZFS_PROJINHERIT, FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT}
827 };
828 
829 static uint32_t
__zpl_ioctl_getflags(struct inode * ip)830 __zpl_ioctl_getflags(struct inode *ip)
831 {
832 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
833 	uint32_t ioctl_flags = 0;
834 	for (int i = 0; i < ARRAY_SIZE(flags_lookup); i++)
835 		if (zfs_flags & flags_lookup[i].zfs_flag)
836 			ioctl_flags |= flags_lookup[i].fs_flag;
837 
838 	return (ioctl_flags);
839 }
840 
841 static uint32_t
__zpl_ioctl_getxflags(struct inode * ip)842 __zpl_ioctl_getxflags(struct inode *ip)
843 {
844 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
845 	uint32_t ioctl_flags = 0;
846 
847 	for (int i = 0; i < ARRAY_SIZE(flags_lookup); i++)
848 		if (zfs_flags & flags_lookup[i].zfs_flag)
849 			ioctl_flags |= flags_lookup[i].xflag;
850 
851 	return (ioctl_flags);
852 }
853 
854 /*
855  * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
856  * attributes common to both Linux and Solaris are mapped.
857  */
858 static int
zpl_ioctl_getflags(struct file * filp,void __user * arg)859 zpl_ioctl_getflags(struct file *filp, void __user *arg)
860 {
861 	uint32_t flags;
862 	int err;
863 
864 	flags = __zpl_ioctl_getflags(file_inode(filp));
865 	flags = flags & ZFS_FL_USER_VISIBLE;
866 	err = copy_to_user(arg, &flags, sizeof (flags));
867 
868 	return (err);
869 }
870 
871 /*
872  * fchange() is a helper macro to detect if we have been asked to change a
873  * flag. This is ugly, but the requirement that we do this is a consequence of
874  * how the Linux file attribute interface was designed. Another consequence is
875  * that concurrent modification of files suffers from a TOCTOU race. Neither
876  * are things we can fix without modifying the kernel-userland interface, which
877  * is outside of our jurisdiction.
878  */
879 
880 #define	fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
881 
882 static int
__zpl_ioctl_setflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)883 __zpl_ioctl_setflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
884 {
885 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
886 	xoptattr_t *xoap;
887 
888 	if (ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL |
889 	    FS_PROJINHERIT_FL))
890 		return (-EOPNOTSUPP);
891 
892 	if (ioctl_flags & ~ZFS_FL_USER_MODIFIABLE)
893 		return (-EACCES);
894 
895 	if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) ||
896 	    fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) &&
897 	    !capable(CAP_LINUX_IMMUTABLE))
898 		return (-EPERM);
899 
900 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
901 		return (-EACCES);
902 
903 	xva_init(xva);
904 	xoap = xva_getxoptattr(xva);
905 
906 #define	FLAG_CHANGE(iflag, zflag, xflag, xfield)	do {	\
907 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) ||	\
908 	    ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) {	\
909 		XVA_SET_REQ(xva, (xflag));	\
910 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
911 	}	\
912 } while (0)
913 
914 	FLAG_CHANGE(FS_IMMUTABLE_FL, ZFS_IMMUTABLE, XAT_IMMUTABLE,
915 	    xoap->xoa_immutable);
916 	FLAG_CHANGE(FS_APPEND_FL, ZFS_APPENDONLY, XAT_APPENDONLY,
917 	    xoap->xoa_appendonly);
918 	FLAG_CHANGE(FS_NODUMP_FL, ZFS_NODUMP, XAT_NODUMP,
919 	    xoap->xoa_nodump);
920 	FLAG_CHANGE(FS_PROJINHERIT_FL, ZFS_PROJINHERIT, XAT_PROJINHERIT,
921 	    xoap->xoa_projinherit);
922 
923 #undef	FLAG_CHANGE
924 
925 	return (0);
926 }
927 
928 static int
__zpl_ioctl_setxflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)929 __zpl_ioctl_setxflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
930 {
931 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
932 	xoptattr_t *xoap;
933 
934 	if (ioctl_flags & ~(FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND |
935 	    FS_XFLAG_NODUMP | FS_XFLAG_PROJINHERIT))
936 		return (-EOPNOTSUPP);
937 
938 	if ((fchange(ioctl_flags, zfs_flags, FS_XFLAG_IMMUTABLE,
939 	    ZFS_IMMUTABLE) ||
940 	    fchange(ioctl_flags, zfs_flags, FS_XFLAG_APPEND, ZFS_APPENDONLY)) &&
941 	    !capable(CAP_LINUX_IMMUTABLE))
942 		return (-EPERM);
943 
944 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
945 		return (-EACCES);
946 
947 	xva_init(xva);
948 	xoap = xva_getxoptattr(xva);
949 
950 #define	FLAG_CHANGE(iflag, zflag, xflag, xfield)	do {	\
951 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) ||	\
952 	    ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) {	\
953 		XVA_SET_REQ(xva, (xflag));	\
954 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
955 	}	\
956 } while (0)
957 
958 	FLAG_CHANGE(FS_XFLAG_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
959 	    xoap->xoa_immutable);
960 	FLAG_CHANGE(FS_XFLAG_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
961 	    xoap->xoa_appendonly);
962 	FLAG_CHANGE(FS_XFLAG_NODUMP, ZFS_NODUMP, XAT_NODUMP,
963 	    xoap->xoa_nodump);
964 	FLAG_CHANGE(FS_XFLAG_PROJINHERIT, ZFS_PROJINHERIT, XAT_PROJINHERIT,
965 	    xoap->xoa_projinherit);
966 
967 #undef	FLAG_CHANGE
968 
969 	return (0);
970 }
971 
972 static int
zpl_ioctl_setflags(struct file * filp,void __user * arg)973 zpl_ioctl_setflags(struct file *filp, void __user *arg)
974 {
975 	struct inode *ip = file_inode(filp);
976 	uint32_t flags;
977 	cred_t *cr = CRED();
978 	xvattr_t xva;
979 	int err;
980 	fstrans_cookie_t cookie;
981 
982 	if (copy_from_user(&flags, arg, sizeof (flags)))
983 		return (-EFAULT);
984 
985 	err = __zpl_ioctl_setflags(ip, flags, &xva);
986 	if (err)
987 		return (err);
988 
989 	crhold(cr);
990 	cookie = spl_fstrans_mark();
991 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
992 	spl_fstrans_unmark(cookie);
993 	crfree(cr);
994 
995 	return (err);
996 }
997 
998 static int
zpl_ioctl_getxattr(struct file * filp,void __user * arg)999 zpl_ioctl_getxattr(struct file *filp, void __user *arg)
1000 {
1001 	zfsxattr_t fsx = { 0 };
1002 	struct inode *ip = file_inode(filp);
1003 	int err;
1004 
1005 	fsx.fsx_xflags = __zpl_ioctl_getxflags(ip);
1006 	fsx.fsx_projid = ITOZ(ip)->z_projid;
1007 	err = copy_to_user(arg, &fsx, sizeof (fsx));
1008 
1009 	return (err);
1010 }
1011 
1012 static int
zpl_ioctl_setxattr(struct file * filp,void __user * arg)1013 zpl_ioctl_setxattr(struct file *filp, void __user *arg)
1014 {
1015 	struct inode *ip = file_inode(filp);
1016 	zfsxattr_t fsx;
1017 	cred_t *cr = CRED();
1018 	xvattr_t xva;
1019 	xoptattr_t *xoap;
1020 	int err;
1021 	fstrans_cookie_t cookie;
1022 
1023 	if (copy_from_user(&fsx, arg, sizeof (fsx)))
1024 		return (-EFAULT);
1025 
1026 	if (!zpl_is_valid_projid(fsx.fsx_projid))
1027 		return (-EINVAL);
1028 
1029 	err = __zpl_ioctl_setxflags(ip, fsx.fsx_xflags, &xva);
1030 	if (err)
1031 		return (err);
1032 
1033 	xoap = xva_getxoptattr(&xva);
1034 	XVA_SET_REQ(&xva, XAT_PROJID);
1035 	xoap->xoa_projid = fsx.fsx_projid;
1036 
1037 	crhold(cr);
1038 	cookie = spl_fstrans_mark();
1039 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
1040 	spl_fstrans_unmark(cookie);
1041 	crfree(cr);
1042 
1043 	return (err);
1044 }
1045 
1046 /*
1047  * Expose Additional File Level Attributes of ZFS.
1048  */
1049 static int
zpl_ioctl_getdosflags(struct file * filp,void __user * arg)1050 zpl_ioctl_getdosflags(struct file *filp, void __user *arg)
1051 {
1052 	struct inode *ip = file_inode(filp);
1053 	uint64_t dosflags = ITOZ(ip)->z_pflags;
1054 	dosflags &= ZFS_DOS_FL_USER_VISIBLE;
1055 	int err = copy_to_user(arg, &dosflags, sizeof (dosflags));
1056 
1057 	return (err);
1058 }
1059 
1060 static int
__zpl_ioctl_setdosflags(struct inode * ip,uint64_t ioctl_flags,xvattr_t * xva)1061 __zpl_ioctl_setdosflags(struct inode *ip, uint64_t ioctl_flags, xvattr_t *xva)
1062 {
1063 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
1064 	xoptattr_t *xoap;
1065 
1066 	if (ioctl_flags & (~ZFS_DOS_FL_USER_VISIBLE))
1067 		return (-EOPNOTSUPP);
1068 
1069 	if ((fchange(ioctl_flags, zfs_flags, ZFS_IMMUTABLE, ZFS_IMMUTABLE) ||
1070 	    fchange(ioctl_flags, zfs_flags, ZFS_APPENDONLY, ZFS_APPENDONLY)) &&
1071 	    !capable(CAP_LINUX_IMMUTABLE))
1072 		return (-EPERM);
1073 
1074 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
1075 		return (-EACCES);
1076 
1077 	xva_init(xva);
1078 	xoap = xva_getxoptattr(xva);
1079 
1080 #define	FLAG_CHANGE(iflag, xflag, xfield)	do {	\
1081 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (iflag))) ||	\
1082 	    ((zfs_flags & (iflag)) && !(ioctl_flags & (iflag)))) {	\
1083 		XVA_SET_REQ(xva, (xflag));	\
1084 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
1085 	}	\
1086 } while (0)
1087 
1088 	FLAG_CHANGE(ZFS_IMMUTABLE, XAT_IMMUTABLE, xoap->xoa_immutable);
1089 	FLAG_CHANGE(ZFS_APPENDONLY, XAT_APPENDONLY, xoap->xoa_appendonly);
1090 	FLAG_CHANGE(ZFS_NODUMP, XAT_NODUMP, xoap->xoa_nodump);
1091 	FLAG_CHANGE(ZFS_READONLY, XAT_READONLY, xoap->xoa_readonly);
1092 	FLAG_CHANGE(ZFS_HIDDEN, XAT_HIDDEN, xoap->xoa_hidden);
1093 	FLAG_CHANGE(ZFS_SYSTEM, XAT_SYSTEM, xoap->xoa_system);
1094 	FLAG_CHANGE(ZFS_ARCHIVE, XAT_ARCHIVE, xoap->xoa_archive);
1095 	FLAG_CHANGE(ZFS_NOUNLINK, XAT_NOUNLINK, xoap->xoa_nounlink);
1096 	FLAG_CHANGE(ZFS_REPARSE, XAT_REPARSE, xoap->xoa_reparse);
1097 	FLAG_CHANGE(ZFS_OFFLINE, XAT_OFFLINE, xoap->xoa_offline);
1098 	FLAG_CHANGE(ZFS_SPARSE, XAT_SPARSE, xoap->xoa_sparse);
1099 
1100 #undef	FLAG_CHANGE
1101 
1102 	return (0);
1103 }
1104 
1105 /*
1106  * Set Additional File Level Attributes of ZFS.
1107  */
1108 static int
zpl_ioctl_setdosflags(struct file * filp,void __user * arg)1109 zpl_ioctl_setdosflags(struct file *filp, void __user *arg)
1110 {
1111 	struct inode *ip = file_inode(filp);
1112 	uint64_t dosflags;
1113 	cred_t *cr = CRED();
1114 	xvattr_t xva;
1115 	int err;
1116 	fstrans_cookie_t cookie;
1117 
1118 	if (copy_from_user(&dosflags, arg, sizeof (dosflags)))
1119 		return (-EFAULT);
1120 
1121 	err = __zpl_ioctl_setdosflags(ip, dosflags, &xva);
1122 	if (err)
1123 		return (err);
1124 
1125 	crhold(cr);
1126 	cookie = spl_fstrans_mark();
1127 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
1128 	spl_fstrans_unmark(cookie);
1129 	crfree(cr);
1130 
1131 	return (err);
1132 }
1133 
1134 static int
zpl_ioctl_rewrite(struct file * filp,void __user * arg)1135 zpl_ioctl_rewrite(struct file *filp, void __user *arg)
1136 {
1137 	struct inode *ip = file_inode(filp);
1138 	zfs_rewrite_args_t args;
1139 	fstrans_cookie_t cookie;
1140 	int err;
1141 
1142 	if (copy_from_user(&args, arg, sizeof (args)))
1143 		return (-EFAULT);
1144 
1145 	if (unlikely(!(filp->f_mode & FMODE_WRITE)))
1146 		return (-EBADF);
1147 
1148 	cookie = spl_fstrans_mark();
1149 	err = -zfs_rewrite(ITOZ(ip), args.off, args.len, args.flags, args.arg);
1150 	spl_fstrans_unmark(cookie);
1151 
1152 	return (err);
1153 }
1154 
1155 static long
zpl_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1156 zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1157 {
1158 	switch (cmd) {
1159 	case FS_IOC_GETVERSION:
1160 		return (zpl_ioctl_getversion(filp, (void *)arg));
1161 	case FS_IOC_GETFLAGS:
1162 		return (zpl_ioctl_getflags(filp, (void *)arg));
1163 	case FS_IOC_SETFLAGS:
1164 		return (zpl_ioctl_setflags(filp, (void *)arg));
1165 	case ZFS_IOC_FSGETXATTR:
1166 		return (zpl_ioctl_getxattr(filp, (void *)arg));
1167 	case ZFS_IOC_FSSETXATTR:
1168 		return (zpl_ioctl_setxattr(filp, (void *)arg));
1169 	case ZFS_IOC_GETDOSFLAGS:
1170 		return (zpl_ioctl_getdosflags(filp, (void *)arg));
1171 	case ZFS_IOC_SETDOSFLAGS:
1172 		return (zpl_ioctl_setdosflags(filp, (void *)arg));
1173 	case ZFS_IOC_REWRITE:
1174 		return (zpl_ioctl_rewrite(filp, (void *)arg));
1175 	default:
1176 		return (-ENOTTY);
1177 	}
1178 }
1179 
1180 #ifdef CONFIG_COMPAT
1181 static long
zpl_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1182 zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1183 {
1184 	switch (cmd) {
1185 	case FS_IOC32_GETVERSION:
1186 		cmd = FS_IOC_GETVERSION;
1187 		break;
1188 	case FS_IOC32_GETFLAGS:
1189 		cmd = FS_IOC_GETFLAGS;
1190 		break;
1191 	case FS_IOC32_SETFLAGS:
1192 		cmd = FS_IOC_SETFLAGS;
1193 		break;
1194 	default:
1195 		return (-ENOTTY);
1196 	}
1197 	return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)));
1198 }
1199 #endif /* CONFIG_COMPAT */
1200 
1201 const struct address_space_operations zpl_address_space_operations = {
1202 #ifdef HAVE_VFS_READPAGES
1203 	.readpages	= zpl_readpages,
1204 #else
1205 	.readahead	= zpl_readahead,
1206 #endif
1207 #ifdef HAVE_VFS_READ_FOLIO
1208 	.read_folio	= zpl_read_folio,
1209 #else
1210 	.readpage	= zpl_readpage,
1211 #endif
1212 #ifdef HAVE_VFS_WRITEPAGE
1213 	.writepage	= zpl_writepage,
1214 #endif
1215 	.writepages	= zpl_writepages,
1216 	.direct_IO	= zpl_direct_IO,
1217 #ifdef HAVE_VFS_SET_PAGE_DIRTY_NOBUFFERS
1218 	.set_page_dirty = __set_page_dirty_nobuffers,
1219 #endif
1220 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
1221 	.dirty_folio	= filemap_dirty_folio,
1222 #endif
1223 #ifdef HAVE_VFS_MIGRATE_FOLIO
1224 	.migrate_folio	= migrate_folio,
1225 #elif defined(HAVE_VFS_MIGRATEPAGE)
1226 	.migratepage	= migrate_page,
1227 #endif
1228 };
1229 
1230 const struct file_operations zpl_file_operations = {
1231 	.open		= zpl_open,
1232 	.release	= zpl_release,
1233 	.llseek		= zpl_llseek,
1234 	.read_iter	= zpl_iter_read,
1235 	.write_iter	= zpl_iter_write,
1236 #ifdef HAVE_COPY_SPLICE_READ
1237 	.splice_read	= copy_splice_read,
1238 #else
1239 	.splice_read	= generic_file_splice_read,
1240 #endif
1241 	.splice_write	= iter_file_splice_write,
1242 	.mmap		= zpl_mmap,
1243 	.fsync		= zpl_fsync,
1244 	.fallocate	= zpl_fallocate,
1245 	.setlease	= generic_setlease,
1246 	.copy_file_range	= zpl_copy_file_range,
1247 #ifdef HAVE_VFS_CLONE_FILE_RANGE
1248 	.clone_file_range	= zpl_clone_file_range,
1249 #endif
1250 #ifdef HAVE_VFS_REMAP_FILE_RANGE
1251 	.remap_file_range	= zpl_remap_file_range,
1252 #endif
1253 #ifdef HAVE_VFS_DEDUPE_FILE_RANGE
1254 	.dedupe_file_range	= zpl_dedupe_file_range,
1255 #endif
1256 	.fadvise	= zpl_fadvise,
1257 	.unlocked_ioctl	= zpl_ioctl,
1258 #ifdef CONFIG_COMPAT
1259 	.compat_ioctl	= zpl_compat_ioctl,
1260 #endif
1261 };
1262 
1263 const struct file_operations zpl_dir_file_operations = {
1264 	.llseek		= generic_file_llseek,
1265 	.read		= generic_read_dir,
1266 	.iterate_shared	= zpl_iterate,
1267 	.fsync		= zpl_fsync,
1268 	.setlease	= generic_setlease,
1269 	.unlocked_ioctl = zpl_ioctl,
1270 #ifdef CONFIG_COMPAT
1271 	.compat_ioctl   = zpl_compat_ioctl,
1272 #endif
1273 };
1274 
1275 module_param(zfs_fallocate_reserve_percent, uint, 0644);
1276 MODULE_PARM_DESC(zfs_fallocate_reserve_percent,
1277 	"Percentage of length to use for the available capacity check");
1278