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