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; 10777847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 10787847SMark.Shellenbaum@Sun.COM uid_t uid; 10797847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 1080*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 1081*9179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 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); 1144789Sahrens return (error); 1145789Sahrens } 1146789Sahrens } 1147789Sahrens if (zp == NULL) { 11485331Samw uint64_t txtype; 11495331Samw 1150789Sahrens /* 1151789Sahrens * Create a new file object and update the directory 1152789Sahrens * to reference it. 1153789Sahrens */ 11545331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1155789Sahrens goto out; 1156789Sahrens } 1157789Sahrens 1158789Sahrens /* 1159789Sahrens * We only support the creation of regular files in 1160789Sahrens * extended attribute directories. 1161789Sahrens */ 1162789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1163789Sahrens (vap->va_type != VREG)) { 1164789Sahrens error = EINVAL; 1165789Sahrens goto out; 1166789Sahrens } 1167789Sahrens 1168*9179SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp, 1169*9179SMark.Shellenbaum@Sun.COM &acl_ids)) != 0) 1170*9179SMark.Shellenbaum@Sun.COM goto out; 1171*9179SMark.Shellenbaum@Sun.COM 1172789Sahrens tx = dmu_tx_create(os); 1173789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1174*9179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 1175*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 11765824Smarks if (zfsvfs->z_fuid_obj == 0) { 11775824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 11785824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 11795824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 11805824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 11815824Smarks FALSE, NULL); 11825824Smarks } else { 11835824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 11845824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 11855824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 11865824Smarks } 11875331Samw } 1188789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 11891544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1190*9179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1191789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1192789Sahrens 0, SPA_MAXBLOCKSIZE); 11935331Samw } 11948227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1195789Sahrens if (error) { 1196*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1197789Sahrens zfs_dirent_unlock(dl); 11988227SNeil.Perrin@Sun.COM if (error == ERESTART) { 11992113Sahrens dmu_tx_wait(tx); 12002113Sahrens dmu_tx_abort(tx); 1201789Sahrens goto top; 1202789Sahrens } 12032113Sahrens dmu_tx_abort(tx); 1204789Sahrens ZFS_EXIT(zfsvfs); 1205789Sahrens return (error); 1206789Sahrens } 1207*9179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 1208*9179SMark.Shellenbaum@Sun.COM 1209*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 1210*9179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 1211*9179SMark.Shellenbaum@Sun.COM 1212789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1213*9179SMark.Shellenbaum@Sun.COM 12145331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 12155331Samw if (flag & FIGNORECASE) 12165331Samw txtype |= TX_CI; 12175331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 1218*9179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 1219*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1220789Sahrens dmu_tx_commit(tx); 1221789Sahrens } else { 12225331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 12235331Samw 1224789Sahrens /* 1225789Sahrens * A directory entry already exists for this name. 1226789Sahrens */ 1227789Sahrens /* 1228789Sahrens * Can't truncate an existing file if in exclusive mode. 1229789Sahrens */ 1230789Sahrens if (excl == EXCL) { 1231789Sahrens error = EEXIST; 1232789Sahrens goto out; 1233789Sahrens } 1234789Sahrens /* 1235789Sahrens * Can't open a directory for writing. 1236789Sahrens */ 1237789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1238789Sahrens error = EISDIR; 1239789Sahrens goto out; 1240789Sahrens } 1241789Sahrens /* 1242789Sahrens * Verify requested access to file. 1243789Sahrens */ 12445331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1245789Sahrens goto out; 1246789Sahrens } 1247789Sahrens 1248789Sahrens mutex_enter(&dzp->z_lock); 1249789Sahrens dzp->z_seq++; 1250789Sahrens mutex_exit(&dzp->z_lock); 1251789Sahrens 12521878Smaybee /* 12531878Smaybee * Truncate regular files if requested. 12541878Smaybee */ 12551878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1256789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 12576992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 12586992Smaybee zfs_dirent_unlock(dl); 12596992Smaybee dl = NULL; 12601878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 12614863Spraks if (error == 0) { 12625331Samw vnevent_create(ZTOV(zp), ct); 12634863Spraks } 1264789Sahrens } 1265789Sahrens } 1266789Sahrens out: 1267789Sahrens 1268789Sahrens if (dl) 1269789Sahrens zfs_dirent_unlock(dl); 1270789Sahrens 1271789Sahrens if (error) { 1272789Sahrens if (zp) 1273789Sahrens VN_RELE(ZTOV(zp)); 1274789Sahrens } else { 1275789Sahrens *vpp = ZTOV(zp); 1276789Sahrens /* 1277789Sahrens * If vnode is for a device return a specfs vnode instead. 1278789Sahrens */ 1279789Sahrens if (IS_DEVVP(*vpp)) { 1280789Sahrens struct vnode *svp; 1281789Sahrens 1282789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1283789Sahrens VN_RELE(*vpp); 1284789Sahrens if (svp == NULL) { 1285789Sahrens error = ENOSYS; 1286789Sahrens } 1287789Sahrens *vpp = svp; 1288789Sahrens } 1289789Sahrens } 1290789Sahrens 1291789Sahrens ZFS_EXIT(zfsvfs); 1292789Sahrens return (error); 1293789Sahrens } 1294789Sahrens 1295789Sahrens /* 1296789Sahrens * Remove an entry from a directory. 1297789Sahrens * 1298789Sahrens * IN: dvp - vnode of directory to remove entry from. 1299789Sahrens * name - name of entry to remove. 1300789Sahrens * cr - credentials of caller. 13015331Samw * ct - caller context 13025331Samw * flags - case flags 1303789Sahrens * 1304789Sahrens * RETURN: 0 if success 1305789Sahrens * error code if failure 1306789Sahrens * 1307789Sahrens * Timestamps: 1308789Sahrens * dvp - ctime|mtime 1309789Sahrens * vp - ctime (if nlink > 0) 1310789Sahrens */ 13115331Samw /*ARGSUSED*/ 1312789Sahrens static int 13135331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 13145331Samw int flags) 1315789Sahrens { 1316789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1317789Sahrens znode_t *xzp = NULL; 1318789Sahrens vnode_t *vp; 1319789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 13205326Sek110237 zilog_t *zilog; 1321789Sahrens uint64_t acl_obj, xattr_obj; 1322789Sahrens zfs_dirlock_t *dl; 1323789Sahrens dmu_tx_t *tx; 13243461Sahrens boolean_t may_delete_now, delete_now = FALSE; 13256992Smaybee boolean_t unlinked, toobig = FALSE; 13265331Samw uint64_t txtype; 13275331Samw pathname_t *realnmp = NULL; 13285331Samw pathname_t realnm; 1329789Sahrens int error; 13305331Samw int zflg = ZEXISTS; 1331789Sahrens 13325367Sahrens ZFS_ENTER(zfsvfs); 13335367Sahrens ZFS_VERIFY_ZP(dzp); 13345326Sek110237 zilog = zfsvfs->z_log; 1335789Sahrens 13365331Samw if (flags & FIGNORECASE) { 13375331Samw zflg |= ZCILOOK; 13385331Samw pn_alloc(&realnm); 13395331Samw realnmp = &realnm; 13405331Samw } 13415331Samw 1342789Sahrens top: 1343789Sahrens /* 1344789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1345789Sahrens */ 13465331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13475331Samw NULL, realnmp)) { 13485331Samw if (realnmp) 13495331Samw pn_free(realnmp); 1350789Sahrens ZFS_EXIT(zfsvfs); 1351789Sahrens return (error); 1352789Sahrens } 1353789Sahrens 1354789Sahrens vp = ZTOV(zp); 1355789Sahrens 1356789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1357789Sahrens goto out; 1358789Sahrens } 1359789Sahrens 1360789Sahrens /* 1361789Sahrens * Need to use rmdir for removing directories. 1362789Sahrens */ 1363789Sahrens if (vp->v_type == VDIR) { 1364789Sahrens error = EPERM; 1365789Sahrens goto out; 1366789Sahrens } 1367789Sahrens 13685331Samw vnevent_remove(vp, dvp, name, ct); 13695331Samw 13705331Samw if (realnmp) 13716492Stimh dnlc_remove(dvp, realnmp->pn_buf); 13725331Samw else 13735331Samw dnlc_remove(dvp, name); 13741484Sek110237 1375789Sahrens mutex_enter(&vp->v_lock); 1376789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1377789Sahrens mutex_exit(&vp->v_lock); 1378789Sahrens 1379789Sahrens /* 13803461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1381789Sahrens * it depends on whether we're the last link, and on whether there are 1382789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1383789Sahrens * allow for either case. 1384789Sahrens */ 1385789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 13861544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1387789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 13886992Smaybee if (may_delete_now) { 13896992Smaybee toobig = 13906992Smaybee zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 13916992Smaybee /* if the file is too big, only hold_free a token amount */ 13926992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 13936992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 13946992Smaybee } 1395789Sahrens 1396789Sahrens /* are there any extended attributes? */ 1397789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1398789Sahrens /* XXX - do we need this if we are deleting? */ 1399789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1400789Sahrens } 1401789Sahrens 1402789Sahrens /* are there any additional acls */ 1403789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1404789Sahrens may_delete_now) 1405789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1406789Sahrens 1407789Sahrens /* charge as an update -- would be nice not to charge at all */ 14083461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1409789Sahrens 14108227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1411789Sahrens if (error) { 1412789Sahrens zfs_dirent_unlock(dl); 1413789Sahrens VN_RELE(vp); 14148227SNeil.Perrin@Sun.COM if (error == ERESTART) { 14152113Sahrens dmu_tx_wait(tx); 14162113Sahrens dmu_tx_abort(tx); 1417789Sahrens goto top; 1418789Sahrens } 14195331Samw if (realnmp) 14205331Samw pn_free(realnmp); 14212113Sahrens dmu_tx_abort(tx); 1422789Sahrens ZFS_EXIT(zfsvfs); 1423789Sahrens return (error); 1424789Sahrens } 1425789Sahrens 1426789Sahrens /* 1427789Sahrens * Remove the directory entry. 1428789Sahrens */ 14295331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1430789Sahrens 1431789Sahrens if (error) { 1432789Sahrens dmu_tx_commit(tx); 1433789Sahrens goto out; 1434789Sahrens } 1435789Sahrens 14363461Sahrens if (unlinked) { 1437789Sahrens mutex_enter(&vp->v_lock); 14386992Smaybee delete_now = may_delete_now && !toobig && 1439789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1440789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1441789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1442789Sahrens mutex_exit(&vp->v_lock); 1443789Sahrens } 1444789Sahrens 1445789Sahrens if (delete_now) { 1446789Sahrens if (zp->z_phys->zp_xattr) { 1447789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1448789Sahrens ASSERT3U(error, ==, 0); 1449789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1450789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1451789Sahrens mutex_enter(&xzp->z_lock); 14523461Sahrens xzp->z_unlinked = 1; 1453789Sahrens xzp->z_phys->zp_links = 0; 1454789Sahrens mutex_exit(&xzp->z_lock); 14553461Sahrens zfs_unlinked_add(xzp, tx); 1456789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1457789Sahrens } 1458789Sahrens mutex_enter(&zp->z_lock); 1459789Sahrens mutex_enter(&vp->v_lock); 1460789Sahrens vp->v_count--; 1461789Sahrens ASSERT3U(vp->v_count, ==, 0); 1462789Sahrens mutex_exit(&vp->v_lock); 1463789Sahrens mutex_exit(&zp->z_lock); 1464789Sahrens zfs_znode_delete(zp, tx); 14653461Sahrens } else if (unlinked) { 14663461Sahrens zfs_unlinked_add(zp, tx); 1467789Sahrens } 1468789Sahrens 14695331Samw txtype = TX_REMOVE; 14705331Samw if (flags & FIGNORECASE) 14715331Samw txtype |= TX_CI; 14725331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 1473789Sahrens 1474789Sahrens dmu_tx_commit(tx); 1475789Sahrens out: 14765331Samw if (realnmp) 14775331Samw pn_free(realnmp); 14785331Samw 1479789Sahrens zfs_dirent_unlock(dl); 1480789Sahrens 1481789Sahrens if (!delete_now) { 1482789Sahrens VN_RELE(vp); 1483789Sahrens } else if (xzp) { 14846992Smaybee /* this rele is delayed to prevent nesting transactions */ 1485789Sahrens VN_RELE(ZTOV(xzp)); 1486789Sahrens } 1487789Sahrens 1488789Sahrens ZFS_EXIT(zfsvfs); 1489789Sahrens return (error); 1490789Sahrens } 1491789Sahrens 1492789Sahrens /* 1493789Sahrens * Create a new directory and insert it into dvp using the name 1494789Sahrens * provided. Return a pointer to the inserted directory. 1495789Sahrens * 1496789Sahrens * IN: dvp - vnode of directory to add subdir to. 1497789Sahrens * dirname - name of new directory. 1498789Sahrens * vap - attributes of new directory. 1499789Sahrens * cr - credentials of caller. 15005331Samw * ct - caller context 15015331Samw * vsecp - ACL to be set 1502789Sahrens * 1503789Sahrens * OUT: vpp - vnode of created directory. 1504789Sahrens * 1505789Sahrens * RETURN: 0 if success 1506789Sahrens * error code if failure 1507789Sahrens * 1508789Sahrens * Timestamps: 1509789Sahrens * dvp - ctime|mtime updated 1510789Sahrens * vp - ctime|mtime|atime updated 1511789Sahrens */ 15125331Samw /*ARGSUSED*/ 1513789Sahrens static int 15145331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 15155331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1516789Sahrens { 1517789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1518789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15195326Sek110237 zilog_t *zilog; 1520789Sahrens zfs_dirlock_t *dl; 15215331Samw uint64_t txtype; 1522789Sahrens dmu_tx_t *tx; 1523789Sahrens int error; 15245331Samw int zf = ZNEW; 15257847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 15267847SMark.Shellenbaum@Sun.COM uid_t uid; 15277847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 1528*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 1529*9179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1530789Sahrens 1531789Sahrens ASSERT(vap->va_type == VDIR); 1532789Sahrens 15335331Samw /* 15345331Samw * If we have an ephemeral id, ACL, or XVATTR then 15355331Samw * make sure file system is at proper version 15365331Samw */ 15375331Samw 15387847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 15397847SMark.Shellenbaum@Sun.COM if (ksid) 15407847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 15417847SMark.Shellenbaum@Sun.COM else 15427847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 15435331Samw if (zfsvfs->z_use_fuids == B_FALSE && 15447847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 15457876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 15465331Samw return (EINVAL); 15475331Samw 15485367Sahrens ZFS_ENTER(zfsvfs); 15495367Sahrens ZFS_VERIFY_ZP(dzp); 15505326Sek110237 zilog = zfsvfs->z_log; 1551789Sahrens 1552789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1553789Sahrens ZFS_EXIT(zfsvfs); 1554789Sahrens return (EINVAL); 1555789Sahrens } 15565331Samw 15575498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 15585331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 15595331Samw ZFS_EXIT(zfsvfs); 15605331Samw return (EILSEQ); 15615331Samw } 15625331Samw if (flags & FIGNORECASE) 15635331Samw zf |= ZCILOOK; 15645331Samw 15655331Samw if (vap->va_mask & AT_XVATTR) 15665331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 15675331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 15685331Samw ZFS_EXIT(zfsvfs); 15695331Samw return (error); 15705331Samw } 1571789Sahrens 1572789Sahrens /* 1573789Sahrens * First make sure the new directory doesn't exist. 1574789Sahrens */ 15755331Samw top: 15765331Samw *vpp = NULL; 15775331Samw 15785331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 15795331Samw NULL, NULL)) { 1580789Sahrens ZFS_EXIT(zfsvfs); 1581789Sahrens return (error); 1582789Sahrens } 1583789Sahrens 15845331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 15851231Smarks zfs_dirent_unlock(dl); 15861231Smarks ZFS_EXIT(zfsvfs); 15871231Smarks return (error); 15881231Smarks } 15891231Smarks 1590*9179SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp, 1591*9179SMark.Shellenbaum@Sun.COM &acl_ids)) != 0) { 1592*9179SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 1593*9179SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1594*9179SMark.Shellenbaum@Sun.COM return (error); 15955331Samw } 1596*9179SMark.Shellenbaum@Sun.COM 1597789Sahrens /* 1598789Sahrens * Add a new entry to the directory. 1599789Sahrens */ 1600789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16011544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 16021544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1603*9179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 1604*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 16055824Smarks if (zfsvfs->z_fuid_obj == 0) { 16065824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 16075824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 16085824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16095824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 16105824Smarks } else { 16115824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 16125824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 16135824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 16145824Smarks } 16155331Samw } 1616*9179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 1617789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1618789Sahrens 0, SPA_MAXBLOCKSIZE); 16198227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1620789Sahrens if (error) { 1621*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1622789Sahrens zfs_dirent_unlock(dl); 16238227SNeil.Perrin@Sun.COM if (error == ERESTART) { 16242113Sahrens dmu_tx_wait(tx); 16252113Sahrens dmu_tx_abort(tx); 1626789Sahrens goto top; 1627789Sahrens } 16282113Sahrens dmu_tx_abort(tx); 1629789Sahrens ZFS_EXIT(zfsvfs); 1630789Sahrens return (error); 1631789Sahrens } 1632789Sahrens 1633789Sahrens /* 1634789Sahrens * Create new node. 1635789Sahrens */ 1636*9179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 1637*9179SMark.Shellenbaum@Sun.COM 1638*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 1639*9179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 1640789Sahrens /* 1641789Sahrens * Now put new name in parent dir. 1642789Sahrens */ 1643789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1644789Sahrens 1645789Sahrens *vpp = ZTOV(zp); 1646789Sahrens 16475331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 16485331Samw if (flags & FIGNORECASE) 16495331Samw txtype |= TX_CI; 1650*9179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 1651*9179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 1652*9179SMark.Shellenbaum@Sun.COM 1653*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1654789Sahrens dmu_tx_commit(tx); 1655789Sahrens 1656789Sahrens zfs_dirent_unlock(dl); 1657789Sahrens 1658789Sahrens ZFS_EXIT(zfsvfs); 1659789Sahrens return (0); 1660789Sahrens } 1661789Sahrens 1662789Sahrens /* 1663789Sahrens * Remove a directory subdir entry. If the current working 1664789Sahrens * directory is the same as the subdir to be removed, the 1665789Sahrens * remove will fail. 1666789Sahrens * 1667789Sahrens * IN: dvp - vnode of directory to remove from. 1668789Sahrens * name - name of directory to be removed. 1669789Sahrens * cwd - vnode of current working directory. 1670789Sahrens * cr - credentials of caller. 16715331Samw * ct - caller context 16725331Samw * flags - case flags 1673789Sahrens * 1674789Sahrens * RETURN: 0 if success 1675789Sahrens * error code if failure 1676789Sahrens * 1677789Sahrens * Timestamps: 1678789Sahrens * dvp - ctime|mtime updated 1679789Sahrens */ 16805331Samw /*ARGSUSED*/ 1681789Sahrens static int 16825331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 16835331Samw caller_context_t *ct, int flags) 1684789Sahrens { 1685789Sahrens znode_t *dzp = VTOZ(dvp); 1686789Sahrens znode_t *zp; 1687789Sahrens vnode_t *vp; 1688789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 16895326Sek110237 zilog_t *zilog; 1690789Sahrens zfs_dirlock_t *dl; 1691789Sahrens dmu_tx_t *tx; 1692789Sahrens int error; 16935331Samw int zflg = ZEXISTS; 1694789Sahrens 16955367Sahrens ZFS_ENTER(zfsvfs); 16965367Sahrens ZFS_VERIFY_ZP(dzp); 16975326Sek110237 zilog = zfsvfs->z_log; 1698789Sahrens 16995331Samw if (flags & FIGNORECASE) 17005331Samw zflg |= ZCILOOK; 1701789Sahrens top: 1702789Sahrens zp = NULL; 1703789Sahrens 1704789Sahrens /* 1705789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1706789Sahrens */ 17075331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 17085331Samw NULL, NULL)) { 1709789Sahrens ZFS_EXIT(zfsvfs); 1710789Sahrens return (error); 1711789Sahrens } 1712789Sahrens 1713789Sahrens vp = ZTOV(zp); 1714789Sahrens 1715789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1716789Sahrens goto out; 1717789Sahrens } 1718789Sahrens 1719789Sahrens if (vp->v_type != VDIR) { 1720789Sahrens error = ENOTDIR; 1721789Sahrens goto out; 1722789Sahrens } 1723789Sahrens 1724789Sahrens if (vp == cwd) { 1725789Sahrens error = EINVAL; 1726789Sahrens goto out; 1727789Sahrens } 1728789Sahrens 17295331Samw vnevent_rmdir(vp, dvp, name, ct); 1730789Sahrens 1731789Sahrens /* 17323897Smaybee * Grab a lock on the directory to make sure that noone is 17333897Smaybee * trying to add (or lookup) entries while we are removing it. 17343897Smaybee */ 17353897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 17363897Smaybee 17373897Smaybee /* 17383897Smaybee * Grab a lock on the parent pointer to make sure we play well 1739789Sahrens * with the treewalk and directory rename code. 1740789Sahrens */ 1741789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1742789Sahrens 1743789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 17441544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1745789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 17463461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 17478227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1748789Sahrens if (error) { 1749789Sahrens rw_exit(&zp->z_parent_lock); 17503897Smaybee rw_exit(&zp->z_name_lock); 1751789Sahrens zfs_dirent_unlock(dl); 1752789Sahrens VN_RELE(vp); 17538227SNeil.Perrin@Sun.COM if (error == ERESTART) { 17542113Sahrens dmu_tx_wait(tx); 17552113Sahrens dmu_tx_abort(tx); 1756789Sahrens goto top; 1757789Sahrens } 17582113Sahrens dmu_tx_abort(tx); 1759789Sahrens ZFS_EXIT(zfsvfs); 1760789Sahrens return (error); 1761789Sahrens } 1762789Sahrens 17635331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 17645331Samw 17655331Samw if (error == 0) { 17665331Samw uint64_t txtype = TX_RMDIR; 17675331Samw if (flags & FIGNORECASE) 17685331Samw txtype |= TX_CI; 17695331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 17705331Samw } 1771789Sahrens 1772789Sahrens dmu_tx_commit(tx); 1773789Sahrens 1774789Sahrens rw_exit(&zp->z_parent_lock); 17753897Smaybee rw_exit(&zp->z_name_lock); 1776789Sahrens out: 1777789Sahrens zfs_dirent_unlock(dl); 1778789Sahrens 1779789Sahrens VN_RELE(vp); 1780789Sahrens 1781789Sahrens ZFS_EXIT(zfsvfs); 1782789Sahrens return (error); 1783789Sahrens } 1784789Sahrens 1785789Sahrens /* 1786789Sahrens * Read as many directory entries as will fit into the provided 1787789Sahrens * buffer from the given directory cursor position (specified in 1788789Sahrens * the uio structure. 1789789Sahrens * 1790789Sahrens * IN: vp - vnode of directory to read. 1791789Sahrens * uio - structure supplying read location, range info, 1792789Sahrens * and return buffer. 1793789Sahrens * cr - credentials of caller. 17945331Samw * ct - caller context 17955331Samw * flags - case flags 1796789Sahrens * 1797789Sahrens * OUT: uio - updated offset and range, buffer filled. 1798789Sahrens * eofp - set to true if end-of-file detected. 1799789Sahrens * 1800789Sahrens * RETURN: 0 if success 1801789Sahrens * error code if failure 1802789Sahrens * 1803789Sahrens * Timestamps: 1804789Sahrens * vp - atime updated 1805789Sahrens * 1806789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1807789Sahrens * This allows us to use the low range for "special" directory entries: 1808789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1809789Sahrens * we use the offset 2 for the '.zfs' directory. 1810789Sahrens */ 1811789Sahrens /* ARGSUSED */ 1812789Sahrens static int 18135331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 18145331Samw caller_context_t *ct, int flags) 1815789Sahrens { 1816789Sahrens znode_t *zp = VTOZ(vp); 1817789Sahrens iovec_t *iovp; 18185331Samw edirent_t *eodp; 1819789Sahrens dirent64_t *odp; 1820789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1821869Sperrin objset_t *os; 1822789Sahrens caddr_t outbuf; 1823789Sahrens size_t bufsize; 1824789Sahrens zap_cursor_t zc; 1825789Sahrens zap_attribute_t zap; 1826789Sahrens uint_t bytes_wanted; 1827789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1828789Sahrens int local_eof; 1829869Sperrin int outcount; 1830869Sperrin int error; 1831869Sperrin uint8_t prefetch; 18325663Sck153898 boolean_t check_sysattrs; 1833789Sahrens 18345367Sahrens ZFS_ENTER(zfsvfs); 18355367Sahrens ZFS_VERIFY_ZP(zp); 1836789Sahrens 1837789Sahrens /* 1838789Sahrens * If we are not given an eof variable, 1839789Sahrens * use a local one. 1840789Sahrens */ 1841789Sahrens if (eofp == NULL) 1842789Sahrens eofp = &local_eof; 1843789Sahrens 1844789Sahrens /* 1845789Sahrens * Check for valid iov_len. 1846789Sahrens */ 1847789Sahrens if (uio->uio_iov->iov_len <= 0) { 1848789Sahrens ZFS_EXIT(zfsvfs); 1849789Sahrens return (EINVAL); 1850789Sahrens } 1851789Sahrens 1852789Sahrens /* 1853789Sahrens * Quit if directory has been removed (posix) 1854789Sahrens */ 18553461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 1856789Sahrens ZFS_EXIT(zfsvfs); 1857789Sahrens return (0); 1858789Sahrens } 1859789Sahrens 1860869Sperrin error = 0; 1861869Sperrin os = zfsvfs->z_os; 1862869Sperrin offset = uio->uio_loffset; 1863869Sperrin prefetch = zp->z_zn_prefetch; 1864869Sperrin 1865789Sahrens /* 1866789Sahrens * Initialize the iterator cursor. 1867789Sahrens */ 1868789Sahrens if (offset <= 3) { 1869789Sahrens /* 1870789Sahrens * Start iteration from the beginning of the directory. 1871789Sahrens */ 1872869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1873789Sahrens } else { 1874789Sahrens /* 1875789Sahrens * The offset is a serialized cursor. 1876789Sahrens */ 1877869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1878789Sahrens } 1879789Sahrens 1880789Sahrens /* 1881789Sahrens * Get space to change directory entries into fs independent format. 1882789Sahrens */ 1883789Sahrens iovp = uio->uio_iov; 1884789Sahrens bytes_wanted = iovp->iov_len; 1885789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1886789Sahrens bufsize = bytes_wanted; 1887789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1888789Sahrens odp = (struct dirent64 *)outbuf; 1889789Sahrens } else { 1890789Sahrens bufsize = bytes_wanted; 1891789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1892789Sahrens } 18935331Samw eodp = (struct edirent *)odp; 1894789Sahrens 1895789Sahrens /* 18967757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 18977757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 18987757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 18997757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 19005663Sck153898 */ 19017757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 19025663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 19035663Sck153898 (flags & V_RDDIR_ENTFLAGS); 19045663Sck153898 19055663Sck153898 /* 1906789Sahrens * Transform to file-system independent format 1907789Sahrens */ 1908789Sahrens outcount = 0; 1909789Sahrens while (outcount < bytes_wanted) { 19103912Slling ino64_t objnum; 19113912Slling ushort_t reclen; 19123912Slling off64_t *next; 19133912Slling 1914789Sahrens /* 1915789Sahrens * Special case `.', `..', and `.zfs'. 1916789Sahrens */ 1917789Sahrens if (offset == 0) { 1918789Sahrens (void) strcpy(zap.za_name, "."); 19195331Samw zap.za_normalization_conflict = 0; 19203912Slling objnum = zp->z_id; 1921789Sahrens } else if (offset == 1) { 1922789Sahrens (void) strcpy(zap.za_name, ".."); 19235331Samw zap.za_normalization_conflict = 0; 19243912Slling objnum = zp->z_phys->zp_parent; 1925789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1926789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 19275331Samw zap.za_normalization_conflict = 0; 19283912Slling objnum = ZFSCTL_INO_ROOT; 1929789Sahrens } else { 1930789Sahrens /* 1931789Sahrens * Grab next entry. 1932789Sahrens */ 1933789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1934789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1935789Sahrens break; 1936789Sahrens else 1937789Sahrens goto update; 1938789Sahrens } 1939789Sahrens 1940789Sahrens if (zap.za_integer_length != 8 || 1941789Sahrens zap.za_num_integers != 1) { 1942789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1943789Sahrens "entry, obj = %lld, offset = %lld\n", 1944789Sahrens (u_longlong_t)zp->z_id, 1945789Sahrens (u_longlong_t)offset); 1946789Sahrens error = ENXIO; 1947789Sahrens goto update; 1948789Sahrens } 19493912Slling 19503912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 19513912Slling /* 19523912Slling * MacOS X can extract the object type here such as: 19533912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 19543912Slling */ 19555663Sck153898 19565663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 19575663Sck153898 zap.za_normalization_conflict = 19585663Sck153898 xattr_sysattr_casechk(zap.za_name); 19595663Sck153898 } 1960789Sahrens } 19615331Samw 19625331Samw if (flags & V_RDDIR_ENTFLAGS) 19635331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 19645331Samw else 19655331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1966789Sahrens 1967789Sahrens /* 1968789Sahrens * Will this entry fit in the buffer? 1969789Sahrens */ 19703912Slling if (outcount + reclen > bufsize) { 1971789Sahrens /* 1972789Sahrens * Did we manage to fit anything in the buffer? 1973789Sahrens */ 1974789Sahrens if (!outcount) { 1975789Sahrens error = EINVAL; 1976789Sahrens goto update; 1977789Sahrens } 1978789Sahrens break; 1979789Sahrens } 19805331Samw if (flags & V_RDDIR_ENTFLAGS) { 19815331Samw /* 19825331Samw * Add extended flag entry: 19835331Samw */ 19845331Samw eodp->ed_ino = objnum; 19855331Samw eodp->ed_reclen = reclen; 19865331Samw /* NOTE: ed_off is the offset for the *next* entry */ 19875331Samw next = &(eodp->ed_off); 19885331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 19895331Samw ED_CASE_CONFLICT : 0; 19905331Samw (void) strncpy(eodp->ed_name, zap.za_name, 19915331Samw EDIRENT_NAMELEN(reclen)); 19925331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 19935331Samw } else { 19945331Samw /* 19955331Samw * Add normal entry: 19965331Samw */ 19975331Samw odp->d_ino = objnum; 19985331Samw odp->d_reclen = reclen; 19995331Samw /* NOTE: d_off is the offset for the *next* entry */ 20005331Samw next = &(odp->d_off); 20015331Samw (void) strncpy(odp->d_name, zap.za_name, 20025331Samw DIRENT64_NAMELEN(reclen)); 20035331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 20045331Samw } 20053912Slling outcount += reclen; 2006789Sahrens 2007789Sahrens ASSERT(outcount <= bufsize); 2008789Sahrens 2009789Sahrens /* Prefetch znode */ 2010869Sperrin if (prefetch) 20113912Slling dmu_prefetch(os, objnum, 0, 0); 2012789Sahrens 2013789Sahrens /* 2014789Sahrens * Move to the next entry, fill in the previous offset. 2015789Sahrens */ 2016789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2017789Sahrens zap_cursor_advance(&zc); 2018789Sahrens offset = zap_cursor_serialize(&zc); 2019789Sahrens } else { 2020789Sahrens offset += 1; 2021789Sahrens } 2022789Sahrens *next = offset; 2023789Sahrens } 2024869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2025789Sahrens 2026789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2027789Sahrens iovp->iov_base += outcount; 2028789Sahrens iovp->iov_len -= outcount; 2029789Sahrens uio->uio_resid -= outcount; 2030789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2031789Sahrens /* 2032789Sahrens * Reset the pointer. 2033789Sahrens */ 2034789Sahrens offset = uio->uio_loffset; 2035789Sahrens } 2036789Sahrens 2037789Sahrens update: 2038885Sahrens zap_cursor_fini(&zc); 2039789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2040789Sahrens kmem_free(outbuf, bufsize); 2041789Sahrens 2042789Sahrens if (error == ENOENT) 2043789Sahrens error = 0; 2044789Sahrens 2045789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2046789Sahrens 2047789Sahrens uio->uio_loffset = offset; 2048789Sahrens ZFS_EXIT(zfsvfs); 2049789Sahrens return (error); 2050789Sahrens } 2051789Sahrens 20524720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 20534720Sfr157268 2054789Sahrens static int 20555331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2056789Sahrens { 2057789Sahrens znode_t *zp = VTOZ(vp); 2058789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2059789Sahrens 20601773Seschrock /* 20611773Seschrock * Regardless of whether this is required for standards conformance, 20621773Seschrock * this is the logical behavior when fsync() is called on a file with 20631773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 20641773Seschrock * going to be pushed out as part of the zil_commit(). 20651773Seschrock */ 20661773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 20671773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 20685331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 20691773Seschrock 20704720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 20714720Sfr157268 20725367Sahrens ZFS_ENTER(zfsvfs); 20735367Sahrens ZFS_VERIFY_ZP(zp); 20742638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 2075789Sahrens ZFS_EXIT(zfsvfs); 2076789Sahrens return (0); 2077789Sahrens } 2078789Sahrens 20795331Samw 2080789Sahrens /* 2081789Sahrens * Get the requested file attributes and place them in the provided 2082789Sahrens * vattr structure. 2083789Sahrens * 2084789Sahrens * IN: vp - vnode of file. 2085789Sahrens * vap - va_mask identifies requested attributes. 20865331Samw * If AT_XVATTR set, then optional attrs are requested 20875331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2088789Sahrens * cr - credentials of caller. 20895331Samw * ct - caller context 2090789Sahrens * 2091789Sahrens * OUT: vap - attribute values. 2092789Sahrens * 2093789Sahrens * RETURN: 0 (always succeeds) 2094789Sahrens */ 2095789Sahrens /* ARGSUSED */ 2096789Sahrens static int 20975331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 20985331Samw caller_context_t *ct) 2099789Sahrens { 2100789Sahrens znode_t *zp = VTOZ(vp); 2101789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 21025326Sek110237 znode_phys_t *pzp; 21035331Samw int error = 0; 21044543Smarks uint64_t links; 21055331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 21065331Samw xoptattr_t *xoap = NULL; 21075331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2108789Sahrens 21095367Sahrens ZFS_ENTER(zfsvfs); 21105367Sahrens ZFS_VERIFY_ZP(zp); 21115326Sek110237 pzp = zp->z_phys; 2112789Sahrens 21135331Samw mutex_enter(&zp->z_lock); 21145331Samw 21155331Samw /* 21165331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 21175331Samw * Also, if we are the owner don't bother, since owner should 21185331Samw * always be allowed to read basic attributes of file. 21195331Samw */ 21205331Samw if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) && 21215331Samw (pzp->zp_uid != crgetuid(cr))) { 21225331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 21235331Samw skipaclchk, cr)) { 21245331Samw mutex_exit(&zp->z_lock); 21255331Samw ZFS_EXIT(zfsvfs); 21265331Samw return (error); 21275331Samw } 21285331Samw } 21295331Samw 2130789Sahrens /* 2131789Sahrens * Return all attributes. It's cheaper to provide the answer 2132789Sahrens * than to determine whether we were asked the question. 2133789Sahrens */ 2134789Sahrens 2135789Sahrens vap->va_type = vp->v_type; 2136789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 21375771Sjp151216 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2138789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2139789Sahrens vap->va_nodeid = zp->z_id; 21404543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 21414543Smarks links = pzp->zp_links + 1; 21424543Smarks else 21434543Smarks links = pzp->zp_links; 21444543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2145789Sahrens vap->va_size = pzp->zp_size; 21461816Smarks vap->va_rdev = vp->v_rdev; 2147789Sahrens vap->va_seq = zp->z_seq; 2148789Sahrens 21495331Samw /* 21505331Samw * Add in any requested optional attributes and the create time. 21515331Samw * Also set the corresponding bits in the returned attribute bitmap. 21525331Samw */ 21535331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 21545331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 21555331Samw xoap->xoa_archive = 21565331Samw ((pzp->zp_flags & ZFS_ARCHIVE) != 0); 21575331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 21585331Samw } 21595331Samw 21605331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 21615331Samw xoap->xoa_readonly = 21625331Samw ((pzp->zp_flags & ZFS_READONLY) != 0); 21635331Samw XVA_SET_RTN(xvap, XAT_READONLY); 21645331Samw } 21655331Samw 21665331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 21675331Samw xoap->xoa_system = 21685331Samw ((pzp->zp_flags & ZFS_SYSTEM) != 0); 21695331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 21705331Samw } 21715331Samw 21725331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 21735331Samw xoap->xoa_hidden = 21745331Samw ((pzp->zp_flags & ZFS_HIDDEN) != 0); 21755331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 21765331Samw } 21775331Samw 21785331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 21795331Samw xoap->xoa_nounlink = 21805331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0); 21815331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 21825331Samw } 21835331Samw 21845331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 21855331Samw xoap->xoa_immutable = 21865331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0); 21875331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 21885331Samw } 21895331Samw 21905331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 21915331Samw xoap->xoa_appendonly = 21925331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0); 21935331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 21945331Samw } 21955331Samw 21965331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 21975331Samw xoap->xoa_nodump = 21985331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0); 21995331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 22005331Samw } 22015331Samw 22025331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 22035331Samw xoap->xoa_opaque = 22045331Samw ((pzp->zp_flags & ZFS_OPAQUE) != 0); 22055331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 22065331Samw } 22075331Samw 22085331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 22095331Samw xoap->xoa_av_quarantined = 22105331Samw ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0); 22115331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 22125331Samw } 22135331Samw 22145331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 22155331Samw xoap->xoa_av_modified = 22165331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0); 22175331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 22185331Samw } 22195331Samw 22205331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 22215331Samw vp->v_type == VREG && 22225331Samw (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) { 22235331Samw size_t len; 22245331Samw dmu_object_info_t doi; 22255331Samw 22265331Samw /* 22275331Samw * Only VREG files have anti-virus scanstamps, so we 22285331Samw * won't conflict with symlinks in the bonus buffer. 22295331Samw */ 22305331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 22315331Samw len = sizeof (xoap->xoa_av_scanstamp) + 22325331Samw sizeof (znode_phys_t); 22335331Samw if (len <= doi.doi_bonus_size) { 22345331Samw /* 22355331Samw * pzp points to the start of the 22365331Samw * znode_phys_t. pzp + 1 points to the 22375331Samw * first byte after the znode_phys_t. 22385331Samw */ 22395331Samw (void) memcpy(xoap->xoa_av_scanstamp, 22405331Samw pzp + 1, 22415331Samw sizeof (xoap->xoa_av_scanstamp)); 22425331Samw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 22435331Samw } 22445331Samw } 22455331Samw 22465331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 22475331Samw ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime); 22485331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 22495331Samw } 22505331Samw } 22515331Samw 2252789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2253789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2254789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2255789Sahrens 2256789Sahrens mutex_exit(&zp->z_lock); 2257789Sahrens 2258789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2259789Sahrens 2260789Sahrens if (zp->z_blksz == 0) { 2261789Sahrens /* 2262789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2263789Sahrens */ 2264789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2265789Sahrens } 2266789Sahrens 2267789Sahrens ZFS_EXIT(zfsvfs); 2268789Sahrens return (0); 2269789Sahrens } 2270789Sahrens 2271789Sahrens /* 2272789Sahrens * Set the file attributes to the values contained in the 2273789Sahrens * vattr structure. 2274789Sahrens * 2275789Sahrens * IN: vp - vnode of file to be modified. 2276789Sahrens * vap - new attribute values. 22775331Samw * If AT_XVATTR set, then optional attrs are being set 2278789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 22795331Samw * - ATTR_NOACLCHECK (CIFS context only). 2280789Sahrens * cr - credentials of caller. 22815331Samw * ct - caller context 2282789Sahrens * 2283789Sahrens * RETURN: 0 if success 2284789Sahrens * error code if failure 2285789Sahrens * 2286789Sahrens * Timestamps: 2287789Sahrens * vp - ctime updated, mtime updated if size changed. 2288789Sahrens */ 2289789Sahrens /* ARGSUSED */ 2290789Sahrens static int 2291789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2292789Sahrens caller_context_t *ct) 2293789Sahrens { 22945326Sek110237 znode_t *zp = VTOZ(vp); 22955326Sek110237 znode_phys_t *pzp; 2296789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 22975326Sek110237 zilog_t *zilog; 2298789Sahrens dmu_tx_t *tx; 22991878Smaybee vattr_t oldva; 23008190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2301789Sahrens uint_t mask = vap->va_mask; 23021878Smaybee uint_t saved_mask; 23032796Smarks int trim_mask = 0; 2304789Sahrens uint64_t new_mode; 2305*9179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 23061231Smarks znode_t *attrzp; 2307789Sahrens int need_policy = FALSE; 2308789Sahrens int err; 23095331Samw zfs_fuid_info_t *fuidp = NULL; 23105331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23115331Samw xoptattr_t *xoap; 23125824Smarks zfs_acl_t *aclp = NULL; 23135331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2314*9179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 2315789Sahrens 2316789Sahrens if (mask == 0) 2317789Sahrens return (0); 2318789Sahrens 2319789Sahrens if (mask & AT_NOSET) 2320789Sahrens return (EINVAL); 2321789Sahrens 23225367Sahrens ZFS_ENTER(zfsvfs); 23235367Sahrens ZFS_VERIFY_ZP(zp); 23245331Samw 23255331Samw pzp = zp->z_phys; 23265331Samw zilog = zfsvfs->z_log; 23275331Samw 23285331Samw /* 23295331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 23305331Samw * that file system is at proper version level 23315331Samw */ 23325331Samw 23335331Samw if (zfsvfs->z_use_fuids == B_FALSE && 23345331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 23355331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 23365386Stimh (mask & AT_XVATTR))) { 23375386Stimh ZFS_EXIT(zfsvfs); 23385331Samw return (EINVAL); 23395386Stimh } 23405386Stimh 23415386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 23425386Stimh ZFS_EXIT(zfsvfs); 2343789Sahrens return (EISDIR); 23445386Stimh } 23455386Stimh 23465386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 23475386Stimh ZFS_EXIT(zfsvfs); 23481308Smarks return (EINVAL); 23495386Stimh } 23501308Smarks 23515331Samw /* 23525331Samw * If this is an xvattr_t, then get a pointer to the structure of 23535331Samw * optional attributes. If this is NULL, then we have a vattr_t. 23545331Samw */ 23555331Samw xoap = xva_getxoptattr(xvap); 23565331Samw 23578190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 23588190SMark.Shellenbaum@Sun.COM 23595331Samw /* 23605331Samw * Immutable files can only alter immutable bit and atime 23615331Samw */ 23625331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 23635331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 23645386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 23655386Stimh ZFS_EXIT(zfsvfs); 23665331Samw return (EPERM); 23675386Stimh } 23685386Stimh 23695386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 23705386Stimh ZFS_EXIT(zfsvfs); 23715331Samw return (EPERM); 23725386Stimh } 2373789Sahrens 23746064Smarks /* 23756064Smarks * Verify timestamps doesn't overflow 32 bits. 23766064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 23776064Smarks * handle times greater than 2039. This check should be removed 23786064Smarks * once large timestamps are fully supported. 23796064Smarks */ 23806064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 23816064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 23826064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 23836064Smarks ZFS_EXIT(zfsvfs); 23846064Smarks return (EOVERFLOW); 23856064Smarks } 23866064Smarks } 23876064Smarks 2388789Sahrens top: 23891231Smarks attrzp = NULL; 2390789Sahrens 2391789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2392789Sahrens ZFS_EXIT(zfsvfs); 2393789Sahrens return (EROFS); 2394789Sahrens } 2395789Sahrens 2396789Sahrens /* 2397789Sahrens * First validate permissions 2398789Sahrens */ 2399789Sahrens 2400789Sahrens if (mask & AT_SIZE) { 24015331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2402789Sahrens if (err) { 2403789Sahrens ZFS_EXIT(zfsvfs); 2404789Sahrens return (err); 2405789Sahrens } 24061878Smaybee /* 24071878Smaybee * XXX - Note, we are not providing any open 24081878Smaybee * mode flags here (like FNDELAY), so we may 24091878Smaybee * block if there are locks present... this 24101878Smaybee * should be addressed in openat(). 24111878Smaybee */ 24126992Smaybee /* XXX - would it be OK to generate a log record here? */ 24136992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 24141878Smaybee if (err) { 24151878Smaybee ZFS_EXIT(zfsvfs); 24161878Smaybee return (err); 24171878Smaybee } 2418789Sahrens } 2419789Sahrens 24205331Samw if (mask & (AT_ATIME|AT_MTIME) || 24215331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 24225331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 24235331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 24245331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 24255331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 24265331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 24275331Samw skipaclchk, cr); 2428789Sahrens 2429789Sahrens if (mask & (AT_UID|AT_GID)) { 2430789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2431789Sahrens int take_owner; 2432789Sahrens int take_group; 2433789Sahrens 2434789Sahrens /* 2435913Smarks * NOTE: even if a new mode is being set, 2436913Smarks * we may clear S_ISUID/S_ISGID bits. 2437913Smarks */ 2438913Smarks 2439913Smarks if (!(mask & AT_MODE)) 2440913Smarks vap->va_mode = pzp->zp_mode; 2441913Smarks 2442913Smarks /* 2443789Sahrens * Take ownership or chgrp to group we are a member of 2444789Sahrens */ 2445789Sahrens 2446789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 24475331Samw take_group = (mask & AT_GID) && 24485331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2449789Sahrens 2450789Sahrens /* 2451789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2452789Sahrens * take_group must both be set in order to allow taking 2453789Sahrens * ownership. 2454789Sahrens * 2455789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2456789Sahrens * 2457789Sahrens */ 2458789Sahrens 2459789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2460789Sahrens ((idmask == AT_UID) && take_owner) || 2461789Sahrens ((idmask == AT_GID) && take_group)) { 24625331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 24635331Samw skipaclchk, cr) == 0) { 2464789Sahrens /* 2465789Sahrens * Remove setuid/setgid for non-privileged users 2466789Sahrens */ 24671115Smarks secpolicy_setid_clear(vap, cr); 24682796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2469789Sahrens } else { 2470789Sahrens need_policy = TRUE; 2471789Sahrens } 2472789Sahrens } else { 2473789Sahrens need_policy = TRUE; 2474789Sahrens } 2475789Sahrens } 2476789Sahrens 24772796Smarks mutex_enter(&zp->z_lock); 24782796Smarks oldva.va_mode = pzp->zp_mode; 24795771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 24805331Samw if (mask & AT_XVATTR) { 24818190SMark.Shellenbaum@Sun.COM /* 24828190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 24838190SMark.Shellenbaum@Sun.COM * that are actually changing. 24848190SMark.Shellenbaum@Sun.COM * 24858190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 24868190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 24878190SMark.Shellenbaum@Sun.COM */ 24888190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 24898190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 24908190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) { 24918190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 24928190SMark.Shellenbaum@Sun.COM } else { 24938190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 24948190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 24958190SMark.Shellenbaum@Sun.COM } 24968190SMark.Shellenbaum@Sun.COM } 24978190SMark.Shellenbaum@Sun.COM 24988190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 24998190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 25008190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) { 25018190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25028190SMark.Shellenbaum@Sun.COM } else { 25038190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 25048190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 25058190SMark.Shellenbaum@Sun.COM } 25068190SMark.Shellenbaum@Sun.COM } 25078190SMark.Shellenbaum@Sun.COM 25088190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 25098190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 25108190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) { 25118190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25128190SMark.Shellenbaum@Sun.COM } else { 25138190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 25148190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 25158190SMark.Shellenbaum@Sun.COM } 25168190SMark.Shellenbaum@Sun.COM } 25178190SMark.Shellenbaum@Sun.COM 25188190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 25198190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 25208190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NODUMP) != 0)) { 25218190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25228190SMark.Shellenbaum@Sun.COM } else { 25238190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 25248190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 25258190SMark.Shellenbaum@Sun.COM } 25268190SMark.Shellenbaum@Sun.COM } 25278190SMark.Shellenbaum@Sun.COM 25288190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 25298190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 25308190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) { 25318190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25328190SMark.Shellenbaum@Sun.COM } else { 25338190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 25348190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 25358190SMark.Shellenbaum@Sun.COM } 25368190SMark.Shellenbaum@Sun.COM } 25378190SMark.Shellenbaum@Sun.COM 25388190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 25398190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 25408190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 25418190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 25428190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) { 25438190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 25448190SMark.Shellenbaum@Sun.COM } else { 25458190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 25468190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 25478190SMark.Shellenbaum@Sun.COM } 25488190SMark.Shellenbaum@Sun.COM } 25498190SMark.Shellenbaum@Sun.COM 25508190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 25518190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 25528190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 25535331Samw need_policy = TRUE; 25545331Samw } 25555331Samw } 25565331Samw 25572796Smarks mutex_exit(&zp->z_lock); 25582796Smarks 25592796Smarks if (mask & AT_MODE) { 25605331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 25612796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 25622796Smarks &oldva, cr); 25632796Smarks if (err) { 25642796Smarks ZFS_EXIT(zfsvfs); 25652796Smarks return (err); 25662796Smarks } 25672796Smarks trim_mask |= AT_MODE; 25682796Smarks } else { 25692796Smarks need_policy = TRUE; 25702796Smarks } 25712796Smarks } 2572789Sahrens 2573789Sahrens if (need_policy) { 25741115Smarks /* 25751115Smarks * If trim_mask is set then take ownership 25762796Smarks * has been granted or write_acl is present and user 25772796Smarks * has the ability to modify mode. In that case remove 25782796Smarks * UID|GID and or MODE from mask so that 25791115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 25801115Smarks */ 25812796Smarks 25822796Smarks if (trim_mask) { 25832796Smarks saved_mask = vap->va_mask; 25842796Smarks vap->va_mask &= ~trim_mask; 25852796Smarks } 2586789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 25875331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2588789Sahrens if (err) { 2589789Sahrens ZFS_EXIT(zfsvfs); 2590789Sahrens return (err); 2591789Sahrens } 25921115Smarks 25931115Smarks if (trim_mask) 25942796Smarks vap->va_mask |= saved_mask; 2595789Sahrens } 2596789Sahrens 2597789Sahrens /* 2598789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2599789Sahrens * changed va_mask 2600789Sahrens */ 2601789Sahrens mask = vap->va_mask; 2602789Sahrens 2603789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2604789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2605789Sahrens 2606789Sahrens if (mask & AT_MODE) { 26071576Smarks uint64_t pmode = pzp->zp_mode; 26081576Smarks 26091576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2610789Sahrens 26115824Smarks if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) { 26125824Smarks dmu_tx_abort(tx); 26135824Smarks ZFS_EXIT(zfsvfs); 26145824Smarks return (err); 26155824Smarks } 26165331Samw if (pzp->zp_acl.z_acl_extern_obj) { 26175331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 26185331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 26195331Samw pzp->zp_acl.z_acl_version == 26205331Samw ZFS_ACL_VERSION_INITIAL) { 26215331Samw dmu_tx_hold_free(tx, 26225331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26235331Samw DMU_OBJECT_END); 26245331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26255824Smarks 0, aclp->z_acl_bytes); 26265331Samw } else { 26275331Samw dmu_tx_hold_write(tx, 26285331Samw pzp->zp_acl.z_acl_extern_obj, 0, 26295824Smarks aclp->z_acl_bytes); 26305331Samw } 26316180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 26326180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 26336180Smarks 0, aclp->z_acl_bytes); 26345331Samw } 2635789Sahrens } 2636789Sahrens 2637*9179SMark.Shellenbaum@Sun.COM if (mask & (AT_UID | AT_GID)) { 2638*9179SMark.Shellenbaum@Sun.COM if (pzp->zp_xattr) { 2639*9179SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 2640*9179SMark.Shellenbaum@Sun.COM if (err) { 2641*9179SMark.Shellenbaum@Sun.COM dmu_tx_abort(tx); 2642*9179SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 2643*9179SMark.Shellenbaum@Sun.COM if (aclp) 2644*9179SMark.Shellenbaum@Sun.COM zfs_acl_free(aclp); 2645*9179SMark.Shellenbaum@Sun.COM return (err); 2646*9179SMark.Shellenbaum@Sun.COM } 2647*9179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, attrzp->z_id); 2648*9179SMark.Shellenbaum@Sun.COM } 2649*9179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 2650*9179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 2651*9179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 26521231Smarks } 2653*9179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 2654*9179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 2655*9179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 2656*9179SMark.Shellenbaum@Sun.COM } 2657*9179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 2658*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 2659*9179SMark.Shellenbaum@Sun.COM if (zfsvfs->z_fuid_obj == 0) { 2660*9179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 2661*9179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 2662*9179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 2663*9179SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 2664*9179SMark.Shellenbaum@Sun.COM FALSE, NULL); 2665*9179SMark.Shellenbaum@Sun.COM } else { 2666*9179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 2667*9179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 2668*9179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 2669*9179SMark.Shellenbaum@Sun.COM } 2670*9179SMark.Shellenbaum@Sun.COM } 26711231Smarks } 26721231Smarks 26738227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2674789Sahrens if (err) { 26751231Smarks if (attrzp) 26761231Smarks VN_RELE(ZTOV(attrzp)); 26775824Smarks 26785824Smarks if (aclp) { 26795824Smarks zfs_acl_free(aclp); 26805824Smarks aclp = NULL; 26815824Smarks } 26825824Smarks 26838227SNeil.Perrin@Sun.COM if (err == ERESTART) { 26842113Sahrens dmu_tx_wait(tx); 26852113Sahrens dmu_tx_abort(tx); 2686789Sahrens goto top; 2687789Sahrens } 26882113Sahrens dmu_tx_abort(tx); 2689789Sahrens ZFS_EXIT(zfsvfs); 2690789Sahrens return (err); 2691789Sahrens } 2692789Sahrens 2693789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2694789Sahrens 2695789Sahrens /* 2696789Sahrens * Set each attribute requested. 2697789Sahrens * We group settings according to the locks they need to acquire. 2698789Sahrens * 2699789Sahrens * Note: you cannot set ctime directly, although it will be 2700789Sahrens * updated as a side-effect of calling this function. 2701789Sahrens */ 2702789Sahrens 2703789Sahrens mutex_enter(&zp->z_lock); 2704789Sahrens 2705789Sahrens if (mask & AT_MODE) { 27065824Smarks mutex_enter(&zp->z_acl_lock); 27075824Smarks zp->z_phys->zp_mode = new_mode; 2708*9179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 2709789Sahrens ASSERT3U(err, ==, 0); 27105824Smarks mutex_exit(&zp->z_acl_lock); 2711789Sahrens } 2712789Sahrens 27131231Smarks if (attrzp) 27141231Smarks mutex_enter(&attrzp->z_lock); 27151231Smarks 27161231Smarks if (mask & AT_UID) { 2717*9179SMark.Shellenbaum@Sun.COM pzp->zp_uid = new_uid; 2718*9179SMark.Shellenbaum@Sun.COM if (attrzp) 2719*9179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_uid = new_uid; 27201231Smarks } 27211231Smarks 27221231Smarks if (mask & AT_GID) { 2723*9179SMark.Shellenbaum@Sun.COM pzp->zp_gid = new_gid; 27241231Smarks if (attrzp) 2725*9179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_gid = new_gid; 27261231Smarks } 27271231Smarks 27285824Smarks if (aclp) 27295824Smarks zfs_acl_free(aclp); 27305824Smarks 27311231Smarks if (attrzp) 27321231Smarks mutex_exit(&attrzp->z_lock); 2733789Sahrens 2734789Sahrens if (mask & AT_ATIME) 2735789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2736789Sahrens 2737789Sahrens if (mask & AT_MTIME) 2738789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2739789Sahrens 27406992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 27411878Smaybee if (mask & AT_SIZE) 2742789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 27431878Smaybee else if (mask != 0) 2744789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 27455331Samw /* 27465331Samw * Do this after setting timestamps to prevent timestamp 27475331Samw * update from toggling bit 27485331Samw */ 27495331Samw 27505331Samw if (xoap && (mask & AT_XVATTR)) { 27518190SMark.Shellenbaum@Sun.COM 27528190SMark.Shellenbaum@Sun.COM /* 27538190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 27548190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 27558190SMark.Shellenbaum@Sun.COM */ 27568190SMark.Shellenbaum@Sun.COM 27578190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 27588190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 27598190SMark.Shellenbaum@Sun.COM } 27608190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 27618190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 27628190SMark.Shellenbaum@Sun.COM } 27638190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 27648190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 27658190SMark.Shellenbaum@Sun.COM } 27668190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 27678190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 27688190SMark.Shellenbaum@Sun.COM } 27698190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 27708190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 27718190SMark.Shellenbaum@Sun.COM } 27728190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 27738190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 27748190SMark.Shellenbaum@Sun.COM } 27758190SMark.Shellenbaum@Sun.COM 27765331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 27775331Samw size_t len; 27785331Samw dmu_object_info_t doi; 27795331Samw 27805331Samw ASSERT(vp->v_type == VREG); 27815331Samw 27825331Samw /* Grow the bonus buffer if necessary. */ 27835331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 27845331Samw len = sizeof (xoap->xoa_av_scanstamp) + 27855331Samw sizeof (znode_phys_t); 27865331Samw if (len > doi.doi_bonus_size) 27875331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 27885331Samw } 27895331Samw zfs_xvattr_set(zp, xvap); 27905331Samw } 2791789Sahrens 2792*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 2793*9179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 2794*9179SMark.Shellenbaum@Sun.COM 27951878Smaybee if (mask != 0) 27965331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 27975331Samw 27985331Samw if (fuidp) 27995331Samw zfs_fuid_info_free(fuidp); 2800789Sahrens mutex_exit(&zp->z_lock); 2801789Sahrens 2802*9179SMark.Shellenbaum@Sun.COM dmu_tx_commit(tx); 2803*9179SMark.Shellenbaum@Sun.COM 28041231Smarks if (attrzp) 28051231Smarks VN_RELE(ZTOV(attrzp)); 28061231Smarks 2807789Sahrens 2808789Sahrens ZFS_EXIT(zfsvfs); 2809789Sahrens return (err); 2810789Sahrens } 2811789Sahrens 28123271Smaybee typedef struct zfs_zlock { 28133271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 28143271Smaybee znode_t *zl_znode; /* znode we held */ 28153271Smaybee struct zfs_zlock *zl_next; /* next in list */ 28163271Smaybee } zfs_zlock_t; 28173271Smaybee 28183271Smaybee /* 28193271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 28203271Smaybee */ 28213271Smaybee static void 28223271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 28233271Smaybee { 28243271Smaybee zfs_zlock_t *zl; 28253271Smaybee 28263271Smaybee while ((zl = *zlpp) != NULL) { 28273271Smaybee if (zl->zl_znode != NULL) 28283271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 28293271Smaybee rw_exit(zl->zl_rwlock); 28303271Smaybee *zlpp = zl->zl_next; 28313271Smaybee kmem_free(zl, sizeof (*zl)); 28323271Smaybee } 28333271Smaybee } 28343271Smaybee 2835789Sahrens /* 2836789Sahrens * Search back through the directory tree, using the ".." entries. 2837789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2838789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2839789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2840789Sahrens */ 2841789Sahrens static int 2842789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2843789Sahrens { 2844789Sahrens zfs_zlock_t *zl; 28453638Sbillm znode_t *zp = tdzp; 2846789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2847789Sahrens uint64_t *oidp = &zp->z_id; 2848789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2849789Sahrens krw_t rw = RW_WRITER; 2850789Sahrens 2851789Sahrens /* 2852789Sahrens * First pass write-locks szp and compares to zp->z_id. 2853789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2854789Sahrens */ 2855789Sahrens do { 28563271Smaybee if (!rw_tryenter(rwlp, rw)) { 28573271Smaybee /* 28583271Smaybee * Another thread is renaming in this path. 28593271Smaybee * Note that if we are a WRITER, we don't have any 28603271Smaybee * parent_locks held yet. 28613271Smaybee */ 28623271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 28633271Smaybee /* 28643271Smaybee * Drop our locks and restart 28653271Smaybee */ 28663271Smaybee zfs_rename_unlock(&zl); 28673271Smaybee *zlpp = NULL; 28683271Smaybee zp = tdzp; 28693271Smaybee oidp = &zp->z_id; 28703271Smaybee rwlp = &szp->z_parent_lock; 28713271Smaybee rw = RW_WRITER; 28723271Smaybee continue; 28733271Smaybee } else { 28743271Smaybee /* 28753271Smaybee * Wait for other thread to drop its locks 28763271Smaybee */ 28773271Smaybee rw_enter(rwlp, rw); 28783271Smaybee } 28793271Smaybee } 28803271Smaybee 2881789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2882789Sahrens zl->zl_rwlock = rwlp; 2883789Sahrens zl->zl_znode = NULL; 2884789Sahrens zl->zl_next = *zlpp; 2885789Sahrens *zlpp = zl; 2886789Sahrens 2887789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2888789Sahrens return (EINVAL); 2889789Sahrens 2890789Sahrens if (*oidp == rootid) /* We've hit the top */ 2891789Sahrens return (0); 2892789Sahrens 2893789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2894789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2895789Sahrens if (error) 2896789Sahrens return (error); 2897789Sahrens zl->zl_znode = zp; 2898789Sahrens } 2899789Sahrens oidp = &zp->z_phys->zp_parent; 2900789Sahrens rwlp = &zp->z_parent_lock; 2901789Sahrens rw = RW_READER; 2902789Sahrens 2903789Sahrens } while (zp->z_id != sdzp->z_id); 2904789Sahrens 2905789Sahrens return (0); 2906789Sahrens } 2907789Sahrens 2908789Sahrens /* 2909789Sahrens * Move an entry from the provided source directory to the target 2910789Sahrens * directory. Change the entry name as indicated. 2911789Sahrens * 2912789Sahrens * IN: sdvp - Source directory containing the "old entry". 2913789Sahrens * snm - Old entry name. 2914789Sahrens * tdvp - Target directory to contain the "new entry". 2915789Sahrens * tnm - New entry name. 2916789Sahrens * cr - credentials of caller. 29175331Samw * ct - caller context 29185331Samw * flags - case flags 2919789Sahrens * 2920789Sahrens * RETURN: 0 if success 2921789Sahrens * error code if failure 2922789Sahrens * 2923789Sahrens * Timestamps: 2924789Sahrens * sdvp,tdvp - ctime|mtime updated 2925789Sahrens */ 29265331Samw /*ARGSUSED*/ 2927789Sahrens static int 29285331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 29295331Samw caller_context_t *ct, int flags) 2930789Sahrens { 2931789Sahrens znode_t *tdzp, *szp, *tzp; 2932789Sahrens znode_t *sdzp = VTOZ(sdvp); 2933789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 29345326Sek110237 zilog_t *zilog; 2935789Sahrens vnode_t *realvp; 2936789Sahrens zfs_dirlock_t *sdl, *tdl; 2937789Sahrens dmu_tx_t *tx; 2938789Sahrens zfs_zlock_t *zl; 29395331Samw int cmp, serr, terr; 29405331Samw int error = 0; 29415331Samw int zflg = 0; 2942789Sahrens 29435367Sahrens ZFS_ENTER(zfsvfs); 29445367Sahrens ZFS_VERIFY_ZP(sdzp); 29455326Sek110237 zilog = zfsvfs->z_log; 2946789Sahrens 2947789Sahrens /* 2948789Sahrens * Make sure we have the real vp for the target directory. 2949789Sahrens */ 29505331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 2951789Sahrens tdvp = realvp; 2952789Sahrens 2953789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2954789Sahrens ZFS_EXIT(zfsvfs); 2955789Sahrens return (EXDEV); 2956789Sahrens } 2957789Sahrens 2958789Sahrens tdzp = VTOZ(tdvp); 29595367Sahrens ZFS_VERIFY_ZP(tdzp); 29605498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 29615331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 29625331Samw ZFS_EXIT(zfsvfs); 29635331Samw return (EILSEQ); 29645331Samw } 29655331Samw 29665331Samw if (flags & FIGNORECASE) 29675331Samw zflg |= ZCILOOK; 29685331Samw 2969789Sahrens top: 2970789Sahrens szp = NULL; 2971789Sahrens tzp = NULL; 2972789Sahrens zl = NULL; 2973789Sahrens 2974789Sahrens /* 2975789Sahrens * This is to prevent the creation of links into attribute space 2976789Sahrens * by renaming a linked file into/outof an attribute directory. 2977789Sahrens * See the comment in zfs_link() for why this is considered bad. 2978789Sahrens */ 2979789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2980789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2981789Sahrens ZFS_EXIT(zfsvfs); 2982789Sahrens return (EINVAL); 2983789Sahrens } 2984789Sahrens 2985789Sahrens /* 2986789Sahrens * Lock source and target directory entries. To prevent deadlock, 2987789Sahrens * a lock ordering must be defined. We lock the directory with 2988789Sahrens * the smallest object id first, or if it's a tie, the one with 2989789Sahrens * the lexically first name. 2990789Sahrens */ 2991789Sahrens if (sdzp->z_id < tdzp->z_id) { 2992789Sahrens cmp = -1; 2993789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2994789Sahrens cmp = 1; 2995789Sahrens } else { 29965331Samw /* 29975331Samw * First compare the two name arguments without 29985331Samw * considering any case folding. 29995331Samw */ 30005331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 30015331Samw 30025331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 30035498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3004789Sahrens if (cmp == 0) { 3005789Sahrens /* 3006789Sahrens * POSIX: "If the old argument and the new argument 3007789Sahrens * both refer to links to the same existing file, 3008789Sahrens * the rename() function shall return successfully 3009789Sahrens * and perform no other action." 3010789Sahrens */ 3011789Sahrens ZFS_EXIT(zfsvfs); 3012789Sahrens return (0); 3013789Sahrens } 30145331Samw /* 30155331Samw * If the file system is case-folding, then we may 30165331Samw * have some more checking to do. A case-folding file 30175331Samw * system is either supporting mixed case sensitivity 30185331Samw * access or is completely case-insensitive. Note 30195331Samw * that the file system is always case preserving. 30205331Samw * 30215331Samw * In mixed sensitivity mode case sensitive behavior 30225331Samw * is the default. FIGNORECASE must be used to 30235331Samw * explicitly request case insensitive behavior. 30245331Samw * 30255331Samw * If the source and target names provided differ only 30265331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 30275331Samw * we will treat this as a special case in the 30285331Samw * case-insensitive mode: as long as the source name 30295331Samw * is an exact match, we will allow this to proceed as 30305331Samw * a name-change request. 30315331Samw */ 30325498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 30335498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 30345498Stimh flags & FIGNORECASE)) && 30355331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 30365331Samw &error) == 0) { 30375331Samw /* 30385331Samw * case preserving rename request, require exact 30395331Samw * name matches 30405331Samw */ 30415331Samw zflg |= ZCIEXACT; 30425331Samw zflg &= ~ZCILOOK; 30435331Samw } 3044789Sahrens } 30455331Samw 3046789Sahrens if (cmp < 0) { 30475331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 30485331Samw ZEXISTS | zflg, NULL, NULL); 30495331Samw terr = zfs_dirent_lock(&tdl, 30505331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3051789Sahrens } else { 30525331Samw terr = zfs_dirent_lock(&tdl, 30535331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 30545331Samw serr = zfs_dirent_lock(&sdl, 30555331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 30565331Samw NULL, NULL); 3057789Sahrens } 3058789Sahrens 3059789Sahrens if (serr) { 3060789Sahrens /* 3061789Sahrens * Source entry invalid or not there. 3062789Sahrens */ 3063789Sahrens if (!terr) { 3064789Sahrens zfs_dirent_unlock(tdl); 3065789Sahrens if (tzp) 3066789Sahrens VN_RELE(ZTOV(tzp)); 3067789Sahrens } 3068789Sahrens if (strcmp(snm, "..") == 0) 3069789Sahrens serr = EINVAL; 3070789Sahrens ZFS_EXIT(zfsvfs); 3071789Sahrens return (serr); 3072789Sahrens } 3073789Sahrens if (terr) { 3074789Sahrens zfs_dirent_unlock(sdl); 3075789Sahrens VN_RELE(ZTOV(szp)); 3076789Sahrens if (strcmp(tnm, "..") == 0) 3077789Sahrens terr = EINVAL; 3078789Sahrens ZFS_EXIT(zfsvfs); 3079789Sahrens return (terr); 3080789Sahrens } 3081789Sahrens 3082789Sahrens /* 3083789Sahrens * Must have write access at the source to remove the old entry 3084789Sahrens * and write access at the target to create the new entry. 3085789Sahrens * Note that if target and source are the same, this can be 3086789Sahrens * done in a single check. 3087789Sahrens */ 3088789Sahrens 3089789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3090789Sahrens goto out; 3091789Sahrens 3092789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3093789Sahrens /* 3094789Sahrens * Check to make sure rename is valid. 3095789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3096789Sahrens */ 3097789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3098789Sahrens goto out; 3099789Sahrens } 3100789Sahrens 3101789Sahrens /* 3102789Sahrens * Does target exist? 3103789Sahrens */ 3104789Sahrens if (tzp) { 3105789Sahrens /* 3106789Sahrens * Source and target must be the same type. 3107789Sahrens */ 3108789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3109789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3110789Sahrens error = ENOTDIR; 3111789Sahrens goto out; 3112789Sahrens } 3113789Sahrens } else { 3114789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3115789Sahrens error = EISDIR; 3116789Sahrens goto out; 3117789Sahrens } 3118789Sahrens } 3119789Sahrens /* 3120789Sahrens * POSIX dictates that when the source and target 3121789Sahrens * entries refer to the same file object, rename 3122789Sahrens * must do nothing and exit without error. 3123789Sahrens */ 3124789Sahrens if (szp->z_id == tzp->z_id) { 3125789Sahrens error = 0; 3126789Sahrens goto out; 3127789Sahrens } 3128789Sahrens } 3129789Sahrens 31305331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3131789Sahrens if (tzp) 31325331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 31334863Spraks 31344863Spraks /* 31354863Spraks * notify the target directory if it is not the same 31364863Spraks * as source directory. 31374863Spraks */ 31384863Spraks if (tdvp != sdvp) { 31395331Samw vnevent_rename_dest_dir(tdvp, ct); 31404863Spraks } 3141789Sahrens 3142789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3143789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3144789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 31451544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 31461544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 31471544Seschrock if (sdzp != tdzp) 3148789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 31491544Seschrock if (tzp) 31501544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 31513461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 31528227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3153789Sahrens if (error) { 3154789Sahrens if (zl != NULL) 3155789Sahrens zfs_rename_unlock(&zl); 3156789Sahrens zfs_dirent_unlock(sdl); 3157789Sahrens zfs_dirent_unlock(tdl); 3158789Sahrens VN_RELE(ZTOV(szp)); 3159789Sahrens if (tzp) 3160789Sahrens VN_RELE(ZTOV(tzp)); 31618227SNeil.Perrin@Sun.COM if (error == ERESTART) { 31622113Sahrens dmu_tx_wait(tx); 31632113Sahrens dmu_tx_abort(tx); 3164789Sahrens goto top; 3165789Sahrens } 31662113Sahrens dmu_tx_abort(tx); 3167789Sahrens ZFS_EXIT(zfsvfs); 3168789Sahrens return (error); 3169789Sahrens } 3170789Sahrens 3171789Sahrens if (tzp) /* Attempt to remove the existing target */ 31725331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3173789Sahrens 3174789Sahrens if (error == 0) { 3175789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3176789Sahrens if (error == 0) { 31775331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 31785331Samw 3179789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3180789Sahrens ASSERT(error == 0); 31815331Samw 31825331Samw zfs_log_rename(zilog, tx, 31835331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 31845331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 31856976Seschrock 31866976Seschrock /* Update path information for the target vnode */ 31876976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3188789Sahrens } 3189789Sahrens } 3190789Sahrens 3191789Sahrens dmu_tx_commit(tx); 3192789Sahrens out: 3193789Sahrens if (zl != NULL) 3194789Sahrens zfs_rename_unlock(&zl); 3195789Sahrens 3196789Sahrens zfs_dirent_unlock(sdl); 3197789Sahrens zfs_dirent_unlock(tdl); 3198789Sahrens 3199789Sahrens VN_RELE(ZTOV(szp)); 3200789Sahrens if (tzp) 3201789Sahrens VN_RELE(ZTOV(tzp)); 3202789Sahrens 3203789Sahrens ZFS_EXIT(zfsvfs); 3204789Sahrens return (error); 3205789Sahrens } 3206789Sahrens 3207789Sahrens /* 3208789Sahrens * Insert the indicated symbolic reference entry into the directory. 3209789Sahrens * 3210789Sahrens * IN: dvp - Directory to contain new symbolic link. 3211789Sahrens * link - Name for new symlink entry. 3212789Sahrens * vap - Attributes of new entry. 3213789Sahrens * target - Target path of new symlink. 3214789Sahrens * cr - credentials of caller. 32155331Samw * ct - caller context 32165331Samw * flags - case flags 3217789Sahrens * 3218789Sahrens * RETURN: 0 if success 3219789Sahrens * error code if failure 3220789Sahrens * 3221789Sahrens * Timestamps: 3222789Sahrens * dvp - ctime|mtime updated 3223789Sahrens */ 32245331Samw /*ARGSUSED*/ 3225789Sahrens static int 32265331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 32275331Samw caller_context_t *ct, int flags) 3228789Sahrens { 3229789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3230789Sahrens zfs_dirlock_t *dl; 3231789Sahrens dmu_tx_t *tx; 3232789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 32335326Sek110237 zilog_t *zilog; 3234789Sahrens int len = strlen(link); 3235789Sahrens int error; 32365331Samw int zflg = ZNEW; 3237*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 3238*9179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 3239789Sahrens 3240789Sahrens ASSERT(vap->va_type == VLNK); 3241789Sahrens 32425367Sahrens ZFS_ENTER(zfsvfs); 32435367Sahrens ZFS_VERIFY_ZP(dzp); 32445326Sek110237 zilog = zfsvfs->z_log; 32455331Samw 32465498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 32475331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 32485331Samw ZFS_EXIT(zfsvfs); 32495331Samw return (EILSEQ); 32505331Samw } 32515331Samw if (flags & FIGNORECASE) 32525331Samw zflg |= ZCILOOK; 3253789Sahrens top: 32545331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3255789Sahrens ZFS_EXIT(zfsvfs); 3256789Sahrens return (error); 3257789Sahrens } 3258789Sahrens 3259789Sahrens if (len > MAXPATHLEN) { 3260789Sahrens ZFS_EXIT(zfsvfs); 3261789Sahrens return (ENAMETOOLONG); 3262789Sahrens } 3263789Sahrens 3264789Sahrens /* 3265789Sahrens * Attempt to lock directory; fail if entry already exists. 3266789Sahrens */ 32675331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 32685331Samw if (error) { 3269789Sahrens ZFS_EXIT(zfsvfs); 3270789Sahrens return (error); 3271789Sahrens } 3272789Sahrens 3273*9179SMark.Shellenbaum@Sun.COM VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids)); 3274789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3275*9179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3276789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3277789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 32781544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 3279*9179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 3280789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 3281*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 32825824Smarks if (zfsvfs->z_fuid_obj == 0) { 32835824Smarks dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 32845824Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 32855824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32865824Smarks dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, FALSE, NULL); 32875824Smarks } else { 32885824Smarks dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 32895824Smarks dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 32905824Smarks FUID_SIZE_ESTIMATE(zfsvfs)); 32915824Smarks } 32925331Samw } 32938227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3294789Sahrens if (error) { 3295*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3296789Sahrens zfs_dirent_unlock(dl); 32978227SNeil.Perrin@Sun.COM if (error == ERESTART) { 32982113Sahrens dmu_tx_wait(tx); 32992113Sahrens dmu_tx_abort(tx); 3300789Sahrens goto top; 3301789Sahrens } 33022113Sahrens dmu_tx_abort(tx); 3303789Sahrens ZFS_EXIT(zfsvfs); 3304789Sahrens return (error); 3305789Sahrens } 3306789Sahrens 3307789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3308789Sahrens 3309789Sahrens /* 3310789Sahrens * Create a new object for the symlink. 3311789Sahrens * Put the link content into bonus buffer if it will fit; 3312789Sahrens * otherwise, store it just like any other file data. 3313789Sahrens */ 3314789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 3315*9179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids); 3316789Sahrens if (len != 0) 3317789Sahrens bcopy(link, zp->z_phys + 1, len); 3318789Sahrens } else { 3319789Sahrens dmu_buf_t *dbp; 33201669Sperrin 3321*9179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 3322*9179SMark.Shellenbaum@Sun.COM 3323*9179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 3324*9179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 33251669Sperrin /* 33261669Sperrin * Nothing can access the znode yet so no locking needed 33271669Sperrin * for growing the znode's blocksize. 33281669Sperrin */ 33291669Sperrin zfs_grow_blocksize(zp, len, tx); 3330789Sahrens 33315446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 33325446Sahrens zp->z_id, 0, FTAG, &dbp)); 3333789Sahrens dmu_buf_will_dirty(dbp, tx); 3334789Sahrens 3335789Sahrens ASSERT3U(len, <=, dbp->db_size); 3336789Sahrens bcopy(link, dbp->db_data, len); 33371544Seschrock dmu_buf_rele(dbp, FTAG); 3338789Sahrens } 3339789Sahrens zp->z_phys->zp_size = len; 3340789Sahrens 3341789Sahrens /* 3342789Sahrens * Insert the new object into the directory. 3343789Sahrens */ 3344789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 3345789Sahrens out: 33465331Samw if (error == 0) { 33475331Samw uint64_t txtype = TX_SYMLINK; 33485331Samw if (flags & FIGNORECASE) 33495331Samw txtype |= TX_CI; 33505331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 33515331Samw } 3352*9179SMark.Shellenbaum@Sun.COM 3353*9179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3354789Sahrens 3355789Sahrens dmu_tx_commit(tx); 3356789Sahrens 3357789Sahrens zfs_dirent_unlock(dl); 3358789Sahrens 3359789Sahrens VN_RELE(ZTOV(zp)); 3360789Sahrens 3361789Sahrens ZFS_EXIT(zfsvfs); 3362789Sahrens return (error); 3363789Sahrens } 3364789Sahrens 3365789Sahrens /* 3366789Sahrens * Return, in the buffer contained in the provided uio structure, 3367789Sahrens * the symbolic path referred to by vp. 3368789Sahrens * 3369789Sahrens * IN: vp - vnode of symbolic link. 3370789Sahrens * uoip - structure to contain the link path. 3371789Sahrens * cr - credentials of caller. 33725331Samw * ct - caller context 3373789Sahrens * 3374789Sahrens * OUT: uio - structure to contain the link path. 3375789Sahrens * 3376789Sahrens * RETURN: 0 if success 3377789Sahrens * error code if failure 3378789Sahrens * 3379789Sahrens * Timestamps: 3380789Sahrens * vp - atime updated 3381789Sahrens */ 3382789Sahrens /* ARGSUSED */ 3383789Sahrens static int 33845331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3385789Sahrens { 3386789Sahrens znode_t *zp = VTOZ(vp); 3387789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3388789Sahrens size_t bufsz; 3389789Sahrens int error; 3390789Sahrens 33915367Sahrens ZFS_ENTER(zfsvfs); 33925367Sahrens ZFS_VERIFY_ZP(zp); 3393789Sahrens 3394789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3395789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3396789Sahrens error = uiomove(zp->z_phys + 1, 3397789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3398789Sahrens } else { 33991544Seschrock dmu_buf_t *dbp; 34001544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 34011544Seschrock if (error) { 3402789Sahrens ZFS_EXIT(zfsvfs); 3403789Sahrens return (error); 3404789Sahrens } 3405789Sahrens error = uiomove(dbp->db_data, 3406789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 34071544Seschrock dmu_buf_rele(dbp, FTAG); 3408789Sahrens } 3409789Sahrens 3410789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3411789Sahrens ZFS_EXIT(zfsvfs); 3412789Sahrens return (error); 3413789Sahrens } 3414789Sahrens 3415789Sahrens /* 3416789Sahrens * Insert a new entry into directory tdvp referencing svp. 3417789Sahrens * 3418789Sahrens * IN: tdvp - Directory to contain new entry. 3419789Sahrens * svp - vnode of new entry. 3420789Sahrens * name - name of new entry. 3421789Sahrens * cr - credentials of caller. 34225331Samw * ct - caller context 3423789Sahrens * 3424789Sahrens * RETURN: 0 if success 3425789Sahrens * error code if failure 3426789Sahrens * 3427789Sahrens * Timestamps: 3428789Sahrens * tdvp - ctime|mtime updated 3429789Sahrens * svp - ctime updated 3430789Sahrens */ 3431789Sahrens /* ARGSUSED */ 3432789Sahrens static int 34335331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 34345331Samw caller_context_t *ct, int flags) 3435789Sahrens { 3436789Sahrens znode_t *dzp = VTOZ(tdvp); 3437789Sahrens znode_t *tzp, *szp; 3438789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 34395326Sek110237 zilog_t *zilog; 3440789Sahrens zfs_dirlock_t *dl; 3441789Sahrens dmu_tx_t *tx; 3442789Sahrens vnode_t *realvp; 3443789Sahrens int error; 34445331Samw int zf = ZNEW; 34455331Samw uid_t owner; 3446789Sahrens 3447789Sahrens ASSERT(tdvp->v_type == VDIR); 3448789Sahrens 34495367Sahrens ZFS_ENTER(zfsvfs); 34505367Sahrens ZFS_VERIFY_ZP(dzp); 34515326Sek110237 zilog = zfsvfs->z_log; 3452789Sahrens 34535331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3454789Sahrens svp = realvp; 3455789Sahrens 3456789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3457789Sahrens ZFS_EXIT(zfsvfs); 3458789Sahrens return (EXDEV); 3459789Sahrens } 34605367Sahrens szp = VTOZ(svp); 34615367Sahrens ZFS_VERIFY_ZP(szp); 3462789Sahrens 34635498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 34645331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 34655331Samw ZFS_EXIT(zfsvfs); 34665331Samw return (EILSEQ); 34675331Samw } 34685331Samw if (flags & FIGNORECASE) 34695331Samw zf |= ZCILOOK; 34705331Samw 3471789Sahrens top: 3472789Sahrens /* 3473789Sahrens * We do not support links between attributes and non-attributes 3474789Sahrens * because of the potential security risk of creating links 3475789Sahrens * into "normal" file space in order to circumvent restrictions 3476789Sahrens * imposed in attribute space. 3477789Sahrens */ 3478789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3479789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3480789Sahrens ZFS_EXIT(zfsvfs); 3481789Sahrens return (EINVAL); 3482789Sahrens } 3483789Sahrens 3484789Sahrens /* 3485789Sahrens * POSIX dictates that we return EPERM here. 3486789Sahrens * Better choices include ENOTSUP or EISDIR. 3487789Sahrens */ 3488789Sahrens if (svp->v_type == VDIR) { 3489789Sahrens ZFS_EXIT(zfsvfs); 3490789Sahrens return (EPERM); 3491789Sahrens } 3492789Sahrens 34935959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 34945331Samw if (owner != crgetuid(cr) && 3495789Sahrens secpolicy_basic_link(cr) != 0) { 3496789Sahrens ZFS_EXIT(zfsvfs); 3497789Sahrens return (EPERM); 3498789Sahrens } 3499789Sahrens 35005331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3501789Sahrens ZFS_EXIT(zfsvfs); 3502789Sahrens return (error); 3503789Sahrens } 3504789Sahrens 3505789Sahrens /* 3506789Sahrens * Attempt to lock directory; fail if entry already exists. 3507789Sahrens */ 35085331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 35095331Samw if (error) { 3510789Sahrens ZFS_EXIT(zfsvfs); 3511789Sahrens return (error); 3512789Sahrens } 3513789Sahrens 3514789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3515789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 35161544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 35178227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3518789Sahrens if (error) { 3519789Sahrens zfs_dirent_unlock(dl); 35208227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35212113Sahrens dmu_tx_wait(tx); 35222113Sahrens dmu_tx_abort(tx); 3523789Sahrens goto top; 3524789Sahrens } 35252113Sahrens dmu_tx_abort(tx); 3526789Sahrens ZFS_EXIT(zfsvfs); 3527789Sahrens return (error); 3528789Sahrens } 3529789Sahrens 3530789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3531789Sahrens 35325331Samw if (error == 0) { 35335331Samw uint64_t txtype = TX_LINK; 35345331Samw if (flags & FIGNORECASE) 35355331Samw txtype |= TX_CI; 35365331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 35375331Samw } 3538789Sahrens 3539789Sahrens dmu_tx_commit(tx); 3540789Sahrens 3541789Sahrens zfs_dirent_unlock(dl); 3542789Sahrens 35434863Spraks if (error == 0) { 35445331Samw vnevent_link(svp, ct); 35454863Spraks } 35464863Spraks 3547789Sahrens ZFS_EXIT(zfsvfs); 3548789Sahrens return (error); 3549789Sahrens } 3550789Sahrens 3551789Sahrens /* 3552789Sahrens * zfs_null_putapage() is used when the file system has been force 3553789Sahrens * unmounted. It just drops the pages. 3554789Sahrens */ 3555789Sahrens /* ARGSUSED */ 3556789Sahrens static int 3557789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3558789Sahrens size_t *lenp, int flags, cred_t *cr) 3559789Sahrens { 3560789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3561789Sahrens return (0); 3562789Sahrens } 3563789Sahrens 35642688Smaybee /* 35652688Smaybee * Push a page out to disk, klustering if possible. 35662688Smaybee * 35672688Smaybee * IN: vp - file to push page to. 35682688Smaybee * pp - page to push. 35692688Smaybee * flags - additional flags. 35702688Smaybee * cr - credentials of caller. 35712688Smaybee * 35722688Smaybee * OUT: offp - start of range pushed. 35732688Smaybee * lenp - len of range pushed. 35742688Smaybee * 35752688Smaybee * RETURN: 0 if success 35762688Smaybee * error code if failure 35772688Smaybee * 35782688Smaybee * NOTE: callers must have locked the page to be pushed. On 35792688Smaybee * exit, the page (and all other pages in the kluster) must be 35802688Smaybee * unlocked. 35812688Smaybee */ 3582789Sahrens /* ARGSUSED */ 3583789Sahrens static int 3584789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3585789Sahrens size_t *lenp, int flags, cred_t *cr) 3586789Sahrens { 3587789Sahrens znode_t *zp = VTOZ(vp); 3588789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3589789Sahrens dmu_tx_t *tx; 35902688Smaybee u_offset_t off, koff; 35912688Smaybee size_t len, klen; 35924709Smaybee uint64_t filesz; 3593789Sahrens int err; 3594789Sahrens 35954709Smaybee filesz = zp->z_phys->zp_size; 35962688Smaybee off = pp->p_offset; 35972688Smaybee len = PAGESIZE; 35982688Smaybee /* 35992688Smaybee * If our blocksize is bigger than the page size, try to kluster 36008227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 36012688Smaybee * a read-modify-write). 36022688Smaybee */ 36034709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 36048636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 36058636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 36062688Smaybee ASSERT(koff <= filesz); 36072688Smaybee if (koff + klen > filesz) 36082688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 36092688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 36102688Smaybee } 36112688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 36128636SMark.Maybee@Sun.COM 36131819Smaybee /* 36141819Smaybee * Can't push pages past end-of-file. 36151819Smaybee */ 36164709Smaybee if (off >= filesz) { 36174709Smaybee /* ignore all pages */ 36182688Smaybee err = 0; 36192688Smaybee goto out; 36204709Smaybee } else if (off + len > filesz) { 36214709Smaybee int npages = btopr(filesz - off); 36222688Smaybee page_t *trunc; 36232688Smaybee 36242688Smaybee page_list_break(&pp, &trunc, npages); 36254709Smaybee /* ignore pages past end of file */ 36262688Smaybee if (trunc) 36274709Smaybee pvn_write_done(trunc, flags); 36284709Smaybee len = filesz - off; 36291819Smaybee } 36308636SMark.Maybee@Sun.COM top: 3631789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3632789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3633789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 36348227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 3635789Sahrens if (err != 0) { 36368227SNeil.Perrin@Sun.COM if (err == ERESTART) { 36372113Sahrens dmu_tx_wait(tx); 36382113Sahrens dmu_tx_abort(tx); 3639789Sahrens goto top; 3640789Sahrens } 36412113Sahrens dmu_tx_abort(tx); 3642789Sahrens goto out; 3643789Sahrens } 3644789Sahrens 36452688Smaybee if (zp->z_blksz <= PAGESIZE) { 36467315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 36472688Smaybee ASSERT3U(len, <=, PAGESIZE); 36482688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 36497315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 36502688Smaybee } else { 36512688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 36522688Smaybee } 36532688Smaybee 36542688Smaybee if (err == 0) { 36552688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 36568636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 36572688Smaybee dmu_tx_commit(tx); 36582688Smaybee } 36592688Smaybee 36602688Smaybee out: 36614709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3662789Sahrens if (offp) 3663789Sahrens *offp = off; 3664789Sahrens if (lenp) 3665789Sahrens *lenp = len; 3666789Sahrens 3667789Sahrens return (err); 3668789Sahrens } 3669789Sahrens 3670789Sahrens /* 3671789Sahrens * Copy the portion of the file indicated from pages into the file. 3672789Sahrens * The pages are stored in a page list attached to the files vnode. 3673789Sahrens * 3674789Sahrens * IN: vp - vnode of file to push page data to. 3675789Sahrens * off - position in file to put data. 3676789Sahrens * len - amount of data to write. 3677789Sahrens * flags - flags to control the operation. 3678789Sahrens * cr - credentials of caller. 36795331Samw * ct - caller context. 3680789Sahrens * 3681789Sahrens * RETURN: 0 if success 3682789Sahrens * error code if failure 3683789Sahrens * 3684789Sahrens * Timestamps: 3685789Sahrens * vp - ctime|mtime updated 3686789Sahrens */ 36875331Samw /*ARGSUSED*/ 3688789Sahrens static int 36895331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 36905331Samw caller_context_t *ct) 3691789Sahrens { 3692789Sahrens znode_t *zp = VTOZ(vp); 3693789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3694789Sahrens page_t *pp; 3695789Sahrens size_t io_len; 3696789Sahrens u_offset_t io_off; 36978636SMark.Maybee@Sun.COM uint_t blksz; 36988636SMark.Maybee@Sun.COM rl_t *rl; 3699789Sahrens int error = 0; 3700789Sahrens 37015367Sahrens ZFS_ENTER(zfsvfs); 37025367Sahrens ZFS_VERIFY_ZP(zp); 3703789Sahrens 37048636SMark.Maybee@Sun.COM /* 37058636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 37068636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 37078636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 37088636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 37098636SMark.Maybee@Sun.COM * operations to get narrower locking. 37108636SMark.Maybee@Sun.COM */ 37118636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 37128636SMark.Maybee@Sun.COM if (ISP2(blksz)) 37138636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 37148636SMark.Maybee@Sun.COM else 37158636SMark.Maybee@Sun.COM io_off = 0; 37168636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 37179141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 37188636SMark.Maybee@Sun.COM else 37198636SMark.Maybee@Sun.COM io_len = 0; 37208636SMark.Maybee@Sun.COM 37218636SMark.Maybee@Sun.COM if (io_len == 0) { 3722789Sahrens /* 37238636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 3724789Sahrens */ 37258636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 37268636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 37271472Sperrin goto out; 3728789Sahrens } 37298636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 37308636SMark.Maybee@Sun.COM 37318636SMark.Maybee@Sun.COM if (off > zp->z_phys->zp_size) { 3732789Sahrens /* past end of file */ 37338636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 3734789Sahrens ZFS_EXIT(zfsvfs); 3735789Sahrens return (0); 3736789Sahrens } 3737789Sahrens 37388636SMark.Maybee@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off); 37398636SMark.Maybee@Sun.COM 37408636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 3741789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 37421669Sperrin pp = page_lookup(vp, io_off, 37434339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3744789Sahrens } else { 3745789Sahrens pp = page_lookup_nowait(vp, io_off, 37464339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3747789Sahrens } 3748789Sahrens 3749789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3750789Sahrens int err; 3751789Sahrens 3752789Sahrens /* 3753789Sahrens * Found a dirty page to push 3754789Sahrens */ 37551669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 37561669Sperrin if (err) 3757789Sahrens error = err; 3758789Sahrens } else { 3759789Sahrens io_len = PAGESIZE; 3760789Sahrens } 3761789Sahrens } 37621472Sperrin out: 37638636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 37642638Sperrin if ((flags & B_ASYNC) == 0) 37652638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3766789Sahrens ZFS_EXIT(zfsvfs); 3767789Sahrens return (error); 3768789Sahrens } 3769789Sahrens 37705331Samw /*ARGSUSED*/ 3771789Sahrens void 37725331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3773789Sahrens { 3774789Sahrens znode_t *zp = VTOZ(vp); 3775789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3776789Sahrens int error; 3777789Sahrens 37785326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 37795446Sahrens if (zp->z_dbuf == NULL) { 37805446Sahrens /* 37815642Smaybee * The fs has been unmounted, or we did a 37825642Smaybee * suspend/resume and this file no longer exists. 37835446Sahrens */ 3784789Sahrens if (vn_has_cached_data(vp)) { 3785789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3786789Sahrens B_INVAL, cr); 3787789Sahrens } 3788789Sahrens 37891544Seschrock mutex_enter(&zp->z_lock); 3790789Sahrens vp->v_count = 0; /* count arrives as 1 */ 37915446Sahrens mutex_exit(&zp->z_lock); 37925642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 37935446Sahrens zfs_znode_free(zp); 3794789Sahrens return; 3795789Sahrens } 3796789Sahrens 3797789Sahrens /* 3798789Sahrens * Attempt to push any data in the page cache. If this fails 3799789Sahrens * we will get kicked out later in zfs_zinactive(). 3800789Sahrens */ 38011298Sperrin if (vn_has_cached_data(vp)) { 38021298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 38031298Sperrin cr); 38041298Sperrin } 3805789Sahrens 38063461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3807789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3808789Sahrens 3809789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3810789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3811789Sahrens if (error) { 3812789Sahrens dmu_tx_abort(tx); 3813789Sahrens } else { 3814789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3815789Sahrens mutex_enter(&zp->z_lock); 3816789Sahrens zp->z_atime_dirty = 0; 3817789Sahrens mutex_exit(&zp->z_lock); 3818789Sahrens dmu_tx_commit(tx); 3819789Sahrens } 3820789Sahrens } 3821789Sahrens 3822789Sahrens zfs_zinactive(zp); 38235326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3824789Sahrens } 3825789Sahrens 3826789Sahrens /* 3827789Sahrens * Bounds-check the seek operation. 3828789Sahrens * 3829789Sahrens * IN: vp - vnode seeking within 3830789Sahrens * ooff - old file offset 3831789Sahrens * noffp - pointer to new file offset 38325331Samw * ct - caller context 3833789Sahrens * 3834789Sahrens * RETURN: 0 if success 3835789Sahrens * EINVAL if new offset invalid 3836789Sahrens */ 3837789Sahrens /* ARGSUSED */ 3838789Sahrens static int 38395331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 38405331Samw caller_context_t *ct) 3841789Sahrens { 3842789Sahrens if (vp->v_type == VDIR) 3843789Sahrens return (0); 3844789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3845789Sahrens } 3846789Sahrens 3847789Sahrens /* 3848789Sahrens * Pre-filter the generic locking function to trap attempts to place 3849789Sahrens * a mandatory lock on a memory mapped file. 3850789Sahrens */ 3851789Sahrens static int 3852789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 38535331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 3854789Sahrens { 3855789Sahrens znode_t *zp = VTOZ(vp); 3856789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3857789Sahrens int error; 3858789Sahrens 38595367Sahrens ZFS_ENTER(zfsvfs); 38605367Sahrens ZFS_VERIFY_ZP(zp); 3861789Sahrens 3862789Sahrens /* 38631544Seschrock * We are following the UFS semantics with respect to mapcnt 38641544Seschrock * here: If we see that the file is mapped already, then we will 38651544Seschrock * return an error, but we don't worry about races between this 38661544Seschrock * function and zfs_map(). 3867789Sahrens */ 38681544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3869789Sahrens ZFS_EXIT(zfsvfs); 3870789Sahrens return (EAGAIN); 3871789Sahrens } 38725331Samw error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct); 3873789Sahrens ZFS_EXIT(zfsvfs); 3874789Sahrens return (error); 3875789Sahrens } 3876789Sahrens 3877789Sahrens /* 3878789Sahrens * If we can't find a page in the cache, we will create a new page 3879789Sahrens * and fill it with file data. For efficiency, we may try to fill 38808636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 38818636SMark.Maybee@Sun.COM * list. 3882789Sahrens */ 3883789Sahrens static int 3884789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3885789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3886789Sahrens { 3887789Sahrens znode_t *zp = VTOZ(vp); 3888789Sahrens page_t *pp, *cur_pp; 3889789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3890789Sahrens u_offset_t io_off, total; 3891789Sahrens size_t io_len; 3892789Sahrens int err; 3893789Sahrens 38942688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 38958636SMark.Maybee@Sun.COM /* 38968636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 38978636SMark.Maybee@Sun.COM */ 3898789Sahrens io_off = off; 3899789Sahrens io_len = PAGESIZE; 3900789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3901789Sahrens } else { 3902789Sahrens /* 39038636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 3904789Sahrens */ 3905789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 39068636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 3907789Sahrens } 3908789Sahrens if (pp == NULL) { 3909789Sahrens /* 39108636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 3911789Sahrens */ 3912789Sahrens *pl = NULL; 3913789Sahrens return (0); 3914789Sahrens } 3915789Sahrens 3916789Sahrens /* 3917789Sahrens * Fill the pages in the kluster. 3918789Sahrens */ 3919789Sahrens cur_pp = pp; 3920789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 39218636SMark.Maybee@Sun.COM caddr_t va; 39228636SMark.Maybee@Sun.COM 39232688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 39247315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 39258636SMark.Maybee@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va); 39267315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 3927789Sahrens if (err) { 3928789Sahrens /* On error, toss the entire kluster */ 3929789Sahrens pvn_read_done(pp, B_ERROR); 39307294Sperrin /* convert checksum errors into IO errors */ 39317294Sperrin if (err == ECKSUM) 39327294Sperrin err = EIO; 3933789Sahrens return (err); 3934789Sahrens } 3935789Sahrens cur_pp = cur_pp->p_next; 3936789Sahrens } 39378636SMark.Maybee@Sun.COM 3938789Sahrens /* 39398636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 39408636SMark.Maybee@Sun.COM * from the desired offset `off'. 3941789Sahrens * NOTE: the page list will always be null terminated. 3942789Sahrens */ 3943789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 39448636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 3945789Sahrens 3946789Sahrens return (0); 3947789Sahrens } 3948789Sahrens 3949789Sahrens /* 3950789Sahrens * Return pointers to the pages for the file region [off, off + len] 3951789Sahrens * in the pl array. If plsz is greater than len, this function may 39528636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 39538636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 39548636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 39558636SMark.Maybee@Sun.COM * part of a klustered read. 3956789Sahrens * 3957789Sahrens * IN: vp - vnode of file to get data from. 3958789Sahrens * off - position in file to get data from. 3959789Sahrens * len - amount of data to retrieve. 3960789Sahrens * plsz - length of provided page list. 3961789Sahrens * seg - segment to obtain pages for. 3962789Sahrens * addr - virtual address of fault. 3963789Sahrens * rw - mode of created pages. 3964789Sahrens * cr - credentials of caller. 39655331Samw * ct - caller context. 3966789Sahrens * 3967789Sahrens * OUT: protp - protection mode of created pages. 3968789Sahrens * pl - list of pages created. 3969789Sahrens * 3970789Sahrens * RETURN: 0 if success 3971789Sahrens * error code if failure 3972789Sahrens * 3973789Sahrens * Timestamps: 3974789Sahrens * vp - atime updated 3975789Sahrens */ 3976789Sahrens /* ARGSUSED */ 3977789Sahrens static int 3978789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3979789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 39805331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 3981789Sahrens { 3982789Sahrens znode_t *zp = VTOZ(vp); 3983789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 39848636SMark.Maybee@Sun.COM page_t **pl0 = pl; 39858636SMark.Maybee@Sun.COM int err = 0; 39868636SMark.Maybee@Sun.COM 39878636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 39888636SMark.Maybee@Sun.COM if (pl == NULL) 39898636SMark.Maybee@Sun.COM return (0); 39908636SMark.Maybee@Sun.COM else if (len > plsz) 39918636SMark.Maybee@Sun.COM len = plsz; 39928681SMark.Maybee@Sun.COM else 39938681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 39948636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 3995789Sahrens 39965367Sahrens ZFS_ENTER(zfsvfs); 39975367Sahrens ZFS_VERIFY_ZP(zp); 3998789Sahrens 3999789Sahrens if (protp) 4000789Sahrens *protp = PROT_ALL; 4001789Sahrens 4002789Sahrens /* 4003789Sahrens * Loop through the requested range [off, off + len] looking 4004789Sahrens * for pages. If we don't find a page, we will need to create 4005789Sahrens * a new page and fill it with data from the file. 4006789Sahrens */ 4007789Sahrens while (len > 0) { 40088636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 40098636SMark.Maybee@Sun.COM *(pl+1) = NULL; 40108636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 40118636SMark.Maybee@Sun.COM goto out; 40128636SMark.Maybee@Sun.COM while (*pl) { 40138636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4014789Sahrens off += PAGESIZE; 4015789Sahrens addr += PAGESIZE; 40168681SMark.Maybee@Sun.COM if (len > 0) { 40178681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 40188636SMark.Maybee@Sun.COM len -= PAGESIZE; 40198681SMark.Maybee@Sun.COM } 40208636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4021789Sahrens plsz -= PAGESIZE; 40228636SMark.Maybee@Sun.COM pl++; 4023789Sahrens } 4024789Sahrens } 4025789Sahrens 4026789Sahrens /* 4027789Sahrens * Fill out the page array with any pages already in the cache. 4028789Sahrens */ 40298636SMark.Maybee@Sun.COM while (plsz > 0 && 40308636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 40318636SMark.Maybee@Sun.COM off += PAGESIZE; 40328636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4033789Sahrens } 4034789Sahrens out: 40352752Sperrin if (err) { 40362752Sperrin /* 40372752Sperrin * Release any pages we have previously locked. 40382752Sperrin */ 40392752Sperrin while (pl > pl0) 40402752Sperrin page_unlock(*--pl); 40418636SMark.Maybee@Sun.COM } else { 40428636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 40432752Sperrin } 40442752Sperrin 4045789Sahrens *pl = NULL; 4046789Sahrens 4047789Sahrens ZFS_EXIT(zfsvfs); 4048789Sahrens return (err); 4049789Sahrens } 4050789Sahrens 40511544Seschrock /* 40521544Seschrock * Request a memory map for a section of a file. This code interacts 40531544Seschrock * with common code and the VM system as follows: 40541544Seschrock * 40551544Seschrock * common code calls mmap(), which ends up in smmap_common() 40561544Seschrock * 40571544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 40581544Seschrock * 40591544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 40601544Seschrock * 40611544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 40621544Seschrock * 40631544Seschrock * zfs_addmap() updates z_mapcnt 40641544Seschrock */ 40655331Samw /*ARGSUSED*/ 4066789Sahrens static int 4067789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 40685331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 40695331Samw caller_context_t *ct) 4070789Sahrens { 4071789Sahrens znode_t *zp = VTOZ(vp); 4072789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4073789Sahrens segvn_crargs_t vn_a; 4074789Sahrens int error; 4075789Sahrens 40765929Smarks ZFS_ENTER(zfsvfs); 40775929Smarks ZFS_VERIFY_ZP(zp); 40785929Smarks 40795331Samw if ((prot & PROT_WRITE) && 40805331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 40815929Smarks ZFS_APPENDONLY))) { 40825929Smarks ZFS_EXIT(zfsvfs); 40835331Samw return (EPERM); 40845929Smarks } 40855929Smarks 40865929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 40875929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 40885929Smarks ZFS_EXIT(zfsvfs); 40895929Smarks return (EACCES); 40905929Smarks } 4091789Sahrens 4092789Sahrens if (vp->v_flag & VNOMAP) { 4093789Sahrens ZFS_EXIT(zfsvfs); 4094789Sahrens return (ENOSYS); 4095789Sahrens } 4096789Sahrens 4097789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4098789Sahrens ZFS_EXIT(zfsvfs); 4099789Sahrens return (ENXIO); 4100789Sahrens } 4101789Sahrens 4102789Sahrens if (vp->v_type != VREG) { 4103789Sahrens ZFS_EXIT(zfsvfs); 4104789Sahrens return (ENODEV); 4105789Sahrens } 4106789Sahrens 4107789Sahrens /* 4108789Sahrens * If file is locked, disallow mapping. 4109789Sahrens */ 41101544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 41111544Seschrock ZFS_EXIT(zfsvfs); 41121544Seschrock return (EAGAIN); 4113789Sahrens } 4114789Sahrens 4115789Sahrens as_rangelock(as); 41166036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 41176036Smec if (error != 0) { 41186036Smec as_rangeunlock(as); 41196036Smec ZFS_EXIT(zfsvfs); 41206036Smec return (error); 4121789Sahrens } 4122789Sahrens 4123789Sahrens vn_a.vp = vp; 4124789Sahrens vn_a.offset = (u_offset_t)off; 4125789Sahrens vn_a.type = flags & MAP_TYPE; 4126789Sahrens vn_a.prot = prot; 4127789Sahrens vn_a.maxprot = maxprot; 4128789Sahrens vn_a.cred = cr; 4129789Sahrens vn_a.amp = NULL; 4130789Sahrens vn_a.flags = flags & ~MAP_TYPE; 41311417Skchow vn_a.szc = 0; 41321417Skchow vn_a.lgrp_mem_policy_flags = 0; 4133789Sahrens 4134789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4135789Sahrens 4136789Sahrens as_rangeunlock(as); 4137789Sahrens ZFS_EXIT(zfsvfs); 4138789Sahrens return (error); 4139789Sahrens } 4140789Sahrens 4141789Sahrens /* ARGSUSED */ 4142789Sahrens static int 4143789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41445331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 41455331Samw caller_context_t *ct) 4146789Sahrens { 41471544Seschrock uint64_t pages = btopr(len); 41481544Seschrock 41491544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4150789Sahrens return (0); 4151789Sahrens } 4152789Sahrens 41531773Seschrock /* 41541773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 41551773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 41561773Seschrock * detecting when the data was actually modified, we have to resort to 41571773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 41581773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 41591773Seschrock * which by far the most common case: 41601773Seschrock * 41611773Seschrock * open() 41621773Seschrock * mmap() 41631773Seschrock * <modify memory> 41641773Seschrock * munmap() 41651773Seschrock * close() 41661773Seschrock * <time lapse> 41671773Seschrock * putpage() via fsflush 41681773Seschrock * 41691773Seschrock * If we wait until fsflush to come along, we can have a modification time that 41701773Seschrock * is some arbitrary point in the future. In order to prevent this in the 41711773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 41721773Seschrock * torn down. 41731773Seschrock */ 4174789Sahrens /* ARGSUSED */ 4175789Sahrens static int 4176789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 41775331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 41785331Samw caller_context_t *ct) 4179789Sahrens { 41801544Seschrock uint64_t pages = btopr(len); 41811544Seschrock 41821544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 41831544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 41841773Seschrock 41851773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 41861773Seschrock vn_has_cached_data(vp)) 41875331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 41881773Seschrock 4189789Sahrens return (0); 4190789Sahrens } 4191789Sahrens 4192789Sahrens /* 4193789Sahrens * Free or allocate space in a file. Currently, this function only 4194789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4195789Sahrens * misnamed, as its functionality includes the ability to allocate as 4196789Sahrens * well as free space. 4197789Sahrens * 4198789Sahrens * IN: vp - vnode of file to free data in. 4199789Sahrens * cmd - action to take (only F_FREESP supported). 4200789Sahrens * bfp - section of file to free/alloc. 4201789Sahrens * flag - current file open mode flags. 4202789Sahrens * offset - current file offset. 4203789Sahrens * cr - credentials of caller [UNUSED]. 42045331Samw * ct - caller context. 4205789Sahrens * 4206789Sahrens * RETURN: 0 if success 4207789Sahrens * error code if failure 4208789Sahrens * 4209789Sahrens * Timestamps: 4210789Sahrens * vp - ctime|mtime updated 4211789Sahrens */ 4212789Sahrens /* ARGSUSED */ 4213789Sahrens static int 4214789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4215789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4216789Sahrens { 4217789Sahrens znode_t *zp = VTOZ(vp); 4218789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4219789Sahrens uint64_t off, len; 4220789Sahrens int error; 4221789Sahrens 42225367Sahrens ZFS_ENTER(zfsvfs); 42235367Sahrens ZFS_VERIFY_ZP(zp); 4224789Sahrens 4225789Sahrens if (cmd != F_FREESP) { 4226789Sahrens ZFS_EXIT(zfsvfs); 4227789Sahrens return (EINVAL); 4228789Sahrens } 4229789Sahrens 4230789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4231789Sahrens ZFS_EXIT(zfsvfs); 4232789Sahrens return (error); 4233789Sahrens } 4234789Sahrens 4235789Sahrens if (bfp->l_len < 0) { 4236789Sahrens ZFS_EXIT(zfsvfs); 4237789Sahrens return (EINVAL); 4238789Sahrens } 4239789Sahrens 4240789Sahrens off = bfp->l_start; 42411669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 42421878Smaybee 42436992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4244789Sahrens 4245789Sahrens ZFS_EXIT(zfsvfs); 4246789Sahrens return (error); 4247789Sahrens } 4248789Sahrens 42495331Samw /*ARGSUSED*/ 4250789Sahrens static int 42515331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4252789Sahrens { 4253789Sahrens znode_t *zp = VTOZ(vp); 4254789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 42555326Sek110237 uint32_t gen; 4256789Sahrens uint64_t object = zp->z_id; 4257789Sahrens zfid_short_t *zfid; 4258789Sahrens int size, i; 4259789Sahrens 42605367Sahrens ZFS_ENTER(zfsvfs); 42615367Sahrens ZFS_VERIFY_ZP(zp); 42625326Sek110237 gen = (uint32_t)zp->z_gen; 4263789Sahrens 4264789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4265789Sahrens if (fidp->fid_len < size) { 4266789Sahrens fidp->fid_len = size; 42671512Sek110237 ZFS_EXIT(zfsvfs); 4268789Sahrens return (ENOSPC); 4269789Sahrens } 4270789Sahrens 4271789Sahrens zfid = (zfid_short_t *)fidp; 4272789Sahrens 4273789Sahrens zfid->zf_len = size; 4274789Sahrens 4275789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4276789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4277789Sahrens 4278789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4279789Sahrens if (gen == 0) 4280789Sahrens gen = 1; 4281789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4282789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4283789Sahrens 4284789Sahrens if (size == LONG_FID_LEN) { 4285789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4286789Sahrens zfid_long_t *zlfid; 4287789Sahrens 4288789Sahrens zlfid = (zfid_long_t *)fidp; 4289789Sahrens 4290789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4291789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4292789Sahrens 4293789Sahrens /* XXX - this should be the generation number for the objset */ 4294789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4295789Sahrens zlfid->zf_setgen[i] = 0; 4296789Sahrens } 4297789Sahrens 4298789Sahrens ZFS_EXIT(zfsvfs); 4299789Sahrens return (0); 4300789Sahrens } 4301789Sahrens 4302789Sahrens static int 43035331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 43045331Samw caller_context_t *ct) 4305789Sahrens { 4306789Sahrens znode_t *zp, *xzp; 4307789Sahrens zfsvfs_t *zfsvfs; 4308789Sahrens zfs_dirlock_t *dl; 4309789Sahrens int error; 4310789Sahrens 4311789Sahrens switch (cmd) { 4312789Sahrens case _PC_LINK_MAX: 4313789Sahrens *valp = ULONG_MAX; 4314789Sahrens return (0); 4315789Sahrens 4316789Sahrens case _PC_FILESIZEBITS: 4317789Sahrens *valp = 64; 4318789Sahrens return (0); 4319789Sahrens 4320789Sahrens case _PC_XATTR_EXISTS: 4321789Sahrens zp = VTOZ(vp); 4322789Sahrens zfsvfs = zp->z_zfsvfs; 43235367Sahrens ZFS_ENTER(zfsvfs); 43245367Sahrens ZFS_VERIFY_ZP(zp); 4325789Sahrens *valp = 0; 4326789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 43275331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4328789Sahrens if (error == 0) { 4329789Sahrens zfs_dirent_unlock(dl); 4330789Sahrens if (!zfs_dirempty(xzp)) 4331789Sahrens *valp = 1; 4332789Sahrens VN_RELE(ZTOV(xzp)); 4333789Sahrens } else if (error == ENOENT) { 4334789Sahrens /* 4335789Sahrens * If there aren't extended attributes, it's the 4336789Sahrens * same as having zero of them. 4337789Sahrens */ 4338789Sahrens error = 0; 4339789Sahrens } 4340789Sahrens ZFS_EXIT(zfsvfs); 4341789Sahrens return (error); 4342789Sahrens 43435331Samw case _PC_SATTR_ENABLED: 43445331Samw case _PC_SATTR_EXISTS: 43457757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 43465331Samw (vp->v_type == VREG || vp->v_type == VDIR); 43475331Samw return (0); 43485331Samw 4349789Sahrens case _PC_ACL_ENABLED: 4350789Sahrens *valp = _ACL_ACE_ENABLED; 4351789Sahrens return (0); 4352789Sahrens 4353789Sahrens case _PC_MIN_HOLE_SIZE: 4354789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4355789Sahrens return (0); 4356789Sahrens 4357789Sahrens default: 43585331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4359789Sahrens } 4360789Sahrens } 4361789Sahrens 4362789Sahrens /*ARGSUSED*/ 4363789Sahrens static int 43645331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 43655331Samw caller_context_t *ct) 4366789Sahrens { 4367789Sahrens znode_t *zp = VTOZ(vp); 4368789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4369789Sahrens int error; 43705331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4371789Sahrens 43725367Sahrens ZFS_ENTER(zfsvfs); 43735367Sahrens ZFS_VERIFY_ZP(zp); 43745331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4375789Sahrens ZFS_EXIT(zfsvfs); 4376789Sahrens 4377789Sahrens return (error); 4378789Sahrens } 4379789Sahrens 4380789Sahrens /*ARGSUSED*/ 4381789Sahrens static int 43825331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 43835331Samw caller_context_t *ct) 4384789Sahrens { 4385789Sahrens znode_t *zp = VTOZ(vp); 4386789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4387789Sahrens int error; 43885331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4389789Sahrens 43905367Sahrens ZFS_ENTER(zfsvfs); 43915367Sahrens ZFS_VERIFY_ZP(zp); 43925331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4393789Sahrens ZFS_EXIT(zfsvfs); 4394789Sahrens return (error); 4395789Sahrens } 4396789Sahrens 4397789Sahrens /* 4398789Sahrens * Predeclare these here so that the compiler assumes that 4399789Sahrens * this is an "old style" function declaration that does 4400789Sahrens * not include arguments => we won't get type mismatch errors 4401789Sahrens * in the initializations that follow. 4402789Sahrens */ 4403789Sahrens static int zfs_inval(); 4404789Sahrens static int zfs_isdir(); 4405789Sahrens 4406789Sahrens static int 4407789Sahrens zfs_inval() 4408789Sahrens { 4409789Sahrens return (EINVAL); 4410789Sahrens } 4411789Sahrens 4412789Sahrens static int 4413789Sahrens zfs_isdir() 4414789Sahrens { 4415789Sahrens return (EISDIR); 4416789Sahrens } 4417789Sahrens /* 4418789Sahrens * Directory vnode operations template 4419789Sahrens */ 4420789Sahrens vnodeops_t *zfs_dvnodeops; 4421789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 44223898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44233898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44243898Srsb VOPNAME_READ, { .error = zfs_isdir }, 44253898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 44263898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44273898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44283898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44293898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44303898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44313898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 44323898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 44333898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 44343898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44353898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 44363898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 44373898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 44383898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 44393898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44403898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44413898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44423898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44433898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44443898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44453898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44464863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44473898Srsb NULL, NULL 4448789Sahrens }; 4449789Sahrens 4450789Sahrens /* 4451789Sahrens * Regular file vnode operations template 4452789Sahrens */ 4453789Sahrens vnodeops_t *zfs_fvnodeops; 4454789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 44553898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 44563898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 44573898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 44583898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 44593898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 44603898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44613898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44623898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44633898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 44643898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44653898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 44663898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44673898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44683898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 44693898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 44703898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 44713898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 44723898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 44733898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 44743898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 44753898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 44763898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44773898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 44783898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 44793898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44803898Srsb NULL, NULL 4481789Sahrens }; 4482789Sahrens 4483789Sahrens /* 4484789Sahrens * Symbolic link vnode operations template 4485789Sahrens */ 4486789Sahrens vnodeops_t *zfs_symvnodeops; 4487789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 44883898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 44893898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 44903898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 44913898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 44923898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 44933898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 44943898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 44953898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 44963898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 44973898Srsb NULL, NULL 4498789Sahrens }; 4499789Sahrens 4500789Sahrens /* 45018845Samw@Sun.COM * special share hidden files vnode operations template 45028845Samw@Sun.COM */ 45038845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 45048845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 45058845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45068845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 45078845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45088845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 45098845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45108845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45118845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45128845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45138845Samw@Sun.COM NULL, NULL 45148845Samw@Sun.COM }; 45158845Samw@Sun.COM 45168845Samw@Sun.COM /* 4517789Sahrens * Extended attribute directory vnode operations template 4518789Sahrens * This template is identical to the directory vnodes 4519789Sahrens * operation template except for restricted operations: 4520789Sahrens * VOP_MKDIR() 4521789Sahrens * VOP_SYMLINK() 4522789Sahrens * Note that there are other restrictions embedded in: 4523789Sahrens * zfs_create() - restrict type to VREG 4524789Sahrens * zfs_link() - no links into/out of attribute space 4525789Sahrens * zfs_rename() - no moves into/out of attribute space 4526789Sahrens */ 4527789Sahrens vnodeops_t *zfs_xdvnodeops; 4528789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 45293898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45303898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45313898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45323898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45333898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45343898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45353898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45363898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 45373898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 45383898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 45393898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45403898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 45413898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 45423898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 45433898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 45443898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45453898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45463898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45473898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45483898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45493898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45503898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45513898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45523898Srsb NULL, NULL 4553789Sahrens }; 4554789Sahrens 4555789Sahrens /* 4556789Sahrens * Error vnode operations template 4557789Sahrens */ 4558789Sahrens vnodeops_t *zfs_evnodeops; 4559789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 45603898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45613898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45623898Srsb NULL, NULL 4563789Sahrens }; 4564