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 /* 221231Smarks * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 23789Sahrens * Use is subject to license terms. 24789Sahrens */ 25789Sahrens 26789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 27789Sahrens 28789Sahrens #include <sys/types.h> 29789Sahrens #include <sys/param.h> 30789Sahrens #include <sys/time.h> 31789Sahrens #include <sys/systm.h> 32789Sahrens #include <sys/sysmacros.h> 33789Sahrens #include <sys/resource.h> 34789Sahrens #include <sys/vfs.h> 35789Sahrens #include <sys/vnode.h> 36789Sahrens #include <sys/file.h> 37789Sahrens #include <sys/stat.h> 38789Sahrens #include <sys/kmem.h> 39789Sahrens #include <sys/taskq.h> 40789Sahrens #include <sys/uio.h> 41789Sahrens #include <sys/vmsystm.h> 42789Sahrens #include <sys/atomic.h> 432688Smaybee #include <sys/vm.h> 44789Sahrens #include <vm/seg_vn.h> 45789Sahrens #include <vm/pvn.h> 46789Sahrens #include <vm/as.h> 47789Sahrens #include <sys/mman.h> 48789Sahrens #include <sys/pathname.h> 49789Sahrens #include <sys/cmn_err.h> 50789Sahrens #include <sys/errno.h> 51789Sahrens #include <sys/unistd.h> 52789Sahrens #include <sys/zfs_vfsops.h> 53789Sahrens #include <sys/zfs_dir.h> 54789Sahrens #include <sys/zfs_acl.h> 55789Sahrens #include <sys/zfs_ioctl.h> 56789Sahrens #include <sys/fs/zfs.h> 57789Sahrens #include <sys/dmu.h> 58789Sahrens #include <sys/spa.h> 59789Sahrens #include <sys/txg.h> 60789Sahrens #include <sys/dbuf.h> 61789Sahrens #include <sys/zap.h> 62789Sahrens #include <sys/dirent.h> 63789Sahrens #include <sys/policy.h> 64789Sahrens #include <sys/sunddi.h> 65789Sahrens #include <sys/filio.h> 66789Sahrens #include "fs/fs_subr.h" 67789Sahrens #include <sys/zfs_ctldir.h> 681484Sek110237 #include <sys/dnlc.h> 691669Sperrin #include <sys/zfs_rlock.h> 70789Sahrens 71789Sahrens /* 72789Sahrens * Programming rules. 73789Sahrens * 74789Sahrens * Each vnode op performs some logical unit of work. To do this, the ZPL must 75789Sahrens * properly lock its in-core state, create a DMU transaction, do the work, 76789Sahrens * record this work in the intent log (ZIL), commit the DMU transaction, 77789Sahrens * and wait the the intent log to commit if it's is a synchronous operation. 78789Sahrens * Morover, the vnode ops must work in both normal and log replay context. 79789Sahrens * The ordering of events is important to avoid deadlocks and references 80789Sahrens * to freed memory. The example below illustrates the following Big Rules: 81789Sahrens * 82789Sahrens * (1) A check must be made in each zfs thread for a mounted file system. 83789Sahrens * This is done avoiding races using ZFS_ENTER(zfsvfs). 84789Sahrens * A ZFS_EXIT(zfsvfs) is needed before all returns. 85789Sahrens * 86789Sahrens * (2) VN_RELE() should always be the last thing except for zil_commit() 872638Sperrin * (if necessary) and ZFS_EXIT(). This is for 3 reasons: 88789Sahrens * First, if it's the last reference, the vnode/znode 89789Sahrens * can be freed, so the zp may point to freed memory. Second, the last 90789Sahrens * reference will call zfs_zinactive(), which may induce a lot of work -- 911669Sperrin * pushing cached pages (which acquires range locks) and syncing out 92789Sahrens * cached atime changes. Third, zfs_zinactive() may require a new tx, 93789Sahrens * which could deadlock the system if you were already holding one. 94789Sahrens * 951757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 961757Sperrin * as they can span dmu_tx_assign() calls. 971757Sperrin * 981757Sperrin * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign(). 99789Sahrens * In normal operation, this will be TXG_NOWAIT. During ZIL replay, 100789Sahrens * it will be a specific txg. Either way, dmu_tx_assign() never blocks. 101789Sahrens * This is critical because we don't want to block while holding locks. 102789Sahrens * Note, in particular, that if a lock is sometimes acquired before 103789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 104789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 105789Sahrens * 106789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 107789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 108789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 109789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 110789Sahrens * 111789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1122113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 113789Sahrens * 1141757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 115789Sahrens * before dropping locks. This ensures that the ordering of events 116789Sahrens * in the intent log matches the order in which they actually occurred. 117789Sahrens * 1181757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 119789Sahrens * regardless of whether there were any errors. 120789Sahrens * 1212638Sperrin * (7) After dropping all locks, invoke zil_commit(zilog, seq, foid) 122789Sahrens * to ensure that synchronous semantics are provided when necessary. 123789Sahrens * 124789Sahrens * In general, this is how things should be ordered in each vnode op: 125789Sahrens * 126789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 127789Sahrens * top: 128789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 129789Sahrens * rw_enter(...); // grab any other locks you need 130789Sahrens * tx = dmu_tx_create(...); // get DMU tx 131789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 132789Sahrens * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign 133789Sahrens * if (error) { 134789Sahrens * rw_exit(...); // drop locks 135789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 136789Sahrens * VN_RELE(...); // release held vnodes 137789Sahrens * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1382113Sahrens * dmu_tx_wait(tx); 1392113Sahrens * dmu_tx_abort(tx); 140789Sahrens * goto top; 141789Sahrens * } 1422113Sahrens * dmu_tx_abort(tx); // abort DMU tx 143789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 144789Sahrens * return (error); // really out of space 145789Sahrens * } 146789Sahrens * error = do_real_work(); // do whatever this VOP does 147789Sahrens * if (error == 0) 1482638Sperrin * zfs_log_*(...); // on success, make ZIL entry 149789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 150789Sahrens * rw_exit(...); // drop locks 151789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 152789Sahrens * VN_RELE(...); // release held vnodes 1532638Sperrin * zil_commit(zilog, seq, foid); // synchronous when necessary 154789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 155789Sahrens * return (error); // done, report error 156789Sahrens */ 157789Sahrens /* ARGSUSED */ 158789Sahrens static int 159789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr) 160789Sahrens { 1613063Sperrin znode_t *zp = VTOZ(*vpp); 1623063Sperrin 1633063Sperrin /* Keep a count of the synchronous opens in the znode */ 1643063Sperrin if (flag & (FSYNC | FDSYNC)) 1653063Sperrin atomic_inc_32(&zp->z_sync_cnt); 166789Sahrens return (0); 167789Sahrens } 168789Sahrens 169789Sahrens /* ARGSUSED */ 170789Sahrens static int 171789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 172789Sahrens { 1733063Sperrin znode_t *zp = VTOZ(vp); 1743063Sperrin 1753063Sperrin /* Decrement the synchronous opens in the znode */ 1763063Sperrin if (flag & (FSYNC | FDSYNC)) 1773063Sperrin atomic_dec_32(&zp->z_sync_cnt); 1783063Sperrin 179789Sahrens /* 180789Sahrens * Clean up any locks held by this process on the vp. 181789Sahrens */ 182789Sahrens cleanlocks(vp, ddi_get_pid(), 0); 183789Sahrens cleanshares(vp, ddi_get_pid()); 184789Sahrens 185789Sahrens return (0); 186789Sahrens } 187789Sahrens 188789Sahrens /* 189789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 190789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 191789Sahrens */ 192789Sahrens static int 193789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 194789Sahrens { 195789Sahrens znode_t *zp = VTOZ(vp); 196789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 197789Sahrens uint64_t file_sz; 198789Sahrens int error; 199789Sahrens boolean_t hole; 200789Sahrens 201789Sahrens file_sz = zp->z_phys->zp_size; 202789Sahrens if (noff >= file_sz) { 203789Sahrens return (ENXIO); 204789Sahrens } 205789Sahrens 206789Sahrens if (cmd == _FIO_SEEK_HOLE) 207789Sahrens hole = B_TRUE; 208789Sahrens else 209789Sahrens hole = B_FALSE; 210789Sahrens 211789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 212789Sahrens 213789Sahrens /* end of file? */ 214789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 215789Sahrens /* 216789Sahrens * Handle the virtual hole at the end of file. 217789Sahrens */ 218789Sahrens if (hole) { 219789Sahrens *off = file_sz; 220789Sahrens return (0); 221789Sahrens } 222789Sahrens return (ENXIO); 223789Sahrens } 224789Sahrens 225789Sahrens if (noff < *off) 226789Sahrens return (error); 227789Sahrens *off = noff; 228789Sahrens return (error); 229789Sahrens } 230789Sahrens 231789Sahrens /* ARGSUSED */ 232789Sahrens static int 233789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 234789Sahrens int *rvalp) 235789Sahrens { 236789Sahrens offset_t off; 237789Sahrens int error; 238789Sahrens zfsvfs_t *zfsvfs; 239789Sahrens 240789Sahrens switch (com) { 241789Sahrens case _FIOFFS: 242789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 243789Sahrens 2441544Seschrock /* 2451544Seschrock * The following two ioctls are used by bfu. Faking out, 2461544Seschrock * necessary to avoid bfu errors. 2471544Seschrock */ 2481544Seschrock case _FIOGDIO: 2491544Seschrock case _FIOSDIO: 2501544Seschrock return (0); 2511544Seschrock 252789Sahrens case _FIO_SEEK_DATA: 253789Sahrens case _FIO_SEEK_HOLE: 254789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 255789Sahrens return (EFAULT); 256789Sahrens 257789Sahrens zfsvfs = VTOZ(vp)->z_zfsvfs; 258789Sahrens ZFS_ENTER(zfsvfs); 259789Sahrens 260789Sahrens /* offset parameter is in/out */ 261789Sahrens error = zfs_holey(vp, com, &off); 262789Sahrens ZFS_EXIT(zfsvfs); 263789Sahrens if (error) 264789Sahrens return (error); 265789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 266789Sahrens return (EFAULT); 267789Sahrens return (0); 268789Sahrens } 269789Sahrens return (ENOTTY); 270789Sahrens } 271789Sahrens 272789Sahrens /* 273789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 274789Sahrens * between the DMU cache and the memory mapped pages. What this means: 275789Sahrens * 276789Sahrens * On Write: If we find a memory mapped page, we write to *both* 277789Sahrens * the page and the dmu buffer. 278789Sahrens * 279789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 280789Sahrens * the file is memory mapped. 281789Sahrens */ 282789Sahrens static int 283789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx) 284789Sahrens { 285789Sahrens znode_t *zp = VTOZ(vp); 286789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 287789Sahrens int64_t start, off; 288789Sahrens int len = nbytes; 289789Sahrens int error = 0; 290789Sahrens 291789Sahrens start = uio->uio_loffset; 292789Sahrens off = start & PAGEOFFSET; 293789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 294789Sahrens page_t *pp; 295789Sahrens uint64_t bytes = MIN(PAGESIZE - off, len); 296789Sahrens 297789Sahrens /* 298789Sahrens * We don't want a new page to "appear" in the middle of 299789Sahrens * the file update (because it may not get the write 300789Sahrens * update data), so we grab a lock to block 301789Sahrens * zfs_getpage(). 302789Sahrens */ 303789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 304789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 305789Sahrens caddr_t va; 306789Sahrens 307789Sahrens rw_exit(&zp->z_map_lock); 308789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 309789Sahrens error = uiomove(va+off, bytes, UIO_WRITE, uio); 310789Sahrens if (error == 0) { 311789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, 312789Sahrens woff, bytes, va+off, tx); 313789Sahrens } 314789Sahrens ppmapout(va); 315789Sahrens page_unlock(pp); 316789Sahrens } else { 317789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 318789Sahrens woff, bytes, uio, tx); 319789Sahrens rw_exit(&zp->z_map_lock); 320789Sahrens } 321789Sahrens len -= bytes; 322789Sahrens woff += bytes; 323789Sahrens off = 0; 324789Sahrens if (error) 325789Sahrens break; 326789Sahrens } 327789Sahrens return (error); 328789Sahrens } 329789Sahrens 330789Sahrens /* 331789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 332789Sahrens * between the DMU cache and the memory mapped pages. What this means: 333789Sahrens * 334789Sahrens * On Read: We "read" preferentially from memory mapped pages, 335789Sahrens * else we default from the dmu buffer. 336789Sahrens * 337789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 338789Sahrens * the file is memory mapped. 339789Sahrens */ 340789Sahrens static int 341789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio) 342789Sahrens { 343789Sahrens int64_t start, off, bytes; 344789Sahrens int len = nbytes; 345789Sahrens int error = 0; 346789Sahrens 347789Sahrens start = uio->uio_loffset; 348789Sahrens off = start & PAGEOFFSET; 349789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 350789Sahrens page_t *pp; 351789Sahrens 352789Sahrens bytes = MIN(PAGESIZE - off, len); 353789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 354789Sahrens caddr_t va; 355789Sahrens 3562688Smaybee va = ppmapin(pp, PROT_READ, (caddr_t)-1L); 357789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 358789Sahrens ppmapout(va); 359789Sahrens page_unlock(pp); 360789Sahrens } else { 361789Sahrens /* XXX use dmu_read here? */ 362789Sahrens error = uiomove(addr, bytes, UIO_READ, uio); 363789Sahrens } 364789Sahrens len -= bytes; 365789Sahrens addr += bytes; 366789Sahrens off = 0; 367789Sahrens if (error) 368789Sahrens break; 369789Sahrens } 370789Sahrens return (error); 371789Sahrens } 372789Sahrens 373789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 374789Sahrens 375789Sahrens /* 376789Sahrens * Read bytes from specified file into supplied buffer. 377789Sahrens * 378789Sahrens * IN: vp - vnode of file to be read from. 379789Sahrens * uio - structure supplying read location, range info, 380789Sahrens * and return buffer. 381789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 382789Sahrens * cr - credentials of caller. 383789Sahrens * 384789Sahrens * OUT: uio - updated offset and range, buffer filled. 385789Sahrens * 386789Sahrens * RETURN: 0 if success 387789Sahrens * error code if failure 388789Sahrens * 389789Sahrens * Side Effects: 390789Sahrens * vp - atime updated if byte count > 0 391789Sahrens */ 392789Sahrens /* ARGSUSED */ 393789Sahrens static int 394789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 395789Sahrens { 396789Sahrens znode_t *zp = VTOZ(vp); 397789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 398789Sahrens uint64_t delta; 399789Sahrens ssize_t n, size, cnt, ndone; 400789Sahrens int error, i, numbufs; 401789Sahrens dmu_buf_t *dbp, **dbpp; 4021669Sperrin rl_t *rl; 403789Sahrens 404789Sahrens ZFS_ENTER(zfsvfs); 405789Sahrens 406789Sahrens /* 407789Sahrens * Validate file offset 408789Sahrens */ 409789Sahrens if (uio->uio_loffset < (offset_t)0) { 410789Sahrens ZFS_EXIT(zfsvfs); 411789Sahrens return (EINVAL); 412789Sahrens } 413789Sahrens 414789Sahrens /* 415789Sahrens * Fasttrack empty reads 416789Sahrens */ 417789Sahrens if (uio->uio_resid == 0) { 418789Sahrens ZFS_EXIT(zfsvfs); 419789Sahrens return (0); 420789Sahrens } 421789Sahrens 422789Sahrens /* 4231669Sperrin * Check for mandatory locks 424789Sahrens */ 425789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 426789Sahrens if (error = chklock(vp, FREAD, 427789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 428789Sahrens ZFS_EXIT(zfsvfs); 429789Sahrens return (error); 430789Sahrens } 431789Sahrens } 432789Sahrens 433789Sahrens /* 434789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 435789Sahrens */ 4362638Sperrin if (ioflag & FRSYNC) 4372638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 438789Sahrens 439789Sahrens /* 4401669Sperrin * Lock the range against changes. 441789Sahrens */ 4421669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4431669Sperrin 444789Sahrens /* 445789Sahrens * If we are reading past end-of-file we can skip 446789Sahrens * to the end; but we might still need to set atime. 447789Sahrens */ 448789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 449789Sahrens cnt = 0; 450789Sahrens error = 0; 451789Sahrens goto out; 452789Sahrens } 453789Sahrens 454789Sahrens cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 455789Sahrens 456789Sahrens for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) { 457789Sahrens ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 458789Sahrens n = MIN(zfs_read_chunk_size, 459789Sahrens zp->z_phys->zp_size - uio->uio_loffset); 460789Sahrens n = MIN(n, cnt); 4612391Smaybee error = dmu_buf_hold_array_by_bonus(zp->z_dbuf, 4621544Seschrock uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp); 4631544Seschrock if (error) 464789Sahrens goto out; 465789Sahrens /* 466789Sahrens * Compute the adjustment to align the dmu buffers 467789Sahrens * with the uio buffer. 468789Sahrens */ 469789Sahrens delta = uio->uio_loffset - dbpp[0]->db_offset; 470789Sahrens 471789Sahrens for (i = 0; i < numbufs; i++) { 472789Sahrens if (n < 0) 473789Sahrens break; 474789Sahrens dbp = dbpp[i]; 475789Sahrens size = dbp->db_size - delta; 476789Sahrens /* 477789Sahrens * XXX -- this is correct, but may be suboptimal. 478789Sahrens * If the pages are all clean, we don't need to 479789Sahrens * go through mappedread(). Maybe the VMODSORT 480789Sahrens * stuff can help us here. 481789Sahrens */ 482789Sahrens if (vn_has_cached_data(vp)) { 483789Sahrens error = mappedread(vp, (caddr_t)dbp->db_data + 484789Sahrens delta, (n < size ? n : size), uio); 485789Sahrens } else { 486789Sahrens error = uiomove((caddr_t)dbp->db_data + delta, 487789Sahrens (n < size ? n : size), UIO_READ, uio); 488789Sahrens } 489789Sahrens if (error) { 4901544Seschrock dmu_buf_rele_array(dbpp, numbufs, FTAG); 491789Sahrens goto out; 492789Sahrens } 493789Sahrens n -= dbp->db_size; 494789Sahrens if (delta) { 495789Sahrens n += delta; 496789Sahrens delta = 0; 497789Sahrens } 498789Sahrens } 4991544Seschrock dmu_buf_rele_array(dbpp, numbufs, FTAG); 500789Sahrens } 501789Sahrens out: 5022237Smaybee zfs_range_unlock(rl); 503789Sahrens 504789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 505789Sahrens ZFS_EXIT(zfsvfs); 506789Sahrens return (error); 507789Sahrens } 508789Sahrens 509789Sahrens /* 510789Sahrens * Fault in the pages of the first n bytes specified by the uio structure. 511789Sahrens * 1 byte in each page is touched and the uio struct is unmodified. 512789Sahrens * Any error will exit this routine as this is only a best 513789Sahrens * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 514789Sahrens */ 515789Sahrens static void 516789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio) 517789Sahrens { 518789Sahrens struct iovec *iov; 519789Sahrens ulong_t cnt, incr; 520789Sahrens caddr_t p; 521789Sahrens uint8_t tmp; 522789Sahrens 523789Sahrens iov = uio->uio_iov; 524789Sahrens 525789Sahrens while (n) { 526789Sahrens cnt = MIN(iov->iov_len, n); 527789Sahrens if (cnt == 0) { 528789Sahrens /* empty iov entry */ 529789Sahrens iov++; 530789Sahrens continue; 531789Sahrens } 532789Sahrens n -= cnt; 533789Sahrens /* 534789Sahrens * touch each page in this segment. 535789Sahrens */ 536789Sahrens p = iov->iov_base; 537789Sahrens while (cnt) { 538789Sahrens switch (uio->uio_segflg) { 539789Sahrens case UIO_USERSPACE: 540789Sahrens case UIO_USERISPACE: 541789Sahrens if (fuword8(p, &tmp)) 542789Sahrens return; 543789Sahrens break; 544789Sahrens case UIO_SYSSPACE: 545789Sahrens if (kcopy(p, &tmp, 1)) 546789Sahrens return; 547789Sahrens break; 548789Sahrens } 549789Sahrens incr = MIN(cnt, PAGESIZE); 550789Sahrens p += incr; 551789Sahrens cnt -= incr; 552789Sahrens } 553789Sahrens /* 554789Sahrens * touch the last byte in case it straddles a page. 555789Sahrens */ 556789Sahrens p--; 557789Sahrens switch (uio->uio_segflg) { 558789Sahrens case UIO_USERSPACE: 559789Sahrens case UIO_USERISPACE: 560789Sahrens if (fuword8(p, &tmp)) 561789Sahrens return; 562789Sahrens break; 563789Sahrens case UIO_SYSSPACE: 564789Sahrens if (kcopy(p, &tmp, 1)) 565789Sahrens return; 566789Sahrens break; 567789Sahrens } 568789Sahrens iov++; 569789Sahrens } 570789Sahrens } 571789Sahrens 572789Sahrens /* 573789Sahrens * Write the bytes to a file. 574789Sahrens * 575789Sahrens * IN: vp - vnode of file to be written to. 576789Sahrens * uio - structure supplying write location, range info, 577789Sahrens * and data buffer. 578789Sahrens * ioflag - FAPPEND flag set if in append mode. 579789Sahrens * cr - credentials of caller. 580789Sahrens * 581789Sahrens * OUT: uio - updated offset and range. 582789Sahrens * 583789Sahrens * RETURN: 0 if success 584789Sahrens * error code if failure 585789Sahrens * 586789Sahrens * Timestamps: 587789Sahrens * vp - ctime|mtime updated if byte count > 0 588789Sahrens */ 589789Sahrens /* ARGSUSED */ 590789Sahrens static int 591789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 592789Sahrens { 593789Sahrens znode_t *zp = VTOZ(vp); 594789Sahrens rlim64_t limit = uio->uio_llimit; 595789Sahrens ssize_t start_resid = uio->uio_resid; 596789Sahrens ssize_t tx_bytes; 597789Sahrens uint64_t end_size; 598789Sahrens dmu_tx_t *tx; 599789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 600789Sahrens zilog_t *zilog = zfsvfs->z_log; 601789Sahrens offset_t woff; 602789Sahrens ssize_t n, nbytes; 6031669Sperrin rl_t *rl; 604789Sahrens int max_blksz = zfsvfs->z_max_blksz; 6051669Sperrin int error; 606789Sahrens 607789Sahrens /* 608789Sahrens * Fasttrack empty write 609789Sahrens */ 6101669Sperrin n = start_resid; 611789Sahrens if (n == 0) 612789Sahrens return (0); 613789Sahrens 6141669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6151669Sperrin limit = MAXOFFSET_T; 6161669Sperrin 617789Sahrens ZFS_ENTER(zfsvfs); 618789Sahrens 619789Sahrens /* 6202237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6211669Sperrin * don't hold up txg. 622789Sahrens */ 6232237Smaybee zfs_prefault_write(n, uio); 624789Sahrens 625789Sahrens /* 626789Sahrens * If in append mode, set the io offset pointer to eof. 627789Sahrens */ 6281669Sperrin if (ioflag & FAPPEND) { 6291669Sperrin /* 6301669Sperrin * Range lock for a file append: 6311669Sperrin * The value for the start of range will be determined by 6321669Sperrin * zfs_range_lock() (to guarantee append semantics). 6331669Sperrin * If this write will cause the block size to increase, 6341669Sperrin * zfs_range_lock() will lock the entire file, so we must 6351669Sperrin * later reduce the range after we grow the block size. 6361669Sperrin */ 6371669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6381669Sperrin if (rl->r_len == UINT64_MAX) { 6391669Sperrin /* overlocked, zp_size can't change */ 6401669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6411669Sperrin } else { 6421669Sperrin woff = uio->uio_loffset = rl->r_off; 6431669Sperrin } 644789Sahrens } else { 645789Sahrens woff = uio->uio_loffset; 646789Sahrens /* 647789Sahrens * Validate file offset 648789Sahrens */ 649789Sahrens if (woff < 0) { 650789Sahrens ZFS_EXIT(zfsvfs); 651789Sahrens return (EINVAL); 652789Sahrens } 653789Sahrens 654789Sahrens /* 6551669Sperrin * If we need to grow the block size then zfs_range_lock() 6561669Sperrin * will lock a wider range than we request here. 6571669Sperrin * Later after growing the block size we reduce the range. 658789Sahrens */ 6591669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 660789Sahrens } 661789Sahrens 662789Sahrens if (woff >= limit) { 663789Sahrens error = EFBIG; 664789Sahrens goto no_tx_done; 665789Sahrens } 666789Sahrens 667789Sahrens if ((woff + n) > limit || woff > (limit - n)) 668789Sahrens n = limit - woff; 669789Sahrens 670789Sahrens /* 6711669Sperrin * Check for mandatory locks 672789Sahrens */ 673789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 674789Sahrens (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) 675789Sahrens goto no_tx_done; 6761669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 677789Sahrens top: 678789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 679789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 680789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 681789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 682789Sahrens if (error) { 683789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 6842113Sahrens dmu_tx_wait(tx); 6852113Sahrens dmu_tx_abort(tx); 686789Sahrens goto top; 687789Sahrens } 6882113Sahrens dmu_tx_abort(tx); 689789Sahrens goto no_tx_done; 690789Sahrens } 691789Sahrens 6921669Sperrin /* 6931669Sperrin * If zfs_range_lock() over-locked we grow the blocksize 6941669Sperrin * and then reduce the lock range. 6951669Sperrin */ 6961669Sperrin if (rl->r_len == UINT64_MAX) { 697789Sahrens uint64_t new_blksz; 6981669Sperrin 699789Sahrens if (zp->z_blksz > max_blksz) { 700789Sahrens ASSERT(!ISP2(zp->z_blksz)); 701789Sahrens new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 702789Sahrens } else { 703789Sahrens new_blksz = MIN(end_size, max_blksz); 704789Sahrens } 7051669Sperrin zfs_grow_blocksize(zp, new_blksz, tx); 7062237Smaybee zfs_range_reduce(rl, woff, n); 707789Sahrens } 708789Sahrens 709789Sahrens /* 710789Sahrens * The file data does not fit in the znode "cache", so we 711789Sahrens * will be writing to the file block data buffers. 712789Sahrens * Each buffer will be written in a separate transaction; 713789Sahrens * this keeps the intent log records small and allows us 714789Sahrens * to do more fine-grained space accounting. 715789Sahrens */ 716789Sahrens while (n > 0) { 717789Sahrens /* 718789Sahrens * XXX - should we really limit each write to z_max_blksz? 719789Sahrens * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 720789Sahrens */ 721789Sahrens nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 722789Sahrens rw_enter(&zp->z_map_lock, RW_READER); 723789Sahrens 724789Sahrens tx_bytes = uio->uio_resid; 725789Sahrens if (vn_has_cached_data(vp)) { 726789Sahrens rw_exit(&zp->z_map_lock); 727789Sahrens error = mappedwrite(vp, woff, nbytes, uio, tx); 728789Sahrens } else { 729789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 730789Sahrens woff, nbytes, uio, tx); 731789Sahrens rw_exit(&zp->z_map_lock); 732789Sahrens } 733789Sahrens tx_bytes -= uio->uio_resid; 734789Sahrens 735789Sahrens if (error) { 736789Sahrens /* XXX - do we need to "clean up" the dmu buffer? */ 737789Sahrens break; 738789Sahrens } 739789Sahrens 740789Sahrens ASSERT(tx_bytes == nbytes); 741789Sahrens 7421576Smarks /* 7431576Smarks * Clear Set-UID/Set-GID bits on successful write if not 7441576Smarks * privileged and at least one of the excute bits is set. 7451576Smarks * 7461576Smarks * It would be nice to to this after all writes have 7471576Smarks * been done, but that would still expose the ISUID/ISGID 7481576Smarks * to another app after the partial write is committed. 7491576Smarks */ 7501576Smarks 7511576Smarks mutex_enter(&zp->z_acl_lock); 7521576Smarks if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7531576Smarks (S_IXUSR >> 6))) != 0 && 7541576Smarks (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7551576Smarks secpolicy_vnode_setid_retain(cr, 7561576Smarks (zp->z_phys->zp_mode & S_ISUID) != 0 && 7571576Smarks zp->z_phys->zp_uid == 0) != 0) { 7581576Smarks zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 7591576Smarks } 7601576Smarks mutex_exit(&zp->z_acl_lock); 7611576Smarks 762789Sahrens n -= nbytes; 763789Sahrens if (n <= 0) 764789Sahrens break; 765789Sahrens 766789Sahrens /* 767789Sahrens * We have more work ahead of us, so wrap up this transaction 768789Sahrens * and start another. Exact same logic as tx_done below. 769789Sahrens */ 770789Sahrens while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 771789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 772789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 773789Sahrens uio->uio_loffset); 774789Sahrens } 775789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 7762638Sperrin zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 777789Sahrens ioflag, uio); 778789Sahrens dmu_tx_commit(tx); 779789Sahrens 780789Sahrens /* 781789Sahrens * Start another transaction. 782789Sahrens */ 783789Sahrens woff = uio->uio_loffset; 784789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 785789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 786789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 787789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 788789Sahrens if (error) { 789789Sahrens if (error == ERESTART && 790789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 7912113Sahrens dmu_tx_wait(tx); 7922113Sahrens dmu_tx_abort(tx); 793789Sahrens goto top; 794789Sahrens } 7952113Sahrens dmu_tx_abort(tx); 796789Sahrens goto no_tx_done; 797789Sahrens } 798789Sahrens } 799789Sahrens 800789Sahrens tx_done: 801789Sahrens 802789Sahrens if (tx_bytes != 0) { 803789Sahrens /* 804789Sahrens * Update the file size if it has changed; account 805789Sahrens * for possible concurrent updates. 806789Sahrens */ 807789Sahrens while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 808789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 809789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 810789Sahrens uio->uio_loffset); 811789Sahrens } 812789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 8132638Sperrin zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 814789Sahrens ioflag, uio); 815789Sahrens } 816789Sahrens dmu_tx_commit(tx); 817789Sahrens 818789Sahrens 819789Sahrens no_tx_done: 820789Sahrens 8212237Smaybee zfs_range_unlock(rl); 822789Sahrens 823789Sahrens /* 824789Sahrens * If we're in replay mode, or we made no progress, return error. 825789Sahrens * Otherwise, it's at least a partial write, so it's successful. 826789Sahrens */ 827789Sahrens if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 828789Sahrens ZFS_EXIT(zfsvfs); 829789Sahrens return (error); 830789Sahrens } 831789Sahrens 8322638Sperrin if (ioflag & (FSYNC | FDSYNC)) 8332638Sperrin zil_commit(zilog, zp->z_last_itx, zp->z_id); 834789Sahrens 835789Sahrens ZFS_EXIT(zfsvfs); 836789Sahrens return (0); 837789Sahrens } 838789Sahrens 8392237Smaybee void 8403063Sperrin zfs_get_done(dmu_buf_t *db, void *vzgd) 8412237Smaybee { 8423063Sperrin zgd_t *zgd = (zgd_t *)vzgd; 8433063Sperrin rl_t *rl = zgd->zgd_rl; 8442237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 8452237Smaybee 8463063Sperrin dmu_buf_rele(db, vzgd); 8472237Smaybee zfs_range_unlock(rl); 8482237Smaybee VN_RELE(vp); 8493063Sperrin zil_add_vdev(zgd->zgd_zilog, DVA_GET_VDEV(BP_IDENTITY(zgd->zgd_bp))); 8503063Sperrin kmem_free(zgd, sizeof (zgd_t)); 8512237Smaybee } 8522237Smaybee 853789Sahrens /* 854789Sahrens * Get data to generate a TX_WRITE intent log record. 855789Sahrens */ 856789Sahrens int 8572237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 858789Sahrens { 859789Sahrens zfsvfs_t *zfsvfs = arg; 860789Sahrens objset_t *os = zfsvfs->z_os; 861789Sahrens znode_t *zp; 862789Sahrens uint64_t off = lr->lr_offset; 8632237Smaybee dmu_buf_t *db; 8641669Sperrin rl_t *rl; 8653063Sperrin zgd_t *zgd; 866789Sahrens int dlen = lr->lr_length; /* length of user data */ 867789Sahrens int error = 0; 868789Sahrens 8693063Sperrin ASSERT(zio); 870789Sahrens ASSERT(dlen != 0); 871789Sahrens 872789Sahrens /* 8731669Sperrin * Nothing to do if the file has been removed 874789Sahrens */ 875789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 876789Sahrens return (ENOENT); 8771669Sperrin if (zp->z_reap) { 878789Sahrens VN_RELE(ZTOV(zp)); 879789Sahrens return (ENOENT); 880789Sahrens } 881789Sahrens 882789Sahrens /* 883789Sahrens * Write records come in two flavors: immediate and indirect. 884789Sahrens * For small writes it's cheaper to store the data with the 885789Sahrens * log record (immediate); for large writes it's cheaper to 886789Sahrens * sync the data and get a pointer to it (indirect) so that 887789Sahrens * we don't have to write the data twice. 888789Sahrens */ 8891669Sperrin if (buf != NULL) { /* immediate write */ 8901669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 8911669Sperrin /* test for truncation needs to be done while range locked */ 8921669Sperrin if (off >= zp->z_phys->zp_size) { 8931669Sperrin error = ENOENT; 8941669Sperrin goto out; 8951669Sperrin } 8962449Smaybee VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 8971669Sperrin } else { /* indirect write */ 8981669Sperrin uint64_t boff; /* block starting offset */ 8991669Sperrin 9002449Smaybee ASSERT3U(dlen, <=, zp->z_blksz); 901789Sahrens /* 9021669Sperrin * Have to lock the whole block to ensure when it's 9031669Sperrin * written out and it's checksum is being calculated 9041669Sperrin * that no one can change the data. We need to re-check 9051669Sperrin * blocksize after we get the lock in case it's changed! 906789Sahrens */ 9071669Sperrin for (;;) { 9081941Sperrin if (ISP2(zp->z_blksz)) { 9091941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 9101941Sperrin uint64_t); 9111941Sperrin } else { 9121941Sperrin boff = 0; 9131941Sperrin } 9141669Sperrin dlen = zp->z_blksz; 9151669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 9161669Sperrin if (zp->z_blksz == dlen) 9171669Sperrin break; 9182237Smaybee zfs_range_unlock(rl); 9191669Sperrin } 9201669Sperrin /* test for truncation needs to be done while range locked */ 9211669Sperrin if (off >= zp->z_phys->zp_size) { 9221669Sperrin error = ENOENT; 9231669Sperrin goto out; 9241669Sperrin } 9253063Sperrin zgd = (zgd_t *)kmem_alloc(sizeof (zgd_t), KM_SLEEP); 9263063Sperrin zgd->zgd_rl = rl; 9273063Sperrin zgd->zgd_zilog = zfsvfs->z_log; 9283063Sperrin zgd->zgd_bp = &lr->lr_blkptr; 9293063Sperrin VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, zgd, &db)); 9302237Smaybee ASSERT(boff == db->db_offset); 9312237Smaybee lr->lr_blkoff = off - boff; 9322237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 9333063Sperrin lr->lr_common.lrc_txg, zfs_get_done, zgd); 9343063Sperrin if (error == 0) { 9353063Sperrin zil_add_vdev(zfsvfs->z_log, 9363063Sperrin DVA_GET_VDEV(BP_IDENTITY(&lr->lr_blkptr))); 9373063Sperrin } 9382237Smaybee /* 9392237Smaybee * If we get EINPROGRESS, then we need to wait for a 9402237Smaybee * write IO initiated by dmu_sync() to complete before 9412638Sperrin * we can release this dbuf. We will finish everything 9422237Smaybee * up in the zfs_get_done() callback. 9432237Smaybee */ 9442237Smaybee if (error == EINPROGRESS) 9452237Smaybee return (0); 9463063Sperrin dmu_buf_rele(db, zgd); 9473063Sperrin kmem_free(zgd, sizeof (zgd_t)); 948789Sahrens } 9491669Sperrin out: 9502237Smaybee zfs_range_unlock(rl); 951789Sahrens VN_RELE(ZTOV(zp)); 952789Sahrens return (error); 953789Sahrens } 954789Sahrens 955789Sahrens /*ARGSUSED*/ 956789Sahrens static int 957789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr) 958789Sahrens { 959789Sahrens znode_t *zp = VTOZ(vp); 960789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 961789Sahrens int error; 962789Sahrens 963789Sahrens ZFS_ENTER(zfsvfs); 964789Sahrens error = zfs_zaccess_rwx(zp, mode, cr); 965789Sahrens ZFS_EXIT(zfsvfs); 966789Sahrens return (error); 967789Sahrens } 968789Sahrens 969789Sahrens /* 970789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 971789Sahrens * If it exists, return a held vnode reference for it. 972789Sahrens * 973789Sahrens * IN: dvp - vnode of directory to search. 974789Sahrens * nm - name of entry to lookup. 975789Sahrens * pnp - full pathname to lookup [UNUSED]. 976789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 977789Sahrens * rdir - root directory vnode [UNUSED]. 978789Sahrens * cr - credentials of caller. 979789Sahrens * 980789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 981789Sahrens * 982789Sahrens * RETURN: 0 if success 983789Sahrens * error code if failure 984789Sahrens * 985789Sahrens * Timestamps: 986789Sahrens * NA 987789Sahrens */ 988789Sahrens /* ARGSUSED */ 989789Sahrens static int 990789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 991789Sahrens int flags, vnode_t *rdir, cred_t *cr) 992789Sahrens { 993789Sahrens 994789Sahrens znode_t *zdp = VTOZ(dvp); 995789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 996789Sahrens int error; 997789Sahrens 998789Sahrens ZFS_ENTER(zfsvfs); 999789Sahrens 1000789Sahrens *vpp = NULL; 1001789Sahrens 1002789Sahrens if (flags & LOOKUP_XATTR) { 1003789Sahrens /* 1004789Sahrens * We don't allow recursive attributes.. 1005789Sahrens * Maybe someday we will. 1006789Sahrens */ 1007789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 1008789Sahrens ZFS_EXIT(zfsvfs); 1009789Sahrens return (EINVAL); 1010789Sahrens } 1011789Sahrens 1012789Sahrens if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) { 1013789Sahrens ZFS_EXIT(zfsvfs); 1014789Sahrens return (error); 1015789Sahrens } 1016789Sahrens 1017789Sahrens /* 1018789Sahrens * Do we have permission to get into attribute directory? 1019789Sahrens */ 1020789Sahrens 1021789Sahrens if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 1022789Sahrens VN_RELE(*vpp); 1023789Sahrens } 1024789Sahrens 1025789Sahrens ZFS_EXIT(zfsvfs); 1026789Sahrens return (error); 1027789Sahrens } 1028789Sahrens 10291512Sek110237 if (dvp->v_type != VDIR) { 10301512Sek110237 ZFS_EXIT(zfsvfs); 10311460Smarks return (ENOTDIR); 10321512Sek110237 } 10331460Smarks 1034789Sahrens /* 1035789Sahrens * Check accessibility of directory. 1036789Sahrens */ 1037789Sahrens 1038789Sahrens if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 1039789Sahrens ZFS_EXIT(zfsvfs); 1040789Sahrens return (error); 1041789Sahrens } 1042789Sahrens 1043789Sahrens if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 1044789Sahrens 1045789Sahrens /* 1046789Sahrens * Convert device special files 1047789Sahrens */ 1048789Sahrens if (IS_DEVVP(*vpp)) { 1049789Sahrens vnode_t *svp; 1050789Sahrens 1051789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1052789Sahrens VN_RELE(*vpp); 1053789Sahrens if (svp == NULL) 1054789Sahrens error = ENOSYS; 1055789Sahrens else 1056789Sahrens *vpp = svp; 1057789Sahrens } 1058789Sahrens } 1059789Sahrens 1060789Sahrens ZFS_EXIT(zfsvfs); 1061789Sahrens return (error); 1062789Sahrens } 1063789Sahrens 1064789Sahrens /* 1065789Sahrens * Attempt to create a new entry in a directory. If the entry 1066789Sahrens * already exists, truncate the file if permissible, else return 1067789Sahrens * an error. Return the vp of the created or trunc'd file. 1068789Sahrens * 1069789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1070789Sahrens * name - name of new file entry. 1071789Sahrens * vap - attributes of new file. 1072789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1073789Sahrens * mode - mode to open file with. 1074789Sahrens * cr - credentials of caller. 1075789Sahrens * flag - large file flag [UNUSED]. 1076789Sahrens * 1077789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1078789Sahrens * 1079789Sahrens * RETURN: 0 if success 1080789Sahrens * error code if failure 1081789Sahrens * 1082789Sahrens * Timestamps: 1083789Sahrens * dvp - ctime|mtime updated if new entry created 1084789Sahrens * vp - ctime|mtime always, atime if new 1085789Sahrens */ 1086789Sahrens /* ARGSUSED */ 1087789Sahrens static int 1088789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1089789Sahrens int mode, vnode_t **vpp, cred_t *cr, int flag) 1090789Sahrens { 1091789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1092789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1093789Sahrens zilog_t *zilog = zfsvfs->z_log; 1094789Sahrens objset_t *os = zfsvfs->z_os; 1095789Sahrens zfs_dirlock_t *dl; 1096789Sahrens dmu_tx_t *tx; 1097789Sahrens int error; 1098789Sahrens uint64_t zoid; 1099789Sahrens 1100789Sahrens ZFS_ENTER(zfsvfs); 1101789Sahrens 1102789Sahrens top: 1103789Sahrens *vpp = NULL; 1104789Sahrens 1105789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1106789Sahrens vap->va_mode &= ~VSVTX; 1107789Sahrens 1108789Sahrens if (*name == '\0') { 1109789Sahrens /* 1110789Sahrens * Null component name refers to the directory itself. 1111789Sahrens */ 1112789Sahrens VN_HOLD(dvp); 1113789Sahrens zp = dzp; 1114789Sahrens dl = NULL; 1115789Sahrens error = 0; 1116789Sahrens } else { 1117789Sahrens /* possible VN_HOLD(zp) */ 1118789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1119789Sahrens if (strcmp(name, "..") == 0) 1120789Sahrens error = EISDIR; 1121789Sahrens ZFS_EXIT(zfsvfs); 1122789Sahrens return (error); 1123789Sahrens } 1124789Sahrens } 1125789Sahrens 1126789Sahrens zoid = zp ? zp->z_id : -1ULL; 1127789Sahrens 1128789Sahrens if (zp == NULL) { 1129789Sahrens /* 1130789Sahrens * Create a new file object and update the directory 1131789Sahrens * to reference it. 1132789Sahrens */ 1133789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1134789Sahrens goto out; 1135789Sahrens } 1136789Sahrens 1137789Sahrens /* 1138789Sahrens * We only support the creation of regular files in 1139789Sahrens * extended attribute directories. 1140789Sahrens */ 1141789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1142789Sahrens (vap->va_type != VREG)) { 1143789Sahrens error = EINVAL; 1144789Sahrens goto out; 1145789Sahrens } 1146789Sahrens 1147789Sahrens tx = dmu_tx_create(os); 1148789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1149789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 11501544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1151789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1152789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1153789Sahrens 0, SPA_MAXBLOCKSIZE); 1154789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1155789Sahrens if (error) { 1156789Sahrens zfs_dirent_unlock(dl); 1157789Sahrens if (error == ERESTART && 1158789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 11592113Sahrens dmu_tx_wait(tx); 11602113Sahrens dmu_tx_abort(tx); 1161789Sahrens goto top; 1162789Sahrens } 11632113Sahrens dmu_tx_abort(tx); 1164789Sahrens ZFS_EXIT(zfsvfs); 1165789Sahrens return (error); 1166789Sahrens } 1167789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1168789Sahrens ASSERT(zp->z_id == zoid); 1169789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 11702638Sperrin zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1171789Sahrens dmu_tx_commit(tx); 1172789Sahrens } else { 1173789Sahrens /* 1174789Sahrens * A directory entry already exists for this name. 1175789Sahrens */ 1176789Sahrens /* 1177789Sahrens * Can't truncate an existing file if in exclusive mode. 1178789Sahrens */ 1179789Sahrens if (excl == EXCL) { 1180789Sahrens error = EEXIST; 1181789Sahrens goto out; 1182789Sahrens } 1183789Sahrens /* 1184789Sahrens * Can't open a directory for writing. 1185789Sahrens */ 1186789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1187789Sahrens error = EISDIR; 1188789Sahrens goto out; 1189789Sahrens } 1190789Sahrens /* 1191789Sahrens * Verify requested access to file. 1192789Sahrens */ 1193789Sahrens if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1194789Sahrens goto out; 1195789Sahrens } 1196789Sahrens 1197789Sahrens mutex_enter(&dzp->z_lock); 1198789Sahrens dzp->z_seq++; 1199789Sahrens mutex_exit(&dzp->z_lock); 1200789Sahrens 12011878Smaybee /* 12021878Smaybee * Truncate regular files if requested. 12031878Smaybee */ 12041878Smaybee if ((ZTOV(zp)->v_type == VREG) && 12051878Smaybee (zp->z_phys->zp_size != 0) && 1206789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 12071878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 12081878Smaybee if (error == ERESTART && 12091878Smaybee zfsvfs->z_assign == TXG_NOWAIT) { 12102113Sahrens /* NB: we already did dmu_tx_wait() */ 12111878Smaybee zfs_dirent_unlock(dl); 12122365Sperrin VN_RELE(ZTOV(zp)); 12131878Smaybee goto top; 1214789Sahrens } 1215789Sahrens } 1216789Sahrens } 1217789Sahrens out: 1218789Sahrens 1219789Sahrens if (dl) 1220789Sahrens zfs_dirent_unlock(dl); 1221789Sahrens 1222789Sahrens if (error) { 1223789Sahrens if (zp) 1224789Sahrens VN_RELE(ZTOV(zp)); 1225789Sahrens } else { 1226789Sahrens *vpp = ZTOV(zp); 1227789Sahrens /* 1228789Sahrens * If vnode is for a device return a specfs vnode instead. 1229789Sahrens */ 1230789Sahrens if (IS_DEVVP(*vpp)) { 1231789Sahrens struct vnode *svp; 1232789Sahrens 1233789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1234789Sahrens VN_RELE(*vpp); 1235789Sahrens if (svp == NULL) { 1236789Sahrens error = ENOSYS; 1237789Sahrens } 1238789Sahrens *vpp = svp; 1239789Sahrens } 1240789Sahrens } 1241789Sahrens 1242789Sahrens ZFS_EXIT(zfsvfs); 1243789Sahrens return (error); 1244789Sahrens } 1245789Sahrens 1246789Sahrens /* 1247789Sahrens * Remove an entry from a directory. 1248789Sahrens * 1249789Sahrens * IN: dvp - vnode of directory to remove entry from. 1250789Sahrens * name - name of entry to remove. 1251789Sahrens * cr - credentials of caller. 1252789Sahrens * 1253789Sahrens * RETURN: 0 if success 1254789Sahrens * error code if failure 1255789Sahrens * 1256789Sahrens * Timestamps: 1257789Sahrens * dvp - ctime|mtime 1258789Sahrens * vp - ctime (if nlink > 0) 1259789Sahrens */ 1260789Sahrens static int 1261789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1262789Sahrens { 1263789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1264789Sahrens znode_t *xzp = NULL; 1265789Sahrens vnode_t *vp; 1266789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1267789Sahrens zilog_t *zilog = zfsvfs->z_log; 1268789Sahrens uint64_t acl_obj, xattr_obj; 1269789Sahrens zfs_dirlock_t *dl; 1270789Sahrens dmu_tx_t *tx; 1271789Sahrens int may_delete_now, delete_now = FALSE; 1272789Sahrens int reaped; 1273789Sahrens int error; 1274789Sahrens 1275789Sahrens ZFS_ENTER(zfsvfs); 1276789Sahrens 1277789Sahrens top: 1278789Sahrens /* 1279789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1280789Sahrens */ 1281789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1282789Sahrens ZFS_EXIT(zfsvfs); 1283789Sahrens return (error); 1284789Sahrens } 1285789Sahrens 1286789Sahrens vp = ZTOV(zp); 1287789Sahrens 1288789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1289789Sahrens goto out; 1290789Sahrens } 1291789Sahrens 1292789Sahrens /* 1293789Sahrens * Need to use rmdir for removing directories. 1294789Sahrens */ 1295789Sahrens if (vp->v_type == VDIR) { 1296789Sahrens error = EPERM; 1297789Sahrens goto out; 1298789Sahrens } 1299789Sahrens 1300789Sahrens vnevent_remove(vp); 1301789Sahrens 13021484Sek110237 dnlc_remove(dvp, name); 13031484Sek110237 1304789Sahrens mutex_enter(&vp->v_lock); 1305789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1306789Sahrens mutex_exit(&vp->v_lock); 1307789Sahrens 1308789Sahrens /* 1309789Sahrens * We may delete the znode now, or we may put it on the delete queue; 1310789Sahrens * it depends on whether we're the last link, and on whether there are 1311789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1312789Sahrens * allow for either case. 1313789Sahrens */ 1314789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 13151544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1316789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 1317789Sahrens if (may_delete_now) 1318789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1319789Sahrens 1320789Sahrens /* are there any extended attributes? */ 1321789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1322789Sahrens /* 1323789Sahrens * XXX - There is a possibility that the delete 1324789Sahrens * of the parent file could succeed, but then we get 1325789Sahrens * an ENOSPC when we try to delete the xattrs... 1326789Sahrens * so we would need to re-try the deletes periodically 1327789Sahrens */ 1328789Sahrens /* XXX - do we need this if we are deleting? */ 1329789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1330789Sahrens } 1331789Sahrens 1332789Sahrens /* are there any additional acls */ 1333789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1334789Sahrens may_delete_now) 1335789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1336789Sahrens 1337789Sahrens /* charge as an update -- would be nice not to charge at all */ 13381544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1339789Sahrens 1340789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1341789Sahrens if (error) { 1342789Sahrens zfs_dirent_unlock(dl); 1343789Sahrens VN_RELE(vp); 1344789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 13452113Sahrens dmu_tx_wait(tx); 13462113Sahrens dmu_tx_abort(tx); 1347789Sahrens goto top; 1348789Sahrens } 13492113Sahrens dmu_tx_abort(tx); 1350789Sahrens ZFS_EXIT(zfsvfs); 1351789Sahrens return (error); 1352789Sahrens } 1353789Sahrens 1354789Sahrens /* 1355789Sahrens * Remove the directory entry. 1356789Sahrens */ 1357789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, &reaped); 1358789Sahrens 1359789Sahrens if (error) { 1360789Sahrens dmu_tx_commit(tx); 1361789Sahrens goto out; 1362789Sahrens } 1363789Sahrens 1364789Sahrens if (reaped) { 1365789Sahrens mutex_enter(&vp->v_lock); 1366789Sahrens delete_now = may_delete_now && 1367789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1368789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1369789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1370789Sahrens mutex_exit(&vp->v_lock); 1371789Sahrens } 1372789Sahrens 1373789Sahrens if (delete_now) { 1374789Sahrens if (zp->z_phys->zp_xattr) { 1375789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1376789Sahrens ASSERT3U(error, ==, 0); 1377789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1378789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1379789Sahrens mutex_enter(&xzp->z_lock); 1380789Sahrens xzp->z_reap = 1; 1381789Sahrens xzp->z_phys->zp_links = 0; 1382789Sahrens mutex_exit(&xzp->z_lock); 1383789Sahrens zfs_dq_add(xzp, tx); 1384789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1385789Sahrens } 1386789Sahrens mutex_enter(&zp->z_lock); 1387789Sahrens mutex_enter(&vp->v_lock); 1388789Sahrens vp->v_count--; 1389789Sahrens ASSERT3U(vp->v_count, ==, 0); 1390789Sahrens mutex_exit(&vp->v_lock); 1391789Sahrens mutex_exit(&zp->z_lock); 1392789Sahrens zfs_znode_delete(zp, tx); 1393789Sahrens VFS_RELE(zfsvfs->z_vfs); 1394789Sahrens } else if (reaped) { 1395789Sahrens zfs_dq_add(zp, tx); 1396789Sahrens } 1397789Sahrens 13982638Sperrin zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1399789Sahrens 1400789Sahrens dmu_tx_commit(tx); 1401789Sahrens out: 1402789Sahrens zfs_dirent_unlock(dl); 1403789Sahrens 1404789Sahrens if (!delete_now) { 1405789Sahrens VN_RELE(vp); 1406789Sahrens } else if (xzp) { 1407789Sahrens /* this rele delayed to prevent nesting transactions */ 1408789Sahrens VN_RELE(ZTOV(xzp)); 1409789Sahrens } 1410789Sahrens 1411789Sahrens ZFS_EXIT(zfsvfs); 1412789Sahrens return (error); 1413789Sahrens } 1414789Sahrens 1415789Sahrens /* 1416789Sahrens * Create a new directory and insert it into dvp using the name 1417789Sahrens * provided. Return a pointer to the inserted directory. 1418789Sahrens * 1419789Sahrens * IN: dvp - vnode of directory to add subdir to. 1420789Sahrens * dirname - name of new directory. 1421789Sahrens * vap - attributes of new directory. 1422789Sahrens * cr - credentials of caller. 1423789Sahrens * 1424789Sahrens * OUT: vpp - vnode of created directory. 1425789Sahrens * 1426789Sahrens * RETURN: 0 if success 1427789Sahrens * error code if failure 1428789Sahrens * 1429789Sahrens * Timestamps: 1430789Sahrens * dvp - ctime|mtime updated 1431789Sahrens * vp - ctime|mtime|atime updated 1432789Sahrens */ 1433789Sahrens static int 1434789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1435789Sahrens { 1436789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1437789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1438789Sahrens zilog_t *zilog = zfsvfs->z_log; 1439789Sahrens zfs_dirlock_t *dl; 1440789Sahrens uint64_t zoid = 0; 1441789Sahrens dmu_tx_t *tx; 1442789Sahrens int error; 1443789Sahrens 1444789Sahrens ASSERT(vap->va_type == VDIR); 1445789Sahrens 1446789Sahrens ZFS_ENTER(zfsvfs); 1447789Sahrens 1448789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1449789Sahrens ZFS_EXIT(zfsvfs); 1450789Sahrens return (EINVAL); 1451789Sahrens } 1452789Sahrens top: 1453789Sahrens *vpp = NULL; 1454789Sahrens 1455789Sahrens /* 1456789Sahrens * First make sure the new directory doesn't exist. 1457789Sahrens */ 1458789Sahrens if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1459789Sahrens ZFS_EXIT(zfsvfs); 1460789Sahrens return (error); 1461789Sahrens } 1462789Sahrens 14631231Smarks if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 14641231Smarks zfs_dirent_unlock(dl); 14651231Smarks ZFS_EXIT(zfsvfs); 14661231Smarks return (error); 14671231Smarks } 14681231Smarks 1469789Sahrens /* 1470789Sahrens * Add a new entry to the directory. 1471789Sahrens */ 1472789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14731544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 14741544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1475789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1476789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1477789Sahrens 0, SPA_MAXBLOCKSIZE); 1478789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1479789Sahrens if (error) { 1480789Sahrens zfs_dirent_unlock(dl); 1481789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 14822113Sahrens dmu_tx_wait(tx); 14832113Sahrens dmu_tx_abort(tx); 1484789Sahrens goto top; 1485789Sahrens } 14862113Sahrens dmu_tx_abort(tx); 1487789Sahrens ZFS_EXIT(zfsvfs); 1488789Sahrens return (error); 1489789Sahrens } 1490789Sahrens 1491789Sahrens /* 1492789Sahrens * Create new node. 1493789Sahrens */ 1494789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1495789Sahrens 1496789Sahrens /* 1497789Sahrens * Now put new name in parent dir. 1498789Sahrens */ 1499789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1500789Sahrens 1501789Sahrens *vpp = ZTOV(zp); 1502789Sahrens 15032638Sperrin zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1504789Sahrens dmu_tx_commit(tx); 1505789Sahrens 1506789Sahrens zfs_dirent_unlock(dl); 1507789Sahrens 1508789Sahrens ZFS_EXIT(zfsvfs); 1509789Sahrens return (0); 1510789Sahrens } 1511789Sahrens 1512789Sahrens /* 1513789Sahrens * Remove a directory subdir entry. If the current working 1514789Sahrens * directory is the same as the subdir to be removed, the 1515789Sahrens * remove will fail. 1516789Sahrens * 1517789Sahrens * IN: dvp - vnode of directory to remove from. 1518789Sahrens * name - name of directory to be removed. 1519789Sahrens * cwd - vnode of current working directory. 1520789Sahrens * cr - credentials of caller. 1521789Sahrens * 1522789Sahrens * RETURN: 0 if success 1523789Sahrens * error code if failure 1524789Sahrens * 1525789Sahrens * Timestamps: 1526789Sahrens * dvp - ctime|mtime updated 1527789Sahrens */ 1528789Sahrens static int 1529789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1530789Sahrens { 1531789Sahrens znode_t *dzp = VTOZ(dvp); 1532789Sahrens znode_t *zp; 1533789Sahrens vnode_t *vp; 1534789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1535789Sahrens zilog_t *zilog = zfsvfs->z_log; 1536789Sahrens zfs_dirlock_t *dl; 1537789Sahrens dmu_tx_t *tx; 1538789Sahrens int error; 1539789Sahrens 1540789Sahrens ZFS_ENTER(zfsvfs); 1541789Sahrens 1542789Sahrens top: 1543789Sahrens zp = NULL; 1544789Sahrens 1545789Sahrens /* 1546789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1547789Sahrens */ 1548789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1549789Sahrens ZFS_EXIT(zfsvfs); 1550789Sahrens return (error); 1551789Sahrens } 1552789Sahrens 1553789Sahrens vp = ZTOV(zp); 1554789Sahrens 1555789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1556789Sahrens goto out; 1557789Sahrens } 1558789Sahrens 1559789Sahrens if (vp->v_type != VDIR) { 1560789Sahrens error = ENOTDIR; 1561789Sahrens goto out; 1562789Sahrens } 1563789Sahrens 1564789Sahrens if (vp == cwd) { 1565789Sahrens error = EINVAL; 1566789Sahrens goto out; 1567789Sahrens } 1568789Sahrens 1569789Sahrens vnevent_rmdir(vp); 1570789Sahrens 1571789Sahrens /* 1572789Sahrens * Grab a lock on the parent pointer make sure we play well 1573789Sahrens * with the treewalk and directory rename code. 1574789Sahrens */ 1575789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1576789Sahrens 1577789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 15781544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1579789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 15801544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1581789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1582789Sahrens if (error) { 1583789Sahrens rw_exit(&zp->z_parent_lock); 1584789Sahrens zfs_dirent_unlock(dl); 1585789Sahrens VN_RELE(vp); 1586789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 15872113Sahrens dmu_tx_wait(tx); 15882113Sahrens dmu_tx_abort(tx); 1589789Sahrens goto top; 1590789Sahrens } 15912113Sahrens dmu_tx_abort(tx); 1592789Sahrens ZFS_EXIT(zfsvfs); 1593789Sahrens return (error); 1594789Sahrens } 1595789Sahrens 1596789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1597789Sahrens 1598789Sahrens if (error == 0) 15992638Sperrin zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1600789Sahrens 1601789Sahrens dmu_tx_commit(tx); 1602789Sahrens 1603789Sahrens rw_exit(&zp->z_parent_lock); 1604789Sahrens out: 1605789Sahrens zfs_dirent_unlock(dl); 1606789Sahrens 1607789Sahrens VN_RELE(vp); 1608789Sahrens 1609789Sahrens ZFS_EXIT(zfsvfs); 1610789Sahrens return (error); 1611789Sahrens } 1612789Sahrens 1613789Sahrens /* 1614789Sahrens * Read as many directory entries as will fit into the provided 1615789Sahrens * buffer from the given directory cursor position (specified in 1616789Sahrens * the uio structure. 1617789Sahrens * 1618789Sahrens * IN: vp - vnode of directory to read. 1619789Sahrens * uio - structure supplying read location, range info, 1620789Sahrens * and return buffer. 1621789Sahrens * cr - credentials of caller. 1622789Sahrens * 1623789Sahrens * OUT: uio - updated offset and range, buffer filled. 1624789Sahrens * eofp - set to true if end-of-file detected. 1625789Sahrens * 1626789Sahrens * RETURN: 0 if success 1627789Sahrens * error code if failure 1628789Sahrens * 1629789Sahrens * Timestamps: 1630789Sahrens * vp - atime updated 1631789Sahrens * 1632789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1633789Sahrens * This allows us to use the low range for "special" directory entries: 1634789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1635789Sahrens * we use the offset 2 for the '.zfs' directory. 1636789Sahrens */ 1637789Sahrens /* ARGSUSED */ 1638789Sahrens static int 1639789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1640789Sahrens { 1641789Sahrens znode_t *zp = VTOZ(vp); 1642789Sahrens iovec_t *iovp; 1643789Sahrens dirent64_t *odp; 1644789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1645869Sperrin objset_t *os; 1646789Sahrens caddr_t outbuf; 1647789Sahrens size_t bufsize; 1648789Sahrens zap_cursor_t zc; 1649789Sahrens zap_attribute_t zap; 1650789Sahrens uint_t bytes_wanted; 1651789Sahrens ushort_t this_reclen; 1652789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1653789Sahrens off64_t *next; 1654789Sahrens int local_eof; 1655869Sperrin int outcount; 1656869Sperrin int error; 1657869Sperrin uint8_t prefetch; 1658789Sahrens 1659789Sahrens ZFS_ENTER(zfsvfs); 1660789Sahrens 1661789Sahrens /* 1662789Sahrens * If we are not given an eof variable, 1663789Sahrens * use a local one. 1664789Sahrens */ 1665789Sahrens if (eofp == NULL) 1666789Sahrens eofp = &local_eof; 1667789Sahrens 1668789Sahrens /* 1669789Sahrens * Check for valid iov_len. 1670789Sahrens */ 1671789Sahrens if (uio->uio_iov->iov_len <= 0) { 1672789Sahrens ZFS_EXIT(zfsvfs); 1673789Sahrens return (EINVAL); 1674789Sahrens } 1675789Sahrens 1676789Sahrens /* 1677789Sahrens * Quit if directory has been removed (posix) 1678789Sahrens */ 1679789Sahrens if ((*eofp = zp->z_reap) != 0) { 1680789Sahrens ZFS_EXIT(zfsvfs); 1681789Sahrens return (0); 1682789Sahrens } 1683789Sahrens 1684869Sperrin error = 0; 1685869Sperrin os = zfsvfs->z_os; 1686869Sperrin offset = uio->uio_loffset; 1687869Sperrin prefetch = zp->z_zn_prefetch; 1688869Sperrin 1689789Sahrens /* 1690789Sahrens * Initialize the iterator cursor. 1691789Sahrens */ 1692789Sahrens if (offset <= 3) { 1693789Sahrens /* 1694789Sahrens * Start iteration from the beginning of the directory. 1695789Sahrens */ 1696869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1697789Sahrens } else { 1698789Sahrens /* 1699789Sahrens * The offset is a serialized cursor. 1700789Sahrens */ 1701869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1702789Sahrens } 1703789Sahrens 1704789Sahrens /* 1705789Sahrens * Get space to change directory entries into fs independent format. 1706789Sahrens */ 1707789Sahrens iovp = uio->uio_iov; 1708789Sahrens bytes_wanted = iovp->iov_len; 1709789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1710789Sahrens bufsize = bytes_wanted; 1711789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1712789Sahrens odp = (struct dirent64 *)outbuf; 1713789Sahrens } else { 1714789Sahrens bufsize = bytes_wanted; 1715789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1716789Sahrens } 1717789Sahrens 1718789Sahrens /* 1719789Sahrens * Transform to file-system independent format 1720789Sahrens */ 1721789Sahrens outcount = 0; 1722789Sahrens while (outcount < bytes_wanted) { 1723789Sahrens /* 1724789Sahrens * Special case `.', `..', and `.zfs'. 1725789Sahrens */ 1726789Sahrens if (offset == 0) { 1727789Sahrens (void) strcpy(zap.za_name, "."); 1728789Sahrens zap.za_first_integer = zp->z_id; 1729789Sahrens this_reclen = DIRENT64_RECLEN(1); 1730789Sahrens } else if (offset == 1) { 1731789Sahrens (void) strcpy(zap.za_name, ".."); 1732789Sahrens zap.za_first_integer = zp->z_phys->zp_parent; 1733789Sahrens this_reclen = DIRENT64_RECLEN(2); 1734789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1735789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1736789Sahrens zap.za_first_integer = ZFSCTL_INO_ROOT; 1737789Sahrens this_reclen = 1738789Sahrens DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1); 1739789Sahrens } else { 1740789Sahrens /* 1741789Sahrens * Grab next entry. 1742789Sahrens */ 1743789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1744789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1745789Sahrens break; 1746789Sahrens else 1747789Sahrens goto update; 1748789Sahrens } 1749789Sahrens 1750789Sahrens if (zap.za_integer_length != 8 || 1751789Sahrens zap.za_num_integers != 1) { 1752789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1753789Sahrens "entry, obj = %lld, offset = %lld\n", 1754789Sahrens (u_longlong_t)zp->z_id, 1755789Sahrens (u_longlong_t)offset); 1756789Sahrens error = ENXIO; 1757789Sahrens goto update; 1758789Sahrens } 1759789Sahrens this_reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1760789Sahrens } 1761789Sahrens 1762789Sahrens /* 1763789Sahrens * Will this entry fit in the buffer? 1764789Sahrens */ 1765789Sahrens if (outcount + this_reclen > bufsize) { 1766789Sahrens /* 1767789Sahrens * Did we manage to fit anything in the buffer? 1768789Sahrens */ 1769789Sahrens if (!outcount) { 1770789Sahrens error = EINVAL; 1771789Sahrens goto update; 1772789Sahrens } 1773789Sahrens break; 1774789Sahrens } 1775789Sahrens /* 1776789Sahrens * Add this entry: 1777789Sahrens */ 1778789Sahrens odp->d_ino = (ino64_t)zap.za_first_integer; 1779789Sahrens odp->d_reclen = (ushort_t)this_reclen; 1780789Sahrens /* NOTE: d_off is the offset for the *next* entry */ 1781789Sahrens next = &(odp->d_off); 1782789Sahrens (void) strncpy(odp->d_name, zap.za_name, 1783789Sahrens DIRENT64_NAMELEN(this_reclen)); 1784789Sahrens outcount += this_reclen; 1785789Sahrens odp = (dirent64_t *)((intptr_t)odp + this_reclen); 1786789Sahrens 1787789Sahrens ASSERT(outcount <= bufsize); 1788789Sahrens 1789789Sahrens /* Prefetch znode */ 1790869Sperrin if (prefetch) 1791869Sperrin dmu_prefetch(os, zap.za_first_integer, 0, 0); 1792789Sahrens 1793789Sahrens /* 1794789Sahrens * Move to the next entry, fill in the previous offset. 1795789Sahrens */ 1796789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1797789Sahrens zap_cursor_advance(&zc); 1798789Sahrens offset = zap_cursor_serialize(&zc); 1799789Sahrens } else { 1800789Sahrens offset += 1; 1801789Sahrens } 1802789Sahrens *next = offset; 1803789Sahrens } 1804869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1805789Sahrens 1806789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1807789Sahrens iovp->iov_base += outcount; 1808789Sahrens iovp->iov_len -= outcount; 1809789Sahrens uio->uio_resid -= outcount; 1810789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1811789Sahrens /* 1812789Sahrens * Reset the pointer. 1813789Sahrens */ 1814789Sahrens offset = uio->uio_loffset; 1815789Sahrens } 1816789Sahrens 1817789Sahrens update: 1818885Sahrens zap_cursor_fini(&zc); 1819789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1820789Sahrens kmem_free(outbuf, bufsize); 1821789Sahrens 1822789Sahrens if (error == ENOENT) 1823789Sahrens error = 0; 1824789Sahrens 1825789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1826789Sahrens 1827789Sahrens uio->uio_loffset = offset; 1828789Sahrens ZFS_EXIT(zfsvfs); 1829789Sahrens return (error); 1830789Sahrens } 1831789Sahrens 1832789Sahrens static int 1833789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1834789Sahrens { 1835789Sahrens znode_t *zp = VTOZ(vp); 1836789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1837789Sahrens 18381773Seschrock /* 18391773Seschrock * Regardless of whether this is required for standards conformance, 18401773Seschrock * this is the logical behavior when fsync() is called on a file with 18411773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 18421773Seschrock * going to be pushed out as part of the zil_commit(). 18431773Seschrock */ 18441773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 18451773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 18461773Seschrock (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 18471773Seschrock 1848789Sahrens ZFS_ENTER(zfsvfs); 18492638Sperrin zil_commit(zfsvfs->z_log, zp->z_last_itx, zp->z_id); 1850789Sahrens ZFS_EXIT(zfsvfs); 1851789Sahrens return (0); 1852789Sahrens } 1853789Sahrens 1854789Sahrens /* 1855789Sahrens * Get the requested file attributes and place them in the provided 1856789Sahrens * vattr structure. 1857789Sahrens * 1858789Sahrens * IN: vp - vnode of file. 1859789Sahrens * vap - va_mask identifies requested attributes. 1860789Sahrens * flags - [UNUSED] 1861789Sahrens * cr - credentials of caller. 1862789Sahrens * 1863789Sahrens * OUT: vap - attribute values. 1864789Sahrens * 1865789Sahrens * RETURN: 0 (always succeeds) 1866789Sahrens */ 1867789Sahrens /* ARGSUSED */ 1868789Sahrens static int 1869789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1870789Sahrens { 1871789Sahrens znode_t *zp = VTOZ(vp); 1872789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1873789Sahrens znode_phys_t *pzp = zp->z_phys; 1874789Sahrens int error; 1875789Sahrens 1876789Sahrens ZFS_ENTER(zfsvfs); 1877789Sahrens 1878789Sahrens /* 1879789Sahrens * Return all attributes. It's cheaper to provide the answer 1880789Sahrens * than to determine whether we were asked the question. 1881789Sahrens */ 1882789Sahrens mutex_enter(&zp->z_lock); 1883789Sahrens 1884789Sahrens vap->va_type = vp->v_type; 1885789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 1886789Sahrens vap->va_uid = zp->z_phys->zp_uid; 1887789Sahrens vap->va_gid = zp->z_phys->zp_gid; 1888789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1889789Sahrens vap->va_nodeid = zp->z_id; 1890789Sahrens vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX); /* nlink_t limit! */ 1891789Sahrens vap->va_size = pzp->zp_size; 18921816Smarks vap->va_rdev = vp->v_rdev; 1893789Sahrens vap->va_seq = zp->z_seq; 1894789Sahrens 1895789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1896789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1897789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1898789Sahrens 1899789Sahrens /* 1900905Smarks * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1901905Smarks * Also, if we are the owner don't bother, since owner should 1902905Smarks * always be allowed to read basic attributes of file. 1903789Sahrens */ 1904905Smarks if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1905905Smarks (zp->z_phys->zp_uid != crgetuid(cr))) { 1906905Smarks if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1907789Sahrens mutex_exit(&zp->z_lock); 1908789Sahrens ZFS_EXIT(zfsvfs); 1909789Sahrens return (error); 1910789Sahrens } 1911789Sahrens } 1912789Sahrens 1913789Sahrens mutex_exit(&zp->z_lock); 1914789Sahrens 1915789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1916789Sahrens 1917789Sahrens if (zp->z_blksz == 0) { 1918789Sahrens /* 1919789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 1920789Sahrens */ 1921789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 1922789Sahrens } 1923789Sahrens 1924789Sahrens ZFS_EXIT(zfsvfs); 1925789Sahrens return (0); 1926789Sahrens } 1927789Sahrens 1928789Sahrens /* 1929789Sahrens * Set the file attributes to the values contained in the 1930789Sahrens * vattr structure. 1931789Sahrens * 1932789Sahrens * IN: vp - vnode of file to be modified. 1933789Sahrens * vap - new attribute values. 1934789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 1935789Sahrens * cr - credentials of caller. 1936789Sahrens * 1937789Sahrens * RETURN: 0 if success 1938789Sahrens * error code if failure 1939789Sahrens * 1940789Sahrens * Timestamps: 1941789Sahrens * vp - ctime updated, mtime updated if size changed. 1942789Sahrens */ 1943789Sahrens /* ARGSUSED */ 1944789Sahrens static int 1945789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1946789Sahrens caller_context_t *ct) 1947789Sahrens { 1948789Sahrens struct znode *zp = VTOZ(vp); 1949789Sahrens znode_phys_t *pzp = zp->z_phys; 1950789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1951789Sahrens zilog_t *zilog = zfsvfs->z_log; 1952789Sahrens dmu_tx_t *tx; 19531878Smaybee vattr_t oldva; 1954789Sahrens uint_t mask = vap->va_mask; 19551878Smaybee uint_t saved_mask; 19562796Smarks int trim_mask = 0; 1957789Sahrens uint64_t new_mode; 19581231Smarks znode_t *attrzp; 1959789Sahrens int need_policy = FALSE; 1960789Sahrens int err; 1961789Sahrens 1962789Sahrens if (mask == 0) 1963789Sahrens return (0); 1964789Sahrens 1965789Sahrens if (mask & AT_NOSET) 1966789Sahrens return (EINVAL); 1967789Sahrens 1968789Sahrens if (mask & AT_SIZE && vp->v_type == VDIR) 1969789Sahrens return (EISDIR); 1970789Sahrens 19711394Smarks if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 19721308Smarks return (EINVAL); 19731308Smarks 1974789Sahrens ZFS_ENTER(zfsvfs); 1975789Sahrens 1976789Sahrens top: 19771231Smarks attrzp = NULL; 1978789Sahrens 1979789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1980789Sahrens ZFS_EXIT(zfsvfs); 1981789Sahrens return (EROFS); 1982789Sahrens } 1983789Sahrens 1984789Sahrens /* 1985789Sahrens * First validate permissions 1986789Sahrens */ 1987789Sahrens 1988789Sahrens if (mask & AT_SIZE) { 1989789Sahrens err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1990789Sahrens if (err) { 1991789Sahrens ZFS_EXIT(zfsvfs); 1992789Sahrens return (err); 1993789Sahrens } 19941878Smaybee /* 19951878Smaybee * XXX - Note, we are not providing any open 19961878Smaybee * mode flags here (like FNDELAY), so we may 19971878Smaybee * block if there are locks present... this 19981878Smaybee * should be addressed in openat(). 19991878Smaybee */ 20001878Smaybee do { 20011878Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 20022113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 20031878Smaybee } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 20041878Smaybee if (err) { 20051878Smaybee ZFS_EXIT(zfsvfs); 20061878Smaybee return (err); 20071878Smaybee } 2008789Sahrens } 2009789Sahrens 2010789Sahrens if (mask & (AT_ATIME|AT_MTIME)) 2011789Sahrens need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 2012789Sahrens 2013789Sahrens if (mask & (AT_UID|AT_GID)) { 2014789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2015789Sahrens int take_owner; 2016789Sahrens int take_group; 2017789Sahrens 2018789Sahrens /* 2019913Smarks * NOTE: even if a new mode is being set, 2020913Smarks * we may clear S_ISUID/S_ISGID bits. 2021913Smarks */ 2022913Smarks 2023913Smarks if (!(mask & AT_MODE)) 2024913Smarks vap->va_mode = pzp->zp_mode; 2025913Smarks 2026913Smarks /* 2027789Sahrens * Take ownership or chgrp to group we are a member of 2028789Sahrens */ 2029789Sahrens 2030789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2031789Sahrens take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 2032789Sahrens 2033789Sahrens /* 2034789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2035789Sahrens * take_group must both be set in order to allow taking 2036789Sahrens * ownership. 2037789Sahrens * 2038789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2039789Sahrens * 2040789Sahrens */ 2041789Sahrens 2042789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2043789Sahrens ((idmask == AT_UID) && take_owner) || 2044789Sahrens ((idmask == AT_GID) && take_group)) { 2045789Sahrens if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 2046789Sahrens /* 2047789Sahrens * Remove setuid/setgid for non-privileged users 2048789Sahrens */ 20491115Smarks secpolicy_setid_clear(vap, cr); 20502796Smarks trim_mask = (mask & (AT_UID|AT_GID)); 2051789Sahrens } else { 2052789Sahrens need_policy = TRUE; 2053789Sahrens } 2054789Sahrens } else { 2055789Sahrens need_policy = TRUE; 2056789Sahrens } 2057789Sahrens } 2058789Sahrens 20592796Smarks mutex_enter(&zp->z_lock); 20602796Smarks oldva.va_mode = pzp->zp_mode; 20612796Smarks oldva.va_uid = zp->z_phys->zp_uid; 20622796Smarks oldva.va_gid = zp->z_phys->zp_gid; 20632796Smarks mutex_exit(&zp->z_lock); 20642796Smarks 20652796Smarks if (mask & AT_MODE) { 20662796Smarks if (zfs_zaccess_v4_perm(zp, ACE_WRITE_ACL, cr) == 0) { 20672796Smarks err = secpolicy_setid_setsticky_clear(vp, vap, 20682796Smarks &oldva, cr); 20692796Smarks if (err) { 20702796Smarks ZFS_EXIT(zfsvfs); 20712796Smarks return (err); 20722796Smarks } 20732796Smarks trim_mask |= AT_MODE; 20742796Smarks } else { 20752796Smarks need_policy = TRUE; 20762796Smarks } 20772796Smarks } 2078789Sahrens 2079789Sahrens if (need_policy) { 20801115Smarks /* 20811115Smarks * If trim_mask is set then take ownership 20822796Smarks * has been granted or write_acl is present and user 20832796Smarks * has the ability to modify mode. In that case remove 20842796Smarks * UID|GID and or MODE from mask so that 20851115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 20861115Smarks */ 20872796Smarks 20882796Smarks if (trim_mask) { 20892796Smarks saved_mask = vap->va_mask; 20902796Smarks vap->va_mask &= ~trim_mask; 20912796Smarks 20922796Smarks } 2093789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2094789Sahrens (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2095789Sahrens if (err) { 2096789Sahrens ZFS_EXIT(zfsvfs); 2097789Sahrens return (err); 2098789Sahrens } 20991115Smarks 21001115Smarks if (trim_mask) 21012796Smarks vap->va_mask |= saved_mask; 2102789Sahrens } 2103789Sahrens 2104789Sahrens /* 2105789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2106789Sahrens * changed va_mask 2107789Sahrens */ 2108789Sahrens mask = vap->va_mask; 2109789Sahrens 2110789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2111789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2112789Sahrens 2113789Sahrens if (mask & AT_MODE) { 21141576Smarks uint64_t pmode = pzp->zp_mode; 21151576Smarks 21161576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2117789Sahrens 2118789Sahrens if (zp->z_phys->zp_acl.z_acl_extern_obj) 2119789Sahrens dmu_tx_hold_write(tx, 2120789Sahrens pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2121789Sahrens else 2122789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2123789Sahrens 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2124789Sahrens } 2125789Sahrens 21261231Smarks if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 21271231Smarks err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 21281231Smarks if (err) { 21291231Smarks dmu_tx_abort(tx); 21301231Smarks ZFS_EXIT(zfsvfs); 21311231Smarks return (err); 21321231Smarks } 21331231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 21341231Smarks } 21351231Smarks 2136789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2137789Sahrens if (err) { 21381231Smarks if (attrzp) 21391231Smarks VN_RELE(ZTOV(attrzp)); 2140789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 21412113Sahrens dmu_tx_wait(tx); 21422113Sahrens dmu_tx_abort(tx); 2143789Sahrens goto top; 2144789Sahrens } 21452113Sahrens dmu_tx_abort(tx); 2146789Sahrens ZFS_EXIT(zfsvfs); 2147789Sahrens return (err); 2148789Sahrens } 2149789Sahrens 2150789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2151789Sahrens 2152789Sahrens /* 2153789Sahrens * Set each attribute requested. 2154789Sahrens * We group settings according to the locks they need to acquire. 2155789Sahrens * 2156789Sahrens * Note: you cannot set ctime directly, although it will be 2157789Sahrens * updated as a side-effect of calling this function. 2158789Sahrens */ 2159789Sahrens 2160789Sahrens mutex_enter(&zp->z_lock); 2161789Sahrens 2162789Sahrens if (mask & AT_MODE) { 2163789Sahrens err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2164789Sahrens ASSERT3U(err, ==, 0); 2165789Sahrens } 2166789Sahrens 21671231Smarks if (attrzp) 21681231Smarks mutex_enter(&attrzp->z_lock); 21691231Smarks 21701231Smarks if (mask & AT_UID) { 2171789Sahrens zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21721231Smarks if (attrzp) { 21731231Smarks attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21741231Smarks } 21751231Smarks } 21761231Smarks 21771231Smarks if (mask & AT_GID) { 2178789Sahrens zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21791231Smarks if (attrzp) 21801231Smarks attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21811231Smarks } 21821231Smarks 21831231Smarks if (attrzp) 21841231Smarks mutex_exit(&attrzp->z_lock); 2185789Sahrens 2186789Sahrens if (mask & AT_ATIME) 2187789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2188789Sahrens 2189789Sahrens if (mask & AT_MTIME) 2190789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2191789Sahrens 21921878Smaybee if (mask & AT_SIZE) 2193789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 21941878Smaybee else if (mask != 0) 2195789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2196789Sahrens 21971878Smaybee if (mask != 0) 21982638Sperrin zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2199789Sahrens 2200789Sahrens mutex_exit(&zp->z_lock); 2201789Sahrens 22021231Smarks if (attrzp) 22031231Smarks VN_RELE(ZTOV(attrzp)); 22041231Smarks 2205789Sahrens dmu_tx_commit(tx); 2206789Sahrens 2207789Sahrens ZFS_EXIT(zfsvfs); 2208789Sahrens return (err); 2209789Sahrens } 2210789Sahrens 2211789Sahrens /* 2212789Sahrens * Search back through the directory tree, using the ".." entries. 2213789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2214789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2215789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2216789Sahrens */ 2217789Sahrens typedef struct zfs_zlock { 2218789Sahrens krwlock_t *zl_rwlock; /* lock we acquired */ 2219789Sahrens znode_t *zl_znode; /* znode we held */ 2220789Sahrens struct zfs_zlock *zl_next; /* next in list */ 2221789Sahrens } zfs_zlock_t; 2222789Sahrens 2223789Sahrens static int 2224789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2225789Sahrens { 2226789Sahrens zfs_zlock_t *zl; 2227789Sahrens znode_t *zp = tdzp; 2228789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2229789Sahrens uint64_t *oidp = &zp->z_id; 2230789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2231789Sahrens krw_t rw = RW_WRITER; 2232789Sahrens 2233789Sahrens /* 2234789Sahrens * First pass write-locks szp and compares to zp->z_id. 2235789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2236789Sahrens */ 2237789Sahrens do { 2238789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2239789Sahrens zl->zl_rwlock = rwlp; 2240789Sahrens zl->zl_znode = NULL; 2241789Sahrens zl->zl_next = *zlpp; 2242789Sahrens *zlpp = zl; 2243789Sahrens 2244789Sahrens rw_enter(rwlp, rw); 2245789Sahrens 2246789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2247789Sahrens return (EINVAL); 2248789Sahrens 2249789Sahrens if (*oidp == rootid) /* We've hit the top */ 2250789Sahrens return (0); 2251789Sahrens 2252789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2253789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2254789Sahrens if (error) 2255789Sahrens return (error); 2256789Sahrens zl->zl_znode = zp; 2257789Sahrens } 2258789Sahrens oidp = &zp->z_phys->zp_parent; 2259789Sahrens rwlp = &zp->z_parent_lock; 2260789Sahrens rw = RW_READER; 2261789Sahrens 2262789Sahrens } while (zp->z_id != sdzp->z_id); 2263789Sahrens 2264789Sahrens return (0); 2265789Sahrens } 2266789Sahrens 2267789Sahrens /* 2268789Sahrens * Drop locks and release vnodes that were held by zfs_rename_lock(). 2269789Sahrens */ 2270789Sahrens static void 2271789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp) 2272789Sahrens { 2273789Sahrens zfs_zlock_t *zl; 2274789Sahrens 2275789Sahrens while ((zl = *zlpp) != NULL) { 2276789Sahrens if (zl->zl_znode != NULL) 2277789Sahrens VN_RELE(ZTOV(zl->zl_znode)); 2278789Sahrens rw_exit(zl->zl_rwlock); 2279789Sahrens *zlpp = zl->zl_next; 2280789Sahrens kmem_free(zl, sizeof (*zl)); 2281789Sahrens } 2282789Sahrens } 2283789Sahrens 2284789Sahrens /* 2285789Sahrens * Move an entry from the provided source directory to the target 2286789Sahrens * directory. Change the entry name as indicated. 2287789Sahrens * 2288789Sahrens * IN: sdvp - Source directory containing the "old entry". 2289789Sahrens * snm - Old entry name. 2290789Sahrens * tdvp - Target directory to contain the "new entry". 2291789Sahrens * tnm - New entry name. 2292789Sahrens * cr - credentials of caller. 2293789Sahrens * 2294789Sahrens * RETURN: 0 if success 2295789Sahrens * error code if failure 2296789Sahrens * 2297789Sahrens * Timestamps: 2298789Sahrens * sdvp,tdvp - ctime|mtime updated 2299789Sahrens */ 2300789Sahrens static int 2301789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2302789Sahrens { 2303789Sahrens znode_t *tdzp, *szp, *tzp; 2304789Sahrens znode_t *sdzp = VTOZ(sdvp); 2305789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2306789Sahrens zilog_t *zilog = zfsvfs->z_log; 2307789Sahrens vnode_t *realvp; 2308789Sahrens zfs_dirlock_t *sdl, *tdl; 2309789Sahrens dmu_tx_t *tx; 2310789Sahrens zfs_zlock_t *zl; 2311789Sahrens int cmp, serr, terr, error; 2312789Sahrens 2313789Sahrens ZFS_ENTER(zfsvfs); 2314789Sahrens 2315789Sahrens /* 2316789Sahrens * Make sure we have the real vp for the target directory. 2317789Sahrens */ 2318789Sahrens if (VOP_REALVP(tdvp, &realvp) == 0) 2319789Sahrens tdvp = realvp; 2320789Sahrens 2321789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2322789Sahrens ZFS_EXIT(zfsvfs); 2323789Sahrens return (EXDEV); 2324789Sahrens } 2325789Sahrens 2326789Sahrens tdzp = VTOZ(tdvp); 2327789Sahrens top: 2328789Sahrens szp = NULL; 2329789Sahrens tzp = NULL; 2330789Sahrens zl = NULL; 2331789Sahrens 2332789Sahrens /* 2333789Sahrens * This is to prevent the creation of links into attribute space 2334789Sahrens * by renaming a linked file into/outof an attribute directory. 2335789Sahrens * See the comment in zfs_link() for why this is considered bad. 2336789Sahrens */ 2337789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2338789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2339789Sahrens ZFS_EXIT(zfsvfs); 2340789Sahrens return (EINVAL); 2341789Sahrens } 2342789Sahrens 2343789Sahrens /* 2344789Sahrens * Lock source and target directory entries. To prevent deadlock, 2345789Sahrens * a lock ordering must be defined. We lock the directory with 2346789Sahrens * the smallest object id first, or if it's a tie, the one with 2347789Sahrens * the lexically first name. 2348789Sahrens */ 2349789Sahrens if (sdzp->z_id < tdzp->z_id) { 2350789Sahrens cmp = -1; 2351789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2352789Sahrens cmp = 1; 2353789Sahrens } else { 2354789Sahrens cmp = strcmp(snm, tnm); 2355789Sahrens if (cmp == 0) { 2356789Sahrens /* 2357789Sahrens * POSIX: "If the old argument and the new argument 2358789Sahrens * both refer to links to the same existing file, 2359789Sahrens * the rename() function shall return successfully 2360789Sahrens * and perform no other action." 2361789Sahrens */ 2362789Sahrens ZFS_EXIT(zfsvfs); 2363789Sahrens return (0); 2364789Sahrens } 2365789Sahrens } 2366789Sahrens if (cmp < 0) { 2367789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2368789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2369789Sahrens } else { 2370789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2371789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2372789Sahrens } 2373789Sahrens 2374789Sahrens if (serr) { 2375789Sahrens /* 2376789Sahrens * Source entry invalid or not there. 2377789Sahrens */ 2378789Sahrens if (!terr) { 2379789Sahrens zfs_dirent_unlock(tdl); 2380789Sahrens if (tzp) 2381789Sahrens VN_RELE(ZTOV(tzp)); 2382789Sahrens } 2383789Sahrens if (strcmp(snm, "..") == 0) 2384789Sahrens serr = EINVAL; 2385789Sahrens ZFS_EXIT(zfsvfs); 2386789Sahrens return (serr); 2387789Sahrens } 2388789Sahrens if (terr) { 2389789Sahrens zfs_dirent_unlock(sdl); 2390789Sahrens VN_RELE(ZTOV(szp)); 2391789Sahrens if (strcmp(tnm, "..") == 0) 2392789Sahrens terr = EINVAL; 2393789Sahrens ZFS_EXIT(zfsvfs); 2394789Sahrens return (terr); 2395789Sahrens } 2396789Sahrens 2397789Sahrens /* 2398789Sahrens * Must have write access at the source to remove the old entry 2399789Sahrens * and write access at the target to create the new entry. 2400789Sahrens * Note that if target and source are the same, this can be 2401789Sahrens * done in a single check. 2402789Sahrens */ 2403789Sahrens 2404789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2405789Sahrens goto out; 2406789Sahrens 2407789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2408789Sahrens /* 2409789Sahrens * Check to make sure rename is valid. 2410789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2411789Sahrens */ 2412789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2413789Sahrens goto out; 2414789Sahrens } 2415789Sahrens 2416789Sahrens /* 2417789Sahrens * Does target exist? 2418789Sahrens */ 2419789Sahrens if (tzp) { 2420789Sahrens /* 2421789Sahrens * Source and target must be the same type. 2422789Sahrens */ 2423789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2424789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 2425789Sahrens error = ENOTDIR; 2426789Sahrens goto out; 2427789Sahrens } 2428789Sahrens } else { 2429789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 2430789Sahrens error = EISDIR; 2431789Sahrens goto out; 2432789Sahrens } 2433789Sahrens } 2434789Sahrens /* 2435789Sahrens * POSIX dictates that when the source and target 2436789Sahrens * entries refer to the same file object, rename 2437789Sahrens * must do nothing and exit without error. 2438789Sahrens */ 2439789Sahrens if (szp->z_id == tzp->z_id) { 2440789Sahrens error = 0; 2441789Sahrens goto out; 2442789Sahrens } 2443789Sahrens } 2444789Sahrens 2445789Sahrens vnevent_rename_src(ZTOV(szp)); 2446789Sahrens if (tzp) 2447789Sahrens vnevent_rename_dest(ZTOV(tzp)); 2448789Sahrens 2449789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2450789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2451789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 24521544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 24531544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 24541544Seschrock if (sdzp != tdzp) 2455789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 24561544Seschrock if (tzp) 24571544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 24581544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 2459789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2460789Sahrens if (error) { 2461789Sahrens if (zl != NULL) 2462789Sahrens zfs_rename_unlock(&zl); 2463789Sahrens zfs_dirent_unlock(sdl); 2464789Sahrens zfs_dirent_unlock(tdl); 2465789Sahrens VN_RELE(ZTOV(szp)); 2466789Sahrens if (tzp) 2467789Sahrens VN_RELE(ZTOV(tzp)); 2468789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 24692113Sahrens dmu_tx_wait(tx); 24702113Sahrens dmu_tx_abort(tx); 2471789Sahrens goto top; 2472789Sahrens } 24732113Sahrens dmu_tx_abort(tx); 2474789Sahrens ZFS_EXIT(zfsvfs); 2475789Sahrens return (error); 2476789Sahrens } 2477789Sahrens 2478789Sahrens if (tzp) /* Attempt to remove the existing target */ 2479789Sahrens error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2480789Sahrens 2481789Sahrens if (error == 0) { 2482789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2483789Sahrens if (error == 0) { 2484789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2485789Sahrens ASSERT(error == 0); 24862638Sperrin zfs_log_rename(zilog, tx, TX_RENAME, sdzp, 24872638Sperrin sdl->dl_name, tdzp, tdl->dl_name, szp); 2488789Sahrens } 2489789Sahrens } 2490789Sahrens 2491789Sahrens dmu_tx_commit(tx); 2492789Sahrens out: 2493789Sahrens if (zl != NULL) 2494789Sahrens zfs_rename_unlock(&zl); 2495789Sahrens 2496789Sahrens zfs_dirent_unlock(sdl); 2497789Sahrens zfs_dirent_unlock(tdl); 2498789Sahrens 2499789Sahrens VN_RELE(ZTOV(szp)); 2500789Sahrens if (tzp) 2501789Sahrens VN_RELE(ZTOV(tzp)); 2502789Sahrens 2503789Sahrens ZFS_EXIT(zfsvfs); 2504789Sahrens return (error); 2505789Sahrens } 2506789Sahrens 2507789Sahrens /* 2508789Sahrens * Insert the indicated symbolic reference entry into the directory. 2509789Sahrens * 2510789Sahrens * IN: dvp - Directory to contain new symbolic link. 2511789Sahrens * link - Name for new symlink entry. 2512789Sahrens * vap - Attributes of new entry. 2513789Sahrens * target - Target path of new symlink. 2514789Sahrens * cr - credentials of caller. 2515789Sahrens * 2516789Sahrens * RETURN: 0 if success 2517789Sahrens * error code if failure 2518789Sahrens * 2519789Sahrens * Timestamps: 2520789Sahrens * dvp - ctime|mtime updated 2521789Sahrens */ 2522789Sahrens static int 2523789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2524789Sahrens { 2525789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 2526789Sahrens zfs_dirlock_t *dl; 2527789Sahrens dmu_tx_t *tx; 2528789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2529789Sahrens zilog_t *zilog = zfsvfs->z_log; 2530789Sahrens uint64_t zoid; 2531789Sahrens int len = strlen(link); 2532789Sahrens int error; 2533789Sahrens 2534789Sahrens ASSERT(vap->va_type == VLNK); 2535789Sahrens 2536789Sahrens ZFS_ENTER(zfsvfs); 2537789Sahrens top: 2538789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2539789Sahrens ZFS_EXIT(zfsvfs); 2540789Sahrens return (error); 2541789Sahrens } 2542789Sahrens 2543789Sahrens if (len > MAXPATHLEN) { 2544789Sahrens ZFS_EXIT(zfsvfs); 2545789Sahrens return (ENAMETOOLONG); 2546789Sahrens } 2547789Sahrens 2548789Sahrens /* 2549789Sahrens * Attempt to lock directory; fail if entry already exists. 2550789Sahrens */ 2551789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2552789Sahrens ZFS_EXIT(zfsvfs); 2553789Sahrens return (error); 2554789Sahrens } 2555789Sahrens 2556789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2557789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2558789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 25591544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2560789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2561789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2562789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2563789Sahrens if (error) { 2564789Sahrens zfs_dirent_unlock(dl); 2565789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 25662113Sahrens dmu_tx_wait(tx); 25672113Sahrens dmu_tx_abort(tx); 2568789Sahrens goto top; 2569789Sahrens } 25702113Sahrens dmu_tx_abort(tx); 2571789Sahrens ZFS_EXIT(zfsvfs); 2572789Sahrens return (error); 2573789Sahrens } 2574789Sahrens 2575789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 2576789Sahrens 2577789Sahrens /* 2578789Sahrens * Create a new object for the symlink. 2579789Sahrens * Put the link content into bonus buffer if it will fit; 2580789Sahrens * otherwise, store it just like any other file data. 2581789Sahrens */ 2582789Sahrens zoid = 0; 2583789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2584789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2585789Sahrens if (len != 0) 2586789Sahrens bcopy(link, zp->z_phys + 1, len); 2587789Sahrens } else { 2588789Sahrens dmu_buf_t *dbp; 25891669Sperrin 2590789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2591789Sahrens 25921669Sperrin /* 25931669Sperrin * Nothing can access the znode yet so no locking needed 25941669Sperrin * for growing the znode's blocksize. 25951669Sperrin */ 25961669Sperrin zfs_grow_blocksize(zp, len, tx); 2597789Sahrens 25981544Seschrock VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2599789Sahrens dmu_buf_will_dirty(dbp, tx); 2600789Sahrens 2601789Sahrens ASSERT3U(len, <=, dbp->db_size); 2602789Sahrens bcopy(link, dbp->db_data, len); 26031544Seschrock dmu_buf_rele(dbp, FTAG); 2604789Sahrens } 2605789Sahrens zp->z_phys->zp_size = len; 2606789Sahrens 2607789Sahrens /* 2608789Sahrens * Insert the new object into the directory. 2609789Sahrens */ 2610789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 2611789Sahrens out: 2612789Sahrens if (error == 0) 26132638Sperrin zfs_log_symlink(zilog, tx, TX_SYMLINK, dzp, zp, name, link); 2614789Sahrens 2615789Sahrens dmu_tx_commit(tx); 2616789Sahrens 2617789Sahrens zfs_dirent_unlock(dl); 2618789Sahrens 2619789Sahrens VN_RELE(ZTOV(zp)); 2620789Sahrens 2621789Sahrens ZFS_EXIT(zfsvfs); 2622789Sahrens return (error); 2623789Sahrens } 2624789Sahrens 2625789Sahrens /* 2626789Sahrens * Return, in the buffer contained in the provided uio structure, 2627789Sahrens * the symbolic path referred to by vp. 2628789Sahrens * 2629789Sahrens * IN: vp - vnode of symbolic link. 2630789Sahrens * uoip - structure to contain the link path. 2631789Sahrens * cr - credentials of caller. 2632789Sahrens * 2633789Sahrens * OUT: uio - structure to contain the link path. 2634789Sahrens * 2635789Sahrens * RETURN: 0 if success 2636789Sahrens * error code if failure 2637789Sahrens * 2638789Sahrens * Timestamps: 2639789Sahrens * vp - atime updated 2640789Sahrens */ 2641789Sahrens /* ARGSUSED */ 2642789Sahrens static int 2643789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2644789Sahrens { 2645789Sahrens znode_t *zp = VTOZ(vp); 2646789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2647789Sahrens size_t bufsz; 2648789Sahrens int error; 2649789Sahrens 2650789Sahrens ZFS_ENTER(zfsvfs); 2651789Sahrens 2652789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 2653789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2654789Sahrens error = uiomove(zp->z_phys + 1, 2655789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2656789Sahrens } else { 26571544Seschrock dmu_buf_t *dbp; 26581544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 26591544Seschrock if (error) { 2660789Sahrens ZFS_EXIT(zfsvfs); 2661789Sahrens return (error); 2662789Sahrens } 2663789Sahrens error = uiomove(dbp->db_data, 2664789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 26651544Seschrock dmu_buf_rele(dbp, FTAG); 2666789Sahrens } 2667789Sahrens 2668789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2669789Sahrens ZFS_EXIT(zfsvfs); 2670789Sahrens return (error); 2671789Sahrens } 2672789Sahrens 2673789Sahrens /* 2674789Sahrens * Insert a new entry into directory tdvp referencing svp. 2675789Sahrens * 2676789Sahrens * IN: tdvp - Directory to contain new entry. 2677789Sahrens * svp - vnode of new entry. 2678789Sahrens * name - name of new entry. 2679789Sahrens * cr - credentials of caller. 2680789Sahrens * 2681789Sahrens * RETURN: 0 if success 2682789Sahrens * error code if failure 2683789Sahrens * 2684789Sahrens * Timestamps: 2685789Sahrens * tdvp - ctime|mtime updated 2686789Sahrens * svp - ctime updated 2687789Sahrens */ 2688789Sahrens /* ARGSUSED */ 2689789Sahrens static int 2690789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2691789Sahrens { 2692789Sahrens znode_t *dzp = VTOZ(tdvp); 2693789Sahrens znode_t *tzp, *szp; 2694789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2695789Sahrens zilog_t *zilog = zfsvfs->z_log; 2696789Sahrens zfs_dirlock_t *dl; 2697789Sahrens dmu_tx_t *tx; 2698789Sahrens vnode_t *realvp; 2699789Sahrens int error; 2700789Sahrens 2701789Sahrens ASSERT(tdvp->v_type == VDIR); 2702789Sahrens 2703789Sahrens ZFS_ENTER(zfsvfs); 2704789Sahrens 2705789Sahrens if (VOP_REALVP(svp, &realvp) == 0) 2706789Sahrens svp = realvp; 2707789Sahrens 2708789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 2709789Sahrens ZFS_EXIT(zfsvfs); 2710789Sahrens return (EXDEV); 2711789Sahrens } 2712789Sahrens 2713789Sahrens szp = VTOZ(svp); 2714789Sahrens top: 2715789Sahrens /* 2716789Sahrens * We do not support links between attributes and non-attributes 2717789Sahrens * because of the potential security risk of creating links 2718789Sahrens * into "normal" file space in order to circumvent restrictions 2719789Sahrens * imposed in attribute space. 2720789Sahrens */ 2721789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2722789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2723789Sahrens ZFS_EXIT(zfsvfs); 2724789Sahrens return (EINVAL); 2725789Sahrens } 2726789Sahrens 2727789Sahrens /* 2728789Sahrens * POSIX dictates that we return EPERM here. 2729789Sahrens * Better choices include ENOTSUP or EISDIR. 2730789Sahrens */ 2731789Sahrens if (svp->v_type == VDIR) { 2732789Sahrens ZFS_EXIT(zfsvfs); 2733789Sahrens return (EPERM); 2734789Sahrens } 2735789Sahrens 2736789Sahrens if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2737789Sahrens secpolicy_basic_link(cr) != 0) { 2738789Sahrens ZFS_EXIT(zfsvfs); 2739789Sahrens return (EPERM); 2740789Sahrens } 2741789Sahrens 2742789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2743789Sahrens ZFS_EXIT(zfsvfs); 2744789Sahrens return (error); 2745789Sahrens } 2746789Sahrens 2747789Sahrens /* 2748789Sahrens * Attempt to lock directory; fail if entry already exists. 2749789Sahrens */ 2750789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2751789Sahrens ZFS_EXIT(zfsvfs); 2752789Sahrens return (error); 2753789Sahrens } 2754789Sahrens 2755789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2756789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 27571544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2758789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2759789Sahrens if (error) { 2760789Sahrens zfs_dirent_unlock(dl); 2761789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 27622113Sahrens dmu_tx_wait(tx); 27632113Sahrens dmu_tx_abort(tx); 2764789Sahrens goto top; 2765789Sahrens } 27662113Sahrens dmu_tx_abort(tx); 2767789Sahrens ZFS_EXIT(zfsvfs); 2768789Sahrens return (error); 2769789Sahrens } 2770789Sahrens 2771789Sahrens error = zfs_link_create(dl, szp, tx, 0); 2772789Sahrens 2773789Sahrens if (error == 0) 27742638Sperrin zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2775789Sahrens 2776789Sahrens dmu_tx_commit(tx); 2777789Sahrens 2778789Sahrens zfs_dirent_unlock(dl); 2779789Sahrens 2780789Sahrens ZFS_EXIT(zfsvfs); 2781789Sahrens return (error); 2782789Sahrens } 2783789Sahrens 2784789Sahrens /* 2785789Sahrens * zfs_null_putapage() is used when the file system has been force 2786789Sahrens * unmounted. It just drops the pages. 2787789Sahrens */ 2788789Sahrens /* ARGSUSED */ 2789789Sahrens static int 2790789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2791789Sahrens size_t *lenp, int flags, cred_t *cr) 2792789Sahrens { 2793789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2794789Sahrens return (0); 2795789Sahrens } 2796789Sahrens 27972688Smaybee /* 27982688Smaybee * Push a page out to disk, klustering if possible. 27992688Smaybee * 28002688Smaybee * IN: vp - file to push page to. 28012688Smaybee * pp - page to push. 28022688Smaybee * flags - additional flags. 28032688Smaybee * cr - credentials of caller. 28042688Smaybee * 28052688Smaybee * OUT: offp - start of range pushed. 28062688Smaybee * lenp - len of range pushed. 28072688Smaybee * 28082688Smaybee * RETURN: 0 if success 28092688Smaybee * error code if failure 28102688Smaybee * 28112688Smaybee * NOTE: callers must have locked the page to be pushed. On 28122688Smaybee * exit, the page (and all other pages in the kluster) must be 28132688Smaybee * unlocked. 28142688Smaybee */ 2815789Sahrens /* ARGSUSED */ 2816789Sahrens static int 2817789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2818789Sahrens size_t *lenp, int flags, cred_t *cr) 2819789Sahrens { 2820789Sahrens znode_t *zp = VTOZ(vp); 2821789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2822789Sahrens zilog_t *zilog = zfsvfs->z_log; 2823789Sahrens dmu_tx_t *tx; 28241669Sperrin rl_t *rl; 28252688Smaybee u_offset_t off, koff; 28262688Smaybee size_t len, klen; 2827789Sahrens int err; 2828789Sahrens 28292688Smaybee off = pp->p_offset; 28302688Smaybee len = PAGESIZE; 28312688Smaybee /* 28322688Smaybee * If our blocksize is bigger than the page size, try to kluster 28332688Smaybee * muiltiple pages so that we write a full block (thus avoiding 28342688Smaybee * a read-modify-write). 28352688Smaybee */ 28362688Smaybee if (zp->z_blksz > PAGESIZE) { 28372688Smaybee uint64_t filesz = zp->z_phys->zp_size; 28382688Smaybee 28392688Smaybee if (!ISP2(zp->z_blksz)) { 28402688Smaybee /* Only one block in the file. */ 28412688Smaybee klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 28422688Smaybee koff = 0; 28432688Smaybee } else { 28442688Smaybee klen = zp->z_blksz; 28452688Smaybee koff = P2ALIGN(off, (u_offset_t)klen); 28462688Smaybee } 28472688Smaybee ASSERT(koff <= filesz); 28482688Smaybee if (koff + klen > filesz) 28492688Smaybee klen = P2ROUNDUP(filesz - koff, (uint64_t)PAGESIZE); 28502688Smaybee pp = pvn_write_kluster(vp, pp, &off, &len, koff, klen, flags); 28512688Smaybee } 28522688Smaybee ASSERT3U(btop(len), ==, btopr(len)); 2853789Sahrens top: 28542688Smaybee rl = zfs_range_lock(zp, off, len, RL_WRITER); 28551819Smaybee /* 28561819Smaybee * Can't push pages past end-of-file. 28571819Smaybee */ 28581819Smaybee if (off >= zp->z_phys->zp_size) { 28592688Smaybee /* discard all pages */ 28602688Smaybee flags |= B_INVAL; 28612688Smaybee err = 0; 28622688Smaybee goto out; 28632688Smaybee } else if (off + len > zp->z_phys->zp_size) { 28642688Smaybee int npages = btopr(zp->z_phys->zp_size - off); 28652688Smaybee page_t *trunc; 28662688Smaybee 28672688Smaybee page_list_break(&pp, &trunc, npages); 28682688Smaybee /* discard pages past end of file */ 28692688Smaybee if (trunc) 28702688Smaybee pvn_write_done(trunc, B_INVAL | flags); 28712688Smaybee len = zp->z_phys->zp_size - off; 28721819Smaybee } 2873789Sahrens 2874789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2875789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 2876789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2877789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2878789Sahrens if (err != 0) { 2879789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 28802688Smaybee zfs_range_unlock(rl); 28812113Sahrens dmu_tx_wait(tx); 28822113Sahrens dmu_tx_abort(tx); 28832688Smaybee err = 0; 2884789Sahrens goto top; 2885789Sahrens } 28862113Sahrens dmu_tx_abort(tx); 2887789Sahrens goto out; 2888789Sahrens } 2889789Sahrens 28902688Smaybee if (zp->z_blksz <= PAGESIZE) { 28912688Smaybee caddr_t va = ppmapin(pp, PROT_READ, (caddr_t)-1); 28922688Smaybee ASSERT3U(len, <=, PAGESIZE); 28932688Smaybee dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 28942688Smaybee ppmapout(va); 28952688Smaybee } else { 28962688Smaybee err = dmu_write_pages(zfsvfs->z_os, zp->z_id, off, len, pp, tx); 28972688Smaybee } 28982688Smaybee 28992688Smaybee if (err == 0) { 29002688Smaybee zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 29012688Smaybee (void) zfs_log_write( 29022688Smaybee zilog, tx, TX_WRITE, zp, off, len, 0, NULL); 29032688Smaybee dmu_tx_commit(tx); 29042688Smaybee } 29052688Smaybee 29062688Smaybee out: 29072237Smaybee zfs_range_unlock(rl); 29082688Smaybee pvn_write_done(pp, (err ? B_ERROR : 0) | B_WRITE | flags); 2909789Sahrens if (offp) 2910789Sahrens *offp = off; 2911789Sahrens if (lenp) 2912789Sahrens *lenp = len; 2913789Sahrens 2914789Sahrens return (err); 2915789Sahrens } 2916789Sahrens 2917789Sahrens /* 2918789Sahrens * Copy the portion of the file indicated from pages into the file. 2919789Sahrens * The pages are stored in a page list attached to the files vnode. 2920789Sahrens * 2921789Sahrens * IN: vp - vnode of file to push page data to. 2922789Sahrens * off - position in file to put data. 2923789Sahrens * len - amount of data to write. 2924789Sahrens * flags - flags to control the operation. 2925789Sahrens * cr - credentials of caller. 2926789Sahrens * 2927789Sahrens * RETURN: 0 if success 2928789Sahrens * error code if failure 2929789Sahrens * 2930789Sahrens * Timestamps: 2931789Sahrens * vp - ctime|mtime updated 2932789Sahrens */ 2933789Sahrens static int 2934789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2935789Sahrens { 2936789Sahrens znode_t *zp = VTOZ(vp); 2937789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2938789Sahrens page_t *pp; 2939789Sahrens size_t io_len; 2940789Sahrens u_offset_t io_off; 29411669Sperrin uint64_t filesz; 2942789Sahrens int error = 0; 2943789Sahrens 2944789Sahrens ZFS_ENTER(zfsvfs); 2945789Sahrens 2946789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 2947789Sahrens 2948789Sahrens if (len == 0) { 2949789Sahrens /* 2950789Sahrens * Search the entire vp list for pages >= off. 2951789Sahrens */ 2952789Sahrens error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2953789Sahrens flags, cr); 29541472Sperrin goto out; 2955789Sahrens } 2956789Sahrens 29571669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 29581669Sperrin if (off > filesz) { 2959789Sahrens /* past end of file */ 2960789Sahrens ZFS_EXIT(zfsvfs); 2961789Sahrens return (0); 2962789Sahrens } 2963789Sahrens 29641669Sperrin len = MIN(len, filesz - off); 2965789Sahrens 29661472Sperrin for (io_off = off; io_off < off + len; io_off += io_len) { 2967789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 29681669Sperrin pp = page_lookup(vp, io_off, 2969789Sahrens (flags & (B_INVAL | B_FREE)) ? 2970789Sahrens SE_EXCL : SE_SHARED); 2971789Sahrens } else { 2972789Sahrens pp = page_lookup_nowait(vp, io_off, 2973789Sahrens (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2974789Sahrens } 2975789Sahrens 2976789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 2977789Sahrens int err; 2978789Sahrens 2979789Sahrens /* 2980789Sahrens * Found a dirty page to push 2981789Sahrens */ 29821669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 29831669Sperrin if (err) 2984789Sahrens error = err; 2985789Sahrens } else { 2986789Sahrens io_len = PAGESIZE; 2987789Sahrens } 2988789Sahrens } 29891472Sperrin out: 29902638Sperrin if ((flags & B_ASYNC) == 0) 29912638Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, zp->z_id); 2992789Sahrens ZFS_EXIT(zfsvfs); 2993789Sahrens return (error); 2994789Sahrens } 2995789Sahrens 2996789Sahrens void 2997789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr) 2998789Sahrens { 2999789Sahrens znode_t *zp = VTOZ(vp); 3000789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3001789Sahrens int error; 3002789Sahrens 3003789Sahrens rw_enter(&zfsvfs->z_um_lock, RW_READER); 3004789Sahrens if (zfsvfs->z_unmounted2) { 3005789Sahrens ASSERT(zp->z_dbuf_held == 0); 3006789Sahrens 3007789Sahrens if (vn_has_cached_data(vp)) { 3008789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 3009789Sahrens B_INVAL, cr); 3010789Sahrens } 3011789Sahrens 30121544Seschrock mutex_enter(&zp->z_lock); 3013789Sahrens vp->v_count = 0; /* count arrives as 1 */ 30141544Seschrock if (zp->z_dbuf == NULL) { 30151544Seschrock mutex_exit(&zp->z_lock); 30161544Seschrock zfs_znode_free(zp); 30171544Seschrock } else { 30181544Seschrock mutex_exit(&zp->z_lock); 30191544Seschrock } 3020789Sahrens rw_exit(&zfsvfs->z_um_lock); 3021789Sahrens VFS_RELE(zfsvfs->z_vfs); 3022789Sahrens return; 3023789Sahrens } 3024789Sahrens 3025789Sahrens /* 3026789Sahrens * Attempt to push any data in the page cache. If this fails 3027789Sahrens * we will get kicked out later in zfs_zinactive(). 3028789Sahrens */ 30291298Sperrin if (vn_has_cached_data(vp)) { 30301298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 30311298Sperrin cr); 30321298Sperrin } 3033789Sahrens 3034789Sahrens if (zp->z_atime_dirty && zp->z_reap == 0) { 3035789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 3036789Sahrens 3037789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 3038789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 3039789Sahrens if (error) { 3040789Sahrens dmu_tx_abort(tx); 3041789Sahrens } else { 3042789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 3043789Sahrens mutex_enter(&zp->z_lock); 3044789Sahrens zp->z_atime_dirty = 0; 3045789Sahrens mutex_exit(&zp->z_lock); 3046789Sahrens dmu_tx_commit(tx); 3047789Sahrens } 3048789Sahrens } 3049789Sahrens 3050789Sahrens zfs_zinactive(zp); 3051789Sahrens rw_exit(&zfsvfs->z_um_lock); 3052789Sahrens } 3053789Sahrens 3054789Sahrens /* 3055789Sahrens * Bounds-check the seek operation. 3056789Sahrens * 3057789Sahrens * IN: vp - vnode seeking within 3058789Sahrens * ooff - old file offset 3059789Sahrens * noffp - pointer to new file offset 3060789Sahrens * 3061789Sahrens * RETURN: 0 if success 3062789Sahrens * EINVAL if new offset invalid 3063789Sahrens */ 3064789Sahrens /* ARGSUSED */ 3065789Sahrens static int 3066789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 3067789Sahrens { 3068789Sahrens if (vp->v_type == VDIR) 3069789Sahrens return (0); 3070789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3071789Sahrens } 3072789Sahrens 3073789Sahrens /* 3074789Sahrens * Pre-filter the generic locking function to trap attempts to place 3075789Sahrens * a mandatory lock on a memory mapped file. 3076789Sahrens */ 3077789Sahrens static int 3078789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 3079789Sahrens flk_callback_t *flk_cbp, cred_t *cr) 3080789Sahrens { 3081789Sahrens znode_t *zp = VTOZ(vp); 3082789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3083789Sahrens int error; 3084789Sahrens 3085789Sahrens ZFS_ENTER(zfsvfs); 3086789Sahrens 3087789Sahrens /* 30881544Seschrock * We are following the UFS semantics with respect to mapcnt 30891544Seschrock * here: If we see that the file is mapped already, then we will 30901544Seschrock * return an error, but we don't worry about races between this 30911544Seschrock * function and zfs_map(). 3092789Sahrens */ 30931544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3094789Sahrens ZFS_EXIT(zfsvfs); 3095789Sahrens return (EAGAIN); 3096789Sahrens } 3097789Sahrens error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 3098789Sahrens ZFS_EXIT(zfsvfs); 3099789Sahrens return (error); 3100789Sahrens } 3101789Sahrens 3102789Sahrens /* 3103789Sahrens * If we can't find a page in the cache, we will create a new page 3104789Sahrens * and fill it with file data. For efficiency, we may try to fill 31051669Sperrin * multiple pages at once (klustering). 3106789Sahrens */ 3107789Sahrens static int 3108789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3109789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3110789Sahrens { 3111789Sahrens znode_t *zp = VTOZ(vp); 3112789Sahrens page_t *pp, *cur_pp; 3113789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3114789Sahrens caddr_t va; 3115789Sahrens u_offset_t io_off, total; 3116789Sahrens uint64_t oid = zp->z_id; 3117789Sahrens size_t io_len; 31181669Sperrin uint64_t filesz; 3119789Sahrens int err; 3120789Sahrens 3121789Sahrens /* 3122789Sahrens * If we are only asking for a single page don't bother klustering. 3123789Sahrens */ 31241669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 31252688Smaybee if (off >= filesz) 31262688Smaybee return (EFAULT); 31272688Smaybee if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE) { 3128789Sahrens io_off = off; 3129789Sahrens io_len = PAGESIZE; 3130789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3131789Sahrens } else { 3132789Sahrens /* 3133789Sahrens * Try to fill a kluster of pages (a blocks worth). 3134789Sahrens */ 3135789Sahrens size_t klen; 3136789Sahrens u_offset_t koff; 3137789Sahrens 3138789Sahrens if (!ISP2(zp->z_blksz)) { 3139789Sahrens /* Only one block in the file. */ 3140789Sahrens klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3141789Sahrens koff = 0; 3142789Sahrens } else { 3143*3131Sgw25295 /* 3144*3131Sgw25295 * It would be ideal to align our offset to the 3145*3131Sgw25295 * blocksize but doing so has resulted in some 3146*3131Sgw25295 * strange application crashes. For now, we 3147*3131Sgw25295 * leave the offset as is and only adjust the 3148*3131Sgw25295 * length if we are off the end of the file. 3149*3131Sgw25295 */ 3150*3131Sgw25295 koff = off; 3151789Sahrens klen = plsz; 3152789Sahrens } 31531819Smaybee ASSERT(koff <= filesz); 31541819Smaybee if (koff + klen > filesz) 31551819Smaybee klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 31562688Smaybee ASSERT3U(off, >=, koff); 31572688Smaybee ASSERT3U(off, <, koff + klen); 3158789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 3159789Sahrens &io_len, koff, klen, 0); 3160789Sahrens } 3161789Sahrens if (pp == NULL) { 3162789Sahrens /* 3163789Sahrens * Some other thread entered the page before us. 3164789Sahrens * Return to zfs_getpage to retry the lookup. 3165789Sahrens */ 3166789Sahrens *pl = NULL; 3167789Sahrens return (0); 3168789Sahrens } 3169789Sahrens 3170789Sahrens /* 3171789Sahrens * Fill the pages in the kluster. 3172789Sahrens */ 3173789Sahrens cur_pp = pp; 3174789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 31752688Smaybee ASSERT3U(io_off, ==, cur_pp->p_offset); 3176789Sahrens va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 31771544Seschrock err = dmu_read(os, oid, io_off, PAGESIZE, va); 3178789Sahrens ppmapout(va); 3179789Sahrens if (err) { 3180789Sahrens /* On error, toss the entire kluster */ 3181789Sahrens pvn_read_done(pp, B_ERROR); 3182789Sahrens return (err); 3183789Sahrens } 3184789Sahrens cur_pp = cur_pp->p_next; 3185789Sahrens } 3186789Sahrens out: 3187789Sahrens /* 3188789Sahrens * Fill in the page list array from the kluster. If 3189789Sahrens * there are too many pages in the kluster, return 3190789Sahrens * as many pages as possible starting from the desired 3191789Sahrens * offset `off'. 3192789Sahrens * NOTE: the page list will always be null terminated. 3193789Sahrens */ 3194789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3195789Sahrens 3196789Sahrens return (0); 3197789Sahrens } 3198789Sahrens 3199789Sahrens /* 3200789Sahrens * Return pointers to the pages for the file region [off, off + len] 3201789Sahrens * in the pl array. If plsz is greater than len, this function may 3202789Sahrens * also return page pointers from before or after the specified 3203789Sahrens * region (i.e. some region [off', off' + plsz]). These additional 3204789Sahrens * pages are only returned if they are already in the cache, or were 3205789Sahrens * created as part of a klustered read. 3206789Sahrens * 3207789Sahrens * IN: vp - vnode of file to get data from. 3208789Sahrens * off - position in file to get data from. 3209789Sahrens * len - amount of data to retrieve. 3210789Sahrens * plsz - length of provided page list. 3211789Sahrens * seg - segment to obtain pages for. 3212789Sahrens * addr - virtual address of fault. 3213789Sahrens * rw - mode of created pages. 3214789Sahrens * cr - credentials of caller. 3215789Sahrens * 3216789Sahrens * OUT: protp - protection mode of created pages. 3217789Sahrens * pl - list of pages created. 3218789Sahrens * 3219789Sahrens * RETURN: 0 if success 3220789Sahrens * error code if failure 3221789Sahrens * 3222789Sahrens * Timestamps: 3223789Sahrens * vp - atime updated 3224789Sahrens */ 3225789Sahrens /* ARGSUSED */ 3226789Sahrens static int 3227789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3228789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3229789Sahrens enum seg_rw rw, cred_t *cr) 3230789Sahrens { 3231789Sahrens znode_t *zp = VTOZ(vp); 3232789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3233789Sahrens page_t *pp, **pl0 = pl; 32342752Sperrin int need_unlock = 0, err = 0; 32352752Sperrin offset_t orig_off; 3236789Sahrens 3237789Sahrens ZFS_ENTER(zfsvfs); 3238789Sahrens 3239789Sahrens if (protp) 3240789Sahrens *protp = PROT_ALL; 3241789Sahrens 3242789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 3243789Sahrens 3244789Sahrens /* no faultahead (for now) */ 3245789Sahrens if (pl == NULL) { 3246789Sahrens ZFS_EXIT(zfsvfs); 3247789Sahrens return (0); 3248789Sahrens } 3249789Sahrens 3250789Sahrens /* can't fault past EOF */ 3251789Sahrens if (off >= zp->z_phys->zp_size) { 3252789Sahrens ZFS_EXIT(zfsvfs); 3253789Sahrens return (EFAULT); 3254789Sahrens } 32552752Sperrin orig_off = off; 3256789Sahrens 3257789Sahrens /* 3258789Sahrens * If we already own the lock, then we must be page faulting 3259789Sahrens * in the middle of a write to this file (i.e., we are writing 3260789Sahrens * to this file using data from a mapped region of the file). 3261789Sahrens */ 32622752Sperrin if (rw_owner(&zp->z_map_lock) != curthread) { 3263789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 3264789Sahrens need_unlock = TRUE; 3265789Sahrens } 3266789Sahrens 3267789Sahrens /* 3268789Sahrens * Loop through the requested range [off, off + len] looking 3269789Sahrens * for pages. If we don't find a page, we will need to create 3270789Sahrens * a new page and fill it with data from the file. 3271789Sahrens */ 3272789Sahrens while (len > 0) { 3273789Sahrens if (plsz < PAGESIZE) 3274789Sahrens break; 3275789Sahrens if (pp = page_lookup(vp, off, SE_SHARED)) { 3276789Sahrens *pl++ = pp; 3277789Sahrens off += PAGESIZE; 3278789Sahrens addr += PAGESIZE; 3279789Sahrens len -= PAGESIZE; 3280789Sahrens plsz -= PAGESIZE; 3281789Sahrens } else { 3282789Sahrens err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 32832752Sperrin if (err) 32842752Sperrin goto out; 3285789Sahrens /* 3286789Sahrens * klustering may have changed our region 3287789Sahrens * to be block aligned. 3288789Sahrens */ 3289789Sahrens if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3290789Sahrens int delta = off - pp->p_offset; 3291789Sahrens len += delta; 3292789Sahrens off -= delta; 3293789Sahrens addr -= delta; 3294789Sahrens } 3295789Sahrens while (*pl) { 3296789Sahrens pl++; 3297789Sahrens off += PAGESIZE; 3298789Sahrens addr += PAGESIZE; 3299789Sahrens plsz -= PAGESIZE; 3300789Sahrens if (len > PAGESIZE) 3301789Sahrens len -= PAGESIZE; 3302789Sahrens else 3303789Sahrens len = 0; 3304789Sahrens } 3305789Sahrens } 3306789Sahrens } 3307789Sahrens 3308789Sahrens /* 3309789Sahrens * Fill out the page array with any pages already in the cache. 3310789Sahrens */ 3311789Sahrens while (plsz > 0) { 3312789Sahrens pp = page_lookup_nowait(vp, off, SE_SHARED); 3313789Sahrens if (pp == NULL) 3314789Sahrens break; 3315789Sahrens *pl++ = pp; 3316789Sahrens off += PAGESIZE; 3317789Sahrens plsz -= PAGESIZE; 3318789Sahrens } 3319789Sahrens 3320789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3321789Sahrens out: 33222752Sperrin /* 33232752Sperrin * We can't grab the range lock for the page as reader which would 33242752Sperrin * stop truncation as this leads to deadlock. So we need to recheck 33252752Sperrin * the file size. 33262752Sperrin */ 33272752Sperrin if (orig_off >= zp->z_phys->zp_size) 33282752Sperrin err = EFAULT; 33292752Sperrin if (err) { 33302752Sperrin /* 33312752Sperrin * Release any pages we have previously locked. 33322752Sperrin */ 33332752Sperrin while (pl > pl0) 33342752Sperrin page_unlock(*--pl); 33352752Sperrin } 33362752Sperrin 3337789Sahrens *pl = NULL; 3338789Sahrens 3339789Sahrens if (need_unlock) 3340789Sahrens rw_exit(&zp->z_map_lock); 3341789Sahrens 3342789Sahrens ZFS_EXIT(zfsvfs); 3343789Sahrens return (err); 3344789Sahrens } 3345789Sahrens 33461544Seschrock /* 33471544Seschrock * Request a memory map for a section of a file. This code interacts 33481544Seschrock * with common code and the VM system as follows: 33491544Seschrock * 33501544Seschrock * common code calls mmap(), which ends up in smmap_common() 33511544Seschrock * 33521544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 33531544Seschrock * 33541544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 33551544Seschrock * 33561544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 33571544Seschrock * 33581544Seschrock * zfs_addmap() updates z_mapcnt 33591544Seschrock */ 3360789Sahrens static int 3361789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3362789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3363789Sahrens { 3364789Sahrens znode_t *zp = VTOZ(vp); 3365789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3366789Sahrens segvn_crargs_t vn_a; 3367789Sahrens int error; 3368789Sahrens 3369789Sahrens ZFS_ENTER(zfsvfs); 3370789Sahrens 3371789Sahrens if (vp->v_flag & VNOMAP) { 3372789Sahrens ZFS_EXIT(zfsvfs); 3373789Sahrens return (ENOSYS); 3374789Sahrens } 3375789Sahrens 3376789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 3377789Sahrens ZFS_EXIT(zfsvfs); 3378789Sahrens return (ENXIO); 3379789Sahrens } 3380789Sahrens 3381789Sahrens if (vp->v_type != VREG) { 3382789Sahrens ZFS_EXIT(zfsvfs); 3383789Sahrens return (ENODEV); 3384789Sahrens } 3385789Sahrens 3386789Sahrens /* 3387789Sahrens * If file is locked, disallow mapping. 3388789Sahrens */ 33891544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 33901544Seschrock ZFS_EXIT(zfsvfs); 33911544Seschrock return (EAGAIN); 3392789Sahrens } 3393789Sahrens 3394789Sahrens as_rangelock(as); 3395789Sahrens if ((flags & MAP_FIXED) == 0) { 3396789Sahrens map_addr(addrp, len, off, 1, flags); 3397789Sahrens if (*addrp == NULL) { 3398789Sahrens as_rangeunlock(as); 3399789Sahrens ZFS_EXIT(zfsvfs); 3400789Sahrens return (ENOMEM); 3401789Sahrens } 3402789Sahrens } else { 3403789Sahrens /* 3404789Sahrens * User specified address - blow away any previous mappings 3405789Sahrens */ 3406789Sahrens (void) as_unmap(as, *addrp, len); 3407789Sahrens } 3408789Sahrens 3409789Sahrens vn_a.vp = vp; 3410789Sahrens vn_a.offset = (u_offset_t)off; 3411789Sahrens vn_a.type = flags & MAP_TYPE; 3412789Sahrens vn_a.prot = prot; 3413789Sahrens vn_a.maxprot = maxprot; 3414789Sahrens vn_a.cred = cr; 3415789Sahrens vn_a.amp = NULL; 3416789Sahrens vn_a.flags = flags & ~MAP_TYPE; 34171417Skchow vn_a.szc = 0; 34181417Skchow vn_a.lgrp_mem_policy_flags = 0; 3419789Sahrens 3420789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 3421789Sahrens 3422789Sahrens as_rangeunlock(as); 3423789Sahrens ZFS_EXIT(zfsvfs); 3424789Sahrens return (error); 3425789Sahrens } 3426789Sahrens 3427789Sahrens /* ARGSUSED */ 3428789Sahrens static int 3429789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3430789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3431789Sahrens { 34321544Seschrock uint64_t pages = btopr(len); 34331544Seschrock 34341544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3435789Sahrens return (0); 3436789Sahrens } 3437789Sahrens 34381773Seschrock /* 34391773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 34401773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 34411773Seschrock * detecting when the data was actually modified, we have to resort to 34421773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 34431773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 34441773Seschrock * which by far the most common case: 34451773Seschrock * 34461773Seschrock * open() 34471773Seschrock * mmap() 34481773Seschrock * <modify memory> 34491773Seschrock * munmap() 34501773Seschrock * close() 34511773Seschrock * <time lapse> 34521773Seschrock * putpage() via fsflush 34531773Seschrock * 34541773Seschrock * If we wait until fsflush to come along, we can have a modification time that 34551773Seschrock * is some arbitrary point in the future. In order to prevent this in the 34561773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 34571773Seschrock * torn down. 34581773Seschrock */ 3459789Sahrens /* ARGSUSED */ 3460789Sahrens static int 3461789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3462789Sahrens size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3463789Sahrens { 34641544Seschrock uint64_t pages = btopr(len); 34651544Seschrock 34661544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 34671544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 34681773Seschrock 34691773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 34701773Seschrock vn_has_cached_data(vp)) 34711773Seschrock (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 34721773Seschrock 3473789Sahrens return (0); 3474789Sahrens } 3475789Sahrens 3476789Sahrens /* 3477789Sahrens * Free or allocate space in a file. Currently, this function only 3478789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 3479789Sahrens * misnamed, as its functionality includes the ability to allocate as 3480789Sahrens * well as free space. 3481789Sahrens * 3482789Sahrens * IN: vp - vnode of file to free data in. 3483789Sahrens * cmd - action to take (only F_FREESP supported). 3484789Sahrens * bfp - section of file to free/alloc. 3485789Sahrens * flag - current file open mode flags. 3486789Sahrens * offset - current file offset. 3487789Sahrens * cr - credentials of caller [UNUSED]. 3488789Sahrens * 3489789Sahrens * RETURN: 0 if success 3490789Sahrens * error code if failure 3491789Sahrens * 3492789Sahrens * Timestamps: 3493789Sahrens * vp - ctime|mtime updated 3494789Sahrens */ 3495789Sahrens /* ARGSUSED */ 3496789Sahrens static int 3497789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3498789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 3499789Sahrens { 3500789Sahrens znode_t *zp = VTOZ(vp); 3501789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3502789Sahrens uint64_t off, len; 3503789Sahrens int error; 3504789Sahrens 3505789Sahrens ZFS_ENTER(zfsvfs); 3506789Sahrens 3507789Sahrens top: 3508789Sahrens if (cmd != F_FREESP) { 3509789Sahrens ZFS_EXIT(zfsvfs); 3510789Sahrens return (EINVAL); 3511789Sahrens } 3512789Sahrens 3513789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 3514789Sahrens ZFS_EXIT(zfsvfs); 3515789Sahrens return (error); 3516789Sahrens } 3517789Sahrens 3518789Sahrens if (bfp->l_len < 0) { 3519789Sahrens ZFS_EXIT(zfsvfs); 3520789Sahrens return (EINVAL); 3521789Sahrens } 3522789Sahrens 3523789Sahrens off = bfp->l_start; 35241669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 35251878Smaybee 35261878Smaybee do { 35271878Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 35282113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 35291878Smaybee } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3530789Sahrens 3531789Sahrens ZFS_EXIT(zfsvfs); 3532789Sahrens return (error); 3533789Sahrens } 3534789Sahrens 3535789Sahrens static int 3536789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp) 3537789Sahrens { 3538789Sahrens znode_t *zp = VTOZ(vp); 3539789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3540789Sahrens uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3541789Sahrens uint64_t object = zp->z_id; 3542789Sahrens zfid_short_t *zfid; 3543789Sahrens int size, i; 3544789Sahrens 3545789Sahrens ZFS_ENTER(zfsvfs); 3546789Sahrens 3547789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3548789Sahrens if (fidp->fid_len < size) { 3549789Sahrens fidp->fid_len = size; 35501512Sek110237 ZFS_EXIT(zfsvfs); 3551789Sahrens return (ENOSPC); 3552789Sahrens } 3553789Sahrens 3554789Sahrens zfid = (zfid_short_t *)fidp; 3555789Sahrens 3556789Sahrens zfid->zf_len = size; 3557789Sahrens 3558789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 3559789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3560789Sahrens 3561789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 3562789Sahrens if (gen == 0) 3563789Sahrens gen = 1; 3564789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 3565789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3566789Sahrens 3567789Sahrens if (size == LONG_FID_LEN) { 3568789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3569789Sahrens zfid_long_t *zlfid; 3570789Sahrens 3571789Sahrens zlfid = (zfid_long_t *)fidp; 3572789Sahrens 3573789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3574789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3575789Sahrens 3576789Sahrens /* XXX - this should be the generation number for the objset */ 3577789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3578789Sahrens zlfid->zf_setgen[i] = 0; 3579789Sahrens } 3580789Sahrens 3581789Sahrens ZFS_EXIT(zfsvfs); 3582789Sahrens return (0); 3583789Sahrens } 3584789Sahrens 3585789Sahrens static int 3586789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3587789Sahrens { 3588789Sahrens znode_t *zp, *xzp; 3589789Sahrens zfsvfs_t *zfsvfs; 3590789Sahrens zfs_dirlock_t *dl; 3591789Sahrens int error; 3592789Sahrens 3593789Sahrens switch (cmd) { 3594789Sahrens case _PC_LINK_MAX: 3595789Sahrens *valp = ULONG_MAX; 3596789Sahrens return (0); 3597789Sahrens 3598789Sahrens case _PC_FILESIZEBITS: 3599789Sahrens *valp = 64; 3600789Sahrens return (0); 3601789Sahrens 3602789Sahrens case _PC_XATTR_EXISTS: 3603789Sahrens zp = VTOZ(vp); 3604789Sahrens zfsvfs = zp->z_zfsvfs; 3605789Sahrens ZFS_ENTER(zfsvfs); 3606789Sahrens *valp = 0; 3607789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 3608789Sahrens ZXATTR | ZEXISTS | ZSHARED); 3609789Sahrens if (error == 0) { 3610789Sahrens zfs_dirent_unlock(dl); 3611789Sahrens if (!zfs_dirempty(xzp)) 3612789Sahrens *valp = 1; 3613789Sahrens VN_RELE(ZTOV(xzp)); 3614789Sahrens } else if (error == ENOENT) { 3615789Sahrens /* 3616789Sahrens * If there aren't extended attributes, it's the 3617789Sahrens * same as having zero of them. 3618789Sahrens */ 3619789Sahrens error = 0; 3620789Sahrens } 3621789Sahrens ZFS_EXIT(zfsvfs); 3622789Sahrens return (error); 3623789Sahrens 3624789Sahrens case _PC_ACL_ENABLED: 3625789Sahrens *valp = _ACL_ACE_ENABLED; 3626789Sahrens return (0); 3627789Sahrens 3628789Sahrens case _PC_MIN_HOLE_SIZE: 3629789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 3630789Sahrens return (0); 3631789Sahrens 3632789Sahrens default: 3633789Sahrens return (fs_pathconf(vp, cmd, valp, cr)); 3634789Sahrens } 3635789Sahrens } 3636789Sahrens 3637789Sahrens /*ARGSUSED*/ 3638789Sahrens static int 3639789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3640789Sahrens { 3641789Sahrens znode_t *zp = VTOZ(vp); 3642789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3643789Sahrens int error; 3644789Sahrens 3645789Sahrens ZFS_ENTER(zfsvfs); 3646789Sahrens error = zfs_getacl(zp, vsecp, cr); 3647789Sahrens ZFS_EXIT(zfsvfs); 3648789Sahrens 3649789Sahrens return (error); 3650789Sahrens } 3651789Sahrens 3652789Sahrens /*ARGSUSED*/ 3653789Sahrens static int 3654789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3655789Sahrens { 3656789Sahrens znode_t *zp = VTOZ(vp); 3657789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3658789Sahrens int error; 3659789Sahrens 3660789Sahrens ZFS_ENTER(zfsvfs); 3661789Sahrens error = zfs_setacl(zp, vsecp, cr); 3662789Sahrens ZFS_EXIT(zfsvfs); 3663789Sahrens return (error); 3664789Sahrens } 3665789Sahrens 3666789Sahrens /* 3667789Sahrens * Predeclare these here so that the compiler assumes that 3668789Sahrens * this is an "old style" function declaration that does 3669789Sahrens * not include arguments => we won't get type mismatch errors 3670789Sahrens * in the initializations that follow. 3671789Sahrens */ 3672789Sahrens static int zfs_inval(); 3673789Sahrens static int zfs_isdir(); 3674789Sahrens 3675789Sahrens static int 3676789Sahrens zfs_inval() 3677789Sahrens { 3678789Sahrens return (EINVAL); 3679789Sahrens } 3680789Sahrens 3681789Sahrens static int 3682789Sahrens zfs_isdir() 3683789Sahrens { 3684789Sahrens return (EISDIR); 3685789Sahrens } 3686789Sahrens /* 3687789Sahrens * Directory vnode operations template 3688789Sahrens */ 3689789Sahrens vnodeops_t *zfs_dvnodeops; 3690789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 3691789Sahrens VOPNAME_OPEN, zfs_open, 3692789Sahrens VOPNAME_CLOSE, zfs_close, 3693789Sahrens VOPNAME_READ, zfs_isdir, 3694789Sahrens VOPNAME_WRITE, zfs_isdir, 3695789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3696789Sahrens VOPNAME_GETATTR, zfs_getattr, 3697789Sahrens VOPNAME_SETATTR, zfs_setattr, 3698789Sahrens VOPNAME_ACCESS, zfs_access, 3699789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3700789Sahrens VOPNAME_CREATE, zfs_create, 3701789Sahrens VOPNAME_REMOVE, zfs_remove, 3702789Sahrens VOPNAME_LINK, zfs_link, 3703789Sahrens VOPNAME_RENAME, zfs_rename, 3704789Sahrens VOPNAME_MKDIR, zfs_mkdir, 3705789Sahrens VOPNAME_RMDIR, zfs_rmdir, 3706789Sahrens VOPNAME_READDIR, zfs_readdir, 3707789Sahrens VOPNAME_SYMLINK, zfs_symlink, 3708789Sahrens VOPNAME_FSYNC, zfs_fsync, 3709789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3710789Sahrens VOPNAME_FID, zfs_fid, 3711789Sahrens VOPNAME_SEEK, zfs_seek, 3712789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3713789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3714789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3715789Sahrens NULL, NULL 3716789Sahrens }; 3717789Sahrens 3718789Sahrens /* 3719789Sahrens * Regular file vnode operations template 3720789Sahrens */ 3721789Sahrens vnodeops_t *zfs_fvnodeops; 3722789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 3723789Sahrens VOPNAME_OPEN, zfs_open, 3724789Sahrens VOPNAME_CLOSE, zfs_close, 3725789Sahrens VOPNAME_READ, zfs_read, 3726789Sahrens VOPNAME_WRITE, zfs_write, 3727789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3728789Sahrens VOPNAME_GETATTR, zfs_getattr, 3729789Sahrens VOPNAME_SETATTR, zfs_setattr, 3730789Sahrens VOPNAME_ACCESS, zfs_access, 3731789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3732789Sahrens VOPNAME_RENAME, zfs_rename, 3733789Sahrens VOPNAME_FSYNC, zfs_fsync, 3734789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive, 3735789Sahrens VOPNAME_FID, zfs_fid, 3736789Sahrens VOPNAME_SEEK, zfs_seek, 3737789Sahrens VOPNAME_FRLOCK, zfs_frlock, 3738789Sahrens VOPNAME_SPACE, zfs_space, 3739789Sahrens VOPNAME_GETPAGE, zfs_getpage, 3740789Sahrens VOPNAME_PUTPAGE, zfs_putpage, 3741789Sahrens VOPNAME_MAP, (fs_generic_func_p) zfs_map, 3742789Sahrens VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap, 3743789Sahrens VOPNAME_DELMAP, zfs_delmap, 3744789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3745789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3746789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3747789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3748789Sahrens NULL, NULL 3749789Sahrens }; 3750789Sahrens 3751789Sahrens /* 3752789Sahrens * Symbolic link vnode operations template 3753789Sahrens */ 3754789Sahrens vnodeops_t *zfs_symvnodeops; 3755789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 3756789Sahrens VOPNAME_GETATTR, zfs_getattr, 3757789Sahrens VOPNAME_SETATTR, zfs_setattr, 3758789Sahrens VOPNAME_ACCESS, zfs_access, 3759789Sahrens VOPNAME_RENAME, zfs_rename, 3760789Sahrens VOPNAME_READLINK, zfs_readlink, 3761789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3762789Sahrens VOPNAME_FID, zfs_fid, 3763789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3764789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3765789Sahrens NULL, NULL 3766789Sahrens }; 3767789Sahrens 3768789Sahrens /* 3769789Sahrens * Extended attribute directory vnode operations template 3770789Sahrens * This template is identical to the directory vnodes 3771789Sahrens * operation template except for restricted operations: 3772789Sahrens * VOP_MKDIR() 3773789Sahrens * VOP_SYMLINK() 3774789Sahrens * Note that there are other restrictions embedded in: 3775789Sahrens * zfs_create() - restrict type to VREG 3776789Sahrens * zfs_link() - no links into/out of attribute space 3777789Sahrens * zfs_rename() - no moves into/out of attribute space 3778789Sahrens */ 3779789Sahrens vnodeops_t *zfs_xdvnodeops; 3780789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 3781789Sahrens VOPNAME_OPEN, zfs_open, 3782789Sahrens VOPNAME_CLOSE, zfs_close, 3783789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3784789Sahrens VOPNAME_GETATTR, zfs_getattr, 3785789Sahrens VOPNAME_SETATTR, zfs_setattr, 3786789Sahrens VOPNAME_ACCESS, zfs_access, 3787789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3788789Sahrens VOPNAME_CREATE, zfs_create, 3789789Sahrens VOPNAME_REMOVE, zfs_remove, 3790789Sahrens VOPNAME_LINK, zfs_link, 3791789Sahrens VOPNAME_RENAME, zfs_rename, 3792789Sahrens VOPNAME_MKDIR, zfs_inval, 3793789Sahrens VOPNAME_RMDIR, zfs_rmdir, 3794789Sahrens VOPNAME_READDIR, zfs_readdir, 3795789Sahrens VOPNAME_SYMLINK, zfs_inval, 3796789Sahrens VOPNAME_FSYNC, zfs_fsync, 3797789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3798789Sahrens VOPNAME_FID, zfs_fid, 3799789Sahrens VOPNAME_SEEK, zfs_seek, 3800789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3801789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3802789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3803789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3804789Sahrens NULL, NULL 3805789Sahrens }; 3806789Sahrens 3807789Sahrens /* 3808789Sahrens * Error vnode operations template 3809789Sahrens */ 3810789Sahrens vnodeops_t *zfs_evnodeops; 3811789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 3812789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3813789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3814789Sahrens NULL, NULL 3815789Sahrens }; 3816