]> git.proxmox.com Git - mirror_zfs-debian.git/blame - module/zfs/zpl_file.c
Merge branch 'add_breaks_replaces_zfs_initramfs' into 'master'
[mirror_zfs-debian.git] / module / zfs / zpl_file.c
CommitLineData
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.
e10b0808 23 * Copyright (c) 2015 by Chunwei Chen. All rights reserved.
1efb473f
BB
24 */
25
26
5eacc075
AX
27#ifdef CONFIG_COMPAT
28#include <linux/compat.h>
29#endif
a08ee875 30#include <sys/dmu_objset.h>
1efb473f
BB
31#include <sys/zfs_vfsops.h>
32#include <sys/zfs_vnops.h>
33#include <sys/zfs_znode.h>
34#include <sys/zpl.h>
35
36
126400a1
BB
37static int
38zpl_open(struct inode *ip, struct file *filp)
39{
81e97e21 40 cred_t *cr = CRED();
126400a1 41 int error;
ea04106b 42 fstrans_cookie_t cookie;
126400a1 43
a08ee875
LG
44 error = generic_file_open(ip, filp);
45 if (error)
46 return (error);
47
81e97e21 48 crhold(cr);
ea04106b 49 cookie = spl_fstrans_mark();
126400a1 50 error = -zfs_open(ip, filp->f_mode, filp->f_flags, cr);
ea04106b 51 spl_fstrans_unmark(cookie);
81e97e21 52 crfree(cr);
126400a1
BB
53 ASSERT3S(error, <=, 0);
54
a08ee875 55 return (error);
126400a1
BB
56}
57
58static int
59zpl_release(struct inode *ip, struct file *filp)
60{
81e97e21 61 cred_t *cr = CRED();
126400a1 62 int error;
ea04106b 63 fstrans_cookie_t cookie;
126400a1 64
ea04106b 65 cookie = spl_fstrans_mark();
c06d4368 66 if (ITOZ(ip)->z_atime_dirty)
ea04106b 67 zfs_mark_inode_dirty(ip);
c06d4368 68
81e97e21 69 crhold(cr);
126400a1 70 error = -zfs_close(ip, filp->f_flags, cr);
ea04106b 71 spl_fstrans_unmark(cookie);
81e97e21 72 crfree(cr);
126400a1
BB
73 ASSERT3S(error, <=, 0);
74
75 return (error);
76}
77
1efb473f 78static int
42f7b73b 79zpl_iterate(struct file *filp, zpl_dir_context_t *ctx)
1efb473f 80{
81e97e21 81 cred_t *cr = CRED();
1efb473f 82 int error;
ea04106b 83 fstrans_cookie_t cookie;
1efb473f 84
81e97e21 85 crhold(cr);
ea04106b 86 cookie = spl_fstrans_mark();
cae5b340 87 error = -zfs_readdir(file_inode(filp), ctx, cr);
ea04106b 88 spl_fstrans_unmark(cookie);
81e97e21 89 crfree(cr);
1efb473f
BB
90 ASSERT3S(error, <=, 0);
91
92 return (error);
93}
94
87dac73d 95#if !defined(HAVE_VFS_ITERATE) && !defined(HAVE_VFS_ITERATE_SHARED)
c06d4368
AX
96static int
97zpl_readdir(struct file *filp, void *dirent, filldir_t filldir)
98{
42f7b73b
AX
99 zpl_dir_context_t ctx =
100 ZPL_DIR_CONTEXT_INIT(dirent, filldir, filp->f_pos);
c06d4368
AX
101 int error;
102
103 error = zpl_iterate(filp, &ctx);
104 filp->f_pos = ctx.pos;
105
106 return (error);
107}
42f7b73b 108#endif /* !HAVE_VFS_ITERATE && !HAVE_VFS_ITERATE_SHARED */
c06d4368 109
adcd70bd 110#if defined(HAVE_FSYNC_WITH_DENTRY)
3117dd0b 111/*
adcd70bd
BB
112 * Linux 2.6.x - 2.6.34 API,
113 * Through 2.6.34 the nfsd kernel server would pass a NULL 'file struct *'
114 * to the fops->fsync() hook. For this reason, we must be careful not to
115 * use filp unconditionally.
116 */
117static int
118zpl_fsync(struct file *filp, struct dentry *dentry, int datasync)
119{
120 cred_t *cr = CRED();
121 int error;
ea04106b 122 fstrans_cookie_t cookie;
adcd70bd
BB
123
124 crhold(cr);
ea04106b 125 cookie = spl_fstrans_mark();
adcd70bd 126 error = -zfs_fsync(dentry->d_inode, datasync, cr);
ea04106b 127 spl_fstrans_unmark(cookie);
adcd70bd
BB
128 crfree(cr);
129 ASSERT3S(error, <=, 0);
130
131 return (error);
132}
133
68d83c55 134#ifdef HAVE_FILE_AIO_FSYNC
ea04106b
AX
135static int
136zpl_aio_fsync(struct kiocb *kiocb, int datasync)
137{
138 struct file *filp = kiocb->ki_filp;
cae5b340 139 return (zpl_fsync(filp, file_dentry(filp), datasync));
ea04106b 140}
68d83c55
AX
141#endif
142
adcd70bd
BB
143#elif defined(HAVE_FSYNC_WITHOUT_DENTRY)
144/*
145 * Linux 2.6.35 - 3.0 API,
146 * As of 2.6.35 the dentry argument to the fops->fsync() hook was deemed
3117dd0b
BB
147 * redundant. The dentry is still accessible via filp->f_path.dentry,
148 * and we are guaranteed that filp will never be NULL.
3117dd0b 149 */
3117dd0b
BB
150static int
151zpl_fsync(struct file *filp, int datasync)
152{
adcd70bd
BB
153 struct inode *inode = filp->f_mapping->host;
154 cred_t *cr = CRED();
155 int error;
ea04106b 156 fstrans_cookie_t cookie;
adcd70bd
BB
157
158 crhold(cr);
ea04106b 159 cookie = spl_fstrans_mark();
adcd70bd 160 error = -zfs_fsync(inode, datasync, cr);
ea04106b 161 spl_fstrans_unmark(cookie);
adcd70bd
BB
162 crfree(cr);
163 ASSERT3S(error, <=, 0);
164
165 return (error);
166}
167
68d83c55 168#ifdef HAVE_FILE_AIO_FSYNC
ea04106b
AX
169static int
170zpl_aio_fsync(struct kiocb *kiocb, int datasync)
171{
172 return (zpl_fsync(kiocb->ki_filp, datasync));
173}
68d83c55
AX
174#endif
175
adcd70bd
BB
176#elif defined(HAVE_FSYNC_RANGE)
177/*
178 * Linux 3.1 - 3.x API,
179 * As of 3.1 the responsibility to call filemap_write_and_wait_range() has
180 * been pushed down in to the .fsync() vfs hook. Additionally, the i_mutex
181 * lock is no longer held by the caller, for zfs we don't require the lock
182 * to be held so we don't acquire it.
183 */
3117dd0b 184static int
adcd70bd 185zpl_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
1efb473f 186{
adcd70bd 187 struct inode *inode = filp->f_mapping->host;
81e97e21 188 cred_t *cr = CRED();
1efb473f 189 int error;
ea04106b 190 fstrans_cookie_t cookie;
1efb473f 191
adcd70bd
BB
192 error = filemap_write_and_wait_range(inode->i_mapping, start, end);
193 if (error)
194 return (error);
195
81e97e21 196 crhold(cr);
ea04106b 197 cookie = spl_fstrans_mark();
adcd70bd 198 error = -zfs_fsync(inode, datasync, cr);
ea04106b 199 spl_fstrans_unmark(cookie);
81e97e21 200 crfree(cr);
1efb473f
BB
201 ASSERT3S(error, <=, 0);
202
203 return (error);
204}
ea04106b 205
68d83c55 206#ifdef HAVE_FILE_AIO_FSYNC
ea04106b
AX
207static int
208zpl_aio_fsync(struct kiocb *kiocb, int datasync)
209{
210 return (zpl_fsync(kiocb->ki_filp, kiocb->ki_pos, -1, datasync));
211}
68d83c55
AX
212#endif
213
adcd70bd
BB
214#else
215#error "Unsupported fops->fsync() implementation"
216#endif
1efb473f 217
e10b0808 218static ssize_t
ea04106b 219zpl_read_common_iovec(struct inode *ip, const struct iovec *iovp, size_t count,
e10b0808
AX
220 unsigned long nr_segs, loff_t *ppos, uio_seg_t segment, int flags,
221 cred_t *cr, size_t skip)
1efb473f 222{
a08ee875 223 ssize_t read;
1efb473f 224 uio_t uio;
ea04106b
AX
225 int error;
226 fstrans_cookie_t cookie;
1efb473f 227
e10b0808
AX
228 uio.uio_iov = iovp;
229 uio.uio_skip = skip;
ea04106b
AX
230 uio.uio_resid = count;
231 uio.uio_iovcnt = nr_segs;
232 uio.uio_loffset = *ppos;
1efb473f
BB
233 uio.uio_limit = MAXOFFSET_T;
234 uio.uio_segflg = segment;
235
ea04106b 236 cookie = spl_fstrans_mark();
1efb473f 237 error = -zfs_read(ip, &uio, flags, cr);
ea04106b 238 spl_fstrans_unmark(cookie);
1efb473f
BB
239 if (error < 0)
240 return (error);
241
ea04106b
AX
242 read = count - uio.uio_resid;
243 *ppos += read;
a08ee875
LG
244 task_io_account_read(read);
245
246 return (read);
1efb473f
BB
247}
248
ea04106b
AX
249inline ssize_t
250zpl_read_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos,
251 uio_seg_t segment, int flags, cred_t *cr)
252{
253 struct iovec iov;
254
255 iov.iov_base = (void *)buf;
256 iov.iov_len = len;
257
258 return (zpl_read_common_iovec(ip, &iov, len, 1, ppos, segment,
e10b0808 259 flags, cr, 0));
ea04106b
AX
260}
261
ea04106b
AX
262static ssize_t
263zpl_iter_read_common(struct kiocb *kiocb, const struct iovec *iovp,
e10b0808 264 unsigned long nr_segs, size_t count, uio_seg_t seg, size_t skip)
ea04106b
AX
265{
266 cred_t *cr = CRED();
267 struct file *filp = kiocb->ki_filp;
268 ssize_t read;
ea04106b
AX
269
270 crhold(cr);
e10b0808
AX
271 read = zpl_read_common_iovec(filp->f_mapping->host, iovp, count,
272 nr_segs, &kiocb->ki_pos, seg, filp->f_flags, cr, skip);
ea04106b
AX
273 crfree(cr);
274
68d83c55 275 file_accessed(filp);
1efb473f
BB
276 return (read);
277}
278
ea04106b
AX
279#if defined(HAVE_VFS_RW_ITERATE)
280static ssize_t
281zpl_iter_read(struct kiocb *kiocb, struct iov_iter *to)
282{
e10b0808
AX
283 ssize_t ret;
284 uio_seg_t seg = UIO_USERSPACE;
285 if (to->type & ITER_KVEC)
286 seg = UIO_SYSSPACE;
287 if (to->type & ITER_BVEC)
288 seg = UIO_BVEC;
289 ret = zpl_iter_read_common(kiocb, to->iov, to->nr_segs,
290 iov_iter_count(to), seg, to->iov_offset);
291 if (ret > 0)
292 iov_iter_advance(to, ret);
293 return (ret);
ea04106b
AX
294}
295#else
296static ssize_t
297zpl_aio_read(struct kiocb *kiocb, const struct iovec *iovp,
298 unsigned long nr_segs, loff_t pos)
299{
68d83c55
AX
300 ssize_t ret;
301 size_t count;
302
303 ret = generic_segment_checks(iovp, &nr_segs, &count, VERIFY_WRITE);
304 if (ret)
305 return (ret);
306
307 return (zpl_iter_read_common(kiocb, iovp, nr_segs, count,
e10b0808 308 UIO_USERSPACE, 0));
ea04106b
AX
309}
310#endif /* HAVE_VFS_RW_ITERATE */
311
e10b0808 312static ssize_t
ea04106b 313zpl_write_common_iovec(struct inode *ip, const struct iovec *iovp, size_t count,
e10b0808
AX
314 unsigned long nr_segs, loff_t *ppos, uio_seg_t segment, int flags,
315 cred_t *cr, size_t skip)
1efb473f 316{
a08ee875 317 ssize_t wrote;
1efb473f 318 uio_t uio;
ea04106b
AX
319 int error;
320 fstrans_cookie_t cookie;
1efb473f 321
ea04106b
AX
322 if (flags & O_APPEND)
323 *ppos = i_size_read(ip);
1efb473f 324
e10b0808
AX
325 uio.uio_iov = iovp;
326 uio.uio_skip = skip;
ea04106b
AX
327 uio.uio_resid = count;
328 uio.uio_iovcnt = nr_segs;
329 uio.uio_loffset = *ppos;
1efb473f
BB
330 uio.uio_limit = MAXOFFSET_T;
331 uio.uio_segflg = segment;
332
ea04106b 333 cookie = spl_fstrans_mark();
1efb473f 334 error = -zfs_write(ip, &uio, flags, cr);
ea04106b 335 spl_fstrans_unmark(cookie);
1efb473f
BB
336 if (error < 0)
337 return (error);
338
ea04106b
AX
339 wrote = count - uio.uio_resid;
340 *ppos += wrote;
a08ee875
LG
341 task_io_account_write(wrote);
342
343 return (wrote);
1efb473f 344}
68d83c55 345
ea04106b
AX
346inline ssize_t
347zpl_write_common(struct inode *ip, const char *buf, size_t len, loff_t *ppos,
348 uio_seg_t segment, int flags, cred_t *cr)
349{
350 struct iovec iov;
351
352 iov.iov_base = (void *)buf;
353 iov.iov_len = len;
354
355 return (zpl_write_common_iovec(ip, &iov, len, 1, ppos, segment,
e10b0808 356 flags, cr, 0));
ea04106b 357}
1efb473f 358
ea04106b
AX
359static ssize_t
360zpl_iter_write_common(struct kiocb *kiocb, const struct iovec *iovp,
e10b0808 361 unsigned long nr_segs, size_t count, uio_seg_t seg, size_t skip)
ea04106b
AX
362{
363 cred_t *cr = CRED();
364 struct file *filp = kiocb->ki_filp;
365 ssize_t wrote;
ea04106b
AX
366
367 crhold(cr);
e10b0808
AX
368 wrote = zpl_write_common_iovec(filp->f_mapping->host, iovp, count,
369 nr_segs, &kiocb->ki_pos, seg, filp->f_flags, cr, skip);
ea04106b
AX
370 crfree(cr);
371
1efb473f
BB
372 return (wrote);
373}
374
ea04106b
AX
375#if defined(HAVE_VFS_RW_ITERATE)
376static ssize_t
377zpl_iter_write(struct kiocb *kiocb, struct iov_iter *from)
378{
68d83c55 379 size_t count;
e10b0808
AX
380 ssize_t ret;
381 uio_seg_t seg = UIO_USERSPACE;
68d83c55
AX
382
383#ifndef HAVE_GENERIC_WRITE_CHECKS_KIOCB
384 struct file *file = kiocb->ki_filp;
385 struct address_space *mapping = file->f_mapping;
386 struct inode *ip = mapping->host;
387 int isblk = S_ISBLK(ip->i_mode);
388
389 count = iov_iter_count(from);
390 ret = generic_write_checks(file, &kiocb->ki_pos, &count, isblk);
391 if (ret)
392 return (ret);
393#else
394 /*
395 * XXX - ideally this check should be in the same lock region with
396 * write operations, so that there's no TOCTTOU race when doing
397 * append and someone else grow the file.
398 */
399 ret = generic_write_checks(kiocb, from);
400 if (ret <= 0)
401 return (ret);
402 count = ret;
403#endif
404
e10b0808
AX
405 if (from->type & ITER_KVEC)
406 seg = UIO_SYSSPACE;
407 if (from->type & ITER_BVEC)
408 seg = UIO_BVEC;
68d83c55 409
e10b0808 410 ret = zpl_iter_write_common(kiocb, from->iov, from->nr_segs,
68d83c55 411 count, seg, from->iov_offset);
e10b0808
AX
412 if (ret > 0)
413 iov_iter_advance(from, ret);
68d83c55 414
e10b0808 415 return (ret);
ea04106b
AX
416}
417#else
418static ssize_t
419zpl_aio_write(struct kiocb *kiocb, const struct iovec *iovp,
420 unsigned long nr_segs, loff_t pos)
421{
68d83c55
AX
422 struct file *file = kiocb->ki_filp;
423 struct address_space *mapping = file->f_mapping;
424 struct inode *ip = mapping->host;
425 int isblk = S_ISBLK(ip->i_mode);
426 size_t count;
427 ssize_t ret;
428
429 ret = generic_segment_checks(iovp, &nr_segs, &count, VERIFY_READ);
430 if (ret)
431 return (ret);
432
433 ret = generic_write_checks(file, &pos, &count, isblk);
434 if (ret)
435 return (ret);
436
437 return (zpl_iter_write_common(kiocb, iovp, nr_segs, count,
e10b0808 438 UIO_USERSPACE, 0));
ea04106b
AX
439}
440#endif /* HAVE_VFS_RW_ITERATE */
441
c06d4368
AX
442static loff_t
443zpl_llseek(struct file *filp, loff_t offset, int whence)
444{
445#if defined(SEEK_HOLE) && defined(SEEK_DATA)
ea04106b
AX
446 fstrans_cookie_t cookie;
447
c06d4368
AX
448 if (whence == SEEK_DATA || whence == SEEK_HOLE) {
449 struct inode *ip = filp->f_mapping->host;
450 loff_t maxbytes = ip->i_sb->s_maxbytes;
451 loff_t error;
452
87dac73d 453 spl_inode_lock_shared(ip);
ea04106b 454 cookie = spl_fstrans_mark();
c06d4368 455 error = -zfs_holey(ip, whence, &offset);
ea04106b 456 spl_fstrans_unmark(cookie);
c06d4368
AX
457 if (error == 0)
458 error = lseek_execute(filp, ip, offset, maxbytes);
87dac73d 459 spl_inode_unlock_shared(ip);
c06d4368
AX
460
461 return (error);
462 }
463#endif /* SEEK_HOLE && SEEK_DATA */
464
a08ee875 465 return (generic_file_llseek(filp, offset, whence));
c06d4368
AX
466}
467
c0d35759
BB
468/*
469 * It's worth taking a moment to describe how mmap is implemented
470 * for zfs because it differs considerably from other Linux filesystems.
471 * However, this issue is handled the same way under OpenSolaris.
472 *
473 * The issue is that by design zfs bypasses the Linux page cache and
474 * leaves all caching up to the ARC. This has been shown to work
475 * well for the common read(2)/write(2) case. However, mmap(2)
476 * is problem because it relies on being tightly integrated with the
477 * page cache. To handle this we cache mmap'ed files twice, once in
478 * the ARC and a second time in the page cache. The code is careful
479 * to keep both copies synchronized.
480 *
481 * When a file with an mmap'ed region is written to using write(2)
482 * both the data in the ARC and existing pages in the page cache
483 * are updated. For a read(2) data will be read first from the page
484 * cache then the ARC if needed. Neither a write(2) or read(2) will
485 * will ever result in new pages being added to the page cache.
486 *
487 * New pages are added to the page cache only via .readpage() which
488 * is called when the vfs needs to read a page off disk to back the
489 * virtual memory region. These pages may be modified without
490 * notifying the ARC and will be written out periodically via
491 * .writepage(). This will occur due to either a sync or the usual
492 * page aging behavior. Note because a read(2) of a mmap'ed file
493 * will always check the page cache first even when the ARC is out
494 * of date correct data will still be returned.
495 *
496 * While this implementation ensures correct behavior it does have
497 * have some drawbacks. The most obvious of which is that it
498 * increases the required memory footprint when access mmap'ed
499 * files. It also adds additional complexity to the code keeping
500 * both caches synchronized.
501 *
502 * Longer term it may be possible to cleanly resolve this wart by
503 * mapping page cache pages directly on to the ARC buffers. The
504 * Linux address space operations are flexible enough to allow
505 * selection of which pages back a particular index. The trick
506 * would be working out the details of which subsystem is in
507 * charge, the ARC, the page cache, or both. It may also prove
508 * helpful to move the ARC buffers to a scatter-gather lists
509 * rather than a vmalloc'ed region.
510 */
511static int
512zpl_mmap(struct file *filp, struct vm_area_struct *vma)
513{
e2e7aa2d
BB
514 struct inode *ip = filp->f_mapping->host;
515 znode_t *zp = ITOZ(ip);
c0d35759 516 int error;
ea04106b 517 fstrans_cookie_t cookie;
c0d35759 518
ea04106b 519 cookie = spl_fstrans_mark();
e2e7aa2d
BB
520 error = -zfs_map(ip, vma->vm_pgoff, (caddr_t *)vma->vm_start,
521 (size_t)(vma->vm_end - vma->vm_start), vma->vm_flags);
ea04106b 522 spl_fstrans_unmark(cookie);
e2e7aa2d
BB
523 if (error)
524 return (error);
525
c0d35759
BB
526 error = generic_file_mmap(filp, vma);
527 if (error)
528 return (error);
529
530 mutex_enter(&zp->z_lock);
531 zp->z_is_mapped = 1;
532 mutex_exit(&zp->z_lock);
533
534 return (error);
535}
536
537/*
538 * Populate a page with data for the Linux page cache. This function is
539 * only used to support mmap(2). There will be an identical copy of the
540 * data in the ARC which is kept up to date via .write() and .writepage().
541 *
542 * Current this function relies on zpl_read_common() and the O_DIRECT
543 * flag to read in a page. This works but the more correct way is to
544 * update zfs_fillpage() to be Linux friendly and use that interface.
545 */
546static int
547zpl_readpage(struct file *filp, struct page *pp)
548{
549 struct inode *ip;
dde471ef 550 struct page *pl[1];
c0d35759 551 int error = 0;
ea04106b 552 fstrans_cookie_t cookie;
c0d35759
BB
553
554 ASSERT(PageLocked(pp));
555 ip = pp->mapping->host;
dde471ef 556 pl[0] = pp;
c0d35759 557
ea04106b 558 cookie = spl_fstrans_mark();
dde471ef 559 error = -zfs_getpage(ip, pl, 1);
ea04106b 560 spl_fstrans_unmark(cookie);
c0d35759 561
dde471ef
PJ
562 if (error) {
563 SetPageError(pp);
564 ClearPageUptodate(pp);
565 } else {
566 ClearPageError(pp);
567 SetPageUptodate(pp);
568 flush_dcache_page(pp);
569 }
c0d35759 570
dde471ef 571 unlock_page(pp);
a08ee875 572 return (error);
dde471ef 573}
c0d35759 574
f3ab88d6
BB
575/*
576 * Populate a set of pages with data for the Linux page cache. This
577 * function will only be called for read ahead and never for demand
578 * paging. For simplicity, the code relies on read_cache_pages() to
579 * correctly lock each page for IO and call zpl_readpage().
580 */
581static int
582zpl_readpages(struct file *filp, struct address_space *mapping,
cae5b340 583 struct list_head *pages, unsigned nr_pages)
f3ab88d6 584{
95d9fd02
BB
585 return (read_cache_pages(mapping, pages,
586 (filler_t *)zpl_readpage, filp));
f3ab88d6
BB
587}
588
dde471ef
PJ
589int
590zpl_putpage(struct page *pp, struct writeback_control *wbc, void *data)
591{
3c0e5c0f 592 struct address_space *mapping = data;
ea04106b 593 fstrans_cookie_t cookie;
3c0e5c0f
BB
594
595 ASSERT(PageLocked(pp));
596 ASSERT(!PageWriteback(pp));
8630650a 597
ea04106b 598 cookie = spl_fstrans_mark();
62c4165a 599 (void) zfs_putpage(mapping->host, pp, wbc);
ea04106b 600 spl_fstrans_unmark(cookie);
c0d35759 601
3c0e5c0f 602 return (0);
dde471ef 603}
c0d35759 604
dde471ef
PJ
605static int
606zpl_writepages(struct address_space *mapping, struct writeback_control *wbc)
607{
a08ee875 608 znode_t *zp = ITOZ(mapping->host);
cae5b340 609 zfsvfs_t *zfsvfs = ITOZSB(mapping->host);
a08ee875
LG
610 enum writeback_sync_modes sync_mode;
611 int result;
612
cae5b340
AX
613 ZFS_ENTER(zfsvfs);
614 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)
a08ee875 615 wbc->sync_mode = WB_SYNC_ALL;
cae5b340 616 ZFS_EXIT(zfsvfs);
a08ee875
LG
617 sync_mode = wbc->sync_mode;
618
619 /*
620 * We don't want to run write_cache_pages() in SYNC mode here, because
621 * that would make putpage() wait for a single page to be committed to
622 * disk every single time, resulting in atrocious performance. Instead
623 * we run it once in non-SYNC mode so that the ZIL gets all the data,
624 * and then we commit it all in one go.
625 */
626 wbc->sync_mode = WB_SYNC_NONE;
627 result = write_cache_pages(mapping, wbc, zpl_putpage, mapping);
628 if (sync_mode != wbc->sync_mode) {
cae5b340 629 ZFS_ENTER(zfsvfs);
a08ee875 630 ZFS_VERIFY_ZP(zp);
cae5b340
AX
631 if (zfsvfs->z_log != NULL)
632 zil_commit(zfsvfs->z_log, zp->z_id);
633 ZFS_EXIT(zfsvfs);
a08ee875
LG
634
635 /*
636 * We need to call write_cache_pages() again (we can't just
637 * return after the commit) because the previous call in
638 * non-SYNC mode does not guarantee that we got all the dirty
639 * pages (see the implementation of write_cache_pages() for
640 * details). That being said, this is a no-op in most cases.
641 */
642 wbc->sync_mode = sync_mode;
643 result = write_cache_pages(mapping, wbc, zpl_putpage, mapping);
644 }
645 return (result);
c0d35759
BB
646}
647
648/*
649 * Write out dirty pages to the ARC, this function is only required to
650 * support mmap(2). Mapped pages may be dirtied by memory operations
651 * which never call .write(). These dirty pages are kept in sync with
652 * the ARC buffers via this hook.
c0d35759
BB
653 */
654static int
655zpl_writepage(struct page *pp, struct writeback_control *wbc)
656{
a08ee875
LG
657 if (ITOZSB(pp->mapping->host)->z_os->os_sync == ZFS_SYNC_ALWAYS)
658 wbc->sync_mode = WB_SYNC_ALL;
659
660 return (zpl_putpage(pp, wbc, pp->mapping));
c0d35759
BB
661}
662
cb2d1901
ED
663/*
664 * The only flag combination which matches the behavior of zfs_space()
ea04106b
AX
665 * is FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE. The FALLOC_FL_PUNCH_HOLE
666 * flag was introduced in the 2.6.38 kernel.
cb2d1901 667 */
ea04106b 668#if defined(HAVE_FILE_FALLOCATE) || defined(HAVE_INODE_FALLOCATE)
cb2d1901
ED
669long
670zpl_fallocate_common(struct inode *ip, int mode, loff_t offset, loff_t len)
671{
cb2d1901
ED
672 int error = -EOPNOTSUPP;
673
ea04106b
AX
674#if defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE)
675 cred_t *cr = CRED();
676 flock64_t bf;
677 loff_t olen;
678 fstrans_cookie_t cookie;
679
680 if (mode != (FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
681 return (error);
cb2d1901 682
ea04106b
AX
683 if (offset < 0 || len <= 0)
684 return (-EINVAL);
cb2d1901 685
ea04106b
AX
686 spl_inode_lock(ip);
687 olen = i_size_read(ip);
cb2d1901 688
ea04106b
AX
689 if (offset > olen) {
690 spl_inode_unlock(ip);
691 return (0);
cb2d1901 692 }
ea04106b
AX
693 if (offset + len > olen)
694 len = olen - offset;
695 bf.l_type = F_WRLCK;
696 bf.l_whence = 0;
697 bf.l_start = offset;
698 bf.l_len = len;
699 bf.l_pid = 0;
700
68d83c55 701 crhold(cr);
ea04106b
AX
702 cookie = spl_fstrans_mark();
703 error = -zfs_space(ip, F_FREESP, &bf, FWRITE, offset, cr);
704 spl_fstrans_unmark(cookie);
705 spl_inode_unlock(ip);
cb2d1901
ED
706
707 crfree(cr);
ea04106b 708#endif /* defined(FALLOC_FL_PUNCH_HOLE) && defined(FALLOC_FL_KEEP_SIZE) */
cb2d1901
ED
709
710 ASSERT3S(error, <=, 0);
711 return (error);
712}
ea04106b 713#endif /* defined(HAVE_FILE_FALLOCATE) || defined(HAVE_INODE_FALLOCATE) */
cb2d1901
ED
714
715#ifdef HAVE_FILE_FALLOCATE
716static long
717zpl_fallocate(struct file *filp, int mode, loff_t offset, loff_t len)
718{
cae5b340 719 return zpl_fallocate_common(file_inode(filp),
cb2d1901
ED
720 mode, offset, len);
721}
722#endif /* HAVE_FILE_FALLOCATE */
723
ea04106b
AX
724/*
725 * Map zfs file z_pflags (xvattr_t) to linux file attributes. Only file
726 * attributes common to both Linux and Solaris are mapped.
727 */
728static int
729zpl_ioctl_getflags(struct file *filp, void __user *arg)
730{
731 struct inode *ip = file_inode(filp);
732 unsigned int ioctl_flags = 0;
733 uint64_t zfs_flags = ITOZ(ip)->z_pflags;
734 int error;
735
736 if (zfs_flags & ZFS_IMMUTABLE)
737 ioctl_flags |= FS_IMMUTABLE_FL;
738
739 if (zfs_flags & ZFS_APPENDONLY)
740 ioctl_flags |= FS_APPEND_FL;
741
742 if (zfs_flags & ZFS_NODUMP)
743 ioctl_flags |= FS_NODUMP_FL;
744
745 ioctl_flags &= FS_FL_USER_VISIBLE;
746
747 error = copy_to_user(arg, &ioctl_flags, sizeof (ioctl_flags));
748
749 return (error);
750}
751
752/*
753 * fchange() is a helper macro to detect if we have been asked to change a
754 * flag. This is ugly, but the requirement that we do this is a consequence of
755 * how the Linux file attribute interface was designed. Another consequence is
756 * that concurrent modification of files suffers from a TOCTOU race. Neither
757 * are things we can fix without modifying the kernel-userland interface, which
758 * is outside of our jurisdiction.
759 */
760
68d83c55 761#define fchange(f0, f1, b0, b1) (!((f0) & (b0)) != !((f1) & (b1)))
ea04106b
AX
762
763static int
764zpl_ioctl_setflags(struct file *filp, void __user *arg)
765{
766 struct inode *ip = file_inode(filp);
767 uint64_t zfs_flags = ITOZ(ip)->z_pflags;
768 unsigned int ioctl_flags;
769 cred_t *cr = CRED();
770 xvattr_t xva;
771 xoptattr_t *xoap;
772 int error;
773 fstrans_cookie_t cookie;
774
775 if (copy_from_user(&ioctl_flags, arg, sizeof (ioctl_flags)))
776 return (-EFAULT);
777
778 if ((ioctl_flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NODUMP_FL)))
779 return (-EOPNOTSUPP);
780
781 if ((ioctl_flags & ~(FS_FL_USER_MODIFIABLE)))
782 return (-EACCES);
783
784 if ((fchange(ioctl_flags, zfs_flags, FS_IMMUTABLE_FL, ZFS_IMMUTABLE) ||
785 fchange(ioctl_flags, zfs_flags, FS_APPEND_FL, ZFS_APPENDONLY)) &&
786 !capable(CAP_LINUX_IMMUTABLE))
787 return (-EACCES);
788
789 if (!zpl_inode_owner_or_capable(ip))
790 return (-EACCES);
791
792 xva_init(&xva);
793 xoap = xva_getxoptattr(&xva);
794
795 XVA_SET_REQ(&xva, XAT_IMMUTABLE);
796 if (ioctl_flags & FS_IMMUTABLE_FL)
797 xoap->xoa_immutable = B_TRUE;
798
799 XVA_SET_REQ(&xva, XAT_APPENDONLY);
800 if (ioctl_flags & FS_APPEND_FL)
801 xoap->xoa_appendonly = B_TRUE;
802
803 XVA_SET_REQ(&xva, XAT_NODUMP);
804 if (ioctl_flags & FS_NODUMP_FL)
805 xoap->xoa_nodump = B_TRUE;
806
807 crhold(cr);
808 cookie = spl_fstrans_mark();
809 error = -zfs_setattr(ip, (vattr_t *)&xva, 0, cr);
810 spl_fstrans_unmark(cookie);
811 crfree(cr);
812
813 return (error);
814}
815
c06d4368
AX
816static long
817zpl_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
818{
819 switch (cmd) {
ea04106b
AX
820 case FS_IOC_GETFLAGS:
821 return (zpl_ioctl_getflags(filp, (void *)arg));
822 case FS_IOC_SETFLAGS:
823 return (zpl_ioctl_setflags(filp, (void *)arg));
c06d4368
AX
824 default:
825 return (-ENOTTY);
826 }
827}
828
829#ifdef CONFIG_COMPAT
830static long
831zpl_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
832{
5eacc075
AX
833 switch (cmd) {
834 case FS_IOC32_GETFLAGS:
835 cmd = FS_IOC_GETFLAGS;
836 break;
837 case FS_IOC32_SETFLAGS:
838 cmd = FS_IOC_SETFLAGS;
839 break;
840 default:
841 return (-ENOTTY);
842 }
843 return (zpl_ioctl(filp, cmd, (unsigned long)compat_ptr(arg)));
c06d4368
AX
844}
845#endif /* CONFIG_COMPAT */
846
847
1efb473f 848const struct address_space_operations zpl_address_space_operations = {
dde471ef 849 .readpages = zpl_readpages,
1efb473f
BB
850 .readpage = zpl_readpage,
851 .writepage = zpl_writepage,
a08ee875 852 .writepages = zpl_writepages,
1efb473f
BB
853};
854
855const struct file_operations zpl_file_operations = {
126400a1
BB
856 .open = zpl_open,
857 .release = zpl_release,
c06d4368 858 .llseek = zpl_llseek,
ea04106b 859#ifdef HAVE_VFS_RW_ITERATE
22929307
AX
860#ifdef HAVE_NEW_SYNC_READ
861 .read = new_sync_read,
862 .write = new_sync_write,
863#endif
ea04106b
AX
864 .read_iter = zpl_iter_read,
865 .write_iter = zpl_iter_write,
866#else
22929307
AX
867 .read = do_sync_read,
868 .write = do_sync_write,
ea04106b
AX
869 .aio_read = zpl_aio_read,
870 .aio_write = zpl_aio_write,
871#endif
c0d35759 872 .mmap = zpl_mmap,
1efb473f 873 .fsync = zpl_fsync,
68d83c55 874#ifdef HAVE_FILE_AIO_FSYNC
ea04106b 875 .aio_fsync = zpl_aio_fsync,
68d83c55 876#endif
cb2d1901 877#ifdef HAVE_FILE_FALLOCATE
a08ee875 878 .fallocate = zpl_fallocate,
cb2d1901 879#endif /* HAVE_FILE_FALLOCATE */
a08ee875 880 .unlocked_ioctl = zpl_ioctl,
c06d4368 881#ifdef CONFIG_COMPAT
a08ee875 882 .compat_ioctl = zpl_compat_ioctl,
c06d4368 883#endif
1efb473f
BB
884};
885
886const struct file_operations zpl_dir_file_operations = {
887 .llseek = generic_file_llseek,
888 .read = generic_read_dir,
42f7b73b 889#if defined(HAVE_VFS_ITERATE_SHARED)
87dac73d
AX
890 .iterate_shared = zpl_iterate,
891#elif defined(HAVE_VFS_ITERATE)
c06d4368
AX
892 .iterate = zpl_iterate,
893#else
1efb473f 894 .readdir = zpl_readdir,
c06d4368 895#endif
1efb473f 896 .fsync = zpl_fsync,
c06d4368
AX
897 .unlocked_ioctl = zpl_ioctl,
898#ifdef CONFIG_COMPAT
899 .compat_ioctl = zpl_compat_ioctl,
900#endif
1efb473f 901};