1789Sahrens /* 2789Sahrens * CDDL HEADER START 3789Sahrens * 4789Sahrens * The contents of this file are subject to the terms of the 51460Smarks * Common Development and Distribution License (the "License"). 61460Smarks * You may not use this file except in compliance with the License. 7789Sahrens * 8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9789Sahrens * or http://www.opensolaris.org/os/licensing. 10789Sahrens * See the License for the specific language governing permissions 11789Sahrens * and limitations under the License. 12789Sahrens * 13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15789Sahrens * If applicable, add the following below this CDDL HEADER, with the 16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 18789Sahrens * 19789Sahrens * CDDL HEADER END 20789Sahrens */ 21789Sahrens /* 225771Sjp151216 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 264144Speteh /* Portions Copyright 2007 Jeremy Teo */ 274144Speteh 28789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 29789Sahrens 30789Sahrens #include <sys/types.h> 31789Sahrens #include <sys/param.h> 32789Sahrens #include <sys/time.h> 33789Sahrens #include <sys/systm.h> 34789Sahrens #include <sys/sysmacros.h> 35789Sahrens #include <sys/resource.h> 36789Sahrens #include <sys/vfs.h> 373898Srsb #include <sys/vfs_opreg.h> 38789Sahrens #include <sys/vnode.h> 39789Sahrens #include <sys/file.h> 40789Sahrens #include <sys/stat.h> 41789Sahrens #include <sys/kmem.h> 42789Sahrens #include <sys/taskq.h> 43789Sahrens #include <sys/uio.h> 44789Sahrens #include <sys/vmsystm.h> 45789Sahrens #include <sys/atomic.h> 462688Smaybee #include <sys/vm.h> 47789Sahrens #include <vm/seg_vn.h> 48789Sahrens #include <vm/pvn.h> 49789Sahrens #include <vm/as.h> 50789Sahrens #include <sys/mman.h> 51789Sahrens #include <sys/pathname.h> 52789Sahrens #include <sys/cmn_err.h> 53789Sahrens #include <sys/errno.h> 54789Sahrens #include <sys/unistd.h> 55789Sahrens #include <sys/zfs_dir.h> 56789Sahrens #include <sys/zfs_acl.h> 57789Sahrens #include <sys/zfs_ioctl.h> 58789Sahrens #include <sys/fs/zfs.h> 59789Sahrens #include <sys/dmu.h> 60789Sahrens #include <sys/spa.h> 61789Sahrens #include <sys/txg.h> 62789Sahrens #include <sys/dbuf.h> 63789Sahrens #include <sys/zap.h> 64789Sahrens #include <sys/dirent.h> 65789Sahrens #include <sys/policy.h> 66789Sahrens #include <sys/sunddi.h> 67789Sahrens #include <sys/filio.h> 68789Sahrens #include "fs/fs_subr.h" 69789Sahrens #include <sys/zfs_ctldir.h> 705331Samw #include <sys/zfs_fuid.h> 711484Sek110237 #include <sys/dnlc.h> 721669Sperrin #include <sys/zfs_rlock.h> 735331Samw #include <sys/extdirent.h> 745331Samw #include <sys/kidmap.h> 755331Samw #include <sys/cred_impl.h> 765663Sck153898 #include <sys/attr.h> 77789Sahrens 78789Sahrens /* 79789Sahrens * Programming rules. 80789Sahrens * 81789Sahrens * Each vnode op performs some logical unit of work. To do this, the ZPL must 82789Sahrens * properly lock its in-core state, create a DMU transaction, do the work, 83789Sahrens * record this work in the intent log (ZIL), commit the DMU transaction, 845331Samw * and wait for the intent log to commit if it is a synchronous operation. 855331Samw * Moreover, the vnode ops must work in both normal and log replay context. 86789Sahrens * The ordering of events is important to avoid deadlocks and references 87789Sahrens * to freed memory. The example below illustrates the following Big Rules: 88789Sahrens * 89789Sahrens * (1) A check must be made in each zfs thread for a mounted file system. 905367Sahrens * This is done avoiding races using ZFS_ENTER(zfsvfs). 915367Sahrens * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 925367Sahrens * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 935367Sahrens * can return EIO from the calling function. 94789Sahrens * 95789Sahrens * (2) VN_RELE() should always be the last thing except for zil_commit() 962638Sperrin * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 97789Sahrens * First, if it's the last reference, the vnode/znode 98789Sahrens * can be freed, so the zp may point to freed memory. Second, the last 99789Sahrens * reference will call zfs_zinactive(), which may induce a lot of work -- 1001669Sperrin * pushing cached pages (which acquires range locks) and syncing out 101789Sahrens * cached atime changes. Third, zfs_zinactive() may require a new tx, 102789Sahrens * which could deadlock the system if you were already holding one. 103789Sahrens * 1041757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 1051757Sperrin * as they can span dmu_tx_assign() calls. 1061757Sperrin * 1071757Sperrin * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign(). 108789Sahrens * In normal operation, this will be TXG_NOWAIT. During ZIL replay, 109789Sahrens * it will be a specific txg. Either way, dmu_tx_assign() never blocks. 110789Sahrens * This is critical because we don't want to block while holding locks. 111789Sahrens * Note, in particular, that if a lock is sometimes acquired before 112789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 113789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 114789Sahrens * 115789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 116789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 117789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 118789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 119789Sahrens * 120789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1212113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 122789Sahrens * 1231757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 124789Sahrens * before dropping locks. This ensures that the ordering of events 125789Sahrens * in the intent log matches the order in which they actually occurred. 126789Sahrens * 1271757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 128789Sahrens * regardless of whether there were any errors. 129789Sahrens * 1302638Sperrin * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 131789Sahrens * to ensure that synchronous semantics are provided when necessary. 132789Sahrens * 133789Sahrens * In general, this is how things should be ordered in each vnode op: 134789Sahrens * 135789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 136789Sahrens * top: 137789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 138789Sahrens * rw_enter(...); // grab any other locks you need 139789Sahrens * tx = dmu_tx_create(...); // get DMU tx 140789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 141789Sahrens * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign 142789Sahrens * if (error) { 143789Sahrens * rw_exit(...); // drop locks 144789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 145789Sahrens * VN_RELE(...); // release held vnodes 146789Sahrens * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1472113Sahrens * dmu_tx_wait(tx); 1482113Sahrens * dmu_tx_abort(tx); 149789Sahrens * goto top; 150789Sahrens * } 1512113Sahrens * dmu_tx_abort(tx); // abort DMU tx 152789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 153789Sahrens * return (error); // really out of space 154789Sahrens * } 155789Sahrens * error = do_real_work(); // do whatever this VOP does 156789Sahrens * if (error == 0) 1572638Sperrin * zfs_log_*(...); // on success, make ZIL entry 158789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 159789Sahrens * rw_exit(...); // drop locks 160789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 161789Sahrens * VN_RELE(...); // release held vnodes 1622638Sperrin * zil_commit(zilog, seq, foid); // synchronous when necessary 163789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 164789Sahrens * return (error); // done, report error 165789Sahrens */ 1665367Sahrens 167789Sahrens /* ARGSUSED */ 168789Sahrens static int 1695331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 170789Sahrens { 1713063Sperrin znode_t *zp = VTOZ(*vpp); 1723063Sperrin 1735331Samw if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) && 1745331Samw ((flag & FAPPEND) == 0)) { 1755331Samw return (EPERM); 1765331Samw } 1775331Samw 1785331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 1795331Samw ZTOV(zp)->v_type == VREG && 1805331Samw !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) && 1815331Samw zp->z_phys->zp_size > 0) 1825331Samw if (fs_vscan(*vpp, cr, 0) != 0) 1835331Samw return (EACCES); 1845331Samw 1853063Sperrin /* Keep a count of the synchronous opens in the znode */ 1863063Sperrin if (flag & (FSYNC | FDSYNC)) 1873063Sperrin atomic_inc_32(&zp->z_sync_cnt); 1885331Samw 189789Sahrens return (0); 190789Sahrens } 191789Sahrens 192789Sahrens /* ARGSUSED */ 193789Sahrens static int 1945331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 1955331Samw caller_context_t *ct) 196789Sahrens { 1973063Sperrin znode_t *zp = VTOZ(vp); 1983063Sperrin 1993063Sperrin /* Decrement the synchronous opens in the znode */ 2004339Sperrin if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 2013063Sperrin atomic_dec_32(&zp->z_sync_cnt); 2023063Sperrin 203789Sahrens /* 204789Sahrens * Clean up any locks held by this process on the vp. 205789Sahrens */ 206789Sahrens cleanlocks(vp, ddi_get_pid(), 0); 207789Sahrens cleanshares(vp, ddi_get_pid()); 208789Sahrens 2095331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 2105331Samw ZTOV(zp)->v_type == VREG && 2115331Samw !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) && 2125331Samw zp->z_phys->zp_size > 0) 2135331Samw VERIFY(fs_vscan(vp, cr, 1) == 0); 2145331Samw 215789Sahrens return (0); 216789Sahrens } 217789Sahrens 218789Sahrens /* 219789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 220789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 221789Sahrens */ 222789Sahrens static int 223789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 224789Sahrens { 225789Sahrens znode_t *zp = VTOZ(vp); 226789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 227789Sahrens uint64_t file_sz; 228789Sahrens int error; 229789Sahrens boolean_t hole; 230789Sahrens 231789Sahrens file_sz = zp->z_phys->zp_size; 232789Sahrens if (noff >= file_sz) { 233789Sahrens return (ENXIO); 234789Sahrens } 235789Sahrens 236789Sahrens if (cmd == _FIO_SEEK_HOLE) 237789Sahrens hole = B_TRUE; 238789Sahrens else 239789Sahrens hole = B_FALSE; 240789Sahrens 241789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 242789Sahrens 243789Sahrens /* end of file? */ 244789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 245789Sahrens /* 246789Sahrens * Handle the virtual hole at the end of file. 247789Sahrens */ 248789Sahrens if (hole) { 249789Sahrens *off = file_sz; 250789Sahrens return (0); 251789Sahrens } 252789Sahrens return (ENXIO); 253789Sahrens } 254789Sahrens 255789Sahrens if (noff < *off) 256789Sahrens return (error); 257789Sahrens *off = noff; 258789Sahrens return (error); 259789Sahrens } 260789Sahrens 261789Sahrens /* ARGSUSED */ 262789Sahrens static int 263789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 2645331Samw int *rvalp, caller_context_t *ct) 265789Sahrens { 266789Sahrens offset_t off; 267789Sahrens int error; 268789Sahrens zfsvfs_t *zfsvfs; 2695326Sek110237 znode_t *zp; 270789Sahrens 271789Sahrens switch (com) { 2724339Sperrin case _FIOFFS: 273789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 274789Sahrens 2751544Seschrock /* 2761544Seschrock * The following two ioctls are used by bfu. Faking out, 2771544Seschrock * necessary to avoid bfu errors. 2781544Seschrock */ 2794339Sperrin case _FIOGDIO: 2804339Sperrin case _FIOSDIO: 2811544Seschrock return (0); 2821544Seschrock 2834339Sperrin case _FIO_SEEK_DATA: 2844339Sperrin case _FIO_SEEK_HOLE: 285789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 286789Sahrens return (EFAULT); 287789Sahrens 2885326Sek110237 zp = VTOZ(vp); 2895326Sek110237 zfsvfs = zp->z_zfsvfs; 2905367Sahrens ZFS_ENTER(zfsvfs); 2915367Sahrens ZFS_VERIFY_ZP(zp); 292789Sahrens 293789Sahrens /* offset parameter is in/out */ 294789Sahrens error = zfs_holey(vp, com, &off); 295789Sahrens ZFS_EXIT(zfsvfs); 296789Sahrens if (error) 297789Sahrens return (error); 298789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 299789Sahrens return (EFAULT); 300789Sahrens return (0); 301789Sahrens } 302789Sahrens return (ENOTTY); 303789Sahrens } 304789Sahrens 305789Sahrens /* 306789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 307789Sahrens * between the DMU cache and the memory mapped pages. What this means: 308789Sahrens * 309789Sahrens * On Write: If we find a memory mapped page, we write to *both* 310789Sahrens * the page and the dmu buffer. 311789Sahrens * 312789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 313789Sahrens * the file is memory mapped. 314789Sahrens */ 315789Sahrens static int 3163638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx) 317789Sahrens { 318789Sahrens znode_t *zp = VTOZ(vp); 319789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 320789Sahrens int64_t start, off; 321789Sahrens int len = nbytes; 322789Sahrens int error = 0; 323789Sahrens 324789Sahrens start = uio->uio_loffset; 325789Sahrens off = start & PAGEOFFSET; 326789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 327789Sahrens page_t *pp; 328789Sahrens uint64_t bytes = MIN(PAGESIZE - off, len); 3293638Sbillm uint64_t woff = uio->uio_loffset; 330789Sahrens 331789Sahrens /* 332789Sahrens * We don't want a new page to "appear" in the middle of 333789Sahrens * the file update (because it may not get the write 334789Sahrens * update data), so we grab a lock to block 335789Sahrens * zfs_getpage(). 336789Sahrens */ 337789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 338789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 339789Sahrens caddr_t va; 340789Sahrens 341789Sahrens rw_exit(&zp->z_map_lock); 342789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 343789Sahrens error = uiomove(va+off, bytes, UIO_WRITE, uio); 344789Sahrens if (error == 0) { 345789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, 346789Sahrens woff, bytes, va+off, tx); 347789Sahrens } 348789Sahrens ppmapout(va); 349789Sahrens page_unlock(pp); 350789Sahrens } else { 351789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 3523638Sbillm uio, bytes, tx); 353789Sahrens rw_exit(&zp->z_map_lock); 354789Sahrens } 355789Sahrens len -= bytes; 356789Sahrens off = 0; 357789Sahrens if (error) 358789Sahrens break; 359789Sahrens } 360789Sahrens return (error); 361789Sahrens } 362789Sahrens 363789Sahrens /* 364789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 365789Sahrens * between the DMU cache and the memory mapped pages. What this means: 366789Sahrens * 367789Sahrens * On Read: We "read" preferentially from memory mapped pages, 368789Sahrens * else we default from the dmu buffer. 369789Sahrens * 370789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 371789Sahrens * the file is memory mapped. 372789Sahrens */ 373789Sahrens static int 3743638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio) 375789Sahrens { 3763638Sbillm znode_t *zp = VTOZ(vp); 3773638Sbillm objset_t *os = zp->z_zfsvfs->z_os; 3783638Sbillm int64_t start, off; 379789Sahrens int len = nbytes; 380789Sahrens int error = 0; 381789Sahrens 382789Sahrens start = uio->uio_loffset; 383789Sahrens off = start & PAGEOFFSET; 384789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 385789Sahrens page_t *pp; 3863638Sbillm uint64_t bytes = MIN(PAGESIZE - off, len); 3873638Sbillm 388789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 389789Sahrens caddr_t va; 390789Sahrens 3912688Smaybee va = ppmapin(pp, PROT_READ, (caddr_t)-1L); 392789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 393789Sahrens ppmapout(va); 394789Sahrens page_unlock(pp); 395789Sahrens } else { 3963638Sbillm error = dmu_read_uio(os, zp->z_id, uio, bytes); 397789Sahrens } 398789Sahrens len -= bytes; 399789Sahrens off = 0; 400789Sahrens if (error) 401789Sahrens break; 402789Sahrens } 403789Sahrens return (error); 404789Sahrens } 405789Sahrens 4063638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 407789Sahrens 408789Sahrens /* 409789Sahrens * Read bytes from specified file into supplied buffer. 410789Sahrens * 411789Sahrens * IN: vp - vnode of file to be read from. 412789Sahrens * uio - structure supplying read location, range info, 413789Sahrens * and return buffer. 414789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 415789Sahrens * cr - credentials of caller. 4165331Samw * ct - caller context 417789Sahrens * 418789Sahrens * OUT: uio - updated offset and range, buffer filled. 419789Sahrens * 420789Sahrens * RETURN: 0 if success 421789Sahrens * error code if failure 422789Sahrens * 423789Sahrens * Side Effects: 424789Sahrens * vp - atime updated if byte count > 0 425789Sahrens */ 426789Sahrens /* ARGSUSED */ 427789Sahrens static int 428789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 429789Sahrens { 430789Sahrens znode_t *zp = VTOZ(vp); 431789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4325326Sek110237 objset_t *os; 4333638Sbillm ssize_t n, nbytes; 4343638Sbillm int error; 4351669Sperrin rl_t *rl; 436789Sahrens 4375367Sahrens ZFS_ENTER(zfsvfs); 4385367Sahrens ZFS_VERIFY_ZP(zp); 4395326Sek110237 os = zfsvfs->z_os; 440789Sahrens 4415929Smarks if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) { 4425929Smarks ZFS_EXIT(zfsvfs); 4435929Smarks return (EACCES); 4445929Smarks } 4455929Smarks 446789Sahrens /* 447789Sahrens * Validate file offset 448789Sahrens */ 449789Sahrens if (uio->uio_loffset < (offset_t)0) { 450789Sahrens ZFS_EXIT(zfsvfs); 451789Sahrens return (EINVAL); 452789Sahrens } 453789Sahrens 454789Sahrens /* 455789Sahrens * Fasttrack empty reads 456789Sahrens */ 457789Sahrens if (uio->uio_resid == 0) { 458789Sahrens ZFS_EXIT(zfsvfs); 459789Sahrens return (0); 460789Sahrens } 461789Sahrens 462789Sahrens /* 4631669Sperrin * Check for mandatory locks 464789Sahrens */ 465789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 466789Sahrens if (error = chklock(vp, FREAD, 467789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 468789Sahrens ZFS_EXIT(zfsvfs); 469789Sahrens return (error); 470789Sahrens } 471789Sahrens } 472789Sahrens 473789Sahrens /* 474789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 475789Sahrens */ 4762638Sperrin if (ioflag & FRSYNC) 4772638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 478789Sahrens 479789Sahrens /* 4801669Sperrin * Lock the range against changes. 481789Sahrens */ 4821669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4831669Sperrin 484789Sahrens /* 485789Sahrens * If we are reading past end-of-file we can skip 486789Sahrens * to the end; but we might still need to set atime. 487789Sahrens */ 488789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 489789Sahrens error = 0; 490789Sahrens goto out; 491789Sahrens } 492789Sahrens 4933638Sbillm ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 4943638Sbillm n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 4953638Sbillm 4963638Sbillm while (n > 0) { 4973638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 4983638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 4993638Sbillm 5003638Sbillm if (vn_has_cached_data(vp)) 5013638Sbillm error = mappedread(vp, nbytes, uio); 5023638Sbillm else 5033638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 504*7294Sperrin if (error) { 505*7294Sperrin /* convert checksum errors into IO errors */ 506*7294Sperrin if (error == ECKSUM) 507*7294Sperrin error = EIO; 5083638Sbillm break; 509*7294Sperrin } 5103638Sbillm 5113638Sbillm n -= nbytes; 512789Sahrens } 5133638Sbillm 514789Sahrens out: 5152237Smaybee zfs_range_unlock(rl); 516789Sahrens 517789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 518789Sahrens ZFS_EXIT(zfsvfs); 519789Sahrens return (error); 520789Sahrens } 521789Sahrens 522789Sahrens /* 523789Sahrens * Fault in the pages of the first n bytes specified by the uio structure. 524789Sahrens * 1 byte in each page is touched and the uio struct is unmodified. 525789Sahrens * Any error will exit this routine as this is only a best 526789Sahrens * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 527789Sahrens */ 528789Sahrens static void 529789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio) 530789Sahrens { 531789Sahrens struct iovec *iov; 532789Sahrens ulong_t cnt, incr; 533789Sahrens caddr_t p; 534789Sahrens uint8_t tmp; 535789Sahrens 536789Sahrens iov = uio->uio_iov; 537789Sahrens 538789Sahrens while (n) { 539789Sahrens cnt = MIN(iov->iov_len, n); 540789Sahrens if (cnt == 0) { 541789Sahrens /* empty iov entry */ 542789Sahrens iov++; 543789Sahrens continue; 544789Sahrens } 545789Sahrens n -= cnt; 546789Sahrens /* 547789Sahrens * touch each page in this segment. 548789Sahrens */ 549789Sahrens p = iov->iov_base; 550789Sahrens while (cnt) { 551789Sahrens switch (uio->uio_segflg) { 552789Sahrens case UIO_USERSPACE: 553789Sahrens case UIO_USERISPACE: 554789Sahrens if (fuword8(p, &tmp)) 555789Sahrens return; 556789Sahrens break; 557789Sahrens case UIO_SYSSPACE: 558789Sahrens if (kcopy(p, &tmp, 1)) 559789Sahrens return; 560789Sahrens break; 561789Sahrens } 562789Sahrens incr = MIN(cnt, PAGESIZE); 563789Sahrens p += incr; 564789Sahrens cnt -= incr; 565789Sahrens } 566789Sahrens /* 567789Sahrens * touch the last byte in case it straddles a page. 568789Sahrens */ 569789Sahrens p--; 570789Sahrens switch (uio->uio_segflg) { 571789Sahrens case UIO_USERSPACE: 572789Sahrens case UIO_USERISPACE: 573789Sahrens if (fuword8(p, &tmp)) 574789Sahrens return; 575789Sahrens break; 576789Sahrens case UIO_SYSSPACE: 577789Sahrens if (kcopy(p, &tmp, 1)) 578789Sahrens return; 579789Sahrens break; 580789Sahrens } 581789Sahrens iov++; 582789Sahrens } 583789Sahrens } 584789Sahrens 585789Sahrens /* 586789Sahrens * Write the bytes to a file. 587789Sahrens * 588789Sahrens * IN: vp - vnode of file to be written to. 589789Sahrens * uio - structure supplying write location, range info, 590789Sahrens * and data buffer. 591789Sahrens * ioflag - FAPPEND flag set if in append mode. 592789Sahrens * cr - credentials of caller. 5935331Samw * ct - caller context (NFS/CIFS fem monitor only) 594789Sahrens * 595789Sahrens * OUT: uio - updated offset and range. 596789Sahrens * 597789Sahrens * RETURN: 0 if success 598789Sahrens * error code if failure 599789Sahrens * 600789Sahrens * Timestamps: 601789Sahrens * vp - ctime|mtime updated if byte count > 0 602789Sahrens */ 603789Sahrens /* ARGSUSED */ 604789Sahrens static int 605789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 606789Sahrens { 607789Sahrens znode_t *zp = VTOZ(vp); 608789Sahrens rlim64_t limit = uio->uio_llimit; 609789Sahrens ssize_t start_resid = uio->uio_resid; 610789Sahrens ssize_t tx_bytes; 611789Sahrens uint64_t end_size; 612789Sahrens dmu_tx_t *tx; 613789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 6145326Sek110237 zilog_t *zilog; 615789Sahrens offset_t woff; 616789Sahrens ssize_t n, nbytes; 6171669Sperrin rl_t *rl; 618789Sahrens int max_blksz = zfsvfs->z_max_blksz; 6196743Smarks uint64_t pflags; 6201669Sperrin int error; 621789Sahrens 622789Sahrens /* 623789Sahrens * Fasttrack empty write 624789Sahrens */ 6251669Sperrin n = start_resid; 626789Sahrens if (n == 0) 627789Sahrens return (0); 628789Sahrens 6291669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6301669Sperrin limit = MAXOFFSET_T; 6311669Sperrin 6325367Sahrens ZFS_ENTER(zfsvfs); 6335367Sahrens ZFS_VERIFY_ZP(zp); 6346743Smarks 6356743Smarks /* 6366743Smarks * If immutable or not appending then return EPERM 6376743Smarks */ 6386743Smarks pflags = zp->z_phys->zp_flags; 6396743Smarks if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 6406743Smarks ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 6416743Smarks (uio->uio_loffset < zp->z_phys->zp_size))) { 6426743Smarks ZFS_EXIT(zfsvfs); 6436743Smarks return (EPERM); 6446743Smarks } 6456743Smarks 6465326Sek110237 zilog = zfsvfs->z_log; 647789Sahrens 648789Sahrens /* 6492237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6501669Sperrin * don't hold up txg. 651789Sahrens */ 6522237Smaybee zfs_prefault_write(n, uio); 653789Sahrens 654789Sahrens /* 655789Sahrens * If in append mode, set the io offset pointer to eof. 656789Sahrens */ 6571669Sperrin if (ioflag & FAPPEND) { 6581669Sperrin /* 6591669Sperrin * Range lock for a file append: 6601669Sperrin * The value for the start of range will be determined by 6611669Sperrin * zfs_range_lock() (to guarantee append semantics). 6621669Sperrin * If this write will cause the block size to increase, 6631669Sperrin * zfs_range_lock() will lock the entire file, so we must 6641669Sperrin * later reduce the range after we grow the block size. 6651669Sperrin */ 6661669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6671669Sperrin if (rl->r_len == UINT64_MAX) { 6681669Sperrin /* overlocked, zp_size can't change */ 6691669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6701669Sperrin } else { 6711669Sperrin woff = uio->uio_loffset = rl->r_off; 6721669Sperrin } 673789Sahrens } else { 674789Sahrens woff = uio->uio_loffset; 675789Sahrens /* 676789Sahrens * Validate file offset 677789Sahrens */ 678789Sahrens if (woff < 0) { 679789Sahrens ZFS_EXIT(zfsvfs); 680789Sahrens return (EINVAL); 681789Sahrens } 682789Sahrens 683789Sahrens /* 6841669Sperrin * If we need to grow the block size then zfs_range_lock() 6851669Sperrin * will lock a wider range than we request here. 6861669Sperrin * Later after growing the block size we reduce the range. 687789Sahrens */ 6881669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 689789Sahrens } 690789Sahrens 691789Sahrens if (woff >= limit) { 6923638Sbillm zfs_range_unlock(rl); 6933638Sbillm ZFS_EXIT(zfsvfs); 6943638Sbillm return (EFBIG); 695789Sahrens } 696789Sahrens 697789Sahrens if ((woff + n) > limit || woff > (limit - n)) 698789Sahrens n = limit - woff; 699789Sahrens 700789Sahrens /* 7011669Sperrin * Check for mandatory locks 702789Sahrens */ 703789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 7043638Sbillm (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 7053638Sbillm zfs_range_unlock(rl); 7063638Sbillm ZFS_EXIT(zfsvfs); 7073638Sbillm return (error); 7083638Sbillm } 7091669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 710789Sahrens 7111669Sperrin /* 7123638Sbillm * Write the file in reasonable size chunks. Each chunk is written 7133638Sbillm * in a separate transaction; this keeps the intent log records small 7143638Sbillm * and allows us to do more fine-grained space accounting. 715789Sahrens */ 716789Sahrens while (n > 0) { 717789Sahrens /* 7183638Sbillm * Start a transaction. 719789Sahrens */ 720789Sahrens woff = uio->uio_loffset; 721789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 722789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 723789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 724789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 725789Sahrens if (error) { 726789Sahrens if (error == ERESTART && 727789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 7282113Sahrens dmu_tx_wait(tx); 7292113Sahrens dmu_tx_abort(tx); 7303638Sbillm continue; 731789Sahrens } 7322113Sahrens dmu_tx_abort(tx); 7333638Sbillm break; 7343638Sbillm } 7353638Sbillm 7363638Sbillm /* 7373638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 7383638Sbillm * and then reduce the lock range. This will only happen 7393638Sbillm * on the first iteration since zfs_range_reduce() will 7403638Sbillm * shrink down r_len to the appropriate size. 7413638Sbillm */ 7423638Sbillm if (rl->r_len == UINT64_MAX) { 7433638Sbillm uint64_t new_blksz; 7443638Sbillm 7453638Sbillm if (zp->z_blksz > max_blksz) { 7463638Sbillm ASSERT(!ISP2(zp->z_blksz)); 7473638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 7483638Sbillm } else { 7493638Sbillm new_blksz = MIN(end_size, max_blksz); 7503638Sbillm } 7513638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 7523638Sbillm zfs_range_reduce(rl, woff, n); 7533638Sbillm } 7543638Sbillm 7553638Sbillm /* 7563638Sbillm * XXX - should we really limit each write to z_max_blksz? 7573638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 7583638Sbillm */ 7593638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 7603638Sbillm rw_enter(&zp->z_map_lock, RW_READER); 7613638Sbillm 7623638Sbillm tx_bytes = uio->uio_resid; 7633638Sbillm if (vn_has_cached_data(vp)) { 7643638Sbillm rw_exit(&zp->z_map_lock); 7653638Sbillm error = mappedwrite(vp, nbytes, uio, tx); 7663638Sbillm } else { 7673638Sbillm error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 7683638Sbillm uio, nbytes, tx); 7693638Sbillm rw_exit(&zp->z_map_lock); 770789Sahrens } 7713638Sbillm tx_bytes -= uio->uio_resid; 7723638Sbillm 7733638Sbillm /* 7743638Sbillm * If we made no progress, we're done. If we made even 7753638Sbillm * partial progress, update the znode and ZIL accordingly. 7763638Sbillm */ 7773638Sbillm if (tx_bytes == 0) { 7783897Smaybee dmu_tx_commit(tx); 7793638Sbillm ASSERT(error != 0); 7803638Sbillm break; 7813638Sbillm } 7823638Sbillm 783789Sahrens /* 7843638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 7853638Sbillm * privileged and at least one of the excute bits is set. 7863638Sbillm * 7873638Sbillm * It would be nice to to this after all writes have 7883638Sbillm * been done, but that would still expose the ISUID/ISGID 7893638Sbillm * to another app after the partial write is committed. 7905331Samw * 7915331Samw * Note: we don't call zfs_fuid_map_id() here because 7925331Samw * user 0 is not an ephemeral uid. 793789Sahrens */ 7943638Sbillm mutex_enter(&zp->z_acl_lock); 7953638Sbillm if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7963638Sbillm (S_IXUSR >> 6))) != 0 && 7973638Sbillm (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7983638Sbillm secpolicy_vnode_setid_retain(cr, 7993638Sbillm (zp->z_phys->zp_mode & S_ISUID) != 0 && 8003638Sbillm zp->z_phys->zp_uid == 0) != 0) { 8014339Sperrin zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 8023638Sbillm } 8033638Sbillm mutex_exit(&zp->z_acl_lock); 8043638Sbillm 8053638Sbillm /* 8063638Sbillm * Update time stamp. NOTE: This marks the bonus buffer as 8073638Sbillm * dirty, so we don't have to do it again for zp_size. 8083638Sbillm */ 8093638Sbillm zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 8103638Sbillm 8113638Sbillm /* 8123638Sbillm * Update the file size (zp_size) if it has changed; 8133638Sbillm * account for possible concurrent updates. 8143638Sbillm */ 8153638Sbillm while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 816789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 817789Sahrens uio->uio_loffset); 8183638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 8193638Sbillm dmu_tx_commit(tx); 8203638Sbillm 8213638Sbillm if (error != 0) 8223638Sbillm break; 8233638Sbillm ASSERT(tx_bytes == nbytes); 8243638Sbillm n -= nbytes; 825789Sahrens } 826789Sahrens 8272237Smaybee zfs_range_unlock(rl); 828789Sahrens 829789Sahrens /* 830789Sahrens * If we're in replay mode, or we made no progress, return error. 831789Sahrens * Otherwise, it's at least a partial write, so it's successful. 832789Sahrens */ 833789Sahrens if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 834789Sahrens ZFS_EXIT(zfsvfs); 835789Sahrens return (error); 836789Sahrens } 837789Sahrens 8382638Sperrin if (ioflag & (FSYNC | FDSYNC)) 8392638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 840789Sahrens 841789Sahrens ZFS_EXIT(zfsvfs); 842789Sahrens return (0); 843789Sahrens } 844789Sahrens 8452237Smaybee void 8463063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd) 8472237Smaybee { 8483063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 8493063Sperrin rl_t *rl = zgd->zgd_rl; 8502237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 8512237Smaybee 8523063Sperrin dmu_buf_rele(db, vzgd); 8532237Smaybee zfs_range_unlock(rl); 8542237Smaybee VN_RELE(vp); 8555688Sbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 8563063Sperrin kmem_free(zgd, sizeof (zgd_t)); 8572237Smaybee } 8582237Smaybee 859789Sahrens /* 860789Sahrens * Get data to generate a TX_WRITE intent log record. 861789Sahrens */ 862789Sahrens int 8632237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 864789Sahrens { 865789Sahrens zfsvfs_t *zfsvfs = arg; 866789Sahrens objset_t *os = zfsvfs->z_os; 867789Sahrens znode_t *zp; 868789Sahrens uint64_t off = lr->lr_offset; 8692237Smaybee dmu_buf_t *db; 8701669Sperrin rl_t *rl; 8713063Sperrin zgd_t *zgd; 8723638Sbillm int dlen = lr->lr_length; /* length of user data */ 873789Sahrens int error = 0; 874789Sahrens 8753063Sperrin ASSERT(zio); 876789Sahrens ASSERT(dlen != 0); 877789Sahrens 878789Sahrens /* 8791669Sperrin * Nothing to do if the file has been removed 880789Sahrens */ 881789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 882789Sahrens return (ENOENT); 8833461Sahrens if (zp->z_unlinked) { 884789Sahrens VN_RELE(ZTOV(zp)); 885789Sahrens return (ENOENT); 886789Sahrens } 887789Sahrens 888789Sahrens /* 889789Sahrens * Write records come in two flavors: immediate and indirect. 890789Sahrens * For small writes it's cheaper to store the data with the 891789Sahrens * log record (immediate); for large writes it's cheaper to 892789Sahrens * sync the data and get a pointer to it (indirect) so that 893789Sahrens * we don't have to write the data twice. 894789Sahrens */ 8951669Sperrin if (buf != NULL) { /* immediate write */ 8961669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 8971669Sperrin /* test for truncation needs to be done while range locked */ 8981669Sperrin if (off >= zp->z_phys->zp_size) { 8991669Sperrin error = ENOENT; 9001669Sperrin goto out; 9011669Sperrin } 9022449Smaybee VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 9031669Sperrin } else { /* indirect write */ 9041669Sperrin uint64_t boff; /* block starting offset */ 9051669Sperrin 906789Sahrens /* 9071669Sperrin * Have to lock the whole block to ensure when it's 9081669Sperrin * written out and it's checksum is being calculated 9091669Sperrin * that no one can change the data. We need to re-check 9101669Sperrin * blocksize after we get the lock in case it's changed! 911789Sahrens */ 9121669Sperrin for (;;) { 9131941Sperrin if (ISP2(zp->z_blksz)) { 9141941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 9151941Sperrin uint64_t); 9161941Sperrin } else { 9171941Sperrin boff = 0; 9181941Sperrin } 9191669Sperrin dlen = zp->z_blksz; 9201669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 9211669Sperrin if (zp->z_blksz == dlen) 9221669Sperrin break; 9232237Smaybee zfs_range_unlock(rl); 9241669Sperrin } 9251669Sperrin /* test for truncation needs to be done while range locked */ 9261669Sperrin if (off >= zp->z_phys->zp_size) { 9271669Sperrin error = ENOENT; 9281669Sperrin goto out; 9291669Sperrin } 9303063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 9313063Sperrin zgd->zgd_rl = rl; 9323063Sperrin zgd->zgd_zilog = zfsvfs->z_log; 9333063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 9343063Sperrin VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 9352237Smaybee ASSERT(boff == db->db_offset); 9362237Smaybee lr->lr_blkoff = off - boff; 9372237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 9383063Sperrin lr->lr_common.lrc_txg, zfs_get_done, zgd); 9394709Smaybee ASSERT((error && error != EINPROGRESS) || 9404709Smaybee lr->lr_length <= zp->z_blksz); 9415688Sbonwick if (error == 0) 9425688Sbonwick zil_add_block(zfsvfs->z_log, &lr->lr_blkptr); 9432237Smaybee /* 9442237Smaybee * If we get EINPROGRESS, then we need to wait for a 9452237Smaybee * write IO initiated by dmu_sync() to complete before 9462638Sperrin * we can release this dbuf. We will finish everything 9472237Smaybee * up in the zfs_get_done() callback. 9482237Smaybee */ 9492237Smaybee if (error == EINPROGRESS) 9502237Smaybee return (0); 9513063Sperrin dmu_buf_rele(db, zgd); 9523063Sperrin kmem_free(zgd, sizeof (zgd_t)); 953789Sahrens } 9541669Sperrin out: 9552237Smaybee zfs_range_unlock(rl); 956789Sahrens VN_RELE(ZTOV(zp)); 957789Sahrens return (error); 958789Sahrens } 959789Sahrens 960789Sahrens /*ARGSUSED*/ 961789Sahrens static int 9625331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 9635331Samw caller_context_t *ct) 964789Sahrens { 965789Sahrens znode_t *zp = VTOZ(vp); 966789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 967789Sahrens int error; 968789Sahrens 9695367Sahrens ZFS_ENTER(zfsvfs); 9705367Sahrens ZFS_VERIFY_ZP(zp); 9715331Samw 9725331Samw if (flag & V_ACE_MASK) 9735331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 9745331Samw else 9755331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 9765331Samw 977789Sahrens ZFS_EXIT(zfsvfs); 978789Sahrens return (error); 979789Sahrens } 980789Sahrens 981789Sahrens /* 982789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 983789Sahrens * If it exists, return a held vnode reference for it. 984789Sahrens * 985789Sahrens * IN: dvp - vnode of directory to search. 986789Sahrens * nm - name of entry to lookup. 987789Sahrens * pnp - full pathname to lookup [UNUSED]. 988789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 989789Sahrens * rdir - root directory vnode [UNUSED]. 990789Sahrens * cr - credentials of caller. 9915331Samw * ct - caller context 9925331Samw * direntflags - directory lookup flags 9935331Samw * realpnp - returned pathname. 994789Sahrens * 995789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 996789Sahrens * 997789Sahrens * RETURN: 0 if success 998789Sahrens * error code if failure 999789Sahrens * 1000789Sahrens * Timestamps: 1001789Sahrens * NA 1002789Sahrens */ 1003789Sahrens /* ARGSUSED */ 1004789Sahrens static int 1005789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 10065331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 10075331Samw int *direntflags, pathname_t *realpnp) 1008789Sahrens { 1009789Sahrens znode_t *zdp = VTOZ(dvp); 1010789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 1011789Sahrens int error; 1012789Sahrens 10135367Sahrens ZFS_ENTER(zfsvfs); 10145367Sahrens ZFS_VERIFY_ZP(zdp); 1015789Sahrens 1016789Sahrens *vpp = NULL; 1017789Sahrens 1018789Sahrens if (flags & LOOKUP_XATTR) { 1019789Sahrens /* 10203234Sck153898 * If the xattr property is off, refuse the lookup request. 10213234Sck153898 */ 10223234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 10233234Sck153898 ZFS_EXIT(zfsvfs); 10243234Sck153898 return (EINVAL); 10253234Sck153898 } 10263234Sck153898 10273234Sck153898 /* 1028789Sahrens * We don't allow recursive attributes.. 1029789Sahrens * Maybe someday we will. 1030789Sahrens */ 1031789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1032789Sahrens ZFS_EXIT(zfsvfs); 1033789Sahrens return (EINVAL); 1034789Sahrens } 1035789Sahrens 10363280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1037789Sahrens ZFS_EXIT(zfsvfs); 1038789Sahrens return (error); 1039789Sahrens } 1040789Sahrens 1041789Sahrens /* 1042789Sahrens * Do we have permission to get into attribute directory? 1043789Sahrens */ 1044789Sahrens 10455331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 10465331Samw B_FALSE, cr)) { 1047789Sahrens VN_RELE(*vpp); 10485331Samw *vpp = NULL; 1049789Sahrens } 1050789Sahrens 1051789Sahrens ZFS_EXIT(zfsvfs); 1052789Sahrens return (error); 1053789Sahrens } 1054789Sahrens 10551512Sek110237 if (dvp->v_type != VDIR) { 10561512Sek110237 ZFS_EXIT(zfsvfs); 10571460Smarks return (ENOTDIR); 10581512Sek110237 } 10591460Smarks 1060789Sahrens /* 1061789Sahrens * Check accessibility of directory. 1062789Sahrens */ 1063789Sahrens 10645331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1065789Sahrens ZFS_EXIT(zfsvfs); 1066789Sahrens return (error); 1067789Sahrens } 1068789Sahrens 10695498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 10705331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 10715331Samw ZFS_EXIT(zfsvfs); 10725331Samw return (EILSEQ); 10735331Samw } 10745331Samw 10755331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 10765331Samw if (error == 0) { 1077789Sahrens /* 1078789Sahrens * Convert device special files 1079789Sahrens */ 1080789Sahrens if (IS_DEVVP(*vpp)) { 1081789Sahrens vnode_t *svp; 1082789Sahrens 1083789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1084789Sahrens VN_RELE(*vpp); 1085789Sahrens if (svp == NULL) 1086789Sahrens error = ENOSYS; 1087789Sahrens else 1088789Sahrens *vpp = svp; 1089789Sahrens } 1090789Sahrens } 1091789Sahrens 1092789Sahrens ZFS_EXIT(zfsvfs); 1093789Sahrens return (error); 1094789Sahrens } 1095789Sahrens 1096789Sahrens /* 1097789Sahrens * Attempt to create a new entry in a directory. If the entry 1098789Sahrens * already exists, truncate the file if permissible, else return 1099789Sahrens * an error. Return the vp of the created or trunc'd file. 1100789Sahrens * 1101789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1102789Sahrens * name - name of new file entry. 1103789Sahrens * vap - attributes of new file. 1104789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1105789Sahrens * mode - mode to open file with. 1106789Sahrens * cr - credentials of caller. 1107789Sahrens * flag - large file flag [UNUSED]. 11085331Samw * ct - caller context 11095331Samw * vsecp - ACL to be set 1110789Sahrens * 1111789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1112789Sahrens * 1113789Sahrens * RETURN: 0 if success 1114789Sahrens * error code if failure 1115789Sahrens * 1116789Sahrens * Timestamps: 1117789Sahrens * dvp - ctime|mtime updated if new entry created 1118789Sahrens * vp - ctime|mtime always, atime if new 1119789Sahrens */ 11205331Samw 1121789Sahrens /* ARGSUSED */ 1122789Sahrens static int 1123789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 11245331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 11255331Samw vsecattr_t *vsecp) 1126789Sahrens { 1127789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1128789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 11295326Sek110237 zilog_t *zilog; 11305326Sek110237 objset_t *os; 1131789Sahrens zfs_dirlock_t *dl; 1132789Sahrens dmu_tx_t *tx; 1133789Sahrens int error; 11345331Samw zfs_acl_t *aclp = NULL; 11355331Samw zfs_fuid_info_t *fuidp = NULL; 11365331Samw 11375331Samw /* 11385331Samw * If we have an ephemeral id, ACL, or XVATTR then 11395331Samw * make sure file system is at proper version 11405331Samw */ 11415331Samw 11425331Samw if (zfsvfs->z_use_fuids == B_FALSE && 11435331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 11445331Samw IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr)))) 11455331Samw return (EINVAL); 1146789Sahrens 11475367Sahrens ZFS_ENTER(zfsvfs); 11485367Sahrens ZFS_VERIFY_ZP(dzp); 11495326Sek110237 os = zfsvfs->z_os; 11505326Sek110237 zilog = zfsvfs->z_log; 1151789Sahrens 11525498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 11535331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 11545331Samw ZFS_EXIT(zfsvfs); 11555331Samw return (EILSEQ); 11565331Samw } 11575331Samw 11585331Samw if (vap->va_mask & AT_XVATTR) { 11595331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 11605331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 11615331Samw ZFS_EXIT(zfsvfs); 11625331Samw return (error); 11635331Samw } 11645331Samw } 1165789Sahrens top: 1166789Sahrens *vpp = NULL; 1167789Sahrens 1168789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1169789Sahrens vap->va_mode &= ~VSVTX; 1170789Sahrens 1171789Sahrens if (*name == '\0') { 1172789Sahrens /* 1173789Sahrens * Null component name refers to the directory itself. 1174789Sahrens */ 1175789Sahrens VN_HOLD(dvp); 1176789Sahrens zp = dzp; 1177789Sahrens dl = NULL; 1178789Sahrens error = 0; 1179789Sahrens } else { 1180789Sahrens /* possible VN_HOLD(zp) */ 11815331Samw int zflg = 0; 11825331Samw 11835331Samw if (flag & FIGNORECASE) 11845331Samw zflg |= ZCILOOK; 11855331Samw 11865331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 11875331Samw NULL, NULL); 11885331Samw if (error) { 1189789Sahrens if (strcmp(name, "..") == 0) 1190789Sahrens error = EISDIR; 1191789Sahrens ZFS_EXIT(zfsvfs); 11925331Samw if (aclp) 11935331Samw zfs_acl_free(aclp); 1194789Sahrens return (error); 1195789Sahrens } 1196789Sahrens } 11975331Samw if (vsecp && aclp == NULL) { 11985331Samw error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp); 11995331Samw if (error) { 12005331Samw ZFS_EXIT(zfsvfs); 12015331Samw if (dl) 12025331Samw zfs_dirent_unlock(dl); 12035331Samw return (error); 12045331Samw } 12055331Samw } 1206789Sahrens 1207789Sahrens if (zp == NULL) { 12085331Samw uint64_t txtype; 12095331Samw 1210789Sahrens /* 1211789Sahrens * Create a new file object and update the directory 1212789Sahrens * to reference it. 1213789Sahrens */ 12145331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1215789Sahrens goto out; 1216789Sahrens } 1217789Sahrens 1218789Sahrens /* 1219789Sahrens * We only support the creation of regular files in 1220789Sahrens * extended attribute directories. 1221789Sahrens */ 1222789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1223789Sahrens (vap->va_type != VREG)) { 1224789Sahrens error = EINVAL; 1225789Sahrens goto out; 1226789Sahrens } 1227789Sahrens 1228789Sahrens tx = dmu_tx_create(os); 1229789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 12305959Smarks if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(crgetuid(cr)) || 12315959Smarks IS_EPHEMERAL(crgetgid(cr))) { 12325824Smarks if (zfsvfs->z_fuid_obj == 0) { 12335824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 12345824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 12355824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 12365824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 12375824Smarks FALSE, NULL); 12385824Smarks } else { 12395824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 12405824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 12415824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 12425824Smarks } 12435331Samw } 1244789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 12451544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 12465331Samw if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) { 1247789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1248789Sahrens 0, SPA_MAXBLOCKSIZE); 12495331Samw } 1250789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1251789Sahrens if (error) { 1252789Sahrens zfs_dirent_unlock(dl); 1253789Sahrens if (error == ERESTART && 1254789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 12552113Sahrens dmu_tx_wait(tx); 12562113Sahrens dmu_tx_abort(tx); 1257789Sahrens goto top; 1258789Sahrens } 12592113Sahrens dmu_tx_abort(tx); 1260789Sahrens ZFS_EXIT(zfsvfs); 12615331Samw if (aclp) 12625331Samw zfs_acl_free(aclp); 1263789Sahrens return (error); 1264789Sahrens } 12655446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp); 1266789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 12675331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 12685331Samw if (flag & FIGNORECASE) 12695331Samw txtype |= TX_CI; 12705331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 12715331Samw vsecp, fuidp, vap); 12725331Samw if (fuidp) 12735331Samw zfs_fuid_info_free(fuidp); 1274789Sahrens dmu_tx_commit(tx); 1275789Sahrens } else { 12765331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 12775331Samw 1278789Sahrens /* 1279789Sahrens * A directory entry already exists for this name. 1280789Sahrens */ 1281789Sahrens /* 1282789Sahrens * Can't truncate an existing file if in exclusive mode. 1283789Sahrens */ 1284789Sahrens if (excl == EXCL) { 1285789Sahrens error = EEXIST; 1286789Sahrens goto out; 1287789Sahrens } 1288789Sahrens /* 1289789Sahrens * Can't open a directory for writing. 1290789Sahrens */ 1291789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1292789Sahrens error = EISDIR; 1293789Sahrens goto out; 1294789Sahrens } 1295789Sahrens /* 1296789Sahrens * Verify requested access to file. 1297789Sahrens */ 12985331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1299789Sahrens goto out; 1300789Sahrens } 1301789Sahrens 1302789Sahrens mutex_enter(&dzp->z_lock); 1303789Sahrens dzp->z_seq++; 1304789Sahrens mutex_exit(&dzp->z_lock); 1305789Sahrens 13061878Smaybee /* 13071878Smaybee * Truncate regular files if requested. 13081878Smaybee */ 13091878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1310789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 13116992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 13126992Smaybee zfs_dirent_unlock(dl); 13136992Smaybee dl = NULL; 13141878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 13154863Spraks if (error == 0) { 13165331Samw vnevent_create(ZTOV(zp), ct); 13174863Spraks } 1318789Sahrens } 1319789Sahrens } 1320789Sahrens out: 1321789Sahrens 1322789Sahrens if (dl) 1323789Sahrens zfs_dirent_unlock(dl); 1324789Sahrens 1325789Sahrens if (error) { 1326789Sahrens if (zp) 1327789Sahrens VN_RELE(ZTOV(zp)); 1328789Sahrens } else { 1329789Sahrens *vpp = ZTOV(zp); 1330789Sahrens /* 1331789Sahrens * If vnode is for a device return a specfs vnode instead. 1332789Sahrens */ 1333789Sahrens if (IS_DEVVP(*vpp)) { 1334789Sahrens struct vnode *svp; 1335789Sahrens 1336789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1337789Sahrens VN_RELE(*vpp); 1338789Sahrens if (svp == NULL) { 1339789Sahrens error = ENOSYS; 1340789Sahrens } 1341789Sahrens *vpp = svp; 1342789Sahrens } 1343789Sahrens } 13445331Samw if (aclp) 13455331Samw zfs_acl_free(aclp); 1346789Sahrens 1347789Sahrens ZFS_EXIT(zfsvfs); 1348789Sahrens return (error); 1349789Sahrens } 1350789Sahrens 1351789Sahrens /* 1352789Sahrens * Remove an entry from a directory. 1353789Sahrens * 1354789Sahrens * IN: dvp - vnode of directory to remove entry from. 1355789Sahrens * name - name of entry to remove. 1356789Sahrens * cr - credentials of caller. 13575331Samw * ct - caller context 13585331Samw * flags - case flags 1359789Sahrens * 1360789Sahrens * RETURN: 0 if success 1361789Sahrens * error code if failure 1362789Sahrens * 1363789Sahrens * Timestamps: 1364789Sahrens * dvp - ctime|mtime 1365789Sahrens * vp - ctime (if nlink > 0) 1366789Sahrens */ 13675331Samw /*ARGSUSED*/ 1368789Sahrens static int 13695331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 13705331Samw int flags) 1371789Sahrens { 1372789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1373789Sahrens znode_t *xzp = NULL; 1374789Sahrens vnode_t *vp; 1375789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 13765326Sek110237 zilog_t *zilog; 1377789Sahrens uint64_t acl_obj, xattr_obj; 1378789Sahrens zfs_dirlock_t *dl; 1379789Sahrens dmu_tx_t *tx; 13803461Sahrens boolean_t may_delete_now, delete_now = FALSE; 13816992Smaybee boolean_t unlinked, toobig = FALSE; 13825331Samw uint64_t txtype; 13835331Samw pathname_t *realnmp = NULL; 13845331Samw pathname_t realnm; 1385789Sahrens int error; 13865331Samw int zflg = ZEXISTS; 1387789Sahrens 13885367Sahrens ZFS_ENTER(zfsvfs); 13895367Sahrens ZFS_VERIFY_ZP(dzp); 13905326Sek110237 zilog = zfsvfs->z_log; 1391789Sahrens 13925331Samw if (flags & FIGNORECASE) { 13935331Samw zflg |= ZCILOOK; 13945331Samw pn_alloc(&realnm); 13955331Samw realnmp = &realnm; 13965331Samw } 13975331Samw 1398789Sahrens top: 1399789Sahrens /* 1400789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1401789Sahrens */ 14025331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 14035331Samw NULL, realnmp)) { 14045331Samw if (realnmp) 14055331Samw pn_free(realnmp); 1406789Sahrens ZFS_EXIT(zfsvfs); 1407789Sahrens return (error); 1408789Sahrens } 1409789Sahrens 1410789Sahrens vp = ZTOV(zp); 1411789Sahrens 1412789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1413789Sahrens goto out; 1414789Sahrens } 1415789Sahrens 1416789Sahrens /* 1417789Sahrens * Need to use rmdir for removing directories. 1418789Sahrens */ 1419789Sahrens if (vp->v_type == VDIR) { 1420789Sahrens error = EPERM; 1421789Sahrens goto out; 1422789Sahrens } 1423789Sahrens 14245331Samw vnevent_remove(vp, dvp, name, ct); 14255331Samw 14265331Samw if (realnmp) 14276492Stimh dnlc_remove(dvp, realnmp->pn_buf); 14285331Samw else 14295331Samw dnlc_remove(dvp, name); 14301484Sek110237 1431789Sahrens mutex_enter(&vp->v_lock); 1432789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1433789Sahrens mutex_exit(&vp->v_lock); 1434789Sahrens 1435789Sahrens /* 14363461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1437789Sahrens * it depends on whether we're the last link, and on whether there are 1438789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1439789Sahrens * allow for either case. 1440789Sahrens */ 1441789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14421544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1443789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 14446992Smaybee if (may_delete_now) { 14456992Smaybee toobig = 14466992Smaybee zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 14476992Smaybee /* if the file is too big, only hold_free a token amount */ 14486992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 14496992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 14506992Smaybee } 1451789Sahrens 1452789Sahrens /* are there any extended attributes? */ 1453789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1454789Sahrens /* XXX - do we need this if we are deleting? */ 1455789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1456789Sahrens } 1457789Sahrens 1458789Sahrens /* are there any additional acls */ 1459789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1460789Sahrens may_delete_now) 1461789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1462789Sahrens 1463789Sahrens /* charge as an update -- would be nice not to charge at all */ 14643461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1465789Sahrens 1466789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1467789Sahrens if (error) { 1468789Sahrens zfs_dirent_unlock(dl); 1469789Sahrens VN_RELE(vp); 1470789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 14712113Sahrens dmu_tx_wait(tx); 14722113Sahrens dmu_tx_abort(tx); 1473789Sahrens goto top; 1474789Sahrens } 14755331Samw if (realnmp) 14765331Samw pn_free(realnmp); 14772113Sahrens dmu_tx_abort(tx); 1478789Sahrens ZFS_EXIT(zfsvfs); 1479789Sahrens return (error); 1480789Sahrens } 1481789Sahrens 1482789Sahrens /* 1483789Sahrens * Remove the directory entry. 1484789Sahrens */ 14855331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1486789Sahrens 1487789Sahrens if (error) { 1488789Sahrens dmu_tx_commit(tx); 1489789Sahrens goto out; 1490789Sahrens } 1491789Sahrens 14923461Sahrens if (unlinked) { 1493789Sahrens mutex_enter(&vp->v_lock); 14946992Smaybee delete_now = may_delete_now && !toobig && 1495789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1496789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1497789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1498789Sahrens mutex_exit(&vp->v_lock); 1499789Sahrens } 1500789Sahrens 1501789Sahrens if (delete_now) { 1502789Sahrens if (zp->z_phys->zp_xattr) { 1503789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1504789Sahrens ASSERT3U(error, ==, 0); 1505789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1506789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1507789Sahrens mutex_enter(&xzp->z_lock); 15083461Sahrens xzp->z_unlinked = 1; 1509789Sahrens xzp->z_phys->zp_links = 0; 1510789Sahrens mutex_exit(&xzp->z_lock); 15113461Sahrens zfs_unlinked_add(xzp, tx); 1512789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1513789Sahrens } 1514789Sahrens mutex_enter(&zp->z_lock); 1515789Sahrens mutex_enter(&vp->v_lock); 1516789Sahrens vp->v_count--; 1517789Sahrens ASSERT3U(vp->v_count, ==, 0); 1518789Sahrens mutex_exit(&vp->v_lock); 1519789Sahrens mutex_exit(&zp->z_lock); 1520789Sahrens zfs_znode_delete(zp, tx); 15213461Sahrens } else if (unlinked) { 15223461Sahrens zfs_unlinked_add(zp, tx); 1523789Sahrens } 1524789Sahrens 15255331Samw txtype = TX_REMOVE; 15265331Samw if (flags & FIGNORECASE) 15275331Samw txtype |= TX_CI; 15285331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 1529789Sahrens 1530789Sahrens dmu_tx_commit(tx); 1531789Sahrens out: 15325331Samw if (realnmp) 15335331Samw pn_free(realnmp); 15345331Samw 1535789Sahrens zfs_dirent_unlock(dl); 1536789Sahrens 1537789Sahrens if (!delete_now) { 1538789Sahrens VN_RELE(vp); 1539789Sahrens } else if (xzp) { 15406992Smaybee /* this rele is delayed to prevent nesting transactions */ 1541789Sahrens VN_RELE(ZTOV(xzp)); 1542789Sahrens } 1543789Sahrens 1544789Sahrens ZFS_EXIT(zfsvfs); 1545789Sahrens return (error); 1546789Sahrens } 1547789Sahrens 1548789Sahrens /* 1549789Sahrens * Create a new directory and insert it into dvp using the name 1550789Sahrens * provided. Return a pointer to the inserted directory. 1551789Sahrens * 1552789Sahrens * IN: dvp - vnode of directory to add subdir to. 1553789Sahrens * dirname - name of new directory. 1554789Sahrens * vap - attributes of new directory. 1555789Sahrens * cr - credentials of caller. 15565331Samw * ct - caller context 15575331Samw * vsecp - ACL to be set 1558789Sahrens * 1559789Sahrens * OUT: vpp - vnode of created directory. 1560789Sahrens * 1561789Sahrens * RETURN: 0 if success 1562789Sahrens * error code if failure 1563789Sahrens * 1564789Sahrens * Timestamps: 1565789Sahrens * dvp - ctime|mtime updated 1566789Sahrens * vp - ctime|mtime|atime updated 1567789Sahrens */ 15685331Samw /*ARGSUSED*/ 1569789Sahrens static int 15705331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 15715331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1572789Sahrens { 1573789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1574789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15755326Sek110237 zilog_t *zilog; 1576789Sahrens zfs_dirlock_t *dl; 15775331Samw uint64_t txtype; 1578789Sahrens dmu_tx_t *tx; 1579789Sahrens int error; 15805331Samw zfs_acl_t *aclp = NULL; 15815331Samw zfs_fuid_info_t *fuidp = NULL; 15825331Samw int zf = ZNEW; 1583789Sahrens 1584789Sahrens ASSERT(vap->va_type == VDIR); 1585789Sahrens 15865331Samw /* 15875331Samw * If we have an ephemeral id, ACL, or XVATTR then 15885331Samw * make sure file system is at proper version 15895331Samw */ 15905331Samw 15915331Samw if (zfsvfs->z_use_fuids == B_FALSE && 15925331Samw (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))|| 15935331Samw IS_EPHEMERAL(crgetgid(cr)))) 15945331Samw return (EINVAL); 15955331Samw 15965367Sahrens ZFS_ENTER(zfsvfs); 15975367Sahrens ZFS_VERIFY_ZP(dzp); 15985326Sek110237 zilog = zfsvfs->z_log; 1599789Sahrens 1600789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1601789Sahrens ZFS_EXIT(zfsvfs); 1602789Sahrens return (EINVAL); 1603789Sahrens } 16045331Samw 16055498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 16065331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 16075331Samw ZFS_EXIT(zfsvfs); 16085331Samw return (EILSEQ); 16095331Samw } 16105331Samw if (flags & FIGNORECASE) 16115331Samw zf |= ZCILOOK; 16125331Samw 16135331Samw if (vap->va_mask & AT_XVATTR) 16145331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 16155331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 16165331Samw ZFS_EXIT(zfsvfs); 16175331Samw return (error); 16185331Samw } 1619789Sahrens 1620789Sahrens /* 1621789Sahrens * First make sure the new directory doesn't exist. 1622789Sahrens */ 16235331Samw top: 16245331Samw *vpp = NULL; 16255331Samw 16265331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 16275331Samw NULL, NULL)) { 1628789Sahrens ZFS_EXIT(zfsvfs); 1629789Sahrens return (error); 1630789Sahrens } 1631789Sahrens 16325331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 16331231Smarks zfs_dirent_unlock(dl); 16341231Smarks ZFS_EXIT(zfsvfs); 16351231Smarks return (error); 16361231Smarks } 16371231Smarks 16385331Samw if (vsecp && aclp == NULL) { 16395331Samw error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp); 16405331Samw if (error) { 16415331Samw zfs_dirent_unlock(dl); 16425331Samw ZFS_EXIT(zfsvfs); 16435331Samw return (error); 16445331Samw } 16455331Samw } 1646789Sahrens /* 1647789Sahrens * Add a new entry to the directory. 1648789Sahrens */ 1649789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16501544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 16511544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 16525959Smarks if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(crgetuid(cr)) || 16535959Smarks IS_EPHEMERAL(crgetgid(cr))) { 16545824Smarks if (zfsvfs->z_fuid_obj == 0) { 16555824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 16565824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 16575824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16585824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 16595824Smarks } else { 16605824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 16615824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 16625824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16635824Smarks } 16645331Samw } 16655331Samw if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) 1666789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1667789Sahrens 0, SPA_MAXBLOCKSIZE); 1668789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1669789Sahrens if (error) { 1670789Sahrens zfs_dirent_unlock(dl); 1671789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 16722113Sahrens dmu_tx_wait(tx); 16732113Sahrens dmu_tx_abort(tx); 1674789Sahrens goto top; 1675789Sahrens } 16762113Sahrens dmu_tx_abort(tx); 1677789Sahrens ZFS_EXIT(zfsvfs); 16785331Samw if (aclp) 16795331Samw zfs_acl_free(aclp); 1680789Sahrens return (error); 1681789Sahrens } 1682789Sahrens 1683789Sahrens /* 1684789Sahrens * Create new node. 1685789Sahrens */ 16865446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp); 16875331Samw 16885331Samw if (aclp) 16895331Samw zfs_acl_free(aclp); 1690789Sahrens 1691789Sahrens /* 1692789Sahrens * Now put new name in parent dir. 1693789Sahrens */ 1694789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1695789Sahrens 1696789Sahrens *vpp = ZTOV(zp); 1697789Sahrens 16985331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 16995331Samw if (flags & FIGNORECASE) 17005331Samw txtype |= TX_CI; 17015331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap); 17025331Samw 17035331Samw if (fuidp) 17045331Samw zfs_fuid_info_free(fuidp); 1705789Sahrens dmu_tx_commit(tx); 1706789Sahrens 1707789Sahrens zfs_dirent_unlock(dl); 1708789Sahrens 1709789Sahrens ZFS_EXIT(zfsvfs); 1710789Sahrens return (0); 1711789Sahrens } 1712789Sahrens 1713789Sahrens /* 1714789Sahrens * Remove a directory subdir entry. If the current working 1715789Sahrens * directory is the same as the subdir to be removed, the 1716789Sahrens * remove will fail. 1717789Sahrens * 1718789Sahrens * IN: dvp - vnode of directory to remove from. 1719789Sahrens * name - name of directory to be removed. 1720789Sahrens * cwd - vnode of current working directory. 1721789Sahrens * cr - credentials of caller. 17225331Samw * ct - caller context 17235331Samw * flags - case flags 1724789Sahrens * 1725789Sahrens * RETURN: 0 if success 1726789Sahrens * error code if failure 1727789Sahrens * 1728789Sahrens * Timestamps: 1729789Sahrens * dvp - ctime|mtime updated 1730789Sahrens */ 17315331Samw /*ARGSUSED*/ 1732789Sahrens static int 17335331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 17345331Samw caller_context_t *ct, int flags) 1735789Sahrens { 1736789Sahrens znode_t *dzp = VTOZ(dvp); 1737789Sahrens znode_t *zp; 1738789Sahrens vnode_t *vp; 1739789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17405326Sek110237 zilog_t *zilog; 1741789Sahrens zfs_dirlock_t *dl; 1742789Sahrens dmu_tx_t *tx; 1743789Sahrens int error; 17445331Samw int zflg = ZEXISTS; 1745789Sahrens 17465367Sahrens ZFS_ENTER(zfsvfs); 17475367Sahrens ZFS_VERIFY_ZP(dzp); 17485326Sek110237 zilog = zfsvfs->z_log; 1749789Sahrens 17505331Samw if (flags & FIGNORECASE) 17515331Samw zflg |= ZCILOOK; 1752789Sahrens top: 1753789Sahrens zp = NULL; 1754789Sahrens 1755789Sahrens /* 1756789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1757789Sahrens */ 17585331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 17595331Samw NULL, NULL)) { 1760789Sahrens ZFS_EXIT(zfsvfs); 1761789Sahrens return (error); 1762789Sahrens } 1763789Sahrens 1764789Sahrens vp = ZTOV(zp); 1765789Sahrens 1766789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1767789Sahrens goto out; 1768789Sahrens } 1769789Sahrens 1770789Sahrens if (vp->v_type != VDIR) { 1771789Sahrens error = ENOTDIR; 1772789Sahrens goto out; 1773789Sahrens } 1774789Sahrens 1775789Sahrens if (vp == cwd) { 1776789Sahrens error = EINVAL; 1777789Sahrens goto out; 1778789Sahrens } 1779789Sahrens 17805331Samw vnevent_rmdir(vp, dvp, name, ct); 1781789Sahrens 1782789Sahrens /* 17833897Smaybee * Grab a lock on the directory to make sure that noone is 17843897Smaybee * trying to add (or lookup) entries while we are removing it. 17853897Smaybee */ 17863897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 17873897Smaybee 17883897Smaybee /* 17893897Smaybee * Grab a lock on the parent pointer to make sure we play well 1790789Sahrens * with the treewalk and directory rename code. 1791789Sahrens */ 1792789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1793789Sahrens 1794789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 17951544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1796789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 17973461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1798789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1799789Sahrens if (error) { 1800789Sahrens rw_exit(&zp->z_parent_lock); 18013897Smaybee rw_exit(&zp->z_name_lock); 1802789Sahrens zfs_dirent_unlock(dl); 1803789Sahrens VN_RELE(vp); 1804789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 18052113Sahrens dmu_tx_wait(tx); 18062113Sahrens dmu_tx_abort(tx); 1807789Sahrens goto top; 1808789Sahrens } 18092113Sahrens dmu_tx_abort(tx); 1810789Sahrens ZFS_EXIT(zfsvfs); 1811789Sahrens return (error); 1812789Sahrens } 1813789Sahrens 18145331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 18155331Samw 18165331Samw if (error == 0) { 18175331Samw uint64_t txtype = TX_RMDIR; 18185331Samw if (flags & FIGNORECASE) 18195331Samw txtype |= TX_CI; 18205331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 18215331Samw } 1822789Sahrens 1823789Sahrens dmu_tx_commit(tx); 1824789Sahrens 1825789Sahrens rw_exit(&zp->z_parent_lock); 18263897Smaybee rw_exit(&zp->z_name_lock); 1827789Sahrens out: 1828789Sahrens zfs_dirent_unlock(dl); 1829789Sahrens 1830789Sahrens VN_RELE(vp); 1831789Sahrens 1832789Sahrens ZFS_EXIT(zfsvfs); 1833789Sahrens return (error); 1834789Sahrens } 1835789Sahrens 1836789Sahrens /* 1837789Sahrens * Read as many directory entries as will fit into the provided 1838789Sahrens * buffer from the given directory cursor position (specified in 1839789Sahrens * the uio structure. 1840789Sahrens * 1841789Sahrens * IN: vp - vnode of directory to read. 1842789Sahrens * uio - structure supplying read location, range info, 1843789Sahrens * and return buffer. 1844789Sahrens * cr - credentials of caller. 18455331Samw * ct - caller context 18465331Samw * flags - case flags 1847789Sahrens * 1848789Sahrens * OUT: uio - updated offset and range, buffer filled. 1849789Sahrens * eofp - set to true if end-of-file detected. 1850789Sahrens * 1851789Sahrens * RETURN: 0 if success 1852789Sahrens * error code if failure 1853789Sahrens * 1854789Sahrens * Timestamps: 1855789Sahrens * vp - atime updated 1856789Sahrens * 1857789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1858789Sahrens * This allows us to use the low range for "special" directory entries: 1859789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1860789Sahrens * we use the offset 2 for the '.zfs' directory. 1861789Sahrens */ 1862789Sahrens /* ARGSUSED */ 1863789Sahrens static int 18645331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 18655331Samw caller_context_t *ct, int flags) 1866789Sahrens { 1867789Sahrens znode_t *zp = VTOZ(vp); 1868789Sahrens iovec_t *iovp; 18695331Samw edirent_t *eodp; 1870789Sahrens dirent64_t *odp; 1871789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1872869Sperrin objset_t *os; 1873789Sahrens caddr_t outbuf; 1874789Sahrens size_t bufsize; 1875789Sahrens zap_cursor_t zc; 1876789Sahrens zap_attribute_t zap; 1877789Sahrens uint_t bytes_wanted; 1878789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1879789Sahrens int local_eof; 1880869Sperrin int outcount; 1881869Sperrin int error; 1882869Sperrin uint8_t prefetch; 18835663Sck153898 boolean_t check_sysattrs; 1884789Sahrens 18855367Sahrens ZFS_ENTER(zfsvfs); 18865367Sahrens ZFS_VERIFY_ZP(zp); 1887789Sahrens 1888789Sahrens /* 1889789Sahrens * If we are not given an eof variable, 1890789Sahrens * use a local one. 1891789Sahrens */ 1892789Sahrens if (eofp == NULL) 1893789Sahrens eofp = &local_eof; 1894789Sahrens 1895789Sahrens /* 1896789Sahrens * Check for valid iov_len. 1897789Sahrens */ 1898789Sahrens if (uio->uio_iov->iov_len <= 0) { 1899789Sahrens ZFS_EXIT(zfsvfs); 1900789Sahrens return (EINVAL); 1901789Sahrens } 1902789Sahrens 1903789Sahrens /* 1904789Sahrens * Quit if directory has been removed (posix) 1905789Sahrens */ 19063461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 1907789Sahrens ZFS_EXIT(zfsvfs); 1908789Sahrens return (0); 1909789Sahrens } 1910789Sahrens 1911869Sperrin error = 0; 1912869Sperrin os = zfsvfs->z_os; 1913869Sperrin offset = uio->uio_loffset; 1914869Sperrin prefetch = zp->z_zn_prefetch; 1915869Sperrin 1916789Sahrens /* 1917789Sahrens * Initialize the iterator cursor. 1918789Sahrens */ 1919789Sahrens if (offset <= 3) { 1920789Sahrens /* 1921789Sahrens * Start iteration from the beginning of the directory. 1922789Sahrens */ 1923869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1924789Sahrens } else { 1925789Sahrens /* 1926789Sahrens * The offset is a serialized cursor. 1927789Sahrens */ 1928869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1929789Sahrens } 1930789Sahrens 1931789Sahrens /* 1932789Sahrens * Get space to change directory entries into fs independent format. 1933789Sahrens */ 1934789Sahrens iovp = uio->uio_iov; 1935789Sahrens bytes_wanted = iovp->iov_len; 1936789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1937789Sahrens bufsize = bytes_wanted; 1938789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1939789Sahrens odp = (struct dirent64 *)outbuf; 1940789Sahrens } else { 1941789Sahrens bufsize = bytes_wanted; 1942789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1943789Sahrens } 19445331Samw eodp = (struct edirent *)odp; 1945789Sahrens 1946789Sahrens /* 19475663Sck153898 * If this VFS supports system attributes; and we're looking at an 19485663Sck153898 * extended attribute directory; and we care about normalization 19495663Sck153898 * conflicts on this vfs; then we must check for normalization 19505663Sck153898 * conflicts with the sysattr name space. 19515663Sck153898 */ 19525663Sck153898 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) && 19535663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 19545663Sck153898 (flags & V_RDDIR_ENTFLAGS); 19555663Sck153898 19565663Sck153898 /* 1957789Sahrens * Transform to file-system independent format 1958789Sahrens */ 1959789Sahrens outcount = 0; 1960789Sahrens while (outcount < bytes_wanted) { 19613912Slling ino64_t objnum; 19623912Slling ushort_t reclen; 19633912Slling off64_t *next; 19643912Slling 1965789Sahrens /* 1966789Sahrens * Special case `.', `..', and `.zfs'. 1967789Sahrens */ 1968789Sahrens if (offset == 0) { 1969789Sahrens (void) strcpy(zap.za_name, "."); 19705331Samw zap.za_normalization_conflict = 0; 19713912Slling objnum = zp->z_id; 1972789Sahrens } else if (offset == 1) { 1973789Sahrens (void) strcpy(zap.za_name, ".."); 19745331Samw zap.za_normalization_conflict = 0; 19753912Slling objnum = zp->z_phys->zp_parent; 1976789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1977789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 19785331Samw zap.za_normalization_conflict = 0; 19793912Slling objnum = ZFSCTL_INO_ROOT; 1980789Sahrens } else { 1981789Sahrens /* 1982789Sahrens * Grab next entry. 1983789Sahrens */ 1984789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1985789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1986789Sahrens break; 1987789Sahrens else 1988789Sahrens goto update; 1989789Sahrens } 1990789Sahrens 1991789Sahrens if (zap.za_integer_length != 8 || 1992789Sahrens zap.za_num_integers != 1) { 1993789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1994789Sahrens "entry, obj = %lld, offset = %lld\n", 1995789Sahrens (u_longlong_t)zp->z_id, 1996789Sahrens (u_longlong_t)offset); 1997789Sahrens error = ENXIO; 1998789Sahrens goto update; 1999789Sahrens } 20003912Slling 20013912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 20023912Slling /* 20033912Slling * MacOS X can extract the object type here such as: 20043912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 20053912Slling */ 20065663Sck153898 20075663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 20085663Sck153898 zap.za_normalization_conflict = 20095663Sck153898 xattr_sysattr_casechk(zap.za_name); 20105663Sck153898 } 2011789Sahrens } 20125331Samw 20135331Samw if (flags & V_RDDIR_ENTFLAGS) 20145331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 20155331Samw else 20165331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2017789Sahrens 2018789Sahrens /* 2019789Sahrens * Will this entry fit in the buffer? 2020789Sahrens */ 20213912Slling if (outcount + reclen > bufsize) { 2022789Sahrens /* 2023789Sahrens * Did we manage to fit anything in the buffer? 2024789Sahrens */ 2025789Sahrens if (!outcount) { 2026789Sahrens error = EINVAL; 2027789Sahrens goto update; 2028789Sahrens } 2029789Sahrens break; 2030789Sahrens } 20315331Samw if (flags & V_RDDIR_ENTFLAGS) { 20325331Samw /* 20335331Samw * Add extended flag entry: 20345331Samw */ 20355331Samw eodp->ed_ino = objnum; 20365331Samw eodp->ed_reclen = reclen; 20375331Samw /* NOTE: ed_off is the offset for the *next* entry */ 20385331Samw next = &(eodp->ed_off); 20395331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 20405331Samw ED_CASE_CONFLICT : 0; 20415331Samw (void) strncpy(eodp->ed_name, zap.za_name, 20425331Samw EDIRENT_NAMELEN(reclen)); 20435331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 20445331Samw } else { 20455331Samw /* 20465331Samw * Add normal entry: 20475331Samw */ 20485331Samw odp->d_ino = objnum; 20495331Samw odp->d_reclen = reclen; 20505331Samw /* NOTE: d_off is the offset for the *next* entry */ 20515331Samw next = &(odp->d_off); 20525331Samw (void) strncpy(odp->d_name, zap.za_name, 20535331Samw DIRENT64_NAMELEN(reclen)); 20545331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 20555331Samw } 20563912Slling outcount += reclen; 2057789Sahrens 2058789Sahrens ASSERT(outcount <= bufsize); 2059789Sahrens 2060789Sahrens /* Prefetch znode */ 2061869Sperrin if (prefetch) 20623912Slling dmu_prefetch(os, objnum, 0, 0); 2063789Sahrens 2064789Sahrens /* 2065789Sahrens * Move to the next entry, fill in the previous offset. 2066789Sahrens */ 2067789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2068789Sahrens zap_cursor_advance(&zc); 2069789Sahrens offset = zap_cursor_serialize(&zc); 2070789Sahrens } else { 2071789Sahrens offset += 1; 2072789Sahrens } 2073789Sahrens *next = offset; 2074789Sahrens } 2075869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2076789Sahrens 2077789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2078789Sahrens iovp->iov_base += outcount; 2079789Sahrens iovp->iov_len -= outcount; 2080789Sahrens uio->uio_resid -= outcount; 2081789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2082789Sahrens /* 2083789Sahrens * Reset the pointer. 2084789Sahrens */ 2085789Sahrens offset = uio->uio_loffset; 2086789Sahrens } 2087789Sahrens 2088789Sahrens update: 2089885Sahrens zap_cursor_fini(&zc); 2090789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2091789Sahrens kmem_free(outbuf, bufsize); 2092789Sahrens 2093789Sahrens if (error == ENOENT) 2094789Sahrens error = 0; 2095789Sahrens 2096789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2097789Sahrens 2098789Sahrens uio->uio_loffset = offset; 2099789Sahrens ZFS_EXIT(zfsvfs); 2100789Sahrens return (error); 2101789Sahrens } 2102789Sahrens 21034720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 21044720Sfr157268 2105789Sahrens static int 21065331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2107789Sahrens { 2108789Sahrens znode_t *zp = VTOZ(vp); 2109789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2110789Sahrens 21111773Seschrock /* 21121773Seschrock * Regardless of whether this is required for standards conformance, 21131773Seschrock * this is the logical behavior when fsync() is called on a file with 21141773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 21151773Seschrock * going to be pushed out as part of the zil_commit(). 21161773Seschrock */ 21171773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 21181773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 21195331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 21201773Seschrock 21214720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 21224720Sfr157268 21235367Sahrens ZFS_ENTER(zfsvfs); 21245367Sahrens ZFS_VERIFY_ZP(zp); 21252638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 2126789Sahrens ZFS_EXIT(zfsvfs); 2127789Sahrens return (0); 2128789Sahrens } 2129789Sahrens 21305331Samw 2131789Sahrens /* 2132789Sahrens * Get the requested file attributes and place them in the provided 2133789Sahrens * vattr structure. 2134789Sahrens * 2135789Sahrens * IN: vp - vnode of file. 2136789Sahrens * vap - va_mask identifies requested attributes. 21375331Samw * If AT_XVATTR set, then optional attrs are requested 21385331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2139789Sahrens * cr - credentials of caller. 21405331Samw * ct - caller context 2141789Sahrens * 2142789Sahrens * OUT: vap - attribute values. 2143789Sahrens * 2144789Sahrens * RETURN: 0 (always succeeds) 2145789Sahrens */ 2146789Sahrens /* ARGSUSED */ 2147789Sahrens static int 21485331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 21495331Samw caller_context_t *ct) 2150789Sahrens { 2151789Sahrens znode_t *zp = VTOZ(vp); 2152789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 21535326Sek110237 znode_phys_t *pzp; 21545331Samw int error = 0; 21554543Smarks uint64_t links; 21565331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 21575331Samw xoptattr_t *xoap = NULL; 21585331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2159789Sahrens 21605367Sahrens ZFS_ENTER(zfsvfs); 21615367Sahrens ZFS_VERIFY_ZP(zp); 21625326Sek110237 pzp = zp->z_phys; 2163789Sahrens 21645331Samw mutex_enter(&zp->z_lock); 21655331Samw 21665331Samw /* 21675331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 21685331Samw * Also, if we are the owner don't bother, since owner should 21695331Samw * always be allowed to read basic attributes of file. 21705331Samw */ 21715331Samw if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) && 21725331Samw (pzp->zp_uid != crgetuid(cr))) { 21735331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 21745331Samw skipaclchk, cr)) { 21755331Samw mutex_exit(&zp->z_lock); 21765331Samw ZFS_EXIT(zfsvfs); 21775331Samw return (error); 21785331Samw } 21795331Samw } 21805331Samw 2181789Sahrens /* 2182789Sahrens * Return all attributes. It's cheaper to provide the answer 2183789Sahrens * than to determine whether we were asked the question. 2184789Sahrens */ 2185789Sahrens 2186789Sahrens vap->va_type = vp->v_type; 2187789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 21885771Sjp151216 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2189789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2190789Sahrens vap->va_nodeid = zp->z_id; 21914543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 21924543Smarks links = pzp->zp_links + 1; 21934543Smarks else 21944543Smarks links = pzp->zp_links; 21954543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2196789Sahrens vap->va_size = pzp->zp_size; 21971816Smarks vap->va_rdev = vp->v_rdev; 2198789Sahrens vap->va_seq = zp->z_seq; 2199789Sahrens 22005331Samw /* 22015331Samw * Add in any requested optional attributes and the create time. 22025331Samw * Also set the corresponding bits in the returned attribute bitmap. 22035331Samw */ 22045331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 22055331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 22065331Samw xoap->xoa_archive = 22075331Samw ((pzp->zp_flags & ZFS_ARCHIVE) != 0); 22085331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 22095331Samw } 22105331Samw 22115331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 22125331Samw xoap->xoa_readonly = 22135331Samw ((pzp->zp_flags & ZFS_READONLY) != 0); 22145331Samw XVA_SET_RTN(xvap, XAT_READONLY); 22155331Samw } 22165331Samw 22175331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 22185331Samw xoap->xoa_system = 22195331Samw ((pzp->zp_flags & ZFS_SYSTEM) != 0); 22205331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 22215331Samw } 22225331Samw 22235331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 22245331Samw xoap->xoa_hidden = 22255331Samw ((pzp->zp_flags & ZFS_HIDDEN) != 0); 22265331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 22275331Samw } 22285331Samw 22295331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 22305331Samw xoap->xoa_nounlink = 22315331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0); 22325331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 22335331Samw } 22345331Samw 22355331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 22365331Samw xoap->xoa_immutable = 22375331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0); 22385331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 22395331Samw } 22405331Samw 22415331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 22425331Samw xoap->xoa_appendonly = 22435331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0); 22445331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 22455331Samw } 22465331Samw 22475331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 22485331Samw xoap->xoa_nodump = 22495331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0); 22505331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 22515331Samw } 22525331Samw 22535331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 22545331Samw xoap->xoa_opaque = 22555331Samw ((pzp->zp_flags & ZFS_OPAQUE) != 0); 22565331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 22575331Samw } 22585331Samw 22595331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 22605331Samw xoap->xoa_av_quarantined = 22615331Samw ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0); 22625331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 22635331Samw } 22645331Samw 22655331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 22665331Samw xoap->xoa_av_modified = 22675331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0); 22685331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 22695331Samw } 22705331Samw 22715331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 22725331Samw vp->v_type == VREG && 22735331Samw (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) { 22745331Samw size_t len; 22755331Samw dmu_object_info_t doi; 22765331Samw 22775331Samw /* 22785331Samw * Only VREG files have anti-virus scanstamps, so we 22795331Samw * won't conflict with symlinks in the bonus buffer. 22805331Samw */ 22815331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 22825331Samw len = sizeof (xoap->xoa_av_scanstamp) + 22835331Samw sizeof (znode_phys_t); 22845331Samw if (len <= doi.doi_bonus_size) { 22855331Samw /* 22865331Samw * pzp points to the start of the 22875331Samw * znode_phys_t. pzp + 1 points to the 22885331Samw * first byte after the znode_phys_t. 22895331Samw */ 22905331Samw (void) memcpy(xoap->xoa_av_scanstamp, 22915331Samw pzp + 1, 22925331Samw sizeof (xoap->xoa_av_scanstamp)); 22935331Samw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 22945331Samw } 22955331Samw } 22965331Samw 22975331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 22985331Samw ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime); 22995331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 23005331Samw } 23015331Samw } 23025331Samw 2303789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2304789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2305789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2306789Sahrens 2307789Sahrens mutex_exit(&zp->z_lock); 2308789Sahrens 2309789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2310789Sahrens 2311789Sahrens if (zp->z_blksz == 0) { 2312789Sahrens /* 2313789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2314789Sahrens */ 2315789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2316789Sahrens } 2317789Sahrens 2318789Sahrens ZFS_EXIT(zfsvfs); 2319789Sahrens return (0); 2320789Sahrens } 2321789Sahrens 2322789Sahrens /* 2323789Sahrens * Set the file attributes to the values contained in the 2324789Sahrens * vattr structure. 2325789Sahrens * 2326789Sahrens * IN: vp - vnode of file to be modified. 2327789Sahrens * vap - new attribute values. 23285331Samw * If AT_XVATTR set, then optional attrs are being set 2329789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 23305331Samw * - ATTR_NOACLCHECK (CIFS context only). 2331789Sahrens * cr - credentials of caller. 23325331Samw * ct - caller context 2333789Sahrens * 2334789Sahrens * RETURN: 0 if success 2335789Sahrens * error code if failure 2336789Sahrens * 2337789Sahrens * Timestamps: 2338789Sahrens * vp - ctime updated, mtime updated if size changed. 2339789Sahrens */ 2340789Sahrens /* ARGSUSED */ 2341789Sahrens static int 2342789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2343789Sahrens caller_context_t *ct) 2344789Sahrens { 23455326Sek110237 znode_t *zp = VTOZ(vp); 23465326Sek110237 znode_phys_t *pzp; 2347789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23485326Sek110237 zilog_t *zilog; 2349789Sahrens dmu_tx_t *tx; 23501878Smaybee vattr_t oldva; 2351789Sahrens uint_t mask = vap->va_mask; 23521878Smaybee uint_t saved_mask; 23532796Smarks int trim_mask = 0; 2354789Sahrens uint64_t new_mode; 23551231Smarks znode_t *attrzp; 2356789Sahrens int need_policy = FALSE; 2357789Sahrens int err; 23585331Samw zfs_fuid_info_t *fuidp = NULL; 23595331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23605331Samw xoptattr_t *xoap; 23615824Smarks zfs_acl_t *aclp = NULL; 23625331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2363789Sahrens 2364789Sahrens if (mask == 0) 2365789Sahrens return (0); 2366789Sahrens 2367789Sahrens if (mask & AT_NOSET) 2368789Sahrens return (EINVAL); 2369789Sahrens 23705367Sahrens ZFS_ENTER(zfsvfs); 23715367Sahrens ZFS_VERIFY_ZP(zp); 23725331Samw 23735331Samw pzp = zp->z_phys; 23745331Samw zilog = zfsvfs->z_log; 23755331Samw 23765331Samw /* 23775331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 23785331Samw * that file system is at proper version level 23795331Samw */ 23805331Samw 23815331Samw if (zfsvfs->z_use_fuids == B_FALSE && 23825331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 23835331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 23845386Stimh (mask & AT_XVATTR))) { 23855386Stimh ZFS_EXIT(zfsvfs); 23865331Samw return (EINVAL); 23875386Stimh } 23885386Stimh 23895386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 23905386Stimh ZFS_EXIT(zfsvfs); 2391789Sahrens return (EISDIR); 23925386Stimh } 23935386Stimh 23945386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 23955386Stimh ZFS_EXIT(zfsvfs); 23961308Smarks return (EINVAL); 23975386Stimh } 23981308Smarks 23995331Samw /* 24005331Samw * If this is an xvattr_t, then get a pointer to the structure of 24015331Samw * optional attributes. If this is NULL, then we have a vattr_t. 24025331Samw */ 24035331Samw xoap = xva_getxoptattr(xvap); 24045331Samw 24055331Samw /* 24065331Samw * Immutable files can only alter immutable bit and atime 24075331Samw */ 24085331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 24095331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 24105386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 24115386Stimh ZFS_EXIT(zfsvfs); 24125331Samw return (EPERM); 24135386Stimh } 24145386Stimh 24155386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 24165386Stimh ZFS_EXIT(zfsvfs); 24175331Samw return (EPERM); 24185386Stimh } 2419789Sahrens 24206064Smarks /* 24216064Smarks * Verify timestamps doesn't overflow 32 bits. 24226064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 24236064Smarks * handle times greater than 2039. This check should be removed 24246064Smarks * once large timestamps are fully supported. 24256064Smarks */ 24266064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 24276064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 24286064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 24296064Smarks ZFS_EXIT(zfsvfs); 24306064Smarks return (EOVERFLOW); 24316064Smarks } 24326064Smarks } 24336064Smarks 2434789Sahrens top: 24351231Smarks attrzp = NULL; 2436789Sahrens 2437789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2438789Sahrens ZFS_EXIT(zfsvfs); 2439789Sahrens return (EROFS); 2440789Sahrens } 2441789Sahrens 2442789Sahrens /* 2443789Sahrens * First validate permissions 2444789Sahrens */ 2445789Sahrens 2446789Sahrens if (mask & AT_SIZE) { 24475331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2448789Sahrens if (err) { 2449789Sahrens ZFS_EXIT(zfsvfs); 2450789Sahrens return (err); 2451789Sahrens } 24521878Smaybee /* 24531878Smaybee * XXX - Note, we are not providing any open 24541878Smaybee * mode flags here (like FNDELAY), so we may 24551878Smaybee * block if there are locks present... this 24561878Smaybee * should be addressed in openat(). 24571878Smaybee */ 24586992Smaybee /* XXX - would it be OK to generate a log record here? */ 24596992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 24601878Smaybee if (err) { 24611878Smaybee ZFS_EXIT(zfsvfs); 24621878Smaybee return (err); 24631878Smaybee } 2464789Sahrens } 2465789Sahrens 24665331Samw if (mask & (AT_ATIME|AT_MTIME) || 24675331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 24685331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 24695331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 24705331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 24715331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 24725331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 24735331Samw skipaclchk, cr); 2474789Sahrens 2475789Sahrens if (mask & (AT_UID|AT_GID)) { 2476789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2477789Sahrens int take_owner; 2478789Sahrens int take_group; 2479789Sahrens 2480789Sahrens /* 2481913Smarks * NOTE: even if a new mode is being set, 2482913Smarks * we may clear S_ISUID/S_ISGID bits. 2483913Smarks */ 2484913Smarks 2485913Smarks if (!(mask & AT_MODE)) 2486913Smarks vap->va_mode = pzp->zp_mode; 2487913Smarks 2488913Smarks /* 2489789Sahrens * Take ownership or chgrp to group we are a member of 2490789Sahrens */ 2491789Sahrens 2492789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 24935331Samw take_group = (mask & AT_GID) && 24945331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2495789Sahrens 2496789Sahrens /* 2497789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2498789Sahrens * take_group must both be set in order to allow taking 2499789Sahrens * ownership. 2500789Sahrens * 2501789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2502789Sahrens * 2503789Sahrens */ 2504789Sahrens 2505789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2506789Sahrens ((idmask == AT_UID) && take_owner) || 2507789Sahrens ((idmask == AT_GID) && take_group)) { 25085331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 25095331Samw skipaclchk, cr) == 0) { 2510789Sahrens /* 2511789Sahrens * Remove setuid/setgid for non-privileged users 2512789Sahrens */ 25131115Smarks secpolicy_setid_clear(vap, cr); 25142796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2515789Sahrens } else { 2516789Sahrens need_policy = TRUE; 2517789Sahrens } 2518789Sahrens } else { 2519789Sahrens need_policy = TRUE; 2520789Sahrens } 2521789Sahrens } 2522789Sahrens 25232796Smarks mutex_enter(&zp->z_lock); 25242796Smarks oldva.va_mode = pzp->zp_mode; 25255771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 25265331Samw if (mask & AT_XVATTR) { 25275331Samw if ((need_policy == FALSE) && 25285331Samw (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) && 25295331Samw xoap->xoa_appendonly != 25305331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) || 25315331Samw (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) && 25325331Samw xoap->xoa_nounlink != 25335331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) || 25345331Samw (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) && 25355331Samw xoap->xoa_immutable != 25365331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) || 25375331Samw (XVA_ISSET_REQ(xvap, XAT_NODUMP) && 25385331Samw xoap->xoa_nodump != 25395331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0)) || 25405331Samw (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) && 25415331Samw xoap->xoa_av_modified != 25425331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) || 25435545Smarks ((XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) && 25445545Smarks ((vp->v_type != VREG && xoap->xoa_av_quarantined) || 25455331Samw xoap->xoa_av_quarantined != 25465545Smarks ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)))) || 25475331Samw (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) || 25485331Samw (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 25495331Samw need_policy = TRUE; 25505331Samw } 25515331Samw } 25525331Samw 25532796Smarks mutex_exit(&zp->z_lock); 25542796Smarks 25552796Smarks if (mask & AT_MODE) { 25565331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 25572796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 25582796Smarks &oldva, cr); 25592796Smarks if (err) { 25602796Smarks ZFS_EXIT(zfsvfs); 25612796Smarks return (err); 25622796Smarks } 25632796Smarks trim_mask |= AT_MODE; 25642796Smarks } else { 25652796Smarks need_policy = TRUE; 25662796Smarks } 25672796Smarks } 2568789Sahrens 2569789Sahrens if (need_policy) { 25701115Smarks /* 25711115Smarks * If trim_mask is set then take ownership 25722796Smarks * has been granted or write_acl is present and user 25732796Smarks * has the ability to modify mode. In that case remove 25742796Smarks * UID|GID and or MODE from mask so that 25751115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 25761115Smarks */ 25772796Smarks 25782796Smarks if (trim_mask) { 25792796Smarks saved_mask = vap->va_mask; 25802796Smarks vap->va_mask &= ~trim_mask; 25812796Smarks } 2582789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 25835331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2584789Sahrens if (err) { 2585789Sahrens ZFS_EXIT(zfsvfs); 2586789Sahrens return (err); 2587789Sahrens } 25881115Smarks 25891115Smarks if (trim_mask) 25902796Smarks vap->va_mask |= saved_mask; 2591789Sahrens } 2592789Sahrens 2593789Sahrens /* 2594789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2595789Sahrens * changed va_mask 2596789Sahrens */ 2597789Sahrens mask = vap->va_mask; 2598789Sahrens 2599789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2600789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 26015824Smarks if (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26025824Smarks ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid))) { 26035824Smarks if (zfsvfs->z_fuid_obj == 0) { 26045824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 26055824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 26065824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 26075824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 26085824Smarks } else { 26095824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 26105824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 26115824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 26125824Smarks } 26135331Samw } 2614789Sahrens 2615789Sahrens if (mask & AT_MODE) { 26161576Smarks uint64_t pmode = pzp->zp_mode; 26171576Smarks 26181576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2619789Sahrens 26205824Smarks if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) { 26215824Smarks dmu_tx_abort(tx); 26225824Smarks ZFS_EXIT(zfsvfs); 26235824Smarks return (err); 26245824Smarks } 26255331Samw if (pzp->zp_acl.z_acl_extern_obj) { 26265331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 26275331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 26285331Samw pzp->zp_acl.z_acl_version == 26295331Samw ZFS_ACL_VERSION_INITIAL) { 26305331Samw dmu_tx_hold_free(tx, 26315331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26325331Samw DMU_OBJECT_END); 26335331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26345824Smarks 0, aclp->z_acl_bytes); 26355331Samw } else { 26365331Samw dmu_tx_hold_write(tx, 26375331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26385824Smarks aclp->z_acl_bytes); 26395331Samw } 26406180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 26416180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26426180Smarks 0, aclp->z_acl_bytes); 26435331Samw } 2644789Sahrens } 2645789Sahrens 26465331Samw if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) { 26475331Samw err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 26481231Smarks if (err) { 26491231Smarks dmu_tx_abort(tx); 26501231Smarks ZFS_EXIT(zfsvfs); 26515824Smarks if (aclp) 26525824Smarks zfs_acl_free(aclp); 26531231Smarks return (err); 26541231Smarks } 26551231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 26561231Smarks } 26571231Smarks 2658789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2659789Sahrens if (err) { 26601231Smarks if (attrzp) 26611231Smarks VN_RELE(ZTOV(attrzp)); 26625824Smarks 26635824Smarks if (aclp) { 26645824Smarks zfs_acl_free(aclp); 26655824Smarks aclp = NULL; 26665824Smarks } 26675824Smarks 2668789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 26692113Sahrens dmu_tx_wait(tx); 26702113Sahrens dmu_tx_abort(tx); 2671789Sahrens goto top; 2672789Sahrens } 26732113Sahrens dmu_tx_abort(tx); 2674789Sahrens ZFS_EXIT(zfsvfs); 2675789Sahrens return (err); 2676789Sahrens } 2677789Sahrens 2678789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2679789Sahrens 2680789Sahrens /* 2681789Sahrens * Set each attribute requested. 2682789Sahrens * We group settings according to the locks they need to acquire. 2683789Sahrens * 2684789Sahrens * Note: you cannot set ctime directly, although it will be 2685789Sahrens * updated as a side-effect of calling this function. 2686789Sahrens */ 2687789Sahrens 2688789Sahrens mutex_enter(&zp->z_lock); 2689789Sahrens 2690789Sahrens if (mask & AT_MODE) { 26915824Smarks mutex_enter(&zp->z_acl_lock); 26925824Smarks zp->z_phys->zp_mode = new_mode; 26935824Smarks err = zfs_aclset_common(zp, aclp, cr, &fuidp, tx); 2694789Sahrens ASSERT3U(err, ==, 0); 26955824Smarks mutex_exit(&zp->z_acl_lock); 2696789Sahrens } 2697789Sahrens 26981231Smarks if (attrzp) 26991231Smarks mutex_enter(&attrzp->z_lock); 27001231Smarks 27011231Smarks if (mask & AT_UID) { 27025331Samw pzp->zp_uid = zfs_fuid_create(zfsvfs, 27035771Sjp151216 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp); 27041231Smarks if (attrzp) { 27055331Samw attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs, 27065771Sjp151216 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp); 27071231Smarks } 27081231Smarks } 27091231Smarks 27101231Smarks if (mask & AT_GID) { 27115331Samw pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid, 27125771Sjp151216 cr, ZFS_GROUP, tx, &fuidp); 27131231Smarks if (attrzp) 27145331Samw attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs, 27155771Sjp151216 vap->va_gid, cr, ZFS_GROUP, tx, &fuidp); 27161231Smarks } 27171231Smarks 27185824Smarks if (aclp) 27195824Smarks zfs_acl_free(aclp); 27205824Smarks 27211231Smarks if (attrzp) 27221231Smarks mutex_exit(&attrzp->z_lock); 2723789Sahrens 2724789Sahrens if (mask & AT_ATIME) 2725789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2726789Sahrens 2727789Sahrens if (mask & AT_MTIME) 2728789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2729789Sahrens 27306992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 27311878Smaybee if (mask & AT_SIZE) 2732789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 27331878Smaybee else if (mask != 0) 2734789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 27355331Samw /* 27365331Samw * Do this after setting timestamps to prevent timestamp 27375331Samw * update from toggling bit 27385331Samw */ 27395331Samw 27405331Samw if (xoap && (mask & AT_XVATTR)) { 27415331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 27425331Samw size_t len; 27435331Samw dmu_object_info_t doi; 27445331Samw 27455331Samw ASSERT(vp->v_type == VREG); 27465331Samw 27475331Samw /* Grow the bonus buffer if necessary. */ 27485331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 27495331Samw len = sizeof (xoap->xoa_av_scanstamp) + 27505331Samw sizeof (znode_phys_t); 27515331Samw if (len > doi.doi_bonus_size) 27525331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 27535331Samw } 27545331Samw zfs_xvattr_set(zp, xvap); 27555331Samw } 2756789Sahrens 27571878Smaybee if (mask != 0) 27585331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 27595331Samw 27605331Samw if (fuidp) 27615331Samw zfs_fuid_info_free(fuidp); 2762789Sahrens mutex_exit(&zp->z_lock); 2763789Sahrens 27641231Smarks if (attrzp) 27651231Smarks VN_RELE(ZTOV(attrzp)); 27661231Smarks 2767789Sahrens dmu_tx_commit(tx); 2768789Sahrens 2769789Sahrens ZFS_EXIT(zfsvfs); 2770789Sahrens return (err); 2771789Sahrens } 2772789Sahrens 27733271Smaybee typedef struct zfs_zlock { 27743271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 27753271Smaybee znode_t *zl_znode; /* znode we held */ 27763271Smaybee struct zfs_zlock *zl_next; /* next in list */ 27773271Smaybee } zfs_zlock_t; 27783271Smaybee 27793271Smaybee /* 27803271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 27813271Smaybee */ 27823271Smaybee static void 27833271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 27843271Smaybee { 27853271Smaybee zfs_zlock_t *zl; 27863271Smaybee 27873271Smaybee while ((zl = *zlpp) != NULL) { 27883271Smaybee if (zl->zl_znode != NULL) 27893271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 27903271Smaybee rw_exit(zl->zl_rwlock); 27913271Smaybee *zlpp = zl->zl_next; 27923271Smaybee kmem_free(zl, sizeof (*zl)); 27933271Smaybee } 27943271Smaybee } 27953271Smaybee 2796789Sahrens /* 2797789Sahrens * Search back through the directory tree, using the ".." entries. 2798789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2799789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2800789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2801789Sahrens */ 2802789Sahrens static int 2803789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2804789Sahrens { 2805789Sahrens zfs_zlock_t *zl; 28063638Sbillm znode_t *zp = tdzp; 2807789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2808789Sahrens uint64_t *oidp = &zp->z_id; 2809789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2810789Sahrens krw_t rw = RW_WRITER; 2811789Sahrens 2812789Sahrens /* 2813789Sahrens * First pass write-locks szp and compares to zp->z_id. 2814789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2815789Sahrens */ 2816789Sahrens do { 28173271Smaybee if (!rw_tryenter(rwlp, rw)) { 28183271Smaybee /* 28193271Smaybee * Another thread is renaming in this path. 28203271Smaybee * Note that if we are a WRITER, we don't have any 28213271Smaybee * parent_locks held yet. 28223271Smaybee */ 28233271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 28243271Smaybee /* 28253271Smaybee * Drop our locks and restart 28263271Smaybee */ 28273271Smaybee zfs_rename_unlock(&zl); 28283271Smaybee *zlpp = NULL; 28293271Smaybee zp = tdzp; 28303271Smaybee oidp = &zp->z_id; 28313271Smaybee rwlp = &szp->z_parent_lock; 28323271Smaybee rw = RW_WRITER; 28333271Smaybee continue; 28343271Smaybee } else { 28353271Smaybee /* 28363271Smaybee * Wait for other thread to drop its locks 28373271Smaybee */ 28383271Smaybee rw_enter(rwlp, rw); 28393271Smaybee } 28403271Smaybee } 28413271Smaybee 2842789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2843789Sahrens zl->zl_rwlock = rwlp; 2844789Sahrens zl->zl_znode = NULL; 2845789Sahrens zl->zl_next = *zlpp; 2846789Sahrens *zlpp = zl; 2847789Sahrens 2848789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2849789Sahrens return (EINVAL); 2850789Sahrens 2851789Sahrens if (*oidp == rootid) /* We've hit the top */ 2852789Sahrens return (0); 2853789Sahrens 2854789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2855789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2856789Sahrens if (error) 2857789Sahrens return (error); 2858789Sahrens zl->zl_znode = zp; 2859789Sahrens } 2860789Sahrens oidp = &zp->z_phys->zp_parent; 2861789Sahrens rwlp = &zp->z_parent_lock; 2862789Sahrens rw = RW_READER; 2863789Sahrens 2864789Sahrens } while (zp->z_id != sdzp->z_id); 2865789Sahrens 2866789Sahrens return (0); 2867789Sahrens } 2868789Sahrens 2869789Sahrens /* 2870789Sahrens * Move an entry from the provided source directory to the target 2871789Sahrens * directory. Change the entry name as indicated. 2872789Sahrens * 2873789Sahrens * IN: sdvp - Source directory containing the "old entry". 2874789Sahrens * snm - Old entry name. 2875789Sahrens * tdvp - Target directory to contain the "new entry". 2876789Sahrens * tnm - New entry name. 2877789Sahrens * cr - credentials of caller. 28785331Samw * ct - caller context 28795331Samw * flags - case flags 2880789Sahrens * 2881789Sahrens * RETURN: 0 if success 2882789Sahrens * error code if failure 2883789Sahrens * 2884789Sahrens * Timestamps: 2885789Sahrens * sdvp,tdvp - ctime|mtime updated 2886789Sahrens */ 28875331Samw /*ARGSUSED*/ 2888789Sahrens static int 28895331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 28905331Samw caller_context_t *ct, int flags) 2891789Sahrens { 2892789Sahrens znode_t *tdzp, *szp, *tzp; 2893789Sahrens znode_t *sdzp = VTOZ(sdvp); 2894789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 28955326Sek110237 zilog_t *zilog; 2896789Sahrens vnode_t *realvp; 2897789Sahrens zfs_dirlock_t *sdl, *tdl; 2898789Sahrens dmu_tx_t *tx; 2899789Sahrens zfs_zlock_t *zl; 29005331Samw int cmp, serr, terr; 29015331Samw int error = 0; 29025331Samw int zflg = 0; 2903789Sahrens 29045367Sahrens ZFS_ENTER(zfsvfs); 29055367Sahrens ZFS_VERIFY_ZP(sdzp); 29065326Sek110237 zilog = zfsvfs->z_log; 2907789Sahrens 2908789Sahrens /* 2909789Sahrens * Make sure we have the real vp for the target directory. 2910789Sahrens */ 29115331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 2912789Sahrens tdvp = realvp; 2913789Sahrens 2914789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2915789Sahrens ZFS_EXIT(zfsvfs); 2916789Sahrens return (EXDEV); 2917789Sahrens } 2918789Sahrens 2919789Sahrens tdzp = VTOZ(tdvp); 29205367Sahrens ZFS_VERIFY_ZP(tdzp); 29215498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 29225331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 29235331Samw ZFS_EXIT(zfsvfs); 29245331Samw return (EILSEQ); 29255331Samw } 29265331Samw 29275331Samw if (flags & FIGNORECASE) 29285331Samw zflg |= ZCILOOK; 29295331Samw 2930789Sahrens top: 2931789Sahrens szp = NULL; 2932789Sahrens tzp = NULL; 2933789Sahrens zl = NULL; 2934789Sahrens 2935789Sahrens /* 2936789Sahrens * This is to prevent the creation of links into attribute space 2937789Sahrens * by renaming a linked file into/outof an attribute directory. 2938789Sahrens * See the comment in zfs_link() for why this is considered bad. 2939789Sahrens */ 2940789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2941789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2942789Sahrens ZFS_EXIT(zfsvfs); 2943789Sahrens return (EINVAL); 2944789Sahrens } 2945789Sahrens 2946789Sahrens /* 2947789Sahrens * Lock source and target directory entries. To prevent deadlock, 2948789Sahrens * a lock ordering must be defined. We lock the directory with 2949789Sahrens * the smallest object id first, or if it's a tie, the one with 2950789Sahrens * the lexically first name. 2951789Sahrens */ 2952789Sahrens if (sdzp->z_id < tdzp->z_id) { 2953789Sahrens cmp = -1; 2954789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2955789Sahrens cmp = 1; 2956789Sahrens } else { 29575331Samw /* 29585331Samw * First compare the two name arguments without 29595331Samw * considering any case folding. 29605331Samw */ 29615331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 29625331Samw 29635331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 29645498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 2965789Sahrens if (cmp == 0) { 2966789Sahrens /* 2967789Sahrens * POSIX: "If the old argument and the new argument 2968789Sahrens * both refer to links to the same existing file, 2969789Sahrens * the rename() function shall return successfully 2970789Sahrens * and perform no other action." 2971789Sahrens */ 2972789Sahrens ZFS_EXIT(zfsvfs); 2973789Sahrens return (0); 2974789Sahrens } 29755331Samw /* 29765331Samw * If the file system is case-folding, then we may 29775331Samw * have some more checking to do. A case-folding file 29785331Samw * system is either supporting mixed case sensitivity 29795331Samw * access or is completely case-insensitive. Note 29805331Samw * that the file system is always case preserving. 29815331Samw * 29825331Samw * In mixed sensitivity mode case sensitive behavior 29835331Samw * is the default. FIGNORECASE must be used to 29845331Samw * explicitly request case insensitive behavior. 29855331Samw * 29865331Samw * If the source and target names provided differ only 29875331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 29885331Samw * we will treat this as a special case in the 29895331Samw * case-insensitive mode: as long as the source name 29905331Samw * is an exact match, we will allow this to proceed as 29915331Samw * a name-change request. 29925331Samw */ 29935498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 29945498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 29955498Stimh flags & FIGNORECASE)) && 29965331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 29975331Samw &error) == 0) { 29985331Samw /* 29995331Samw * case preserving rename request, require exact 30005331Samw * name matches 30015331Samw */ 30025331Samw zflg |= ZCIEXACT; 30035331Samw zflg &= ~ZCILOOK; 30045331Samw } 3005789Sahrens } 30065331Samw 3007789Sahrens if (cmp < 0) { 30085331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 30095331Samw ZEXISTS | zflg, NULL, NULL); 30105331Samw terr = zfs_dirent_lock(&tdl, 30115331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3012789Sahrens } else { 30135331Samw terr = zfs_dirent_lock(&tdl, 30145331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 30155331Samw serr = zfs_dirent_lock(&sdl, 30165331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 30175331Samw NULL, NULL); 3018789Sahrens } 3019789Sahrens 3020789Sahrens if (serr) { 3021789Sahrens /* 3022789Sahrens * Source entry invalid or not there. 3023789Sahrens */ 3024789Sahrens if (!terr) { 3025789Sahrens zfs_dirent_unlock(tdl); 3026789Sahrens if (tzp) 3027789Sahrens VN_RELE(ZTOV(tzp)); 3028789Sahrens } 3029789Sahrens if (strcmp(snm, "..") == 0) 3030789Sahrens serr = EINVAL; 3031789Sahrens ZFS_EXIT(zfsvfs); 3032789Sahrens return (serr); 3033789Sahrens } 3034789Sahrens if (terr) { 3035789Sahrens zfs_dirent_unlock(sdl); 3036789Sahrens VN_RELE(ZTOV(szp)); 3037789Sahrens if (strcmp(tnm, "..") == 0) 3038789Sahrens terr = EINVAL; 3039789Sahrens ZFS_EXIT(zfsvfs); 3040789Sahrens return (terr); 3041789Sahrens } 3042789Sahrens 3043789Sahrens /* 3044789Sahrens * Must have write access at the source to remove the old entry 3045789Sahrens * and write access at the target to create the new entry. 3046789Sahrens * Note that if target and source are the same, this can be 3047789Sahrens * done in a single check. 3048789Sahrens */ 3049789Sahrens 3050789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3051789Sahrens goto out; 3052789Sahrens 3053789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3054789Sahrens /* 3055789Sahrens * Check to make sure rename is valid. 3056789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3057789Sahrens */ 3058789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3059789Sahrens goto out; 3060789Sahrens } 3061789Sahrens 3062789Sahrens /* 3063789Sahrens * Does target exist? 3064789Sahrens */ 3065789Sahrens if (tzp) { 3066789Sahrens /* 3067789Sahrens * Source and target must be the same type. 3068789Sahrens */ 3069789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3070789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3071789Sahrens error = ENOTDIR; 3072789Sahrens goto out; 3073789Sahrens } 3074789Sahrens } else { 3075789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3076789Sahrens error = EISDIR; 3077789Sahrens goto out; 3078789Sahrens } 3079789Sahrens } 3080789Sahrens /* 3081789Sahrens * POSIX dictates that when the source and target 3082789Sahrens * entries refer to the same file object, rename 3083789Sahrens * must do nothing and exit without error. 3084789Sahrens */ 3085789Sahrens if (szp->z_id == tzp->z_id) { 3086789Sahrens error = 0; 3087789Sahrens goto out; 3088789Sahrens } 3089789Sahrens } 3090789Sahrens 30915331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3092789Sahrens if (tzp) 30935331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 30944863Spraks 30954863Spraks /* 30964863Spraks * notify the target directory if it is not the same 30974863Spraks * as source directory. 30984863Spraks */ 30994863Spraks if (tdvp != sdvp) { 31005331Samw vnevent_rename_dest_dir(tdvp, ct); 31014863Spraks } 3102789Sahrens 3103789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3104789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3105789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 31061544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 31071544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 31081544Seschrock if (sdzp != tdzp) 3109789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 31101544Seschrock if (tzp) 31111544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 31123461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3113789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 3114789Sahrens if (error) { 3115789Sahrens if (zl != NULL) 3116789Sahrens zfs_rename_unlock(&zl); 3117789Sahrens zfs_dirent_unlock(sdl); 3118789Sahrens zfs_dirent_unlock(tdl); 3119789Sahrens VN_RELE(ZTOV(szp)); 3120789Sahrens if (tzp) 3121789Sahrens VN_RELE(ZTOV(tzp)); 3122789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 31232113Sahrens dmu_tx_wait(tx); 31242113Sahrens dmu_tx_abort(tx); 3125789Sahrens goto top; 3126789Sahrens } 31272113Sahrens dmu_tx_abort(tx); 3128789Sahrens ZFS_EXIT(zfsvfs); 3129789Sahrens return (error); 3130789Sahrens } 3131789Sahrens 3132789Sahrens if (tzp) /* Attempt to remove the existing target */ 31335331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3134789Sahrens 3135789Sahrens if (error == 0) { 3136789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3137789Sahrens if (error == 0) { 31385331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 31395331Samw 3140789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3141789Sahrens ASSERT(error == 0); 31425331Samw 31435331Samw zfs_log_rename(zilog, tx, 31445331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 31455331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 31466976Seschrock 31476976Seschrock /* Update path information for the target vnode */ 31486976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3149789Sahrens } 3150789Sahrens } 3151789Sahrens 3152789Sahrens dmu_tx_commit(tx); 3153789Sahrens out: 3154789Sahrens if (zl != NULL) 3155789Sahrens zfs_rename_unlock(&zl); 3156789Sahrens 3157789Sahrens zfs_dirent_unlock(sdl); 3158789Sahrens zfs_dirent_unlock(tdl); 3159789Sahrens 3160789Sahrens VN_RELE(ZTOV(szp)); 3161789Sahrens if (tzp) 3162789Sahrens VN_RELE(ZTOV(tzp)); 3163789Sahrens 3164789Sahrens ZFS_EXIT(zfsvfs); 3165789Sahrens return (error); 3166789Sahrens } 3167789Sahrens 3168789Sahrens /* 3169789Sahrens * Insert the indicated symbolic reference entry into the directory. 3170789Sahrens * 3171789Sahrens * IN: dvp - Directory to contain new symbolic link. 3172789Sahrens * link - Name for new symlink entry. 3173789Sahrens * vap - Attributes of new entry. 3174789Sahrens * target - Target path of new symlink. 3175789Sahrens * cr - credentials of caller. 31765331Samw * ct - caller context 31775331Samw * flags - case flags 3178789Sahrens * 3179789Sahrens * RETURN: 0 if success 3180789Sahrens * error code if failure 3181789Sahrens * 3182789Sahrens * Timestamps: 3183789Sahrens * dvp - ctime|mtime updated 3184789Sahrens */ 31855331Samw /*ARGSUSED*/ 3186789Sahrens static int 31875331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 31885331Samw caller_context_t *ct, int flags) 3189789Sahrens { 3190789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3191789Sahrens zfs_dirlock_t *dl; 3192789Sahrens dmu_tx_t *tx; 3193789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 31945326Sek110237 zilog_t *zilog; 3195789Sahrens int len = strlen(link); 3196789Sahrens int error; 31975331Samw int zflg = ZNEW; 31985331Samw zfs_fuid_info_t *fuidp = NULL; 3199789Sahrens 3200789Sahrens ASSERT(vap->va_type == VLNK); 3201789Sahrens 32025367Sahrens ZFS_ENTER(zfsvfs); 32035367Sahrens ZFS_VERIFY_ZP(dzp); 32045326Sek110237 zilog = zfsvfs->z_log; 32055331Samw 32065498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 32075331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 32085331Samw ZFS_EXIT(zfsvfs); 32095331Samw return (EILSEQ); 32105331Samw } 32115331Samw if (flags & FIGNORECASE) 32125331Samw zflg |= ZCILOOK; 3213789Sahrens top: 32145331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3215789Sahrens ZFS_EXIT(zfsvfs); 3216789Sahrens return (error); 3217789Sahrens } 3218789Sahrens 3219789Sahrens if (len > MAXPATHLEN) { 3220789Sahrens ZFS_EXIT(zfsvfs); 3221789Sahrens return (ENAMETOOLONG); 3222789Sahrens } 3223789Sahrens 3224789Sahrens /* 3225789Sahrens * Attempt to lock directory; fail if entry already exists. 3226789Sahrens */ 32275331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 32285331Samw if (error) { 3229789Sahrens ZFS_EXIT(zfsvfs); 3230789Sahrens return (error); 3231789Sahrens } 3232789Sahrens 3233789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3234789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3235789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 32361544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3237789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 3238789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 32395824Smarks if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) { 32405824Smarks if (zfsvfs->z_fuid_obj == 0) { 32415824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 32425824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 32435824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32445824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 32455824Smarks } else { 32465824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 32475824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 32485824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32495824Smarks } 32505331Samw } 3251789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 3252789Sahrens if (error) { 3253789Sahrens zfs_dirent_unlock(dl); 3254789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 32552113Sahrens dmu_tx_wait(tx); 32562113Sahrens dmu_tx_abort(tx); 3257789Sahrens goto top; 3258789Sahrens } 32592113Sahrens dmu_tx_abort(tx); 3260789Sahrens ZFS_EXIT(zfsvfs); 3261789Sahrens return (error); 3262789Sahrens } 3263789Sahrens 3264789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3265789Sahrens 3266789Sahrens /* 3267789Sahrens * Create a new object for the symlink. 3268789Sahrens * Put the link content into bonus buffer if it will fit; 3269789Sahrens * otherwise, store it just like any other file data. 3270789Sahrens */ 3271789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 32725446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp); 3273789Sahrens if (len != 0) 3274789Sahrens bcopy(link, zp->z_phys + 1, len); 3275789Sahrens } else { 3276789Sahrens dmu_buf_t *dbp; 32771669Sperrin 32785446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp); 32791669Sperrin /* 32801669Sperrin * Nothing can access the znode yet so no locking needed 32811669Sperrin * for growing the znode's blocksize. 32821669Sperrin */ 32831669Sperrin zfs_grow_blocksize(zp, len, tx); 3284789Sahrens 32855446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 32865446Sahrens zp->z_id, 0, FTAG, &dbp)); 3287789Sahrens dmu_buf_will_dirty(dbp, tx); 3288789Sahrens 3289789Sahrens ASSERT3U(len, <=, dbp->db_size); 3290789Sahrens bcopy(link, dbp->db_data, len); 32911544Seschrock dmu_buf_rele(dbp, FTAG); 3292789Sahrens } 3293789Sahrens zp->z_phys->zp_size = len; 3294789Sahrens 3295789Sahrens /* 3296789Sahrens * Insert the new object into the directory. 3297789Sahrens */ 3298789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 3299789Sahrens out: 33005331Samw if (error == 0) { 33015331Samw uint64_t txtype = TX_SYMLINK; 33025331Samw if (flags & FIGNORECASE) 33035331Samw txtype |= TX_CI; 33045331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 33055331Samw } 33065331Samw if (fuidp) 33075331Samw zfs_fuid_info_free(fuidp); 3308789Sahrens 3309789Sahrens dmu_tx_commit(tx); 3310789Sahrens 3311789Sahrens zfs_dirent_unlock(dl); 3312789Sahrens 3313789Sahrens VN_RELE(ZTOV(zp)); 3314789Sahrens 3315789Sahrens ZFS_EXIT(zfsvfs); 3316789Sahrens return (error); 3317789Sahrens } 3318789Sahrens 3319789Sahrens /* 3320789Sahrens * Return, in the buffer contained in the provided uio structure, 3321789Sahrens * the symbolic path referred to by vp. 3322789Sahrens * 3323789Sahrens * IN: vp - vnode of symbolic link. 3324789Sahrens * uoip - structure to contain the link path. 3325789Sahrens * cr - credentials of caller. 33265331Samw * ct - caller context 3327789Sahrens * 3328789Sahrens * OUT: uio - structure to contain the link path. 3329789Sahrens * 3330789Sahrens * RETURN: 0 if success 3331789Sahrens * error code if failure 3332789Sahrens * 3333789Sahrens * Timestamps: 3334789Sahrens * vp - atime updated 3335789Sahrens */ 3336789Sahrens /* ARGSUSED */ 3337789Sahrens static int 33385331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3339789Sahrens { 3340789Sahrens znode_t *zp = VTOZ(vp); 3341789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3342789Sahrens size_t bufsz; 3343789Sahrens int error; 3344789Sahrens 33455367Sahrens ZFS_ENTER(zfsvfs); 33465367Sahrens ZFS_VERIFY_ZP(zp); 3347789Sahrens 3348789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3349789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3350789Sahrens error = uiomove(zp->z_phys + 1, 3351789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3352789Sahrens } else { 33531544Seschrock dmu_buf_t *dbp; 33541544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 33551544Seschrock if (error) { 3356789Sahrens ZFS_EXIT(zfsvfs); 3357789Sahrens return (error); 3358789Sahrens } 3359789Sahrens error = uiomove(dbp->db_data, 3360789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 33611544Seschrock dmu_buf_rele(dbp, FTAG); 3362789Sahrens } 3363789Sahrens 3364789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3365789Sahrens ZFS_EXIT(zfsvfs); 3366789Sahrens return (error); 3367789Sahrens } 3368789Sahrens 3369789Sahrens /* 3370789Sahrens * Insert a new entry into directory tdvp referencing svp. 3371789Sahrens * 3372789Sahrens * IN: tdvp - Directory to contain new entry. 3373789Sahrens * svp - vnode of new entry. 3374789Sahrens * name - name of new entry. 3375789Sahrens * cr - credentials of caller. 33765331Samw * ct - caller context 3377789Sahrens * 3378789Sahrens * RETURN: 0 if success 3379789Sahrens * error code if failure 3380789Sahrens * 3381789Sahrens * Timestamps: 3382789Sahrens * tdvp - ctime|mtime updated 3383789Sahrens * svp - ctime updated 3384789Sahrens */ 3385789Sahrens /* ARGSUSED */ 3386789Sahrens static int 33875331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 33885331Samw caller_context_t *ct, int flags) 3389789Sahrens { 3390789Sahrens znode_t *dzp = VTOZ(tdvp); 3391789Sahrens znode_t *tzp, *szp; 3392789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 33935326Sek110237 zilog_t *zilog; 3394789Sahrens zfs_dirlock_t *dl; 3395789Sahrens dmu_tx_t *tx; 3396789Sahrens vnode_t *realvp; 3397789Sahrens int error; 33985331Samw int zf = ZNEW; 33995331Samw uid_t owner; 3400789Sahrens 3401789Sahrens ASSERT(tdvp->v_type == VDIR); 3402789Sahrens 34035367Sahrens ZFS_ENTER(zfsvfs); 34045367Sahrens ZFS_VERIFY_ZP(dzp); 34055326Sek110237 zilog = zfsvfs->z_log; 3406789Sahrens 34075331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3408789Sahrens svp = realvp; 3409789Sahrens 3410789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3411789Sahrens ZFS_EXIT(zfsvfs); 3412789Sahrens return (EXDEV); 3413789Sahrens } 34145367Sahrens szp = VTOZ(svp); 34155367Sahrens ZFS_VERIFY_ZP(szp); 3416789Sahrens 34175498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 34185331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 34195331Samw ZFS_EXIT(zfsvfs); 34205331Samw return (EILSEQ); 34215331Samw } 34225331Samw if (flags & FIGNORECASE) 34235331Samw zf |= ZCILOOK; 34245331Samw 3425789Sahrens top: 3426789Sahrens /* 3427789Sahrens * We do not support links between attributes and non-attributes 3428789Sahrens * because of the potential security risk of creating links 3429789Sahrens * into "normal" file space in order to circumvent restrictions 3430789Sahrens * imposed in attribute space. 3431789Sahrens */ 3432789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3433789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3434789Sahrens ZFS_EXIT(zfsvfs); 3435789Sahrens return (EINVAL); 3436789Sahrens } 3437789Sahrens 3438789Sahrens /* 3439789Sahrens * POSIX dictates that we return EPERM here. 3440789Sahrens * Better choices include ENOTSUP or EISDIR. 3441789Sahrens */ 3442789Sahrens if (svp->v_type == VDIR) { 3443789Sahrens ZFS_EXIT(zfsvfs); 3444789Sahrens return (EPERM); 3445789Sahrens } 3446789Sahrens 34475959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 34485331Samw if (owner != crgetuid(cr) && 3449789Sahrens secpolicy_basic_link(cr) != 0) { 3450789Sahrens ZFS_EXIT(zfsvfs); 3451789Sahrens return (EPERM); 3452789Sahrens } 3453789Sahrens 34545331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3455789Sahrens ZFS_EXIT(zfsvfs); 3456789Sahrens return (error); 3457789Sahrens } 3458789Sahrens 3459789Sahrens /* 3460789Sahrens * Attempt to lock directory; fail if entry already exists. 3461789Sahrens */ 34625331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 34635331Samw if (error) { 3464789Sahrens ZFS_EXIT(zfsvfs); 3465789Sahrens return (error); 3466789Sahrens } 3467789Sahrens 3468789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3469789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 34701544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3471789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 3472789Sahrens if (error) { 3473789Sahrens zfs_dirent_unlock(dl); 3474789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 34752113Sahrens dmu_tx_wait(tx); 34762113Sahrens dmu_tx_abort(tx); 3477789Sahrens goto top; 3478789Sahrens } 34792113Sahrens dmu_tx_abort(tx); 3480789Sahrens ZFS_EXIT(zfsvfs); 3481789Sahrens return (error); 3482789Sahrens } 3483789Sahrens 3484789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3485789Sahrens 34865331Samw if (error == 0) { 34875331Samw uint64_t txtype = TX_LINK; 34885331Samw if (flags & FIGNORECASE) 34895331Samw txtype |= TX_CI; 34905331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 34915331Samw } 3492789Sahrens 3493789Sahrens dmu_tx_commit(tx); 3494789Sahrens 3495789Sahrens zfs_dirent_unlock(dl); 3496789Sahrens 34974863Spraks if (error == 0) { 34985331Samw vnevent_link(svp, ct); 34994863Spraks } 35004863Spraks 3501789Sahrens ZFS_EXIT(zfsvfs); 3502789Sahrens return (error); 3503789Sahrens } 3504789Sahrens 3505789Sahrens /* 3506789Sahrens * zfs_null_putapage() is used when the file system has been force 3507789Sahrens * unmounted. It just drops the pages. 3508789Sahrens */ 3509789Sahrens /* ARGSUSED */ 3510789Sahrens static int 3511789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3512789Sahrens size_t *lenp, int flags, cred_t *cr) 3513789Sahrens { 3514789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3515789Sahrens return (0); 3516789Sahrens } 3517789Sahrens 35182688Smaybee /* 35192688Smaybee * Push a page out to disk, klustering if possible. 35202688Smaybee * 35212688Smaybee * IN: vp - file to push page to. 35222688Smaybee * pp - page to push. 35232688Smaybee * flags - additional flags. 35242688Smaybee * cr - credentials of caller. 35252688Smaybee * 35262688Smaybee * OUT: offp - start of range pushed. 35272688Smaybee * lenp - len of range pushed. 35282688Smaybee * 35292688Smaybee * RETURN: 0 if success 35302688Smaybee * error code if failure 35312688Smaybee * 35322688Smaybee * NOTE: callers must have locked the page to be pushed. On 35332688Smaybee * exit, the page (and all other pages in the kluster) must be 35342688Smaybee * unlocked. 35352688Smaybee */ 3536789Sahrens /* ARGSUSED */ 3537789Sahrens static int 3538789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3539789Sahrens size_t *lenp, int flags, cred_t *cr) 3540789Sahrens { 3541789Sahrens znode_t *zp = VTOZ(vp); 3542789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3543789Sahrens zilog_t *zilog = zfsvfs->z_log; 3544789Sahrens dmu_tx_t *tx; 35451669Sperrin rl_t *rl; 35462688Smaybee u_offset_t off, koff; 35472688Smaybee size_t len, klen; 35484709Smaybee uint64_t filesz; 3549789Sahrens int err; 3550789Sahrens 35514709Smaybee filesz = zp->z_phys->zp_size; 35522688Smaybee off = pp->p_offset; 35532688Smaybee len = PAGESIZE; 35542688Smaybee /* 35552688Smaybee * If our blocksize is bigger than the page size, try to kluster 35562688Smaybee * muiltiple pages so that we write a full block (thus avoiding 35572688Smaybee * a read-modify-write). 35582688Smaybee */ 35594709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 35602688Smaybee if (!ISP2(zp->z_blksz)) { 35612688Smaybee /* Only one block in the file. */ 35622688Smaybee klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 35632688Smaybee koff = 0; 35642688Smaybee } else { 35652688Smaybee klen = zp->z_blksz; 35662688Smaybee koff = P2ALIGN(off, (u_offset_t)klen); 35672688Smaybee } 35682688Smaybee ASSERT(koff <= filesz); 35692688Smaybee if (koff + klen > filesz) 35702688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 35712688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 35722688Smaybee } 35732688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 3574789Sahrens top: 35752688Smaybee rl = zfs_range_lock(zp, off, len, RL_WRITER); 35761819Smaybee /* 35771819Smaybee * Can't push pages past end-of-file. 35781819Smaybee */ 35794709Smaybee filesz = zp->z_phys->zp_size; 35804709Smaybee if (off >= filesz) { 35814709Smaybee /* ignore all pages */ 35822688Smaybee err = 0; 35832688Smaybee goto out; 35844709Smaybee } else if (off + len > filesz) { 35854709Smaybee int npages = btopr(filesz - off); 35862688Smaybee page_t *trunc; 35872688Smaybee 35882688Smaybee page_list_break(&pp, &trunc, npages); 35894709Smaybee /* ignore pages past end of file */ 35902688Smaybee if (trunc) 35914709Smaybee pvn_write_done(trunc, flags); 35924709Smaybee len = filesz - off; 35931819Smaybee } 3594789Sahrens 3595789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3596789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3597789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3598789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 3599789Sahrens if (err != 0) { 3600789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 36012688Smaybee zfs_range_unlock(rl); 36022113Sahrens dmu_tx_wait(tx); 36032113Sahrens dmu_tx_abort(tx); 36042688Smaybee err = 0; 3605789Sahrens goto top; 3606789Sahrens } 36072113Sahrens dmu_tx_abort(tx); 3608789Sahrens goto out; 3609789Sahrens } 3610789Sahrens 36112688Smaybee if (zp->z_blksz <= PAGESIZE) { 36122688Smaybee caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1); 36132688Smaybee ASSERT3U(len, <=, PAGESIZE); 36142688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 36152688Smaybee ppmapout(va); 36162688Smaybee } else { 36172688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 36182688Smaybee } 36192688Smaybee 36202688Smaybee if (err == 0) { 36212688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 36223638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0); 36232688Smaybee dmu_tx_commit(tx); 36242688Smaybee } 36252688Smaybee 36262688Smaybee out: 36272237Smaybee zfs_range_unlock(rl); 36284709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3629789Sahrens if (offp) 3630789Sahrens *offp = off; 3631789Sahrens if (lenp) 3632789Sahrens *lenp = len; 3633789Sahrens 3634789Sahrens return (err); 3635789Sahrens } 3636789Sahrens 3637789Sahrens /* 3638789Sahrens * Copy the portion of the file indicated from pages into the file. 3639789Sahrens * The pages are stored in a page list attached to the files vnode. 3640789Sahrens * 3641789Sahrens * IN: vp - vnode of file to push page data to. 3642789Sahrens * off - position in file to put data. 3643789Sahrens * len - amount of data to write. 3644789Sahrens * flags - flags to control the operation. 3645789Sahrens * cr - credentials of caller. 36465331Samw * ct - caller context. 3647789Sahrens * 3648789Sahrens * RETURN: 0 if success 3649789Sahrens * error code if failure 3650789Sahrens * 3651789Sahrens * Timestamps: 3652789Sahrens * vp - ctime|mtime updated 3653789Sahrens */ 36545331Samw /*ARGSUSED*/ 3655789Sahrens static int 36565331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 36575331Samw caller_context_t *ct) 3658789Sahrens { 3659789Sahrens znode_t *zp = VTOZ(vp); 3660789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3661789Sahrens page_t *pp; 3662789Sahrens size_t io_len; 3663789Sahrens u_offset_t io_off; 36641669Sperrin uint64_t filesz; 3665789Sahrens int error = 0; 3666789Sahrens 36675367Sahrens ZFS_ENTER(zfsvfs); 36685367Sahrens ZFS_VERIFY_ZP(zp); 3669789Sahrens 3670789Sahrens if (len == 0) { 3671789Sahrens /* 3672789Sahrens * Search the entire vp list for pages >= off. 3673789Sahrens */ 3674789Sahrens error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 3675789Sahrens flags, cr); 36761472Sperrin goto out; 3677789Sahrens } 3678789Sahrens 36791669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 36801669Sperrin if (off > filesz) { 3681789Sahrens /* past end of file */ 3682789Sahrens ZFS_EXIT(zfsvfs); 3683789Sahrens return (0); 3684789Sahrens } 3685789Sahrens 36861669Sperrin len = MIN(len, filesz - off); 3687789Sahrens 36881472Sperrin for (io_off = off; io_off < off + len; io_off += io_len) { 3689789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 36901669Sperrin pp = page_lookup(vp, io_off, 36914339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3692789Sahrens } else { 3693789Sahrens pp = page_lookup_nowait(vp, io_off, 36944339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3695789Sahrens } 3696789Sahrens 3697789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3698789Sahrens int err; 3699789Sahrens 3700789Sahrens /* 3701789Sahrens * Found a dirty page to push 3702789Sahrens */ 37031669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 37041669Sperrin if (err) 3705789Sahrens error = err; 3706789Sahrens } else { 3707789Sahrens io_len = PAGESIZE; 3708789Sahrens } 3709789Sahrens } 37101472Sperrin out: 37112638Sperrin if ((flags & B_ASYNC) == 0) 37122638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3713789Sahrens ZFS_EXIT(zfsvfs); 3714789Sahrens return (error); 3715789Sahrens } 3716789Sahrens 37175331Samw /*ARGSUSED*/ 3718789Sahrens void 37195331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3720789Sahrens { 3721789Sahrens znode_t *zp = VTOZ(vp); 3722789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3723789Sahrens int error; 3724789Sahrens 37255326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 37265446Sahrens if (zp->z_dbuf == NULL) { 37275446Sahrens /* 37285642Smaybee * The fs has been unmounted, or we did a 37295642Smaybee * suspend/resume and this file no longer exists. 37305446Sahrens */ 3731789Sahrens if (vn_has_cached_data(vp)) { 3732789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3733789Sahrens B_INVAL, cr); 3734789Sahrens } 3735789Sahrens 37361544Seschrock mutex_enter(&zp->z_lock); 3737789Sahrens vp->v_count = 0; /* count arrives as 1 */ 37385446Sahrens mutex_exit(&zp->z_lock); 37395642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 37405446Sahrens zfs_znode_free(zp); 3741789Sahrens return; 3742789Sahrens } 3743789Sahrens 3744789Sahrens /* 3745789Sahrens * Attempt to push any data in the page cache. If this fails 3746789Sahrens * we will get kicked out later in zfs_zinactive(). 3747789Sahrens */ 37481298Sperrin if (vn_has_cached_data(vp)) { 37491298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 37501298Sperrin cr); 37511298Sperrin } 3752789Sahrens 37533461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3754789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3755789Sahrens 3756789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3757789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3758789Sahrens if (error) { 3759789Sahrens dmu_tx_abort(tx); 3760789Sahrens } else { 3761789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3762789Sahrens mutex_enter(&zp->z_lock); 3763789Sahrens zp->z_atime_dirty = 0; 3764789Sahrens mutex_exit(&zp->z_lock); 3765789Sahrens dmu_tx_commit(tx); 3766789Sahrens } 3767789Sahrens } 3768789Sahrens 3769789Sahrens zfs_zinactive(zp); 37705326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3771789Sahrens } 3772789Sahrens 3773789Sahrens /* 3774789Sahrens * Bounds-check the seek operation. 3775789Sahrens * 3776789Sahrens * IN: vp - vnode seeking within 3777789Sahrens * ooff - old file offset 3778789Sahrens * noffp - pointer to new file offset 37795331Samw * ct - caller context 3780789Sahrens * 3781789Sahrens * RETURN: 0 if success 3782789Sahrens * EINVAL if new offset invalid 3783789Sahrens */ 3784789Sahrens /* ARGSUSED */ 3785789Sahrens static int 37865331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 37875331Samw caller_context_t *ct) 3788789Sahrens { 3789789Sahrens if (vp->v_type == VDIR) 3790789Sahrens return (0); 3791789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3792789Sahrens } 3793789Sahrens 3794789Sahrens /* 3795789Sahrens * Pre-filter the generic locking function to trap attempts to place 3796789Sahrens * a mandatory lock on a memory mapped file. 3797789Sahrens */ 3798789Sahrens static int 3799789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 38005331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 3801789Sahrens { 3802789Sahrens znode_t *zp = VTOZ(vp); 3803789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3804789Sahrens int error; 3805789Sahrens 38065367Sahrens ZFS_ENTER(zfsvfs); 38075367Sahrens ZFS_VERIFY_ZP(zp); 3808789Sahrens 3809789Sahrens /* 38101544Seschrock * We are following the UFS semantics with respect to mapcnt 38111544Seschrock * here: If we see that the file is mapped already, then we will 38121544Seschrock * return an error, but we don't worry about races between this 38131544Seschrock * function and zfs_map(). 3814789Sahrens */ 38151544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3816789Sahrens ZFS_EXIT(zfsvfs); 3817789Sahrens return (EAGAIN); 3818789Sahrens } 38195331Samw error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct); 3820789Sahrens ZFS_EXIT(zfsvfs); 3821789Sahrens return (error); 3822789Sahrens } 3823789Sahrens 3824789Sahrens /* 3825789Sahrens * If we can't find a page in the cache, we will create a new page 3826789Sahrens * and fill it with file data. For efficiency, we may try to fill 38271669Sperrin * multiple pages at once (klustering). 3828789Sahrens */ 3829789Sahrens static int 3830789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3831789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3832789Sahrens { 3833789Sahrens znode_t *zp = VTOZ(vp); 3834789Sahrens page_t *pp, *cur_pp; 3835789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3836789Sahrens caddr_t va; 3837789Sahrens u_offset_t io_off, total; 3838789Sahrens uint64_t oid = zp->z_id; 3839789Sahrens size_t io_len; 38401669Sperrin uint64_t filesz; 3841789Sahrens int err; 3842789Sahrens 3843789Sahrens /* 3844789Sahrens * If we are only asking for a single page don't bother klustering. 3845789Sahrens */ 38461669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 38472688Smaybee if (off >= filesz) 38482688Smaybee return (EFAULT); 38492688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3850789Sahrens io_off = off; 3851789Sahrens io_len = PAGESIZE; 3852789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3853789Sahrens } else { 3854789Sahrens /* 3855789Sahrens * Try to fill a kluster of pages (a blocks worth). 3856789Sahrens */ 3857789Sahrens size_t klen; 3858789Sahrens u_offset_t koff; 3859789Sahrens 3860789Sahrens if (!ISP2(zp->z_blksz)) { 3861789Sahrens /* Only one block in the file. */ 3862789Sahrens klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3863789Sahrens koff = 0; 3864789Sahrens } else { 38653131Sgw25295 /* 38663131Sgw25295 * It would be ideal to align our offset to the 38673131Sgw25295 * blocksize but doing so has resulted in some 38683131Sgw25295 * strange application crashes. For now, we 38693131Sgw25295 * leave the offset as is and only adjust the 38703131Sgw25295 * length if we are off the end of the file. 38713131Sgw25295 */ 38723131Sgw25295 koff = off; 3873789Sahrens klen = plsz; 3874789Sahrens } 38751819Smaybee ASSERT(koff <= filesz); 38761819Smaybee if (koff + klen > filesz) 38771819Smaybee klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 38782688Smaybee ASSERT3U(off, >=, koff); 38792688Smaybee ASSERT3U(off, <, koff + klen); 3880789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 38814339Sperrin &io_len, koff, klen, 0); 3882789Sahrens } 3883789Sahrens if (pp == NULL) { 3884789Sahrens /* 3885789Sahrens * Some other thread entered the page before us. 3886789Sahrens * Return to zfs_getpage to retry the lookup. 3887789Sahrens */ 3888789Sahrens *pl = NULL; 3889789Sahrens return (0); 3890789Sahrens } 3891789Sahrens 3892789Sahrens /* 3893789Sahrens * Fill the pages in the kluster. 3894789Sahrens */ 3895789Sahrens cur_pp = pp; 3896789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 38972688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 3898789Sahrens va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 38991544Seschrock err = dmu_read(os, oid, io_off, PAGESIZE, va); 3900789Sahrens ppmapout(va); 3901789Sahrens if (err) { 3902789Sahrens /* On error, toss the entire kluster */ 3903789Sahrens pvn_read_done(pp, B_ERROR); 3904*7294Sperrin /* convert checksum errors into IO errors */ 3905*7294Sperrin if (err == ECKSUM) 3906*7294Sperrin err = EIO; 3907789Sahrens return (err); 3908789Sahrens } 3909789Sahrens cur_pp = cur_pp->p_next; 3910789Sahrens } 3911789Sahrens out: 3912789Sahrens /* 3913789Sahrens * Fill in the page list array from the kluster. If 3914789Sahrens * there are too many pages in the kluster, return 3915789Sahrens * as many pages as possible starting from the desired 3916789Sahrens * offset `off'. 3917789Sahrens * NOTE: the page list will always be null terminated. 3918789Sahrens */ 3919789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3920789Sahrens 3921789Sahrens return (0); 3922789Sahrens } 3923789Sahrens 3924789Sahrens /* 3925789Sahrens * Return pointers to the pages for the file region [off, off + len] 3926789Sahrens * in the pl array. If plsz is greater than len, this function may 3927789Sahrens * also return page pointers from before or after the specified 3928789Sahrens * region (i.e. some region [off', off' + plsz]). These additional 3929789Sahrens * pages are only returned if they are already in the cache, or were 3930789Sahrens * created as part of a klustered read. 3931789Sahrens * 3932789Sahrens * IN: vp - vnode of file to get data from. 3933789Sahrens * off - position in file to get data from. 3934789Sahrens * len - amount of data to retrieve. 3935789Sahrens * plsz - length of provided page list. 3936789Sahrens * seg - segment to obtain pages for. 3937789Sahrens * addr - virtual address of fault. 3938789Sahrens * rw - mode of created pages. 3939789Sahrens * cr - credentials of caller. 39405331Samw * ct - caller context. 3941789Sahrens * 3942789Sahrens * OUT: protp - protection mode of created pages. 3943789Sahrens * pl - list of pages created. 3944789Sahrens * 3945789Sahrens * RETURN: 0 if success 3946789Sahrens * error code if failure 3947789Sahrens * 3948789Sahrens * Timestamps: 3949789Sahrens * vp - atime updated 3950789Sahrens */ 3951789Sahrens /* ARGSUSED */ 3952789Sahrens static int 3953789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3954789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 39555331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 3956789Sahrens { 3957789Sahrens znode_t *zp = VTOZ(vp); 3958789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3959789Sahrens page_t *pp, **pl0 = pl; 39602752Sperrin int need_unlock = 0, err = 0; 39612752Sperrin offset_t orig_off; 3962789Sahrens 39635367Sahrens ZFS_ENTER(zfsvfs); 39645367Sahrens ZFS_VERIFY_ZP(zp); 3965789Sahrens 3966789Sahrens if (protp) 3967789Sahrens *protp = PROT_ALL; 3968789Sahrens 3969789Sahrens /* no faultahead (for now) */ 3970789Sahrens if (pl == NULL) { 3971789Sahrens ZFS_EXIT(zfsvfs); 3972789Sahrens return (0); 3973789Sahrens } 3974789Sahrens 3975789Sahrens /* can't fault past EOF */ 3976789Sahrens if (off >= zp->z_phys->zp_size) { 3977789Sahrens ZFS_EXIT(zfsvfs); 3978789Sahrens return (EFAULT); 3979789Sahrens } 39802752Sperrin orig_off = off; 3981789Sahrens 3982789Sahrens /* 3983789Sahrens * If we already own the lock, then we must be page faulting 3984789Sahrens * in the middle of a write to this file (i.e., we are writing 3985789Sahrens * to this file using data from a mapped region of the file). 3986789Sahrens */ 39872752Sperrin if (rw_owner(&zp->z_map_lock) != curthread) { 3988789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 3989789Sahrens need_unlock = TRUE; 3990789Sahrens } 3991789Sahrens 3992789Sahrens /* 3993789Sahrens * Loop through the requested range [off, off + len] looking 3994789Sahrens * for pages. If we don't find a page, we will need to create 3995789Sahrens * a new page and fill it with data from the file. 3996789Sahrens */ 3997789Sahrens while (len > 0) { 3998789Sahrens if (plsz < PAGESIZE) 3999789Sahrens break; 4000789Sahrens if (pp = page_lookup(vp, off, SE_SHARED)) { 4001789Sahrens *pl++ = pp; 4002789Sahrens off += PAGESIZE; 4003789Sahrens addr += PAGESIZE; 4004789Sahrens len -= PAGESIZE; 4005789Sahrens plsz -= PAGESIZE; 4006789Sahrens } else { 4007789Sahrens err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 40082752Sperrin if (err) 40092752Sperrin goto out; 4010789Sahrens /* 4011789Sahrens * klustering may have changed our region 4012789Sahrens * to be block aligned. 4013789Sahrens */ 4014789Sahrens if (((pp = *pl) != 0) && (off != pp->p_offset)) { 4015789Sahrens int delta = off - pp->p_offset; 4016789Sahrens len += delta; 4017789Sahrens off -= delta; 4018789Sahrens addr -= delta; 4019789Sahrens } 4020789Sahrens while (*pl) { 4021789Sahrens pl++; 4022789Sahrens off += PAGESIZE; 4023789Sahrens addr += PAGESIZE; 4024789Sahrens plsz -= PAGESIZE; 4025789Sahrens if (len > PAGESIZE) 4026789Sahrens len -= PAGESIZE; 4027789Sahrens else 4028789Sahrens len = 0; 4029789Sahrens } 4030789Sahrens } 4031789Sahrens } 4032789Sahrens 4033789Sahrens /* 4034789Sahrens * Fill out the page array with any pages already in the cache. 4035789Sahrens */ 4036789Sahrens while (plsz > 0) { 4037789Sahrens pp = page_lookup_nowait(vp, off, SE_SHARED); 4038789Sahrens if (pp == NULL) 4039789Sahrens break; 4040789Sahrens *pl++ = pp; 4041789Sahrens off += PAGESIZE; 4042789Sahrens plsz -= PAGESIZE; 4043789Sahrens } 4044789Sahrens 4045789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4046789Sahrens out: 40472752Sperrin /* 40482752Sperrin * We can't grab the range lock for the page as reader which would 40492752Sperrin * stop truncation as this leads to deadlock. So we need to recheck 40502752Sperrin * the file size. 40512752Sperrin */ 40522752Sperrin if (orig_off >= zp->z_phys->zp_size) 40532752Sperrin err = EFAULT; 40542752Sperrin if (err) { 40552752Sperrin /* 40562752Sperrin * Release any pages we have previously locked. 40572752Sperrin */ 40582752Sperrin while (pl > pl0) 40592752Sperrin page_unlock(*--pl); 40602752Sperrin } 40612752Sperrin 4062789Sahrens *pl = NULL; 4063789Sahrens 4064789Sahrens if (need_unlock) 4065789Sahrens rw_exit(&zp->z_map_lock); 4066789Sahrens 4067789Sahrens ZFS_EXIT(zfsvfs); 4068789Sahrens return (err); 4069789Sahrens } 4070789Sahrens 40711544Seschrock /* 40721544Seschrock * Request a memory map for a section of a file. This code interacts 40731544Seschrock * with common code and the VM system as follows: 40741544Seschrock * 40751544Seschrock * common code calls mmap(), which ends up in smmap_common() 40761544Seschrock * 40771544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 40781544Seschrock * 40791544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 40801544Seschrock * 40811544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 40821544Seschrock * 40831544Seschrock * zfs_addmap() updates z_mapcnt 40841544Seschrock */ 40855331Samw /*ARGSUSED*/ 4086789Sahrens static int 4087789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 40885331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 40895331Samw caller_context_t *ct) 4090789Sahrens { 4091789Sahrens znode_t *zp = VTOZ(vp); 4092789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4093789Sahrens segvn_crargs_t vn_a; 4094789Sahrens int error; 4095789Sahrens 40965929Smarks ZFS_ENTER(zfsvfs); 40975929Smarks ZFS_VERIFY_ZP(zp); 40985929Smarks 40995331Samw if ((prot & PROT_WRITE) && 41005331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 41015929Smarks ZFS_APPENDONLY))) { 41025929Smarks ZFS_EXIT(zfsvfs); 41035331Samw return (EPERM); 41045929Smarks } 41055929Smarks 41065929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 41075929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 41085929Smarks ZFS_EXIT(zfsvfs); 41095929Smarks return (EACCES); 41105929Smarks } 4111789Sahrens 4112789Sahrens if (vp->v_flag & VNOMAP) { 4113789Sahrens ZFS_EXIT(zfsvfs); 4114789Sahrens return (ENOSYS); 4115789Sahrens } 4116789Sahrens 4117789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4118789Sahrens ZFS_EXIT(zfsvfs); 4119789Sahrens return (ENXIO); 4120789Sahrens } 4121789Sahrens 4122789Sahrens if (vp->v_type != VREG) { 4123789Sahrens ZFS_EXIT(zfsvfs); 4124789Sahrens return (ENODEV); 4125789Sahrens } 4126789Sahrens 4127789Sahrens /* 4128789Sahrens * If file is locked, disallow mapping. 4129789Sahrens */ 41301544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 41311544Seschrock ZFS_EXIT(zfsvfs); 41321544Seschrock return (EAGAIN); 4133789Sahrens } 4134789Sahrens 4135789Sahrens as_rangelock(as); 41366036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 41376036Smec if (error != 0) { 41386036Smec as_rangeunlock(as); 41396036Smec ZFS_EXIT(zfsvfs); 41406036Smec return (error); 4141789Sahrens } 4142789Sahrens 4143789Sahrens vn_a.vp = vp; 4144789Sahrens vn_a.offset = (u_offset_t)off; 4145789Sahrens vn_a.type = flags & MAP_TYPE; 4146789Sahrens vn_a.prot = prot; 4147789Sahrens vn_a.maxprot = maxprot; 4148789Sahrens vn_a.cred = cr; 4149789Sahrens vn_a.amp = NULL; 4150789Sahrens vn_a.flags = flags & ~MAP_TYPE; 41511417Skchow vn_a.szc = 0; 41521417Skchow vn_a.lgrp_mem_policy_flags = 0; 4153789Sahrens 4154789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4155789Sahrens 4156789Sahrens as_rangeunlock(as); 4157789Sahrens ZFS_EXIT(zfsvfs); 4158789Sahrens return (error); 4159789Sahrens } 4160789Sahrens 4161789Sahrens /* ARGSUSED */ 4162789Sahrens static int 4163789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41645331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 41655331Samw caller_context_t *ct) 4166789Sahrens { 41671544Seschrock uint64_t pages = btopr(len); 41681544Seschrock 41691544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4170789Sahrens return (0); 4171789Sahrens } 4172789Sahrens 41731773Seschrock /* 41741773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 41751773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 41761773Seschrock * detecting when the data was actually modified, we have to resort to 41771773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 41781773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 41791773Seschrock * which by far the most common case: 41801773Seschrock * 41811773Seschrock * open() 41821773Seschrock * mmap() 41831773Seschrock * <modify memory> 41841773Seschrock * munmap() 41851773Seschrock * close() 41861773Seschrock * <time lapse> 41871773Seschrock * putpage() via fsflush 41881773Seschrock * 41891773Seschrock * If we wait until fsflush to come along, we can have a modification time that 41901773Seschrock * is some arbitrary point in the future. In order to prevent this in the 41911773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 41921773Seschrock * torn down. 41931773Seschrock */ 4194789Sahrens /* ARGSUSED */ 4195789Sahrens static int 4196789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41975331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 41985331Samw caller_context_t *ct) 4199789Sahrens { 42001544Seschrock uint64_t pages = btopr(len); 42011544Seschrock 42021544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 42031544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 42041773Seschrock 42051773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 42061773Seschrock vn_has_cached_data(vp)) 42075331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 42081773Seschrock 4209789Sahrens return (0); 4210789Sahrens } 4211789Sahrens 4212789Sahrens /* 4213789Sahrens * Free or allocate space in a file. Currently, this function only 4214789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4215789Sahrens * misnamed, as its functionality includes the ability to allocate as 4216789Sahrens * well as free space. 4217789Sahrens * 4218789Sahrens * IN: vp - vnode of file to free data in. 4219789Sahrens * cmd - action to take (only F_FREESP supported). 4220789Sahrens * bfp - section of file to free/alloc. 4221789Sahrens * flag - current file open mode flags. 4222789Sahrens * offset - current file offset. 4223789Sahrens * cr - credentials of caller [UNUSED]. 42245331Samw * ct - caller context. 4225789Sahrens * 4226789Sahrens * RETURN: 0 if success 4227789Sahrens * error code if failure 4228789Sahrens * 4229789Sahrens * Timestamps: 4230789Sahrens * vp - ctime|mtime updated 4231789Sahrens */ 4232789Sahrens /* ARGSUSED */ 4233789Sahrens static int 4234789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4235789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4236789Sahrens { 4237789Sahrens znode_t *zp = VTOZ(vp); 4238789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4239789Sahrens uint64_t off, len; 4240789Sahrens int error; 4241789Sahrens 42425367Sahrens ZFS_ENTER(zfsvfs); 42435367Sahrens ZFS_VERIFY_ZP(zp); 4244789Sahrens 4245789Sahrens if (cmd != F_FREESP) { 4246789Sahrens ZFS_EXIT(zfsvfs); 4247789Sahrens return (EINVAL); 4248789Sahrens } 4249789Sahrens 4250789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4251789Sahrens ZFS_EXIT(zfsvfs); 4252789Sahrens return (error); 4253789Sahrens } 4254789Sahrens 4255789Sahrens if (bfp->l_len < 0) { 4256789Sahrens ZFS_EXIT(zfsvfs); 4257789Sahrens return (EINVAL); 4258789Sahrens } 4259789Sahrens 4260789Sahrens off = bfp->l_start; 42611669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 42621878Smaybee 42636992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4264789Sahrens 4265789Sahrens ZFS_EXIT(zfsvfs); 4266789Sahrens return (error); 4267789Sahrens } 4268789Sahrens 42695331Samw /*ARGSUSED*/ 4270789Sahrens static int 42715331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4272789Sahrens { 4273789Sahrens znode_t *zp = VTOZ(vp); 4274789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 42755326Sek110237 uint32_t gen; 4276789Sahrens uint64_t object = zp->z_id; 4277789Sahrens zfid_short_t *zfid; 4278789Sahrens int size, i; 4279789Sahrens 42805367Sahrens ZFS_ENTER(zfsvfs); 42815367Sahrens ZFS_VERIFY_ZP(zp); 42825326Sek110237 gen = (uint32_t)zp->z_gen; 4283789Sahrens 4284789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4285789Sahrens if (fidp->fid_len < size) { 4286789Sahrens fidp->fid_len = size; 42871512Sek110237 ZFS_EXIT(zfsvfs); 4288789Sahrens return (ENOSPC); 4289789Sahrens } 4290789Sahrens 4291789Sahrens zfid = (zfid_short_t *)fidp; 4292789Sahrens 4293789Sahrens zfid->zf_len = size; 4294789Sahrens 4295789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4296789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4297789Sahrens 4298789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4299789Sahrens if (gen == 0) 4300789Sahrens gen = 1; 4301789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4302789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4303789Sahrens 4304789Sahrens if (size == LONG_FID_LEN) { 4305789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4306789Sahrens zfid_long_t *zlfid; 4307789Sahrens 4308789Sahrens zlfid = (zfid_long_t *)fidp; 4309789Sahrens 4310789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4311789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4312789Sahrens 4313789Sahrens /* XXX - this should be the generation number for the objset */ 4314789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4315789Sahrens zlfid->zf_setgen[i] = 0; 4316789Sahrens } 4317789Sahrens 4318789Sahrens ZFS_EXIT(zfsvfs); 4319789Sahrens return (0); 4320789Sahrens } 4321789Sahrens 4322789Sahrens static int 43235331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 43245331Samw caller_context_t *ct) 4325789Sahrens { 4326789Sahrens znode_t *zp, *xzp; 4327789Sahrens zfsvfs_t *zfsvfs; 4328789Sahrens zfs_dirlock_t *dl; 4329789Sahrens int error; 4330789Sahrens 4331789Sahrens switch (cmd) { 4332789Sahrens case _PC_LINK_MAX: 4333789Sahrens *valp = ULONG_MAX; 4334789Sahrens return (0); 4335789Sahrens 4336789Sahrens case _PC_FILESIZEBITS: 4337789Sahrens *valp = 64; 4338789Sahrens return (0); 4339789Sahrens 4340789Sahrens case _PC_XATTR_EXISTS: 4341789Sahrens zp = VTOZ(vp); 4342789Sahrens zfsvfs = zp->z_zfsvfs; 43435367Sahrens ZFS_ENTER(zfsvfs); 43445367Sahrens ZFS_VERIFY_ZP(zp); 4345789Sahrens *valp = 0; 4346789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 43475331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4348789Sahrens if (error == 0) { 4349789Sahrens zfs_dirent_unlock(dl); 4350789Sahrens if (!zfs_dirempty(xzp)) 4351789Sahrens *valp = 1; 4352789Sahrens VN_RELE(ZTOV(xzp)); 4353789Sahrens } else if (error == ENOENT) { 4354789Sahrens /* 4355789Sahrens * If there aren't extended attributes, it's the 4356789Sahrens * same as having zero of them. 4357789Sahrens */ 4358789Sahrens error = 0; 4359789Sahrens } 4360789Sahrens ZFS_EXIT(zfsvfs); 4361789Sahrens return (error); 4362789Sahrens 43635331Samw case _PC_SATTR_ENABLED: 43645331Samw case _PC_SATTR_EXISTS: 43655331Samw *valp = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) && 43665331Samw (vp->v_type == VREG || vp->v_type == VDIR); 43675331Samw return (0); 43685331Samw 4369789Sahrens case _PC_ACL_ENABLED: 4370789Sahrens *valp = _ACL_ACE_ENABLED; 4371789Sahrens return (0); 4372789Sahrens 4373789Sahrens case _PC_MIN_HOLE_SIZE: 4374789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4375789Sahrens return (0); 4376789Sahrens 4377789Sahrens default: 43785331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4379789Sahrens } 4380789Sahrens } 4381789Sahrens 4382789Sahrens /*ARGSUSED*/ 4383789Sahrens static int 43845331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 43855331Samw caller_context_t *ct) 4386789Sahrens { 4387789Sahrens znode_t *zp = VTOZ(vp); 4388789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4389789Sahrens int error; 43905331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4391789Sahrens 43925367Sahrens ZFS_ENTER(zfsvfs); 43935367Sahrens ZFS_VERIFY_ZP(zp); 43945331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4395789Sahrens ZFS_EXIT(zfsvfs); 4396789Sahrens 4397789Sahrens return (error); 4398789Sahrens } 4399789Sahrens 4400789Sahrens /*ARGSUSED*/ 4401789Sahrens static int 44025331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 44035331Samw caller_context_t *ct) 4404789Sahrens { 4405789Sahrens znode_t *zp = VTOZ(vp); 4406789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4407789Sahrens int error; 44085331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4409789Sahrens 44105367Sahrens ZFS_ENTER(zfsvfs); 44115367Sahrens ZFS_VERIFY_ZP(zp); 44125331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4413789Sahrens ZFS_EXIT(zfsvfs); 4414789Sahrens return (error); 4415789Sahrens } 4416789Sahrens 4417789Sahrens /* 4418789Sahrens * Predeclare these here so that the compiler assumes that 4419789Sahrens * this is an "old style" function declaration that does 4420789Sahrens * not include arguments => we won't get type mismatch errors 4421789Sahrens * in the initializations that follow. 4422789Sahrens */ 4423789Sahrens static int zfs_inval(); 4424789Sahrens static int zfs_isdir(); 4425789Sahrens 4426789Sahrens static int 4427789Sahrens zfs_inval() 4428789Sahrens { 4429789Sahrens return (EINVAL); 4430789Sahrens } 4431789Sahrens 4432789Sahrens static int 4433789Sahrens zfs_isdir() 4434789Sahrens { 4435789Sahrens return (EISDIR); 4436789Sahrens } 4437789Sahrens /* 4438789Sahrens * Directory vnode operations template 4439789Sahrens */ 4440789Sahrens vnodeops_t *zfs_dvnodeops; 4441789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 44423898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44433898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44443898Srsb VOPNAME_READ, { .error = zfs_isdir }, 44453898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 44463898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44473898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44483898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44493898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44503898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44513898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 44523898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 44533898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 44543898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44553898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 44563898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 44573898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 44583898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 44593898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44603898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44613898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44623898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44633898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44643898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44653898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44664863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44673898Srsb NULL, NULL 4468789Sahrens }; 4469789Sahrens 4470789Sahrens /* 4471789Sahrens * Regular file vnode operations template 4472789Sahrens */ 4473789Sahrens vnodeops_t *zfs_fvnodeops; 4474789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 44753898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44763898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44773898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 44783898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 44793898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44803898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44813898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44823898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44833898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44843898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44853898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44863898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44873898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44883898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44893898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 44903898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 44913898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 44923898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 44933898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 44943898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 44953898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 44963898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44973898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44983898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44993898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45003898Srsb NULL, NULL 4501789Sahrens }; 4502789Sahrens 4503789Sahrens /* 4504789Sahrens * Symbolic link vnode operations template 4505789Sahrens */ 4506789Sahrens vnodeops_t *zfs_symvnodeops; 4507789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 45083898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45093898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45103898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45113898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45123898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 45133898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45143898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45153898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45163898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45173898Srsb NULL, NULL 4518789Sahrens }; 4519789Sahrens 4520789Sahrens /* 4521789Sahrens * Extended attribute directory vnode operations template 4522789Sahrens * This template is identical to the directory vnodes 4523789Sahrens * operation template except for restricted operations: 4524789Sahrens * VOP_MKDIR() 4525789Sahrens * VOP_SYMLINK() 4526789Sahrens * Note that there are other restrictions embedded in: 4527789Sahrens * zfs_create() - restrict type to VREG 4528789Sahrens * zfs_link() - no links into/out of attribute space 4529789Sahrens * zfs_rename() - no moves into/out of attribute space 4530789Sahrens */ 4531789Sahrens vnodeops_t *zfs_xdvnodeops; 4532789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 45333898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45343898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45353898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45363898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45373898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45383898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45393898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45403898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 45413898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 45423898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 45433898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45443898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 45453898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 45463898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 45473898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 45483898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45493898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45503898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45513898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45523898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45533898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45543898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45553898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45563898Srsb NULL, NULL 4557789Sahrens }; 4558789Sahrens 4559789Sahrens /* 4560789Sahrens * Error vnode operations template 4561789Sahrens */ 4562789Sahrens vnodeops_t *zfs_evnodeops; 4563789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 45643898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45653898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45663898Srsb NULL, NULL 4567789Sahrens }; 4568