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_readdir(struct file *filp, void *dirent, filldir_t filldir)
35 struct dentry *dentry = filp->f_path.dentry;
39 cr = (cred_t *)get_current_cred();
40 error = -zfs_readdir(dentry->d_inode, dirent, filldir,
43 ASSERT3S(error, <=, 0);
49 zpl_fsync(struct file *filp, struct dentry *dentry, int datasync)
54 cr = (cred_t *)get_current_cred();
55 error = -zfs_fsync(filp->f_path.dentry->d_inode, datasync, cr);
57 ASSERT3S(error, <=, 0);
63 zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t pos,
64 uio_seg_t segment, int flags, cred_t *cr)
70 iov.iov_base = (void *)buf;
76 uio.uio_loffset = pos;
77 uio.uio_limit = MAXOFFSET_T;
78 uio.uio_segflg = segment;
80 error = -zfs_read(ip, &uio, flags, cr);
84 return (len - uio.uio_resid);
88 zpl_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
93 cr = (cred_t *)get_current_cred();
94 read = zpl_read_common(filp->f_mapping->host, buf, len, *ppos,
95 UIO_USERSPACE, filp->f_flags, cr);
106 zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t pos,
107 uio_seg_t segment, int flags, cred_t *cr)
113 iov.iov_base = (void *)buf;
119 uio.uio_loffset = pos;
120 uio.uio_limit = MAXOFFSET_T;
121 uio.uio_segflg = segment;
123 error = -zfs_write(ip, &uio, flags, cr);
127 return (len - uio.uio_resid);
131 zpl_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
136 cr = (cred_t *)get_current_cred();
137 wrote = zpl_write_common(filp->f_mapping->host, buf, len, *ppos,
138 UIO_USERSPACE, filp->f_flags, cr);
149 * It's worth taking a moment to describe how mmap is implemented
150 * for zfs because it differs considerably from other Linux filesystems.
151 * However, this issue is handled the same way under OpenSolaris.
153 * The issue is that by design zfs bypasses the Linux page cache and
154 * leaves all caching up to the ARC. This has been shown to work
155 * well for the common read(2)/write(2) case. However, mmap(2)
156 * is problem because it relies on being tightly integrated with the
157 * page cache. To handle this we cache mmap'ed files twice, once in
158 * the ARC and a second time in the page cache. The code is careful
159 * to keep both copies synchronized.
161 * When a file with an mmap'ed region is written to using write(2)
162 * both the data in the ARC and existing pages in the page cache
163 * are updated. For a read(2) data will be read first from the page
164 * cache then the ARC if needed. Neither a write(2) or read(2) will
165 * will ever result in new pages being added to the page cache.
167 * New pages are added to the page cache only via .readpage() which
168 * is called when the vfs needs to read a page off disk to back the
169 * virtual memory region. These pages may be modified without
170 * notifying the ARC and will be written out periodically via
171 * .writepage(). This will occur due to either a sync or the usual
172 * page aging behavior. Note because a read(2) of a mmap'ed file
173 * will always check the page cache first even when the ARC is out
174 * of date correct data will still be returned.
176 * While this implementation ensures correct behavior it does have
177 * have some drawbacks. The most obvious of which is that it
178 * increases the required memory footprint when access mmap'ed
179 * files. It also adds additional complexity to the code keeping
180 * both caches synchronized.
182 * Longer term it may be possible to cleanly resolve this wart by
183 * mapping page cache pages directly on to the ARC buffers. The
184 * Linux address space operations are flexible enough to allow
185 * selection of which pages back a particular index. The trick
186 * would be working out the details of which subsystem is in
187 * charge, the ARC, the page cache, or both. It may also prove
188 * helpful to move the ARC buffers to a scatter-gather lists
189 * rather than a vmalloc'ed region.
192 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
194 znode_t *zp = ITOZ(filp->f_mapping->host);
197 error = generic_file_mmap(filp, vma);
201 mutex_enter(&zp->z_lock);
203 mutex_exit(&zp->z_lock);
209 * Populate a page with data for the Linux page cache. This function is
210 * only used to support mmap(2). There will be an identical copy of the
211 * data in the ARC which is kept up to date via .write() and .writepage().
213 * Current this function relies on zpl_read_common() and the O_DIRECT
214 * flag to read in a page. This works but the more correct way is to
215 * update zfs_fillpage() to be Linux friendly and use that interface.
218 zpl_readpage(struct file *filp, struct page *pp)
227 ASSERT(PageLocked(pp));
228 ip = pp->mapping->host;
229 off = page_offset(pp);
230 i_size = i_size_read(ip);
231 ASSERT3S(off, <, i_size);
233 cr = (cred_t *)get_current_cred();
234 len = MIN(PAGE_CACHE_SIZE, i_size - off);
238 /* O_DIRECT is passed to bypass the page cache and avoid deadlock. */
239 wrote = zpl_read_common(ip, pb, len, off, UIO_SYSSPACE, O_DIRECT, cr);
243 if (!error && (len < PAGE_CACHE_SIZE))
244 memset(pb + len, 0, PAGE_CACHE_SIZE - len);
251 ClearPageUptodate(pp);
255 flush_dcache_page(pp);
264 * Write out dirty pages to the ARC, this function is only required to
265 * support mmap(2). Mapped pages may be dirtied by memory operations
266 * which never call .write(). These dirty pages are kept in sync with
267 * the ARC buffers via this hook.
269 * Currently this function relies on zpl_write_common() and the O_DIRECT
270 * flag to push out the page. This works but the more correct way is
271 * to update zfs_putapage() to be Linux friendly and use that interface.
274 zpl_writepage(struct page *pp, struct writeback_control *wbc)
283 ASSERT(PageLocked(pp));
284 ip = pp->mapping->host;
285 off = page_offset(pp);
286 i_size = i_size_read(ip);
288 cr = (cred_t *)get_current_cred();
289 len = MIN(PAGE_CACHE_SIZE, i_size - off);
293 /* O_DIRECT is passed to bypass the page cache and avoid deadlock. */
294 read = zpl_write_common(ip, pb, len, off, UIO_SYSSPACE, O_DIRECT, cr);
303 ClearPageUptodate(pp);
314 const struct address_space_operations zpl_address_space_operations = {
315 .readpage = zpl_readpage,
316 .writepage = zpl_writepage,
319 const struct file_operations zpl_file_operations = {
320 .open = generic_file_open,
321 .llseek = generic_file_llseek,
324 .readdir = zpl_readdir,
329 const struct file_operations zpl_dir_file_operations = {
330 .llseek = generic_file_llseek,
331 .read = generic_read_dir,
332 .readdir = zpl_readdir,