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