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 /* 228636SMark.Maybee@Sun.COM * Copyright 2009 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> 487315SJonathan.Adams@Sun.COM #include <vm/kpm.h> 497315SJonathan.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> 687847SMark.Shellenbaum@Sun.COM #include <sys/sid.h> 69789Sahrens #include "fs/fs_subr.h" 70789Sahrens #include <sys/zfs_ctldir.h> 715331Samw #include <sys/zfs_fuid.h> 721484Sek110237 #include <sys/dnlc.h> 731669Sperrin #include <sys/zfs_rlock.h> 745331Samw #include <sys/extdirent.h> 755331Samw #include <sys/kidmap.h> 765331Samw #include <sys/cred_impl.h> 775663Sck153898 #include <sys/attr.h> 78789Sahrens 79789Sahrens /* 80789Sahrens * Programming rules. 81789Sahrens * 82789Sahrens * Each vnode op performs some logical unit of work. To do this, the ZPL must 83789Sahrens * properly lock its in-core state, create a DMU transaction, do the work, 84789Sahrens * record this work in the intent log (ZIL), commit the DMU transaction, 855331Samw * and wait for the intent log to commit if it is a synchronous operation. 865331Samw * Moreover, the vnode ops must work in both normal and log replay context. 87789Sahrens * The ordering of events is important to avoid deadlocks and references 88789Sahrens * to freed memory. The example below illustrates the following Big Rules: 89789Sahrens * 90789Sahrens * (1) A check must be made in each zfs thread for a mounted file system. 915367Sahrens * This is done avoiding races using ZFS_ENTER(zfsvfs). 925367Sahrens * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 935367Sahrens * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 945367Sahrens * can return EIO from the calling function. 95789Sahrens * 96789Sahrens * (2) VN_RELE() should always be the last thing except for zil_commit() 972638Sperrin * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 98789Sahrens * First, if it's the last reference, the vnode/znode 99789Sahrens * can be freed, so the zp may point to freed memory. Second, the last 100789Sahrens * reference will call zfs_zinactive(), which may induce a lot of work -- 1011669Sperrin * pushing cached pages (which acquires range locks) and syncing out 102789Sahrens * cached atime changes. Third, zfs_zinactive() may require a new tx, 103789Sahrens * which could deadlock the system if you were already holding one. 104789Sahrens * 1051757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 1061757Sperrin * as they can span dmu_tx_assign() calls. 1071757Sperrin * 1088227SNeil.Perrin@Sun.COM * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign(). 109789Sahrens * This is critical because we don't want to block while holding locks. 110789Sahrens * Note, in particular, that if a lock is sometimes acquired before 111789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 112789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 113789Sahrens * 114789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 115789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 116789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 117789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 118789Sahrens * 119789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1202113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 121789Sahrens * 1221757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 123789Sahrens * before dropping locks. This ensures that the ordering of events 124789Sahrens * in the intent log matches the order in which they actually occurred. 1258227SNeil.Perrin@Sun.COM * During ZIL replay the zfs_log_* functions will update the sequence 1268227SNeil.Perrin@Sun.COM * number to indicate the zil transaction has replayed. 127789Sahrens * 1281757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 129789Sahrens * regardless of whether there were any errors. 130789Sahrens * 1312638Sperrin * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 132789Sahrens * to ensure that synchronous semantics are provided when necessary. 133789Sahrens * 134789Sahrens * In general, this is how things should be ordered in each vnode op: 135789Sahrens * 136789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 137789Sahrens * top: 138789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 139789Sahrens * rw_enter(...); // grab any other locks you need 140789Sahrens * tx = dmu_tx_create(...); // get DMU tx 141789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 1428227SNeil.Perrin@Sun.COM * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign 143789Sahrens * if (error) { 144789Sahrens * rw_exit(...); // drop locks 145789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 146789Sahrens * VN_RELE(...); // release held vnodes 1478227SNeil.Perrin@Sun.COM * if (error == ERESTART) { 1482113Sahrens * dmu_tx_wait(tx); 1492113Sahrens * dmu_tx_abort(tx); 150789Sahrens * goto top; 151789Sahrens * } 1522113Sahrens * dmu_tx_abort(tx); // abort DMU tx 153789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 154789Sahrens * return (error); // really out of space 155789Sahrens * } 156789Sahrens * error = do_real_work(); // do whatever this VOP does 157789Sahrens * if (error == 0) 1582638Sperrin * zfs_log_*(...); // on success, make ZIL entry 159789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 160789Sahrens * rw_exit(...); // drop locks 161789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 162789Sahrens * VN_RELE(...); // release held vnodes 1632638Sperrin * zil_commit(zilog, seq, foid); // synchronous when necessary 164789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 165789Sahrens * return (error); // done, report error 166789Sahrens */ 1675367Sahrens 168789Sahrens /* ARGSUSED */ 169789Sahrens static int 1705331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 171789Sahrens { 1723063Sperrin znode_t *zp = VTOZ(*vpp); 1737844SMark.Shellenbaum@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1747844SMark.Shellenbaum@Sun.COM 1757844SMark.Shellenbaum@Sun.COM ZFS_ENTER(zfsvfs); 1767844SMark.Shellenbaum@Sun.COM ZFS_VERIFY_ZP(zp); 1773063Sperrin 1785331Samw if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) && 1795331Samw ((flag & FAPPEND) == 0)) { 1807844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1815331Samw return (EPERM); 1825331Samw } 1835331Samw 1845331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 1855331Samw ZTOV(zp)->v_type == VREG && 1865331Samw !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) && 1877844SMark.Shellenbaum@Sun.COM zp->z_phys->zp_size > 0) { 1887844SMark.Shellenbaum@Sun.COM if (fs_vscan(*vpp, cr, 0) != 0) { 1897844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1905331Samw return (EACCES); 1917844SMark.Shellenbaum@Sun.COM } 1927844SMark.Shellenbaum@Sun.COM } 1935331Samw 1943063Sperrin /* Keep a count of the synchronous opens in the znode */ 1953063Sperrin if (flag & (FSYNC | FDSYNC)) 1963063Sperrin atomic_inc_32(&zp->z_sync_cnt); 1975331Samw 1987844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 199789Sahrens return (0); 200789Sahrens } 201789Sahrens 202789Sahrens /* ARGSUSED */ 203789Sahrens static int 2045331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2055331Samw caller_context_t *ct) 206789Sahrens { 2073063Sperrin znode_t *zp = VTOZ(vp); 2087844SMark.Shellenbaum@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2097844SMark.Shellenbaum@Sun.COM 2107844SMark.Shellenbaum@Sun.COM ZFS_ENTER(zfsvfs); 2117844SMark.Shellenbaum@Sun.COM ZFS_VERIFY_ZP(zp); 2123063Sperrin 2133063Sperrin /* Decrement the synchronous opens in the znode */ 2144339Sperrin if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 2153063Sperrin atomic_dec_32(&zp->z_sync_cnt); 2163063Sperrin 217789Sahrens /* 218789Sahrens * Clean up any locks held by this process on the vp. 219789Sahrens */ 220789Sahrens cleanlocks(vp, ddi_get_pid(), 0); 221789Sahrens cleanshares(vp, ddi_get_pid()); 222789Sahrens 2235331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 2245331Samw ZTOV(zp)->v_type == VREG && 2255331Samw !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) && 2265331Samw zp->z_phys->zp_size > 0) 2275331Samw VERIFY(fs_vscan(vp, cr, 1) == 0); 2285331Samw 2297844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 230789Sahrens return (0); 231789Sahrens } 232789Sahrens 233789Sahrens /* 234789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 235789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 236789Sahrens */ 237789Sahrens static int 238789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 239789Sahrens { 240789Sahrens znode_t *zp = VTOZ(vp); 241789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 242789Sahrens uint64_t file_sz; 243789Sahrens int error; 244789Sahrens boolean_t hole; 245789Sahrens 246789Sahrens file_sz = zp->z_phys->zp_size; 247789Sahrens if (noff >= file_sz) { 248789Sahrens return (ENXIO); 249789Sahrens } 250789Sahrens 251789Sahrens if (cmd == _FIO_SEEK_HOLE) 252789Sahrens hole = B_TRUE; 253789Sahrens else 254789Sahrens hole = B_FALSE; 255789Sahrens 256789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 257789Sahrens 258789Sahrens /* end of file? */ 259789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 260789Sahrens /* 261789Sahrens * Handle the virtual hole at the end of file. 262789Sahrens */ 263789Sahrens if (hole) { 264789Sahrens *off = file_sz; 265789Sahrens return (0); 266789Sahrens } 267789Sahrens return (ENXIO); 268789Sahrens } 269789Sahrens 270789Sahrens if (noff < *off) 271789Sahrens return (error); 272789Sahrens *off = noff; 273789Sahrens return (error); 274789Sahrens } 275789Sahrens 276789Sahrens /* ARGSUSED */ 277789Sahrens static int 278789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 2795331Samw int *rvalp, caller_context_t *ct) 280789Sahrens { 281789Sahrens offset_t off; 282789Sahrens int error; 283789Sahrens zfsvfs_t *zfsvfs; 2845326Sek110237 znode_t *zp; 285789Sahrens 286789Sahrens switch (com) { 2874339Sperrin case _FIOFFS: 288789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 289789Sahrens 2901544Seschrock /* 2911544Seschrock * The following two ioctls are used by bfu. Faking out, 2921544Seschrock * necessary to avoid bfu errors. 2931544Seschrock */ 2944339Sperrin case _FIOGDIO: 2954339Sperrin case _FIOSDIO: 2961544Seschrock return (0); 2971544Seschrock 2984339Sperrin case _FIO_SEEK_DATA: 2994339Sperrin case _FIO_SEEK_HOLE: 300789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 301789Sahrens return (EFAULT); 302789Sahrens 3035326Sek110237 zp = VTOZ(vp); 3045326Sek110237 zfsvfs = zp->z_zfsvfs; 3055367Sahrens ZFS_ENTER(zfsvfs); 3065367Sahrens ZFS_VERIFY_ZP(zp); 307789Sahrens 308789Sahrens /* offset parameter is in/out */ 309789Sahrens error = zfs_holey(vp, com, &off); 310789Sahrens ZFS_EXIT(zfsvfs); 311789Sahrens if (error) 312789Sahrens return (error); 313789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 314789Sahrens return (EFAULT); 315789Sahrens return (0); 316789Sahrens } 317789Sahrens return (ENOTTY); 318789Sahrens } 319789Sahrens 320789Sahrens /* 3217315SJonathan.Adams@Sun.COM * Utility functions to map and unmap a single physical page. These 3227315SJonathan.Adams@Sun.COM * are used to manage the mappable copies of ZFS file data, and therefore 3237315SJonathan.Adams@Sun.COM * do not update ref/mod bits. 3247315SJonathan.Adams@Sun.COM */ 3257315SJonathan.Adams@Sun.COM caddr_t 3267315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw) 3277315SJonathan.Adams@Sun.COM { 3287315SJonathan.Adams@Sun.COM if (kpm_enable) 3297315SJonathan.Adams@Sun.COM return (hat_kpm_mapin(pp, 0)); 3307315SJonathan.Adams@Sun.COM ASSERT(rw == S_READ || rw == S_WRITE); 3317315SJonathan.Adams@Sun.COM return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0), 3327315SJonathan.Adams@Sun.COM (caddr_t)-1)); 3337315SJonathan.Adams@Sun.COM } 3347315SJonathan.Adams@Sun.COM 3357315SJonathan.Adams@Sun.COM void 3367315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr) 3377315SJonathan.Adams@Sun.COM { 3387315SJonathan.Adams@Sun.COM if (kpm_enable) { 3397315SJonathan.Adams@Sun.COM hat_kpm_mapout(pp, 0, addr); 3407315SJonathan.Adams@Sun.COM } else { 3417315SJonathan.Adams@Sun.COM ppmapout(addr); 3427315SJonathan.Adams@Sun.COM } 3437315SJonathan.Adams@Sun.COM } 3447315SJonathan.Adams@Sun.COM 3457315SJonathan.Adams@Sun.COM /* 346789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 347789Sahrens * between the DMU cache and the memory mapped pages. What this means: 348789Sahrens * 349789Sahrens * On Write: If we find a memory mapped page, we write to *both* 350789Sahrens * the page and the dmu buffer. 351789Sahrens */ 3528636SMark.Maybee@Sun.COM static void 3538636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid) 354789Sahrens { 3558636SMark.Maybee@Sun.COM int64_t off; 3568636SMark.Maybee@Sun.COM 357789Sahrens off = start & PAGEOFFSET; 358789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 359789Sahrens page_t *pp; 3608636SMark.Maybee@Sun.COM uint64_t nbytes = MIN(PAGESIZE - off, len); 3618636SMark.Maybee@Sun.COM 362789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 363789Sahrens caddr_t va; 364789Sahrens 3657315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_WRITE); 3668636SMark.Maybee@Sun.COM (void) dmu_read(os, oid, start+off, nbytes, va+off); 3677315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 368789Sahrens page_unlock(pp); 369789Sahrens } 3708636SMark.Maybee@Sun.COM len -= nbytes; 371789Sahrens off = 0; 372789Sahrens } 373789Sahrens } 374789Sahrens 375789Sahrens /* 376789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 377789Sahrens * between the DMU cache and the memory mapped pages. What this means: 378789Sahrens * 379789Sahrens * On Read: We "read" preferentially from memory mapped pages, 380789Sahrens * else we default from the dmu buffer. 381789Sahrens * 382789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 383789Sahrens * the file is memory mapped. 384789Sahrens */ 385789Sahrens static int 3863638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio) 387789Sahrens { 3883638Sbillm znode_t *zp = VTOZ(vp); 3893638Sbillm objset_t *os = zp->z_zfsvfs->z_os; 3903638Sbillm int64_t start, off; 391789Sahrens int len = nbytes; 392789Sahrens int error = 0; 393789Sahrens 394789Sahrens start = uio->uio_loffset; 395789Sahrens off = start & PAGEOFFSET; 396789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 397789Sahrens page_t *pp; 3983638Sbillm uint64_t bytes = MIN(PAGESIZE - off, len); 3993638Sbillm 400789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 401789Sahrens caddr_t va; 402789Sahrens 4037315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_READ); 404789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 4057315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 406789Sahrens page_unlock(pp); 407789Sahrens } else { 4083638Sbillm error = dmu_read_uio(os, zp->z_id, uio, bytes); 409789Sahrens } 410789Sahrens len -= bytes; 411789Sahrens off = 0; 412789Sahrens if (error) 413789Sahrens break; 414789Sahrens } 415789Sahrens return (error); 416789Sahrens } 417789Sahrens 4183638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 419789Sahrens 420789Sahrens /* 421789Sahrens * Read bytes from specified file into supplied buffer. 422789Sahrens * 423789Sahrens * IN: vp - vnode of file to be read from. 424789Sahrens * uio - structure supplying read location, range info, 425789Sahrens * and return buffer. 426789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 427789Sahrens * cr - credentials of caller. 4285331Samw * ct - caller context 429789Sahrens * 430789Sahrens * OUT: uio - updated offset and range, buffer filled. 431789Sahrens * 432789Sahrens * RETURN: 0 if success 433789Sahrens * error code if failure 434789Sahrens * 435789Sahrens * Side Effects: 436789Sahrens * vp - atime updated if byte count > 0 437789Sahrens */ 438789Sahrens /* ARGSUSED */ 439789Sahrens static int 440789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 441789Sahrens { 442789Sahrens znode_t *zp = VTOZ(vp); 443789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4445326Sek110237 objset_t *os; 4453638Sbillm ssize_t n, nbytes; 4463638Sbillm int error; 4471669Sperrin rl_t *rl; 448789Sahrens 4495367Sahrens ZFS_ENTER(zfsvfs); 4505367Sahrens ZFS_VERIFY_ZP(zp); 4515326Sek110237 os = zfsvfs->z_os; 452789Sahrens 4535929Smarks if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) { 4545929Smarks ZFS_EXIT(zfsvfs); 4555929Smarks return (EACCES); 4565929Smarks } 4575929Smarks 458789Sahrens /* 459789Sahrens * Validate file offset 460789Sahrens */ 461789Sahrens if (uio->uio_loffset < (offset_t)0) { 462789Sahrens ZFS_EXIT(zfsvfs); 463789Sahrens return (EINVAL); 464789Sahrens } 465789Sahrens 466789Sahrens /* 467789Sahrens * Fasttrack empty reads 468789Sahrens */ 469789Sahrens if (uio->uio_resid == 0) { 470789Sahrens ZFS_EXIT(zfsvfs); 471789Sahrens return (0); 472789Sahrens } 473789Sahrens 474789Sahrens /* 4751669Sperrin * Check for mandatory locks 476789Sahrens */ 477789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 478789Sahrens if (error = chklock(vp, FREAD, 479789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 480789Sahrens ZFS_EXIT(zfsvfs); 481789Sahrens return (error); 482789Sahrens } 483789Sahrens } 484789Sahrens 485789Sahrens /* 486789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 487789Sahrens */ 4882638Sperrin if (ioflag & FRSYNC) 4892638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 490789Sahrens 491789Sahrens /* 4921669Sperrin * Lock the range against changes. 493789Sahrens */ 4941669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4951669Sperrin 496789Sahrens /* 497789Sahrens * If we are reading past end-of-file we can skip 498789Sahrens * to the end; but we might still need to set atime. 499789Sahrens */ 500789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 501789Sahrens error = 0; 502789Sahrens goto out; 503789Sahrens } 504789Sahrens 5053638Sbillm ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 5063638Sbillm n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 5073638Sbillm 5083638Sbillm while (n > 0) { 5093638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 5103638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 5113638Sbillm 5123638Sbillm if (vn_has_cached_data(vp)) 5133638Sbillm error = mappedread(vp, nbytes, uio); 5143638Sbillm else 5153638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 5167294Sperrin if (error) { 5177294Sperrin /* convert checksum errors into IO errors */ 5187294Sperrin if (error == ECKSUM) 5197294Sperrin error = EIO; 5203638Sbillm break; 5217294Sperrin } 5223638Sbillm 5233638Sbillm n -= nbytes; 524789Sahrens } 5253638Sbillm 526789Sahrens out: 5272237Smaybee zfs_range_unlock(rl); 528789Sahrens 529789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 530789Sahrens ZFS_EXIT(zfsvfs); 531789Sahrens return (error); 532789Sahrens } 533789Sahrens 534789Sahrens /* 535789Sahrens * Write the bytes to a file. 536789Sahrens * 537789Sahrens * IN: vp - vnode of file to be written to. 538789Sahrens * uio - structure supplying write location, range info, 539789Sahrens * and data buffer. 540789Sahrens * ioflag - FAPPEND flag set if in append mode. 541789Sahrens * cr - credentials of caller. 5425331Samw * ct - caller context (NFS/CIFS fem monitor only) 543789Sahrens * 544789Sahrens * OUT: uio - updated offset and range. 545789Sahrens * 546789Sahrens * RETURN: 0 if success 547789Sahrens * error code if failure 548789Sahrens * 549789Sahrens * Timestamps: 550789Sahrens * vp - ctime|mtime updated if byte count > 0 551789Sahrens */ 552789Sahrens /* ARGSUSED */ 553789Sahrens static int 554789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 555789Sahrens { 556789Sahrens znode_t *zp = VTOZ(vp); 557789Sahrens rlim64_t limit = uio->uio_llimit; 558789Sahrens ssize_t start_resid = uio->uio_resid; 559789Sahrens ssize_t tx_bytes; 560789Sahrens uint64_t end_size; 561789Sahrens dmu_tx_t *tx; 562789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5635326Sek110237 zilog_t *zilog; 564789Sahrens offset_t woff; 565789Sahrens ssize_t n, nbytes; 5661669Sperrin rl_t *rl; 567789Sahrens int max_blksz = zfsvfs->z_max_blksz; 5686743Smarks uint64_t pflags; 5691669Sperrin int error; 570789Sahrens 571789Sahrens /* 572789Sahrens * Fasttrack empty write 573789Sahrens */ 5741669Sperrin n = start_resid; 575789Sahrens if (n == 0) 576789Sahrens return (0); 577789Sahrens 5781669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 5791669Sperrin limit = MAXOFFSET_T; 5801669Sperrin 5815367Sahrens ZFS_ENTER(zfsvfs); 5825367Sahrens ZFS_VERIFY_ZP(zp); 5836743Smarks 5846743Smarks /* 5856743Smarks * If immutable or not appending then return EPERM 5866743Smarks */ 5876743Smarks pflags = zp->z_phys->zp_flags; 5886743Smarks if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 5896743Smarks ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 5906743Smarks (uio->uio_loffset < zp->z_phys->zp_size))) { 5916743Smarks ZFS_EXIT(zfsvfs); 5926743Smarks return (EPERM); 5936743Smarks } 5946743Smarks 5955326Sek110237 zilog = zfsvfs->z_log; 596789Sahrens 597789Sahrens /* 5982237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 5991669Sperrin * don't hold up txg. 600789Sahrens */ 6018059SDonghai.Qiao@Sun.COM uio_prefaultpages(n, uio); 602789Sahrens 603789Sahrens /* 604789Sahrens * If in append mode, set the io offset pointer to eof. 605789Sahrens */ 6061669Sperrin if (ioflag & FAPPEND) { 6071669Sperrin /* 6081669Sperrin * Range lock for a file append: 6091669Sperrin * The value for the start of range will be determined by 6101669Sperrin * zfs_range_lock() (to guarantee append semantics). 6111669Sperrin * If this write will cause the block size to increase, 6121669Sperrin * zfs_range_lock() will lock the entire file, so we must 6131669Sperrin * later reduce the range after we grow the block size. 6141669Sperrin */ 6151669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6161669Sperrin if (rl->r_len == UINT64_MAX) { 6171669Sperrin /* overlocked, zp_size can't change */ 6181669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6191669Sperrin } else { 6201669Sperrin woff = uio->uio_loffset = rl->r_off; 6211669Sperrin } 622789Sahrens } else { 623789Sahrens woff = uio->uio_loffset; 624789Sahrens /* 625789Sahrens * Validate file offset 626789Sahrens */ 627789Sahrens if (woff < 0) { 628789Sahrens ZFS_EXIT(zfsvfs); 629789Sahrens return (EINVAL); 630789Sahrens } 631789Sahrens 632789Sahrens /* 6331669Sperrin * If we need to grow the block size then zfs_range_lock() 6341669Sperrin * will lock a wider range than we request here. 6351669Sperrin * Later after growing the block size we reduce the range. 636789Sahrens */ 6371669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 638789Sahrens } 639789Sahrens 640789Sahrens if (woff >= limit) { 6413638Sbillm zfs_range_unlock(rl); 6423638Sbillm ZFS_EXIT(zfsvfs); 6433638Sbillm return (EFBIG); 644789Sahrens } 645789Sahrens 646789Sahrens if ((woff + n) > limit || woff > (limit - n)) 647789Sahrens n = limit - woff; 648789Sahrens 649789Sahrens /* 6501669Sperrin * Check for mandatory locks 651789Sahrens */ 652789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 6533638Sbillm (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 6543638Sbillm zfs_range_unlock(rl); 6553638Sbillm ZFS_EXIT(zfsvfs); 6563638Sbillm return (error); 6573638Sbillm } 6581669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 659789Sahrens 6601669Sperrin /* 6613638Sbillm * Write the file in reasonable size chunks. Each chunk is written 6623638Sbillm * in a separate transaction; this keeps the intent log records small 6633638Sbillm * and allows us to do more fine-grained space accounting. 664789Sahrens */ 665789Sahrens while (n > 0) { 666789Sahrens /* 6673638Sbillm * Start a transaction. 668789Sahrens */ 669789Sahrens woff = uio->uio_loffset; 670789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 671789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 672789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 6738227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 674789Sahrens if (error) { 6758227SNeil.Perrin@Sun.COM if (error == ERESTART) { 6762113Sahrens dmu_tx_wait(tx); 6772113Sahrens dmu_tx_abort(tx); 6783638Sbillm continue; 679789Sahrens } 6802113Sahrens dmu_tx_abort(tx); 6813638Sbillm break; 6823638Sbillm } 6833638Sbillm 6843638Sbillm /* 6853638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 6863638Sbillm * and then reduce the lock range. This will only happen 6873638Sbillm * on the first iteration since zfs_range_reduce() will 6883638Sbillm * shrink down r_len to the appropriate size. 6893638Sbillm */ 6903638Sbillm if (rl->r_len == UINT64_MAX) { 6913638Sbillm uint64_t new_blksz; 6923638Sbillm 6933638Sbillm if (zp->z_blksz > max_blksz) { 6943638Sbillm ASSERT(!ISP2(zp->z_blksz)); 6953638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 6963638Sbillm } else { 6973638Sbillm new_blksz = MIN(end_size, max_blksz); 6983638Sbillm } 6993638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 7003638Sbillm zfs_range_reduce(rl, woff, n); 7013638Sbillm } 7023638Sbillm 7033638Sbillm /* 7043638Sbillm * XXX - should we really limit each write to z_max_blksz? 7053638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 7063638Sbillm */ 7073638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 7083638Sbillm 7093638Sbillm tx_bytes = uio->uio_resid; 7108636SMark.Maybee@Sun.COM error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio, nbytes, tx); 7113638Sbillm tx_bytes -= uio->uio_resid; 7128636SMark.Maybee@Sun.COM if (tx_bytes && vn_has_cached_data(vp)) 7138636SMark.Maybee@Sun.COM update_pages(vp, woff, 7148636SMark.Maybee@Sun.COM tx_bytes, zfsvfs->z_os, zp->z_id); 7153638Sbillm 7163638Sbillm /* 7173638Sbillm * If we made no progress, we're done. If we made even 7183638Sbillm * partial progress, update the znode and ZIL accordingly. 7193638Sbillm */ 7203638Sbillm if (tx_bytes == 0) { 7213897Smaybee dmu_tx_commit(tx); 7223638Sbillm ASSERT(error != 0); 7233638Sbillm break; 7243638Sbillm } 7253638Sbillm 726789Sahrens /* 7273638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 7283638Sbillm * privileged and at least one of the excute bits is set. 7293638Sbillm * 7303638Sbillm * It would be nice to to this after all writes have 7313638Sbillm * been done, but that would still expose the ISUID/ISGID 7323638Sbillm * to another app after the partial write is committed. 7335331Samw * 7345331Samw * Note: we don't call zfs_fuid_map_id() here because 7355331Samw * user 0 is not an ephemeral uid. 736789Sahrens */ 7373638Sbillm mutex_enter(&zp->z_acl_lock); 7383638Sbillm if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7393638Sbillm (S_IXUSR >> 6))) != 0 && 7403638Sbillm (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7413638Sbillm secpolicy_vnode_setid_retain(cr, 7423638Sbillm (zp->z_phys->zp_mode & S_ISUID) != 0 && 7433638Sbillm zp->z_phys->zp_uid == 0) != 0) { 7444339Sperrin zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 7453638Sbillm } 7463638Sbillm mutex_exit(&zp->z_acl_lock); 7473638Sbillm 7483638Sbillm /* 7493638Sbillm * Update time stamp. NOTE: This marks the bonus buffer as 7503638Sbillm * dirty, so we don't have to do it again for zp_size. 7513638Sbillm */ 7523638Sbillm zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 7533638Sbillm 7543638Sbillm /* 7553638Sbillm * Update the file size (zp_size) if it has changed; 7563638Sbillm * account for possible concurrent updates. 7573638Sbillm */ 7583638Sbillm while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 759789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 760789Sahrens uio->uio_loffset); 7613638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 7623638Sbillm dmu_tx_commit(tx); 7633638Sbillm 7643638Sbillm if (error != 0) 7653638Sbillm break; 7663638Sbillm ASSERT(tx_bytes == nbytes); 7673638Sbillm n -= nbytes; 768789Sahrens } 769789Sahrens 7702237Smaybee zfs_range_unlock(rl); 771789Sahrens 772789Sahrens /* 773789Sahrens * If we're in replay mode, or we made no progress, return error. 774789Sahrens * Otherwise, it's at least a partial write, so it's successful. 775789Sahrens */ 7768227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 777789Sahrens ZFS_EXIT(zfsvfs); 778789Sahrens return (error); 779789Sahrens } 780789Sahrens 7812638Sperrin if (ioflag & (FSYNC | FDSYNC)) 7822638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 783789Sahrens 784789Sahrens ZFS_EXIT(zfsvfs); 785789Sahrens return (0); 786789Sahrens } 787789Sahrens 7882237Smaybee void 7893063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd) 7902237Smaybee { 7913063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 7923063Sperrin rl_t *rl = zgd->zgd_rl; 7932237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 7942237Smaybee 7953063Sperrin dmu_buf_rele(db, vzgd); 7962237Smaybee zfs_range_unlock(rl); 7972237Smaybee VN_RELE(vp); 7985688Sbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 7993063Sperrin kmem_free(zgd, sizeof (zgd_t)); 8002237Smaybee } 8012237Smaybee 802789Sahrens /* 803789Sahrens * Get data to generate a TX_WRITE intent log record. 804789Sahrens */ 805789Sahrens int 8062237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 807789Sahrens { 808789Sahrens zfsvfs_t *zfsvfs = arg; 809789Sahrens objset_t *os = zfsvfs->z_os; 810789Sahrens znode_t *zp; 811789Sahrens uint64_t off = lr->lr_offset; 8122237Smaybee dmu_buf_t *db; 8131669Sperrin rl_t *rl; 8143063Sperrin zgd_t *zgd; 8153638Sbillm int dlen = lr->lr_length; /* length of user data */ 816789Sahrens int error = 0; 817789Sahrens 8183063Sperrin ASSERT(zio); 819789Sahrens ASSERT(dlen != 0); 820789Sahrens 821789Sahrens /* 8221669Sperrin * Nothing to do if the file has been removed 823789Sahrens */ 824789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 825789Sahrens return (ENOENT); 8263461Sahrens if (zp->z_unlinked) { 827789Sahrens VN_RELE(ZTOV(zp)); 828789Sahrens return (ENOENT); 829789Sahrens } 830789Sahrens 831789Sahrens /* 832789Sahrens * Write records come in two flavors: immediate and indirect. 833789Sahrens * For small writes it's cheaper to store the data with the 834789Sahrens * log record (immediate); for large writes it's cheaper to 835789Sahrens * sync the data and get a pointer to it (indirect) so that 836789Sahrens * we don't have to write the data twice. 837789Sahrens */ 8381669Sperrin if (buf != NULL) { /* immediate write */ 8391669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 8401669Sperrin /* test for truncation needs to be done while range locked */ 8411669Sperrin if (off >= zp->z_phys->zp_size) { 8421669Sperrin error = ENOENT; 8431669Sperrin goto out; 8441669Sperrin } 8452449Smaybee VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 8461669Sperrin } else { /* indirect write */ 8471669Sperrin uint64_t boff; /* block starting offset */ 8481669Sperrin 849789Sahrens /* 8501669Sperrin * Have to lock the whole block to ensure when it's 8511669Sperrin * written out and it's checksum is being calculated 8521669Sperrin * that no one can change the data. We need to re-check 8531669Sperrin * blocksize after we get the lock in case it's changed! 854789Sahrens */ 8551669Sperrin for (;;) { 8561941Sperrin if (ISP2(zp->z_blksz)) { 8571941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 8581941Sperrin uint64_t); 8591941Sperrin } else { 8601941Sperrin boff = 0; 8611941Sperrin } 8621669Sperrin dlen = zp->z_blksz; 8631669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 8641669Sperrin if (zp->z_blksz == dlen) 8651669Sperrin break; 8662237Smaybee zfs_range_unlock(rl); 8671669Sperrin } 8681669Sperrin /* test for truncation needs to be done while range locked */ 8691669Sperrin if (off >= zp->z_phys->zp_size) { 8701669Sperrin error = ENOENT; 8711669Sperrin goto out; 8721669Sperrin } 8733063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 8743063Sperrin zgd->zgd_rl = rl; 8753063Sperrin zgd->zgd_zilog = zfsvfs->z_log; 8763063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 8773063Sperrin VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 8782237Smaybee ASSERT(boff == db->db_offset); 8792237Smaybee lr->lr_blkoff = off - boff; 8802237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 8813063Sperrin lr->lr_common.lrc_txg, zfs_get_done, zgd); 8824709Smaybee ASSERT((error && error != EINPROGRESS) || 8834709Smaybee lr->lr_length <= zp->z_blksz); 8845688Sbonwick if (error == 0) 8855688Sbonwick zil_add_block(zfsvfs->z_log, &lr->lr_blkptr); 8862237Smaybee /* 8872237Smaybee * If we get EINPROGRESS, then we need to wait for a 8882237Smaybee * write IO initiated by dmu_sync() to complete before 8892638Sperrin * we can release this dbuf. We will finish everything 8902237Smaybee * up in the zfs_get_done() callback. 8912237Smaybee */ 8922237Smaybee if (error == EINPROGRESS) 8932237Smaybee return (0); 8943063Sperrin dmu_buf_rele(db, zgd); 8953063Sperrin kmem_free(zgd, sizeof (zgd_t)); 896789Sahrens } 8971669Sperrin out: 8982237Smaybee zfs_range_unlock(rl); 899789Sahrens VN_RELE(ZTOV(zp)); 900789Sahrens return (error); 901789Sahrens } 902789Sahrens 903789Sahrens /*ARGSUSED*/ 904789Sahrens static int 9055331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 9065331Samw caller_context_t *ct) 907789Sahrens { 908789Sahrens znode_t *zp = VTOZ(vp); 909789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 910789Sahrens int error; 911789Sahrens 9125367Sahrens ZFS_ENTER(zfsvfs); 9135367Sahrens ZFS_VERIFY_ZP(zp); 9145331Samw 9155331Samw if (flag & V_ACE_MASK) 9165331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 9175331Samw else 9185331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 9195331Samw 920789Sahrens ZFS_EXIT(zfsvfs); 921789Sahrens return (error); 922789Sahrens } 923789Sahrens 924789Sahrens /* 925789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 926789Sahrens * If it exists, return a held vnode reference for it. 927789Sahrens * 928789Sahrens * IN: dvp - vnode of directory to search. 929789Sahrens * nm - name of entry to lookup. 930789Sahrens * pnp - full pathname to lookup [UNUSED]. 931789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 932789Sahrens * rdir - root directory vnode [UNUSED]. 933789Sahrens * cr - credentials of caller. 9345331Samw * ct - caller context 9355331Samw * direntflags - directory lookup flags 9365331Samw * realpnp - returned pathname. 937789Sahrens * 938789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 939789Sahrens * 940789Sahrens * RETURN: 0 if success 941789Sahrens * error code if failure 942789Sahrens * 943789Sahrens * Timestamps: 944789Sahrens * NA 945789Sahrens */ 946789Sahrens /* ARGSUSED */ 947789Sahrens static int 948789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 9495331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 9505331Samw int *direntflags, pathname_t *realpnp) 951789Sahrens { 952789Sahrens znode_t *zdp = VTOZ(dvp); 953789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 954789Sahrens int error; 955789Sahrens 9565367Sahrens ZFS_ENTER(zfsvfs); 9575367Sahrens ZFS_VERIFY_ZP(zdp); 958789Sahrens 959789Sahrens *vpp = NULL; 960789Sahrens 961789Sahrens if (flags & LOOKUP_XATTR) { 962789Sahrens /* 9633234Sck153898 * If the xattr property is off, refuse the lookup request. 9643234Sck153898 */ 9653234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 9663234Sck153898 ZFS_EXIT(zfsvfs); 9673234Sck153898 return (EINVAL); 9683234Sck153898 } 9693234Sck153898 9703234Sck153898 /* 971789Sahrens * We don't allow recursive attributes.. 972789Sahrens * Maybe someday we will. 973789Sahrens */ 974789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 975789Sahrens ZFS_EXIT(zfsvfs); 976789Sahrens return (EINVAL); 977789Sahrens } 978789Sahrens 9793280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 980789Sahrens ZFS_EXIT(zfsvfs); 981789Sahrens return (error); 982789Sahrens } 983789Sahrens 984789Sahrens /* 985789Sahrens * Do we have permission to get into attribute directory? 986789Sahrens */ 987789Sahrens 9885331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 9895331Samw B_FALSE, cr)) { 990789Sahrens VN_RELE(*vpp); 9915331Samw *vpp = NULL; 992789Sahrens } 993789Sahrens 994789Sahrens ZFS_EXIT(zfsvfs); 995789Sahrens return (error); 996789Sahrens } 997789Sahrens 9981512Sek110237 if (dvp->v_type != VDIR) { 9991512Sek110237 ZFS_EXIT(zfsvfs); 10001460Smarks return (ENOTDIR); 10011512Sek110237 } 10021460Smarks 1003789Sahrens /* 1004789Sahrens * Check accessibility of directory. 1005789Sahrens */ 1006789Sahrens 10075331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1008789Sahrens ZFS_EXIT(zfsvfs); 1009789Sahrens return (error); 1010789Sahrens } 1011789Sahrens 10125498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 10135331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 10145331Samw ZFS_EXIT(zfsvfs); 10155331Samw return (EILSEQ); 10165331Samw } 10175331Samw 10185331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 10195331Samw if (error == 0) { 1020789Sahrens /* 1021789Sahrens * Convert device special files 1022789Sahrens */ 1023789Sahrens if (IS_DEVVP(*vpp)) { 1024789Sahrens vnode_t *svp; 1025789Sahrens 1026789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1027789Sahrens VN_RELE(*vpp); 1028789Sahrens if (svp == NULL) 1029789Sahrens error = ENOSYS; 1030789Sahrens else 1031789Sahrens *vpp = svp; 1032789Sahrens } 1033789Sahrens } 1034789Sahrens 1035789Sahrens ZFS_EXIT(zfsvfs); 1036789Sahrens return (error); 1037789Sahrens } 1038789Sahrens 1039789Sahrens /* 1040789Sahrens * Attempt to create a new entry in a directory. If the entry 1041789Sahrens * already exists, truncate the file if permissible, else return 1042789Sahrens * an error. Return the vp of the created or trunc'd file. 1043789Sahrens * 1044789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1045789Sahrens * name - name of new file entry. 1046789Sahrens * vap - attributes of new file. 1047789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1048789Sahrens * mode - mode to open file with. 1049789Sahrens * cr - credentials of caller. 1050789Sahrens * flag - large file flag [UNUSED]. 10515331Samw * ct - caller context 10525331Samw * vsecp - ACL to be set 1053789Sahrens * 1054789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1055789Sahrens * 1056789Sahrens * RETURN: 0 if success 1057789Sahrens * error code if failure 1058789Sahrens * 1059789Sahrens * Timestamps: 1060789Sahrens * dvp - ctime|mtime updated if new entry created 1061789Sahrens * vp - ctime|mtime always, atime if new 1062789Sahrens */ 10635331Samw 1064789Sahrens /* ARGSUSED */ 1065789Sahrens static int 1066789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 10675331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 10685331Samw vsecattr_t *vsecp) 1069789Sahrens { 1070789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1071789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 10725326Sek110237 zilog_t *zilog; 10735326Sek110237 objset_t *os; 1074789Sahrens zfs_dirlock_t *dl; 1075789Sahrens dmu_tx_t *tx; 1076789Sahrens int error; 10775331Samw zfs_acl_t *aclp = NULL; 10785331Samw zfs_fuid_info_t *fuidp = NULL; 10797847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 10807847SMark.Shellenbaum@Sun.COM uid_t uid; 10817847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 10825331Samw 10835331Samw /* 10845331Samw * If we have an ephemeral id, ACL, or XVATTR then 10855331Samw * make sure file system is at proper version 10865331Samw */ 10875331Samw 10887847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 10897847SMark.Shellenbaum@Sun.COM if (ksid) 10907847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 10917847SMark.Shellenbaum@Sun.COM else 10927847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 10937847SMark.Shellenbaum@Sun.COM 10945331Samw if (zfsvfs->z_use_fuids == B_FALSE && 10955331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 10967847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 10975331Samw return (EINVAL); 1098789Sahrens 10995367Sahrens ZFS_ENTER(zfsvfs); 11005367Sahrens ZFS_VERIFY_ZP(dzp); 11015326Sek110237 os = zfsvfs->z_os; 11025326Sek110237 zilog = zfsvfs->z_log; 1103789Sahrens 11045498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 11055331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 11065331Samw ZFS_EXIT(zfsvfs); 11075331Samw return (EILSEQ); 11085331Samw } 11095331Samw 11105331Samw if (vap->va_mask & AT_XVATTR) { 11115331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 11125331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 11135331Samw ZFS_EXIT(zfsvfs); 11145331Samw return (error); 11155331Samw } 11165331Samw } 1117789Sahrens top: 1118789Sahrens *vpp = NULL; 1119789Sahrens 1120789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1121789Sahrens vap->va_mode &= ~VSVTX; 1122789Sahrens 1123789Sahrens if (*name == '\0') { 1124789Sahrens /* 1125789Sahrens * Null component name refers to the directory itself. 1126789Sahrens */ 1127789Sahrens VN_HOLD(dvp); 1128789Sahrens zp = dzp; 1129789Sahrens dl = NULL; 1130789Sahrens error = 0; 1131789Sahrens } else { 1132789Sahrens /* possible VN_HOLD(zp) */ 11335331Samw int zflg = 0; 11345331Samw 11355331Samw if (flag & FIGNORECASE) 11365331Samw zflg |= ZCILOOK; 11375331Samw 11385331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 11395331Samw NULL, NULL); 11405331Samw if (error) { 1141789Sahrens if (strcmp(name, "..") == 0) 1142789Sahrens error = EISDIR; 1143789Sahrens ZFS_EXIT(zfsvfs); 11445331Samw if (aclp) 11455331Samw zfs_acl_free(aclp); 1146789Sahrens return (error); 1147789Sahrens } 1148789Sahrens } 11495331Samw if (vsecp && aclp == NULL) { 11505331Samw error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp); 11515331Samw if (error) { 11525331Samw ZFS_EXIT(zfsvfs); 11535331Samw if (dl) 11545331Samw zfs_dirent_unlock(dl); 11555331Samw return (error); 11565331Samw } 11575331Samw } 1158789Sahrens 1159789Sahrens if (zp == NULL) { 11605331Samw uint64_t txtype; 11615331Samw 1162789Sahrens /* 1163789Sahrens * Create a new file object and update the directory 1164789Sahrens * to reference it. 1165789Sahrens */ 11665331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1167789Sahrens goto out; 1168789Sahrens } 1169789Sahrens 1170789Sahrens /* 1171789Sahrens * We only support the creation of regular files in 1172789Sahrens * extended attribute directories. 1173789Sahrens */ 1174789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1175789Sahrens (vap->va_type != VREG)) { 1176789Sahrens error = EINVAL; 1177789Sahrens goto out; 1178789Sahrens } 1179789Sahrens 1180789Sahrens tx = dmu_tx_create(os); 1181789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 11827847SMark.Shellenbaum@Sun.COM if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(uid) || 11837847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(gid)) { 11845824Smarks if (zfsvfs->z_fuid_obj == 0) { 11855824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 11865824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 11875824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 11885824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 11895824Smarks FALSE, NULL); 11905824Smarks } else { 11915824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 11925824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 11935824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 11945824Smarks } 11955331Samw } 1196789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 11971544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 11985331Samw if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) { 1199789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1200789Sahrens 0, SPA_MAXBLOCKSIZE); 12015331Samw } 12028227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1203789Sahrens if (error) { 1204789Sahrens zfs_dirent_unlock(dl); 12058227SNeil.Perrin@Sun.COM if (error == ERESTART) { 12062113Sahrens dmu_tx_wait(tx); 12072113Sahrens dmu_tx_abort(tx); 1208789Sahrens goto top; 1209789Sahrens } 12102113Sahrens dmu_tx_abort(tx); 1211789Sahrens ZFS_EXIT(zfsvfs); 12125331Samw if (aclp) 12135331Samw zfs_acl_free(aclp); 1214789Sahrens return (error); 1215789Sahrens } 12165446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp); 1217789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 12185331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 12195331Samw if (flag & FIGNORECASE) 12205331Samw txtype |= TX_CI; 12215331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 12225331Samw vsecp, fuidp, vap); 12235331Samw if (fuidp) 12245331Samw zfs_fuid_info_free(fuidp); 1225789Sahrens dmu_tx_commit(tx); 1226789Sahrens } else { 12275331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 12285331Samw 1229789Sahrens /* 1230789Sahrens * A directory entry already exists for this name. 1231789Sahrens */ 1232789Sahrens /* 1233789Sahrens * Can't truncate an existing file if in exclusive mode. 1234789Sahrens */ 1235789Sahrens if (excl == EXCL) { 1236789Sahrens error = EEXIST; 1237789Sahrens goto out; 1238789Sahrens } 1239789Sahrens /* 1240789Sahrens * Can't open a directory for writing. 1241789Sahrens */ 1242789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1243789Sahrens error = EISDIR; 1244789Sahrens goto out; 1245789Sahrens } 1246789Sahrens /* 1247789Sahrens * Verify requested access to file. 1248789Sahrens */ 12495331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1250789Sahrens goto out; 1251789Sahrens } 1252789Sahrens 1253789Sahrens mutex_enter(&dzp->z_lock); 1254789Sahrens dzp->z_seq++; 1255789Sahrens mutex_exit(&dzp->z_lock); 1256789Sahrens 12571878Smaybee /* 12581878Smaybee * Truncate regular files if requested. 12591878Smaybee */ 12601878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1261789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 12626992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 12636992Smaybee zfs_dirent_unlock(dl); 12646992Smaybee dl = NULL; 12651878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 12664863Spraks if (error == 0) { 12675331Samw vnevent_create(ZTOV(zp), ct); 12684863Spraks } 1269789Sahrens } 1270789Sahrens } 1271789Sahrens out: 1272789Sahrens 1273789Sahrens if (dl) 1274789Sahrens zfs_dirent_unlock(dl); 1275789Sahrens 1276789Sahrens if (error) { 1277789Sahrens if (zp) 1278789Sahrens VN_RELE(ZTOV(zp)); 1279789Sahrens } else { 1280789Sahrens *vpp = ZTOV(zp); 1281789Sahrens /* 1282789Sahrens * If vnode is for a device return a specfs vnode instead. 1283789Sahrens */ 1284789Sahrens if (IS_DEVVP(*vpp)) { 1285789Sahrens struct vnode *svp; 1286789Sahrens 1287789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1288789Sahrens VN_RELE(*vpp); 1289789Sahrens if (svp == NULL) { 1290789Sahrens error = ENOSYS; 1291789Sahrens } 1292789Sahrens *vpp = svp; 1293789Sahrens } 1294789Sahrens } 12955331Samw if (aclp) 12965331Samw zfs_acl_free(aclp); 1297789Sahrens 1298789Sahrens ZFS_EXIT(zfsvfs); 1299789Sahrens return (error); 1300789Sahrens } 1301789Sahrens 1302789Sahrens /* 1303789Sahrens * Remove an entry from a directory. 1304789Sahrens * 1305789Sahrens * IN: dvp - vnode of directory to remove entry from. 1306789Sahrens * name - name of entry to remove. 1307789Sahrens * cr - credentials of caller. 13085331Samw * ct - caller context 13095331Samw * flags - case flags 1310789Sahrens * 1311789Sahrens * RETURN: 0 if success 1312789Sahrens * error code if failure 1313789Sahrens * 1314789Sahrens * Timestamps: 1315789Sahrens * dvp - ctime|mtime 1316789Sahrens * vp - ctime (if nlink > 0) 1317789Sahrens */ 13185331Samw /*ARGSUSED*/ 1319789Sahrens static int 13205331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 13215331Samw int flags) 1322789Sahrens { 1323789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1324789Sahrens znode_t *xzp = NULL; 1325789Sahrens vnode_t *vp; 1326789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 13275326Sek110237 zilog_t *zilog; 1328789Sahrens uint64_t acl_obj, xattr_obj; 1329789Sahrens zfs_dirlock_t *dl; 1330789Sahrens dmu_tx_t *tx; 13313461Sahrens boolean_t may_delete_now, delete_now = FALSE; 13326992Smaybee boolean_t unlinked, toobig = FALSE; 13335331Samw uint64_t txtype; 13345331Samw pathname_t *realnmp = NULL; 13355331Samw pathname_t realnm; 1336789Sahrens int error; 13375331Samw int zflg = ZEXISTS; 1338789Sahrens 13395367Sahrens ZFS_ENTER(zfsvfs); 13405367Sahrens ZFS_VERIFY_ZP(dzp); 13415326Sek110237 zilog = zfsvfs->z_log; 1342789Sahrens 13435331Samw if (flags & FIGNORECASE) { 13445331Samw zflg |= ZCILOOK; 13455331Samw pn_alloc(&realnm); 13465331Samw realnmp = &realnm; 13475331Samw } 13485331Samw 1349789Sahrens top: 1350789Sahrens /* 1351789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1352789Sahrens */ 13535331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13545331Samw NULL, realnmp)) { 13555331Samw if (realnmp) 13565331Samw pn_free(realnmp); 1357789Sahrens ZFS_EXIT(zfsvfs); 1358789Sahrens return (error); 1359789Sahrens } 1360789Sahrens 1361789Sahrens vp = ZTOV(zp); 1362789Sahrens 1363789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1364789Sahrens goto out; 1365789Sahrens } 1366789Sahrens 1367789Sahrens /* 1368789Sahrens * Need to use rmdir for removing directories. 1369789Sahrens */ 1370789Sahrens if (vp->v_type == VDIR) { 1371789Sahrens error = EPERM; 1372789Sahrens goto out; 1373789Sahrens } 1374789Sahrens 13755331Samw vnevent_remove(vp, dvp, name, ct); 13765331Samw 13775331Samw if (realnmp) 13786492Stimh dnlc_remove(dvp, realnmp->pn_buf); 13795331Samw else 13805331Samw dnlc_remove(dvp, name); 13811484Sek110237 1382789Sahrens mutex_enter(&vp->v_lock); 1383789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1384789Sahrens mutex_exit(&vp->v_lock); 1385789Sahrens 1386789Sahrens /* 13873461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1388789Sahrens * it depends on whether we're the last link, and on whether there are 1389789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1390789Sahrens * allow for either case. 1391789Sahrens */ 1392789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 13931544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1394789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 13956992Smaybee if (may_delete_now) { 13966992Smaybee toobig = 13976992Smaybee zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 13986992Smaybee /* if the file is too big, only hold_free a token amount */ 13996992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 14006992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 14016992Smaybee } 1402789Sahrens 1403789Sahrens /* are there any extended attributes? */ 1404789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1405789Sahrens /* XXX - do we need this if we are deleting? */ 1406789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1407789Sahrens } 1408789Sahrens 1409789Sahrens /* are there any additional acls */ 1410789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1411789Sahrens may_delete_now) 1412789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1413789Sahrens 1414789Sahrens /* charge as an update -- would be nice not to charge at all */ 14153461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1416789Sahrens 14178227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1418789Sahrens if (error) { 1419789Sahrens zfs_dirent_unlock(dl); 1420789Sahrens VN_RELE(vp); 14218227SNeil.Perrin@Sun.COM if (error == ERESTART) { 14222113Sahrens dmu_tx_wait(tx); 14232113Sahrens dmu_tx_abort(tx); 1424789Sahrens goto top; 1425789Sahrens } 14265331Samw if (realnmp) 14275331Samw pn_free(realnmp); 14282113Sahrens dmu_tx_abort(tx); 1429789Sahrens ZFS_EXIT(zfsvfs); 1430789Sahrens return (error); 1431789Sahrens } 1432789Sahrens 1433789Sahrens /* 1434789Sahrens * Remove the directory entry. 1435789Sahrens */ 14365331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1437789Sahrens 1438789Sahrens if (error) { 1439789Sahrens dmu_tx_commit(tx); 1440789Sahrens goto out; 1441789Sahrens } 1442789Sahrens 14433461Sahrens if (unlinked) { 1444789Sahrens mutex_enter(&vp->v_lock); 14456992Smaybee delete_now = may_delete_now && !toobig && 1446789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1447789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1448789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1449789Sahrens mutex_exit(&vp->v_lock); 1450789Sahrens } 1451789Sahrens 1452789Sahrens if (delete_now) { 1453789Sahrens if (zp->z_phys->zp_xattr) { 1454789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1455789Sahrens ASSERT3U(error, ==, 0); 1456789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1457789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1458789Sahrens mutex_enter(&xzp->z_lock); 14593461Sahrens xzp->z_unlinked = 1; 1460789Sahrens xzp->z_phys->zp_links = 0; 1461789Sahrens mutex_exit(&xzp->z_lock); 14623461Sahrens zfs_unlinked_add(xzp, tx); 1463789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1464789Sahrens } 1465789Sahrens mutex_enter(&zp->z_lock); 1466789Sahrens mutex_enter(&vp->v_lock); 1467789Sahrens vp->v_count--; 1468789Sahrens ASSERT3U(vp->v_count, ==, 0); 1469789Sahrens mutex_exit(&vp->v_lock); 1470789Sahrens mutex_exit(&zp->z_lock); 1471789Sahrens zfs_znode_delete(zp, tx); 14723461Sahrens } else if (unlinked) { 14733461Sahrens zfs_unlinked_add(zp, tx); 1474789Sahrens } 1475789Sahrens 14765331Samw txtype = TX_REMOVE; 14775331Samw if (flags & FIGNORECASE) 14785331Samw txtype |= TX_CI; 14795331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 1480789Sahrens 1481789Sahrens dmu_tx_commit(tx); 1482789Sahrens out: 14835331Samw if (realnmp) 14845331Samw pn_free(realnmp); 14855331Samw 1486789Sahrens zfs_dirent_unlock(dl); 1487789Sahrens 1488789Sahrens if (!delete_now) { 1489789Sahrens VN_RELE(vp); 1490789Sahrens } else if (xzp) { 14916992Smaybee /* this rele is delayed to prevent nesting transactions */ 1492789Sahrens VN_RELE(ZTOV(xzp)); 1493789Sahrens } 1494789Sahrens 1495789Sahrens ZFS_EXIT(zfsvfs); 1496789Sahrens return (error); 1497789Sahrens } 1498789Sahrens 1499789Sahrens /* 1500789Sahrens * Create a new directory and insert it into dvp using the name 1501789Sahrens * provided. Return a pointer to the inserted directory. 1502789Sahrens * 1503789Sahrens * IN: dvp - vnode of directory to add subdir to. 1504789Sahrens * dirname - name of new directory. 1505789Sahrens * vap - attributes of new directory. 1506789Sahrens * cr - credentials of caller. 15075331Samw * ct - caller context 15085331Samw * vsecp - ACL to be set 1509789Sahrens * 1510789Sahrens * OUT: vpp - vnode of created directory. 1511789Sahrens * 1512789Sahrens * RETURN: 0 if success 1513789Sahrens * error code if failure 1514789Sahrens * 1515789Sahrens * Timestamps: 1516789Sahrens * dvp - ctime|mtime updated 1517789Sahrens * vp - ctime|mtime|atime updated 1518789Sahrens */ 15195331Samw /*ARGSUSED*/ 1520789Sahrens static int 15215331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 15225331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1523789Sahrens { 1524789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1525789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15265326Sek110237 zilog_t *zilog; 1527789Sahrens zfs_dirlock_t *dl; 15285331Samw uint64_t txtype; 1529789Sahrens dmu_tx_t *tx; 1530789Sahrens int error; 15315331Samw zfs_acl_t *aclp = NULL; 15325331Samw zfs_fuid_info_t *fuidp = NULL; 15335331Samw int zf = ZNEW; 15347847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 15357847SMark.Shellenbaum@Sun.COM uid_t uid; 15367847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 1537789Sahrens 1538789Sahrens ASSERT(vap->va_type == VDIR); 1539789Sahrens 15405331Samw /* 15415331Samw * If we have an ephemeral id, ACL, or XVATTR then 15425331Samw * make sure file system is at proper version 15435331Samw */ 15445331Samw 15457847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 15467847SMark.Shellenbaum@Sun.COM if (ksid) 15477847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 15487847SMark.Shellenbaum@Sun.COM else 15497847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 15505331Samw if (zfsvfs->z_use_fuids == B_FALSE && 15517847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 15527876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 15535331Samw return (EINVAL); 15545331Samw 15555367Sahrens ZFS_ENTER(zfsvfs); 15565367Sahrens ZFS_VERIFY_ZP(dzp); 15575326Sek110237 zilog = zfsvfs->z_log; 1558789Sahrens 1559789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1560789Sahrens ZFS_EXIT(zfsvfs); 1561789Sahrens return (EINVAL); 1562789Sahrens } 15635331Samw 15645498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 15655331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 15665331Samw ZFS_EXIT(zfsvfs); 15675331Samw return (EILSEQ); 15685331Samw } 15695331Samw if (flags & FIGNORECASE) 15705331Samw zf |= ZCILOOK; 15715331Samw 15725331Samw if (vap->va_mask & AT_XVATTR) 15735331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 15745331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 15755331Samw ZFS_EXIT(zfsvfs); 15765331Samw return (error); 15775331Samw } 1578789Sahrens 1579789Sahrens /* 1580789Sahrens * First make sure the new directory doesn't exist. 1581789Sahrens */ 15825331Samw top: 15835331Samw *vpp = NULL; 15845331Samw 15855331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 15865331Samw NULL, NULL)) { 1587789Sahrens ZFS_EXIT(zfsvfs); 1588789Sahrens return (error); 1589789Sahrens } 1590789Sahrens 15915331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 15921231Smarks zfs_dirent_unlock(dl); 15931231Smarks ZFS_EXIT(zfsvfs); 15941231Smarks return (error); 15951231Smarks } 15961231Smarks 15975331Samw if (vsecp && aclp == NULL) { 15985331Samw error = zfs_vsec_2_aclp(zfsvfs, vap->va_type, vsecp, &aclp); 15995331Samw if (error) { 16005331Samw zfs_dirent_unlock(dl); 16015331Samw ZFS_EXIT(zfsvfs); 16025331Samw return (error); 16035331Samw } 16045331Samw } 1605789Sahrens /* 1606789Sahrens * Add a new entry to the directory. 1607789Sahrens */ 1608789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16091544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 16101544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 16117847SMark.Shellenbaum@Sun.COM if ((aclp && aclp->z_has_fuids) || IS_EPHEMERAL(uid) || 16127847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(gid)) { 16135824Smarks if (zfsvfs->z_fuid_obj == 0) { 16145824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 16155824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 16165824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16175824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 16185824Smarks } else { 16195824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 16205824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 16215824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16225824Smarks } 16235331Samw } 16245331Samw if ((dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) || aclp) 1625789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1626789Sahrens 0, SPA_MAXBLOCKSIZE); 16278227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1628789Sahrens if (error) { 1629789Sahrens zfs_dirent_unlock(dl); 16308227SNeil.Perrin@Sun.COM if (error == ERESTART) { 16312113Sahrens dmu_tx_wait(tx); 16322113Sahrens dmu_tx_abort(tx); 1633789Sahrens goto top; 1634789Sahrens } 16352113Sahrens dmu_tx_abort(tx); 1636789Sahrens ZFS_EXIT(zfsvfs); 16375331Samw if (aclp) 16385331Samw zfs_acl_free(aclp); 1639789Sahrens return (error); 1640789Sahrens } 1641789Sahrens 1642789Sahrens /* 1643789Sahrens * Create new node. 1644789Sahrens */ 16455446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, aclp, &fuidp); 16465331Samw 16475331Samw if (aclp) 16485331Samw zfs_acl_free(aclp); 1649789Sahrens 1650789Sahrens /* 1651789Sahrens * Now put new name in parent dir. 1652789Sahrens */ 1653789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1654789Sahrens 1655789Sahrens *vpp = ZTOV(zp); 1656789Sahrens 16575331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 16585331Samw if (flags & FIGNORECASE) 16595331Samw txtype |= TX_CI; 16605331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, fuidp, vap); 16615331Samw 16625331Samw if (fuidp) 16635331Samw zfs_fuid_info_free(fuidp); 1664789Sahrens dmu_tx_commit(tx); 1665789Sahrens 1666789Sahrens zfs_dirent_unlock(dl); 1667789Sahrens 1668789Sahrens ZFS_EXIT(zfsvfs); 1669789Sahrens return (0); 1670789Sahrens } 1671789Sahrens 1672789Sahrens /* 1673789Sahrens * Remove a directory subdir entry. If the current working 1674789Sahrens * directory is the same as the subdir to be removed, the 1675789Sahrens * remove will fail. 1676789Sahrens * 1677789Sahrens * IN: dvp - vnode of directory to remove from. 1678789Sahrens * name - name of directory to be removed. 1679789Sahrens * cwd - vnode of current working directory. 1680789Sahrens * cr - credentials of caller. 16815331Samw * ct - caller context 16825331Samw * flags - case flags 1683789Sahrens * 1684789Sahrens * RETURN: 0 if success 1685789Sahrens * error code if failure 1686789Sahrens * 1687789Sahrens * Timestamps: 1688789Sahrens * dvp - ctime|mtime updated 1689789Sahrens */ 16905331Samw /*ARGSUSED*/ 1691789Sahrens static int 16925331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 16935331Samw caller_context_t *ct, int flags) 1694789Sahrens { 1695789Sahrens znode_t *dzp = VTOZ(dvp); 1696789Sahrens znode_t *zp; 1697789Sahrens vnode_t *vp; 1698789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 16995326Sek110237 zilog_t *zilog; 1700789Sahrens zfs_dirlock_t *dl; 1701789Sahrens dmu_tx_t *tx; 1702789Sahrens int error; 17035331Samw int zflg = ZEXISTS; 1704789Sahrens 17055367Sahrens ZFS_ENTER(zfsvfs); 17065367Sahrens ZFS_VERIFY_ZP(dzp); 17075326Sek110237 zilog = zfsvfs->z_log; 1708789Sahrens 17095331Samw if (flags & FIGNORECASE) 17105331Samw zflg |= ZCILOOK; 1711789Sahrens top: 1712789Sahrens zp = NULL; 1713789Sahrens 1714789Sahrens /* 1715789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1716789Sahrens */ 17175331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 17185331Samw NULL, NULL)) { 1719789Sahrens ZFS_EXIT(zfsvfs); 1720789Sahrens return (error); 1721789Sahrens } 1722789Sahrens 1723789Sahrens vp = ZTOV(zp); 1724789Sahrens 1725789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1726789Sahrens goto out; 1727789Sahrens } 1728789Sahrens 1729789Sahrens if (vp->v_type != VDIR) { 1730789Sahrens error = ENOTDIR; 1731789Sahrens goto out; 1732789Sahrens } 1733789Sahrens 1734789Sahrens if (vp == cwd) { 1735789Sahrens error = EINVAL; 1736789Sahrens goto out; 1737789Sahrens } 1738789Sahrens 17395331Samw vnevent_rmdir(vp, dvp, name, ct); 1740789Sahrens 1741789Sahrens /* 17423897Smaybee * Grab a lock on the directory to make sure that noone is 17433897Smaybee * trying to add (or lookup) entries while we are removing it. 17443897Smaybee */ 17453897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 17463897Smaybee 17473897Smaybee /* 17483897Smaybee * Grab a lock on the parent pointer to make sure we play well 1749789Sahrens * with the treewalk and directory rename code. 1750789Sahrens */ 1751789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1752789Sahrens 1753789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 17541544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1755789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 17563461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 17578227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1758789Sahrens if (error) { 1759789Sahrens rw_exit(&zp->z_parent_lock); 17603897Smaybee rw_exit(&zp->z_name_lock); 1761789Sahrens zfs_dirent_unlock(dl); 1762789Sahrens VN_RELE(vp); 17638227SNeil.Perrin@Sun.COM if (error == ERESTART) { 17642113Sahrens dmu_tx_wait(tx); 17652113Sahrens dmu_tx_abort(tx); 1766789Sahrens goto top; 1767789Sahrens } 17682113Sahrens dmu_tx_abort(tx); 1769789Sahrens ZFS_EXIT(zfsvfs); 1770789Sahrens return (error); 1771789Sahrens } 1772789Sahrens 17735331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 17745331Samw 17755331Samw if (error == 0) { 17765331Samw uint64_t txtype = TX_RMDIR; 17775331Samw if (flags & FIGNORECASE) 17785331Samw txtype |= TX_CI; 17795331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 17805331Samw } 1781789Sahrens 1782789Sahrens dmu_tx_commit(tx); 1783789Sahrens 1784789Sahrens rw_exit(&zp->z_parent_lock); 17853897Smaybee rw_exit(&zp->z_name_lock); 1786789Sahrens out: 1787789Sahrens zfs_dirent_unlock(dl); 1788789Sahrens 1789789Sahrens VN_RELE(vp); 1790789Sahrens 1791789Sahrens ZFS_EXIT(zfsvfs); 1792789Sahrens return (error); 1793789Sahrens } 1794789Sahrens 1795789Sahrens /* 1796789Sahrens * Read as many directory entries as will fit into the provided 1797789Sahrens * buffer from the given directory cursor position (specified in 1798789Sahrens * the uio structure. 1799789Sahrens * 1800789Sahrens * IN: vp - vnode of directory to read. 1801789Sahrens * uio - structure supplying read location, range info, 1802789Sahrens * and return buffer. 1803789Sahrens * cr - credentials of caller. 18045331Samw * ct - caller context 18055331Samw * flags - case flags 1806789Sahrens * 1807789Sahrens * OUT: uio - updated offset and range, buffer filled. 1808789Sahrens * eofp - set to true if end-of-file detected. 1809789Sahrens * 1810789Sahrens * RETURN: 0 if success 1811789Sahrens * error code if failure 1812789Sahrens * 1813789Sahrens * Timestamps: 1814789Sahrens * vp - atime updated 1815789Sahrens * 1816789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1817789Sahrens * This allows us to use the low range for "special" directory entries: 1818789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1819789Sahrens * we use the offset 2 for the '.zfs' directory. 1820789Sahrens */ 1821789Sahrens /* ARGSUSED */ 1822789Sahrens static int 18235331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 18245331Samw caller_context_t *ct, int flags) 1825789Sahrens { 1826789Sahrens znode_t *zp = VTOZ(vp); 1827789Sahrens iovec_t *iovp; 18285331Samw edirent_t *eodp; 1829789Sahrens dirent64_t *odp; 1830789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1831869Sperrin objset_t *os; 1832789Sahrens caddr_t outbuf; 1833789Sahrens size_t bufsize; 1834789Sahrens zap_cursor_t zc; 1835789Sahrens zap_attribute_t zap; 1836789Sahrens uint_t bytes_wanted; 1837789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1838789Sahrens int local_eof; 1839869Sperrin int outcount; 1840869Sperrin int error; 1841869Sperrin uint8_t prefetch; 18425663Sck153898 boolean_t check_sysattrs; 1843789Sahrens 18445367Sahrens ZFS_ENTER(zfsvfs); 18455367Sahrens ZFS_VERIFY_ZP(zp); 1846789Sahrens 1847789Sahrens /* 1848789Sahrens * If we are not given an eof variable, 1849789Sahrens * use a local one. 1850789Sahrens */ 1851789Sahrens if (eofp == NULL) 1852789Sahrens eofp = &local_eof; 1853789Sahrens 1854789Sahrens /* 1855789Sahrens * Check for valid iov_len. 1856789Sahrens */ 1857789Sahrens if (uio->uio_iov->iov_len <= 0) { 1858789Sahrens ZFS_EXIT(zfsvfs); 1859789Sahrens return (EINVAL); 1860789Sahrens } 1861789Sahrens 1862789Sahrens /* 1863789Sahrens * Quit if directory has been removed (posix) 1864789Sahrens */ 18653461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 1866789Sahrens ZFS_EXIT(zfsvfs); 1867789Sahrens return (0); 1868789Sahrens } 1869789Sahrens 1870869Sperrin error = 0; 1871869Sperrin os = zfsvfs->z_os; 1872869Sperrin offset = uio->uio_loffset; 1873869Sperrin prefetch = zp->z_zn_prefetch; 1874869Sperrin 1875789Sahrens /* 1876789Sahrens * Initialize the iterator cursor. 1877789Sahrens */ 1878789Sahrens if (offset <= 3) { 1879789Sahrens /* 1880789Sahrens * Start iteration from the beginning of the directory. 1881789Sahrens */ 1882869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1883789Sahrens } else { 1884789Sahrens /* 1885789Sahrens * The offset is a serialized cursor. 1886789Sahrens */ 1887869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1888789Sahrens } 1889789Sahrens 1890789Sahrens /* 1891789Sahrens * Get space to change directory entries into fs independent format. 1892789Sahrens */ 1893789Sahrens iovp = uio->uio_iov; 1894789Sahrens bytes_wanted = iovp->iov_len; 1895789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1896789Sahrens bufsize = bytes_wanted; 1897789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1898789Sahrens odp = (struct dirent64 *)outbuf; 1899789Sahrens } else { 1900789Sahrens bufsize = bytes_wanted; 1901789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1902789Sahrens } 19035331Samw eodp = (struct edirent *)odp; 1904789Sahrens 1905789Sahrens /* 19067757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 19077757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 19087757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 19097757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 19105663Sck153898 */ 19117757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 19125663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 19135663Sck153898 (flags & V_RDDIR_ENTFLAGS); 19145663Sck153898 19155663Sck153898 /* 1916789Sahrens * Transform to file-system independent format 1917789Sahrens */ 1918789Sahrens outcount = 0; 1919789Sahrens while (outcount < bytes_wanted) { 19203912Slling ino64_t objnum; 19213912Slling ushort_t reclen; 19223912Slling off64_t *next; 19233912Slling 1924789Sahrens /* 1925789Sahrens * Special case `.', `..', and `.zfs'. 1926789Sahrens */ 1927789Sahrens if (offset == 0) { 1928789Sahrens (void) strcpy(zap.za_name, "."); 19295331Samw zap.za_normalization_conflict = 0; 19303912Slling objnum = zp->z_id; 1931789Sahrens } else if (offset == 1) { 1932789Sahrens (void) strcpy(zap.za_name, ".."); 19335331Samw zap.za_normalization_conflict = 0; 19343912Slling objnum = zp->z_phys->zp_parent; 1935789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1936789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 19375331Samw zap.za_normalization_conflict = 0; 19383912Slling objnum = ZFSCTL_INO_ROOT; 1939789Sahrens } else { 1940789Sahrens /* 1941789Sahrens * Grab next entry. 1942789Sahrens */ 1943789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1944789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1945789Sahrens break; 1946789Sahrens else 1947789Sahrens goto update; 1948789Sahrens } 1949789Sahrens 1950789Sahrens if (zap.za_integer_length != 8 || 1951789Sahrens zap.za_num_integers != 1) { 1952789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1953789Sahrens "entry, obj = %lld, offset = %lld\n", 1954789Sahrens (u_longlong_t)zp->z_id, 1955789Sahrens (u_longlong_t)offset); 1956789Sahrens error = ENXIO; 1957789Sahrens goto update; 1958789Sahrens } 19593912Slling 19603912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 19613912Slling /* 19623912Slling * MacOS X can extract the object type here such as: 19633912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 19643912Slling */ 19655663Sck153898 19665663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 19675663Sck153898 zap.za_normalization_conflict = 19685663Sck153898 xattr_sysattr_casechk(zap.za_name); 19695663Sck153898 } 1970789Sahrens } 19715331Samw 19725331Samw if (flags & V_RDDIR_ENTFLAGS) 19735331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 19745331Samw else 19755331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1976789Sahrens 1977789Sahrens /* 1978789Sahrens * Will this entry fit in the buffer? 1979789Sahrens */ 19803912Slling if (outcount + reclen > bufsize) { 1981789Sahrens /* 1982789Sahrens * Did we manage to fit anything in the buffer? 1983789Sahrens */ 1984789Sahrens if (!outcount) { 1985789Sahrens error = EINVAL; 1986789Sahrens goto update; 1987789Sahrens } 1988789Sahrens break; 1989789Sahrens } 19905331Samw if (flags & V_RDDIR_ENTFLAGS) { 19915331Samw /* 19925331Samw * Add extended flag entry: 19935331Samw */ 19945331Samw eodp->ed_ino = objnum; 19955331Samw eodp->ed_reclen = reclen; 19965331Samw /* NOTE: ed_off is the offset for the *next* entry */ 19975331Samw next = &(eodp->ed_off); 19985331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 19995331Samw ED_CASE_CONFLICT : 0; 20005331Samw (void) strncpy(eodp->ed_name, zap.za_name, 20015331Samw EDIRENT_NAMELEN(reclen)); 20025331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 20035331Samw } else { 20045331Samw /* 20055331Samw * Add normal entry: 20065331Samw */ 20075331Samw odp->d_ino = objnum; 20085331Samw odp->d_reclen = reclen; 20095331Samw /* NOTE: d_off is the offset for the *next* entry */ 20105331Samw next = &(odp->d_off); 20115331Samw (void) strncpy(odp->d_name, zap.za_name, 20125331Samw DIRENT64_NAMELEN(reclen)); 20135331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 20145331Samw } 20153912Slling outcount += reclen; 2016789Sahrens 2017789Sahrens ASSERT(outcount <= bufsize); 2018789Sahrens 2019789Sahrens /* Prefetch znode */ 2020869Sperrin if (prefetch) 20213912Slling dmu_prefetch(os, objnum, 0, 0); 2022789Sahrens 2023789Sahrens /* 2024789Sahrens * Move to the next entry, fill in the previous offset. 2025789Sahrens */ 2026789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2027789Sahrens zap_cursor_advance(&zc); 2028789Sahrens offset = zap_cursor_serialize(&zc); 2029789Sahrens } else { 2030789Sahrens offset += 1; 2031789Sahrens } 2032789Sahrens *next = offset; 2033789Sahrens } 2034869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2035789Sahrens 2036789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2037789Sahrens iovp->iov_base += outcount; 2038789Sahrens iovp->iov_len -= outcount; 2039789Sahrens uio->uio_resid -= outcount; 2040789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2041789Sahrens /* 2042789Sahrens * Reset the pointer. 2043789Sahrens */ 2044789Sahrens offset = uio->uio_loffset; 2045789Sahrens } 2046789Sahrens 2047789Sahrens update: 2048885Sahrens zap_cursor_fini(&zc); 2049789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2050789Sahrens kmem_free(outbuf, bufsize); 2051789Sahrens 2052789Sahrens if (error == ENOENT) 2053789Sahrens error = 0; 2054789Sahrens 2055789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2056789Sahrens 2057789Sahrens uio->uio_loffset = offset; 2058789Sahrens ZFS_EXIT(zfsvfs); 2059789Sahrens return (error); 2060789Sahrens } 2061789Sahrens 20624720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 20634720Sfr157268 2064789Sahrens static int 20655331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2066789Sahrens { 2067789Sahrens znode_t *zp = VTOZ(vp); 2068789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2069789Sahrens 20701773Seschrock /* 20711773Seschrock * Regardless of whether this is required for standards conformance, 20721773Seschrock * this is the logical behavior when fsync() is called on a file with 20731773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 20741773Seschrock * going to be pushed out as part of the zil_commit(). 20751773Seschrock */ 20761773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 20771773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 20785331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 20791773Seschrock 20804720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 20814720Sfr157268 20825367Sahrens ZFS_ENTER(zfsvfs); 20835367Sahrens ZFS_VERIFY_ZP(zp); 20842638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 2085789Sahrens ZFS_EXIT(zfsvfs); 2086789Sahrens return (0); 2087789Sahrens } 2088789Sahrens 20895331Samw 2090789Sahrens /* 2091789Sahrens * Get the requested file attributes and place them in the provided 2092789Sahrens * vattr structure. 2093789Sahrens * 2094789Sahrens * IN: vp - vnode of file. 2095789Sahrens * vap - va_mask identifies requested attributes. 20965331Samw * If AT_XVATTR set, then optional attrs are requested 20975331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2098789Sahrens * cr - credentials of caller. 20995331Samw * ct - caller context 2100789Sahrens * 2101789Sahrens * OUT: vap - attribute values. 2102789Sahrens * 2103789Sahrens * RETURN: 0 (always succeeds) 2104789Sahrens */ 2105789Sahrens /* ARGSUSED */ 2106789Sahrens static int 21075331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 21085331Samw caller_context_t *ct) 2109789Sahrens { 2110789Sahrens znode_t *zp = VTOZ(vp); 2111789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 21125326Sek110237 znode_phys_t *pzp; 21135331Samw int error = 0; 21144543Smarks uint64_t links; 21155331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 21165331Samw xoptattr_t *xoap = NULL; 21175331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2118789Sahrens 21195367Sahrens ZFS_ENTER(zfsvfs); 21205367Sahrens ZFS_VERIFY_ZP(zp); 21215326Sek110237 pzp = zp->z_phys; 2122789Sahrens 21235331Samw mutex_enter(&zp->z_lock); 21245331Samw 21255331Samw /* 21265331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 21275331Samw * Also, if we are the owner don't bother, since owner should 21285331Samw * always be allowed to read basic attributes of file. 21295331Samw */ 21305331Samw if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) && 21315331Samw (pzp->zp_uid != crgetuid(cr))) { 21325331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 21335331Samw skipaclchk, cr)) { 21345331Samw mutex_exit(&zp->z_lock); 21355331Samw ZFS_EXIT(zfsvfs); 21365331Samw return (error); 21375331Samw } 21385331Samw } 21395331Samw 2140789Sahrens /* 2141789Sahrens * Return all attributes. It's cheaper to provide the answer 2142789Sahrens * than to determine whether we were asked the question. 2143789Sahrens */ 2144789Sahrens 2145789Sahrens vap->va_type = vp->v_type; 2146789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 21475771Sjp151216 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2148789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2149789Sahrens vap->va_nodeid = zp->z_id; 21504543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 21514543Smarks links = pzp->zp_links + 1; 21524543Smarks else 21534543Smarks links = pzp->zp_links; 21544543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2155789Sahrens vap->va_size = pzp->zp_size; 21561816Smarks vap->va_rdev = vp->v_rdev; 2157789Sahrens vap->va_seq = zp->z_seq; 2158789Sahrens 21595331Samw /* 21605331Samw * Add in any requested optional attributes and the create time. 21615331Samw * Also set the corresponding bits in the returned attribute bitmap. 21625331Samw */ 21635331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 21645331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 21655331Samw xoap->xoa_archive = 21665331Samw ((pzp->zp_flags & ZFS_ARCHIVE) != 0); 21675331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 21685331Samw } 21695331Samw 21705331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 21715331Samw xoap->xoa_readonly = 21725331Samw ((pzp->zp_flags & ZFS_READONLY) != 0); 21735331Samw XVA_SET_RTN(xvap, XAT_READONLY); 21745331Samw } 21755331Samw 21765331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 21775331Samw xoap->xoa_system = 21785331Samw ((pzp->zp_flags & ZFS_SYSTEM) != 0); 21795331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 21805331Samw } 21815331Samw 21825331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 21835331Samw xoap->xoa_hidden = 21845331Samw ((pzp->zp_flags & ZFS_HIDDEN) != 0); 21855331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 21865331Samw } 21875331Samw 21885331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 21895331Samw xoap->xoa_nounlink = 21905331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0); 21915331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 21925331Samw } 21935331Samw 21945331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 21955331Samw xoap->xoa_immutable = 21965331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0); 21975331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 21985331Samw } 21995331Samw 22005331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 22015331Samw xoap->xoa_appendonly = 22025331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0); 22035331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 22045331Samw } 22055331Samw 22065331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 22075331Samw xoap->xoa_nodump = 22085331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0); 22095331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 22105331Samw } 22115331Samw 22125331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 22135331Samw xoap->xoa_opaque = 22145331Samw ((pzp->zp_flags & ZFS_OPAQUE) != 0); 22155331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 22165331Samw } 22175331Samw 22185331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 22195331Samw xoap->xoa_av_quarantined = 22205331Samw ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0); 22215331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 22225331Samw } 22235331Samw 22245331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 22255331Samw xoap->xoa_av_modified = 22265331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0); 22275331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 22285331Samw } 22295331Samw 22305331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 22315331Samw vp->v_type == VREG && 22325331Samw (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) { 22335331Samw size_t len; 22345331Samw dmu_object_info_t doi; 22355331Samw 22365331Samw /* 22375331Samw * Only VREG files have anti-virus scanstamps, so we 22385331Samw * won't conflict with symlinks in the bonus buffer. 22395331Samw */ 22405331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 22415331Samw len = sizeof (xoap->xoa_av_scanstamp) + 22425331Samw sizeof (znode_phys_t); 22435331Samw if (len <= doi.doi_bonus_size) { 22445331Samw /* 22455331Samw * pzp points to the start of the 22465331Samw * znode_phys_t. pzp + 1 points to the 22475331Samw * first byte after the znode_phys_t. 22485331Samw */ 22495331Samw (void) memcpy(xoap->xoa_av_scanstamp, 22505331Samw pzp + 1, 22515331Samw sizeof (xoap->xoa_av_scanstamp)); 22525331Samw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 22535331Samw } 22545331Samw } 22555331Samw 22565331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 22575331Samw ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime); 22585331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 22595331Samw } 22605331Samw } 22615331Samw 2262789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2263789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2264789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2265789Sahrens 2266789Sahrens mutex_exit(&zp->z_lock); 2267789Sahrens 2268789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2269789Sahrens 2270789Sahrens if (zp->z_blksz == 0) { 2271789Sahrens /* 2272789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2273789Sahrens */ 2274789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2275789Sahrens } 2276789Sahrens 2277789Sahrens ZFS_EXIT(zfsvfs); 2278789Sahrens return (0); 2279789Sahrens } 2280789Sahrens 2281789Sahrens /* 2282789Sahrens * Set the file attributes to the values contained in the 2283789Sahrens * vattr structure. 2284789Sahrens * 2285789Sahrens * IN: vp - vnode of file to be modified. 2286789Sahrens * vap - new attribute values. 22875331Samw * If AT_XVATTR set, then optional attrs are being set 2288789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 22895331Samw * - ATTR_NOACLCHECK (CIFS context only). 2290789Sahrens * cr - credentials of caller. 22915331Samw * ct - caller context 2292789Sahrens * 2293789Sahrens * RETURN: 0 if success 2294789Sahrens * error code if failure 2295789Sahrens * 2296789Sahrens * Timestamps: 2297789Sahrens * vp - ctime updated, mtime updated if size changed. 2298789Sahrens */ 2299789Sahrens /* ARGSUSED */ 2300789Sahrens static int 2301789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2302789Sahrens caller_context_t *ct) 2303789Sahrens { 23045326Sek110237 znode_t *zp = VTOZ(vp); 23055326Sek110237 znode_phys_t *pzp; 2306789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23075326Sek110237 zilog_t *zilog; 2308789Sahrens dmu_tx_t *tx; 23091878Smaybee vattr_t oldva; 23108190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2311789Sahrens uint_t mask = vap->va_mask; 23121878Smaybee uint_t saved_mask; 23132796Smarks int trim_mask = 0; 2314789Sahrens uint64_t new_mode; 23151231Smarks znode_t *attrzp; 2316789Sahrens int need_policy = FALSE; 2317789Sahrens int err; 23185331Samw zfs_fuid_info_t *fuidp = NULL; 23195331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23205331Samw xoptattr_t *xoap; 23215824Smarks zfs_acl_t *aclp = NULL; 23225331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2323789Sahrens 2324789Sahrens if (mask == 0) 2325789Sahrens return (0); 2326789Sahrens 2327789Sahrens if (mask & AT_NOSET) 2328789Sahrens return (EINVAL); 2329789Sahrens 23305367Sahrens ZFS_ENTER(zfsvfs); 23315367Sahrens ZFS_VERIFY_ZP(zp); 23325331Samw 23335331Samw pzp = zp->z_phys; 23345331Samw zilog = zfsvfs->z_log; 23355331Samw 23365331Samw /* 23375331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 23385331Samw * that file system is at proper version level 23395331Samw */ 23405331Samw 23415331Samw if (zfsvfs->z_use_fuids == B_FALSE && 23425331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 23435331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 23445386Stimh (mask & AT_XVATTR))) { 23455386Stimh ZFS_EXIT(zfsvfs); 23465331Samw return (EINVAL); 23475386Stimh } 23485386Stimh 23495386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 23505386Stimh ZFS_EXIT(zfsvfs); 2351789Sahrens return (EISDIR); 23525386Stimh } 23535386Stimh 23545386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 23555386Stimh ZFS_EXIT(zfsvfs); 23561308Smarks return (EINVAL); 23575386Stimh } 23581308Smarks 23595331Samw /* 23605331Samw * If this is an xvattr_t, then get a pointer to the structure of 23615331Samw * optional attributes. If this is NULL, then we have a vattr_t. 23625331Samw */ 23635331Samw xoap = xva_getxoptattr(xvap); 23645331Samw 23658190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 23668190SMark.Shellenbaum@Sun.COM 23675331Samw /* 23685331Samw * Immutable files can only alter immutable bit and atime 23695331Samw */ 23705331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 23715331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 23725386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 23735386Stimh ZFS_EXIT(zfsvfs); 23745331Samw return (EPERM); 23755386Stimh } 23765386Stimh 23775386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 23785386Stimh ZFS_EXIT(zfsvfs); 23795331Samw return (EPERM); 23805386Stimh } 2381789Sahrens 23826064Smarks /* 23836064Smarks * Verify timestamps doesn't overflow 32 bits. 23846064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 23856064Smarks * handle times greater than 2039. This check should be removed 23866064Smarks * once large timestamps are fully supported. 23876064Smarks */ 23886064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 23896064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 23906064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 23916064Smarks ZFS_EXIT(zfsvfs); 23926064Smarks return (EOVERFLOW); 23936064Smarks } 23946064Smarks } 23956064Smarks 2396789Sahrens top: 23971231Smarks attrzp = NULL; 2398789Sahrens 2399789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2400789Sahrens ZFS_EXIT(zfsvfs); 2401789Sahrens return (EROFS); 2402789Sahrens } 2403789Sahrens 2404789Sahrens /* 2405789Sahrens * First validate permissions 2406789Sahrens */ 2407789Sahrens 2408789Sahrens if (mask & AT_SIZE) { 24095331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2410789Sahrens if (err) { 2411789Sahrens ZFS_EXIT(zfsvfs); 2412789Sahrens return (err); 2413789Sahrens } 24141878Smaybee /* 24151878Smaybee * XXX - Note, we are not providing any open 24161878Smaybee * mode flags here (like FNDELAY), so we may 24171878Smaybee * block if there are locks present... this 24181878Smaybee * should be addressed in openat(). 24191878Smaybee */ 24206992Smaybee /* XXX - would it be OK to generate a log record here? */ 24216992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 24221878Smaybee if (err) { 24231878Smaybee ZFS_EXIT(zfsvfs); 24241878Smaybee return (err); 24251878Smaybee } 2426789Sahrens } 2427789Sahrens 24285331Samw if (mask & (AT_ATIME|AT_MTIME) || 24295331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 24305331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 24315331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 24325331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 24335331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 24345331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 24355331Samw skipaclchk, cr); 2436789Sahrens 2437789Sahrens if (mask & (AT_UID|AT_GID)) { 2438789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2439789Sahrens int take_owner; 2440789Sahrens int take_group; 2441789Sahrens 2442789Sahrens /* 2443913Smarks * NOTE: even if a new mode is being set, 2444913Smarks * we may clear S_ISUID/S_ISGID bits. 2445913Smarks */ 2446913Smarks 2447913Smarks if (!(mask & AT_MODE)) 2448913Smarks vap->va_mode = pzp->zp_mode; 2449913Smarks 2450913Smarks /* 2451789Sahrens * Take ownership or chgrp to group we are a member of 2452789Sahrens */ 2453789Sahrens 2454789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 24555331Samw take_group = (mask & AT_GID) && 24565331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2457789Sahrens 2458789Sahrens /* 2459789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2460789Sahrens * take_group must both be set in order to allow taking 2461789Sahrens * ownership. 2462789Sahrens * 2463789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2464789Sahrens * 2465789Sahrens */ 2466789Sahrens 2467789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2468789Sahrens ((idmask == AT_UID) && take_owner) || 2469789Sahrens ((idmask == AT_GID) && take_group)) { 24705331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 24715331Samw skipaclchk, cr) == 0) { 2472789Sahrens /* 2473789Sahrens * Remove setuid/setgid for non-privileged users 2474789Sahrens */ 24751115Smarks secpolicy_setid_clear(vap, cr); 24762796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2477789Sahrens } else { 2478789Sahrens need_policy = TRUE; 2479789Sahrens } 2480789Sahrens } else { 2481789Sahrens need_policy = TRUE; 2482789Sahrens } 2483789Sahrens } 2484789Sahrens 24852796Smarks mutex_enter(&zp->z_lock); 24862796Smarks oldva.va_mode = pzp->zp_mode; 24875771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 24885331Samw if (mask & AT_XVATTR) { 24898190SMark.Shellenbaum@Sun.COM /* 24908190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 24918190SMark.Shellenbaum@Sun.COM * that are actually changing. 24928190SMark.Shellenbaum@Sun.COM * 24938190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 24948190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 24958190SMark.Shellenbaum@Sun.COM */ 24968190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 24978190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 24988190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) { 24998190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25008190SMark.Shellenbaum@Sun.COM } else { 25018190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 25028190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 25038190SMark.Shellenbaum@Sun.COM } 25048190SMark.Shellenbaum@Sun.COM } 25058190SMark.Shellenbaum@Sun.COM 25068190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 25078190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 25088190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) { 25098190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25108190SMark.Shellenbaum@Sun.COM } else { 25118190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 25128190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 25138190SMark.Shellenbaum@Sun.COM } 25148190SMark.Shellenbaum@Sun.COM } 25158190SMark.Shellenbaum@Sun.COM 25168190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 25178190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 25188190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) { 25198190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25208190SMark.Shellenbaum@Sun.COM } else { 25218190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 25228190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 25238190SMark.Shellenbaum@Sun.COM } 25248190SMark.Shellenbaum@Sun.COM } 25258190SMark.Shellenbaum@Sun.COM 25268190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 25278190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 25288190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NODUMP) != 0)) { 25298190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25308190SMark.Shellenbaum@Sun.COM } else { 25318190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 25328190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 25338190SMark.Shellenbaum@Sun.COM } 25348190SMark.Shellenbaum@Sun.COM } 25358190SMark.Shellenbaum@Sun.COM 25368190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 25378190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 25388190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) { 25398190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25408190SMark.Shellenbaum@Sun.COM } else { 25418190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 25428190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 25438190SMark.Shellenbaum@Sun.COM } 25448190SMark.Shellenbaum@Sun.COM } 25458190SMark.Shellenbaum@Sun.COM 25468190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 25478190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 25488190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 25498190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 25508190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) { 25518190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25528190SMark.Shellenbaum@Sun.COM } else { 25538190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 25548190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 25558190SMark.Shellenbaum@Sun.COM } 25568190SMark.Shellenbaum@Sun.COM } 25578190SMark.Shellenbaum@Sun.COM 25588190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 25598190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 25608190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 25615331Samw need_policy = TRUE; 25625331Samw } 25635331Samw } 25645331Samw 25652796Smarks mutex_exit(&zp->z_lock); 25662796Smarks 25672796Smarks if (mask & AT_MODE) { 25685331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 25692796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 25702796Smarks &oldva, cr); 25712796Smarks if (err) { 25722796Smarks ZFS_EXIT(zfsvfs); 25732796Smarks return (err); 25742796Smarks } 25752796Smarks trim_mask |= AT_MODE; 25762796Smarks } else { 25772796Smarks need_policy = TRUE; 25782796Smarks } 25792796Smarks } 2580789Sahrens 2581789Sahrens if (need_policy) { 25821115Smarks /* 25831115Smarks * If trim_mask is set then take ownership 25842796Smarks * has been granted or write_acl is present and user 25852796Smarks * has the ability to modify mode. In that case remove 25862796Smarks * UID|GID and or MODE from mask so that 25871115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 25881115Smarks */ 25892796Smarks 25902796Smarks if (trim_mask) { 25912796Smarks saved_mask = vap->va_mask; 25922796Smarks vap->va_mask &= ~trim_mask; 25932796Smarks } 2594789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 25955331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2596789Sahrens if (err) { 2597789Sahrens ZFS_EXIT(zfsvfs); 2598789Sahrens return (err); 2599789Sahrens } 26001115Smarks 26011115Smarks if (trim_mask) 26022796Smarks vap->va_mask |= saved_mask; 2603789Sahrens } 2604789Sahrens 2605789Sahrens /* 2606789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2607789Sahrens * changed va_mask 2608789Sahrens */ 2609789Sahrens mask = vap->va_mask; 2610789Sahrens 2611789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2612789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 26135824Smarks if (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26145824Smarks ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid))) { 26155824Smarks if (zfsvfs->z_fuid_obj == 0) { 26165824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 26175824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 26185824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 26195824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 26205824Smarks } else { 26215824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 26225824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 26235824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 26245824Smarks } 26255331Samw } 2626789Sahrens 2627789Sahrens if (mask & AT_MODE) { 26281576Smarks uint64_t pmode = pzp->zp_mode; 26291576Smarks 26301576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2631789Sahrens 26325824Smarks if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) { 26335824Smarks dmu_tx_abort(tx); 26345824Smarks ZFS_EXIT(zfsvfs); 26355824Smarks return (err); 26365824Smarks } 26375331Samw if (pzp->zp_acl.z_acl_extern_obj) { 26385331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 26395331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 26405331Samw pzp->zp_acl.z_acl_version == 26415331Samw ZFS_ACL_VERSION_INITIAL) { 26425331Samw dmu_tx_hold_free(tx, 26435331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26445331Samw DMU_OBJECT_END); 26455331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26465824Smarks 0, aclp->z_acl_bytes); 26475331Samw } else { 26485331Samw dmu_tx_hold_write(tx, 26495331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26505824Smarks aclp->z_acl_bytes); 26515331Samw } 26526180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 26536180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26546180Smarks 0, aclp->z_acl_bytes); 26555331Samw } 2656789Sahrens } 2657789Sahrens 26585331Samw if ((mask & (AT_UID | AT_GID)) && pzp->zp_xattr != 0) { 26595331Samw err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 26601231Smarks if (err) { 26611231Smarks dmu_tx_abort(tx); 26621231Smarks ZFS_EXIT(zfsvfs); 26635824Smarks if (aclp) 26645824Smarks zfs_acl_free(aclp); 26651231Smarks return (err); 26661231Smarks } 26671231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 26681231Smarks } 26691231Smarks 26708227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2671789Sahrens if (err) { 26721231Smarks if (attrzp) 26731231Smarks VN_RELE(ZTOV(attrzp)); 26745824Smarks 26755824Smarks if (aclp) { 26765824Smarks zfs_acl_free(aclp); 26775824Smarks aclp = NULL; 26785824Smarks } 26795824Smarks 26808227SNeil.Perrin@Sun.COM if (err == ERESTART) { 26812113Sahrens dmu_tx_wait(tx); 26822113Sahrens dmu_tx_abort(tx); 2683789Sahrens goto top; 2684789Sahrens } 26852113Sahrens dmu_tx_abort(tx); 2686789Sahrens ZFS_EXIT(zfsvfs); 2687789Sahrens return (err); 2688789Sahrens } 2689789Sahrens 2690789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2691789Sahrens 2692789Sahrens /* 2693789Sahrens * Set each attribute requested. 2694789Sahrens * We group settings according to the locks they need to acquire. 2695789Sahrens * 2696789Sahrens * Note: you cannot set ctime directly, although it will be 2697789Sahrens * updated as a side-effect of calling this function. 2698789Sahrens */ 2699789Sahrens 2700789Sahrens mutex_enter(&zp->z_lock); 2701789Sahrens 2702789Sahrens if (mask & AT_MODE) { 27035824Smarks mutex_enter(&zp->z_acl_lock); 27045824Smarks zp->z_phys->zp_mode = new_mode; 27055824Smarks err = zfs_aclset_common(zp, aclp, cr, &fuidp, tx); 2706789Sahrens ASSERT3U(err, ==, 0); 27075824Smarks mutex_exit(&zp->z_acl_lock); 2708789Sahrens } 2709789Sahrens 27101231Smarks if (attrzp) 27111231Smarks mutex_enter(&attrzp->z_lock); 27121231Smarks 27131231Smarks if (mask & AT_UID) { 27145331Samw pzp->zp_uid = zfs_fuid_create(zfsvfs, 27155771Sjp151216 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp); 27161231Smarks if (attrzp) { 27175331Samw attrzp->z_phys->zp_uid = zfs_fuid_create(zfsvfs, 27185771Sjp151216 vap->va_uid, cr, ZFS_OWNER, tx, &fuidp); 27191231Smarks } 27201231Smarks } 27211231Smarks 27221231Smarks if (mask & AT_GID) { 27235331Samw pzp->zp_gid = zfs_fuid_create(zfsvfs, vap->va_gid, 27245771Sjp151216 cr, ZFS_GROUP, tx, &fuidp); 27251231Smarks if (attrzp) 27265331Samw attrzp->z_phys->zp_gid = zfs_fuid_create(zfsvfs, 27275771Sjp151216 vap->va_gid, cr, ZFS_GROUP, tx, &fuidp); 27281231Smarks } 27291231Smarks 27305824Smarks if (aclp) 27315824Smarks zfs_acl_free(aclp); 27325824Smarks 27331231Smarks if (attrzp) 27341231Smarks mutex_exit(&attrzp->z_lock); 2735789Sahrens 2736789Sahrens if (mask & AT_ATIME) 2737789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2738789Sahrens 2739789Sahrens if (mask & AT_MTIME) 2740789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2741789Sahrens 27426992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 27431878Smaybee if (mask & AT_SIZE) 2744789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 27451878Smaybee else if (mask != 0) 2746789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 27475331Samw /* 27485331Samw * Do this after setting timestamps to prevent timestamp 27495331Samw * update from toggling bit 27505331Samw */ 27515331Samw 27525331Samw if (xoap && (mask & AT_XVATTR)) { 27538190SMark.Shellenbaum@Sun.COM 27548190SMark.Shellenbaum@Sun.COM /* 27558190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 27568190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 27578190SMark.Shellenbaum@Sun.COM */ 27588190SMark.Shellenbaum@Sun.COM 27598190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 27608190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 27618190SMark.Shellenbaum@Sun.COM } 27628190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 27638190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 27648190SMark.Shellenbaum@Sun.COM } 27658190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 27668190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 27678190SMark.Shellenbaum@Sun.COM } 27688190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 27698190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 27708190SMark.Shellenbaum@Sun.COM } 27718190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 27728190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 27738190SMark.Shellenbaum@Sun.COM } 27748190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 27758190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 27768190SMark.Shellenbaum@Sun.COM } 27778190SMark.Shellenbaum@Sun.COM 27785331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 27795331Samw size_t len; 27805331Samw dmu_object_info_t doi; 27815331Samw 27825331Samw ASSERT(vp->v_type == VREG); 27835331Samw 27845331Samw /* Grow the bonus buffer if necessary. */ 27855331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 27865331Samw len = sizeof (xoap->xoa_av_scanstamp) + 27875331Samw sizeof (znode_phys_t); 27885331Samw if (len > doi.doi_bonus_size) 27895331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 27905331Samw } 27915331Samw zfs_xvattr_set(zp, xvap); 27925331Samw } 2793789Sahrens 27941878Smaybee if (mask != 0) 27955331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 27965331Samw 27975331Samw if (fuidp) 27985331Samw zfs_fuid_info_free(fuidp); 2799789Sahrens mutex_exit(&zp->z_lock); 2800789Sahrens 28011231Smarks if (attrzp) 28021231Smarks VN_RELE(ZTOV(attrzp)); 28031231Smarks 2804789Sahrens dmu_tx_commit(tx); 2805789Sahrens 2806789Sahrens ZFS_EXIT(zfsvfs); 2807789Sahrens return (err); 2808789Sahrens } 2809789Sahrens 28103271Smaybee typedef struct zfs_zlock { 28113271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 28123271Smaybee znode_t *zl_znode; /* znode we held */ 28133271Smaybee struct zfs_zlock *zl_next; /* next in list */ 28143271Smaybee } zfs_zlock_t; 28153271Smaybee 28163271Smaybee /* 28173271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 28183271Smaybee */ 28193271Smaybee static void 28203271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 28213271Smaybee { 28223271Smaybee zfs_zlock_t *zl; 28233271Smaybee 28243271Smaybee while ((zl = *zlpp) != NULL) { 28253271Smaybee if (zl->zl_znode != NULL) 28263271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 28273271Smaybee rw_exit(zl->zl_rwlock); 28283271Smaybee *zlpp = zl->zl_next; 28293271Smaybee kmem_free(zl, sizeof (*zl)); 28303271Smaybee } 28313271Smaybee } 28323271Smaybee 2833789Sahrens /* 2834789Sahrens * Search back through the directory tree, using the ".." entries. 2835789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2836789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2837789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2838789Sahrens */ 2839789Sahrens static int 2840789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2841789Sahrens { 2842789Sahrens zfs_zlock_t *zl; 28433638Sbillm znode_t *zp = tdzp; 2844789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2845789Sahrens uint64_t *oidp = &zp->z_id; 2846789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2847789Sahrens krw_t rw = RW_WRITER; 2848789Sahrens 2849789Sahrens /* 2850789Sahrens * First pass write-locks szp and compares to zp->z_id. 2851789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2852789Sahrens */ 2853789Sahrens do { 28543271Smaybee if (!rw_tryenter(rwlp, rw)) { 28553271Smaybee /* 28563271Smaybee * Another thread is renaming in this path. 28573271Smaybee * Note that if we are a WRITER, we don't have any 28583271Smaybee * parent_locks held yet. 28593271Smaybee */ 28603271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 28613271Smaybee /* 28623271Smaybee * Drop our locks and restart 28633271Smaybee */ 28643271Smaybee zfs_rename_unlock(&zl); 28653271Smaybee *zlpp = NULL; 28663271Smaybee zp = tdzp; 28673271Smaybee oidp = &zp->z_id; 28683271Smaybee rwlp = &szp->z_parent_lock; 28693271Smaybee rw = RW_WRITER; 28703271Smaybee continue; 28713271Smaybee } else { 28723271Smaybee /* 28733271Smaybee * Wait for other thread to drop its locks 28743271Smaybee */ 28753271Smaybee rw_enter(rwlp, rw); 28763271Smaybee } 28773271Smaybee } 28783271Smaybee 2879789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2880789Sahrens zl->zl_rwlock = rwlp; 2881789Sahrens zl->zl_znode = NULL; 2882789Sahrens zl->zl_next = *zlpp; 2883789Sahrens *zlpp = zl; 2884789Sahrens 2885789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2886789Sahrens return (EINVAL); 2887789Sahrens 2888789Sahrens if (*oidp == rootid) /* We've hit the top */ 2889789Sahrens return (0); 2890789Sahrens 2891789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2892789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2893789Sahrens if (error) 2894789Sahrens return (error); 2895789Sahrens zl->zl_znode = zp; 2896789Sahrens } 2897789Sahrens oidp = &zp->z_phys->zp_parent; 2898789Sahrens rwlp = &zp->z_parent_lock; 2899789Sahrens rw = RW_READER; 2900789Sahrens 2901789Sahrens } while (zp->z_id != sdzp->z_id); 2902789Sahrens 2903789Sahrens return (0); 2904789Sahrens } 2905789Sahrens 2906789Sahrens /* 2907789Sahrens * Move an entry from the provided source directory to the target 2908789Sahrens * directory. Change the entry name as indicated. 2909789Sahrens * 2910789Sahrens * IN: sdvp - Source directory containing the "old entry". 2911789Sahrens * snm - Old entry name. 2912789Sahrens * tdvp - Target directory to contain the "new entry". 2913789Sahrens * tnm - New entry name. 2914789Sahrens * cr - credentials of caller. 29155331Samw * ct - caller context 29165331Samw * flags - case flags 2917789Sahrens * 2918789Sahrens * RETURN: 0 if success 2919789Sahrens * error code if failure 2920789Sahrens * 2921789Sahrens * Timestamps: 2922789Sahrens * sdvp,tdvp - ctime|mtime updated 2923789Sahrens */ 29245331Samw /*ARGSUSED*/ 2925789Sahrens static int 29265331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 29275331Samw caller_context_t *ct, int flags) 2928789Sahrens { 2929789Sahrens znode_t *tdzp, *szp, *tzp; 2930789Sahrens znode_t *sdzp = VTOZ(sdvp); 2931789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 29325326Sek110237 zilog_t *zilog; 2933789Sahrens vnode_t *realvp; 2934789Sahrens zfs_dirlock_t *sdl, *tdl; 2935789Sahrens dmu_tx_t *tx; 2936789Sahrens zfs_zlock_t *zl; 29375331Samw int cmp, serr, terr; 29385331Samw int error = 0; 29395331Samw int zflg = 0; 2940789Sahrens 29415367Sahrens ZFS_ENTER(zfsvfs); 29425367Sahrens ZFS_VERIFY_ZP(sdzp); 29435326Sek110237 zilog = zfsvfs->z_log; 2944789Sahrens 2945789Sahrens /* 2946789Sahrens * Make sure we have the real vp for the target directory. 2947789Sahrens */ 29485331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 2949789Sahrens tdvp = realvp; 2950789Sahrens 2951789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2952789Sahrens ZFS_EXIT(zfsvfs); 2953789Sahrens return (EXDEV); 2954789Sahrens } 2955789Sahrens 2956789Sahrens tdzp = VTOZ(tdvp); 29575367Sahrens ZFS_VERIFY_ZP(tdzp); 29585498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 29595331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 29605331Samw ZFS_EXIT(zfsvfs); 29615331Samw return (EILSEQ); 29625331Samw } 29635331Samw 29645331Samw if (flags & FIGNORECASE) 29655331Samw zflg |= ZCILOOK; 29665331Samw 2967789Sahrens top: 2968789Sahrens szp = NULL; 2969789Sahrens tzp = NULL; 2970789Sahrens zl = NULL; 2971789Sahrens 2972789Sahrens /* 2973789Sahrens * This is to prevent the creation of links into attribute space 2974789Sahrens * by renaming a linked file into/outof an attribute directory. 2975789Sahrens * See the comment in zfs_link() for why this is considered bad. 2976789Sahrens */ 2977789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2978789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2979789Sahrens ZFS_EXIT(zfsvfs); 2980789Sahrens return (EINVAL); 2981789Sahrens } 2982789Sahrens 2983789Sahrens /* 2984789Sahrens * Lock source and target directory entries. To prevent deadlock, 2985789Sahrens * a lock ordering must be defined. We lock the directory with 2986789Sahrens * the smallest object id first, or if it's a tie, the one with 2987789Sahrens * the lexically first name. 2988789Sahrens */ 2989789Sahrens if (sdzp->z_id < tdzp->z_id) { 2990789Sahrens cmp = -1; 2991789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2992789Sahrens cmp = 1; 2993789Sahrens } else { 29945331Samw /* 29955331Samw * First compare the two name arguments without 29965331Samw * considering any case folding. 29975331Samw */ 29985331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 29995331Samw 30005331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 30015498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3002789Sahrens if (cmp == 0) { 3003789Sahrens /* 3004789Sahrens * POSIX: "If the old argument and the new argument 3005789Sahrens * both refer to links to the same existing file, 3006789Sahrens * the rename() function shall return successfully 3007789Sahrens * and perform no other action." 3008789Sahrens */ 3009789Sahrens ZFS_EXIT(zfsvfs); 3010789Sahrens return (0); 3011789Sahrens } 30125331Samw /* 30135331Samw * If the file system is case-folding, then we may 30145331Samw * have some more checking to do. A case-folding file 30155331Samw * system is either supporting mixed case sensitivity 30165331Samw * access or is completely case-insensitive. Note 30175331Samw * that the file system is always case preserving. 30185331Samw * 30195331Samw * In mixed sensitivity mode case sensitive behavior 30205331Samw * is the default. FIGNORECASE must be used to 30215331Samw * explicitly request case insensitive behavior. 30225331Samw * 30235331Samw * If the source and target names provided differ only 30245331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 30255331Samw * we will treat this as a special case in the 30265331Samw * case-insensitive mode: as long as the source name 30275331Samw * is an exact match, we will allow this to proceed as 30285331Samw * a name-change request. 30295331Samw */ 30305498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 30315498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 30325498Stimh flags & FIGNORECASE)) && 30335331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 30345331Samw &error) == 0) { 30355331Samw /* 30365331Samw * case preserving rename request, require exact 30375331Samw * name matches 30385331Samw */ 30395331Samw zflg |= ZCIEXACT; 30405331Samw zflg &= ~ZCILOOK; 30415331Samw } 3042789Sahrens } 30435331Samw 3044789Sahrens if (cmp < 0) { 30455331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 30465331Samw ZEXISTS | zflg, NULL, NULL); 30475331Samw terr = zfs_dirent_lock(&tdl, 30485331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3049789Sahrens } else { 30505331Samw terr = zfs_dirent_lock(&tdl, 30515331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 30525331Samw serr = zfs_dirent_lock(&sdl, 30535331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 30545331Samw NULL, NULL); 3055789Sahrens } 3056789Sahrens 3057789Sahrens if (serr) { 3058789Sahrens /* 3059789Sahrens * Source entry invalid or not there. 3060789Sahrens */ 3061789Sahrens if (!terr) { 3062789Sahrens zfs_dirent_unlock(tdl); 3063789Sahrens if (tzp) 3064789Sahrens VN_RELE(ZTOV(tzp)); 3065789Sahrens } 3066789Sahrens if (strcmp(snm, "..") == 0) 3067789Sahrens serr = EINVAL; 3068789Sahrens ZFS_EXIT(zfsvfs); 3069789Sahrens return (serr); 3070789Sahrens } 3071789Sahrens if (terr) { 3072789Sahrens zfs_dirent_unlock(sdl); 3073789Sahrens VN_RELE(ZTOV(szp)); 3074789Sahrens if (strcmp(tnm, "..") == 0) 3075789Sahrens terr = EINVAL; 3076789Sahrens ZFS_EXIT(zfsvfs); 3077789Sahrens return (terr); 3078789Sahrens } 3079789Sahrens 3080789Sahrens /* 3081789Sahrens * Must have write access at the source to remove the old entry 3082789Sahrens * and write access at the target to create the new entry. 3083789Sahrens * Note that if target and source are the same, this can be 3084789Sahrens * done in a single check. 3085789Sahrens */ 3086789Sahrens 3087789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3088789Sahrens goto out; 3089789Sahrens 3090789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3091789Sahrens /* 3092789Sahrens * Check to make sure rename is valid. 3093789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3094789Sahrens */ 3095789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3096789Sahrens goto out; 3097789Sahrens } 3098789Sahrens 3099789Sahrens /* 3100789Sahrens * Does target exist? 3101789Sahrens */ 3102789Sahrens if (tzp) { 3103789Sahrens /* 3104789Sahrens * Source and target must be the same type. 3105789Sahrens */ 3106789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3107789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3108789Sahrens error = ENOTDIR; 3109789Sahrens goto out; 3110789Sahrens } 3111789Sahrens } else { 3112789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3113789Sahrens error = EISDIR; 3114789Sahrens goto out; 3115789Sahrens } 3116789Sahrens } 3117789Sahrens /* 3118789Sahrens * POSIX dictates that when the source and target 3119789Sahrens * entries refer to the same file object, rename 3120789Sahrens * must do nothing and exit without error. 3121789Sahrens */ 3122789Sahrens if (szp->z_id == tzp->z_id) { 3123789Sahrens error = 0; 3124789Sahrens goto out; 3125789Sahrens } 3126789Sahrens } 3127789Sahrens 31285331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3129789Sahrens if (tzp) 31305331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 31314863Spraks 31324863Spraks /* 31334863Spraks * notify the target directory if it is not the same 31344863Spraks * as source directory. 31354863Spraks */ 31364863Spraks if (tdvp != sdvp) { 31375331Samw vnevent_rename_dest_dir(tdvp, ct); 31384863Spraks } 3139789Sahrens 3140789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3141789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3142789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 31431544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 31441544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 31451544Seschrock if (sdzp != tdzp) 3146789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 31471544Seschrock if (tzp) 31481544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 31493461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 31508227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3151789Sahrens if (error) { 3152789Sahrens if (zl != NULL) 3153789Sahrens zfs_rename_unlock(&zl); 3154789Sahrens zfs_dirent_unlock(sdl); 3155789Sahrens zfs_dirent_unlock(tdl); 3156789Sahrens VN_RELE(ZTOV(szp)); 3157789Sahrens if (tzp) 3158789Sahrens VN_RELE(ZTOV(tzp)); 31598227SNeil.Perrin@Sun.COM if (error == ERESTART) { 31602113Sahrens dmu_tx_wait(tx); 31612113Sahrens dmu_tx_abort(tx); 3162789Sahrens goto top; 3163789Sahrens } 31642113Sahrens dmu_tx_abort(tx); 3165789Sahrens ZFS_EXIT(zfsvfs); 3166789Sahrens return (error); 3167789Sahrens } 3168789Sahrens 3169789Sahrens if (tzp) /* Attempt to remove the existing target */ 31705331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3171789Sahrens 3172789Sahrens if (error == 0) { 3173789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3174789Sahrens if (error == 0) { 31755331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 31765331Samw 3177789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3178789Sahrens ASSERT(error == 0); 31795331Samw 31805331Samw zfs_log_rename(zilog, tx, 31815331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 31825331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 31836976Seschrock 31846976Seschrock /* Update path information for the target vnode */ 31856976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3186789Sahrens } 3187789Sahrens } 3188789Sahrens 3189789Sahrens dmu_tx_commit(tx); 3190789Sahrens out: 3191789Sahrens if (zl != NULL) 3192789Sahrens zfs_rename_unlock(&zl); 3193789Sahrens 3194789Sahrens zfs_dirent_unlock(sdl); 3195789Sahrens zfs_dirent_unlock(tdl); 3196789Sahrens 3197789Sahrens VN_RELE(ZTOV(szp)); 3198789Sahrens if (tzp) 3199789Sahrens VN_RELE(ZTOV(tzp)); 3200789Sahrens 3201789Sahrens ZFS_EXIT(zfsvfs); 3202789Sahrens return (error); 3203789Sahrens } 3204789Sahrens 3205789Sahrens /* 3206789Sahrens * Insert the indicated symbolic reference entry into the directory. 3207789Sahrens * 3208789Sahrens * IN: dvp - Directory to contain new symbolic link. 3209789Sahrens * link - Name for new symlink entry. 3210789Sahrens * vap - Attributes of new entry. 3211789Sahrens * target - Target path of new symlink. 3212789Sahrens * cr - credentials of caller. 32135331Samw * ct - caller context 32145331Samw * flags - case flags 3215789Sahrens * 3216789Sahrens * RETURN: 0 if success 3217789Sahrens * error code if failure 3218789Sahrens * 3219789Sahrens * Timestamps: 3220789Sahrens * dvp - ctime|mtime updated 3221789Sahrens */ 32225331Samw /*ARGSUSED*/ 3223789Sahrens static int 32245331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 32255331Samw caller_context_t *ct, int flags) 3226789Sahrens { 3227789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3228789Sahrens zfs_dirlock_t *dl; 3229789Sahrens dmu_tx_t *tx; 3230789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 32315326Sek110237 zilog_t *zilog; 3232789Sahrens int len = strlen(link); 3233789Sahrens int error; 32345331Samw int zflg = ZNEW; 32355331Samw zfs_fuid_info_t *fuidp = NULL; 3236789Sahrens 3237789Sahrens ASSERT(vap->va_type == VLNK); 3238789Sahrens 32395367Sahrens ZFS_ENTER(zfsvfs); 32405367Sahrens ZFS_VERIFY_ZP(dzp); 32415326Sek110237 zilog = zfsvfs->z_log; 32425331Samw 32435498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 32445331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 32455331Samw ZFS_EXIT(zfsvfs); 32465331Samw return (EILSEQ); 32475331Samw } 32485331Samw if (flags & FIGNORECASE) 32495331Samw zflg |= ZCILOOK; 3250789Sahrens top: 32515331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3252789Sahrens ZFS_EXIT(zfsvfs); 3253789Sahrens return (error); 3254789Sahrens } 3255789Sahrens 3256789Sahrens if (len > MAXPATHLEN) { 3257789Sahrens ZFS_EXIT(zfsvfs); 3258789Sahrens return (ENAMETOOLONG); 3259789Sahrens } 3260789Sahrens 3261789Sahrens /* 3262789Sahrens * Attempt to lock directory; fail if entry already exists. 3263789Sahrens */ 32645331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 32655331Samw if (error) { 3266789Sahrens ZFS_EXIT(zfsvfs); 3267789Sahrens return (error); 3268789Sahrens } 3269789Sahrens 3270789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3271789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3272789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 32731544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3274789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 3275789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 32765824Smarks if (IS_EPHEMERAL(crgetuid(cr)) || IS_EPHEMERAL(crgetgid(cr))) { 32775824Smarks if (zfsvfs->z_fuid_obj == 0) { 32785824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 32795824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 32805824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32815824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 32825824Smarks } else { 32835824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 32845824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 32855824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32865824Smarks } 32875331Samw } 32888227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3289789Sahrens if (error) { 3290789Sahrens zfs_dirent_unlock(dl); 32918227SNeil.Perrin@Sun.COM if (error == ERESTART) { 32922113Sahrens dmu_tx_wait(tx); 32932113Sahrens dmu_tx_abort(tx); 3294789Sahrens goto top; 3295789Sahrens } 32962113Sahrens dmu_tx_abort(tx); 3297789Sahrens ZFS_EXIT(zfsvfs); 3298789Sahrens return (error); 3299789Sahrens } 3300789Sahrens 3301789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3302789Sahrens 3303789Sahrens /* 3304789Sahrens * Create a new object for the symlink. 3305789Sahrens * Put the link content into bonus buffer if it will fit; 3306789Sahrens * otherwise, store it just like any other file data. 3307789Sahrens */ 3308789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 33095446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, NULL, &fuidp); 3310789Sahrens if (len != 0) 3311789Sahrens bcopy(link, zp->z_phys + 1, len); 3312789Sahrens } else { 3313789Sahrens dmu_buf_t *dbp; 33141669Sperrin 33155446Sahrens zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, NULL, &fuidp); 33161669Sperrin /* 33171669Sperrin * Nothing can access the znode yet so no locking needed 33181669Sperrin * for growing the znode's blocksize. 33191669Sperrin */ 33201669Sperrin zfs_grow_blocksize(zp, len, tx); 3321789Sahrens 33225446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 33235446Sahrens zp->z_id, 0, FTAG, &dbp)); 3324789Sahrens dmu_buf_will_dirty(dbp, tx); 3325789Sahrens 3326789Sahrens ASSERT3U(len, <=, dbp->db_size); 3327789Sahrens bcopy(link, dbp->db_data, len); 33281544Seschrock dmu_buf_rele(dbp, FTAG); 3329789Sahrens } 3330789Sahrens zp->z_phys->zp_size = len; 3331789Sahrens 3332789Sahrens /* 3333789Sahrens * Insert the new object into the directory. 3334789Sahrens */ 3335789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 3336789Sahrens out: 33375331Samw if (error == 0) { 33385331Samw uint64_t txtype = TX_SYMLINK; 33395331Samw if (flags & FIGNORECASE) 33405331Samw txtype |= TX_CI; 33415331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 33425331Samw } 33435331Samw if (fuidp) 33445331Samw zfs_fuid_info_free(fuidp); 3345789Sahrens 3346789Sahrens dmu_tx_commit(tx); 3347789Sahrens 3348789Sahrens zfs_dirent_unlock(dl); 3349789Sahrens 3350789Sahrens VN_RELE(ZTOV(zp)); 3351789Sahrens 3352789Sahrens ZFS_EXIT(zfsvfs); 3353789Sahrens return (error); 3354789Sahrens } 3355789Sahrens 3356789Sahrens /* 3357789Sahrens * Return, in the buffer contained in the provided uio structure, 3358789Sahrens * the symbolic path referred to by vp. 3359789Sahrens * 3360789Sahrens * IN: vp - vnode of symbolic link. 3361789Sahrens * uoip - structure to contain the link path. 3362789Sahrens * cr - credentials of caller. 33635331Samw * ct - caller context 3364789Sahrens * 3365789Sahrens * OUT: uio - structure to contain the link path. 3366789Sahrens * 3367789Sahrens * RETURN: 0 if success 3368789Sahrens * error code if failure 3369789Sahrens * 3370789Sahrens * Timestamps: 3371789Sahrens * vp - atime updated 3372789Sahrens */ 3373789Sahrens /* ARGSUSED */ 3374789Sahrens static int 33755331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3376789Sahrens { 3377789Sahrens znode_t *zp = VTOZ(vp); 3378789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3379789Sahrens size_t bufsz; 3380789Sahrens int error; 3381789Sahrens 33825367Sahrens ZFS_ENTER(zfsvfs); 33835367Sahrens ZFS_VERIFY_ZP(zp); 3384789Sahrens 3385789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3386789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3387789Sahrens error = uiomove(zp->z_phys + 1, 3388789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3389789Sahrens } else { 33901544Seschrock dmu_buf_t *dbp; 33911544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 33921544Seschrock if (error) { 3393789Sahrens ZFS_EXIT(zfsvfs); 3394789Sahrens return (error); 3395789Sahrens } 3396789Sahrens error = uiomove(dbp->db_data, 3397789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 33981544Seschrock dmu_buf_rele(dbp, FTAG); 3399789Sahrens } 3400789Sahrens 3401789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3402789Sahrens ZFS_EXIT(zfsvfs); 3403789Sahrens return (error); 3404789Sahrens } 3405789Sahrens 3406789Sahrens /* 3407789Sahrens * Insert a new entry into directory tdvp referencing svp. 3408789Sahrens * 3409789Sahrens * IN: tdvp - Directory to contain new entry. 3410789Sahrens * svp - vnode of new entry. 3411789Sahrens * name - name of new entry. 3412789Sahrens * cr - credentials of caller. 34135331Samw * ct - caller context 3414789Sahrens * 3415789Sahrens * RETURN: 0 if success 3416789Sahrens * error code if failure 3417789Sahrens * 3418789Sahrens * Timestamps: 3419789Sahrens * tdvp - ctime|mtime updated 3420789Sahrens * svp - ctime updated 3421789Sahrens */ 3422789Sahrens /* ARGSUSED */ 3423789Sahrens static int 34245331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 34255331Samw caller_context_t *ct, int flags) 3426789Sahrens { 3427789Sahrens znode_t *dzp = VTOZ(tdvp); 3428789Sahrens znode_t *tzp, *szp; 3429789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 34305326Sek110237 zilog_t *zilog; 3431789Sahrens zfs_dirlock_t *dl; 3432789Sahrens dmu_tx_t *tx; 3433789Sahrens vnode_t *realvp; 3434789Sahrens int error; 34355331Samw int zf = ZNEW; 34365331Samw uid_t owner; 3437789Sahrens 3438789Sahrens ASSERT(tdvp->v_type == VDIR); 3439789Sahrens 34405367Sahrens ZFS_ENTER(zfsvfs); 34415367Sahrens ZFS_VERIFY_ZP(dzp); 34425326Sek110237 zilog = zfsvfs->z_log; 3443789Sahrens 34445331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3445789Sahrens svp = realvp; 3446789Sahrens 3447789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3448789Sahrens ZFS_EXIT(zfsvfs); 3449789Sahrens return (EXDEV); 3450789Sahrens } 34515367Sahrens szp = VTOZ(svp); 34525367Sahrens ZFS_VERIFY_ZP(szp); 3453789Sahrens 34545498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 34555331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 34565331Samw ZFS_EXIT(zfsvfs); 34575331Samw return (EILSEQ); 34585331Samw } 34595331Samw if (flags & FIGNORECASE) 34605331Samw zf |= ZCILOOK; 34615331Samw 3462789Sahrens top: 3463789Sahrens /* 3464789Sahrens * We do not support links between attributes and non-attributes 3465789Sahrens * because of the potential security risk of creating links 3466789Sahrens * into "normal" file space in order to circumvent restrictions 3467789Sahrens * imposed in attribute space. 3468789Sahrens */ 3469789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3470789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3471789Sahrens ZFS_EXIT(zfsvfs); 3472789Sahrens return (EINVAL); 3473789Sahrens } 3474789Sahrens 3475789Sahrens /* 3476789Sahrens * POSIX dictates that we return EPERM here. 3477789Sahrens * Better choices include ENOTSUP or EISDIR. 3478789Sahrens */ 3479789Sahrens if (svp->v_type == VDIR) { 3480789Sahrens ZFS_EXIT(zfsvfs); 3481789Sahrens return (EPERM); 3482789Sahrens } 3483789Sahrens 34845959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 34855331Samw if (owner != crgetuid(cr) && 3486789Sahrens secpolicy_basic_link(cr) != 0) { 3487789Sahrens ZFS_EXIT(zfsvfs); 3488789Sahrens return (EPERM); 3489789Sahrens } 3490789Sahrens 34915331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3492789Sahrens ZFS_EXIT(zfsvfs); 3493789Sahrens return (error); 3494789Sahrens } 3495789Sahrens 3496789Sahrens /* 3497789Sahrens * Attempt to lock directory; fail if entry already exists. 3498789Sahrens */ 34995331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 35005331Samw if (error) { 3501789Sahrens ZFS_EXIT(zfsvfs); 3502789Sahrens return (error); 3503789Sahrens } 3504789Sahrens 3505789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3506789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 35071544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 35088227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3509789Sahrens if (error) { 3510789Sahrens zfs_dirent_unlock(dl); 35118227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35122113Sahrens dmu_tx_wait(tx); 35132113Sahrens dmu_tx_abort(tx); 3514789Sahrens goto top; 3515789Sahrens } 35162113Sahrens dmu_tx_abort(tx); 3517789Sahrens ZFS_EXIT(zfsvfs); 3518789Sahrens return (error); 3519789Sahrens } 3520789Sahrens 3521789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3522789Sahrens 35235331Samw if (error == 0) { 35245331Samw uint64_t txtype = TX_LINK; 35255331Samw if (flags & FIGNORECASE) 35265331Samw txtype |= TX_CI; 35275331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 35285331Samw } 3529789Sahrens 3530789Sahrens dmu_tx_commit(tx); 3531789Sahrens 3532789Sahrens zfs_dirent_unlock(dl); 3533789Sahrens 35344863Spraks if (error == 0) { 35355331Samw vnevent_link(svp, ct); 35364863Spraks } 35374863Spraks 3538789Sahrens ZFS_EXIT(zfsvfs); 3539789Sahrens return (error); 3540789Sahrens } 3541789Sahrens 3542789Sahrens /* 3543789Sahrens * zfs_null_putapage() is used when the file system has been force 3544789Sahrens * unmounted. It just drops the pages. 3545789Sahrens */ 3546789Sahrens /* ARGSUSED */ 3547789Sahrens static int 3548789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3549789Sahrens size_t *lenp, int flags, cred_t *cr) 3550789Sahrens { 3551789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3552789Sahrens return (0); 3553789Sahrens } 3554789Sahrens 35552688Smaybee /* 35562688Smaybee * Push a page out to disk, klustering if possible. 35572688Smaybee * 35582688Smaybee * IN: vp - file to push page to. 35592688Smaybee * pp - page to push. 35602688Smaybee * flags - additional flags. 35612688Smaybee * cr - credentials of caller. 35622688Smaybee * 35632688Smaybee * OUT: offp - start of range pushed. 35642688Smaybee * lenp - len of range pushed. 35652688Smaybee * 35662688Smaybee * RETURN: 0 if success 35672688Smaybee * error code if failure 35682688Smaybee * 35692688Smaybee * NOTE: callers must have locked the page to be pushed. On 35702688Smaybee * exit, the page (and all other pages in the kluster) must be 35712688Smaybee * unlocked. 35722688Smaybee */ 3573789Sahrens /* ARGSUSED */ 3574789Sahrens static int 3575789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3576789Sahrens size_t *lenp, int flags, cred_t *cr) 3577789Sahrens { 3578789Sahrens znode_t *zp = VTOZ(vp); 3579789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3580789Sahrens dmu_tx_t *tx; 35812688Smaybee u_offset_t off, koff; 35822688Smaybee size_t len, klen; 35834709Smaybee uint64_t filesz; 3584789Sahrens int err; 3585789Sahrens 35864709Smaybee filesz = zp->z_phys->zp_size; 35872688Smaybee off = pp->p_offset; 35882688Smaybee len = PAGESIZE; 35892688Smaybee /* 35902688Smaybee * If our blocksize is bigger than the page size, try to kluster 35918227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 35922688Smaybee * a read-modify-write). 35932688Smaybee */ 35944709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 35958636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 35968636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 35972688Smaybee ASSERT(koff <= filesz); 35982688Smaybee if (koff + klen > filesz) 35992688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 36002688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 36012688Smaybee } 36022688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 36038636SMark.Maybee@Sun.COM 36041819Smaybee /* 36051819Smaybee * Can't push pages past end-of-file. 36061819Smaybee */ 36074709Smaybee if (off >= filesz) { 36084709Smaybee /* ignore all pages */ 36092688Smaybee err = 0; 36102688Smaybee goto out; 36114709Smaybee } else if (off + len > filesz) { 36124709Smaybee int npages = btopr(filesz - off); 36132688Smaybee page_t *trunc; 36142688Smaybee 36152688Smaybee page_list_break(&pp, &trunc, npages); 36164709Smaybee /* ignore pages past end of file */ 36172688Smaybee if (trunc) 36184709Smaybee pvn_write_done(trunc, flags); 36194709Smaybee len = filesz - off; 36201819Smaybee } 36218636SMark.Maybee@Sun.COM top: 3622789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3623789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3624789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 36258227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 3626789Sahrens if (err != 0) { 36278227SNeil.Perrin@Sun.COM if (err == ERESTART) { 36282113Sahrens dmu_tx_wait(tx); 36292113Sahrens dmu_tx_abort(tx); 3630789Sahrens goto top; 3631789Sahrens } 36322113Sahrens dmu_tx_abort(tx); 3633789Sahrens goto out; 3634789Sahrens } 3635789Sahrens 36362688Smaybee if (zp->z_blksz <= PAGESIZE) { 36377315SJonathan.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); 36407315SJonathan.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); 36478636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 36482688Smaybee dmu_tx_commit(tx); 36492688Smaybee } 36502688Smaybee 36512688Smaybee out: 36524709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3653789Sahrens if (offp) 3654789Sahrens *offp = off; 3655789Sahrens if (lenp) 3656789Sahrens *lenp = len; 3657789Sahrens 3658789Sahrens return (err); 3659789Sahrens } 3660789Sahrens 3661789Sahrens /* 3662789Sahrens * Copy the portion of the file indicated from pages into the file. 3663789Sahrens * The pages are stored in a page list attached to the files vnode. 3664789Sahrens * 3665789Sahrens * IN: vp - vnode of file to push page data to. 3666789Sahrens * off - position in file to put data. 3667789Sahrens * len - amount of data to write. 3668789Sahrens * flags - flags to control the operation. 3669789Sahrens * cr - credentials of caller. 36705331Samw * ct - caller context. 3671789Sahrens * 3672789Sahrens * RETURN: 0 if success 3673789Sahrens * error code if failure 3674789Sahrens * 3675789Sahrens * Timestamps: 3676789Sahrens * vp - ctime|mtime updated 3677789Sahrens */ 36785331Samw /*ARGSUSED*/ 3679789Sahrens static int 36805331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 36815331Samw caller_context_t *ct) 3682789Sahrens { 3683789Sahrens znode_t *zp = VTOZ(vp); 3684789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3685789Sahrens page_t *pp; 3686789Sahrens size_t io_len; 3687789Sahrens u_offset_t io_off; 36888636SMark.Maybee@Sun.COM uint_t blksz; 36898636SMark.Maybee@Sun.COM rl_t *rl; 3690789Sahrens int error = 0; 3691789Sahrens 36925367Sahrens ZFS_ENTER(zfsvfs); 36935367Sahrens ZFS_VERIFY_ZP(zp); 3694789Sahrens 36958636SMark.Maybee@Sun.COM /* 36968636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 36978636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 36988636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 36998636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 37008636SMark.Maybee@Sun.COM * operations to get narrower locking. 37018636SMark.Maybee@Sun.COM */ 37028636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 37038636SMark.Maybee@Sun.COM if (ISP2(blksz)) 37048636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 37058636SMark.Maybee@Sun.COM else 37068636SMark.Maybee@Sun.COM io_off = 0; 37078636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 37088636SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (io_off - off), blksz, size_t); 37098636SMark.Maybee@Sun.COM else 37108636SMark.Maybee@Sun.COM io_len = 0; 37118636SMark.Maybee@Sun.COM 37128636SMark.Maybee@Sun.COM if (io_len == 0) { 3713789Sahrens /* 37148636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 3715789Sahrens */ 37168636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 37178636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 37181472Sperrin goto out; 3719789Sahrens } 37208636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 37218636SMark.Maybee@Sun.COM 37228636SMark.Maybee@Sun.COM if (off > zp->z_phys->zp_size) { 3723789Sahrens /* past end of file */ 37248636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 3725789Sahrens ZFS_EXIT(zfsvfs); 3726789Sahrens return (0); 3727789Sahrens } 3728789Sahrens 37298636SMark.Maybee@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off); 37308636SMark.Maybee@Sun.COM 37318636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 3732789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 37331669Sperrin pp = page_lookup(vp, io_off, 37344339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3735789Sahrens } else { 3736789Sahrens pp = page_lookup_nowait(vp, io_off, 37374339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3738789Sahrens } 3739789Sahrens 3740789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3741789Sahrens int err; 3742789Sahrens 3743789Sahrens /* 3744789Sahrens * Found a dirty page to push 3745789Sahrens */ 37461669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 37471669Sperrin if (err) 3748789Sahrens error = err; 3749789Sahrens } else { 3750789Sahrens io_len = PAGESIZE; 3751789Sahrens } 3752789Sahrens } 37531472Sperrin out: 37548636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 37552638Sperrin if ((flags & B_ASYNC) == 0) 37562638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3757789Sahrens ZFS_EXIT(zfsvfs); 3758789Sahrens return (error); 3759789Sahrens } 3760789Sahrens 37615331Samw /*ARGSUSED*/ 3762789Sahrens void 37635331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3764789Sahrens { 3765789Sahrens znode_t *zp = VTOZ(vp); 3766789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3767789Sahrens int error; 3768789Sahrens 37695326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 37705446Sahrens if (zp->z_dbuf == NULL) { 37715446Sahrens /* 37725642Smaybee * The fs has been unmounted, or we did a 37735642Smaybee * suspend/resume and this file no longer exists. 37745446Sahrens */ 3775789Sahrens if (vn_has_cached_data(vp)) { 3776789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3777789Sahrens B_INVAL, cr); 3778789Sahrens } 3779789Sahrens 37801544Seschrock mutex_enter(&zp->z_lock); 3781789Sahrens vp->v_count = 0; /* count arrives as 1 */ 37825446Sahrens mutex_exit(&zp->z_lock); 37835642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 37845446Sahrens zfs_znode_free(zp); 3785789Sahrens return; 3786789Sahrens } 3787789Sahrens 3788789Sahrens /* 3789789Sahrens * Attempt to push any data in the page cache. If this fails 3790789Sahrens * we will get kicked out later in zfs_zinactive(). 3791789Sahrens */ 37921298Sperrin if (vn_has_cached_data(vp)) { 37931298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 37941298Sperrin cr); 37951298Sperrin } 3796789Sahrens 37973461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3798789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3799789Sahrens 3800789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3801789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3802789Sahrens if (error) { 3803789Sahrens dmu_tx_abort(tx); 3804789Sahrens } else { 3805789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3806789Sahrens mutex_enter(&zp->z_lock); 3807789Sahrens zp->z_atime_dirty = 0; 3808789Sahrens mutex_exit(&zp->z_lock); 3809789Sahrens dmu_tx_commit(tx); 3810789Sahrens } 3811789Sahrens } 3812789Sahrens 3813789Sahrens zfs_zinactive(zp); 38145326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3815789Sahrens } 3816789Sahrens 3817789Sahrens /* 3818789Sahrens * Bounds-check the seek operation. 3819789Sahrens * 3820789Sahrens * IN: vp - vnode seeking within 3821789Sahrens * ooff - old file offset 3822789Sahrens * noffp - pointer to new file offset 38235331Samw * ct - caller context 3824789Sahrens * 3825789Sahrens * RETURN: 0 if success 3826789Sahrens * EINVAL if new offset invalid 3827789Sahrens */ 3828789Sahrens /* ARGSUSED */ 3829789Sahrens static int 38305331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 38315331Samw caller_context_t *ct) 3832789Sahrens { 3833789Sahrens if (vp->v_type == VDIR) 3834789Sahrens return (0); 3835789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3836789Sahrens } 3837789Sahrens 3838789Sahrens /* 3839789Sahrens * Pre-filter the generic locking function to trap attempts to place 3840789Sahrens * a mandatory lock on a memory mapped file. 3841789Sahrens */ 3842789Sahrens static int 3843789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 38445331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 3845789Sahrens { 3846789Sahrens znode_t *zp = VTOZ(vp); 3847789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3848789Sahrens int error; 3849789Sahrens 38505367Sahrens ZFS_ENTER(zfsvfs); 38515367Sahrens ZFS_VERIFY_ZP(zp); 3852789Sahrens 3853789Sahrens /* 38541544Seschrock * We are following the UFS semantics with respect to mapcnt 38551544Seschrock * here: If we see that the file is mapped already, then we will 38561544Seschrock * return an error, but we don't worry about races between this 38571544Seschrock * function and zfs_map(). 3858789Sahrens */ 38591544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3860789Sahrens ZFS_EXIT(zfsvfs); 3861789Sahrens return (EAGAIN); 3862789Sahrens } 38635331Samw error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct); 3864789Sahrens ZFS_EXIT(zfsvfs); 3865789Sahrens return (error); 3866789Sahrens } 3867789Sahrens 3868789Sahrens /* 3869789Sahrens * If we can't find a page in the cache, we will create a new page 3870789Sahrens * and fill it with file data. For efficiency, we may try to fill 38718636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 38728636SMark.Maybee@Sun.COM * list. 3873789Sahrens */ 3874789Sahrens static int 3875789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3876789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3877789Sahrens { 3878789Sahrens znode_t *zp = VTOZ(vp); 3879789Sahrens page_t *pp, *cur_pp; 3880789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3881789Sahrens u_offset_t io_off, total; 3882789Sahrens size_t io_len; 3883789Sahrens int err; 3884789Sahrens 38852688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 38868636SMark.Maybee@Sun.COM /* 38878636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 38888636SMark.Maybee@Sun.COM */ 3889789Sahrens io_off = off; 3890789Sahrens io_len = PAGESIZE; 3891789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3892789Sahrens } else { 3893789Sahrens /* 38948636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 3895789Sahrens */ 3896789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 38978636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 3898789Sahrens } 3899789Sahrens if (pp == NULL) { 3900789Sahrens /* 39018636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 3902789Sahrens */ 3903789Sahrens *pl = NULL; 3904789Sahrens return (0); 3905789Sahrens } 3906789Sahrens 3907789Sahrens /* 3908789Sahrens * Fill the pages in the kluster. 3909789Sahrens */ 3910789Sahrens cur_pp = pp; 3911789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 39128636SMark.Maybee@Sun.COM caddr_t va; 39138636SMark.Maybee@Sun.COM 39142688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 39157315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 39168636SMark.Maybee@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va); 39177315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 3918789Sahrens if (err) { 3919789Sahrens /* On error, toss the entire kluster */ 3920789Sahrens pvn_read_done(pp, B_ERROR); 39217294Sperrin /* convert checksum errors into IO errors */ 39227294Sperrin if (err == ECKSUM) 39237294Sperrin err = EIO; 3924789Sahrens return (err); 3925789Sahrens } 3926789Sahrens cur_pp = cur_pp->p_next; 3927789Sahrens } 39288636SMark.Maybee@Sun.COM 3929789Sahrens /* 39308636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 39318636SMark.Maybee@Sun.COM * from the desired offset `off'. 3932789Sahrens * NOTE: the page list will always be null terminated. 3933789Sahrens */ 3934789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 39358636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 3936789Sahrens 3937789Sahrens return (0); 3938789Sahrens } 3939789Sahrens 3940789Sahrens /* 3941789Sahrens * Return pointers to the pages for the file region [off, off + len] 3942789Sahrens * in the pl array. If plsz is greater than len, this function may 39438636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 39448636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 39458636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 39468636SMark.Maybee@Sun.COM * part of a klustered read. 3947789Sahrens * 3948789Sahrens * IN: vp - vnode of file to get data from. 3949789Sahrens * off - position in file to get data from. 3950789Sahrens * len - amount of data to retrieve. 3951789Sahrens * plsz - length of provided page list. 3952789Sahrens * seg - segment to obtain pages for. 3953789Sahrens * addr - virtual address of fault. 3954789Sahrens * rw - mode of created pages. 3955789Sahrens * cr - credentials of caller. 39565331Samw * ct - caller context. 3957789Sahrens * 3958789Sahrens * OUT: protp - protection mode of created pages. 3959789Sahrens * pl - list of pages created. 3960789Sahrens * 3961789Sahrens * RETURN: 0 if success 3962789Sahrens * error code if failure 3963789Sahrens * 3964789Sahrens * Timestamps: 3965789Sahrens * vp - atime updated 3966789Sahrens */ 3967789Sahrens /* ARGSUSED */ 3968789Sahrens static int 3969789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3970789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 39715331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 3972789Sahrens { 3973789Sahrens znode_t *zp = VTOZ(vp); 3974789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 39758636SMark.Maybee@Sun.COM page_t **pl0 = pl; 39768636SMark.Maybee@Sun.COM int err = 0; 39778636SMark.Maybee@Sun.COM 39788636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 39798636SMark.Maybee@Sun.COM if (pl == NULL) 39808636SMark.Maybee@Sun.COM return (0); 39818636SMark.Maybee@Sun.COM else if (len > plsz) 39828636SMark.Maybee@Sun.COM len = plsz; 3983*8681SMark.Maybee@Sun.COM else 3984*8681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 39858636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 3986789Sahrens 39875367Sahrens ZFS_ENTER(zfsvfs); 39885367Sahrens ZFS_VERIFY_ZP(zp); 3989789Sahrens 3990789Sahrens if (protp) 3991789Sahrens *protp = PROT_ALL; 3992789Sahrens 3993789Sahrens /* 3994789Sahrens * Loop through the requested range [off, off + len] looking 3995789Sahrens * for pages. If we don't find a page, we will need to create 3996789Sahrens * a new page and fill it with data from the file. 3997789Sahrens */ 3998789Sahrens while (len > 0) { 39998636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 40008636SMark.Maybee@Sun.COM *(pl+1) = NULL; 40018636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 40028636SMark.Maybee@Sun.COM goto out; 40038636SMark.Maybee@Sun.COM while (*pl) { 40048636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4005789Sahrens off += PAGESIZE; 4006789Sahrens addr += PAGESIZE; 4007*8681SMark.Maybee@Sun.COM if (len > 0) { 4008*8681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 40098636SMark.Maybee@Sun.COM len -= PAGESIZE; 4010*8681SMark.Maybee@Sun.COM } 40118636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4012789Sahrens plsz -= PAGESIZE; 40138636SMark.Maybee@Sun.COM pl++; 4014789Sahrens } 4015789Sahrens } 4016789Sahrens 4017789Sahrens /* 4018789Sahrens * Fill out the page array with any pages already in the cache. 4019789Sahrens */ 40208636SMark.Maybee@Sun.COM while (plsz > 0 && 40218636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 40228636SMark.Maybee@Sun.COM off += PAGESIZE; 40238636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4024789Sahrens } 4025789Sahrens out: 40262752Sperrin if (err) { 40272752Sperrin /* 40282752Sperrin * Release any pages we have previously locked. 40292752Sperrin */ 40302752Sperrin while (pl > pl0) 40312752Sperrin page_unlock(*--pl); 40328636SMark.Maybee@Sun.COM } else { 40338636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 40342752Sperrin } 40352752Sperrin 4036789Sahrens *pl = NULL; 4037789Sahrens 4038789Sahrens ZFS_EXIT(zfsvfs); 4039789Sahrens return (err); 4040789Sahrens } 4041789Sahrens 40421544Seschrock /* 40431544Seschrock * Request a memory map for a section of a file. This code interacts 40441544Seschrock * with common code and the VM system as follows: 40451544Seschrock * 40461544Seschrock * common code calls mmap(), which ends up in smmap_common() 40471544Seschrock * 40481544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 40491544Seschrock * 40501544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 40511544Seschrock * 40521544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 40531544Seschrock * 40541544Seschrock * zfs_addmap() updates z_mapcnt 40551544Seschrock */ 40565331Samw /*ARGSUSED*/ 4057789Sahrens static int 4058789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 40595331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 40605331Samw caller_context_t *ct) 4061789Sahrens { 4062789Sahrens znode_t *zp = VTOZ(vp); 4063789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4064789Sahrens segvn_crargs_t vn_a; 4065789Sahrens int error; 4066789Sahrens 40675929Smarks ZFS_ENTER(zfsvfs); 40685929Smarks ZFS_VERIFY_ZP(zp); 40695929Smarks 40705331Samw if ((prot & PROT_WRITE) && 40715331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 40725929Smarks ZFS_APPENDONLY))) { 40735929Smarks ZFS_EXIT(zfsvfs); 40745331Samw return (EPERM); 40755929Smarks } 40765929Smarks 40775929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 40785929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 40795929Smarks ZFS_EXIT(zfsvfs); 40805929Smarks return (EACCES); 40815929Smarks } 4082789Sahrens 4083789Sahrens if (vp->v_flag & VNOMAP) { 4084789Sahrens ZFS_EXIT(zfsvfs); 4085789Sahrens return (ENOSYS); 4086789Sahrens } 4087789Sahrens 4088789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4089789Sahrens ZFS_EXIT(zfsvfs); 4090789Sahrens return (ENXIO); 4091789Sahrens } 4092789Sahrens 4093789Sahrens if (vp->v_type != VREG) { 4094789Sahrens ZFS_EXIT(zfsvfs); 4095789Sahrens return (ENODEV); 4096789Sahrens } 4097789Sahrens 4098789Sahrens /* 4099789Sahrens * If file is locked, disallow mapping. 4100789Sahrens */ 41011544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 41021544Seschrock ZFS_EXIT(zfsvfs); 41031544Seschrock return (EAGAIN); 4104789Sahrens } 4105789Sahrens 4106789Sahrens as_rangelock(as); 41076036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 41086036Smec if (error != 0) { 41096036Smec as_rangeunlock(as); 41106036Smec ZFS_EXIT(zfsvfs); 41116036Smec return (error); 4112789Sahrens } 4113789Sahrens 4114789Sahrens vn_a.vp = vp; 4115789Sahrens vn_a.offset = (u_offset_t)off; 4116789Sahrens vn_a.type = flags & MAP_TYPE; 4117789Sahrens vn_a.prot = prot; 4118789Sahrens vn_a.maxprot = maxprot; 4119789Sahrens vn_a.cred = cr; 4120789Sahrens vn_a.amp = NULL; 4121789Sahrens vn_a.flags = flags & ~MAP_TYPE; 41221417Skchow vn_a.szc = 0; 41231417Skchow vn_a.lgrp_mem_policy_flags = 0; 4124789Sahrens 4125789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4126789Sahrens 4127789Sahrens as_rangeunlock(as); 4128789Sahrens ZFS_EXIT(zfsvfs); 4129789Sahrens return (error); 4130789Sahrens } 4131789Sahrens 4132789Sahrens /* ARGSUSED */ 4133789Sahrens static int 4134789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41355331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 41365331Samw caller_context_t *ct) 4137789Sahrens { 41381544Seschrock uint64_t pages = btopr(len); 41391544Seschrock 41401544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4141789Sahrens return (0); 4142789Sahrens } 4143789Sahrens 41441773Seschrock /* 41451773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 41461773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 41471773Seschrock * detecting when the data was actually modified, we have to resort to 41481773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 41491773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 41501773Seschrock * which by far the most common case: 41511773Seschrock * 41521773Seschrock * open() 41531773Seschrock * mmap() 41541773Seschrock * <modify memory> 41551773Seschrock * munmap() 41561773Seschrock * close() 41571773Seschrock * <time lapse> 41581773Seschrock * putpage() via fsflush 41591773Seschrock * 41601773Seschrock * If we wait until fsflush to come along, we can have a modification time that 41611773Seschrock * is some arbitrary point in the future. In order to prevent this in the 41621773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 41631773Seschrock * torn down. 41641773Seschrock */ 4165789Sahrens /* ARGSUSED */ 4166789Sahrens static int 4167789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41685331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 41695331Samw caller_context_t *ct) 4170789Sahrens { 41711544Seschrock uint64_t pages = btopr(len); 41721544Seschrock 41731544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 41741544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 41751773Seschrock 41761773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 41771773Seschrock vn_has_cached_data(vp)) 41785331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 41791773Seschrock 4180789Sahrens return (0); 4181789Sahrens } 4182789Sahrens 4183789Sahrens /* 4184789Sahrens * Free or allocate space in a file. Currently, this function only 4185789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4186789Sahrens * misnamed, as its functionality includes the ability to allocate as 4187789Sahrens * well as free space. 4188789Sahrens * 4189789Sahrens * IN: vp - vnode of file to free data in. 4190789Sahrens * cmd - action to take (only F_FREESP supported). 4191789Sahrens * bfp - section of file to free/alloc. 4192789Sahrens * flag - current file open mode flags. 4193789Sahrens * offset - current file offset. 4194789Sahrens * cr - credentials of caller [UNUSED]. 41955331Samw * ct - caller context. 4196789Sahrens * 4197789Sahrens * RETURN: 0 if success 4198789Sahrens * error code if failure 4199789Sahrens * 4200789Sahrens * Timestamps: 4201789Sahrens * vp - ctime|mtime updated 4202789Sahrens */ 4203789Sahrens /* ARGSUSED */ 4204789Sahrens static int 4205789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4206789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4207789Sahrens { 4208789Sahrens znode_t *zp = VTOZ(vp); 4209789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4210789Sahrens uint64_t off, len; 4211789Sahrens int error; 4212789Sahrens 42135367Sahrens ZFS_ENTER(zfsvfs); 42145367Sahrens ZFS_VERIFY_ZP(zp); 4215789Sahrens 4216789Sahrens if (cmd != F_FREESP) { 4217789Sahrens ZFS_EXIT(zfsvfs); 4218789Sahrens return (EINVAL); 4219789Sahrens } 4220789Sahrens 4221789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4222789Sahrens ZFS_EXIT(zfsvfs); 4223789Sahrens return (error); 4224789Sahrens } 4225789Sahrens 4226789Sahrens if (bfp->l_len < 0) { 4227789Sahrens ZFS_EXIT(zfsvfs); 4228789Sahrens return (EINVAL); 4229789Sahrens } 4230789Sahrens 4231789Sahrens off = bfp->l_start; 42321669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 42331878Smaybee 42346992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4235789Sahrens 4236789Sahrens ZFS_EXIT(zfsvfs); 4237789Sahrens return (error); 4238789Sahrens } 4239789Sahrens 42405331Samw /*ARGSUSED*/ 4241789Sahrens static int 42425331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4243789Sahrens { 4244789Sahrens znode_t *zp = VTOZ(vp); 4245789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 42465326Sek110237 uint32_t gen; 4247789Sahrens uint64_t object = zp->z_id; 4248789Sahrens zfid_short_t *zfid; 4249789Sahrens int size, i; 4250789Sahrens 42515367Sahrens ZFS_ENTER(zfsvfs); 42525367Sahrens ZFS_VERIFY_ZP(zp); 42535326Sek110237 gen = (uint32_t)zp->z_gen; 4254789Sahrens 4255789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4256789Sahrens if (fidp->fid_len < size) { 4257789Sahrens fidp->fid_len = size; 42581512Sek110237 ZFS_EXIT(zfsvfs); 4259789Sahrens return (ENOSPC); 4260789Sahrens } 4261789Sahrens 4262789Sahrens zfid = (zfid_short_t *)fidp; 4263789Sahrens 4264789Sahrens zfid->zf_len = size; 4265789Sahrens 4266789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4267789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4268789Sahrens 4269789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4270789Sahrens if (gen == 0) 4271789Sahrens gen = 1; 4272789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4273789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4274789Sahrens 4275789Sahrens if (size == LONG_FID_LEN) { 4276789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4277789Sahrens zfid_long_t *zlfid; 4278789Sahrens 4279789Sahrens zlfid = (zfid_long_t *)fidp; 4280789Sahrens 4281789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4282789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4283789Sahrens 4284789Sahrens /* XXX - this should be the generation number for the objset */ 4285789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4286789Sahrens zlfid->zf_setgen[i] = 0; 4287789Sahrens } 4288789Sahrens 4289789Sahrens ZFS_EXIT(zfsvfs); 4290789Sahrens return (0); 4291789Sahrens } 4292789Sahrens 4293789Sahrens static int 42945331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 42955331Samw caller_context_t *ct) 4296789Sahrens { 4297789Sahrens znode_t *zp, *xzp; 4298789Sahrens zfsvfs_t *zfsvfs; 4299789Sahrens zfs_dirlock_t *dl; 4300789Sahrens int error; 4301789Sahrens 4302789Sahrens switch (cmd) { 4303789Sahrens case _PC_LINK_MAX: 4304789Sahrens *valp = ULONG_MAX; 4305789Sahrens return (0); 4306789Sahrens 4307789Sahrens case _PC_FILESIZEBITS: 4308789Sahrens *valp = 64; 4309789Sahrens return (0); 4310789Sahrens 4311789Sahrens case _PC_XATTR_EXISTS: 4312789Sahrens zp = VTOZ(vp); 4313789Sahrens zfsvfs = zp->z_zfsvfs; 43145367Sahrens ZFS_ENTER(zfsvfs); 43155367Sahrens ZFS_VERIFY_ZP(zp); 4316789Sahrens *valp = 0; 4317789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 43185331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4319789Sahrens if (error == 0) { 4320789Sahrens zfs_dirent_unlock(dl); 4321789Sahrens if (!zfs_dirempty(xzp)) 4322789Sahrens *valp = 1; 4323789Sahrens VN_RELE(ZTOV(xzp)); 4324789Sahrens } else if (error == ENOENT) { 4325789Sahrens /* 4326789Sahrens * If there aren't extended attributes, it's the 4327789Sahrens * same as having zero of them. 4328789Sahrens */ 4329789Sahrens error = 0; 4330789Sahrens } 4331789Sahrens ZFS_EXIT(zfsvfs); 4332789Sahrens return (error); 4333789Sahrens 43345331Samw case _PC_SATTR_ENABLED: 43355331Samw case _PC_SATTR_EXISTS: 43367757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 43375331Samw (vp->v_type == VREG || vp->v_type == VDIR); 43385331Samw return (0); 43395331Samw 4340789Sahrens case _PC_ACL_ENABLED: 4341789Sahrens *valp = _ACL_ACE_ENABLED; 4342789Sahrens return (0); 4343789Sahrens 4344789Sahrens case _PC_MIN_HOLE_SIZE: 4345789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4346789Sahrens return (0); 4347789Sahrens 4348789Sahrens default: 43495331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4350789Sahrens } 4351789Sahrens } 4352789Sahrens 4353789Sahrens /*ARGSUSED*/ 4354789Sahrens static int 43555331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 43565331Samw caller_context_t *ct) 4357789Sahrens { 4358789Sahrens znode_t *zp = VTOZ(vp); 4359789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4360789Sahrens int error; 43615331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4362789Sahrens 43635367Sahrens ZFS_ENTER(zfsvfs); 43645367Sahrens ZFS_VERIFY_ZP(zp); 43655331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4366789Sahrens ZFS_EXIT(zfsvfs); 4367789Sahrens 4368789Sahrens return (error); 4369789Sahrens } 4370789Sahrens 4371789Sahrens /*ARGSUSED*/ 4372789Sahrens static int 43735331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 43745331Samw caller_context_t *ct) 4375789Sahrens { 4376789Sahrens znode_t *zp = VTOZ(vp); 4377789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4378789Sahrens int error; 43795331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4380789Sahrens 43815367Sahrens ZFS_ENTER(zfsvfs); 43825367Sahrens ZFS_VERIFY_ZP(zp); 43835331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4384789Sahrens ZFS_EXIT(zfsvfs); 4385789Sahrens return (error); 4386789Sahrens } 4387789Sahrens 4388789Sahrens /* 4389789Sahrens * Predeclare these here so that the compiler assumes that 4390789Sahrens * this is an "old style" function declaration that does 4391789Sahrens * not include arguments => we won't get type mismatch errors 4392789Sahrens * in the initializations that follow. 4393789Sahrens */ 4394789Sahrens static int zfs_inval(); 4395789Sahrens static int zfs_isdir(); 4396789Sahrens 4397789Sahrens static int 4398789Sahrens zfs_inval() 4399789Sahrens { 4400789Sahrens return (EINVAL); 4401789Sahrens } 4402789Sahrens 4403789Sahrens static int 4404789Sahrens zfs_isdir() 4405789Sahrens { 4406789Sahrens return (EISDIR); 4407789Sahrens } 4408789Sahrens /* 4409789Sahrens * Directory vnode operations template 4410789Sahrens */ 4411789Sahrens vnodeops_t *zfs_dvnodeops; 4412789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 44133898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44143898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44153898Srsb VOPNAME_READ, { .error = zfs_isdir }, 44163898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 44173898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44183898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44193898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44203898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44213898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44223898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 44233898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 44243898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 44253898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44263898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 44273898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 44283898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 44293898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 44303898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44313898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44323898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44333898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44343898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44353898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44363898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44374863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44383898Srsb NULL, NULL 4439789Sahrens }; 4440789Sahrens 4441789Sahrens /* 4442789Sahrens * Regular file vnode operations template 4443789Sahrens */ 4444789Sahrens vnodeops_t *zfs_fvnodeops; 4445789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 44463898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44473898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44483898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 44493898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 44503898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44513898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44523898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44533898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44543898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44553898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44563898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44573898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44583898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44593898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44603898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 44613898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 44623898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 44633898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 44643898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 44653898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 44663898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 44673898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44683898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44693898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44703898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44713898Srsb NULL, NULL 4472789Sahrens }; 4473789Sahrens 4474789Sahrens /* 4475789Sahrens * Symbolic link vnode operations template 4476789Sahrens */ 4477789Sahrens vnodeops_t *zfs_symvnodeops; 4478789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 44793898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44803898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44813898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44823898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44833898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 44843898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44853898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44863898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44873898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44883898Srsb NULL, NULL 4489789Sahrens }; 4490789Sahrens 4491789Sahrens /* 4492789Sahrens * Extended attribute directory vnode operations template 4493789Sahrens * This template is identical to the directory vnodes 4494789Sahrens * operation template except for restricted operations: 4495789Sahrens * VOP_MKDIR() 4496789Sahrens * VOP_SYMLINK() 4497789Sahrens * Note that there are other restrictions embedded in: 4498789Sahrens * zfs_create() - restrict type to VREG 4499789Sahrens * zfs_link() - no links into/out of attribute space 4500789Sahrens * zfs_rename() - no moves into/out of attribute space 4501789Sahrens */ 4502789Sahrens vnodeops_t *zfs_xdvnodeops; 4503789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 45043898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45053898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45063898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45073898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45083898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45093898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45103898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45113898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 45123898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 45133898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 45143898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45153898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 45163898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 45173898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 45183898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 45193898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45203898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45213898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45223898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45233898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45243898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45253898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45263898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45273898Srsb NULL, NULL 4528789Sahrens }; 4529789Sahrens 4530789Sahrens /* 4531789Sahrens * Error vnode operations template 4532789Sahrens */ 4533789Sahrens vnodeops_t *zfs_evnodeops; 4534789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 45353898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45363898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45373898Srsb NULL, NULL 4538789Sahrens }; 4539