xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zpl_file.c (revision d0b3ecdc274930e190ea233b6b69ff03782eaf8d)
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 /*
376  * Bump z_seq when a clean page first transitions to dirty via an mmap store.
377  * The default generic_file_vm_ops.page_mkwrite (filemap_page_mkwrite) updates
378  * mtime/ctime via file_update_time -> __mark_inode_dirty, but never tells the
379  * filesystem that the change cookie should advance. Without this hook NFSv4
380  * GETATTR between an mmap store and writeback returns a stale change_cookie
381  * alongside the newer mtime, violating monotonicity. zfs_dirty_inode persists
382  * the new value on the same dirty path.
383  */
384 static vm_fault_t
zpl_page_mkwrite(struct vm_fault * vmf)385 zpl_page_mkwrite(struct vm_fault *vmf)
386 {
387 	znode_t *zp = ITOZ(file_inode(vmf->vma->vm_file));
388 
389 	atomic_inc_64(&zp->z_seq);
390 
391 	return (filemap_page_mkwrite(vmf));
392 }
393 
394 static const struct vm_operations_struct zpl_vm_ops = {
395 	.fault		= filemap_fault,
396 	.map_pages	= filemap_map_pages,
397 	.page_mkwrite	= zpl_page_mkwrite,
398 };
399 
400 static int
zpl_mmap(struct file * filp,struct vm_area_struct * vma)401 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
402 {
403 	struct inode *ip = filp->f_mapping->host;
404 	int error;
405 	fstrans_cookie_t cookie;
406 
407 	cookie = spl_fstrans_mark();
408 	error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start,
409 	    (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags);
410 	spl_fstrans_unmark(cookie);
411 
412 	if (error)
413 		return (error);
414 
415 	error = generic_file_mmap(filp, vma);
416 	if (error)
417 		return (error);
418 
419 	vma->vm_ops = &zpl_vm_ops;
420 	return (error);
421 }
422 
423 /*
424  * Populate a page with data for the Linux page cache.  This function is
425  * only used to support mmap(2).  There will be an identical copy of the
426  * data in the ARC which is kept up to date via .write() and .writepage().
427  */
428 static inline int
zpl_readpage_common(struct page * pp)429 zpl_readpage_common(struct page *pp)
430 {
431 	fstrans_cookie_t cookie;
432 
433 	ASSERT(PageLocked(pp));
434 
435 	cookie = spl_fstrans_mark();
436 	int error = -zfs_getpage(pp->mapping->host, pp);
437 	spl_fstrans_unmark(cookie);
438 
439 	unlock_page(pp);
440 
441 	return (error);
442 }
443 
444 #ifdef HAVE_VFS_READ_FOLIO
445 static int
zpl_read_folio(struct file * filp,struct folio * folio)446 zpl_read_folio(struct file *filp, struct folio *folio)
447 {
448 	return (zpl_readpage_common(&folio->page));
449 }
450 #else
451 static int
zpl_readpage(struct file * filp,struct page * pp)452 zpl_readpage(struct file *filp, struct page *pp)
453 {
454 	return (zpl_readpage_common(pp));
455 }
456 #endif
457 
458 static int
zpl_readpage_filler(void * data,struct page * pp)459 zpl_readpage_filler(void *data, struct page *pp)
460 {
461 	return (zpl_readpage_common(pp));
462 }
463 
464 /*
465  * Populate a set of pages with data for the Linux page cache.  This
466  * function will only be called for read ahead and never for demand
467  * paging.  For simplicity, the code relies on read_cache_pages() to
468  * correctly lock each page for IO and call zpl_readpage().
469  */
470 #ifdef HAVE_VFS_READPAGES
471 static int
zpl_readpages(struct file * filp,struct address_space * mapping,struct list_head * pages,unsigned nr_pages)472 zpl_readpages(struct file *filp, struct address_space *mapping,
473     struct list_head *pages, unsigned nr_pages)
474 {
475 	return (read_cache_pages(mapping, pages, zpl_readpage_filler, NULL));
476 }
477 #else
478 static void
zpl_readahead(struct readahead_control * ractl)479 zpl_readahead(struct readahead_control *ractl)
480 {
481 	struct page *page;
482 
483 	while ((page = readahead_page(ractl)) != NULL) {
484 		int ret;
485 
486 		ret = zpl_readpage_filler(NULL, page);
487 		put_page(page);
488 		if (ret)
489 			break;
490 	}
491 }
492 #endif
493 
494 static int
zpl_putpage(struct page * pp,struct writeback_control * wbc,void * data)495 zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data)
496 {
497 	boolean_t *for_sync = data;
498 	fstrans_cookie_t cookie;
499 	int ret;
500 
501 	ASSERT(PageLocked(pp));
502 	ASSERT(!PageWriteback(pp));
503 
504 	cookie = spl_fstrans_mark();
505 	ret = zfs_putpage(pp->mapping->host, pp, wbc, *for_sync);
506 	spl_fstrans_unmark(cookie);
507 
508 	return (ret);
509 }
510 
511 #ifdef HAVE_WRITE_CACHE_PAGES
512 #ifdef HAVE_WRITEPAGE_T_FOLIO
513 static int
zpl_putfolio(struct folio * pp,struct writeback_control * wbc,void * data)514 zpl_putfolio(struct folio *pp, struct writeback_control *wbc, void *data)
515 {
516 	return (zpl_putpage(&pp->page, wbc, data));
517 }
518 #endif
519 
520 static inline int
zpl_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,void * data)521 zpl_write_cache_pages(struct address_space *mapping,
522     struct writeback_control *wbc, void *data)
523 {
524 	int result;
525 
526 #ifdef HAVE_WRITEPAGE_T_FOLIO
527 	result = write_cache_pages(mapping, wbc, zpl_putfolio, data);
528 #else
529 	result = write_cache_pages(mapping, wbc, zpl_putpage, data);
530 #endif
531 	return (result);
532 }
533 #else
534 static inline int
zpl_write_cache_pages(struct address_space * mapping,struct writeback_control * wbc,void * data)535 zpl_write_cache_pages(struct address_space *mapping,
536     struct writeback_control *wbc, void *data)
537 {
538 	pgoff_t start = wbc->range_start >> PAGE_SHIFT;
539 	pgoff_t end = wbc->range_end >> PAGE_SHIFT;
540 
541 	struct folio_batch fbatch;
542 	folio_batch_init(&fbatch);
543 
544 	/*
545 	 * This atomically (-ish) tags all DIRTY pages in the range with
546 	 * TOWRITE, allowing users to continue dirtying or undirtying pages
547 	 * while we get on with writeback, without us treading on each other.
548 	 */
549 	tag_pages_for_writeback(mapping, start, end);
550 
551 	int err = 0;
552 	unsigned int npages;
553 
554 	/*
555 	 * Grab references to the TOWRITE pages just flagged. This may not get
556 	 * all of them, so we do it in a loop until there are none left.
557 	 */
558 	while ((npages = filemap_get_folios_tag(mapping, &start, end,
559 	    PAGECACHE_TAG_TOWRITE, &fbatch)) != 0) {
560 
561 		/* Loop over each page and write it out. */
562 		struct folio *folio;
563 		while ((folio = folio_batch_next(&fbatch)) != NULL) {
564 			folio_lock(folio);
565 
566 			/*
567 			 * If the folio has been remapped, or is no longer
568 			 * dirty, then there's nothing to do.
569 			 */
570 			if (folio->mapping != mapping ||
571 			    !folio_test_dirty(folio)) {
572 				folio_unlock(folio);
573 				continue;
574 			}
575 
576 			/*
577 			 * If writeback is already in progress, wait for it to
578 			 * finish. We continue after this even if the page
579 			 * ends up clean; zfs_putpage() will skip it if no
580 			 * further work is required.
581 			 */
582 			while (folio_test_writeback(folio))
583 				folio_wait_bit(folio, PG_writeback);
584 
585 			/*
586 			 * Write it out and collect any error. zfs_putpage()
587 			 * will clear the TOWRITE and DIRTY flags, and return
588 			 * with the page unlocked.
589 			 */
590 			int ferr = zpl_putpage(&folio->page, wbc, data);
591 			if (err == 0 && ferr != 0)
592 				err = ferr;
593 
594 			/* Housekeeping for the caller. */
595 			wbc->nr_to_write -= folio_nr_pages(folio);
596 		}
597 
598 		/* Release any remaining references on the batch. */
599 		folio_batch_release(&fbatch);
600 	}
601 
602 	return (err);
603 }
604 #endif
605 
606 static int
zpl_writepages(struct address_space * mapping,struct writeback_control * wbc)607 zpl_writepages(struct address_space *mapping, struct writeback_control *wbc)
608 {
609 	znode_t		*zp = ITOZ(mapping->host);
610 	zfsvfs_t	*zfsvfs = ITOZSB(mapping->host);
611 	enum writeback_sync_modes sync_mode;
612 	int result;
613 
614 	if ((result = zpl_enter(zfsvfs, FTAG)) != 0)
615 		return (result);
616 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
617 		wbc->sync_mode = WB_SYNC_ALL;
618 	zpl_exit(zfsvfs, FTAG);
619 	sync_mode = wbc->sync_mode;
620 
621 	/*
622 	 * We don't want to run write_cache_pages() in SYNC mode here, because
623 	 * that would make putpage() wait for a single page to be committed to
624 	 * disk every single time, resulting in atrocious performance. Instead
625 	 * we run it once in non-SYNC mode so that the ZIL gets all the data,
626 	 * and then we commit it all in one go.
627 	 */
628 	boolean_t for_sync = (sync_mode == WB_SYNC_ALL);
629 	wbc->sync_mode = WB_SYNC_NONE;
630 	result = zpl_write_cache_pages(mapping, wbc, &for_sync);
631 	if (sync_mode != wbc->sync_mode) {
632 		if ((result = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
633 			return (result);
634 
635 		if (zfsvfs->z_log != NULL) {
636 			/*
637 			 * We don't want to block here if the pool suspends,
638 			 * because this is not a syncing op by itself, but
639 			 * might be part of one that the caller will
640 			 * coordinate.
641 			 */
642 			result = -zil_commit_flags(zfsvfs->z_log, zp->z_id,
643 			    ZIL_COMMIT_NOW);
644 		}
645 
646 		zpl_exit(zfsvfs, FTAG);
647 
648 		/*
649 		 * If zil_commit_flags() failed, it's unclear what state things
650 		 * are currently in. putpage() has written back out what it can
651 		 * to the DMU, but it may not be on disk. We have little choice
652 		 * but to escape.
653 		 */
654 		if (result != 0)
655 			return (result);
656 
657 		/*
658 		 * We need to call write_cache_pages() again (we can't just
659 		 * return after the commit) because the previous call in
660 		 * non-SYNC mode does not guarantee that we got all the dirty
661 		 * pages (see the implementation of write_cache_pages() for
662 		 * details). That being said, this is a no-op in most cases.
663 		 */
664 		wbc->sync_mode = sync_mode;
665 		result = zpl_write_cache_pages(mapping, wbc, &for_sync);
666 	}
667 	return (result);
668 }
669 
670 #ifdef HAVE_VFS_WRITEPAGE
671 /*
672  * Write out dirty pages to the ARC, this function is only required to
673  * support mmap(2).  Mapped pages may be dirtied by memory operations
674  * which never call .write().  These dirty pages are kept in sync with
675  * the ARC buffers via this hook.
676  */
677 static int
zpl_writepage(struct page * pp,struct writeback_control * wbc)678 zpl_writepage(struct page *pp, struct writeback_control *wbc)
679 {
680 	if (ITOZSB(pp->mapping->host)->z_os->os_sync == ZFS_SYNC_ALWAYS)
681 		wbc->sync_mode = WB_SYNC_ALL;
682 
683 	boolean_t for_sync = (wbc->sync_mode == WB_SYNC_ALL);
684 
685 	return (zpl_putpage(pp, wbc, &for_sync));
686 }
687 #endif
688 
689 /*
690  * The flag combination which matches the behavior of zfs_space() is
691  * FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE.  The FALLOC_FL_PUNCH_HOLE
692  * flag was introduced in the 2.6.38 kernel.
693  *
694  * The original mode=0 (allocate space) behavior can be reasonably emulated
695  * by checking if enough space exists and creating a sparse file, as real
696  * persistent space reservation is not possible due to COW, snapshots, etc.
697  */
698 static long
zpl_fallocate_common(struct inode * ip,int mode,loff_t offset,loff_t len)699 zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len)
700 {
701 	cred_t *cr = CRED();
702 	znode_t *zp = ITOZ(ip);
703 	zfsvfs_t *zfsvfs = ITOZSB(ip);
704 	loff_t olen;
705 	fstrans_cookie_t cookie;
706 	int error = 0;
707 
708 	int test_mode = FALLOC_FL_PUNCH_HOLE | FALLOC_FL_ZERO_RANGE;
709 
710 	if ((mode & ~(FALLOC_FL_KEEP_SIZE | test_mode)) != 0)
711 		return (-EOPNOTSUPP);
712 
713 	if (offset < 0 || len <= 0)
714 		return (-EINVAL);
715 
716 	spl_inode_lock(ip);
717 	olen = i_size_read(ip);
718 
719 	crhold(cr);
720 	cookie = spl_fstrans_mark();
721 	if (mode & (test_mode)) {
722 		flock64_t bf;
723 
724 		if (mode & FALLOC_FL_KEEP_SIZE) {
725 			if (offset > olen)
726 				goto out_unmark;
727 
728 			if (offset + len > olen)
729 				len = olen - offset;
730 		}
731 		bf.l_type = F_WRLCK;
732 		bf.l_whence = SEEK_SET;
733 		bf.l_start = offset;
734 		bf.l_len = len;
735 		bf.l_pid = 0;
736 
737 		error = -zfs_space(zp, F_FREESP, &bf, O_RDWR, offset, cr);
738 	} else if ((mode & ~FALLOC_FL_KEEP_SIZE) == 0) {
739 		unsigned int percent = zfs_fallocate_reserve_percent;
740 		struct kstatfs statfs;
741 
742 		/* Legacy mode, disable fallocate compatibility. */
743 		if (percent == 0) {
744 			error = -EOPNOTSUPP;
745 			goto out_unmark;
746 		}
747 
748 		/*
749 		 * Use zfs_statvfs() instead of dmu_objset_space() since it
750 		 * also checks project quota limits, which are relevant here.
751 		 */
752 		error = -zfs_statvfs(ip, &statfs);
753 		if (error)
754 			goto out_unmark;
755 
756 		/*
757 		 * Shrink available space a bit to account for overhead/races.
758 		 * We know the product previously fit into availbytes from
759 		 * dmu_objset_space(), so the smaller product will also fit.
760 		 */
761 		if (len > statfs.f_bavail * (statfs.f_bsize * 100 / percent)) {
762 			error = -ENOSPC;
763 			goto out_unmark;
764 		}
765 		if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > olen) {
766 			error = zpl_enter_verify_zp(zfsvfs, zp, FTAG);
767 			if (error)
768 				goto out_unmark;
769 
770 			/*
771 			 * extend file: log=TRUE drives z_seq bump,
772 			 * mtime/ctime advance, and TX_TRUNCATE ZIL
773 			 * record; matches zfs_space().
774 			 */
775 			error = -zfs_freesp(zp, offset + len, 0, 0, TRUE);
776 			zfs_exit(zfsvfs, FTAG);
777 		}
778 	}
779 out_unmark:
780 	spl_fstrans_unmark(cookie);
781 	spl_inode_unlock(ip);
782 
783 	crfree(cr);
784 
785 	return (error);
786 }
787 
788 static long
zpl_fallocate(struct file * filp,int mode,loff_t offset,loff_t len)789 zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
790 {
791 	return zpl_fallocate_common(file_inode(filp),
792 	    mode, offset, len);
793 }
794 
795 static int
zpl_ioctl_getversion(struct file * filp,void __user * arg)796 zpl_ioctl_getversion(struct file *filp, void __user *arg)
797 {
798 	uint32_t generation = file_inode(filp)->i_generation;
799 
800 	return (copy_to_user(arg, &generation, sizeof (generation)));
801 }
802 
803 static int
zpl_fadvise(struct file * filp,loff_t offset,loff_t len,int advice)804 zpl_fadvise(struct file *filp, loff_t offset, loff_t len, int advice)
805 {
806 	struct inode *ip = file_inode(filp);
807 	znode_t *zp = ITOZ(ip);
808 	zfsvfs_t *zfsvfs = ITOZSB(ip);
809 	objset_t *os = zfsvfs->z_os;
810 	int error = 0;
811 
812 	if (S_ISFIFO(ip->i_mode))
813 		return (-ESPIPE);
814 
815 	if (offset < 0 || len < 0)
816 		return (-EINVAL);
817 
818 	if ((error = zpl_enter_verify_zp(zfsvfs, zp, FTAG)) != 0)
819 		return (error);
820 
821 	if (advice == POSIX_FADV_WILLNEED) {
822 		loff_t rlen = len ? len : i_size_read(ip) - offset;
823 		dmu_prefetch(os, zp->z_id, 0, offset, rlen,
824 		    ZIO_PRIORITY_ASYNC_READ);
825 		if (!zn_has_cached_data(zp, offset, offset + rlen - 1)) {
826 			zfs_exit(zfsvfs, FTAG);
827 			return (error);
828 		}
829 	}
830 
831 #ifdef HAVE_GENERIC_FADVISE
832 	error = generic_fadvise(filp, offset, len, advice);
833 #endif
834 
835 	if (error == 0 && advice == POSIX_FADV_DONTNEED) {
836 		loff_t rlen = len ? len : i_size_read(ip) - offset;
837 		dmu_evict_range(os, zp->z_id, offset, rlen);
838 	}
839 
840 	zfs_exit(zfsvfs, FTAG);
841 
842 	return (error);
843 }
844 
845 #define	ZFS_FL_USER_VISIBLE	(FS_FL_USER_VISIBLE | FS_PROJINHERIT_FL)
846 #define	ZFS_FL_USER_MODIFIABLE	(FS_FL_USER_MODIFIABLE | FS_PROJINHERIT_FL)
847 
848 
849 static struct {
850 	uint64_t zfs_flag;
851 	uint32_t fs_flag;
852 	uint32_t xflag;
853 } flags_lookup[] = {
854 	{ZFS_IMMUTABLE, FS_IMMUTABLE_FL, FS_XFLAG_IMMUTABLE},
855 	{ZFS_APPENDONLY, FS_APPEND_FL, FS_XFLAG_APPEND},
856 	{ZFS_NODUMP, FS_NODUMP_FL, FS_XFLAG_NODUMP},
857 	{ZFS_PROJINHERIT, FS_PROJINHERIT_FL, FS_XFLAG_PROJINHERIT}
858 };
859 
860 static uint32_t
__zpl_ioctl_getflags(struct inode * ip)861 __zpl_ioctl_getflags(struct inode *ip)
862 {
863 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
864 	uint32_t ioctl_flags = 0;
865 	for (int i = 0; i < ARRAY_SIZE(flags_lookup); i++)
866 		if (zfs_flags & flags_lookup[i].zfs_flag)
867 			ioctl_flags |= flags_lookup[i].fs_flag;
868 
869 	return (ioctl_flags);
870 }
871 
872 static uint32_t
__zpl_ioctl_getxflags(struct inode * ip)873 __zpl_ioctl_getxflags(struct inode *ip)
874 {
875 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
876 	uint32_t ioctl_flags = 0;
877 
878 	for (int i = 0; i < ARRAY_SIZE(flags_lookup); i++)
879 		if (zfs_flags & flags_lookup[i].zfs_flag)
880 			ioctl_flags |= flags_lookup[i].xflag;
881 
882 	return (ioctl_flags);
883 }
884 
885 /*
886  * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
887  * attributes common to both Linux and Solaris are mapped.
888  */
889 static int
zpl_ioctl_getflags(struct file * filp,void __user * arg)890 zpl_ioctl_getflags(struct file *filp, void __user *arg)
891 {
892 	uint32_t flags;
893 	int err;
894 
895 	flags = __zpl_ioctl_getflags(file_inode(filp));
896 	flags = flags & ZFS_FL_USER_VISIBLE;
897 	err = copy_to_user(arg, &flags, sizeof (flags));
898 
899 	return (err);
900 }
901 
902 /*
903  * fchange() is a helper macro to detect if we have been asked to change a
904  * flag. This is ugly, but the requirement that we do this is a consequence of
905  * how the Linux file attribute interface was designed. Another consequence is
906  * that concurrent modification of files suffers from a TOCTOU race. Neither
907  * are things we can fix without modifying the kernel-userland interface, which
908  * is outside of our jurisdiction.
909  */
910 
911 #define	fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
912 
913 static int
__zpl_ioctl_setflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)914 __zpl_ioctl_setflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
915 {
916 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
917 	xoptattr_t *xoap;
918 
919 	if (ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL |
920 	    FS_PROJINHERIT_FL))
921 		return (-EOPNOTSUPP);
922 
923 	if (ioctl_flags & ~ZFS_FL_USER_MODIFIABLE)
924 		return (-EACCES);
925 
926 	if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) ||
927 	    fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) &&
928 	    !capable(CAP_LINUX_IMMUTABLE))
929 		return (-EPERM);
930 
931 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
932 		return (-EACCES);
933 
934 	xva_init(xva);
935 	xoap = xva_getxoptattr(xva);
936 
937 #define	FLAG_CHANGE(iflag, zflag, xflag, xfield)	do {	\
938 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) ||	\
939 	    ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) {	\
940 		XVA_SET_REQ(xva, (xflag));	\
941 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
942 	}	\
943 } while (0)
944 
945 	FLAG_CHANGE(FS_IMMUTABLE_FL, ZFS_IMMUTABLE, XAT_IMMUTABLE,
946 	    xoap->xoa_immutable);
947 	FLAG_CHANGE(FS_APPEND_FL, ZFS_APPENDONLY, XAT_APPENDONLY,
948 	    xoap->xoa_appendonly);
949 	FLAG_CHANGE(FS_NODUMP_FL, ZFS_NODUMP, XAT_NODUMP,
950 	    xoap->xoa_nodump);
951 	FLAG_CHANGE(FS_PROJINHERIT_FL, ZFS_PROJINHERIT, XAT_PROJINHERIT,
952 	    xoap->xoa_projinherit);
953 
954 #undef	FLAG_CHANGE
955 
956 	return (0);
957 }
958 
959 static int
__zpl_ioctl_setxflags(struct inode * ip,uint32_t ioctl_flags,xvattr_t * xva)960 __zpl_ioctl_setxflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
961 {
962 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
963 	xoptattr_t *xoap;
964 
965 	if (ioctl_flags & ~(FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND |
966 	    FS_XFLAG_NODUMP | FS_XFLAG_PROJINHERIT))
967 		return (-EOPNOTSUPP);
968 
969 	if ((fchange(ioctl_flags, zfs_flags, FS_XFLAG_IMMUTABLE,
970 	    ZFS_IMMUTABLE) ||
971 	    fchange(ioctl_flags, zfs_flags, FS_XFLAG_APPEND, ZFS_APPENDONLY)) &&
972 	    !capable(CAP_LINUX_IMMUTABLE))
973 		return (-EPERM);
974 
975 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
976 		return (-EACCES);
977 
978 	xva_init(xva);
979 	xoap = xva_getxoptattr(xva);
980 
981 #define	FLAG_CHANGE(iflag, zflag, xflag, xfield)	do {	\
982 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (zflag))) ||	\
983 	    ((zfs_flags & (zflag)) && !(ioctl_flags & (iflag)))) {	\
984 		XVA_SET_REQ(xva, (xflag));	\
985 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
986 	}	\
987 } while (0)
988 
989 	FLAG_CHANGE(FS_XFLAG_IMMUTABLE, ZFS_IMMUTABLE, XAT_IMMUTABLE,
990 	    xoap->xoa_immutable);
991 	FLAG_CHANGE(FS_XFLAG_APPEND, ZFS_APPENDONLY, XAT_APPENDONLY,
992 	    xoap->xoa_appendonly);
993 	FLAG_CHANGE(FS_XFLAG_NODUMP, ZFS_NODUMP, XAT_NODUMP,
994 	    xoap->xoa_nodump);
995 	FLAG_CHANGE(FS_XFLAG_PROJINHERIT, ZFS_PROJINHERIT, XAT_PROJINHERIT,
996 	    xoap->xoa_projinherit);
997 
998 #undef	FLAG_CHANGE
999 
1000 	return (0);
1001 }
1002 
1003 static int
zpl_ioctl_setflags(struct file * filp,void __user * arg)1004 zpl_ioctl_setflags(struct file *filp, void __user *arg)
1005 {
1006 	struct inode *ip = file_inode(filp);
1007 	uint32_t flags;
1008 	cred_t *cr = CRED();
1009 	xvattr_t xva;
1010 	int err;
1011 	fstrans_cookie_t cookie;
1012 
1013 	if (copy_from_user(&flags, arg, sizeof (flags)))
1014 		return (-EFAULT);
1015 
1016 	err = __zpl_ioctl_setflags(ip, flags, &xva);
1017 	if (err)
1018 		return (err);
1019 
1020 	crhold(cr);
1021 	cookie = spl_fstrans_mark();
1022 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
1023 	spl_fstrans_unmark(cookie);
1024 	crfree(cr);
1025 
1026 	return (err);
1027 }
1028 
1029 static int
zpl_ioctl_getxattr(struct file * filp,void __user * arg)1030 zpl_ioctl_getxattr(struct file *filp, void __user *arg)
1031 {
1032 	zfsxattr_t fsx = { 0 };
1033 	struct inode *ip = file_inode(filp);
1034 	int err;
1035 
1036 	fsx.fsx_xflags = __zpl_ioctl_getxflags(ip);
1037 	fsx.fsx_projid = ITOZ(ip)->z_projid;
1038 	err = copy_to_user(arg, &fsx, sizeof (fsx));
1039 
1040 	return (err);
1041 }
1042 
1043 static int
zpl_ioctl_setxattr(struct file * filp,void __user * arg)1044 zpl_ioctl_setxattr(struct file *filp, void __user *arg)
1045 {
1046 	struct inode *ip = file_inode(filp);
1047 	zfsxattr_t fsx;
1048 	cred_t *cr = CRED();
1049 	xvattr_t xva;
1050 	xoptattr_t *xoap;
1051 	int err;
1052 	fstrans_cookie_t cookie;
1053 
1054 	if (copy_from_user(&fsx, arg, sizeof (fsx)))
1055 		return (-EFAULT);
1056 
1057 	if (!zpl_is_valid_projid(fsx.fsx_projid))
1058 		return (-EINVAL);
1059 
1060 	err = __zpl_ioctl_setxflags(ip, fsx.fsx_xflags, &xva);
1061 	if (err)
1062 		return (err);
1063 
1064 	xoap = xva_getxoptattr(&xva);
1065 	XVA_SET_REQ(&xva, XAT_PROJID);
1066 	xoap->xoa_projid = fsx.fsx_projid;
1067 
1068 	crhold(cr);
1069 	cookie = spl_fstrans_mark();
1070 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
1071 	spl_fstrans_unmark(cookie);
1072 	crfree(cr);
1073 
1074 	return (err);
1075 }
1076 
1077 /*
1078  * Expose Additional File Level Attributes of ZFS.
1079  */
1080 static int
zpl_ioctl_getdosflags(struct file * filp,void __user * arg)1081 zpl_ioctl_getdosflags(struct file *filp, void __user *arg)
1082 {
1083 	struct inode *ip = file_inode(filp);
1084 	uint64_t dosflags = ITOZ(ip)->z_pflags;
1085 	dosflags &= ZFS_DOS_FL_USER_VISIBLE;
1086 	int err = copy_to_user(arg, &dosflags, sizeof (dosflags));
1087 
1088 	return (err);
1089 }
1090 
1091 static int
__zpl_ioctl_setdosflags(struct inode * ip,uint64_t ioctl_flags,xvattr_t * xva)1092 __zpl_ioctl_setdosflags(struct inode *ip, uint64_t ioctl_flags, xvattr_t *xva)
1093 {
1094 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
1095 	xoptattr_t *xoap;
1096 
1097 	if (ioctl_flags & (~ZFS_DOS_FL_USER_VISIBLE))
1098 		return (-EOPNOTSUPP);
1099 
1100 	if ((fchange(ioctl_flags, zfs_flags, ZFS_IMMUTABLE, ZFS_IMMUTABLE) ||
1101 	    fchange(ioctl_flags, zfs_flags, ZFS_APPENDONLY, ZFS_APPENDONLY)) &&
1102 	    !capable(CAP_LINUX_IMMUTABLE))
1103 		return (-EPERM);
1104 
1105 	if (!zpl_inode_owner_or_capable(zfs_init_idmap, ip))
1106 		return (-EACCES);
1107 
1108 	xva_init(xva);
1109 	xoap = xva_getxoptattr(xva);
1110 
1111 #define	FLAG_CHANGE(iflag, xflag, xfield)	do {	\
1112 	if (((ioctl_flags & (iflag)) && !(zfs_flags & (iflag))) ||	\
1113 	    ((zfs_flags & (iflag)) && !(ioctl_flags & (iflag)))) {	\
1114 		XVA_SET_REQ(xva, (xflag));	\
1115 		(xfield) = ((ioctl_flags & (iflag)) != 0);	\
1116 	}	\
1117 } while (0)
1118 
1119 	FLAG_CHANGE(ZFS_IMMUTABLE, XAT_IMMUTABLE, xoap->xoa_immutable);
1120 	FLAG_CHANGE(ZFS_APPENDONLY, XAT_APPENDONLY, xoap->xoa_appendonly);
1121 	FLAG_CHANGE(ZFS_NODUMP, XAT_NODUMP, xoap->xoa_nodump);
1122 	FLAG_CHANGE(ZFS_READONLY, XAT_READONLY, xoap->xoa_readonly);
1123 	FLAG_CHANGE(ZFS_HIDDEN, XAT_HIDDEN, xoap->xoa_hidden);
1124 	FLAG_CHANGE(ZFS_SYSTEM, XAT_SYSTEM, xoap->xoa_system);
1125 	FLAG_CHANGE(ZFS_ARCHIVE, XAT_ARCHIVE, xoap->xoa_archive);
1126 	FLAG_CHANGE(ZFS_NOUNLINK, XAT_NOUNLINK, xoap->xoa_nounlink);
1127 	FLAG_CHANGE(ZFS_REPARSE, XAT_REPARSE, xoap->xoa_reparse);
1128 	FLAG_CHANGE(ZFS_OFFLINE, XAT_OFFLINE, xoap->xoa_offline);
1129 	FLAG_CHANGE(ZFS_SPARSE, XAT_SPARSE, xoap->xoa_sparse);
1130 
1131 #undef	FLAG_CHANGE
1132 
1133 	return (0);
1134 }
1135 
1136 /*
1137  * Set Additional File Level Attributes of ZFS.
1138  */
1139 static int
zpl_ioctl_setdosflags(struct file * filp,void __user * arg)1140 zpl_ioctl_setdosflags(struct file *filp, void __user *arg)
1141 {
1142 	struct inode *ip = file_inode(filp);
1143 	uint64_t dosflags;
1144 	cred_t *cr = CRED();
1145 	xvattr_t xva;
1146 	int err;
1147 	fstrans_cookie_t cookie;
1148 
1149 	if (copy_from_user(&dosflags, arg, sizeof (dosflags)))
1150 		return (-EFAULT);
1151 
1152 	err = __zpl_ioctl_setdosflags(ip, dosflags, &xva);
1153 	if (err)
1154 		return (err);
1155 
1156 	crhold(cr);
1157 	cookie = spl_fstrans_mark();
1158 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr, zfs_init_idmap);
1159 	spl_fstrans_unmark(cookie);
1160 	crfree(cr);
1161 
1162 	return (err);
1163 }
1164 
1165 static int
zpl_ioctl_rewrite(struct file * filp,void __user * arg)1166 zpl_ioctl_rewrite(struct file *filp, void __user *arg)
1167 {
1168 	struct inode *ip = file_inode(filp);
1169 	zfs_rewrite_args_t args;
1170 	fstrans_cookie_t cookie;
1171 	int err;
1172 
1173 	if (copy_from_user(&args, arg, sizeof (args)))
1174 		return (-EFAULT);
1175 
1176 	if (unlikely(!(filp->f_mode & FMODE_WRITE)))
1177 		return (-EBADF);
1178 
1179 	cookie = spl_fstrans_mark();
1180 	err = -zfs_rewrite(ITOZ(ip), args.off, args.len, args.flags, args.arg);
1181 	spl_fstrans_unmark(cookie);
1182 
1183 	return (err);
1184 }
1185 
1186 static long
zpl_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1187 zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1188 {
1189 	switch (cmd) {
1190 	case FS_IOC_GETVERSION:
1191 		return (zpl_ioctl_getversion(filp, (void *)arg));
1192 	case FS_IOC_GETFLAGS:
1193 		return (zpl_ioctl_getflags(filp, (void *)arg));
1194 	case FS_IOC_SETFLAGS:
1195 		return (zpl_ioctl_setflags(filp, (void *)arg));
1196 	case ZFS_IOC_FSGETXATTR:
1197 		return (zpl_ioctl_getxattr(filp, (void *)arg));
1198 	case ZFS_IOC_FSSETXATTR:
1199 		return (zpl_ioctl_setxattr(filp, (void *)arg));
1200 	case ZFS_IOC_GETDOSFLAGS:
1201 		return (zpl_ioctl_getdosflags(filp, (void *)arg));
1202 	case ZFS_IOC_SETDOSFLAGS:
1203 		return (zpl_ioctl_setdosflags(filp, (void *)arg));
1204 	case ZFS_IOC_REWRITE:
1205 		return (zpl_ioctl_rewrite(filp, (void *)arg));
1206 	default:
1207 		return (-ENOTTY);
1208 	}
1209 }
1210 
1211 #ifdef CONFIG_COMPAT
1212 static long
zpl_compat_ioctl(struct file * filp,unsigned int cmd,unsigned long arg)1213 zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
1214 {
1215 	switch (cmd) {
1216 	case FS_IOC32_GETVERSION:
1217 		cmd = FS_IOC_GETVERSION;
1218 		break;
1219 	case FS_IOC32_GETFLAGS:
1220 		cmd = FS_IOC_GETFLAGS;
1221 		break;
1222 	case FS_IOC32_SETFLAGS:
1223 		cmd = FS_IOC_SETFLAGS;
1224 		break;
1225 	default:
1226 		return (-ENOTTY);
1227 	}
1228 	return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)));
1229 }
1230 #endif /* CONFIG_COMPAT */
1231 
1232 const struct address_space_operations zpl_address_space_operations = {
1233 #ifdef HAVE_VFS_READPAGES
1234 	.readpages	= zpl_readpages,
1235 #else
1236 	.readahead	= zpl_readahead,
1237 #endif
1238 #ifdef HAVE_VFS_READ_FOLIO
1239 	.read_folio	= zpl_read_folio,
1240 #else
1241 	.readpage	= zpl_readpage,
1242 #endif
1243 #ifdef HAVE_VFS_WRITEPAGE
1244 	.writepage	= zpl_writepage,
1245 #endif
1246 	.writepages	= zpl_writepages,
1247 	.direct_IO	= zpl_direct_IO,
1248 #ifdef HAVE_VFS_SET_PAGE_DIRTY_NOBUFFERS
1249 	.set_page_dirty = __set_page_dirty_nobuffers,
1250 #endif
1251 #ifdef HAVE_VFS_FILEMAP_DIRTY_FOLIO
1252 	.dirty_folio	= filemap_dirty_folio,
1253 #endif
1254 #ifdef HAVE_VFS_MIGRATE_FOLIO
1255 	.migrate_folio	= migrate_folio,
1256 #elif defined(HAVE_VFS_MIGRATEPAGE)
1257 	.migratepage	= migrate_page,
1258 #endif
1259 };
1260 
1261 const struct file_operations zpl_file_operations = {
1262 	.open		= zpl_open,
1263 	.release	= zpl_release,
1264 	.llseek		= zpl_llseek,
1265 	.read_iter	= zpl_iter_read,
1266 	.write_iter	= zpl_iter_write,
1267 #ifdef HAVE_COPY_SPLICE_READ
1268 	.splice_read	= copy_splice_read,
1269 #else
1270 	.splice_read	= generic_file_splice_read,
1271 #endif
1272 	.splice_write	= iter_file_splice_write,
1273 	.mmap		= zpl_mmap,
1274 	.fsync		= zpl_fsync,
1275 	.fallocate	= zpl_fallocate,
1276 	.setlease	= generic_setlease,
1277 	.copy_file_range	= zpl_copy_file_range,
1278 #ifdef HAVE_VFS_CLONE_FILE_RANGE
1279 	.clone_file_range	= zpl_clone_file_range,
1280 #endif
1281 #ifdef HAVE_VFS_REMAP_FILE_RANGE
1282 	.remap_file_range	= zpl_remap_file_range,
1283 #endif
1284 #ifdef HAVE_VFS_DEDUPE_FILE_RANGE
1285 	.dedupe_file_range	= zpl_dedupe_file_range,
1286 #endif
1287 	.fadvise	= zpl_fadvise,
1288 	.unlocked_ioctl	= zpl_ioctl,
1289 #ifdef CONFIG_COMPAT
1290 	.compat_ioctl	= zpl_compat_ioctl,
1291 #endif
1292 };
1293 
1294 const struct file_operations zpl_dir_file_operations = {
1295 	.llseek		= generic_file_llseek,
1296 	.read		= generic_read_dir,
1297 	.iterate_shared	= zpl_iterate,
1298 	.fsync		= zpl_fsync,
1299 	.setlease	= generic_setlease,
1300 	.unlocked_ioctl = zpl_ioctl,
1301 #ifdef CONFIG_COMPAT
1302 	.compat_ioctl   = zpl_compat_ioctl,
1303 #endif
1304 };
1305 
1306 module_param(zfs_fallocate_reserve_percent, uint, 0644);
1307 MODULE_PARM_DESC(zfs_fallocate_reserve_percent,
1308 	"Percentage of length to use for the available capacity check");
1309