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