]> git.proxmox.com Git - mirror_zfs.git/blob - module/zfs/zpl_file.c
Write dirty inodes on 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 = CRED();
36 int error;
37
38 crhold(cr);
39 error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr);
40 crfree(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 = CRED();
53 int error;
54
55 if (ITOZ(ip)->z_atime_dirty)
56 mark_inode_dirty(ip);
57
58 crhold(cr);
59 error = -zfs_close(ip, filp->f_flags, cr);
60 crfree(cr);
61 ASSERT3S(error, <=, 0);
62
63 return (error);
64 }
65
66 static int
67 zpl_readdir(struct file *filp, void *dirent, filldir_t filldir)
68 {
69 struct dentry *dentry = filp->f_path.dentry;
70 cred_t *cr = CRED();
71 int error;
72
73 crhold(cr);
74 error = -zfs_readdir(dentry->d_inode, dirent, filldir,
75 &filp->f_pos, cr);
76 crfree(cr);
77 ASSERT3S(error, <=, 0);
78
79 return (error);
80 }
81
82 #if defined(HAVE_FSYNC_WITH_DENTRY)
83 /*
84 * Linux 2.6.x - 2.6.34 API,
85 * Through 2.6.34 the nfsd kernel server would pass a NULL 'file struct *'
86 * to the fops->fsync() hook. For this reason, we must be careful not to
87 * use filp unconditionally.
88 */
89 static int
90 zpl_fsync(struct file *filp, struct dentry *dentry, int datasync)
91 {
92 cred_t *cr = CRED();
93 int error;
94
95 crhold(cr);
96 error = -zfs_fsync(dentry->d_inode, datasync, cr);
97 crfree(cr);
98 ASSERT3S(error, <=, 0);
99
100 return (error);
101 }
102
103 #elif defined(HAVE_FSYNC_WITHOUT_DENTRY)
104 /*
105 * Linux 2.6.35 - 3.0 API,
106 * As of 2.6.35 the dentry argument to the fops->fsync() hook was deemed
107 * redundant. The dentry is still accessible via filp->f_path.dentry,
108 * and we are guaranteed that filp will never be NULL.
109 */
110 static int
111 zpl_fsync(struct file *filp, int datasync)
112 {
113 struct inode *inode = filp->f_mapping->host;
114 cred_t *cr = CRED();
115 int error;
116
117 crhold(cr);
118 error = -zfs_fsync(inode, datasync, cr);
119 crfree(cr);
120 ASSERT3S(error, <=, 0);
121
122 return (error);
123 }
124
125 #elif defined(HAVE_FSYNC_RANGE)
126 /*
127 * Linux 3.1 - 3.x API,
128 * As of 3.1 the responsibility to call filemap_write_and_wait_range() has
129 * been pushed down in to the .fsync() vfs hook. Additionally, the i_mutex
130 * lock is no longer held by the caller, for zfs we don't require the lock
131 * to be held so we don't acquire it.
132 */
133 static int
134 zpl_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
135 {
136 struct inode *inode = filp->f_mapping->host;
137 cred_t *cr = CRED();
138 int error;
139
140 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
141 if (error)
142 return (error);
143
144 crhold(cr);
145 error = -zfs_fsync(inode, datasync, cr);
146 crfree(cr);
147 ASSERT3S(error, <=, 0);
148
149 return (error);
150 }
151 #else
152 #error "Unsupported fops->fsync() implementation"
153 #endif
154
155 ssize_t
156 zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t pos,
157 uio_seg_t segment, int flags, cred_t *cr)
158 {
159 int error;
160 struct iovec iov;
161 uio_t uio;
162
163 iov.iov_base = (void *)buf;
164 iov.iov_len = len;
165
166 uio.uio_iov = &iov;
167 uio.uio_resid = len;
168 uio.uio_iovcnt = 1;
169 uio.uio_loffset = pos;
170 uio.uio_limit = MAXOFFSET_T;
171 uio.uio_segflg = segment;
172
173 error = -zfs_read(ip, &uio, flags, cr);
174 if (error < 0)
175 return (error);
176
177 return (len - uio.uio_resid);
178 }
179
180 static ssize_t
181 zpl_read(struct file *filp, char __user *buf, size_t len, loff_t *ppos)
182 {
183 cred_t *cr = CRED();
184 ssize_t read;
185
186 crhold(cr);
187 read = zpl_read_common(filp->f_mapping->host, buf, len, *ppos,
188 UIO_USERSPACE, filp->f_flags, cr);
189 crfree(cr);
190
191 if (read < 0)
192 return (read);
193
194 *ppos += read;
195 return (read);
196 }
197
198 ssize_t
199 zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t pos,
200 uio_seg_t segment, int flags, cred_t *cr)
201 {
202 int error;
203 struct iovec iov;
204 uio_t uio;
205
206 iov.iov_base = (void *)buf;
207 iov.iov_len = len;
208
209 uio.uio_iov = &iov;
210 uio.uio_resid = len,
211 uio.uio_iovcnt = 1;
212 uio.uio_loffset = pos;
213 uio.uio_limit = MAXOFFSET_T;
214 uio.uio_segflg = segment;
215
216 error = -zfs_write(ip, &uio, flags, cr);
217 if (error < 0)
218 return (error);
219
220 return (len - uio.uio_resid);
221 }
222
223 static ssize_t
224 zpl_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
225 {
226 cred_t *cr = CRED();
227 ssize_t wrote;
228
229 crhold(cr);
230 wrote = zpl_write_common(filp->f_mapping->host, buf, len, *ppos,
231 UIO_USERSPACE, filp->f_flags, cr);
232 crfree(cr);
233
234 if (wrote < 0)
235 return (wrote);
236
237 *ppos += wrote;
238 return (wrote);
239 }
240
241 static loff_t
242 zpl_llseek(struct file *filp, loff_t offset, int whence)
243 {
244 #if defined(SEEK_HOLE) && defined(SEEK_DATA)
245 if (whence == SEEK_DATA || whence == SEEK_HOLE) {
246 struct inode *ip = filp->f_mapping->host;
247 loff_t maxbytes = ip->i_sb->s_maxbytes;
248 loff_t error;
249
250 spl_inode_lock(ip);
251 error = -zfs_holey(ip, whence, &offset);
252 if (error == 0)
253 error = lseek_execute(filp, ip, offset, maxbytes);
254 spl_inode_unlock(ip);
255
256 return (error);
257 }
258 #endif /* SEEK_HOLE && SEEK_DATA */
259
260 return generic_file_llseek(filp, offset, whence);
261 }
262
263 /*
264 * It's worth taking a moment to describe how mmap is implemented
265 * for zfs because it differs considerably from other Linux filesystems.
266 * However, this issue is handled the same way under OpenSolaris.
267 *
268 * The issue is that by design zfs bypasses the Linux page cache and
269 * leaves all caching up to the ARC. This has been shown to work
270 * well for the common read(2)/write(2) case. However, mmap(2)
271 * is problem because it relies on being tightly integrated with the
272 * page cache. To handle this we cache mmap'ed files twice, once in
273 * the ARC and a second time in the page cache. The code is careful
274 * to keep both copies synchronized.
275 *
276 * When a file with an mmap'ed region is written to using write(2)
277 * both the data in the ARC and existing pages in the page cache
278 * are updated. For a read(2) data will be read first from the page
279 * cache then the ARC if needed. Neither a write(2) or read(2) will
280 * will ever result in new pages being added to the page cache.
281 *
282 * New pages are added to the page cache only via .readpage() which
283 * is called when the vfs needs to read a page off disk to back the
284 * virtual memory region. These pages may be modified without
285 * notifying the ARC and will be written out periodically via
286 * .writepage(). This will occur due to either a sync or the usual
287 * page aging behavior. Note because a read(2) of a mmap'ed file
288 * will always check the page cache first even when the ARC is out
289 * of date correct data will still be returned.
290 *
291 * While this implementation ensures correct behavior it does have
292 * have some drawbacks. The most obvious of which is that it
293 * increases the required memory footprint when access mmap'ed
294 * files. It also adds additional complexity to the code keeping
295 * both caches synchronized.
296 *
297 * Longer term it may be possible to cleanly resolve this wart by
298 * mapping page cache pages directly on to the ARC buffers. The
299 * Linux address space operations are flexible enough to allow
300 * selection of which pages back a particular index. The trick
301 * would be working out the details of which subsystem is in
302 * charge, the ARC, the page cache, or both. It may also prove
303 * helpful to move the ARC buffers to a scatter-gather lists
304 * rather than a vmalloc'ed region.
305 */
306 static int
307 zpl_mmap(struct file *filp, struct vm_area_struct *vma)
308 {
309 struct inode *ip = filp->f_mapping->host;
310 znode_t *zp = ITOZ(ip);
311 int error;
312
313 error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start,
314 (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags);
315 if (error)
316 return (error);
317
318 error = generic_file_mmap(filp, vma);
319 if (error)
320 return (error);
321
322 mutex_enter(&zp->z_lock);
323 zp->z_is_mapped = 1;
324 mutex_exit(&zp->z_lock);
325
326 return (error);
327 }
328
329 /*
330 * Populate a page with data for the Linux page cache. This function is
331 * only used to support mmap(2). There will be an identical copy of the
332 * data in the ARC which is kept up to date via .write() and .writepage().
333 *
334 * Current this function relies on zpl_read_common() and the O_DIRECT
335 * flag to read in a page. This works but the more correct way is to
336 * update zfs_fillpage() to be Linux friendly and use that interface.
337 */
338 static int
339 zpl_readpage(struct file *filp, struct page *pp)
340 {
341 struct inode *ip;
342 struct page *pl[1];
343 int error = 0;
344
345 ASSERT(PageLocked(pp));
346 ip = pp->mapping->host;
347 pl[0] = pp;
348
349 error = -zfs_getpage(ip, pl, 1);
350
351 if (error) {
352 SetPageError(pp);
353 ClearPageUptodate(pp);
354 } else {
355 ClearPageError(pp);
356 SetPageUptodate(pp);
357 flush_dcache_page(pp);
358 }
359
360 unlock_page(pp);
361 return error;
362 }
363
364 /*
365 * Populate a set of pages with data for the Linux page cache. This
366 * function will only be called for read ahead and never for demand
367 * paging. For simplicity, the code relies on read_cache_pages() to
368 * correctly lock each page for IO and call zpl_readpage().
369 */
370 static int
371 zpl_readpages(struct file *filp, struct address_space *mapping,
372 struct list_head *pages, unsigned nr_pages)
373 {
374 return (read_cache_pages(mapping, pages,
375 (filler_t *)zpl_readpage, filp));
376 }
377
378 int
379 zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data)
380 {
381 struct address_space *mapping = data;
382
383 ASSERT(PageLocked(pp));
384 ASSERT(!PageWriteback(pp));
385 ASSERT(!(current->flags & PF_NOFS));
386
387 /*
388 * Annotate this call path with a flag that indicates that it is
389 * unsafe to use KM_SLEEP during memory allocations due to the
390 * potential for a deadlock. KM_PUSHPAGE should be used instead.
391 */
392 current->flags |= PF_NOFS;
393 (void) zfs_putpage(mapping->host, pp, wbc);
394 current->flags &= ~PF_NOFS;
395
396 return (0);
397 }
398
399 static int
400 zpl_writepages(struct address_space *mapping, struct writeback_control *wbc)
401 {
402 return write_cache_pages(mapping, wbc, zpl_putpage, mapping);
403 }
404
405 /*
406 * Write out dirty pages to the ARC, this function is only required to
407 * support mmap(2). Mapped pages may be dirtied by memory operations
408 * which never call .write(). These dirty pages are kept in sync with
409 * the ARC buffers via this hook.
410 */
411 static int
412 zpl_writepage(struct page *pp, struct writeback_control *wbc)
413 {
414 return zpl_putpage(pp, wbc, pp->mapping);
415 }
416
417 /*
418 * The only flag combination which matches the behavior of zfs_space()
419 * is FALLOC_FL_PUNCH_HOLE. This flag was introduced in the 2.6.38 kernel.
420 */
421 long
422 zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len)
423 {
424 cred_t *cr = CRED();
425 int error = -EOPNOTSUPP;
426
427 if (mode & FALLOC_FL_KEEP_SIZE)
428 return (-EOPNOTSUPP);
429
430 crhold(cr);
431
432 #ifdef FALLOC_FL_PUNCH_HOLE
433 if (mode & FALLOC_FL_PUNCH_HOLE) {
434 flock64_t bf;
435
436 bf.l_type = F_WRLCK;
437 bf.l_whence = 0;
438 bf.l_start = offset;
439 bf.l_len = len;
440 bf.l_pid = 0;
441
442 error = -zfs_space(ip, F_FREESP, &bf, FWRITE, offset, cr);
443 }
444 #endif /* FALLOC_FL_PUNCH_HOLE */
445
446 crfree(cr);
447
448 ASSERT3S(error, <=, 0);
449 return (error);
450 }
451
452 #ifdef HAVE_FILE_FALLOCATE
453 static long
454 zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
455 {
456 return zpl_fallocate_common(filp->f_path.dentry->d_inode,
457 mode, offset, len);
458 }
459 #endif /* HAVE_FILE_FALLOCATE */
460
461 static long
462 zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
463 {
464 switch (cmd) {
465 case ZFS_IOC_GETFLAGS:
466 case ZFS_IOC_SETFLAGS:
467 return (-EOPNOTSUPP);
468 default:
469 return (-ENOTTY);
470 }
471 }
472
473 #ifdef CONFIG_COMPAT
474 static long
475 zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
476 {
477 return zpl_ioctl(filp, cmd, arg);
478 }
479 #endif /* CONFIG_COMPAT */
480
481
482 const struct address_space_operations zpl_address_space_operations = {
483 .readpages = zpl_readpages,
484 .readpage = zpl_readpage,
485 .writepage = zpl_writepage,
486 .writepages = zpl_writepages,
487 };
488
489 const struct file_operations zpl_file_operations = {
490 .open = zpl_open,
491 .release = zpl_release,
492 .llseek = zpl_llseek,
493 .read = zpl_read,
494 .write = zpl_write,
495 .mmap = zpl_mmap,
496 .fsync = zpl_fsync,
497 #ifdef HAVE_FILE_FALLOCATE
498 .fallocate = zpl_fallocate,
499 #endif /* HAVE_FILE_FALLOCATE */
500 .unlocked_ioctl = zpl_ioctl,
501 #ifdef CONFIG_COMPAT
502 .compat_ioctl = zpl_compat_ioctl,
503 #endif
504 };
505
506 const struct file_operations zpl_dir_file_operations = {
507 .llseek = generic_file_llseek,
508 .read = generic_read_dir,
509 .readdir = zpl_readdir,
510 .fsync = zpl_fsync,
511 .unlocked_ioctl = zpl_ioctl,
512 #ifdef CONFIG_COMPAT
513 .compat_ioctl = zpl_compat_ioctl,
514 #endif
515 };