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