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