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. 1049321SNeil.Perrin@Sun.COM * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC(). 105789Sahrens * 1061757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 1071757Sperrin * as they can span dmu_tx_assign() calls. 1081757Sperrin * 1098227SNeil.Perrin@Sun.COM * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign(). 110789Sahrens * This is critical because we don't want to block while holding locks. 111789Sahrens * Note, in particular, that if a lock is sometimes acquired before 112789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 113789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 114789Sahrens * 115789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 116789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 117789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 118789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 119789Sahrens * 120789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1212113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 122789Sahrens * 1231757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 124789Sahrens * before dropping locks. This ensures that the ordering of events 125789Sahrens * in the intent log matches the order in which they actually occurred. 1268227SNeil.Perrin@Sun.COM * During ZIL replay the zfs_log_* functions will update the sequence 1278227SNeil.Perrin@Sun.COM * number to indicate the zil transaction has replayed. 128789Sahrens * 1291757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 130789Sahrens * regardless of whether there were any errors. 131789Sahrens * 1322638Sperrin * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 133789Sahrens * to ensure that synchronous semantics are provided when necessary. 134789Sahrens * 135789Sahrens * In general, this is how things should be ordered in each vnode op: 136789Sahrens * 137789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 138789Sahrens * top: 139789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 140789Sahrens * rw_enter(...); // grab any other locks you need 141789Sahrens * tx = dmu_tx_create(...); // get DMU tx 142789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 1438227SNeil.Perrin@Sun.COM * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign 144789Sahrens * if (error) { 145789Sahrens * rw_exit(...); // drop locks 146789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 147789Sahrens * VN_RELE(...); // release held vnodes 1488227SNeil.Perrin@Sun.COM * if (error == ERESTART) { 1492113Sahrens * dmu_tx_wait(tx); 1502113Sahrens * dmu_tx_abort(tx); 151789Sahrens * goto top; 152789Sahrens * } 1532113Sahrens * dmu_tx_abort(tx); // abort DMU tx 154789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 155789Sahrens * return (error); // really out of space 156789Sahrens * } 157789Sahrens * error = do_real_work(); // do whatever this VOP does 158789Sahrens * if (error == 0) 1592638Sperrin * zfs_log_*(...); // on success, make ZIL entry 160789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 161789Sahrens * rw_exit(...); // drop locks 162789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 163789Sahrens * VN_RELE(...); // release held vnodes 1642638Sperrin * zil_commit(zilog, seq, foid); // synchronous when necessary 165789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 166789Sahrens * return (error); // done, report error 167789Sahrens */ 1685367Sahrens 169789Sahrens /* ARGSUSED */ 170789Sahrens static int 1715331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 172789Sahrens { 1733063Sperrin znode_t *zp = VTOZ(*vpp); 1747844SMark.Shellenbaum@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1757844SMark.Shellenbaum@Sun.COM 1767844SMark.Shellenbaum@Sun.COM ZFS_ENTER(zfsvfs); 1777844SMark.Shellenbaum@Sun.COM ZFS_VERIFY_ZP(zp); 1783063Sperrin 1795331Samw if ((flag & FWRITE) && (zp->z_phys->zp_flags & ZFS_APPENDONLY) && 1805331Samw ((flag & FAPPEND) == 0)) { 1817844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1825331Samw return (EPERM); 1835331Samw } 1845331Samw 1855331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 1865331Samw ZTOV(zp)->v_type == VREG && 1875331Samw !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) && 1887844SMark.Shellenbaum@Sun.COM zp->z_phys->zp_size > 0) { 1897844SMark.Shellenbaum@Sun.COM if (fs_vscan(*vpp, cr, 0) != 0) { 1907844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1915331Samw return (EACCES); 1927844SMark.Shellenbaum@Sun.COM } 1937844SMark.Shellenbaum@Sun.COM } 1945331Samw 1953063Sperrin /* Keep a count of the synchronous opens in the znode */ 1963063Sperrin if (flag & (FSYNC | FDSYNC)) 1973063Sperrin atomic_inc_32(&zp->z_sync_cnt); 1985331Samw 1997844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 200789Sahrens return (0); 201789Sahrens } 202789Sahrens 203789Sahrens /* ARGSUSED */ 204789Sahrens static int 2055331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2065331Samw caller_context_t *ct) 207789Sahrens { 2083063Sperrin znode_t *zp = VTOZ(vp); 2097844SMark.Shellenbaum@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2107844SMark.Shellenbaum@Sun.COM 2119909Schris.kirby@sun.com /* 2129909Schris.kirby@sun.com * Clean up any locks held by this process on the vp. 2139909Schris.kirby@sun.com */ 2149909Schris.kirby@sun.com cleanlocks(vp, ddi_get_pid(), 0); 2159909Schris.kirby@sun.com cleanshares(vp, ddi_get_pid()); 2169909Schris.kirby@sun.com 2177844SMark.Shellenbaum@Sun.COM ZFS_ENTER(zfsvfs); 2187844SMark.Shellenbaum@Sun.COM ZFS_VERIFY_ZP(zp); 2193063Sperrin 2203063Sperrin /* Decrement the synchronous opens in the znode */ 2214339Sperrin if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 2223063Sperrin atomic_dec_32(&zp->z_sync_cnt); 2233063Sperrin 2245331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 2255331Samw ZTOV(zp)->v_type == VREG && 2265331Samw !(zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) && 2275331Samw zp->z_phys->zp_size > 0) 2285331Samw VERIFY(fs_vscan(vp, cr, 1) == 0); 2295331Samw 2307844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 231789Sahrens return (0); 232789Sahrens } 233789Sahrens 234789Sahrens /* 235789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 236789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 237789Sahrens */ 238789Sahrens static int 239789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 240789Sahrens { 241789Sahrens znode_t *zp = VTOZ(vp); 242789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 243789Sahrens uint64_t file_sz; 244789Sahrens int error; 245789Sahrens boolean_t hole; 246789Sahrens 247789Sahrens file_sz = zp->z_phys->zp_size; 248789Sahrens if (noff >= file_sz) { 249789Sahrens return (ENXIO); 250789Sahrens } 251789Sahrens 252789Sahrens if (cmd == _FIO_SEEK_HOLE) 253789Sahrens hole = B_TRUE; 254789Sahrens else 255789Sahrens hole = B_FALSE; 256789Sahrens 257789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 258789Sahrens 259789Sahrens /* end of file? */ 260789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 261789Sahrens /* 262789Sahrens * Handle the virtual hole at the end of file. 263789Sahrens */ 264789Sahrens if (hole) { 265789Sahrens *off = file_sz; 266789Sahrens return (0); 267789Sahrens } 268789Sahrens return (ENXIO); 269789Sahrens } 270789Sahrens 271789Sahrens if (noff < *off) 272789Sahrens return (error); 273789Sahrens *off = noff; 274789Sahrens return (error); 275789Sahrens } 276789Sahrens 277789Sahrens /* ARGSUSED */ 278789Sahrens static int 279789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 2805331Samw int *rvalp, caller_context_t *ct) 281789Sahrens { 282789Sahrens offset_t off; 283789Sahrens int error; 284789Sahrens zfsvfs_t *zfsvfs; 2855326Sek110237 znode_t *zp; 286789Sahrens 287789Sahrens switch (com) { 2884339Sperrin case _FIOFFS: 289789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 290789Sahrens 2911544Seschrock /* 2921544Seschrock * The following two ioctls are used by bfu. Faking out, 2931544Seschrock * necessary to avoid bfu errors. 2941544Seschrock */ 2954339Sperrin case _FIOGDIO: 2964339Sperrin case _FIOSDIO: 2971544Seschrock return (0); 2981544Seschrock 2994339Sperrin case _FIO_SEEK_DATA: 3004339Sperrin case _FIO_SEEK_HOLE: 301789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 302789Sahrens return (EFAULT); 303789Sahrens 3045326Sek110237 zp = VTOZ(vp); 3055326Sek110237 zfsvfs = zp->z_zfsvfs; 3065367Sahrens ZFS_ENTER(zfsvfs); 3075367Sahrens ZFS_VERIFY_ZP(zp); 308789Sahrens 309789Sahrens /* offset parameter is in/out */ 310789Sahrens error = zfs_holey(vp, com, &off); 311789Sahrens ZFS_EXIT(zfsvfs); 312789Sahrens if (error) 313789Sahrens return (error); 314789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 315789Sahrens return (EFAULT); 316789Sahrens return (0); 317789Sahrens } 318789Sahrens return (ENOTTY); 319789Sahrens } 320789Sahrens 321789Sahrens /* 3227315SJonathan.Adams@Sun.COM * Utility functions to map and unmap a single physical page. These 3237315SJonathan.Adams@Sun.COM * are used to manage the mappable copies of ZFS file data, and therefore 3247315SJonathan.Adams@Sun.COM * do not update ref/mod bits. 3257315SJonathan.Adams@Sun.COM */ 3267315SJonathan.Adams@Sun.COM caddr_t 3277315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw) 3287315SJonathan.Adams@Sun.COM { 3297315SJonathan.Adams@Sun.COM if (kpm_enable) 3307315SJonathan.Adams@Sun.COM return (hat_kpm_mapin(pp, 0)); 3317315SJonathan.Adams@Sun.COM ASSERT(rw == S_READ || rw == S_WRITE); 3327315SJonathan.Adams@Sun.COM return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0), 3337315SJonathan.Adams@Sun.COM (caddr_t)-1)); 3347315SJonathan.Adams@Sun.COM } 3357315SJonathan.Adams@Sun.COM 3367315SJonathan.Adams@Sun.COM void 3377315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr) 3387315SJonathan.Adams@Sun.COM { 3397315SJonathan.Adams@Sun.COM if (kpm_enable) { 3407315SJonathan.Adams@Sun.COM hat_kpm_mapout(pp, 0, addr); 3417315SJonathan.Adams@Sun.COM } else { 3427315SJonathan.Adams@Sun.COM ppmapout(addr); 3437315SJonathan.Adams@Sun.COM } 3447315SJonathan.Adams@Sun.COM } 3457315SJonathan.Adams@Sun.COM 3467315SJonathan.Adams@Sun.COM /* 347789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 348789Sahrens * between the DMU cache and the memory mapped pages. What this means: 349789Sahrens * 350789Sahrens * On Write: If we find a memory mapped page, we write to *both* 351789Sahrens * the page and the dmu buffer. 352789Sahrens */ 3538636SMark.Maybee@Sun.COM static void 3548636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid) 355789Sahrens { 3568636SMark.Maybee@Sun.COM int64_t off; 3578636SMark.Maybee@Sun.COM 358789Sahrens off = start & PAGEOFFSET; 359789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 360789Sahrens page_t *pp; 3618636SMark.Maybee@Sun.COM uint64_t nbytes = MIN(PAGESIZE - off, len); 3628636SMark.Maybee@Sun.COM 363789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 364789Sahrens caddr_t va; 365789Sahrens 3667315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_WRITE); 3679512SNeil.Perrin@Sun.COM (void) dmu_read(os, oid, start+off, nbytes, va+off, 3689512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 3697315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 370789Sahrens page_unlock(pp); 371789Sahrens } 3728636SMark.Maybee@Sun.COM len -= nbytes; 373789Sahrens off = 0; 374789Sahrens } 375789Sahrens } 376789Sahrens 377789Sahrens /* 378789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 379789Sahrens * between the DMU cache and the memory mapped pages. What this means: 380789Sahrens * 381789Sahrens * On Read: We "read" preferentially from memory mapped pages, 382789Sahrens * else we default from the dmu buffer. 383789Sahrens * 384789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 385789Sahrens * the file is memory mapped. 386789Sahrens */ 387789Sahrens static int 3883638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio) 389789Sahrens { 3903638Sbillm znode_t *zp = VTOZ(vp); 3913638Sbillm objset_t *os = zp->z_zfsvfs->z_os; 3923638Sbillm int64_t start, off; 393789Sahrens int len = nbytes; 394789Sahrens int error = 0; 395789Sahrens 396789Sahrens start = uio->uio_loffset; 397789Sahrens off = start & PAGEOFFSET; 398789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 399789Sahrens page_t *pp; 4003638Sbillm uint64_t bytes = MIN(PAGESIZE - off, len); 4013638Sbillm 402789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 403789Sahrens caddr_t va; 404789Sahrens 4057315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_READ); 406789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 4077315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 408789Sahrens page_unlock(pp); 409789Sahrens } else { 4103638Sbillm error = dmu_read_uio(os, zp->z_id, uio, bytes); 411789Sahrens } 412789Sahrens len -= bytes; 413789Sahrens off = 0; 414789Sahrens if (error) 415789Sahrens break; 416789Sahrens } 417789Sahrens return (error); 418789Sahrens } 419789Sahrens 4203638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 421789Sahrens 422789Sahrens /* 423789Sahrens * Read bytes from specified file into supplied buffer. 424789Sahrens * 425789Sahrens * IN: vp - vnode of file to be read from. 426789Sahrens * uio - structure supplying read location, range info, 427789Sahrens * and return buffer. 428789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 429789Sahrens * cr - credentials of caller. 4305331Samw * ct - caller context 431789Sahrens * 432789Sahrens * OUT: uio - updated offset and range, buffer filled. 433789Sahrens * 434789Sahrens * RETURN: 0 if success 435789Sahrens * error code if failure 436789Sahrens * 437789Sahrens * Side Effects: 438789Sahrens * vp - atime updated if byte count > 0 439789Sahrens */ 440789Sahrens /* ARGSUSED */ 441789Sahrens static int 442789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 443789Sahrens { 444789Sahrens znode_t *zp = VTOZ(vp); 445789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4465326Sek110237 objset_t *os; 4473638Sbillm ssize_t n, nbytes; 4483638Sbillm int error; 4491669Sperrin rl_t *rl; 450789Sahrens 4515367Sahrens ZFS_ENTER(zfsvfs); 4525367Sahrens ZFS_VERIFY_ZP(zp); 4535326Sek110237 os = zfsvfs->z_os; 454789Sahrens 4555929Smarks if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) { 4565929Smarks ZFS_EXIT(zfsvfs); 4575929Smarks return (EACCES); 4585929Smarks } 4595929Smarks 460789Sahrens /* 461789Sahrens * Validate file offset 462789Sahrens */ 463789Sahrens if (uio->uio_loffset < (offset_t)0) { 464789Sahrens ZFS_EXIT(zfsvfs); 465789Sahrens return (EINVAL); 466789Sahrens } 467789Sahrens 468789Sahrens /* 469789Sahrens * Fasttrack empty reads 470789Sahrens */ 471789Sahrens if (uio->uio_resid == 0) { 472789Sahrens ZFS_EXIT(zfsvfs); 473789Sahrens return (0); 474789Sahrens } 475789Sahrens 476789Sahrens /* 4771669Sperrin * Check for mandatory locks 478789Sahrens */ 479789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 480789Sahrens if (error = chklock(vp, FREAD, 481789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 482789Sahrens ZFS_EXIT(zfsvfs); 483789Sahrens return (error); 484789Sahrens } 485789Sahrens } 486789Sahrens 487789Sahrens /* 488789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 489789Sahrens */ 4902638Sperrin if (ioflag & FRSYNC) 4912638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 492789Sahrens 493789Sahrens /* 4941669Sperrin * Lock the range against changes. 495789Sahrens */ 4961669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4971669Sperrin 498789Sahrens /* 499789Sahrens * If we are reading past end-of-file we can skip 500789Sahrens * to the end; but we might still need to set atime. 501789Sahrens */ 502789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 503789Sahrens error = 0; 504789Sahrens goto out; 505789Sahrens } 506789Sahrens 5073638Sbillm ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 5083638Sbillm n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 5093638Sbillm 5103638Sbillm while (n > 0) { 5113638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 5123638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 5133638Sbillm 5143638Sbillm if (vn_has_cached_data(vp)) 5153638Sbillm error = mappedread(vp, nbytes, uio); 5163638Sbillm else 5173638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 5187294Sperrin if (error) { 5197294Sperrin /* convert checksum errors into IO errors */ 5207294Sperrin if (error == ECKSUM) 5217294Sperrin error = EIO; 5223638Sbillm break; 5237294Sperrin } 5243638Sbillm 5253638Sbillm n -= nbytes; 526789Sahrens } 5273638Sbillm 528789Sahrens out: 5292237Smaybee zfs_range_unlock(rl); 530789Sahrens 531789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 532789Sahrens ZFS_EXIT(zfsvfs); 533789Sahrens return (error); 534789Sahrens } 535789Sahrens 536789Sahrens /* 537789Sahrens * Write the bytes to a file. 538789Sahrens * 539789Sahrens * IN: vp - vnode of file to be written to. 540789Sahrens * uio - structure supplying write location, range info, 541789Sahrens * and data buffer. 542789Sahrens * ioflag - FAPPEND flag set if in append mode. 543789Sahrens * cr - credentials of caller. 5445331Samw * ct - caller context (NFS/CIFS fem monitor only) 545789Sahrens * 546789Sahrens * OUT: uio - updated offset and range. 547789Sahrens * 548789Sahrens * RETURN: 0 if success 549789Sahrens * error code if failure 550789Sahrens * 551789Sahrens * Timestamps: 552789Sahrens * vp - ctime|mtime updated if byte count > 0 553789Sahrens */ 554789Sahrens /* ARGSUSED */ 555789Sahrens static int 556789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 557789Sahrens { 558789Sahrens znode_t *zp = VTOZ(vp); 559789Sahrens rlim64_t limit = uio->uio_llimit; 560789Sahrens ssize_t start_resid = uio->uio_resid; 561789Sahrens ssize_t tx_bytes; 562789Sahrens uint64_t end_size; 563789Sahrens dmu_tx_t *tx; 564789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5655326Sek110237 zilog_t *zilog; 566789Sahrens offset_t woff; 567789Sahrens ssize_t n, nbytes; 5681669Sperrin rl_t *rl; 569789Sahrens int max_blksz = zfsvfs->z_max_blksz; 5706743Smarks uint64_t pflags; 5711669Sperrin int error; 5729412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *abuf; 573789Sahrens 574789Sahrens /* 575789Sahrens * Fasttrack empty write 576789Sahrens */ 5771669Sperrin n = start_resid; 578789Sahrens if (n == 0) 579789Sahrens return (0); 580789Sahrens 5811669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 5821669Sperrin limit = MAXOFFSET_T; 5831669Sperrin 5845367Sahrens ZFS_ENTER(zfsvfs); 5855367Sahrens ZFS_VERIFY_ZP(zp); 5866743Smarks 5876743Smarks /* 5886743Smarks * If immutable or not appending then return EPERM 5896743Smarks */ 5906743Smarks pflags = zp->z_phys->zp_flags; 5916743Smarks if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 5926743Smarks ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 5936743Smarks (uio->uio_loffset < zp->z_phys->zp_size))) { 5946743Smarks ZFS_EXIT(zfsvfs); 5956743Smarks return (EPERM); 5966743Smarks } 5976743Smarks 5985326Sek110237 zilog = zfsvfs->z_log; 599789Sahrens 600789Sahrens /* 6012237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6021669Sperrin * don't hold up txg. 603789Sahrens */ 6048059SDonghai.Qiao@Sun.COM uio_prefaultpages(n, uio); 605789Sahrens 606789Sahrens /* 607789Sahrens * If in append mode, set the io offset pointer to eof. 608789Sahrens */ 6091669Sperrin if (ioflag & FAPPEND) { 6101669Sperrin /* 6111669Sperrin * Range lock for a file append: 6121669Sperrin * The value for the start of range will be determined by 6131669Sperrin * zfs_range_lock() (to guarantee append semantics). 6141669Sperrin * If this write will cause the block size to increase, 6151669Sperrin * zfs_range_lock() will lock the entire file, so we must 6161669Sperrin * later reduce the range after we grow the block size. 6171669Sperrin */ 6181669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6191669Sperrin if (rl->r_len == UINT64_MAX) { 6201669Sperrin /* overlocked, zp_size can't change */ 6211669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6221669Sperrin } else { 6231669Sperrin woff = uio->uio_loffset = rl->r_off; 6241669Sperrin } 625789Sahrens } else { 626789Sahrens woff = uio->uio_loffset; 627789Sahrens /* 628789Sahrens * Validate file offset 629789Sahrens */ 630789Sahrens if (woff < 0) { 631789Sahrens ZFS_EXIT(zfsvfs); 632789Sahrens return (EINVAL); 633789Sahrens } 634789Sahrens 635789Sahrens /* 6361669Sperrin * If we need to grow the block size then zfs_range_lock() 6371669Sperrin * will lock a wider range than we request here. 6381669Sperrin * Later after growing the block size we reduce the range. 639789Sahrens */ 6401669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 641789Sahrens } 642789Sahrens 643789Sahrens if (woff >= limit) { 6443638Sbillm zfs_range_unlock(rl); 6453638Sbillm ZFS_EXIT(zfsvfs); 6463638Sbillm return (EFBIG); 647789Sahrens } 648789Sahrens 649789Sahrens if ((woff + n) > limit || woff > (limit - n)) 650789Sahrens n = limit - woff; 651789Sahrens 652789Sahrens /* 6531669Sperrin * Check for mandatory locks 654789Sahrens */ 655789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 6563638Sbillm (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 6573638Sbillm zfs_range_unlock(rl); 6583638Sbillm ZFS_EXIT(zfsvfs); 6593638Sbillm return (error); 6603638Sbillm } 6611669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 662789Sahrens 6631669Sperrin /* 6643638Sbillm * Write the file in reasonable size chunks. Each chunk is written 6653638Sbillm * in a separate transaction; this keeps the intent log records small 6663638Sbillm * and allows us to do more fine-grained space accounting. 667789Sahrens */ 668789Sahrens while (n > 0) { 6699412SAleksandr.Guzovskiy@Sun.COM abuf = NULL; 6709412SAleksandr.Guzovskiy@Sun.COM woff = uio->uio_loffset; 6719412SAleksandr.Guzovskiy@Sun.COM 6729412SAleksandr.Guzovskiy@Sun.COM again: 6739396SMatthew.Ahrens@Sun.COM if (zfs_usergroup_overquota(zfsvfs, 6749396SMatthew.Ahrens@Sun.COM B_FALSE, zp->z_phys->zp_uid) || 6759396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, 6769396SMatthew.Ahrens@Sun.COM B_TRUE, zp->z_phys->zp_gid)) { 6779412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 6789412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 6799396SMatthew.Ahrens@Sun.COM error = EDQUOT; 6809396SMatthew.Ahrens@Sun.COM break; 6819396SMatthew.Ahrens@Sun.COM } 6829412SAleksandr.Guzovskiy@Sun.COM 6839412SAleksandr.Guzovskiy@Sun.COM /* 6849412SAleksandr.Guzovskiy@Sun.COM * If dmu_assign_arcbuf() is expected to execute with minimum 6859412SAleksandr.Guzovskiy@Sun.COM * overhead loan an arc buffer and copy user data to it before 6869412SAleksandr.Guzovskiy@Sun.COM * we enter a txg. This avoids holding a txg forever while we 6879412SAleksandr.Guzovskiy@Sun.COM * pagefault on a hanging NFS server mapping. 6889412SAleksandr.Guzovskiy@Sun.COM */ 6899412SAleksandr.Guzovskiy@Sun.COM if (abuf == NULL && n >= max_blksz && 6909412SAleksandr.Guzovskiy@Sun.COM woff >= zp->z_phys->zp_size && 6919412SAleksandr.Guzovskiy@Sun.COM P2PHASE(woff, max_blksz) == 0 && 6929412SAleksandr.Guzovskiy@Sun.COM zp->z_blksz == max_blksz) { 6939412SAleksandr.Guzovskiy@Sun.COM size_t cbytes; 6949412SAleksandr.Guzovskiy@Sun.COM 6959412SAleksandr.Guzovskiy@Sun.COM abuf = dmu_request_arcbuf(zp->z_dbuf, max_blksz); 6969412SAleksandr.Guzovskiy@Sun.COM ASSERT(abuf != NULL); 6979412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_buf_size(abuf) == max_blksz); 6989412SAleksandr.Guzovskiy@Sun.COM if (error = uiocopy(abuf->b_data, max_blksz, 6999412SAleksandr.Guzovskiy@Sun.COM UIO_WRITE, uio, &cbytes)) { 7009412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7019412SAleksandr.Guzovskiy@Sun.COM break; 7029412SAleksandr.Guzovskiy@Sun.COM } 7039412SAleksandr.Guzovskiy@Sun.COM ASSERT(cbytes == max_blksz); 7049412SAleksandr.Guzovskiy@Sun.COM } 7059412SAleksandr.Guzovskiy@Sun.COM 7069412SAleksandr.Guzovskiy@Sun.COM /* 7079412SAleksandr.Guzovskiy@Sun.COM * Start a transaction. 7089412SAleksandr.Guzovskiy@Sun.COM */ 709789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 710789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 711789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 7128227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 713789Sahrens if (error) { 7148227SNeil.Perrin@Sun.COM if (error == ERESTART) { 7152113Sahrens dmu_tx_wait(tx); 7162113Sahrens dmu_tx_abort(tx); 7179412SAleksandr.Guzovskiy@Sun.COM goto again; 718789Sahrens } 7192113Sahrens dmu_tx_abort(tx); 7209412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7219412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7223638Sbillm break; 7233638Sbillm } 7243638Sbillm 7253638Sbillm /* 7263638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 7273638Sbillm * and then reduce the lock range. This will only happen 7283638Sbillm * on the first iteration since zfs_range_reduce() will 7293638Sbillm * shrink down r_len to the appropriate size. 7303638Sbillm */ 7313638Sbillm if (rl->r_len == UINT64_MAX) { 7323638Sbillm uint64_t new_blksz; 7333638Sbillm 7343638Sbillm if (zp->z_blksz > max_blksz) { 7353638Sbillm ASSERT(!ISP2(zp->z_blksz)); 7363638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 7373638Sbillm } else { 7383638Sbillm new_blksz = MIN(end_size, max_blksz); 7393638Sbillm } 7403638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 7413638Sbillm zfs_range_reduce(rl, woff, n); 7423638Sbillm } 7433638Sbillm 7443638Sbillm /* 7453638Sbillm * XXX - should we really limit each write to z_max_blksz? 7463638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 7473638Sbillm */ 7483638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 7493638Sbillm 7509412SAleksandr.Guzovskiy@Sun.COM if (abuf == NULL) { 7519412SAleksandr.Guzovskiy@Sun.COM tx_bytes = uio->uio_resid; 7529412SAleksandr.Guzovskiy@Sun.COM error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio, 7539412SAleksandr.Guzovskiy@Sun.COM nbytes, tx); 7549412SAleksandr.Guzovskiy@Sun.COM tx_bytes -= uio->uio_resid; 7559412SAleksandr.Guzovskiy@Sun.COM } else { 7569412SAleksandr.Guzovskiy@Sun.COM tx_bytes = nbytes; 7579412SAleksandr.Guzovskiy@Sun.COM ASSERT(tx_bytes == max_blksz); 7589412SAleksandr.Guzovskiy@Sun.COM dmu_assign_arcbuf(zp->z_dbuf, woff, abuf, tx); 7599412SAleksandr.Guzovskiy@Sun.COM ASSERT(tx_bytes <= uio->uio_resid); 7609412SAleksandr.Guzovskiy@Sun.COM uioskip(uio, tx_bytes); 7619412SAleksandr.Guzovskiy@Sun.COM } 7629412SAleksandr.Guzovskiy@Sun.COM if (tx_bytes && vn_has_cached_data(vp)) { 7638636SMark.Maybee@Sun.COM update_pages(vp, woff, 7648636SMark.Maybee@Sun.COM tx_bytes, zfsvfs->z_os, zp->z_id); 7659412SAleksandr.Guzovskiy@Sun.COM } 7663638Sbillm 7673638Sbillm /* 7683638Sbillm * If we made no progress, we're done. If we made even 7693638Sbillm * partial progress, update the znode and ZIL accordingly. 7703638Sbillm */ 7713638Sbillm if (tx_bytes == 0) { 7723897Smaybee dmu_tx_commit(tx); 7733638Sbillm ASSERT(error != 0); 7743638Sbillm break; 7753638Sbillm } 7763638Sbillm 777789Sahrens /* 7783638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 7793638Sbillm * privileged and at least one of the excute bits is set. 7803638Sbillm * 7813638Sbillm * It would be nice to to this after all writes have 7823638Sbillm * been done, but that would still expose the ISUID/ISGID 7833638Sbillm * to another app after the partial write is committed. 7845331Samw * 7855331Samw * Note: we don't call zfs_fuid_map_id() here because 7865331Samw * user 0 is not an ephemeral uid. 787789Sahrens */ 7883638Sbillm mutex_enter(&zp->z_acl_lock); 7893638Sbillm if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7903638Sbillm (S_IXUSR >> 6))) != 0 && 7913638Sbillm (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7923638Sbillm secpolicy_vnode_setid_retain(cr, 7933638Sbillm (zp->z_phys->zp_mode & S_ISUID) != 0 && 7943638Sbillm zp->z_phys->zp_uid == 0) != 0) { 7954339Sperrin zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 7963638Sbillm } 7973638Sbillm mutex_exit(&zp->z_acl_lock); 7983638Sbillm 7993638Sbillm /* 8003638Sbillm * Update time stamp. NOTE: This marks the bonus buffer as 8013638Sbillm * dirty, so we don't have to do it again for zp_size. 8023638Sbillm */ 8033638Sbillm zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 8043638Sbillm 8053638Sbillm /* 8063638Sbillm * Update the file size (zp_size) if it has changed; 8073638Sbillm * account for possible concurrent updates. 8083638Sbillm */ 8093638Sbillm while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 810789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 811789Sahrens uio->uio_loffset); 8123638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 8133638Sbillm dmu_tx_commit(tx); 8143638Sbillm 8153638Sbillm if (error != 0) 8163638Sbillm break; 8173638Sbillm ASSERT(tx_bytes == nbytes); 8183638Sbillm n -= nbytes; 819789Sahrens } 820789Sahrens 8212237Smaybee zfs_range_unlock(rl); 822789Sahrens 823789Sahrens /* 824789Sahrens * If we're in replay mode, or we made no progress, return error. 825789Sahrens * Otherwise, it's at least a partial write, so it's successful. 826789Sahrens */ 8278227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 828789Sahrens ZFS_EXIT(zfsvfs); 829789Sahrens return (error); 830789Sahrens } 831789Sahrens 8322638Sperrin if (ioflag & (FSYNC | FDSYNC)) 8332638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 834789Sahrens 835789Sahrens ZFS_EXIT(zfsvfs); 836789Sahrens return (0); 837789Sahrens } 838789Sahrens 8392237Smaybee void 8403063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd) 8412237Smaybee { 8423063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 8433063Sperrin rl_t *rl = zgd->zgd_rl; 8442237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 8459321SNeil.Perrin@Sun.COM objset_t *os = rl->r_zp->z_zfsvfs->z_os; 8462237Smaybee 8473063Sperrin dmu_buf_rele(db, vzgd); 8482237Smaybee zfs_range_unlock(rl); 8499321SNeil.Perrin@Sun.COM /* 8509321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 8519321SNeil.Perrin@Sun.COM * txg stopped from syncing. 8529321SNeil.Perrin@Sun.COM */ 8539321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(vp, dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 8545688Sbonwick zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 8553063Sperrin kmem_free(zgd, sizeof (zgd_t)); 8562237Smaybee } 8572237Smaybee 858*10209SMark.Musante@Sun.COM #ifdef DEBUG 859*10209SMark.Musante@Sun.COM static int zil_fault_io = 0; 860*10209SMark.Musante@Sun.COM #endif 861*10209SMark.Musante@Sun.COM 862789Sahrens /* 863789Sahrens * Get data to generate a TX_WRITE intent log record. 864789Sahrens */ 865789Sahrens int 8662237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 867789Sahrens { 868789Sahrens zfsvfs_t *zfsvfs = arg; 869789Sahrens objset_t *os = zfsvfs->z_os; 870789Sahrens znode_t *zp; 871789Sahrens uint64_t off = lr->lr_offset; 8722237Smaybee dmu_buf_t *db; 8731669Sperrin rl_t *rl; 8743063Sperrin zgd_t *zgd; 8753638Sbillm int dlen = lr->lr_length; /* length of user data */ 876789Sahrens int error = 0; 877789Sahrens 8783063Sperrin ASSERT(zio); 879789Sahrens ASSERT(dlen != 0); 880789Sahrens 881789Sahrens /* 8821669Sperrin * Nothing to do if the file has been removed 883789Sahrens */ 884789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 885789Sahrens return (ENOENT); 8863461Sahrens if (zp->z_unlinked) { 8879321SNeil.Perrin@Sun.COM /* 8889321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 8899321SNeil.Perrin@Sun.COM * txg stopped from syncing. 8909321SNeil.Perrin@Sun.COM */ 8919321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), 8929321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 893789Sahrens return (ENOENT); 894789Sahrens } 895789Sahrens 896789Sahrens /* 897789Sahrens * Write records come in two flavors: immediate and indirect. 898789Sahrens * For small writes it's cheaper to store the data with the 899789Sahrens * log record (immediate); for large writes it's cheaper to 900789Sahrens * sync the data and get a pointer to it (indirect) so that 901789Sahrens * we don't have to write the data twice. 902789Sahrens */ 9031669Sperrin if (buf != NULL) { /* immediate write */ 9041669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 9051669Sperrin /* test for truncation needs to be done while range locked */ 9061669Sperrin if (off >= zp->z_phys->zp_size) { 9071669Sperrin error = ENOENT; 9081669Sperrin goto out; 9091669Sperrin } 9109512SNeil.Perrin@Sun.COM VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf, 9119512SNeil.Perrin@Sun.COM DMU_READ_NO_PREFETCH)); 9121669Sperrin } else { /* indirect write */ 9131669Sperrin uint64_t boff; /* block starting offset */ 9141669Sperrin 915789Sahrens /* 9161669Sperrin * Have to lock the whole block to ensure when it's 9171669Sperrin * written out and it's checksum is being calculated 9181669Sperrin * that no one can change the data. We need to re-check 9191669Sperrin * blocksize after we get the lock in case it's changed! 920789Sahrens */ 9211669Sperrin for (;;) { 9221941Sperrin if (ISP2(zp->z_blksz)) { 9231941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 9241941Sperrin uint64_t); 9251941Sperrin } else { 9261941Sperrin boff = 0; 9271941Sperrin } 9281669Sperrin dlen = zp->z_blksz; 9291669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 9301669Sperrin if (zp->z_blksz == dlen) 9311669Sperrin break; 9322237Smaybee zfs_range_unlock(rl); 9331669Sperrin } 9341669Sperrin /* test for truncation needs to be done while range locked */ 9351669Sperrin if (off >= zp->z_phys->zp_size) { 9361669Sperrin error = ENOENT; 9371669Sperrin goto out; 9381669Sperrin } 9393063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 9403063Sperrin zgd->zgd_rl = rl; 9413063Sperrin zgd->zgd_zilog = zfsvfs->z_log; 9423063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 943*10209SMark.Musante@Sun.COM #ifdef DEBUG 944*10209SMark.Musante@Sun.COM if (zil_fault_io) { 945*10209SMark.Musante@Sun.COM error = EIO; 946*10209SMark.Musante@Sun.COM zil_fault_io = 0; 947*10209SMark.Musante@Sun.COM } else { 948*10209SMark.Musante@Sun.COM error = dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db); 949*10209SMark.Musante@Sun.COM } 950*10209SMark.Musante@Sun.COM #else 951*10209SMark.Musante@Sun.COM error = dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db); 952*10209SMark.Musante@Sun.COM #endif 953*10209SMark.Musante@Sun.COM if (error != 0) { 954*10209SMark.Musante@Sun.COM kmem_free(zgd, sizeof (zgd_t)); 955*10209SMark.Musante@Sun.COM goto out; 956*10209SMark.Musante@Sun.COM } 957*10209SMark.Musante@Sun.COM 9582237Smaybee ASSERT(boff == db->db_offset); 9592237Smaybee lr->lr_blkoff = off - boff; 9602237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 9613063Sperrin lr->lr_common.lrc_txg, zfs_get_done, zgd); 9624709Smaybee ASSERT((error && error != EINPROGRESS) || 9634709Smaybee lr->lr_length <= zp->z_blksz); 9645688Sbonwick if (error == 0) 9655688Sbonwick zil_add_block(zfsvfs->z_log, &lr->lr_blkptr); 9662237Smaybee /* 9672237Smaybee * If we get EINPROGRESS, then we need to wait for a 9682237Smaybee * write IO initiated by dmu_sync() to complete before 9692638Sperrin * we can release this dbuf. We will finish everything 9702237Smaybee * up in the zfs_get_done() callback. 9712237Smaybee */ 9722237Smaybee if (error == EINPROGRESS) 9732237Smaybee return (0); 9743063Sperrin dmu_buf_rele(db, zgd); 9753063Sperrin kmem_free(zgd, sizeof (zgd_t)); 976789Sahrens } 9771669Sperrin out: 9782237Smaybee zfs_range_unlock(rl); 9799321SNeil.Perrin@Sun.COM /* 9809321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9819321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9829321SNeil.Perrin@Sun.COM */ 9839321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 984789Sahrens return (error); 985789Sahrens } 986789Sahrens 987789Sahrens /*ARGSUSED*/ 988789Sahrens static int 9895331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 9905331Samw caller_context_t *ct) 991789Sahrens { 992789Sahrens znode_t *zp = VTOZ(vp); 993789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 994789Sahrens int error; 995789Sahrens 9965367Sahrens ZFS_ENTER(zfsvfs); 9975367Sahrens ZFS_VERIFY_ZP(zp); 9985331Samw 9995331Samw if (flag & V_ACE_MASK) 10005331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 10015331Samw else 10025331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 10035331Samw 1004789Sahrens ZFS_EXIT(zfsvfs); 1005789Sahrens return (error); 1006789Sahrens } 1007789Sahrens 1008789Sahrens /* 10099981STim.Haley@Sun.COM * If vnode is for a device return a specfs vnode instead. 10109981STim.Haley@Sun.COM */ 10119981STim.Haley@Sun.COM static int 10129981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr) 10139981STim.Haley@Sun.COM { 10149981STim.Haley@Sun.COM int error = 0; 10159981STim.Haley@Sun.COM 10169981STim.Haley@Sun.COM if (IS_DEVVP(*vpp)) { 10179981STim.Haley@Sun.COM struct vnode *svp; 10189981STim.Haley@Sun.COM 10199981STim.Haley@Sun.COM svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 10209981STim.Haley@Sun.COM VN_RELE(*vpp); 10219981STim.Haley@Sun.COM if (svp == NULL) 10229981STim.Haley@Sun.COM error = ENOSYS; 10239981STim.Haley@Sun.COM *vpp = svp; 10249981STim.Haley@Sun.COM } 10259981STim.Haley@Sun.COM return (error); 10269981STim.Haley@Sun.COM } 10279981STim.Haley@Sun.COM 10289981STim.Haley@Sun.COM 10299981STim.Haley@Sun.COM /* 1030789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1031789Sahrens * If it exists, return a held vnode reference for it. 1032789Sahrens * 1033789Sahrens * IN: dvp - vnode of directory to search. 1034789Sahrens * nm - name of entry to lookup. 1035789Sahrens * pnp - full pathname to lookup [UNUSED]. 1036789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1037789Sahrens * rdir - root directory vnode [UNUSED]. 1038789Sahrens * cr - credentials of caller. 10395331Samw * ct - caller context 10405331Samw * direntflags - directory lookup flags 10415331Samw * realpnp - returned pathname. 1042789Sahrens * 1043789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1044789Sahrens * 1045789Sahrens * RETURN: 0 if success 1046789Sahrens * error code if failure 1047789Sahrens * 1048789Sahrens * Timestamps: 1049789Sahrens * NA 1050789Sahrens */ 1051789Sahrens /* ARGSUSED */ 1052789Sahrens static int 1053789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 10545331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 10555331Samw int *direntflags, pathname_t *realpnp) 1056789Sahrens { 1057789Sahrens znode_t *zdp = VTOZ(dvp); 1058789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 10599981STim.Haley@Sun.COM int error = 0; 10609981STim.Haley@Sun.COM 10619981STim.Haley@Sun.COM /* fast path */ 10629981STim.Haley@Sun.COM if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 10639981STim.Haley@Sun.COM 10649981STim.Haley@Sun.COM if (dvp->v_type != VDIR) { 10659981STim.Haley@Sun.COM return (ENOTDIR); 10669981STim.Haley@Sun.COM } else if (zdp->z_dbuf == NULL) { 10679981STim.Haley@Sun.COM return (EIO); 10689981STim.Haley@Sun.COM } 10699981STim.Haley@Sun.COM 10709981STim.Haley@Sun.COM if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 10719981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 10729981STim.Haley@Sun.COM if (!error) { 10739981STim.Haley@Sun.COM *vpp = dvp; 10749981STim.Haley@Sun.COM VN_HOLD(*vpp); 10759981STim.Haley@Sun.COM return (0); 10769981STim.Haley@Sun.COM } 10779981STim.Haley@Sun.COM return (error); 10789981STim.Haley@Sun.COM } else { 10799981STim.Haley@Sun.COM vnode_t *tvp = dnlc_lookup(dvp, nm); 10809981STim.Haley@Sun.COM 10819981STim.Haley@Sun.COM if (tvp) { 10829981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 10839981STim.Haley@Sun.COM if (error) { 10849981STim.Haley@Sun.COM VN_RELE(tvp); 10859981STim.Haley@Sun.COM return (error); 10869981STim.Haley@Sun.COM } 10879981STim.Haley@Sun.COM if (tvp == DNLC_NO_VNODE) { 10889981STim.Haley@Sun.COM VN_RELE(tvp); 10899981STim.Haley@Sun.COM return (ENOENT); 10909981STim.Haley@Sun.COM } else { 10919981STim.Haley@Sun.COM *vpp = tvp; 10929981STim.Haley@Sun.COM return (specvp_check(vpp, cr)); 10939981STim.Haley@Sun.COM } 10949981STim.Haley@Sun.COM } 10959981STim.Haley@Sun.COM } 10969981STim.Haley@Sun.COM } 10979981STim.Haley@Sun.COM 10989981STim.Haley@Sun.COM DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1099789Sahrens 11005367Sahrens ZFS_ENTER(zfsvfs); 11015367Sahrens ZFS_VERIFY_ZP(zdp); 1102789Sahrens 1103789Sahrens *vpp = NULL; 1104789Sahrens 1105789Sahrens if (flags & LOOKUP_XATTR) { 1106789Sahrens /* 11073234Sck153898 * If the xattr property is off, refuse the lookup request. 11083234Sck153898 */ 11093234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 11103234Sck153898 ZFS_EXIT(zfsvfs); 11113234Sck153898 return (EINVAL); 11123234Sck153898 } 11133234Sck153898 11143234Sck153898 /* 1115789Sahrens * We don't allow recursive attributes.. 1116789Sahrens * Maybe someday we will. 1117789Sahrens */ 1118789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1119789Sahrens ZFS_EXIT(zfsvfs); 1120789Sahrens return (EINVAL); 1121789Sahrens } 1122789Sahrens 11233280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1124789Sahrens ZFS_EXIT(zfsvfs); 1125789Sahrens return (error); 1126789Sahrens } 1127789Sahrens 1128789Sahrens /* 1129789Sahrens * Do we have permission to get into attribute directory? 1130789Sahrens */ 1131789Sahrens 11325331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 11335331Samw B_FALSE, cr)) { 1134789Sahrens VN_RELE(*vpp); 11355331Samw *vpp = NULL; 1136789Sahrens } 1137789Sahrens 1138789Sahrens ZFS_EXIT(zfsvfs); 1139789Sahrens return (error); 1140789Sahrens } 1141789Sahrens 11421512Sek110237 if (dvp->v_type != VDIR) { 11431512Sek110237 ZFS_EXIT(zfsvfs); 11441460Smarks return (ENOTDIR); 11451512Sek110237 } 11461460Smarks 1147789Sahrens /* 1148789Sahrens * Check accessibility of directory. 1149789Sahrens */ 1150789Sahrens 11515331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1152789Sahrens ZFS_EXIT(zfsvfs); 1153789Sahrens return (error); 1154789Sahrens } 1155789Sahrens 11565498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 11575331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 11585331Samw ZFS_EXIT(zfsvfs); 11595331Samw return (EILSEQ); 11605331Samw } 11615331Samw 11625331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 11639981STim.Haley@Sun.COM if (error == 0) 11649981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1165789Sahrens 1166789Sahrens ZFS_EXIT(zfsvfs); 1167789Sahrens return (error); 1168789Sahrens } 1169789Sahrens 1170789Sahrens /* 1171789Sahrens * Attempt to create a new entry in a directory. If the entry 1172789Sahrens * already exists, truncate the file if permissible, else return 1173789Sahrens * an error. Return the vp of the created or trunc'd file. 1174789Sahrens * 1175789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1176789Sahrens * name - name of new file entry. 1177789Sahrens * vap - attributes of new file. 1178789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1179789Sahrens * mode - mode to open file with. 1180789Sahrens * cr - credentials of caller. 1181789Sahrens * flag - large file flag [UNUSED]. 11825331Samw * ct - caller context 11835331Samw * vsecp - ACL to be set 1184789Sahrens * 1185789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1186789Sahrens * 1187789Sahrens * RETURN: 0 if success 1188789Sahrens * error code if failure 1189789Sahrens * 1190789Sahrens * Timestamps: 1191789Sahrens * dvp - ctime|mtime updated if new entry created 1192789Sahrens * vp - ctime|mtime always, atime if new 1193789Sahrens */ 11945331Samw 1195789Sahrens /* ARGSUSED */ 1196789Sahrens static int 1197789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 11985331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 11995331Samw vsecattr_t *vsecp) 1200789Sahrens { 1201789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1202789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 12035326Sek110237 zilog_t *zilog; 12045326Sek110237 objset_t *os; 1205789Sahrens zfs_dirlock_t *dl; 1206789Sahrens dmu_tx_t *tx; 1207789Sahrens int error; 12087847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 12097847SMark.Shellenbaum@Sun.COM uid_t uid; 12107847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 12119179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 12129179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 12135331Samw 12145331Samw /* 12155331Samw * If we have an ephemeral id, ACL, or XVATTR then 12165331Samw * make sure file system is at proper version 12175331Samw */ 12185331Samw 12197847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 12207847SMark.Shellenbaum@Sun.COM if (ksid) 12217847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 12227847SMark.Shellenbaum@Sun.COM else 12237847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 12247847SMark.Shellenbaum@Sun.COM 12255331Samw if (zfsvfs->z_use_fuids == B_FALSE && 12265331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 12277847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 12285331Samw return (EINVAL); 1229789Sahrens 12305367Sahrens ZFS_ENTER(zfsvfs); 12315367Sahrens ZFS_VERIFY_ZP(dzp); 12325326Sek110237 os = zfsvfs->z_os; 12335326Sek110237 zilog = zfsvfs->z_log; 1234789Sahrens 12355498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 12365331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 12375331Samw ZFS_EXIT(zfsvfs); 12385331Samw return (EILSEQ); 12395331Samw } 12405331Samw 12415331Samw if (vap->va_mask & AT_XVATTR) { 12425331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 12435331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 12445331Samw ZFS_EXIT(zfsvfs); 12455331Samw return (error); 12465331Samw } 12475331Samw } 1248789Sahrens top: 1249789Sahrens *vpp = NULL; 1250789Sahrens 1251789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1252789Sahrens vap->va_mode &= ~VSVTX; 1253789Sahrens 1254789Sahrens if (*name == '\0') { 1255789Sahrens /* 1256789Sahrens * Null component name refers to the directory itself. 1257789Sahrens */ 1258789Sahrens VN_HOLD(dvp); 1259789Sahrens zp = dzp; 1260789Sahrens dl = NULL; 1261789Sahrens error = 0; 1262789Sahrens } else { 1263789Sahrens /* possible VN_HOLD(zp) */ 12645331Samw int zflg = 0; 12655331Samw 12665331Samw if (flag & FIGNORECASE) 12675331Samw zflg |= ZCILOOK; 12685331Samw 12695331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 12705331Samw NULL, NULL); 12715331Samw if (error) { 1272789Sahrens if (strcmp(name, "..") == 0) 1273789Sahrens error = EISDIR; 1274789Sahrens ZFS_EXIT(zfsvfs); 1275789Sahrens return (error); 1276789Sahrens } 1277789Sahrens } 1278789Sahrens if (zp == NULL) { 12795331Samw uint64_t txtype; 12805331Samw 1281789Sahrens /* 1282789Sahrens * Create a new file object and update the directory 1283789Sahrens * to reference it. 1284789Sahrens */ 12855331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1286789Sahrens goto out; 1287789Sahrens } 1288789Sahrens 1289789Sahrens /* 1290789Sahrens * We only support the creation of regular files in 1291789Sahrens * extended attribute directories. 1292789Sahrens */ 1293789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1294789Sahrens (vap->va_type != VREG)) { 1295789Sahrens error = EINVAL; 1296789Sahrens goto out; 1297789Sahrens } 1298789Sahrens 12999179SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp, 13009179SMark.Shellenbaum@Sun.COM &acl_ids)) != 0) 13019179SMark.Shellenbaum@Sun.COM goto out; 13029396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 130310143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 13049396SMatthew.Ahrens@Sun.COM error = EDQUOT; 13059396SMatthew.Ahrens@Sun.COM goto out; 13069396SMatthew.Ahrens@Sun.COM } 13079179SMark.Shellenbaum@Sun.COM 1308789Sahrens tx = dmu_tx_create(os); 1309789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 13109179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 13119396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 13129396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 1313789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 13141544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 13159179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1316789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1317789Sahrens 0, SPA_MAXBLOCKSIZE); 13185331Samw } 13198227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1320789Sahrens if (error) { 13219179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1322789Sahrens zfs_dirent_unlock(dl); 13238227SNeil.Perrin@Sun.COM if (error == ERESTART) { 13242113Sahrens dmu_tx_wait(tx); 13252113Sahrens dmu_tx_abort(tx); 1326789Sahrens goto top; 1327789Sahrens } 13282113Sahrens dmu_tx_abort(tx); 1329789Sahrens ZFS_EXIT(zfsvfs); 1330789Sahrens return (error); 1331789Sahrens } 13329179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 13339179SMark.Shellenbaum@Sun.COM 13349179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 13359179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 13369179SMark.Shellenbaum@Sun.COM 1337789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 13389179SMark.Shellenbaum@Sun.COM 13395331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 13405331Samw if (flag & FIGNORECASE) 13415331Samw txtype |= TX_CI; 13425331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 13439179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 13449179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1345789Sahrens dmu_tx_commit(tx); 1346789Sahrens } else { 13475331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 13485331Samw 1349789Sahrens /* 1350789Sahrens * A directory entry already exists for this name. 1351789Sahrens */ 1352789Sahrens /* 1353789Sahrens * Can't truncate an existing file if in exclusive mode. 1354789Sahrens */ 1355789Sahrens if (excl == EXCL) { 1356789Sahrens error = EEXIST; 1357789Sahrens goto out; 1358789Sahrens } 1359789Sahrens /* 1360789Sahrens * Can't open a directory for writing. 1361789Sahrens */ 1362789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1363789Sahrens error = EISDIR; 1364789Sahrens goto out; 1365789Sahrens } 1366789Sahrens /* 1367789Sahrens * Verify requested access to file. 1368789Sahrens */ 13695331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1370789Sahrens goto out; 1371789Sahrens } 1372789Sahrens 1373789Sahrens mutex_enter(&dzp->z_lock); 1374789Sahrens dzp->z_seq++; 1375789Sahrens mutex_exit(&dzp->z_lock); 1376789Sahrens 13771878Smaybee /* 13781878Smaybee * Truncate regular files if requested. 13791878Smaybee */ 13801878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1381789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 13826992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 13836992Smaybee zfs_dirent_unlock(dl); 13846992Smaybee dl = NULL; 13851878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 13864863Spraks if (error == 0) { 13875331Samw vnevent_create(ZTOV(zp), ct); 13884863Spraks } 1389789Sahrens } 1390789Sahrens } 1391789Sahrens out: 1392789Sahrens 1393789Sahrens if (dl) 1394789Sahrens zfs_dirent_unlock(dl); 1395789Sahrens 1396789Sahrens if (error) { 1397789Sahrens if (zp) 1398789Sahrens VN_RELE(ZTOV(zp)); 1399789Sahrens } else { 1400789Sahrens *vpp = ZTOV(zp); 14019981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1402789Sahrens } 1403789Sahrens 1404789Sahrens ZFS_EXIT(zfsvfs); 1405789Sahrens return (error); 1406789Sahrens } 1407789Sahrens 1408789Sahrens /* 1409789Sahrens * Remove an entry from a directory. 1410789Sahrens * 1411789Sahrens * IN: dvp - vnode of directory to remove entry from. 1412789Sahrens * name - name of entry to remove. 1413789Sahrens * cr - credentials of caller. 14145331Samw * ct - caller context 14155331Samw * flags - case flags 1416789Sahrens * 1417789Sahrens * RETURN: 0 if success 1418789Sahrens * error code if failure 1419789Sahrens * 1420789Sahrens * Timestamps: 1421789Sahrens * dvp - ctime|mtime 1422789Sahrens * vp - ctime (if nlink > 0) 1423789Sahrens */ 14245331Samw /*ARGSUSED*/ 1425789Sahrens static int 14265331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 14275331Samw int flags) 1428789Sahrens { 1429789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1430789Sahrens znode_t *xzp = NULL; 1431789Sahrens vnode_t *vp; 1432789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 14335326Sek110237 zilog_t *zilog; 1434789Sahrens uint64_t acl_obj, xattr_obj; 1435789Sahrens zfs_dirlock_t *dl; 1436789Sahrens dmu_tx_t *tx; 14373461Sahrens boolean_t may_delete_now, delete_now = FALSE; 14386992Smaybee boolean_t unlinked, toobig = FALSE; 14395331Samw uint64_t txtype; 14405331Samw pathname_t *realnmp = NULL; 14415331Samw pathname_t realnm; 1442789Sahrens int error; 14435331Samw int zflg = ZEXISTS; 1444789Sahrens 14455367Sahrens ZFS_ENTER(zfsvfs); 14465367Sahrens ZFS_VERIFY_ZP(dzp); 14475326Sek110237 zilog = zfsvfs->z_log; 1448789Sahrens 14495331Samw if (flags & FIGNORECASE) { 14505331Samw zflg |= ZCILOOK; 14515331Samw pn_alloc(&realnm); 14525331Samw realnmp = &realnm; 14535331Samw } 14545331Samw 1455789Sahrens top: 1456789Sahrens /* 1457789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1458789Sahrens */ 14595331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 14605331Samw NULL, realnmp)) { 14615331Samw if (realnmp) 14625331Samw pn_free(realnmp); 1463789Sahrens ZFS_EXIT(zfsvfs); 1464789Sahrens return (error); 1465789Sahrens } 1466789Sahrens 1467789Sahrens vp = ZTOV(zp); 1468789Sahrens 1469789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1470789Sahrens goto out; 1471789Sahrens } 1472789Sahrens 1473789Sahrens /* 1474789Sahrens * Need to use rmdir for removing directories. 1475789Sahrens */ 1476789Sahrens if (vp->v_type == VDIR) { 1477789Sahrens error = EPERM; 1478789Sahrens goto out; 1479789Sahrens } 1480789Sahrens 14815331Samw vnevent_remove(vp, dvp, name, ct); 14825331Samw 14835331Samw if (realnmp) 14846492Stimh dnlc_remove(dvp, realnmp->pn_buf); 14855331Samw else 14865331Samw dnlc_remove(dvp, name); 14871484Sek110237 1488789Sahrens mutex_enter(&vp->v_lock); 1489789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1490789Sahrens mutex_exit(&vp->v_lock); 1491789Sahrens 1492789Sahrens /* 14933461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1494789Sahrens * it depends on whether we're the last link, and on whether there are 1495789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1496789Sahrens * allow for either case. 1497789Sahrens */ 1498789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14991544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1500789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 15016992Smaybee if (may_delete_now) { 15026992Smaybee toobig = 15036992Smaybee zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 15046992Smaybee /* if the file is too big, only hold_free a token amount */ 15056992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 15066992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 15076992Smaybee } 1508789Sahrens 1509789Sahrens /* are there any extended attributes? */ 1510789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1511789Sahrens /* XXX - do we need this if we are deleting? */ 1512789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1513789Sahrens } 1514789Sahrens 1515789Sahrens /* are there any additional acls */ 1516789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1517789Sahrens may_delete_now) 1518789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1519789Sahrens 1520789Sahrens /* charge as an update -- would be nice not to charge at all */ 15213461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1522789Sahrens 15238227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1524789Sahrens if (error) { 1525789Sahrens zfs_dirent_unlock(dl); 1526789Sahrens VN_RELE(vp); 15278227SNeil.Perrin@Sun.COM if (error == ERESTART) { 15282113Sahrens dmu_tx_wait(tx); 15292113Sahrens dmu_tx_abort(tx); 1530789Sahrens goto top; 1531789Sahrens } 15325331Samw if (realnmp) 15335331Samw pn_free(realnmp); 15342113Sahrens dmu_tx_abort(tx); 1535789Sahrens ZFS_EXIT(zfsvfs); 1536789Sahrens return (error); 1537789Sahrens } 1538789Sahrens 1539789Sahrens /* 1540789Sahrens * Remove the directory entry. 1541789Sahrens */ 15425331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1543789Sahrens 1544789Sahrens if (error) { 1545789Sahrens dmu_tx_commit(tx); 1546789Sahrens goto out; 1547789Sahrens } 1548789Sahrens 15493461Sahrens if (unlinked) { 1550789Sahrens mutex_enter(&vp->v_lock); 15516992Smaybee delete_now = may_delete_now && !toobig && 1552789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1553789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1554789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1555789Sahrens mutex_exit(&vp->v_lock); 1556789Sahrens } 1557789Sahrens 1558789Sahrens if (delete_now) { 1559789Sahrens if (zp->z_phys->zp_xattr) { 1560789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1561789Sahrens ASSERT3U(error, ==, 0); 1562789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1563789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1564789Sahrens mutex_enter(&xzp->z_lock); 15653461Sahrens xzp->z_unlinked = 1; 1566789Sahrens xzp->z_phys->zp_links = 0; 1567789Sahrens mutex_exit(&xzp->z_lock); 15683461Sahrens zfs_unlinked_add(xzp, tx); 1569789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1570789Sahrens } 1571789Sahrens mutex_enter(&zp->z_lock); 1572789Sahrens mutex_enter(&vp->v_lock); 1573789Sahrens vp->v_count--; 1574789Sahrens ASSERT3U(vp->v_count, ==, 0); 1575789Sahrens mutex_exit(&vp->v_lock); 1576789Sahrens mutex_exit(&zp->z_lock); 1577789Sahrens zfs_znode_delete(zp, tx); 15783461Sahrens } else if (unlinked) { 15793461Sahrens zfs_unlinked_add(zp, tx); 1580789Sahrens } 1581789Sahrens 15825331Samw txtype = TX_REMOVE; 15835331Samw if (flags & FIGNORECASE) 15845331Samw txtype |= TX_CI; 15855331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 1586789Sahrens 1587789Sahrens dmu_tx_commit(tx); 1588789Sahrens out: 15895331Samw if (realnmp) 15905331Samw pn_free(realnmp); 15915331Samw 1592789Sahrens zfs_dirent_unlock(dl); 1593789Sahrens 1594789Sahrens if (!delete_now) { 1595789Sahrens VN_RELE(vp); 1596789Sahrens } else if (xzp) { 15976992Smaybee /* this rele is delayed to prevent nesting transactions */ 1598789Sahrens VN_RELE(ZTOV(xzp)); 1599789Sahrens } 1600789Sahrens 1601789Sahrens ZFS_EXIT(zfsvfs); 1602789Sahrens return (error); 1603789Sahrens } 1604789Sahrens 1605789Sahrens /* 1606789Sahrens * Create a new directory and insert it into dvp using the name 1607789Sahrens * provided. Return a pointer to the inserted directory. 1608789Sahrens * 1609789Sahrens * IN: dvp - vnode of directory to add subdir to. 1610789Sahrens * dirname - name of new directory. 1611789Sahrens * vap - attributes of new directory. 1612789Sahrens * cr - credentials of caller. 16135331Samw * ct - caller context 16145331Samw * vsecp - ACL to be set 1615789Sahrens * 1616789Sahrens * OUT: vpp - vnode of created directory. 1617789Sahrens * 1618789Sahrens * RETURN: 0 if success 1619789Sahrens * error code if failure 1620789Sahrens * 1621789Sahrens * Timestamps: 1622789Sahrens * dvp - ctime|mtime updated 1623789Sahrens * vp - ctime|mtime|atime updated 1624789Sahrens */ 16255331Samw /*ARGSUSED*/ 1626789Sahrens static int 16275331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 16285331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1629789Sahrens { 1630789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1631789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 16325326Sek110237 zilog_t *zilog; 1633789Sahrens zfs_dirlock_t *dl; 16345331Samw uint64_t txtype; 1635789Sahrens dmu_tx_t *tx; 1636789Sahrens int error; 16375331Samw int zf = ZNEW; 16387847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 16397847SMark.Shellenbaum@Sun.COM uid_t uid; 16407847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 16419179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 16429179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1643789Sahrens 1644789Sahrens ASSERT(vap->va_type == VDIR); 1645789Sahrens 16465331Samw /* 16475331Samw * If we have an ephemeral id, ACL, or XVATTR then 16485331Samw * make sure file system is at proper version 16495331Samw */ 16505331Samw 16517847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 16527847SMark.Shellenbaum@Sun.COM if (ksid) 16537847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 16547847SMark.Shellenbaum@Sun.COM else 16557847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 16565331Samw if (zfsvfs->z_use_fuids == B_FALSE && 16577847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 16587876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 16595331Samw return (EINVAL); 16605331Samw 16615367Sahrens ZFS_ENTER(zfsvfs); 16625367Sahrens ZFS_VERIFY_ZP(dzp); 16635326Sek110237 zilog = zfsvfs->z_log; 1664789Sahrens 1665789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1666789Sahrens ZFS_EXIT(zfsvfs); 1667789Sahrens return (EINVAL); 1668789Sahrens } 16695331Samw 16705498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 16715331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 16725331Samw ZFS_EXIT(zfsvfs); 16735331Samw return (EILSEQ); 16745331Samw } 16755331Samw if (flags & FIGNORECASE) 16765331Samw zf |= ZCILOOK; 16775331Samw 16785331Samw if (vap->va_mask & AT_XVATTR) 16795331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 16805331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 16815331Samw ZFS_EXIT(zfsvfs); 16825331Samw return (error); 16835331Samw } 1684789Sahrens 1685789Sahrens /* 1686789Sahrens * First make sure the new directory doesn't exist. 1687789Sahrens */ 16885331Samw top: 16895331Samw *vpp = NULL; 16905331Samw 16915331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 16925331Samw NULL, NULL)) { 1693789Sahrens ZFS_EXIT(zfsvfs); 1694789Sahrens return (error); 1695789Sahrens } 1696789Sahrens 16975331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 16981231Smarks zfs_dirent_unlock(dl); 16991231Smarks ZFS_EXIT(zfsvfs); 17001231Smarks return (error); 17011231Smarks } 17021231Smarks 17039179SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp, 17049179SMark.Shellenbaum@Sun.COM &acl_ids)) != 0) { 17059179SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 17069179SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 17079179SMark.Shellenbaum@Sun.COM return (error); 17085331Samw } 17099396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 171010143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 17119396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 17129396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 17139396SMatthew.Ahrens@Sun.COM return (EDQUOT); 17149396SMatthew.Ahrens@Sun.COM } 17159179SMark.Shellenbaum@Sun.COM 1716789Sahrens /* 1717789Sahrens * Add a new entry to the directory. 1718789Sahrens */ 1719789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 17201544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 17211544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 17229179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 17239396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 17249396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 17259179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 1726789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1727789Sahrens 0, SPA_MAXBLOCKSIZE); 17288227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1729789Sahrens if (error) { 17309179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1731789Sahrens zfs_dirent_unlock(dl); 17328227SNeil.Perrin@Sun.COM if (error == ERESTART) { 17332113Sahrens dmu_tx_wait(tx); 17342113Sahrens dmu_tx_abort(tx); 1735789Sahrens goto top; 1736789Sahrens } 17372113Sahrens dmu_tx_abort(tx); 1738789Sahrens ZFS_EXIT(zfsvfs); 1739789Sahrens return (error); 1740789Sahrens } 1741789Sahrens 1742789Sahrens /* 1743789Sahrens * Create new node. 1744789Sahrens */ 17459179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 17469179SMark.Shellenbaum@Sun.COM 17479179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 17489179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 1749789Sahrens /* 1750789Sahrens * Now put new name in parent dir. 1751789Sahrens */ 1752789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1753789Sahrens 1754789Sahrens *vpp = ZTOV(zp); 1755789Sahrens 17565331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 17575331Samw if (flags & FIGNORECASE) 17585331Samw txtype |= TX_CI; 17599179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 17609179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 17619179SMark.Shellenbaum@Sun.COM 17629179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1763789Sahrens dmu_tx_commit(tx); 1764789Sahrens 1765789Sahrens zfs_dirent_unlock(dl); 1766789Sahrens 1767789Sahrens ZFS_EXIT(zfsvfs); 1768789Sahrens return (0); 1769789Sahrens } 1770789Sahrens 1771789Sahrens /* 1772789Sahrens * Remove a directory subdir entry. If the current working 1773789Sahrens * directory is the same as the subdir to be removed, the 1774789Sahrens * remove will fail. 1775789Sahrens * 1776789Sahrens * IN: dvp - vnode of directory to remove from. 1777789Sahrens * name - name of directory to be removed. 1778789Sahrens * cwd - vnode of current working directory. 1779789Sahrens * cr - credentials of caller. 17805331Samw * ct - caller context 17815331Samw * flags - case flags 1782789Sahrens * 1783789Sahrens * RETURN: 0 if success 1784789Sahrens * error code if failure 1785789Sahrens * 1786789Sahrens * Timestamps: 1787789Sahrens * dvp - ctime|mtime updated 1788789Sahrens */ 17895331Samw /*ARGSUSED*/ 1790789Sahrens static int 17915331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 17925331Samw caller_context_t *ct, int flags) 1793789Sahrens { 1794789Sahrens znode_t *dzp = VTOZ(dvp); 1795789Sahrens znode_t *zp; 1796789Sahrens vnode_t *vp; 1797789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17985326Sek110237 zilog_t *zilog; 1799789Sahrens zfs_dirlock_t *dl; 1800789Sahrens dmu_tx_t *tx; 1801789Sahrens int error; 18025331Samw int zflg = ZEXISTS; 1803789Sahrens 18045367Sahrens ZFS_ENTER(zfsvfs); 18055367Sahrens ZFS_VERIFY_ZP(dzp); 18065326Sek110237 zilog = zfsvfs->z_log; 1807789Sahrens 18085331Samw if (flags & FIGNORECASE) 18095331Samw zflg |= ZCILOOK; 1810789Sahrens top: 1811789Sahrens zp = NULL; 1812789Sahrens 1813789Sahrens /* 1814789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1815789Sahrens */ 18165331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 18175331Samw NULL, NULL)) { 1818789Sahrens ZFS_EXIT(zfsvfs); 1819789Sahrens return (error); 1820789Sahrens } 1821789Sahrens 1822789Sahrens vp = ZTOV(zp); 1823789Sahrens 1824789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1825789Sahrens goto out; 1826789Sahrens } 1827789Sahrens 1828789Sahrens if (vp->v_type != VDIR) { 1829789Sahrens error = ENOTDIR; 1830789Sahrens goto out; 1831789Sahrens } 1832789Sahrens 1833789Sahrens if (vp == cwd) { 1834789Sahrens error = EINVAL; 1835789Sahrens goto out; 1836789Sahrens } 1837789Sahrens 18385331Samw vnevent_rmdir(vp, dvp, name, ct); 1839789Sahrens 1840789Sahrens /* 18413897Smaybee * Grab a lock on the directory to make sure that noone is 18423897Smaybee * trying to add (or lookup) entries while we are removing it. 18433897Smaybee */ 18443897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 18453897Smaybee 18463897Smaybee /* 18473897Smaybee * Grab a lock on the parent pointer to make sure we play well 1848789Sahrens * with the treewalk and directory rename code. 1849789Sahrens */ 1850789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1851789Sahrens 1852789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 18531544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1854789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 18553461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 18568227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1857789Sahrens if (error) { 1858789Sahrens rw_exit(&zp->z_parent_lock); 18593897Smaybee rw_exit(&zp->z_name_lock); 1860789Sahrens zfs_dirent_unlock(dl); 1861789Sahrens VN_RELE(vp); 18628227SNeil.Perrin@Sun.COM if (error == ERESTART) { 18632113Sahrens dmu_tx_wait(tx); 18642113Sahrens dmu_tx_abort(tx); 1865789Sahrens goto top; 1866789Sahrens } 18672113Sahrens dmu_tx_abort(tx); 1868789Sahrens ZFS_EXIT(zfsvfs); 1869789Sahrens return (error); 1870789Sahrens } 1871789Sahrens 18725331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 18735331Samw 18745331Samw if (error == 0) { 18755331Samw uint64_t txtype = TX_RMDIR; 18765331Samw if (flags & FIGNORECASE) 18775331Samw txtype |= TX_CI; 18785331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 18795331Samw } 1880789Sahrens 1881789Sahrens dmu_tx_commit(tx); 1882789Sahrens 1883789Sahrens rw_exit(&zp->z_parent_lock); 18843897Smaybee rw_exit(&zp->z_name_lock); 1885789Sahrens out: 1886789Sahrens zfs_dirent_unlock(dl); 1887789Sahrens 1888789Sahrens VN_RELE(vp); 1889789Sahrens 1890789Sahrens ZFS_EXIT(zfsvfs); 1891789Sahrens return (error); 1892789Sahrens } 1893789Sahrens 1894789Sahrens /* 1895789Sahrens * Read as many directory entries as will fit into the provided 1896789Sahrens * buffer from the given directory cursor position (specified in 1897789Sahrens * the uio structure. 1898789Sahrens * 1899789Sahrens * IN: vp - vnode of directory to read. 1900789Sahrens * uio - structure supplying read location, range info, 1901789Sahrens * and return buffer. 1902789Sahrens * cr - credentials of caller. 19035331Samw * ct - caller context 19045331Samw * flags - case flags 1905789Sahrens * 1906789Sahrens * OUT: uio - updated offset and range, buffer filled. 1907789Sahrens * eofp - set to true if end-of-file detected. 1908789Sahrens * 1909789Sahrens * RETURN: 0 if success 1910789Sahrens * error code if failure 1911789Sahrens * 1912789Sahrens * Timestamps: 1913789Sahrens * vp - atime updated 1914789Sahrens * 1915789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1916789Sahrens * This allows us to use the low range for "special" directory entries: 1917789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1918789Sahrens * we use the offset 2 for the '.zfs' directory. 1919789Sahrens */ 1920789Sahrens /* ARGSUSED */ 1921789Sahrens static int 19225331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 19235331Samw caller_context_t *ct, int flags) 1924789Sahrens { 1925789Sahrens znode_t *zp = VTOZ(vp); 1926789Sahrens iovec_t *iovp; 19275331Samw edirent_t *eodp; 1928789Sahrens dirent64_t *odp; 1929789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1930869Sperrin objset_t *os; 1931789Sahrens caddr_t outbuf; 1932789Sahrens size_t bufsize; 1933789Sahrens zap_cursor_t zc; 1934789Sahrens zap_attribute_t zap; 1935789Sahrens uint_t bytes_wanted; 1936789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1937789Sahrens int local_eof; 1938869Sperrin int outcount; 1939869Sperrin int error; 1940869Sperrin uint8_t prefetch; 19415663Sck153898 boolean_t check_sysattrs; 1942789Sahrens 19435367Sahrens ZFS_ENTER(zfsvfs); 19445367Sahrens ZFS_VERIFY_ZP(zp); 1945789Sahrens 1946789Sahrens /* 1947789Sahrens * If we are not given an eof variable, 1948789Sahrens * use a local one. 1949789Sahrens */ 1950789Sahrens if (eofp == NULL) 1951789Sahrens eofp = &local_eof; 1952789Sahrens 1953789Sahrens /* 1954789Sahrens * Check for valid iov_len. 1955789Sahrens */ 1956789Sahrens if (uio->uio_iov->iov_len <= 0) { 1957789Sahrens ZFS_EXIT(zfsvfs); 1958789Sahrens return (EINVAL); 1959789Sahrens } 1960789Sahrens 1961789Sahrens /* 1962789Sahrens * Quit if directory has been removed (posix) 1963789Sahrens */ 19643461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 1965789Sahrens ZFS_EXIT(zfsvfs); 1966789Sahrens return (0); 1967789Sahrens } 1968789Sahrens 1969869Sperrin error = 0; 1970869Sperrin os = zfsvfs->z_os; 1971869Sperrin offset = uio->uio_loffset; 1972869Sperrin prefetch = zp->z_zn_prefetch; 1973869Sperrin 1974789Sahrens /* 1975789Sahrens * Initialize the iterator cursor. 1976789Sahrens */ 1977789Sahrens if (offset <= 3) { 1978789Sahrens /* 1979789Sahrens * Start iteration from the beginning of the directory. 1980789Sahrens */ 1981869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1982789Sahrens } else { 1983789Sahrens /* 1984789Sahrens * The offset is a serialized cursor. 1985789Sahrens */ 1986869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1987789Sahrens } 1988789Sahrens 1989789Sahrens /* 1990789Sahrens * Get space to change directory entries into fs independent format. 1991789Sahrens */ 1992789Sahrens iovp = uio->uio_iov; 1993789Sahrens bytes_wanted = iovp->iov_len; 1994789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1995789Sahrens bufsize = bytes_wanted; 1996789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1997789Sahrens odp = (struct dirent64 *)outbuf; 1998789Sahrens } else { 1999789Sahrens bufsize = bytes_wanted; 2000789Sahrens odp = (struct dirent64 *)iovp->iov_base; 2001789Sahrens } 20025331Samw eodp = (struct edirent *)odp; 2003789Sahrens 2004789Sahrens /* 20057757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 20067757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 20077757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 20087757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 20095663Sck153898 */ 20107757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 20115663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 20125663Sck153898 (flags & V_RDDIR_ENTFLAGS); 20135663Sck153898 20145663Sck153898 /* 2015789Sahrens * Transform to file-system independent format 2016789Sahrens */ 2017789Sahrens outcount = 0; 2018789Sahrens while (outcount < bytes_wanted) { 20193912Slling ino64_t objnum; 20203912Slling ushort_t reclen; 20213912Slling off64_t *next; 20223912Slling 2023789Sahrens /* 2024789Sahrens * Special case `.', `..', and `.zfs'. 2025789Sahrens */ 2026789Sahrens if (offset == 0) { 2027789Sahrens (void) strcpy(zap.za_name, "."); 20285331Samw zap.za_normalization_conflict = 0; 20293912Slling objnum = zp->z_id; 2030789Sahrens } else if (offset == 1) { 2031789Sahrens (void) strcpy(zap.za_name, ".."); 20325331Samw zap.za_normalization_conflict = 0; 20333912Slling objnum = zp->z_phys->zp_parent; 2034789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2035789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 20365331Samw zap.za_normalization_conflict = 0; 20373912Slling objnum = ZFSCTL_INO_ROOT; 2038789Sahrens } else { 2039789Sahrens /* 2040789Sahrens * Grab next entry. 2041789Sahrens */ 2042789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2043789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2044789Sahrens break; 2045789Sahrens else 2046789Sahrens goto update; 2047789Sahrens } 2048789Sahrens 2049789Sahrens if (zap.za_integer_length != 8 || 2050789Sahrens zap.za_num_integers != 1) { 2051789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2052789Sahrens "entry, obj = %lld, offset = %lld\n", 2053789Sahrens (u_longlong_t)zp->z_id, 2054789Sahrens (u_longlong_t)offset); 2055789Sahrens error = ENXIO; 2056789Sahrens goto update; 2057789Sahrens } 20583912Slling 20593912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 20603912Slling /* 20613912Slling * MacOS X can extract the object type here such as: 20623912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 20633912Slling */ 20645663Sck153898 20655663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 20665663Sck153898 zap.za_normalization_conflict = 20675663Sck153898 xattr_sysattr_casechk(zap.za_name); 20685663Sck153898 } 2069789Sahrens } 20705331Samw 20719749STim.Haley@Sun.COM if (flags & V_RDDIR_ACCFILTER) { 20729749STim.Haley@Sun.COM /* 20739749STim.Haley@Sun.COM * If we have no access at all, don't include 20749749STim.Haley@Sun.COM * this entry in the returned information 20759749STim.Haley@Sun.COM */ 20769749STim.Haley@Sun.COM znode_t *ezp; 20779749STim.Haley@Sun.COM if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 20789749STim.Haley@Sun.COM goto skip_entry; 20799749STim.Haley@Sun.COM if (!zfs_has_access(ezp, cr)) { 20809749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 20819749STim.Haley@Sun.COM goto skip_entry; 20829749STim.Haley@Sun.COM } 20839749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 20849749STim.Haley@Sun.COM } 20859749STim.Haley@Sun.COM 20865331Samw if (flags & V_RDDIR_ENTFLAGS) 20875331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 20885331Samw else 20895331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2090789Sahrens 2091789Sahrens /* 2092789Sahrens * Will this entry fit in the buffer? 2093789Sahrens */ 20943912Slling if (outcount + reclen > bufsize) { 2095789Sahrens /* 2096789Sahrens * Did we manage to fit anything in the buffer? 2097789Sahrens */ 2098789Sahrens if (!outcount) { 2099789Sahrens error = EINVAL; 2100789Sahrens goto update; 2101789Sahrens } 2102789Sahrens break; 2103789Sahrens } 21045331Samw if (flags & V_RDDIR_ENTFLAGS) { 21055331Samw /* 21065331Samw * Add extended flag entry: 21075331Samw */ 21085331Samw eodp->ed_ino = objnum; 21095331Samw eodp->ed_reclen = reclen; 21105331Samw /* NOTE: ed_off is the offset for the *next* entry */ 21115331Samw next = &(eodp->ed_off); 21125331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 21135331Samw ED_CASE_CONFLICT : 0; 21145331Samw (void) strncpy(eodp->ed_name, zap.za_name, 21155331Samw EDIRENT_NAMELEN(reclen)); 21165331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 21175331Samw } else { 21185331Samw /* 21195331Samw * Add normal entry: 21205331Samw */ 21215331Samw odp->d_ino = objnum; 21225331Samw odp->d_reclen = reclen; 21235331Samw /* NOTE: d_off is the offset for the *next* entry */ 21245331Samw next = &(odp->d_off); 21255331Samw (void) strncpy(odp->d_name, zap.za_name, 21265331Samw DIRENT64_NAMELEN(reclen)); 21275331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 21285331Samw } 21293912Slling outcount += reclen; 2130789Sahrens 2131789Sahrens ASSERT(outcount <= bufsize); 2132789Sahrens 2133789Sahrens /* Prefetch znode */ 2134869Sperrin if (prefetch) 21353912Slling dmu_prefetch(os, objnum, 0, 0); 2136789Sahrens 21379749STim.Haley@Sun.COM skip_entry: 2138789Sahrens /* 2139789Sahrens * Move to the next entry, fill in the previous offset. 2140789Sahrens */ 2141789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2142789Sahrens zap_cursor_advance(&zc); 2143789Sahrens offset = zap_cursor_serialize(&zc); 2144789Sahrens } else { 2145789Sahrens offset += 1; 2146789Sahrens } 2147789Sahrens *next = offset; 2148789Sahrens } 2149869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2150789Sahrens 2151789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2152789Sahrens iovp->iov_base += outcount; 2153789Sahrens iovp->iov_len -= outcount; 2154789Sahrens uio->uio_resid -= outcount; 2155789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2156789Sahrens /* 2157789Sahrens * Reset the pointer. 2158789Sahrens */ 2159789Sahrens offset = uio->uio_loffset; 2160789Sahrens } 2161789Sahrens 2162789Sahrens update: 2163885Sahrens zap_cursor_fini(&zc); 2164789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2165789Sahrens kmem_free(outbuf, bufsize); 2166789Sahrens 2167789Sahrens if (error == ENOENT) 2168789Sahrens error = 0; 2169789Sahrens 2170789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2171789Sahrens 2172789Sahrens uio->uio_loffset = offset; 2173789Sahrens ZFS_EXIT(zfsvfs); 2174789Sahrens return (error); 2175789Sahrens } 2176789Sahrens 21774720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 21784720Sfr157268 2179789Sahrens static int 21805331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2181789Sahrens { 2182789Sahrens znode_t *zp = VTOZ(vp); 2183789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2184789Sahrens 21851773Seschrock /* 21861773Seschrock * Regardless of whether this is required for standards conformance, 21871773Seschrock * this is the logical behavior when fsync() is called on a file with 21881773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 21891773Seschrock * going to be pushed out as part of the zil_commit(). 21901773Seschrock */ 21911773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 21921773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 21935331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 21941773Seschrock 21954720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 21964720Sfr157268 21975367Sahrens ZFS_ENTER(zfsvfs); 21985367Sahrens ZFS_VERIFY_ZP(zp); 21992638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 2200789Sahrens ZFS_EXIT(zfsvfs); 2201789Sahrens return (0); 2202789Sahrens } 2203789Sahrens 22045331Samw 2205789Sahrens /* 2206789Sahrens * Get the requested file attributes and place them in the provided 2207789Sahrens * vattr structure. 2208789Sahrens * 2209789Sahrens * IN: vp - vnode of file. 2210789Sahrens * vap - va_mask identifies requested attributes. 22115331Samw * If AT_XVATTR set, then optional attrs are requested 22125331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2213789Sahrens * cr - credentials of caller. 22145331Samw * ct - caller context 2215789Sahrens * 2216789Sahrens * OUT: vap - attribute values. 2217789Sahrens * 2218789Sahrens * RETURN: 0 (always succeeds) 2219789Sahrens */ 2220789Sahrens /* ARGSUSED */ 2221789Sahrens static int 22225331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 22235331Samw caller_context_t *ct) 2224789Sahrens { 2225789Sahrens znode_t *zp = VTOZ(vp); 2226789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 22275326Sek110237 znode_phys_t *pzp; 22285331Samw int error = 0; 22294543Smarks uint64_t links; 22305331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 22315331Samw xoptattr_t *xoap = NULL; 22325331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2233789Sahrens 22345367Sahrens ZFS_ENTER(zfsvfs); 22355367Sahrens ZFS_VERIFY_ZP(zp); 22365326Sek110237 pzp = zp->z_phys; 2237789Sahrens 22385331Samw /* 22395331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 22405331Samw * Also, if we are the owner don't bother, since owner should 22415331Samw * always be allowed to read basic attributes of file. 22425331Samw */ 22435331Samw if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) && 22445331Samw (pzp->zp_uid != crgetuid(cr))) { 22455331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 22465331Samw skipaclchk, cr)) { 22475331Samw ZFS_EXIT(zfsvfs); 22485331Samw return (error); 22495331Samw } 22505331Samw } 22515331Samw 2252789Sahrens /* 2253789Sahrens * Return all attributes. It's cheaper to provide the answer 2254789Sahrens * than to determine whether we were asked the question. 2255789Sahrens */ 2256789Sahrens 22579774SRay.Hassan@Sun.COM mutex_enter(&zp->z_lock); 2258789Sahrens vap->va_type = vp->v_type; 2259789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 22605771Sjp151216 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2261789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2262789Sahrens vap->va_nodeid = zp->z_id; 22634543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 22644543Smarks links = pzp->zp_links + 1; 22654543Smarks else 22664543Smarks links = pzp->zp_links; 22674543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2268789Sahrens vap->va_size = pzp->zp_size; 22691816Smarks vap->va_rdev = vp->v_rdev; 2270789Sahrens vap->va_seq = zp->z_seq; 2271789Sahrens 22725331Samw /* 22735331Samw * Add in any requested optional attributes and the create time. 22745331Samw * Also set the corresponding bits in the returned attribute bitmap. 22755331Samw */ 22765331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 22775331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 22785331Samw xoap->xoa_archive = 22795331Samw ((pzp->zp_flags & ZFS_ARCHIVE) != 0); 22805331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 22815331Samw } 22825331Samw 22835331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 22845331Samw xoap->xoa_readonly = 22855331Samw ((pzp->zp_flags & ZFS_READONLY) != 0); 22865331Samw XVA_SET_RTN(xvap, XAT_READONLY); 22875331Samw } 22885331Samw 22895331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 22905331Samw xoap->xoa_system = 22915331Samw ((pzp->zp_flags & ZFS_SYSTEM) != 0); 22925331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 22935331Samw } 22945331Samw 22955331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 22965331Samw xoap->xoa_hidden = 22975331Samw ((pzp->zp_flags & ZFS_HIDDEN) != 0); 22985331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 22995331Samw } 23005331Samw 23015331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 23025331Samw xoap->xoa_nounlink = 23035331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0); 23045331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 23055331Samw } 23065331Samw 23075331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 23085331Samw xoap->xoa_immutable = 23095331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0); 23105331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 23115331Samw } 23125331Samw 23135331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 23145331Samw xoap->xoa_appendonly = 23155331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0); 23165331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 23175331Samw } 23185331Samw 23195331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 23205331Samw xoap->xoa_nodump = 23215331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0); 23225331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 23235331Samw } 23245331Samw 23255331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 23265331Samw xoap->xoa_opaque = 23275331Samw ((pzp->zp_flags & ZFS_OPAQUE) != 0); 23285331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 23295331Samw } 23305331Samw 23315331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 23325331Samw xoap->xoa_av_quarantined = 23335331Samw ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0); 23345331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 23355331Samw } 23365331Samw 23375331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 23385331Samw xoap->xoa_av_modified = 23395331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0); 23405331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 23415331Samw } 23425331Samw 23435331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 23445331Samw vp->v_type == VREG && 23455331Samw (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) { 23465331Samw size_t len; 23475331Samw dmu_object_info_t doi; 23485331Samw 23495331Samw /* 23505331Samw * Only VREG files have anti-virus scanstamps, so we 23515331Samw * won't conflict with symlinks in the bonus buffer. 23525331Samw */ 23535331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 23545331Samw len = sizeof (xoap->xoa_av_scanstamp) + 23555331Samw sizeof (znode_phys_t); 23565331Samw if (len <= doi.doi_bonus_size) { 23575331Samw /* 23585331Samw * pzp points to the start of the 23595331Samw * znode_phys_t. pzp + 1 points to the 23605331Samw * first byte after the znode_phys_t. 23615331Samw */ 23625331Samw (void) memcpy(xoap->xoa_av_scanstamp, 23635331Samw pzp + 1, 23645331Samw sizeof (xoap->xoa_av_scanstamp)); 23655331Samw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 23665331Samw } 23675331Samw } 23685331Samw 23695331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 23705331Samw ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime); 23715331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 23725331Samw } 23735331Samw } 23745331Samw 2375789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2376789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2377789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2378789Sahrens 2379789Sahrens mutex_exit(&zp->z_lock); 2380789Sahrens 2381789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2382789Sahrens 2383789Sahrens if (zp->z_blksz == 0) { 2384789Sahrens /* 2385789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2386789Sahrens */ 2387789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2388789Sahrens } 2389789Sahrens 2390789Sahrens ZFS_EXIT(zfsvfs); 2391789Sahrens return (0); 2392789Sahrens } 2393789Sahrens 2394789Sahrens /* 2395789Sahrens * Set the file attributes to the values contained in the 2396789Sahrens * vattr structure. 2397789Sahrens * 2398789Sahrens * IN: vp - vnode of file to be modified. 2399789Sahrens * vap - new attribute values. 24005331Samw * If AT_XVATTR set, then optional attrs are being set 2401789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 24025331Samw * - ATTR_NOACLCHECK (CIFS context only). 2403789Sahrens * cr - credentials of caller. 24045331Samw * ct - caller context 2405789Sahrens * 2406789Sahrens * RETURN: 0 if success 2407789Sahrens * error code if failure 2408789Sahrens * 2409789Sahrens * Timestamps: 2410789Sahrens * vp - ctime updated, mtime updated if size changed. 2411789Sahrens */ 2412789Sahrens /* ARGSUSED */ 2413789Sahrens static int 2414789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2415789Sahrens caller_context_t *ct) 2416789Sahrens { 24175326Sek110237 znode_t *zp = VTOZ(vp); 24185326Sek110237 znode_phys_t *pzp; 2419789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 24205326Sek110237 zilog_t *zilog; 2421789Sahrens dmu_tx_t *tx; 24221878Smaybee vattr_t oldva; 24238190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2424789Sahrens uint_t mask = vap->va_mask; 24251878Smaybee uint_t saved_mask; 24262796Smarks int trim_mask = 0; 2427789Sahrens uint64_t new_mode; 24289179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 24291231Smarks znode_t *attrzp; 2430789Sahrens int need_policy = FALSE; 2431789Sahrens int err; 24325331Samw zfs_fuid_info_t *fuidp = NULL; 24335331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 24345331Samw xoptattr_t *xoap; 24355824Smarks zfs_acl_t *aclp = NULL; 24365331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 24379179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 2438789Sahrens 2439789Sahrens if (mask == 0) 2440789Sahrens return (0); 2441789Sahrens 2442789Sahrens if (mask & AT_NOSET) 2443789Sahrens return (EINVAL); 2444789Sahrens 24455367Sahrens ZFS_ENTER(zfsvfs); 24465367Sahrens ZFS_VERIFY_ZP(zp); 24475331Samw 24485331Samw pzp = zp->z_phys; 24495331Samw zilog = zfsvfs->z_log; 24505331Samw 24515331Samw /* 24525331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 24535331Samw * that file system is at proper version level 24545331Samw */ 24555331Samw 24565331Samw if (zfsvfs->z_use_fuids == B_FALSE && 24575331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 24585331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 24595386Stimh (mask & AT_XVATTR))) { 24605386Stimh ZFS_EXIT(zfsvfs); 24615331Samw return (EINVAL); 24625386Stimh } 24635386Stimh 24645386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 24655386Stimh ZFS_EXIT(zfsvfs); 2466789Sahrens return (EISDIR); 24675386Stimh } 24685386Stimh 24695386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 24705386Stimh ZFS_EXIT(zfsvfs); 24711308Smarks return (EINVAL); 24725386Stimh } 24731308Smarks 24745331Samw /* 24755331Samw * If this is an xvattr_t, then get a pointer to the structure of 24765331Samw * optional attributes. If this is NULL, then we have a vattr_t. 24775331Samw */ 24785331Samw xoap = xva_getxoptattr(xvap); 24795331Samw 24808190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 24818190SMark.Shellenbaum@Sun.COM 24825331Samw /* 24835331Samw * Immutable files can only alter immutable bit and atime 24845331Samw */ 24855331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 24865331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 24875386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 24885386Stimh ZFS_EXIT(zfsvfs); 24895331Samw return (EPERM); 24905386Stimh } 24915386Stimh 24925386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 24935386Stimh ZFS_EXIT(zfsvfs); 24945331Samw return (EPERM); 24955386Stimh } 2496789Sahrens 24976064Smarks /* 24986064Smarks * Verify timestamps doesn't overflow 32 bits. 24996064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 25006064Smarks * handle times greater than 2039. This check should be removed 25016064Smarks * once large timestamps are fully supported. 25026064Smarks */ 25036064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 25046064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 25056064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 25066064Smarks ZFS_EXIT(zfsvfs); 25076064Smarks return (EOVERFLOW); 25086064Smarks } 25096064Smarks } 25106064Smarks 2511789Sahrens top: 25121231Smarks attrzp = NULL; 2513789Sahrens 25149981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2515789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2516789Sahrens ZFS_EXIT(zfsvfs); 2517789Sahrens return (EROFS); 2518789Sahrens } 2519789Sahrens 2520789Sahrens /* 2521789Sahrens * First validate permissions 2522789Sahrens */ 2523789Sahrens 2524789Sahrens if (mask & AT_SIZE) { 25255331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2526789Sahrens if (err) { 2527789Sahrens ZFS_EXIT(zfsvfs); 2528789Sahrens return (err); 2529789Sahrens } 25301878Smaybee /* 25311878Smaybee * XXX - Note, we are not providing any open 25321878Smaybee * mode flags here (like FNDELAY), so we may 25331878Smaybee * block if there are locks present... this 25341878Smaybee * should be addressed in openat(). 25351878Smaybee */ 25366992Smaybee /* XXX - would it be OK to generate a log record here? */ 25376992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 25381878Smaybee if (err) { 25391878Smaybee ZFS_EXIT(zfsvfs); 25401878Smaybee return (err); 25411878Smaybee } 2542789Sahrens } 2543789Sahrens 25445331Samw if (mask & (AT_ATIME|AT_MTIME) || 25455331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 25465331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 25475331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 25485331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 25495331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 25505331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 25515331Samw skipaclchk, cr); 2552789Sahrens 2553789Sahrens if (mask & (AT_UID|AT_GID)) { 2554789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2555789Sahrens int take_owner; 2556789Sahrens int take_group; 2557789Sahrens 2558789Sahrens /* 2559913Smarks * NOTE: even if a new mode is being set, 2560913Smarks * we may clear S_ISUID/S_ISGID bits. 2561913Smarks */ 2562913Smarks 2563913Smarks if (!(mask & AT_MODE)) 2564913Smarks vap->va_mode = pzp->zp_mode; 2565913Smarks 2566913Smarks /* 2567789Sahrens * Take ownership or chgrp to group we are a member of 2568789Sahrens */ 2569789Sahrens 2570789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 25715331Samw take_group = (mask & AT_GID) && 25725331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2573789Sahrens 2574789Sahrens /* 2575789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2576789Sahrens * take_group must both be set in order to allow taking 2577789Sahrens * ownership. 2578789Sahrens * 2579789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2580789Sahrens * 2581789Sahrens */ 2582789Sahrens 2583789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2584789Sahrens ((idmask == AT_UID) && take_owner) || 2585789Sahrens ((idmask == AT_GID) && take_group)) { 25865331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 25875331Samw skipaclchk, cr) == 0) { 2588789Sahrens /* 2589789Sahrens * Remove setuid/setgid for non-privileged users 2590789Sahrens */ 25911115Smarks secpolicy_setid_clear(vap, cr); 25922796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2593789Sahrens } else { 2594789Sahrens need_policy = TRUE; 2595789Sahrens } 2596789Sahrens } else { 2597789Sahrens need_policy = TRUE; 2598789Sahrens } 2599789Sahrens } 2600789Sahrens 26012796Smarks mutex_enter(&zp->z_lock); 26022796Smarks oldva.va_mode = pzp->zp_mode; 26035771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 26045331Samw if (mask & AT_XVATTR) { 26058190SMark.Shellenbaum@Sun.COM /* 26068190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 26078190SMark.Shellenbaum@Sun.COM * that are actually changing. 26088190SMark.Shellenbaum@Sun.COM * 26098190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 26108190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 26118190SMark.Shellenbaum@Sun.COM */ 26128190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 26138190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 26148190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) { 26158190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26168190SMark.Shellenbaum@Sun.COM } else { 26178190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 26188190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 26198190SMark.Shellenbaum@Sun.COM } 26208190SMark.Shellenbaum@Sun.COM } 26218190SMark.Shellenbaum@Sun.COM 26228190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 26238190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 26248190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) { 26258190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26268190SMark.Shellenbaum@Sun.COM } else { 26278190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 26288190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 26298190SMark.Shellenbaum@Sun.COM } 26308190SMark.Shellenbaum@Sun.COM } 26318190SMark.Shellenbaum@Sun.COM 26328190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 26338190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 26348190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) { 26358190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26368190SMark.Shellenbaum@Sun.COM } else { 26378190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 26388190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 26398190SMark.Shellenbaum@Sun.COM } 26408190SMark.Shellenbaum@Sun.COM } 26418190SMark.Shellenbaum@Sun.COM 26428190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 26438190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 26448190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NODUMP) != 0)) { 26458190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26468190SMark.Shellenbaum@Sun.COM } else { 26478190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 26488190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 26498190SMark.Shellenbaum@Sun.COM } 26508190SMark.Shellenbaum@Sun.COM } 26518190SMark.Shellenbaum@Sun.COM 26528190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 26538190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 26548190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) { 26558190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26568190SMark.Shellenbaum@Sun.COM } else { 26578190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 26588190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 26598190SMark.Shellenbaum@Sun.COM } 26608190SMark.Shellenbaum@Sun.COM } 26618190SMark.Shellenbaum@Sun.COM 26628190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 26638190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 26648190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 26658190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 26668190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) { 26678190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26688190SMark.Shellenbaum@Sun.COM } else { 26698190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 26708190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 26718190SMark.Shellenbaum@Sun.COM } 26728190SMark.Shellenbaum@Sun.COM } 26738190SMark.Shellenbaum@Sun.COM 26748190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 26758190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 26768190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 26775331Samw need_policy = TRUE; 26785331Samw } 26795331Samw } 26805331Samw 26812796Smarks mutex_exit(&zp->z_lock); 26822796Smarks 26832796Smarks if (mask & AT_MODE) { 26845331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 26852796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 26862796Smarks &oldva, cr); 26872796Smarks if (err) { 26882796Smarks ZFS_EXIT(zfsvfs); 26892796Smarks return (err); 26902796Smarks } 26912796Smarks trim_mask |= AT_MODE; 26922796Smarks } else { 26932796Smarks need_policy = TRUE; 26942796Smarks } 26952796Smarks } 2696789Sahrens 2697789Sahrens if (need_policy) { 26981115Smarks /* 26991115Smarks * If trim_mask is set then take ownership 27002796Smarks * has been granted or write_acl is present and user 27012796Smarks * has the ability to modify mode. In that case remove 27022796Smarks * UID|GID and or MODE from mask so that 27031115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 27041115Smarks */ 27052796Smarks 27062796Smarks if (trim_mask) { 27072796Smarks saved_mask = vap->va_mask; 27082796Smarks vap->va_mask &= ~trim_mask; 27092796Smarks } 2710789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 27115331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2712789Sahrens if (err) { 2713789Sahrens ZFS_EXIT(zfsvfs); 2714789Sahrens return (err); 2715789Sahrens } 27161115Smarks 27171115Smarks if (trim_mask) 27182796Smarks vap->va_mask |= saved_mask; 2719789Sahrens } 2720789Sahrens 2721789Sahrens /* 2722789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2723789Sahrens * changed va_mask 2724789Sahrens */ 2725789Sahrens mask = vap->va_mask; 2726789Sahrens 2727789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2728789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2729789Sahrens 2730789Sahrens if (mask & AT_MODE) { 27311576Smarks uint64_t pmode = pzp->zp_mode; 27321576Smarks 27331576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2734789Sahrens 27359396SMatthew.Ahrens@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 27369396SMatthew.Ahrens@Sun.COM goto out; 27375331Samw if (pzp->zp_acl.z_acl_extern_obj) { 27385331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 27395331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 27405331Samw pzp->zp_acl.z_acl_version == 27415331Samw ZFS_ACL_VERSION_INITIAL) { 27425331Samw dmu_tx_hold_free(tx, 27435331Samw pzp->zp_acl.z_acl_extern_obj, 0, 27445331Samw DMU_OBJECT_END); 27455331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 27465824Smarks 0, aclp->z_acl_bytes); 27475331Samw } else { 27485331Samw dmu_tx_hold_write(tx, 27495331Samw pzp->zp_acl.z_acl_extern_obj, 0, 27505824Smarks aclp->z_acl_bytes); 27515331Samw } 27526180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 27536180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 27546180Smarks 0, aclp->z_acl_bytes); 27555331Samw } 2756789Sahrens } 2757789Sahrens 27589179SMark.Shellenbaum@Sun.COM if (mask & (AT_UID | AT_GID)) { 27599179SMark.Shellenbaum@Sun.COM if (pzp->zp_xattr) { 27609179SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 27619396SMatthew.Ahrens@Sun.COM if (err) 27629396SMatthew.Ahrens@Sun.COM goto out; 27639179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, attrzp->z_id); 27649179SMark.Shellenbaum@Sun.COM } 27659179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 27669179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 27679179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 27689396SMatthew.Ahrens@Sun.COM if (new_uid != pzp->zp_uid && 27699396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) { 27709396SMatthew.Ahrens@Sun.COM err = EDQUOT; 27719396SMatthew.Ahrens@Sun.COM goto out; 27729396SMatthew.Ahrens@Sun.COM } 27731231Smarks } 27749396SMatthew.Ahrens@Sun.COM 27759179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 27769179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 27779179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 27789396SMatthew.Ahrens@Sun.COM if (new_gid != pzp->zp_gid && 27799396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) { 27809396SMatthew.Ahrens@Sun.COM err = EDQUOT; 27819396SMatthew.Ahrens@Sun.COM goto out; 27829396SMatthew.Ahrens@Sun.COM } 27839179SMark.Shellenbaum@Sun.COM } 27849179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 27859179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 27869179SMark.Shellenbaum@Sun.COM if (zfsvfs->z_fuid_obj == 0) { 27879179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 27889179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 27899179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 27909179SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 27919179SMark.Shellenbaum@Sun.COM FALSE, NULL); 27929179SMark.Shellenbaum@Sun.COM } else { 27939179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 27949179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 27959179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 27969179SMark.Shellenbaum@Sun.COM } 27979179SMark.Shellenbaum@Sun.COM } 27981231Smarks } 27991231Smarks 28008227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2801789Sahrens if (err) { 28029396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 28032113Sahrens dmu_tx_wait(tx); 28049396SMatthew.Ahrens@Sun.COM goto out; 2805789Sahrens } 2806789Sahrens 2807789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2808789Sahrens 2809789Sahrens /* 2810789Sahrens * Set each attribute requested. 2811789Sahrens * We group settings according to the locks they need to acquire. 2812789Sahrens * 2813789Sahrens * Note: you cannot set ctime directly, although it will be 2814789Sahrens * updated as a side-effect of calling this function. 2815789Sahrens */ 2816789Sahrens 2817789Sahrens mutex_enter(&zp->z_lock); 2818789Sahrens 2819789Sahrens if (mask & AT_MODE) { 28205824Smarks mutex_enter(&zp->z_acl_lock); 28215824Smarks zp->z_phys->zp_mode = new_mode; 28229179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 2823789Sahrens ASSERT3U(err, ==, 0); 282410143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 282510143STim.Haley@Sun.COM aclp = NULL; 28265824Smarks mutex_exit(&zp->z_acl_lock); 2827789Sahrens } 2828789Sahrens 28291231Smarks if (attrzp) 28301231Smarks mutex_enter(&attrzp->z_lock); 28311231Smarks 28321231Smarks if (mask & AT_UID) { 28339179SMark.Shellenbaum@Sun.COM pzp->zp_uid = new_uid; 28349179SMark.Shellenbaum@Sun.COM if (attrzp) 28359179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_uid = new_uid; 28361231Smarks } 28371231Smarks 28381231Smarks if (mask & AT_GID) { 28399179SMark.Shellenbaum@Sun.COM pzp->zp_gid = new_gid; 28401231Smarks if (attrzp) 28419179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_gid = new_gid; 28421231Smarks } 28431231Smarks 28441231Smarks if (attrzp) 28451231Smarks mutex_exit(&attrzp->z_lock); 2846789Sahrens 2847789Sahrens if (mask & AT_ATIME) 2848789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2849789Sahrens 2850789Sahrens if (mask & AT_MTIME) 2851789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2852789Sahrens 28536992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 28541878Smaybee if (mask & AT_SIZE) 2855789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 28561878Smaybee else if (mask != 0) 2857789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 28585331Samw /* 28595331Samw * Do this after setting timestamps to prevent timestamp 28605331Samw * update from toggling bit 28615331Samw */ 28625331Samw 28635331Samw if (xoap && (mask & AT_XVATTR)) { 28648190SMark.Shellenbaum@Sun.COM 28658190SMark.Shellenbaum@Sun.COM /* 28668190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 28678190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 28688190SMark.Shellenbaum@Sun.COM */ 28698190SMark.Shellenbaum@Sun.COM 28708190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 28718190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 28728190SMark.Shellenbaum@Sun.COM } 28738190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 28748190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 28758190SMark.Shellenbaum@Sun.COM } 28768190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 28778190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 28788190SMark.Shellenbaum@Sun.COM } 28798190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 28808190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 28818190SMark.Shellenbaum@Sun.COM } 28828190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 28838190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 28848190SMark.Shellenbaum@Sun.COM } 28858190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 28868190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 28878190SMark.Shellenbaum@Sun.COM } 28888190SMark.Shellenbaum@Sun.COM 28895331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 28905331Samw size_t len; 28915331Samw dmu_object_info_t doi; 28925331Samw 28935331Samw ASSERT(vp->v_type == VREG); 28945331Samw 28955331Samw /* Grow the bonus buffer if necessary. */ 28965331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 28975331Samw len = sizeof (xoap->xoa_av_scanstamp) + 28985331Samw sizeof (znode_phys_t); 28995331Samw if (len > doi.doi_bonus_size) 29005331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 29015331Samw } 29025331Samw zfs_xvattr_set(zp, xvap); 29035331Samw } 2904789Sahrens 29059179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 29069179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 29079179SMark.Shellenbaum@Sun.COM 29081878Smaybee if (mask != 0) 29095331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 29105331Samw 2911789Sahrens mutex_exit(&zp->z_lock); 2912789Sahrens 29139396SMatthew.Ahrens@Sun.COM out: 29141231Smarks if (attrzp) 29151231Smarks VN_RELE(ZTOV(attrzp)); 29161231Smarks 291710143STim.Haley@Sun.COM if (aclp) 291810143STim.Haley@Sun.COM zfs_acl_free(aclp); 291910143STim.Haley@Sun.COM 29209396SMatthew.Ahrens@Sun.COM if (fuidp) { 29219396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 29229396SMatthew.Ahrens@Sun.COM fuidp = NULL; 29239396SMatthew.Ahrens@Sun.COM } 29249396SMatthew.Ahrens@Sun.COM 29259396SMatthew.Ahrens@Sun.COM if (err) 29269396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 29279396SMatthew.Ahrens@Sun.COM else 29289396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 29299396SMatthew.Ahrens@Sun.COM 29309396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 29319396SMatthew.Ahrens@Sun.COM goto top; 2932789Sahrens 2933789Sahrens ZFS_EXIT(zfsvfs); 2934789Sahrens return (err); 2935789Sahrens } 2936789Sahrens 29373271Smaybee typedef struct zfs_zlock { 29383271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 29393271Smaybee znode_t *zl_znode; /* znode we held */ 29403271Smaybee struct zfs_zlock *zl_next; /* next in list */ 29413271Smaybee } zfs_zlock_t; 29423271Smaybee 29433271Smaybee /* 29443271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 29453271Smaybee */ 29463271Smaybee static void 29473271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 29483271Smaybee { 29493271Smaybee zfs_zlock_t *zl; 29503271Smaybee 29513271Smaybee while ((zl = *zlpp) != NULL) { 29523271Smaybee if (zl->zl_znode != NULL) 29533271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 29543271Smaybee rw_exit(zl->zl_rwlock); 29553271Smaybee *zlpp = zl->zl_next; 29563271Smaybee kmem_free(zl, sizeof (*zl)); 29573271Smaybee } 29583271Smaybee } 29593271Smaybee 2960789Sahrens /* 2961789Sahrens * Search back through the directory tree, using the ".." entries. 2962789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2963789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2964789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2965789Sahrens */ 2966789Sahrens static int 2967789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2968789Sahrens { 2969789Sahrens zfs_zlock_t *zl; 29703638Sbillm znode_t *zp = tdzp; 2971789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2972789Sahrens uint64_t *oidp = &zp->z_id; 2973789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2974789Sahrens krw_t rw = RW_WRITER; 2975789Sahrens 2976789Sahrens /* 2977789Sahrens * First pass write-locks szp and compares to zp->z_id. 2978789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2979789Sahrens */ 2980789Sahrens do { 29813271Smaybee if (!rw_tryenter(rwlp, rw)) { 29823271Smaybee /* 29833271Smaybee * Another thread is renaming in this path. 29843271Smaybee * Note that if we are a WRITER, we don't have any 29853271Smaybee * parent_locks held yet. 29863271Smaybee */ 29873271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 29883271Smaybee /* 29893271Smaybee * Drop our locks and restart 29903271Smaybee */ 29913271Smaybee zfs_rename_unlock(&zl); 29923271Smaybee *zlpp = NULL; 29933271Smaybee zp = tdzp; 29943271Smaybee oidp = &zp->z_id; 29953271Smaybee rwlp = &szp->z_parent_lock; 29963271Smaybee rw = RW_WRITER; 29973271Smaybee continue; 29983271Smaybee } else { 29993271Smaybee /* 30003271Smaybee * Wait for other thread to drop its locks 30013271Smaybee */ 30023271Smaybee rw_enter(rwlp, rw); 30033271Smaybee } 30043271Smaybee } 30053271Smaybee 3006789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3007789Sahrens zl->zl_rwlock = rwlp; 3008789Sahrens zl->zl_znode = NULL; 3009789Sahrens zl->zl_next = *zlpp; 3010789Sahrens *zlpp = zl; 3011789Sahrens 3012789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 3013789Sahrens return (EINVAL); 3014789Sahrens 3015789Sahrens if (*oidp == rootid) /* We've hit the top */ 3016789Sahrens return (0); 3017789Sahrens 3018789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 3019789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 3020789Sahrens if (error) 3021789Sahrens return (error); 3022789Sahrens zl->zl_znode = zp; 3023789Sahrens } 3024789Sahrens oidp = &zp->z_phys->zp_parent; 3025789Sahrens rwlp = &zp->z_parent_lock; 3026789Sahrens rw = RW_READER; 3027789Sahrens 3028789Sahrens } while (zp->z_id != sdzp->z_id); 3029789Sahrens 3030789Sahrens return (0); 3031789Sahrens } 3032789Sahrens 3033789Sahrens /* 3034789Sahrens * Move an entry from the provided source directory to the target 3035789Sahrens * directory. Change the entry name as indicated. 3036789Sahrens * 3037789Sahrens * IN: sdvp - Source directory containing the "old entry". 3038789Sahrens * snm - Old entry name. 3039789Sahrens * tdvp - Target directory to contain the "new entry". 3040789Sahrens * tnm - New entry name. 3041789Sahrens * cr - credentials of caller. 30425331Samw * ct - caller context 30435331Samw * flags - case flags 3044789Sahrens * 3045789Sahrens * RETURN: 0 if success 3046789Sahrens * error code if failure 3047789Sahrens * 3048789Sahrens * Timestamps: 3049789Sahrens * sdvp,tdvp - ctime|mtime updated 3050789Sahrens */ 30515331Samw /*ARGSUSED*/ 3052789Sahrens static int 30535331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 30545331Samw caller_context_t *ct, int flags) 3055789Sahrens { 3056789Sahrens znode_t *tdzp, *szp, *tzp; 3057789Sahrens znode_t *sdzp = VTOZ(sdvp); 3058789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 30595326Sek110237 zilog_t *zilog; 3060789Sahrens vnode_t *realvp; 3061789Sahrens zfs_dirlock_t *sdl, *tdl; 3062789Sahrens dmu_tx_t *tx; 3063789Sahrens zfs_zlock_t *zl; 30645331Samw int cmp, serr, terr; 30655331Samw int error = 0; 30665331Samw int zflg = 0; 3067789Sahrens 30685367Sahrens ZFS_ENTER(zfsvfs); 30695367Sahrens ZFS_VERIFY_ZP(sdzp); 30705326Sek110237 zilog = zfsvfs->z_log; 3071789Sahrens 3072789Sahrens /* 3073789Sahrens * Make sure we have the real vp for the target directory. 3074789Sahrens */ 30755331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3076789Sahrens tdvp = realvp; 3077789Sahrens 3078789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 3079789Sahrens ZFS_EXIT(zfsvfs); 3080789Sahrens return (EXDEV); 3081789Sahrens } 3082789Sahrens 3083789Sahrens tdzp = VTOZ(tdvp); 30845367Sahrens ZFS_VERIFY_ZP(tdzp); 30855498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 30865331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 30875331Samw ZFS_EXIT(zfsvfs); 30885331Samw return (EILSEQ); 30895331Samw } 30905331Samw 30915331Samw if (flags & FIGNORECASE) 30925331Samw zflg |= ZCILOOK; 30935331Samw 3094789Sahrens top: 3095789Sahrens szp = NULL; 3096789Sahrens tzp = NULL; 3097789Sahrens zl = NULL; 3098789Sahrens 3099789Sahrens /* 3100789Sahrens * This is to prevent the creation of links into attribute space 3101789Sahrens * by renaming a linked file into/outof an attribute directory. 3102789Sahrens * See the comment in zfs_link() for why this is considered bad. 3103789Sahrens */ 3104789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 3105789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 3106789Sahrens ZFS_EXIT(zfsvfs); 3107789Sahrens return (EINVAL); 3108789Sahrens } 3109789Sahrens 3110789Sahrens /* 3111789Sahrens * Lock source and target directory entries. To prevent deadlock, 3112789Sahrens * a lock ordering must be defined. We lock the directory with 3113789Sahrens * the smallest object id first, or if it's a tie, the one with 3114789Sahrens * the lexically first name. 3115789Sahrens */ 3116789Sahrens if (sdzp->z_id < tdzp->z_id) { 3117789Sahrens cmp = -1; 3118789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3119789Sahrens cmp = 1; 3120789Sahrens } else { 31215331Samw /* 31225331Samw * First compare the two name arguments without 31235331Samw * considering any case folding. 31245331Samw */ 31255331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 31265331Samw 31275331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 31285498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3129789Sahrens if (cmp == 0) { 3130789Sahrens /* 3131789Sahrens * POSIX: "If the old argument and the new argument 3132789Sahrens * both refer to links to the same existing file, 3133789Sahrens * the rename() function shall return successfully 3134789Sahrens * and perform no other action." 3135789Sahrens */ 3136789Sahrens ZFS_EXIT(zfsvfs); 3137789Sahrens return (0); 3138789Sahrens } 31395331Samw /* 31405331Samw * If the file system is case-folding, then we may 31415331Samw * have some more checking to do. A case-folding file 31425331Samw * system is either supporting mixed case sensitivity 31435331Samw * access or is completely case-insensitive. Note 31445331Samw * that the file system is always case preserving. 31455331Samw * 31465331Samw * In mixed sensitivity mode case sensitive behavior 31475331Samw * is the default. FIGNORECASE must be used to 31485331Samw * explicitly request case insensitive behavior. 31495331Samw * 31505331Samw * If the source and target names provided differ only 31515331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 31525331Samw * we will treat this as a special case in the 31535331Samw * case-insensitive mode: as long as the source name 31545331Samw * is an exact match, we will allow this to proceed as 31555331Samw * a name-change request. 31565331Samw */ 31575498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 31585498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 31595498Stimh flags & FIGNORECASE)) && 31605331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 31615331Samw &error) == 0) { 31625331Samw /* 31635331Samw * case preserving rename request, require exact 31645331Samw * name matches 31655331Samw */ 31665331Samw zflg |= ZCIEXACT; 31675331Samw zflg &= ~ZCILOOK; 31685331Samw } 3169789Sahrens } 31705331Samw 3171789Sahrens if (cmp < 0) { 31725331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 31735331Samw ZEXISTS | zflg, NULL, NULL); 31745331Samw terr = zfs_dirent_lock(&tdl, 31755331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3176789Sahrens } else { 31775331Samw terr = zfs_dirent_lock(&tdl, 31785331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 31795331Samw serr = zfs_dirent_lock(&sdl, 31805331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 31815331Samw NULL, NULL); 3182789Sahrens } 3183789Sahrens 3184789Sahrens if (serr) { 3185789Sahrens /* 3186789Sahrens * Source entry invalid or not there. 3187789Sahrens */ 3188789Sahrens if (!terr) { 3189789Sahrens zfs_dirent_unlock(tdl); 3190789Sahrens if (tzp) 3191789Sahrens VN_RELE(ZTOV(tzp)); 3192789Sahrens } 3193789Sahrens if (strcmp(snm, "..") == 0) 3194789Sahrens serr = EINVAL; 3195789Sahrens ZFS_EXIT(zfsvfs); 3196789Sahrens return (serr); 3197789Sahrens } 3198789Sahrens if (terr) { 3199789Sahrens zfs_dirent_unlock(sdl); 3200789Sahrens VN_RELE(ZTOV(szp)); 3201789Sahrens if (strcmp(tnm, "..") == 0) 3202789Sahrens terr = EINVAL; 3203789Sahrens ZFS_EXIT(zfsvfs); 3204789Sahrens return (terr); 3205789Sahrens } 3206789Sahrens 3207789Sahrens /* 3208789Sahrens * Must have write access at the source to remove the old entry 3209789Sahrens * and write access at the target to create the new entry. 3210789Sahrens * Note that if target and source are the same, this can be 3211789Sahrens * done in a single check. 3212789Sahrens */ 3213789Sahrens 3214789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3215789Sahrens goto out; 3216789Sahrens 3217789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3218789Sahrens /* 3219789Sahrens * Check to make sure rename is valid. 3220789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3221789Sahrens */ 3222789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3223789Sahrens goto out; 3224789Sahrens } 3225789Sahrens 3226789Sahrens /* 3227789Sahrens * Does target exist? 3228789Sahrens */ 3229789Sahrens if (tzp) { 3230789Sahrens /* 3231789Sahrens * Source and target must be the same type. 3232789Sahrens */ 3233789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3234789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3235789Sahrens error = ENOTDIR; 3236789Sahrens goto out; 3237789Sahrens } 3238789Sahrens } else { 3239789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3240789Sahrens error = EISDIR; 3241789Sahrens goto out; 3242789Sahrens } 3243789Sahrens } 3244789Sahrens /* 3245789Sahrens * POSIX dictates that when the source and target 3246789Sahrens * entries refer to the same file object, rename 3247789Sahrens * must do nothing and exit without error. 3248789Sahrens */ 3249789Sahrens if (szp->z_id == tzp->z_id) { 3250789Sahrens error = 0; 3251789Sahrens goto out; 3252789Sahrens } 3253789Sahrens } 3254789Sahrens 32555331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3256789Sahrens if (tzp) 32575331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 32584863Spraks 32594863Spraks /* 32604863Spraks * notify the target directory if it is not the same 32614863Spraks * as source directory. 32624863Spraks */ 32634863Spraks if (tdvp != sdvp) { 32645331Samw vnevent_rename_dest_dir(tdvp, ct); 32654863Spraks } 3266789Sahrens 3267789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3268789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3269789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 32701544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 32711544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 32721544Seschrock if (sdzp != tdzp) 3273789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 32741544Seschrock if (tzp) 32751544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 32763461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 32778227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3278789Sahrens if (error) { 3279789Sahrens if (zl != NULL) 3280789Sahrens zfs_rename_unlock(&zl); 3281789Sahrens zfs_dirent_unlock(sdl); 3282789Sahrens zfs_dirent_unlock(tdl); 3283789Sahrens VN_RELE(ZTOV(szp)); 3284789Sahrens if (tzp) 3285789Sahrens VN_RELE(ZTOV(tzp)); 32868227SNeil.Perrin@Sun.COM if (error == ERESTART) { 32872113Sahrens dmu_tx_wait(tx); 32882113Sahrens dmu_tx_abort(tx); 3289789Sahrens goto top; 3290789Sahrens } 32912113Sahrens dmu_tx_abort(tx); 3292789Sahrens ZFS_EXIT(zfsvfs); 3293789Sahrens return (error); 3294789Sahrens } 3295789Sahrens 3296789Sahrens if (tzp) /* Attempt to remove the existing target */ 32975331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3298789Sahrens 3299789Sahrens if (error == 0) { 3300789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3301789Sahrens if (error == 0) { 33025331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 33035331Samw 3304789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3305789Sahrens ASSERT(error == 0); 33065331Samw 33075331Samw zfs_log_rename(zilog, tx, 33085331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 33095331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 33106976Seschrock 33116976Seschrock /* Update path information for the target vnode */ 33126976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3313789Sahrens } 3314789Sahrens } 3315789Sahrens 3316789Sahrens dmu_tx_commit(tx); 3317789Sahrens out: 3318789Sahrens if (zl != NULL) 3319789Sahrens zfs_rename_unlock(&zl); 3320789Sahrens 3321789Sahrens zfs_dirent_unlock(sdl); 3322789Sahrens zfs_dirent_unlock(tdl); 3323789Sahrens 3324789Sahrens VN_RELE(ZTOV(szp)); 3325789Sahrens if (tzp) 3326789Sahrens VN_RELE(ZTOV(tzp)); 3327789Sahrens 3328789Sahrens ZFS_EXIT(zfsvfs); 3329789Sahrens return (error); 3330789Sahrens } 3331789Sahrens 3332789Sahrens /* 3333789Sahrens * Insert the indicated symbolic reference entry into the directory. 3334789Sahrens * 3335789Sahrens * IN: dvp - Directory to contain new symbolic link. 3336789Sahrens * link - Name for new symlink entry. 3337789Sahrens * vap - Attributes of new entry. 3338789Sahrens * target - Target path of new symlink. 3339789Sahrens * cr - credentials of caller. 33405331Samw * ct - caller context 33415331Samw * flags - case flags 3342789Sahrens * 3343789Sahrens * RETURN: 0 if success 3344789Sahrens * error code if failure 3345789Sahrens * 3346789Sahrens * Timestamps: 3347789Sahrens * dvp - ctime|mtime updated 3348789Sahrens */ 33495331Samw /*ARGSUSED*/ 3350789Sahrens static int 33515331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 33525331Samw caller_context_t *ct, int flags) 3353789Sahrens { 3354789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3355789Sahrens zfs_dirlock_t *dl; 3356789Sahrens dmu_tx_t *tx; 3357789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 33585326Sek110237 zilog_t *zilog; 3359789Sahrens int len = strlen(link); 3360789Sahrens int error; 33615331Samw int zflg = ZNEW; 33629179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 33639179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 3364789Sahrens 3365789Sahrens ASSERT(vap->va_type == VLNK); 3366789Sahrens 33675367Sahrens ZFS_ENTER(zfsvfs); 33685367Sahrens ZFS_VERIFY_ZP(dzp); 33695326Sek110237 zilog = zfsvfs->z_log; 33705331Samw 33715498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 33725331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 33735331Samw ZFS_EXIT(zfsvfs); 33745331Samw return (EILSEQ); 33755331Samw } 33765331Samw if (flags & FIGNORECASE) 33775331Samw zflg |= ZCILOOK; 3378789Sahrens top: 33795331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3380789Sahrens ZFS_EXIT(zfsvfs); 3381789Sahrens return (error); 3382789Sahrens } 3383789Sahrens 3384789Sahrens if (len > MAXPATHLEN) { 3385789Sahrens ZFS_EXIT(zfsvfs); 3386789Sahrens return (ENAMETOOLONG); 3387789Sahrens } 3388789Sahrens 3389789Sahrens /* 3390789Sahrens * Attempt to lock directory; fail if entry already exists. 3391789Sahrens */ 33925331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 33935331Samw if (error) { 3394789Sahrens ZFS_EXIT(zfsvfs); 3395789Sahrens return (error); 3396789Sahrens } 3397789Sahrens 33989179SMark.Shellenbaum@Sun.COM VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids)); 33999396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 34009396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 34019396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 34029396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 34039396SMatthew.Ahrens@Sun.COM return (EDQUOT); 34049396SMatthew.Ahrens@Sun.COM } 3405789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 34069179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3407789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3408789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 34091544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 34109179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 3411789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 34129396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 34139396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 34148227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3415789Sahrens if (error) { 34169179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3417789Sahrens zfs_dirent_unlock(dl); 34188227SNeil.Perrin@Sun.COM if (error == ERESTART) { 34192113Sahrens dmu_tx_wait(tx); 34202113Sahrens dmu_tx_abort(tx); 3421789Sahrens goto top; 3422789Sahrens } 34232113Sahrens dmu_tx_abort(tx); 3424789Sahrens ZFS_EXIT(zfsvfs); 3425789Sahrens return (error); 3426789Sahrens } 3427789Sahrens 3428789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3429789Sahrens 3430789Sahrens /* 3431789Sahrens * Create a new object for the symlink. 3432789Sahrens * Put the link content into bonus buffer if it will fit; 3433789Sahrens * otherwise, store it just like any other file data. 3434789Sahrens */ 3435789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 34369179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids); 3437789Sahrens if (len != 0) 3438789Sahrens bcopy(link, zp->z_phys + 1, len); 3439789Sahrens } else { 3440789Sahrens dmu_buf_t *dbp; 34411669Sperrin 34429179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 34439179SMark.Shellenbaum@Sun.COM 34449179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 34459179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 34461669Sperrin /* 34471669Sperrin * Nothing can access the znode yet so no locking needed 34481669Sperrin * for growing the znode's blocksize. 34491669Sperrin */ 34501669Sperrin zfs_grow_blocksize(zp, len, tx); 3451789Sahrens 34525446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 34535446Sahrens zp->z_id, 0, FTAG, &dbp)); 3454789Sahrens dmu_buf_will_dirty(dbp, tx); 3455789Sahrens 3456789Sahrens ASSERT3U(len, <=, dbp->db_size); 3457789Sahrens bcopy(link, dbp->db_data, len); 34581544Seschrock dmu_buf_rele(dbp, FTAG); 3459789Sahrens } 3460789Sahrens zp->z_phys->zp_size = len; 3461789Sahrens 3462789Sahrens /* 3463789Sahrens * Insert the new object into the directory. 3464789Sahrens */ 3465789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 34665331Samw if (error == 0) { 34675331Samw uint64_t txtype = TX_SYMLINK; 34685331Samw if (flags & FIGNORECASE) 34695331Samw txtype |= TX_CI; 34705331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 34715331Samw } 34729179SMark.Shellenbaum@Sun.COM 34739179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3474789Sahrens 3475789Sahrens dmu_tx_commit(tx); 3476789Sahrens 3477789Sahrens zfs_dirent_unlock(dl); 3478789Sahrens 3479789Sahrens VN_RELE(ZTOV(zp)); 3480789Sahrens 3481789Sahrens ZFS_EXIT(zfsvfs); 3482789Sahrens return (error); 3483789Sahrens } 3484789Sahrens 3485789Sahrens /* 3486789Sahrens * Return, in the buffer contained in the provided uio structure, 3487789Sahrens * the symbolic path referred to by vp. 3488789Sahrens * 3489789Sahrens * IN: vp - vnode of symbolic link. 3490789Sahrens * uoip - structure to contain the link path. 3491789Sahrens * cr - credentials of caller. 34925331Samw * ct - caller context 3493789Sahrens * 3494789Sahrens * OUT: uio - structure to contain the link path. 3495789Sahrens * 3496789Sahrens * RETURN: 0 if success 3497789Sahrens * error code if failure 3498789Sahrens * 3499789Sahrens * Timestamps: 3500789Sahrens * vp - atime updated 3501789Sahrens */ 3502789Sahrens /* ARGSUSED */ 3503789Sahrens static int 35045331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3505789Sahrens { 3506789Sahrens znode_t *zp = VTOZ(vp); 3507789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3508789Sahrens size_t bufsz; 3509789Sahrens int error; 3510789Sahrens 35115367Sahrens ZFS_ENTER(zfsvfs); 35125367Sahrens ZFS_VERIFY_ZP(zp); 3513789Sahrens 3514789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3515789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3516789Sahrens error = uiomove(zp->z_phys + 1, 3517789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3518789Sahrens } else { 35191544Seschrock dmu_buf_t *dbp; 35201544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 35211544Seschrock if (error) { 3522789Sahrens ZFS_EXIT(zfsvfs); 3523789Sahrens return (error); 3524789Sahrens } 3525789Sahrens error = uiomove(dbp->db_data, 3526789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 35271544Seschrock dmu_buf_rele(dbp, FTAG); 3528789Sahrens } 3529789Sahrens 3530789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3531789Sahrens ZFS_EXIT(zfsvfs); 3532789Sahrens return (error); 3533789Sahrens } 3534789Sahrens 3535789Sahrens /* 3536789Sahrens * Insert a new entry into directory tdvp referencing svp. 3537789Sahrens * 3538789Sahrens * IN: tdvp - Directory to contain new entry. 3539789Sahrens * svp - vnode of new entry. 3540789Sahrens * name - name of new entry. 3541789Sahrens * cr - credentials of caller. 35425331Samw * ct - caller context 3543789Sahrens * 3544789Sahrens * RETURN: 0 if success 3545789Sahrens * error code if failure 3546789Sahrens * 3547789Sahrens * Timestamps: 3548789Sahrens * tdvp - ctime|mtime updated 3549789Sahrens * svp - ctime updated 3550789Sahrens */ 3551789Sahrens /* ARGSUSED */ 3552789Sahrens static int 35535331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 35545331Samw caller_context_t *ct, int flags) 3555789Sahrens { 3556789Sahrens znode_t *dzp = VTOZ(tdvp); 3557789Sahrens znode_t *tzp, *szp; 3558789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 35595326Sek110237 zilog_t *zilog; 3560789Sahrens zfs_dirlock_t *dl; 3561789Sahrens dmu_tx_t *tx; 3562789Sahrens vnode_t *realvp; 3563789Sahrens int error; 35645331Samw int zf = ZNEW; 35655331Samw uid_t owner; 3566789Sahrens 3567789Sahrens ASSERT(tdvp->v_type == VDIR); 3568789Sahrens 35695367Sahrens ZFS_ENTER(zfsvfs); 35705367Sahrens ZFS_VERIFY_ZP(dzp); 35715326Sek110237 zilog = zfsvfs->z_log; 3572789Sahrens 35735331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3574789Sahrens svp = realvp; 3575789Sahrens 3576789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3577789Sahrens ZFS_EXIT(zfsvfs); 3578789Sahrens return (EXDEV); 3579789Sahrens } 35805367Sahrens szp = VTOZ(svp); 35815367Sahrens ZFS_VERIFY_ZP(szp); 3582789Sahrens 35835498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 35845331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 35855331Samw ZFS_EXIT(zfsvfs); 35865331Samw return (EILSEQ); 35875331Samw } 35885331Samw if (flags & FIGNORECASE) 35895331Samw zf |= ZCILOOK; 35905331Samw 3591789Sahrens top: 3592789Sahrens /* 3593789Sahrens * We do not support links between attributes and non-attributes 3594789Sahrens * because of the potential security risk of creating links 3595789Sahrens * into "normal" file space in order to circumvent restrictions 3596789Sahrens * imposed in attribute space. 3597789Sahrens */ 3598789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3599789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3600789Sahrens ZFS_EXIT(zfsvfs); 3601789Sahrens return (EINVAL); 3602789Sahrens } 3603789Sahrens 3604789Sahrens /* 3605789Sahrens * POSIX dictates that we return EPERM here. 3606789Sahrens * Better choices include ENOTSUP or EISDIR. 3607789Sahrens */ 3608789Sahrens if (svp->v_type == VDIR) { 3609789Sahrens ZFS_EXIT(zfsvfs); 3610789Sahrens return (EPERM); 3611789Sahrens } 3612789Sahrens 36135959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 36145331Samw if (owner != crgetuid(cr) && 3615789Sahrens secpolicy_basic_link(cr) != 0) { 3616789Sahrens ZFS_EXIT(zfsvfs); 3617789Sahrens return (EPERM); 3618789Sahrens } 3619789Sahrens 36205331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3621789Sahrens ZFS_EXIT(zfsvfs); 3622789Sahrens return (error); 3623789Sahrens } 3624789Sahrens 3625789Sahrens /* 3626789Sahrens * Attempt to lock directory; fail if entry already exists. 3627789Sahrens */ 36285331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 36295331Samw if (error) { 3630789Sahrens ZFS_EXIT(zfsvfs); 3631789Sahrens return (error); 3632789Sahrens } 3633789Sahrens 3634789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3635789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 36361544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 36378227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3638789Sahrens if (error) { 3639789Sahrens zfs_dirent_unlock(dl); 36408227SNeil.Perrin@Sun.COM if (error == ERESTART) { 36412113Sahrens dmu_tx_wait(tx); 36422113Sahrens dmu_tx_abort(tx); 3643789Sahrens goto top; 3644789Sahrens } 36452113Sahrens dmu_tx_abort(tx); 3646789Sahrens ZFS_EXIT(zfsvfs); 3647789Sahrens return (error); 3648789Sahrens } 3649789Sahrens 3650789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3651789Sahrens 36525331Samw if (error == 0) { 36535331Samw uint64_t txtype = TX_LINK; 36545331Samw if (flags & FIGNORECASE) 36555331Samw txtype |= TX_CI; 36565331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 36575331Samw } 3658789Sahrens 3659789Sahrens dmu_tx_commit(tx); 3660789Sahrens 3661789Sahrens zfs_dirent_unlock(dl); 3662789Sahrens 36634863Spraks if (error == 0) { 36645331Samw vnevent_link(svp, ct); 36654863Spraks } 36664863Spraks 3667789Sahrens ZFS_EXIT(zfsvfs); 3668789Sahrens return (error); 3669789Sahrens } 3670789Sahrens 3671789Sahrens /* 3672789Sahrens * zfs_null_putapage() is used when the file system has been force 3673789Sahrens * unmounted. It just drops the pages. 3674789Sahrens */ 3675789Sahrens /* ARGSUSED */ 3676789Sahrens static int 3677789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3678789Sahrens size_t *lenp, int flags, cred_t *cr) 3679789Sahrens { 3680789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3681789Sahrens return (0); 3682789Sahrens } 3683789Sahrens 36842688Smaybee /* 36852688Smaybee * Push a page out to disk, klustering if possible. 36862688Smaybee * 36872688Smaybee * IN: vp - file to push page to. 36882688Smaybee * pp - page to push. 36892688Smaybee * flags - additional flags. 36902688Smaybee * cr - credentials of caller. 36912688Smaybee * 36922688Smaybee * OUT: offp - start of range pushed. 36932688Smaybee * lenp - len of range pushed. 36942688Smaybee * 36952688Smaybee * RETURN: 0 if success 36962688Smaybee * error code if failure 36972688Smaybee * 36982688Smaybee * NOTE: callers must have locked the page to be pushed. On 36992688Smaybee * exit, the page (and all other pages in the kluster) must be 37002688Smaybee * unlocked. 37012688Smaybee */ 3702789Sahrens /* ARGSUSED */ 3703789Sahrens static int 3704789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3705789Sahrens size_t *lenp, int flags, cred_t *cr) 3706789Sahrens { 3707789Sahrens znode_t *zp = VTOZ(vp); 3708789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3709789Sahrens dmu_tx_t *tx; 37102688Smaybee u_offset_t off, koff; 37112688Smaybee size_t len, klen; 37124709Smaybee uint64_t filesz; 3713789Sahrens int err; 3714789Sahrens 37154709Smaybee filesz = zp->z_phys->zp_size; 37162688Smaybee off = pp->p_offset; 37172688Smaybee len = PAGESIZE; 37182688Smaybee /* 37192688Smaybee * If our blocksize is bigger than the page size, try to kluster 37208227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 37212688Smaybee * a read-modify-write). 37222688Smaybee */ 37234709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 37248636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 37258636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 37262688Smaybee ASSERT(koff <= filesz); 37272688Smaybee if (koff + klen > filesz) 37282688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 37292688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 37302688Smaybee } 37312688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 37328636SMark.Maybee@Sun.COM 37331819Smaybee /* 37341819Smaybee * Can't push pages past end-of-file. 37351819Smaybee */ 37364709Smaybee if (off >= filesz) { 37374709Smaybee /* ignore all pages */ 37382688Smaybee err = 0; 37392688Smaybee goto out; 37404709Smaybee } else if (off + len > filesz) { 37414709Smaybee int npages = btopr(filesz - off); 37422688Smaybee page_t *trunc; 37432688Smaybee 37442688Smaybee page_list_break(&pp, &trunc, npages); 37454709Smaybee /* ignore pages past end of file */ 37462688Smaybee if (trunc) 37474709Smaybee pvn_write_done(trunc, flags); 37484709Smaybee len = filesz - off; 37491819Smaybee } 37509396SMatthew.Ahrens@Sun.COM 37519396SMatthew.Ahrens@Sun.COM if (zfs_usergroup_overquota(zfsvfs, B_FALSE, zp->z_phys->zp_uid) || 37529396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_TRUE, zp->z_phys->zp_gid)) { 37539396SMatthew.Ahrens@Sun.COM err = EDQUOT; 37549396SMatthew.Ahrens@Sun.COM goto out; 37559396SMatthew.Ahrens@Sun.COM } 37568636SMark.Maybee@Sun.COM top: 3757789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3758789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3759789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 37608227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 3761789Sahrens if (err != 0) { 37628227SNeil.Perrin@Sun.COM if (err == ERESTART) { 37632113Sahrens dmu_tx_wait(tx); 37642113Sahrens dmu_tx_abort(tx); 3765789Sahrens goto top; 3766789Sahrens } 37672113Sahrens dmu_tx_abort(tx); 3768789Sahrens goto out; 3769789Sahrens } 3770789Sahrens 37712688Smaybee if (zp->z_blksz <= PAGESIZE) { 37727315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 37732688Smaybee ASSERT3U(len, <=, PAGESIZE); 37742688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 37757315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 37762688Smaybee } else { 37772688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 37782688Smaybee } 37792688Smaybee 37802688Smaybee if (err == 0) { 37812688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 37828636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 37832688Smaybee } 37849951SLin.Ling@Sun.COM dmu_tx_commit(tx); 37852688Smaybee 37862688Smaybee out: 37874709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3788789Sahrens if (offp) 3789789Sahrens *offp = off; 3790789Sahrens if (lenp) 3791789Sahrens *lenp = len; 3792789Sahrens 3793789Sahrens return (err); 3794789Sahrens } 3795789Sahrens 3796789Sahrens /* 3797789Sahrens * Copy the portion of the file indicated from pages into the file. 3798789Sahrens * The pages are stored in a page list attached to the files vnode. 3799789Sahrens * 3800789Sahrens * IN: vp - vnode of file to push page data to. 3801789Sahrens * off - position in file to put data. 3802789Sahrens * len - amount of data to write. 3803789Sahrens * flags - flags to control the operation. 3804789Sahrens * cr - credentials of caller. 38055331Samw * ct - caller context. 3806789Sahrens * 3807789Sahrens * RETURN: 0 if success 3808789Sahrens * error code if failure 3809789Sahrens * 3810789Sahrens * Timestamps: 3811789Sahrens * vp - ctime|mtime updated 3812789Sahrens */ 38135331Samw /*ARGSUSED*/ 3814789Sahrens static int 38155331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 38165331Samw caller_context_t *ct) 3817789Sahrens { 3818789Sahrens znode_t *zp = VTOZ(vp); 3819789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3820789Sahrens page_t *pp; 3821789Sahrens size_t io_len; 3822789Sahrens u_offset_t io_off; 38238636SMark.Maybee@Sun.COM uint_t blksz; 38248636SMark.Maybee@Sun.COM rl_t *rl; 3825789Sahrens int error = 0; 3826789Sahrens 38275367Sahrens ZFS_ENTER(zfsvfs); 38285367Sahrens ZFS_VERIFY_ZP(zp); 3829789Sahrens 38308636SMark.Maybee@Sun.COM /* 38318636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 38328636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 38338636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 38348636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 38358636SMark.Maybee@Sun.COM * operations to get narrower locking. 38368636SMark.Maybee@Sun.COM */ 38378636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 38388636SMark.Maybee@Sun.COM if (ISP2(blksz)) 38398636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 38408636SMark.Maybee@Sun.COM else 38418636SMark.Maybee@Sun.COM io_off = 0; 38428636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 38439141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 38448636SMark.Maybee@Sun.COM else 38458636SMark.Maybee@Sun.COM io_len = 0; 38468636SMark.Maybee@Sun.COM 38478636SMark.Maybee@Sun.COM if (io_len == 0) { 3848789Sahrens /* 38498636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 3850789Sahrens */ 38518636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 38528636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 38531472Sperrin goto out; 3854789Sahrens } 38558636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 38568636SMark.Maybee@Sun.COM 38578636SMark.Maybee@Sun.COM if (off > zp->z_phys->zp_size) { 3858789Sahrens /* past end of file */ 38598636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 3860789Sahrens ZFS_EXIT(zfsvfs); 3861789Sahrens return (0); 3862789Sahrens } 3863789Sahrens 38648636SMark.Maybee@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off); 38658636SMark.Maybee@Sun.COM 38668636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 3867789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 38681669Sperrin pp = page_lookup(vp, io_off, 38694339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3870789Sahrens } else { 3871789Sahrens pp = page_lookup_nowait(vp, io_off, 38724339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3873789Sahrens } 3874789Sahrens 3875789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3876789Sahrens int err; 3877789Sahrens 3878789Sahrens /* 3879789Sahrens * Found a dirty page to push 3880789Sahrens */ 38811669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 38821669Sperrin if (err) 3883789Sahrens error = err; 3884789Sahrens } else { 3885789Sahrens io_len = PAGESIZE; 3886789Sahrens } 3887789Sahrens } 38881472Sperrin out: 38898636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 38902638Sperrin if ((flags & B_ASYNC) == 0) 38912638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3892789Sahrens ZFS_EXIT(zfsvfs); 3893789Sahrens return (error); 3894789Sahrens } 3895789Sahrens 38965331Samw /*ARGSUSED*/ 3897789Sahrens void 38985331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3899789Sahrens { 3900789Sahrens znode_t *zp = VTOZ(vp); 3901789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3902789Sahrens int error; 3903789Sahrens 39045326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 39055446Sahrens if (zp->z_dbuf == NULL) { 39065446Sahrens /* 39075642Smaybee * The fs has been unmounted, or we did a 39085642Smaybee * suspend/resume and this file no longer exists. 39095446Sahrens */ 3910789Sahrens if (vn_has_cached_data(vp)) { 3911789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3912789Sahrens B_INVAL, cr); 3913789Sahrens } 3914789Sahrens 39151544Seschrock mutex_enter(&zp->z_lock); 3916789Sahrens vp->v_count = 0; /* count arrives as 1 */ 39175446Sahrens mutex_exit(&zp->z_lock); 39185642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 39195446Sahrens zfs_znode_free(zp); 3920789Sahrens return; 3921789Sahrens } 3922789Sahrens 3923789Sahrens /* 3924789Sahrens * Attempt to push any data in the page cache. If this fails 3925789Sahrens * we will get kicked out later in zfs_zinactive(). 3926789Sahrens */ 39271298Sperrin if (vn_has_cached_data(vp)) { 39281298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 39291298Sperrin cr); 39301298Sperrin } 3931789Sahrens 39323461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3933789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3934789Sahrens 3935789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3936789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3937789Sahrens if (error) { 3938789Sahrens dmu_tx_abort(tx); 3939789Sahrens } else { 3940789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3941789Sahrens mutex_enter(&zp->z_lock); 3942789Sahrens zp->z_atime_dirty = 0; 3943789Sahrens mutex_exit(&zp->z_lock); 3944789Sahrens dmu_tx_commit(tx); 3945789Sahrens } 3946789Sahrens } 3947789Sahrens 3948789Sahrens zfs_zinactive(zp); 39495326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3950789Sahrens } 3951789Sahrens 3952789Sahrens /* 3953789Sahrens * Bounds-check the seek operation. 3954789Sahrens * 3955789Sahrens * IN: vp - vnode seeking within 3956789Sahrens * ooff - old file offset 3957789Sahrens * noffp - pointer to new file offset 39585331Samw * ct - caller context 3959789Sahrens * 3960789Sahrens * RETURN: 0 if success 3961789Sahrens * EINVAL if new offset invalid 3962789Sahrens */ 3963789Sahrens /* ARGSUSED */ 3964789Sahrens static int 39655331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 39665331Samw caller_context_t *ct) 3967789Sahrens { 3968789Sahrens if (vp->v_type == VDIR) 3969789Sahrens return (0); 3970789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3971789Sahrens } 3972789Sahrens 3973789Sahrens /* 3974789Sahrens * Pre-filter the generic locking function to trap attempts to place 3975789Sahrens * a mandatory lock on a memory mapped file. 3976789Sahrens */ 3977789Sahrens static int 3978789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 39795331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 3980789Sahrens { 3981789Sahrens znode_t *zp = VTOZ(vp); 3982789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3983789Sahrens int error; 3984789Sahrens 39855367Sahrens ZFS_ENTER(zfsvfs); 39865367Sahrens ZFS_VERIFY_ZP(zp); 3987789Sahrens 3988789Sahrens /* 39891544Seschrock * We are following the UFS semantics with respect to mapcnt 39901544Seschrock * here: If we see that the file is mapped already, then we will 39911544Seschrock * return an error, but we don't worry about races between this 39921544Seschrock * function and zfs_map(). 3993789Sahrens */ 39941544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3995789Sahrens ZFS_EXIT(zfsvfs); 3996789Sahrens return (EAGAIN); 3997789Sahrens } 39985331Samw error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct); 3999789Sahrens ZFS_EXIT(zfsvfs); 4000789Sahrens return (error); 4001789Sahrens } 4002789Sahrens 4003789Sahrens /* 4004789Sahrens * If we can't find a page in the cache, we will create a new page 4005789Sahrens * and fill it with file data. For efficiency, we may try to fill 40068636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 40079265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 40089265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4009789Sahrens */ 4010789Sahrens static int 4011789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4012789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4013789Sahrens { 4014789Sahrens znode_t *zp = VTOZ(vp); 4015789Sahrens page_t *pp, *cur_pp; 4016789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4017789Sahrens u_offset_t io_off, total; 4018789Sahrens size_t io_len; 4019789Sahrens int err; 4020789Sahrens 40212688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 40228636SMark.Maybee@Sun.COM /* 40238636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 40248636SMark.Maybee@Sun.COM */ 4025789Sahrens io_off = off; 4026789Sahrens io_len = PAGESIZE; 40279265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 40289265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4029789Sahrens } else { 4030789Sahrens /* 40318636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4032789Sahrens */ 4033789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 40348636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4035789Sahrens } 4036789Sahrens if (pp == NULL) { 4037789Sahrens /* 40388636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4039789Sahrens */ 4040789Sahrens *pl = NULL; 4041789Sahrens return (0); 4042789Sahrens } 4043789Sahrens 4044789Sahrens /* 4045789Sahrens * Fill the pages in the kluster. 4046789Sahrens */ 4047789Sahrens cur_pp = pp; 4048789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 40498636SMark.Maybee@Sun.COM caddr_t va; 40508636SMark.Maybee@Sun.COM 40512688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 40527315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 40539512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 40549512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 40557315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4056789Sahrens if (err) { 4057789Sahrens /* On error, toss the entire kluster */ 4058789Sahrens pvn_read_done(pp, B_ERROR); 40597294Sperrin /* convert checksum errors into IO errors */ 40607294Sperrin if (err == ECKSUM) 40617294Sperrin err = EIO; 4062789Sahrens return (err); 4063789Sahrens } 4064789Sahrens cur_pp = cur_pp->p_next; 4065789Sahrens } 40668636SMark.Maybee@Sun.COM 4067789Sahrens /* 40688636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 40698636SMark.Maybee@Sun.COM * from the desired offset `off'. 4070789Sahrens * NOTE: the page list will always be null terminated. 4071789Sahrens */ 4072789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 40738636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4074789Sahrens 4075789Sahrens return (0); 4076789Sahrens } 4077789Sahrens 4078789Sahrens /* 4079789Sahrens * Return pointers to the pages for the file region [off, off + len] 4080789Sahrens * in the pl array. If plsz is greater than len, this function may 40818636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 40828636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 40838636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 40848636SMark.Maybee@Sun.COM * part of a klustered read. 4085789Sahrens * 4086789Sahrens * IN: vp - vnode of file to get data from. 4087789Sahrens * off - position in file to get data from. 4088789Sahrens * len - amount of data to retrieve. 4089789Sahrens * plsz - length of provided page list. 4090789Sahrens * seg - segment to obtain pages for. 4091789Sahrens * addr - virtual address of fault. 4092789Sahrens * rw - mode of created pages. 4093789Sahrens * cr - credentials of caller. 40945331Samw * ct - caller context. 4095789Sahrens * 4096789Sahrens * OUT: protp - protection mode of created pages. 4097789Sahrens * pl - list of pages created. 4098789Sahrens * 4099789Sahrens * RETURN: 0 if success 4100789Sahrens * error code if failure 4101789Sahrens * 4102789Sahrens * Timestamps: 4103789Sahrens * vp - atime updated 4104789Sahrens */ 4105789Sahrens /* ARGSUSED */ 4106789Sahrens static int 4107789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4108789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 41095331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4110789Sahrens { 4111789Sahrens znode_t *zp = VTOZ(vp); 4112789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 41138636SMark.Maybee@Sun.COM page_t **pl0 = pl; 41148636SMark.Maybee@Sun.COM int err = 0; 41158636SMark.Maybee@Sun.COM 41168636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 41178636SMark.Maybee@Sun.COM if (pl == NULL) 41188636SMark.Maybee@Sun.COM return (0); 41198636SMark.Maybee@Sun.COM else if (len > plsz) 41208636SMark.Maybee@Sun.COM len = plsz; 41218681SMark.Maybee@Sun.COM else 41228681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 41238636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4124789Sahrens 41255367Sahrens ZFS_ENTER(zfsvfs); 41265367Sahrens ZFS_VERIFY_ZP(zp); 4127789Sahrens 4128789Sahrens if (protp) 4129789Sahrens *protp = PROT_ALL; 4130789Sahrens 4131789Sahrens /* 41329265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4133789Sahrens * for pages. If we don't find a page, we will need to create 4134789Sahrens * a new page and fill it with data from the file. 4135789Sahrens */ 4136789Sahrens while (len > 0) { 41378636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 41388636SMark.Maybee@Sun.COM *(pl+1) = NULL; 41398636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 41408636SMark.Maybee@Sun.COM goto out; 41418636SMark.Maybee@Sun.COM while (*pl) { 41428636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4143789Sahrens off += PAGESIZE; 4144789Sahrens addr += PAGESIZE; 41458681SMark.Maybee@Sun.COM if (len > 0) { 41468681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 41478636SMark.Maybee@Sun.COM len -= PAGESIZE; 41488681SMark.Maybee@Sun.COM } 41498636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4150789Sahrens plsz -= PAGESIZE; 41518636SMark.Maybee@Sun.COM pl++; 4152789Sahrens } 4153789Sahrens } 4154789Sahrens 4155789Sahrens /* 4156789Sahrens * Fill out the page array with any pages already in the cache. 4157789Sahrens */ 41588636SMark.Maybee@Sun.COM while (plsz > 0 && 41598636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 41608636SMark.Maybee@Sun.COM off += PAGESIZE; 41618636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4162789Sahrens } 4163789Sahrens out: 41642752Sperrin if (err) { 41652752Sperrin /* 41662752Sperrin * Release any pages we have previously locked. 41672752Sperrin */ 41682752Sperrin while (pl > pl0) 41692752Sperrin page_unlock(*--pl); 41708636SMark.Maybee@Sun.COM } else { 41718636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 41722752Sperrin } 41732752Sperrin 4174789Sahrens *pl = NULL; 4175789Sahrens 4176789Sahrens ZFS_EXIT(zfsvfs); 4177789Sahrens return (err); 4178789Sahrens } 4179789Sahrens 41801544Seschrock /* 41811544Seschrock * Request a memory map for a section of a file. This code interacts 41821544Seschrock * with common code and the VM system as follows: 41831544Seschrock * 41841544Seschrock * common code calls mmap(), which ends up in smmap_common() 41851544Seschrock * 41861544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 41871544Seschrock * 41881544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 41891544Seschrock * 41901544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 41911544Seschrock * 41921544Seschrock * zfs_addmap() updates z_mapcnt 41931544Seschrock */ 41945331Samw /*ARGSUSED*/ 4195789Sahrens static int 4196789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 41975331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 41985331Samw caller_context_t *ct) 4199789Sahrens { 4200789Sahrens znode_t *zp = VTOZ(vp); 4201789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4202789Sahrens segvn_crargs_t vn_a; 4203789Sahrens int error; 4204789Sahrens 42055929Smarks ZFS_ENTER(zfsvfs); 42065929Smarks ZFS_VERIFY_ZP(zp); 42075929Smarks 42085331Samw if ((prot & PROT_WRITE) && 42095331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 42105929Smarks ZFS_APPENDONLY))) { 42115929Smarks ZFS_EXIT(zfsvfs); 42125331Samw return (EPERM); 42135929Smarks } 42145929Smarks 42155929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 42165929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 42175929Smarks ZFS_EXIT(zfsvfs); 42185929Smarks return (EACCES); 42195929Smarks } 4220789Sahrens 4221789Sahrens if (vp->v_flag & VNOMAP) { 4222789Sahrens ZFS_EXIT(zfsvfs); 4223789Sahrens return (ENOSYS); 4224789Sahrens } 4225789Sahrens 4226789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4227789Sahrens ZFS_EXIT(zfsvfs); 4228789Sahrens return (ENXIO); 4229789Sahrens } 4230789Sahrens 4231789Sahrens if (vp->v_type != VREG) { 4232789Sahrens ZFS_EXIT(zfsvfs); 4233789Sahrens return (ENODEV); 4234789Sahrens } 4235789Sahrens 4236789Sahrens /* 4237789Sahrens * If file is locked, disallow mapping. 4238789Sahrens */ 42391544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 42401544Seschrock ZFS_EXIT(zfsvfs); 42411544Seschrock return (EAGAIN); 4242789Sahrens } 4243789Sahrens 4244789Sahrens as_rangelock(as); 42456036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 42466036Smec if (error != 0) { 42476036Smec as_rangeunlock(as); 42486036Smec ZFS_EXIT(zfsvfs); 42496036Smec return (error); 4250789Sahrens } 4251789Sahrens 4252789Sahrens vn_a.vp = vp; 4253789Sahrens vn_a.offset = (u_offset_t)off; 4254789Sahrens vn_a.type = flags & MAP_TYPE; 4255789Sahrens vn_a.prot = prot; 4256789Sahrens vn_a.maxprot = maxprot; 4257789Sahrens vn_a.cred = cr; 4258789Sahrens vn_a.amp = NULL; 4259789Sahrens vn_a.flags = flags & ~MAP_TYPE; 42601417Skchow vn_a.szc = 0; 42611417Skchow vn_a.lgrp_mem_policy_flags = 0; 4262789Sahrens 4263789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4264789Sahrens 4265789Sahrens as_rangeunlock(as); 4266789Sahrens ZFS_EXIT(zfsvfs); 4267789Sahrens return (error); 4268789Sahrens } 4269789Sahrens 4270789Sahrens /* ARGSUSED */ 4271789Sahrens static int 4272789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 42735331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 42745331Samw caller_context_t *ct) 4275789Sahrens { 42761544Seschrock uint64_t pages = btopr(len); 42771544Seschrock 42781544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4279789Sahrens return (0); 4280789Sahrens } 4281789Sahrens 42821773Seschrock /* 42831773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 42841773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 42851773Seschrock * detecting when the data was actually modified, we have to resort to 42861773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 42871773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 42881773Seschrock * which by far the most common case: 42891773Seschrock * 42901773Seschrock * open() 42911773Seschrock * mmap() 42921773Seschrock * <modify memory> 42931773Seschrock * munmap() 42941773Seschrock * close() 42951773Seschrock * <time lapse> 42961773Seschrock * putpage() via fsflush 42971773Seschrock * 42981773Seschrock * If we wait until fsflush to come along, we can have a modification time that 42991773Seschrock * is some arbitrary point in the future. In order to prevent this in the 43001773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 43011773Seschrock * torn down. 43021773Seschrock */ 4303789Sahrens /* ARGSUSED */ 4304789Sahrens static int 4305789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 43065331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 43075331Samw caller_context_t *ct) 4308789Sahrens { 43091544Seschrock uint64_t pages = btopr(len); 43101544Seschrock 43111544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 43121544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 43131773Seschrock 43141773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 43151773Seschrock vn_has_cached_data(vp)) 43165331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 43171773Seschrock 4318789Sahrens return (0); 4319789Sahrens } 4320789Sahrens 4321789Sahrens /* 4322789Sahrens * Free or allocate space in a file. Currently, this function only 4323789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4324789Sahrens * misnamed, as its functionality includes the ability to allocate as 4325789Sahrens * well as free space. 4326789Sahrens * 4327789Sahrens * IN: vp - vnode of file to free data in. 4328789Sahrens * cmd - action to take (only F_FREESP supported). 4329789Sahrens * bfp - section of file to free/alloc. 4330789Sahrens * flag - current file open mode flags. 4331789Sahrens * offset - current file offset. 4332789Sahrens * cr - credentials of caller [UNUSED]. 43335331Samw * ct - caller context. 4334789Sahrens * 4335789Sahrens * RETURN: 0 if success 4336789Sahrens * error code if failure 4337789Sahrens * 4338789Sahrens * Timestamps: 4339789Sahrens * vp - ctime|mtime updated 4340789Sahrens */ 4341789Sahrens /* ARGSUSED */ 4342789Sahrens static int 4343789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4344789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4345789Sahrens { 4346789Sahrens znode_t *zp = VTOZ(vp); 4347789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4348789Sahrens uint64_t off, len; 4349789Sahrens int error; 4350789Sahrens 43515367Sahrens ZFS_ENTER(zfsvfs); 43525367Sahrens ZFS_VERIFY_ZP(zp); 4353789Sahrens 4354789Sahrens if (cmd != F_FREESP) { 4355789Sahrens ZFS_EXIT(zfsvfs); 4356789Sahrens return (EINVAL); 4357789Sahrens } 4358789Sahrens 4359789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4360789Sahrens ZFS_EXIT(zfsvfs); 4361789Sahrens return (error); 4362789Sahrens } 4363789Sahrens 4364789Sahrens if (bfp->l_len < 0) { 4365789Sahrens ZFS_EXIT(zfsvfs); 4366789Sahrens return (EINVAL); 4367789Sahrens } 4368789Sahrens 4369789Sahrens off = bfp->l_start; 43701669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 43711878Smaybee 43726992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4373789Sahrens 4374789Sahrens ZFS_EXIT(zfsvfs); 4375789Sahrens return (error); 4376789Sahrens } 4377789Sahrens 43785331Samw /*ARGSUSED*/ 4379789Sahrens static int 43805331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4381789Sahrens { 4382789Sahrens znode_t *zp = VTOZ(vp); 4383789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 43845326Sek110237 uint32_t gen; 4385789Sahrens uint64_t object = zp->z_id; 4386789Sahrens zfid_short_t *zfid; 4387789Sahrens int size, i; 4388789Sahrens 43895367Sahrens ZFS_ENTER(zfsvfs); 43905367Sahrens ZFS_VERIFY_ZP(zp); 43915326Sek110237 gen = (uint32_t)zp->z_gen; 4392789Sahrens 4393789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4394789Sahrens if (fidp->fid_len < size) { 4395789Sahrens fidp->fid_len = size; 43961512Sek110237 ZFS_EXIT(zfsvfs); 4397789Sahrens return (ENOSPC); 4398789Sahrens } 4399789Sahrens 4400789Sahrens zfid = (zfid_short_t *)fidp; 4401789Sahrens 4402789Sahrens zfid->zf_len = size; 4403789Sahrens 4404789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4405789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4406789Sahrens 4407789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4408789Sahrens if (gen == 0) 4409789Sahrens gen = 1; 4410789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4411789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4412789Sahrens 4413789Sahrens if (size == LONG_FID_LEN) { 4414789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4415789Sahrens zfid_long_t *zlfid; 4416789Sahrens 4417789Sahrens zlfid = (zfid_long_t *)fidp; 4418789Sahrens 4419789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4420789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4421789Sahrens 4422789Sahrens /* XXX - this should be the generation number for the objset */ 4423789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4424789Sahrens zlfid->zf_setgen[i] = 0; 4425789Sahrens } 4426789Sahrens 4427789Sahrens ZFS_EXIT(zfsvfs); 4428789Sahrens return (0); 4429789Sahrens } 4430789Sahrens 4431789Sahrens static int 44325331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 44335331Samw caller_context_t *ct) 4434789Sahrens { 4435789Sahrens znode_t *zp, *xzp; 4436789Sahrens zfsvfs_t *zfsvfs; 4437789Sahrens zfs_dirlock_t *dl; 4438789Sahrens int error; 4439789Sahrens 4440789Sahrens switch (cmd) { 4441789Sahrens case _PC_LINK_MAX: 4442789Sahrens *valp = ULONG_MAX; 4443789Sahrens return (0); 4444789Sahrens 4445789Sahrens case _PC_FILESIZEBITS: 4446789Sahrens *valp = 64; 4447789Sahrens return (0); 4448789Sahrens 4449789Sahrens case _PC_XATTR_EXISTS: 4450789Sahrens zp = VTOZ(vp); 4451789Sahrens zfsvfs = zp->z_zfsvfs; 44525367Sahrens ZFS_ENTER(zfsvfs); 44535367Sahrens ZFS_VERIFY_ZP(zp); 4454789Sahrens *valp = 0; 4455789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 44565331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4457789Sahrens if (error == 0) { 4458789Sahrens zfs_dirent_unlock(dl); 4459789Sahrens if (!zfs_dirempty(xzp)) 4460789Sahrens *valp = 1; 4461789Sahrens VN_RELE(ZTOV(xzp)); 4462789Sahrens } else if (error == ENOENT) { 4463789Sahrens /* 4464789Sahrens * If there aren't extended attributes, it's the 4465789Sahrens * same as having zero of them. 4466789Sahrens */ 4467789Sahrens error = 0; 4468789Sahrens } 4469789Sahrens ZFS_EXIT(zfsvfs); 4470789Sahrens return (error); 4471789Sahrens 44725331Samw case _PC_SATTR_ENABLED: 44735331Samw case _PC_SATTR_EXISTS: 44747757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 44755331Samw (vp->v_type == VREG || vp->v_type == VDIR); 44765331Samw return (0); 44775331Samw 44789749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 44799749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 44809749STim.Haley@Sun.COM vp->v_type == VDIR; 44819749STim.Haley@Sun.COM return (0); 44829749STim.Haley@Sun.COM 4483789Sahrens case _PC_ACL_ENABLED: 4484789Sahrens *valp = _ACL_ACE_ENABLED; 4485789Sahrens return (0); 4486789Sahrens 4487789Sahrens case _PC_MIN_HOLE_SIZE: 4488789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4489789Sahrens return (0); 4490789Sahrens 4491789Sahrens default: 44925331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4493789Sahrens } 4494789Sahrens } 4495789Sahrens 4496789Sahrens /*ARGSUSED*/ 4497789Sahrens static int 44985331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 44995331Samw caller_context_t *ct) 4500789Sahrens { 4501789Sahrens znode_t *zp = VTOZ(vp); 4502789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4503789Sahrens int error; 45045331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4505789Sahrens 45065367Sahrens ZFS_ENTER(zfsvfs); 45075367Sahrens ZFS_VERIFY_ZP(zp); 45085331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4509789Sahrens ZFS_EXIT(zfsvfs); 4510789Sahrens 4511789Sahrens return (error); 4512789Sahrens } 4513789Sahrens 4514789Sahrens /*ARGSUSED*/ 4515789Sahrens static int 45165331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 45175331Samw caller_context_t *ct) 4518789Sahrens { 4519789Sahrens znode_t *zp = VTOZ(vp); 4520789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4521789Sahrens int error; 45225331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4523789Sahrens 45245367Sahrens ZFS_ENTER(zfsvfs); 45255367Sahrens ZFS_VERIFY_ZP(zp); 45265331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4527789Sahrens ZFS_EXIT(zfsvfs); 4528789Sahrens return (error); 4529789Sahrens } 4530789Sahrens 4531789Sahrens /* 4532789Sahrens * Predeclare these here so that the compiler assumes that 4533789Sahrens * this is an "old style" function declaration that does 4534789Sahrens * not include arguments => we won't get type mismatch errors 4535789Sahrens * in the initializations that follow. 4536789Sahrens */ 4537789Sahrens static int zfs_inval(); 4538789Sahrens static int zfs_isdir(); 4539789Sahrens 4540789Sahrens static int 4541789Sahrens zfs_inval() 4542789Sahrens { 4543789Sahrens return (EINVAL); 4544789Sahrens } 4545789Sahrens 4546789Sahrens static int 4547789Sahrens zfs_isdir() 4548789Sahrens { 4549789Sahrens return (EISDIR); 4550789Sahrens } 4551789Sahrens /* 4552789Sahrens * Directory vnode operations template 4553789Sahrens */ 4554789Sahrens vnodeops_t *zfs_dvnodeops; 4555789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 45563898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45573898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45583898Srsb VOPNAME_READ, { .error = zfs_isdir }, 45593898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 45603898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45613898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45623898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45633898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45643898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45653898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 45663898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 45673898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 45683898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45693898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 45703898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 45713898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 45723898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 45733898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45743898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45753898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45763898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45773898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45783898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45793898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 45804863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 45813898Srsb NULL, NULL 4582789Sahrens }; 4583789Sahrens 4584789Sahrens /* 4585789Sahrens * Regular file vnode operations template 4586789Sahrens */ 4587789Sahrens vnodeops_t *zfs_fvnodeops; 4588789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 45893898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45903898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45913898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 45923898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 45933898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45943898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45953898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45963898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45973898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45983898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45993898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 46003898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46013898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 46023898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 46033898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 46043898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 46053898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 46063898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 46073898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 46083898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 46093898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 46103898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46113898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 46123898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 46133898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46143898Srsb NULL, NULL 4615789Sahrens }; 4616789Sahrens 4617789Sahrens /* 4618789Sahrens * Symbolic link vnode operations template 4619789Sahrens */ 4620789Sahrens vnodeops_t *zfs_symvnodeops; 4621789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 46223898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46233898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 46243898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 46253898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 46263898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 46273898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46283898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 46293898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46303898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46313898Srsb NULL, NULL 4632789Sahrens }; 4633789Sahrens 4634789Sahrens /* 46358845Samw@Sun.COM * special share hidden files vnode operations template 46368845Samw@Sun.COM */ 46378845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 46388845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 46398845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46408845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 46418845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46428845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 46438845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46448845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 46458845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 46468845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46478845Samw@Sun.COM NULL, NULL 46488845Samw@Sun.COM }; 46498845Samw@Sun.COM 46508845Samw@Sun.COM /* 4651789Sahrens * Extended attribute directory vnode operations template 4652789Sahrens * This template is identical to the directory vnodes 4653789Sahrens * operation template except for restricted operations: 4654789Sahrens * VOP_MKDIR() 4655789Sahrens * VOP_SYMLINK() 4656789Sahrens * Note that there are other restrictions embedded in: 4657789Sahrens * zfs_create() - restrict type to VREG 4658789Sahrens * zfs_link() - no links into/out of attribute space 4659789Sahrens * zfs_rename() - no moves into/out of attribute space 4660789Sahrens */ 4661789Sahrens vnodeops_t *zfs_xdvnodeops; 4662789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 46633898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 46643898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 46653898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 46663898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46673898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 46683898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 46693898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 46703898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 46713898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 46723898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 46733898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 46743898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 46753898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 46763898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 46773898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 46783898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 46793898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46803898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 46813898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 46823898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46833898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 46843898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 46853898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46863898Srsb NULL, NULL 4687789Sahrens }; 4688789Sahrens 4689789Sahrens /* 4690789Sahrens * Error vnode operations template 4691789Sahrens */ 4692789Sahrens vnodeops_t *zfs_evnodeops; 4693789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 46943898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46953898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46963898Srsb NULL, NULL 4697789Sahrens }; 4698