]>
Commit | Line | Data |
---|---|---|
1efb473f BB |
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 | ||
126400a1 BB |
32 | static int |
33 | zpl_open(struct inode *ip, struct file *filp) | |
34 | { | |
81e97e21 | 35 | cred_t *cr = CRED(); |
126400a1 BB |
36 | int error; |
37 | ||
81e97e21 | 38 | crhold(cr); |
126400a1 | 39 | error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr); |
81e97e21 | 40 | crfree(cr); |
126400a1 BB |
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 | { | |
81e97e21 | 52 | cred_t *cr = CRED(); |
126400a1 BB |
53 | int error; |
54 | ||
81e97e21 | 55 | crhold(cr); |
126400a1 | 56 | error = -zfs_close(ip, filp->f_flags, cr); |
81e97e21 | 57 | crfree(cr); |
126400a1 BB |
58 | ASSERT3S(error, <=, 0); |
59 | ||
60 | return (error); | |
61 | } | |
62 | ||
1efb473f BB |
63 | static int |
64 | zpl_readdir(struct file *filp, void *dirent, filldir_t filldir) | |
65 | { | |
66 | struct dentry *dentry = filp->f_path.dentry; | |
81e97e21 | 67 | cred_t *cr = CRED(); |
1efb473f BB |
68 | int error; |
69 | ||
81e97e21 | 70 | crhold(cr); |
1efb473f BB |
71 | error = -zfs_readdir(dentry->d_inode, dirent, filldir, |
72 | &filp->f_pos, cr); | |
81e97e21 | 73 | crfree(cr); |
1efb473f BB |
74 | ASSERT3S(error, <=, 0); |
75 | ||
76 | return (error); | |
77 | } | |
78 | ||
3117dd0b BB |
79 | /* |
80 | * 2.6.35 API change, | |
81 | * As of 2.6.35 the dentry argument to the .fsync() vfs hook was deemed | |
82 | * redundant. The dentry is still accessible via filp->f_path.dentry, | |
83 | * and we are guaranteed that filp will never be NULL. | |
84 | * | |
85 | * 2.6.34 API change, | |
86 | * Prior to 2.6.34 the nfsd kernel server would pass a NULL file struct * | |
87 | * to the .fsync() hook. For this reason, we must be careful not to use | |
88 | * filp unconditionally in the 3 argument case. | |
89 | */ | |
90 | #ifdef HAVE_2ARGS_FSYNC | |
91 | static int | |
92 | zpl_fsync(struct file *filp, int datasync) | |
93 | { | |
94 | struct dentry *dentry = filp->f_path.dentry; | |
95 | #else | |
96 | static int | |
97 | zpl_fsync(struct file *filp, struct dentry *dentry, int datasync) | |
1efb473f | 98 | { |
3117dd0b | 99 | #endif /* HAVE_2ARGS_FSYNC */ |
81e97e21 | 100 | cred_t *cr = CRED(); |
1efb473f BB |
101 | int error; |
102 | ||
81e97e21 | 103 | crhold(cr); |
3117dd0b | 104 | error = -zfs_fsync(dentry->d_inode, datasync, cr); |
81e97e21 | 105 | crfree(cr); |
1efb473f BB |
106 | ASSERT3S(error, <=, 0); |
107 | ||
108 | return (error); | |
109 | } | |
110 | ||
111 | ssize_t | |
112 | zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t pos, | |
113 | uio_seg_t segment, int flags, cred_t *cr) | |
114 | { | |
115 | int error; | |
116 | struct iovec iov; | |
117 | uio_t uio; | |
118 | ||
119 | iov.iov_base = (void *)buf; | |
120 | iov.iov_len = len; | |
121 | ||
122 | uio.uio_iov = &iov; | |
123 | uio.uio_resid = len; | |
124 | uio.uio_iovcnt = 1; | |
125 | uio.uio_loffset = pos; | |
126 | uio.uio_limit = MAXOFFSET_T; | |
127 | uio.uio_segflg = segment; | |
128 | ||
129 | error = -zfs_read(ip, &uio, flags, cr); | |
130 | if (error < 0) | |
131 | return (error); | |
132 | ||
133 | return (len - uio.uio_resid); | |
134 | } | |
135 | ||
136 | static ssize_t | |
137 | zpl_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos) | |
138 | { | |
81e97e21 | 139 | cred_t *cr = CRED(); |
1efb473f BB |
140 | ssize_t read; |
141 | ||
81e97e21 | 142 | crhold(cr); |
1efb473f BB |
143 | read = zpl_read_common(filp->f_mapping->host, buf, len, *ppos, |
144 | UIO_USERSPACE, filp->f_flags, cr); | |
81e97e21 | 145 | crfree(cr); |
1efb473f BB |
146 | |
147 | if (read < 0) | |
148 | return (read); | |
149 | ||
150 | *ppos += read; | |
151 | return (read); | |
152 | } | |
153 | ||
154 | ssize_t | |
155 | zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t pos, | |
156 | uio_seg_t segment, int flags, cred_t *cr) | |
157 | { | |
158 | int error; | |
159 | struct iovec iov; | |
160 | uio_t uio; | |
161 | ||
162 | iov.iov_base = (void *)buf; | |
163 | iov.iov_len = len; | |
164 | ||
165 | uio.uio_iov = &iov; | |
166 | uio.uio_resid = len, | |
167 | uio.uio_iovcnt = 1; | |
168 | uio.uio_loffset = pos; | |
169 | uio.uio_limit = MAXOFFSET_T; | |
170 | uio.uio_segflg = segment; | |
171 | ||
172 | error = -zfs_write(ip, &uio, flags, cr); | |
173 | if (error < 0) | |
174 | return (error); | |
175 | ||
176 | return (len - uio.uio_resid); | |
177 | } | |
178 | ||
179 | static ssize_t | |
180 | zpl_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos) | |
181 | { | |
81e97e21 | 182 | cred_t *cr = CRED(); |
1efb473f BB |
183 | ssize_t wrote; |
184 | ||
81e97e21 | 185 | crhold(cr); |
1efb473f BB |
186 | wrote = zpl_write_common(filp->f_mapping->host, buf, len, *ppos, |
187 | UIO_USERSPACE, filp->f_flags, cr); | |
81e97e21 | 188 | crfree(cr); |
1efb473f BB |
189 | |
190 | if (wrote < 0) | |
191 | return (wrote); | |
192 | ||
193 | *ppos += wrote; | |
194 | return (wrote); | |
195 | } | |
196 | ||
c0d35759 BB |
197 | /* |
198 | * It's worth taking a moment to describe how mmap is implemented | |
199 | * for zfs because it differs considerably from other Linux filesystems. | |
200 | * However, this issue is handled the same way under OpenSolaris. | |
201 | * | |
202 | * The issue is that by design zfs bypasses the Linux page cache and | |
203 | * leaves all caching up to the ARC. This has been shown to work | |
204 | * well for the common read(2)/write(2) case. However, mmap(2) | |
205 | * is problem because it relies on being tightly integrated with the | |
206 | * page cache. To handle this we cache mmap'ed files twice, once in | |
207 | * the ARC and a second time in the page cache. The code is careful | |
208 | * to keep both copies synchronized. | |
209 | * | |
210 | * When a file with an mmap'ed region is written to using write(2) | |
211 | * both the data in the ARC and existing pages in the page cache | |
212 | * are updated. For a read(2) data will be read first from the page | |
213 | * cache then the ARC if needed. Neither a write(2) or read(2) will | |
214 | * will ever result in new pages being added to the page cache. | |
215 | * | |
216 | * New pages are added to the page cache only via .readpage() which | |
217 | * is called when the vfs needs to read a page off disk to back the | |
218 | * virtual memory region. These pages may be modified without | |
219 | * notifying the ARC and will be written out periodically via | |
220 | * .writepage(). This will occur due to either a sync or the usual | |
221 | * page aging behavior. Note because a read(2) of a mmap'ed file | |
222 | * will always check the page cache first even when the ARC is out | |
223 | * of date correct data will still be returned. | |
224 | * | |
225 | * While this implementation ensures correct behavior it does have | |
226 | * have some drawbacks. The most obvious of which is that it | |
227 | * increases the required memory footprint when access mmap'ed | |
228 | * files. It also adds additional complexity to the code keeping | |
229 | * both caches synchronized. | |
230 | * | |
231 | * Longer term it may be possible to cleanly resolve this wart by | |
232 | * mapping page cache pages directly on to the ARC buffers. The | |
233 | * Linux address space operations are flexible enough to allow | |
234 | * selection of which pages back a particular index. The trick | |
235 | * would be working out the details of which subsystem is in | |
236 | * charge, the ARC, the page cache, or both. It may also prove | |
237 | * helpful to move the ARC buffers to a scatter-gather lists | |
238 | * rather than a vmalloc'ed region. | |
239 | */ | |
240 | static int | |
241 | zpl_mmap(struct file *filp, struct vm_area_struct *vma) | |
242 | { | |
243 | znode_t *zp = ITOZ(filp->f_mapping->host); | |
244 | int error; | |
245 | ||
246 | error = generic_file_mmap(filp, vma); | |
247 | if (error) | |
248 | return (error); | |
249 | ||
250 | mutex_enter(&zp->z_lock); | |
251 | zp->z_is_mapped = 1; | |
252 | mutex_exit(&zp->z_lock); | |
253 | ||
254 | return (error); | |
255 | } | |
256 | ||
dde471ef PJ |
257 | static struct page ** |
258 | pages_vector_from_list(struct list_head *pages, unsigned nr_pages) | |
259 | { | |
260 | struct page **pl; | |
261 | struct page *t; | |
262 | unsigned page_idx; | |
263 | ||
264 | pl = kmalloc(sizeof(*pl) * nr_pages, GFP_NOFS); | |
265 | if (!pl) | |
266 | return ERR_PTR(-ENOMEM); | |
267 | ||
268 | page_idx = 0; | |
269 | list_for_each_entry_reverse(t, pages, lru) { | |
270 | pl[page_idx] = t; | |
271 | page_idx++; | |
272 | } | |
273 | ||
274 | return pl; | |
275 | } | |
276 | ||
277 | static int | |
278 | zpl_readpages(struct file *file, struct address_space *mapping, | |
279 | struct list_head *pages, unsigned nr_pages) | |
280 | { | |
281 | struct inode *ip; | |
282 | struct page **pl; | |
283 | struct page *p, *n; | |
284 | int error; | |
285 | ||
286 | ip = mapping->host; | |
287 | ||
288 | pl = pages_vector_from_list(pages, nr_pages); | |
289 | if (IS_ERR(pl)) | |
290 | return PTR_ERR(pl); | |
291 | ||
292 | error = -zfs_getpage(ip, pl, nr_pages); | |
293 | if (error) | |
294 | goto error; | |
295 | ||
296 | list_for_each_entry_safe_reverse(p, n, pages, lru) { | |
297 | ||
298 | list_del(&p->lru); | |
299 | ||
300 | flush_dcache_page(p); | |
301 | SetPageUptodate(p); | |
302 | unlock_page(p); | |
303 | page_cache_release(p); | |
304 | } | |
305 | ||
306 | error: | |
307 | kfree(pl); | |
308 | return error; | |
309 | } | |
310 | ||
c0d35759 BB |
311 | /* |
312 | * Populate a page with data for the Linux page cache. This function is | |
313 | * only used to support mmap(2). There will be an identical copy of the | |
314 | * data in the ARC which is kept up to date via .write() and .writepage(). | |
315 | * | |
316 | * Current this function relies on zpl_read_common() and the O_DIRECT | |
317 | * flag to read in a page. This works but the more correct way is to | |
318 | * update zfs_fillpage() to be Linux friendly and use that interface. | |
319 | */ | |
320 | static int | |
321 | zpl_readpage(struct file *filp, struct page *pp) | |
322 | { | |
323 | struct inode *ip; | |
dde471ef | 324 | struct page *pl[1]; |
c0d35759 BB |
325 | int error = 0; |
326 | ||
327 | ASSERT(PageLocked(pp)); | |
328 | ip = pp->mapping->host; | |
dde471ef | 329 | pl[0] = pp; |
c0d35759 | 330 | |
dde471ef | 331 | error = -zfs_getpage(ip, pl, 1); |
c0d35759 | 332 | |
dde471ef PJ |
333 | if (error) { |
334 | SetPageError(pp); | |
335 | ClearPageUptodate(pp); | |
336 | } else { | |
337 | ClearPageError(pp); | |
338 | SetPageUptodate(pp); | |
339 | flush_dcache_page(pp); | |
340 | } | |
c0d35759 | 341 | |
dde471ef PJ |
342 | unlock_page(pp); |
343 | return error; | |
344 | } | |
c0d35759 | 345 | |
dde471ef PJ |
346 | int |
347 | zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data) | |
348 | { | |
349 | int error; | |
c0d35759 | 350 | |
dde471ef | 351 | error = -zfs_putpage(pp, wbc, data); |
c0d35759 BB |
352 | |
353 | if (error) { | |
354 | SetPageError(pp); | |
355 | ClearPageUptodate(pp); | |
356 | } else { | |
357 | ClearPageError(pp); | |
358 | SetPageUptodate(pp); | |
359 | flush_dcache_page(pp); | |
360 | } | |
361 | ||
362 | unlock_page(pp); | |
dde471ef PJ |
363 | return error; |
364 | } | |
c0d35759 | 365 | |
dde471ef PJ |
366 | static int |
367 | zpl_writepages(struct address_space *mapping, struct writeback_control *wbc) | |
368 | { | |
369 | return write_cache_pages(mapping, wbc, zpl_putpage, mapping); | |
c0d35759 BB |
370 | } |
371 | ||
372 | /* | |
373 | * Write out dirty pages to the ARC, this function is only required to | |
374 | * support mmap(2). Mapped pages may be dirtied by memory operations | |
375 | * which never call .write(). These dirty pages are kept in sync with | |
376 | * the ARC buffers via this hook. | |
c0d35759 BB |
377 | */ |
378 | static int | |
379 | zpl_writepage(struct page *pp, struct writeback_control *wbc) | |
380 | { | |
dde471ef | 381 | return zpl_putpage(pp, wbc, pp->mapping); |
c0d35759 BB |
382 | } |
383 | ||
1efb473f | 384 | const struct address_space_operations zpl_address_space_operations = { |
dde471ef | 385 | .readpages = zpl_readpages, |
1efb473f BB |
386 | .readpage = zpl_readpage, |
387 | .writepage = zpl_writepage, | |
dde471ef | 388 | .writepages = zpl_writepages, |
1efb473f BB |
389 | }; |
390 | ||
391 | const struct file_operations zpl_file_operations = { | |
126400a1 BB |
392 | .open = zpl_open, |
393 | .release = zpl_release, | |
1efb473f | 394 | .llseek = generic_file_llseek, |
c0d35759 BB |
395 | .read = zpl_read, |
396 | .write = zpl_write, | |
1efb473f | 397 | .readdir = zpl_readdir, |
c0d35759 | 398 | .mmap = zpl_mmap, |
1efb473f | 399 | .fsync = zpl_fsync, |
1efb473f BB |
400 | }; |
401 | ||
402 | const struct file_operations zpl_dir_file_operations = { | |
403 | .llseek = generic_file_llseek, | |
404 | .read = generic_read_dir, | |
405 | .readdir = zpl_readdir, | |
406 | .fsync = zpl_fsync, | |
407 | }; |