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