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 /* 2212079SMark.Shellenbaum@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. 23789Sahrens */ 24789Sahrens 254144Speteh /* Portions Copyright 2007 Jeremy Teo */ 2612294SMark.Musante@Sun.COM /* Portions Copyright 2010 Robert Milkowski */ 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> 6012294SMark.Musante@Sun.COM #include <sys/dmu_objset.h> 61789Sahrens #include <sys/spa.h> 62789Sahrens #include <sys/txg.h> 63789Sahrens #include <sys/dbuf.h> 64789Sahrens #include <sys/zap.h> 6511935SMark.Shellenbaum@Sun.COM #include <sys/sa.h> 66789Sahrens #include <sys/dirent.h> 67789Sahrens #include <sys/policy.h> 68789Sahrens #include <sys/sunddi.h> 69789Sahrens #include <sys/filio.h> 707847SMark.Shellenbaum@Sun.COM #include <sys/sid.h> 71789Sahrens #include "fs/fs_subr.h" 72789Sahrens #include <sys/zfs_ctldir.h> 735331Samw #include <sys/zfs_fuid.h> 7411935SMark.Shellenbaum@Sun.COM #include <sys/zfs_sa.h> 751484Sek110237 #include <sys/dnlc.h> 761669Sperrin #include <sys/zfs_rlock.h> 775331Samw #include <sys/extdirent.h> 785331Samw #include <sys/kidmap.h> 7911134SCasper.Dik@Sun.COM #include <sys/cred.h> 805663Sck153898 #include <sys/attr.h> 81789Sahrens 82789Sahrens /* 83789Sahrens * Programming rules. 84789Sahrens * 85789Sahrens * Each vnode op performs some logical unit of work. To do this, the ZPL must 86789Sahrens * properly lock its in-core state, create a DMU transaction, do the work, 87789Sahrens * record this work in the intent log (ZIL), commit the DMU transaction, 885331Samw * and wait for the intent log to commit if it is a synchronous operation. 895331Samw * Moreover, the vnode ops must work in both normal and log replay context. 90789Sahrens * The ordering of events is important to avoid deadlocks and references 91789Sahrens * to freed memory. The example below illustrates the following Big Rules: 92789Sahrens * 93789Sahrens * (1) A check must be made in each zfs thread for a mounted file system. 945367Sahrens * This is done avoiding races using ZFS_ENTER(zfsvfs). 955367Sahrens * A ZFS_EXIT(zfsvfs) is needed before all returns. Any znodes 965367Sahrens * must be checked with ZFS_VERIFY_ZP(zp). Both of these macros 975367Sahrens * can return EIO from the calling function. 98789Sahrens * 99789Sahrens * (2) VN_RELE() should always be the last thing except for zil_commit() 1002638Sperrin * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 101789Sahrens * First, if it's the last reference, the vnode/znode 102789Sahrens * can be freed, so the zp may point to freed memory. Second, the last 103789Sahrens * reference will call zfs_zinactive(), which may induce a lot of work -- 1041669Sperrin * pushing cached pages (which acquires range locks) and syncing out 105789Sahrens * cached atime changes. Third, zfs_zinactive() may require a new tx, 106789Sahrens * which could deadlock the system if you were already holding one. 1079321SNeil.Perrin@Sun.COM * If you must call VN_RELE() within a tx then use VN_RELE_ASYNC(). 108789Sahrens * 1091757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 1101757Sperrin * as they can span dmu_tx_assign() calls. 1111757Sperrin * 1128227SNeil.Perrin@Sun.COM * (4) Always pass TXG_NOWAIT as the second argument to dmu_tx_assign(). 113789Sahrens * This is critical because we don't want to block while holding locks. 114789Sahrens * Note, in particular, that if a lock is sometimes acquired before 115789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 116789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 117789Sahrens * 118789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 119789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 120789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 121789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 122789Sahrens * 123789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1242113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 125789Sahrens * 1261757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 127789Sahrens * before dropping locks. This ensures that the ordering of events 128789Sahrens * in the intent log matches the order in which they actually occurred. 1298227SNeil.Perrin@Sun.COM * During ZIL replay the zfs_log_* functions will update the sequence 1308227SNeil.Perrin@Sun.COM * number to indicate the zil transaction has replayed. 131789Sahrens * 1321757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 133789Sahrens * regardless of whether there were any errors. 134789Sahrens * 135*12699SNeil.Perrin@Sun.COM * (7) After dropping all locks, invoke zil_commit(zilog, foid) 136789Sahrens * to ensure that synchronous semantics are provided when necessary. 137789Sahrens * 138789Sahrens * In general, this is how things should be ordered in each vnode op: 139789Sahrens * 140789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 141789Sahrens * top: 142789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 143789Sahrens * rw_enter(...); // grab any other locks you need 144789Sahrens * tx = dmu_tx_create(...); // get DMU tx 145789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 1468227SNeil.Perrin@Sun.COM * error = dmu_tx_assign(tx, TXG_NOWAIT); // try to assign 147789Sahrens * if (error) { 148789Sahrens * rw_exit(...); // drop locks 149789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 150789Sahrens * VN_RELE(...); // release held vnodes 1518227SNeil.Perrin@Sun.COM * if (error == ERESTART) { 1522113Sahrens * dmu_tx_wait(tx); 1532113Sahrens * dmu_tx_abort(tx); 154789Sahrens * goto top; 155789Sahrens * } 1562113Sahrens * dmu_tx_abort(tx); // abort DMU tx 157789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 158789Sahrens * return (error); // really out of space 159789Sahrens * } 160789Sahrens * error = do_real_work(); // do whatever this VOP does 161789Sahrens * if (error == 0) 1622638Sperrin * zfs_log_*(...); // on success, make ZIL entry 163789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 164789Sahrens * rw_exit(...); // drop locks 165789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 166789Sahrens * VN_RELE(...); // release held vnodes 167*12699SNeil.Perrin@Sun.COM * zil_commit(zilog, foid); // synchronous when necessary 168789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 169789Sahrens * return (error); // done, report error 170789Sahrens */ 1715367Sahrens 172789Sahrens /* ARGSUSED */ 173789Sahrens static int 1745331Samw zfs_open(vnode_t **vpp, int flag, cred_t *cr, caller_context_t *ct) 175789Sahrens { 1763063Sperrin znode_t *zp = VTOZ(*vpp); 1777844SMark.Shellenbaum@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1787844SMark.Shellenbaum@Sun.COM 1797844SMark.Shellenbaum@Sun.COM ZFS_ENTER(zfsvfs); 1807844SMark.Shellenbaum@Sun.COM ZFS_VERIFY_ZP(zp); 1813063Sperrin 18211935SMark.Shellenbaum@Sun.COM if ((flag & FWRITE) && (zp->z_pflags & ZFS_APPENDONLY) && 1835331Samw ((flag & FAPPEND) == 0)) { 1847844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1855331Samw return (EPERM); 1865331Samw } 1875331Samw 1885331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 1895331Samw ZTOV(zp)->v_type == VREG && 19011935SMark.Shellenbaum@Sun.COM !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) { 1917844SMark.Shellenbaum@Sun.COM if (fs_vscan(*vpp, cr, 0) != 0) { 1927844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 1935331Samw return (EACCES); 1947844SMark.Shellenbaum@Sun.COM } 1957844SMark.Shellenbaum@Sun.COM } 1965331Samw 1973063Sperrin /* Keep a count of the synchronous opens in the znode */ 1983063Sperrin if (flag & (FSYNC | FDSYNC)) 1993063Sperrin atomic_inc_32(&zp->z_sync_cnt); 2005331Samw 2017844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 202789Sahrens return (0); 203789Sahrens } 204789Sahrens 205789Sahrens /* ARGSUSED */ 206789Sahrens static int 2075331Samw zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr, 2085331Samw caller_context_t *ct) 209789Sahrens { 2103063Sperrin znode_t *zp = VTOZ(vp); 2117844SMark.Shellenbaum@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2127844SMark.Shellenbaum@Sun.COM 2139909Schris.kirby@sun.com /* 2149909Schris.kirby@sun.com * Clean up any locks held by this process on the vp. 2159909Schris.kirby@sun.com */ 2169909Schris.kirby@sun.com cleanlocks(vp, ddi_get_pid(), 0); 2179909Schris.kirby@sun.com cleanshares(vp, ddi_get_pid()); 2189909Schris.kirby@sun.com 2197844SMark.Shellenbaum@Sun.COM ZFS_ENTER(zfsvfs); 2207844SMark.Shellenbaum@Sun.COM ZFS_VERIFY_ZP(zp); 2213063Sperrin 2223063Sperrin /* Decrement the synchronous opens in the znode */ 2234339Sperrin if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 2243063Sperrin atomic_dec_32(&zp->z_sync_cnt); 2253063Sperrin 2265331Samw if (!zfs_has_ctldir(zp) && zp->z_zfsvfs->z_vscan && 2275331Samw ZTOV(zp)->v_type == VREG && 22811935SMark.Shellenbaum@Sun.COM !(zp->z_pflags & ZFS_AV_QUARANTINED) && zp->z_size > 0) 2295331Samw VERIFY(fs_vscan(vp, cr, 1) == 0); 2305331Samw 2317844SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 232789Sahrens return (0); 233789Sahrens } 234789Sahrens 235789Sahrens /* 236789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 237789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 238789Sahrens */ 239789Sahrens static int 240789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 241789Sahrens { 242789Sahrens znode_t *zp = VTOZ(vp); 243789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 244789Sahrens uint64_t file_sz; 245789Sahrens int error; 246789Sahrens boolean_t hole; 247789Sahrens 24811935SMark.Shellenbaum@Sun.COM file_sz = zp->z_size; 249789Sahrens if (noff >= file_sz) { 250789Sahrens return (ENXIO); 251789Sahrens } 252789Sahrens 253789Sahrens if (cmd == _FIO_SEEK_HOLE) 254789Sahrens hole = B_TRUE; 255789Sahrens else 256789Sahrens hole = B_FALSE; 257789Sahrens 258789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 259789Sahrens 260789Sahrens /* end of file? */ 261789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 262789Sahrens /* 263789Sahrens * Handle the virtual hole at the end of file. 264789Sahrens */ 265789Sahrens if (hole) { 266789Sahrens *off = file_sz; 267789Sahrens return (0); 268789Sahrens } 269789Sahrens return (ENXIO); 270789Sahrens } 271789Sahrens 272789Sahrens if (noff < *off) 273789Sahrens return (error); 274789Sahrens *off = noff; 275789Sahrens return (error); 276789Sahrens } 277789Sahrens 278789Sahrens /* ARGSUSED */ 279789Sahrens static int 280789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 2815331Samw int *rvalp, caller_context_t *ct) 282789Sahrens { 283789Sahrens offset_t off; 284789Sahrens int error; 285789Sahrens zfsvfs_t *zfsvfs; 2865326Sek110237 znode_t *zp; 287789Sahrens 288789Sahrens switch (com) { 2894339Sperrin case _FIOFFS: 290789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 291789Sahrens 2921544Seschrock /* 2931544Seschrock * The following two ioctls are used by bfu. Faking out, 2941544Seschrock * necessary to avoid bfu errors. 2951544Seschrock */ 2964339Sperrin case _FIOGDIO: 2974339Sperrin case _FIOSDIO: 2981544Seschrock return (0); 2991544Seschrock 3004339Sperrin case _FIO_SEEK_DATA: 3014339Sperrin case _FIO_SEEK_HOLE: 302789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 303789Sahrens return (EFAULT); 304789Sahrens 3055326Sek110237 zp = VTOZ(vp); 3065326Sek110237 zfsvfs = zp->z_zfsvfs; 3075367Sahrens ZFS_ENTER(zfsvfs); 3085367Sahrens ZFS_VERIFY_ZP(zp); 309789Sahrens 310789Sahrens /* offset parameter is in/out */ 311789Sahrens error = zfs_holey(vp, com, &off); 312789Sahrens ZFS_EXIT(zfsvfs); 313789Sahrens if (error) 314789Sahrens return (error); 315789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 316789Sahrens return (EFAULT); 317789Sahrens return (0); 318789Sahrens } 319789Sahrens return (ENOTTY); 320789Sahrens } 321789Sahrens 322789Sahrens /* 3237315SJonathan.Adams@Sun.COM * Utility functions to map and unmap a single physical page. These 3247315SJonathan.Adams@Sun.COM * are used to manage the mappable copies of ZFS file data, and therefore 3257315SJonathan.Adams@Sun.COM * do not update ref/mod bits. 3267315SJonathan.Adams@Sun.COM */ 3277315SJonathan.Adams@Sun.COM caddr_t 3287315SJonathan.Adams@Sun.COM zfs_map_page(page_t *pp, enum seg_rw rw) 3297315SJonathan.Adams@Sun.COM { 3307315SJonathan.Adams@Sun.COM if (kpm_enable) 3317315SJonathan.Adams@Sun.COM return (hat_kpm_mapin(pp, 0)); 3327315SJonathan.Adams@Sun.COM ASSERT(rw == S_READ || rw == S_WRITE); 3337315SJonathan.Adams@Sun.COM return (ppmapin(pp, PROT_READ | ((rw == S_WRITE) ? PROT_WRITE : 0), 3347315SJonathan.Adams@Sun.COM (caddr_t)-1)); 3357315SJonathan.Adams@Sun.COM } 3367315SJonathan.Adams@Sun.COM 3377315SJonathan.Adams@Sun.COM void 3387315SJonathan.Adams@Sun.COM zfs_unmap_page(page_t *pp, caddr_t addr) 3397315SJonathan.Adams@Sun.COM { 3407315SJonathan.Adams@Sun.COM if (kpm_enable) { 3417315SJonathan.Adams@Sun.COM hat_kpm_mapout(pp, 0, addr); 3427315SJonathan.Adams@Sun.COM } else { 3437315SJonathan.Adams@Sun.COM ppmapout(addr); 3447315SJonathan.Adams@Sun.COM } 3457315SJonathan.Adams@Sun.COM } 3467315SJonathan.Adams@Sun.COM 3477315SJonathan.Adams@Sun.COM /* 348789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 349789Sahrens * between the DMU cache and the memory mapped pages. What this means: 350789Sahrens * 351789Sahrens * On Write: If we find a memory mapped page, we write to *both* 352789Sahrens * the page and the dmu buffer. 353789Sahrens */ 3548636SMark.Maybee@Sun.COM static void 3558636SMark.Maybee@Sun.COM update_pages(vnode_t *vp, int64_t start, int len, objset_t *os, uint64_t oid) 356789Sahrens { 3578636SMark.Maybee@Sun.COM int64_t off; 3588636SMark.Maybee@Sun.COM 359789Sahrens off = start & PAGEOFFSET; 360789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 361789Sahrens page_t *pp; 3628636SMark.Maybee@Sun.COM uint64_t nbytes = MIN(PAGESIZE - off, len); 3638636SMark.Maybee@Sun.COM 364789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 365789Sahrens caddr_t va; 366789Sahrens 3677315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_WRITE); 3689512SNeil.Perrin@Sun.COM (void) dmu_read(os, oid, start+off, nbytes, va+off, 3699512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 3707315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 371789Sahrens page_unlock(pp); 372789Sahrens } 3738636SMark.Maybee@Sun.COM len -= nbytes; 374789Sahrens off = 0; 375789Sahrens } 376789Sahrens } 377789Sahrens 378789Sahrens /* 379789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 380789Sahrens * between the DMU cache and the memory mapped pages. What this means: 381789Sahrens * 382789Sahrens * On Read: We "read" preferentially from memory mapped pages, 383789Sahrens * else we default from the dmu buffer. 384789Sahrens * 385789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 386789Sahrens * the file is memory mapped. 387789Sahrens */ 388789Sahrens static int 3893638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio) 390789Sahrens { 3913638Sbillm znode_t *zp = VTOZ(vp); 3923638Sbillm objset_t *os = zp->z_zfsvfs->z_os; 3933638Sbillm int64_t start, off; 394789Sahrens int len = nbytes; 395789Sahrens int error = 0; 396789Sahrens 397789Sahrens start = uio->uio_loffset; 398789Sahrens off = start & PAGEOFFSET; 399789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 400789Sahrens page_t *pp; 4013638Sbillm uint64_t bytes = MIN(PAGESIZE - off, len); 4023638Sbillm 403789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 404789Sahrens caddr_t va; 405789Sahrens 4067315SJonathan.Adams@Sun.COM va = zfs_map_page(pp, S_READ); 407789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 4087315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 409789Sahrens page_unlock(pp); 410789Sahrens } else { 4113638Sbillm error = dmu_read_uio(os, zp->z_id, uio, bytes); 412789Sahrens } 413789Sahrens len -= bytes; 414789Sahrens off = 0; 415789Sahrens if (error) 416789Sahrens break; 417789Sahrens } 418789Sahrens return (error); 419789Sahrens } 420789Sahrens 4213638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 422789Sahrens 423789Sahrens /* 424789Sahrens * Read bytes from specified file into supplied buffer. 425789Sahrens * 426789Sahrens * IN: vp - vnode of file to be read from. 427789Sahrens * uio - structure supplying read location, range info, 428789Sahrens * and return buffer. 429789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 430789Sahrens * cr - credentials of caller. 4315331Samw * ct - caller context 432789Sahrens * 433789Sahrens * OUT: uio - updated offset and range, buffer filled. 434789Sahrens * 435789Sahrens * RETURN: 0 if success 436789Sahrens * error code if failure 437789Sahrens * 438789Sahrens * Side Effects: 439789Sahrens * vp - atime updated if byte count > 0 440789Sahrens */ 441789Sahrens /* ARGSUSED */ 442789Sahrens static int 443789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 444789Sahrens { 445789Sahrens znode_t *zp = VTOZ(vp); 446789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4475326Sek110237 objset_t *os; 4483638Sbillm ssize_t n, nbytes; 4493638Sbillm int error; 4501669Sperrin rl_t *rl; 45111539SChunli.Zhang@Sun.COM xuio_t *xuio = NULL; 452789Sahrens 4535367Sahrens ZFS_ENTER(zfsvfs); 4545367Sahrens ZFS_VERIFY_ZP(zp); 4555326Sek110237 os = zfsvfs->z_os; 456789Sahrens 45711935SMark.Shellenbaum@Sun.COM if (zp->z_pflags & ZFS_AV_QUARANTINED) { 4585929Smarks ZFS_EXIT(zfsvfs); 4595929Smarks return (EACCES); 4605929Smarks } 4615929Smarks 462789Sahrens /* 463789Sahrens * Validate file offset 464789Sahrens */ 465789Sahrens if (uio->uio_loffset < (offset_t)0) { 466789Sahrens ZFS_EXIT(zfsvfs); 467789Sahrens return (EINVAL); 468789Sahrens } 469789Sahrens 470789Sahrens /* 471789Sahrens * Fasttrack empty reads 472789Sahrens */ 473789Sahrens if (uio->uio_resid == 0) { 474789Sahrens ZFS_EXIT(zfsvfs); 475789Sahrens return (0); 476789Sahrens } 477789Sahrens 478789Sahrens /* 4791669Sperrin * Check for mandatory locks 480789Sahrens */ 48111935SMark.Shellenbaum@Sun.COM if (MANDMODE(zp->z_mode)) { 482789Sahrens if (error = chklock(vp, FREAD, 483789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 484789Sahrens ZFS_EXIT(zfsvfs); 485789Sahrens return (error); 486789Sahrens } 487789Sahrens } 488789Sahrens 489789Sahrens /* 490789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 491789Sahrens */ 49212294SMark.Musante@Sun.COM if (ioflag & FRSYNC || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 493*12699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 494789Sahrens 495789Sahrens /* 4961669Sperrin * Lock the range against changes. 497789Sahrens */ 4981669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4991669Sperrin 500789Sahrens /* 501789Sahrens * If we are reading past end-of-file we can skip 502789Sahrens * to the end; but we might still need to set atime. 503789Sahrens */ 50411935SMark.Shellenbaum@Sun.COM if (uio->uio_loffset >= zp->z_size) { 505789Sahrens error = 0; 506789Sahrens goto out; 507789Sahrens } 508789Sahrens 50911935SMark.Shellenbaum@Sun.COM ASSERT(uio->uio_loffset < zp->z_size); 51011935SMark.Shellenbaum@Sun.COM n = MIN(uio->uio_resid, zp->z_size - uio->uio_loffset); 5113638Sbillm 51211539SChunli.Zhang@Sun.COM if ((uio->uio_extflg == UIO_XUIO) && 51311539SChunli.Zhang@Sun.COM (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) { 51411539SChunli.Zhang@Sun.COM int nblk; 51511539SChunli.Zhang@Sun.COM int blksz = zp->z_blksz; 51611539SChunli.Zhang@Sun.COM uint64_t offset = uio->uio_loffset; 51711539SChunli.Zhang@Sun.COM 51811539SChunli.Zhang@Sun.COM xuio = (xuio_t *)uio; 51911539SChunli.Zhang@Sun.COM if ((ISP2(blksz))) { 52011539SChunli.Zhang@Sun.COM nblk = (P2ROUNDUP(offset + n, blksz) - P2ALIGN(offset, 52111539SChunli.Zhang@Sun.COM blksz)) / blksz; 52211539SChunli.Zhang@Sun.COM } else { 52311539SChunli.Zhang@Sun.COM ASSERT(offset + n <= blksz); 52411539SChunli.Zhang@Sun.COM nblk = 1; 52511539SChunli.Zhang@Sun.COM } 52611576SSurya.Prakki@Sun.COM (void) dmu_xuio_init(xuio, nblk); 52711539SChunli.Zhang@Sun.COM 52811539SChunli.Zhang@Sun.COM if (vn_has_cached_data(vp)) { 52911539SChunli.Zhang@Sun.COM /* 53011539SChunli.Zhang@Sun.COM * For simplicity, we always allocate a full buffer 53111539SChunli.Zhang@Sun.COM * even if we only expect to read a portion of a block. 53211539SChunli.Zhang@Sun.COM */ 53311539SChunli.Zhang@Sun.COM while (--nblk >= 0) { 53411576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, 53511935SMark.Shellenbaum@Sun.COM dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 53611935SMark.Shellenbaum@Sun.COM blksz), 0, blksz); 53711539SChunli.Zhang@Sun.COM } 53811539SChunli.Zhang@Sun.COM } 53911539SChunli.Zhang@Sun.COM } 54011539SChunli.Zhang@Sun.COM 5413638Sbillm while (n > 0) { 5423638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 5433638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 5443638Sbillm 5453638Sbillm if (vn_has_cached_data(vp)) 5463638Sbillm error = mappedread(vp, nbytes, uio); 5473638Sbillm else 5483638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 5497294Sperrin if (error) { 5507294Sperrin /* convert checksum errors into IO errors */ 5517294Sperrin if (error == ECKSUM) 5527294Sperrin error = EIO; 5533638Sbillm break; 5547294Sperrin } 5553638Sbillm 5563638Sbillm n -= nbytes; 557789Sahrens } 558789Sahrens out: 5592237Smaybee zfs_range_unlock(rl); 560789Sahrens 561789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 562789Sahrens ZFS_EXIT(zfsvfs); 563789Sahrens return (error); 564789Sahrens } 565789Sahrens 566789Sahrens /* 567789Sahrens * Write the bytes to a file. 568789Sahrens * 569789Sahrens * IN: vp - vnode of file to be written to. 570789Sahrens * uio - structure supplying write location, range info, 571789Sahrens * and data buffer. 572789Sahrens * ioflag - FAPPEND flag set if in append mode. 573789Sahrens * cr - credentials of caller. 5745331Samw * ct - caller context (NFS/CIFS fem monitor only) 575789Sahrens * 576789Sahrens * OUT: uio - updated offset and range. 577789Sahrens * 578789Sahrens * RETURN: 0 if success 579789Sahrens * error code if failure 580789Sahrens * 581789Sahrens * Timestamps: 582789Sahrens * vp - ctime|mtime updated if byte count > 0 583789Sahrens */ 58411935SMark.Shellenbaum@Sun.COM 585789Sahrens /* ARGSUSED */ 586789Sahrens static int 587789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 588789Sahrens { 589789Sahrens znode_t *zp = VTOZ(vp); 590789Sahrens rlim64_t limit = uio->uio_llimit; 591789Sahrens ssize_t start_resid = uio->uio_resid; 592789Sahrens ssize_t tx_bytes; 593789Sahrens uint64_t end_size; 594789Sahrens dmu_tx_t *tx; 595789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 5965326Sek110237 zilog_t *zilog; 597789Sahrens offset_t woff; 598789Sahrens ssize_t n, nbytes; 5991669Sperrin rl_t *rl; 600789Sahrens int max_blksz = zfsvfs->z_max_blksz; 6011669Sperrin int error; 6029412SAleksandr.Guzovskiy@Sun.COM arc_buf_t *abuf; 60311539SChunli.Zhang@Sun.COM iovec_t *aiov; 60411539SChunli.Zhang@Sun.COM xuio_t *xuio = NULL; 60511539SChunli.Zhang@Sun.COM int i_iov = 0; 60611539SChunli.Zhang@Sun.COM int iovcnt = uio->uio_iovcnt; 60711539SChunli.Zhang@Sun.COM iovec_t *iovp = uio->uio_iov; 60811539SChunli.Zhang@Sun.COM int write_eof; 60911935SMark.Shellenbaum@Sun.COM int count = 0; 61011935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[4]; 61111935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 612789Sahrens 613789Sahrens /* 614789Sahrens * Fasttrack empty write 615789Sahrens */ 6161669Sperrin n = start_resid; 617789Sahrens if (n == 0) 618789Sahrens return (0); 619789Sahrens 6201669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6211669Sperrin limit = MAXOFFSET_T; 6221669Sperrin 6235367Sahrens ZFS_ENTER(zfsvfs); 6245367Sahrens ZFS_VERIFY_ZP(zp); 6256743Smarks 62611935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 62711935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 62811935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_SIZE(zfsvfs), NULL, 62911935SMark.Shellenbaum@Sun.COM &zp->z_size, 8); 63011935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 63111935SMark.Shellenbaum@Sun.COM &zp->z_pflags, 8); 63211935SMark.Shellenbaum@Sun.COM 6336743Smarks /* 6346743Smarks * If immutable or not appending then return EPERM 6356743Smarks */ 63611935SMark.Shellenbaum@Sun.COM if ((zp->z_pflags & (ZFS_IMMUTABLE | ZFS_READONLY)) || 63711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) && !(ioflag & FAPPEND) && 63811935SMark.Shellenbaum@Sun.COM (uio->uio_loffset < zp->z_size))) { 6396743Smarks ZFS_EXIT(zfsvfs); 6406743Smarks return (EPERM); 6416743Smarks } 6426743Smarks 6435326Sek110237 zilog = zfsvfs->z_log; 644789Sahrens 645789Sahrens /* 64611083Swilliam.gorrell@sun.com * Validate file offset 64711083Swilliam.gorrell@sun.com */ 64811935SMark.Shellenbaum@Sun.COM woff = ioflag & FAPPEND ? zp->z_size : uio->uio_loffset; 64911083Swilliam.gorrell@sun.com if (woff < 0) { 65011083Swilliam.gorrell@sun.com ZFS_EXIT(zfsvfs); 65111083Swilliam.gorrell@sun.com return (EINVAL); 65211083Swilliam.gorrell@sun.com } 65311083Swilliam.gorrell@sun.com 65411083Swilliam.gorrell@sun.com /* 65511083Swilliam.gorrell@sun.com * Check for mandatory locks before calling zfs_range_lock() 65611083Swilliam.gorrell@sun.com * in order to prevent a deadlock with locks set via fcntl(). 65711083Swilliam.gorrell@sun.com */ 65811935SMark.Shellenbaum@Sun.COM if (MANDMODE((mode_t)zp->z_mode) && 65911083Swilliam.gorrell@sun.com (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 66011083Swilliam.gorrell@sun.com ZFS_EXIT(zfsvfs); 66111083Swilliam.gorrell@sun.com return (error); 66211083Swilliam.gorrell@sun.com } 66311083Swilliam.gorrell@sun.com 66411083Swilliam.gorrell@sun.com /* 6652237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6661669Sperrin * don't hold up txg. 66711539SChunli.Zhang@Sun.COM * Skip this if uio contains loaned arc_buf. 668789Sahrens */ 66911539SChunli.Zhang@Sun.COM if ((uio->uio_extflg == UIO_XUIO) && 67011539SChunli.Zhang@Sun.COM (((xuio_t *)uio)->xu_type == UIOTYPE_ZEROCOPY)) 67111539SChunli.Zhang@Sun.COM xuio = (xuio_t *)uio; 67211539SChunli.Zhang@Sun.COM else 67311539SChunli.Zhang@Sun.COM uio_prefaultpages(n, uio); 674789Sahrens 675789Sahrens /* 676789Sahrens * If in append mode, set the io offset pointer to eof. 677789Sahrens */ 6781669Sperrin if (ioflag & FAPPEND) { 6791669Sperrin /* 68011083Swilliam.gorrell@sun.com * Obtain an appending range lock to guarantee file append 68111083Swilliam.gorrell@sun.com * semantics. We reset the write offset once we have the lock. 6821669Sperrin */ 6831669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 68411083Swilliam.gorrell@sun.com woff = rl->r_off; 6851669Sperrin if (rl->r_len == UINT64_MAX) { 68611083Swilliam.gorrell@sun.com /* 68711083Swilliam.gorrell@sun.com * We overlocked the file because this write will cause 68811083Swilliam.gorrell@sun.com * the file block size to increase. 68911083Swilliam.gorrell@sun.com * Note that zp_size cannot change with this lock held. 69011083Swilliam.gorrell@sun.com */ 69111935SMark.Shellenbaum@Sun.COM woff = zp->z_size; 6921669Sperrin } 69311083Swilliam.gorrell@sun.com uio->uio_loffset = woff; 694789Sahrens } else { 695789Sahrens /* 69611083Swilliam.gorrell@sun.com * Note that if the file block size will change as a result of 69711083Swilliam.gorrell@sun.com * this write, then this range lock will lock the entire file 69811083Swilliam.gorrell@sun.com * so that we can re-write the block safely. 699789Sahrens */ 7001669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 701789Sahrens } 702789Sahrens 703789Sahrens if (woff >= limit) { 7043638Sbillm zfs_range_unlock(rl); 7053638Sbillm ZFS_EXIT(zfsvfs); 7063638Sbillm return (EFBIG); 707789Sahrens } 708789Sahrens 709789Sahrens if ((woff + n) > limit || woff > (limit - n)) 710789Sahrens n = limit - woff; 711789Sahrens 71211539SChunli.Zhang@Sun.COM /* Will this write extend the file length? */ 71311935SMark.Shellenbaum@Sun.COM write_eof = (woff + n > zp->z_size); 71411935SMark.Shellenbaum@Sun.COM 71511935SMark.Shellenbaum@Sun.COM end_size = MAX(zp->z_size, woff + n); 716789Sahrens 7171669Sperrin /* 7183638Sbillm * Write the file in reasonable size chunks. Each chunk is written 7193638Sbillm * in a separate transaction; this keeps the intent log records small 7203638Sbillm * and allows us to do more fine-grained space accounting. 721789Sahrens */ 722789Sahrens while (n > 0) { 7239412SAleksandr.Guzovskiy@Sun.COM abuf = NULL; 7249412SAleksandr.Guzovskiy@Sun.COM woff = uio->uio_loffset; 7259412SAleksandr.Guzovskiy@Sun.COM again: 72611935SMark.Shellenbaum@Sun.COM if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 72711935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 7289412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7299412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7309396SMatthew.Ahrens@Sun.COM error = EDQUOT; 7319396SMatthew.Ahrens@Sun.COM break; 7329396SMatthew.Ahrens@Sun.COM } 7339412SAleksandr.Guzovskiy@Sun.COM 73411539SChunli.Zhang@Sun.COM if (xuio && abuf == NULL) { 73511539SChunli.Zhang@Sun.COM ASSERT(i_iov < iovcnt); 73611539SChunli.Zhang@Sun.COM aiov = &iovp[i_iov]; 73711539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i_iov); 73811539SChunli.Zhang@Sun.COM dmu_xuio_clear(xuio, i_iov); 73911539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_cp_write, int, i_iov, 74011539SChunli.Zhang@Sun.COM iovec_t *, aiov, arc_buf_t *, abuf); 74111539SChunli.Zhang@Sun.COM ASSERT((aiov->iov_base == abuf->b_data) || 74211539SChunli.Zhang@Sun.COM ((char *)aiov->iov_base - (char *)abuf->b_data + 74311539SChunli.Zhang@Sun.COM aiov->iov_len == arc_buf_size(abuf))); 74411539SChunli.Zhang@Sun.COM i_iov++; 74511539SChunli.Zhang@Sun.COM } else if (abuf == NULL && n >= max_blksz && 74611935SMark.Shellenbaum@Sun.COM woff >= zp->z_size && 7479412SAleksandr.Guzovskiy@Sun.COM P2PHASE(woff, max_blksz) == 0 && 7489412SAleksandr.Guzovskiy@Sun.COM zp->z_blksz == max_blksz) { 74911539SChunli.Zhang@Sun.COM /* 75011539SChunli.Zhang@Sun.COM * This write covers a full block. "Borrow" a buffer 75111539SChunli.Zhang@Sun.COM * from the dmu so that we can fill it before we enter 75211539SChunli.Zhang@Sun.COM * a transaction. This avoids the possibility of 75311539SChunli.Zhang@Sun.COM * holding up the transaction if the data copy hangs 75411539SChunli.Zhang@Sun.COM * up on a pagefault (e.g., from an NFS server mapping). 75511539SChunli.Zhang@Sun.COM */ 7569412SAleksandr.Guzovskiy@Sun.COM size_t cbytes; 7579412SAleksandr.Guzovskiy@Sun.COM 75811935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 75911935SMark.Shellenbaum@Sun.COM max_blksz); 7609412SAleksandr.Guzovskiy@Sun.COM ASSERT(abuf != NULL); 7619412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_buf_size(abuf) == max_blksz); 7629412SAleksandr.Guzovskiy@Sun.COM if (error = uiocopy(abuf->b_data, max_blksz, 7639412SAleksandr.Guzovskiy@Sun.COM UIO_WRITE, uio, &cbytes)) { 7649412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7659412SAleksandr.Guzovskiy@Sun.COM break; 7669412SAleksandr.Guzovskiy@Sun.COM } 7679412SAleksandr.Guzovskiy@Sun.COM ASSERT(cbytes == max_blksz); 7689412SAleksandr.Guzovskiy@Sun.COM } 7699412SAleksandr.Guzovskiy@Sun.COM 7709412SAleksandr.Guzovskiy@Sun.COM /* 7719412SAleksandr.Guzovskiy@Sun.COM * Start a transaction. 7729412SAleksandr.Guzovskiy@Sun.COM */ 773789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 77411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 775789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 77611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 7778227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 778789Sahrens if (error) { 7798227SNeil.Perrin@Sun.COM if (error == ERESTART) { 7802113Sahrens dmu_tx_wait(tx); 7812113Sahrens dmu_tx_abort(tx); 7829412SAleksandr.Guzovskiy@Sun.COM goto again; 783789Sahrens } 7842113Sahrens dmu_tx_abort(tx); 7859412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7869412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7873638Sbillm break; 7883638Sbillm } 7893638Sbillm 7903638Sbillm /* 7913638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 7923638Sbillm * and then reduce the lock range. This will only happen 7933638Sbillm * on the first iteration since zfs_range_reduce() will 7943638Sbillm * shrink down r_len to the appropriate size. 7953638Sbillm */ 7963638Sbillm if (rl->r_len == UINT64_MAX) { 7973638Sbillm uint64_t new_blksz; 7983638Sbillm 7993638Sbillm if (zp->z_blksz > max_blksz) { 8003638Sbillm ASSERT(!ISP2(zp->z_blksz)); 8013638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 8023638Sbillm } else { 8033638Sbillm new_blksz = MIN(end_size, max_blksz); 8043638Sbillm } 8053638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 8063638Sbillm zfs_range_reduce(rl, woff, n); 8073638Sbillm } 8083638Sbillm 8093638Sbillm /* 8103638Sbillm * XXX - should we really limit each write to z_max_blksz? 8113638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 8123638Sbillm */ 8133638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 8143638Sbillm 8159412SAleksandr.Guzovskiy@Sun.COM if (abuf == NULL) { 8169412SAleksandr.Guzovskiy@Sun.COM tx_bytes = uio->uio_resid; 81712123STim.Haley@Sun.COM error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 81812123STim.Haley@Sun.COM uio, nbytes, tx); 8199412SAleksandr.Guzovskiy@Sun.COM tx_bytes -= uio->uio_resid; 8209412SAleksandr.Guzovskiy@Sun.COM } else { 8219412SAleksandr.Guzovskiy@Sun.COM tx_bytes = nbytes; 82211539SChunli.Zhang@Sun.COM ASSERT(xuio == NULL || tx_bytes == aiov->iov_len); 82311539SChunli.Zhang@Sun.COM /* 82411539SChunli.Zhang@Sun.COM * If this is not a full block write, but we are 82511539SChunli.Zhang@Sun.COM * extending the file past EOF and this data starts 82611539SChunli.Zhang@Sun.COM * block-aligned, use assign_arcbuf(). Otherwise, 82711539SChunli.Zhang@Sun.COM * write via dmu_write(). 82811539SChunli.Zhang@Sun.COM */ 82911539SChunli.Zhang@Sun.COM if (tx_bytes < max_blksz && (!write_eof || 83011539SChunli.Zhang@Sun.COM aiov->iov_base != abuf->b_data)) { 83111539SChunli.Zhang@Sun.COM ASSERT(xuio); 83211539SChunli.Zhang@Sun.COM dmu_write(zfsvfs->z_os, zp->z_id, woff, 83311539SChunli.Zhang@Sun.COM aiov->iov_len, aiov->iov_base, tx); 83411539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 83511539SChunli.Zhang@Sun.COM xuio_stat_wbuf_copied(); 83611539SChunli.Zhang@Sun.COM } else { 83711539SChunli.Zhang@Sun.COM ASSERT(xuio || tx_bytes == max_blksz); 83811935SMark.Shellenbaum@Sun.COM dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl), 83911935SMark.Shellenbaum@Sun.COM woff, abuf, tx); 84011539SChunli.Zhang@Sun.COM } 8419412SAleksandr.Guzovskiy@Sun.COM ASSERT(tx_bytes <= uio->uio_resid); 8429412SAleksandr.Guzovskiy@Sun.COM uioskip(uio, tx_bytes); 8439412SAleksandr.Guzovskiy@Sun.COM } 8449412SAleksandr.Guzovskiy@Sun.COM if (tx_bytes && vn_has_cached_data(vp)) { 8458636SMark.Maybee@Sun.COM update_pages(vp, woff, 8468636SMark.Maybee@Sun.COM tx_bytes, zfsvfs->z_os, zp->z_id); 8479412SAleksandr.Guzovskiy@Sun.COM } 8483638Sbillm 8493638Sbillm /* 8503638Sbillm * If we made no progress, we're done. If we made even 8513638Sbillm * partial progress, update the znode and ZIL accordingly. 8523638Sbillm */ 8533638Sbillm if (tx_bytes == 0) { 85411935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 85511935SMark.Shellenbaum@Sun.COM (void *)&zp->z_size, sizeof (uint64_t), tx); 8563897Smaybee dmu_tx_commit(tx); 8573638Sbillm ASSERT(error != 0); 8583638Sbillm break; 8593638Sbillm } 8603638Sbillm 861789Sahrens /* 8623638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 8633638Sbillm * privileged and at least one of the excute bits is set. 8643638Sbillm * 8653638Sbillm * It would be nice to to this after all writes have 8663638Sbillm * been done, but that would still expose the ISUID/ISGID 8673638Sbillm * to another app after the partial write is committed. 8685331Samw * 869789Sahrens */ 8703638Sbillm mutex_enter(&zp->z_acl_lock); 87111935SMark.Shellenbaum@Sun.COM if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | 8723638Sbillm (S_IXUSR >> 6))) != 0 && 87311935SMark.Shellenbaum@Sun.COM (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 8743638Sbillm secpolicy_vnode_setid_retain(cr, 87511935SMark.Shellenbaum@Sun.COM (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) { 87611935SMark.Shellenbaum@Sun.COM uint64_t newmode; 87711935SMark.Shellenbaum@Sun.COM zp->z_mode &= ~(S_ISUID | S_ISGID); 87811935SMark.Shellenbaum@Sun.COM newmode = zp->z_mode; 87911935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 88011935SMark.Shellenbaum@Sun.COM (void *)&newmode, sizeof (uint64_t), tx); 8813638Sbillm } 8823638Sbillm mutex_exit(&zp->z_acl_lock); 8833638Sbillm 88411935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 88511935SMark.Shellenbaum@Sun.COM B_TRUE); 8863638Sbillm 8873638Sbillm /* 8883638Sbillm * Update the file size (zp_size) if it has changed; 8893638Sbillm * account for possible concurrent updates. 8903638Sbillm */ 89111935SMark.Shellenbaum@Sun.COM while ((end_size = zp->z_size) < uio->uio_loffset) { 89211935SMark.Shellenbaum@Sun.COM (void) atomic_cas_64(&zp->z_size, end_size, 893789Sahrens uio->uio_loffset); 89411935SMark.Shellenbaum@Sun.COM ASSERT(error == 0); 89511935SMark.Shellenbaum@Sun.COM } 89611935SMark.Shellenbaum@Sun.COM error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 89711935SMark.Shellenbaum@Sun.COM 8983638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 8993638Sbillm dmu_tx_commit(tx); 9003638Sbillm 9013638Sbillm if (error != 0) 9023638Sbillm break; 9033638Sbillm ASSERT(tx_bytes == nbytes); 9043638Sbillm n -= nbytes; 905789Sahrens } 906789Sahrens 9072237Smaybee zfs_range_unlock(rl); 908789Sahrens 909789Sahrens /* 910789Sahrens * If we're in replay mode, or we made no progress, return error. 911789Sahrens * Otherwise, it's at least a partial write, so it's successful. 912789Sahrens */ 9138227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 914789Sahrens ZFS_EXIT(zfsvfs); 915789Sahrens return (error); 916789Sahrens } 917789Sahrens 91812294SMark.Musante@Sun.COM if (ioflag & (FSYNC | FDSYNC) || 91912294SMark.Musante@Sun.COM zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 920*12699SNeil.Perrin@Sun.COM zil_commit(zilog, zp->z_id); 921789Sahrens 922789Sahrens ZFS_EXIT(zfsvfs); 923789Sahrens return (0); 924789Sahrens } 925789Sahrens 9262237Smaybee void 92710922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error) 9282237Smaybee { 92910922SJeff.Bonwick@Sun.COM znode_t *zp = zgd->zgd_private; 93010922SJeff.Bonwick@Sun.COM objset_t *os = zp->z_zfsvfs->z_os; 93110922SJeff.Bonwick@Sun.COM 93210922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 93310922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 93410922SJeff.Bonwick@Sun.COM 93510922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 93610922SJeff.Bonwick@Sun.COM 9379321SNeil.Perrin@Sun.COM /* 9389321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9399321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9409321SNeil.Perrin@Sun.COM */ 94110922SJeff.Bonwick@Sun.COM VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 94210922SJeff.Bonwick@Sun.COM 94310922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 94410922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 94510922SJeff.Bonwick@Sun.COM 9463063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9472237Smaybee } 9482237Smaybee 94910209SMark.Musante@Sun.COM #ifdef DEBUG 95010209SMark.Musante@Sun.COM static int zil_fault_io = 0; 95110209SMark.Musante@Sun.COM #endif 95210209SMark.Musante@Sun.COM 953789Sahrens /* 954789Sahrens * Get data to generate a TX_WRITE intent log record. 955789Sahrens */ 956789Sahrens int 9572237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 958789Sahrens { 959789Sahrens zfsvfs_t *zfsvfs = arg; 960789Sahrens objset_t *os = zfsvfs->z_os; 961789Sahrens znode_t *zp; 96210922SJeff.Bonwick@Sun.COM uint64_t object = lr->lr_foid; 96310922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 96410922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; 96510922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9662237Smaybee dmu_buf_t *db; 9673063Sperrin zgd_t *zgd; 968789Sahrens int error = 0; 969789Sahrens 97010922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 97110922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 972789Sahrens 973789Sahrens /* 9741669Sperrin * Nothing to do if the file has been removed 975789Sahrens */ 97610922SJeff.Bonwick@Sun.COM if (zfs_zget(zfsvfs, object, &zp) != 0) 977789Sahrens return (ENOENT); 9783461Sahrens if (zp->z_unlinked) { 9799321SNeil.Perrin@Sun.COM /* 9809321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9819321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9829321SNeil.Perrin@Sun.COM */ 9839321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), 9849321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 985789Sahrens return (ENOENT); 986789Sahrens } 987789Sahrens 98810922SJeff.Bonwick@Sun.COM zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 98910922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zfsvfs->z_log; 99010922SJeff.Bonwick@Sun.COM zgd->zgd_private = zp; 99110922SJeff.Bonwick@Sun.COM 992789Sahrens /* 993789Sahrens * Write records come in two flavors: immediate and indirect. 994789Sahrens * For small writes it's cheaper to store the data with the 995789Sahrens * log record (immediate); for large writes it's cheaper to 996789Sahrens * sync the data and get a pointer to it (indirect) so that 997789Sahrens * we don't have to write the data twice. 998789Sahrens */ 9991669Sperrin if (buf != NULL) { /* immediate write */ 100010922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 10011669Sperrin /* test for truncation needs to be done while range locked */ 100211935SMark.Shellenbaum@Sun.COM if (offset >= zp->z_size) { 10031669Sperrin error = ENOENT; 100410922SJeff.Bonwick@Sun.COM } else { 100510922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 100610922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 10071669Sperrin } 100810922SJeff.Bonwick@Sun.COM ASSERT(error == 0 || error == ENOENT); 10091669Sperrin } else { /* indirect write */ 1010789Sahrens /* 10111669Sperrin * Have to lock the whole block to ensure when it's 10121669Sperrin * written out and it's checksum is being calculated 10131669Sperrin * that no one can change the data. We need to re-check 10141669Sperrin * blocksize after we get the lock in case it's changed! 1015789Sahrens */ 10161669Sperrin for (;;) { 101710922SJeff.Bonwick@Sun.COM uint64_t blkoff; 101810922SJeff.Bonwick@Sun.COM size = zp->z_blksz; 101910945SJeff.Bonwick@Sun.COM blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 102010922SJeff.Bonwick@Sun.COM offset -= blkoff; 102110922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, 102210922SJeff.Bonwick@Sun.COM RL_READER); 102310922SJeff.Bonwick@Sun.COM if (zp->z_blksz == size) 10241669Sperrin break; 102510922SJeff.Bonwick@Sun.COM offset += blkoff; 102610922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 10271669Sperrin } 10281669Sperrin /* test for truncation needs to be done while range locked */ 102911935SMark.Shellenbaum@Sun.COM if (lr->lr_offset >= zp->z_size) 10301669Sperrin error = ENOENT; 103110209SMark.Musante@Sun.COM #ifdef DEBUG 103210209SMark.Musante@Sun.COM if (zil_fault_io) { 103310209SMark.Musante@Sun.COM error = EIO; 103410209SMark.Musante@Sun.COM zil_fault_io = 0; 103510209SMark.Musante@Sun.COM } 103610209SMark.Musante@Sun.COM #endif 103710922SJeff.Bonwick@Sun.COM if (error == 0) 103812285SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db, 103912285SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 104010922SJeff.Bonwick@Sun.COM 104110800SNeil.Perrin@Sun.COM if (error == 0) { 104210922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 104310922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 104410922SJeff.Bonwick@Sun.COM 104510922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 104610922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 104710922SJeff.Bonwick@Sun.COM 104810922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 104910922SJeff.Bonwick@Sun.COM zfs_get_done, zgd); 105010922SJeff.Bonwick@Sun.COM ASSERT(error || lr->lr_length <= zp->z_blksz); 105110922SJeff.Bonwick@Sun.COM 105210800SNeil.Perrin@Sun.COM /* 105310922SJeff.Bonwick@Sun.COM * On success, we need to wait for the write I/O 105410922SJeff.Bonwick@Sun.COM * initiated by dmu_sync() to complete before we can 105510922SJeff.Bonwick@Sun.COM * release this dbuf. We will finish everything up 105610922SJeff.Bonwick@Sun.COM * in the zfs_get_done() callback. 105710800SNeil.Perrin@Sun.COM */ 105810922SJeff.Bonwick@Sun.COM if (error == 0) 105910922SJeff.Bonwick@Sun.COM return (0); 106010922SJeff.Bonwick@Sun.COM 106110922SJeff.Bonwick@Sun.COM if (error == EALREADY) { 106210922SJeff.Bonwick@Sun.COM lr->lr_common.lrc_txtype = TX_WRITE2; 106310922SJeff.Bonwick@Sun.COM error = 0; 106410922SJeff.Bonwick@Sun.COM } 106510800SNeil.Perrin@Sun.COM } 1066789Sahrens } 106710922SJeff.Bonwick@Sun.COM 106810922SJeff.Bonwick@Sun.COM zfs_get_done(zgd, error); 106910922SJeff.Bonwick@Sun.COM 1070789Sahrens return (error); 1071789Sahrens } 1072789Sahrens 1073789Sahrens /*ARGSUSED*/ 1074789Sahrens static int 10755331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 10765331Samw caller_context_t *ct) 1077789Sahrens { 1078789Sahrens znode_t *zp = VTOZ(vp); 1079789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1080789Sahrens int error; 1081789Sahrens 10825367Sahrens ZFS_ENTER(zfsvfs); 10835367Sahrens ZFS_VERIFY_ZP(zp); 10845331Samw 10855331Samw if (flag & V_ACE_MASK) 10865331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 10875331Samw else 10885331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 10895331Samw 1090789Sahrens ZFS_EXIT(zfsvfs); 1091789Sahrens return (error); 1092789Sahrens } 1093789Sahrens 1094789Sahrens /* 10959981STim.Haley@Sun.COM * If vnode is for a device return a specfs vnode instead. 10969981STim.Haley@Sun.COM */ 10979981STim.Haley@Sun.COM static int 10989981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr) 10999981STim.Haley@Sun.COM { 11009981STim.Haley@Sun.COM int error = 0; 11019981STim.Haley@Sun.COM 11029981STim.Haley@Sun.COM if (IS_DEVVP(*vpp)) { 11039981STim.Haley@Sun.COM struct vnode *svp; 11049981STim.Haley@Sun.COM 11059981STim.Haley@Sun.COM svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 11069981STim.Haley@Sun.COM VN_RELE(*vpp); 11079981STim.Haley@Sun.COM if (svp == NULL) 11089981STim.Haley@Sun.COM error = ENOSYS; 11099981STim.Haley@Sun.COM *vpp = svp; 11109981STim.Haley@Sun.COM } 11119981STim.Haley@Sun.COM return (error); 11129981STim.Haley@Sun.COM } 11139981STim.Haley@Sun.COM 11149981STim.Haley@Sun.COM 11159981STim.Haley@Sun.COM /* 1116789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1117789Sahrens * If it exists, return a held vnode reference for it. 1118789Sahrens * 1119789Sahrens * IN: dvp - vnode of directory to search. 1120789Sahrens * nm - name of entry to lookup. 1121789Sahrens * pnp - full pathname to lookup [UNUSED]. 1122789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1123789Sahrens * rdir - root directory vnode [UNUSED]. 1124789Sahrens * cr - credentials of caller. 11255331Samw * ct - caller context 11265331Samw * direntflags - directory lookup flags 11275331Samw * realpnp - returned pathname. 1128789Sahrens * 1129789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1130789Sahrens * 1131789Sahrens * RETURN: 0 if success 1132789Sahrens * error code if failure 1133789Sahrens * 1134789Sahrens * Timestamps: 1135789Sahrens * NA 1136789Sahrens */ 1137789Sahrens /* ARGSUSED */ 1138789Sahrens static int 1139789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 11405331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 11415331Samw int *direntflags, pathname_t *realpnp) 1142789Sahrens { 1143789Sahrens znode_t *zdp = VTOZ(dvp); 1144789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 11459981STim.Haley@Sun.COM int error = 0; 11469981STim.Haley@Sun.COM 11479981STim.Haley@Sun.COM /* fast path */ 11489981STim.Haley@Sun.COM if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 11499981STim.Haley@Sun.COM 11509981STim.Haley@Sun.COM if (dvp->v_type != VDIR) { 11519981STim.Haley@Sun.COM return (ENOTDIR); 115211935SMark.Shellenbaum@Sun.COM } else if (zdp->z_sa_hdl == NULL) { 11539981STim.Haley@Sun.COM return (EIO); 11549981STim.Haley@Sun.COM } 11559981STim.Haley@Sun.COM 11569981STim.Haley@Sun.COM if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 11579981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11589981STim.Haley@Sun.COM if (!error) { 11599981STim.Haley@Sun.COM *vpp = dvp; 11609981STim.Haley@Sun.COM VN_HOLD(*vpp); 11619981STim.Haley@Sun.COM return (0); 11629981STim.Haley@Sun.COM } 11639981STim.Haley@Sun.COM return (error); 11649981STim.Haley@Sun.COM } else { 11659981STim.Haley@Sun.COM vnode_t *tvp = dnlc_lookup(dvp, nm); 11669981STim.Haley@Sun.COM 11679981STim.Haley@Sun.COM if (tvp) { 11689981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11699981STim.Haley@Sun.COM if (error) { 11709981STim.Haley@Sun.COM VN_RELE(tvp); 11719981STim.Haley@Sun.COM return (error); 11729981STim.Haley@Sun.COM } 11739981STim.Haley@Sun.COM if (tvp == DNLC_NO_VNODE) { 11749981STim.Haley@Sun.COM VN_RELE(tvp); 11759981STim.Haley@Sun.COM return (ENOENT); 11769981STim.Haley@Sun.COM } else { 11779981STim.Haley@Sun.COM *vpp = tvp; 11789981STim.Haley@Sun.COM return (specvp_check(vpp, cr)); 11799981STim.Haley@Sun.COM } 11809981STim.Haley@Sun.COM } 11819981STim.Haley@Sun.COM } 11829981STim.Haley@Sun.COM } 11839981STim.Haley@Sun.COM 11849981STim.Haley@Sun.COM DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1185789Sahrens 11865367Sahrens ZFS_ENTER(zfsvfs); 11875367Sahrens ZFS_VERIFY_ZP(zdp); 1188789Sahrens 1189789Sahrens *vpp = NULL; 1190789Sahrens 1191789Sahrens if (flags & LOOKUP_XATTR) { 1192789Sahrens /* 11933234Sck153898 * If the xattr property is off, refuse the lookup request. 11943234Sck153898 */ 11953234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 11963234Sck153898 ZFS_EXIT(zfsvfs); 11973234Sck153898 return (EINVAL); 11983234Sck153898 } 11993234Sck153898 12003234Sck153898 /* 1201789Sahrens * We don't allow recursive attributes.. 1202789Sahrens * Maybe someday we will. 1203789Sahrens */ 120411935SMark.Shellenbaum@Sun.COM if (zdp->z_pflags & ZFS_XATTR) { 1205789Sahrens ZFS_EXIT(zfsvfs); 1206789Sahrens return (EINVAL); 1207789Sahrens } 1208789Sahrens 12093280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1210789Sahrens ZFS_EXIT(zfsvfs); 1211789Sahrens return (error); 1212789Sahrens } 1213789Sahrens 1214789Sahrens /* 1215789Sahrens * Do we have permission to get into attribute directory? 1216789Sahrens */ 1217789Sahrens 12185331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 12195331Samw B_FALSE, cr)) { 1220789Sahrens VN_RELE(*vpp); 12215331Samw *vpp = NULL; 1222789Sahrens } 1223789Sahrens 1224789Sahrens ZFS_EXIT(zfsvfs); 1225789Sahrens return (error); 1226789Sahrens } 1227789Sahrens 12281512Sek110237 if (dvp->v_type != VDIR) { 12291512Sek110237 ZFS_EXIT(zfsvfs); 12301460Smarks return (ENOTDIR); 12311512Sek110237 } 12321460Smarks 1233789Sahrens /* 1234789Sahrens * Check accessibility of directory. 1235789Sahrens */ 1236789Sahrens 12375331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1238789Sahrens ZFS_EXIT(zfsvfs); 1239789Sahrens return (error); 1240789Sahrens } 1241789Sahrens 12425498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 12435331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 12445331Samw ZFS_EXIT(zfsvfs); 12455331Samw return (EILSEQ); 12465331Samw } 12475331Samw 12485331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 12499981STim.Haley@Sun.COM if (error == 0) 12509981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1251789Sahrens 1252789Sahrens ZFS_EXIT(zfsvfs); 1253789Sahrens return (error); 1254789Sahrens } 1255789Sahrens 1256789Sahrens /* 1257789Sahrens * Attempt to create a new entry in a directory. If the entry 1258789Sahrens * already exists, truncate the file if permissible, else return 1259789Sahrens * an error. Return the vp of the created or trunc'd file. 1260789Sahrens * 1261789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1262789Sahrens * name - name of new file entry. 1263789Sahrens * vap - attributes of new file. 1264789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1265789Sahrens * mode - mode to open file with. 1266789Sahrens * cr - credentials of caller. 1267789Sahrens * flag - large file flag [UNUSED]. 12685331Samw * ct - caller context 12695331Samw * vsecp - ACL to be set 1270789Sahrens * 1271789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1272789Sahrens * 1273789Sahrens * RETURN: 0 if success 1274789Sahrens * error code if failure 1275789Sahrens * 1276789Sahrens * Timestamps: 1277789Sahrens * dvp - ctime|mtime updated if new entry created 1278789Sahrens * vp - ctime|mtime always, atime if new 1279789Sahrens */ 12805331Samw 1281789Sahrens /* ARGSUSED */ 1282789Sahrens static int 1283789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 12845331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 12855331Samw vsecattr_t *vsecp) 1286789Sahrens { 1287789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1288789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 12895326Sek110237 zilog_t *zilog; 12905326Sek110237 objset_t *os; 1291789Sahrens zfs_dirlock_t *dl; 1292789Sahrens dmu_tx_t *tx; 1293789Sahrens int error; 12947847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 12957847SMark.Shellenbaum@Sun.COM uid_t uid; 12967847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 129711935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 12989179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 129912302SMark.Shellenbaum@Sun.COM boolean_t have_acl = B_FALSE; 13005331Samw 13015331Samw /* 13025331Samw * If we have an ephemeral id, ACL, or XVATTR then 13035331Samw * make sure file system is at proper version 13045331Samw */ 13055331Samw 13067847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 13077847SMark.Shellenbaum@Sun.COM if (ksid) 13087847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 13097847SMark.Shellenbaum@Sun.COM else 13107847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 13117847SMark.Shellenbaum@Sun.COM 13125331Samw if (zfsvfs->z_use_fuids == B_FALSE && 13135331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 13147847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 13155331Samw return (EINVAL); 1316789Sahrens 13175367Sahrens ZFS_ENTER(zfsvfs); 13185367Sahrens ZFS_VERIFY_ZP(dzp); 13195326Sek110237 os = zfsvfs->z_os; 13205326Sek110237 zilog = zfsvfs->z_log; 1321789Sahrens 13225498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 13235331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 13245331Samw ZFS_EXIT(zfsvfs); 13255331Samw return (EILSEQ); 13265331Samw } 13275331Samw 13285331Samw if (vap->va_mask & AT_XVATTR) { 13295331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 13305331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 13315331Samw ZFS_EXIT(zfsvfs); 13325331Samw return (error); 13335331Samw } 13345331Samw } 1335789Sahrens top: 1336789Sahrens *vpp = NULL; 1337789Sahrens 1338789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1339789Sahrens vap->va_mode &= ~VSVTX; 1340789Sahrens 1341789Sahrens if (*name == '\0') { 1342789Sahrens /* 1343789Sahrens * Null component name refers to the directory itself. 1344789Sahrens */ 1345789Sahrens VN_HOLD(dvp); 1346789Sahrens zp = dzp; 1347789Sahrens dl = NULL; 1348789Sahrens error = 0; 1349789Sahrens } else { 1350789Sahrens /* possible VN_HOLD(zp) */ 13515331Samw int zflg = 0; 13525331Samw 13535331Samw if (flag & FIGNORECASE) 13545331Samw zflg |= ZCILOOK; 13555331Samw 13565331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13575331Samw NULL, NULL); 13585331Samw if (error) { 1359789Sahrens if (strcmp(name, "..") == 0) 1360789Sahrens error = EISDIR; 1361789Sahrens ZFS_EXIT(zfsvfs); 1362789Sahrens return (error); 1363789Sahrens } 1364789Sahrens } 136511935SMark.Shellenbaum@Sun.COM 1366789Sahrens if (zp == NULL) { 13675331Samw uint64_t txtype; 13685331Samw 1369789Sahrens /* 1370789Sahrens * Create a new file object and update the directory 1371789Sahrens * to reference it. 1372789Sahrens */ 13735331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1374789Sahrens goto out; 1375789Sahrens } 1376789Sahrens 1377789Sahrens /* 1378789Sahrens * We only support the creation of regular files in 1379789Sahrens * extended attribute directories. 1380789Sahrens */ 138111935SMark.Shellenbaum@Sun.COM 138211935SMark.Shellenbaum@Sun.COM if ((dzp->z_pflags & ZFS_XATTR) && 1383789Sahrens (vap->va_type != VREG)) { 1384789Sahrens error = EINVAL; 1385789Sahrens goto out; 1386789Sahrens } 1387789Sahrens 138812302SMark.Shellenbaum@Sun.COM if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 138912302SMark.Shellenbaum@Sun.COM cr, vsecp, &acl_ids)) != 0) 13909179SMark.Shellenbaum@Sun.COM goto out; 139112302SMark.Shellenbaum@Sun.COM have_acl = B_TRUE; 139212302SMark.Shellenbaum@Sun.COM 13939396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 139410143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 13959396SMatthew.Ahrens@Sun.COM error = EDQUOT; 13969396SMatthew.Ahrens@Sun.COM goto out; 13979396SMatthew.Ahrens@Sun.COM } 13989179SMark.Shellenbaum@Sun.COM 1399789Sahrens tx = dmu_tx_create(os); 140011935SMark.Shellenbaum@Sun.COM 140111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 140211935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 140311935SMark.Shellenbaum@Sun.COM 14049179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 14059396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 14069396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 14071544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 140811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 140911935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && 141011935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1411789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 141211935SMark.Shellenbaum@Sun.COM 0, acl_ids.z_aclp->z_acl_bytes); 14135331Samw } 14148227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1415789Sahrens if (error) { 1416789Sahrens zfs_dirent_unlock(dl); 14178227SNeil.Perrin@Sun.COM if (error == ERESTART) { 14182113Sahrens dmu_tx_wait(tx); 14192113Sahrens dmu_tx_abort(tx); 1420789Sahrens goto top; 1421789Sahrens } 142212302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 14232113Sahrens dmu_tx_abort(tx); 1424789Sahrens ZFS_EXIT(zfsvfs); 1425789Sahrens return (error); 1426789Sahrens } 142711935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 14289179SMark.Shellenbaum@Sun.COM 14299179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 14309179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 14319179SMark.Shellenbaum@Sun.COM 1432789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 14335331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 14345331Samw if (flag & FIGNORECASE) 14355331Samw txtype |= TX_CI; 14365331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 14379179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 14389179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1439789Sahrens dmu_tx_commit(tx); 1440789Sahrens } else { 14415331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 14425331Samw 1443789Sahrens /* 1444789Sahrens * A directory entry already exists for this name. 1445789Sahrens */ 1446789Sahrens /* 1447789Sahrens * Can't truncate an existing file if in exclusive mode. 1448789Sahrens */ 1449789Sahrens if (excl == EXCL) { 1450789Sahrens error = EEXIST; 1451789Sahrens goto out; 1452789Sahrens } 1453789Sahrens /* 1454789Sahrens * Can't open a directory for writing. 1455789Sahrens */ 1456789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1457789Sahrens error = EISDIR; 1458789Sahrens goto out; 1459789Sahrens } 1460789Sahrens /* 1461789Sahrens * Verify requested access to file. 1462789Sahrens */ 14635331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1464789Sahrens goto out; 1465789Sahrens } 1466789Sahrens 1467789Sahrens mutex_enter(&dzp->z_lock); 1468789Sahrens dzp->z_seq++; 1469789Sahrens mutex_exit(&dzp->z_lock); 1470789Sahrens 14711878Smaybee /* 14721878Smaybee * Truncate regular files if requested. 14731878Smaybee */ 14741878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1475789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 14766992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 14776992Smaybee zfs_dirent_unlock(dl); 14786992Smaybee dl = NULL; 14791878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 14804863Spraks if (error == 0) { 14815331Samw vnevent_create(ZTOV(zp), ct); 14824863Spraks } 1483789Sahrens } 1484789Sahrens } 1485789Sahrens out: 1486789Sahrens 1487789Sahrens if (dl) 1488789Sahrens zfs_dirent_unlock(dl); 1489789Sahrens 1490789Sahrens if (error) { 1491789Sahrens if (zp) 1492789Sahrens VN_RELE(ZTOV(zp)); 1493789Sahrens } else { 1494789Sahrens *vpp = ZTOV(zp); 14959981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1496789Sahrens } 1497789Sahrens 149812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1499*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 150012294SMark.Musante@Sun.COM 1501789Sahrens ZFS_EXIT(zfsvfs); 1502789Sahrens return (error); 1503789Sahrens } 1504789Sahrens 1505789Sahrens /* 1506789Sahrens * Remove an entry from a directory. 1507789Sahrens * 1508789Sahrens * IN: dvp - vnode of directory to remove entry from. 1509789Sahrens * name - name of entry to remove. 1510789Sahrens * cr - credentials of caller. 15115331Samw * ct - caller context 15125331Samw * flags - case flags 1513789Sahrens * 1514789Sahrens * RETURN: 0 if success 1515789Sahrens * error code if failure 1516789Sahrens * 1517789Sahrens * Timestamps: 1518789Sahrens * dvp - ctime|mtime 1519789Sahrens * vp - ctime (if nlink > 0) 1520789Sahrens */ 152111935SMark.Shellenbaum@Sun.COM 152211935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0; 152311935SMark.Shellenbaum@Sun.COM 15245331Samw /*ARGSUSED*/ 1525789Sahrens static int 15265331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 15275331Samw int flags) 1528789Sahrens { 1529789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1530789Sahrens znode_t *xzp = NULL; 1531789Sahrens vnode_t *vp; 1532789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15335326Sek110237 zilog_t *zilog; 153411935SMark.Shellenbaum@Sun.COM uint64_t acl_obj, xattr_obj = 0; 153511935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj_unlinked = 0; 1536789Sahrens zfs_dirlock_t *dl; 1537789Sahrens dmu_tx_t *tx; 15383461Sahrens boolean_t may_delete_now, delete_now = FALSE; 15396992Smaybee boolean_t unlinked, toobig = FALSE; 15405331Samw uint64_t txtype; 15415331Samw pathname_t *realnmp = NULL; 15425331Samw pathname_t realnm; 1543789Sahrens int error; 15445331Samw int zflg = ZEXISTS; 1545789Sahrens 15465367Sahrens ZFS_ENTER(zfsvfs); 15475367Sahrens ZFS_VERIFY_ZP(dzp); 15485326Sek110237 zilog = zfsvfs->z_log; 1549789Sahrens 15505331Samw if (flags & FIGNORECASE) { 15515331Samw zflg |= ZCILOOK; 15525331Samw pn_alloc(&realnm); 15535331Samw realnmp = &realnm; 15545331Samw } 15555331Samw 1556789Sahrens top: 1557789Sahrens /* 1558789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1559789Sahrens */ 15605331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 15615331Samw NULL, realnmp)) { 15625331Samw if (realnmp) 15635331Samw pn_free(realnmp); 1564789Sahrens ZFS_EXIT(zfsvfs); 1565789Sahrens return (error); 1566789Sahrens } 1567789Sahrens 1568789Sahrens vp = ZTOV(zp); 1569789Sahrens 1570789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1571789Sahrens goto out; 1572789Sahrens } 1573789Sahrens 1574789Sahrens /* 1575789Sahrens * Need to use rmdir for removing directories. 1576789Sahrens */ 1577789Sahrens if (vp->v_type == VDIR) { 1578789Sahrens error = EPERM; 1579789Sahrens goto out; 1580789Sahrens } 1581789Sahrens 15825331Samw vnevent_remove(vp, dvp, name, ct); 15835331Samw 15845331Samw if (realnmp) 15856492Stimh dnlc_remove(dvp, realnmp->pn_buf); 15865331Samw else 15875331Samw dnlc_remove(dvp, name); 15881484Sek110237 1589789Sahrens mutex_enter(&vp->v_lock); 1590789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1591789Sahrens mutex_exit(&vp->v_lock); 1592789Sahrens 1593789Sahrens /* 15943461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1595789Sahrens * it depends on whether we're the last link, and on whether there are 1596789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1597789Sahrens * allow for either case. 1598789Sahrens */ 1599789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16001544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 160111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 160211935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 160311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 16046992Smaybee if (may_delete_now) { 16056992Smaybee toobig = 160611935SMark.Shellenbaum@Sun.COM zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 16076992Smaybee /* if the file is too big, only hold_free a token amount */ 16086992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 16096992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 16106992Smaybee } 1611789Sahrens 1612789Sahrens /* are there any extended attributes? */ 161311935SMark.Shellenbaum@Sun.COM error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 161411935SMark.Shellenbaum@Sun.COM &xattr_obj, sizeof (xattr_obj)); 161511935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 161611935SMark.Shellenbaum@Sun.COM error = zfs_zget(zfsvfs, xattr_obj, &xzp); 161711935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 161811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 161911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1620789Sahrens } 1621789Sahrens 162212620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 162312620SMark.Shellenbaum@Oracle.COM if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1624789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 162512620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 1626789Sahrens 1627789Sahrens /* charge as an update -- would be nice not to charge at all */ 16283461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1629789Sahrens 16308227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1631789Sahrens if (error) { 1632789Sahrens zfs_dirent_unlock(dl); 1633789Sahrens VN_RELE(vp); 16348227SNeil.Perrin@Sun.COM if (error == ERESTART) { 16352113Sahrens dmu_tx_wait(tx); 16362113Sahrens dmu_tx_abort(tx); 1637789Sahrens goto top; 1638789Sahrens } 16395331Samw if (realnmp) 16405331Samw pn_free(realnmp); 16412113Sahrens dmu_tx_abort(tx); 1642789Sahrens ZFS_EXIT(zfsvfs); 1643789Sahrens return (error); 1644789Sahrens } 1645789Sahrens 1646789Sahrens /* 1647789Sahrens * Remove the directory entry. 1648789Sahrens */ 16495331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1650789Sahrens 1651789Sahrens if (error) { 1652789Sahrens dmu_tx_commit(tx); 1653789Sahrens goto out; 1654789Sahrens } 1655789Sahrens 16563461Sahrens if (unlinked) { 165711935SMark.Shellenbaum@Sun.COM 165812620SMark.Shellenbaum@Oracle.COM /* 165912620SMark.Shellenbaum@Oracle.COM * Hold z_lock so that we can make sure that the ACL obj 166012620SMark.Shellenbaum@Oracle.COM * hasn't changed. Could have been deleted due to 166112620SMark.Shellenbaum@Oracle.COM * zfs_sa_upgrade(). 166212620SMark.Shellenbaum@Oracle.COM */ 166312620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 1664789Sahrens mutex_enter(&vp->v_lock); 166511935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 166611935SMark.Shellenbaum@Sun.COM &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 16676992Smaybee delete_now = may_delete_now && !toobig && 1668789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 166912620SMark.Shellenbaum@Oracle.COM xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 167011935SMark.Shellenbaum@Sun.COM acl_obj; 1671789Sahrens mutex_exit(&vp->v_lock); 1672789Sahrens } 1673789Sahrens 1674789Sahrens if (delete_now) { 167511935SMark.Shellenbaum@Sun.COM if (xattr_obj_unlinked) { 167611935SMark.Shellenbaum@Sun.COM ASSERT3U(xzp->z_links, ==, 2); 1677789Sahrens mutex_enter(&xzp->z_lock); 16783461Sahrens xzp->z_unlinked = 1; 167911935SMark.Shellenbaum@Sun.COM xzp->z_links = 0; 168011935SMark.Shellenbaum@Sun.COM error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 168111935SMark.Shellenbaum@Sun.COM &xzp->z_links, sizeof (xzp->z_links), tx); 168211935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1683789Sahrens mutex_exit(&xzp->z_lock); 16843461Sahrens zfs_unlinked_add(xzp, tx); 168512620SMark.Shellenbaum@Oracle.COM 168611935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 168711935SMark.Shellenbaum@Sun.COM error = sa_remove(zp->z_sa_hdl, 168811935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), tx); 168911935SMark.Shellenbaum@Sun.COM else 169011935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, 169111935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), &null_xattr, 169211935SMark.Shellenbaum@Sun.COM sizeof (uint64_t), tx); 169311935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1694789Sahrens } 1695789Sahrens mutex_enter(&vp->v_lock); 1696789Sahrens vp->v_count--; 1697789Sahrens ASSERT3U(vp->v_count, ==, 0); 1698789Sahrens mutex_exit(&vp->v_lock); 1699789Sahrens mutex_exit(&zp->z_lock); 1700789Sahrens zfs_znode_delete(zp, tx); 17013461Sahrens } else if (unlinked) { 170212620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 17033461Sahrens zfs_unlinked_add(zp, tx); 1704789Sahrens } 1705789Sahrens 17065331Samw txtype = TX_REMOVE; 17075331Samw if (flags & FIGNORECASE) 17085331Samw txtype |= TX_CI; 1709*12699SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, zp->z_id); 1710789Sahrens 1711789Sahrens dmu_tx_commit(tx); 1712789Sahrens out: 17135331Samw if (realnmp) 17145331Samw pn_free(realnmp); 17155331Samw 1716789Sahrens zfs_dirent_unlock(dl); 1717789Sahrens 171812178SMark.Shellenbaum@Sun.COM if (!delete_now) 1719789Sahrens VN_RELE(vp); 172012178SMark.Shellenbaum@Sun.COM if (xzp) 1721789Sahrens VN_RELE(ZTOV(xzp)); 1722789Sahrens 172312294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1724*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 172512294SMark.Musante@Sun.COM 1726789Sahrens ZFS_EXIT(zfsvfs); 1727789Sahrens return (error); 1728789Sahrens } 1729789Sahrens 1730789Sahrens /* 1731789Sahrens * Create a new directory and insert it into dvp using the name 1732789Sahrens * provided. Return a pointer to the inserted directory. 1733789Sahrens * 1734789Sahrens * IN: dvp - vnode of directory to add subdir to. 1735789Sahrens * dirname - name of new directory. 1736789Sahrens * vap - attributes of new directory. 1737789Sahrens * cr - credentials of caller. 17385331Samw * ct - caller context 17395331Samw * vsecp - ACL to be set 1740789Sahrens * 1741789Sahrens * OUT: vpp - vnode of created directory. 1742789Sahrens * 1743789Sahrens * RETURN: 0 if success 1744789Sahrens * error code if failure 1745789Sahrens * 1746789Sahrens * Timestamps: 1747789Sahrens * dvp - ctime|mtime updated 1748789Sahrens * vp - ctime|mtime|atime updated 1749789Sahrens */ 17505331Samw /*ARGSUSED*/ 1751789Sahrens static int 17525331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 17535331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1754789Sahrens { 1755789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1756789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17575326Sek110237 zilog_t *zilog; 1758789Sahrens zfs_dirlock_t *dl; 17595331Samw uint64_t txtype; 1760789Sahrens dmu_tx_t *tx; 1761789Sahrens int error; 17625331Samw int zf = ZNEW; 17637847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 17647847SMark.Shellenbaum@Sun.COM uid_t uid; 17657847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 176611935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 17679179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1768789Sahrens 1769789Sahrens ASSERT(vap->va_type == VDIR); 1770789Sahrens 17715331Samw /* 17725331Samw * If we have an ephemeral id, ACL, or XVATTR then 17735331Samw * make sure file system is at proper version 17745331Samw */ 17755331Samw 17767847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 17777847SMark.Shellenbaum@Sun.COM if (ksid) 17787847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 17797847SMark.Shellenbaum@Sun.COM else 17807847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 17815331Samw if (zfsvfs->z_use_fuids == B_FALSE && 17827847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 17837876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 17845331Samw return (EINVAL); 17855331Samw 17865367Sahrens ZFS_ENTER(zfsvfs); 17875367Sahrens ZFS_VERIFY_ZP(dzp); 17885326Sek110237 zilog = zfsvfs->z_log; 1789789Sahrens 179011935SMark.Shellenbaum@Sun.COM if (dzp->z_pflags & ZFS_XATTR) { 1791789Sahrens ZFS_EXIT(zfsvfs); 1792789Sahrens return (EINVAL); 1793789Sahrens } 17945331Samw 17955498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 17965331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 17975331Samw ZFS_EXIT(zfsvfs); 17985331Samw return (EILSEQ); 17995331Samw } 18005331Samw if (flags & FIGNORECASE) 18015331Samw zf |= ZCILOOK; 18025331Samw 180312302SMark.Shellenbaum@Sun.COM if (vap->va_mask & AT_XVATTR) { 18045331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 18055331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 18065331Samw ZFS_EXIT(zfsvfs); 18075331Samw return (error); 18085331Samw } 180912302SMark.Shellenbaum@Sun.COM } 181012302SMark.Shellenbaum@Sun.COM 181112302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 181212302SMark.Shellenbaum@Sun.COM vsecp, &acl_ids)) != 0) { 181312302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 181412302SMark.Shellenbaum@Sun.COM return (error); 181512302SMark.Shellenbaum@Sun.COM } 1816789Sahrens /* 1817789Sahrens * First make sure the new directory doesn't exist. 181812302SMark.Shellenbaum@Sun.COM * 181912302SMark.Shellenbaum@Sun.COM * Existence is checked first to make sure we don't return 182012302SMark.Shellenbaum@Sun.COM * EACCES instead of EEXIST which can cause some applications 182112302SMark.Shellenbaum@Sun.COM * to fail. 1822789Sahrens */ 18235331Samw top: 18245331Samw *vpp = NULL; 18255331Samw 18265331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 18275331Samw NULL, NULL)) { 182812302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1829789Sahrens ZFS_EXIT(zfsvfs); 1830789Sahrens return (error); 1831789Sahrens } 1832789Sahrens 18335331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 183412302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18351231Smarks zfs_dirent_unlock(dl); 18361231Smarks ZFS_EXIT(zfsvfs); 18371231Smarks return (error); 18381231Smarks } 18391231Smarks 18409396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 184110143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 18429396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 18439396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 18449396SMatthew.Ahrens@Sun.COM return (EDQUOT); 18459396SMatthew.Ahrens@Sun.COM } 18469179SMark.Shellenbaum@Sun.COM 1847789Sahrens /* 1848789Sahrens * Add a new entry to the directory. 1849789Sahrens */ 1850789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 18511544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 18521544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 18539179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 18549396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 18559396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 185611935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 185711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 185811935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 185911935SMark.Shellenbaum@Sun.COM } 186011935SMark.Shellenbaum@Sun.COM 186111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 186211935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 186311935SMark.Shellenbaum@Sun.COM 18648227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1865789Sahrens if (error) { 1866789Sahrens zfs_dirent_unlock(dl); 18678227SNeil.Perrin@Sun.COM if (error == ERESTART) { 18682113Sahrens dmu_tx_wait(tx); 18692113Sahrens dmu_tx_abort(tx); 1870789Sahrens goto top; 1871789Sahrens } 187212302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18732113Sahrens dmu_tx_abort(tx); 1874789Sahrens ZFS_EXIT(zfsvfs); 1875789Sahrens return (error); 1876789Sahrens } 1877789Sahrens 1878789Sahrens /* 1879789Sahrens * Create new node. 1880789Sahrens */ 188111935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 18829179SMark.Shellenbaum@Sun.COM 18839179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 18849179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 188511935SMark.Shellenbaum@Sun.COM 1886789Sahrens /* 1887789Sahrens * Now put new name in parent dir. 1888789Sahrens */ 1889789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1890789Sahrens 1891789Sahrens *vpp = ZTOV(zp); 1892789Sahrens 18935331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 18945331Samw if (flags & FIGNORECASE) 18955331Samw txtype |= TX_CI; 18969179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 18979179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 18989179SMark.Shellenbaum@Sun.COM 18999179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 190011935SMark.Shellenbaum@Sun.COM 1901789Sahrens dmu_tx_commit(tx); 1902789Sahrens 1903789Sahrens zfs_dirent_unlock(dl); 1904789Sahrens 190512294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 1906*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 190712294SMark.Musante@Sun.COM 1908789Sahrens ZFS_EXIT(zfsvfs); 1909789Sahrens return (0); 1910789Sahrens } 1911789Sahrens 1912789Sahrens /* 1913789Sahrens * Remove a directory subdir entry. If the current working 1914789Sahrens * directory is the same as the subdir to be removed, the 1915789Sahrens * remove will fail. 1916789Sahrens * 1917789Sahrens * IN: dvp - vnode of directory to remove from. 1918789Sahrens * name - name of directory to be removed. 1919789Sahrens * cwd - vnode of current working directory. 1920789Sahrens * cr - credentials of caller. 19215331Samw * ct - caller context 19225331Samw * flags - case flags 1923789Sahrens * 1924789Sahrens * RETURN: 0 if success 1925789Sahrens * error code if failure 1926789Sahrens * 1927789Sahrens * Timestamps: 1928789Sahrens * dvp - ctime|mtime updated 1929789Sahrens */ 19305331Samw /*ARGSUSED*/ 1931789Sahrens static int 19325331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 19335331Samw caller_context_t *ct, int flags) 1934789Sahrens { 1935789Sahrens znode_t *dzp = VTOZ(dvp); 1936789Sahrens znode_t *zp; 1937789Sahrens vnode_t *vp; 1938789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 19395326Sek110237 zilog_t *zilog; 1940789Sahrens zfs_dirlock_t *dl; 1941789Sahrens dmu_tx_t *tx; 1942789Sahrens int error; 19435331Samw int zflg = ZEXISTS; 1944789Sahrens 19455367Sahrens ZFS_ENTER(zfsvfs); 19465367Sahrens ZFS_VERIFY_ZP(dzp); 19475326Sek110237 zilog = zfsvfs->z_log; 1948789Sahrens 19495331Samw if (flags & FIGNORECASE) 19505331Samw zflg |= ZCILOOK; 1951789Sahrens top: 1952789Sahrens zp = NULL; 1953789Sahrens 1954789Sahrens /* 1955789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1956789Sahrens */ 19575331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 19585331Samw NULL, NULL)) { 1959789Sahrens ZFS_EXIT(zfsvfs); 1960789Sahrens return (error); 1961789Sahrens } 1962789Sahrens 1963789Sahrens vp = ZTOV(zp); 1964789Sahrens 1965789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1966789Sahrens goto out; 1967789Sahrens } 1968789Sahrens 1969789Sahrens if (vp->v_type != VDIR) { 1970789Sahrens error = ENOTDIR; 1971789Sahrens goto out; 1972789Sahrens } 1973789Sahrens 1974789Sahrens if (vp == cwd) { 1975789Sahrens error = EINVAL; 1976789Sahrens goto out; 1977789Sahrens } 1978789Sahrens 19795331Samw vnevent_rmdir(vp, dvp, name, ct); 1980789Sahrens 1981789Sahrens /* 19823897Smaybee * Grab a lock on the directory to make sure that noone is 19833897Smaybee * trying to add (or lookup) entries while we are removing it. 19843897Smaybee */ 19853897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 19863897Smaybee 19873897Smaybee /* 19883897Smaybee * Grab a lock on the parent pointer to make sure we play well 1989789Sahrens * with the treewalk and directory rename code. 1990789Sahrens */ 1991789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1992789Sahrens 1993789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 19941544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 199511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 19963461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 199711935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 199811935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 19998227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 2000789Sahrens if (error) { 2001789Sahrens rw_exit(&zp->z_parent_lock); 20023897Smaybee rw_exit(&zp->z_name_lock); 2003789Sahrens zfs_dirent_unlock(dl); 2004789Sahrens VN_RELE(vp); 20058227SNeil.Perrin@Sun.COM if (error == ERESTART) { 20062113Sahrens dmu_tx_wait(tx); 20072113Sahrens dmu_tx_abort(tx); 2008789Sahrens goto top; 2009789Sahrens } 20102113Sahrens dmu_tx_abort(tx); 2011789Sahrens ZFS_EXIT(zfsvfs); 2012789Sahrens return (error); 2013789Sahrens } 2014789Sahrens 20155331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 20165331Samw 20175331Samw if (error == 0) { 20185331Samw uint64_t txtype = TX_RMDIR; 20195331Samw if (flags & FIGNORECASE) 20205331Samw txtype |= TX_CI; 2021*12699SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 20225331Samw } 2023789Sahrens 2024789Sahrens dmu_tx_commit(tx); 2025789Sahrens 2026789Sahrens rw_exit(&zp->z_parent_lock); 20273897Smaybee rw_exit(&zp->z_name_lock); 2028789Sahrens out: 2029789Sahrens zfs_dirent_unlock(dl); 2030789Sahrens 2031789Sahrens VN_RELE(vp); 2032789Sahrens 203312294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 2034*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 203512294SMark.Musante@Sun.COM 2036789Sahrens ZFS_EXIT(zfsvfs); 2037789Sahrens return (error); 2038789Sahrens } 2039789Sahrens 2040789Sahrens /* 2041789Sahrens * Read as many directory entries as will fit into the provided 2042789Sahrens * buffer from the given directory cursor position (specified in 2043789Sahrens * the uio structure. 2044789Sahrens * 2045789Sahrens * IN: vp - vnode of directory to read. 2046789Sahrens * uio - structure supplying read location, range info, 2047789Sahrens * and return buffer. 2048789Sahrens * cr - credentials of caller. 20495331Samw * ct - caller context 20505331Samw * flags - case flags 2051789Sahrens * 2052789Sahrens * OUT: uio - updated offset and range, buffer filled. 2053789Sahrens * eofp - set to true if end-of-file detected. 2054789Sahrens * 2055789Sahrens * RETURN: 0 if success 2056789Sahrens * error code if failure 2057789Sahrens * 2058789Sahrens * Timestamps: 2059789Sahrens * vp - atime updated 2060789Sahrens * 2061789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 2062789Sahrens * This allows us to use the low range for "special" directory entries: 2063789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2064789Sahrens * we use the offset 2 for the '.zfs' directory. 2065789Sahrens */ 2066789Sahrens /* ARGSUSED */ 2067789Sahrens static int 20685331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 20695331Samw caller_context_t *ct, int flags) 2070789Sahrens { 2071789Sahrens znode_t *zp = VTOZ(vp); 2072789Sahrens iovec_t *iovp; 20735331Samw edirent_t *eodp; 2074789Sahrens dirent64_t *odp; 2075789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2076869Sperrin objset_t *os; 2077789Sahrens caddr_t outbuf; 2078789Sahrens size_t bufsize; 2079789Sahrens zap_cursor_t zc; 2080789Sahrens zap_attribute_t zap; 2081789Sahrens uint_t bytes_wanted; 2082789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 208311935SMark.Shellenbaum@Sun.COM uint64_t parent; 2084789Sahrens int local_eof; 2085869Sperrin int outcount; 2086869Sperrin int error; 2087869Sperrin uint8_t prefetch; 20885663Sck153898 boolean_t check_sysattrs; 2089789Sahrens 20905367Sahrens ZFS_ENTER(zfsvfs); 20915367Sahrens ZFS_VERIFY_ZP(zp); 2092789Sahrens 209311935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 209411935SMark.Shellenbaum@Sun.COM &parent, sizeof (parent))) != 0) { 209511935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 209611935SMark.Shellenbaum@Sun.COM return (error); 209711935SMark.Shellenbaum@Sun.COM } 209811935SMark.Shellenbaum@Sun.COM 2099789Sahrens /* 2100789Sahrens * If we are not given an eof variable, 2101789Sahrens * use a local one. 2102789Sahrens */ 2103789Sahrens if (eofp == NULL) 2104789Sahrens eofp = &local_eof; 2105789Sahrens 2106789Sahrens /* 2107789Sahrens * Check for valid iov_len. 2108789Sahrens */ 2109789Sahrens if (uio->uio_iov->iov_len <= 0) { 2110789Sahrens ZFS_EXIT(zfsvfs); 2111789Sahrens return (EINVAL); 2112789Sahrens } 2113789Sahrens 2114789Sahrens /* 2115789Sahrens * Quit if directory has been removed (posix) 2116789Sahrens */ 21173461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 2118789Sahrens ZFS_EXIT(zfsvfs); 2119789Sahrens return (0); 2120789Sahrens } 2121789Sahrens 2122869Sperrin error = 0; 2123869Sperrin os = zfsvfs->z_os; 2124869Sperrin offset = uio->uio_loffset; 2125869Sperrin prefetch = zp->z_zn_prefetch; 2126869Sperrin 2127789Sahrens /* 2128789Sahrens * Initialize the iterator cursor. 2129789Sahrens */ 2130789Sahrens if (offset <= 3) { 2131789Sahrens /* 2132789Sahrens * Start iteration from the beginning of the directory. 2133789Sahrens */ 2134869Sperrin zap_cursor_init(&zc, os, zp->z_id); 2135789Sahrens } else { 2136789Sahrens /* 2137789Sahrens * The offset is a serialized cursor. 2138789Sahrens */ 2139869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2140789Sahrens } 2141789Sahrens 2142789Sahrens /* 2143789Sahrens * Get space to change directory entries into fs independent format. 2144789Sahrens */ 2145789Sahrens iovp = uio->uio_iov; 2146789Sahrens bytes_wanted = iovp->iov_len; 2147789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2148789Sahrens bufsize = bytes_wanted; 2149789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 2150789Sahrens odp = (struct dirent64 *)outbuf; 2151789Sahrens } else { 2152789Sahrens bufsize = bytes_wanted; 2153789Sahrens odp = (struct dirent64 *)iovp->iov_base; 2154789Sahrens } 21555331Samw eodp = (struct edirent *)odp; 2156789Sahrens 2157789Sahrens /* 21587757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 21597757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 21607757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 21617757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 21625663Sck153898 */ 21637757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 21645663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 21655663Sck153898 (flags & V_RDDIR_ENTFLAGS); 21665663Sck153898 21675663Sck153898 /* 2168789Sahrens * Transform to file-system independent format 2169789Sahrens */ 2170789Sahrens outcount = 0; 2171789Sahrens while (outcount < bytes_wanted) { 21723912Slling ino64_t objnum; 21733912Slling ushort_t reclen; 21743912Slling off64_t *next; 21753912Slling 2176789Sahrens /* 2177789Sahrens * Special case `.', `..', and `.zfs'. 2178789Sahrens */ 2179789Sahrens if (offset == 0) { 2180789Sahrens (void) strcpy(zap.za_name, "."); 21815331Samw zap.za_normalization_conflict = 0; 21823912Slling objnum = zp->z_id; 2183789Sahrens } else if (offset == 1) { 2184789Sahrens (void) strcpy(zap.za_name, ".."); 21855331Samw zap.za_normalization_conflict = 0; 218611935SMark.Shellenbaum@Sun.COM objnum = parent; 2187789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2188789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 21895331Samw zap.za_normalization_conflict = 0; 21903912Slling objnum = ZFSCTL_INO_ROOT; 2191789Sahrens } else { 2192789Sahrens /* 2193789Sahrens * Grab next entry. 2194789Sahrens */ 2195789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2196789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2197789Sahrens break; 2198789Sahrens else 2199789Sahrens goto update; 2200789Sahrens } 2201789Sahrens 2202789Sahrens if (zap.za_integer_length != 8 || 2203789Sahrens zap.za_num_integers != 1) { 2204789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2205789Sahrens "entry, obj = %lld, offset = %lld\n", 2206789Sahrens (u_longlong_t)zp->z_id, 2207789Sahrens (u_longlong_t)offset); 2208789Sahrens error = ENXIO; 2209789Sahrens goto update; 2210789Sahrens } 22113912Slling 22123912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 22133912Slling /* 22143912Slling * MacOS X can extract the object type here such as: 22153912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 22163912Slling */ 22175663Sck153898 22185663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 22195663Sck153898 zap.za_normalization_conflict = 22205663Sck153898 xattr_sysattr_casechk(zap.za_name); 22215663Sck153898 } 2222789Sahrens } 22235331Samw 22249749STim.Haley@Sun.COM if (flags & V_RDDIR_ACCFILTER) { 22259749STim.Haley@Sun.COM /* 22269749STim.Haley@Sun.COM * If we have no access at all, don't include 22279749STim.Haley@Sun.COM * this entry in the returned information 22289749STim.Haley@Sun.COM */ 22299749STim.Haley@Sun.COM znode_t *ezp; 22309749STim.Haley@Sun.COM if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 22319749STim.Haley@Sun.COM goto skip_entry; 22329749STim.Haley@Sun.COM if (!zfs_has_access(ezp, cr)) { 22339749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22349749STim.Haley@Sun.COM goto skip_entry; 22359749STim.Haley@Sun.COM } 22369749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22379749STim.Haley@Sun.COM } 22389749STim.Haley@Sun.COM 22395331Samw if (flags & V_RDDIR_ENTFLAGS) 22405331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 22415331Samw else 22425331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2243789Sahrens 2244789Sahrens /* 2245789Sahrens * Will this entry fit in the buffer? 2246789Sahrens */ 22473912Slling if (outcount + reclen > bufsize) { 2248789Sahrens /* 2249789Sahrens * Did we manage to fit anything in the buffer? 2250789Sahrens */ 2251789Sahrens if (!outcount) { 2252789Sahrens error = EINVAL; 2253789Sahrens goto update; 2254789Sahrens } 2255789Sahrens break; 2256789Sahrens } 22575331Samw if (flags & V_RDDIR_ENTFLAGS) { 22585331Samw /* 22595331Samw * Add extended flag entry: 22605331Samw */ 22615331Samw eodp->ed_ino = objnum; 22625331Samw eodp->ed_reclen = reclen; 22635331Samw /* NOTE: ed_off is the offset for the *next* entry */ 22645331Samw next = &(eodp->ed_off); 22655331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 22665331Samw ED_CASE_CONFLICT : 0; 22675331Samw (void) strncpy(eodp->ed_name, zap.za_name, 22685331Samw EDIRENT_NAMELEN(reclen)); 22695331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 22705331Samw } else { 22715331Samw /* 22725331Samw * Add normal entry: 22735331Samw */ 22745331Samw odp->d_ino = objnum; 22755331Samw odp->d_reclen = reclen; 22765331Samw /* NOTE: d_off is the offset for the *next* entry */ 22775331Samw next = &(odp->d_off); 22785331Samw (void) strncpy(odp->d_name, zap.za_name, 22795331Samw DIRENT64_NAMELEN(reclen)); 22805331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 22815331Samw } 22823912Slling outcount += reclen; 2283789Sahrens 2284789Sahrens ASSERT(outcount <= bufsize); 2285789Sahrens 2286789Sahrens /* Prefetch znode */ 2287869Sperrin if (prefetch) 22883912Slling dmu_prefetch(os, objnum, 0, 0); 2289789Sahrens 22909749STim.Haley@Sun.COM skip_entry: 2291789Sahrens /* 2292789Sahrens * Move to the next entry, fill in the previous offset. 2293789Sahrens */ 2294789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2295789Sahrens zap_cursor_advance(&zc); 2296789Sahrens offset = zap_cursor_serialize(&zc); 2297789Sahrens } else { 2298789Sahrens offset += 1; 2299789Sahrens } 2300789Sahrens *next = offset; 2301789Sahrens } 2302869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2303789Sahrens 2304789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2305789Sahrens iovp->iov_base += outcount; 2306789Sahrens iovp->iov_len -= outcount; 2307789Sahrens uio->uio_resid -= outcount; 2308789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2309789Sahrens /* 2310789Sahrens * Reset the pointer. 2311789Sahrens */ 2312789Sahrens offset = uio->uio_loffset; 2313789Sahrens } 2314789Sahrens 2315789Sahrens update: 2316885Sahrens zap_cursor_fini(&zc); 2317789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2318789Sahrens kmem_free(outbuf, bufsize); 2319789Sahrens 2320789Sahrens if (error == ENOENT) 2321789Sahrens error = 0; 2322789Sahrens 2323789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2324789Sahrens 2325789Sahrens uio->uio_loffset = offset; 2326789Sahrens ZFS_EXIT(zfsvfs); 2327789Sahrens return (error); 2328789Sahrens } 2329789Sahrens 23304720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 23314720Sfr157268 2332789Sahrens static int 23335331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2334789Sahrens { 2335789Sahrens znode_t *zp = VTOZ(vp); 2336789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2337789Sahrens 23381773Seschrock /* 23391773Seschrock * Regardless of whether this is required for standards conformance, 23401773Seschrock * this is the logical behavior when fsync() is called on a file with 23411773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 23421773Seschrock * going to be pushed out as part of the zil_commit(). 23431773Seschrock */ 23441773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 23451773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 23465331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 23471773Seschrock 23484720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 23494720Sfr157268 235012294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 235112294SMark.Musante@Sun.COM ZFS_ENTER(zfsvfs); 235212294SMark.Musante@Sun.COM ZFS_VERIFY_ZP(zp); 2353*12699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 235412294SMark.Musante@Sun.COM ZFS_EXIT(zfsvfs); 235512294SMark.Musante@Sun.COM } 2356789Sahrens return (0); 2357789Sahrens } 2358789Sahrens 23595331Samw 2360789Sahrens /* 2361789Sahrens * Get the requested file attributes and place them in the provided 2362789Sahrens * vattr structure. 2363789Sahrens * 2364789Sahrens * IN: vp - vnode of file. 2365789Sahrens * vap - va_mask identifies requested attributes. 23665331Samw * If AT_XVATTR set, then optional attrs are requested 23675331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2368789Sahrens * cr - credentials of caller. 23695331Samw * ct - caller context 2370789Sahrens * 2371789Sahrens * OUT: vap - attribute values. 2372789Sahrens * 2373789Sahrens * RETURN: 0 (always succeeds) 2374789Sahrens */ 2375789Sahrens /* ARGSUSED */ 2376789Sahrens static int 23775331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 23785331Samw caller_context_t *ct) 2379789Sahrens { 2380789Sahrens znode_t *zp = VTOZ(vp); 2381789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23825331Samw int error = 0; 23834543Smarks uint64_t links; 238411935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 23855331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23865331Samw xoptattr_t *xoap = NULL; 23875331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 238811935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[2]; 238911935SMark.Shellenbaum@Sun.COM int count = 0; 2390789Sahrens 23915367Sahrens ZFS_ENTER(zfsvfs); 23925367Sahrens ZFS_VERIFY_ZP(zp); 239311935SMark.Shellenbaum@Sun.COM 239411935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 239511935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 239611935SMark.Shellenbaum@Sun.COM 239711935SMark.Shellenbaum@Sun.COM if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 239811935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 239911935SMark.Shellenbaum@Sun.COM return (error); 240011935SMark.Shellenbaum@Sun.COM } 2401789Sahrens 24025331Samw /* 24035331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 24045331Samw * Also, if we are the owner don't bother, since owner should 24055331Samw * always be allowed to read basic attributes of file. 24065331Samw */ 240711935SMark.Shellenbaum@Sun.COM if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) { 24085331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 24095331Samw skipaclchk, cr)) { 24105331Samw ZFS_EXIT(zfsvfs); 24115331Samw return (error); 24125331Samw } 24135331Samw } 24145331Samw 2415789Sahrens /* 2416789Sahrens * Return all attributes. It's cheaper to provide the answer 2417789Sahrens * than to determine whether we were asked the question. 2418789Sahrens */ 2419789Sahrens 24209774SRay.Hassan@Sun.COM mutex_enter(&zp->z_lock); 2421789Sahrens vap->va_type = vp->v_type; 242211935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode & MODEMASK; 242311935SMark.Shellenbaum@Sun.COM vap->va_uid = zp->z_uid; 242411935SMark.Shellenbaum@Sun.COM vap->va_gid = zp->z_gid; 2425789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2426789Sahrens vap->va_nodeid = zp->z_id; 24274543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 242811935SMark.Shellenbaum@Sun.COM links = zp->z_links + 1; 24294543Smarks else 243011935SMark.Shellenbaum@Sun.COM links = zp->z_links; 24314543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 243211935SMark.Shellenbaum@Sun.COM vap->va_size = zp->z_size; 24331816Smarks vap->va_rdev = vp->v_rdev; 2434789Sahrens vap->va_seq = zp->z_seq; 2435789Sahrens 24365331Samw /* 24375331Samw * Add in any requested optional attributes and the create time. 24385331Samw * Also set the corresponding bits in the returned attribute bitmap. 24395331Samw */ 24405331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 24415331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 24425331Samw xoap->xoa_archive = 244311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_ARCHIVE) != 0); 24445331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 24455331Samw } 24465331Samw 24475331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 24485331Samw xoap->xoa_readonly = 244911935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_READONLY) != 0); 24505331Samw XVA_SET_RTN(xvap, XAT_READONLY); 24515331Samw } 24525331Samw 24535331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 24545331Samw xoap->xoa_system = 245511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_SYSTEM) != 0); 24565331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 24575331Samw } 24585331Samw 24595331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 24605331Samw xoap->xoa_hidden = 246111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_HIDDEN) != 0); 24625331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 24635331Samw } 24645331Samw 24655331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 24665331Samw xoap->xoa_nounlink = 246711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0); 24685331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 24695331Samw } 24705331Samw 24715331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 24725331Samw xoap->xoa_immutable = 247311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 24745331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 24755331Samw } 24765331Samw 24775331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 24785331Samw xoap->xoa_appendonly = 247911935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0); 24805331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 24815331Samw } 24825331Samw 24835331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 24845331Samw xoap->xoa_nodump = 248511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0); 24865331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 24875331Samw } 24885331Samw 24895331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 24905331Samw xoap->xoa_opaque = 249111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_OPAQUE) != 0); 24925331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 24935331Samw } 24945331Samw 24955331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 24965331Samw xoap->xoa_av_quarantined = 249711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 24985331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 24995331Samw } 25005331Samw 25015331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 25025331Samw xoap->xoa_av_modified = 250311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 25045331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 25055331Samw } 25065331Samw 25075331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 250811935SMark.Shellenbaum@Sun.COM vp->v_type == VREG) { 250911935SMark.Shellenbaum@Sun.COM zfs_sa_get_scanstamp(zp, xvap); 25105331Samw } 25115331Samw 25125331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 251311935SMark.Shellenbaum@Sun.COM uint64_t times[2]; 251411935SMark.Shellenbaum@Sun.COM 251511935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 251611935SMark.Shellenbaum@Sun.COM times, sizeof (times)); 251711935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 25185331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 25195331Samw } 252010793Sdai.ngo@sun.com 252110793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 252211935SMark.Shellenbaum@Sun.COM xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 252310793Sdai.ngo@sun.com XVA_SET_RTN(xvap, XAT_REPARSE); 252410793Sdai.ngo@sun.com } 25255331Samw } 25265331Samw 252711935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 252811935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_mtime, mtime); 252911935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2530789Sahrens 2531789Sahrens mutex_exit(&zp->z_lock); 2532789Sahrens 253311935SMark.Shellenbaum@Sun.COM sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); 2534789Sahrens 2535789Sahrens if (zp->z_blksz == 0) { 2536789Sahrens /* 2537789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2538789Sahrens */ 2539789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2540789Sahrens } 2541789Sahrens 2542789Sahrens ZFS_EXIT(zfsvfs); 2543789Sahrens return (0); 2544789Sahrens } 2545789Sahrens 2546789Sahrens /* 2547789Sahrens * Set the file attributes to the values contained in the 2548789Sahrens * vattr structure. 2549789Sahrens * 2550789Sahrens * IN: vp - vnode of file to be modified. 2551789Sahrens * vap - new attribute values. 25525331Samw * If AT_XVATTR set, then optional attrs are being set 2553789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 25545331Samw * - ATTR_NOACLCHECK (CIFS context only). 2555789Sahrens * cr - credentials of caller. 25565331Samw * ct - caller context 2557789Sahrens * 2558789Sahrens * RETURN: 0 if success 2559789Sahrens * error code if failure 2560789Sahrens * 2561789Sahrens * Timestamps: 2562789Sahrens * vp - ctime updated, mtime updated if size changed. 2563789Sahrens */ 2564789Sahrens /* ARGSUSED */ 2565789Sahrens static int 2566789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2567789Sahrens caller_context_t *ct) 2568789Sahrens { 25695326Sek110237 znode_t *zp = VTOZ(vp); 2570789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 25715326Sek110237 zilog_t *zilog; 2572789Sahrens dmu_tx_t *tx; 25731878Smaybee vattr_t oldva; 25748190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2575789Sahrens uint_t mask = vap->va_mask; 25761878Smaybee uint_t saved_mask; 25772796Smarks int trim_mask = 0; 2578789Sahrens uint64_t new_mode; 25799179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 258011935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj = 0; 258111935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 25821231Smarks znode_t *attrzp; 2583789Sahrens int need_policy = FALSE; 258411935SMark.Shellenbaum@Sun.COM int err, err2; 25855331Samw zfs_fuid_info_t *fuidp = NULL; 25865331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 25875331Samw xoptattr_t *xoap; 25885824Smarks zfs_acl_t *aclp = NULL; 25895331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 259011935SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 259111935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[7], xattr_bulk[7]; 259211935SMark.Shellenbaum@Sun.COM int count = 0, xattr_count = 0; 2593789Sahrens 2594789Sahrens if (mask == 0) 2595789Sahrens return (0); 2596789Sahrens 2597789Sahrens if (mask & AT_NOSET) 2598789Sahrens return (EINVAL); 2599789Sahrens 26005367Sahrens ZFS_ENTER(zfsvfs); 26015367Sahrens ZFS_VERIFY_ZP(zp); 26025331Samw 26035331Samw zilog = zfsvfs->z_log; 26045331Samw 26055331Samw /* 26065331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 26075331Samw * that file system is at proper version level 26085331Samw */ 26095331Samw 26105331Samw if (zfsvfs->z_use_fuids == B_FALSE && 26115331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26125331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 26135386Stimh (mask & AT_XVATTR))) { 26145386Stimh ZFS_EXIT(zfsvfs); 26155331Samw return (EINVAL); 26165386Stimh } 26175386Stimh 26185386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 26195386Stimh ZFS_EXIT(zfsvfs); 2620789Sahrens return (EISDIR); 26215386Stimh } 26225386Stimh 26235386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 26245386Stimh ZFS_EXIT(zfsvfs); 26251308Smarks return (EINVAL); 26265386Stimh } 26271308Smarks 26285331Samw /* 26295331Samw * If this is an xvattr_t, then get a pointer to the structure of 26305331Samw * optional attributes. If this is NULL, then we have a vattr_t. 26315331Samw */ 26325331Samw xoap = xva_getxoptattr(xvap); 26335331Samw 26348190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 26358190SMark.Shellenbaum@Sun.COM 26365331Samw /* 26375331Samw * Immutable files can only alter immutable bit and atime 26385331Samw */ 263911935SMark.Shellenbaum@Sun.COM if ((zp->z_pflags & ZFS_IMMUTABLE) && 26405331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 26415386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 26425386Stimh ZFS_EXIT(zfsvfs); 26435331Samw return (EPERM); 26445386Stimh } 26455386Stimh 264611935SMark.Shellenbaum@Sun.COM if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 26475386Stimh ZFS_EXIT(zfsvfs); 26485331Samw return (EPERM); 26495386Stimh } 2650789Sahrens 26516064Smarks /* 26526064Smarks * Verify timestamps doesn't overflow 32 bits. 26536064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 26546064Smarks * handle times greater than 2039. This check should be removed 26556064Smarks * once large timestamps are fully supported. 26566064Smarks */ 26576064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 26586064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 26596064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 26606064Smarks ZFS_EXIT(zfsvfs); 26616064Smarks return (EOVERFLOW); 26626064Smarks } 26636064Smarks } 26646064Smarks 2665789Sahrens top: 26661231Smarks attrzp = NULL; 2667789Sahrens 26689981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2669789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2670789Sahrens ZFS_EXIT(zfsvfs); 2671789Sahrens return (EROFS); 2672789Sahrens } 2673789Sahrens 2674789Sahrens /* 2675789Sahrens * First validate permissions 2676789Sahrens */ 2677789Sahrens 2678789Sahrens if (mask & AT_SIZE) { 26795331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2680789Sahrens if (err) { 2681789Sahrens ZFS_EXIT(zfsvfs); 2682789Sahrens return (err); 2683789Sahrens } 26841878Smaybee /* 26851878Smaybee * XXX - Note, we are not providing any open 26861878Smaybee * mode flags here (like FNDELAY), so we may 26871878Smaybee * block if there are locks present... this 26881878Smaybee * should be addressed in openat(). 26891878Smaybee */ 26906992Smaybee /* XXX - would it be OK to generate a log record here? */ 26916992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 26921878Smaybee if (err) { 26931878Smaybee ZFS_EXIT(zfsvfs); 26941878Smaybee return (err); 26951878Smaybee } 2696789Sahrens } 2697789Sahrens 26985331Samw if (mask & (AT_ATIME|AT_MTIME) || 26995331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 27005331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 27015331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 27025331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 270311935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 27045331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 27055331Samw skipaclchk, cr); 270611935SMark.Shellenbaum@Sun.COM } 2707789Sahrens 2708789Sahrens if (mask & (AT_UID|AT_GID)) { 2709789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2710789Sahrens int take_owner; 2711789Sahrens int take_group; 2712789Sahrens 2713789Sahrens /* 2714913Smarks * NOTE: even if a new mode is being set, 2715913Smarks * we may clear S_ISUID/S_ISGID bits. 2716913Smarks */ 2717913Smarks 2718913Smarks if (!(mask & AT_MODE)) 271911935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode; 2720913Smarks 2721913Smarks /* 2722789Sahrens * Take ownership or chgrp to group we are a member of 2723789Sahrens */ 2724789Sahrens 2725789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 27265331Samw take_group = (mask & AT_GID) && 27275331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2728789Sahrens 2729789Sahrens /* 2730789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2731789Sahrens * take_group must both be set in order to allow taking 2732789Sahrens * ownership. 2733789Sahrens * 2734789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2735789Sahrens * 2736789Sahrens */ 2737789Sahrens 2738789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2739789Sahrens ((idmask == AT_UID) && take_owner) || 2740789Sahrens ((idmask == AT_GID) && take_group)) { 27415331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 27425331Samw skipaclchk, cr) == 0) { 2743789Sahrens /* 2744789Sahrens * Remove setuid/setgid for non-privileged users 2745789Sahrens */ 27461115Smarks secpolicy_setid_clear(vap, cr); 27472796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2748789Sahrens } else { 2749789Sahrens need_policy = TRUE; 2750789Sahrens } 2751789Sahrens } else { 2752789Sahrens need_policy = TRUE; 2753789Sahrens } 2754789Sahrens } 2755789Sahrens 27562796Smarks mutex_enter(&zp->z_lock); 275711935SMark.Shellenbaum@Sun.COM oldva.va_mode = zp->z_mode; 275811935SMark.Shellenbaum@Sun.COM oldva.va_uid = zp->z_uid; 275911935SMark.Shellenbaum@Sun.COM oldva.va_gid = zp->z_gid; 27605331Samw if (mask & AT_XVATTR) { 27618190SMark.Shellenbaum@Sun.COM /* 27628190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 27638190SMark.Shellenbaum@Sun.COM * that are actually changing. 27648190SMark.Shellenbaum@Sun.COM * 27658190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 27668190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 27678190SMark.Shellenbaum@Sun.COM */ 27688190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 27698190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 277011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 27718190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27728190SMark.Shellenbaum@Sun.COM } else { 27738190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 27748190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 27758190SMark.Shellenbaum@Sun.COM } 27768190SMark.Shellenbaum@Sun.COM } 27778190SMark.Shellenbaum@Sun.COM 27788190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 27798190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 278011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 27818190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27828190SMark.Shellenbaum@Sun.COM } else { 27838190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 27848190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 27858190SMark.Shellenbaum@Sun.COM } 27868190SMark.Shellenbaum@Sun.COM } 27878190SMark.Shellenbaum@Sun.COM 27888190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 27898190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 279011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 27918190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27928190SMark.Shellenbaum@Sun.COM } else { 27938190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 27948190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 27958190SMark.Shellenbaum@Sun.COM } 27968190SMark.Shellenbaum@Sun.COM } 27978190SMark.Shellenbaum@Sun.COM 27988190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 27998190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 280011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0)) { 28018190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28028190SMark.Shellenbaum@Sun.COM } else { 28038190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 28048190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 28058190SMark.Shellenbaum@Sun.COM } 28068190SMark.Shellenbaum@Sun.COM } 28078190SMark.Shellenbaum@Sun.COM 28088190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 28098190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 281011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 28118190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28128190SMark.Shellenbaum@Sun.COM } else { 28138190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 28148190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 28158190SMark.Shellenbaum@Sun.COM } 28168190SMark.Shellenbaum@Sun.COM } 28178190SMark.Shellenbaum@Sun.COM 28188190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 28198190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 28208190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 28218190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 282211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 28238190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28248190SMark.Shellenbaum@Sun.COM } else { 28258190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 28268190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 28278190SMark.Shellenbaum@Sun.COM } 28288190SMark.Shellenbaum@Sun.COM } 28298190SMark.Shellenbaum@Sun.COM 283010793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 283110793Sdai.ngo@sun.com mutex_exit(&zp->z_lock); 283210793Sdai.ngo@sun.com ZFS_EXIT(zfsvfs); 283310793Sdai.ngo@sun.com return (EPERM); 283410793Sdai.ngo@sun.com } 283510793Sdai.ngo@sun.com 28368190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 28378190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 28388190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 28395331Samw need_policy = TRUE; 28405331Samw } 28415331Samw } 28425331Samw 28432796Smarks mutex_exit(&zp->z_lock); 28442796Smarks 28452796Smarks if (mask & AT_MODE) { 28465331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 28472796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 28482796Smarks &oldva, cr); 28492796Smarks if (err) { 28502796Smarks ZFS_EXIT(zfsvfs); 28512796Smarks return (err); 28522796Smarks } 28532796Smarks trim_mask |= AT_MODE; 28542796Smarks } else { 28552796Smarks need_policy = TRUE; 28562796Smarks } 28572796Smarks } 2858789Sahrens 2859789Sahrens if (need_policy) { 28601115Smarks /* 28611115Smarks * If trim_mask is set then take ownership 28622796Smarks * has been granted or write_acl is present and user 28632796Smarks * has the ability to modify mode. In that case remove 28642796Smarks * UID|GID and or MODE from mask so that 28651115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 28661115Smarks */ 28672796Smarks 28682796Smarks if (trim_mask) { 28692796Smarks saved_mask = vap->va_mask; 28702796Smarks vap->va_mask &= ~trim_mask; 28712796Smarks } 2872789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 28735331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2874789Sahrens if (err) { 2875789Sahrens ZFS_EXIT(zfsvfs); 2876789Sahrens return (err); 2877789Sahrens } 28781115Smarks 28791115Smarks if (trim_mask) 28802796Smarks vap->va_mask |= saved_mask; 2881789Sahrens } 2882789Sahrens 2883789Sahrens /* 2884789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2885789Sahrens * changed va_mask 2886789Sahrens */ 2887789Sahrens mask = vap->va_mask; 2888789Sahrens 288911935SMark.Shellenbaum@Sun.COM if ((mask & (AT_UID | AT_GID))) { 289011935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj, 289111935SMark.Shellenbaum@Sun.COM sizeof (xattr_obj)); 289211935SMark.Shellenbaum@Sun.COM 289311935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 289411935SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 28959396SMatthew.Ahrens@Sun.COM if (err) 289611935SMark.Shellenbaum@Sun.COM goto out2; 28979179SMark.Shellenbaum@Sun.COM } 28989179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 28999179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 29009179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 290111935SMark.Shellenbaum@Sun.COM if (vap->va_uid != zp->z_uid && 290211935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 29039396SMatthew.Ahrens@Sun.COM err = EDQUOT; 290411935SMark.Shellenbaum@Sun.COM goto out2; 29059396SMatthew.Ahrens@Sun.COM } 29061231Smarks } 29079396SMatthew.Ahrens@Sun.COM 29089179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 29099179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 29109179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 291111935SMark.Shellenbaum@Sun.COM if (new_gid != zp->z_gid && 291211935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 29139396SMatthew.Ahrens@Sun.COM err = EDQUOT; 291411935SMark.Shellenbaum@Sun.COM goto out2; 29159179SMark.Shellenbaum@Sun.COM } 29169179SMark.Shellenbaum@Sun.COM } 29171231Smarks } 291811935SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 291911935SMark.Shellenbaum@Sun.COM 292011935SMark.Shellenbaum@Sun.COM if (mask & AT_MODE) { 292111935SMark.Shellenbaum@Sun.COM uint64_t pmode = zp->z_mode; 292212620SMark.Shellenbaum@Oracle.COM uint64_t acl_obj; 292311935SMark.Shellenbaum@Sun.COM new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 292411935SMark.Shellenbaum@Sun.COM 292511935SMark.Shellenbaum@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 292611935SMark.Shellenbaum@Sun.COM goto out; 292711935SMark.Shellenbaum@Sun.COM 292812620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 292912620SMark.Shellenbaum@Oracle.COM if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 293011935SMark.Shellenbaum@Sun.COM /* 293111935SMark.Shellenbaum@Sun.COM * Are we upgrading ACL from old V0 format 293211935SMark.Shellenbaum@Sun.COM * to V1 format? 293311935SMark.Shellenbaum@Sun.COM */ 293411935SMark.Shellenbaum@Sun.COM if (zfsvfs->z_version <= ZPL_VERSION_FUID && 293512620SMark.Shellenbaum@Oracle.COM zfs_znode_acl_version(zp) == 293611935SMark.Shellenbaum@Sun.COM ZFS_ACL_VERSION_INITIAL) { 293712620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_free(tx, acl_obj, 0, 293811935SMark.Shellenbaum@Sun.COM DMU_OBJECT_END); 293911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 294011935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 294111935SMark.Shellenbaum@Sun.COM } else { 294212620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_write(tx, acl_obj, 0, 294311935SMark.Shellenbaum@Sun.COM aclp->z_acl_bytes); 294411935SMark.Shellenbaum@Sun.COM } 294511935SMark.Shellenbaum@Sun.COM } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 294611935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 294711935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 294811935SMark.Shellenbaum@Sun.COM } 294912620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 295011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 295111935SMark.Shellenbaum@Sun.COM } else { 295211935SMark.Shellenbaum@Sun.COM if ((mask & AT_XVATTR) && 295311935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 295411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 295511935SMark.Shellenbaum@Sun.COM else 295611935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 295711935SMark.Shellenbaum@Sun.COM } 295811935SMark.Shellenbaum@Sun.COM 295911935SMark.Shellenbaum@Sun.COM if (attrzp) { 296011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 296111935SMark.Shellenbaum@Sun.COM } 296211935SMark.Shellenbaum@Sun.COM 296311935SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 296411935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 296511935SMark.Shellenbaum@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 296611935SMark.Shellenbaum@Sun.COM 296711935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 29681231Smarks 29698227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2970789Sahrens if (err) { 29719396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 29722113Sahrens dmu_tx_wait(tx); 29739396SMatthew.Ahrens@Sun.COM goto out; 2974789Sahrens } 2975789Sahrens 297611935SMark.Shellenbaum@Sun.COM count = 0; 2977789Sahrens /* 2978789Sahrens * Set each attribute requested. 2979789Sahrens * We group settings according to the locks they need to acquire. 2980789Sahrens * 2981789Sahrens * Note: you cannot set ctime directly, although it will be 2982789Sahrens * updated as a side-effect of calling this function. 2983789Sahrens */ 2984789Sahrens 298512620SMark.Shellenbaum@Oracle.COM 298612620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 298712620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_acl_lock); 2988789Sahrens mutex_enter(&zp->z_lock); 2989789Sahrens 299012394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 299112394SMark.Shellenbaum@Sun.COM &zp->z_pflags, sizeof (zp->z_pflags)); 299212394SMark.Shellenbaum@Sun.COM 299312394SMark.Shellenbaum@Sun.COM if (attrzp) { 299412620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 299512620SMark.Shellenbaum@Oracle.COM mutex_enter(&attrzp->z_acl_lock); 299611935SMark.Shellenbaum@Sun.COM mutex_enter(&attrzp->z_lock); 299712394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 299812394SMark.Shellenbaum@Sun.COM SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 299912394SMark.Shellenbaum@Sun.COM sizeof (attrzp->z_pflags)); 300012394SMark.Shellenbaum@Sun.COM } 300111935SMark.Shellenbaum@Sun.COM 300212164SMark.Shellenbaum@Sun.COM if (mask & (AT_UID|AT_GID)) { 300312164SMark.Shellenbaum@Sun.COM 300412164SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 300512164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 300612164SMark.Shellenbaum@Sun.COM &new_uid, sizeof (new_uid)); 300712164SMark.Shellenbaum@Sun.COM zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid, 300812164SMark.Shellenbaum@Sun.COM cr, ZFS_OWNER); 300912164SMark.Shellenbaum@Sun.COM if (attrzp) { 301012164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 301112164SMark.Shellenbaum@Sun.COM SA_ZPL_UID(zfsvfs), NULL, &new_uid, 301212164SMark.Shellenbaum@Sun.COM sizeof (new_uid)); 301312394SMark.Shellenbaum@Sun.COM attrzp->z_uid = zp->z_uid; 301412164SMark.Shellenbaum@Sun.COM } 301511935SMark.Shellenbaum@Sun.COM } 301612164SMark.Shellenbaum@Sun.COM 301712164SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 301812164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 301912164SMark.Shellenbaum@Sun.COM NULL, &new_gid, sizeof (new_gid)); 302012164SMark.Shellenbaum@Sun.COM zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr, 302112164SMark.Shellenbaum@Sun.COM ZFS_GROUP); 302212164SMark.Shellenbaum@Sun.COM if (attrzp) { 302312164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 302412164SMark.Shellenbaum@Sun.COM SA_ZPL_GID(zfsvfs), NULL, &new_gid, 302512164SMark.Shellenbaum@Sun.COM sizeof (new_gid)); 302612164SMark.Shellenbaum@Sun.COM attrzp->z_gid = zp->z_gid; 302712164SMark.Shellenbaum@Sun.COM } 302812164SMark.Shellenbaum@Sun.COM } 302912164SMark.Shellenbaum@Sun.COM if (!(mask & AT_MODE)) { 303012164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 303112164SMark.Shellenbaum@Sun.COM NULL, &new_mode, sizeof (new_mode)); 303212164SMark.Shellenbaum@Sun.COM new_mode = zp->z_mode; 303312164SMark.Shellenbaum@Sun.COM } 303412164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(zp); 303512164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 303611935SMark.Shellenbaum@Sun.COM if (attrzp) { 303712164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(attrzp); 303812164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 303911935SMark.Shellenbaum@Sun.COM } 304011935SMark.Shellenbaum@Sun.COM } 304111935SMark.Shellenbaum@Sun.COM 3042789Sahrens if (mask & AT_MODE) { 304311935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 304411935SMark.Shellenbaum@Sun.COM &new_mode, sizeof (new_mode)); 304511935SMark.Shellenbaum@Sun.COM zp->z_mode = new_mode; 304612164SMark.Shellenbaum@Sun.COM ASSERT3U((uintptr_t)aclp, !=, NULL); 30479179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 3048789Sahrens ASSERT3U(err, ==, 0); 304910143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 305010143STim.Haley@Sun.COM aclp = NULL; 3051789Sahrens } 3052789Sahrens 305311935SMark.Shellenbaum@Sun.COM 305411935SMark.Shellenbaum@Sun.COM if (mask & AT_ATIME) { 305511935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 305611935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 305711935SMark.Shellenbaum@Sun.COM &zp->z_atime, sizeof (zp->z_atime)); 30581231Smarks } 30591231Smarks 306011935SMark.Shellenbaum@Sun.COM if (mask & AT_MTIME) { 306111935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 306211935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 306311935SMark.Shellenbaum@Sun.COM mtime, sizeof (mtime)); 30641231Smarks } 30651231Smarks 30666992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 306711935SMark.Shellenbaum@Sun.COM if (mask & AT_SIZE && !(mask & AT_MTIME)) { 306812394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 306912394SMark.Shellenbaum@Sun.COM NULL, mtime, sizeof (mtime)); 307011935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 307111935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 307211935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 307311935SMark.Shellenbaum@Sun.COM B_TRUE); 307411935SMark.Shellenbaum@Sun.COM } else if (mask != 0) { 307511935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 307611935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 307711935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 307811935SMark.Shellenbaum@Sun.COM B_TRUE); 307911935SMark.Shellenbaum@Sun.COM if (attrzp) { 308011935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 308111935SMark.Shellenbaum@Sun.COM SA_ZPL_CTIME(zfsvfs), NULL, 308211935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 308311935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 308411935SMark.Shellenbaum@Sun.COM mtime, ctime, B_TRUE); 308511935SMark.Shellenbaum@Sun.COM } 308611935SMark.Shellenbaum@Sun.COM } 30875331Samw /* 30885331Samw * Do this after setting timestamps to prevent timestamp 30895331Samw * update from toggling bit 30905331Samw */ 30915331Samw 30925331Samw if (xoap && (mask & AT_XVATTR)) { 30938190SMark.Shellenbaum@Sun.COM 30948190SMark.Shellenbaum@Sun.COM /* 30958190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 30968190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 30978190SMark.Shellenbaum@Sun.COM */ 30988190SMark.Shellenbaum@Sun.COM 30998190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 31008190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 31018190SMark.Shellenbaum@Sun.COM } 31028190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 31038190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 31048190SMark.Shellenbaum@Sun.COM } 31058190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 31068190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 31078190SMark.Shellenbaum@Sun.COM } 31088190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 31098190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 31108190SMark.Shellenbaum@Sun.COM } 31118190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 31128190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 31138190SMark.Shellenbaum@Sun.COM } 31148190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 31158190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 31168190SMark.Shellenbaum@Sun.COM } 31178190SMark.Shellenbaum@Sun.COM 311811935SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 31195331Samw ASSERT(vp->v_type == VREG); 31205331Samw 312111935SMark.Shellenbaum@Sun.COM zfs_xvattr_set(zp, xvap, tx); 31225331Samw } 3123789Sahrens 31249179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 31259179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 31269179SMark.Shellenbaum@Sun.COM 31271878Smaybee if (mask != 0) 31285331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 31295331Samw 3130789Sahrens mutex_exit(&zp->z_lock); 313112620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 313212620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_acl_lock); 313312620SMark.Shellenbaum@Oracle.COM 313412620SMark.Shellenbaum@Oracle.COM if (attrzp) { 313512620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 313612620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_acl_lock); 313712620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_lock); 313812620SMark.Shellenbaum@Oracle.COM } 31399396SMatthew.Ahrens@Sun.COM out: 314011935SMark.Shellenbaum@Sun.COM if (err == 0 && attrzp) { 314111935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 314211935SMark.Shellenbaum@Sun.COM xattr_count, tx); 314311935SMark.Shellenbaum@Sun.COM ASSERT(err2 == 0); 314411935SMark.Shellenbaum@Sun.COM } 314511935SMark.Shellenbaum@Sun.COM 31461231Smarks if (attrzp) 31471231Smarks VN_RELE(ZTOV(attrzp)); 314810143STim.Haley@Sun.COM if (aclp) 314910143STim.Haley@Sun.COM zfs_acl_free(aclp); 315010143STim.Haley@Sun.COM 31519396SMatthew.Ahrens@Sun.COM if (fuidp) { 31529396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 31539396SMatthew.Ahrens@Sun.COM fuidp = NULL; 31549396SMatthew.Ahrens@Sun.COM } 31559396SMatthew.Ahrens@Sun.COM 315611935SMark.Shellenbaum@Sun.COM if (err) { 31579396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 315811935SMark.Shellenbaum@Sun.COM if (err == ERESTART) 315911935SMark.Shellenbaum@Sun.COM goto top; 316011935SMark.Shellenbaum@Sun.COM } else { 316111935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 31629396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 316311935SMark.Shellenbaum@Sun.COM } 316411935SMark.Shellenbaum@Sun.COM 316511935SMark.Shellenbaum@Sun.COM 316611935SMark.Shellenbaum@Sun.COM out2: 316712294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3168*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 316912294SMark.Musante@Sun.COM 3170789Sahrens ZFS_EXIT(zfsvfs); 3171789Sahrens return (err); 3172789Sahrens } 3173789Sahrens 31743271Smaybee typedef struct zfs_zlock { 31753271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 31763271Smaybee znode_t *zl_znode; /* znode we held */ 31773271Smaybee struct zfs_zlock *zl_next; /* next in list */ 31783271Smaybee } zfs_zlock_t; 31793271Smaybee 31803271Smaybee /* 31813271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 31823271Smaybee */ 31833271Smaybee static void 31843271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 31853271Smaybee { 31863271Smaybee zfs_zlock_t *zl; 31873271Smaybee 31883271Smaybee while ((zl = *zlpp) != NULL) { 31893271Smaybee if (zl->zl_znode != NULL) 31903271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 31913271Smaybee rw_exit(zl->zl_rwlock); 31923271Smaybee *zlpp = zl->zl_next; 31933271Smaybee kmem_free(zl, sizeof (*zl)); 31943271Smaybee } 31953271Smaybee } 31963271Smaybee 3197789Sahrens /* 3198789Sahrens * Search back through the directory tree, using the ".." entries. 3199789Sahrens * Lock each directory in the chain to prevent concurrent renames. 3200789Sahrens * Fail any attempt to move a directory into one of its own descendants. 3201789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 3202789Sahrens */ 3203789Sahrens static int 3204789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3205789Sahrens { 3206789Sahrens zfs_zlock_t *zl; 32073638Sbillm znode_t *zp = tdzp; 3208789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 320911935SMark.Shellenbaum@Sun.COM uint64_t oidp = zp->z_id; 3210789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 3211789Sahrens krw_t rw = RW_WRITER; 3212789Sahrens 3213789Sahrens /* 3214789Sahrens * First pass write-locks szp and compares to zp->z_id. 3215789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 3216789Sahrens */ 3217789Sahrens do { 32183271Smaybee if (!rw_tryenter(rwlp, rw)) { 32193271Smaybee /* 32203271Smaybee * Another thread is renaming in this path. 32213271Smaybee * Note that if we are a WRITER, we don't have any 32223271Smaybee * parent_locks held yet. 32233271Smaybee */ 32243271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 32253271Smaybee /* 32263271Smaybee * Drop our locks and restart 32273271Smaybee */ 32283271Smaybee zfs_rename_unlock(&zl); 32293271Smaybee *zlpp = NULL; 32303271Smaybee zp = tdzp; 323111935SMark.Shellenbaum@Sun.COM oidp = zp->z_id; 32323271Smaybee rwlp = &szp->z_parent_lock; 32333271Smaybee rw = RW_WRITER; 32343271Smaybee continue; 32353271Smaybee } else { 32363271Smaybee /* 32373271Smaybee * Wait for other thread to drop its locks 32383271Smaybee */ 32393271Smaybee rw_enter(rwlp, rw); 32403271Smaybee } 32413271Smaybee } 32423271Smaybee 3243789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3244789Sahrens zl->zl_rwlock = rwlp; 3245789Sahrens zl->zl_znode = NULL; 3246789Sahrens zl->zl_next = *zlpp; 3247789Sahrens *zlpp = zl; 3248789Sahrens 324911935SMark.Shellenbaum@Sun.COM if (oidp == szp->z_id) /* We're a descendant of szp */ 3250789Sahrens return (EINVAL); 3251789Sahrens 325211935SMark.Shellenbaum@Sun.COM if (oidp == rootid) /* We've hit the top */ 3253789Sahrens return (0); 3254789Sahrens 3255789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 325611935SMark.Shellenbaum@Sun.COM int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3257789Sahrens if (error) 3258789Sahrens return (error); 3259789Sahrens zl->zl_znode = zp; 3260789Sahrens } 326111935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 326211935SMark.Shellenbaum@Sun.COM &oidp, sizeof (oidp)); 3263789Sahrens rwlp = &zp->z_parent_lock; 3264789Sahrens rw = RW_READER; 3265789Sahrens 3266789Sahrens } while (zp->z_id != sdzp->z_id); 3267789Sahrens 3268789Sahrens return (0); 3269789Sahrens } 3270789Sahrens 3271789Sahrens /* 3272789Sahrens * Move an entry from the provided source directory to the target 3273789Sahrens * directory. Change the entry name as indicated. 3274789Sahrens * 3275789Sahrens * IN: sdvp - Source directory containing the "old entry". 3276789Sahrens * snm - Old entry name. 3277789Sahrens * tdvp - Target directory to contain the "new entry". 3278789Sahrens * tnm - New entry name. 3279789Sahrens * cr - credentials of caller. 32805331Samw * ct - caller context 32815331Samw * flags - case flags 3282789Sahrens * 3283789Sahrens * RETURN: 0 if success 3284789Sahrens * error code if failure 3285789Sahrens * 3286789Sahrens * Timestamps: 3287789Sahrens * sdvp,tdvp - ctime|mtime updated 3288789Sahrens */ 32895331Samw /*ARGSUSED*/ 3290789Sahrens static int 32915331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 32925331Samw caller_context_t *ct, int flags) 3293789Sahrens { 3294789Sahrens znode_t *tdzp, *szp, *tzp; 3295789Sahrens znode_t *sdzp = VTOZ(sdvp); 3296789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 32975326Sek110237 zilog_t *zilog; 3298789Sahrens vnode_t *realvp; 3299789Sahrens zfs_dirlock_t *sdl, *tdl; 3300789Sahrens dmu_tx_t *tx; 3301789Sahrens zfs_zlock_t *zl; 33025331Samw int cmp, serr, terr; 33035331Samw int error = 0; 33045331Samw int zflg = 0; 3305789Sahrens 33065367Sahrens ZFS_ENTER(zfsvfs); 33075367Sahrens ZFS_VERIFY_ZP(sdzp); 33085326Sek110237 zilog = zfsvfs->z_log; 3309789Sahrens 3310789Sahrens /* 3311789Sahrens * Make sure we have the real vp for the target directory. 3312789Sahrens */ 33135331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3314789Sahrens tdvp = realvp; 3315789Sahrens 331612079SMark.Shellenbaum@Sun.COM if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) { 3317789Sahrens ZFS_EXIT(zfsvfs); 3318789Sahrens return (EXDEV); 3319789Sahrens } 3320789Sahrens 3321789Sahrens tdzp = VTOZ(tdvp); 33225367Sahrens ZFS_VERIFY_ZP(tdzp); 33235498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 33245331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 33255331Samw ZFS_EXIT(zfsvfs); 33265331Samw return (EILSEQ); 33275331Samw } 33285331Samw 33295331Samw if (flags & FIGNORECASE) 33305331Samw zflg |= ZCILOOK; 33315331Samw 3332789Sahrens top: 3333789Sahrens szp = NULL; 3334789Sahrens tzp = NULL; 3335789Sahrens zl = NULL; 3336789Sahrens 3337789Sahrens /* 3338789Sahrens * This is to prevent the creation of links into attribute space 3339789Sahrens * by renaming a linked file into/outof an attribute directory. 3340789Sahrens * See the comment in zfs_link() for why this is considered bad. 3341789Sahrens */ 334211935SMark.Shellenbaum@Sun.COM if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3343789Sahrens ZFS_EXIT(zfsvfs); 3344789Sahrens return (EINVAL); 3345789Sahrens } 3346789Sahrens 3347789Sahrens /* 3348789Sahrens * Lock source and target directory entries. To prevent deadlock, 3349789Sahrens * a lock ordering must be defined. We lock the directory with 3350789Sahrens * the smallest object id first, or if it's a tie, the one with 3351789Sahrens * the lexically first name. 3352789Sahrens */ 3353789Sahrens if (sdzp->z_id < tdzp->z_id) { 3354789Sahrens cmp = -1; 3355789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3356789Sahrens cmp = 1; 3357789Sahrens } else { 33585331Samw /* 33595331Samw * First compare the two name arguments without 33605331Samw * considering any case folding. 33615331Samw */ 33625331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 33635331Samw 33645331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 33655498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3366789Sahrens if (cmp == 0) { 3367789Sahrens /* 3368789Sahrens * POSIX: "If the old argument and the new argument 3369789Sahrens * both refer to links to the same existing file, 3370789Sahrens * the rename() function shall return successfully 3371789Sahrens * and perform no other action." 3372789Sahrens */ 3373789Sahrens ZFS_EXIT(zfsvfs); 3374789Sahrens return (0); 3375789Sahrens } 33765331Samw /* 33775331Samw * If the file system is case-folding, then we may 33785331Samw * have some more checking to do. A case-folding file 33795331Samw * system is either supporting mixed case sensitivity 33805331Samw * access or is completely case-insensitive. Note 33815331Samw * that the file system is always case preserving. 33825331Samw * 33835331Samw * In mixed sensitivity mode case sensitive behavior 33845331Samw * is the default. FIGNORECASE must be used to 33855331Samw * explicitly request case insensitive behavior. 33865331Samw * 33875331Samw * If the source and target names provided differ only 33885331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 33895331Samw * we will treat this as a special case in the 33905331Samw * case-insensitive mode: as long as the source name 33915331Samw * is an exact match, we will allow this to proceed as 33925331Samw * a name-change request. 33935331Samw */ 33945498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 33955498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 33965498Stimh flags & FIGNORECASE)) && 33975331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 33985331Samw &error) == 0) { 33995331Samw /* 34005331Samw * case preserving rename request, require exact 34015331Samw * name matches 34025331Samw */ 34035331Samw zflg |= ZCIEXACT; 34045331Samw zflg &= ~ZCILOOK; 34055331Samw } 3406789Sahrens } 34075331Samw 340811321SSanjeev.Bagewadi@Sun.COM /* 340911321SSanjeev.Bagewadi@Sun.COM * If the source and destination directories are the same, we should 341011321SSanjeev.Bagewadi@Sun.COM * grab the z_name_lock of that directory only once. 341111321SSanjeev.Bagewadi@Sun.COM */ 341211321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) { 341311321SSanjeev.Bagewadi@Sun.COM zflg |= ZHAVELOCK; 341411321SSanjeev.Bagewadi@Sun.COM rw_enter(&sdzp->z_name_lock, RW_READER); 341511321SSanjeev.Bagewadi@Sun.COM } 341611321SSanjeev.Bagewadi@Sun.COM 3417789Sahrens if (cmp < 0) { 34185331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 34195331Samw ZEXISTS | zflg, NULL, NULL); 34205331Samw terr = zfs_dirent_lock(&tdl, 34215331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3422789Sahrens } else { 34235331Samw terr = zfs_dirent_lock(&tdl, 34245331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 34255331Samw serr = zfs_dirent_lock(&sdl, 34265331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 34275331Samw NULL, NULL); 3428789Sahrens } 3429789Sahrens 3430789Sahrens if (serr) { 3431789Sahrens /* 3432789Sahrens * Source entry invalid or not there. 3433789Sahrens */ 3434789Sahrens if (!terr) { 3435789Sahrens zfs_dirent_unlock(tdl); 3436789Sahrens if (tzp) 3437789Sahrens VN_RELE(ZTOV(tzp)); 3438789Sahrens } 343911321SSanjeev.Bagewadi@Sun.COM 344011321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 344111321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 344211321SSanjeev.Bagewadi@Sun.COM 3443789Sahrens if (strcmp(snm, "..") == 0) 3444789Sahrens serr = EINVAL; 3445789Sahrens ZFS_EXIT(zfsvfs); 3446789Sahrens return (serr); 3447789Sahrens } 3448789Sahrens if (terr) { 3449789Sahrens zfs_dirent_unlock(sdl); 3450789Sahrens VN_RELE(ZTOV(szp)); 345111321SSanjeev.Bagewadi@Sun.COM 345211321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 345311321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 345411321SSanjeev.Bagewadi@Sun.COM 3455789Sahrens if (strcmp(tnm, "..") == 0) 3456789Sahrens terr = EINVAL; 3457789Sahrens ZFS_EXIT(zfsvfs); 3458789Sahrens return (terr); 3459789Sahrens } 3460789Sahrens 3461789Sahrens /* 3462789Sahrens * Must have write access at the source to remove the old entry 3463789Sahrens * and write access at the target to create the new entry. 3464789Sahrens * Note that if target and source are the same, this can be 3465789Sahrens * done in a single check. 3466789Sahrens */ 3467789Sahrens 3468789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3469789Sahrens goto out; 3470789Sahrens 3471789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3472789Sahrens /* 3473789Sahrens * Check to make sure rename is valid. 3474789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3475789Sahrens */ 3476789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3477789Sahrens goto out; 3478789Sahrens } 3479789Sahrens 3480789Sahrens /* 3481789Sahrens * Does target exist? 3482789Sahrens */ 3483789Sahrens if (tzp) { 3484789Sahrens /* 3485789Sahrens * Source and target must be the same type. 3486789Sahrens */ 3487789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3488789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3489789Sahrens error = ENOTDIR; 3490789Sahrens goto out; 3491789Sahrens } 3492789Sahrens } else { 3493789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3494789Sahrens error = EISDIR; 3495789Sahrens goto out; 3496789Sahrens } 3497789Sahrens } 3498789Sahrens /* 3499789Sahrens * POSIX dictates that when the source and target 3500789Sahrens * entries refer to the same file object, rename 3501789Sahrens * must do nothing and exit without error. 3502789Sahrens */ 3503789Sahrens if (szp->z_id == tzp->z_id) { 3504789Sahrens error = 0; 3505789Sahrens goto out; 3506789Sahrens } 3507789Sahrens } 3508789Sahrens 35095331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3510789Sahrens if (tzp) 35115331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 35124863Spraks 35134863Spraks /* 35144863Spraks * notify the target directory if it is not the same 35154863Spraks * as source directory. 35164863Spraks */ 35174863Spraks if (tdvp != sdvp) { 35185331Samw vnevent_rename_dest_dir(tdvp, ct); 35194863Spraks } 3520789Sahrens 3521789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 352211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 352311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 35241544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 35251544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 352611935SMark.Shellenbaum@Sun.COM if (sdzp != tdzp) { 352711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 352811935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tdzp); 352911935SMark.Shellenbaum@Sun.COM } 353011935SMark.Shellenbaum@Sun.COM if (tzp) { 353111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 353211935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tzp); 353311935SMark.Shellenbaum@Sun.COM } 353411935SMark.Shellenbaum@Sun.COM 353511935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 35363461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 35378227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3538789Sahrens if (error) { 3539789Sahrens if (zl != NULL) 3540789Sahrens zfs_rename_unlock(&zl); 3541789Sahrens zfs_dirent_unlock(sdl); 3542789Sahrens zfs_dirent_unlock(tdl); 354311321SSanjeev.Bagewadi@Sun.COM 354411321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 354511321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 354611321SSanjeev.Bagewadi@Sun.COM 3547789Sahrens VN_RELE(ZTOV(szp)); 3548789Sahrens if (tzp) 3549789Sahrens VN_RELE(ZTOV(tzp)); 35508227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35512113Sahrens dmu_tx_wait(tx); 35522113Sahrens dmu_tx_abort(tx); 3553789Sahrens goto top; 3554789Sahrens } 35552113Sahrens dmu_tx_abort(tx); 3556789Sahrens ZFS_EXIT(zfsvfs); 3557789Sahrens return (error); 3558789Sahrens } 3559789Sahrens 3560789Sahrens if (tzp) /* Attempt to remove the existing target */ 35615331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3562789Sahrens 3563789Sahrens if (error == 0) { 3564789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3565789Sahrens if (error == 0) { 356611935SMark.Shellenbaum@Sun.COM szp->z_pflags |= ZFS_AV_MODIFIED; 356711935SMark.Shellenbaum@Sun.COM 356811935SMark.Shellenbaum@Sun.COM error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 356911935SMark.Shellenbaum@Sun.COM (void *)&szp->z_pflags, sizeof (uint64_t), tx); 357011935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 35715331Samw 3572789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 357312413SSam.Falkner@Sun.COM if (error == 0) { 357412413SSam.Falkner@Sun.COM zfs_log_rename(zilog, tx, TX_RENAME | 357512413SSam.Falkner@Sun.COM (flags & FIGNORECASE ? TX_CI : 0), 3576*12699SNeil.Perrin@Sun.COM sdzp, sdl->dl_name, tdzp, tdl->dl_name); 357712413SSam.Falkner@Sun.COM 357812413SSam.Falkner@Sun.COM /* 357912413SSam.Falkner@Sun.COM * Update path information for the target vnode 358012413SSam.Falkner@Sun.COM */ 358112413SSam.Falkner@Sun.COM vn_renamepath(tdvp, ZTOV(szp), tnm, 358212413SSam.Falkner@Sun.COM strlen(tnm)); 358312413SSam.Falkner@Sun.COM } else { 358412413SSam.Falkner@Sun.COM /* 358512413SSam.Falkner@Sun.COM * At this point, we have successfully created 358612413SSam.Falkner@Sun.COM * the target name, but have failed to remove 358712413SSam.Falkner@Sun.COM * the source name. Since the create was done 358812413SSam.Falkner@Sun.COM * with the ZRENAMING flag, there are 358912413SSam.Falkner@Sun.COM * complications; for one, the link count is 359012413SSam.Falkner@Sun.COM * wrong. The easiest way to deal with this 359112413SSam.Falkner@Sun.COM * is to remove the newly created target, and 359212413SSam.Falkner@Sun.COM * return the original error. This must 359312413SSam.Falkner@Sun.COM * succeed; fortunately, it is very unlikely to 359412413SSam.Falkner@Sun.COM * fail, since we just created it. 359512413SSam.Falkner@Sun.COM */ 359612413SSam.Falkner@Sun.COM VERIFY3U(zfs_link_destroy(tdl, szp, tx, 359712413SSam.Falkner@Sun.COM ZRENAMING, NULL), ==, 0); 359812413SSam.Falkner@Sun.COM } 3599789Sahrens } 3600789Sahrens } 3601789Sahrens 3602789Sahrens dmu_tx_commit(tx); 3603789Sahrens out: 3604789Sahrens if (zl != NULL) 3605789Sahrens zfs_rename_unlock(&zl); 3606789Sahrens 3607789Sahrens zfs_dirent_unlock(sdl); 3608789Sahrens zfs_dirent_unlock(tdl); 3609789Sahrens 361011321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 361111321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 361211321SSanjeev.Bagewadi@Sun.COM 361311321SSanjeev.Bagewadi@Sun.COM 3614789Sahrens VN_RELE(ZTOV(szp)); 3615789Sahrens if (tzp) 3616789Sahrens VN_RELE(ZTOV(tzp)); 3617789Sahrens 361812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3619*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 362012294SMark.Musante@Sun.COM 3621789Sahrens ZFS_EXIT(zfsvfs); 3622789Sahrens return (error); 3623789Sahrens } 3624789Sahrens 3625789Sahrens /* 3626789Sahrens * Insert the indicated symbolic reference entry into the directory. 3627789Sahrens * 3628789Sahrens * IN: dvp - Directory to contain new symbolic link. 3629789Sahrens * link - Name for new symlink entry. 3630789Sahrens * vap - Attributes of new entry. 3631789Sahrens * target - Target path of new symlink. 3632789Sahrens * cr - credentials of caller. 36335331Samw * ct - caller context 36345331Samw * flags - case flags 3635789Sahrens * 3636789Sahrens * RETURN: 0 if success 3637789Sahrens * error code if failure 3638789Sahrens * 3639789Sahrens * Timestamps: 3640789Sahrens * dvp - ctime|mtime updated 3641789Sahrens */ 36425331Samw /*ARGSUSED*/ 3643789Sahrens static int 36445331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 36455331Samw caller_context_t *ct, int flags) 3646789Sahrens { 3647789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3648789Sahrens zfs_dirlock_t *dl; 3649789Sahrens dmu_tx_t *tx; 3650789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 36515326Sek110237 zilog_t *zilog; 365211935SMark.Shellenbaum@Sun.COM uint64_t len = strlen(link); 3653789Sahrens int error; 36545331Samw int zflg = ZNEW; 36559179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 36569179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 365711935SMark.Shellenbaum@Sun.COM uint64_t txtype = TX_SYMLINK; 3658789Sahrens 3659789Sahrens ASSERT(vap->va_type == VLNK); 3660789Sahrens 36615367Sahrens ZFS_ENTER(zfsvfs); 36625367Sahrens ZFS_VERIFY_ZP(dzp); 36635326Sek110237 zilog = zfsvfs->z_log; 36645331Samw 36655498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 36665331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 36675331Samw ZFS_EXIT(zfsvfs); 36685331Samw return (EILSEQ); 36695331Samw } 36705331Samw if (flags & FIGNORECASE) 36715331Samw zflg |= ZCILOOK; 3672789Sahrens 3673789Sahrens if (len > MAXPATHLEN) { 3674789Sahrens ZFS_EXIT(zfsvfs); 3675789Sahrens return (ENAMETOOLONG); 3676789Sahrens } 3677789Sahrens 367812302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, 367912302SMark.Shellenbaum@Sun.COM vap, cr, NULL, &acl_ids)) != 0) { 368012302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 368112302SMark.Shellenbaum@Sun.COM return (error); 368212302SMark.Shellenbaum@Sun.COM } 368312302SMark.Shellenbaum@Sun.COM top: 3684789Sahrens /* 3685789Sahrens * Attempt to lock directory; fail if entry already exists. 3686789Sahrens */ 36875331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 36885331Samw if (error) { 368912302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3690789Sahrens ZFS_EXIT(zfsvfs); 3691789Sahrens return (error); 3692789Sahrens } 3693789Sahrens 369412302SMark.Shellenbaum@Sun.COM if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 369512302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 369612421SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 369712302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 369812302SMark.Shellenbaum@Sun.COM return (error); 369912302SMark.Shellenbaum@Sun.COM } 370012302SMark.Shellenbaum@Sun.COM 37019396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 37029396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 37039396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 37049396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 37059396SMatthew.Ahrens@Sun.COM return (EDQUOT); 37069396SMatthew.Ahrens@Sun.COM } 3707789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 37089179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3709789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 37101544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 371111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 371211935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE + len); 371311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 371411935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 371511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 371611935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 371711935SMark.Shellenbaum@Sun.COM } 37189396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 37199396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 37208227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3721789Sahrens if (error) { 3722789Sahrens zfs_dirent_unlock(dl); 37238227SNeil.Perrin@Sun.COM if (error == ERESTART) { 37242113Sahrens dmu_tx_wait(tx); 37252113Sahrens dmu_tx_abort(tx); 3726789Sahrens goto top; 3727789Sahrens } 372812302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 37292113Sahrens dmu_tx_abort(tx); 3730789Sahrens ZFS_EXIT(zfsvfs); 3731789Sahrens return (error); 3732789Sahrens } 3733789Sahrens 3734789Sahrens /* 3735789Sahrens * Create a new object for the symlink. 373611935SMark.Shellenbaum@Sun.COM * for version 4 ZPL datsets the symlink will be an SA attribute 3737789Sahrens */ 373811935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 373911935SMark.Shellenbaum@Sun.COM 374011935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 374111935SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 374211935SMark.Shellenbaum@Sun.COM 374312620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 374411935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 374511935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 374611935SMark.Shellenbaum@Sun.COM link, len, tx); 374711935SMark.Shellenbaum@Sun.COM else 374811935SMark.Shellenbaum@Sun.COM zfs_sa_symlink(zp, link, len, tx); 374912620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 375011935SMark.Shellenbaum@Sun.COM 375111935SMark.Shellenbaum@Sun.COM zp->z_size = len; 375211935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 375311935SMark.Shellenbaum@Sun.COM &zp->z_size, sizeof (zp->z_size), tx); 3754789Sahrens /* 3755789Sahrens * Insert the new object into the directory. 3756789Sahrens */ 3757789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 375811935SMark.Shellenbaum@Sun.COM 375911935SMark.Shellenbaum@Sun.COM if (flags & FIGNORECASE) 376011935SMark.Shellenbaum@Sun.COM txtype |= TX_CI; 376111935SMark.Shellenbaum@Sun.COM zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 37629179SMark.Shellenbaum@Sun.COM 37639179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3764789Sahrens 3765789Sahrens dmu_tx_commit(tx); 3766789Sahrens 3767789Sahrens zfs_dirent_unlock(dl); 3768789Sahrens 3769789Sahrens VN_RELE(ZTOV(zp)); 3770789Sahrens 377112294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3772*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 377312294SMark.Musante@Sun.COM 3774789Sahrens ZFS_EXIT(zfsvfs); 3775789Sahrens return (error); 3776789Sahrens } 3777789Sahrens 3778789Sahrens /* 3779789Sahrens * Return, in the buffer contained in the provided uio structure, 3780789Sahrens * the symbolic path referred to by vp. 3781789Sahrens * 3782789Sahrens * IN: vp - vnode of symbolic link. 3783789Sahrens * uoip - structure to contain the link path. 3784789Sahrens * cr - credentials of caller. 37855331Samw * ct - caller context 3786789Sahrens * 3787789Sahrens * OUT: uio - structure to contain the link path. 3788789Sahrens * 3789789Sahrens * RETURN: 0 if success 3790789Sahrens * error code if failure 3791789Sahrens * 3792789Sahrens * Timestamps: 3793789Sahrens * vp - atime updated 3794789Sahrens */ 3795789Sahrens /* ARGSUSED */ 3796789Sahrens static int 37975331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3798789Sahrens { 3799789Sahrens znode_t *zp = VTOZ(vp); 3800789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3801789Sahrens int error; 3802789Sahrens 38035367Sahrens ZFS_ENTER(zfsvfs); 38045367Sahrens ZFS_VERIFY_ZP(zp); 3805789Sahrens 380612620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 380711935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 380811935SMark.Shellenbaum@Sun.COM error = sa_lookup_uio(zp->z_sa_hdl, 380911935SMark.Shellenbaum@Sun.COM SA_ZPL_SYMLINK(zfsvfs), uio); 381011935SMark.Shellenbaum@Sun.COM else 381111935SMark.Shellenbaum@Sun.COM error = zfs_sa_readlink(zp, uio); 381212620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 3813789Sahrens 3814789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 381511935SMark.Shellenbaum@Sun.COM 3816789Sahrens ZFS_EXIT(zfsvfs); 3817789Sahrens return (error); 3818789Sahrens } 3819789Sahrens 3820789Sahrens /* 3821789Sahrens * Insert a new entry into directory tdvp referencing svp. 3822789Sahrens * 3823789Sahrens * IN: tdvp - Directory to contain new entry. 3824789Sahrens * svp - vnode of new entry. 3825789Sahrens * name - name of new entry. 3826789Sahrens * cr - credentials of caller. 38275331Samw * ct - caller context 3828789Sahrens * 3829789Sahrens * RETURN: 0 if success 3830789Sahrens * error code if failure 3831789Sahrens * 3832789Sahrens * Timestamps: 3833789Sahrens * tdvp - ctime|mtime updated 3834789Sahrens * svp - ctime updated 3835789Sahrens */ 3836789Sahrens /* ARGSUSED */ 3837789Sahrens static int 38385331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 38395331Samw caller_context_t *ct, int flags) 3840789Sahrens { 3841789Sahrens znode_t *dzp = VTOZ(tdvp); 3842789Sahrens znode_t *tzp, *szp; 3843789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 38445326Sek110237 zilog_t *zilog; 3845789Sahrens zfs_dirlock_t *dl; 3846789Sahrens dmu_tx_t *tx; 3847789Sahrens vnode_t *realvp; 3848789Sahrens int error; 38495331Samw int zf = ZNEW; 385012079SMark.Shellenbaum@Sun.COM uint64_t parent; 3851789Sahrens 3852789Sahrens ASSERT(tdvp->v_type == VDIR); 3853789Sahrens 38545367Sahrens ZFS_ENTER(zfsvfs); 38555367Sahrens ZFS_VERIFY_ZP(dzp); 38565326Sek110237 zilog = zfsvfs->z_log; 3857789Sahrens 38585331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3859789Sahrens svp = realvp; 3860789Sahrens 386112079SMark.Shellenbaum@Sun.COM /* 386212079SMark.Shellenbaum@Sun.COM * POSIX dictates that we return EPERM here. 386312079SMark.Shellenbaum@Sun.COM * Better choices include ENOTSUP or EISDIR. 386412079SMark.Shellenbaum@Sun.COM */ 386512079SMark.Shellenbaum@Sun.COM if (svp->v_type == VDIR) { 386612079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 386712079SMark.Shellenbaum@Sun.COM return (EPERM); 386812079SMark.Shellenbaum@Sun.COM } 386912079SMark.Shellenbaum@Sun.COM 387012079SMark.Shellenbaum@Sun.COM if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) { 3871789Sahrens ZFS_EXIT(zfsvfs); 3872789Sahrens return (EXDEV); 3873789Sahrens } 387412079SMark.Shellenbaum@Sun.COM 38755367Sahrens szp = VTOZ(svp); 38765367Sahrens ZFS_VERIFY_ZP(szp); 3877789Sahrens 387812079SMark.Shellenbaum@Sun.COM /* Prevent links to .zfs/shares files */ 387912079SMark.Shellenbaum@Sun.COM 388012079SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 388112079SMark.Shellenbaum@Sun.COM &parent, sizeof (uint64_t))) != 0) { 388212079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 388312079SMark.Shellenbaum@Sun.COM return (error); 388412079SMark.Shellenbaum@Sun.COM } 388512079SMark.Shellenbaum@Sun.COM if (parent == zfsvfs->z_shares_dir) { 388612079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 388712079SMark.Shellenbaum@Sun.COM return (EPERM); 388812079SMark.Shellenbaum@Sun.COM } 388912079SMark.Shellenbaum@Sun.COM 38905498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 38915331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 38925331Samw ZFS_EXIT(zfsvfs); 38935331Samw return (EILSEQ); 38945331Samw } 38955331Samw if (flags & FIGNORECASE) 38965331Samw zf |= ZCILOOK; 38975331Samw 3898789Sahrens /* 3899789Sahrens * We do not support links between attributes and non-attributes 3900789Sahrens * because of the potential security risk of creating links 3901789Sahrens * into "normal" file space in order to circumvent restrictions 3902789Sahrens * imposed in attribute space. 3903789Sahrens */ 390411935SMark.Shellenbaum@Sun.COM if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 3905789Sahrens ZFS_EXIT(zfsvfs); 3906789Sahrens return (EINVAL); 3907789Sahrens } 3908789Sahrens 3909789Sahrens 391011935SMark.Shellenbaum@Sun.COM if (szp->z_uid != crgetuid(cr) && 3911789Sahrens secpolicy_basic_link(cr) != 0) { 3912789Sahrens ZFS_EXIT(zfsvfs); 3913789Sahrens return (EPERM); 3914789Sahrens } 3915789Sahrens 39165331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3917789Sahrens ZFS_EXIT(zfsvfs); 3918789Sahrens return (error); 3919789Sahrens } 3920789Sahrens 392112079SMark.Shellenbaum@Sun.COM top: 3922789Sahrens /* 3923789Sahrens * Attempt to lock directory; fail if entry already exists. 3924789Sahrens */ 39255331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 39265331Samw if (error) { 3927789Sahrens ZFS_EXIT(zfsvfs); 3928789Sahrens return (error); 3929789Sahrens } 3930789Sahrens 3931789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 393211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 39331544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 393411935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 393511935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 39368227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3937789Sahrens if (error) { 3938789Sahrens zfs_dirent_unlock(dl); 39398227SNeil.Perrin@Sun.COM if (error == ERESTART) { 39402113Sahrens dmu_tx_wait(tx); 39412113Sahrens dmu_tx_abort(tx); 3942789Sahrens goto top; 3943789Sahrens } 39442113Sahrens dmu_tx_abort(tx); 3945789Sahrens ZFS_EXIT(zfsvfs); 3946789Sahrens return (error); 3947789Sahrens } 3948789Sahrens 3949789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3950789Sahrens 39515331Samw if (error == 0) { 39525331Samw uint64_t txtype = TX_LINK; 39535331Samw if (flags & FIGNORECASE) 39545331Samw txtype |= TX_CI; 39555331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 39565331Samw } 3957789Sahrens 3958789Sahrens dmu_tx_commit(tx); 3959789Sahrens 3960789Sahrens zfs_dirent_unlock(dl); 3961789Sahrens 39624863Spraks if (error == 0) { 39635331Samw vnevent_link(svp, ct); 39644863Spraks } 39654863Spraks 396612294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 3967*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 396812294SMark.Musante@Sun.COM 3969789Sahrens ZFS_EXIT(zfsvfs); 3970789Sahrens return (error); 3971789Sahrens } 3972789Sahrens 3973789Sahrens /* 3974789Sahrens * zfs_null_putapage() is used when the file system has been force 3975789Sahrens * unmounted. It just drops the pages. 3976789Sahrens */ 3977789Sahrens /* ARGSUSED */ 3978789Sahrens static int 3979789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3980789Sahrens size_t *lenp, int flags, cred_t *cr) 3981789Sahrens { 3982789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3983789Sahrens return (0); 3984789Sahrens } 3985789Sahrens 39862688Smaybee /* 39872688Smaybee * Push a page out to disk, klustering if possible. 39882688Smaybee * 39892688Smaybee * IN: vp - file to push page to. 39902688Smaybee * pp - page to push. 39912688Smaybee * flags - additional flags. 39922688Smaybee * cr - credentials of caller. 39932688Smaybee * 39942688Smaybee * OUT: offp - start of range pushed. 39952688Smaybee * lenp - len of range pushed. 39962688Smaybee * 39972688Smaybee * RETURN: 0 if success 39982688Smaybee * error code if failure 39992688Smaybee * 40002688Smaybee * NOTE: callers must have locked the page to be pushed. On 40012688Smaybee * exit, the page (and all other pages in the kluster) must be 40022688Smaybee * unlocked. 40032688Smaybee */ 4004789Sahrens /* ARGSUSED */ 4005789Sahrens static int 4006789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4007789Sahrens size_t *lenp, int flags, cred_t *cr) 4008789Sahrens { 4009789Sahrens znode_t *zp = VTOZ(vp); 4010789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4011789Sahrens dmu_tx_t *tx; 40122688Smaybee u_offset_t off, koff; 40132688Smaybee size_t len, klen; 4014789Sahrens int err; 4015789Sahrens 40162688Smaybee off = pp->p_offset; 40172688Smaybee len = PAGESIZE; 40182688Smaybee /* 40192688Smaybee * If our blocksize is bigger than the page size, try to kluster 40208227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 40212688Smaybee * a read-modify-write). 40222688Smaybee */ 402311935SMark.Shellenbaum@Sun.COM if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 40248636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 40258636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 402611935SMark.Shellenbaum@Sun.COM ASSERT(koff <= zp->z_size); 402711935SMark.Shellenbaum@Sun.COM if (koff + klen > zp->z_size) 402811935SMark.Shellenbaum@Sun.COM klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 40292688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 40302688Smaybee } 40312688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 40328636SMark.Maybee@Sun.COM 40331819Smaybee /* 40341819Smaybee * Can't push pages past end-of-file. 40351819Smaybee */ 403611935SMark.Shellenbaum@Sun.COM if (off >= zp->z_size) { 40374709Smaybee /* ignore all pages */ 40382688Smaybee err = 0; 40392688Smaybee goto out; 404011935SMark.Shellenbaum@Sun.COM } else if (off + len > zp->z_size) { 404111935SMark.Shellenbaum@Sun.COM int npages = btopr(zp->z_size - off); 40422688Smaybee page_t *trunc; 40432688Smaybee 40442688Smaybee page_list_break(&pp, &trunc, npages); 40454709Smaybee /* ignore pages past end of file */ 40462688Smaybee if (trunc) 40474709Smaybee pvn_write_done(trunc, flags); 404811935SMark.Shellenbaum@Sun.COM len = zp->z_size - off; 40491819Smaybee } 40509396SMatthew.Ahrens@Sun.COM 405111935SMark.Shellenbaum@Sun.COM if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 405211935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 40539396SMatthew.Ahrens@Sun.COM err = EDQUOT; 40549396SMatthew.Ahrens@Sun.COM goto out; 40559396SMatthew.Ahrens@Sun.COM } 40568636SMark.Maybee@Sun.COM top: 4057789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 4058789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 405911935SMark.Shellenbaum@Sun.COM 406011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 406111935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 40628227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 4063789Sahrens if (err != 0) { 40648227SNeil.Perrin@Sun.COM if (err == ERESTART) { 40652113Sahrens dmu_tx_wait(tx); 40662113Sahrens dmu_tx_abort(tx); 4067789Sahrens goto top; 4068789Sahrens } 40692113Sahrens dmu_tx_abort(tx); 4070789Sahrens goto out; 4071789Sahrens } 4072789Sahrens 40732688Smaybee if (zp->z_blksz <= PAGESIZE) { 40747315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 40752688Smaybee ASSERT3U(len, <=, PAGESIZE); 40762688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 40777315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 40782688Smaybee } else { 40792688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 40802688Smaybee } 40812688Smaybee 40822688Smaybee if (err == 0) { 408311935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 408412394SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[3]; 408511935SMark.Shellenbaum@Sun.COM int count = 0; 408611935SMark.Shellenbaum@Sun.COM 408711935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 408811935SMark.Shellenbaum@Sun.COM &mtime, 16); 408911935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 409011935SMark.Shellenbaum@Sun.COM &ctime, 16); 409112394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 409212394SMark.Shellenbaum@Sun.COM &zp->z_pflags, 8); 409311935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 409411935SMark.Shellenbaum@Sun.COM B_TRUE); 40958636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 40962688Smaybee } 40979951SLin.Ling@Sun.COM dmu_tx_commit(tx); 40982688Smaybee 40992688Smaybee out: 41004709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4101789Sahrens if (offp) 4102789Sahrens *offp = off; 4103789Sahrens if (lenp) 4104789Sahrens *lenp = len; 4105789Sahrens 4106789Sahrens return (err); 4107789Sahrens } 4108789Sahrens 4109789Sahrens /* 4110789Sahrens * Copy the portion of the file indicated from pages into the file. 4111789Sahrens * The pages are stored in a page list attached to the files vnode. 4112789Sahrens * 4113789Sahrens * IN: vp - vnode of file to push page data to. 4114789Sahrens * off - position in file to put data. 4115789Sahrens * len - amount of data to write. 4116789Sahrens * flags - flags to control the operation. 4117789Sahrens * cr - credentials of caller. 41185331Samw * ct - caller context. 4119789Sahrens * 4120789Sahrens * RETURN: 0 if success 4121789Sahrens * error code if failure 4122789Sahrens * 4123789Sahrens * Timestamps: 4124789Sahrens * vp - ctime|mtime updated 4125789Sahrens */ 41265331Samw /*ARGSUSED*/ 4127789Sahrens static int 41285331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 41295331Samw caller_context_t *ct) 4130789Sahrens { 4131789Sahrens znode_t *zp = VTOZ(vp); 4132789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4133789Sahrens page_t *pp; 4134789Sahrens size_t io_len; 4135789Sahrens u_offset_t io_off; 41368636SMark.Maybee@Sun.COM uint_t blksz; 41378636SMark.Maybee@Sun.COM rl_t *rl; 4138789Sahrens int error = 0; 4139789Sahrens 41405367Sahrens ZFS_ENTER(zfsvfs); 41415367Sahrens ZFS_VERIFY_ZP(zp); 4142789Sahrens 41438636SMark.Maybee@Sun.COM /* 41448636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 41458636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 41468636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 41478636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 41488636SMark.Maybee@Sun.COM * operations to get narrower locking. 41498636SMark.Maybee@Sun.COM */ 41508636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 41518636SMark.Maybee@Sun.COM if (ISP2(blksz)) 41528636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 41538636SMark.Maybee@Sun.COM else 41548636SMark.Maybee@Sun.COM io_off = 0; 41558636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 41569141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 41578636SMark.Maybee@Sun.COM else 41588636SMark.Maybee@Sun.COM io_len = 0; 41598636SMark.Maybee@Sun.COM 41608636SMark.Maybee@Sun.COM if (io_len == 0) { 4161789Sahrens /* 41628636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 4163789Sahrens */ 41648636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 41658636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 41661472Sperrin goto out; 4167789Sahrens } 41688636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 41698636SMark.Maybee@Sun.COM 417011935SMark.Shellenbaum@Sun.COM if (off > zp->z_size) { 4171789Sahrens /* past end of file */ 41728636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 4173789Sahrens ZFS_EXIT(zfsvfs); 4174789Sahrens return (0); 4175789Sahrens } 4176789Sahrens 417711935SMark.Shellenbaum@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 41788636SMark.Maybee@Sun.COM 41798636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 4180789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 41811669Sperrin pp = page_lookup(vp, io_off, 41824339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4183789Sahrens } else { 4184789Sahrens pp = page_lookup_nowait(vp, io_off, 41854339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4186789Sahrens } 4187789Sahrens 4188789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 4189789Sahrens int err; 4190789Sahrens 4191789Sahrens /* 4192789Sahrens * Found a dirty page to push 4193789Sahrens */ 41941669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 41951669Sperrin if (err) 4196789Sahrens error = err; 4197789Sahrens } else { 4198789Sahrens io_len = PAGESIZE; 4199789Sahrens } 4200789Sahrens } 42011472Sperrin out: 42028636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 420312294SMark.Musante@Sun.COM if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4204*12699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 4205789Sahrens ZFS_EXIT(zfsvfs); 4206789Sahrens return (error); 4207789Sahrens } 4208789Sahrens 42095331Samw /*ARGSUSED*/ 4210789Sahrens void 42115331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4212789Sahrens { 4213789Sahrens znode_t *zp = VTOZ(vp); 4214789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4215789Sahrens int error; 4216789Sahrens 42175326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 421811935SMark.Shellenbaum@Sun.COM if (zp->z_sa_hdl == NULL) { 42195446Sahrens /* 42205642Smaybee * The fs has been unmounted, or we did a 42215642Smaybee * suspend/resume and this file no longer exists. 42225446Sahrens */ 4223789Sahrens if (vn_has_cached_data(vp)) { 4224789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 4225789Sahrens B_INVAL, cr); 4226789Sahrens } 4227789Sahrens 42281544Seschrock mutex_enter(&zp->z_lock); 422910369Schris.kirby@sun.com mutex_enter(&vp->v_lock); 423010369Schris.kirby@sun.com ASSERT(vp->v_count == 1); 423110369Schris.kirby@sun.com vp->v_count = 0; 423210369Schris.kirby@sun.com mutex_exit(&vp->v_lock); 42335446Sahrens mutex_exit(&zp->z_lock); 42345642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 42355446Sahrens zfs_znode_free(zp); 4236789Sahrens return; 4237789Sahrens } 4238789Sahrens 4239789Sahrens /* 4240789Sahrens * Attempt to push any data in the page cache. If this fails 4241789Sahrens * we will get kicked out later in zfs_zinactive(). 4242789Sahrens */ 42431298Sperrin if (vn_has_cached_data(vp)) { 42441298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 42451298Sperrin cr); 42461298Sperrin } 4247789Sahrens 42483461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4249789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4250789Sahrens 425111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 425211935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 4253789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 4254789Sahrens if (error) { 4255789Sahrens dmu_tx_abort(tx); 4256789Sahrens } else { 4257789Sahrens mutex_enter(&zp->z_lock); 425811935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 425911935SMark.Shellenbaum@Sun.COM (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4260789Sahrens zp->z_atime_dirty = 0; 4261789Sahrens mutex_exit(&zp->z_lock); 4262789Sahrens dmu_tx_commit(tx); 4263789Sahrens } 4264789Sahrens } 4265789Sahrens 4266789Sahrens zfs_zinactive(zp); 42675326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4268789Sahrens } 4269789Sahrens 4270789Sahrens /* 4271789Sahrens * Bounds-check the seek operation. 4272789Sahrens * 4273789Sahrens * IN: vp - vnode seeking within 4274789Sahrens * ooff - old file offset 4275789Sahrens * noffp - pointer to new file offset 42765331Samw * ct - caller context 4277789Sahrens * 4278789Sahrens * RETURN: 0 if success 4279789Sahrens * EINVAL if new offset invalid 4280789Sahrens */ 4281789Sahrens /* ARGSUSED */ 4282789Sahrens static int 42835331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 42845331Samw caller_context_t *ct) 4285789Sahrens { 4286789Sahrens if (vp->v_type == VDIR) 4287789Sahrens return (0); 4288789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4289789Sahrens } 4290789Sahrens 4291789Sahrens /* 4292789Sahrens * Pre-filter the generic locking function to trap attempts to place 4293789Sahrens * a mandatory lock on a memory mapped file. 4294789Sahrens */ 4295789Sahrens static int 4296789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 42975331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4298789Sahrens { 4299789Sahrens znode_t *zp = VTOZ(vp); 4300789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4301789Sahrens 43025367Sahrens ZFS_ENTER(zfsvfs); 43035367Sahrens ZFS_VERIFY_ZP(zp); 4304789Sahrens 4305789Sahrens /* 43061544Seschrock * We are following the UFS semantics with respect to mapcnt 43071544Seschrock * here: If we see that the file is mapped already, then we will 43081544Seschrock * return an error, but we don't worry about races between this 43091544Seschrock * function and zfs_map(). 4310789Sahrens */ 431111935SMark.Shellenbaum@Sun.COM if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4312789Sahrens ZFS_EXIT(zfsvfs); 4313789Sahrens return (EAGAIN); 4314789Sahrens } 4315789Sahrens ZFS_EXIT(zfsvfs); 431610896SMark.Shellenbaum@Sun.COM return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4317789Sahrens } 4318789Sahrens 4319789Sahrens /* 4320789Sahrens * If we can't find a page in the cache, we will create a new page 4321789Sahrens * and fill it with file data. For efficiency, we may try to fill 43228636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 43239265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 43249265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4325789Sahrens */ 4326789Sahrens static int 4327789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4328789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4329789Sahrens { 4330789Sahrens znode_t *zp = VTOZ(vp); 4331789Sahrens page_t *pp, *cur_pp; 4332789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4333789Sahrens u_offset_t io_off, total; 4334789Sahrens size_t io_len; 4335789Sahrens int err; 4336789Sahrens 43372688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 43388636SMark.Maybee@Sun.COM /* 43398636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 43408636SMark.Maybee@Sun.COM */ 4341789Sahrens io_off = off; 4342789Sahrens io_len = PAGESIZE; 43439265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 43449265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4345789Sahrens } else { 4346789Sahrens /* 43478636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4348789Sahrens */ 4349789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 43508636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4351789Sahrens } 4352789Sahrens if (pp == NULL) { 4353789Sahrens /* 43548636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4355789Sahrens */ 4356789Sahrens *pl = NULL; 4357789Sahrens return (0); 4358789Sahrens } 4359789Sahrens 4360789Sahrens /* 4361789Sahrens * Fill the pages in the kluster. 4362789Sahrens */ 4363789Sahrens cur_pp = pp; 4364789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 43658636SMark.Maybee@Sun.COM caddr_t va; 43668636SMark.Maybee@Sun.COM 43672688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 43687315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 43699512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 43709512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 43717315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4372789Sahrens if (err) { 4373789Sahrens /* On error, toss the entire kluster */ 4374789Sahrens pvn_read_done(pp, B_ERROR); 43757294Sperrin /* convert checksum errors into IO errors */ 43767294Sperrin if (err == ECKSUM) 43777294Sperrin err = EIO; 4378789Sahrens return (err); 4379789Sahrens } 4380789Sahrens cur_pp = cur_pp->p_next; 4381789Sahrens } 43828636SMark.Maybee@Sun.COM 4383789Sahrens /* 43848636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 43858636SMark.Maybee@Sun.COM * from the desired offset `off'. 4386789Sahrens * NOTE: the page list will always be null terminated. 4387789Sahrens */ 4388789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 43898636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4390789Sahrens 4391789Sahrens return (0); 4392789Sahrens } 4393789Sahrens 4394789Sahrens /* 4395789Sahrens * Return pointers to the pages for the file region [off, off + len] 4396789Sahrens * in the pl array. If plsz is greater than len, this function may 43978636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 43988636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 43998636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 44008636SMark.Maybee@Sun.COM * part of a klustered read. 4401789Sahrens * 4402789Sahrens * IN: vp - vnode of file to get data from. 4403789Sahrens * off - position in file to get data from. 4404789Sahrens * len - amount of data to retrieve. 4405789Sahrens * plsz - length of provided page list. 4406789Sahrens * seg - segment to obtain pages for. 4407789Sahrens * addr - virtual address of fault. 4408789Sahrens * rw - mode of created pages. 4409789Sahrens * cr - credentials of caller. 44105331Samw * ct - caller context. 4411789Sahrens * 4412789Sahrens * OUT: protp - protection mode of created pages. 4413789Sahrens * pl - list of pages created. 4414789Sahrens * 4415789Sahrens * RETURN: 0 if success 4416789Sahrens * error code if failure 4417789Sahrens * 4418789Sahrens * Timestamps: 4419789Sahrens * vp - atime updated 4420789Sahrens */ 4421789Sahrens /* ARGSUSED */ 4422789Sahrens static int 4423789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4424789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 44255331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4426789Sahrens { 4427789Sahrens znode_t *zp = VTOZ(vp); 4428789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 44298636SMark.Maybee@Sun.COM page_t **pl0 = pl; 44308636SMark.Maybee@Sun.COM int err = 0; 44318636SMark.Maybee@Sun.COM 44328636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 44338636SMark.Maybee@Sun.COM if (pl == NULL) 44348636SMark.Maybee@Sun.COM return (0); 44358636SMark.Maybee@Sun.COM else if (len > plsz) 44368636SMark.Maybee@Sun.COM len = plsz; 44378681SMark.Maybee@Sun.COM else 44388681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 44398636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4440789Sahrens 44415367Sahrens ZFS_ENTER(zfsvfs); 44425367Sahrens ZFS_VERIFY_ZP(zp); 4443789Sahrens 4444789Sahrens if (protp) 4445789Sahrens *protp = PROT_ALL; 4446789Sahrens 4447789Sahrens /* 44489265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4449789Sahrens * for pages. If we don't find a page, we will need to create 4450789Sahrens * a new page and fill it with data from the file. 4451789Sahrens */ 4452789Sahrens while (len > 0) { 44538636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 44548636SMark.Maybee@Sun.COM *(pl+1) = NULL; 44558636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 44568636SMark.Maybee@Sun.COM goto out; 44578636SMark.Maybee@Sun.COM while (*pl) { 44588636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4459789Sahrens off += PAGESIZE; 4460789Sahrens addr += PAGESIZE; 44618681SMark.Maybee@Sun.COM if (len > 0) { 44628681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 44638636SMark.Maybee@Sun.COM len -= PAGESIZE; 44648681SMark.Maybee@Sun.COM } 44658636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4466789Sahrens plsz -= PAGESIZE; 44678636SMark.Maybee@Sun.COM pl++; 4468789Sahrens } 4469789Sahrens } 4470789Sahrens 4471789Sahrens /* 4472789Sahrens * Fill out the page array with any pages already in the cache. 4473789Sahrens */ 44748636SMark.Maybee@Sun.COM while (plsz > 0 && 44758636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 44768636SMark.Maybee@Sun.COM off += PAGESIZE; 44778636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4478789Sahrens } 4479789Sahrens out: 44802752Sperrin if (err) { 44812752Sperrin /* 44822752Sperrin * Release any pages we have previously locked. 44832752Sperrin */ 44842752Sperrin while (pl > pl0) 44852752Sperrin page_unlock(*--pl); 44868636SMark.Maybee@Sun.COM } else { 44878636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 44882752Sperrin } 44892752Sperrin 4490789Sahrens *pl = NULL; 4491789Sahrens 4492789Sahrens ZFS_EXIT(zfsvfs); 4493789Sahrens return (err); 4494789Sahrens } 4495789Sahrens 44961544Seschrock /* 44971544Seschrock * Request a memory map for a section of a file. This code interacts 44981544Seschrock * with common code and the VM system as follows: 44991544Seschrock * 45001544Seschrock * common code calls mmap(), which ends up in smmap_common() 45011544Seschrock * 45021544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 45031544Seschrock * 45041544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 45051544Seschrock * 45061544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 45071544Seschrock * 45081544Seschrock * zfs_addmap() updates z_mapcnt 45091544Seschrock */ 45105331Samw /*ARGSUSED*/ 4511789Sahrens static int 4512789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 45135331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 45145331Samw caller_context_t *ct) 4515789Sahrens { 4516789Sahrens znode_t *zp = VTOZ(vp); 4517789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4518789Sahrens segvn_crargs_t vn_a; 4519789Sahrens int error; 4520789Sahrens 45215929Smarks ZFS_ENTER(zfsvfs); 45225929Smarks ZFS_VERIFY_ZP(zp); 45235929Smarks 452411935SMark.Shellenbaum@Sun.COM if ((prot & PROT_WRITE) && (zp->z_pflags & 452511935SMark.Shellenbaum@Sun.COM (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 45265929Smarks ZFS_EXIT(zfsvfs); 45275331Samw return (EPERM); 45285929Smarks } 45295929Smarks 45305929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 453111935SMark.Shellenbaum@Sun.COM (zp->z_pflags & ZFS_AV_QUARANTINED)) { 45325929Smarks ZFS_EXIT(zfsvfs); 45335929Smarks return (EACCES); 45345929Smarks } 4535789Sahrens 4536789Sahrens if (vp->v_flag & VNOMAP) { 4537789Sahrens ZFS_EXIT(zfsvfs); 4538789Sahrens return (ENOSYS); 4539789Sahrens } 4540789Sahrens 4541789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4542789Sahrens ZFS_EXIT(zfsvfs); 4543789Sahrens return (ENXIO); 4544789Sahrens } 4545789Sahrens 4546789Sahrens if (vp->v_type != VREG) { 4547789Sahrens ZFS_EXIT(zfsvfs); 4548789Sahrens return (ENODEV); 4549789Sahrens } 4550789Sahrens 4551789Sahrens /* 4552789Sahrens * If file is locked, disallow mapping. 4553789Sahrens */ 455411935SMark.Shellenbaum@Sun.COM if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 45551544Seschrock ZFS_EXIT(zfsvfs); 45561544Seschrock return (EAGAIN); 4557789Sahrens } 4558789Sahrens 4559789Sahrens as_rangelock(as); 45606036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 45616036Smec if (error != 0) { 45626036Smec as_rangeunlock(as); 45636036Smec ZFS_EXIT(zfsvfs); 45646036Smec return (error); 4565789Sahrens } 4566789Sahrens 4567789Sahrens vn_a.vp = vp; 4568789Sahrens vn_a.offset = (u_offset_t)off; 4569789Sahrens vn_a.type = flags & MAP_TYPE; 4570789Sahrens vn_a.prot = prot; 4571789Sahrens vn_a.maxprot = maxprot; 4572789Sahrens vn_a.cred = cr; 4573789Sahrens vn_a.amp = NULL; 4574789Sahrens vn_a.flags = flags & ~MAP_TYPE; 45751417Skchow vn_a.szc = 0; 45761417Skchow vn_a.lgrp_mem_policy_flags = 0; 4577789Sahrens 4578789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4579789Sahrens 4580789Sahrens as_rangeunlock(as); 4581789Sahrens ZFS_EXIT(zfsvfs); 4582789Sahrens return (error); 4583789Sahrens } 4584789Sahrens 4585789Sahrens /* ARGSUSED */ 4586789Sahrens static int 4587789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 45885331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 45895331Samw caller_context_t *ct) 4590789Sahrens { 45911544Seschrock uint64_t pages = btopr(len); 45921544Seschrock 45931544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4594789Sahrens return (0); 4595789Sahrens } 4596789Sahrens 45971773Seschrock /* 45981773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 45991773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 46001773Seschrock * detecting when the data was actually modified, we have to resort to 46011773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 46021773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 46031773Seschrock * which by far the most common case: 46041773Seschrock * 46051773Seschrock * open() 46061773Seschrock * mmap() 46071773Seschrock * <modify memory> 46081773Seschrock * munmap() 46091773Seschrock * close() 46101773Seschrock * <time lapse> 46111773Seschrock * putpage() via fsflush 46121773Seschrock * 46131773Seschrock * If we wait until fsflush to come along, we can have a modification time that 46141773Seschrock * is some arbitrary point in the future. In order to prevent this in the 46151773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 46161773Seschrock * torn down. 46171773Seschrock */ 4618789Sahrens /* ARGSUSED */ 4619789Sahrens static int 4620789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 46215331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 46225331Samw caller_context_t *ct) 4623789Sahrens { 46241544Seschrock uint64_t pages = btopr(len); 46251544Seschrock 46261544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 46271544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 46281773Seschrock 46291773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 46301773Seschrock vn_has_cached_data(vp)) 46315331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 46321773Seschrock 4633789Sahrens return (0); 4634789Sahrens } 4635789Sahrens 4636789Sahrens /* 4637789Sahrens * Free or allocate space in a file. Currently, this function only 4638789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4639789Sahrens * misnamed, as its functionality includes the ability to allocate as 4640789Sahrens * well as free space. 4641789Sahrens * 4642789Sahrens * IN: vp - vnode of file to free data in. 4643789Sahrens * cmd - action to take (only F_FREESP supported). 4644789Sahrens * bfp - section of file to free/alloc. 4645789Sahrens * flag - current file open mode flags. 4646789Sahrens * offset - current file offset. 4647789Sahrens * cr - credentials of caller [UNUSED]. 46485331Samw * ct - caller context. 4649789Sahrens * 4650789Sahrens * RETURN: 0 if success 4651789Sahrens * error code if failure 4652789Sahrens * 4653789Sahrens * Timestamps: 4654789Sahrens * vp - ctime|mtime updated 4655789Sahrens */ 4656789Sahrens /* ARGSUSED */ 4657789Sahrens static int 4658789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4659789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4660789Sahrens { 4661789Sahrens znode_t *zp = VTOZ(vp); 4662789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4663789Sahrens uint64_t off, len; 4664789Sahrens int error; 4665789Sahrens 46665367Sahrens ZFS_ENTER(zfsvfs); 46675367Sahrens ZFS_VERIFY_ZP(zp); 4668789Sahrens 4669789Sahrens if (cmd != F_FREESP) { 4670789Sahrens ZFS_EXIT(zfsvfs); 4671789Sahrens return (EINVAL); 4672789Sahrens } 4673789Sahrens 4674789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4675789Sahrens ZFS_EXIT(zfsvfs); 4676789Sahrens return (error); 4677789Sahrens } 4678789Sahrens 4679789Sahrens if (bfp->l_len < 0) { 4680789Sahrens ZFS_EXIT(zfsvfs); 4681789Sahrens return (EINVAL); 4682789Sahrens } 4683789Sahrens 4684789Sahrens off = bfp->l_start; 46851669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 46861878Smaybee 46876992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4688789Sahrens 4689789Sahrens ZFS_EXIT(zfsvfs); 4690789Sahrens return (error); 4691789Sahrens } 4692789Sahrens 46935331Samw /*ARGSUSED*/ 4694789Sahrens static int 46955331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4696789Sahrens { 4697789Sahrens znode_t *zp = VTOZ(vp); 4698789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 46995326Sek110237 uint32_t gen; 470011935SMark.Shellenbaum@Sun.COM uint64_t gen64; 4701789Sahrens uint64_t object = zp->z_id; 4702789Sahrens zfid_short_t *zfid; 470311935SMark.Shellenbaum@Sun.COM int size, i, error; 4704789Sahrens 47055367Sahrens ZFS_ENTER(zfsvfs); 47065367Sahrens ZFS_VERIFY_ZP(zp); 470711935SMark.Shellenbaum@Sun.COM 470811935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 470912218SMark.Shellenbaum@Sun.COM &gen64, sizeof (uint64_t))) != 0) { 471012218SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 471111935SMark.Shellenbaum@Sun.COM return (error); 471212218SMark.Shellenbaum@Sun.COM } 471311935SMark.Shellenbaum@Sun.COM 471411935SMark.Shellenbaum@Sun.COM gen = (uint32_t)gen64; 4715789Sahrens 4716789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4717789Sahrens if (fidp->fid_len < size) { 4718789Sahrens fidp->fid_len = size; 47191512Sek110237 ZFS_EXIT(zfsvfs); 4720789Sahrens return (ENOSPC); 4721789Sahrens } 4722789Sahrens 4723789Sahrens zfid = (zfid_short_t *)fidp; 4724789Sahrens 4725789Sahrens zfid->zf_len = size; 4726789Sahrens 4727789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4728789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4729789Sahrens 4730789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4731789Sahrens if (gen == 0) 4732789Sahrens gen = 1; 4733789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4734789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4735789Sahrens 4736789Sahrens if (size == LONG_FID_LEN) { 4737789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4738789Sahrens zfid_long_t *zlfid; 4739789Sahrens 4740789Sahrens zlfid = (zfid_long_t *)fidp; 4741789Sahrens 4742789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4743789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4744789Sahrens 4745789Sahrens /* XXX - this should be the generation number for the objset */ 4746789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4747789Sahrens zlfid->zf_setgen[i] = 0; 4748789Sahrens } 4749789Sahrens 4750789Sahrens ZFS_EXIT(zfsvfs); 4751789Sahrens return (0); 4752789Sahrens } 4753789Sahrens 4754789Sahrens static int 47555331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 47565331Samw caller_context_t *ct) 4757789Sahrens { 4758789Sahrens znode_t *zp, *xzp; 4759789Sahrens zfsvfs_t *zfsvfs; 4760789Sahrens zfs_dirlock_t *dl; 4761789Sahrens int error; 4762789Sahrens 4763789Sahrens switch (cmd) { 4764789Sahrens case _PC_LINK_MAX: 4765789Sahrens *valp = ULONG_MAX; 4766789Sahrens return (0); 4767789Sahrens 4768789Sahrens case _PC_FILESIZEBITS: 4769789Sahrens *valp = 64; 4770789Sahrens return (0); 4771789Sahrens 4772789Sahrens case _PC_XATTR_EXISTS: 4773789Sahrens zp = VTOZ(vp); 4774789Sahrens zfsvfs = zp->z_zfsvfs; 47755367Sahrens ZFS_ENTER(zfsvfs); 47765367Sahrens ZFS_VERIFY_ZP(zp); 4777789Sahrens *valp = 0; 4778789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 47795331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4780789Sahrens if (error == 0) { 4781789Sahrens zfs_dirent_unlock(dl); 4782789Sahrens if (!zfs_dirempty(xzp)) 4783789Sahrens *valp = 1; 4784789Sahrens VN_RELE(ZTOV(xzp)); 4785789Sahrens } else if (error == ENOENT) { 4786789Sahrens /* 4787789Sahrens * If there aren't extended attributes, it's the 4788789Sahrens * same as having zero of them. 4789789Sahrens */ 4790789Sahrens error = 0; 4791789Sahrens } 4792789Sahrens ZFS_EXIT(zfsvfs); 4793789Sahrens return (error); 4794789Sahrens 47955331Samw case _PC_SATTR_ENABLED: 47965331Samw case _PC_SATTR_EXISTS: 47977757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 47985331Samw (vp->v_type == VREG || vp->v_type == VDIR); 47995331Samw return (0); 48005331Samw 48019749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 48029749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 48039749STim.Haley@Sun.COM vp->v_type == VDIR; 48049749STim.Haley@Sun.COM return (0); 48059749STim.Haley@Sun.COM 4806789Sahrens case _PC_ACL_ENABLED: 4807789Sahrens *valp = _ACL_ACE_ENABLED; 4808789Sahrens return (0); 4809789Sahrens 4810789Sahrens case _PC_MIN_HOLE_SIZE: 4811789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4812789Sahrens return (0); 4813789Sahrens 481410440SRoger.Faulkner@Sun.COM case _PC_TIMESTAMP_RESOLUTION: 481510440SRoger.Faulkner@Sun.COM /* nanosecond timestamp resolution */ 481610440SRoger.Faulkner@Sun.COM *valp = 1L; 481710440SRoger.Faulkner@Sun.COM return (0); 481810440SRoger.Faulkner@Sun.COM 4819789Sahrens default: 48205331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4821789Sahrens } 4822789Sahrens } 4823789Sahrens 4824789Sahrens /*ARGSUSED*/ 4825789Sahrens static int 48265331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48275331Samw caller_context_t *ct) 4828789Sahrens { 4829789Sahrens znode_t *zp = VTOZ(vp); 4830789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4831789Sahrens int error; 48325331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4833789Sahrens 48345367Sahrens ZFS_ENTER(zfsvfs); 48355367Sahrens ZFS_VERIFY_ZP(zp); 48365331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4837789Sahrens ZFS_EXIT(zfsvfs); 4838789Sahrens 4839789Sahrens return (error); 4840789Sahrens } 4841789Sahrens 4842789Sahrens /*ARGSUSED*/ 4843789Sahrens static int 48445331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48455331Samw caller_context_t *ct) 4846789Sahrens { 4847789Sahrens znode_t *zp = VTOZ(vp); 4848789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4849789Sahrens int error; 48505331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 485112294SMark.Musante@Sun.COM zilog_t *zilog = zfsvfs->z_log; 4852789Sahrens 48535367Sahrens ZFS_ENTER(zfsvfs); 48545367Sahrens ZFS_VERIFY_ZP(zp); 485512294SMark.Musante@Sun.COM 48565331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 485712294SMark.Musante@Sun.COM 485812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 4859*12699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 486012294SMark.Musante@Sun.COM 4861789Sahrens ZFS_EXIT(zfsvfs); 4862789Sahrens return (error); 4863789Sahrens } 4864789Sahrens 4865789Sahrens /* 486611539SChunli.Zhang@Sun.COM * Tunable, both must be a power of 2. 486711539SChunli.Zhang@Sun.COM * 486811539SChunli.Zhang@Sun.COM * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf 486911539SChunli.Zhang@Sun.COM * zcr_blksz_max: if set to less than the file block size, allow loaning out of 487011539SChunli.Zhang@Sun.COM * an arcbuf for a partial block read 487111539SChunli.Zhang@Sun.COM */ 487211539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10); /* 1K */ 487311539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17); /* 128K */ 487411539SChunli.Zhang@Sun.COM 487511539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 487611539SChunli.Zhang@Sun.COM static int 487711539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 487811539SChunli.Zhang@Sun.COM caller_context_t *ct) 487911539SChunli.Zhang@Sun.COM { 488011539SChunli.Zhang@Sun.COM znode_t *zp = VTOZ(vp); 488111539SChunli.Zhang@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 488211539SChunli.Zhang@Sun.COM int max_blksz = zfsvfs->z_max_blksz; 488311539SChunli.Zhang@Sun.COM uio_t *uio = &xuio->xu_uio; 488411539SChunli.Zhang@Sun.COM ssize_t size = uio->uio_resid; 488511539SChunli.Zhang@Sun.COM offset_t offset = uio->uio_loffset; 488611539SChunli.Zhang@Sun.COM int blksz; 488711539SChunli.Zhang@Sun.COM int fullblk, i; 488811539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 488911539SChunli.Zhang@Sun.COM ssize_t maxsize; 489011539SChunli.Zhang@Sun.COM int preamble, postamble; 489111539SChunli.Zhang@Sun.COM 489211539SChunli.Zhang@Sun.COM if (xuio->xu_type != UIOTYPE_ZEROCOPY) 489311539SChunli.Zhang@Sun.COM return (EINVAL); 489411539SChunli.Zhang@Sun.COM 489511539SChunli.Zhang@Sun.COM ZFS_ENTER(zfsvfs); 489611539SChunli.Zhang@Sun.COM ZFS_VERIFY_ZP(zp); 489711539SChunli.Zhang@Sun.COM switch (ioflag) { 489811539SChunli.Zhang@Sun.COM case UIO_WRITE: 489911539SChunli.Zhang@Sun.COM /* 490011539SChunli.Zhang@Sun.COM * Loan out an arc_buf for write if write size is bigger than 490111539SChunli.Zhang@Sun.COM * max_blksz, and the file's block size is also max_blksz. 490211539SChunli.Zhang@Sun.COM */ 490311539SChunli.Zhang@Sun.COM blksz = max_blksz; 490411539SChunli.Zhang@Sun.COM if (size < blksz || zp->z_blksz != blksz) { 490511539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 490611539SChunli.Zhang@Sun.COM return (EINVAL); 490711539SChunli.Zhang@Sun.COM } 490811539SChunli.Zhang@Sun.COM /* 490911539SChunli.Zhang@Sun.COM * Caller requests buffers for write before knowing where the 491011539SChunli.Zhang@Sun.COM * write offset might be (e.g. NFS TCP write). 491111539SChunli.Zhang@Sun.COM */ 491211539SChunli.Zhang@Sun.COM if (offset == -1) { 491311539SChunli.Zhang@Sun.COM preamble = 0; 491411539SChunli.Zhang@Sun.COM } else { 491511539SChunli.Zhang@Sun.COM preamble = P2PHASE(offset, blksz); 491611539SChunli.Zhang@Sun.COM if (preamble) { 491711539SChunli.Zhang@Sun.COM preamble = blksz - preamble; 491811539SChunli.Zhang@Sun.COM size -= preamble; 491911539SChunli.Zhang@Sun.COM } 492011539SChunli.Zhang@Sun.COM } 492111539SChunli.Zhang@Sun.COM 492211539SChunli.Zhang@Sun.COM postamble = P2PHASE(size, blksz); 492311539SChunli.Zhang@Sun.COM size -= postamble; 492411539SChunli.Zhang@Sun.COM 492511539SChunli.Zhang@Sun.COM fullblk = size / blksz; 492611576SSurya.Prakki@Sun.COM (void) dmu_xuio_init(xuio, 492711539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 492811539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 492911539SChunli.Zhang@Sun.COM int, postamble, int, 493011539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 493111539SChunli.Zhang@Sun.COM 493211539SChunli.Zhang@Sun.COM /* 493311539SChunli.Zhang@Sun.COM * Have to fix iov base/len for partial buffers. They 493411539SChunli.Zhang@Sun.COM * currently represent full arc_buf's. 493511539SChunli.Zhang@Sun.COM */ 493611539SChunli.Zhang@Sun.COM if (preamble) { 493711539SChunli.Zhang@Sun.COM /* data begins in the middle of the arc_buf */ 493811935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 493911935SMark.Shellenbaum@Sun.COM blksz); 494011539SChunli.Zhang@Sun.COM ASSERT(abuf); 494111576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 494211576SSurya.Prakki@Sun.COM blksz - preamble, preamble); 494311539SChunli.Zhang@Sun.COM } 494411539SChunli.Zhang@Sun.COM 494511539SChunli.Zhang@Sun.COM for (i = 0; i < fullblk; i++) { 494611935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 494711935SMark.Shellenbaum@Sun.COM blksz); 494811539SChunli.Zhang@Sun.COM ASSERT(abuf); 494911576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, blksz); 495011539SChunli.Zhang@Sun.COM } 495111539SChunli.Zhang@Sun.COM 495211539SChunli.Zhang@Sun.COM if (postamble) { 495311539SChunli.Zhang@Sun.COM /* data ends in the middle of the arc_buf */ 495411935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 495511935SMark.Shellenbaum@Sun.COM blksz); 495611539SChunli.Zhang@Sun.COM ASSERT(abuf); 495711576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, postamble); 495811539SChunli.Zhang@Sun.COM } 495911539SChunli.Zhang@Sun.COM break; 496011539SChunli.Zhang@Sun.COM case UIO_READ: 496111539SChunli.Zhang@Sun.COM /* 496211539SChunli.Zhang@Sun.COM * Loan out an arc_buf for read if the read size is larger than 496311539SChunli.Zhang@Sun.COM * the current file block size. Block alignment is not 496411539SChunli.Zhang@Sun.COM * considered. Partial arc_buf will be loaned out for read. 496511539SChunli.Zhang@Sun.COM */ 496611539SChunli.Zhang@Sun.COM blksz = zp->z_blksz; 496711539SChunli.Zhang@Sun.COM if (blksz < zcr_blksz_min) 496811539SChunli.Zhang@Sun.COM blksz = zcr_blksz_min; 496911539SChunli.Zhang@Sun.COM if (blksz > zcr_blksz_max) 497011539SChunli.Zhang@Sun.COM blksz = zcr_blksz_max; 497111539SChunli.Zhang@Sun.COM /* avoid potential complexity of dealing with it */ 497211539SChunli.Zhang@Sun.COM if (blksz > max_blksz) { 497311539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 497411539SChunli.Zhang@Sun.COM return (EINVAL); 497511539SChunli.Zhang@Sun.COM } 497611539SChunli.Zhang@Sun.COM 497711935SMark.Shellenbaum@Sun.COM maxsize = zp->z_size - uio->uio_loffset; 497811539SChunli.Zhang@Sun.COM if (size > maxsize) 497911539SChunli.Zhang@Sun.COM size = maxsize; 498011539SChunli.Zhang@Sun.COM 498111539SChunli.Zhang@Sun.COM if (size < blksz || vn_has_cached_data(vp)) { 498211539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 498311539SChunli.Zhang@Sun.COM return (EINVAL); 498411539SChunli.Zhang@Sun.COM } 498511539SChunli.Zhang@Sun.COM break; 498611539SChunli.Zhang@Sun.COM default: 498711539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 498811539SChunli.Zhang@Sun.COM return (EINVAL); 498911539SChunli.Zhang@Sun.COM } 499011539SChunli.Zhang@Sun.COM 499111539SChunli.Zhang@Sun.COM uio->uio_extflg = UIO_XUIO; 499211539SChunli.Zhang@Sun.COM XUIO_XUZC_RW(xuio) = ioflag; 499311539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 499411539SChunli.Zhang@Sun.COM return (0); 499511539SChunli.Zhang@Sun.COM } 499611539SChunli.Zhang@Sun.COM 499711539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 499811539SChunli.Zhang@Sun.COM static int 499911539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 500011539SChunli.Zhang@Sun.COM { 500111539SChunli.Zhang@Sun.COM int i; 500211539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 500311539SChunli.Zhang@Sun.COM int ioflag = XUIO_XUZC_RW(xuio); 500411539SChunli.Zhang@Sun.COM 500511539SChunli.Zhang@Sun.COM ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 500611539SChunli.Zhang@Sun.COM 500711539SChunli.Zhang@Sun.COM i = dmu_xuio_cnt(xuio); 500811539SChunli.Zhang@Sun.COM while (i-- > 0) { 500911539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i); 501011539SChunli.Zhang@Sun.COM /* 501111539SChunli.Zhang@Sun.COM * if abuf == NULL, it must be a write buffer 501211539SChunli.Zhang@Sun.COM * that has been returned in zfs_write(). 501311539SChunli.Zhang@Sun.COM */ 501411539SChunli.Zhang@Sun.COM if (abuf) 501511539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 501611539SChunli.Zhang@Sun.COM ASSERT(abuf || ioflag == UIO_WRITE); 501711539SChunli.Zhang@Sun.COM } 501811539SChunli.Zhang@Sun.COM 501911539SChunli.Zhang@Sun.COM dmu_xuio_fini(xuio); 502011539SChunli.Zhang@Sun.COM return (0); 502111539SChunli.Zhang@Sun.COM } 502211539SChunli.Zhang@Sun.COM 502311539SChunli.Zhang@Sun.COM /* 5024789Sahrens * Predeclare these here so that the compiler assumes that 5025789Sahrens * this is an "old style" function declaration that does 5026789Sahrens * not include arguments => we won't get type mismatch errors 5027789Sahrens * in the initializations that follow. 5028789Sahrens */ 5029789Sahrens static int zfs_inval(); 5030789Sahrens static int zfs_isdir(); 5031789Sahrens 5032789Sahrens static int 5033789Sahrens zfs_inval() 5034789Sahrens { 5035789Sahrens return (EINVAL); 5036789Sahrens } 5037789Sahrens 5038789Sahrens static int 5039789Sahrens zfs_isdir() 5040789Sahrens { 5041789Sahrens return (EISDIR); 5042789Sahrens } 5043789Sahrens /* 5044789Sahrens * Directory vnode operations template 5045789Sahrens */ 5046789Sahrens vnodeops_t *zfs_dvnodeops; 5047789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 50483898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50493898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50503898Srsb VOPNAME_READ, { .error = zfs_isdir }, 50513898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 50523898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50533898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50543898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50553898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 50563898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 50573898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 50583898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 50593898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 50603898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 50613898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 50623898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 50633898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 50643898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 50653898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 50663898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 50673898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 50683898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 50693898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 50703898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 50713898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 50724863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 50733898Srsb NULL, NULL 5074789Sahrens }; 5075789Sahrens 5076789Sahrens /* 5077789Sahrens * Regular file vnode operations template 5078789Sahrens */ 5079789Sahrens vnodeops_t *zfs_fvnodeops; 5080789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 50813898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50823898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50833898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 50843898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 50853898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50863898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50873898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50883898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 50893898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 50903898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 50913898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 50923898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 50933898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 50943898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 50953898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 50963898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 50973898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 50983898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 50993898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 51003898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 51013898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 51023898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51033898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51043898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51053898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 510611539SChunli.Zhang@Sun.COM VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 510711539SChunli.Zhang@Sun.COM VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 51083898Srsb NULL, NULL 5109789Sahrens }; 5110789Sahrens 5111789Sahrens /* 5112789Sahrens * Symbolic link vnode operations template 5113789Sahrens */ 5114789Sahrens vnodeops_t *zfs_symvnodeops; 5115789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 51163898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51173898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51183898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51193898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51203898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 51213898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51223898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51233898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51243898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51253898Srsb NULL, NULL 5126789Sahrens }; 5127789Sahrens 5128789Sahrens /* 51298845Samw@Sun.COM * special share hidden files vnode operations template 51308845Samw@Sun.COM */ 51318845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 51328845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 51338845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51348845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 51358845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51368845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 51378845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51388845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51398845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51408845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51418845Samw@Sun.COM NULL, NULL 51428845Samw@Sun.COM }; 51438845Samw@Sun.COM 51448845Samw@Sun.COM /* 5145789Sahrens * Extended attribute directory vnode operations template 5146789Sahrens * This template is identical to the directory vnodes 5147789Sahrens * operation template except for restricted operations: 5148789Sahrens * VOP_MKDIR() 5149789Sahrens * VOP_SYMLINK() 5150789Sahrens * Note that there are other restrictions embedded in: 5151789Sahrens * zfs_create() - restrict type to VREG 5152789Sahrens * zfs_link() - no links into/out of attribute space 5153789Sahrens * zfs_rename() - no moves into/out of attribute space 5154789Sahrens */ 5155789Sahrens vnodeops_t *zfs_xdvnodeops; 5156789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 51573898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 51583898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 51593898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 51603898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51613898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51623898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51633898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 51643898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 51653898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 51663898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 51673898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51683898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 51693898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 51703898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 51713898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 51723898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 51733898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51743898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51753898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 51763898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51773898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51783898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51793898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51803898Srsb NULL, NULL 5181789Sahrens }; 5182789Sahrens 5183789Sahrens /* 5184789Sahrens * Error vnode operations template 5185789Sahrens */ 5186789Sahrens vnodeops_t *zfs_evnodeops; 5187789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 51883898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51893898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51903898Srsb NULL, NULL 5191789Sahrens }; 5192