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.
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.
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]
22 * Copyright (c) 2011, Lawrence Livermore National Security, LLC.
23 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
28 #include <linux/compat.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>
38 * When using fallocate(2) to preallocate space, inflate the requested
39 * capacity check by 10% to account for the required metadata blocks.
41 unsigned int zfs_fallocate_reserve_percent
= 110;
44 zpl_open(struct inode
*ip
, struct file
*filp
)
48 fstrans_cookie_t cookie
;
50 error
= generic_file_open(ip
, filp
);
55 cookie
= spl_fstrans_mark();
56 error
= -zfs_open(ip
, filp
->f_mode
, filp
->f_flags
, cr
);
57 spl_fstrans_unmark(cookie
);
59 ASSERT3S(error
, <=, 0);
65 zpl_release(struct inode
*ip
, struct file
*filp
)
69 fstrans_cookie_t cookie
;
71 cookie
= spl_fstrans_mark();
72 if (ITOZ(ip
)->z_atime_dirty
)
73 zfs_mark_inode_dirty(ip
);
76 error
= -zfs_close(ip
, filp
->f_flags
, cr
);
77 spl_fstrans_unmark(cookie
);
79 ASSERT3S(error
, <=, 0);
85 zpl_iterate(struct file
*filp
, zpl_dir_context_t
*ctx
)
89 fstrans_cookie_t cookie
;
92 cookie
= spl_fstrans_mark();
93 error
= -zfs_readdir(file_inode(filp
), ctx
, cr
);
94 spl_fstrans_unmark(cookie
);
96 ASSERT3S(error
, <=, 0);
101 #if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
103 zpl_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
105 zpl_dir_context_t ctx
=
106 ZPL_DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
109 error
= zpl_iterate(filp
, &ctx
);
110 filp
->f_pos
= ctx
.pos
;
114 #endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
116 #if defined(HAVE_FSYNC_WITHOUT_DENTRY)
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.
124 zpl_fsync(struct file
*filp
, int datasync
)
126 struct inode
*inode
= filp
->f_mapping
->host
;
129 fstrans_cookie_t cookie
;
132 cookie
= spl_fstrans_mark();
133 error
= -zfs_fsync(ITOZ(inode
), datasync
, cr
);
134 spl_fstrans_unmark(cookie
);
136 ASSERT3S(error
, <=, 0);
141 #ifdef HAVE_FILE_AIO_FSYNC
143 zpl_aio_fsync(struct kiocb
*kiocb
, int datasync
)
145 return (zpl_fsync(kiocb
->ki_filp
, datasync
));
149 #elif defined(HAVE_FSYNC_RANGE)
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.
158 zpl_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
160 struct inode
*inode
= filp
->f_mapping
->host
;
163 fstrans_cookie_t cookie
;
165 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
170 cookie
= spl_fstrans_mark();
171 error
= -zfs_fsync(ITOZ(inode
), datasync
, cr
);
172 spl_fstrans_unmark(cookie
);
174 ASSERT3S(error
, <=, 0);
179 #ifdef HAVE_FILE_AIO_FSYNC
181 zpl_aio_fsync(struct kiocb
*kiocb
, int datasync
)
183 return (zpl_fsync(kiocb
->ki_filp
, kiocb
->ki_pos
, -1, datasync
));
188 #error "Unsupported fops->fsync() implementation"
192 zfs_io_flags(struct kiocb
*kiocb
)
196 #if defined(IOCB_DSYNC)
197 if (kiocb
->ki_flags
& IOCB_DSYNC
)
200 #if defined(IOCB_SYNC)
201 if (kiocb
->ki_flags
& IOCB_SYNC
)
204 #if defined(IOCB_APPEND)
205 if (kiocb
->ki_flags
& IOCB_APPEND
)
208 #if defined(IOCB_DIRECT)
209 if (kiocb
->ki_flags
& IOCB_DIRECT
)
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.
223 zpl_file_accessed(struct file
*filp
)
225 struct inode
*ip
= filp
->f_mapping
->host
;
227 if (!IS_NOATIME(ip
) && ITOZSB(ip
)->z_relatime
) {
228 if (zfs_relatime_need_update(ip
))
235 #if defined(HAVE_VFS_RW_ITERATE)
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.
245 zpl_uio_init(zfs_uio_t
*uio
, struct kiocb
*kiocb
, struct iov_iter
*to
,
246 loff_t pos
, ssize_t count
, size_t skip
)
248 #if defined(HAVE_VFS_IOV_ITER)
249 zfs_uio_iov_iter_init(uio
, to
, pos
, count
, skip
);
251 zfs_uio_iovec_init(uio
, to
->iov
, to
->nr_segs
, pos
,
252 to
->type
& ITER_KVEC
? UIO_SYSSPACE
: UIO_USERSPACE
,
258 zpl_iter_read(struct kiocb
*kiocb
, struct iov_iter
*to
)
261 fstrans_cookie_t cookie
;
262 struct file
*filp
= kiocb
->ki_filp
;
263 ssize_t count
= iov_iter_count(to
);
266 zpl_uio_init(&uio
, kiocb
, to
, kiocb
->ki_pos
, count
, 0);
269 cookie
= spl_fstrans_mark();
271 int error
= -zfs_read(ITOZ(filp
->f_mapping
->host
), &uio
,
272 filp
->f_flags
| zfs_io_flags(kiocb
), cr
);
274 spl_fstrans_unmark(cookie
);
280 ssize_t read
= count
- uio
.uio_resid
;
281 kiocb
->ki_pos
+= read
;
283 zpl_file_accessed(filp
);
288 static inline ssize_t
289 zpl_generic_write_checks(struct kiocb
*kiocb
, struct iov_iter
*from
,
292 #ifdef HAVE_GENERIC_WRITE_CHECKS_KIOCB
293 ssize_t ret
= generic_write_checks(kiocb
, from
);
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
);
304 *countp
= iov_iter_count(from
);
305 ssize_t ret
= generic_write_checks(file
, &kiocb
->ki_pos
, countp
, isblk
);
314 zpl_iter_write(struct kiocb
*kiocb
, struct iov_iter
*from
)
317 fstrans_cookie_t cookie
;
318 struct file
*filp
= kiocb
->ki_filp
;
319 struct inode
*ip
= filp
->f_mapping
->host
;
324 ret
= zpl_generic_write_checks(kiocb
, from
, &count
);
328 zpl_uio_init(&uio
, kiocb
, from
, kiocb
->ki_pos
, count
, from
->iov_offset
);
331 cookie
= spl_fstrans_mark();
333 int error
= -zfs_write(ITOZ(ip
), &uio
,
334 filp
->f_flags
| zfs_io_flags(kiocb
), cr
);
336 spl_fstrans_unmark(cookie
);
342 ssize_t wrote
= count
- uio
.uio_resid
;
343 kiocb
->ki_pos
+= wrote
;
346 iov_iter_advance(from
, wrote
);
351 #else /* !HAVE_VFS_RW_ITERATE */
354 zpl_aio_read(struct kiocb
*kiocb
, const struct iovec
*iov
,
355 unsigned long nr_segs
, loff_t pos
)
358 fstrans_cookie_t cookie
;
359 struct file
*filp
= kiocb
->ki_filp
;
363 ret
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_WRITE
);
368 zfs_uio_iovec_init(&uio
, iov
, nr_segs
, kiocb
->ki_pos
, UIO_USERSPACE
,
372 cookie
= spl_fstrans_mark();
374 int error
= -zfs_read(ITOZ(filp
->f_mapping
->host
), &uio
,
375 filp
->f_flags
| zfs_io_flags(kiocb
), cr
);
377 spl_fstrans_unmark(cookie
);
383 ssize_t read
= count
- uio
.uio_resid
;
384 kiocb
->ki_pos
+= read
;
386 zpl_file_accessed(filp
);
392 zpl_aio_write(struct kiocb
*kiocb
, const struct iovec
*iov
,
393 unsigned long nr_segs
, loff_t pos
)
396 fstrans_cookie_t cookie
;
397 struct file
*filp
= kiocb
->ki_filp
;
398 struct inode
*ip
= filp
->f_mapping
->host
;
402 ret
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_READ
);
406 ret
= generic_write_checks(filp
, &pos
, &count
, S_ISBLK(ip
->i_mode
));
411 zfs_uio_iovec_init(&uio
, iov
, nr_segs
, kiocb
->ki_pos
, UIO_USERSPACE
,
415 cookie
= spl_fstrans_mark();
417 int error
= -zfs_write(ITOZ(ip
), &uio
,
418 filp
->f_flags
| zfs_io_flags(kiocb
), cr
);
420 spl_fstrans_unmark(cookie
);
426 ssize_t wrote
= count
- uio
.uio_resid
;
427 kiocb
->ki_pos
+= wrote
;
431 #endif /* HAVE_VFS_RW_ITERATE */
433 #if defined(HAVE_VFS_RW_ITERATE)
435 zpl_direct_IO_impl(int rw
, struct kiocb
*kiocb
, struct iov_iter
*iter
)
438 return (zpl_iter_write(kiocb
, iter
));
440 return (zpl_iter_read(kiocb
, iter
));
442 #if defined(HAVE_VFS_DIRECT_IO_ITER)
444 zpl_direct_IO(struct kiocb
*kiocb
, struct iov_iter
*iter
)
446 return (zpl_direct_IO_impl(iov_iter_rw(iter
), kiocb
, iter
));
448 #elif defined(HAVE_VFS_DIRECT_IO_ITER_OFFSET)
450 zpl_direct_IO(struct kiocb
*kiocb
, struct iov_iter
*iter
, loff_t pos
)
452 ASSERT3S(pos
, ==, kiocb
->ki_pos
);
453 return (zpl_direct_IO_impl(iov_iter_rw(iter
), kiocb
, iter
));
455 #elif defined(HAVE_VFS_DIRECT_IO_ITER_RW_OFFSET)
457 zpl_direct_IO(int rw
, struct kiocb
*kiocb
, struct iov_iter
*iter
, loff_t pos
)
459 ASSERT3S(pos
, ==, kiocb
->ki_pos
);
460 return (zpl_direct_IO_impl(rw
, kiocb
, iter
));
463 #error "Unknown direct IO interface"
466 #else /* HAVE_VFS_RW_ITERATE */
468 #if defined(HAVE_VFS_DIRECT_IO_IOVEC)
470 zpl_direct_IO(int rw
, struct kiocb
*kiocb
, const struct iovec
*iov
,
471 loff_t pos
, unsigned long nr_segs
)
474 return (zpl_aio_write(kiocb
, iov
, nr_segs
, pos
));
476 return (zpl_aio_read(kiocb
, iov
, nr_segs
, pos
));
478 #elif defined(HAVE_VFS_DIRECT_IO_ITER_RW_OFFSET)
480 zpl_direct_IO(int rw
, struct kiocb
*kiocb
, struct iov_iter
*iter
, loff_t pos
)
482 const struct iovec
*iovp
= iov_iter_iovec(iter
);
483 unsigned long nr_segs
= iter
->nr_segs
;
485 ASSERT3S(pos
, ==, kiocb
->ki_pos
);
487 return (zpl_aio_write(kiocb
, iovp
, nr_segs
, pos
));
489 return (zpl_aio_read(kiocb
, iovp
, nr_segs
, pos
));
492 #error "Unknown direct IO interface"
495 #endif /* HAVE_VFS_RW_ITERATE */
498 zpl_llseek(struct file
*filp
, loff_t offset
, int whence
)
500 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
501 fstrans_cookie_t cookie
;
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
;
508 spl_inode_lock_shared(ip
);
509 cookie
= spl_fstrans_mark();
510 error
= -zfs_holey(ITOZ(ip
), whence
, &offset
);
511 spl_fstrans_unmark(cookie
);
513 error
= lseek_execute(filp
, ip
, offset
, maxbytes
);
514 spl_inode_unlock_shared(ip
);
518 #endif /* SEEK_HOLE && SEEK_DATA */
520 return (generic_file_llseek(filp
, offset
, whence
));
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.
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.
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.
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.
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.
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.
567 zpl_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
569 struct inode
*ip
= filp
->f_mapping
->host
;
570 znode_t
*zp
= ITOZ(ip
);
572 fstrans_cookie_t cookie
;
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
);
581 error
= generic_file_mmap(filp
, vma
);
585 mutex_enter(&zp
->z_lock
);
586 zp
->z_is_mapped
= B_TRUE
;
587 mutex_exit(&zp
->z_lock
);
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().
598 zpl_readpage(struct file
*filp
, struct page
*pp
)
603 fstrans_cookie_t cookie
;
605 ASSERT(PageLocked(pp
));
606 ip
= pp
->mapping
->host
;
609 cookie
= spl_fstrans_mark();
610 error
= -zfs_getpage(ip
, pl
, 1);
611 spl_fstrans_unmark(cookie
);
615 ClearPageUptodate(pp
);
619 flush_dcache_page(pp
);
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().
633 zpl_readpages(struct file
*filp
, struct address_space
*mapping
,
634 struct list_head
*pages
, unsigned nr_pages
)
636 return (read_cache_pages(mapping
, pages
,
637 (filler_t
*)zpl_readpage
, filp
));
641 zpl_putpage(struct page
*pp
, struct writeback_control
*wbc
, void *data
)
643 struct address_space
*mapping
= data
;
644 fstrans_cookie_t cookie
;
646 ASSERT(PageLocked(pp
));
647 ASSERT(!PageWriteback(pp
));
649 cookie
= spl_fstrans_mark();
650 (void) zfs_putpage(mapping
->host
, pp
, wbc
);
651 spl_fstrans_unmark(cookie
);
657 zpl_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
659 znode_t
*zp
= ITOZ(mapping
->host
);
660 zfsvfs_t
*zfsvfs
= ITOZSB(mapping
->host
);
661 enum writeback_sync_modes sync_mode
;
665 if (zfsvfs
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
666 wbc
->sync_mode
= WB_SYNC_ALL
;
668 sync_mode
= wbc
->sync_mode
;
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.
677 wbc
->sync_mode
= WB_SYNC_NONE
;
678 result
= write_cache_pages(mapping
, wbc
, zpl_putpage
, mapping
);
679 if (sync_mode
!= wbc
->sync_mode
) {
682 if (zfsvfs
->z_log
!= NULL
)
683 zil_commit(zfsvfs
->z_log
, zp
->z_id
);
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.
693 wbc
->sync_mode
= sync_mode
;
694 result
= write_cache_pages(mapping
, wbc
, zpl_putpage
, mapping
);
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.
706 zpl_writepage(struct page
*pp
, struct writeback_control
*wbc
)
708 if (ITOZSB(pp
->mapping
->host
)->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
709 wbc
->sync_mode
= WB_SYNC_ALL
;
711 return (zpl_putpage(pp
, wbc
, pp
->mapping
));
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.
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.
724 zpl_fallocate_common(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
728 fstrans_cookie_t cookie
;
731 if ((mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
)) != 0)
732 return (-EOPNOTSUPP
);
734 if (offset
< 0 || len
<= 0)
738 olen
= i_size_read(ip
);
741 cookie
= spl_fstrans_mark();
742 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
748 if (offset
+ len
> olen
)
751 bf
.l_whence
= SEEK_SET
;
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
;
761 /* Legacy mode, disable fallocate compatibility. */
768 * Use zfs_statvfs() instead of dmu_objset_space() since it
769 * also checks project quota limits, which are relevant here.
771 error
= zfs_statvfs(ip
, &statfs
);
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.
780 if (len
> statfs
.f_bavail
* (statfs
.f_bsize
* 100 / percent
)) {
784 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && offset
+ len
> olen
)
785 error
= zfs_freesp(ITOZ(ip
), offset
+ len
, 0, 0, FALSE
);
788 spl_fstrans_unmark(cookie
);
789 spl_inode_unlock(ip
);
797 zpl_fallocate(struct file
*filp
, int mode
, loff_t offset
, loff_t len
)
799 return zpl_fallocate_common(file_inode(filp
),
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)
807 __zpl_ioctl_getflags(struct inode
*ip
)
809 uint64_t zfs_flags
= ITOZ(ip
)->z_pflags
;
810 uint32_t ioctl_flags
= 0;
812 if (zfs_flags
& ZFS_IMMUTABLE
)
813 ioctl_flags
|= FS_IMMUTABLE_FL
;
815 if (zfs_flags
& ZFS_APPENDONLY
)
816 ioctl_flags
|= FS_APPEND_FL
;
818 if (zfs_flags
& ZFS_NODUMP
)
819 ioctl_flags
|= FS_NODUMP_FL
;
821 if (zfs_flags
& ZFS_PROJINHERIT
)
822 ioctl_flags
|= ZFS_PROJINHERIT_FL
;
824 return (ioctl_flags
& ZFS_FL_USER_VISIBLE
);
828 * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
829 * attributes common to both Linux and Solaris are mapped.
832 zpl_ioctl_getflags(struct file
*filp
, void __user
*arg
)
837 flags
= __zpl_ioctl_getflags(file_inode(filp
));
838 err
= copy_to_user(arg
, &flags
, sizeof (flags
));
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.
852 #define fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
855 __zpl_ioctl_setflags(struct inode
*ip
, uint32_t ioctl_flags
, xvattr_t
*xva
)
857 uint64_t zfs_flags
= ITOZ(ip
)->z_pflags
;
860 if (ioctl_flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| FS_NODUMP_FL
|
862 return (-EOPNOTSUPP
);
864 if (ioctl_flags
& ~ZFS_FL_USER_MODIFIABLE
)
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
))
872 if (!inode_owner_or_capable(ip
))
876 xoap
= xva_getxoptattr(xva
);
878 XVA_SET_REQ(xva
, XAT_IMMUTABLE
);
879 if (ioctl_flags
& FS_IMMUTABLE_FL
)
880 xoap
->xoa_immutable
= B_TRUE
;
882 XVA_SET_REQ(xva
, XAT_APPENDONLY
);
883 if (ioctl_flags
& FS_APPEND_FL
)
884 xoap
->xoa_appendonly
= B_TRUE
;
886 XVA_SET_REQ(xva
, XAT_NODUMP
);
887 if (ioctl_flags
& FS_NODUMP_FL
)
888 xoap
->xoa_nodump
= B_TRUE
;
890 XVA_SET_REQ(xva
, XAT_PROJINHERIT
);
891 if (ioctl_flags
& ZFS_PROJINHERIT_FL
)
892 xoap
->xoa_projinherit
= B_TRUE
;
898 zpl_ioctl_setflags(struct file
*filp
, void __user
*arg
)
900 struct inode
*ip
= file_inode(filp
);
905 fstrans_cookie_t cookie
;
907 if (copy_from_user(&flags
, arg
, sizeof (flags
)))
910 err
= __zpl_ioctl_setflags(ip
, flags
, &xva
);
915 cookie
= spl_fstrans_mark();
916 err
= -zfs_setattr(ITOZ(ip
), (vattr_t
*)&xva
, 0, cr
);
917 spl_fstrans_unmark(cookie
);
924 zpl_ioctl_getxattr(struct file
*filp
, void __user
*arg
)
926 zfsxattr_t fsx
= { 0 };
927 struct inode
*ip
= file_inode(filp
);
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
));
938 zpl_ioctl_setxattr(struct file
*filp
, void __user
*arg
)
940 struct inode
*ip
= file_inode(filp
);
946 fstrans_cookie_t cookie
;
948 if (copy_from_user(&fsx
, arg
, sizeof (fsx
)))
951 if (!zpl_is_valid_projid(fsx
.fsx_projid
))
954 err
= __zpl_ioctl_setflags(ip
, fsx
.fsx_xflags
, &xva
);
958 xoap
= xva_getxoptattr(&xva
);
959 XVA_SET_REQ(&xva
, XAT_PROJID
);
960 xoap
->xoa_projid
= fsx
.fsx_projid
;
963 cookie
= spl_fstrans_mark();
964 err
= -zfs_setattr(ITOZ(ip
), (vattr_t
*)&xva
, 0, cr
);
965 spl_fstrans_unmark(cookie
);
972 zpl_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
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
));
990 zpl_compat_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
993 case FS_IOC32_GETFLAGS
:
994 cmd
= FS_IOC_GETFLAGS
;
996 case FS_IOC32_SETFLAGS
:
997 cmd
= FS_IOC_SETFLAGS
;
1002 return (zpl_ioctl(filp
, cmd
, (unsigned long)compat_ptr(arg
)));
1004 #endif /* CONFIG_COMPAT */
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
,
1015 const struct file_operations zpl_file_operations
= {
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
,
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
,
1031 .read
= do_sync_read
,
1032 .write
= do_sync_write
,
1033 .aio_read
= zpl_aio_read
,
1034 .aio_write
= zpl_aio_write
,
1038 #ifdef HAVE_FILE_AIO_FSYNC
1039 .aio_fsync
= zpl_aio_fsync
,
1041 .fallocate
= zpl_fallocate
,
1042 .unlocked_ioctl
= zpl_ioctl
,
1043 #ifdef CONFIG_COMPAT
1044 .compat_ioctl
= zpl_compat_ioctl
,
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
,
1056 .readdir
= zpl_readdir
,
1059 .unlocked_ioctl
= zpl_ioctl
,
1060 #ifdef CONFIG_COMPAT
1061 .compat_ioctl
= zpl_compat_ioctl
,
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");