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 #include <sys/types.h> 29789Sahrens #include <sys/param.h> 30789Sahrens #include <sys/time.h> 31789Sahrens #include <sys/systm.h> 32789Sahrens #include <sys/sysmacros.h> 33789Sahrens #include <sys/resource.h> 34789Sahrens #include <sys/vfs.h> 353898Srsb #include <sys/vfs_opreg.h> 36789Sahrens #include <sys/vnode.h> 37789Sahrens #include <sys/file.h> 38789Sahrens #include <sys/stat.h> 39789Sahrens #include <sys/kmem.h> 40789Sahrens #include <sys/taskq.h> 41789Sahrens #include <sys/uio.h> 42789Sahrens #include <sys/vmsystm.h> 43789Sahrens #include <sys/atomic.h> 442688Smaybee #include <sys/vm.h> 45789Sahrens #include <vm/seg_vn.h> 46789Sahrens #include <vm/pvn.h> 47789Sahrens #include <vm/as.h> 48*7315SJonathan.Adams@Sun.COM #include <vm/kpm.h> 49*7315SJonathan.Adams@Sun.COM #include <vm/seg_kpm.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 /* 306*7315SJonathan.Adams@Sun.COM * Utility functions to map and unmap a single physical page. These 307*7315SJonathan.Adams@Sun.COM * are used to manage the mappable copies of ZFS file data, and therefore 308*7315SJonathan.Adams@Sun.COM * do not update ref/mod bits. 309*7315SJonathan.Adams@Sun.COM */ 310*7315SJonathan.Adams@Sun.COM caddr_t 311*7315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw) 312*7315SJonathan.Adams@Sun.COM { 313*7315SJonathan.Adams@Sun.COM if (kpm_enable) 314*7315SJonathan.Adams@Sun.COM return (hat_kpm_mapin(pp, 0)); 315*7315SJonathan.Adams@Sun.COM ASSERT(rw == S_READ || rw == S_WRITE); 316*7315SJonathan.Adams@Sun.COM return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0), 317*7315SJonathan.Adams@Sun.COM (caddr_t)-1)); 318*7315SJonathan.Adams@Sun.COM } 319*7315SJonathan.Adams@Sun.COM 320*7315SJonathan.Adams@Sun.COM void 321*7315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr) 322*7315SJonathan.Adams@Sun.COM { 323*7315SJonathan.Adams@Sun.COM if (kpm_enable) { 324*7315SJonathan.Adams@Sun.COM hat_kpm_mapout(pp, 0, addr); 325*7315SJonathan.Adams@Sun.COM } else { 326*7315SJonathan.Adams@Sun.COM ppmapout(addr); 327*7315SJonathan.Adams@Sun.COM } 328*7315SJonathan.Adams@Sun.COM } 329*7315SJonathan.Adams@Sun.COM 330*7315SJonathan.Adams@Sun.COM /* 331789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 332789Sahrens * between the DMU cache and the memory mapped pages. What this means: 333789Sahrens * 334789Sahrens * On Write: If we find a memory mapped page, we write to *both* 335789Sahrens * the page and the dmu buffer. 336789Sahrens * 337789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 338789Sahrens * the file is memory mapped. 339789Sahrens */ 340789Sahrens static int 3413638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx) 342789Sahrens { 343789Sahrens znode_t *zp = VTOZ(vp); 344789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 345789Sahrens int64_t start, off; 346789Sahrens int len = nbytes; 347789Sahrens int error = 0; 348789Sahrens 349789Sahrens start = uio->uio_loffset; 350789Sahrens off = start & PAGEOFFSET; 351789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 352789Sahrens page_t *pp; 353789Sahrens uint64_t bytes = MIN(PAGESIZE - off, len); 3543638Sbillm uint64_t woff = uio->uio_loffset; 355789Sahrens 356789Sahrens /* 357789Sahrens * We don't want a new page to "appear" in the middle of 358789Sahrens * the file update (because it may not get the write 359789Sahrens * update data), so we grab a lock to block 360789Sahrens * zfs_getpage(). 361789Sahrens */ 362789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 363789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 364789Sahrens caddr_t va; 365789Sahrens 366789Sahrens rw_exit(&zp->z_map_lock); 367*7315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_WRITE); 368789Sahrens error = uiomove(va+off, bytes, UIO_WRITE, uio); 369789Sahrens if (error == 0) { 370789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, 371789Sahrens woff, bytes, va+off, tx); 372789Sahrens } 373*7315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 374789Sahrens page_unlock(pp); 375789Sahrens } else { 376789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 3773638Sbillm uio, bytes, tx); 378789Sahrens rw_exit(&zp->z_map_lock); 379789Sahrens } 380789Sahrens len -= bytes; 381789Sahrens off = 0; 382789Sahrens if (error) 383789Sahrens break; 384789Sahrens } 385789Sahrens return (error); 386789Sahrens } 387789Sahrens 388789Sahrens /* 389789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 390789Sahrens * between the DMU cache and the memory mapped pages. What this means: 391789Sahrens * 392789Sahrens * On Read: We "read" preferentially from memory mapped pages, 393789Sahrens * else we default from the dmu buffer. 394789Sahrens * 395789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 396789Sahrens * the file is memory mapped. 397789Sahrens */ 398789Sahrens static int 3993638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio) 400789Sahrens { 4013638Sbillm znode_t *zp = VTOZ(vp); 4023638Sbillm objset_t *os = zp->z_zfsvfs->z_os; 4033638Sbillm int64_t start, off; 404789Sahrens int len = nbytes; 405789Sahrens int error = 0; 406789Sahrens 407789Sahrens start = uio->uio_loffset; 408789Sahrens off = start & PAGEOFFSET; 409789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 410789Sahrens page_t *pp; 4113638Sbillm uint64_t bytes = MIN(PAGESIZE - off, len); 4123638Sbillm 413789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 414789Sahrens caddr_t va; 415789Sahrens 416*7315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_READ); 417789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 418*7315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 419789Sahrens page_unlock(pp); 420789Sahrens } else { 4213638Sbillm error = dmu_read_uio(os, zp->z_id, uio, bytes); 422789Sahrens } 423789Sahrens len -= bytes; 424789Sahrens off = 0; 425789Sahrens if (error) 426789Sahrens break; 427789Sahrens } 428789Sahrens return (error); 429789Sahrens } 430789Sahrens 4313638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 432789Sahrens 433789Sahrens /* 434789Sahrens * Read bytes from specified file into supplied buffer. 435789Sahrens * 436789Sahrens * IN: vp - vnode of file to be read from. 437789Sahrens * uio - structure supplying read location, range info, 438789Sahrens * and return buffer. 439789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 440789Sahrens * cr - credentials of caller. 4415331Samw * ct - caller context 442789Sahrens * 443789Sahrens * OUT: uio - updated offset and range, buffer filled. 444789Sahrens * 445789Sahrens * RETURN: 0 if success 446789Sahrens * error code if failure 447789Sahrens * 448789Sahrens * Side Effects: 449789Sahrens * vp - atime updated if byte count > 0 450789Sahrens */ 451789Sahrens /* ARGSUSED */ 452789Sahrens static int 453789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 454789Sahrens { 455789Sahrens znode_t *zp = VTOZ(vp); 456789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4575326Sek110237 objset_t *os; 4583638Sbillm ssize_t n, nbytes; 4593638Sbillm int error; 4601669Sperrin rl_t *rl; 461789Sahrens 4625367Sahrens ZFS_ENTER(zfsvfs); 4635367Sahrens ZFS_VERIFY_ZP(zp); 4645326Sek110237 os = zfsvfs->z_os; 465789Sahrens 4665929Smarks if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) { 4675929Smarks ZFS_EXIT(zfsvfs); 4685929Smarks return (EACCES); 4695929Smarks } 4705929Smarks 471789Sahrens /* 472789Sahrens * Validate file offset 473789Sahrens */ 474789Sahrens if (uio->uio_loffset < (offset_t)0) { 475789Sahrens ZFS_EXIT(zfsvfs); 476789Sahrens return (EINVAL); 477789Sahrens } 478789Sahrens 479789Sahrens /* 480789Sahrens * Fasttrack empty reads 481789Sahrens */ 482789Sahrens if (uio->uio_resid == 0) { 483789Sahrens ZFS_EXIT(zfsvfs); 484789Sahrens return (0); 485789Sahrens } 486789Sahrens 487789Sahrens /* 4881669Sperrin * Check for mandatory locks 489789Sahrens */ 490789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 491789Sahrens if (error = chklock(vp, FREAD, 492789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 493789Sahrens ZFS_EXIT(zfsvfs); 494789Sahrens return (error); 495789Sahrens } 496789Sahrens } 497789Sahrens 498789Sahrens /* 499789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 500789Sahrens */ 5012638Sperrin if (ioflag & FRSYNC) 5022638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 503789Sahrens 504789Sahrens /* 5051669Sperrin * Lock the range against changes. 506789Sahrens */ 5071669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 5081669Sperrin 509789Sahrens /* 510789Sahrens * If we are reading past end-of-file we can skip 511789Sahrens * to the end; but we might still need to set atime. 512789Sahrens */ 513789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 514789Sahrens error = 0; 515789Sahrens goto out; 516789Sahrens } 517789Sahrens 5183638Sbillm ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 5193638Sbillm n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 5203638Sbillm 5213638Sbillm while (n > 0) { 5223638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 5233638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 5243638Sbillm 5253638Sbillm if (vn_has_cached_data(vp)) 5263638Sbillm error = mappedread(vp, nbytes, uio); 5273638Sbillm else 5283638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 5297294Sperrin if (error) { 5307294Sperrin /* convert checksum errors into IO errors */ 5317294Sperrin if (error == ECKSUM) 5327294Sperrin error = EIO; 5333638Sbillm break; 5347294Sperrin } 5353638Sbillm 5363638Sbillm n -= nbytes; 537789Sahrens } 5383638Sbillm 539789Sahrens out: 5402237Smaybee zfs_range_unlock(rl); 541789Sahrens 542789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 543789Sahrens ZFS_EXIT(zfsvfs); 544789Sahrens return (error); 545789Sahrens } 546789Sahrens 547789Sahrens /* 548789Sahrens * Fault in the pages of the first n bytes specified by the uio structure. 549789Sahrens * 1 byte in each page is touched and the uio struct is unmodified. 550789Sahrens * Any error will exit this routine as this is only a best 551789Sahrens * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 552789Sahrens */ 553789Sahrens static void 554789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio) 555789Sahrens { 556789Sahrens struct iovec *iov; 557789Sahrens ulong_t cnt, incr; 558789Sahrens caddr_t p; 559789Sahrens uint8_t tmp; 560789Sahrens 561789Sahrens iov = uio->uio_iov; 562789Sahrens 563789Sahrens while (n) { 564789Sahrens cnt = MIN(iov->iov_len, n); 565789Sahrens if (cnt == 0) { 566789Sahrens /* empty iov entry */ 567789Sahrens iov++; 568789Sahrens continue; 569789Sahrens } 570789Sahrens n -= cnt; 571789Sahrens /* 572789Sahrens * touch each page in this segment. 573789Sahrens */ 574789Sahrens p = iov->iov_base; 575789Sahrens while (cnt) { 576789Sahrens switch (uio->uio_segflg) { 577789Sahrens case UIO_USERSPACE: 578789Sahrens case UIO_USERISPACE: 579789Sahrens if (fuword8(p, &tmp)) 580789Sahrens return; 581789Sahrens break; 582789Sahrens case UIO_SYSSPACE: 583789Sahrens if (kcopy(p, &tmp, 1)) 584789Sahrens return; 585789Sahrens break; 586789Sahrens } 587789Sahrens incr = MIN(cnt, PAGESIZE); 588789Sahrens p += incr; 589789Sahrens cnt -= incr; 590789Sahrens } 591789Sahrens /* 592789Sahrens * touch the last byte in case it straddles a page. 593789Sahrens */ 594789Sahrens p--; 595789Sahrens switch (uio->uio_segflg) { 596789Sahrens case UIO_USERSPACE: 597789Sahrens case UIO_USERISPACE: 598789Sahrens if (fuword8(p, &tmp)) 599789Sahrens return; 600789Sahrens break; 601789Sahrens case UIO_SYSSPACE: 602789Sahrens if (kcopy(p, &tmp, 1)) 603789Sahrens return; 604789Sahrens break; 605789Sahrens } 606789Sahrens iov++; 607789Sahrens } 608789Sahrens } 609789Sahrens 610789Sahrens /* 611789Sahrens * Write the bytes to a file. 612789Sahrens * 613789Sahrens * IN: vp - vnode of file to be written to. 614789Sahrens * uio - structure supplying write location, range info, 615789Sahrens * and data buffer. 616789Sahrens * ioflag - FAPPEND flag set if in append mode. 617789Sahrens * cr - credentials of caller. 6185331Samw * ct - caller context (NFS/CIFS fem monitor only) 619789Sahrens * 620789Sahrens * OUT: uio - updated offset and range. 621789Sahrens * 622789Sahrens * RETURN: 0 if success 623789Sahrens * error code if failure 624789Sahrens * 625789Sahrens * Timestamps: 626789Sahrens * vp - ctime|mtime updated if byte count > 0 627789Sahrens */ 628789Sahrens /* ARGSUSED */ 629789Sahrens static int 630789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 631789Sahrens { 632789Sahrens znode_t *zp = VTOZ(vp); 633789Sahrens rlim64_t limit = uio->uio_llimit; 634789Sahrens ssize_t start_resid = uio->uio_resid; 635789Sahrens ssize_t tx_bytes; 636789Sahrens uint64_t end_size; 637789Sahrens dmu_tx_t *tx; 638789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 6395326Sek110237 zilog_t *zilog; 640789Sahrens offset_t woff; 641789Sahrens ssize_t n, nbytes; 6421669Sperrin rl_t *rl; 643789Sahrens int max_blksz = zfsvfs->z_max_blksz; 6446743Smarks uint64_t pflags; 6451669Sperrin int error; 646789Sahrens 647789Sahrens /* 648789Sahrens * Fasttrack empty write 649789Sahrens */ 6501669Sperrin n = start_resid; 651789Sahrens if (n == 0) 652789Sahrens return (0); 653789Sahrens 6541669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6551669Sperrin limit = MAXOFFSET_T; 6561669Sperrin 6575367Sahrens ZFS_ENTER(zfsvfs); 6585367Sahrens ZFS_VERIFY_ZP(zp); 6596743Smarks 6606743Smarks /* 6616743Smarks * If immutable or not appending then return EPERM 6626743Smarks */ 6636743Smarks pflags = zp->z_phys->zp_flags; 6646743Smarks if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 6656743Smarks ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 6666743Smarks (uio->uio_loffset < zp->z_phys->zp_size))) { 6676743Smarks ZFS_EXIT(zfsvfs); 6686743Smarks return (EPERM); 6696743Smarks } 6706743Smarks 6715326Sek110237 zilog = zfsvfs->z_log; 672789Sahrens 673789Sahrens /* 6742237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6751669Sperrin * don't hold up txg. 676789Sahrens */ 6772237Smaybee zfs_prefault_write(n, uio); 678789Sahrens 679789Sahrens /* 680789Sahrens * If in append mode, set the io offset pointer to eof. 681789Sahrens */ 6821669Sperrin if (ioflag & FAPPEND) { 6831669Sperrin /* 6841669Sperrin * Range lock for a file append: 6851669Sperrin * The value for the start of range will be determined by 6861669Sperrin * zfs_range_lock() (to guarantee append semantics). 6871669Sperrin * If this write will cause the block size to increase, 6881669Sperrin * zfs_range_lock() will lock the entire file, so we must 6891669Sperrin * later reduce the range after we grow the block size. 6901669Sperrin */ 6911669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6921669Sperrin if (rl->r_len == UINT64_MAX) { 6931669Sperrin /* overlocked, zp_size can't change */ 6941669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6951669Sperrin } else { 6961669Sperrin woff = uio->uio_loffset = rl->r_off; 6971669Sperrin } 698789Sahrens } else { 699789Sahrens woff = uio->uio_loffset; 700789Sahrens /* 701789Sahrens * Validate file offset 702789Sahrens */ 703789Sahrens if (woff < 0) { 704789Sahrens ZFS_EXIT(zfsvfs); 705789Sahrens return (EINVAL); 706789Sahrens } 707789Sahrens 708789Sahrens /* 7091669Sperrin * If we need to grow the block size then zfs_range_lock() 7101669Sperrin * will lock a wider range than we request here. 7111669Sperrin * Later after growing the block size we reduce the range. 712789Sahrens */ 7131669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 714789Sahrens } 715789Sahrens 716789Sahrens if (woff >= limit) { 7173638Sbillm zfs_range_unlock(rl); 7183638Sbillm ZFS_EXIT(zfsvfs); 7193638Sbillm return (EFBIG); 720789Sahrens } 721789Sahrens 722789Sahrens if ((woff + n) > limit || woff > (limit - n)) 723789Sahrens n = limit - woff; 724789Sahrens 725789Sahrens /* 7261669Sperrin * Check for mandatory locks 727789Sahrens */ 728789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 7293638Sbillm (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 7303638Sbillm zfs_range_unlock(rl); 7313638Sbillm ZFS_EXIT(zfsvfs); 7323638Sbillm return (error); 7333638Sbillm } 7341669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 735789Sahrens 7361669Sperrin /* 7373638Sbillm * Write the file in reasonable size chunks. Each chunk is written 7383638Sbillm * in a separate transaction; this keeps the intent log records small 7393638Sbillm * and allows us to do more fine-grained space accounting. 740789Sahrens */ 741789Sahrens while (n > 0) { 742789Sahrens /* 7433638Sbillm * Start a transaction. 744789Sahrens */ 745789Sahrens woff = uio->uio_loffset; 746789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 747789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 748789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 749789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 750789Sahrens if (error) { 751789Sahrens if (error == ERESTART && 752789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 7532113Sahrens dmu_tx_wait(tx); 7542113Sahrens dmu_tx_abort(tx); 7553638Sbillm continue; 756789Sahrens } 7572113Sahrens dmu_tx_abort(tx); 7583638Sbillm break; 7593638Sbillm } 7603638Sbillm 7613638Sbillm /* 7623638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 7633638Sbillm * and then reduce the lock range. This will only happen 7643638Sbillm * on the first iteration since zfs_range_reduce() will 7653638Sbillm * shrink down r_len to the appropriate size. 7663638Sbillm */ 7673638Sbillm if (rl->r_len == UINT64_MAX) { 7683638Sbillm uint64_t new_blksz; 7693638Sbillm 7703638Sbillm if (zp->z_blksz > max_blksz) { 7713638Sbillm ASSERT(!ISP2(zp->z_blksz)); 7723638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 7733638Sbillm } else { 7743638Sbillm new_blksz = MIN(end_size, max_blksz); 7753638Sbillm } 7763638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 7773638Sbillm zfs_range_reduce(rl, woff, n); 7783638Sbillm } 7793638Sbillm 7803638Sbillm /* 7813638Sbillm * XXX - should we really limit each write to z_max_blksz? 7823638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 7833638Sbillm */ 7843638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 7853638Sbillm rw_enter(&zp->z_map_lock, RW_READER); 7863638Sbillm 7873638Sbillm tx_bytes = uio->uio_resid; 7883638Sbillm if (vn_has_cached_data(vp)) { 7893638Sbillm rw_exit(&zp->z_map_lock); 7903638Sbillm error = mappedwrite(vp, nbytes, uio, tx); 7913638Sbillm } else { 7923638Sbillm error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 7933638Sbillm uio, nbytes, tx); 7943638Sbillm rw_exit(&zp->z_map_lock); 795789Sahrens } 7963638Sbillm tx_bytes -= uio->uio_resid; 7973638Sbillm 7983638Sbillm /* 7993638Sbillm * If we made no progress, we're done. If we made even 8003638Sbillm * partial progress, update the znode and ZIL accordingly. 8013638Sbillm */ 8023638Sbillm if (tx_bytes == 0) { 8033897Smaybee dmu_tx_commit(tx); 8043638Sbillm ASSERT(error != 0); 8053638Sbillm break; 8063638Sbillm } 8073638Sbillm 808789Sahrens /* 8093638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 8103638Sbillm * privileged and at least one of the excute bits is set. 8113638Sbillm * 8123638Sbillm * It would be nice to to this after all writes have 8133638Sbillm * been done, but that would still expose the ISUID/ISGID 8143638Sbillm * to another app after the partial write is committed. 8155331Samw * 8165331Samw * Note: we don't call zfs_fuid_map_id() here because 8175331Samw * user 0 is not an ephemeral uid. 818789Sahrens */ 8193638Sbillm mutex_enter(&zp->z_acl_lock); 8203638Sbillm if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 8213638Sbillm (S_IXUSR >> 6))) != 0 && 8223638Sbillm (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 8233638Sbillm secpolicy_vnode_setid_retain(cr, 8243638Sbillm (zp->z_phys->zp_mode & S_ISUID) != 0 && 8253638Sbillm zp->z_phys->zp_uid == 0) != 0) { 8264339Sperrin zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 8273638Sbillm } 8283638Sbillm mutex_exit(&zp->z_acl_lock); 8293638Sbillm 8303638Sbillm /* 8313638Sbillm * Update time stamp. NOTE: This marks the bonus buffer as 8323638Sbillm * dirty, so we don't have to do it again for zp_size. 8333638Sbillm */ 8343638Sbillm zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 8353638Sbillm 8363638Sbillm /* 8373638Sbillm * Update the file size (zp_size) if it has changed; 8383638Sbillm * account for possible concurrent updates. 8393638Sbillm */ 8403638Sbillm while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 841789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 842789Sahrens uio->uio_loffset); 8433638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 8443638Sbillm dmu_tx_commit(tx); 8453638Sbillm 8463638Sbillm if (error != 0) 8473638Sbillm break; 8483638Sbillm ASSERT(tx_bytes == nbytes); 8493638Sbillm n -= nbytes; 850789Sahrens } 851789Sahrens 8522237Smaybee zfs_range_unlock(rl); 853789Sahrens 854789Sahrens /* 855789Sahrens * If we're in replay mode, or we made no progress, return error. 856789Sahrens * Otherwise, it's at least a partial write, so it's successful. 857789Sahrens */ 858789Sahrens if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 859789Sahrens ZFS_EXIT(zfsvfs); 860789Sahrens return (error); 861789Sahrens } 862789Sahrens 8632638Sperrin if (ioflag & (FSYNC | FDSYNC)) 8642638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 865789Sahrens 866789Sahrens ZFS_EXIT(zfsvfs); 867789Sahrens return (0); 868789Sahrens } 869789Sahrens 8702237Smaybee void 8713063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd) 8722237Smaybee { 8733063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 8743063Sperrin rl_t *rl = zgd->zgd_rl; 8752237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 8762237Smaybee 8773063Sperrin dmu_buf_rele(db, vzgd); 8782237Smaybee zfs_range_unlock(rl); 8792237Smaybee VN_RELE(vp); 8805688Sbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 8813063Sperrin kmem_free(zgd, sizeof (zgd_t)); 8822237Smaybee } 8832237Smaybee 884789Sahrens /* 885789Sahrens * Get data to generate a TX_WRITE intent log record. 886789Sahrens */ 887789Sahrens int 8882237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 889789Sahrens { 890789Sahrens zfsvfs_t *zfsvfs = arg; 891789Sahrens objset_t *os = zfsvfs->z_os; 892789Sahrens znode_t *zp; 893789Sahrens uint64_t off = lr->lr_offset; 8942237Smaybee dmu_buf_t *db; 8951669Sperrin rl_t *rl; 8963063Sperrin zgd_t *zgd; 8973638Sbillm int dlen = lr->lr_length; /* length of user data */ 898789Sahrens int error = 0; 899789Sahrens 9003063Sperrin ASSERT(zio); 901789Sahrens ASSERT(dlen != 0); 902789Sahrens 903789Sahrens /* 9041669Sperrin * Nothing to do if the file has been removed 905789Sahrens */ 906789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 907789Sahrens return (ENOENT); 9083461Sahrens if (zp->z_unlinked) { 909789Sahrens VN_RELE(ZTOV(zp)); 910789Sahrens return (ENOENT); 911789Sahrens } 912789Sahrens 913789Sahrens /* 914789Sahrens * Write records come in two flavors: immediate and indirect. 915789Sahrens * For small writes it's cheaper to store the data with the 916789Sahrens * log record (immediate); for large writes it's cheaper to 917789Sahrens * sync the data and get a pointer to it (indirect) so that 918789Sahrens * we don't have to write the data twice. 919789Sahrens */ 9201669Sperrin if (buf != NULL) { /* immediate write */ 9211669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 9221669Sperrin /* test for truncation needs to be done while range locked */ 9231669Sperrin if (off >= zp->z_phys->zp_size) { 9241669Sperrin error = ENOENT; 9251669Sperrin goto out; 9261669Sperrin } 9272449Smaybee VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 9281669Sperrin } else { /* indirect write */ 9291669Sperrin uint64_t boff; /* block starting offset */ 9301669Sperrin 931789Sahrens /* 9321669Sperrin * Have to lock the whole block to ensure when it's 9331669Sperrin * written out and it's checksum is being calculated 9341669Sperrin * that no one can change the data. We need to re-check 9351669Sperrin * blocksize after we get the lock in case it's changed! 936789Sahrens */ 9371669Sperrin for (;;) { 9381941Sperrin if (ISP2(zp->z_blksz)) { 9391941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 9401941Sperrin uint64_t); 9411941Sperrin } else { 9421941Sperrin boff = 0; 9431941Sperrin } 9441669Sperrin dlen = zp->z_blksz; 9451669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 9461669Sperrin if (zp->z_blksz == dlen) 9471669Sperrin break; 9482237Smaybee zfs_range_unlock(rl); 9491669Sperrin } 9501669Sperrin /* test for truncation needs to be done while range locked */ 9511669Sperrin if (off >= zp->z_phys->zp_size) { 9521669Sperrin error = ENOENT; 9531669Sperrin goto out; 9541669Sperrin } 9553063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 9563063Sperrin zgd->zgd_rl = rl; 9573063Sperrin zgd->zgd_zilog = zfsvfs->z_log; 9583063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 9593063Sperrin VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 9602237Smaybee ASSERT(boff == db->db_offset); 9612237Smaybee lr->lr_blkoff = off - boff; 9622237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 9633063Sperrin lr->lr_common.lrc_txg, zfs_get_done, zgd); 9644709Smaybee ASSERT((error && error != EINPROGRESS) || 9654709Smaybee lr->lr_length <= zp->z_blksz); 9665688Sbonwick if (error == 0) 9675688Sbonwick zil_add_block(zfsvfs->z_log, &lr->lr_blkptr); 9682237Smaybee /* 9692237Smaybee * If we get EINPROGRESS, then we need to wait for a 9702237Smaybee * write IO initiated by dmu_sync() to complete before 9712638Sperrin * we can release this dbuf. We will finish everything 9722237Smaybee * up in the zfs_get_done() callback. 9732237Smaybee */ 9742237Smaybee if (error == EINPROGRESS) 9752237Smaybee return (0); 9763063Sperrin dmu_buf_rele(db, zgd); 9773063Sperrin kmem_free(zgd, sizeof (zgd_t)); 978789Sahrens } 9791669Sperrin out: 9802237Smaybee zfs_range_unlock(rl); 981789Sahrens VN_RELE(ZTOV(zp)); 982789Sahrens return (error); 983789Sahrens } 984789Sahrens 985789Sahrens /*ARGSUSED*/ 986789Sahrens static int 9875331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 9885331Samw caller_context_t *ct) 989789Sahrens { 990789Sahrens znode_t *zp = VTOZ(vp); 991789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 992789Sahrens int error; 993789Sahrens 9945367Sahrens ZFS_ENTER(zfsvfs); 9955367Sahrens ZFS_VERIFY_ZP(zp); 9965331Samw 9975331Samw if (flag & V_ACE_MASK) 9985331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 9995331Samw else 10005331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 10015331Samw 1002789Sahrens ZFS_EXIT(zfsvfs); 1003789Sahrens return (error); 1004789Sahrens } 1005789Sahrens 1006789Sahrens /* 1007789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1008789Sahrens * If it exists, return a held vnode reference for it. 1009789Sahrens * 1010789Sahrens * IN: dvp - vnode of directory to search. 1011789Sahrens * nm - name of entry to lookup. 1012789Sahrens * pnp - full pathname to lookup [UNUSED]. 1013789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1014789Sahrens * rdir - root directory vnode [UNUSED]. 1015789Sahrens * cr - credentials of caller. 10165331Samw * ct - caller context 10175331Samw * direntflags - directory lookup flags 10185331Samw * realpnp - returned pathname. 1019789Sahrens * 1020789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1021789Sahrens * 1022789Sahrens * RETURN: 0 if success 1023789Sahrens * error code if failure 1024789Sahrens * 1025789Sahrens * Timestamps: 1026789Sahrens * NA 1027789Sahrens */ 1028789Sahrens /* ARGSUSED */ 1029789Sahrens static int 1030789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 10315331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 10325331Samw int *direntflags, pathname_t *realpnp) 1033789Sahrens { 1034789Sahrens znode_t *zdp = VTOZ(dvp); 1035789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 1036789Sahrens int error; 1037789Sahrens 10385367Sahrens ZFS_ENTER(zfsvfs); 10395367Sahrens ZFS_VERIFY_ZP(zdp); 1040789Sahrens 1041789Sahrens *vpp = NULL; 1042789Sahrens 1043789Sahrens if (flags & LOOKUP_XATTR) { 1044789Sahrens /* 10453234Sck153898 * If the xattr property is off, refuse the lookup request. 10463234Sck153898 */ 10473234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 10483234Sck153898 ZFS_EXIT(zfsvfs); 10493234Sck153898 return (EINVAL); 10503234Sck153898 } 10513234Sck153898 10523234Sck153898 /* 1053789Sahrens * We don't allow recursive attributes.. 1054789Sahrens * Maybe someday we will. 1055789Sahrens */ 1056789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1057789Sahrens ZFS_EXIT(zfsvfs); 1058789Sahrens return (EINVAL); 1059789Sahrens } 1060789Sahrens 10613280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1062789Sahrens ZFS_EXIT(zfsvfs); 1063789Sahrens return (error); 1064789Sahrens } 1065789Sahrens 1066789Sahrens /* 1067789Sahrens * Do we have permission to get into attribute directory? 1068789Sahrens */ 1069789Sahrens 10705331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 10715331Samw B_FALSE, cr)) { 1072789Sahrens VN_RELE(*vpp); 10735331Samw *vpp = NULL; 1074789Sahrens } 1075789Sahrens 1076789Sahrens ZFS_EXIT(zfsvfs); 1077789Sahrens return (error); 1078789Sahrens } 1079789Sahrens 10801512Sek110237 if (dvp->v_type != VDIR) { 10811512Sek110237 ZFS_EXIT(zfsvfs); 10821460Smarks return (ENOTDIR); 10831512Sek110237 } 10841460Smarks 1085789Sahrens /* 1086789Sahrens * Check accessibility of directory. 1087789Sahrens */ 1088789Sahrens 10895331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1090789Sahrens ZFS_EXIT(zfsvfs); 1091789Sahrens return (error); 1092789Sahrens } 1093789Sahrens 10945498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 10955331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 10965331Samw ZFS_EXIT(zfsvfs); 10975331Samw return (EILSEQ); 10985331Samw } 10995331Samw 11005331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 11015331Samw if (error == 0) { 1102789Sahrens /* 1103789Sahrens * Convert device special files 1104789Sahrens */ 1105789Sahrens if (IS_DEVVP(*vpp)) { 1106789Sahrens vnode_t *svp; 1107789Sahrens 1108789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1109789Sahrens VN_RELE(*vpp); 1110789Sahrens if (svp == NULL) 1111789Sahrens error = ENOSYS; 1112789Sahrens else 1113789Sahrens *vpp = svp; 1114789Sahrens } 1115789Sahrens } 1116789Sahrens 1117789Sahrens ZFS_EXIT(zfsvfs); 1118789Sahrens return (error); 1119789Sahrens } 1120789Sahrens 1121789Sahrens /* 1122789Sahrens * Attempt to create a new entry in a directory. If the entry 1123789Sahrens * already exists, truncate the file if permissible, else return 1124789Sahrens * an error. Return the vp of the created or trunc'd file. 1125789Sahrens * 1126789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1127789Sahrens * name - name of new file entry. 1128789Sahrens * vap - attributes of new file. 1129789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1130789Sahrens * mode - mode to open file with. 1131789Sahrens * cr - credentials of caller. 1132789Sahrens * flag - large file flag [UNUSED]. 11335331Samw * ct - caller context 11345331Samw * vsecp - ACL to be set 1135789Sahrens * 1136789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1137789Sahrens * 1138789Sahrens * RETURN: 0 if success 1139789Sahrens * error code if failure 1140789Sahrens * 1141789Sahrens * Timestamps: 1142789Sahrens * dvp - ctime|mtime updated if new entry created 1143789Sahrens * vp - ctime|mtime always, atime if new 1144789Sahrens */ 11455331Samw 1146789Sahrens /* ARGSUSED */ 1147789Sahrens static int 1148789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 11495331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 11505331Samw vsecattr_t *vsecp) 1151789Sahrens { 1152789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1153789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 11545326Sek110237 zilog_t *zilog; 11555326Sek110237 objset_t *os; 1156789Sahrens zfs_dirlock_t *dl; 1157789Sahrens dmu_tx_t *tx; 1158789Sahrens int error; 11595331Samw zfs_acl_t *aclp = NULL; 11605331Samw zfs_fuid_info_t *fuidp = NULL; 11615331Samw 11625331Samw /* 11635331Samw * If we have an ephemeral id, ACL, or XVATTR then 11645331Samw * make sure file system is at proper version 11655331Samw */ 11665331Samw 11675331Samw if (zfsvfs->z_use_fuids == B_FALSE && 11685331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 11695331Samw IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr)))) 11705331Samw return (EINVAL); 1171789Sahrens 11725367Sahrens ZFS_ENTER(zfsvfs); 11735367Sahrens ZFS_VERIFY_ZP(dzp); 11745326Sek110237 os = zfsvfs->z_os; 11755326Sek110237 zilog = zfsvfs->z_log; 1176789Sahrens 11775498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 11785331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 11795331Samw ZFS_EXIT(zfsvfs); 11805331Samw return (EILSEQ); 11815331Samw } 11825331Samw 11835331Samw if (vap->va_mask & AT_XVATTR) { 11845331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 11855331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 11865331Samw ZFS_EXIT(zfsvfs); 11875331Samw return (error); 11885331Samw } 11895331Samw } 1190789Sahrens top: 1191789Sahrens *vpp = NULL; 1192789Sahrens 1193789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1194789Sahrens vap->va_mode &= ~VSVTX; 1195789Sahrens 1196789Sahrens if (*name == '\0') { 1197789Sahrens /* 1198789Sahrens * Null component name refers to the directory itself. 1199789Sahrens */ 1200789Sahrens VN_HOLD(dvp); 1201789Sahrens zp = dzp; 1202789Sahrens dl = NULL; 1203789Sahrens error = 0; 1204789Sahrens } else { 1205789Sahrens /* possible VN_HOLD(zp) */ 12065331Samw int zflg = 0; 12075331Samw 12085331Samw if (flag & FIGNORECASE) 12095331Samw zflg |= ZCILOOK; 12105331Samw 12115331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 12125331Samw NULL, NULL); 12135331Samw if (error) { 1214789Sahrens if (strcmp(name, "..") == 0) 1215789Sahrens error = EISDIR; 1216789Sahrens ZFS_EXIT(zfsvfs); 12175331Samw if (aclp) 12185331Samw zfs_acl_free(aclp); 1219789Sahrens return (error); 1220789Sahrens } 1221789Sahrens } 12225331Samw if (vsecp && aclp == NULL) { 12235331Samw error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp); 12245331Samw if (error) { 12255331Samw ZFS_EXIT(zfsvfs); 12265331Samw if (dl) 12275331Samw zfs_dirent_unlock(dl); 12285331Samw return (error); 12295331Samw } 12305331Samw } 1231789Sahrens 1232789Sahrens if (zp == NULL) { 12335331Samw uint64_t txtype; 12345331Samw 1235789Sahrens /* 1236789Sahrens * Create a new file object and update the directory 1237789Sahrens * to reference it. 1238789Sahrens */ 12395331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1240789Sahrens goto out; 1241789Sahrens } 1242789Sahrens 1243789Sahrens /* 1244789Sahrens * We only support the creation of regular files in 1245789Sahrens * extended attribute directories. 1246789Sahrens */ 1247789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1248789Sahrens (vap->va_type != VREG)) { 1249789Sahrens error = EINVAL; 1250789Sahrens goto out; 1251789Sahrens } 1252789Sahrens 1253789Sahrens tx = dmu_tx_create(os); 1254789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 12555959Smarks if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(crgetuid(cr)) || 12565959Smarks IS_EPHEMERAL(crgetgid(cr))) { 12575824Smarks if (zfsvfs->z_fuid_obj == 0) { 12585824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 12595824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 12605824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 12615824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 12625824Smarks FALSE, NULL); 12635824Smarks } else { 12645824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 12655824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 12665824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 12675824Smarks } 12685331Samw } 1269789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 12701544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 12715331Samw if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) { 1272789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1273789Sahrens 0, SPA_MAXBLOCKSIZE); 12745331Samw } 1275789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1276789Sahrens if (error) { 1277789Sahrens zfs_dirent_unlock(dl); 1278789Sahrens if (error == ERESTART && 1279789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 12802113Sahrens dmu_tx_wait(tx); 12812113Sahrens dmu_tx_abort(tx); 1282789Sahrens goto top; 1283789Sahrens } 12842113Sahrens dmu_tx_abort(tx); 1285789Sahrens ZFS_EXIT(zfsvfs); 12865331Samw if (aclp) 12875331Samw zfs_acl_free(aclp); 1288789Sahrens return (error); 1289789Sahrens } 12905446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp); 1291789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 12925331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 12935331Samw if (flag & FIGNORECASE) 12945331Samw txtype |= TX_CI; 12955331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 12965331Samw vsecp, fuidp, vap); 12975331Samw if (fuidp) 12985331Samw zfs_fuid_info_free(fuidp); 1299789Sahrens dmu_tx_commit(tx); 1300789Sahrens } else { 13015331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 13025331Samw 1303789Sahrens /* 1304789Sahrens * A directory entry already exists for this name. 1305789Sahrens */ 1306789Sahrens /* 1307789Sahrens * Can't truncate an existing file if in exclusive mode. 1308789Sahrens */ 1309789Sahrens if (excl == EXCL) { 1310789Sahrens error = EEXIST; 1311789Sahrens goto out; 1312789Sahrens } 1313789Sahrens /* 1314789Sahrens * Can't open a directory for writing. 1315789Sahrens */ 1316789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1317789Sahrens error = EISDIR; 1318789Sahrens goto out; 1319789Sahrens } 1320789Sahrens /* 1321789Sahrens * Verify requested access to file. 1322789Sahrens */ 13235331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1324789Sahrens goto out; 1325789Sahrens } 1326789Sahrens 1327789Sahrens mutex_enter(&dzp->z_lock); 1328789Sahrens dzp->z_seq++; 1329789Sahrens mutex_exit(&dzp->z_lock); 1330789Sahrens 13311878Smaybee /* 13321878Smaybee * Truncate regular files if requested. 13331878Smaybee */ 13341878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1335789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 13366992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 13376992Smaybee zfs_dirent_unlock(dl); 13386992Smaybee dl = NULL; 13391878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 13404863Spraks if (error == 0) { 13415331Samw vnevent_create(ZTOV(zp), ct); 13424863Spraks } 1343789Sahrens } 1344789Sahrens } 1345789Sahrens out: 1346789Sahrens 1347789Sahrens if (dl) 1348789Sahrens zfs_dirent_unlock(dl); 1349789Sahrens 1350789Sahrens if (error) { 1351789Sahrens if (zp) 1352789Sahrens VN_RELE(ZTOV(zp)); 1353789Sahrens } else { 1354789Sahrens *vpp = ZTOV(zp); 1355789Sahrens /* 1356789Sahrens * If vnode is for a device return a specfs vnode instead. 1357789Sahrens */ 1358789Sahrens if (IS_DEVVP(*vpp)) { 1359789Sahrens struct vnode *svp; 1360789Sahrens 1361789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1362789Sahrens VN_RELE(*vpp); 1363789Sahrens if (svp == NULL) { 1364789Sahrens error = ENOSYS; 1365789Sahrens } 1366789Sahrens *vpp = svp; 1367789Sahrens } 1368789Sahrens } 13695331Samw if (aclp) 13705331Samw zfs_acl_free(aclp); 1371789Sahrens 1372789Sahrens ZFS_EXIT(zfsvfs); 1373789Sahrens return (error); 1374789Sahrens } 1375789Sahrens 1376789Sahrens /* 1377789Sahrens * Remove an entry from a directory. 1378789Sahrens * 1379789Sahrens * IN: dvp - vnode of directory to remove entry from. 1380789Sahrens * name - name of entry to remove. 1381789Sahrens * cr - credentials of caller. 13825331Samw * ct - caller context 13835331Samw * flags - case flags 1384789Sahrens * 1385789Sahrens * RETURN: 0 if success 1386789Sahrens * error code if failure 1387789Sahrens * 1388789Sahrens * Timestamps: 1389789Sahrens * dvp - ctime|mtime 1390789Sahrens * vp - ctime (if nlink > 0) 1391789Sahrens */ 13925331Samw /*ARGSUSED*/ 1393789Sahrens static int 13945331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 13955331Samw int flags) 1396789Sahrens { 1397789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1398789Sahrens znode_t *xzp = NULL; 1399789Sahrens vnode_t *vp; 1400789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 14015326Sek110237 zilog_t *zilog; 1402789Sahrens uint64_t acl_obj, xattr_obj; 1403789Sahrens zfs_dirlock_t *dl; 1404789Sahrens dmu_tx_t *tx; 14053461Sahrens boolean_t may_delete_now, delete_now = FALSE; 14066992Smaybee boolean_t unlinked, toobig = FALSE; 14075331Samw uint64_t txtype; 14085331Samw pathname_t *realnmp = NULL; 14095331Samw pathname_t realnm; 1410789Sahrens int error; 14115331Samw int zflg = ZEXISTS; 1412789Sahrens 14135367Sahrens ZFS_ENTER(zfsvfs); 14145367Sahrens ZFS_VERIFY_ZP(dzp); 14155326Sek110237 zilog = zfsvfs->z_log; 1416789Sahrens 14175331Samw if (flags & FIGNORECASE) { 14185331Samw zflg |= ZCILOOK; 14195331Samw pn_alloc(&realnm); 14205331Samw realnmp = &realnm; 14215331Samw } 14225331Samw 1423789Sahrens top: 1424789Sahrens /* 1425789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1426789Sahrens */ 14275331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 14285331Samw NULL, realnmp)) { 14295331Samw if (realnmp) 14305331Samw pn_free(realnmp); 1431789Sahrens ZFS_EXIT(zfsvfs); 1432789Sahrens return (error); 1433789Sahrens } 1434789Sahrens 1435789Sahrens vp = ZTOV(zp); 1436789Sahrens 1437789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1438789Sahrens goto out; 1439789Sahrens } 1440789Sahrens 1441789Sahrens /* 1442789Sahrens * Need to use rmdir for removing directories. 1443789Sahrens */ 1444789Sahrens if (vp->v_type == VDIR) { 1445789Sahrens error = EPERM; 1446789Sahrens goto out; 1447789Sahrens } 1448789Sahrens 14495331Samw vnevent_remove(vp, dvp, name, ct); 14505331Samw 14515331Samw if (realnmp) 14526492Stimh dnlc_remove(dvp, realnmp->pn_buf); 14535331Samw else 14545331Samw dnlc_remove(dvp, name); 14551484Sek110237 1456789Sahrens mutex_enter(&vp->v_lock); 1457789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1458789Sahrens mutex_exit(&vp->v_lock); 1459789Sahrens 1460789Sahrens /* 14613461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1462789Sahrens * it depends on whether we're the last link, and on whether there are 1463789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1464789Sahrens * allow for either case. 1465789Sahrens */ 1466789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14671544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1468789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 14696992Smaybee if (may_delete_now) { 14706992Smaybee toobig = 14716992Smaybee zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 14726992Smaybee /* if the file is too big, only hold_free a token amount */ 14736992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 14746992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 14756992Smaybee } 1476789Sahrens 1477789Sahrens /* are there any extended attributes? */ 1478789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1479789Sahrens /* XXX - do we need this if we are deleting? */ 1480789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1481789Sahrens } 1482789Sahrens 1483789Sahrens /* are there any additional acls */ 1484789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1485789Sahrens may_delete_now) 1486789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1487789Sahrens 1488789Sahrens /* charge as an update -- would be nice not to charge at all */ 14893461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1490789Sahrens 1491789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1492789Sahrens if (error) { 1493789Sahrens zfs_dirent_unlock(dl); 1494789Sahrens VN_RELE(vp); 1495789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 14962113Sahrens dmu_tx_wait(tx); 14972113Sahrens dmu_tx_abort(tx); 1498789Sahrens goto top; 1499789Sahrens } 15005331Samw if (realnmp) 15015331Samw pn_free(realnmp); 15022113Sahrens dmu_tx_abort(tx); 1503789Sahrens ZFS_EXIT(zfsvfs); 1504789Sahrens return (error); 1505789Sahrens } 1506789Sahrens 1507789Sahrens /* 1508789Sahrens * Remove the directory entry. 1509789Sahrens */ 15105331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1511789Sahrens 1512789Sahrens if (error) { 1513789Sahrens dmu_tx_commit(tx); 1514789Sahrens goto out; 1515789Sahrens } 1516789Sahrens 15173461Sahrens if (unlinked) { 1518789Sahrens mutex_enter(&vp->v_lock); 15196992Smaybee delete_now = may_delete_now && !toobig && 1520789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1521789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1522789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1523789Sahrens mutex_exit(&vp->v_lock); 1524789Sahrens } 1525789Sahrens 1526789Sahrens if (delete_now) { 1527789Sahrens if (zp->z_phys->zp_xattr) { 1528789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1529789Sahrens ASSERT3U(error, ==, 0); 1530789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1531789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1532789Sahrens mutex_enter(&xzp->z_lock); 15333461Sahrens xzp->z_unlinked = 1; 1534789Sahrens xzp->z_phys->zp_links = 0; 1535789Sahrens mutex_exit(&xzp->z_lock); 15363461Sahrens zfs_unlinked_add(xzp, tx); 1537789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1538789Sahrens } 1539789Sahrens mutex_enter(&zp->z_lock); 1540789Sahrens mutex_enter(&vp->v_lock); 1541789Sahrens vp->v_count--; 1542789Sahrens ASSERT3U(vp->v_count, ==, 0); 1543789Sahrens mutex_exit(&vp->v_lock); 1544789Sahrens mutex_exit(&zp->z_lock); 1545789Sahrens zfs_znode_delete(zp, tx); 15463461Sahrens } else if (unlinked) { 15473461Sahrens zfs_unlinked_add(zp, tx); 1548789Sahrens } 1549789Sahrens 15505331Samw txtype = TX_REMOVE; 15515331Samw if (flags & FIGNORECASE) 15525331Samw txtype |= TX_CI; 15535331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 1554789Sahrens 1555789Sahrens dmu_tx_commit(tx); 1556789Sahrens out: 15575331Samw if (realnmp) 15585331Samw pn_free(realnmp); 15595331Samw 1560789Sahrens zfs_dirent_unlock(dl); 1561789Sahrens 1562789Sahrens if (!delete_now) { 1563789Sahrens VN_RELE(vp); 1564789Sahrens } else if (xzp) { 15656992Smaybee /* this rele is delayed to prevent nesting transactions */ 1566789Sahrens VN_RELE(ZTOV(xzp)); 1567789Sahrens } 1568789Sahrens 1569789Sahrens ZFS_EXIT(zfsvfs); 1570789Sahrens return (error); 1571789Sahrens } 1572789Sahrens 1573789Sahrens /* 1574789Sahrens * Create a new directory and insert it into dvp using the name 1575789Sahrens * provided. Return a pointer to the inserted directory. 1576789Sahrens * 1577789Sahrens * IN: dvp - vnode of directory to add subdir to. 1578789Sahrens * dirname - name of new directory. 1579789Sahrens * vap - attributes of new directory. 1580789Sahrens * cr - credentials of caller. 15815331Samw * ct - caller context 15825331Samw * vsecp - ACL to be set 1583789Sahrens * 1584789Sahrens * OUT: vpp - vnode of created directory. 1585789Sahrens * 1586789Sahrens * RETURN: 0 if success 1587789Sahrens * error code if failure 1588789Sahrens * 1589789Sahrens * Timestamps: 1590789Sahrens * dvp - ctime|mtime updated 1591789Sahrens * vp - ctime|mtime|atime updated 1592789Sahrens */ 15935331Samw /*ARGSUSED*/ 1594789Sahrens static int 15955331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 15965331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1597789Sahrens { 1598789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1599789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 16005326Sek110237 zilog_t *zilog; 1601789Sahrens zfs_dirlock_t *dl; 16025331Samw uint64_t txtype; 1603789Sahrens dmu_tx_t *tx; 1604789Sahrens int error; 16055331Samw zfs_acl_t *aclp = NULL; 16065331Samw zfs_fuid_info_t *fuidp = NULL; 16075331Samw int zf = ZNEW; 1608789Sahrens 1609789Sahrens ASSERT(vap->va_type == VDIR); 1610789Sahrens 16115331Samw /* 16125331Samw * If we have an ephemeral id, ACL, or XVATTR then 16135331Samw * make sure file system is at proper version 16145331Samw */ 16155331Samw 16165331Samw if (zfsvfs->z_use_fuids == B_FALSE && 16175331Samw (vsecp || (vap->va_mask & AT_XVATTR) || IS_EPHEMERAL(crgetuid(cr))|| 16185331Samw IS_EPHEMERAL(crgetgid(cr)))) 16195331Samw return (EINVAL); 16205331Samw 16215367Sahrens ZFS_ENTER(zfsvfs); 16225367Sahrens ZFS_VERIFY_ZP(dzp); 16235326Sek110237 zilog = zfsvfs->z_log; 1624789Sahrens 1625789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1626789Sahrens ZFS_EXIT(zfsvfs); 1627789Sahrens return (EINVAL); 1628789Sahrens } 16295331Samw 16305498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 16315331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 16325331Samw ZFS_EXIT(zfsvfs); 16335331Samw return (EILSEQ); 16345331Samw } 16355331Samw if (flags & FIGNORECASE) 16365331Samw zf |= ZCILOOK; 16375331Samw 16385331Samw if (vap->va_mask & AT_XVATTR) 16395331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 16405331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 16415331Samw ZFS_EXIT(zfsvfs); 16425331Samw return (error); 16435331Samw } 1644789Sahrens 1645789Sahrens /* 1646789Sahrens * First make sure the new directory doesn't exist. 1647789Sahrens */ 16485331Samw top: 16495331Samw *vpp = NULL; 16505331Samw 16515331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 16525331Samw NULL, NULL)) { 1653789Sahrens ZFS_EXIT(zfsvfs); 1654789Sahrens return (error); 1655789Sahrens } 1656789Sahrens 16575331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 16581231Smarks zfs_dirent_unlock(dl); 16591231Smarks ZFS_EXIT(zfsvfs); 16601231Smarks return (error); 16611231Smarks } 16621231Smarks 16635331Samw if (vsecp && aclp == NULL) { 16645331Samw error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp); 16655331Samw if (error) { 16665331Samw zfs_dirent_unlock(dl); 16675331Samw ZFS_EXIT(zfsvfs); 16685331Samw return (error); 16695331Samw } 16705331Samw } 1671789Sahrens /* 1672789Sahrens * Add a new entry to the directory. 1673789Sahrens */ 1674789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16751544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 16761544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 16775959Smarks if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(crgetuid(cr)) || 16785959Smarks IS_EPHEMERAL(crgetgid(cr))) { 16795824Smarks if (zfsvfs->z_fuid_obj == 0) { 16805824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 16815824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 16825824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16835824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 16845824Smarks } else { 16855824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 16865824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 16875824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16885824Smarks } 16895331Samw } 16905331Samw if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) 1691789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1692789Sahrens 0, SPA_MAXBLOCKSIZE); 1693789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1694789Sahrens if (error) { 1695789Sahrens zfs_dirent_unlock(dl); 1696789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 16972113Sahrens dmu_tx_wait(tx); 16982113Sahrens dmu_tx_abort(tx); 1699789Sahrens goto top; 1700789Sahrens } 17012113Sahrens dmu_tx_abort(tx); 1702789Sahrens ZFS_EXIT(zfsvfs); 17035331Samw if (aclp) 17045331Samw zfs_acl_free(aclp); 1705789Sahrens return (error); 1706789Sahrens } 1707789Sahrens 1708789Sahrens /* 1709789Sahrens * Create new node. 1710789Sahrens */ 17115446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp); 17125331Samw 17135331Samw if (aclp) 17145331Samw zfs_acl_free(aclp); 1715789Sahrens 1716789Sahrens /* 1717789Sahrens * Now put new name in parent dir. 1718789Sahrens */ 1719789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1720789Sahrens 1721789Sahrens *vpp = ZTOV(zp); 1722789Sahrens 17235331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 17245331Samw if (flags & FIGNORECASE) 17255331Samw txtype |= TX_CI; 17265331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap); 17275331Samw 17285331Samw if (fuidp) 17295331Samw zfs_fuid_info_free(fuidp); 1730789Sahrens dmu_tx_commit(tx); 1731789Sahrens 1732789Sahrens zfs_dirent_unlock(dl); 1733789Sahrens 1734789Sahrens ZFS_EXIT(zfsvfs); 1735789Sahrens return (0); 1736789Sahrens } 1737789Sahrens 1738789Sahrens /* 1739789Sahrens * Remove a directory subdir entry. If the current working 1740789Sahrens * directory is the same as the subdir to be removed, the 1741789Sahrens * remove will fail. 1742789Sahrens * 1743789Sahrens * IN: dvp - vnode of directory to remove from. 1744789Sahrens * name - name of directory to be removed. 1745789Sahrens * cwd - vnode of current working directory. 1746789Sahrens * cr - credentials of caller. 17475331Samw * ct - caller context 17485331Samw * flags - case flags 1749789Sahrens * 1750789Sahrens * RETURN: 0 if success 1751789Sahrens * error code if failure 1752789Sahrens * 1753789Sahrens * Timestamps: 1754789Sahrens * dvp - ctime|mtime updated 1755789Sahrens */ 17565331Samw /*ARGSUSED*/ 1757789Sahrens static int 17585331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 17595331Samw caller_context_t *ct, int flags) 1760789Sahrens { 1761789Sahrens znode_t *dzp = VTOZ(dvp); 1762789Sahrens znode_t *zp; 1763789Sahrens vnode_t *vp; 1764789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17655326Sek110237 zilog_t *zilog; 1766789Sahrens zfs_dirlock_t *dl; 1767789Sahrens dmu_tx_t *tx; 1768789Sahrens int error; 17695331Samw int zflg = ZEXISTS; 1770789Sahrens 17715367Sahrens ZFS_ENTER(zfsvfs); 17725367Sahrens ZFS_VERIFY_ZP(dzp); 17735326Sek110237 zilog = zfsvfs->z_log; 1774789Sahrens 17755331Samw if (flags & FIGNORECASE) 17765331Samw zflg |= ZCILOOK; 1777789Sahrens top: 1778789Sahrens zp = NULL; 1779789Sahrens 1780789Sahrens /* 1781789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1782789Sahrens */ 17835331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 17845331Samw NULL, NULL)) { 1785789Sahrens ZFS_EXIT(zfsvfs); 1786789Sahrens return (error); 1787789Sahrens } 1788789Sahrens 1789789Sahrens vp = ZTOV(zp); 1790789Sahrens 1791789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1792789Sahrens goto out; 1793789Sahrens } 1794789Sahrens 1795789Sahrens if (vp->v_type != VDIR) { 1796789Sahrens error = ENOTDIR; 1797789Sahrens goto out; 1798789Sahrens } 1799789Sahrens 1800789Sahrens if (vp == cwd) { 1801789Sahrens error = EINVAL; 1802789Sahrens goto out; 1803789Sahrens } 1804789Sahrens 18055331Samw vnevent_rmdir(vp, dvp, name, ct); 1806789Sahrens 1807789Sahrens /* 18083897Smaybee * Grab a lock on the directory to make sure that noone is 18093897Smaybee * trying to add (or lookup) entries while we are removing it. 18103897Smaybee */ 18113897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 18123897Smaybee 18133897Smaybee /* 18143897Smaybee * Grab a lock on the parent pointer to make sure we play well 1815789Sahrens * with the treewalk and directory rename code. 1816789Sahrens */ 1817789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1818789Sahrens 1819789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 18201544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1821789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 18223461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1823789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1824789Sahrens if (error) { 1825789Sahrens rw_exit(&zp->z_parent_lock); 18263897Smaybee rw_exit(&zp->z_name_lock); 1827789Sahrens zfs_dirent_unlock(dl); 1828789Sahrens VN_RELE(vp); 1829789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 18302113Sahrens dmu_tx_wait(tx); 18312113Sahrens dmu_tx_abort(tx); 1832789Sahrens goto top; 1833789Sahrens } 18342113Sahrens dmu_tx_abort(tx); 1835789Sahrens ZFS_EXIT(zfsvfs); 1836789Sahrens return (error); 1837789Sahrens } 1838789Sahrens 18395331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 18405331Samw 18415331Samw if (error == 0) { 18425331Samw uint64_t txtype = TX_RMDIR; 18435331Samw if (flags & FIGNORECASE) 18445331Samw txtype |= TX_CI; 18455331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 18465331Samw } 1847789Sahrens 1848789Sahrens dmu_tx_commit(tx); 1849789Sahrens 1850789Sahrens rw_exit(&zp->z_parent_lock); 18513897Smaybee rw_exit(&zp->z_name_lock); 1852789Sahrens out: 1853789Sahrens zfs_dirent_unlock(dl); 1854789Sahrens 1855789Sahrens VN_RELE(vp); 1856789Sahrens 1857789Sahrens ZFS_EXIT(zfsvfs); 1858789Sahrens return (error); 1859789Sahrens } 1860789Sahrens 1861789Sahrens /* 1862789Sahrens * Read as many directory entries as will fit into the provided 1863789Sahrens * buffer from the given directory cursor position (specified in 1864789Sahrens * the uio structure. 1865789Sahrens * 1866789Sahrens * IN: vp - vnode of directory to read. 1867789Sahrens * uio - structure supplying read location, range info, 1868789Sahrens * and return buffer. 1869789Sahrens * cr - credentials of caller. 18705331Samw * ct - caller context 18715331Samw * flags - case flags 1872789Sahrens * 1873789Sahrens * OUT: uio - updated offset and range, buffer filled. 1874789Sahrens * eofp - set to true if end-of-file detected. 1875789Sahrens * 1876789Sahrens * RETURN: 0 if success 1877789Sahrens * error code if failure 1878789Sahrens * 1879789Sahrens * Timestamps: 1880789Sahrens * vp - atime updated 1881789Sahrens * 1882789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1883789Sahrens * This allows us to use the low range for "special" directory entries: 1884789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1885789Sahrens * we use the offset 2 for the '.zfs' directory. 1886789Sahrens */ 1887789Sahrens /* ARGSUSED */ 1888789Sahrens static int 18895331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 18905331Samw caller_context_t *ct, int flags) 1891789Sahrens { 1892789Sahrens znode_t *zp = VTOZ(vp); 1893789Sahrens iovec_t *iovp; 18945331Samw edirent_t *eodp; 1895789Sahrens dirent64_t *odp; 1896789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1897869Sperrin objset_t *os; 1898789Sahrens caddr_t outbuf; 1899789Sahrens size_t bufsize; 1900789Sahrens zap_cursor_t zc; 1901789Sahrens zap_attribute_t zap; 1902789Sahrens uint_t bytes_wanted; 1903789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1904789Sahrens int local_eof; 1905869Sperrin int outcount; 1906869Sperrin int error; 1907869Sperrin uint8_t prefetch; 19085663Sck153898 boolean_t check_sysattrs; 1909789Sahrens 19105367Sahrens ZFS_ENTER(zfsvfs); 19115367Sahrens ZFS_VERIFY_ZP(zp); 1912789Sahrens 1913789Sahrens /* 1914789Sahrens * If we are not given an eof variable, 1915789Sahrens * use a local one. 1916789Sahrens */ 1917789Sahrens if (eofp == NULL) 1918789Sahrens eofp = &local_eof; 1919789Sahrens 1920789Sahrens /* 1921789Sahrens * Check for valid iov_len. 1922789Sahrens */ 1923789Sahrens if (uio->uio_iov->iov_len <= 0) { 1924789Sahrens ZFS_EXIT(zfsvfs); 1925789Sahrens return (EINVAL); 1926789Sahrens } 1927789Sahrens 1928789Sahrens /* 1929789Sahrens * Quit if directory has been removed (posix) 1930789Sahrens */ 19313461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 1932789Sahrens ZFS_EXIT(zfsvfs); 1933789Sahrens return (0); 1934789Sahrens } 1935789Sahrens 1936869Sperrin error = 0; 1937869Sperrin os = zfsvfs->z_os; 1938869Sperrin offset = uio->uio_loffset; 1939869Sperrin prefetch = zp->z_zn_prefetch; 1940869Sperrin 1941789Sahrens /* 1942789Sahrens * Initialize the iterator cursor. 1943789Sahrens */ 1944789Sahrens if (offset <= 3) { 1945789Sahrens /* 1946789Sahrens * Start iteration from the beginning of the directory. 1947789Sahrens */ 1948869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1949789Sahrens } else { 1950789Sahrens /* 1951789Sahrens * The offset is a serialized cursor. 1952789Sahrens */ 1953869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1954789Sahrens } 1955789Sahrens 1956789Sahrens /* 1957789Sahrens * Get space to change directory entries into fs independent format. 1958789Sahrens */ 1959789Sahrens iovp = uio->uio_iov; 1960789Sahrens bytes_wanted = iovp->iov_len; 1961789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1962789Sahrens bufsize = bytes_wanted; 1963789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1964789Sahrens odp = (struct dirent64 *)outbuf; 1965789Sahrens } else { 1966789Sahrens bufsize = bytes_wanted; 1967789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1968789Sahrens } 19695331Samw eodp = (struct edirent *)odp; 1970789Sahrens 1971789Sahrens /* 19725663Sck153898 * If this VFS supports system attributes; and we're looking at an 19735663Sck153898 * extended attribute directory; and we care about normalization 19745663Sck153898 * conflicts on this vfs; then we must check for normalization 19755663Sck153898 * conflicts with the sysattr name space. 19765663Sck153898 */ 19775663Sck153898 check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) && 19785663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 19795663Sck153898 (flags & V_RDDIR_ENTFLAGS); 19805663Sck153898 19815663Sck153898 /* 1982789Sahrens * Transform to file-system independent format 1983789Sahrens */ 1984789Sahrens outcount = 0; 1985789Sahrens while (outcount < bytes_wanted) { 19863912Slling ino64_t objnum; 19873912Slling ushort_t reclen; 19883912Slling off64_t *next; 19893912Slling 1990789Sahrens /* 1991789Sahrens * Special case `.', `..', and `.zfs'. 1992789Sahrens */ 1993789Sahrens if (offset == 0) { 1994789Sahrens (void) strcpy(zap.za_name, "."); 19955331Samw zap.za_normalization_conflict = 0; 19963912Slling objnum = zp->z_id; 1997789Sahrens } else if (offset == 1) { 1998789Sahrens (void) strcpy(zap.za_name, ".."); 19995331Samw zap.za_normalization_conflict = 0; 20003912Slling objnum = zp->z_phys->zp_parent; 2001789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2002789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 20035331Samw zap.za_normalization_conflict = 0; 20043912Slling objnum = ZFSCTL_INO_ROOT; 2005789Sahrens } else { 2006789Sahrens /* 2007789Sahrens * Grab next entry. 2008789Sahrens */ 2009789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2010789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2011789Sahrens break; 2012789Sahrens else 2013789Sahrens goto update; 2014789Sahrens } 2015789Sahrens 2016789Sahrens if (zap.za_integer_length != 8 || 2017789Sahrens zap.za_num_integers != 1) { 2018789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2019789Sahrens "entry, obj = %lld, offset = %lld\n", 2020789Sahrens (u_longlong_t)zp->z_id, 2021789Sahrens (u_longlong_t)offset); 2022789Sahrens error = ENXIO; 2023789Sahrens goto update; 2024789Sahrens } 20253912Slling 20263912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 20273912Slling /* 20283912Slling * MacOS X can extract the object type here such as: 20293912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 20303912Slling */ 20315663Sck153898 20325663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 20335663Sck153898 zap.za_normalization_conflict = 20345663Sck153898 xattr_sysattr_casechk(zap.za_name); 20355663Sck153898 } 2036789Sahrens } 20375331Samw 20385331Samw if (flags & V_RDDIR_ENTFLAGS) 20395331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 20405331Samw else 20415331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2042789Sahrens 2043789Sahrens /* 2044789Sahrens * Will this entry fit in the buffer? 2045789Sahrens */ 20463912Slling if (outcount + reclen > bufsize) { 2047789Sahrens /* 2048789Sahrens * Did we manage to fit anything in the buffer? 2049789Sahrens */ 2050789Sahrens if (!outcount) { 2051789Sahrens error = EINVAL; 2052789Sahrens goto update; 2053789Sahrens } 2054789Sahrens break; 2055789Sahrens } 20565331Samw if (flags & V_RDDIR_ENTFLAGS) { 20575331Samw /* 20585331Samw * Add extended flag entry: 20595331Samw */ 20605331Samw eodp->ed_ino = objnum; 20615331Samw eodp->ed_reclen = reclen; 20625331Samw /* NOTE: ed_off is the offset for the *next* entry */ 20635331Samw next = &(eodp->ed_off); 20645331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 20655331Samw ED_CASE_CONFLICT : 0; 20665331Samw (void) strncpy(eodp->ed_name, zap.za_name, 20675331Samw EDIRENT_NAMELEN(reclen)); 20685331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 20695331Samw } else { 20705331Samw /* 20715331Samw * Add normal entry: 20725331Samw */ 20735331Samw odp->d_ino = objnum; 20745331Samw odp->d_reclen = reclen; 20755331Samw /* NOTE: d_off is the offset for the *next* entry */ 20765331Samw next = &(odp->d_off); 20775331Samw (void) strncpy(odp->d_name, zap.za_name, 20785331Samw DIRENT64_NAMELEN(reclen)); 20795331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 20805331Samw } 20813912Slling outcount += reclen; 2082789Sahrens 2083789Sahrens ASSERT(outcount <= bufsize); 2084789Sahrens 2085789Sahrens /* Prefetch znode */ 2086869Sperrin if (prefetch) 20873912Slling dmu_prefetch(os, objnum, 0, 0); 2088789Sahrens 2089789Sahrens /* 2090789Sahrens * Move to the next entry, fill in the previous offset. 2091789Sahrens */ 2092789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2093789Sahrens zap_cursor_advance(&zc); 2094789Sahrens offset = zap_cursor_serialize(&zc); 2095789Sahrens } else { 2096789Sahrens offset += 1; 2097789Sahrens } 2098789Sahrens *next = offset; 2099789Sahrens } 2100869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2101789Sahrens 2102789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2103789Sahrens iovp->iov_base += outcount; 2104789Sahrens iovp->iov_len -= outcount; 2105789Sahrens uio->uio_resid -= outcount; 2106789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2107789Sahrens /* 2108789Sahrens * Reset the pointer. 2109789Sahrens */ 2110789Sahrens offset = uio->uio_loffset; 2111789Sahrens } 2112789Sahrens 2113789Sahrens update: 2114885Sahrens zap_cursor_fini(&zc); 2115789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2116789Sahrens kmem_free(outbuf, bufsize); 2117789Sahrens 2118789Sahrens if (error == ENOENT) 2119789Sahrens error = 0; 2120789Sahrens 2121789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2122789Sahrens 2123789Sahrens uio->uio_loffset = offset; 2124789Sahrens ZFS_EXIT(zfsvfs); 2125789Sahrens return (error); 2126789Sahrens } 2127789Sahrens 21284720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 21294720Sfr157268 2130789Sahrens static int 21315331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2132789Sahrens { 2133789Sahrens znode_t *zp = VTOZ(vp); 2134789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2135789Sahrens 21361773Seschrock /* 21371773Seschrock * Regardless of whether this is required for standards conformance, 21381773Seschrock * this is the logical behavior when fsync() is called on a file with 21391773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 21401773Seschrock * going to be pushed out as part of the zil_commit(). 21411773Seschrock */ 21421773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 21431773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 21445331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 21451773Seschrock 21464720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 21474720Sfr157268 21485367Sahrens ZFS_ENTER(zfsvfs); 21495367Sahrens ZFS_VERIFY_ZP(zp); 21502638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 2151789Sahrens ZFS_EXIT(zfsvfs); 2152789Sahrens return (0); 2153789Sahrens } 2154789Sahrens 21555331Samw 2156789Sahrens /* 2157789Sahrens * Get the requested file attributes and place them in the provided 2158789Sahrens * vattr structure. 2159789Sahrens * 2160789Sahrens * IN: vp - vnode of file. 2161789Sahrens * vap - va_mask identifies requested attributes. 21625331Samw * If AT_XVATTR set, then optional attrs are requested 21635331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2164789Sahrens * cr - credentials of caller. 21655331Samw * ct - caller context 2166789Sahrens * 2167789Sahrens * OUT: vap - attribute values. 2168789Sahrens * 2169789Sahrens * RETURN: 0 (always succeeds) 2170789Sahrens */ 2171789Sahrens /* ARGSUSED */ 2172789Sahrens static int 21735331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 21745331Samw caller_context_t *ct) 2175789Sahrens { 2176789Sahrens znode_t *zp = VTOZ(vp); 2177789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 21785326Sek110237 znode_phys_t *pzp; 21795331Samw int error = 0; 21804543Smarks uint64_t links; 21815331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 21825331Samw xoptattr_t *xoap = NULL; 21835331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2184789Sahrens 21855367Sahrens ZFS_ENTER(zfsvfs); 21865367Sahrens ZFS_VERIFY_ZP(zp); 21875326Sek110237 pzp = zp->z_phys; 2188789Sahrens 21895331Samw mutex_enter(&zp->z_lock); 21905331Samw 21915331Samw /* 21925331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 21935331Samw * Also, if we are the owner don't bother, since owner should 21945331Samw * always be allowed to read basic attributes of file. 21955331Samw */ 21965331Samw if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) && 21975331Samw (pzp->zp_uid != crgetuid(cr))) { 21985331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 21995331Samw skipaclchk, cr)) { 22005331Samw mutex_exit(&zp->z_lock); 22015331Samw ZFS_EXIT(zfsvfs); 22025331Samw return (error); 22035331Samw } 22045331Samw } 22055331Samw 2206789Sahrens /* 2207789Sahrens * Return all attributes. It's cheaper to provide the answer 2208789Sahrens * than to determine whether we were asked the question. 2209789Sahrens */ 2210789Sahrens 2211789Sahrens vap->va_type = vp->v_type; 2212789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 22135771Sjp151216 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2214789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2215789Sahrens vap->va_nodeid = zp->z_id; 22164543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 22174543Smarks links = pzp->zp_links + 1; 22184543Smarks else 22194543Smarks links = pzp->zp_links; 22204543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2221789Sahrens vap->va_size = pzp->zp_size; 22221816Smarks vap->va_rdev = vp->v_rdev; 2223789Sahrens vap->va_seq = zp->z_seq; 2224789Sahrens 22255331Samw /* 22265331Samw * Add in any requested optional attributes and the create time. 22275331Samw * Also set the corresponding bits in the returned attribute bitmap. 22285331Samw */ 22295331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 22305331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 22315331Samw xoap->xoa_archive = 22325331Samw ((pzp->zp_flags & ZFS_ARCHIVE) != 0); 22335331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 22345331Samw } 22355331Samw 22365331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 22375331Samw xoap->xoa_readonly = 22385331Samw ((pzp->zp_flags & ZFS_READONLY) != 0); 22395331Samw XVA_SET_RTN(xvap, XAT_READONLY); 22405331Samw } 22415331Samw 22425331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 22435331Samw xoap->xoa_system = 22445331Samw ((pzp->zp_flags & ZFS_SYSTEM) != 0); 22455331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 22465331Samw } 22475331Samw 22485331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 22495331Samw xoap->xoa_hidden = 22505331Samw ((pzp->zp_flags & ZFS_HIDDEN) != 0); 22515331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 22525331Samw } 22535331Samw 22545331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 22555331Samw xoap->xoa_nounlink = 22565331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0); 22575331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 22585331Samw } 22595331Samw 22605331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 22615331Samw xoap->xoa_immutable = 22625331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0); 22635331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 22645331Samw } 22655331Samw 22665331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 22675331Samw xoap->xoa_appendonly = 22685331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0); 22695331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 22705331Samw } 22715331Samw 22725331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 22735331Samw xoap->xoa_nodump = 22745331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0); 22755331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 22765331Samw } 22775331Samw 22785331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 22795331Samw xoap->xoa_opaque = 22805331Samw ((pzp->zp_flags & ZFS_OPAQUE) != 0); 22815331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 22825331Samw } 22835331Samw 22845331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 22855331Samw xoap->xoa_av_quarantined = 22865331Samw ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0); 22875331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 22885331Samw } 22895331Samw 22905331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 22915331Samw xoap->xoa_av_modified = 22925331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0); 22935331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 22945331Samw } 22955331Samw 22965331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 22975331Samw vp->v_type == VREG && 22985331Samw (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) { 22995331Samw size_t len; 23005331Samw dmu_object_info_t doi; 23015331Samw 23025331Samw /* 23035331Samw * Only VREG files have anti-virus scanstamps, so we 23045331Samw * won't conflict with symlinks in the bonus buffer. 23055331Samw */ 23065331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 23075331Samw len = sizeof (xoap->xoa_av_scanstamp) + 23085331Samw sizeof (znode_phys_t); 23095331Samw if (len <= doi.doi_bonus_size) { 23105331Samw /* 23115331Samw * pzp points to the start of the 23125331Samw * znode_phys_t. pzp + 1 points to the 23135331Samw * first byte after the znode_phys_t. 23145331Samw */ 23155331Samw (void) memcpy(xoap->xoa_av_scanstamp, 23165331Samw pzp + 1, 23175331Samw sizeof (xoap->xoa_av_scanstamp)); 23185331Samw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 23195331Samw } 23205331Samw } 23215331Samw 23225331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 23235331Samw ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime); 23245331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 23255331Samw } 23265331Samw } 23275331Samw 2328789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2329789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2330789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2331789Sahrens 2332789Sahrens mutex_exit(&zp->z_lock); 2333789Sahrens 2334789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2335789Sahrens 2336789Sahrens if (zp->z_blksz == 0) { 2337789Sahrens /* 2338789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2339789Sahrens */ 2340789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2341789Sahrens } 2342789Sahrens 2343789Sahrens ZFS_EXIT(zfsvfs); 2344789Sahrens return (0); 2345789Sahrens } 2346789Sahrens 2347789Sahrens /* 2348789Sahrens * Set the file attributes to the values contained in the 2349789Sahrens * vattr structure. 2350789Sahrens * 2351789Sahrens * IN: vp - vnode of file to be modified. 2352789Sahrens * vap - new attribute values. 23535331Samw * If AT_XVATTR set, then optional attrs are being set 2354789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 23555331Samw * - ATTR_NOACLCHECK (CIFS context only). 2356789Sahrens * cr - credentials of caller. 23575331Samw * ct - caller context 2358789Sahrens * 2359789Sahrens * RETURN: 0 if success 2360789Sahrens * error code if failure 2361789Sahrens * 2362789Sahrens * Timestamps: 2363789Sahrens * vp - ctime updated, mtime updated if size changed. 2364789Sahrens */ 2365789Sahrens /* ARGSUSED */ 2366789Sahrens static int 2367789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2368789Sahrens caller_context_t *ct) 2369789Sahrens { 23705326Sek110237 znode_t *zp = VTOZ(vp); 23715326Sek110237 znode_phys_t *pzp; 2372789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23735326Sek110237 zilog_t *zilog; 2374789Sahrens dmu_tx_t *tx; 23751878Smaybee vattr_t oldva; 2376789Sahrens uint_t mask = vap->va_mask; 23771878Smaybee uint_t saved_mask; 23782796Smarks int trim_mask = 0; 2379789Sahrens uint64_t new_mode; 23801231Smarks znode_t *attrzp; 2381789Sahrens int need_policy = FALSE; 2382789Sahrens int err; 23835331Samw zfs_fuid_info_t *fuidp = NULL; 23845331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23855331Samw xoptattr_t *xoap; 23865824Smarks zfs_acl_t *aclp = NULL; 23875331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2388789Sahrens 2389789Sahrens if (mask == 0) 2390789Sahrens return (0); 2391789Sahrens 2392789Sahrens if (mask & AT_NOSET) 2393789Sahrens return (EINVAL); 2394789Sahrens 23955367Sahrens ZFS_ENTER(zfsvfs); 23965367Sahrens ZFS_VERIFY_ZP(zp); 23975331Samw 23985331Samw pzp = zp->z_phys; 23995331Samw zilog = zfsvfs->z_log; 24005331Samw 24015331Samw /* 24025331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 24035331Samw * that file system is at proper version level 24045331Samw */ 24055331Samw 24065331Samw if (zfsvfs->z_use_fuids == B_FALSE && 24075331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 24085331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 24095386Stimh (mask & AT_XVATTR))) { 24105386Stimh ZFS_EXIT(zfsvfs); 24115331Samw return (EINVAL); 24125386Stimh } 24135386Stimh 24145386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 24155386Stimh ZFS_EXIT(zfsvfs); 2416789Sahrens return (EISDIR); 24175386Stimh } 24185386Stimh 24195386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 24205386Stimh ZFS_EXIT(zfsvfs); 24211308Smarks return (EINVAL); 24225386Stimh } 24231308Smarks 24245331Samw /* 24255331Samw * If this is an xvattr_t, then get a pointer to the structure of 24265331Samw * optional attributes. If this is NULL, then we have a vattr_t. 24275331Samw */ 24285331Samw xoap = xva_getxoptattr(xvap); 24295331Samw 24305331Samw /* 24315331Samw * Immutable files can only alter immutable bit and atime 24325331Samw */ 24335331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 24345331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 24355386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 24365386Stimh ZFS_EXIT(zfsvfs); 24375331Samw return (EPERM); 24385386Stimh } 24395386Stimh 24405386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 24415386Stimh ZFS_EXIT(zfsvfs); 24425331Samw return (EPERM); 24435386Stimh } 2444789Sahrens 24456064Smarks /* 24466064Smarks * Verify timestamps doesn't overflow 32 bits. 24476064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 24486064Smarks * handle times greater than 2039. This check should be removed 24496064Smarks * once large timestamps are fully supported. 24506064Smarks */ 24516064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 24526064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 24536064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 24546064Smarks ZFS_EXIT(zfsvfs); 24556064Smarks return (EOVERFLOW); 24566064Smarks } 24576064Smarks } 24586064Smarks 2459789Sahrens top: 24601231Smarks attrzp = NULL; 2461789Sahrens 2462789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2463789Sahrens ZFS_EXIT(zfsvfs); 2464789Sahrens return (EROFS); 2465789Sahrens } 2466789Sahrens 2467789Sahrens /* 2468789Sahrens * First validate permissions 2469789Sahrens */ 2470789Sahrens 2471789Sahrens if (mask & AT_SIZE) { 24725331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2473789Sahrens if (err) { 2474789Sahrens ZFS_EXIT(zfsvfs); 2475789Sahrens return (err); 2476789Sahrens } 24771878Smaybee /* 24781878Smaybee * XXX - Note, we are not providing any open 24791878Smaybee * mode flags here (like FNDELAY), so we may 24801878Smaybee * block if there are locks present... this 24811878Smaybee * should be addressed in openat(). 24821878Smaybee */ 24836992Smaybee /* XXX - would it be OK to generate a log record here? */ 24846992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 24851878Smaybee if (err) { 24861878Smaybee ZFS_EXIT(zfsvfs); 24871878Smaybee return (err); 24881878Smaybee } 2489789Sahrens } 2490789Sahrens 24915331Samw if (mask & (AT_ATIME|AT_MTIME) || 24925331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 24935331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 24945331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 24955331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 24965331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 24975331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 24985331Samw skipaclchk, cr); 2499789Sahrens 2500789Sahrens if (mask & (AT_UID|AT_GID)) { 2501789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2502789Sahrens int take_owner; 2503789Sahrens int take_group; 2504789Sahrens 2505789Sahrens /* 2506913Smarks * NOTE: even if a new mode is being set, 2507913Smarks * we may clear S_ISUID/S_ISGID bits. 2508913Smarks */ 2509913Smarks 2510913Smarks if (!(mask & AT_MODE)) 2511913Smarks vap->va_mode = pzp->zp_mode; 2512913Smarks 2513913Smarks /* 2514789Sahrens * Take ownership or chgrp to group we are a member of 2515789Sahrens */ 2516789Sahrens 2517789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 25185331Samw take_group = (mask & AT_GID) && 25195331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2520789Sahrens 2521789Sahrens /* 2522789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2523789Sahrens * take_group must both be set in order to allow taking 2524789Sahrens * ownership. 2525789Sahrens * 2526789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2527789Sahrens * 2528789Sahrens */ 2529789Sahrens 2530789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2531789Sahrens ((idmask == AT_UID) && take_owner) || 2532789Sahrens ((idmask == AT_GID) && take_group)) { 25335331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 25345331Samw skipaclchk, cr) == 0) { 2535789Sahrens /* 2536789Sahrens * Remove setuid/setgid for non-privileged users 2537789Sahrens */ 25381115Smarks secpolicy_setid_clear(vap, cr); 25392796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2540789Sahrens } else { 2541789Sahrens need_policy = TRUE; 2542789Sahrens } 2543789Sahrens } else { 2544789Sahrens need_policy = TRUE; 2545789Sahrens } 2546789Sahrens } 2547789Sahrens 25482796Smarks mutex_enter(&zp->z_lock); 25492796Smarks oldva.va_mode = pzp->zp_mode; 25505771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 25515331Samw if (mask & AT_XVATTR) { 25525331Samw if ((need_policy == FALSE) && 25535331Samw (XVA_ISSET_REQ(xvap, XAT_APPENDONLY) && 25545331Samw xoap->xoa_appendonly != 25555331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) || 25565331Samw (XVA_ISSET_REQ(xvap, XAT_NOUNLINK) && 25575331Samw xoap->xoa_nounlink != 25585331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) || 25595331Samw (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE) && 25605331Samw xoap->xoa_immutable != 25615331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) || 25625331Samw (XVA_ISSET_REQ(xvap, XAT_NODUMP) && 25635331Samw xoap->xoa_nodump != 25645331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0)) || 25655331Samw (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED) && 25665331Samw xoap->xoa_av_modified != 25675331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) || 25685545Smarks ((XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED) && 25695545Smarks ((vp->v_type != VREG && xoap->xoa_av_quarantined) || 25705331Samw xoap->xoa_av_quarantined != 25715545Smarks ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)))) || 25725331Samw (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) || 25735331Samw (XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 25745331Samw need_policy = TRUE; 25755331Samw } 25765331Samw } 25775331Samw 25782796Smarks mutex_exit(&zp->z_lock); 25792796Smarks 25802796Smarks if (mask & AT_MODE) { 25815331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 25822796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 25832796Smarks &oldva, cr); 25842796Smarks if (err) { 25852796Smarks ZFS_EXIT(zfsvfs); 25862796Smarks return (err); 25872796Smarks } 25882796Smarks trim_mask |= AT_MODE; 25892796Smarks } else { 25902796Smarks need_policy = TRUE; 25912796Smarks } 25922796Smarks } 2593789Sahrens 2594789Sahrens if (need_policy) { 25951115Smarks /* 25961115Smarks * If trim_mask is set then take ownership 25972796Smarks * has been granted or write_acl is present and user 25982796Smarks * has the ability to modify mode. In that case remove 25992796Smarks * UID|GID and or MODE from mask so that 26001115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 26011115Smarks */ 26022796Smarks 26032796Smarks if (trim_mask) { 26042796Smarks saved_mask = vap->va_mask; 26052796Smarks vap->va_mask &= ~trim_mask; 26062796Smarks } 2607789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 26085331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2609789Sahrens if (err) { 2610789Sahrens ZFS_EXIT(zfsvfs); 2611789Sahrens return (err); 2612789Sahrens } 26131115Smarks 26141115Smarks if (trim_mask) 26152796Smarks vap->va_mask |= saved_mask; 2616789Sahrens } 2617789Sahrens 2618789Sahrens /* 2619789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2620789Sahrens * changed va_mask 2621789Sahrens */ 2622789Sahrens mask = vap->va_mask; 2623789Sahrens 2624789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2625789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 26265824Smarks if (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26275824Smarks ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid))) { 26285824Smarks if (zfsvfs->z_fuid_obj == 0) { 26295824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 26305824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 26315824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 26325824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 26335824Smarks } else { 26345824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 26355824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 26365824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 26375824Smarks } 26385331Samw } 2639789Sahrens 2640789Sahrens if (mask & AT_MODE) { 26411576Smarks uint64_t pmode = pzp->zp_mode; 26421576Smarks 26431576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2644789Sahrens 26455824Smarks if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) { 26465824Smarks dmu_tx_abort(tx); 26475824Smarks ZFS_EXIT(zfsvfs); 26485824Smarks return (err); 26495824Smarks } 26505331Samw if (pzp->zp_acl.z_acl_extern_obj) { 26515331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 26525331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 26535331Samw pzp->zp_acl.z_acl_version == 26545331Samw ZFS_ACL_VERSION_INITIAL) { 26555331Samw dmu_tx_hold_free(tx, 26565331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26575331Samw DMU_OBJECT_END); 26585331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26595824Smarks 0, aclp->z_acl_bytes); 26605331Samw } else { 26615331Samw dmu_tx_hold_write(tx, 26625331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26635824Smarks aclp->z_acl_bytes); 26645331Samw } 26656180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 26666180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26676180Smarks 0, aclp->z_acl_bytes); 26685331Samw } 2669789Sahrens } 2670789Sahrens 26715331Samw if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) { 26725331Samw err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 26731231Smarks if (err) { 26741231Smarks dmu_tx_abort(tx); 26751231Smarks ZFS_EXIT(zfsvfs); 26765824Smarks if (aclp) 26775824Smarks zfs_acl_free(aclp); 26781231Smarks return (err); 26791231Smarks } 26801231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 26811231Smarks } 26821231Smarks 2683789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2684789Sahrens if (err) { 26851231Smarks if (attrzp) 26861231Smarks VN_RELE(ZTOV(attrzp)); 26875824Smarks 26885824Smarks if (aclp) { 26895824Smarks zfs_acl_free(aclp); 26905824Smarks aclp = NULL; 26915824Smarks } 26925824Smarks 2693789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 26942113Sahrens dmu_tx_wait(tx); 26952113Sahrens dmu_tx_abort(tx); 2696789Sahrens goto top; 2697789Sahrens } 26982113Sahrens dmu_tx_abort(tx); 2699789Sahrens ZFS_EXIT(zfsvfs); 2700789Sahrens return (err); 2701789Sahrens } 2702789Sahrens 2703789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2704789Sahrens 2705789Sahrens /* 2706789Sahrens * Set each attribute requested. 2707789Sahrens * We group settings according to the locks they need to acquire. 2708789Sahrens * 2709789Sahrens * Note: you cannot set ctime directly, although it will be 2710789Sahrens * updated as a side-effect of calling this function. 2711789Sahrens */ 2712789Sahrens 2713789Sahrens mutex_enter(&zp->z_lock); 2714789Sahrens 2715789Sahrens if (mask & AT_MODE) { 27165824Smarks mutex_enter(&zp->z_acl_lock); 27175824Smarks zp->z_phys->zp_mode = new_mode; 27185824Smarks err = zfs_aclset_common(zp, aclp, cr, &fuidp, tx); 2719789Sahrens ASSERT3U(err, ==, 0); 27205824Smarks mutex_exit(&zp->z_acl_lock); 2721789Sahrens } 2722789Sahrens 27231231Smarks if (attrzp) 27241231Smarks mutex_enter(&attrzp->z_lock); 27251231Smarks 27261231Smarks if (mask & AT_UID) { 27275331Samw pzp->zp_uid = zfs_fuid_create(zfsvfs, 27285771Sjp151216 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp); 27291231Smarks if (attrzp) { 27305331Samw attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs, 27315771Sjp151216 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp); 27321231Smarks } 27331231Smarks } 27341231Smarks 27351231Smarks if (mask & AT_GID) { 27365331Samw pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid, 27375771Sjp151216 cr, ZFS_GROUP, tx, &fuidp); 27381231Smarks if (attrzp) 27395331Samw attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs, 27405771Sjp151216 vap->va_gid, cr, ZFS_GROUP, tx, &fuidp); 27411231Smarks } 27421231Smarks 27435824Smarks if (aclp) 27445824Smarks zfs_acl_free(aclp); 27455824Smarks 27461231Smarks if (attrzp) 27471231Smarks mutex_exit(&attrzp->z_lock); 2748789Sahrens 2749789Sahrens if (mask & AT_ATIME) 2750789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2751789Sahrens 2752789Sahrens if (mask & AT_MTIME) 2753789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2754789Sahrens 27556992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 27561878Smaybee if (mask & AT_SIZE) 2757789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 27581878Smaybee else if (mask != 0) 2759789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 27605331Samw /* 27615331Samw * Do this after setting timestamps to prevent timestamp 27625331Samw * update from toggling bit 27635331Samw */ 27645331Samw 27655331Samw if (xoap && (mask & AT_XVATTR)) { 27665331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 27675331Samw size_t len; 27685331Samw dmu_object_info_t doi; 27695331Samw 27705331Samw ASSERT(vp->v_type == VREG); 27715331Samw 27725331Samw /* Grow the bonus buffer if necessary. */ 27735331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 27745331Samw len = sizeof (xoap->xoa_av_scanstamp) + 27755331Samw sizeof (znode_phys_t); 27765331Samw if (len > doi.doi_bonus_size) 27775331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 27785331Samw } 27795331Samw zfs_xvattr_set(zp, xvap); 27805331Samw } 2781789Sahrens 27821878Smaybee if (mask != 0) 27835331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 27845331Samw 27855331Samw if (fuidp) 27865331Samw zfs_fuid_info_free(fuidp); 2787789Sahrens mutex_exit(&zp->z_lock); 2788789Sahrens 27891231Smarks if (attrzp) 27901231Smarks VN_RELE(ZTOV(attrzp)); 27911231Smarks 2792789Sahrens dmu_tx_commit(tx); 2793789Sahrens 2794789Sahrens ZFS_EXIT(zfsvfs); 2795789Sahrens return (err); 2796789Sahrens } 2797789Sahrens 27983271Smaybee typedef struct zfs_zlock { 27993271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 28003271Smaybee znode_t *zl_znode; /* znode we held */ 28013271Smaybee struct zfs_zlock *zl_next; /* next in list */ 28023271Smaybee } zfs_zlock_t; 28033271Smaybee 28043271Smaybee /* 28053271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 28063271Smaybee */ 28073271Smaybee static void 28083271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 28093271Smaybee { 28103271Smaybee zfs_zlock_t *zl; 28113271Smaybee 28123271Smaybee while ((zl = *zlpp) != NULL) { 28133271Smaybee if (zl->zl_znode != NULL) 28143271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 28153271Smaybee rw_exit(zl->zl_rwlock); 28163271Smaybee *zlpp = zl->zl_next; 28173271Smaybee kmem_free(zl, sizeof (*zl)); 28183271Smaybee } 28193271Smaybee } 28203271Smaybee 2821789Sahrens /* 2822789Sahrens * Search back through the directory tree, using the ".." entries. 2823789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2824789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2825789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2826789Sahrens */ 2827789Sahrens static int 2828789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2829789Sahrens { 2830789Sahrens zfs_zlock_t *zl; 28313638Sbillm znode_t *zp = tdzp; 2832789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2833789Sahrens uint64_t *oidp = &zp->z_id; 2834789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2835789Sahrens krw_t rw = RW_WRITER; 2836789Sahrens 2837789Sahrens /* 2838789Sahrens * First pass write-locks szp and compares to zp->z_id. 2839789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2840789Sahrens */ 2841789Sahrens do { 28423271Smaybee if (!rw_tryenter(rwlp, rw)) { 28433271Smaybee /* 28443271Smaybee * Another thread is renaming in this path. 28453271Smaybee * Note that if we are a WRITER, we don't have any 28463271Smaybee * parent_locks held yet. 28473271Smaybee */ 28483271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 28493271Smaybee /* 28503271Smaybee * Drop our locks and restart 28513271Smaybee */ 28523271Smaybee zfs_rename_unlock(&zl); 28533271Smaybee *zlpp = NULL; 28543271Smaybee zp = tdzp; 28553271Smaybee oidp = &zp->z_id; 28563271Smaybee rwlp = &szp->z_parent_lock; 28573271Smaybee rw = RW_WRITER; 28583271Smaybee continue; 28593271Smaybee } else { 28603271Smaybee /* 28613271Smaybee * Wait for other thread to drop its locks 28623271Smaybee */ 28633271Smaybee rw_enter(rwlp, rw); 28643271Smaybee } 28653271Smaybee } 28663271Smaybee 2867789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2868789Sahrens zl->zl_rwlock = rwlp; 2869789Sahrens zl->zl_znode = NULL; 2870789Sahrens zl->zl_next = *zlpp; 2871789Sahrens *zlpp = zl; 2872789Sahrens 2873789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2874789Sahrens return (EINVAL); 2875789Sahrens 2876789Sahrens if (*oidp == rootid) /* We've hit the top */ 2877789Sahrens return (0); 2878789Sahrens 2879789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2880789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2881789Sahrens if (error) 2882789Sahrens return (error); 2883789Sahrens zl->zl_znode = zp; 2884789Sahrens } 2885789Sahrens oidp = &zp->z_phys->zp_parent; 2886789Sahrens rwlp = &zp->z_parent_lock; 2887789Sahrens rw = RW_READER; 2888789Sahrens 2889789Sahrens } while (zp->z_id != sdzp->z_id); 2890789Sahrens 2891789Sahrens return (0); 2892789Sahrens } 2893789Sahrens 2894789Sahrens /* 2895789Sahrens * Move an entry from the provided source directory to the target 2896789Sahrens * directory. Change the entry name as indicated. 2897789Sahrens * 2898789Sahrens * IN: sdvp - Source directory containing the "old entry". 2899789Sahrens * snm - Old entry name. 2900789Sahrens * tdvp - Target directory to contain the "new entry". 2901789Sahrens * tnm - New entry name. 2902789Sahrens * cr - credentials of caller. 29035331Samw * ct - caller context 29045331Samw * flags - case flags 2905789Sahrens * 2906789Sahrens * RETURN: 0 if success 2907789Sahrens * error code if failure 2908789Sahrens * 2909789Sahrens * Timestamps: 2910789Sahrens * sdvp,tdvp - ctime|mtime updated 2911789Sahrens */ 29125331Samw /*ARGSUSED*/ 2913789Sahrens static int 29145331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 29155331Samw caller_context_t *ct, int flags) 2916789Sahrens { 2917789Sahrens znode_t *tdzp, *szp, *tzp; 2918789Sahrens znode_t *sdzp = VTOZ(sdvp); 2919789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 29205326Sek110237 zilog_t *zilog; 2921789Sahrens vnode_t *realvp; 2922789Sahrens zfs_dirlock_t *sdl, *tdl; 2923789Sahrens dmu_tx_t *tx; 2924789Sahrens zfs_zlock_t *zl; 29255331Samw int cmp, serr, terr; 29265331Samw int error = 0; 29275331Samw int zflg = 0; 2928789Sahrens 29295367Sahrens ZFS_ENTER(zfsvfs); 29305367Sahrens ZFS_VERIFY_ZP(sdzp); 29315326Sek110237 zilog = zfsvfs->z_log; 2932789Sahrens 2933789Sahrens /* 2934789Sahrens * Make sure we have the real vp for the target directory. 2935789Sahrens */ 29365331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 2937789Sahrens tdvp = realvp; 2938789Sahrens 2939789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2940789Sahrens ZFS_EXIT(zfsvfs); 2941789Sahrens return (EXDEV); 2942789Sahrens } 2943789Sahrens 2944789Sahrens tdzp = VTOZ(tdvp); 29455367Sahrens ZFS_VERIFY_ZP(tdzp); 29465498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 29475331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 29485331Samw ZFS_EXIT(zfsvfs); 29495331Samw return (EILSEQ); 29505331Samw } 29515331Samw 29525331Samw if (flags & FIGNORECASE) 29535331Samw zflg |= ZCILOOK; 29545331Samw 2955789Sahrens top: 2956789Sahrens szp = NULL; 2957789Sahrens tzp = NULL; 2958789Sahrens zl = NULL; 2959789Sahrens 2960789Sahrens /* 2961789Sahrens * This is to prevent the creation of links into attribute space 2962789Sahrens * by renaming a linked file into/outof an attribute directory. 2963789Sahrens * See the comment in zfs_link() for why this is considered bad. 2964789Sahrens */ 2965789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2966789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2967789Sahrens ZFS_EXIT(zfsvfs); 2968789Sahrens return (EINVAL); 2969789Sahrens } 2970789Sahrens 2971789Sahrens /* 2972789Sahrens * Lock source and target directory entries. To prevent deadlock, 2973789Sahrens * a lock ordering must be defined. We lock the directory with 2974789Sahrens * the smallest object id first, or if it's a tie, the one with 2975789Sahrens * the lexically first name. 2976789Sahrens */ 2977789Sahrens if (sdzp->z_id < tdzp->z_id) { 2978789Sahrens cmp = -1; 2979789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2980789Sahrens cmp = 1; 2981789Sahrens } else { 29825331Samw /* 29835331Samw * First compare the two name arguments without 29845331Samw * considering any case folding. 29855331Samw */ 29865331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 29875331Samw 29885331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 29895498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 2990789Sahrens if (cmp == 0) { 2991789Sahrens /* 2992789Sahrens * POSIX: "If the old argument and the new argument 2993789Sahrens * both refer to links to the same existing file, 2994789Sahrens * the rename() function shall return successfully 2995789Sahrens * and perform no other action." 2996789Sahrens */ 2997789Sahrens ZFS_EXIT(zfsvfs); 2998789Sahrens return (0); 2999789Sahrens } 30005331Samw /* 30015331Samw * If the file system is case-folding, then we may 30025331Samw * have some more checking to do. A case-folding file 30035331Samw * system is either supporting mixed case sensitivity 30045331Samw * access or is completely case-insensitive. Note 30055331Samw * that the file system is always case preserving. 30065331Samw * 30075331Samw * In mixed sensitivity mode case sensitive behavior 30085331Samw * is the default. FIGNORECASE must be used to 30095331Samw * explicitly request case insensitive behavior. 30105331Samw * 30115331Samw * If the source and target names provided differ only 30125331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 30135331Samw * we will treat this as a special case in the 30145331Samw * case-insensitive mode: as long as the source name 30155331Samw * is an exact match, we will allow this to proceed as 30165331Samw * a name-change request. 30175331Samw */ 30185498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 30195498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 30205498Stimh flags & FIGNORECASE)) && 30215331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 30225331Samw &error) == 0) { 30235331Samw /* 30245331Samw * case preserving rename request, require exact 30255331Samw * name matches 30265331Samw */ 30275331Samw zflg |= ZCIEXACT; 30285331Samw zflg &= ~ZCILOOK; 30295331Samw } 3030789Sahrens } 30315331Samw 3032789Sahrens if (cmp < 0) { 30335331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 30345331Samw ZEXISTS | zflg, NULL, NULL); 30355331Samw terr = zfs_dirent_lock(&tdl, 30365331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3037789Sahrens } else { 30385331Samw terr = zfs_dirent_lock(&tdl, 30395331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 30405331Samw serr = zfs_dirent_lock(&sdl, 30415331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 30425331Samw NULL, NULL); 3043789Sahrens } 3044789Sahrens 3045789Sahrens if (serr) { 3046789Sahrens /* 3047789Sahrens * Source entry invalid or not there. 3048789Sahrens */ 3049789Sahrens if (!terr) { 3050789Sahrens zfs_dirent_unlock(tdl); 3051789Sahrens if (tzp) 3052789Sahrens VN_RELE(ZTOV(tzp)); 3053789Sahrens } 3054789Sahrens if (strcmp(snm, "..") == 0) 3055789Sahrens serr = EINVAL; 3056789Sahrens ZFS_EXIT(zfsvfs); 3057789Sahrens return (serr); 3058789Sahrens } 3059789Sahrens if (terr) { 3060789Sahrens zfs_dirent_unlock(sdl); 3061789Sahrens VN_RELE(ZTOV(szp)); 3062789Sahrens if (strcmp(tnm, "..") == 0) 3063789Sahrens terr = EINVAL; 3064789Sahrens ZFS_EXIT(zfsvfs); 3065789Sahrens return (terr); 3066789Sahrens } 3067789Sahrens 3068789Sahrens /* 3069789Sahrens * Must have write access at the source to remove the old entry 3070789Sahrens * and write access at the target to create the new entry. 3071789Sahrens * Note that if target and source are the same, this can be 3072789Sahrens * done in a single check. 3073789Sahrens */ 3074789Sahrens 3075789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3076789Sahrens goto out; 3077789Sahrens 3078789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3079789Sahrens /* 3080789Sahrens * Check to make sure rename is valid. 3081789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3082789Sahrens */ 3083789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3084789Sahrens goto out; 3085789Sahrens } 3086789Sahrens 3087789Sahrens /* 3088789Sahrens * Does target exist? 3089789Sahrens */ 3090789Sahrens if (tzp) { 3091789Sahrens /* 3092789Sahrens * Source and target must be the same type. 3093789Sahrens */ 3094789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3095789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3096789Sahrens error = ENOTDIR; 3097789Sahrens goto out; 3098789Sahrens } 3099789Sahrens } else { 3100789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3101789Sahrens error = EISDIR; 3102789Sahrens goto out; 3103789Sahrens } 3104789Sahrens } 3105789Sahrens /* 3106789Sahrens * POSIX dictates that when the source and target 3107789Sahrens * entries refer to the same file object, rename 3108789Sahrens * must do nothing and exit without error. 3109789Sahrens */ 3110789Sahrens if (szp->z_id == tzp->z_id) { 3111789Sahrens error = 0; 3112789Sahrens goto out; 3113789Sahrens } 3114789Sahrens } 3115789Sahrens 31165331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3117789Sahrens if (tzp) 31185331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 31194863Spraks 31204863Spraks /* 31214863Spraks * notify the target directory if it is not the same 31224863Spraks * as source directory. 31234863Spraks */ 31244863Spraks if (tdvp != sdvp) { 31255331Samw vnevent_rename_dest_dir(tdvp, ct); 31264863Spraks } 3127789Sahrens 3128789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3129789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3130789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 31311544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 31321544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 31331544Seschrock if (sdzp != tdzp) 3134789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 31351544Seschrock if (tzp) 31361544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 31373461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 3138789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 3139789Sahrens if (error) { 3140789Sahrens if (zl != NULL) 3141789Sahrens zfs_rename_unlock(&zl); 3142789Sahrens zfs_dirent_unlock(sdl); 3143789Sahrens zfs_dirent_unlock(tdl); 3144789Sahrens VN_RELE(ZTOV(szp)); 3145789Sahrens if (tzp) 3146789Sahrens VN_RELE(ZTOV(tzp)); 3147789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 31482113Sahrens dmu_tx_wait(tx); 31492113Sahrens dmu_tx_abort(tx); 3150789Sahrens goto top; 3151789Sahrens } 31522113Sahrens dmu_tx_abort(tx); 3153789Sahrens ZFS_EXIT(zfsvfs); 3154789Sahrens return (error); 3155789Sahrens } 3156789Sahrens 3157789Sahrens if (tzp) /* Attempt to remove the existing target */ 31585331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3159789Sahrens 3160789Sahrens if (error == 0) { 3161789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3162789Sahrens if (error == 0) { 31635331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 31645331Samw 3165789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3166789Sahrens ASSERT(error == 0); 31675331Samw 31685331Samw zfs_log_rename(zilog, tx, 31695331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 31705331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 31716976Seschrock 31726976Seschrock /* Update path information for the target vnode */ 31736976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3174789Sahrens } 3175789Sahrens } 3176789Sahrens 3177789Sahrens dmu_tx_commit(tx); 3178789Sahrens out: 3179789Sahrens if (zl != NULL) 3180789Sahrens zfs_rename_unlock(&zl); 3181789Sahrens 3182789Sahrens zfs_dirent_unlock(sdl); 3183789Sahrens zfs_dirent_unlock(tdl); 3184789Sahrens 3185789Sahrens VN_RELE(ZTOV(szp)); 3186789Sahrens if (tzp) 3187789Sahrens VN_RELE(ZTOV(tzp)); 3188789Sahrens 3189789Sahrens ZFS_EXIT(zfsvfs); 3190789Sahrens return (error); 3191789Sahrens } 3192789Sahrens 3193789Sahrens /* 3194789Sahrens * Insert the indicated symbolic reference entry into the directory. 3195789Sahrens * 3196789Sahrens * IN: dvp - Directory to contain new symbolic link. 3197789Sahrens * link - Name for new symlink entry. 3198789Sahrens * vap - Attributes of new entry. 3199789Sahrens * target - Target path of new symlink. 3200789Sahrens * cr - credentials of caller. 32015331Samw * ct - caller context 32025331Samw * flags - case flags 3203789Sahrens * 3204789Sahrens * RETURN: 0 if success 3205789Sahrens * error code if failure 3206789Sahrens * 3207789Sahrens * Timestamps: 3208789Sahrens * dvp - ctime|mtime updated 3209789Sahrens */ 32105331Samw /*ARGSUSED*/ 3211789Sahrens static int 32125331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 32135331Samw caller_context_t *ct, int flags) 3214789Sahrens { 3215789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3216789Sahrens zfs_dirlock_t *dl; 3217789Sahrens dmu_tx_t *tx; 3218789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 32195326Sek110237 zilog_t *zilog; 3220789Sahrens int len = strlen(link); 3221789Sahrens int error; 32225331Samw int zflg = ZNEW; 32235331Samw zfs_fuid_info_t *fuidp = NULL; 3224789Sahrens 3225789Sahrens ASSERT(vap->va_type == VLNK); 3226789Sahrens 32275367Sahrens ZFS_ENTER(zfsvfs); 32285367Sahrens ZFS_VERIFY_ZP(dzp); 32295326Sek110237 zilog = zfsvfs->z_log; 32305331Samw 32315498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 32325331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 32335331Samw ZFS_EXIT(zfsvfs); 32345331Samw return (EILSEQ); 32355331Samw } 32365331Samw if (flags & FIGNORECASE) 32375331Samw zflg |= ZCILOOK; 3238789Sahrens top: 32395331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3240789Sahrens ZFS_EXIT(zfsvfs); 3241789Sahrens return (error); 3242789Sahrens } 3243789Sahrens 3244789Sahrens if (len > MAXPATHLEN) { 3245789Sahrens ZFS_EXIT(zfsvfs); 3246789Sahrens return (ENAMETOOLONG); 3247789Sahrens } 3248789Sahrens 3249789Sahrens /* 3250789Sahrens * Attempt to lock directory; fail if entry already exists. 3251789Sahrens */ 32525331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 32535331Samw if (error) { 3254789Sahrens ZFS_EXIT(zfsvfs); 3255789Sahrens return (error); 3256789Sahrens } 3257789Sahrens 3258789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3259789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3260789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 32611544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3262789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 3263789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 32645824Smarks if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) { 32655824Smarks if (zfsvfs->z_fuid_obj == 0) { 32665824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 32675824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 32685824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32695824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 32705824Smarks } else { 32715824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 32725824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 32735824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32745824Smarks } 32755331Samw } 3276789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 3277789Sahrens if (error) { 3278789Sahrens zfs_dirent_unlock(dl); 3279789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 32802113Sahrens dmu_tx_wait(tx); 32812113Sahrens dmu_tx_abort(tx); 3282789Sahrens goto top; 3283789Sahrens } 32842113Sahrens dmu_tx_abort(tx); 3285789Sahrens ZFS_EXIT(zfsvfs); 3286789Sahrens return (error); 3287789Sahrens } 3288789Sahrens 3289789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3290789Sahrens 3291789Sahrens /* 3292789Sahrens * Create a new object for the symlink. 3293789Sahrens * Put the link content into bonus buffer if it will fit; 3294789Sahrens * otherwise, store it just like any other file data. 3295789Sahrens */ 3296789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 32975446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp); 3298789Sahrens if (len != 0) 3299789Sahrens bcopy(link, zp->z_phys + 1, len); 3300789Sahrens } else { 3301789Sahrens dmu_buf_t *dbp; 33021669Sperrin 33035446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp); 33041669Sperrin /* 33051669Sperrin * Nothing can access the znode yet so no locking needed 33061669Sperrin * for growing the znode's blocksize. 33071669Sperrin */ 33081669Sperrin zfs_grow_blocksize(zp, len, tx); 3309789Sahrens 33105446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 33115446Sahrens zp->z_id, 0, FTAG, &dbp)); 3312789Sahrens dmu_buf_will_dirty(dbp, tx); 3313789Sahrens 3314789Sahrens ASSERT3U(len, <=, dbp->db_size); 3315789Sahrens bcopy(link, dbp->db_data, len); 33161544Seschrock dmu_buf_rele(dbp, FTAG); 3317789Sahrens } 3318789Sahrens zp->z_phys->zp_size = len; 3319789Sahrens 3320789Sahrens /* 3321789Sahrens * Insert the new object into the directory. 3322789Sahrens */ 3323789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 3324789Sahrens out: 33255331Samw if (error == 0) { 33265331Samw uint64_t txtype = TX_SYMLINK; 33275331Samw if (flags & FIGNORECASE) 33285331Samw txtype |= TX_CI; 33295331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 33305331Samw } 33315331Samw if (fuidp) 33325331Samw zfs_fuid_info_free(fuidp); 3333789Sahrens 3334789Sahrens dmu_tx_commit(tx); 3335789Sahrens 3336789Sahrens zfs_dirent_unlock(dl); 3337789Sahrens 3338789Sahrens VN_RELE(ZTOV(zp)); 3339789Sahrens 3340789Sahrens ZFS_EXIT(zfsvfs); 3341789Sahrens return (error); 3342789Sahrens } 3343789Sahrens 3344789Sahrens /* 3345789Sahrens * Return, in the buffer contained in the provided uio structure, 3346789Sahrens * the symbolic path referred to by vp. 3347789Sahrens * 3348789Sahrens * IN: vp - vnode of symbolic link. 3349789Sahrens * uoip - structure to contain the link path. 3350789Sahrens * cr - credentials of caller. 33515331Samw * ct - caller context 3352789Sahrens * 3353789Sahrens * OUT: uio - structure to contain the link path. 3354789Sahrens * 3355789Sahrens * RETURN: 0 if success 3356789Sahrens * error code if failure 3357789Sahrens * 3358789Sahrens * Timestamps: 3359789Sahrens * vp - atime updated 3360789Sahrens */ 3361789Sahrens /* ARGSUSED */ 3362789Sahrens static int 33635331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3364789Sahrens { 3365789Sahrens znode_t *zp = VTOZ(vp); 3366789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3367789Sahrens size_t bufsz; 3368789Sahrens int error; 3369789Sahrens 33705367Sahrens ZFS_ENTER(zfsvfs); 33715367Sahrens ZFS_VERIFY_ZP(zp); 3372789Sahrens 3373789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3374789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3375789Sahrens error = uiomove(zp->z_phys + 1, 3376789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3377789Sahrens } else { 33781544Seschrock dmu_buf_t *dbp; 33791544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 33801544Seschrock if (error) { 3381789Sahrens ZFS_EXIT(zfsvfs); 3382789Sahrens return (error); 3383789Sahrens } 3384789Sahrens error = uiomove(dbp->db_data, 3385789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 33861544Seschrock dmu_buf_rele(dbp, FTAG); 3387789Sahrens } 3388789Sahrens 3389789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3390789Sahrens ZFS_EXIT(zfsvfs); 3391789Sahrens return (error); 3392789Sahrens } 3393789Sahrens 3394789Sahrens /* 3395789Sahrens * Insert a new entry into directory tdvp referencing svp. 3396789Sahrens * 3397789Sahrens * IN: tdvp - Directory to contain new entry. 3398789Sahrens * svp - vnode of new entry. 3399789Sahrens * name - name of new entry. 3400789Sahrens * cr - credentials of caller. 34015331Samw * ct - caller context 3402789Sahrens * 3403789Sahrens * RETURN: 0 if success 3404789Sahrens * error code if failure 3405789Sahrens * 3406789Sahrens * Timestamps: 3407789Sahrens * tdvp - ctime|mtime updated 3408789Sahrens * svp - ctime updated 3409789Sahrens */ 3410789Sahrens /* ARGSUSED */ 3411789Sahrens static int 34125331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 34135331Samw caller_context_t *ct, int flags) 3414789Sahrens { 3415789Sahrens znode_t *dzp = VTOZ(tdvp); 3416789Sahrens znode_t *tzp, *szp; 3417789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 34185326Sek110237 zilog_t *zilog; 3419789Sahrens zfs_dirlock_t *dl; 3420789Sahrens dmu_tx_t *tx; 3421789Sahrens vnode_t *realvp; 3422789Sahrens int error; 34235331Samw int zf = ZNEW; 34245331Samw uid_t owner; 3425789Sahrens 3426789Sahrens ASSERT(tdvp->v_type == VDIR); 3427789Sahrens 34285367Sahrens ZFS_ENTER(zfsvfs); 34295367Sahrens ZFS_VERIFY_ZP(dzp); 34305326Sek110237 zilog = zfsvfs->z_log; 3431789Sahrens 34325331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3433789Sahrens svp = realvp; 3434789Sahrens 3435789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3436789Sahrens ZFS_EXIT(zfsvfs); 3437789Sahrens return (EXDEV); 3438789Sahrens } 34395367Sahrens szp = VTOZ(svp); 34405367Sahrens ZFS_VERIFY_ZP(szp); 3441789Sahrens 34425498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 34435331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 34445331Samw ZFS_EXIT(zfsvfs); 34455331Samw return (EILSEQ); 34465331Samw } 34475331Samw if (flags & FIGNORECASE) 34485331Samw zf |= ZCILOOK; 34495331Samw 3450789Sahrens top: 3451789Sahrens /* 3452789Sahrens * We do not support links between attributes and non-attributes 3453789Sahrens * because of the potential security risk of creating links 3454789Sahrens * into "normal" file space in order to circumvent restrictions 3455789Sahrens * imposed in attribute space. 3456789Sahrens */ 3457789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3458789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3459789Sahrens ZFS_EXIT(zfsvfs); 3460789Sahrens return (EINVAL); 3461789Sahrens } 3462789Sahrens 3463789Sahrens /* 3464789Sahrens * POSIX dictates that we return EPERM here. 3465789Sahrens * Better choices include ENOTSUP or EISDIR. 3466789Sahrens */ 3467789Sahrens if (svp->v_type == VDIR) { 3468789Sahrens ZFS_EXIT(zfsvfs); 3469789Sahrens return (EPERM); 3470789Sahrens } 3471789Sahrens 34725959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 34735331Samw if (owner != crgetuid(cr) && 3474789Sahrens secpolicy_basic_link(cr) != 0) { 3475789Sahrens ZFS_EXIT(zfsvfs); 3476789Sahrens return (EPERM); 3477789Sahrens } 3478789Sahrens 34795331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3480789Sahrens ZFS_EXIT(zfsvfs); 3481789Sahrens return (error); 3482789Sahrens } 3483789Sahrens 3484789Sahrens /* 3485789Sahrens * Attempt to lock directory; fail if entry already exists. 3486789Sahrens */ 34875331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 34885331Samw if (error) { 3489789Sahrens ZFS_EXIT(zfsvfs); 3490789Sahrens return (error); 3491789Sahrens } 3492789Sahrens 3493789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3494789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 34951544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3496789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 3497789Sahrens if (error) { 3498789Sahrens zfs_dirent_unlock(dl); 3499789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 35002113Sahrens dmu_tx_wait(tx); 35012113Sahrens dmu_tx_abort(tx); 3502789Sahrens goto top; 3503789Sahrens } 35042113Sahrens dmu_tx_abort(tx); 3505789Sahrens ZFS_EXIT(zfsvfs); 3506789Sahrens return (error); 3507789Sahrens } 3508789Sahrens 3509789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3510789Sahrens 35115331Samw if (error == 0) { 35125331Samw uint64_t txtype = TX_LINK; 35135331Samw if (flags & FIGNORECASE) 35145331Samw txtype |= TX_CI; 35155331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 35165331Samw } 3517789Sahrens 3518789Sahrens dmu_tx_commit(tx); 3519789Sahrens 3520789Sahrens zfs_dirent_unlock(dl); 3521789Sahrens 35224863Spraks if (error == 0) { 35235331Samw vnevent_link(svp, ct); 35244863Spraks } 35254863Spraks 3526789Sahrens ZFS_EXIT(zfsvfs); 3527789Sahrens return (error); 3528789Sahrens } 3529789Sahrens 3530789Sahrens /* 3531789Sahrens * zfs_null_putapage() is used when the file system has been force 3532789Sahrens * unmounted. It just drops the pages. 3533789Sahrens */ 3534789Sahrens /* ARGSUSED */ 3535789Sahrens static int 3536789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3537789Sahrens size_t *lenp, int flags, cred_t *cr) 3538789Sahrens { 3539789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3540789Sahrens return (0); 3541789Sahrens } 3542789Sahrens 35432688Smaybee /* 35442688Smaybee * Push a page out to disk, klustering if possible. 35452688Smaybee * 35462688Smaybee * IN: vp - file to push page to. 35472688Smaybee * pp - page to push. 35482688Smaybee * flags - additional flags. 35492688Smaybee * cr - credentials of caller. 35502688Smaybee * 35512688Smaybee * OUT: offp - start of range pushed. 35522688Smaybee * lenp - len of range pushed. 35532688Smaybee * 35542688Smaybee * RETURN: 0 if success 35552688Smaybee * error code if failure 35562688Smaybee * 35572688Smaybee * NOTE: callers must have locked the page to be pushed. On 35582688Smaybee * exit, the page (and all other pages in the kluster) must be 35592688Smaybee * unlocked. 35602688Smaybee */ 3561789Sahrens /* ARGSUSED */ 3562789Sahrens static int 3563789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3564789Sahrens size_t *lenp, int flags, cred_t *cr) 3565789Sahrens { 3566789Sahrens znode_t *zp = VTOZ(vp); 3567789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3568789Sahrens zilog_t *zilog = zfsvfs->z_log; 3569789Sahrens dmu_tx_t *tx; 35701669Sperrin rl_t *rl; 35712688Smaybee u_offset_t off, koff; 35722688Smaybee size_t len, klen; 35734709Smaybee uint64_t filesz; 3574789Sahrens int err; 3575789Sahrens 35764709Smaybee filesz = zp->z_phys->zp_size; 35772688Smaybee off = pp->p_offset; 35782688Smaybee len = PAGESIZE; 35792688Smaybee /* 35802688Smaybee * If our blocksize is bigger than the page size, try to kluster 35812688Smaybee * muiltiple pages so that we write a full block (thus avoiding 35822688Smaybee * a read-modify-write). 35832688Smaybee */ 35844709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 35852688Smaybee if (!ISP2(zp->z_blksz)) { 35862688Smaybee /* Only one block in the file. */ 35872688Smaybee klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 35882688Smaybee koff = 0; 35892688Smaybee } else { 35902688Smaybee klen = zp->z_blksz; 35912688Smaybee koff = P2ALIGN(off, (u_offset_t)klen); 35922688Smaybee } 35932688Smaybee ASSERT(koff <= filesz); 35942688Smaybee if (koff + klen > filesz) 35952688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 35962688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 35972688Smaybee } 35982688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 3599789Sahrens top: 36002688Smaybee rl = zfs_range_lock(zp, off, len, RL_WRITER); 36011819Smaybee /* 36021819Smaybee * Can't push pages past end-of-file. 36031819Smaybee */ 36044709Smaybee filesz = zp->z_phys->zp_size; 36054709Smaybee if (off >= filesz) { 36064709Smaybee /* ignore all pages */ 36072688Smaybee err = 0; 36082688Smaybee goto out; 36094709Smaybee } else if (off + len > filesz) { 36104709Smaybee int npages = btopr(filesz - off); 36112688Smaybee page_t *trunc; 36122688Smaybee 36132688Smaybee page_list_break(&pp, &trunc, npages); 36144709Smaybee /* ignore pages past end of file */ 36152688Smaybee if (trunc) 36164709Smaybee pvn_write_done(trunc, flags); 36174709Smaybee len = filesz - off; 36181819Smaybee } 3619789Sahrens 3620789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3621789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3622789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3623789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 3624789Sahrens if (err != 0) { 3625789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 36262688Smaybee zfs_range_unlock(rl); 36272113Sahrens dmu_tx_wait(tx); 36282113Sahrens dmu_tx_abort(tx); 36292688Smaybee err = 0; 3630789Sahrens goto top; 3631789Sahrens } 36322113Sahrens dmu_tx_abort(tx); 3633789Sahrens goto out; 3634789Sahrens } 3635789Sahrens 36362688Smaybee if (zp->z_blksz <= PAGESIZE) { 3637*7315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 36382688Smaybee ASSERT3U(len, <=, PAGESIZE); 36392688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 3640*7315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 36412688Smaybee } else { 36422688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 36432688Smaybee } 36442688Smaybee 36452688Smaybee if (err == 0) { 36462688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 36473638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0); 36482688Smaybee dmu_tx_commit(tx); 36492688Smaybee } 36502688Smaybee 36512688Smaybee out: 36522237Smaybee zfs_range_unlock(rl); 36534709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3654789Sahrens if (offp) 3655789Sahrens *offp = off; 3656789Sahrens if (lenp) 3657789Sahrens *lenp = len; 3658789Sahrens 3659789Sahrens return (err); 3660789Sahrens } 3661789Sahrens 3662789Sahrens /* 3663789Sahrens * Copy the portion of the file indicated from pages into the file. 3664789Sahrens * The pages are stored in a page list attached to the files vnode. 3665789Sahrens * 3666789Sahrens * IN: vp - vnode of file to push page data to. 3667789Sahrens * off - position in file to put data. 3668789Sahrens * len - amount of data to write. 3669789Sahrens * flags - flags to control the operation. 3670789Sahrens * cr - credentials of caller. 36715331Samw * ct - caller context. 3672789Sahrens * 3673789Sahrens * RETURN: 0 if success 3674789Sahrens * error code if failure 3675789Sahrens * 3676789Sahrens * Timestamps: 3677789Sahrens * vp - ctime|mtime updated 3678789Sahrens */ 36795331Samw /*ARGSUSED*/ 3680789Sahrens static int 36815331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 36825331Samw caller_context_t *ct) 3683789Sahrens { 3684789Sahrens znode_t *zp = VTOZ(vp); 3685789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3686789Sahrens page_t *pp; 3687789Sahrens size_t io_len; 3688789Sahrens u_offset_t io_off; 36891669Sperrin uint64_t filesz; 3690789Sahrens int error = 0; 3691789Sahrens 36925367Sahrens ZFS_ENTER(zfsvfs); 36935367Sahrens ZFS_VERIFY_ZP(zp); 3694789Sahrens 3695789Sahrens if (len == 0) { 3696789Sahrens /* 3697789Sahrens * Search the entire vp list for pages >= off. 3698789Sahrens */ 3699789Sahrens error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 3700789Sahrens flags, cr); 37011472Sperrin goto out; 3702789Sahrens } 3703789Sahrens 37041669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 37051669Sperrin if (off > filesz) { 3706789Sahrens /* past end of file */ 3707789Sahrens ZFS_EXIT(zfsvfs); 3708789Sahrens return (0); 3709789Sahrens } 3710789Sahrens 37111669Sperrin len = MIN(len, filesz - off); 3712789Sahrens 37131472Sperrin for (io_off = off; io_off < off + len; io_off += io_len) { 3714789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 37151669Sperrin pp = page_lookup(vp, io_off, 37164339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3717789Sahrens } else { 3718789Sahrens pp = page_lookup_nowait(vp, io_off, 37194339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3720789Sahrens } 3721789Sahrens 3722789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3723789Sahrens int err; 3724789Sahrens 3725789Sahrens /* 3726789Sahrens * Found a dirty page to push 3727789Sahrens */ 37281669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 37291669Sperrin if (err) 3730789Sahrens error = err; 3731789Sahrens } else { 3732789Sahrens io_len = PAGESIZE; 3733789Sahrens } 3734789Sahrens } 37351472Sperrin out: 37362638Sperrin if ((flags & B_ASYNC) == 0) 37372638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3738789Sahrens ZFS_EXIT(zfsvfs); 3739789Sahrens return (error); 3740789Sahrens } 3741789Sahrens 37425331Samw /*ARGSUSED*/ 3743789Sahrens void 37445331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3745789Sahrens { 3746789Sahrens znode_t *zp = VTOZ(vp); 3747789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3748789Sahrens int error; 3749789Sahrens 37505326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 37515446Sahrens if (zp->z_dbuf == NULL) { 37525446Sahrens /* 37535642Smaybee * The fs has been unmounted, or we did a 37545642Smaybee * suspend/resume and this file no longer exists. 37555446Sahrens */ 3756789Sahrens if (vn_has_cached_data(vp)) { 3757789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3758789Sahrens B_INVAL, cr); 3759789Sahrens } 3760789Sahrens 37611544Seschrock mutex_enter(&zp->z_lock); 3762789Sahrens vp->v_count = 0; /* count arrives as 1 */ 37635446Sahrens mutex_exit(&zp->z_lock); 37645642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 37655446Sahrens zfs_znode_free(zp); 3766789Sahrens return; 3767789Sahrens } 3768789Sahrens 3769789Sahrens /* 3770789Sahrens * Attempt to push any data in the page cache. If this fails 3771789Sahrens * we will get kicked out later in zfs_zinactive(). 3772789Sahrens */ 37731298Sperrin if (vn_has_cached_data(vp)) { 37741298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 37751298Sperrin cr); 37761298Sperrin } 3777789Sahrens 37783461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3779789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3780789Sahrens 3781789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3782789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3783789Sahrens if (error) { 3784789Sahrens dmu_tx_abort(tx); 3785789Sahrens } else { 3786789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3787789Sahrens mutex_enter(&zp->z_lock); 3788789Sahrens zp->z_atime_dirty = 0; 3789789Sahrens mutex_exit(&zp->z_lock); 3790789Sahrens dmu_tx_commit(tx); 3791789Sahrens } 3792789Sahrens } 3793789Sahrens 3794789Sahrens zfs_zinactive(zp); 37955326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3796789Sahrens } 3797789Sahrens 3798789Sahrens /* 3799789Sahrens * Bounds-check the seek operation. 3800789Sahrens * 3801789Sahrens * IN: vp - vnode seeking within 3802789Sahrens * ooff - old file offset 3803789Sahrens * noffp - pointer to new file offset 38045331Samw * ct - caller context 3805789Sahrens * 3806789Sahrens * RETURN: 0 if success 3807789Sahrens * EINVAL if new offset invalid 3808789Sahrens */ 3809789Sahrens /* ARGSUSED */ 3810789Sahrens static int 38115331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 38125331Samw caller_context_t *ct) 3813789Sahrens { 3814789Sahrens if (vp->v_type == VDIR) 3815789Sahrens return (0); 3816789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3817789Sahrens } 3818789Sahrens 3819789Sahrens /* 3820789Sahrens * Pre-filter the generic locking function to trap attempts to place 3821789Sahrens * a mandatory lock on a memory mapped file. 3822789Sahrens */ 3823789Sahrens static int 3824789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 38255331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 3826789Sahrens { 3827789Sahrens znode_t *zp = VTOZ(vp); 3828789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3829789Sahrens int error; 3830789Sahrens 38315367Sahrens ZFS_ENTER(zfsvfs); 38325367Sahrens ZFS_VERIFY_ZP(zp); 3833789Sahrens 3834789Sahrens /* 38351544Seschrock * We are following the UFS semantics with respect to mapcnt 38361544Seschrock * here: If we see that the file is mapped already, then we will 38371544Seschrock * return an error, but we don't worry about races between this 38381544Seschrock * function and zfs_map(). 3839789Sahrens */ 38401544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3841789Sahrens ZFS_EXIT(zfsvfs); 3842789Sahrens return (EAGAIN); 3843789Sahrens } 38445331Samw error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct); 3845789Sahrens ZFS_EXIT(zfsvfs); 3846789Sahrens return (error); 3847789Sahrens } 3848789Sahrens 3849789Sahrens /* 3850789Sahrens * If we can't find a page in the cache, we will create a new page 3851789Sahrens * and fill it with file data. For efficiency, we may try to fill 38521669Sperrin * multiple pages at once (klustering). 3853789Sahrens */ 3854789Sahrens static int 3855789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3856789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3857789Sahrens { 3858789Sahrens znode_t *zp = VTOZ(vp); 3859789Sahrens page_t *pp, *cur_pp; 3860789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3861789Sahrens caddr_t va; 3862789Sahrens u_offset_t io_off, total; 3863789Sahrens uint64_t oid = zp->z_id; 3864789Sahrens size_t io_len; 38651669Sperrin uint64_t filesz; 3866789Sahrens int err; 3867789Sahrens 3868789Sahrens /* 3869789Sahrens * If we are only asking for a single page don't bother klustering. 3870789Sahrens */ 38711669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 38722688Smaybee if (off >= filesz) 38732688Smaybee return (EFAULT); 38742688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3875789Sahrens io_off = off; 3876789Sahrens io_len = PAGESIZE; 3877789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3878789Sahrens } else { 3879789Sahrens /* 3880789Sahrens * Try to fill a kluster of pages (a blocks worth). 3881789Sahrens */ 3882789Sahrens size_t klen; 3883789Sahrens u_offset_t koff; 3884789Sahrens 3885789Sahrens if (!ISP2(zp->z_blksz)) { 3886789Sahrens /* Only one block in the file. */ 3887789Sahrens klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3888789Sahrens koff = 0; 3889789Sahrens } else { 38903131Sgw25295 /* 38913131Sgw25295 * It would be ideal to align our offset to the 38923131Sgw25295 * blocksize but doing so has resulted in some 38933131Sgw25295 * strange application crashes. For now, we 38943131Sgw25295 * leave the offset as is and only adjust the 38953131Sgw25295 * length if we are off the end of the file. 38963131Sgw25295 */ 38973131Sgw25295 koff = off; 3898789Sahrens klen = plsz; 3899789Sahrens } 39001819Smaybee ASSERT(koff <= filesz); 39011819Smaybee if (koff + klen > filesz) 39021819Smaybee klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 39032688Smaybee ASSERT3U(off, >=, koff); 39042688Smaybee ASSERT3U(off, <, koff + klen); 3905789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 39064339Sperrin &io_len, koff, klen, 0); 3907789Sahrens } 3908789Sahrens if (pp == NULL) { 3909789Sahrens /* 3910789Sahrens * Some other thread entered the page before us. 3911789Sahrens * Return to zfs_getpage to retry the lookup. 3912789Sahrens */ 3913789Sahrens *pl = NULL; 3914789Sahrens return (0); 3915789Sahrens } 3916789Sahrens 3917789Sahrens /* 3918789Sahrens * Fill the pages in the kluster. 3919789Sahrens */ 3920789Sahrens cur_pp = pp; 3921789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 39222688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 3923*7315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 39241544Seschrock err = dmu_read(os, oid, io_off, PAGESIZE, va); 3925*7315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 3926789Sahrens if (err) { 3927789Sahrens /* On error, toss the entire kluster */ 3928789Sahrens pvn_read_done(pp, B_ERROR); 39297294Sperrin /* convert checksum errors into IO errors */ 39307294Sperrin if (err == ECKSUM) 39317294Sperrin err = EIO; 3932789Sahrens return (err); 3933789Sahrens } 3934789Sahrens cur_pp = cur_pp->p_next; 3935789Sahrens } 3936789Sahrens out: 3937789Sahrens /* 3938789Sahrens * Fill in the page list array from the kluster. If 3939789Sahrens * there are too many pages in the kluster, return 3940789Sahrens * as many pages as possible starting from the desired 3941789Sahrens * offset `off'. 3942789Sahrens * NOTE: the page list will always be null terminated. 3943789Sahrens */ 3944789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3945789Sahrens 3946789Sahrens return (0); 3947789Sahrens } 3948789Sahrens 3949789Sahrens /* 3950789Sahrens * Return pointers to the pages for the file region [off, off + len] 3951789Sahrens * in the pl array. If plsz is greater than len, this function may 3952789Sahrens * also return page pointers from before or after the specified 3953789Sahrens * region (i.e. some region [off', off' + plsz]). These additional 3954789Sahrens * pages are only returned if they are already in the cache, or were 3955789Sahrens * created as part of a klustered read. 3956789Sahrens * 3957789Sahrens * IN: vp - vnode of file to get data from. 3958789Sahrens * off - position in file to get data from. 3959789Sahrens * len - amount of data to retrieve. 3960789Sahrens * plsz - length of provided page list. 3961789Sahrens * seg - segment to obtain pages for. 3962789Sahrens * addr - virtual address of fault. 3963789Sahrens * rw - mode of created pages. 3964789Sahrens * cr - credentials of caller. 39655331Samw * ct - caller context. 3966789Sahrens * 3967789Sahrens * OUT: protp - protection mode of created pages. 3968789Sahrens * pl - list of pages created. 3969789Sahrens * 3970789Sahrens * RETURN: 0 if success 3971789Sahrens * error code if failure 3972789Sahrens * 3973789Sahrens * Timestamps: 3974789Sahrens * vp - atime updated 3975789Sahrens */ 3976789Sahrens /* ARGSUSED */ 3977789Sahrens static int 3978789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3979789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 39805331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 3981789Sahrens { 3982789Sahrens znode_t *zp = VTOZ(vp); 3983789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3984789Sahrens page_t *pp, **pl0 = pl; 39852752Sperrin int need_unlock = 0, err = 0; 39862752Sperrin offset_t orig_off; 3987789Sahrens 39885367Sahrens ZFS_ENTER(zfsvfs); 39895367Sahrens ZFS_VERIFY_ZP(zp); 3990789Sahrens 3991789Sahrens if (protp) 3992789Sahrens *protp = PROT_ALL; 3993789Sahrens 3994789Sahrens /* no faultahead (for now) */ 3995789Sahrens if (pl == NULL) { 3996789Sahrens ZFS_EXIT(zfsvfs); 3997789Sahrens return (0); 3998789Sahrens } 3999789Sahrens 4000789Sahrens /* can't fault past EOF */ 4001789Sahrens if (off >= zp->z_phys->zp_size) { 4002789Sahrens ZFS_EXIT(zfsvfs); 4003789Sahrens return (EFAULT); 4004789Sahrens } 40052752Sperrin orig_off = off; 4006789Sahrens 4007789Sahrens /* 4008789Sahrens * If we already own the lock, then we must be page faulting 4009789Sahrens * in the middle of a write to this file (i.e., we are writing 4010789Sahrens * to this file using data from a mapped region of the file). 4011789Sahrens */ 40122752Sperrin if (rw_owner(&zp->z_map_lock) != curthread) { 4013789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 4014789Sahrens need_unlock = TRUE; 4015789Sahrens } 4016789Sahrens 4017789Sahrens /* 4018789Sahrens * Loop through the requested range [off, off + len] looking 4019789Sahrens * for pages. If we don't find a page, we will need to create 4020789Sahrens * a new page and fill it with data from the file. 4021789Sahrens */ 4022789Sahrens while (len > 0) { 4023789Sahrens if (plsz < PAGESIZE) 4024789Sahrens break; 4025789Sahrens if (pp = page_lookup(vp, off, SE_SHARED)) { 4026789Sahrens *pl++ = pp; 4027789Sahrens off += PAGESIZE; 4028789Sahrens addr += PAGESIZE; 4029789Sahrens len -= PAGESIZE; 4030789Sahrens plsz -= PAGESIZE; 4031789Sahrens } else { 4032789Sahrens err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 40332752Sperrin if (err) 40342752Sperrin goto out; 4035789Sahrens /* 4036789Sahrens * klustering may have changed our region 4037789Sahrens * to be block aligned. 4038789Sahrens */ 4039789Sahrens if (((pp = *pl) != 0) && (off != pp->p_offset)) { 4040789Sahrens int delta = off - pp->p_offset; 4041789Sahrens len += delta; 4042789Sahrens off -= delta; 4043789Sahrens addr -= delta; 4044789Sahrens } 4045789Sahrens while (*pl) { 4046789Sahrens pl++; 4047789Sahrens off += PAGESIZE; 4048789Sahrens addr += PAGESIZE; 4049789Sahrens plsz -= PAGESIZE; 4050789Sahrens if (len > PAGESIZE) 4051789Sahrens len -= PAGESIZE; 4052789Sahrens else 4053789Sahrens len = 0; 4054789Sahrens } 4055789Sahrens } 4056789Sahrens } 4057789Sahrens 4058789Sahrens /* 4059789Sahrens * Fill out the page array with any pages already in the cache. 4060789Sahrens */ 4061789Sahrens while (plsz > 0) { 4062789Sahrens pp = page_lookup_nowait(vp, off, SE_SHARED); 4063789Sahrens if (pp == NULL) 4064789Sahrens break; 4065789Sahrens *pl++ = pp; 4066789Sahrens off += PAGESIZE; 4067789Sahrens plsz -= PAGESIZE; 4068789Sahrens } 4069789Sahrens 4070789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 4071789Sahrens out: 40722752Sperrin /* 40732752Sperrin * We can't grab the range lock for the page as reader which would 40742752Sperrin * stop truncation as this leads to deadlock. So we need to recheck 40752752Sperrin * the file size. 40762752Sperrin */ 40772752Sperrin if (orig_off >= zp->z_phys->zp_size) 40782752Sperrin err = EFAULT; 40792752Sperrin if (err) { 40802752Sperrin /* 40812752Sperrin * Release any pages we have previously locked. 40822752Sperrin */ 40832752Sperrin while (pl > pl0) 40842752Sperrin page_unlock(*--pl); 40852752Sperrin } 40862752Sperrin 4087789Sahrens *pl = NULL; 4088789Sahrens 4089789Sahrens if (need_unlock) 4090789Sahrens rw_exit(&zp->z_map_lock); 4091789Sahrens 4092789Sahrens ZFS_EXIT(zfsvfs); 4093789Sahrens return (err); 4094789Sahrens } 4095789Sahrens 40961544Seschrock /* 40971544Seschrock * Request a memory map for a section of a file. This code interacts 40981544Seschrock * with common code and the VM system as follows: 40991544Seschrock * 41001544Seschrock * common code calls mmap(), which ends up in smmap_common() 41011544Seschrock * 41021544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 41031544Seschrock * 41041544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 41051544Seschrock * 41061544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 41071544Seschrock * 41081544Seschrock * zfs_addmap() updates z_mapcnt 41091544Seschrock */ 41105331Samw /*ARGSUSED*/ 4111789Sahrens static int 4112789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 41135331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 41145331Samw caller_context_t *ct) 4115789Sahrens { 4116789Sahrens znode_t *zp = VTOZ(vp); 4117789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4118789Sahrens segvn_crargs_t vn_a; 4119789Sahrens int error; 4120789Sahrens 41215929Smarks ZFS_ENTER(zfsvfs); 41225929Smarks ZFS_VERIFY_ZP(zp); 41235929Smarks 41245331Samw if ((prot & PROT_WRITE) && 41255331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 41265929Smarks ZFS_APPENDONLY))) { 41275929Smarks ZFS_EXIT(zfsvfs); 41285331Samw return (EPERM); 41295929Smarks } 41305929Smarks 41315929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 41325929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 41335929Smarks ZFS_EXIT(zfsvfs); 41345929Smarks return (EACCES); 41355929Smarks } 4136789Sahrens 4137789Sahrens if (vp->v_flag & VNOMAP) { 4138789Sahrens ZFS_EXIT(zfsvfs); 4139789Sahrens return (ENOSYS); 4140789Sahrens } 4141789Sahrens 4142789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4143789Sahrens ZFS_EXIT(zfsvfs); 4144789Sahrens return (ENXIO); 4145789Sahrens } 4146789Sahrens 4147789Sahrens if (vp->v_type != VREG) { 4148789Sahrens ZFS_EXIT(zfsvfs); 4149789Sahrens return (ENODEV); 4150789Sahrens } 4151789Sahrens 4152789Sahrens /* 4153789Sahrens * If file is locked, disallow mapping. 4154789Sahrens */ 41551544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 41561544Seschrock ZFS_EXIT(zfsvfs); 41571544Seschrock return (EAGAIN); 4158789Sahrens } 4159789Sahrens 4160789Sahrens as_rangelock(as); 41616036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 41626036Smec if (error != 0) { 41636036Smec as_rangeunlock(as); 41646036Smec ZFS_EXIT(zfsvfs); 41656036Smec return (error); 4166789Sahrens } 4167789Sahrens 4168789Sahrens vn_a.vp = vp; 4169789Sahrens vn_a.offset = (u_offset_t)off; 4170789Sahrens vn_a.type = flags & MAP_TYPE; 4171789Sahrens vn_a.prot = prot; 4172789Sahrens vn_a.maxprot = maxprot; 4173789Sahrens vn_a.cred = cr; 4174789Sahrens vn_a.amp = NULL; 4175789Sahrens vn_a.flags = flags & ~MAP_TYPE; 41761417Skchow vn_a.szc = 0; 41771417Skchow vn_a.lgrp_mem_policy_flags = 0; 4178789Sahrens 4179789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4180789Sahrens 4181789Sahrens as_rangeunlock(as); 4182789Sahrens ZFS_EXIT(zfsvfs); 4183789Sahrens return (error); 4184789Sahrens } 4185789Sahrens 4186789Sahrens /* ARGSUSED */ 4187789Sahrens static int 4188789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41895331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 41905331Samw caller_context_t *ct) 4191789Sahrens { 41921544Seschrock uint64_t pages = btopr(len); 41931544Seschrock 41941544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4195789Sahrens return (0); 4196789Sahrens } 4197789Sahrens 41981773Seschrock /* 41991773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 42001773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 42011773Seschrock * detecting when the data was actually modified, we have to resort to 42021773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 42031773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 42041773Seschrock * which by far the most common case: 42051773Seschrock * 42061773Seschrock * open() 42071773Seschrock * mmap() 42081773Seschrock * <modify memory> 42091773Seschrock * munmap() 42101773Seschrock * close() 42111773Seschrock * <time lapse> 42121773Seschrock * putpage() via fsflush 42131773Seschrock * 42141773Seschrock * If we wait until fsflush to come along, we can have a modification time that 42151773Seschrock * is some arbitrary point in the future. In order to prevent this in the 42161773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 42171773Seschrock * torn down. 42181773Seschrock */ 4219789Sahrens /* ARGSUSED */ 4220789Sahrens static int 4221789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 42225331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 42235331Samw caller_context_t *ct) 4224789Sahrens { 42251544Seschrock uint64_t pages = btopr(len); 42261544Seschrock 42271544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 42281544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 42291773Seschrock 42301773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 42311773Seschrock vn_has_cached_data(vp)) 42325331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 42331773Seschrock 4234789Sahrens return (0); 4235789Sahrens } 4236789Sahrens 4237789Sahrens /* 4238789Sahrens * Free or allocate space in a file. Currently, this function only 4239789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4240789Sahrens * misnamed, as its functionality includes the ability to allocate as 4241789Sahrens * well as free space. 4242789Sahrens * 4243789Sahrens * IN: vp - vnode of file to free data in. 4244789Sahrens * cmd - action to take (only F_FREESP supported). 4245789Sahrens * bfp - section of file to free/alloc. 4246789Sahrens * flag - current file open mode flags. 4247789Sahrens * offset - current file offset. 4248789Sahrens * cr - credentials of caller [UNUSED]. 42495331Samw * ct - caller context. 4250789Sahrens * 4251789Sahrens * RETURN: 0 if success 4252789Sahrens * error code if failure 4253789Sahrens * 4254789Sahrens * Timestamps: 4255789Sahrens * vp - ctime|mtime updated 4256789Sahrens */ 4257789Sahrens /* ARGSUSED */ 4258789Sahrens static int 4259789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4260789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4261789Sahrens { 4262789Sahrens znode_t *zp = VTOZ(vp); 4263789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4264789Sahrens uint64_t off, len; 4265789Sahrens int error; 4266789Sahrens 42675367Sahrens ZFS_ENTER(zfsvfs); 42685367Sahrens ZFS_VERIFY_ZP(zp); 4269789Sahrens 4270789Sahrens if (cmd != F_FREESP) { 4271789Sahrens ZFS_EXIT(zfsvfs); 4272789Sahrens return (EINVAL); 4273789Sahrens } 4274789Sahrens 4275789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4276789Sahrens ZFS_EXIT(zfsvfs); 4277789Sahrens return (error); 4278789Sahrens } 4279789Sahrens 4280789Sahrens if (bfp->l_len < 0) { 4281789Sahrens ZFS_EXIT(zfsvfs); 4282789Sahrens return (EINVAL); 4283789Sahrens } 4284789Sahrens 4285789Sahrens off = bfp->l_start; 42861669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 42871878Smaybee 42886992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4289789Sahrens 4290789Sahrens ZFS_EXIT(zfsvfs); 4291789Sahrens return (error); 4292789Sahrens } 4293789Sahrens 42945331Samw /*ARGSUSED*/ 4295789Sahrens static int 42965331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4297789Sahrens { 4298789Sahrens znode_t *zp = VTOZ(vp); 4299789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 43005326Sek110237 uint32_t gen; 4301789Sahrens uint64_t object = zp->z_id; 4302789Sahrens zfid_short_t *zfid; 4303789Sahrens int size, i; 4304789Sahrens 43055367Sahrens ZFS_ENTER(zfsvfs); 43065367Sahrens ZFS_VERIFY_ZP(zp); 43075326Sek110237 gen = (uint32_t)zp->z_gen; 4308789Sahrens 4309789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4310789Sahrens if (fidp->fid_len < size) { 4311789Sahrens fidp->fid_len = size; 43121512Sek110237 ZFS_EXIT(zfsvfs); 4313789Sahrens return (ENOSPC); 4314789Sahrens } 4315789Sahrens 4316789Sahrens zfid = (zfid_short_t *)fidp; 4317789Sahrens 4318789Sahrens zfid->zf_len = size; 4319789Sahrens 4320789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4321789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4322789Sahrens 4323789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4324789Sahrens if (gen == 0) 4325789Sahrens gen = 1; 4326789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4327789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4328789Sahrens 4329789Sahrens if (size == LONG_FID_LEN) { 4330789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4331789Sahrens zfid_long_t *zlfid; 4332789Sahrens 4333789Sahrens zlfid = (zfid_long_t *)fidp; 4334789Sahrens 4335789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4336789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4337789Sahrens 4338789Sahrens /* XXX - this should be the generation number for the objset */ 4339789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4340789Sahrens zlfid->zf_setgen[i] = 0; 4341789Sahrens } 4342789Sahrens 4343789Sahrens ZFS_EXIT(zfsvfs); 4344789Sahrens return (0); 4345789Sahrens } 4346789Sahrens 4347789Sahrens static int 43485331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 43495331Samw caller_context_t *ct) 4350789Sahrens { 4351789Sahrens znode_t *zp, *xzp; 4352789Sahrens zfsvfs_t *zfsvfs; 4353789Sahrens zfs_dirlock_t *dl; 4354789Sahrens int error; 4355789Sahrens 4356789Sahrens switch (cmd) { 4357789Sahrens case _PC_LINK_MAX: 4358789Sahrens *valp = ULONG_MAX; 4359789Sahrens return (0); 4360789Sahrens 4361789Sahrens case _PC_FILESIZEBITS: 4362789Sahrens *valp = 64; 4363789Sahrens return (0); 4364789Sahrens 4365789Sahrens case _PC_XATTR_EXISTS: 4366789Sahrens zp = VTOZ(vp); 4367789Sahrens zfsvfs = zp->z_zfsvfs; 43685367Sahrens ZFS_ENTER(zfsvfs); 43695367Sahrens ZFS_VERIFY_ZP(zp); 4370789Sahrens *valp = 0; 4371789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 43725331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4373789Sahrens if (error == 0) { 4374789Sahrens zfs_dirent_unlock(dl); 4375789Sahrens if (!zfs_dirempty(xzp)) 4376789Sahrens *valp = 1; 4377789Sahrens VN_RELE(ZTOV(xzp)); 4378789Sahrens } else if (error == ENOENT) { 4379789Sahrens /* 4380789Sahrens * If there aren't extended attributes, it's the 4381789Sahrens * same as having zero of them. 4382789Sahrens */ 4383789Sahrens error = 0; 4384789Sahrens } 4385789Sahrens ZFS_EXIT(zfsvfs); 4386789Sahrens return (error); 4387789Sahrens 43885331Samw case _PC_SATTR_ENABLED: 43895331Samw case _PC_SATTR_EXISTS: 43905331Samw *valp = vfs_has_feature(vp->v_vfsp, VFSFT_XVATTR) && 43915331Samw (vp->v_type == VREG || vp->v_type == VDIR); 43925331Samw return (0); 43935331Samw 4394789Sahrens case _PC_ACL_ENABLED: 4395789Sahrens *valp = _ACL_ACE_ENABLED; 4396789Sahrens return (0); 4397789Sahrens 4398789Sahrens case _PC_MIN_HOLE_SIZE: 4399789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4400789Sahrens return (0); 4401789Sahrens 4402789Sahrens default: 44035331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4404789Sahrens } 4405789Sahrens } 4406789Sahrens 4407789Sahrens /*ARGSUSED*/ 4408789Sahrens static int 44095331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 44105331Samw caller_context_t *ct) 4411789Sahrens { 4412789Sahrens znode_t *zp = VTOZ(vp); 4413789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4414789Sahrens int error; 44155331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4416789Sahrens 44175367Sahrens ZFS_ENTER(zfsvfs); 44185367Sahrens ZFS_VERIFY_ZP(zp); 44195331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4420789Sahrens ZFS_EXIT(zfsvfs); 4421789Sahrens 4422789Sahrens return (error); 4423789Sahrens } 4424789Sahrens 4425789Sahrens /*ARGSUSED*/ 4426789Sahrens static int 44275331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 44285331Samw caller_context_t *ct) 4429789Sahrens { 4430789Sahrens znode_t *zp = VTOZ(vp); 4431789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4432789Sahrens int error; 44335331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4434789Sahrens 44355367Sahrens ZFS_ENTER(zfsvfs); 44365367Sahrens ZFS_VERIFY_ZP(zp); 44375331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4438789Sahrens ZFS_EXIT(zfsvfs); 4439789Sahrens return (error); 4440789Sahrens } 4441789Sahrens 4442789Sahrens /* 4443789Sahrens * Predeclare these here so that the compiler assumes that 4444789Sahrens * this is an "old style" function declaration that does 4445789Sahrens * not include arguments => we won't get type mismatch errors 4446789Sahrens * in the initializations that follow. 4447789Sahrens */ 4448789Sahrens static int zfs_inval(); 4449789Sahrens static int zfs_isdir(); 4450789Sahrens 4451789Sahrens static int 4452789Sahrens zfs_inval() 4453789Sahrens { 4454789Sahrens return (EINVAL); 4455789Sahrens } 4456789Sahrens 4457789Sahrens static int 4458789Sahrens zfs_isdir() 4459789Sahrens { 4460789Sahrens return (EISDIR); 4461789Sahrens } 4462789Sahrens /* 4463789Sahrens * Directory vnode operations template 4464789Sahrens */ 4465789Sahrens vnodeops_t *zfs_dvnodeops; 4466789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 44673898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44683898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44693898Srsb VOPNAME_READ, { .error = zfs_isdir }, 44703898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 44713898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44723898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44733898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44743898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44753898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44763898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 44773898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 44783898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 44793898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44803898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 44813898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 44823898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 44833898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 44843898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44853898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44863898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44873898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44883898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44893898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44903898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44914863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44923898Srsb NULL, NULL 4493789Sahrens }; 4494789Sahrens 4495789Sahrens /* 4496789Sahrens * Regular file vnode operations template 4497789Sahrens */ 4498789Sahrens vnodeops_t *zfs_fvnodeops; 4499789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 45003898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45013898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45023898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 45033898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 45043898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45053898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45063898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45073898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45083898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45093898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45103898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45113898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45123898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45133898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45143898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 45153898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 45163898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 45173898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 45183898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 45193898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 45203898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 45213898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45223898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45233898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45243898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45253898Srsb NULL, NULL 4526789Sahrens }; 4527789Sahrens 4528789Sahrens /* 4529789Sahrens * Symbolic link vnode operations template 4530789Sahrens */ 4531789Sahrens vnodeops_t *zfs_symvnodeops; 4532789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 45333898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45343898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45353898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45363898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45373898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 45383898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45393898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45403898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45413898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45423898Srsb NULL, NULL 4543789Sahrens }; 4544789Sahrens 4545789Sahrens /* 4546789Sahrens * Extended attribute directory vnode operations template 4547789Sahrens * This template is identical to the directory vnodes 4548789Sahrens * operation template except for restricted operations: 4549789Sahrens * VOP_MKDIR() 4550789Sahrens * VOP_SYMLINK() 4551789Sahrens * Note that there are other restrictions embedded in: 4552789Sahrens * zfs_create() - restrict type to VREG 4553789Sahrens * zfs_link() - no links into/out of attribute space 4554789Sahrens * zfs_rename() - no moves into/out of attribute space 4555789Sahrens */ 4556789Sahrens vnodeops_t *zfs_xdvnodeops; 4557789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 45583898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45593898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45603898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45613898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45623898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45633898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45643898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45653898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 45663898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 45673898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 45683898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45693898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 45703898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 45713898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 45723898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 45733898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45743898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45753898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45763898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45773898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45783898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45793898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45803898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45813898Srsb NULL, NULL 4582789Sahrens }; 4583789Sahrens 4584789Sahrens /* 4585789Sahrens * Error vnode operations template 4586789Sahrens */ 4587789Sahrens vnodeops_t *zfs_evnodeops; 4588789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 45893898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45903898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45913898Srsb NULL, NULL 4592789Sahrens }; 4593