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