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 /* 23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 24 * Copyright (c) 2012, 2018 by Delphix. All rights reserved. 25 * Copyright (c) 2015 by Chunwei Chen. All rights reserved. 26 * Copyright 2017 Nexenta Systems, Inc. 27 * Copyright (c) 2021, 2022 by Pawel Jakub Dawidek 28 */ 29 30 /* Portions Copyright 2007 Jeremy Teo */ 31 /* Portions Copyright 2010 Robert Milkowski */ 32 33 #include <sys/types.h> 34 #include <sys/param.h> 35 #include <sys/time.h> 36 #include <sys/sysmacros.h> 37 #include <sys/vfs.h> 38 #include <sys/uio_impl.h> 39 #include <sys/file.h> 40 #include <sys/stat.h> 41 #include <sys/kmem.h> 42 #include <sys/cmn_err.h> 43 #include <sys/errno.h> 44 #include <sys/zfs_dir.h> 45 #include <sys/zfs_acl.h> 46 #include <sys/zfs_ioctl.h> 47 #include <sys/fs/zfs.h> 48 #include <sys/dmu.h> 49 #include <sys/dmu_objset.h> 50 #include <sys/spa.h> 51 #include <sys/txg.h> 52 #include <sys/dbuf.h> 53 #include <sys/policy.h> 54 #include <sys/zfeature.h> 55 #include <sys/zfs_vnops.h> 56 #include <sys/zfs_quota.h> 57 #include <sys/zfs_vfsops.h> 58 #include <sys/zfs_znode.h> 59 60 61 int 62 zfs_fsync(znode_t *zp, int syncflag, cred_t *cr) 63 { 64 int error = 0; 65 zfsvfs_t *zfsvfs = ZTOZSB(zp); 66 67 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 68 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 69 return (error); 70 atomic_inc_32(&zp->z_sync_writes_cnt); 71 zil_commit(zfsvfs->z_log, zp->z_id); 72 atomic_dec_32(&zp->z_sync_writes_cnt); 73 zfs_exit(zfsvfs, FTAG); 74 } 75 return (error); 76 } 77 78 79 #if defined(SEEK_HOLE) && defined(SEEK_DATA) 80 /* 81 * Lseek support for finding holes (cmd == SEEK_HOLE) and 82 * data (cmd == SEEK_DATA). "off" is an in/out parameter. 83 */ 84 static int 85 zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off) 86 { 87 zfs_locked_range_t *lr; 88 uint64_t noff = (uint64_t)*off; /* new offset */ 89 uint64_t file_sz; 90 int error; 91 boolean_t hole; 92 93 file_sz = zp->z_size; 94 if (noff >= file_sz) { 95 return (SET_ERROR(ENXIO)); 96 } 97 98 if (cmd == F_SEEK_HOLE) 99 hole = B_TRUE; 100 else 101 hole = B_FALSE; 102 103 /* Flush any mmap()'d data to disk */ 104 if (zn_has_cached_data(zp, 0, file_sz - 1)) 105 zn_flush_cached_data(zp, B_FALSE); 106 107 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, UINT64_MAX, RL_READER); 108 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff); 109 zfs_rangelock_exit(lr); 110 111 if (error == ESRCH) 112 return (SET_ERROR(ENXIO)); 113 114 /* File was dirty, so fall back to using generic logic */ 115 if (error == EBUSY) { 116 if (hole) 117 *off = file_sz; 118 119 return (0); 120 } 121 122 /* 123 * We could find a hole that begins after the logical end-of-file, 124 * because dmu_offset_next() only works on whole blocks. If the 125 * EOF falls mid-block, then indicate that the "virtual hole" 126 * at the end of the file begins at the logical EOF, rather than 127 * at the end of the last block. 128 */ 129 if (noff > file_sz) { 130 ASSERT(hole); 131 noff = file_sz; 132 } 133 134 if (noff < *off) 135 return (error); 136 *off = noff; 137 return (error); 138 } 139 140 int 141 zfs_holey(znode_t *zp, ulong_t cmd, loff_t *off) 142 { 143 zfsvfs_t *zfsvfs = ZTOZSB(zp); 144 int error; 145 146 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 147 return (error); 148 149 error = zfs_holey_common(zp, cmd, off); 150 151 zfs_exit(zfsvfs, FTAG); 152 return (error); 153 } 154 #endif /* SEEK_HOLE && SEEK_DATA */ 155 156 int 157 zfs_access(znode_t *zp, int mode, int flag, cred_t *cr) 158 { 159 zfsvfs_t *zfsvfs = ZTOZSB(zp); 160 int error; 161 162 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 163 return (error); 164 165 if (flag & V_ACE_MASK) 166 #if defined(__linux__) 167 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr, 168 zfs_init_idmap); 169 #else 170 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr, 171 NULL); 172 #endif 173 else 174 #if defined(__linux__) 175 error = zfs_zaccess_rwx(zp, mode, flag, cr, zfs_init_idmap); 176 #else 177 error = zfs_zaccess_rwx(zp, mode, flag, cr, NULL); 178 #endif 179 180 zfs_exit(zfsvfs, FTAG); 181 return (error); 182 } 183 184 static uint64_t zfs_vnops_read_chunk_size = 1024 * 1024; /* Tunable */ 185 186 /* 187 * Read bytes from specified file into supplied buffer. 188 * 189 * IN: zp - inode of file to be read from. 190 * uio - structure supplying read location, range info, 191 * and return buffer. 192 * ioflag - O_SYNC flags; used to provide FRSYNC semantics. 193 * O_DIRECT flag; used to bypass page cache. 194 * cr - credentials of caller. 195 * 196 * OUT: uio - updated offset and range, buffer filled. 197 * 198 * RETURN: 0 on success, error code on failure. 199 * 200 * Side Effects: 201 * inode - atime updated if byte count > 0 202 */ 203 int 204 zfs_read(struct znode *zp, zfs_uio_t *uio, int ioflag, cred_t *cr) 205 { 206 (void) cr; 207 int error = 0; 208 boolean_t frsync = B_FALSE; 209 210 zfsvfs_t *zfsvfs = ZTOZSB(zp); 211 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 212 return (error); 213 214 if (zp->z_pflags & ZFS_AV_QUARANTINED) { 215 zfs_exit(zfsvfs, FTAG); 216 return (SET_ERROR(EACCES)); 217 } 218 219 /* We don't copy out anything useful for directories. */ 220 if (Z_ISDIR(ZTOTYPE(zp))) { 221 zfs_exit(zfsvfs, FTAG); 222 return (SET_ERROR(EISDIR)); 223 } 224 225 /* 226 * Validate file offset 227 */ 228 if (zfs_uio_offset(uio) < (offset_t)0) { 229 zfs_exit(zfsvfs, FTAG); 230 return (SET_ERROR(EINVAL)); 231 } 232 233 /* 234 * Fasttrack empty reads 235 */ 236 if (zfs_uio_resid(uio) == 0) { 237 zfs_exit(zfsvfs, FTAG); 238 return (0); 239 } 240 241 #ifdef FRSYNC 242 /* 243 * If we're in FRSYNC mode, sync out this znode before reading it. 244 * Only do this for non-snapshots. 245 * 246 * Some platforms do not support FRSYNC and instead map it 247 * to O_SYNC, which results in unnecessary calls to zil_commit. We 248 * only honor FRSYNC requests on platforms which support it. 249 */ 250 frsync = !!(ioflag & FRSYNC); 251 #endif 252 if (zfsvfs->z_log && 253 (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)) 254 zil_commit(zfsvfs->z_log, zp->z_id); 255 256 /* 257 * Lock the range against changes. 258 */ 259 zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock, 260 zfs_uio_offset(uio), zfs_uio_resid(uio), RL_READER); 261 262 /* 263 * If we are reading past end-of-file we can skip 264 * to the end; but we might still need to set atime. 265 */ 266 if (zfs_uio_offset(uio) >= zp->z_size) { 267 error = 0; 268 goto out; 269 } 270 271 ASSERT(zfs_uio_offset(uio) < zp->z_size); 272 #if defined(__linux__) 273 ssize_t start_offset = zfs_uio_offset(uio); 274 #endif 275 ssize_t n = MIN(zfs_uio_resid(uio), zp->z_size - zfs_uio_offset(uio)); 276 ssize_t start_resid = n; 277 278 while (n > 0) { 279 ssize_t nbytes = MIN(n, zfs_vnops_read_chunk_size - 280 P2PHASE(zfs_uio_offset(uio), zfs_vnops_read_chunk_size)); 281 #ifdef UIO_NOCOPY 282 if (zfs_uio_segflg(uio) == UIO_NOCOPY) 283 error = mappedread_sf(zp, nbytes, uio); 284 else 285 #endif 286 if (zn_has_cached_data(zp, zfs_uio_offset(uio), 287 zfs_uio_offset(uio) + nbytes - 1) && !(ioflag & O_DIRECT)) { 288 error = mappedread(zp, nbytes, uio); 289 } else { 290 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 291 uio, nbytes); 292 } 293 294 if (error) { 295 /* convert checksum errors into IO errors */ 296 if (error == ECKSUM) 297 error = SET_ERROR(EIO); 298 299 #if defined(__linux__) 300 /* 301 * if we actually read some bytes, bubbling EFAULT 302 * up to become EAGAIN isn't what we want here... 303 * 304 * ...on Linux, at least. On FBSD, doing this breaks. 305 */ 306 if (error == EFAULT && 307 (zfs_uio_offset(uio) - start_offset) != 0) 308 error = 0; 309 #endif 310 break; 311 } 312 313 n -= nbytes; 314 } 315 316 int64_t nread = start_resid - n; 317 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread); 318 task_io_account_read(nread); 319 out: 320 zfs_rangelock_exit(lr); 321 322 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 323 zfs_exit(zfsvfs, FTAG); 324 return (error); 325 } 326 327 static void 328 zfs_clear_setid_bits_if_necessary(zfsvfs_t *zfsvfs, znode_t *zp, cred_t *cr, 329 uint64_t *clear_setid_bits_txgp, dmu_tx_t *tx) 330 { 331 zilog_t *zilog = zfsvfs->z_log; 332 const uint64_t uid = KUID_TO_SUID(ZTOUID(zp)); 333 334 ASSERT(clear_setid_bits_txgp != NULL); 335 ASSERT(tx != NULL); 336 337 /* 338 * Clear Set-UID/Set-GID bits on successful write if not 339 * privileged and at least one of the execute bits is set. 340 * 341 * It would be nice to do this after all writes have 342 * been done, but that would still expose the ISUID/ISGID 343 * to another app after the partial write is committed. 344 * 345 * Note: we don't call zfs_fuid_map_id() here because 346 * user 0 is not an ephemeral uid. 347 */ 348 mutex_enter(&zp->z_acl_lock); 349 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | (S_IXUSR >> 6))) != 0 && 350 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 351 secpolicy_vnode_setid_retain(zp, cr, 352 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) { 353 uint64_t newmode; 354 355 zp->z_mode &= ~(S_ISUID | S_ISGID); 356 newmode = zp->z_mode; 357 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 358 (void *)&newmode, sizeof (uint64_t), tx); 359 360 mutex_exit(&zp->z_acl_lock); 361 362 /* 363 * Make sure SUID/SGID bits will be removed when we replay the 364 * log. If the setid bits are keep coming back, don't log more 365 * than one TX_SETATTR per transaction group. 366 */ 367 if (*clear_setid_bits_txgp != dmu_tx_get_txg(tx)) { 368 vattr_t va = {0}; 369 370 va.va_mask = ATTR_MODE; 371 va.va_nodeid = zp->z_id; 372 va.va_mode = newmode; 373 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, &va, 374 ATTR_MODE, NULL); 375 *clear_setid_bits_txgp = dmu_tx_get_txg(tx); 376 } 377 } else { 378 mutex_exit(&zp->z_acl_lock); 379 } 380 } 381 382 /* 383 * Write the bytes to a file. 384 * 385 * IN: zp - znode of file to be written to. 386 * uio - structure supplying write location, range info, 387 * and data buffer. 388 * ioflag - O_APPEND flag set if in append mode. 389 * O_DIRECT flag; used to bypass page cache. 390 * cr - credentials of caller. 391 * 392 * OUT: uio - updated offset and range. 393 * 394 * RETURN: 0 if success 395 * error code if failure 396 * 397 * Timestamps: 398 * ip - ctime|mtime updated if byte count > 0 399 */ 400 int 401 zfs_write(znode_t *zp, zfs_uio_t *uio, int ioflag, cred_t *cr) 402 { 403 int error = 0, error1; 404 ssize_t start_resid = zfs_uio_resid(uio); 405 uint64_t clear_setid_bits_txg = 0; 406 407 /* 408 * Fasttrack empty write 409 */ 410 ssize_t n = start_resid; 411 if (n == 0) 412 return (0); 413 414 zfsvfs_t *zfsvfs = ZTOZSB(zp); 415 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 416 return (error); 417 418 sa_bulk_attr_t bulk[4]; 419 int count = 0; 420 uint64_t mtime[2], ctime[2]; 421 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 422 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 423 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 424 &zp->z_size, 8); 425 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 426 &zp->z_pflags, 8); 427 428 /* 429 * Callers might not be able to detect properly that we are read-only, 430 * so check it explicitly here. 431 */ 432 if (zfs_is_readonly(zfsvfs)) { 433 zfs_exit(zfsvfs, FTAG); 434 return (SET_ERROR(EROFS)); 435 } 436 437 /* 438 * If immutable or not appending then return EPERM. 439 * Intentionally allow ZFS_READONLY through here. 440 * See zfs_zaccess_common() 441 */ 442 if ((zp->z_pflags & ZFS_IMMUTABLE) || 443 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & O_APPEND) && 444 (zfs_uio_offset(uio) < zp->z_size))) { 445 zfs_exit(zfsvfs, FTAG); 446 return (SET_ERROR(EPERM)); 447 } 448 449 /* 450 * Validate file offset 451 */ 452 offset_t woff = ioflag & O_APPEND ? zp->z_size : zfs_uio_offset(uio); 453 if (woff < 0) { 454 zfs_exit(zfsvfs, FTAG); 455 return (SET_ERROR(EINVAL)); 456 } 457 458 /* 459 * Pre-fault the pages to ensure slow (eg NFS) pages 460 * don't hold up txg. 461 */ 462 ssize_t pfbytes = MIN(n, DMU_MAX_ACCESS >> 1); 463 if (zfs_uio_prefaultpages(pfbytes, uio)) { 464 zfs_exit(zfsvfs, FTAG); 465 return (SET_ERROR(EFAULT)); 466 } 467 468 /* 469 * If in append mode, set the io offset pointer to eof. 470 */ 471 zfs_locked_range_t *lr; 472 if (ioflag & O_APPEND) { 473 /* 474 * Obtain an appending range lock to guarantee file append 475 * semantics. We reset the write offset once we have the lock. 476 */ 477 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND); 478 woff = lr->lr_offset; 479 if (lr->lr_length == UINT64_MAX) { 480 /* 481 * We overlocked the file because this write will cause 482 * the file block size to increase. 483 * Note that zp_size cannot change with this lock held. 484 */ 485 woff = zp->z_size; 486 } 487 zfs_uio_setoffset(uio, woff); 488 } else { 489 /* 490 * Note that if the file block size will change as a result of 491 * this write, then this range lock will lock the entire file 492 * so that we can re-write the block safely. 493 */ 494 lr = zfs_rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER); 495 } 496 497 if (zn_rlimit_fsize_uio(zp, uio)) { 498 zfs_rangelock_exit(lr); 499 zfs_exit(zfsvfs, FTAG); 500 return (SET_ERROR(EFBIG)); 501 } 502 503 const rlim64_t limit = MAXOFFSET_T; 504 505 if (woff >= limit) { 506 zfs_rangelock_exit(lr); 507 zfs_exit(zfsvfs, FTAG); 508 return (SET_ERROR(EFBIG)); 509 } 510 511 if (n > limit - woff) 512 n = limit - woff; 513 514 uint64_t end_size = MAX(zp->z_size, woff + n); 515 zilog_t *zilog = zfsvfs->z_log; 516 boolean_t commit = (ioflag & (O_SYNC | O_DSYNC)) || 517 (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS); 518 519 const uint64_t uid = KUID_TO_SUID(ZTOUID(zp)); 520 const uint64_t gid = KGID_TO_SGID(ZTOGID(zp)); 521 const uint64_t projid = zp->z_projid; 522 523 /* 524 * Write the file in reasonable size chunks. Each chunk is written 525 * in a separate transaction; this keeps the intent log records small 526 * and allows us to do more fine-grained space accounting. 527 */ 528 while (n > 0) { 529 woff = zfs_uio_offset(uio); 530 531 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, uid) || 532 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, gid) || 533 (projid != ZFS_DEFAULT_PROJID && 534 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT, 535 projid))) { 536 error = SET_ERROR(EDQUOT); 537 break; 538 } 539 540 uint64_t blksz; 541 if (lr->lr_length == UINT64_MAX && zp->z_size <= zp->z_blksz) { 542 if (zp->z_blksz > zfsvfs->z_max_blksz && 543 !ISP2(zp->z_blksz)) { 544 /* 545 * File's blocksize is already larger than the 546 * "recordsize" property. Only let it grow to 547 * the next power of 2. 548 */ 549 blksz = 1 << highbit64(zp->z_blksz); 550 } else { 551 blksz = zfsvfs->z_max_blksz; 552 } 553 blksz = MIN(blksz, P2ROUNDUP(end_size, 554 SPA_MINBLOCKSIZE)); 555 blksz = MAX(blksz, zp->z_blksz); 556 } else { 557 blksz = zp->z_blksz; 558 } 559 560 arc_buf_t *abuf = NULL; 561 ssize_t nbytes = n; 562 if (n >= blksz && woff >= zp->z_size && 563 P2PHASE(woff, blksz) == 0 && 564 (blksz >= SPA_OLD_MAXBLOCKSIZE || n < 4 * blksz)) { 565 /* 566 * This write covers a full block. "Borrow" a buffer 567 * from the dmu so that we can fill it before we enter 568 * a transaction. This avoids the possibility of 569 * holding up the transaction if the data copy hangs 570 * up on a pagefault (e.g., from an NFS server mapping). 571 */ 572 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 573 blksz); 574 ASSERT(abuf != NULL); 575 ASSERT(arc_buf_size(abuf) == blksz); 576 if ((error = zfs_uiocopy(abuf->b_data, blksz, 577 UIO_WRITE, uio, &nbytes))) { 578 dmu_return_arcbuf(abuf); 579 break; 580 } 581 ASSERT3S(nbytes, ==, blksz); 582 } else { 583 nbytes = MIN(n, (DMU_MAX_ACCESS >> 1) - 584 P2PHASE(woff, blksz)); 585 if (pfbytes < nbytes) { 586 if (zfs_uio_prefaultpages(nbytes, uio)) { 587 error = SET_ERROR(EFAULT); 588 break; 589 } 590 pfbytes = nbytes; 591 } 592 } 593 594 /* 595 * Start a transaction. 596 */ 597 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 598 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 599 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl); 600 DB_DNODE_ENTER(db); 601 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff, nbytes); 602 DB_DNODE_EXIT(db); 603 zfs_sa_upgrade_txholds(tx, zp); 604 error = dmu_tx_assign(tx, TXG_WAIT); 605 if (error) { 606 dmu_tx_abort(tx); 607 if (abuf != NULL) 608 dmu_return_arcbuf(abuf); 609 break; 610 } 611 612 /* 613 * NB: We must call zfs_clear_setid_bits_if_necessary before 614 * committing the transaction! 615 */ 616 617 /* 618 * If rangelock_enter() over-locked we grow the blocksize 619 * and then reduce the lock range. This will only happen 620 * on the first iteration since rangelock_reduce() will 621 * shrink down lr_length to the appropriate size. 622 */ 623 if (lr->lr_length == UINT64_MAX) { 624 zfs_grow_blocksize(zp, blksz, tx); 625 zfs_rangelock_reduce(lr, woff, n); 626 } 627 628 ssize_t tx_bytes; 629 if (abuf == NULL) { 630 tx_bytes = zfs_uio_resid(uio); 631 zfs_uio_fault_disable(uio, B_TRUE); 632 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 633 uio, nbytes, tx); 634 zfs_uio_fault_disable(uio, B_FALSE); 635 #ifdef __linux__ 636 if (error == EFAULT) { 637 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, 638 cr, &clear_setid_bits_txg, tx); 639 dmu_tx_commit(tx); 640 /* 641 * Account for partial writes before 642 * continuing the loop. 643 * Update needs to occur before the next 644 * zfs_uio_prefaultpages, or prefaultpages may 645 * error, and we may break the loop early. 646 */ 647 n -= tx_bytes - zfs_uio_resid(uio); 648 pfbytes -= tx_bytes - zfs_uio_resid(uio); 649 continue; 650 } 651 #endif 652 /* 653 * On FreeBSD, EFAULT should be propagated back to the 654 * VFS, which will handle faulting and will retry. 655 */ 656 if (error != 0 && error != EFAULT) { 657 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, 658 cr, &clear_setid_bits_txg, tx); 659 dmu_tx_commit(tx); 660 break; 661 } 662 tx_bytes -= zfs_uio_resid(uio); 663 } else { 664 /* 665 * Thus, we're writing a full block at a block-aligned 666 * offset and extending the file past EOF. 667 * 668 * dmu_assign_arcbuf_by_dbuf() will directly assign the 669 * arc buffer to a dbuf. 670 */ 671 error = dmu_assign_arcbuf_by_dbuf( 672 sa_get_db(zp->z_sa_hdl), woff, abuf, tx); 673 if (error != 0) { 674 /* 675 * XXX This might not be necessary if 676 * dmu_assign_arcbuf_by_dbuf is guaranteed 677 * to be atomic. 678 */ 679 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, 680 cr, &clear_setid_bits_txg, tx); 681 dmu_return_arcbuf(abuf); 682 dmu_tx_commit(tx); 683 break; 684 } 685 ASSERT3S(nbytes, <=, zfs_uio_resid(uio)); 686 zfs_uioskip(uio, nbytes); 687 tx_bytes = nbytes; 688 } 689 if (tx_bytes && 690 zn_has_cached_data(zp, woff, woff + tx_bytes - 1) && 691 !(ioflag & O_DIRECT)) { 692 update_pages(zp, woff, tx_bytes, zfsvfs->z_os); 693 } 694 695 /* 696 * If we made no progress, we're done. If we made even 697 * partial progress, update the znode and ZIL accordingly. 698 */ 699 if (tx_bytes == 0) { 700 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 701 (void *)&zp->z_size, sizeof (uint64_t), tx); 702 dmu_tx_commit(tx); 703 ASSERT(error != 0); 704 break; 705 } 706 707 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, cr, 708 &clear_setid_bits_txg, tx); 709 710 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime); 711 712 /* 713 * Update the file size (zp_size) if it has changed; 714 * account for possible concurrent updates. 715 */ 716 while ((end_size = zp->z_size) < zfs_uio_offset(uio)) { 717 (void) atomic_cas_64(&zp->z_size, end_size, 718 zfs_uio_offset(uio)); 719 ASSERT(error == 0 || error == EFAULT); 720 } 721 /* 722 * If we are replaying and eof is non zero then force 723 * the file size to the specified eof. Note, there's no 724 * concurrency during replay. 725 */ 726 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 727 zp->z_size = zfsvfs->z_replay_eof; 728 729 error1 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 730 if (error1 != 0) 731 /* Avoid clobbering EFAULT. */ 732 error = error1; 733 734 /* 735 * NB: During replay, the TX_SETATTR record logged by 736 * zfs_clear_setid_bits_if_necessary must precede any of 737 * the TX_WRITE records logged here. 738 */ 739 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, commit, 740 NULL, NULL); 741 742 dmu_tx_commit(tx); 743 744 if (error != 0) 745 break; 746 ASSERT3S(tx_bytes, ==, nbytes); 747 n -= nbytes; 748 pfbytes -= nbytes; 749 } 750 751 zfs_znode_update_vfs(zp); 752 zfs_rangelock_exit(lr); 753 754 /* 755 * If we're in replay mode, or we made no progress, or the 756 * uio data is inaccessible return an error. Otherwise, it's 757 * at least a partial write, so it's successful. 758 */ 759 if (zfsvfs->z_replay || zfs_uio_resid(uio) == start_resid || 760 error == EFAULT) { 761 zfs_exit(zfsvfs, FTAG); 762 return (error); 763 } 764 765 if (commit) 766 zil_commit(zilog, zp->z_id); 767 768 const int64_t nwritten = start_resid - zfs_uio_resid(uio); 769 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten); 770 task_io_account_write(nwritten); 771 772 zfs_exit(zfsvfs, FTAG); 773 return (0); 774 } 775 776 int 777 zfs_getsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr) 778 { 779 zfsvfs_t *zfsvfs = ZTOZSB(zp); 780 int error; 781 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 782 783 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 784 return (error); 785 error = zfs_getacl(zp, vsecp, skipaclchk, cr); 786 zfs_exit(zfsvfs, FTAG); 787 788 return (error); 789 } 790 791 int 792 zfs_setsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr) 793 { 794 zfsvfs_t *zfsvfs = ZTOZSB(zp); 795 int error; 796 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 797 zilog_t *zilog = zfsvfs->z_log; 798 799 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 800 return (error); 801 802 error = zfs_setacl(zp, vsecp, skipaclchk, cr); 803 804 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 805 zil_commit(zilog, 0); 806 807 zfs_exit(zfsvfs, FTAG); 808 return (error); 809 } 810 811 #ifdef ZFS_DEBUG 812 static int zil_fault_io = 0; 813 #endif 814 815 static void zfs_get_done(zgd_t *zgd, int error); 816 817 /* 818 * Get data to generate a TX_WRITE intent log record. 819 */ 820 int 821 zfs_get_data(void *arg, uint64_t gen, lr_write_t *lr, char *buf, 822 struct lwb *lwb, zio_t *zio) 823 { 824 zfsvfs_t *zfsvfs = arg; 825 objset_t *os = zfsvfs->z_os; 826 znode_t *zp; 827 uint64_t object = lr->lr_foid; 828 uint64_t offset = lr->lr_offset; 829 uint64_t size = lr->lr_length; 830 dmu_buf_t *db; 831 zgd_t *zgd; 832 int error = 0; 833 uint64_t zp_gen; 834 835 ASSERT3P(lwb, !=, NULL); 836 ASSERT3U(size, !=, 0); 837 838 /* 839 * Nothing to do if the file has been removed 840 */ 841 if (zfs_zget(zfsvfs, object, &zp) != 0) 842 return (SET_ERROR(ENOENT)); 843 if (zp->z_unlinked) { 844 /* 845 * Release the vnode asynchronously as we currently have the 846 * txg stopped from syncing. 847 */ 848 zfs_zrele_async(zp); 849 return (SET_ERROR(ENOENT)); 850 } 851 /* check if generation number matches */ 852 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen, 853 sizeof (zp_gen)) != 0) { 854 zfs_zrele_async(zp); 855 return (SET_ERROR(EIO)); 856 } 857 if (zp_gen != gen) { 858 zfs_zrele_async(zp); 859 return (SET_ERROR(ENOENT)); 860 } 861 862 zgd = kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 863 zgd->zgd_lwb = lwb; 864 zgd->zgd_private = zp; 865 866 /* 867 * Write records come in two flavors: immediate and indirect. 868 * For small writes it's cheaper to store the data with the 869 * log record (immediate); for large writes it's cheaper to 870 * sync the data and get a pointer to it (indirect) so that 871 * we don't have to write the data twice. 872 */ 873 if (buf != NULL) { /* immediate write */ 874 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock, 875 offset, size, RL_READER); 876 /* test for truncation needs to be done while range locked */ 877 if (offset >= zp->z_size) { 878 error = SET_ERROR(ENOENT); 879 } else { 880 error = dmu_read(os, object, offset, size, buf, 881 DMU_READ_NO_PREFETCH); 882 } 883 ASSERT(error == 0 || error == ENOENT); 884 } else { /* indirect write */ 885 ASSERT3P(zio, !=, NULL); 886 /* 887 * Have to lock the whole block to ensure when it's 888 * written out and its checksum is being calculated 889 * that no one can change the data. We need to re-check 890 * blocksize after we get the lock in case it's changed! 891 */ 892 for (;;) { 893 uint64_t blkoff; 894 size = zp->z_blksz; 895 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 896 offset -= blkoff; 897 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock, 898 offset, size, RL_READER); 899 if (zp->z_blksz == size) 900 break; 901 offset += blkoff; 902 zfs_rangelock_exit(zgd->zgd_lr); 903 } 904 /* test for truncation needs to be done while range locked */ 905 if (lr->lr_offset >= zp->z_size) 906 error = SET_ERROR(ENOENT); 907 #ifdef ZFS_DEBUG 908 if (zil_fault_io) { 909 error = SET_ERROR(EIO); 910 zil_fault_io = 0; 911 } 912 #endif 913 if (error == 0) 914 error = dmu_buf_hold_noread(os, object, offset, zgd, 915 &db); 916 917 if (error == 0) { 918 blkptr_t *bp = &lr->lr_blkptr; 919 920 zgd->zgd_db = db; 921 zgd->zgd_bp = bp; 922 923 ASSERT(db->db_offset == offset); 924 ASSERT(db->db_size == size); 925 926 error = dmu_sync(zio, lr->lr_common.lrc_txg, 927 zfs_get_done, zgd); 928 ASSERT(error || lr->lr_length <= size); 929 930 /* 931 * On success, we need to wait for the write I/O 932 * initiated by dmu_sync() to complete before we can 933 * release this dbuf. We will finish everything up 934 * in the zfs_get_done() callback. 935 */ 936 if (error == 0) 937 return (0); 938 939 if (error == EALREADY) { 940 lr->lr_common.lrc_txtype = TX_WRITE2; 941 /* 942 * TX_WRITE2 relies on the data previously 943 * written by the TX_WRITE that caused 944 * EALREADY. We zero out the BP because 945 * it is the old, currently-on-disk BP. 946 */ 947 zgd->zgd_bp = NULL; 948 BP_ZERO(bp); 949 error = 0; 950 } 951 } 952 } 953 954 zfs_get_done(zgd, error); 955 956 return (error); 957 } 958 959 960 static void 961 zfs_get_done(zgd_t *zgd, int error) 962 { 963 (void) error; 964 znode_t *zp = zgd->zgd_private; 965 966 if (zgd->zgd_db) 967 dmu_buf_rele(zgd->zgd_db, zgd); 968 969 zfs_rangelock_exit(zgd->zgd_lr); 970 971 /* 972 * Release the vnode asynchronously as we currently have the 973 * txg stopped from syncing. 974 */ 975 zfs_zrele_async(zp); 976 977 kmem_free(zgd, sizeof (zgd_t)); 978 } 979 980 static int 981 zfs_enter_two(zfsvfs_t *zfsvfs1, zfsvfs_t *zfsvfs2, const char *tag) 982 { 983 int error; 984 985 /* Swap. Not sure if the order of zfs_enter()s is important. */ 986 if (zfsvfs1 > zfsvfs2) { 987 zfsvfs_t *tmpzfsvfs; 988 989 tmpzfsvfs = zfsvfs2; 990 zfsvfs2 = zfsvfs1; 991 zfsvfs1 = tmpzfsvfs; 992 } 993 994 error = zfs_enter(zfsvfs1, tag); 995 if (error != 0) 996 return (error); 997 if (zfsvfs1 != zfsvfs2) { 998 error = zfs_enter(zfsvfs2, tag); 999 if (error != 0) { 1000 zfs_exit(zfsvfs1, tag); 1001 return (error); 1002 } 1003 } 1004 1005 return (0); 1006 } 1007 1008 static void 1009 zfs_exit_two(zfsvfs_t *zfsvfs1, zfsvfs_t *zfsvfs2, const char *tag) 1010 { 1011 1012 zfs_exit(zfsvfs1, tag); 1013 if (zfsvfs1 != zfsvfs2) 1014 zfs_exit(zfsvfs2, tag); 1015 } 1016 1017 /* 1018 * We split each clone request in chunks that can fit into a single ZIL 1019 * log entry. Each ZIL log entry can fit 130816 bytes for a block cloning 1020 * operation (see zil_max_log_data() and zfs_log_clone_range()). This gives 1021 * us room for storing 1022 block pointers. 1022 * 1023 * On success, the function return the number of bytes copied in *lenp. 1024 * Note, it doesn't return how much bytes are left to be copied. 1025 * On errors which are caused by any file system limitations or 1026 * brt limitations `EINVAL` is returned. In the most cases a user 1027 * requested bad parameters, it could be possible to clone the file but 1028 * some parameters don't match the requirements. 1029 */ 1030 int 1031 zfs_clone_range(znode_t *inzp, uint64_t *inoffp, znode_t *outzp, 1032 uint64_t *outoffp, uint64_t *lenp, cred_t *cr) 1033 { 1034 zfsvfs_t *inzfsvfs, *outzfsvfs; 1035 objset_t *inos, *outos; 1036 zfs_locked_range_t *inlr, *outlr; 1037 dmu_buf_impl_t *db; 1038 dmu_tx_t *tx; 1039 zilog_t *zilog; 1040 uint64_t inoff, outoff, len, done; 1041 uint64_t outsize, size; 1042 int error; 1043 int count = 0; 1044 sa_bulk_attr_t bulk[3]; 1045 uint64_t mtime[2], ctime[2]; 1046 uint64_t uid, gid, projid; 1047 blkptr_t *bps; 1048 size_t maxblocks, nbps; 1049 uint_t inblksz; 1050 uint64_t clear_setid_bits_txg = 0; 1051 1052 inoff = *inoffp; 1053 outoff = *outoffp; 1054 len = *lenp; 1055 done = 0; 1056 1057 inzfsvfs = ZTOZSB(inzp); 1058 outzfsvfs = ZTOZSB(outzp); 1059 1060 /* 1061 * We need to call zfs_enter() potentially on two different datasets, 1062 * so we need a dedicated function for that. 1063 */ 1064 error = zfs_enter_two(inzfsvfs, outzfsvfs, FTAG); 1065 if (error != 0) 1066 return (error); 1067 1068 inos = inzfsvfs->z_os; 1069 outos = outzfsvfs->z_os; 1070 1071 /* 1072 * Both source and destination have to belong to the same storage pool. 1073 */ 1074 if (dmu_objset_spa(inos) != dmu_objset_spa(outos)) { 1075 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1076 return (SET_ERROR(EXDEV)); 1077 } 1078 1079 /* 1080 * outos and inos belongs to the same storage pool. 1081 * see a few lines above, only one check. 1082 */ 1083 if (!spa_feature_is_enabled(dmu_objset_spa(outos), 1084 SPA_FEATURE_BLOCK_CLONING)) { 1085 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1086 return (SET_ERROR(EOPNOTSUPP)); 1087 } 1088 1089 ASSERT(!outzfsvfs->z_replay); 1090 1091 /* 1092 * Block cloning from an unencrypted dataset into an encrypted 1093 * dataset and vice versa is not supported. 1094 */ 1095 if (inos->os_encrypted != outos->os_encrypted) { 1096 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1097 return (SET_ERROR(EXDEV)); 1098 } 1099 1100 error = zfs_verify_zp(inzp); 1101 if (error == 0) 1102 error = zfs_verify_zp(outzp); 1103 if (error != 0) { 1104 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1105 return (error); 1106 } 1107 1108 /* 1109 * We don't copy source file's flags that's why we don't allow to clone 1110 * files that are in quarantine. 1111 */ 1112 if (inzp->z_pflags & ZFS_AV_QUARANTINED) { 1113 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1114 return (SET_ERROR(EACCES)); 1115 } 1116 1117 if (inoff >= inzp->z_size) { 1118 *lenp = 0; 1119 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1120 return (0); 1121 } 1122 if (len > inzp->z_size - inoff) { 1123 len = inzp->z_size - inoff; 1124 } 1125 if (len == 0) { 1126 *lenp = 0; 1127 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1128 return (0); 1129 } 1130 1131 /* 1132 * Callers might not be able to detect properly that we are read-only, 1133 * so check it explicitly here. 1134 */ 1135 if (zfs_is_readonly(outzfsvfs)) { 1136 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1137 return (SET_ERROR(EROFS)); 1138 } 1139 1140 /* 1141 * If immutable or not appending then return EPERM. 1142 * Intentionally allow ZFS_READONLY through here. 1143 * See zfs_zaccess_common() 1144 */ 1145 if ((outzp->z_pflags & ZFS_IMMUTABLE) != 0) { 1146 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1147 return (SET_ERROR(EPERM)); 1148 } 1149 1150 /* 1151 * No overlapping if we are cloning within the same file. 1152 */ 1153 if (inzp == outzp) { 1154 if (inoff < outoff + len && outoff < inoff + len) { 1155 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1156 return (SET_ERROR(EINVAL)); 1157 } 1158 } 1159 1160 /* 1161 * Maintain predictable lock order. 1162 */ 1163 if (inzp < outzp || (inzp == outzp && inoff < outoff)) { 1164 inlr = zfs_rangelock_enter(&inzp->z_rangelock, inoff, len, 1165 RL_READER); 1166 outlr = zfs_rangelock_enter(&outzp->z_rangelock, outoff, len, 1167 RL_WRITER); 1168 } else { 1169 outlr = zfs_rangelock_enter(&outzp->z_rangelock, outoff, len, 1170 RL_WRITER); 1171 inlr = zfs_rangelock_enter(&inzp->z_rangelock, inoff, len, 1172 RL_READER); 1173 } 1174 1175 inblksz = inzp->z_blksz; 1176 1177 /* 1178 * We cannot clone into files with different block size if we can't 1179 * grow it (block size is already bigger or more than one block). 1180 */ 1181 if (inblksz != outzp->z_blksz && (outzp->z_size > outzp->z_blksz || 1182 outzp->z_size > inblksz)) { 1183 error = SET_ERROR(EINVAL); 1184 goto unlock; 1185 } 1186 1187 /* 1188 * Block size must be power-of-2 if destination offset != 0. 1189 * There can be no multiple blocks of non-power-of-2 size. 1190 */ 1191 if (outoff != 0 && !ISP2(inblksz)) { 1192 error = SET_ERROR(EINVAL); 1193 goto unlock; 1194 } 1195 1196 /* 1197 * Offsets and len must be at block boundries. 1198 */ 1199 if ((inoff % inblksz) != 0 || (outoff % inblksz) != 0) { 1200 error = SET_ERROR(EINVAL); 1201 goto unlock; 1202 } 1203 /* 1204 * Length must be multipe of blksz, except for the end of the file. 1205 */ 1206 if ((len % inblksz) != 0 && 1207 (len < inzp->z_size - inoff || len < outzp->z_size - outoff)) { 1208 error = SET_ERROR(EINVAL); 1209 goto unlock; 1210 } 1211 1212 /* 1213 * If we are copying only one block and it is smaller than recordsize 1214 * property, do not allow destination to grow beyond one block if it 1215 * is not there yet. Otherwise the destination will get stuck with 1216 * that block size forever, that can be as small as 512 bytes, no 1217 * matter how big the destination grow later. 1218 */ 1219 if (len <= inblksz && inblksz < outzfsvfs->z_max_blksz && 1220 outzp->z_size <= inblksz && outoff + len > inblksz) { 1221 error = SET_ERROR(EINVAL); 1222 goto unlock; 1223 } 1224 1225 error = zn_rlimit_fsize(outoff + len); 1226 if (error != 0) { 1227 goto unlock; 1228 } 1229 1230 if (inoff >= MAXOFFSET_T || outoff >= MAXOFFSET_T) { 1231 error = SET_ERROR(EFBIG); 1232 goto unlock; 1233 } 1234 1235 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(outzfsvfs), NULL, 1236 &mtime, 16); 1237 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(outzfsvfs), NULL, 1238 &ctime, 16); 1239 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(outzfsvfs), NULL, 1240 &outzp->z_size, 8); 1241 1242 zilog = outzfsvfs->z_log; 1243 maxblocks = zil_max_log_data(zilog, sizeof (lr_clone_range_t)) / 1244 sizeof (bps[0]); 1245 1246 uid = KUID_TO_SUID(ZTOUID(outzp)); 1247 gid = KGID_TO_SGID(ZTOGID(outzp)); 1248 projid = outzp->z_projid; 1249 1250 bps = vmem_alloc(sizeof (bps[0]) * maxblocks, KM_SLEEP); 1251 1252 /* 1253 * Clone the file in reasonable size chunks. Each chunk is cloned 1254 * in a separate transaction; this keeps the intent log records small 1255 * and allows us to do more fine-grained space accounting. 1256 */ 1257 while (len > 0) { 1258 size = MIN(inblksz * maxblocks, len); 1259 1260 if (zfs_id_overblockquota(outzfsvfs, DMU_USERUSED_OBJECT, 1261 uid) || 1262 zfs_id_overblockquota(outzfsvfs, DMU_GROUPUSED_OBJECT, 1263 gid) || 1264 (projid != ZFS_DEFAULT_PROJID && 1265 zfs_id_overblockquota(outzfsvfs, DMU_PROJECTUSED_OBJECT, 1266 projid))) { 1267 error = SET_ERROR(EDQUOT); 1268 break; 1269 } 1270 1271 nbps = maxblocks; 1272 error = dmu_read_l0_bps(inos, inzp->z_id, inoff, size, bps, 1273 &nbps); 1274 if (error != 0) { 1275 /* 1276 * If we are trying to clone a block that was created 1277 * in the current transaction group, error will be 1278 * EAGAIN here, which we can just return to the caller 1279 * so it can fallback if it likes. 1280 */ 1281 break; 1282 } 1283 /* 1284 * Encrypted data is fine as long as it comes from the same 1285 * dataset. 1286 * TODO: We want to extend it in the future to allow cloning to 1287 * datasets with the same keys, like clones or to be able to 1288 * clone a file from a snapshot of an encrypted dataset into the 1289 * dataset itself. 1290 */ 1291 if (BP_IS_PROTECTED(&bps[0])) { 1292 if (inzfsvfs != outzfsvfs) { 1293 error = SET_ERROR(EXDEV); 1294 break; 1295 } 1296 } 1297 1298 /* 1299 * Start a transaction. 1300 */ 1301 tx = dmu_tx_create(outos); 1302 dmu_tx_hold_sa(tx, outzp->z_sa_hdl, B_FALSE); 1303 db = (dmu_buf_impl_t *)sa_get_db(outzp->z_sa_hdl); 1304 DB_DNODE_ENTER(db); 1305 dmu_tx_hold_clone_by_dnode(tx, DB_DNODE(db), outoff, size); 1306 DB_DNODE_EXIT(db); 1307 zfs_sa_upgrade_txholds(tx, outzp); 1308 error = dmu_tx_assign(tx, TXG_WAIT); 1309 if (error != 0) { 1310 dmu_tx_abort(tx); 1311 break; 1312 } 1313 1314 /* 1315 * Copy source znode's block size. This only happens on the 1316 * first iteration since zfs_rangelock_reduce() will shrink down 1317 * lr_len to the appropriate size. 1318 */ 1319 if (outlr->lr_length == UINT64_MAX) { 1320 zfs_grow_blocksize(outzp, inblksz, tx); 1321 /* 1322 * Round range lock up to the block boundary, so we 1323 * prevent appends until we are done. 1324 */ 1325 zfs_rangelock_reduce(outlr, outoff, 1326 ((len - 1) / inblksz + 1) * inblksz); 1327 } 1328 1329 error = dmu_brt_clone(outos, outzp->z_id, outoff, size, tx, 1330 bps, nbps, B_FALSE); 1331 if (error != 0) { 1332 dmu_tx_commit(tx); 1333 break; 1334 } 1335 1336 zfs_clear_setid_bits_if_necessary(outzfsvfs, outzp, cr, 1337 &clear_setid_bits_txg, tx); 1338 1339 zfs_tstamp_update_setup(outzp, CONTENT_MODIFIED, mtime, ctime); 1340 1341 /* 1342 * Update the file size (zp_size) if it has changed; 1343 * account for possible concurrent updates. 1344 */ 1345 while ((outsize = outzp->z_size) < outoff + size) { 1346 (void) atomic_cas_64(&outzp->z_size, outsize, 1347 outoff + size); 1348 } 1349 1350 error = sa_bulk_update(outzp->z_sa_hdl, bulk, count, tx); 1351 1352 zfs_log_clone_range(zilog, tx, TX_CLONE_RANGE, outzp, outoff, 1353 size, inblksz, bps, nbps); 1354 1355 dmu_tx_commit(tx); 1356 1357 if (error != 0) 1358 break; 1359 1360 inoff += size; 1361 outoff += size; 1362 len -= size; 1363 done += size; 1364 } 1365 1366 vmem_free(bps, sizeof (bps[0]) * maxblocks); 1367 zfs_znode_update_vfs(outzp); 1368 1369 unlock: 1370 zfs_rangelock_exit(outlr); 1371 zfs_rangelock_exit(inlr); 1372 1373 if (done > 0) { 1374 /* 1375 * If we have made at least partial progress, reset the error. 1376 */ 1377 error = 0; 1378 1379 ZFS_ACCESSTIME_STAMP(inzfsvfs, inzp); 1380 1381 if (outos->os_sync == ZFS_SYNC_ALWAYS) { 1382 zil_commit(zilog, outzp->z_id); 1383 } 1384 1385 *inoffp += done; 1386 *outoffp += done; 1387 *lenp = done; 1388 } else { 1389 /* 1390 * If we made no progress, there must be a good reason. 1391 * EOF is handled explicitly above, before the loop. 1392 */ 1393 ASSERT3S(error, !=, 0); 1394 } 1395 1396 zfs_exit_two(inzfsvfs, outzfsvfs, FTAG); 1397 1398 return (error); 1399 } 1400 1401 /* 1402 * Usual pattern would be to call zfs_clone_range() from zfs_replay_clone(), 1403 * but we cannot do that, because when replaying we don't have source znode 1404 * available. This is why we need a dedicated replay function. 1405 */ 1406 int 1407 zfs_clone_range_replay(znode_t *zp, uint64_t off, uint64_t len, uint64_t blksz, 1408 const blkptr_t *bps, size_t nbps) 1409 { 1410 zfsvfs_t *zfsvfs; 1411 dmu_buf_impl_t *db; 1412 dmu_tx_t *tx; 1413 int error; 1414 int count = 0; 1415 sa_bulk_attr_t bulk[3]; 1416 uint64_t mtime[2], ctime[2]; 1417 1418 ASSERT3U(off, <, MAXOFFSET_T); 1419 ASSERT3U(len, >, 0); 1420 ASSERT3U(nbps, >, 0); 1421 1422 zfsvfs = ZTOZSB(zp); 1423 1424 ASSERT(spa_feature_is_enabled(dmu_objset_spa(zfsvfs->z_os), 1425 SPA_FEATURE_BLOCK_CLONING)); 1426 1427 if ((error = zfs_enter_verify_zp(zfsvfs, zp, FTAG)) != 0) 1428 return (error); 1429 1430 ASSERT(zfsvfs->z_replay); 1431 ASSERT(!zfs_is_readonly(zfsvfs)); 1432 1433 if ((off % blksz) != 0) { 1434 zfs_exit(zfsvfs, FTAG); 1435 return (SET_ERROR(EINVAL)); 1436 } 1437 1438 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 1439 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 1440 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 1441 &zp->z_size, 8); 1442 1443 /* 1444 * Start a transaction. 1445 */ 1446 tx = dmu_tx_create(zfsvfs->z_os); 1447 1448 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1449 db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl); 1450 DB_DNODE_ENTER(db); 1451 dmu_tx_hold_clone_by_dnode(tx, DB_DNODE(db), off, len); 1452 DB_DNODE_EXIT(db); 1453 zfs_sa_upgrade_txholds(tx, zp); 1454 error = dmu_tx_assign(tx, TXG_WAIT); 1455 if (error != 0) { 1456 dmu_tx_abort(tx); 1457 zfs_exit(zfsvfs, FTAG); 1458 return (error); 1459 } 1460 1461 if (zp->z_blksz < blksz) 1462 zfs_grow_blocksize(zp, blksz, tx); 1463 1464 dmu_brt_clone(zfsvfs->z_os, zp->z_id, off, len, tx, bps, nbps, B_TRUE); 1465 1466 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime); 1467 1468 if (zp->z_size < off + len) 1469 zp->z_size = off + len; 1470 1471 error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1472 1473 /* 1474 * zil_replaying() not only check if we are replaying ZIL, but also 1475 * updates the ZIL header to record replay progress. 1476 */ 1477 VERIFY(zil_replaying(zfsvfs->z_log, tx)); 1478 1479 dmu_tx_commit(tx); 1480 1481 zfs_znode_update_vfs(zp); 1482 1483 zfs_exit(zfsvfs, FTAG); 1484 1485 return (error); 1486 } 1487 1488 EXPORT_SYMBOL(zfs_access); 1489 EXPORT_SYMBOL(zfs_fsync); 1490 EXPORT_SYMBOL(zfs_holey); 1491 EXPORT_SYMBOL(zfs_read); 1492 EXPORT_SYMBOL(zfs_write); 1493 EXPORT_SYMBOL(zfs_getsecattr); 1494 EXPORT_SYMBOL(zfs_setsecattr); 1495 EXPORT_SYMBOL(zfs_clone_range); 1496 EXPORT_SYMBOL(zfs_clone_range_replay); 1497 1498 ZFS_MODULE_PARAM(zfs_vnops, zfs_vnops_, read_chunk_size, U64, ZMOD_RW, 1499 "Bytes to read per chunk"); 1500