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/zfs_vfsops.h>
27 #include <sys/zfs_vnops.h>
28 #include <sys/zfs_znode.h>
33 zpl_open(struct inode
*ip
, struct file
*filp
)
39 error
= -zfs_open(ip
, filp
->f_mode
, filp
->f_flags
, cr
);
41 ASSERT3S(error
, <=, 0);
46 return generic_file_open(ip
, filp
);
50 zpl_release(struct inode
*ip
, struct file
*filp
)
55 if (ITOZ(ip
)->z_atime_dirty
)
59 error
= -zfs_close(ip
, filp
->f_flags
, cr
);
61 ASSERT3S(error
, <=, 0);
67 zpl_iterate(struct file
*filp
, struct dir_context
*ctx
)
69 struct dentry
*dentry
= filp
->f_path
.dentry
;
74 error
= -zfs_readdir(dentry
->d_inode
, ctx
, cr
);
76 ASSERT3S(error
, <=, 0);
81 #if !defined(HAVE_VFS_ITERATE)
83 zpl_readdir(struct file
*filp
, void *dirent
, filldir_t filldir
)
85 struct dir_context ctx
= DIR_CONTEXT_INIT(dirent
, filldir
, filp
->f_pos
);
88 error
= zpl_iterate(filp
, &ctx
);
89 filp
->f_pos
= ctx
.pos
;
93 #endif /* HAVE_VFS_ITERATE */
95 #if defined(HAVE_FSYNC_WITH_DENTRY)
97 * Linux 2.6.x - 2.6.34 API,
98 * Through 2.6.34 the nfsd kernel server would pass a NULL 'file struct *'
99 * to the fops->fsync() hook. For this reason, we must be careful not to
100 * use filp unconditionally.
103 zpl_fsync(struct file
*filp
, struct dentry
*dentry
, int datasync
)
109 error
= -zfs_fsync(dentry
->d_inode
, datasync
, cr
);
111 ASSERT3S(error
, <=, 0);
116 #elif 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
;
131 error
= -zfs_fsync(inode
, datasync
, cr
);
133 ASSERT3S(error
, <=, 0);
138 #elif defined(HAVE_FSYNC_RANGE)
140 * Linux 3.1 - 3.x API,
141 * As of 3.1 the responsibility to call filemap_write_and_wait_range() has
142 * been pushed down in to the .fsync() vfs hook. Additionally, the i_mutex
143 * lock is no longer held by the caller, for zfs we don't require the lock
144 * to be held so we don't acquire it.
147 zpl_fsync(struct file
*filp
, loff_t start
, loff_t end
, int datasync
)
149 struct inode
*inode
= filp
->f_mapping
->host
;
153 error
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
158 error
= -zfs_fsync(inode
, datasync
, cr
);
160 ASSERT3S(error
, <=, 0);
165 #error "Unsupported fops->fsync() implementation"
169 zpl_read_common(struct inode
*ip
, const char *buf
, size_t len
, loff_t pos
,
170 uio_seg_t segment
, int flags
, cred_t
*cr
)
177 iov
.iov_base
= (void *)buf
;
183 uio
.uio_loffset
= pos
;
184 uio
.uio_limit
= MAXOFFSET_T
;
185 uio
.uio_segflg
= segment
;
187 error
= -zfs_read(ip
, &uio
, flags
, cr
);
191 read
= len
- uio
.uio_resid
;
192 task_io_account_read(read
);
198 zpl_read(struct file
*filp
, char __user
*buf
, size_t len
, loff_t
*ppos
)
204 read
= zpl_read_common(filp
->f_mapping
->host
, buf
, len
, *ppos
,
205 UIO_USERSPACE
, filp
->f_flags
, cr
);
216 zpl_write_common(struct inode
*ip
, const char *buf
, size_t len
, loff_t pos
,
217 uio_seg_t segment
, int flags
, cred_t
*cr
)
224 iov
.iov_base
= (void *)buf
;
230 uio
.uio_loffset
= pos
;
231 uio
.uio_limit
= MAXOFFSET_T
;
232 uio
.uio_segflg
= segment
;
234 error
= -zfs_write(ip
, &uio
, flags
, cr
);
238 wrote
= len
- uio
.uio_resid
;
239 task_io_account_write(wrote
);
245 zpl_write(struct file
*filp
, const char __user
*buf
, size_t len
, loff_t
*ppos
)
251 wrote
= zpl_write_common(filp
->f_mapping
->host
, buf
, len
, *ppos
,
252 UIO_USERSPACE
, filp
->f_flags
, cr
);
263 zpl_llseek(struct file
*filp
, loff_t offset
, int whence
)
265 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
266 if (whence
== SEEK_DATA
|| whence
== SEEK_HOLE
) {
267 struct inode
*ip
= filp
->f_mapping
->host
;
268 loff_t maxbytes
= ip
->i_sb
->s_maxbytes
;
272 error
= -zfs_holey(ip
, whence
, &offset
);
274 error
= lseek_execute(filp
, ip
, offset
, maxbytes
);
275 spl_inode_unlock(ip
);
279 #endif /* SEEK_HOLE && SEEK_DATA */
281 return generic_file_llseek(filp
, offset
, whence
);
285 * It's worth taking a moment to describe how mmap is implemented
286 * for zfs because it differs considerably from other Linux filesystems.
287 * However, this issue is handled the same way under OpenSolaris.
289 * The issue is that by design zfs bypasses the Linux page cache and
290 * leaves all caching up to the ARC. This has been shown to work
291 * well for the common read(2)/write(2) case. However, mmap(2)
292 * is problem because it relies on being tightly integrated with the
293 * page cache. To handle this we cache mmap'ed files twice, once in
294 * the ARC and a second time in the page cache. The code is careful
295 * to keep both copies synchronized.
297 * When a file with an mmap'ed region is written to using write(2)
298 * both the data in the ARC and existing pages in the page cache
299 * are updated. For a read(2) data will be read first from the page
300 * cache then the ARC if needed. Neither a write(2) or read(2) will
301 * will ever result in new pages being added to the page cache.
303 * New pages are added to the page cache only via .readpage() which
304 * is called when the vfs needs to read a page off disk to back the
305 * virtual memory region. These pages may be modified without
306 * notifying the ARC and will be written out periodically via
307 * .writepage(). This will occur due to either a sync or the usual
308 * page aging behavior. Note because a read(2) of a mmap'ed file
309 * will always check the page cache first even when the ARC is out
310 * of date correct data will still be returned.
312 * While this implementation ensures correct behavior it does have
313 * have some drawbacks. The most obvious of which is that it
314 * increases the required memory footprint when access mmap'ed
315 * files. It also adds additional complexity to the code keeping
316 * both caches synchronized.
318 * Longer term it may be possible to cleanly resolve this wart by
319 * mapping page cache pages directly on to the ARC buffers. The
320 * Linux address space operations are flexible enough to allow
321 * selection of which pages back a particular index. The trick
322 * would be working out the details of which subsystem is in
323 * charge, the ARC, the page cache, or both. It may also prove
324 * helpful to move the ARC buffers to a scatter-gather lists
325 * rather than a vmalloc'ed region.
328 zpl_mmap(struct file
*filp
, struct vm_area_struct
*vma
)
330 struct inode
*ip
= filp
->f_mapping
->host
;
331 znode_t
*zp
= ITOZ(ip
);
334 error
= -zfs_map(ip
, vma
->vm_pgoff
, (caddr_t
*)vma
->vm_start
,
335 (size_t)(vma
->vm_end
- vma
->vm_start
), vma
->vm_flags
);
339 error
= generic_file_mmap(filp
, vma
);
343 mutex_enter(&zp
->z_lock
);
345 mutex_exit(&zp
->z_lock
);
351 * Populate a page with data for the Linux page cache. This function is
352 * only used to support mmap(2). There will be an identical copy of the
353 * data in the ARC which is kept up to date via .write() and .writepage().
355 * Current this function relies on zpl_read_common() and the O_DIRECT
356 * flag to read in a page. This works but the more correct way is to
357 * update zfs_fillpage() to be Linux friendly and use that interface.
360 zpl_readpage(struct file
*filp
, struct page
*pp
)
366 ASSERT(PageLocked(pp
));
367 ip
= pp
->mapping
->host
;
370 error
= -zfs_getpage(ip
, pl
, 1);
374 ClearPageUptodate(pp
);
378 flush_dcache_page(pp
);
386 * Populate a set of pages with data for the Linux page cache. This
387 * function will only be called for read ahead and never for demand
388 * paging. For simplicity, the code relies on read_cache_pages() to
389 * correctly lock each page for IO and call zpl_readpage().
392 zpl_readpages(struct file
*filp
, struct address_space
*mapping
,
393 struct list_head
*pages
, unsigned nr_pages
)
395 return (read_cache_pages(mapping
, pages
,
396 (filler_t
*)zpl_readpage
, filp
));
400 zpl_putpage(struct page
*pp
, struct writeback_control
*wbc
, void *data
)
402 struct address_space
*mapping
= data
;
404 ASSERT(PageLocked(pp
));
405 ASSERT(!PageWriteback(pp
));
406 ASSERT(!(current
->flags
& PF_NOFS
));
409 * Annotate this call path with a flag that indicates that it is
410 * unsafe to use KM_SLEEP during memory allocations due to the
411 * potential for a deadlock. KM_PUSHPAGE should be used instead.
413 current
->flags
|= PF_NOFS
;
414 (void) zfs_putpage(mapping
->host
, pp
, wbc
);
415 current
->flags
&= ~PF_NOFS
;
421 zpl_writepages(struct address_space
*mapping
, struct writeback_control
*wbc
)
423 return write_cache_pages(mapping
, wbc
, zpl_putpage
, mapping
);
427 * Write out dirty pages to the ARC, this function is only required to
428 * support mmap(2). Mapped pages may be dirtied by memory operations
429 * which never call .write(). These dirty pages are kept in sync with
430 * the ARC buffers via this hook.
433 zpl_writepage(struct page
*pp
, struct writeback_control
*wbc
)
435 return zpl_putpage(pp
, wbc
, pp
->mapping
);
439 * The only flag combination which matches the behavior of zfs_space()
440 * is FALLOC_FL_PUNCH_HOLE. This flag was introduced in the 2.6.38 kernel.
443 zpl_fallocate_common(struct inode
*ip
, int mode
, loff_t offset
, loff_t len
)
446 int error
= -EOPNOTSUPP
;
448 if (mode
& FALLOC_FL_KEEP_SIZE
)
449 return (-EOPNOTSUPP
);
453 #ifdef FALLOC_FL_PUNCH_HOLE
454 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
463 error
= -zfs_space(ip
, F_FREESP
, &bf
, FWRITE
, offset
, cr
);
465 #endif /* FALLOC_FL_PUNCH_HOLE */
469 ASSERT3S(error
, <=, 0);
473 #ifdef HAVE_FILE_FALLOCATE
475 zpl_fallocate(struct file
*filp
, int mode
, loff_t offset
, loff_t len
)
477 return zpl_fallocate_common(filp
->f_path
.dentry
->d_inode
,
480 #endif /* HAVE_FILE_FALLOCATE */
483 zpl_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
486 case ZFS_IOC_GETFLAGS
:
487 case ZFS_IOC_SETFLAGS
:
488 return (-EOPNOTSUPP
);
496 zpl_compat_ioctl(struct file
*filp
, unsigned int cmd
, unsigned long arg
)
498 return zpl_ioctl(filp
, cmd
, arg
);
500 #endif /* CONFIG_COMPAT */
503 const struct address_space_operations zpl_address_space_operations
= {
504 .readpages
= zpl_readpages
,
505 .readpage
= zpl_readpage
,
506 .writepage
= zpl_writepage
,
507 .writepages
= zpl_writepages
,
510 const struct file_operations zpl_file_operations
= {
512 .release
= zpl_release
,
513 .llseek
= zpl_llseek
,
518 #ifdef HAVE_FILE_FALLOCATE
519 .fallocate
= zpl_fallocate
,
520 #endif /* HAVE_FILE_FALLOCATE */
521 .unlocked_ioctl
= zpl_ioctl
,
523 .compat_ioctl
= zpl_compat_ioctl
,
527 const struct file_operations zpl_dir_file_operations
= {
528 .llseek
= generic_file_llseek
,
529 .read
= generic_read_dir
,
530 #ifdef HAVE_VFS_ITERATE
531 .iterate
= zpl_iterate
,
533 .readdir
= zpl_readdir
,
536 .unlocked_ioctl
= zpl_ioctl
,
538 .compat_ioctl
= zpl_compat_ioctl
,