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 /* 22*11539SChunli.Zhang@Sun.COM * Copyright 2010 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> 7611134SCasper.Dik@Sun.COM #include <sys/cred.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; 450*11539SChunli.Zhang@Sun.COM xuio_t *xuio = NULL; 451789Sahrens 4525367Sahrens ZFS_ENTER(zfsvfs); 4535367Sahrens ZFS_VERIFY_ZP(zp); 4545326Sek110237 os = zfsvfs->z_os; 455789Sahrens 4565929Smarks if (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED) { 4575929Smarks ZFS_EXIT(zfsvfs); 4585929Smarks return (EACCES); 4595929Smarks } 4605929Smarks 461789Sahrens /* 462789Sahrens * Validate file offset 463789Sahrens */ 464789Sahrens if (uio->uio_loffset < (offset_t)0) { 465789Sahrens ZFS_EXIT(zfsvfs); 466789Sahrens return (EINVAL); 467789Sahrens } 468789Sahrens 469789Sahrens /* 470789Sahrens * Fasttrack empty reads 471789Sahrens */ 472789Sahrens if (uio->uio_resid == 0) { 473789Sahrens ZFS_EXIT(zfsvfs); 474789Sahrens return (0); 475789Sahrens } 476789Sahrens 477789Sahrens /* 4781669Sperrin * Check for mandatory locks 479789Sahrens */ 480789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 481789Sahrens if (error = chklock(vp, FREAD, 482789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 483789Sahrens ZFS_EXIT(zfsvfs); 484789Sahrens return (error); 485789Sahrens } 486789Sahrens } 487789Sahrens 488789Sahrens /* 489789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 490789Sahrens */ 4912638Sperrin if (ioflag & FRSYNC) 4922638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 493789Sahrens 494789Sahrens /* 4951669Sperrin * Lock the range against changes. 496789Sahrens */ 4971669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4981669Sperrin 499789Sahrens /* 500789Sahrens * If we are reading past end-of-file we can skip 501789Sahrens * to the end; but we might still need to set atime. 502789Sahrens */ 503789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 504789Sahrens error = 0; 505789Sahrens goto out; 506789Sahrens } 507789Sahrens 5083638Sbillm ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 5093638Sbillm n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 5103638Sbillm 511*11539SChunli.Zhang@Sun.COM if ((uio->uio_extflg == UIO_XUIO) && 512*11539SChunli.Zhang@Sun.COM (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) { 513*11539SChunli.Zhang@Sun.COM int nblk; 514*11539SChunli.Zhang@Sun.COM int blksz = zp->z_blksz; 515*11539SChunli.Zhang@Sun.COM uint64_t offset = uio->uio_loffset; 516*11539SChunli.Zhang@Sun.COM 517*11539SChunli.Zhang@Sun.COM xuio = (xuio_t *)uio; 518*11539SChunli.Zhang@Sun.COM if ((ISP2(blksz))) { 519*11539SChunli.Zhang@Sun.COM nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset, 520*11539SChunli.Zhang@Sun.COM blksz)) / blksz; 521*11539SChunli.Zhang@Sun.COM } else { 522*11539SChunli.Zhang@Sun.COM ASSERT(offset + n <= blksz); 523*11539SChunli.Zhang@Sun.COM nblk = 1; 524*11539SChunli.Zhang@Sun.COM } 525*11539SChunli.Zhang@Sun.COM dmu_xuio_init(xuio, nblk); 526*11539SChunli.Zhang@Sun.COM 527*11539SChunli.Zhang@Sun.COM if (vn_has_cached_data(vp)) { 528*11539SChunli.Zhang@Sun.COM /* 529*11539SChunli.Zhang@Sun.COM * For simplicity, we always allocate a full buffer 530*11539SChunli.Zhang@Sun.COM * even if we only expect to read a portion of a block. 531*11539SChunli.Zhang@Sun.COM */ 532*11539SChunli.Zhang@Sun.COM while (--nblk >= 0) { 533*11539SChunli.Zhang@Sun.COM dmu_xuio_add(xuio, 534*11539SChunli.Zhang@Sun.COM dmu_request_arcbuf(zp->z_dbuf, blksz), 535*11539SChunli.Zhang@Sun.COM 0, blksz); 536*11539SChunli.Zhang@Sun.COM } 537*11539SChunli.Zhang@Sun.COM } 538*11539SChunli.Zhang@Sun.COM } 539*11539SChunli.Zhang@Sun.COM 5403638Sbillm while (n > 0) { 5413638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 5423638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 5433638Sbillm 5443638Sbillm if (vn_has_cached_data(vp)) 5453638Sbillm error = mappedread(vp, nbytes, uio); 5463638Sbillm else 5473638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 5487294Sperrin if (error) { 5497294Sperrin /* convert checksum errors into IO errors */ 5507294Sperrin if (error == ECKSUM) 5517294Sperrin error = EIO; 5523638Sbillm break; 5537294Sperrin } 5543638Sbillm 5553638Sbillm n -= nbytes; 556789Sahrens } 557789Sahrens out: 5582237Smaybee zfs_range_unlock(rl); 559789Sahrens 560789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 561789Sahrens ZFS_EXIT(zfsvfs); 562789Sahrens return (error); 563789Sahrens } 564789Sahrens 565789Sahrens /* 566789Sahrens * Write the bytes to a file. 567789Sahrens * 568789Sahrens * IN: vp - vnode of file to be written to. 569789Sahrens * uio - structure supplying write location, range info, 570789Sahrens * and data buffer. 571789Sahrens * ioflag - FAPPEND flag set if in append mode. 572789Sahrens * cr - credentials of caller. 5735331Samw * ct - caller context (NFS/CIFS fem monitor only) 574789Sahrens * 575789Sahrens * OUT: uio - updated offset and range. 576789Sahrens * 577789Sahrens * RETURN: 0 if success 578789Sahrens * error code if failure 579789Sahrens * 580789Sahrens * Timestamps: 581789Sahrens * vp - ctime|mtime updated if byte count > 0 582789Sahrens */ 583789Sahrens /* ARGSUSED */ 584789Sahrens static int 585789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 586789Sahrens { 587789Sahrens znode_t *zp = VTOZ(vp); 588789Sahrens rlim64_t limit = uio->uio_llimit; 589789Sahrens ssize_t start_resid = uio->uio_resid; 590789Sahrens ssize_t tx_bytes; 591789Sahrens uint64_t end_size; 592789Sahrens dmu_tx_t *tx; 593789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5945326Sek110237 zilog_t *zilog; 595789Sahrens offset_t woff; 596789Sahrens ssize_t n, nbytes; 5971669Sperrin rl_t *rl; 598789Sahrens int max_blksz = zfsvfs->z_max_blksz; 5996743Smarks uint64_t pflags; 6001669Sperrin int error; 6019412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *abuf; 602*11539SChunli.Zhang@Sun.COM iovec_t *aiov; 603*11539SChunli.Zhang@Sun.COM xuio_t *xuio = NULL; 604*11539SChunli.Zhang@Sun.COM int i_iov = 0; 605*11539SChunli.Zhang@Sun.COM int iovcnt = uio->uio_iovcnt; 606*11539SChunli.Zhang@Sun.COM iovec_t *iovp = uio->uio_iov; 607*11539SChunli.Zhang@Sun.COM int write_eof; 608789Sahrens 609789Sahrens /* 610789Sahrens * Fasttrack empty write 611789Sahrens */ 6121669Sperrin n = start_resid; 613789Sahrens if (n == 0) 614789Sahrens return (0); 615789Sahrens 6161669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6171669Sperrin limit = MAXOFFSET_T; 6181669Sperrin 6195367Sahrens ZFS_ENTER(zfsvfs); 6205367Sahrens ZFS_VERIFY_ZP(zp); 6216743Smarks 6226743Smarks /* 6236743Smarks * If immutable or not appending then return EPERM 6246743Smarks */ 6256743Smarks pflags = zp->z_phys->zp_flags; 6266743Smarks if ((pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 6276743Smarks ((pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 6286743Smarks (uio->uio_loffset < zp->z_phys->zp_size))) { 6296743Smarks ZFS_EXIT(zfsvfs); 6306743Smarks return (EPERM); 6316743Smarks } 6326743Smarks 6335326Sek110237 zilog = zfsvfs->z_log; 634789Sahrens 635789Sahrens /* 63611083Swilliam.gorrell@sun.com * Validate file offset 63711083Swilliam.gorrell@sun.com */ 63811083Swilliam.gorrell@sun.com woff = ioflag & FAPPEND ? zp->z_phys->zp_size : uio->uio_loffset; 63911083Swilliam.gorrell@sun.com if (woff < 0) { 64011083Swilliam.gorrell@sun.com ZFS_EXIT(zfsvfs); 64111083Swilliam.gorrell@sun.com return (EINVAL); 64211083Swilliam.gorrell@sun.com } 64311083Swilliam.gorrell@sun.com 64411083Swilliam.gorrell@sun.com /* 64511083Swilliam.gorrell@sun.com * Check for mandatory locks before calling zfs_range_lock() 64611083Swilliam.gorrell@sun.com * in order to prevent a deadlock with locks set via fcntl(). 64711083Swilliam.gorrell@sun.com */ 64811083Swilliam.gorrell@sun.com if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 64911083Swilliam.gorrell@sun.com (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 65011083Swilliam.gorrell@sun.com ZFS_EXIT(zfsvfs); 65111083Swilliam.gorrell@sun.com return (error); 65211083Swilliam.gorrell@sun.com } 65311083Swilliam.gorrell@sun.com 65411083Swilliam.gorrell@sun.com /* 6552237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6561669Sperrin * don't hold up txg. 657*11539SChunli.Zhang@Sun.COM * Skip this if uio contains loaned arc_buf. 658789Sahrens */ 659*11539SChunli.Zhang@Sun.COM if ((uio->uio_extflg == UIO_XUIO) && 660*11539SChunli.Zhang@Sun.COM (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) 661*11539SChunli.Zhang@Sun.COM xuio = (xuio_t *)uio; 662*11539SChunli.Zhang@Sun.COM else 663*11539SChunli.Zhang@Sun.COM uio_prefaultpages(n, uio); 664789Sahrens 665789Sahrens /* 666789Sahrens * If in append mode, set the io offset pointer to eof. 667789Sahrens */ 6681669Sperrin if (ioflag & FAPPEND) { 6691669Sperrin /* 67011083Swilliam.gorrell@sun.com * Obtain an appending range lock to guarantee file append 67111083Swilliam.gorrell@sun.com * semantics. We reset the write offset once we have the lock. 6721669Sperrin */ 6731669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 67411083Swilliam.gorrell@sun.com woff = rl->r_off; 6751669Sperrin if (rl->r_len == UINT64_MAX) { 67611083Swilliam.gorrell@sun.com /* 67711083Swilliam.gorrell@sun.com * We overlocked the file because this write will cause 67811083Swilliam.gorrell@sun.com * the file block size to increase. 67911083Swilliam.gorrell@sun.com * Note that zp_size cannot change with this lock held. 68011083Swilliam.gorrell@sun.com */ 68111083Swilliam.gorrell@sun.com woff = zp->z_phys->zp_size; 6821669Sperrin } 68311083Swilliam.gorrell@sun.com uio->uio_loffset = woff; 684789Sahrens } else { 685789Sahrens /* 68611083Swilliam.gorrell@sun.com * Note that if the file block size will change as a result of 68711083Swilliam.gorrell@sun.com * this write, then this range lock will lock the entire file 68811083Swilliam.gorrell@sun.com * so that we can re-write the block safely. 689789Sahrens */ 6901669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 691789Sahrens } 692789Sahrens 693789Sahrens if (woff >= limit) { 6943638Sbillm zfs_range_unlock(rl); 6953638Sbillm ZFS_EXIT(zfsvfs); 6963638Sbillm return (EFBIG); 697789Sahrens } 698789Sahrens 699789Sahrens if ((woff + n) > limit || woff > (limit - n)) 700789Sahrens n = limit - woff; 701789Sahrens 702*11539SChunli.Zhang@Sun.COM /* Will this write extend the file length? */ 703*11539SChunli.Zhang@Sun.COM write_eof = (woff + n > zp->z_phys->zp_size); 704*11539SChunli.Zhang@Sun.COM 7051669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 706789Sahrens 7071669Sperrin /* 7083638Sbillm * Write the file in reasonable size chunks. Each chunk is written 7093638Sbillm * in a separate transaction; this keeps the intent log records small 7103638Sbillm * and allows us to do more fine-grained space accounting. 711789Sahrens */ 712789Sahrens while (n > 0) { 7139412SAleksandr.Guzovskiy@Sun.COM abuf = NULL; 7149412SAleksandr.Guzovskiy@Sun.COM woff = uio->uio_loffset; 7159412SAleksandr.Guzovskiy@Sun.COM again: 7169396SMatthew.Ahrens@Sun.COM if (zfs_usergroup_overquota(zfsvfs, 7179396SMatthew.Ahrens@Sun.COM B_FALSE, zp->z_phys->zp_uid) || 7189396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, 7199396SMatthew.Ahrens@Sun.COM B_TRUE, zp->z_phys->zp_gid)) { 7209412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7219412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7229396SMatthew.Ahrens@Sun.COM error = EDQUOT; 7239396SMatthew.Ahrens@Sun.COM break; 7249396SMatthew.Ahrens@Sun.COM } 7259412SAleksandr.Guzovskiy@Sun.COM 726*11539SChunli.Zhang@Sun.COM if (xuio && abuf == NULL) { 727*11539SChunli.Zhang@Sun.COM ASSERT(i_iov < iovcnt); 728*11539SChunli.Zhang@Sun.COM aiov = &iovp[i_iov]; 729*11539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i_iov); 730*11539SChunli.Zhang@Sun.COM dmu_xuio_clear(xuio, i_iov); 731*11539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_cp_write, int, i_iov, 732*11539SChunli.Zhang@Sun.COM iovec_t *, aiov, arc_buf_t *, abuf); 733*11539SChunli.Zhang@Sun.COM ASSERT((aiov->iov_base == abuf->b_data) || 734*11539SChunli.Zhang@Sun.COM ((char *)aiov->iov_base - (char *)abuf->b_data + 735*11539SChunli.Zhang@Sun.COM aiov->iov_len == arc_buf_size(abuf))); 736*11539SChunli.Zhang@Sun.COM i_iov++; 737*11539SChunli.Zhang@Sun.COM } else if (abuf == NULL && n >= max_blksz && 7389412SAleksandr.Guzovskiy@Sun.COM woff >= zp->z_phys->zp_size && 7399412SAleksandr.Guzovskiy@Sun.COM P2PHASE(woff, max_blksz) == 0 && 7409412SAleksandr.Guzovskiy@Sun.COM zp->z_blksz == max_blksz) { 741*11539SChunli.Zhang@Sun.COM /* 742*11539SChunli.Zhang@Sun.COM * This write covers a full block. "Borrow" a buffer 743*11539SChunli.Zhang@Sun.COM * from the dmu so that we can fill it before we enter 744*11539SChunli.Zhang@Sun.COM * a transaction. This avoids the possibility of 745*11539SChunli.Zhang@Sun.COM * holding up the transaction if the data copy hangs 746*11539SChunli.Zhang@Sun.COM * up on a pagefault (e.g., from an NFS server mapping). 747*11539SChunli.Zhang@Sun.COM */ 7489412SAleksandr.Guzovskiy@Sun.COM size_t cbytes; 7499412SAleksandr.Guzovskiy@Sun.COM 7509412SAleksandr.Guzovskiy@Sun.COM abuf = dmu_request_arcbuf(zp->z_dbuf, max_blksz); 7519412SAleksandr.Guzovskiy@Sun.COM ASSERT(abuf != NULL); 7529412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_buf_size(abuf) == max_blksz); 7539412SAleksandr.Guzovskiy@Sun.COM if (error = uiocopy(abuf->b_data, max_blksz, 7549412SAleksandr.Guzovskiy@Sun.COM UIO_WRITE, uio, &cbytes)) { 7559412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7569412SAleksandr.Guzovskiy@Sun.COM break; 7579412SAleksandr.Guzovskiy@Sun.COM } 7589412SAleksandr.Guzovskiy@Sun.COM ASSERT(cbytes == max_blksz); 7599412SAleksandr.Guzovskiy@Sun.COM } 7609412SAleksandr.Guzovskiy@Sun.COM 7619412SAleksandr.Guzovskiy@Sun.COM /* 7629412SAleksandr.Guzovskiy@Sun.COM * Start a transaction. 7639412SAleksandr.Guzovskiy@Sun.COM */ 764789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 765789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 766789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 7678227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 768789Sahrens if (error) { 7698227SNeil.Perrin@Sun.COM if (error == ERESTART) { 7702113Sahrens dmu_tx_wait(tx); 7712113Sahrens dmu_tx_abort(tx); 7729412SAleksandr.Guzovskiy@Sun.COM goto again; 773789Sahrens } 7742113Sahrens dmu_tx_abort(tx); 7759412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7769412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7773638Sbillm break; 7783638Sbillm } 7793638Sbillm 7803638Sbillm /* 7813638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 7823638Sbillm * and then reduce the lock range. This will only happen 7833638Sbillm * on the first iteration since zfs_range_reduce() will 7843638Sbillm * shrink down r_len to the appropriate size. 7853638Sbillm */ 7863638Sbillm if (rl->r_len == UINT64_MAX) { 7873638Sbillm uint64_t new_blksz; 7883638Sbillm 7893638Sbillm if (zp->z_blksz > max_blksz) { 7903638Sbillm ASSERT(!ISP2(zp->z_blksz)); 7913638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 7923638Sbillm } else { 7933638Sbillm new_blksz = MIN(end_size, max_blksz); 7943638Sbillm } 7953638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 7963638Sbillm zfs_range_reduce(rl, woff, n); 7973638Sbillm } 7983638Sbillm 7993638Sbillm /* 8003638Sbillm * XXX - should we really limit each write to z_max_blksz? 8013638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 8023638Sbillm */ 8033638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 8043638Sbillm 8059412SAleksandr.Guzovskiy@Sun.COM if (abuf == NULL) { 8069412SAleksandr.Guzovskiy@Sun.COM tx_bytes = uio->uio_resid; 8079412SAleksandr.Guzovskiy@Sun.COM error = dmu_write_uio(zfsvfs->z_os, zp->z_id, uio, 8089412SAleksandr.Guzovskiy@Sun.COM nbytes, tx); 8099412SAleksandr.Guzovskiy@Sun.COM tx_bytes -= uio->uio_resid; 8109412SAleksandr.Guzovskiy@Sun.COM } else { 8119412SAleksandr.Guzovskiy@Sun.COM tx_bytes = nbytes; 812*11539SChunli.Zhang@Sun.COM ASSERT(xuio == NULL || tx_bytes == aiov->iov_len); 813*11539SChunli.Zhang@Sun.COM /* 814*11539SChunli.Zhang@Sun.COM * If this is not a full block write, but we are 815*11539SChunli.Zhang@Sun.COM * extending the file past EOF and this data starts 816*11539SChunli.Zhang@Sun.COM * block-aligned, use assign_arcbuf(). Otherwise, 817*11539SChunli.Zhang@Sun.COM * write via dmu_write(). 818*11539SChunli.Zhang@Sun.COM */ 819*11539SChunli.Zhang@Sun.COM if (tx_bytes < max_blksz && (!write_eof || 820*11539SChunli.Zhang@Sun.COM aiov->iov_base != abuf->b_data)) { 821*11539SChunli.Zhang@Sun.COM ASSERT(xuio); 822*11539SChunli.Zhang@Sun.COM dmu_write(zfsvfs->z_os, zp->z_id, woff, 823*11539SChunli.Zhang@Sun.COM aiov->iov_len, aiov->iov_base, tx); 824*11539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 825*11539SChunli.Zhang@Sun.COM xuio_stat_wbuf_copied(); 826*11539SChunli.Zhang@Sun.COM } else { 827*11539SChunli.Zhang@Sun.COM ASSERT(xuio || tx_bytes == max_blksz); 828*11539SChunli.Zhang@Sun.COM dmu_assign_arcbuf(zp->z_dbuf, woff, abuf, tx); 829*11539SChunli.Zhang@Sun.COM } 8309412SAleksandr.Guzovskiy@Sun.COM ASSERT(tx_bytes <= uio->uio_resid); 8319412SAleksandr.Guzovskiy@Sun.COM uioskip(uio, tx_bytes); 8329412SAleksandr.Guzovskiy@Sun.COM } 8339412SAleksandr.Guzovskiy@Sun.COM if (tx_bytes && vn_has_cached_data(vp)) { 8348636SMark.Maybee@Sun.COM update_pages(vp, woff, 8358636SMark.Maybee@Sun.COM tx_bytes, zfsvfs->z_os, zp->z_id); 8369412SAleksandr.Guzovskiy@Sun.COM } 8373638Sbillm 8383638Sbillm /* 8393638Sbillm * If we made no progress, we're done. If we made even 8403638Sbillm * partial progress, update the znode and ZIL accordingly. 8413638Sbillm */ 8423638Sbillm if (tx_bytes == 0) { 8433897Smaybee dmu_tx_commit(tx); 8443638Sbillm ASSERT(error != 0); 8453638Sbillm break; 8463638Sbillm } 8473638Sbillm 848789Sahrens /* 8493638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 8503638Sbillm * privileged and at least one of the excute bits is set. 8513638Sbillm * 8523638Sbillm * It would be nice to to this after all writes have 8533638Sbillm * been done, but that would still expose the ISUID/ISGID 8543638Sbillm * to another app after the partial write is committed. 8555331Samw * 8565331Samw * Note: we don't call zfs_fuid_map_id() here because 8575331Samw * user 0 is not an ephemeral uid. 858789Sahrens */ 8593638Sbillm mutex_enter(&zp->z_acl_lock); 8603638Sbillm if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 8613638Sbillm (S_IXUSR >> 6))) != 0 && 8623638Sbillm (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 8633638Sbillm secpolicy_vnode_setid_retain(cr, 8643638Sbillm (zp->z_phys->zp_mode & S_ISUID) != 0 && 8653638Sbillm zp->z_phys->zp_uid == 0) != 0) { 8664339Sperrin zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 8673638Sbillm } 8683638Sbillm mutex_exit(&zp->z_acl_lock); 8693638Sbillm 8703638Sbillm /* 8713638Sbillm * Update time stamp. NOTE: This marks the bonus buffer as 8723638Sbillm * dirty, so we don't have to do it again for zp_size. 8733638Sbillm */ 8743638Sbillm zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 8753638Sbillm 8763638Sbillm /* 8773638Sbillm * Update the file size (zp_size) if it has changed; 8783638Sbillm * account for possible concurrent updates. 8793638Sbillm */ 8803638Sbillm while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 881789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 882789Sahrens uio->uio_loffset); 8833638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 8843638Sbillm dmu_tx_commit(tx); 8853638Sbillm 8863638Sbillm if (error != 0) 8873638Sbillm break; 8883638Sbillm ASSERT(tx_bytes == nbytes); 8893638Sbillm n -= nbytes; 890789Sahrens } 891789Sahrens 8922237Smaybee zfs_range_unlock(rl); 893789Sahrens 894789Sahrens /* 895789Sahrens * If we're in replay mode, or we made no progress, return error. 896789Sahrens * Otherwise, it's at least a partial write, so it's successful. 897789Sahrens */ 8988227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 899789Sahrens ZFS_EXIT(zfsvfs); 900789Sahrens return (error); 901789Sahrens } 902789Sahrens 9032638Sperrin if (ioflag & (FSYNC | FDSYNC)) 9042638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 905789Sahrens 906789Sahrens ZFS_EXIT(zfsvfs); 907789Sahrens return (0); 908789Sahrens } 909789Sahrens 9102237Smaybee void 91110922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error) 9122237Smaybee { 91310922SJeff.Bonwick@Sun.COM znode_t *zp = zgd->zgd_private; 91410922SJeff.Bonwick@Sun.COM objset_t *os = zp->z_zfsvfs->z_os; 91510922SJeff.Bonwick@Sun.COM 91610922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 91710922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 91810922SJeff.Bonwick@Sun.COM 91910922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 92010922SJeff.Bonwick@Sun.COM 9219321SNeil.Perrin@Sun.COM /* 9229321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9239321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9249321SNeil.Perrin@Sun.COM */ 92510922SJeff.Bonwick@Sun.COM VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 92610922SJeff.Bonwick@Sun.COM 92710922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 92810922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 92910922SJeff.Bonwick@Sun.COM 9303063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9312237Smaybee } 9322237Smaybee 93310209SMark.Musante@Sun.COM #ifdef DEBUG 93410209SMark.Musante@Sun.COM static int zil_fault_io = 0; 93510209SMark.Musante@Sun.COM #endif 93610209SMark.Musante@Sun.COM 937789Sahrens /* 938789Sahrens * Get data to generate a TX_WRITE intent log record. 939789Sahrens */ 940789Sahrens int 9412237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 942789Sahrens { 943789Sahrens zfsvfs_t *zfsvfs = arg; 944789Sahrens objset_t *os = zfsvfs->z_os; 945789Sahrens znode_t *zp; 94610922SJeff.Bonwick@Sun.COM uint64_t object = lr->lr_foid; 94710922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 94810922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; 94910922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9502237Smaybee dmu_buf_t *db; 9513063Sperrin zgd_t *zgd; 952789Sahrens int error = 0; 953789Sahrens 95410922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 95510922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 956789Sahrens 957789Sahrens /* 9581669Sperrin * Nothing to do if the file has been removed 959789Sahrens */ 96010922SJeff.Bonwick@Sun.COM if (zfs_zget(zfsvfs, object, &zp) != 0) 961789Sahrens return (ENOENT); 9623461Sahrens if (zp->z_unlinked) { 9639321SNeil.Perrin@Sun.COM /* 9649321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9659321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9669321SNeil.Perrin@Sun.COM */ 9679321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), 9689321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 969789Sahrens return (ENOENT); 970789Sahrens } 971789Sahrens 97210922SJeff.Bonwick@Sun.COM zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 97310922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zfsvfs->z_log; 97410922SJeff.Bonwick@Sun.COM zgd->zgd_private = zp; 97510922SJeff.Bonwick@Sun.COM 976789Sahrens /* 977789Sahrens * Write records come in two flavors: immediate and indirect. 978789Sahrens * For small writes it's cheaper to store the data with the 979789Sahrens * log record (immediate); for large writes it's cheaper to 980789Sahrens * sync the data and get a pointer to it (indirect) so that 981789Sahrens * we don't have to write the data twice. 982789Sahrens */ 9831669Sperrin if (buf != NULL) { /* immediate write */ 98410922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 9851669Sperrin /* test for truncation needs to be done while range locked */ 98610922SJeff.Bonwick@Sun.COM if (offset >= zp->z_phys->zp_size) { 9871669Sperrin error = ENOENT; 98810922SJeff.Bonwick@Sun.COM } else { 98910922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 99010922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 9911669Sperrin } 99210922SJeff.Bonwick@Sun.COM ASSERT(error == 0 || error == ENOENT); 9931669Sperrin } else { /* indirect write */ 994789Sahrens /* 9951669Sperrin * Have to lock the whole block to ensure when it's 9961669Sperrin * written out and it's checksum is being calculated 9971669Sperrin * that no one can change the data. We need to re-check 9981669Sperrin * blocksize after we get the lock in case it's changed! 999789Sahrens */ 10001669Sperrin for (;;) { 100110922SJeff.Bonwick@Sun.COM uint64_t blkoff; 100210922SJeff.Bonwick@Sun.COM size = zp->z_blksz; 100310945SJeff.Bonwick@Sun.COM blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 100410922SJeff.Bonwick@Sun.COM offset -= blkoff; 100510922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, 100610922SJeff.Bonwick@Sun.COM RL_READER); 100710922SJeff.Bonwick@Sun.COM if (zp->z_blksz == size) 10081669Sperrin break; 100910922SJeff.Bonwick@Sun.COM offset += blkoff; 101010922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 10111669Sperrin } 10121669Sperrin /* test for truncation needs to be done while range locked */ 101310945SJeff.Bonwick@Sun.COM if (lr->lr_offset >= zp->z_phys->zp_size) 10141669Sperrin error = ENOENT; 101510209SMark.Musante@Sun.COM #ifdef DEBUG 101610209SMark.Musante@Sun.COM if (zil_fault_io) { 101710209SMark.Musante@Sun.COM error = EIO; 101810209SMark.Musante@Sun.COM zil_fault_io = 0; 101910209SMark.Musante@Sun.COM } 102010209SMark.Musante@Sun.COM #endif 102110922SJeff.Bonwick@Sun.COM if (error == 0) 102210922SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db); 102310922SJeff.Bonwick@Sun.COM 102410800SNeil.Perrin@Sun.COM if (error == 0) { 102510922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 102610922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 102710922SJeff.Bonwick@Sun.COM 102810922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 102910922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 103010922SJeff.Bonwick@Sun.COM 103110922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 103210922SJeff.Bonwick@Sun.COM zfs_get_done, zgd); 103310922SJeff.Bonwick@Sun.COM ASSERT(error || lr->lr_length <= zp->z_blksz); 103410922SJeff.Bonwick@Sun.COM 103510800SNeil.Perrin@Sun.COM /* 103610922SJeff.Bonwick@Sun.COM * On success, we need to wait for the write I/O 103710922SJeff.Bonwick@Sun.COM * initiated by dmu_sync() to complete before we can 103810922SJeff.Bonwick@Sun.COM * release this dbuf. We will finish everything up 103910922SJeff.Bonwick@Sun.COM * in the zfs_get_done() callback. 104010800SNeil.Perrin@Sun.COM */ 104110922SJeff.Bonwick@Sun.COM if (error == 0) 104210922SJeff.Bonwick@Sun.COM return (0); 104310922SJeff.Bonwick@Sun.COM 104410922SJeff.Bonwick@Sun.COM if (error == EALREADY) { 104510922SJeff.Bonwick@Sun.COM lr->lr_common.lrc_txtype = TX_WRITE2; 104610922SJeff.Bonwick@Sun.COM error = 0; 104710922SJeff.Bonwick@Sun.COM } 104810800SNeil.Perrin@Sun.COM } 1049789Sahrens } 105010922SJeff.Bonwick@Sun.COM 105110922SJeff.Bonwick@Sun.COM zfs_get_done(zgd, error); 105210922SJeff.Bonwick@Sun.COM 1053789Sahrens return (error); 1054789Sahrens } 1055789Sahrens 1056789Sahrens /*ARGSUSED*/ 1057789Sahrens static int 10585331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 10595331Samw caller_context_t *ct) 1060789Sahrens { 1061789Sahrens znode_t *zp = VTOZ(vp); 1062789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1063789Sahrens int error; 1064789Sahrens 10655367Sahrens ZFS_ENTER(zfsvfs); 10665367Sahrens ZFS_VERIFY_ZP(zp); 10675331Samw 10685331Samw if (flag & V_ACE_MASK) 10695331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 10705331Samw else 10715331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 10725331Samw 1073789Sahrens ZFS_EXIT(zfsvfs); 1074789Sahrens return (error); 1075789Sahrens } 1076789Sahrens 1077789Sahrens /* 10789981STim.Haley@Sun.COM * If vnode is for a device return a specfs vnode instead. 10799981STim.Haley@Sun.COM */ 10809981STim.Haley@Sun.COM static int 10819981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr) 10829981STim.Haley@Sun.COM { 10839981STim.Haley@Sun.COM int error = 0; 10849981STim.Haley@Sun.COM 10859981STim.Haley@Sun.COM if (IS_DEVVP(*vpp)) { 10869981STim.Haley@Sun.COM struct vnode *svp; 10879981STim.Haley@Sun.COM 10889981STim.Haley@Sun.COM svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 10899981STim.Haley@Sun.COM VN_RELE(*vpp); 10909981STim.Haley@Sun.COM if (svp == NULL) 10919981STim.Haley@Sun.COM error = ENOSYS; 10929981STim.Haley@Sun.COM *vpp = svp; 10939981STim.Haley@Sun.COM } 10949981STim.Haley@Sun.COM return (error); 10959981STim.Haley@Sun.COM } 10969981STim.Haley@Sun.COM 10979981STim.Haley@Sun.COM 10989981STim.Haley@Sun.COM /* 1099789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1100789Sahrens * If it exists, return a held vnode reference for it. 1101789Sahrens * 1102789Sahrens * IN: dvp - vnode of directory to search. 1103789Sahrens * nm - name of entry to lookup. 1104789Sahrens * pnp - full pathname to lookup [UNUSED]. 1105789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1106789Sahrens * rdir - root directory vnode [UNUSED]. 1107789Sahrens * cr - credentials of caller. 11085331Samw * ct - caller context 11095331Samw * direntflags - directory lookup flags 11105331Samw * realpnp - returned pathname. 1111789Sahrens * 1112789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1113789Sahrens * 1114789Sahrens * RETURN: 0 if success 1115789Sahrens * error code if failure 1116789Sahrens * 1117789Sahrens * Timestamps: 1118789Sahrens * NA 1119789Sahrens */ 1120789Sahrens /* ARGSUSED */ 1121789Sahrens static int 1122789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 11235331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 11245331Samw int *direntflags, pathname_t *realpnp) 1125789Sahrens { 1126789Sahrens znode_t *zdp = VTOZ(dvp); 1127789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 11289981STim.Haley@Sun.COM int error = 0; 11299981STim.Haley@Sun.COM 11309981STim.Haley@Sun.COM /* fast path */ 11319981STim.Haley@Sun.COM if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 11329981STim.Haley@Sun.COM 11339981STim.Haley@Sun.COM if (dvp->v_type != VDIR) { 11349981STim.Haley@Sun.COM return (ENOTDIR); 11359981STim.Haley@Sun.COM } else if (zdp->z_dbuf == NULL) { 11369981STim.Haley@Sun.COM return (EIO); 11379981STim.Haley@Sun.COM } 11389981STim.Haley@Sun.COM 11399981STim.Haley@Sun.COM if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 11409981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11419981STim.Haley@Sun.COM if (!error) { 11429981STim.Haley@Sun.COM *vpp = dvp; 11439981STim.Haley@Sun.COM VN_HOLD(*vpp); 11449981STim.Haley@Sun.COM return (0); 11459981STim.Haley@Sun.COM } 11469981STim.Haley@Sun.COM return (error); 11479981STim.Haley@Sun.COM } else { 11489981STim.Haley@Sun.COM vnode_t *tvp = dnlc_lookup(dvp, nm); 11499981STim.Haley@Sun.COM 11509981STim.Haley@Sun.COM if (tvp) { 11519981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11529981STim.Haley@Sun.COM if (error) { 11539981STim.Haley@Sun.COM VN_RELE(tvp); 11549981STim.Haley@Sun.COM return (error); 11559981STim.Haley@Sun.COM } 11569981STim.Haley@Sun.COM if (tvp == DNLC_NO_VNODE) { 11579981STim.Haley@Sun.COM VN_RELE(tvp); 11589981STim.Haley@Sun.COM return (ENOENT); 11599981STim.Haley@Sun.COM } else { 11609981STim.Haley@Sun.COM *vpp = tvp; 11619981STim.Haley@Sun.COM return (specvp_check(vpp, cr)); 11629981STim.Haley@Sun.COM } 11639981STim.Haley@Sun.COM } 11649981STim.Haley@Sun.COM } 11659981STim.Haley@Sun.COM } 11669981STim.Haley@Sun.COM 11679981STim.Haley@Sun.COM DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1168789Sahrens 11695367Sahrens ZFS_ENTER(zfsvfs); 11705367Sahrens ZFS_VERIFY_ZP(zdp); 1171789Sahrens 1172789Sahrens *vpp = NULL; 1173789Sahrens 1174789Sahrens if (flags & LOOKUP_XATTR) { 1175789Sahrens /* 11763234Sck153898 * If the xattr property is off, refuse the lookup request. 11773234Sck153898 */ 11783234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 11793234Sck153898 ZFS_EXIT(zfsvfs); 11803234Sck153898 return (EINVAL); 11813234Sck153898 } 11823234Sck153898 11833234Sck153898 /* 1184789Sahrens * We don't allow recursive attributes.. 1185789Sahrens * Maybe someday we will. 1186789Sahrens */ 1187789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1188789Sahrens ZFS_EXIT(zfsvfs); 1189789Sahrens return (EINVAL); 1190789Sahrens } 1191789Sahrens 11923280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1193789Sahrens ZFS_EXIT(zfsvfs); 1194789Sahrens return (error); 1195789Sahrens } 1196789Sahrens 1197789Sahrens /* 1198789Sahrens * Do we have permission to get into attribute directory? 1199789Sahrens */ 1200789Sahrens 12015331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 12025331Samw B_FALSE, cr)) { 1203789Sahrens VN_RELE(*vpp); 12045331Samw *vpp = NULL; 1205789Sahrens } 1206789Sahrens 1207789Sahrens ZFS_EXIT(zfsvfs); 1208789Sahrens return (error); 1209789Sahrens } 1210789Sahrens 12111512Sek110237 if (dvp->v_type != VDIR) { 12121512Sek110237 ZFS_EXIT(zfsvfs); 12131460Smarks return (ENOTDIR); 12141512Sek110237 } 12151460Smarks 1216789Sahrens /* 1217789Sahrens * Check accessibility of directory. 1218789Sahrens */ 1219789Sahrens 12205331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1221789Sahrens ZFS_EXIT(zfsvfs); 1222789Sahrens return (error); 1223789Sahrens } 1224789Sahrens 12255498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 12265331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 12275331Samw ZFS_EXIT(zfsvfs); 12285331Samw return (EILSEQ); 12295331Samw } 12305331Samw 12315331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 12329981STim.Haley@Sun.COM if (error == 0) 12339981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1234789Sahrens 1235789Sahrens ZFS_EXIT(zfsvfs); 1236789Sahrens return (error); 1237789Sahrens } 1238789Sahrens 1239789Sahrens /* 1240789Sahrens * Attempt to create a new entry in a directory. If the entry 1241789Sahrens * already exists, truncate the file if permissible, else return 1242789Sahrens * an error. Return the vp of the created or trunc'd file. 1243789Sahrens * 1244789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1245789Sahrens * name - name of new file entry. 1246789Sahrens * vap - attributes of new file. 1247789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1248789Sahrens * mode - mode to open file with. 1249789Sahrens * cr - credentials of caller. 1250789Sahrens * flag - large file flag [UNUSED]. 12515331Samw * ct - caller context 12525331Samw * vsecp - ACL to be set 1253789Sahrens * 1254789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1255789Sahrens * 1256789Sahrens * RETURN: 0 if success 1257789Sahrens * error code if failure 1258789Sahrens * 1259789Sahrens * Timestamps: 1260789Sahrens * dvp - ctime|mtime updated if new entry created 1261789Sahrens * vp - ctime|mtime always, atime if new 1262789Sahrens */ 12635331Samw 1264789Sahrens /* ARGSUSED */ 1265789Sahrens static int 1266789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 12675331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 12685331Samw vsecattr_t *vsecp) 1269789Sahrens { 1270789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1271789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 12725326Sek110237 zilog_t *zilog; 12735326Sek110237 objset_t *os; 1274789Sahrens zfs_dirlock_t *dl; 1275789Sahrens dmu_tx_t *tx; 1276789Sahrens int error; 12777847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 12787847SMark.Shellenbaum@Sun.COM uid_t uid; 12797847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 12809179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 12819179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 12825331Samw 12835331Samw /* 12845331Samw * If we have an ephemeral id, ACL, or XVATTR then 12855331Samw * make sure file system is at proper version 12865331Samw */ 12875331Samw 12887847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 12897847SMark.Shellenbaum@Sun.COM if (ksid) 12907847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 12917847SMark.Shellenbaum@Sun.COM else 12927847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 12937847SMark.Shellenbaum@Sun.COM 12945331Samw if (zfsvfs->z_use_fuids == B_FALSE && 12955331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 12967847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 12975331Samw return (EINVAL); 1298789Sahrens 12995367Sahrens ZFS_ENTER(zfsvfs); 13005367Sahrens ZFS_VERIFY_ZP(dzp); 13015326Sek110237 os = zfsvfs->z_os; 13025326Sek110237 zilog = zfsvfs->z_log; 1303789Sahrens 13045498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 13055331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 13065331Samw ZFS_EXIT(zfsvfs); 13075331Samw return (EILSEQ); 13085331Samw } 13095331Samw 13105331Samw if (vap->va_mask & AT_XVATTR) { 13115331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 13125331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 13135331Samw ZFS_EXIT(zfsvfs); 13145331Samw return (error); 13155331Samw } 13165331Samw } 1317789Sahrens top: 1318789Sahrens *vpp = NULL; 1319789Sahrens 1320789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1321789Sahrens vap->va_mode &= ~VSVTX; 1322789Sahrens 1323789Sahrens if (*name == '\0') { 1324789Sahrens /* 1325789Sahrens * Null component name refers to the directory itself. 1326789Sahrens */ 1327789Sahrens VN_HOLD(dvp); 1328789Sahrens zp = dzp; 1329789Sahrens dl = NULL; 1330789Sahrens error = 0; 1331789Sahrens } else { 1332789Sahrens /* possible VN_HOLD(zp) */ 13335331Samw int zflg = 0; 13345331Samw 13355331Samw if (flag & FIGNORECASE) 13365331Samw zflg |= ZCILOOK; 13375331Samw 13385331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13395331Samw NULL, NULL); 13405331Samw if (error) { 1341789Sahrens if (strcmp(name, "..") == 0) 1342789Sahrens error = EISDIR; 1343789Sahrens ZFS_EXIT(zfsvfs); 1344789Sahrens return (error); 1345789Sahrens } 1346789Sahrens } 1347789Sahrens if (zp == NULL) { 13485331Samw uint64_t txtype; 13495331Samw 1350789Sahrens /* 1351789Sahrens * Create a new file object and update the directory 1352789Sahrens * to reference it. 1353789Sahrens */ 13545331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1355789Sahrens goto out; 1356789Sahrens } 1357789Sahrens 1358789Sahrens /* 1359789Sahrens * We only support the creation of regular files in 1360789Sahrens * extended attribute directories. 1361789Sahrens */ 1362789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1363789Sahrens (vap->va_type != VREG)) { 1364789Sahrens error = EINVAL; 1365789Sahrens goto out; 1366789Sahrens } 1367789Sahrens 13689179SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp, 13699179SMark.Shellenbaum@Sun.COM &acl_ids)) != 0) 13709179SMark.Shellenbaum@Sun.COM goto out; 13719396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 137210143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 13739396SMatthew.Ahrens@Sun.COM error = EDQUOT; 13749396SMatthew.Ahrens@Sun.COM goto out; 13759396SMatthew.Ahrens@Sun.COM } 13769179SMark.Shellenbaum@Sun.COM 1377789Sahrens tx = dmu_tx_create(os); 1378789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 13799179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 13809396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 13819396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 1382789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 13831544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 13849179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1385789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1386789Sahrens 0, SPA_MAXBLOCKSIZE); 13875331Samw } 13888227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1389789Sahrens if (error) { 13909179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1391789Sahrens zfs_dirent_unlock(dl); 13928227SNeil.Perrin@Sun.COM if (error == ERESTART) { 13932113Sahrens dmu_tx_wait(tx); 13942113Sahrens dmu_tx_abort(tx); 1395789Sahrens goto top; 1396789Sahrens } 13972113Sahrens dmu_tx_abort(tx); 1398789Sahrens ZFS_EXIT(zfsvfs); 1399789Sahrens return (error); 1400789Sahrens } 14019179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 14029179SMark.Shellenbaum@Sun.COM 14039179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 14049179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 14059179SMark.Shellenbaum@Sun.COM 1406789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 14079179SMark.Shellenbaum@Sun.COM 14085331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 14095331Samw if (flag & FIGNORECASE) 14105331Samw txtype |= TX_CI; 14115331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 14129179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 14139179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1414789Sahrens dmu_tx_commit(tx); 1415789Sahrens } else { 14165331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 14175331Samw 1418789Sahrens /* 1419789Sahrens * A directory entry already exists for this name. 1420789Sahrens */ 1421789Sahrens /* 1422789Sahrens * Can't truncate an existing file if in exclusive mode. 1423789Sahrens */ 1424789Sahrens if (excl == EXCL) { 1425789Sahrens error = EEXIST; 1426789Sahrens goto out; 1427789Sahrens } 1428789Sahrens /* 1429789Sahrens * Can't open a directory for writing. 1430789Sahrens */ 1431789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1432789Sahrens error = EISDIR; 1433789Sahrens goto out; 1434789Sahrens } 1435789Sahrens /* 1436789Sahrens * Verify requested access to file. 1437789Sahrens */ 14385331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1439789Sahrens goto out; 1440789Sahrens } 1441789Sahrens 1442789Sahrens mutex_enter(&dzp->z_lock); 1443789Sahrens dzp->z_seq++; 1444789Sahrens mutex_exit(&dzp->z_lock); 1445789Sahrens 14461878Smaybee /* 14471878Smaybee * Truncate regular files if requested. 14481878Smaybee */ 14491878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1450789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 14516992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 14526992Smaybee zfs_dirent_unlock(dl); 14536992Smaybee dl = NULL; 14541878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 14554863Spraks if (error == 0) { 14565331Samw vnevent_create(ZTOV(zp), ct); 14574863Spraks } 1458789Sahrens } 1459789Sahrens } 1460789Sahrens out: 1461789Sahrens 1462789Sahrens if (dl) 1463789Sahrens zfs_dirent_unlock(dl); 1464789Sahrens 1465789Sahrens if (error) { 1466789Sahrens if (zp) 1467789Sahrens VN_RELE(ZTOV(zp)); 1468789Sahrens } else { 1469789Sahrens *vpp = ZTOV(zp); 14709981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1471789Sahrens } 1472789Sahrens 1473789Sahrens ZFS_EXIT(zfsvfs); 1474789Sahrens return (error); 1475789Sahrens } 1476789Sahrens 1477789Sahrens /* 1478789Sahrens * Remove an entry from a directory. 1479789Sahrens * 1480789Sahrens * IN: dvp - vnode of directory to remove entry from. 1481789Sahrens * name - name of entry to remove. 1482789Sahrens * cr - credentials of caller. 14835331Samw * ct - caller context 14845331Samw * flags - case flags 1485789Sahrens * 1486789Sahrens * RETURN: 0 if success 1487789Sahrens * error code if failure 1488789Sahrens * 1489789Sahrens * Timestamps: 1490789Sahrens * dvp - ctime|mtime 1491789Sahrens * vp - ctime (if nlink > 0) 1492789Sahrens */ 14935331Samw /*ARGSUSED*/ 1494789Sahrens static int 14955331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 14965331Samw int flags) 1497789Sahrens { 1498789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1499789Sahrens znode_t *xzp = NULL; 1500789Sahrens vnode_t *vp; 1501789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15025326Sek110237 zilog_t *zilog; 1503789Sahrens uint64_t acl_obj, xattr_obj; 1504789Sahrens zfs_dirlock_t *dl; 1505789Sahrens dmu_tx_t *tx; 15063461Sahrens boolean_t may_delete_now, delete_now = FALSE; 15076992Smaybee boolean_t unlinked, toobig = FALSE; 15085331Samw uint64_t txtype; 15095331Samw pathname_t *realnmp = NULL; 15105331Samw pathname_t realnm; 1511789Sahrens int error; 15125331Samw int zflg = ZEXISTS; 1513789Sahrens 15145367Sahrens ZFS_ENTER(zfsvfs); 15155367Sahrens ZFS_VERIFY_ZP(dzp); 15165326Sek110237 zilog = zfsvfs->z_log; 1517789Sahrens 15185331Samw if (flags & FIGNORECASE) { 15195331Samw zflg |= ZCILOOK; 15205331Samw pn_alloc(&realnm); 15215331Samw realnmp = &realnm; 15225331Samw } 15235331Samw 1524789Sahrens top: 1525789Sahrens /* 1526789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1527789Sahrens */ 15285331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 15295331Samw NULL, realnmp)) { 15305331Samw if (realnmp) 15315331Samw pn_free(realnmp); 1532789Sahrens ZFS_EXIT(zfsvfs); 1533789Sahrens return (error); 1534789Sahrens } 1535789Sahrens 1536789Sahrens vp = ZTOV(zp); 1537789Sahrens 1538789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1539789Sahrens goto out; 1540789Sahrens } 1541789Sahrens 1542789Sahrens /* 1543789Sahrens * Need to use rmdir for removing directories. 1544789Sahrens */ 1545789Sahrens if (vp->v_type == VDIR) { 1546789Sahrens error = EPERM; 1547789Sahrens goto out; 1548789Sahrens } 1549789Sahrens 15505331Samw vnevent_remove(vp, dvp, name, ct); 15515331Samw 15525331Samw if (realnmp) 15536492Stimh dnlc_remove(dvp, realnmp->pn_buf); 15545331Samw else 15555331Samw dnlc_remove(dvp, name); 15561484Sek110237 1557789Sahrens mutex_enter(&vp->v_lock); 1558789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1559789Sahrens mutex_exit(&vp->v_lock); 1560789Sahrens 1561789Sahrens /* 15623461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1563789Sahrens * it depends on whether we're the last link, and on whether there are 1564789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1565789Sahrens * allow for either case. 1566789Sahrens */ 1567789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 15681544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1569789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 15706992Smaybee if (may_delete_now) { 15716992Smaybee toobig = 15726992Smaybee zp->z_phys->zp_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 15736992Smaybee /* if the file is too big, only hold_free a token amount */ 15746992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 15756992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 15766992Smaybee } 1577789Sahrens 1578789Sahrens /* are there any extended attributes? */ 1579789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1580789Sahrens /* XXX - do we need this if we are deleting? */ 1581789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1582789Sahrens } 1583789Sahrens 1584789Sahrens /* are there any additional acls */ 1585789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1586789Sahrens may_delete_now) 1587789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1588789Sahrens 1589789Sahrens /* charge as an update -- would be nice not to charge at all */ 15903461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1591789Sahrens 15928227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1593789Sahrens if (error) { 1594789Sahrens zfs_dirent_unlock(dl); 1595789Sahrens VN_RELE(vp); 15968227SNeil.Perrin@Sun.COM if (error == ERESTART) { 15972113Sahrens dmu_tx_wait(tx); 15982113Sahrens dmu_tx_abort(tx); 1599789Sahrens goto top; 1600789Sahrens } 16015331Samw if (realnmp) 16025331Samw pn_free(realnmp); 16032113Sahrens dmu_tx_abort(tx); 1604789Sahrens ZFS_EXIT(zfsvfs); 1605789Sahrens return (error); 1606789Sahrens } 1607789Sahrens 1608789Sahrens /* 1609789Sahrens * Remove the directory entry. 1610789Sahrens */ 16115331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1612789Sahrens 1613789Sahrens if (error) { 1614789Sahrens dmu_tx_commit(tx); 1615789Sahrens goto out; 1616789Sahrens } 1617789Sahrens 16183461Sahrens if (unlinked) { 1619789Sahrens mutex_enter(&vp->v_lock); 16206992Smaybee delete_now = may_delete_now && !toobig && 1621789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1622789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1623789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1624789Sahrens mutex_exit(&vp->v_lock); 1625789Sahrens } 1626789Sahrens 1627789Sahrens if (delete_now) { 1628789Sahrens if (zp->z_phys->zp_xattr) { 1629789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1630789Sahrens ASSERT3U(error, ==, 0); 1631789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1632789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1633789Sahrens mutex_enter(&xzp->z_lock); 16343461Sahrens xzp->z_unlinked = 1; 1635789Sahrens xzp->z_phys->zp_links = 0; 1636789Sahrens mutex_exit(&xzp->z_lock); 16373461Sahrens zfs_unlinked_add(xzp, tx); 1638789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1639789Sahrens } 1640789Sahrens mutex_enter(&zp->z_lock); 1641789Sahrens mutex_enter(&vp->v_lock); 1642789Sahrens vp->v_count--; 1643789Sahrens ASSERT3U(vp->v_count, ==, 0); 1644789Sahrens mutex_exit(&vp->v_lock); 1645789Sahrens mutex_exit(&zp->z_lock); 1646789Sahrens zfs_znode_delete(zp, tx); 16473461Sahrens } else if (unlinked) { 16483461Sahrens zfs_unlinked_add(zp, tx); 1649789Sahrens } 1650789Sahrens 16515331Samw txtype = TX_REMOVE; 16525331Samw if (flags & FIGNORECASE) 16535331Samw txtype |= TX_CI; 16545331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 1655789Sahrens 1656789Sahrens dmu_tx_commit(tx); 1657789Sahrens out: 16585331Samw if (realnmp) 16595331Samw pn_free(realnmp); 16605331Samw 1661789Sahrens zfs_dirent_unlock(dl); 1662789Sahrens 1663789Sahrens if (!delete_now) { 1664789Sahrens VN_RELE(vp); 1665789Sahrens } else if (xzp) { 16666992Smaybee /* this rele is delayed to prevent nesting transactions */ 1667789Sahrens VN_RELE(ZTOV(xzp)); 1668789Sahrens } 1669789Sahrens 1670789Sahrens ZFS_EXIT(zfsvfs); 1671789Sahrens return (error); 1672789Sahrens } 1673789Sahrens 1674789Sahrens /* 1675789Sahrens * Create a new directory and insert it into dvp using the name 1676789Sahrens * provided. Return a pointer to the inserted directory. 1677789Sahrens * 1678789Sahrens * IN: dvp - vnode of directory to add subdir to. 1679789Sahrens * dirname - name of new directory. 1680789Sahrens * vap - attributes of new directory. 1681789Sahrens * cr - credentials of caller. 16825331Samw * ct - caller context 16835331Samw * vsecp - ACL to be set 1684789Sahrens * 1685789Sahrens * OUT: vpp - vnode of created directory. 1686789Sahrens * 1687789Sahrens * RETURN: 0 if success 1688789Sahrens * error code if failure 1689789Sahrens * 1690789Sahrens * Timestamps: 1691789Sahrens * dvp - ctime|mtime updated 1692789Sahrens * vp - ctime|mtime|atime updated 1693789Sahrens */ 16945331Samw /*ARGSUSED*/ 1695789Sahrens static int 16965331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 16975331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1698789Sahrens { 1699789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1700789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17015326Sek110237 zilog_t *zilog; 1702789Sahrens zfs_dirlock_t *dl; 17035331Samw uint64_t txtype; 1704789Sahrens dmu_tx_t *tx; 1705789Sahrens int error; 17065331Samw int zf = ZNEW; 17077847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 17087847SMark.Shellenbaum@Sun.COM uid_t uid; 17097847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 17109179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 17119179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1712789Sahrens 1713789Sahrens ASSERT(vap->va_type == VDIR); 1714789Sahrens 17155331Samw /* 17165331Samw * If we have an ephemeral id, ACL, or XVATTR then 17175331Samw * make sure file system is at proper version 17185331Samw */ 17195331Samw 17207847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 17217847SMark.Shellenbaum@Sun.COM if (ksid) 17227847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 17237847SMark.Shellenbaum@Sun.COM else 17247847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 17255331Samw if (zfsvfs->z_use_fuids == B_FALSE && 17267847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 17277876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 17285331Samw return (EINVAL); 17295331Samw 17305367Sahrens ZFS_ENTER(zfsvfs); 17315367Sahrens ZFS_VERIFY_ZP(dzp); 17325326Sek110237 zilog = zfsvfs->z_log; 1733789Sahrens 1734789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1735789Sahrens ZFS_EXIT(zfsvfs); 1736789Sahrens return (EINVAL); 1737789Sahrens } 17385331Samw 17395498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 17405331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 17415331Samw ZFS_EXIT(zfsvfs); 17425331Samw return (EILSEQ); 17435331Samw } 17445331Samw if (flags & FIGNORECASE) 17455331Samw zf |= ZCILOOK; 17465331Samw 17475331Samw if (vap->va_mask & AT_XVATTR) 17485331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 17495331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 17505331Samw ZFS_EXIT(zfsvfs); 17515331Samw return (error); 17525331Samw } 1753789Sahrens 1754789Sahrens /* 1755789Sahrens * First make sure the new directory doesn't exist. 1756789Sahrens */ 17575331Samw top: 17585331Samw *vpp = NULL; 17595331Samw 17605331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 17615331Samw NULL, NULL)) { 1762789Sahrens ZFS_EXIT(zfsvfs); 1763789Sahrens return (error); 1764789Sahrens } 1765789Sahrens 17665331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 17671231Smarks zfs_dirent_unlock(dl); 17681231Smarks ZFS_EXIT(zfsvfs); 17691231Smarks return (error); 17701231Smarks } 17711231Smarks 17729179SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, vsecp, 17739179SMark.Shellenbaum@Sun.COM &acl_ids)) != 0) { 17749179SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 17759179SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 17769179SMark.Shellenbaum@Sun.COM return (error); 17775331Samw } 17789396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 177910143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 17809396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 17819396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 17829396SMatthew.Ahrens@Sun.COM return (EDQUOT); 17839396SMatthew.Ahrens@Sun.COM } 17849179SMark.Shellenbaum@Sun.COM 1785789Sahrens /* 1786789Sahrens * Add a new entry to the directory. 1787789Sahrens */ 1788789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 17891544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 17901544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 17919179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 17929396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 17939396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 17949179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 1795789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1796789Sahrens 0, SPA_MAXBLOCKSIZE); 17978227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1798789Sahrens if (error) { 17999179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1800789Sahrens zfs_dirent_unlock(dl); 18018227SNeil.Perrin@Sun.COM if (error == ERESTART) { 18022113Sahrens dmu_tx_wait(tx); 18032113Sahrens dmu_tx_abort(tx); 1804789Sahrens goto top; 1805789Sahrens } 18062113Sahrens dmu_tx_abort(tx); 1807789Sahrens ZFS_EXIT(zfsvfs); 1808789Sahrens return (error); 1809789Sahrens } 1810789Sahrens 1811789Sahrens /* 1812789Sahrens * Create new node. 1813789Sahrens */ 18149179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 18159179SMark.Shellenbaum@Sun.COM 18169179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 18179179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 1818789Sahrens /* 1819789Sahrens * Now put new name in parent dir. 1820789Sahrens */ 1821789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1822789Sahrens 1823789Sahrens *vpp = ZTOV(zp); 1824789Sahrens 18255331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 18265331Samw if (flags & FIGNORECASE) 18275331Samw txtype |= TX_CI; 18289179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 18299179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 18309179SMark.Shellenbaum@Sun.COM 18319179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1832789Sahrens dmu_tx_commit(tx); 1833789Sahrens 1834789Sahrens zfs_dirent_unlock(dl); 1835789Sahrens 1836789Sahrens ZFS_EXIT(zfsvfs); 1837789Sahrens return (0); 1838789Sahrens } 1839789Sahrens 1840789Sahrens /* 1841789Sahrens * Remove a directory subdir entry. If the current working 1842789Sahrens * directory is the same as the subdir to be removed, the 1843789Sahrens * remove will fail. 1844789Sahrens * 1845789Sahrens * IN: dvp - vnode of directory to remove from. 1846789Sahrens * name - name of directory to be removed. 1847789Sahrens * cwd - vnode of current working directory. 1848789Sahrens * cr - credentials of caller. 18495331Samw * ct - caller context 18505331Samw * flags - case flags 1851789Sahrens * 1852789Sahrens * RETURN: 0 if success 1853789Sahrens * error code if failure 1854789Sahrens * 1855789Sahrens * Timestamps: 1856789Sahrens * dvp - ctime|mtime updated 1857789Sahrens */ 18585331Samw /*ARGSUSED*/ 1859789Sahrens static int 18605331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 18615331Samw caller_context_t *ct, int flags) 1862789Sahrens { 1863789Sahrens znode_t *dzp = VTOZ(dvp); 1864789Sahrens znode_t *zp; 1865789Sahrens vnode_t *vp; 1866789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 18675326Sek110237 zilog_t *zilog; 1868789Sahrens zfs_dirlock_t *dl; 1869789Sahrens dmu_tx_t *tx; 1870789Sahrens int error; 18715331Samw int zflg = ZEXISTS; 1872789Sahrens 18735367Sahrens ZFS_ENTER(zfsvfs); 18745367Sahrens ZFS_VERIFY_ZP(dzp); 18755326Sek110237 zilog = zfsvfs->z_log; 1876789Sahrens 18775331Samw if (flags & FIGNORECASE) 18785331Samw zflg |= ZCILOOK; 1879789Sahrens top: 1880789Sahrens zp = NULL; 1881789Sahrens 1882789Sahrens /* 1883789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1884789Sahrens */ 18855331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 18865331Samw NULL, NULL)) { 1887789Sahrens ZFS_EXIT(zfsvfs); 1888789Sahrens return (error); 1889789Sahrens } 1890789Sahrens 1891789Sahrens vp = ZTOV(zp); 1892789Sahrens 1893789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1894789Sahrens goto out; 1895789Sahrens } 1896789Sahrens 1897789Sahrens if (vp->v_type != VDIR) { 1898789Sahrens error = ENOTDIR; 1899789Sahrens goto out; 1900789Sahrens } 1901789Sahrens 1902789Sahrens if (vp == cwd) { 1903789Sahrens error = EINVAL; 1904789Sahrens goto out; 1905789Sahrens } 1906789Sahrens 19075331Samw vnevent_rmdir(vp, dvp, name, ct); 1908789Sahrens 1909789Sahrens /* 19103897Smaybee * Grab a lock on the directory to make sure that noone is 19113897Smaybee * trying to add (or lookup) entries while we are removing it. 19123897Smaybee */ 19133897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 19143897Smaybee 19153897Smaybee /* 19163897Smaybee * Grab a lock on the parent pointer to make sure we play well 1917789Sahrens * with the treewalk and directory rename code. 1918789Sahrens */ 1919789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1920789Sahrens 1921789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 19221544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1923789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 19243461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 19258227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1926789Sahrens if (error) { 1927789Sahrens rw_exit(&zp->z_parent_lock); 19283897Smaybee rw_exit(&zp->z_name_lock); 1929789Sahrens zfs_dirent_unlock(dl); 1930789Sahrens VN_RELE(vp); 19318227SNeil.Perrin@Sun.COM if (error == ERESTART) { 19322113Sahrens dmu_tx_wait(tx); 19332113Sahrens dmu_tx_abort(tx); 1934789Sahrens goto top; 1935789Sahrens } 19362113Sahrens dmu_tx_abort(tx); 1937789Sahrens ZFS_EXIT(zfsvfs); 1938789Sahrens return (error); 1939789Sahrens } 1940789Sahrens 19415331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 19425331Samw 19435331Samw if (error == 0) { 19445331Samw uint64_t txtype = TX_RMDIR; 19455331Samw if (flags & FIGNORECASE) 19465331Samw txtype |= TX_CI; 19475331Samw zfs_log_remove(zilog, tx, txtype, dzp, name); 19485331Samw } 1949789Sahrens 1950789Sahrens dmu_tx_commit(tx); 1951789Sahrens 1952789Sahrens rw_exit(&zp->z_parent_lock); 19533897Smaybee rw_exit(&zp->z_name_lock); 1954789Sahrens out: 1955789Sahrens zfs_dirent_unlock(dl); 1956789Sahrens 1957789Sahrens VN_RELE(vp); 1958789Sahrens 1959789Sahrens ZFS_EXIT(zfsvfs); 1960789Sahrens return (error); 1961789Sahrens } 1962789Sahrens 1963789Sahrens /* 1964789Sahrens * Read as many directory entries as will fit into the provided 1965789Sahrens * buffer from the given directory cursor position (specified in 1966789Sahrens * the uio structure. 1967789Sahrens * 1968789Sahrens * IN: vp - vnode of directory to read. 1969789Sahrens * uio - structure supplying read location, range info, 1970789Sahrens * and return buffer. 1971789Sahrens * cr - credentials of caller. 19725331Samw * ct - caller context 19735331Samw * flags - case flags 1974789Sahrens * 1975789Sahrens * OUT: uio - updated offset and range, buffer filled. 1976789Sahrens * eofp - set to true if end-of-file detected. 1977789Sahrens * 1978789Sahrens * RETURN: 0 if success 1979789Sahrens * error code if failure 1980789Sahrens * 1981789Sahrens * Timestamps: 1982789Sahrens * vp - atime updated 1983789Sahrens * 1984789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1985789Sahrens * This allows us to use the low range for "special" directory entries: 1986789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1987789Sahrens * we use the offset 2 for the '.zfs' directory. 1988789Sahrens */ 1989789Sahrens /* ARGSUSED */ 1990789Sahrens static int 19915331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 19925331Samw caller_context_t *ct, int flags) 1993789Sahrens { 1994789Sahrens znode_t *zp = VTOZ(vp); 1995789Sahrens iovec_t *iovp; 19965331Samw edirent_t *eodp; 1997789Sahrens dirent64_t *odp; 1998789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1999869Sperrin objset_t *os; 2000789Sahrens caddr_t outbuf; 2001789Sahrens size_t bufsize; 2002789Sahrens zap_cursor_t zc; 2003789Sahrens zap_attribute_t zap; 2004789Sahrens uint_t bytes_wanted; 2005789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 2006789Sahrens int local_eof; 2007869Sperrin int outcount; 2008869Sperrin int error; 2009869Sperrin uint8_t prefetch; 20105663Sck153898 boolean_t check_sysattrs; 2011789Sahrens 20125367Sahrens ZFS_ENTER(zfsvfs); 20135367Sahrens ZFS_VERIFY_ZP(zp); 2014789Sahrens 2015789Sahrens /* 2016789Sahrens * If we are not given an eof variable, 2017789Sahrens * use a local one. 2018789Sahrens */ 2019789Sahrens if (eofp == NULL) 2020789Sahrens eofp = &local_eof; 2021789Sahrens 2022789Sahrens /* 2023789Sahrens * Check for valid iov_len. 2024789Sahrens */ 2025789Sahrens if (uio->uio_iov->iov_len <= 0) { 2026789Sahrens ZFS_EXIT(zfsvfs); 2027789Sahrens return (EINVAL); 2028789Sahrens } 2029789Sahrens 2030789Sahrens /* 2031789Sahrens * Quit if directory has been removed (posix) 2032789Sahrens */ 20333461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 2034789Sahrens ZFS_EXIT(zfsvfs); 2035789Sahrens return (0); 2036789Sahrens } 2037789Sahrens 2038869Sperrin error = 0; 2039869Sperrin os = zfsvfs->z_os; 2040869Sperrin offset = uio->uio_loffset; 2041869Sperrin prefetch = zp->z_zn_prefetch; 2042869Sperrin 2043789Sahrens /* 2044789Sahrens * Initialize the iterator cursor. 2045789Sahrens */ 2046789Sahrens if (offset <= 3) { 2047789Sahrens /* 2048789Sahrens * Start iteration from the beginning of the directory. 2049789Sahrens */ 2050869Sperrin zap_cursor_init(&zc, os, zp->z_id); 2051789Sahrens } else { 2052789Sahrens /* 2053789Sahrens * The offset is a serialized cursor. 2054789Sahrens */ 2055869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2056789Sahrens } 2057789Sahrens 2058789Sahrens /* 2059789Sahrens * Get space to change directory entries into fs independent format. 2060789Sahrens */ 2061789Sahrens iovp = uio->uio_iov; 2062789Sahrens bytes_wanted = iovp->iov_len; 2063789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2064789Sahrens bufsize = bytes_wanted; 2065789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 2066789Sahrens odp = (struct dirent64 *)outbuf; 2067789Sahrens } else { 2068789Sahrens bufsize = bytes_wanted; 2069789Sahrens odp = (struct dirent64 *)iovp->iov_base; 2070789Sahrens } 20715331Samw eodp = (struct edirent *)odp; 2072789Sahrens 2073789Sahrens /* 20747757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 20757757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 20767757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 20777757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 20785663Sck153898 */ 20797757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 20805663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 20815663Sck153898 (flags & V_RDDIR_ENTFLAGS); 20825663Sck153898 20835663Sck153898 /* 2084789Sahrens * Transform to file-system independent format 2085789Sahrens */ 2086789Sahrens outcount = 0; 2087789Sahrens while (outcount < bytes_wanted) { 20883912Slling ino64_t objnum; 20893912Slling ushort_t reclen; 20903912Slling off64_t *next; 20913912Slling 2092789Sahrens /* 2093789Sahrens * Special case `.', `..', and `.zfs'. 2094789Sahrens */ 2095789Sahrens if (offset == 0) { 2096789Sahrens (void) strcpy(zap.za_name, "."); 20975331Samw zap.za_normalization_conflict = 0; 20983912Slling objnum = zp->z_id; 2099789Sahrens } else if (offset == 1) { 2100789Sahrens (void) strcpy(zap.za_name, ".."); 21015331Samw zap.za_normalization_conflict = 0; 21023912Slling objnum = zp->z_phys->zp_parent; 2103789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2104789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 21055331Samw zap.za_normalization_conflict = 0; 21063912Slling objnum = ZFSCTL_INO_ROOT; 2107789Sahrens } else { 2108789Sahrens /* 2109789Sahrens * Grab next entry. 2110789Sahrens */ 2111789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2112789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2113789Sahrens break; 2114789Sahrens else 2115789Sahrens goto update; 2116789Sahrens } 2117789Sahrens 2118789Sahrens if (zap.za_integer_length != 8 || 2119789Sahrens zap.za_num_integers != 1) { 2120789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2121789Sahrens "entry, obj = %lld, offset = %lld\n", 2122789Sahrens (u_longlong_t)zp->z_id, 2123789Sahrens (u_longlong_t)offset); 2124789Sahrens error = ENXIO; 2125789Sahrens goto update; 2126789Sahrens } 21273912Slling 21283912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 21293912Slling /* 21303912Slling * MacOS X can extract the object type here such as: 21313912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 21323912Slling */ 21335663Sck153898 21345663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 21355663Sck153898 zap.za_normalization_conflict = 21365663Sck153898 xattr_sysattr_casechk(zap.za_name); 21375663Sck153898 } 2138789Sahrens } 21395331Samw 21409749STim.Haley@Sun.COM if (flags & V_RDDIR_ACCFILTER) { 21419749STim.Haley@Sun.COM /* 21429749STim.Haley@Sun.COM * If we have no access at all, don't include 21439749STim.Haley@Sun.COM * this entry in the returned information 21449749STim.Haley@Sun.COM */ 21459749STim.Haley@Sun.COM znode_t *ezp; 21469749STim.Haley@Sun.COM if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 21479749STim.Haley@Sun.COM goto skip_entry; 21489749STim.Haley@Sun.COM if (!zfs_has_access(ezp, cr)) { 21499749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 21509749STim.Haley@Sun.COM goto skip_entry; 21519749STim.Haley@Sun.COM } 21529749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 21539749STim.Haley@Sun.COM } 21549749STim.Haley@Sun.COM 21555331Samw if (flags & V_RDDIR_ENTFLAGS) 21565331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 21575331Samw else 21585331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2159789Sahrens 2160789Sahrens /* 2161789Sahrens * Will this entry fit in the buffer? 2162789Sahrens */ 21633912Slling if (outcount + reclen > bufsize) { 2164789Sahrens /* 2165789Sahrens * Did we manage to fit anything in the buffer? 2166789Sahrens */ 2167789Sahrens if (!outcount) { 2168789Sahrens error = EINVAL; 2169789Sahrens goto update; 2170789Sahrens } 2171789Sahrens break; 2172789Sahrens } 21735331Samw if (flags & V_RDDIR_ENTFLAGS) { 21745331Samw /* 21755331Samw * Add extended flag entry: 21765331Samw */ 21775331Samw eodp->ed_ino = objnum; 21785331Samw eodp->ed_reclen = reclen; 21795331Samw /* NOTE: ed_off is the offset for the *next* entry */ 21805331Samw next = &(eodp->ed_off); 21815331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 21825331Samw ED_CASE_CONFLICT : 0; 21835331Samw (void) strncpy(eodp->ed_name, zap.za_name, 21845331Samw EDIRENT_NAMELEN(reclen)); 21855331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 21865331Samw } else { 21875331Samw /* 21885331Samw * Add normal entry: 21895331Samw */ 21905331Samw odp->d_ino = objnum; 21915331Samw odp->d_reclen = reclen; 21925331Samw /* NOTE: d_off is the offset for the *next* entry */ 21935331Samw next = &(odp->d_off); 21945331Samw (void) strncpy(odp->d_name, zap.za_name, 21955331Samw DIRENT64_NAMELEN(reclen)); 21965331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 21975331Samw } 21983912Slling outcount += reclen; 2199789Sahrens 2200789Sahrens ASSERT(outcount <= bufsize); 2201789Sahrens 2202789Sahrens /* Prefetch znode */ 2203869Sperrin if (prefetch) 22043912Slling dmu_prefetch(os, objnum, 0, 0); 2205789Sahrens 22069749STim.Haley@Sun.COM skip_entry: 2207789Sahrens /* 2208789Sahrens * Move to the next entry, fill in the previous offset. 2209789Sahrens */ 2210789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2211789Sahrens zap_cursor_advance(&zc); 2212789Sahrens offset = zap_cursor_serialize(&zc); 2213789Sahrens } else { 2214789Sahrens offset += 1; 2215789Sahrens } 2216789Sahrens *next = offset; 2217789Sahrens } 2218869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2219789Sahrens 2220789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2221789Sahrens iovp->iov_base += outcount; 2222789Sahrens iovp->iov_len -= outcount; 2223789Sahrens uio->uio_resid -= outcount; 2224789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2225789Sahrens /* 2226789Sahrens * Reset the pointer. 2227789Sahrens */ 2228789Sahrens offset = uio->uio_loffset; 2229789Sahrens } 2230789Sahrens 2231789Sahrens update: 2232885Sahrens zap_cursor_fini(&zc); 2233789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2234789Sahrens kmem_free(outbuf, bufsize); 2235789Sahrens 2236789Sahrens if (error == ENOENT) 2237789Sahrens error = 0; 2238789Sahrens 2239789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2240789Sahrens 2241789Sahrens uio->uio_loffset = offset; 2242789Sahrens ZFS_EXIT(zfsvfs); 2243789Sahrens return (error); 2244789Sahrens } 2245789Sahrens 22464720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 22474720Sfr157268 2248789Sahrens static int 22495331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2250789Sahrens { 2251789Sahrens znode_t *zp = VTOZ(vp); 2252789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2253789Sahrens 22541773Seschrock /* 22551773Seschrock * Regardless of whether this is required for standards conformance, 22561773Seschrock * this is the logical behavior when fsync() is called on a file with 22571773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 22581773Seschrock * going to be pushed out as part of the zil_commit(). 22591773Seschrock */ 22601773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 22611773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 22625331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 22631773Seschrock 22644720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 22654720Sfr157268 22665367Sahrens ZFS_ENTER(zfsvfs); 22675367Sahrens ZFS_VERIFY_ZP(zp); 22682638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 2269789Sahrens ZFS_EXIT(zfsvfs); 2270789Sahrens return (0); 2271789Sahrens } 2272789Sahrens 22735331Samw 2274789Sahrens /* 2275789Sahrens * Get the requested file attributes and place them in the provided 2276789Sahrens * vattr structure. 2277789Sahrens * 2278789Sahrens * IN: vp - vnode of file. 2279789Sahrens * vap - va_mask identifies requested attributes. 22805331Samw * If AT_XVATTR set, then optional attrs are requested 22815331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2282789Sahrens * cr - credentials of caller. 22835331Samw * ct - caller context 2284789Sahrens * 2285789Sahrens * OUT: vap - attribute values. 2286789Sahrens * 2287789Sahrens * RETURN: 0 (always succeeds) 2288789Sahrens */ 2289789Sahrens /* ARGSUSED */ 2290789Sahrens static int 22915331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 22925331Samw caller_context_t *ct) 2293789Sahrens { 2294789Sahrens znode_t *zp = VTOZ(vp); 2295789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 22965326Sek110237 znode_phys_t *pzp; 22975331Samw int error = 0; 22984543Smarks uint64_t links; 22995331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23005331Samw xoptattr_t *xoap = NULL; 23015331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 2302789Sahrens 23035367Sahrens ZFS_ENTER(zfsvfs); 23045367Sahrens ZFS_VERIFY_ZP(zp); 23055326Sek110237 pzp = zp->z_phys; 2306789Sahrens 23075331Samw /* 23085331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 23095331Samw * Also, if we are the owner don't bother, since owner should 23105331Samw * always be allowed to read basic attributes of file. 23115331Samw */ 23125331Samw if (!(pzp->zp_flags & ZFS_ACL_TRIVIAL) && 23135331Samw (pzp->zp_uid != crgetuid(cr))) { 23145331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 23155331Samw skipaclchk, cr)) { 23165331Samw ZFS_EXIT(zfsvfs); 23175331Samw return (error); 23185331Samw } 23195331Samw } 23205331Samw 2321789Sahrens /* 2322789Sahrens * Return all attributes. It's cheaper to provide the answer 2323789Sahrens * than to determine whether we were asked the question. 2324789Sahrens */ 2325789Sahrens 23269774SRay.Hassan@Sun.COM mutex_enter(&zp->z_lock); 2327789Sahrens vap->va_type = vp->v_type; 2328789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 23295771Sjp151216 zfs_fuid_map_ids(zp, cr, &vap->va_uid, &vap->va_gid); 2330789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2331789Sahrens vap->va_nodeid = zp->z_id; 23324543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 23334543Smarks links = pzp->zp_links + 1; 23344543Smarks else 23354543Smarks links = pzp->zp_links; 23364543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 2337789Sahrens vap->va_size = pzp->zp_size; 23381816Smarks vap->va_rdev = vp->v_rdev; 2339789Sahrens vap->va_seq = zp->z_seq; 2340789Sahrens 23415331Samw /* 23425331Samw * Add in any requested optional attributes and the create time. 23435331Samw * Also set the corresponding bits in the returned attribute bitmap. 23445331Samw */ 23455331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 23465331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 23475331Samw xoap->xoa_archive = 23485331Samw ((pzp->zp_flags & ZFS_ARCHIVE) != 0); 23495331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 23505331Samw } 23515331Samw 23525331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 23535331Samw xoap->xoa_readonly = 23545331Samw ((pzp->zp_flags & ZFS_READONLY) != 0); 23555331Samw XVA_SET_RTN(xvap, XAT_READONLY); 23565331Samw } 23575331Samw 23585331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 23595331Samw xoap->xoa_system = 23605331Samw ((pzp->zp_flags & ZFS_SYSTEM) != 0); 23615331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 23625331Samw } 23635331Samw 23645331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 23655331Samw xoap->xoa_hidden = 23665331Samw ((pzp->zp_flags & ZFS_HIDDEN) != 0); 23675331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 23685331Samw } 23695331Samw 23705331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 23715331Samw xoap->xoa_nounlink = 23725331Samw ((pzp->zp_flags & ZFS_NOUNLINK) != 0); 23735331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 23745331Samw } 23755331Samw 23765331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 23775331Samw xoap->xoa_immutable = 23785331Samw ((pzp->zp_flags & ZFS_IMMUTABLE) != 0); 23795331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 23805331Samw } 23815331Samw 23825331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 23835331Samw xoap->xoa_appendonly = 23845331Samw ((pzp->zp_flags & ZFS_APPENDONLY) != 0); 23855331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 23865331Samw } 23875331Samw 23885331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 23895331Samw xoap->xoa_nodump = 23905331Samw ((pzp->zp_flags & ZFS_NODUMP) != 0); 23915331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 23925331Samw } 23935331Samw 23945331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 23955331Samw xoap->xoa_opaque = 23965331Samw ((pzp->zp_flags & ZFS_OPAQUE) != 0); 23975331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 23985331Samw } 23995331Samw 24005331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 24015331Samw xoap->xoa_av_quarantined = 24025331Samw ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0); 24035331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 24045331Samw } 24055331Samw 24065331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 24075331Samw xoap->xoa_av_modified = 24085331Samw ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0); 24095331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 24105331Samw } 24115331Samw 24125331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 24135331Samw vp->v_type == VREG && 24145331Samw (pzp->zp_flags & ZFS_BONUS_SCANSTAMP)) { 24155331Samw size_t len; 24165331Samw dmu_object_info_t doi; 24175331Samw 24185331Samw /* 24195331Samw * Only VREG files have anti-virus scanstamps, so we 24205331Samw * won't conflict with symlinks in the bonus buffer. 24215331Samw */ 24225331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 24235331Samw len = sizeof (xoap->xoa_av_scanstamp) + 24245331Samw sizeof (znode_phys_t); 24255331Samw if (len <= doi.doi_bonus_size) { 24265331Samw /* 24275331Samw * pzp points to the start of the 24285331Samw * znode_phys_t. pzp + 1 points to the 24295331Samw * first byte after the znode_phys_t. 24305331Samw */ 24315331Samw (void) memcpy(xoap->xoa_av_scanstamp, 24325331Samw pzp + 1, 24335331Samw sizeof (xoap->xoa_av_scanstamp)); 24345331Samw XVA_SET_RTN(xvap, XAT_AV_SCANSTAMP); 24355331Samw } 24365331Samw } 24375331Samw 24385331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 24395331Samw ZFS_TIME_DECODE(&xoap->xoa_createtime, pzp->zp_crtime); 24405331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 24415331Samw } 244210793Sdai.ngo@sun.com 244310793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 244410793Sdai.ngo@sun.com xoap->xoa_reparse = 244510793Sdai.ngo@sun.com ((pzp->zp_flags & ZFS_REPARSE) != 0); 244610793Sdai.ngo@sun.com XVA_SET_RTN(xvap, XAT_REPARSE); 244710793Sdai.ngo@sun.com } 24485331Samw } 24495331Samw 2450789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 2451789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 2452789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 2453789Sahrens 2454789Sahrens mutex_exit(&zp->z_lock); 2455789Sahrens 2456789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 2457789Sahrens 2458789Sahrens if (zp->z_blksz == 0) { 2459789Sahrens /* 2460789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2461789Sahrens */ 2462789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2463789Sahrens } 2464789Sahrens 2465789Sahrens ZFS_EXIT(zfsvfs); 2466789Sahrens return (0); 2467789Sahrens } 2468789Sahrens 2469789Sahrens /* 2470789Sahrens * Set the file attributes to the values contained in the 2471789Sahrens * vattr structure. 2472789Sahrens * 2473789Sahrens * IN: vp - vnode of file to be modified. 2474789Sahrens * vap - new attribute values. 24755331Samw * If AT_XVATTR set, then optional attrs are being set 2476789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 24775331Samw * - ATTR_NOACLCHECK (CIFS context only). 2478789Sahrens * cr - credentials of caller. 24795331Samw * ct - caller context 2480789Sahrens * 2481789Sahrens * RETURN: 0 if success 2482789Sahrens * error code if failure 2483789Sahrens * 2484789Sahrens * Timestamps: 2485789Sahrens * vp - ctime updated, mtime updated if size changed. 2486789Sahrens */ 2487789Sahrens /* ARGSUSED */ 2488789Sahrens static int 2489789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2490789Sahrens caller_context_t *ct) 2491789Sahrens { 24925326Sek110237 znode_t *zp = VTOZ(vp); 24935326Sek110237 znode_phys_t *pzp; 2494789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 24955326Sek110237 zilog_t *zilog; 2496789Sahrens dmu_tx_t *tx; 24971878Smaybee vattr_t oldva; 24988190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2499789Sahrens uint_t mask = vap->va_mask; 25001878Smaybee uint_t saved_mask; 25012796Smarks int trim_mask = 0; 2502789Sahrens uint64_t new_mode; 25039179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 25041231Smarks znode_t *attrzp; 2505789Sahrens int need_policy = FALSE; 2506789Sahrens int err; 25075331Samw zfs_fuid_info_t *fuidp = NULL; 25085331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 25095331Samw xoptattr_t *xoap; 25105824Smarks zfs_acl_t *aclp = NULL; 25115331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 25129179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 2513789Sahrens 2514789Sahrens if (mask == 0) 2515789Sahrens return (0); 2516789Sahrens 2517789Sahrens if (mask & AT_NOSET) 2518789Sahrens return (EINVAL); 2519789Sahrens 25205367Sahrens ZFS_ENTER(zfsvfs); 25215367Sahrens ZFS_VERIFY_ZP(zp); 25225331Samw 25235331Samw pzp = zp->z_phys; 25245331Samw zilog = zfsvfs->z_log; 25255331Samw 25265331Samw /* 25275331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 25285331Samw * that file system is at proper version level 25295331Samw */ 25305331Samw 25315331Samw if (zfsvfs->z_use_fuids == B_FALSE && 25325331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 25335331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 25345386Stimh (mask & AT_XVATTR))) { 25355386Stimh ZFS_EXIT(zfsvfs); 25365331Samw return (EINVAL); 25375386Stimh } 25385386Stimh 25395386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 25405386Stimh ZFS_EXIT(zfsvfs); 2541789Sahrens return (EISDIR); 25425386Stimh } 25435386Stimh 25445386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 25455386Stimh ZFS_EXIT(zfsvfs); 25461308Smarks return (EINVAL); 25475386Stimh } 25481308Smarks 25495331Samw /* 25505331Samw * If this is an xvattr_t, then get a pointer to the structure of 25515331Samw * optional attributes. If this is NULL, then we have a vattr_t. 25525331Samw */ 25535331Samw xoap = xva_getxoptattr(xvap); 25545331Samw 25558190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 25568190SMark.Shellenbaum@Sun.COM 25575331Samw /* 25585331Samw * Immutable files can only alter immutable bit and atime 25595331Samw */ 25605331Samw if ((pzp->zp_flags & ZFS_IMMUTABLE) && 25615331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 25625386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 25635386Stimh ZFS_EXIT(zfsvfs); 25645331Samw return (EPERM); 25655386Stimh } 25665386Stimh 25675386Stimh if ((mask & AT_SIZE) && (pzp->zp_flags & ZFS_READONLY)) { 25685386Stimh ZFS_EXIT(zfsvfs); 25695331Samw return (EPERM); 25705386Stimh } 2571789Sahrens 25726064Smarks /* 25736064Smarks * Verify timestamps doesn't overflow 32 bits. 25746064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 25756064Smarks * handle times greater than 2039. This check should be removed 25766064Smarks * once large timestamps are fully supported. 25776064Smarks */ 25786064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 25796064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 25806064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 25816064Smarks ZFS_EXIT(zfsvfs); 25826064Smarks return (EOVERFLOW); 25836064Smarks } 25846064Smarks } 25856064Smarks 2586789Sahrens top: 25871231Smarks attrzp = NULL; 2588789Sahrens 25899981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2590789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2591789Sahrens ZFS_EXIT(zfsvfs); 2592789Sahrens return (EROFS); 2593789Sahrens } 2594789Sahrens 2595789Sahrens /* 2596789Sahrens * First validate permissions 2597789Sahrens */ 2598789Sahrens 2599789Sahrens if (mask & AT_SIZE) { 26005331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2601789Sahrens if (err) { 2602789Sahrens ZFS_EXIT(zfsvfs); 2603789Sahrens return (err); 2604789Sahrens } 26051878Smaybee /* 26061878Smaybee * XXX - Note, we are not providing any open 26071878Smaybee * mode flags here (like FNDELAY), so we may 26081878Smaybee * block if there are locks present... this 26091878Smaybee * should be addressed in openat(). 26101878Smaybee */ 26116992Smaybee /* XXX - would it be OK to generate a log record here? */ 26126992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 26131878Smaybee if (err) { 26141878Smaybee ZFS_EXIT(zfsvfs); 26151878Smaybee return (err); 26161878Smaybee } 2617789Sahrens } 2618789Sahrens 26195331Samw if (mask & (AT_ATIME|AT_MTIME) || 26205331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 26215331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 26225331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 26235331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 26245331Samw XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) 26255331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 26265331Samw skipaclchk, cr); 2627789Sahrens 2628789Sahrens if (mask & (AT_UID|AT_GID)) { 2629789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2630789Sahrens int take_owner; 2631789Sahrens int take_group; 2632789Sahrens 2633789Sahrens /* 2634913Smarks * NOTE: even if a new mode is being set, 2635913Smarks * we may clear S_ISUID/S_ISGID bits. 2636913Smarks */ 2637913Smarks 2638913Smarks if (!(mask & AT_MODE)) 2639913Smarks vap->va_mode = pzp->zp_mode; 2640913Smarks 2641913Smarks /* 2642789Sahrens * Take ownership or chgrp to group we are a member of 2643789Sahrens */ 2644789Sahrens 2645789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 26465331Samw take_group = (mask & AT_GID) && 26475331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2648789Sahrens 2649789Sahrens /* 2650789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2651789Sahrens * take_group must both be set in order to allow taking 2652789Sahrens * ownership. 2653789Sahrens * 2654789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2655789Sahrens * 2656789Sahrens */ 2657789Sahrens 2658789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2659789Sahrens ((idmask == AT_UID) && take_owner) || 2660789Sahrens ((idmask == AT_GID) && take_group)) { 26615331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 26625331Samw skipaclchk, cr) == 0) { 2663789Sahrens /* 2664789Sahrens * Remove setuid/setgid for non-privileged users 2665789Sahrens */ 26661115Smarks secpolicy_setid_clear(vap, cr); 26672796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2668789Sahrens } else { 2669789Sahrens need_policy = TRUE; 2670789Sahrens } 2671789Sahrens } else { 2672789Sahrens need_policy = TRUE; 2673789Sahrens } 2674789Sahrens } 2675789Sahrens 26762796Smarks mutex_enter(&zp->z_lock); 26772796Smarks oldva.va_mode = pzp->zp_mode; 26785771Sjp151216 zfs_fuid_map_ids(zp, cr, &oldva.va_uid, &oldva.va_gid); 26795331Samw if (mask & AT_XVATTR) { 26808190SMark.Shellenbaum@Sun.COM /* 26818190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 26828190SMark.Shellenbaum@Sun.COM * that are actually changing. 26838190SMark.Shellenbaum@Sun.COM * 26848190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 26858190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 26868190SMark.Shellenbaum@Sun.COM */ 26878190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 26888190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 26898190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_APPENDONLY) != 0)) { 26908190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 26918190SMark.Shellenbaum@Sun.COM } else { 26928190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 26938190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 26948190SMark.Shellenbaum@Sun.COM } 26958190SMark.Shellenbaum@Sun.COM } 26968190SMark.Shellenbaum@Sun.COM 26978190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 26988190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 26998190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NOUNLINK) != 0)) { 27008190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27018190SMark.Shellenbaum@Sun.COM } else { 27028190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 27038190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 27048190SMark.Shellenbaum@Sun.COM } 27058190SMark.Shellenbaum@Sun.COM } 27068190SMark.Shellenbaum@Sun.COM 27078190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 27088190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 27098190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_IMMUTABLE) != 0)) { 27108190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27118190SMark.Shellenbaum@Sun.COM } else { 27128190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 27138190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 27148190SMark.Shellenbaum@Sun.COM } 27158190SMark.Shellenbaum@Sun.COM } 27168190SMark.Shellenbaum@Sun.COM 27178190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 27188190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 27198190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_NODUMP) != 0)) { 27208190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27218190SMark.Shellenbaum@Sun.COM } else { 27228190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 27238190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 27248190SMark.Shellenbaum@Sun.COM } 27258190SMark.Shellenbaum@Sun.COM } 27268190SMark.Shellenbaum@Sun.COM 27278190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 27288190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 27298190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_MODIFIED) != 0)) { 27308190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27318190SMark.Shellenbaum@Sun.COM } else { 27328190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 27338190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 27348190SMark.Shellenbaum@Sun.COM } 27358190SMark.Shellenbaum@Sun.COM } 27368190SMark.Shellenbaum@Sun.COM 27378190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 27388190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 27398190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 27408190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 27418190SMark.Shellenbaum@Sun.COM ((pzp->zp_flags & ZFS_AV_QUARANTINED) != 0)) { 27428190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27438190SMark.Shellenbaum@Sun.COM } else { 27448190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 27458190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 27468190SMark.Shellenbaum@Sun.COM } 27478190SMark.Shellenbaum@Sun.COM } 27488190SMark.Shellenbaum@Sun.COM 274910793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 275010793Sdai.ngo@sun.com mutex_exit(&zp->z_lock); 275110793Sdai.ngo@sun.com ZFS_EXIT(zfsvfs); 275210793Sdai.ngo@sun.com return (EPERM); 275310793Sdai.ngo@sun.com } 275410793Sdai.ngo@sun.com 27558190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 27568190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 27578190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 27585331Samw need_policy = TRUE; 27595331Samw } 27605331Samw } 27615331Samw 27622796Smarks mutex_exit(&zp->z_lock); 27632796Smarks 27642796Smarks if (mask & AT_MODE) { 27655331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 27662796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 27672796Smarks &oldva, cr); 27682796Smarks if (err) { 27692796Smarks ZFS_EXIT(zfsvfs); 27702796Smarks return (err); 27712796Smarks } 27722796Smarks trim_mask |= AT_MODE; 27732796Smarks } else { 27742796Smarks need_policy = TRUE; 27752796Smarks } 27762796Smarks } 2777789Sahrens 2778789Sahrens if (need_policy) { 27791115Smarks /* 27801115Smarks * If trim_mask is set then take ownership 27812796Smarks * has been granted or write_acl is present and user 27822796Smarks * has the ability to modify mode. In that case remove 27832796Smarks * UID|GID and or MODE from mask so that 27841115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 27851115Smarks */ 27862796Smarks 27872796Smarks if (trim_mask) { 27882796Smarks saved_mask = vap->va_mask; 27892796Smarks vap->va_mask &= ~trim_mask; 27902796Smarks } 2791789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 27925331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2793789Sahrens if (err) { 2794789Sahrens ZFS_EXIT(zfsvfs); 2795789Sahrens return (err); 2796789Sahrens } 27971115Smarks 27981115Smarks if (trim_mask) 27992796Smarks vap->va_mask |= saved_mask; 2800789Sahrens } 2801789Sahrens 2802789Sahrens /* 2803789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2804789Sahrens * changed va_mask 2805789Sahrens */ 2806789Sahrens mask = vap->va_mask; 2807789Sahrens 2808789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2809789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2810789Sahrens 2811789Sahrens if (mask & AT_MODE) { 28121576Smarks uint64_t pmode = pzp->zp_mode; 28131576Smarks 28141576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2815789Sahrens 28169396SMatthew.Ahrens@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 28179396SMatthew.Ahrens@Sun.COM goto out; 28185331Samw if (pzp->zp_acl.z_acl_extern_obj) { 28195331Samw /* Are we upgrading ACL from old V0 format to new V1 */ 28205331Samw if (zfsvfs->z_version <= ZPL_VERSION_FUID && 28215331Samw pzp->zp_acl.z_acl_version == 28225331Samw ZFS_ACL_VERSION_INITIAL) { 28235331Samw dmu_tx_hold_free(tx, 28245331Samw pzp->zp_acl.z_acl_extern_obj, 0, 28255331Samw DMU_OBJECT_END); 28265331Samw dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 28275824Smarks 0, aclp->z_acl_bytes); 28285331Samw } else { 28295331Samw dmu_tx_hold_write(tx, 28305331Samw pzp->zp_acl.z_acl_extern_obj, 0, 28315824Smarks aclp->z_acl_bytes); 28325331Samw } 28336180Smarks } else if (aclp->z_acl_bytes > ZFS_ACE_SPACE) { 28346180Smarks dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 28356180Smarks 0, aclp->z_acl_bytes); 28365331Samw } 2837789Sahrens } 2838789Sahrens 28399179SMark.Shellenbaum@Sun.COM if (mask & (AT_UID | AT_GID)) { 28409179SMark.Shellenbaum@Sun.COM if (pzp->zp_xattr) { 28419179SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, pzp->zp_xattr, &attrzp); 28429396SMatthew.Ahrens@Sun.COM if (err) 28439396SMatthew.Ahrens@Sun.COM goto out; 28449179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, attrzp->z_id); 28459179SMark.Shellenbaum@Sun.COM } 28469179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 28479179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 28489179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 28499396SMatthew.Ahrens@Sun.COM if (new_uid != pzp->zp_uid && 28509396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_FALSE, new_uid)) { 28519396SMatthew.Ahrens@Sun.COM err = EDQUOT; 28529396SMatthew.Ahrens@Sun.COM goto out; 28539396SMatthew.Ahrens@Sun.COM } 28541231Smarks } 28559396SMatthew.Ahrens@Sun.COM 28569179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 28579179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 28589179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 28599396SMatthew.Ahrens@Sun.COM if (new_gid != pzp->zp_gid && 28609396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_TRUE, new_gid)) { 28619396SMatthew.Ahrens@Sun.COM err = EDQUOT; 28629396SMatthew.Ahrens@Sun.COM goto out; 28639396SMatthew.Ahrens@Sun.COM } 28649179SMark.Shellenbaum@Sun.COM } 28659179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 28669179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) { 28679179SMark.Shellenbaum@Sun.COM if (zfsvfs->z_fuid_obj == 0) { 28689179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 28699179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 28709179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 28719179SMark.Shellenbaum@Sun.COM dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, 28729179SMark.Shellenbaum@Sun.COM FALSE, NULL); 28739179SMark.Shellenbaum@Sun.COM } else { 28749179SMark.Shellenbaum@Sun.COM dmu_tx_hold_bonus(tx, zfsvfs->z_fuid_obj); 28759179SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, zfsvfs->z_fuid_obj, 0, 28769179SMark.Shellenbaum@Sun.COM FUID_SIZE_ESTIMATE(zfsvfs)); 28779179SMark.Shellenbaum@Sun.COM } 28789179SMark.Shellenbaum@Sun.COM } 28791231Smarks } 28801231Smarks 28818227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2882789Sahrens if (err) { 28839396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 28842113Sahrens dmu_tx_wait(tx); 28859396SMatthew.Ahrens@Sun.COM goto out; 2886789Sahrens } 2887789Sahrens 2888789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2889789Sahrens 2890789Sahrens /* 2891789Sahrens * Set each attribute requested. 2892789Sahrens * We group settings according to the locks they need to acquire. 2893789Sahrens * 2894789Sahrens * Note: you cannot set ctime directly, although it will be 2895789Sahrens * updated as a side-effect of calling this function. 2896789Sahrens */ 2897789Sahrens 2898789Sahrens mutex_enter(&zp->z_lock); 2899789Sahrens 2900789Sahrens if (mask & AT_MODE) { 29015824Smarks mutex_enter(&zp->z_acl_lock); 29025824Smarks zp->z_phys->zp_mode = new_mode; 29039179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 2904789Sahrens ASSERT3U(err, ==, 0); 290510143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 290610143STim.Haley@Sun.COM aclp = NULL; 29075824Smarks mutex_exit(&zp->z_acl_lock); 2908789Sahrens } 2909789Sahrens 29101231Smarks if (attrzp) 29111231Smarks mutex_enter(&attrzp->z_lock); 29121231Smarks 29131231Smarks if (mask & AT_UID) { 29149179SMark.Shellenbaum@Sun.COM pzp->zp_uid = new_uid; 29159179SMark.Shellenbaum@Sun.COM if (attrzp) 29169179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_uid = new_uid; 29171231Smarks } 29181231Smarks 29191231Smarks if (mask & AT_GID) { 29209179SMark.Shellenbaum@Sun.COM pzp->zp_gid = new_gid; 29211231Smarks if (attrzp) 29229179SMark.Shellenbaum@Sun.COM attrzp->z_phys->zp_gid = new_gid; 29231231Smarks } 29241231Smarks 29251231Smarks if (attrzp) 29261231Smarks mutex_exit(&attrzp->z_lock); 2927789Sahrens 2928789Sahrens if (mask & AT_ATIME) 2929789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2930789Sahrens 2931789Sahrens if (mask & AT_MTIME) 2932789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2933789Sahrens 29346992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 29351878Smaybee if (mask & AT_SIZE) 2936789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 29371878Smaybee else if (mask != 0) 2938789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 29395331Samw /* 29405331Samw * Do this after setting timestamps to prevent timestamp 29415331Samw * update from toggling bit 29425331Samw */ 29435331Samw 29445331Samw if (xoap && (mask & AT_XVATTR)) { 29458190SMark.Shellenbaum@Sun.COM 29468190SMark.Shellenbaum@Sun.COM /* 29478190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 29488190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 29498190SMark.Shellenbaum@Sun.COM */ 29508190SMark.Shellenbaum@Sun.COM 29518190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 29528190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 29538190SMark.Shellenbaum@Sun.COM } 29548190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 29558190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 29568190SMark.Shellenbaum@Sun.COM } 29578190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 29588190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 29598190SMark.Shellenbaum@Sun.COM } 29608190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 29618190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 29628190SMark.Shellenbaum@Sun.COM } 29638190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 29648190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 29658190SMark.Shellenbaum@Sun.COM } 29668190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 29678190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 29688190SMark.Shellenbaum@Sun.COM } 29698190SMark.Shellenbaum@Sun.COM 29705331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) { 29715331Samw size_t len; 29725331Samw dmu_object_info_t doi; 29735331Samw 29745331Samw ASSERT(vp->v_type == VREG); 29755331Samw 29765331Samw /* Grow the bonus buffer if necessary. */ 29775331Samw dmu_object_info_from_db(zp->z_dbuf, &doi); 29785331Samw len = sizeof (xoap->xoa_av_scanstamp) + 29795331Samw sizeof (znode_phys_t); 29805331Samw if (len > doi.doi_bonus_size) 29815331Samw VERIFY(dmu_set_bonus(zp->z_dbuf, len, tx) == 0); 29825331Samw } 29835331Samw zfs_xvattr_set(zp, xvap); 29845331Samw } 2985789Sahrens 29869179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 29879179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 29889179SMark.Shellenbaum@Sun.COM 29891878Smaybee if (mask != 0) 29905331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 29915331Samw 2992789Sahrens mutex_exit(&zp->z_lock); 2993789Sahrens 29949396SMatthew.Ahrens@Sun.COM out: 29951231Smarks if (attrzp) 29961231Smarks VN_RELE(ZTOV(attrzp)); 29971231Smarks 299810143STim.Haley@Sun.COM if (aclp) 299910143STim.Haley@Sun.COM zfs_acl_free(aclp); 300010143STim.Haley@Sun.COM 30019396SMatthew.Ahrens@Sun.COM if (fuidp) { 30029396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 30039396SMatthew.Ahrens@Sun.COM fuidp = NULL; 30049396SMatthew.Ahrens@Sun.COM } 30059396SMatthew.Ahrens@Sun.COM 30069396SMatthew.Ahrens@Sun.COM if (err) 30079396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 30089396SMatthew.Ahrens@Sun.COM else 30099396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 30109396SMatthew.Ahrens@Sun.COM 30119396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 30129396SMatthew.Ahrens@Sun.COM goto top; 3013789Sahrens 3014789Sahrens ZFS_EXIT(zfsvfs); 3015789Sahrens return (err); 3016789Sahrens } 3017789Sahrens 30183271Smaybee typedef struct zfs_zlock { 30193271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 30203271Smaybee znode_t *zl_znode; /* znode we held */ 30213271Smaybee struct zfs_zlock *zl_next; /* next in list */ 30223271Smaybee } zfs_zlock_t; 30233271Smaybee 30243271Smaybee /* 30253271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 30263271Smaybee */ 30273271Smaybee static void 30283271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 30293271Smaybee { 30303271Smaybee zfs_zlock_t *zl; 30313271Smaybee 30323271Smaybee while ((zl = *zlpp) != NULL) { 30333271Smaybee if (zl->zl_znode != NULL) 30343271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 30353271Smaybee rw_exit(zl->zl_rwlock); 30363271Smaybee *zlpp = zl->zl_next; 30373271Smaybee kmem_free(zl, sizeof (*zl)); 30383271Smaybee } 30393271Smaybee } 30403271Smaybee 3041789Sahrens /* 3042789Sahrens * Search back through the directory tree, using the ".." entries. 3043789Sahrens * Lock each directory in the chain to prevent concurrent renames. 3044789Sahrens * Fail any attempt to move a directory into one of its own descendants. 3045789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 3046789Sahrens */ 3047789Sahrens static int 3048789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3049789Sahrens { 3050789Sahrens zfs_zlock_t *zl; 30513638Sbillm znode_t *zp = tdzp; 3052789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 3053789Sahrens uint64_t *oidp = &zp->z_id; 3054789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 3055789Sahrens krw_t rw = RW_WRITER; 3056789Sahrens 3057789Sahrens /* 3058789Sahrens * First pass write-locks szp and compares to zp->z_id. 3059789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 3060789Sahrens */ 3061789Sahrens do { 30623271Smaybee if (!rw_tryenter(rwlp, rw)) { 30633271Smaybee /* 30643271Smaybee * Another thread is renaming in this path. 30653271Smaybee * Note that if we are a WRITER, we don't have any 30663271Smaybee * parent_locks held yet. 30673271Smaybee */ 30683271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 30693271Smaybee /* 30703271Smaybee * Drop our locks and restart 30713271Smaybee */ 30723271Smaybee zfs_rename_unlock(&zl); 30733271Smaybee *zlpp = NULL; 30743271Smaybee zp = tdzp; 30753271Smaybee oidp = &zp->z_id; 30763271Smaybee rwlp = &szp->z_parent_lock; 30773271Smaybee rw = RW_WRITER; 30783271Smaybee continue; 30793271Smaybee } else { 30803271Smaybee /* 30813271Smaybee * Wait for other thread to drop its locks 30823271Smaybee */ 30833271Smaybee rw_enter(rwlp, rw); 30843271Smaybee } 30853271Smaybee } 30863271Smaybee 3087789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3088789Sahrens zl->zl_rwlock = rwlp; 3089789Sahrens zl->zl_znode = NULL; 3090789Sahrens zl->zl_next = *zlpp; 3091789Sahrens *zlpp = zl; 3092789Sahrens 3093789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 3094789Sahrens return (EINVAL); 3095789Sahrens 3096789Sahrens if (*oidp == rootid) /* We've hit the top */ 3097789Sahrens return (0); 3098789Sahrens 3099789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 3100789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 3101789Sahrens if (error) 3102789Sahrens return (error); 3103789Sahrens zl->zl_znode = zp; 3104789Sahrens } 3105789Sahrens oidp = &zp->z_phys->zp_parent; 3106789Sahrens rwlp = &zp->z_parent_lock; 3107789Sahrens rw = RW_READER; 3108789Sahrens 3109789Sahrens } while (zp->z_id != sdzp->z_id); 3110789Sahrens 3111789Sahrens return (0); 3112789Sahrens } 3113789Sahrens 3114789Sahrens /* 3115789Sahrens * Move an entry from the provided source directory to the target 3116789Sahrens * directory. Change the entry name as indicated. 3117789Sahrens * 3118789Sahrens * IN: sdvp - Source directory containing the "old entry". 3119789Sahrens * snm - Old entry name. 3120789Sahrens * tdvp - Target directory to contain the "new entry". 3121789Sahrens * tnm - New entry name. 3122789Sahrens * cr - credentials of caller. 31235331Samw * ct - caller context 31245331Samw * flags - case flags 3125789Sahrens * 3126789Sahrens * RETURN: 0 if success 3127789Sahrens * error code if failure 3128789Sahrens * 3129789Sahrens * Timestamps: 3130789Sahrens * sdvp,tdvp - ctime|mtime updated 3131789Sahrens */ 31325331Samw /*ARGSUSED*/ 3133789Sahrens static int 31345331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 31355331Samw caller_context_t *ct, int flags) 3136789Sahrens { 3137789Sahrens znode_t *tdzp, *szp, *tzp; 3138789Sahrens znode_t *sdzp = VTOZ(sdvp); 3139789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 31405326Sek110237 zilog_t *zilog; 3141789Sahrens vnode_t *realvp; 3142789Sahrens zfs_dirlock_t *sdl, *tdl; 3143789Sahrens dmu_tx_t *tx; 3144789Sahrens zfs_zlock_t *zl; 31455331Samw int cmp, serr, terr; 31465331Samw int error = 0; 31475331Samw int zflg = 0; 3148789Sahrens 31495367Sahrens ZFS_ENTER(zfsvfs); 31505367Sahrens ZFS_VERIFY_ZP(sdzp); 31515326Sek110237 zilog = zfsvfs->z_log; 3152789Sahrens 3153789Sahrens /* 3154789Sahrens * Make sure we have the real vp for the target directory. 3155789Sahrens */ 31565331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3157789Sahrens tdvp = realvp; 3158789Sahrens 3159789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 3160789Sahrens ZFS_EXIT(zfsvfs); 3161789Sahrens return (EXDEV); 3162789Sahrens } 3163789Sahrens 3164789Sahrens tdzp = VTOZ(tdvp); 31655367Sahrens ZFS_VERIFY_ZP(tdzp); 31665498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 31675331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 31685331Samw ZFS_EXIT(zfsvfs); 31695331Samw return (EILSEQ); 31705331Samw } 31715331Samw 31725331Samw if (flags & FIGNORECASE) 31735331Samw zflg |= ZCILOOK; 31745331Samw 3175789Sahrens top: 3176789Sahrens szp = NULL; 3177789Sahrens tzp = NULL; 3178789Sahrens zl = NULL; 3179789Sahrens 3180789Sahrens /* 3181789Sahrens * This is to prevent the creation of links into attribute space 3182789Sahrens * by renaming a linked file into/outof an attribute directory. 3183789Sahrens * See the comment in zfs_link() for why this is considered bad. 3184789Sahrens */ 3185789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 3186789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 3187789Sahrens ZFS_EXIT(zfsvfs); 3188789Sahrens return (EINVAL); 3189789Sahrens } 3190789Sahrens 3191789Sahrens /* 3192789Sahrens * Lock source and target directory entries. To prevent deadlock, 3193789Sahrens * a lock ordering must be defined. We lock the directory with 3194789Sahrens * the smallest object id first, or if it's a tie, the one with 3195789Sahrens * the lexically first name. 3196789Sahrens */ 3197789Sahrens if (sdzp->z_id < tdzp->z_id) { 3198789Sahrens cmp = -1; 3199789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3200789Sahrens cmp = 1; 3201789Sahrens } else { 32025331Samw /* 32035331Samw * First compare the two name arguments without 32045331Samw * considering any case folding. 32055331Samw */ 32065331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 32075331Samw 32085331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 32095498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3210789Sahrens if (cmp == 0) { 3211789Sahrens /* 3212789Sahrens * POSIX: "If the old argument and the new argument 3213789Sahrens * both refer to links to the same existing file, 3214789Sahrens * the rename() function shall return successfully 3215789Sahrens * and perform no other action." 3216789Sahrens */ 3217789Sahrens ZFS_EXIT(zfsvfs); 3218789Sahrens return (0); 3219789Sahrens } 32205331Samw /* 32215331Samw * If the file system is case-folding, then we may 32225331Samw * have some more checking to do. A case-folding file 32235331Samw * system is either supporting mixed case sensitivity 32245331Samw * access or is completely case-insensitive. Note 32255331Samw * that the file system is always case preserving. 32265331Samw * 32275331Samw * In mixed sensitivity mode case sensitive behavior 32285331Samw * is the default. FIGNORECASE must be used to 32295331Samw * explicitly request case insensitive behavior. 32305331Samw * 32315331Samw * If the source and target names provided differ only 32325331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 32335331Samw * we will treat this as a special case in the 32345331Samw * case-insensitive mode: as long as the source name 32355331Samw * is an exact match, we will allow this to proceed as 32365331Samw * a name-change request. 32375331Samw */ 32385498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 32395498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 32405498Stimh flags & FIGNORECASE)) && 32415331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 32425331Samw &error) == 0) { 32435331Samw /* 32445331Samw * case preserving rename request, require exact 32455331Samw * name matches 32465331Samw */ 32475331Samw zflg |= ZCIEXACT; 32485331Samw zflg &= ~ZCILOOK; 32495331Samw } 3250789Sahrens } 32515331Samw 325211321SSanjeev.Bagewadi@Sun.COM /* 325311321SSanjeev.Bagewadi@Sun.COM * If the source and destination directories are the same, we should 325411321SSanjeev.Bagewadi@Sun.COM * grab the z_name_lock of that directory only once. 325511321SSanjeev.Bagewadi@Sun.COM */ 325611321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) { 325711321SSanjeev.Bagewadi@Sun.COM zflg |= ZHAVELOCK; 325811321SSanjeev.Bagewadi@Sun.COM rw_enter(&sdzp->z_name_lock, RW_READER); 325911321SSanjeev.Bagewadi@Sun.COM } 326011321SSanjeev.Bagewadi@Sun.COM 3261789Sahrens if (cmp < 0) { 32625331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 32635331Samw ZEXISTS | zflg, NULL, NULL); 32645331Samw terr = zfs_dirent_lock(&tdl, 32655331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3266789Sahrens } else { 32675331Samw terr = zfs_dirent_lock(&tdl, 32685331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 32695331Samw serr = zfs_dirent_lock(&sdl, 32705331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 32715331Samw NULL, NULL); 3272789Sahrens } 3273789Sahrens 3274789Sahrens if (serr) { 3275789Sahrens /* 3276789Sahrens * Source entry invalid or not there. 3277789Sahrens */ 3278789Sahrens if (!terr) { 3279789Sahrens zfs_dirent_unlock(tdl); 3280789Sahrens if (tzp) 3281789Sahrens VN_RELE(ZTOV(tzp)); 3282789Sahrens } 328311321SSanjeev.Bagewadi@Sun.COM 328411321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 328511321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 328611321SSanjeev.Bagewadi@Sun.COM 3287789Sahrens if (strcmp(snm, "..") == 0) 3288789Sahrens serr = EINVAL; 3289789Sahrens ZFS_EXIT(zfsvfs); 3290789Sahrens return (serr); 3291789Sahrens } 3292789Sahrens if (terr) { 3293789Sahrens zfs_dirent_unlock(sdl); 3294789Sahrens VN_RELE(ZTOV(szp)); 329511321SSanjeev.Bagewadi@Sun.COM 329611321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 329711321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 329811321SSanjeev.Bagewadi@Sun.COM 3299789Sahrens if (strcmp(tnm, "..") == 0) 3300789Sahrens terr = EINVAL; 3301789Sahrens ZFS_EXIT(zfsvfs); 3302789Sahrens return (terr); 3303789Sahrens } 3304789Sahrens 3305789Sahrens /* 3306789Sahrens * Must have write access at the source to remove the old entry 3307789Sahrens * and write access at the target to create the new entry. 3308789Sahrens * Note that if target and source are the same, this can be 3309789Sahrens * done in a single check. 3310789Sahrens */ 3311789Sahrens 3312789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3313789Sahrens goto out; 3314789Sahrens 3315789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3316789Sahrens /* 3317789Sahrens * Check to make sure rename is valid. 3318789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3319789Sahrens */ 3320789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3321789Sahrens goto out; 3322789Sahrens } 3323789Sahrens 3324789Sahrens /* 3325789Sahrens * Does target exist? 3326789Sahrens */ 3327789Sahrens if (tzp) { 3328789Sahrens /* 3329789Sahrens * Source and target must be the same type. 3330789Sahrens */ 3331789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3332789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3333789Sahrens error = ENOTDIR; 3334789Sahrens goto out; 3335789Sahrens } 3336789Sahrens } else { 3337789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3338789Sahrens error = EISDIR; 3339789Sahrens goto out; 3340789Sahrens } 3341789Sahrens } 3342789Sahrens /* 3343789Sahrens * POSIX dictates that when the source and target 3344789Sahrens * entries refer to the same file object, rename 3345789Sahrens * must do nothing and exit without error. 3346789Sahrens */ 3347789Sahrens if (szp->z_id == tzp->z_id) { 3348789Sahrens error = 0; 3349789Sahrens goto out; 3350789Sahrens } 3351789Sahrens } 3352789Sahrens 33535331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3354789Sahrens if (tzp) 33555331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 33564863Spraks 33574863Spraks /* 33584863Spraks * notify the target directory if it is not the same 33594863Spraks * as source directory. 33604863Spraks */ 33614863Spraks if (tdvp != sdvp) { 33625331Samw vnevent_rename_dest_dir(tdvp, ct); 33634863Spraks } 3364789Sahrens 3365789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3366789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 3367789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 33681544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 33691544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 33701544Seschrock if (sdzp != tdzp) 3371789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 33721544Seschrock if (tzp) 33731544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 33743461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 33758227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3376789Sahrens if (error) { 3377789Sahrens if (zl != NULL) 3378789Sahrens zfs_rename_unlock(&zl); 3379789Sahrens zfs_dirent_unlock(sdl); 3380789Sahrens zfs_dirent_unlock(tdl); 338111321SSanjeev.Bagewadi@Sun.COM 338211321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 338311321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 338411321SSanjeev.Bagewadi@Sun.COM 3385789Sahrens VN_RELE(ZTOV(szp)); 3386789Sahrens if (tzp) 3387789Sahrens VN_RELE(ZTOV(tzp)); 33888227SNeil.Perrin@Sun.COM if (error == ERESTART) { 33892113Sahrens dmu_tx_wait(tx); 33902113Sahrens dmu_tx_abort(tx); 3391789Sahrens goto top; 3392789Sahrens } 33932113Sahrens dmu_tx_abort(tx); 3394789Sahrens ZFS_EXIT(zfsvfs); 3395789Sahrens return (error); 3396789Sahrens } 3397789Sahrens 3398789Sahrens if (tzp) /* Attempt to remove the existing target */ 33995331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3400789Sahrens 3401789Sahrens if (error == 0) { 3402789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3403789Sahrens if (error == 0) { 34045331Samw szp->z_phys->zp_flags |= ZFS_AV_MODIFIED; 34055331Samw 3406789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 3407789Sahrens ASSERT(error == 0); 34085331Samw 34095331Samw zfs_log_rename(zilog, tx, 34105331Samw TX_RENAME | (flags & FIGNORECASE ? TX_CI : 0), 34115331Samw sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 34126976Seschrock 34136976Seschrock /* Update path information for the target vnode */ 34146976Seschrock vn_renamepath(tdvp, ZTOV(szp), tnm, strlen(tnm)); 3415789Sahrens } 3416789Sahrens } 3417789Sahrens 3418789Sahrens dmu_tx_commit(tx); 3419789Sahrens out: 3420789Sahrens if (zl != NULL) 3421789Sahrens zfs_rename_unlock(&zl); 3422789Sahrens 3423789Sahrens zfs_dirent_unlock(sdl); 3424789Sahrens zfs_dirent_unlock(tdl); 3425789Sahrens 342611321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 342711321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 342811321SSanjeev.Bagewadi@Sun.COM 342911321SSanjeev.Bagewadi@Sun.COM 3430789Sahrens VN_RELE(ZTOV(szp)); 3431789Sahrens if (tzp) 3432789Sahrens VN_RELE(ZTOV(tzp)); 3433789Sahrens 3434789Sahrens ZFS_EXIT(zfsvfs); 3435789Sahrens return (error); 3436789Sahrens } 3437789Sahrens 3438789Sahrens /* 3439789Sahrens * Insert the indicated symbolic reference entry into the directory. 3440789Sahrens * 3441789Sahrens * IN: dvp - Directory to contain new symbolic link. 3442789Sahrens * link - Name for new symlink entry. 3443789Sahrens * vap - Attributes of new entry. 3444789Sahrens * target - Target path of new symlink. 3445789Sahrens * cr - credentials of caller. 34465331Samw * ct - caller context 34475331Samw * flags - case flags 3448789Sahrens * 3449789Sahrens * RETURN: 0 if success 3450789Sahrens * error code if failure 3451789Sahrens * 3452789Sahrens * Timestamps: 3453789Sahrens * dvp - ctime|mtime updated 3454789Sahrens */ 34555331Samw /*ARGSUSED*/ 3456789Sahrens static int 34575331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 34585331Samw caller_context_t *ct, int flags) 3459789Sahrens { 3460789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3461789Sahrens zfs_dirlock_t *dl; 3462789Sahrens dmu_tx_t *tx; 3463789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 34645326Sek110237 zilog_t *zilog; 3465789Sahrens int len = strlen(link); 3466789Sahrens int error; 34675331Samw int zflg = ZNEW; 34689179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 34699179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 3470789Sahrens 3471789Sahrens ASSERT(vap->va_type == VLNK); 3472789Sahrens 34735367Sahrens ZFS_ENTER(zfsvfs); 34745367Sahrens ZFS_VERIFY_ZP(dzp); 34755326Sek110237 zilog = zfsvfs->z_log; 34765331Samw 34775498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 34785331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 34795331Samw ZFS_EXIT(zfsvfs); 34805331Samw return (EILSEQ); 34815331Samw } 34825331Samw if (flags & FIGNORECASE) 34835331Samw zflg |= ZCILOOK; 3484789Sahrens top: 34855331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3486789Sahrens ZFS_EXIT(zfsvfs); 3487789Sahrens return (error); 3488789Sahrens } 3489789Sahrens 3490789Sahrens if (len > MAXPATHLEN) { 3491789Sahrens ZFS_EXIT(zfsvfs); 3492789Sahrens return (ENAMETOOLONG); 3493789Sahrens } 3494789Sahrens 3495789Sahrens /* 3496789Sahrens * Attempt to lock directory; fail if entry already exists. 3497789Sahrens */ 34985331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 34995331Samw if (error) { 3500789Sahrens ZFS_EXIT(zfsvfs); 3501789Sahrens return (error); 3502789Sahrens } 3503789Sahrens 35049179SMark.Shellenbaum@Sun.COM VERIFY(0 == zfs_acl_ids_create(dzp, 0, vap, cr, NULL, &acl_ids)); 35059396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 35069396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 35079396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 35089396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 35099396SMatthew.Ahrens@Sun.COM return (EDQUOT); 35109396SMatthew.Ahrens@Sun.COM } 3511789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 35129179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3513789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 3514789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 35151544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 35169179SMark.Shellenbaum@Sun.COM if (acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) 3517789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 35189396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 35199396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 35208227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3521789Sahrens if (error) { 35229179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3523789Sahrens zfs_dirent_unlock(dl); 35248227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35252113Sahrens dmu_tx_wait(tx); 35262113Sahrens dmu_tx_abort(tx); 3527789Sahrens goto top; 3528789Sahrens } 35292113Sahrens dmu_tx_abort(tx); 3530789Sahrens ZFS_EXIT(zfsvfs); 3531789Sahrens return (error); 3532789Sahrens } 3533789Sahrens 3534789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 3535789Sahrens 3536789Sahrens /* 3537789Sahrens * Create a new object for the symlink. 3538789Sahrens * Put the link content into bonus buffer if it will fit; 3539789Sahrens * otherwise, store it just like any other file data. 3540789Sahrens */ 3541789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 35429179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, len, &acl_ids); 3543789Sahrens if (len != 0) 3544789Sahrens bcopy(link, zp->z_phys + 1, len); 3545789Sahrens } else { 3546789Sahrens dmu_buf_t *dbp; 35471669Sperrin 35489179SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, 0, &acl_ids); 35499179SMark.Shellenbaum@Sun.COM 35509179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 35519179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 35521669Sperrin /* 35531669Sperrin * Nothing can access the znode yet so no locking needed 35541669Sperrin * for growing the znode's blocksize. 35551669Sperrin */ 35561669Sperrin zfs_grow_blocksize(zp, len, tx); 3557789Sahrens 35585446Sahrens VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, 35595446Sahrens zp->z_id, 0, FTAG, &dbp)); 3560789Sahrens dmu_buf_will_dirty(dbp, tx); 3561789Sahrens 3562789Sahrens ASSERT3U(len, <=, dbp->db_size); 3563789Sahrens bcopy(link, dbp->db_data, len); 35641544Seschrock dmu_buf_rele(dbp, FTAG); 3565789Sahrens } 3566789Sahrens zp->z_phys->zp_size = len; 3567789Sahrens 3568789Sahrens /* 3569789Sahrens * Insert the new object into the directory. 3570789Sahrens */ 3571789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 35725331Samw if (error == 0) { 35735331Samw uint64_t txtype = TX_SYMLINK; 35745331Samw if (flags & FIGNORECASE) 35755331Samw txtype |= TX_CI; 35765331Samw zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 35775331Samw } 35789179SMark.Shellenbaum@Sun.COM 35799179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3580789Sahrens 3581789Sahrens dmu_tx_commit(tx); 3582789Sahrens 3583789Sahrens zfs_dirent_unlock(dl); 3584789Sahrens 3585789Sahrens VN_RELE(ZTOV(zp)); 3586789Sahrens 3587789Sahrens ZFS_EXIT(zfsvfs); 3588789Sahrens return (error); 3589789Sahrens } 3590789Sahrens 3591789Sahrens /* 3592789Sahrens * Return, in the buffer contained in the provided uio structure, 3593789Sahrens * the symbolic path referred to by vp. 3594789Sahrens * 3595789Sahrens * IN: vp - vnode of symbolic link. 3596789Sahrens * uoip - structure to contain the link path. 3597789Sahrens * cr - credentials of caller. 35985331Samw * ct - caller context 3599789Sahrens * 3600789Sahrens * OUT: uio - structure to contain the link path. 3601789Sahrens * 3602789Sahrens * RETURN: 0 if success 3603789Sahrens * error code if failure 3604789Sahrens * 3605789Sahrens * Timestamps: 3606789Sahrens * vp - atime updated 3607789Sahrens */ 3608789Sahrens /* ARGSUSED */ 3609789Sahrens static int 36105331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3611789Sahrens { 3612789Sahrens znode_t *zp = VTOZ(vp); 3613789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3614789Sahrens size_t bufsz; 3615789Sahrens int error; 3616789Sahrens 36175367Sahrens ZFS_ENTER(zfsvfs); 36185367Sahrens ZFS_VERIFY_ZP(zp); 3619789Sahrens 3620789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 3621789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 3622789Sahrens error = uiomove(zp->z_phys + 1, 3623789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 3624789Sahrens } else { 36251544Seschrock dmu_buf_t *dbp; 36261544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 36271544Seschrock if (error) { 3628789Sahrens ZFS_EXIT(zfsvfs); 3629789Sahrens return (error); 3630789Sahrens } 3631789Sahrens error = uiomove(dbp->db_data, 3632789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 36331544Seschrock dmu_buf_rele(dbp, FTAG); 3634789Sahrens } 3635789Sahrens 3636789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3637789Sahrens ZFS_EXIT(zfsvfs); 3638789Sahrens return (error); 3639789Sahrens } 3640789Sahrens 3641789Sahrens /* 3642789Sahrens * Insert a new entry into directory tdvp referencing svp. 3643789Sahrens * 3644789Sahrens * IN: tdvp - Directory to contain new entry. 3645789Sahrens * svp - vnode of new entry. 3646789Sahrens * name - name of new entry. 3647789Sahrens * cr - credentials of caller. 36485331Samw * ct - caller context 3649789Sahrens * 3650789Sahrens * RETURN: 0 if success 3651789Sahrens * error code if failure 3652789Sahrens * 3653789Sahrens * Timestamps: 3654789Sahrens * tdvp - ctime|mtime updated 3655789Sahrens * svp - ctime updated 3656789Sahrens */ 3657789Sahrens /* ARGSUSED */ 3658789Sahrens static int 36595331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 36605331Samw caller_context_t *ct, int flags) 3661789Sahrens { 3662789Sahrens znode_t *dzp = VTOZ(tdvp); 3663789Sahrens znode_t *tzp, *szp; 3664789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 36655326Sek110237 zilog_t *zilog; 3666789Sahrens zfs_dirlock_t *dl; 3667789Sahrens dmu_tx_t *tx; 3668789Sahrens vnode_t *realvp; 3669789Sahrens int error; 36705331Samw int zf = ZNEW; 36715331Samw uid_t owner; 3672789Sahrens 3673789Sahrens ASSERT(tdvp->v_type == VDIR); 3674789Sahrens 36755367Sahrens ZFS_ENTER(zfsvfs); 36765367Sahrens ZFS_VERIFY_ZP(dzp); 36775326Sek110237 zilog = zfsvfs->z_log; 3678789Sahrens 36795331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3680789Sahrens svp = realvp; 3681789Sahrens 3682789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 3683789Sahrens ZFS_EXIT(zfsvfs); 3684789Sahrens return (EXDEV); 3685789Sahrens } 36865367Sahrens szp = VTOZ(svp); 36875367Sahrens ZFS_VERIFY_ZP(szp); 3688789Sahrens 36895498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 36905331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 36915331Samw ZFS_EXIT(zfsvfs); 36925331Samw return (EILSEQ); 36935331Samw } 36945331Samw if (flags & FIGNORECASE) 36955331Samw zf |= ZCILOOK; 36965331Samw 3697789Sahrens top: 3698789Sahrens /* 3699789Sahrens * We do not support links between attributes and non-attributes 3700789Sahrens * because of the potential security risk of creating links 3701789Sahrens * into "normal" file space in order to circumvent restrictions 3702789Sahrens * imposed in attribute space. 3703789Sahrens */ 3704789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 3705789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 3706789Sahrens ZFS_EXIT(zfsvfs); 3707789Sahrens return (EINVAL); 3708789Sahrens } 3709789Sahrens 3710789Sahrens /* 3711789Sahrens * POSIX dictates that we return EPERM here. 3712789Sahrens * Better choices include ENOTSUP or EISDIR. 3713789Sahrens */ 3714789Sahrens if (svp->v_type == VDIR) { 3715789Sahrens ZFS_EXIT(zfsvfs); 3716789Sahrens return (EPERM); 3717789Sahrens } 3718789Sahrens 37195959Smarks owner = zfs_fuid_map_id(zfsvfs, szp->z_phys->zp_uid, cr, ZFS_OWNER); 37205331Samw if (owner != crgetuid(cr) && 3721789Sahrens secpolicy_basic_link(cr) != 0) { 3722789Sahrens ZFS_EXIT(zfsvfs); 3723789Sahrens return (EPERM); 3724789Sahrens } 3725789Sahrens 37265331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3727789Sahrens ZFS_EXIT(zfsvfs); 3728789Sahrens return (error); 3729789Sahrens } 3730789Sahrens 3731789Sahrens /* 3732789Sahrens * Attempt to lock directory; fail if entry already exists. 3733789Sahrens */ 37345331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 37355331Samw if (error) { 3736789Sahrens ZFS_EXIT(zfsvfs); 3737789Sahrens return (error); 3738789Sahrens } 3739789Sahrens 3740789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3741789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 37421544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 37438227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3744789Sahrens if (error) { 3745789Sahrens zfs_dirent_unlock(dl); 37468227SNeil.Perrin@Sun.COM if (error == ERESTART) { 37472113Sahrens dmu_tx_wait(tx); 37482113Sahrens dmu_tx_abort(tx); 3749789Sahrens goto top; 3750789Sahrens } 37512113Sahrens dmu_tx_abort(tx); 3752789Sahrens ZFS_EXIT(zfsvfs); 3753789Sahrens return (error); 3754789Sahrens } 3755789Sahrens 3756789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3757789Sahrens 37585331Samw if (error == 0) { 37595331Samw uint64_t txtype = TX_LINK; 37605331Samw if (flags & FIGNORECASE) 37615331Samw txtype |= TX_CI; 37625331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 37635331Samw } 3764789Sahrens 3765789Sahrens dmu_tx_commit(tx); 3766789Sahrens 3767789Sahrens zfs_dirent_unlock(dl); 3768789Sahrens 37694863Spraks if (error == 0) { 37705331Samw vnevent_link(svp, ct); 37714863Spraks } 37724863Spraks 3773789Sahrens ZFS_EXIT(zfsvfs); 3774789Sahrens return (error); 3775789Sahrens } 3776789Sahrens 3777789Sahrens /* 3778789Sahrens * zfs_null_putapage() is used when the file system has been force 3779789Sahrens * unmounted. It just drops the pages. 3780789Sahrens */ 3781789Sahrens /* ARGSUSED */ 3782789Sahrens static int 3783789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3784789Sahrens size_t *lenp, int flags, cred_t *cr) 3785789Sahrens { 3786789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3787789Sahrens return (0); 3788789Sahrens } 3789789Sahrens 37902688Smaybee /* 37912688Smaybee * Push a page out to disk, klustering if possible. 37922688Smaybee * 37932688Smaybee * IN: vp - file to push page to. 37942688Smaybee * pp - page to push. 37952688Smaybee * flags - additional flags. 37962688Smaybee * cr - credentials of caller. 37972688Smaybee * 37982688Smaybee * OUT: offp - start of range pushed. 37992688Smaybee * lenp - len of range pushed. 38002688Smaybee * 38012688Smaybee * RETURN: 0 if success 38022688Smaybee * error code if failure 38032688Smaybee * 38042688Smaybee * NOTE: callers must have locked the page to be pushed. On 38052688Smaybee * exit, the page (and all other pages in the kluster) must be 38062688Smaybee * unlocked. 38072688Smaybee */ 3808789Sahrens /* ARGSUSED */ 3809789Sahrens static int 3810789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3811789Sahrens size_t *lenp, int flags, cred_t *cr) 3812789Sahrens { 3813789Sahrens znode_t *zp = VTOZ(vp); 3814789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3815789Sahrens dmu_tx_t *tx; 38162688Smaybee u_offset_t off, koff; 38172688Smaybee size_t len, klen; 38184709Smaybee uint64_t filesz; 3819789Sahrens int err; 3820789Sahrens 38214709Smaybee filesz = zp->z_phys->zp_size; 38222688Smaybee off = pp->p_offset; 38232688Smaybee len = PAGESIZE; 38242688Smaybee /* 38252688Smaybee * If our blocksize is bigger than the page size, try to kluster 38268227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 38272688Smaybee * a read-modify-write). 38282688Smaybee */ 38294709Smaybee if (off < filesz && zp->z_blksz > PAGESIZE) { 38308636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 38318636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 38322688Smaybee ASSERT(koff <= filesz); 38332688Smaybee if (koff + klen > filesz) 38342688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 38352688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 38362688Smaybee } 38372688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 38388636SMark.Maybee@Sun.COM 38391819Smaybee /* 38401819Smaybee * Can't push pages past end-of-file. 38411819Smaybee */ 38424709Smaybee if (off >= filesz) { 38434709Smaybee /* ignore all pages */ 38442688Smaybee err = 0; 38452688Smaybee goto out; 38464709Smaybee } else if (off + len > filesz) { 38474709Smaybee int npages = btopr(filesz - off); 38482688Smaybee page_t *trunc; 38492688Smaybee 38502688Smaybee page_list_break(&pp, &trunc, npages); 38514709Smaybee /* ignore pages past end of file */ 38522688Smaybee if (trunc) 38534709Smaybee pvn_write_done(trunc, flags); 38544709Smaybee len = filesz - off; 38551819Smaybee } 38569396SMatthew.Ahrens@Sun.COM 38579396SMatthew.Ahrens@Sun.COM if (zfs_usergroup_overquota(zfsvfs, B_FALSE, zp->z_phys->zp_uid) || 38589396SMatthew.Ahrens@Sun.COM zfs_usergroup_overquota(zfsvfs, B_TRUE, zp->z_phys->zp_gid)) { 38599396SMatthew.Ahrens@Sun.COM err = EDQUOT; 38609396SMatthew.Ahrens@Sun.COM goto out; 38619396SMatthew.Ahrens@Sun.COM } 38628636SMark.Maybee@Sun.COM top: 3863789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 3864789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 3865789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 38668227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 3867789Sahrens if (err != 0) { 38688227SNeil.Perrin@Sun.COM if (err == ERESTART) { 38692113Sahrens dmu_tx_wait(tx); 38702113Sahrens dmu_tx_abort(tx); 3871789Sahrens goto top; 3872789Sahrens } 38732113Sahrens dmu_tx_abort(tx); 3874789Sahrens goto out; 3875789Sahrens } 3876789Sahrens 38772688Smaybee if (zp->z_blksz <= PAGESIZE) { 38787315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 38792688Smaybee ASSERT3U(len, <=, PAGESIZE); 38802688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 38817315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 38822688Smaybee } else { 38832688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 38842688Smaybee } 38852688Smaybee 38862688Smaybee if (err == 0) { 38872688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 38888636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 38892688Smaybee } 38909951SLin.Ling@Sun.COM dmu_tx_commit(tx); 38912688Smaybee 38922688Smaybee out: 38934709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 3894789Sahrens if (offp) 3895789Sahrens *offp = off; 3896789Sahrens if (lenp) 3897789Sahrens *lenp = len; 3898789Sahrens 3899789Sahrens return (err); 3900789Sahrens } 3901789Sahrens 3902789Sahrens /* 3903789Sahrens * Copy the portion of the file indicated from pages into the file. 3904789Sahrens * The pages are stored in a page list attached to the files vnode. 3905789Sahrens * 3906789Sahrens * IN: vp - vnode of file to push page data to. 3907789Sahrens * off - position in file to put data. 3908789Sahrens * len - amount of data to write. 3909789Sahrens * flags - flags to control the operation. 3910789Sahrens * cr - credentials of caller. 39115331Samw * ct - caller context. 3912789Sahrens * 3913789Sahrens * RETURN: 0 if success 3914789Sahrens * error code if failure 3915789Sahrens * 3916789Sahrens * Timestamps: 3917789Sahrens * vp - ctime|mtime updated 3918789Sahrens */ 39195331Samw /*ARGSUSED*/ 3920789Sahrens static int 39215331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 39225331Samw caller_context_t *ct) 3923789Sahrens { 3924789Sahrens znode_t *zp = VTOZ(vp); 3925789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3926789Sahrens page_t *pp; 3927789Sahrens size_t io_len; 3928789Sahrens u_offset_t io_off; 39298636SMark.Maybee@Sun.COM uint_t blksz; 39308636SMark.Maybee@Sun.COM rl_t *rl; 3931789Sahrens int error = 0; 3932789Sahrens 39335367Sahrens ZFS_ENTER(zfsvfs); 39345367Sahrens ZFS_VERIFY_ZP(zp); 3935789Sahrens 39368636SMark.Maybee@Sun.COM /* 39378636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 39388636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 39398636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 39408636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 39418636SMark.Maybee@Sun.COM * operations to get narrower locking. 39428636SMark.Maybee@Sun.COM */ 39438636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 39448636SMark.Maybee@Sun.COM if (ISP2(blksz)) 39458636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 39468636SMark.Maybee@Sun.COM else 39478636SMark.Maybee@Sun.COM io_off = 0; 39488636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 39499141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 39508636SMark.Maybee@Sun.COM else 39518636SMark.Maybee@Sun.COM io_len = 0; 39528636SMark.Maybee@Sun.COM 39538636SMark.Maybee@Sun.COM if (io_len == 0) { 3954789Sahrens /* 39558636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 3956789Sahrens */ 39578636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 39588636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 39591472Sperrin goto out; 3960789Sahrens } 39618636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 39628636SMark.Maybee@Sun.COM 39638636SMark.Maybee@Sun.COM if (off > zp->z_phys->zp_size) { 3964789Sahrens /* past end of file */ 39658636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 3966789Sahrens ZFS_EXIT(zfsvfs); 3967789Sahrens return (0); 3968789Sahrens } 3969789Sahrens 39708636SMark.Maybee@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_phys->zp_size, PAGESIZE) - io_off); 39718636SMark.Maybee@Sun.COM 39728636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 3973789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 39741669Sperrin pp = page_lookup(vp, io_off, 39754339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 3976789Sahrens } else { 3977789Sahrens pp = page_lookup_nowait(vp, io_off, 39784339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 3979789Sahrens } 3980789Sahrens 3981789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 3982789Sahrens int err; 3983789Sahrens 3984789Sahrens /* 3985789Sahrens * Found a dirty page to push 3986789Sahrens */ 39871669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 39881669Sperrin if (err) 3989789Sahrens error = err; 3990789Sahrens } else { 3991789Sahrens io_len = PAGESIZE; 3992789Sahrens } 3993789Sahrens } 39941472Sperrin out: 39958636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 39962638Sperrin if ((flags & B_ASYNC) == 0) 39972638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 3998789Sahrens ZFS_EXIT(zfsvfs); 3999789Sahrens return (error); 4000789Sahrens } 4001789Sahrens 40025331Samw /*ARGSUSED*/ 4003789Sahrens void 40045331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4005789Sahrens { 4006789Sahrens znode_t *zp = VTOZ(vp); 4007789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4008789Sahrens int error; 4009789Sahrens 40105326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 40115446Sahrens if (zp->z_dbuf == NULL) { 40125446Sahrens /* 40135642Smaybee * The fs has been unmounted, or we did a 40145642Smaybee * suspend/resume and this file no longer exists. 40155446Sahrens */ 4016789Sahrens if (vn_has_cached_data(vp)) { 4017789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 4018789Sahrens B_INVAL, cr); 4019789Sahrens } 4020789Sahrens 40211544Seschrock mutex_enter(&zp->z_lock); 402210369Schris.kirby@sun.com mutex_enter(&vp->v_lock); 402310369Schris.kirby@sun.com ASSERT(vp->v_count == 1); 402410369Schris.kirby@sun.com vp->v_count = 0; 402510369Schris.kirby@sun.com mutex_exit(&vp->v_lock); 40265446Sahrens mutex_exit(&zp->z_lock); 40275642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 40285446Sahrens zfs_znode_free(zp); 4029789Sahrens return; 4030789Sahrens } 4031789Sahrens 4032789Sahrens /* 4033789Sahrens * Attempt to push any data in the page cache. If this fails 4034789Sahrens * we will get kicked out later in zfs_zinactive(). 4035789Sahrens */ 40361298Sperrin if (vn_has_cached_data(vp)) { 40371298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 40381298Sperrin cr); 40391298Sperrin } 4040789Sahrens 40413461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4042789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4043789Sahrens 4044789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 4045789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 4046789Sahrens if (error) { 4047789Sahrens dmu_tx_abort(tx); 4048789Sahrens } else { 4049789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 4050789Sahrens mutex_enter(&zp->z_lock); 4051789Sahrens zp->z_atime_dirty = 0; 4052789Sahrens mutex_exit(&zp->z_lock); 4053789Sahrens dmu_tx_commit(tx); 4054789Sahrens } 4055789Sahrens } 4056789Sahrens 4057789Sahrens zfs_zinactive(zp); 40585326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4059789Sahrens } 4060789Sahrens 4061789Sahrens /* 4062789Sahrens * Bounds-check the seek operation. 4063789Sahrens * 4064789Sahrens * IN: vp - vnode seeking within 4065789Sahrens * ooff - old file offset 4066789Sahrens * noffp - pointer to new file offset 40675331Samw * ct - caller context 4068789Sahrens * 4069789Sahrens * RETURN: 0 if success 4070789Sahrens * EINVAL if new offset invalid 4071789Sahrens */ 4072789Sahrens /* ARGSUSED */ 4073789Sahrens static int 40745331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 40755331Samw caller_context_t *ct) 4076789Sahrens { 4077789Sahrens if (vp->v_type == VDIR) 4078789Sahrens return (0); 4079789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4080789Sahrens } 4081789Sahrens 4082789Sahrens /* 4083789Sahrens * Pre-filter the generic locking function to trap attempts to place 4084789Sahrens * a mandatory lock on a memory mapped file. 4085789Sahrens */ 4086789Sahrens static int 4087789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 40885331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4089789Sahrens { 4090789Sahrens znode_t *zp = VTOZ(vp); 4091789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4092789Sahrens 40935367Sahrens ZFS_ENTER(zfsvfs); 40945367Sahrens ZFS_VERIFY_ZP(zp); 4095789Sahrens 4096789Sahrens /* 40971544Seschrock * We are following the UFS semantics with respect to mapcnt 40981544Seschrock * here: If we see that the file is mapped already, then we will 40991544Seschrock * return an error, but we don't worry about races between this 41001544Seschrock * function and zfs_map(). 4101789Sahrens */ 41021544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 4103789Sahrens ZFS_EXIT(zfsvfs); 4104789Sahrens return (EAGAIN); 4105789Sahrens } 4106789Sahrens ZFS_EXIT(zfsvfs); 410710896SMark.Shellenbaum@Sun.COM return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4108789Sahrens } 4109789Sahrens 4110789Sahrens /* 4111789Sahrens * If we can't find a page in the cache, we will create a new page 4112789Sahrens * and fill it with file data. For efficiency, we may try to fill 41138636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 41149265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 41159265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4116789Sahrens */ 4117789Sahrens static int 4118789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4119789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4120789Sahrens { 4121789Sahrens znode_t *zp = VTOZ(vp); 4122789Sahrens page_t *pp, *cur_pp; 4123789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4124789Sahrens u_offset_t io_off, total; 4125789Sahrens size_t io_len; 4126789Sahrens int err; 4127789Sahrens 41282688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 41298636SMark.Maybee@Sun.COM /* 41308636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 41318636SMark.Maybee@Sun.COM */ 4132789Sahrens io_off = off; 4133789Sahrens io_len = PAGESIZE; 41349265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 41359265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4136789Sahrens } else { 4137789Sahrens /* 41388636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4139789Sahrens */ 4140789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 41418636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4142789Sahrens } 4143789Sahrens if (pp == NULL) { 4144789Sahrens /* 41458636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4146789Sahrens */ 4147789Sahrens *pl = NULL; 4148789Sahrens return (0); 4149789Sahrens } 4150789Sahrens 4151789Sahrens /* 4152789Sahrens * Fill the pages in the kluster. 4153789Sahrens */ 4154789Sahrens cur_pp = pp; 4155789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 41568636SMark.Maybee@Sun.COM caddr_t va; 41578636SMark.Maybee@Sun.COM 41582688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 41597315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 41609512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 41619512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 41627315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4163789Sahrens if (err) { 4164789Sahrens /* On error, toss the entire kluster */ 4165789Sahrens pvn_read_done(pp, B_ERROR); 41667294Sperrin /* convert checksum errors into IO errors */ 41677294Sperrin if (err == ECKSUM) 41687294Sperrin err = EIO; 4169789Sahrens return (err); 4170789Sahrens } 4171789Sahrens cur_pp = cur_pp->p_next; 4172789Sahrens } 41738636SMark.Maybee@Sun.COM 4174789Sahrens /* 41758636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 41768636SMark.Maybee@Sun.COM * from the desired offset `off'. 4177789Sahrens * NOTE: the page list will always be null terminated. 4178789Sahrens */ 4179789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 41808636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4181789Sahrens 4182789Sahrens return (0); 4183789Sahrens } 4184789Sahrens 4185789Sahrens /* 4186789Sahrens * Return pointers to the pages for the file region [off, off + len] 4187789Sahrens * in the pl array. If plsz is greater than len, this function may 41888636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 41898636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 41908636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 41918636SMark.Maybee@Sun.COM * part of a klustered read. 4192789Sahrens * 4193789Sahrens * IN: vp - vnode of file to get data from. 4194789Sahrens * off - position in file to get data from. 4195789Sahrens * len - amount of data to retrieve. 4196789Sahrens * plsz - length of provided page list. 4197789Sahrens * seg - segment to obtain pages for. 4198789Sahrens * addr - virtual address of fault. 4199789Sahrens * rw - mode of created pages. 4200789Sahrens * cr - credentials of caller. 42015331Samw * ct - caller context. 4202789Sahrens * 4203789Sahrens * OUT: protp - protection mode of created pages. 4204789Sahrens * pl - list of pages created. 4205789Sahrens * 4206789Sahrens * RETURN: 0 if success 4207789Sahrens * error code if failure 4208789Sahrens * 4209789Sahrens * Timestamps: 4210789Sahrens * vp - atime updated 4211789Sahrens */ 4212789Sahrens /* ARGSUSED */ 4213789Sahrens static int 4214789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4215789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 42165331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4217789Sahrens { 4218789Sahrens znode_t *zp = VTOZ(vp); 4219789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 42208636SMark.Maybee@Sun.COM page_t **pl0 = pl; 42218636SMark.Maybee@Sun.COM int err = 0; 42228636SMark.Maybee@Sun.COM 42238636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 42248636SMark.Maybee@Sun.COM if (pl == NULL) 42258636SMark.Maybee@Sun.COM return (0); 42268636SMark.Maybee@Sun.COM else if (len > plsz) 42278636SMark.Maybee@Sun.COM len = plsz; 42288681SMark.Maybee@Sun.COM else 42298681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 42308636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4231789Sahrens 42325367Sahrens ZFS_ENTER(zfsvfs); 42335367Sahrens ZFS_VERIFY_ZP(zp); 4234789Sahrens 4235789Sahrens if (protp) 4236789Sahrens *protp = PROT_ALL; 4237789Sahrens 4238789Sahrens /* 42399265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4240789Sahrens * for pages. If we don't find a page, we will need to create 4241789Sahrens * a new page and fill it with data from the file. 4242789Sahrens */ 4243789Sahrens while (len > 0) { 42448636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 42458636SMark.Maybee@Sun.COM *(pl+1) = NULL; 42468636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 42478636SMark.Maybee@Sun.COM goto out; 42488636SMark.Maybee@Sun.COM while (*pl) { 42498636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4250789Sahrens off += PAGESIZE; 4251789Sahrens addr += PAGESIZE; 42528681SMark.Maybee@Sun.COM if (len > 0) { 42538681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 42548636SMark.Maybee@Sun.COM len -= PAGESIZE; 42558681SMark.Maybee@Sun.COM } 42568636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4257789Sahrens plsz -= PAGESIZE; 42588636SMark.Maybee@Sun.COM pl++; 4259789Sahrens } 4260789Sahrens } 4261789Sahrens 4262789Sahrens /* 4263789Sahrens * Fill out the page array with any pages already in the cache. 4264789Sahrens */ 42658636SMark.Maybee@Sun.COM while (plsz > 0 && 42668636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 42678636SMark.Maybee@Sun.COM off += PAGESIZE; 42688636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4269789Sahrens } 4270789Sahrens out: 42712752Sperrin if (err) { 42722752Sperrin /* 42732752Sperrin * Release any pages we have previously locked. 42742752Sperrin */ 42752752Sperrin while (pl > pl0) 42762752Sperrin page_unlock(*--pl); 42778636SMark.Maybee@Sun.COM } else { 42788636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 42792752Sperrin } 42802752Sperrin 4281789Sahrens *pl = NULL; 4282789Sahrens 4283789Sahrens ZFS_EXIT(zfsvfs); 4284789Sahrens return (err); 4285789Sahrens } 4286789Sahrens 42871544Seschrock /* 42881544Seschrock * Request a memory map for a section of a file. This code interacts 42891544Seschrock * with common code and the VM system as follows: 42901544Seschrock * 42911544Seschrock * common code calls mmap(), which ends up in smmap_common() 42921544Seschrock * 42931544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 42941544Seschrock * 42951544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 42961544Seschrock * 42971544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 42981544Seschrock * 42991544Seschrock * zfs_addmap() updates z_mapcnt 43001544Seschrock */ 43015331Samw /*ARGSUSED*/ 4302789Sahrens static int 4303789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 43045331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 43055331Samw caller_context_t *ct) 4306789Sahrens { 4307789Sahrens znode_t *zp = VTOZ(vp); 4308789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4309789Sahrens segvn_crargs_t vn_a; 4310789Sahrens int error; 4311789Sahrens 43125929Smarks ZFS_ENTER(zfsvfs); 43135929Smarks ZFS_VERIFY_ZP(zp); 43145929Smarks 43155331Samw if ((prot & PROT_WRITE) && 43165331Samw (zp->z_phys->zp_flags & (ZFS_IMMUTABLE | ZFS_READONLY | 43175929Smarks ZFS_APPENDONLY))) { 43185929Smarks ZFS_EXIT(zfsvfs); 43195331Samw return (EPERM); 43205929Smarks } 43215929Smarks 43225929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 43235929Smarks (zp->z_phys->zp_flags & ZFS_AV_QUARANTINED)) { 43245929Smarks ZFS_EXIT(zfsvfs); 43255929Smarks return (EACCES); 43265929Smarks } 4327789Sahrens 4328789Sahrens if (vp->v_flag & VNOMAP) { 4329789Sahrens ZFS_EXIT(zfsvfs); 4330789Sahrens return (ENOSYS); 4331789Sahrens } 4332789Sahrens 4333789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4334789Sahrens ZFS_EXIT(zfsvfs); 4335789Sahrens return (ENXIO); 4336789Sahrens } 4337789Sahrens 4338789Sahrens if (vp->v_type != VREG) { 4339789Sahrens ZFS_EXIT(zfsvfs); 4340789Sahrens return (ENODEV); 4341789Sahrens } 4342789Sahrens 4343789Sahrens /* 4344789Sahrens * If file is locked, disallow mapping. 4345789Sahrens */ 43461544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 43471544Seschrock ZFS_EXIT(zfsvfs); 43481544Seschrock return (EAGAIN); 4349789Sahrens } 4350789Sahrens 4351789Sahrens as_rangelock(as); 43526036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 43536036Smec if (error != 0) { 43546036Smec as_rangeunlock(as); 43556036Smec ZFS_EXIT(zfsvfs); 43566036Smec return (error); 4357789Sahrens } 4358789Sahrens 4359789Sahrens vn_a.vp = vp; 4360789Sahrens vn_a.offset = (u_offset_t)off; 4361789Sahrens vn_a.type = flags & MAP_TYPE; 4362789Sahrens vn_a.prot = prot; 4363789Sahrens vn_a.maxprot = maxprot; 4364789Sahrens vn_a.cred = cr; 4365789Sahrens vn_a.amp = NULL; 4366789Sahrens vn_a.flags = flags & ~MAP_TYPE; 43671417Skchow vn_a.szc = 0; 43681417Skchow vn_a.lgrp_mem_policy_flags = 0; 4369789Sahrens 4370789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4371789Sahrens 4372789Sahrens as_rangeunlock(as); 4373789Sahrens ZFS_EXIT(zfsvfs); 4374789Sahrens return (error); 4375789Sahrens } 4376789Sahrens 4377789Sahrens /* ARGSUSED */ 4378789Sahrens static int 4379789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 43805331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 43815331Samw caller_context_t *ct) 4382789Sahrens { 43831544Seschrock uint64_t pages = btopr(len); 43841544Seschrock 43851544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4386789Sahrens return (0); 4387789Sahrens } 4388789Sahrens 43891773Seschrock /* 43901773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 43911773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 43921773Seschrock * detecting when the data was actually modified, we have to resort to 43931773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 43941773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 43951773Seschrock * which by far the most common case: 43961773Seschrock * 43971773Seschrock * open() 43981773Seschrock * mmap() 43991773Seschrock * <modify memory> 44001773Seschrock * munmap() 44011773Seschrock * close() 44021773Seschrock * <time lapse> 44031773Seschrock * putpage() via fsflush 44041773Seschrock * 44051773Seschrock * If we wait until fsflush to come along, we can have a modification time that 44061773Seschrock * is some arbitrary point in the future. In order to prevent this in the 44071773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 44081773Seschrock * torn down. 44091773Seschrock */ 4410789Sahrens /* ARGSUSED */ 4411789Sahrens static int 4412789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 44135331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 44145331Samw caller_context_t *ct) 4415789Sahrens { 44161544Seschrock uint64_t pages = btopr(len); 44171544Seschrock 44181544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 44191544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 44201773Seschrock 44211773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 44221773Seschrock vn_has_cached_data(vp)) 44235331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 44241773Seschrock 4425789Sahrens return (0); 4426789Sahrens } 4427789Sahrens 4428789Sahrens /* 4429789Sahrens * Free or allocate space in a file. Currently, this function only 4430789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4431789Sahrens * misnamed, as its functionality includes the ability to allocate as 4432789Sahrens * well as free space. 4433789Sahrens * 4434789Sahrens * IN: vp - vnode of file to free data in. 4435789Sahrens * cmd - action to take (only F_FREESP supported). 4436789Sahrens * bfp - section of file to free/alloc. 4437789Sahrens * flag - current file open mode flags. 4438789Sahrens * offset - current file offset. 4439789Sahrens * cr - credentials of caller [UNUSED]. 44405331Samw * ct - caller context. 4441789Sahrens * 4442789Sahrens * RETURN: 0 if success 4443789Sahrens * error code if failure 4444789Sahrens * 4445789Sahrens * Timestamps: 4446789Sahrens * vp - ctime|mtime updated 4447789Sahrens */ 4448789Sahrens /* ARGSUSED */ 4449789Sahrens static int 4450789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4451789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4452789Sahrens { 4453789Sahrens znode_t *zp = VTOZ(vp); 4454789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4455789Sahrens uint64_t off, len; 4456789Sahrens int error; 4457789Sahrens 44585367Sahrens ZFS_ENTER(zfsvfs); 44595367Sahrens ZFS_VERIFY_ZP(zp); 4460789Sahrens 4461789Sahrens if (cmd != F_FREESP) { 4462789Sahrens ZFS_EXIT(zfsvfs); 4463789Sahrens return (EINVAL); 4464789Sahrens } 4465789Sahrens 4466789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4467789Sahrens ZFS_EXIT(zfsvfs); 4468789Sahrens return (error); 4469789Sahrens } 4470789Sahrens 4471789Sahrens if (bfp->l_len < 0) { 4472789Sahrens ZFS_EXIT(zfsvfs); 4473789Sahrens return (EINVAL); 4474789Sahrens } 4475789Sahrens 4476789Sahrens off = bfp->l_start; 44771669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 44781878Smaybee 44796992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4480789Sahrens 4481789Sahrens ZFS_EXIT(zfsvfs); 4482789Sahrens return (error); 4483789Sahrens } 4484789Sahrens 44855331Samw /*ARGSUSED*/ 4486789Sahrens static int 44875331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4488789Sahrens { 4489789Sahrens znode_t *zp = VTOZ(vp); 4490789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 44915326Sek110237 uint32_t gen; 4492789Sahrens uint64_t object = zp->z_id; 4493789Sahrens zfid_short_t *zfid; 4494789Sahrens int size, i; 4495789Sahrens 44965367Sahrens ZFS_ENTER(zfsvfs); 44975367Sahrens ZFS_VERIFY_ZP(zp); 44985326Sek110237 gen = (uint32_t)zp->z_gen; 4499789Sahrens 4500789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4501789Sahrens if (fidp->fid_len < size) { 4502789Sahrens fidp->fid_len = size; 45031512Sek110237 ZFS_EXIT(zfsvfs); 4504789Sahrens return (ENOSPC); 4505789Sahrens } 4506789Sahrens 4507789Sahrens zfid = (zfid_short_t *)fidp; 4508789Sahrens 4509789Sahrens zfid->zf_len = size; 4510789Sahrens 4511789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4512789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4513789Sahrens 4514789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4515789Sahrens if (gen == 0) 4516789Sahrens gen = 1; 4517789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4518789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4519789Sahrens 4520789Sahrens if (size == LONG_FID_LEN) { 4521789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4522789Sahrens zfid_long_t *zlfid; 4523789Sahrens 4524789Sahrens zlfid = (zfid_long_t *)fidp; 4525789Sahrens 4526789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4527789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4528789Sahrens 4529789Sahrens /* XXX - this should be the generation number for the objset */ 4530789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4531789Sahrens zlfid->zf_setgen[i] = 0; 4532789Sahrens } 4533789Sahrens 4534789Sahrens ZFS_EXIT(zfsvfs); 4535789Sahrens return (0); 4536789Sahrens } 4537789Sahrens 4538789Sahrens static int 45395331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 45405331Samw caller_context_t *ct) 4541789Sahrens { 4542789Sahrens znode_t *zp, *xzp; 4543789Sahrens zfsvfs_t *zfsvfs; 4544789Sahrens zfs_dirlock_t *dl; 4545789Sahrens int error; 4546789Sahrens 4547789Sahrens switch (cmd) { 4548789Sahrens case _PC_LINK_MAX: 4549789Sahrens *valp = ULONG_MAX; 4550789Sahrens return (0); 4551789Sahrens 4552789Sahrens case _PC_FILESIZEBITS: 4553789Sahrens *valp = 64; 4554789Sahrens return (0); 4555789Sahrens 4556789Sahrens case _PC_XATTR_EXISTS: 4557789Sahrens zp = VTOZ(vp); 4558789Sahrens zfsvfs = zp->z_zfsvfs; 45595367Sahrens ZFS_ENTER(zfsvfs); 45605367Sahrens ZFS_VERIFY_ZP(zp); 4561789Sahrens *valp = 0; 4562789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 45635331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4564789Sahrens if (error == 0) { 4565789Sahrens zfs_dirent_unlock(dl); 4566789Sahrens if (!zfs_dirempty(xzp)) 4567789Sahrens *valp = 1; 4568789Sahrens VN_RELE(ZTOV(xzp)); 4569789Sahrens } else if (error == ENOENT) { 4570789Sahrens /* 4571789Sahrens * If there aren't extended attributes, it's the 4572789Sahrens * same as having zero of them. 4573789Sahrens */ 4574789Sahrens error = 0; 4575789Sahrens } 4576789Sahrens ZFS_EXIT(zfsvfs); 4577789Sahrens return (error); 4578789Sahrens 45795331Samw case _PC_SATTR_ENABLED: 45805331Samw case _PC_SATTR_EXISTS: 45817757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 45825331Samw (vp->v_type == VREG || vp->v_type == VDIR); 45835331Samw return (0); 45845331Samw 45859749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 45869749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 45879749STim.Haley@Sun.COM vp->v_type == VDIR; 45889749STim.Haley@Sun.COM return (0); 45899749STim.Haley@Sun.COM 4590789Sahrens case _PC_ACL_ENABLED: 4591789Sahrens *valp = _ACL_ACE_ENABLED; 4592789Sahrens return (0); 4593789Sahrens 4594789Sahrens case _PC_MIN_HOLE_SIZE: 4595789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4596789Sahrens return (0); 4597789Sahrens 459810440SRoger.Faulkner@Sun.COM case _PC_TIMESTAMP_RESOLUTION: 459910440SRoger.Faulkner@Sun.COM /* nanosecond timestamp resolution */ 460010440SRoger.Faulkner@Sun.COM *valp = 1L; 460110440SRoger.Faulkner@Sun.COM return (0); 460210440SRoger.Faulkner@Sun.COM 4603789Sahrens default: 46045331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4605789Sahrens } 4606789Sahrens } 4607789Sahrens 4608789Sahrens /*ARGSUSED*/ 4609789Sahrens static int 46105331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 46115331Samw caller_context_t *ct) 4612789Sahrens { 4613789Sahrens znode_t *zp = VTOZ(vp); 4614789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4615789Sahrens int error; 46165331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4617789Sahrens 46185367Sahrens ZFS_ENTER(zfsvfs); 46195367Sahrens ZFS_VERIFY_ZP(zp); 46205331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4621789Sahrens ZFS_EXIT(zfsvfs); 4622789Sahrens 4623789Sahrens return (error); 4624789Sahrens } 4625789Sahrens 4626789Sahrens /*ARGSUSED*/ 4627789Sahrens static int 46285331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 46295331Samw caller_context_t *ct) 4630789Sahrens { 4631789Sahrens znode_t *zp = VTOZ(vp); 4632789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4633789Sahrens int error; 46345331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4635789Sahrens 46365367Sahrens ZFS_ENTER(zfsvfs); 46375367Sahrens ZFS_VERIFY_ZP(zp); 46385331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 4639789Sahrens ZFS_EXIT(zfsvfs); 4640789Sahrens return (error); 4641789Sahrens } 4642789Sahrens 4643789Sahrens /* 4644*11539SChunli.Zhang@Sun.COM * Tunable, both must be a power of 2. 4645*11539SChunli.Zhang@Sun.COM * 4646*11539SChunli.Zhang@Sun.COM * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf 4647*11539SChunli.Zhang@Sun.COM * zcr_blksz_max: if set to less than the file block size, allow loaning out of 4648*11539SChunli.Zhang@Sun.COM * an arcbuf for a partial block read 4649*11539SChunli.Zhang@Sun.COM */ 4650*11539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10); /* 1K */ 4651*11539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17); /* 128K */ 4652*11539SChunli.Zhang@Sun.COM 4653*11539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 4654*11539SChunli.Zhang@Sun.COM static int 4655*11539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 4656*11539SChunli.Zhang@Sun.COM caller_context_t *ct) 4657*11539SChunli.Zhang@Sun.COM { 4658*11539SChunli.Zhang@Sun.COM znode_t *zp = VTOZ(vp); 4659*11539SChunli.Zhang@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4660*11539SChunli.Zhang@Sun.COM int max_blksz = zfsvfs->z_max_blksz; 4661*11539SChunli.Zhang@Sun.COM uio_t *uio = &xuio->xu_uio; 4662*11539SChunli.Zhang@Sun.COM ssize_t size = uio->uio_resid; 4663*11539SChunli.Zhang@Sun.COM offset_t offset = uio->uio_loffset; 4664*11539SChunli.Zhang@Sun.COM int blksz; 4665*11539SChunli.Zhang@Sun.COM int fullblk, i; 4666*11539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 4667*11539SChunli.Zhang@Sun.COM ssize_t maxsize; 4668*11539SChunli.Zhang@Sun.COM int preamble, postamble; 4669*11539SChunli.Zhang@Sun.COM 4670*11539SChunli.Zhang@Sun.COM if (xuio->xu_type != UIOTYPE_ZEROCOPY) 4671*11539SChunli.Zhang@Sun.COM return (EINVAL); 4672*11539SChunli.Zhang@Sun.COM 4673*11539SChunli.Zhang@Sun.COM ZFS_ENTER(zfsvfs); 4674*11539SChunli.Zhang@Sun.COM ZFS_VERIFY_ZP(zp); 4675*11539SChunli.Zhang@Sun.COM switch (ioflag) { 4676*11539SChunli.Zhang@Sun.COM case UIO_WRITE: 4677*11539SChunli.Zhang@Sun.COM /* 4678*11539SChunli.Zhang@Sun.COM * Loan out an arc_buf for write if write size is bigger than 4679*11539SChunli.Zhang@Sun.COM * max_blksz, and the file's block size is also max_blksz. 4680*11539SChunli.Zhang@Sun.COM */ 4681*11539SChunli.Zhang@Sun.COM blksz = max_blksz; 4682*11539SChunli.Zhang@Sun.COM if (size < blksz || zp->z_blksz != blksz) { 4683*11539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 4684*11539SChunli.Zhang@Sun.COM return (EINVAL); 4685*11539SChunli.Zhang@Sun.COM } 4686*11539SChunli.Zhang@Sun.COM /* 4687*11539SChunli.Zhang@Sun.COM * Caller requests buffers for write before knowing where the 4688*11539SChunli.Zhang@Sun.COM * write offset might be (e.g. NFS TCP write). 4689*11539SChunli.Zhang@Sun.COM */ 4690*11539SChunli.Zhang@Sun.COM if (offset == -1) { 4691*11539SChunli.Zhang@Sun.COM preamble = 0; 4692*11539SChunli.Zhang@Sun.COM } else { 4693*11539SChunli.Zhang@Sun.COM preamble = P2PHASE(offset, blksz); 4694*11539SChunli.Zhang@Sun.COM if (preamble) { 4695*11539SChunli.Zhang@Sun.COM preamble = blksz - preamble; 4696*11539SChunli.Zhang@Sun.COM size -= preamble; 4697*11539SChunli.Zhang@Sun.COM } 4698*11539SChunli.Zhang@Sun.COM } 4699*11539SChunli.Zhang@Sun.COM 4700*11539SChunli.Zhang@Sun.COM postamble = P2PHASE(size, blksz); 4701*11539SChunli.Zhang@Sun.COM size -= postamble; 4702*11539SChunli.Zhang@Sun.COM 4703*11539SChunli.Zhang@Sun.COM fullblk = size / blksz; 4704*11539SChunli.Zhang@Sun.COM dmu_xuio_init(xuio, 4705*11539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 4706*11539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 4707*11539SChunli.Zhang@Sun.COM int, postamble, int, 4708*11539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 4709*11539SChunli.Zhang@Sun.COM 4710*11539SChunli.Zhang@Sun.COM /* 4711*11539SChunli.Zhang@Sun.COM * Have to fix iov base/len for partial buffers. They 4712*11539SChunli.Zhang@Sun.COM * currently represent full arc_buf's. 4713*11539SChunli.Zhang@Sun.COM */ 4714*11539SChunli.Zhang@Sun.COM if (preamble) { 4715*11539SChunli.Zhang@Sun.COM /* data begins in the middle of the arc_buf */ 4716*11539SChunli.Zhang@Sun.COM abuf = dmu_request_arcbuf(zp->z_dbuf, blksz); 4717*11539SChunli.Zhang@Sun.COM ASSERT(abuf); 4718*11539SChunli.Zhang@Sun.COM dmu_xuio_add(xuio, abuf, blksz - preamble, preamble); 4719*11539SChunli.Zhang@Sun.COM } 4720*11539SChunli.Zhang@Sun.COM 4721*11539SChunli.Zhang@Sun.COM for (i = 0; i < fullblk; i++) { 4722*11539SChunli.Zhang@Sun.COM abuf = dmu_request_arcbuf(zp->z_dbuf, blksz); 4723*11539SChunli.Zhang@Sun.COM ASSERT(abuf); 4724*11539SChunli.Zhang@Sun.COM dmu_xuio_add(xuio, abuf, 0, blksz); 4725*11539SChunli.Zhang@Sun.COM } 4726*11539SChunli.Zhang@Sun.COM 4727*11539SChunli.Zhang@Sun.COM if (postamble) { 4728*11539SChunli.Zhang@Sun.COM /* data ends in the middle of the arc_buf */ 4729*11539SChunli.Zhang@Sun.COM abuf = dmu_request_arcbuf(zp->z_dbuf, blksz); 4730*11539SChunli.Zhang@Sun.COM ASSERT(abuf); 4731*11539SChunli.Zhang@Sun.COM dmu_xuio_add(xuio, abuf, 0, postamble); 4732*11539SChunli.Zhang@Sun.COM } 4733*11539SChunli.Zhang@Sun.COM break; 4734*11539SChunli.Zhang@Sun.COM case UIO_READ: 4735*11539SChunli.Zhang@Sun.COM /* 4736*11539SChunli.Zhang@Sun.COM * Loan out an arc_buf for read if the read size is larger than 4737*11539SChunli.Zhang@Sun.COM * the current file block size. Block alignment is not 4738*11539SChunli.Zhang@Sun.COM * considered. Partial arc_buf will be loaned out for read. 4739*11539SChunli.Zhang@Sun.COM */ 4740*11539SChunli.Zhang@Sun.COM blksz = zp->z_blksz; 4741*11539SChunli.Zhang@Sun.COM if (blksz < zcr_blksz_min) 4742*11539SChunli.Zhang@Sun.COM blksz = zcr_blksz_min; 4743*11539SChunli.Zhang@Sun.COM if (blksz > zcr_blksz_max) 4744*11539SChunli.Zhang@Sun.COM blksz = zcr_blksz_max; 4745*11539SChunli.Zhang@Sun.COM /* avoid potential complexity of dealing with it */ 4746*11539SChunli.Zhang@Sun.COM if (blksz > max_blksz) { 4747*11539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 4748*11539SChunli.Zhang@Sun.COM return (EINVAL); 4749*11539SChunli.Zhang@Sun.COM } 4750*11539SChunli.Zhang@Sun.COM 4751*11539SChunli.Zhang@Sun.COM maxsize = zp->z_phys->zp_size - uio->uio_loffset; 4752*11539SChunli.Zhang@Sun.COM if (size > maxsize) 4753*11539SChunli.Zhang@Sun.COM size = maxsize; 4754*11539SChunli.Zhang@Sun.COM 4755*11539SChunli.Zhang@Sun.COM if (size < blksz || vn_has_cached_data(vp)) { 4756*11539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 4757*11539SChunli.Zhang@Sun.COM return (EINVAL); 4758*11539SChunli.Zhang@Sun.COM } 4759*11539SChunli.Zhang@Sun.COM break; 4760*11539SChunli.Zhang@Sun.COM default: 4761*11539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 4762*11539SChunli.Zhang@Sun.COM return (EINVAL); 4763*11539SChunli.Zhang@Sun.COM } 4764*11539SChunli.Zhang@Sun.COM 4765*11539SChunli.Zhang@Sun.COM uio->uio_extflg = UIO_XUIO; 4766*11539SChunli.Zhang@Sun.COM XUIO_XUZC_RW(xuio) = ioflag; 4767*11539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 4768*11539SChunli.Zhang@Sun.COM return (0); 4769*11539SChunli.Zhang@Sun.COM } 4770*11539SChunli.Zhang@Sun.COM 4771*11539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 4772*11539SChunli.Zhang@Sun.COM static int 4773*11539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 4774*11539SChunli.Zhang@Sun.COM { 4775*11539SChunli.Zhang@Sun.COM int i; 4776*11539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 4777*11539SChunli.Zhang@Sun.COM int ioflag = XUIO_XUZC_RW(xuio); 4778*11539SChunli.Zhang@Sun.COM 4779*11539SChunli.Zhang@Sun.COM ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 4780*11539SChunli.Zhang@Sun.COM 4781*11539SChunli.Zhang@Sun.COM i = dmu_xuio_cnt(xuio); 4782*11539SChunli.Zhang@Sun.COM while (i-- > 0) { 4783*11539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i); 4784*11539SChunli.Zhang@Sun.COM /* 4785*11539SChunli.Zhang@Sun.COM * if abuf == NULL, it must be a write buffer 4786*11539SChunli.Zhang@Sun.COM * that has been returned in zfs_write(). 4787*11539SChunli.Zhang@Sun.COM */ 4788*11539SChunli.Zhang@Sun.COM if (abuf) 4789*11539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 4790*11539SChunli.Zhang@Sun.COM ASSERT(abuf || ioflag == UIO_WRITE); 4791*11539SChunli.Zhang@Sun.COM } 4792*11539SChunli.Zhang@Sun.COM 4793*11539SChunli.Zhang@Sun.COM dmu_xuio_fini(xuio); 4794*11539SChunli.Zhang@Sun.COM return (0); 4795*11539SChunli.Zhang@Sun.COM } 4796*11539SChunli.Zhang@Sun.COM 4797*11539SChunli.Zhang@Sun.COM /* 4798789Sahrens * Predeclare these here so that the compiler assumes that 4799789Sahrens * this is an "old style" function declaration that does 4800789Sahrens * not include arguments => we won't get type mismatch errors 4801789Sahrens * in the initializations that follow. 4802789Sahrens */ 4803789Sahrens static int zfs_inval(); 4804789Sahrens static int zfs_isdir(); 4805789Sahrens 4806789Sahrens static int 4807789Sahrens zfs_inval() 4808789Sahrens { 4809789Sahrens return (EINVAL); 4810789Sahrens } 4811789Sahrens 4812789Sahrens static int 4813789Sahrens zfs_isdir() 4814789Sahrens { 4815789Sahrens return (EISDIR); 4816789Sahrens } 4817789Sahrens /* 4818789Sahrens * Directory vnode operations template 4819789Sahrens */ 4820789Sahrens vnodeops_t *zfs_dvnodeops; 4821789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 48223898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 48233898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 48243898Srsb VOPNAME_READ, { .error = zfs_isdir }, 48253898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 48263898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 48273898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 48283898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 48293898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 48303898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 48313898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 48323898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 48333898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 48343898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 48353898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 48363898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 48373898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 48383898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 48393898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 48403898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 48413898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 48423898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 48433898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 48443898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 48453898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 48464863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 48473898Srsb NULL, NULL 4848789Sahrens }; 4849789Sahrens 4850789Sahrens /* 4851789Sahrens * Regular file vnode operations template 4852789Sahrens */ 4853789Sahrens vnodeops_t *zfs_fvnodeops; 4854789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 48553898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 48563898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 48573898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 48583898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 48593898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 48603898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 48613898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 48623898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 48633898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 48643898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 48653898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 48663898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 48673898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 48683898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 48693898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 48703898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 48713898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 48723898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 48733898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 48743898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 48753898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 48763898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 48773898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 48783898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 48793898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 4880*11539SChunli.Zhang@Sun.COM VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 4881*11539SChunli.Zhang@Sun.COM VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 48823898Srsb NULL, NULL 4883789Sahrens }; 4884789Sahrens 4885789Sahrens /* 4886789Sahrens * Symbolic link vnode operations template 4887789Sahrens */ 4888789Sahrens vnodeops_t *zfs_symvnodeops; 4889789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 48903898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 48913898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 48923898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 48933898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 48943898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 48953898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 48963898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 48973898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 48983898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 48993898Srsb NULL, NULL 4900789Sahrens }; 4901789Sahrens 4902789Sahrens /* 49038845Samw@Sun.COM * special share hidden files vnode operations template 49048845Samw@Sun.COM */ 49058845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 49068845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 49078845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 49088845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 49098845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 49108845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 49118845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 49128845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 49138845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 49148845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 49158845Samw@Sun.COM NULL, NULL 49168845Samw@Sun.COM }; 49178845Samw@Sun.COM 49188845Samw@Sun.COM /* 4919789Sahrens * Extended attribute directory vnode operations template 4920789Sahrens * This template is identical to the directory vnodes 4921789Sahrens * operation template except for restricted operations: 4922789Sahrens * VOP_MKDIR() 4923789Sahrens * VOP_SYMLINK() 4924789Sahrens * Note that there are other restrictions embedded in: 4925789Sahrens * zfs_create() - restrict type to VREG 4926789Sahrens * zfs_link() - no links into/out of attribute space 4927789Sahrens * zfs_rename() - no moves into/out of attribute space 4928789Sahrens */ 4929789Sahrens vnodeops_t *zfs_xdvnodeops; 4930789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 49313898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 49323898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 49333898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 49343898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 49353898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 49363898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 49373898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 49383898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 49393898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 49403898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 49413898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 49423898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 49433898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 49443898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 49453898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 49463898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 49473898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 49483898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 49493898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 49503898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 49513898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 49523898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 49533898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 49543898Srsb NULL, NULL 4955789Sahrens }; 4956789Sahrens 4957789Sahrens /* 4958789Sahrens * Error vnode operations template 4959789Sahrens */ 4960789Sahrens vnodeops_t *zfs_evnodeops; 4961789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 49623898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 49633898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 49643898Srsb NULL, NULL 4965789Sahrens }; 4966