xref: /freebsd/sys/contrib/openzfs/module/os/linux/zfs/zpl_file.c (revision 90b5fc95832da64a5f56295e687379732c33718f)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
23  * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
24  */
25 
26 
27 #ifdef CONFIG_COMPAT
28 #include <linux/compat.h>
29 #endif
30 #include <sys/file.h>
31 #include <sys/dmu_objset.h>
32 #include <sys/zfs_znode.h>
33 #include <sys/zfs_vfsops.h>
34 #include <sys/zfs_vnops.h>
35 #include <sys/zfs_project.h>
36 
37 /*
38  * When using fallocate(2) to preallocate space, inflate the requested
39  * capacity check by 10% to account for the required metadata blocks.
40  */
41 unsigned int zfs_fallocate_reserve_percent = 110;
42 
43 static int
44 zpl_open(struct inode *ip, struct file *filp)
45 {
46 	cred_t *cr = CRED();
47 	int error;
48 	fstrans_cookie_t cookie;
49 
50 	error = generic_file_open(ip, filp);
51 	if (error)
52 		return (error);
53 
54 	crhold(cr);
55 	cookie = spl_fstrans_mark();
56 	error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr);
57 	spl_fstrans_unmark(cookie);
58 	crfree(cr);
59 	ASSERT3S(error, <=, 0);
60 
61 	return (error);
62 }
63 
64 static int
65 zpl_release(struct inode *ip, struct file *filp)
66 {
67 	cred_t *cr = CRED();
68 	int error;
69 	fstrans_cookie_t cookie;
70 
71 	cookie = spl_fstrans_mark();
72 	if (ITOZ(ip)->z_atime_dirty)
73 		zfs_mark_inode_dirty(ip);
74 
75 	crhold(cr);
76 	error = -zfs_close(ip, filp->f_flags, cr);
77 	spl_fstrans_unmark(cookie);
78 	crfree(cr);
79 	ASSERT3S(error, <=, 0);
80 
81 	return (error);
82 }
83 
84 static int
85 zpl_iterate(struct file *filp, zpl_dir_context_t *ctx)
86 {
87 	cred_t *cr = CRED();
88 	int error;
89 	fstrans_cookie_t cookie;
90 
91 	crhold(cr);
92 	cookie = spl_fstrans_mark();
93 	error = -zfs_readdir(file_inode(filp), ctx, cr);
94 	spl_fstrans_unmark(cookie);
95 	crfree(cr);
96 	ASSERT3S(error, <=, 0);
97 
98 	return (error);
99 }
100 
101 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
102 static int
103 zpl_readdir(struct file *filp, void *dirent, filldir_t filldir)
104 {
105 	zpl_dir_context_t ctx =
106 	    ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
107 	int error;
108 
109 	error = zpl_iterate(filp, &ctx);
110 	filp->f_pos = ctx.pos;
111 
112 	return (error);
113 }
114 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
115 
116 #if defined(HAVE_FSYNC_WITHOUT_DENTRY)
117 /*
118  * Linux 2.6.35 - 3.0 API,
119  * As of 2.6.35 the dentry argument to the fops->fsync() hook was deemed
120  * redundant.  The dentry is still accessible via filp->f_path.dentry,
121  * and we are guaranteed that filp will never be NULL.
122  */
123 static int
124 zpl_fsync(struct file *filp, int datasync)
125 {
126 	struct inode *inode = filp->f_mapping->host;
127 	cred_t *cr = CRED();
128 	int error;
129 	fstrans_cookie_t cookie;
130 
131 	crhold(cr);
132 	cookie = spl_fstrans_mark();
133 	error = -zfs_fsync(ITOZ(inode), datasync, cr);
134 	spl_fstrans_unmark(cookie);
135 	crfree(cr);
136 	ASSERT3S(error, <=, 0);
137 
138 	return (error);
139 }
140 
141 #ifdef HAVE_FILE_AIO_FSYNC
142 static int
143 zpl_aio_fsync(struct kiocb *kiocb, int datasync)
144 {
145 	return (zpl_fsync(kiocb->ki_filp, datasync));
146 }
147 #endif
148 
149 #elif defined(HAVE_FSYNC_RANGE)
150 /*
151  * Linux 3.1 - 3.x API,
152  * As of 3.1 the responsibility to call filemap_write_and_wait_range() has
153  * been pushed down in to the .fsync() vfs hook.  Additionally, the i_mutex
154  * lock is no longer held by the caller, for zfs we don't require the lock
155  * to be held so we don't acquire it.
156  */
157 static int
158 zpl_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
159 {
160 	struct inode *inode = filp->f_mapping->host;
161 	cred_t *cr = CRED();
162 	int error;
163 	fstrans_cookie_t cookie;
164 
165 	error = filemap_write_and_wait_range(inode->i_mapping, start, end);
166 	if (error)
167 		return (error);
168 
169 	crhold(cr);
170 	cookie = spl_fstrans_mark();
171 	error = -zfs_fsync(ITOZ(inode), datasync, cr);
172 	spl_fstrans_unmark(cookie);
173 	crfree(cr);
174 	ASSERT3S(error, <=, 0);
175 
176 	return (error);
177 }
178 
179 #ifdef HAVE_FILE_AIO_FSYNC
180 static int
181 zpl_aio_fsync(struct kiocb *kiocb, int datasync)
182 {
183 	return (zpl_fsync(kiocb->ki_filp, kiocb->ki_pos, -1, datasync));
184 }
185 #endif
186 
187 #else
188 #error "Unsupported fops->fsync() implementation"
189 #endif
190 
191 static inline int
192 zfs_io_flags(struct kiocb *kiocb)
193 {
194 	int flags = 0;
195 
196 #if defined(IOCB_DSYNC)
197 	if (kiocb->ki_flags & IOCB_DSYNC)
198 		flags |= O_DSYNC;
199 #endif
200 #if defined(IOCB_SYNC)
201 	if (kiocb->ki_flags & IOCB_SYNC)
202 		flags |= O_SYNC;
203 #endif
204 #if defined(IOCB_APPEND)
205 	if (kiocb->ki_flags & IOCB_APPEND)
206 		flags |= O_APPEND;
207 #endif
208 #if defined(IOCB_DIRECT)
209 	if (kiocb->ki_flags & IOCB_DIRECT)
210 		flags |= O_DIRECT;
211 #endif
212 	return (flags);
213 }
214 
215 /*
216  * If relatime is enabled, call file_accessed() if zfs_relatime_need_update()
217  * is true.  This is needed since datasets with inherited "relatime" property
218  * aren't necessarily mounted with the MNT_RELATIME flag (e.g. after
219  * `zfs set relatime=...`), which is what relatime test in VFS by
220  * relatime_need_update() is based on.
221  */
222 static inline void
223 zpl_file_accessed(struct file *filp)
224 {
225 	struct inode *ip = filp->f_mapping->host;
226 
227 	if (!IS_NOATIME(ip) && ITOZSB(ip)->z_relatime) {
228 		if (zfs_relatime_need_update(ip))
229 			file_accessed(filp);
230 	} else {
231 		file_accessed(filp);
232 	}
233 }
234 
235 #if defined(HAVE_VFS_RW_ITERATE)
236 
237 /*
238  * When HAVE_VFS_IOV_ITER is defined the iov_iter structure supports
239  * iovecs, kvevs, bvecs and pipes, plus all the required interfaces to
240  * manipulate the iov_iter are available.  In which case the full iov_iter
241  * can be attached to the uio and correctly handled in the lower layers.
242  * Otherwise, for older kernels extract the iovec and pass it instead.
243  */
244 static void
245 zpl_uio_init(uio_t *uio, struct kiocb *kiocb, struct iov_iter *to,
246     loff_t pos, ssize_t count, size_t skip)
247 {
248 #if defined(HAVE_VFS_IOV_ITER)
249 	uio_iov_iter_init(uio, to, pos, count, skip);
250 #else
251 	uio_iovec_init(uio, to->iov, to->nr_segs, pos,
252 	    to->type & ITER_KVEC ? UIO_SYSSPACE : UIO_USERSPACE,
253 	    count, skip);
254 #endif
255 }
256 
257 static ssize_t
258 zpl_iter_read(struct kiocb *kiocb, struct iov_iter *to)
259 {
260 	cred_t *cr = CRED();
261 	fstrans_cookie_t cookie;
262 	struct file *filp = kiocb->ki_filp;
263 	ssize_t count = iov_iter_count(to);
264 	uio_t uio;
265 
266 	zpl_uio_init(&uio, kiocb, to, kiocb->ki_pos, count, 0);
267 
268 	crhold(cr);
269 	cookie = spl_fstrans_mark();
270 
271 	int error = -zfs_read(ITOZ(filp->f_mapping->host), &uio,
272 	    filp->f_flags | zfs_io_flags(kiocb), cr);
273 
274 	spl_fstrans_unmark(cookie);
275 	crfree(cr);
276 
277 	if (error < 0)
278 		return (error);
279 
280 	ssize_t read = count - uio.uio_resid;
281 	kiocb->ki_pos += read;
282 
283 	zpl_file_accessed(filp);
284 
285 	return (read);
286 }
287 
288 static inline ssize_t
289 zpl_generic_write_checks(struct kiocb *kiocb, struct iov_iter *from,
290     size_t *countp)
291 {
292 #ifdef HAVE_GENERIC_WRITE_CHECKS_KIOCB
293 	ssize_t ret = generic_write_checks(kiocb, from);
294 	if (ret <= 0)
295 		return (ret);
296 
297 	*countp = ret;
298 #else
299 	struct file *file = kiocb->ki_filp;
300 	struct address_space *mapping = file->f_mapping;
301 	struct inode *ip = mapping->host;
302 	int isblk = S_ISBLK(ip->i_mode);
303 
304 	*countp = iov_iter_count(from);
305 	ssize_t ret = generic_write_checks(file, &kiocb->ki_pos, countp, isblk);
306 	if (ret)
307 		return (ret);
308 #endif
309 
310 	return (0);
311 }
312 
313 static ssize_t
314 zpl_iter_write(struct kiocb *kiocb, struct iov_iter *from)
315 {
316 	cred_t *cr = CRED();
317 	fstrans_cookie_t cookie;
318 	struct file *filp = kiocb->ki_filp;
319 	struct inode *ip = filp->f_mapping->host;
320 	uio_t uio;
321 	size_t count = 0;
322 	ssize_t ret;
323 
324 	ret = zpl_generic_write_checks(kiocb, from, &count);
325 	if (ret)
326 		return (ret);
327 
328 	zpl_uio_init(&uio, kiocb, from, kiocb->ki_pos, count, from->iov_offset);
329 
330 	crhold(cr);
331 	cookie = spl_fstrans_mark();
332 
333 	int error = -zfs_write(ITOZ(ip), &uio,
334 	    filp->f_flags | zfs_io_flags(kiocb), cr);
335 
336 	spl_fstrans_unmark(cookie);
337 	crfree(cr);
338 
339 	if (error < 0)
340 		return (error);
341 
342 	ssize_t wrote = count - uio.uio_resid;
343 	kiocb->ki_pos += wrote;
344 
345 	if (wrote > 0)
346 		iov_iter_advance(from, wrote);
347 
348 	return (wrote);
349 }
350 
351 #else /* !HAVE_VFS_RW_ITERATE */
352 
353 static ssize_t
354 zpl_aio_read(struct kiocb *kiocb, const struct iovec *iov,
355     unsigned long nr_segs, loff_t pos)
356 {
357 	cred_t *cr = CRED();
358 	fstrans_cookie_t cookie;
359 	struct file *filp = kiocb->ki_filp;
360 	size_t count;
361 	ssize_t ret;
362 
363 	ret = generic_segment_checks(iov, &nr_segs, &count, VERIFY_WRITE);
364 	if (ret)
365 		return (ret);
366 
367 	uio_t uio;
368 	uio_iovec_init(&uio, iov, nr_segs, kiocb->ki_pos, UIO_USERSPACE,
369 	    count, 0);
370 
371 	crhold(cr);
372 	cookie = spl_fstrans_mark();
373 
374 	int error = -zfs_read(ITOZ(filp->f_mapping->host), &uio,
375 	    filp->f_flags | zfs_io_flags(kiocb), cr);
376 
377 	spl_fstrans_unmark(cookie);
378 	crfree(cr);
379 
380 	if (error < 0)
381 		return (error);
382 
383 	ssize_t read = count - uio.uio_resid;
384 	kiocb->ki_pos += read;
385 
386 	zpl_file_accessed(filp);
387 
388 	return (read);
389 }
390 
391 static ssize_t
392 zpl_aio_write(struct kiocb *kiocb, const struct iovec *iov,
393     unsigned long nr_segs, loff_t pos)
394 {
395 	cred_t *cr = CRED();
396 	fstrans_cookie_t cookie;
397 	struct file *filp = kiocb->ki_filp;
398 	struct inode *ip = filp->f_mapping->host;
399 	size_t count;
400 	ssize_t ret;
401 
402 	ret = generic_segment_checks(iov, &nr_segs, &count, VERIFY_READ);
403 	if (ret)
404 		return (ret);
405 
406 	ret = generic_write_checks(filp, &pos, &count, S_ISBLK(ip->i_mode));
407 	if (ret)
408 		return (ret);
409 
410 	uio_t uio;
411 	uio_iovec_init(&uio, iov, nr_segs, kiocb->ki_pos, UIO_USERSPACE,
412 	    count, 0);
413 
414 	crhold(cr);
415 	cookie = spl_fstrans_mark();
416 
417 	int error = -zfs_write(ITOZ(ip), &uio,
418 	    filp->f_flags | zfs_io_flags(kiocb), cr);
419 
420 	spl_fstrans_unmark(cookie);
421 	crfree(cr);
422 
423 	if (error < 0)
424 		return (error);
425 
426 	ssize_t wrote = count - uio.uio_resid;
427 	kiocb->ki_pos += wrote;
428 
429 	return (wrote);
430 }
431 #endif /* HAVE_VFS_RW_ITERATE */
432 
433 #if defined(HAVE_VFS_RW_ITERATE)
434 static ssize_t
435 zpl_direct_IO_impl(int rw, struct kiocb *kiocb, struct iov_iter *iter)
436 {
437 	if (rw == WRITE)
438 		return (zpl_iter_write(kiocb, iter));
439 	else
440 		return (zpl_iter_read(kiocb, iter));
441 }
442 #if defined(HAVE_VFS_DIRECT_IO_ITER)
443 static ssize_t
444 zpl_direct_IO(struct kiocb *kiocb, struct iov_iter *iter)
445 {
446 	return (zpl_direct_IO_impl(iov_iter_rw(iter), kiocb, iter));
447 }
448 #elif defined(HAVE_VFS_DIRECT_IO_ITER_OFFSET)
449 static ssize_t
450 zpl_direct_IO(struct kiocb *kiocb, struct iov_iter *iter, loff_t pos)
451 {
452 	ASSERT3S(pos, ==, kiocb->ki_pos);
453 	return (zpl_direct_IO_impl(iov_iter_rw(iter), kiocb, iter));
454 }
455 #elif defined(HAVE_VFS_DIRECT_IO_ITER_RW_OFFSET)
456 static ssize_t
457 zpl_direct_IO(int rw, struct kiocb *kiocb, struct iov_iter *iter, loff_t pos)
458 {
459 	ASSERT3S(pos, ==, kiocb->ki_pos);
460 	return (zpl_direct_IO_impl(rw, kiocb, iter));
461 }
462 #else
463 #error "Unknown direct IO interface"
464 #endif
465 
466 #else /* HAVE_VFS_RW_ITERATE */
467 
468 #if defined(HAVE_VFS_DIRECT_IO_IOVEC)
469 static ssize_t
470 zpl_direct_IO(int rw, struct kiocb *kiocb, const struct iovec *iov,
471     loff_t pos, unsigned long nr_segs)
472 {
473 	if (rw == WRITE)
474 		return (zpl_aio_write(kiocb, iov, nr_segs, pos));
475 	else
476 		return (zpl_aio_read(kiocb, iov, nr_segs, pos));
477 }
478 #elif defined(HAVE_VFS_DIRECT_IO_ITER_RW_OFFSET)
479 static ssize_t
480 zpl_direct_IO(int rw, struct kiocb *kiocb, struct iov_iter *iter, loff_t pos)
481 {
482 	const struct iovec *iovp = iov_iter_iovec(iter);
483 	unsigned long nr_segs = iter->nr_segs;
484 
485 	ASSERT3S(pos, ==, kiocb->ki_pos);
486 	if (rw == WRITE)
487 		return (zpl_aio_write(kiocb, iovp, nr_segs, pos));
488 	else
489 		return (zpl_aio_read(kiocb, iovp, nr_segs, pos));
490 }
491 #else
492 #error "Unknown direct IO interface"
493 #endif
494 
495 #endif /* HAVE_VFS_RW_ITERATE */
496 
497 static loff_t
498 zpl_llseek(struct file *filp, loff_t offset, int whence)
499 {
500 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
501 	fstrans_cookie_t cookie;
502 
503 	if (whence == SEEK_DATA || whence == SEEK_HOLE) {
504 		struct inode *ip = filp->f_mapping->host;
505 		loff_t maxbytes = ip->i_sb->s_maxbytes;
506 		loff_t error;
507 
508 		spl_inode_lock_shared(ip);
509 		cookie = spl_fstrans_mark();
510 		error = -zfs_holey(ITOZ(ip), whence, &offset);
511 		spl_fstrans_unmark(cookie);
512 		if (error == 0)
513 			error = lseek_execute(filp, ip, offset, maxbytes);
514 		spl_inode_unlock_shared(ip);
515 
516 		return (error);
517 	}
518 #endif /* SEEK_HOLE && SEEK_DATA */
519 
520 	return (generic_file_llseek(filp, offset, whence));
521 }
522 
523 /*
524  * It's worth taking a moment to describe how mmap is implemented
525  * for zfs because it differs considerably from other Linux filesystems.
526  * However, this issue is handled the same way under OpenSolaris.
527  *
528  * The issue is that by design zfs bypasses the Linux page cache and
529  * leaves all caching up to the ARC.  This has been shown to work
530  * well for the common read(2)/write(2) case.  However, mmap(2)
531  * is problem because it relies on being tightly integrated with the
532  * page cache.  To handle this we cache mmap'ed files twice, once in
533  * the ARC and a second time in the page cache.  The code is careful
534  * to keep both copies synchronized.
535  *
536  * When a file with an mmap'ed region is written to using write(2)
537  * both the data in the ARC and existing pages in the page cache
538  * are updated.  For a read(2) data will be read first from the page
539  * cache then the ARC if needed.  Neither a write(2) or read(2) will
540  * will ever result in new pages being added to the page cache.
541  *
542  * New pages are added to the page cache only via .readpage() which
543  * is called when the vfs needs to read a page off disk to back the
544  * virtual memory region.  These pages may be modified without
545  * notifying the ARC and will be written out periodically via
546  * .writepage().  This will occur due to either a sync or the usual
547  * page aging behavior.  Note because a read(2) of a mmap'ed file
548  * will always check the page cache first even when the ARC is out
549  * of date correct data will still be returned.
550  *
551  * While this implementation ensures correct behavior it does have
552  * have some drawbacks.  The most obvious of which is that it
553  * increases the required memory footprint when access mmap'ed
554  * files.  It also adds additional complexity to the code keeping
555  * both caches synchronized.
556  *
557  * Longer term it may be possible to cleanly resolve this wart by
558  * mapping page cache pages directly on to the ARC buffers.  The
559  * Linux address space operations are flexible enough to allow
560  * selection of which pages back a particular index.  The trick
561  * would be working out the details of which subsystem is in
562  * charge, the ARC, the page cache, or both.  It may also prove
563  * helpful to move the ARC buffers to a scatter-gather lists
564  * rather than a vmalloc'ed region.
565  */
566 static int
567 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
568 {
569 	struct inode *ip = filp->f_mapping->host;
570 	znode_t *zp = ITOZ(ip);
571 	int error;
572 	fstrans_cookie_t cookie;
573 
574 	cookie = spl_fstrans_mark();
575 	error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start,
576 	    (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags);
577 	spl_fstrans_unmark(cookie);
578 	if (error)
579 		return (error);
580 
581 	error = generic_file_mmap(filp, vma);
582 	if (error)
583 		return (error);
584 
585 	mutex_enter(&zp->z_lock);
586 	zp->z_is_mapped = B_TRUE;
587 	mutex_exit(&zp->z_lock);
588 
589 	return (error);
590 }
591 
592 /*
593  * Populate a page with data for the Linux page cache.  This function is
594  * only used to support mmap(2).  There will be an identical copy of the
595  * data in the ARC which is kept up to date via .write() and .writepage().
596  */
597 static int
598 zpl_readpage(struct file *filp, struct page *pp)
599 {
600 	struct inode *ip;
601 	struct page *pl[1];
602 	int error = 0;
603 	fstrans_cookie_t cookie;
604 
605 	ASSERT(PageLocked(pp));
606 	ip = pp->mapping->host;
607 	pl[0] = pp;
608 
609 	cookie = spl_fstrans_mark();
610 	error = -zfs_getpage(ip, pl, 1);
611 	spl_fstrans_unmark(cookie);
612 
613 	if (error) {
614 		SetPageError(pp);
615 		ClearPageUptodate(pp);
616 	} else {
617 		ClearPageError(pp);
618 		SetPageUptodate(pp);
619 		flush_dcache_page(pp);
620 	}
621 
622 	unlock_page(pp);
623 	return (error);
624 }
625 
626 /*
627  * Populate a set of pages with data for the Linux page cache.  This
628  * function will only be called for read ahead and never for demand
629  * paging.  For simplicity, the code relies on read_cache_pages() to
630  * correctly lock each page for IO and call zpl_readpage().
631  */
632 static int
633 zpl_readpages(struct file *filp, struct address_space *mapping,
634     struct list_head *pages, unsigned nr_pages)
635 {
636 	return (read_cache_pages(mapping, pages,
637 	    (filler_t *)zpl_readpage, filp));
638 }
639 
640 static int
641 zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data)
642 {
643 	struct address_space *mapping = data;
644 	fstrans_cookie_t cookie;
645 
646 	ASSERT(PageLocked(pp));
647 	ASSERT(!PageWriteback(pp));
648 
649 	cookie = spl_fstrans_mark();
650 	(void) zfs_putpage(mapping->host, pp, wbc);
651 	spl_fstrans_unmark(cookie);
652 
653 	return (0);
654 }
655 
656 static int
657 zpl_writepages(struct address_space *mapping, struct writeback_control *wbc)
658 {
659 	znode_t		*zp = ITOZ(mapping->host);
660 	zfsvfs_t	*zfsvfs = ITOZSB(mapping->host);
661 	enum writeback_sync_modes sync_mode;
662 	int result;
663 
664 	ZPL_ENTER(zfsvfs);
665 	if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
666 		wbc->sync_mode = WB_SYNC_ALL;
667 	ZPL_EXIT(zfsvfs);
668 	sync_mode = wbc->sync_mode;
669 
670 	/*
671 	 * We don't want to run write_cache_pages() in SYNC mode here, because
672 	 * that would make putpage() wait for a single page to be committed to
673 	 * disk every single time, resulting in atrocious performance. Instead
674 	 * we run it once in non-SYNC mode so that the ZIL gets all the data,
675 	 * and then we commit it all in one go.
676 	 */
677 	wbc->sync_mode = WB_SYNC_NONE;
678 	result = write_cache_pages(mapping, wbc, zpl_putpage, mapping);
679 	if (sync_mode != wbc->sync_mode) {
680 		ZPL_ENTER(zfsvfs);
681 		ZPL_VERIFY_ZP(zp);
682 		if (zfsvfs->z_log != NULL)
683 			zil_commit(zfsvfs->z_log, zp->z_id);
684 		ZPL_EXIT(zfsvfs);
685 
686 		/*
687 		 * We need to call write_cache_pages() again (we can't just
688 		 * return after the commit) because the previous call in
689 		 * non-SYNC mode does not guarantee that we got all the dirty
690 		 * pages (see the implementation of write_cache_pages() for
691 		 * details). That being said, this is a no-op in most cases.
692 		 */
693 		wbc->sync_mode = sync_mode;
694 		result = write_cache_pages(mapping, wbc, zpl_putpage, mapping);
695 	}
696 	return (result);
697 }
698 
699 /*
700  * Write out dirty pages to the ARC, this function is only required to
701  * support mmap(2).  Mapped pages may be dirtied by memory operations
702  * which never call .write().  These dirty pages are kept in sync with
703  * the ARC buffers via this hook.
704  */
705 static int
706 zpl_writepage(struct page *pp, struct writeback_control *wbc)
707 {
708 	if (ITOZSB(pp->mapping->host)->z_os->os_sync == ZFS_SYNC_ALWAYS)
709 		wbc->sync_mode = WB_SYNC_ALL;
710 
711 	return (zpl_putpage(pp, wbc, pp->mapping));
712 }
713 
714 /*
715  * The flag combination which matches the behavior of zfs_space() is
716  * FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE.  The FALLOC_FL_PUNCH_HOLE
717  * flag was introduced in the 2.6.38 kernel.
718  *
719  * The original mode=0 (allocate space) behavior can be reasonably emulated
720  * by checking if enough space exists and creating a sparse file, as real
721  * persistent space reservation is not possible due to COW, snapshots, etc.
722  */
723 static long
724 zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len)
725 {
726 	cred_t *cr = CRED();
727 	loff_t olen;
728 	fstrans_cookie_t cookie;
729 	int error = 0;
730 
731 	if ((mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE)) != 0)
732 		return (-EOPNOTSUPP);
733 
734 	if (offset < 0 || len <= 0)
735 		return (-EINVAL);
736 
737 	spl_inode_lock(ip);
738 	olen = i_size_read(ip);
739 
740 	crhold(cr);
741 	cookie = spl_fstrans_mark();
742 	if (mode & FALLOC_FL_PUNCH_HOLE) {
743 		flock64_t bf;
744 
745 		if (offset > olen)
746 			goto out_unmark;
747 
748 		if (offset + len > olen)
749 			len = olen - offset;
750 		bf.l_type = F_WRLCK;
751 		bf.l_whence = SEEK_SET;
752 		bf.l_start = offset;
753 		bf.l_len = len;
754 		bf.l_pid = 0;
755 
756 		error = -zfs_space(ITOZ(ip), F_FREESP, &bf, O_RDWR, offset, cr);
757 	} else if ((mode & ~FALLOC_FL_KEEP_SIZE) == 0) {
758 		unsigned int percent = zfs_fallocate_reserve_percent;
759 		struct kstatfs statfs;
760 
761 		/* Legacy mode, disable fallocate compatibility. */
762 		if (percent == 0) {
763 			error = -EOPNOTSUPP;
764 			goto out_unmark;
765 		}
766 
767 		/*
768 		 * Use zfs_statvfs() instead of dmu_objset_space() since it
769 		 * also checks project quota limits, which are relevant here.
770 		 */
771 		error = zfs_statvfs(ip, &statfs);
772 		if (error)
773 			goto out_unmark;
774 
775 		/*
776 		 * Shrink available space a bit to account for overhead/races.
777 		 * We know the product previously fit into availbytes from
778 		 * dmu_objset_space(), so the smaller product will also fit.
779 		 */
780 		if (len > statfs.f_bavail * (statfs.f_bsize * 100 / percent)) {
781 			error = -ENOSPC;
782 			goto out_unmark;
783 		}
784 		if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + len > olen)
785 			error = zfs_freesp(ITOZ(ip), offset + len, 0, 0, FALSE);
786 	}
787 out_unmark:
788 	spl_fstrans_unmark(cookie);
789 	spl_inode_unlock(ip);
790 
791 	crfree(cr);
792 
793 	return (error);
794 }
795 
796 static long
797 zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
798 {
799 	return zpl_fallocate_common(file_inode(filp),
800 	    mode, offset, len);
801 }
802 
803 #define	ZFS_FL_USER_VISIBLE	(FS_FL_USER_VISIBLE | ZFS_PROJINHERIT_FL)
804 #define	ZFS_FL_USER_MODIFIABLE	(FS_FL_USER_MODIFIABLE | ZFS_PROJINHERIT_FL)
805 
806 static uint32_t
807 __zpl_ioctl_getflags(struct inode *ip)
808 {
809 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
810 	uint32_t ioctl_flags = 0;
811 
812 	if (zfs_flags & ZFS_IMMUTABLE)
813 		ioctl_flags |= FS_IMMUTABLE_FL;
814 
815 	if (zfs_flags & ZFS_APPENDONLY)
816 		ioctl_flags |= FS_APPEND_FL;
817 
818 	if (zfs_flags & ZFS_NODUMP)
819 		ioctl_flags |= FS_NODUMP_FL;
820 
821 	if (zfs_flags & ZFS_PROJINHERIT)
822 		ioctl_flags |= ZFS_PROJINHERIT_FL;
823 
824 	return (ioctl_flags & ZFS_FL_USER_VISIBLE);
825 }
826 
827 /*
828  * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
829  * attributes common to both Linux and Solaris are mapped.
830  */
831 static int
832 zpl_ioctl_getflags(struct file *filp, void __user *arg)
833 {
834 	uint32_t flags;
835 	int err;
836 
837 	flags = __zpl_ioctl_getflags(file_inode(filp));
838 	err = copy_to_user(arg, &flags, sizeof (flags));
839 
840 	return (err);
841 }
842 
843 /*
844  * fchange() is a helper macro to detect if we have been asked to change a
845  * flag. This is ugly, but the requirement that we do this is a consequence of
846  * how the Linux file attribute interface was designed. Another consequence is
847  * that concurrent modification of files suffers from a TOCTOU race. Neither
848  * are things we can fix without modifying the kernel-userland interface, which
849  * is outside of our jurisdiction.
850  */
851 
852 #define	fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
853 
854 static int
855 __zpl_ioctl_setflags(struct inode *ip, uint32_t ioctl_flags, xvattr_t *xva)
856 {
857 	uint64_t zfs_flags = ITOZ(ip)->z_pflags;
858 	xoptattr_t *xoap;
859 
860 	if (ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL |
861 	    ZFS_PROJINHERIT_FL))
862 		return (-EOPNOTSUPP);
863 
864 	if (ioctl_flags & ~ZFS_FL_USER_MODIFIABLE)
865 		return (-EACCES);
866 
867 	if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) ||
868 	    fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) &&
869 	    !capable(CAP_LINUX_IMMUTABLE))
870 		return (-EACCES);
871 
872 	if (!inode_owner_or_capable(ip))
873 		return (-EACCES);
874 
875 	xva_init(xva);
876 	xoap = xva_getxoptattr(xva);
877 
878 	XVA_SET_REQ(xva, XAT_IMMUTABLE);
879 	if (ioctl_flags & FS_IMMUTABLE_FL)
880 		xoap->xoa_immutable = B_TRUE;
881 
882 	XVA_SET_REQ(xva, XAT_APPENDONLY);
883 	if (ioctl_flags & FS_APPEND_FL)
884 		xoap->xoa_appendonly = B_TRUE;
885 
886 	XVA_SET_REQ(xva, XAT_NODUMP);
887 	if (ioctl_flags & FS_NODUMP_FL)
888 		xoap->xoa_nodump = B_TRUE;
889 
890 	XVA_SET_REQ(xva, XAT_PROJINHERIT);
891 	if (ioctl_flags & ZFS_PROJINHERIT_FL)
892 		xoap->xoa_projinherit = B_TRUE;
893 
894 	return (0);
895 }
896 
897 static int
898 zpl_ioctl_setflags(struct file *filp, void __user *arg)
899 {
900 	struct inode *ip = file_inode(filp);
901 	uint32_t flags;
902 	cred_t *cr = CRED();
903 	xvattr_t xva;
904 	int err;
905 	fstrans_cookie_t cookie;
906 
907 	if (copy_from_user(&flags, arg, sizeof (flags)))
908 		return (-EFAULT);
909 
910 	err = __zpl_ioctl_setflags(ip, flags, &xva);
911 	if (err)
912 		return (err);
913 
914 	crhold(cr);
915 	cookie = spl_fstrans_mark();
916 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr);
917 	spl_fstrans_unmark(cookie);
918 	crfree(cr);
919 
920 	return (err);
921 }
922 
923 static int
924 zpl_ioctl_getxattr(struct file *filp, void __user *arg)
925 {
926 	zfsxattr_t fsx = { 0 };
927 	struct inode *ip = file_inode(filp);
928 	int err;
929 
930 	fsx.fsx_xflags = __zpl_ioctl_getflags(ip);
931 	fsx.fsx_projid = ITOZ(ip)->z_projid;
932 	err = copy_to_user(arg, &fsx, sizeof (fsx));
933 
934 	return (err);
935 }
936 
937 static int
938 zpl_ioctl_setxattr(struct file *filp, void __user *arg)
939 {
940 	struct inode *ip = file_inode(filp);
941 	zfsxattr_t fsx;
942 	cred_t *cr = CRED();
943 	xvattr_t xva;
944 	xoptattr_t *xoap;
945 	int err;
946 	fstrans_cookie_t cookie;
947 
948 	if (copy_from_user(&fsx, arg, sizeof (fsx)))
949 		return (-EFAULT);
950 
951 	if (!zpl_is_valid_projid(fsx.fsx_projid))
952 		return (-EINVAL);
953 
954 	err = __zpl_ioctl_setflags(ip, fsx.fsx_xflags, &xva);
955 	if (err)
956 		return (err);
957 
958 	xoap = xva_getxoptattr(&xva);
959 	XVA_SET_REQ(&xva, XAT_PROJID);
960 	xoap->xoa_projid = fsx.fsx_projid;
961 
962 	crhold(cr);
963 	cookie = spl_fstrans_mark();
964 	err = -zfs_setattr(ITOZ(ip), (vattr_t *)&xva, 0, cr);
965 	spl_fstrans_unmark(cookie);
966 	crfree(cr);
967 
968 	return (err);
969 }
970 
971 static long
972 zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
973 {
974 	switch (cmd) {
975 	case FS_IOC_GETFLAGS:
976 		return (zpl_ioctl_getflags(filp, (void *)arg));
977 	case FS_IOC_SETFLAGS:
978 		return (zpl_ioctl_setflags(filp, (void *)arg));
979 	case ZFS_IOC_FSGETXATTR:
980 		return (zpl_ioctl_getxattr(filp, (void *)arg));
981 	case ZFS_IOC_FSSETXATTR:
982 		return (zpl_ioctl_setxattr(filp, (void *)arg));
983 	default:
984 		return (-ENOTTY);
985 	}
986 }
987 
988 #ifdef CONFIG_COMPAT
989 static long
990 zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
991 {
992 	switch (cmd) {
993 	case FS_IOC32_GETFLAGS:
994 		cmd = FS_IOC_GETFLAGS;
995 		break;
996 	case FS_IOC32_SETFLAGS:
997 		cmd = FS_IOC_SETFLAGS;
998 		break;
999 	default:
1000 		return (-ENOTTY);
1001 	}
1002 	return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)));
1003 }
1004 #endif /* CONFIG_COMPAT */
1005 
1006 
1007 const struct address_space_operations zpl_address_space_operations = {
1008 	.readpages	= zpl_readpages,
1009 	.readpage	= zpl_readpage,
1010 	.writepage	= zpl_writepage,
1011 	.writepages	= zpl_writepages,
1012 	.direct_IO	= zpl_direct_IO,
1013 };
1014 
1015 const struct file_operations zpl_file_operations = {
1016 	.open		= zpl_open,
1017 	.release	= zpl_release,
1018 	.llseek		= zpl_llseek,
1019 #ifdef HAVE_VFS_RW_ITERATE
1020 #ifdef HAVE_NEW_SYNC_READ
1021 	.read		= new_sync_read,
1022 	.write		= new_sync_write,
1023 #endif
1024 	.read_iter	= zpl_iter_read,
1025 	.write_iter	= zpl_iter_write,
1026 #ifdef HAVE_VFS_IOV_ITER
1027 	.splice_read	= generic_file_splice_read,
1028 	.splice_write	= iter_file_splice_write,
1029 #endif
1030 #else
1031 	.read		= do_sync_read,
1032 	.write		= do_sync_write,
1033 	.aio_read	= zpl_aio_read,
1034 	.aio_write	= zpl_aio_write,
1035 #endif
1036 	.mmap		= zpl_mmap,
1037 	.fsync		= zpl_fsync,
1038 #ifdef HAVE_FILE_AIO_FSYNC
1039 	.aio_fsync	= zpl_aio_fsync,
1040 #endif
1041 	.fallocate	= zpl_fallocate,
1042 	.unlocked_ioctl	= zpl_ioctl,
1043 #ifdef CONFIG_COMPAT
1044 	.compat_ioctl	= zpl_compat_ioctl,
1045 #endif
1046 };
1047 
1048 const struct file_operations zpl_dir_file_operations = {
1049 	.llseek		= generic_file_llseek,
1050 	.read		= generic_read_dir,
1051 #if defined(HAVE_VFS_ITERATE_SHARED)
1052 	.iterate_shared	= zpl_iterate,
1053 #elif defined(HAVE_VFS_ITERATE)
1054 	.iterate	= zpl_iterate,
1055 #else
1056 	.readdir	= zpl_readdir,
1057 #endif
1058 	.fsync		= zpl_fsync,
1059 	.unlocked_ioctl = zpl_ioctl,
1060 #ifdef CONFIG_COMPAT
1061 	.compat_ioctl   = zpl_compat_ioctl,
1062 #endif
1063 };
1064 
1065 /* BEGIN CSTYLED */
1066 module_param(zfs_fallocate_reserve_percent, uint, 0644);
1067 MODULE_PARM_DESC(zfs_fallocate_reserve_percent,
1068     "Percentage of length to use for the available capacity check");
1069 /* END CSTYLED */
1070