]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zpl_file.c
Add zfs_open()/zfs_close()
[mirror_zfs.git] / module / zfs / zpl_file.c
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 */
24
25
26 #include <sys/zfs_vfsops.h>
27 #include <sys/zfs_vnops.h>
28 #include <sys/zfs_znode.h>
29 #include <sys/zpl.h>
30
31
32 static int
33 zpl_open(struct inode *ip, struct file *filp)
34 {
35 cred_t *cr;
36 int error;
37
38 cr = (cred_t *)get_current_cred();
39 error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr);
40 put_cred(cr);
41 ASSERT3S(error, <=, 0);
42
43 if (error)
44 return (error);
45
46 return generic_file_open(ip, filp);
47 }
48
49 static int
50 zpl_release(struct inode *ip, struct file *filp)
51 {
52 cred_t *cr;
53 int error;
54
55 cr = (cred_t *)get_current_cred();
56 error = -zfs_close(ip, filp->f_flags, cr);
57 put_cred(cr);
58 ASSERT3S(error, <=, 0);
59
60 return (error);
61 }
62
63 static int
64 zpl_readdir(struct file *filp, void *dirent, filldir_t filldir)
65 {
66 struct dentry *dentry = filp->f_path.dentry;
67 cred_t *cr;
68 int error;
69
70 cr = (cred_t *)get_current_cred();
71 error = -zfs_readdir(dentry->d_inode, dirent, filldir,
72 &filp->f_pos, cr);
73 put_cred(cr);
74 ASSERT3S(error, <=, 0);
75
76 return (error);
77 }
78
79 ZPL_FSYNC_PROTO(zpl_fsync, filp, unused_dentry, datasync)
80 {
81 cred_t *cr;
82 int error;
83
84 cr = (cred_t *)get_current_cred();
85 error = -zfs_fsync(filp->f_path.dentry->d_inode, datasync, cr);
86 put_cred(cr);
87 ASSERT3S(error, <=, 0);
88
89 return (error);
90 }
91
92 ssize_t
93 zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t pos,
94 uio_seg_t segment, int flags, cred_t *cr)
95 {
96 int error;
97 struct iovec iov;
98 uio_t uio;
99
100 iov.iov_base = (void *)buf;
101 iov.iov_len = len;
102
103 uio.uio_iov = &iov;
104 uio.uio_resid = len;
105 uio.uio_iovcnt = 1;
106 uio.uio_loffset = pos;
107 uio.uio_limit = MAXOFFSET_T;
108 uio.uio_segflg = segment;
109
110 error = -zfs_read(ip, &uio, flags, cr);
111 if (error < 0)
112 return (error);
113
114 return (len - uio.uio_resid);
115 }
116
117 static ssize_t
118 zpl_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
119 {
120 cred_t *cr;
121 ssize_t read;
122
123 cr = (cred_t *)get_current_cred();
124 read = zpl_read_common(filp->f_mapping->host, buf, len, *ppos,
125 UIO_USERSPACE, filp->f_flags, cr);
126 put_cred(cr);
127
128 if (read < 0)
129 return (read);
130
131 *ppos += read;
132 return (read);
133 }
134
135 ssize_t
136 zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t pos,
137 uio_seg_t segment, int flags, cred_t *cr)
138 {
139 int error;
140 struct iovec iov;
141 uio_t uio;
142
143 iov.iov_base = (void *)buf;
144 iov.iov_len = len;
145
146 uio.uio_iov = &iov;
147 uio.uio_resid = len,
148 uio.uio_iovcnt = 1;
149 uio.uio_loffset = pos;
150 uio.uio_limit = MAXOFFSET_T;
151 uio.uio_segflg = segment;
152
153 error = -zfs_write(ip, &uio, flags, cr);
154 if (error < 0)
155 return (error);
156
157 return (len - uio.uio_resid);
158 }
159
160 static ssize_t
161 zpl_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
162 {
163 cred_t *cr;
164 ssize_t wrote;
165
166 cr = (cred_t *)get_current_cred();
167 wrote = zpl_write_common(filp->f_mapping->host, buf, len, *ppos,
168 UIO_USERSPACE, filp->f_flags, cr);
169 put_cred(cr);
170
171 if (wrote < 0)
172 return (wrote);
173
174 *ppos += wrote;
175 return (wrote);
176 }
177
178 /*
179 * It's worth taking a moment to describe how mmap is implemented
180 * for zfs because it differs considerably from other Linux filesystems.
181 * However, this issue is handled the same way under OpenSolaris.
182 *
183 * The issue is that by design zfs bypasses the Linux page cache and
184 * leaves all caching up to the ARC. This has been shown to work
185 * well for the common read(2)/write(2) case. However, mmap(2)
186 * is problem because it relies on being tightly integrated with the
187 * page cache. To handle this we cache mmap'ed files twice, once in
188 * the ARC and a second time in the page cache. The code is careful
189 * to keep both copies synchronized.
190 *
191 * When a file with an mmap'ed region is written to using write(2)
192 * both the data in the ARC and existing pages in the page cache
193 * are updated. For a read(2) data will be read first from the page
194 * cache then the ARC if needed. Neither a write(2) or read(2) will
195 * will ever result in new pages being added to the page cache.
196 *
197 * New pages are added to the page cache only via .readpage() which
198 * is called when the vfs needs to read a page off disk to back the
199 * virtual memory region. These pages may be modified without
200 * notifying the ARC and will be written out periodically via
201 * .writepage(). This will occur due to either a sync or the usual
202 * page aging behavior. Note because a read(2) of a mmap'ed file
203 * will always check the page cache first even when the ARC is out
204 * of date correct data will still be returned.
205 *
206 * While this implementation ensures correct behavior it does have
207 * have some drawbacks. The most obvious of which is that it
208 * increases the required memory footprint when access mmap'ed
209 * files. It also adds additional complexity to the code keeping
210 * both caches synchronized.
211 *
212 * Longer term it may be possible to cleanly resolve this wart by
213 * mapping page cache pages directly on to the ARC buffers. The
214 * Linux address space operations are flexible enough to allow
215 * selection of which pages back a particular index. The trick
216 * would be working out the details of which subsystem is in
217 * charge, the ARC, the page cache, or both. It may also prove
218 * helpful to move the ARC buffers to a scatter-gather lists
219 * rather than a vmalloc'ed region.
220 */
221 static int
222 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
223 {
224 znode_t *zp = ITOZ(filp->f_mapping->host);
225 int error;
226
227 error = generic_file_mmap(filp, vma);
228 if (error)
229 return (error);
230
231 mutex_enter(&zp->z_lock);
232 zp->z_is_mapped = 1;
233 mutex_exit(&zp->z_lock);
234
235 return (error);
236 }
237
238 /*
239 * Populate a page with data for the Linux page cache. This function is
240 * only used to support mmap(2). There will be an identical copy of the
241 * data in the ARC which is kept up to date via .write() and .writepage().
242 *
243 * Current this function relies on zpl_read_common() and the O_DIRECT
244 * flag to read in a page. This works but the more correct way is to
245 * update zfs_fillpage() to be Linux friendly and use that interface.
246 */
247 static int
248 zpl_readpage(struct file *filp, struct page *pp)
249 {
250 struct inode *ip;
251 loff_t off, i_size;
252 size_t len, wrote;
253 cred_t *cr;
254 void *pb;
255 int error = 0;
256
257 ASSERT(PageLocked(pp));
258 ip = pp->mapping->host;
259 off = page_offset(pp);
260 i_size = i_size_read(ip);
261 ASSERT3S(off, <, i_size);
262
263 cr = (cred_t *)get_current_cred();
264 len = MIN(PAGE_CACHE_SIZE, i_size - off);
265
266 pb = kmap(pp);
267
268 /* O_DIRECT is passed to bypass the page cache and avoid deadlock. */
269 wrote = zpl_read_common(ip, pb, len, off, UIO_SYSSPACE, O_DIRECT, cr);
270 if (wrote != len)
271 error = -EIO;
272
273 if (!error && (len < PAGE_CACHE_SIZE))
274 memset(pb + len, 0, PAGE_CACHE_SIZE - len);
275
276 kunmap(pp);
277 put_cred(cr);
278
279 if (error) {
280 SetPageError(pp);
281 ClearPageUptodate(pp);
282 } else {
283 ClearPageError(pp);
284 SetPageUptodate(pp);
285 flush_dcache_page(pp);
286 }
287
288 unlock_page(pp);
289
290 return (error);
291 }
292
293 /*
294 * Write out dirty pages to the ARC, this function is only required to
295 * support mmap(2). Mapped pages may be dirtied by memory operations
296 * which never call .write(). These dirty pages are kept in sync with
297 * the ARC buffers via this hook.
298 *
299 * Currently this function relies on zpl_write_common() and the O_DIRECT
300 * flag to push out the page. This works but the more correct way is
301 * to update zfs_putapage() to be Linux friendly and use that interface.
302 */
303 static int
304 zpl_writepage(struct page *pp, struct writeback_control *wbc)
305 {
306 struct inode *ip;
307 loff_t off, i_size;
308 size_t len, read;
309 cred_t *cr;
310 void *pb;
311 int error = 0;
312
313 ASSERT(PageLocked(pp));
314 ip = pp->mapping->host;
315 off = page_offset(pp);
316 i_size = i_size_read(ip);
317
318 cr = (cred_t *)get_current_cred();
319 len = MIN(PAGE_CACHE_SIZE, i_size - off);
320
321 pb = kmap(pp);
322
323 /* O_DIRECT is passed to bypass the page cache and avoid deadlock. */
324 read = zpl_write_common(ip, pb, len, off, UIO_SYSSPACE, O_DIRECT, cr);
325 if (read != len)
326 error = -EIO;
327
328 kunmap(pp);
329 put_cred(cr);
330
331 if (error) {
332 SetPageError(pp);
333 ClearPageUptodate(pp);
334 } else {
335 ClearPageError(pp);
336 SetPageUptodate(pp);
337 }
338
339 unlock_page(pp);
340
341 return (error);
342 }
343
344 const struct address_space_operations zpl_address_space_operations = {
345 .readpage = zpl_readpage,
346 .writepage = zpl_writepage,
347 };
348
349 const struct file_operations zpl_file_operations = {
350 .open = zpl_open,
351 .release = zpl_release,
352 .llseek = generic_file_llseek,
353 .read = zpl_read,
354 .write = zpl_write,
355 .readdir = zpl_readdir,
356 .mmap = zpl_mmap,
357 .fsync = zpl_fsync,
358 };
359
360 const struct file_operations zpl_dir_file_operations = {
361 .llseek = generic_file_llseek,
362 .read = generic_read_dir,
363 .readdir = zpl_readdir,
364 .fsync = zpl_fsync,
365 };