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 } 89612772SNeil.Perrin@Sun.COM /* 89712772SNeil.Perrin@Sun.COM * If we are replaying and eof is non zero then force 89812772SNeil.Perrin@Sun.COM * the file size to the specified eof. Note, there's no 89912772SNeil.Perrin@Sun.COM * concurrency during replay. 90012772SNeil.Perrin@Sun.COM */ 90112772SNeil.Perrin@Sun.COM if (zfsvfs->z_replay && zfsvfs->z_replay_eof != 0) 90212772SNeil.Perrin@Sun.COM zp->z_size = zfsvfs->z_replay_eof; 90312772SNeil.Perrin@Sun.COM 90411935SMark.Shellenbaum@Sun.COM error = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 90511935SMark.Shellenbaum@Sun.COM 9063638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 9073638Sbillm dmu_tx_commit(tx); 9083638Sbillm 9093638Sbillm if (error != 0) 9103638Sbillm break; 9113638Sbillm ASSERT(tx_bytes == nbytes); 9123638Sbillm n -= nbytes; 913789Sahrens } 914789Sahrens 9152237Smaybee zfs_range_unlock(rl); 916789Sahrens 917789Sahrens /* 918789Sahrens * If we're in replay mode, or we made no progress, return error. 919789Sahrens * Otherwise, it's at least a partial write, so it's successful. 920789Sahrens */ 9218227SNeil.Perrin@Sun.COM if (zfsvfs->z_replay || uio->uio_resid == start_resid) { 922789Sahrens ZFS_EXIT(zfsvfs); 923789Sahrens return (error); 924789Sahrens } 925789Sahrens 92612294SMark.Musante@Sun.COM if (ioflag & (FSYNC | FDSYNC) || 92712294SMark.Musante@Sun.COM zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 92812699SNeil.Perrin@Sun.COM zil_commit(zilog, zp->z_id); 929789Sahrens 930789Sahrens ZFS_EXIT(zfsvfs); 931789Sahrens return (0); 932789Sahrens } 933789Sahrens 9342237Smaybee void 93510922SJeff.Bonwick@Sun.COM zfs_get_done(zgd_t *zgd, int error) 9362237Smaybee { 93710922SJeff.Bonwick@Sun.COM znode_t *zp = zgd->zgd_private; 93810922SJeff.Bonwick@Sun.COM objset_t *os = zp->z_zfsvfs->z_os; 93910922SJeff.Bonwick@Sun.COM 94010922SJeff.Bonwick@Sun.COM if (zgd->zgd_db) 94110922SJeff.Bonwick@Sun.COM dmu_buf_rele(zgd->zgd_db, zgd); 94210922SJeff.Bonwick@Sun.COM 94310922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 94410922SJeff.Bonwick@Sun.COM 9459321SNeil.Perrin@Sun.COM /* 9469321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9479321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9489321SNeil.Perrin@Sun.COM */ 94910922SJeff.Bonwick@Sun.COM VN_RELE_ASYNC(ZTOV(zp), dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 95010922SJeff.Bonwick@Sun.COM 95110922SJeff.Bonwick@Sun.COM if (error == 0 && zgd->zgd_bp) 95210922SJeff.Bonwick@Sun.COM zil_add_block(zgd->zgd_zilog, zgd->zgd_bp); 95310922SJeff.Bonwick@Sun.COM 9543063Sperrin kmem_free(zgd, sizeof (zgd_t)); 9552237Smaybee } 9562237Smaybee 95710209SMark.Musante@Sun.COM #ifdef DEBUG 95810209SMark.Musante@Sun.COM static int zil_fault_io = 0; 95910209SMark.Musante@Sun.COM #endif 96010209SMark.Musante@Sun.COM 961789Sahrens /* 962789Sahrens * Get data to generate a TX_WRITE intent log record. 963789Sahrens */ 964789Sahrens int 9652237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 966789Sahrens { 967789Sahrens zfsvfs_t *zfsvfs = arg; 968789Sahrens objset_t *os = zfsvfs->z_os; 969789Sahrens znode_t *zp; 97010922SJeff.Bonwick@Sun.COM uint64_t object = lr->lr_foid; 97110922SJeff.Bonwick@Sun.COM uint64_t offset = lr->lr_offset; 97210922SJeff.Bonwick@Sun.COM uint64_t size = lr->lr_length; 97310922SJeff.Bonwick@Sun.COM blkptr_t *bp = &lr->lr_blkptr; 9742237Smaybee dmu_buf_t *db; 9753063Sperrin zgd_t *zgd; 976789Sahrens int error = 0; 977789Sahrens 97810922SJeff.Bonwick@Sun.COM ASSERT(zio != NULL); 97910922SJeff.Bonwick@Sun.COM ASSERT(size != 0); 980789Sahrens 981789Sahrens /* 9821669Sperrin * Nothing to do if the file has been removed 983789Sahrens */ 98410922SJeff.Bonwick@Sun.COM if (zfs_zget(zfsvfs, object, &zp) != 0) 985789Sahrens return (ENOENT); 9863461Sahrens if (zp->z_unlinked) { 9879321SNeil.Perrin@Sun.COM /* 9889321SNeil.Perrin@Sun.COM * Release the vnode asynchronously as we currently have the 9899321SNeil.Perrin@Sun.COM * txg stopped from syncing. 9909321SNeil.Perrin@Sun.COM */ 9919321SNeil.Perrin@Sun.COM VN_RELE_ASYNC(ZTOV(zp), 9929321SNeil.Perrin@Sun.COM dsl_pool_vnrele_taskq(dmu_objset_pool(os))); 993789Sahrens return (ENOENT); 994789Sahrens } 995789Sahrens 99610922SJeff.Bonwick@Sun.COM zgd = (zgd_t *)kmem_zalloc(sizeof (zgd_t), KM_SLEEP); 99710922SJeff.Bonwick@Sun.COM zgd->zgd_zilog = zfsvfs->z_log; 99810922SJeff.Bonwick@Sun.COM zgd->zgd_private = zp; 99910922SJeff.Bonwick@Sun.COM 1000789Sahrens /* 1001789Sahrens * Write records come in two flavors: immediate and indirect. 1002789Sahrens * For small writes it's cheaper to store the data with the 1003789Sahrens * log record (immediate); for large writes it's cheaper to 1004789Sahrens * sync the data and get a pointer to it (indirect) so that 1005789Sahrens * we don't have to write the data twice. 1006789Sahrens */ 10071669Sperrin if (buf != NULL) { /* immediate write */ 100810922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, RL_READER); 10091669Sperrin /* test for truncation needs to be done while range locked */ 101011935SMark.Shellenbaum@Sun.COM if (offset >= zp->z_size) { 10111669Sperrin error = ENOENT; 101210922SJeff.Bonwick@Sun.COM } else { 101310922SJeff.Bonwick@Sun.COM error = dmu_read(os, object, offset, size, buf, 101410922SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 10151669Sperrin } 101610922SJeff.Bonwick@Sun.COM ASSERT(error == 0 || error == ENOENT); 10171669Sperrin } else { /* indirect write */ 1018789Sahrens /* 10191669Sperrin * Have to lock the whole block to ensure when it's 10201669Sperrin * written out and it's checksum is being calculated 10211669Sperrin * that no one can change the data. We need to re-check 10221669Sperrin * blocksize after we get the lock in case it's changed! 1023789Sahrens */ 10241669Sperrin for (;;) { 102510922SJeff.Bonwick@Sun.COM uint64_t blkoff; 102610922SJeff.Bonwick@Sun.COM size = zp->z_blksz; 102710945SJeff.Bonwick@Sun.COM blkoff = ISP2(size) ? P2PHASE(offset, size) : offset; 102810922SJeff.Bonwick@Sun.COM offset -= blkoff; 102910922SJeff.Bonwick@Sun.COM zgd->zgd_rl = zfs_range_lock(zp, offset, size, 103010922SJeff.Bonwick@Sun.COM RL_READER); 103110922SJeff.Bonwick@Sun.COM if (zp->z_blksz == size) 10321669Sperrin break; 103310922SJeff.Bonwick@Sun.COM offset += blkoff; 103410922SJeff.Bonwick@Sun.COM zfs_range_unlock(zgd->zgd_rl); 10351669Sperrin } 10361669Sperrin /* test for truncation needs to be done while range locked */ 103711935SMark.Shellenbaum@Sun.COM if (lr->lr_offset >= zp->z_size) 10381669Sperrin error = ENOENT; 103910209SMark.Musante@Sun.COM #ifdef DEBUG 104010209SMark.Musante@Sun.COM if (zil_fault_io) { 104110209SMark.Musante@Sun.COM error = EIO; 104210209SMark.Musante@Sun.COM zil_fault_io = 0; 104310209SMark.Musante@Sun.COM } 104410209SMark.Musante@Sun.COM #endif 104510922SJeff.Bonwick@Sun.COM if (error == 0) 104612285SJeff.Bonwick@Sun.COM error = dmu_buf_hold(os, object, offset, zgd, &db, 104712285SJeff.Bonwick@Sun.COM DMU_READ_NO_PREFETCH); 104810922SJeff.Bonwick@Sun.COM 104910800SNeil.Perrin@Sun.COM if (error == 0) { 105010922SJeff.Bonwick@Sun.COM zgd->zgd_db = db; 105110922SJeff.Bonwick@Sun.COM zgd->zgd_bp = bp; 105210922SJeff.Bonwick@Sun.COM 105310922SJeff.Bonwick@Sun.COM ASSERT(db->db_offset == offset); 105410922SJeff.Bonwick@Sun.COM ASSERT(db->db_size == size); 105510922SJeff.Bonwick@Sun.COM 105610922SJeff.Bonwick@Sun.COM error = dmu_sync(zio, lr->lr_common.lrc_txg, 105710922SJeff.Bonwick@Sun.COM zfs_get_done, zgd); 105810922SJeff.Bonwick@Sun.COM ASSERT(error || lr->lr_length <= zp->z_blksz); 105910922SJeff.Bonwick@Sun.COM 106010800SNeil.Perrin@Sun.COM /* 106110922SJeff.Bonwick@Sun.COM * On success, we need to wait for the write I/O 106210922SJeff.Bonwick@Sun.COM * initiated by dmu_sync() to complete before we can 106310922SJeff.Bonwick@Sun.COM * release this dbuf. We will finish everything up 106410922SJeff.Bonwick@Sun.COM * in the zfs_get_done() callback. 106510800SNeil.Perrin@Sun.COM */ 106610922SJeff.Bonwick@Sun.COM if (error == 0) 106710922SJeff.Bonwick@Sun.COM return (0); 106810922SJeff.Bonwick@Sun.COM 106910922SJeff.Bonwick@Sun.COM if (error == EALREADY) { 107010922SJeff.Bonwick@Sun.COM lr->lr_common.lrc_txtype = TX_WRITE2; 107110922SJeff.Bonwick@Sun.COM error = 0; 107210922SJeff.Bonwick@Sun.COM } 107310800SNeil.Perrin@Sun.COM } 1074789Sahrens } 107510922SJeff.Bonwick@Sun.COM 107610922SJeff.Bonwick@Sun.COM zfs_get_done(zgd, error); 107710922SJeff.Bonwick@Sun.COM 1078789Sahrens return (error); 1079789Sahrens } 1080789Sahrens 1081789Sahrens /*ARGSUSED*/ 1082789Sahrens static int 10835331Samw zfs_access(vnode_t *vp, int mode, int flag, cred_t *cr, 10845331Samw caller_context_t *ct) 1085789Sahrens { 1086789Sahrens znode_t *zp = VTOZ(vp); 1087789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1088789Sahrens int error; 1089789Sahrens 10905367Sahrens ZFS_ENTER(zfsvfs); 10915367Sahrens ZFS_VERIFY_ZP(zp); 10925331Samw 10935331Samw if (flag & V_ACE_MASK) 10945331Samw error = zfs_zaccess(zp, mode, flag, B_FALSE, cr); 10955331Samw else 10965331Samw error = zfs_zaccess_rwx(zp, mode, flag, cr); 10975331Samw 1098789Sahrens ZFS_EXIT(zfsvfs); 1099789Sahrens return (error); 1100789Sahrens } 1101789Sahrens 1102789Sahrens /* 11039981STim.Haley@Sun.COM * If vnode is for a device return a specfs vnode instead. 11049981STim.Haley@Sun.COM */ 11059981STim.Haley@Sun.COM static int 11069981STim.Haley@Sun.COM specvp_check(vnode_t **vpp, cred_t *cr) 11079981STim.Haley@Sun.COM { 11089981STim.Haley@Sun.COM int error = 0; 11099981STim.Haley@Sun.COM 11109981STim.Haley@Sun.COM if (IS_DEVVP(*vpp)) { 11119981STim.Haley@Sun.COM struct vnode *svp; 11129981STim.Haley@Sun.COM 11139981STim.Haley@Sun.COM svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 11149981STim.Haley@Sun.COM VN_RELE(*vpp); 11159981STim.Haley@Sun.COM if (svp == NULL) 11169981STim.Haley@Sun.COM error = ENOSYS; 11179981STim.Haley@Sun.COM *vpp = svp; 11189981STim.Haley@Sun.COM } 11199981STim.Haley@Sun.COM return (error); 11209981STim.Haley@Sun.COM } 11219981STim.Haley@Sun.COM 11229981STim.Haley@Sun.COM 11239981STim.Haley@Sun.COM /* 1124789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 1125789Sahrens * If it exists, return a held vnode reference for it. 1126789Sahrens * 1127789Sahrens * IN: dvp - vnode of directory to search. 1128789Sahrens * nm - name of entry to lookup. 1129789Sahrens * pnp - full pathname to lookup [UNUSED]. 1130789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 1131789Sahrens * rdir - root directory vnode [UNUSED]. 1132789Sahrens * cr - credentials of caller. 11335331Samw * ct - caller context 11345331Samw * direntflags - directory lookup flags 11355331Samw * realpnp - returned pathname. 1136789Sahrens * 1137789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 1138789Sahrens * 1139789Sahrens * RETURN: 0 if success 1140789Sahrens * error code if failure 1141789Sahrens * 1142789Sahrens * Timestamps: 1143789Sahrens * NA 1144789Sahrens */ 1145789Sahrens /* ARGSUSED */ 1146789Sahrens static int 1147789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 11485331Samw int flags, vnode_t *rdir, cred_t *cr, caller_context_t *ct, 11495331Samw int *direntflags, pathname_t *realpnp) 1150789Sahrens { 1151789Sahrens znode_t *zdp = VTOZ(dvp); 1152789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 11539981STim.Haley@Sun.COM int error = 0; 11549981STim.Haley@Sun.COM 11559981STim.Haley@Sun.COM /* fast path */ 11569981STim.Haley@Sun.COM if (!(flags & (LOOKUP_XATTR | FIGNORECASE))) { 11579981STim.Haley@Sun.COM 11589981STim.Haley@Sun.COM if (dvp->v_type != VDIR) { 11599981STim.Haley@Sun.COM return (ENOTDIR); 116011935SMark.Shellenbaum@Sun.COM } else if (zdp->z_sa_hdl == NULL) { 11619981STim.Haley@Sun.COM return (EIO); 11629981STim.Haley@Sun.COM } 11639981STim.Haley@Sun.COM 11649981STim.Haley@Sun.COM if (nm[0] == 0 || (nm[0] == '.' && nm[1] == '\0')) { 11659981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11669981STim.Haley@Sun.COM if (!error) { 11679981STim.Haley@Sun.COM *vpp = dvp; 11689981STim.Haley@Sun.COM VN_HOLD(*vpp); 11699981STim.Haley@Sun.COM return (0); 11709981STim.Haley@Sun.COM } 11719981STim.Haley@Sun.COM return (error); 11729981STim.Haley@Sun.COM } else { 11739981STim.Haley@Sun.COM vnode_t *tvp = dnlc_lookup(dvp, nm); 11749981STim.Haley@Sun.COM 11759981STim.Haley@Sun.COM if (tvp) { 11769981STim.Haley@Sun.COM error = zfs_fastaccesschk_execute(zdp, cr); 11779981STim.Haley@Sun.COM if (error) { 11789981STim.Haley@Sun.COM VN_RELE(tvp); 11799981STim.Haley@Sun.COM return (error); 11809981STim.Haley@Sun.COM } 11819981STim.Haley@Sun.COM if (tvp == DNLC_NO_VNODE) { 11829981STim.Haley@Sun.COM VN_RELE(tvp); 11839981STim.Haley@Sun.COM return (ENOENT); 11849981STim.Haley@Sun.COM } else { 11859981STim.Haley@Sun.COM *vpp = tvp; 11869981STim.Haley@Sun.COM return (specvp_check(vpp, cr)); 11879981STim.Haley@Sun.COM } 11889981STim.Haley@Sun.COM } 11899981STim.Haley@Sun.COM } 11909981STim.Haley@Sun.COM } 11919981STim.Haley@Sun.COM 11929981STim.Haley@Sun.COM DTRACE_PROBE2(zfs__fastpath__lookup__miss, vnode_t *, dvp, char *, nm); 1193789Sahrens 11945367Sahrens ZFS_ENTER(zfsvfs); 11955367Sahrens ZFS_VERIFY_ZP(zdp); 1196789Sahrens 1197789Sahrens *vpp = NULL; 1198789Sahrens 1199789Sahrens if (flags & LOOKUP_XATTR) { 1200789Sahrens /* 12013234Sck153898 * If the xattr property is off, refuse the lookup request. 12023234Sck153898 */ 12033234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 12043234Sck153898 ZFS_EXIT(zfsvfs); 12053234Sck153898 return (EINVAL); 12063234Sck153898 } 12073234Sck153898 12083234Sck153898 /* 1209789Sahrens * We don't allow recursive attributes.. 1210789Sahrens * Maybe someday we will. 1211789Sahrens */ 121211935SMark.Shellenbaum@Sun.COM if (zdp->z_pflags & ZFS_XATTR) { 1213789Sahrens ZFS_EXIT(zfsvfs); 1214789Sahrens return (EINVAL); 1215789Sahrens } 1216789Sahrens 12173280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 1218789Sahrens ZFS_EXIT(zfsvfs); 1219789Sahrens return (error); 1220789Sahrens } 1221789Sahrens 1222789Sahrens /* 1223789Sahrens * Do we have permission to get into attribute directory? 1224789Sahrens */ 1225789Sahrens 12265331Samw if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, 0, 12275331Samw B_FALSE, cr)) { 1228789Sahrens VN_RELE(*vpp); 12295331Samw *vpp = NULL; 1230789Sahrens } 1231789Sahrens 1232789Sahrens ZFS_EXIT(zfsvfs); 1233789Sahrens return (error); 1234789Sahrens } 1235789Sahrens 12361512Sek110237 if (dvp->v_type != VDIR) { 12371512Sek110237 ZFS_EXIT(zfsvfs); 12381460Smarks return (ENOTDIR); 12391512Sek110237 } 12401460Smarks 1241789Sahrens /* 1242789Sahrens * Check accessibility of directory. 1243789Sahrens */ 1244789Sahrens 12455331Samw if (error = zfs_zaccess(zdp, ACE_EXECUTE, 0, B_FALSE, cr)) { 1246789Sahrens ZFS_EXIT(zfsvfs); 1247789Sahrens return (error); 1248789Sahrens } 1249789Sahrens 12505498Stimh if (zfsvfs->z_utf8 && u8_validate(nm, strlen(nm), 12515331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 12525331Samw ZFS_EXIT(zfsvfs); 12535331Samw return (EILSEQ); 12545331Samw } 12555331Samw 12565331Samw error = zfs_dirlook(zdp, nm, vpp, flags, direntflags, realpnp); 12579981STim.Haley@Sun.COM if (error == 0) 12589981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1259789Sahrens 1260789Sahrens ZFS_EXIT(zfsvfs); 1261789Sahrens return (error); 1262789Sahrens } 1263789Sahrens 1264789Sahrens /* 1265789Sahrens * Attempt to create a new entry in a directory. If the entry 1266789Sahrens * already exists, truncate the file if permissible, else return 1267789Sahrens * an error. Return the vp of the created or trunc'd file. 1268789Sahrens * 1269789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1270789Sahrens * name - name of new file entry. 1271789Sahrens * vap - attributes of new file. 1272789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1273789Sahrens * mode - mode to open file with. 1274789Sahrens * cr - credentials of caller. 1275789Sahrens * flag - large file flag [UNUSED]. 12765331Samw * ct - caller context 12775331Samw * vsecp - ACL to be set 1278789Sahrens * 1279789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1280789Sahrens * 1281789Sahrens * RETURN: 0 if success 1282789Sahrens * error code if failure 1283789Sahrens * 1284789Sahrens * Timestamps: 1285789Sahrens * dvp - ctime|mtime updated if new entry created 1286789Sahrens * vp - ctime|mtime always, atime if new 1287789Sahrens */ 12885331Samw 1289789Sahrens /* ARGSUSED */ 1290789Sahrens static int 1291789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 12925331Samw int mode, vnode_t **vpp, cred_t *cr, int flag, caller_context_t *ct, 12935331Samw vsecattr_t *vsecp) 1294789Sahrens { 1295789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1296789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 12975326Sek110237 zilog_t *zilog; 12985326Sek110237 objset_t *os; 1299789Sahrens zfs_dirlock_t *dl; 1300789Sahrens dmu_tx_t *tx; 1301789Sahrens int error; 13027847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 13037847SMark.Shellenbaum@Sun.COM uid_t uid; 13047847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 130511935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 13069179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 130712302SMark.Shellenbaum@Sun.COM boolean_t have_acl = B_FALSE; 13085331Samw 13095331Samw /* 13105331Samw * If we have an ephemeral id, ACL, or XVATTR then 13115331Samw * make sure file system is at proper version 13125331Samw */ 13135331Samw 13147847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 13157847SMark.Shellenbaum@Sun.COM if (ksid) 13167847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 13177847SMark.Shellenbaum@Sun.COM else 13187847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 13197847SMark.Shellenbaum@Sun.COM 13205331Samw if (zfsvfs->z_use_fuids == B_FALSE && 13215331Samw (vsecp || (vap->va_mask & AT_XVATTR) || 13227847SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 13235331Samw return (EINVAL); 1324789Sahrens 13255367Sahrens ZFS_ENTER(zfsvfs); 13265367Sahrens ZFS_VERIFY_ZP(dzp); 13275326Sek110237 os = zfsvfs->z_os; 13285326Sek110237 zilog = zfsvfs->z_log; 1329789Sahrens 13305498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 13315331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 13325331Samw ZFS_EXIT(zfsvfs); 13335331Samw return (EILSEQ); 13345331Samw } 13355331Samw 13365331Samw if (vap->va_mask & AT_XVATTR) { 13375331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 13385331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 13395331Samw ZFS_EXIT(zfsvfs); 13405331Samw return (error); 13415331Samw } 13425331Samw } 1343789Sahrens top: 1344789Sahrens *vpp = NULL; 1345789Sahrens 1346789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1347789Sahrens vap->va_mode &= ~VSVTX; 1348789Sahrens 1349789Sahrens if (*name == '\0') { 1350789Sahrens /* 1351789Sahrens * Null component name refers to the directory itself. 1352789Sahrens */ 1353789Sahrens VN_HOLD(dvp); 1354789Sahrens zp = dzp; 1355789Sahrens dl = NULL; 1356789Sahrens error = 0; 1357789Sahrens } else { 1358789Sahrens /* possible VN_HOLD(zp) */ 13595331Samw int zflg = 0; 13605331Samw 13615331Samw if (flag & FIGNORECASE) 13625331Samw zflg |= ZCILOOK; 13635331Samw 13645331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 13655331Samw NULL, NULL); 13665331Samw if (error) { 1367789Sahrens if (strcmp(name, "..") == 0) 1368789Sahrens error = EISDIR; 1369789Sahrens ZFS_EXIT(zfsvfs); 1370789Sahrens return (error); 1371789Sahrens } 1372789Sahrens } 137311935SMark.Shellenbaum@Sun.COM 1374789Sahrens if (zp == NULL) { 13755331Samw uint64_t txtype; 13765331Samw 1377789Sahrens /* 1378789Sahrens * Create a new file object and update the directory 1379789Sahrens * to reference it. 1380789Sahrens */ 13815331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 1382789Sahrens goto out; 1383789Sahrens } 1384789Sahrens 1385789Sahrens /* 1386789Sahrens * We only support the creation of regular files in 1387789Sahrens * extended attribute directories. 1388789Sahrens */ 138911935SMark.Shellenbaum@Sun.COM 139011935SMark.Shellenbaum@Sun.COM if ((dzp->z_pflags & ZFS_XATTR) && 1391789Sahrens (vap->va_type != VREG)) { 1392789Sahrens error = EINVAL; 1393789Sahrens goto out; 1394789Sahrens } 1395789Sahrens 139612302SMark.Shellenbaum@Sun.COM if (!have_acl && (error = zfs_acl_ids_create(dzp, 0, vap, 139712302SMark.Shellenbaum@Sun.COM cr, vsecp, &acl_ids)) != 0) 13989179SMark.Shellenbaum@Sun.COM goto out; 139912302SMark.Shellenbaum@Sun.COM have_acl = B_TRUE; 140012302SMark.Shellenbaum@Sun.COM 14019396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 140210143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 14039396SMatthew.Ahrens@Sun.COM error = EDQUOT; 14049396SMatthew.Ahrens@Sun.COM goto out; 14059396SMatthew.Ahrens@Sun.COM } 14069179SMark.Shellenbaum@Sun.COM 1407789Sahrens tx = dmu_tx_create(os); 140811935SMark.Shellenbaum@Sun.COM 140911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 141011935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 141111935SMark.Shellenbaum@Sun.COM 14129179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 14139396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 14149396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 14151544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 141611935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 141711935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && 141811935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 1419789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 142011935SMark.Shellenbaum@Sun.COM 0, acl_ids.z_aclp->z_acl_bytes); 14215331Samw } 14228227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1423789Sahrens if (error) { 1424789Sahrens zfs_dirent_unlock(dl); 14258227SNeil.Perrin@Sun.COM if (error == ERESTART) { 14262113Sahrens dmu_tx_wait(tx); 14272113Sahrens dmu_tx_abort(tx); 1428789Sahrens goto top; 1429789Sahrens } 143012302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 14312113Sahrens dmu_tx_abort(tx); 1432789Sahrens ZFS_EXIT(zfsvfs); 1433789Sahrens return (error); 1434789Sahrens } 143511935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 14369179SMark.Shellenbaum@Sun.COM 14379179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 14389179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 14399179SMark.Shellenbaum@Sun.COM 1440789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 14415331Samw txtype = zfs_log_create_txtype(Z_FILE, vsecp, vap); 14425331Samw if (flag & FIGNORECASE) 14435331Samw txtype |= TX_CI; 14445331Samw zfs_log_create(zilog, tx, txtype, dzp, zp, name, 14459179SMark.Shellenbaum@Sun.COM vsecp, acl_ids.z_fuidp, vap); 14469179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1447789Sahrens dmu_tx_commit(tx); 1448789Sahrens } else { 14495331Samw int aflags = (flag & FAPPEND) ? V_APPEND : 0; 14505331Samw 1451789Sahrens /* 1452789Sahrens * A directory entry already exists for this name. 1453789Sahrens */ 1454789Sahrens /* 1455789Sahrens * Can't truncate an existing file if in exclusive mode. 1456789Sahrens */ 1457789Sahrens if (excl == EXCL) { 1458789Sahrens error = EEXIST; 1459789Sahrens goto out; 1460789Sahrens } 1461789Sahrens /* 1462789Sahrens * Can't open a directory for writing. 1463789Sahrens */ 1464789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1465789Sahrens error = EISDIR; 1466789Sahrens goto out; 1467789Sahrens } 1468789Sahrens /* 1469789Sahrens * Verify requested access to file. 1470789Sahrens */ 14715331Samw if (mode && (error = zfs_zaccess_rwx(zp, mode, aflags, cr))) { 1472789Sahrens goto out; 1473789Sahrens } 1474789Sahrens 1475789Sahrens mutex_enter(&dzp->z_lock); 1476789Sahrens dzp->z_seq++; 1477789Sahrens mutex_exit(&dzp->z_lock); 1478789Sahrens 14791878Smaybee /* 14801878Smaybee * Truncate regular files if requested. 14811878Smaybee */ 14821878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1483789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 14846992Smaybee /* we can't hold any locks when calling zfs_freesp() */ 14856992Smaybee zfs_dirent_unlock(dl); 14866992Smaybee dl = NULL; 14871878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 14884863Spraks if (error == 0) { 14895331Samw vnevent_create(ZTOV(zp), ct); 14904863Spraks } 1491789Sahrens } 1492789Sahrens } 1493789Sahrens out: 1494789Sahrens 1495789Sahrens if (dl) 1496789Sahrens zfs_dirent_unlock(dl); 1497789Sahrens 1498789Sahrens if (error) { 1499789Sahrens if (zp) 1500789Sahrens VN_RELE(ZTOV(zp)); 1501789Sahrens } else { 1502789Sahrens *vpp = ZTOV(zp); 15039981STim.Haley@Sun.COM error = specvp_check(vpp, cr); 1504789Sahrens } 1505789Sahrens 150612294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 150712699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 150812294SMark.Musante@Sun.COM 1509789Sahrens ZFS_EXIT(zfsvfs); 1510789Sahrens return (error); 1511789Sahrens } 1512789Sahrens 1513789Sahrens /* 1514789Sahrens * Remove an entry from a directory. 1515789Sahrens * 1516789Sahrens * IN: dvp - vnode of directory to remove entry from. 1517789Sahrens * name - name of entry to remove. 1518789Sahrens * cr - credentials of caller. 15195331Samw * ct - caller context 15205331Samw * flags - case flags 1521789Sahrens * 1522789Sahrens * RETURN: 0 if success 1523789Sahrens * error code if failure 1524789Sahrens * 1525789Sahrens * Timestamps: 1526789Sahrens * dvp - ctime|mtime 1527789Sahrens * vp - ctime (if nlink > 0) 1528789Sahrens */ 152911935SMark.Shellenbaum@Sun.COM 153011935SMark.Shellenbaum@Sun.COM uint64_t null_xattr = 0; 153111935SMark.Shellenbaum@Sun.COM 15325331Samw /*ARGSUSED*/ 1533789Sahrens static int 15345331Samw zfs_remove(vnode_t *dvp, char *name, cred_t *cr, caller_context_t *ct, 15355331Samw int flags) 1536789Sahrens { 1537789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1538789Sahrens znode_t *xzp = NULL; 1539789Sahrens vnode_t *vp; 1540789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 15415326Sek110237 zilog_t *zilog; 154211935SMark.Shellenbaum@Sun.COM uint64_t acl_obj, xattr_obj = 0; 154311935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj_unlinked = 0; 154412700SNeil.Perrin@Sun.COM uint64_t obj = 0; 1545789Sahrens zfs_dirlock_t *dl; 1546789Sahrens dmu_tx_t *tx; 15473461Sahrens boolean_t may_delete_now, delete_now = FALSE; 15486992Smaybee boolean_t unlinked, toobig = FALSE; 15495331Samw uint64_t txtype; 15505331Samw pathname_t *realnmp = NULL; 15515331Samw pathname_t realnm; 1552789Sahrens int error; 15535331Samw int zflg = ZEXISTS; 1554789Sahrens 15555367Sahrens ZFS_ENTER(zfsvfs); 15565367Sahrens ZFS_VERIFY_ZP(dzp); 15575326Sek110237 zilog = zfsvfs->z_log; 1558789Sahrens 15595331Samw if (flags & FIGNORECASE) { 15605331Samw zflg |= ZCILOOK; 15615331Samw pn_alloc(&realnm); 15625331Samw realnmp = &realnm; 15635331Samw } 15645331Samw 1565789Sahrens top: 1566789Sahrens /* 1567789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1568789Sahrens */ 15695331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 15705331Samw NULL, realnmp)) { 15715331Samw if (realnmp) 15725331Samw pn_free(realnmp); 1573789Sahrens ZFS_EXIT(zfsvfs); 1574789Sahrens return (error); 1575789Sahrens } 1576789Sahrens 1577789Sahrens vp = ZTOV(zp); 1578789Sahrens 1579789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1580789Sahrens goto out; 1581789Sahrens } 1582789Sahrens 1583789Sahrens /* 1584789Sahrens * Need to use rmdir for removing directories. 1585789Sahrens */ 1586789Sahrens if (vp->v_type == VDIR) { 1587789Sahrens error = EPERM; 1588789Sahrens goto out; 1589789Sahrens } 1590789Sahrens 15915331Samw vnevent_remove(vp, dvp, name, ct); 15925331Samw 15935331Samw if (realnmp) 15946492Stimh dnlc_remove(dvp, realnmp->pn_buf); 15955331Samw else 15965331Samw dnlc_remove(dvp, name); 15971484Sek110237 1598789Sahrens mutex_enter(&vp->v_lock); 1599789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1600789Sahrens mutex_exit(&vp->v_lock); 1601789Sahrens 1602789Sahrens /* 16033461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1604789Sahrens * it depends on whether we're the last link, and on whether there are 1605789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1606789Sahrens * allow for either case. 1607789Sahrens */ 160812700SNeil.Perrin@Sun.COM obj = zp->z_id; 1609789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 16101544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 161111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 161211935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 161311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 16146992Smaybee if (may_delete_now) { 16156992Smaybee toobig = 161611935SMark.Shellenbaum@Sun.COM zp->z_size > zp->z_blksz * DMU_MAX_DELETEBLKCNT; 16176992Smaybee /* if the file is too big, only hold_free a token amount */ 16186992Smaybee dmu_tx_hold_free(tx, zp->z_id, 0, 16196992Smaybee (toobig ? DMU_MAX_ACCESS : DMU_OBJECT_END)); 16206992Smaybee } 1621789Sahrens 1622789Sahrens /* are there any extended attributes? */ 162311935SMark.Shellenbaum@Sun.COM error = sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 162411935SMark.Shellenbaum@Sun.COM &xattr_obj, sizeof (xattr_obj)); 162511935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 162611935SMark.Shellenbaum@Sun.COM error = zfs_zget(zfsvfs, xattr_obj, &xzp); 162711935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 162811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 162911935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, xzp->z_sa_hdl, B_FALSE); 1630789Sahrens } 1631789Sahrens 163212620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 163312620SMark.Shellenbaum@Oracle.COM if ((acl_obj = zfs_external_acl(zp)) != 0 && may_delete_now) 1634789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 163512620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 1636789Sahrens 1637789Sahrens /* charge as an update -- would be nice not to charge at all */ 16383461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1639789Sahrens 16408227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1641789Sahrens if (error) { 1642789Sahrens zfs_dirent_unlock(dl); 1643789Sahrens VN_RELE(vp); 16448227SNeil.Perrin@Sun.COM if (error == ERESTART) { 16452113Sahrens dmu_tx_wait(tx); 16462113Sahrens dmu_tx_abort(tx); 1647789Sahrens goto top; 1648789Sahrens } 16495331Samw if (realnmp) 16505331Samw pn_free(realnmp); 16512113Sahrens dmu_tx_abort(tx); 1652789Sahrens ZFS_EXIT(zfsvfs); 1653789Sahrens return (error); 1654789Sahrens } 1655789Sahrens 1656789Sahrens /* 1657789Sahrens * Remove the directory entry. 1658789Sahrens */ 16595331Samw error = zfs_link_destroy(dl, zp, tx, zflg, &unlinked); 1660789Sahrens 1661789Sahrens if (error) { 1662789Sahrens dmu_tx_commit(tx); 1663789Sahrens goto out; 1664789Sahrens } 1665789Sahrens 16663461Sahrens if (unlinked) { 166711935SMark.Shellenbaum@Sun.COM 166812620SMark.Shellenbaum@Oracle.COM /* 166912620SMark.Shellenbaum@Oracle.COM * Hold z_lock so that we can make sure that the ACL obj 167012620SMark.Shellenbaum@Oracle.COM * hasn't changed. Could have been deleted due to 167112620SMark.Shellenbaum@Oracle.COM * zfs_sa_upgrade(). 167212620SMark.Shellenbaum@Oracle.COM */ 167312620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 1674789Sahrens mutex_enter(&vp->v_lock); 167511935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), 167611935SMark.Shellenbaum@Sun.COM &xattr_obj_unlinked, sizeof (xattr_obj_unlinked)); 16776992Smaybee delete_now = may_delete_now && !toobig && 1678789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 167912620SMark.Shellenbaum@Oracle.COM xattr_obj == xattr_obj_unlinked && zfs_external_acl(zp) == 168011935SMark.Shellenbaum@Sun.COM acl_obj; 1681789Sahrens mutex_exit(&vp->v_lock); 1682789Sahrens } 1683789Sahrens 1684789Sahrens if (delete_now) { 168511935SMark.Shellenbaum@Sun.COM if (xattr_obj_unlinked) { 168611935SMark.Shellenbaum@Sun.COM ASSERT3U(xzp->z_links, ==, 2); 1687789Sahrens mutex_enter(&xzp->z_lock); 16883461Sahrens xzp->z_unlinked = 1; 168911935SMark.Shellenbaum@Sun.COM xzp->z_links = 0; 169011935SMark.Shellenbaum@Sun.COM error = sa_update(xzp->z_sa_hdl, SA_ZPL_LINKS(zfsvfs), 169111935SMark.Shellenbaum@Sun.COM &xzp->z_links, sizeof (xzp->z_links), tx); 169211935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1693789Sahrens mutex_exit(&xzp->z_lock); 16943461Sahrens zfs_unlinked_add(xzp, tx); 169512620SMark.Shellenbaum@Oracle.COM 169611935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 169711935SMark.Shellenbaum@Sun.COM error = sa_remove(zp->z_sa_hdl, 169811935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), tx); 169911935SMark.Shellenbaum@Sun.COM else 170011935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, 170111935SMark.Shellenbaum@Sun.COM SA_ZPL_XATTR(zfsvfs), &null_xattr, 170211935SMark.Shellenbaum@Sun.COM sizeof (uint64_t), tx); 170311935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 1704789Sahrens } 1705789Sahrens mutex_enter(&vp->v_lock); 1706789Sahrens vp->v_count--; 1707789Sahrens ASSERT3U(vp->v_count, ==, 0); 1708789Sahrens mutex_exit(&vp->v_lock); 1709789Sahrens mutex_exit(&zp->z_lock); 1710789Sahrens zfs_znode_delete(zp, tx); 17113461Sahrens } else if (unlinked) { 171212620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 17133461Sahrens zfs_unlinked_add(zp, tx); 1714789Sahrens } 1715789Sahrens 17165331Samw txtype = TX_REMOVE; 17175331Samw if (flags & FIGNORECASE) 17185331Samw txtype |= TX_CI; 171912700SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, obj); 1720789Sahrens 1721789Sahrens dmu_tx_commit(tx); 1722789Sahrens out: 17235331Samw if (realnmp) 17245331Samw pn_free(realnmp); 17255331Samw 1726789Sahrens zfs_dirent_unlock(dl); 1727789Sahrens 172812178SMark.Shellenbaum@Sun.COM if (!delete_now) 1729789Sahrens VN_RELE(vp); 173012178SMark.Shellenbaum@Sun.COM if (xzp) 1731789Sahrens VN_RELE(ZTOV(xzp)); 1732789Sahrens 173312294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 173412699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 173512294SMark.Musante@Sun.COM 1736789Sahrens ZFS_EXIT(zfsvfs); 1737789Sahrens return (error); 1738789Sahrens } 1739789Sahrens 1740789Sahrens /* 1741789Sahrens * Create a new directory and insert it into dvp using the name 1742789Sahrens * provided. Return a pointer to the inserted directory. 1743789Sahrens * 1744789Sahrens * IN: dvp - vnode of directory to add subdir to. 1745789Sahrens * dirname - name of new directory. 1746789Sahrens * vap - attributes of new directory. 1747789Sahrens * cr - credentials of caller. 17485331Samw * ct - caller context 17495331Samw * vsecp - ACL to be set 1750789Sahrens * 1751789Sahrens * OUT: vpp - vnode of created directory. 1752789Sahrens * 1753789Sahrens * RETURN: 0 if success 1754789Sahrens * error code if failure 1755789Sahrens * 1756789Sahrens * Timestamps: 1757789Sahrens * dvp - ctime|mtime updated 1758789Sahrens * vp - ctime|mtime|atime updated 1759789Sahrens */ 17605331Samw /*ARGSUSED*/ 1761789Sahrens static int 17625331Samw zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr, 17635331Samw caller_context_t *ct, int flags, vsecattr_t *vsecp) 1764789Sahrens { 1765789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1766789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 17675326Sek110237 zilog_t *zilog; 1768789Sahrens zfs_dirlock_t *dl; 17695331Samw uint64_t txtype; 1770789Sahrens dmu_tx_t *tx; 1771789Sahrens int error; 17725331Samw int zf = ZNEW; 17737847SMark.Shellenbaum@Sun.COM ksid_t *ksid; 17747847SMark.Shellenbaum@Sun.COM uid_t uid; 17757847SMark.Shellenbaum@Sun.COM gid_t gid = crgetgid(cr); 177611935SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 17779179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 1778789Sahrens 1779789Sahrens ASSERT(vap->va_type == VDIR); 1780789Sahrens 17815331Samw /* 17825331Samw * If we have an ephemeral id, ACL, or XVATTR then 17835331Samw * make sure file system is at proper version 17845331Samw */ 17855331Samw 17867847SMark.Shellenbaum@Sun.COM ksid = crgetsid(cr, KSID_OWNER); 17877847SMark.Shellenbaum@Sun.COM if (ksid) 17887847SMark.Shellenbaum@Sun.COM uid = ksid_getid(ksid); 17897847SMark.Shellenbaum@Sun.COM else 17907847SMark.Shellenbaum@Sun.COM uid = crgetuid(cr); 17915331Samw if (zfsvfs->z_use_fuids == B_FALSE && 17927847SMark.Shellenbaum@Sun.COM (vsecp || (vap->va_mask & AT_XVATTR) || 17937876SMark.Shellenbaum@Sun.COM IS_EPHEMERAL(uid) || IS_EPHEMERAL(gid))) 17945331Samw return (EINVAL); 17955331Samw 17965367Sahrens ZFS_ENTER(zfsvfs); 17975367Sahrens ZFS_VERIFY_ZP(dzp); 17985326Sek110237 zilog = zfsvfs->z_log; 1799789Sahrens 180011935SMark.Shellenbaum@Sun.COM if (dzp->z_pflags & ZFS_XATTR) { 1801789Sahrens ZFS_EXIT(zfsvfs); 1802789Sahrens return (EINVAL); 1803789Sahrens } 18045331Samw 18055498Stimh if (zfsvfs->z_utf8 && u8_validate(dirname, 18065331Samw strlen(dirname), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 18075331Samw ZFS_EXIT(zfsvfs); 18085331Samw return (EILSEQ); 18095331Samw } 18105331Samw if (flags & FIGNORECASE) 18115331Samw zf |= ZCILOOK; 18125331Samw 181312302SMark.Shellenbaum@Sun.COM if (vap->va_mask & AT_XVATTR) { 18145331Samw if ((error = secpolicy_xvattr((xvattr_t *)vap, 18155331Samw crgetuid(cr), cr, vap->va_type)) != 0) { 18165331Samw ZFS_EXIT(zfsvfs); 18175331Samw return (error); 18185331Samw } 181912302SMark.Shellenbaum@Sun.COM } 182012302SMark.Shellenbaum@Sun.COM 182112302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, vap, cr, 182212302SMark.Shellenbaum@Sun.COM vsecp, &acl_ids)) != 0) { 182312302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 182412302SMark.Shellenbaum@Sun.COM return (error); 182512302SMark.Shellenbaum@Sun.COM } 1826789Sahrens /* 1827789Sahrens * First make sure the new directory doesn't exist. 182812302SMark.Shellenbaum@Sun.COM * 182912302SMark.Shellenbaum@Sun.COM * Existence is checked first to make sure we don't return 183012302SMark.Shellenbaum@Sun.COM * EACCES instead of EEXIST which can cause some applications 183112302SMark.Shellenbaum@Sun.COM * to fail. 1832789Sahrens */ 18335331Samw top: 18345331Samw *vpp = NULL; 18355331Samw 18365331Samw if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, zf, 18375331Samw NULL, NULL)) { 183812302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 1839789Sahrens ZFS_EXIT(zfsvfs); 1840789Sahrens return (error); 1841789Sahrens } 1842789Sahrens 18435331Samw if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, 0, B_FALSE, cr)) { 184412302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18451231Smarks zfs_dirent_unlock(dl); 18461231Smarks ZFS_EXIT(zfsvfs); 18471231Smarks return (error); 18481231Smarks } 18491231Smarks 18509396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 185110143STim.Haley@Sun.COM zfs_acl_ids_free(&acl_ids); 18529396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 18539396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 18549396SMatthew.Ahrens@Sun.COM return (EDQUOT); 18559396SMatthew.Ahrens@Sun.COM } 18569179SMark.Shellenbaum@Sun.COM 1857789Sahrens /* 1858789Sahrens * Add a new entry to the directory. 1859789Sahrens */ 1860789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 18611544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 18621544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 18639179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 18649396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 18659396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 186611935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 186711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 186811935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 186911935SMark.Shellenbaum@Sun.COM } 187011935SMark.Shellenbaum@Sun.COM 187111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 187211935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE); 187311935SMark.Shellenbaum@Sun.COM 18748227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 1875789Sahrens if (error) { 1876789Sahrens zfs_dirent_unlock(dl); 18778227SNeil.Perrin@Sun.COM if (error == ERESTART) { 18782113Sahrens dmu_tx_wait(tx); 18792113Sahrens dmu_tx_abort(tx); 1880789Sahrens goto top; 1881789Sahrens } 188212302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 18832113Sahrens dmu_tx_abort(tx); 1884789Sahrens ZFS_EXIT(zfsvfs); 1885789Sahrens return (error); 1886789Sahrens } 1887789Sahrens 1888789Sahrens /* 1889789Sahrens * Create new node. 1890789Sahrens */ 189111935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 18929179SMark.Shellenbaum@Sun.COM 18939179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 18949179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 189511935SMark.Shellenbaum@Sun.COM 1896789Sahrens /* 1897789Sahrens * Now put new name in parent dir. 1898789Sahrens */ 1899789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1900789Sahrens 1901789Sahrens *vpp = ZTOV(zp); 1902789Sahrens 19035331Samw txtype = zfs_log_create_txtype(Z_DIR, vsecp, vap); 19045331Samw if (flags & FIGNORECASE) 19055331Samw txtype |= TX_CI; 19069179SMark.Shellenbaum@Sun.COM zfs_log_create(zilog, tx, txtype, dzp, zp, dirname, vsecp, 19079179SMark.Shellenbaum@Sun.COM acl_ids.z_fuidp, vap); 19089179SMark.Shellenbaum@Sun.COM 19099179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 191011935SMark.Shellenbaum@Sun.COM 1911789Sahrens dmu_tx_commit(tx); 1912789Sahrens 1913789Sahrens zfs_dirent_unlock(dl); 1914789Sahrens 191512294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 191612699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 191712294SMark.Musante@Sun.COM 1918789Sahrens ZFS_EXIT(zfsvfs); 1919789Sahrens return (0); 1920789Sahrens } 1921789Sahrens 1922789Sahrens /* 1923789Sahrens * Remove a directory subdir entry. If the current working 1924789Sahrens * directory is the same as the subdir to be removed, the 1925789Sahrens * remove will fail. 1926789Sahrens * 1927789Sahrens * IN: dvp - vnode of directory to remove from. 1928789Sahrens * name - name of directory to be removed. 1929789Sahrens * cwd - vnode of current working directory. 1930789Sahrens * cr - credentials of caller. 19315331Samw * ct - caller context 19325331Samw * flags - case flags 1933789Sahrens * 1934789Sahrens * RETURN: 0 if success 1935789Sahrens * error code if failure 1936789Sahrens * 1937789Sahrens * Timestamps: 1938789Sahrens * dvp - ctime|mtime updated 1939789Sahrens */ 19405331Samw /*ARGSUSED*/ 1941789Sahrens static int 19425331Samw zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr, 19435331Samw caller_context_t *ct, int flags) 1944789Sahrens { 1945789Sahrens znode_t *dzp = VTOZ(dvp); 1946789Sahrens znode_t *zp; 1947789Sahrens vnode_t *vp; 1948789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 19495326Sek110237 zilog_t *zilog; 1950789Sahrens zfs_dirlock_t *dl; 1951789Sahrens dmu_tx_t *tx; 1952789Sahrens int error; 19535331Samw int zflg = ZEXISTS; 1954789Sahrens 19555367Sahrens ZFS_ENTER(zfsvfs); 19565367Sahrens ZFS_VERIFY_ZP(dzp); 19575326Sek110237 zilog = zfsvfs->z_log; 1958789Sahrens 19595331Samw if (flags & FIGNORECASE) 19605331Samw zflg |= ZCILOOK; 1961789Sahrens top: 1962789Sahrens zp = NULL; 1963789Sahrens 1964789Sahrens /* 1965789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1966789Sahrens */ 19675331Samw if (error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, 19685331Samw NULL, NULL)) { 1969789Sahrens ZFS_EXIT(zfsvfs); 1970789Sahrens return (error); 1971789Sahrens } 1972789Sahrens 1973789Sahrens vp = ZTOV(zp); 1974789Sahrens 1975789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1976789Sahrens goto out; 1977789Sahrens } 1978789Sahrens 1979789Sahrens if (vp->v_type != VDIR) { 1980789Sahrens error = ENOTDIR; 1981789Sahrens goto out; 1982789Sahrens } 1983789Sahrens 1984789Sahrens if (vp == cwd) { 1985789Sahrens error = EINVAL; 1986789Sahrens goto out; 1987789Sahrens } 1988789Sahrens 19895331Samw vnevent_rmdir(vp, dvp, name, ct); 1990789Sahrens 1991789Sahrens /* 19923897Smaybee * Grab a lock on the directory to make sure that noone is 19933897Smaybee * trying to add (or lookup) entries while we are removing it. 19943897Smaybee */ 19953897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 19963897Smaybee 19973897Smaybee /* 19983897Smaybee * Grab a lock on the parent pointer to make sure we play well 1999789Sahrens * with the treewalk and directory rename code. 2000789Sahrens */ 2001789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 2002789Sahrens 2003789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 20041544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 200511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 20063461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 200711935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 200811935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 20098227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 2010789Sahrens if (error) { 2011789Sahrens rw_exit(&zp->z_parent_lock); 20123897Smaybee rw_exit(&zp->z_name_lock); 2013789Sahrens zfs_dirent_unlock(dl); 2014789Sahrens VN_RELE(vp); 20158227SNeil.Perrin@Sun.COM if (error == ERESTART) { 20162113Sahrens dmu_tx_wait(tx); 20172113Sahrens dmu_tx_abort(tx); 2018789Sahrens goto top; 2019789Sahrens } 20202113Sahrens dmu_tx_abort(tx); 2021789Sahrens ZFS_EXIT(zfsvfs); 2022789Sahrens return (error); 2023789Sahrens } 2024789Sahrens 20255331Samw error = zfs_link_destroy(dl, zp, tx, zflg, NULL); 20265331Samw 20275331Samw if (error == 0) { 20285331Samw uint64_t txtype = TX_RMDIR; 20295331Samw if (flags & FIGNORECASE) 20305331Samw txtype |= TX_CI; 203112699SNeil.Perrin@Sun.COM zfs_log_remove(zilog, tx, txtype, dzp, name, ZFS_NO_OBJECT); 20325331Samw } 2033789Sahrens 2034789Sahrens dmu_tx_commit(tx); 2035789Sahrens 2036789Sahrens rw_exit(&zp->z_parent_lock); 20373897Smaybee rw_exit(&zp->z_name_lock); 2038789Sahrens out: 2039789Sahrens zfs_dirent_unlock(dl); 2040789Sahrens 2041789Sahrens VN_RELE(vp); 2042789Sahrens 204312294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 204412699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 204512294SMark.Musante@Sun.COM 2046789Sahrens ZFS_EXIT(zfsvfs); 2047789Sahrens return (error); 2048789Sahrens } 2049789Sahrens 2050789Sahrens /* 2051789Sahrens * Read as many directory entries as will fit into the provided 2052789Sahrens * buffer from the given directory cursor position (specified in 2053789Sahrens * the uio structure. 2054789Sahrens * 2055789Sahrens * IN: vp - vnode of directory to read. 2056789Sahrens * uio - structure supplying read location, range info, 2057789Sahrens * and return buffer. 2058789Sahrens * cr - credentials of caller. 20595331Samw * ct - caller context 20605331Samw * flags - case flags 2061789Sahrens * 2062789Sahrens * OUT: uio - updated offset and range, buffer filled. 2063789Sahrens * eofp - set to true if end-of-file detected. 2064789Sahrens * 2065789Sahrens * RETURN: 0 if success 2066789Sahrens * error code if failure 2067789Sahrens * 2068789Sahrens * Timestamps: 2069789Sahrens * vp - atime updated 2070789Sahrens * 2071789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 2072789Sahrens * This allows us to use the low range for "special" directory entries: 2073789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 2074789Sahrens * we use the offset 2 for the '.zfs' directory. 2075789Sahrens */ 2076789Sahrens /* ARGSUSED */ 2077789Sahrens static int 20785331Samw zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp, 20795331Samw caller_context_t *ct, int flags) 2080789Sahrens { 2081789Sahrens znode_t *zp = VTOZ(vp); 2082789Sahrens iovec_t *iovp; 20835331Samw edirent_t *eodp; 2084789Sahrens dirent64_t *odp; 2085789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2086869Sperrin objset_t *os; 2087789Sahrens caddr_t outbuf; 2088789Sahrens size_t bufsize; 2089789Sahrens zap_cursor_t zc; 2090789Sahrens zap_attribute_t zap; 2091789Sahrens uint_t bytes_wanted; 2092789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 209311935SMark.Shellenbaum@Sun.COM uint64_t parent; 2094789Sahrens int local_eof; 2095869Sperrin int outcount; 2096869Sperrin int error; 2097869Sperrin uint8_t prefetch; 20985663Sck153898 boolean_t check_sysattrs; 2099789Sahrens 21005367Sahrens ZFS_ENTER(zfsvfs); 21015367Sahrens ZFS_VERIFY_ZP(zp); 2102789Sahrens 210311935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 210411935SMark.Shellenbaum@Sun.COM &parent, sizeof (parent))) != 0) { 210511935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 210611935SMark.Shellenbaum@Sun.COM return (error); 210711935SMark.Shellenbaum@Sun.COM } 210811935SMark.Shellenbaum@Sun.COM 2109789Sahrens /* 2110789Sahrens * If we are not given an eof variable, 2111789Sahrens * use a local one. 2112789Sahrens */ 2113789Sahrens if (eofp == NULL) 2114789Sahrens eofp = &local_eof; 2115789Sahrens 2116789Sahrens /* 2117789Sahrens * Check for valid iov_len. 2118789Sahrens */ 2119789Sahrens if (uio->uio_iov->iov_len <= 0) { 2120789Sahrens ZFS_EXIT(zfsvfs); 2121789Sahrens return (EINVAL); 2122789Sahrens } 2123789Sahrens 2124789Sahrens /* 2125789Sahrens * Quit if directory has been removed (posix) 2126789Sahrens */ 21273461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 2128789Sahrens ZFS_EXIT(zfsvfs); 2129789Sahrens return (0); 2130789Sahrens } 2131789Sahrens 2132869Sperrin error = 0; 2133869Sperrin os = zfsvfs->z_os; 2134869Sperrin offset = uio->uio_loffset; 2135869Sperrin prefetch = zp->z_zn_prefetch; 2136869Sperrin 2137789Sahrens /* 2138789Sahrens * Initialize the iterator cursor. 2139789Sahrens */ 2140789Sahrens if (offset <= 3) { 2141789Sahrens /* 2142789Sahrens * Start iteration from the beginning of the directory. 2143789Sahrens */ 2144869Sperrin zap_cursor_init(&zc, os, zp->z_id); 2145789Sahrens } else { 2146789Sahrens /* 2147789Sahrens * The offset is a serialized cursor. 2148789Sahrens */ 2149869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 2150789Sahrens } 2151789Sahrens 2152789Sahrens /* 2153789Sahrens * Get space to change directory entries into fs independent format. 2154789Sahrens */ 2155789Sahrens iovp = uio->uio_iov; 2156789Sahrens bytes_wanted = iovp->iov_len; 2157789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 2158789Sahrens bufsize = bytes_wanted; 2159789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 2160789Sahrens odp = (struct dirent64 *)outbuf; 2161789Sahrens } else { 2162789Sahrens bufsize = bytes_wanted; 2163789Sahrens odp = (struct dirent64 *)iovp->iov_base; 2164789Sahrens } 21655331Samw eodp = (struct edirent *)odp; 2166789Sahrens 2167789Sahrens /* 21687757SJanice.Chang@Sun.COM * If this VFS supports the system attribute view interface; and 21697757SJanice.Chang@Sun.COM * we're looking at an extended attribute directory; and we care 21707757SJanice.Chang@Sun.COM * about normalization conflicts on this vfs; then we must check 21717757SJanice.Chang@Sun.COM * for normalization conflicts with the sysattr name space. 21725663Sck153898 */ 21737757SJanice.Chang@Sun.COM check_sysattrs = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 21745663Sck153898 (vp->v_flag & V_XATTRDIR) && zfsvfs->z_norm && 21755663Sck153898 (flags & V_RDDIR_ENTFLAGS); 21765663Sck153898 21775663Sck153898 /* 2178789Sahrens * Transform to file-system independent format 2179789Sahrens */ 2180789Sahrens outcount = 0; 2181789Sahrens while (outcount < bytes_wanted) { 21823912Slling ino64_t objnum; 21833912Slling ushort_t reclen; 2184*12799STim.Haley@Sun.COM off64_t *next = NULL; 21853912Slling 2186789Sahrens /* 2187789Sahrens * Special case `.', `..', and `.zfs'. 2188789Sahrens */ 2189789Sahrens if (offset == 0) { 2190789Sahrens (void) strcpy(zap.za_name, "."); 21915331Samw zap.za_normalization_conflict = 0; 21923912Slling objnum = zp->z_id; 2193789Sahrens } else if (offset == 1) { 2194789Sahrens (void) strcpy(zap.za_name, ".."); 21955331Samw zap.za_normalization_conflict = 0; 219611935SMark.Shellenbaum@Sun.COM objnum = parent; 2197789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 2198789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 21995331Samw zap.za_normalization_conflict = 0; 22003912Slling objnum = ZFSCTL_INO_ROOT; 2201789Sahrens } else { 2202789Sahrens /* 2203789Sahrens * Grab next entry. 2204789Sahrens */ 2205789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 2206789Sahrens if ((*eofp = (error == ENOENT)) != 0) 2207789Sahrens break; 2208789Sahrens else 2209789Sahrens goto update; 2210789Sahrens } 2211789Sahrens 2212789Sahrens if (zap.za_integer_length != 8 || 2213789Sahrens zap.za_num_integers != 1) { 2214789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 2215789Sahrens "entry, obj = %lld, offset = %lld\n", 2216789Sahrens (u_longlong_t)zp->z_id, 2217789Sahrens (u_longlong_t)offset); 2218789Sahrens error = ENXIO; 2219789Sahrens goto update; 2220789Sahrens } 22213912Slling 22223912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 22233912Slling /* 22243912Slling * MacOS X can extract the object type here such as: 22253912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 22263912Slling */ 22275663Sck153898 22285663Sck153898 if (check_sysattrs && !zap.za_normalization_conflict) { 22295663Sck153898 zap.za_normalization_conflict = 22305663Sck153898 xattr_sysattr_casechk(zap.za_name); 22315663Sck153898 } 2232789Sahrens } 22335331Samw 22349749STim.Haley@Sun.COM if (flags & V_RDDIR_ACCFILTER) { 22359749STim.Haley@Sun.COM /* 22369749STim.Haley@Sun.COM * If we have no access at all, don't include 22379749STim.Haley@Sun.COM * this entry in the returned information 22389749STim.Haley@Sun.COM */ 22399749STim.Haley@Sun.COM znode_t *ezp; 22409749STim.Haley@Sun.COM if (zfs_zget(zp->z_zfsvfs, objnum, &ezp) != 0) 22419749STim.Haley@Sun.COM goto skip_entry; 22429749STim.Haley@Sun.COM if (!zfs_has_access(ezp, cr)) { 22439749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22449749STim.Haley@Sun.COM goto skip_entry; 22459749STim.Haley@Sun.COM } 22469749STim.Haley@Sun.COM VN_RELE(ZTOV(ezp)); 22479749STim.Haley@Sun.COM } 22489749STim.Haley@Sun.COM 22495331Samw if (flags & V_RDDIR_ENTFLAGS) 22505331Samw reclen = EDIRENT_RECLEN(strlen(zap.za_name)); 22515331Samw else 22525331Samw reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 2253789Sahrens 2254789Sahrens /* 2255789Sahrens * Will this entry fit in the buffer? 2256789Sahrens */ 22573912Slling if (outcount + reclen > bufsize) { 2258789Sahrens /* 2259789Sahrens * Did we manage to fit anything in the buffer? 2260789Sahrens */ 2261789Sahrens if (!outcount) { 2262789Sahrens error = EINVAL; 2263789Sahrens goto update; 2264789Sahrens } 2265789Sahrens break; 2266789Sahrens } 22675331Samw if (flags & V_RDDIR_ENTFLAGS) { 22685331Samw /* 22695331Samw * Add extended flag entry: 22705331Samw */ 22715331Samw eodp->ed_ino = objnum; 22725331Samw eodp->ed_reclen = reclen; 22735331Samw /* NOTE: ed_off is the offset for the *next* entry */ 22745331Samw next = &(eodp->ed_off); 22755331Samw eodp->ed_eflags = zap.za_normalization_conflict ? 22765331Samw ED_CASE_CONFLICT : 0; 22775331Samw (void) strncpy(eodp->ed_name, zap.za_name, 22785331Samw EDIRENT_NAMELEN(reclen)); 22795331Samw eodp = (edirent_t *)((intptr_t)eodp + reclen); 22805331Samw } else { 22815331Samw /* 22825331Samw * Add normal entry: 22835331Samw */ 22845331Samw odp->d_ino = objnum; 22855331Samw odp->d_reclen = reclen; 22865331Samw /* NOTE: d_off is the offset for the *next* entry */ 22875331Samw next = &(odp->d_off); 22885331Samw (void) strncpy(odp->d_name, zap.za_name, 22895331Samw DIRENT64_NAMELEN(reclen)); 22905331Samw odp = (dirent64_t *)((intptr_t)odp + reclen); 22915331Samw } 22923912Slling outcount += reclen; 2293789Sahrens 2294789Sahrens ASSERT(outcount <= bufsize); 2295789Sahrens 2296789Sahrens /* Prefetch znode */ 2297869Sperrin if (prefetch) 22983912Slling dmu_prefetch(os, objnum, 0, 0); 2299789Sahrens 23009749STim.Haley@Sun.COM skip_entry: 2301789Sahrens /* 2302789Sahrens * Move to the next entry, fill in the previous offset. 2303789Sahrens */ 2304789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 2305789Sahrens zap_cursor_advance(&zc); 2306789Sahrens offset = zap_cursor_serialize(&zc); 2307789Sahrens } else { 2308789Sahrens offset += 1; 2309789Sahrens } 2310*12799STim.Haley@Sun.COM if (next) 2311*12799STim.Haley@Sun.COM *next = offset; 2312789Sahrens } 2313869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 2314789Sahrens 2315789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 2316789Sahrens iovp->iov_base += outcount; 2317789Sahrens iovp->iov_len -= outcount; 2318789Sahrens uio->uio_resid -= outcount; 2319789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 2320789Sahrens /* 2321789Sahrens * Reset the pointer. 2322789Sahrens */ 2323789Sahrens offset = uio->uio_loffset; 2324789Sahrens } 2325789Sahrens 2326789Sahrens update: 2327885Sahrens zap_cursor_fini(&zc); 2328789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 2329789Sahrens kmem_free(outbuf, bufsize); 2330789Sahrens 2331789Sahrens if (error == ENOENT) 2332789Sahrens error = 0; 2333789Sahrens 2334789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2335789Sahrens 2336789Sahrens uio->uio_loffset = offset; 2337789Sahrens ZFS_EXIT(zfsvfs); 2338789Sahrens return (error); 2339789Sahrens } 2340789Sahrens 23414720Sfr157268 ulong_t zfs_fsync_sync_cnt = 4; 23424720Sfr157268 2343789Sahrens static int 23445331Samw zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr, caller_context_t *ct) 2345789Sahrens { 2346789Sahrens znode_t *zp = VTOZ(vp); 2347789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2348789Sahrens 23491773Seschrock /* 23501773Seschrock * Regardless of whether this is required for standards conformance, 23511773Seschrock * this is the logical behavior when fsync() is called on a file with 23521773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 23531773Seschrock * going to be pushed out as part of the zil_commit(). 23541773Seschrock */ 23551773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 23561773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 23575331Samw (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr, ct); 23581773Seschrock 23594720Sfr157268 (void) tsd_set(zfs_fsyncer_key, (void *)zfs_fsync_sync_cnt); 23604720Sfr157268 236112294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync != ZFS_SYNC_DISABLED) { 236212294SMark.Musante@Sun.COM ZFS_ENTER(zfsvfs); 236312294SMark.Musante@Sun.COM ZFS_VERIFY_ZP(zp); 236412699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 236512294SMark.Musante@Sun.COM ZFS_EXIT(zfsvfs); 236612294SMark.Musante@Sun.COM } 2367789Sahrens return (0); 2368789Sahrens } 2369789Sahrens 23705331Samw 2371789Sahrens /* 2372789Sahrens * Get the requested file attributes and place them in the provided 2373789Sahrens * vattr structure. 2374789Sahrens * 2375789Sahrens * IN: vp - vnode of file. 2376789Sahrens * vap - va_mask identifies requested attributes. 23775331Samw * If AT_XVATTR set, then optional attrs are requested 23785331Samw * flags - ATTR_NOACLCHECK (CIFS server context) 2379789Sahrens * cr - credentials of caller. 23805331Samw * ct - caller context 2381789Sahrens * 2382789Sahrens * OUT: vap - attribute values. 2383789Sahrens * 2384789Sahrens * RETURN: 0 (always succeeds) 2385789Sahrens */ 2386789Sahrens /* ARGSUSED */ 2387789Sahrens static int 23885331Samw zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 23895331Samw caller_context_t *ct) 2390789Sahrens { 2391789Sahrens znode_t *zp = VTOZ(vp); 2392789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 23935331Samw int error = 0; 23944543Smarks uint64_t links; 239511935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 23965331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 23975331Samw xoptattr_t *xoap = NULL; 23985331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 239911935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[2]; 240011935SMark.Shellenbaum@Sun.COM int count = 0; 2401789Sahrens 24025367Sahrens ZFS_ENTER(zfsvfs); 24035367Sahrens ZFS_VERIFY_ZP(zp); 240411935SMark.Shellenbaum@Sun.COM 240511935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, &mtime, 16); 240611935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, &ctime, 16); 240711935SMark.Shellenbaum@Sun.COM 240811935SMark.Shellenbaum@Sun.COM if ((error = sa_bulk_lookup(zp->z_sa_hdl, bulk, count)) != 0) { 240911935SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 241011935SMark.Shellenbaum@Sun.COM return (error); 241111935SMark.Shellenbaum@Sun.COM } 2412789Sahrens 24135331Samw /* 24145331Samw * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 24155331Samw * Also, if we are the owner don't bother, since owner should 24165331Samw * always be allowed to read basic attributes of file. 24175331Samw */ 241811935SMark.Shellenbaum@Sun.COM if (!(zp->z_pflags & ZFS_ACL_TRIVIAL) && (zp->z_uid != crgetuid(cr))) { 24195331Samw if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, 0, 24205331Samw skipaclchk, cr)) { 24215331Samw ZFS_EXIT(zfsvfs); 24225331Samw return (error); 24235331Samw } 24245331Samw } 24255331Samw 2426789Sahrens /* 2427789Sahrens * Return all attributes. It's cheaper to provide the answer 2428789Sahrens * than to determine whether we were asked the question. 2429789Sahrens */ 2430789Sahrens 24319774SRay.Hassan@Sun.COM mutex_enter(&zp->z_lock); 2432789Sahrens vap->va_type = vp->v_type; 243311935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode & MODEMASK; 243411935SMark.Shellenbaum@Sun.COM vap->va_uid = zp->z_uid; 243511935SMark.Shellenbaum@Sun.COM vap->va_gid = zp->z_gid; 2436789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 2437789Sahrens vap->va_nodeid = zp->z_id; 24384543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 243911935SMark.Shellenbaum@Sun.COM links = zp->z_links + 1; 24404543Smarks else 244111935SMark.Shellenbaum@Sun.COM links = zp->z_links; 24424543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 244311935SMark.Shellenbaum@Sun.COM vap->va_size = zp->z_size; 24441816Smarks vap->va_rdev = vp->v_rdev; 2445789Sahrens vap->va_seq = zp->z_seq; 2446789Sahrens 24475331Samw /* 24485331Samw * Add in any requested optional attributes and the create time. 24495331Samw * Also set the corresponding bits in the returned attribute bitmap. 24505331Samw */ 24515331Samw if ((xoap = xva_getxoptattr(xvap)) != NULL && zfsvfs->z_use_fuids) { 24525331Samw if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE)) { 24535331Samw xoap->xoa_archive = 245411935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_ARCHIVE) != 0); 24555331Samw XVA_SET_RTN(xvap, XAT_ARCHIVE); 24565331Samw } 24575331Samw 24585331Samw if (XVA_ISSET_REQ(xvap, XAT_READONLY)) { 24595331Samw xoap->xoa_readonly = 246011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_READONLY) != 0); 24615331Samw XVA_SET_RTN(xvap, XAT_READONLY); 24625331Samw } 24635331Samw 24645331Samw if (XVA_ISSET_REQ(xvap, XAT_SYSTEM)) { 24655331Samw xoap->xoa_system = 246611935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_SYSTEM) != 0); 24675331Samw XVA_SET_RTN(xvap, XAT_SYSTEM); 24685331Samw } 24695331Samw 24705331Samw if (XVA_ISSET_REQ(xvap, XAT_HIDDEN)) { 24715331Samw xoap->xoa_hidden = 247211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_HIDDEN) != 0); 24735331Samw XVA_SET_RTN(xvap, XAT_HIDDEN); 24745331Samw } 24755331Samw 24765331Samw if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 24775331Samw xoap->xoa_nounlink = 247811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0); 24795331Samw XVA_SET_RTN(xvap, XAT_NOUNLINK); 24805331Samw } 24815331Samw 24825331Samw if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 24835331Samw xoap->xoa_immutable = 248411935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0); 24855331Samw XVA_SET_RTN(xvap, XAT_IMMUTABLE); 24865331Samw } 24875331Samw 24885331Samw if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 24895331Samw xoap->xoa_appendonly = 249011935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0); 24915331Samw XVA_SET_RTN(xvap, XAT_APPENDONLY); 24925331Samw } 24935331Samw 24945331Samw if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 24955331Samw xoap->xoa_nodump = 249611935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0); 24975331Samw XVA_SET_RTN(xvap, XAT_NODUMP); 24985331Samw } 24995331Samw 25005331Samw if (XVA_ISSET_REQ(xvap, XAT_OPAQUE)) { 25015331Samw xoap->xoa_opaque = 250211935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_OPAQUE) != 0); 25035331Samw XVA_SET_RTN(xvap, XAT_OPAQUE); 25045331Samw } 25055331Samw 25065331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 25075331Samw xoap->xoa_av_quarantined = 250811935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0); 25095331Samw XVA_SET_RTN(xvap, XAT_AV_QUARANTINED); 25105331Samw } 25115331Samw 25125331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 25135331Samw xoap->xoa_av_modified = 251411935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0); 25155331Samw XVA_SET_RTN(xvap, XAT_AV_MODIFIED); 25165331Samw } 25175331Samw 25185331Samw if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) && 251911935SMark.Shellenbaum@Sun.COM vp->v_type == VREG) { 252011935SMark.Shellenbaum@Sun.COM zfs_sa_get_scanstamp(zp, xvap); 25215331Samw } 25225331Samw 25235331Samw if (XVA_ISSET_REQ(xvap, XAT_CREATETIME)) { 252411935SMark.Shellenbaum@Sun.COM uint64_t times[2]; 252511935SMark.Shellenbaum@Sun.COM 252611935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_CRTIME(zfsvfs), 252711935SMark.Shellenbaum@Sun.COM times, sizeof (times)); 252811935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&xoap->xoa_createtime, times); 25295331Samw XVA_SET_RTN(xvap, XAT_CREATETIME); 25305331Samw } 253110793Sdai.ngo@sun.com 253210793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 253311935SMark.Shellenbaum@Sun.COM xoap->xoa_reparse = ((zp->z_pflags & ZFS_REPARSE) != 0); 253410793Sdai.ngo@sun.com XVA_SET_RTN(xvap, XAT_REPARSE); 253510793Sdai.ngo@sun.com } 25365331Samw } 25375331Samw 253811935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_atime, zp->z_atime); 253911935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_mtime, mtime); 254011935SMark.Shellenbaum@Sun.COM ZFS_TIME_DECODE(&vap->va_ctime, ctime); 2541789Sahrens 2542789Sahrens mutex_exit(&zp->z_lock); 2543789Sahrens 254411935SMark.Shellenbaum@Sun.COM sa_object_size(zp->z_sa_hdl, &vap->va_blksize, &vap->va_nblocks); 2545789Sahrens 2546789Sahrens if (zp->z_blksz == 0) { 2547789Sahrens /* 2548789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 2549789Sahrens */ 2550789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 2551789Sahrens } 2552789Sahrens 2553789Sahrens ZFS_EXIT(zfsvfs); 2554789Sahrens return (0); 2555789Sahrens } 2556789Sahrens 2557789Sahrens /* 2558789Sahrens * Set the file attributes to the values contained in the 2559789Sahrens * vattr structure. 2560789Sahrens * 2561789Sahrens * IN: vp - vnode of file to be modified. 2562789Sahrens * vap - new attribute values. 25635331Samw * If AT_XVATTR set, then optional attrs are being set 2564789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 25655331Samw * - ATTR_NOACLCHECK (CIFS context only). 2566789Sahrens * cr - credentials of caller. 25675331Samw * ct - caller context 2568789Sahrens * 2569789Sahrens * RETURN: 0 if success 2570789Sahrens * error code if failure 2571789Sahrens * 2572789Sahrens * Timestamps: 2573789Sahrens * vp - ctime updated, mtime updated if size changed. 2574789Sahrens */ 2575789Sahrens /* ARGSUSED */ 2576789Sahrens static int 2577789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 2578789Sahrens caller_context_t *ct) 2579789Sahrens { 25805326Sek110237 znode_t *zp = VTOZ(vp); 2581789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 25825326Sek110237 zilog_t *zilog; 2583789Sahrens dmu_tx_t *tx; 25841878Smaybee vattr_t oldva; 25858190SMark.Shellenbaum@Sun.COM xvattr_t tmpxvattr; 2586789Sahrens uint_t mask = vap->va_mask; 25871878Smaybee uint_t saved_mask; 25882796Smarks int trim_mask = 0; 2589789Sahrens uint64_t new_mode; 25909179SMark.Shellenbaum@Sun.COM uint64_t new_uid, new_gid; 259111935SMark.Shellenbaum@Sun.COM uint64_t xattr_obj = 0; 259211935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 25931231Smarks znode_t *attrzp; 2594789Sahrens int need_policy = FALSE; 259511935SMark.Shellenbaum@Sun.COM int err, err2; 25965331Samw zfs_fuid_info_t *fuidp = NULL; 25975331Samw xvattr_t *xvap = (xvattr_t *)vap; /* vap may be an xvattr_t * */ 25985331Samw xoptattr_t *xoap; 25995824Smarks zfs_acl_t *aclp = NULL; 26005331Samw boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 260111935SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied = B_FALSE; 260211935SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[7], xattr_bulk[7]; 260311935SMark.Shellenbaum@Sun.COM int count = 0, xattr_count = 0; 2604789Sahrens 2605789Sahrens if (mask == 0) 2606789Sahrens return (0); 2607789Sahrens 2608789Sahrens if (mask & AT_NOSET) 2609789Sahrens return (EINVAL); 2610789Sahrens 26115367Sahrens ZFS_ENTER(zfsvfs); 26125367Sahrens ZFS_VERIFY_ZP(zp); 26135331Samw 26145331Samw zilog = zfsvfs->z_log; 26155331Samw 26165331Samw /* 26175331Samw * Make sure that if we have ephemeral uid/gid or xvattr specified 26185331Samw * that file system is at proper version level 26195331Samw */ 26205331Samw 26215331Samw if (zfsvfs->z_use_fuids == B_FALSE && 26225331Samw (((mask & AT_UID) && IS_EPHEMERAL(vap->va_uid)) || 26235331Samw ((mask & AT_GID) && IS_EPHEMERAL(vap->va_gid)) || 26245386Stimh (mask & AT_XVATTR))) { 26255386Stimh ZFS_EXIT(zfsvfs); 26265331Samw return (EINVAL); 26275386Stimh } 26285386Stimh 26295386Stimh if (mask & AT_SIZE && vp->v_type == VDIR) { 26305386Stimh ZFS_EXIT(zfsvfs); 2631789Sahrens return (EISDIR); 26325386Stimh } 26335386Stimh 26345386Stimh if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) { 26355386Stimh ZFS_EXIT(zfsvfs); 26361308Smarks return (EINVAL); 26375386Stimh } 26381308Smarks 26395331Samw /* 26405331Samw * If this is an xvattr_t, then get a pointer to the structure of 26415331Samw * optional attributes. If this is NULL, then we have a vattr_t. 26425331Samw */ 26435331Samw xoap = xva_getxoptattr(xvap); 26445331Samw 26458190SMark.Shellenbaum@Sun.COM xva_init(&tmpxvattr); 26468190SMark.Shellenbaum@Sun.COM 26475331Samw /* 26485331Samw * Immutable files can only alter immutable bit and atime 26495331Samw */ 265011935SMark.Shellenbaum@Sun.COM if ((zp->z_pflags & ZFS_IMMUTABLE) && 26515331Samw ((mask & (AT_SIZE|AT_UID|AT_GID|AT_MTIME|AT_MODE)) || 26525386Stimh ((mask & AT_XVATTR) && XVA_ISSET_REQ(xvap, XAT_CREATETIME)))) { 26535386Stimh ZFS_EXIT(zfsvfs); 26545331Samw return (EPERM); 26555386Stimh } 26565386Stimh 265711935SMark.Shellenbaum@Sun.COM if ((mask & AT_SIZE) && (zp->z_pflags & ZFS_READONLY)) { 26585386Stimh ZFS_EXIT(zfsvfs); 26595331Samw return (EPERM); 26605386Stimh } 2661789Sahrens 26626064Smarks /* 26636064Smarks * Verify timestamps doesn't overflow 32 bits. 26646064Smarks * ZFS can handle large timestamps, but 32bit syscalls can't 26656064Smarks * handle times greater than 2039. This check should be removed 26666064Smarks * once large timestamps are fully supported. 26676064Smarks */ 26686064Smarks if (mask & (AT_ATIME | AT_MTIME)) { 26696064Smarks if (((mask & AT_ATIME) && TIMESPEC_OVERFLOW(&vap->va_atime)) || 26706064Smarks ((mask & AT_MTIME) && TIMESPEC_OVERFLOW(&vap->va_mtime))) { 26716064Smarks ZFS_EXIT(zfsvfs); 26726064Smarks return (EOVERFLOW); 26736064Smarks } 26746064Smarks } 26756064Smarks 2676789Sahrens top: 26771231Smarks attrzp = NULL; 2678789Sahrens 26799981STim.Haley@Sun.COM /* Can this be moved to before the top label? */ 2680789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 2681789Sahrens ZFS_EXIT(zfsvfs); 2682789Sahrens return (EROFS); 2683789Sahrens } 2684789Sahrens 2685789Sahrens /* 2686789Sahrens * First validate permissions 2687789Sahrens */ 2688789Sahrens 2689789Sahrens if (mask & AT_SIZE) { 26905331Samw err = zfs_zaccess(zp, ACE_WRITE_DATA, 0, skipaclchk, cr); 2691789Sahrens if (err) { 2692789Sahrens ZFS_EXIT(zfsvfs); 2693789Sahrens return (err); 2694789Sahrens } 26951878Smaybee /* 26961878Smaybee * XXX - Note, we are not providing any open 26971878Smaybee * mode flags here (like FNDELAY), so we may 26981878Smaybee * block if there are locks present... this 26991878Smaybee * should be addressed in openat(). 27001878Smaybee */ 27016992Smaybee /* XXX - would it be OK to generate a log record here? */ 27026992Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 27031878Smaybee if (err) { 27041878Smaybee ZFS_EXIT(zfsvfs); 27051878Smaybee return (err); 27061878Smaybee } 2707789Sahrens } 2708789Sahrens 27095331Samw if (mask & (AT_ATIME|AT_MTIME) || 27105331Samw ((mask & AT_XVATTR) && (XVA_ISSET_REQ(xvap, XAT_HIDDEN) || 27115331Samw XVA_ISSET_REQ(xvap, XAT_READONLY) || 27125331Samw XVA_ISSET_REQ(xvap, XAT_ARCHIVE) || 27135331Samw XVA_ISSET_REQ(xvap, XAT_CREATETIME) || 271411935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_SYSTEM)))) { 27155331Samw need_policy = zfs_zaccess(zp, ACE_WRITE_ATTRIBUTES, 0, 27165331Samw skipaclchk, cr); 271711935SMark.Shellenbaum@Sun.COM } 2718789Sahrens 2719789Sahrens if (mask & (AT_UID|AT_GID)) { 2720789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2721789Sahrens int take_owner; 2722789Sahrens int take_group; 2723789Sahrens 2724789Sahrens /* 2725913Smarks * NOTE: even if a new mode is being set, 2726913Smarks * we may clear S_ISUID/S_ISGID bits. 2727913Smarks */ 2728913Smarks 2729913Smarks if (!(mask & AT_MODE)) 273011935SMark.Shellenbaum@Sun.COM vap->va_mode = zp->z_mode; 2731913Smarks 2732913Smarks /* 2733789Sahrens * Take ownership or chgrp to group we are a member of 2734789Sahrens */ 2735789Sahrens 2736789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 27375331Samw take_group = (mask & AT_GID) && 27385331Samw zfs_groupmember(zfsvfs, vap->va_gid, cr); 2739789Sahrens 2740789Sahrens /* 2741789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2742789Sahrens * take_group must both be set in order to allow taking 2743789Sahrens * ownership. 2744789Sahrens * 2745789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2746789Sahrens * 2747789Sahrens */ 2748789Sahrens 2749789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2750789Sahrens ((idmask == AT_UID) && take_owner) || 2751789Sahrens ((idmask == AT_GID) && take_group)) { 27525331Samw if (zfs_zaccess(zp, ACE_WRITE_OWNER, 0, 27535331Samw skipaclchk, cr) == 0) { 2754789Sahrens /* 2755789Sahrens * Remove setuid/setgid for non-privileged users 2756789Sahrens */ 27571115Smarks secpolicy_setid_clear(vap, cr); 27582796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2759789Sahrens } else { 2760789Sahrens need_policy = TRUE; 2761789Sahrens } 2762789Sahrens } else { 2763789Sahrens need_policy = TRUE; 2764789Sahrens } 2765789Sahrens } 2766789Sahrens 27672796Smarks mutex_enter(&zp->z_lock); 276811935SMark.Shellenbaum@Sun.COM oldva.va_mode = zp->z_mode; 276911935SMark.Shellenbaum@Sun.COM oldva.va_uid = zp->z_uid; 277011935SMark.Shellenbaum@Sun.COM oldva.va_gid = zp->z_gid; 27715331Samw if (mask & AT_XVATTR) { 27728190SMark.Shellenbaum@Sun.COM /* 27738190SMark.Shellenbaum@Sun.COM * Update xvattr mask to include only those attributes 27748190SMark.Shellenbaum@Sun.COM * that are actually changing. 27758190SMark.Shellenbaum@Sun.COM * 27768190SMark.Shellenbaum@Sun.COM * the bits will be restored prior to actually setting 27778190SMark.Shellenbaum@Sun.COM * the attributes so the caller thinks they were set. 27788190SMark.Shellenbaum@Sun.COM */ 27798190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_APPENDONLY)) { 27808190SMark.Shellenbaum@Sun.COM if (xoap->xoa_appendonly != 278111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_APPENDONLY) != 0)) { 27828190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27838190SMark.Shellenbaum@Sun.COM } else { 27848190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_APPENDONLY); 27858190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_APPENDONLY); 27868190SMark.Shellenbaum@Sun.COM } 27878190SMark.Shellenbaum@Sun.COM } 27888190SMark.Shellenbaum@Sun.COM 27898190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NOUNLINK)) { 27908190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nounlink != 279111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NOUNLINK) != 0)) { 27928190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 27938190SMark.Shellenbaum@Sun.COM } else { 27948190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NOUNLINK); 27958190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NOUNLINK); 27968190SMark.Shellenbaum@Sun.COM } 27978190SMark.Shellenbaum@Sun.COM } 27988190SMark.Shellenbaum@Sun.COM 27998190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE)) { 28008190SMark.Shellenbaum@Sun.COM if (xoap->xoa_immutable != 280111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_IMMUTABLE) != 0)) { 28028190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28038190SMark.Shellenbaum@Sun.COM } else { 28048190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_IMMUTABLE); 28058190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_IMMUTABLE); 28068190SMark.Shellenbaum@Sun.COM } 28078190SMark.Shellenbaum@Sun.COM } 28088190SMark.Shellenbaum@Sun.COM 28098190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_NODUMP)) { 28108190SMark.Shellenbaum@Sun.COM if (xoap->xoa_nodump != 281111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_NODUMP) != 0)) { 28128190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28138190SMark.Shellenbaum@Sun.COM } else { 28148190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_NODUMP); 28158190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_NODUMP); 28168190SMark.Shellenbaum@Sun.COM } 28178190SMark.Shellenbaum@Sun.COM } 28188190SMark.Shellenbaum@Sun.COM 28198190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED)) { 28208190SMark.Shellenbaum@Sun.COM if (xoap->xoa_av_modified != 282111935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_MODIFIED) != 0)) { 28228190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28238190SMark.Shellenbaum@Sun.COM } else { 28248190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_MODIFIED); 28258190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_MODIFIED); 28268190SMark.Shellenbaum@Sun.COM } 28278190SMark.Shellenbaum@Sun.COM } 28288190SMark.Shellenbaum@Sun.COM 28298190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) { 28308190SMark.Shellenbaum@Sun.COM if ((vp->v_type != VREG && 28318190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined) || 28328190SMark.Shellenbaum@Sun.COM xoap->xoa_av_quarantined != 283311935SMark.Shellenbaum@Sun.COM ((zp->z_pflags & ZFS_AV_QUARANTINED) != 0)) { 28348190SMark.Shellenbaum@Sun.COM need_policy = TRUE; 28358190SMark.Shellenbaum@Sun.COM } else { 28368190SMark.Shellenbaum@Sun.COM XVA_CLR_REQ(xvap, XAT_AV_QUARANTINED); 28378190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(&tmpxvattr, XAT_AV_QUARANTINED); 28388190SMark.Shellenbaum@Sun.COM } 28398190SMark.Shellenbaum@Sun.COM } 28408190SMark.Shellenbaum@Sun.COM 284110793Sdai.ngo@sun.com if (XVA_ISSET_REQ(xvap, XAT_REPARSE)) { 284210793Sdai.ngo@sun.com mutex_exit(&zp->z_lock); 284310793Sdai.ngo@sun.com ZFS_EXIT(zfsvfs); 284410793Sdai.ngo@sun.com return (EPERM); 284510793Sdai.ngo@sun.com } 284610793Sdai.ngo@sun.com 28478190SMark.Shellenbaum@Sun.COM if (need_policy == FALSE && 28488190SMark.Shellenbaum@Sun.COM (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP) || 28498190SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_OPAQUE))) { 28505331Samw need_policy = TRUE; 28515331Samw } 28525331Samw } 28535331Samw 28542796Smarks mutex_exit(&zp->z_lock); 28552796Smarks 28562796Smarks if (mask & AT_MODE) { 28575331Samw if (zfs_zaccess(zp, ACE_WRITE_ACL, 0, skipaclchk, cr) == 0) { 28582796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 28592796Smarks &oldva, cr); 28602796Smarks if (err) { 28612796Smarks ZFS_EXIT(zfsvfs); 28622796Smarks return (err); 28632796Smarks } 28642796Smarks trim_mask |= AT_MODE; 28652796Smarks } else { 28662796Smarks need_policy = TRUE; 28672796Smarks } 28682796Smarks } 2869789Sahrens 2870789Sahrens if (need_policy) { 28711115Smarks /* 28721115Smarks * If trim_mask is set then take ownership 28732796Smarks * has been granted or write_acl is present and user 28742796Smarks * has the ability to modify mode. In that case remove 28752796Smarks * UID|GID and or MODE from mask so that 28761115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 28771115Smarks */ 28782796Smarks 28792796Smarks if (trim_mask) { 28802796Smarks saved_mask = vap->va_mask; 28812796Smarks vap->va_mask &= ~trim_mask; 28822796Smarks } 2883789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 28845331Samw (int (*)(void *, int, cred_t *))zfs_zaccess_unix, zp); 2885789Sahrens if (err) { 2886789Sahrens ZFS_EXIT(zfsvfs); 2887789Sahrens return (err); 2888789Sahrens } 28891115Smarks 28901115Smarks if (trim_mask) 28912796Smarks vap->va_mask |= saved_mask; 2892789Sahrens } 2893789Sahrens 2894789Sahrens /* 2895789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2896789Sahrens * changed va_mask 2897789Sahrens */ 2898789Sahrens mask = vap->va_mask; 2899789Sahrens 290011935SMark.Shellenbaum@Sun.COM if ((mask & (AT_UID | AT_GID))) { 290111935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_XATTR(zfsvfs), &xattr_obj, 290211935SMark.Shellenbaum@Sun.COM sizeof (xattr_obj)); 290311935SMark.Shellenbaum@Sun.COM 290411935SMark.Shellenbaum@Sun.COM if (xattr_obj) { 290511935SMark.Shellenbaum@Sun.COM err = zfs_zget(zp->z_zfsvfs, xattr_obj, &attrzp); 29069396SMatthew.Ahrens@Sun.COM if (err) 290711935SMark.Shellenbaum@Sun.COM goto out2; 29089179SMark.Shellenbaum@Sun.COM } 29099179SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 29109179SMark.Shellenbaum@Sun.COM new_uid = zfs_fuid_create(zfsvfs, 29119179SMark.Shellenbaum@Sun.COM (uint64_t)vap->va_uid, cr, ZFS_OWNER, &fuidp); 291211935SMark.Shellenbaum@Sun.COM if (vap->va_uid != zp->z_uid && 291311935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_FALSE, new_uid)) { 29149396SMatthew.Ahrens@Sun.COM err = EDQUOT; 291511935SMark.Shellenbaum@Sun.COM goto out2; 29169396SMatthew.Ahrens@Sun.COM } 29171231Smarks } 29189396SMatthew.Ahrens@Sun.COM 29199179SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 29209179SMark.Shellenbaum@Sun.COM new_gid = zfs_fuid_create(zfsvfs, (uint64_t)vap->va_gid, 29219179SMark.Shellenbaum@Sun.COM cr, ZFS_GROUP, &fuidp); 292211935SMark.Shellenbaum@Sun.COM if (new_gid != zp->z_gid && 292311935SMark.Shellenbaum@Sun.COM zfs_fuid_overquota(zfsvfs, B_TRUE, new_gid)) { 29249396SMatthew.Ahrens@Sun.COM err = EDQUOT; 292511935SMark.Shellenbaum@Sun.COM goto out2; 29269179SMark.Shellenbaum@Sun.COM } 29279179SMark.Shellenbaum@Sun.COM } 29281231Smarks } 292911935SMark.Shellenbaum@Sun.COM tx = dmu_tx_create(zfsvfs->z_os); 293011935SMark.Shellenbaum@Sun.COM 293111935SMark.Shellenbaum@Sun.COM if (mask & AT_MODE) { 293211935SMark.Shellenbaum@Sun.COM uint64_t pmode = zp->z_mode; 293312620SMark.Shellenbaum@Oracle.COM uint64_t acl_obj; 293411935SMark.Shellenbaum@Sun.COM new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 293511935SMark.Shellenbaum@Sun.COM 293611935SMark.Shellenbaum@Sun.COM if (err = zfs_acl_chmod_setattr(zp, &aclp, new_mode)) 293711935SMark.Shellenbaum@Sun.COM goto out; 293811935SMark.Shellenbaum@Sun.COM 293912620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 294012620SMark.Shellenbaum@Oracle.COM if (!zp->z_is_sa && ((acl_obj = zfs_external_acl(zp)) != 0)) { 294111935SMark.Shellenbaum@Sun.COM /* 294211935SMark.Shellenbaum@Sun.COM * Are we upgrading ACL from old V0 format 294311935SMark.Shellenbaum@Sun.COM * to V1 format? 294411935SMark.Shellenbaum@Sun.COM */ 294511935SMark.Shellenbaum@Sun.COM if (zfsvfs->z_version <= ZPL_VERSION_FUID && 294612620SMark.Shellenbaum@Oracle.COM zfs_znode_acl_version(zp) == 294711935SMark.Shellenbaum@Sun.COM ZFS_ACL_VERSION_INITIAL) { 294812620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_free(tx, acl_obj, 0, 294911935SMark.Shellenbaum@Sun.COM DMU_OBJECT_END); 295011935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 295111935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 295211935SMark.Shellenbaum@Sun.COM } else { 295312620SMark.Shellenbaum@Oracle.COM dmu_tx_hold_write(tx, acl_obj, 0, 295411935SMark.Shellenbaum@Sun.COM aclp->z_acl_bytes); 295511935SMark.Shellenbaum@Sun.COM } 295611935SMark.Shellenbaum@Sun.COM } else if (!zp->z_is_sa && aclp->z_acl_bytes > ZFS_ACE_SPACE) { 295711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 295811935SMark.Shellenbaum@Sun.COM 0, aclp->z_acl_bytes); 295911935SMark.Shellenbaum@Sun.COM } 296012620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 296111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 296211935SMark.Shellenbaum@Sun.COM } else { 296311935SMark.Shellenbaum@Sun.COM if ((mask & AT_XVATTR) && 296411935SMark.Shellenbaum@Sun.COM XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 296511935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_TRUE); 296611935SMark.Shellenbaum@Sun.COM else 296711935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 296811935SMark.Shellenbaum@Sun.COM } 296911935SMark.Shellenbaum@Sun.COM 297011935SMark.Shellenbaum@Sun.COM if (attrzp) { 297111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, attrzp->z_sa_hdl, B_FALSE); 297211935SMark.Shellenbaum@Sun.COM } 297311935SMark.Shellenbaum@Sun.COM 297411935SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 297511935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 297611935SMark.Shellenbaum@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 297711935SMark.Shellenbaum@Sun.COM 297811935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 29791231Smarks 29808227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 2981789Sahrens if (err) { 29829396SMatthew.Ahrens@Sun.COM if (err == ERESTART) 29832113Sahrens dmu_tx_wait(tx); 29849396SMatthew.Ahrens@Sun.COM goto out; 2985789Sahrens } 2986789Sahrens 298711935SMark.Shellenbaum@Sun.COM count = 0; 2988789Sahrens /* 2989789Sahrens * Set each attribute requested. 2990789Sahrens * We group settings according to the locks they need to acquire. 2991789Sahrens * 2992789Sahrens * Note: you cannot set ctime directly, although it will be 2993789Sahrens * updated as a side-effect of calling this function. 2994789Sahrens */ 2995789Sahrens 299612620SMark.Shellenbaum@Oracle.COM 299712620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 299812620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_acl_lock); 2999789Sahrens mutex_enter(&zp->z_lock); 3000789Sahrens 300112394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 300212394SMark.Shellenbaum@Sun.COM &zp->z_pflags, sizeof (zp->z_pflags)); 300312394SMark.Shellenbaum@Sun.COM 300412394SMark.Shellenbaum@Sun.COM if (attrzp) { 300512620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 300612620SMark.Shellenbaum@Oracle.COM mutex_enter(&attrzp->z_acl_lock); 300711935SMark.Shellenbaum@Sun.COM mutex_enter(&attrzp->z_lock); 300812394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 300912394SMark.Shellenbaum@Sun.COM SA_ZPL_FLAGS(zfsvfs), NULL, &attrzp->z_pflags, 301012394SMark.Shellenbaum@Sun.COM sizeof (attrzp->z_pflags)); 301112394SMark.Shellenbaum@Sun.COM } 301211935SMark.Shellenbaum@Sun.COM 301312164SMark.Shellenbaum@Sun.COM if (mask & (AT_UID|AT_GID)) { 301412164SMark.Shellenbaum@Sun.COM 301512164SMark.Shellenbaum@Sun.COM if (mask & AT_UID) { 301612164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_UID(zfsvfs), NULL, 301712164SMark.Shellenbaum@Sun.COM &new_uid, sizeof (new_uid)); 301812164SMark.Shellenbaum@Sun.COM zp->z_uid = zfs_fuid_map_id(zfsvfs, new_uid, 301912164SMark.Shellenbaum@Sun.COM cr, ZFS_OWNER); 302012164SMark.Shellenbaum@Sun.COM if (attrzp) { 302112164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 302212164SMark.Shellenbaum@Sun.COM SA_ZPL_UID(zfsvfs), NULL, &new_uid, 302312164SMark.Shellenbaum@Sun.COM sizeof (new_uid)); 302412394SMark.Shellenbaum@Sun.COM attrzp->z_uid = zp->z_uid; 302512164SMark.Shellenbaum@Sun.COM } 302611935SMark.Shellenbaum@Sun.COM } 302712164SMark.Shellenbaum@Sun.COM 302812164SMark.Shellenbaum@Sun.COM if (mask & AT_GID) { 302912164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_GID(zfsvfs), 303012164SMark.Shellenbaum@Sun.COM NULL, &new_gid, sizeof (new_gid)); 303112164SMark.Shellenbaum@Sun.COM zp->z_gid = zfs_fuid_map_id(zfsvfs, new_gid, cr, 303212164SMark.Shellenbaum@Sun.COM ZFS_GROUP); 303312164SMark.Shellenbaum@Sun.COM if (attrzp) { 303412164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 303512164SMark.Shellenbaum@Sun.COM SA_ZPL_GID(zfsvfs), NULL, &new_gid, 303612164SMark.Shellenbaum@Sun.COM sizeof (new_gid)); 303712164SMark.Shellenbaum@Sun.COM attrzp->z_gid = zp->z_gid; 303812164SMark.Shellenbaum@Sun.COM } 303912164SMark.Shellenbaum@Sun.COM } 304012164SMark.Shellenbaum@Sun.COM if (!(mask & AT_MODE)) { 304112164SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), 304212164SMark.Shellenbaum@Sun.COM NULL, &new_mode, sizeof (new_mode)); 304312164SMark.Shellenbaum@Sun.COM new_mode = zp->z_mode; 304412164SMark.Shellenbaum@Sun.COM } 304512164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(zp); 304612164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 304711935SMark.Shellenbaum@Sun.COM if (attrzp) { 304812164SMark.Shellenbaum@Sun.COM err = zfs_acl_chown_setattr(attrzp); 304912164SMark.Shellenbaum@Sun.COM ASSERT(err == 0); 305011935SMark.Shellenbaum@Sun.COM } 305111935SMark.Shellenbaum@Sun.COM } 305211935SMark.Shellenbaum@Sun.COM 3053789Sahrens if (mask & AT_MODE) { 305411935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MODE(zfsvfs), NULL, 305511935SMark.Shellenbaum@Sun.COM &new_mode, sizeof (new_mode)); 305611935SMark.Shellenbaum@Sun.COM zp->z_mode = new_mode; 305712164SMark.Shellenbaum@Sun.COM ASSERT3U((uintptr_t)aclp, !=, NULL); 30589179SMark.Shellenbaum@Sun.COM err = zfs_aclset_common(zp, aclp, cr, tx); 3059789Sahrens ASSERT3U(err, ==, 0); 306010143STim.Haley@Sun.COM zp->z_acl_cached = aclp; 306110143STim.Haley@Sun.COM aclp = NULL; 3062789Sahrens } 3063789Sahrens 306411935SMark.Shellenbaum@Sun.COM 306511935SMark.Shellenbaum@Sun.COM if (mask & AT_ATIME) { 306611935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_atime, zp->z_atime); 306711935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_ATIME(zfsvfs), NULL, 306811935SMark.Shellenbaum@Sun.COM &zp->z_atime, sizeof (zp->z_atime)); 30691231Smarks } 30701231Smarks 307111935SMark.Shellenbaum@Sun.COM if (mask & AT_MTIME) { 307211935SMark.Shellenbaum@Sun.COM ZFS_TIME_ENCODE(&vap->va_mtime, mtime); 307311935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 307411935SMark.Shellenbaum@Sun.COM mtime, sizeof (mtime)); 30751231Smarks } 30761231Smarks 30776992Smaybee /* XXX - shouldn't this be done *before* the ATIME/MTIME checks? */ 307811935SMark.Shellenbaum@Sun.COM if (mask & AT_SIZE && !(mask & AT_MTIME)) { 307912394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), 308012394SMark.Shellenbaum@Sun.COM NULL, mtime, sizeof (mtime)); 308111935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 308211935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 308311935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 308411935SMark.Shellenbaum@Sun.COM B_TRUE); 308511935SMark.Shellenbaum@Sun.COM } else if (mask != 0) { 308611935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 308711935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 308811935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, STATE_CHANGED, mtime, ctime, 308911935SMark.Shellenbaum@Sun.COM B_TRUE); 309011935SMark.Shellenbaum@Sun.COM if (attrzp) { 309111935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(xattr_bulk, xattr_count, 309211935SMark.Shellenbaum@Sun.COM SA_ZPL_CTIME(zfsvfs), NULL, 309311935SMark.Shellenbaum@Sun.COM &ctime, sizeof (ctime)); 309411935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(attrzp, STATE_CHANGED, 309511935SMark.Shellenbaum@Sun.COM mtime, ctime, B_TRUE); 309611935SMark.Shellenbaum@Sun.COM } 309711935SMark.Shellenbaum@Sun.COM } 30985331Samw /* 30995331Samw * Do this after setting timestamps to prevent timestamp 31005331Samw * update from toggling bit 31015331Samw */ 31025331Samw 31035331Samw if (xoap && (mask & AT_XVATTR)) { 31048190SMark.Shellenbaum@Sun.COM 31058190SMark.Shellenbaum@Sun.COM /* 31068190SMark.Shellenbaum@Sun.COM * restore trimmed off masks 31078190SMark.Shellenbaum@Sun.COM * so that return masks can be set for caller. 31088190SMark.Shellenbaum@Sun.COM */ 31098190SMark.Shellenbaum@Sun.COM 31108190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_APPENDONLY)) { 31118190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_APPENDONLY); 31128190SMark.Shellenbaum@Sun.COM } 31138190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NOUNLINK)) { 31148190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NOUNLINK); 31158190SMark.Shellenbaum@Sun.COM } 31168190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_IMMUTABLE)) { 31178190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_IMMUTABLE); 31188190SMark.Shellenbaum@Sun.COM } 31198190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_NODUMP)) { 31208190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_NODUMP); 31218190SMark.Shellenbaum@Sun.COM } 31228190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_MODIFIED)) { 31238190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_MODIFIED); 31248190SMark.Shellenbaum@Sun.COM } 31258190SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(&tmpxvattr, XAT_AV_QUARANTINED)) { 31268190SMark.Shellenbaum@Sun.COM XVA_SET_REQ(xvap, XAT_AV_QUARANTINED); 31278190SMark.Shellenbaum@Sun.COM } 31288190SMark.Shellenbaum@Sun.COM 312911935SMark.Shellenbaum@Sun.COM if (XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) 31305331Samw ASSERT(vp->v_type == VREG); 31315331Samw 313211935SMark.Shellenbaum@Sun.COM zfs_xvattr_set(zp, xvap, tx); 31335331Samw } 3134789Sahrens 31359179SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 31369179SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 31379179SMark.Shellenbaum@Sun.COM 31381878Smaybee if (mask != 0) 31395331Samw zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask, fuidp); 31405331Samw 3141789Sahrens mutex_exit(&zp->z_lock); 314212620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 314312620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_acl_lock); 314412620SMark.Shellenbaum@Oracle.COM 314512620SMark.Shellenbaum@Oracle.COM if (attrzp) { 314612620SMark.Shellenbaum@Oracle.COM if (mask & (AT_UID|AT_GID|AT_MODE)) 314712620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_acl_lock); 314812620SMark.Shellenbaum@Oracle.COM mutex_exit(&attrzp->z_lock); 314912620SMark.Shellenbaum@Oracle.COM } 31509396SMatthew.Ahrens@Sun.COM out: 315111935SMark.Shellenbaum@Sun.COM if (err == 0 && attrzp) { 315211935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(attrzp->z_sa_hdl, xattr_bulk, 315311935SMark.Shellenbaum@Sun.COM xattr_count, tx); 315411935SMark.Shellenbaum@Sun.COM ASSERT(err2 == 0); 315511935SMark.Shellenbaum@Sun.COM } 315611935SMark.Shellenbaum@Sun.COM 31571231Smarks if (attrzp) 31581231Smarks VN_RELE(ZTOV(attrzp)); 315910143STim.Haley@Sun.COM if (aclp) 316010143STim.Haley@Sun.COM zfs_acl_free(aclp); 316110143STim.Haley@Sun.COM 31629396SMatthew.Ahrens@Sun.COM if (fuidp) { 31639396SMatthew.Ahrens@Sun.COM zfs_fuid_info_free(fuidp); 31649396SMatthew.Ahrens@Sun.COM fuidp = NULL; 31659396SMatthew.Ahrens@Sun.COM } 31669396SMatthew.Ahrens@Sun.COM 316711935SMark.Shellenbaum@Sun.COM if (err) { 31689396SMatthew.Ahrens@Sun.COM dmu_tx_abort(tx); 316911935SMark.Shellenbaum@Sun.COM if (err == ERESTART) 317011935SMark.Shellenbaum@Sun.COM goto top; 317111935SMark.Shellenbaum@Sun.COM } else { 317211935SMark.Shellenbaum@Sun.COM err2 = sa_bulk_update(zp->z_sa_hdl, bulk, count, tx); 31739396SMatthew.Ahrens@Sun.COM dmu_tx_commit(tx); 317411935SMark.Shellenbaum@Sun.COM } 317511935SMark.Shellenbaum@Sun.COM 317611935SMark.Shellenbaum@Sun.COM 317711935SMark.Shellenbaum@Sun.COM out2: 317812294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 317912699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 318012294SMark.Musante@Sun.COM 3181789Sahrens ZFS_EXIT(zfsvfs); 3182789Sahrens return (err); 3183789Sahrens } 3184789Sahrens 31853271Smaybee typedef struct zfs_zlock { 31863271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 31873271Smaybee znode_t *zl_znode; /* znode we held */ 31883271Smaybee struct zfs_zlock *zl_next; /* next in list */ 31893271Smaybee } zfs_zlock_t; 31903271Smaybee 31913271Smaybee /* 31923271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 31933271Smaybee */ 31943271Smaybee static void 31953271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 31963271Smaybee { 31973271Smaybee zfs_zlock_t *zl; 31983271Smaybee 31993271Smaybee while ((zl = *zlpp) != NULL) { 32003271Smaybee if (zl->zl_znode != NULL) 32013271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 32023271Smaybee rw_exit(zl->zl_rwlock); 32033271Smaybee *zlpp = zl->zl_next; 32043271Smaybee kmem_free(zl, sizeof (*zl)); 32053271Smaybee } 32063271Smaybee } 32073271Smaybee 3208789Sahrens /* 3209789Sahrens * Search back through the directory tree, using the ".." entries. 3210789Sahrens * Lock each directory in the chain to prevent concurrent renames. 3211789Sahrens * Fail any attempt to move a directory into one of its own descendants. 3212789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 3213789Sahrens */ 3214789Sahrens static int 3215789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 3216789Sahrens { 3217789Sahrens zfs_zlock_t *zl; 32183638Sbillm znode_t *zp = tdzp; 3219789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 322011935SMark.Shellenbaum@Sun.COM uint64_t oidp = zp->z_id; 3221789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 3222789Sahrens krw_t rw = RW_WRITER; 3223789Sahrens 3224789Sahrens /* 3225789Sahrens * First pass write-locks szp and compares to zp->z_id. 3226789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 3227789Sahrens */ 3228789Sahrens do { 32293271Smaybee if (!rw_tryenter(rwlp, rw)) { 32303271Smaybee /* 32313271Smaybee * Another thread is renaming in this path. 32323271Smaybee * Note that if we are a WRITER, we don't have any 32333271Smaybee * parent_locks held yet. 32343271Smaybee */ 32353271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 32363271Smaybee /* 32373271Smaybee * Drop our locks and restart 32383271Smaybee */ 32393271Smaybee zfs_rename_unlock(&zl); 32403271Smaybee *zlpp = NULL; 32413271Smaybee zp = tdzp; 324211935SMark.Shellenbaum@Sun.COM oidp = zp->z_id; 32433271Smaybee rwlp = &szp->z_parent_lock; 32443271Smaybee rw = RW_WRITER; 32453271Smaybee continue; 32463271Smaybee } else { 32473271Smaybee /* 32483271Smaybee * Wait for other thread to drop its locks 32493271Smaybee */ 32503271Smaybee rw_enter(rwlp, rw); 32513271Smaybee } 32523271Smaybee } 32533271Smaybee 3254789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 3255789Sahrens zl->zl_rwlock = rwlp; 3256789Sahrens zl->zl_znode = NULL; 3257789Sahrens zl->zl_next = *zlpp; 3258789Sahrens *zlpp = zl; 3259789Sahrens 326011935SMark.Shellenbaum@Sun.COM if (oidp == szp->z_id) /* We're a descendant of szp */ 3261789Sahrens return (EINVAL); 3262789Sahrens 326311935SMark.Shellenbaum@Sun.COM if (oidp == rootid) /* We've hit the top */ 3264789Sahrens return (0); 3265789Sahrens 3266789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 326711935SMark.Shellenbaum@Sun.COM int error = zfs_zget(zp->z_zfsvfs, oidp, &zp); 3268789Sahrens if (error) 3269789Sahrens return (error); 3270789Sahrens zl->zl_znode = zp; 3271789Sahrens } 327211935SMark.Shellenbaum@Sun.COM (void) sa_lookup(zp->z_sa_hdl, SA_ZPL_PARENT(zp->z_zfsvfs), 327311935SMark.Shellenbaum@Sun.COM &oidp, sizeof (oidp)); 3274789Sahrens rwlp = &zp->z_parent_lock; 3275789Sahrens rw = RW_READER; 3276789Sahrens 3277789Sahrens } while (zp->z_id != sdzp->z_id); 3278789Sahrens 3279789Sahrens return (0); 3280789Sahrens } 3281789Sahrens 3282789Sahrens /* 3283789Sahrens * Move an entry from the provided source directory to the target 3284789Sahrens * directory. Change the entry name as indicated. 3285789Sahrens * 3286789Sahrens * IN: sdvp - Source directory containing the "old entry". 3287789Sahrens * snm - Old entry name. 3288789Sahrens * tdvp - Target directory to contain the "new entry". 3289789Sahrens * tnm - New entry name. 3290789Sahrens * cr - credentials of caller. 32915331Samw * ct - caller context 32925331Samw * flags - case flags 3293789Sahrens * 3294789Sahrens * RETURN: 0 if success 3295789Sahrens * error code if failure 3296789Sahrens * 3297789Sahrens * Timestamps: 3298789Sahrens * sdvp,tdvp - ctime|mtime updated 3299789Sahrens */ 33005331Samw /*ARGSUSED*/ 3301789Sahrens static int 33025331Samw zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr, 33035331Samw caller_context_t *ct, int flags) 3304789Sahrens { 3305789Sahrens znode_t *tdzp, *szp, *tzp; 3306789Sahrens znode_t *sdzp = VTOZ(sdvp); 3307789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 33085326Sek110237 zilog_t *zilog; 3309789Sahrens vnode_t *realvp; 3310789Sahrens zfs_dirlock_t *sdl, *tdl; 3311789Sahrens dmu_tx_t *tx; 3312789Sahrens zfs_zlock_t *zl; 33135331Samw int cmp, serr, terr; 33145331Samw int error = 0; 33155331Samw int zflg = 0; 3316789Sahrens 33175367Sahrens ZFS_ENTER(zfsvfs); 33185367Sahrens ZFS_VERIFY_ZP(sdzp); 33195326Sek110237 zilog = zfsvfs->z_log; 3320789Sahrens 3321789Sahrens /* 3322789Sahrens * Make sure we have the real vp for the target directory. 3323789Sahrens */ 33245331Samw if (VOP_REALVP(tdvp, &realvp, ct) == 0) 3325789Sahrens tdvp = realvp; 3326789Sahrens 332712079SMark.Shellenbaum@Sun.COM if (tdvp->v_vfsp != sdvp->v_vfsp || zfsctl_is_node(tdvp)) { 3328789Sahrens ZFS_EXIT(zfsvfs); 3329789Sahrens return (EXDEV); 3330789Sahrens } 3331789Sahrens 3332789Sahrens tdzp = VTOZ(tdvp); 33335367Sahrens ZFS_VERIFY_ZP(tdzp); 33345498Stimh if (zfsvfs->z_utf8 && u8_validate(tnm, 33355331Samw strlen(tnm), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 33365331Samw ZFS_EXIT(zfsvfs); 33375331Samw return (EILSEQ); 33385331Samw } 33395331Samw 33405331Samw if (flags & FIGNORECASE) 33415331Samw zflg |= ZCILOOK; 33425331Samw 3343789Sahrens top: 3344789Sahrens szp = NULL; 3345789Sahrens tzp = NULL; 3346789Sahrens zl = NULL; 3347789Sahrens 3348789Sahrens /* 3349789Sahrens * This is to prevent the creation of links into attribute space 3350789Sahrens * by renaming a linked file into/outof an attribute directory. 3351789Sahrens * See the comment in zfs_link() for why this is considered bad. 3352789Sahrens */ 335311935SMark.Shellenbaum@Sun.COM if ((tdzp->z_pflags & ZFS_XATTR) != (sdzp->z_pflags & ZFS_XATTR)) { 3354789Sahrens ZFS_EXIT(zfsvfs); 3355789Sahrens return (EINVAL); 3356789Sahrens } 3357789Sahrens 3358789Sahrens /* 3359789Sahrens * Lock source and target directory entries. To prevent deadlock, 3360789Sahrens * a lock ordering must be defined. We lock the directory with 3361789Sahrens * the smallest object id first, or if it's a tie, the one with 3362789Sahrens * the lexically first name. 3363789Sahrens */ 3364789Sahrens if (sdzp->z_id < tdzp->z_id) { 3365789Sahrens cmp = -1; 3366789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 3367789Sahrens cmp = 1; 3368789Sahrens } else { 33695331Samw /* 33705331Samw * First compare the two name arguments without 33715331Samw * considering any case folding. 33725331Samw */ 33735331Samw int nofold = (zfsvfs->z_norm & ~U8_TEXTPREP_TOUPPER); 33745331Samw 33755331Samw cmp = u8_strcmp(snm, tnm, 0, nofold, U8_UNICODE_LATEST, &error); 33765498Stimh ASSERT(error == 0 || !zfsvfs->z_utf8); 3377789Sahrens if (cmp == 0) { 3378789Sahrens /* 3379789Sahrens * POSIX: "If the old argument and the new argument 3380789Sahrens * both refer to links to the same existing file, 3381789Sahrens * the rename() function shall return successfully 3382789Sahrens * and perform no other action." 3383789Sahrens */ 3384789Sahrens ZFS_EXIT(zfsvfs); 3385789Sahrens return (0); 3386789Sahrens } 33875331Samw /* 33885331Samw * If the file system is case-folding, then we may 33895331Samw * have some more checking to do. A case-folding file 33905331Samw * system is either supporting mixed case sensitivity 33915331Samw * access or is completely case-insensitive. Note 33925331Samw * that the file system is always case preserving. 33935331Samw * 33945331Samw * In mixed sensitivity mode case sensitive behavior 33955331Samw * is the default. FIGNORECASE must be used to 33965331Samw * explicitly request case insensitive behavior. 33975331Samw * 33985331Samw * If the source and target names provided differ only 33995331Samw * by case (e.g., a request to rename 'tim' to 'Tim'), 34005331Samw * we will treat this as a special case in the 34015331Samw * case-insensitive mode: as long as the source name 34025331Samw * is an exact match, we will allow this to proceed as 34035331Samw * a name-change request. 34045331Samw */ 34055498Stimh if ((zfsvfs->z_case == ZFS_CASE_INSENSITIVE || 34065498Stimh (zfsvfs->z_case == ZFS_CASE_MIXED && 34075498Stimh flags & FIGNORECASE)) && 34085331Samw u8_strcmp(snm, tnm, 0, zfsvfs->z_norm, U8_UNICODE_LATEST, 34095331Samw &error) == 0) { 34105331Samw /* 34115331Samw * case preserving rename request, require exact 34125331Samw * name matches 34135331Samw */ 34145331Samw zflg |= ZCIEXACT; 34155331Samw zflg &= ~ZCILOOK; 34165331Samw } 3417789Sahrens } 34185331Samw 341911321SSanjeev.Bagewadi@Sun.COM /* 342011321SSanjeev.Bagewadi@Sun.COM * If the source and destination directories are the same, we should 342111321SSanjeev.Bagewadi@Sun.COM * grab the z_name_lock of that directory only once. 342211321SSanjeev.Bagewadi@Sun.COM */ 342311321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) { 342411321SSanjeev.Bagewadi@Sun.COM zflg |= ZHAVELOCK; 342511321SSanjeev.Bagewadi@Sun.COM rw_enter(&sdzp->z_name_lock, RW_READER); 342611321SSanjeev.Bagewadi@Sun.COM } 342711321SSanjeev.Bagewadi@Sun.COM 3428789Sahrens if (cmp < 0) { 34295331Samw serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, 34305331Samw ZEXISTS | zflg, NULL, NULL); 34315331Samw terr = zfs_dirent_lock(&tdl, 34325331Samw tdzp, tnm, &tzp, ZRENAMING | zflg, NULL, NULL); 3433789Sahrens } else { 34345331Samw terr = zfs_dirent_lock(&tdl, 34355331Samw tdzp, tnm, &tzp, zflg, NULL, NULL); 34365331Samw serr = zfs_dirent_lock(&sdl, 34375331Samw sdzp, snm, &szp, ZEXISTS | ZRENAMING | zflg, 34385331Samw NULL, NULL); 3439789Sahrens } 3440789Sahrens 3441789Sahrens if (serr) { 3442789Sahrens /* 3443789Sahrens * Source entry invalid or not there. 3444789Sahrens */ 3445789Sahrens if (!terr) { 3446789Sahrens zfs_dirent_unlock(tdl); 3447789Sahrens if (tzp) 3448789Sahrens VN_RELE(ZTOV(tzp)); 3449789Sahrens } 345011321SSanjeev.Bagewadi@Sun.COM 345111321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 345211321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 345311321SSanjeev.Bagewadi@Sun.COM 3454789Sahrens if (strcmp(snm, "..") == 0) 3455789Sahrens serr = EINVAL; 3456789Sahrens ZFS_EXIT(zfsvfs); 3457789Sahrens return (serr); 3458789Sahrens } 3459789Sahrens if (terr) { 3460789Sahrens zfs_dirent_unlock(sdl); 3461789Sahrens VN_RELE(ZTOV(szp)); 346211321SSanjeev.Bagewadi@Sun.COM 346311321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 346411321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 346511321SSanjeev.Bagewadi@Sun.COM 3466789Sahrens if (strcmp(tnm, "..") == 0) 3467789Sahrens terr = EINVAL; 3468789Sahrens ZFS_EXIT(zfsvfs); 3469789Sahrens return (terr); 3470789Sahrens } 3471789Sahrens 3472789Sahrens /* 3473789Sahrens * Must have write access at the source to remove the old entry 3474789Sahrens * and write access at the target to create the new entry. 3475789Sahrens * Note that if target and source are the same, this can be 3476789Sahrens * done in a single check. 3477789Sahrens */ 3478789Sahrens 3479789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 3480789Sahrens goto out; 3481789Sahrens 3482789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3483789Sahrens /* 3484789Sahrens * Check to make sure rename is valid. 3485789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 3486789Sahrens */ 3487789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 3488789Sahrens goto out; 3489789Sahrens } 3490789Sahrens 3491789Sahrens /* 3492789Sahrens * Does target exist? 3493789Sahrens */ 3494789Sahrens if (tzp) { 3495789Sahrens /* 3496789Sahrens * Source and target must be the same type. 3497789Sahrens */ 3498789Sahrens if (ZTOV(szp)->v_type == VDIR) { 3499789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 3500789Sahrens error = ENOTDIR; 3501789Sahrens goto out; 3502789Sahrens } 3503789Sahrens } else { 3504789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 3505789Sahrens error = EISDIR; 3506789Sahrens goto out; 3507789Sahrens } 3508789Sahrens } 3509789Sahrens /* 3510789Sahrens * POSIX dictates that when the source and target 3511789Sahrens * entries refer to the same file object, rename 3512789Sahrens * must do nothing and exit without error. 3513789Sahrens */ 3514789Sahrens if (szp->z_id == tzp->z_id) { 3515789Sahrens error = 0; 3516789Sahrens goto out; 3517789Sahrens } 3518789Sahrens } 3519789Sahrens 35205331Samw vnevent_rename_src(ZTOV(szp), sdvp, snm, ct); 3521789Sahrens if (tzp) 35225331Samw vnevent_rename_dest(ZTOV(tzp), tdvp, tnm, ct); 35234863Spraks 35244863Spraks /* 35254863Spraks * notify the target directory if it is not the same 35264863Spraks * as source directory. 35274863Spraks */ 35284863Spraks if (tdvp != sdvp) { 35295331Samw vnevent_rename_dest_dir(tdvp, ct); 35304863Spraks } 3531789Sahrens 3532789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 353311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 353411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, sdzp->z_sa_hdl, B_FALSE); 35351544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 35361544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 353711935SMark.Shellenbaum@Sun.COM if (sdzp != tdzp) { 353811935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tdzp->z_sa_hdl, B_FALSE); 353911935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tdzp); 354011935SMark.Shellenbaum@Sun.COM } 354111935SMark.Shellenbaum@Sun.COM if (tzp) { 354211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, tzp->z_sa_hdl, B_FALSE); 354311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, tzp); 354411935SMark.Shellenbaum@Sun.COM } 354511935SMark.Shellenbaum@Sun.COM 354611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 35473461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 35488227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3549789Sahrens if (error) { 3550789Sahrens if (zl != NULL) 3551789Sahrens zfs_rename_unlock(&zl); 3552789Sahrens zfs_dirent_unlock(sdl); 3553789Sahrens zfs_dirent_unlock(tdl); 355411321SSanjeev.Bagewadi@Sun.COM 355511321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 355611321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 355711321SSanjeev.Bagewadi@Sun.COM 3558789Sahrens VN_RELE(ZTOV(szp)); 3559789Sahrens if (tzp) 3560789Sahrens VN_RELE(ZTOV(tzp)); 35618227SNeil.Perrin@Sun.COM if (error == ERESTART) { 35622113Sahrens dmu_tx_wait(tx); 35632113Sahrens dmu_tx_abort(tx); 3564789Sahrens goto top; 3565789Sahrens } 35662113Sahrens dmu_tx_abort(tx); 3567789Sahrens ZFS_EXIT(zfsvfs); 3568789Sahrens return (error); 3569789Sahrens } 3570789Sahrens 3571789Sahrens if (tzp) /* Attempt to remove the existing target */ 35725331Samw error = zfs_link_destroy(tdl, tzp, tx, zflg, NULL); 3573789Sahrens 3574789Sahrens if (error == 0) { 3575789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 3576789Sahrens if (error == 0) { 357711935SMark.Shellenbaum@Sun.COM szp->z_pflags |= ZFS_AV_MODIFIED; 357811935SMark.Shellenbaum@Sun.COM 357911935SMark.Shellenbaum@Sun.COM error = sa_update(szp->z_sa_hdl, SA_ZPL_FLAGS(zfsvfs), 358011935SMark.Shellenbaum@Sun.COM (void *)&szp->z_pflags, sizeof (uint64_t), tx); 358111935SMark.Shellenbaum@Sun.COM ASSERT3U(error, ==, 0); 35825331Samw 3583789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 358412413SSam.Falkner@Sun.COM if (error == 0) { 358512413SSam.Falkner@Sun.COM zfs_log_rename(zilog, tx, TX_RENAME | 358612771SNeil.Perrin@Sun.COM (flags & FIGNORECASE ? TX_CI : 0), sdzp, 358712771SNeil.Perrin@Sun.COM sdl->dl_name, tdzp, tdl->dl_name, szp); 358812413SSam.Falkner@Sun.COM 358912413SSam.Falkner@Sun.COM /* 359012413SSam.Falkner@Sun.COM * Update path information for the target vnode 359112413SSam.Falkner@Sun.COM */ 359212413SSam.Falkner@Sun.COM vn_renamepath(tdvp, ZTOV(szp), tnm, 359312413SSam.Falkner@Sun.COM strlen(tnm)); 359412413SSam.Falkner@Sun.COM } else { 359512413SSam.Falkner@Sun.COM /* 359612413SSam.Falkner@Sun.COM * At this point, we have successfully created 359712413SSam.Falkner@Sun.COM * the target name, but have failed to remove 359812413SSam.Falkner@Sun.COM * the source name. Since the create was done 359912413SSam.Falkner@Sun.COM * with the ZRENAMING flag, there are 360012413SSam.Falkner@Sun.COM * complications; for one, the link count is 360112413SSam.Falkner@Sun.COM * wrong. The easiest way to deal with this 360212413SSam.Falkner@Sun.COM * is to remove the newly created target, and 360312413SSam.Falkner@Sun.COM * return the original error. This must 360412413SSam.Falkner@Sun.COM * succeed; fortunately, it is very unlikely to 360512413SSam.Falkner@Sun.COM * fail, since we just created it. 360612413SSam.Falkner@Sun.COM */ 360712413SSam.Falkner@Sun.COM VERIFY3U(zfs_link_destroy(tdl, szp, tx, 360812413SSam.Falkner@Sun.COM ZRENAMING, NULL), ==, 0); 360912413SSam.Falkner@Sun.COM } 3610789Sahrens } 3611789Sahrens } 3612789Sahrens 3613789Sahrens dmu_tx_commit(tx); 3614789Sahrens out: 3615789Sahrens if (zl != NULL) 3616789Sahrens zfs_rename_unlock(&zl); 3617789Sahrens 3618789Sahrens zfs_dirent_unlock(sdl); 3619789Sahrens zfs_dirent_unlock(tdl); 3620789Sahrens 362111321SSanjeev.Bagewadi@Sun.COM if (sdzp == tdzp) 362211321SSanjeev.Bagewadi@Sun.COM rw_exit(&sdzp->z_name_lock); 362311321SSanjeev.Bagewadi@Sun.COM 362411321SSanjeev.Bagewadi@Sun.COM 3625789Sahrens VN_RELE(ZTOV(szp)); 3626789Sahrens if (tzp) 3627789Sahrens VN_RELE(ZTOV(tzp)); 3628789Sahrens 362912294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 363012699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 363112294SMark.Musante@Sun.COM 3632789Sahrens ZFS_EXIT(zfsvfs); 3633789Sahrens return (error); 3634789Sahrens } 3635789Sahrens 3636789Sahrens /* 3637789Sahrens * Insert the indicated symbolic reference entry into the directory. 3638789Sahrens * 3639789Sahrens * IN: dvp - Directory to contain new symbolic link. 3640789Sahrens * link - Name for new symlink entry. 3641789Sahrens * vap - Attributes of new entry. 3642789Sahrens * target - Target path of new symlink. 3643789Sahrens * cr - credentials of caller. 36445331Samw * ct - caller context 36455331Samw * flags - case flags 3646789Sahrens * 3647789Sahrens * RETURN: 0 if success 3648789Sahrens * error code if failure 3649789Sahrens * 3650789Sahrens * Timestamps: 3651789Sahrens * dvp - ctime|mtime updated 3652789Sahrens */ 36535331Samw /*ARGSUSED*/ 3654789Sahrens static int 36555331Samw zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr, 36565331Samw caller_context_t *ct, int flags) 3657789Sahrens { 3658789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 3659789Sahrens zfs_dirlock_t *dl; 3660789Sahrens dmu_tx_t *tx; 3661789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 36625326Sek110237 zilog_t *zilog; 366311935SMark.Shellenbaum@Sun.COM uint64_t len = strlen(link); 3664789Sahrens int error; 36655331Samw int zflg = ZNEW; 36669179SMark.Shellenbaum@Sun.COM zfs_acl_ids_t acl_ids; 36679179SMark.Shellenbaum@Sun.COM boolean_t fuid_dirtied; 366811935SMark.Shellenbaum@Sun.COM uint64_t txtype = TX_SYMLINK; 3669789Sahrens 3670789Sahrens ASSERT(vap->va_type == VLNK); 3671789Sahrens 36725367Sahrens ZFS_ENTER(zfsvfs); 36735367Sahrens ZFS_VERIFY_ZP(dzp); 36745326Sek110237 zilog = zfsvfs->z_log; 36755331Samw 36765498Stimh if (zfsvfs->z_utf8 && u8_validate(name, strlen(name), 36775331Samw NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 36785331Samw ZFS_EXIT(zfsvfs); 36795331Samw return (EILSEQ); 36805331Samw } 36815331Samw if (flags & FIGNORECASE) 36825331Samw zflg |= ZCILOOK; 3683789Sahrens 3684789Sahrens if (len > MAXPATHLEN) { 3685789Sahrens ZFS_EXIT(zfsvfs); 3686789Sahrens return (ENAMETOOLONG); 3687789Sahrens } 3688789Sahrens 368912302SMark.Shellenbaum@Sun.COM if ((error = zfs_acl_ids_create(dzp, 0, 369012302SMark.Shellenbaum@Sun.COM vap, cr, NULL, &acl_ids)) != 0) { 369112302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 369212302SMark.Shellenbaum@Sun.COM return (error); 369312302SMark.Shellenbaum@Sun.COM } 369412302SMark.Shellenbaum@Sun.COM top: 3695789Sahrens /* 3696789Sahrens * Attempt to lock directory; fail if entry already exists. 3697789Sahrens */ 36985331Samw error = zfs_dirent_lock(&dl, dzp, name, &zp, zflg, NULL, NULL); 36995331Samw if (error) { 370012302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3701789Sahrens ZFS_EXIT(zfsvfs); 3702789Sahrens return (error); 3703789Sahrens } 3704789Sahrens 370512302SMark.Shellenbaum@Sun.COM if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 370612302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 370712421SMark.Shellenbaum@Sun.COM zfs_dirent_unlock(dl); 370812302SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 370912302SMark.Shellenbaum@Sun.COM return (error); 371012302SMark.Shellenbaum@Sun.COM } 371112302SMark.Shellenbaum@Sun.COM 37129396SMatthew.Ahrens@Sun.COM if (zfs_acl_ids_overquota(zfsvfs, &acl_ids)) { 37139396SMatthew.Ahrens@Sun.COM zfs_acl_ids_free(&acl_ids); 37149396SMatthew.Ahrens@Sun.COM zfs_dirent_unlock(dl); 37159396SMatthew.Ahrens@Sun.COM ZFS_EXIT(zfsvfs); 37169396SMatthew.Ahrens@Sun.COM return (EDQUOT); 37179396SMatthew.Ahrens@Sun.COM } 3718789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 37199179SMark.Shellenbaum@Sun.COM fuid_dirtied = zfsvfs->z_fuid_dirty; 3720789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 37211544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 372211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa_create(tx, acl_ids.z_aclp->z_acl_bytes + 372311935SMark.Shellenbaum@Sun.COM ZFS_SA_BASE_ATTR_SIZE + len); 372411935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, dzp->z_sa_hdl, B_FALSE); 372511935SMark.Shellenbaum@Sun.COM if (!zfsvfs->z_use_sa && acl_ids.z_aclp->z_acl_bytes > ZFS_ACE_SPACE) { 372611935SMark.Shellenbaum@Sun.COM dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, 372711935SMark.Shellenbaum@Sun.COM acl_ids.z_aclp->z_acl_bytes); 372811935SMark.Shellenbaum@Sun.COM } 37299396SMatthew.Ahrens@Sun.COM if (fuid_dirtied) 37309396SMatthew.Ahrens@Sun.COM zfs_fuid_txhold(zfsvfs, tx); 37318227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3732789Sahrens if (error) { 3733789Sahrens zfs_dirent_unlock(dl); 37348227SNeil.Perrin@Sun.COM if (error == ERESTART) { 37352113Sahrens dmu_tx_wait(tx); 37362113Sahrens dmu_tx_abort(tx); 3737789Sahrens goto top; 3738789Sahrens } 373912302SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 37402113Sahrens dmu_tx_abort(tx); 3741789Sahrens ZFS_EXIT(zfsvfs); 3742789Sahrens return (error); 3743789Sahrens } 3744789Sahrens 3745789Sahrens /* 3746789Sahrens * Create a new object for the symlink. 374711935SMark.Shellenbaum@Sun.COM * for version 4 ZPL datsets the symlink will be an SA attribute 3748789Sahrens */ 374911935SMark.Shellenbaum@Sun.COM zfs_mknode(dzp, vap, tx, cr, 0, &zp, &acl_ids); 375011935SMark.Shellenbaum@Sun.COM 375111935SMark.Shellenbaum@Sun.COM if (fuid_dirtied) 375211935SMark.Shellenbaum@Sun.COM zfs_fuid_sync(zfsvfs, tx); 375311935SMark.Shellenbaum@Sun.COM 375412620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 375511935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 375611935SMark.Shellenbaum@Sun.COM error = sa_update(zp->z_sa_hdl, SA_ZPL_SYMLINK(zfsvfs), 375711935SMark.Shellenbaum@Sun.COM link, len, tx); 375811935SMark.Shellenbaum@Sun.COM else 375911935SMark.Shellenbaum@Sun.COM zfs_sa_symlink(zp, link, len, tx); 376012620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 376111935SMark.Shellenbaum@Sun.COM 376211935SMark.Shellenbaum@Sun.COM zp->z_size = len; 376311935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_SIZE(zfsvfs), 376411935SMark.Shellenbaum@Sun.COM &zp->z_size, sizeof (zp->z_size), tx); 3765789Sahrens /* 3766789Sahrens * Insert the new object into the directory. 3767789Sahrens */ 3768789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 376911935SMark.Shellenbaum@Sun.COM 377011935SMark.Shellenbaum@Sun.COM if (flags & FIGNORECASE) 377111935SMark.Shellenbaum@Sun.COM txtype |= TX_CI; 377211935SMark.Shellenbaum@Sun.COM zfs_log_symlink(zilog, tx, txtype, dzp, zp, name, link); 37739179SMark.Shellenbaum@Sun.COM 37749179SMark.Shellenbaum@Sun.COM zfs_acl_ids_free(&acl_ids); 3775789Sahrens 3776789Sahrens dmu_tx_commit(tx); 3777789Sahrens 3778789Sahrens zfs_dirent_unlock(dl); 3779789Sahrens 3780789Sahrens VN_RELE(ZTOV(zp)); 3781789Sahrens 378212294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 378312699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 378412294SMark.Musante@Sun.COM 3785789Sahrens ZFS_EXIT(zfsvfs); 3786789Sahrens return (error); 3787789Sahrens } 3788789Sahrens 3789789Sahrens /* 3790789Sahrens * Return, in the buffer contained in the provided uio structure, 3791789Sahrens * the symbolic path referred to by vp. 3792789Sahrens * 3793789Sahrens * IN: vp - vnode of symbolic link. 3794789Sahrens * uoip - structure to contain the link path. 3795789Sahrens * cr - credentials of caller. 37965331Samw * ct - caller context 3797789Sahrens * 3798789Sahrens * OUT: uio - structure to contain the link path. 3799789Sahrens * 3800789Sahrens * RETURN: 0 if success 3801789Sahrens * error code if failure 3802789Sahrens * 3803789Sahrens * Timestamps: 3804789Sahrens * vp - atime updated 3805789Sahrens */ 3806789Sahrens /* ARGSUSED */ 3807789Sahrens static int 38085331Samw zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr, caller_context_t *ct) 3809789Sahrens { 3810789Sahrens znode_t *zp = VTOZ(vp); 3811789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3812789Sahrens int error; 3813789Sahrens 38145367Sahrens ZFS_ENTER(zfsvfs); 38155367Sahrens ZFS_VERIFY_ZP(zp); 3816789Sahrens 381712620SMark.Shellenbaum@Oracle.COM mutex_enter(&zp->z_lock); 381811935SMark.Shellenbaum@Sun.COM if (zp->z_is_sa) 381911935SMark.Shellenbaum@Sun.COM error = sa_lookup_uio(zp->z_sa_hdl, 382011935SMark.Shellenbaum@Sun.COM SA_ZPL_SYMLINK(zfsvfs), uio); 382111935SMark.Shellenbaum@Sun.COM else 382211935SMark.Shellenbaum@Sun.COM error = zfs_sa_readlink(zp, uio); 382312620SMark.Shellenbaum@Oracle.COM mutex_exit(&zp->z_lock); 3824789Sahrens 3825789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 382611935SMark.Shellenbaum@Sun.COM 3827789Sahrens ZFS_EXIT(zfsvfs); 3828789Sahrens return (error); 3829789Sahrens } 3830789Sahrens 3831789Sahrens /* 3832789Sahrens * Insert a new entry into directory tdvp referencing svp. 3833789Sahrens * 3834789Sahrens * IN: tdvp - Directory to contain new entry. 3835789Sahrens * svp - vnode of new entry. 3836789Sahrens * name - name of new entry. 3837789Sahrens * cr - credentials of caller. 38385331Samw * ct - caller context 3839789Sahrens * 3840789Sahrens * RETURN: 0 if success 3841789Sahrens * error code if failure 3842789Sahrens * 3843789Sahrens * Timestamps: 3844789Sahrens * tdvp - ctime|mtime updated 3845789Sahrens * svp - ctime updated 3846789Sahrens */ 3847789Sahrens /* ARGSUSED */ 3848789Sahrens static int 38495331Samw zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr, 38505331Samw caller_context_t *ct, int flags) 3851789Sahrens { 3852789Sahrens znode_t *dzp = VTOZ(tdvp); 3853789Sahrens znode_t *tzp, *szp; 3854789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 38555326Sek110237 zilog_t *zilog; 3856789Sahrens zfs_dirlock_t *dl; 3857789Sahrens dmu_tx_t *tx; 3858789Sahrens vnode_t *realvp; 3859789Sahrens int error; 38605331Samw int zf = ZNEW; 386112079SMark.Shellenbaum@Sun.COM uint64_t parent; 3862789Sahrens 3863789Sahrens ASSERT(tdvp->v_type == VDIR); 3864789Sahrens 38655367Sahrens ZFS_ENTER(zfsvfs); 38665367Sahrens ZFS_VERIFY_ZP(dzp); 38675326Sek110237 zilog = zfsvfs->z_log; 3868789Sahrens 38695331Samw if (VOP_REALVP(svp, &realvp, ct) == 0) 3870789Sahrens svp = realvp; 3871789Sahrens 387212079SMark.Shellenbaum@Sun.COM /* 387312079SMark.Shellenbaum@Sun.COM * POSIX dictates that we return EPERM here. 387412079SMark.Shellenbaum@Sun.COM * Better choices include ENOTSUP or EISDIR. 387512079SMark.Shellenbaum@Sun.COM */ 387612079SMark.Shellenbaum@Sun.COM if (svp->v_type == VDIR) { 387712079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 387812079SMark.Shellenbaum@Sun.COM return (EPERM); 387912079SMark.Shellenbaum@Sun.COM } 388012079SMark.Shellenbaum@Sun.COM 388112079SMark.Shellenbaum@Sun.COM if (svp->v_vfsp != tdvp->v_vfsp || zfsctl_is_node(svp)) { 3882789Sahrens ZFS_EXIT(zfsvfs); 3883789Sahrens return (EXDEV); 3884789Sahrens } 388512079SMark.Shellenbaum@Sun.COM 38865367Sahrens szp = VTOZ(svp); 38875367Sahrens ZFS_VERIFY_ZP(szp); 3888789Sahrens 388912079SMark.Shellenbaum@Sun.COM /* Prevent links to .zfs/shares files */ 389012079SMark.Shellenbaum@Sun.COM 389112079SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(szp->z_sa_hdl, SA_ZPL_PARENT(zfsvfs), 389212079SMark.Shellenbaum@Sun.COM &parent, sizeof (uint64_t))) != 0) { 389312079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 389412079SMark.Shellenbaum@Sun.COM return (error); 389512079SMark.Shellenbaum@Sun.COM } 389612079SMark.Shellenbaum@Sun.COM if (parent == zfsvfs->z_shares_dir) { 389712079SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 389812079SMark.Shellenbaum@Sun.COM return (EPERM); 389912079SMark.Shellenbaum@Sun.COM } 390012079SMark.Shellenbaum@Sun.COM 39015498Stimh if (zfsvfs->z_utf8 && u8_validate(name, 39025331Samw strlen(name), NULL, U8_VALIDATE_ENTIRE, &error) < 0) { 39035331Samw ZFS_EXIT(zfsvfs); 39045331Samw return (EILSEQ); 39055331Samw } 39065331Samw if (flags & FIGNORECASE) 39075331Samw zf |= ZCILOOK; 39085331Samw 3909789Sahrens /* 3910789Sahrens * We do not support links between attributes and non-attributes 3911789Sahrens * because of the potential security risk of creating links 3912789Sahrens * into "normal" file space in order to circumvent restrictions 3913789Sahrens * imposed in attribute space. 3914789Sahrens */ 391511935SMark.Shellenbaum@Sun.COM if ((szp->z_pflags & ZFS_XATTR) != (dzp->z_pflags & ZFS_XATTR)) { 3916789Sahrens ZFS_EXIT(zfsvfs); 3917789Sahrens return (EINVAL); 3918789Sahrens } 3919789Sahrens 3920789Sahrens 392111935SMark.Shellenbaum@Sun.COM if (szp->z_uid != crgetuid(cr) && 3922789Sahrens secpolicy_basic_link(cr) != 0) { 3923789Sahrens ZFS_EXIT(zfsvfs); 3924789Sahrens return (EPERM); 3925789Sahrens } 3926789Sahrens 39275331Samw if (error = zfs_zaccess(dzp, ACE_ADD_FILE, 0, B_FALSE, cr)) { 3928789Sahrens ZFS_EXIT(zfsvfs); 3929789Sahrens return (error); 3930789Sahrens } 3931789Sahrens 393212079SMark.Shellenbaum@Sun.COM top: 3933789Sahrens /* 3934789Sahrens * Attempt to lock directory; fail if entry already exists. 3935789Sahrens */ 39365331Samw error = zfs_dirent_lock(&dl, dzp, name, &tzp, zf, NULL, NULL); 39375331Samw if (error) { 3938789Sahrens ZFS_EXIT(zfsvfs); 3939789Sahrens return (error); 3940789Sahrens } 3941789Sahrens 3942789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 394311935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, szp->z_sa_hdl, B_FALSE); 39441544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 394511935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, szp); 394611935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, dzp); 39478227SNeil.Perrin@Sun.COM error = dmu_tx_assign(tx, TXG_NOWAIT); 3948789Sahrens if (error) { 3949789Sahrens zfs_dirent_unlock(dl); 39508227SNeil.Perrin@Sun.COM if (error == ERESTART) { 39512113Sahrens dmu_tx_wait(tx); 39522113Sahrens dmu_tx_abort(tx); 3953789Sahrens goto top; 3954789Sahrens } 39552113Sahrens dmu_tx_abort(tx); 3956789Sahrens ZFS_EXIT(zfsvfs); 3957789Sahrens return (error); 3958789Sahrens } 3959789Sahrens 3960789Sahrens error = zfs_link_create(dl, szp, tx, 0); 3961789Sahrens 39625331Samw if (error == 0) { 39635331Samw uint64_t txtype = TX_LINK; 39645331Samw if (flags & FIGNORECASE) 39655331Samw txtype |= TX_CI; 39665331Samw zfs_log_link(zilog, tx, txtype, dzp, szp, name); 39675331Samw } 3968789Sahrens 3969789Sahrens dmu_tx_commit(tx); 3970789Sahrens 3971789Sahrens zfs_dirent_unlock(dl); 3972789Sahrens 39734863Spraks if (error == 0) { 39745331Samw vnevent_link(svp, ct); 39754863Spraks } 39764863Spraks 397712294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 397812699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 397912294SMark.Musante@Sun.COM 3980789Sahrens ZFS_EXIT(zfsvfs); 3981789Sahrens return (error); 3982789Sahrens } 3983789Sahrens 3984789Sahrens /* 3985789Sahrens * zfs_null_putapage() is used when the file system has been force 3986789Sahrens * unmounted. It just drops the pages. 3987789Sahrens */ 3988789Sahrens /* ARGSUSED */ 3989789Sahrens static int 3990789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 3991789Sahrens size_t *lenp, int flags, cred_t *cr) 3992789Sahrens { 3993789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 3994789Sahrens return (0); 3995789Sahrens } 3996789Sahrens 39972688Smaybee /* 39982688Smaybee * Push a page out to disk, klustering if possible. 39992688Smaybee * 40002688Smaybee * IN: vp - file to push page to. 40012688Smaybee * pp - page to push. 40022688Smaybee * flags - additional flags. 40032688Smaybee * cr - credentials of caller. 40042688Smaybee * 40052688Smaybee * OUT: offp - start of range pushed. 40062688Smaybee * lenp - len of range pushed. 40072688Smaybee * 40082688Smaybee * RETURN: 0 if success 40092688Smaybee * error code if failure 40102688Smaybee * 40112688Smaybee * NOTE: callers must have locked the page to be pushed. On 40122688Smaybee * exit, the page (and all other pages in the kluster) must be 40132688Smaybee * unlocked. 40142688Smaybee */ 4015789Sahrens /* ARGSUSED */ 4016789Sahrens static int 4017789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 4018789Sahrens size_t *lenp, int flags, cred_t *cr) 4019789Sahrens { 4020789Sahrens znode_t *zp = VTOZ(vp); 4021789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4022789Sahrens dmu_tx_t *tx; 40232688Smaybee u_offset_t off, koff; 40242688Smaybee size_t len, klen; 4025789Sahrens int err; 4026789Sahrens 40272688Smaybee off = pp->p_offset; 40282688Smaybee len = PAGESIZE; 40292688Smaybee /* 40302688Smaybee * If our blocksize is bigger than the page size, try to kluster 40318227SNeil.Perrin@Sun.COM * multiple pages so that we write a full block (thus avoiding 40322688Smaybee * a read-modify-write). 40332688Smaybee */ 403411935SMark.Shellenbaum@Sun.COM if (off < zp->z_size && zp->z_blksz > PAGESIZE) { 40358636SMark.Maybee@Sun.COM klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 40368636SMark.Maybee@Sun.COM koff = ISP2(klen) ? P2ALIGN(off, (u_offset_t)klen) : 0; 403711935SMark.Shellenbaum@Sun.COM ASSERT(koff <= zp->z_size); 403811935SMark.Shellenbaum@Sun.COM if (koff + klen > zp->z_size) 403911935SMark.Shellenbaum@Sun.COM klen = P2ROUNDUP(zp->z_size - koff, (uint64_t)PAGESIZE); 40402688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 40412688Smaybee } 40422688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 40438636SMark.Maybee@Sun.COM 40441819Smaybee /* 40451819Smaybee * Can't push pages past end-of-file. 40461819Smaybee */ 404711935SMark.Shellenbaum@Sun.COM if (off >= zp->z_size) { 40484709Smaybee /* ignore all pages */ 40492688Smaybee err = 0; 40502688Smaybee goto out; 405111935SMark.Shellenbaum@Sun.COM } else if (off + len > zp->z_size) { 405211935SMark.Shellenbaum@Sun.COM int npages = btopr(zp->z_size - off); 40532688Smaybee page_t *trunc; 40542688Smaybee 40552688Smaybee page_list_break(&pp, &trunc, npages); 40564709Smaybee /* ignore pages past end of file */ 40572688Smaybee if (trunc) 40584709Smaybee pvn_write_done(trunc, flags); 405911935SMark.Shellenbaum@Sun.COM len = zp->z_size - off; 40601819Smaybee } 40619396SMatthew.Ahrens@Sun.COM 406211935SMark.Shellenbaum@Sun.COM if (zfs_owner_overquota(zfsvfs, zp, B_FALSE) || 406311935SMark.Shellenbaum@Sun.COM zfs_owner_overquota(zfsvfs, zp, B_TRUE)) { 40649396SMatthew.Ahrens@Sun.COM err = EDQUOT; 40659396SMatthew.Ahrens@Sun.COM goto out; 40669396SMatthew.Ahrens@Sun.COM } 40678636SMark.Maybee@Sun.COM top: 4068789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 4069789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 407011935SMark.Shellenbaum@Sun.COM 407111935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 407211935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 40738227SNeil.Perrin@Sun.COM err = dmu_tx_assign(tx, TXG_NOWAIT); 4074789Sahrens if (err != 0) { 40758227SNeil.Perrin@Sun.COM if (err == ERESTART) { 40762113Sahrens dmu_tx_wait(tx); 40772113Sahrens dmu_tx_abort(tx); 4078789Sahrens goto top; 4079789Sahrens } 40802113Sahrens dmu_tx_abort(tx); 4081789Sahrens goto out; 4082789Sahrens } 4083789Sahrens 40842688Smaybee if (zp->z_blksz <= PAGESIZE) { 40857315SJonathan.Adams@Sun.COM caddr_t va = zfs_map_page(pp, S_READ); 40862688Smaybee ASSERT3U(len, <=, PAGESIZE); 40872688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 40887315SJonathan.Adams@Sun.COM zfs_unmap_page(pp, va); 40892688Smaybee } else { 40902688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 40912688Smaybee } 40922688Smaybee 40932688Smaybee if (err == 0) { 409411935SMark.Shellenbaum@Sun.COM uint64_t mtime[2], ctime[2]; 409512394SMark.Shellenbaum@Sun.COM sa_bulk_attr_t bulk[3]; 409611935SMark.Shellenbaum@Sun.COM int count = 0; 409711935SMark.Shellenbaum@Sun.COM 409811935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_MTIME(zfsvfs), NULL, 409911935SMark.Shellenbaum@Sun.COM &mtime, 16); 410011935SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_CTIME(zfsvfs), NULL, 410111935SMark.Shellenbaum@Sun.COM &ctime, 16); 410212394SMark.Shellenbaum@Sun.COM SA_ADD_BULK_ATTR(bulk, count, SA_ZPL_FLAGS(zfsvfs), NULL, 410312394SMark.Shellenbaum@Sun.COM &zp->z_pflags, 8); 410411935SMark.Shellenbaum@Sun.COM zfs_tstamp_update_setup(zp, CONTENT_MODIFIED, mtime, ctime, 410511935SMark.Shellenbaum@Sun.COM B_TRUE); 41068636SMark.Maybee@Sun.COM zfs_log_write(zfsvfs->z_log, tx, TX_WRITE, zp, off, len, 0); 41072688Smaybee } 41089951SLin.Ling@Sun.COM dmu_tx_commit(tx); 41092688Smaybee 41102688Smaybee out: 41114709Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | flags); 4112789Sahrens if (offp) 4113789Sahrens *offp = off; 4114789Sahrens if (lenp) 4115789Sahrens *lenp = len; 4116789Sahrens 4117789Sahrens return (err); 4118789Sahrens } 4119789Sahrens 4120789Sahrens /* 4121789Sahrens * Copy the portion of the file indicated from pages into the file. 4122789Sahrens * The pages are stored in a page list attached to the files vnode. 4123789Sahrens * 4124789Sahrens * IN: vp - vnode of file to push page data to. 4125789Sahrens * off - position in file to put data. 4126789Sahrens * len - amount of data to write. 4127789Sahrens * flags - flags to control the operation. 4128789Sahrens * cr - credentials of caller. 41295331Samw * ct - caller context. 4130789Sahrens * 4131789Sahrens * RETURN: 0 if success 4132789Sahrens * error code if failure 4133789Sahrens * 4134789Sahrens * Timestamps: 4135789Sahrens * vp - ctime|mtime updated 4136789Sahrens */ 41375331Samw /*ARGSUSED*/ 4138789Sahrens static int 41395331Samw zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr, 41405331Samw caller_context_t *ct) 4141789Sahrens { 4142789Sahrens znode_t *zp = VTOZ(vp); 4143789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4144789Sahrens page_t *pp; 4145789Sahrens size_t io_len; 4146789Sahrens u_offset_t io_off; 41478636SMark.Maybee@Sun.COM uint_t blksz; 41488636SMark.Maybee@Sun.COM rl_t *rl; 4149789Sahrens int error = 0; 4150789Sahrens 41515367Sahrens ZFS_ENTER(zfsvfs); 41525367Sahrens ZFS_VERIFY_ZP(zp); 4153789Sahrens 41548636SMark.Maybee@Sun.COM /* 41558636SMark.Maybee@Sun.COM * Align this request to the file block size in case we kluster. 41568636SMark.Maybee@Sun.COM * XXX - this can result in pretty aggresive locking, which can 41578636SMark.Maybee@Sun.COM * impact simultanious read/write access. One option might be 41588636SMark.Maybee@Sun.COM * to break up long requests (len == 0) into block-by-block 41598636SMark.Maybee@Sun.COM * operations to get narrower locking. 41608636SMark.Maybee@Sun.COM */ 41618636SMark.Maybee@Sun.COM blksz = zp->z_blksz; 41628636SMark.Maybee@Sun.COM if (ISP2(blksz)) 41638636SMark.Maybee@Sun.COM io_off = P2ALIGN_TYPED(off, blksz, u_offset_t); 41648636SMark.Maybee@Sun.COM else 41658636SMark.Maybee@Sun.COM io_off = 0; 41668636SMark.Maybee@Sun.COM if (len > 0 && ISP2(blksz)) 41679141SMark.Maybee@Sun.COM io_len = P2ROUNDUP_TYPED(len + (off - io_off), blksz, size_t); 41688636SMark.Maybee@Sun.COM else 41698636SMark.Maybee@Sun.COM io_len = 0; 41708636SMark.Maybee@Sun.COM 41718636SMark.Maybee@Sun.COM if (io_len == 0) { 4172789Sahrens /* 41738636SMark.Maybee@Sun.COM * Search the entire vp list for pages >= io_off. 4174789Sahrens */ 41758636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, UINT64_MAX, RL_WRITER); 41768636SMark.Maybee@Sun.COM error = pvn_vplist_dirty(vp, io_off, zfs_putapage, flags, cr); 41771472Sperrin goto out; 4178789Sahrens } 41798636SMark.Maybee@Sun.COM rl = zfs_range_lock(zp, io_off, io_len, RL_WRITER); 41808636SMark.Maybee@Sun.COM 418111935SMark.Shellenbaum@Sun.COM if (off > zp->z_size) { 4182789Sahrens /* past end of file */ 41838636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 4184789Sahrens ZFS_EXIT(zfsvfs); 4185789Sahrens return (0); 4186789Sahrens } 4187789Sahrens 418811935SMark.Shellenbaum@Sun.COM len = MIN(io_len, P2ROUNDUP(zp->z_size, PAGESIZE) - io_off); 41898636SMark.Maybee@Sun.COM 41908636SMark.Maybee@Sun.COM for (off = io_off; io_off < off + len; io_off += io_len) { 4191789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 41921669Sperrin pp = page_lookup(vp, io_off, 41934339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 4194789Sahrens } else { 4195789Sahrens pp = page_lookup_nowait(vp, io_off, 41964339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 4197789Sahrens } 4198789Sahrens 4199789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 4200789Sahrens int err; 4201789Sahrens 4202789Sahrens /* 4203789Sahrens * Found a dirty page to push 4204789Sahrens */ 42051669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 42061669Sperrin if (err) 4207789Sahrens error = err; 4208789Sahrens } else { 4209789Sahrens io_len = PAGESIZE; 4210789Sahrens } 4211789Sahrens } 42121472Sperrin out: 42138636SMark.Maybee@Sun.COM zfs_range_unlock(rl); 421412294SMark.Musante@Sun.COM if ((flags & B_ASYNC) == 0 || zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 421512699SNeil.Perrin@Sun.COM zil_commit(zfsvfs->z_log, zp->z_id); 4216789Sahrens ZFS_EXIT(zfsvfs); 4217789Sahrens return (error); 4218789Sahrens } 4219789Sahrens 42205331Samw /*ARGSUSED*/ 4221789Sahrens void 42225331Samw zfs_inactive(vnode_t *vp, cred_t *cr, caller_context_t *ct) 4223789Sahrens { 4224789Sahrens znode_t *zp = VTOZ(vp); 4225789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4226789Sahrens int error; 4227789Sahrens 42285326Sek110237 rw_enter(&zfsvfs->z_teardown_inactive_lock, RW_READER); 422911935SMark.Shellenbaum@Sun.COM if (zp->z_sa_hdl == NULL) { 42305446Sahrens /* 42315642Smaybee * The fs has been unmounted, or we did a 42325642Smaybee * suspend/resume and this file no longer exists. 42335446Sahrens */ 4234789Sahrens if (vn_has_cached_data(vp)) { 4235789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 4236789Sahrens B_INVAL, cr); 4237789Sahrens } 4238789Sahrens 42391544Seschrock mutex_enter(&zp->z_lock); 424010369Schris.kirby@sun.com mutex_enter(&vp->v_lock); 424110369Schris.kirby@sun.com ASSERT(vp->v_count == 1); 424210369Schris.kirby@sun.com vp->v_count = 0; 424310369Schris.kirby@sun.com mutex_exit(&vp->v_lock); 42445446Sahrens mutex_exit(&zp->z_lock); 42455642Smaybee rw_exit(&zfsvfs->z_teardown_inactive_lock); 42465446Sahrens zfs_znode_free(zp); 4247789Sahrens return; 4248789Sahrens } 4249789Sahrens 4250789Sahrens /* 4251789Sahrens * Attempt to push any data in the page cache. If this fails 4252789Sahrens * we will get kicked out later in zfs_zinactive(). 4253789Sahrens */ 42541298Sperrin if (vn_has_cached_data(vp)) { 42551298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 42561298Sperrin cr); 42571298Sperrin } 4258789Sahrens 42593461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 4260789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 4261789Sahrens 426211935SMark.Shellenbaum@Sun.COM dmu_tx_hold_sa(tx, zp->z_sa_hdl, B_FALSE); 426311935SMark.Shellenbaum@Sun.COM zfs_sa_upgrade_txholds(tx, zp); 4264789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 4265789Sahrens if (error) { 4266789Sahrens dmu_tx_abort(tx); 4267789Sahrens } else { 4268789Sahrens mutex_enter(&zp->z_lock); 426911935SMark.Shellenbaum@Sun.COM (void) sa_update(zp->z_sa_hdl, SA_ZPL_ATIME(zfsvfs), 427011935SMark.Shellenbaum@Sun.COM (void *)&zp->z_atime, sizeof (zp->z_atime), tx); 4271789Sahrens zp->z_atime_dirty = 0; 4272789Sahrens mutex_exit(&zp->z_lock); 4273789Sahrens dmu_tx_commit(tx); 4274789Sahrens } 4275789Sahrens } 4276789Sahrens 4277789Sahrens zfs_zinactive(zp); 42785326Sek110237 rw_exit(&zfsvfs->z_teardown_inactive_lock); 4279789Sahrens } 4280789Sahrens 4281789Sahrens /* 4282789Sahrens * Bounds-check the seek operation. 4283789Sahrens * 4284789Sahrens * IN: vp - vnode seeking within 4285789Sahrens * ooff - old file offset 4286789Sahrens * noffp - pointer to new file offset 42875331Samw * ct - caller context 4288789Sahrens * 4289789Sahrens * RETURN: 0 if success 4290789Sahrens * EINVAL if new offset invalid 4291789Sahrens */ 4292789Sahrens /* ARGSUSED */ 4293789Sahrens static int 42945331Samw zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp, 42955331Samw caller_context_t *ct) 4296789Sahrens { 4297789Sahrens if (vp->v_type == VDIR) 4298789Sahrens return (0); 4299789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 4300789Sahrens } 4301789Sahrens 4302789Sahrens /* 4303789Sahrens * Pre-filter the generic locking function to trap attempts to place 4304789Sahrens * a mandatory lock on a memory mapped file. 4305789Sahrens */ 4306789Sahrens static int 4307789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 43085331Samw flk_callback_t *flk_cbp, cred_t *cr, caller_context_t *ct) 4309789Sahrens { 4310789Sahrens znode_t *zp = VTOZ(vp); 4311789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4312789Sahrens 43135367Sahrens ZFS_ENTER(zfsvfs); 43145367Sahrens ZFS_VERIFY_ZP(zp); 4315789Sahrens 4316789Sahrens /* 43171544Seschrock * We are following the UFS semantics with respect to mapcnt 43181544Seschrock * here: If we see that the file is mapped already, then we will 43191544Seschrock * return an error, but we don't worry about races between this 43201544Seschrock * function and zfs_map(). 4321789Sahrens */ 432211935SMark.Shellenbaum@Sun.COM if (zp->z_mapcnt > 0 && MANDMODE(zp->z_mode)) { 4323789Sahrens ZFS_EXIT(zfsvfs); 4324789Sahrens return (EAGAIN); 4325789Sahrens } 4326789Sahrens ZFS_EXIT(zfsvfs); 432710896SMark.Shellenbaum@Sun.COM return (fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr, ct)); 4328789Sahrens } 4329789Sahrens 4330789Sahrens /* 4331789Sahrens * If we can't find a page in the cache, we will create a new page 4332789Sahrens * and fill it with file data. For efficiency, we may try to fill 43338636SMark.Maybee@Sun.COM * multiple pages at once (klustering) to fill up the supplied page 43349265SMark.Maybee@Sun.COM * list. Note that the pages to be filled are held with an exclusive 43359265SMark.Maybee@Sun.COM * lock to prevent access by other threads while they are being filled. 4336789Sahrens */ 4337789Sahrens static int 4338789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 4339789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 4340789Sahrens { 4341789Sahrens znode_t *zp = VTOZ(vp); 4342789Sahrens page_t *pp, *cur_pp; 4343789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 4344789Sahrens u_offset_t io_off, total; 4345789Sahrens size_t io_len; 4346789Sahrens int err; 4347789Sahrens 43482688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 43498636SMark.Maybee@Sun.COM /* 43508636SMark.Maybee@Sun.COM * We only have a single page, don't bother klustering 43518636SMark.Maybee@Sun.COM */ 4352789Sahrens io_off = off; 4353789Sahrens io_len = PAGESIZE; 43549265SMark.Maybee@Sun.COM pp = page_create_va(vp, io_off, io_len, 43559265SMark.Maybee@Sun.COM PG_EXCL | PG_WAIT, seg, addr); 4356789Sahrens } else { 4357789Sahrens /* 43588636SMark.Maybee@Sun.COM * Try to find enough pages to fill the page list 4359789Sahrens */ 4360789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 43618636SMark.Maybee@Sun.COM &io_len, off, plsz, 0); 4362789Sahrens } 4363789Sahrens if (pp == NULL) { 4364789Sahrens /* 43658636SMark.Maybee@Sun.COM * The page already exists, nothing to do here. 4366789Sahrens */ 4367789Sahrens *pl = NULL; 4368789Sahrens return (0); 4369789Sahrens } 4370789Sahrens 4371789Sahrens /* 4372789Sahrens * Fill the pages in the kluster. 4373789Sahrens */ 4374789Sahrens cur_pp = pp; 4375789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 43768636SMark.Maybee@Sun.COM caddr_t va; 43778636SMark.Maybee@Sun.COM 43782688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 43797315SJonathan.Adams@Sun.COM va = zfs_map_page(cur_pp, S_WRITE); 43809512SNeil.Perrin@Sun.COM err = dmu_read(os, zp->z_id, io_off, PAGESIZE, va, 43819512SNeil.Perrin@Sun.COM DMU_READ_PREFETCH); 43827315SJonathan.Adams@Sun.COM zfs_unmap_page(cur_pp, va); 4383789Sahrens if (err) { 4384789Sahrens /* On error, toss the entire kluster */ 4385789Sahrens pvn_read_done(pp, B_ERROR); 43867294Sperrin /* convert checksum errors into IO errors */ 43877294Sperrin if (err == ECKSUM) 43887294Sperrin err = EIO; 4389789Sahrens return (err); 4390789Sahrens } 4391789Sahrens cur_pp = cur_pp->p_next; 4392789Sahrens } 43938636SMark.Maybee@Sun.COM 4394789Sahrens /* 43958636SMark.Maybee@Sun.COM * Fill in the page list array from the kluster starting 43968636SMark.Maybee@Sun.COM * from the desired offset `off'. 4397789Sahrens * NOTE: the page list will always be null terminated. 4398789Sahrens */ 4399789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 44008636SMark.Maybee@Sun.COM ASSERT(pl == NULL || (*pl)->p_offset == off); 4401789Sahrens 4402789Sahrens return (0); 4403789Sahrens } 4404789Sahrens 4405789Sahrens /* 4406789Sahrens * Return pointers to the pages for the file region [off, off + len] 4407789Sahrens * in the pl array. If plsz is greater than len, this function may 44088636SMark.Maybee@Sun.COM * also return page pointers from after the specified region 44098636SMark.Maybee@Sun.COM * (i.e. the region [off, off + plsz]). These additional pages are 44108636SMark.Maybee@Sun.COM * only returned if they are already in the cache, or were created as 44118636SMark.Maybee@Sun.COM * part of a klustered read. 4412789Sahrens * 4413789Sahrens * IN: vp - vnode of file to get data from. 4414789Sahrens * off - position in file to get data from. 4415789Sahrens * len - amount of data to retrieve. 4416789Sahrens * plsz - length of provided page list. 4417789Sahrens * seg - segment to obtain pages for. 4418789Sahrens * addr - virtual address of fault. 4419789Sahrens * rw - mode of created pages. 4420789Sahrens * cr - credentials of caller. 44215331Samw * ct - caller context. 4422789Sahrens * 4423789Sahrens * OUT: protp - protection mode of created pages. 4424789Sahrens * pl - list of pages created. 4425789Sahrens * 4426789Sahrens * RETURN: 0 if success 4427789Sahrens * error code if failure 4428789Sahrens * 4429789Sahrens * Timestamps: 4430789Sahrens * vp - atime updated 4431789Sahrens */ 4432789Sahrens /* ARGSUSED */ 4433789Sahrens static int 4434789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 4435789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 44365331Samw enum seg_rw rw, cred_t *cr, caller_context_t *ct) 4437789Sahrens { 4438789Sahrens znode_t *zp = VTOZ(vp); 4439789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 44408636SMark.Maybee@Sun.COM page_t **pl0 = pl; 44418636SMark.Maybee@Sun.COM int err = 0; 44428636SMark.Maybee@Sun.COM 44438636SMark.Maybee@Sun.COM /* we do our own caching, faultahead is unnecessary */ 44448636SMark.Maybee@Sun.COM if (pl == NULL) 44458636SMark.Maybee@Sun.COM return (0); 44468636SMark.Maybee@Sun.COM else if (len > plsz) 44478636SMark.Maybee@Sun.COM len = plsz; 44488681SMark.Maybee@Sun.COM else 44498681SMark.Maybee@Sun.COM len = P2ROUNDUP(len, PAGESIZE); 44508636SMark.Maybee@Sun.COM ASSERT(plsz >= len); 4451789Sahrens 44525367Sahrens ZFS_ENTER(zfsvfs); 44535367Sahrens ZFS_VERIFY_ZP(zp); 4454789Sahrens 4455789Sahrens if (protp) 4456789Sahrens *protp = PROT_ALL; 4457789Sahrens 4458789Sahrens /* 44599265SMark.Maybee@Sun.COM * Loop through the requested range [off, off + len) looking 4460789Sahrens * for pages. If we don't find a page, we will need to create 4461789Sahrens * a new page and fill it with data from the file. 4462789Sahrens */ 4463789Sahrens while (len > 0) { 44648636SMark.Maybee@Sun.COM if (*pl = page_lookup(vp, off, SE_SHARED)) 44658636SMark.Maybee@Sun.COM *(pl+1) = NULL; 44668636SMark.Maybee@Sun.COM else if (err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw)) 44678636SMark.Maybee@Sun.COM goto out; 44688636SMark.Maybee@Sun.COM while (*pl) { 44698636SMark.Maybee@Sun.COM ASSERT3U((*pl)->p_offset, ==, off); 4470789Sahrens off += PAGESIZE; 4471789Sahrens addr += PAGESIZE; 44728681SMark.Maybee@Sun.COM if (len > 0) { 44738681SMark.Maybee@Sun.COM ASSERT3U(len, >=, PAGESIZE); 44748636SMark.Maybee@Sun.COM len -= PAGESIZE; 44758681SMark.Maybee@Sun.COM } 44768636SMark.Maybee@Sun.COM ASSERT3U(plsz, >=, PAGESIZE); 4477789Sahrens plsz -= PAGESIZE; 44788636SMark.Maybee@Sun.COM pl++; 4479789Sahrens } 4480789Sahrens } 4481789Sahrens 4482789Sahrens /* 4483789Sahrens * Fill out the page array with any pages already in the cache. 4484789Sahrens */ 44858636SMark.Maybee@Sun.COM while (plsz > 0 && 44868636SMark.Maybee@Sun.COM (*pl++ = page_lookup_nowait(vp, off, SE_SHARED))) { 44878636SMark.Maybee@Sun.COM off += PAGESIZE; 44888636SMark.Maybee@Sun.COM plsz -= PAGESIZE; 4489789Sahrens } 4490789Sahrens out: 44912752Sperrin if (err) { 44922752Sperrin /* 44932752Sperrin * Release any pages we have previously locked. 44942752Sperrin */ 44952752Sperrin while (pl > pl0) 44962752Sperrin page_unlock(*--pl); 44978636SMark.Maybee@Sun.COM } else { 44988636SMark.Maybee@Sun.COM ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 44992752Sperrin } 45002752Sperrin 4501789Sahrens *pl = NULL; 4502789Sahrens 4503789Sahrens ZFS_EXIT(zfsvfs); 4504789Sahrens return (err); 4505789Sahrens } 4506789Sahrens 45071544Seschrock /* 45081544Seschrock * Request a memory map for a section of a file. This code interacts 45091544Seschrock * with common code and the VM system as follows: 45101544Seschrock * 45111544Seschrock * common code calls mmap(), which ends up in smmap_common() 45121544Seschrock * 45131544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 45141544Seschrock * 45151544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 45161544Seschrock * 45171544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 45181544Seschrock * 45191544Seschrock * zfs_addmap() updates z_mapcnt 45201544Seschrock */ 45215331Samw /*ARGSUSED*/ 4522789Sahrens static int 4523789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 45245331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 45255331Samw caller_context_t *ct) 4526789Sahrens { 4527789Sahrens znode_t *zp = VTOZ(vp); 4528789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4529789Sahrens segvn_crargs_t vn_a; 4530789Sahrens int error; 4531789Sahrens 45325929Smarks ZFS_ENTER(zfsvfs); 45335929Smarks ZFS_VERIFY_ZP(zp); 45345929Smarks 453511935SMark.Shellenbaum@Sun.COM if ((prot & PROT_WRITE) && (zp->z_pflags & 453611935SMark.Shellenbaum@Sun.COM (ZFS_IMMUTABLE | ZFS_READONLY | ZFS_APPENDONLY))) { 45375929Smarks ZFS_EXIT(zfsvfs); 45385331Samw return (EPERM); 45395929Smarks } 45405929Smarks 45415929Smarks if ((prot & (PROT_READ | PROT_EXEC)) && 454211935SMark.Shellenbaum@Sun.COM (zp->z_pflags & ZFS_AV_QUARANTINED)) { 45435929Smarks ZFS_EXIT(zfsvfs); 45445929Smarks return (EACCES); 45455929Smarks } 4546789Sahrens 4547789Sahrens if (vp->v_flag & VNOMAP) { 4548789Sahrens ZFS_EXIT(zfsvfs); 4549789Sahrens return (ENOSYS); 4550789Sahrens } 4551789Sahrens 4552789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 4553789Sahrens ZFS_EXIT(zfsvfs); 4554789Sahrens return (ENXIO); 4555789Sahrens } 4556789Sahrens 4557789Sahrens if (vp->v_type != VREG) { 4558789Sahrens ZFS_EXIT(zfsvfs); 4559789Sahrens return (ENODEV); 4560789Sahrens } 4561789Sahrens 4562789Sahrens /* 4563789Sahrens * If file is locked, disallow mapping. 4564789Sahrens */ 456511935SMark.Shellenbaum@Sun.COM if (MANDMODE(zp->z_mode) && vn_has_flocks(vp)) { 45661544Seschrock ZFS_EXIT(zfsvfs); 45671544Seschrock return (EAGAIN); 4568789Sahrens } 4569789Sahrens 4570789Sahrens as_rangelock(as); 45716036Smec error = choose_addr(as, addrp, len, off, ADDR_VACALIGN, flags); 45726036Smec if (error != 0) { 45736036Smec as_rangeunlock(as); 45746036Smec ZFS_EXIT(zfsvfs); 45756036Smec return (error); 4576789Sahrens } 4577789Sahrens 4578789Sahrens vn_a.vp = vp; 4579789Sahrens vn_a.offset = (u_offset_t)off; 4580789Sahrens vn_a.type = flags & MAP_TYPE; 4581789Sahrens vn_a.prot = prot; 4582789Sahrens vn_a.maxprot = maxprot; 4583789Sahrens vn_a.cred = cr; 4584789Sahrens vn_a.amp = NULL; 4585789Sahrens vn_a.flags = flags & ~MAP_TYPE; 45861417Skchow vn_a.szc = 0; 45871417Skchow vn_a.lgrp_mem_policy_flags = 0; 4588789Sahrens 4589789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 4590789Sahrens 4591789Sahrens as_rangeunlock(as); 4592789Sahrens ZFS_EXIT(zfsvfs); 4593789Sahrens return (error); 4594789Sahrens } 4595789Sahrens 4596789Sahrens /* ARGSUSED */ 4597789Sahrens static int 4598789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 45995331Samw size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr, 46005331Samw caller_context_t *ct) 4601789Sahrens { 46021544Seschrock uint64_t pages = btopr(len); 46031544Seschrock 46041544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 4605789Sahrens return (0); 4606789Sahrens } 4607789Sahrens 46081773Seschrock /* 46091773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 46101773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 46111773Seschrock * detecting when the data was actually modified, we have to resort to 46121773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 46131773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 46141773Seschrock * which by far the most common case: 46151773Seschrock * 46161773Seschrock * open() 46171773Seschrock * mmap() 46181773Seschrock * <modify memory> 46191773Seschrock * munmap() 46201773Seschrock * close() 46211773Seschrock * <time lapse> 46221773Seschrock * putpage() via fsflush 46231773Seschrock * 46241773Seschrock * If we wait until fsflush to come along, we can have a modification time that 46251773Seschrock * is some arbitrary point in the future. In order to prevent this in the 46261773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 46271773Seschrock * torn down. 46281773Seschrock */ 4629789Sahrens /* ARGSUSED */ 4630789Sahrens static int 4631789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 46325331Samw size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr, 46335331Samw caller_context_t *ct) 4634789Sahrens { 46351544Seschrock uint64_t pages = btopr(len); 46361544Seschrock 46371544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 46381544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 46391773Seschrock 46401773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 46411773Seschrock vn_has_cached_data(vp)) 46425331Samw (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr, ct); 46431773Seschrock 4644789Sahrens return (0); 4645789Sahrens } 4646789Sahrens 4647789Sahrens /* 4648789Sahrens * Free or allocate space in a file. Currently, this function only 4649789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 4650789Sahrens * misnamed, as its functionality includes the ability to allocate as 4651789Sahrens * well as free space. 4652789Sahrens * 4653789Sahrens * IN: vp - vnode of file to free data in. 4654789Sahrens * cmd - action to take (only F_FREESP supported). 4655789Sahrens * bfp - section of file to free/alloc. 4656789Sahrens * flag - current file open mode flags. 4657789Sahrens * offset - current file offset. 4658789Sahrens * cr - credentials of caller [UNUSED]. 46595331Samw * ct - caller context. 4660789Sahrens * 4661789Sahrens * RETURN: 0 if success 4662789Sahrens * error code if failure 4663789Sahrens * 4664789Sahrens * Timestamps: 4665789Sahrens * vp - ctime|mtime updated 4666789Sahrens */ 4667789Sahrens /* ARGSUSED */ 4668789Sahrens static int 4669789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 4670789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 4671789Sahrens { 4672789Sahrens znode_t *zp = VTOZ(vp); 4673789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4674789Sahrens uint64_t off, len; 4675789Sahrens int error; 4676789Sahrens 46775367Sahrens ZFS_ENTER(zfsvfs); 46785367Sahrens ZFS_VERIFY_ZP(zp); 4679789Sahrens 4680789Sahrens if (cmd != F_FREESP) { 4681789Sahrens ZFS_EXIT(zfsvfs); 4682789Sahrens return (EINVAL); 4683789Sahrens } 4684789Sahrens 4685789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 4686789Sahrens ZFS_EXIT(zfsvfs); 4687789Sahrens return (error); 4688789Sahrens } 4689789Sahrens 4690789Sahrens if (bfp->l_len < 0) { 4691789Sahrens ZFS_EXIT(zfsvfs); 4692789Sahrens return (EINVAL); 4693789Sahrens } 4694789Sahrens 4695789Sahrens off = bfp->l_start; 46961669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 46971878Smaybee 46986992Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 4699789Sahrens 4700789Sahrens ZFS_EXIT(zfsvfs); 4701789Sahrens return (error); 4702789Sahrens } 4703789Sahrens 47045331Samw /*ARGSUSED*/ 4705789Sahrens static int 47065331Samw zfs_fid(vnode_t *vp, fid_t *fidp, caller_context_t *ct) 4707789Sahrens { 4708789Sahrens znode_t *zp = VTOZ(vp); 4709789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 47105326Sek110237 uint32_t gen; 471111935SMark.Shellenbaum@Sun.COM uint64_t gen64; 4712789Sahrens uint64_t object = zp->z_id; 4713789Sahrens zfid_short_t *zfid; 471411935SMark.Shellenbaum@Sun.COM int size, i, error; 4715789Sahrens 47165367Sahrens ZFS_ENTER(zfsvfs); 47175367Sahrens ZFS_VERIFY_ZP(zp); 471811935SMark.Shellenbaum@Sun.COM 471911935SMark.Shellenbaum@Sun.COM if ((error = sa_lookup(zp->z_sa_hdl, SA_ZPL_GEN(zfsvfs), 472012218SMark.Shellenbaum@Sun.COM &gen64, sizeof (uint64_t))) != 0) { 472112218SMark.Shellenbaum@Sun.COM ZFS_EXIT(zfsvfs); 472211935SMark.Shellenbaum@Sun.COM return (error); 472312218SMark.Shellenbaum@Sun.COM } 472411935SMark.Shellenbaum@Sun.COM 472511935SMark.Shellenbaum@Sun.COM gen = (uint32_t)gen64; 4726789Sahrens 4727789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 4728789Sahrens if (fidp->fid_len < size) { 4729789Sahrens fidp->fid_len = size; 47301512Sek110237 ZFS_EXIT(zfsvfs); 4731789Sahrens return (ENOSPC); 4732789Sahrens } 4733789Sahrens 4734789Sahrens zfid = (zfid_short_t *)fidp; 4735789Sahrens 4736789Sahrens zfid->zf_len = size; 4737789Sahrens 4738789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 4739789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 4740789Sahrens 4741789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 4742789Sahrens if (gen == 0) 4743789Sahrens gen = 1; 4744789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 4745789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 4746789Sahrens 4747789Sahrens if (size == LONG_FID_LEN) { 4748789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 4749789Sahrens zfid_long_t *zlfid; 4750789Sahrens 4751789Sahrens zlfid = (zfid_long_t *)fidp; 4752789Sahrens 4753789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 4754789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 4755789Sahrens 4756789Sahrens /* XXX - this should be the generation number for the objset */ 4757789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 4758789Sahrens zlfid->zf_setgen[i] = 0; 4759789Sahrens } 4760789Sahrens 4761789Sahrens ZFS_EXIT(zfsvfs); 4762789Sahrens return (0); 4763789Sahrens } 4764789Sahrens 4765789Sahrens static int 47665331Samw zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr, 47675331Samw caller_context_t *ct) 4768789Sahrens { 4769789Sahrens znode_t *zp, *xzp; 4770789Sahrens zfsvfs_t *zfsvfs; 4771789Sahrens zfs_dirlock_t *dl; 4772789Sahrens int error; 4773789Sahrens 4774789Sahrens switch (cmd) { 4775789Sahrens case _PC_LINK_MAX: 4776789Sahrens *valp = ULONG_MAX; 4777789Sahrens return (0); 4778789Sahrens 4779789Sahrens case _PC_FILESIZEBITS: 4780789Sahrens *valp = 64; 4781789Sahrens return (0); 4782789Sahrens 4783789Sahrens case _PC_XATTR_EXISTS: 4784789Sahrens zp = VTOZ(vp); 4785789Sahrens zfsvfs = zp->z_zfsvfs; 47865367Sahrens ZFS_ENTER(zfsvfs); 47875367Sahrens ZFS_VERIFY_ZP(zp); 4788789Sahrens *valp = 0; 4789789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 47905331Samw ZXATTR | ZEXISTS | ZSHARED, NULL, NULL); 4791789Sahrens if (error == 0) { 4792789Sahrens zfs_dirent_unlock(dl); 4793789Sahrens if (!zfs_dirempty(xzp)) 4794789Sahrens *valp = 1; 4795789Sahrens VN_RELE(ZTOV(xzp)); 4796789Sahrens } else if (error == ENOENT) { 4797789Sahrens /* 4798789Sahrens * If there aren't extended attributes, it's the 4799789Sahrens * same as having zero of them. 4800789Sahrens */ 4801789Sahrens error = 0; 4802789Sahrens } 4803789Sahrens ZFS_EXIT(zfsvfs); 4804789Sahrens return (error); 4805789Sahrens 48065331Samw case _PC_SATTR_ENABLED: 48075331Samw case _PC_SATTR_EXISTS: 48087757SJanice.Chang@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_SYSATTR_VIEWS) && 48095331Samw (vp->v_type == VREG || vp->v_type == VDIR); 48105331Samw return (0); 48115331Samw 48129749STim.Haley@Sun.COM case _PC_ACCESS_FILTERING: 48139749STim.Haley@Sun.COM *valp = vfs_has_feature(vp->v_vfsp, VFSFT_ACCESS_FILTER) && 48149749STim.Haley@Sun.COM vp->v_type == VDIR; 48159749STim.Haley@Sun.COM return (0); 48169749STim.Haley@Sun.COM 4817789Sahrens case _PC_ACL_ENABLED: 4818789Sahrens *valp = _ACL_ACE_ENABLED; 4819789Sahrens return (0); 4820789Sahrens 4821789Sahrens case _PC_MIN_HOLE_SIZE: 4822789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 4823789Sahrens return (0); 4824789Sahrens 482510440SRoger.Faulkner@Sun.COM case _PC_TIMESTAMP_RESOLUTION: 482610440SRoger.Faulkner@Sun.COM /* nanosecond timestamp resolution */ 482710440SRoger.Faulkner@Sun.COM *valp = 1L; 482810440SRoger.Faulkner@Sun.COM return (0); 482910440SRoger.Faulkner@Sun.COM 4830789Sahrens default: 48315331Samw return (fs_pathconf(vp, cmd, valp, cr, ct)); 4832789Sahrens } 4833789Sahrens } 4834789Sahrens 4835789Sahrens /*ARGSUSED*/ 4836789Sahrens static int 48375331Samw zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48385331Samw caller_context_t *ct) 4839789Sahrens { 4840789Sahrens znode_t *zp = VTOZ(vp); 4841789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4842789Sahrens int error; 48435331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 4844789Sahrens 48455367Sahrens ZFS_ENTER(zfsvfs); 48465367Sahrens ZFS_VERIFY_ZP(zp); 48475331Samw error = zfs_getacl(zp, vsecp, skipaclchk, cr); 4848789Sahrens ZFS_EXIT(zfsvfs); 4849789Sahrens 4850789Sahrens return (error); 4851789Sahrens } 4852789Sahrens 4853789Sahrens /*ARGSUSED*/ 4854789Sahrens static int 48555331Samw zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr, 48565331Samw caller_context_t *ct) 4857789Sahrens { 4858789Sahrens znode_t *zp = VTOZ(vp); 4859789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4860789Sahrens int error; 48615331Samw boolean_t skipaclchk = (flag & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE; 486212294SMark.Musante@Sun.COM zilog_t *zilog = zfsvfs->z_log; 4863789Sahrens 48645367Sahrens ZFS_ENTER(zfsvfs); 48655367Sahrens ZFS_VERIFY_ZP(zp); 486612294SMark.Musante@Sun.COM 48675331Samw error = zfs_setacl(zp, vsecp, skipaclchk, cr); 486812294SMark.Musante@Sun.COM 486912294SMark.Musante@Sun.COM if (zfsvfs->z_os->os_sync == ZFS_SYNC_ALWAYS) 487012699SNeil.Perrin@Sun.COM zil_commit(zilog, 0); 487112294SMark.Musante@Sun.COM 4872789Sahrens ZFS_EXIT(zfsvfs); 4873789Sahrens return (error); 4874789Sahrens } 4875789Sahrens 4876789Sahrens /* 487711539SChunli.Zhang@Sun.COM * Tunable, both must be a power of 2. 487811539SChunli.Zhang@Sun.COM * 487911539SChunli.Zhang@Sun.COM * zcr_blksz_min: the smallest read we may consider to loan out an arcbuf 488011539SChunli.Zhang@Sun.COM * zcr_blksz_max: if set to less than the file block size, allow loaning out of 488111539SChunli.Zhang@Sun.COM * an arcbuf for a partial block read 488211539SChunli.Zhang@Sun.COM */ 488311539SChunli.Zhang@Sun.COM int zcr_blksz_min = (1 << 10); /* 1K */ 488411539SChunli.Zhang@Sun.COM int zcr_blksz_max = (1 << 17); /* 128K */ 488511539SChunli.Zhang@Sun.COM 488611539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 488711539SChunli.Zhang@Sun.COM static int 488811539SChunli.Zhang@Sun.COM zfs_reqzcbuf(vnode_t *vp, enum uio_rw ioflag, xuio_t *xuio, cred_t *cr, 488911539SChunli.Zhang@Sun.COM caller_context_t *ct) 489011539SChunli.Zhang@Sun.COM { 489111539SChunli.Zhang@Sun.COM znode_t *zp = VTOZ(vp); 489211539SChunli.Zhang@Sun.COM zfsvfs_t *zfsvfs = zp->z_zfsvfs; 489311539SChunli.Zhang@Sun.COM int max_blksz = zfsvfs->z_max_blksz; 489411539SChunli.Zhang@Sun.COM uio_t *uio = &xuio->xu_uio; 489511539SChunli.Zhang@Sun.COM ssize_t size = uio->uio_resid; 489611539SChunli.Zhang@Sun.COM offset_t offset = uio->uio_loffset; 489711539SChunli.Zhang@Sun.COM int blksz; 489811539SChunli.Zhang@Sun.COM int fullblk, i; 489911539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 490011539SChunli.Zhang@Sun.COM ssize_t maxsize; 490111539SChunli.Zhang@Sun.COM int preamble, postamble; 490211539SChunli.Zhang@Sun.COM 490311539SChunli.Zhang@Sun.COM if (xuio->xu_type != UIOTYPE_ZEROCOPY) 490411539SChunli.Zhang@Sun.COM return (EINVAL); 490511539SChunli.Zhang@Sun.COM 490611539SChunli.Zhang@Sun.COM ZFS_ENTER(zfsvfs); 490711539SChunli.Zhang@Sun.COM ZFS_VERIFY_ZP(zp); 490811539SChunli.Zhang@Sun.COM switch (ioflag) { 490911539SChunli.Zhang@Sun.COM case UIO_WRITE: 491011539SChunli.Zhang@Sun.COM /* 491111539SChunli.Zhang@Sun.COM * Loan out an arc_buf for write if write size is bigger than 491211539SChunli.Zhang@Sun.COM * max_blksz, and the file's block size is also max_blksz. 491311539SChunli.Zhang@Sun.COM */ 491411539SChunli.Zhang@Sun.COM blksz = max_blksz; 491511539SChunli.Zhang@Sun.COM if (size < blksz || zp->z_blksz != blksz) { 491611539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 491711539SChunli.Zhang@Sun.COM return (EINVAL); 491811539SChunli.Zhang@Sun.COM } 491911539SChunli.Zhang@Sun.COM /* 492011539SChunli.Zhang@Sun.COM * Caller requests buffers for write before knowing where the 492111539SChunli.Zhang@Sun.COM * write offset might be (e.g. NFS TCP write). 492211539SChunli.Zhang@Sun.COM */ 492311539SChunli.Zhang@Sun.COM if (offset == -1) { 492411539SChunli.Zhang@Sun.COM preamble = 0; 492511539SChunli.Zhang@Sun.COM } else { 492611539SChunli.Zhang@Sun.COM preamble = P2PHASE(offset, blksz); 492711539SChunli.Zhang@Sun.COM if (preamble) { 492811539SChunli.Zhang@Sun.COM preamble = blksz - preamble; 492911539SChunli.Zhang@Sun.COM size -= preamble; 493011539SChunli.Zhang@Sun.COM } 493111539SChunli.Zhang@Sun.COM } 493211539SChunli.Zhang@Sun.COM 493311539SChunli.Zhang@Sun.COM postamble = P2PHASE(size, blksz); 493411539SChunli.Zhang@Sun.COM size -= postamble; 493511539SChunli.Zhang@Sun.COM 493611539SChunli.Zhang@Sun.COM fullblk = size / blksz; 493711576SSurya.Prakki@Sun.COM (void) dmu_xuio_init(xuio, 493811539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 493911539SChunli.Zhang@Sun.COM DTRACE_PROBE3(zfs_reqzcbuf_align, int, preamble, 494011539SChunli.Zhang@Sun.COM int, postamble, int, 494111539SChunli.Zhang@Sun.COM (preamble != 0) + fullblk + (postamble != 0)); 494211539SChunli.Zhang@Sun.COM 494311539SChunli.Zhang@Sun.COM /* 494411539SChunli.Zhang@Sun.COM * Have to fix iov base/len for partial buffers. They 494511539SChunli.Zhang@Sun.COM * currently represent full arc_buf's. 494611539SChunli.Zhang@Sun.COM */ 494711539SChunli.Zhang@Sun.COM if (preamble) { 494811539SChunli.Zhang@Sun.COM /* data begins in the middle of the arc_buf */ 494911935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 495011935SMark.Shellenbaum@Sun.COM blksz); 495111539SChunli.Zhang@Sun.COM ASSERT(abuf); 495211576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 495311576SSurya.Prakki@Sun.COM blksz - preamble, preamble); 495411539SChunli.Zhang@Sun.COM } 495511539SChunli.Zhang@Sun.COM 495611539SChunli.Zhang@Sun.COM for (i = 0; i < fullblk; i++) { 495711935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 495811935SMark.Shellenbaum@Sun.COM blksz); 495911539SChunli.Zhang@Sun.COM ASSERT(abuf); 496011576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, blksz); 496111539SChunli.Zhang@Sun.COM } 496211539SChunli.Zhang@Sun.COM 496311539SChunli.Zhang@Sun.COM if (postamble) { 496411539SChunli.Zhang@Sun.COM /* data ends in the middle of the arc_buf */ 496511935SMark.Shellenbaum@Sun.COM abuf = dmu_request_arcbuf(sa_get_db(zp->z_sa_hdl), 496611935SMark.Shellenbaum@Sun.COM blksz); 496711539SChunli.Zhang@Sun.COM ASSERT(abuf); 496811576SSurya.Prakki@Sun.COM (void) dmu_xuio_add(xuio, abuf, 0, postamble); 496911539SChunli.Zhang@Sun.COM } 497011539SChunli.Zhang@Sun.COM break; 497111539SChunli.Zhang@Sun.COM case UIO_READ: 497211539SChunli.Zhang@Sun.COM /* 497311539SChunli.Zhang@Sun.COM * Loan out an arc_buf for read if the read size is larger than 497411539SChunli.Zhang@Sun.COM * the current file block size. Block alignment is not 497511539SChunli.Zhang@Sun.COM * considered. Partial arc_buf will be loaned out for read. 497611539SChunli.Zhang@Sun.COM */ 497711539SChunli.Zhang@Sun.COM blksz = zp->z_blksz; 497811539SChunli.Zhang@Sun.COM if (blksz < zcr_blksz_min) 497911539SChunli.Zhang@Sun.COM blksz = zcr_blksz_min; 498011539SChunli.Zhang@Sun.COM if (blksz > zcr_blksz_max) 498111539SChunli.Zhang@Sun.COM blksz = zcr_blksz_max; 498211539SChunli.Zhang@Sun.COM /* avoid potential complexity of dealing with it */ 498311539SChunli.Zhang@Sun.COM if (blksz > max_blksz) { 498411539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 498511539SChunli.Zhang@Sun.COM return (EINVAL); 498611539SChunli.Zhang@Sun.COM } 498711539SChunli.Zhang@Sun.COM 498811935SMark.Shellenbaum@Sun.COM maxsize = zp->z_size - uio->uio_loffset; 498911539SChunli.Zhang@Sun.COM if (size > maxsize) 499011539SChunli.Zhang@Sun.COM size = maxsize; 499111539SChunli.Zhang@Sun.COM 499211539SChunli.Zhang@Sun.COM if (size < blksz || vn_has_cached_data(vp)) { 499311539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 499411539SChunli.Zhang@Sun.COM return (EINVAL); 499511539SChunli.Zhang@Sun.COM } 499611539SChunli.Zhang@Sun.COM break; 499711539SChunli.Zhang@Sun.COM default: 499811539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 499911539SChunli.Zhang@Sun.COM return (EINVAL); 500011539SChunli.Zhang@Sun.COM } 500111539SChunli.Zhang@Sun.COM 500211539SChunli.Zhang@Sun.COM uio->uio_extflg = UIO_XUIO; 500311539SChunli.Zhang@Sun.COM XUIO_XUZC_RW(xuio) = ioflag; 500411539SChunli.Zhang@Sun.COM ZFS_EXIT(zfsvfs); 500511539SChunli.Zhang@Sun.COM return (0); 500611539SChunli.Zhang@Sun.COM } 500711539SChunli.Zhang@Sun.COM 500811539SChunli.Zhang@Sun.COM /*ARGSUSED*/ 500911539SChunli.Zhang@Sun.COM static int 501011539SChunli.Zhang@Sun.COM zfs_retzcbuf(vnode_t *vp, xuio_t *xuio, cred_t *cr, caller_context_t *ct) 501111539SChunli.Zhang@Sun.COM { 501211539SChunli.Zhang@Sun.COM int i; 501311539SChunli.Zhang@Sun.COM arc_buf_t *abuf; 501411539SChunli.Zhang@Sun.COM int ioflag = XUIO_XUZC_RW(xuio); 501511539SChunli.Zhang@Sun.COM 501611539SChunli.Zhang@Sun.COM ASSERT(xuio->xu_type == UIOTYPE_ZEROCOPY); 501711539SChunli.Zhang@Sun.COM 501811539SChunli.Zhang@Sun.COM i = dmu_xuio_cnt(xuio); 501911539SChunli.Zhang@Sun.COM while (i-- > 0) { 502011539SChunli.Zhang@Sun.COM abuf = dmu_xuio_arcbuf(xuio, i); 502111539SChunli.Zhang@Sun.COM /* 502211539SChunli.Zhang@Sun.COM * if abuf == NULL, it must be a write buffer 502311539SChunli.Zhang@Sun.COM * that has been returned in zfs_write(). 502411539SChunli.Zhang@Sun.COM */ 502511539SChunli.Zhang@Sun.COM if (abuf) 502611539SChunli.Zhang@Sun.COM dmu_return_arcbuf(abuf); 502711539SChunli.Zhang@Sun.COM ASSERT(abuf || ioflag == UIO_WRITE); 502811539SChunli.Zhang@Sun.COM } 502911539SChunli.Zhang@Sun.COM 503011539SChunli.Zhang@Sun.COM dmu_xuio_fini(xuio); 503111539SChunli.Zhang@Sun.COM return (0); 503211539SChunli.Zhang@Sun.COM } 503311539SChunli.Zhang@Sun.COM 503411539SChunli.Zhang@Sun.COM /* 5035789Sahrens * Predeclare these here so that the compiler assumes that 5036789Sahrens * this is an "old style" function declaration that does 5037789Sahrens * not include arguments => we won't get type mismatch errors 5038789Sahrens * in the initializations that follow. 5039789Sahrens */ 5040789Sahrens static int zfs_inval(); 5041789Sahrens static int zfs_isdir(); 5042789Sahrens 5043789Sahrens static int 5044789Sahrens zfs_inval() 5045789Sahrens { 5046789Sahrens return (EINVAL); 5047789Sahrens } 5048789Sahrens 5049789Sahrens static int 5050789Sahrens zfs_isdir() 5051789Sahrens { 5052789Sahrens return (EISDIR); 5053789Sahrens } 5054789Sahrens /* 5055789Sahrens * Directory vnode operations template 5056789Sahrens */ 5057789Sahrens vnodeops_t *zfs_dvnodeops; 5058789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 50593898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50603898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50613898Srsb VOPNAME_READ, { .error = zfs_isdir }, 50623898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 50633898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50643898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50653898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50663898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 50673898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 50683898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 50693898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 50703898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 50713898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 50723898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 50733898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 50743898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 50753898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 50763898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 50773898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 50783898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 50793898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 50803898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 50813898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 50823898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 50834863Spraks VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 50843898Srsb NULL, NULL 5085789Sahrens }; 5086789Sahrens 5087789Sahrens /* 5088789Sahrens * Regular file vnode operations template 5089789Sahrens */ 5090789Sahrens vnodeops_t *zfs_fvnodeops; 5091789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 50923898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 50933898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 50943898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 50953898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 50963898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 50973898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 50983898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 50993898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51003898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 51013898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51023898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 51033898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51043898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51053898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 51063898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 51073898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 51083898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 51093898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 51103898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 51113898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 51123898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 51133898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51143898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51153898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51163898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 511711539SChunli.Zhang@Sun.COM VOPNAME_REQZCBUF, { .vop_reqzcbuf = zfs_reqzcbuf }, 511811539SChunli.Zhang@Sun.COM VOPNAME_RETZCBUF, { .vop_retzcbuf = zfs_retzcbuf }, 51193898Srsb NULL, NULL 5120789Sahrens }; 5121789Sahrens 5122789Sahrens /* 5123789Sahrens * Symbolic link vnode operations template 5124789Sahrens */ 5125789Sahrens vnodeops_t *zfs_symvnodeops; 5126789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 51273898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51283898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51293898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51303898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51313898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 51323898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51333898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51343898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51353898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51363898Srsb NULL, NULL 5137789Sahrens }; 5138789Sahrens 5139789Sahrens /* 51408845Samw@Sun.COM * special share hidden files vnode operations template 51418845Samw@Sun.COM */ 51428845Samw@Sun.COM vnodeops_t *zfs_sharevnodeops; 51438845Samw@Sun.COM const fs_operation_def_t zfs_sharevnodeops_template[] = { 51448845Samw@Sun.COM VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51458845Samw@Sun.COM VOPNAME_ACCESS, { .vop_access = zfs_access }, 51468845Samw@Sun.COM VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51478845Samw@Sun.COM VOPNAME_FID, { .vop_fid = zfs_fid }, 51488845Samw@Sun.COM VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51498845Samw@Sun.COM VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51508845Samw@Sun.COM VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51518845Samw@Sun.COM VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51528845Samw@Sun.COM NULL, NULL 51538845Samw@Sun.COM }; 51548845Samw@Sun.COM 51558845Samw@Sun.COM /* 5156789Sahrens * Extended attribute directory vnode operations template 5157789Sahrens * This template is identical to the directory vnodes 5158789Sahrens * operation template except for restricted operations: 5159789Sahrens * VOP_MKDIR() 5160789Sahrens * VOP_SYMLINK() 5161789Sahrens * Note that there are other restrictions embedded in: 5162789Sahrens * zfs_create() - restrict type to VREG 5163789Sahrens * zfs_link() - no links into/out of attribute space 5164789Sahrens * zfs_rename() - no moves into/out of attribute space 5165789Sahrens */ 5166789Sahrens vnodeops_t *zfs_xdvnodeops; 5167789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 51683898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 51693898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 51703898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 51713898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 51723898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 51733898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 51743898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 51753898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 51763898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 51773898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 51783898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 51793898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 51803898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 51813898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 51823898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 51833898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 51843898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 51853898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 51863898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 51873898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 51883898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 51893898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 51903898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 51913898Srsb NULL, NULL 5192789Sahrens }; 5193789Sahrens 5194789Sahrens /* 5195789Sahrens * Error vnode operations template 5196789Sahrens */ 5197789Sahrens vnodeops_t *zfs_evnodeops; 5198789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 51993898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 52003898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 52013898Srsb NULL, NULL 5202789Sahrens }; 5203