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 /* 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 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/file.h> 39 #include <sys/stat.h> 40 #include <sys/kmem.h> 41 #include <sys/taskq.h> 42 #include <sys/uio.h> 43 #include <sys/vmsystm.h> 44 #include <sys/atomic.h> 45 #include <sys/pathname.h> 46 #include <sys/cmn_err.h> 47 #include <sys/errno.h> 48 #include <sys/zfs_dir.h> 49 #include <sys/zfs_acl.h> 50 #include <sys/zfs_ioctl.h> 51 #include <sys/fs/zfs.h> 52 #include <sys/dmu.h> 53 #include <sys/dmu_objset.h> 54 #include <sys/spa.h> 55 #include <sys/txg.h> 56 #include <sys/dbuf.h> 57 #include <sys/zap.h> 58 #include <sys/sa.h> 59 #include <sys/policy.h> 60 #include <sys/sunddi.h> 61 #include <sys/sid.h> 62 #include <sys/zfs_ctldir.h> 63 #include <sys/zfs_fuid.h> 64 #include <sys/zfs_quota.h> 65 #include <sys/zfs_sa.h> 66 #include <sys/zfs_vnops.h> 67 #include <sys/zfs_rlock.h> 68 #include <sys/cred.h> 69 #include <sys/zpl.h> 70 #include <sys/zil.h> 71 #include <sys/sa_impl.h> 72 73 /* 74 * Programming rules. 75 * 76 * Each vnode op performs some logical unit of work. To do this, the ZPL must 77 * properly lock its in-core state, create a DMU transaction, do the work, 78 * record this work in the intent log (ZIL), commit the DMU transaction, 79 * and wait for the intent log to commit if it is a synchronous operation. 80 * Moreover, the vnode ops must work in both normal and log replay context. 81 * The ordering of events is important to avoid deadlocks and references 82 * to freed memory. The example below illustrates the following Big Rules: 83 * 84 * (1) A check must be made in each zfs thread for a mounted file system. 85 * This is done avoiding races using ZFS_ENTER(zfsvfs). 86 * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 87 * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 88 * can return EIO from the calling function. 89 * 90 * (2) zrele() should always be the last thing except for zil_commit() (if 91 * necessary) and ZFS_EXIT(). This is for 3 reasons: First, if it's the 92 * last reference, the vnode/znode can be freed, so the zp may point to 93 * freed memory. Second, the last reference will call zfs_zinactive(), 94 * which may induce a lot of work -- pushing cached pages (which acquires 95 * range locks) and syncing out cached atime changes. Third, 96 * zfs_zinactive() may require a new tx, which could deadlock the system 97 * if you were already holding one. This deadlock occurs because the tx 98 * currently being operated on prevents a txg from syncing, which 99 * prevents the new tx from progressing, resulting in a deadlock. If you 100 * must call zrele() within a tx, use zfs_zrele_async(). Note that iput() 101 * is a synonym for zrele(). 102 * 103 * (3) All range locks must be grabbed before calling dmu_tx_assign(), 104 * as they can span dmu_tx_assign() calls. 105 * 106 * (4) If ZPL locks are held, pass TXG_NOWAIT as the second argument to 107 * dmu_tx_assign(). This is critical because we don't want to block 108 * while holding locks. 109 * 110 * If no ZPL locks are held (aside from ZFS_ENTER()), use TXG_WAIT. This 111 * reduces lock contention and CPU usage when we must wait (note that if 112 * throughput is constrained by the storage, nearly every transaction 113 * must wait). 114 * 115 * Note, in particular, that if a lock is sometimes acquired before 116 * the tx assigns, and sometimes after (e.g. z_lock), then failing 117 * to use a non-blocking assign can deadlock the system. The scenario: 118 * 119 * Thread A has grabbed a lock before calling dmu_tx_assign(). 120 * Thread B is in an already-assigned tx, and blocks for this lock. 121 * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 122 * forever, because the previous txg can't quiesce until B's tx commits. 123 * 124 * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 125 * then drop all locks, call dmu_tx_wait(), and try again. On subsequent 126 * calls to dmu_tx_assign(), pass TXG_NOTHROTTLE in addition to TXG_NOWAIT, 127 * to indicate that this operation has already called dmu_tx_wait(). 128 * This will ensure that we don't retry forever, waiting a short bit 129 * each time. 130 * 131 * (5) If the operation succeeded, generate the intent log entry for it 132 * before dropping locks. This ensures that the ordering of events 133 * in the intent log matches the order in which they actually occurred. 134 * During ZIL replay the zfs_log_* functions will update the sequence 135 * number to indicate the zil transaction has replayed. 136 * 137 * (6) At the end of each vnode op, the DMU tx must always commit, 138 * regardless of whether there were any errors. 139 * 140 * (7) After dropping all locks, invoke zil_commit(zilog, foid) 141 * to ensure that synchronous semantics are provided when necessary. 142 * 143 * In general, this is how things should be ordered in each vnode op: 144 * 145 * ZFS_ENTER(zfsvfs); // exit if unmounted 146 * top: 147 * zfs_dirent_lock(&dl, ...) // lock directory entry (may igrab()) 148 * rw_enter(...); // grab any other locks you need 149 * tx = dmu_tx_create(...); // get DMU tx 150 * dmu_tx_hold_*(); // hold each object you might modify 151 * error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 152 * if (error) { 153 * rw_exit(...); // drop locks 154 * zfs_dirent_unlock(dl); // unlock directory entry 155 * zrele(...); // release held znodes 156 * if (error == ERESTART) { 157 * waited = B_TRUE; 158 * dmu_tx_wait(tx); 159 * dmu_tx_abort(tx); 160 * goto top; 161 * } 162 * dmu_tx_abort(tx); // abort DMU tx 163 * ZFS_EXIT(zfsvfs); // finished in zfs 164 * return (error); // really out of space 165 * } 166 * error = do_real_work(); // do whatever this VOP does 167 * if (error == 0) 168 * zfs_log_*(...); // on success, make ZIL entry 169 * dmu_tx_commit(tx); // commit DMU tx -- error or not 170 * rw_exit(...); // drop locks 171 * zfs_dirent_unlock(dl); // unlock directory entry 172 * zrele(...); // release held znodes 173 * zil_commit(zilog, foid); // synchronous when necessary 174 * ZFS_EXIT(zfsvfs); // finished in zfs 175 * return (error); // done, report error 176 */ 177 178 /* ARGSUSED */ 179 int 180 zfs_open(struct inode *ip, int mode, int flag, cred_t *cr) 181 { 182 znode_t *zp = ITOZ(ip); 183 zfsvfs_t *zfsvfs = ITOZSB(ip); 184 185 ZFS_ENTER(zfsvfs); 186 ZFS_VERIFY_ZP(zp); 187 188 /* Honor ZFS_APPENDONLY file attribute */ 189 if ((mode & FMODE_WRITE) && (zp->z_pflags & ZFS_APPENDONLY) && 190 ((flag & O_APPEND) == 0)) { 191 ZFS_EXIT(zfsvfs); 192 return (SET_ERROR(EPERM)); 193 } 194 195 /* Keep a count of the synchronous opens in the znode */ 196 if (flag & O_SYNC) 197 atomic_inc_32(&zp->z_sync_cnt); 198 199 ZFS_EXIT(zfsvfs); 200 return (0); 201 } 202 203 /* ARGSUSED */ 204 int 205 zfs_close(struct inode *ip, int flag, cred_t *cr) 206 { 207 znode_t *zp = ITOZ(ip); 208 zfsvfs_t *zfsvfs = ITOZSB(ip); 209 210 ZFS_ENTER(zfsvfs); 211 ZFS_VERIFY_ZP(zp); 212 213 /* Decrement the synchronous opens in the znode */ 214 if (flag & O_SYNC) 215 atomic_dec_32(&zp->z_sync_cnt); 216 217 ZFS_EXIT(zfsvfs); 218 return (0); 219 } 220 221 #if defined(_KERNEL) 222 /* 223 * When a file is memory mapped, we must keep the IO data synchronized 224 * between the DMU cache and the memory mapped pages. What this means: 225 * 226 * On Write: If we find a memory mapped page, we write to *both* 227 * the page and the dmu buffer. 228 */ 229 void 230 update_pages(znode_t *zp, int64_t start, int len, objset_t *os) 231 { 232 struct inode *ip = ZTOI(zp); 233 struct address_space *mp = ip->i_mapping; 234 struct page *pp; 235 uint64_t nbytes; 236 int64_t off; 237 void *pb; 238 239 off = start & (PAGE_SIZE-1); 240 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) { 241 nbytes = MIN(PAGE_SIZE - off, len); 242 243 pp = find_lock_page(mp, start >> PAGE_SHIFT); 244 if (pp) { 245 if (mapping_writably_mapped(mp)) 246 flush_dcache_page(pp); 247 248 pb = kmap(pp); 249 (void) dmu_read(os, zp->z_id, start + off, nbytes, 250 pb + off, DMU_READ_PREFETCH); 251 kunmap(pp); 252 253 if (mapping_writably_mapped(mp)) 254 flush_dcache_page(pp); 255 256 mark_page_accessed(pp); 257 SetPageUptodate(pp); 258 ClearPageError(pp); 259 unlock_page(pp); 260 put_page(pp); 261 } 262 263 len -= nbytes; 264 off = 0; 265 } 266 } 267 268 /* 269 * When a file is memory mapped, we must keep the IO data synchronized 270 * between the DMU cache and the memory mapped pages. What this means: 271 * 272 * On Read: We "read" preferentially from memory mapped pages, 273 * else we default from the dmu buffer. 274 * 275 * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 276 * the file is memory mapped. 277 */ 278 int 279 mappedread(znode_t *zp, int nbytes, zfs_uio_t *uio) 280 { 281 struct inode *ip = ZTOI(zp); 282 struct address_space *mp = ip->i_mapping; 283 struct page *pp; 284 int64_t start, off; 285 uint64_t bytes; 286 int len = nbytes; 287 int error = 0; 288 void *pb; 289 290 start = uio->uio_loffset; 291 off = start & (PAGE_SIZE-1); 292 for (start &= PAGE_MASK; len > 0; start += PAGE_SIZE) { 293 bytes = MIN(PAGE_SIZE - off, len); 294 295 pp = find_lock_page(mp, start >> PAGE_SHIFT); 296 if (pp) { 297 ASSERT(PageUptodate(pp)); 298 unlock_page(pp); 299 300 pb = kmap(pp); 301 error = zfs_uiomove(pb + off, bytes, UIO_READ, uio); 302 kunmap(pp); 303 304 if (mapping_writably_mapped(mp)) 305 flush_dcache_page(pp); 306 307 mark_page_accessed(pp); 308 put_page(pp); 309 } else { 310 error = dmu_read_uio_dbuf(sa_get_db(zp->z_sa_hdl), 311 uio, bytes); 312 } 313 314 len -= bytes; 315 off = 0; 316 if (error) 317 break; 318 } 319 return (error); 320 } 321 #endif /* _KERNEL */ 322 323 unsigned long zfs_delete_blocks = DMU_MAX_DELETEBLKCNT; 324 325 /* 326 * Write the bytes to a file. 327 * 328 * IN: zp - znode of file to be written to 329 * data - bytes to write 330 * len - number of bytes to write 331 * pos - offset to start writing at 332 * 333 * OUT: resid - remaining bytes to write 334 * 335 * RETURN: 0 if success 336 * positive error code if failure. EIO is returned 337 * for a short write when residp isn't provided. 338 * 339 * Timestamps: 340 * zp - ctime|mtime updated if byte count > 0 341 */ 342 int 343 zfs_write_simple(znode_t *zp, const void *data, size_t len, 344 loff_t pos, size_t *residp) 345 { 346 fstrans_cookie_t cookie; 347 int error; 348 349 struct iovec iov; 350 iov.iov_base = (void *)data; 351 iov.iov_len = len; 352 353 zfs_uio_t uio; 354 zfs_uio_iovec_init(&uio, &iov, 1, pos, UIO_SYSSPACE, len, 0); 355 356 cookie = spl_fstrans_mark(); 357 error = zfs_write(zp, &uio, 0, kcred); 358 spl_fstrans_unmark(cookie); 359 360 if (error == 0) { 361 if (residp != NULL) 362 *residp = zfs_uio_resid(&uio); 363 else if (zfs_uio_resid(&uio) != 0) 364 error = SET_ERROR(EIO); 365 } 366 367 return (error); 368 } 369 370 void 371 zfs_zrele_async(znode_t *zp) 372 { 373 struct inode *ip = ZTOI(zp); 374 objset_t *os = ITOZSB(ip)->z_os; 375 376 ASSERT(atomic_read(&ip->i_count) > 0); 377 ASSERT(os != NULL); 378 379 /* 380 * If decrementing the count would put us at 0, we can't do it inline 381 * here, because that would be synchronous. Instead, dispatch an iput 382 * to run later. 383 * 384 * For more information on the dangers of a synchronous iput, see the 385 * header comment of this file. 386 */ 387 if (!atomic_add_unless(&ip->i_count, -1, 1)) { 388 VERIFY(taskq_dispatch(dsl_pool_zrele_taskq(dmu_objset_pool(os)), 389 (task_func_t *)iput, ip, TQ_SLEEP) != TASKQID_INVALID); 390 } 391 } 392 393 394 /* 395 * Lookup an entry in a directory, or an extended attribute directory. 396 * If it exists, return a held inode reference for it. 397 * 398 * IN: zdp - znode of directory to search. 399 * nm - name of entry to lookup. 400 * flags - LOOKUP_XATTR set if looking for an attribute. 401 * cr - credentials of caller. 402 * direntflags - directory lookup flags 403 * realpnp - returned pathname. 404 * 405 * OUT: zpp - znode of located entry, NULL if not found. 406 * 407 * RETURN: 0 on success, error code on failure. 408 * 409 * Timestamps: 410 * NA 411 */ 412 /* ARGSUSED */ 413 int 414 zfs_lookup(znode_t *zdp, char *nm, znode_t **zpp, int flags, cred_t *cr, 415 int *direntflags, pathname_t *realpnp) 416 { 417 zfsvfs_t *zfsvfs = ZTOZSB(zdp); 418 int error = 0; 419 420 /* 421 * Fast path lookup, however we must skip DNLC lookup 422 * for case folding or normalizing lookups because the 423 * DNLC code only stores the passed in name. This means 424 * creating 'a' and removing 'A' on a case insensitive 425 * file system would work, but DNLC still thinks 'a' 426 * exists and won't let you create it again on the next 427 * pass through fast path. 428 */ 429 if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 430 431 if (!S_ISDIR(ZTOI(zdp)->i_mode)) { 432 return (SET_ERROR(ENOTDIR)); 433 } else if (zdp->z_sa_hdl == NULL) { 434 return (SET_ERROR(EIO)); 435 } 436 437 if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 438 error = zfs_fastaccesschk_execute(zdp, cr); 439 if (!error) { 440 *zpp = zdp; 441 zhold(*zpp); 442 return (0); 443 } 444 return (error); 445 } 446 } 447 448 ZFS_ENTER(zfsvfs); 449 ZFS_VERIFY_ZP(zdp); 450 451 *zpp = NULL; 452 453 if (flags & LOOKUP_XATTR) { 454 /* 455 * We don't allow recursive attributes.. 456 * Maybe someday we will. 457 */ 458 if (zdp->z_pflags & ZFS_XATTR) { 459 ZFS_EXIT(zfsvfs); 460 return (SET_ERROR(EINVAL)); 461 } 462 463 if ((error = zfs_get_xattrdir(zdp, zpp, cr, flags))) { 464 ZFS_EXIT(zfsvfs); 465 return (error); 466 } 467 468 /* 469 * Do we have permission to get into attribute directory? 470 */ 471 472 if ((error = zfs_zaccess(*zpp, ACE_EXECUTE, 0, 473 B_FALSE, cr))) { 474 zrele(*zpp); 475 *zpp = NULL; 476 } 477 478 ZFS_EXIT(zfsvfs); 479 return (error); 480 } 481 482 if (!S_ISDIR(ZTOI(zdp)->i_mode)) { 483 ZFS_EXIT(zfsvfs); 484 return (SET_ERROR(ENOTDIR)); 485 } 486 487 /* 488 * Check accessibility of directory. 489 */ 490 491 if ((error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr))) { 492 ZFS_EXIT(zfsvfs); 493 return (error); 494 } 495 496 if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 497 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 498 ZFS_EXIT(zfsvfs); 499 return (SET_ERROR(EILSEQ)); 500 } 501 502 error = zfs_dirlook(zdp, nm, zpp, flags, direntflags, realpnp); 503 if ((error == 0) && (*zpp)) 504 zfs_znode_update_vfs(*zpp); 505 506 ZFS_EXIT(zfsvfs); 507 return (error); 508 } 509 510 /* 511 * Attempt to create a new entry in a directory. If the entry 512 * already exists, truncate the file if permissible, else return 513 * an error. Return the ip of the created or trunc'd file. 514 * 515 * IN: dzp - znode of directory to put new file entry in. 516 * name - name of new file entry. 517 * vap - attributes of new file. 518 * excl - flag indicating exclusive or non-exclusive mode. 519 * mode - mode to open file with. 520 * cr - credentials of caller. 521 * flag - file flag. 522 * vsecp - ACL to be set 523 * 524 * OUT: zpp - znode of created or trunc'd entry. 525 * 526 * RETURN: 0 on success, error code on failure. 527 * 528 * Timestamps: 529 * dzp - ctime|mtime updated if new entry created 530 * zp - ctime|mtime always, atime if new 531 */ 532 533 /* ARGSUSED */ 534 int 535 zfs_create(znode_t *dzp, char *name, vattr_t *vap, int excl, 536 int mode, znode_t **zpp, cred_t *cr, int flag, vsecattr_t *vsecp) 537 { 538 znode_t *zp; 539 zfsvfs_t *zfsvfs = ZTOZSB(dzp); 540 zilog_t *zilog; 541 objset_t *os; 542 zfs_dirlock_t *dl; 543 dmu_tx_t *tx; 544 int error; 545 uid_t uid; 546 gid_t gid; 547 zfs_acl_ids_t acl_ids; 548 boolean_t fuid_dirtied; 549 boolean_t have_acl = B_FALSE; 550 boolean_t waited = B_FALSE; 551 552 /* 553 * If we have an ephemeral id, ACL, or XVATTR then 554 * make sure file system is at proper version 555 */ 556 557 gid = crgetgid(cr); 558 uid = crgetuid(cr); 559 560 if (zfsvfs->z_use_fuids == B_FALSE && 561 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 562 return (SET_ERROR(EINVAL)); 563 564 if (name == NULL) 565 return (SET_ERROR(EINVAL)); 566 567 ZFS_ENTER(zfsvfs); 568 ZFS_VERIFY_ZP(dzp); 569 os = zfsvfs->z_os; 570 zilog = zfsvfs->z_log; 571 572 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 573 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 574 ZFS_EXIT(zfsvfs); 575 return (SET_ERROR(EILSEQ)); 576 } 577 578 if (vap->va_mask & ATTR_XVATTR) { 579 if ((error = secpolicy_xvattr((xvattr_t *)vap, 580 crgetuid(cr), cr, vap->va_mode)) != 0) { 581 ZFS_EXIT(zfsvfs); 582 return (error); 583 } 584 } 585 586 top: 587 *zpp = NULL; 588 if (*name == '\0') { 589 /* 590 * Null component name refers to the directory itself. 591 */ 592 zhold(dzp); 593 zp = dzp; 594 dl = NULL; 595 error = 0; 596 } else { 597 /* possible igrab(zp) */ 598 int zflg = 0; 599 600 if (flag & FIGNORECASE) 601 zflg |= ZCILOOK; 602 603 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 604 NULL, NULL); 605 if (error) { 606 if (have_acl) 607 zfs_acl_ids_free(&acl_ids); 608 if (strcmp(name, "..") == 0) 609 error = SET_ERROR(EISDIR); 610 ZFS_EXIT(zfsvfs); 611 return (error); 612 } 613 } 614 615 if (zp == NULL) { 616 uint64_t txtype; 617 uint64_t projid = ZFS_DEFAULT_PROJID; 618 619 /* 620 * Create a new file object and update the directory 621 * to reference it. 622 */ 623 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { 624 if (have_acl) 625 zfs_acl_ids_free(&acl_ids); 626 goto out; 627 } 628 629 /* 630 * We only support the creation of regular files in 631 * extended attribute directories. 632 */ 633 634 if ((dzp->z_pflags & ZFS_XATTR) && !S_ISREG(vap->va_mode)) { 635 if (have_acl) 636 zfs_acl_ids_free(&acl_ids); 637 error = SET_ERROR(EINVAL); 638 goto out; 639 } 640 641 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 642 cr, vsecp, &acl_ids)) != 0) 643 goto out; 644 have_acl = B_TRUE; 645 646 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) 647 projid = zfs_inherit_projid(dzp); 648 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) { 649 zfs_acl_ids_free(&acl_ids); 650 error = SET_ERROR(EDQUOT); 651 goto out; 652 } 653 654 tx = dmu_tx_create(os); 655 656 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 657 ZFS_SA_BASE_ATTR_SIZE); 658 659 fuid_dirtied = zfsvfs->z_fuid_dirty; 660 if (fuid_dirtied) 661 zfs_fuid_txhold(zfsvfs, tx); 662 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 663 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 664 if (!zfsvfs->z_use_sa && 665 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 666 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 667 0, acl_ids.z_aclp->z_acl_bytes); 668 } 669 670 error = dmu_tx_assign(tx, 671 (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 672 if (error) { 673 zfs_dirent_unlock(dl); 674 if (error == ERESTART) { 675 waited = B_TRUE; 676 dmu_tx_wait(tx); 677 dmu_tx_abort(tx); 678 goto top; 679 } 680 zfs_acl_ids_free(&acl_ids); 681 dmu_tx_abort(tx); 682 ZFS_EXIT(zfsvfs); 683 return (error); 684 } 685 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 686 687 error = zfs_link_create(dl, zp, tx, ZNEW); 688 if (error != 0) { 689 /* 690 * Since, we failed to add the directory entry for it, 691 * delete the newly created dnode. 692 */ 693 zfs_znode_delete(zp, tx); 694 remove_inode_hash(ZTOI(zp)); 695 zfs_acl_ids_free(&acl_ids); 696 dmu_tx_commit(tx); 697 goto out; 698 } 699 700 if (fuid_dirtied) 701 zfs_fuid_sync(zfsvfs, tx); 702 703 txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 704 if (flag & FIGNORECASE) 705 txtype |= TX_CI; 706 zfs_log_create(zilog, tx, txtype, dzp, zp, name, 707 vsecp, acl_ids.z_fuidp, vap); 708 zfs_acl_ids_free(&acl_ids); 709 dmu_tx_commit(tx); 710 } else { 711 int aflags = (flag & O_APPEND) ? V_APPEND : 0; 712 713 if (have_acl) 714 zfs_acl_ids_free(&acl_ids); 715 have_acl = B_FALSE; 716 717 /* 718 * A directory entry already exists for this name. 719 */ 720 /* 721 * Can't truncate an existing file if in exclusive mode. 722 */ 723 if (excl) { 724 error = SET_ERROR(EEXIST); 725 goto out; 726 } 727 /* 728 * Can't open a directory for writing. 729 */ 730 if (S_ISDIR(ZTOI(zp)->i_mode)) { 731 error = SET_ERROR(EISDIR); 732 goto out; 733 } 734 /* 735 * Verify requested access to file. 736 */ 737 if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 738 goto out; 739 } 740 741 mutex_enter(&dzp->z_lock); 742 dzp->z_seq++; 743 mutex_exit(&dzp->z_lock); 744 745 /* 746 * Truncate regular files if requested. 747 */ 748 if (S_ISREG(ZTOI(zp)->i_mode) && 749 (vap->va_mask & ATTR_SIZE) && (vap->va_size == 0)) { 750 /* we can't hold any locks when calling zfs_freesp() */ 751 if (dl) { 752 zfs_dirent_unlock(dl); 753 dl = NULL; 754 } 755 error = zfs_freesp(zp, 0, 0, mode, TRUE); 756 } 757 } 758 out: 759 760 if (dl) 761 zfs_dirent_unlock(dl); 762 763 if (error) { 764 if (zp) 765 zrele(zp); 766 } else { 767 zfs_znode_update_vfs(dzp); 768 zfs_znode_update_vfs(zp); 769 *zpp = zp; 770 } 771 772 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 773 zil_commit(zilog, 0); 774 775 ZFS_EXIT(zfsvfs); 776 return (error); 777 } 778 779 /* ARGSUSED */ 780 int 781 zfs_tmpfile(struct inode *dip, vattr_t *vap, int excl, 782 int mode, struct inode **ipp, cred_t *cr, int flag, vsecattr_t *vsecp) 783 { 784 znode_t *zp = NULL, *dzp = ITOZ(dip); 785 zfsvfs_t *zfsvfs = ITOZSB(dip); 786 objset_t *os; 787 dmu_tx_t *tx; 788 int error; 789 uid_t uid; 790 gid_t gid; 791 zfs_acl_ids_t acl_ids; 792 uint64_t projid = ZFS_DEFAULT_PROJID; 793 boolean_t fuid_dirtied; 794 boolean_t have_acl = B_FALSE; 795 boolean_t waited = B_FALSE; 796 797 /* 798 * If we have an ephemeral id, ACL, or XVATTR then 799 * make sure file system is at proper version 800 */ 801 802 gid = crgetgid(cr); 803 uid = crgetuid(cr); 804 805 if (zfsvfs->z_use_fuids == B_FALSE && 806 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 807 return (SET_ERROR(EINVAL)); 808 809 ZFS_ENTER(zfsvfs); 810 ZFS_VERIFY_ZP(dzp); 811 os = zfsvfs->z_os; 812 813 if (vap->va_mask & ATTR_XVATTR) { 814 if ((error = secpolicy_xvattr((xvattr_t *)vap, 815 crgetuid(cr), cr, vap->va_mode)) != 0) { 816 ZFS_EXIT(zfsvfs); 817 return (error); 818 } 819 } 820 821 top: 822 *ipp = NULL; 823 824 /* 825 * Create a new file object and update the directory 826 * to reference it. 827 */ 828 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { 829 if (have_acl) 830 zfs_acl_ids_free(&acl_ids); 831 goto out; 832 } 833 834 if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 835 cr, vsecp, &acl_ids)) != 0) 836 goto out; 837 have_acl = B_TRUE; 838 839 if (S_ISREG(vap->va_mode) || S_ISDIR(vap->va_mode)) 840 projid = zfs_inherit_projid(dzp); 841 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, projid)) { 842 zfs_acl_ids_free(&acl_ids); 843 error = SET_ERROR(EDQUOT); 844 goto out; 845 } 846 847 tx = dmu_tx_create(os); 848 849 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 850 ZFS_SA_BASE_ATTR_SIZE); 851 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 852 853 fuid_dirtied = zfsvfs->z_fuid_dirty; 854 if (fuid_dirtied) 855 zfs_fuid_txhold(zfsvfs, tx); 856 if (!zfsvfs->z_use_sa && 857 acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 858 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 859 0, acl_ids.z_aclp->z_acl_bytes); 860 } 861 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 862 if (error) { 863 if (error == ERESTART) { 864 waited = B_TRUE; 865 dmu_tx_wait(tx); 866 dmu_tx_abort(tx); 867 goto top; 868 } 869 zfs_acl_ids_free(&acl_ids); 870 dmu_tx_abort(tx); 871 ZFS_EXIT(zfsvfs); 872 return (error); 873 } 874 zfs_mknode(dzp, vap, tx, cr, IS_TMPFILE, &zp, &acl_ids); 875 876 if (fuid_dirtied) 877 zfs_fuid_sync(zfsvfs, tx); 878 879 /* Add to unlinked set */ 880 zp->z_unlinked = B_TRUE; 881 zfs_unlinked_add(zp, tx); 882 zfs_acl_ids_free(&acl_ids); 883 dmu_tx_commit(tx); 884 out: 885 886 if (error) { 887 if (zp) 888 zrele(zp); 889 } else { 890 zfs_znode_update_vfs(dzp); 891 zfs_znode_update_vfs(zp); 892 *ipp = ZTOI(zp); 893 } 894 895 ZFS_EXIT(zfsvfs); 896 return (error); 897 } 898 899 /* 900 * Remove an entry from a directory. 901 * 902 * IN: dzp - znode of directory to remove entry from. 903 * name - name of entry to remove. 904 * cr - credentials of caller. 905 * flags - case flags. 906 * 907 * RETURN: 0 if success 908 * error code if failure 909 * 910 * Timestamps: 911 * dzp - ctime|mtime 912 * ip - ctime (if nlink > 0) 913 */ 914 915 uint64_t null_xattr = 0; 916 917 /*ARGSUSED*/ 918 int 919 zfs_remove(znode_t *dzp, char *name, cred_t *cr, int flags) 920 { 921 znode_t *zp; 922 znode_t *xzp; 923 zfsvfs_t *zfsvfs = ZTOZSB(dzp); 924 zilog_t *zilog; 925 uint64_t acl_obj, xattr_obj; 926 uint64_t xattr_obj_unlinked = 0; 927 uint64_t obj = 0; 928 uint64_t links; 929 zfs_dirlock_t *dl; 930 dmu_tx_t *tx; 931 boolean_t may_delete_now, delete_now = FALSE; 932 boolean_t unlinked, toobig = FALSE; 933 uint64_t txtype; 934 pathname_t *realnmp = NULL; 935 pathname_t realnm; 936 int error; 937 int zflg = ZEXISTS; 938 boolean_t waited = B_FALSE; 939 940 if (name == NULL) 941 return (SET_ERROR(EINVAL)); 942 943 ZFS_ENTER(zfsvfs); 944 ZFS_VERIFY_ZP(dzp); 945 zilog = zfsvfs->z_log; 946 947 if (flags & FIGNORECASE) { 948 zflg |= ZCILOOK; 949 pn_alloc(&realnm); 950 realnmp = &realnm; 951 } 952 953 top: 954 xattr_obj = 0; 955 xzp = NULL; 956 /* 957 * Attempt to lock directory; fail if entry doesn't exist. 958 */ 959 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 960 NULL, realnmp))) { 961 if (realnmp) 962 pn_free(realnmp); 963 ZFS_EXIT(zfsvfs); 964 return (error); 965 } 966 967 if ((error = zfs_zaccess_delete(dzp, zp, cr))) { 968 goto out; 969 } 970 971 /* 972 * Need to use rmdir for removing directories. 973 */ 974 if (S_ISDIR(ZTOI(zp)->i_mode)) { 975 error = SET_ERROR(EPERM); 976 goto out; 977 } 978 979 mutex_enter(&zp->z_lock); 980 may_delete_now = atomic_read(&ZTOI(zp)->i_count) == 1 && 981 !(zp->z_is_mapped); 982 mutex_exit(&zp->z_lock); 983 984 /* 985 * We may delete the znode now, or we may put it in the unlinked set; 986 * it depends on whether we're the last link, and on whether there are 987 * other holds on the inode. So we dmu_tx_hold() the right things to 988 * allow for either case. 989 */ 990 obj = zp->z_id; 991 tx = dmu_tx_create(zfsvfs->z_os); 992 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 993 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 994 zfs_sa_upgrade_txholds(tx, zp); 995 zfs_sa_upgrade_txholds(tx, dzp); 996 if (may_delete_now) { 997 toobig = zp->z_size > zp->z_blksz * zfs_delete_blocks; 998 /* if the file is too big, only hold_free a token amount */ 999 dmu_tx_hold_free(tx, zp->z_id, 0, 1000 (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 1001 } 1002 1003 /* are there any extended attributes? */ 1004 error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1005 &xattr_obj, sizeof (xattr_obj)); 1006 if (error == 0 && xattr_obj) { 1007 error = zfs_zget(zfsvfs, xattr_obj, &xzp); 1008 ASSERT0(error); 1009 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 1010 dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1011 } 1012 1013 mutex_enter(&zp->z_lock); 1014 if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1015 dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1016 mutex_exit(&zp->z_lock); 1017 1018 /* charge as an update -- would be nice not to charge at all */ 1019 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1020 1021 /* 1022 * Mark this transaction as typically resulting in a net free of space 1023 */ 1024 dmu_tx_mark_netfree(tx); 1025 1026 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 1027 if (error) { 1028 zfs_dirent_unlock(dl); 1029 if (error == ERESTART) { 1030 waited = B_TRUE; 1031 dmu_tx_wait(tx); 1032 dmu_tx_abort(tx); 1033 zrele(zp); 1034 if (xzp) 1035 zrele(xzp); 1036 goto top; 1037 } 1038 if (realnmp) 1039 pn_free(realnmp); 1040 dmu_tx_abort(tx); 1041 zrele(zp); 1042 if (xzp) 1043 zrele(xzp); 1044 ZFS_EXIT(zfsvfs); 1045 return (error); 1046 } 1047 1048 /* 1049 * Remove the directory entry. 1050 */ 1051 error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1052 1053 if (error) { 1054 dmu_tx_commit(tx); 1055 goto out; 1056 } 1057 1058 if (unlinked) { 1059 /* 1060 * Hold z_lock so that we can make sure that the ACL obj 1061 * hasn't changed. Could have been deleted due to 1062 * zfs_sa_upgrade(). 1063 */ 1064 mutex_enter(&zp->z_lock); 1065 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 1066 &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 1067 delete_now = may_delete_now && !toobig && 1068 atomic_read(&ZTOI(zp)->i_count) == 1 && 1069 !(zp->z_is_mapped) && xattr_obj == xattr_obj_unlinked && 1070 zfs_external_acl(zp) == acl_obj; 1071 } 1072 1073 if (delete_now) { 1074 if (xattr_obj_unlinked) { 1075 ASSERT3U(ZTOI(xzp)->i_nlink, ==, 2); 1076 mutex_enter(&xzp->z_lock); 1077 xzp->z_unlinked = B_TRUE; 1078 clear_nlink(ZTOI(xzp)); 1079 links = 0; 1080 error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 1081 &links, sizeof (links), tx); 1082 ASSERT3U(error, ==, 0); 1083 mutex_exit(&xzp->z_lock); 1084 zfs_unlinked_add(xzp, tx); 1085 1086 if (zp->z_is_sa) 1087 error = sa_remove(zp->z_sa_hdl, 1088 SA_ZPL_XATTR(zfsvfs), tx); 1089 else 1090 error = sa_update(zp->z_sa_hdl, 1091 SA_ZPL_XATTR(zfsvfs), &null_xattr, 1092 sizeof (uint64_t), tx); 1093 ASSERT0(error); 1094 } 1095 /* 1096 * Add to the unlinked set because a new reference could be 1097 * taken concurrently resulting in a deferred destruction. 1098 */ 1099 zfs_unlinked_add(zp, tx); 1100 mutex_exit(&zp->z_lock); 1101 } else if (unlinked) { 1102 mutex_exit(&zp->z_lock); 1103 zfs_unlinked_add(zp, tx); 1104 } 1105 1106 txtype = TX_REMOVE; 1107 if (flags & FIGNORECASE) 1108 txtype |= TX_CI; 1109 zfs_log_remove(zilog, tx, txtype, dzp, name, obj, unlinked); 1110 1111 dmu_tx_commit(tx); 1112 out: 1113 if (realnmp) 1114 pn_free(realnmp); 1115 1116 zfs_dirent_unlock(dl); 1117 zfs_znode_update_vfs(dzp); 1118 zfs_znode_update_vfs(zp); 1119 1120 if (delete_now) 1121 zrele(zp); 1122 else 1123 zfs_zrele_async(zp); 1124 1125 if (xzp) { 1126 zfs_znode_update_vfs(xzp); 1127 zfs_zrele_async(xzp); 1128 } 1129 1130 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1131 zil_commit(zilog, 0); 1132 1133 ZFS_EXIT(zfsvfs); 1134 return (error); 1135 } 1136 1137 /* 1138 * Create a new directory and insert it into dzp using the name 1139 * provided. Return a pointer to the inserted directory. 1140 * 1141 * IN: dzp - znode of directory to add subdir to. 1142 * dirname - name of new directory. 1143 * vap - attributes of new directory. 1144 * cr - credentials of caller. 1145 * flags - case flags. 1146 * vsecp - ACL to be set 1147 * 1148 * OUT: zpp - znode of created directory. 1149 * 1150 * RETURN: 0 if success 1151 * error code if failure 1152 * 1153 * Timestamps: 1154 * dzp - ctime|mtime updated 1155 * zpp - ctime|mtime|atime updated 1156 */ 1157 /*ARGSUSED*/ 1158 int 1159 zfs_mkdir(znode_t *dzp, char *dirname, vattr_t *vap, znode_t **zpp, 1160 cred_t *cr, int flags, vsecattr_t *vsecp) 1161 { 1162 znode_t *zp; 1163 zfsvfs_t *zfsvfs = ZTOZSB(dzp); 1164 zilog_t *zilog; 1165 zfs_dirlock_t *dl; 1166 uint64_t txtype; 1167 dmu_tx_t *tx; 1168 int error; 1169 int zf = ZNEW; 1170 uid_t uid; 1171 gid_t gid = crgetgid(cr); 1172 zfs_acl_ids_t acl_ids; 1173 boolean_t fuid_dirtied; 1174 boolean_t waited = B_FALSE; 1175 1176 ASSERT(S_ISDIR(vap->va_mode)); 1177 1178 /* 1179 * If we have an ephemeral id, ACL, or XVATTR then 1180 * make sure file system is at proper version 1181 */ 1182 1183 uid = crgetuid(cr); 1184 if (zfsvfs->z_use_fuids == B_FALSE && 1185 (vsecp || IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 1186 return (SET_ERROR(EINVAL)); 1187 1188 if (dirname == NULL) 1189 return (SET_ERROR(EINVAL)); 1190 1191 ZFS_ENTER(zfsvfs); 1192 ZFS_VERIFY_ZP(dzp); 1193 zilog = zfsvfs->z_log; 1194 1195 if (dzp->z_pflags & ZFS_XATTR) { 1196 ZFS_EXIT(zfsvfs); 1197 return (SET_ERROR(EINVAL)); 1198 } 1199 1200 if (zfsvfs->z_utf8 && u8_validate(dirname, 1201 strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 1202 ZFS_EXIT(zfsvfs); 1203 return (SET_ERROR(EILSEQ)); 1204 } 1205 if (flags & FIGNORECASE) 1206 zf |= ZCILOOK; 1207 1208 if (vap->va_mask & ATTR_XVATTR) { 1209 if ((error = secpolicy_xvattr((xvattr_t *)vap, 1210 crgetuid(cr), cr, vap->va_mode)) != 0) { 1211 ZFS_EXIT(zfsvfs); 1212 return (error); 1213 } 1214 } 1215 1216 if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 1217 vsecp, &acl_ids)) != 0) { 1218 ZFS_EXIT(zfsvfs); 1219 return (error); 1220 } 1221 /* 1222 * First make sure the new directory doesn't exist. 1223 * 1224 * Existence is checked first to make sure we don't return 1225 * EACCES instead of EEXIST which can cause some applications 1226 * to fail. 1227 */ 1228 top: 1229 *zpp = NULL; 1230 1231 if ((error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 1232 NULL, NULL))) { 1233 zfs_acl_ids_free(&acl_ids); 1234 ZFS_EXIT(zfsvfs); 1235 return (error); 1236 } 1237 1238 if ((error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr))) { 1239 zfs_acl_ids_free(&acl_ids); 1240 zfs_dirent_unlock(dl); 1241 ZFS_EXIT(zfsvfs); 1242 return (error); 1243 } 1244 1245 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, zfs_inherit_projid(dzp))) { 1246 zfs_acl_ids_free(&acl_ids); 1247 zfs_dirent_unlock(dl); 1248 ZFS_EXIT(zfsvfs); 1249 return (SET_ERROR(EDQUOT)); 1250 } 1251 1252 /* 1253 * Add a new entry to the directory. 1254 */ 1255 tx = dmu_tx_create(zfsvfs->z_os); 1256 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 1257 dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1258 fuid_dirtied = zfsvfs->z_fuid_dirty; 1259 if (fuid_dirtied) 1260 zfs_fuid_txhold(zfsvfs, tx); 1261 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1262 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 1263 acl_ids.z_aclp->z_acl_bytes); 1264 } 1265 1266 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 1267 ZFS_SA_BASE_ATTR_SIZE); 1268 1269 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 1270 if (error) { 1271 zfs_dirent_unlock(dl); 1272 if (error == ERESTART) { 1273 waited = B_TRUE; 1274 dmu_tx_wait(tx); 1275 dmu_tx_abort(tx); 1276 goto top; 1277 } 1278 zfs_acl_ids_free(&acl_ids); 1279 dmu_tx_abort(tx); 1280 ZFS_EXIT(zfsvfs); 1281 return (error); 1282 } 1283 1284 /* 1285 * Create new node. 1286 */ 1287 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 1288 1289 /* 1290 * Now put new name in parent dir. 1291 */ 1292 error = zfs_link_create(dl, zp, tx, ZNEW); 1293 if (error != 0) { 1294 zfs_znode_delete(zp, tx); 1295 remove_inode_hash(ZTOI(zp)); 1296 goto out; 1297 } 1298 1299 if (fuid_dirtied) 1300 zfs_fuid_sync(zfsvfs, tx); 1301 1302 *zpp = zp; 1303 1304 txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 1305 if (flags & FIGNORECASE) 1306 txtype |= TX_CI; 1307 zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 1308 acl_ids.z_fuidp, vap); 1309 1310 out: 1311 zfs_acl_ids_free(&acl_ids); 1312 1313 dmu_tx_commit(tx); 1314 1315 zfs_dirent_unlock(dl); 1316 1317 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1318 zil_commit(zilog, 0); 1319 1320 if (error != 0) { 1321 zrele(zp); 1322 } else { 1323 zfs_znode_update_vfs(dzp); 1324 zfs_znode_update_vfs(zp); 1325 } 1326 ZFS_EXIT(zfsvfs); 1327 return (error); 1328 } 1329 1330 /* 1331 * Remove a directory subdir entry. If the current working 1332 * directory is the same as the subdir to be removed, the 1333 * remove will fail. 1334 * 1335 * IN: dzp - znode of directory to remove from. 1336 * name - name of directory to be removed. 1337 * cwd - inode of current working directory. 1338 * cr - credentials of caller. 1339 * flags - case flags 1340 * 1341 * RETURN: 0 on success, error code on failure. 1342 * 1343 * Timestamps: 1344 * dzp - ctime|mtime updated 1345 */ 1346 /*ARGSUSED*/ 1347 int 1348 zfs_rmdir(znode_t *dzp, char *name, znode_t *cwd, cred_t *cr, 1349 int flags) 1350 { 1351 znode_t *zp; 1352 zfsvfs_t *zfsvfs = ZTOZSB(dzp); 1353 zilog_t *zilog; 1354 zfs_dirlock_t *dl; 1355 dmu_tx_t *tx; 1356 int error; 1357 int zflg = ZEXISTS; 1358 boolean_t waited = B_FALSE; 1359 1360 if (name == NULL) 1361 return (SET_ERROR(EINVAL)); 1362 1363 ZFS_ENTER(zfsvfs); 1364 ZFS_VERIFY_ZP(dzp); 1365 zilog = zfsvfs->z_log; 1366 1367 if (flags & FIGNORECASE) 1368 zflg |= ZCILOOK; 1369 top: 1370 zp = NULL; 1371 1372 /* 1373 * Attempt to lock directory; fail if entry doesn't exist. 1374 */ 1375 if ((error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 1376 NULL, NULL))) { 1377 ZFS_EXIT(zfsvfs); 1378 return (error); 1379 } 1380 1381 if ((error = zfs_zaccess_delete(dzp, zp, cr))) { 1382 goto out; 1383 } 1384 1385 if (!S_ISDIR(ZTOI(zp)->i_mode)) { 1386 error = SET_ERROR(ENOTDIR); 1387 goto out; 1388 } 1389 1390 if (zp == cwd) { 1391 error = SET_ERROR(EINVAL); 1392 goto out; 1393 } 1394 1395 /* 1396 * Grab a lock on the directory to make sure that no one is 1397 * trying to add (or lookup) entries while we are removing it. 1398 */ 1399 rw_enter(&zp->z_name_lock, RW_WRITER); 1400 1401 /* 1402 * Grab a lock on the parent pointer to make sure we play well 1403 * with the treewalk and directory rename code. 1404 */ 1405 rw_enter(&zp->z_parent_lock, RW_WRITER); 1406 1407 tx = dmu_tx_create(zfsvfs->z_os); 1408 dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1409 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1410 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1411 zfs_sa_upgrade_txholds(tx, zp); 1412 zfs_sa_upgrade_txholds(tx, dzp); 1413 dmu_tx_mark_netfree(tx); 1414 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 1415 if (error) { 1416 rw_exit(&zp->z_parent_lock); 1417 rw_exit(&zp->z_name_lock); 1418 zfs_dirent_unlock(dl); 1419 if (error == ERESTART) { 1420 waited = B_TRUE; 1421 dmu_tx_wait(tx); 1422 dmu_tx_abort(tx); 1423 zrele(zp); 1424 goto top; 1425 } 1426 dmu_tx_abort(tx); 1427 zrele(zp); 1428 ZFS_EXIT(zfsvfs); 1429 return (error); 1430 } 1431 1432 error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 1433 1434 if (error == 0) { 1435 uint64_t txtype = TX_RMDIR; 1436 if (flags & FIGNORECASE) 1437 txtype |= TX_CI; 1438 zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT, 1439 B_FALSE); 1440 } 1441 1442 dmu_tx_commit(tx); 1443 1444 rw_exit(&zp->z_parent_lock); 1445 rw_exit(&zp->z_name_lock); 1446 out: 1447 zfs_dirent_unlock(dl); 1448 1449 zfs_znode_update_vfs(dzp); 1450 zfs_znode_update_vfs(zp); 1451 zrele(zp); 1452 1453 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1454 zil_commit(zilog, 0); 1455 1456 ZFS_EXIT(zfsvfs); 1457 return (error); 1458 } 1459 1460 /* 1461 * Read directory entries from the given directory cursor position and emit 1462 * name and position for each entry. 1463 * 1464 * IN: ip - inode of directory to read. 1465 * ctx - directory entry context. 1466 * cr - credentials of caller. 1467 * 1468 * RETURN: 0 if success 1469 * error code if failure 1470 * 1471 * Timestamps: 1472 * ip - atime updated 1473 * 1474 * Note that the low 4 bits of the cookie returned by zap is always zero. 1475 * This allows us to use the low range for "special" directory entries: 1476 * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1477 * we use the offset 2 for the '.zfs' directory. 1478 */ 1479 /* ARGSUSED */ 1480 int 1481 zfs_readdir(struct inode *ip, zpl_dir_context_t *ctx, cred_t *cr) 1482 { 1483 znode_t *zp = ITOZ(ip); 1484 zfsvfs_t *zfsvfs = ITOZSB(ip); 1485 objset_t *os; 1486 zap_cursor_t zc; 1487 zap_attribute_t zap; 1488 int error; 1489 uint8_t prefetch; 1490 uint8_t type; 1491 int done = 0; 1492 uint64_t parent; 1493 uint64_t offset; /* must be unsigned; checks for < 1 */ 1494 1495 ZFS_ENTER(zfsvfs); 1496 ZFS_VERIFY_ZP(zp); 1497 1498 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 1499 &parent, sizeof (parent))) != 0) 1500 goto out; 1501 1502 /* 1503 * Quit if directory has been removed (posix) 1504 */ 1505 if (zp->z_unlinked) 1506 goto out; 1507 1508 error = 0; 1509 os = zfsvfs->z_os; 1510 offset = ctx->pos; 1511 prefetch = zp->z_zn_prefetch; 1512 1513 /* 1514 * Initialize the iterator cursor. 1515 */ 1516 if (offset <= 3) { 1517 /* 1518 * Start iteration from the beginning of the directory. 1519 */ 1520 zap_cursor_init(&zc, os, zp->z_id); 1521 } else { 1522 /* 1523 * The offset is a serialized cursor. 1524 */ 1525 zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1526 } 1527 1528 /* 1529 * Transform to file-system independent format 1530 */ 1531 while (!done) { 1532 uint64_t objnum; 1533 /* 1534 * Special case `.', `..', and `.zfs'. 1535 */ 1536 if (offset == 0) { 1537 (void) strcpy(zap.za_name, "."); 1538 zap.za_normalization_conflict = 0; 1539 objnum = zp->z_id; 1540 type = DT_DIR; 1541 } else if (offset == 1) { 1542 (void) strcpy(zap.za_name, ".."); 1543 zap.za_normalization_conflict = 0; 1544 objnum = parent; 1545 type = DT_DIR; 1546 } else if (offset == 2 && zfs_show_ctldir(zp)) { 1547 (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1548 zap.za_normalization_conflict = 0; 1549 objnum = ZFSCTL_INO_ROOT; 1550 type = DT_DIR; 1551 } else { 1552 /* 1553 * Grab next entry. 1554 */ 1555 if ((error = zap_cursor_retrieve(&zc, &zap))) { 1556 if (error == ENOENT) 1557 break; 1558 else 1559 goto update; 1560 } 1561 1562 /* 1563 * Allow multiple entries provided the first entry is 1564 * the object id. Non-zpl consumers may safely make 1565 * use of the additional space. 1566 * 1567 * XXX: This should be a feature flag for compatibility 1568 */ 1569 if (zap.za_integer_length != 8 || 1570 zap.za_num_integers == 0) { 1571 cmn_err(CE_WARN, "zap_readdir: bad directory " 1572 "entry, obj = %lld, offset = %lld, " 1573 "length = %d, num = %lld\n", 1574 (u_longlong_t)zp->z_id, 1575 (u_longlong_t)offset, 1576 zap.za_integer_length, 1577 (u_longlong_t)zap.za_num_integers); 1578 error = SET_ERROR(ENXIO); 1579 goto update; 1580 } 1581 1582 objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 1583 type = ZFS_DIRENT_TYPE(zap.za_first_integer); 1584 } 1585 1586 done = !zpl_dir_emit(ctx, zap.za_name, strlen(zap.za_name), 1587 objnum, type); 1588 if (done) 1589 break; 1590 1591 /* Prefetch znode */ 1592 if (prefetch) { 1593 dmu_prefetch(os, objnum, 0, 0, 0, 1594 ZIO_PRIORITY_SYNC_READ); 1595 } 1596 1597 /* 1598 * Move to the next entry, fill in the previous offset. 1599 */ 1600 if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1601 zap_cursor_advance(&zc); 1602 offset = zap_cursor_serialize(&zc); 1603 } else { 1604 offset += 1; 1605 } 1606 ctx->pos = offset; 1607 } 1608 zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1609 1610 update: 1611 zap_cursor_fini(&zc); 1612 if (error == ENOENT) 1613 error = 0; 1614 out: 1615 ZFS_EXIT(zfsvfs); 1616 1617 return (error); 1618 } 1619 1620 /* 1621 * Get the basic file attributes and place them in the provided kstat 1622 * structure. The inode is assumed to be the authoritative source 1623 * for most of the attributes. However, the znode currently has the 1624 * authoritative atime, blksize, and block count. 1625 * 1626 * IN: ip - inode of file. 1627 * 1628 * OUT: sp - kstat values. 1629 * 1630 * RETURN: 0 (always succeeds) 1631 */ 1632 /* ARGSUSED */ 1633 int 1634 zfs_getattr_fast(struct user_namespace *user_ns, struct inode *ip, 1635 struct kstat *sp) 1636 { 1637 znode_t *zp = ITOZ(ip); 1638 zfsvfs_t *zfsvfs = ITOZSB(ip); 1639 uint32_t blksize; 1640 u_longlong_t nblocks; 1641 1642 ZFS_ENTER(zfsvfs); 1643 ZFS_VERIFY_ZP(zp); 1644 1645 mutex_enter(&zp->z_lock); 1646 1647 zpl_generic_fillattr(user_ns, ip, sp); 1648 /* 1649 * +1 link count for root inode with visible '.zfs' directory. 1650 */ 1651 if ((zp->z_id == zfsvfs->z_root) && zfs_show_ctldir(zp)) 1652 if (sp->nlink < ZFS_LINK_MAX) 1653 sp->nlink++; 1654 1655 sa_object_size(zp->z_sa_hdl, &blksize, &nblocks); 1656 sp->blksize = blksize; 1657 sp->blocks = nblocks; 1658 1659 if (unlikely(zp->z_blksz == 0)) { 1660 /* 1661 * Block size hasn't been set; suggest maximal I/O transfers. 1662 */ 1663 sp->blksize = zfsvfs->z_max_blksz; 1664 } 1665 1666 mutex_exit(&zp->z_lock); 1667 1668 /* 1669 * Required to prevent NFS client from detecting different inode 1670 * numbers of snapshot root dentry before and after snapshot mount. 1671 */ 1672 if (zfsvfs->z_issnap) { 1673 if (ip->i_sb->s_root->d_inode == ip) 1674 sp->ino = ZFSCTL_INO_SNAPDIRS - 1675 dmu_objset_id(zfsvfs->z_os); 1676 } 1677 1678 ZFS_EXIT(zfsvfs); 1679 1680 return (0); 1681 } 1682 1683 /* 1684 * For the operation of changing file's user/group/project, we need to 1685 * handle not only the main object that is assigned to the file directly, 1686 * but also the ones that are used by the file via hidden xattr directory. 1687 * 1688 * Because the xattr directory may contains many EA entries, as to it may 1689 * be impossible to change all of them via the transaction of changing the 1690 * main object's user/group/project attributes. Then we have to change them 1691 * via other multiple independent transactions one by one. It may be not good 1692 * solution, but we have no better idea yet. 1693 */ 1694 static int 1695 zfs_setattr_dir(znode_t *dzp) 1696 { 1697 struct inode *dxip = ZTOI(dzp); 1698 struct inode *xip = NULL; 1699 zfsvfs_t *zfsvfs = ZTOZSB(dzp); 1700 objset_t *os = zfsvfs->z_os; 1701 zap_cursor_t zc; 1702 zap_attribute_t zap; 1703 zfs_dirlock_t *dl; 1704 znode_t *zp = NULL; 1705 dmu_tx_t *tx = NULL; 1706 uint64_t uid, gid; 1707 sa_bulk_attr_t bulk[4]; 1708 int count; 1709 int err; 1710 1711 zap_cursor_init(&zc, os, dzp->z_id); 1712 while ((err = zap_cursor_retrieve(&zc, &zap)) == 0) { 1713 count = 0; 1714 if (zap.za_integer_length != 8 || zap.za_num_integers != 1) { 1715 err = ENXIO; 1716 break; 1717 } 1718 1719 err = zfs_dirent_lock(&dl, dzp, (char *)zap.za_name, &zp, 1720 ZEXISTS, NULL, NULL); 1721 if (err == ENOENT) 1722 goto next; 1723 if (err) 1724 break; 1725 1726 xip = ZTOI(zp); 1727 if (KUID_TO_SUID(xip->i_uid) == KUID_TO_SUID(dxip->i_uid) && 1728 KGID_TO_SGID(xip->i_gid) == KGID_TO_SGID(dxip->i_gid) && 1729 zp->z_projid == dzp->z_projid) 1730 goto next; 1731 1732 tx = dmu_tx_create(os); 1733 if (!(zp->z_pflags & ZFS_PROJID)) 1734 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 1735 else 1736 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 1737 1738 err = dmu_tx_assign(tx, TXG_WAIT); 1739 if (err) 1740 break; 1741 1742 mutex_enter(&dzp->z_lock); 1743 1744 if (KUID_TO_SUID(xip->i_uid) != KUID_TO_SUID(dxip->i_uid)) { 1745 xip->i_uid = dxip->i_uid; 1746 uid = zfs_uid_read(dxip); 1747 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 1748 &uid, sizeof (uid)); 1749 } 1750 1751 if (KGID_TO_SGID(xip->i_gid) != KGID_TO_SGID(dxip->i_gid)) { 1752 xip->i_gid = dxip->i_gid; 1753 gid = zfs_gid_read(dxip); 1754 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), NULL, 1755 &gid, sizeof (gid)); 1756 } 1757 1758 if (zp->z_projid != dzp->z_projid) { 1759 if (!(zp->z_pflags & ZFS_PROJID)) { 1760 zp->z_pflags |= ZFS_PROJID; 1761 SA_ADD_BULK_ATTR(bulk, count, 1762 SA_ZPL_FLAGS(zfsvfs), NULL, &zp->z_pflags, 1763 sizeof (zp->z_pflags)); 1764 } 1765 1766 zp->z_projid = dzp->z_projid; 1767 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_PROJID(zfsvfs), 1768 NULL, &zp->z_projid, sizeof (zp->z_projid)); 1769 } 1770 1771 mutex_exit(&dzp->z_lock); 1772 1773 if (likely(count > 0)) { 1774 err = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 1775 dmu_tx_commit(tx); 1776 } else { 1777 dmu_tx_abort(tx); 1778 } 1779 tx = NULL; 1780 if (err != 0 && err != ENOENT) 1781 break; 1782 1783 next: 1784 if (zp) { 1785 zrele(zp); 1786 zp = NULL; 1787 zfs_dirent_unlock(dl); 1788 } 1789 zap_cursor_advance(&zc); 1790 } 1791 1792 if (tx) 1793 dmu_tx_abort(tx); 1794 if (zp) { 1795 zrele(zp); 1796 zfs_dirent_unlock(dl); 1797 } 1798 zap_cursor_fini(&zc); 1799 1800 return (err == ENOENT ? 0 : err); 1801 } 1802 1803 /* 1804 * Set the file attributes to the values contained in the 1805 * vattr structure. 1806 * 1807 * IN: zp - znode of file to be modified. 1808 * vap - new attribute values. 1809 * If ATTR_XVATTR set, then optional attrs are being set 1810 * flags - ATTR_UTIME set if non-default time values provided. 1811 * - ATTR_NOACLCHECK (CIFS context only). 1812 * cr - credentials of caller. 1813 * 1814 * RETURN: 0 if success 1815 * error code if failure 1816 * 1817 * Timestamps: 1818 * ip - ctime updated, mtime updated if size changed. 1819 */ 1820 /* ARGSUSED */ 1821 int 1822 zfs_setattr(znode_t *zp, vattr_t *vap, int flags, cred_t *cr) 1823 { 1824 struct inode *ip; 1825 zfsvfs_t *zfsvfs = ZTOZSB(zp); 1826 objset_t *os = zfsvfs->z_os; 1827 zilog_t *zilog; 1828 dmu_tx_t *tx; 1829 vattr_t oldva; 1830 xvattr_t *tmpxvattr; 1831 uint_t mask = vap->va_mask; 1832 uint_t saved_mask = 0; 1833 int trim_mask = 0; 1834 uint64_t new_mode; 1835 uint64_t new_kuid = 0, new_kgid = 0, new_uid, new_gid; 1836 uint64_t xattr_obj; 1837 uint64_t mtime[2], ctime[2], atime[2]; 1838 uint64_t projid = ZFS_INVALID_PROJID; 1839 znode_t *attrzp; 1840 int need_policy = FALSE; 1841 int err, err2 = 0; 1842 zfs_fuid_info_t *fuidp = NULL; 1843 xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 1844 xoptattr_t *xoap; 1845 zfs_acl_t *aclp; 1846 boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 1847 boolean_t fuid_dirtied = B_FALSE; 1848 boolean_t handle_eadir = B_FALSE; 1849 sa_bulk_attr_t *bulk, *xattr_bulk; 1850 int count = 0, xattr_count = 0, bulks = 8; 1851 1852 if (mask == 0) 1853 return (0); 1854 1855 ZFS_ENTER(zfsvfs); 1856 ZFS_VERIFY_ZP(zp); 1857 ip = ZTOI(zp); 1858 1859 /* 1860 * If this is a xvattr_t, then get a pointer to the structure of 1861 * optional attributes. If this is NULL, then we have a vattr_t. 1862 */ 1863 xoap = xva_getxoptattr(xvap); 1864 if (xoap != NULL && (mask & ATTR_XVATTR)) { 1865 if (XVA_ISSET_REQ(xvap, XAT_PROJID)) { 1866 if (!dmu_objset_projectquota_enabled(os) || 1867 (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode))) { 1868 ZFS_EXIT(zfsvfs); 1869 return (SET_ERROR(ENOTSUP)); 1870 } 1871 1872 projid = xoap->xoa_projid; 1873 if (unlikely(projid == ZFS_INVALID_PROJID)) { 1874 ZFS_EXIT(zfsvfs); 1875 return (SET_ERROR(EINVAL)); 1876 } 1877 1878 if (projid == zp->z_projid && zp->z_pflags & ZFS_PROJID) 1879 projid = ZFS_INVALID_PROJID; 1880 else 1881 need_policy = TRUE; 1882 } 1883 1884 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT) && 1885 (xoap->xoa_projinherit != 1886 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) && 1887 (!dmu_objset_projectquota_enabled(os) || 1888 (!S_ISREG(ip->i_mode) && !S_ISDIR(ip->i_mode)))) { 1889 ZFS_EXIT(zfsvfs); 1890 return (SET_ERROR(ENOTSUP)); 1891 } 1892 } 1893 1894 zilog = zfsvfs->z_log; 1895 1896 /* 1897 * Make sure that if we have ephemeral uid/gid or xvattr specified 1898 * that file system is at proper version level 1899 */ 1900 1901 if (zfsvfs->z_use_fuids == B_FALSE && 1902 (((mask & ATTR_UID) && IS_EPHEMERAL(vap->va_uid)) || 1903 ((mask & ATTR_GID) && IS_EPHEMERAL(vap->va_gid)) || 1904 (mask & ATTR_XVATTR))) { 1905 ZFS_EXIT(zfsvfs); 1906 return (SET_ERROR(EINVAL)); 1907 } 1908 1909 if (mask & ATTR_SIZE && S_ISDIR(ip->i_mode)) { 1910 ZFS_EXIT(zfsvfs); 1911 return (SET_ERROR(EISDIR)); 1912 } 1913 1914 if (mask & ATTR_SIZE && !S_ISREG(ip->i_mode) && !S_ISFIFO(ip->i_mode)) { 1915 ZFS_EXIT(zfsvfs); 1916 return (SET_ERROR(EINVAL)); 1917 } 1918 1919 tmpxvattr = kmem_alloc(sizeof (xvattr_t), KM_SLEEP); 1920 xva_init(tmpxvattr); 1921 1922 bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP); 1923 xattr_bulk = kmem_alloc(sizeof (sa_bulk_attr_t) * bulks, KM_SLEEP); 1924 1925 /* 1926 * Immutable files can only alter immutable bit and atime 1927 */ 1928 if ((zp->z_pflags & ZFS_IMMUTABLE) && 1929 ((mask & (ATTR_SIZE|ATTR_UID|ATTR_GID|ATTR_MTIME|ATTR_MODE)) || 1930 ((mask & ATTR_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 1931 err = SET_ERROR(EPERM); 1932 goto out3; 1933 } 1934 1935 if ((mask & ATTR_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 1936 err = SET_ERROR(EPERM); 1937 goto out3; 1938 } 1939 1940 /* 1941 * Verify timestamps doesn't overflow 32 bits. 1942 * ZFS can handle large timestamps, but 32bit syscalls can't 1943 * handle times greater than 2039. This check should be removed 1944 * once large timestamps are fully supported. 1945 */ 1946 if (mask & (ATTR_ATIME | ATTR_MTIME)) { 1947 if (((mask & ATTR_ATIME) && 1948 TIMESPEC_OVERFLOW(&vap->va_atime)) || 1949 ((mask & ATTR_MTIME) && 1950 TIMESPEC_OVERFLOW(&vap->va_mtime))) { 1951 err = SET_ERROR(EOVERFLOW); 1952 goto out3; 1953 } 1954 } 1955 1956 top: 1957 attrzp = NULL; 1958 aclp = NULL; 1959 1960 /* Can this be moved to before the top label? */ 1961 if (zfs_is_readonly(zfsvfs)) { 1962 err = SET_ERROR(EROFS); 1963 goto out3; 1964 } 1965 1966 /* 1967 * First validate permissions 1968 */ 1969 1970 if (mask & ATTR_SIZE) { 1971 err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 1972 if (err) 1973 goto out3; 1974 1975 /* 1976 * XXX - Note, we are not providing any open 1977 * mode flags here (like FNDELAY), so we may 1978 * block if there are locks present... this 1979 * should be addressed in openat(). 1980 */ 1981 /* XXX - would it be OK to generate a log record here? */ 1982 err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 1983 if (err) 1984 goto out3; 1985 } 1986 1987 if (mask & (ATTR_ATIME|ATTR_MTIME) || 1988 ((mask & ATTR_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 1989 XVA_ISSET_REQ(xvap, XAT_READONLY) || 1990 XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 1991 XVA_ISSET_REQ(xvap, XAT_OFFLINE) || 1992 XVA_ISSET_REQ(xvap, XAT_SPARSE) || 1993 XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 1994 XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 1995 need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 1996 skipaclchk, cr); 1997 } 1998 1999 if (mask & (ATTR_UID|ATTR_GID)) { 2000 int idmask = (mask & (ATTR_UID|ATTR_GID)); 2001 int take_owner; 2002 int take_group; 2003 2004 /* 2005 * NOTE: even if a new mode is being set, 2006 * we may clear S_ISUID/S_ISGID bits. 2007 */ 2008 2009 if (!(mask & ATTR_MODE)) 2010 vap->va_mode = zp->z_mode; 2011 2012 /* 2013 * Take ownership or chgrp to group we are a member of 2014 */ 2015 2016 take_owner = (mask & ATTR_UID) && (vap->va_uid == crgetuid(cr)); 2017 take_group = (mask & ATTR_GID) && 2018 zfs_groupmember(zfsvfs, vap->va_gid, cr); 2019 2020 /* 2021 * If both ATTR_UID and ATTR_GID are set then take_owner and 2022 * take_group must both be set in order to allow taking 2023 * ownership. 2024 * 2025 * Otherwise, send the check through secpolicy_vnode_setattr() 2026 * 2027 */ 2028 2029 if (((idmask == (ATTR_UID|ATTR_GID)) && 2030 take_owner && take_group) || 2031 ((idmask == ATTR_UID) && take_owner) || 2032 ((idmask == ATTR_GID) && take_group)) { 2033 if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 2034 skipaclchk, cr) == 0) { 2035 /* 2036 * Remove setuid/setgid for non-privileged users 2037 */ 2038 (void) secpolicy_setid_clear(vap, cr); 2039 trim_mask = (mask & (ATTR_UID|ATTR_GID)); 2040 } else { 2041 need_policy = TRUE; 2042 } 2043 } else { 2044 need_policy = TRUE; 2045 } 2046 } 2047 2048 mutex_enter(&zp->z_lock); 2049 oldva.va_mode = zp->z_mode; 2050 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 2051 if (mask & ATTR_XVATTR) { 2052 /* 2053 * Update xvattr mask to include only those attributes 2054 * that are actually changing. 2055 * 2056 * the bits will be restored prior to actually setting 2057 * the attributes so the caller thinks they were set. 2058 */ 2059 if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 2060 if (xoap->xoa_appendonly != 2061 ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 2062 need_policy = TRUE; 2063 } else { 2064 XVA_CLR_REQ(xvap, XAT_APPENDONLY); 2065 XVA_SET_REQ(tmpxvattr, XAT_APPENDONLY); 2066 } 2067 } 2068 2069 if (XVA_ISSET_REQ(xvap, XAT_PROJINHERIT)) { 2070 if (xoap->xoa_projinherit != 2071 ((zp->z_pflags & ZFS_PROJINHERIT) != 0)) { 2072 need_policy = TRUE; 2073 } else { 2074 XVA_CLR_REQ(xvap, XAT_PROJINHERIT); 2075 XVA_SET_REQ(tmpxvattr, XAT_PROJINHERIT); 2076 } 2077 } 2078 2079 if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 2080 if (xoap->xoa_nounlink != 2081 ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 2082 need_policy = TRUE; 2083 } else { 2084 XVA_CLR_REQ(xvap, XAT_NOUNLINK); 2085 XVA_SET_REQ(tmpxvattr, XAT_NOUNLINK); 2086 } 2087 } 2088 2089 if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 2090 if (xoap->xoa_immutable != 2091 ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 2092 need_policy = TRUE; 2093 } else { 2094 XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 2095 XVA_SET_REQ(tmpxvattr, XAT_IMMUTABLE); 2096 } 2097 } 2098 2099 if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 2100 if (xoap->xoa_nodump != 2101 ((zp->z_pflags & ZFS_NODUMP) != 0)) { 2102 need_policy = TRUE; 2103 } else { 2104 XVA_CLR_REQ(xvap, XAT_NODUMP); 2105 XVA_SET_REQ(tmpxvattr, XAT_NODUMP); 2106 } 2107 } 2108 2109 if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 2110 if (xoap->xoa_av_modified != 2111 ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 2112 need_policy = TRUE; 2113 } else { 2114 XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 2115 XVA_SET_REQ(tmpxvattr, XAT_AV_MODIFIED); 2116 } 2117 } 2118 2119 if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 2120 if ((!S_ISREG(ip->i_mode) && 2121 xoap->xoa_av_quarantined) || 2122 xoap->xoa_av_quarantined != 2123 ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 2124 need_policy = TRUE; 2125 } else { 2126 XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 2127 XVA_SET_REQ(tmpxvattr, XAT_AV_QUARANTINED); 2128 } 2129 } 2130 2131 if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2132 mutex_exit(&zp->z_lock); 2133 err = SET_ERROR(EPERM); 2134 goto out3; 2135 } 2136 2137 if (need_policy == FALSE && 2138 (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 2139 XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 2140 need_policy = TRUE; 2141 } 2142 } 2143 2144 mutex_exit(&zp->z_lock); 2145 2146 if (mask & ATTR_MODE) { 2147 if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 2148 err = secpolicy_setid_setsticky_clear(ip, vap, 2149 &oldva, cr); 2150 if (err) 2151 goto out3; 2152 2153 trim_mask |= ATTR_MODE; 2154 } else { 2155 need_policy = TRUE; 2156 } 2157 } 2158 2159 if (need_policy) { 2160 /* 2161 * If trim_mask is set then take ownership 2162 * has been granted or write_acl is present and user 2163 * has the ability to modify mode. In that case remove 2164 * UID|GID and or MODE from mask so that 2165 * secpolicy_vnode_setattr() doesn't revoke it. 2166 */ 2167 2168 if (trim_mask) { 2169 saved_mask = vap->va_mask; 2170 vap->va_mask &= ~trim_mask; 2171 } 2172 err = secpolicy_vnode_setattr(cr, ip, vap, &oldva, flags, 2173 (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2174 if (err) 2175 goto out3; 2176 2177 if (trim_mask) 2178 vap->va_mask |= saved_mask; 2179 } 2180 2181 /* 2182 * secpolicy_vnode_setattr, or take ownership may have 2183 * changed va_mask 2184 */ 2185 mask = vap->va_mask; 2186 2187 if ((mask & (ATTR_UID | ATTR_GID)) || projid != ZFS_INVALID_PROJID) { 2188 handle_eadir = B_TRUE; 2189 err = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 2190 &xattr_obj, sizeof (xattr_obj)); 2191 2192 if (err == 0 && xattr_obj) { 2193 err = zfs_zget(ZTOZSB(zp), xattr_obj, &attrzp); 2194 if (err) 2195 goto out2; 2196 } 2197 if (mask & ATTR_UID) { 2198 new_kuid = zfs_fuid_create(zfsvfs, 2199 (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 2200 if (new_kuid != KUID_TO_SUID(ZTOI(zp)->i_uid) && 2201 zfs_id_overquota(zfsvfs, DMU_USERUSED_OBJECT, 2202 new_kuid)) { 2203 if (attrzp) 2204 zrele(attrzp); 2205 err = SET_ERROR(EDQUOT); 2206 goto out2; 2207 } 2208 } 2209 2210 if (mask & ATTR_GID) { 2211 new_kgid = zfs_fuid_create(zfsvfs, 2212 (uint64_t)vap->va_gid, cr, ZFS_GROUP, &fuidp); 2213 if (new_kgid != KGID_TO_SGID(ZTOI(zp)->i_gid) && 2214 zfs_id_overquota(zfsvfs, DMU_GROUPUSED_OBJECT, 2215 new_kgid)) { 2216 if (attrzp) 2217 zrele(attrzp); 2218 err = SET_ERROR(EDQUOT); 2219 goto out2; 2220 } 2221 } 2222 2223 if (projid != ZFS_INVALID_PROJID && 2224 zfs_id_overquota(zfsvfs, DMU_PROJECTUSED_OBJECT, projid)) { 2225 if (attrzp) 2226 zrele(attrzp); 2227 err = EDQUOT; 2228 goto out2; 2229 } 2230 } 2231 tx = dmu_tx_create(os); 2232 2233 if (mask & ATTR_MODE) { 2234 uint64_t pmode = zp->z_mode; 2235 uint64_t acl_obj; 2236 new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2237 2238 if (ZTOZSB(zp)->z_acl_mode == ZFS_ACL_RESTRICTED && 2239 !(zp->z_pflags & ZFS_ACL_TRIVIAL)) { 2240 err = EPERM; 2241 goto out; 2242 } 2243 2244 if ((err = zfs_acl_chmod_setattr(zp, &aclp, new_mode))) 2245 goto out; 2246 2247 mutex_enter(&zp->z_lock); 2248 if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 2249 /* 2250 * Are we upgrading ACL from old V0 format 2251 * to V1 format? 2252 */ 2253 if (zfsvfs->z_version >= ZPL_VERSION_FUID && 2254 zfs_znode_acl_version(zp) == 2255 ZFS_ACL_VERSION_INITIAL) { 2256 dmu_tx_hold_free(tx, acl_obj, 0, 2257 DMU_OBJECT_END); 2258 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2259 0, aclp->z_acl_bytes); 2260 } else { 2261 dmu_tx_hold_write(tx, acl_obj, 0, 2262 aclp->z_acl_bytes); 2263 } 2264 } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 2265 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2266 0, aclp->z_acl_bytes); 2267 } 2268 mutex_exit(&zp->z_lock); 2269 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 2270 } else { 2271 if (((mask & ATTR_XVATTR) && 2272 XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) || 2273 (projid != ZFS_INVALID_PROJID && 2274 !(zp->z_pflags & ZFS_PROJID))) 2275 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 2276 else 2277 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 2278 } 2279 2280 if (attrzp) { 2281 dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 2282 } 2283 2284 fuid_dirtied = zfsvfs->z_fuid_dirty; 2285 if (fuid_dirtied) 2286 zfs_fuid_txhold(zfsvfs, tx); 2287 2288 zfs_sa_upgrade_txholds(tx, zp); 2289 2290 err = dmu_tx_assign(tx, TXG_WAIT); 2291 if (err) 2292 goto out; 2293 2294 count = 0; 2295 /* 2296 * Set each attribute requested. 2297 * We group settings according to the locks they need to acquire. 2298 * 2299 * Note: you cannot set ctime directly, although it will be 2300 * updated as a side-effect of calling this function. 2301 */ 2302 2303 if (projid != ZFS_INVALID_PROJID && !(zp->z_pflags & ZFS_PROJID)) { 2304 /* 2305 * For the existed object that is upgraded from old system, 2306 * its on-disk layout has no slot for the project ID attribute. 2307 * But quota accounting logic needs to access related slots by 2308 * offset directly. So we need to adjust old objects' layout 2309 * to make the project ID to some unified and fixed offset. 2310 */ 2311 if (attrzp) 2312 err = sa_add_projid(attrzp->z_sa_hdl, tx, projid); 2313 if (err == 0) 2314 err = sa_add_projid(zp->z_sa_hdl, tx, projid); 2315 2316 if (unlikely(err == EEXIST)) 2317 err = 0; 2318 else if (err != 0) 2319 goto out; 2320 else 2321 projid = ZFS_INVALID_PROJID; 2322 } 2323 2324 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) 2325 mutex_enter(&zp->z_acl_lock); 2326 mutex_enter(&zp->z_lock); 2327 2328 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 2329 &zp->z_pflags, sizeof (zp->z_pflags)); 2330 2331 if (attrzp) { 2332 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) 2333 mutex_enter(&attrzp->z_acl_lock); 2334 mutex_enter(&attrzp->z_lock); 2335 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2336 SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 2337 sizeof (attrzp->z_pflags)); 2338 if (projid != ZFS_INVALID_PROJID) { 2339 attrzp->z_projid = projid; 2340 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2341 SA_ZPL_PROJID(zfsvfs), NULL, &attrzp->z_projid, 2342 sizeof (attrzp->z_projid)); 2343 } 2344 } 2345 2346 if (mask & (ATTR_UID|ATTR_GID)) { 2347 2348 if (mask & ATTR_UID) { 2349 ZTOI(zp)->i_uid = SUID_TO_KUID(new_kuid); 2350 new_uid = zfs_uid_read(ZTOI(zp)); 2351 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 2352 &new_uid, sizeof (new_uid)); 2353 if (attrzp) { 2354 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2355 SA_ZPL_UID(zfsvfs), NULL, &new_uid, 2356 sizeof (new_uid)); 2357 ZTOI(attrzp)->i_uid = SUID_TO_KUID(new_uid); 2358 } 2359 } 2360 2361 if (mask & ATTR_GID) { 2362 ZTOI(zp)->i_gid = SGID_TO_KGID(new_kgid); 2363 new_gid = zfs_gid_read(ZTOI(zp)); 2364 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 2365 NULL, &new_gid, sizeof (new_gid)); 2366 if (attrzp) { 2367 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2368 SA_ZPL_GID(zfsvfs), NULL, &new_gid, 2369 sizeof (new_gid)); 2370 ZTOI(attrzp)->i_gid = SGID_TO_KGID(new_kgid); 2371 } 2372 } 2373 if (!(mask & ATTR_MODE)) { 2374 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 2375 NULL, &new_mode, sizeof (new_mode)); 2376 new_mode = zp->z_mode; 2377 } 2378 err = zfs_acl_chown_setattr(zp); 2379 ASSERT(err == 0); 2380 if (attrzp) { 2381 err = zfs_acl_chown_setattr(attrzp); 2382 ASSERT(err == 0); 2383 } 2384 } 2385 2386 if (mask & ATTR_MODE) { 2387 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 2388 &new_mode, sizeof (new_mode)); 2389 zp->z_mode = ZTOI(zp)->i_mode = new_mode; 2390 ASSERT3P(aclp, !=, NULL); 2391 err = zfs_aclset_common(zp, aclp, cr, tx); 2392 ASSERT0(err); 2393 if (zp->z_acl_cached) 2394 zfs_acl_free(zp->z_acl_cached); 2395 zp->z_acl_cached = aclp; 2396 aclp = NULL; 2397 } 2398 2399 if ((mask & ATTR_ATIME) || zp->z_atime_dirty) { 2400 zp->z_atime_dirty = B_FALSE; 2401 ZFS_TIME_ENCODE(&ip->i_atime, atime); 2402 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 2403 &atime, sizeof (atime)); 2404 } 2405 2406 if (mask & (ATTR_MTIME | ATTR_SIZE)) { 2407 ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 2408 ZTOI(zp)->i_mtime = zpl_inode_timestamp_truncate( 2409 vap->va_mtime, ZTOI(zp)); 2410 2411 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 2412 mtime, sizeof (mtime)); 2413 } 2414 2415 if (mask & (ATTR_CTIME | ATTR_SIZE)) { 2416 ZFS_TIME_ENCODE(&vap->va_ctime, ctime); 2417 ZTOI(zp)->i_ctime = zpl_inode_timestamp_truncate(vap->va_ctime, 2418 ZTOI(zp)); 2419 SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 2420 ctime, sizeof (ctime)); 2421 } 2422 2423 if (projid != ZFS_INVALID_PROJID) { 2424 zp->z_projid = projid; 2425 SA_ADD_BULK_ATTR(bulk, count, 2426 SA_ZPL_PROJID(zfsvfs), NULL, &zp->z_projid, 2427 sizeof (zp->z_projid)); 2428 } 2429 2430 if (attrzp && mask) { 2431 SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 2432 SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 2433 sizeof (ctime)); 2434 } 2435 2436 /* 2437 * Do this after setting timestamps to prevent timestamp 2438 * update from toggling bit 2439 */ 2440 2441 if (xoap && (mask & ATTR_XVATTR)) { 2442 2443 /* 2444 * restore trimmed off masks 2445 * so that return masks can be set for caller. 2446 */ 2447 2448 if (XVA_ISSET_REQ(tmpxvattr, XAT_APPENDONLY)) { 2449 XVA_SET_REQ(xvap, XAT_APPENDONLY); 2450 } 2451 if (XVA_ISSET_REQ(tmpxvattr, XAT_NOUNLINK)) { 2452 XVA_SET_REQ(xvap, XAT_NOUNLINK); 2453 } 2454 if (XVA_ISSET_REQ(tmpxvattr, XAT_IMMUTABLE)) { 2455 XVA_SET_REQ(xvap, XAT_IMMUTABLE); 2456 } 2457 if (XVA_ISSET_REQ(tmpxvattr, XAT_NODUMP)) { 2458 XVA_SET_REQ(xvap, XAT_NODUMP); 2459 } 2460 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_MODIFIED)) { 2461 XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 2462 } 2463 if (XVA_ISSET_REQ(tmpxvattr, XAT_AV_QUARANTINED)) { 2464 XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 2465 } 2466 if (XVA_ISSET_REQ(tmpxvattr, XAT_PROJINHERIT)) { 2467 XVA_SET_REQ(xvap, XAT_PROJINHERIT); 2468 } 2469 2470 if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 2471 ASSERT(S_ISREG(ip->i_mode)); 2472 2473 zfs_xvattr_set(zp, xvap, tx); 2474 } 2475 2476 if (fuid_dirtied) 2477 zfs_fuid_sync(zfsvfs, tx); 2478 2479 if (mask != 0) 2480 zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 2481 2482 mutex_exit(&zp->z_lock); 2483 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) 2484 mutex_exit(&zp->z_acl_lock); 2485 2486 if (attrzp) { 2487 if (mask & (ATTR_UID|ATTR_GID|ATTR_MODE)) 2488 mutex_exit(&attrzp->z_acl_lock); 2489 mutex_exit(&attrzp->z_lock); 2490 } 2491 out: 2492 if (err == 0 && xattr_count > 0) { 2493 err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 2494 xattr_count, tx); 2495 ASSERT(err2 == 0); 2496 } 2497 2498 if (aclp) 2499 zfs_acl_free(aclp); 2500 2501 if (fuidp) { 2502 zfs_fuid_info_free(fuidp); 2503 fuidp = NULL; 2504 } 2505 2506 if (err) { 2507 dmu_tx_abort(tx); 2508 if (attrzp) 2509 zrele(attrzp); 2510 if (err == ERESTART) 2511 goto top; 2512 } else { 2513 if (count > 0) 2514 err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 2515 dmu_tx_commit(tx); 2516 if (attrzp) { 2517 if (err2 == 0 && handle_eadir) 2518 err2 = zfs_setattr_dir(attrzp); 2519 zrele(attrzp); 2520 } 2521 zfs_znode_update_vfs(zp); 2522 } 2523 2524 out2: 2525 if (os->os_sync == ZFS_SYNC_ALWAYS) 2526 zil_commit(zilog, 0); 2527 2528 out3: 2529 kmem_free(xattr_bulk, sizeof (sa_bulk_attr_t) * bulks); 2530 kmem_free(bulk, sizeof (sa_bulk_attr_t) * bulks); 2531 kmem_free(tmpxvattr, sizeof (xvattr_t)); 2532 ZFS_EXIT(zfsvfs); 2533 return (err); 2534 } 2535 2536 typedef struct zfs_zlock { 2537 krwlock_t *zl_rwlock; /* lock we acquired */ 2538 znode_t *zl_znode; /* znode we held */ 2539 struct zfs_zlock *zl_next; /* next in list */ 2540 } zfs_zlock_t; 2541 2542 /* 2543 * Drop locks and release vnodes that were held by zfs_rename_lock(). 2544 */ 2545 static void 2546 zfs_rename_unlock(zfs_zlock_t **zlpp) 2547 { 2548 zfs_zlock_t *zl; 2549 2550 while ((zl = *zlpp) != NULL) { 2551 if (zl->zl_znode != NULL) 2552 zfs_zrele_async(zl->zl_znode); 2553 rw_exit(zl->zl_rwlock); 2554 *zlpp = zl->zl_next; 2555 kmem_free(zl, sizeof (*zl)); 2556 } 2557 } 2558 2559 /* 2560 * Search back through the directory tree, using the ".." entries. 2561 * Lock each directory in the chain to prevent concurrent renames. 2562 * Fail any attempt to move a directory into one of its own descendants. 2563 * XXX - z_parent_lock can overlap with map or grow locks 2564 */ 2565 static int 2566 zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2567 { 2568 zfs_zlock_t *zl; 2569 znode_t *zp = tdzp; 2570 uint64_t rootid = ZTOZSB(zp)->z_root; 2571 uint64_t oidp = zp->z_id; 2572 krwlock_t *rwlp = &szp->z_parent_lock; 2573 krw_t rw = RW_WRITER; 2574 2575 /* 2576 * First pass write-locks szp and compares to zp->z_id. 2577 * Later passes read-lock zp and compare to zp->z_parent. 2578 */ 2579 do { 2580 if (!rw_tryenter(rwlp, rw)) { 2581 /* 2582 * Another thread is renaming in this path. 2583 * Note that if we are a WRITER, we don't have any 2584 * parent_locks held yet. 2585 */ 2586 if (rw == RW_READER && zp->z_id > szp->z_id) { 2587 /* 2588 * Drop our locks and restart 2589 */ 2590 zfs_rename_unlock(&zl); 2591 *zlpp = NULL; 2592 zp = tdzp; 2593 oidp = zp->z_id; 2594 rwlp = &szp->z_parent_lock; 2595 rw = RW_WRITER; 2596 continue; 2597 } else { 2598 /* 2599 * Wait for other thread to drop its locks 2600 */ 2601 rw_enter(rwlp, rw); 2602 } 2603 } 2604 2605 zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2606 zl->zl_rwlock = rwlp; 2607 zl->zl_znode = NULL; 2608 zl->zl_next = *zlpp; 2609 *zlpp = zl; 2610 2611 if (oidp == szp->z_id) /* We're a descendant of szp */ 2612 return (SET_ERROR(EINVAL)); 2613 2614 if (oidp == rootid) /* We've hit the top */ 2615 return (0); 2616 2617 if (rw == RW_READER) { /* i.e. not the first pass */ 2618 int error = zfs_zget(ZTOZSB(zp), oidp, &zp); 2619 if (error) 2620 return (error); 2621 zl->zl_znode = zp; 2622 } 2623 (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(ZTOZSB(zp)), 2624 &oidp, sizeof (oidp)); 2625 rwlp = &zp->z_parent_lock; 2626 rw = RW_READER; 2627 2628 } while (zp->z_id != sdzp->z_id); 2629 2630 return (0); 2631 } 2632 2633 /* 2634 * Move an entry from the provided source directory to the target 2635 * directory. Change the entry name as indicated. 2636 * 2637 * IN: sdzp - Source directory containing the "old entry". 2638 * snm - Old entry name. 2639 * tdzp - Target directory to contain the "new entry". 2640 * tnm - New entry name. 2641 * cr - credentials of caller. 2642 * flags - case flags 2643 * 2644 * RETURN: 0 on success, error code on failure. 2645 * 2646 * Timestamps: 2647 * sdzp,tdzp - ctime|mtime updated 2648 */ 2649 /*ARGSUSED*/ 2650 int 2651 zfs_rename(znode_t *sdzp, char *snm, znode_t *tdzp, char *tnm, 2652 cred_t *cr, int flags) 2653 { 2654 znode_t *szp, *tzp; 2655 zfsvfs_t *zfsvfs = ZTOZSB(sdzp); 2656 zilog_t *zilog; 2657 zfs_dirlock_t *sdl, *tdl; 2658 dmu_tx_t *tx; 2659 zfs_zlock_t *zl; 2660 int cmp, serr, terr; 2661 int error = 0; 2662 int zflg = 0; 2663 boolean_t waited = B_FALSE; 2664 2665 if (snm == NULL || tnm == NULL) 2666 return (SET_ERROR(EINVAL)); 2667 2668 ZFS_ENTER(zfsvfs); 2669 ZFS_VERIFY_ZP(sdzp); 2670 zilog = zfsvfs->z_log; 2671 2672 ZFS_VERIFY_ZP(tdzp); 2673 2674 /* 2675 * We check i_sb because snapshots and the ctldir must have different 2676 * super blocks. 2677 */ 2678 if (ZTOI(tdzp)->i_sb != ZTOI(sdzp)->i_sb || 2679 zfsctl_is_node(ZTOI(tdzp))) { 2680 ZFS_EXIT(zfsvfs); 2681 return (SET_ERROR(EXDEV)); 2682 } 2683 2684 if (zfsvfs->z_utf8 && u8_validate(tnm, 2685 strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 2686 ZFS_EXIT(zfsvfs); 2687 return (SET_ERROR(EILSEQ)); 2688 } 2689 2690 if (flags & FIGNORECASE) 2691 zflg |= ZCILOOK; 2692 2693 top: 2694 szp = NULL; 2695 tzp = NULL; 2696 zl = NULL; 2697 2698 /* 2699 * This is to prevent the creation of links into attribute space 2700 * by renaming a linked file into/outof an attribute directory. 2701 * See the comment in zfs_link() for why this is considered bad. 2702 */ 2703 if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 2704 ZFS_EXIT(zfsvfs); 2705 return (SET_ERROR(EINVAL)); 2706 } 2707 2708 /* 2709 * Lock source and target directory entries. To prevent deadlock, 2710 * a lock ordering must be defined. We lock the directory with 2711 * the smallest object id first, or if it's a tie, the one with 2712 * the lexically first name. 2713 */ 2714 if (sdzp->z_id < tdzp->z_id) { 2715 cmp = -1; 2716 } else if (sdzp->z_id > tdzp->z_id) { 2717 cmp = 1; 2718 } else { 2719 /* 2720 * First compare the two name arguments without 2721 * considering any case folding. 2722 */ 2723 int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 2724 2725 cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 2726 ASSERT(error == 0 || !zfsvfs->z_utf8); 2727 if (cmp == 0) { 2728 /* 2729 * POSIX: "If the old argument and the new argument 2730 * both refer to links to the same existing file, 2731 * the rename() function shall return successfully 2732 * and perform no other action." 2733 */ 2734 ZFS_EXIT(zfsvfs); 2735 return (0); 2736 } 2737 /* 2738 * If the file system is case-folding, then we may 2739 * have some more checking to do. A case-folding file 2740 * system is either supporting mixed case sensitivity 2741 * access or is completely case-insensitive. Note 2742 * that the file system is always case preserving. 2743 * 2744 * In mixed sensitivity mode case sensitive behavior 2745 * is the default. FIGNORECASE must be used to 2746 * explicitly request case insensitive behavior. 2747 * 2748 * If the source and target names provided differ only 2749 * by case (e.g., a request to rename 'tim' to 'Tim'), 2750 * we will treat this as a special case in the 2751 * case-insensitive mode: as long as the source name 2752 * is an exact match, we will allow this to proceed as 2753 * a name-change request. 2754 */ 2755 if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 2756 (zfsvfs->z_case == ZFS_CASE_MIXED && 2757 flags & FIGNORECASE)) && 2758 u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 2759 &error) == 0) { 2760 /* 2761 * case preserving rename request, require exact 2762 * name matches 2763 */ 2764 zflg |= ZCIEXACT; 2765 zflg &= ~ZCILOOK; 2766 } 2767 } 2768 2769 /* 2770 * If the source and destination directories are the same, we should 2771 * grab the z_name_lock of that directory only once. 2772 */ 2773 if (sdzp == tdzp) { 2774 zflg |= ZHAVELOCK; 2775 rw_enter(&sdzp->z_name_lock, RW_READER); 2776 } 2777 2778 if (cmp < 0) { 2779 serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 2780 ZEXISTS | zflg, NULL, NULL); 2781 terr = zfs_dirent_lock(&tdl, 2782 tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 2783 } else { 2784 terr = zfs_dirent_lock(&tdl, 2785 tdzp, tnm, &tzp, zflg, NULL, NULL); 2786 serr = zfs_dirent_lock(&sdl, 2787 sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 2788 NULL, NULL); 2789 } 2790 2791 if (serr) { 2792 /* 2793 * Source entry invalid or not there. 2794 */ 2795 if (!terr) { 2796 zfs_dirent_unlock(tdl); 2797 if (tzp) 2798 zrele(tzp); 2799 } 2800 2801 if (sdzp == tdzp) 2802 rw_exit(&sdzp->z_name_lock); 2803 2804 if (strcmp(snm, "..") == 0) 2805 serr = EINVAL; 2806 ZFS_EXIT(zfsvfs); 2807 return (serr); 2808 } 2809 if (terr) { 2810 zfs_dirent_unlock(sdl); 2811 zrele(szp); 2812 2813 if (sdzp == tdzp) 2814 rw_exit(&sdzp->z_name_lock); 2815 2816 if (strcmp(tnm, "..") == 0) 2817 terr = EINVAL; 2818 ZFS_EXIT(zfsvfs); 2819 return (terr); 2820 } 2821 2822 /* 2823 * If we are using project inheritance, means if the directory has 2824 * ZFS_PROJINHERIT set, then its descendant directories will inherit 2825 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under 2826 * such case, we only allow renames into our tree when the project 2827 * IDs are the same. 2828 */ 2829 if (tdzp->z_pflags & ZFS_PROJINHERIT && 2830 tdzp->z_projid != szp->z_projid) { 2831 error = SET_ERROR(EXDEV); 2832 goto out; 2833 } 2834 2835 /* 2836 * Must have write access at the source to remove the old entry 2837 * and write access at the target to create the new entry. 2838 * Note that if target and source are the same, this can be 2839 * done in a single check. 2840 */ 2841 2842 if ((error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr))) 2843 goto out; 2844 2845 if (S_ISDIR(ZTOI(szp)->i_mode)) { 2846 /* 2847 * Check to make sure rename is valid. 2848 * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2849 */ 2850 if ((error = zfs_rename_lock(szp, tdzp, sdzp, &zl))) 2851 goto out; 2852 } 2853 2854 /* 2855 * Does target exist? 2856 */ 2857 if (tzp) { 2858 /* 2859 * Source and target must be the same type. 2860 */ 2861 if (S_ISDIR(ZTOI(szp)->i_mode)) { 2862 if (!S_ISDIR(ZTOI(tzp)->i_mode)) { 2863 error = SET_ERROR(ENOTDIR); 2864 goto out; 2865 } 2866 } else { 2867 if (S_ISDIR(ZTOI(tzp)->i_mode)) { 2868 error = SET_ERROR(EISDIR); 2869 goto out; 2870 } 2871 } 2872 /* 2873 * POSIX dictates that when the source and target 2874 * entries refer to the same file object, rename 2875 * must do nothing and exit without error. 2876 */ 2877 if (szp->z_id == tzp->z_id) { 2878 error = 0; 2879 goto out; 2880 } 2881 } 2882 2883 tx = dmu_tx_create(zfsvfs->z_os); 2884 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 2885 dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 2886 dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 2887 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 2888 if (sdzp != tdzp) { 2889 dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 2890 zfs_sa_upgrade_txholds(tx, tdzp); 2891 } 2892 if (tzp) { 2893 dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 2894 zfs_sa_upgrade_txholds(tx, tzp); 2895 } 2896 2897 zfs_sa_upgrade_txholds(tx, szp); 2898 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2899 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 2900 if (error) { 2901 if (zl != NULL) 2902 zfs_rename_unlock(&zl); 2903 zfs_dirent_unlock(sdl); 2904 zfs_dirent_unlock(tdl); 2905 2906 if (sdzp == tdzp) 2907 rw_exit(&sdzp->z_name_lock); 2908 2909 if (error == ERESTART) { 2910 waited = B_TRUE; 2911 dmu_tx_wait(tx); 2912 dmu_tx_abort(tx); 2913 zrele(szp); 2914 if (tzp) 2915 zrele(tzp); 2916 goto top; 2917 } 2918 dmu_tx_abort(tx); 2919 zrele(szp); 2920 if (tzp) 2921 zrele(tzp); 2922 ZFS_EXIT(zfsvfs); 2923 return (error); 2924 } 2925 2926 if (tzp) /* Attempt to remove the existing target */ 2927 error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 2928 2929 if (error == 0) { 2930 error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2931 if (error == 0) { 2932 szp->z_pflags |= ZFS_AV_MODIFIED; 2933 if (tdzp->z_pflags & ZFS_PROJINHERIT) 2934 szp->z_pflags |= ZFS_PROJINHERIT; 2935 2936 error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 2937 (void *)&szp->z_pflags, sizeof (uint64_t), tx); 2938 ASSERT0(error); 2939 2940 error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2941 if (error == 0) { 2942 zfs_log_rename(zilog, tx, TX_RENAME | 2943 (flags & FIGNORECASE ? TX_CI : 0), sdzp, 2944 sdl->dl_name, tdzp, tdl->dl_name, szp); 2945 } else { 2946 /* 2947 * At this point, we have successfully created 2948 * the target name, but have failed to remove 2949 * the source name. Since the create was done 2950 * with the ZRENAMING flag, there are 2951 * complications; for one, the link count is 2952 * wrong. The easiest way to deal with this 2953 * is to remove the newly created target, and 2954 * return the original error. This must 2955 * succeed; fortunately, it is very unlikely to 2956 * fail, since we just created it. 2957 */ 2958 VERIFY3U(zfs_link_destroy(tdl, szp, tx, 2959 ZRENAMING, NULL), ==, 0); 2960 } 2961 } else { 2962 /* 2963 * If we had removed the existing target, subsequent 2964 * call to zfs_link_create() to add back the same entry 2965 * but, the new dnode (szp) should not fail. 2966 */ 2967 ASSERT(tzp == NULL); 2968 } 2969 } 2970 2971 dmu_tx_commit(tx); 2972 out: 2973 if (zl != NULL) 2974 zfs_rename_unlock(&zl); 2975 2976 zfs_dirent_unlock(sdl); 2977 zfs_dirent_unlock(tdl); 2978 2979 zfs_znode_update_vfs(sdzp); 2980 if (sdzp == tdzp) 2981 rw_exit(&sdzp->z_name_lock); 2982 2983 if (sdzp != tdzp) 2984 zfs_znode_update_vfs(tdzp); 2985 2986 zfs_znode_update_vfs(szp); 2987 zrele(szp); 2988 if (tzp) { 2989 zfs_znode_update_vfs(tzp); 2990 zrele(tzp); 2991 } 2992 2993 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2994 zil_commit(zilog, 0); 2995 2996 ZFS_EXIT(zfsvfs); 2997 return (error); 2998 } 2999 3000 /* 3001 * Insert the indicated symbolic reference entry into the directory. 3002 * 3003 * IN: dzp - Directory to contain new symbolic link. 3004 * name - Name of directory entry in dip. 3005 * vap - Attributes of new entry. 3006 * link - Name for new symlink entry. 3007 * cr - credentials of caller. 3008 * flags - case flags 3009 * 3010 * OUT: zpp - Znode for new symbolic link. 3011 * 3012 * RETURN: 0 on success, error code on failure. 3013 * 3014 * Timestamps: 3015 * dip - ctime|mtime updated 3016 */ 3017 /*ARGSUSED*/ 3018 int 3019 zfs_symlink(znode_t *dzp, char *name, vattr_t *vap, char *link, 3020 znode_t **zpp, cred_t *cr, int flags) 3021 { 3022 znode_t *zp; 3023 zfs_dirlock_t *dl; 3024 dmu_tx_t *tx; 3025 zfsvfs_t *zfsvfs = ZTOZSB(dzp); 3026 zilog_t *zilog; 3027 uint64_t len = strlen(link); 3028 int error; 3029 int zflg = ZNEW; 3030 zfs_acl_ids_t acl_ids; 3031 boolean_t fuid_dirtied; 3032 uint64_t txtype = TX_SYMLINK; 3033 boolean_t waited = B_FALSE; 3034 3035 ASSERT(S_ISLNK(vap->va_mode)); 3036 3037 if (name == NULL) 3038 return (SET_ERROR(EINVAL)); 3039 3040 ZFS_ENTER(zfsvfs); 3041 ZFS_VERIFY_ZP(dzp); 3042 zilog = zfsvfs->z_log; 3043 3044 if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 3045 NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3046 ZFS_EXIT(zfsvfs); 3047 return (SET_ERROR(EILSEQ)); 3048 } 3049 if (flags & FIGNORECASE) 3050 zflg |= ZCILOOK; 3051 3052 if (len > MAXPATHLEN) { 3053 ZFS_EXIT(zfsvfs); 3054 return (SET_ERROR(ENAMETOOLONG)); 3055 } 3056 3057 if ((error = zfs_acl_ids_create(dzp, 0, 3058 vap, cr, NULL, &acl_ids)) != 0) { 3059 ZFS_EXIT(zfsvfs); 3060 return (error); 3061 } 3062 top: 3063 *zpp = NULL; 3064 3065 /* 3066 * Attempt to lock directory; fail if entry already exists. 3067 */ 3068 error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 3069 if (error) { 3070 zfs_acl_ids_free(&acl_ids); 3071 ZFS_EXIT(zfsvfs); 3072 return (error); 3073 } 3074 3075 if ((error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { 3076 zfs_acl_ids_free(&acl_ids); 3077 zfs_dirent_unlock(dl); 3078 ZFS_EXIT(zfsvfs); 3079 return (error); 3080 } 3081 3082 if (zfs_acl_ids_overquota(zfsvfs, &acl_ids, ZFS_DEFAULT_PROJID)) { 3083 zfs_acl_ids_free(&acl_ids); 3084 zfs_dirent_unlock(dl); 3085 ZFS_EXIT(zfsvfs); 3086 return (SET_ERROR(EDQUOT)); 3087 } 3088 tx = dmu_tx_create(zfsvfs->z_os); 3089 fuid_dirtied = zfsvfs->z_fuid_dirty; 3090 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3091 dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3092 dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 3093 ZFS_SA_BASE_ATTR_SIZE + len); 3094 dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 3095 if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 3096 dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 3097 acl_ids.z_aclp->z_acl_bytes); 3098 } 3099 if (fuid_dirtied) 3100 zfs_fuid_txhold(zfsvfs, tx); 3101 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 3102 if (error) { 3103 zfs_dirent_unlock(dl); 3104 if (error == ERESTART) { 3105 waited = B_TRUE; 3106 dmu_tx_wait(tx); 3107 dmu_tx_abort(tx); 3108 goto top; 3109 } 3110 zfs_acl_ids_free(&acl_ids); 3111 dmu_tx_abort(tx); 3112 ZFS_EXIT(zfsvfs); 3113 return (error); 3114 } 3115 3116 /* 3117 * Create a new object for the symlink. 3118 * for version 4 ZPL datasets the symlink will be an SA attribute 3119 */ 3120 zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 3121 3122 if (fuid_dirtied) 3123 zfs_fuid_sync(zfsvfs, tx); 3124 3125 mutex_enter(&zp->z_lock); 3126 if (zp->z_is_sa) 3127 error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 3128 link, len, tx); 3129 else 3130 zfs_sa_symlink(zp, link, len, tx); 3131 mutex_exit(&zp->z_lock); 3132 3133 zp->z_size = len; 3134 (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 3135 &zp->z_size, sizeof (zp->z_size), tx); 3136 /* 3137 * Insert the new object into the directory. 3138 */ 3139 error = zfs_link_create(dl, zp, tx, ZNEW); 3140 if (error != 0) { 3141 zfs_znode_delete(zp, tx); 3142 remove_inode_hash(ZTOI(zp)); 3143 } else { 3144 if (flags & FIGNORECASE) 3145 txtype |= TX_CI; 3146 zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 3147 3148 zfs_znode_update_vfs(dzp); 3149 zfs_znode_update_vfs(zp); 3150 } 3151 3152 zfs_acl_ids_free(&acl_ids); 3153 3154 dmu_tx_commit(tx); 3155 3156 zfs_dirent_unlock(dl); 3157 3158 if (error == 0) { 3159 *zpp = zp; 3160 3161 if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3162 zil_commit(zilog, 0); 3163 } else { 3164 zrele(zp); 3165 } 3166 3167 ZFS_EXIT(zfsvfs); 3168 return (error); 3169 } 3170 3171 /* 3172 * Return, in the buffer contained in the provided uio structure, 3173 * the symbolic path referred to by ip. 3174 * 3175 * IN: ip - inode of symbolic link 3176 * uio - structure to contain the link path. 3177 * cr - credentials of caller. 3178 * 3179 * RETURN: 0 if success 3180 * error code if failure 3181 * 3182 * Timestamps: 3183 * ip - atime updated 3184 */ 3185 /* ARGSUSED */ 3186 int 3187 zfs_readlink(struct inode *ip, zfs_uio_t *uio, cred_t *cr) 3188 { 3189 znode_t *zp = ITOZ(ip); 3190 zfsvfs_t *zfsvfs = ITOZSB(ip); 3191 int error; 3192 3193 ZFS_ENTER(zfsvfs); 3194 ZFS_VERIFY_ZP(zp); 3195 3196 mutex_enter(&zp->z_lock); 3197 if (zp->z_is_sa) 3198 error = sa_lookup_uio(zp->z_sa_hdl, 3199 SA_ZPL_SYMLINK(zfsvfs), uio); 3200 else 3201 error = zfs_sa_readlink(zp, uio); 3202 mutex_exit(&zp->z_lock); 3203 3204 ZFS_EXIT(zfsvfs); 3205 return (error); 3206 } 3207 3208 /* 3209 * Insert a new entry into directory tdzp referencing szp. 3210 * 3211 * IN: tdzp - Directory to contain new entry. 3212 * szp - znode of new entry. 3213 * name - name of new entry. 3214 * cr - credentials of caller. 3215 * flags - case flags. 3216 * 3217 * RETURN: 0 if success 3218 * error code if failure 3219 * 3220 * Timestamps: 3221 * tdzp - ctime|mtime updated 3222 * szp - ctime updated 3223 */ 3224 /* ARGSUSED */ 3225 int 3226 zfs_link(znode_t *tdzp, znode_t *szp, char *name, cred_t *cr, 3227 int flags) 3228 { 3229 struct inode *sip = ZTOI(szp); 3230 znode_t *tzp; 3231 zfsvfs_t *zfsvfs = ZTOZSB(tdzp); 3232 zilog_t *zilog; 3233 zfs_dirlock_t *dl; 3234 dmu_tx_t *tx; 3235 int error; 3236 int zf = ZNEW; 3237 uint64_t parent; 3238 uid_t owner; 3239 boolean_t waited = B_FALSE; 3240 boolean_t is_tmpfile = 0; 3241 uint64_t txg; 3242 #ifdef HAVE_TMPFILE 3243 is_tmpfile = (sip->i_nlink == 0 && (sip->i_state & I_LINKABLE)); 3244 #endif 3245 ASSERT(S_ISDIR(ZTOI(tdzp)->i_mode)); 3246 3247 if (name == NULL) 3248 return (SET_ERROR(EINVAL)); 3249 3250 ZFS_ENTER(zfsvfs); 3251 ZFS_VERIFY_ZP(tdzp); 3252 zilog = zfsvfs->z_log; 3253 3254 /* 3255 * POSIX dictates that we return EPERM here. 3256 * Better choices include ENOTSUP or EISDIR. 3257 */ 3258 if (S_ISDIR(sip->i_mode)) { 3259 ZFS_EXIT(zfsvfs); 3260 return (SET_ERROR(EPERM)); 3261 } 3262 3263 ZFS_VERIFY_ZP(szp); 3264 3265 /* 3266 * If we are using project inheritance, means if the directory has 3267 * ZFS_PROJINHERIT set, then its descendant directories will inherit 3268 * not only the project ID, but also the ZFS_PROJINHERIT flag. Under 3269 * such case, we only allow hard link creation in our tree when the 3270 * project IDs are the same. 3271 */ 3272 if (tdzp->z_pflags & ZFS_PROJINHERIT && 3273 tdzp->z_projid != szp->z_projid) { 3274 ZFS_EXIT(zfsvfs); 3275 return (SET_ERROR(EXDEV)); 3276 } 3277 3278 /* 3279 * We check i_sb because snapshots and the ctldir must have different 3280 * super blocks. 3281 */ 3282 if (sip->i_sb != ZTOI(tdzp)->i_sb || zfsctl_is_node(sip)) { 3283 ZFS_EXIT(zfsvfs); 3284 return (SET_ERROR(EXDEV)); 3285 } 3286 3287 /* Prevent links to .zfs/shares files */ 3288 3289 if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 3290 &parent, sizeof (uint64_t))) != 0) { 3291 ZFS_EXIT(zfsvfs); 3292 return (error); 3293 } 3294 if (parent == zfsvfs->z_shares_dir) { 3295 ZFS_EXIT(zfsvfs); 3296 return (SET_ERROR(EPERM)); 3297 } 3298 3299 if (zfsvfs->z_utf8 && u8_validate(name, 3300 strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 3301 ZFS_EXIT(zfsvfs); 3302 return (SET_ERROR(EILSEQ)); 3303 } 3304 if (flags & FIGNORECASE) 3305 zf |= ZCILOOK; 3306 3307 /* 3308 * We do not support links between attributes and non-attributes 3309 * because of the potential security risk of creating links 3310 * into "normal" file space in order to circumvent restrictions 3311 * imposed in attribute space. 3312 */ 3313 if ((szp->z_pflags & ZFS_XATTR) != (tdzp->z_pflags & ZFS_XATTR)) { 3314 ZFS_EXIT(zfsvfs); 3315 return (SET_ERROR(EINVAL)); 3316 } 3317 3318 owner = zfs_fuid_map_id(zfsvfs, KUID_TO_SUID(sip->i_uid), 3319 cr, ZFS_OWNER); 3320 if (owner != crgetuid(cr) && secpolicy_basic_link(cr) != 0) { 3321 ZFS_EXIT(zfsvfs); 3322 return (SET_ERROR(EPERM)); 3323 } 3324 3325 if ((error = zfs_zaccess(tdzp, ACE_ADD_FILE, 0, B_FALSE, cr))) { 3326 ZFS_EXIT(zfsvfs); 3327 return (error); 3328 } 3329 3330 top: 3331 /* 3332 * Attempt to lock directory; fail if entry already exists. 3333 */ 3334 error = zfs_dirent_lock(&dl, tdzp, name, &tzp, zf, NULL, NULL); 3335 if (error) { 3336 ZFS_EXIT(zfsvfs); 3337 return (error); 3338 } 3339 3340 tx = dmu_tx_create(zfsvfs->z_os); 3341 dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 3342 dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, name); 3343 if (is_tmpfile) 3344 dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3345 3346 zfs_sa_upgrade_txholds(tx, szp); 3347 zfs_sa_upgrade_txholds(tx, tdzp); 3348 error = dmu_tx_assign(tx, (waited ? TXG_NOTHROTTLE : 0) | TXG_NOWAIT); 3349 if (error) { 3350 zfs_dirent_unlock(dl); 3351 if (error == ERESTART) { 3352 waited = B_TRUE; 3353 dmu_tx_wait(tx); 3354 dmu_tx_abort(tx); 3355 goto top; 3356 } 3357 dmu_tx_abort(tx); 3358 ZFS_EXIT(zfsvfs); 3359 return (error); 3360 } 3361 /* unmark z_unlinked so zfs_link_create will not reject */ 3362 if (is_tmpfile) 3363 szp->z_unlinked = B_FALSE; 3364 error = zfs_link_create(dl, szp, tx, 0); 3365 3366 if (error == 0) { 3367 uint64_t txtype = TX_LINK; 3368 /* 3369 * tmpfile is created to be in z_unlinkedobj, so remove it. 3370 * Also, we don't log in ZIL, because all previous file 3371 * operation on the tmpfile are ignored by ZIL. Instead we 3372 * always wait for txg to sync to make sure all previous 3373 * operation are sync safe. 3374 */ 3375 if (is_tmpfile) { 3376 VERIFY(zap_remove_int(zfsvfs->z_os, 3377 zfsvfs->z_unlinkedobj, szp->z_id, tx) == 0); 3378 } else { 3379 if (flags & FIGNORECASE) 3380 txtype |= TX_CI; 3381 zfs_log_link(zilog, tx, txtype, tdzp, szp, name); 3382 } 3383 } else if (is_tmpfile) { 3384 /* restore z_unlinked since when linking failed */ 3385 szp->z_unlinked = B_TRUE; 3386 } 3387 txg = dmu_tx_get_txg(tx); 3388 dmu_tx_commit(tx); 3389 3390 zfs_dirent_unlock(dl); 3391 3392 if (!is_tmpfile && zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3393 zil_commit(zilog, 0); 3394 3395 if (is_tmpfile && zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) 3396 txg_wait_synced(dmu_objset_pool(zfsvfs->z_os), txg); 3397 3398 zfs_znode_update_vfs(tdzp); 3399 zfs_znode_update_vfs(szp); 3400 ZFS_EXIT(zfsvfs); 3401 return (error); 3402 } 3403 3404 static void 3405 zfs_putpage_commit_cb(void *arg) 3406 { 3407 struct page *pp = arg; 3408 3409 ClearPageError(pp); 3410 end_page_writeback(pp); 3411 } 3412 3413 /* 3414 * Push a page out to disk, once the page is on stable storage the 3415 * registered commit callback will be run as notification of completion. 3416 * 3417 * IN: ip - page mapped for inode. 3418 * pp - page to push (page is locked) 3419 * wbc - writeback control data 3420 * 3421 * RETURN: 0 if success 3422 * error code if failure 3423 * 3424 * Timestamps: 3425 * ip - ctime|mtime updated 3426 */ 3427 /* ARGSUSED */ 3428 int 3429 zfs_putpage(struct inode *ip, struct page *pp, struct writeback_control *wbc) 3430 { 3431 znode_t *zp = ITOZ(ip); 3432 zfsvfs_t *zfsvfs = ITOZSB(ip); 3433 loff_t offset; 3434 loff_t pgoff; 3435 unsigned int pglen; 3436 dmu_tx_t *tx; 3437 caddr_t va; 3438 int err = 0; 3439 uint64_t mtime[2], ctime[2]; 3440 sa_bulk_attr_t bulk[3]; 3441 int cnt = 0; 3442 struct address_space *mapping; 3443 3444 ZFS_ENTER(zfsvfs); 3445 ZFS_VERIFY_ZP(zp); 3446 3447 ASSERT(PageLocked(pp)); 3448 3449 pgoff = page_offset(pp); /* Page byte-offset in file */ 3450 offset = i_size_read(ip); /* File length in bytes */ 3451 pglen = MIN(PAGE_SIZE, /* Page length in bytes */ 3452 P2ROUNDUP(offset, PAGE_SIZE)-pgoff); 3453 3454 /* Page is beyond end of file */ 3455 if (pgoff >= offset) { 3456 unlock_page(pp); 3457 ZFS_EXIT(zfsvfs); 3458 return (0); 3459 } 3460 3461 /* Truncate page length to end of file */ 3462 if (pgoff + pglen > offset) 3463 pglen = offset - pgoff; 3464 3465 #if 0 3466 /* 3467 * FIXME: Allow mmap writes past its quota. The correct fix 3468 * is to register a page_mkwrite() handler to count the page 3469 * against its quota when it is about to be dirtied. 3470 */ 3471 if (zfs_id_overblockquota(zfsvfs, DMU_USERUSED_OBJECT, 3472 KUID_TO_SUID(ip->i_uid)) || 3473 zfs_id_overblockquota(zfsvfs, DMU_GROUPUSED_OBJECT, 3474 KGID_TO_SGID(ip->i_gid)) || 3475 (zp->z_projid != ZFS_DEFAULT_PROJID && 3476 zfs_id_overblockquota(zfsvfs, DMU_PROJECTUSED_OBJECT, 3477 zp->z_projid))) { 3478 err = EDQUOT; 3479 } 3480 #endif 3481 3482 /* 3483 * The ordering here is critical and must adhere to the following 3484 * rules in order to avoid deadlocking in either zfs_read() or 3485 * zfs_free_range() due to a lock inversion. 3486 * 3487 * 1) The page must be unlocked prior to acquiring the range lock. 3488 * This is critical because zfs_read() calls find_lock_page() 3489 * which may block on the page lock while holding the range lock. 3490 * 3491 * 2) Before setting or clearing write back on a page the range lock 3492 * must be held in order to prevent a lock inversion with the 3493 * zfs_free_range() function. 3494 * 3495 * This presents a problem because upon entering this function the 3496 * page lock is already held. To safely acquire the range lock the 3497 * page lock must be dropped. This creates a window where another 3498 * process could truncate, invalidate, dirty, or write out the page. 3499 * 3500 * Therefore, after successfully reacquiring the range and page locks 3501 * the current page state is checked. In the common case everything 3502 * will be as is expected and it can be written out. However, if 3503 * the page state has changed it must be handled accordingly. 3504 */ 3505 mapping = pp->mapping; 3506 redirty_page_for_writepage(wbc, pp); 3507 unlock_page(pp); 3508 3509 zfs_locked_range_t *lr = zfs_rangelock_enter(&zp->z_rangelock, 3510 pgoff, pglen, RL_WRITER); 3511 lock_page(pp); 3512 3513 /* Page mapping changed or it was no longer dirty, we're done */ 3514 if (unlikely((mapping != pp->mapping) || !PageDirty(pp))) { 3515 unlock_page(pp); 3516 zfs_rangelock_exit(lr); 3517 ZFS_EXIT(zfsvfs); 3518 return (0); 3519 } 3520 3521 /* Another process started write block if required */ 3522 if (PageWriteback(pp)) { 3523 unlock_page(pp); 3524 zfs_rangelock_exit(lr); 3525 3526 if (wbc->sync_mode != WB_SYNC_NONE) { 3527 if (PageWriteback(pp)) 3528 wait_on_page_bit(pp, PG_writeback); 3529 } 3530 3531 ZFS_EXIT(zfsvfs); 3532 return (0); 3533 } 3534 3535 /* Clear the dirty flag the required locks are held */ 3536 if (!clear_page_dirty_for_io(pp)) { 3537 unlock_page(pp); 3538 zfs_rangelock_exit(lr); 3539 ZFS_EXIT(zfsvfs); 3540 return (0); 3541 } 3542 3543 /* 3544 * Counterpart for redirty_page_for_writepage() above. This page 3545 * was in fact not skipped and should not be counted as if it were. 3546 */ 3547 wbc->pages_skipped--; 3548 set_page_writeback(pp); 3549 unlock_page(pp); 3550 3551 tx = dmu_tx_create(zfsvfs->z_os); 3552 dmu_tx_hold_write(tx, zp->z_id, pgoff, pglen); 3553 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3554 zfs_sa_upgrade_txholds(tx, zp); 3555 3556 err = dmu_tx_assign(tx, TXG_NOWAIT); 3557 if (err != 0) { 3558 if (err == ERESTART) 3559 dmu_tx_wait(tx); 3560 3561 dmu_tx_abort(tx); 3562 __set_page_dirty_nobuffers(pp); 3563 ClearPageError(pp); 3564 end_page_writeback(pp); 3565 zfs_rangelock_exit(lr); 3566 ZFS_EXIT(zfsvfs); 3567 return (err); 3568 } 3569 3570 va = kmap(pp); 3571 ASSERT3U(pglen, <=, PAGE_SIZE); 3572 dmu_write(zfsvfs->z_os, zp->z_id, pgoff, pglen, va, tx); 3573 kunmap(pp); 3574 3575 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 3576 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 3577 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_FLAGS(zfsvfs), NULL, 3578 &zp->z_pflags, 8); 3579 3580 /* Preserve the mtime and ctime provided by the inode */ 3581 ZFS_TIME_ENCODE(&ip->i_mtime, mtime); 3582 ZFS_TIME_ENCODE(&ip->i_ctime, ctime); 3583 zp->z_atime_dirty = B_FALSE; 3584 zp->z_seq++; 3585 3586 err = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx); 3587 3588 zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, pgoff, pglen, 0, 3589 zfs_putpage_commit_cb, pp); 3590 dmu_tx_commit(tx); 3591 3592 zfs_rangelock_exit(lr); 3593 3594 if (wbc->sync_mode != WB_SYNC_NONE) { 3595 /* 3596 * Note that this is rarely called under writepages(), because 3597 * writepages() normally handles the entire commit for 3598 * performance reasons. 3599 */ 3600 zil_commit(zfsvfs->z_log, zp->z_id); 3601 } 3602 3603 ZFS_EXIT(zfsvfs); 3604 return (err); 3605 } 3606 3607 /* 3608 * Update the system attributes when the inode has been dirtied. For the 3609 * moment we only update the mode, atime, mtime, and ctime. 3610 */ 3611 int 3612 zfs_dirty_inode(struct inode *ip, int flags) 3613 { 3614 znode_t *zp = ITOZ(ip); 3615 zfsvfs_t *zfsvfs = ITOZSB(ip); 3616 dmu_tx_t *tx; 3617 uint64_t mode, atime[2], mtime[2], ctime[2]; 3618 sa_bulk_attr_t bulk[4]; 3619 int error = 0; 3620 int cnt = 0; 3621 3622 if (zfs_is_readonly(zfsvfs) || dmu_objset_is_snapshot(zfsvfs->z_os)) 3623 return (0); 3624 3625 ZFS_ENTER(zfsvfs); 3626 ZFS_VERIFY_ZP(zp); 3627 3628 #ifdef I_DIRTY_TIME 3629 /* 3630 * This is the lazytime semantic introduced in Linux 4.0 3631 * This flag will only be called from update_time when lazytime is set. 3632 * (Note, I_DIRTY_SYNC will also set if not lazytime) 3633 * Fortunately mtime and ctime are managed within ZFS itself, so we 3634 * only need to dirty atime. 3635 */ 3636 if (flags == I_DIRTY_TIME) { 3637 zp->z_atime_dirty = B_TRUE; 3638 goto out; 3639 } 3640 #endif 3641 3642 tx = dmu_tx_create(zfsvfs->z_os); 3643 3644 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3645 zfs_sa_upgrade_txholds(tx, zp); 3646 3647 error = dmu_tx_assign(tx, TXG_WAIT); 3648 if (error) { 3649 dmu_tx_abort(tx); 3650 goto out; 3651 } 3652 3653 mutex_enter(&zp->z_lock); 3654 zp->z_atime_dirty = B_FALSE; 3655 3656 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MODE(zfsvfs), NULL, &mode, 8); 3657 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_ATIME(zfsvfs), NULL, &atime, 16); 3658 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 3659 SA_ADD_BULK_ATTR(bulk, cnt, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 3660 3661 /* Preserve the mode, mtime and ctime provided by the inode */ 3662 ZFS_TIME_ENCODE(&ip->i_atime, atime); 3663 ZFS_TIME_ENCODE(&ip->i_mtime, mtime); 3664 ZFS_TIME_ENCODE(&ip->i_ctime, ctime); 3665 mode = ip->i_mode; 3666 3667 zp->z_mode = mode; 3668 3669 error = sa_bulk_update(zp->z_sa_hdl, bulk, cnt, tx); 3670 mutex_exit(&zp->z_lock); 3671 3672 dmu_tx_commit(tx); 3673 out: 3674 ZFS_EXIT(zfsvfs); 3675 return (error); 3676 } 3677 3678 /*ARGSUSED*/ 3679 void 3680 zfs_inactive(struct inode *ip) 3681 { 3682 znode_t *zp = ITOZ(ip); 3683 zfsvfs_t *zfsvfs = ITOZSB(ip); 3684 uint64_t atime[2]; 3685 int error; 3686 int need_unlock = 0; 3687 3688 /* Only read lock if we haven't already write locked, e.g. rollback */ 3689 if (!RW_WRITE_HELD(&zfsvfs->z_teardown_inactive_lock)) { 3690 need_unlock = 1; 3691 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 3692 } 3693 if (zp->z_sa_hdl == NULL) { 3694 if (need_unlock) 3695 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3696 return; 3697 } 3698 3699 if (zp->z_atime_dirty && zp->z_unlinked == B_FALSE) { 3700 dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3701 3702 dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 3703 zfs_sa_upgrade_txholds(tx, zp); 3704 error = dmu_tx_assign(tx, TXG_WAIT); 3705 if (error) { 3706 dmu_tx_abort(tx); 3707 } else { 3708 ZFS_TIME_ENCODE(&ip->i_atime, atime); 3709 mutex_enter(&zp->z_lock); 3710 (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 3711 (void *)&atime, sizeof (atime), tx); 3712 zp->z_atime_dirty = B_FALSE; 3713 mutex_exit(&zp->z_lock); 3714 dmu_tx_commit(tx); 3715 } 3716 } 3717 3718 zfs_zinactive(zp); 3719 if (need_unlock) 3720 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3721 } 3722 3723 /* 3724 * Fill pages with data from the disk. 3725 */ 3726 static int 3727 zfs_fillpage(struct inode *ip, struct page *pl[], int nr_pages) 3728 { 3729 znode_t *zp = ITOZ(ip); 3730 zfsvfs_t *zfsvfs = ITOZSB(ip); 3731 objset_t *os; 3732 struct page *cur_pp; 3733 u_offset_t io_off, total; 3734 size_t io_len; 3735 loff_t i_size; 3736 unsigned page_idx; 3737 int err; 3738 3739 os = zfsvfs->z_os; 3740 io_len = nr_pages << PAGE_SHIFT; 3741 i_size = i_size_read(ip); 3742 io_off = page_offset(pl[0]); 3743 3744 if (io_off + io_len > i_size) 3745 io_len = i_size - io_off; 3746 3747 /* 3748 * Iterate over list of pages and read each page individually. 3749 */ 3750 page_idx = 0; 3751 for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 3752 caddr_t va; 3753 3754 cur_pp = pl[page_idx++]; 3755 va = kmap(cur_pp); 3756 err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 3757 DMU_READ_PREFETCH); 3758 kunmap(cur_pp); 3759 if (err) { 3760 /* convert checksum errors into IO errors */ 3761 if (err == ECKSUM) 3762 err = SET_ERROR(EIO); 3763 return (err); 3764 } 3765 } 3766 3767 return (0); 3768 } 3769 3770 /* 3771 * Uses zfs_fillpage to read data from the file and fill the pages. 3772 * 3773 * IN: ip - inode of file to get data from. 3774 * pl - list of pages to read 3775 * nr_pages - number of pages to read 3776 * 3777 * RETURN: 0 on success, error code on failure. 3778 * 3779 * Timestamps: 3780 * vp - atime updated 3781 */ 3782 /* ARGSUSED */ 3783 int 3784 zfs_getpage(struct inode *ip, struct page *pl[], int nr_pages) 3785 { 3786 znode_t *zp = ITOZ(ip); 3787 zfsvfs_t *zfsvfs = ITOZSB(ip); 3788 int err; 3789 3790 if (pl == NULL) 3791 return (0); 3792 3793 ZFS_ENTER(zfsvfs); 3794 ZFS_VERIFY_ZP(zp); 3795 3796 err = zfs_fillpage(ip, pl, nr_pages); 3797 3798 ZFS_EXIT(zfsvfs); 3799 return (err); 3800 } 3801 3802 /* 3803 * Check ZFS specific permissions to memory map a section of a file. 3804 * 3805 * IN: ip - inode of the file to mmap 3806 * off - file offset 3807 * addrp - start address in memory region 3808 * len - length of memory region 3809 * vm_flags- address flags 3810 * 3811 * RETURN: 0 if success 3812 * error code if failure 3813 */ 3814 /*ARGSUSED*/ 3815 int 3816 zfs_map(struct inode *ip, offset_t off, caddr_t *addrp, size_t len, 3817 unsigned long vm_flags) 3818 { 3819 znode_t *zp = ITOZ(ip); 3820 zfsvfs_t *zfsvfs = ITOZSB(ip); 3821 3822 ZFS_ENTER(zfsvfs); 3823 ZFS_VERIFY_ZP(zp); 3824 3825 if ((vm_flags & VM_WRITE) && (zp->z_pflags & 3826 (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 3827 ZFS_EXIT(zfsvfs); 3828 return (SET_ERROR(EPERM)); 3829 } 3830 3831 if ((vm_flags & (VM_READ | VM_EXEC)) && 3832 (zp->z_pflags & ZFS_AV_QUARANTINED)) { 3833 ZFS_EXIT(zfsvfs); 3834 return (SET_ERROR(EACCES)); 3835 } 3836 3837 if (off < 0 || len > MAXOFFSET_T - off) { 3838 ZFS_EXIT(zfsvfs); 3839 return (SET_ERROR(ENXIO)); 3840 } 3841 3842 ZFS_EXIT(zfsvfs); 3843 return (0); 3844 } 3845 3846 /* 3847 * Free or allocate space in a file. Currently, this function only 3848 * supports the `F_FREESP' command. However, this command is somewhat 3849 * misnamed, as its functionality includes the ability to allocate as 3850 * well as free space. 3851 * 3852 * IN: zp - znode of file to free data in. 3853 * cmd - action to take (only F_FREESP supported). 3854 * bfp - section of file to free/alloc. 3855 * flag - current file open mode flags. 3856 * offset - current file offset. 3857 * cr - credentials of caller. 3858 * 3859 * RETURN: 0 on success, error code on failure. 3860 * 3861 * Timestamps: 3862 * zp - ctime|mtime updated 3863 */ 3864 /* ARGSUSED */ 3865 int 3866 zfs_space(znode_t *zp, int cmd, flock64_t *bfp, int flag, 3867 offset_t offset, cred_t *cr) 3868 { 3869 zfsvfs_t *zfsvfs = ZTOZSB(zp); 3870 uint64_t off, len; 3871 int error; 3872 3873 ZFS_ENTER(zfsvfs); 3874 ZFS_VERIFY_ZP(zp); 3875 3876 if (cmd != F_FREESP) { 3877 ZFS_EXIT(zfsvfs); 3878 return (SET_ERROR(EINVAL)); 3879 } 3880 3881 /* 3882 * Callers might not be able to detect properly that we are read-only, 3883 * so check it explicitly here. 3884 */ 3885 if (zfs_is_readonly(zfsvfs)) { 3886 ZFS_EXIT(zfsvfs); 3887 return (SET_ERROR(EROFS)); 3888 } 3889 3890 if (bfp->l_len < 0) { 3891 ZFS_EXIT(zfsvfs); 3892 return (SET_ERROR(EINVAL)); 3893 } 3894 3895 /* 3896 * Permissions aren't checked on Solaris because on this OS 3897 * zfs_space() can only be called with an opened file handle. 3898 * On Linux we can get here through truncate_range() which 3899 * operates directly on inodes, so we need to check access rights. 3900 */ 3901 if ((error = zfs_zaccess(zp, ACE_WRITE_DATA, 0, B_FALSE, cr))) { 3902 ZFS_EXIT(zfsvfs); 3903 return (error); 3904 } 3905 3906 off = bfp->l_start; 3907 len = bfp->l_len; /* 0 means from off to end of file */ 3908 3909 error = zfs_freesp(zp, off, len, flag, TRUE); 3910 3911 ZFS_EXIT(zfsvfs); 3912 return (error); 3913 } 3914 3915 /*ARGSUSED*/ 3916 int 3917 zfs_fid(struct inode *ip, fid_t *fidp) 3918 { 3919 znode_t *zp = ITOZ(ip); 3920 zfsvfs_t *zfsvfs = ITOZSB(ip); 3921 uint32_t gen; 3922 uint64_t gen64; 3923 uint64_t object = zp->z_id; 3924 zfid_short_t *zfid; 3925 int size, i, error; 3926 3927 ZFS_ENTER(zfsvfs); 3928 3929 if (fidp->fid_len < SHORT_FID_LEN) { 3930 fidp->fid_len = SHORT_FID_LEN; 3931 ZFS_EXIT(zfsvfs); 3932 return (SET_ERROR(ENOSPC)); 3933 } 3934 3935 ZFS_VERIFY_ZP(zp); 3936 3937 if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 3938 &gen64, sizeof (uint64_t))) != 0) { 3939 ZFS_EXIT(zfsvfs); 3940 return (error); 3941 } 3942 3943 gen = (uint32_t)gen64; 3944 3945 size = SHORT_FID_LEN; 3946 3947 zfid = (zfid_short_t *)fidp; 3948 3949 zfid->zf_len = size; 3950 3951 for (i = 0; i < sizeof (zfid->zf_object); i++) 3952 zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3953 3954 /* Must have a non-zero generation number to distinguish from .zfs */ 3955 if (gen == 0) 3956 gen = 1; 3957 for (i = 0; i < sizeof (zfid->zf_gen); i++) 3958 zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3959 3960 ZFS_EXIT(zfsvfs); 3961 return (0); 3962 } 3963 3964 #if defined(_KERNEL) 3965 EXPORT_SYMBOL(zfs_open); 3966 EXPORT_SYMBOL(zfs_close); 3967 EXPORT_SYMBOL(zfs_lookup); 3968 EXPORT_SYMBOL(zfs_create); 3969 EXPORT_SYMBOL(zfs_tmpfile); 3970 EXPORT_SYMBOL(zfs_remove); 3971 EXPORT_SYMBOL(zfs_mkdir); 3972 EXPORT_SYMBOL(zfs_rmdir); 3973 EXPORT_SYMBOL(zfs_readdir); 3974 EXPORT_SYMBOL(zfs_getattr_fast); 3975 EXPORT_SYMBOL(zfs_setattr); 3976 EXPORT_SYMBOL(zfs_rename); 3977 EXPORT_SYMBOL(zfs_symlink); 3978 EXPORT_SYMBOL(zfs_readlink); 3979 EXPORT_SYMBOL(zfs_link); 3980 EXPORT_SYMBOL(zfs_inactive); 3981 EXPORT_SYMBOL(zfs_space); 3982 EXPORT_SYMBOL(zfs_fid); 3983 EXPORT_SYMBOL(zfs_getpage); 3984 EXPORT_SYMBOL(zfs_putpage); 3985 EXPORT_SYMBOL(zfs_dirty_inode); 3986 EXPORT_SYMBOL(zfs_map); 3987 3988 /* BEGIN CSTYLED */ 3989 module_param(zfs_delete_blocks, ulong, 0644); 3990 MODULE_PARM_DESC(zfs_delete_blocks, "Delete files larger than N blocks async"); 3991 /* END CSTYLED */ 3992 3993 #endif 3994