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