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