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 */ 28 29 /* Portions Copyright 2007 Jeremy Teo */ 30 /* Portions Copyright 2010 Robert Milkowski */ 31 32 #include <sys/types.h> 33 #include <sys/param.h> 34 #include <sys/time.h> 35 #include <sys/sysmacros.h> 36 #include <sys/vfs.h> 37 #include <sys/uio_impl.h> 38 #include <sys/file.h> 39 #include <sys/stat.h> 40 #include <sys/kmem.h> 41 #include <sys/cmn_err.h> 42 #include <sys/errno.h> 43 #include <sys/zfs_dir.h> 44 #include <sys/zfs_acl.h> 45 #include <sys/zfs_ioctl.h> 46 #include <sys/fs/zfs.h> 47 #include <sys/dmu.h> 48 #include <sys/dmu_objset.h> 49 #include <sys/spa.h> 50 #include <sys/txg.h> 51 #include <sys/dbuf.h> 52 #include <sys/policy.h> 53 #include <sys/zfs_vnops.h> 54 #include <sys/zfs_quota.h> 55 #include <sys/zfs_vfsops.h> 56 #include <sys/zfs_znode.h> 57 58 59 static ulong_t zfs_fsync_sync_cnt = 4; 60 61 int 62 zfs_fsync(znode_t *zp, int syncflag, cred_t *cr) 63 { 64 zfsvfs_t *zfsvfs = ZTOZSB(zp); 65 66 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 67 68 if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 69 ZFS_ENTER(zfsvfs); 70 ZFS_VERIFY_ZP(zp); 71 atomic_inc_32(&zp->z_sync_writes_cnt); 72 zil_commit(zfsvfs->z_log, zp->z_id); 73 atomic_dec_32(&zp->z_sync_writes_cnt); 74 ZFS_EXIT(zfsvfs); 75 } 76 tsd_set(zfs_fsyncer_key, NULL); 77 78 return (0); 79 } 80 81 82 #if defined(SEEK_HOLE) && defined(SEEK_DATA) 83 /* 84 * Lseek support for finding holes (cmd == SEEK_HOLE) and 85 * data (cmd == SEEK_DATA). "off" is an in/out parameter. 86 */ 87 static int 88 zfs_holey_common(znode_t *zp, ulong_t cmd, loff_t *off) 89 { 90 zfs_locked_range_t *lr; 91 uint64_t noff = (uint64_t)*off; /* new offset */ 92 uint64_t file_sz; 93 int error; 94 boolean_t hole; 95 96 file_sz = zp->z_size; 97 if (noff >= file_sz) { 98 return (SET_ERROR(ENXIO)); 99 } 100 101 if (cmd == F_SEEK_HOLE) 102 hole = B_TRUE; 103 else 104 hole = B_FALSE; 105 106 /* Flush any mmap()'d data to disk */ 107 if (zn_has_cached_data(zp)) 108 zn_flush_cached_data(zp, B_FALSE); 109 110 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, file_sz, RL_READER); 111 error = dmu_offset_next(ZTOZSB(zp)->z_os, zp->z_id, hole, &noff); 112 zfs_rangelock_exit(lr); 113 114 if (error == ESRCH) 115 return (SET_ERROR(ENXIO)); 116 117 /* File was dirty, so fall back to using generic logic */ 118 if (error == EBUSY) { 119 if (hole) 120 *off = file_sz; 121 122 return (0); 123 } 124 125 /* 126 * We could find a hole that begins after the logical end-of-file, 127 * because dmu_offset_next() only works on whole blocks. If the 128 * EOF falls mid-block, then indicate that the "virtual hole" 129 * at the end of the file begins at the logical EOF, rather than 130 * at the end of the last block. 131 */ 132 if (noff > file_sz) { 133 ASSERT(hole); 134 noff = file_sz; 135 } 136 137 if (noff < *off) 138 return (error); 139 *off = noff; 140 return (error); 141 } 142 143 int 144 zfs_holey(znode_t *zp, ulong_t cmd, loff_t *off) 145 { 146 zfsvfs_t *zfsvfs = ZTOZSB(zp); 147 int error; 148 149 ZFS_ENTER(zfsvfs); 150 ZFS_VERIFY_ZP(zp); 151 152 error = zfs_holey_common(zp, cmd, off); 153 154 ZFS_EXIT(zfsvfs); 155 return (error); 156 } 157 #endif /* SEEK_HOLE && SEEK_DATA */ 158 159 int 160 zfs_access(znode_t *zp, int mode, int flag, cred_t *cr) 161 { 162 zfsvfs_t *zfsvfs = ZTOZSB(zp); 163 int error; 164 165 ZFS_ENTER(zfsvfs); 166 ZFS_VERIFY_ZP(zp); 167 168 if (flag & V_ACE_MASK) 169 error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 170 else 171 error = zfs_zaccess_rwx(zp, mode, flag, cr); 172 173 ZFS_EXIT(zfsvfs); 174 return (error); 175 } 176 177 static unsigned long zfs_vnops_read_chunk_size = 1024 * 1024; /* Tunable */ 178 179 /* 180 * Read bytes from specified file into supplied buffer. 181 * 182 * IN: zp - inode of file to be read from. 183 * uio - structure supplying read location, range info, 184 * and return buffer. 185 * ioflag - O_SYNC flags; used to provide FRSYNC semantics. 186 * O_DIRECT flag; used to bypass page cache. 187 * cr - credentials of caller. 188 * 189 * OUT: uio - updated offset and range, buffer filled. 190 * 191 * RETURN: 0 on success, error code on failure. 192 * 193 * Side Effects: 194 * inode - atime updated if byte count > 0 195 */ 196 int 197 zfs_read(struct znode *zp, zfs_uio_t *uio, int ioflag, cred_t *cr) 198 { 199 (void) cr; 200 int error = 0; 201 boolean_t frsync = B_FALSE; 202 203 zfsvfs_t *zfsvfs = ZTOZSB(zp); 204 ZFS_ENTER(zfsvfs); 205 ZFS_VERIFY_ZP(zp); 206 207 if (zp->z_pflags & ZFS_AV_QUARANTINED) { 208 ZFS_EXIT(zfsvfs); 209 return (SET_ERROR(EACCES)); 210 } 211 212 /* We don't copy out anything useful for directories. */ 213 if (Z_ISDIR(ZTOTYPE(zp))) { 214 ZFS_EXIT(zfsvfs); 215 return (SET_ERROR(EISDIR)); 216 } 217 218 /* 219 * Validate file offset 220 */ 221 if (zfs_uio_offset(uio) < (offset_t)0) { 222 ZFS_EXIT(zfsvfs); 223 return (SET_ERROR(EINVAL)); 224 } 225 226 /* 227 * Fasttrack empty reads 228 */ 229 if (zfs_uio_resid(uio) == 0) { 230 ZFS_EXIT(zfsvfs); 231 return (0); 232 } 233 234 #ifdef FRSYNC 235 /* 236 * If we're in FRSYNC mode, sync out this znode before reading it. 237 * Only do this for non-snapshots. 238 * 239 * Some platforms do not support FRSYNC and instead map it 240 * to O_SYNC, which results in unnecessary calls to zil_commit. We 241 * only honor FRSYNC requests on platforms which support it. 242 */ 243 frsync = !!(ioflag & FRSYNC); 244 #endif 245 if (zfsvfs->z_log && 246 (frsync || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS)) 247 zil_commit(zfsvfs->z_log, zp->z_id); 248 249 /* 250 * Lock the range against changes. 251 */ 252 zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock, 253 zfs_uio_offset(uio), zfs_uio_resid(uio), RL_READER); 254 255 /* 256 * If we are reading past end-of-file we can skip 257 * to the end; but we might still need to set atime. 258 */ 259 if (zfs_uio_offset(uio) >= zp->z_size) { 260 error = 0; 261 goto out; 262 } 263 264 ASSERT(zfs_uio_offset(uio) < zp->z_size); 265 #if defined(__linux__) 266 ssize_t start_offset = zfs_uio_offset(uio); 267 #endif 268 ssize_t n = MIN(zfs_uio_resid(uio), zp->z_size - zfs_uio_offset(uio)); 269 ssize_t start_resid = n; 270 271 while (n > 0) { 272 ssize_t nbytes = MIN(n, zfs_vnops_read_chunk_size - 273 P2PHASE(zfs_uio_offset(uio), zfs_vnops_read_chunk_size)); 274 #ifdef UIO_NOCOPY 275 if (zfs_uio_segflg(uio) == UIO_NOCOPY) 276 error = mappedread_sf(zp, nbytes, uio); 277 else 278 #endif 279 if (zn_has_cached_data(zp) && !(ioflag & O_DIRECT)) { 280 error = mappedread(zp, nbytes, uio); 281 } else { 282 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 283 uio, nbytes); 284 } 285 286 if (error) { 287 /* convert checksum errors into IO errors */ 288 if (error == ECKSUM) 289 error = SET_ERROR(EIO); 290 291 #if defined(__linux__) 292 /* 293 * if we actually read some bytes, bubbling EFAULT 294 * up to become EAGAIN isn't what we want here... 295 * 296 * ...on Linux, at least. On FBSD, doing this breaks. 297 */ 298 if (error == EFAULT && 299 (zfs_uio_offset(uio) - start_offset) != 0) 300 error = 0; 301 #endif 302 break; 303 } 304 305 n -= nbytes; 306 } 307 308 int64_t nread = start_resid - n; 309 dataset_kstats_update_read_kstats(&zfsvfs->z_kstat, nread); 310 task_io_account_read(nread); 311 out: 312 zfs_rangelock_exit(lr); 313 314 ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 315 ZFS_EXIT(zfsvfs); 316 return (error); 317 } 318 319 static void 320 zfs_clear_setid_bits_if_necessary(zfsvfs_t *zfsvfs, znode_t *zp, cred_t *cr, 321 uint64_t *clear_setid_bits_txgp, dmu_tx_t *tx) 322 { 323 zilog_t *zilog = zfsvfs->z_log; 324 const uint64_t uid = KUID_TO_SUID(ZTOUID(zp)); 325 326 ASSERT(clear_setid_bits_txgp != NULL); 327 ASSERT(tx != NULL); 328 329 /* 330 * Clear Set-UID/Set-GID bits on successful write if not 331 * privileged and at least one of the execute bits is set. 332 * 333 * It would be nice to do this after all writes have 334 * been done, but that would still expose the ISUID/ISGID 335 * to another app after the partial write is committed. 336 * 337 * Note: we don't call zfs_fuid_map_id() here because 338 * user 0 is not an ephemeral uid. 339 */ 340 mutex_enter(&zp->z_acl_lock); 341 if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | (S_IXUSR >> 6))) != 0 && 342 (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 343 secpolicy_vnode_setid_retain(zp, cr, 344 ((zp->z_mode & S_ISUID) != 0 && uid == 0)) != 0) { 345 uint64_t newmode; 346 347 zp->z_mode &= ~(S_ISUID | S_ISGID); 348 newmode = zp->z_mode; 349 (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 350 (void *)&newmode, sizeof (uint64_t), tx); 351 352 mutex_exit(&zp->z_acl_lock); 353 354 /* 355 * Make sure SUID/SGID bits will be removed when we replay the 356 * log. If the setid bits are keep coming back, don't log more 357 * than one TX_SETATTR per transaction group. 358 */ 359 if (*clear_setid_bits_txgp != dmu_tx_get_txg(tx)) { 360 vattr_t va = {0}; 361 362 va.va_mask = ATTR_MODE; 363 va.va_nodeid = zp->z_id; 364 va.va_mode = newmode; 365 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, &va, 366 ATTR_MODE, NULL); 367 *clear_setid_bits_txgp = dmu_tx_get_txg(tx); 368 } 369 } else { 370 mutex_exit(&zp->z_acl_lock); 371 } 372 } 373 374 /* 375 * Write the bytes to a file. 376 * 377 * IN: zp - znode of file to be written to. 378 * uio - structure supplying write location, range info, 379 * and data buffer. 380 * ioflag - O_APPEND flag set if in append mode. 381 * O_DIRECT flag; used to bypass page cache. 382 * cr - credentials of caller. 383 * 384 * OUT: uio - updated offset and range. 385 * 386 * RETURN: 0 if success 387 * error code if failure 388 * 389 * Timestamps: 390 * ip - ctime|mtime updated if byte count > 0 391 */ 392 int 393 zfs_write(znode_t *zp, zfs_uio_t *uio, int ioflag, cred_t *cr) 394 { 395 int error = 0, error1; 396 ssize_t start_resid = zfs_uio_resid(uio); 397 uint64_t clear_setid_bits_txg = 0; 398 399 /* 400 * Fasttrack empty write 401 */ 402 ssize_t n = start_resid; 403 if (n == 0) 404 return (0); 405 406 zfsvfs_t *zfsvfs = ZTOZSB(zp); 407 ZFS_ENTER(zfsvfs); 408 ZFS_VERIFY_ZP(zp); 409 410 sa_bulk_attr_t bulk[4]; 411 int count = 0; 412 uint64_t mtime[2], ctime[2]; 413 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 414 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 415 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 416 &zp->z_size, 8); 417 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 418 &zp->z_pflags, 8); 419 420 /* 421 * Callers might not be able to detect properly that we are read-only, 422 * so check it explicitly here. 423 */ 424 if (zfs_is_readonly(zfsvfs)) { 425 ZFS_EXIT(zfsvfs); 426 return (SET_ERROR(EROFS)); 427 } 428 429 /* 430 * If immutable or not appending then return EPERM. 431 * Intentionally allow ZFS_READONLY through here. 432 * See zfs_zaccess_common() 433 */ 434 if ((zp->z_pflags & ZFS_IMMUTABLE) || 435 ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & O_APPEND) && 436 (zfs_uio_offset(uio) < zp->z_size))) { 437 ZFS_EXIT(zfsvfs); 438 return (SET_ERROR(EPERM)); 439 } 440 441 /* 442 * Validate file offset 443 */ 444 offset_t woff = ioflag & O_APPEND ? zp->z_size : zfs_uio_offset(uio); 445 if (woff < 0) { 446 ZFS_EXIT(zfsvfs); 447 return (SET_ERROR(EINVAL)); 448 } 449 450 const uint64_t max_blksz = zfsvfs->z_max_blksz; 451 452 /* 453 * Pre-fault the pages to ensure slow (eg NFS) pages 454 * don't hold up txg. 455 * Skip this if uio contains loaned arc_buf. 456 */ 457 if (zfs_uio_prefaultpages(MIN(n, max_blksz), uio)) { 458 ZFS_EXIT(zfsvfs); 459 return (SET_ERROR(EFAULT)); 460 } 461 462 /* 463 * If in append mode, set the io offset pointer to eof. 464 */ 465 zfs_locked_range_t *lr; 466 if (ioflag & O_APPEND) { 467 /* 468 * Obtain an appending range lock to guarantee file append 469 * semantics. We reset the write offset once we have the lock. 470 */ 471 lr = zfs_rangelock_enter(&zp->z_rangelock, 0, n, RL_APPEND); 472 woff = lr->lr_offset; 473 if (lr->lr_length == UINT64_MAX) { 474 /* 475 * We overlocked the file because this write will cause 476 * the file block size to increase. 477 * Note that zp_size cannot change with this lock held. 478 */ 479 woff = zp->z_size; 480 } 481 zfs_uio_setoffset(uio, woff); 482 } else { 483 /* 484 * Note that if the file block size will change as a result of 485 * this write, then this range lock will lock the entire file 486 * so that we can re-write the block safely. 487 */ 488 lr = zfs_rangelock_enter(&zp->z_rangelock, woff, n, RL_WRITER); 489 } 490 491 if (zn_rlimit_fsize(zp, uio)) { 492 zfs_rangelock_exit(lr); 493 ZFS_EXIT(zfsvfs); 494 return (SET_ERROR(EFBIG)); 495 } 496 497 const rlim64_t limit = MAXOFFSET_T; 498 499 if (woff >= limit) { 500 zfs_rangelock_exit(lr); 501 ZFS_EXIT(zfsvfs); 502 return (SET_ERROR(EFBIG)); 503 } 504 505 if (n > limit - woff) 506 n = limit - woff; 507 508 uint64_t end_size = MAX(zp->z_size, woff + n); 509 zilog_t *zilog = zfsvfs->z_log; 510 511 const uint64_t uid = KUID_TO_SUID(ZTOUID(zp)); 512 const uint64_t gid = KGID_TO_SGID(ZTOGID(zp)); 513 const uint64_t projid = zp->z_projid; 514 515 /* 516 * Write the file in reasonable size chunks. Each chunk is written 517 * in a separate transaction; this keeps the intent log records small 518 * and allows us to do more fine-grained space accounting. 519 */ 520 while (n > 0) { 521 woff = zfs_uio_offset(uio); 522 523 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, uid) || 524 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, gid) || 525 (projid != ZFS_DEFAULT_PROJID && 526 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT, 527 projid))) { 528 error = SET_ERROR(EDQUOT); 529 break; 530 } 531 532 arc_buf_t *abuf = NULL; 533 if (n >= max_blksz && woff >= zp->z_size && 534 P2PHASE(woff, max_blksz) == 0 && 535 zp->z_blksz == max_blksz) { 536 /* 537 * This write covers a full block. "Borrow" a buffer 538 * from the dmu so that we can fill it before we enter 539 * a transaction. This avoids the possibility of 540 * holding up the transaction if the data copy hangs 541 * up on a pagefault (e.g., from an NFS server mapping). 542 */ 543 size_t cbytes; 544 545 abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 546 max_blksz); 547 ASSERT(abuf != NULL); 548 ASSERT(arc_buf_size(abuf) == max_blksz); 549 if ((error = zfs_uiocopy(abuf->b_data, max_blksz, 550 UIO_WRITE, uio, &cbytes))) { 551 dmu_return_arcbuf(abuf); 552 break; 553 } 554 ASSERT3S(cbytes, ==, max_blksz); 555 } 556 557 /* 558 * Start a transaction. 559 */ 560 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 561 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 562 dmu_buf_impl_t *db = (dmu_buf_impl_t *)sa_get_db(zp->z_sa_hdl); 563 DB_DNODE_ENTER(db); 564 dmu_tx_hold_write_by_dnode(tx, DB_DNODE(db), woff, 565 MIN(n, max_blksz)); 566 DB_DNODE_EXIT(db); 567 zfs_sa_upgrade_txholds(tx, zp); 568 error = dmu_tx_assign(tx, TXG_WAIT); 569 if (error) { 570 dmu_tx_abort(tx); 571 if (abuf != NULL) 572 dmu_return_arcbuf(abuf); 573 break; 574 } 575 576 /* 577 * NB: We must call zfs_clear_setid_bits_if_necessary before 578 * committing the transaction! 579 */ 580 581 /* 582 * If rangelock_enter() over-locked we grow the blocksize 583 * and then reduce the lock range. This will only happen 584 * on the first iteration since rangelock_reduce() will 585 * shrink down lr_length to the appropriate size. 586 */ 587 if (lr->lr_length == UINT64_MAX) { 588 uint64_t new_blksz; 589 590 if (zp->z_blksz > max_blksz) { 591 /* 592 * File's blocksize is already larger than the 593 * "recordsize" property. Only let it grow to 594 * the next power of 2. 595 */ 596 ASSERT(!ISP2(zp->z_blksz)); 597 new_blksz = MIN(end_size, 598 1 << highbit64(zp->z_blksz)); 599 } else { 600 new_blksz = MIN(end_size, max_blksz); 601 } 602 zfs_grow_blocksize(zp, new_blksz, tx); 603 zfs_rangelock_reduce(lr, woff, n); 604 } 605 606 /* 607 * XXX - should we really limit each write to z_max_blksz? 608 * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 609 */ 610 const ssize_t nbytes = 611 MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 612 613 ssize_t tx_bytes; 614 if (abuf == NULL) { 615 tx_bytes = zfs_uio_resid(uio); 616 zfs_uio_fault_disable(uio, B_TRUE); 617 error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 618 uio, nbytes, tx); 619 zfs_uio_fault_disable(uio, B_FALSE); 620 #ifdef __linux__ 621 if (error == EFAULT) { 622 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, 623 cr, &clear_setid_bits_txg, tx); 624 dmu_tx_commit(tx); 625 /* 626 * Account for partial writes before 627 * continuing the loop. 628 * Update needs to occur before the next 629 * zfs_uio_prefaultpages, or prefaultpages may 630 * error, and we may break the loop early. 631 */ 632 if (tx_bytes != zfs_uio_resid(uio)) 633 n -= tx_bytes - zfs_uio_resid(uio); 634 if (zfs_uio_prefaultpages(MIN(n, max_blksz), 635 uio)) { 636 break; 637 } 638 continue; 639 } 640 #endif 641 /* 642 * On FreeBSD, EFAULT should be propagated back to the 643 * VFS, which will handle faulting and will retry. 644 */ 645 if (error != 0 && error != EFAULT) { 646 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, 647 cr, &clear_setid_bits_txg, tx); 648 dmu_tx_commit(tx); 649 break; 650 } 651 tx_bytes -= zfs_uio_resid(uio); 652 } else { 653 /* Implied by abuf != NULL: */ 654 ASSERT3S(n, >=, max_blksz); 655 ASSERT0(P2PHASE(woff, max_blksz)); 656 /* 657 * We can simplify nbytes to MIN(n, max_blksz) since 658 * P2PHASE(woff, max_blksz) is 0, and knowing 659 * n >= max_blksz lets us simplify further: 660 */ 661 ASSERT3S(nbytes, ==, max_blksz); 662 /* 663 * Thus, we're writing a full block at a block-aligned 664 * offset and extending the file past EOF. 665 * 666 * dmu_assign_arcbuf_by_dbuf() will directly assign the 667 * arc buffer to a dbuf. 668 */ 669 error = dmu_assign_arcbuf_by_dbuf( 670 sa_get_db(zp->z_sa_hdl), woff, abuf, tx); 671 if (error != 0) { 672 /* 673 * XXX This might not be necessary if 674 * dmu_assign_arcbuf_by_dbuf is guaranteed 675 * to be atomic. 676 */ 677 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, 678 cr, &clear_setid_bits_txg, tx); 679 dmu_return_arcbuf(abuf); 680 dmu_tx_commit(tx); 681 break; 682 } 683 ASSERT3S(nbytes, <=, zfs_uio_resid(uio)); 684 zfs_uioskip(uio, nbytes); 685 tx_bytes = nbytes; 686 } 687 if (tx_bytes && zn_has_cached_data(zp) && 688 !(ioflag & O_DIRECT)) { 689 update_pages(zp, woff, tx_bytes, zfsvfs->z_os); 690 } 691 692 /* 693 * If we made no progress, we're done. If we made even 694 * partial progress, update the znode and ZIL accordingly. 695 */ 696 if (tx_bytes == 0) { 697 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 698 (void *)&zp->z_size, sizeof (uint64_t), tx); 699 dmu_tx_commit(tx); 700 ASSERT(error != 0); 701 break; 702 } 703 704 zfs_clear_setid_bits_if_necessary(zfsvfs, zp, cr, 705 &clear_setid_bits_txg, tx); 706 707 zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime); 708 709 /* 710 * Update the file size (zp_size) if it has changed; 711 * account for possible concurrent updates. 712 */ 713 while ((end_size = zp->z_size) < zfs_uio_offset(uio)) { 714 (void) atomic_cas_64(&zp->z_size, end_size, 715 zfs_uio_offset(uio)); 716 ASSERT(error == 0 || error == EFAULT); 717 } 718 /* 719 * If we are replaying and eof is non zero then force 720 * the file size to the specified eof. Note, there's no 721 * concurrency during replay. 722 */ 723 if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 724 zp->z_size = zfsvfs->z_replay_eof; 725 726 error1 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 727 if (error1 != 0) 728 /* Avoid clobbering EFAULT. */ 729 error = error1; 730 731 /* 732 * NB: During replay, the TX_SETATTR record logged by 733 * zfs_clear_setid_bits_if_necessary must precede any of 734 * the TX_WRITE records logged here. 735 */ 736 zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag, 737 NULL, NULL); 738 739 dmu_tx_commit(tx); 740 741 if (error != 0) 742 break; 743 ASSERT3S(tx_bytes, ==, nbytes); 744 n -= nbytes; 745 746 if (n > 0) { 747 if (zfs_uio_prefaultpages(MIN(n, max_blksz), uio)) { 748 error = SET_ERROR(EFAULT); 749 break; 750 } 751 } 752 } 753 754 zfs_znode_update_vfs(zp); 755 zfs_rangelock_exit(lr); 756 757 /* 758 * If we're in replay mode, or we made no progress, or the 759 * uio data is inaccessible return an error. Otherwise, it's 760 * at least a partial write, so it's successful. 761 */ 762 if (zfsvfs->z_replay || zfs_uio_resid(uio) == start_resid || 763 error == EFAULT) { 764 ZFS_EXIT(zfsvfs); 765 return (error); 766 } 767 768 if (ioflag & (O_SYNC | O_DSYNC) || 769 zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 770 zil_commit(zilog, zp->z_id); 771 772 const int64_t nwritten = start_resid - zfs_uio_resid(uio); 773 dataset_kstats_update_write_kstats(&zfsvfs->z_kstat, nwritten); 774 task_io_account_write(nwritten); 775 776 ZFS_EXIT(zfsvfs); 777 return (0); 778 } 779 780 int 781 zfs_getsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr) 782 { 783 zfsvfs_t *zfsvfs = ZTOZSB(zp); 784 int error; 785 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 786 787 ZFS_ENTER(zfsvfs); 788 ZFS_VERIFY_ZP(zp); 789 error = zfs_getacl(zp, vsecp, skipaclchk, cr); 790 ZFS_EXIT(zfsvfs); 791 792 return (error); 793 } 794 795 int 796 zfs_setsecattr(znode_t *zp, vsecattr_t *vsecp, int flag, cred_t *cr) 797 { 798 zfsvfs_t *zfsvfs = ZTOZSB(zp); 799 int error; 800 boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 801 zilog_t *zilog = zfsvfs->z_log; 802 803 ZFS_ENTER(zfsvfs); 804 ZFS_VERIFY_ZP(zp); 805 806 error = zfs_setacl(zp, vsecp, skipaclchk, cr); 807 808 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 809 zil_commit(zilog, 0); 810 811 ZFS_EXIT(zfsvfs); 812 return (error); 813 } 814 815 #ifdef ZFS_DEBUG 816 static int zil_fault_io = 0; 817 #endif 818 819 static void zfs_get_done(zgd_t *zgd, int error); 820 821 /* 822 * Get data to generate a TX_WRITE intent log record. 823 */ 824 int 825 zfs_get_data(void *arg, uint64_t gen, lr_write_t *lr, char *buf, 826 struct lwb *lwb, zio_t *zio) 827 { 828 zfsvfs_t *zfsvfs = arg; 829 objset_t *os = zfsvfs->z_os; 830 znode_t *zp; 831 uint64_t object = lr->lr_foid; 832 uint64_t offset = lr->lr_offset; 833 uint64_t size = lr->lr_length; 834 dmu_buf_t *db; 835 zgd_t *zgd; 836 int error = 0; 837 uint64_t zp_gen; 838 839 ASSERT3P(lwb, !=, NULL); 840 ASSERT3P(zio, !=, NULL); 841 ASSERT3U(size, !=, 0); 842 843 /* 844 * Nothing to do if the file has been removed 845 */ 846 if (zfs_zget(zfsvfs, object, &zp) != 0) 847 return (SET_ERROR(ENOENT)); 848 if (zp->z_unlinked) { 849 /* 850 * Release the vnode asynchronously as we currently have the 851 * txg stopped from syncing. 852 */ 853 zfs_zrele_async(zp); 854 return (SET_ERROR(ENOENT)); 855 } 856 /* check if generation number matches */ 857 if (sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), &zp_gen, 858 sizeof (zp_gen)) != 0) { 859 zfs_zrele_async(zp); 860 return (SET_ERROR(EIO)); 861 } 862 if (zp_gen != gen) { 863 zfs_zrele_async(zp); 864 return (SET_ERROR(ENOENT)); 865 } 866 867 zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 868 zgd->zgd_lwb = lwb; 869 zgd->zgd_private = zp; 870 871 /* 872 * Write records come in two flavors: immediate and indirect. 873 * For small writes it's cheaper to store the data with the 874 * log record (immediate); for large writes it's cheaper to 875 * sync the data and get a pointer to it (indirect) so that 876 * we don't have to write the data twice. 877 */ 878 if (buf != NULL) { /* immediate write */ 879 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock, 880 offset, size, RL_READER); 881 /* test for truncation needs to be done while range locked */ 882 if (offset >= zp->z_size) { 883 error = SET_ERROR(ENOENT); 884 } else { 885 error = dmu_read(os, object, offset, size, buf, 886 DMU_READ_NO_PREFETCH); 887 } 888 ASSERT(error == 0 || error == ENOENT); 889 } else { /* indirect write */ 890 /* 891 * Have to lock the whole block to ensure when it's 892 * written out and its checksum is being calculated 893 * that no one can change the data. We need to re-check 894 * blocksize after we get the lock in case it's changed! 895 */ 896 for (;;) { 897 uint64_t blkoff; 898 size = zp->z_blksz; 899 blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 900 offset -= blkoff; 901 zgd->zgd_lr = zfs_rangelock_enter(&zp->z_rangelock, 902 offset, size, RL_READER); 903 if (zp->z_blksz == size) 904 break; 905 offset += blkoff; 906 zfs_rangelock_exit(zgd->zgd_lr); 907 } 908 /* test for truncation needs to be done while range locked */ 909 if (lr->lr_offset >= zp->z_size) 910 error = SET_ERROR(ENOENT); 911 #ifdef ZFS_DEBUG 912 if (zil_fault_io) { 913 error = SET_ERROR(EIO); 914 zil_fault_io = 0; 915 } 916 #endif 917 if (error == 0) 918 error = dmu_buf_hold(os, object, offset, zgd, &db, 919 DMU_READ_NO_PREFETCH); 920 921 if (error == 0) { 922 blkptr_t *bp = &lr->lr_blkptr; 923 924 zgd->zgd_db = db; 925 zgd->zgd_bp = bp; 926 927 ASSERT(db->db_offset == offset); 928 ASSERT(db->db_size == size); 929 930 error = dmu_sync(zio, lr->lr_common.lrc_txg, 931 zfs_get_done, zgd); 932 ASSERT(error || lr->lr_length <= size); 933 934 /* 935 * On success, we need to wait for the write I/O 936 * initiated by dmu_sync() to complete before we can 937 * release this dbuf. We will finish everything up 938 * in the zfs_get_done() callback. 939 */ 940 if (error == 0) 941 return (0); 942 943 if (error == EALREADY) { 944 lr->lr_common.lrc_txtype = TX_WRITE2; 945 /* 946 * TX_WRITE2 relies on the data previously 947 * written by the TX_WRITE that caused 948 * EALREADY. We zero out the BP because 949 * it is the old, currently-on-disk BP. 950 */ 951 zgd->zgd_bp = NULL; 952 BP_ZERO(bp); 953 error = 0; 954 } 955 } 956 } 957 958 zfs_get_done(zgd, error); 959 960 return (error); 961 } 962 963 964 static void 965 zfs_get_done(zgd_t *zgd, int error) 966 { 967 (void) error; 968 znode_t *zp = zgd->zgd_private; 969 970 if (zgd->zgd_db) 971 dmu_buf_rele(zgd->zgd_db, zgd); 972 973 zfs_rangelock_exit(zgd->zgd_lr); 974 975 /* 976 * Release the vnode asynchronously as we currently have the 977 * txg stopped from syncing. 978 */ 979 zfs_zrele_async(zp); 980 981 kmem_free(zgd, sizeof (zgd_t)); 982 } 983 984 EXPORT_SYMBOL(zfs_access); 985 EXPORT_SYMBOL(zfs_fsync); 986 EXPORT_SYMBOL(zfs_holey); 987 EXPORT_SYMBOL(zfs_read); 988 EXPORT_SYMBOL(zfs_write); 989 EXPORT_SYMBOL(zfs_getsecattr); 990 EXPORT_SYMBOL(zfs_setsecattr); 991 992 ZFS_MODULE_PARAM(zfs_vnops, zfs_vnops_, read_chunk_size, ULONG, ZMOD_RW, 993 "Bytes to read per chunk"); 994