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 /* 223461Sahrens * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 264144Speteh /* Portions Copyright 2007 Jeremy Teo */ 274144Speteh 28789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 29789Sahrens 30789Sahrens #include <sys/types.h> 31789Sahrens #include <sys/param.h> 32789Sahrens #include <sys/time.h> 33789Sahrens #include <sys/systm.h> 34789Sahrens #include <sys/sysmacros.h> 35789Sahrens #include <sys/resource.h> 36789Sahrens #include <sys/vfs.h> 373898Srsb #include <sys/vfs_opreg.h> 38789Sahrens #include <sys/vnode.h> 39789Sahrens #include <sys/file.h> 40789Sahrens #include <sys/stat.h> 41789Sahrens #include <sys/kmem.h> 42789Sahrens #include <sys/taskq.h> 43789Sahrens #include <sys/uio.h> 44789Sahrens #include <sys/vmsystm.h> 45789Sahrens #include <sys/atomic.h> 462688Smaybee #include <sys/vm.h> 47789Sahrens #include <vm/seg_vn.h> 48789Sahrens #include <vm/pvn.h> 49789Sahrens #include <vm/as.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_vfsops.h> 56789Sahrens #include <sys/zfs_dir.h> 57789Sahrens #include <sys/zfs_acl.h> 58789Sahrens #include <sys/zfs_ioctl.h> 59789Sahrens #include <sys/fs/zfs.h> 60789Sahrens #include <sys/dmu.h> 61789Sahrens #include <sys/spa.h> 62789Sahrens #include <sys/txg.h> 63789Sahrens #include <sys/dbuf.h> 64789Sahrens #include <sys/zap.h> 65789Sahrens #include <sys/dirent.h> 66789Sahrens #include <sys/policy.h> 67789Sahrens #include <sys/sunddi.h> 68789Sahrens #include <sys/filio.h> 69789Sahrens #include "fs/fs_subr.h" 70789Sahrens #include <sys/zfs_ctldir.h> 711484Sek110237 #include <sys/dnlc.h> 721669Sperrin #include <sys/zfs_rlock.h> 73789Sahrens 74789Sahrens /* 75789Sahrens * Programming rules. 76789Sahrens * 77789Sahrens * Each vnode op performs some logical unit of work. To do this, the ZPL must 78789Sahrens * properly lock its in-core state, create a DMU transaction, do the work, 79789Sahrens * record this work in the intent log (ZIL), commit the DMU transaction, 80789Sahrens * and wait the the intent log to commit if it's is a synchronous operation. 81789Sahrens * Morover, the vnode ops must work in both normal and log replay context. 82789Sahrens * The ordering of events is important to avoid deadlocks and references 83789Sahrens * to freed memory. The example below illustrates the following Big Rules: 84789Sahrens * 85789Sahrens * (1) A check must be made in each zfs thread for a mounted file system. 86789Sahrens * This is done avoiding races using ZFS_ENTER(zfsvfs). 87789Sahrens * A ZFS_EXIT(zfsvfs) is needed before all returns. 88789Sahrens * 89789Sahrens * (2) VN_RELE() should always be the last thing except for zil_commit() 902638Sperrin * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 91789Sahrens * First, if it's the last reference, the vnode/znode 92789Sahrens * can be freed, so the zp may point to freed memory. Second, the last 93789Sahrens * reference will call zfs_zinactive(), which may induce a lot of work -- 941669Sperrin * pushing cached pages (which acquires range locks) and syncing out 95789Sahrens * cached atime changes. Third, zfs_zinactive() may require a new tx, 96789Sahrens * which could deadlock the system if you were already holding one. 97789Sahrens * 981757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 991757Sperrin * as they can span dmu_tx_assign() calls. 1001757Sperrin * 1011757Sperrin * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign(). 102789Sahrens * In normal operation, this will be TXG_NOWAIT. During ZIL replay, 103789Sahrens * it will be a specific txg. Either way, dmu_tx_assign() never blocks. 104789Sahrens * This is critical because we don't want to block while holding locks. 105789Sahrens * Note, in particular, that if a lock is sometimes acquired before 106789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 107789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 108789Sahrens * 109789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 110789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 111789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 112789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 113789Sahrens * 114789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1152113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 116789Sahrens * 1171757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 118789Sahrens * before dropping locks. This ensures that the ordering of events 119789Sahrens * in the intent log matches the order in which they actually occurred. 120789Sahrens * 1211757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 122789Sahrens * regardless of whether there were any errors. 123789Sahrens * 1242638Sperrin * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 125789Sahrens * to ensure that synchronous semantics are provided when necessary. 126789Sahrens * 127789Sahrens * In general, this is how things should be ordered in each vnode op: 128789Sahrens * 129789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 130789Sahrens * top: 131789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 132789Sahrens * rw_enter(...); // grab any other locks you need 133789Sahrens * tx = dmu_tx_create(...); // get DMU tx 134789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 135789Sahrens * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign 136789Sahrens * if (error) { 137789Sahrens * rw_exit(...); // drop locks 138789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 139789Sahrens * VN_RELE(...); // release held vnodes 140789Sahrens * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1412113Sahrens * dmu_tx_wait(tx); 1422113Sahrens * dmu_tx_abort(tx); 143789Sahrens * goto top; 144789Sahrens * } 1452113Sahrens * dmu_tx_abort(tx); // abort DMU tx 146789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 147789Sahrens * return (error); // really out of space 148789Sahrens * } 149789Sahrens * error = do_real_work(); // do whatever this VOP does 150789Sahrens * if (error == 0) 1512638Sperrin * zfs_log_*(...); // on success, make ZIL entry 152789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 153789Sahrens * rw_exit(...); // drop locks 154789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 155789Sahrens * VN_RELE(...); // release held vnodes 1562638Sperrin * zil_commit(zilog, seq, foid); // synchronous when necessary 157789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 158789Sahrens * return (error); // done, report error 159789Sahrens */ 160789Sahrens /* ARGSUSED */ 161789Sahrens static int 162789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr) 163789Sahrens { 1643063Sperrin znode_t *zp = VTOZ(*vpp); 1653063Sperrin 1663063Sperrin /* Keep a count of the synchronous opens in the znode */ 1673063Sperrin if (flag & (FSYNC | FDSYNC)) 1683063Sperrin atomic_inc_32(&zp->z_sync_cnt); 169789Sahrens return (0); 170789Sahrens } 171789Sahrens 172789Sahrens /* ARGSUSED */ 173789Sahrens static int 174789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 175789Sahrens { 1763063Sperrin znode_t *zp = VTOZ(vp); 1773063Sperrin 1783063Sperrin /* Decrement the synchronous opens in the znode */ 1794339Sperrin if ((flag & (FSYNC | FDSYNC)) && (count == 1)) 1803063Sperrin atomic_dec_32(&zp->z_sync_cnt); 1813063Sperrin 182789Sahrens /* 183789Sahrens * Clean up any locks held by this process on the vp. 184789Sahrens */ 185789Sahrens cleanlocks(vp, ddi_get_pid(), 0); 186789Sahrens cleanshares(vp, ddi_get_pid()); 187789Sahrens 188789Sahrens return (0); 189789Sahrens } 190789Sahrens 191789Sahrens /* 192789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 193789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 194789Sahrens */ 195789Sahrens static int 196789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 197789Sahrens { 198789Sahrens znode_t *zp = VTOZ(vp); 199789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 200789Sahrens uint64_t file_sz; 201789Sahrens int error; 202789Sahrens boolean_t hole; 203789Sahrens 204789Sahrens file_sz = zp->z_phys->zp_size; 205789Sahrens if (noff >= file_sz) { 206789Sahrens return (ENXIO); 207789Sahrens } 208789Sahrens 209789Sahrens if (cmd == _FIO_SEEK_HOLE) 210789Sahrens hole = B_TRUE; 211789Sahrens else 212789Sahrens hole = B_FALSE; 213789Sahrens 214789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 215789Sahrens 216789Sahrens /* end of file? */ 217789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 218789Sahrens /* 219789Sahrens * Handle the virtual hole at the end of file. 220789Sahrens */ 221789Sahrens if (hole) { 222789Sahrens *off = file_sz; 223789Sahrens return (0); 224789Sahrens } 225789Sahrens return (ENXIO); 226789Sahrens } 227789Sahrens 228789Sahrens if (noff < *off) 229789Sahrens return (error); 230789Sahrens *off = noff; 231789Sahrens return (error); 232789Sahrens } 233789Sahrens 234789Sahrens /* ARGSUSED */ 235789Sahrens static int 236789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 237789Sahrens int *rvalp) 238789Sahrens { 239789Sahrens offset_t off; 240789Sahrens int error; 241789Sahrens zfsvfs_t *zfsvfs; 242789Sahrens 243789Sahrens switch (com) { 2444339Sperrin case _FIOFFS: 245789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 246789Sahrens 2471544Seschrock /* 2481544Seschrock * The following two ioctls are used by bfu. Faking out, 2491544Seschrock * necessary to avoid bfu errors. 2501544Seschrock */ 2514339Sperrin case _FIOGDIO: 2524339Sperrin case _FIOSDIO: 2531544Seschrock return (0); 2541544Seschrock 2554339Sperrin case _FIO_SEEK_DATA: 2564339Sperrin case _FIO_SEEK_HOLE: 257789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 258789Sahrens return (EFAULT); 259789Sahrens 260789Sahrens zfsvfs = VTOZ(vp)->z_zfsvfs; 261789Sahrens ZFS_ENTER(zfsvfs); 262789Sahrens 263789Sahrens /* offset parameter is in/out */ 264789Sahrens error = zfs_holey(vp, com, &off); 265789Sahrens ZFS_EXIT(zfsvfs); 266789Sahrens if (error) 267789Sahrens return (error); 268789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 269789Sahrens return (EFAULT); 270789Sahrens return (0); 271789Sahrens } 272789Sahrens return (ENOTTY); 273789Sahrens } 274789Sahrens 275789Sahrens /* 276789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 277789Sahrens * between the DMU cache and the memory mapped pages. What this means: 278789Sahrens * 279789Sahrens * On Write: If we find a memory mapped page, we write to *both* 280789Sahrens * the page and the dmu buffer. 281789Sahrens * 282789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 283789Sahrens * the file is memory mapped. 284789Sahrens */ 285789Sahrens static int 2863638Sbillm mappedwrite(vnode_t *vp, int nbytes, uio_t *uio, dmu_tx_t *tx) 287789Sahrens { 288789Sahrens znode_t *zp = VTOZ(vp); 289789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 290789Sahrens int64_t start, off; 291789Sahrens int len = nbytes; 292789Sahrens int error = 0; 293789Sahrens 294789Sahrens start = uio->uio_loffset; 295789Sahrens off = start & PAGEOFFSET; 296789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 297789Sahrens page_t *pp; 298789Sahrens uint64_t bytes = MIN(PAGESIZE - off, len); 2993638Sbillm uint64_t woff = uio->uio_loffset; 300789Sahrens 301789Sahrens /* 302789Sahrens * We don't want a new page to "appear" in the middle of 303789Sahrens * the file update (because it may not get the write 304789Sahrens * update data), so we grab a lock to block 305789Sahrens * zfs_getpage(). 306789Sahrens */ 307789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 308789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 309789Sahrens caddr_t va; 310789Sahrens 311789Sahrens rw_exit(&zp->z_map_lock); 312789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 313789Sahrens error = uiomove(va+off, bytes, UIO_WRITE, uio); 314789Sahrens if (error == 0) { 315789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, 316789Sahrens woff, bytes, va+off, tx); 317789Sahrens } 318789Sahrens ppmapout(va); 319789Sahrens page_unlock(pp); 320789Sahrens } else { 321789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 3223638Sbillm uio, bytes, tx); 323789Sahrens rw_exit(&zp->z_map_lock); 324789Sahrens } 325789Sahrens len -= bytes; 326789Sahrens off = 0; 327789Sahrens if (error) 328789Sahrens break; 329789Sahrens } 330789Sahrens return (error); 331789Sahrens } 332789Sahrens 333789Sahrens /* 334789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 335789Sahrens * between the DMU cache and the memory mapped pages. What this means: 336789Sahrens * 337789Sahrens * On Read: We "read" preferentially from memory mapped pages, 338789Sahrens * else we default from the dmu buffer. 339789Sahrens * 340789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 341789Sahrens * the file is memory mapped. 342789Sahrens */ 343789Sahrens static int 3443638Sbillm mappedread(vnode_t *vp, int nbytes, uio_t *uio) 345789Sahrens { 3463638Sbillm znode_t *zp = VTOZ(vp); 3473638Sbillm objset_t *os = zp->z_zfsvfs->z_os; 3483638Sbillm int64_t start, off; 349789Sahrens int len = nbytes; 350789Sahrens int error = 0; 351789Sahrens 352789Sahrens start = uio->uio_loffset; 353789Sahrens off = start & PAGEOFFSET; 354789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 355789Sahrens page_t *pp; 3563638Sbillm uint64_t bytes = MIN(PAGESIZE - off, len); 3573638Sbillm 358789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 359789Sahrens caddr_t va; 360789Sahrens 3612688Smaybee va = ppmapin(pp, PROT_READ, (caddr_t)-1L); 362789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 363789Sahrens ppmapout(va); 364789Sahrens page_unlock(pp); 365789Sahrens } else { 3663638Sbillm error = dmu_read_uio(os, zp->z_id, uio, bytes); 367789Sahrens } 368789Sahrens len -= bytes; 369789Sahrens off = 0; 370789Sahrens if (error) 371789Sahrens break; 372789Sahrens } 373789Sahrens return (error); 374789Sahrens } 375789Sahrens 3763638Sbillm offset_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 377789Sahrens 378789Sahrens /* 379789Sahrens * Read bytes from specified file into supplied buffer. 380789Sahrens * 381789Sahrens * IN: vp - vnode of file to be read from. 382789Sahrens * uio - structure supplying read location, range info, 383789Sahrens * and return buffer. 384789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 385789Sahrens * cr - credentials of caller. 386789Sahrens * 387789Sahrens * OUT: uio - updated offset and range, buffer filled. 388789Sahrens * 389789Sahrens * RETURN: 0 if success 390789Sahrens * error code if failure 391789Sahrens * 392789Sahrens * Side Effects: 393789Sahrens * vp - atime updated if byte count > 0 394789Sahrens */ 395789Sahrens /* ARGSUSED */ 396789Sahrens static int 397789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 398789Sahrens { 399789Sahrens znode_t *zp = VTOZ(vp); 400789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 4013638Sbillm objset_t *os = zfsvfs->z_os; 4023638Sbillm ssize_t n, nbytes; 4033638Sbillm int error; 4041669Sperrin rl_t *rl; 405789Sahrens 406789Sahrens ZFS_ENTER(zfsvfs); 407789Sahrens 408789Sahrens /* 409789Sahrens * Validate file offset 410789Sahrens */ 411789Sahrens if (uio->uio_loffset < (offset_t)0) { 412789Sahrens ZFS_EXIT(zfsvfs); 413789Sahrens return (EINVAL); 414789Sahrens } 415789Sahrens 416789Sahrens /* 417789Sahrens * Fasttrack empty reads 418789Sahrens */ 419789Sahrens if (uio->uio_resid == 0) { 420789Sahrens ZFS_EXIT(zfsvfs); 421789Sahrens return (0); 422789Sahrens } 423789Sahrens 424789Sahrens /* 4251669Sperrin * Check for mandatory locks 426789Sahrens */ 427789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 428789Sahrens if (error = chklock(vp, FREAD, 429789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 430789Sahrens ZFS_EXIT(zfsvfs); 431789Sahrens return (error); 432789Sahrens } 433789Sahrens } 434789Sahrens 435789Sahrens /* 436789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 437789Sahrens */ 4382638Sperrin if (ioflag & FRSYNC) 4392638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 440789Sahrens 441789Sahrens /* 4421669Sperrin * Lock the range against changes. 443789Sahrens */ 4441669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4451669Sperrin 446789Sahrens /* 447789Sahrens * If we are reading past end-of-file we can skip 448789Sahrens * to the end; but we might still need to set atime. 449789Sahrens */ 450789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 451789Sahrens error = 0; 452789Sahrens goto out; 453789Sahrens } 454789Sahrens 4553638Sbillm ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 4563638Sbillm n = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 4573638Sbillm 4583638Sbillm while (n > 0) { 4593638Sbillm nbytes = MIN(n, zfs_read_chunk_size - 4603638Sbillm P2PHASE(uio->uio_loffset, zfs_read_chunk_size)); 4613638Sbillm 4623638Sbillm if (vn_has_cached_data(vp)) 4633638Sbillm error = mappedread(vp, nbytes, uio); 4643638Sbillm else 4653638Sbillm error = dmu_read_uio(os, zp->z_id, uio, nbytes); 4661544Seschrock if (error) 4673638Sbillm break; 4683638Sbillm 4693638Sbillm n -= nbytes; 470789Sahrens } 4713638Sbillm 472789Sahrens out: 4732237Smaybee zfs_range_unlock(rl); 474789Sahrens 475789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 476789Sahrens ZFS_EXIT(zfsvfs); 477789Sahrens return (error); 478789Sahrens } 479789Sahrens 480789Sahrens /* 481789Sahrens * Fault in the pages of the first n bytes specified by the uio structure. 482789Sahrens * 1 byte in each page is touched and the uio struct is unmodified. 483789Sahrens * Any error will exit this routine as this is only a best 484789Sahrens * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 485789Sahrens */ 486789Sahrens static void 487789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio) 488789Sahrens { 489789Sahrens struct iovec *iov; 490789Sahrens ulong_t cnt, incr; 491789Sahrens caddr_t p; 492789Sahrens uint8_t tmp; 493789Sahrens 494789Sahrens iov = uio->uio_iov; 495789Sahrens 496789Sahrens while (n) { 497789Sahrens cnt = MIN(iov->iov_len, n); 498789Sahrens if (cnt == 0) { 499789Sahrens /* empty iov entry */ 500789Sahrens iov++; 501789Sahrens continue; 502789Sahrens } 503789Sahrens n -= cnt; 504789Sahrens /* 505789Sahrens * touch each page in this segment. 506789Sahrens */ 507789Sahrens p = iov->iov_base; 508789Sahrens while (cnt) { 509789Sahrens switch (uio->uio_segflg) { 510789Sahrens case UIO_USERSPACE: 511789Sahrens case UIO_USERISPACE: 512789Sahrens if (fuword8(p, &tmp)) 513789Sahrens return; 514789Sahrens break; 515789Sahrens case UIO_SYSSPACE: 516789Sahrens if (kcopy(p, &tmp, 1)) 517789Sahrens return; 518789Sahrens break; 519789Sahrens } 520789Sahrens incr = MIN(cnt, PAGESIZE); 521789Sahrens p += incr; 522789Sahrens cnt -= incr; 523789Sahrens } 524789Sahrens /* 525789Sahrens * touch the last byte in case it straddles a page. 526789Sahrens */ 527789Sahrens p--; 528789Sahrens switch (uio->uio_segflg) { 529789Sahrens case UIO_USERSPACE: 530789Sahrens case UIO_USERISPACE: 531789Sahrens if (fuword8(p, &tmp)) 532789Sahrens return; 533789Sahrens break; 534789Sahrens case UIO_SYSSPACE: 535789Sahrens if (kcopy(p, &tmp, 1)) 536789Sahrens return; 537789Sahrens break; 538789Sahrens } 539789Sahrens iov++; 540789Sahrens } 541789Sahrens } 542789Sahrens 543789Sahrens /* 544789Sahrens * Write the bytes to a file. 545789Sahrens * 546789Sahrens * IN: vp - vnode of file to be written to. 547789Sahrens * uio - structure supplying write location, range info, 548789Sahrens * and data buffer. 549789Sahrens * ioflag - FAPPEND flag set if in append mode. 550789Sahrens * cr - credentials of caller. 551789Sahrens * 552789Sahrens * OUT: uio - updated offset and range. 553789Sahrens * 554789Sahrens * RETURN: 0 if success 555789Sahrens * error code if failure 556789Sahrens * 557789Sahrens * Timestamps: 558789Sahrens * vp - ctime|mtime updated if byte count > 0 559789Sahrens */ 560789Sahrens /* ARGSUSED */ 561789Sahrens static int 562789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 563789Sahrens { 564789Sahrens znode_t *zp = VTOZ(vp); 565789Sahrens rlim64_t limit = uio->uio_llimit; 566789Sahrens ssize_t start_resid = uio->uio_resid; 567789Sahrens ssize_t tx_bytes; 568789Sahrens uint64_t end_size; 569789Sahrens dmu_tx_t *tx; 570789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 571789Sahrens zilog_t *zilog = zfsvfs->z_log; 572789Sahrens offset_t woff; 573789Sahrens ssize_t n, nbytes; 5741669Sperrin rl_t *rl; 575789Sahrens int max_blksz = zfsvfs->z_max_blksz; 5761669Sperrin int error; 577789Sahrens 578789Sahrens /* 579789Sahrens * Fasttrack empty write 580789Sahrens */ 5811669Sperrin n = start_resid; 582789Sahrens if (n == 0) 583789Sahrens return (0); 584789Sahrens 5851669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 5861669Sperrin limit = MAXOFFSET_T; 5871669Sperrin 588789Sahrens ZFS_ENTER(zfsvfs); 589789Sahrens 590789Sahrens /* 5912237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 5921669Sperrin * don't hold up txg. 593789Sahrens */ 5942237Smaybee zfs_prefault_write(n, uio); 595789Sahrens 596789Sahrens /* 597789Sahrens * If in append mode, set the io offset pointer to eof. 598789Sahrens */ 5991669Sperrin if (ioflag & FAPPEND) { 6001669Sperrin /* 6011669Sperrin * Range lock for a file append: 6021669Sperrin * The value for the start of range will be determined by 6031669Sperrin * zfs_range_lock() (to guarantee append semantics). 6041669Sperrin * If this write will cause the block size to increase, 6051669Sperrin * zfs_range_lock() will lock the entire file, so we must 6061669Sperrin * later reduce the range after we grow the block size. 6071669Sperrin */ 6081669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6091669Sperrin if (rl->r_len == UINT64_MAX) { 6101669Sperrin /* overlocked, zp_size can't change */ 6111669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6121669Sperrin } else { 6131669Sperrin woff = uio->uio_loffset = rl->r_off; 6141669Sperrin } 615789Sahrens } else { 616789Sahrens woff = uio->uio_loffset; 617789Sahrens /* 618789Sahrens * Validate file offset 619789Sahrens */ 620789Sahrens if (woff < 0) { 621789Sahrens ZFS_EXIT(zfsvfs); 622789Sahrens return (EINVAL); 623789Sahrens } 624789Sahrens 625789Sahrens /* 6261669Sperrin * If we need to grow the block size then zfs_range_lock() 6271669Sperrin * will lock a wider range than we request here. 6281669Sperrin * Later after growing the block size we reduce the range. 629789Sahrens */ 6301669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 631789Sahrens } 632789Sahrens 633789Sahrens if (woff >= limit) { 6343638Sbillm zfs_range_unlock(rl); 6353638Sbillm ZFS_EXIT(zfsvfs); 6363638Sbillm return (EFBIG); 637789Sahrens } 638789Sahrens 639789Sahrens if ((woff + n) > limit || woff > (limit - n)) 640789Sahrens n = limit - woff; 641789Sahrens 642789Sahrens /* 6431669Sperrin * Check for mandatory locks 644789Sahrens */ 645789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 6463638Sbillm (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) { 6473638Sbillm zfs_range_unlock(rl); 6483638Sbillm ZFS_EXIT(zfsvfs); 6493638Sbillm return (error); 6503638Sbillm } 6511669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 652789Sahrens 6531669Sperrin /* 6543638Sbillm * Write the file in reasonable size chunks. Each chunk is written 6553638Sbillm * in a separate transaction; this keeps the intent log records small 6563638Sbillm * and allows us to do more fine-grained space accounting. 657789Sahrens */ 658789Sahrens while (n > 0) { 659789Sahrens /* 6603638Sbillm * Start a transaction. 661789Sahrens */ 662789Sahrens woff = uio->uio_loffset; 663789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 664789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 665789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 666789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 667789Sahrens if (error) { 668789Sahrens if (error == ERESTART && 669789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 6702113Sahrens dmu_tx_wait(tx); 6712113Sahrens dmu_tx_abort(tx); 6723638Sbillm continue; 673789Sahrens } 6742113Sahrens dmu_tx_abort(tx); 6753638Sbillm break; 6763638Sbillm } 6773638Sbillm 6783638Sbillm /* 6793638Sbillm * If zfs_range_lock() over-locked we grow the blocksize 6803638Sbillm * and then reduce the lock range. This will only happen 6813638Sbillm * on the first iteration since zfs_range_reduce() will 6823638Sbillm * shrink down r_len to the appropriate size. 6833638Sbillm */ 6843638Sbillm if (rl->r_len == UINT64_MAX) { 6853638Sbillm uint64_t new_blksz; 6863638Sbillm 6873638Sbillm if (zp->z_blksz > max_blksz) { 6883638Sbillm ASSERT(!ISP2(zp->z_blksz)); 6893638Sbillm new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 6903638Sbillm } else { 6913638Sbillm new_blksz = MIN(end_size, max_blksz); 6923638Sbillm } 6933638Sbillm zfs_grow_blocksize(zp, new_blksz, tx); 6943638Sbillm zfs_range_reduce(rl, woff, n); 6953638Sbillm } 6963638Sbillm 6973638Sbillm /* 6983638Sbillm * XXX - should we really limit each write to z_max_blksz? 6993638Sbillm * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 7003638Sbillm */ 7013638Sbillm nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 7023638Sbillm rw_enter(&zp->z_map_lock, RW_READER); 7033638Sbillm 7043638Sbillm tx_bytes = uio->uio_resid; 7053638Sbillm if (vn_has_cached_data(vp)) { 7063638Sbillm rw_exit(&zp->z_map_lock); 7073638Sbillm error = mappedwrite(vp, nbytes, uio, tx); 7083638Sbillm } else { 7093638Sbillm error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 7103638Sbillm uio, nbytes, tx); 7113638Sbillm rw_exit(&zp->z_map_lock); 712789Sahrens } 7133638Sbillm tx_bytes -= uio->uio_resid; 7143638Sbillm 7153638Sbillm /* 7163638Sbillm * If we made no progress, we're done. If we made even 7173638Sbillm * partial progress, update the znode and ZIL accordingly. 7183638Sbillm */ 7193638Sbillm if (tx_bytes == 0) { 7203897Smaybee dmu_tx_commit(tx); 7213638Sbillm ASSERT(error != 0); 7223638Sbillm break; 7233638Sbillm } 7243638Sbillm 725789Sahrens /* 7263638Sbillm * Clear Set-UID/Set-GID bits on successful write if not 7273638Sbillm * privileged and at least one of the excute bits is set. 7283638Sbillm * 7293638Sbillm * It would be nice to to this after all writes have 7303638Sbillm * been done, but that would still expose the ISUID/ISGID 7313638Sbillm * to another app after the partial write is committed. 732789Sahrens */ 7333638Sbillm mutex_enter(&zp->z_acl_lock); 7343638Sbillm if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7353638Sbillm (S_IXUSR >> 6))) != 0 && 7363638Sbillm (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7373638Sbillm secpolicy_vnode_setid_retain(cr, 7383638Sbillm (zp->z_phys->zp_mode & S_ISUID) != 0 && 7393638Sbillm zp->z_phys->zp_uid == 0) != 0) { 7404339Sperrin zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 7413638Sbillm } 7423638Sbillm mutex_exit(&zp->z_acl_lock); 7433638Sbillm 7443638Sbillm /* 7453638Sbillm * Update time stamp. NOTE: This marks the bonus buffer as 7463638Sbillm * dirty, so we don't have to do it again for zp_size. 7473638Sbillm */ 7483638Sbillm zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 7493638Sbillm 7503638Sbillm /* 7513638Sbillm * Update the file size (zp_size) if it has changed; 7523638Sbillm * account for possible concurrent updates. 7533638Sbillm */ 7543638Sbillm while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) 755789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 756789Sahrens uio->uio_loffset); 7573638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, ioflag); 7583638Sbillm dmu_tx_commit(tx); 7593638Sbillm 7603638Sbillm if (error != 0) 7613638Sbillm break; 7623638Sbillm ASSERT(tx_bytes == nbytes); 7633638Sbillm n -= nbytes; 764789Sahrens } 765789Sahrens 7662237Smaybee zfs_range_unlock(rl); 767789Sahrens 768789Sahrens /* 769789Sahrens * If we're in replay mode, or we made no progress, return error. 770789Sahrens * Otherwise, it's at least a partial write, so it's successful. 771789Sahrens */ 772789Sahrens if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 773789Sahrens ZFS_EXIT(zfsvfs); 774789Sahrens return (error); 775789Sahrens } 776789Sahrens 7772638Sperrin if (ioflag & (FSYNC | FDSYNC)) 7782638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 779789Sahrens 780789Sahrens ZFS_EXIT(zfsvfs); 781789Sahrens return (0); 782789Sahrens } 783789Sahrens 7842237Smaybee void 7853063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd) 7862237Smaybee { 7873063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 7883063Sperrin rl_t *rl = zgd->zgd_rl; 7892237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 7902237Smaybee 7913063Sperrin dmu_buf_rele(db, vzgd); 7922237Smaybee zfs_range_unlock(rl); 7932237Smaybee VN_RELE(vp); 7943063Sperrin zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp))); 7953063Sperrin kmem_free(zgd, sizeof (zgd_t)); 7962237Smaybee } 7972237Smaybee 798789Sahrens /* 799789Sahrens * Get data to generate a TX_WRITE intent log record. 800789Sahrens */ 801789Sahrens int 8022237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 803789Sahrens { 804789Sahrens zfsvfs_t *zfsvfs = arg; 805789Sahrens objset_t *os = zfsvfs->z_os; 806789Sahrens znode_t *zp; 807789Sahrens uint64_t off = lr->lr_offset; 8082237Smaybee dmu_buf_t *db; 8091669Sperrin rl_t *rl; 8103063Sperrin zgd_t *zgd; 8113638Sbillm int dlen = lr->lr_length; /* length of user data */ 812789Sahrens int error = 0; 813789Sahrens 8143063Sperrin ASSERT(zio); 815789Sahrens ASSERT(dlen != 0); 816789Sahrens 817789Sahrens /* 8181669Sperrin * Nothing to do if the file has been removed 819789Sahrens */ 820789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 821789Sahrens return (ENOENT); 8223461Sahrens if (zp->z_unlinked) { 823789Sahrens VN_RELE(ZTOV(zp)); 824789Sahrens return (ENOENT); 825789Sahrens } 826789Sahrens 827789Sahrens /* 828789Sahrens * Write records come in two flavors: immediate and indirect. 829789Sahrens * For small writes it's cheaper to store the data with the 830789Sahrens * log record (immediate); for large writes it's cheaper to 831789Sahrens * sync the data and get a pointer to it (indirect) so that 832789Sahrens * we don't have to write the data twice. 833789Sahrens */ 8341669Sperrin if (buf != NULL) { /* immediate write */ 8351669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 8361669Sperrin /* test for truncation needs to be done while range locked */ 8371669Sperrin if (off >= zp->z_phys->zp_size) { 8381669Sperrin error = ENOENT; 8391669Sperrin goto out; 8401669Sperrin } 8412449Smaybee VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 8421669Sperrin } else { /* indirect write */ 8431669Sperrin uint64_t boff; /* block starting offset */ 8441669Sperrin 845789Sahrens /* 8461669Sperrin * Have to lock the whole block to ensure when it's 8471669Sperrin * written out and it's checksum is being calculated 8481669Sperrin * that no one can change the data. We need to re-check 8491669Sperrin * blocksize after we get the lock in case it's changed! 850789Sahrens */ 8511669Sperrin for (;;) { 8521941Sperrin if (ISP2(zp->z_blksz)) { 8531941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 8541941Sperrin uint64_t); 8551941Sperrin } else { 8561941Sperrin boff = 0; 8571941Sperrin } 8581669Sperrin dlen = zp->z_blksz; 8591669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 8601669Sperrin if (zp->z_blksz == dlen) 8611669Sperrin break; 8622237Smaybee zfs_range_unlock(rl); 8631669Sperrin } 8641669Sperrin /* test for truncation needs to be done while range locked */ 8651669Sperrin if (off >= zp->z_phys->zp_size) { 8661669Sperrin error = ENOENT; 8671669Sperrin goto out; 8681669Sperrin } 8693063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 8703063Sperrin zgd->zgd_rl = rl; 8713063Sperrin zgd->zgd_zilog = zfsvfs->z_log; 8723063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 8733063Sperrin VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 8742237Smaybee ASSERT(boff == db->db_offset); 8752237Smaybee lr->lr_blkoff = off - boff; 8762237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 8773063Sperrin lr->lr_common.lrc_txg, zfs_get_done, zgd); 8783897Smaybee ASSERT(error == EEXIST || lr->lr_length <= zp->z_blksz); 8793063Sperrin if (error == 0) { 8803063Sperrin zil_add_vdev(zfsvfs->z_log, 8813063Sperrin DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr))); 8823063Sperrin } 8832237Smaybee /* 8842237Smaybee * If we get EINPROGRESS, then we need to wait for a 8852237Smaybee * write IO initiated by dmu_sync() to complete before 8862638Sperrin * we can release this dbuf. We will finish everything 8872237Smaybee * up in the zfs_get_done() callback. 8882237Smaybee */ 8892237Smaybee if (error == EINPROGRESS) 8902237Smaybee return (0); 8913063Sperrin dmu_buf_rele(db, zgd); 8923063Sperrin kmem_free(zgd, sizeof (zgd_t)); 893789Sahrens } 8941669Sperrin out: 8952237Smaybee zfs_range_unlock(rl); 896789Sahrens VN_RELE(ZTOV(zp)); 897789Sahrens return (error); 898789Sahrens } 899789Sahrens 900789Sahrens /*ARGSUSED*/ 901789Sahrens static int 902789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr) 903789Sahrens { 904789Sahrens znode_t *zp = VTOZ(vp); 905789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 906789Sahrens int error; 907789Sahrens 908789Sahrens ZFS_ENTER(zfsvfs); 909789Sahrens error = zfs_zaccess_rwx(zp, mode, cr); 910789Sahrens ZFS_EXIT(zfsvfs); 911789Sahrens return (error); 912789Sahrens } 913789Sahrens 914789Sahrens /* 915789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 916789Sahrens * If it exists, return a held vnode reference for it. 917789Sahrens * 918789Sahrens * IN: dvp - vnode of directory to search. 919789Sahrens * nm - name of entry to lookup. 920789Sahrens * pnp - full pathname to lookup [UNUSED]. 921789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 922789Sahrens * rdir - root directory vnode [UNUSED]. 923789Sahrens * cr - credentials of caller. 924789Sahrens * 925789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 926789Sahrens * 927789Sahrens * RETURN: 0 if success 928789Sahrens * error code if failure 929789Sahrens * 930789Sahrens * Timestamps: 931789Sahrens * NA 932789Sahrens */ 933789Sahrens /* ARGSUSED */ 934789Sahrens static int 935789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 936789Sahrens int flags, vnode_t *rdir, cred_t *cr) 937789Sahrens { 938789Sahrens 939789Sahrens znode_t *zdp = VTOZ(dvp); 940789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 941789Sahrens int error; 942789Sahrens 943789Sahrens ZFS_ENTER(zfsvfs); 944789Sahrens 945789Sahrens *vpp = NULL; 946789Sahrens 947789Sahrens if (flags & LOOKUP_XATTR) { 948789Sahrens /* 9493234Sck153898 * If the xattr property is off, refuse the lookup request. 9503234Sck153898 */ 9513234Sck153898 if (!(zfsvfs->z_vfs->vfs_flag & VFS_XATTR)) { 9523234Sck153898 ZFS_EXIT(zfsvfs); 9533234Sck153898 return (EINVAL); 9543234Sck153898 } 9553234Sck153898 9563234Sck153898 /* 957789Sahrens * We don't allow recursive attributes.. 958789Sahrens * Maybe someday we will. 959789Sahrens */ 960789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 961789Sahrens ZFS_EXIT(zfsvfs); 962789Sahrens return (EINVAL); 963789Sahrens } 964789Sahrens 9653280Sck153898 if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr, flags)) { 966789Sahrens ZFS_EXIT(zfsvfs); 967789Sahrens return (error); 968789Sahrens } 969789Sahrens 970789Sahrens /* 971789Sahrens * Do we have permission to get into attribute directory? 972789Sahrens */ 973789Sahrens 974789Sahrens if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 975789Sahrens VN_RELE(*vpp); 976789Sahrens } 977789Sahrens 978789Sahrens ZFS_EXIT(zfsvfs); 979789Sahrens return (error); 980789Sahrens } 981789Sahrens 9821512Sek110237 if (dvp->v_type != VDIR) { 9831512Sek110237 ZFS_EXIT(zfsvfs); 9841460Smarks return (ENOTDIR); 9851512Sek110237 } 9861460Smarks 987789Sahrens /* 988789Sahrens * Check accessibility of directory. 989789Sahrens */ 990789Sahrens 991789Sahrens if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 992789Sahrens ZFS_EXIT(zfsvfs); 993789Sahrens return (error); 994789Sahrens } 995789Sahrens 996789Sahrens if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 997789Sahrens 998789Sahrens /* 999789Sahrens * Convert device special files 1000789Sahrens */ 1001789Sahrens if (IS_DEVVP(*vpp)) { 1002789Sahrens vnode_t *svp; 1003789Sahrens 1004789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1005789Sahrens VN_RELE(*vpp); 1006789Sahrens if (svp == NULL) 1007789Sahrens error = ENOSYS; 1008789Sahrens else 1009789Sahrens *vpp = svp; 1010789Sahrens } 1011789Sahrens } 1012789Sahrens 1013789Sahrens ZFS_EXIT(zfsvfs); 1014789Sahrens return (error); 1015789Sahrens } 1016789Sahrens 1017789Sahrens /* 1018789Sahrens * Attempt to create a new entry in a directory. If the entry 1019789Sahrens * already exists, truncate the file if permissible, else return 1020789Sahrens * an error. Return the vp of the created or trunc'd file. 1021789Sahrens * 1022789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1023789Sahrens * name - name of new file entry. 1024789Sahrens * vap - attributes of new file. 1025789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1026789Sahrens * mode - mode to open file with. 1027789Sahrens * cr - credentials of caller. 1028789Sahrens * flag - large file flag [UNUSED]. 1029789Sahrens * 1030789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1031789Sahrens * 1032789Sahrens * RETURN: 0 if success 1033789Sahrens * error code if failure 1034789Sahrens * 1035789Sahrens * Timestamps: 1036789Sahrens * dvp - ctime|mtime updated if new entry created 1037789Sahrens * vp - ctime|mtime always, atime if new 1038789Sahrens */ 1039789Sahrens /* ARGSUSED */ 1040789Sahrens static int 1041789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1042789Sahrens int mode, vnode_t **vpp, cred_t *cr, int flag) 1043789Sahrens { 1044789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1045789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1046789Sahrens zilog_t *zilog = zfsvfs->z_log; 1047789Sahrens objset_t *os = zfsvfs->z_os; 1048789Sahrens zfs_dirlock_t *dl; 1049789Sahrens dmu_tx_t *tx; 1050789Sahrens int error; 1051789Sahrens uint64_t zoid; 1052789Sahrens 1053789Sahrens ZFS_ENTER(zfsvfs); 1054789Sahrens 1055789Sahrens top: 1056789Sahrens *vpp = NULL; 1057789Sahrens 1058789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1059789Sahrens vap->va_mode &= ~VSVTX; 1060789Sahrens 1061789Sahrens if (*name == '\0') { 1062789Sahrens /* 1063789Sahrens * Null component name refers to the directory itself. 1064789Sahrens */ 1065789Sahrens VN_HOLD(dvp); 1066789Sahrens zp = dzp; 1067789Sahrens dl = NULL; 1068789Sahrens error = 0; 1069789Sahrens } else { 1070789Sahrens /* possible VN_HOLD(zp) */ 1071789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1072789Sahrens if (strcmp(name, "..") == 0) 1073789Sahrens error = EISDIR; 1074789Sahrens ZFS_EXIT(zfsvfs); 1075789Sahrens return (error); 1076789Sahrens } 1077789Sahrens } 1078789Sahrens 1079789Sahrens zoid = zp ? zp->z_id : -1ULL; 1080789Sahrens 1081789Sahrens if (zp == NULL) { 1082789Sahrens /* 1083789Sahrens * Create a new file object and update the directory 1084789Sahrens * to reference it. 1085789Sahrens */ 1086789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1087789Sahrens goto out; 1088789Sahrens } 1089789Sahrens 1090789Sahrens /* 1091789Sahrens * We only support the creation of regular files in 1092789Sahrens * extended attribute directories. 1093789Sahrens */ 1094789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1095789Sahrens (vap->va_type != VREG)) { 1096789Sahrens error = EINVAL; 1097789Sahrens goto out; 1098789Sahrens } 1099789Sahrens 1100789Sahrens tx = dmu_tx_create(os); 1101789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1102789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 11031544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1104789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1105789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1106789Sahrens 0, SPA_MAXBLOCKSIZE); 1107789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1108789Sahrens if (error) { 1109789Sahrens zfs_dirent_unlock(dl); 1110789Sahrens if (error == ERESTART && 1111789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 11122113Sahrens dmu_tx_wait(tx); 11132113Sahrens dmu_tx_abort(tx); 1114789Sahrens goto top; 1115789Sahrens } 11162113Sahrens dmu_tx_abort(tx); 1117789Sahrens ZFS_EXIT(zfsvfs); 1118789Sahrens return (error); 1119789Sahrens } 1120789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1121789Sahrens ASSERT(zp->z_id == zoid); 1122789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 11232638Sperrin zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1124789Sahrens dmu_tx_commit(tx); 1125789Sahrens } else { 1126789Sahrens /* 1127789Sahrens * A directory entry already exists for this name. 1128789Sahrens */ 1129789Sahrens /* 1130789Sahrens * Can't truncate an existing file if in exclusive mode. 1131789Sahrens */ 1132789Sahrens if (excl == EXCL) { 1133789Sahrens error = EEXIST; 1134789Sahrens goto out; 1135789Sahrens } 1136789Sahrens /* 1137789Sahrens * Can't open a directory for writing. 1138789Sahrens */ 1139789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1140789Sahrens error = EISDIR; 1141789Sahrens goto out; 1142789Sahrens } 1143789Sahrens /* 1144789Sahrens * Verify requested access to file. 1145789Sahrens */ 1146789Sahrens if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1147789Sahrens goto out; 1148789Sahrens } 1149789Sahrens 1150789Sahrens mutex_enter(&dzp->z_lock); 1151789Sahrens dzp->z_seq++; 1152789Sahrens mutex_exit(&dzp->z_lock); 1153789Sahrens 11541878Smaybee /* 11551878Smaybee * Truncate regular files if requested. 11561878Smaybee */ 11571878Smaybee if ((ZTOV(zp)->v_type == VREG) && 1158789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 11591878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 11601878Smaybee if (error == ERESTART && 11611878Smaybee zfsvfs->z_assign == TXG_NOWAIT) { 11622113Sahrens /* NB: we already did dmu_tx_wait() */ 11631878Smaybee zfs_dirent_unlock(dl); 11642365Sperrin VN_RELE(ZTOV(zp)); 11651878Smaybee goto top; 1166789Sahrens } 1167789Sahrens } 1168789Sahrens } 1169789Sahrens out: 1170789Sahrens 1171789Sahrens if (dl) 1172789Sahrens zfs_dirent_unlock(dl); 1173789Sahrens 1174789Sahrens if (error) { 1175789Sahrens if (zp) 1176789Sahrens VN_RELE(ZTOV(zp)); 1177789Sahrens } else { 1178789Sahrens *vpp = ZTOV(zp); 1179789Sahrens /* 1180789Sahrens * If vnode is for a device return a specfs vnode instead. 1181789Sahrens */ 1182789Sahrens if (IS_DEVVP(*vpp)) { 1183789Sahrens struct vnode *svp; 1184789Sahrens 1185789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1186789Sahrens VN_RELE(*vpp); 1187789Sahrens if (svp == NULL) { 1188789Sahrens error = ENOSYS; 1189789Sahrens } 1190789Sahrens *vpp = svp; 1191789Sahrens } 1192789Sahrens } 1193789Sahrens 1194789Sahrens ZFS_EXIT(zfsvfs); 1195789Sahrens return (error); 1196789Sahrens } 1197789Sahrens 1198789Sahrens /* 1199789Sahrens * Remove an entry from a directory. 1200789Sahrens * 1201789Sahrens * IN: dvp - vnode of directory to remove entry from. 1202789Sahrens * name - name of entry to remove. 1203789Sahrens * cr - credentials of caller. 1204789Sahrens * 1205789Sahrens * RETURN: 0 if success 1206789Sahrens * error code if failure 1207789Sahrens * 1208789Sahrens * Timestamps: 1209789Sahrens * dvp - ctime|mtime 1210789Sahrens * vp - ctime (if nlink > 0) 1211789Sahrens */ 1212789Sahrens static int 1213789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1214789Sahrens { 1215789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1216789Sahrens znode_t *xzp = NULL; 1217789Sahrens vnode_t *vp; 1218789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1219789Sahrens zilog_t *zilog = zfsvfs->z_log; 1220789Sahrens uint64_t acl_obj, xattr_obj; 1221789Sahrens zfs_dirlock_t *dl; 1222789Sahrens dmu_tx_t *tx; 12233461Sahrens boolean_t may_delete_now, delete_now = FALSE; 12243461Sahrens boolean_t unlinked; 1225789Sahrens int error; 1226789Sahrens 1227789Sahrens ZFS_ENTER(zfsvfs); 1228789Sahrens 1229789Sahrens top: 1230789Sahrens /* 1231789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1232789Sahrens */ 1233789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1234789Sahrens ZFS_EXIT(zfsvfs); 1235789Sahrens return (error); 1236789Sahrens } 1237789Sahrens 1238789Sahrens vp = ZTOV(zp); 1239789Sahrens 1240789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1241789Sahrens goto out; 1242789Sahrens } 1243789Sahrens 1244789Sahrens /* 1245789Sahrens * Need to use rmdir for removing directories. 1246789Sahrens */ 1247789Sahrens if (vp->v_type == VDIR) { 1248789Sahrens error = EPERM; 1249789Sahrens goto out; 1250789Sahrens } 1251789Sahrens 1252789Sahrens vnevent_remove(vp); 1253789Sahrens 12541484Sek110237 dnlc_remove(dvp, name); 12551484Sek110237 1256789Sahrens mutex_enter(&vp->v_lock); 1257789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1258789Sahrens mutex_exit(&vp->v_lock); 1259789Sahrens 1260789Sahrens /* 12613461Sahrens * We may delete the znode now, or we may put it in the unlinked set; 1262789Sahrens * it depends on whether we're the last link, and on whether there are 1263789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1264789Sahrens * allow for either case. 1265789Sahrens */ 1266789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 12671544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1268789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 1269789Sahrens if (may_delete_now) 1270789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1271789Sahrens 1272789Sahrens /* are there any extended attributes? */ 1273789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1274789Sahrens /* XXX - do we need this if we are deleting? */ 1275789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1276789Sahrens } 1277789Sahrens 1278789Sahrens /* are there any additional acls */ 1279789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1280789Sahrens may_delete_now) 1281789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1282789Sahrens 1283789Sahrens /* charge as an update -- would be nice not to charge at all */ 12843461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1285789Sahrens 1286789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1287789Sahrens if (error) { 1288789Sahrens zfs_dirent_unlock(dl); 1289789Sahrens VN_RELE(vp); 1290789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 12912113Sahrens dmu_tx_wait(tx); 12922113Sahrens dmu_tx_abort(tx); 1293789Sahrens goto top; 1294789Sahrens } 12952113Sahrens dmu_tx_abort(tx); 1296789Sahrens ZFS_EXIT(zfsvfs); 1297789Sahrens return (error); 1298789Sahrens } 1299789Sahrens 1300789Sahrens /* 1301789Sahrens * Remove the directory entry. 1302789Sahrens */ 13033461Sahrens error = zfs_link_destroy(dl, zp, tx, 0, &unlinked); 1304789Sahrens 1305789Sahrens if (error) { 1306789Sahrens dmu_tx_commit(tx); 1307789Sahrens goto out; 1308789Sahrens } 1309789Sahrens 13103461Sahrens if (unlinked) { 1311789Sahrens mutex_enter(&vp->v_lock); 1312789Sahrens delete_now = may_delete_now && 1313789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1314789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1315789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1316789Sahrens mutex_exit(&vp->v_lock); 1317789Sahrens } 1318789Sahrens 1319789Sahrens if (delete_now) { 1320789Sahrens if (zp->z_phys->zp_xattr) { 1321789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1322789Sahrens ASSERT3U(error, ==, 0); 1323789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1324789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1325789Sahrens mutex_enter(&xzp->z_lock); 13263461Sahrens xzp->z_unlinked = 1; 1327789Sahrens xzp->z_phys->zp_links = 0; 1328789Sahrens mutex_exit(&xzp->z_lock); 13293461Sahrens zfs_unlinked_add(xzp, tx); 1330789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1331789Sahrens } 1332789Sahrens mutex_enter(&zp->z_lock); 1333789Sahrens mutex_enter(&vp->v_lock); 1334789Sahrens vp->v_count--; 1335789Sahrens ASSERT3U(vp->v_count, ==, 0); 1336789Sahrens mutex_exit(&vp->v_lock); 1337789Sahrens mutex_exit(&zp->z_lock); 1338789Sahrens zfs_znode_delete(zp, tx); 1339789Sahrens VFS_RELE(zfsvfs->z_vfs); 13403461Sahrens } else if (unlinked) { 13413461Sahrens zfs_unlinked_add(zp, tx); 1342789Sahrens } 1343789Sahrens 13442638Sperrin zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1345789Sahrens 1346789Sahrens dmu_tx_commit(tx); 1347789Sahrens out: 1348789Sahrens zfs_dirent_unlock(dl); 1349789Sahrens 1350789Sahrens if (!delete_now) { 1351789Sahrens VN_RELE(vp); 1352789Sahrens } else if (xzp) { 1353789Sahrens /* this rele delayed to prevent nesting transactions */ 1354789Sahrens VN_RELE(ZTOV(xzp)); 1355789Sahrens } 1356789Sahrens 1357789Sahrens ZFS_EXIT(zfsvfs); 1358789Sahrens return (error); 1359789Sahrens } 1360789Sahrens 1361789Sahrens /* 1362789Sahrens * Create a new directory and insert it into dvp using the name 1363789Sahrens * provided. Return a pointer to the inserted directory. 1364789Sahrens * 1365789Sahrens * IN: dvp - vnode of directory to add subdir to. 1366789Sahrens * dirname - name of new directory. 1367789Sahrens * vap - attributes of new directory. 1368789Sahrens * cr - credentials of caller. 1369789Sahrens * 1370789Sahrens * OUT: vpp - vnode of created directory. 1371789Sahrens * 1372789Sahrens * RETURN: 0 if success 1373789Sahrens * error code if failure 1374789Sahrens * 1375789Sahrens * Timestamps: 1376789Sahrens * dvp - ctime|mtime updated 1377789Sahrens * vp - ctime|mtime|atime updated 1378789Sahrens */ 1379789Sahrens static int 1380789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1381789Sahrens { 1382789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1383789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1384789Sahrens zilog_t *zilog = zfsvfs->z_log; 1385789Sahrens zfs_dirlock_t *dl; 1386789Sahrens uint64_t zoid = 0; 1387789Sahrens dmu_tx_t *tx; 1388789Sahrens int error; 1389789Sahrens 1390789Sahrens ASSERT(vap->va_type == VDIR); 1391789Sahrens 1392789Sahrens ZFS_ENTER(zfsvfs); 1393789Sahrens 1394789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1395789Sahrens ZFS_EXIT(zfsvfs); 1396789Sahrens return (EINVAL); 1397789Sahrens } 1398789Sahrens top: 1399789Sahrens *vpp = NULL; 1400789Sahrens 1401789Sahrens /* 1402789Sahrens * First make sure the new directory doesn't exist. 1403789Sahrens */ 1404789Sahrens if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1405789Sahrens ZFS_EXIT(zfsvfs); 1406789Sahrens return (error); 1407789Sahrens } 1408789Sahrens 14091231Smarks if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 14101231Smarks zfs_dirent_unlock(dl); 14111231Smarks ZFS_EXIT(zfsvfs); 14121231Smarks return (error); 14131231Smarks } 14141231Smarks 1415789Sahrens /* 1416789Sahrens * Add a new entry to the directory. 1417789Sahrens */ 1418789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14191544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 14201544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1421789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1422789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1423789Sahrens 0, SPA_MAXBLOCKSIZE); 1424789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1425789Sahrens if (error) { 1426789Sahrens zfs_dirent_unlock(dl); 1427789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 14282113Sahrens dmu_tx_wait(tx); 14292113Sahrens dmu_tx_abort(tx); 1430789Sahrens goto top; 1431789Sahrens } 14322113Sahrens dmu_tx_abort(tx); 1433789Sahrens ZFS_EXIT(zfsvfs); 1434789Sahrens return (error); 1435789Sahrens } 1436789Sahrens 1437789Sahrens /* 1438789Sahrens * Create new node. 1439789Sahrens */ 1440789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1441789Sahrens 1442789Sahrens /* 1443789Sahrens * Now put new name in parent dir. 1444789Sahrens */ 1445789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1446789Sahrens 1447789Sahrens *vpp = ZTOV(zp); 1448789Sahrens 14492638Sperrin zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1450789Sahrens dmu_tx_commit(tx); 1451789Sahrens 1452789Sahrens zfs_dirent_unlock(dl); 1453789Sahrens 1454789Sahrens ZFS_EXIT(zfsvfs); 1455789Sahrens return (0); 1456789Sahrens } 1457789Sahrens 1458789Sahrens /* 1459789Sahrens * Remove a directory subdir entry. If the current working 1460789Sahrens * directory is the same as the subdir to be removed, the 1461789Sahrens * remove will fail. 1462789Sahrens * 1463789Sahrens * IN: dvp - vnode of directory to remove from. 1464789Sahrens * name - name of directory to be removed. 1465789Sahrens * cwd - vnode of current working directory. 1466789Sahrens * cr - credentials of caller. 1467789Sahrens * 1468789Sahrens * RETURN: 0 if success 1469789Sahrens * error code if failure 1470789Sahrens * 1471789Sahrens * Timestamps: 1472789Sahrens * dvp - ctime|mtime updated 1473789Sahrens */ 1474789Sahrens static int 1475789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1476789Sahrens { 1477789Sahrens znode_t *dzp = VTOZ(dvp); 1478789Sahrens znode_t *zp; 1479789Sahrens vnode_t *vp; 1480789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1481789Sahrens zilog_t *zilog = zfsvfs->z_log; 1482789Sahrens zfs_dirlock_t *dl; 1483789Sahrens dmu_tx_t *tx; 1484789Sahrens int error; 1485789Sahrens 1486789Sahrens ZFS_ENTER(zfsvfs); 1487789Sahrens 1488789Sahrens top: 1489789Sahrens zp = NULL; 1490789Sahrens 1491789Sahrens /* 1492789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1493789Sahrens */ 1494789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1495789Sahrens ZFS_EXIT(zfsvfs); 1496789Sahrens return (error); 1497789Sahrens } 1498789Sahrens 1499789Sahrens vp = ZTOV(zp); 1500789Sahrens 1501789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1502789Sahrens goto out; 1503789Sahrens } 1504789Sahrens 1505789Sahrens if (vp->v_type != VDIR) { 1506789Sahrens error = ENOTDIR; 1507789Sahrens goto out; 1508789Sahrens } 1509789Sahrens 1510789Sahrens if (vp == cwd) { 1511789Sahrens error = EINVAL; 1512789Sahrens goto out; 1513789Sahrens } 1514789Sahrens 1515789Sahrens vnevent_rmdir(vp); 1516789Sahrens 1517789Sahrens /* 15183897Smaybee * Grab a lock on the directory to make sure that noone is 15193897Smaybee * trying to add (or lookup) entries while we are removing it. 15203897Smaybee */ 15213897Smaybee rw_enter(&zp->z_name_lock, RW_WRITER); 15223897Smaybee 15233897Smaybee /* 15243897Smaybee * Grab a lock on the parent pointer to make sure we play well 1525789Sahrens * with the treewalk and directory rename code. 1526789Sahrens */ 1527789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1528789Sahrens 1529789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 15301544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1531789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 15323461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 1533789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1534789Sahrens if (error) { 1535789Sahrens rw_exit(&zp->z_parent_lock); 15363897Smaybee rw_exit(&zp->z_name_lock); 1537789Sahrens zfs_dirent_unlock(dl); 1538789Sahrens VN_RELE(vp); 1539789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 15402113Sahrens dmu_tx_wait(tx); 15412113Sahrens dmu_tx_abort(tx); 1542789Sahrens goto top; 1543789Sahrens } 15442113Sahrens dmu_tx_abort(tx); 1545789Sahrens ZFS_EXIT(zfsvfs); 1546789Sahrens return (error); 1547789Sahrens } 1548789Sahrens 1549789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1550789Sahrens 1551789Sahrens if (error == 0) 15522638Sperrin zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1553789Sahrens 1554789Sahrens dmu_tx_commit(tx); 1555789Sahrens 1556789Sahrens rw_exit(&zp->z_parent_lock); 15573897Smaybee rw_exit(&zp->z_name_lock); 1558789Sahrens out: 1559789Sahrens zfs_dirent_unlock(dl); 1560789Sahrens 1561789Sahrens VN_RELE(vp); 1562789Sahrens 1563789Sahrens ZFS_EXIT(zfsvfs); 1564789Sahrens return (error); 1565789Sahrens } 1566789Sahrens 1567789Sahrens /* 1568789Sahrens * Read as many directory entries as will fit into the provided 1569789Sahrens * buffer from the given directory cursor position (specified in 1570789Sahrens * the uio structure. 1571789Sahrens * 1572789Sahrens * IN: vp - vnode of directory to read. 1573789Sahrens * uio - structure supplying read location, range info, 1574789Sahrens * and return buffer. 1575789Sahrens * cr - credentials of caller. 1576789Sahrens * 1577789Sahrens * OUT: uio - updated offset and range, buffer filled. 1578789Sahrens * eofp - set to true if end-of-file detected. 1579789Sahrens * 1580789Sahrens * RETURN: 0 if success 1581789Sahrens * error code if failure 1582789Sahrens * 1583789Sahrens * Timestamps: 1584789Sahrens * vp - atime updated 1585789Sahrens * 1586789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1587789Sahrens * This allows us to use the low range for "special" directory entries: 1588789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1589789Sahrens * we use the offset 2 for the '.zfs' directory. 1590789Sahrens */ 1591789Sahrens /* ARGSUSED */ 1592789Sahrens static int 1593789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1594789Sahrens { 1595789Sahrens znode_t *zp = VTOZ(vp); 1596789Sahrens iovec_t *iovp; 1597789Sahrens dirent64_t *odp; 1598789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1599869Sperrin objset_t *os; 1600789Sahrens caddr_t outbuf; 1601789Sahrens size_t bufsize; 1602789Sahrens zap_cursor_t zc; 1603789Sahrens zap_attribute_t zap; 1604789Sahrens uint_t bytes_wanted; 1605789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1606789Sahrens int local_eof; 1607869Sperrin int outcount; 1608869Sperrin int error; 1609869Sperrin uint8_t prefetch; 1610789Sahrens 1611789Sahrens ZFS_ENTER(zfsvfs); 1612789Sahrens 1613789Sahrens /* 1614789Sahrens * If we are not given an eof variable, 1615789Sahrens * use a local one. 1616789Sahrens */ 1617789Sahrens if (eofp == NULL) 1618789Sahrens eofp = &local_eof; 1619789Sahrens 1620789Sahrens /* 1621789Sahrens * Check for valid iov_len. 1622789Sahrens */ 1623789Sahrens if (uio->uio_iov->iov_len <= 0) { 1624789Sahrens ZFS_EXIT(zfsvfs); 1625789Sahrens return (EINVAL); 1626789Sahrens } 1627789Sahrens 1628789Sahrens /* 1629789Sahrens * Quit if directory has been removed (posix) 1630789Sahrens */ 16313461Sahrens if ((*eofp = zp->z_unlinked) != 0) { 1632789Sahrens ZFS_EXIT(zfsvfs); 1633789Sahrens return (0); 1634789Sahrens } 1635789Sahrens 1636869Sperrin error = 0; 1637869Sperrin os = zfsvfs->z_os; 1638869Sperrin offset = uio->uio_loffset; 1639869Sperrin prefetch = zp->z_zn_prefetch; 1640869Sperrin 1641789Sahrens /* 1642789Sahrens * Initialize the iterator cursor. 1643789Sahrens */ 1644789Sahrens if (offset <= 3) { 1645789Sahrens /* 1646789Sahrens * Start iteration from the beginning of the directory. 1647789Sahrens */ 1648869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1649789Sahrens } else { 1650789Sahrens /* 1651789Sahrens * The offset is a serialized cursor. 1652789Sahrens */ 1653869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1654789Sahrens } 1655789Sahrens 1656789Sahrens /* 1657789Sahrens * Get space to change directory entries into fs independent format. 1658789Sahrens */ 1659789Sahrens iovp = uio->uio_iov; 1660789Sahrens bytes_wanted = iovp->iov_len; 1661789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1662789Sahrens bufsize = bytes_wanted; 1663789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1664789Sahrens odp = (struct dirent64 *)outbuf; 1665789Sahrens } else { 1666789Sahrens bufsize = bytes_wanted; 1667789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1668789Sahrens } 1669789Sahrens 1670789Sahrens /* 1671789Sahrens * Transform to file-system independent format 1672789Sahrens */ 1673789Sahrens outcount = 0; 1674789Sahrens while (outcount < bytes_wanted) { 16753912Slling ino64_t objnum; 16763912Slling ushort_t reclen; 16773912Slling off64_t *next; 16783912Slling 1679789Sahrens /* 1680789Sahrens * Special case `.', `..', and `.zfs'. 1681789Sahrens */ 1682789Sahrens if (offset == 0) { 1683789Sahrens (void) strcpy(zap.za_name, "."); 16843912Slling objnum = zp->z_id; 1685789Sahrens } else if (offset == 1) { 1686789Sahrens (void) strcpy(zap.za_name, ".."); 16873912Slling objnum = zp->z_phys->zp_parent; 1688789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1689789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 16903912Slling objnum = ZFSCTL_INO_ROOT; 1691789Sahrens } else { 1692789Sahrens /* 1693789Sahrens * Grab next entry. 1694789Sahrens */ 1695789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1696789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1697789Sahrens break; 1698789Sahrens else 1699789Sahrens goto update; 1700789Sahrens } 1701789Sahrens 1702789Sahrens if (zap.za_integer_length != 8 || 1703789Sahrens zap.za_num_integers != 1) { 1704789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1705789Sahrens "entry, obj = %lld, offset = %lld\n", 1706789Sahrens (u_longlong_t)zp->z_id, 1707789Sahrens (u_longlong_t)offset); 1708789Sahrens error = ENXIO; 1709789Sahrens goto update; 1710789Sahrens } 17113912Slling 17123912Slling objnum = ZFS_DIRENT_OBJ(zap.za_first_integer); 17133912Slling /* 17143912Slling * MacOS X can extract the object type here such as: 17153912Slling * uint8_t type = ZFS_DIRENT_TYPE(zap.za_first_integer); 17163912Slling */ 1717789Sahrens } 17183912Slling reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1719789Sahrens 1720789Sahrens /* 1721789Sahrens * Will this entry fit in the buffer? 1722789Sahrens */ 17233912Slling if (outcount + reclen > bufsize) { 1724789Sahrens /* 1725789Sahrens * Did we manage to fit anything in the buffer? 1726789Sahrens */ 1727789Sahrens if (!outcount) { 1728789Sahrens error = EINVAL; 1729789Sahrens goto update; 1730789Sahrens } 1731789Sahrens break; 1732789Sahrens } 1733789Sahrens /* 1734789Sahrens * Add this entry: 1735789Sahrens */ 17363912Slling odp->d_ino = objnum; 17373912Slling odp->d_reclen = reclen; 1738789Sahrens /* NOTE: d_off is the offset for the *next* entry */ 1739789Sahrens next = &(odp->d_off); 1740789Sahrens (void) strncpy(odp->d_name, zap.za_name, 17413912Slling DIRENT64_NAMELEN(reclen)); 17423912Slling outcount += reclen; 17433912Slling odp = (dirent64_t *)((intptr_t)odp + reclen); 1744789Sahrens 1745789Sahrens ASSERT(outcount <= bufsize); 1746789Sahrens 1747789Sahrens /* Prefetch znode */ 1748869Sperrin if (prefetch) 17493912Slling dmu_prefetch(os, objnum, 0, 0); 1750789Sahrens 1751789Sahrens /* 1752789Sahrens * Move to the next entry, fill in the previous offset. 1753789Sahrens */ 1754789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1755789Sahrens zap_cursor_advance(&zc); 1756789Sahrens offset = zap_cursor_serialize(&zc); 1757789Sahrens } else { 1758789Sahrens offset += 1; 1759789Sahrens } 1760789Sahrens *next = offset; 1761789Sahrens } 1762869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1763789Sahrens 1764789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1765789Sahrens iovp->iov_base += outcount; 1766789Sahrens iovp->iov_len -= outcount; 1767789Sahrens uio->uio_resid -= outcount; 1768789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1769789Sahrens /* 1770789Sahrens * Reset the pointer. 1771789Sahrens */ 1772789Sahrens offset = uio->uio_loffset; 1773789Sahrens } 1774789Sahrens 1775789Sahrens update: 1776885Sahrens zap_cursor_fini(&zc); 1777789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1778789Sahrens kmem_free(outbuf, bufsize); 1779789Sahrens 1780789Sahrens if (error == ENOENT) 1781789Sahrens error = 0; 1782789Sahrens 1783789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1784789Sahrens 1785789Sahrens uio->uio_loffset = offset; 1786789Sahrens ZFS_EXIT(zfsvfs); 1787789Sahrens return (error); 1788789Sahrens } 1789789Sahrens 1790789Sahrens static int 1791789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1792789Sahrens { 1793789Sahrens znode_t *zp = VTOZ(vp); 1794789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1795789Sahrens 17961773Seschrock /* 17971773Seschrock * Regardless of whether this is required for standards conformance, 17981773Seschrock * this is the logical behavior when fsync() is called on a file with 17991773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 18001773Seschrock * going to be pushed out as part of the zil_commit(). 18011773Seschrock */ 18021773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 18031773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 18041773Seschrock (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 18051773Seschrock 1806789Sahrens ZFS_ENTER(zfsvfs); 18072638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 1808789Sahrens ZFS_EXIT(zfsvfs); 1809789Sahrens return (0); 1810789Sahrens } 1811789Sahrens 1812789Sahrens /* 1813789Sahrens * Get the requested file attributes and place them in the provided 1814789Sahrens * vattr structure. 1815789Sahrens * 1816789Sahrens * IN: vp - vnode of file. 1817789Sahrens * vap - va_mask identifies requested attributes. 1818789Sahrens * flags - [UNUSED] 1819789Sahrens * cr - credentials of caller. 1820789Sahrens * 1821789Sahrens * OUT: vap - attribute values. 1822789Sahrens * 1823789Sahrens * RETURN: 0 (always succeeds) 1824789Sahrens */ 1825789Sahrens /* ARGSUSED */ 1826789Sahrens static int 1827789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1828789Sahrens { 1829789Sahrens znode_t *zp = VTOZ(vp); 1830789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1831789Sahrens znode_phys_t *pzp = zp->z_phys; 1832789Sahrens int error; 1833*4543Smarks uint64_t links; 1834789Sahrens 1835789Sahrens ZFS_ENTER(zfsvfs); 1836789Sahrens 1837789Sahrens /* 1838789Sahrens * Return all attributes. It's cheaper to provide the answer 1839789Sahrens * than to determine whether we were asked the question. 1840789Sahrens */ 1841789Sahrens mutex_enter(&zp->z_lock); 1842789Sahrens 1843789Sahrens vap->va_type = vp->v_type; 1844789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 1845789Sahrens vap->va_uid = zp->z_phys->zp_uid; 1846789Sahrens vap->va_gid = zp->z_phys->zp_gid; 1847789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1848789Sahrens vap->va_nodeid = zp->z_id; 1849*4543Smarks if ((vp->v_flag & VROOT) && zfs_show_ctldir(zp)) 1850*4543Smarks links = pzp->zp_links + 1; 1851*4543Smarks else 1852*4543Smarks links = pzp->zp_links; 1853*4543Smarks vap->va_nlink = MIN(links, UINT32_MAX); /* nlink_t limit! */ 1854789Sahrens vap->va_size = pzp->zp_size; 18551816Smarks vap->va_rdev = vp->v_rdev; 1856789Sahrens vap->va_seq = zp->z_seq; 1857789Sahrens 1858789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1859789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1860789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1861789Sahrens 1862789Sahrens /* 1863905Smarks * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1864905Smarks * Also, if we are the owner don't bother, since owner should 1865905Smarks * always be allowed to read basic attributes of file. 1866789Sahrens */ 1867905Smarks if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1868905Smarks (zp->z_phys->zp_uid != crgetuid(cr))) { 1869905Smarks if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1870789Sahrens mutex_exit(&zp->z_lock); 1871789Sahrens ZFS_EXIT(zfsvfs); 1872789Sahrens return (error); 1873789Sahrens } 1874789Sahrens } 1875789Sahrens 1876789Sahrens mutex_exit(&zp->z_lock); 1877789Sahrens 1878789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1879789Sahrens 1880789Sahrens if (zp->z_blksz == 0) { 1881789Sahrens /* 1882789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 1883789Sahrens */ 1884789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 1885789Sahrens } 1886789Sahrens 1887789Sahrens ZFS_EXIT(zfsvfs); 1888789Sahrens return (0); 1889789Sahrens } 1890789Sahrens 1891789Sahrens /* 1892789Sahrens * Set the file attributes to the values contained in the 1893789Sahrens * vattr structure. 1894789Sahrens * 1895789Sahrens * IN: vp - vnode of file to be modified. 1896789Sahrens * vap - new attribute values. 1897789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 1898789Sahrens * cr - credentials of caller. 1899789Sahrens * 1900789Sahrens * RETURN: 0 if success 1901789Sahrens * error code if failure 1902789Sahrens * 1903789Sahrens * Timestamps: 1904789Sahrens * vp - ctime updated, mtime updated if size changed. 1905789Sahrens */ 1906789Sahrens /* ARGSUSED */ 1907789Sahrens static int 1908789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1909789Sahrens caller_context_t *ct) 1910789Sahrens { 1911789Sahrens struct znode *zp = VTOZ(vp); 1912789Sahrens znode_phys_t *pzp = zp->z_phys; 1913789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1914789Sahrens zilog_t *zilog = zfsvfs->z_log; 1915789Sahrens dmu_tx_t *tx; 19161878Smaybee vattr_t oldva; 1917789Sahrens uint_t mask = vap->va_mask; 19181878Smaybee uint_t saved_mask; 19192796Smarks int trim_mask = 0; 1920789Sahrens uint64_t new_mode; 19211231Smarks znode_t *attrzp; 1922789Sahrens int need_policy = FALSE; 1923789Sahrens int err; 1924789Sahrens 1925789Sahrens if (mask == 0) 1926789Sahrens return (0); 1927789Sahrens 1928789Sahrens if (mask & AT_NOSET) 1929789Sahrens return (EINVAL); 1930789Sahrens 1931789Sahrens if (mask & AT_SIZE && vp->v_type == VDIR) 1932789Sahrens return (EISDIR); 1933789Sahrens 19341394Smarks if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 19351308Smarks return (EINVAL); 19361308Smarks 1937789Sahrens ZFS_ENTER(zfsvfs); 1938789Sahrens 1939789Sahrens top: 19401231Smarks attrzp = NULL; 1941789Sahrens 1942789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1943789Sahrens ZFS_EXIT(zfsvfs); 1944789Sahrens return (EROFS); 1945789Sahrens } 1946789Sahrens 1947789Sahrens /* 1948789Sahrens * First validate permissions 1949789Sahrens */ 1950789Sahrens 1951789Sahrens if (mask & AT_SIZE) { 1952789Sahrens err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1953789Sahrens if (err) { 1954789Sahrens ZFS_EXIT(zfsvfs); 1955789Sahrens return (err); 1956789Sahrens } 19571878Smaybee /* 19581878Smaybee * XXX - Note, we are not providing any open 19591878Smaybee * mode flags here (like FNDELAY), so we may 19601878Smaybee * block if there are locks present... this 19611878Smaybee * should be addressed in openat(). 19621878Smaybee */ 19631878Smaybee do { 19641878Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 19652113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 19661878Smaybee } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 19671878Smaybee if (err) { 19681878Smaybee ZFS_EXIT(zfsvfs); 19691878Smaybee return (err); 19701878Smaybee } 1971789Sahrens } 1972789Sahrens 1973789Sahrens if (mask & (AT_ATIME|AT_MTIME)) 1974789Sahrens need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 1975789Sahrens 1976789Sahrens if (mask & (AT_UID|AT_GID)) { 1977789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 1978789Sahrens int take_owner; 1979789Sahrens int take_group; 1980789Sahrens 1981789Sahrens /* 1982913Smarks * NOTE: even if a new mode is being set, 1983913Smarks * we may clear S_ISUID/S_ISGID bits. 1984913Smarks */ 1985913Smarks 1986913Smarks if (!(mask & AT_MODE)) 1987913Smarks vap->va_mode = pzp->zp_mode; 1988913Smarks 1989913Smarks /* 1990789Sahrens * Take ownership or chgrp to group we are a member of 1991789Sahrens */ 1992789Sahrens 1993789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 1994789Sahrens take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 1995789Sahrens 1996789Sahrens /* 1997789Sahrens * If both AT_UID and AT_GID are set then take_owner and 1998789Sahrens * take_group must both be set in order to allow taking 1999789Sahrens * ownership. 2000789Sahrens * 2001789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2002789Sahrens * 2003789Sahrens */ 2004789Sahrens 2005789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2006789Sahrens ((idmask == AT_UID) && take_owner) || 2007789Sahrens ((idmask == AT_GID) && take_group)) { 2008789Sahrens if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 2009789Sahrens /* 2010789Sahrens * Remove setuid/setgid for non-privileged users 2011789Sahrens */ 20121115Smarks secpolicy_setid_clear(vap, cr); 20132796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2014789Sahrens } else { 2015789Sahrens need_policy = TRUE; 2016789Sahrens } 2017789Sahrens } else { 2018789Sahrens need_policy = TRUE; 2019789Sahrens } 2020789Sahrens } 2021789Sahrens 20222796Smarks mutex_enter(&zp->z_lock); 20232796Smarks oldva.va_mode = pzp->zp_mode; 20242796Smarks oldva.va_uid = zp->z_phys->zp_uid; 20252796Smarks oldva.va_gid = zp->z_phys->zp_gid; 20262796Smarks mutex_exit(&zp->z_lock); 20272796Smarks 20282796Smarks if (mask & AT_MODE) { 20292796Smarks if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) { 20302796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 20312796Smarks &oldva, cr); 20322796Smarks if (err) { 20332796Smarks ZFS_EXIT(zfsvfs); 20342796Smarks return (err); 20352796Smarks } 20362796Smarks trim_mask |= AT_MODE; 20372796Smarks } else { 20382796Smarks need_policy = TRUE; 20392796Smarks } 20402796Smarks } 2041789Sahrens 2042789Sahrens if (need_policy) { 20431115Smarks /* 20441115Smarks * If trim_mask is set then take ownership 20452796Smarks * has been granted or write_acl is present and user 20462796Smarks * has the ability to modify mode. In that case remove 20472796Smarks * UID|GID and or MODE from mask so that 20481115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 20491115Smarks */ 20502796Smarks 20512796Smarks if (trim_mask) { 20522796Smarks saved_mask = vap->va_mask; 20532796Smarks vap->va_mask &= ~trim_mask; 20542796Smarks 20552796Smarks } 2056789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2057789Sahrens (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2058789Sahrens if (err) { 2059789Sahrens ZFS_EXIT(zfsvfs); 2060789Sahrens return (err); 2061789Sahrens } 20621115Smarks 20631115Smarks if (trim_mask) 20642796Smarks vap->va_mask |= saved_mask; 2065789Sahrens } 2066789Sahrens 2067789Sahrens /* 2068789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2069789Sahrens * changed va_mask 2070789Sahrens */ 2071789Sahrens mask = vap->va_mask; 2072789Sahrens 2073789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2074789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2075789Sahrens 2076789Sahrens if (mask & AT_MODE) { 20771576Smarks uint64_t pmode = pzp->zp_mode; 20781576Smarks 20791576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2080789Sahrens 2081789Sahrens if (zp->z_phys->zp_acl.z_acl_extern_obj) 2082789Sahrens dmu_tx_hold_write(tx, 2083789Sahrens pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2084789Sahrens else 2085789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2086789Sahrens 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2087789Sahrens } 2088789Sahrens 20891231Smarks if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 20901231Smarks err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 20911231Smarks if (err) { 20921231Smarks dmu_tx_abort(tx); 20931231Smarks ZFS_EXIT(zfsvfs); 20941231Smarks return (err); 20951231Smarks } 20961231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 20971231Smarks } 20981231Smarks 2099789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2100789Sahrens if (err) { 21011231Smarks if (attrzp) 21021231Smarks VN_RELE(ZTOV(attrzp)); 2103789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 21042113Sahrens dmu_tx_wait(tx); 21052113Sahrens dmu_tx_abort(tx); 2106789Sahrens goto top; 2107789Sahrens } 21082113Sahrens dmu_tx_abort(tx); 2109789Sahrens ZFS_EXIT(zfsvfs); 2110789Sahrens return (err); 2111789Sahrens } 2112789Sahrens 2113789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2114789Sahrens 2115789Sahrens /* 2116789Sahrens * Set each attribute requested. 2117789Sahrens * We group settings according to the locks they need to acquire. 2118789Sahrens * 2119789Sahrens * Note: you cannot set ctime directly, although it will be 2120789Sahrens * updated as a side-effect of calling this function. 2121789Sahrens */ 2122789Sahrens 2123789Sahrens mutex_enter(&zp->z_lock); 2124789Sahrens 2125789Sahrens if (mask & AT_MODE) { 2126789Sahrens err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2127789Sahrens ASSERT3U(err, ==, 0); 2128789Sahrens } 2129789Sahrens 21301231Smarks if (attrzp) 21311231Smarks mutex_enter(&attrzp->z_lock); 21321231Smarks 21331231Smarks if (mask & AT_UID) { 2134789Sahrens zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21351231Smarks if (attrzp) { 21361231Smarks attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21371231Smarks } 21381231Smarks } 21391231Smarks 21401231Smarks if (mask & AT_GID) { 2141789Sahrens zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21421231Smarks if (attrzp) 21431231Smarks attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21441231Smarks } 21451231Smarks 21461231Smarks if (attrzp) 21471231Smarks mutex_exit(&attrzp->z_lock); 2148789Sahrens 2149789Sahrens if (mask & AT_ATIME) 2150789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2151789Sahrens 2152789Sahrens if (mask & AT_MTIME) 2153789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2154789Sahrens 21551878Smaybee if (mask & AT_SIZE) 2156789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 21571878Smaybee else if (mask != 0) 2158789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2159789Sahrens 21601878Smaybee if (mask != 0) 21612638Sperrin zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2162789Sahrens 2163789Sahrens mutex_exit(&zp->z_lock); 2164789Sahrens 21651231Smarks if (attrzp) 21661231Smarks VN_RELE(ZTOV(attrzp)); 21671231Smarks 2168789Sahrens dmu_tx_commit(tx); 2169789Sahrens 2170789Sahrens ZFS_EXIT(zfsvfs); 2171789Sahrens return (err); 2172789Sahrens } 2173789Sahrens 21743271Smaybee typedef struct zfs_zlock { 21753271Smaybee krwlock_t *zl_rwlock; /* lock we acquired */ 21763271Smaybee znode_t *zl_znode; /* znode we held */ 21773271Smaybee struct zfs_zlock *zl_next; /* next in list */ 21783271Smaybee } zfs_zlock_t; 21793271Smaybee 21803271Smaybee /* 21813271Smaybee * Drop locks and release vnodes that were held by zfs_rename_lock(). 21823271Smaybee */ 21833271Smaybee static void 21843271Smaybee zfs_rename_unlock(zfs_zlock_t **zlpp) 21853271Smaybee { 21863271Smaybee zfs_zlock_t *zl; 21873271Smaybee 21883271Smaybee while ((zl = *zlpp) != NULL) { 21893271Smaybee if (zl->zl_znode != NULL) 21903271Smaybee VN_RELE(ZTOV(zl->zl_znode)); 21913271Smaybee rw_exit(zl->zl_rwlock); 21923271Smaybee *zlpp = zl->zl_next; 21933271Smaybee kmem_free(zl, sizeof (*zl)); 21943271Smaybee } 21953271Smaybee } 21963271Smaybee 2197789Sahrens /* 2198789Sahrens * Search back through the directory tree, using the ".." entries. 2199789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2200789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2201789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2202789Sahrens */ 2203789Sahrens static int 2204789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2205789Sahrens { 2206789Sahrens zfs_zlock_t *zl; 22073638Sbillm znode_t *zp = tdzp; 2208789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2209789Sahrens uint64_t *oidp = &zp->z_id; 2210789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2211789Sahrens krw_t rw = RW_WRITER; 2212789Sahrens 2213789Sahrens /* 2214789Sahrens * First pass write-locks szp and compares to zp->z_id. 2215789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2216789Sahrens */ 2217789Sahrens do { 22183271Smaybee if (!rw_tryenter(rwlp, rw)) { 22193271Smaybee /* 22203271Smaybee * Another thread is renaming in this path. 22213271Smaybee * Note that if we are a WRITER, we don't have any 22223271Smaybee * parent_locks held yet. 22233271Smaybee */ 22243271Smaybee if (rw == RW_READER && zp->z_id > szp->z_id) { 22253271Smaybee /* 22263271Smaybee * Drop our locks and restart 22273271Smaybee */ 22283271Smaybee zfs_rename_unlock(&zl); 22293271Smaybee *zlpp = NULL; 22303271Smaybee zp = tdzp; 22313271Smaybee oidp = &zp->z_id; 22323271Smaybee rwlp = &szp->z_parent_lock; 22333271Smaybee rw = RW_WRITER; 22343271Smaybee continue; 22353271Smaybee } else { 22363271Smaybee /* 22373271Smaybee * Wait for other thread to drop its locks 22383271Smaybee */ 22393271Smaybee rw_enter(rwlp, rw); 22403271Smaybee } 22413271Smaybee } 22423271Smaybee 2243789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2244789Sahrens zl->zl_rwlock = rwlp; 2245789Sahrens zl->zl_znode = NULL; 2246789Sahrens zl->zl_next = *zlpp; 2247789Sahrens *zlpp = zl; 2248789Sahrens 2249789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2250789Sahrens return (EINVAL); 2251789Sahrens 2252789Sahrens if (*oidp == rootid) /* We've hit the top */ 2253789Sahrens return (0); 2254789Sahrens 2255789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2256789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2257789Sahrens if (error) 2258789Sahrens return (error); 2259789Sahrens zl->zl_znode = zp; 2260789Sahrens } 2261789Sahrens oidp = &zp->z_phys->zp_parent; 2262789Sahrens rwlp = &zp->z_parent_lock; 2263789Sahrens rw = RW_READER; 2264789Sahrens 2265789Sahrens } while (zp->z_id != sdzp->z_id); 2266789Sahrens 2267789Sahrens return (0); 2268789Sahrens } 2269789Sahrens 2270789Sahrens /* 2271789Sahrens * Move an entry from the provided source directory to the target 2272789Sahrens * directory. Change the entry name as indicated. 2273789Sahrens * 2274789Sahrens * IN: sdvp - Source directory containing the "old entry". 2275789Sahrens * snm - Old entry name. 2276789Sahrens * tdvp - Target directory to contain the "new entry". 2277789Sahrens * tnm - New entry name. 2278789Sahrens * cr - credentials of caller. 2279789Sahrens * 2280789Sahrens * RETURN: 0 if success 2281789Sahrens * error code if failure 2282789Sahrens * 2283789Sahrens * Timestamps: 2284789Sahrens * sdvp,tdvp - ctime|mtime updated 2285789Sahrens */ 2286789Sahrens static int 2287789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2288789Sahrens { 2289789Sahrens znode_t *tdzp, *szp, *tzp; 2290789Sahrens znode_t *sdzp = VTOZ(sdvp); 2291789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2292789Sahrens zilog_t *zilog = zfsvfs->z_log; 2293789Sahrens vnode_t *realvp; 2294789Sahrens zfs_dirlock_t *sdl, *tdl; 2295789Sahrens dmu_tx_t *tx; 2296789Sahrens zfs_zlock_t *zl; 2297789Sahrens int cmp, serr, terr, error; 2298789Sahrens 2299789Sahrens ZFS_ENTER(zfsvfs); 2300789Sahrens 2301789Sahrens /* 2302789Sahrens * Make sure we have the real vp for the target directory. 2303789Sahrens */ 2304789Sahrens if (VOP_REALVP(tdvp, &realvp) == 0) 2305789Sahrens tdvp = realvp; 2306789Sahrens 2307789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2308789Sahrens ZFS_EXIT(zfsvfs); 2309789Sahrens return (EXDEV); 2310789Sahrens } 2311789Sahrens 2312789Sahrens tdzp = VTOZ(tdvp); 2313789Sahrens top: 2314789Sahrens szp = NULL; 2315789Sahrens tzp = NULL; 2316789Sahrens zl = NULL; 2317789Sahrens 2318789Sahrens /* 2319789Sahrens * This is to prevent the creation of links into attribute space 2320789Sahrens * by renaming a linked file into/outof an attribute directory. 2321789Sahrens * See the comment in zfs_link() for why this is considered bad. 2322789Sahrens */ 2323789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2324789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2325789Sahrens ZFS_EXIT(zfsvfs); 2326789Sahrens return (EINVAL); 2327789Sahrens } 2328789Sahrens 2329789Sahrens /* 2330789Sahrens * Lock source and target directory entries. To prevent deadlock, 2331789Sahrens * a lock ordering must be defined. We lock the directory with 2332789Sahrens * the smallest object id first, or if it's a tie, the one with 2333789Sahrens * the lexically first name. 2334789Sahrens */ 2335789Sahrens if (sdzp->z_id < tdzp->z_id) { 2336789Sahrens cmp = -1; 2337789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2338789Sahrens cmp = 1; 2339789Sahrens } else { 2340789Sahrens cmp = strcmp(snm, tnm); 2341789Sahrens if (cmp == 0) { 2342789Sahrens /* 2343789Sahrens * POSIX: "If the old argument and the new argument 2344789Sahrens * both refer to links to the same existing file, 2345789Sahrens * the rename() function shall return successfully 2346789Sahrens * and perform no other action." 2347789Sahrens */ 2348789Sahrens ZFS_EXIT(zfsvfs); 2349789Sahrens return (0); 2350789Sahrens } 2351789Sahrens } 2352789Sahrens if (cmp < 0) { 2353789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2354789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2355789Sahrens } else { 2356789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2357789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2358789Sahrens } 2359789Sahrens 2360789Sahrens if (serr) { 2361789Sahrens /* 2362789Sahrens * Source entry invalid or not there. 2363789Sahrens */ 2364789Sahrens if (!terr) { 2365789Sahrens zfs_dirent_unlock(tdl); 2366789Sahrens if (tzp) 2367789Sahrens VN_RELE(ZTOV(tzp)); 2368789Sahrens } 2369789Sahrens if (strcmp(snm, "..") == 0) 2370789Sahrens serr = EINVAL; 2371789Sahrens ZFS_EXIT(zfsvfs); 2372789Sahrens return (serr); 2373789Sahrens } 2374789Sahrens if (terr) { 2375789Sahrens zfs_dirent_unlock(sdl); 2376789Sahrens VN_RELE(ZTOV(szp)); 2377789Sahrens if (strcmp(tnm, "..") == 0) 2378789Sahrens terr = EINVAL; 2379789Sahrens ZFS_EXIT(zfsvfs); 2380789Sahrens return (terr); 2381789Sahrens } 2382789Sahrens 2383789Sahrens /* 2384789Sahrens * Must have write access at the source to remove the old entry 2385789Sahrens * and write access at the target to create the new entry. 2386789Sahrens * Note that if target and source are the same, this can be 2387789Sahrens * done in a single check. 2388789Sahrens */ 2389789Sahrens 2390789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2391789Sahrens goto out; 2392789Sahrens 2393789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2394789Sahrens /* 2395789Sahrens * Check to make sure rename is valid. 2396789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2397789Sahrens */ 2398789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2399789Sahrens goto out; 2400789Sahrens } 2401789Sahrens 2402789Sahrens /* 2403789Sahrens * Does target exist? 2404789Sahrens */ 2405789Sahrens if (tzp) { 2406789Sahrens /* 2407789Sahrens * Source and target must be the same type. 2408789Sahrens */ 2409789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2410789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 2411789Sahrens error = ENOTDIR; 2412789Sahrens goto out; 2413789Sahrens } 2414789Sahrens } else { 2415789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 2416789Sahrens error = EISDIR; 2417789Sahrens goto out; 2418789Sahrens } 2419789Sahrens } 2420789Sahrens /* 2421789Sahrens * POSIX dictates that when the source and target 2422789Sahrens * entries refer to the same file object, rename 2423789Sahrens * must do nothing and exit without error. 2424789Sahrens */ 2425789Sahrens if (szp->z_id == tzp->z_id) { 2426789Sahrens error = 0; 2427789Sahrens goto out; 2428789Sahrens } 2429789Sahrens } 2430789Sahrens 2431789Sahrens vnevent_rename_src(ZTOV(szp)); 2432789Sahrens if (tzp) 2433789Sahrens vnevent_rename_dest(ZTOV(tzp)); 2434789Sahrens 2435789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2436789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2437789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 24381544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 24391544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 24401544Seschrock if (sdzp != tdzp) 2441789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 24421544Seschrock if (tzp) 24431544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 24443461Sahrens dmu_tx_hold_zap(tx, zfsvfs->z_unlinkedobj, FALSE, NULL); 2445789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2446789Sahrens if (error) { 2447789Sahrens if (zl != NULL) 2448789Sahrens zfs_rename_unlock(&zl); 2449789Sahrens zfs_dirent_unlock(sdl); 2450789Sahrens zfs_dirent_unlock(tdl); 2451789Sahrens VN_RELE(ZTOV(szp)); 2452789Sahrens if (tzp) 2453789Sahrens VN_RELE(ZTOV(tzp)); 2454789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 24552113Sahrens dmu_tx_wait(tx); 24562113Sahrens dmu_tx_abort(tx); 2457789Sahrens goto top; 2458789Sahrens } 24592113Sahrens dmu_tx_abort(tx); 2460789Sahrens ZFS_EXIT(zfsvfs); 2461789Sahrens return (error); 2462789Sahrens } 2463789Sahrens 2464789Sahrens if (tzp) /* Attempt to remove the existing target */ 2465789Sahrens error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2466789Sahrens 2467789Sahrens if (error == 0) { 2468789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2469789Sahrens if (error == 0) { 2470789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2471789Sahrens ASSERT(error == 0); 24722638Sperrin zfs_log_rename(zilog, tx, TX_RENAME, sdzp, 24732638Sperrin sdl->dl_name, tdzp, tdl->dl_name, szp); 2474789Sahrens } 2475789Sahrens } 2476789Sahrens 2477789Sahrens dmu_tx_commit(tx); 2478789Sahrens out: 2479789Sahrens if (zl != NULL) 2480789Sahrens zfs_rename_unlock(&zl); 2481789Sahrens 2482789Sahrens zfs_dirent_unlock(sdl); 2483789Sahrens zfs_dirent_unlock(tdl); 2484789Sahrens 2485789Sahrens VN_RELE(ZTOV(szp)); 2486789Sahrens if (tzp) 2487789Sahrens VN_RELE(ZTOV(tzp)); 2488789Sahrens 2489789Sahrens ZFS_EXIT(zfsvfs); 2490789Sahrens return (error); 2491789Sahrens } 2492789Sahrens 2493789Sahrens /* 2494789Sahrens * Insert the indicated symbolic reference entry into the directory. 2495789Sahrens * 2496789Sahrens * IN: dvp - Directory to contain new symbolic link. 2497789Sahrens * link - Name for new symlink entry. 2498789Sahrens * vap - Attributes of new entry. 2499789Sahrens * target - Target path of new symlink. 2500789Sahrens * cr - credentials of caller. 2501789Sahrens * 2502789Sahrens * RETURN: 0 if success 2503789Sahrens * error code if failure 2504789Sahrens * 2505789Sahrens * Timestamps: 2506789Sahrens * dvp - ctime|mtime updated 2507789Sahrens */ 2508789Sahrens static int 2509789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2510789Sahrens { 2511789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 2512789Sahrens zfs_dirlock_t *dl; 2513789Sahrens dmu_tx_t *tx; 2514789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2515789Sahrens zilog_t *zilog = zfsvfs->z_log; 2516789Sahrens uint64_t zoid; 2517789Sahrens int len = strlen(link); 2518789Sahrens int error; 2519789Sahrens 2520789Sahrens ASSERT(vap->va_type == VLNK); 2521789Sahrens 2522789Sahrens ZFS_ENTER(zfsvfs); 2523789Sahrens top: 2524789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2525789Sahrens ZFS_EXIT(zfsvfs); 2526789Sahrens return (error); 2527789Sahrens } 2528789Sahrens 2529789Sahrens if (len > MAXPATHLEN) { 2530789Sahrens ZFS_EXIT(zfsvfs); 2531789Sahrens return (ENAMETOOLONG); 2532789Sahrens } 2533789Sahrens 2534789Sahrens /* 2535789Sahrens * Attempt to lock directory; fail if entry already exists. 2536789Sahrens */ 2537789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2538789Sahrens ZFS_EXIT(zfsvfs); 2539789Sahrens return (error); 2540789Sahrens } 2541789Sahrens 2542789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2543789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2544789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 25451544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2546789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2547789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2548789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2549789Sahrens if (error) { 2550789Sahrens zfs_dirent_unlock(dl); 2551789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 25522113Sahrens dmu_tx_wait(tx); 25532113Sahrens dmu_tx_abort(tx); 2554789Sahrens goto top; 2555789Sahrens } 25562113Sahrens dmu_tx_abort(tx); 2557789Sahrens ZFS_EXIT(zfsvfs); 2558789Sahrens return (error); 2559789Sahrens } 2560789Sahrens 2561789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 2562789Sahrens 2563789Sahrens /* 2564789Sahrens * Create a new object for the symlink. 2565789Sahrens * Put the link content into bonus buffer if it will fit; 2566789Sahrens * otherwise, store it just like any other file data. 2567789Sahrens */ 2568789Sahrens zoid = 0; 2569789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2570789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2571789Sahrens if (len != 0) 2572789Sahrens bcopy(link, zp->z_phys + 1, len); 2573789Sahrens } else { 2574789Sahrens dmu_buf_t *dbp; 25751669Sperrin 2576789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2577789Sahrens 25781669Sperrin /* 25791669Sperrin * Nothing can access the znode yet so no locking needed 25801669Sperrin * for growing the znode's blocksize. 25811669Sperrin */ 25821669Sperrin zfs_grow_blocksize(zp, len, tx); 2583789Sahrens 25841544Seschrock VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2585789Sahrens dmu_buf_will_dirty(dbp, tx); 2586789Sahrens 2587789Sahrens ASSERT3U(len, <=, dbp->db_size); 2588789Sahrens bcopy(link, dbp->db_data, len); 25891544Seschrock dmu_buf_rele(dbp, FTAG); 2590789Sahrens } 2591789Sahrens zp->z_phys->zp_size = len; 2592789Sahrens 2593789Sahrens /* 2594789Sahrens * Insert the new object into the directory. 2595789Sahrens */ 2596789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 2597789Sahrens out: 2598789Sahrens if (error == 0) 25992638Sperrin zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link); 2600789Sahrens 2601789Sahrens dmu_tx_commit(tx); 2602789Sahrens 2603789Sahrens zfs_dirent_unlock(dl); 2604789Sahrens 2605789Sahrens VN_RELE(ZTOV(zp)); 2606789Sahrens 2607789Sahrens ZFS_EXIT(zfsvfs); 2608789Sahrens return (error); 2609789Sahrens } 2610789Sahrens 2611789Sahrens /* 2612789Sahrens * Return, in the buffer contained in the provided uio structure, 2613789Sahrens * the symbolic path referred to by vp. 2614789Sahrens * 2615789Sahrens * IN: vp - vnode of symbolic link. 2616789Sahrens * uoip - structure to contain the link path. 2617789Sahrens * cr - credentials of caller. 2618789Sahrens * 2619789Sahrens * OUT: uio - structure to contain the link path. 2620789Sahrens * 2621789Sahrens * RETURN: 0 if success 2622789Sahrens * error code if failure 2623789Sahrens * 2624789Sahrens * Timestamps: 2625789Sahrens * vp - atime updated 2626789Sahrens */ 2627789Sahrens /* ARGSUSED */ 2628789Sahrens static int 2629789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2630789Sahrens { 2631789Sahrens znode_t *zp = VTOZ(vp); 2632789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2633789Sahrens size_t bufsz; 2634789Sahrens int error; 2635789Sahrens 2636789Sahrens ZFS_ENTER(zfsvfs); 2637789Sahrens 2638789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 2639789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2640789Sahrens error = uiomove(zp->z_phys + 1, 2641789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2642789Sahrens } else { 26431544Seschrock dmu_buf_t *dbp; 26441544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 26451544Seschrock if (error) { 2646789Sahrens ZFS_EXIT(zfsvfs); 2647789Sahrens return (error); 2648789Sahrens } 2649789Sahrens error = uiomove(dbp->db_data, 2650789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 26511544Seschrock dmu_buf_rele(dbp, FTAG); 2652789Sahrens } 2653789Sahrens 2654789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2655789Sahrens ZFS_EXIT(zfsvfs); 2656789Sahrens return (error); 2657789Sahrens } 2658789Sahrens 2659789Sahrens /* 2660789Sahrens * Insert a new entry into directory tdvp referencing svp. 2661789Sahrens * 2662789Sahrens * IN: tdvp - Directory to contain new entry. 2663789Sahrens * svp - vnode of new entry. 2664789Sahrens * name - name of new entry. 2665789Sahrens * cr - credentials of caller. 2666789Sahrens * 2667789Sahrens * RETURN: 0 if success 2668789Sahrens * error code if failure 2669789Sahrens * 2670789Sahrens * Timestamps: 2671789Sahrens * tdvp - ctime|mtime updated 2672789Sahrens * svp - ctime updated 2673789Sahrens */ 2674789Sahrens /* ARGSUSED */ 2675789Sahrens static int 2676789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2677789Sahrens { 2678789Sahrens znode_t *dzp = VTOZ(tdvp); 2679789Sahrens znode_t *tzp, *szp; 2680789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2681789Sahrens zilog_t *zilog = zfsvfs->z_log; 2682789Sahrens zfs_dirlock_t *dl; 2683789Sahrens dmu_tx_t *tx; 2684789Sahrens vnode_t *realvp; 2685789Sahrens int error; 2686789Sahrens 2687789Sahrens ASSERT(tdvp->v_type == VDIR); 2688789Sahrens 2689789Sahrens ZFS_ENTER(zfsvfs); 2690789Sahrens 2691789Sahrens if (VOP_REALVP(svp, &realvp) == 0) 2692789Sahrens svp = realvp; 2693789Sahrens 2694789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 2695789Sahrens ZFS_EXIT(zfsvfs); 2696789Sahrens return (EXDEV); 2697789Sahrens } 2698789Sahrens 2699789Sahrens szp = VTOZ(svp); 2700789Sahrens top: 2701789Sahrens /* 2702789Sahrens * We do not support links between attributes and non-attributes 2703789Sahrens * because of the potential security risk of creating links 2704789Sahrens * into "normal" file space in order to circumvent restrictions 2705789Sahrens * imposed in attribute space. 2706789Sahrens */ 2707789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2708789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2709789Sahrens ZFS_EXIT(zfsvfs); 2710789Sahrens return (EINVAL); 2711789Sahrens } 2712789Sahrens 2713789Sahrens /* 2714789Sahrens * POSIX dictates that we return EPERM here. 2715789Sahrens * Better choices include ENOTSUP or EISDIR. 2716789Sahrens */ 2717789Sahrens if (svp->v_type == VDIR) { 2718789Sahrens ZFS_EXIT(zfsvfs); 2719789Sahrens return (EPERM); 2720789Sahrens } 2721789Sahrens 2722789Sahrens if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2723789Sahrens secpolicy_basic_link(cr) != 0) { 2724789Sahrens ZFS_EXIT(zfsvfs); 2725789Sahrens return (EPERM); 2726789Sahrens } 2727789Sahrens 2728789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2729789Sahrens ZFS_EXIT(zfsvfs); 2730789Sahrens return (error); 2731789Sahrens } 2732789Sahrens 2733789Sahrens /* 2734789Sahrens * Attempt to lock directory; fail if entry already exists. 2735789Sahrens */ 2736789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2737789Sahrens ZFS_EXIT(zfsvfs); 2738789Sahrens return (error); 2739789Sahrens } 2740789Sahrens 2741789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2742789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 27431544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2744789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2745789Sahrens if (error) { 2746789Sahrens zfs_dirent_unlock(dl); 2747789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 27482113Sahrens dmu_tx_wait(tx); 27492113Sahrens dmu_tx_abort(tx); 2750789Sahrens goto top; 2751789Sahrens } 27522113Sahrens dmu_tx_abort(tx); 2753789Sahrens ZFS_EXIT(zfsvfs); 2754789Sahrens return (error); 2755789Sahrens } 2756789Sahrens 2757789Sahrens error = zfs_link_create(dl, szp, tx, 0); 2758789Sahrens 2759789Sahrens if (error == 0) 27602638Sperrin zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2761789Sahrens 2762789Sahrens dmu_tx_commit(tx); 2763789Sahrens 2764789Sahrens zfs_dirent_unlock(dl); 2765789Sahrens 2766789Sahrens ZFS_EXIT(zfsvfs); 2767789Sahrens return (error); 2768789Sahrens } 2769789Sahrens 2770789Sahrens /* 2771789Sahrens * zfs_null_putapage() is used when the file system has been force 2772789Sahrens * unmounted. It just drops the pages. 2773789Sahrens */ 2774789Sahrens /* ARGSUSED */ 2775789Sahrens static int 2776789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2777789Sahrens size_t *lenp, int flags, cred_t *cr) 2778789Sahrens { 2779789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2780789Sahrens return (0); 2781789Sahrens } 2782789Sahrens 27832688Smaybee /* 27842688Smaybee * Push a page out to disk, klustering if possible. 27852688Smaybee * 27862688Smaybee * IN: vp - file to push page to. 27872688Smaybee * pp - page to push. 27882688Smaybee * flags - additional flags. 27892688Smaybee * cr - credentials of caller. 27902688Smaybee * 27912688Smaybee * OUT: offp - start of range pushed. 27922688Smaybee * lenp - len of range pushed. 27932688Smaybee * 27942688Smaybee * RETURN: 0 if success 27952688Smaybee * error code if failure 27962688Smaybee * 27972688Smaybee * NOTE: callers must have locked the page to be pushed. On 27982688Smaybee * exit, the page (and all other pages in the kluster) must be 27992688Smaybee * unlocked. 28002688Smaybee */ 2801789Sahrens /* ARGSUSED */ 2802789Sahrens static int 2803789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2804789Sahrens size_t *lenp, int flags, cred_t *cr) 2805789Sahrens { 2806789Sahrens znode_t *zp = VTOZ(vp); 2807789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2808789Sahrens zilog_t *zilog = zfsvfs->z_log; 2809789Sahrens dmu_tx_t *tx; 28101669Sperrin rl_t *rl; 28112688Smaybee u_offset_t off, koff; 28122688Smaybee size_t len, klen; 2813789Sahrens int err; 2814789Sahrens 28152688Smaybee off = pp->p_offset; 28162688Smaybee len = PAGESIZE; 28172688Smaybee /* 28182688Smaybee * If our blocksize is bigger than the page size, try to kluster 28192688Smaybee * muiltiple pages so that we write a full block (thus avoiding 28202688Smaybee * a read-modify-write). 28212688Smaybee */ 28222688Smaybee if (zp->z_blksz > PAGESIZE) { 28232688Smaybee uint64_t filesz = zp->z_phys->zp_size; 28242688Smaybee 28252688Smaybee if (!ISP2(zp->z_blksz)) { 28262688Smaybee /* Only one block in the file. */ 28272688Smaybee klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 28282688Smaybee koff = 0; 28292688Smaybee } else { 28302688Smaybee klen = zp->z_blksz; 28312688Smaybee koff = P2ALIGN(off, (u_offset_t)klen); 28322688Smaybee } 28332688Smaybee ASSERT(koff <= filesz); 28342688Smaybee if (koff + klen > filesz) 28352688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 28362688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 28372688Smaybee } 28382688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 2839789Sahrens top: 28402688Smaybee rl = zfs_range_lock(zp, off, len, RL_WRITER); 28411819Smaybee /* 28421819Smaybee * Can't push pages past end-of-file. 28431819Smaybee */ 28441819Smaybee if (off >= zp->z_phys->zp_size) { 28452688Smaybee /* discard all pages */ 28462688Smaybee flags |= B_INVAL; 28472688Smaybee err = 0; 28482688Smaybee goto out; 28492688Smaybee } else if (off + len > zp->z_phys->zp_size) { 28502688Smaybee int npages = btopr(zp->z_phys->zp_size - off); 28512688Smaybee page_t *trunc; 28522688Smaybee 28532688Smaybee page_list_break(&pp, &trunc, npages); 28542688Smaybee /* discard pages past end of file */ 28552688Smaybee if (trunc) 28562688Smaybee pvn_write_done(trunc, B_INVAL | flags); 28572688Smaybee len = zp->z_phys->zp_size - off; 28581819Smaybee } 2859789Sahrens 2860789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2861789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 2862789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2863789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2864789Sahrens if (err != 0) { 2865789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 28662688Smaybee zfs_range_unlock(rl); 28672113Sahrens dmu_tx_wait(tx); 28682113Sahrens dmu_tx_abort(tx); 28692688Smaybee err = 0; 2870789Sahrens goto top; 2871789Sahrens } 28722113Sahrens dmu_tx_abort(tx); 2873789Sahrens goto out; 2874789Sahrens } 2875789Sahrens 28762688Smaybee if (zp->z_blksz <= PAGESIZE) { 28772688Smaybee caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1); 28782688Smaybee ASSERT3U(len, <=, PAGESIZE); 28792688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 28802688Smaybee ppmapout(va); 28812688Smaybee } else { 28822688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 28832688Smaybee } 28842688Smaybee 28852688Smaybee if (err == 0) { 28862688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 28873638Sbillm zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0); 28882688Smaybee dmu_tx_commit(tx); 28892688Smaybee } 28902688Smaybee 28912688Smaybee out: 28922237Smaybee zfs_range_unlock(rl); 28932688Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags); 2894789Sahrens if (offp) 2895789Sahrens *offp = off; 2896789Sahrens if (lenp) 2897789Sahrens *lenp = len; 2898789Sahrens 2899789Sahrens return (err); 2900789Sahrens } 2901789Sahrens 2902789Sahrens /* 2903789Sahrens * Copy the portion of the file indicated from pages into the file. 2904789Sahrens * The pages are stored in a page list attached to the files vnode. 2905789Sahrens * 2906789Sahrens * IN: vp - vnode of file to push page data to. 2907789Sahrens * off - position in file to put data. 2908789Sahrens * len - amount of data to write. 2909789Sahrens * flags - flags to control the operation. 2910789Sahrens * cr - credentials of caller. 2911789Sahrens * 2912789Sahrens * RETURN: 0 if success 2913789Sahrens * error code if failure 2914789Sahrens * 2915789Sahrens * Timestamps: 2916789Sahrens * vp - ctime|mtime updated 2917789Sahrens */ 2918789Sahrens static int 2919789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2920789Sahrens { 2921789Sahrens znode_t *zp = VTOZ(vp); 2922789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2923789Sahrens page_t *pp; 2924789Sahrens size_t io_len; 2925789Sahrens u_offset_t io_off; 29261669Sperrin uint64_t filesz; 2927789Sahrens int error = 0; 2928789Sahrens 2929789Sahrens ZFS_ENTER(zfsvfs); 2930789Sahrens 2931789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 2932789Sahrens 2933789Sahrens if (len == 0) { 2934789Sahrens /* 2935789Sahrens * Search the entire vp list for pages >= off. 2936789Sahrens */ 2937789Sahrens error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2938789Sahrens flags, cr); 29391472Sperrin goto out; 2940789Sahrens } 2941789Sahrens 29421669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 29431669Sperrin if (off > filesz) { 2944789Sahrens /* past end of file */ 2945789Sahrens ZFS_EXIT(zfsvfs); 2946789Sahrens return (0); 2947789Sahrens } 2948789Sahrens 29491669Sperrin len = MIN(len, filesz - off); 2950789Sahrens 29511472Sperrin for (io_off = off; io_off < off + len; io_off += io_len) { 2952789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 29531669Sperrin pp = page_lookup(vp, io_off, 29544339Sperrin (flags & (B_INVAL | B_FREE)) ? SE_EXCL : SE_SHARED); 2955789Sahrens } else { 2956789Sahrens pp = page_lookup_nowait(vp, io_off, 29574339Sperrin (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2958789Sahrens } 2959789Sahrens 2960789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 2961789Sahrens int err; 2962789Sahrens 2963789Sahrens /* 2964789Sahrens * Found a dirty page to push 2965789Sahrens */ 29661669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 29671669Sperrin if (err) 2968789Sahrens error = err; 2969789Sahrens } else { 2970789Sahrens io_len = PAGESIZE; 2971789Sahrens } 2972789Sahrens } 29731472Sperrin out: 29742638Sperrin if ((flags & B_ASYNC) == 0) 29752638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 2976789Sahrens ZFS_EXIT(zfsvfs); 2977789Sahrens return (error); 2978789Sahrens } 2979789Sahrens 2980789Sahrens void 2981789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr) 2982789Sahrens { 2983789Sahrens znode_t *zp = VTOZ(vp); 2984789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2985789Sahrens int error; 2986789Sahrens 2987789Sahrens rw_enter(&zfsvfs->z_um_lock, RW_READER); 2988789Sahrens if (zfsvfs->z_unmounted2) { 2989789Sahrens ASSERT(zp->z_dbuf_held == 0); 2990789Sahrens 2991789Sahrens if (vn_has_cached_data(vp)) { 2992789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 2993789Sahrens B_INVAL, cr); 2994789Sahrens } 2995789Sahrens 29961544Seschrock mutex_enter(&zp->z_lock); 2997789Sahrens vp->v_count = 0; /* count arrives as 1 */ 29981544Seschrock if (zp->z_dbuf == NULL) { 29991544Seschrock mutex_exit(&zp->z_lock); 30001544Seschrock zfs_znode_free(zp); 30011544Seschrock } else { 30021544Seschrock mutex_exit(&zp->z_lock); 30031544Seschrock } 3004789Sahrens rw_exit(&zfsvfs->z_um_lock); 3005789Sahrens VFS_RELE(zfsvfs->z_vfs); 3006789Sahrens return; 3007789Sahrens } 3008789Sahrens 3009789Sahrens /* 3010789Sahrens * Attempt to push any data in the page cache. If this fails 3011789Sahrens * we will get kicked out later in zfs_zinactive(). 3012789Sahrens */ 30131298Sperrin if (vn_has_cached_data(vp)) { 30141298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 30151298Sperrin cr); 30161298Sperrin } 3017789Sahrens 30183461Sahrens if (zp->z_atime_dirty && zp->z_unlinked == 0) { 3019789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3020789Sahrens 3021789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3022789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3023789Sahrens if (error) { 3024789Sahrens dmu_tx_abort(tx); 3025789Sahrens } else { 3026789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3027789Sahrens mutex_enter(&zp->z_lock); 3028789Sahrens zp->z_atime_dirty = 0; 3029789Sahrens mutex_exit(&zp->z_lock); 3030789Sahrens dmu_tx_commit(tx); 3031789Sahrens } 3032789Sahrens } 3033789Sahrens 3034789Sahrens zfs_zinactive(zp); 3035789Sahrens rw_exit(&zfsvfs->z_um_lock); 3036789Sahrens } 3037789Sahrens 3038789Sahrens /* 3039789Sahrens * Bounds-check the seek operation. 3040789Sahrens * 3041789Sahrens * IN: vp - vnode seeking within 3042789Sahrens * ooff - old file offset 3043789Sahrens * noffp - pointer to new file offset 3044789Sahrens * 3045789Sahrens * RETURN: 0 if success 3046789Sahrens * EINVAL if new offset invalid 3047789Sahrens */ 3048789Sahrens /* ARGSUSED */ 3049789Sahrens static int 3050789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 3051789Sahrens { 3052789Sahrens if (vp->v_type == VDIR) 3053789Sahrens return (0); 3054789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3055789Sahrens } 3056789Sahrens 3057789Sahrens /* 3058789Sahrens * Pre-filter the generic locking function to trap attempts to place 3059789Sahrens * a mandatory lock on a memory mapped file. 3060789Sahrens */ 3061789Sahrens static int 3062789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 3063789Sahrens flk_callback_t *flk_cbp, cred_t *cr) 3064789Sahrens { 3065789Sahrens znode_t *zp = VTOZ(vp); 3066789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3067789Sahrens int error; 3068789Sahrens 3069789Sahrens ZFS_ENTER(zfsvfs); 3070789Sahrens 3071789Sahrens /* 30721544Seschrock * We are following the UFS semantics with respect to mapcnt 30731544Seschrock * here: If we see that the file is mapped already, then we will 30741544Seschrock * return an error, but we don't worry about races between this 30751544Seschrock * function and zfs_map(). 3076789Sahrens */ 30771544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3078789Sahrens ZFS_EXIT(zfsvfs); 3079789Sahrens return (EAGAIN); 3080789Sahrens } 3081789Sahrens error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 3082789Sahrens ZFS_EXIT(zfsvfs); 3083789Sahrens return (error); 3084789Sahrens } 3085789Sahrens 3086789Sahrens /* 3087789Sahrens * If we can't find a page in the cache, we will create a new page 3088789Sahrens * and fill it with file data. For efficiency, we may try to fill 30891669Sperrin * multiple pages at once (klustering). 3090789Sahrens */ 3091789Sahrens static int 3092789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3093789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3094789Sahrens { 3095789Sahrens znode_t *zp = VTOZ(vp); 3096789Sahrens page_t *pp, *cur_pp; 3097789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3098789Sahrens caddr_t va; 3099789Sahrens u_offset_t io_off, total; 3100789Sahrens uint64_t oid = zp->z_id; 3101789Sahrens size_t io_len; 31021669Sperrin uint64_t filesz; 3103789Sahrens int err; 3104789Sahrens 3105789Sahrens /* 3106789Sahrens * If we are only asking for a single page don't bother klustering. 3107789Sahrens */ 31081669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 31092688Smaybee if (off >= filesz) 31102688Smaybee return (EFAULT); 31112688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3112789Sahrens io_off = off; 3113789Sahrens io_len = PAGESIZE; 3114789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3115789Sahrens } else { 3116789Sahrens /* 3117789Sahrens * Try to fill a kluster of pages (a blocks worth). 3118789Sahrens */ 3119789Sahrens size_t klen; 3120789Sahrens u_offset_t koff; 3121789Sahrens 3122789Sahrens if (!ISP2(zp->z_blksz)) { 3123789Sahrens /* Only one block in the file. */ 3124789Sahrens klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3125789Sahrens koff = 0; 3126789Sahrens } else { 31273131Sgw25295 /* 31283131Sgw25295 * It would be ideal to align our offset to the 31293131Sgw25295 * blocksize but doing so has resulted in some 31303131Sgw25295 * strange application crashes. For now, we 31313131Sgw25295 * leave the offset as is and only adjust the 31323131Sgw25295 * length if we are off the end of the file. 31333131Sgw25295 */ 31343131Sgw25295 koff = off; 3135789Sahrens klen = plsz; 3136789Sahrens } 31371819Smaybee ASSERT(koff <= filesz); 31381819Smaybee if (koff + klen > filesz) 31391819Smaybee klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 31402688Smaybee ASSERT3U(off, >=, koff); 31412688Smaybee ASSERT3U(off, <, koff + klen); 3142789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 31434339Sperrin &io_len, koff, klen, 0); 3144789Sahrens } 3145789Sahrens if (pp == NULL) { 3146789Sahrens /* 3147789Sahrens * Some other thread entered the page before us. 3148789Sahrens * Return to zfs_getpage to retry the lookup. 3149789Sahrens */ 3150789Sahrens *pl = NULL; 3151789Sahrens return (0); 3152789Sahrens } 3153789Sahrens 3154789Sahrens /* 3155789Sahrens * Fill the pages in the kluster. 3156789Sahrens */ 3157789Sahrens cur_pp = pp; 3158789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 31592688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 3160789Sahrens va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 31611544Seschrock err = dmu_read(os, oid, io_off, PAGESIZE, va); 3162789Sahrens ppmapout(va); 3163789Sahrens if (err) { 3164789Sahrens /* On error, toss the entire kluster */ 3165789Sahrens pvn_read_done(pp, B_ERROR); 3166789Sahrens return (err); 3167789Sahrens } 3168789Sahrens cur_pp = cur_pp->p_next; 3169789Sahrens } 3170789Sahrens out: 3171789Sahrens /* 3172789Sahrens * Fill in the page list array from the kluster. If 3173789Sahrens * there are too many pages in the kluster, return 3174789Sahrens * as many pages as possible starting from the desired 3175789Sahrens * offset `off'. 3176789Sahrens * NOTE: the page list will always be null terminated. 3177789Sahrens */ 3178789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3179789Sahrens 3180789Sahrens return (0); 3181789Sahrens } 3182789Sahrens 3183789Sahrens /* 3184789Sahrens * Return pointers to the pages for the file region [off, off + len] 3185789Sahrens * in the pl array. If plsz is greater than len, this function may 3186789Sahrens * also return page pointers from before or after the specified 3187789Sahrens * region (i.e. some region [off', off' + plsz]). These additional 3188789Sahrens * pages are only returned if they are already in the cache, or were 3189789Sahrens * created as part of a klustered read. 3190789Sahrens * 3191789Sahrens * IN: vp - vnode of file to get data from. 3192789Sahrens * off - position in file to get data from. 3193789Sahrens * len - amount of data to retrieve. 3194789Sahrens * plsz - length of provided page list. 3195789Sahrens * seg - segment to obtain pages for. 3196789Sahrens * addr - virtual address of fault. 3197789Sahrens * rw - mode of created pages. 3198789Sahrens * cr - credentials of caller. 3199789Sahrens * 3200789Sahrens * OUT: protp - protection mode of created pages. 3201789Sahrens * pl - list of pages created. 3202789Sahrens * 3203789Sahrens * RETURN: 0 if success 3204789Sahrens * error code if failure 3205789Sahrens * 3206789Sahrens * Timestamps: 3207789Sahrens * vp - atime updated 3208789Sahrens */ 3209789Sahrens /* ARGSUSED */ 3210789Sahrens static int 3211789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3212789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3213789Sahrens enum seg_rw rw, cred_t *cr) 3214789Sahrens { 3215789Sahrens znode_t *zp = VTOZ(vp); 3216789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3217789Sahrens page_t *pp, **pl0 = pl; 32182752Sperrin int need_unlock = 0, err = 0; 32192752Sperrin offset_t orig_off; 3220789Sahrens 3221789Sahrens ZFS_ENTER(zfsvfs); 3222789Sahrens 3223789Sahrens if (protp) 3224789Sahrens *protp = PROT_ALL; 3225789Sahrens 3226789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 3227789Sahrens 3228789Sahrens /* no faultahead (for now) */ 3229789Sahrens if (pl == NULL) { 3230789Sahrens ZFS_EXIT(zfsvfs); 3231789Sahrens return (0); 3232789Sahrens } 3233789Sahrens 3234789Sahrens /* can't fault past EOF */ 3235789Sahrens if (off >= zp->z_phys->zp_size) { 3236789Sahrens ZFS_EXIT(zfsvfs); 3237789Sahrens return (EFAULT); 3238789Sahrens } 32392752Sperrin orig_off = off; 3240789Sahrens 3241789Sahrens /* 3242789Sahrens * If we already own the lock, then we must be page faulting 3243789Sahrens * in the middle of a write to this file (i.e., we are writing 3244789Sahrens * to this file using data from a mapped region of the file). 3245789Sahrens */ 32462752Sperrin if (rw_owner(&zp->z_map_lock) != curthread) { 3247789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 3248789Sahrens need_unlock = TRUE; 3249789Sahrens } 3250789Sahrens 3251789Sahrens /* 3252789Sahrens * Loop through the requested range [off, off + len] looking 3253789Sahrens * for pages. If we don't find a page, we will need to create 3254789Sahrens * a new page and fill it with data from the file. 3255789Sahrens */ 3256789Sahrens while (len > 0) { 3257789Sahrens if (plsz < PAGESIZE) 3258789Sahrens break; 3259789Sahrens if (pp = page_lookup(vp, off, SE_SHARED)) { 3260789Sahrens *pl++ = pp; 3261789Sahrens off += PAGESIZE; 3262789Sahrens addr += PAGESIZE; 3263789Sahrens len -= PAGESIZE; 3264789Sahrens plsz -= PAGESIZE; 3265789Sahrens } else { 3266789Sahrens err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 32672752Sperrin if (err) 32682752Sperrin goto out; 3269789Sahrens /* 3270789Sahrens * klustering may have changed our region 3271789Sahrens * to be block aligned. 3272789Sahrens */ 3273789Sahrens if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3274789Sahrens int delta = off - pp->p_offset; 3275789Sahrens len += delta; 3276789Sahrens off -= delta; 3277789Sahrens addr -= delta; 3278789Sahrens } 3279789Sahrens while (*pl) { 3280789Sahrens pl++; 3281789Sahrens off += PAGESIZE; 3282789Sahrens addr += PAGESIZE; 3283789Sahrens plsz -= PAGESIZE; 3284789Sahrens if (len > PAGESIZE) 3285789Sahrens len -= PAGESIZE; 3286789Sahrens else 3287789Sahrens len = 0; 3288789Sahrens } 3289789Sahrens } 3290789Sahrens } 3291789Sahrens 3292789Sahrens /* 3293789Sahrens * Fill out the page array with any pages already in the cache. 3294789Sahrens */ 3295789Sahrens while (plsz > 0) { 3296789Sahrens pp = page_lookup_nowait(vp, off, SE_SHARED); 3297789Sahrens if (pp == NULL) 3298789Sahrens break; 3299789Sahrens *pl++ = pp; 3300789Sahrens off += PAGESIZE; 3301789Sahrens plsz -= PAGESIZE; 3302789Sahrens } 3303789Sahrens 3304789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3305789Sahrens out: 33062752Sperrin /* 33072752Sperrin * We can't grab the range lock for the page as reader which would 33082752Sperrin * stop truncation as this leads to deadlock. So we need to recheck 33092752Sperrin * the file size. 33102752Sperrin */ 33112752Sperrin if (orig_off >= zp->z_phys->zp_size) 33122752Sperrin err = EFAULT; 33132752Sperrin if (err) { 33142752Sperrin /* 33152752Sperrin * Release any pages we have previously locked. 33162752Sperrin */ 33172752Sperrin while (pl > pl0) 33182752Sperrin page_unlock(*--pl); 33192752Sperrin } 33202752Sperrin 3321789Sahrens *pl = NULL; 3322789Sahrens 3323789Sahrens if (need_unlock) 3324789Sahrens rw_exit(&zp->z_map_lock); 3325789Sahrens 3326789Sahrens ZFS_EXIT(zfsvfs); 3327789Sahrens return (err); 3328789Sahrens } 3329789Sahrens 33301544Seschrock /* 33311544Seschrock * Request a memory map for a section of a file. This code interacts 33321544Seschrock * with common code and the VM system as follows: 33331544Seschrock * 33341544Seschrock * common code calls mmap(), which ends up in smmap_common() 33351544Seschrock * 33361544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 33371544Seschrock * 33381544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 33391544Seschrock * 33401544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 33411544Seschrock * 33421544Seschrock * zfs_addmap() updates z_mapcnt 33431544Seschrock */ 3344789Sahrens static int 3345789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3346789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3347789Sahrens { 3348789Sahrens znode_t *zp = VTOZ(vp); 3349789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3350789Sahrens segvn_crargs_t vn_a; 3351789Sahrens int error; 3352789Sahrens 3353789Sahrens ZFS_ENTER(zfsvfs); 3354789Sahrens 3355789Sahrens if (vp->v_flag & VNOMAP) { 3356789Sahrens ZFS_EXIT(zfsvfs); 3357789Sahrens return (ENOSYS); 3358789Sahrens } 3359789Sahrens 3360789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 3361789Sahrens ZFS_EXIT(zfsvfs); 3362789Sahrens return (ENXIO); 3363789Sahrens } 3364789Sahrens 3365789Sahrens if (vp->v_type != VREG) { 3366789Sahrens ZFS_EXIT(zfsvfs); 3367789Sahrens return (ENODEV); 3368789Sahrens } 3369789Sahrens 3370789Sahrens /* 3371789Sahrens * If file is locked, disallow mapping. 3372789Sahrens */ 33731544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 33741544Seschrock ZFS_EXIT(zfsvfs); 33751544Seschrock return (EAGAIN); 3376789Sahrens } 3377789Sahrens 3378789Sahrens as_rangelock(as); 3379789Sahrens if ((flags & MAP_FIXED) == 0) { 3380789Sahrens map_addr(addrp, len, off, 1, flags); 3381789Sahrens if (*addrp == NULL) { 3382789Sahrens as_rangeunlock(as); 3383789Sahrens ZFS_EXIT(zfsvfs); 3384789Sahrens return (ENOMEM); 3385789Sahrens } 3386789Sahrens } else { 3387789Sahrens /* 3388789Sahrens * User specified address - blow away any previous mappings 3389789Sahrens */ 3390789Sahrens (void) as_unmap(as, *addrp, len); 3391789Sahrens } 3392789Sahrens 3393789Sahrens vn_a.vp = vp; 3394789Sahrens vn_a.offset = (u_offset_t)off; 3395789Sahrens vn_a.type = flags & MAP_TYPE; 3396789Sahrens vn_a.prot = prot; 3397789Sahrens vn_a.maxprot = maxprot; 3398789Sahrens vn_a.cred = cr; 3399789Sahrens vn_a.amp = NULL; 3400789Sahrens vn_a.flags = flags & ~MAP_TYPE; 34011417Skchow vn_a.szc = 0; 34021417Skchow vn_a.lgrp_mem_policy_flags = 0; 3403789Sahrens 3404789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 3405789Sahrens 3406789Sahrens as_rangeunlock(as); 3407789Sahrens ZFS_EXIT(zfsvfs); 3408789Sahrens return (error); 3409789Sahrens } 3410789Sahrens 3411789Sahrens /* ARGSUSED */ 3412789Sahrens static int 3413789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3414789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3415789Sahrens { 34161544Seschrock uint64_t pages = btopr(len); 34171544Seschrock 34181544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3419789Sahrens return (0); 3420789Sahrens } 3421789Sahrens 34221773Seschrock /* 34231773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 34241773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 34251773Seschrock * detecting when the data was actually modified, we have to resort to 34261773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 34271773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 34281773Seschrock * which by far the most common case: 34291773Seschrock * 34301773Seschrock * open() 34311773Seschrock * mmap() 34321773Seschrock * <modify memory> 34331773Seschrock * munmap() 34341773Seschrock * close() 34351773Seschrock * <time lapse> 34361773Seschrock * putpage() via fsflush 34371773Seschrock * 34381773Seschrock * If we wait until fsflush to come along, we can have a modification time that 34391773Seschrock * is some arbitrary point in the future. In order to prevent this in the 34401773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 34411773Seschrock * torn down. 34421773Seschrock */ 3443789Sahrens /* ARGSUSED */ 3444789Sahrens static int 3445789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3446789Sahrens size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3447789Sahrens { 34481544Seschrock uint64_t pages = btopr(len); 34491544Seschrock 34501544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 34511544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 34521773Seschrock 34531773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 34541773Seschrock vn_has_cached_data(vp)) 34551773Seschrock (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 34561773Seschrock 3457789Sahrens return (0); 3458789Sahrens } 3459789Sahrens 3460789Sahrens /* 3461789Sahrens * Free or allocate space in a file. Currently, this function only 3462789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 3463789Sahrens * misnamed, as its functionality includes the ability to allocate as 3464789Sahrens * well as free space. 3465789Sahrens * 3466789Sahrens * IN: vp - vnode of file to free data in. 3467789Sahrens * cmd - action to take (only F_FREESP supported). 3468789Sahrens * bfp - section of file to free/alloc. 3469789Sahrens * flag - current file open mode flags. 3470789Sahrens * offset - current file offset. 3471789Sahrens * cr - credentials of caller [UNUSED]. 3472789Sahrens * 3473789Sahrens * RETURN: 0 if success 3474789Sahrens * error code if failure 3475789Sahrens * 3476789Sahrens * Timestamps: 3477789Sahrens * vp - ctime|mtime updated 3478789Sahrens */ 3479789Sahrens /* ARGSUSED */ 3480789Sahrens static int 3481789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3482789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 3483789Sahrens { 3484789Sahrens znode_t *zp = VTOZ(vp); 3485789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3486789Sahrens uint64_t off, len; 3487789Sahrens int error; 3488789Sahrens 3489789Sahrens ZFS_ENTER(zfsvfs); 3490789Sahrens 3491789Sahrens top: 3492789Sahrens if (cmd != F_FREESP) { 3493789Sahrens ZFS_EXIT(zfsvfs); 3494789Sahrens return (EINVAL); 3495789Sahrens } 3496789Sahrens 3497789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 3498789Sahrens ZFS_EXIT(zfsvfs); 3499789Sahrens return (error); 3500789Sahrens } 3501789Sahrens 3502789Sahrens if (bfp->l_len < 0) { 3503789Sahrens ZFS_EXIT(zfsvfs); 3504789Sahrens return (EINVAL); 3505789Sahrens } 3506789Sahrens 3507789Sahrens off = bfp->l_start; 35081669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 35091878Smaybee 35101878Smaybee do { 35111878Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 35122113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 35131878Smaybee } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3514789Sahrens 3515789Sahrens ZFS_EXIT(zfsvfs); 3516789Sahrens return (error); 3517789Sahrens } 3518789Sahrens 3519789Sahrens static int 3520789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp) 3521789Sahrens { 3522789Sahrens znode_t *zp = VTOZ(vp); 3523789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3524789Sahrens uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3525789Sahrens uint64_t object = zp->z_id; 3526789Sahrens zfid_short_t *zfid; 3527789Sahrens int size, i; 3528789Sahrens 3529789Sahrens ZFS_ENTER(zfsvfs); 3530789Sahrens 3531789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3532789Sahrens if (fidp->fid_len < size) { 3533789Sahrens fidp->fid_len = size; 35341512Sek110237 ZFS_EXIT(zfsvfs); 3535789Sahrens return (ENOSPC); 3536789Sahrens } 3537789Sahrens 3538789Sahrens zfid = (zfid_short_t *)fidp; 3539789Sahrens 3540789Sahrens zfid->zf_len = size; 3541789Sahrens 3542789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 3543789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3544789Sahrens 3545789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 3546789Sahrens if (gen == 0) 3547789Sahrens gen = 1; 3548789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 3549789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3550789Sahrens 3551789Sahrens if (size == LONG_FID_LEN) { 3552789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3553789Sahrens zfid_long_t *zlfid; 3554789Sahrens 3555789Sahrens zlfid = (zfid_long_t *)fidp; 3556789Sahrens 3557789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3558789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3559789Sahrens 3560789Sahrens /* XXX - this should be the generation number for the objset */ 3561789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3562789Sahrens zlfid->zf_setgen[i] = 0; 3563789Sahrens } 3564789Sahrens 3565789Sahrens ZFS_EXIT(zfsvfs); 3566789Sahrens return (0); 3567789Sahrens } 3568789Sahrens 3569789Sahrens static int 3570789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3571789Sahrens { 3572789Sahrens znode_t *zp, *xzp; 3573789Sahrens zfsvfs_t *zfsvfs; 3574789Sahrens zfs_dirlock_t *dl; 3575789Sahrens int error; 3576789Sahrens 3577789Sahrens switch (cmd) { 3578789Sahrens case _PC_LINK_MAX: 3579789Sahrens *valp = ULONG_MAX; 3580789Sahrens return (0); 3581789Sahrens 3582789Sahrens case _PC_FILESIZEBITS: 3583789Sahrens *valp = 64; 3584789Sahrens return (0); 3585789Sahrens 3586789Sahrens case _PC_XATTR_EXISTS: 3587789Sahrens zp = VTOZ(vp); 3588789Sahrens zfsvfs = zp->z_zfsvfs; 3589789Sahrens ZFS_ENTER(zfsvfs); 3590789Sahrens *valp = 0; 3591789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 3592789Sahrens ZXATTR | ZEXISTS | ZSHARED); 3593789Sahrens if (error == 0) { 3594789Sahrens zfs_dirent_unlock(dl); 3595789Sahrens if (!zfs_dirempty(xzp)) 3596789Sahrens *valp = 1; 3597789Sahrens VN_RELE(ZTOV(xzp)); 3598789Sahrens } else if (error == ENOENT) { 3599789Sahrens /* 3600789Sahrens * If there aren't extended attributes, it's the 3601789Sahrens * same as having zero of them. 3602789Sahrens */ 3603789Sahrens error = 0; 3604789Sahrens } 3605789Sahrens ZFS_EXIT(zfsvfs); 3606789Sahrens return (error); 3607789Sahrens 3608789Sahrens case _PC_ACL_ENABLED: 3609789Sahrens *valp = _ACL_ACE_ENABLED; 3610789Sahrens return (0); 3611789Sahrens 3612789Sahrens case _PC_MIN_HOLE_SIZE: 3613789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 3614789Sahrens return (0); 3615789Sahrens 3616789Sahrens default: 3617789Sahrens return (fs_pathconf(vp, cmd, valp, cr)); 3618789Sahrens } 3619789Sahrens } 3620789Sahrens 3621789Sahrens /*ARGSUSED*/ 3622789Sahrens static int 3623789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3624789Sahrens { 3625789Sahrens znode_t *zp = VTOZ(vp); 3626789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3627789Sahrens int error; 3628789Sahrens 3629789Sahrens ZFS_ENTER(zfsvfs); 3630789Sahrens error = zfs_getacl(zp, vsecp, cr); 3631789Sahrens ZFS_EXIT(zfsvfs); 3632789Sahrens 3633789Sahrens return (error); 3634789Sahrens } 3635789Sahrens 3636789Sahrens /*ARGSUSED*/ 3637789Sahrens static int 3638789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3639789Sahrens { 3640789Sahrens znode_t *zp = VTOZ(vp); 3641789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3642789Sahrens int error; 3643789Sahrens 3644789Sahrens ZFS_ENTER(zfsvfs); 3645789Sahrens error = zfs_setacl(zp, vsecp, cr); 3646789Sahrens ZFS_EXIT(zfsvfs); 3647789Sahrens return (error); 3648789Sahrens } 3649789Sahrens 3650789Sahrens /* 3651789Sahrens * Predeclare these here so that the compiler assumes that 3652789Sahrens * this is an "old style" function declaration that does 3653789Sahrens * not include arguments => we won't get type mismatch errors 3654789Sahrens * in the initializations that follow. 3655789Sahrens */ 3656789Sahrens static int zfs_inval(); 3657789Sahrens static int zfs_isdir(); 3658789Sahrens 3659789Sahrens static int 3660789Sahrens zfs_inval() 3661789Sahrens { 3662789Sahrens return (EINVAL); 3663789Sahrens } 3664789Sahrens 3665789Sahrens static int 3666789Sahrens zfs_isdir() 3667789Sahrens { 3668789Sahrens return (EISDIR); 3669789Sahrens } 3670789Sahrens /* 3671789Sahrens * Directory vnode operations template 3672789Sahrens */ 3673789Sahrens vnodeops_t *zfs_dvnodeops; 3674789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 36753898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 36763898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 36773898Srsb VOPNAME_READ, { .error = zfs_isdir }, 36783898Srsb VOPNAME_WRITE, { .error = zfs_isdir }, 36793898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 36803898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 36813898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 36823898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 36833898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 36843898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 36853898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 36863898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 36873898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 36883898Srsb VOPNAME_MKDIR, { .vop_mkdir = zfs_mkdir }, 36893898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 36903898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 36913898Srsb VOPNAME_SYMLINK, { .vop_symlink = zfs_symlink }, 36923898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 36933898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 36943898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 36953898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 36963898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 36973898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 36983898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 36993898Srsb NULL, NULL 3700789Sahrens }; 3701789Sahrens 3702789Sahrens /* 3703789Sahrens * Regular file vnode operations template 3704789Sahrens */ 3705789Sahrens vnodeops_t *zfs_fvnodeops; 3706789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 37073898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 37083898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 37093898Srsb VOPNAME_READ, { .vop_read = zfs_read }, 37103898Srsb VOPNAME_WRITE, { .vop_write = zfs_write }, 37113898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 37123898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 37133898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 37143898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 37153898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 37163898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 37173898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 37183898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 37193898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 37203898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 37213898Srsb VOPNAME_FRLOCK, { .vop_frlock = zfs_frlock }, 37223898Srsb VOPNAME_SPACE, { .vop_space = zfs_space }, 37233898Srsb VOPNAME_GETPAGE, { .vop_getpage = zfs_getpage }, 37243898Srsb VOPNAME_PUTPAGE, { .vop_putpage = zfs_putpage }, 37253898Srsb VOPNAME_MAP, { .vop_map = zfs_map }, 37263898Srsb VOPNAME_ADDMAP, { .vop_addmap = zfs_addmap }, 37273898Srsb VOPNAME_DELMAP, { .vop_delmap = zfs_delmap }, 37283898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 37293898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 37303898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 37313898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 37323898Srsb NULL, NULL 3733789Sahrens }; 3734789Sahrens 3735789Sahrens /* 3736789Sahrens * Symbolic link vnode operations template 3737789Sahrens */ 3738789Sahrens vnodeops_t *zfs_symvnodeops; 3739789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 37403898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 37413898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 37423898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 37433898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 37443898Srsb VOPNAME_READLINK, { .vop_readlink = zfs_readlink }, 37453898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 37463898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 37473898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 37483898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 37493898Srsb NULL, NULL 3750789Sahrens }; 3751789Sahrens 3752789Sahrens /* 3753789Sahrens * Extended attribute directory vnode operations template 3754789Sahrens * This template is identical to the directory vnodes 3755789Sahrens * operation template except for restricted operations: 3756789Sahrens * VOP_MKDIR() 3757789Sahrens * VOP_SYMLINK() 3758789Sahrens * Note that there are other restrictions embedded in: 3759789Sahrens * zfs_create() - restrict type to VREG 3760789Sahrens * zfs_link() - no links into/out of attribute space 3761789Sahrens * zfs_rename() - no moves into/out of attribute space 3762789Sahrens */ 3763789Sahrens vnodeops_t *zfs_xdvnodeops; 3764789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 37653898Srsb VOPNAME_OPEN, { .vop_open = zfs_open }, 37663898Srsb VOPNAME_CLOSE, { .vop_close = zfs_close }, 37673898Srsb VOPNAME_IOCTL, { .vop_ioctl = zfs_ioctl }, 37683898Srsb VOPNAME_GETATTR, { .vop_getattr = zfs_getattr }, 37693898Srsb VOPNAME_SETATTR, { .vop_setattr = zfs_setattr }, 37703898Srsb VOPNAME_ACCESS, { .vop_access = zfs_access }, 37713898Srsb VOPNAME_LOOKUP, { .vop_lookup = zfs_lookup }, 37723898Srsb VOPNAME_CREATE, { .vop_create = zfs_create }, 37733898Srsb VOPNAME_REMOVE, { .vop_remove = zfs_remove }, 37743898Srsb VOPNAME_LINK, { .vop_link = zfs_link }, 37753898Srsb VOPNAME_RENAME, { .vop_rename = zfs_rename }, 37763898Srsb VOPNAME_MKDIR, { .error = zfs_inval }, 37773898Srsb VOPNAME_RMDIR, { .vop_rmdir = zfs_rmdir }, 37783898Srsb VOPNAME_READDIR, { .vop_readdir = zfs_readdir }, 37793898Srsb VOPNAME_SYMLINK, { .error = zfs_inval }, 37803898Srsb VOPNAME_FSYNC, { .vop_fsync = zfs_fsync }, 37813898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 37823898Srsb VOPNAME_FID, { .vop_fid = zfs_fid }, 37833898Srsb VOPNAME_SEEK, { .vop_seek = zfs_seek }, 37843898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 37853898Srsb VOPNAME_GETSECATTR, { .vop_getsecattr = zfs_getsecattr }, 37863898Srsb VOPNAME_SETSECATTR, { .vop_setsecattr = zfs_setsecattr }, 37873898Srsb VOPNAME_VNEVENT, { .vop_vnevent = fs_vnevent_support }, 37883898Srsb NULL, NULL 3789789Sahrens }; 3790789Sahrens 3791789Sahrens /* 3792789Sahrens * Error vnode operations template 3793789Sahrens */ 3794789Sahrens vnodeops_t *zfs_evnodeops; 3795789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 37963898Srsb VOPNAME_INACTIVE, { .vop_inactive = zfs_inactive }, 37973898Srsb VOPNAME_PATHCONF, { .vop_pathconf = zfs_pathconf }, 37983898Srsb NULL, NULL 3799789Sahrens }; 3800