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 * 13512699SNeil.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 16712699SNeil.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) 49312699SNeil.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 67312829SSanjeev.Bagewadi@Sun.COM uio_prefaultpages(MIN(n, max_blksz), 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 } 89612772SNeil.Perrin@Sun.COM /* 89712772SNeil.Perrin@Sun.COM * If we are replaying and eof is non zero then force 89812772SNeil.Perrin@Sun.COM * the file size to the specified eof. Note, there's no 89912772SNeil.Perrin@Sun.COM * concurrency during replay. 90012772SNeil.Perrin@Sun.COM */ 90112772SNeil.Perrin@Sun.COM if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 90212772SNeil.Perrin@Sun.COM zp->z_size = zfsvfs->z_replay_eof; 90312772SNeil.Perrin@Sun.COM 90411935SMark.Shellenbaum@Sun.COM error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 90511935SMark.Shellenbaum@Sun.COM 9063638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 9073638Sbillm dmu_tx_commit(tx); 9083638Sbillm 9093638Sbillm if (error != 0) 9103638Sbillm break; 9113638Sbillm ASSERT(tx_bytes == nbytes); 9123638Sbillm n -= nbytes; 91312829SSanjeev.Bagewadi@Sun.COM 91412829SSanjeev.Bagewadi@Sun.COM if (!xuio && n > 0) 91512829SSanjeev.Bagewadi@Sun.COM uio_prefaultpages(MIN(n, max_blksz), uio); 916789Sahrens } 917789Sahrens 9182237Smaybee zfs_range_unlock(rl); 919789Sahrens 920789Sahrens /* 921789Sahrens * If we're in replay mode, or we made no progress, return error. 922789Sahrens * Otherwise, it's at least a partial write, so it's successful. 923789Sahrens */ 9248227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 925789Sahrens ZFS_EXIT(zfsvfs); 926789Sahrens return (error); 927789Sahrens } 928789Sahrens 92912294SMark.Musante@Sun.COM if (ioflag & (FSYNC | FDSYNC) || 93012294SMark.Musante@Sun.COM zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 93112699SNeil.Perrin@Sun.COM zil_commit(zilog, zp->z_id); 932789Sahrens 933789Sahrens ZFS_EXIT(zfsvfs); 934789Sahrens return (0); 935789Sahrens } 936789Sahrens 9372237Smaybee void 93810922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error) 9392237Smaybee { 94010922SJeff.Bonwick@Sun.COM znode_t *zp = zgd->zgd_private; 94110922SJeff.Bonwick@Sun.COM objset_t *os = zp->z_zfsvfs->z_os; 94210922SJeff.Bonwick@Sun.COM 94310922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 94410922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 94510922SJeff.Bonwick@Sun.COM 94610922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 94710922SJeff.Bonwick@Sun.COM 9489321SNeil.Perrin@Sun.COM /* 9499321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9509321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9519321SNeil.Perrin@Sun.COM */ 95210922SJeff.Bonwick@Sun.COM VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 95310922SJeff.Bonwick@Sun.COM 95410922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 95510922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 95610922SJeff.Bonwick@Sun.COM 9573063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9582237Smaybee } 9592237Smaybee 96010209SMark.Musante@Sun.COM #ifdef DEBUG 96110209SMark.Musante@Sun.COM static int zil_fault_io = 0; 96210209SMark.Musante@Sun.COM #endif 96310209SMark.Musante@Sun.COM 964789Sahrens /* 965789Sahrens * Get data to generate a TX_WRITE intent log record. 966789Sahrens */ 967789Sahrens int 9682237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 969789Sahrens { 970789Sahrens zfsvfs_t *zfsvfs = arg; 971789Sahrens objset_t *os = zfsvfs->z_os; 972789Sahrens znode_t *zp; 97310922SJeff.Bonwick@Sun.COM uint64_t object = lr->lr_foid; 97410922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 97510922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; 97610922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9772237Smaybee dmu_buf_t *db; 9783063Sperrin zgd_t *zgd; 979789Sahrens int error = 0; 980789Sahrens 98110922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 98210922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 983789Sahrens 984789Sahrens /* 9851669Sperrin * Nothing to do if the file has been removed 986789Sahrens */ 98710922SJeff.Bonwick@Sun.COM if (zfs_zget(zfsvfs, object, &zp) != 0) 988789Sahrens return (ENOENT); 9893461Sahrens if (zp->z_unlinked) { 9909321SNeil.Perrin@Sun.COM /* 9919321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9929321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9939321SNeil.Perrin@Sun.COM */ 9949321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), 9959321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 996789Sahrens return (ENOENT); 997789Sahrens } 998789Sahrens 99910922SJeff.Bonwick@Sun.COM zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 100010922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zfsvfs->z_log; 100110922SJeff.Bonwick@Sun.COM zgd->zgd_private = zp; 100210922SJeff.Bonwick@Sun.COM 1003789Sahrens /* 1004789Sahrens * Write records come in two flavors: immediate and indirect. 1005789Sahrens * For small writes it's cheaper to store the data with the 1006789Sahrens * log record (immediate); for large writes it's cheaper to 1007789Sahrens * sync the data and get a pointer to it (indirect) so that 1008789Sahrens * we don't have to write the data twice. 1009789Sahrens */ 10101669Sperrin if (buf != NULL) { /* immediate write */ 101110922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 10121669Sperrin /* test for truncation needs to be done while range locked */ 101311935SMark.Shellenbaum@Sun.COM if (offset >= zp->z_size) { 10141669Sperrin error = ENOENT; 101510922SJeff.Bonwick@Sun.COM } else { 101610922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 101710922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 10181669Sperrin } 101910922SJeff.Bonwick@Sun.COM ASSERT(error == 0 || error == ENOENT); 10201669Sperrin } else { /* indirect write */ 1021789Sahrens /* 10221669Sperrin * Have to lock the whole block to ensure when it's 10231669Sperrin * written out and it's checksum is being calculated 10241669Sperrin * that no one can change the data. We need to re-check 10251669Sperrin * blocksize after we get the lock in case it's changed! 1026789Sahrens */ 10271669Sperrin for (;;) { 102810922SJeff.Bonwick@Sun.COM uint64_t blkoff; 102910922SJeff.Bonwick@Sun.COM size = zp->z_blksz; 103010945SJeff.Bonwick@Sun.COM blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 103110922SJeff.Bonwick@Sun.COM offset -= blkoff; 103210922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, 103310922SJeff.Bonwick@Sun.COM RL_READER); 103410922SJeff.Bonwick@Sun.COM if (zp->z_blksz == size) 10351669Sperrin break; 103610922SJeff.Bonwick@Sun.COM offset += blkoff; 103710922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 10381669Sperrin } 10391669Sperrin /* test for truncation needs to be done while range locked */ 104011935SMark.Shellenbaum@Sun.COM if (lr->lr_offset >= zp->z_size) 10411669Sperrin error = ENOENT; 104210209SMark.Musante@Sun.COM #ifdef DEBUG 104310209SMark.Musante@Sun.COM if (zil_fault_io) { 104410209SMark.Musante@Sun.COM error = EIO; 104510209SMark.Musante@Sun.COM zil_fault_io = 0; 104610209SMark.Musante@Sun.COM } 104710209SMark.Musante@Sun.COM #endif 104810922SJeff.Bonwick@Sun.COM if (error == 0) 104912285SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db, 105012285SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 105110922SJeff.Bonwick@Sun.COM 105210800SNeil.Perrin@Sun.COM if (error == 0) { 105310922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 105410922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 105510922SJeff.Bonwick@Sun.COM 105610922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 105710922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 105810922SJeff.Bonwick@Sun.COM 105910922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 106010922SJeff.Bonwick@Sun.COM zfs_get_done, zgd); 106110922SJeff.Bonwick@Sun.COM ASSERT(error || lr->lr_length <= zp->z_blksz); 106210922SJeff.Bonwick@Sun.COM 106310800SNeil.Perrin@Sun.COM /* 106410922SJeff.Bonwick@Sun.COM * On success, we need to wait for the write I/O 106510922SJeff.Bonwick@Sun.COM * initiated by dmu_sync() to complete before we can 106610922SJeff.Bonwick@Sun.COM * release this dbuf. We will finish everything up 106710922SJeff.Bonwick@Sun.COM * in the zfs_get_done() callback. 106810800SNeil.Perrin@Sun.COM */ 106910922SJeff.Bonwick@Sun.COM if (error == 0) 107010922SJeff.Bonwick@Sun.COM return (0); 107110922SJeff.Bonwick@Sun.COM 107210922SJeff.Bonwick@Sun.COM if (error == EALREADY) { 107310922SJeff.Bonwick@Sun.COM lr->lr_common.lrc_txtype = TX_WRITE2; 107410922SJeff.Bonwick@Sun.COM error = 0; 107510922SJeff.Bonwick@Sun.COM } 107610800SNeil.Perrin@Sun.COM } 1077789Sahrens } 107810922SJeff.Bonwick@Sun.COM 107910922SJeff.Bonwick@Sun.COM zfs_get_done(zgd, error); 108010922SJeff.Bonwick@Sun.COM 1081789Sahrens return (error); 1082789Sahrens } 1083789Sahrens 1084789Sahrens /*ARGSUSED*/ 1085789Sahrens static int 10865331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 10875331Samw caller_context_t *ct) 1088789Sahrens { 1089789Sahrens znode_t *zp = VTOZ(vp); 1090789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1091789Sahrens int error; 1092789Sahrens 10935367Sahrens ZFS_ENTER(zfsvfs); 10945367Sahrens ZFS_VERIFY_ZP(zp); 10955331Samw 10965331Samw if (flag & V_ACE_MASK) 10975331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 10985331Samw else 10995331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 11005331Samw 1101789Sahrens ZFS_EXIT(zfsvfs); 1102789Sahrens return (error); 1103789Sahrens } 1104789Sahrens 1105789Sahrens /* 11069981STim.Haley@Sun.COM * If vnode is for a device return a specfs vnode instead. 11079981STim.Haley@Sun.COM */ 11089981STim.Haley@Sun.COM static int 11099981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr) 11109981STim.Haley@Sun.COM { 11119981STim.Haley@Sun.COM int error = 0; 11129981STim.Haley@Sun.COM 11139981STim.Haley@Sun.COM if (IS_DEVVP(*vpp)) { 11149981STim.Haley@Sun.COM struct vnode *svp; 11159981STim.Haley@Sun.COM 11169981STim.Haley@Sun.COM svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 11179981STim.Haley@Sun.COM VN_RELE(*vpp); 11189981STim.Haley@Sun.COM if (svp == NULL) 11199981STim.Haley@Sun.COM error = ENOSYS; 11209981STim.Haley@Sun.COM *vpp = svp; 11219981STim.Haley@Sun.COM } 11229981STim.Haley@Sun.COM return (error); 11239981STim.Haley@Sun.COM } 11249981STim.Haley@Sun.COM 11259981STim.Haley@Sun.COM 11269981STim.Haley@Sun.COM /* 1127789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1128789Sahrens * If it exists, return a held vnode reference for it. 1129789Sahrens * 1130789Sahrens * IN: dvp - vnode of directory to search. 1131789Sahrens * nm - name of entry to lookup. 1132789Sahrens * pnp - full pathname to lookup [UNUSED]. 1133789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1134789Sahrens * rdir - root directory vnode [UNUSED]. 1135789Sahrens * cr - credentials of caller. 11365331Samw * ct - caller context 11375331Samw * direntflags - directory lookup flags 11385331Samw * realpnp - returned pathname. 1139789Sahrens * 1140789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1141789Sahrens * 1142789Sahrens * RETURN: 0 if success 1143789Sahrens * error code if failure 1144789Sahrens * 1145789Sahrens * Timestamps: 1146789Sahrens * NA 1147789Sahrens */ 1148789Sahrens /* ARGSUSED */ 1149789Sahrens static int 1150789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 11515331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 11525331Samw int *direntflags, pathname_t *realpnp) 1153789Sahrens { 1154789Sahrens znode_t *zdp = VTOZ(dvp); 1155789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 11569981STim.Haley@Sun.COM int error = 0; 11579981STim.Haley@Sun.COM 11589981STim.Haley@Sun.COM /* fast path */ 11599981STim.Haley@Sun.COM if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 11609981STim.Haley@Sun.COM 11619981STim.Haley@Sun.COM if (dvp->v_type != VDIR) { 11629981STim.Haley@Sun.COM return (ENOTDIR); 116311935SMark.Shellenbaum@Sun.COM } else if (zdp->z_sa_hdl == NULL) { 11649981STim.Haley@Sun.COM return (EIO); 11659981STim.Haley@Sun.COM } 11669981STim.Haley@Sun.COM 11679981STim.Haley@Sun.COM if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 11689981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11699981STim.Haley@Sun.COM if (!error) { 11709981STim.Haley@Sun.COM *vpp = dvp; 11719981STim.Haley@Sun.COM VN_HOLD(*vpp); 11729981STim.Haley@Sun.COM return (0); 11739981STim.Haley@Sun.COM } 11749981STim.Haley@Sun.COM return (error); 11759981STim.Haley@Sun.COM } else { 11769981STim.Haley@Sun.COM vnode_t *tvp = dnlc_lookup(dvp, nm); 11779981STim.Haley@Sun.COM 11789981STim.Haley@Sun.COM if (tvp) { 11799981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11809981STim.Haley@Sun.COM if (error) { 11819981STim.Haley@Sun.COM VN_RELE(tvp); 11829981STim.Haley@Sun.COM return (error); 11839981STim.Haley@Sun.COM } 11849981STim.Haley@Sun.COM if (tvp == DNLC_NO_VNODE) { 11859981STim.Haley@Sun.COM VN_RELE(tvp); 11869981STim.Haley@Sun.COM return (ENOENT); 11879981STim.Haley@Sun.COM } else { 11889981STim.Haley@Sun.COM *vpp = tvp; 11899981STim.Haley@Sun.COM return (specvp_check(vpp, cr)); 11909981STim.Haley@Sun.COM } 11919981STim.Haley@Sun.COM } 11929981STim.Haley@Sun.COM } 11939981STim.Haley@Sun.COM } 11949981STim.Haley@Sun.COM 11959981STim.Haley@Sun.COM DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1196789Sahrens 11975367Sahrens ZFS_ENTER(zfsvfs); 11985367Sahrens ZFS_VERIFY_ZP(zdp); 1199789Sahrens 1200789Sahrens *vpp = NULL; 1201789Sahrens 1202789Sahrens if (flags & LOOKUP_XATTR) { 1203789Sahrens /* 12043234Sck153898 * If the xattr property is off, refuse the lookup request. 12053234Sck153898 */ 12063234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 12073234Sck153898 ZFS_EXIT(zfsvfs); 12083234Sck153898 return (EINVAL); 12093234Sck153898 } 12103234Sck153898 12113234Sck153898 /* 1212789Sahrens * We don't allow recursive attributes.. 1213789Sahrens * Maybe someday we will. 1214789Sahrens */ 121511935SMark.Shellenbaum@Sun.COM if (zdp->z_pflags & ZFS_XATTR) { 1216789Sahrens ZFS_EXIT(zfsvfs); 1217789Sahrens return (EINVAL); 1218789Sahrens } 1219789Sahrens 12203280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1221789Sahrens ZFS_EXIT(zfsvfs); 1222789Sahrens return (error); 1223789Sahrens } 1224789Sahrens 1225789Sahrens /* 1226789Sahrens * Do we have permission to get into attribute directory? 1227789Sahrens */ 1228789Sahrens 12295331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 12305331Samw B_FALSE, cr)) { 1231789Sahrens VN_RELE(*vpp); 12325331Samw *vpp = NULL; 1233789Sahrens } 1234789Sahrens 1235789Sahrens ZFS_EXIT(zfsvfs); 1236789Sahrens return (error); 1237789Sahrens } 1238789Sahrens 12391512Sek110237 if (dvp->v_type != VDIR) { 12401512Sek110237 ZFS_EXIT(zfsvfs); 12411460Smarks return (ENOTDIR); 12421512Sek110237 } 12431460Smarks 1244789Sahrens /* 1245789Sahrens * Check accessibility of directory. 1246789Sahrens */ 1247789Sahrens 12485331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1249789Sahrens ZFS_EXIT(zfsvfs); 1250789Sahrens return (error); 1251789Sahrens } 1252789Sahrens 12535498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 12545331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 12555331Samw ZFS_EXIT(zfsvfs); 12565331Samw return (EILSEQ); 12575331Samw } 12585331Samw 12595331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 12609981STim.Haley@Sun.COM if (error == 0) 12619981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1262789Sahrens 1263789Sahrens ZFS_EXIT(zfsvfs); 1264789Sahrens return (error); 1265789Sahrens } 1266789Sahrens 1267789Sahrens /* 1268789Sahrens * Attempt to create a new entry in a directory. If the entry 1269789Sahrens * already exists, truncate the file if permissible, else return 1270789Sahrens * an error. Return the vp of the created or trunc'd file. 1271789Sahrens * 1272789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1273789Sahrens * name - name of new file entry. 1274789Sahrens * vap - attributes of new file. 1275789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1276789Sahrens * mode - mode to open file with. 1277789Sahrens * cr - credentials of caller. 1278789Sahrens * flag - large file flag [UNUSED]. 12795331Samw * ct - caller context 12805331Samw * vsecp - ACL to be set 1281789Sahrens * 1282789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1283789Sahrens * 1284789Sahrens * RETURN: 0 if success 1285789Sahrens * error code if failure 1286789Sahrens * 1287789Sahrens * Timestamps: 1288789Sahrens * dvp - ctime|mtime updated if new entry created 1289789Sahrens * vp - ctime|mtime always, atime if new 1290789Sahrens */ 12915331Samw 1292789Sahrens /* ARGSUSED */ 1293789Sahrens static int 1294789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 12955331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 12965331Samw vsecattr_t *vsecp) 1297789Sahrens { 1298789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1299789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 13005326Sek110237 zilog_t *zilog; 13015326Sek110237 objset_t *os; 1302789Sahrens zfs_dirlock_t *dl; 1303789Sahrens dmu_tx_t *tx; 1304789Sahrens int error; 13057847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 13067847SMark.Shellenbaum@Sun.COM uid_t uid; 13077847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 130811935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 13099179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 131012302SMark.Shellenbaum@Sun.COM boolean_t have_acl = B_FALSE; 13115331Samw 13125331Samw /* 13135331Samw * If we have an ephemeral id, ACL, or XVATTR then 13145331Samw * make sure file system is at proper version 13155331Samw */ 13165331Samw 13177847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 13187847SMark.Shellenbaum@Sun.COM if (ksid) 13197847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 13207847SMark.Shellenbaum@Sun.COM else 13217847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 13227847SMark.Shellenbaum@Sun.COM 13235331Samw if (zfsvfs->z_use_fuids == B_FALSE && 13245331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 13257847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 13265331Samw return (EINVAL); 1327789Sahrens 13285367Sahrens ZFS_ENTER(zfsvfs); 13295367Sahrens ZFS_VERIFY_ZP(dzp); 13305326Sek110237 os = zfsvfs->z_os; 13315326Sek110237 zilog = zfsvfs->z_log; 1332789Sahrens 13335498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 13345331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 13355331Samw ZFS_EXIT(zfsvfs); 13365331Samw return (EILSEQ); 13375331Samw } 13385331Samw 13395331Samw if (vap->va_mask & AT_XVATTR) { 13405331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 13415331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 13425331Samw ZFS_EXIT(zfsvfs); 13435331Samw return (error); 13445331Samw } 13455331Samw } 1346789Sahrens top: 1347789Sahrens *vpp = NULL; 1348789Sahrens 1349789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1350789Sahrens vap->va_mode &= ~VSVTX; 1351789Sahrens 1352789Sahrens if (*name == '\0') { 1353789Sahrens /* 1354789Sahrens * Null component name refers to the directory itself. 1355789Sahrens */ 1356789Sahrens VN_HOLD(dvp); 1357789Sahrens zp = dzp; 1358789Sahrens dl = NULL; 1359789Sahrens error = 0; 1360789Sahrens } else { 1361789Sahrens /* possible VN_HOLD(zp) */ 13625331Samw int zflg = 0; 13635331Samw 13645331Samw if (flag & FIGNORECASE) 13655331Samw zflg |= ZCILOOK; 13665331Samw 13675331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13685331Samw NULL, NULL); 13695331Samw if (error) { 1370789Sahrens if (strcmp(name, "..") == 0) 1371789Sahrens error = EISDIR; 1372789Sahrens ZFS_EXIT(zfsvfs); 1373789Sahrens return (error); 1374789Sahrens } 1375789Sahrens } 137611935SMark.Shellenbaum@Sun.COM 1377789Sahrens if (zp == NULL) { 13785331Samw uint64_t txtype; 13795331Samw 1380789Sahrens /* 1381789Sahrens * Create a new file object and update the directory 1382789Sahrens * to reference it. 1383789Sahrens */ 13845331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1385789Sahrens goto out; 1386789Sahrens } 1387789Sahrens 1388789Sahrens /* 1389789Sahrens * We only support the creation of regular files in 1390789Sahrens * extended attribute directories. 1391789Sahrens */ 139211935SMark.Shellenbaum@Sun.COM 139311935SMark.Shellenbaum@Sun.COM if ((dzp->z_pflags & ZFS_XATTR) && 1394789Sahrens (vap->va_type != VREG)) { 1395789Sahrens error = EINVAL; 1396789Sahrens goto out; 1397789Sahrens } 1398789Sahrens 139912302SMark.Shellenbaum@Sun.COM if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 140012302SMark.Shellenbaum@Sun.COM cr, vsecp, &acl_ids)) != 0) 14019179SMark.Shellenbaum@Sun.COM goto out; 140212302SMark.Shellenbaum@Sun.COM have_acl = B_TRUE; 140312302SMark.Shellenbaum@Sun.COM 14049396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 140510143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 14069396SMatthew.Ahrens@Sun.COM error = EDQUOT; 14079396SMatthew.Ahrens@Sun.COM goto out; 14089396SMatthew.Ahrens@Sun.COM } 14099179SMark.Shellenbaum@Sun.COM 1410789Sahrens tx = dmu_tx_create(os); 141111935SMark.Shellenbaum@Sun.COM 141211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 141311935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 141411935SMark.Shellenbaum@Sun.COM 14159179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 14169396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 14179396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 14181544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 141911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 142011935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && 142111935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1422789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 142311935SMark.Shellenbaum@Sun.COM 0, acl_ids.z_aclp->z_acl_bytes); 14245331Samw } 14258227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1426789Sahrens if (error) { 1427789Sahrens zfs_dirent_unlock(dl); 14288227SNeil.Perrin@Sun.COM if (error == ERESTART) { 14292113Sahrens dmu_tx_wait(tx); 14302113Sahrens dmu_tx_abort(tx); 1431789Sahrens goto top; 1432789Sahrens } 143312302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 14342113Sahrens dmu_tx_abort(tx); 1435789Sahrens ZFS_EXIT(zfsvfs); 1436789Sahrens return (error); 1437789Sahrens } 143811935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 14399179SMark.Shellenbaum@Sun.COM 14409179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 14419179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 14429179SMark.Shellenbaum@Sun.COM 1443789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 14445331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 14455331Samw if (flag & FIGNORECASE) 14465331Samw txtype |= TX_CI; 14475331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 14489179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 14499179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1450789Sahrens dmu_tx_commit(tx); 1451789Sahrens } else { 14525331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 14535331Samw 1454789Sahrens /* 1455789Sahrens * A directory entry already exists for this name. 1456789Sahrens */ 1457789Sahrens /* 1458789Sahrens * Can't truncate an existing file if in exclusive mode. 1459789Sahrens */ 1460789Sahrens if (excl == EXCL) { 1461789Sahrens error = EEXIST; 1462789Sahrens goto out; 1463789Sahrens } 1464789Sahrens /* 1465789Sahrens * Can't open a directory for writing. 1466789Sahrens */ 1467789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1468789Sahrens error = EISDIR; 1469789Sahrens goto out; 1470789Sahrens } 1471789Sahrens /* 1472789Sahrens * Verify requested access to file. 1473789Sahrens */ 14745331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1475789Sahrens goto out; 1476789Sahrens } 1477789Sahrens 1478789Sahrens mutex_enter(&dzp->z_lock); 1479789Sahrens dzp->z_seq++; 1480789Sahrens mutex_exit(&dzp->z_lock); 1481789Sahrens 14821878Smaybee /* 14831878Smaybee * Truncate regular files if requested. 14841878Smaybee */ 14851878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1486789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 14876992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 14886992Smaybee zfs_dirent_unlock(dl); 14896992Smaybee dl = NULL; 14901878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 14914863Spraks if (error == 0) { 14925331Samw vnevent_create(ZTOV(zp), ct); 14934863Spraks } 1494789Sahrens } 1495789Sahrens } 1496789Sahrens out: 1497789Sahrens 1498789Sahrens if (dl) 1499789Sahrens zfs_dirent_unlock(dl); 1500789Sahrens 1501789Sahrens if (error) { 1502789Sahrens if (zp) 1503789Sahrens VN_RELE(ZTOV(zp)); 1504789Sahrens } else { 1505789Sahrens *vpp = ZTOV(zp); 15069981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1507789Sahrens } 1508789Sahrens 150912294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 151012699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 151112294SMark.Musante@Sun.COM 1512789Sahrens ZFS_EXIT(zfsvfs); 1513789Sahrens return (error); 1514789Sahrens } 1515789Sahrens 1516789Sahrens /* 1517789Sahrens * Remove an entry from a directory. 1518789Sahrens * 1519789Sahrens * IN: dvp - vnode of directory to remove entry from. 1520789Sahrens * name - name of entry to remove. 1521789Sahrens * cr - credentials of caller. 15225331Samw * ct - caller context 15235331Samw * flags - case flags 1524789Sahrens * 1525789Sahrens * RETURN: 0 if success 1526789Sahrens * error code if failure 1527789Sahrens * 1528789Sahrens * Timestamps: 1529789Sahrens * dvp - ctime|mtime 1530789Sahrens * vp - ctime (if nlink > 0) 1531789Sahrens */ 153211935SMark.Shellenbaum@Sun.COM 153311935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0; 153411935SMark.Shellenbaum@Sun.COM 15355331Samw /*ARGSUSED*/ 1536789Sahrens static int 15375331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 15385331Samw int flags) 1539789Sahrens { 1540789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1541789Sahrens znode_t *xzp = NULL; 1542789Sahrens vnode_t *vp; 1543789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15445326Sek110237 zilog_t *zilog; 154511935SMark.Shellenbaum@Sun.COM uint64_t acl_obj, xattr_obj = 0; 154611935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj_unlinked = 0; 154712700SNeil.Perrin@Sun.COM uint64_t obj = 0; 1548789Sahrens zfs_dirlock_t *dl; 1549789Sahrens dmu_tx_t *tx; 15503461Sahrens boolean_t may_delete_now, delete_now = FALSE; 15516992Smaybee boolean_t unlinked, toobig = FALSE; 15525331Samw uint64_t txtype; 15535331Samw pathname_t *realnmp = NULL; 15545331Samw pathname_t realnm; 1555789Sahrens int error; 15565331Samw int zflg = ZEXISTS; 1557789Sahrens 15585367Sahrens ZFS_ENTER(zfsvfs); 15595367Sahrens ZFS_VERIFY_ZP(dzp); 15605326Sek110237 zilog = zfsvfs->z_log; 1561789Sahrens 15625331Samw if (flags & FIGNORECASE) { 15635331Samw zflg |= ZCILOOK; 15645331Samw pn_alloc(&realnm); 15655331Samw realnmp = &realnm; 15665331Samw } 15675331Samw 1568789Sahrens top: 1569789Sahrens /* 1570789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1571789Sahrens */ 15725331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 15735331Samw NULL, realnmp)) { 15745331Samw if (realnmp) 15755331Samw pn_free(realnmp); 1576789Sahrens ZFS_EXIT(zfsvfs); 1577789Sahrens return (error); 1578789Sahrens } 1579789Sahrens 1580789Sahrens vp = ZTOV(zp); 1581789Sahrens 1582789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1583789Sahrens goto out; 1584789Sahrens } 1585789Sahrens 1586789Sahrens /* 1587789Sahrens * Need to use rmdir for removing directories. 1588789Sahrens */ 1589789Sahrens if (vp->v_type == VDIR) { 1590789Sahrens error = EPERM; 1591789Sahrens goto out; 1592789Sahrens } 1593789Sahrens 15945331Samw vnevent_remove(vp, dvp, name, ct); 15955331Samw 15965331Samw if (realnmp) 15976492Stimh dnlc_remove(dvp, realnmp->pn_buf); 15985331Samw else 15995331Samw dnlc_remove(dvp, name); 16001484Sek110237 1601789Sahrens mutex_enter(&vp->v_lock); 1602789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1603789Sahrens mutex_exit(&vp->v_lock); 1604789Sahrens 1605789Sahrens /* 16063461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1607789Sahrens * it depends on whether we're the last link, and on whether there are 1608789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1609789Sahrens * allow for either case. 1610789Sahrens */ 161112700SNeil.Perrin@Sun.COM obj = zp->z_id; 1612789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16131544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 161411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 161511935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 161611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 16176992Smaybee if (may_delete_now) { 16186992Smaybee toobig = 161911935SMark.Shellenbaum@Sun.COM zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 16206992Smaybee /* if the file is too big, only hold_free a token amount */ 16216992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 16226992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 16236992Smaybee } 1624789Sahrens 1625789Sahrens /* are there any extended attributes? */ 162611935SMark.Shellenbaum@Sun.COM error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 162711935SMark.Shellenbaum@Sun.COM &xattr_obj, sizeof (xattr_obj)); 162811935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 162911935SMark.Shellenbaum@Sun.COM error = zfs_zget(zfsvfs, xattr_obj, &xzp); 163011935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 163111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 163211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1633789Sahrens } 1634789Sahrens 163512620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 163612620SMark.Shellenbaum@Oracle.COM if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1637789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 163812620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 1639789Sahrens 1640789Sahrens /* charge as an update -- would be nice not to charge at all */ 16413461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1642789Sahrens 16438227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1644789Sahrens if (error) { 1645789Sahrens zfs_dirent_unlock(dl); 1646789Sahrens VN_RELE(vp); 16478227SNeil.Perrin@Sun.COM if (error == ERESTART) { 16482113Sahrens dmu_tx_wait(tx); 16492113Sahrens dmu_tx_abort(tx); 1650789Sahrens goto top; 1651789Sahrens } 16525331Samw if (realnmp) 16535331Samw pn_free(realnmp); 16542113Sahrens dmu_tx_abort(tx); 1655789Sahrens ZFS_EXIT(zfsvfs); 1656789Sahrens return (error); 1657789Sahrens } 1658789Sahrens 1659789Sahrens /* 1660789Sahrens * Remove the directory entry. 1661789Sahrens */ 16625331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1663789Sahrens 1664789Sahrens if (error) { 1665789Sahrens dmu_tx_commit(tx); 1666789Sahrens goto out; 1667789Sahrens } 1668789Sahrens 16693461Sahrens if (unlinked) { 167011935SMark.Shellenbaum@Sun.COM 167112620SMark.Shellenbaum@Oracle.COM /* 167212620SMark.Shellenbaum@Oracle.COM * Hold z_lock so that we can make sure that the ACL obj 167312620SMark.Shellenbaum@Oracle.COM * hasn't changed. Could have been deleted due to 167412620SMark.Shellenbaum@Oracle.COM * zfs_sa_upgrade(). 167512620SMark.Shellenbaum@Oracle.COM */ 167612620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 1677789Sahrens mutex_enter(&vp->v_lock); 167811935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 167911935SMark.Shellenbaum@Sun.COM &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 16806992Smaybee delete_now = may_delete_now && !toobig && 1681789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 168212620SMark.Shellenbaum@Oracle.COM xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 168311935SMark.Shellenbaum@Sun.COM acl_obj; 1684789Sahrens mutex_exit(&vp->v_lock); 1685789Sahrens } 1686789Sahrens 1687789Sahrens if (delete_now) { 168811935SMark.Shellenbaum@Sun.COM if (xattr_obj_unlinked) { 168911935SMark.Shellenbaum@Sun.COM ASSERT3U(xzp->z_links, ==, 2); 1690789Sahrens mutex_enter(&xzp->z_lock); 16913461Sahrens xzp->z_unlinked = 1; 169211935SMark.Shellenbaum@Sun.COM xzp->z_links = 0; 169311935SMark.Shellenbaum@Sun.COM error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 169411935SMark.Shellenbaum@Sun.COM &xzp->z_links, sizeof (xzp->z_links), tx); 169511935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1696789Sahrens mutex_exit(&xzp->z_lock); 16973461Sahrens zfs_unlinked_add(xzp, tx); 169812620SMark.Shellenbaum@Oracle.COM 169911935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 170011935SMark.Shellenbaum@Sun.COM error = sa_remove(zp->z_sa_hdl, 170111935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), tx); 170211935SMark.Shellenbaum@Sun.COM else 170311935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, 170411935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), &null_xattr, 170511935SMark.Shellenbaum@Sun.COM sizeof (uint64_t), tx); 170611935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1707789Sahrens } 1708789Sahrens mutex_enter(&vp->v_lock); 1709789Sahrens vp->v_count--; 1710789Sahrens ASSERT3U(vp->v_count, ==, 0); 1711789Sahrens mutex_exit(&vp->v_lock); 1712789Sahrens mutex_exit(&zp->z_lock); 1713789Sahrens zfs_znode_delete(zp, tx); 17143461Sahrens } else if (unlinked) { 171512620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 17163461Sahrens zfs_unlinked_add(zp, tx); 1717789Sahrens } 1718789Sahrens 17195331Samw txtype = TX_REMOVE; 17205331Samw if (flags & FIGNORECASE) 17215331Samw txtype |= TX_CI; 172212700SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 1723789Sahrens 1724789Sahrens dmu_tx_commit(tx); 1725789Sahrens out: 17265331Samw if (realnmp) 17275331Samw pn_free(realnmp); 17285331Samw 1729789Sahrens zfs_dirent_unlock(dl); 1730789Sahrens 173112178SMark.Shellenbaum@Sun.COM if (!delete_now) 1732789Sahrens VN_RELE(vp); 173312178SMark.Shellenbaum@Sun.COM if (xzp) 1734789Sahrens VN_RELE(ZTOV(xzp)); 1735789Sahrens 173612294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 173712699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 173812294SMark.Musante@Sun.COM 1739789Sahrens ZFS_EXIT(zfsvfs); 1740789Sahrens return (error); 1741789Sahrens } 1742789Sahrens 1743789Sahrens /* 1744789Sahrens * Create a new directory and insert it into dvp using the name 1745789Sahrens * provided. Return a pointer to the inserted directory. 1746789Sahrens * 1747789Sahrens * IN: dvp - vnode of directory to add subdir to. 1748789Sahrens * dirname - name of new directory. 1749789Sahrens * vap - attributes of new directory. 1750789Sahrens * cr - credentials of caller. 17515331Samw * ct - caller context 17525331Samw * vsecp - ACL to be set 1753789Sahrens * 1754789Sahrens * OUT: vpp - vnode of created directory. 1755789Sahrens * 1756789Sahrens * RETURN: 0 if success 1757789Sahrens * error code if failure 1758789Sahrens * 1759789Sahrens * Timestamps: 1760789Sahrens * dvp - ctime|mtime updated 1761789Sahrens * vp - ctime|mtime|atime updated 1762789Sahrens */ 17635331Samw /*ARGSUSED*/ 1764789Sahrens static int 17655331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 17665331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1767789Sahrens { 1768789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1769789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17705326Sek110237 zilog_t *zilog; 1771789Sahrens zfs_dirlock_t *dl; 17725331Samw uint64_t txtype; 1773789Sahrens dmu_tx_t *tx; 1774789Sahrens int error; 17755331Samw int zf = ZNEW; 17767847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 17777847SMark.Shellenbaum@Sun.COM uid_t uid; 17787847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 177911935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 17809179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1781789Sahrens 1782789Sahrens ASSERT(vap->va_type == VDIR); 1783789Sahrens 17845331Samw /* 17855331Samw * If we have an ephemeral id, ACL, or XVATTR then 17865331Samw * make sure file system is at proper version 17875331Samw */ 17885331Samw 17897847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 17907847SMark.Shellenbaum@Sun.COM if (ksid) 17917847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 17927847SMark.Shellenbaum@Sun.COM else 17937847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 17945331Samw if (zfsvfs->z_use_fuids == B_FALSE && 17957847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 17967876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 17975331Samw return (EINVAL); 17985331Samw 17995367Sahrens ZFS_ENTER(zfsvfs); 18005367Sahrens ZFS_VERIFY_ZP(dzp); 18015326Sek110237 zilog = zfsvfs->z_log; 1802789Sahrens 180311935SMark.Shellenbaum@Sun.COM if (dzp->z_pflags & ZFS_XATTR) { 1804789Sahrens ZFS_EXIT(zfsvfs); 1805789Sahrens return (EINVAL); 1806789Sahrens } 18075331Samw 18085498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 18095331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 18105331Samw ZFS_EXIT(zfsvfs); 18115331Samw return (EILSEQ); 18125331Samw } 18135331Samw if (flags & FIGNORECASE) 18145331Samw zf |= ZCILOOK; 18155331Samw 181612302SMark.Shellenbaum@Sun.COM if (vap->va_mask & AT_XVATTR) { 18175331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 18185331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 18195331Samw ZFS_EXIT(zfsvfs); 18205331Samw return (error); 18215331Samw } 182212302SMark.Shellenbaum@Sun.COM } 182312302SMark.Shellenbaum@Sun.COM 182412302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 182512302SMark.Shellenbaum@Sun.COM vsecp, &acl_ids)) != 0) { 182612302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 182712302SMark.Shellenbaum@Sun.COM return (error); 182812302SMark.Shellenbaum@Sun.COM } 1829789Sahrens /* 1830789Sahrens * First make sure the new directory doesn't exist. 183112302SMark.Shellenbaum@Sun.COM * 183212302SMark.Shellenbaum@Sun.COM * Existence is checked first to make sure we don't return 183312302SMark.Shellenbaum@Sun.COM * EACCES instead of EEXIST which can cause some applications 183412302SMark.Shellenbaum@Sun.COM * to fail. 1835789Sahrens */ 18365331Samw top: 18375331Samw *vpp = NULL; 18385331Samw 18395331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 18405331Samw NULL, NULL)) { 184112302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1842789Sahrens ZFS_EXIT(zfsvfs); 1843789Sahrens return (error); 1844789Sahrens } 1845789Sahrens 18465331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 184712302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18481231Smarks zfs_dirent_unlock(dl); 18491231Smarks ZFS_EXIT(zfsvfs); 18501231Smarks return (error); 18511231Smarks } 18521231Smarks 18539396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 185410143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 18559396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 18569396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 18579396SMatthew.Ahrens@Sun.COM return (EDQUOT); 18589396SMatthew.Ahrens@Sun.COM } 18599179SMark.Shellenbaum@Sun.COM 1860789Sahrens /* 1861789Sahrens * Add a new entry to the directory. 1862789Sahrens */ 1863789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 18641544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 18651544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 18669179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 18679396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 18689396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 186911935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 187011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 187111935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 187211935SMark.Shellenbaum@Sun.COM } 187311935SMark.Shellenbaum@Sun.COM 187411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 187511935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 187611935SMark.Shellenbaum@Sun.COM 18778227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1878789Sahrens if (error) { 1879789Sahrens zfs_dirent_unlock(dl); 18808227SNeil.Perrin@Sun.COM if (error == ERESTART) { 18812113Sahrens dmu_tx_wait(tx); 18822113Sahrens dmu_tx_abort(tx); 1883789Sahrens goto top; 1884789Sahrens } 188512302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18862113Sahrens dmu_tx_abort(tx); 1887789Sahrens ZFS_EXIT(zfsvfs); 1888789Sahrens return (error); 1889789Sahrens } 1890789Sahrens 1891789Sahrens /* 1892789Sahrens * Create new node. 1893789Sahrens */ 189411935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 18959179SMark.Shellenbaum@Sun.COM 18969179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 18979179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 189811935SMark.Shellenbaum@Sun.COM 1899789Sahrens /* 1900789Sahrens * Now put new name in parent dir. 1901789Sahrens */ 1902789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1903789Sahrens 1904789Sahrens *vpp = ZTOV(zp); 1905789Sahrens 19065331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 19075331Samw if (flags & FIGNORECASE) 19085331Samw txtype |= TX_CI; 19099179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 19109179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 19119179SMark.Shellenbaum@Sun.COM 19129179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 191311935SMark.Shellenbaum@Sun.COM 1914789Sahrens dmu_tx_commit(tx); 1915789Sahrens 1916789Sahrens zfs_dirent_unlock(dl); 1917789Sahrens 191812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 191912699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 192012294SMark.Musante@Sun.COM 1921789Sahrens ZFS_EXIT(zfsvfs); 1922789Sahrens return (0); 1923789Sahrens } 1924789Sahrens 1925789Sahrens /* 1926789Sahrens * Remove a directory subdir entry. If the current working 1927789Sahrens * directory is the same as the subdir to be removed, the 1928789Sahrens * remove will fail. 1929789Sahrens * 1930789Sahrens * IN: dvp - vnode of directory to remove from. 1931789Sahrens * name - name of directory to be removed. 1932789Sahrens * cwd - vnode of current working directory. 1933789Sahrens * cr - credentials of caller. 19345331Samw * ct - caller context 19355331Samw * flags - case flags 1936789Sahrens * 1937789Sahrens * RETURN: 0 if success 1938789Sahrens * error code if failure 1939789Sahrens * 1940789Sahrens * Timestamps: 1941789Sahrens * dvp - ctime|mtime updated 1942789Sahrens */ 19435331Samw /*ARGSUSED*/ 1944789Sahrens static int 19455331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 19465331Samw caller_context_t *ct, int flags) 1947789Sahrens { 1948789Sahrens znode_t *dzp = VTOZ(dvp); 1949789Sahrens znode_t *zp; 1950789Sahrens vnode_t *vp; 1951789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 19525326Sek110237 zilog_t *zilog; 1953789Sahrens zfs_dirlock_t *dl; 1954789Sahrens dmu_tx_t *tx; 1955789Sahrens int error; 19565331Samw int zflg = ZEXISTS; 1957789Sahrens 19585367Sahrens ZFS_ENTER(zfsvfs); 19595367Sahrens ZFS_VERIFY_ZP(dzp); 19605326Sek110237 zilog = zfsvfs->z_log; 1961789Sahrens 19625331Samw if (flags & FIGNORECASE) 19635331Samw zflg |= ZCILOOK; 1964789Sahrens top: 1965789Sahrens zp = NULL; 1966789Sahrens 1967789Sahrens /* 1968789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1969789Sahrens */ 19705331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 19715331Samw NULL, NULL)) { 1972789Sahrens ZFS_EXIT(zfsvfs); 1973789Sahrens return (error); 1974789Sahrens } 1975789Sahrens 1976789Sahrens vp = ZTOV(zp); 1977789Sahrens 1978789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1979789Sahrens goto out; 1980789Sahrens } 1981789Sahrens 1982789Sahrens if (vp->v_type != VDIR) { 1983789Sahrens error = ENOTDIR; 1984789Sahrens goto out; 1985789Sahrens } 1986789Sahrens 1987789Sahrens if (vp == cwd) { 1988789Sahrens error = EINVAL; 1989789Sahrens goto out; 1990789Sahrens } 1991789Sahrens 19925331Samw vnevent_rmdir(vp, dvp, name, ct); 1993789Sahrens 1994789Sahrens /* 19953897Smaybee * Grab a lock on the directory to make sure that noone is 19963897Smaybee * trying to add (or lookup) entries while we are removing it. 19973897Smaybee */ 19983897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 19993897Smaybee 20003897Smaybee /* 20013897Smaybee * Grab a lock on the parent pointer to make sure we play well 2002789Sahrens * with the treewalk and directory rename code. 2003789Sahrens */ 2004789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 2005789Sahrens 2006789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 20071544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 200811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 20093461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 201011935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 201111935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 20128227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 2013789Sahrens if (error) { 2014789Sahrens rw_exit(&zp->z_parent_lock); 20153897Smaybee rw_exit(&zp->z_name_lock); 2016789Sahrens zfs_dirent_unlock(dl); 2017789Sahrens VN_RELE(vp); 20188227SNeil.Perrin@Sun.COM if (error == ERESTART) { 20192113Sahrens dmu_tx_wait(tx); 20202113Sahrens dmu_tx_abort(tx); 2021789Sahrens goto top; 2022789Sahrens } 20232113Sahrens dmu_tx_abort(tx); 2024789Sahrens ZFS_EXIT(zfsvfs); 2025789Sahrens return (error); 2026789Sahrens } 2027789Sahrens 20285331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 20295331Samw 20305331Samw if (error == 0) { 20315331Samw uint64_t txtype = TX_RMDIR; 20325331Samw if (flags & FIGNORECASE) 20335331Samw txtype |= TX_CI; 203412699SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 20355331Samw } 2036789Sahrens 2037789Sahrens dmu_tx_commit(tx); 2038789Sahrens 2039789Sahrens rw_exit(&zp->z_parent_lock); 20403897Smaybee rw_exit(&zp->z_name_lock); 2041789Sahrens out: 2042789Sahrens zfs_dirent_unlock(dl); 2043789Sahrens 2044789Sahrens VN_RELE(vp); 2045789Sahrens 204612294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 204712699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 204812294SMark.Musante@Sun.COM 2049789Sahrens ZFS_EXIT(zfsvfs); 2050789Sahrens return (error); 2051789Sahrens } 2052789Sahrens 2053789Sahrens /* 2054789Sahrens * Read as many directory entries as will fit into the provided 2055789Sahrens * buffer from the given directory cursor position (specified in 2056789Sahrens * the uio structure. 2057789Sahrens * 2058789Sahrens * IN: vp - vnode of directory to read. 2059789Sahrens * uio - structure supplying read location, range info, 2060789Sahrens * and return buffer. 2061789Sahrens * cr - credentials of caller. 20625331Samw * ct - caller context 20635331Samw * flags - case flags 2064789Sahrens * 2065789Sahrens * OUT: uio - updated offset and range, buffer filled. 2066789Sahrens * eofp - set to true if end-of-file detected. 2067789Sahrens * 2068789Sahrens * RETURN: 0 if success 2069789Sahrens * error code if failure 2070789Sahrens * 2071789Sahrens * Timestamps: 2072789Sahrens * vp - atime updated 2073789Sahrens * 2074789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 2075789Sahrens * This allows us to use the low range for "special" directory entries: 2076789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2077789Sahrens * we use the offset 2 for the '.zfs' directory. 2078789Sahrens */ 2079789Sahrens /* ARGSUSED */ 2080789Sahrens static int 20815331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 20825331Samw caller_context_t *ct, int flags) 2083789Sahrens { 2084789Sahrens znode_t *zp = VTOZ(vp); 2085789Sahrens iovec_t *iovp; 20865331Samw edirent_t *eodp; 2087789Sahrens dirent64_t *odp; 2088789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2089869Sperrin objset_t *os; 2090789Sahrens caddr_t outbuf; 2091789Sahrens size_t bufsize; 2092789Sahrens zap_cursor_t zc; 2093789Sahrens zap_attribute_t zap; 2094789Sahrens uint_t bytes_wanted; 2095789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 209611935SMark.Shellenbaum@Sun.COM uint64_t parent; 2097789Sahrens int local_eof; 2098869Sperrin int outcount; 2099869Sperrin int error; 2100869Sperrin uint8_t prefetch; 21015663Sck153898 boolean_t check_sysattrs; 2102789Sahrens 21035367Sahrens ZFS_ENTER(zfsvfs); 21045367Sahrens ZFS_VERIFY_ZP(zp); 2105789Sahrens 210611935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 210711935SMark.Shellenbaum@Sun.COM &parent, sizeof (parent))) != 0) { 210811935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 210911935SMark.Shellenbaum@Sun.COM return (error); 211011935SMark.Shellenbaum@Sun.COM } 211111935SMark.Shellenbaum@Sun.COM 2112789Sahrens /* 2113789Sahrens * If we are not given an eof variable, 2114789Sahrens * use a local one. 2115789Sahrens */ 2116789Sahrens if (eofp == NULL) 2117789Sahrens eofp = &local_eof; 2118789Sahrens 2119789Sahrens /* 2120789Sahrens * Check for valid iov_len. 2121789Sahrens */ 2122789Sahrens if (uio->uio_iov->iov_len <= 0) { 2123789Sahrens ZFS_EXIT(zfsvfs); 2124789Sahrens return (EINVAL); 2125789Sahrens } 2126789Sahrens 2127789Sahrens /* 2128789Sahrens * Quit if directory has been removed (posix) 2129789Sahrens */ 21303461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 2131789Sahrens ZFS_EXIT(zfsvfs); 2132789Sahrens return (0); 2133789Sahrens } 2134789Sahrens 2135869Sperrin error = 0; 2136869Sperrin os = zfsvfs->z_os; 2137869Sperrin offset = uio->uio_loffset; 2138869Sperrin prefetch = zp->z_zn_prefetch; 2139869Sperrin 2140789Sahrens /* 2141789Sahrens * Initialize the iterator cursor. 2142789Sahrens */ 2143789Sahrens if (offset <= 3) { 2144789Sahrens /* 2145789Sahrens * Start iteration from the beginning of the directory. 2146789Sahrens */ 2147869Sperrin zap_cursor_init(&zc, os, zp->z_id); 2148789Sahrens } else { 2149789Sahrens /* 2150789Sahrens * The offset is a serialized cursor. 2151789Sahrens */ 2152869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2153789Sahrens } 2154789Sahrens 2155789Sahrens /* 2156789Sahrens * Get space to change directory entries into fs independent format. 2157789Sahrens */ 2158789Sahrens iovp = uio->uio_iov; 2159789Sahrens bytes_wanted = iovp->iov_len; 2160789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2161789Sahrens bufsize = bytes_wanted; 2162789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 2163789Sahrens odp = (struct dirent64 *)outbuf; 2164789Sahrens } else { 2165789Sahrens bufsize = bytes_wanted; 2166789Sahrens odp = (struct dirent64 *)iovp->iov_base; 2167789Sahrens } 21685331Samw eodp = (struct edirent *)odp; 2169789Sahrens 2170789Sahrens /* 21717757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 21727757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 21737757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 21747757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 21755663Sck153898 */ 21767757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 21775663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 21785663Sck153898 (flags & V_RDDIR_ENTFLAGS); 21795663Sck153898 21805663Sck153898 /* 2181789Sahrens * Transform to file-system independent format 2182789Sahrens */ 2183789Sahrens outcount = 0; 2184789Sahrens while (outcount < bytes_wanted) { 21853912Slling ino64_t objnum; 21863912Slling ushort_t reclen; 218712799STim.Haley@Sun.COM off64_t *next = NULL; 21883912Slling 2189789Sahrens /* 2190789Sahrens * Special case `.', `..', and `.zfs'. 2191789Sahrens */ 2192789Sahrens if (offset == 0) { 2193789Sahrens (void) strcpy(zap.za_name, "."); 21945331Samw zap.za_normalization_conflict = 0; 21953912Slling objnum = zp->z_id; 2196789Sahrens } else if (offset == 1) { 2197789Sahrens (void) strcpy(zap.za_name, ".."); 21985331Samw zap.za_normalization_conflict = 0; 219911935SMark.Shellenbaum@Sun.COM objnum = parent; 2200789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2201789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 22025331Samw zap.za_normalization_conflict = 0; 22033912Slling objnum = ZFSCTL_INO_ROOT; 2204789Sahrens } else { 2205789Sahrens /* 2206789Sahrens * Grab next entry. 2207789Sahrens */ 2208789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2209789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2210789Sahrens break; 2211789Sahrens else 2212789Sahrens goto update; 2213789Sahrens } 2214789Sahrens 2215789Sahrens if (zap.za_integer_length != 8 || 2216789Sahrens zap.za_num_integers != 1) { 2217789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2218789Sahrens "entry, obj = %lld, offset = %lld\n", 2219789Sahrens (u_longlong_t)zp->z_id, 2220789Sahrens (u_longlong_t)offset); 2221789Sahrens error = ENXIO; 2222789Sahrens goto update; 2223789Sahrens } 22243912Slling 22253912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 22263912Slling /* 22273912Slling * MacOS X can extract the object type here such as: 22283912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 22293912Slling */ 22305663Sck153898 22315663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 22325663Sck153898 zap.za_normalization_conflict = 22335663Sck153898 xattr_sysattr_casechk(zap.za_name); 22345663Sck153898 } 2235789Sahrens } 22365331Samw 22379749STim.Haley@Sun.COM if (flags & V_RDDIR_ACCFILTER) { 22389749STim.Haley@Sun.COM /* 22399749STim.Haley@Sun.COM * If we have no access at all, don't include 22409749STim.Haley@Sun.COM * this entry in the returned information 22419749STim.Haley@Sun.COM */ 22429749STim.Haley@Sun.COM znode_t *ezp; 22439749STim.Haley@Sun.COM if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 22449749STim.Haley@Sun.COM goto skip_entry; 22459749STim.Haley@Sun.COM if (!zfs_has_access(ezp, cr)) { 22469749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22479749STim.Haley@Sun.COM goto skip_entry; 22489749STim.Haley@Sun.COM } 22499749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22509749STim.Haley@Sun.COM } 22519749STim.Haley@Sun.COM 22525331Samw if (flags & V_RDDIR_ENTFLAGS) 22535331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 22545331Samw else 22555331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2256789Sahrens 2257789Sahrens /* 2258789Sahrens * Will this entry fit in the buffer? 2259789Sahrens */ 22603912Slling if (outcount + reclen > bufsize) { 2261789Sahrens /* 2262789Sahrens * Did we manage to fit anything in the buffer? 2263789Sahrens */ 2264789Sahrens if (!outcount) { 2265789Sahrens error = EINVAL; 2266789Sahrens goto update; 2267789Sahrens } 2268789Sahrens break; 2269789Sahrens } 22705331Samw if (flags & V_RDDIR_ENTFLAGS) { 22715331Samw /* 22725331Samw * Add extended flag entry: 22735331Samw */ 22745331Samw eodp->ed_ino = objnum; 22755331Samw eodp->ed_reclen = reclen; 22765331Samw /* NOTE: ed_off is the offset for the *next* entry */ 22775331Samw next = &(eodp->ed_off); 22785331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 22795331Samw ED_CASE_CONFLICT : 0; 22805331Samw (void) strncpy(eodp->ed_name, zap.za_name, 22815331Samw EDIRENT_NAMELEN(reclen)); 22825331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 22835331Samw } else { 22845331Samw /* 22855331Samw * Add normal entry: 22865331Samw */ 22875331Samw odp->d_ino = objnum; 22885331Samw odp->d_reclen = reclen; 22895331Samw /* NOTE: d_off is the offset for the *next* entry */ 22905331Samw next = &(odp->d_off); 22915331Samw (void) strncpy(odp->d_name, zap.za_name, 22925331Samw DIRENT64_NAMELEN(reclen)); 22935331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 22945331Samw } 22953912Slling outcount += reclen; 2296789Sahrens 2297789Sahrens ASSERT(outcount <= bufsize); 2298789Sahrens 2299789Sahrens /* Prefetch znode */ 2300869Sperrin if (prefetch) 23013912Slling dmu_prefetch(os, objnum, 0, 0); 2302789Sahrens 23039749STim.Haley@Sun.COM skip_entry: 2304789Sahrens /* 2305789Sahrens * Move to the next entry, fill in the previous offset. 2306789Sahrens */ 2307789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2308789Sahrens zap_cursor_advance(&zc); 2309789Sahrens offset = zap_cursor_serialize(&zc); 2310789Sahrens } else { 2311789Sahrens offset += 1; 2312789Sahrens } 231312799STim.Haley@Sun.COM if (next) 231412799STim.Haley@Sun.COM *next = offset; 2315789Sahrens } 2316869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2317789Sahrens 2318789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2319789Sahrens iovp->iov_base += outcount; 2320789Sahrens iovp->iov_len -= outcount; 2321789Sahrens uio->uio_resid -= outcount; 2322789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2323789Sahrens /* 2324789Sahrens * Reset the pointer. 2325789Sahrens */ 2326789Sahrens offset = uio->uio_loffset; 2327789Sahrens } 2328789Sahrens 2329789Sahrens update: 2330885Sahrens zap_cursor_fini(&zc); 2331789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2332789Sahrens kmem_free(outbuf, bufsize); 2333789Sahrens 2334789Sahrens if (error == ENOENT) 2335789Sahrens error = 0; 2336789Sahrens 2337789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2338789Sahrens 2339789Sahrens uio->uio_loffset = offset; 2340789Sahrens ZFS_EXIT(zfsvfs); 2341789Sahrens return (error); 2342789Sahrens } 2343789Sahrens 23444720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 23454720Sfr157268 2346789Sahrens static int 23475331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2348789Sahrens { 2349789Sahrens znode_t *zp = VTOZ(vp); 2350789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2351789Sahrens 23521773Seschrock /* 23531773Seschrock * Regardless of whether this is required for standards conformance, 23541773Seschrock * this is the logical behavior when fsync() is called on a file with 23551773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 23561773Seschrock * going to be pushed out as part of the zil_commit(). 23571773Seschrock */ 23581773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 23591773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 23605331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 23611773Seschrock 23624720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 23634720Sfr157268 236412294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 236512294SMark.Musante@Sun.COM ZFS_ENTER(zfsvfs); 236612294SMark.Musante@Sun.COM ZFS_VERIFY_ZP(zp); 236712699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 236812294SMark.Musante@Sun.COM ZFS_EXIT(zfsvfs); 236912294SMark.Musante@Sun.COM } 2370789Sahrens return (0); 2371789Sahrens } 2372789Sahrens 23735331Samw 2374789Sahrens /* 2375789Sahrens * Get the requested file attributes and place them in the provided 2376789Sahrens * vattr structure. 2377789Sahrens * 2378789Sahrens * IN: vp - vnode of file. 2379789Sahrens * vap - va_mask identifies requested attributes. 23805331Samw * If AT_XVATTR set, then optional attrs are requested 23815331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2382789Sahrens * cr - credentials of caller. 23835331Samw * ct - caller context 2384789Sahrens * 2385789Sahrens * OUT: vap - attribute values. 2386789Sahrens * 2387789Sahrens * RETURN: 0 (always succeeds) 2388789Sahrens */ 2389789Sahrens /* ARGSUSED */ 2390789Sahrens static int 23915331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 23925331Samw caller_context_t *ct) 2393789Sahrens { 2394789Sahrens znode_t *zp = VTOZ(vp); 2395789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23965331Samw int error = 0; 23974543Smarks uint64_t links; 239811935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 23995331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 24005331Samw xoptattr_t *xoap = NULL; 24015331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 240211935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[2]; 240311935SMark.Shellenbaum@Sun.COM int count = 0; 2404789Sahrens 24055367Sahrens ZFS_ENTER(zfsvfs); 24065367Sahrens ZFS_VERIFY_ZP(zp); 240711935SMark.Shellenbaum@Sun.COM 240811935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 240911935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 241011935SMark.Shellenbaum@Sun.COM 241111935SMark.Shellenbaum@Sun.COM if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 241211935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 241311935SMark.Shellenbaum@Sun.COM return (error); 241411935SMark.Shellenbaum@Sun.COM } 2415789Sahrens 24165331Samw /* 24175331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 24185331Samw * Also, if we are the owner don't bother, since owner should 24195331Samw * always be allowed to read basic attributes of file. 24205331Samw */ 242111935SMark.Shellenbaum@Sun.COM if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) { 24225331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 24235331Samw skipaclchk, cr)) { 24245331Samw ZFS_EXIT(zfsvfs); 24255331Samw return (error); 24265331Samw } 24275331Samw } 24285331Samw 2429789Sahrens /* 2430789Sahrens * Return all attributes. It's cheaper to provide the answer 2431789Sahrens * than to determine whether we were asked the question. 2432789Sahrens */ 2433789Sahrens 24349774SRay.Hassan@Sun.COM mutex_enter(&zp->z_lock); 2435789Sahrens vap->va_type = vp->v_type; 243611935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode & MODEMASK; 243711935SMark.Shellenbaum@Sun.COM vap->va_uid = zp->z_uid; 243811935SMark.Shellenbaum@Sun.COM vap->va_gid = zp->z_gid; 2439789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2440789Sahrens vap->va_nodeid = zp->z_id; 24414543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 244211935SMark.Shellenbaum@Sun.COM links = zp->z_links + 1; 24434543Smarks else 244411935SMark.Shellenbaum@Sun.COM links = zp->z_links; 24454543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 244611935SMark.Shellenbaum@Sun.COM vap->va_size = zp->z_size; 24471816Smarks vap->va_rdev = vp->v_rdev; 2448789Sahrens vap->va_seq = zp->z_seq; 2449789Sahrens 24505331Samw /* 24515331Samw * Add in any requested optional attributes and the create time. 24525331Samw * Also set the corresponding bits in the returned attribute bitmap. 24535331Samw */ 24545331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 24555331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 24565331Samw xoap->xoa_archive = 245711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_ARCHIVE) != 0); 24585331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 24595331Samw } 24605331Samw 24615331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 24625331Samw xoap->xoa_readonly = 246311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_READONLY) != 0); 24645331Samw XVA_SET_RTN(xvap, XAT_READONLY); 24655331Samw } 24665331Samw 24675331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 24685331Samw xoap->xoa_system = 246911935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_SYSTEM) != 0); 24705331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 24715331Samw } 24725331Samw 24735331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 24745331Samw xoap->xoa_hidden = 247511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_HIDDEN) != 0); 24765331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 24775331Samw } 24785331Samw 24795331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 24805331Samw xoap->xoa_nounlink = 248111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0); 24825331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 24835331Samw } 24845331Samw 24855331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 24865331Samw xoap->xoa_immutable = 248711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 24885331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 24895331Samw } 24905331Samw 24915331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 24925331Samw xoap->xoa_appendonly = 249311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0); 24945331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 24955331Samw } 24965331Samw 24975331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 24985331Samw xoap->xoa_nodump = 249911935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0); 25005331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 25015331Samw } 25025331Samw 25035331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 25045331Samw xoap->xoa_opaque = 250511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_OPAQUE) != 0); 25065331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 25075331Samw } 25085331Samw 25095331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 25105331Samw xoap->xoa_av_quarantined = 251111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 25125331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 25135331Samw } 25145331Samw 25155331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 25165331Samw xoap->xoa_av_modified = 251711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 25185331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 25195331Samw } 25205331Samw 25215331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 252211935SMark.Shellenbaum@Sun.COM vp->v_type == VREG) { 252311935SMark.Shellenbaum@Sun.COM zfs_sa_get_scanstamp(zp, xvap); 25245331Samw } 25255331Samw 25265331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 252711935SMark.Shellenbaum@Sun.COM uint64_t times[2]; 252811935SMark.Shellenbaum@Sun.COM 252911935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 253011935SMark.Shellenbaum@Sun.COM times, sizeof (times)); 253111935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 25325331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 25335331Samw } 253410793Sdai.ngo@sun.com 253510793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 253611935SMark.Shellenbaum@Sun.COM xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 253710793Sdai.ngo@sun.com XVA_SET_RTN(xvap, XAT_REPARSE); 253810793Sdai.ngo@sun.com } 2539*13043STim.Haley@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_GEN)) { 2540*13043STim.Haley@Sun.COM xoap->xoa_generation = zp->z_gen; 2541*13043STim.Haley@Sun.COM XVA_SET_RTN(xvap, XAT_GEN); 2542*13043STim.Haley@Sun.COM } 25435331Samw } 25445331Samw 254511935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 254611935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_mtime, mtime); 254711935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2548789Sahrens 2549789Sahrens mutex_exit(&zp->z_lock); 2550789Sahrens 255111935SMark.Shellenbaum@Sun.COM sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); 2552789Sahrens 2553789Sahrens if (zp->z_blksz == 0) { 2554789Sahrens /* 2555789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2556789Sahrens */ 2557789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2558789Sahrens } 2559789Sahrens 2560789Sahrens ZFS_EXIT(zfsvfs); 2561789Sahrens return (0); 2562789Sahrens } 2563789Sahrens 2564789Sahrens /* 2565789Sahrens * Set the file attributes to the values contained in the 2566789Sahrens * vattr structure. 2567789Sahrens * 2568789Sahrens * IN: vp - vnode of file to be modified. 2569789Sahrens * vap - new attribute values. 25705331Samw * If AT_XVATTR set, then optional attrs are being set 2571789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 25725331Samw * - ATTR_NOACLCHECK (CIFS context only). 2573789Sahrens * cr - credentials of caller. 25745331Samw * ct - caller context 2575789Sahrens * 2576789Sahrens * RETURN: 0 if success 2577789Sahrens * error code if failure 2578789Sahrens * 2579789Sahrens * Timestamps: 2580789Sahrens * vp - ctime updated, mtime updated if size changed. 2581789Sahrens */ 2582789Sahrens /* ARGSUSED */ 2583789Sahrens static int 2584789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2585789Sahrens caller_context_t *ct) 2586789Sahrens { 25875326Sek110237 znode_t *zp = VTOZ(vp); 2588789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 25895326Sek110237 zilog_t *zilog; 2590789Sahrens dmu_tx_t *tx; 25911878Smaybee vattr_t oldva; 25928190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2593789Sahrens uint_t mask = vap->va_mask; 25941878Smaybee uint_t saved_mask; 25952796Smarks int trim_mask = 0; 2596789Sahrens uint64_t new_mode; 25979179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 259811935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj = 0; 259911935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 26001231Smarks znode_t *attrzp; 2601789Sahrens int need_policy = FALSE; 260211935SMark.Shellenbaum@Sun.COM int err, err2; 26035331Samw zfs_fuid_info_t *fuidp = NULL; 26045331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 26055331Samw xoptattr_t *xoap; 26065824Smarks zfs_acl_t *aclp = NULL; 26075331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 260811935SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 260911935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[7], xattr_bulk[7]; 261011935SMark.Shellenbaum@Sun.COM int count = 0, xattr_count = 0; 2611789Sahrens 2612789Sahrens if (mask == 0) 2613789Sahrens return (0); 2614789Sahrens 2615789Sahrens if (mask & AT_NOSET) 2616789Sahrens return (EINVAL); 2617789Sahrens 26185367Sahrens ZFS_ENTER(zfsvfs); 26195367Sahrens ZFS_VERIFY_ZP(zp); 26205331Samw 26215331Samw zilog = zfsvfs->z_log; 26225331Samw 26235331Samw /* 26245331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 26255331Samw * that file system is at proper version level 26265331Samw */ 26275331Samw 26285331Samw if (zfsvfs->z_use_fuids == B_FALSE && 26295331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26305331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 26315386Stimh (mask & AT_XVATTR))) { 26325386Stimh ZFS_EXIT(zfsvfs); 26335331Samw return (EINVAL); 26345386Stimh } 26355386Stimh 26365386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 26375386Stimh ZFS_EXIT(zfsvfs); 2638789Sahrens return (EISDIR); 26395386Stimh } 26405386Stimh 26415386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 26425386Stimh ZFS_EXIT(zfsvfs); 26431308Smarks return (EINVAL); 26445386Stimh } 26451308Smarks 26465331Samw /* 26475331Samw * If this is an xvattr_t, then get a pointer to the structure of 26485331Samw * optional attributes. If this is NULL, then we have a vattr_t. 26495331Samw */ 26505331Samw xoap = xva_getxoptattr(xvap); 26515331Samw 26528190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 26538190SMark.Shellenbaum@Sun.COM 26545331Samw /* 26555331Samw * Immutable files can only alter immutable bit and atime 26565331Samw */ 265711935SMark.Shellenbaum@Sun.COM if ((zp->z_pflags & ZFS_IMMUTABLE) && 26585331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 26595386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 26605386Stimh ZFS_EXIT(zfsvfs); 26615331Samw return (EPERM); 26625386Stimh } 26635386Stimh 266411935SMark.Shellenbaum@Sun.COM if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 26655386Stimh ZFS_EXIT(zfsvfs); 26665331Samw return (EPERM); 26675386Stimh } 2668789Sahrens 26696064Smarks /* 26706064Smarks * Verify timestamps doesn't overflow 32 bits. 26716064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 26726064Smarks * handle times greater than 2039. This check should be removed 26736064Smarks * once large timestamps are fully supported. 26746064Smarks */ 26756064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 26766064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 26776064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 26786064Smarks ZFS_EXIT(zfsvfs); 26796064Smarks return (EOVERFLOW); 26806064Smarks } 26816064Smarks } 26826064Smarks 2683789Sahrens top: 26841231Smarks attrzp = NULL; 2685789Sahrens 26869981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2687789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2688789Sahrens ZFS_EXIT(zfsvfs); 2689789Sahrens return (EROFS); 2690789Sahrens } 2691789Sahrens 2692789Sahrens /* 2693789Sahrens * First validate permissions 2694789Sahrens */ 2695789Sahrens 2696789Sahrens if (mask & AT_SIZE) { 26975331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2698789Sahrens if (err) { 2699789Sahrens ZFS_EXIT(zfsvfs); 2700789Sahrens return (err); 2701789Sahrens } 27021878Smaybee /* 27031878Smaybee * XXX - Note, we are not providing any open 27041878Smaybee * mode flags here (like FNDELAY), so we may 27051878Smaybee * block if there are locks present... this 27061878Smaybee * should be addressed in openat(). 27071878Smaybee */ 27086992Smaybee /* XXX - would it be OK to generate a log record here? */ 27096992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 27101878Smaybee if (err) { 27111878Smaybee ZFS_EXIT(zfsvfs); 27121878Smaybee return (err); 27131878Smaybee } 2714789Sahrens } 2715789Sahrens 27165331Samw if (mask & (AT_ATIME|AT_MTIME) || 27175331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 27185331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 27195331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 27205331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 272111935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 27225331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 27235331Samw skipaclchk, cr); 272411935SMark.Shellenbaum@Sun.COM } 2725789Sahrens 2726789Sahrens if (mask & (AT_UID|AT_GID)) { 2727789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2728789Sahrens int take_owner; 2729789Sahrens int take_group; 2730789Sahrens 2731789Sahrens /* 2732913Smarks * NOTE: even if a new mode is being set, 2733913Smarks * we may clear S_ISUID/S_ISGID bits. 2734913Smarks */ 2735913Smarks 2736913Smarks if (!(mask & AT_MODE)) 273711935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode; 2738913Smarks 2739913Smarks /* 2740789Sahrens * Take ownership or chgrp to group we are a member of 2741789Sahrens */ 2742789Sahrens 2743789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 27445331Samw take_group = (mask & AT_GID) && 27455331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2746789Sahrens 2747789Sahrens /* 2748789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2749789Sahrens * take_group must both be set in order to allow taking 2750789Sahrens * ownership. 2751789Sahrens * 2752789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2753789Sahrens * 2754789Sahrens */ 2755789Sahrens 2756789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2757789Sahrens ((idmask == AT_UID) && take_owner) || 2758789Sahrens ((idmask == AT_GID) && take_group)) { 27595331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 27605331Samw skipaclchk, cr) == 0) { 2761789Sahrens /* 2762789Sahrens * Remove setuid/setgid for non-privileged users 2763789Sahrens */ 27641115Smarks secpolicy_setid_clear(vap, cr); 27652796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2766789Sahrens } else { 2767789Sahrens need_policy = TRUE; 2768789Sahrens } 2769789Sahrens } else { 2770789Sahrens need_policy = TRUE; 2771789Sahrens } 2772789Sahrens } 2773789Sahrens 27742796Smarks mutex_enter(&zp->z_lock); 277511935SMark.Shellenbaum@Sun.COM oldva.va_mode = zp->z_mode; 277611935SMark.Shellenbaum@Sun.COM oldva.va_uid = zp->z_uid; 277711935SMark.Shellenbaum@Sun.COM oldva.va_gid = zp->z_gid; 27785331Samw if (mask & AT_XVATTR) { 27798190SMark.Shellenbaum@Sun.COM /* 27808190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 27818190SMark.Shellenbaum@Sun.COM * that are actually changing. 27828190SMark.Shellenbaum@Sun.COM * 27838190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 27848190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 27858190SMark.Shellenbaum@Sun.COM */ 27868190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 27878190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 278811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 27898190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27908190SMark.Shellenbaum@Sun.COM } else { 27918190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 27928190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 27938190SMark.Shellenbaum@Sun.COM } 27948190SMark.Shellenbaum@Sun.COM } 27958190SMark.Shellenbaum@Sun.COM 27968190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 27978190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 279811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 27998190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28008190SMark.Shellenbaum@Sun.COM } else { 28018190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 28028190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 28038190SMark.Shellenbaum@Sun.COM } 28048190SMark.Shellenbaum@Sun.COM } 28058190SMark.Shellenbaum@Sun.COM 28068190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 28078190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 280811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 28098190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28108190SMark.Shellenbaum@Sun.COM } else { 28118190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 28128190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 28138190SMark.Shellenbaum@Sun.COM } 28148190SMark.Shellenbaum@Sun.COM } 28158190SMark.Shellenbaum@Sun.COM 28168190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 28178190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 281811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0)) { 28198190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28208190SMark.Shellenbaum@Sun.COM } else { 28218190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 28228190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 28238190SMark.Shellenbaum@Sun.COM } 28248190SMark.Shellenbaum@Sun.COM } 28258190SMark.Shellenbaum@Sun.COM 28268190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 28278190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 282811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 28298190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28308190SMark.Shellenbaum@Sun.COM } else { 28318190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 28328190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 28338190SMark.Shellenbaum@Sun.COM } 28348190SMark.Shellenbaum@Sun.COM } 28358190SMark.Shellenbaum@Sun.COM 28368190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 28378190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 28388190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 28398190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 284011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 28418190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28428190SMark.Shellenbaum@Sun.COM } else { 28438190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 28448190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 28458190SMark.Shellenbaum@Sun.COM } 28468190SMark.Shellenbaum@Sun.COM } 28478190SMark.Shellenbaum@Sun.COM 284810793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 284910793Sdai.ngo@sun.com mutex_exit(&zp->z_lock); 285010793Sdai.ngo@sun.com ZFS_EXIT(zfsvfs); 285110793Sdai.ngo@sun.com return (EPERM); 285210793Sdai.ngo@sun.com } 285310793Sdai.ngo@sun.com 28548190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 28558190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 28568190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 28575331Samw need_policy = TRUE; 28585331Samw } 28595331Samw } 28605331Samw 28612796Smarks mutex_exit(&zp->z_lock); 28622796Smarks 28632796Smarks if (mask & AT_MODE) { 28645331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 28652796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 28662796Smarks &oldva, cr); 28672796Smarks if (err) { 28682796Smarks ZFS_EXIT(zfsvfs); 28692796Smarks return (err); 28702796Smarks } 28712796Smarks trim_mask |= AT_MODE; 28722796Smarks } else { 28732796Smarks need_policy = TRUE; 28742796Smarks } 28752796Smarks } 2876789Sahrens 2877789Sahrens if (need_policy) { 28781115Smarks /* 28791115Smarks * If trim_mask is set then take ownership 28802796Smarks * has been granted or write_acl is present and user 28812796Smarks * has the ability to modify mode. In that case remove 28822796Smarks * UID|GID and or MODE from mask so that 28831115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 28841115Smarks */ 28852796Smarks 28862796Smarks if (trim_mask) { 28872796Smarks saved_mask = vap->va_mask; 28882796Smarks vap->va_mask &= ~trim_mask; 28892796Smarks } 2890789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 28915331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2892789Sahrens if (err) { 2893789Sahrens ZFS_EXIT(zfsvfs); 2894789Sahrens return (err); 2895789Sahrens } 28961115Smarks 28971115Smarks if (trim_mask) 28982796Smarks vap->va_mask |= saved_mask; 2899789Sahrens } 2900789Sahrens 2901789Sahrens /* 2902789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2903789Sahrens * changed va_mask 2904789Sahrens */ 2905789Sahrens mask = vap->va_mask; 2906789Sahrens 290711935SMark.Shellenbaum@Sun.COM if ((mask & (AT_UID | AT_GID))) { 290811935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj, 290911935SMark.Shellenbaum@Sun.COM sizeof (xattr_obj)); 291011935SMark.Shellenbaum@Sun.COM 291111935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 291211935SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 29139396SMatthew.Ahrens@Sun.COM if (err) 291411935SMark.Shellenbaum@Sun.COM goto out2; 29159179SMark.Shellenbaum@Sun.COM } 29169179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 29179179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 29189179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 291911935SMark.Shellenbaum@Sun.COM if (vap->va_uid != zp->z_uid && 292011935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 29219396SMatthew.Ahrens@Sun.COM err = EDQUOT; 292211935SMark.Shellenbaum@Sun.COM goto out2; 29239396SMatthew.Ahrens@Sun.COM } 29241231Smarks } 29259396SMatthew.Ahrens@Sun.COM 29269179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 29279179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 29289179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 292911935SMark.Shellenbaum@Sun.COM if (new_gid != zp->z_gid && 293011935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 29319396SMatthew.Ahrens@Sun.COM err = EDQUOT; 293211935SMark.Shellenbaum@Sun.COM goto out2; 29339179SMark.Shellenbaum@Sun.COM } 29349179SMark.Shellenbaum@Sun.COM } 29351231Smarks } 293611935SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 293711935SMark.Shellenbaum@Sun.COM 293811935SMark.Shellenbaum@Sun.COM if (mask & AT_MODE) { 293911935SMark.Shellenbaum@Sun.COM uint64_t pmode = zp->z_mode; 294012620SMark.Shellenbaum@Oracle.COM uint64_t acl_obj; 294111935SMark.Shellenbaum@Sun.COM new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 294211935SMark.Shellenbaum@Sun.COM 294311935SMark.Shellenbaum@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 294411935SMark.Shellenbaum@Sun.COM goto out; 294511935SMark.Shellenbaum@Sun.COM 294612620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 294712620SMark.Shellenbaum@Oracle.COM if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 294811935SMark.Shellenbaum@Sun.COM /* 294911935SMark.Shellenbaum@Sun.COM * Are we upgrading ACL from old V0 format 295011935SMark.Shellenbaum@Sun.COM * to V1 format? 295111935SMark.Shellenbaum@Sun.COM */ 295211935SMark.Shellenbaum@Sun.COM if (zfsvfs->z_version <= ZPL_VERSION_FUID && 295312620SMark.Shellenbaum@Oracle.COM zfs_znode_acl_version(zp) == 295411935SMark.Shellenbaum@Sun.COM ZFS_ACL_VERSION_INITIAL) { 295512620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_free(tx, acl_obj, 0, 295611935SMark.Shellenbaum@Sun.COM DMU_OBJECT_END); 295711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 295811935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 295911935SMark.Shellenbaum@Sun.COM } else { 296012620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_write(tx, acl_obj, 0, 296111935SMark.Shellenbaum@Sun.COM aclp->z_acl_bytes); 296211935SMark.Shellenbaum@Sun.COM } 296311935SMark.Shellenbaum@Sun.COM } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 296411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 296511935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 296611935SMark.Shellenbaum@Sun.COM } 296712620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 296811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 296911935SMark.Shellenbaum@Sun.COM } else { 297011935SMark.Shellenbaum@Sun.COM if ((mask & AT_XVATTR) && 297111935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 297211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 297311935SMark.Shellenbaum@Sun.COM else 297411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 297511935SMark.Shellenbaum@Sun.COM } 297611935SMark.Shellenbaum@Sun.COM 297711935SMark.Shellenbaum@Sun.COM if (attrzp) { 297811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 297911935SMark.Shellenbaum@Sun.COM } 298011935SMark.Shellenbaum@Sun.COM 298111935SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 298211935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 298311935SMark.Shellenbaum@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 298411935SMark.Shellenbaum@Sun.COM 298511935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 29861231Smarks 29878227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2988789Sahrens if (err) { 29899396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 29902113Sahrens dmu_tx_wait(tx); 29919396SMatthew.Ahrens@Sun.COM goto out; 2992789Sahrens } 2993789Sahrens 299411935SMark.Shellenbaum@Sun.COM count = 0; 2995789Sahrens /* 2996789Sahrens * Set each attribute requested. 2997789Sahrens * We group settings according to the locks they need to acquire. 2998789Sahrens * 2999789Sahrens * Note: you cannot set ctime directly, although it will be 3000789Sahrens * updated as a side-effect of calling this function. 3001789Sahrens */ 3002789Sahrens 300312620SMark.Shellenbaum@Oracle.COM 300412620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 300512620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_acl_lock); 3006789Sahrens mutex_enter(&zp->z_lock); 3007789Sahrens 300812394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 300912394SMark.Shellenbaum@Sun.COM &zp->z_pflags, sizeof (zp->z_pflags)); 301012394SMark.Shellenbaum@Sun.COM 301112394SMark.Shellenbaum@Sun.COM if (attrzp) { 301212620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 301312620SMark.Shellenbaum@Oracle.COM mutex_enter(&attrzp->z_acl_lock); 301411935SMark.Shellenbaum@Sun.COM mutex_enter(&attrzp->z_lock); 301512394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 301612394SMark.Shellenbaum@Sun.COM SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 301712394SMark.Shellenbaum@Sun.COM sizeof (attrzp->z_pflags)); 301812394SMark.Shellenbaum@Sun.COM } 301911935SMark.Shellenbaum@Sun.COM 302012164SMark.Shellenbaum@Sun.COM if (mask & (AT_UID|AT_GID)) { 302112164SMark.Shellenbaum@Sun.COM 302212164SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 302312164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 302412164SMark.Shellenbaum@Sun.COM &new_uid, sizeof (new_uid)); 302512164SMark.Shellenbaum@Sun.COM zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid, 302612164SMark.Shellenbaum@Sun.COM cr, ZFS_OWNER); 302712164SMark.Shellenbaum@Sun.COM if (attrzp) { 302812164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 302912164SMark.Shellenbaum@Sun.COM SA_ZPL_UID(zfsvfs), NULL, &new_uid, 303012164SMark.Shellenbaum@Sun.COM sizeof (new_uid)); 303112394SMark.Shellenbaum@Sun.COM attrzp->z_uid = zp->z_uid; 303212164SMark.Shellenbaum@Sun.COM } 303311935SMark.Shellenbaum@Sun.COM } 303412164SMark.Shellenbaum@Sun.COM 303512164SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 303612164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 303712164SMark.Shellenbaum@Sun.COM NULL, &new_gid, sizeof (new_gid)); 303812164SMark.Shellenbaum@Sun.COM zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr, 303912164SMark.Shellenbaum@Sun.COM ZFS_GROUP); 304012164SMark.Shellenbaum@Sun.COM if (attrzp) { 304112164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 304212164SMark.Shellenbaum@Sun.COM SA_ZPL_GID(zfsvfs), NULL, &new_gid, 304312164SMark.Shellenbaum@Sun.COM sizeof (new_gid)); 304412164SMark.Shellenbaum@Sun.COM attrzp->z_gid = zp->z_gid; 304512164SMark.Shellenbaum@Sun.COM } 304612164SMark.Shellenbaum@Sun.COM } 304712164SMark.Shellenbaum@Sun.COM if (!(mask & AT_MODE)) { 304812164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 304912164SMark.Shellenbaum@Sun.COM NULL, &new_mode, sizeof (new_mode)); 305012164SMark.Shellenbaum@Sun.COM new_mode = zp->z_mode; 305112164SMark.Shellenbaum@Sun.COM } 305212164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(zp); 305312164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 305411935SMark.Shellenbaum@Sun.COM if (attrzp) { 305512164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(attrzp); 305612164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 305711935SMark.Shellenbaum@Sun.COM } 305811935SMark.Shellenbaum@Sun.COM } 305911935SMark.Shellenbaum@Sun.COM 3060789Sahrens if (mask & AT_MODE) { 306111935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 306211935SMark.Shellenbaum@Sun.COM &new_mode, sizeof (new_mode)); 306311935SMark.Shellenbaum@Sun.COM zp->z_mode = new_mode; 306412164SMark.Shellenbaum@Sun.COM ASSERT3U((uintptr_t)aclp, !=, NULL); 30659179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 3066789Sahrens ASSERT3U(err, ==, 0); 306710143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 306810143STim.Haley@Sun.COM aclp = NULL; 3069789Sahrens } 3070789Sahrens 307111935SMark.Shellenbaum@Sun.COM 307211935SMark.Shellenbaum@Sun.COM if (mask & AT_ATIME) { 307311935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 307411935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 307511935SMark.Shellenbaum@Sun.COM &zp->z_atime, sizeof (zp->z_atime)); 30761231Smarks } 30771231Smarks 307811935SMark.Shellenbaum@Sun.COM if (mask & AT_MTIME) { 307911935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 308011935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 308111935SMark.Shellenbaum@Sun.COM mtime, sizeof (mtime)); 30821231Smarks } 30831231Smarks 30846992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 308511935SMark.Shellenbaum@Sun.COM if (mask & AT_SIZE && !(mask & AT_MTIME)) { 308612394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 308712394SMark.Shellenbaum@Sun.COM NULL, mtime, sizeof (mtime)); 308811935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 308911935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 309011935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 309111935SMark.Shellenbaum@Sun.COM B_TRUE); 309211935SMark.Shellenbaum@Sun.COM } else if (mask != 0) { 309311935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 309411935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 309511935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 309611935SMark.Shellenbaum@Sun.COM B_TRUE); 309711935SMark.Shellenbaum@Sun.COM if (attrzp) { 309811935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 309911935SMark.Shellenbaum@Sun.COM SA_ZPL_CTIME(zfsvfs), NULL, 310011935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 310111935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 310211935SMark.Shellenbaum@Sun.COM mtime, ctime, B_TRUE); 310311935SMark.Shellenbaum@Sun.COM } 310411935SMark.Shellenbaum@Sun.COM } 31055331Samw /* 31065331Samw * Do this after setting timestamps to prevent timestamp 31075331Samw * update from toggling bit 31085331Samw */ 31095331Samw 31105331Samw if (xoap && (mask & AT_XVATTR)) { 31118190SMark.Shellenbaum@Sun.COM 31128190SMark.Shellenbaum@Sun.COM /* 31138190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 31148190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 31158190SMark.Shellenbaum@Sun.COM */ 31168190SMark.Shellenbaum@Sun.COM 31178190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 31188190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 31198190SMark.Shellenbaum@Sun.COM } 31208190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 31218190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 31228190SMark.Shellenbaum@Sun.COM } 31238190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 31248190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 31258190SMark.Shellenbaum@Sun.COM } 31268190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 31278190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 31288190SMark.Shellenbaum@Sun.COM } 31298190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 31308190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 31318190SMark.Shellenbaum@Sun.COM } 31328190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 31338190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 31348190SMark.Shellenbaum@Sun.COM } 31358190SMark.Shellenbaum@Sun.COM 313611935SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 31375331Samw ASSERT(vp->v_type == VREG); 31385331Samw 313911935SMark.Shellenbaum@Sun.COM zfs_xvattr_set(zp, xvap, tx); 31405331Samw } 3141789Sahrens 31429179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 31439179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 31449179SMark.Shellenbaum@Sun.COM 31451878Smaybee if (mask != 0) 31465331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 31475331Samw 3148789Sahrens mutex_exit(&zp->z_lock); 314912620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 315012620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_acl_lock); 315112620SMark.Shellenbaum@Oracle.COM 315212620SMark.Shellenbaum@Oracle.COM if (attrzp) { 315312620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 315412620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_acl_lock); 315512620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_lock); 315612620SMark.Shellenbaum@Oracle.COM } 31579396SMatthew.Ahrens@Sun.COM out: 315811935SMark.Shellenbaum@Sun.COM if (err == 0 && attrzp) { 315911935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 316011935SMark.Shellenbaum@Sun.COM xattr_count, tx); 316111935SMark.Shellenbaum@Sun.COM ASSERT(err2 == 0); 316211935SMark.Shellenbaum@Sun.COM } 316311935SMark.Shellenbaum@Sun.COM 31641231Smarks if (attrzp) 31651231Smarks VN_RELE(ZTOV(attrzp)); 316610143STim.Haley@Sun.COM if (aclp) 316710143STim.Haley@Sun.COM zfs_acl_free(aclp); 316810143STim.Haley@Sun.COM 31699396SMatthew.Ahrens@Sun.COM if (fuidp) { 31709396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 31719396SMatthew.Ahrens@Sun.COM fuidp = NULL; 31729396SMatthew.Ahrens@Sun.COM } 31739396SMatthew.Ahrens@Sun.COM 317411935SMark.Shellenbaum@Sun.COM if (err) { 31759396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 317611935SMark.Shellenbaum@Sun.COM if (err == ERESTART) 317711935SMark.Shellenbaum@Sun.COM goto top; 317811935SMark.Shellenbaum@Sun.COM } else { 317911935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 31809396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 318111935SMark.Shellenbaum@Sun.COM } 318211935SMark.Shellenbaum@Sun.COM 318311935SMark.Shellenbaum@Sun.COM 318411935SMark.Shellenbaum@Sun.COM out2: 318512294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 318612699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 318712294SMark.Musante@Sun.COM 3188789Sahrens ZFS_EXIT(zfsvfs); 3189789Sahrens return (err); 3190789Sahrens } 3191789Sahrens 31923271Smaybee typedef struct zfs_zlock { 31933271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 31943271Smaybee znode_t *zl_znode; /* znode we held */ 31953271Smaybee struct zfs_zlock *zl_next; /* next in list */ 31963271Smaybee } zfs_zlock_t; 31973271Smaybee 31983271Smaybee /* 31993271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 32003271Smaybee */ 32013271Smaybee static void 32023271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 32033271Smaybee { 32043271Smaybee zfs_zlock_t *zl; 32053271Smaybee 32063271Smaybee while ((zl = *zlpp) != NULL) { 32073271Smaybee if (zl->zl_znode != NULL) 32083271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 32093271Smaybee rw_exit(zl->zl_rwlock); 32103271Smaybee *zlpp = zl->zl_next; 32113271Smaybee kmem_free(zl, sizeof (*zl)); 32123271Smaybee } 32133271Smaybee } 32143271Smaybee 3215789Sahrens /* 3216789Sahrens * Search back through the directory tree, using the ".." entries. 3217789Sahrens * Lock each directory in the chain to prevent concurrent renames. 3218789Sahrens * Fail any attempt to move a directory into one of its own descendants. 3219789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 3220789Sahrens */ 3221789Sahrens static int 3222789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3223789Sahrens { 3224789Sahrens zfs_zlock_t *zl; 32253638Sbillm znode_t *zp = tdzp; 3226789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 322711935SMark.Shellenbaum@Sun.COM uint64_t oidp = zp->z_id; 3228789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 3229789Sahrens krw_t rw = RW_WRITER; 3230789Sahrens 3231789Sahrens /* 3232789Sahrens * First pass write-locks szp and compares to zp->z_id. 3233789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 3234789Sahrens */ 3235789Sahrens do { 32363271Smaybee if (!rw_tryenter(rwlp, rw)) { 32373271Smaybee /* 32383271Smaybee * Another thread is renaming in this path. 32393271Smaybee * Note that if we are a WRITER, we don't have any 32403271Smaybee * parent_locks held yet. 32413271Smaybee */ 32423271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 32433271Smaybee /* 32443271Smaybee * Drop our locks and restart 32453271Smaybee */ 32463271Smaybee zfs_rename_unlock(&zl); 32473271Smaybee *zlpp = NULL; 32483271Smaybee zp = tdzp; 324911935SMark.Shellenbaum@Sun.COM oidp = zp->z_id; 32503271Smaybee rwlp = &szp->z_parent_lock; 32513271Smaybee rw = RW_WRITER; 32523271Smaybee continue; 32533271Smaybee } else { 32543271Smaybee /* 32553271Smaybee * Wait for other thread to drop its locks 32563271Smaybee */ 32573271Smaybee rw_enter(rwlp, rw); 32583271Smaybee } 32593271Smaybee } 32603271Smaybee 3261789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3262789Sahrens zl->zl_rwlock = rwlp; 3263789Sahrens zl->zl_znode = NULL; 3264789Sahrens zl->zl_next = *zlpp; 3265789Sahrens *zlpp = zl; 3266789Sahrens 326711935SMark.Shellenbaum@Sun.COM if (oidp == szp->z_id) /* We're a descendant of szp */ 3268789Sahrens return (EINVAL); 3269789Sahrens 327011935SMark.Shellenbaum@Sun.COM if (oidp == rootid) /* We've hit the top */ 3271789Sahrens return (0); 3272789Sahrens 3273789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 327411935SMark.Shellenbaum@Sun.COM int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3275789Sahrens if (error) 3276789Sahrens return (error); 3277789Sahrens zl->zl_znode = zp; 3278789Sahrens } 327911935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 328011935SMark.Shellenbaum@Sun.COM &oidp, sizeof (oidp)); 3281789Sahrens rwlp = &zp->z_parent_lock; 3282789Sahrens rw = RW_READER; 3283789Sahrens 3284789Sahrens } while (zp->z_id != sdzp->z_id); 3285789Sahrens 3286789Sahrens return (0); 3287789Sahrens } 3288789Sahrens 3289789Sahrens /* 3290789Sahrens * Move an entry from the provided source directory to the target 3291789Sahrens * directory. Change the entry name as indicated. 3292789Sahrens * 3293789Sahrens * IN: sdvp - Source directory containing the "old entry". 3294789Sahrens * snm - Old entry name. 3295789Sahrens * tdvp - Target directory to contain the "new entry". 3296789Sahrens * tnm - New entry name. 3297789Sahrens * cr - credentials of caller. 32985331Samw * ct - caller context 32995331Samw * flags - case flags 3300789Sahrens * 3301789Sahrens * RETURN: 0 if success 3302789Sahrens * error code if failure 3303789Sahrens * 3304789Sahrens * Timestamps: 3305789Sahrens * sdvp,tdvp - ctime|mtime updated 3306789Sahrens */ 33075331Samw /*ARGSUSED*/ 3308789Sahrens static int 33095331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 33105331Samw caller_context_t *ct, int flags) 3311789Sahrens { 3312789Sahrens znode_t *tdzp, *szp, *tzp; 3313789Sahrens znode_t *sdzp = VTOZ(sdvp); 3314789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 33155326Sek110237 zilog_t *zilog; 3316789Sahrens vnode_t *realvp; 3317789Sahrens zfs_dirlock_t *sdl, *tdl; 3318789Sahrens dmu_tx_t *tx; 3319789Sahrens zfs_zlock_t *zl; 33205331Samw int cmp, serr, terr; 33215331Samw int error = 0; 33225331Samw int zflg = 0; 3323789Sahrens 33245367Sahrens ZFS_ENTER(zfsvfs); 33255367Sahrens ZFS_VERIFY_ZP(sdzp); 33265326Sek110237 zilog = zfsvfs->z_log; 3327789Sahrens 3328789Sahrens /* 3329789Sahrens * Make sure we have the real vp for the target directory. 3330789Sahrens */ 33315331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3332789Sahrens tdvp = realvp; 3333789Sahrens 333412079SMark.Shellenbaum@Sun.COM if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) { 3335789Sahrens ZFS_EXIT(zfsvfs); 3336789Sahrens return (EXDEV); 3337789Sahrens } 3338789Sahrens 3339789Sahrens tdzp = VTOZ(tdvp); 33405367Sahrens ZFS_VERIFY_ZP(tdzp); 33415498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 33425331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 33435331Samw ZFS_EXIT(zfsvfs); 33445331Samw return (EILSEQ); 33455331Samw } 33465331Samw 33475331Samw if (flags & FIGNORECASE) 33485331Samw zflg |= ZCILOOK; 33495331Samw 3350789Sahrens top: 3351789Sahrens szp = NULL; 3352789Sahrens tzp = NULL; 3353789Sahrens zl = NULL; 3354789Sahrens 3355789Sahrens /* 3356789Sahrens * This is to prevent the creation of links into attribute space 3357789Sahrens * by renaming a linked file into/outof an attribute directory. 3358789Sahrens * See the comment in zfs_link() for why this is considered bad. 3359789Sahrens */ 336011935SMark.Shellenbaum@Sun.COM if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3361789Sahrens ZFS_EXIT(zfsvfs); 3362789Sahrens return (EINVAL); 3363789Sahrens } 3364789Sahrens 3365789Sahrens /* 3366789Sahrens * Lock source and target directory entries. To prevent deadlock, 3367789Sahrens * a lock ordering must be defined. We lock the directory with 3368789Sahrens * the smallest object id first, or if it's a tie, the one with 3369789Sahrens * the lexically first name. 3370789Sahrens */ 3371789Sahrens if (sdzp->z_id < tdzp->z_id) { 3372789Sahrens cmp = -1; 3373789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3374789Sahrens cmp = 1; 3375789Sahrens } else { 33765331Samw /* 33775331Samw * First compare the two name arguments without 33785331Samw * considering any case folding. 33795331Samw */ 33805331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 33815331Samw 33825331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 33835498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3384789Sahrens if (cmp == 0) { 3385789Sahrens /* 3386789Sahrens * POSIX: "If the old argument and the new argument 3387789Sahrens * both refer to links to the same existing file, 3388789Sahrens * the rename() function shall return successfully 3389789Sahrens * and perform no other action." 3390789Sahrens */ 3391789Sahrens ZFS_EXIT(zfsvfs); 3392789Sahrens return (0); 3393789Sahrens } 33945331Samw /* 33955331Samw * If the file system is case-folding, then we may 33965331Samw * have some more checking to do. A case-folding file 33975331Samw * system is either supporting mixed case sensitivity 33985331Samw * access or is completely case-insensitive. Note 33995331Samw * that the file system is always case preserving. 34005331Samw * 34015331Samw * In mixed sensitivity mode case sensitive behavior 34025331Samw * is the default. FIGNORECASE must be used to 34035331Samw * explicitly request case insensitive behavior. 34045331Samw * 34055331Samw * If the source and target names provided differ only 34065331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 34075331Samw * we will treat this as a special case in the 34085331Samw * case-insensitive mode: as long as the source name 34095331Samw * is an exact match, we will allow this to proceed as 34105331Samw * a name-change request. 34115331Samw */ 34125498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 34135498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 34145498Stimh flags & FIGNORECASE)) && 34155331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 34165331Samw &error) == 0) { 34175331Samw /* 34185331Samw * case preserving rename request, require exact 34195331Samw * name matches 34205331Samw */ 34215331Samw zflg |= ZCIEXACT; 34225331Samw zflg &= ~ZCILOOK; 34235331Samw } 3424789Sahrens } 34255331Samw 342611321SSanjeev.Bagewadi@Sun.COM /* 342711321SSanjeev.Bagewadi@Sun.COM * If the source and destination directories are the same, we should 342811321SSanjeev.Bagewadi@Sun.COM * grab the z_name_lock of that directory only once. 342911321SSanjeev.Bagewadi@Sun.COM */ 343011321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) { 343111321SSanjeev.Bagewadi@Sun.COM zflg |= ZHAVELOCK; 343211321SSanjeev.Bagewadi@Sun.COM rw_enter(&sdzp->z_name_lock, RW_READER); 343311321SSanjeev.Bagewadi@Sun.COM } 343411321SSanjeev.Bagewadi@Sun.COM 3435789Sahrens if (cmp < 0) { 34365331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 34375331Samw ZEXISTS | zflg, NULL, NULL); 34385331Samw terr = zfs_dirent_lock(&tdl, 34395331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3440789Sahrens } else { 34415331Samw terr = zfs_dirent_lock(&tdl, 34425331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 34435331Samw serr = zfs_dirent_lock(&sdl, 34445331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 34455331Samw NULL, NULL); 3446789Sahrens } 3447789Sahrens 3448789Sahrens if (serr) { 3449789Sahrens /* 3450789Sahrens * Source entry invalid or not there. 3451789Sahrens */ 3452789Sahrens if (!terr) { 3453789Sahrens zfs_dirent_unlock(tdl); 3454789Sahrens if (tzp) 3455789Sahrens VN_RELE(ZTOV(tzp)); 3456789Sahrens } 345711321SSanjeev.Bagewadi@Sun.COM 345811321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 345911321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 346011321SSanjeev.Bagewadi@Sun.COM 3461789Sahrens if (strcmp(snm, "..") == 0) 3462789Sahrens serr = EINVAL; 3463789Sahrens ZFS_EXIT(zfsvfs); 3464789Sahrens return (serr); 3465789Sahrens } 3466789Sahrens if (terr) { 3467789Sahrens zfs_dirent_unlock(sdl); 3468789Sahrens VN_RELE(ZTOV(szp)); 346911321SSanjeev.Bagewadi@Sun.COM 347011321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 347111321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 347211321SSanjeev.Bagewadi@Sun.COM 3473789Sahrens if (strcmp(tnm, "..") == 0) 3474789Sahrens terr = EINVAL; 3475789Sahrens ZFS_EXIT(zfsvfs); 3476789Sahrens return (terr); 3477789Sahrens } 3478789Sahrens 3479789Sahrens /* 3480789Sahrens * Must have write access at the source to remove the old entry 3481789Sahrens * and write access at the target to create the new entry. 3482789Sahrens * Note that if target and source are the same, this can be 3483789Sahrens * done in a single check. 3484789Sahrens */ 3485789Sahrens 3486789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3487789Sahrens goto out; 3488789Sahrens 3489789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3490789Sahrens /* 3491789Sahrens * Check to make sure rename is valid. 3492789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3493789Sahrens */ 3494789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3495789Sahrens goto out; 3496789Sahrens } 3497789Sahrens 3498789Sahrens /* 3499789Sahrens * Does target exist? 3500789Sahrens */ 3501789Sahrens if (tzp) { 3502789Sahrens /* 3503789Sahrens * Source and target must be the same type. 3504789Sahrens */ 3505789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3506789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3507789Sahrens error = ENOTDIR; 3508789Sahrens goto out; 3509789Sahrens } 3510789Sahrens } else { 3511789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3512789Sahrens error = EISDIR; 3513789Sahrens goto out; 3514789Sahrens } 3515789Sahrens } 3516789Sahrens /* 3517789Sahrens * POSIX dictates that when the source and target 3518789Sahrens * entries refer to the same file object, rename 3519789Sahrens * must do nothing and exit without error. 3520789Sahrens */ 3521789Sahrens if (szp->z_id == tzp->z_id) { 3522789Sahrens error = 0; 3523789Sahrens goto out; 3524789Sahrens } 3525789Sahrens } 3526789Sahrens 35275331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3528789Sahrens if (tzp) 35295331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 35304863Spraks 35314863Spraks /* 35324863Spraks * notify the target directory if it is not the same 35334863Spraks * as source directory. 35344863Spraks */ 35354863Spraks if (tdvp != sdvp) { 35365331Samw vnevent_rename_dest_dir(tdvp, ct); 35374863Spraks } 3538789Sahrens 3539789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 354011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 354111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 35421544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 35431544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 354411935SMark.Shellenbaum@Sun.COM if (sdzp != tdzp) { 354511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 354611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tdzp); 354711935SMark.Shellenbaum@Sun.COM } 354811935SMark.Shellenbaum@Sun.COM if (tzp) { 354911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 355011935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tzp); 355111935SMark.Shellenbaum@Sun.COM } 355211935SMark.Shellenbaum@Sun.COM 355311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 35543461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 35558227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3556789Sahrens if (error) { 3557789Sahrens if (zl != NULL) 3558789Sahrens zfs_rename_unlock(&zl); 3559789Sahrens zfs_dirent_unlock(sdl); 3560789Sahrens zfs_dirent_unlock(tdl); 356111321SSanjeev.Bagewadi@Sun.COM 356211321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 356311321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 356411321SSanjeev.Bagewadi@Sun.COM 3565789Sahrens VN_RELE(ZTOV(szp)); 3566789Sahrens if (tzp) 3567789Sahrens VN_RELE(ZTOV(tzp)); 35688227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35692113Sahrens dmu_tx_wait(tx); 35702113Sahrens dmu_tx_abort(tx); 3571789Sahrens goto top; 3572789Sahrens } 35732113Sahrens dmu_tx_abort(tx); 3574789Sahrens ZFS_EXIT(zfsvfs); 3575789Sahrens return (error); 3576789Sahrens } 3577789Sahrens 3578789Sahrens if (tzp) /* Attempt to remove the existing target */ 35795331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3580789Sahrens 3581789Sahrens if (error == 0) { 3582789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3583789Sahrens if (error == 0) { 358411935SMark.Shellenbaum@Sun.COM szp->z_pflags |= ZFS_AV_MODIFIED; 358511935SMark.Shellenbaum@Sun.COM 358611935SMark.Shellenbaum@Sun.COM error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 358711935SMark.Shellenbaum@Sun.COM (void *)&szp->z_pflags, sizeof (uint64_t), tx); 358811935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 35895331Samw 3590789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 359112413SSam.Falkner@Sun.COM if (error == 0) { 359212413SSam.Falkner@Sun.COM zfs_log_rename(zilog, tx, TX_RENAME | 359312771SNeil.Perrin@Sun.COM (flags & FIGNORECASE ? TX_CI : 0), sdzp, 359412771SNeil.Perrin@Sun.COM sdl->dl_name, tdzp, tdl->dl_name, szp); 359512413SSam.Falkner@Sun.COM 359612413SSam.Falkner@Sun.COM /* 359712413SSam.Falkner@Sun.COM * Update path information for the target vnode 359812413SSam.Falkner@Sun.COM */ 359912413SSam.Falkner@Sun.COM vn_renamepath(tdvp, ZTOV(szp), tnm, 360012413SSam.Falkner@Sun.COM strlen(tnm)); 360112413SSam.Falkner@Sun.COM } else { 360212413SSam.Falkner@Sun.COM /* 360312413SSam.Falkner@Sun.COM * At this point, we have successfully created 360412413SSam.Falkner@Sun.COM * the target name, but have failed to remove 360512413SSam.Falkner@Sun.COM * the source name. Since the create was done 360612413SSam.Falkner@Sun.COM * with the ZRENAMING flag, there are 360712413SSam.Falkner@Sun.COM * complications; for one, the link count is 360812413SSam.Falkner@Sun.COM * wrong. The easiest way to deal with this 360912413SSam.Falkner@Sun.COM * is to remove the newly created target, and 361012413SSam.Falkner@Sun.COM * return the original error. This must 361112413SSam.Falkner@Sun.COM * succeed; fortunately, it is very unlikely to 361212413SSam.Falkner@Sun.COM * fail, since we just created it. 361312413SSam.Falkner@Sun.COM */ 361412413SSam.Falkner@Sun.COM VERIFY3U(zfs_link_destroy(tdl, szp, tx, 361512413SSam.Falkner@Sun.COM ZRENAMING, NULL), ==, 0); 361612413SSam.Falkner@Sun.COM } 3617789Sahrens } 3618789Sahrens } 3619789Sahrens 3620789Sahrens dmu_tx_commit(tx); 3621789Sahrens out: 3622789Sahrens if (zl != NULL) 3623789Sahrens zfs_rename_unlock(&zl); 3624789Sahrens 3625789Sahrens zfs_dirent_unlock(sdl); 3626789Sahrens zfs_dirent_unlock(tdl); 3627789Sahrens 362811321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 362911321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 363011321SSanjeev.Bagewadi@Sun.COM 363111321SSanjeev.Bagewadi@Sun.COM 3632789Sahrens VN_RELE(ZTOV(szp)); 3633789Sahrens if (tzp) 3634789Sahrens VN_RELE(ZTOV(tzp)); 3635789Sahrens 363612294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 363712699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 363812294SMark.Musante@Sun.COM 3639789Sahrens ZFS_EXIT(zfsvfs); 3640789Sahrens return (error); 3641789Sahrens } 3642789Sahrens 3643789Sahrens /* 3644789Sahrens * Insert the indicated symbolic reference entry into the directory. 3645789Sahrens * 3646789Sahrens * IN: dvp - Directory to contain new symbolic link. 3647789Sahrens * link - Name for new symlink entry. 3648789Sahrens * vap - Attributes of new entry. 3649789Sahrens * target - Target path of new symlink. 3650789Sahrens * cr - credentials of caller. 36515331Samw * ct - caller context 36525331Samw * flags - case flags 3653789Sahrens * 3654789Sahrens * RETURN: 0 if success 3655789Sahrens * error code if failure 3656789Sahrens * 3657789Sahrens * Timestamps: 3658789Sahrens * dvp - ctime|mtime updated 3659789Sahrens */ 36605331Samw /*ARGSUSED*/ 3661789Sahrens static int 36625331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 36635331Samw caller_context_t *ct, int flags) 3664789Sahrens { 3665789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3666789Sahrens zfs_dirlock_t *dl; 3667789Sahrens dmu_tx_t *tx; 3668789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 36695326Sek110237 zilog_t *zilog; 367011935SMark.Shellenbaum@Sun.COM uint64_t len = strlen(link); 3671789Sahrens int error; 36725331Samw int zflg = ZNEW; 36739179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 36749179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 367511935SMark.Shellenbaum@Sun.COM uint64_t txtype = TX_SYMLINK; 3676789Sahrens 3677789Sahrens ASSERT(vap->va_type == VLNK); 3678789Sahrens 36795367Sahrens ZFS_ENTER(zfsvfs); 36805367Sahrens ZFS_VERIFY_ZP(dzp); 36815326Sek110237 zilog = zfsvfs->z_log; 36825331Samw 36835498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 36845331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 36855331Samw ZFS_EXIT(zfsvfs); 36865331Samw return (EILSEQ); 36875331Samw } 36885331Samw if (flags & FIGNORECASE) 36895331Samw zflg |= ZCILOOK; 3690789Sahrens 3691789Sahrens if (len > MAXPATHLEN) { 3692789Sahrens ZFS_EXIT(zfsvfs); 3693789Sahrens return (ENAMETOOLONG); 3694789Sahrens } 3695789Sahrens 369612302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, 369712302SMark.Shellenbaum@Sun.COM vap, cr, NULL, &acl_ids)) != 0) { 369812302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 369912302SMark.Shellenbaum@Sun.COM return (error); 370012302SMark.Shellenbaum@Sun.COM } 370112302SMark.Shellenbaum@Sun.COM top: 3702789Sahrens /* 3703789Sahrens * Attempt to lock directory; fail if entry already exists. 3704789Sahrens */ 37055331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 37065331Samw if (error) { 370712302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3708789Sahrens ZFS_EXIT(zfsvfs); 3709789Sahrens return (error); 3710789Sahrens } 3711789Sahrens 371212302SMark.Shellenbaum@Sun.COM if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 371312302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 371412421SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 371512302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 371612302SMark.Shellenbaum@Sun.COM return (error); 371712302SMark.Shellenbaum@Sun.COM } 371812302SMark.Shellenbaum@Sun.COM 37199396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 37209396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 37219396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 37229396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 37239396SMatthew.Ahrens@Sun.COM return (EDQUOT); 37249396SMatthew.Ahrens@Sun.COM } 3725789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 37269179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3727789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 37281544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 372911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 373011935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE + len); 373111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 373211935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 373311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 373411935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 373511935SMark.Shellenbaum@Sun.COM } 37369396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 37379396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 37388227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3739789Sahrens if (error) { 3740789Sahrens zfs_dirent_unlock(dl); 37418227SNeil.Perrin@Sun.COM if (error == ERESTART) { 37422113Sahrens dmu_tx_wait(tx); 37432113Sahrens dmu_tx_abort(tx); 3744789Sahrens goto top; 3745789Sahrens } 374612302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 37472113Sahrens dmu_tx_abort(tx); 3748789Sahrens ZFS_EXIT(zfsvfs); 3749789Sahrens return (error); 3750789Sahrens } 3751789Sahrens 3752789Sahrens /* 3753789Sahrens * Create a new object for the symlink. 375411935SMark.Shellenbaum@Sun.COM * for version 4 ZPL datsets the symlink will be an SA attribute 3755789Sahrens */ 375611935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 375711935SMark.Shellenbaum@Sun.COM 375811935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 375911935SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 376011935SMark.Shellenbaum@Sun.COM 376112620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 376211935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 376311935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 376411935SMark.Shellenbaum@Sun.COM link, len, tx); 376511935SMark.Shellenbaum@Sun.COM else 376611935SMark.Shellenbaum@Sun.COM zfs_sa_symlink(zp, link, len, tx); 376712620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 376811935SMark.Shellenbaum@Sun.COM 376911935SMark.Shellenbaum@Sun.COM zp->z_size = len; 377011935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 377111935SMark.Shellenbaum@Sun.COM &zp->z_size, sizeof (zp->z_size), tx); 3772789Sahrens /* 3773789Sahrens * Insert the new object into the directory. 3774789Sahrens */ 3775789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 377611935SMark.Shellenbaum@Sun.COM 377711935SMark.Shellenbaum@Sun.COM if (flags & FIGNORECASE) 377811935SMark.Shellenbaum@Sun.COM txtype |= TX_CI; 377911935SMark.Shellenbaum@Sun.COM zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 37809179SMark.Shellenbaum@Sun.COM 37819179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3782789Sahrens 3783789Sahrens dmu_tx_commit(tx); 3784789Sahrens 3785789Sahrens zfs_dirent_unlock(dl); 3786789Sahrens 3787789Sahrens VN_RELE(ZTOV(zp)); 3788789Sahrens 378912294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 379012699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 379112294SMark.Musante@Sun.COM 3792789Sahrens ZFS_EXIT(zfsvfs); 3793789Sahrens return (error); 3794789Sahrens } 3795789Sahrens 3796789Sahrens /* 3797789Sahrens * Return, in the buffer contained in the provided uio structure, 3798789Sahrens * the symbolic path referred to by vp. 3799789Sahrens * 3800789Sahrens * IN: vp - vnode of symbolic link. 3801789Sahrens * uoip - structure to contain the link path. 3802789Sahrens * cr - credentials of caller. 38035331Samw * ct - caller context 3804789Sahrens * 3805789Sahrens * OUT: uio - structure to contain the link path. 3806789Sahrens * 3807789Sahrens * RETURN: 0 if success 3808789Sahrens * error code if failure 3809789Sahrens * 3810789Sahrens * Timestamps: 3811789Sahrens * vp - atime updated 3812789Sahrens */ 3813789Sahrens /* ARGSUSED */ 3814789Sahrens static int 38155331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3816789Sahrens { 3817789Sahrens znode_t *zp = VTOZ(vp); 3818789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3819789Sahrens int error; 3820789Sahrens 38215367Sahrens ZFS_ENTER(zfsvfs); 38225367Sahrens ZFS_VERIFY_ZP(zp); 3823789Sahrens 382412620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 382511935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 382611935SMark.Shellenbaum@Sun.COM error = sa_lookup_uio(zp->z_sa_hdl, 382711935SMark.Shellenbaum@Sun.COM SA_ZPL_SYMLINK(zfsvfs), uio); 382811935SMark.Shellenbaum@Sun.COM else 382911935SMark.Shellenbaum@Sun.COM error = zfs_sa_readlink(zp, uio); 383012620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 3831789Sahrens 3832789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 383311935SMark.Shellenbaum@Sun.COM 3834789Sahrens ZFS_EXIT(zfsvfs); 3835789Sahrens return (error); 3836789Sahrens } 3837789Sahrens 3838789Sahrens /* 3839789Sahrens * Insert a new entry into directory tdvp referencing svp. 3840789Sahrens * 3841789Sahrens * IN: tdvp - Directory to contain new entry. 3842789Sahrens * svp - vnode of new entry. 3843789Sahrens * name - name of new entry. 3844789Sahrens * cr - credentials of caller. 38455331Samw * ct - caller context 3846789Sahrens * 3847789Sahrens * RETURN: 0 if success 3848789Sahrens * error code if failure 3849789Sahrens * 3850789Sahrens * Timestamps: 3851789Sahrens * tdvp - ctime|mtime updated 3852789Sahrens * svp - ctime updated 3853789Sahrens */ 3854789Sahrens /* ARGSUSED */ 3855789Sahrens static int 38565331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 38575331Samw caller_context_t *ct, int flags) 3858789Sahrens { 3859789Sahrens znode_t *dzp = VTOZ(tdvp); 3860789Sahrens znode_t *tzp, *szp; 3861789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 38625326Sek110237 zilog_t *zilog; 3863789Sahrens zfs_dirlock_t *dl; 3864789Sahrens dmu_tx_t *tx; 3865789Sahrens vnode_t *realvp; 3866789Sahrens int error; 38675331Samw int zf = ZNEW; 386812079SMark.Shellenbaum@Sun.COM uint64_t parent; 3869789Sahrens 3870789Sahrens ASSERT(tdvp->v_type == VDIR); 3871789Sahrens 38725367Sahrens ZFS_ENTER(zfsvfs); 38735367Sahrens ZFS_VERIFY_ZP(dzp); 38745326Sek110237 zilog = zfsvfs->z_log; 3875789Sahrens 38765331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3877789Sahrens svp = realvp; 3878789Sahrens 387912079SMark.Shellenbaum@Sun.COM /* 388012079SMark.Shellenbaum@Sun.COM * POSIX dictates that we return EPERM here. 388112079SMark.Shellenbaum@Sun.COM * Better choices include ENOTSUP or EISDIR. 388212079SMark.Shellenbaum@Sun.COM */ 388312079SMark.Shellenbaum@Sun.COM if (svp->v_type == VDIR) { 388412079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 388512079SMark.Shellenbaum@Sun.COM return (EPERM); 388612079SMark.Shellenbaum@Sun.COM } 388712079SMark.Shellenbaum@Sun.COM 388812079SMark.Shellenbaum@Sun.COM if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) { 3889789Sahrens ZFS_EXIT(zfsvfs); 3890789Sahrens return (EXDEV); 3891789Sahrens } 389212079SMark.Shellenbaum@Sun.COM 38935367Sahrens szp = VTOZ(svp); 38945367Sahrens ZFS_VERIFY_ZP(szp); 3895789Sahrens 389612079SMark.Shellenbaum@Sun.COM /* Prevent links to .zfs/shares files */ 389712079SMark.Shellenbaum@Sun.COM 389812079SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 389912079SMark.Shellenbaum@Sun.COM &parent, sizeof (uint64_t))) != 0) { 390012079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 390112079SMark.Shellenbaum@Sun.COM return (error); 390212079SMark.Shellenbaum@Sun.COM } 390312079SMark.Shellenbaum@Sun.COM if (parent == zfsvfs->z_shares_dir) { 390412079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 390512079SMark.Shellenbaum@Sun.COM return (EPERM); 390612079SMark.Shellenbaum@Sun.COM } 390712079SMark.Shellenbaum@Sun.COM 39085498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 39095331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 39105331Samw ZFS_EXIT(zfsvfs); 39115331Samw return (EILSEQ); 39125331Samw } 39135331Samw if (flags & FIGNORECASE) 39145331Samw zf |= ZCILOOK; 39155331Samw 3916789Sahrens /* 3917789Sahrens * We do not support links between attributes and non-attributes 3918789Sahrens * because of the potential security risk of creating links 3919789Sahrens * into "normal" file space in order to circumvent restrictions 3920789Sahrens * imposed in attribute space. 3921789Sahrens */ 392211935SMark.Shellenbaum@Sun.COM if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 3923789Sahrens ZFS_EXIT(zfsvfs); 3924789Sahrens return (EINVAL); 3925789Sahrens } 3926789Sahrens 3927789Sahrens 392811935SMark.Shellenbaum@Sun.COM if (szp->z_uid != crgetuid(cr) && 3929789Sahrens secpolicy_basic_link(cr) != 0) { 3930789Sahrens ZFS_EXIT(zfsvfs); 3931789Sahrens return (EPERM); 3932789Sahrens } 3933789Sahrens 39345331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3935789Sahrens ZFS_EXIT(zfsvfs); 3936789Sahrens return (error); 3937789Sahrens } 3938789Sahrens 393912079SMark.Shellenbaum@Sun.COM top: 3940789Sahrens /* 3941789Sahrens * Attempt to lock directory; fail if entry already exists. 3942789Sahrens */ 39435331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 39445331Samw if (error) { 3945789Sahrens ZFS_EXIT(zfsvfs); 3946789Sahrens return (error); 3947789Sahrens } 3948789Sahrens 3949789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 395011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 39511544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 395211935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 395311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 39548227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3955789Sahrens if (error) { 3956789Sahrens zfs_dirent_unlock(dl); 39578227SNeil.Perrin@Sun.COM if (error == ERESTART) { 39582113Sahrens dmu_tx_wait(tx); 39592113Sahrens dmu_tx_abort(tx); 3960789Sahrens goto top; 3961789Sahrens } 39622113Sahrens dmu_tx_abort(tx); 3963789Sahrens ZFS_EXIT(zfsvfs); 3964789Sahrens return (error); 3965789Sahrens } 3966789Sahrens 3967789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3968789Sahrens 39695331Samw if (error == 0) { 39705331Samw uint64_t txtype = TX_LINK; 39715331Samw if (flags & FIGNORECASE) 39725331Samw txtype |= TX_CI; 39735331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 39745331Samw } 3975789Sahrens 3976789Sahrens dmu_tx_commit(tx); 3977789Sahrens 3978789Sahrens zfs_dirent_unlock(dl); 3979789Sahrens 39804863Spraks if (error == 0) { 39815331Samw vnevent_link(svp, ct); 39824863Spraks } 39834863Spraks 398412294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 398512699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 398612294SMark.Musante@Sun.COM 3987789Sahrens ZFS_EXIT(zfsvfs); 3988789Sahrens return (error); 3989789Sahrens } 3990789Sahrens 3991789Sahrens /* 3992789Sahrens * zfs_null_putapage() is used when the file system has been force 3993789Sahrens * unmounted. It just drops the pages. 3994789Sahrens */ 3995789Sahrens /* ARGSUSED */ 3996789Sahrens static int 3997789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3998789Sahrens size_t *lenp, int flags, cred_t *cr) 3999789Sahrens { 4000789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 4001789Sahrens return (0); 4002789Sahrens } 4003789Sahrens 40042688Smaybee /* 40052688Smaybee * Push a page out to disk, klustering if possible. 40062688Smaybee * 40072688Smaybee * IN: vp - file to push page to. 40082688Smaybee * pp - page to push. 40092688Smaybee * flags - additional flags. 40102688Smaybee * cr - credentials of caller. 40112688Smaybee * 40122688Smaybee * OUT: offp - start of range pushed. 40132688Smaybee * lenp - len of range pushed. 40142688Smaybee * 40152688Smaybee * RETURN: 0 if success 40162688Smaybee * error code if failure 40172688Smaybee * 40182688Smaybee * NOTE: callers must have locked the page to be pushed. On 40192688Smaybee * exit, the page (and all other pages in the kluster) must be 40202688Smaybee * unlocked. 40212688Smaybee */ 4022789Sahrens /* ARGSUSED */ 4023789Sahrens static int 4024789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4025789Sahrens size_t *lenp, int flags, cred_t *cr) 4026789Sahrens { 4027789Sahrens znode_t *zp = VTOZ(vp); 4028789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4029789Sahrens dmu_tx_t *tx; 40302688Smaybee u_offset_t off, koff; 40312688Smaybee size_t len, klen; 4032789Sahrens int err; 4033789Sahrens 40342688Smaybee off = pp->p_offset; 40352688Smaybee len = PAGESIZE; 40362688Smaybee /* 40372688Smaybee * If our blocksize is bigger than the page size, try to kluster 40388227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 40392688Smaybee * a read-modify-write). 40402688Smaybee */ 404111935SMark.Shellenbaum@Sun.COM if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 40428636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 40438636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 404411935SMark.Shellenbaum@Sun.COM ASSERT(koff <= zp->z_size); 404511935SMark.Shellenbaum@Sun.COM if (koff + klen > zp->z_size) 404611935SMark.Shellenbaum@Sun.COM klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 40472688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 40482688Smaybee } 40492688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 40508636SMark.Maybee@Sun.COM 40511819Smaybee /* 40521819Smaybee * Can't push pages past end-of-file. 40531819Smaybee */ 405411935SMark.Shellenbaum@Sun.COM if (off >= zp->z_size) { 40554709Smaybee /* ignore all pages */ 40562688Smaybee err = 0; 40572688Smaybee goto out; 405811935SMark.Shellenbaum@Sun.COM } else if (off + len > zp->z_size) { 405911935SMark.Shellenbaum@Sun.COM int npages = btopr(zp->z_size - off); 40602688Smaybee page_t *trunc; 40612688Smaybee 40622688Smaybee page_list_break(&pp, &trunc, npages); 40634709Smaybee /* ignore pages past end of file */ 40642688Smaybee if (trunc) 40654709Smaybee pvn_write_done(trunc, flags); 406611935SMark.Shellenbaum@Sun.COM len = zp->z_size - off; 40671819Smaybee } 40689396SMatthew.Ahrens@Sun.COM 406911935SMark.Shellenbaum@Sun.COM if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 407011935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 40719396SMatthew.Ahrens@Sun.COM err = EDQUOT; 40729396SMatthew.Ahrens@Sun.COM goto out; 40739396SMatthew.Ahrens@Sun.COM } 40748636SMark.Maybee@Sun.COM top: 4075789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 4076789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 407711935SMark.Shellenbaum@Sun.COM 407811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 407911935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 40808227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 4081789Sahrens if (err != 0) { 40828227SNeil.Perrin@Sun.COM if (err == ERESTART) { 40832113Sahrens dmu_tx_wait(tx); 40842113Sahrens dmu_tx_abort(tx); 4085789Sahrens goto top; 4086789Sahrens } 40872113Sahrens dmu_tx_abort(tx); 4088789Sahrens goto out; 4089789Sahrens } 4090789Sahrens 40912688Smaybee if (zp->z_blksz <= PAGESIZE) { 40927315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 40932688Smaybee ASSERT3U(len, <=, PAGESIZE); 40942688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 40957315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 40962688Smaybee } else { 40972688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 40982688Smaybee } 40992688Smaybee 41002688Smaybee if (err == 0) { 410111935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 410212394SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[3]; 410311935SMark.Shellenbaum@Sun.COM int count = 0; 410411935SMark.Shellenbaum@Sun.COM 410511935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 410611935SMark.Shellenbaum@Sun.COM &mtime, 16); 410711935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 410811935SMark.Shellenbaum@Sun.COM &ctime, 16); 410912394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 411012394SMark.Shellenbaum@Sun.COM &zp->z_pflags, 8); 411111935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 411211935SMark.Shellenbaum@Sun.COM B_TRUE); 41138636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 41142688Smaybee } 41159951SLin.Ling@Sun.COM dmu_tx_commit(tx); 41162688Smaybee 41172688Smaybee out: 41184709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4119789Sahrens if (offp) 4120789Sahrens *offp = off; 4121789Sahrens if (lenp) 4122789Sahrens *lenp = len; 4123789Sahrens 4124789Sahrens return (err); 4125789Sahrens } 4126789Sahrens 4127789Sahrens /* 4128789Sahrens * Copy the portion of the file indicated from pages into the file. 4129789Sahrens * The pages are stored in a page list attached to the files vnode. 4130789Sahrens * 4131789Sahrens * IN: vp - vnode of file to push page data to. 4132789Sahrens * off - position in file to put data. 4133789Sahrens * len - amount of data to write. 4134789Sahrens * flags - flags to control the operation. 4135789Sahrens * cr - credentials of caller. 41365331Samw * ct - caller context. 4137789Sahrens * 4138789Sahrens * RETURN: 0 if success 4139789Sahrens * error code if failure 4140789Sahrens * 4141789Sahrens * Timestamps: 4142789Sahrens * vp - ctime|mtime updated 4143789Sahrens */ 41445331Samw /*ARGSUSED*/ 4145789Sahrens static int 41465331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 41475331Samw caller_context_t *ct) 4148789Sahrens { 4149789Sahrens znode_t *zp = VTOZ(vp); 4150789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4151789Sahrens page_t *pp; 4152789Sahrens size_t io_len; 4153789Sahrens u_offset_t io_off; 41548636SMark.Maybee@Sun.COM uint_t blksz; 41558636SMark.Maybee@Sun.COM rl_t *rl; 4156789Sahrens int error = 0; 4157789Sahrens 41585367Sahrens ZFS_ENTER(zfsvfs); 41595367Sahrens ZFS_VERIFY_ZP(zp); 4160789Sahrens 41618636SMark.Maybee@Sun.COM /* 41628636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 41638636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 41648636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 41658636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 41668636SMark.Maybee@Sun.COM * operations to get narrower locking. 41678636SMark.Maybee@Sun.COM */ 41688636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 41698636SMark.Maybee@Sun.COM if (ISP2(blksz)) 41708636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 41718636SMark.Maybee@Sun.COM else 41728636SMark.Maybee@Sun.COM io_off = 0; 41738636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 41749141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 41758636SMark.Maybee@Sun.COM else 41768636SMark.Maybee@Sun.COM io_len = 0; 41778636SMark.Maybee@Sun.COM 41788636SMark.Maybee@Sun.COM if (io_len == 0) { 4179789Sahrens /* 41808636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 4181789Sahrens */ 41828636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 41838636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 41841472Sperrin goto out; 4185789Sahrens } 41868636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 41878636SMark.Maybee@Sun.COM 418811935SMark.Shellenbaum@Sun.COM if (off > zp->z_size) { 4189789Sahrens /* past end of file */ 41908636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 4191789Sahrens ZFS_EXIT(zfsvfs); 4192789Sahrens return (0); 4193789Sahrens } 4194789Sahrens 419511935SMark.Shellenbaum@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 41968636SMark.Maybee@Sun.COM 41978636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 4198789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 41991669Sperrin pp = page_lookup(vp, io_off, 42004339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4201789Sahrens } else { 4202789Sahrens pp = page_lookup_nowait(vp, io_off, 42034339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4204789Sahrens } 4205789Sahrens 4206789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 4207789Sahrens int err; 4208789Sahrens 4209789Sahrens /* 4210789Sahrens * Found a dirty page to push 4211789Sahrens */ 42121669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 42131669Sperrin if (err) 4214789Sahrens error = err; 4215789Sahrens } else { 4216789Sahrens io_len = PAGESIZE; 4217789Sahrens } 4218789Sahrens } 42191472Sperrin out: 42208636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 422112294SMark.Musante@Sun.COM if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 422212699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 4223789Sahrens ZFS_EXIT(zfsvfs); 4224789Sahrens return (error); 4225789Sahrens } 4226789Sahrens 42275331Samw /*ARGSUSED*/ 4228789Sahrens void 42295331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4230789Sahrens { 4231789Sahrens znode_t *zp = VTOZ(vp); 4232789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4233789Sahrens int error; 4234789Sahrens 42355326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 423611935SMark.Shellenbaum@Sun.COM if (zp->z_sa_hdl == NULL) { 42375446Sahrens /* 42385642Smaybee * The fs has been unmounted, or we did a 42395642Smaybee * suspend/resume and this file no longer exists. 42405446Sahrens */ 4241789Sahrens if (vn_has_cached_data(vp)) { 4242789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 4243789Sahrens B_INVAL, cr); 4244789Sahrens } 4245789Sahrens 42461544Seschrock mutex_enter(&zp->z_lock); 424710369Schris.kirby@sun.com mutex_enter(&vp->v_lock); 424810369Schris.kirby@sun.com ASSERT(vp->v_count == 1); 424910369Schris.kirby@sun.com vp->v_count = 0; 425010369Schris.kirby@sun.com mutex_exit(&vp->v_lock); 42515446Sahrens mutex_exit(&zp->z_lock); 42525642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 42535446Sahrens zfs_znode_free(zp); 4254789Sahrens return; 4255789Sahrens } 4256789Sahrens 4257789Sahrens /* 4258789Sahrens * Attempt to push any data in the page cache. If this fails 4259789Sahrens * we will get kicked out later in zfs_zinactive(). 4260789Sahrens */ 42611298Sperrin if (vn_has_cached_data(vp)) { 42621298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 42631298Sperrin cr); 42641298Sperrin } 4265789Sahrens 42663461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4267789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4268789Sahrens 426911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 427011935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 4271789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 4272789Sahrens if (error) { 4273789Sahrens dmu_tx_abort(tx); 4274789Sahrens } else { 4275789Sahrens mutex_enter(&zp->z_lock); 427611935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 427711935SMark.Shellenbaum@Sun.COM (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4278789Sahrens zp->z_atime_dirty = 0; 4279789Sahrens mutex_exit(&zp->z_lock); 4280789Sahrens dmu_tx_commit(tx); 4281789Sahrens } 4282789Sahrens } 4283789Sahrens 4284789Sahrens zfs_zinactive(zp); 42855326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4286789Sahrens } 4287789Sahrens 4288789Sahrens /* 4289789Sahrens * Bounds-check the seek operation. 4290789Sahrens * 4291789Sahrens * IN: vp - vnode seeking within 4292789Sahrens * ooff - old file offset 4293789Sahrens * noffp - pointer to new file offset 42945331Samw * ct - caller context 4295789Sahrens * 4296789Sahrens * RETURN: 0 if success 4297789Sahrens * EINVAL if new offset invalid 4298789Sahrens */ 4299789Sahrens /* ARGSUSED */ 4300789Sahrens static int 43015331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 43025331Samw caller_context_t *ct) 4303789Sahrens { 4304789Sahrens if (vp->v_type == VDIR) 4305789Sahrens return (0); 4306789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4307789Sahrens } 4308789Sahrens 4309789Sahrens /* 4310789Sahrens * Pre-filter the generic locking function to trap attempts to place 4311789Sahrens * a mandatory lock on a memory mapped file. 4312789Sahrens */ 4313789Sahrens static int 4314789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 43155331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4316789Sahrens { 4317789Sahrens znode_t *zp = VTOZ(vp); 4318789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4319789Sahrens 43205367Sahrens ZFS_ENTER(zfsvfs); 43215367Sahrens ZFS_VERIFY_ZP(zp); 4322789Sahrens 4323789Sahrens /* 43241544Seschrock * We are following the UFS semantics with respect to mapcnt 43251544Seschrock * here: If we see that the file is mapped already, then we will 43261544Seschrock * return an error, but we don't worry about races between this 43271544Seschrock * function and zfs_map(). 4328789Sahrens */ 432911935SMark.Shellenbaum@Sun.COM if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4330789Sahrens ZFS_EXIT(zfsvfs); 4331789Sahrens return (EAGAIN); 4332789Sahrens } 4333789Sahrens ZFS_EXIT(zfsvfs); 433410896SMark.Shellenbaum@Sun.COM return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4335789Sahrens } 4336789Sahrens 4337789Sahrens /* 4338789Sahrens * If we can't find a page in the cache, we will create a new page 4339789Sahrens * and fill it with file data. For efficiency, we may try to fill 43408636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 43419265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 43429265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4343789Sahrens */ 4344789Sahrens static int 4345789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4346789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4347789Sahrens { 4348789Sahrens znode_t *zp = VTOZ(vp); 4349789Sahrens page_t *pp, *cur_pp; 4350789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4351789Sahrens u_offset_t io_off, total; 4352789Sahrens size_t io_len; 4353789Sahrens int err; 4354789Sahrens 43552688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 43568636SMark.Maybee@Sun.COM /* 43578636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 43588636SMark.Maybee@Sun.COM */ 4359789Sahrens io_off = off; 4360789Sahrens io_len = PAGESIZE; 43619265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 43629265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4363789Sahrens } else { 4364789Sahrens /* 43658636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4366789Sahrens */ 4367789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 43688636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4369789Sahrens } 4370789Sahrens if (pp == NULL) { 4371789Sahrens /* 43728636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4373789Sahrens */ 4374789Sahrens *pl = NULL; 4375789Sahrens return (0); 4376789Sahrens } 4377789Sahrens 4378789Sahrens /* 4379789Sahrens * Fill the pages in the kluster. 4380789Sahrens */ 4381789Sahrens cur_pp = pp; 4382789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 43838636SMark.Maybee@Sun.COM caddr_t va; 43848636SMark.Maybee@Sun.COM 43852688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 43867315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 43879512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 43889512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 43897315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4390789Sahrens if (err) { 4391789Sahrens /* On error, toss the entire kluster */ 4392789Sahrens pvn_read_done(pp, B_ERROR); 43937294Sperrin /* convert checksum errors into IO errors */ 43947294Sperrin if (err == ECKSUM) 43957294Sperrin err = EIO; 4396789Sahrens return (err); 4397789Sahrens } 4398789Sahrens cur_pp = cur_pp->p_next; 4399789Sahrens } 44008636SMark.Maybee@Sun.COM 4401789Sahrens /* 44028636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 44038636SMark.Maybee@Sun.COM * from the desired offset `off'. 4404789Sahrens * NOTE: the page list will always be null terminated. 4405789Sahrens */ 4406789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 44078636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4408789Sahrens 4409789Sahrens return (0); 4410789Sahrens } 4411789Sahrens 4412789Sahrens /* 4413789Sahrens * Return pointers to the pages for the file region [off, off + len] 4414789Sahrens * in the pl array. If plsz is greater than len, this function may 44158636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 44168636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 44178636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 44188636SMark.Maybee@Sun.COM * part of a klustered read. 4419789Sahrens * 4420789Sahrens * IN: vp - vnode of file to get data from. 4421789Sahrens * off - position in file to get data from. 4422789Sahrens * len - amount of data to retrieve. 4423789Sahrens * plsz - length of provided page list. 4424789Sahrens * seg - segment to obtain pages for. 4425789Sahrens * addr - virtual address of fault. 4426789Sahrens * rw - mode of created pages. 4427789Sahrens * cr - credentials of caller. 44285331Samw * ct - caller context. 4429789Sahrens * 4430789Sahrens * OUT: protp - protection mode of created pages. 4431789Sahrens * pl - list of pages created. 4432789Sahrens * 4433789Sahrens * RETURN: 0 if success 4434789Sahrens * error code if failure 4435789Sahrens * 4436789Sahrens * Timestamps: 4437789Sahrens * vp - atime updated 4438789Sahrens */ 4439789Sahrens /* ARGSUSED */ 4440789Sahrens static int 4441789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4442789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 44435331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4444789Sahrens { 4445789Sahrens znode_t *zp = VTOZ(vp); 4446789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 44478636SMark.Maybee@Sun.COM page_t **pl0 = pl; 44488636SMark.Maybee@Sun.COM int err = 0; 44498636SMark.Maybee@Sun.COM 44508636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 44518636SMark.Maybee@Sun.COM if (pl == NULL) 44528636SMark.Maybee@Sun.COM return (0); 44538636SMark.Maybee@Sun.COM else if (len > plsz) 44548636SMark.Maybee@Sun.COM len = plsz; 44558681SMark.Maybee@Sun.COM else 44568681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 44578636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4458789Sahrens 44595367Sahrens ZFS_ENTER(zfsvfs); 44605367Sahrens ZFS_VERIFY_ZP(zp); 4461789Sahrens 4462789Sahrens if (protp) 4463789Sahrens *protp = PROT_ALL; 4464789Sahrens 4465789Sahrens /* 44669265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4467789Sahrens * for pages. If we don't find a page, we will need to create 4468789Sahrens * a new page and fill it with data from the file. 4469789Sahrens */ 4470789Sahrens while (len > 0) { 44718636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 44728636SMark.Maybee@Sun.COM *(pl+1) = NULL; 44738636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 44748636SMark.Maybee@Sun.COM goto out; 44758636SMark.Maybee@Sun.COM while (*pl) { 44768636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4477789Sahrens off += PAGESIZE; 4478789Sahrens addr += PAGESIZE; 44798681SMark.Maybee@Sun.COM if (len > 0) { 44808681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 44818636SMark.Maybee@Sun.COM len -= PAGESIZE; 44828681SMark.Maybee@Sun.COM } 44838636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4484789Sahrens plsz -= PAGESIZE; 44858636SMark.Maybee@Sun.COM pl++; 4486789Sahrens } 4487789Sahrens } 4488789Sahrens 4489789Sahrens /* 4490789Sahrens * Fill out the page array with any pages already in the cache. 4491789Sahrens */ 44928636SMark.Maybee@Sun.COM while (plsz > 0 && 44938636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 44948636SMark.Maybee@Sun.COM off += PAGESIZE; 44958636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4496789Sahrens } 4497789Sahrens out: 44982752Sperrin if (err) { 44992752Sperrin /* 45002752Sperrin * Release any pages we have previously locked. 45012752Sperrin */ 45022752Sperrin while (pl > pl0) 45032752Sperrin page_unlock(*--pl); 45048636SMark.Maybee@Sun.COM } else { 45058636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 45062752Sperrin } 45072752Sperrin 4508789Sahrens *pl = NULL; 4509789Sahrens 4510789Sahrens ZFS_EXIT(zfsvfs); 4511789Sahrens return (err); 4512789Sahrens } 4513789Sahrens 45141544Seschrock /* 45151544Seschrock * Request a memory map for a section of a file. This code interacts 45161544Seschrock * with common code and the VM system as follows: 45171544Seschrock * 45181544Seschrock * common code calls mmap(), which ends up in smmap_common() 45191544Seschrock * 45201544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 45211544Seschrock * 45221544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 45231544Seschrock * 45241544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 45251544Seschrock * 45261544Seschrock * zfs_addmap() updates z_mapcnt 45271544Seschrock */ 45285331Samw /*ARGSUSED*/ 4529789Sahrens static int 4530789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 45315331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 45325331Samw caller_context_t *ct) 4533789Sahrens { 4534789Sahrens znode_t *zp = VTOZ(vp); 4535789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4536789Sahrens segvn_crargs_t vn_a; 4537789Sahrens int error; 4538789Sahrens 45395929Smarks ZFS_ENTER(zfsvfs); 45405929Smarks ZFS_VERIFY_ZP(zp); 45415929Smarks 454211935SMark.Shellenbaum@Sun.COM if ((prot & PROT_WRITE) && (zp->z_pflags & 454311935SMark.Shellenbaum@Sun.COM (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 45445929Smarks ZFS_EXIT(zfsvfs); 45455331Samw return (EPERM); 45465929Smarks } 45475929Smarks 45485929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 454911935SMark.Shellenbaum@Sun.COM (zp->z_pflags & ZFS_AV_QUARANTINED)) { 45505929Smarks ZFS_EXIT(zfsvfs); 45515929Smarks return (EACCES); 45525929Smarks } 4553789Sahrens 4554789Sahrens if (vp->v_flag & VNOMAP) { 4555789Sahrens ZFS_EXIT(zfsvfs); 4556789Sahrens return (ENOSYS); 4557789Sahrens } 4558789Sahrens 4559789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4560789Sahrens ZFS_EXIT(zfsvfs); 4561789Sahrens return (ENXIO); 4562789Sahrens } 4563789Sahrens 4564789Sahrens if (vp->v_type != VREG) { 4565789Sahrens ZFS_EXIT(zfsvfs); 4566789Sahrens return (ENODEV); 4567789Sahrens } 4568789Sahrens 4569789Sahrens /* 4570789Sahrens * If file is locked, disallow mapping. 4571789Sahrens */ 457211935SMark.Shellenbaum@Sun.COM if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 45731544Seschrock ZFS_EXIT(zfsvfs); 45741544Seschrock return (EAGAIN); 4575789Sahrens } 4576789Sahrens 4577789Sahrens as_rangelock(as); 45786036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 45796036Smec if (error != 0) { 45806036Smec as_rangeunlock(as); 45816036Smec ZFS_EXIT(zfsvfs); 45826036Smec return (error); 4583789Sahrens } 4584789Sahrens 4585789Sahrens vn_a.vp = vp; 4586789Sahrens vn_a.offset = (u_offset_t)off; 4587789Sahrens vn_a.type = flags & MAP_TYPE; 4588789Sahrens vn_a.prot = prot; 4589789Sahrens vn_a.maxprot = maxprot; 4590789Sahrens vn_a.cred = cr; 4591789Sahrens vn_a.amp = NULL; 4592789Sahrens vn_a.flags = flags & ~MAP_TYPE; 45931417Skchow vn_a.szc = 0; 45941417Skchow vn_a.lgrp_mem_policy_flags = 0; 4595789Sahrens 4596789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4597789Sahrens 4598789Sahrens as_rangeunlock(as); 4599789Sahrens ZFS_EXIT(zfsvfs); 4600789Sahrens return (error); 4601789Sahrens } 4602789Sahrens 4603789Sahrens /* ARGSUSED */ 4604789Sahrens static int 4605789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 46065331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 46075331Samw caller_context_t *ct) 4608789Sahrens { 46091544Seschrock uint64_t pages = btopr(len); 46101544Seschrock 46111544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4612789Sahrens return (0); 4613789Sahrens } 4614789Sahrens 46151773Seschrock /* 46161773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 46171773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 46181773Seschrock * detecting when the data was actually modified, we have to resort to 46191773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 46201773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 46211773Seschrock * which by far the most common case: 46221773Seschrock * 46231773Seschrock * open() 46241773Seschrock * mmap() 46251773Seschrock * <modify memory> 46261773Seschrock * munmap() 46271773Seschrock * close() 46281773Seschrock * <time lapse> 46291773Seschrock * putpage() via fsflush 46301773Seschrock * 46311773Seschrock * If we wait until fsflush to come along, we can have a modification time that 46321773Seschrock * is some arbitrary point in the future. In order to prevent this in the 46331773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 46341773Seschrock * torn down. 46351773Seschrock */ 4636789Sahrens /* ARGSUSED */ 4637789Sahrens static int 4638789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 46395331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 46405331Samw caller_context_t *ct) 4641789Sahrens { 46421544Seschrock uint64_t pages = btopr(len); 46431544Seschrock 46441544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 46451544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 46461773Seschrock 46471773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 46481773Seschrock vn_has_cached_data(vp)) 46495331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 46501773Seschrock 4651789Sahrens return (0); 4652789Sahrens } 4653789Sahrens 4654789Sahrens /* 4655789Sahrens * Free or allocate space in a file. Currently, this function only 4656789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4657789Sahrens * misnamed, as its functionality includes the ability to allocate as 4658789Sahrens * well as free space. 4659789Sahrens * 4660789Sahrens * IN: vp - vnode of file to free data in. 4661789Sahrens * cmd - action to take (only F_FREESP supported). 4662789Sahrens * bfp - section of file to free/alloc. 4663789Sahrens * flag - current file open mode flags. 4664789Sahrens * offset - current file offset. 4665789Sahrens * cr - credentials of caller [UNUSED]. 46665331Samw * ct - caller context. 4667789Sahrens * 4668789Sahrens * RETURN: 0 if success 4669789Sahrens * error code if failure 4670789Sahrens * 4671789Sahrens * Timestamps: 4672789Sahrens * vp - ctime|mtime updated 4673789Sahrens */ 4674789Sahrens /* ARGSUSED */ 4675789Sahrens static int 4676789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4677789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4678789Sahrens { 4679789Sahrens znode_t *zp = VTOZ(vp); 4680789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4681789Sahrens uint64_t off, len; 4682789Sahrens int error; 4683789Sahrens 46845367Sahrens ZFS_ENTER(zfsvfs); 46855367Sahrens ZFS_VERIFY_ZP(zp); 4686789Sahrens 4687789Sahrens if (cmd != F_FREESP) { 4688789Sahrens ZFS_EXIT(zfsvfs); 4689789Sahrens return (EINVAL); 4690789Sahrens } 4691789Sahrens 4692789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4693789Sahrens ZFS_EXIT(zfsvfs); 4694789Sahrens return (error); 4695789Sahrens } 4696789Sahrens 4697789Sahrens if (bfp->l_len < 0) { 4698789Sahrens ZFS_EXIT(zfsvfs); 4699789Sahrens return (EINVAL); 4700789Sahrens } 4701789Sahrens 4702789Sahrens off = bfp->l_start; 47031669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 47041878Smaybee 47056992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4706789Sahrens 4707789Sahrens ZFS_EXIT(zfsvfs); 4708789Sahrens return (error); 4709789Sahrens } 4710789Sahrens 47115331Samw /*ARGSUSED*/ 4712789Sahrens static int 47135331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4714789Sahrens { 4715789Sahrens znode_t *zp = VTOZ(vp); 4716789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 47175326Sek110237 uint32_t gen; 471811935SMark.Shellenbaum@Sun.COM uint64_t gen64; 4719789Sahrens uint64_t object = zp->z_id; 4720789Sahrens zfid_short_t *zfid; 472111935SMark.Shellenbaum@Sun.COM int size, i, error; 4722789Sahrens 47235367Sahrens ZFS_ENTER(zfsvfs); 47245367Sahrens ZFS_VERIFY_ZP(zp); 472511935SMark.Shellenbaum@Sun.COM 472611935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 472712218SMark.Shellenbaum@Sun.COM &gen64, sizeof (uint64_t))) != 0) { 472812218SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 472911935SMark.Shellenbaum@Sun.COM return (error); 473012218SMark.Shellenbaum@Sun.COM } 473111935SMark.Shellenbaum@Sun.COM 473211935SMark.Shellenbaum@Sun.COM gen = (uint32_t)gen64; 4733789Sahrens 4734789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4735789Sahrens if (fidp->fid_len < size) { 4736789Sahrens fidp->fid_len = size; 47371512Sek110237 ZFS_EXIT(zfsvfs); 4738789Sahrens return (ENOSPC); 4739789Sahrens } 4740789Sahrens 4741789Sahrens zfid = (zfid_short_t *)fidp; 4742789Sahrens 4743789Sahrens zfid->zf_len = size; 4744789Sahrens 4745789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4746789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4747789Sahrens 4748789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4749789Sahrens if (gen == 0) 4750789Sahrens gen = 1; 4751789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4752789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4753789Sahrens 4754789Sahrens if (size == LONG_FID_LEN) { 4755789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4756789Sahrens zfid_long_t *zlfid; 4757789Sahrens 4758789Sahrens zlfid = (zfid_long_t *)fidp; 4759789Sahrens 4760789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4761789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4762789Sahrens 4763789Sahrens /* XXX - this should be the generation number for the objset */ 4764789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4765789Sahrens zlfid->zf_setgen[i] = 0; 4766789Sahrens } 4767789Sahrens 4768789Sahrens ZFS_EXIT(zfsvfs); 4769789Sahrens return (0); 4770789Sahrens } 4771789Sahrens 4772789Sahrens static int 47735331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 47745331Samw caller_context_t *ct) 4775789Sahrens { 4776789Sahrens znode_t *zp, *xzp; 4777789Sahrens zfsvfs_t *zfsvfs; 4778789Sahrens zfs_dirlock_t *dl; 4779789Sahrens int error; 4780789Sahrens 4781789Sahrens switch (cmd) { 4782789Sahrens case _PC_LINK_MAX: 4783789Sahrens *valp = ULONG_MAX; 4784789Sahrens return (0); 4785789Sahrens 4786789Sahrens case _PC_FILESIZEBITS: 4787789Sahrens *valp = 64; 4788789Sahrens return (0); 4789789Sahrens 4790789Sahrens case _PC_XATTR_EXISTS: 4791789Sahrens zp = VTOZ(vp); 4792789Sahrens zfsvfs = zp->z_zfsvfs; 47935367Sahrens ZFS_ENTER(zfsvfs); 47945367Sahrens ZFS_VERIFY_ZP(zp); 4795789Sahrens *valp = 0; 4796789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 47975331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4798789Sahrens if (error == 0) { 4799789Sahrens zfs_dirent_unlock(dl); 4800789Sahrens if (!zfs_dirempty(xzp)) 4801789Sahrens *valp = 1; 4802789Sahrens VN_RELE(ZTOV(xzp)); 4803789Sahrens } else if (error == ENOENT) { 4804789Sahrens /* 4805789Sahrens * If there aren't extended attributes, it's the 4806789Sahrens * same as having zero of them. 4807789Sahrens */ 4808789Sahrens error = 0; 4809789Sahrens } 4810789Sahrens ZFS_EXIT(zfsvfs); 4811789Sahrens return (error); 4812789Sahrens 48135331Samw case _PC_SATTR_ENABLED: 48145331Samw case _PC_SATTR_EXISTS: 48157757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 48165331Samw (vp->v_type == VREG || vp->v_type == VDIR); 48175331Samw return (0); 48185331Samw 48199749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 48209749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 48219749STim.Haley@Sun.COM vp->v_type == VDIR; 48229749STim.Haley@Sun.COM return (0); 48239749STim.Haley@Sun.COM 4824789Sahrens case _PC_ACL_ENABLED: 4825789Sahrens *valp = _ACL_ACE_ENABLED; 4826789Sahrens return (0); 4827789Sahrens 4828789Sahrens case _PC_MIN_HOLE_SIZE: 4829789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4830789Sahrens return (0); 4831789Sahrens 483210440SRoger.Faulkner@Sun.COM case _PC_TIMESTAMP_RESOLUTION: 483310440SRoger.Faulkner@Sun.COM /* nanosecond timestamp resolution */ 483410440SRoger.Faulkner@Sun.COM *valp = 1L; 483510440SRoger.Faulkner@Sun.COM return (0); 483610440SRoger.Faulkner@Sun.COM 4837789Sahrens default: 48385331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4839789Sahrens } 4840789Sahrens } 4841789Sahrens 4842789Sahrens /*ARGSUSED*/ 4843789Sahrens static int 48445331Samw zfs_getsecattr(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; 4851789Sahrens 48525367Sahrens ZFS_ENTER(zfsvfs); 48535367Sahrens ZFS_VERIFY_ZP(zp); 48545331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4855789Sahrens ZFS_EXIT(zfsvfs); 4856789Sahrens 4857789Sahrens return (error); 4858789Sahrens } 4859789Sahrens 4860789Sahrens /*ARGSUSED*/ 4861789Sahrens static int 48625331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48635331Samw caller_context_t *ct) 4864789Sahrens { 4865789Sahrens znode_t *zp = VTOZ(vp); 4866789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4867789Sahrens int error; 48685331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 486912294SMark.Musante@Sun.COM zilog_t *zilog = zfsvfs->z_log; 4870789Sahrens 48715367Sahrens ZFS_ENTER(zfsvfs); 48725367Sahrens ZFS_VERIFY_ZP(zp); 487312294SMark.Musante@Sun.COM 48745331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 487512294SMark.Musante@Sun.COM 487612294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 487712699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 487812294SMark.Musante@Sun.COM 4879789Sahrens ZFS_EXIT(zfsvfs); 4880789Sahrens return (error); 4881789Sahrens } 4882789Sahrens 4883789Sahrens /* 488411539SChunli.Zhang@Sun.COM * Tunable, both must be a power of 2. 488511539SChunli.Zhang@Sun.COM * 488611539SChunli.Zhang@Sun.COM * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf 488711539SChunli.Zhang@Sun.COM * zcr_blksz_max: if set to less than the file block size, allow loaning out of 488811539SChunli.Zhang@Sun.COM * an arcbuf for a partial block read 488911539SChunli.Zhang@Sun.COM */ 489011539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10); /* 1K */ 489111539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17); /* 128K */ 489211539SChunli.Zhang@Sun.COM 489311539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 489411539SChunli.Zhang@Sun.COM static int 489511539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 489611539SChunli.Zhang@Sun.COM caller_context_t *ct) 489711539SChunli.Zhang@Sun.COM { 489811539SChunli.Zhang@Sun.COM znode_t *zp = VTOZ(vp); 489911539SChunli.Zhang@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 490011539SChunli.Zhang@Sun.COM int max_blksz = zfsvfs->z_max_blksz; 490111539SChunli.Zhang@Sun.COM uio_t *uio = &xuio->xu_uio; 490211539SChunli.Zhang@Sun.COM ssize_t size = uio->uio_resid; 490311539SChunli.Zhang@Sun.COM offset_t offset = uio->uio_loffset; 490411539SChunli.Zhang@Sun.COM int blksz; 490511539SChunli.Zhang@Sun.COM int fullblk, i; 490611539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 490711539SChunli.Zhang@Sun.COM ssize_t maxsize; 490811539SChunli.Zhang@Sun.COM int preamble, postamble; 490911539SChunli.Zhang@Sun.COM 491011539SChunli.Zhang@Sun.COM if (xuio->xu_type != UIOTYPE_ZEROCOPY) 491111539SChunli.Zhang@Sun.COM return (EINVAL); 491211539SChunli.Zhang@Sun.COM 491311539SChunli.Zhang@Sun.COM ZFS_ENTER(zfsvfs); 491411539SChunli.Zhang@Sun.COM ZFS_VERIFY_ZP(zp); 491511539SChunli.Zhang@Sun.COM switch (ioflag) { 491611539SChunli.Zhang@Sun.COM case UIO_WRITE: 491711539SChunli.Zhang@Sun.COM /* 491811539SChunli.Zhang@Sun.COM * Loan out an arc_buf for write if write size is bigger than 491911539SChunli.Zhang@Sun.COM * max_blksz, and the file's block size is also max_blksz. 492011539SChunli.Zhang@Sun.COM */ 492111539SChunli.Zhang@Sun.COM blksz = max_blksz; 492211539SChunli.Zhang@Sun.COM if (size < blksz || zp->z_blksz != blksz) { 492311539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 492411539SChunli.Zhang@Sun.COM return (EINVAL); 492511539SChunli.Zhang@Sun.COM } 492611539SChunli.Zhang@Sun.COM /* 492711539SChunli.Zhang@Sun.COM * Caller requests buffers for write before knowing where the 492811539SChunli.Zhang@Sun.COM * write offset might be (e.g. NFS TCP write). 492911539SChunli.Zhang@Sun.COM */ 493011539SChunli.Zhang@Sun.COM if (offset == -1) { 493111539SChunli.Zhang@Sun.COM preamble = 0; 493211539SChunli.Zhang@Sun.COM } else { 493311539SChunli.Zhang@Sun.COM preamble = P2PHASE(offset, blksz); 493411539SChunli.Zhang@Sun.COM if (preamble) { 493511539SChunli.Zhang@Sun.COM preamble = blksz - preamble; 493611539SChunli.Zhang@Sun.COM size -= preamble; 493711539SChunli.Zhang@Sun.COM } 493811539SChunli.Zhang@Sun.COM } 493911539SChunli.Zhang@Sun.COM 494011539SChunli.Zhang@Sun.COM postamble = P2PHASE(size, blksz); 494111539SChunli.Zhang@Sun.COM size -= postamble; 494211539SChunli.Zhang@Sun.COM 494311539SChunli.Zhang@Sun.COM fullblk = size / blksz; 494411576SSurya.Prakki@Sun.COM (void) dmu_xuio_init(xuio, 494511539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 494611539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 494711539SChunli.Zhang@Sun.COM int, postamble, int, 494811539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 494911539SChunli.Zhang@Sun.COM 495011539SChunli.Zhang@Sun.COM /* 495111539SChunli.Zhang@Sun.COM * Have to fix iov base/len for partial buffers. They 495211539SChunli.Zhang@Sun.COM * currently represent full arc_buf's. 495311539SChunli.Zhang@Sun.COM */ 495411539SChunli.Zhang@Sun.COM if (preamble) { 495511539SChunli.Zhang@Sun.COM /* data begins in the middle of the arc_buf */ 495611935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 495711935SMark.Shellenbaum@Sun.COM blksz); 495811539SChunli.Zhang@Sun.COM ASSERT(abuf); 495911576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 496011576SSurya.Prakki@Sun.COM blksz - preamble, preamble); 496111539SChunli.Zhang@Sun.COM } 496211539SChunli.Zhang@Sun.COM 496311539SChunli.Zhang@Sun.COM for (i = 0; i < fullblk; i++) { 496411935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 496511935SMark.Shellenbaum@Sun.COM blksz); 496611539SChunli.Zhang@Sun.COM ASSERT(abuf); 496711576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, blksz); 496811539SChunli.Zhang@Sun.COM } 496911539SChunli.Zhang@Sun.COM 497011539SChunli.Zhang@Sun.COM if (postamble) { 497111539SChunli.Zhang@Sun.COM /* data ends in the middle of the arc_buf */ 497211935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 497311935SMark.Shellenbaum@Sun.COM blksz); 497411539SChunli.Zhang@Sun.COM ASSERT(abuf); 497511576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, postamble); 497611539SChunli.Zhang@Sun.COM } 497711539SChunli.Zhang@Sun.COM break; 497811539SChunli.Zhang@Sun.COM case UIO_READ: 497911539SChunli.Zhang@Sun.COM /* 498011539SChunli.Zhang@Sun.COM * Loan out an arc_buf for read if the read size is larger than 498111539SChunli.Zhang@Sun.COM * the current file block size. Block alignment is not 498211539SChunli.Zhang@Sun.COM * considered. Partial arc_buf will be loaned out for read. 498311539SChunli.Zhang@Sun.COM */ 498411539SChunli.Zhang@Sun.COM blksz = zp->z_blksz; 498511539SChunli.Zhang@Sun.COM if (blksz < zcr_blksz_min) 498611539SChunli.Zhang@Sun.COM blksz = zcr_blksz_min; 498711539SChunli.Zhang@Sun.COM if (blksz > zcr_blksz_max) 498811539SChunli.Zhang@Sun.COM blksz = zcr_blksz_max; 498911539SChunli.Zhang@Sun.COM /* avoid potential complexity of dealing with it */ 499011539SChunli.Zhang@Sun.COM if (blksz > max_blksz) { 499111539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 499211539SChunli.Zhang@Sun.COM return (EINVAL); 499311539SChunli.Zhang@Sun.COM } 499411539SChunli.Zhang@Sun.COM 499511935SMark.Shellenbaum@Sun.COM maxsize = zp->z_size - uio->uio_loffset; 499611539SChunli.Zhang@Sun.COM if (size > maxsize) 499711539SChunli.Zhang@Sun.COM size = maxsize; 499811539SChunli.Zhang@Sun.COM 499911539SChunli.Zhang@Sun.COM if (size < blksz || vn_has_cached_data(vp)) { 500011539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 500111539SChunli.Zhang@Sun.COM return (EINVAL); 500211539SChunli.Zhang@Sun.COM } 500311539SChunli.Zhang@Sun.COM break; 500411539SChunli.Zhang@Sun.COM default: 500511539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 500611539SChunli.Zhang@Sun.COM return (EINVAL); 500711539SChunli.Zhang@Sun.COM } 500811539SChunli.Zhang@Sun.COM 500911539SChunli.Zhang@Sun.COM uio->uio_extflg = UIO_XUIO; 501011539SChunli.Zhang@Sun.COM XUIO_XUZC_RW(xuio) = ioflag; 501111539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 501211539SChunli.Zhang@Sun.COM return (0); 501311539SChunli.Zhang@Sun.COM } 501411539SChunli.Zhang@Sun.COM 501511539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 501611539SChunli.Zhang@Sun.COM static int 501711539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 501811539SChunli.Zhang@Sun.COM { 501911539SChunli.Zhang@Sun.COM int i; 502011539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 502111539SChunli.Zhang@Sun.COM int ioflag = XUIO_XUZC_RW(xuio); 502211539SChunli.Zhang@Sun.COM 502311539SChunli.Zhang@Sun.COM ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 502411539SChunli.Zhang@Sun.COM 502511539SChunli.Zhang@Sun.COM i = dmu_xuio_cnt(xuio); 502611539SChunli.Zhang@Sun.COM while (i-- > 0) { 502711539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i); 502811539SChunli.Zhang@Sun.COM /* 502911539SChunli.Zhang@Sun.COM * if abuf == NULL, it must be a write buffer 503011539SChunli.Zhang@Sun.COM * that has been returned in zfs_write(). 503111539SChunli.Zhang@Sun.COM */ 503211539SChunli.Zhang@Sun.COM if (abuf) 503311539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 503411539SChunli.Zhang@Sun.COM ASSERT(abuf || ioflag == UIO_WRITE); 503511539SChunli.Zhang@Sun.COM } 503611539SChunli.Zhang@Sun.COM 503711539SChunli.Zhang@Sun.COM dmu_xuio_fini(xuio); 503811539SChunli.Zhang@Sun.COM return (0); 503911539SChunli.Zhang@Sun.COM } 504011539SChunli.Zhang@Sun.COM 504111539SChunli.Zhang@Sun.COM /* 5042789Sahrens * Predeclare these here so that the compiler assumes that 5043789Sahrens * this is an "old style" function declaration that does 5044789Sahrens * not include arguments => we won't get type mismatch errors 5045789Sahrens * in the initializations that follow. 5046789Sahrens */ 5047789Sahrens static int zfs_inval(); 5048789Sahrens static int zfs_isdir(); 5049789Sahrens 5050789Sahrens static int 5051789Sahrens zfs_inval() 5052789Sahrens { 5053789Sahrens return (EINVAL); 5054789Sahrens } 5055789Sahrens 5056789Sahrens static int 5057789Sahrens zfs_isdir() 5058789Sahrens { 5059789Sahrens return (EISDIR); 5060789Sahrens } 5061789Sahrens /* 5062789Sahrens * Directory vnode operations template 5063789Sahrens */ 5064789Sahrens vnodeops_t *zfs_dvnodeops; 5065789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 50663898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50673898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50683898Srsb VOPNAME_READ, { .error = zfs_isdir }, 50693898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 50703898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50713898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50723898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50733898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 50743898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 50753898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 50763898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 50773898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 50783898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 50793898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 50803898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 50813898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 50823898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 50833898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 50843898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 50853898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 50863898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 50873898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 50883898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 50893898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 50904863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 50913898Srsb NULL, NULL 5092789Sahrens }; 5093789Sahrens 5094789Sahrens /* 5095789Sahrens * Regular file vnode operations template 5096789Sahrens */ 5097789Sahrens vnodeops_t *zfs_fvnodeops; 5098789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 50993898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 51003898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 51013898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 51023898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 51033898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 51043898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51053898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51063898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51073898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 51083898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51093898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 51103898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51113898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51123898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 51133898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 51143898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 51153898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 51163898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 51173898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 51183898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 51193898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 51203898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51213898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51223898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51233898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 512411539SChunli.Zhang@Sun.COM VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 512511539SChunli.Zhang@Sun.COM VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 51263898Srsb NULL, NULL 5127789Sahrens }; 5128789Sahrens 5129789Sahrens /* 5130789Sahrens * Symbolic link vnode operations template 5131789Sahrens */ 5132789Sahrens vnodeops_t *zfs_symvnodeops; 5133789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 51343898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51353898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51363898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51373898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51383898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 51393898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51403898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51413898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51423898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51433898Srsb NULL, NULL 5144789Sahrens }; 5145789Sahrens 5146789Sahrens /* 51478845Samw@Sun.COM * special share hidden files vnode operations template 51488845Samw@Sun.COM */ 51498845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 51508845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 51518845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51528845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 51538845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51548845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 51558845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51568845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51578845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51588845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51598845Samw@Sun.COM NULL, NULL 51608845Samw@Sun.COM }; 51618845Samw@Sun.COM 51628845Samw@Sun.COM /* 5163789Sahrens * Extended attribute directory vnode operations template 5164789Sahrens * This template is identical to the directory vnodes 5165789Sahrens * operation template except for restricted operations: 5166789Sahrens * VOP_MKDIR() 5167789Sahrens * VOP_SYMLINK() 5168789Sahrens * Note that there are other restrictions embedded in: 5169789Sahrens * zfs_create() - restrict type to VREG 5170789Sahrens * zfs_link() - no links into/out of attribute space 5171789Sahrens * zfs_rename() - no moves into/out of attribute space 5172789Sahrens */ 5173789Sahrens vnodeops_t *zfs_xdvnodeops; 5174789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 51753898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 51763898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 51773898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 51783898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51793898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51803898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51813898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 51823898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 51833898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 51843898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 51853898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51863898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 51873898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 51883898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 51893898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 51903898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 51913898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51923898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51933898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 51943898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51953898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51963898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51973898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51983898Srsb NULL, NULL 5199789Sahrens }; 5200789Sahrens 5201789Sahrens /* 5202789Sahrens * Error vnode operations template 5203789Sahrens */ 5204789Sahrens vnodeops_t *zfs_evnodeops; 5205789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 52063898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 52073898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 52083898Srsb NULL, NULL 5209789Sahrens }; 5210