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.
26 #include <sys/dmu_objset.h>
27 #include <sys/zfs_vfsops.h>
28 #include <sys/zfs_vnops.h>
29 #include <sys/zfs_znode.h>
34 zpl_open(struct inode
*ip
, struct file
*filp
)
38 fstrans_cookie_t cookie
;
40 error
= generic_file_open(ip
, filp
);
45 cookie
= spl_fstrans_mark();
46 error
= -zfs_open(ip
, filp
->f_mode
, filp
->f_flags
, cr
);
47 spl_fstrans_unmark(cookie
);
49 ASSERT3S(error
, <=, 0);
55 zpl_release(struct inode
*ip
, struct file
*filp
)
59 fstrans_cookie_t cookie
;
61 cookie
= spl_fstrans_mark();
62 if (ITOZ(ip
)->z_atime_dirty
)
63 zfs_mark_inode_dirty(ip
);
66 error
= -zfs_close(ip
, filp
->f_flags
, cr
);
67 spl_fstrans_unmark(cookie
);
69 ASSERT3S(error
, <=, 0);
75 zpl_iterate(struct file
*filp
, struct dir_context
*ctx
)
77 struct dentry
*dentry
= filp
->f_path
.dentry
;
80 fstrans_cookie_t cookie
;
83 cookie
= spl_fstrans_mark();
84 error
= -zfs_readdir(dentry
->d_inode
, ctx
, cr
);
85 spl_fstrans_unmark(cookie
);
87 ASSERT3S(error
, <=, 0);
92 #if !defined(HAVE_VFS_ITERATE)
94 zpl_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
96 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
99 error
= zpl_iterate(filp
, &ctx
);
100 filp
->f_pos
= ctx
.pos
;
104 #endif /* HAVE_VFS_ITERATE */
106 #if defined(HAVE_FSYNC_WITH_DENTRY)
108 * Linux 2.6.x - 2.6.34 API,
109 * Through 2.6.34 the nfsd kernel server would pass a NULL 'file struct *'
110 * to the fops->fsync() hook. For this reason, we must be careful not to
111 * use filp unconditionally.
114 zpl_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
118 fstrans_cookie_t cookie
;
121 cookie
= spl_fstrans_mark();
122 error
= -zfs_fsync(dentry
->d_inode
, datasync
, cr
);
123 spl_fstrans_unmark(cookie
);
125 ASSERT3S(error
, <=, 0);
131 zpl_aio_fsync(struct kiocb
*kiocb
, int datasync
)
133 struct file
*filp
= kiocb
->ki_filp
;
134 return (zpl_fsync(filp
, filp
->f_path
.dentry
, datasync
));
136 #elif defined(HAVE_FSYNC_WITHOUT_DENTRY)
138 * Linux 2.6.35 - 3.0 API,
139 * As of 2.6.35 the dentry argument to the fops->fsync() hook was deemed
140 * redundant. The dentry is still accessible via filp->f_path.dentry,
141 * and we are guaranteed that filp will never be NULL.
144 zpl_fsync(struct file
*filp
, int datasync
)
146 struct inode
*inode
= filp
->f_mapping
->host
;
149 fstrans_cookie_t cookie
;
152 cookie
= spl_fstrans_mark();
153 error
= -zfs_fsync(inode
, datasync
, cr
);
154 spl_fstrans_unmark(cookie
);
156 ASSERT3S(error
, <=, 0);
162 zpl_aio_fsync(struct kiocb
*kiocb
, int datasync
)
164 return (zpl_fsync(kiocb
->ki_filp
, datasync
));
166 #elif defined(HAVE_FSYNC_RANGE)
168 * Linux 3.1 - 3.x API,
169 * As of 3.1 the responsibility to call filemap_write_and_wait_range() has
170 * been pushed down in to the .fsync() vfs hook. Additionally, the i_mutex
171 * lock is no longer held by the caller, for zfs we don't require the lock
172 * to be held so we don't acquire it.
175 zpl_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
177 struct inode
*inode
= filp
->f_mapping
->host
;
180 fstrans_cookie_t cookie
;
182 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
187 cookie
= spl_fstrans_mark();
188 error
= -zfs_fsync(inode
, datasync
, cr
);
189 spl_fstrans_unmark(cookie
);
191 ASSERT3S(error
, <=, 0);
197 zpl_aio_fsync(struct kiocb
*kiocb
, int datasync
)
199 return (zpl_fsync(kiocb
->ki_filp
, kiocb
->ki_pos
,
200 kiocb
->ki_pos
+ kiocb
->ki_nbytes
, datasync
));
203 #error "Unsupported fops->fsync() implementation"
206 static inline ssize_t
207 zpl_read_common_iovec(struct inode
*ip
, const struct iovec
*iovp
, size_t count
,
208 unsigned long nr_segs
, loff_t
*ppos
, uio_seg_t segment
,
209 int flags
, cred_t
*cr
)
214 fstrans_cookie_t cookie
;
216 uio
.uio_iov
= (struct iovec
*)iovp
;
217 uio
.uio_resid
= count
;
218 uio
.uio_iovcnt
= nr_segs
;
219 uio
.uio_loffset
= *ppos
;
220 uio
.uio_limit
= MAXOFFSET_T
;
221 uio
.uio_segflg
= segment
;
223 cookie
= spl_fstrans_mark();
224 error
= -zfs_read(ip
, &uio
, flags
, cr
);
225 spl_fstrans_unmark(cookie
);
229 read
= count
- uio
.uio_resid
;
231 task_io_account_read(read
);
237 zpl_read_common(struct inode
*ip
, const char *buf
, size_t len
, loff_t
*ppos
,
238 uio_seg_t segment
, int flags
, cred_t
*cr
)
242 iov
.iov_base
= (void *)buf
;
245 return (zpl_read_common_iovec(ip
, &iov
, len
, 1, ppos
, segment
,
250 zpl_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
256 read
= zpl_read_common(filp
->f_mapping
->host
, buf
, len
, ppos
,
257 UIO_USERSPACE
, filp
->f_flags
, cr
);
264 zpl_aio_read(struct kiocb
*kiocb
, const struct iovec
*iovp
,
265 unsigned long nr_segs
, loff_t pos
)
268 struct file
*filp
= kiocb
->ki_filp
;
269 size_t count
= kiocb
->ki_nbytes
;
271 size_t alloc_size
= sizeof (struct iovec
) * nr_segs
;
272 struct iovec
*iov_tmp
= kmem_alloc(alloc_size
, KM_SLEEP
);
273 bcopy(iovp
, iov_tmp
, alloc_size
);
278 read
= zpl_read_common_iovec(filp
->f_mapping
->host
, iov_tmp
, count
,
279 nr_segs
, &kiocb
->ki_pos
, UIO_USERSPACE
, filp
->f_flags
, cr
);
282 kmem_free(iov_tmp
, alloc_size
);
287 static inline ssize_t
288 zpl_write_common_iovec(struct inode
*ip
, const struct iovec
*iovp
, size_t count
,
289 unsigned long nr_segs
, loff_t
*ppos
, uio_seg_t segment
,
290 int flags
, cred_t
*cr
)
295 fstrans_cookie_t cookie
;
297 if (flags
& O_APPEND
)
298 *ppos
= i_size_read(ip
);
300 uio
.uio_iov
= (struct iovec
*)iovp
;
301 uio
.uio_resid
= count
;
302 uio
.uio_iovcnt
= nr_segs
;
303 uio
.uio_loffset
= *ppos
;
304 uio
.uio_limit
= MAXOFFSET_T
;
305 uio
.uio_segflg
= segment
;
307 cookie
= spl_fstrans_mark();
308 error
= -zfs_write(ip
, &uio
, flags
, cr
);
309 spl_fstrans_unmark(cookie
);
313 wrote
= count
- uio
.uio_resid
;
315 task_io_account_write(wrote
);
320 zpl_write_common(struct inode
*ip
, const char *buf
, size_t len
, loff_t
*ppos
,
321 uio_seg_t segment
, int flags
, cred_t
*cr
)
325 iov
.iov_base
= (void *)buf
;
328 return (zpl_write_common_iovec(ip
, &iov
, len
, 1, ppos
, segment
,
333 zpl_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
339 wrote
= zpl_write_common(filp
->f_mapping
->host
, buf
, len
, ppos
,
340 UIO_USERSPACE
, filp
->f_flags
, cr
);
347 zpl_aio_write(struct kiocb
*kiocb
, const struct iovec
*iovp
,
348 unsigned long nr_segs
, loff_t pos
)
351 struct file
*filp
= kiocb
->ki_filp
;
352 size_t count
= kiocb
->ki_nbytes
;
354 size_t alloc_size
= sizeof (struct iovec
) * nr_segs
;
355 struct iovec
*iov_tmp
= kmem_alloc(alloc_size
, KM_SLEEP
);
356 bcopy(iovp
, iov_tmp
, alloc_size
);
361 wrote
= zpl_write_common_iovec(filp
->f_mapping
->host
, iov_tmp
, count
,
362 nr_segs
, &kiocb
->ki_pos
, UIO_USERSPACE
, filp
->f_flags
, cr
);
365 kmem_free(iov_tmp
, alloc_size
);
371 zpl_llseek(struct file
*filp
, loff_t offset
, int whence
)
373 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
374 fstrans_cookie_t cookie
;
376 if (whence
== SEEK_DATA
|| whence
== SEEK_HOLE
) {
377 struct inode
*ip
= filp
->f_mapping
->host
;
378 loff_t maxbytes
= ip
->i_sb
->s_maxbytes
;
382 cookie
= spl_fstrans_mark();
383 error
= -zfs_holey(ip
, whence
, &offset
);
384 spl_fstrans_unmark(cookie
);
386 error
= lseek_execute(filp
, ip
, offset
, maxbytes
);
387 spl_inode_unlock(ip
);
391 #endif /* SEEK_HOLE && SEEK_DATA */
393 return (generic_file_llseek(filp
, offset
, whence
));
397 * It's worth taking a moment to describe how mmap is implemented
398 * for zfs because it differs considerably from other Linux filesystems.
399 * However, this issue is handled the same way under OpenSolaris.
401 * The issue is that by design zfs bypasses the Linux page cache and
402 * leaves all caching up to the ARC. This has been shown to work
403 * well for the common read(2)/write(2) case. However, mmap(2)
404 * is problem because it relies on being tightly integrated with the
405 * page cache. To handle this we cache mmap'ed files twice, once in
406 * the ARC and a second time in the page cache. The code is careful
407 * to keep both copies synchronized.
409 * When a file with an mmap'ed region is written to using write(2)
410 * both the data in the ARC and existing pages in the page cache
411 * are updated. For a read(2) data will be read first from the page
412 * cache then the ARC if needed. Neither a write(2) or read(2) will
413 * will ever result in new pages being added to the page cache.
415 * New pages are added to the page cache only via .readpage() which
416 * is called when the vfs needs to read a page off disk to back the
417 * virtual memory region. These pages may be modified without
418 * notifying the ARC and will be written out periodically via
419 * .writepage(). This will occur due to either a sync or the usual
420 * page aging behavior. Note because a read(2) of a mmap'ed file
421 * will always check the page cache first even when the ARC is out
422 * of date correct data will still be returned.
424 * While this implementation ensures correct behavior it does have
425 * have some drawbacks. The most obvious of which is that it
426 * increases the required memory footprint when access mmap'ed
427 * files. It also adds additional complexity to the code keeping
428 * both caches synchronized.
430 * Longer term it may be possible to cleanly resolve this wart by
431 * mapping page cache pages directly on to the ARC buffers. The
432 * Linux address space operations are flexible enough to allow
433 * selection of which pages back a particular index. The trick
434 * would be working out the details of which subsystem is in
435 * charge, the ARC, the page cache, or both. It may also prove
436 * helpful to move the ARC buffers to a scatter-gather lists
437 * rather than a vmalloc'ed region.
440 zpl_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
442 struct inode
*ip
= filp
->f_mapping
->host
;
443 znode_t
*zp
= ITOZ(ip
);
445 fstrans_cookie_t cookie
;
447 cookie
= spl_fstrans_mark();
448 error
= -zfs_map(ip
, vma
->vm_pgoff
, (caddr_t
*)vma
->vm_start
,
449 (size_t)(vma
->vm_end
- vma
->vm_start
), vma
->vm_flags
);
450 spl_fstrans_unmark(cookie
);
454 error
= generic_file_mmap(filp
, vma
);
458 mutex_enter(&zp
->z_lock
);
460 mutex_exit(&zp
->z_lock
);
466 * Populate a page with data for the Linux page cache. This function is
467 * only used to support mmap(2). There will be an identical copy of the
468 * data in the ARC which is kept up to date via .write() and .writepage().
470 * Current this function relies on zpl_read_common() and the O_DIRECT
471 * flag to read in a page. This works but the more correct way is to
472 * update zfs_fillpage() to be Linux friendly and use that interface.
475 zpl_readpage(struct file
*filp
, struct page
*pp
)
480 fstrans_cookie_t cookie
;
482 ASSERT(PageLocked(pp
));
483 ip
= pp
->mapping
->host
;
486 cookie
= spl_fstrans_mark();
487 error
= -zfs_getpage(ip
, pl
, 1);
488 spl_fstrans_unmark(cookie
);
492 ClearPageUptodate(pp
);
496 flush_dcache_page(pp
);
504 * Populate a set of pages with data for the Linux page cache. This
505 * function will only be called for read ahead and never for demand
506 * paging. For simplicity, the code relies on read_cache_pages() to
507 * correctly lock each page for IO and call zpl_readpage().
510 zpl_readpages(struct file
*filp
, struct address_space
*mapping
,
511 struct list_head
*pages
, unsigned nr_pages
)
513 return (read_cache_pages(mapping
, pages
,
514 (filler_t
*)zpl_readpage
, filp
));
518 zpl_putpage(struct page
*pp
, struct writeback_control
*wbc
, void *data
)
520 struct address_space
*mapping
= data
;
521 fstrans_cookie_t cookie
;
523 ASSERT(PageLocked(pp
));
524 ASSERT(!PageWriteback(pp
));
526 cookie
= spl_fstrans_mark();
527 (void) zfs_putpage(mapping
->host
, pp
, wbc
);
528 spl_fstrans_unmark(cookie
);
534 zpl_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
536 znode_t
*zp
= ITOZ(mapping
->host
);
537 zfs_sb_t
*zsb
= ITOZSB(mapping
->host
);
538 enum writeback_sync_modes sync_mode
;
542 if (zsb
->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
543 wbc
->sync_mode
= WB_SYNC_ALL
;
545 sync_mode
= wbc
->sync_mode
;
548 * We don't want to run write_cache_pages() in SYNC mode here, because
549 * that would make putpage() wait for a single page to be committed to
550 * disk every single time, resulting in atrocious performance. Instead
551 * we run it once in non-SYNC mode so that the ZIL gets all the data,
552 * and then we commit it all in one go.
554 wbc
->sync_mode
= WB_SYNC_NONE
;
555 result
= write_cache_pages(mapping
, wbc
, zpl_putpage
, mapping
);
556 if (sync_mode
!= wbc
->sync_mode
) {
559 if (zsb
->z_log
!= NULL
)
560 zil_commit(zsb
->z_log
, zp
->z_id
);
564 * We need to call write_cache_pages() again (we can't just
565 * return after the commit) because the previous call in
566 * non-SYNC mode does not guarantee that we got all the dirty
567 * pages (see the implementation of write_cache_pages() for
568 * details). That being said, this is a no-op in most cases.
570 wbc
->sync_mode
= sync_mode
;
571 result
= write_cache_pages(mapping
, wbc
, zpl_putpage
, mapping
);
577 * Write out dirty pages to the ARC, this function is only required to
578 * support mmap(2). Mapped pages may be dirtied by memory operations
579 * which never call .write(). These dirty pages are kept in sync with
580 * the ARC buffers via this hook.
583 zpl_writepage(struct page
*pp
, struct writeback_control
*wbc
)
585 if (ITOZSB(pp
->mapping
->host
)->z_os
->os_sync
== ZFS_SYNC_ALWAYS
)
586 wbc
->sync_mode
= WB_SYNC_ALL
;
588 return (zpl_putpage(pp
, wbc
, pp
->mapping
));
592 * The only flag combination which matches the behavior of zfs_space()
593 * is FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE. The FALLOC_FL_PUNCH_HOLE
594 * flag was introduced in the 2.6.38 kernel.
596 #if defined(HAVE_FILE_FALLOCATE) || defined(HAVE_INODE_FALLOCATE)
598 zpl_fallocate_common(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
600 int error
= -EOPNOTSUPP
;
602 #if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
606 fstrans_cookie_t cookie
;
608 if (mode
!= (FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
613 if (offset
< 0 || len
<= 0)
617 olen
= i_size_read(ip
);
620 spl_inode_unlock(ip
);
623 if (offset
+ len
> olen
)
631 cookie
= spl_fstrans_mark();
632 error
= -zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, offset
, cr
);
633 spl_fstrans_unmark(cookie
);
634 spl_inode_unlock(ip
);
637 #endif /* defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) */
639 ASSERT3S(error
, <=, 0);
642 #endif /* defined(HAVE_FILE_FALLOCATE) || defined(HAVE_INODE_FALLOCATE) */
644 #ifdef HAVE_FILE_FALLOCATE
646 zpl_fallocate(struct file
*filp
, int mode
, loff_t offset
, loff_t len
)
648 return zpl_fallocate_common(filp
->f_path
.dentry
->d_inode
,
651 #endif /* HAVE_FILE_FALLOCATE */
654 * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
655 * attributes common to both Linux and Solaris are mapped.
658 zpl_ioctl_getflags(struct file
*filp
, void __user
*arg
)
660 struct inode
*ip
= file_inode(filp
);
661 unsigned int ioctl_flags
= 0;
662 uint64_t zfs_flags
= ITOZ(ip
)->z_pflags
;
665 if (zfs_flags
& ZFS_IMMUTABLE
)
666 ioctl_flags
|= FS_IMMUTABLE_FL
;
668 if (zfs_flags
& ZFS_APPENDONLY
)
669 ioctl_flags
|= FS_APPEND_FL
;
671 if (zfs_flags
& ZFS_NODUMP
)
672 ioctl_flags
|= FS_NODUMP_FL
;
674 ioctl_flags
&= FS_FL_USER_VISIBLE
;
676 error
= copy_to_user(arg
, &ioctl_flags
, sizeof (ioctl_flags
));
682 * fchange() is a helper macro to detect if we have been asked to change a
683 * flag. This is ugly, but the requirement that we do this is a consequence of
684 * how the Linux file attribute interface was designed. Another consequence is
685 * that concurrent modification of files suffers from a TOCTOU race. Neither
686 * are things we can fix without modifying the kernel-userland interface, which
687 * is outside of our jurisdiction.
690 #define fchange(f0, f1, b0, b1) ((((f0) & (b0)) == (b0)) != \
691 (((b1) & (f1)) == (f1)))
694 zpl_ioctl_setflags(struct file
*filp
, void __user
*arg
)
696 struct inode
*ip
= file_inode(filp
);
697 uint64_t zfs_flags
= ITOZ(ip
)->z_pflags
;
698 unsigned int ioctl_flags
;
703 fstrans_cookie_t cookie
;
705 if (copy_from_user(&ioctl_flags
, arg
, sizeof (ioctl_flags
)))
708 if ((ioctl_flags
& ~(FS_IMMUTABLE_FL
| FS_APPEND_FL
| FS_NODUMP_FL
)))
709 return (-EOPNOTSUPP
);
711 if ((ioctl_flags
& ~(FS_FL_USER_MODIFIABLE
)))
714 if ((fchange(ioctl_flags
, zfs_flags
, FS_IMMUTABLE_FL
, ZFS_IMMUTABLE
) ||
715 fchange(ioctl_flags
, zfs_flags
, FS_APPEND_FL
, ZFS_APPENDONLY
)) &&
716 !capable(CAP_LINUX_IMMUTABLE
))
719 if (!zpl_inode_owner_or_capable(ip
))
723 xoap
= xva_getxoptattr(&xva
);
725 XVA_SET_REQ(&xva
, XAT_IMMUTABLE
);
726 if (ioctl_flags
& FS_IMMUTABLE_FL
)
727 xoap
->xoa_immutable
= B_TRUE
;
729 XVA_SET_REQ(&xva
, XAT_APPENDONLY
);
730 if (ioctl_flags
& FS_APPEND_FL
)
731 xoap
->xoa_appendonly
= B_TRUE
;
733 XVA_SET_REQ(&xva
, XAT_NODUMP
);
734 if (ioctl_flags
& FS_NODUMP_FL
)
735 xoap
->xoa_nodump
= B_TRUE
;
738 cookie
= spl_fstrans_mark();
739 error
= -zfs_setattr(ip
, (vattr_t
*)&xva
, 0, cr
);
740 spl_fstrans_unmark(cookie
);
747 zpl_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
750 case FS_IOC_GETFLAGS
:
751 return (zpl_ioctl_getflags(filp
, (void *)arg
));
752 case FS_IOC_SETFLAGS
:
753 return (zpl_ioctl_setflags(filp
, (void *)arg
));
761 zpl_compat_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
763 return (zpl_ioctl(filp
, cmd
, arg
));
765 #endif /* CONFIG_COMPAT */
768 const struct address_space_operations zpl_address_space_operations
= {
769 .readpages
= zpl_readpages
,
770 .readpage
= zpl_readpage
,
771 .writepage
= zpl_writepage
,
772 .writepages
= zpl_writepages
,
775 const struct file_operations zpl_file_operations
= {
777 .release
= zpl_release
,
778 .llseek
= zpl_llseek
,
781 .aio_read
= zpl_aio_read
,
782 .aio_write
= zpl_aio_write
,
785 .aio_fsync
= zpl_aio_fsync
,
786 #ifdef HAVE_FILE_FALLOCATE
787 .fallocate
= zpl_fallocate
,
788 #endif /* HAVE_FILE_FALLOCATE */
789 .unlocked_ioctl
= zpl_ioctl
,
791 .compat_ioctl
= zpl_compat_ioctl
,
795 const struct file_operations zpl_dir_file_operations
= {
796 .llseek
= generic_file_llseek
,
797 .read
= generic_read_dir
,
798 #ifdef HAVE_VFS_ITERATE
799 .iterate
= zpl_iterate
,
801 .readdir
= zpl_readdir
,
804 .unlocked_ioctl
= zpl_ioctl
,
806 .compat_ioctl
= zpl_compat_ioctl
,