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> 43789Sahrens #include <vm/seg_vn.h> 44789Sahrens #include <vm/pvn.h> 45789Sahrens #include <vm/as.h> 46789Sahrens #include <sys/mman.h> 47789Sahrens #include <sys/pathname.h> 48789Sahrens #include <sys/cmn_err.h> 49789Sahrens #include <sys/errno.h> 50789Sahrens #include <sys/unistd.h> 51789Sahrens #include <sys/zfs_vfsops.h> 52789Sahrens #include <sys/zfs_dir.h> 53789Sahrens #include <sys/zfs_acl.h> 54789Sahrens #include <sys/zfs_ioctl.h> 55789Sahrens #include <sys/fs/zfs.h> 56789Sahrens #include <sys/dmu.h> 57789Sahrens #include <sys/spa.h> 58789Sahrens #include <sys/txg.h> 59789Sahrens #include <sys/dbuf.h> 60789Sahrens #include <sys/zap.h> 61789Sahrens #include <sys/dirent.h> 62789Sahrens #include <sys/policy.h> 63789Sahrens #include <sys/sunddi.h> 64789Sahrens #include <sys/filio.h> 65789Sahrens #include "fs/fs_subr.h" 66789Sahrens #include <sys/zfs_ctldir.h> 671484Sek110237 #include <sys/dnlc.h> 681669Sperrin #include <sys/zfs_rlock.h> 69789Sahrens 70789Sahrens /* 71789Sahrens * Programming rules. 72789Sahrens * 73789Sahrens * Each vnode op performs some logical unit of work. To do this, the ZPL must 74789Sahrens * properly lock its in-core state, create a DMU transaction, do the work, 75789Sahrens * record this work in the intent log (ZIL), commit the DMU transaction, 76789Sahrens * and wait the the intent log to commit if it's is a synchronous operation. 77789Sahrens * Morover, the vnode ops must work in both normal and log replay context. 78789Sahrens * The ordering of events is important to avoid deadlocks and references 79789Sahrens * to freed memory. The example below illustrates the following Big Rules: 80789Sahrens * 81789Sahrens * (1) A check must be made in each zfs thread for a mounted file system. 82789Sahrens * This is done avoiding races using ZFS_ENTER(zfsvfs). 83789Sahrens * A ZFS_EXIT(zfsvfs) is needed before all returns. 84789Sahrens * 85789Sahrens * (2) VN_RELE() should always be the last thing except for zil_commit() 86789Sahrens * and ZFS_EXIT(). This is for 3 reasons: 87789Sahrens * First, if it's the last reference, the vnode/znode 88789Sahrens * can be freed, so the zp may point to freed memory. Second, the last 89789Sahrens * reference will call zfs_zinactive(), which may induce a lot of work -- 901669Sperrin * pushing cached pages (which acquires range locks) and syncing out 91789Sahrens * cached atime changes. Third, zfs_zinactive() may require a new tx, 92789Sahrens * which could deadlock the system if you were already holding one. 93789Sahrens * 941757Sperrin * (3) All range locks must be grabbed before calling dmu_tx_assign(), 951757Sperrin * as they can span dmu_tx_assign() calls. 961757Sperrin * 971757Sperrin * (4) Always pass zfsvfs->z_assign as the second argument to dmu_tx_assign(). 98789Sahrens * In normal operation, this will be TXG_NOWAIT. During ZIL replay, 99789Sahrens * it will be a specific txg. Either way, dmu_tx_assign() never blocks. 100789Sahrens * This is critical because we don't want to block while holding locks. 101789Sahrens * Note, in particular, that if a lock is sometimes acquired before 102789Sahrens * the tx assigns, and sometimes after (e.g. z_lock), then failing to 103789Sahrens * use a non-blocking assign can deadlock the system. The scenario: 104789Sahrens * 105789Sahrens * Thread A has grabbed a lock before calling dmu_tx_assign(). 106789Sahrens * Thread B is in an already-assigned tx, and blocks for this lock. 107789Sahrens * Thread A calls dmu_tx_assign(TXG_WAIT) and blocks in txg_wait_open() 108789Sahrens * forever, because the previous txg can't quiesce until B's tx commits. 109789Sahrens * 110789Sahrens * If dmu_tx_assign() returns ERESTART and zfsvfs->z_assign is TXG_NOWAIT, 1112113Sahrens * then drop all locks, call dmu_tx_wait(), and try again. 112789Sahrens * 1131757Sperrin * (5) If the operation succeeded, generate the intent log entry for it 114789Sahrens * before dropping locks. This ensures that the ordering of events 115789Sahrens * in the intent log matches the order in which they actually occurred. 116789Sahrens * 1171757Sperrin * (6) At the end of each vnode op, the DMU tx must always commit, 118789Sahrens * regardless of whether there were any errors. 119789Sahrens * 1201757Sperrin * (7) After dropping all locks, invoke zil_commit(zilog, seq, ioflag) 121789Sahrens * to ensure that synchronous semantics are provided when necessary. 122789Sahrens * 123789Sahrens * In general, this is how things should be ordered in each vnode op: 124789Sahrens * 125789Sahrens * ZFS_ENTER(zfsvfs); // exit if unmounted 126789Sahrens * top: 127789Sahrens * zfs_dirent_lock(&dl, ...) // lock directory entry (may VN_HOLD()) 128789Sahrens * rw_enter(...); // grab any other locks you need 129789Sahrens * tx = dmu_tx_create(...); // get DMU tx 130789Sahrens * dmu_tx_hold_*(); // hold each object you might modify 131789Sahrens * error = dmu_tx_assign(tx, zfsvfs->z_assign); // try to assign 132789Sahrens * if (error) { 133789Sahrens * rw_exit(...); // drop locks 134789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 135789Sahrens * VN_RELE(...); // release held vnodes 136789Sahrens * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1372113Sahrens * dmu_tx_wait(tx); 1382113Sahrens * dmu_tx_abort(tx); 139789Sahrens * goto top; 140789Sahrens * } 1412113Sahrens * dmu_tx_abort(tx); // abort DMU tx 142789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 143789Sahrens * return (error); // really out of space 144789Sahrens * } 145789Sahrens * error = do_real_work(); // do whatever this VOP does 146789Sahrens * if (error == 0) 147789Sahrens * seq = zfs_log_*(...); // on success, make ZIL entry 148789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 149789Sahrens * rw_exit(...); // drop locks 150789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 151789Sahrens * VN_RELE(...); // release held vnodes 152789Sahrens * zil_commit(zilog, seq, ioflag); // synchronous when necessary 153789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 154789Sahrens * return (error); // done, report error 155789Sahrens */ 156789Sahrens 157789Sahrens /* ARGSUSED */ 158789Sahrens static int 159789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr) 160789Sahrens { 161789Sahrens return (0); 162789Sahrens } 163789Sahrens 164789Sahrens /* ARGSUSED */ 165789Sahrens static int 166789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 167789Sahrens { 168789Sahrens /* 169789Sahrens * Clean up any locks held by this process on the vp. 170789Sahrens */ 171789Sahrens cleanlocks(vp, ddi_get_pid(), 0); 172789Sahrens cleanshares(vp, ddi_get_pid()); 173789Sahrens 174789Sahrens return (0); 175789Sahrens } 176789Sahrens 177789Sahrens /* 178789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 179789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 180789Sahrens */ 181789Sahrens static int 182789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 183789Sahrens { 184789Sahrens znode_t *zp = VTOZ(vp); 185789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 186789Sahrens uint64_t file_sz; 187789Sahrens int error; 188789Sahrens boolean_t hole; 189789Sahrens 190789Sahrens file_sz = zp->z_phys->zp_size; 191789Sahrens if (noff >= file_sz) { 192789Sahrens return (ENXIO); 193789Sahrens } 194789Sahrens 195789Sahrens if (cmd == _FIO_SEEK_HOLE) 196789Sahrens hole = B_TRUE; 197789Sahrens else 198789Sahrens hole = B_FALSE; 199789Sahrens 200789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 201789Sahrens 202789Sahrens /* end of file? */ 203789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 204789Sahrens /* 205789Sahrens * Handle the virtual hole at the end of file. 206789Sahrens */ 207789Sahrens if (hole) { 208789Sahrens *off = file_sz; 209789Sahrens return (0); 210789Sahrens } 211789Sahrens return (ENXIO); 212789Sahrens } 213789Sahrens 214789Sahrens if (noff < *off) 215789Sahrens return (error); 216789Sahrens *off = noff; 217789Sahrens return (error); 218789Sahrens } 219789Sahrens 220789Sahrens /* ARGSUSED */ 221789Sahrens static int 222789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 223789Sahrens int *rvalp) 224789Sahrens { 225789Sahrens offset_t off; 226789Sahrens int error; 227789Sahrens zfsvfs_t *zfsvfs; 228789Sahrens 229789Sahrens switch (com) { 230789Sahrens case _FIOFFS: 231789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 232789Sahrens 2331544Seschrock /* 2341544Seschrock * The following two ioctls are used by bfu. Faking out, 2351544Seschrock * necessary to avoid bfu errors. 2361544Seschrock */ 2371544Seschrock case _FIOGDIO: 2381544Seschrock case _FIOSDIO: 2391544Seschrock return (0); 2401544Seschrock 241789Sahrens case _FIO_SEEK_DATA: 242789Sahrens case _FIO_SEEK_HOLE: 243789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 244789Sahrens return (EFAULT); 245789Sahrens 246789Sahrens zfsvfs = VTOZ(vp)->z_zfsvfs; 247789Sahrens ZFS_ENTER(zfsvfs); 248789Sahrens 249789Sahrens /* offset parameter is in/out */ 250789Sahrens error = zfs_holey(vp, com, &off); 251789Sahrens ZFS_EXIT(zfsvfs); 252789Sahrens if (error) 253789Sahrens return (error); 254789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 255789Sahrens return (EFAULT); 256789Sahrens return (0); 257789Sahrens } 258789Sahrens return (ENOTTY); 259789Sahrens } 260789Sahrens 261789Sahrens /* 262789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 263789Sahrens * between the DMU cache and the memory mapped pages. What this means: 264789Sahrens * 265789Sahrens * On Write: If we find a memory mapped page, we write to *both* 266789Sahrens * the page and the dmu buffer. 267789Sahrens * 268789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 269789Sahrens * the file is memory mapped. 270789Sahrens */ 271789Sahrens static int 272789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx) 273789Sahrens { 274789Sahrens znode_t *zp = VTOZ(vp); 275789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 276789Sahrens int64_t start, off; 277789Sahrens int len = nbytes; 278789Sahrens int error = 0; 279789Sahrens 280789Sahrens start = uio->uio_loffset; 281789Sahrens off = start & PAGEOFFSET; 282789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 283789Sahrens page_t *pp; 284789Sahrens uint64_t bytes = MIN(PAGESIZE - off, len); 285789Sahrens 286789Sahrens /* 287789Sahrens * We don't want a new page to "appear" in the middle of 288789Sahrens * the file update (because it may not get the write 289789Sahrens * update data), so we grab a lock to block 290789Sahrens * zfs_getpage(). 291789Sahrens */ 292789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 293789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 294789Sahrens caddr_t va; 295789Sahrens 296789Sahrens rw_exit(&zp->z_map_lock); 297789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 298789Sahrens error = uiomove(va+off, bytes, UIO_WRITE, uio); 299789Sahrens if (error == 0) { 300789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, 301789Sahrens woff, bytes, va+off, tx); 302789Sahrens } 303789Sahrens ppmapout(va); 304789Sahrens page_unlock(pp); 305789Sahrens } else { 306789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 307789Sahrens woff, bytes, uio, tx); 308789Sahrens rw_exit(&zp->z_map_lock); 309789Sahrens } 310789Sahrens len -= bytes; 311789Sahrens woff += bytes; 312789Sahrens off = 0; 313789Sahrens if (error) 314789Sahrens break; 315789Sahrens } 316789Sahrens return (error); 317789Sahrens } 318789Sahrens 319789Sahrens /* 320789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 321789Sahrens * between the DMU cache and the memory mapped pages. What this means: 322789Sahrens * 323789Sahrens * On Read: We "read" preferentially from memory mapped pages, 324789Sahrens * else we default from the dmu buffer. 325789Sahrens * 326789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 327789Sahrens * the file is memory mapped. 328789Sahrens */ 329789Sahrens static int 330789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio) 331789Sahrens { 332789Sahrens int64_t start, off, bytes; 333789Sahrens int len = nbytes; 334789Sahrens int error = 0; 335789Sahrens 336789Sahrens start = uio->uio_loffset; 337789Sahrens off = start & PAGEOFFSET; 338789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 339789Sahrens page_t *pp; 340789Sahrens 341789Sahrens bytes = MIN(PAGESIZE - off, len); 342789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 343789Sahrens caddr_t va; 344789Sahrens 345789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 346789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 347789Sahrens ppmapout(va); 348789Sahrens page_unlock(pp); 349789Sahrens } else { 350789Sahrens /* XXX use dmu_read here? */ 351789Sahrens error = uiomove(addr, bytes, UIO_READ, uio); 352789Sahrens } 353789Sahrens len -= bytes; 354789Sahrens addr += bytes; 355789Sahrens off = 0; 356789Sahrens if (error) 357789Sahrens break; 358789Sahrens } 359789Sahrens return (error); 360789Sahrens } 361789Sahrens 362789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 363789Sahrens 364789Sahrens /* 365789Sahrens * Read bytes from specified file into supplied buffer. 366789Sahrens * 367789Sahrens * IN: vp - vnode of file to be read from. 368789Sahrens * uio - structure supplying read location, range info, 369789Sahrens * and return buffer. 370789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 371789Sahrens * cr - credentials of caller. 372789Sahrens * 373789Sahrens * OUT: uio - updated offset and range, buffer filled. 374789Sahrens * 375789Sahrens * RETURN: 0 if success 376789Sahrens * error code if failure 377789Sahrens * 378789Sahrens * Side Effects: 379789Sahrens * vp - atime updated if byte count > 0 380789Sahrens */ 381789Sahrens /* ARGSUSED */ 382789Sahrens static int 383789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 384789Sahrens { 385789Sahrens znode_t *zp = VTOZ(vp); 386789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 387789Sahrens uint64_t delta; 388789Sahrens ssize_t n, size, cnt, ndone; 389789Sahrens int error, i, numbufs; 390789Sahrens dmu_buf_t *dbp, **dbpp; 3911669Sperrin rl_t *rl; 392789Sahrens 393789Sahrens ZFS_ENTER(zfsvfs); 394789Sahrens 395789Sahrens /* 396789Sahrens * Validate file offset 397789Sahrens */ 398789Sahrens if (uio->uio_loffset < (offset_t)0) { 399789Sahrens ZFS_EXIT(zfsvfs); 400789Sahrens return (EINVAL); 401789Sahrens } 402789Sahrens 403789Sahrens /* 404789Sahrens * Fasttrack empty reads 405789Sahrens */ 406789Sahrens if (uio->uio_resid == 0) { 407789Sahrens ZFS_EXIT(zfsvfs); 408789Sahrens return (0); 409789Sahrens } 410789Sahrens 411789Sahrens /* 4121669Sperrin * Check for mandatory locks 413789Sahrens */ 414789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 415789Sahrens if (error = chklock(vp, FREAD, 416789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 417789Sahrens ZFS_EXIT(zfsvfs); 418789Sahrens return (error); 419789Sahrens } 420789Sahrens } 421789Sahrens 422789Sahrens /* 423789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 424789Sahrens */ 425789Sahrens zil_commit(zfsvfs->z_log, zp->z_last_itx, ioflag & FRSYNC); 426789Sahrens 427789Sahrens /* 4281669Sperrin * Lock the range against changes. 429789Sahrens */ 4301669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4311669Sperrin 432789Sahrens /* 433789Sahrens * If we are reading past end-of-file we can skip 434789Sahrens * to the end; but we might still need to set atime. 435789Sahrens */ 436789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 437789Sahrens cnt = 0; 438789Sahrens error = 0; 439789Sahrens goto out; 440789Sahrens } 441789Sahrens 442789Sahrens cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 443789Sahrens 444789Sahrens for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) { 445789Sahrens ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 446789Sahrens n = MIN(zfs_read_chunk_size, 447789Sahrens zp->z_phys->zp_size - uio->uio_loffset); 448789Sahrens n = MIN(n, cnt); 4492391Smaybee error = dmu_buf_hold_array_by_bonus(zp->z_dbuf, 4501544Seschrock uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp); 4511544Seschrock if (error) 452789Sahrens goto out; 453789Sahrens /* 454789Sahrens * Compute the adjustment to align the dmu buffers 455789Sahrens * with the uio buffer. 456789Sahrens */ 457789Sahrens delta = uio->uio_loffset - dbpp[0]->db_offset; 458789Sahrens 459789Sahrens for (i = 0; i < numbufs; i++) { 460789Sahrens if (n < 0) 461789Sahrens break; 462789Sahrens dbp = dbpp[i]; 463789Sahrens size = dbp->db_size - delta; 464789Sahrens /* 465789Sahrens * XXX -- this is correct, but may be suboptimal. 466789Sahrens * If the pages are all clean, we don't need to 467789Sahrens * go through mappedread(). Maybe the VMODSORT 468789Sahrens * stuff can help us here. 469789Sahrens */ 470789Sahrens if (vn_has_cached_data(vp)) { 471789Sahrens error = mappedread(vp, (caddr_t)dbp->db_data + 472789Sahrens delta, (n < size ? n : size), uio); 473789Sahrens } else { 474789Sahrens error = uiomove((caddr_t)dbp->db_data + delta, 475789Sahrens (n < size ? n : size), UIO_READ, uio); 476789Sahrens } 477789Sahrens if (error) { 4781544Seschrock dmu_buf_rele_array(dbpp, numbufs, FTAG); 479789Sahrens goto out; 480789Sahrens } 481789Sahrens n -= dbp->db_size; 482789Sahrens if (delta) { 483789Sahrens n += delta; 484789Sahrens delta = 0; 485789Sahrens } 486789Sahrens } 4871544Seschrock dmu_buf_rele_array(dbpp, numbufs, FTAG); 488789Sahrens } 489789Sahrens out: 4902237Smaybee zfs_range_unlock(rl); 491789Sahrens 492789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 493789Sahrens ZFS_EXIT(zfsvfs); 494789Sahrens return (error); 495789Sahrens } 496789Sahrens 497789Sahrens /* 498789Sahrens * Fault in the pages of the first n bytes specified by the uio structure. 499789Sahrens * 1 byte in each page is touched and the uio struct is unmodified. 500789Sahrens * Any error will exit this routine as this is only a best 501789Sahrens * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 502789Sahrens */ 503789Sahrens static void 504789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio) 505789Sahrens { 506789Sahrens struct iovec *iov; 507789Sahrens ulong_t cnt, incr; 508789Sahrens caddr_t p; 509789Sahrens uint8_t tmp; 510789Sahrens 511789Sahrens iov = uio->uio_iov; 512789Sahrens 513789Sahrens while (n) { 514789Sahrens cnt = MIN(iov->iov_len, n); 515789Sahrens if (cnt == 0) { 516789Sahrens /* empty iov entry */ 517789Sahrens iov++; 518789Sahrens continue; 519789Sahrens } 520789Sahrens n -= cnt; 521789Sahrens /* 522789Sahrens * touch each page in this segment. 523789Sahrens */ 524789Sahrens p = iov->iov_base; 525789Sahrens while (cnt) { 526789Sahrens switch (uio->uio_segflg) { 527789Sahrens case UIO_USERSPACE: 528789Sahrens case UIO_USERISPACE: 529789Sahrens if (fuword8(p, &tmp)) 530789Sahrens return; 531789Sahrens break; 532789Sahrens case UIO_SYSSPACE: 533789Sahrens if (kcopy(p, &tmp, 1)) 534789Sahrens return; 535789Sahrens break; 536789Sahrens } 537789Sahrens incr = MIN(cnt, PAGESIZE); 538789Sahrens p += incr; 539789Sahrens cnt -= incr; 540789Sahrens } 541789Sahrens /* 542789Sahrens * touch the last byte in case it straddles a page. 543789Sahrens */ 544789Sahrens p--; 545789Sahrens switch (uio->uio_segflg) { 546789Sahrens case UIO_USERSPACE: 547789Sahrens case UIO_USERISPACE: 548789Sahrens if (fuword8(p, &tmp)) 549789Sahrens return; 550789Sahrens break; 551789Sahrens case UIO_SYSSPACE: 552789Sahrens if (kcopy(p, &tmp, 1)) 553789Sahrens return; 554789Sahrens break; 555789Sahrens } 556789Sahrens iov++; 557789Sahrens } 558789Sahrens } 559789Sahrens 560789Sahrens /* 561789Sahrens * Write the bytes to a file. 562789Sahrens * 563789Sahrens * IN: vp - vnode of file to be written to. 564789Sahrens * uio - structure supplying write location, range info, 565789Sahrens * and data buffer. 566789Sahrens * ioflag - FAPPEND flag set if in append mode. 567789Sahrens * cr - credentials of caller. 568789Sahrens * 569789Sahrens * OUT: uio - updated offset and range. 570789Sahrens * 571789Sahrens * RETURN: 0 if success 572789Sahrens * error code if failure 573789Sahrens * 574789Sahrens * Timestamps: 575789Sahrens * vp - ctime|mtime updated if byte count > 0 576789Sahrens */ 577789Sahrens /* ARGSUSED */ 578789Sahrens static int 579789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 580789Sahrens { 581789Sahrens znode_t *zp = VTOZ(vp); 582789Sahrens rlim64_t limit = uio->uio_llimit; 583789Sahrens ssize_t start_resid = uio->uio_resid; 584789Sahrens ssize_t tx_bytes; 585789Sahrens uint64_t end_size; 586789Sahrens dmu_tx_t *tx; 587789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 588789Sahrens zilog_t *zilog = zfsvfs->z_log; 589789Sahrens uint64_t seq = 0; 590789Sahrens offset_t woff; 591789Sahrens ssize_t n, nbytes; 5921669Sperrin rl_t *rl; 593789Sahrens int max_blksz = zfsvfs->z_max_blksz; 5941669Sperrin int error; 595789Sahrens 596789Sahrens /* 597789Sahrens * Fasttrack empty write 598789Sahrens */ 5991669Sperrin n = start_resid; 600789Sahrens if (n == 0) 601789Sahrens return (0); 602789Sahrens 6031669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6041669Sperrin limit = MAXOFFSET_T; 6051669Sperrin 606789Sahrens ZFS_ENTER(zfsvfs); 607789Sahrens 608789Sahrens /* 6092237Smaybee * Pre-fault the pages to ensure slow (eg NFS) pages 6101669Sperrin * don't hold up txg. 611789Sahrens */ 6122237Smaybee zfs_prefault_write(n, uio); 613789Sahrens 614789Sahrens /* 615789Sahrens * If in append mode, set the io offset pointer to eof. 616789Sahrens */ 6171669Sperrin if (ioflag & FAPPEND) { 6181669Sperrin /* 6191669Sperrin * Range lock for a file append: 6201669Sperrin * The value for the start of range will be determined by 6211669Sperrin * zfs_range_lock() (to guarantee append semantics). 6221669Sperrin * If this write will cause the block size to increase, 6231669Sperrin * zfs_range_lock() will lock the entire file, so we must 6241669Sperrin * later reduce the range after we grow the block size. 6251669Sperrin */ 6261669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6271669Sperrin if (rl->r_len == UINT64_MAX) { 6281669Sperrin /* overlocked, zp_size can't change */ 6291669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6301669Sperrin } else { 6311669Sperrin woff = uio->uio_loffset = rl->r_off; 6321669Sperrin } 633789Sahrens } else { 634789Sahrens woff = uio->uio_loffset; 635789Sahrens /* 636789Sahrens * Validate file offset 637789Sahrens */ 638789Sahrens if (woff < 0) { 639789Sahrens ZFS_EXIT(zfsvfs); 640789Sahrens return (EINVAL); 641789Sahrens } 642789Sahrens 643789Sahrens /* 6441669Sperrin * If we need to grow the block size then zfs_range_lock() 6451669Sperrin * will lock a wider range than we request here. 6461669Sperrin * Later after growing the block size we reduce the range. 647789Sahrens */ 6481669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 649789Sahrens } 650789Sahrens 651789Sahrens if (woff >= limit) { 652789Sahrens error = EFBIG; 653789Sahrens goto no_tx_done; 654789Sahrens } 655789Sahrens 656789Sahrens if ((woff + n) > limit || woff > (limit - n)) 657789Sahrens n = limit - woff; 658789Sahrens 659789Sahrens /* 6601669Sperrin * Check for mandatory locks 661789Sahrens */ 662789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 663789Sahrens (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) 664789Sahrens goto no_tx_done; 6651669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 666789Sahrens top: 667789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 668789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 669789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 670789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 671789Sahrens if (error) { 672789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 6732113Sahrens dmu_tx_wait(tx); 6742113Sahrens dmu_tx_abort(tx); 675789Sahrens goto top; 676789Sahrens } 6772113Sahrens dmu_tx_abort(tx); 678789Sahrens goto no_tx_done; 679789Sahrens } 680789Sahrens 6811669Sperrin /* 6821669Sperrin * If zfs_range_lock() over-locked we grow the blocksize 6831669Sperrin * and then reduce the lock range. 6841669Sperrin */ 6851669Sperrin if (rl->r_len == UINT64_MAX) { 686789Sahrens uint64_t new_blksz; 6871669Sperrin 688789Sahrens if (zp->z_blksz > max_blksz) { 689789Sahrens ASSERT(!ISP2(zp->z_blksz)); 690789Sahrens new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 691789Sahrens } else { 692789Sahrens new_blksz = MIN(end_size, max_blksz); 693789Sahrens } 6941669Sperrin zfs_grow_blocksize(zp, new_blksz, tx); 6952237Smaybee zfs_range_reduce(rl, woff, n); 696789Sahrens } 697789Sahrens 698789Sahrens /* 699789Sahrens * The file data does not fit in the znode "cache", so we 700789Sahrens * will be writing to the file block data buffers. 701789Sahrens * Each buffer will be written in a separate transaction; 702789Sahrens * this keeps the intent log records small and allows us 703789Sahrens * to do more fine-grained space accounting. 704789Sahrens */ 705789Sahrens while (n > 0) { 706789Sahrens /* 707789Sahrens * XXX - should we really limit each write to z_max_blksz? 708789Sahrens * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 709789Sahrens */ 710789Sahrens nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 711789Sahrens rw_enter(&zp->z_map_lock, RW_READER); 712789Sahrens 713789Sahrens tx_bytes = uio->uio_resid; 714789Sahrens if (vn_has_cached_data(vp)) { 715789Sahrens rw_exit(&zp->z_map_lock); 716789Sahrens error = mappedwrite(vp, woff, nbytes, uio, tx); 717789Sahrens } else { 718789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 719789Sahrens woff, nbytes, uio, tx); 720789Sahrens rw_exit(&zp->z_map_lock); 721789Sahrens } 722789Sahrens tx_bytes -= uio->uio_resid; 723789Sahrens 724789Sahrens if (error) { 725789Sahrens /* XXX - do we need to "clean up" the dmu buffer? */ 726789Sahrens break; 727789Sahrens } 728789Sahrens 729789Sahrens ASSERT(tx_bytes == nbytes); 730789Sahrens 7311576Smarks /* 7321576Smarks * Clear Set-UID/Set-GID bits on successful write if not 7331576Smarks * privileged and at least one of the excute bits is set. 7341576Smarks * 7351576Smarks * It would be nice to to this after all writes have 7361576Smarks * been done, but that would still expose the ISUID/ISGID 7371576Smarks * to another app after the partial write is committed. 7381576Smarks */ 7391576Smarks 7401576Smarks mutex_enter(&zp->z_acl_lock); 7411576Smarks if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7421576Smarks (S_IXUSR >> 6))) != 0 && 7431576Smarks (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7441576Smarks secpolicy_vnode_setid_retain(cr, 7451576Smarks (zp->z_phys->zp_mode & S_ISUID) != 0 && 7461576Smarks zp->z_phys->zp_uid == 0) != 0) { 7471576Smarks zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 7481576Smarks } 7491576Smarks mutex_exit(&zp->z_acl_lock); 7501576Smarks 751789Sahrens n -= nbytes; 752789Sahrens if (n <= 0) 753789Sahrens break; 754789Sahrens 755789Sahrens /* 756789Sahrens * We have more work ahead of us, so wrap up this transaction 757789Sahrens * and start another. Exact same logic as tx_done below. 758789Sahrens */ 759789Sahrens while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 760789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 761789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 762789Sahrens uio->uio_loffset); 763789Sahrens } 764789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 765789Sahrens seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 766789Sahrens ioflag, uio); 767789Sahrens dmu_tx_commit(tx); 768789Sahrens 769789Sahrens /* 770789Sahrens * Start another transaction. 771789Sahrens */ 772789Sahrens woff = uio->uio_loffset; 773789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 774789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 775789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 776789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 777789Sahrens if (error) { 778789Sahrens if (error == ERESTART && 779789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 7802113Sahrens dmu_tx_wait(tx); 7812113Sahrens dmu_tx_abort(tx); 782789Sahrens goto top; 783789Sahrens } 7842113Sahrens dmu_tx_abort(tx); 785789Sahrens goto no_tx_done; 786789Sahrens } 787789Sahrens } 788789Sahrens 789789Sahrens tx_done: 790789Sahrens 791789Sahrens if (tx_bytes != 0) { 792789Sahrens /* 793789Sahrens * Update the file size if it has changed; account 794789Sahrens * for possible concurrent updates. 795789Sahrens */ 796789Sahrens while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 797789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 798789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 799789Sahrens uio->uio_loffset); 800789Sahrens } 801789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 802789Sahrens seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 803789Sahrens ioflag, uio); 804789Sahrens } 805789Sahrens dmu_tx_commit(tx); 806789Sahrens 807789Sahrens 808789Sahrens no_tx_done: 809789Sahrens 8102237Smaybee zfs_range_unlock(rl); 811789Sahrens 812789Sahrens /* 813789Sahrens * If we're in replay mode, or we made no progress, return error. 814789Sahrens * Otherwise, it's at least a partial write, so it's successful. 815789Sahrens */ 816789Sahrens if (zfsvfs->z_assign >= TXG_INITIAL || uio->uio_resid == start_resid) { 817789Sahrens ZFS_EXIT(zfsvfs); 818789Sahrens return (error); 819789Sahrens } 820789Sahrens 821789Sahrens zil_commit(zilog, seq, ioflag & (FSYNC | FDSYNC)); 822789Sahrens 823789Sahrens ZFS_EXIT(zfsvfs); 824789Sahrens return (0); 825789Sahrens } 826789Sahrens 8272237Smaybee void 8282237Smaybee zfs_get_done(dmu_buf_t *db, void *vrl) 8292237Smaybee { 8302237Smaybee rl_t *rl = (rl_t *)vrl; 8312237Smaybee vnode_t *vp = ZTOV(rl->r_zp); 8322237Smaybee 8332237Smaybee dmu_buf_rele(db, rl); 8342237Smaybee zfs_range_unlock(rl); 8352237Smaybee VN_RELE(vp); 8362237Smaybee } 8372237Smaybee 838789Sahrens /* 839789Sahrens * Get data to generate a TX_WRITE intent log record. 840789Sahrens */ 841789Sahrens int 8422237Smaybee zfs_get_data(void *arg, lr_write_t *lr, char *buf, zio_t *zio) 843789Sahrens { 844789Sahrens zfsvfs_t *zfsvfs = arg; 845789Sahrens objset_t *os = zfsvfs->z_os; 846789Sahrens znode_t *zp; 847789Sahrens uint64_t off = lr->lr_offset; 8482237Smaybee dmu_buf_t *db; 8491669Sperrin rl_t *rl; 850789Sahrens int dlen = lr->lr_length; /* length of user data */ 851789Sahrens int error = 0; 852789Sahrens 853789Sahrens ASSERT(dlen != 0); 854789Sahrens 855789Sahrens /* 8561669Sperrin * Nothing to do if the file has been removed 857789Sahrens */ 858789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 859789Sahrens return (ENOENT); 8601669Sperrin if (zp->z_reap) { 861789Sahrens VN_RELE(ZTOV(zp)); 862789Sahrens return (ENOENT); 863789Sahrens } 864789Sahrens 865789Sahrens /* 866789Sahrens * Write records come in two flavors: immediate and indirect. 867789Sahrens * For small writes it's cheaper to store the data with the 868789Sahrens * log record (immediate); for large writes it's cheaper to 869789Sahrens * sync the data and get a pointer to it (indirect) so that 870789Sahrens * we don't have to write the data twice. 871789Sahrens */ 8721669Sperrin if (buf != NULL) { /* immediate write */ 8731669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 8741669Sperrin /* test for truncation needs to be done while range locked */ 8751669Sperrin if (off >= zp->z_phys->zp_size) { 8761669Sperrin error = ENOENT; 8771669Sperrin goto out; 8781669Sperrin } 879*2449Smaybee VERIFY(0 == dmu_read(os, lr->lr_foid, off, dlen, buf)); 8801669Sperrin } else { /* indirect write */ 8811669Sperrin uint64_t boff; /* block starting offset */ 8821669Sperrin 883*2449Smaybee ASSERT3U(dlen, <=, zp->z_blksz); 884789Sahrens /* 8851669Sperrin * Have to lock the whole block to ensure when it's 8861669Sperrin * written out and it's checksum is being calculated 8871669Sperrin * that no one can change the data. We need to re-check 8881669Sperrin * blocksize after we get the lock in case it's changed! 889789Sahrens */ 8901669Sperrin for (;;) { 8911941Sperrin if (ISP2(zp->z_blksz)) { 8921941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 8931941Sperrin uint64_t); 8941941Sperrin } else { 8951941Sperrin boff = 0; 8961941Sperrin } 8971669Sperrin dlen = zp->z_blksz; 8981669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 8991669Sperrin if (zp->z_blksz == dlen) 9001669Sperrin break; 9012237Smaybee zfs_range_unlock(rl); 9021669Sperrin } 9031669Sperrin /* test for truncation needs to be done while range locked */ 9041669Sperrin if (off >= zp->z_phys->zp_size) { 9051669Sperrin error = ENOENT; 9061669Sperrin goto out; 9071669Sperrin } 9082237Smaybee VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, boff, rl, &db)); 9092237Smaybee ASSERT(boff == db->db_offset); 9102237Smaybee lr->lr_blkoff = off - boff; 9112237Smaybee error = dmu_sync(zio, db, &lr->lr_blkptr, 9122237Smaybee lr->lr_common.lrc_txg, zio ? zfs_get_done : NULL, rl); 9132237Smaybee /* 9142237Smaybee * If we get EINPROGRESS, then we need to wait for a 9152237Smaybee * write IO initiated by dmu_sync() to complete before 9162237Smaybee * we can release this dbuf. We will finish everthing 9172237Smaybee * up in the zfs_get_done() callback. 9182237Smaybee */ 9192237Smaybee if (error == EINPROGRESS) 9202237Smaybee return (0); 9212237Smaybee dmu_buf_rele(db, rl); 922789Sahrens } 9231669Sperrin out: 9242237Smaybee zfs_range_unlock(rl); 925789Sahrens VN_RELE(ZTOV(zp)); 926789Sahrens return (error); 927789Sahrens } 928789Sahrens 929789Sahrens /*ARGSUSED*/ 930789Sahrens static int 931789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr) 932789Sahrens { 933789Sahrens znode_t *zp = VTOZ(vp); 934789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 935789Sahrens int error; 936789Sahrens 937789Sahrens ZFS_ENTER(zfsvfs); 938789Sahrens error = zfs_zaccess_rwx(zp, mode, cr); 939789Sahrens ZFS_EXIT(zfsvfs); 940789Sahrens return (error); 941789Sahrens } 942789Sahrens 943789Sahrens /* 944789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 945789Sahrens * If it exists, return a held vnode reference for it. 946789Sahrens * 947789Sahrens * IN: dvp - vnode of directory to search. 948789Sahrens * nm - name of entry to lookup. 949789Sahrens * pnp - full pathname to lookup [UNUSED]. 950789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 951789Sahrens * rdir - root directory vnode [UNUSED]. 952789Sahrens * cr - credentials of caller. 953789Sahrens * 954789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 955789Sahrens * 956789Sahrens * RETURN: 0 if success 957789Sahrens * error code if failure 958789Sahrens * 959789Sahrens * Timestamps: 960789Sahrens * NA 961789Sahrens */ 962789Sahrens /* ARGSUSED */ 963789Sahrens static int 964789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 965789Sahrens int flags, vnode_t *rdir, cred_t *cr) 966789Sahrens { 967789Sahrens 968789Sahrens znode_t *zdp = VTOZ(dvp); 969789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 970789Sahrens int error; 971789Sahrens 972789Sahrens ZFS_ENTER(zfsvfs); 973789Sahrens 974789Sahrens *vpp = NULL; 975789Sahrens 976789Sahrens if (flags & LOOKUP_XATTR) { 977789Sahrens /* 978789Sahrens * We don't allow recursive attributes.. 979789Sahrens * Maybe someday we will. 980789Sahrens */ 981789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 982789Sahrens ZFS_EXIT(zfsvfs); 983789Sahrens return (EINVAL); 984789Sahrens } 985789Sahrens 986789Sahrens if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) { 987789Sahrens ZFS_EXIT(zfsvfs); 988789Sahrens return (error); 989789Sahrens } 990789Sahrens 991789Sahrens /* 992789Sahrens * Do we have permission to get into attribute directory? 993789Sahrens */ 994789Sahrens 995789Sahrens if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 996789Sahrens VN_RELE(*vpp); 997789Sahrens } 998789Sahrens 999789Sahrens ZFS_EXIT(zfsvfs); 1000789Sahrens return (error); 1001789Sahrens } 1002789Sahrens 10031512Sek110237 if (dvp->v_type != VDIR) { 10041512Sek110237 ZFS_EXIT(zfsvfs); 10051460Smarks return (ENOTDIR); 10061512Sek110237 } 10071460Smarks 1008789Sahrens /* 1009789Sahrens * Check accessibility of directory. 1010789Sahrens */ 1011789Sahrens 1012789Sahrens if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 1013789Sahrens ZFS_EXIT(zfsvfs); 1014789Sahrens return (error); 1015789Sahrens } 1016789Sahrens 1017789Sahrens if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 1018789Sahrens 1019789Sahrens /* 1020789Sahrens * Convert device special files 1021789Sahrens */ 1022789Sahrens if (IS_DEVVP(*vpp)) { 1023789Sahrens vnode_t *svp; 1024789Sahrens 1025789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1026789Sahrens VN_RELE(*vpp); 1027789Sahrens if (svp == NULL) 1028789Sahrens error = ENOSYS; 1029789Sahrens else 1030789Sahrens *vpp = svp; 1031789Sahrens } 1032789Sahrens } 1033789Sahrens 1034789Sahrens ZFS_EXIT(zfsvfs); 1035789Sahrens return (error); 1036789Sahrens } 1037789Sahrens 1038789Sahrens /* 1039789Sahrens * Attempt to create a new entry in a directory. If the entry 1040789Sahrens * already exists, truncate the file if permissible, else return 1041789Sahrens * an error. Return the vp of the created or trunc'd file. 1042789Sahrens * 1043789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1044789Sahrens * name - name of new file entry. 1045789Sahrens * vap - attributes of new file. 1046789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1047789Sahrens * mode - mode to open file with. 1048789Sahrens * cr - credentials of caller. 1049789Sahrens * flag - large file flag [UNUSED]. 1050789Sahrens * 1051789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1052789Sahrens * 1053789Sahrens * RETURN: 0 if success 1054789Sahrens * error code if failure 1055789Sahrens * 1056789Sahrens * Timestamps: 1057789Sahrens * dvp - ctime|mtime updated if new entry created 1058789Sahrens * vp - ctime|mtime always, atime if new 1059789Sahrens */ 1060789Sahrens /* ARGSUSED */ 1061789Sahrens static int 1062789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1063789Sahrens int mode, vnode_t **vpp, cred_t *cr, int flag) 1064789Sahrens { 1065789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1066789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1067789Sahrens zilog_t *zilog = zfsvfs->z_log; 1068789Sahrens uint64_t seq = 0; 1069789Sahrens objset_t *os = zfsvfs->z_os; 1070789Sahrens zfs_dirlock_t *dl; 1071789Sahrens dmu_tx_t *tx; 1072789Sahrens int error; 1073789Sahrens uint64_t zoid; 1074789Sahrens 1075789Sahrens ZFS_ENTER(zfsvfs); 1076789Sahrens 1077789Sahrens top: 1078789Sahrens *vpp = NULL; 1079789Sahrens 1080789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1081789Sahrens vap->va_mode &= ~VSVTX; 1082789Sahrens 1083789Sahrens if (*name == '\0') { 1084789Sahrens /* 1085789Sahrens * Null component name refers to the directory itself. 1086789Sahrens */ 1087789Sahrens VN_HOLD(dvp); 1088789Sahrens zp = dzp; 1089789Sahrens dl = NULL; 1090789Sahrens error = 0; 1091789Sahrens } else { 1092789Sahrens /* possible VN_HOLD(zp) */ 1093789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1094789Sahrens if (strcmp(name, "..") == 0) 1095789Sahrens error = EISDIR; 1096789Sahrens ZFS_EXIT(zfsvfs); 1097789Sahrens return (error); 1098789Sahrens } 1099789Sahrens } 1100789Sahrens 1101789Sahrens zoid = zp ? zp->z_id : -1ULL; 1102789Sahrens 1103789Sahrens if (zp == NULL) { 1104789Sahrens /* 1105789Sahrens * Create a new file object and update the directory 1106789Sahrens * to reference it. 1107789Sahrens */ 1108789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1109789Sahrens goto out; 1110789Sahrens } 1111789Sahrens 1112789Sahrens /* 1113789Sahrens * We only support the creation of regular files in 1114789Sahrens * extended attribute directories. 1115789Sahrens */ 1116789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1117789Sahrens (vap->va_type != VREG)) { 1118789Sahrens error = EINVAL; 1119789Sahrens goto out; 1120789Sahrens } 1121789Sahrens 1122789Sahrens tx = dmu_tx_create(os); 1123789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1124789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 11251544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1126789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1127789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1128789Sahrens 0, SPA_MAXBLOCKSIZE); 1129789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1130789Sahrens if (error) { 1131789Sahrens zfs_dirent_unlock(dl); 1132789Sahrens if (error == ERESTART && 1133789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 11342113Sahrens dmu_tx_wait(tx); 11352113Sahrens dmu_tx_abort(tx); 1136789Sahrens goto top; 1137789Sahrens } 11382113Sahrens dmu_tx_abort(tx); 1139789Sahrens ZFS_EXIT(zfsvfs); 1140789Sahrens return (error); 1141789Sahrens } 1142789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1143789Sahrens ASSERT(zp->z_id == zoid); 1144789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1145789Sahrens seq = zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1146789Sahrens dmu_tx_commit(tx); 1147789Sahrens } else { 1148789Sahrens /* 1149789Sahrens * A directory entry already exists for this name. 1150789Sahrens */ 1151789Sahrens /* 1152789Sahrens * Can't truncate an existing file if in exclusive mode. 1153789Sahrens */ 1154789Sahrens if (excl == EXCL) { 1155789Sahrens error = EEXIST; 1156789Sahrens goto out; 1157789Sahrens } 1158789Sahrens /* 1159789Sahrens * Can't open a directory for writing. 1160789Sahrens */ 1161789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1162789Sahrens error = EISDIR; 1163789Sahrens goto out; 1164789Sahrens } 1165789Sahrens /* 1166789Sahrens * Verify requested access to file. 1167789Sahrens */ 1168789Sahrens if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1169789Sahrens goto out; 1170789Sahrens } 1171789Sahrens 1172789Sahrens mutex_enter(&dzp->z_lock); 1173789Sahrens dzp->z_seq++; 1174789Sahrens mutex_exit(&dzp->z_lock); 1175789Sahrens 11761878Smaybee /* 11771878Smaybee * Truncate regular files if requested. 11781878Smaybee */ 11791878Smaybee if ((ZTOV(zp)->v_type == VREG) && 11801878Smaybee (zp->z_phys->zp_size != 0) && 1181789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 11821878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 11831878Smaybee if (error == ERESTART && 11841878Smaybee zfsvfs->z_assign == TXG_NOWAIT) { 11852113Sahrens /* NB: we already did dmu_tx_wait() */ 11861878Smaybee zfs_dirent_unlock(dl); 11872365Sperrin VN_RELE(ZTOV(zp)); 11881878Smaybee goto top; 1189789Sahrens } 1190789Sahrens } 1191789Sahrens } 1192789Sahrens out: 1193789Sahrens 1194789Sahrens if (dl) 1195789Sahrens zfs_dirent_unlock(dl); 1196789Sahrens 1197789Sahrens if (error) { 1198789Sahrens if (zp) 1199789Sahrens VN_RELE(ZTOV(zp)); 1200789Sahrens } else { 1201789Sahrens *vpp = ZTOV(zp); 1202789Sahrens /* 1203789Sahrens * If vnode is for a device return a specfs vnode instead. 1204789Sahrens */ 1205789Sahrens if (IS_DEVVP(*vpp)) { 1206789Sahrens struct vnode *svp; 1207789Sahrens 1208789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1209789Sahrens VN_RELE(*vpp); 1210789Sahrens if (svp == NULL) { 1211789Sahrens error = ENOSYS; 1212789Sahrens } 1213789Sahrens *vpp = svp; 1214789Sahrens } 1215789Sahrens } 1216789Sahrens 1217789Sahrens zil_commit(zilog, seq, 0); 1218789Sahrens 1219789Sahrens ZFS_EXIT(zfsvfs); 1220789Sahrens return (error); 1221789Sahrens } 1222789Sahrens 1223789Sahrens /* 1224789Sahrens * Remove an entry from a directory. 1225789Sahrens * 1226789Sahrens * IN: dvp - vnode of directory to remove entry from. 1227789Sahrens * name - name of entry to remove. 1228789Sahrens * cr - credentials of caller. 1229789Sahrens * 1230789Sahrens * RETURN: 0 if success 1231789Sahrens * error code if failure 1232789Sahrens * 1233789Sahrens * Timestamps: 1234789Sahrens * dvp - ctime|mtime 1235789Sahrens * vp - ctime (if nlink > 0) 1236789Sahrens */ 1237789Sahrens static int 1238789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1239789Sahrens { 1240789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1241789Sahrens znode_t *xzp = NULL; 1242789Sahrens vnode_t *vp; 1243789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1244789Sahrens zilog_t *zilog = zfsvfs->z_log; 1245789Sahrens uint64_t seq = 0; 1246789Sahrens uint64_t acl_obj, xattr_obj; 1247789Sahrens zfs_dirlock_t *dl; 1248789Sahrens dmu_tx_t *tx; 1249789Sahrens int may_delete_now, delete_now = FALSE; 1250789Sahrens int reaped; 1251789Sahrens int error; 1252789Sahrens 1253789Sahrens ZFS_ENTER(zfsvfs); 1254789Sahrens 1255789Sahrens top: 1256789Sahrens /* 1257789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1258789Sahrens */ 1259789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1260789Sahrens ZFS_EXIT(zfsvfs); 1261789Sahrens return (error); 1262789Sahrens } 1263789Sahrens 1264789Sahrens vp = ZTOV(zp); 1265789Sahrens 1266789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1267789Sahrens goto out; 1268789Sahrens } 1269789Sahrens 1270789Sahrens /* 1271789Sahrens * Need to use rmdir for removing directories. 1272789Sahrens */ 1273789Sahrens if (vp->v_type == VDIR) { 1274789Sahrens error = EPERM; 1275789Sahrens goto out; 1276789Sahrens } 1277789Sahrens 1278789Sahrens vnevent_remove(vp); 1279789Sahrens 12801484Sek110237 dnlc_remove(dvp, name); 12811484Sek110237 1282789Sahrens mutex_enter(&vp->v_lock); 1283789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1284789Sahrens mutex_exit(&vp->v_lock); 1285789Sahrens 1286789Sahrens /* 1287789Sahrens * We may delete the znode now, or we may put it on the delete queue; 1288789Sahrens * it depends on whether we're the last link, and on whether there are 1289789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1290789Sahrens * allow for either case. 1291789Sahrens */ 1292789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 12931544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1294789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 1295789Sahrens if (may_delete_now) 1296789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1297789Sahrens 1298789Sahrens /* are there any extended attributes? */ 1299789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1300789Sahrens /* 1301789Sahrens * XXX - There is a possibility that the delete 1302789Sahrens * of the parent file could succeed, but then we get 1303789Sahrens * an ENOSPC when we try to delete the xattrs... 1304789Sahrens * so we would need to re-try the deletes periodically 1305789Sahrens */ 1306789Sahrens /* XXX - do we need this if we are deleting? */ 1307789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1308789Sahrens } 1309789Sahrens 1310789Sahrens /* are there any additional acls */ 1311789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1312789Sahrens may_delete_now) 1313789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1314789Sahrens 1315789Sahrens /* charge as an update -- would be nice not to charge at all */ 13161544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1317789Sahrens 1318789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1319789Sahrens if (error) { 1320789Sahrens zfs_dirent_unlock(dl); 1321789Sahrens VN_RELE(vp); 1322789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 13232113Sahrens dmu_tx_wait(tx); 13242113Sahrens dmu_tx_abort(tx); 1325789Sahrens goto top; 1326789Sahrens } 13272113Sahrens dmu_tx_abort(tx); 1328789Sahrens ZFS_EXIT(zfsvfs); 1329789Sahrens return (error); 1330789Sahrens } 1331789Sahrens 1332789Sahrens /* 1333789Sahrens * Remove the directory entry. 1334789Sahrens */ 1335789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, &reaped); 1336789Sahrens 1337789Sahrens if (error) { 1338789Sahrens dmu_tx_commit(tx); 1339789Sahrens goto out; 1340789Sahrens } 1341789Sahrens 1342789Sahrens if (reaped) { 1343789Sahrens mutex_enter(&vp->v_lock); 1344789Sahrens delete_now = may_delete_now && 1345789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1346789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1347789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1348789Sahrens mutex_exit(&vp->v_lock); 1349789Sahrens } 1350789Sahrens 1351789Sahrens if (delete_now) { 1352789Sahrens if (zp->z_phys->zp_xattr) { 1353789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1354789Sahrens ASSERT3U(error, ==, 0); 1355789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1356789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1357789Sahrens mutex_enter(&xzp->z_lock); 1358789Sahrens xzp->z_reap = 1; 1359789Sahrens xzp->z_phys->zp_links = 0; 1360789Sahrens mutex_exit(&xzp->z_lock); 1361789Sahrens zfs_dq_add(xzp, tx); 1362789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1363789Sahrens } 1364789Sahrens mutex_enter(&zp->z_lock); 1365789Sahrens mutex_enter(&vp->v_lock); 1366789Sahrens vp->v_count--; 1367789Sahrens ASSERT3U(vp->v_count, ==, 0); 1368789Sahrens mutex_exit(&vp->v_lock); 1369789Sahrens zp->z_active = 0; 1370789Sahrens mutex_exit(&zp->z_lock); 1371789Sahrens zfs_znode_delete(zp, tx); 1372789Sahrens VFS_RELE(zfsvfs->z_vfs); 1373789Sahrens } else if (reaped) { 1374789Sahrens zfs_dq_add(zp, tx); 1375789Sahrens } 1376789Sahrens 1377789Sahrens seq = zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1378789Sahrens 1379789Sahrens dmu_tx_commit(tx); 1380789Sahrens out: 1381789Sahrens zfs_dirent_unlock(dl); 1382789Sahrens 1383789Sahrens if (!delete_now) { 1384789Sahrens VN_RELE(vp); 1385789Sahrens } else if (xzp) { 1386789Sahrens /* this rele delayed to prevent nesting transactions */ 1387789Sahrens VN_RELE(ZTOV(xzp)); 1388789Sahrens } 1389789Sahrens 1390789Sahrens zil_commit(zilog, seq, 0); 1391789Sahrens 1392789Sahrens ZFS_EXIT(zfsvfs); 1393789Sahrens return (error); 1394789Sahrens } 1395789Sahrens 1396789Sahrens /* 1397789Sahrens * Create a new directory and insert it into dvp using the name 1398789Sahrens * provided. Return a pointer to the inserted directory. 1399789Sahrens * 1400789Sahrens * IN: dvp - vnode of directory to add subdir to. 1401789Sahrens * dirname - name of new directory. 1402789Sahrens * vap - attributes of new directory. 1403789Sahrens * cr - credentials of caller. 1404789Sahrens * 1405789Sahrens * OUT: vpp - vnode of created directory. 1406789Sahrens * 1407789Sahrens * RETURN: 0 if success 1408789Sahrens * error code if failure 1409789Sahrens * 1410789Sahrens * Timestamps: 1411789Sahrens * dvp - ctime|mtime updated 1412789Sahrens * vp - ctime|mtime|atime updated 1413789Sahrens */ 1414789Sahrens static int 1415789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1416789Sahrens { 1417789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1418789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1419789Sahrens zilog_t *zilog = zfsvfs->z_log; 1420789Sahrens uint64_t seq = 0; 1421789Sahrens zfs_dirlock_t *dl; 1422789Sahrens uint64_t zoid = 0; 1423789Sahrens dmu_tx_t *tx; 1424789Sahrens int error; 1425789Sahrens 1426789Sahrens ASSERT(vap->va_type == VDIR); 1427789Sahrens 1428789Sahrens ZFS_ENTER(zfsvfs); 1429789Sahrens 1430789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1431789Sahrens ZFS_EXIT(zfsvfs); 1432789Sahrens return (EINVAL); 1433789Sahrens } 1434789Sahrens top: 1435789Sahrens *vpp = NULL; 1436789Sahrens 1437789Sahrens /* 1438789Sahrens * First make sure the new directory doesn't exist. 1439789Sahrens */ 1440789Sahrens if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1441789Sahrens ZFS_EXIT(zfsvfs); 1442789Sahrens return (error); 1443789Sahrens } 1444789Sahrens 14451231Smarks if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 14461231Smarks zfs_dirent_unlock(dl); 14471231Smarks ZFS_EXIT(zfsvfs); 14481231Smarks return (error); 14491231Smarks } 14501231Smarks 1451789Sahrens /* 1452789Sahrens * Add a new entry to the directory. 1453789Sahrens */ 1454789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14551544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 14561544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1457789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1458789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1459789Sahrens 0, SPA_MAXBLOCKSIZE); 1460789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1461789Sahrens if (error) { 1462789Sahrens zfs_dirent_unlock(dl); 1463789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 14642113Sahrens dmu_tx_wait(tx); 14652113Sahrens dmu_tx_abort(tx); 1466789Sahrens goto top; 1467789Sahrens } 14682113Sahrens dmu_tx_abort(tx); 1469789Sahrens ZFS_EXIT(zfsvfs); 1470789Sahrens return (error); 1471789Sahrens } 1472789Sahrens 1473789Sahrens /* 1474789Sahrens * Create new node. 1475789Sahrens */ 1476789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1477789Sahrens 1478789Sahrens /* 1479789Sahrens * Now put new name in parent dir. 1480789Sahrens */ 1481789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1482789Sahrens 1483789Sahrens *vpp = ZTOV(zp); 1484789Sahrens 1485789Sahrens seq = zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1486789Sahrens dmu_tx_commit(tx); 1487789Sahrens 1488789Sahrens zfs_dirent_unlock(dl); 1489789Sahrens 1490789Sahrens zil_commit(zilog, seq, 0); 1491789Sahrens 1492789Sahrens ZFS_EXIT(zfsvfs); 1493789Sahrens return (0); 1494789Sahrens } 1495789Sahrens 1496789Sahrens /* 1497789Sahrens * Remove a directory subdir entry. If the current working 1498789Sahrens * directory is the same as the subdir to be removed, the 1499789Sahrens * remove will fail. 1500789Sahrens * 1501789Sahrens * IN: dvp - vnode of directory to remove from. 1502789Sahrens * name - name of directory to be removed. 1503789Sahrens * cwd - vnode of current working directory. 1504789Sahrens * cr - credentials of caller. 1505789Sahrens * 1506789Sahrens * RETURN: 0 if success 1507789Sahrens * error code if failure 1508789Sahrens * 1509789Sahrens * Timestamps: 1510789Sahrens * dvp - ctime|mtime updated 1511789Sahrens */ 1512789Sahrens static int 1513789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1514789Sahrens { 1515789Sahrens znode_t *dzp = VTOZ(dvp); 1516789Sahrens znode_t *zp; 1517789Sahrens vnode_t *vp; 1518789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1519789Sahrens zilog_t *zilog = zfsvfs->z_log; 1520789Sahrens uint64_t seq = 0; 1521789Sahrens zfs_dirlock_t *dl; 1522789Sahrens dmu_tx_t *tx; 1523789Sahrens int error; 1524789Sahrens 1525789Sahrens ZFS_ENTER(zfsvfs); 1526789Sahrens 1527789Sahrens top: 1528789Sahrens zp = NULL; 1529789Sahrens 1530789Sahrens /* 1531789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1532789Sahrens */ 1533789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1534789Sahrens ZFS_EXIT(zfsvfs); 1535789Sahrens return (error); 1536789Sahrens } 1537789Sahrens 1538789Sahrens vp = ZTOV(zp); 1539789Sahrens 1540789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1541789Sahrens goto out; 1542789Sahrens } 1543789Sahrens 1544789Sahrens if (vp->v_type != VDIR) { 1545789Sahrens error = ENOTDIR; 1546789Sahrens goto out; 1547789Sahrens } 1548789Sahrens 1549789Sahrens if (vp == cwd) { 1550789Sahrens error = EINVAL; 1551789Sahrens goto out; 1552789Sahrens } 1553789Sahrens 1554789Sahrens vnevent_rmdir(vp); 1555789Sahrens 1556789Sahrens /* 1557789Sahrens * Grab a lock on the parent pointer make sure we play well 1558789Sahrens * with the treewalk and directory rename code. 1559789Sahrens */ 1560789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1561789Sahrens 1562789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 15631544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1564789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 15651544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1566789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1567789Sahrens if (error) { 1568789Sahrens rw_exit(&zp->z_parent_lock); 1569789Sahrens zfs_dirent_unlock(dl); 1570789Sahrens VN_RELE(vp); 1571789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 15722113Sahrens dmu_tx_wait(tx); 15732113Sahrens dmu_tx_abort(tx); 1574789Sahrens goto top; 1575789Sahrens } 15762113Sahrens dmu_tx_abort(tx); 1577789Sahrens ZFS_EXIT(zfsvfs); 1578789Sahrens return (error); 1579789Sahrens } 1580789Sahrens 1581789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1582789Sahrens 1583789Sahrens if (error == 0) 1584789Sahrens seq = zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1585789Sahrens 1586789Sahrens dmu_tx_commit(tx); 1587789Sahrens 1588789Sahrens rw_exit(&zp->z_parent_lock); 1589789Sahrens out: 1590789Sahrens zfs_dirent_unlock(dl); 1591789Sahrens 1592789Sahrens VN_RELE(vp); 1593789Sahrens 1594789Sahrens zil_commit(zilog, seq, 0); 1595789Sahrens 1596789Sahrens ZFS_EXIT(zfsvfs); 1597789Sahrens return (error); 1598789Sahrens } 1599789Sahrens 1600789Sahrens /* 1601789Sahrens * Read as many directory entries as will fit into the provided 1602789Sahrens * buffer from the given directory cursor position (specified in 1603789Sahrens * the uio structure. 1604789Sahrens * 1605789Sahrens * IN: vp - vnode of directory to read. 1606789Sahrens * uio - structure supplying read location, range info, 1607789Sahrens * and return buffer. 1608789Sahrens * cr - credentials of caller. 1609789Sahrens * 1610789Sahrens * OUT: uio - updated offset and range, buffer filled. 1611789Sahrens * eofp - set to true if end-of-file detected. 1612789Sahrens * 1613789Sahrens * RETURN: 0 if success 1614789Sahrens * error code if failure 1615789Sahrens * 1616789Sahrens * Timestamps: 1617789Sahrens * vp - atime updated 1618789Sahrens * 1619789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1620789Sahrens * This allows us to use the low range for "special" directory entries: 1621789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1622789Sahrens * we use the offset 2 for the '.zfs' directory. 1623789Sahrens */ 1624789Sahrens /* ARGSUSED */ 1625789Sahrens static int 1626789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1627789Sahrens { 1628789Sahrens znode_t *zp = VTOZ(vp); 1629789Sahrens iovec_t *iovp; 1630789Sahrens dirent64_t *odp; 1631789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1632869Sperrin objset_t *os; 1633789Sahrens caddr_t outbuf; 1634789Sahrens size_t bufsize; 1635789Sahrens zap_cursor_t zc; 1636789Sahrens zap_attribute_t zap; 1637789Sahrens uint_t bytes_wanted; 1638789Sahrens ushort_t this_reclen; 1639789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1640789Sahrens off64_t *next; 1641789Sahrens int local_eof; 1642869Sperrin int outcount; 1643869Sperrin int error; 1644869Sperrin uint8_t prefetch; 1645789Sahrens 1646789Sahrens ZFS_ENTER(zfsvfs); 1647789Sahrens 1648789Sahrens /* 1649789Sahrens * If we are not given an eof variable, 1650789Sahrens * use a local one. 1651789Sahrens */ 1652789Sahrens if (eofp == NULL) 1653789Sahrens eofp = &local_eof; 1654789Sahrens 1655789Sahrens /* 1656789Sahrens * Check for valid iov_len. 1657789Sahrens */ 1658789Sahrens if (uio->uio_iov->iov_len <= 0) { 1659789Sahrens ZFS_EXIT(zfsvfs); 1660789Sahrens return (EINVAL); 1661789Sahrens } 1662789Sahrens 1663789Sahrens /* 1664789Sahrens * Quit if directory has been removed (posix) 1665789Sahrens */ 1666789Sahrens if ((*eofp = zp->z_reap) != 0) { 1667789Sahrens ZFS_EXIT(zfsvfs); 1668789Sahrens return (0); 1669789Sahrens } 1670789Sahrens 1671869Sperrin error = 0; 1672869Sperrin os = zfsvfs->z_os; 1673869Sperrin offset = uio->uio_loffset; 1674869Sperrin prefetch = zp->z_zn_prefetch; 1675869Sperrin 1676789Sahrens /* 1677789Sahrens * Initialize the iterator cursor. 1678789Sahrens */ 1679789Sahrens if (offset <= 3) { 1680789Sahrens /* 1681789Sahrens * Start iteration from the beginning of the directory. 1682789Sahrens */ 1683869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1684789Sahrens } else { 1685789Sahrens /* 1686789Sahrens * The offset is a serialized cursor. 1687789Sahrens */ 1688869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1689789Sahrens } 1690789Sahrens 1691789Sahrens /* 1692789Sahrens * Get space to change directory entries into fs independent format. 1693789Sahrens */ 1694789Sahrens iovp = uio->uio_iov; 1695789Sahrens bytes_wanted = iovp->iov_len; 1696789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1697789Sahrens bufsize = bytes_wanted; 1698789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1699789Sahrens odp = (struct dirent64 *)outbuf; 1700789Sahrens } else { 1701789Sahrens bufsize = bytes_wanted; 1702789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1703789Sahrens } 1704789Sahrens 1705789Sahrens /* 1706789Sahrens * Transform to file-system independent format 1707789Sahrens */ 1708789Sahrens outcount = 0; 1709789Sahrens while (outcount < bytes_wanted) { 1710789Sahrens /* 1711789Sahrens * Special case `.', `..', and `.zfs'. 1712789Sahrens */ 1713789Sahrens if (offset == 0) { 1714789Sahrens (void) strcpy(zap.za_name, "."); 1715789Sahrens zap.za_first_integer = zp->z_id; 1716789Sahrens this_reclen = DIRENT64_RECLEN(1); 1717789Sahrens } else if (offset == 1) { 1718789Sahrens (void) strcpy(zap.za_name, ".."); 1719789Sahrens zap.za_first_integer = zp->z_phys->zp_parent; 1720789Sahrens this_reclen = DIRENT64_RECLEN(2); 1721789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1722789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1723789Sahrens zap.za_first_integer = ZFSCTL_INO_ROOT; 1724789Sahrens this_reclen = 1725789Sahrens DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1); 1726789Sahrens } else { 1727789Sahrens /* 1728789Sahrens * Grab next entry. 1729789Sahrens */ 1730789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1731789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1732789Sahrens break; 1733789Sahrens else 1734789Sahrens goto update; 1735789Sahrens } 1736789Sahrens 1737789Sahrens if (zap.za_integer_length != 8 || 1738789Sahrens zap.za_num_integers != 1) { 1739789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1740789Sahrens "entry, obj = %lld, offset = %lld\n", 1741789Sahrens (u_longlong_t)zp->z_id, 1742789Sahrens (u_longlong_t)offset); 1743789Sahrens error = ENXIO; 1744789Sahrens goto update; 1745789Sahrens } 1746789Sahrens this_reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1747789Sahrens } 1748789Sahrens 1749789Sahrens /* 1750789Sahrens * Will this entry fit in the buffer? 1751789Sahrens */ 1752789Sahrens if (outcount + this_reclen > bufsize) { 1753789Sahrens /* 1754789Sahrens * Did we manage to fit anything in the buffer? 1755789Sahrens */ 1756789Sahrens if (!outcount) { 1757789Sahrens error = EINVAL; 1758789Sahrens goto update; 1759789Sahrens } 1760789Sahrens break; 1761789Sahrens } 1762789Sahrens /* 1763789Sahrens * Add this entry: 1764789Sahrens */ 1765789Sahrens odp->d_ino = (ino64_t)zap.za_first_integer; 1766789Sahrens odp->d_reclen = (ushort_t)this_reclen; 1767789Sahrens /* NOTE: d_off is the offset for the *next* entry */ 1768789Sahrens next = &(odp->d_off); 1769789Sahrens (void) strncpy(odp->d_name, zap.za_name, 1770789Sahrens DIRENT64_NAMELEN(this_reclen)); 1771789Sahrens outcount += this_reclen; 1772789Sahrens odp = (dirent64_t *)((intptr_t)odp + this_reclen); 1773789Sahrens 1774789Sahrens ASSERT(outcount <= bufsize); 1775789Sahrens 1776789Sahrens /* Prefetch znode */ 1777869Sperrin if (prefetch) 1778869Sperrin dmu_prefetch(os, zap.za_first_integer, 0, 0); 1779789Sahrens 1780789Sahrens /* 1781789Sahrens * Move to the next entry, fill in the previous offset. 1782789Sahrens */ 1783789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1784789Sahrens zap_cursor_advance(&zc); 1785789Sahrens offset = zap_cursor_serialize(&zc); 1786789Sahrens } else { 1787789Sahrens offset += 1; 1788789Sahrens } 1789789Sahrens *next = offset; 1790789Sahrens } 1791869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1792789Sahrens 1793789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1794789Sahrens iovp->iov_base += outcount; 1795789Sahrens iovp->iov_len -= outcount; 1796789Sahrens uio->uio_resid -= outcount; 1797789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1798789Sahrens /* 1799789Sahrens * Reset the pointer. 1800789Sahrens */ 1801789Sahrens offset = uio->uio_loffset; 1802789Sahrens } 1803789Sahrens 1804789Sahrens update: 1805885Sahrens zap_cursor_fini(&zc); 1806789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1807789Sahrens kmem_free(outbuf, bufsize); 1808789Sahrens 1809789Sahrens if (error == ENOENT) 1810789Sahrens error = 0; 1811789Sahrens 1812789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1813789Sahrens 1814789Sahrens uio->uio_loffset = offset; 1815789Sahrens ZFS_EXIT(zfsvfs); 1816789Sahrens return (error); 1817789Sahrens } 1818789Sahrens 1819789Sahrens static int 1820789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1821789Sahrens { 1822789Sahrens znode_t *zp = VTOZ(vp); 1823789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1824789Sahrens 18251773Seschrock /* 18261773Seschrock * Regardless of whether this is required for standards conformance, 18271773Seschrock * this is the logical behavior when fsync() is called on a file with 18281773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 18291773Seschrock * going to be pushed out as part of the zil_commit(). 18301773Seschrock */ 18311773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 18321773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 18331773Seschrock (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 18341773Seschrock 1835789Sahrens ZFS_ENTER(zfsvfs); 1836789Sahrens zil_commit(zfsvfs->z_log, zp->z_last_itx, FSYNC); 1837789Sahrens ZFS_EXIT(zfsvfs); 1838789Sahrens return (0); 1839789Sahrens } 1840789Sahrens 1841789Sahrens /* 1842789Sahrens * Get the requested file attributes and place them in the provided 1843789Sahrens * vattr structure. 1844789Sahrens * 1845789Sahrens * IN: vp - vnode of file. 1846789Sahrens * vap - va_mask identifies requested attributes. 1847789Sahrens * flags - [UNUSED] 1848789Sahrens * cr - credentials of caller. 1849789Sahrens * 1850789Sahrens * OUT: vap - attribute values. 1851789Sahrens * 1852789Sahrens * RETURN: 0 (always succeeds) 1853789Sahrens */ 1854789Sahrens /* ARGSUSED */ 1855789Sahrens static int 1856789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1857789Sahrens { 1858789Sahrens znode_t *zp = VTOZ(vp); 1859789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1860789Sahrens znode_phys_t *pzp = zp->z_phys; 1861789Sahrens int error; 1862789Sahrens 1863789Sahrens ZFS_ENTER(zfsvfs); 1864789Sahrens 1865789Sahrens /* 1866789Sahrens * Return all attributes. It's cheaper to provide the answer 1867789Sahrens * than to determine whether we were asked the question. 1868789Sahrens */ 1869789Sahrens mutex_enter(&zp->z_lock); 1870789Sahrens 1871789Sahrens vap->va_type = vp->v_type; 1872789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 1873789Sahrens vap->va_uid = zp->z_phys->zp_uid; 1874789Sahrens vap->va_gid = zp->z_phys->zp_gid; 1875789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1876789Sahrens vap->va_nodeid = zp->z_id; 1877789Sahrens vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX); /* nlink_t limit! */ 1878789Sahrens vap->va_size = pzp->zp_size; 18791816Smarks vap->va_rdev = vp->v_rdev; 1880789Sahrens vap->va_seq = zp->z_seq; 1881789Sahrens 1882789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1883789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1884789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1885789Sahrens 1886789Sahrens /* 1887905Smarks * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1888905Smarks * Also, if we are the owner don't bother, since owner should 1889905Smarks * always be allowed to read basic attributes of file. 1890789Sahrens */ 1891905Smarks if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1892905Smarks (zp->z_phys->zp_uid != crgetuid(cr))) { 1893905Smarks if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1894789Sahrens mutex_exit(&zp->z_lock); 1895789Sahrens ZFS_EXIT(zfsvfs); 1896789Sahrens return (error); 1897789Sahrens } 1898789Sahrens } 1899789Sahrens 1900789Sahrens mutex_exit(&zp->z_lock); 1901789Sahrens 1902789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1903789Sahrens 1904789Sahrens if (zp->z_blksz == 0) { 1905789Sahrens /* 1906789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 1907789Sahrens */ 1908789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 1909789Sahrens } 1910789Sahrens 1911789Sahrens ZFS_EXIT(zfsvfs); 1912789Sahrens return (0); 1913789Sahrens } 1914789Sahrens 1915789Sahrens /* 1916789Sahrens * Set the file attributes to the values contained in the 1917789Sahrens * vattr structure. 1918789Sahrens * 1919789Sahrens * IN: vp - vnode of file to be modified. 1920789Sahrens * vap - new attribute values. 1921789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 1922789Sahrens * cr - credentials of caller. 1923789Sahrens * 1924789Sahrens * RETURN: 0 if success 1925789Sahrens * error code if failure 1926789Sahrens * 1927789Sahrens * Timestamps: 1928789Sahrens * vp - ctime updated, mtime updated if size changed. 1929789Sahrens */ 1930789Sahrens /* ARGSUSED */ 1931789Sahrens static int 1932789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1933789Sahrens caller_context_t *ct) 1934789Sahrens { 1935789Sahrens struct znode *zp = VTOZ(vp); 1936789Sahrens znode_phys_t *pzp = zp->z_phys; 1937789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1938789Sahrens zilog_t *zilog = zfsvfs->z_log; 1939789Sahrens uint64_t seq = 0; 1940789Sahrens dmu_tx_t *tx; 19411878Smaybee vattr_t oldva; 1942789Sahrens uint_t mask = vap->va_mask; 19431878Smaybee uint_t saved_mask; 19441115Smarks int trim_mask = FALSE; 1945789Sahrens uint64_t new_mode; 19461231Smarks znode_t *attrzp; 1947789Sahrens int need_policy = FALSE; 1948789Sahrens int err; 1949789Sahrens 1950789Sahrens if (mask == 0) 1951789Sahrens return (0); 1952789Sahrens 1953789Sahrens if (mask & AT_NOSET) 1954789Sahrens return (EINVAL); 1955789Sahrens 1956789Sahrens if (mask & AT_SIZE && vp->v_type == VDIR) 1957789Sahrens return (EISDIR); 1958789Sahrens 19591394Smarks if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 19601308Smarks return (EINVAL); 19611308Smarks 1962789Sahrens ZFS_ENTER(zfsvfs); 1963789Sahrens 1964789Sahrens top: 19651231Smarks attrzp = NULL; 1966789Sahrens 1967789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1968789Sahrens ZFS_EXIT(zfsvfs); 1969789Sahrens return (EROFS); 1970789Sahrens } 1971789Sahrens 1972789Sahrens /* 1973789Sahrens * First validate permissions 1974789Sahrens */ 1975789Sahrens 1976789Sahrens if (mask & AT_SIZE) { 1977789Sahrens err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1978789Sahrens if (err) { 1979789Sahrens ZFS_EXIT(zfsvfs); 1980789Sahrens return (err); 1981789Sahrens } 19821878Smaybee /* 19831878Smaybee * XXX - Note, we are not providing any open 19841878Smaybee * mode flags here (like FNDELAY), so we may 19851878Smaybee * block if there are locks present... this 19861878Smaybee * should be addressed in openat(). 19871878Smaybee */ 19881878Smaybee do { 19891878Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 19902113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 19911878Smaybee } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 19921878Smaybee if (err) { 19931878Smaybee ZFS_EXIT(zfsvfs); 19941878Smaybee return (err); 19951878Smaybee } 1996789Sahrens } 1997789Sahrens 1998789Sahrens if (mask & (AT_ATIME|AT_MTIME)) 1999789Sahrens need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 2000789Sahrens 2001789Sahrens if (mask & (AT_UID|AT_GID)) { 2002789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 2003789Sahrens int take_owner; 2004789Sahrens int take_group; 2005789Sahrens 2006789Sahrens /* 2007913Smarks * NOTE: even if a new mode is being set, 2008913Smarks * we may clear S_ISUID/S_ISGID bits. 2009913Smarks */ 2010913Smarks 2011913Smarks if (!(mask & AT_MODE)) 2012913Smarks vap->va_mode = pzp->zp_mode; 2013913Smarks 2014913Smarks /* 2015789Sahrens * Take ownership or chgrp to group we are a member of 2016789Sahrens */ 2017789Sahrens 2018789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 2019789Sahrens take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 2020789Sahrens 2021789Sahrens /* 2022789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2023789Sahrens * take_group must both be set in order to allow taking 2024789Sahrens * ownership. 2025789Sahrens * 2026789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2027789Sahrens * 2028789Sahrens */ 2029789Sahrens 2030789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2031789Sahrens ((idmask == AT_UID) && take_owner) || 2032789Sahrens ((idmask == AT_GID) && take_group)) { 2033789Sahrens if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 2034789Sahrens /* 2035789Sahrens * Remove setuid/setgid for non-privileged users 2036789Sahrens */ 20371115Smarks secpolicy_setid_clear(vap, cr); 20381115Smarks trim_mask = TRUE; 20391115Smarks saved_mask = vap->va_mask; 2040789Sahrens } else { 2041789Sahrens need_policy = TRUE; 2042789Sahrens } 2043789Sahrens } else { 2044789Sahrens need_policy = TRUE; 2045789Sahrens } 2046789Sahrens } 2047789Sahrens 2048789Sahrens if (mask & AT_MODE) 2049789Sahrens need_policy = TRUE; 2050789Sahrens 2051789Sahrens if (need_policy) { 2052789Sahrens mutex_enter(&zp->z_lock); 2053789Sahrens oldva.va_mode = pzp->zp_mode; 2054789Sahrens oldva.va_uid = zp->z_phys->zp_uid; 2055789Sahrens oldva.va_gid = zp->z_phys->zp_gid; 2056789Sahrens mutex_exit(&zp->z_lock); 20571115Smarks 20581115Smarks /* 20591115Smarks * If trim_mask is set then take ownership 20601115Smarks * has been granted. In that case remove 20611115Smarks * UID|GID from mask so that 20621115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 20631115Smarks */ 20641115Smarks if (trim_mask) 20651115Smarks vap->va_mask &= ~(AT_UID|AT_GID); 20661115Smarks 2067789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2068789Sahrens (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2069789Sahrens if (err) { 2070789Sahrens ZFS_EXIT(zfsvfs); 2071789Sahrens return (err); 2072789Sahrens } 20731115Smarks 20741115Smarks if (trim_mask) 20751115Smarks vap->va_mask |= (saved_mask & (AT_UID|AT_GID)); 2076789Sahrens } 2077789Sahrens 2078789Sahrens /* 2079789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2080789Sahrens * changed va_mask 2081789Sahrens */ 2082789Sahrens mask = vap->va_mask; 2083789Sahrens 2084789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2085789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2086789Sahrens 2087789Sahrens if (mask & AT_MODE) { 20881576Smarks uint64_t pmode = pzp->zp_mode; 20891576Smarks 20901576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2091789Sahrens 2092789Sahrens if (zp->z_phys->zp_acl.z_acl_extern_obj) 2093789Sahrens dmu_tx_hold_write(tx, 2094789Sahrens pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2095789Sahrens else 2096789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2097789Sahrens 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2098789Sahrens } 2099789Sahrens 21001231Smarks if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 21011231Smarks err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 21021231Smarks if (err) { 21031231Smarks dmu_tx_abort(tx); 21041231Smarks ZFS_EXIT(zfsvfs); 21051231Smarks return (err); 21061231Smarks } 21071231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 21081231Smarks } 21091231Smarks 2110789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2111789Sahrens if (err) { 21121231Smarks if (attrzp) 21131231Smarks VN_RELE(ZTOV(attrzp)); 2114789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 21152113Sahrens dmu_tx_wait(tx); 21162113Sahrens dmu_tx_abort(tx); 2117789Sahrens goto top; 2118789Sahrens } 21192113Sahrens dmu_tx_abort(tx); 2120789Sahrens ZFS_EXIT(zfsvfs); 2121789Sahrens return (err); 2122789Sahrens } 2123789Sahrens 2124789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2125789Sahrens 2126789Sahrens /* 2127789Sahrens * Set each attribute requested. 2128789Sahrens * We group settings according to the locks they need to acquire. 2129789Sahrens * 2130789Sahrens * Note: you cannot set ctime directly, although it will be 2131789Sahrens * updated as a side-effect of calling this function. 2132789Sahrens */ 2133789Sahrens 2134789Sahrens mutex_enter(&zp->z_lock); 2135789Sahrens 2136789Sahrens if (mask & AT_MODE) { 2137789Sahrens err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2138789Sahrens ASSERT3U(err, ==, 0); 2139789Sahrens } 2140789Sahrens 21411231Smarks if (attrzp) 21421231Smarks mutex_enter(&attrzp->z_lock); 21431231Smarks 21441231Smarks if (mask & AT_UID) { 2145789Sahrens zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21461231Smarks if (attrzp) { 21471231Smarks attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21481231Smarks } 21491231Smarks } 21501231Smarks 21511231Smarks if (mask & AT_GID) { 2152789Sahrens zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21531231Smarks if (attrzp) 21541231Smarks attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21551231Smarks } 21561231Smarks 21571231Smarks if (attrzp) 21581231Smarks mutex_exit(&attrzp->z_lock); 2159789Sahrens 2160789Sahrens if (mask & AT_ATIME) 2161789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2162789Sahrens 2163789Sahrens if (mask & AT_MTIME) 2164789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2165789Sahrens 21661878Smaybee if (mask & AT_SIZE) 2167789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 21681878Smaybee else if (mask != 0) 2169789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2170789Sahrens 21711878Smaybee if (mask != 0) 21721878Smaybee seq = zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2173789Sahrens 2174789Sahrens mutex_exit(&zp->z_lock); 2175789Sahrens 21761231Smarks if (attrzp) 21771231Smarks VN_RELE(ZTOV(attrzp)); 21781231Smarks 2179789Sahrens dmu_tx_commit(tx); 2180789Sahrens 2181789Sahrens zil_commit(zilog, seq, 0); 2182789Sahrens 2183789Sahrens ZFS_EXIT(zfsvfs); 2184789Sahrens return (err); 2185789Sahrens } 2186789Sahrens 2187789Sahrens /* 2188789Sahrens * Search back through the directory tree, using the ".." entries. 2189789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2190789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2191789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2192789Sahrens */ 2193789Sahrens typedef struct zfs_zlock { 2194789Sahrens krwlock_t *zl_rwlock; /* lock we acquired */ 2195789Sahrens znode_t *zl_znode; /* znode we held */ 2196789Sahrens struct zfs_zlock *zl_next; /* next in list */ 2197789Sahrens } zfs_zlock_t; 2198789Sahrens 2199789Sahrens static int 2200789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2201789Sahrens { 2202789Sahrens zfs_zlock_t *zl; 2203789Sahrens znode_t *zp = tdzp; 2204789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2205789Sahrens uint64_t *oidp = &zp->z_id; 2206789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2207789Sahrens krw_t rw = RW_WRITER; 2208789Sahrens 2209789Sahrens /* 2210789Sahrens * First pass write-locks szp and compares to zp->z_id. 2211789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2212789Sahrens */ 2213789Sahrens do { 2214789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2215789Sahrens zl->zl_rwlock = rwlp; 2216789Sahrens zl->zl_znode = NULL; 2217789Sahrens zl->zl_next = *zlpp; 2218789Sahrens *zlpp = zl; 2219789Sahrens 2220789Sahrens rw_enter(rwlp, rw); 2221789Sahrens 2222789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2223789Sahrens return (EINVAL); 2224789Sahrens 2225789Sahrens if (*oidp == rootid) /* We've hit the top */ 2226789Sahrens return (0); 2227789Sahrens 2228789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2229789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2230789Sahrens if (error) 2231789Sahrens return (error); 2232789Sahrens zl->zl_znode = zp; 2233789Sahrens } 2234789Sahrens oidp = &zp->z_phys->zp_parent; 2235789Sahrens rwlp = &zp->z_parent_lock; 2236789Sahrens rw = RW_READER; 2237789Sahrens 2238789Sahrens } while (zp->z_id != sdzp->z_id); 2239789Sahrens 2240789Sahrens return (0); 2241789Sahrens } 2242789Sahrens 2243789Sahrens /* 2244789Sahrens * Drop locks and release vnodes that were held by zfs_rename_lock(). 2245789Sahrens */ 2246789Sahrens static void 2247789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp) 2248789Sahrens { 2249789Sahrens zfs_zlock_t *zl; 2250789Sahrens 2251789Sahrens while ((zl = *zlpp) != NULL) { 2252789Sahrens if (zl->zl_znode != NULL) 2253789Sahrens VN_RELE(ZTOV(zl->zl_znode)); 2254789Sahrens rw_exit(zl->zl_rwlock); 2255789Sahrens *zlpp = zl->zl_next; 2256789Sahrens kmem_free(zl, sizeof (*zl)); 2257789Sahrens } 2258789Sahrens } 2259789Sahrens 2260789Sahrens /* 2261789Sahrens * Move an entry from the provided source directory to the target 2262789Sahrens * directory. Change the entry name as indicated. 2263789Sahrens * 2264789Sahrens * IN: sdvp - Source directory containing the "old entry". 2265789Sahrens * snm - Old entry name. 2266789Sahrens * tdvp - Target directory to contain the "new entry". 2267789Sahrens * tnm - New entry name. 2268789Sahrens * cr - credentials of caller. 2269789Sahrens * 2270789Sahrens * RETURN: 0 if success 2271789Sahrens * error code if failure 2272789Sahrens * 2273789Sahrens * Timestamps: 2274789Sahrens * sdvp,tdvp - ctime|mtime updated 2275789Sahrens */ 2276789Sahrens static int 2277789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2278789Sahrens { 2279789Sahrens znode_t *tdzp, *szp, *tzp; 2280789Sahrens znode_t *sdzp = VTOZ(sdvp); 2281789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2282789Sahrens zilog_t *zilog = zfsvfs->z_log; 2283789Sahrens uint64_t seq = 0; 2284789Sahrens vnode_t *realvp; 2285789Sahrens zfs_dirlock_t *sdl, *tdl; 2286789Sahrens dmu_tx_t *tx; 2287789Sahrens zfs_zlock_t *zl; 2288789Sahrens int cmp, serr, terr, error; 2289789Sahrens 2290789Sahrens ZFS_ENTER(zfsvfs); 2291789Sahrens 2292789Sahrens /* 2293789Sahrens * Make sure we have the real vp for the target directory. 2294789Sahrens */ 2295789Sahrens if (VOP_REALVP(tdvp, &realvp) == 0) 2296789Sahrens tdvp = realvp; 2297789Sahrens 2298789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2299789Sahrens ZFS_EXIT(zfsvfs); 2300789Sahrens return (EXDEV); 2301789Sahrens } 2302789Sahrens 2303789Sahrens tdzp = VTOZ(tdvp); 2304789Sahrens top: 2305789Sahrens szp = NULL; 2306789Sahrens tzp = NULL; 2307789Sahrens zl = NULL; 2308789Sahrens 2309789Sahrens /* 2310789Sahrens * This is to prevent the creation of links into attribute space 2311789Sahrens * by renaming a linked file into/outof an attribute directory. 2312789Sahrens * See the comment in zfs_link() for why this is considered bad. 2313789Sahrens */ 2314789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2315789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2316789Sahrens ZFS_EXIT(zfsvfs); 2317789Sahrens return (EINVAL); 2318789Sahrens } 2319789Sahrens 2320789Sahrens /* 2321789Sahrens * Lock source and target directory entries. To prevent deadlock, 2322789Sahrens * a lock ordering must be defined. We lock the directory with 2323789Sahrens * the smallest object id first, or if it's a tie, the one with 2324789Sahrens * the lexically first name. 2325789Sahrens */ 2326789Sahrens if (sdzp->z_id < tdzp->z_id) { 2327789Sahrens cmp = -1; 2328789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2329789Sahrens cmp = 1; 2330789Sahrens } else { 2331789Sahrens cmp = strcmp(snm, tnm); 2332789Sahrens if (cmp == 0) { 2333789Sahrens /* 2334789Sahrens * POSIX: "If the old argument and the new argument 2335789Sahrens * both refer to links to the same existing file, 2336789Sahrens * the rename() function shall return successfully 2337789Sahrens * and perform no other action." 2338789Sahrens */ 2339789Sahrens ZFS_EXIT(zfsvfs); 2340789Sahrens return (0); 2341789Sahrens } 2342789Sahrens } 2343789Sahrens if (cmp < 0) { 2344789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2345789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2346789Sahrens } else { 2347789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2348789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2349789Sahrens } 2350789Sahrens 2351789Sahrens if (serr) { 2352789Sahrens /* 2353789Sahrens * Source entry invalid or not there. 2354789Sahrens */ 2355789Sahrens if (!terr) { 2356789Sahrens zfs_dirent_unlock(tdl); 2357789Sahrens if (tzp) 2358789Sahrens VN_RELE(ZTOV(tzp)); 2359789Sahrens } 2360789Sahrens if (strcmp(snm, "..") == 0) 2361789Sahrens serr = EINVAL; 2362789Sahrens ZFS_EXIT(zfsvfs); 2363789Sahrens return (serr); 2364789Sahrens } 2365789Sahrens if (terr) { 2366789Sahrens zfs_dirent_unlock(sdl); 2367789Sahrens VN_RELE(ZTOV(szp)); 2368789Sahrens if (strcmp(tnm, "..") == 0) 2369789Sahrens terr = EINVAL; 2370789Sahrens ZFS_EXIT(zfsvfs); 2371789Sahrens return (terr); 2372789Sahrens } 2373789Sahrens 2374789Sahrens /* 2375789Sahrens * Must have write access at the source to remove the old entry 2376789Sahrens * and write access at the target to create the new entry. 2377789Sahrens * Note that if target and source are the same, this can be 2378789Sahrens * done in a single check. 2379789Sahrens */ 2380789Sahrens 2381789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2382789Sahrens goto out; 2383789Sahrens 2384789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2385789Sahrens /* 2386789Sahrens * Check to make sure rename is valid. 2387789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2388789Sahrens */ 2389789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2390789Sahrens goto out; 2391789Sahrens } 2392789Sahrens 2393789Sahrens /* 2394789Sahrens * Does target exist? 2395789Sahrens */ 2396789Sahrens if (tzp) { 2397789Sahrens /* 2398789Sahrens * Source and target must be the same type. 2399789Sahrens */ 2400789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2401789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 2402789Sahrens error = ENOTDIR; 2403789Sahrens goto out; 2404789Sahrens } 2405789Sahrens } else { 2406789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 2407789Sahrens error = EISDIR; 2408789Sahrens goto out; 2409789Sahrens } 2410789Sahrens } 2411789Sahrens /* 2412789Sahrens * POSIX dictates that when the source and target 2413789Sahrens * entries refer to the same file object, rename 2414789Sahrens * must do nothing and exit without error. 2415789Sahrens */ 2416789Sahrens if (szp->z_id == tzp->z_id) { 2417789Sahrens error = 0; 2418789Sahrens goto out; 2419789Sahrens } 2420789Sahrens } 2421789Sahrens 2422789Sahrens vnevent_rename_src(ZTOV(szp)); 2423789Sahrens if (tzp) 2424789Sahrens vnevent_rename_dest(ZTOV(tzp)); 2425789Sahrens 2426789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2427789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2428789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 24291544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 24301544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 24311544Seschrock if (sdzp != tdzp) 2432789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 24331544Seschrock if (tzp) 24341544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 24351544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 2436789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2437789Sahrens if (error) { 2438789Sahrens if (zl != NULL) 2439789Sahrens zfs_rename_unlock(&zl); 2440789Sahrens zfs_dirent_unlock(sdl); 2441789Sahrens zfs_dirent_unlock(tdl); 2442789Sahrens VN_RELE(ZTOV(szp)); 2443789Sahrens if (tzp) 2444789Sahrens VN_RELE(ZTOV(tzp)); 2445789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 24462113Sahrens dmu_tx_wait(tx); 24472113Sahrens dmu_tx_abort(tx); 2448789Sahrens goto top; 2449789Sahrens } 24502113Sahrens dmu_tx_abort(tx); 2451789Sahrens ZFS_EXIT(zfsvfs); 2452789Sahrens return (error); 2453789Sahrens } 2454789Sahrens 2455789Sahrens if (tzp) /* Attempt to remove the existing target */ 2456789Sahrens error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2457789Sahrens 2458789Sahrens if (error == 0) { 2459789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2460789Sahrens if (error == 0) { 2461789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2462789Sahrens ASSERT(error == 0); 2463789Sahrens seq = zfs_log_rename(zilog, tx, TX_RENAME, 2464789Sahrens sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 2465789Sahrens } 2466789Sahrens } 2467789Sahrens 2468789Sahrens dmu_tx_commit(tx); 2469789Sahrens out: 2470789Sahrens if (zl != NULL) 2471789Sahrens zfs_rename_unlock(&zl); 2472789Sahrens 2473789Sahrens zfs_dirent_unlock(sdl); 2474789Sahrens zfs_dirent_unlock(tdl); 2475789Sahrens 2476789Sahrens VN_RELE(ZTOV(szp)); 2477789Sahrens if (tzp) 2478789Sahrens VN_RELE(ZTOV(tzp)); 2479789Sahrens 2480789Sahrens zil_commit(zilog, seq, 0); 2481789Sahrens 2482789Sahrens ZFS_EXIT(zfsvfs); 2483789Sahrens return (error); 2484789Sahrens } 2485789Sahrens 2486789Sahrens /* 2487789Sahrens * Insert the indicated symbolic reference entry into the directory. 2488789Sahrens * 2489789Sahrens * IN: dvp - Directory to contain new symbolic link. 2490789Sahrens * link - Name for new symlink entry. 2491789Sahrens * vap - Attributes of new entry. 2492789Sahrens * target - Target path of new symlink. 2493789Sahrens * cr - credentials of caller. 2494789Sahrens * 2495789Sahrens * RETURN: 0 if success 2496789Sahrens * error code if failure 2497789Sahrens * 2498789Sahrens * Timestamps: 2499789Sahrens * dvp - ctime|mtime updated 2500789Sahrens */ 2501789Sahrens static int 2502789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2503789Sahrens { 2504789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 2505789Sahrens zfs_dirlock_t *dl; 2506789Sahrens dmu_tx_t *tx; 2507789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2508789Sahrens zilog_t *zilog = zfsvfs->z_log; 2509789Sahrens uint64_t seq = 0; 2510789Sahrens uint64_t zoid; 2511789Sahrens int len = strlen(link); 2512789Sahrens int error; 2513789Sahrens 2514789Sahrens ASSERT(vap->va_type == VLNK); 2515789Sahrens 2516789Sahrens ZFS_ENTER(zfsvfs); 2517789Sahrens top: 2518789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2519789Sahrens ZFS_EXIT(zfsvfs); 2520789Sahrens return (error); 2521789Sahrens } 2522789Sahrens 2523789Sahrens if (len > MAXPATHLEN) { 2524789Sahrens ZFS_EXIT(zfsvfs); 2525789Sahrens return (ENAMETOOLONG); 2526789Sahrens } 2527789Sahrens 2528789Sahrens /* 2529789Sahrens * Attempt to lock directory; fail if entry already exists. 2530789Sahrens */ 2531789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2532789Sahrens ZFS_EXIT(zfsvfs); 2533789Sahrens return (error); 2534789Sahrens } 2535789Sahrens 2536789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2537789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2538789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 25391544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2540789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2541789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2542789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2543789Sahrens if (error) { 2544789Sahrens zfs_dirent_unlock(dl); 2545789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 25462113Sahrens dmu_tx_wait(tx); 25472113Sahrens dmu_tx_abort(tx); 2548789Sahrens goto top; 2549789Sahrens } 25502113Sahrens dmu_tx_abort(tx); 2551789Sahrens ZFS_EXIT(zfsvfs); 2552789Sahrens return (error); 2553789Sahrens } 2554789Sahrens 2555789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 2556789Sahrens 2557789Sahrens /* 2558789Sahrens * Create a new object for the symlink. 2559789Sahrens * Put the link content into bonus buffer if it will fit; 2560789Sahrens * otherwise, store it just like any other file data. 2561789Sahrens */ 2562789Sahrens zoid = 0; 2563789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2564789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2565789Sahrens if (len != 0) 2566789Sahrens bcopy(link, zp->z_phys + 1, len); 2567789Sahrens } else { 2568789Sahrens dmu_buf_t *dbp; 25691669Sperrin 2570789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2571789Sahrens 25721669Sperrin /* 25731669Sperrin * Nothing can access the znode yet so no locking needed 25741669Sperrin * for growing the znode's blocksize. 25751669Sperrin */ 25761669Sperrin zfs_grow_blocksize(zp, len, tx); 2577789Sahrens 25781544Seschrock VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2579789Sahrens dmu_buf_will_dirty(dbp, tx); 2580789Sahrens 2581789Sahrens ASSERT3U(len, <=, dbp->db_size); 2582789Sahrens bcopy(link, dbp->db_data, len); 25831544Seschrock dmu_buf_rele(dbp, FTAG); 2584789Sahrens } 2585789Sahrens zp->z_phys->zp_size = len; 2586789Sahrens 2587789Sahrens /* 2588789Sahrens * Insert the new object into the directory. 2589789Sahrens */ 2590789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 2591789Sahrens out: 2592789Sahrens if (error == 0) 2593789Sahrens seq = zfs_log_symlink(zilog, tx, TX_SYMLINK, 2594789Sahrens dzp, zp, name, link); 2595789Sahrens 2596789Sahrens dmu_tx_commit(tx); 2597789Sahrens 2598789Sahrens zfs_dirent_unlock(dl); 2599789Sahrens 2600789Sahrens VN_RELE(ZTOV(zp)); 2601789Sahrens 2602789Sahrens zil_commit(zilog, seq, 0); 2603789Sahrens 2604789Sahrens ZFS_EXIT(zfsvfs); 2605789Sahrens return (error); 2606789Sahrens } 2607789Sahrens 2608789Sahrens /* 2609789Sahrens * Return, in the buffer contained in the provided uio structure, 2610789Sahrens * the symbolic path referred to by vp. 2611789Sahrens * 2612789Sahrens * IN: vp - vnode of symbolic link. 2613789Sahrens * uoip - structure to contain the link path. 2614789Sahrens * cr - credentials of caller. 2615789Sahrens * 2616789Sahrens * OUT: uio - structure to contain the link path. 2617789Sahrens * 2618789Sahrens * RETURN: 0 if success 2619789Sahrens * error code if failure 2620789Sahrens * 2621789Sahrens * Timestamps: 2622789Sahrens * vp - atime updated 2623789Sahrens */ 2624789Sahrens /* ARGSUSED */ 2625789Sahrens static int 2626789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2627789Sahrens { 2628789Sahrens znode_t *zp = VTOZ(vp); 2629789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2630789Sahrens size_t bufsz; 2631789Sahrens int error; 2632789Sahrens 2633789Sahrens ZFS_ENTER(zfsvfs); 2634789Sahrens 2635789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 2636789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2637789Sahrens error = uiomove(zp->z_phys + 1, 2638789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2639789Sahrens } else { 26401544Seschrock dmu_buf_t *dbp; 26411544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 26421544Seschrock if (error) { 2643789Sahrens ZFS_EXIT(zfsvfs); 2644789Sahrens return (error); 2645789Sahrens } 2646789Sahrens error = uiomove(dbp->db_data, 2647789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 26481544Seschrock dmu_buf_rele(dbp, FTAG); 2649789Sahrens } 2650789Sahrens 2651789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2652789Sahrens ZFS_EXIT(zfsvfs); 2653789Sahrens return (error); 2654789Sahrens } 2655789Sahrens 2656789Sahrens /* 2657789Sahrens * Insert a new entry into directory tdvp referencing svp. 2658789Sahrens * 2659789Sahrens * IN: tdvp - Directory to contain new entry. 2660789Sahrens * svp - vnode of new entry. 2661789Sahrens * name - name of new entry. 2662789Sahrens * cr - credentials of caller. 2663789Sahrens * 2664789Sahrens * RETURN: 0 if success 2665789Sahrens * error code if failure 2666789Sahrens * 2667789Sahrens * Timestamps: 2668789Sahrens * tdvp - ctime|mtime updated 2669789Sahrens * svp - ctime updated 2670789Sahrens */ 2671789Sahrens /* ARGSUSED */ 2672789Sahrens static int 2673789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2674789Sahrens { 2675789Sahrens znode_t *dzp = VTOZ(tdvp); 2676789Sahrens znode_t *tzp, *szp; 2677789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2678789Sahrens zilog_t *zilog = zfsvfs->z_log; 2679789Sahrens uint64_t seq = 0; 2680789Sahrens zfs_dirlock_t *dl; 2681789Sahrens dmu_tx_t *tx; 2682789Sahrens vnode_t *realvp; 2683789Sahrens int error; 2684789Sahrens 2685789Sahrens ASSERT(tdvp->v_type == VDIR); 2686789Sahrens 2687789Sahrens ZFS_ENTER(zfsvfs); 2688789Sahrens 2689789Sahrens if (VOP_REALVP(svp, &realvp) == 0) 2690789Sahrens svp = realvp; 2691789Sahrens 2692789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 2693789Sahrens ZFS_EXIT(zfsvfs); 2694789Sahrens return (EXDEV); 2695789Sahrens } 2696789Sahrens 2697789Sahrens szp = VTOZ(svp); 2698789Sahrens top: 2699789Sahrens /* 2700789Sahrens * We do not support links between attributes and non-attributes 2701789Sahrens * because of the potential security risk of creating links 2702789Sahrens * into "normal" file space in order to circumvent restrictions 2703789Sahrens * imposed in attribute space. 2704789Sahrens */ 2705789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2706789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2707789Sahrens ZFS_EXIT(zfsvfs); 2708789Sahrens return (EINVAL); 2709789Sahrens } 2710789Sahrens 2711789Sahrens /* 2712789Sahrens * POSIX dictates that we return EPERM here. 2713789Sahrens * Better choices include ENOTSUP or EISDIR. 2714789Sahrens */ 2715789Sahrens if (svp->v_type == VDIR) { 2716789Sahrens ZFS_EXIT(zfsvfs); 2717789Sahrens return (EPERM); 2718789Sahrens } 2719789Sahrens 2720789Sahrens if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2721789Sahrens secpolicy_basic_link(cr) != 0) { 2722789Sahrens ZFS_EXIT(zfsvfs); 2723789Sahrens return (EPERM); 2724789Sahrens } 2725789Sahrens 2726789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2727789Sahrens ZFS_EXIT(zfsvfs); 2728789Sahrens return (error); 2729789Sahrens } 2730789Sahrens 2731789Sahrens /* 2732789Sahrens * Attempt to lock directory; fail if entry already exists. 2733789Sahrens */ 2734789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2735789Sahrens ZFS_EXIT(zfsvfs); 2736789Sahrens return (error); 2737789Sahrens } 2738789Sahrens 2739789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2740789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 27411544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2742789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2743789Sahrens if (error) { 2744789Sahrens zfs_dirent_unlock(dl); 2745789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 27462113Sahrens dmu_tx_wait(tx); 27472113Sahrens dmu_tx_abort(tx); 2748789Sahrens goto top; 2749789Sahrens } 27502113Sahrens dmu_tx_abort(tx); 2751789Sahrens ZFS_EXIT(zfsvfs); 2752789Sahrens return (error); 2753789Sahrens } 2754789Sahrens 2755789Sahrens error = zfs_link_create(dl, szp, tx, 0); 2756789Sahrens 2757789Sahrens if (error == 0) 2758789Sahrens seq = zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2759789Sahrens 2760789Sahrens dmu_tx_commit(tx); 2761789Sahrens 2762789Sahrens zfs_dirent_unlock(dl); 2763789Sahrens 2764789Sahrens zil_commit(zilog, seq, 0); 2765789Sahrens 2766789Sahrens ZFS_EXIT(zfsvfs); 2767789Sahrens return (error); 2768789Sahrens } 2769789Sahrens 2770789Sahrens /* 2771789Sahrens * zfs_null_putapage() is used when the file system has been force 2772789Sahrens * unmounted. It just drops the pages. 2773789Sahrens */ 2774789Sahrens /* ARGSUSED */ 2775789Sahrens static int 2776789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2777789Sahrens size_t *lenp, int flags, cred_t *cr) 2778789Sahrens { 2779789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2780789Sahrens return (0); 2781789Sahrens } 2782789Sahrens 2783789Sahrens /* ARGSUSED */ 2784789Sahrens static int 2785789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2786789Sahrens size_t *lenp, int flags, cred_t *cr) 2787789Sahrens { 2788789Sahrens znode_t *zp = VTOZ(vp); 2789789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2790789Sahrens zilog_t *zilog = zfsvfs->z_log; 2791789Sahrens dmu_tx_t *tx; 27921669Sperrin rl_t *rl; 2793789Sahrens u_offset_t off; 2794789Sahrens ssize_t len; 2795789Sahrens caddr_t va; 2796789Sahrens int err; 2797789Sahrens 2798789Sahrens top: 2799789Sahrens off = pp->p_offset; 28001669Sperrin rl = zfs_range_lock(zp, off, PAGESIZE, RL_WRITER); 28011819Smaybee /* 28021819Smaybee * Can't push pages past end-of-file. 28031819Smaybee */ 28041819Smaybee if (off >= zp->z_phys->zp_size) { 28052237Smaybee zfs_range_unlock(rl); 28061819Smaybee return (EIO); 28071819Smaybee } 2808789Sahrens len = MIN(PAGESIZE, zp->z_phys->zp_size - off); 2809789Sahrens 2810789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2811789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 2812789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2813789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2814789Sahrens if (err != 0) { 28152237Smaybee zfs_range_unlock(rl); 2816789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 28172113Sahrens dmu_tx_wait(tx); 28182113Sahrens dmu_tx_abort(tx); 2819789Sahrens goto top; 2820789Sahrens } 28212113Sahrens dmu_tx_abort(tx); 2822789Sahrens goto out; 2823789Sahrens } 2824789Sahrens 2825789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 2826789Sahrens 2827789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 2828789Sahrens 2829789Sahrens ppmapout(va); 2830789Sahrens 2831789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 28321472Sperrin (void) zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0, NULL); 2833789Sahrens dmu_tx_commit(tx); 2834789Sahrens 28352237Smaybee zfs_range_unlock(rl); 2836789Sahrens 2837789Sahrens pvn_write_done(pp, B_WRITE | flags); 2838789Sahrens if (offp) 2839789Sahrens *offp = off; 2840789Sahrens if (lenp) 2841789Sahrens *lenp = len; 2842789Sahrens 2843789Sahrens out: 2844789Sahrens return (err); 2845789Sahrens } 2846789Sahrens 2847789Sahrens /* 2848789Sahrens * Copy the portion of the file indicated from pages into the file. 2849789Sahrens * The pages are stored in a page list attached to the files vnode. 2850789Sahrens * 2851789Sahrens * IN: vp - vnode of file to push page data to. 2852789Sahrens * off - position in file to put data. 2853789Sahrens * len - amount of data to write. 2854789Sahrens * flags - flags to control the operation. 2855789Sahrens * cr - credentials of caller. 2856789Sahrens * 2857789Sahrens * RETURN: 0 if success 2858789Sahrens * error code if failure 2859789Sahrens * 2860789Sahrens * Timestamps: 2861789Sahrens * vp - ctime|mtime updated 2862789Sahrens */ 2863789Sahrens static int 2864789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2865789Sahrens { 2866789Sahrens znode_t *zp = VTOZ(vp); 2867789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2868789Sahrens page_t *pp; 2869789Sahrens size_t io_len; 2870789Sahrens u_offset_t io_off; 28711669Sperrin uint64_t filesz; 2872789Sahrens int error = 0; 2873789Sahrens 2874789Sahrens ZFS_ENTER(zfsvfs); 2875789Sahrens 2876789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 2877789Sahrens 2878789Sahrens if (len == 0) { 2879789Sahrens /* 2880789Sahrens * Search the entire vp list for pages >= off. 2881789Sahrens */ 2882789Sahrens error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2883789Sahrens flags, cr); 28841472Sperrin goto out; 2885789Sahrens } 2886789Sahrens 28871669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 28881669Sperrin if (off > filesz) { 2889789Sahrens /* past end of file */ 2890789Sahrens ZFS_EXIT(zfsvfs); 2891789Sahrens return (0); 2892789Sahrens } 2893789Sahrens 28941669Sperrin len = MIN(len, filesz - off); 2895789Sahrens 28961472Sperrin for (io_off = off; io_off < off + len; io_off += io_len) { 2897789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 28981669Sperrin pp = page_lookup(vp, io_off, 2899789Sahrens (flags & (B_INVAL | B_FREE)) ? 2900789Sahrens SE_EXCL : SE_SHARED); 2901789Sahrens } else { 2902789Sahrens pp = page_lookup_nowait(vp, io_off, 2903789Sahrens (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2904789Sahrens } 2905789Sahrens 2906789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 2907789Sahrens int err; 2908789Sahrens 2909789Sahrens /* 2910789Sahrens * Found a dirty page to push 2911789Sahrens */ 29121669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 29131669Sperrin if (err) 2914789Sahrens error = err; 2915789Sahrens } else { 2916789Sahrens io_len = PAGESIZE; 2917789Sahrens } 2918789Sahrens } 29191472Sperrin out: 29201472Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, (flags & B_ASYNC) ? 0 : FDSYNC); 2921789Sahrens ZFS_EXIT(zfsvfs); 2922789Sahrens return (error); 2923789Sahrens } 2924789Sahrens 2925789Sahrens void 2926789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr) 2927789Sahrens { 2928789Sahrens znode_t *zp = VTOZ(vp); 2929789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2930789Sahrens int error; 2931789Sahrens 2932789Sahrens rw_enter(&zfsvfs->z_um_lock, RW_READER); 2933789Sahrens if (zfsvfs->z_unmounted2) { 2934789Sahrens ASSERT(zp->z_dbuf_held == 0); 2935789Sahrens 2936789Sahrens if (vn_has_cached_data(vp)) { 2937789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 2938789Sahrens B_INVAL, cr); 2939789Sahrens } 2940789Sahrens 29411544Seschrock mutex_enter(&zp->z_lock); 2942789Sahrens vp->v_count = 0; /* count arrives as 1 */ 29431544Seschrock if (zp->z_dbuf == NULL) { 29441544Seschrock mutex_exit(&zp->z_lock); 29451544Seschrock zfs_znode_free(zp); 29461544Seschrock } else { 29471544Seschrock mutex_exit(&zp->z_lock); 29481544Seschrock } 2949789Sahrens rw_exit(&zfsvfs->z_um_lock); 2950789Sahrens VFS_RELE(zfsvfs->z_vfs); 2951789Sahrens return; 2952789Sahrens } 2953789Sahrens 2954789Sahrens /* 2955789Sahrens * Attempt to push any data in the page cache. If this fails 2956789Sahrens * we will get kicked out later in zfs_zinactive(). 2957789Sahrens */ 29581298Sperrin if (vn_has_cached_data(vp)) { 29591298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 29601298Sperrin cr); 29611298Sperrin } 2962789Sahrens 2963789Sahrens if (zp->z_atime_dirty && zp->z_reap == 0) { 2964789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 2965789Sahrens 2966789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2967789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2968789Sahrens if (error) { 2969789Sahrens dmu_tx_abort(tx); 2970789Sahrens } else { 2971789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2972789Sahrens mutex_enter(&zp->z_lock); 2973789Sahrens zp->z_atime_dirty = 0; 2974789Sahrens mutex_exit(&zp->z_lock); 2975789Sahrens dmu_tx_commit(tx); 2976789Sahrens } 2977789Sahrens } 2978789Sahrens 2979789Sahrens zfs_zinactive(zp); 2980789Sahrens rw_exit(&zfsvfs->z_um_lock); 2981789Sahrens } 2982789Sahrens 2983789Sahrens /* 2984789Sahrens * Bounds-check the seek operation. 2985789Sahrens * 2986789Sahrens * IN: vp - vnode seeking within 2987789Sahrens * ooff - old file offset 2988789Sahrens * noffp - pointer to new file offset 2989789Sahrens * 2990789Sahrens * RETURN: 0 if success 2991789Sahrens * EINVAL if new offset invalid 2992789Sahrens */ 2993789Sahrens /* ARGSUSED */ 2994789Sahrens static int 2995789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 2996789Sahrens { 2997789Sahrens if (vp->v_type == VDIR) 2998789Sahrens return (0); 2999789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 3000789Sahrens } 3001789Sahrens 3002789Sahrens /* 3003789Sahrens * Pre-filter the generic locking function to trap attempts to place 3004789Sahrens * a mandatory lock on a memory mapped file. 3005789Sahrens */ 3006789Sahrens static int 3007789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 3008789Sahrens flk_callback_t *flk_cbp, cred_t *cr) 3009789Sahrens { 3010789Sahrens znode_t *zp = VTOZ(vp); 3011789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3012789Sahrens int error; 3013789Sahrens 3014789Sahrens ZFS_ENTER(zfsvfs); 3015789Sahrens 3016789Sahrens /* 30171544Seschrock * We are following the UFS semantics with respect to mapcnt 30181544Seschrock * here: If we see that the file is mapped already, then we will 30191544Seschrock * return an error, but we don't worry about races between this 30201544Seschrock * function and zfs_map(). 3021789Sahrens */ 30221544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 3023789Sahrens ZFS_EXIT(zfsvfs); 3024789Sahrens return (EAGAIN); 3025789Sahrens } 3026789Sahrens error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 3027789Sahrens ZFS_EXIT(zfsvfs); 3028789Sahrens return (error); 3029789Sahrens } 3030789Sahrens 3031789Sahrens /* 3032789Sahrens * If we can't find a page in the cache, we will create a new page 3033789Sahrens * and fill it with file data. For efficiency, we may try to fill 30341669Sperrin * multiple pages at once (klustering). 3035789Sahrens */ 3036789Sahrens static int 3037789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3038789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3039789Sahrens { 3040789Sahrens znode_t *zp = VTOZ(vp); 3041789Sahrens page_t *pp, *cur_pp; 3042789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3043789Sahrens caddr_t va; 3044789Sahrens u_offset_t io_off, total; 3045789Sahrens uint64_t oid = zp->z_id; 3046789Sahrens size_t io_len; 30471669Sperrin uint64_t filesz; 3048789Sahrens int err; 3049789Sahrens 3050789Sahrens /* 3051789Sahrens * If we are only asking for a single page don't bother klustering. 3052789Sahrens */ 30531669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 30541669Sperrin if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE || off > filesz) { 3055789Sahrens io_off = off; 3056789Sahrens io_len = PAGESIZE; 3057789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3058789Sahrens } else { 3059789Sahrens /* 3060789Sahrens * Try to fill a kluster of pages (a blocks worth). 3061789Sahrens */ 3062789Sahrens size_t klen; 3063789Sahrens u_offset_t koff; 3064789Sahrens 3065789Sahrens if (!ISP2(zp->z_blksz)) { 3066789Sahrens /* Only one block in the file. */ 3067789Sahrens klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3068789Sahrens koff = 0; 3069789Sahrens } else { 3070789Sahrens klen = plsz; 3071789Sahrens koff = P2ALIGN(off, (u_offset_t)klen); 3072789Sahrens } 30731819Smaybee ASSERT(koff <= filesz); 30741819Smaybee if (koff + klen > filesz) 30751819Smaybee klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 3076789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 3077789Sahrens &io_len, koff, klen, 0); 3078789Sahrens } 3079789Sahrens if (pp == NULL) { 3080789Sahrens /* 3081789Sahrens * Some other thread entered the page before us. 3082789Sahrens * Return to zfs_getpage to retry the lookup. 3083789Sahrens */ 3084789Sahrens *pl = NULL; 3085789Sahrens return (0); 3086789Sahrens } 3087789Sahrens 3088789Sahrens /* 3089789Sahrens * Fill the pages in the kluster. 3090789Sahrens */ 3091789Sahrens cur_pp = pp; 3092789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 3093789Sahrens ASSERT(io_off == cur_pp->p_offset); 3094789Sahrens va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 30951544Seschrock err = dmu_read(os, oid, io_off, PAGESIZE, va); 3096789Sahrens ppmapout(va); 3097789Sahrens if (err) { 3098789Sahrens /* On error, toss the entire kluster */ 3099789Sahrens pvn_read_done(pp, B_ERROR); 3100789Sahrens return (err); 3101789Sahrens } 3102789Sahrens cur_pp = cur_pp->p_next; 3103789Sahrens } 3104789Sahrens out: 3105789Sahrens /* 3106789Sahrens * Fill in the page list array from the kluster. If 3107789Sahrens * there are too many pages in the kluster, return 3108789Sahrens * as many pages as possible starting from the desired 3109789Sahrens * offset `off'. 3110789Sahrens * NOTE: the page list will always be null terminated. 3111789Sahrens */ 3112789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3113789Sahrens 3114789Sahrens return (0); 3115789Sahrens } 3116789Sahrens 3117789Sahrens /* 3118789Sahrens * Return pointers to the pages for the file region [off, off + len] 3119789Sahrens * in the pl array. If plsz is greater than len, this function may 3120789Sahrens * also return page pointers from before or after the specified 3121789Sahrens * region (i.e. some region [off', off' + plsz]). These additional 3122789Sahrens * pages are only returned if they are already in the cache, or were 3123789Sahrens * created as part of a klustered read. 3124789Sahrens * 3125789Sahrens * IN: vp - vnode of file to get data from. 3126789Sahrens * off - position in file to get data from. 3127789Sahrens * len - amount of data to retrieve. 3128789Sahrens * plsz - length of provided page list. 3129789Sahrens * seg - segment to obtain pages for. 3130789Sahrens * addr - virtual address of fault. 3131789Sahrens * rw - mode of created pages. 3132789Sahrens * cr - credentials of caller. 3133789Sahrens * 3134789Sahrens * OUT: protp - protection mode of created pages. 3135789Sahrens * pl - list of pages created. 3136789Sahrens * 3137789Sahrens * RETURN: 0 if success 3138789Sahrens * error code if failure 3139789Sahrens * 3140789Sahrens * Timestamps: 3141789Sahrens * vp - atime updated 3142789Sahrens */ 3143789Sahrens /* ARGSUSED */ 3144789Sahrens static int 3145789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3146789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3147789Sahrens enum seg_rw rw, cred_t *cr) 3148789Sahrens { 3149789Sahrens znode_t *zp = VTOZ(vp); 3150789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3151789Sahrens page_t *pp, **pl0 = pl; 31521669Sperrin rl_t *rl; 3153789Sahrens int cnt = 0, need_unlock = 0, err = 0; 3154789Sahrens 3155789Sahrens ZFS_ENTER(zfsvfs); 3156789Sahrens 3157789Sahrens if (protp) 3158789Sahrens *protp = PROT_ALL; 3159789Sahrens 3160789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 3161789Sahrens 3162789Sahrens /* no faultahead (for now) */ 3163789Sahrens if (pl == NULL) { 3164789Sahrens ZFS_EXIT(zfsvfs); 3165789Sahrens return (0); 3166789Sahrens } 3167789Sahrens 31681669Sperrin /* 31691669Sperrin * Make sure nobody restructures the file in the middle of the getpage. 31701669Sperrin */ 31711669Sperrin rl = zfs_range_lock(zp, off, len, RL_READER); 31721669Sperrin 3173789Sahrens /* can't fault past EOF */ 3174789Sahrens if (off >= zp->z_phys->zp_size) { 31752237Smaybee zfs_range_unlock(rl); 3176789Sahrens ZFS_EXIT(zfsvfs); 3177789Sahrens return (EFAULT); 3178789Sahrens } 3179789Sahrens 3180789Sahrens /* 3181789Sahrens * If we already own the lock, then we must be page faulting 3182789Sahrens * in the middle of a write to this file (i.e., we are writing 3183789Sahrens * to this file using data from a mapped region of the file). 3184789Sahrens */ 3185789Sahrens if (!rw_owner(&zp->z_map_lock)) { 3186789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 3187789Sahrens need_unlock = TRUE; 3188789Sahrens } 3189789Sahrens 3190789Sahrens /* 3191789Sahrens * Loop through the requested range [off, off + len] looking 3192789Sahrens * for pages. If we don't find a page, we will need to create 3193789Sahrens * a new page and fill it with data from the file. 3194789Sahrens */ 3195789Sahrens while (len > 0) { 3196789Sahrens if (plsz < PAGESIZE) 3197789Sahrens break; 3198789Sahrens if (pp = page_lookup(vp, off, SE_SHARED)) { 3199789Sahrens *pl++ = pp; 3200789Sahrens off += PAGESIZE; 3201789Sahrens addr += PAGESIZE; 3202789Sahrens len -= PAGESIZE; 3203789Sahrens plsz -= PAGESIZE; 3204789Sahrens } else { 3205789Sahrens err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 3206789Sahrens /* 3207789Sahrens * klustering may have changed our region 3208789Sahrens * to be block aligned. 3209789Sahrens */ 3210789Sahrens if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3211789Sahrens int delta = off - pp->p_offset; 3212789Sahrens len += delta; 3213789Sahrens off -= delta; 3214789Sahrens addr -= delta; 3215789Sahrens } 3216789Sahrens while (*pl) { 3217789Sahrens pl++; 3218789Sahrens cnt++; 3219789Sahrens off += PAGESIZE; 3220789Sahrens addr += PAGESIZE; 3221789Sahrens plsz -= PAGESIZE; 3222789Sahrens if (len > PAGESIZE) 3223789Sahrens len -= PAGESIZE; 3224789Sahrens else 3225789Sahrens len = 0; 3226789Sahrens } 32271669Sperrin if (err) { 32281669Sperrin /* 32291669Sperrin * Release any pages we have locked. 32301669Sperrin */ 32311669Sperrin while (pl > pl0) 32321669Sperrin page_unlock(*--pl); 32331669Sperrin goto out; 32341669Sperrin } 3235789Sahrens } 3236789Sahrens } 3237789Sahrens 3238789Sahrens /* 3239789Sahrens * Fill out the page array with any pages already in the cache. 3240789Sahrens */ 3241789Sahrens while (plsz > 0) { 3242789Sahrens pp = page_lookup_nowait(vp, off, SE_SHARED); 3243789Sahrens if (pp == NULL) 3244789Sahrens break; 3245789Sahrens *pl++ = pp; 3246789Sahrens off += PAGESIZE; 3247789Sahrens plsz -= PAGESIZE; 3248789Sahrens } 3249789Sahrens 3250789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3251789Sahrens out: 3252789Sahrens *pl = NULL; 3253789Sahrens 3254789Sahrens if (need_unlock) 3255789Sahrens rw_exit(&zp->z_map_lock); 32562237Smaybee zfs_range_unlock(rl); 3257789Sahrens 3258789Sahrens ZFS_EXIT(zfsvfs); 3259789Sahrens return (err); 3260789Sahrens } 3261789Sahrens 32621544Seschrock /* 32631544Seschrock * Request a memory map for a section of a file. This code interacts 32641544Seschrock * with common code and the VM system as follows: 32651544Seschrock * 32661544Seschrock * common code calls mmap(), which ends up in smmap_common() 32671544Seschrock * 32681544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 32691544Seschrock * 32701544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 32711544Seschrock * 32721544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 32731544Seschrock * 32741544Seschrock * zfs_addmap() updates z_mapcnt 32751544Seschrock */ 3276789Sahrens static int 3277789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3278789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3279789Sahrens { 3280789Sahrens znode_t *zp = VTOZ(vp); 3281789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3282789Sahrens segvn_crargs_t vn_a; 3283789Sahrens int error; 3284789Sahrens 3285789Sahrens ZFS_ENTER(zfsvfs); 3286789Sahrens 3287789Sahrens if (vp->v_flag & VNOMAP) { 3288789Sahrens ZFS_EXIT(zfsvfs); 3289789Sahrens return (ENOSYS); 3290789Sahrens } 3291789Sahrens 3292789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 3293789Sahrens ZFS_EXIT(zfsvfs); 3294789Sahrens return (ENXIO); 3295789Sahrens } 3296789Sahrens 3297789Sahrens if (vp->v_type != VREG) { 3298789Sahrens ZFS_EXIT(zfsvfs); 3299789Sahrens return (ENODEV); 3300789Sahrens } 3301789Sahrens 3302789Sahrens /* 3303789Sahrens * If file is locked, disallow mapping. 3304789Sahrens */ 33051544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 33061544Seschrock ZFS_EXIT(zfsvfs); 33071544Seschrock return (EAGAIN); 3308789Sahrens } 3309789Sahrens 3310789Sahrens as_rangelock(as); 3311789Sahrens if ((flags & MAP_FIXED) == 0) { 3312789Sahrens map_addr(addrp, len, off, 1, flags); 3313789Sahrens if (*addrp == NULL) { 3314789Sahrens as_rangeunlock(as); 3315789Sahrens ZFS_EXIT(zfsvfs); 3316789Sahrens return (ENOMEM); 3317789Sahrens } 3318789Sahrens } else { 3319789Sahrens /* 3320789Sahrens * User specified address - blow away any previous mappings 3321789Sahrens */ 3322789Sahrens (void) as_unmap(as, *addrp, len); 3323789Sahrens } 3324789Sahrens 3325789Sahrens vn_a.vp = vp; 3326789Sahrens vn_a.offset = (u_offset_t)off; 3327789Sahrens vn_a.type = flags & MAP_TYPE; 3328789Sahrens vn_a.prot = prot; 3329789Sahrens vn_a.maxprot = maxprot; 3330789Sahrens vn_a.cred = cr; 3331789Sahrens vn_a.amp = NULL; 3332789Sahrens vn_a.flags = flags & ~MAP_TYPE; 33331417Skchow vn_a.szc = 0; 33341417Skchow vn_a.lgrp_mem_policy_flags = 0; 3335789Sahrens 3336789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 3337789Sahrens 3338789Sahrens as_rangeunlock(as); 3339789Sahrens ZFS_EXIT(zfsvfs); 3340789Sahrens return (error); 3341789Sahrens } 3342789Sahrens 3343789Sahrens /* ARGSUSED */ 3344789Sahrens static int 3345789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3346789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3347789Sahrens { 33481544Seschrock uint64_t pages = btopr(len); 33491544Seschrock 33501544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3351789Sahrens return (0); 3352789Sahrens } 3353789Sahrens 33541773Seschrock /* 33551773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 33561773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 33571773Seschrock * detecting when the data was actually modified, we have to resort to 33581773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 33591773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 33601773Seschrock * which by far the most common case: 33611773Seschrock * 33621773Seschrock * open() 33631773Seschrock * mmap() 33641773Seschrock * <modify memory> 33651773Seschrock * munmap() 33661773Seschrock * close() 33671773Seschrock * <time lapse> 33681773Seschrock * putpage() via fsflush 33691773Seschrock * 33701773Seschrock * If we wait until fsflush to come along, we can have a modification time that 33711773Seschrock * is some arbitrary point in the future. In order to prevent this in the 33721773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 33731773Seschrock * torn down. 33741773Seschrock */ 3375789Sahrens /* ARGSUSED */ 3376789Sahrens static int 3377789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3378789Sahrens size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3379789Sahrens { 33801544Seschrock uint64_t pages = btopr(len); 33811544Seschrock 33821544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 33831544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 33841773Seschrock 33851773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 33861773Seschrock vn_has_cached_data(vp)) 33871773Seschrock (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 33881773Seschrock 3389789Sahrens return (0); 3390789Sahrens } 3391789Sahrens 3392789Sahrens /* 3393789Sahrens * Free or allocate space in a file. Currently, this function only 3394789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 3395789Sahrens * misnamed, as its functionality includes the ability to allocate as 3396789Sahrens * well as free space. 3397789Sahrens * 3398789Sahrens * IN: vp - vnode of file to free data in. 3399789Sahrens * cmd - action to take (only F_FREESP supported). 3400789Sahrens * bfp - section of file to free/alloc. 3401789Sahrens * flag - current file open mode flags. 3402789Sahrens * offset - current file offset. 3403789Sahrens * cr - credentials of caller [UNUSED]. 3404789Sahrens * 3405789Sahrens * RETURN: 0 if success 3406789Sahrens * error code if failure 3407789Sahrens * 3408789Sahrens * Timestamps: 3409789Sahrens * vp - ctime|mtime updated 3410789Sahrens */ 3411789Sahrens /* ARGSUSED */ 3412789Sahrens static int 3413789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3414789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 3415789Sahrens { 3416789Sahrens znode_t *zp = VTOZ(vp); 3417789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3418789Sahrens uint64_t off, len; 3419789Sahrens int error; 3420789Sahrens 3421789Sahrens ZFS_ENTER(zfsvfs); 3422789Sahrens 3423789Sahrens top: 3424789Sahrens if (cmd != F_FREESP) { 3425789Sahrens ZFS_EXIT(zfsvfs); 3426789Sahrens return (EINVAL); 3427789Sahrens } 3428789Sahrens 3429789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 3430789Sahrens ZFS_EXIT(zfsvfs); 3431789Sahrens return (error); 3432789Sahrens } 3433789Sahrens 3434789Sahrens if (bfp->l_len < 0) { 3435789Sahrens ZFS_EXIT(zfsvfs); 3436789Sahrens return (EINVAL); 3437789Sahrens } 3438789Sahrens 3439789Sahrens off = bfp->l_start; 34401669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 34411878Smaybee 34421878Smaybee do { 34431878Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 34442113Sahrens /* NB: we already did dmu_tx_wait() if necessary */ 34451878Smaybee } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3446789Sahrens 3447789Sahrens ZFS_EXIT(zfsvfs); 3448789Sahrens return (error); 3449789Sahrens } 3450789Sahrens 3451789Sahrens static int 3452789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp) 3453789Sahrens { 3454789Sahrens znode_t *zp = VTOZ(vp); 3455789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3456789Sahrens uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3457789Sahrens uint64_t object = zp->z_id; 3458789Sahrens zfid_short_t *zfid; 3459789Sahrens int size, i; 3460789Sahrens 3461789Sahrens ZFS_ENTER(zfsvfs); 3462789Sahrens 3463789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3464789Sahrens if (fidp->fid_len < size) { 3465789Sahrens fidp->fid_len = size; 34661512Sek110237 ZFS_EXIT(zfsvfs); 3467789Sahrens return (ENOSPC); 3468789Sahrens } 3469789Sahrens 3470789Sahrens zfid = (zfid_short_t *)fidp; 3471789Sahrens 3472789Sahrens zfid->zf_len = size; 3473789Sahrens 3474789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 3475789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3476789Sahrens 3477789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 3478789Sahrens if (gen == 0) 3479789Sahrens gen = 1; 3480789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 3481789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3482789Sahrens 3483789Sahrens if (size == LONG_FID_LEN) { 3484789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3485789Sahrens zfid_long_t *zlfid; 3486789Sahrens 3487789Sahrens zlfid = (zfid_long_t *)fidp; 3488789Sahrens 3489789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3490789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3491789Sahrens 3492789Sahrens /* XXX - this should be the generation number for the objset */ 3493789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3494789Sahrens zlfid->zf_setgen[i] = 0; 3495789Sahrens } 3496789Sahrens 3497789Sahrens ZFS_EXIT(zfsvfs); 3498789Sahrens return (0); 3499789Sahrens } 3500789Sahrens 3501789Sahrens static int 3502789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3503789Sahrens { 3504789Sahrens znode_t *zp, *xzp; 3505789Sahrens zfsvfs_t *zfsvfs; 3506789Sahrens zfs_dirlock_t *dl; 3507789Sahrens int error; 3508789Sahrens 3509789Sahrens switch (cmd) { 3510789Sahrens case _PC_LINK_MAX: 3511789Sahrens *valp = ULONG_MAX; 3512789Sahrens return (0); 3513789Sahrens 3514789Sahrens case _PC_FILESIZEBITS: 3515789Sahrens *valp = 64; 3516789Sahrens return (0); 3517789Sahrens 3518789Sahrens case _PC_XATTR_EXISTS: 3519789Sahrens zp = VTOZ(vp); 3520789Sahrens zfsvfs = zp->z_zfsvfs; 3521789Sahrens ZFS_ENTER(zfsvfs); 3522789Sahrens *valp = 0; 3523789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 3524789Sahrens ZXATTR | ZEXISTS | ZSHARED); 3525789Sahrens if (error == 0) { 3526789Sahrens zfs_dirent_unlock(dl); 3527789Sahrens if (!zfs_dirempty(xzp)) 3528789Sahrens *valp = 1; 3529789Sahrens VN_RELE(ZTOV(xzp)); 3530789Sahrens } else if (error == ENOENT) { 3531789Sahrens /* 3532789Sahrens * If there aren't extended attributes, it's the 3533789Sahrens * same as having zero of them. 3534789Sahrens */ 3535789Sahrens error = 0; 3536789Sahrens } 3537789Sahrens ZFS_EXIT(zfsvfs); 3538789Sahrens return (error); 3539789Sahrens 3540789Sahrens case _PC_ACL_ENABLED: 3541789Sahrens *valp = _ACL_ACE_ENABLED; 3542789Sahrens return (0); 3543789Sahrens 3544789Sahrens case _PC_MIN_HOLE_SIZE: 3545789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 3546789Sahrens return (0); 3547789Sahrens 3548789Sahrens default: 3549789Sahrens return (fs_pathconf(vp, cmd, valp, cr)); 3550789Sahrens } 3551789Sahrens } 3552789Sahrens 3553789Sahrens /*ARGSUSED*/ 3554789Sahrens static int 3555789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3556789Sahrens { 3557789Sahrens znode_t *zp = VTOZ(vp); 3558789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3559789Sahrens int error; 3560789Sahrens 3561789Sahrens ZFS_ENTER(zfsvfs); 3562789Sahrens error = zfs_getacl(zp, vsecp, cr); 3563789Sahrens ZFS_EXIT(zfsvfs); 3564789Sahrens 3565789Sahrens return (error); 3566789Sahrens } 3567789Sahrens 3568789Sahrens /*ARGSUSED*/ 3569789Sahrens static int 3570789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3571789Sahrens { 3572789Sahrens znode_t *zp = VTOZ(vp); 3573789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3574789Sahrens int error; 3575789Sahrens 3576789Sahrens ZFS_ENTER(zfsvfs); 3577789Sahrens error = zfs_setacl(zp, vsecp, cr); 3578789Sahrens ZFS_EXIT(zfsvfs); 3579789Sahrens return (error); 3580789Sahrens } 3581789Sahrens 3582789Sahrens /* 3583789Sahrens * Predeclare these here so that the compiler assumes that 3584789Sahrens * this is an "old style" function declaration that does 3585789Sahrens * not include arguments => we won't get type mismatch errors 3586789Sahrens * in the initializations that follow. 3587789Sahrens */ 3588789Sahrens static int zfs_inval(); 3589789Sahrens static int zfs_isdir(); 3590789Sahrens 3591789Sahrens static int 3592789Sahrens zfs_inval() 3593789Sahrens { 3594789Sahrens return (EINVAL); 3595789Sahrens } 3596789Sahrens 3597789Sahrens static int 3598789Sahrens zfs_isdir() 3599789Sahrens { 3600789Sahrens return (EISDIR); 3601789Sahrens } 3602789Sahrens /* 3603789Sahrens * Directory vnode operations template 3604789Sahrens */ 3605789Sahrens vnodeops_t *zfs_dvnodeops; 3606789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 3607789Sahrens VOPNAME_OPEN, zfs_open, 3608789Sahrens VOPNAME_CLOSE, zfs_close, 3609789Sahrens VOPNAME_READ, zfs_isdir, 3610789Sahrens VOPNAME_WRITE, zfs_isdir, 3611789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3612789Sahrens VOPNAME_GETATTR, zfs_getattr, 3613789Sahrens VOPNAME_SETATTR, zfs_setattr, 3614789Sahrens VOPNAME_ACCESS, zfs_access, 3615789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3616789Sahrens VOPNAME_CREATE, zfs_create, 3617789Sahrens VOPNAME_REMOVE, zfs_remove, 3618789Sahrens VOPNAME_LINK, zfs_link, 3619789Sahrens VOPNAME_RENAME, zfs_rename, 3620789Sahrens VOPNAME_MKDIR, zfs_mkdir, 3621789Sahrens VOPNAME_RMDIR, zfs_rmdir, 3622789Sahrens VOPNAME_READDIR, zfs_readdir, 3623789Sahrens VOPNAME_SYMLINK, zfs_symlink, 3624789Sahrens VOPNAME_FSYNC, zfs_fsync, 3625789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3626789Sahrens VOPNAME_FID, zfs_fid, 3627789Sahrens VOPNAME_SEEK, zfs_seek, 3628789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3629789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3630789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3631789Sahrens NULL, NULL 3632789Sahrens }; 3633789Sahrens 3634789Sahrens /* 3635789Sahrens * Regular file vnode operations template 3636789Sahrens */ 3637789Sahrens vnodeops_t *zfs_fvnodeops; 3638789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 3639789Sahrens VOPNAME_OPEN, zfs_open, 3640789Sahrens VOPNAME_CLOSE, zfs_close, 3641789Sahrens VOPNAME_READ, zfs_read, 3642789Sahrens VOPNAME_WRITE, zfs_write, 3643789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3644789Sahrens VOPNAME_GETATTR, zfs_getattr, 3645789Sahrens VOPNAME_SETATTR, zfs_setattr, 3646789Sahrens VOPNAME_ACCESS, zfs_access, 3647789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3648789Sahrens VOPNAME_RENAME, zfs_rename, 3649789Sahrens VOPNAME_FSYNC, zfs_fsync, 3650789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive, 3651789Sahrens VOPNAME_FID, zfs_fid, 3652789Sahrens VOPNAME_SEEK, zfs_seek, 3653789Sahrens VOPNAME_FRLOCK, zfs_frlock, 3654789Sahrens VOPNAME_SPACE, zfs_space, 3655789Sahrens VOPNAME_GETPAGE, zfs_getpage, 3656789Sahrens VOPNAME_PUTPAGE, zfs_putpage, 3657789Sahrens VOPNAME_MAP, (fs_generic_func_p) zfs_map, 3658789Sahrens VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap, 3659789Sahrens VOPNAME_DELMAP, zfs_delmap, 3660789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3661789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3662789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3663789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3664789Sahrens NULL, NULL 3665789Sahrens }; 3666789Sahrens 3667789Sahrens /* 3668789Sahrens * Symbolic link vnode operations template 3669789Sahrens */ 3670789Sahrens vnodeops_t *zfs_symvnodeops; 3671789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 3672789Sahrens VOPNAME_GETATTR, zfs_getattr, 3673789Sahrens VOPNAME_SETATTR, zfs_setattr, 3674789Sahrens VOPNAME_ACCESS, zfs_access, 3675789Sahrens VOPNAME_RENAME, zfs_rename, 3676789Sahrens VOPNAME_READLINK, zfs_readlink, 3677789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3678789Sahrens VOPNAME_FID, zfs_fid, 3679789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3680789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3681789Sahrens NULL, NULL 3682789Sahrens }; 3683789Sahrens 3684789Sahrens /* 3685789Sahrens * Extended attribute directory vnode operations template 3686789Sahrens * This template is identical to the directory vnodes 3687789Sahrens * operation template except for restricted operations: 3688789Sahrens * VOP_MKDIR() 3689789Sahrens * VOP_SYMLINK() 3690789Sahrens * Note that there are other restrictions embedded in: 3691789Sahrens * zfs_create() - restrict type to VREG 3692789Sahrens * zfs_link() - no links into/out of attribute space 3693789Sahrens * zfs_rename() - no moves into/out of attribute space 3694789Sahrens */ 3695789Sahrens vnodeops_t *zfs_xdvnodeops; 3696789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 3697789Sahrens VOPNAME_OPEN, zfs_open, 3698789Sahrens VOPNAME_CLOSE, zfs_close, 3699789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3700789Sahrens VOPNAME_GETATTR, zfs_getattr, 3701789Sahrens VOPNAME_SETATTR, zfs_setattr, 3702789Sahrens VOPNAME_ACCESS, zfs_access, 3703789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3704789Sahrens VOPNAME_CREATE, zfs_create, 3705789Sahrens VOPNAME_REMOVE, zfs_remove, 3706789Sahrens VOPNAME_LINK, zfs_link, 3707789Sahrens VOPNAME_RENAME, zfs_rename, 3708789Sahrens VOPNAME_MKDIR, zfs_inval, 3709789Sahrens VOPNAME_RMDIR, zfs_rmdir, 3710789Sahrens VOPNAME_READDIR, zfs_readdir, 3711789Sahrens VOPNAME_SYMLINK, zfs_inval, 3712789Sahrens VOPNAME_FSYNC, zfs_fsync, 3713789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3714789Sahrens VOPNAME_FID, zfs_fid, 3715789Sahrens VOPNAME_SEEK, zfs_seek, 3716789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3717789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3718789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3719789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3720789Sahrens NULL, NULL 3721789Sahrens }; 3722789Sahrens 3723789Sahrens /* 3724789Sahrens * Error vnode operations template 3725789Sahrens */ 3726789Sahrens vnodeops_t *zfs_evnodeops; 3727789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 3728789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3729789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3730789Sahrens NULL, NULL 3731789Sahrens }; 3732