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 85810209SMark.Musante@Sun.COM #ifdef DEBUG 85910209SMark.Musante@Sun.COM static int zil_fault_io = 0; 86010209SMark.Musante@Sun.COM #endif 86110209SMark.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; 94310209SMark.Musante@Sun.COM #ifdef DEBUG 94410209SMark.Musante@Sun.COM if (zil_fault_io) { 94510209SMark.Musante@Sun.COM error = EIO; 94610209SMark.Musante@Sun.COM zil_fault_io = 0; 94710209SMark.Musante@Sun.COM } else { 94810209SMark.Musante@Sun.COM error = dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db); 94910209SMark.Musante@Sun.COM } 95010209SMark.Musante@Sun.COM #else 95110209SMark.Musante@Sun.COM error = dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db); 95210209SMark.Musante@Sun.COM #endif 95310209SMark.Musante@Sun.COM if (error != 0) { 95410209SMark.Musante@Sun.COM kmem_free(zgd, sizeof (zgd_t)); 95510209SMark.Musante@Sun.COM goto out; 95610209SMark.Musante@Sun.COM } 95710209SMark.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 } 2373*10793Sdai.ngo@sun.com 2374*10793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2375*10793Sdai.ngo@sun.com xoap->xoa_reparse = 2376*10793Sdai.ngo@sun.com ((pzp->zp_flags & ZFS_REPARSE) != 0); 2377*10793Sdai.ngo@sun.com XVA_SET_RTN(xvap, XAT_REPARSE); 2378*10793Sdai.ngo@sun.com } 23795331Samw } 23805331Samw 2381789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2382789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2383789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2384789Sahrens 2385789Sahrens mutex_exit(&zp->z_lock); 2386789Sahrens 2387789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2388789Sahrens 2389789Sahrens if (zp->z_blksz == 0) { 2390789Sahrens /* 2391789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2392789Sahrens */ 2393789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2394789Sahrens } 2395789Sahrens 2396789Sahrens ZFS_EXIT(zfsvfs); 2397789Sahrens return (0); 2398789Sahrens } 2399789Sahrens 2400789Sahrens /* 2401789Sahrens * Set the file attributes to the values contained in the 2402789Sahrens * vattr structure. 2403789Sahrens * 2404789Sahrens * IN: vp - vnode of file to be modified. 2405789Sahrens * vap - new attribute values. 24065331Samw * If AT_XVATTR set, then optional attrs are being set 2407789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 24085331Samw * - ATTR_NOACLCHECK (CIFS context only). 2409789Sahrens * cr - credentials of caller. 24105331Samw * ct - caller context 2411789Sahrens * 2412789Sahrens * RETURN: 0 if success 2413789Sahrens * error code if failure 2414789Sahrens * 2415789Sahrens * Timestamps: 2416789Sahrens * vp - ctime updated, mtime updated if size changed. 2417789Sahrens */ 2418789Sahrens /* ARGSUSED */ 2419789Sahrens static int 2420789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2421789Sahrens caller_context_t *ct) 2422789Sahrens { 24235326Sek110237 znode_t *zp = VTOZ(vp); 24245326Sek110237 znode_phys_t *pzp; 2425789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 24265326Sek110237 zilog_t *zilog; 2427789Sahrens dmu_tx_t *tx; 24281878Smaybee vattr_t oldva; 24298190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2430789Sahrens uint_t mask = vap->va_mask; 24311878Smaybee uint_t saved_mask; 24322796Smarks int trim_mask = 0; 2433789Sahrens uint64_t new_mode; 24349179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 24351231Smarks znode_t *attrzp; 2436789Sahrens int need_policy = FALSE; 2437789Sahrens int err; 24385331Samw zfs_fuid_info_t *fuidp = NULL; 24395331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 24405331Samw xoptattr_t *xoap; 24415824Smarks zfs_acl_t *aclp = NULL; 24425331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 24439179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 2444789Sahrens 2445789Sahrens if (mask == 0) 2446789Sahrens return (0); 2447789Sahrens 2448789Sahrens if (mask & AT_NOSET) 2449789Sahrens return (EINVAL); 2450789Sahrens 24515367Sahrens ZFS_ENTER(zfsvfs); 24525367Sahrens ZFS_VERIFY_ZP(zp); 24535331Samw 24545331Samw pzp = zp->z_phys; 24555331Samw zilog = zfsvfs->z_log; 24565331Samw 24575331Samw /* 24585331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 24595331Samw * that file system is at proper version level 24605331Samw */ 24615331Samw 24625331Samw if (zfsvfs->z_use_fuids == B_FALSE && 24635331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 24645331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 24655386Stimh (mask & AT_XVATTR))) { 24665386Stimh ZFS_EXIT(zfsvfs); 24675331Samw return (EINVAL); 24685386Stimh } 24695386Stimh 24705386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 24715386Stimh ZFS_EXIT(zfsvfs); 2472789Sahrens return (EISDIR); 24735386Stimh } 24745386Stimh 24755386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 24765386Stimh ZFS_EXIT(zfsvfs); 24771308Smarks return (EINVAL); 24785386Stimh } 24791308Smarks 24805331Samw /* 24815331Samw * If this is an xvattr_t, then get a pointer to the structure of 24825331Samw * optional attributes. If this is NULL, then we have a vattr_t. 24835331Samw */ 24845331Samw xoap = xva_getxoptattr(xvap); 24855331Samw 24868190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 24878190SMark.Shellenbaum@Sun.COM 24885331Samw /* 24895331Samw * Immutable files can only alter immutable bit and atime 24905331Samw */ 24915331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 24925331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 24935386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 24945386Stimh ZFS_EXIT(zfsvfs); 24955331Samw return (EPERM); 24965386Stimh } 24975386Stimh 24985386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 24995386Stimh ZFS_EXIT(zfsvfs); 25005331Samw return (EPERM); 25015386Stimh } 2502789Sahrens 25036064Smarks /* 25046064Smarks * Verify timestamps doesn't overflow 32 bits. 25056064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 25066064Smarks * handle times greater than 2039. This check should be removed 25076064Smarks * once large timestamps are fully supported. 25086064Smarks */ 25096064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 25106064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 25116064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 25126064Smarks ZFS_EXIT(zfsvfs); 25136064Smarks return (EOVERFLOW); 25146064Smarks } 25156064Smarks } 25166064Smarks 2517789Sahrens top: 25181231Smarks attrzp = NULL; 2519789Sahrens 25209981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2521789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2522789Sahrens ZFS_EXIT(zfsvfs); 2523789Sahrens return (EROFS); 2524789Sahrens } 2525789Sahrens 2526789Sahrens /* 2527789Sahrens * First validate permissions 2528789Sahrens */ 2529789Sahrens 2530789Sahrens if (mask & AT_SIZE) { 25315331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2532789Sahrens if (err) { 2533789Sahrens ZFS_EXIT(zfsvfs); 2534789Sahrens return (err); 2535789Sahrens } 25361878Smaybee /* 25371878Smaybee * XXX - Note, we are not providing any open 25381878Smaybee * mode flags here (like FNDELAY), so we may 25391878Smaybee * block if there are locks present... this 25401878Smaybee * should be addressed in openat(). 25411878Smaybee */ 25426992Smaybee /* XXX - would it be OK to generate a log record here? */ 25436992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 25441878Smaybee if (err) { 25451878Smaybee ZFS_EXIT(zfsvfs); 25461878Smaybee return (err); 25471878Smaybee } 2548789Sahrens } 2549789Sahrens 25505331Samw if (mask & (AT_ATIME|AT_MTIME) || 25515331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 25525331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 25535331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 25545331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 25555331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 25565331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 25575331Samw skipaclchk, cr); 2558789Sahrens 2559789Sahrens if (mask & (AT_UID|AT_GID)) { 2560789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2561789Sahrens int take_owner; 2562789Sahrens int take_group; 2563789Sahrens 2564789Sahrens /* 2565913Smarks * NOTE: even if a new mode is being set, 2566913Smarks * we may clear S_ISUID/S_ISGID bits. 2567913Smarks */ 2568913Smarks 2569913Smarks if (!(mask & AT_MODE)) 2570913Smarks vap->va_mode = pzp->zp_mode; 2571913Smarks 2572913Smarks /* 2573789Sahrens * Take ownership or chgrp to group we are a member of 2574789Sahrens */ 2575789Sahrens 2576789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 25775331Samw take_group = (mask & AT_GID) && 25785331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2579789Sahrens 2580789Sahrens /* 2581789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2582789Sahrens * take_group must both be set in order to allow taking 2583789Sahrens * ownership. 2584789Sahrens * 2585789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2586789Sahrens * 2587789Sahrens */ 2588789Sahrens 2589789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2590789Sahrens ((idmask == AT_UID) && take_owner) || 2591789Sahrens ((idmask == AT_GID) && take_group)) { 25925331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 25935331Samw skipaclchk, cr) == 0) { 2594789Sahrens /* 2595789Sahrens * Remove setuid/setgid for non-privileged users 2596789Sahrens */ 25971115Smarks secpolicy_setid_clear(vap, cr); 25982796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2599789Sahrens } else { 2600789Sahrens need_policy = TRUE; 2601789Sahrens } 2602789Sahrens } else { 2603789Sahrens need_policy = TRUE; 2604789Sahrens } 2605789Sahrens } 2606789Sahrens 26072796Smarks mutex_enter(&zp->z_lock); 26082796Smarks oldva.va_mode = pzp->zp_mode; 26095771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 26105331Samw if (mask & AT_XVATTR) { 26118190SMark.Shellenbaum@Sun.COM /* 26128190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 26138190SMark.Shellenbaum@Sun.COM * that are actually changing. 26148190SMark.Shellenbaum@Sun.COM * 26158190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 26168190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 26178190SMark.Shellenbaum@Sun.COM */ 26188190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 26198190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 26208190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) { 26218190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26228190SMark.Shellenbaum@Sun.COM } else { 26238190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 26248190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 26258190SMark.Shellenbaum@Sun.COM } 26268190SMark.Shellenbaum@Sun.COM } 26278190SMark.Shellenbaum@Sun.COM 26288190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 26298190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 26308190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) { 26318190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26328190SMark.Shellenbaum@Sun.COM } else { 26338190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 26348190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 26358190SMark.Shellenbaum@Sun.COM } 26368190SMark.Shellenbaum@Sun.COM } 26378190SMark.Shellenbaum@Sun.COM 26388190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 26398190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 26408190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) { 26418190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26428190SMark.Shellenbaum@Sun.COM } else { 26438190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 26448190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 26458190SMark.Shellenbaum@Sun.COM } 26468190SMark.Shellenbaum@Sun.COM } 26478190SMark.Shellenbaum@Sun.COM 26488190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 26498190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 26508190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NODUMP) != 0)) { 26518190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26528190SMark.Shellenbaum@Sun.COM } else { 26538190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 26548190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 26558190SMark.Shellenbaum@Sun.COM } 26568190SMark.Shellenbaum@Sun.COM } 26578190SMark.Shellenbaum@Sun.COM 26588190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 26598190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 26608190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) { 26618190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26628190SMark.Shellenbaum@Sun.COM } else { 26638190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 26648190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 26658190SMark.Shellenbaum@Sun.COM } 26668190SMark.Shellenbaum@Sun.COM } 26678190SMark.Shellenbaum@Sun.COM 26688190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 26698190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 26708190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 26718190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 26728190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) { 26738190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26748190SMark.Shellenbaum@Sun.COM } else { 26758190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 26768190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 26778190SMark.Shellenbaum@Sun.COM } 26788190SMark.Shellenbaum@Sun.COM } 26798190SMark.Shellenbaum@Sun.COM 2680*10793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 2681*10793Sdai.ngo@sun.com mutex_exit(&zp->z_lock); 2682*10793Sdai.ngo@sun.com ZFS_EXIT(zfsvfs); 2683*10793Sdai.ngo@sun.com return (EPERM); 2684*10793Sdai.ngo@sun.com } 2685*10793Sdai.ngo@sun.com 26868190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 26878190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 26888190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 26895331Samw need_policy = TRUE; 26905331Samw } 26915331Samw } 26925331Samw 26932796Smarks mutex_exit(&zp->z_lock); 26942796Smarks 26952796Smarks if (mask & AT_MODE) { 26965331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 26972796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 26982796Smarks &oldva, cr); 26992796Smarks if (err) { 27002796Smarks ZFS_EXIT(zfsvfs); 27012796Smarks return (err); 27022796Smarks } 27032796Smarks trim_mask |= AT_MODE; 27042796Smarks } else { 27052796Smarks need_policy = TRUE; 27062796Smarks } 27072796Smarks } 2708789Sahrens 2709789Sahrens if (need_policy) { 27101115Smarks /* 27111115Smarks * If trim_mask is set then take ownership 27122796Smarks * has been granted or write_acl is present and user 27132796Smarks * has the ability to modify mode. In that case remove 27142796Smarks * UID|GID and or MODE from mask so that 27151115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 27161115Smarks */ 27172796Smarks 27182796Smarks if (trim_mask) { 27192796Smarks saved_mask = vap->va_mask; 27202796Smarks vap->va_mask &= ~trim_mask; 27212796Smarks } 2722789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 27235331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2724789Sahrens if (err) { 2725789Sahrens ZFS_EXIT(zfsvfs); 2726789Sahrens return (err); 2727789Sahrens } 27281115Smarks 27291115Smarks if (trim_mask) 27302796Smarks vap->va_mask |= saved_mask; 2731789Sahrens } 2732789Sahrens 2733789Sahrens /* 2734789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2735789Sahrens * changed va_mask 2736789Sahrens */ 2737789Sahrens mask = vap->va_mask; 2738789Sahrens 2739789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2740789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2741789Sahrens 2742789Sahrens if (mask & AT_MODE) { 27431576Smarks uint64_t pmode = pzp->zp_mode; 27441576Smarks 27451576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2746789Sahrens 27479396SMatthew.Ahrens@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 27489396SMatthew.Ahrens@Sun.COM goto out; 27495331Samw if (pzp->zp_acl.z_acl_extern_obj) { 27505331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 27515331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 27525331Samw pzp->zp_acl.z_acl_version == 27535331Samw ZFS_ACL_VERSION_INITIAL) { 27545331Samw dmu_tx_hold_free(tx, 27555331Samw pzp->zp_acl.z_acl_extern_obj, 0, 27565331Samw DMU_OBJECT_END); 27575331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 27585824Smarks 0, aclp->z_acl_bytes); 27595331Samw } else { 27605331Samw dmu_tx_hold_write(tx, 27615331Samw pzp->zp_acl.z_acl_extern_obj, 0, 27625824Smarks aclp->z_acl_bytes); 27635331Samw } 27646180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 27656180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 27666180Smarks 0, aclp->z_acl_bytes); 27675331Samw } 2768789Sahrens } 2769789Sahrens 27709179SMark.Shellenbaum@Sun.COM if (mask & (AT_UID | AT_GID)) { 27719179SMark.Shellenbaum@Sun.COM if (pzp->zp_xattr) { 27729179SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 27739396SMatthew.Ahrens@Sun.COM if (err) 27749396SMatthew.Ahrens@Sun.COM goto out; 27759179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, attrzp->z_id); 27769179SMark.Shellenbaum@Sun.COM } 27779179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 27789179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 27799179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 27809396SMatthew.Ahrens@Sun.COM if (new_uid != pzp->zp_uid && 27819396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) { 27829396SMatthew.Ahrens@Sun.COM err = EDQUOT; 27839396SMatthew.Ahrens@Sun.COM goto out; 27849396SMatthew.Ahrens@Sun.COM } 27851231Smarks } 27869396SMatthew.Ahrens@Sun.COM 27879179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 27889179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 27899179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 27909396SMatthew.Ahrens@Sun.COM if (new_gid != pzp->zp_gid && 27919396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) { 27929396SMatthew.Ahrens@Sun.COM err = EDQUOT; 27939396SMatthew.Ahrens@Sun.COM goto out; 27949396SMatthew.Ahrens@Sun.COM } 27959179SMark.Shellenbaum@Sun.COM } 27969179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 27979179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 27989179SMark.Shellenbaum@Sun.COM if (zfsvfs->z_fuid_obj == 0) { 27999179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 28009179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 28019179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 28029179SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 28039179SMark.Shellenbaum@Sun.COM FALSE, NULL); 28049179SMark.Shellenbaum@Sun.COM } else { 28059179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 28069179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 28079179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 28089179SMark.Shellenbaum@Sun.COM } 28099179SMark.Shellenbaum@Sun.COM } 28101231Smarks } 28111231Smarks 28128227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2813789Sahrens if (err) { 28149396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 28152113Sahrens dmu_tx_wait(tx); 28169396SMatthew.Ahrens@Sun.COM goto out; 2817789Sahrens } 2818789Sahrens 2819789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2820789Sahrens 2821789Sahrens /* 2822789Sahrens * Set each attribute requested. 2823789Sahrens * We group settings according to the locks they need to acquire. 2824789Sahrens * 2825789Sahrens * Note: you cannot set ctime directly, although it will be 2826789Sahrens * updated as a side-effect of calling this function. 2827789Sahrens */ 2828789Sahrens 2829789Sahrens mutex_enter(&zp->z_lock); 2830789Sahrens 2831789Sahrens if (mask & AT_MODE) { 28325824Smarks mutex_enter(&zp->z_acl_lock); 28335824Smarks zp->z_phys->zp_mode = new_mode; 28349179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 2835789Sahrens ASSERT3U(err, ==, 0); 283610143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 283710143STim.Haley@Sun.COM aclp = NULL; 28385824Smarks mutex_exit(&zp->z_acl_lock); 2839789Sahrens } 2840789Sahrens 28411231Smarks if (attrzp) 28421231Smarks mutex_enter(&attrzp->z_lock); 28431231Smarks 28441231Smarks if (mask & AT_UID) { 28459179SMark.Shellenbaum@Sun.COM pzp->zp_uid = new_uid; 28469179SMark.Shellenbaum@Sun.COM if (attrzp) 28479179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_uid = new_uid; 28481231Smarks } 28491231Smarks 28501231Smarks if (mask & AT_GID) { 28519179SMark.Shellenbaum@Sun.COM pzp->zp_gid = new_gid; 28521231Smarks if (attrzp) 28539179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_gid = new_gid; 28541231Smarks } 28551231Smarks 28561231Smarks if (attrzp) 28571231Smarks mutex_exit(&attrzp->z_lock); 2858789Sahrens 2859789Sahrens if (mask & AT_ATIME) 2860789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2861789Sahrens 2862789Sahrens if (mask & AT_MTIME) 2863789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2864789Sahrens 28656992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 28661878Smaybee if (mask & AT_SIZE) 2867789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 28681878Smaybee else if (mask != 0) 2869789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 28705331Samw /* 28715331Samw * Do this after setting timestamps to prevent timestamp 28725331Samw * update from toggling bit 28735331Samw */ 28745331Samw 28755331Samw if (xoap && (mask & AT_XVATTR)) { 28768190SMark.Shellenbaum@Sun.COM 28778190SMark.Shellenbaum@Sun.COM /* 28788190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 28798190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 28808190SMark.Shellenbaum@Sun.COM */ 28818190SMark.Shellenbaum@Sun.COM 28828190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 28838190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 28848190SMark.Shellenbaum@Sun.COM } 28858190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 28868190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 28878190SMark.Shellenbaum@Sun.COM } 28888190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 28898190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 28908190SMark.Shellenbaum@Sun.COM } 28918190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 28928190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 28938190SMark.Shellenbaum@Sun.COM } 28948190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 28958190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 28968190SMark.Shellenbaum@Sun.COM } 28978190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 28988190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 28998190SMark.Shellenbaum@Sun.COM } 29008190SMark.Shellenbaum@Sun.COM 29015331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 29025331Samw size_t len; 29035331Samw dmu_object_info_t doi; 29045331Samw 29055331Samw ASSERT(vp->v_type == VREG); 29065331Samw 29075331Samw /* Grow the bonus buffer if necessary. */ 29085331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 29095331Samw len = sizeof (xoap->xoa_av_scanstamp) + 29105331Samw sizeof (znode_phys_t); 29115331Samw if (len > doi.doi_bonus_size) 29125331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 29135331Samw } 29145331Samw zfs_xvattr_set(zp, xvap); 29155331Samw } 2916789Sahrens 29179179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 29189179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 29199179SMark.Shellenbaum@Sun.COM 29201878Smaybee if (mask != 0) 29215331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 29225331Samw 2923789Sahrens mutex_exit(&zp->z_lock); 2924789Sahrens 29259396SMatthew.Ahrens@Sun.COM out: 29261231Smarks if (attrzp) 29271231Smarks VN_RELE(ZTOV(attrzp)); 29281231Smarks 292910143STim.Haley@Sun.COM if (aclp) 293010143STim.Haley@Sun.COM zfs_acl_free(aclp); 293110143STim.Haley@Sun.COM 29329396SMatthew.Ahrens@Sun.COM if (fuidp) { 29339396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 29349396SMatthew.Ahrens@Sun.COM fuidp = NULL; 29359396SMatthew.Ahrens@Sun.COM } 29369396SMatthew.Ahrens@Sun.COM 29379396SMatthew.Ahrens@Sun.COM if (err) 29389396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 29399396SMatthew.Ahrens@Sun.COM else 29409396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 29419396SMatthew.Ahrens@Sun.COM 29429396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 29439396SMatthew.Ahrens@Sun.COM goto top; 2944789Sahrens 2945789Sahrens ZFS_EXIT(zfsvfs); 2946789Sahrens return (err); 2947789Sahrens } 2948789Sahrens 29493271Smaybee typedef struct zfs_zlock { 29503271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 29513271Smaybee znode_t *zl_znode; /* znode we held */ 29523271Smaybee struct zfs_zlock *zl_next; /* next in list */ 29533271Smaybee } zfs_zlock_t; 29543271Smaybee 29553271Smaybee /* 29563271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 29573271Smaybee */ 29583271Smaybee static void 29593271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 29603271Smaybee { 29613271Smaybee zfs_zlock_t *zl; 29623271Smaybee 29633271Smaybee while ((zl = *zlpp) != NULL) { 29643271Smaybee if (zl->zl_znode != NULL) 29653271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 29663271Smaybee rw_exit(zl->zl_rwlock); 29673271Smaybee *zlpp = zl->zl_next; 29683271Smaybee kmem_free(zl, sizeof (*zl)); 29693271Smaybee } 29703271Smaybee } 29713271Smaybee 2972789Sahrens /* 2973789Sahrens * Search back through the directory tree, using the ".." entries. 2974789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2975789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2976789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2977789Sahrens */ 2978789Sahrens static int 2979789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2980789Sahrens { 2981789Sahrens zfs_zlock_t *zl; 29823638Sbillm znode_t *zp = tdzp; 2983789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2984789Sahrens uint64_t *oidp = &zp->z_id; 2985789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2986789Sahrens krw_t rw = RW_WRITER; 2987789Sahrens 2988789Sahrens /* 2989789Sahrens * First pass write-locks szp and compares to zp->z_id. 2990789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2991789Sahrens */ 2992789Sahrens do { 29933271Smaybee if (!rw_tryenter(rwlp, rw)) { 29943271Smaybee /* 29953271Smaybee * Another thread is renaming in this path. 29963271Smaybee * Note that if we are a WRITER, we don't have any 29973271Smaybee * parent_locks held yet. 29983271Smaybee */ 29993271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 30003271Smaybee /* 30013271Smaybee * Drop our locks and restart 30023271Smaybee */ 30033271Smaybee zfs_rename_unlock(&zl); 30043271Smaybee *zlpp = NULL; 30053271Smaybee zp = tdzp; 30063271Smaybee oidp = &zp->z_id; 30073271Smaybee rwlp = &szp->z_parent_lock; 30083271Smaybee rw = RW_WRITER; 30093271Smaybee continue; 30103271Smaybee } else { 30113271Smaybee /* 30123271Smaybee * Wait for other thread to drop its locks 30133271Smaybee */ 30143271Smaybee rw_enter(rwlp, rw); 30153271Smaybee } 30163271Smaybee } 30173271Smaybee 3018789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3019789Sahrens zl->zl_rwlock = rwlp; 3020789Sahrens zl->zl_znode = NULL; 3021789Sahrens zl->zl_next = *zlpp; 3022789Sahrens *zlpp = zl; 3023789Sahrens 3024789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 3025789Sahrens return (EINVAL); 3026789Sahrens 3027789Sahrens if (*oidp == rootid) /* We've hit the top */ 3028789Sahrens return (0); 3029789Sahrens 3030789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 3031789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 3032789Sahrens if (error) 3033789Sahrens return (error); 3034789Sahrens zl->zl_znode = zp; 3035789Sahrens } 3036789Sahrens oidp = &zp->z_phys->zp_parent; 3037789Sahrens rwlp = &zp->z_parent_lock; 3038789Sahrens rw = RW_READER; 3039789Sahrens 3040789Sahrens } while (zp->z_id != sdzp->z_id); 3041789Sahrens 3042789Sahrens return (0); 3043789Sahrens } 3044789Sahrens 3045789Sahrens /* 3046789Sahrens * Move an entry from the provided source directory to the target 3047789Sahrens * directory. Change the entry name as indicated. 3048789Sahrens * 3049789Sahrens * IN: sdvp - Source directory containing the "old entry". 3050789Sahrens * snm - Old entry name. 3051789Sahrens * tdvp - Target directory to contain the "new entry". 3052789Sahrens * tnm - New entry name. 3053789Sahrens * cr - credentials of caller. 30545331Samw * ct - caller context 30555331Samw * flags - case flags 3056789Sahrens * 3057789Sahrens * RETURN: 0 if success 3058789Sahrens * error code if failure 3059789Sahrens * 3060789Sahrens * Timestamps: 3061789Sahrens * sdvp,tdvp - ctime|mtime updated 3062789Sahrens */ 30635331Samw /*ARGSUSED*/ 3064789Sahrens static int 30655331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 30665331Samw caller_context_t *ct, int flags) 3067789Sahrens { 3068789Sahrens znode_t *tdzp, *szp, *tzp; 3069789Sahrens znode_t *sdzp = VTOZ(sdvp); 3070789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 30715326Sek110237 zilog_t *zilog; 3072789Sahrens vnode_t *realvp; 3073789Sahrens zfs_dirlock_t *sdl, *tdl; 3074789Sahrens dmu_tx_t *tx; 3075789Sahrens zfs_zlock_t *zl; 30765331Samw int cmp, serr, terr; 30775331Samw int error = 0; 30785331Samw int zflg = 0; 3079789Sahrens 30805367Sahrens ZFS_ENTER(zfsvfs); 30815367Sahrens ZFS_VERIFY_ZP(sdzp); 30825326Sek110237 zilog = zfsvfs->z_log; 3083789Sahrens 3084789Sahrens /* 3085789Sahrens * Make sure we have the real vp for the target directory. 3086789Sahrens */ 30875331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3088789Sahrens tdvp = realvp; 3089789Sahrens 3090789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 3091789Sahrens ZFS_EXIT(zfsvfs); 3092789Sahrens return (EXDEV); 3093789Sahrens } 3094789Sahrens 3095789Sahrens tdzp = VTOZ(tdvp); 30965367Sahrens ZFS_VERIFY_ZP(tdzp); 30975498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 30985331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 30995331Samw ZFS_EXIT(zfsvfs); 31005331Samw return (EILSEQ); 31015331Samw } 31025331Samw 31035331Samw if (flags & FIGNORECASE) 31045331Samw zflg |= ZCILOOK; 31055331Samw 3106789Sahrens top: 3107789Sahrens szp = NULL; 3108789Sahrens tzp = NULL; 3109789Sahrens zl = NULL; 3110789Sahrens 3111789Sahrens /* 3112789Sahrens * This is to prevent the creation of links into attribute space 3113789Sahrens * by renaming a linked file into/outof an attribute directory. 3114789Sahrens * See the comment in zfs_link() for why this is considered bad. 3115789Sahrens */ 3116789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 3117789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 3118789Sahrens ZFS_EXIT(zfsvfs); 3119789Sahrens return (EINVAL); 3120789Sahrens } 3121789Sahrens 3122789Sahrens /* 3123789Sahrens * Lock source and target directory entries. To prevent deadlock, 3124789Sahrens * a lock ordering must be defined. We lock the directory with 3125789Sahrens * the smallest object id first, or if it's a tie, the one with 3126789Sahrens * the lexically first name. 3127789Sahrens */ 3128789Sahrens if (sdzp->z_id < tdzp->z_id) { 3129789Sahrens cmp = -1; 3130789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3131789Sahrens cmp = 1; 3132789Sahrens } else { 31335331Samw /* 31345331Samw * First compare the two name arguments without 31355331Samw * considering any case folding. 31365331Samw */ 31375331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 31385331Samw 31395331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 31405498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3141789Sahrens if (cmp == 0) { 3142789Sahrens /* 3143789Sahrens * POSIX: "If the old argument and the new argument 3144789Sahrens * both refer to links to the same existing file, 3145789Sahrens * the rename() function shall return successfully 3146789Sahrens * and perform no other action." 3147789Sahrens */ 3148789Sahrens ZFS_EXIT(zfsvfs); 3149789Sahrens return (0); 3150789Sahrens } 31515331Samw /* 31525331Samw * If the file system is case-folding, then we may 31535331Samw * have some more checking to do. A case-folding file 31545331Samw * system is either supporting mixed case sensitivity 31555331Samw * access or is completely case-insensitive. Note 31565331Samw * that the file system is always case preserving. 31575331Samw * 31585331Samw * In mixed sensitivity mode case sensitive behavior 31595331Samw * is the default. FIGNORECASE must be used to 31605331Samw * explicitly request case insensitive behavior. 31615331Samw * 31625331Samw * If the source and target names provided differ only 31635331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 31645331Samw * we will treat this as a special case in the 31655331Samw * case-insensitive mode: as long as the source name 31665331Samw * is an exact match, we will allow this to proceed as 31675331Samw * a name-change request. 31685331Samw */ 31695498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 31705498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 31715498Stimh flags & FIGNORECASE)) && 31725331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 31735331Samw &error) == 0) { 31745331Samw /* 31755331Samw * case preserving rename request, require exact 31765331Samw * name matches 31775331Samw */ 31785331Samw zflg |= ZCIEXACT; 31795331Samw zflg &= ~ZCILOOK; 31805331Samw } 3181789Sahrens } 31825331Samw 3183789Sahrens if (cmp < 0) { 31845331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 31855331Samw ZEXISTS | zflg, NULL, NULL); 31865331Samw terr = zfs_dirent_lock(&tdl, 31875331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3188789Sahrens } else { 31895331Samw terr = zfs_dirent_lock(&tdl, 31905331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 31915331Samw serr = zfs_dirent_lock(&sdl, 31925331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 31935331Samw NULL, NULL); 3194789Sahrens } 3195789Sahrens 3196789Sahrens if (serr) { 3197789Sahrens /* 3198789Sahrens * Source entry invalid or not there. 3199789Sahrens */ 3200789Sahrens if (!terr) { 3201789Sahrens zfs_dirent_unlock(tdl); 3202789Sahrens if (tzp) 3203789Sahrens VN_RELE(ZTOV(tzp)); 3204789Sahrens } 3205789Sahrens if (strcmp(snm, "..") == 0) 3206789Sahrens serr = EINVAL; 3207789Sahrens ZFS_EXIT(zfsvfs); 3208789Sahrens return (serr); 3209789Sahrens } 3210789Sahrens if (terr) { 3211789Sahrens zfs_dirent_unlock(sdl); 3212789Sahrens VN_RELE(ZTOV(szp)); 3213789Sahrens if (strcmp(tnm, "..") == 0) 3214789Sahrens terr = EINVAL; 3215789Sahrens ZFS_EXIT(zfsvfs); 3216789Sahrens return (terr); 3217789Sahrens } 3218789Sahrens 3219789Sahrens /* 3220789Sahrens * Must have write access at the source to remove the old entry 3221789Sahrens * and write access at the target to create the new entry. 3222789Sahrens * Note that if target and source are the same, this can be 3223789Sahrens * done in a single check. 3224789Sahrens */ 3225789Sahrens 3226789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3227789Sahrens goto out; 3228789Sahrens 3229789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3230789Sahrens /* 3231789Sahrens * Check to make sure rename is valid. 3232789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3233789Sahrens */ 3234789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3235789Sahrens goto out; 3236789Sahrens } 3237789Sahrens 3238789Sahrens /* 3239789Sahrens * Does target exist? 3240789Sahrens */ 3241789Sahrens if (tzp) { 3242789Sahrens /* 3243789Sahrens * Source and target must be the same type. 3244789Sahrens */ 3245789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3246789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3247789Sahrens error = ENOTDIR; 3248789Sahrens goto out; 3249789Sahrens } 3250789Sahrens } else { 3251789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3252789Sahrens error = EISDIR; 3253789Sahrens goto out; 3254789Sahrens } 3255789Sahrens } 3256789Sahrens /* 3257789Sahrens * POSIX dictates that when the source and target 3258789Sahrens * entries refer to the same file object, rename 3259789Sahrens * must do nothing and exit without error. 3260789Sahrens */ 3261789Sahrens if (szp->z_id == tzp->z_id) { 3262789Sahrens error = 0; 3263789Sahrens goto out; 3264789Sahrens } 3265789Sahrens } 3266789Sahrens 32675331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3268789Sahrens if (tzp) 32695331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 32704863Spraks 32714863Spraks /* 32724863Spraks * notify the target directory if it is not the same 32734863Spraks * as source directory. 32744863Spraks */ 32754863Spraks if (tdvp != sdvp) { 32765331Samw vnevent_rename_dest_dir(tdvp, ct); 32774863Spraks } 3278789Sahrens 3279789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3280789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3281789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 32821544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 32831544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 32841544Seschrock if (sdzp != tdzp) 3285789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 32861544Seschrock if (tzp) 32871544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 32883461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 32898227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3290789Sahrens if (error) { 3291789Sahrens if (zl != NULL) 3292789Sahrens zfs_rename_unlock(&zl); 3293789Sahrens zfs_dirent_unlock(sdl); 3294789Sahrens zfs_dirent_unlock(tdl); 3295789Sahrens VN_RELE(ZTOV(szp)); 3296789Sahrens if (tzp) 3297789Sahrens VN_RELE(ZTOV(tzp)); 32988227SNeil.Perrin@Sun.COM if (error == ERESTART) { 32992113Sahrens dmu_tx_wait(tx); 33002113Sahrens dmu_tx_abort(tx); 3301789Sahrens goto top; 3302789Sahrens } 33032113Sahrens dmu_tx_abort(tx); 3304789Sahrens ZFS_EXIT(zfsvfs); 3305789Sahrens return (error); 3306789Sahrens } 3307789Sahrens 3308789Sahrens if (tzp) /* Attempt to remove the existing target */ 33095331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3310789Sahrens 3311789Sahrens if (error == 0) { 3312789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3313789Sahrens if (error == 0) { 33145331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 33155331Samw 3316789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3317789Sahrens ASSERT(error == 0); 33185331Samw 33195331Samw zfs_log_rename(zilog, tx, 33205331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 33215331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 33226976Seschrock 33236976Seschrock /* Update path information for the target vnode */ 33246976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3325789Sahrens } 3326789Sahrens } 3327789Sahrens 3328789Sahrens dmu_tx_commit(tx); 3329789Sahrens out: 3330789Sahrens if (zl != NULL) 3331789Sahrens zfs_rename_unlock(&zl); 3332789Sahrens 3333789Sahrens zfs_dirent_unlock(sdl); 3334789Sahrens zfs_dirent_unlock(tdl); 3335789Sahrens 3336789Sahrens VN_RELE(ZTOV(szp)); 3337789Sahrens if (tzp) 3338789Sahrens VN_RELE(ZTOV(tzp)); 3339789Sahrens 3340789Sahrens ZFS_EXIT(zfsvfs); 3341789Sahrens return (error); 3342789Sahrens } 3343789Sahrens 3344789Sahrens /* 3345789Sahrens * Insert the indicated symbolic reference entry into the directory. 3346789Sahrens * 3347789Sahrens * IN: dvp - Directory to contain new symbolic link. 3348789Sahrens * link - Name for new symlink entry. 3349789Sahrens * vap - Attributes of new entry. 3350789Sahrens * target - Target path of new symlink. 3351789Sahrens * cr - credentials of caller. 33525331Samw * ct - caller context 33535331Samw * flags - case flags 3354789Sahrens * 3355789Sahrens * RETURN: 0 if success 3356789Sahrens * error code if failure 3357789Sahrens * 3358789Sahrens * Timestamps: 3359789Sahrens * dvp - ctime|mtime updated 3360789Sahrens */ 33615331Samw /*ARGSUSED*/ 3362789Sahrens static int 33635331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 33645331Samw caller_context_t *ct, int flags) 3365789Sahrens { 3366789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3367789Sahrens zfs_dirlock_t *dl; 3368789Sahrens dmu_tx_t *tx; 3369789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 33705326Sek110237 zilog_t *zilog; 3371789Sahrens int len = strlen(link); 3372789Sahrens int error; 33735331Samw int zflg = ZNEW; 33749179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 33759179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 3376789Sahrens 3377789Sahrens ASSERT(vap->va_type == VLNK); 3378789Sahrens 33795367Sahrens ZFS_ENTER(zfsvfs); 33805367Sahrens ZFS_VERIFY_ZP(dzp); 33815326Sek110237 zilog = zfsvfs->z_log; 33825331Samw 33835498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 33845331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 33855331Samw ZFS_EXIT(zfsvfs); 33865331Samw return (EILSEQ); 33875331Samw } 33885331Samw if (flags & FIGNORECASE) 33895331Samw zflg |= ZCILOOK; 3390789Sahrens top: 33915331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3392789Sahrens ZFS_EXIT(zfsvfs); 3393789Sahrens return (error); 3394789Sahrens } 3395789Sahrens 3396789Sahrens if (len > MAXPATHLEN) { 3397789Sahrens ZFS_EXIT(zfsvfs); 3398789Sahrens return (ENAMETOOLONG); 3399789Sahrens } 3400789Sahrens 3401789Sahrens /* 3402789Sahrens * Attempt to lock directory; fail if entry already exists. 3403789Sahrens */ 34045331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 34055331Samw if (error) { 3406789Sahrens ZFS_EXIT(zfsvfs); 3407789Sahrens return (error); 3408789Sahrens } 3409789Sahrens 34109179SMark.Shellenbaum@Sun.COM VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids)); 34119396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 34129396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 34139396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 34149396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 34159396SMatthew.Ahrens@Sun.COM return (EDQUOT); 34169396SMatthew.Ahrens@Sun.COM } 3417789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 34189179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3419789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3420789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 34211544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 34229179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 3423789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 34249396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 34259396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 34268227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3427789Sahrens if (error) { 34289179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3429789Sahrens zfs_dirent_unlock(dl); 34308227SNeil.Perrin@Sun.COM if (error == ERESTART) { 34312113Sahrens dmu_tx_wait(tx); 34322113Sahrens dmu_tx_abort(tx); 3433789Sahrens goto top; 3434789Sahrens } 34352113Sahrens dmu_tx_abort(tx); 3436789Sahrens ZFS_EXIT(zfsvfs); 3437789Sahrens return (error); 3438789Sahrens } 3439789Sahrens 3440789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3441789Sahrens 3442789Sahrens /* 3443789Sahrens * Create a new object for the symlink. 3444789Sahrens * Put the link content into bonus buffer if it will fit; 3445789Sahrens * otherwise, store it just like any other file data. 3446789Sahrens */ 3447789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 34489179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids); 3449789Sahrens if (len != 0) 3450789Sahrens bcopy(link, zp->z_phys + 1, len); 3451789Sahrens } else { 3452789Sahrens dmu_buf_t *dbp; 34531669Sperrin 34549179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 34559179SMark.Shellenbaum@Sun.COM 34569179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 34579179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 34581669Sperrin /* 34591669Sperrin * Nothing can access the znode yet so no locking needed 34601669Sperrin * for growing the znode's blocksize. 34611669Sperrin */ 34621669Sperrin zfs_grow_blocksize(zp, len, tx); 3463789Sahrens 34645446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 34655446Sahrens zp->z_id, 0, FTAG, &dbp)); 3466789Sahrens dmu_buf_will_dirty(dbp, tx); 3467789Sahrens 3468789Sahrens ASSERT3U(len, <=, dbp->db_size); 3469789Sahrens bcopy(link, dbp->db_data, len); 34701544Seschrock dmu_buf_rele(dbp, FTAG); 3471789Sahrens } 3472789Sahrens zp->z_phys->zp_size = len; 3473789Sahrens 3474789Sahrens /* 3475789Sahrens * Insert the new object into the directory. 3476789Sahrens */ 3477789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 34785331Samw if (error == 0) { 34795331Samw uint64_t txtype = TX_SYMLINK; 34805331Samw if (flags & FIGNORECASE) 34815331Samw txtype |= TX_CI; 34825331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 34835331Samw } 34849179SMark.Shellenbaum@Sun.COM 34859179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3486789Sahrens 3487789Sahrens dmu_tx_commit(tx); 3488789Sahrens 3489789Sahrens zfs_dirent_unlock(dl); 3490789Sahrens 3491789Sahrens VN_RELE(ZTOV(zp)); 3492789Sahrens 3493789Sahrens ZFS_EXIT(zfsvfs); 3494789Sahrens return (error); 3495789Sahrens } 3496789Sahrens 3497789Sahrens /* 3498789Sahrens * Return, in the buffer contained in the provided uio structure, 3499789Sahrens * the symbolic path referred to by vp. 3500789Sahrens * 3501789Sahrens * IN: vp - vnode of symbolic link. 3502789Sahrens * uoip - structure to contain the link path. 3503789Sahrens * cr - credentials of caller. 35045331Samw * ct - caller context 3505789Sahrens * 3506789Sahrens * OUT: uio - structure to contain the link path. 3507789Sahrens * 3508789Sahrens * RETURN: 0 if success 3509789Sahrens * error code if failure 3510789Sahrens * 3511789Sahrens * Timestamps: 3512789Sahrens * vp - atime updated 3513789Sahrens */ 3514789Sahrens /* ARGSUSED */ 3515789Sahrens static int 35165331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3517789Sahrens { 3518789Sahrens znode_t *zp = VTOZ(vp); 3519789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3520789Sahrens size_t bufsz; 3521789Sahrens int error; 3522789Sahrens 35235367Sahrens ZFS_ENTER(zfsvfs); 35245367Sahrens ZFS_VERIFY_ZP(zp); 3525789Sahrens 3526789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3527789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3528789Sahrens error = uiomove(zp->z_phys + 1, 3529789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3530789Sahrens } else { 35311544Seschrock dmu_buf_t *dbp; 35321544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 35331544Seschrock if (error) { 3534789Sahrens ZFS_EXIT(zfsvfs); 3535789Sahrens return (error); 3536789Sahrens } 3537789Sahrens error = uiomove(dbp->db_data, 3538789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 35391544Seschrock dmu_buf_rele(dbp, FTAG); 3540789Sahrens } 3541789Sahrens 3542789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3543789Sahrens ZFS_EXIT(zfsvfs); 3544789Sahrens return (error); 3545789Sahrens } 3546789Sahrens 3547789Sahrens /* 3548789Sahrens * Insert a new entry into directory tdvp referencing svp. 3549789Sahrens * 3550789Sahrens * IN: tdvp - Directory to contain new entry. 3551789Sahrens * svp - vnode of new entry. 3552789Sahrens * name - name of new entry. 3553789Sahrens * cr - credentials of caller. 35545331Samw * ct - caller context 3555789Sahrens * 3556789Sahrens * RETURN: 0 if success 3557789Sahrens * error code if failure 3558789Sahrens * 3559789Sahrens * Timestamps: 3560789Sahrens * tdvp - ctime|mtime updated 3561789Sahrens * svp - ctime updated 3562789Sahrens */ 3563789Sahrens /* ARGSUSED */ 3564789Sahrens static int 35655331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 35665331Samw caller_context_t *ct, int flags) 3567789Sahrens { 3568789Sahrens znode_t *dzp = VTOZ(tdvp); 3569789Sahrens znode_t *tzp, *szp; 3570789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 35715326Sek110237 zilog_t *zilog; 3572789Sahrens zfs_dirlock_t *dl; 3573789Sahrens dmu_tx_t *tx; 3574789Sahrens vnode_t *realvp; 3575789Sahrens int error; 35765331Samw int zf = ZNEW; 35775331Samw uid_t owner; 3578789Sahrens 3579789Sahrens ASSERT(tdvp->v_type == VDIR); 3580789Sahrens 35815367Sahrens ZFS_ENTER(zfsvfs); 35825367Sahrens ZFS_VERIFY_ZP(dzp); 35835326Sek110237 zilog = zfsvfs->z_log; 3584789Sahrens 35855331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3586789Sahrens svp = realvp; 3587789Sahrens 3588789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3589789Sahrens ZFS_EXIT(zfsvfs); 3590789Sahrens return (EXDEV); 3591789Sahrens } 35925367Sahrens szp = VTOZ(svp); 35935367Sahrens ZFS_VERIFY_ZP(szp); 3594789Sahrens 35955498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 35965331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 35975331Samw ZFS_EXIT(zfsvfs); 35985331Samw return (EILSEQ); 35995331Samw } 36005331Samw if (flags & FIGNORECASE) 36015331Samw zf |= ZCILOOK; 36025331Samw 3603789Sahrens top: 3604789Sahrens /* 3605789Sahrens * We do not support links between attributes and non-attributes 3606789Sahrens * because of the potential security risk of creating links 3607789Sahrens * into "normal" file space in order to circumvent restrictions 3608789Sahrens * imposed in attribute space. 3609789Sahrens */ 3610789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3611789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3612789Sahrens ZFS_EXIT(zfsvfs); 3613789Sahrens return (EINVAL); 3614789Sahrens } 3615789Sahrens 3616789Sahrens /* 3617789Sahrens * POSIX dictates that we return EPERM here. 3618789Sahrens * Better choices include ENOTSUP or EISDIR. 3619789Sahrens */ 3620789Sahrens if (svp->v_type == VDIR) { 3621789Sahrens ZFS_EXIT(zfsvfs); 3622789Sahrens return (EPERM); 3623789Sahrens } 3624789Sahrens 36255959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 36265331Samw if (owner != crgetuid(cr) && 3627789Sahrens secpolicy_basic_link(cr) != 0) { 3628789Sahrens ZFS_EXIT(zfsvfs); 3629789Sahrens return (EPERM); 3630789Sahrens } 3631789Sahrens 36325331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3633789Sahrens ZFS_EXIT(zfsvfs); 3634789Sahrens return (error); 3635789Sahrens } 3636789Sahrens 3637789Sahrens /* 3638789Sahrens * Attempt to lock directory; fail if entry already exists. 3639789Sahrens */ 36405331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 36415331Samw if (error) { 3642789Sahrens ZFS_EXIT(zfsvfs); 3643789Sahrens return (error); 3644789Sahrens } 3645789Sahrens 3646789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3647789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 36481544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 36498227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3650789Sahrens if (error) { 3651789Sahrens zfs_dirent_unlock(dl); 36528227SNeil.Perrin@Sun.COM if (error == ERESTART) { 36532113Sahrens dmu_tx_wait(tx); 36542113Sahrens dmu_tx_abort(tx); 3655789Sahrens goto top; 3656789Sahrens } 36572113Sahrens dmu_tx_abort(tx); 3658789Sahrens ZFS_EXIT(zfsvfs); 3659789Sahrens return (error); 3660789Sahrens } 3661789Sahrens 3662789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3663789Sahrens 36645331Samw if (error == 0) { 36655331Samw uint64_t txtype = TX_LINK; 36665331Samw if (flags & FIGNORECASE) 36675331Samw txtype |= TX_CI; 36685331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 36695331Samw } 3670789Sahrens 3671789Sahrens dmu_tx_commit(tx); 3672789Sahrens 3673789Sahrens zfs_dirent_unlock(dl); 3674789Sahrens 36754863Spraks if (error == 0) { 36765331Samw vnevent_link(svp, ct); 36774863Spraks } 36784863Spraks 3679789Sahrens ZFS_EXIT(zfsvfs); 3680789Sahrens return (error); 3681789Sahrens } 3682789Sahrens 3683789Sahrens /* 3684789Sahrens * zfs_null_putapage() is used when the file system has been force 3685789Sahrens * unmounted. It just drops the pages. 3686789Sahrens */ 3687789Sahrens /* ARGSUSED */ 3688789Sahrens static int 3689789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3690789Sahrens size_t *lenp, int flags, cred_t *cr) 3691789Sahrens { 3692789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3693789Sahrens return (0); 3694789Sahrens } 3695789Sahrens 36962688Smaybee /* 36972688Smaybee * Push a page out to disk, klustering if possible. 36982688Smaybee * 36992688Smaybee * IN: vp - file to push page to. 37002688Smaybee * pp - page to push. 37012688Smaybee * flags - additional flags. 37022688Smaybee * cr - credentials of caller. 37032688Smaybee * 37042688Smaybee * OUT: offp - start of range pushed. 37052688Smaybee * lenp - len of range pushed. 37062688Smaybee * 37072688Smaybee * RETURN: 0 if success 37082688Smaybee * error code if failure 37092688Smaybee * 37102688Smaybee * NOTE: callers must have locked the page to be pushed. On 37112688Smaybee * exit, the page (and all other pages in the kluster) must be 37122688Smaybee * unlocked. 37132688Smaybee */ 3714789Sahrens /* ARGSUSED */ 3715789Sahrens static int 3716789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3717789Sahrens size_t *lenp, int flags, cred_t *cr) 3718789Sahrens { 3719789Sahrens znode_t *zp = VTOZ(vp); 3720789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3721789Sahrens dmu_tx_t *tx; 37222688Smaybee u_offset_t off, koff; 37232688Smaybee size_t len, klen; 37244709Smaybee uint64_t filesz; 3725789Sahrens int err; 3726789Sahrens 37274709Smaybee filesz = zp->z_phys->zp_size; 37282688Smaybee off = pp->p_offset; 37292688Smaybee len = PAGESIZE; 37302688Smaybee /* 37312688Smaybee * If our blocksize is bigger than the page size, try to kluster 37328227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 37332688Smaybee * a read-modify-write). 37342688Smaybee */ 37354709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 37368636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 37378636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 37382688Smaybee ASSERT(koff <= filesz); 37392688Smaybee if (koff + klen > filesz) 37402688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 37412688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 37422688Smaybee } 37432688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 37448636SMark.Maybee@Sun.COM 37451819Smaybee /* 37461819Smaybee * Can't push pages past end-of-file. 37471819Smaybee */ 37484709Smaybee if (off >= filesz) { 37494709Smaybee /* ignore all pages */ 37502688Smaybee err = 0; 37512688Smaybee goto out; 37524709Smaybee } else if (off + len > filesz) { 37534709Smaybee int npages = btopr(filesz - off); 37542688Smaybee page_t *trunc; 37552688Smaybee 37562688Smaybee page_list_break(&pp, &trunc, npages); 37574709Smaybee /* ignore pages past end of file */ 37582688Smaybee if (trunc) 37594709Smaybee pvn_write_done(trunc, flags); 37604709Smaybee len = filesz - off; 37611819Smaybee } 37629396SMatthew.Ahrens@Sun.COM 37639396SMatthew.Ahrens@Sun.COM if (zfs_usergroup_overquota(zfsvfs, B_FALSE, zp->z_phys->zp_uid) || 37649396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_TRUE, zp->z_phys->zp_gid)) { 37659396SMatthew.Ahrens@Sun.COM err = EDQUOT; 37669396SMatthew.Ahrens@Sun.COM goto out; 37679396SMatthew.Ahrens@Sun.COM } 37688636SMark.Maybee@Sun.COM top: 3769789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3770789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3771789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 37728227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 3773789Sahrens if (err != 0) { 37748227SNeil.Perrin@Sun.COM if (err == ERESTART) { 37752113Sahrens dmu_tx_wait(tx); 37762113Sahrens dmu_tx_abort(tx); 3777789Sahrens goto top; 3778789Sahrens } 37792113Sahrens dmu_tx_abort(tx); 3780789Sahrens goto out; 3781789Sahrens } 3782789Sahrens 37832688Smaybee if (zp->z_blksz <= PAGESIZE) { 37847315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 37852688Smaybee ASSERT3U(len, <=, PAGESIZE); 37862688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 37877315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 37882688Smaybee } else { 37892688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 37902688Smaybee } 37912688Smaybee 37922688Smaybee if (err == 0) { 37932688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 37948636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 37952688Smaybee } 37969951SLin.Ling@Sun.COM dmu_tx_commit(tx); 37972688Smaybee 37982688Smaybee out: 37994709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3800789Sahrens if (offp) 3801789Sahrens *offp = off; 3802789Sahrens if (lenp) 3803789Sahrens *lenp = len; 3804789Sahrens 3805789Sahrens return (err); 3806789Sahrens } 3807789Sahrens 3808789Sahrens /* 3809789Sahrens * Copy the portion of the file indicated from pages into the file. 3810789Sahrens * The pages are stored in a page list attached to the files vnode. 3811789Sahrens * 3812789Sahrens * IN: vp - vnode of file to push page data to. 3813789Sahrens * off - position in file to put data. 3814789Sahrens * len - amount of data to write. 3815789Sahrens * flags - flags to control the operation. 3816789Sahrens * cr - credentials of caller. 38175331Samw * ct - caller context. 3818789Sahrens * 3819789Sahrens * RETURN: 0 if success 3820789Sahrens * error code if failure 3821789Sahrens * 3822789Sahrens * Timestamps: 3823789Sahrens * vp - ctime|mtime updated 3824789Sahrens */ 38255331Samw /*ARGSUSED*/ 3826789Sahrens static int 38275331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 38285331Samw caller_context_t *ct) 3829789Sahrens { 3830789Sahrens znode_t *zp = VTOZ(vp); 3831789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3832789Sahrens page_t *pp; 3833789Sahrens size_t io_len; 3834789Sahrens u_offset_t io_off; 38358636SMark.Maybee@Sun.COM uint_t blksz; 38368636SMark.Maybee@Sun.COM rl_t *rl; 3837789Sahrens int error = 0; 3838789Sahrens 38395367Sahrens ZFS_ENTER(zfsvfs); 38405367Sahrens ZFS_VERIFY_ZP(zp); 3841789Sahrens 38428636SMark.Maybee@Sun.COM /* 38438636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 38448636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 38458636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 38468636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 38478636SMark.Maybee@Sun.COM * operations to get narrower locking. 38488636SMark.Maybee@Sun.COM */ 38498636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 38508636SMark.Maybee@Sun.COM if (ISP2(blksz)) 38518636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 38528636SMark.Maybee@Sun.COM else 38538636SMark.Maybee@Sun.COM io_off = 0; 38548636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 38559141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 38568636SMark.Maybee@Sun.COM else 38578636SMark.Maybee@Sun.COM io_len = 0; 38588636SMark.Maybee@Sun.COM 38598636SMark.Maybee@Sun.COM if (io_len == 0) { 3860789Sahrens /* 38618636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 3862789Sahrens */ 38638636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 38648636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 38651472Sperrin goto out; 3866789Sahrens } 38678636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 38688636SMark.Maybee@Sun.COM 38698636SMark.Maybee@Sun.COM if (off > zp->z_phys->zp_size) { 3870789Sahrens /* past end of file */ 38718636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 3872789Sahrens ZFS_EXIT(zfsvfs); 3873789Sahrens return (0); 3874789Sahrens } 3875789Sahrens 38768636SMark.Maybee@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off); 38778636SMark.Maybee@Sun.COM 38788636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 3879789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 38801669Sperrin pp = page_lookup(vp, io_off, 38814339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3882789Sahrens } else { 3883789Sahrens pp = page_lookup_nowait(vp, io_off, 38844339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3885789Sahrens } 3886789Sahrens 3887789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3888789Sahrens int err; 3889789Sahrens 3890789Sahrens /* 3891789Sahrens * Found a dirty page to push 3892789Sahrens */ 38931669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 38941669Sperrin if (err) 3895789Sahrens error = err; 3896789Sahrens } else { 3897789Sahrens io_len = PAGESIZE; 3898789Sahrens } 3899789Sahrens } 39001472Sperrin out: 39018636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 39022638Sperrin if ((flags & B_ASYNC) == 0) 39032638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3904789Sahrens ZFS_EXIT(zfsvfs); 3905789Sahrens return (error); 3906789Sahrens } 3907789Sahrens 39085331Samw /*ARGSUSED*/ 3909789Sahrens void 39105331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 3911789Sahrens { 3912789Sahrens znode_t *zp = VTOZ(vp); 3913789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3914789Sahrens int error; 3915789Sahrens 39165326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 39175446Sahrens if (zp->z_dbuf == NULL) { 39185446Sahrens /* 39195642Smaybee * The fs has been unmounted, or we did a 39205642Smaybee * suspend/resume and this file no longer exists. 39215446Sahrens */ 3922789Sahrens if (vn_has_cached_data(vp)) { 3923789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3924789Sahrens B_INVAL, cr); 3925789Sahrens } 3926789Sahrens 39271544Seschrock mutex_enter(&zp->z_lock); 392810369Schris.kirby@sun.com mutex_enter(&vp->v_lock); 392910369Schris.kirby@sun.com ASSERT(vp->v_count == 1); 393010369Schris.kirby@sun.com vp->v_count = 0; 393110369Schris.kirby@sun.com mutex_exit(&vp->v_lock); 39325446Sahrens mutex_exit(&zp->z_lock); 39335642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 39345446Sahrens zfs_znode_free(zp); 3935789Sahrens return; 3936789Sahrens } 3937789Sahrens 3938789Sahrens /* 3939789Sahrens * Attempt to push any data in the page cache. If this fails 3940789Sahrens * we will get kicked out later in zfs_zinactive(). 3941789Sahrens */ 39421298Sperrin if (vn_has_cached_data(vp)) { 39431298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 39441298Sperrin cr); 39451298Sperrin } 3946789Sahrens 39473461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3948789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3949789Sahrens 3950789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3951789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3952789Sahrens if (error) { 3953789Sahrens dmu_tx_abort(tx); 3954789Sahrens } else { 3955789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3956789Sahrens mutex_enter(&zp->z_lock); 3957789Sahrens zp->z_atime_dirty = 0; 3958789Sahrens mutex_exit(&zp->z_lock); 3959789Sahrens dmu_tx_commit(tx); 3960789Sahrens } 3961789Sahrens } 3962789Sahrens 3963789Sahrens zfs_zinactive(zp); 39645326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 3965789Sahrens } 3966789Sahrens 3967789Sahrens /* 3968789Sahrens * Bounds-check the seek operation. 3969789Sahrens * 3970789Sahrens * IN: vp - vnode seeking within 3971789Sahrens * ooff - old file offset 3972789Sahrens * noffp - pointer to new file offset 39735331Samw * ct - caller context 3974789Sahrens * 3975789Sahrens * RETURN: 0 if success 3976789Sahrens * EINVAL if new offset invalid 3977789Sahrens */ 3978789Sahrens /* ARGSUSED */ 3979789Sahrens static int 39805331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 39815331Samw caller_context_t *ct) 3982789Sahrens { 3983789Sahrens if (vp->v_type == VDIR) 3984789Sahrens return (0); 3985789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3986789Sahrens } 3987789Sahrens 3988789Sahrens /* 3989789Sahrens * Pre-filter the generic locking function to trap attempts to place 3990789Sahrens * a mandatory lock on a memory mapped file. 3991789Sahrens */ 3992789Sahrens static int 3993789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 39945331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 3995789Sahrens { 3996789Sahrens znode_t *zp = VTOZ(vp); 3997789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3998789Sahrens int error; 3999789Sahrens 40005367Sahrens ZFS_ENTER(zfsvfs); 40015367Sahrens ZFS_VERIFY_ZP(zp); 4002789Sahrens 4003789Sahrens /* 40041544Seschrock * We are following the UFS semantics with respect to mapcnt 40051544Seschrock * here: If we see that the file is mapped already, then we will 40061544Seschrock * return an error, but we don't worry about races between this 40071544Seschrock * function and zfs_map(). 4008789Sahrens */ 40091544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 4010789Sahrens ZFS_EXIT(zfsvfs); 4011789Sahrens return (EAGAIN); 4012789Sahrens } 40135331Samw error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct); 4014789Sahrens ZFS_EXIT(zfsvfs); 4015789Sahrens return (error); 4016789Sahrens } 4017789Sahrens 4018789Sahrens /* 4019789Sahrens * If we can't find a page in the cache, we will create a new page 4020789Sahrens * and fill it with file data. For efficiency, we may try to fill 40218636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 40229265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 40239265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4024789Sahrens */ 4025789Sahrens static int 4026789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4027789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4028789Sahrens { 4029789Sahrens znode_t *zp = VTOZ(vp); 4030789Sahrens page_t *pp, *cur_pp; 4031789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4032789Sahrens u_offset_t io_off, total; 4033789Sahrens size_t io_len; 4034789Sahrens int err; 4035789Sahrens 40362688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 40378636SMark.Maybee@Sun.COM /* 40388636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 40398636SMark.Maybee@Sun.COM */ 4040789Sahrens io_off = off; 4041789Sahrens io_len = PAGESIZE; 40429265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 40439265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4044789Sahrens } else { 4045789Sahrens /* 40468636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4047789Sahrens */ 4048789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 40498636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4050789Sahrens } 4051789Sahrens if (pp == NULL) { 4052789Sahrens /* 40538636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4054789Sahrens */ 4055789Sahrens *pl = NULL; 4056789Sahrens return (0); 4057789Sahrens } 4058789Sahrens 4059789Sahrens /* 4060789Sahrens * Fill the pages in the kluster. 4061789Sahrens */ 4062789Sahrens cur_pp = pp; 4063789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 40648636SMark.Maybee@Sun.COM caddr_t va; 40658636SMark.Maybee@Sun.COM 40662688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 40677315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 40689512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 40699512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 40707315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4071789Sahrens if (err) { 4072789Sahrens /* On error, toss the entire kluster */ 4073789Sahrens pvn_read_done(pp, B_ERROR); 40747294Sperrin /* convert checksum errors into IO errors */ 40757294Sperrin if (err == ECKSUM) 40767294Sperrin err = EIO; 4077789Sahrens return (err); 4078789Sahrens } 4079789Sahrens cur_pp = cur_pp->p_next; 4080789Sahrens } 40818636SMark.Maybee@Sun.COM 4082789Sahrens /* 40838636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 40848636SMark.Maybee@Sun.COM * from the desired offset `off'. 4085789Sahrens * NOTE: the page list will always be null terminated. 4086789Sahrens */ 4087789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 40888636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4089789Sahrens 4090789Sahrens return (0); 4091789Sahrens } 4092789Sahrens 4093789Sahrens /* 4094789Sahrens * Return pointers to the pages for the file region [off, off + len] 4095789Sahrens * in the pl array. If plsz is greater than len, this function may 40968636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 40978636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 40988636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 40998636SMark.Maybee@Sun.COM * part of a klustered read. 4100789Sahrens * 4101789Sahrens * IN: vp - vnode of file to get data from. 4102789Sahrens * off - position in file to get data from. 4103789Sahrens * len - amount of data to retrieve. 4104789Sahrens * plsz - length of provided page list. 4105789Sahrens * seg - segment to obtain pages for. 4106789Sahrens * addr - virtual address of fault. 4107789Sahrens * rw - mode of created pages. 4108789Sahrens * cr - credentials of caller. 41095331Samw * ct - caller context. 4110789Sahrens * 4111789Sahrens * OUT: protp - protection mode of created pages. 4112789Sahrens * pl - list of pages created. 4113789Sahrens * 4114789Sahrens * RETURN: 0 if success 4115789Sahrens * error code if failure 4116789Sahrens * 4117789Sahrens * Timestamps: 4118789Sahrens * vp - atime updated 4119789Sahrens */ 4120789Sahrens /* ARGSUSED */ 4121789Sahrens static int 4122789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4123789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 41245331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4125789Sahrens { 4126789Sahrens znode_t *zp = VTOZ(vp); 4127789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 41288636SMark.Maybee@Sun.COM page_t **pl0 = pl; 41298636SMark.Maybee@Sun.COM int err = 0; 41308636SMark.Maybee@Sun.COM 41318636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 41328636SMark.Maybee@Sun.COM if (pl == NULL) 41338636SMark.Maybee@Sun.COM return (0); 41348636SMark.Maybee@Sun.COM else if (len > plsz) 41358636SMark.Maybee@Sun.COM len = plsz; 41368681SMark.Maybee@Sun.COM else 41378681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 41388636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4139789Sahrens 41405367Sahrens ZFS_ENTER(zfsvfs); 41415367Sahrens ZFS_VERIFY_ZP(zp); 4142789Sahrens 4143789Sahrens if (protp) 4144789Sahrens *protp = PROT_ALL; 4145789Sahrens 4146789Sahrens /* 41479265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4148789Sahrens * for pages. If we don't find a page, we will need to create 4149789Sahrens * a new page and fill it with data from the file. 4150789Sahrens */ 4151789Sahrens while (len > 0) { 41528636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 41538636SMark.Maybee@Sun.COM *(pl+1) = NULL; 41548636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 41558636SMark.Maybee@Sun.COM goto out; 41568636SMark.Maybee@Sun.COM while (*pl) { 41578636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4158789Sahrens off += PAGESIZE; 4159789Sahrens addr += PAGESIZE; 41608681SMark.Maybee@Sun.COM if (len > 0) { 41618681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 41628636SMark.Maybee@Sun.COM len -= PAGESIZE; 41638681SMark.Maybee@Sun.COM } 41648636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4165789Sahrens plsz -= PAGESIZE; 41668636SMark.Maybee@Sun.COM pl++; 4167789Sahrens } 4168789Sahrens } 4169789Sahrens 4170789Sahrens /* 4171789Sahrens * Fill out the page array with any pages already in the cache. 4172789Sahrens */ 41738636SMark.Maybee@Sun.COM while (plsz > 0 && 41748636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 41758636SMark.Maybee@Sun.COM off += PAGESIZE; 41768636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4177789Sahrens } 4178789Sahrens out: 41792752Sperrin if (err) { 41802752Sperrin /* 41812752Sperrin * Release any pages we have previously locked. 41822752Sperrin */ 41832752Sperrin while (pl > pl0) 41842752Sperrin page_unlock(*--pl); 41858636SMark.Maybee@Sun.COM } else { 41868636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 41872752Sperrin } 41882752Sperrin 4189789Sahrens *pl = NULL; 4190789Sahrens 4191789Sahrens ZFS_EXIT(zfsvfs); 4192789Sahrens return (err); 4193789Sahrens } 4194789Sahrens 41951544Seschrock /* 41961544Seschrock * Request a memory map for a section of a file. This code interacts 41971544Seschrock * with common code and the VM system as follows: 41981544Seschrock * 41991544Seschrock * common code calls mmap(), which ends up in smmap_common() 42001544Seschrock * 42011544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 42021544Seschrock * 42031544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 42041544Seschrock * 42051544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 42061544Seschrock * 42071544Seschrock * zfs_addmap() updates z_mapcnt 42081544Seschrock */ 42095331Samw /*ARGSUSED*/ 4210789Sahrens static int 4211789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 42125331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 42135331Samw caller_context_t *ct) 4214789Sahrens { 4215789Sahrens znode_t *zp = VTOZ(vp); 4216789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4217789Sahrens segvn_crargs_t vn_a; 4218789Sahrens int error; 4219789Sahrens 42205929Smarks ZFS_ENTER(zfsvfs); 42215929Smarks ZFS_VERIFY_ZP(zp); 42225929Smarks 42235331Samw if ((prot & PROT_WRITE) && 42245331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 42255929Smarks ZFS_APPENDONLY))) { 42265929Smarks ZFS_EXIT(zfsvfs); 42275331Samw return (EPERM); 42285929Smarks } 42295929Smarks 42305929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 42315929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 42325929Smarks ZFS_EXIT(zfsvfs); 42335929Smarks return (EACCES); 42345929Smarks } 4235789Sahrens 4236789Sahrens if (vp->v_flag & VNOMAP) { 4237789Sahrens ZFS_EXIT(zfsvfs); 4238789Sahrens return (ENOSYS); 4239789Sahrens } 4240789Sahrens 4241789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4242789Sahrens ZFS_EXIT(zfsvfs); 4243789Sahrens return (ENXIO); 4244789Sahrens } 4245789Sahrens 4246789Sahrens if (vp->v_type != VREG) { 4247789Sahrens ZFS_EXIT(zfsvfs); 4248789Sahrens return (ENODEV); 4249789Sahrens } 4250789Sahrens 4251789Sahrens /* 4252789Sahrens * If file is locked, disallow mapping. 4253789Sahrens */ 42541544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 42551544Seschrock ZFS_EXIT(zfsvfs); 42561544Seschrock return (EAGAIN); 4257789Sahrens } 4258789Sahrens 4259789Sahrens as_rangelock(as); 42606036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 42616036Smec if (error != 0) { 42626036Smec as_rangeunlock(as); 42636036Smec ZFS_EXIT(zfsvfs); 42646036Smec return (error); 4265789Sahrens } 4266789Sahrens 4267789Sahrens vn_a.vp = vp; 4268789Sahrens vn_a.offset = (u_offset_t)off; 4269789Sahrens vn_a.type = flags & MAP_TYPE; 4270789Sahrens vn_a.prot = prot; 4271789Sahrens vn_a.maxprot = maxprot; 4272789Sahrens vn_a.cred = cr; 4273789Sahrens vn_a.amp = NULL; 4274789Sahrens vn_a.flags = flags & ~MAP_TYPE; 42751417Skchow vn_a.szc = 0; 42761417Skchow vn_a.lgrp_mem_policy_flags = 0; 4277789Sahrens 4278789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4279789Sahrens 4280789Sahrens as_rangeunlock(as); 4281789Sahrens ZFS_EXIT(zfsvfs); 4282789Sahrens return (error); 4283789Sahrens } 4284789Sahrens 4285789Sahrens /* ARGSUSED */ 4286789Sahrens static int 4287789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 42885331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 42895331Samw caller_context_t *ct) 4290789Sahrens { 42911544Seschrock uint64_t pages = btopr(len); 42921544Seschrock 42931544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4294789Sahrens return (0); 4295789Sahrens } 4296789Sahrens 42971773Seschrock /* 42981773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 42991773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 43001773Seschrock * detecting when the data was actually modified, we have to resort to 43011773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 43021773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 43031773Seschrock * which by far the most common case: 43041773Seschrock * 43051773Seschrock * open() 43061773Seschrock * mmap() 43071773Seschrock * <modify memory> 43081773Seschrock * munmap() 43091773Seschrock * close() 43101773Seschrock * <time lapse> 43111773Seschrock * putpage() via fsflush 43121773Seschrock * 43131773Seschrock * If we wait until fsflush to come along, we can have a modification time that 43141773Seschrock * is some arbitrary point in the future. In order to prevent this in the 43151773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 43161773Seschrock * torn down. 43171773Seschrock */ 4318789Sahrens /* ARGSUSED */ 4319789Sahrens static int 4320789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 43215331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 43225331Samw caller_context_t *ct) 4323789Sahrens { 43241544Seschrock uint64_t pages = btopr(len); 43251544Seschrock 43261544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 43271544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 43281773Seschrock 43291773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 43301773Seschrock vn_has_cached_data(vp)) 43315331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 43321773Seschrock 4333789Sahrens return (0); 4334789Sahrens } 4335789Sahrens 4336789Sahrens /* 4337789Sahrens * Free or allocate space in a file. Currently, this function only 4338789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4339789Sahrens * misnamed, as its functionality includes the ability to allocate as 4340789Sahrens * well as free space. 4341789Sahrens * 4342789Sahrens * IN: vp - vnode of file to free data in. 4343789Sahrens * cmd - action to take (only F_FREESP supported). 4344789Sahrens * bfp - section of file to free/alloc. 4345789Sahrens * flag - current file open mode flags. 4346789Sahrens * offset - current file offset. 4347789Sahrens * cr - credentials of caller [UNUSED]. 43485331Samw * ct - caller context. 4349789Sahrens * 4350789Sahrens * RETURN: 0 if success 4351789Sahrens * error code if failure 4352789Sahrens * 4353789Sahrens * Timestamps: 4354789Sahrens * vp - ctime|mtime updated 4355789Sahrens */ 4356789Sahrens /* ARGSUSED */ 4357789Sahrens static int 4358789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4359789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4360789Sahrens { 4361789Sahrens znode_t *zp = VTOZ(vp); 4362789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4363789Sahrens uint64_t off, len; 4364789Sahrens int error; 4365789Sahrens 43665367Sahrens ZFS_ENTER(zfsvfs); 43675367Sahrens ZFS_VERIFY_ZP(zp); 4368789Sahrens 4369789Sahrens if (cmd != F_FREESP) { 4370789Sahrens ZFS_EXIT(zfsvfs); 4371789Sahrens return (EINVAL); 4372789Sahrens } 4373789Sahrens 4374789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4375789Sahrens ZFS_EXIT(zfsvfs); 4376789Sahrens return (error); 4377789Sahrens } 4378789Sahrens 4379789Sahrens if (bfp->l_len < 0) { 4380789Sahrens ZFS_EXIT(zfsvfs); 4381789Sahrens return (EINVAL); 4382789Sahrens } 4383789Sahrens 4384789Sahrens off = bfp->l_start; 43851669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 43861878Smaybee 43876992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4388789Sahrens 4389789Sahrens ZFS_EXIT(zfsvfs); 4390789Sahrens return (error); 4391789Sahrens } 4392789Sahrens 43935331Samw /*ARGSUSED*/ 4394789Sahrens static int 43955331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4396789Sahrens { 4397789Sahrens znode_t *zp = VTOZ(vp); 4398789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 43995326Sek110237 uint32_t gen; 4400789Sahrens uint64_t object = zp->z_id; 4401789Sahrens zfid_short_t *zfid; 4402789Sahrens int size, i; 4403789Sahrens 44045367Sahrens ZFS_ENTER(zfsvfs); 44055367Sahrens ZFS_VERIFY_ZP(zp); 44065326Sek110237 gen = (uint32_t)zp->z_gen; 4407789Sahrens 4408789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4409789Sahrens if (fidp->fid_len < size) { 4410789Sahrens fidp->fid_len = size; 44111512Sek110237 ZFS_EXIT(zfsvfs); 4412789Sahrens return (ENOSPC); 4413789Sahrens } 4414789Sahrens 4415789Sahrens zfid = (zfid_short_t *)fidp; 4416789Sahrens 4417789Sahrens zfid->zf_len = size; 4418789Sahrens 4419789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4420789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4421789Sahrens 4422789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4423789Sahrens if (gen == 0) 4424789Sahrens gen = 1; 4425789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4426789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4427789Sahrens 4428789Sahrens if (size == LONG_FID_LEN) { 4429789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4430789Sahrens zfid_long_t *zlfid; 4431789Sahrens 4432789Sahrens zlfid = (zfid_long_t *)fidp; 4433789Sahrens 4434789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4435789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4436789Sahrens 4437789Sahrens /* XXX - this should be the generation number for the objset */ 4438789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4439789Sahrens zlfid->zf_setgen[i] = 0; 4440789Sahrens } 4441789Sahrens 4442789Sahrens ZFS_EXIT(zfsvfs); 4443789Sahrens return (0); 4444789Sahrens } 4445789Sahrens 4446789Sahrens static int 44475331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 44485331Samw caller_context_t *ct) 4449789Sahrens { 4450789Sahrens znode_t *zp, *xzp; 4451789Sahrens zfsvfs_t *zfsvfs; 4452789Sahrens zfs_dirlock_t *dl; 4453789Sahrens int error; 4454789Sahrens 4455789Sahrens switch (cmd) { 4456789Sahrens case _PC_LINK_MAX: 4457789Sahrens *valp = ULONG_MAX; 4458789Sahrens return (0); 4459789Sahrens 4460789Sahrens case _PC_FILESIZEBITS: 4461789Sahrens *valp = 64; 4462789Sahrens return (0); 4463789Sahrens 4464789Sahrens case _PC_XATTR_EXISTS: 4465789Sahrens zp = VTOZ(vp); 4466789Sahrens zfsvfs = zp->z_zfsvfs; 44675367Sahrens ZFS_ENTER(zfsvfs); 44685367Sahrens ZFS_VERIFY_ZP(zp); 4469789Sahrens *valp = 0; 4470789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 44715331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4472789Sahrens if (error == 0) { 4473789Sahrens zfs_dirent_unlock(dl); 4474789Sahrens if (!zfs_dirempty(xzp)) 4475789Sahrens *valp = 1; 4476789Sahrens VN_RELE(ZTOV(xzp)); 4477789Sahrens } else if (error == ENOENT) { 4478789Sahrens /* 4479789Sahrens * If there aren't extended attributes, it's the 4480789Sahrens * same as having zero of them. 4481789Sahrens */ 4482789Sahrens error = 0; 4483789Sahrens } 4484789Sahrens ZFS_EXIT(zfsvfs); 4485789Sahrens return (error); 4486789Sahrens 44875331Samw case _PC_SATTR_ENABLED: 44885331Samw case _PC_SATTR_EXISTS: 44897757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 44905331Samw (vp->v_type == VREG || vp->v_type == VDIR); 44915331Samw return (0); 44925331Samw 44939749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 44949749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 44959749STim.Haley@Sun.COM vp->v_type == VDIR; 44969749STim.Haley@Sun.COM return (0); 44979749STim.Haley@Sun.COM 4498789Sahrens case _PC_ACL_ENABLED: 4499789Sahrens *valp = _ACL_ACE_ENABLED; 4500789Sahrens return (0); 4501789Sahrens 4502789Sahrens case _PC_MIN_HOLE_SIZE: 4503789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4504789Sahrens return (0); 4505789Sahrens 450610440SRoger.Faulkner@Sun.COM case _PC_TIMESTAMP_RESOLUTION: 450710440SRoger.Faulkner@Sun.COM /* nanosecond timestamp resolution */ 450810440SRoger.Faulkner@Sun.COM *valp = 1L; 450910440SRoger.Faulkner@Sun.COM return (0); 451010440SRoger.Faulkner@Sun.COM 4511789Sahrens default: 45125331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4513789Sahrens } 4514789Sahrens } 4515789Sahrens 4516789Sahrens /*ARGSUSED*/ 4517789Sahrens static int 45185331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 45195331Samw caller_context_t *ct) 4520789Sahrens { 4521789Sahrens znode_t *zp = VTOZ(vp); 4522789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4523789Sahrens int error; 45245331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4525789Sahrens 45265367Sahrens ZFS_ENTER(zfsvfs); 45275367Sahrens ZFS_VERIFY_ZP(zp); 45285331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4529789Sahrens ZFS_EXIT(zfsvfs); 4530789Sahrens 4531789Sahrens return (error); 4532789Sahrens } 4533789Sahrens 4534789Sahrens /*ARGSUSED*/ 4535789Sahrens static int 45365331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 45375331Samw caller_context_t *ct) 4538789Sahrens { 4539789Sahrens znode_t *zp = VTOZ(vp); 4540789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4541789Sahrens int error; 45425331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4543789Sahrens 45445367Sahrens ZFS_ENTER(zfsvfs); 45455367Sahrens ZFS_VERIFY_ZP(zp); 45465331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4547789Sahrens ZFS_EXIT(zfsvfs); 4548789Sahrens return (error); 4549789Sahrens } 4550789Sahrens 4551789Sahrens /* 4552789Sahrens * Predeclare these here so that the compiler assumes that 4553789Sahrens * this is an "old style" function declaration that does 4554789Sahrens * not include arguments => we won't get type mismatch errors 4555789Sahrens * in the initializations that follow. 4556789Sahrens */ 4557789Sahrens static int zfs_inval(); 4558789Sahrens static int zfs_isdir(); 4559789Sahrens 4560789Sahrens static int 4561789Sahrens zfs_inval() 4562789Sahrens { 4563789Sahrens return (EINVAL); 4564789Sahrens } 4565789Sahrens 4566789Sahrens static int 4567789Sahrens zfs_isdir() 4568789Sahrens { 4569789Sahrens return (EISDIR); 4570789Sahrens } 4571789Sahrens /* 4572789Sahrens * Directory vnode operations template 4573789Sahrens */ 4574789Sahrens vnodeops_t *zfs_dvnodeops; 4575789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 45763898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 45773898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 45783898Srsb VOPNAME_READ, { .error = zfs_isdir }, 45793898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 45803898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 45813898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 45823898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 45833898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 45843898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 45853898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 45863898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 45873898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 45883898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 45893898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 45903898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 45913898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 45923898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 45933898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 45943898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 45953898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 45963898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 45973898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 45983898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 45993898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 46004863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46013898Srsb NULL, NULL 4602789Sahrens }; 4603789Sahrens 4604789Sahrens /* 4605789Sahrens * Regular file vnode operations template 4606789Sahrens */ 4607789Sahrens vnodeops_t *zfs_fvnodeops; 4608789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 46093898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 46103898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 46113898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 46123898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 46133898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 46143898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46153898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 46163898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 46173898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 46183898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 46193898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 46203898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46213898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 46223898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 46233898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 46243898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 46253898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 46263898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 46273898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 46283898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 46293898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 46303898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46313898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 46323898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 46333898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46343898Srsb NULL, NULL 4635789Sahrens }; 4636789Sahrens 4637789Sahrens /* 4638789Sahrens * Symbolic link vnode operations template 4639789Sahrens */ 4640789Sahrens vnodeops_t *zfs_symvnodeops; 4641789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 46423898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46433898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 46443898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 46453898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 46463898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 46473898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46483898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 46493898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46503898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46513898Srsb NULL, NULL 4652789Sahrens }; 4653789Sahrens 4654789Sahrens /* 46558845Samw@Sun.COM * special share hidden files vnode operations template 46568845Samw@Sun.COM */ 46578845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 46588845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 46598845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46608845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 46618845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 46628845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 46638845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 46648845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 46658845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 46668845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 46678845Samw@Sun.COM NULL, NULL 46688845Samw@Sun.COM }; 46698845Samw@Sun.COM 46708845Samw@Sun.COM /* 4671789Sahrens * Extended attribute directory vnode operations template 4672789Sahrens * This template is identical to the directory vnodes 4673789Sahrens * operation template except for restricted operations: 4674789Sahrens * VOP_MKDIR() 4675789Sahrens * VOP_SYMLINK() 4676789Sahrens * Note that there are other restrictions embedded in: 4677789Sahrens * zfs_create() - restrict type to VREG 4678789Sahrens * zfs_link() - no links into/out of attribute space 4679789Sahrens * zfs_rename() - no moves into/out of attribute space 4680789Sahrens */ 4681789Sahrens vnodeops_t *zfs_xdvnodeops; 4682789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 46833898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 46843898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 46853898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 46863898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 46873898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 46883898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 46893898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 46903898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 46913898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 46923898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 46933898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 46943898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 46953898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 46963898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 46973898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 46983898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 46993898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 47003898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 47013898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 47023898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 47033898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 47043898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 47053898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 47063898Srsb NULL, NULL 4707789Sahrens }; 4708789Sahrens 4709789Sahrens /* 4710789Sahrens * Error vnode operations template 4711789Sahrens */ 4712789Sahrens vnodeops_t *zfs_evnodeops; 4713789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 47143898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 47153898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 47163898Srsb NULL, NULL 4717789Sahrens }; 4718