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 67311539SChunli.Zhang@Sun.COM uio_prefaultpages(n, uio); 674789Sahrens 675789Sahrens /* 676789Sahrens * If in append mode, set the io offset pointer to eof. 677789Sahrens */ 6781669Sperrin if (ioflag & FAPPEND) { 6791669Sperrin /* 68011083Swilliam.gorrell@sun.com * Obtain an appending range lock to guarantee file append 68111083Swilliam.gorrell@sun.com * semantics. We reset the write offset once we have the lock. 6821669Sperrin */ 6831669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 68411083Swilliam.gorrell@sun.com woff = rl->r_off; 6851669Sperrin if (rl->r_len == UINT64_MAX) { 68611083Swilliam.gorrell@sun.com /* 68711083Swilliam.gorrell@sun.com * We overlocked the file because this write will cause 68811083Swilliam.gorrell@sun.com * the file block size to increase. 68911083Swilliam.gorrell@sun.com * Note that zp_size cannot change with this lock held. 69011083Swilliam.gorrell@sun.com */ 69111935SMark.Shellenbaum@Sun.COM woff = zp->z_size; 6921669Sperrin } 69311083Swilliam.gorrell@sun.com uio->uio_loffset = woff; 694789Sahrens } else { 695789Sahrens /* 69611083Swilliam.gorrell@sun.com * Note that if the file block size will change as a result of 69711083Swilliam.gorrell@sun.com * this write, then this range lock will lock the entire file 69811083Swilliam.gorrell@sun.com * so that we can re-write the block safely. 699789Sahrens */ 7001669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 701789Sahrens } 702789Sahrens 703789Sahrens if (woff >= limit) { 7043638Sbillm zfs_range_unlock(rl); 7053638Sbillm ZFS_EXIT(zfsvfs); 7063638Sbillm return (EFBIG); 707789Sahrens } 708789Sahrens 709789Sahrens if ((woff + n) > limit || woff > (limit - n)) 710789Sahrens n = limit - woff; 711789Sahrens 71211539SChunli.Zhang@Sun.COM /* Will this write extend the file length? */ 71311935SMark.Shellenbaum@Sun.COM write_eof = (woff + n > zp->z_size); 71411935SMark.Shellenbaum@Sun.COM 71511935SMark.Shellenbaum@Sun.COM end_size = MAX(zp->z_size, woff + n); 716789Sahrens 7171669Sperrin /* 7183638Sbillm * Write the file in reasonable size chunks. Each chunk is written 7193638Sbillm * in a separate transaction; this keeps the intent log records small 7203638Sbillm * and allows us to do more fine-grained space accounting. 721789Sahrens */ 722789Sahrens while (n > 0) { 7239412SAleksandr.Guzovskiy@Sun.COM abuf = NULL; 7249412SAleksandr.Guzovskiy@Sun.COM woff = uio->uio_loffset; 7259412SAleksandr.Guzovskiy@Sun.COM again: 72611935SMark.Shellenbaum@Sun.COM if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 72711935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 7289412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7299412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7309396SMatthew.Ahrens@Sun.COM error = EDQUOT; 7319396SMatthew.Ahrens@Sun.COM break; 7329396SMatthew.Ahrens@Sun.COM } 7339412SAleksandr.Guzovskiy@Sun.COM 73411539SChunli.Zhang@Sun.COM if (xuio && abuf == NULL) { 73511539SChunli.Zhang@Sun.COM ASSERT(i_iov < iovcnt); 73611539SChunli.Zhang@Sun.COM aiov = &iovp[i_iov]; 73711539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i_iov); 73811539SChunli.Zhang@Sun.COM dmu_xuio_clear(xuio, i_iov); 73911539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_cp_write, int, i_iov, 74011539SChunli.Zhang@Sun.COM iovec_t *, aiov, arc_buf_t *, abuf); 74111539SChunli.Zhang@Sun.COM ASSERT((aiov->iov_base == abuf->b_data) || 74211539SChunli.Zhang@Sun.COM ((char *)aiov->iov_base - (char *)abuf->b_data + 74311539SChunli.Zhang@Sun.COM aiov->iov_len == arc_buf_size(abuf))); 74411539SChunli.Zhang@Sun.COM i_iov++; 74511539SChunli.Zhang@Sun.COM } else if (abuf == NULL && n >= max_blksz && 74611935SMark.Shellenbaum@Sun.COM woff >= zp->z_size && 7479412SAleksandr.Guzovskiy@Sun.COM P2PHASE(woff, max_blksz) == 0 && 7489412SAleksandr.Guzovskiy@Sun.COM zp->z_blksz == max_blksz) { 74911539SChunli.Zhang@Sun.COM /* 75011539SChunli.Zhang@Sun.COM * This write covers a full block. "Borrow" a buffer 75111539SChunli.Zhang@Sun.COM * from the dmu so that we can fill it before we enter 75211539SChunli.Zhang@Sun.COM * a transaction. This avoids the possibility of 75311539SChunli.Zhang@Sun.COM * holding up the transaction if the data copy hangs 75411539SChunli.Zhang@Sun.COM * up on a pagefault (e.g., from an NFS server mapping). 75511539SChunli.Zhang@Sun.COM */ 7569412SAleksandr.Guzovskiy@Sun.COM size_t cbytes; 7579412SAleksandr.Guzovskiy@Sun.COM 75811935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 75911935SMark.Shellenbaum@Sun.COM max_blksz); 7609412SAleksandr.Guzovskiy@Sun.COM ASSERT(abuf != NULL); 7619412SAleksandr.Guzovskiy@Sun.COM ASSERT(arc_buf_size(abuf) == max_blksz); 7629412SAleksandr.Guzovskiy@Sun.COM if (error = uiocopy(abuf->b_data, max_blksz, 7639412SAleksandr.Guzovskiy@Sun.COM UIO_WRITE, uio, &cbytes)) { 7649412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7659412SAleksandr.Guzovskiy@Sun.COM break; 7669412SAleksandr.Guzovskiy@Sun.COM } 7679412SAleksandr.Guzovskiy@Sun.COM ASSERT(cbytes == max_blksz); 7689412SAleksandr.Guzovskiy@Sun.COM } 7699412SAleksandr.Guzovskiy@Sun.COM 7709412SAleksandr.Guzovskiy@Sun.COM /* 7719412SAleksandr.Guzovskiy@Sun.COM * Start a transaction. 7729412SAleksandr.Guzovskiy@Sun.COM */ 773789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 77411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 775789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 77611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 7778227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 778789Sahrens if (error) { 7798227SNeil.Perrin@Sun.COM if (error == ERESTART) { 7802113Sahrens dmu_tx_wait(tx); 7812113Sahrens dmu_tx_abort(tx); 7829412SAleksandr.Guzovskiy@Sun.COM goto again; 783789Sahrens } 7842113Sahrens dmu_tx_abort(tx); 7859412SAleksandr.Guzovskiy@Sun.COM if (abuf != NULL) 7869412SAleksandr.Guzovskiy@Sun.COM dmu_return_arcbuf(abuf); 7873638Sbillm break; 7883638Sbillm } 7893638Sbillm 7903638Sbillm /* 7913638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 7923638Sbillm * and then reduce the lock range. This will only happen 7933638Sbillm * on the first iteration since zfs_range_reduce() will 7943638Sbillm * shrink down r_len to the appropriate size. 7953638Sbillm */ 7963638Sbillm if (rl->r_len == UINT64_MAX) { 7973638Sbillm uint64_t new_blksz; 7983638Sbillm 7993638Sbillm if (zp->z_blksz > max_blksz) { 8003638Sbillm ASSERT(!ISP2(zp->z_blksz)); 8013638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 8023638Sbillm } else { 8033638Sbillm new_blksz = MIN(end_size, max_blksz); 8043638Sbillm } 8053638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 8063638Sbillm zfs_range_reduce(rl, woff, n); 8073638Sbillm } 8083638Sbillm 8093638Sbillm /* 8103638Sbillm * XXX - should we really limit each write to z_max_blksz? 8113638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 8123638Sbillm */ 8133638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 8143638Sbillm 8159412SAleksandr.Guzovskiy@Sun.COM if (abuf == NULL) { 8169412SAleksandr.Guzovskiy@Sun.COM tx_bytes = uio->uio_resid; 81712123STim.Haley@Sun.COM error = dmu_write_uio_dbuf(sa_get_db(zp->z_sa_hdl), 81812123STim.Haley@Sun.COM uio, nbytes, tx); 8199412SAleksandr.Guzovskiy@Sun.COM tx_bytes -= uio->uio_resid; 8209412SAleksandr.Guzovskiy@Sun.COM } else { 8219412SAleksandr.Guzovskiy@Sun.COM tx_bytes = nbytes; 82211539SChunli.Zhang@Sun.COM ASSERT(xuio == NULL || tx_bytes == aiov->iov_len); 82311539SChunli.Zhang@Sun.COM /* 82411539SChunli.Zhang@Sun.COM * If this is not a full block write, but we are 82511539SChunli.Zhang@Sun.COM * extending the file past EOF and this data starts 82611539SChunli.Zhang@Sun.COM * block-aligned, use assign_arcbuf(). Otherwise, 82711539SChunli.Zhang@Sun.COM * write via dmu_write(). 82811539SChunli.Zhang@Sun.COM */ 82911539SChunli.Zhang@Sun.COM if (tx_bytes < max_blksz && (!write_eof || 83011539SChunli.Zhang@Sun.COM aiov->iov_base != abuf->b_data)) { 83111539SChunli.Zhang@Sun.COM ASSERT(xuio); 83211539SChunli.Zhang@Sun.COM dmu_write(zfsvfs->z_os, zp->z_id, woff, 83311539SChunli.Zhang@Sun.COM aiov->iov_len, aiov->iov_base, tx); 83411539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 83511539SChunli.Zhang@Sun.COM xuio_stat_wbuf_copied(); 83611539SChunli.Zhang@Sun.COM } else { 83711539SChunli.Zhang@Sun.COM ASSERT(xuio || tx_bytes == max_blksz); 83811935SMark.Shellenbaum@Sun.COM dmu_assign_arcbuf(sa_get_db(zp->z_sa_hdl), 83911935SMark.Shellenbaum@Sun.COM woff, abuf, tx); 84011539SChunli.Zhang@Sun.COM } 8419412SAleksandr.Guzovskiy@Sun.COM ASSERT(tx_bytes <= uio->uio_resid); 8429412SAleksandr.Guzovskiy@Sun.COM uioskip(uio, tx_bytes); 8439412SAleksandr.Guzovskiy@Sun.COM } 8449412SAleksandr.Guzovskiy@Sun.COM if (tx_bytes && vn_has_cached_data(vp)) { 8458636SMark.Maybee@Sun.COM update_pages(vp, woff, 8468636SMark.Maybee@Sun.COM tx_bytes, zfsvfs->z_os, zp->z_id); 8479412SAleksandr.Guzovskiy@Sun.COM } 8483638Sbillm 8493638Sbillm /* 8503638Sbillm * If we made no progress, we're done. If we made even 8513638Sbillm * partial progress, update the znode and ZIL accordingly. 8523638Sbillm */ 8533638Sbillm if (tx_bytes == 0) { 85411935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 85511935SMark.Shellenbaum@Sun.COM (void *)&zp->z_size, sizeof (uint64_t), tx); 8563897Smaybee dmu_tx_commit(tx); 8573638Sbillm ASSERT(error != 0); 8583638Sbillm break; 8593638Sbillm } 8603638Sbillm 861789Sahrens /* 8623638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 8633638Sbillm * privileged and at least one of the excute bits is set. 8643638Sbillm * 8653638Sbillm * It would be nice to to this after all writes have 8663638Sbillm * been done, but that would still expose the ISUID/ISGID 8673638Sbillm * to another app after the partial write is committed. 8685331Samw * 869789Sahrens */ 8703638Sbillm mutex_enter(&zp->z_acl_lock); 87111935SMark.Shellenbaum@Sun.COM if ((zp->z_mode & (S_IXUSR | (S_IXUSR >> 3) | 8723638Sbillm (S_IXUSR >> 6))) != 0 && 87311935SMark.Shellenbaum@Sun.COM (zp->z_mode & (S_ISUID | S_ISGID)) != 0 && 8743638Sbillm secpolicy_vnode_setid_retain(cr, 87511935SMark.Shellenbaum@Sun.COM (zp->z_mode & S_ISUID) != 0 && zp->z_uid == 0) != 0) { 87611935SMark.Shellenbaum@Sun.COM uint64_t newmode; 87711935SMark.Shellenbaum@Sun.COM zp->z_mode &= ~(S_ISUID | S_ISGID); 87811935SMark.Shellenbaum@Sun.COM newmode = zp->z_mode; 87911935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_MODE(zfsvfs), 88011935SMark.Shellenbaum@Sun.COM (void *)&newmode, sizeof (uint64_t), tx); 8813638Sbillm } 8823638Sbillm mutex_exit(&zp->z_acl_lock); 8833638Sbillm 88411935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 88511935SMark.Shellenbaum@Sun.COM B_TRUE); 8863638Sbillm 8873638Sbillm /* 8883638Sbillm * Update the file size (zp_size) if it has changed; 8893638Sbillm * account for possible concurrent updates. 8903638Sbillm */ 89111935SMark.Shellenbaum@Sun.COM while ((end_size = zp->z_size) < uio->uio_loffset) { 89211935SMark.Shellenbaum@Sun.COM (void) atomic_cas_64(&zp->z_size, end_size, 893789Sahrens uio->uio_loffset); 89411935SMark.Shellenbaum@Sun.COM ASSERT(error == 0); 89511935SMark.Shellenbaum@Sun.COM } 89611935SMark.Shellenbaum@Sun.COM error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 89711935SMark.Shellenbaum@Sun.COM 8983638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 8993638Sbillm dmu_tx_commit(tx); 9003638Sbillm 9013638Sbillm if (error != 0) 9023638Sbillm break; 9033638Sbillm ASSERT(tx_bytes == nbytes); 9043638Sbillm n -= nbytes; 905789Sahrens } 906789Sahrens 9072237Smaybee zfs_range_unlock(rl); 908789Sahrens 909789Sahrens /* 910789Sahrens * If we're in replay mode, or we made no progress, return error. 911789Sahrens * Otherwise, it's at least a partial write, so it's successful. 912789Sahrens */ 9138227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 914789Sahrens ZFS_EXIT(zfsvfs); 915789Sahrens return (error); 916789Sahrens } 917789Sahrens 91812294SMark.Musante@Sun.COM if (ioflag & (FSYNC | FDSYNC) || 91912294SMark.Musante@Sun.COM zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 92012699SNeil.Perrin@Sun.COM zil_commit(zilog, zp->z_id); 921789Sahrens 922789Sahrens ZFS_EXIT(zfsvfs); 923789Sahrens return (0); 924789Sahrens } 925789Sahrens 9262237Smaybee void 92710922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error) 9282237Smaybee { 92910922SJeff.Bonwick@Sun.COM znode_t *zp = zgd->zgd_private; 93010922SJeff.Bonwick@Sun.COM objset_t *os = zp->z_zfsvfs->z_os; 93110922SJeff.Bonwick@Sun.COM 93210922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 93310922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 93410922SJeff.Bonwick@Sun.COM 93510922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 93610922SJeff.Bonwick@Sun.COM 9379321SNeil.Perrin@Sun.COM /* 9389321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9399321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9409321SNeil.Perrin@Sun.COM */ 94110922SJeff.Bonwick@Sun.COM VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 94210922SJeff.Bonwick@Sun.COM 94310922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 94410922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 94510922SJeff.Bonwick@Sun.COM 9463063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9472237Smaybee } 9482237Smaybee 94910209SMark.Musante@Sun.COM #ifdef DEBUG 95010209SMark.Musante@Sun.COM static int zil_fault_io = 0; 95110209SMark.Musante@Sun.COM #endif 95210209SMark.Musante@Sun.COM 953789Sahrens /* 954789Sahrens * Get data to generate a TX_WRITE intent log record. 955789Sahrens */ 956789Sahrens int 9572237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 958789Sahrens { 959789Sahrens zfsvfs_t *zfsvfs = arg; 960789Sahrens objset_t *os = zfsvfs->z_os; 961789Sahrens znode_t *zp; 96210922SJeff.Bonwick@Sun.COM uint64_t object = lr->lr_foid; 96310922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 96410922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; 96510922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9662237Smaybee dmu_buf_t *db; 9673063Sperrin zgd_t *zgd; 968789Sahrens int error = 0; 969789Sahrens 97010922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 97110922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 972789Sahrens 973789Sahrens /* 9741669Sperrin * Nothing to do if the file has been removed 975789Sahrens */ 97610922SJeff.Bonwick@Sun.COM if (zfs_zget(zfsvfs, object, &zp) != 0) 977789Sahrens return (ENOENT); 9783461Sahrens if (zp->z_unlinked) { 9799321SNeil.Perrin@Sun.COM /* 9809321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9819321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9829321SNeil.Perrin@Sun.COM */ 9839321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), 9849321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 985789Sahrens return (ENOENT); 986789Sahrens } 987789Sahrens 98810922SJeff.Bonwick@Sun.COM zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 98910922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zfsvfs->z_log; 99010922SJeff.Bonwick@Sun.COM zgd->zgd_private = zp; 99110922SJeff.Bonwick@Sun.COM 992789Sahrens /* 993789Sahrens * Write records come in two flavors: immediate and indirect. 994789Sahrens * For small writes it's cheaper to store the data with the 995789Sahrens * log record (immediate); for large writes it's cheaper to 996789Sahrens * sync the data and get a pointer to it (indirect) so that 997789Sahrens * we don't have to write the data twice. 998789Sahrens */ 9991669Sperrin if (buf != NULL) { /* immediate write */ 100010922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 10011669Sperrin /* test for truncation needs to be done while range locked */ 100211935SMark.Shellenbaum@Sun.COM if (offset >= zp->z_size) { 10031669Sperrin error = ENOENT; 100410922SJeff.Bonwick@Sun.COM } else { 100510922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 100610922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 10071669Sperrin } 100810922SJeff.Bonwick@Sun.COM ASSERT(error == 0 || error == ENOENT); 10091669Sperrin } else { /* indirect write */ 1010789Sahrens /* 10111669Sperrin * Have to lock the whole block to ensure when it's 10121669Sperrin * written out and it's checksum is being calculated 10131669Sperrin * that no one can change the data. We need to re-check 10141669Sperrin * blocksize after we get the lock in case it's changed! 1015789Sahrens */ 10161669Sperrin for (;;) { 101710922SJeff.Bonwick@Sun.COM uint64_t blkoff; 101810922SJeff.Bonwick@Sun.COM size = zp->z_blksz; 101910945SJeff.Bonwick@Sun.COM blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 102010922SJeff.Bonwick@Sun.COM offset -= blkoff; 102110922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, 102210922SJeff.Bonwick@Sun.COM RL_READER); 102310922SJeff.Bonwick@Sun.COM if (zp->z_blksz == size) 10241669Sperrin break; 102510922SJeff.Bonwick@Sun.COM offset += blkoff; 102610922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 10271669Sperrin } 10281669Sperrin /* test for truncation needs to be done while range locked */ 102911935SMark.Shellenbaum@Sun.COM if (lr->lr_offset >= zp->z_size) 10301669Sperrin error = ENOENT; 103110209SMark.Musante@Sun.COM #ifdef DEBUG 103210209SMark.Musante@Sun.COM if (zil_fault_io) { 103310209SMark.Musante@Sun.COM error = EIO; 103410209SMark.Musante@Sun.COM zil_fault_io = 0; 103510209SMark.Musante@Sun.COM } 103610209SMark.Musante@Sun.COM #endif 103710922SJeff.Bonwick@Sun.COM if (error == 0) 103812285SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db, 103912285SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 104010922SJeff.Bonwick@Sun.COM 104110800SNeil.Perrin@Sun.COM if (error == 0) { 104210922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 104310922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 104410922SJeff.Bonwick@Sun.COM 104510922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 104610922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 104710922SJeff.Bonwick@Sun.COM 104810922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 104910922SJeff.Bonwick@Sun.COM zfs_get_done, zgd); 105010922SJeff.Bonwick@Sun.COM ASSERT(error || lr->lr_length <= zp->z_blksz); 105110922SJeff.Bonwick@Sun.COM 105210800SNeil.Perrin@Sun.COM /* 105310922SJeff.Bonwick@Sun.COM * On success, we need to wait for the write I/O 105410922SJeff.Bonwick@Sun.COM * initiated by dmu_sync() to complete before we can 105510922SJeff.Bonwick@Sun.COM * release this dbuf. We will finish everything up 105610922SJeff.Bonwick@Sun.COM * in the zfs_get_done() callback. 105710800SNeil.Perrin@Sun.COM */ 105810922SJeff.Bonwick@Sun.COM if (error == 0) 105910922SJeff.Bonwick@Sun.COM return (0); 106010922SJeff.Bonwick@Sun.COM 106110922SJeff.Bonwick@Sun.COM if (error == EALREADY) { 106210922SJeff.Bonwick@Sun.COM lr->lr_common.lrc_txtype = TX_WRITE2; 106310922SJeff.Bonwick@Sun.COM error = 0; 106410922SJeff.Bonwick@Sun.COM } 106510800SNeil.Perrin@Sun.COM } 1066789Sahrens } 106710922SJeff.Bonwick@Sun.COM 106810922SJeff.Bonwick@Sun.COM zfs_get_done(zgd, error); 106910922SJeff.Bonwick@Sun.COM 1070789Sahrens return (error); 1071789Sahrens } 1072789Sahrens 1073789Sahrens /*ARGSUSED*/ 1074789Sahrens static int 10755331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 10765331Samw caller_context_t *ct) 1077789Sahrens { 1078789Sahrens znode_t *zp = VTOZ(vp); 1079789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1080789Sahrens int error; 1081789Sahrens 10825367Sahrens ZFS_ENTER(zfsvfs); 10835367Sahrens ZFS_VERIFY_ZP(zp); 10845331Samw 10855331Samw if (flag & V_ACE_MASK) 10865331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 10875331Samw else 10885331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 10895331Samw 1090789Sahrens ZFS_EXIT(zfsvfs); 1091789Sahrens return (error); 1092789Sahrens } 1093789Sahrens 1094789Sahrens /* 10959981STim.Haley@Sun.COM * If vnode is for a device return a specfs vnode instead. 10969981STim.Haley@Sun.COM */ 10979981STim.Haley@Sun.COM static int 10989981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr) 10999981STim.Haley@Sun.COM { 11009981STim.Haley@Sun.COM int error = 0; 11019981STim.Haley@Sun.COM 11029981STim.Haley@Sun.COM if (IS_DEVVP(*vpp)) { 11039981STim.Haley@Sun.COM struct vnode *svp; 11049981STim.Haley@Sun.COM 11059981STim.Haley@Sun.COM svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 11069981STim.Haley@Sun.COM VN_RELE(*vpp); 11079981STim.Haley@Sun.COM if (svp == NULL) 11089981STim.Haley@Sun.COM error = ENOSYS; 11099981STim.Haley@Sun.COM *vpp = svp; 11109981STim.Haley@Sun.COM } 11119981STim.Haley@Sun.COM return (error); 11129981STim.Haley@Sun.COM } 11139981STim.Haley@Sun.COM 11149981STim.Haley@Sun.COM 11159981STim.Haley@Sun.COM /* 1116789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1117789Sahrens * If it exists, return a held vnode reference for it. 1118789Sahrens * 1119789Sahrens * IN: dvp - vnode of directory to search. 1120789Sahrens * nm - name of entry to lookup. 1121789Sahrens * pnp - full pathname to lookup [UNUSED]. 1122789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1123789Sahrens * rdir - root directory vnode [UNUSED]. 1124789Sahrens * cr - credentials of caller. 11255331Samw * ct - caller context 11265331Samw * direntflags - directory lookup flags 11275331Samw * realpnp - returned pathname. 1128789Sahrens * 1129789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1130789Sahrens * 1131789Sahrens * RETURN: 0 if success 1132789Sahrens * error code if failure 1133789Sahrens * 1134789Sahrens * Timestamps: 1135789Sahrens * NA 1136789Sahrens */ 1137789Sahrens /* ARGSUSED */ 1138789Sahrens static int 1139789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 11405331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 11415331Samw int *direntflags, pathname_t *realpnp) 1142789Sahrens { 1143789Sahrens znode_t *zdp = VTOZ(dvp); 1144789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 11459981STim.Haley@Sun.COM int error = 0; 11469981STim.Haley@Sun.COM 11479981STim.Haley@Sun.COM /* fast path */ 11489981STim.Haley@Sun.COM if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 11499981STim.Haley@Sun.COM 11509981STim.Haley@Sun.COM if (dvp->v_type != VDIR) { 11519981STim.Haley@Sun.COM return (ENOTDIR); 115211935SMark.Shellenbaum@Sun.COM } else if (zdp->z_sa_hdl == NULL) { 11539981STim.Haley@Sun.COM return (EIO); 11549981STim.Haley@Sun.COM } 11559981STim.Haley@Sun.COM 11569981STim.Haley@Sun.COM if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 11579981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11589981STim.Haley@Sun.COM if (!error) { 11599981STim.Haley@Sun.COM *vpp = dvp; 11609981STim.Haley@Sun.COM VN_HOLD(*vpp); 11619981STim.Haley@Sun.COM return (0); 11629981STim.Haley@Sun.COM } 11639981STim.Haley@Sun.COM return (error); 11649981STim.Haley@Sun.COM } else { 11659981STim.Haley@Sun.COM vnode_t *tvp = dnlc_lookup(dvp, nm); 11669981STim.Haley@Sun.COM 11679981STim.Haley@Sun.COM if (tvp) { 11689981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11699981STim.Haley@Sun.COM if (error) { 11709981STim.Haley@Sun.COM VN_RELE(tvp); 11719981STim.Haley@Sun.COM return (error); 11729981STim.Haley@Sun.COM } 11739981STim.Haley@Sun.COM if (tvp == DNLC_NO_VNODE) { 11749981STim.Haley@Sun.COM VN_RELE(tvp); 11759981STim.Haley@Sun.COM return (ENOENT); 11769981STim.Haley@Sun.COM } else { 11779981STim.Haley@Sun.COM *vpp = tvp; 11789981STim.Haley@Sun.COM return (specvp_check(vpp, cr)); 11799981STim.Haley@Sun.COM } 11809981STim.Haley@Sun.COM } 11819981STim.Haley@Sun.COM } 11829981STim.Haley@Sun.COM } 11839981STim.Haley@Sun.COM 11849981STim.Haley@Sun.COM DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1185789Sahrens 11865367Sahrens ZFS_ENTER(zfsvfs); 11875367Sahrens ZFS_VERIFY_ZP(zdp); 1188789Sahrens 1189789Sahrens *vpp = NULL; 1190789Sahrens 1191789Sahrens if (flags & LOOKUP_XATTR) { 1192789Sahrens /* 11933234Sck153898 * If the xattr property is off, refuse the lookup request. 11943234Sck153898 */ 11953234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 11963234Sck153898 ZFS_EXIT(zfsvfs); 11973234Sck153898 return (EINVAL); 11983234Sck153898 } 11993234Sck153898 12003234Sck153898 /* 1201789Sahrens * We don't allow recursive attributes.. 1202789Sahrens * Maybe someday we will. 1203789Sahrens */ 120411935SMark.Shellenbaum@Sun.COM if (zdp->z_pflags & ZFS_XATTR) { 1205789Sahrens ZFS_EXIT(zfsvfs); 1206789Sahrens return (EINVAL); 1207789Sahrens } 1208789Sahrens 12093280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1210789Sahrens ZFS_EXIT(zfsvfs); 1211789Sahrens return (error); 1212789Sahrens } 1213789Sahrens 1214789Sahrens /* 1215789Sahrens * Do we have permission to get into attribute directory? 1216789Sahrens */ 1217789Sahrens 12185331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 12195331Samw B_FALSE, cr)) { 1220789Sahrens VN_RELE(*vpp); 12215331Samw *vpp = NULL; 1222789Sahrens } 1223789Sahrens 1224789Sahrens ZFS_EXIT(zfsvfs); 1225789Sahrens return (error); 1226789Sahrens } 1227789Sahrens 12281512Sek110237 if (dvp->v_type != VDIR) { 12291512Sek110237 ZFS_EXIT(zfsvfs); 12301460Smarks return (ENOTDIR); 12311512Sek110237 } 12321460Smarks 1233789Sahrens /* 1234789Sahrens * Check accessibility of directory. 1235789Sahrens */ 1236789Sahrens 12375331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1238789Sahrens ZFS_EXIT(zfsvfs); 1239789Sahrens return (error); 1240789Sahrens } 1241789Sahrens 12425498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 12435331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 12445331Samw ZFS_EXIT(zfsvfs); 12455331Samw return (EILSEQ); 12465331Samw } 12475331Samw 12485331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 12499981STim.Haley@Sun.COM if (error == 0) 12509981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1251789Sahrens 1252789Sahrens ZFS_EXIT(zfsvfs); 1253789Sahrens return (error); 1254789Sahrens } 1255789Sahrens 1256789Sahrens /* 1257789Sahrens * Attempt to create a new entry in a directory. If the entry 1258789Sahrens * already exists, truncate the file if permissible, else return 1259789Sahrens * an error. Return the vp of the created or trunc'd file. 1260789Sahrens * 1261789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1262789Sahrens * name - name of new file entry. 1263789Sahrens * vap - attributes of new file. 1264789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1265789Sahrens * mode - mode to open file with. 1266789Sahrens * cr - credentials of caller. 1267789Sahrens * flag - large file flag [UNUSED]. 12685331Samw * ct - caller context 12695331Samw * vsecp - ACL to be set 1270789Sahrens * 1271789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1272789Sahrens * 1273789Sahrens * RETURN: 0 if success 1274789Sahrens * error code if failure 1275789Sahrens * 1276789Sahrens * Timestamps: 1277789Sahrens * dvp - ctime|mtime updated if new entry created 1278789Sahrens * vp - ctime|mtime always, atime if new 1279789Sahrens */ 12805331Samw 1281789Sahrens /* ARGSUSED */ 1282789Sahrens static int 1283789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 12845331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 12855331Samw vsecattr_t *vsecp) 1286789Sahrens { 1287789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1288789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 12895326Sek110237 zilog_t *zilog; 12905326Sek110237 objset_t *os; 1291789Sahrens zfs_dirlock_t *dl; 1292789Sahrens dmu_tx_t *tx; 1293789Sahrens int error; 12947847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 12957847SMark.Shellenbaum@Sun.COM uid_t uid; 12967847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 129711935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 12989179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 129912302SMark.Shellenbaum@Sun.COM boolean_t have_acl = B_FALSE; 13005331Samw 13015331Samw /* 13025331Samw * If we have an ephemeral id, ACL, or XVATTR then 13035331Samw * make sure file system is at proper version 13045331Samw */ 13055331Samw 13067847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 13077847SMark.Shellenbaum@Sun.COM if (ksid) 13087847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 13097847SMark.Shellenbaum@Sun.COM else 13107847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 13117847SMark.Shellenbaum@Sun.COM 13125331Samw if (zfsvfs->z_use_fuids == B_FALSE && 13135331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 13147847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 13155331Samw return (EINVAL); 1316789Sahrens 13175367Sahrens ZFS_ENTER(zfsvfs); 13185367Sahrens ZFS_VERIFY_ZP(dzp); 13195326Sek110237 os = zfsvfs->z_os; 13205326Sek110237 zilog = zfsvfs->z_log; 1321789Sahrens 13225498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 13235331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 13245331Samw ZFS_EXIT(zfsvfs); 13255331Samw return (EILSEQ); 13265331Samw } 13275331Samw 13285331Samw if (vap->va_mask & AT_XVATTR) { 13295331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 13305331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 13315331Samw ZFS_EXIT(zfsvfs); 13325331Samw return (error); 13335331Samw } 13345331Samw } 1335789Sahrens top: 1336789Sahrens *vpp = NULL; 1337789Sahrens 1338789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1339789Sahrens vap->va_mode &= ~VSVTX; 1340789Sahrens 1341789Sahrens if (*name == '\0') { 1342789Sahrens /* 1343789Sahrens * Null component name refers to the directory itself. 1344789Sahrens */ 1345789Sahrens VN_HOLD(dvp); 1346789Sahrens zp = dzp; 1347789Sahrens dl = NULL; 1348789Sahrens error = 0; 1349789Sahrens } else { 1350789Sahrens /* possible VN_HOLD(zp) */ 13515331Samw int zflg = 0; 13525331Samw 13535331Samw if (flag & FIGNORECASE) 13545331Samw zflg |= ZCILOOK; 13555331Samw 13565331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13575331Samw NULL, NULL); 13585331Samw if (error) { 1359789Sahrens if (strcmp(name, "..") == 0) 1360789Sahrens error = EISDIR; 1361789Sahrens ZFS_EXIT(zfsvfs); 1362789Sahrens return (error); 1363789Sahrens } 1364789Sahrens } 136511935SMark.Shellenbaum@Sun.COM 1366789Sahrens if (zp == NULL) { 13675331Samw uint64_t txtype; 13685331Samw 1369789Sahrens /* 1370789Sahrens * Create a new file object and update the directory 1371789Sahrens * to reference it. 1372789Sahrens */ 13735331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1374789Sahrens goto out; 1375789Sahrens } 1376789Sahrens 1377789Sahrens /* 1378789Sahrens * We only support the creation of regular files in 1379789Sahrens * extended attribute directories. 1380789Sahrens */ 138111935SMark.Shellenbaum@Sun.COM 138211935SMark.Shellenbaum@Sun.COM if ((dzp->z_pflags & ZFS_XATTR) && 1383789Sahrens (vap->va_type != VREG)) { 1384789Sahrens error = EINVAL; 1385789Sahrens goto out; 1386789Sahrens } 1387789Sahrens 138812302SMark.Shellenbaum@Sun.COM if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 138912302SMark.Shellenbaum@Sun.COM cr, vsecp, &acl_ids)) != 0) 13909179SMark.Shellenbaum@Sun.COM goto out; 139112302SMark.Shellenbaum@Sun.COM have_acl = B_TRUE; 139212302SMark.Shellenbaum@Sun.COM 13939396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 139410143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 13959396SMatthew.Ahrens@Sun.COM error = EDQUOT; 13969396SMatthew.Ahrens@Sun.COM goto out; 13979396SMatthew.Ahrens@Sun.COM } 13989179SMark.Shellenbaum@Sun.COM 1399789Sahrens tx = dmu_tx_create(os); 140011935SMark.Shellenbaum@Sun.COM 140111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 140211935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 140311935SMark.Shellenbaum@Sun.COM 14049179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 14059396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 14069396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 14071544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 140811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 140911935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && 141011935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1411789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 141211935SMark.Shellenbaum@Sun.COM 0, acl_ids.z_aclp->z_acl_bytes); 14135331Samw } 14148227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1415789Sahrens if (error) { 1416789Sahrens zfs_dirent_unlock(dl); 14178227SNeil.Perrin@Sun.COM if (error == ERESTART) { 14182113Sahrens dmu_tx_wait(tx); 14192113Sahrens dmu_tx_abort(tx); 1420789Sahrens goto top; 1421789Sahrens } 142212302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 14232113Sahrens dmu_tx_abort(tx); 1424789Sahrens ZFS_EXIT(zfsvfs); 1425789Sahrens return (error); 1426789Sahrens } 142711935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 14289179SMark.Shellenbaum@Sun.COM 14299179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 14309179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 14319179SMark.Shellenbaum@Sun.COM 1432789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 14335331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 14345331Samw if (flag & FIGNORECASE) 14355331Samw txtype |= TX_CI; 14365331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 14379179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 14389179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1439789Sahrens dmu_tx_commit(tx); 1440789Sahrens } else { 14415331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 14425331Samw 1443789Sahrens /* 1444789Sahrens * A directory entry already exists for this name. 1445789Sahrens */ 1446789Sahrens /* 1447789Sahrens * Can't truncate an existing file if in exclusive mode. 1448789Sahrens */ 1449789Sahrens if (excl == EXCL) { 1450789Sahrens error = EEXIST; 1451789Sahrens goto out; 1452789Sahrens } 1453789Sahrens /* 1454789Sahrens * Can't open a directory for writing. 1455789Sahrens */ 1456789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1457789Sahrens error = EISDIR; 1458789Sahrens goto out; 1459789Sahrens } 1460789Sahrens /* 1461789Sahrens * Verify requested access to file. 1462789Sahrens */ 14635331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1464789Sahrens goto out; 1465789Sahrens } 1466789Sahrens 1467789Sahrens mutex_enter(&dzp->z_lock); 1468789Sahrens dzp->z_seq++; 1469789Sahrens mutex_exit(&dzp->z_lock); 1470789Sahrens 14711878Smaybee /* 14721878Smaybee * Truncate regular files if requested. 14731878Smaybee */ 14741878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1475789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 14766992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 14776992Smaybee zfs_dirent_unlock(dl); 14786992Smaybee dl = NULL; 14791878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 14804863Spraks if (error == 0) { 14815331Samw vnevent_create(ZTOV(zp), ct); 14824863Spraks } 1483789Sahrens } 1484789Sahrens } 1485789Sahrens out: 1486789Sahrens 1487789Sahrens if (dl) 1488789Sahrens zfs_dirent_unlock(dl); 1489789Sahrens 1490789Sahrens if (error) { 1491789Sahrens if (zp) 1492789Sahrens VN_RELE(ZTOV(zp)); 1493789Sahrens } else { 1494789Sahrens *vpp = ZTOV(zp); 14959981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1496789Sahrens } 1497789Sahrens 149812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 149912699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 150012294SMark.Musante@Sun.COM 1501789Sahrens ZFS_EXIT(zfsvfs); 1502789Sahrens return (error); 1503789Sahrens } 1504789Sahrens 1505789Sahrens /* 1506789Sahrens * Remove an entry from a directory. 1507789Sahrens * 1508789Sahrens * IN: dvp - vnode of directory to remove entry from. 1509789Sahrens * name - name of entry to remove. 1510789Sahrens * cr - credentials of caller. 15115331Samw * ct - caller context 15125331Samw * flags - case flags 1513789Sahrens * 1514789Sahrens * RETURN: 0 if success 1515789Sahrens * error code if failure 1516789Sahrens * 1517789Sahrens * Timestamps: 1518789Sahrens * dvp - ctime|mtime 1519789Sahrens * vp - ctime (if nlink > 0) 1520789Sahrens */ 152111935SMark.Shellenbaum@Sun.COM 152211935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0; 152311935SMark.Shellenbaum@Sun.COM 15245331Samw /*ARGSUSED*/ 1525789Sahrens static int 15265331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 15275331Samw int flags) 1528789Sahrens { 1529789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1530789Sahrens znode_t *xzp = NULL; 1531789Sahrens vnode_t *vp; 1532789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15335326Sek110237 zilog_t *zilog; 153411935SMark.Shellenbaum@Sun.COM uint64_t acl_obj, xattr_obj = 0; 153511935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj_unlinked = 0; 153612700SNeil.Perrin@Sun.COM uint64_t obj = 0; 1537789Sahrens zfs_dirlock_t *dl; 1538789Sahrens dmu_tx_t *tx; 15393461Sahrens boolean_t may_delete_now, delete_now = FALSE; 15406992Smaybee boolean_t unlinked, toobig = FALSE; 15415331Samw uint64_t txtype; 15425331Samw pathname_t *realnmp = NULL; 15435331Samw pathname_t realnm; 1544789Sahrens int error; 15455331Samw int zflg = ZEXISTS; 1546789Sahrens 15475367Sahrens ZFS_ENTER(zfsvfs); 15485367Sahrens ZFS_VERIFY_ZP(dzp); 15495326Sek110237 zilog = zfsvfs->z_log; 1550789Sahrens 15515331Samw if (flags & FIGNORECASE) { 15525331Samw zflg |= ZCILOOK; 15535331Samw pn_alloc(&realnm); 15545331Samw realnmp = &realnm; 15555331Samw } 15565331Samw 1557789Sahrens top: 1558789Sahrens /* 1559789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1560789Sahrens */ 15615331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 15625331Samw NULL, realnmp)) { 15635331Samw if (realnmp) 15645331Samw pn_free(realnmp); 1565789Sahrens ZFS_EXIT(zfsvfs); 1566789Sahrens return (error); 1567789Sahrens } 1568789Sahrens 1569789Sahrens vp = ZTOV(zp); 1570789Sahrens 1571789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1572789Sahrens goto out; 1573789Sahrens } 1574789Sahrens 1575789Sahrens /* 1576789Sahrens * Need to use rmdir for removing directories. 1577789Sahrens */ 1578789Sahrens if (vp->v_type == VDIR) { 1579789Sahrens error = EPERM; 1580789Sahrens goto out; 1581789Sahrens } 1582789Sahrens 15835331Samw vnevent_remove(vp, dvp, name, ct); 15845331Samw 15855331Samw if (realnmp) 15866492Stimh dnlc_remove(dvp, realnmp->pn_buf); 15875331Samw else 15885331Samw dnlc_remove(dvp, name); 15891484Sek110237 1590789Sahrens mutex_enter(&vp->v_lock); 1591789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1592789Sahrens mutex_exit(&vp->v_lock); 1593789Sahrens 1594789Sahrens /* 15953461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1596789Sahrens * it depends on whether we're the last link, and on whether there are 1597789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1598789Sahrens * allow for either case. 1599789Sahrens */ 160012700SNeil.Perrin@Sun.COM obj = zp->z_id; 1601789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16021544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 160311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 160411935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 160511935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 16066992Smaybee if (may_delete_now) { 16076992Smaybee toobig = 160811935SMark.Shellenbaum@Sun.COM zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 16096992Smaybee /* if the file is too big, only hold_free a token amount */ 16106992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 16116992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 16126992Smaybee } 1613789Sahrens 1614789Sahrens /* are there any extended attributes? */ 161511935SMark.Shellenbaum@Sun.COM error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 161611935SMark.Shellenbaum@Sun.COM &xattr_obj, sizeof (xattr_obj)); 161711935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 161811935SMark.Shellenbaum@Sun.COM error = zfs_zget(zfsvfs, xattr_obj, &xzp); 161911935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 162011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 162111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1622789Sahrens } 1623789Sahrens 162412620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 162512620SMark.Shellenbaum@Oracle.COM if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1626789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 162712620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 1628789Sahrens 1629789Sahrens /* charge as an update -- would be nice not to charge at all */ 16303461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1631789Sahrens 16328227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1633789Sahrens if (error) { 1634789Sahrens zfs_dirent_unlock(dl); 1635789Sahrens VN_RELE(vp); 16368227SNeil.Perrin@Sun.COM if (error == ERESTART) { 16372113Sahrens dmu_tx_wait(tx); 16382113Sahrens dmu_tx_abort(tx); 1639789Sahrens goto top; 1640789Sahrens } 16415331Samw if (realnmp) 16425331Samw pn_free(realnmp); 16432113Sahrens dmu_tx_abort(tx); 1644789Sahrens ZFS_EXIT(zfsvfs); 1645789Sahrens return (error); 1646789Sahrens } 1647789Sahrens 1648789Sahrens /* 1649789Sahrens * Remove the directory entry. 1650789Sahrens */ 16515331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1652789Sahrens 1653789Sahrens if (error) { 1654789Sahrens dmu_tx_commit(tx); 1655789Sahrens goto out; 1656789Sahrens } 1657789Sahrens 16583461Sahrens if (unlinked) { 165911935SMark.Shellenbaum@Sun.COM 166012620SMark.Shellenbaum@Oracle.COM /* 166112620SMark.Shellenbaum@Oracle.COM * Hold z_lock so that we can make sure that the ACL obj 166212620SMark.Shellenbaum@Oracle.COM * hasn't changed. Could have been deleted due to 166312620SMark.Shellenbaum@Oracle.COM * zfs_sa_upgrade(). 166412620SMark.Shellenbaum@Oracle.COM */ 166512620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 1666789Sahrens mutex_enter(&vp->v_lock); 166711935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 166811935SMark.Shellenbaum@Sun.COM &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 16696992Smaybee delete_now = may_delete_now && !toobig && 1670789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 167112620SMark.Shellenbaum@Oracle.COM xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 167211935SMark.Shellenbaum@Sun.COM acl_obj; 1673789Sahrens mutex_exit(&vp->v_lock); 1674789Sahrens } 1675789Sahrens 1676789Sahrens if (delete_now) { 167711935SMark.Shellenbaum@Sun.COM if (xattr_obj_unlinked) { 167811935SMark.Shellenbaum@Sun.COM ASSERT3U(xzp->z_links, ==, 2); 1679789Sahrens mutex_enter(&xzp->z_lock); 16803461Sahrens xzp->z_unlinked = 1; 168111935SMark.Shellenbaum@Sun.COM xzp->z_links = 0; 168211935SMark.Shellenbaum@Sun.COM error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 168311935SMark.Shellenbaum@Sun.COM &xzp->z_links, sizeof (xzp->z_links), tx); 168411935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1685789Sahrens mutex_exit(&xzp->z_lock); 16863461Sahrens zfs_unlinked_add(xzp, tx); 168712620SMark.Shellenbaum@Oracle.COM 168811935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 168911935SMark.Shellenbaum@Sun.COM error = sa_remove(zp->z_sa_hdl, 169011935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), tx); 169111935SMark.Shellenbaum@Sun.COM else 169211935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, 169311935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), &null_xattr, 169411935SMark.Shellenbaum@Sun.COM sizeof (uint64_t), tx); 169511935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1696789Sahrens } 1697789Sahrens mutex_enter(&vp->v_lock); 1698789Sahrens vp->v_count--; 1699789Sahrens ASSERT3U(vp->v_count, ==, 0); 1700789Sahrens mutex_exit(&vp->v_lock); 1701789Sahrens mutex_exit(&zp->z_lock); 1702789Sahrens zfs_znode_delete(zp, tx); 17033461Sahrens } else if (unlinked) { 170412620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 17053461Sahrens zfs_unlinked_add(zp, tx); 1706789Sahrens } 1707789Sahrens 17085331Samw txtype = TX_REMOVE; 17095331Samw if (flags & FIGNORECASE) 17105331Samw txtype |= TX_CI; 171112700SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 1712789Sahrens 1713789Sahrens dmu_tx_commit(tx); 1714789Sahrens out: 17155331Samw if (realnmp) 17165331Samw pn_free(realnmp); 17175331Samw 1718789Sahrens zfs_dirent_unlock(dl); 1719789Sahrens 172012178SMark.Shellenbaum@Sun.COM if (!delete_now) 1721789Sahrens VN_RELE(vp); 172212178SMark.Shellenbaum@Sun.COM if (xzp) 1723789Sahrens VN_RELE(ZTOV(xzp)); 1724789Sahrens 172512294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 172612699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 172712294SMark.Musante@Sun.COM 1728789Sahrens ZFS_EXIT(zfsvfs); 1729789Sahrens return (error); 1730789Sahrens } 1731789Sahrens 1732789Sahrens /* 1733789Sahrens * Create a new directory and insert it into dvp using the name 1734789Sahrens * provided. Return a pointer to the inserted directory. 1735789Sahrens * 1736789Sahrens * IN: dvp - vnode of directory to add subdir to. 1737789Sahrens * dirname - name of new directory. 1738789Sahrens * vap - attributes of new directory. 1739789Sahrens * cr - credentials of caller. 17405331Samw * ct - caller context 17415331Samw * vsecp - ACL to be set 1742789Sahrens * 1743789Sahrens * OUT: vpp - vnode of created directory. 1744789Sahrens * 1745789Sahrens * RETURN: 0 if success 1746789Sahrens * error code if failure 1747789Sahrens * 1748789Sahrens * Timestamps: 1749789Sahrens * dvp - ctime|mtime updated 1750789Sahrens * vp - ctime|mtime|atime updated 1751789Sahrens */ 17525331Samw /*ARGSUSED*/ 1753789Sahrens static int 17545331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 17555331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1756789Sahrens { 1757789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1758789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17595326Sek110237 zilog_t *zilog; 1760789Sahrens zfs_dirlock_t *dl; 17615331Samw uint64_t txtype; 1762789Sahrens dmu_tx_t *tx; 1763789Sahrens int error; 17645331Samw int zf = ZNEW; 17657847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 17667847SMark.Shellenbaum@Sun.COM uid_t uid; 17677847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 176811935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 17699179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1770789Sahrens 1771789Sahrens ASSERT(vap->va_type == VDIR); 1772789Sahrens 17735331Samw /* 17745331Samw * If we have an ephemeral id, ACL, or XVATTR then 17755331Samw * make sure file system is at proper version 17765331Samw */ 17775331Samw 17787847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 17797847SMark.Shellenbaum@Sun.COM if (ksid) 17807847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 17817847SMark.Shellenbaum@Sun.COM else 17827847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 17835331Samw if (zfsvfs->z_use_fuids == B_FALSE && 17847847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 17857876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 17865331Samw return (EINVAL); 17875331Samw 17885367Sahrens ZFS_ENTER(zfsvfs); 17895367Sahrens ZFS_VERIFY_ZP(dzp); 17905326Sek110237 zilog = zfsvfs->z_log; 1791789Sahrens 179211935SMark.Shellenbaum@Sun.COM if (dzp->z_pflags & ZFS_XATTR) { 1793789Sahrens ZFS_EXIT(zfsvfs); 1794789Sahrens return (EINVAL); 1795789Sahrens } 17965331Samw 17975498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 17985331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 17995331Samw ZFS_EXIT(zfsvfs); 18005331Samw return (EILSEQ); 18015331Samw } 18025331Samw if (flags & FIGNORECASE) 18035331Samw zf |= ZCILOOK; 18045331Samw 180512302SMark.Shellenbaum@Sun.COM if (vap->va_mask & AT_XVATTR) { 18065331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 18075331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 18085331Samw ZFS_EXIT(zfsvfs); 18095331Samw return (error); 18105331Samw } 181112302SMark.Shellenbaum@Sun.COM } 181212302SMark.Shellenbaum@Sun.COM 181312302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 181412302SMark.Shellenbaum@Sun.COM vsecp, &acl_ids)) != 0) { 181512302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 181612302SMark.Shellenbaum@Sun.COM return (error); 181712302SMark.Shellenbaum@Sun.COM } 1818789Sahrens /* 1819789Sahrens * First make sure the new directory doesn't exist. 182012302SMark.Shellenbaum@Sun.COM * 182112302SMark.Shellenbaum@Sun.COM * Existence is checked first to make sure we don't return 182212302SMark.Shellenbaum@Sun.COM * EACCES instead of EEXIST which can cause some applications 182312302SMark.Shellenbaum@Sun.COM * to fail. 1824789Sahrens */ 18255331Samw top: 18265331Samw *vpp = NULL; 18275331Samw 18285331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 18295331Samw NULL, NULL)) { 183012302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1831789Sahrens ZFS_EXIT(zfsvfs); 1832789Sahrens return (error); 1833789Sahrens } 1834789Sahrens 18355331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 183612302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18371231Smarks zfs_dirent_unlock(dl); 18381231Smarks ZFS_EXIT(zfsvfs); 18391231Smarks return (error); 18401231Smarks } 18411231Smarks 18429396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 184310143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 18449396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 18459396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 18469396SMatthew.Ahrens@Sun.COM return (EDQUOT); 18479396SMatthew.Ahrens@Sun.COM } 18489179SMark.Shellenbaum@Sun.COM 1849789Sahrens /* 1850789Sahrens * Add a new entry to the directory. 1851789Sahrens */ 1852789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 18531544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 18541544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 18559179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 18569396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 18579396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 185811935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 185911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 186011935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 186111935SMark.Shellenbaum@Sun.COM } 186211935SMark.Shellenbaum@Sun.COM 186311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 186411935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 186511935SMark.Shellenbaum@Sun.COM 18668227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1867789Sahrens if (error) { 1868789Sahrens zfs_dirent_unlock(dl); 18698227SNeil.Perrin@Sun.COM if (error == ERESTART) { 18702113Sahrens dmu_tx_wait(tx); 18712113Sahrens dmu_tx_abort(tx); 1872789Sahrens goto top; 1873789Sahrens } 187412302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18752113Sahrens dmu_tx_abort(tx); 1876789Sahrens ZFS_EXIT(zfsvfs); 1877789Sahrens return (error); 1878789Sahrens } 1879789Sahrens 1880789Sahrens /* 1881789Sahrens * Create new node. 1882789Sahrens */ 188311935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 18849179SMark.Shellenbaum@Sun.COM 18859179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 18869179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 188711935SMark.Shellenbaum@Sun.COM 1888789Sahrens /* 1889789Sahrens * Now put new name in parent dir. 1890789Sahrens */ 1891789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1892789Sahrens 1893789Sahrens *vpp = ZTOV(zp); 1894789Sahrens 18955331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 18965331Samw if (flags & FIGNORECASE) 18975331Samw txtype |= TX_CI; 18989179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 18999179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 19009179SMark.Shellenbaum@Sun.COM 19019179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 190211935SMark.Shellenbaum@Sun.COM 1903789Sahrens dmu_tx_commit(tx); 1904789Sahrens 1905789Sahrens zfs_dirent_unlock(dl); 1906789Sahrens 190712294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 190812699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 190912294SMark.Musante@Sun.COM 1910789Sahrens ZFS_EXIT(zfsvfs); 1911789Sahrens return (0); 1912789Sahrens } 1913789Sahrens 1914789Sahrens /* 1915789Sahrens * Remove a directory subdir entry. If the current working 1916789Sahrens * directory is the same as the subdir to be removed, the 1917789Sahrens * remove will fail. 1918789Sahrens * 1919789Sahrens * IN: dvp - vnode of directory to remove from. 1920789Sahrens * name - name of directory to be removed. 1921789Sahrens * cwd - vnode of current working directory. 1922789Sahrens * cr - credentials of caller. 19235331Samw * ct - caller context 19245331Samw * flags - case flags 1925789Sahrens * 1926789Sahrens * RETURN: 0 if success 1927789Sahrens * error code if failure 1928789Sahrens * 1929789Sahrens * Timestamps: 1930789Sahrens * dvp - ctime|mtime updated 1931789Sahrens */ 19325331Samw /*ARGSUSED*/ 1933789Sahrens static int 19345331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 19355331Samw caller_context_t *ct, int flags) 1936789Sahrens { 1937789Sahrens znode_t *dzp = VTOZ(dvp); 1938789Sahrens znode_t *zp; 1939789Sahrens vnode_t *vp; 1940789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 19415326Sek110237 zilog_t *zilog; 1942789Sahrens zfs_dirlock_t *dl; 1943789Sahrens dmu_tx_t *tx; 1944789Sahrens int error; 19455331Samw int zflg = ZEXISTS; 1946789Sahrens 19475367Sahrens ZFS_ENTER(zfsvfs); 19485367Sahrens ZFS_VERIFY_ZP(dzp); 19495326Sek110237 zilog = zfsvfs->z_log; 1950789Sahrens 19515331Samw if (flags & FIGNORECASE) 19525331Samw zflg |= ZCILOOK; 1953789Sahrens top: 1954789Sahrens zp = NULL; 1955789Sahrens 1956789Sahrens /* 1957789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1958789Sahrens */ 19595331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 19605331Samw NULL, NULL)) { 1961789Sahrens ZFS_EXIT(zfsvfs); 1962789Sahrens return (error); 1963789Sahrens } 1964789Sahrens 1965789Sahrens vp = ZTOV(zp); 1966789Sahrens 1967789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1968789Sahrens goto out; 1969789Sahrens } 1970789Sahrens 1971789Sahrens if (vp->v_type != VDIR) { 1972789Sahrens error = ENOTDIR; 1973789Sahrens goto out; 1974789Sahrens } 1975789Sahrens 1976789Sahrens if (vp == cwd) { 1977789Sahrens error = EINVAL; 1978789Sahrens goto out; 1979789Sahrens } 1980789Sahrens 19815331Samw vnevent_rmdir(vp, dvp, name, ct); 1982789Sahrens 1983789Sahrens /* 19843897Smaybee * Grab a lock on the directory to make sure that noone is 19853897Smaybee * trying to add (or lookup) entries while we are removing it. 19863897Smaybee */ 19873897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 19883897Smaybee 19893897Smaybee /* 19903897Smaybee * Grab a lock on the parent pointer to make sure we play well 1991789Sahrens * with the treewalk and directory rename code. 1992789Sahrens */ 1993789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1994789Sahrens 1995789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 19961544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 199711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 19983461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 199911935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 200011935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 20018227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 2002789Sahrens if (error) { 2003789Sahrens rw_exit(&zp->z_parent_lock); 20043897Smaybee rw_exit(&zp->z_name_lock); 2005789Sahrens zfs_dirent_unlock(dl); 2006789Sahrens VN_RELE(vp); 20078227SNeil.Perrin@Sun.COM if (error == ERESTART) { 20082113Sahrens dmu_tx_wait(tx); 20092113Sahrens dmu_tx_abort(tx); 2010789Sahrens goto top; 2011789Sahrens } 20122113Sahrens dmu_tx_abort(tx); 2013789Sahrens ZFS_EXIT(zfsvfs); 2014789Sahrens return (error); 2015789Sahrens } 2016789Sahrens 20175331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 20185331Samw 20195331Samw if (error == 0) { 20205331Samw uint64_t txtype = TX_RMDIR; 20215331Samw if (flags & FIGNORECASE) 20225331Samw txtype |= TX_CI; 202312699SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 20245331Samw } 2025789Sahrens 2026789Sahrens dmu_tx_commit(tx); 2027789Sahrens 2028789Sahrens rw_exit(&zp->z_parent_lock); 20293897Smaybee rw_exit(&zp->z_name_lock); 2030789Sahrens out: 2031789Sahrens zfs_dirent_unlock(dl); 2032789Sahrens 2033789Sahrens VN_RELE(vp); 2034789Sahrens 203512294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 203612699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 203712294SMark.Musante@Sun.COM 2038789Sahrens ZFS_EXIT(zfsvfs); 2039789Sahrens return (error); 2040789Sahrens } 2041789Sahrens 2042789Sahrens /* 2043789Sahrens * Read as many directory entries as will fit into the provided 2044789Sahrens * buffer from the given directory cursor position (specified in 2045789Sahrens * the uio structure. 2046789Sahrens * 2047789Sahrens * IN: vp - vnode of directory to read. 2048789Sahrens * uio - structure supplying read location, range info, 2049789Sahrens * and return buffer. 2050789Sahrens * cr - credentials of caller. 20515331Samw * ct - caller context 20525331Samw * flags - case flags 2053789Sahrens * 2054789Sahrens * OUT: uio - updated offset and range, buffer filled. 2055789Sahrens * eofp - set to true if end-of-file detected. 2056789Sahrens * 2057789Sahrens * RETURN: 0 if success 2058789Sahrens * error code if failure 2059789Sahrens * 2060789Sahrens * Timestamps: 2061789Sahrens * vp - atime updated 2062789Sahrens * 2063789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 2064789Sahrens * This allows us to use the low range for "special" directory entries: 2065789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2066789Sahrens * we use the offset 2 for the '.zfs' directory. 2067789Sahrens */ 2068789Sahrens /* ARGSUSED */ 2069789Sahrens static int 20705331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 20715331Samw caller_context_t *ct, int flags) 2072789Sahrens { 2073789Sahrens znode_t *zp = VTOZ(vp); 2074789Sahrens iovec_t *iovp; 20755331Samw edirent_t *eodp; 2076789Sahrens dirent64_t *odp; 2077789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2078869Sperrin objset_t *os; 2079789Sahrens caddr_t outbuf; 2080789Sahrens size_t bufsize; 2081789Sahrens zap_cursor_t zc; 2082789Sahrens zap_attribute_t zap; 2083789Sahrens uint_t bytes_wanted; 2084789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 208511935SMark.Shellenbaum@Sun.COM uint64_t parent; 2086789Sahrens int local_eof; 2087869Sperrin int outcount; 2088869Sperrin int error; 2089869Sperrin uint8_t prefetch; 20905663Sck153898 boolean_t check_sysattrs; 2091789Sahrens 20925367Sahrens ZFS_ENTER(zfsvfs); 20935367Sahrens ZFS_VERIFY_ZP(zp); 2094789Sahrens 209511935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 209611935SMark.Shellenbaum@Sun.COM &parent, sizeof (parent))) != 0) { 209711935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 209811935SMark.Shellenbaum@Sun.COM return (error); 209911935SMark.Shellenbaum@Sun.COM } 210011935SMark.Shellenbaum@Sun.COM 2101789Sahrens /* 2102789Sahrens * If we are not given an eof variable, 2103789Sahrens * use a local one. 2104789Sahrens */ 2105789Sahrens if (eofp == NULL) 2106789Sahrens eofp = &local_eof; 2107789Sahrens 2108789Sahrens /* 2109789Sahrens * Check for valid iov_len. 2110789Sahrens */ 2111789Sahrens if (uio->uio_iov->iov_len <= 0) { 2112789Sahrens ZFS_EXIT(zfsvfs); 2113789Sahrens return (EINVAL); 2114789Sahrens } 2115789Sahrens 2116789Sahrens /* 2117789Sahrens * Quit if directory has been removed (posix) 2118789Sahrens */ 21193461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 2120789Sahrens ZFS_EXIT(zfsvfs); 2121789Sahrens return (0); 2122789Sahrens } 2123789Sahrens 2124869Sperrin error = 0; 2125869Sperrin os = zfsvfs->z_os; 2126869Sperrin offset = uio->uio_loffset; 2127869Sperrin prefetch = zp->z_zn_prefetch; 2128869Sperrin 2129789Sahrens /* 2130789Sahrens * Initialize the iterator cursor. 2131789Sahrens */ 2132789Sahrens if (offset <= 3) { 2133789Sahrens /* 2134789Sahrens * Start iteration from the beginning of the directory. 2135789Sahrens */ 2136869Sperrin zap_cursor_init(&zc, os, zp->z_id); 2137789Sahrens } else { 2138789Sahrens /* 2139789Sahrens * The offset is a serialized cursor. 2140789Sahrens */ 2141869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2142789Sahrens } 2143789Sahrens 2144789Sahrens /* 2145789Sahrens * Get space to change directory entries into fs independent format. 2146789Sahrens */ 2147789Sahrens iovp = uio->uio_iov; 2148789Sahrens bytes_wanted = iovp->iov_len; 2149789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2150789Sahrens bufsize = bytes_wanted; 2151789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 2152789Sahrens odp = (struct dirent64 *)outbuf; 2153789Sahrens } else { 2154789Sahrens bufsize = bytes_wanted; 2155789Sahrens odp = (struct dirent64 *)iovp->iov_base; 2156789Sahrens } 21575331Samw eodp = (struct edirent *)odp; 2158789Sahrens 2159789Sahrens /* 21607757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 21617757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 21627757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 21637757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 21645663Sck153898 */ 21657757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 21665663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 21675663Sck153898 (flags & V_RDDIR_ENTFLAGS); 21685663Sck153898 21695663Sck153898 /* 2170789Sahrens * Transform to file-system independent format 2171789Sahrens */ 2172789Sahrens outcount = 0; 2173789Sahrens while (outcount < bytes_wanted) { 21743912Slling ino64_t objnum; 21753912Slling ushort_t reclen; 21763912Slling off64_t *next; 21773912Slling 2178789Sahrens /* 2179789Sahrens * Special case `.', `..', and `.zfs'. 2180789Sahrens */ 2181789Sahrens if (offset == 0) { 2182789Sahrens (void) strcpy(zap.za_name, "."); 21835331Samw zap.za_normalization_conflict = 0; 21843912Slling objnum = zp->z_id; 2185789Sahrens } else if (offset == 1) { 2186789Sahrens (void) strcpy(zap.za_name, ".."); 21875331Samw zap.za_normalization_conflict = 0; 218811935SMark.Shellenbaum@Sun.COM objnum = parent; 2189789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2190789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 21915331Samw zap.za_normalization_conflict = 0; 21923912Slling objnum = ZFSCTL_INO_ROOT; 2193789Sahrens } else { 2194789Sahrens /* 2195789Sahrens * Grab next entry. 2196789Sahrens */ 2197789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2198789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2199789Sahrens break; 2200789Sahrens else 2201789Sahrens goto update; 2202789Sahrens } 2203789Sahrens 2204789Sahrens if (zap.za_integer_length != 8 || 2205789Sahrens zap.za_num_integers != 1) { 2206789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2207789Sahrens "entry, obj = %lld, offset = %lld\n", 2208789Sahrens (u_longlong_t)zp->z_id, 2209789Sahrens (u_longlong_t)offset); 2210789Sahrens error = ENXIO; 2211789Sahrens goto update; 2212789Sahrens } 22133912Slling 22143912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 22153912Slling /* 22163912Slling * MacOS X can extract the object type here such as: 22173912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 22183912Slling */ 22195663Sck153898 22205663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 22215663Sck153898 zap.za_normalization_conflict = 22225663Sck153898 xattr_sysattr_casechk(zap.za_name); 22235663Sck153898 } 2224789Sahrens } 22255331Samw 22269749STim.Haley@Sun.COM if (flags & V_RDDIR_ACCFILTER) { 22279749STim.Haley@Sun.COM /* 22289749STim.Haley@Sun.COM * If we have no access at all, don't include 22299749STim.Haley@Sun.COM * this entry in the returned information 22309749STim.Haley@Sun.COM */ 22319749STim.Haley@Sun.COM znode_t *ezp; 22329749STim.Haley@Sun.COM if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 22339749STim.Haley@Sun.COM goto skip_entry; 22349749STim.Haley@Sun.COM if (!zfs_has_access(ezp, cr)) { 22359749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22369749STim.Haley@Sun.COM goto skip_entry; 22379749STim.Haley@Sun.COM } 22389749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22399749STim.Haley@Sun.COM } 22409749STim.Haley@Sun.COM 22415331Samw if (flags & V_RDDIR_ENTFLAGS) 22425331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 22435331Samw else 22445331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2245789Sahrens 2246789Sahrens /* 2247789Sahrens * Will this entry fit in the buffer? 2248789Sahrens */ 22493912Slling if (outcount + reclen > bufsize) { 2250789Sahrens /* 2251789Sahrens * Did we manage to fit anything in the buffer? 2252789Sahrens */ 2253789Sahrens if (!outcount) { 2254789Sahrens error = EINVAL; 2255789Sahrens goto update; 2256789Sahrens } 2257789Sahrens break; 2258789Sahrens } 22595331Samw if (flags & V_RDDIR_ENTFLAGS) { 22605331Samw /* 22615331Samw * Add extended flag entry: 22625331Samw */ 22635331Samw eodp->ed_ino = objnum; 22645331Samw eodp->ed_reclen = reclen; 22655331Samw /* NOTE: ed_off is the offset for the *next* entry */ 22665331Samw next = &(eodp->ed_off); 22675331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 22685331Samw ED_CASE_CONFLICT : 0; 22695331Samw (void) strncpy(eodp->ed_name, zap.za_name, 22705331Samw EDIRENT_NAMELEN(reclen)); 22715331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 22725331Samw } else { 22735331Samw /* 22745331Samw * Add normal entry: 22755331Samw */ 22765331Samw odp->d_ino = objnum; 22775331Samw odp->d_reclen = reclen; 22785331Samw /* NOTE: d_off is the offset for the *next* entry */ 22795331Samw next = &(odp->d_off); 22805331Samw (void) strncpy(odp->d_name, zap.za_name, 22815331Samw DIRENT64_NAMELEN(reclen)); 22825331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 22835331Samw } 22843912Slling outcount += reclen; 2285789Sahrens 2286789Sahrens ASSERT(outcount <= bufsize); 2287789Sahrens 2288789Sahrens /* Prefetch znode */ 2289869Sperrin if (prefetch) 22903912Slling dmu_prefetch(os, objnum, 0, 0); 2291789Sahrens 22929749STim.Haley@Sun.COM skip_entry: 2293789Sahrens /* 2294789Sahrens * Move to the next entry, fill in the previous offset. 2295789Sahrens */ 2296789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2297789Sahrens zap_cursor_advance(&zc); 2298789Sahrens offset = zap_cursor_serialize(&zc); 2299789Sahrens } else { 2300789Sahrens offset += 1; 2301789Sahrens } 2302789Sahrens *next = offset; 2303789Sahrens } 2304869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2305789Sahrens 2306789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2307789Sahrens iovp->iov_base += outcount; 2308789Sahrens iovp->iov_len -= outcount; 2309789Sahrens uio->uio_resid -= outcount; 2310789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2311789Sahrens /* 2312789Sahrens * Reset the pointer. 2313789Sahrens */ 2314789Sahrens offset = uio->uio_loffset; 2315789Sahrens } 2316789Sahrens 2317789Sahrens update: 2318885Sahrens zap_cursor_fini(&zc); 2319789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2320789Sahrens kmem_free(outbuf, bufsize); 2321789Sahrens 2322789Sahrens if (error == ENOENT) 2323789Sahrens error = 0; 2324789Sahrens 2325789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2326789Sahrens 2327789Sahrens uio->uio_loffset = offset; 2328789Sahrens ZFS_EXIT(zfsvfs); 2329789Sahrens return (error); 2330789Sahrens } 2331789Sahrens 23324720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 23334720Sfr157268 2334789Sahrens static int 23355331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2336789Sahrens { 2337789Sahrens znode_t *zp = VTOZ(vp); 2338789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2339789Sahrens 23401773Seschrock /* 23411773Seschrock * Regardless of whether this is required for standards conformance, 23421773Seschrock * this is the logical behavior when fsync() is called on a file with 23431773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 23441773Seschrock * going to be pushed out as part of the zil_commit(). 23451773Seschrock */ 23461773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 23471773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 23485331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 23491773Seschrock 23504720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 23514720Sfr157268 235212294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 235312294SMark.Musante@Sun.COM ZFS_ENTER(zfsvfs); 235412294SMark.Musante@Sun.COM ZFS_VERIFY_ZP(zp); 235512699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 235612294SMark.Musante@Sun.COM ZFS_EXIT(zfsvfs); 235712294SMark.Musante@Sun.COM } 2358789Sahrens return (0); 2359789Sahrens } 2360789Sahrens 23615331Samw 2362789Sahrens /* 2363789Sahrens * Get the requested file attributes and place them in the provided 2364789Sahrens * vattr structure. 2365789Sahrens * 2366789Sahrens * IN: vp - vnode of file. 2367789Sahrens * vap - va_mask identifies requested attributes. 23685331Samw * If AT_XVATTR set, then optional attrs are requested 23695331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2370789Sahrens * cr - credentials of caller. 23715331Samw * ct - caller context 2372789Sahrens * 2373789Sahrens * OUT: vap - attribute values. 2374789Sahrens * 2375789Sahrens * RETURN: 0 (always succeeds) 2376789Sahrens */ 2377789Sahrens /* ARGSUSED */ 2378789Sahrens static int 23795331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 23805331Samw caller_context_t *ct) 2381789Sahrens { 2382789Sahrens znode_t *zp = VTOZ(vp); 2383789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23845331Samw int error = 0; 23854543Smarks uint64_t links; 238611935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 23875331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23885331Samw xoptattr_t *xoap = NULL; 23895331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 239011935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[2]; 239111935SMark.Shellenbaum@Sun.COM int count = 0; 2392789Sahrens 23935367Sahrens ZFS_ENTER(zfsvfs); 23945367Sahrens ZFS_VERIFY_ZP(zp); 239511935SMark.Shellenbaum@Sun.COM 239611935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 239711935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 239811935SMark.Shellenbaum@Sun.COM 239911935SMark.Shellenbaum@Sun.COM if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 240011935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 240111935SMark.Shellenbaum@Sun.COM return (error); 240211935SMark.Shellenbaum@Sun.COM } 2403789Sahrens 24045331Samw /* 24055331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 24065331Samw * Also, if we are the owner don't bother, since owner should 24075331Samw * always be allowed to read basic attributes of file. 24085331Samw */ 240911935SMark.Shellenbaum@Sun.COM if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) { 24105331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 24115331Samw skipaclchk, cr)) { 24125331Samw ZFS_EXIT(zfsvfs); 24135331Samw return (error); 24145331Samw } 24155331Samw } 24165331Samw 2417789Sahrens /* 2418789Sahrens * Return all attributes. It's cheaper to provide the answer 2419789Sahrens * than to determine whether we were asked the question. 2420789Sahrens */ 2421789Sahrens 24229774SRay.Hassan@Sun.COM mutex_enter(&zp->z_lock); 2423789Sahrens vap->va_type = vp->v_type; 242411935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode & MODEMASK; 242511935SMark.Shellenbaum@Sun.COM vap->va_uid = zp->z_uid; 242611935SMark.Shellenbaum@Sun.COM vap->va_gid = zp->z_gid; 2427789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2428789Sahrens vap->va_nodeid = zp->z_id; 24294543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 243011935SMark.Shellenbaum@Sun.COM links = zp->z_links + 1; 24314543Smarks else 243211935SMark.Shellenbaum@Sun.COM links = zp->z_links; 24334543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 243411935SMark.Shellenbaum@Sun.COM vap->va_size = zp->z_size; 24351816Smarks vap->va_rdev = vp->v_rdev; 2436789Sahrens vap->va_seq = zp->z_seq; 2437789Sahrens 24385331Samw /* 24395331Samw * Add in any requested optional attributes and the create time. 24405331Samw * Also set the corresponding bits in the returned attribute bitmap. 24415331Samw */ 24425331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 24435331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 24445331Samw xoap->xoa_archive = 244511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_ARCHIVE) != 0); 24465331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 24475331Samw } 24485331Samw 24495331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 24505331Samw xoap->xoa_readonly = 245111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_READONLY) != 0); 24525331Samw XVA_SET_RTN(xvap, XAT_READONLY); 24535331Samw } 24545331Samw 24555331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 24565331Samw xoap->xoa_system = 245711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_SYSTEM) != 0); 24585331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 24595331Samw } 24605331Samw 24615331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 24625331Samw xoap->xoa_hidden = 246311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_HIDDEN) != 0); 24645331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 24655331Samw } 24665331Samw 24675331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 24685331Samw xoap->xoa_nounlink = 246911935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0); 24705331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 24715331Samw } 24725331Samw 24735331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 24745331Samw xoap->xoa_immutable = 247511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 24765331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 24775331Samw } 24785331Samw 24795331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 24805331Samw xoap->xoa_appendonly = 248111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0); 24825331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 24835331Samw } 24845331Samw 24855331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 24865331Samw xoap->xoa_nodump = 248711935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0); 24885331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 24895331Samw } 24905331Samw 24915331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 24925331Samw xoap->xoa_opaque = 249311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_OPAQUE) != 0); 24945331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 24955331Samw } 24965331Samw 24975331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 24985331Samw xoap->xoa_av_quarantined = 249911935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 25005331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 25015331Samw } 25025331Samw 25035331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 25045331Samw xoap->xoa_av_modified = 250511935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 25065331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 25075331Samw } 25085331Samw 25095331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 251011935SMark.Shellenbaum@Sun.COM vp->v_type == VREG) { 251111935SMark.Shellenbaum@Sun.COM zfs_sa_get_scanstamp(zp, xvap); 25125331Samw } 25135331Samw 25145331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 251511935SMark.Shellenbaum@Sun.COM uint64_t times[2]; 251611935SMark.Shellenbaum@Sun.COM 251711935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 251811935SMark.Shellenbaum@Sun.COM times, sizeof (times)); 251911935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 25205331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 25215331Samw } 252210793Sdai.ngo@sun.com 252310793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 252411935SMark.Shellenbaum@Sun.COM xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 252510793Sdai.ngo@sun.com XVA_SET_RTN(xvap, XAT_REPARSE); 252610793Sdai.ngo@sun.com } 25275331Samw } 25285331Samw 252911935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 253011935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_mtime, mtime); 253111935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2532789Sahrens 2533789Sahrens mutex_exit(&zp->z_lock); 2534789Sahrens 253511935SMark.Shellenbaum@Sun.COM sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); 2536789Sahrens 2537789Sahrens if (zp->z_blksz == 0) { 2538789Sahrens /* 2539789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2540789Sahrens */ 2541789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2542789Sahrens } 2543789Sahrens 2544789Sahrens ZFS_EXIT(zfsvfs); 2545789Sahrens return (0); 2546789Sahrens } 2547789Sahrens 2548789Sahrens /* 2549789Sahrens * Set the file attributes to the values contained in the 2550789Sahrens * vattr structure. 2551789Sahrens * 2552789Sahrens * IN: vp - vnode of file to be modified. 2553789Sahrens * vap - new attribute values. 25545331Samw * If AT_XVATTR set, then optional attrs are being set 2555789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 25565331Samw * - ATTR_NOACLCHECK (CIFS context only). 2557789Sahrens * cr - credentials of caller. 25585331Samw * ct - caller context 2559789Sahrens * 2560789Sahrens * RETURN: 0 if success 2561789Sahrens * error code if failure 2562789Sahrens * 2563789Sahrens * Timestamps: 2564789Sahrens * vp - ctime updated, mtime updated if size changed. 2565789Sahrens */ 2566789Sahrens /* ARGSUSED */ 2567789Sahrens static int 2568789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2569789Sahrens caller_context_t *ct) 2570789Sahrens { 25715326Sek110237 znode_t *zp = VTOZ(vp); 2572789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 25735326Sek110237 zilog_t *zilog; 2574789Sahrens dmu_tx_t *tx; 25751878Smaybee vattr_t oldva; 25768190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2577789Sahrens uint_t mask = vap->va_mask; 25781878Smaybee uint_t saved_mask; 25792796Smarks int trim_mask = 0; 2580789Sahrens uint64_t new_mode; 25819179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 258211935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj = 0; 258311935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 25841231Smarks znode_t *attrzp; 2585789Sahrens int need_policy = FALSE; 258611935SMark.Shellenbaum@Sun.COM int err, err2; 25875331Samw zfs_fuid_info_t *fuidp = NULL; 25885331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 25895331Samw xoptattr_t *xoap; 25905824Smarks zfs_acl_t *aclp = NULL; 25915331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 259211935SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 259311935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[7], xattr_bulk[7]; 259411935SMark.Shellenbaum@Sun.COM int count = 0, xattr_count = 0; 2595789Sahrens 2596789Sahrens if (mask == 0) 2597789Sahrens return (0); 2598789Sahrens 2599789Sahrens if (mask & AT_NOSET) 2600789Sahrens return (EINVAL); 2601789Sahrens 26025367Sahrens ZFS_ENTER(zfsvfs); 26035367Sahrens ZFS_VERIFY_ZP(zp); 26045331Samw 26055331Samw zilog = zfsvfs->z_log; 26065331Samw 26075331Samw /* 26085331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 26095331Samw * that file system is at proper version level 26105331Samw */ 26115331Samw 26125331Samw if (zfsvfs->z_use_fuids == B_FALSE && 26135331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26145331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 26155386Stimh (mask & AT_XVATTR))) { 26165386Stimh ZFS_EXIT(zfsvfs); 26175331Samw return (EINVAL); 26185386Stimh } 26195386Stimh 26205386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 26215386Stimh ZFS_EXIT(zfsvfs); 2622789Sahrens return (EISDIR); 26235386Stimh } 26245386Stimh 26255386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 26265386Stimh ZFS_EXIT(zfsvfs); 26271308Smarks return (EINVAL); 26285386Stimh } 26291308Smarks 26305331Samw /* 26315331Samw * If this is an xvattr_t, then get a pointer to the structure of 26325331Samw * optional attributes. If this is NULL, then we have a vattr_t. 26335331Samw */ 26345331Samw xoap = xva_getxoptattr(xvap); 26355331Samw 26368190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 26378190SMark.Shellenbaum@Sun.COM 26385331Samw /* 26395331Samw * Immutable files can only alter immutable bit and atime 26405331Samw */ 264111935SMark.Shellenbaum@Sun.COM if ((zp->z_pflags & ZFS_IMMUTABLE) && 26425331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 26435386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 26445386Stimh ZFS_EXIT(zfsvfs); 26455331Samw return (EPERM); 26465386Stimh } 26475386Stimh 264811935SMark.Shellenbaum@Sun.COM if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 26495386Stimh ZFS_EXIT(zfsvfs); 26505331Samw return (EPERM); 26515386Stimh } 2652789Sahrens 26536064Smarks /* 26546064Smarks * Verify timestamps doesn't overflow 32 bits. 26556064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 26566064Smarks * handle times greater than 2039. This check should be removed 26576064Smarks * once large timestamps are fully supported. 26586064Smarks */ 26596064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 26606064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 26616064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 26626064Smarks ZFS_EXIT(zfsvfs); 26636064Smarks return (EOVERFLOW); 26646064Smarks } 26656064Smarks } 26666064Smarks 2667789Sahrens top: 26681231Smarks attrzp = NULL; 2669789Sahrens 26709981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2671789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2672789Sahrens ZFS_EXIT(zfsvfs); 2673789Sahrens return (EROFS); 2674789Sahrens } 2675789Sahrens 2676789Sahrens /* 2677789Sahrens * First validate permissions 2678789Sahrens */ 2679789Sahrens 2680789Sahrens if (mask & AT_SIZE) { 26815331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2682789Sahrens if (err) { 2683789Sahrens ZFS_EXIT(zfsvfs); 2684789Sahrens return (err); 2685789Sahrens } 26861878Smaybee /* 26871878Smaybee * XXX - Note, we are not providing any open 26881878Smaybee * mode flags here (like FNDELAY), so we may 26891878Smaybee * block if there are locks present... this 26901878Smaybee * should be addressed in openat(). 26911878Smaybee */ 26926992Smaybee /* XXX - would it be OK to generate a log record here? */ 26936992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 26941878Smaybee if (err) { 26951878Smaybee ZFS_EXIT(zfsvfs); 26961878Smaybee return (err); 26971878Smaybee } 2698789Sahrens } 2699789Sahrens 27005331Samw if (mask & (AT_ATIME|AT_MTIME) || 27015331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 27025331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 27035331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 27045331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 270511935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 27065331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 27075331Samw skipaclchk, cr); 270811935SMark.Shellenbaum@Sun.COM } 2709789Sahrens 2710789Sahrens if (mask & (AT_UID|AT_GID)) { 2711789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2712789Sahrens int take_owner; 2713789Sahrens int take_group; 2714789Sahrens 2715789Sahrens /* 2716913Smarks * NOTE: even if a new mode is being set, 2717913Smarks * we may clear S_ISUID/S_ISGID bits. 2718913Smarks */ 2719913Smarks 2720913Smarks if (!(mask & AT_MODE)) 272111935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode; 2722913Smarks 2723913Smarks /* 2724789Sahrens * Take ownership or chgrp to group we are a member of 2725789Sahrens */ 2726789Sahrens 2727789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 27285331Samw take_group = (mask & AT_GID) && 27295331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2730789Sahrens 2731789Sahrens /* 2732789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2733789Sahrens * take_group must both be set in order to allow taking 2734789Sahrens * ownership. 2735789Sahrens * 2736789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2737789Sahrens * 2738789Sahrens */ 2739789Sahrens 2740789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2741789Sahrens ((idmask == AT_UID) && take_owner) || 2742789Sahrens ((idmask == AT_GID) && take_group)) { 27435331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 27445331Samw skipaclchk, cr) == 0) { 2745789Sahrens /* 2746789Sahrens * Remove setuid/setgid for non-privileged users 2747789Sahrens */ 27481115Smarks secpolicy_setid_clear(vap, cr); 27492796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2750789Sahrens } else { 2751789Sahrens need_policy = TRUE; 2752789Sahrens } 2753789Sahrens } else { 2754789Sahrens need_policy = TRUE; 2755789Sahrens } 2756789Sahrens } 2757789Sahrens 27582796Smarks mutex_enter(&zp->z_lock); 275911935SMark.Shellenbaum@Sun.COM oldva.va_mode = zp->z_mode; 276011935SMark.Shellenbaum@Sun.COM oldva.va_uid = zp->z_uid; 276111935SMark.Shellenbaum@Sun.COM oldva.va_gid = zp->z_gid; 27625331Samw if (mask & AT_XVATTR) { 27638190SMark.Shellenbaum@Sun.COM /* 27648190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 27658190SMark.Shellenbaum@Sun.COM * that are actually changing. 27668190SMark.Shellenbaum@Sun.COM * 27678190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 27688190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 27698190SMark.Shellenbaum@Sun.COM */ 27708190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 27718190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 277211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 27738190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27748190SMark.Shellenbaum@Sun.COM } else { 27758190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 27768190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 27778190SMark.Shellenbaum@Sun.COM } 27788190SMark.Shellenbaum@Sun.COM } 27798190SMark.Shellenbaum@Sun.COM 27808190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 27818190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 278211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 27838190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27848190SMark.Shellenbaum@Sun.COM } else { 27858190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 27868190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 27878190SMark.Shellenbaum@Sun.COM } 27888190SMark.Shellenbaum@Sun.COM } 27898190SMark.Shellenbaum@Sun.COM 27908190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 27918190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 279211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 27938190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27948190SMark.Shellenbaum@Sun.COM } else { 27958190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 27968190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 27978190SMark.Shellenbaum@Sun.COM } 27988190SMark.Shellenbaum@Sun.COM } 27998190SMark.Shellenbaum@Sun.COM 28008190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 28018190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 280211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0)) { 28038190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28048190SMark.Shellenbaum@Sun.COM } else { 28058190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 28068190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 28078190SMark.Shellenbaum@Sun.COM } 28088190SMark.Shellenbaum@Sun.COM } 28098190SMark.Shellenbaum@Sun.COM 28108190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 28118190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 281211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 28138190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28148190SMark.Shellenbaum@Sun.COM } else { 28158190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 28168190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 28178190SMark.Shellenbaum@Sun.COM } 28188190SMark.Shellenbaum@Sun.COM } 28198190SMark.Shellenbaum@Sun.COM 28208190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 28218190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 28228190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 28238190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 282411935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 28258190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28268190SMark.Shellenbaum@Sun.COM } else { 28278190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 28288190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 28298190SMark.Shellenbaum@Sun.COM } 28308190SMark.Shellenbaum@Sun.COM } 28318190SMark.Shellenbaum@Sun.COM 283210793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 283310793Sdai.ngo@sun.com mutex_exit(&zp->z_lock); 283410793Sdai.ngo@sun.com ZFS_EXIT(zfsvfs); 283510793Sdai.ngo@sun.com return (EPERM); 283610793Sdai.ngo@sun.com } 283710793Sdai.ngo@sun.com 28388190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 28398190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 28408190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 28415331Samw need_policy = TRUE; 28425331Samw } 28435331Samw } 28445331Samw 28452796Smarks mutex_exit(&zp->z_lock); 28462796Smarks 28472796Smarks if (mask & AT_MODE) { 28485331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 28492796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 28502796Smarks &oldva, cr); 28512796Smarks if (err) { 28522796Smarks ZFS_EXIT(zfsvfs); 28532796Smarks return (err); 28542796Smarks } 28552796Smarks trim_mask |= AT_MODE; 28562796Smarks } else { 28572796Smarks need_policy = TRUE; 28582796Smarks } 28592796Smarks } 2860789Sahrens 2861789Sahrens if (need_policy) { 28621115Smarks /* 28631115Smarks * If trim_mask is set then take ownership 28642796Smarks * has been granted or write_acl is present and user 28652796Smarks * has the ability to modify mode. In that case remove 28662796Smarks * UID|GID and or MODE from mask so that 28671115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 28681115Smarks */ 28692796Smarks 28702796Smarks if (trim_mask) { 28712796Smarks saved_mask = vap->va_mask; 28722796Smarks vap->va_mask &= ~trim_mask; 28732796Smarks } 2874789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 28755331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2876789Sahrens if (err) { 2877789Sahrens ZFS_EXIT(zfsvfs); 2878789Sahrens return (err); 2879789Sahrens } 28801115Smarks 28811115Smarks if (trim_mask) 28822796Smarks vap->va_mask |= saved_mask; 2883789Sahrens } 2884789Sahrens 2885789Sahrens /* 2886789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2887789Sahrens * changed va_mask 2888789Sahrens */ 2889789Sahrens mask = vap->va_mask; 2890789Sahrens 289111935SMark.Shellenbaum@Sun.COM if ((mask & (AT_UID | AT_GID))) { 289211935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj, 289311935SMark.Shellenbaum@Sun.COM sizeof (xattr_obj)); 289411935SMark.Shellenbaum@Sun.COM 289511935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 289611935SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 28979396SMatthew.Ahrens@Sun.COM if (err) 289811935SMark.Shellenbaum@Sun.COM goto out2; 28999179SMark.Shellenbaum@Sun.COM } 29009179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 29019179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 29029179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 290311935SMark.Shellenbaum@Sun.COM if (vap->va_uid != zp->z_uid && 290411935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 29059396SMatthew.Ahrens@Sun.COM err = EDQUOT; 290611935SMark.Shellenbaum@Sun.COM goto out2; 29079396SMatthew.Ahrens@Sun.COM } 29081231Smarks } 29099396SMatthew.Ahrens@Sun.COM 29109179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 29119179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 29129179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 291311935SMark.Shellenbaum@Sun.COM if (new_gid != zp->z_gid && 291411935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 29159396SMatthew.Ahrens@Sun.COM err = EDQUOT; 291611935SMark.Shellenbaum@Sun.COM goto out2; 29179179SMark.Shellenbaum@Sun.COM } 29189179SMark.Shellenbaum@Sun.COM } 29191231Smarks } 292011935SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 292111935SMark.Shellenbaum@Sun.COM 292211935SMark.Shellenbaum@Sun.COM if (mask & AT_MODE) { 292311935SMark.Shellenbaum@Sun.COM uint64_t pmode = zp->z_mode; 292412620SMark.Shellenbaum@Oracle.COM uint64_t acl_obj; 292511935SMark.Shellenbaum@Sun.COM new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 292611935SMark.Shellenbaum@Sun.COM 292711935SMark.Shellenbaum@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 292811935SMark.Shellenbaum@Sun.COM goto out; 292911935SMark.Shellenbaum@Sun.COM 293012620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 293112620SMark.Shellenbaum@Oracle.COM if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 293211935SMark.Shellenbaum@Sun.COM /* 293311935SMark.Shellenbaum@Sun.COM * Are we upgrading ACL from old V0 format 293411935SMark.Shellenbaum@Sun.COM * to V1 format? 293511935SMark.Shellenbaum@Sun.COM */ 293611935SMark.Shellenbaum@Sun.COM if (zfsvfs->z_version <= ZPL_VERSION_FUID && 293712620SMark.Shellenbaum@Oracle.COM zfs_znode_acl_version(zp) == 293811935SMark.Shellenbaum@Sun.COM ZFS_ACL_VERSION_INITIAL) { 293912620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_free(tx, acl_obj, 0, 294011935SMark.Shellenbaum@Sun.COM DMU_OBJECT_END); 294111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 294211935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 294311935SMark.Shellenbaum@Sun.COM } else { 294412620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_write(tx, acl_obj, 0, 294511935SMark.Shellenbaum@Sun.COM aclp->z_acl_bytes); 294611935SMark.Shellenbaum@Sun.COM } 294711935SMark.Shellenbaum@Sun.COM } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 294811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 294911935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 295011935SMark.Shellenbaum@Sun.COM } 295112620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 295211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 295311935SMark.Shellenbaum@Sun.COM } else { 295411935SMark.Shellenbaum@Sun.COM if ((mask & AT_XVATTR) && 295511935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 295611935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 295711935SMark.Shellenbaum@Sun.COM else 295811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 295911935SMark.Shellenbaum@Sun.COM } 296011935SMark.Shellenbaum@Sun.COM 296111935SMark.Shellenbaum@Sun.COM if (attrzp) { 296211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 296311935SMark.Shellenbaum@Sun.COM } 296411935SMark.Shellenbaum@Sun.COM 296511935SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 296611935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 296711935SMark.Shellenbaum@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 296811935SMark.Shellenbaum@Sun.COM 296911935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 29701231Smarks 29718227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2972789Sahrens if (err) { 29739396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 29742113Sahrens dmu_tx_wait(tx); 29759396SMatthew.Ahrens@Sun.COM goto out; 2976789Sahrens } 2977789Sahrens 297811935SMark.Shellenbaum@Sun.COM count = 0; 2979789Sahrens /* 2980789Sahrens * Set each attribute requested. 2981789Sahrens * We group settings according to the locks they need to acquire. 2982789Sahrens * 2983789Sahrens * Note: you cannot set ctime directly, although it will be 2984789Sahrens * updated as a side-effect of calling this function. 2985789Sahrens */ 2986789Sahrens 298712620SMark.Shellenbaum@Oracle.COM 298812620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 298912620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_acl_lock); 2990789Sahrens mutex_enter(&zp->z_lock); 2991789Sahrens 299212394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 299312394SMark.Shellenbaum@Sun.COM &zp->z_pflags, sizeof (zp->z_pflags)); 299412394SMark.Shellenbaum@Sun.COM 299512394SMark.Shellenbaum@Sun.COM if (attrzp) { 299612620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 299712620SMark.Shellenbaum@Oracle.COM mutex_enter(&attrzp->z_acl_lock); 299811935SMark.Shellenbaum@Sun.COM mutex_enter(&attrzp->z_lock); 299912394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 300012394SMark.Shellenbaum@Sun.COM SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 300112394SMark.Shellenbaum@Sun.COM sizeof (attrzp->z_pflags)); 300212394SMark.Shellenbaum@Sun.COM } 300311935SMark.Shellenbaum@Sun.COM 300412164SMark.Shellenbaum@Sun.COM if (mask & (AT_UID|AT_GID)) { 300512164SMark.Shellenbaum@Sun.COM 300612164SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 300712164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 300812164SMark.Shellenbaum@Sun.COM &new_uid, sizeof (new_uid)); 300912164SMark.Shellenbaum@Sun.COM zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid, 301012164SMark.Shellenbaum@Sun.COM cr, ZFS_OWNER); 301112164SMark.Shellenbaum@Sun.COM if (attrzp) { 301212164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 301312164SMark.Shellenbaum@Sun.COM SA_ZPL_UID(zfsvfs), NULL, &new_uid, 301412164SMark.Shellenbaum@Sun.COM sizeof (new_uid)); 301512394SMark.Shellenbaum@Sun.COM attrzp->z_uid = zp->z_uid; 301612164SMark.Shellenbaum@Sun.COM } 301711935SMark.Shellenbaum@Sun.COM } 301812164SMark.Shellenbaum@Sun.COM 301912164SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 302012164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 302112164SMark.Shellenbaum@Sun.COM NULL, &new_gid, sizeof (new_gid)); 302212164SMark.Shellenbaum@Sun.COM zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr, 302312164SMark.Shellenbaum@Sun.COM ZFS_GROUP); 302412164SMark.Shellenbaum@Sun.COM if (attrzp) { 302512164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 302612164SMark.Shellenbaum@Sun.COM SA_ZPL_GID(zfsvfs), NULL, &new_gid, 302712164SMark.Shellenbaum@Sun.COM sizeof (new_gid)); 302812164SMark.Shellenbaum@Sun.COM attrzp->z_gid = zp->z_gid; 302912164SMark.Shellenbaum@Sun.COM } 303012164SMark.Shellenbaum@Sun.COM } 303112164SMark.Shellenbaum@Sun.COM if (!(mask & AT_MODE)) { 303212164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 303312164SMark.Shellenbaum@Sun.COM NULL, &new_mode, sizeof (new_mode)); 303412164SMark.Shellenbaum@Sun.COM new_mode = zp->z_mode; 303512164SMark.Shellenbaum@Sun.COM } 303612164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(zp); 303712164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 303811935SMark.Shellenbaum@Sun.COM if (attrzp) { 303912164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(attrzp); 304012164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 304111935SMark.Shellenbaum@Sun.COM } 304211935SMark.Shellenbaum@Sun.COM } 304311935SMark.Shellenbaum@Sun.COM 3044789Sahrens if (mask & AT_MODE) { 304511935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 304611935SMark.Shellenbaum@Sun.COM &new_mode, sizeof (new_mode)); 304711935SMark.Shellenbaum@Sun.COM zp->z_mode = new_mode; 304812164SMark.Shellenbaum@Sun.COM ASSERT3U((uintptr_t)aclp, !=, NULL); 30499179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 3050789Sahrens ASSERT3U(err, ==, 0); 305110143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 305210143STim.Haley@Sun.COM aclp = NULL; 3053789Sahrens } 3054789Sahrens 305511935SMark.Shellenbaum@Sun.COM 305611935SMark.Shellenbaum@Sun.COM if (mask & AT_ATIME) { 305711935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 305811935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 305911935SMark.Shellenbaum@Sun.COM &zp->z_atime, sizeof (zp->z_atime)); 30601231Smarks } 30611231Smarks 306211935SMark.Shellenbaum@Sun.COM if (mask & AT_MTIME) { 306311935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 306411935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 306511935SMark.Shellenbaum@Sun.COM mtime, sizeof (mtime)); 30661231Smarks } 30671231Smarks 30686992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 306911935SMark.Shellenbaum@Sun.COM if (mask & AT_SIZE && !(mask & AT_MTIME)) { 307012394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 307112394SMark.Shellenbaum@Sun.COM NULL, mtime, sizeof (mtime)); 307211935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 307311935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 307411935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 307511935SMark.Shellenbaum@Sun.COM B_TRUE); 307611935SMark.Shellenbaum@Sun.COM } else if (mask != 0) { 307711935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 307811935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 307911935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 308011935SMark.Shellenbaum@Sun.COM B_TRUE); 308111935SMark.Shellenbaum@Sun.COM if (attrzp) { 308211935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 308311935SMark.Shellenbaum@Sun.COM SA_ZPL_CTIME(zfsvfs), NULL, 308411935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 308511935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 308611935SMark.Shellenbaum@Sun.COM mtime, ctime, B_TRUE); 308711935SMark.Shellenbaum@Sun.COM } 308811935SMark.Shellenbaum@Sun.COM } 30895331Samw /* 30905331Samw * Do this after setting timestamps to prevent timestamp 30915331Samw * update from toggling bit 30925331Samw */ 30935331Samw 30945331Samw if (xoap && (mask & AT_XVATTR)) { 30958190SMark.Shellenbaum@Sun.COM 30968190SMark.Shellenbaum@Sun.COM /* 30978190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 30988190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 30998190SMark.Shellenbaum@Sun.COM */ 31008190SMark.Shellenbaum@Sun.COM 31018190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 31028190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 31038190SMark.Shellenbaum@Sun.COM } 31048190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 31058190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 31068190SMark.Shellenbaum@Sun.COM } 31078190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 31088190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 31098190SMark.Shellenbaum@Sun.COM } 31108190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 31118190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 31128190SMark.Shellenbaum@Sun.COM } 31138190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 31148190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 31158190SMark.Shellenbaum@Sun.COM } 31168190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 31178190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 31188190SMark.Shellenbaum@Sun.COM } 31198190SMark.Shellenbaum@Sun.COM 312011935SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 31215331Samw ASSERT(vp->v_type == VREG); 31225331Samw 312311935SMark.Shellenbaum@Sun.COM zfs_xvattr_set(zp, xvap, tx); 31245331Samw } 3125789Sahrens 31269179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 31279179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 31289179SMark.Shellenbaum@Sun.COM 31291878Smaybee if (mask != 0) 31305331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 31315331Samw 3132789Sahrens mutex_exit(&zp->z_lock); 313312620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 313412620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_acl_lock); 313512620SMark.Shellenbaum@Oracle.COM 313612620SMark.Shellenbaum@Oracle.COM if (attrzp) { 313712620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 313812620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_acl_lock); 313912620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_lock); 314012620SMark.Shellenbaum@Oracle.COM } 31419396SMatthew.Ahrens@Sun.COM out: 314211935SMark.Shellenbaum@Sun.COM if (err == 0 && attrzp) { 314311935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 314411935SMark.Shellenbaum@Sun.COM xattr_count, tx); 314511935SMark.Shellenbaum@Sun.COM ASSERT(err2 == 0); 314611935SMark.Shellenbaum@Sun.COM } 314711935SMark.Shellenbaum@Sun.COM 31481231Smarks if (attrzp) 31491231Smarks VN_RELE(ZTOV(attrzp)); 315010143STim.Haley@Sun.COM if (aclp) 315110143STim.Haley@Sun.COM zfs_acl_free(aclp); 315210143STim.Haley@Sun.COM 31539396SMatthew.Ahrens@Sun.COM if (fuidp) { 31549396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 31559396SMatthew.Ahrens@Sun.COM fuidp = NULL; 31569396SMatthew.Ahrens@Sun.COM } 31579396SMatthew.Ahrens@Sun.COM 315811935SMark.Shellenbaum@Sun.COM if (err) { 31599396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 316011935SMark.Shellenbaum@Sun.COM if (err == ERESTART) 316111935SMark.Shellenbaum@Sun.COM goto top; 316211935SMark.Shellenbaum@Sun.COM } else { 316311935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 31649396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 316511935SMark.Shellenbaum@Sun.COM } 316611935SMark.Shellenbaum@Sun.COM 316711935SMark.Shellenbaum@Sun.COM 316811935SMark.Shellenbaum@Sun.COM out2: 316912294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 317012699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 317112294SMark.Musante@Sun.COM 3172789Sahrens ZFS_EXIT(zfsvfs); 3173789Sahrens return (err); 3174789Sahrens } 3175789Sahrens 31763271Smaybee typedef struct zfs_zlock { 31773271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 31783271Smaybee znode_t *zl_znode; /* znode we held */ 31793271Smaybee struct zfs_zlock *zl_next; /* next in list */ 31803271Smaybee } zfs_zlock_t; 31813271Smaybee 31823271Smaybee /* 31833271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 31843271Smaybee */ 31853271Smaybee static void 31863271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 31873271Smaybee { 31883271Smaybee zfs_zlock_t *zl; 31893271Smaybee 31903271Smaybee while ((zl = *zlpp) != NULL) { 31913271Smaybee if (zl->zl_znode != NULL) 31923271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 31933271Smaybee rw_exit(zl->zl_rwlock); 31943271Smaybee *zlpp = zl->zl_next; 31953271Smaybee kmem_free(zl, sizeof (*zl)); 31963271Smaybee } 31973271Smaybee } 31983271Smaybee 3199789Sahrens /* 3200789Sahrens * Search back through the directory tree, using the ".." entries. 3201789Sahrens * Lock each directory in the chain to prevent concurrent renames. 3202789Sahrens * Fail any attempt to move a directory into one of its own descendants. 3203789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 3204789Sahrens */ 3205789Sahrens static int 3206789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3207789Sahrens { 3208789Sahrens zfs_zlock_t *zl; 32093638Sbillm znode_t *zp = tdzp; 3210789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 321111935SMark.Shellenbaum@Sun.COM uint64_t oidp = zp->z_id; 3212789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 3213789Sahrens krw_t rw = RW_WRITER; 3214789Sahrens 3215789Sahrens /* 3216789Sahrens * First pass write-locks szp and compares to zp->z_id. 3217789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 3218789Sahrens */ 3219789Sahrens do { 32203271Smaybee if (!rw_tryenter(rwlp, rw)) { 32213271Smaybee /* 32223271Smaybee * Another thread is renaming in this path. 32233271Smaybee * Note that if we are a WRITER, we don't have any 32243271Smaybee * parent_locks held yet. 32253271Smaybee */ 32263271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 32273271Smaybee /* 32283271Smaybee * Drop our locks and restart 32293271Smaybee */ 32303271Smaybee zfs_rename_unlock(&zl); 32313271Smaybee *zlpp = NULL; 32323271Smaybee zp = tdzp; 323311935SMark.Shellenbaum@Sun.COM oidp = zp->z_id; 32343271Smaybee rwlp = &szp->z_parent_lock; 32353271Smaybee rw = RW_WRITER; 32363271Smaybee continue; 32373271Smaybee } else { 32383271Smaybee /* 32393271Smaybee * Wait for other thread to drop its locks 32403271Smaybee */ 32413271Smaybee rw_enter(rwlp, rw); 32423271Smaybee } 32433271Smaybee } 32443271Smaybee 3245789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3246789Sahrens zl->zl_rwlock = rwlp; 3247789Sahrens zl->zl_znode = NULL; 3248789Sahrens zl->zl_next = *zlpp; 3249789Sahrens *zlpp = zl; 3250789Sahrens 325111935SMark.Shellenbaum@Sun.COM if (oidp == szp->z_id) /* We're a descendant of szp */ 3252789Sahrens return (EINVAL); 3253789Sahrens 325411935SMark.Shellenbaum@Sun.COM if (oidp == rootid) /* We've hit the top */ 3255789Sahrens return (0); 3256789Sahrens 3257789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 325811935SMark.Shellenbaum@Sun.COM int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3259789Sahrens if (error) 3260789Sahrens return (error); 3261789Sahrens zl->zl_znode = zp; 3262789Sahrens } 326311935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 326411935SMark.Shellenbaum@Sun.COM &oidp, sizeof (oidp)); 3265789Sahrens rwlp = &zp->z_parent_lock; 3266789Sahrens rw = RW_READER; 3267789Sahrens 3268789Sahrens } while (zp->z_id != sdzp->z_id); 3269789Sahrens 3270789Sahrens return (0); 3271789Sahrens } 3272789Sahrens 3273789Sahrens /* 3274789Sahrens * Move an entry from the provided source directory to the target 3275789Sahrens * directory. Change the entry name as indicated. 3276789Sahrens * 3277789Sahrens * IN: sdvp - Source directory containing the "old entry". 3278789Sahrens * snm - Old entry name. 3279789Sahrens * tdvp - Target directory to contain the "new entry". 3280789Sahrens * tnm - New entry name. 3281789Sahrens * cr - credentials of caller. 32825331Samw * ct - caller context 32835331Samw * flags - case flags 3284789Sahrens * 3285789Sahrens * RETURN: 0 if success 3286789Sahrens * error code if failure 3287789Sahrens * 3288789Sahrens * Timestamps: 3289789Sahrens * sdvp,tdvp - ctime|mtime updated 3290789Sahrens */ 32915331Samw /*ARGSUSED*/ 3292789Sahrens static int 32935331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 32945331Samw caller_context_t *ct, int flags) 3295789Sahrens { 3296789Sahrens znode_t *tdzp, *szp, *tzp; 3297789Sahrens znode_t *sdzp = VTOZ(sdvp); 3298789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 32995326Sek110237 zilog_t *zilog; 3300789Sahrens vnode_t *realvp; 3301789Sahrens zfs_dirlock_t *sdl, *tdl; 3302789Sahrens dmu_tx_t *tx; 3303789Sahrens zfs_zlock_t *zl; 33045331Samw int cmp, serr, terr; 33055331Samw int error = 0; 33065331Samw int zflg = 0; 3307789Sahrens 33085367Sahrens ZFS_ENTER(zfsvfs); 33095367Sahrens ZFS_VERIFY_ZP(sdzp); 33105326Sek110237 zilog = zfsvfs->z_log; 3311789Sahrens 3312789Sahrens /* 3313789Sahrens * Make sure we have the real vp for the target directory. 3314789Sahrens */ 33155331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3316789Sahrens tdvp = realvp; 3317789Sahrens 331812079SMark.Shellenbaum@Sun.COM if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) { 3319789Sahrens ZFS_EXIT(zfsvfs); 3320789Sahrens return (EXDEV); 3321789Sahrens } 3322789Sahrens 3323789Sahrens tdzp = VTOZ(tdvp); 33245367Sahrens ZFS_VERIFY_ZP(tdzp); 33255498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 33265331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 33275331Samw ZFS_EXIT(zfsvfs); 33285331Samw return (EILSEQ); 33295331Samw } 33305331Samw 33315331Samw if (flags & FIGNORECASE) 33325331Samw zflg |= ZCILOOK; 33335331Samw 3334789Sahrens top: 3335789Sahrens szp = NULL; 3336789Sahrens tzp = NULL; 3337789Sahrens zl = NULL; 3338789Sahrens 3339789Sahrens /* 3340789Sahrens * This is to prevent the creation of links into attribute space 3341789Sahrens * by renaming a linked file into/outof an attribute directory. 3342789Sahrens * See the comment in zfs_link() for why this is considered bad. 3343789Sahrens */ 334411935SMark.Shellenbaum@Sun.COM if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3345789Sahrens ZFS_EXIT(zfsvfs); 3346789Sahrens return (EINVAL); 3347789Sahrens } 3348789Sahrens 3349789Sahrens /* 3350789Sahrens * Lock source and target directory entries. To prevent deadlock, 3351789Sahrens * a lock ordering must be defined. We lock the directory with 3352789Sahrens * the smallest object id first, or if it's a tie, the one with 3353789Sahrens * the lexically first name. 3354789Sahrens */ 3355789Sahrens if (sdzp->z_id < tdzp->z_id) { 3356789Sahrens cmp = -1; 3357789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3358789Sahrens cmp = 1; 3359789Sahrens } else { 33605331Samw /* 33615331Samw * First compare the two name arguments without 33625331Samw * considering any case folding. 33635331Samw */ 33645331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 33655331Samw 33665331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 33675498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3368789Sahrens if (cmp == 0) { 3369789Sahrens /* 3370789Sahrens * POSIX: "If the old argument and the new argument 3371789Sahrens * both refer to links to the same existing file, 3372789Sahrens * the rename() function shall return successfully 3373789Sahrens * and perform no other action." 3374789Sahrens */ 3375789Sahrens ZFS_EXIT(zfsvfs); 3376789Sahrens return (0); 3377789Sahrens } 33785331Samw /* 33795331Samw * If the file system is case-folding, then we may 33805331Samw * have some more checking to do. A case-folding file 33815331Samw * system is either supporting mixed case sensitivity 33825331Samw * access or is completely case-insensitive. Note 33835331Samw * that the file system is always case preserving. 33845331Samw * 33855331Samw * In mixed sensitivity mode case sensitive behavior 33865331Samw * is the default. FIGNORECASE must be used to 33875331Samw * explicitly request case insensitive behavior. 33885331Samw * 33895331Samw * If the source and target names provided differ only 33905331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 33915331Samw * we will treat this as a special case in the 33925331Samw * case-insensitive mode: as long as the source name 33935331Samw * is an exact match, we will allow this to proceed as 33945331Samw * a name-change request. 33955331Samw */ 33965498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 33975498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 33985498Stimh flags & FIGNORECASE)) && 33995331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 34005331Samw &error) == 0) { 34015331Samw /* 34025331Samw * case preserving rename request, require exact 34035331Samw * name matches 34045331Samw */ 34055331Samw zflg |= ZCIEXACT; 34065331Samw zflg &= ~ZCILOOK; 34075331Samw } 3408789Sahrens } 34095331Samw 341011321SSanjeev.Bagewadi@Sun.COM /* 341111321SSanjeev.Bagewadi@Sun.COM * If the source and destination directories are the same, we should 341211321SSanjeev.Bagewadi@Sun.COM * grab the z_name_lock of that directory only once. 341311321SSanjeev.Bagewadi@Sun.COM */ 341411321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) { 341511321SSanjeev.Bagewadi@Sun.COM zflg |= ZHAVELOCK; 341611321SSanjeev.Bagewadi@Sun.COM rw_enter(&sdzp->z_name_lock, RW_READER); 341711321SSanjeev.Bagewadi@Sun.COM } 341811321SSanjeev.Bagewadi@Sun.COM 3419789Sahrens if (cmp < 0) { 34205331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 34215331Samw ZEXISTS | zflg, NULL, NULL); 34225331Samw terr = zfs_dirent_lock(&tdl, 34235331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3424789Sahrens } else { 34255331Samw terr = zfs_dirent_lock(&tdl, 34265331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 34275331Samw serr = zfs_dirent_lock(&sdl, 34285331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 34295331Samw NULL, NULL); 3430789Sahrens } 3431789Sahrens 3432789Sahrens if (serr) { 3433789Sahrens /* 3434789Sahrens * Source entry invalid or not there. 3435789Sahrens */ 3436789Sahrens if (!terr) { 3437789Sahrens zfs_dirent_unlock(tdl); 3438789Sahrens if (tzp) 3439789Sahrens VN_RELE(ZTOV(tzp)); 3440789Sahrens } 344111321SSanjeev.Bagewadi@Sun.COM 344211321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 344311321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 344411321SSanjeev.Bagewadi@Sun.COM 3445789Sahrens if (strcmp(snm, "..") == 0) 3446789Sahrens serr = EINVAL; 3447789Sahrens ZFS_EXIT(zfsvfs); 3448789Sahrens return (serr); 3449789Sahrens } 3450789Sahrens if (terr) { 3451789Sahrens zfs_dirent_unlock(sdl); 3452789Sahrens VN_RELE(ZTOV(szp)); 345311321SSanjeev.Bagewadi@Sun.COM 345411321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 345511321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 345611321SSanjeev.Bagewadi@Sun.COM 3457789Sahrens if (strcmp(tnm, "..") == 0) 3458789Sahrens terr = EINVAL; 3459789Sahrens ZFS_EXIT(zfsvfs); 3460789Sahrens return (terr); 3461789Sahrens } 3462789Sahrens 3463789Sahrens /* 3464789Sahrens * Must have write access at the source to remove the old entry 3465789Sahrens * and write access at the target to create the new entry. 3466789Sahrens * Note that if target and source are the same, this can be 3467789Sahrens * done in a single check. 3468789Sahrens */ 3469789Sahrens 3470789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3471789Sahrens goto out; 3472789Sahrens 3473789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3474789Sahrens /* 3475789Sahrens * Check to make sure rename is valid. 3476789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3477789Sahrens */ 3478789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3479789Sahrens goto out; 3480789Sahrens } 3481789Sahrens 3482789Sahrens /* 3483789Sahrens * Does target exist? 3484789Sahrens */ 3485789Sahrens if (tzp) { 3486789Sahrens /* 3487789Sahrens * Source and target must be the same type. 3488789Sahrens */ 3489789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3490789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3491789Sahrens error = ENOTDIR; 3492789Sahrens goto out; 3493789Sahrens } 3494789Sahrens } else { 3495789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3496789Sahrens error = EISDIR; 3497789Sahrens goto out; 3498789Sahrens } 3499789Sahrens } 3500789Sahrens /* 3501789Sahrens * POSIX dictates that when the source and target 3502789Sahrens * entries refer to the same file object, rename 3503789Sahrens * must do nothing and exit without error. 3504789Sahrens */ 3505789Sahrens if (szp->z_id == tzp->z_id) { 3506789Sahrens error = 0; 3507789Sahrens goto out; 3508789Sahrens } 3509789Sahrens } 3510789Sahrens 35115331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3512789Sahrens if (tzp) 35135331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 35144863Spraks 35154863Spraks /* 35164863Spraks * notify the target directory if it is not the same 35174863Spraks * as source directory. 35184863Spraks */ 35194863Spraks if (tdvp != sdvp) { 35205331Samw vnevent_rename_dest_dir(tdvp, ct); 35214863Spraks } 3522789Sahrens 3523789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 352411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 352511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 35261544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 35271544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 352811935SMark.Shellenbaum@Sun.COM if (sdzp != tdzp) { 352911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 353011935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tdzp); 353111935SMark.Shellenbaum@Sun.COM } 353211935SMark.Shellenbaum@Sun.COM if (tzp) { 353311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 353411935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tzp); 353511935SMark.Shellenbaum@Sun.COM } 353611935SMark.Shellenbaum@Sun.COM 353711935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 35383461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 35398227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3540789Sahrens if (error) { 3541789Sahrens if (zl != NULL) 3542789Sahrens zfs_rename_unlock(&zl); 3543789Sahrens zfs_dirent_unlock(sdl); 3544789Sahrens zfs_dirent_unlock(tdl); 354511321SSanjeev.Bagewadi@Sun.COM 354611321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 354711321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 354811321SSanjeev.Bagewadi@Sun.COM 3549789Sahrens VN_RELE(ZTOV(szp)); 3550789Sahrens if (tzp) 3551789Sahrens VN_RELE(ZTOV(tzp)); 35528227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35532113Sahrens dmu_tx_wait(tx); 35542113Sahrens dmu_tx_abort(tx); 3555789Sahrens goto top; 3556789Sahrens } 35572113Sahrens dmu_tx_abort(tx); 3558789Sahrens ZFS_EXIT(zfsvfs); 3559789Sahrens return (error); 3560789Sahrens } 3561789Sahrens 3562789Sahrens if (tzp) /* Attempt to remove the existing target */ 35635331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3564789Sahrens 3565789Sahrens if (error == 0) { 3566789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3567789Sahrens if (error == 0) { 356811935SMark.Shellenbaum@Sun.COM szp->z_pflags |= ZFS_AV_MODIFIED; 356911935SMark.Shellenbaum@Sun.COM 357011935SMark.Shellenbaum@Sun.COM error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 357111935SMark.Shellenbaum@Sun.COM (void *)&szp->z_pflags, sizeof (uint64_t), tx); 357211935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 35735331Samw 3574789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 357512413SSam.Falkner@Sun.COM if (error == 0) { 357612413SSam.Falkner@Sun.COM zfs_log_rename(zilog, tx, TX_RENAME | 3577*12771SNeil.Perrin@Sun.COM (flags & FIGNORECASE ? TX_CI : 0), sdzp, 3578*12771SNeil.Perrin@Sun.COM sdl->dl_name, tdzp, tdl->dl_name, szp); 357912413SSam.Falkner@Sun.COM 358012413SSam.Falkner@Sun.COM /* 358112413SSam.Falkner@Sun.COM * Update path information for the target vnode 358212413SSam.Falkner@Sun.COM */ 358312413SSam.Falkner@Sun.COM vn_renamepath(tdvp, ZTOV(szp), tnm, 358412413SSam.Falkner@Sun.COM strlen(tnm)); 358512413SSam.Falkner@Sun.COM } else { 358612413SSam.Falkner@Sun.COM /* 358712413SSam.Falkner@Sun.COM * At this point, we have successfully created 358812413SSam.Falkner@Sun.COM * the target name, but have failed to remove 358912413SSam.Falkner@Sun.COM * the source name. Since the create was done 359012413SSam.Falkner@Sun.COM * with the ZRENAMING flag, there are 359112413SSam.Falkner@Sun.COM * complications; for one, the link count is 359212413SSam.Falkner@Sun.COM * wrong. The easiest way to deal with this 359312413SSam.Falkner@Sun.COM * is to remove the newly created target, and 359412413SSam.Falkner@Sun.COM * return the original error. This must 359512413SSam.Falkner@Sun.COM * succeed; fortunately, it is very unlikely to 359612413SSam.Falkner@Sun.COM * fail, since we just created it. 359712413SSam.Falkner@Sun.COM */ 359812413SSam.Falkner@Sun.COM VERIFY3U(zfs_link_destroy(tdl, szp, tx, 359912413SSam.Falkner@Sun.COM ZRENAMING, NULL), ==, 0); 360012413SSam.Falkner@Sun.COM } 3601789Sahrens } 3602789Sahrens } 3603789Sahrens 3604789Sahrens dmu_tx_commit(tx); 3605789Sahrens out: 3606789Sahrens if (zl != NULL) 3607789Sahrens zfs_rename_unlock(&zl); 3608789Sahrens 3609789Sahrens zfs_dirent_unlock(sdl); 3610789Sahrens zfs_dirent_unlock(tdl); 3611789Sahrens 361211321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 361311321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 361411321SSanjeev.Bagewadi@Sun.COM 361511321SSanjeev.Bagewadi@Sun.COM 3616789Sahrens VN_RELE(ZTOV(szp)); 3617789Sahrens if (tzp) 3618789Sahrens VN_RELE(ZTOV(tzp)); 3619789Sahrens 362012294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 362112699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 362212294SMark.Musante@Sun.COM 3623789Sahrens ZFS_EXIT(zfsvfs); 3624789Sahrens return (error); 3625789Sahrens } 3626789Sahrens 3627789Sahrens /* 3628789Sahrens * Insert the indicated symbolic reference entry into the directory. 3629789Sahrens * 3630789Sahrens * IN: dvp - Directory to contain new symbolic link. 3631789Sahrens * link - Name for new symlink entry. 3632789Sahrens * vap - Attributes of new entry. 3633789Sahrens * target - Target path of new symlink. 3634789Sahrens * cr - credentials of caller. 36355331Samw * ct - caller context 36365331Samw * flags - case flags 3637789Sahrens * 3638789Sahrens * RETURN: 0 if success 3639789Sahrens * error code if failure 3640789Sahrens * 3641789Sahrens * Timestamps: 3642789Sahrens * dvp - ctime|mtime updated 3643789Sahrens */ 36445331Samw /*ARGSUSED*/ 3645789Sahrens static int 36465331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 36475331Samw caller_context_t *ct, int flags) 3648789Sahrens { 3649789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3650789Sahrens zfs_dirlock_t *dl; 3651789Sahrens dmu_tx_t *tx; 3652789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 36535326Sek110237 zilog_t *zilog; 365411935SMark.Shellenbaum@Sun.COM uint64_t len = strlen(link); 3655789Sahrens int error; 36565331Samw int zflg = ZNEW; 36579179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 36589179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 365911935SMark.Shellenbaum@Sun.COM uint64_t txtype = TX_SYMLINK; 3660789Sahrens 3661789Sahrens ASSERT(vap->va_type == VLNK); 3662789Sahrens 36635367Sahrens ZFS_ENTER(zfsvfs); 36645367Sahrens ZFS_VERIFY_ZP(dzp); 36655326Sek110237 zilog = zfsvfs->z_log; 36665331Samw 36675498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 36685331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 36695331Samw ZFS_EXIT(zfsvfs); 36705331Samw return (EILSEQ); 36715331Samw } 36725331Samw if (flags & FIGNORECASE) 36735331Samw zflg |= ZCILOOK; 3674789Sahrens 3675789Sahrens if (len > MAXPATHLEN) { 3676789Sahrens ZFS_EXIT(zfsvfs); 3677789Sahrens return (ENAMETOOLONG); 3678789Sahrens } 3679789Sahrens 368012302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, 368112302SMark.Shellenbaum@Sun.COM vap, cr, NULL, &acl_ids)) != 0) { 368212302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 368312302SMark.Shellenbaum@Sun.COM return (error); 368412302SMark.Shellenbaum@Sun.COM } 368512302SMark.Shellenbaum@Sun.COM top: 3686789Sahrens /* 3687789Sahrens * Attempt to lock directory; fail if entry already exists. 3688789Sahrens */ 36895331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 36905331Samw if (error) { 369112302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3692789Sahrens ZFS_EXIT(zfsvfs); 3693789Sahrens return (error); 3694789Sahrens } 3695789Sahrens 369612302SMark.Shellenbaum@Sun.COM if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 369712302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 369812421SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 369912302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 370012302SMark.Shellenbaum@Sun.COM return (error); 370112302SMark.Shellenbaum@Sun.COM } 370212302SMark.Shellenbaum@Sun.COM 37039396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 37049396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 37059396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 37069396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 37079396SMatthew.Ahrens@Sun.COM return (EDQUOT); 37089396SMatthew.Ahrens@Sun.COM } 3709789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 37109179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3711789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 37121544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 371311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 371411935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE + len); 371511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 371611935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 371711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 371811935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 371911935SMark.Shellenbaum@Sun.COM } 37209396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 37219396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 37228227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3723789Sahrens if (error) { 3724789Sahrens zfs_dirent_unlock(dl); 37258227SNeil.Perrin@Sun.COM if (error == ERESTART) { 37262113Sahrens dmu_tx_wait(tx); 37272113Sahrens dmu_tx_abort(tx); 3728789Sahrens goto top; 3729789Sahrens } 373012302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 37312113Sahrens dmu_tx_abort(tx); 3732789Sahrens ZFS_EXIT(zfsvfs); 3733789Sahrens return (error); 3734789Sahrens } 3735789Sahrens 3736789Sahrens /* 3737789Sahrens * Create a new object for the symlink. 373811935SMark.Shellenbaum@Sun.COM * for version 4 ZPL datsets the symlink will be an SA attribute 3739789Sahrens */ 374011935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 374111935SMark.Shellenbaum@Sun.COM 374211935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 374311935SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 374411935SMark.Shellenbaum@Sun.COM 374512620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 374611935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 374711935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 374811935SMark.Shellenbaum@Sun.COM link, len, tx); 374911935SMark.Shellenbaum@Sun.COM else 375011935SMark.Shellenbaum@Sun.COM zfs_sa_symlink(zp, link, len, tx); 375112620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 375211935SMark.Shellenbaum@Sun.COM 375311935SMark.Shellenbaum@Sun.COM zp->z_size = len; 375411935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 375511935SMark.Shellenbaum@Sun.COM &zp->z_size, sizeof (zp->z_size), tx); 3756789Sahrens /* 3757789Sahrens * Insert the new object into the directory. 3758789Sahrens */ 3759789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 376011935SMark.Shellenbaum@Sun.COM 376111935SMark.Shellenbaum@Sun.COM if (flags & FIGNORECASE) 376211935SMark.Shellenbaum@Sun.COM txtype |= TX_CI; 376311935SMark.Shellenbaum@Sun.COM zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 37649179SMark.Shellenbaum@Sun.COM 37659179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3766789Sahrens 3767789Sahrens dmu_tx_commit(tx); 3768789Sahrens 3769789Sahrens zfs_dirent_unlock(dl); 3770789Sahrens 3771789Sahrens VN_RELE(ZTOV(zp)); 3772789Sahrens 377312294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 377412699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 377512294SMark.Musante@Sun.COM 3776789Sahrens ZFS_EXIT(zfsvfs); 3777789Sahrens return (error); 3778789Sahrens } 3779789Sahrens 3780789Sahrens /* 3781789Sahrens * Return, in the buffer contained in the provided uio structure, 3782789Sahrens * the symbolic path referred to by vp. 3783789Sahrens * 3784789Sahrens * IN: vp - vnode of symbolic link. 3785789Sahrens * uoip - structure to contain the link path. 3786789Sahrens * cr - credentials of caller. 37875331Samw * ct - caller context 3788789Sahrens * 3789789Sahrens * OUT: uio - structure to contain the link path. 3790789Sahrens * 3791789Sahrens * RETURN: 0 if success 3792789Sahrens * error code if failure 3793789Sahrens * 3794789Sahrens * Timestamps: 3795789Sahrens * vp - atime updated 3796789Sahrens */ 3797789Sahrens /* ARGSUSED */ 3798789Sahrens static int 37995331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3800789Sahrens { 3801789Sahrens znode_t *zp = VTOZ(vp); 3802789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3803789Sahrens int error; 3804789Sahrens 38055367Sahrens ZFS_ENTER(zfsvfs); 38065367Sahrens ZFS_VERIFY_ZP(zp); 3807789Sahrens 380812620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 380911935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 381011935SMark.Shellenbaum@Sun.COM error = sa_lookup_uio(zp->z_sa_hdl, 381111935SMark.Shellenbaum@Sun.COM SA_ZPL_SYMLINK(zfsvfs), uio); 381211935SMark.Shellenbaum@Sun.COM else 381311935SMark.Shellenbaum@Sun.COM error = zfs_sa_readlink(zp, uio); 381412620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 3815789Sahrens 3816789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 381711935SMark.Shellenbaum@Sun.COM 3818789Sahrens ZFS_EXIT(zfsvfs); 3819789Sahrens return (error); 3820789Sahrens } 3821789Sahrens 3822789Sahrens /* 3823789Sahrens * Insert a new entry into directory tdvp referencing svp. 3824789Sahrens * 3825789Sahrens * IN: tdvp - Directory to contain new entry. 3826789Sahrens * svp - vnode of new entry. 3827789Sahrens * name - name of new entry. 3828789Sahrens * cr - credentials of caller. 38295331Samw * ct - caller context 3830789Sahrens * 3831789Sahrens * RETURN: 0 if success 3832789Sahrens * error code if failure 3833789Sahrens * 3834789Sahrens * Timestamps: 3835789Sahrens * tdvp - ctime|mtime updated 3836789Sahrens * svp - ctime updated 3837789Sahrens */ 3838789Sahrens /* ARGSUSED */ 3839789Sahrens static int 38405331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 38415331Samw caller_context_t *ct, int flags) 3842789Sahrens { 3843789Sahrens znode_t *dzp = VTOZ(tdvp); 3844789Sahrens znode_t *tzp, *szp; 3845789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 38465326Sek110237 zilog_t *zilog; 3847789Sahrens zfs_dirlock_t *dl; 3848789Sahrens dmu_tx_t *tx; 3849789Sahrens vnode_t *realvp; 3850789Sahrens int error; 38515331Samw int zf = ZNEW; 385212079SMark.Shellenbaum@Sun.COM uint64_t parent; 3853789Sahrens 3854789Sahrens ASSERT(tdvp->v_type == VDIR); 3855789Sahrens 38565367Sahrens ZFS_ENTER(zfsvfs); 38575367Sahrens ZFS_VERIFY_ZP(dzp); 38585326Sek110237 zilog = zfsvfs->z_log; 3859789Sahrens 38605331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3861789Sahrens svp = realvp; 3862789Sahrens 386312079SMark.Shellenbaum@Sun.COM /* 386412079SMark.Shellenbaum@Sun.COM * POSIX dictates that we return EPERM here. 386512079SMark.Shellenbaum@Sun.COM * Better choices include ENOTSUP or EISDIR. 386612079SMark.Shellenbaum@Sun.COM */ 386712079SMark.Shellenbaum@Sun.COM if (svp->v_type == VDIR) { 386812079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 386912079SMark.Shellenbaum@Sun.COM return (EPERM); 387012079SMark.Shellenbaum@Sun.COM } 387112079SMark.Shellenbaum@Sun.COM 387212079SMark.Shellenbaum@Sun.COM if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) { 3873789Sahrens ZFS_EXIT(zfsvfs); 3874789Sahrens return (EXDEV); 3875789Sahrens } 387612079SMark.Shellenbaum@Sun.COM 38775367Sahrens szp = VTOZ(svp); 38785367Sahrens ZFS_VERIFY_ZP(szp); 3879789Sahrens 388012079SMark.Shellenbaum@Sun.COM /* Prevent links to .zfs/shares files */ 388112079SMark.Shellenbaum@Sun.COM 388212079SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 388312079SMark.Shellenbaum@Sun.COM &parent, sizeof (uint64_t))) != 0) { 388412079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 388512079SMark.Shellenbaum@Sun.COM return (error); 388612079SMark.Shellenbaum@Sun.COM } 388712079SMark.Shellenbaum@Sun.COM if (parent == zfsvfs->z_shares_dir) { 388812079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 388912079SMark.Shellenbaum@Sun.COM return (EPERM); 389012079SMark.Shellenbaum@Sun.COM } 389112079SMark.Shellenbaum@Sun.COM 38925498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 38935331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 38945331Samw ZFS_EXIT(zfsvfs); 38955331Samw return (EILSEQ); 38965331Samw } 38975331Samw if (flags & FIGNORECASE) 38985331Samw zf |= ZCILOOK; 38995331Samw 3900789Sahrens /* 3901789Sahrens * We do not support links between attributes and non-attributes 3902789Sahrens * because of the potential security risk of creating links 3903789Sahrens * into "normal" file space in order to circumvent restrictions 3904789Sahrens * imposed in attribute space. 3905789Sahrens */ 390611935SMark.Shellenbaum@Sun.COM if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 3907789Sahrens ZFS_EXIT(zfsvfs); 3908789Sahrens return (EINVAL); 3909789Sahrens } 3910789Sahrens 3911789Sahrens 391211935SMark.Shellenbaum@Sun.COM if (szp->z_uid != crgetuid(cr) && 3913789Sahrens secpolicy_basic_link(cr) != 0) { 3914789Sahrens ZFS_EXIT(zfsvfs); 3915789Sahrens return (EPERM); 3916789Sahrens } 3917789Sahrens 39185331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3919789Sahrens ZFS_EXIT(zfsvfs); 3920789Sahrens return (error); 3921789Sahrens } 3922789Sahrens 392312079SMark.Shellenbaum@Sun.COM top: 3924789Sahrens /* 3925789Sahrens * Attempt to lock directory; fail if entry already exists. 3926789Sahrens */ 39275331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 39285331Samw if (error) { 3929789Sahrens ZFS_EXIT(zfsvfs); 3930789Sahrens return (error); 3931789Sahrens } 3932789Sahrens 3933789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 393411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 39351544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 393611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 393711935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 39388227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3939789Sahrens if (error) { 3940789Sahrens zfs_dirent_unlock(dl); 39418227SNeil.Perrin@Sun.COM if (error == ERESTART) { 39422113Sahrens dmu_tx_wait(tx); 39432113Sahrens dmu_tx_abort(tx); 3944789Sahrens goto top; 3945789Sahrens } 39462113Sahrens dmu_tx_abort(tx); 3947789Sahrens ZFS_EXIT(zfsvfs); 3948789Sahrens return (error); 3949789Sahrens } 3950789Sahrens 3951789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3952789Sahrens 39535331Samw if (error == 0) { 39545331Samw uint64_t txtype = TX_LINK; 39555331Samw if (flags & FIGNORECASE) 39565331Samw txtype |= TX_CI; 39575331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 39585331Samw } 3959789Sahrens 3960789Sahrens dmu_tx_commit(tx); 3961789Sahrens 3962789Sahrens zfs_dirent_unlock(dl); 3963789Sahrens 39644863Spraks if (error == 0) { 39655331Samw vnevent_link(svp, ct); 39664863Spraks } 39674863Spraks 396812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 396912699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 397012294SMark.Musante@Sun.COM 3971789Sahrens ZFS_EXIT(zfsvfs); 3972789Sahrens return (error); 3973789Sahrens } 3974789Sahrens 3975789Sahrens /* 3976789Sahrens * zfs_null_putapage() is used when the file system has been force 3977789Sahrens * unmounted. It just drops the pages. 3978789Sahrens */ 3979789Sahrens /* ARGSUSED */ 3980789Sahrens static int 3981789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3982789Sahrens size_t *lenp, int flags, cred_t *cr) 3983789Sahrens { 3984789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3985789Sahrens return (0); 3986789Sahrens } 3987789Sahrens 39882688Smaybee /* 39892688Smaybee * Push a page out to disk, klustering if possible. 39902688Smaybee * 39912688Smaybee * IN: vp - file to push page to. 39922688Smaybee * pp - page to push. 39932688Smaybee * flags - additional flags. 39942688Smaybee * cr - credentials of caller. 39952688Smaybee * 39962688Smaybee * OUT: offp - start of range pushed. 39972688Smaybee * lenp - len of range pushed. 39982688Smaybee * 39992688Smaybee * RETURN: 0 if success 40002688Smaybee * error code if failure 40012688Smaybee * 40022688Smaybee * NOTE: callers must have locked the page to be pushed. On 40032688Smaybee * exit, the page (and all other pages in the kluster) must be 40042688Smaybee * unlocked. 40052688Smaybee */ 4006789Sahrens /* ARGSUSED */ 4007789Sahrens static int 4008789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4009789Sahrens size_t *lenp, int flags, cred_t *cr) 4010789Sahrens { 4011789Sahrens znode_t *zp = VTOZ(vp); 4012789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4013789Sahrens dmu_tx_t *tx; 40142688Smaybee u_offset_t off, koff; 40152688Smaybee size_t len, klen; 4016789Sahrens int err; 4017789Sahrens 40182688Smaybee off = pp->p_offset; 40192688Smaybee len = PAGESIZE; 40202688Smaybee /* 40212688Smaybee * If our blocksize is bigger than the page size, try to kluster 40228227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 40232688Smaybee * a read-modify-write). 40242688Smaybee */ 402511935SMark.Shellenbaum@Sun.COM if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 40268636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 40278636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 402811935SMark.Shellenbaum@Sun.COM ASSERT(koff <= zp->z_size); 402911935SMark.Shellenbaum@Sun.COM if (koff + klen > zp->z_size) 403011935SMark.Shellenbaum@Sun.COM klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 40312688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 40322688Smaybee } 40332688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 40348636SMark.Maybee@Sun.COM 40351819Smaybee /* 40361819Smaybee * Can't push pages past end-of-file. 40371819Smaybee */ 403811935SMark.Shellenbaum@Sun.COM if (off >= zp->z_size) { 40394709Smaybee /* ignore all pages */ 40402688Smaybee err = 0; 40412688Smaybee goto out; 404211935SMark.Shellenbaum@Sun.COM } else if (off + len > zp->z_size) { 404311935SMark.Shellenbaum@Sun.COM int npages = btopr(zp->z_size - off); 40442688Smaybee page_t *trunc; 40452688Smaybee 40462688Smaybee page_list_break(&pp, &trunc, npages); 40474709Smaybee /* ignore pages past end of file */ 40482688Smaybee if (trunc) 40494709Smaybee pvn_write_done(trunc, flags); 405011935SMark.Shellenbaum@Sun.COM len = zp->z_size - off; 40511819Smaybee } 40529396SMatthew.Ahrens@Sun.COM 405311935SMark.Shellenbaum@Sun.COM if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 405411935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 40559396SMatthew.Ahrens@Sun.COM err = EDQUOT; 40569396SMatthew.Ahrens@Sun.COM goto out; 40579396SMatthew.Ahrens@Sun.COM } 40588636SMark.Maybee@Sun.COM top: 4059789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 4060789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 406111935SMark.Shellenbaum@Sun.COM 406211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 406311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 40648227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 4065789Sahrens if (err != 0) { 40668227SNeil.Perrin@Sun.COM if (err == ERESTART) { 40672113Sahrens dmu_tx_wait(tx); 40682113Sahrens dmu_tx_abort(tx); 4069789Sahrens goto top; 4070789Sahrens } 40712113Sahrens dmu_tx_abort(tx); 4072789Sahrens goto out; 4073789Sahrens } 4074789Sahrens 40752688Smaybee if (zp->z_blksz <= PAGESIZE) { 40767315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 40772688Smaybee ASSERT3U(len, <=, PAGESIZE); 40782688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 40797315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 40802688Smaybee } else { 40812688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 40822688Smaybee } 40832688Smaybee 40842688Smaybee if (err == 0) { 408511935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 408612394SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[3]; 408711935SMark.Shellenbaum@Sun.COM int count = 0; 408811935SMark.Shellenbaum@Sun.COM 408911935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 409011935SMark.Shellenbaum@Sun.COM &mtime, 16); 409111935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 409211935SMark.Shellenbaum@Sun.COM &ctime, 16); 409312394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 409412394SMark.Shellenbaum@Sun.COM &zp->z_pflags, 8); 409511935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 409611935SMark.Shellenbaum@Sun.COM B_TRUE); 40978636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 40982688Smaybee } 40999951SLin.Ling@Sun.COM dmu_tx_commit(tx); 41002688Smaybee 41012688Smaybee out: 41024709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4103789Sahrens if (offp) 4104789Sahrens *offp = off; 4105789Sahrens if (lenp) 4106789Sahrens *lenp = len; 4107789Sahrens 4108789Sahrens return (err); 4109789Sahrens } 4110789Sahrens 4111789Sahrens /* 4112789Sahrens * Copy the portion of the file indicated from pages into the file. 4113789Sahrens * The pages are stored in a page list attached to the files vnode. 4114789Sahrens * 4115789Sahrens * IN: vp - vnode of file to push page data to. 4116789Sahrens * off - position in file to put data. 4117789Sahrens * len - amount of data to write. 4118789Sahrens * flags - flags to control the operation. 4119789Sahrens * cr - credentials of caller. 41205331Samw * ct - caller context. 4121789Sahrens * 4122789Sahrens * RETURN: 0 if success 4123789Sahrens * error code if failure 4124789Sahrens * 4125789Sahrens * Timestamps: 4126789Sahrens * vp - ctime|mtime updated 4127789Sahrens */ 41285331Samw /*ARGSUSED*/ 4129789Sahrens static int 41305331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 41315331Samw caller_context_t *ct) 4132789Sahrens { 4133789Sahrens znode_t *zp = VTOZ(vp); 4134789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4135789Sahrens page_t *pp; 4136789Sahrens size_t io_len; 4137789Sahrens u_offset_t io_off; 41388636SMark.Maybee@Sun.COM uint_t blksz; 41398636SMark.Maybee@Sun.COM rl_t *rl; 4140789Sahrens int error = 0; 4141789Sahrens 41425367Sahrens ZFS_ENTER(zfsvfs); 41435367Sahrens ZFS_VERIFY_ZP(zp); 4144789Sahrens 41458636SMark.Maybee@Sun.COM /* 41468636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 41478636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 41488636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 41498636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 41508636SMark.Maybee@Sun.COM * operations to get narrower locking. 41518636SMark.Maybee@Sun.COM */ 41528636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 41538636SMark.Maybee@Sun.COM if (ISP2(blksz)) 41548636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 41558636SMark.Maybee@Sun.COM else 41568636SMark.Maybee@Sun.COM io_off = 0; 41578636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 41589141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 41598636SMark.Maybee@Sun.COM else 41608636SMark.Maybee@Sun.COM io_len = 0; 41618636SMark.Maybee@Sun.COM 41628636SMark.Maybee@Sun.COM if (io_len == 0) { 4163789Sahrens /* 41648636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 4165789Sahrens */ 41668636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 41678636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 41681472Sperrin goto out; 4169789Sahrens } 41708636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 41718636SMark.Maybee@Sun.COM 417211935SMark.Shellenbaum@Sun.COM if (off > zp->z_size) { 4173789Sahrens /* past end of file */ 41748636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 4175789Sahrens ZFS_EXIT(zfsvfs); 4176789Sahrens return (0); 4177789Sahrens } 4178789Sahrens 417911935SMark.Shellenbaum@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 41808636SMark.Maybee@Sun.COM 41818636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 4182789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 41831669Sperrin pp = page_lookup(vp, io_off, 41844339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4185789Sahrens } else { 4186789Sahrens pp = page_lookup_nowait(vp, io_off, 41874339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4188789Sahrens } 4189789Sahrens 4190789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 4191789Sahrens int err; 4192789Sahrens 4193789Sahrens /* 4194789Sahrens * Found a dirty page to push 4195789Sahrens */ 41961669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 41971669Sperrin if (err) 4198789Sahrens error = err; 4199789Sahrens } else { 4200789Sahrens io_len = PAGESIZE; 4201789Sahrens } 4202789Sahrens } 42031472Sperrin out: 42048636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 420512294SMark.Musante@Sun.COM if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 420612699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 4207789Sahrens ZFS_EXIT(zfsvfs); 4208789Sahrens return (error); 4209789Sahrens } 4210789Sahrens 42115331Samw /*ARGSUSED*/ 4212789Sahrens void 42135331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4214789Sahrens { 4215789Sahrens znode_t *zp = VTOZ(vp); 4216789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4217789Sahrens int error; 4218789Sahrens 42195326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 422011935SMark.Shellenbaum@Sun.COM if (zp->z_sa_hdl == NULL) { 42215446Sahrens /* 42225642Smaybee * The fs has been unmounted, or we did a 42235642Smaybee * suspend/resume and this file no longer exists. 42245446Sahrens */ 4225789Sahrens if (vn_has_cached_data(vp)) { 4226789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 4227789Sahrens B_INVAL, cr); 4228789Sahrens } 4229789Sahrens 42301544Seschrock mutex_enter(&zp->z_lock); 423110369Schris.kirby@sun.com mutex_enter(&vp->v_lock); 423210369Schris.kirby@sun.com ASSERT(vp->v_count == 1); 423310369Schris.kirby@sun.com vp->v_count = 0; 423410369Schris.kirby@sun.com mutex_exit(&vp->v_lock); 42355446Sahrens mutex_exit(&zp->z_lock); 42365642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 42375446Sahrens zfs_znode_free(zp); 4238789Sahrens return; 4239789Sahrens } 4240789Sahrens 4241789Sahrens /* 4242789Sahrens * Attempt to push any data in the page cache. If this fails 4243789Sahrens * we will get kicked out later in zfs_zinactive(). 4244789Sahrens */ 42451298Sperrin if (vn_has_cached_data(vp)) { 42461298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 42471298Sperrin cr); 42481298Sperrin } 4249789Sahrens 42503461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4251789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4252789Sahrens 425311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 425411935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 4255789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 4256789Sahrens if (error) { 4257789Sahrens dmu_tx_abort(tx); 4258789Sahrens } else { 4259789Sahrens mutex_enter(&zp->z_lock); 426011935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 426111935SMark.Shellenbaum@Sun.COM (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4262789Sahrens zp->z_atime_dirty = 0; 4263789Sahrens mutex_exit(&zp->z_lock); 4264789Sahrens dmu_tx_commit(tx); 4265789Sahrens } 4266789Sahrens } 4267789Sahrens 4268789Sahrens zfs_zinactive(zp); 42695326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4270789Sahrens } 4271789Sahrens 4272789Sahrens /* 4273789Sahrens * Bounds-check the seek operation. 4274789Sahrens * 4275789Sahrens * IN: vp - vnode seeking within 4276789Sahrens * ooff - old file offset 4277789Sahrens * noffp - pointer to new file offset 42785331Samw * ct - caller context 4279789Sahrens * 4280789Sahrens * RETURN: 0 if success 4281789Sahrens * EINVAL if new offset invalid 4282789Sahrens */ 4283789Sahrens /* ARGSUSED */ 4284789Sahrens static int 42855331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 42865331Samw caller_context_t *ct) 4287789Sahrens { 4288789Sahrens if (vp->v_type == VDIR) 4289789Sahrens return (0); 4290789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4291789Sahrens } 4292789Sahrens 4293789Sahrens /* 4294789Sahrens * Pre-filter the generic locking function to trap attempts to place 4295789Sahrens * a mandatory lock on a memory mapped file. 4296789Sahrens */ 4297789Sahrens static int 4298789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 42995331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4300789Sahrens { 4301789Sahrens znode_t *zp = VTOZ(vp); 4302789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4303789Sahrens 43045367Sahrens ZFS_ENTER(zfsvfs); 43055367Sahrens ZFS_VERIFY_ZP(zp); 4306789Sahrens 4307789Sahrens /* 43081544Seschrock * We are following the UFS semantics with respect to mapcnt 43091544Seschrock * here: If we see that the file is mapped already, then we will 43101544Seschrock * return an error, but we don't worry about races between this 43111544Seschrock * function and zfs_map(). 4312789Sahrens */ 431311935SMark.Shellenbaum@Sun.COM if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4314789Sahrens ZFS_EXIT(zfsvfs); 4315789Sahrens return (EAGAIN); 4316789Sahrens } 4317789Sahrens ZFS_EXIT(zfsvfs); 431810896SMark.Shellenbaum@Sun.COM return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4319789Sahrens } 4320789Sahrens 4321789Sahrens /* 4322789Sahrens * If we can't find a page in the cache, we will create a new page 4323789Sahrens * and fill it with file data. For efficiency, we may try to fill 43248636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 43259265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 43269265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4327789Sahrens */ 4328789Sahrens static int 4329789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4330789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4331789Sahrens { 4332789Sahrens znode_t *zp = VTOZ(vp); 4333789Sahrens page_t *pp, *cur_pp; 4334789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4335789Sahrens u_offset_t io_off, total; 4336789Sahrens size_t io_len; 4337789Sahrens int err; 4338789Sahrens 43392688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 43408636SMark.Maybee@Sun.COM /* 43418636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 43428636SMark.Maybee@Sun.COM */ 4343789Sahrens io_off = off; 4344789Sahrens io_len = PAGESIZE; 43459265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 43469265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4347789Sahrens } else { 4348789Sahrens /* 43498636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4350789Sahrens */ 4351789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 43528636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4353789Sahrens } 4354789Sahrens if (pp == NULL) { 4355789Sahrens /* 43568636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4357789Sahrens */ 4358789Sahrens *pl = NULL; 4359789Sahrens return (0); 4360789Sahrens } 4361789Sahrens 4362789Sahrens /* 4363789Sahrens * Fill the pages in the kluster. 4364789Sahrens */ 4365789Sahrens cur_pp = pp; 4366789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 43678636SMark.Maybee@Sun.COM caddr_t va; 43688636SMark.Maybee@Sun.COM 43692688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 43707315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 43719512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 43729512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 43737315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4374789Sahrens if (err) { 4375789Sahrens /* On error, toss the entire kluster */ 4376789Sahrens pvn_read_done(pp, B_ERROR); 43777294Sperrin /* convert checksum errors into IO errors */ 43787294Sperrin if (err == ECKSUM) 43797294Sperrin err = EIO; 4380789Sahrens return (err); 4381789Sahrens } 4382789Sahrens cur_pp = cur_pp->p_next; 4383789Sahrens } 43848636SMark.Maybee@Sun.COM 4385789Sahrens /* 43868636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 43878636SMark.Maybee@Sun.COM * from the desired offset `off'. 4388789Sahrens * NOTE: the page list will always be null terminated. 4389789Sahrens */ 4390789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 43918636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4392789Sahrens 4393789Sahrens return (0); 4394789Sahrens } 4395789Sahrens 4396789Sahrens /* 4397789Sahrens * Return pointers to the pages for the file region [off, off + len] 4398789Sahrens * in the pl array. If plsz is greater than len, this function may 43998636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 44008636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 44018636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 44028636SMark.Maybee@Sun.COM * part of a klustered read. 4403789Sahrens * 4404789Sahrens * IN: vp - vnode of file to get data from. 4405789Sahrens * off - position in file to get data from. 4406789Sahrens * len - amount of data to retrieve. 4407789Sahrens * plsz - length of provided page list. 4408789Sahrens * seg - segment to obtain pages for. 4409789Sahrens * addr - virtual address of fault. 4410789Sahrens * rw - mode of created pages. 4411789Sahrens * cr - credentials of caller. 44125331Samw * ct - caller context. 4413789Sahrens * 4414789Sahrens * OUT: protp - protection mode of created pages. 4415789Sahrens * pl - list of pages created. 4416789Sahrens * 4417789Sahrens * RETURN: 0 if success 4418789Sahrens * error code if failure 4419789Sahrens * 4420789Sahrens * Timestamps: 4421789Sahrens * vp - atime updated 4422789Sahrens */ 4423789Sahrens /* ARGSUSED */ 4424789Sahrens static int 4425789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4426789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 44275331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4428789Sahrens { 4429789Sahrens znode_t *zp = VTOZ(vp); 4430789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 44318636SMark.Maybee@Sun.COM page_t **pl0 = pl; 44328636SMark.Maybee@Sun.COM int err = 0; 44338636SMark.Maybee@Sun.COM 44348636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 44358636SMark.Maybee@Sun.COM if (pl == NULL) 44368636SMark.Maybee@Sun.COM return (0); 44378636SMark.Maybee@Sun.COM else if (len > plsz) 44388636SMark.Maybee@Sun.COM len = plsz; 44398681SMark.Maybee@Sun.COM else 44408681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 44418636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4442789Sahrens 44435367Sahrens ZFS_ENTER(zfsvfs); 44445367Sahrens ZFS_VERIFY_ZP(zp); 4445789Sahrens 4446789Sahrens if (protp) 4447789Sahrens *protp = PROT_ALL; 4448789Sahrens 4449789Sahrens /* 44509265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4451789Sahrens * for pages. If we don't find a page, we will need to create 4452789Sahrens * a new page and fill it with data from the file. 4453789Sahrens */ 4454789Sahrens while (len > 0) { 44558636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 44568636SMark.Maybee@Sun.COM *(pl+1) = NULL; 44578636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 44588636SMark.Maybee@Sun.COM goto out; 44598636SMark.Maybee@Sun.COM while (*pl) { 44608636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4461789Sahrens off += PAGESIZE; 4462789Sahrens addr += PAGESIZE; 44638681SMark.Maybee@Sun.COM if (len > 0) { 44648681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 44658636SMark.Maybee@Sun.COM len -= PAGESIZE; 44668681SMark.Maybee@Sun.COM } 44678636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4468789Sahrens plsz -= PAGESIZE; 44698636SMark.Maybee@Sun.COM pl++; 4470789Sahrens } 4471789Sahrens } 4472789Sahrens 4473789Sahrens /* 4474789Sahrens * Fill out the page array with any pages already in the cache. 4475789Sahrens */ 44768636SMark.Maybee@Sun.COM while (plsz > 0 && 44778636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 44788636SMark.Maybee@Sun.COM off += PAGESIZE; 44798636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4480789Sahrens } 4481789Sahrens out: 44822752Sperrin if (err) { 44832752Sperrin /* 44842752Sperrin * Release any pages we have previously locked. 44852752Sperrin */ 44862752Sperrin while (pl > pl0) 44872752Sperrin page_unlock(*--pl); 44888636SMark.Maybee@Sun.COM } else { 44898636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 44902752Sperrin } 44912752Sperrin 4492789Sahrens *pl = NULL; 4493789Sahrens 4494789Sahrens ZFS_EXIT(zfsvfs); 4495789Sahrens return (err); 4496789Sahrens } 4497789Sahrens 44981544Seschrock /* 44991544Seschrock * Request a memory map for a section of a file. This code interacts 45001544Seschrock * with common code and the VM system as follows: 45011544Seschrock * 45021544Seschrock * common code calls mmap(), which ends up in smmap_common() 45031544Seschrock * 45041544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 45051544Seschrock * 45061544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 45071544Seschrock * 45081544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 45091544Seschrock * 45101544Seschrock * zfs_addmap() updates z_mapcnt 45111544Seschrock */ 45125331Samw /*ARGSUSED*/ 4513789Sahrens static int 4514789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 45155331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 45165331Samw caller_context_t *ct) 4517789Sahrens { 4518789Sahrens znode_t *zp = VTOZ(vp); 4519789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4520789Sahrens segvn_crargs_t vn_a; 4521789Sahrens int error; 4522789Sahrens 45235929Smarks ZFS_ENTER(zfsvfs); 45245929Smarks ZFS_VERIFY_ZP(zp); 45255929Smarks 452611935SMark.Shellenbaum@Sun.COM if ((prot & PROT_WRITE) && (zp->z_pflags & 452711935SMark.Shellenbaum@Sun.COM (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 45285929Smarks ZFS_EXIT(zfsvfs); 45295331Samw return (EPERM); 45305929Smarks } 45315929Smarks 45325929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 453311935SMark.Shellenbaum@Sun.COM (zp->z_pflags & ZFS_AV_QUARANTINED)) { 45345929Smarks ZFS_EXIT(zfsvfs); 45355929Smarks return (EACCES); 45365929Smarks } 4537789Sahrens 4538789Sahrens if (vp->v_flag & VNOMAP) { 4539789Sahrens ZFS_EXIT(zfsvfs); 4540789Sahrens return (ENOSYS); 4541789Sahrens } 4542789Sahrens 4543789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4544789Sahrens ZFS_EXIT(zfsvfs); 4545789Sahrens return (ENXIO); 4546789Sahrens } 4547789Sahrens 4548789Sahrens if (vp->v_type != VREG) { 4549789Sahrens ZFS_EXIT(zfsvfs); 4550789Sahrens return (ENODEV); 4551789Sahrens } 4552789Sahrens 4553789Sahrens /* 4554789Sahrens * If file is locked, disallow mapping. 4555789Sahrens */ 455611935SMark.Shellenbaum@Sun.COM if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 45571544Seschrock ZFS_EXIT(zfsvfs); 45581544Seschrock return (EAGAIN); 4559789Sahrens } 4560789Sahrens 4561789Sahrens as_rangelock(as); 45626036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 45636036Smec if (error != 0) { 45646036Smec as_rangeunlock(as); 45656036Smec ZFS_EXIT(zfsvfs); 45666036Smec return (error); 4567789Sahrens } 4568789Sahrens 4569789Sahrens vn_a.vp = vp; 4570789Sahrens vn_a.offset = (u_offset_t)off; 4571789Sahrens vn_a.type = flags & MAP_TYPE; 4572789Sahrens vn_a.prot = prot; 4573789Sahrens vn_a.maxprot = maxprot; 4574789Sahrens vn_a.cred = cr; 4575789Sahrens vn_a.amp = NULL; 4576789Sahrens vn_a.flags = flags & ~MAP_TYPE; 45771417Skchow vn_a.szc = 0; 45781417Skchow vn_a.lgrp_mem_policy_flags = 0; 4579789Sahrens 4580789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4581789Sahrens 4582789Sahrens as_rangeunlock(as); 4583789Sahrens ZFS_EXIT(zfsvfs); 4584789Sahrens return (error); 4585789Sahrens } 4586789Sahrens 4587789Sahrens /* ARGSUSED */ 4588789Sahrens static int 4589789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 45905331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 45915331Samw caller_context_t *ct) 4592789Sahrens { 45931544Seschrock uint64_t pages = btopr(len); 45941544Seschrock 45951544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4596789Sahrens return (0); 4597789Sahrens } 4598789Sahrens 45991773Seschrock /* 46001773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 46011773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 46021773Seschrock * detecting when the data was actually modified, we have to resort to 46031773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 46041773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 46051773Seschrock * which by far the most common case: 46061773Seschrock * 46071773Seschrock * open() 46081773Seschrock * mmap() 46091773Seschrock * <modify memory> 46101773Seschrock * munmap() 46111773Seschrock * close() 46121773Seschrock * <time lapse> 46131773Seschrock * putpage() via fsflush 46141773Seschrock * 46151773Seschrock * If we wait until fsflush to come along, we can have a modification time that 46161773Seschrock * is some arbitrary point in the future. In order to prevent this in the 46171773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 46181773Seschrock * torn down. 46191773Seschrock */ 4620789Sahrens /* ARGSUSED */ 4621789Sahrens static int 4622789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 46235331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 46245331Samw caller_context_t *ct) 4625789Sahrens { 46261544Seschrock uint64_t pages = btopr(len); 46271544Seschrock 46281544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 46291544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 46301773Seschrock 46311773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 46321773Seschrock vn_has_cached_data(vp)) 46335331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 46341773Seschrock 4635789Sahrens return (0); 4636789Sahrens } 4637789Sahrens 4638789Sahrens /* 4639789Sahrens * Free or allocate space in a file. Currently, this function only 4640789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4641789Sahrens * misnamed, as its functionality includes the ability to allocate as 4642789Sahrens * well as free space. 4643789Sahrens * 4644789Sahrens * IN: vp - vnode of file to free data in. 4645789Sahrens * cmd - action to take (only F_FREESP supported). 4646789Sahrens * bfp - section of file to free/alloc. 4647789Sahrens * flag - current file open mode flags. 4648789Sahrens * offset - current file offset. 4649789Sahrens * cr - credentials of caller [UNUSED]. 46505331Samw * ct - caller context. 4651789Sahrens * 4652789Sahrens * RETURN: 0 if success 4653789Sahrens * error code if failure 4654789Sahrens * 4655789Sahrens * Timestamps: 4656789Sahrens * vp - ctime|mtime updated 4657789Sahrens */ 4658789Sahrens /* ARGSUSED */ 4659789Sahrens static int 4660789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4661789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4662789Sahrens { 4663789Sahrens znode_t *zp = VTOZ(vp); 4664789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4665789Sahrens uint64_t off, len; 4666789Sahrens int error; 4667789Sahrens 46685367Sahrens ZFS_ENTER(zfsvfs); 46695367Sahrens ZFS_VERIFY_ZP(zp); 4670789Sahrens 4671789Sahrens if (cmd != F_FREESP) { 4672789Sahrens ZFS_EXIT(zfsvfs); 4673789Sahrens return (EINVAL); 4674789Sahrens } 4675789Sahrens 4676789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4677789Sahrens ZFS_EXIT(zfsvfs); 4678789Sahrens return (error); 4679789Sahrens } 4680789Sahrens 4681789Sahrens if (bfp->l_len < 0) { 4682789Sahrens ZFS_EXIT(zfsvfs); 4683789Sahrens return (EINVAL); 4684789Sahrens } 4685789Sahrens 4686789Sahrens off = bfp->l_start; 46871669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 46881878Smaybee 46896992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4690789Sahrens 4691789Sahrens ZFS_EXIT(zfsvfs); 4692789Sahrens return (error); 4693789Sahrens } 4694789Sahrens 46955331Samw /*ARGSUSED*/ 4696789Sahrens static int 46975331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4698789Sahrens { 4699789Sahrens znode_t *zp = VTOZ(vp); 4700789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 47015326Sek110237 uint32_t gen; 470211935SMark.Shellenbaum@Sun.COM uint64_t gen64; 4703789Sahrens uint64_t object = zp->z_id; 4704789Sahrens zfid_short_t *zfid; 470511935SMark.Shellenbaum@Sun.COM int size, i, error; 4706789Sahrens 47075367Sahrens ZFS_ENTER(zfsvfs); 47085367Sahrens ZFS_VERIFY_ZP(zp); 470911935SMark.Shellenbaum@Sun.COM 471011935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 471112218SMark.Shellenbaum@Sun.COM &gen64, sizeof (uint64_t))) != 0) { 471212218SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 471311935SMark.Shellenbaum@Sun.COM return (error); 471412218SMark.Shellenbaum@Sun.COM } 471511935SMark.Shellenbaum@Sun.COM 471611935SMark.Shellenbaum@Sun.COM gen = (uint32_t)gen64; 4717789Sahrens 4718789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4719789Sahrens if (fidp->fid_len < size) { 4720789Sahrens fidp->fid_len = size; 47211512Sek110237 ZFS_EXIT(zfsvfs); 4722789Sahrens return (ENOSPC); 4723789Sahrens } 4724789Sahrens 4725789Sahrens zfid = (zfid_short_t *)fidp; 4726789Sahrens 4727789Sahrens zfid->zf_len = size; 4728789Sahrens 4729789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4730789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4731789Sahrens 4732789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4733789Sahrens if (gen == 0) 4734789Sahrens gen = 1; 4735789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4736789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4737789Sahrens 4738789Sahrens if (size == LONG_FID_LEN) { 4739789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4740789Sahrens zfid_long_t *zlfid; 4741789Sahrens 4742789Sahrens zlfid = (zfid_long_t *)fidp; 4743789Sahrens 4744789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4745789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4746789Sahrens 4747789Sahrens /* XXX - this should be the generation number for the objset */ 4748789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4749789Sahrens zlfid->zf_setgen[i] = 0; 4750789Sahrens } 4751789Sahrens 4752789Sahrens ZFS_EXIT(zfsvfs); 4753789Sahrens return (0); 4754789Sahrens } 4755789Sahrens 4756789Sahrens static int 47575331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 47585331Samw caller_context_t *ct) 4759789Sahrens { 4760789Sahrens znode_t *zp, *xzp; 4761789Sahrens zfsvfs_t *zfsvfs; 4762789Sahrens zfs_dirlock_t *dl; 4763789Sahrens int error; 4764789Sahrens 4765789Sahrens switch (cmd) { 4766789Sahrens case _PC_LINK_MAX: 4767789Sahrens *valp = ULONG_MAX; 4768789Sahrens return (0); 4769789Sahrens 4770789Sahrens case _PC_FILESIZEBITS: 4771789Sahrens *valp = 64; 4772789Sahrens return (0); 4773789Sahrens 4774789Sahrens case _PC_XATTR_EXISTS: 4775789Sahrens zp = VTOZ(vp); 4776789Sahrens zfsvfs = zp->z_zfsvfs; 47775367Sahrens ZFS_ENTER(zfsvfs); 47785367Sahrens ZFS_VERIFY_ZP(zp); 4779789Sahrens *valp = 0; 4780789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 47815331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4782789Sahrens if (error == 0) { 4783789Sahrens zfs_dirent_unlock(dl); 4784789Sahrens if (!zfs_dirempty(xzp)) 4785789Sahrens *valp = 1; 4786789Sahrens VN_RELE(ZTOV(xzp)); 4787789Sahrens } else if (error == ENOENT) { 4788789Sahrens /* 4789789Sahrens * If there aren't extended attributes, it's the 4790789Sahrens * same as having zero of them. 4791789Sahrens */ 4792789Sahrens error = 0; 4793789Sahrens } 4794789Sahrens ZFS_EXIT(zfsvfs); 4795789Sahrens return (error); 4796789Sahrens 47975331Samw case _PC_SATTR_ENABLED: 47985331Samw case _PC_SATTR_EXISTS: 47997757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 48005331Samw (vp->v_type == VREG || vp->v_type == VDIR); 48015331Samw return (0); 48025331Samw 48039749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 48049749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 48059749STim.Haley@Sun.COM vp->v_type == VDIR; 48069749STim.Haley@Sun.COM return (0); 48079749STim.Haley@Sun.COM 4808789Sahrens case _PC_ACL_ENABLED: 4809789Sahrens *valp = _ACL_ACE_ENABLED; 4810789Sahrens return (0); 4811789Sahrens 4812789Sahrens case _PC_MIN_HOLE_SIZE: 4813789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4814789Sahrens return (0); 4815789Sahrens 481610440SRoger.Faulkner@Sun.COM case _PC_TIMESTAMP_RESOLUTION: 481710440SRoger.Faulkner@Sun.COM /* nanosecond timestamp resolution */ 481810440SRoger.Faulkner@Sun.COM *valp = 1L; 481910440SRoger.Faulkner@Sun.COM return (0); 482010440SRoger.Faulkner@Sun.COM 4821789Sahrens default: 48225331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4823789Sahrens } 4824789Sahrens } 4825789Sahrens 4826789Sahrens /*ARGSUSED*/ 4827789Sahrens static int 48285331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48295331Samw caller_context_t *ct) 4830789Sahrens { 4831789Sahrens znode_t *zp = VTOZ(vp); 4832789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4833789Sahrens int error; 48345331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4835789Sahrens 48365367Sahrens ZFS_ENTER(zfsvfs); 48375367Sahrens ZFS_VERIFY_ZP(zp); 48385331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4839789Sahrens ZFS_EXIT(zfsvfs); 4840789Sahrens 4841789Sahrens return (error); 4842789Sahrens } 4843789Sahrens 4844789Sahrens /*ARGSUSED*/ 4845789Sahrens static int 48465331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48475331Samw caller_context_t *ct) 4848789Sahrens { 4849789Sahrens znode_t *zp = VTOZ(vp); 4850789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4851789Sahrens int error; 48525331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 485312294SMark.Musante@Sun.COM zilog_t *zilog = zfsvfs->z_log; 4854789Sahrens 48555367Sahrens ZFS_ENTER(zfsvfs); 48565367Sahrens ZFS_VERIFY_ZP(zp); 485712294SMark.Musante@Sun.COM 48585331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 485912294SMark.Musante@Sun.COM 486012294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 486112699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 486212294SMark.Musante@Sun.COM 4863789Sahrens ZFS_EXIT(zfsvfs); 4864789Sahrens return (error); 4865789Sahrens } 4866789Sahrens 4867789Sahrens /* 486811539SChunli.Zhang@Sun.COM * Tunable, both must be a power of 2. 486911539SChunli.Zhang@Sun.COM * 487011539SChunli.Zhang@Sun.COM * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf 487111539SChunli.Zhang@Sun.COM * zcr_blksz_max: if set to less than the file block size, allow loaning out of 487211539SChunli.Zhang@Sun.COM * an arcbuf for a partial block read 487311539SChunli.Zhang@Sun.COM */ 487411539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10); /* 1K */ 487511539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17); /* 128K */ 487611539SChunli.Zhang@Sun.COM 487711539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 487811539SChunli.Zhang@Sun.COM static int 487911539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 488011539SChunli.Zhang@Sun.COM caller_context_t *ct) 488111539SChunli.Zhang@Sun.COM { 488211539SChunli.Zhang@Sun.COM znode_t *zp = VTOZ(vp); 488311539SChunli.Zhang@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 488411539SChunli.Zhang@Sun.COM int max_blksz = zfsvfs->z_max_blksz; 488511539SChunli.Zhang@Sun.COM uio_t *uio = &xuio->xu_uio; 488611539SChunli.Zhang@Sun.COM ssize_t size = uio->uio_resid; 488711539SChunli.Zhang@Sun.COM offset_t offset = uio->uio_loffset; 488811539SChunli.Zhang@Sun.COM int blksz; 488911539SChunli.Zhang@Sun.COM int fullblk, i; 489011539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 489111539SChunli.Zhang@Sun.COM ssize_t maxsize; 489211539SChunli.Zhang@Sun.COM int preamble, postamble; 489311539SChunli.Zhang@Sun.COM 489411539SChunli.Zhang@Sun.COM if (xuio->xu_type != UIOTYPE_ZEROCOPY) 489511539SChunli.Zhang@Sun.COM return (EINVAL); 489611539SChunli.Zhang@Sun.COM 489711539SChunli.Zhang@Sun.COM ZFS_ENTER(zfsvfs); 489811539SChunli.Zhang@Sun.COM ZFS_VERIFY_ZP(zp); 489911539SChunli.Zhang@Sun.COM switch (ioflag) { 490011539SChunli.Zhang@Sun.COM case UIO_WRITE: 490111539SChunli.Zhang@Sun.COM /* 490211539SChunli.Zhang@Sun.COM * Loan out an arc_buf for write if write size is bigger than 490311539SChunli.Zhang@Sun.COM * max_blksz, and the file's block size is also max_blksz. 490411539SChunli.Zhang@Sun.COM */ 490511539SChunli.Zhang@Sun.COM blksz = max_blksz; 490611539SChunli.Zhang@Sun.COM if (size < blksz || zp->z_blksz != blksz) { 490711539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 490811539SChunli.Zhang@Sun.COM return (EINVAL); 490911539SChunli.Zhang@Sun.COM } 491011539SChunli.Zhang@Sun.COM /* 491111539SChunli.Zhang@Sun.COM * Caller requests buffers for write before knowing where the 491211539SChunli.Zhang@Sun.COM * write offset might be (e.g. NFS TCP write). 491311539SChunli.Zhang@Sun.COM */ 491411539SChunli.Zhang@Sun.COM if (offset == -1) { 491511539SChunli.Zhang@Sun.COM preamble = 0; 491611539SChunli.Zhang@Sun.COM } else { 491711539SChunli.Zhang@Sun.COM preamble = P2PHASE(offset, blksz); 491811539SChunli.Zhang@Sun.COM if (preamble) { 491911539SChunli.Zhang@Sun.COM preamble = blksz - preamble; 492011539SChunli.Zhang@Sun.COM size -= preamble; 492111539SChunli.Zhang@Sun.COM } 492211539SChunli.Zhang@Sun.COM } 492311539SChunli.Zhang@Sun.COM 492411539SChunli.Zhang@Sun.COM postamble = P2PHASE(size, blksz); 492511539SChunli.Zhang@Sun.COM size -= postamble; 492611539SChunli.Zhang@Sun.COM 492711539SChunli.Zhang@Sun.COM fullblk = size / blksz; 492811576SSurya.Prakki@Sun.COM (void) dmu_xuio_init(xuio, 492911539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 493011539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 493111539SChunli.Zhang@Sun.COM int, postamble, int, 493211539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 493311539SChunli.Zhang@Sun.COM 493411539SChunli.Zhang@Sun.COM /* 493511539SChunli.Zhang@Sun.COM * Have to fix iov base/len for partial buffers. They 493611539SChunli.Zhang@Sun.COM * currently represent full arc_buf's. 493711539SChunli.Zhang@Sun.COM */ 493811539SChunli.Zhang@Sun.COM if (preamble) { 493911539SChunli.Zhang@Sun.COM /* data begins in the middle of the arc_buf */ 494011935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 494111935SMark.Shellenbaum@Sun.COM blksz); 494211539SChunli.Zhang@Sun.COM ASSERT(abuf); 494311576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 494411576SSurya.Prakki@Sun.COM blksz - preamble, preamble); 494511539SChunli.Zhang@Sun.COM } 494611539SChunli.Zhang@Sun.COM 494711539SChunli.Zhang@Sun.COM for (i = 0; i < fullblk; i++) { 494811935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 494911935SMark.Shellenbaum@Sun.COM blksz); 495011539SChunli.Zhang@Sun.COM ASSERT(abuf); 495111576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, blksz); 495211539SChunli.Zhang@Sun.COM } 495311539SChunli.Zhang@Sun.COM 495411539SChunli.Zhang@Sun.COM if (postamble) { 495511539SChunli.Zhang@Sun.COM /* data ends 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, 0, postamble); 496011539SChunli.Zhang@Sun.COM } 496111539SChunli.Zhang@Sun.COM break; 496211539SChunli.Zhang@Sun.COM case UIO_READ: 496311539SChunli.Zhang@Sun.COM /* 496411539SChunli.Zhang@Sun.COM * Loan out an arc_buf for read if the read size is larger than 496511539SChunli.Zhang@Sun.COM * the current file block size. Block alignment is not 496611539SChunli.Zhang@Sun.COM * considered. Partial arc_buf will be loaned out for read. 496711539SChunli.Zhang@Sun.COM */ 496811539SChunli.Zhang@Sun.COM blksz = zp->z_blksz; 496911539SChunli.Zhang@Sun.COM if (blksz < zcr_blksz_min) 497011539SChunli.Zhang@Sun.COM blksz = zcr_blksz_min; 497111539SChunli.Zhang@Sun.COM if (blksz > zcr_blksz_max) 497211539SChunli.Zhang@Sun.COM blksz = zcr_blksz_max; 497311539SChunli.Zhang@Sun.COM /* avoid potential complexity of dealing with it */ 497411539SChunli.Zhang@Sun.COM if (blksz > max_blksz) { 497511539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 497611539SChunli.Zhang@Sun.COM return (EINVAL); 497711539SChunli.Zhang@Sun.COM } 497811539SChunli.Zhang@Sun.COM 497911935SMark.Shellenbaum@Sun.COM maxsize = zp->z_size - uio->uio_loffset; 498011539SChunli.Zhang@Sun.COM if (size > maxsize) 498111539SChunli.Zhang@Sun.COM size = maxsize; 498211539SChunli.Zhang@Sun.COM 498311539SChunli.Zhang@Sun.COM if (size < blksz || vn_has_cached_data(vp)) { 498411539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 498511539SChunli.Zhang@Sun.COM return (EINVAL); 498611539SChunli.Zhang@Sun.COM } 498711539SChunli.Zhang@Sun.COM break; 498811539SChunli.Zhang@Sun.COM default: 498911539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 499011539SChunli.Zhang@Sun.COM return (EINVAL); 499111539SChunli.Zhang@Sun.COM } 499211539SChunli.Zhang@Sun.COM 499311539SChunli.Zhang@Sun.COM uio->uio_extflg = UIO_XUIO; 499411539SChunli.Zhang@Sun.COM XUIO_XUZC_RW(xuio) = ioflag; 499511539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 499611539SChunli.Zhang@Sun.COM return (0); 499711539SChunli.Zhang@Sun.COM } 499811539SChunli.Zhang@Sun.COM 499911539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 500011539SChunli.Zhang@Sun.COM static int 500111539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 500211539SChunli.Zhang@Sun.COM { 500311539SChunli.Zhang@Sun.COM int i; 500411539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 500511539SChunli.Zhang@Sun.COM int ioflag = XUIO_XUZC_RW(xuio); 500611539SChunli.Zhang@Sun.COM 500711539SChunli.Zhang@Sun.COM ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 500811539SChunli.Zhang@Sun.COM 500911539SChunli.Zhang@Sun.COM i = dmu_xuio_cnt(xuio); 501011539SChunli.Zhang@Sun.COM while (i-- > 0) { 501111539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i); 501211539SChunli.Zhang@Sun.COM /* 501311539SChunli.Zhang@Sun.COM * if abuf == NULL, it must be a write buffer 501411539SChunli.Zhang@Sun.COM * that has been returned in zfs_write(). 501511539SChunli.Zhang@Sun.COM */ 501611539SChunli.Zhang@Sun.COM if (abuf) 501711539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 501811539SChunli.Zhang@Sun.COM ASSERT(abuf || ioflag == UIO_WRITE); 501911539SChunli.Zhang@Sun.COM } 502011539SChunli.Zhang@Sun.COM 502111539SChunli.Zhang@Sun.COM dmu_xuio_fini(xuio); 502211539SChunli.Zhang@Sun.COM return (0); 502311539SChunli.Zhang@Sun.COM } 502411539SChunli.Zhang@Sun.COM 502511539SChunli.Zhang@Sun.COM /* 5026789Sahrens * Predeclare these here so that the compiler assumes that 5027789Sahrens * this is an "old style" function declaration that does 5028789Sahrens * not include arguments => we won't get type mismatch errors 5029789Sahrens * in the initializations that follow. 5030789Sahrens */ 5031789Sahrens static int zfs_inval(); 5032789Sahrens static int zfs_isdir(); 5033789Sahrens 5034789Sahrens static int 5035789Sahrens zfs_inval() 5036789Sahrens { 5037789Sahrens return (EINVAL); 5038789Sahrens } 5039789Sahrens 5040789Sahrens static int 5041789Sahrens zfs_isdir() 5042789Sahrens { 5043789Sahrens return (EISDIR); 5044789Sahrens } 5045789Sahrens /* 5046789Sahrens * Directory vnode operations template 5047789Sahrens */ 5048789Sahrens vnodeops_t *zfs_dvnodeops; 5049789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 50503898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50513898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50523898Srsb VOPNAME_READ, { .error = zfs_isdir }, 50533898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 50543898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50553898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50563898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50573898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 50583898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 50593898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 50603898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 50613898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 50623898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 50633898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 50643898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 50653898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 50663898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 50673898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 50683898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 50693898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 50703898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 50713898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 50723898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 50733898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 50744863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 50753898Srsb NULL, NULL 5076789Sahrens }; 5077789Sahrens 5078789Sahrens /* 5079789Sahrens * Regular file vnode operations template 5080789Sahrens */ 5081789Sahrens vnodeops_t *zfs_fvnodeops; 5082789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 50833898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50843898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50853898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 50863898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 50873898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50883898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50893898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50903898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 50913898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 50923898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 50933898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 50943898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 50953898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 50963898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 50973898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 50983898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 50993898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 51003898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 51013898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 51023898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 51033898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 51043898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51053898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51063898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51073898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 510811539SChunli.Zhang@Sun.COM VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 510911539SChunli.Zhang@Sun.COM VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 51103898Srsb NULL, NULL 5111789Sahrens }; 5112789Sahrens 5113789Sahrens /* 5114789Sahrens * Symbolic link vnode operations template 5115789Sahrens */ 5116789Sahrens vnodeops_t *zfs_symvnodeops; 5117789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 51183898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51193898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51203898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51213898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51223898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 51233898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51243898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51253898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51263898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51273898Srsb NULL, NULL 5128789Sahrens }; 5129789Sahrens 5130789Sahrens /* 51318845Samw@Sun.COM * special share hidden files vnode operations template 51328845Samw@Sun.COM */ 51338845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 51348845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 51358845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51368845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 51378845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51388845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 51398845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51408845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51418845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51428845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51438845Samw@Sun.COM NULL, NULL 51448845Samw@Sun.COM }; 51458845Samw@Sun.COM 51468845Samw@Sun.COM /* 5147789Sahrens * Extended attribute directory vnode operations template 5148789Sahrens * This template is identical to the directory vnodes 5149789Sahrens * operation template except for restricted operations: 5150789Sahrens * VOP_MKDIR() 5151789Sahrens * VOP_SYMLINK() 5152789Sahrens * Note that there are other restrictions embedded in: 5153789Sahrens * zfs_create() - restrict type to VREG 5154789Sahrens * zfs_link() - no links into/out of attribute space 5155789Sahrens * zfs_rename() - no moves into/out of attribute space 5156789Sahrens */ 5157789Sahrens vnodeops_t *zfs_xdvnodeops; 5158789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 51593898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 51603898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 51613898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 51623898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51633898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51643898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51653898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 51663898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 51673898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 51683898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 51693898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51703898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 51713898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 51723898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 51733898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 51743898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 51753898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51763898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51773898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 51783898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51793898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51803898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51813898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51823898Srsb NULL, NULL 5183789Sahrens }; 5184789Sahrens 5185789Sahrens /* 5186789Sahrens * Error vnode operations template 5187789Sahrens */ 5188789Sahrens vnodeops_t *zfs_evnodeops; 5189789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 51903898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51913898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51923898Srsb NULL, NULL 5193789Sahrens }; 5194