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