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, 111789Sahrens * then drop all locks, call txg_wait_open(), 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 * dmu_tx_abort(tx); // abort DMU tx 134789Sahrens * rw_exit(...); // drop locks 135789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 136789Sahrens * VN_RELE(...); // release held vnodes 137789Sahrens * if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 138789Sahrens * txg_wait_open(dmu_objset_pool(os), 0); 139789Sahrens * goto top; 140789Sahrens * } 141789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 142789Sahrens * return (error); // really out of space 143789Sahrens * } 144789Sahrens * error = do_real_work(); // do whatever this VOP does 145789Sahrens * if (error == 0) 146789Sahrens * seq = zfs_log_*(...); // on success, make ZIL entry 147789Sahrens * dmu_tx_commit(tx); // commit DMU tx -- error or not 148789Sahrens * rw_exit(...); // drop locks 149789Sahrens * zfs_dirent_unlock(dl); // unlock directory entry 150789Sahrens * VN_RELE(...); // release held vnodes 151789Sahrens * zil_commit(zilog, seq, ioflag); // synchronous when necessary 152789Sahrens * ZFS_EXIT(zfsvfs); // finished in zfs 153789Sahrens * return (error); // done, report error 154789Sahrens */ 155789Sahrens 156789Sahrens /* ARGSUSED */ 157789Sahrens static int 158789Sahrens zfs_open(vnode_t **vpp, int flag, cred_t *cr) 159789Sahrens { 160789Sahrens return (0); 161789Sahrens } 162789Sahrens 163789Sahrens /* ARGSUSED */ 164789Sahrens static int 165789Sahrens zfs_close(vnode_t *vp, int flag, int count, offset_t offset, cred_t *cr) 166789Sahrens { 167789Sahrens /* 168789Sahrens * Clean up any locks held by this process on the vp. 169789Sahrens */ 170789Sahrens cleanlocks(vp, ddi_get_pid(), 0); 171789Sahrens cleanshares(vp, ddi_get_pid()); 172789Sahrens 173789Sahrens return (0); 174789Sahrens } 175789Sahrens 176789Sahrens /* 177789Sahrens * Lseek support for finding holes (cmd == _FIO_SEEK_HOLE) and 178789Sahrens * data (cmd == _FIO_SEEK_DATA). "off" is an in/out parameter. 179789Sahrens */ 180789Sahrens static int 181789Sahrens zfs_holey(vnode_t *vp, int cmd, offset_t *off) 182789Sahrens { 183789Sahrens znode_t *zp = VTOZ(vp); 184789Sahrens uint64_t noff = (uint64_t)*off; /* new offset */ 185789Sahrens uint64_t file_sz; 186789Sahrens int error; 187789Sahrens boolean_t hole; 188789Sahrens 189789Sahrens file_sz = zp->z_phys->zp_size; 190789Sahrens if (noff >= file_sz) { 191789Sahrens return (ENXIO); 192789Sahrens } 193789Sahrens 194789Sahrens if (cmd == _FIO_SEEK_HOLE) 195789Sahrens hole = B_TRUE; 196789Sahrens else 197789Sahrens hole = B_FALSE; 198789Sahrens 199789Sahrens error = dmu_offset_next(zp->z_zfsvfs->z_os, zp->z_id, hole, &noff); 200789Sahrens 201789Sahrens /* end of file? */ 202789Sahrens if ((error == ESRCH) || (noff > file_sz)) { 203789Sahrens /* 204789Sahrens * Handle the virtual hole at the end of file. 205789Sahrens */ 206789Sahrens if (hole) { 207789Sahrens *off = file_sz; 208789Sahrens return (0); 209789Sahrens } 210789Sahrens return (ENXIO); 211789Sahrens } 212789Sahrens 213789Sahrens if (noff < *off) 214789Sahrens return (error); 215789Sahrens *off = noff; 216789Sahrens return (error); 217789Sahrens } 218789Sahrens 219789Sahrens /* ARGSUSED */ 220789Sahrens static int 221789Sahrens zfs_ioctl(vnode_t *vp, int com, intptr_t data, int flag, cred_t *cred, 222789Sahrens int *rvalp) 223789Sahrens { 224789Sahrens offset_t off; 225789Sahrens int error; 226789Sahrens zfsvfs_t *zfsvfs; 227789Sahrens 228789Sahrens switch (com) { 229789Sahrens case _FIOFFS: 230789Sahrens return (zfs_sync(vp->v_vfsp, 0, cred)); 231789Sahrens 2321544Seschrock /* 2331544Seschrock * The following two ioctls are used by bfu. Faking out, 2341544Seschrock * necessary to avoid bfu errors. 2351544Seschrock */ 2361544Seschrock case _FIOGDIO: 2371544Seschrock case _FIOSDIO: 2381544Seschrock return (0); 2391544Seschrock 240789Sahrens case _FIO_SEEK_DATA: 241789Sahrens case _FIO_SEEK_HOLE: 242789Sahrens if (ddi_copyin((void *)data, &off, sizeof (off), flag)) 243789Sahrens return (EFAULT); 244789Sahrens 245789Sahrens zfsvfs = VTOZ(vp)->z_zfsvfs; 246789Sahrens ZFS_ENTER(zfsvfs); 247789Sahrens 248789Sahrens /* offset parameter is in/out */ 249789Sahrens error = zfs_holey(vp, com, &off); 250789Sahrens ZFS_EXIT(zfsvfs); 251789Sahrens if (error) 252789Sahrens return (error); 253789Sahrens if (ddi_copyout(&off, (void *)data, sizeof (off), flag)) 254789Sahrens return (EFAULT); 255789Sahrens return (0); 256789Sahrens } 257789Sahrens return (ENOTTY); 258789Sahrens } 259789Sahrens 260789Sahrens /* 261789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 262789Sahrens * between the DMU cache and the memory mapped pages. What this means: 263789Sahrens * 264789Sahrens * On Write: If we find a memory mapped page, we write to *both* 265789Sahrens * the page and the dmu buffer. 266789Sahrens * 267789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 268789Sahrens * the file is memory mapped. 269789Sahrens */ 270789Sahrens static int 271789Sahrens mappedwrite(vnode_t *vp, uint64_t woff, int nbytes, uio_t *uio, dmu_tx_t *tx) 272789Sahrens { 273789Sahrens znode_t *zp = VTOZ(vp); 274789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 275789Sahrens int64_t start, off; 276789Sahrens int len = nbytes; 277789Sahrens int error = 0; 278789Sahrens 279789Sahrens start = uio->uio_loffset; 280789Sahrens off = start & PAGEOFFSET; 281789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 282789Sahrens page_t *pp; 283789Sahrens uint64_t bytes = MIN(PAGESIZE - off, len); 284789Sahrens 285789Sahrens /* 286789Sahrens * We don't want a new page to "appear" in the middle of 287789Sahrens * the file update (because it may not get the write 288789Sahrens * update data), so we grab a lock to block 289789Sahrens * zfs_getpage(). 290789Sahrens */ 291789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 292789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 293789Sahrens caddr_t va; 294789Sahrens 295789Sahrens rw_exit(&zp->z_map_lock); 296789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 297789Sahrens error = uiomove(va+off, bytes, UIO_WRITE, uio); 298789Sahrens if (error == 0) { 299789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, 300789Sahrens woff, bytes, va+off, tx); 301789Sahrens } 302789Sahrens ppmapout(va); 303789Sahrens page_unlock(pp); 304789Sahrens } else { 305789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 306789Sahrens woff, bytes, uio, tx); 307789Sahrens rw_exit(&zp->z_map_lock); 308789Sahrens } 309789Sahrens len -= bytes; 310789Sahrens woff += bytes; 311789Sahrens off = 0; 312789Sahrens if (error) 313789Sahrens break; 314789Sahrens } 315789Sahrens return (error); 316789Sahrens } 317789Sahrens 318789Sahrens /* 319789Sahrens * When a file is memory mapped, we must keep the IO data synchronized 320789Sahrens * between the DMU cache and the memory mapped pages. What this means: 321789Sahrens * 322789Sahrens * On Read: We "read" preferentially from memory mapped pages, 323789Sahrens * else we default from the dmu buffer. 324789Sahrens * 325789Sahrens * NOTE: We will always "break up" the IO into PAGESIZE uiomoves when 326789Sahrens * the file is memory mapped. 327789Sahrens */ 328789Sahrens static int 329789Sahrens mappedread(vnode_t *vp, char *addr, int nbytes, uio_t *uio) 330789Sahrens { 331789Sahrens int64_t start, off, bytes; 332789Sahrens int len = nbytes; 333789Sahrens int error = 0; 334789Sahrens 335789Sahrens start = uio->uio_loffset; 336789Sahrens off = start & PAGEOFFSET; 337789Sahrens for (start &= PAGEMASK; len > 0; start += PAGESIZE) { 338789Sahrens page_t *pp; 339789Sahrens 340789Sahrens bytes = MIN(PAGESIZE - off, len); 341789Sahrens if (pp = page_lookup(vp, start, SE_SHARED)) { 342789Sahrens caddr_t va; 343789Sahrens 344789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1L); 345789Sahrens error = uiomove(va + off, bytes, UIO_READ, uio); 346789Sahrens ppmapout(va); 347789Sahrens page_unlock(pp); 348789Sahrens } else { 349789Sahrens /* XXX use dmu_read here? */ 350789Sahrens error = uiomove(addr, bytes, UIO_READ, uio); 351789Sahrens } 352789Sahrens len -= bytes; 353789Sahrens addr += bytes; 354789Sahrens off = 0; 355789Sahrens if (error) 356789Sahrens break; 357789Sahrens } 358789Sahrens return (error); 359789Sahrens } 360789Sahrens 361789Sahrens uint_t zfs_read_chunk_size = 1024 * 1024; /* Tunable */ 362789Sahrens 363789Sahrens /* 364789Sahrens * Read bytes from specified file into supplied buffer. 365789Sahrens * 366789Sahrens * IN: vp - vnode of file to be read from. 367789Sahrens * uio - structure supplying read location, range info, 368789Sahrens * and return buffer. 369789Sahrens * ioflag - SYNC flags; used to provide FRSYNC semantics. 370789Sahrens * cr - credentials of caller. 371789Sahrens * 372789Sahrens * OUT: uio - updated offset and range, buffer filled. 373789Sahrens * 374789Sahrens * RETURN: 0 if success 375789Sahrens * error code if failure 376789Sahrens * 377789Sahrens * Side Effects: 378789Sahrens * vp - atime updated if byte count > 0 379789Sahrens */ 380789Sahrens /* ARGSUSED */ 381789Sahrens static int 382789Sahrens zfs_read(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 383789Sahrens { 384789Sahrens znode_t *zp = VTOZ(vp); 385789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 386789Sahrens uint64_t delta; 387789Sahrens ssize_t n, size, cnt, ndone; 388789Sahrens int error, i, numbufs; 389789Sahrens dmu_buf_t *dbp, **dbpp; 3901669Sperrin rl_t *rl; 391789Sahrens 392789Sahrens ZFS_ENTER(zfsvfs); 393789Sahrens 394789Sahrens /* 395789Sahrens * Validate file offset 396789Sahrens */ 397789Sahrens if (uio->uio_loffset < (offset_t)0) { 398789Sahrens ZFS_EXIT(zfsvfs); 399789Sahrens return (EINVAL); 400789Sahrens } 401789Sahrens 402789Sahrens /* 403789Sahrens * Fasttrack empty reads 404789Sahrens */ 405789Sahrens if (uio->uio_resid == 0) { 406789Sahrens ZFS_EXIT(zfsvfs); 407789Sahrens return (0); 408789Sahrens } 409789Sahrens 410789Sahrens /* 4111669Sperrin * Check for mandatory locks 412789Sahrens */ 413789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode)) { 414789Sahrens if (error = chklock(vp, FREAD, 415789Sahrens uio->uio_loffset, uio->uio_resid, uio->uio_fmode, ct)) { 416789Sahrens ZFS_EXIT(zfsvfs); 417789Sahrens return (error); 418789Sahrens } 419789Sahrens } 420789Sahrens 421789Sahrens /* 422789Sahrens * If we're in FRSYNC mode, sync out this znode before reading it. 423789Sahrens */ 424789Sahrens zil_commit(zfsvfs->z_log, zp->z_last_itx, ioflag & FRSYNC); 425789Sahrens 426789Sahrens /* 4271669Sperrin * Lock the range against changes. 428789Sahrens */ 4291669Sperrin rl = zfs_range_lock(zp, uio->uio_loffset, uio->uio_resid, RL_READER); 4301669Sperrin 431789Sahrens /* 432789Sahrens * If we are reading past end-of-file we can skip 433789Sahrens * to the end; but we might still need to set atime. 434789Sahrens */ 435789Sahrens if (uio->uio_loffset >= zp->z_phys->zp_size) { 436789Sahrens cnt = 0; 437789Sahrens error = 0; 438789Sahrens goto out; 439789Sahrens } 440789Sahrens 441789Sahrens cnt = MIN(uio->uio_resid, zp->z_phys->zp_size - uio->uio_loffset); 442789Sahrens 443789Sahrens for (ndone = 0; ndone < cnt; ndone += zfs_read_chunk_size) { 444789Sahrens ASSERT(uio->uio_loffset < zp->z_phys->zp_size); 445789Sahrens n = MIN(zfs_read_chunk_size, 446789Sahrens zp->z_phys->zp_size - uio->uio_loffset); 447789Sahrens n = MIN(n, cnt); 4481544Seschrock error = dmu_buf_hold_array(zfsvfs->z_os, zp->z_id, 4491544Seschrock uio->uio_loffset, n, TRUE, FTAG, &numbufs, &dbpp); 4501544Seschrock if (error) 451789Sahrens goto out; 452789Sahrens /* 453789Sahrens * Compute the adjustment to align the dmu buffers 454789Sahrens * with the uio buffer. 455789Sahrens */ 456789Sahrens delta = uio->uio_loffset - dbpp[0]->db_offset; 457789Sahrens 458789Sahrens for (i = 0; i < numbufs; i++) { 459789Sahrens if (n < 0) 460789Sahrens break; 461789Sahrens dbp = dbpp[i]; 462789Sahrens size = dbp->db_size - delta; 463789Sahrens /* 464789Sahrens * XXX -- this is correct, but may be suboptimal. 465789Sahrens * If the pages are all clean, we don't need to 466789Sahrens * go through mappedread(). Maybe the VMODSORT 467789Sahrens * stuff can help us here. 468789Sahrens */ 469789Sahrens if (vn_has_cached_data(vp)) { 470789Sahrens error = mappedread(vp, (caddr_t)dbp->db_data + 471789Sahrens delta, (n < size ? n : size), uio); 472789Sahrens } else { 473789Sahrens error = uiomove((caddr_t)dbp->db_data + delta, 474789Sahrens (n < size ? n : size), UIO_READ, uio); 475789Sahrens } 476789Sahrens if (error) { 4771544Seschrock dmu_buf_rele_array(dbpp, numbufs, FTAG); 478789Sahrens goto out; 479789Sahrens } 480789Sahrens n -= dbp->db_size; 481789Sahrens if (delta) { 482789Sahrens n += delta; 483789Sahrens delta = 0; 484789Sahrens } 485789Sahrens } 4861544Seschrock dmu_buf_rele_array(dbpp, numbufs, FTAG); 487789Sahrens } 488789Sahrens out: 4891669Sperrin zfs_range_unlock(zp, rl); 490789Sahrens 491789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 492789Sahrens ZFS_EXIT(zfsvfs); 493789Sahrens return (error); 494789Sahrens } 495789Sahrens 496789Sahrens /* 497789Sahrens * Fault in the pages of the first n bytes specified by the uio structure. 498789Sahrens * 1 byte in each page is touched and the uio struct is unmodified. 499789Sahrens * Any error will exit this routine as this is only a best 500789Sahrens * attempt to get the pages resident. This is a copy of ufs_trans_touch(). 501789Sahrens */ 502789Sahrens static void 503789Sahrens zfs_prefault_write(ssize_t n, struct uio *uio) 504789Sahrens { 505789Sahrens struct iovec *iov; 506789Sahrens ulong_t cnt, incr; 507789Sahrens caddr_t p; 508789Sahrens uint8_t tmp; 509789Sahrens 510789Sahrens iov = uio->uio_iov; 511789Sahrens 512789Sahrens while (n) { 513789Sahrens cnt = MIN(iov->iov_len, n); 514789Sahrens if (cnt == 0) { 515789Sahrens /* empty iov entry */ 516789Sahrens iov++; 517789Sahrens continue; 518789Sahrens } 519789Sahrens n -= cnt; 520789Sahrens /* 521789Sahrens * touch each page in this segment. 522789Sahrens */ 523789Sahrens p = iov->iov_base; 524789Sahrens while (cnt) { 525789Sahrens switch (uio->uio_segflg) { 526789Sahrens case UIO_USERSPACE: 527789Sahrens case UIO_USERISPACE: 528789Sahrens if (fuword8(p, &tmp)) 529789Sahrens return; 530789Sahrens break; 531789Sahrens case UIO_SYSSPACE: 532789Sahrens if (kcopy(p, &tmp, 1)) 533789Sahrens return; 534789Sahrens break; 535789Sahrens } 536789Sahrens incr = MIN(cnt, PAGESIZE); 537789Sahrens p += incr; 538789Sahrens cnt -= incr; 539789Sahrens } 540789Sahrens /* 541789Sahrens * touch the last byte in case it straddles a page. 542789Sahrens */ 543789Sahrens p--; 544789Sahrens switch (uio->uio_segflg) { 545789Sahrens case UIO_USERSPACE: 546789Sahrens case UIO_USERISPACE: 547789Sahrens if (fuword8(p, &tmp)) 548789Sahrens return; 549789Sahrens break; 550789Sahrens case UIO_SYSSPACE: 551789Sahrens if (kcopy(p, &tmp, 1)) 552789Sahrens return; 553789Sahrens break; 554789Sahrens } 555789Sahrens iov++; 556789Sahrens } 557789Sahrens } 558789Sahrens 559789Sahrens /* 560789Sahrens * Write the bytes to a file. 561789Sahrens * 562789Sahrens * IN: vp - vnode of file to be written to. 563789Sahrens * uio - structure supplying write location, range info, 564789Sahrens * and data buffer. 565789Sahrens * ioflag - FAPPEND flag set if in append mode. 566789Sahrens * cr - credentials of caller. 567789Sahrens * 568789Sahrens * OUT: uio - updated offset and range. 569789Sahrens * 570789Sahrens * RETURN: 0 if success 571789Sahrens * error code if failure 572789Sahrens * 573789Sahrens * Timestamps: 574789Sahrens * vp - ctime|mtime updated if byte count > 0 575789Sahrens */ 576789Sahrens /* ARGSUSED */ 577789Sahrens static int 578789Sahrens zfs_write(vnode_t *vp, uio_t *uio, int ioflag, cred_t *cr, caller_context_t *ct) 579789Sahrens { 580789Sahrens znode_t *zp = VTOZ(vp); 581789Sahrens rlim64_t limit = uio->uio_llimit; 582789Sahrens ssize_t start_resid = uio->uio_resid; 583789Sahrens ssize_t tx_bytes; 584789Sahrens uint64_t end_size; 585789Sahrens dmu_tx_t *tx; 586789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 587789Sahrens zilog_t *zilog = zfsvfs->z_log; 588789Sahrens uint64_t seq = 0; 589789Sahrens offset_t woff; 590789Sahrens ssize_t n, nbytes; 5911669Sperrin rl_t *rl; 592789Sahrens int max_blksz = zfsvfs->z_max_blksz; 5931669Sperrin int error; 594789Sahrens 595789Sahrens /* 596789Sahrens * Fasttrack empty write 597789Sahrens */ 5981669Sperrin n = start_resid; 599789Sahrens if (n == 0) 600789Sahrens return (0); 601789Sahrens 6021669Sperrin if (limit == RLIM64_INFINITY || limit > MAXOFFSET_T) 6031669Sperrin limit = MAXOFFSET_T; 6041669Sperrin 605789Sahrens ZFS_ENTER(zfsvfs); 606789Sahrens 607789Sahrens /* 6081669Sperrin * Pre-fault the initial pages to ensure slow (eg NFS) pages 6091669Sperrin * don't hold up txg. 610789Sahrens */ 611789Sahrens zfs_prefault_write(MIN(start_resid, SPA_MAXBLOCKSIZE), uio); 612789Sahrens 613789Sahrens /* 614789Sahrens * If in append mode, set the io offset pointer to eof. 615789Sahrens */ 6161669Sperrin if (ioflag & FAPPEND) { 6171669Sperrin /* 6181669Sperrin * Range lock for a file append: 6191669Sperrin * The value for the start of range will be determined by 6201669Sperrin * zfs_range_lock() (to guarantee append semantics). 6211669Sperrin * If this write will cause the block size to increase, 6221669Sperrin * zfs_range_lock() will lock the entire file, so we must 6231669Sperrin * later reduce the range after we grow the block size. 6241669Sperrin */ 6251669Sperrin rl = zfs_range_lock(zp, 0, n, RL_APPEND); 6261669Sperrin if (rl->r_len == UINT64_MAX) { 6271669Sperrin /* overlocked, zp_size can't change */ 6281669Sperrin woff = uio->uio_loffset = zp->z_phys->zp_size; 6291669Sperrin } else { 6301669Sperrin woff = uio->uio_loffset = rl->r_off; 6311669Sperrin } 632789Sahrens } else { 633789Sahrens woff = uio->uio_loffset; 634789Sahrens /* 635789Sahrens * Validate file offset 636789Sahrens */ 637789Sahrens if (woff < 0) { 638789Sahrens ZFS_EXIT(zfsvfs); 639789Sahrens return (EINVAL); 640789Sahrens } 641789Sahrens 642789Sahrens /* 6431669Sperrin * If we need to grow the block size then zfs_range_lock() 6441669Sperrin * will lock a wider range than we request here. 6451669Sperrin * Later after growing the block size we reduce the range. 646789Sahrens */ 6471669Sperrin rl = zfs_range_lock(zp, woff, n, RL_WRITER); 648789Sahrens } 649789Sahrens 650789Sahrens if (woff >= limit) { 651789Sahrens error = EFBIG; 652789Sahrens goto no_tx_done; 653789Sahrens } 654789Sahrens 655789Sahrens if ((woff + n) > limit || woff > (limit - n)) 656789Sahrens n = limit - woff; 657789Sahrens 658789Sahrens /* 6591669Sperrin * Check for mandatory locks 660789Sahrens */ 661789Sahrens if (MANDMODE((mode_t)zp->z_phys->zp_mode) && 662789Sahrens (error = chklock(vp, FWRITE, woff, n, uio->uio_fmode, ct)) != 0) 663789Sahrens goto no_tx_done; 6641669Sperrin end_size = MAX(zp->z_phys->zp_size, woff + n); 665789Sahrens top: 666789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 667789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 668789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 669789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 670789Sahrens if (error) { 671789Sahrens dmu_tx_abort(tx); 672789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 673789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 674789Sahrens goto top; 675789Sahrens } 676789Sahrens goto no_tx_done; 677789Sahrens } 678789Sahrens 6791669Sperrin /* 6801669Sperrin * If zfs_range_lock() over-locked we grow the blocksize 6811669Sperrin * and then reduce the lock range. 6821669Sperrin */ 6831669Sperrin if (rl->r_len == UINT64_MAX) { 684789Sahrens uint64_t new_blksz; 6851669Sperrin 686789Sahrens if (zp->z_blksz > max_blksz) { 687789Sahrens ASSERT(!ISP2(zp->z_blksz)); 688789Sahrens new_blksz = MIN(end_size, SPA_MAXBLOCKSIZE); 689789Sahrens } else { 690789Sahrens new_blksz = MIN(end_size, max_blksz); 691789Sahrens } 6921669Sperrin zfs_grow_blocksize(zp, new_blksz, tx); 6931669Sperrin zfs_range_reduce(zp, rl, woff, n); 694789Sahrens } 695789Sahrens 696789Sahrens /* 697789Sahrens * The file data does not fit in the znode "cache", so we 698789Sahrens * will be writing to the file block data buffers. 699789Sahrens * Each buffer will be written in a separate transaction; 700789Sahrens * this keeps the intent log records small and allows us 701789Sahrens * to do more fine-grained space accounting. 702789Sahrens */ 703789Sahrens while (n > 0) { 704789Sahrens /* 705789Sahrens * XXX - should we really limit each write to z_max_blksz? 706789Sahrens * Perhaps we should use SPA_MAXBLOCKSIZE chunks? 707789Sahrens */ 708789Sahrens nbytes = MIN(n, max_blksz - P2PHASE(woff, max_blksz)); 709789Sahrens rw_enter(&zp->z_map_lock, RW_READER); 710789Sahrens 711789Sahrens tx_bytes = uio->uio_resid; 712789Sahrens if (vn_has_cached_data(vp)) { 713789Sahrens rw_exit(&zp->z_map_lock); 714789Sahrens error = mappedwrite(vp, woff, nbytes, uio, tx); 715789Sahrens } else { 716789Sahrens error = dmu_write_uio(zfsvfs->z_os, zp->z_id, 717789Sahrens woff, nbytes, uio, tx); 718789Sahrens rw_exit(&zp->z_map_lock); 719789Sahrens } 720789Sahrens tx_bytes -= uio->uio_resid; 721789Sahrens 722789Sahrens if (error) { 723789Sahrens /* XXX - do we need to "clean up" the dmu buffer? */ 724789Sahrens break; 725789Sahrens } 726789Sahrens 727789Sahrens ASSERT(tx_bytes == nbytes); 728789Sahrens 7291576Smarks /* 7301576Smarks * Clear Set-UID/Set-GID bits on successful write if not 7311576Smarks * privileged and at least one of the excute bits is set. 7321576Smarks * 7331576Smarks * It would be nice to to this after all writes have 7341576Smarks * been done, but that would still expose the ISUID/ISGID 7351576Smarks * to another app after the partial write is committed. 7361576Smarks */ 7371576Smarks 7381576Smarks mutex_enter(&zp->z_acl_lock); 7391576Smarks if ((zp->z_phys->zp_mode & (S_IXUSR | (S_IXUSR >> 3) | 7401576Smarks (S_IXUSR >> 6))) != 0 && 7411576Smarks (zp->z_phys->zp_mode & (S_ISUID | S_ISGID)) != 0 && 7421576Smarks secpolicy_vnode_setid_retain(cr, 7431576Smarks (zp->z_phys->zp_mode & S_ISUID) != 0 && 7441576Smarks zp->z_phys->zp_uid == 0) != 0) { 7451576Smarks zp->z_phys->zp_mode &= ~(S_ISUID | S_ISGID); 7461576Smarks } 7471576Smarks mutex_exit(&zp->z_acl_lock); 7481576Smarks 749789Sahrens n -= nbytes; 750789Sahrens if (n <= 0) 751789Sahrens break; 752789Sahrens 753789Sahrens /* 754789Sahrens * We have more work ahead of us, so wrap up this transaction 755789Sahrens * and start another. Exact same logic as tx_done below. 756789Sahrens */ 757789Sahrens while ((end_size = zp->z_phys->zp_size) < uio->uio_loffset) { 758789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 759789Sahrens (void) atomic_cas_64(&zp->z_phys->zp_size, end_size, 760789Sahrens uio->uio_loffset); 761789Sahrens } 762789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 763789Sahrens seq = zfs_log_write(zilog, tx, TX_WRITE, zp, woff, tx_bytes, 764789Sahrens ioflag, uio); 765789Sahrens dmu_tx_commit(tx); 766789Sahrens 767789Sahrens /* Pre-fault the next set of pages */ 768789Sahrens zfs_prefault_write(MIN(n, SPA_MAXBLOCKSIZE), uio); 769789Sahrens 770789Sahrens /* 771789Sahrens * Start another transaction. 772789Sahrens */ 773789Sahrens woff = uio->uio_loffset; 774789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 775789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 776789Sahrens dmu_tx_hold_write(tx, zp->z_id, woff, MIN(n, max_blksz)); 777789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 778789Sahrens if (error) { 779789Sahrens dmu_tx_abort(tx); 780789Sahrens if (error == ERESTART && 781789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 782789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 783789Sahrens goto top; 784789Sahrens } 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 8101669Sperrin zfs_range_unlock(zp, 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 827789Sahrens /* 828789Sahrens * Get data to generate a TX_WRITE intent log record. 829789Sahrens */ 830789Sahrens int 8311669Sperrin zfs_get_data(void *arg, lr_write_t *lr, char *buf) 832789Sahrens { 833789Sahrens zfsvfs_t *zfsvfs = arg; 834789Sahrens objset_t *os = zfsvfs->z_os; 835789Sahrens znode_t *zp; 836789Sahrens uint64_t off = lr->lr_offset; 8371669Sperrin rl_t *rl; 838789Sahrens int dlen = lr->lr_length; /* length of user data */ 839789Sahrens int error = 0; 840789Sahrens 841789Sahrens ASSERT(dlen != 0); 842789Sahrens 843789Sahrens /* 8441669Sperrin * Nothing to do if the file has been removed 845789Sahrens */ 846789Sahrens if (zfs_zget(zfsvfs, lr->lr_foid, &zp) != 0) 847789Sahrens return (ENOENT); 8481669Sperrin if (zp->z_reap) { 849789Sahrens VN_RELE(ZTOV(zp)); 850789Sahrens return (ENOENT); 851789Sahrens } 852789Sahrens 853789Sahrens /* 854789Sahrens * Write records come in two flavors: immediate and indirect. 855789Sahrens * For small writes it's cheaper to store the data with the 856789Sahrens * log record (immediate); for large writes it's cheaper to 857789Sahrens * sync the data and get a pointer to it (indirect) so that 858789Sahrens * we don't have to write the data twice. 859789Sahrens */ 8601669Sperrin if (buf != NULL) { /* immediate write */ 8611544Seschrock dmu_buf_t *db; 8621669Sperrin 8631669Sperrin rl = zfs_range_lock(zp, off, dlen, RL_READER); 8641669Sperrin /* test for truncation needs to be done while range locked */ 8651669Sperrin if (off >= zp->z_phys->zp_size) { 8661669Sperrin error = ENOENT; 8671669Sperrin goto out; 8681669Sperrin } 8691544Seschrock VERIFY(0 == dmu_buf_hold(os, lr->lr_foid, off, FTAG, &db)); 8701669Sperrin bcopy((char *)db->db_data + off - db->db_offset, buf, dlen); 8711544Seschrock dmu_buf_rele(db, FTAG); 8721669Sperrin } else { /* indirect write */ 8731669Sperrin uint64_t boff; /* block starting offset */ 8741669Sperrin 875789Sahrens /* 8761669Sperrin * Have to lock the whole block to ensure when it's 8771669Sperrin * written out and it's checksum is being calculated 8781669Sperrin * that no one can change the data. We need to re-check 8791669Sperrin * blocksize after we get the lock in case it's changed! 880789Sahrens */ 8811669Sperrin for (;;) { 882*1941Sperrin if (ISP2(zp->z_blksz)) { 883*1941Sperrin boff = P2ALIGN_TYPED(off, zp->z_blksz, 884*1941Sperrin uint64_t); 885*1941Sperrin } else { 886*1941Sperrin boff = 0; 887*1941Sperrin } 8881669Sperrin dlen = zp->z_blksz; 8891669Sperrin rl = zfs_range_lock(zp, boff, dlen, RL_READER); 8901669Sperrin if (zp->z_blksz == dlen) 8911669Sperrin break; 8921669Sperrin zfs_range_unlock(zp, rl); 8931669Sperrin } 8941669Sperrin /* test for truncation needs to be done while range locked */ 8951669Sperrin if (off >= zp->z_phys->zp_size) { 8961669Sperrin error = ENOENT; 8971669Sperrin goto out; 8981669Sperrin } 899789Sahrens txg_suspend(dmu_objset_pool(os)); 900789Sahrens error = dmu_sync(os, lr->lr_foid, off, &lr->lr_blkoff, 901789Sahrens &lr->lr_blkptr, lr->lr_common.lrc_txg); 902789Sahrens txg_resume(dmu_objset_pool(os)); 903789Sahrens } 9041669Sperrin out: 9051669Sperrin zfs_range_unlock(zp, rl); 906789Sahrens VN_RELE(ZTOV(zp)); 907789Sahrens return (error); 908789Sahrens } 909789Sahrens 910789Sahrens /*ARGSUSED*/ 911789Sahrens static int 912789Sahrens zfs_access(vnode_t *vp, int mode, int flags, cred_t *cr) 913789Sahrens { 914789Sahrens znode_t *zp = VTOZ(vp); 915789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 916789Sahrens int error; 917789Sahrens 918789Sahrens ZFS_ENTER(zfsvfs); 919789Sahrens error = zfs_zaccess_rwx(zp, mode, cr); 920789Sahrens ZFS_EXIT(zfsvfs); 921789Sahrens return (error); 922789Sahrens } 923789Sahrens 924789Sahrens /* 925789Sahrens * Lookup an entry in a directory, or an extended attribute directory. 926789Sahrens * If it exists, return a held vnode reference for it. 927789Sahrens * 928789Sahrens * IN: dvp - vnode of directory to search. 929789Sahrens * nm - name of entry to lookup. 930789Sahrens * pnp - full pathname to lookup [UNUSED]. 931789Sahrens * flags - LOOKUP_XATTR set if looking for an attribute. 932789Sahrens * rdir - root directory vnode [UNUSED]. 933789Sahrens * cr - credentials of caller. 934789Sahrens * 935789Sahrens * OUT: vpp - vnode of located entry, NULL if not found. 936789Sahrens * 937789Sahrens * RETURN: 0 if success 938789Sahrens * error code if failure 939789Sahrens * 940789Sahrens * Timestamps: 941789Sahrens * NA 942789Sahrens */ 943789Sahrens /* ARGSUSED */ 944789Sahrens static int 945789Sahrens zfs_lookup(vnode_t *dvp, char *nm, vnode_t **vpp, struct pathname *pnp, 946789Sahrens int flags, vnode_t *rdir, cred_t *cr) 947789Sahrens { 948789Sahrens 949789Sahrens znode_t *zdp = VTOZ(dvp); 950789Sahrens zfsvfs_t *zfsvfs = zdp->z_zfsvfs; 951789Sahrens int error; 952789Sahrens 953789Sahrens ZFS_ENTER(zfsvfs); 954789Sahrens 955789Sahrens *vpp = NULL; 956789Sahrens 957789Sahrens if (flags & LOOKUP_XATTR) { 958789Sahrens /* 959789Sahrens * We don't allow recursive attributes.. 960789Sahrens * Maybe someday we will. 961789Sahrens */ 962789Sahrens if (zdp->z_phys->zp_flags & ZFS_XATTR) { 963789Sahrens ZFS_EXIT(zfsvfs); 964789Sahrens return (EINVAL); 965789Sahrens } 966789Sahrens 967789Sahrens if (error = zfs_get_xattrdir(VTOZ(dvp), vpp, cr)) { 968789Sahrens ZFS_EXIT(zfsvfs); 969789Sahrens return (error); 970789Sahrens } 971789Sahrens 972789Sahrens /* 973789Sahrens * Do we have permission to get into attribute directory? 974789Sahrens */ 975789Sahrens 976789Sahrens if (error = zfs_zaccess(VTOZ(*vpp), ACE_EXECUTE, cr)) { 977789Sahrens VN_RELE(*vpp); 978789Sahrens } 979789Sahrens 980789Sahrens ZFS_EXIT(zfsvfs); 981789Sahrens return (error); 982789Sahrens } 983789Sahrens 9841512Sek110237 if (dvp->v_type != VDIR) { 9851512Sek110237 ZFS_EXIT(zfsvfs); 9861460Smarks return (ENOTDIR); 9871512Sek110237 } 9881460Smarks 989789Sahrens /* 990789Sahrens * Check accessibility of directory. 991789Sahrens */ 992789Sahrens 993789Sahrens if (error = zfs_zaccess(zdp, ACE_EXECUTE, cr)) { 994789Sahrens ZFS_EXIT(zfsvfs); 995789Sahrens return (error); 996789Sahrens } 997789Sahrens 998789Sahrens if ((error = zfs_dirlook(zdp, nm, vpp)) == 0) { 999789Sahrens 1000789Sahrens /* 1001789Sahrens * Convert device special files 1002789Sahrens */ 1003789Sahrens if (IS_DEVVP(*vpp)) { 1004789Sahrens vnode_t *svp; 1005789Sahrens 1006789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1007789Sahrens VN_RELE(*vpp); 1008789Sahrens if (svp == NULL) 1009789Sahrens error = ENOSYS; 1010789Sahrens else 1011789Sahrens *vpp = svp; 1012789Sahrens } 1013789Sahrens } 1014789Sahrens 1015789Sahrens ZFS_EXIT(zfsvfs); 1016789Sahrens return (error); 1017789Sahrens } 1018789Sahrens 1019789Sahrens /* 1020789Sahrens * Attempt to create a new entry in a directory. If the entry 1021789Sahrens * already exists, truncate the file if permissible, else return 1022789Sahrens * an error. Return the vp of the created or trunc'd file. 1023789Sahrens * 1024789Sahrens * IN: dvp - vnode of directory to put new file entry in. 1025789Sahrens * name - name of new file entry. 1026789Sahrens * vap - attributes of new file. 1027789Sahrens * excl - flag indicating exclusive or non-exclusive mode. 1028789Sahrens * mode - mode to open file with. 1029789Sahrens * cr - credentials of caller. 1030789Sahrens * flag - large file flag [UNUSED]. 1031789Sahrens * 1032789Sahrens * OUT: vpp - vnode of created or trunc'd entry. 1033789Sahrens * 1034789Sahrens * RETURN: 0 if success 1035789Sahrens * error code if failure 1036789Sahrens * 1037789Sahrens * Timestamps: 1038789Sahrens * dvp - ctime|mtime updated if new entry created 1039789Sahrens * vp - ctime|mtime always, atime if new 1040789Sahrens */ 1041789Sahrens /* ARGSUSED */ 1042789Sahrens static int 1043789Sahrens zfs_create(vnode_t *dvp, char *name, vattr_t *vap, vcexcl_t excl, 1044789Sahrens int mode, vnode_t **vpp, cred_t *cr, int flag) 1045789Sahrens { 1046789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1047789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1048789Sahrens zilog_t *zilog = zfsvfs->z_log; 1049789Sahrens uint64_t seq = 0; 1050789Sahrens objset_t *os = zfsvfs->z_os; 1051789Sahrens zfs_dirlock_t *dl; 1052789Sahrens dmu_tx_t *tx; 1053789Sahrens int error; 1054789Sahrens uint64_t zoid; 1055789Sahrens 1056789Sahrens ZFS_ENTER(zfsvfs); 1057789Sahrens 1058789Sahrens top: 1059789Sahrens *vpp = NULL; 1060789Sahrens 1061789Sahrens if ((vap->va_mode & VSVTX) && secpolicy_vnode_stky_modify(cr)) 1062789Sahrens vap->va_mode &= ~VSVTX; 1063789Sahrens 1064789Sahrens if (*name == '\0') { 1065789Sahrens /* 1066789Sahrens * Null component name refers to the directory itself. 1067789Sahrens */ 1068789Sahrens VN_HOLD(dvp); 1069789Sahrens zp = dzp; 1070789Sahrens dl = NULL; 1071789Sahrens error = 0; 1072789Sahrens } else { 1073789Sahrens /* possible VN_HOLD(zp) */ 1074789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, 0)) { 1075789Sahrens if (strcmp(name, "..") == 0) 1076789Sahrens error = EISDIR; 1077789Sahrens ZFS_EXIT(zfsvfs); 1078789Sahrens return (error); 1079789Sahrens } 1080789Sahrens } 1081789Sahrens 1082789Sahrens zoid = zp ? zp->z_id : -1ULL; 1083789Sahrens 1084789Sahrens if (zp == NULL) { 1085789Sahrens /* 1086789Sahrens * Create a new file object and update the directory 1087789Sahrens * to reference it. 1088789Sahrens */ 1089789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 1090789Sahrens goto out; 1091789Sahrens } 1092789Sahrens 1093789Sahrens /* 1094789Sahrens * We only support the creation of regular files in 1095789Sahrens * extended attribute directories. 1096789Sahrens */ 1097789Sahrens if ((dzp->z_phys->zp_flags & ZFS_XATTR) && 1098789Sahrens (vap->va_type != VREG)) { 1099789Sahrens error = EINVAL; 1100789Sahrens goto out; 1101789Sahrens } 1102789Sahrens 1103789Sahrens tx = dmu_tx_create(os); 1104789Sahrens dmu_tx_hold_bonus(tx, DMU_NEW_OBJECT); 1105789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 11061544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 1107789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1108789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1109789Sahrens 0, SPA_MAXBLOCKSIZE); 1110789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1111789Sahrens if (error) { 1112789Sahrens dmu_tx_abort(tx); 1113789Sahrens zfs_dirent_unlock(dl); 1114789Sahrens if (error == ERESTART && 1115789Sahrens zfsvfs->z_assign == TXG_NOWAIT) { 1116789Sahrens txg_wait_open(dmu_objset_pool(os), 0); 1117789Sahrens goto top; 1118789Sahrens } 1119789Sahrens ZFS_EXIT(zfsvfs); 1120789Sahrens return (error); 1121789Sahrens } 1122789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1123789Sahrens ASSERT(zp->z_id == zoid); 1124789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1125789Sahrens seq = zfs_log_create(zilog, tx, TX_CREATE, dzp, zp, name); 1126789Sahrens dmu_tx_commit(tx); 1127789Sahrens } else { 1128789Sahrens /* 1129789Sahrens * A directory entry already exists for this name. 1130789Sahrens */ 1131789Sahrens /* 1132789Sahrens * Can't truncate an existing file if in exclusive mode. 1133789Sahrens */ 1134789Sahrens if (excl == EXCL) { 1135789Sahrens error = EEXIST; 1136789Sahrens goto out; 1137789Sahrens } 1138789Sahrens /* 1139789Sahrens * Can't open a directory for writing. 1140789Sahrens */ 1141789Sahrens if ((ZTOV(zp)->v_type == VDIR) && (mode & S_IWRITE)) { 1142789Sahrens error = EISDIR; 1143789Sahrens goto out; 1144789Sahrens } 1145789Sahrens /* 1146789Sahrens * Verify requested access to file. 1147789Sahrens */ 1148789Sahrens if (mode && (error = zfs_zaccess_rwx(zp, mode, cr))) { 1149789Sahrens goto out; 1150789Sahrens } 1151789Sahrens 1152789Sahrens mutex_enter(&dzp->z_lock); 1153789Sahrens dzp->z_seq++; 1154789Sahrens mutex_exit(&dzp->z_lock); 1155789Sahrens 11561878Smaybee /* 11571878Smaybee * Truncate regular files if requested. 11581878Smaybee */ 11591878Smaybee if ((ZTOV(zp)->v_type == VREG) && 11601878Smaybee (zp->z_phys->zp_size != 0) && 1161789Sahrens (vap->va_mask & AT_SIZE) && (vap->va_size == 0)) { 11621878Smaybee error = zfs_freesp(zp, 0, 0, mode, TRUE); 11631878Smaybee if (error == ERESTART && 11641878Smaybee zfsvfs->z_assign == TXG_NOWAIT) { 11651878Smaybee zfs_dirent_unlock(dl); 11661878Smaybee txg_wait_open(dmu_objset_pool(os), 0); 11671878Smaybee goto top; 1168789Sahrens } 1169789Sahrens } 1170789Sahrens } 1171789Sahrens out: 1172789Sahrens 1173789Sahrens if (dl) 1174789Sahrens zfs_dirent_unlock(dl); 1175789Sahrens 1176789Sahrens if (error) { 1177789Sahrens if (zp) 1178789Sahrens VN_RELE(ZTOV(zp)); 1179789Sahrens } else { 1180789Sahrens *vpp = ZTOV(zp); 1181789Sahrens /* 1182789Sahrens * If vnode is for a device return a specfs vnode instead. 1183789Sahrens */ 1184789Sahrens if (IS_DEVVP(*vpp)) { 1185789Sahrens struct vnode *svp; 1186789Sahrens 1187789Sahrens svp = specvp(*vpp, (*vpp)->v_rdev, (*vpp)->v_type, cr); 1188789Sahrens VN_RELE(*vpp); 1189789Sahrens if (svp == NULL) { 1190789Sahrens error = ENOSYS; 1191789Sahrens } 1192789Sahrens *vpp = svp; 1193789Sahrens } 1194789Sahrens } 1195789Sahrens 1196789Sahrens zil_commit(zilog, seq, 0); 1197789Sahrens 1198789Sahrens ZFS_EXIT(zfsvfs); 1199789Sahrens return (error); 1200789Sahrens } 1201789Sahrens 1202789Sahrens /* 1203789Sahrens * Remove an entry from a directory. 1204789Sahrens * 1205789Sahrens * IN: dvp - vnode of directory to remove entry from. 1206789Sahrens * name - name of entry to remove. 1207789Sahrens * cr - credentials of caller. 1208789Sahrens * 1209789Sahrens * RETURN: 0 if success 1210789Sahrens * error code if failure 1211789Sahrens * 1212789Sahrens * Timestamps: 1213789Sahrens * dvp - ctime|mtime 1214789Sahrens * vp - ctime (if nlink > 0) 1215789Sahrens */ 1216789Sahrens static int 1217789Sahrens zfs_remove(vnode_t *dvp, char *name, cred_t *cr) 1218789Sahrens { 1219789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1220789Sahrens znode_t *xzp = NULL; 1221789Sahrens vnode_t *vp; 1222789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1223789Sahrens zilog_t *zilog = zfsvfs->z_log; 1224789Sahrens uint64_t seq = 0; 1225789Sahrens uint64_t acl_obj, xattr_obj; 1226789Sahrens zfs_dirlock_t *dl; 1227789Sahrens dmu_tx_t *tx; 1228789Sahrens int may_delete_now, delete_now = FALSE; 1229789Sahrens int reaped; 1230789Sahrens int error; 1231789Sahrens 1232789Sahrens ZFS_ENTER(zfsvfs); 1233789Sahrens 1234789Sahrens top: 1235789Sahrens /* 1236789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1237789Sahrens */ 1238789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1239789Sahrens ZFS_EXIT(zfsvfs); 1240789Sahrens return (error); 1241789Sahrens } 1242789Sahrens 1243789Sahrens vp = ZTOV(zp); 1244789Sahrens 1245789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1246789Sahrens goto out; 1247789Sahrens } 1248789Sahrens 1249789Sahrens /* 1250789Sahrens * Need to use rmdir for removing directories. 1251789Sahrens */ 1252789Sahrens if (vp->v_type == VDIR) { 1253789Sahrens error = EPERM; 1254789Sahrens goto out; 1255789Sahrens } 1256789Sahrens 1257789Sahrens vnevent_remove(vp); 1258789Sahrens 12591484Sek110237 dnlc_remove(dvp, name); 12601484Sek110237 1261789Sahrens mutex_enter(&vp->v_lock); 1262789Sahrens may_delete_now = vp->v_count == 1 && !vn_has_cached_data(vp); 1263789Sahrens mutex_exit(&vp->v_lock); 1264789Sahrens 1265789Sahrens /* 1266789Sahrens * We may delete the znode now, or we may put it on the delete queue; 1267789Sahrens * it depends on whether we're the last link, and on whether there are 1268789Sahrens * other holds on the vnode. So we dmu_tx_hold() the right things to 1269789Sahrens * allow for either case. 1270789Sahrens */ 1271789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 12721544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1273789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 1274789Sahrens if (may_delete_now) 1275789Sahrens dmu_tx_hold_free(tx, zp->z_id, 0, DMU_OBJECT_END); 1276789Sahrens 1277789Sahrens /* are there any extended attributes? */ 1278789Sahrens if ((xattr_obj = zp->z_phys->zp_xattr) != 0) { 1279789Sahrens /* 1280789Sahrens * XXX - There is a possibility that the delete 1281789Sahrens * of the parent file could succeed, but then we get 1282789Sahrens * an ENOSPC when we try to delete the xattrs... 1283789Sahrens * so we would need to re-try the deletes periodically 1284789Sahrens */ 1285789Sahrens /* XXX - do we need this if we are deleting? */ 1286789Sahrens dmu_tx_hold_bonus(tx, xattr_obj); 1287789Sahrens } 1288789Sahrens 1289789Sahrens /* are there any additional acls */ 1290789Sahrens if ((acl_obj = zp->z_phys->zp_acl.z_acl_extern_obj) != 0 && 1291789Sahrens may_delete_now) 1292789Sahrens dmu_tx_hold_free(tx, acl_obj, 0, DMU_OBJECT_END); 1293789Sahrens 1294789Sahrens /* charge as an update -- would be nice not to charge at all */ 12951544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1296789Sahrens 1297789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1298789Sahrens if (error) { 1299789Sahrens dmu_tx_abort(tx); 1300789Sahrens zfs_dirent_unlock(dl); 1301789Sahrens VN_RELE(vp); 1302789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1303789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 1304789Sahrens goto top; 1305789Sahrens } 1306789Sahrens ZFS_EXIT(zfsvfs); 1307789Sahrens return (error); 1308789Sahrens } 1309789Sahrens 1310789Sahrens /* 1311789Sahrens * Remove the directory entry. 1312789Sahrens */ 1313789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, &reaped); 1314789Sahrens 1315789Sahrens if (error) { 1316789Sahrens dmu_tx_commit(tx); 1317789Sahrens goto out; 1318789Sahrens } 1319789Sahrens 1320789Sahrens if (reaped) { 1321789Sahrens mutex_enter(&vp->v_lock); 1322789Sahrens delete_now = may_delete_now && 1323789Sahrens vp->v_count == 1 && !vn_has_cached_data(vp) && 1324789Sahrens zp->z_phys->zp_xattr == xattr_obj && 1325789Sahrens zp->z_phys->zp_acl.z_acl_extern_obj == acl_obj; 1326789Sahrens mutex_exit(&vp->v_lock); 1327789Sahrens } 1328789Sahrens 1329789Sahrens if (delete_now) { 1330789Sahrens if (zp->z_phys->zp_xattr) { 1331789Sahrens error = zfs_zget(zfsvfs, zp->z_phys->zp_xattr, &xzp); 1332789Sahrens ASSERT3U(error, ==, 0); 1333789Sahrens ASSERT3U(xzp->z_phys->zp_links, ==, 2); 1334789Sahrens dmu_buf_will_dirty(xzp->z_dbuf, tx); 1335789Sahrens mutex_enter(&xzp->z_lock); 1336789Sahrens xzp->z_reap = 1; 1337789Sahrens xzp->z_phys->zp_links = 0; 1338789Sahrens mutex_exit(&xzp->z_lock); 1339789Sahrens zfs_dq_add(xzp, tx); 1340789Sahrens zp->z_phys->zp_xattr = 0; /* probably unnecessary */ 1341789Sahrens } 1342789Sahrens mutex_enter(&zp->z_lock); 1343789Sahrens mutex_enter(&vp->v_lock); 1344789Sahrens vp->v_count--; 1345789Sahrens ASSERT3U(vp->v_count, ==, 0); 1346789Sahrens mutex_exit(&vp->v_lock); 1347789Sahrens zp->z_active = 0; 1348789Sahrens mutex_exit(&zp->z_lock); 1349789Sahrens zfs_znode_delete(zp, tx); 1350789Sahrens VFS_RELE(zfsvfs->z_vfs); 1351789Sahrens } else if (reaped) { 1352789Sahrens zfs_dq_add(zp, tx); 1353789Sahrens } 1354789Sahrens 1355789Sahrens seq = zfs_log_remove(zilog, tx, TX_REMOVE, dzp, name); 1356789Sahrens 1357789Sahrens dmu_tx_commit(tx); 1358789Sahrens out: 1359789Sahrens zfs_dirent_unlock(dl); 1360789Sahrens 1361789Sahrens if (!delete_now) { 1362789Sahrens VN_RELE(vp); 1363789Sahrens } else if (xzp) { 1364789Sahrens /* this rele delayed to prevent nesting transactions */ 1365789Sahrens VN_RELE(ZTOV(xzp)); 1366789Sahrens } 1367789Sahrens 1368789Sahrens zil_commit(zilog, seq, 0); 1369789Sahrens 1370789Sahrens ZFS_EXIT(zfsvfs); 1371789Sahrens return (error); 1372789Sahrens } 1373789Sahrens 1374789Sahrens /* 1375789Sahrens * Create a new directory and insert it into dvp using the name 1376789Sahrens * provided. Return a pointer to the inserted directory. 1377789Sahrens * 1378789Sahrens * IN: dvp - vnode of directory to add subdir to. 1379789Sahrens * dirname - name of new directory. 1380789Sahrens * vap - attributes of new directory. 1381789Sahrens * cr - credentials of caller. 1382789Sahrens * 1383789Sahrens * OUT: vpp - vnode of created directory. 1384789Sahrens * 1385789Sahrens * RETURN: 0 if success 1386789Sahrens * error code if failure 1387789Sahrens * 1388789Sahrens * Timestamps: 1389789Sahrens * dvp - ctime|mtime updated 1390789Sahrens * vp - ctime|mtime|atime updated 1391789Sahrens */ 1392789Sahrens static int 1393789Sahrens zfs_mkdir(vnode_t *dvp, char *dirname, vattr_t *vap, vnode_t **vpp, cred_t *cr) 1394789Sahrens { 1395789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 1396789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1397789Sahrens zilog_t *zilog = zfsvfs->z_log; 1398789Sahrens uint64_t seq = 0; 1399789Sahrens zfs_dirlock_t *dl; 1400789Sahrens uint64_t zoid = 0; 1401789Sahrens dmu_tx_t *tx; 1402789Sahrens int error; 1403789Sahrens 1404789Sahrens ASSERT(vap->va_type == VDIR); 1405789Sahrens 1406789Sahrens ZFS_ENTER(zfsvfs); 1407789Sahrens 1408789Sahrens if (dzp->z_phys->zp_flags & ZFS_XATTR) { 1409789Sahrens ZFS_EXIT(zfsvfs); 1410789Sahrens return (EINVAL); 1411789Sahrens } 1412789Sahrens top: 1413789Sahrens *vpp = NULL; 1414789Sahrens 1415789Sahrens /* 1416789Sahrens * First make sure the new directory doesn't exist. 1417789Sahrens */ 1418789Sahrens if (error = zfs_dirent_lock(&dl, dzp, dirname, &zp, ZNEW)) { 1419789Sahrens ZFS_EXIT(zfsvfs); 1420789Sahrens return (error); 1421789Sahrens } 1422789Sahrens 14231231Smarks if (error = zfs_zaccess(dzp, ACE_ADD_SUBDIRECTORY, cr)) { 14241231Smarks zfs_dirent_unlock(dl); 14251231Smarks ZFS_EXIT(zfsvfs); 14261231Smarks return (error); 14271231Smarks } 14281231Smarks 1429789Sahrens /* 1430789Sahrens * Add a new entry to the directory. 1431789Sahrens */ 1432789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 14331544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, dirname); 14341544Seschrock dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); 1435789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 1436789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 1437789Sahrens 0, SPA_MAXBLOCKSIZE); 1438789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1439789Sahrens if (error) { 1440789Sahrens dmu_tx_abort(tx); 1441789Sahrens zfs_dirent_unlock(dl); 1442789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1443789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 1444789Sahrens goto top; 1445789Sahrens } 1446789Sahrens ZFS_EXIT(zfsvfs); 1447789Sahrens return (error); 1448789Sahrens } 1449789Sahrens 1450789Sahrens /* 1451789Sahrens * Create new node. 1452789Sahrens */ 1453789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 1454789Sahrens 1455789Sahrens /* 1456789Sahrens * Now put new name in parent dir. 1457789Sahrens */ 1458789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 1459789Sahrens 1460789Sahrens *vpp = ZTOV(zp); 1461789Sahrens 1462789Sahrens seq = zfs_log_create(zilog, tx, TX_MKDIR, dzp, zp, dirname); 1463789Sahrens dmu_tx_commit(tx); 1464789Sahrens 1465789Sahrens zfs_dirent_unlock(dl); 1466789Sahrens 1467789Sahrens zil_commit(zilog, seq, 0); 1468789Sahrens 1469789Sahrens ZFS_EXIT(zfsvfs); 1470789Sahrens return (0); 1471789Sahrens } 1472789Sahrens 1473789Sahrens /* 1474789Sahrens * Remove a directory subdir entry. If the current working 1475789Sahrens * directory is the same as the subdir to be removed, the 1476789Sahrens * remove will fail. 1477789Sahrens * 1478789Sahrens * IN: dvp - vnode of directory to remove from. 1479789Sahrens * name - name of directory to be removed. 1480789Sahrens * cwd - vnode of current working directory. 1481789Sahrens * cr - credentials of caller. 1482789Sahrens * 1483789Sahrens * RETURN: 0 if success 1484789Sahrens * error code if failure 1485789Sahrens * 1486789Sahrens * Timestamps: 1487789Sahrens * dvp - ctime|mtime updated 1488789Sahrens */ 1489789Sahrens static int 1490789Sahrens zfs_rmdir(vnode_t *dvp, char *name, vnode_t *cwd, cred_t *cr) 1491789Sahrens { 1492789Sahrens znode_t *dzp = VTOZ(dvp); 1493789Sahrens znode_t *zp; 1494789Sahrens vnode_t *vp; 1495789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 1496789Sahrens zilog_t *zilog = zfsvfs->z_log; 1497789Sahrens uint64_t seq = 0; 1498789Sahrens zfs_dirlock_t *dl; 1499789Sahrens dmu_tx_t *tx; 1500789Sahrens int error; 1501789Sahrens 1502789Sahrens ZFS_ENTER(zfsvfs); 1503789Sahrens 1504789Sahrens top: 1505789Sahrens zp = NULL; 1506789Sahrens 1507789Sahrens /* 1508789Sahrens * Attempt to lock directory; fail if entry doesn't exist. 1509789Sahrens */ 1510789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZEXISTS)) { 1511789Sahrens ZFS_EXIT(zfsvfs); 1512789Sahrens return (error); 1513789Sahrens } 1514789Sahrens 1515789Sahrens vp = ZTOV(zp); 1516789Sahrens 1517789Sahrens if (error = zfs_zaccess_delete(dzp, zp, cr)) { 1518789Sahrens goto out; 1519789Sahrens } 1520789Sahrens 1521789Sahrens if (vp->v_type != VDIR) { 1522789Sahrens error = ENOTDIR; 1523789Sahrens goto out; 1524789Sahrens } 1525789Sahrens 1526789Sahrens if (vp == cwd) { 1527789Sahrens error = EINVAL; 1528789Sahrens goto out; 1529789Sahrens } 1530789Sahrens 1531789Sahrens vnevent_rmdir(vp); 1532789Sahrens 1533789Sahrens /* 1534789Sahrens * Grab a lock on the parent pointer make sure we play well 1535789Sahrens * with the treewalk and directory rename code. 1536789Sahrens */ 1537789Sahrens rw_enter(&zp->z_parent_lock, RW_WRITER); 1538789Sahrens 1539789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 15401544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, FALSE, name); 1541789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 15421544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 1543789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 1544789Sahrens if (error) { 1545789Sahrens dmu_tx_abort(tx); 1546789Sahrens rw_exit(&zp->z_parent_lock); 1547789Sahrens zfs_dirent_unlock(dl); 1548789Sahrens VN_RELE(vp); 1549789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 1550789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 1551789Sahrens goto top; 1552789Sahrens } 1553789Sahrens ZFS_EXIT(zfsvfs); 1554789Sahrens return (error); 1555789Sahrens } 1556789Sahrens 1557789Sahrens error = zfs_link_destroy(dl, zp, tx, 0, NULL); 1558789Sahrens 1559789Sahrens if (error == 0) 1560789Sahrens seq = zfs_log_remove(zilog, tx, TX_RMDIR, dzp, name); 1561789Sahrens 1562789Sahrens dmu_tx_commit(tx); 1563789Sahrens 1564789Sahrens rw_exit(&zp->z_parent_lock); 1565789Sahrens out: 1566789Sahrens zfs_dirent_unlock(dl); 1567789Sahrens 1568789Sahrens VN_RELE(vp); 1569789Sahrens 1570789Sahrens zil_commit(zilog, seq, 0); 1571789Sahrens 1572789Sahrens ZFS_EXIT(zfsvfs); 1573789Sahrens return (error); 1574789Sahrens } 1575789Sahrens 1576789Sahrens /* 1577789Sahrens * Read as many directory entries as will fit into the provided 1578789Sahrens * buffer from the given directory cursor position (specified in 1579789Sahrens * the uio structure. 1580789Sahrens * 1581789Sahrens * IN: vp - vnode of directory to read. 1582789Sahrens * uio - structure supplying read location, range info, 1583789Sahrens * and return buffer. 1584789Sahrens * cr - credentials of caller. 1585789Sahrens * 1586789Sahrens * OUT: uio - updated offset and range, buffer filled. 1587789Sahrens * eofp - set to true if end-of-file detected. 1588789Sahrens * 1589789Sahrens * RETURN: 0 if success 1590789Sahrens * error code if failure 1591789Sahrens * 1592789Sahrens * Timestamps: 1593789Sahrens * vp - atime updated 1594789Sahrens * 1595789Sahrens * Note that the low 4 bits of the cookie returned by zap is always zero. 1596789Sahrens * This allows us to use the low range for "special" directory entries: 1597789Sahrens * We use 0 for '.', and 1 for '..'. If this is the root of the filesystem, 1598789Sahrens * we use the offset 2 for the '.zfs' directory. 1599789Sahrens */ 1600789Sahrens /* ARGSUSED */ 1601789Sahrens static int 1602789Sahrens zfs_readdir(vnode_t *vp, uio_t *uio, cred_t *cr, int *eofp) 1603789Sahrens { 1604789Sahrens znode_t *zp = VTOZ(vp); 1605789Sahrens iovec_t *iovp; 1606789Sahrens dirent64_t *odp; 1607789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1608869Sperrin objset_t *os; 1609789Sahrens caddr_t outbuf; 1610789Sahrens size_t bufsize; 1611789Sahrens zap_cursor_t zc; 1612789Sahrens zap_attribute_t zap; 1613789Sahrens uint_t bytes_wanted; 1614789Sahrens ushort_t this_reclen; 1615789Sahrens uint64_t offset; /* must be unsigned; checks for < 1 */ 1616789Sahrens off64_t *next; 1617789Sahrens int local_eof; 1618869Sperrin int outcount; 1619869Sperrin int error; 1620869Sperrin uint8_t prefetch; 1621789Sahrens 1622789Sahrens ZFS_ENTER(zfsvfs); 1623789Sahrens 1624789Sahrens /* 1625789Sahrens * If we are not given an eof variable, 1626789Sahrens * use a local one. 1627789Sahrens */ 1628789Sahrens if (eofp == NULL) 1629789Sahrens eofp = &local_eof; 1630789Sahrens 1631789Sahrens /* 1632789Sahrens * Check for valid iov_len. 1633789Sahrens */ 1634789Sahrens if (uio->uio_iov->iov_len <= 0) { 1635789Sahrens ZFS_EXIT(zfsvfs); 1636789Sahrens return (EINVAL); 1637789Sahrens } 1638789Sahrens 1639789Sahrens /* 1640789Sahrens * Quit if directory has been removed (posix) 1641789Sahrens */ 1642789Sahrens if ((*eofp = zp->z_reap) != 0) { 1643789Sahrens ZFS_EXIT(zfsvfs); 1644789Sahrens return (0); 1645789Sahrens } 1646789Sahrens 1647869Sperrin error = 0; 1648869Sperrin os = zfsvfs->z_os; 1649869Sperrin offset = uio->uio_loffset; 1650869Sperrin prefetch = zp->z_zn_prefetch; 1651869Sperrin 1652789Sahrens /* 1653789Sahrens * Initialize the iterator cursor. 1654789Sahrens */ 1655789Sahrens if (offset <= 3) { 1656789Sahrens /* 1657789Sahrens * Start iteration from the beginning of the directory. 1658789Sahrens */ 1659869Sperrin zap_cursor_init(&zc, os, zp->z_id); 1660789Sahrens } else { 1661789Sahrens /* 1662789Sahrens * The offset is a serialized cursor. 1663789Sahrens */ 1664869Sperrin zap_cursor_init_serialized(&zc, os, zp->z_id, offset); 1665789Sahrens } 1666789Sahrens 1667789Sahrens /* 1668789Sahrens * Get space to change directory entries into fs independent format. 1669789Sahrens */ 1670789Sahrens iovp = uio->uio_iov; 1671789Sahrens bytes_wanted = iovp->iov_len; 1672789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) { 1673789Sahrens bufsize = bytes_wanted; 1674789Sahrens outbuf = kmem_alloc(bufsize, KM_SLEEP); 1675789Sahrens odp = (struct dirent64 *)outbuf; 1676789Sahrens } else { 1677789Sahrens bufsize = bytes_wanted; 1678789Sahrens odp = (struct dirent64 *)iovp->iov_base; 1679789Sahrens } 1680789Sahrens 1681789Sahrens /* 1682789Sahrens * Transform to file-system independent format 1683789Sahrens */ 1684789Sahrens outcount = 0; 1685789Sahrens while (outcount < bytes_wanted) { 1686789Sahrens /* 1687789Sahrens * Special case `.', `..', and `.zfs'. 1688789Sahrens */ 1689789Sahrens if (offset == 0) { 1690789Sahrens (void) strcpy(zap.za_name, "."); 1691789Sahrens zap.za_first_integer = zp->z_id; 1692789Sahrens this_reclen = DIRENT64_RECLEN(1); 1693789Sahrens } else if (offset == 1) { 1694789Sahrens (void) strcpy(zap.za_name, ".."); 1695789Sahrens zap.za_first_integer = zp->z_phys->zp_parent; 1696789Sahrens this_reclen = DIRENT64_RECLEN(2); 1697789Sahrens } else if (offset == 2 && zfs_show_ctldir(zp)) { 1698789Sahrens (void) strcpy(zap.za_name, ZFS_CTLDIR_NAME); 1699789Sahrens zap.za_first_integer = ZFSCTL_INO_ROOT; 1700789Sahrens this_reclen = 1701789Sahrens DIRENT64_RECLEN(sizeof (ZFS_CTLDIR_NAME) - 1); 1702789Sahrens } else { 1703789Sahrens /* 1704789Sahrens * Grab next entry. 1705789Sahrens */ 1706789Sahrens if (error = zap_cursor_retrieve(&zc, &zap)) { 1707789Sahrens if ((*eofp = (error == ENOENT)) != 0) 1708789Sahrens break; 1709789Sahrens else 1710789Sahrens goto update; 1711789Sahrens } 1712789Sahrens 1713789Sahrens if (zap.za_integer_length != 8 || 1714789Sahrens zap.za_num_integers != 1) { 1715789Sahrens cmn_err(CE_WARN, "zap_readdir: bad directory " 1716789Sahrens "entry, obj = %lld, offset = %lld\n", 1717789Sahrens (u_longlong_t)zp->z_id, 1718789Sahrens (u_longlong_t)offset); 1719789Sahrens error = ENXIO; 1720789Sahrens goto update; 1721789Sahrens } 1722789Sahrens this_reclen = DIRENT64_RECLEN(strlen(zap.za_name)); 1723789Sahrens } 1724789Sahrens 1725789Sahrens /* 1726789Sahrens * Will this entry fit in the buffer? 1727789Sahrens */ 1728789Sahrens if (outcount + this_reclen > bufsize) { 1729789Sahrens /* 1730789Sahrens * Did we manage to fit anything in the buffer? 1731789Sahrens */ 1732789Sahrens if (!outcount) { 1733789Sahrens error = EINVAL; 1734789Sahrens goto update; 1735789Sahrens } 1736789Sahrens break; 1737789Sahrens } 1738789Sahrens /* 1739789Sahrens * Add this entry: 1740789Sahrens */ 1741789Sahrens odp->d_ino = (ino64_t)zap.za_first_integer; 1742789Sahrens odp->d_reclen = (ushort_t)this_reclen; 1743789Sahrens /* NOTE: d_off is the offset for the *next* entry */ 1744789Sahrens next = &(odp->d_off); 1745789Sahrens (void) strncpy(odp->d_name, zap.za_name, 1746789Sahrens DIRENT64_NAMELEN(this_reclen)); 1747789Sahrens outcount += this_reclen; 1748789Sahrens odp = (dirent64_t *)((intptr_t)odp + this_reclen); 1749789Sahrens 1750789Sahrens ASSERT(outcount <= bufsize); 1751789Sahrens 1752789Sahrens /* Prefetch znode */ 1753869Sperrin if (prefetch) 1754869Sperrin dmu_prefetch(os, zap.za_first_integer, 0, 0); 1755789Sahrens 1756789Sahrens /* 1757789Sahrens * Move to the next entry, fill in the previous offset. 1758789Sahrens */ 1759789Sahrens if (offset > 2 || (offset == 2 && !zfs_show_ctldir(zp))) { 1760789Sahrens zap_cursor_advance(&zc); 1761789Sahrens offset = zap_cursor_serialize(&zc); 1762789Sahrens } else { 1763789Sahrens offset += 1; 1764789Sahrens } 1765789Sahrens *next = offset; 1766789Sahrens } 1767869Sperrin zp->z_zn_prefetch = B_FALSE; /* a lookup will re-enable pre-fetching */ 1768789Sahrens 1769789Sahrens if (uio->uio_segflg == UIO_SYSSPACE && uio->uio_iovcnt == 1) { 1770789Sahrens iovp->iov_base += outcount; 1771789Sahrens iovp->iov_len -= outcount; 1772789Sahrens uio->uio_resid -= outcount; 1773789Sahrens } else if (error = uiomove(outbuf, (long)outcount, UIO_READ, uio)) { 1774789Sahrens /* 1775789Sahrens * Reset the pointer. 1776789Sahrens */ 1777789Sahrens offset = uio->uio_loffset; 1778789Sahrens } 1779789Sahrens 1780789Sahrens update: 1781885Sahrens zap_cursor_fini(&zc); 1782789Sahrens if (uio->uio_segflg != UIO_SYSSPACE || uio->uio_iovcnt != 1) 1783789Sahrens kmem_free(outbuf, bufsize); 1784789Sahrens 1785789Sahrens if (error == ENOENT) 1786789Sahrens error = 0; 1787789Sahrens 1788789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 1789789Sahrens 1790789Sahrens uio->uio_loffset = offset; 1791789Sahrens ZFS_EXIT(zfsvfs); 1792789Sahrens return (error); 1793789Sahrens } 1794789Sahrens 1795789Sahrens static int 1796789Sahrens zfs_fsync(vnode_t *vp, int syncflag, cred_t *cr) 1797789Sahrens { 1798789Sahrens znode_t *zp = VTOZ(vp); 1799789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1800789Sahrens 18011773Seschrock /* 18021773Seschrock * Regardless of whether this is required for standards conformance, 18031773Seschrock * this is the logical behavior when fsync() is called on a file with 18041773Seschrock * dirty pages. We use B_ASYNC since the ZIL transactions are already 18051773Seschrock * going to be pushed out as part of the zil_commit(). 18061773Seschrock */ 18071773Seschrock if (vn_has_cached_data(vp) && !(syncflag & FNODSYNC) && 18081773Seschrock (vp->v_type == VREG) && !(IS_SWAPVP(vp))) 18091773Seschrock (void) VOP_PUTPAGE(vp, (offset_t)0, (size_t)0, B_ASYNC, cr); 18101773Seschrock 1811789Sahrens ZFS_ENTER(zfsvfs); 1812789Sahrens zil_commit(zfsvfs->z_log, zp->z_last_itx, FSYNC); 1813789Sahrens ZFS_EXIT(zfsvfs); 1814789Sahrens return (0); 1815789Sahrens } 1816789Sahrens 1817789Sahrens /* 1818789Sahrens * Get the requested file attributes and place them in the provided 1819789Sahrens * vattr structure. 1820789Sahrens * 1821789Sahrens * IN: vp - vnode of file. 1822789Sahrens * vap - va_mask identifies requested attributes. 1823789Sahrens * flags - [UNUSED] 1824789Sahrens * cr - credentials of caller. 1825789Sahrens * 1826789Sahrens * OUT: vap - attribute values. 1827789Sahrens * 1828789Sahrens * RETURN: 0 (always succeeds) 1829789Sahrens */ 1830789Sahrens /* ARGSUSED */ 1831789Sahrens static int 1832789Sahrens zfs_getattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr) 1833789Sahrens { 1834789Sahrens znode_t *zp = VTOZ(vp); 1835789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1836789Sahrens znode_phys_t *pzp = zp->z_phys; 1837789Sahrens int error; 1838789Sahrens 1839789Sahrens ZFS_ENTER(zfsvfs); 1840789Sahrens 1841789Sahrens /* 1842789Sahrens * Return all attributes. It's cheaper to provide the answer 1843789Sahrens * than to determine whether we were asked the question. 1844789Sahrens */ 1845789Sahrens mutex_enter(&zp->z_lock); 1846789Sahrens 1847789Sahrens vap->va_type = vp->v_type; 1848789Sahrens vap->va_mode = pzp->zp_mode & MODEMASK; 1849789Sahrens vap->va_uid = zp->z_phys->zp_uid; 1850789Sahrens vap->va_gid = zp->z_phys->zp_gid; 1851789Sahrens vap->va_fsid = zp->z_zfsvfs->z_vfs->vfs_dev; 1852789Sahrens vap->va_nodeid = zp->z_id; 1853789Sahrens vap->va_nlink = MIN(pzp->zp_links, UINT32_MAX); /* nlink_t limit! */ 1854789Sahrens vap->va_size = pzp->zp_size; 18551816Smarks vap->va_rdev = vp->v_rdev; 1856789Sahrens vap->va_seq = zp->z_seq; 1857789Sahrens 1858789Sahrens ZFS_TIME_DECODE(&vap->va_atime, pzp->zp_atime); 1859789Sahrens ZFS_TIME_DECODE(&vap->va_mtime, pzp->zp_mtime); 1860789Sahrens ZFS_TIME_DECODE(&vap->va_ctime, pzp->zp_ctime); 1861789Sahrens 1862789Sahrens /* 1863905Smarks * If ACL is trivial don't bother looking for ACE_READ_ATTRIBUTES. 1864905Smarks * Also, if we are the owner don't bother, since owner should 1865905Smarks * always be allowed to read basic attributes of file. 1866789Sahrens */ 1867905Smarks if (!(zp->z_phys->zp_flags & ZFS_ACL_TRIVIAL) && 1868905Smarks (zp->z_phys->zp_uid != crgetuid(cr))) { 1869905Smarks if (error = zfs_zaccess(zp, ACE_READ_ATTRIBUTES, cr)) { 1870789Sahrens mutex_exit(&zp->z_lock); 1871789Sahrens ZFS_EXIT(zfsvfs); 1872789Sahrens return (error); 1873789Sahrens } 1874789Sahrens } 1875789Sahrens 1876789Sahrens mutex_exit(&zp->z_lock); 1877789Sahrens 1878789Sahrens dmu_object_size_from_db(zp->z_dbuf, &vap->va_blksize, &vap->va_nblocks); 1879789Sahrens 1880789Sahrens if (zp->z_blksz == 0) { 1881789Sahrens /* 1882789Sahrens * Block size hasn't been set; suggest maximal I/O transfers. 1883789Sahrens */ 1884789Sahrens vap->va_blksize = zfsvfs->z_max_blksz; 1885789Sahrens } 1886789Sahrens 1887789Sahrens ZFS_EXIT(zfsvfs); 1888789Sahrens return (0); 1889789Sahrens } 1890789Sahrens 1891789Sahrens /* 1892789Sahrens * Set the file attributes to the values contained in the 1893789Sahrens * vattr structure. 1894789Sahrens * 1895789Sahrens * IN: vp - vnode of file to be modified. 1896789Sahrens * vap - new attribute values. 1897789Sahrens * flags - ATTR_UTIME set if non-default time values provided. 1898789Sahrens * cr - credentials of caller. 1899789Sahrens * 1900789Sahrens * RETURN: 0 if success 1901789Sahrens * error code if failure 1902789Sahrens * 1903789Sahrens * Timestamps: 1904789Sahrens * vp - ctime updated, mtime updated if size changed. 1905789Sahrens */ 1906789Sahrens /* ARGSUSED */ 1907789Sahrens static int 1908789Sahrens zfs_setattr(vnode_t *vp, vattr_t *vap, int flags, cred_t *cr, 1909789Sahrens caller_context_t *ct) 1910789Sahrens { 1911789Sahrens struct znode *zp = VTOZ(vp); 1912789Sahrens znode_phys_t *pzp = zp->z_phys; 1913789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 1914789Sahrens zilog_t *zilog = zfsvfs->z_log; 1915789Sahrens uint64_t seq = 0; 1916789Sahrens dmu_tx_t *tx; 19171878Smaybee vattr_t oldva; 1918789Sahrens uint_t mask = vap->va_mask; 19191878Smaybee uint_t saved_mask; 19201115Smarks int trim_mask = FALSE; 1921789Sahrens uint64_t new_mode; 19221231Smarks znode_t *attrzp; 1923789Sahrens int need_policy = FALSE; 1924789Sahrens int err; 1925789Sahrens 1926789Sahrens if (mask == 0) 1927789Sahrens return (0); 1928789Sahrens 1929789Sahrens if (mask & AT_NOSET) 1930789Sahrens return (EINVAL); 1931789Sahrens 1932789Sahrens if (mask & AT_SIZE && vp->v_type == VDIR) 1933789Sahrens return (EISDIR); 1934789Sahrens 19351394Smarks if (mask & AT_SIZE && vp->v_type != VREG && vp->v_type != VFIFO) 19361308Smarks return (EINVAL); 19371308Smarks 1938789Sahrens ZFS_ENTER(zfsvfs); 1939789Sahrens 1940789Sahrens top: 19411231Smarks attrzp = NULL; 1942789Sahrens 1943789Sahrens if (zfsvfs->z_vfs->vfs_flag & VFS_RDONLY) { 1944789Sahrens ZFS_EXIT(zfsvfs); 1945789Sahrens return (EROFS); 1946789Sahrens } 1947789Sahrens 1948789Sahrens /* 1949789Sahrens * First validate permissions 1950789Sahrens */ 1951789Sahrens 1952789Sahrens if (mask & AT_SIZE) { 1953789Sahrens err = zfs_zaccess(zp, ACE_WRITE_DATA, cr); 1954789Sahrens if (err) { 1955789Sahrens ZFS_EXIT(zfsvfs); 1956789Sahrens return (err); 1957789Sahrens } 19581878Smaybee /* 19591878Smaybee * XXX - Note, we are not providing any open 19601878Smaybee * mode flags here (like FNDELAY), so we may 19611878Smaybee * block if there are locks present... this 19621878Smaybee * should be addressed in openat(). 19631878Smaybee */ 19641878Smaybee do { 19651878Smaybee if (err == ERESTART) 19661878Smaybee txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 19671878Smaybee err = zfs_freesp(zp, vap->va_size, 0, 0, FALSE); 19681878Smaybee } while (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 19691878Smaybee if (err) { 19701878Smaybee ZFS_EXIT(zfsvfs); 19711878Smaybee return (err); 19721878Smaybee } 1973789Sahrens } 1974789Sahrens 1975789Sahrens if (mask & (AT_ATIME|AT_MTIME)) 1976789Sahrens need_policy = zfs_zaccess_v4_perm(zp, ACE_WRITE_ATTRIBUTES, cr); 1977789Sahrens 1978789Sahrens if (mask & (AT_UID|AT_GID)) { 1979789Sahrens int idmask = (mask & (AT_UID|AT_GID)); 1980789Sahrens int take_owner; 1981789Sahrens int take_group; 1982789Sahrens 1983789Sahrens /* 1984913Smarks * NOTE: even if a new mode is being set, 1985913Smarks * we may clear S_ISUID/S_ISGID bits. 1986913Smarks */ 1987913Smarks 1988913Smarks if (!(mask & AT_MODE)) 1989913Smarks vap->va_mode = pzp->zp_mode; 1990913Smarks 1991913Smarks /* 1992789Sahrens * Take ownership or chgrp to group we are a member of 1993789Sahrens */ 1994789Sahrens 1995789Sahrens take_owner = (mask & AT_UID) && (vap->va_uid == crgetuid(cr)); 1996789Sahrens take_group = (mask & AT_GID) && groupmember(vap->va_gid, cr); 1997789Sahrens 1998789Sahrens /* 1999789Sahrens * If both AT_UID and AT_GID are set then take_owner and 2000789Sahrens * take_group must both be set in order to allow taking 2001789Sahrens * ownership. 2002789Sahrens * 2003789Sahrens * Otherwise, send the check through secpolicy_vnode_setattr() 2004789Sahrens * 2005789Sahrens */ 2006789Sahrens 2007789Sahrens if (((idmask == (AT_UID|AT_GID)) && take_owner && take_group) || 2008789Sahrens ((idmask == AT_UID) && take_owner) || 2009789Sahrens ((idmask == AT_GID) && take_group)) { 2010789Sahrens if (zfs_zaccess_v4_perm(zp, ACE_WRITE_OWNER, cr) == 0) { 2011789Sahrens /* 2012789Sahrens * Remove setuid/setgid for non-privileged users 2013789Sahrens */ 20141115Smarks secpolicy_setid_clear(vap, cr); 20151115Smarks trim_mask = TRUE; 20161115Smarks saved_mask = vap->va_mask; 2017789Sahrens } else { 2018789Sahrens need_policy = TRUE; 2019789Sahrens } 2020789Sahrens } else { 2021789Sahrens need_policy = TRUE; 2022789Sahrens } 2023789Sahrens } 2024789Sahrens 2025789Sahrens if (mask & AT_MODE) 2026789Sahrens need_policy = TRUE; 2027789Sahrens 2028789Sahrens if (need_policy) { 2029789Sahrens mutex_enter(&zp->z_lock); 2030789Sahrens oldva.va_mode = pzp->zp_mode; 2031789Sahrens oldva.va_uid = zp->z_phys->zp_uid; 2032789Sahrens oldva.va_gid = zp->z_phys->zp_gid; 2033789Sahrens mutex_exit(&zp->z_lock); 20341115Smarks 20351115Smarks /* 20361115Smarks * If trim_mask is set then take ownership 20371115Smarks * has been granted. In that case remove 20381115Smarks * UID|GID from mask so that 20391115Smarks * secpolicy_vnode_setattr() doesn't revoke it. 20401115Smarks */ 20411115Smarks if (trim_mask) 20421115Smarks vap->va_mask &= ~(AT_UID|AT_GID); 20431115Smarks 2044789Sahrens err = secpolicy_vnode_setattr(cr, vp, vap, &oldva, flags, 2045789Sahrens (int (*)(void *, int, cred_t *))zfs_zaccess_rwx, zp); 2046789Sahrens if (err) { 2047789Sahrens ZFS_EXIT(zfsvfs); 2048789Sahrens return (err); 2049789Sahrens } 20501115Smarks 20511115Smarks if (trim_mask) 20521115Smarks vap->va_mask |= (saved_mask & (AT_UID|AT_GID)); 2053789Sahrens } 2054789Sahrens 2055789Sahrens /* 2056789Sahrens * secpolicy_vnode_setattr, or take ownership may have 2057789Sahrens * changed va_mask 2058789Sahrens */ 2059789Sahrens mask = vap->va_mask; 2060789Sahrens 2061789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2062789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2063789Sahrens 2064789Sahrens if (mask & AT_MODE) { 20651576Smarks uint64_t pmode = pzp->zp_mode; 20661576Smarks 20671576Smarks new_mode = (pmode & S_IFMT) | (vap->va_mode & ~S_IFMT); 2068789Sahrens 2069789Sahrens if (zp->z_phys->zp_acl.z_acl_extern_obj) 2070789Sahrens dmu_tx_hold_write(tx, 2071789Sahrens pzp->zp_acl.z_acl_extern_obj, 0, SPA_MAXBLOCKSIZE); 2072789Sahrens else 2073789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 2074789Sahrens 0, ZFS_ACL_SIZE(MAX_ACL_SIZE)); 2075789Sahrens } 2076789Sahrens 20771231Smarks if ((mask & (AT_UID | AT_GID)) && zp->z_phys->zp_xattr != 0) { 20781231Smarks err = zfs_zget(zp->z_zfsvfs, zp->z_phys->zp_xattr, &attrzp); 20791231Smarks if (err) { 20801231Smarks dmu_tx_abort(tx); 20811231Smarks ZFS_EXIT(zfsvfs); 20821231Smarks return (err); 20831231Smarks } 20841231Smarks dmu_tx_hold_bonus(tx, attrzp->z_id); 20851231Smarks } 20861231Smarks 2087789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2088789Sahrens if (err) { 20891231Smarks if (attrzp) 20901231Smarks VN_RELE(ZTOV(attrzp)); 2091789Sahrens dmu_tx_abort(tx); 2092789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2093789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 2094789Sahrens goto top; 2095789Sahrens } 2096789Sahrens ZFS_EXIT(zfsvfs); 2097789Sahrens return (err); 2098789Sahrens } 2099789Sahrens 2100789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2101789Sahrens 2102789Sahrens /* 2103789Sahrens * Set each attribute requested. 2104789Sahrens * We group settings according to the locks they need to acquire. 2105789Sahrens * 2106789Sahrens * Note: you cannot set ctime directly, although it will be 2107789Sahrens * updated as a side-effect of calling this function. 2108789Sahrens */ 2109789Sahrens 2110789Sahrens mutex_enter(&zp->z_lock); 2111789Sahrens 2112789Sahrens if (mask & AT_MODE) { 2113789Sahrens err = zfs_acl_chmod_setattr(zp, new_mode, tx); 2114789Sahrens ASSERT3U(err, ==, 0); 2115789Sahrens } 2116789Sahrens 21171231Smarks if (attrzp) 21181231Smarks mutex_enter(&attrzp->z_lock); 21191231Smarks 21201231Smarks if (mask & AT_UID) { 2121789Sahrens zp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21221231Smarks if (attrzp) { 21231231Smarks attrzp->z_phys->zp_uid = (uint64_t)vap->va_uid; 21241231Smarks } 21251231Smarks } 21261231Smarks 21271231Smarks if (mask & AT_GID) { 2128789Sahrens zp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21291231Smarks if (attrzp) 21301231Smarks attrzp->z_phys->zp_gid = (uint64_t)vap->va_gid; 21311231Smarks } 21321231Smarks 21331231Smarks if (attrzp) 21341231Smarks mutex_exit(&attrzp->z_lock); 2135789Sahrens 2136789Sahrens if (mask & AT_ATIME) 2137789Sahrens ZFS_TIME_ENCODE(&vap->va_atime, pzp->zp_atime); 2138789Sahrens 2139789Sahrens if (mask & AT_MTIME) 2140789Sahrens ZFS_TIME_ENCODE(&vap->va_mtime, pzp->zp_mtime); 2141789Sahrens 21421878Smaybee if (mask & AT_SIZE) 2143789Sahrens zfs_time_stamper_locked(zp, CONTENT_MODIFIED, tx); 21441878Smaybee else if (mask != 0) 2145789Sahrens zfs_time_stamper_locked(zp, STATE_CHANGED, tx); 2146789Sahrens 21471878Smaybee if (mask != 0) 21481878Smaybee seq = zfs_log_setattr(zilog, tx, TX_SETATTR, zp, vap, mask); 2149789Sahrens 2150789Sahrens mutex_exit(&zp->z_lock); 2151789Sahrens 21521231Smarks if (attrzp) 21531231Smarks VN_RELE(ZTOV(attrzp)); 21541231Smarks 2155789Sahrens dmu_tx_commit(tx); 2156789Sahrens 2157789Sahrens zil_commit(zilog, seq, 0); 2158789Sahrens 2159789Sahrens ZFS_EXIT(zfsvfs); 2160789Sahrens return (err); 2161789Sahrens } 2162789Sahrens 2163789Sahrens /* 2164789Sahrens * Search back through the directory tree, using the ".." entries. 2165789Sahrens * Lock each directory in the chain to prevent concurrent renames. 2166789Sahrens * Fail any attempt to move a directory into one of its own descendants. 2167789Sahrens * XXX - z_parent_lock can overlap with map or grow locks 2168789Sahrens */ 2169789Sahrens typedef struct zfs_zlock { 2170789Sahrens krwlock_t *zl_rwlock; /* lock we acquired */ 2171789Sahrens znode_t *zl_znode; /* znode we held */ 2172789Sahrens struct zfs_zlock *zl_next; /* next in list */ 2173789Sahrens } zfs_zlock_t; 2174789Sahrens 2175789Sahrens static int 2176789Sahrens zfs_rename_lock(znode_t *szp, znode_t *tdzp, znode_t *sdzp, zfs_zlock_t **zlpp) 2177789Sahrens { 2178789Sahrens zfs_zlock_t *zl; 2179789Sahrens znode_t *zp = tdzp; 2180789Sahrens uint64_t rootid = zp->z_zfsvfs->z_root; 2181789Sahrens uint64_t *oidp = &zp->z_id; 2182789Sahrens krwlock_t *rwlp = &szp->z_parent_lock; 2183789Sahrens krw_t rw = RW_WRITER; 2184789Sahrens 2185789Sahrens /* 2186789Sahrens * First pass write-locks szp and compares to zp->z_id. 2187789Sahrens * Later passes read-lock zp and compare to zp->z_parent. 2188789Sahrens */ 2189789Sahrens do { 2190789Sahrens zl = kmem_alloc(sizeof (*zl), KM_SLEEP); 2191789Sahrens zl->zl_rwlock = rwlp; 2192789Sahrens zl->zl_znode = NULL; 2193789Sahrens zl->zl_next = *zlpp; 2194789Sahrens *zlpp = zl; 2195789Sahrens 2196789Sahrens rw_enter(rwlp, rw); 2197789Sahrens 2198789Sahrens if (*oidp == szp->z_id) /* We're a descendant of szp */ 2199789Sahrens return (EINVAL); 2200789Sahrens 2201789Sahrens if (*oidp == rootid) /* We've hit the top */ 2202789Sahrens return (0); 2203789Sahrens 2204789Sahrens if (rw == RW_READER) { /* i.e. not the first pass */ 2205789Sahrens int error = zfs_zget(zp->z_zfsvfs, *oidp, &zp); 2206789Sahrens if (error) 2207789Sahrens return (error); 2208789Sahrens zl->zl_znode = zp; 2209789Sahrens } 2210789Sahrens oidp = &zp->z_phys->zp_parent; 2211789Sahrens rwlp = &zp->z_parent_lock; 2212789Sahrens rw = RW_READER; 2213789Sahrens 2214789Sahrens } while (zp->z_id != sdzp->z_id); 2215789Sahrens 2216789Sahrens return (0); 2217789Sahrens } 2218789Sahrens 2219789Sahrens /* 2220789Sahrens * Drop locks and release vnodes that were held by zfs_rename_lock(). 2221789Sahrens */ 2222789Sahrens static void 2223789Sahrens zfs_rename_unlock(zfs_zlock_t **zlpp) 2224789Sahrens { 2225789Sahrens zfs_zlock_t *zl; 2226789Sahrens 2227789Sahrens while ((zl = *zlpp) != NULL) { 2228789Sahrens if (zl->zl_znode != NULL) 2229789Sahrens VN_RELE(ZTOV(zl->zl_znode)); 2230789Sahrens rw_exit(zl->zl_rwlock); 2231789Sahrens *zlpp = zl->zl_next; 2232789Sahrens kmem_free(zl, sizeof (*zl)); 2233789Sahrens } 2234789Sahrens } 2235789Sahrens 2236789Sahrens /* 2237789Sahrens * Move an entry from the provided source directory to the target 2238789Sahrens * directory. Change the entry name as indicated. 2239789Sahrens * 2240789Sahrens * IN: sdvp - Source directory containing the "old entry". 2241789Sahrens * snm - Old entry name. 2242789Sahrens * tdvp - Target directory to contain the "new entry". 2243789Sahrens * tnm - New entry name. 2244789Sahrens * cr - credentials of caller. 2245789Sahrens * 2246789Sahrens * RETURN: 0 if success 2247789Sahrens * error code if failure 2248789Sahrens * 2249789Sahrens * Timestamps: 2250789Sahrens * sdvp,tdvp - ctime|mtime updated 2251789Sahrens */ 2252789Sahrens static int 2253789Sahrens zfs_rename(vnode_t *sdvp, char *snm, vnode_t *tdvp, char *tnm, cred_t *cr) 2254789Sahrens { 2255789Sahrens znode_t *tdzp, *szp, *tzp; 2256789Sahrens znode_t *sdzp = VTOZ(sdvp); 2257789Sahrens zfsvfs_t *zfsvfs = sdzp->z_zfsvfs; 2258789Sahrens zilog_t *zilog = zfsvfs->z_log; 2259789Sahrens uint64_t seq = 0; 2260789Sahrens vnode_t *realvp; 2261789Sahrens zfs_dirlock_t *sdl, *tdl; 2262789Sahrens dmu_tx_t *tx; 2263789Sahrens zfs_zlock_t *zl; 2264789Sahrens int cmp, serr, terr, error; 2265789Sahrens 2266789Sahrens ZFS_ENTER(zfsvfs); 2267789Sahrens 2268789Sahrens /* 2269789Sahrens * Make sure we have the real vp for the target directory. 2270789Sahrens */ 2271789Sahrens if (VOP_REALVP(tdvp, &realvp) == 0) 2272789Sahrens tdvp = realvp; 2273789Sahrens 2274789Sahrens if (tdvp->v_vfsp != sdvp->v_vfsp) { 2275789Sahrens ZFS_EXIT(zfsvfs); 2276789Sahrens return (EXDEV); 2277789Sahrens } 2278789Sahrens 2279789Sahrens tdzp = VTOZ(tdvp); 2280789Sahrens top: 2281789Sahrens szp = NULL; 2282789Sahrens tzp = NULL; 2283789Sahrens zl = NULL; 2284789Sahrens 2285789Sahrens /* 2286789Sahrens * This is to prevent the creation of links into attribute space 2287789Sahrens * by renaming a linked file into/outof an attribute directory. 2288789Sahrens * See the comment in zfs_link() for why this is considered bad. 2289789Sahrens */ 2290789Sahrens if ((tdzp->z_phys->zp_flags & ZFS_XATTR) != 2291789Sahrens (sdzp->z_phys->zp_flags & ZFS_XATTR)) { 2292789Sahrens ZFS_EXIT(zfsvfs); 2293789Sahrens return (EINVAL); 2294789Sahrens } 2295789Sahrens 2296789Sahrens /* 2297789Sahrens * Lock source and target directory entries. To prevent deadlock, 2298789Sahrens * a lock ordering must be defined. We lock the directory with 2299789Sahrens * the smallest object id first, or if it's a tie, the one with 2300789Sahrens * the lexically first name. 2301789Sahrens */ 2302789Sahrens if (sdzp->z_id < tdzp->z_id) { 2303789Sahrens cmp = -1; 2304789Sahrens } else if (sdzp->z_id > tdzp->z_id) { 2305789Sahrens cmp = 1; 2306789Sahrens } else { 2307789Sahrens cmp = strcmp(snm, tnm); 2308789Sahrens if (cmp == 0) { 2309789Sahrens /* 2310789Sahrens * POSIX: "If the old argument and the new argument 2311789Sahrens * both refer to links to the same existing file, 2312789Sahrens * the rename() function shall return successfully 2313789Sahrens * and perform no other action." 2314789Sahrens */ 2315789Sahrens ZFS_EXIT(zfsvfs); 2316789Sahrens return (0); 2317789Sahrens } 2318789Sahrens } 2319789Sahrens if (cmp < 0) { 2320789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2321789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2322789Sahrens } else { 2323789Sahrens terr = zfs_dirent_lock(&tdl, tdzp, tnm, &tzp, 0); 2324789Sahrens serr = zfs_dirent_lock(&sdl, sdzp, snm, &szp, ZEXISTS); 2325789Sahrens } 2326789Sahrens 2327789Sahrens if (serr) { 2328789Sahrens /* 2329789Sahrens * Source entry invalid or not there. 2330789Sahrens */ 2331789Sahrens if (!terr) { 2332789Sahrens zfs_dirent_unlock(tdl); 2333789Sahrens if (tzp) 2334789Sahrens VN_RELE(ZTOV(tzp)); 2335789Sahrens } 2336789Sahrens if (strcmp(snm, "..") == 0) 2337789Sahrens serr = EINVAL; 2338789Sahrens ZFS_EXIT(zfsvfs); 2339789Sahrens return (serr); 2340789Sahrens } 2341789Sahrens if (terr) { 2342789Sahrens zfs_dirent_unlock(sdl); 2343789Sahrens VN_RELE(ZTOV(szp)); 2344789Sahrens if (strcmp(tnm, "..") == 0) 2345789Sahrens terr = EINVAL; 2346789Sahrens ZFS_EXIT(zfsvfs); 2347789Sahrens return (terr); 2348789Sahrens } 2349789Sahrens 2350789Sahrens /* 2351789Sahrens * Must have write access at the source to remove the old entry 2352789Sahrens * and write access at the target to create the new entry. 2353789Sahrens * Note that if target and source are the same, this can be 2354789Sahrens * done in a single check. 2355789Sahrens */ 2356789Sahrens 2357789Sahrens if (error = zfs_zaccess_rename(sdzp, szp, tdzp, tzp, cr)) 2358789Sahrens goto out; 2359789Sahrens 2360789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2361789Sahrens /* 2362789Sahrens * Check to make sure rename is valid. 2363789Sahrens * Can't do a move like this: /usr/a/b to /usr/a/b/c/d 2364789Sahrens */ 2365789Sahrens if (error = zfs_rename_lock(szp, tdzp, sdzp, &zl)) 2366789Sahrens goto out; 2367789Sahrens } 2368789Sahrens 2369789Sahrens /* 2370789Sahrens * Does target exist? 2371789Sahrens */ 2372789Sahrens if (tzp) { 2373789Sahrens /* 2374789Sahrens * Source and target must be the same type. 2375789Sahrens */ 2376789Sahrens if (ZTOV(szp)->v_type == VDIR) { 2377789Sahrens if (ZTOV(tzp)->v_type != VDIR) { 2378789Sahrens error = ENOTDIR; 2379789Sahrens goto out; 2380789Sahrens } 2381789Sahrens } else { 2382789Sahrens if (ZTOV(tzp)->v_type == VDIR) { 2383789Sahrens error = EISDIR; 2384789Sahrens goto out; 2385789Sahrens } 2386789Sahrens } 2387789Sahrens /* 2388789Sahrens * POSIX dictates that when the source and target 2389789Sahrens * entries refer to the same file object, rename 2390789Sahrens * must do nothing and exit without error. 2391789Sahrens */ 2392789Sahrens if (szp->z_id == tzp->z_id) { 2393789Sahrens error = 0; 2394789Sahrens goto out; 2395789Sahrens } 2396789Sahrens } 2397789Sahrens 2398789Sahrens vnevent_rename_src(ZTOV(szp)); 2399789Sahrens if (tzp) 2400789Sahrens vnevent_rename_dest(ZTOV(tzp)); 2401789Sahrens 2402789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2403789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); /* nlink changes */ 2404789Sahrens dmu_tx_hold_bonus(tx, sdzp->z_id); /* nlink changes */ 24051544Seschrock dmu_tx_hold_zap(tx, sdzp->z_id, FALSE, snm); 24061544Seschrock dmu_tx_hold_zap(tx, tdzp->z_id, TRUE, tnm); 24071544Seschrock if (sdzp != tdzp) 2408789Sahrens dmu_tx_hold_bonus(tx, tdzp->z_id); /* nlink changes */ 24091544Seschrock if (tzp) 24101544Seschrock dmu_tx_hold_bonus(tx, tzp->z_id); /* parent changes */ 24111544Seschrock dmu_tx_hold_zap(tx, zfsvfs->z_dqueue, FALSE, NULL); 2412789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2413789Sahrens if (error) { 2414789Sahrens dmu_tx_abort(tx); 2415789Sahrens if (zl != NULL) 2416789Sahrens zfs_rename_unlock(&zl); 2417789Sahrens zfs_dirent_unlock(sdl); 2418789Sahrens zfs_dirent_unlock(tdl); 2419789Sahrens VN_RELE(ZTOV(szp)); 2420789Sahrens if (tzp) 2421789Sahrens VN_RELE(ZTOV(tzp)); 2422789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2423789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 2424789Sahrens goto top; 2425789Sahrens } 2426789Sahrens ZFS_EXIT(zfsvfs); 2427789Sahrens return (error); 2428789Sahrens } 2429789Sahrens 2430789Sahrens if (tzp) /* Attempt to remove the existing target */ 2431789Sahrens error = zfs_link_destroy(tdl, tzp, tx, 0, NULL); 2432789Sahrens 2433789Sahrens if (error == 0) { 2434789Sahrens error = zfs_link_create(tdl, szp, tx, ZRENAMING); 2435789Sahrens if (error == 0) { 2436789Sahrens error = zfs_link_destroy(sdl, szp, tx, ZRENAMING, NULL); 2437789Sahrens ASSERT(error == 0); 2438789Sahrens seq = zfs_log_rename(zilog, tx, TX_RENAME, 2439789Sahrens sdzp, sdl->dl_name, tdzp, tdl->dl_name, szp); 2440789Sahrens } 2441789Sahrens } 2442789Sahrens 2443789Sahrens dmu_tx_commit(tx); 2444789Sahrens out: 2445789Sahrens if (zl != NULL) 2446789Sahrens zfs_rename_unlock(&zl); 2447789Sahrens 2448789Sahrens zfs_dirent_unlock(sdl); 2449789Sahrens zfs_dirent_unlock(tdl); 2450789Sahrens 2451789Sahrens VN_RELE(ZTOV(szp)); 2452789Sahrens if (tzp) 2453789Sahrens VN_RELE(ZTOV(tzp)); 2454789Sahrens 2455789Sahrens zil_commit(zilog, seq, 0); 2456789Sahrens 2457789Sahrens ZFS_EXIT(zfsvfs); 2458789Sahrens return (error); 2459789Sahrens } 2460789Sahrens 2461789Sahrens /* 2462789Sahrens * Insert the indicated symbolic reference entry into the directory. 2463789Sahrens * 2464789Sahrens * IN: dvp - Directory to contain new symbolic link. 2465789Sahrens * link - Name for new symlink entry. 2466789Sahrens * vap - Attributes of new entry. 2467789Sahrens * target - Target path of new symlink. 2468789Sahrens * cr - credentials of caller. 2469789Sahrens * 2470789Sahrens * RETURN: 0 if success 2471789Sahrens * error code if failure 2472789Sahrens * 2473789Sahrens * Timestamps: 2474789Sahrens * dvp - ctime|mtime updated 2475789Sahrens */ 2476789Sahrens static int 2477789Sahrens zfs_symlink(vnode_t *dvp, char *name, vattr_t *vap, char *link, cred_t *cr) 2478789Sahrens { 2479789Sahrens znode_t *zp, *dzp = VTOZ(dvp); 2480789Sahrens zfs_dirlock_t *dl; 2481789Sahrens dmu_tx_t *tx; 2482789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2483789Sahrens zilog_t *zilog = zfsvfs->z_log; 2484789Sahrens uint64_t seq = 0; 2485789Sahrens uint64_t zoid; 2486789Sahrens int len = strlen(link); 2487789Sahrens int error; 2488789Sahrens 2489789Sahrens ASSERT(vap->va_type == VLNK); 2490789Sahrens 2491789Sahrens ZFS_ENTER(zfsvfs); 2492789Sahrens top: 2493789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2494789Sahrens ZFS_EXIT(zfsvfs); 2495789Sahrens return (error); 2496789Sahrens } 2497789Sahrens 2498789Sahrens if (len > MAXPATHLEN) { 2499789Sahrens ZFS_EXIT(zfsvfs); 2500789Sahrens return (ENAMETOOLONG); 2501789Sahrens } 2502789Sahrens 2503789Sahrens /* 2504789Sahrens * Attempt to lock directory; fail if entry already exists. 2505789Sahrens */ 2506789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &zp, ZNEW)) { 2507789Sahrens ZFS_EXIT(zfsvfs); 2508789Sahrens return (error); 2509789Sahrens } 2510789Sahrens 2511789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2512789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, MAX(1, len)); 2513789Sahrens dmu_tx_hold_bonus(tx, dzp->z_id); 25141544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2515789Sahrens if (dzp->z_phys->zp_flags & ZFS_INHERIT_ACE) 2516789Sahrens dmu_tx_hold_write(tx, DMU_NEW_OBJECT, 0, SPA_MAXBLOCKSIZE); 2517789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2518789Sahrens if (error) { 2519789Sahrens dmu_tx_abort(tx); 2520789Sahrens zfs_dirent_unlock(dl); 2521789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2522789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 2523789Sahrens goto top; 2524789Sahrens } 2525789Sahrens ZFS_EXIT(zfsvfs); 2526789Sahrens return (error); 2527789Sahrens } 2528789Sahrens 2529789Sahrens dmu_buf_will_dirty(dzp->z_dbuf, tx); 2530789Sahrens 2531789Sahrens /* 2532789Sahrens * Create a new object for the symlink. 2533789Sahrens * Put the link content into bonus buffer if it will fit; 2534789Sahrens * otherwise, store it just like any other file data. 2535789Sahrens */ 2536789Sahrens zoid = 0; 2537789Sahrens if (sizeof (znode_phys_t) + len <= dmu_bonus_max()) { 2538789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, len); 2539789Sahrens if (len != 0) 2540789Sahrens bcopy(link, zp->z_phys + 1, len); 2541789Sahrens } else { 2542789Sahrens dmu_buf_t *dbp; 25431669Sperrin 2544789Sahrens zfs_mknode(dzp, vap, &zoid, tx, cr, 0, &zp, 0); 2545789Sahrens 25461669Sperrin /* 25471669Sperrin * Nothing can access the znode yet so no locking needed 25481669Sperrin * for growing the znode's blocksize. 25491669Sperrin */ 25501669Sperrin zfs_grow_blocksize(zp, len, tx); 2551789Sahrens 25521544Seschrock VERIFY(0 == dmu_buf_hold(zfsvfs->z_os, zoid, 0, FTAG, &dbp)); 2553789Sahrens dmu_buf_will_dirty(dbp, tx); 2554789Sahrens 2555789Sahrens ASSERT3U(len, <=, dbp->db_size); 2556789Sahrens bcopy(link, dbp->db_data, len); 25571544Seschrock dmu_buf_rele(dbp, FTAG); 2558789Sahrens } 2559789Sahrens zp->z_phys->zp_size = len; 2560789Sahrens 2561789Sahrens /* 2562789Sahrens * Insert the new object into the directory. 2563789Sahrens */ 2564789Sahrens (void) zfs_link_create(dl, zp, tx, ZNEW); 2565789Sahrens out: 2566789Sahrens if (error == 0) 2567789Sahrens seq = zfs_log_symlink(zilog, tx, TX_SYMLINK, 2568789Sahrens dzp, zp, name, link); 2569789Sahrens 2570789Sahrens dmu_tx_commit(tx); 2571789Sahrens 2572789Sahrens zfs_dirent_unlock(dl); 2573789Sahrens 2574789Sahrens VN_RELE(ZTOV(zp)); 2575789Sahrens 2576789Sahrens zil_commit(zilog, seq, 0); 2577789Sahrens 2578789Sahrens ZFS_EXIT(zfsvfs); 2579789Sahrens return (error); 2580789Sahrens } 2581789Sahrens 2582789Sahrens /* 2583789Sahrens * Return, in the buffer contained in the provided uio structure, 2584789Sahrens * the symbolic path referred to by vp. 2585789Sahrens * 2586789Sahrens * IN: vp - vnode of symbolic link. 2587789Sahrens * uoip - structure to contain the link path. 2588789Sahrens * cr - credentials of caller. 2589789Sahrens * 2590789Sahrens * OUT: uio - structure to contain the link path. 2591789Sahrens * 2592789Sahrens * RETURN: 0 if success 2593789Sahrens * error code if failure 2594789Sahrens * 2595789Sahrens * Timestamps: 2596789Sahrens * vp - atime updated 2597789Sahrens */ 2598789Sahrens /* ARGSUSED */ 2599789Sahrens static int 2600789Sahrens zfs_readlink(vnode_t *vp, uio_t *uio, cred_t *cr) 2601789Sahrens { 2602789Sahrens znode_t *zp = VTOZ(vp); 2603789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2604789Sahrens size_t bufsz; 2605789Sahrens int error; 2606789Sahrens 2607789Sahrens ZFS_ENTER(zfsvfs); 2608789Sahrens 2609789Sahrens bufsz = (size_t)zp->z_phys->zp_size; 2610789Sahrens if (bufsz + sizeof (znode_phys_t) <= zp->z_dbuf->db_size) { 2611789Sahrens error = uiomove(zp->z_phys + 1, 2612789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 2613789Sahrens } else { 26141544Seschrock dmu_buf_t *dbp; 26151544Seschrock error = dmu_buf_hold(zfsvfs->z_os, zp->z_id, 0, FTAG, &dbp); 26161544Seschrock if (error) { 2617789Sahrens ZFS_EXIT(zfsvfs); 2618789Sahrens return (error); 2619789Sahrens } 2620789Sahrens error = uiomove(dbp->db_data, 2621789Sahrens MIN((size_t)bufsz, uio->uio_resid), UIO_READ, uio); 26221544Seschrock dmu_buf_rele(dbp, FTAG); 2623789Sahrens } 2624789Sahrens 2625789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 2626789Sahrens ZFS_EXIT(zfsvfs); 2627789Sahrens return (error); 2628789Sahrens } 2629789Sahrens 2630789Sahrens /* 2631789Sahrens * Insert a new entry into directory tdvp referencing svp. 2632789Sahrens * 2633789Sahrens * IN: tdvp - Directory to contain new entry. 2634789Sahrens * svp - vnode of new entry. 2635789Sahrens * name - name of new entry. 2636789Sahrens * cr - credentials of caller. 2637789Sahrens * 2638789Sahrens * RETURN: 0 if success 2639789Sahrens * error code if failure 2640789Sahrens * 2641789Sahrens * Timestamps: 2642789Sahrens * tdvp - ctime|mtime updated 2643789Sahrens * svp - ctime updated 2644789Sahrens */ 2645789Sahrens /* ARGSUSED */ 2646789Sahrens static int 2647789Sahrens zfs_link(vnode_t *tdvp, vnode_t *svp, char *name, cred_t *cr) 2648789Sahrens { 2649789Sahrens znode_t *dzp = VTOZ(tdvp); 2650789Sahrens znode_t *tzp, *szp; 2651789Sahrens zfsvfs_t *zfsvfs = dzp->z_zfsvfs; 2652789Sahrens zilog_t *zilog = zfsvfs->z_log; 2653789Sahrens uint64_t seq = 0; 2654789Sahrens zfs_dirlock_t *dl; 2655789Sahrens dmu_tx_t *tx; 2656789Sahrens vnode_t *realvp; 2657789Sahrens int error; 2658789Sahrens 2659789Sahrens ASSERT(tdvp->v_type == VDIR); 2660789Sahrens 2661789Sahrens ZFS_ENTER(zfsvfs); 2662789Sahrens 2663789Sahrens if (VOP_REALVP(svp, &realvp) == 0) 2664789Sahrens svp = realvp; 2665789Sahrens 2666789Sahrens if (svp->v_vfsp != tdvp->v_vfsp) { 2667789Sahrens ZFS_EXIT(zfsvfs); 2668789Sahrens return (EXDEV); 2669789Sahrens } 2670789Sahrens 2671789Sahrens szp = VTOZ(svp); 2672789Sahrens top: 2673789Sahrens /* 2674789Sahrens * We do not support links between attributes and non-attributes 2675789Sahrens * because of the potential security risk of creating links 2676789Sahrens * into "normal" file space in order to circumvent restrictions 2677789Sahrens * imposed in attribute space. 2678789Sahrens */ 2679789Sahrens if ((szp->z_phys->zp_flags & ZFS_XATTR) != 2680789Sahrens (dzp->z_phys->zp_flags & ZFS_XATTR)) { 2681789Sahrens ZFS_EXIT(zfsvfs); 2682789Sahrens return (EINVAL); 2683789Sahrens } 2684789Sahrens 2685789Sahrens /* 2686789Sahrens * POSIX dictates that we return EPERM here. 2687789Sahrens * Better choices include ENOTSUP or EISDIR. 2688789Sahrens */ 2689789Sahrens if (svp->v_type == VDIR) { 2690789Sahrens ZFS_EXIT(zfsvfs); 2691789Sahrens return (EPERM); 2692789Sahrens } 2693789Sahrens 2694789Sahrens if ((uid_t)szp->z_phys->zp_uid != crgetuid(cr) && 2695789Sahrens secpolicy_basic_link(cr) != 0) { 2696789Sahrens ZFS_EXIT(zfsvfs); 2697789Sahrens return (EPERM); 2698789Sahrens } 2699789Sahrens 2700789Sahrens if (error = zfs_zaccess(dzp, ACE_ADD_FILE, cr)) { 2701789Sahrens ZFS_EXIT(zfsvfs); 2702789Sahrens return (error); 2703789Sahrens } 2704789Sahrens 2705789Sahrens /* 2706789Sahrens * Attempt to lock directory; fail if entry already exists. 2707789Sahrens */ 2708789Sahrens if (error = zfs_dirent_lock(&dl, dzp, name, &tzp, ZNEW)) { 2709789Sahrens ZFS_EXIT(zfsvfs); 2710789Sahrens return (error); 2711789Sahrens } 2712789Sahrens 2713789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2714789Sahrens dmu_tx_hold_bonus(tx, szp->z_id); 27151544Seschrock dmu_tx_hold_zap(tx, dzp->z_id, TRUE, name); 2716789Sahrens error = dmu_tx_assign(tx, zfsvfs->z_assign); 2717789Sahrens if (error) { 2718789Sahrens dmu_tx_abort(tx); 2719789Sahrens zfs_dirent_unlock(dl); 2720789Sahrens if (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2721789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 2722789Sahrens goto top; 2723789Sahrens } 2724789Sahrens ZFS_EXIT(zfsvfs); 2725789Sahrens return (error); 2726789Sahrens } 2727789Sahrens 2728789Sahrens error = zfs_link_create(dl, szp, tx, 0); 2729789Sahrens 2730789Sahrens if (error == 0) 2731789Sahrens seq = zfs_log_link(zilog, tx, TX_LINK, dzp, szp, name); 2732789Sahrens 2733789Sahrens dmu_tx_commit(tx); 2734789Sahrens 2735789Sahrens zfs_dirent_unlock(dl); 2736789Sahrens 2737789Sahrens zil_commit(zilog, seq, 0); 2738789Sahrens 2739789Sahrens ZFS_EXIT(zfsvfs); 2740789Sahrens return (error); 2741789Sahrens } 2742789Sahrens 2743789Sahrens /* 2744789Sahrens * zfs_null_putapage() is used when the file system has been force 2745789Sahrens * unmounted. It just drops the pages. 2746789Sahrens */ 2747789Sahrens /* ARGSUSED */ 2748789Sahrens static int 2749789Sahrens zfs_null_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2750789Sahrens size_t *lenp, int flags, cred_t *cr) 2751789Sahrens { 2752789Sahrens pvn_write_done(pp, B_INVAL|B_FORCE|B_ERROR); 2753789Sahrens return (0); 2754789Sahrens } 2755789Sahrens 2756789Sahrens /* ARGSUSED */ 2757789Sahrens static int 2758789Sahrens zfs_putapage(vnode_t *vp, page_t *pp, u_offset_t *offp, 2759789Sahrens size_t *lenp, int flags, cred_t *cr) 2760789Sahrens { 2761789Sahrens znode_t *zp = VTOZ(vp); 2762789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2763789Sahrens zilog_t *zilog = zfsvfs->z_log; 2764789Sahrens dmu_tx_t *tx; 27651669Sperrin rl_t *rl; 2766789Sahrens u_offset_t off; 2767789Sahrens ssize_t len; 2768789Sahrens caddr_t va; 2769789Sahrens int err; 2770789Sahrens 2771789Sahrens top: 2772789Sahrens off = pp->p_offset; 27731669Sperrin rl = zfs_range_lock(zp, off, PAGESIZE, RL_WRITER); 27741819Smaybee /* 27751819Smaybee * Can't push pages past end-of-file. 27761819Smaybee */ 27771819Smaybee if (off >= zp->z_phys->zp_size) { 27781819Smaybee zfs_range_unlock(zp, rl); 27791819Smaybee return (EIO); 27801819Smaybee } 2781789Sahrens len = MIN(PAGESIZE, zp->z_phys->zp_size - off); 2782789Sahrens 2783789Sahrens tx = dmu_tx_create(zfsvfs->z_os); 2784789Sahrens dmu_tx_hold_write(tx, zp->z_id, off, len); 2785789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2786789Sahrens err = dmu_tx_assign(tx, zfsvfs->z_assign); 2787789Sahrens if (err != 0) { 2788789Sahrens dmu_tx_abort(tx); 27891669Sperrin zfs_range_unlock(zp, rl); 2790789Sahrens if (err == ERESTART && zfsvfs->z_assign == TXG_NOWAIT) { 2791789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 2792789Sahrens goto top; 2793789Sahrens } 2794789Sahrens goto out; 2795789Sahrens } 2796789Sahrens 2797789Sahrens va = ppmapin(pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 2798789Sahrens 2799789Sahrens dmu_write(zfsvfs->z_os, zp->z_id, off, len, va, tx); 2800789Sahrens 2801789Sahrens ppmapout(va); 2802789Sahrens 2803789Sahrens zfs_time_stamper(zp, CONTENT_MODIFIED, tx); 28041472Sperrin (void) zfs_log_write(zilog, tx, TX_WRITE, zp, off, len, 0, NULL); 2805789Sahrens dmu_tx_commit(tx); 2806789Sahrens 28071669Sperrin zfs_range_unlock(zp, rl); 2808789Sahrens 2809789Sahrens pvn_write_done(pp, B_WRITE | flags); 2810789Sahrens if (offp) 2811789Sahrens *offp = off; 2812789Sahrens if (lenp) 2813789Sahrens *lenp = len; 2814789Sahrens 2815789Sahrens out: 2816789Sahrens return (err); 2817789Sahrens } 2818789Sahrens 2819789Sahrens /* 2820789Sahrens * Copy the portion of the file indicated from pages into the file. 2821789Sahrens * The pages are stored in a page list attached to the files vnode. 2822789Sahrens * 2823789Sahrens * IN: vp - vnode of file to push page data to. 2824789Sahrens * off - position in file to put data. 2825789Sahrens * len - amount of data to write. 2826789Sahrens * flags - flags to control the operation. 2827789Sahrens * cr - credentials of caller. 2828789Sahrens * 2829789Sahrens * RETURN: 0 if success 2830789Sahrens * error code if failure 2831789Sahrens * 2832789Sahrens * Timestamps: 2833789Sahrens * vp - ctime|mtime updated 2834789Sahrens */ 2835789Sahrens static int 2836789Sahrens zfs_putpage(vnode_t *vp, offset_t off, size_t len, int flags, cred_t *cr) 2837789Sahrens { 2838789Sahrens znode_t *zp = VTOZ(vp); 2839789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2840789Sahrens page_t *pp; 2841789Sahrens size_t io_len; 2842789Sahrens u_offset_t io_off; 28431669Sperrin uint64_t filesz; 2844789Sahrens int error = 0; 2845789Sahrens 2846789Sahrens ZFS_ENTER(zfsvfs); 2847789Sahrens 2848789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 2849789Sahrens 2850789Sahrens if (len == 0) { 2851789Sahrens /* 2852789Sahrens * Search the entire vp list for pages >= off. 2853789Sahrens */ 2854789Sahrens error = pvn_vplist_dirty(vp, (u_offset_t)off, zfs_putapage, 2855789Sahrens flags, cr); 28561472Sperrin goto out; 2857789Sahrens } 2858789Sahrens 28591669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 28601669Sperrin if (off > filesz) { 2861789Sahrens /* past end of file */ 2862789Sahrens ZFS_EXIT(zfsvfs); 2863789Sahrens return (0); 2864789Sahrens } 2865789Sahrens 28661669Sperrin len = MIN(len, filesz - off); 2867789Sahrens 28681472Sperrin for (io_off = off; io_off < off + len; io_off += io_len) { 2869789Sahrens if ((flags & B_INVAL) || ((flags & B_ASYNC) == 0)) { 28701669Sperrin pp = page_lookup(vp, io_off, 2871789Sahrens (flags & (B_INVAL | B_FREE)) ? 2872789Sahrens SE_EXCL : SE_SHARED); 2873789Sahrens } else { 2874789Sahrens pp = page_lookup_nowait(vp, io_off, 2875789Sahrens (flags & B_FREE) ? SE_EXCL : SE_SHARED); 2876789Sahrens } 2877789Sahrens 2878789Sahrens if (pp != NULL && pvn_getdirty(pp, flags)) { 2879789Sahrens int err; 2880789Sahrens 2881789Sahrens /* 2882789Sahrens * Found a dirty page to push 2883789Sahrens */ 28841669Sperrin err = zfs_putapage(vp, pp, &io_off, &io_len, flags, cr); 28851669Sperrin if (err) 2886789Sahrens error = err; 2887789Sahrens } else { 2888789Sahrens io_len = PAGESIZE; 2889789Sahrens } 2890789Sahrens } 28911472Sperrin out: 28921472Sperrin zil_commit(zfsvfs->z_log, UINT64_MAX, (flags & B_ASYNC) ? 0 : FDSYNC); 2893789Sahrens ZFS_EXIT(zfsvfs); 2894789Sahrens return (error); 2895789Sahrens } 2896789Sahrens 2897789Sahrens void 2898789Sahrens zfs_inactive(vnode_t *vp, cred_t *cr) 2899789Sahrens { 2900789Sahrens znode_t *zp = VTOZ(vp); 2901789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2902789Sahrens int error; 2903789Sahrens 2904789Sahrens rw_enter(&zfsvfs->z_um_lock, RW_READER); 2905789Sahrens if (zfsvfs->z_unmounted2) { 2906789Sahrens ASSERT(zp->z_dbuf_held == 0); 2907789Sahrens 2908789Sahrens if (vn_has_cached_data(vp)) { 2909789Sahrens (void) pvn_vplist_dirty(vp, 0, zfs_null_putapage, 2910789Sahrens B_INVAL, cr); 2911789Sahrens } 2912789Sahrens 29131544Seschrock mutex_enter(&zp->z_lock); 2914789Sahrens vp->v_count = 0; /* count arrives as 1 */ 29151544Seschrock if (zp->z_dbuf == NULL) { 29161544Seschrock mutex_exit(&zp->z_lock); 29171544Seschrock zfs_znode_free(zp); 29181544Seschrock } else { 29191544Seschrock mutex_exit(&zp->z_lock); 29201544Seschrock } 2921789Sahrens rw_exit(&zfsvfs->z_um_lock); 2922789Sahrens VFS_RELE(zfsvfs->z_vfs); 2923789Sahrens return; 2924789Sahrens } 2925789Sahrens 2926789Sahrens /* 2927789Sahrens * Attempt to push any data in the page cache. If this fails 2928789Sahrens * we will get kicked out later in zfs_zinactive(). 2929789Sahrens */ 29301298Sperrin if (vn_has_cached_data(vp)) { 29311298Sperrin (void) pvn_vplist_dirty(vp, 0, zfs_putapage, B_INVAL|B_ASYNC, 29321298Sperrin cr); 29331298Sperrin } 2934789Sahrens 2935789Sahrens if (zp->z_atime_dirty && zp->z_reap == 0) { 2936789Sahrens dmu_tx_t *tx = dmu_tx_create(zfsvfs->z_os); 2937789Sahrens 2938789Sahrens dmu_tx_hold_bonus(tx, zp->z_id); 2939789Sahrens error = dmu_tx_assign(tx, TXG_WAIT); 2940789Sahrens if (error) { 2941789Sahrens dmu_tx_abort(tx); 2942789Sahrens } else { 2943789Sahrens dmu_buf_will_dirty(zp->z_dbuf, tx); 2944789Sahrens mutex_enter(&zp->z_lock); 2945789Sahrens zp->z_atime_dirty = 0; 2946789Sahrens mutex_exit(&zp->z_lock); 2947789Sahrens dmu_tx_commit(tx); 2948789Sahrens } 2949789Sahrens } 2950789Sahrens 2951789Sahrens zfs_zinactive(zp); 2952789Sahrens rw_exit(&zfsvfs->z_um_lock); 2953789Sahrens } 2954789Sahrens 2955789Sahrens /* 2956789Sahrens * Bounds-check the seek operation. 2957789Sahrens * 2958789Sahrens * IN: vp - vnode seeking within 2959789Sahrens * ooff - old file offset 2960789Sahrens * noffp - pointer to new file offset 2961789Sahrens * 2962789Sahrens * RETURN: 0 if success 2963789Sahrens * EINVAL if new offset invalid 2964789Sahrens */ 2965789Sahrens /* ARGSUSED */ 2966789Sahrens static int 2967789Sahrens zfs_seek(vnode_t *vp, offset_t ooff, offset_t *noffp) 2968789Sahrens { 2969789Sahrens if (vp->v_type == VDIR) 2970789Sahrens return (0); 2971789Sahrens return ((*noffp < 0 || *noffp > MAXOFFSET_T) ? EINVAL : 0); 2972789Sahrens } 2973789Sahrens 2974789Sahrens /* 2975789Sahrens * Pre-filter the generic locking function to trap attempts to place 2976789Sahrens * a mandatory lock on a memory mapped file. 2977789Sahrens */ 2978789Sahrens static int 2979789Sahrens zfs_frlock(vnode_t *vp, int cmd, flock64_t *bfp, int flag, offset_t offset, 2980789Sahrens flk_callback_t *flk_cbp, cred_t *cr) 2981789Sahrens { 2982789Sahrens znode_t *zp = VTOZ(vp); 2983789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 2984789Sahrens int error; 2985789Sahrens 2986789Sahrens ZFS_ENTER(zfsvfs); 2987789Sahrens 2988789Sahrens /* 29891544Seschrock * We are following the UFS semantics with respect to mapcnt 29901544Seschrock * here: If we see that the file is mapped already, then we will 29911544Seschrock * return an error, but we don't worry about races between this 29921544Seschrock * function and zfs_map(). 2993789Sahrens */ 29941544Seschrock if (zp->z_mapcnt > 0 && MANDMODE((mode_t)zp->z_phys->zp_mode)) { 2995789Sahrens ZFS_EXIT(zfsvfs); 2996789Sahrens return (EAGAIN); 2997789Sahrens } 2998789Sahrens error = fs_frlock(vp, cmd, bfp, flag, offset, flk_cbp, cr); 2999789Sahrens ZFS_EXIT(zfsvfs); 3000789Sahrens return (error); 3001789Sahrens } 3002789Sahrens 3003789Sahrens /* 3004789Sahrens * If we can't find a page in the cache, we will create a new page 3005789Sahrens * and fill it with file data. For efficiency, we may try to fill 30061669Sperrin * multiple pages at once (klustering). 3007789Sahrens */ 3008789Sahrens static int 3009789Sahrens zfs_fillpage(vnode_t *vp, u_offset_t off, struct seg *seg, 3010789Sahrens caddr_t addr, page_t *pl[], size_t plsz, enum seg_rw rw) 3011789Sahrens { 3012789Sahrens znode_t *zp = VTOZ(vp); 3013789Sahrens page_t *pp, *cur_pp; 3014789Sahrens objset_t *os = zp->z_zfsvfs->z_os; 3015789Sahrens caddr_t va; 3016789Sahrens u_offset_t io_off, total; 3017789Sahrens uint64_t oid = zp->z_id; 3018789Sahrens size_t io_len; 30191669Sperrin uint64_t filesz; 3020789Sahrens int err; 3021789Sahrens 3022789Sahrens /* 3023789Sahrens * If we are only asking for a single page don't bother klustering. 3024789Sahrens */ 30251669Sperrin filesz = zp->z_phys->zp_size; /* get consistent copy of zp_size */ 30261669Sperrin if (plsz == PAGESIZE || zp->z_blksz <= PAGESIZE || off > filesz) { 3027789Sahrens io_off = off; 3028789Sahrens io_len = PAGESIZE; 3029789Sahrens pp = page_create_va(vp, io_off, io_len, PG_WAIT, seg, addr); 3030789Sahrens } else { 3031789Sahrens /* 3032789Sahrens * Try to fill a kluster of pages (a blocks worth). 3033789Sahrens */ 3034789Sahrens size_t klen; 3035789Sahrens u_offset_t koff; 3036789Sahrens 3037789Sahrens if (!ISP2(zp->z_blksz)) { 3038789Sahrens /* Only one block in the file. */ 3039789Sahrens klen = P2ROUNDUP((ulong_t)zp->z_blksz, PAGESIZE); 3040789Sahrens koff = 0; 3041789Sahrens } else { 3042789Sahrens klen = plsz; 3043789Sahrens koff = P2ALIGN(off, (u_offset_t)klen); 3044789Sahrens } 30451819Smaybee ASSERT(koff <= filesz); 30461819Smaybee if (koff + klen > filesz) 30471819Smaybee klen = P2ROUNDUP(filesz, (uint64_t)PAGESIZE) - koff; 3048789Sahrens pp = pvn_read_kluster(vp, off, seg, addr, &io_off, 3049789Sahrens &io_len, koff, klen, 0); 3050789Sahrens } 3051789Sahrens if (pp == NULL) { 3052789Sahrens /* 3053789Sahrens * Some other thread entered the page before us. 3054789Sahrens * Return to zfs_getpage to retry the lookup. 3055789Sahrens */ 3056789Sahrens *pl = NULL; 3057789Sahrens return (0); 3058789Sahrens } 3059789Sahrens 3060789Sahrens /* 3061789Sahrens * Fill the pages in the kluster. 3062789Sahrens */ 3063789Sahrens cur_pp = pp; 3064789Sahrens for (total = io_off + io_len; io_off < total; io_off += PAGESIZE) { 3065789Sahrens ASSERT(io_off == cur_pp->p_offset); 3066789Sahrens va = ppmapin(cur_pp, PROT_READ | PROT_WRITE, (caddr_t)-1); 30671544Seschrock err = dmu_read(os, oid, io_off, PAGESIZE, va); 3068789Sahrens ppmapout(va); 3069789Sahrens if (err) { 3070789Sahrens /* On error, toss the entire kluster */ 3071789Sahrens pvn_read_done(pp, B_ERROR); 3072789Sahrens return (err); 3073789Sahrens } 3074789Sahrens cur_pp = cur_pp->p_next; 3075789Sahrens } 3076789Sahrens out: 3077789Sahrens /* 3078789Sahrens * Fill in the page list array from the kluster. If 3079789Sahrens * there are too many pages in the kluster, return 3080789Sahrens * as many pages as possible starting from the desired 3081789Sahrens * offset `off'. 3082789Sahrens * NOTE: the page list will always be null terminated. 3083789Sahrens */ 3084789Sahrens pvn_plist_init(pp, pl, plsz, off, io_len, rw); 3085789Sahrens 3086789Sahrens return (0); 3087789Sahrens } 3088789Sahrens 3089789Sahrens /* 3090789Sahrens * Return pointers to the pages for the file region [off, off + len] 3091789Sahrens * in the pl array. If plsz is greater than len, this function may 3092789Sahrens * also return page pointers from before or after the specified 3093789Sahrens * region (i.e. some region [off', off' + plsz]). These additional 3094789Sahrens * pages are only returned if they are already in the cache, or were 3095789Sahrens * created as part of a klustered read. 3096789Sahrens * 3097789Sahrens * IN: vp - vnode of file to get data from. 3098789Sahrens * off - position in file to get data from. 3099789Sahrens * len - amount of data to retrieve. 3100789Sahrens * plsz - length of provided page list. 3101789Sahrens * seg - segment to obtain pages for. 3102789Sahrens * addr - virtual address of fault. 3103789Sahrens * rw - mode of created pages. 3104789Sahrens * cr - credentials of caller. 3105789Sahrens * 3106789Sahrens * OUT: protp - protection mode of created pages. 3107789Sahrens * pl - list of pages created. 3108789Sahrens * 3109789Sahrens * RETURN: 0 if success 3110789Sahrens * error code if failure 3111789Sahrens * 3112789Sahrens * Timestamps: 3113789Sahrens * vp - atime updated 3114789Sahrens */ 3115789Sahrens /* ARGSUSED */ 3116789Sahrens static int 3117789Sahrens zfs_getpage(vnode_t *vp, offset_t off, size_t len, uint_t *protp, 3118789Sahrens page_t *pl[], size_t plsz, struct seg *seg, caddr_t addr, 3119789Sahrens enum seg_rw rw, cred_t *cr) 3120789Sahrens { 3121789Sahrens znode_t *zp = VTOZ(vp); 3122789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3123789Sahrens page_t *pp, **pl0 = pl; 31241669Sperrin rl_t *rl; 3125789Sahrens int cnt = 0, need_unlock = 0, err = 0; 3126789Sahrens 3127789Sahrens ZFS_ENTER(zfsvfs); 3128789Sahrens 3129789Sahrens if (protp) 3130789Sahrens *protp = PROT_ALL; 3131789Sahrens 3132789Sahrens ASSERT(zp->z_dbuf_held && zp->z_phys); 3133789Sahrens 3134789Sahrens /* no faultahead (for now) */ 3135789Sahrens if (pl == NULL) { 3136789Sahrens ZFS_EXIT(zfsvfs); 3137789Sahrens return (0); 3138789Sahrens } 3139789Sahrens 31401669Sperrin /* 31411669Sperrin * Make sure nobody restructures the file in the middle of the getpage. 31421669Sperrin */ 31431669Sperrin rl = zfs_range_lock(zp, off, len, RL_READER); 31441669Sperrin 3145789Sahrens /* can't fault past EOF */ 3146789Sahrens if (off >= zp->z_phys->zp_size) { 31471669Sperrin zfs_range_unlock(zp, rl); 3148789Sahrens ZFS_EXIT(zfsvfs); 3149789Sahrens return (EFAULT); 3150789Sahrens } 3151789Sahrens 3152789Sahrens /* 3153789Sahrens * If we already own the lock, then we must be page faulting 3154789Sahrens * in the middle of a write to this file (i.e., we are writing 3155789Sahrens * to this file using data from a mapped region of the file). 3156789Sahrens */ 3157789Sahrens if (!rw_owner(&zp->z_map_lock)) { 3158789Sahrens rw_enter(&zp->z_map_lock, RW_WRITER); 3159789Sahrens need_unlock = TRUE; 3160789Sahrens } 3161789Sahrens 3162789Sahrens /* 3163789Sahrens * Loop through the requested range [off, off + len] looking 3164789Sahrens * for pages. If we don't find a page, we will need to create 3165789Sahrens * a new page and fill it with data from the file. 3166789Sahrens */ 3167789Sahrens while (len > 0) { 3168789Sahrens if (plsz < PAGESIZE) 3169789Sahrens break; 3170789Sahrens if (pp = page_lookup(vp, off, SE_SHARED)) { 3171789Sahrens *pl++ = pp; 3172789Sahrens off += PAGESIZE; 3173789Sahrens addr += PAGESIZE; 3174789Sahrens len -= PAGESIZE; 3175789Sahrens plsz -= PAGESIZE; 3176789Sahrens } else { 3177789Sahrens err = zfs_fillpage(vp, off, seg, addr, pl, plsz, rw); 3178789Sahrens /* 3179789Sahrens * klustering may have changed our region 3180789Sahrens * to be block aligned. 3181789Sahrens */ 3182789Sahrens if (((pp = *pl) != 0) && (off != pp->p_offset)) { 3183789Sahrens int delta = off - pp->p_offset; 3184789Sahrens len += delta; 3185789Sahrens off -= delta; 3186789Sahrens addr -= delta; 3187789Sahrens } 3188789Sahrens while (*pl) { 3189789Sahrens pl++; 3190789Sahrens cnt++; 3191789Sahrens off += PAGESIZE; 3192789Sahrens addr += PAGESIZE; 3193789Sahrens plsz -= PAGESIZE; 3194789Sahrens if (len > PAGESIZE) 3195789Sahrens len -= PAGESIZE; 3196789Sahrens else 3197789Sahrens len = 0; 3198789Sahrens } 31991669Sperrin if (err) { 32001669Sperrin /* 32011669Sperrin * Release any pages we have locked. 32021669Sperrin */ 32031669Sperrin while (pl > pl0) 32041669Sperrin page_unlock(*--pl); 32051669Sperrin goto out; 32061669Sperrin } 3207789Sahrens } 3208789Sahrens } 3209789Sahrens 3210789Sahrens /* 3211789Sahrens * Fill out the page array with any pages already in the cache. 3212789Sahrens */ 3213789Sahrens while (plsz > 0) { 3214789Sahrens pp = page_lookup_nowait(vp, off, SE_SHARED); 3215789Sahrens if (pp == NULL) 3216789Sahrens break; 3217789Sahrens *pl++ = pp; 3218789Sahrens off += PAGESIZE; 3219789Sahrens plsz -= PAGESIZE; 3220789Sahrens } 3221789Sahrens 3222789Sahrens ZFS_ACCESSTIME_STAMP(zfsvfs, zp); 3223789Sahrens out: 3224789Sahrens *pl = NULL; 3225789Sahrens 3226789Sahrens if (need_unlock) 3227789Sahrens rw_exit(&zp->z_map_lock); 32281669Sperrin zfs_range_unlock(zp, rl); 3229789Sahrens 3230789Sahrens ZFS_EXIT(zfsvfs); 3231789Sahrens return (err); 3232789Sahrens } 3233789Sahrens 32341544Seschrock /* 32351544Seschrock * Request a memory map for a section of a file. This code interacts 32361544Seschrock * with common code and the VM system as follows: 32371544Seschrock * 32381544Seschrock * common code calls mmap(), which ends up in smmap_common() 32391544Seschrock * 32401544Seschrock * this calls VOP_MAP(), which takes you into (say) zfs 32411544Seschrock * 32421544Seschrock * zfs_map() calls as_map(), passing segvn_create() as the callback 32431544Seschrock * 32441544Seschrock * segvn_create() creates the new segment and calls VOP_ADDMAP() 32451544Seschrock * 32461544Seschrock * zfs_addmap() updates z_mapcnt 32471544Seschrock */ 3248789Sahrens static int 3249789Sahrens zfs_map(vnode_t *vp, offset_t off, struct as *as, caddr_t *addrp, 3250789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3251789Sahrens { 3252789Sahrens znode_t *zp = VTOZ(vp); 3253789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3254789Sahrens segvn_crargs_t vn_a; 3255789Sahrens int error; 3256789Sahrens 3257789Sahrens ZFS_ENTER(zfsvfs); 3258789Sahrens 3259789Sahrens if (vp->v_flag & VNOMAP) { 3260789Sahrens ZFS_EXIT(zfsvfs); 3261789Sahrens return (ENOSYS); 3262789Sahrens } 3263789Sahrens 3264789Sahrens if (off < 0 || len > MAXOFFSET_T - off) { 3265789Sahrens ZFS_EXIT(zfsvfs); 3266789Sahrens return (ENXIO); 3267789Sahrens } 3268789Sahrens 3269789Sahrens if (vp->v_type != VREG) { 3270789Sahrens ZFS_EXIT(zfsvfs); 3271789Sahrens return (ENODEV); 3272789Sahrens } 3273789Sahrens 3274789Sahrens /* 3275789Sahrens * If file is locked, disallow mapping. 3276789Sahrens */ 32771544Seschrock if (MANDMODE((mode_t)zp->z_phys->zp_mode) && vn_has_flocks(vp)) { 32781544Seschrock ZFS_EXIT(zfsvfs); 32791544Seschrock return (EAGAIN); 3280789Sahrens } 3281789Sahrens 3282789Sahrens as_rangelock(as); 3283789Sahrens if ((flags & MAP_FIXED) == 0) { 3284789Sahrens map_addr(addrp, len, off, 1, flags); 3285789Sahrens if (*addrp == NULL) { 3286789Sahrens as_rangeunlock(as); 3287789Sahrens ZFS_EXIT(zfsvfs); 3288789Sahrens return (ENOMEM); 3289789Sahrens } 3290789Sahrens } else { 3291789Sahrens /* 3292789Sahrens * User specified address - blow away any previous mappings 3293789Sahrens */ 3294789Sahrens (void) as_unmap(as, *addrp, len); 3295789Sahrens } 3296789Sahrens 3297789Sahrens vn_a.vp = vp; 3298789Sahrens vn_a.offset = (u_offset_t)off; 3299789Sahrens vn_a.type = flags & MAP_TYPE; 3300789Sahrens vn_a.prot = prot; 3301789Sahrens vn_a.maxprot = maxprot; 3302789Sahrens vn_a.cred = cr; 3303789Sahrens vn_a.amp = NULL; 3304789Sahrens vn_a.flags = flags & ~MAP_TYPE; 33051417Skchow vn_a.szc = 0; 33061417Skchow vn_a.lgrp_mem_policy_flags = 0; 3307789Sahrens 3308789Sahrens error = as_map(as, *addrp, len, segvn_create, &vn_a); 3309789Sahrens 3310789Sahrens as_rangeunlock(as); 3311789Sahrens ZFS_EXIT(zfsvfs); 3312789Sahrens return (error); 3313789Sahrens } 3314789Sahrens 3315789Sahrens /* ARGSUSED */ 3316789Sahrens static int 3317789Sahrens zfs_addmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3318789Sahrens size_t len, uchar_t prot, uchar_t maxprot, uint_t flags, cred_t *cr) 3319789Sahrens { 33201544Seschrock uint64_t pages = btopr(len); 33211544Seschrock 33221544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, pages); 3323789Sahrens return (0); 3324789Sahrens } 3325789Sahrens 33261773Seschrock /* 33271773Seschrock * The reason we push dirty pages as part of zfs_delmap() is so that we get a 33281773Seschrock * more accurate mtime for the associated file. Since we don't have a way of 33291773Seschrock * detecting when the data was actually modified, we have to resort to 33301773Seschrock * heuristics. If an explicit msync() is done, then we mark the mtime when the 33311773Seschrock * last page is pushed. The problem occurs when the msync() call is omitted, 33321773Seschrock * which by far the most common case: 33331773Seschrock * 33341773Seschrock * open() 33351773Seschrock * mmap() 33361773Seschrock * <modify memory> 33371773Seschrock * munmap() 33381773Seschrock * close() 33391773Seschrock * <time lapse> 33401773Seschrock * putpage() via fsflush 33411773Seschrock * 33421773Seschrock * If we wait until fsflush to come along, we can have a modification time that 33431773Seschrock * is some arbitrary point in the future. In order to prevent this in the 33441773Seschrock * common case, we flush pages whenever a (MAP_SHARED, PROT_WRITE) mapping is 33451773Seschrock * torn down. 33461773Seschrock */ 3347789Sahrens /* ARGSUSED */ 3348789Sahrens static int 3349789Sahrens zfs_delmap(vnode_t *vp, offset_t off, struct as *as, caddr_t addr, 3350789Sahrens size_t len, uint_t prot, uint_t maxprot, uint_t flags, cred_t *cr) 3351789Sahrens { 33521544Seschrock uint64_t pages = btopr(len); 33531544Seschrock 33541544Seschrock ASSERT3U(VTOZ(vp)->z_mapcnt, >=, pages); 33551544Seschrock atomic_add_64(&VTOZ(vp)->z_mapcnt, -pages); 33561773Seschrock 33571773Seschrock if ((flags & MAP_SHARED) && (prot & PROT_WRITE) && 33581773Seschrock vn_has_cached_data(vp)) 33591773Seschrock (void) VOP_PUTPAGE(vp, off, len, B_ASYNC, cr); 33601773Seschrock 3361789Sahrens return (0); 3362789Sahrens } 3363789Sahrens 3364789Sahrens /* 3365789Sahrens * Free or allocate space in a file. Currently, this function only 3366789Sahrens * supports the `F_FREESP' command. However, this command is somewhat 3367789Sahrens * misnamed, as its functionality includes the ability to allocate as 3368789Sahrens * well as free space. 3369789Sahrens * 3370789Sahrens * IN: vp - vnode of file to free data in. 3371789Sahrens * cmd - action to take (only F_FREESP supported). 3372789Sahrens * bfp - section of file to free/alloc. 3373789Sahrens * flag - current file open mode flags. 3374789Sahrens * offset - current file offset. 3375789Sahrens * cr - credentials of caller [UNUSED]. 3376789Sahrens * 3377789Sahrens * RETURN: 0 if success 3378789Sahrens * error code if failure 3379789Sahrens * 3380789Sahrens * Timestamps: 3381789Sahrens * vp - ctime|mtime updated 3382789Sahrens */ 3383789Sahrens /* ARGSUSED */ 3384789Sahrens static int 3385789Sahrens zfs_space(vnode_t *vp, int cmd, flock64_t *bfp, int flag, 3386789Sahrens offset_t offset, cred_t *cr, caller_context_t *ct) 3387789Sahrens { 3388789Sahrens znode_t *zp = VTOZ(vp); 3389789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3390789Sahrens uint64_t off, len; 3391789Sahrens int error; 3392789Sahrens 3393789Sahrens ZFS_ENTER(zfsvfs); 3394789Sahrens 3395789Sahrens top: 3396789Sahrens if (cmd != F_FREESP) { 3397789Sahrens ZFS_EXIT(zfsvfs); 3398789Sahrens return (EINVAL); 3399789Sahrens } 3400789Sahrens 3401789Sahrens if (error = convoff(vp, bfp, 0, offset)) { 3402789Sahrens ZFS_EXIT(zfsvfs); 3403789Sahrens return (error); 3404789Sahrens } 3405789Sahrens 3406789Sahrens if (bfp->l_len < 0) { 3407789Sahrens ZFS_EXIT(zfsvfs); 3408789Sahrens return (EINVAL); 3409789Sahrens } 3410789Sahrens 3411789Sahrens off = bfp->l_start; 34121669Sperrin len = bfp->l_len; /* 0 means from off to end of file */ 34131878Smaybee 34141878Smaybee do { 34151878Smaybee if (error == ERESTART) 3416789Sahrens txg_wait_open(dmu_objset_pool(zfsvfs->z_os), 0); 34171878Smaybee error = zfs_freesp(zp, off, len, flag, TRUE); 34181878Smaybee } while (error == ERESTART && zfsvfs->z_assign == TXG_NOWAIT); 3419789Sahrens 3420789Sahrens ZFS_EXIT(zfsvfs); 3421789Sahrens return (error); 3422789Sahrens } 3423789Sahrens 3424789Sahrens static int 3425789Sahrens zfs_fid(vnode_t *vp, fid_t *fidp) 3426789Sahrens { 3427789Sahrens znode_t *zp = VTOZ(vp); 3428789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3429789Sahrens uint32_t gen = (uint32_t)zp->z_phys->zp_gen; 3430789Sahrens uint64_t object = zp->z_id; 3431789Sahrens zfid_short_t *zfid; 3432789Sahrens int size, i; 3433789Sahrens 3434789Sahrens ZFS_ENTER(zfsvfs); 3435789Sahrens 3436789Sahrens size = (zfsvfs->z_parent != zfsvfs) ? LONG_FID_LEN : SHORT_FID_LEN; 3437789Sahrens if (fidp->fid_len < size) { 3438789Sahrens fidp->fid_len = size; 34391512Sek110237 ZFS_EXIT(zfsvfs); 3440789Sahrens return (ENOSPC); 3441789Sahrens } 3442789Sahrens 3443789Sahrens zfid = (zfid_short_t *)fidp; 3444789Sahrens 3445789Sahrens zfid->zf_len = size; 3446789Sahrens 3447789Sahrens for (i = 0; i < sizeof (zfid->zf_object); i++) 3448789Sahrens zfid->zf_object[i] = (uint8_t)(object >> (8 * i)); 3449789Sahrens 3450789Sahrens /* Must have a non-zero generation number to distinguish from .zfs */ 3451789Sahrens if (gen == 0) 3452789Sahrens gen = 1; 3453789Sahrens for (i = 0; i < sizeof (zfid->zf_gen); i++) 3454789Sahrens zfid->zf_gen[i] = (uint8_t)(gen >> (8 * i)); 3455789Sahrens 3456789Sahrens if (size == LONG_FID_LEN) { 3457789Sahrens uint64_t objsetid = dmu_objset_id(zfsvfs->z_os); 3458789Sahrens zfid_long_t *zlfid; 3459789Sahrens 3460789Sahrens zlfid = (zfid_long_t *)fidp; 3461789Sahrens 3462789Sahrens for (i = 0; i < sizeof (zlfid->zf_setid); i++) 3463789Sahrens zlfid->zf_setid[i] = (uint8_t)(objsetid >> (8 * i)); 3464789Sahrens 3465789Sahrens /* XXX - this should be the generation number for the objset */ 3466789Sahrens for (i = 0; i < sizeof (zlfid->zf_setgen); i++) 3467789Sahrens zlfid->zf_setgen[i] = 0; 3468789Sahrens } 3469789Sahrens 3470789Sahrens ZFS_EXIT(zfsvfs); 3471789Sahrens return (0); 3472789Sahrens } 3473789Sahrens 3474789Sahrens static int 3475789Sahrens zfs_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr) 3476789Sahrens { 3477789Sahrens znode_t *zp, *xzp; 3478789Sahrens zfsvfs_t *zfsvfs; 3479789Sahrens zfs_dirlock_t *dl; 3480789Sahrens int error; 3481789Sahrens 3482789Sahrens switch (cmd) { 3483789Sahrens case _PC_LINK_MAX: 3484789Sahrens *valp = ULONG_MAX; 3485789Sahrens return (0); 3486789Sahrens 3487789Sahrens case _PC_FILESIZEBITS: 3488789Sahrens *valp = 64; 3489789Sahrens return (0); 3490789Sahrens 3491789Sahrens case _PC_XATTR_EXISTS: 3492789Sahrens zp = VTOZ(vp); 3493789Sahrens zfsvfs = zp->z_zfsvfs; 3494789Sahrens ZFS_ENTER(zfsvfs); 3495789Sahrens *valp = 0; 3496789Sahrens error = zfs_dirent_lock(&dl, zp, "", &xzp, 3497789Sahrens ZXATTR | ZEXISTS | ZSHARED); 3498789Sahrens if (error == 0) { 3499789Sahrens zfs_dirent_unlock(dl); 3500789Sahrens if (!zfs_dirempty(xzp)) 3501789Sahrens *valp = 1; 3502789Sahrens VN_RELE(ZTOV(xzp)); 3503789Sahrens } else if (error == ENOENT) { 3504789Sahrens /* 3505789Sahrens * If there aren't extended attributes, it's the 3506789Sahrens * same as having zero of them. 3507789Sahrens */ 3508789Sahrens error = 0; 3509789Sahrens } 3510789Sahrens ZFS_EXIT(zfsvfs); 3511789Sahrens return (error); 3512789Sahrens 3513789Sahrens case _PC_ACL_ENABLED: 3514789Sahrens *valp = _ACL_ACE_ENABLED; 3515789Sahrens return (0); 3516789Sahrens 3517789Sahrens case _PC_MIN_HOLE_SIZE: 3518789Sahrens *valp = (ulong_t)SPA_MINBLOCKSIZE; 3519789Sahrens return (0); 3520789Sahrens 3521789Sahrens default: 3522789Sahrens return (fs_pathconf(vp, cmd, valp, cr)); 3523789Sahrens } 3524789Sahrens } 3525789Sahrens 3526789Sahrens /*ARGSUSED*/ 3527789Sahrens static int 3528789Sahrens zfs_getsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3529789Sahrens { 3530789Sahrens znode_t *zp = VTOZ(vp); 3531789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3532789Sahrens int error; 3533789Sahrens 3534789Sahrens ZFS_ENTER(zfsvfs); 3535789Sahrens error = zfs_getacl(zp, vsecp, cr); 3536789Sahrens ZFS_EXIT(zfsvfs); 3537789Sahrens 3538789Sahrens return (error); 3539789Sahrens } 3540789Sahrens 3541789Sahrens /*ARGSUSED*/ 3542789Sahrens static int 3543789Sahrens zfs_setsecattr(vnode_t *vp, vsecattr_t *vsecp, int flag, cred_t *cr) 3544789Sahrens { 3545789Sahrens znode_t *zp = VTOZ(vp); 3546789Sahrens zfsvfs_t *zfsvfs = zp->z_zfsvfs; 3547789Sahrens int error; 3548789Sahrens 3549789Sahrens ZFS_ENTER(zfsvfs); 3550789Sahrens error = zfs_setacl(zp, vsecp, cr); 3551789Sahrens ZFS_EXIT(zfsvfs); 3552789Sahrens return (error); 3553789Sahrens } 3554789Sahrens 3555789Sahrens /* 3556789Sahrens * Predeclare these here so that the compiler assumes that 3557789Sahrens * this is an "old style" function declaration that does 3558789Sahrens * not include arguments => we won't get type mismatch errors 3559789Sahrens * in the initializations that follow. 3560789Sahrens */ 3561789Sahrens static int zfs_inval(); 3562789Sahrens static int zfs_isdir(); 3563789Sahrens 3564789Sahrens static int 3565789Sahrens zfs_inval() 3566789Sahrens { 3567789Sahrens return (EINVAL); 3568789Sahrens } 3569789Sahrens 3570789Sahrens static int 3571789Sahrens zfs_isdir() 3572789Sahrens { 3573789Sahrens return (EISDIR); 3574789Sahrens } 3575789Sahrens /* 3576789Sahrens * Directory vnode operations template 3577789Sahrens */ 3578789Sahrens vnodeops_t *zfs_dvnodeops; 3579789Sahrens const fs_operation_def_t zfs_dvnodeops_template[] = { 3580789Sahrens VOPNAME_OPEN, zfs_open, 3581789Sahrens VOPNAME_CLOSE, zfs_close, 3582789Sahrens VOPNAME_READ, zfs_isdir, 3583789Sahrens VOPNAME_WRITE, zfs_isdir, 3584789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3585789Sahrens VOPNAME_GETATTR, zfs_getattr, 3586789Sahrens VOPNAME_SETATTR, zfs_setattr, 3587789Sahrens VOPNAME_ACCESS, zfs_access, 3588789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3589789Sahrens VOPNAME_CREATE, zfs_create, 3590789Sahrens VOPNAME_REMOVE, zfs_remove, 3591789Sahrens VOPNAME_LINK, zfs_link, 3592789Sahrens VOPNAME_RENAME, zfs_rename, 3593789Sahrens VOPNAME_MKDIR, zfs_mkdir, 3594789Sahrens VOPNAME_RMDIR, zfs_rmdir, 3595789Sahrens VOPNAME_READDIR, zfs_readdir, 3596789Sahrens VOPNAME_SYMLINK, zfs_symlink, 3597789Sahrens VOPNAME_FSYNC, zfs_fsync, 3598789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3599789Sahrens VOPNAME_FID, zfs_fid, 3600789Sahrens VOPNAME_SEEK, zfs_seek, 3601789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3602789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3603789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3604789Sahrens NULL, NULL 3605789Sahrens }; 3606789Sahrens 3607789Sahrens /* 3608789Sahrens * Regular file vnode operations template 3609789Sahrens */ 3610789Sahrens vnodeops_t *zfs_fvnodeops; 3611789Sahrens const fs_operation_def_t zfs_fvnodeops_template[] = { 3612789Sahrens VOPNAME_OPEN, zfs_open, 3613789Sahrens VOPNAME_CLOSE, zfs_close, 3614789Sahrens VOPNAME_READ, zfs_read, 3615789Sahrens VOPNAME_WRITE, zfs_write, 3616789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3617789Sahrens VOPNAME_GETATTR, zfs_getattr, 3618789Sahrens VOPNAME_SETATTR, zfs_setattr, 3619789Sahrens VOPNAME_ACCESS, zfs_access, 3620789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3621789Sahrens VOPNAME_RENAME, zfs_rename, 3622789Sahrens VOPNAME_FSYNC, zfs_fsync, 3623789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p)zfs_inactive, 3624789Sahrens VOPNAME_FID, zfs_fid, 3625789Sahrens VOPNAME_SEEK, zfs_seek, 3626789Sahrens VOPNAME_FRLOCK, zfs_frlock, 3627789Sahrens VOPNAME_SPACE, zfs_space, 3628789Sahrens VOPNAME_GETPAGE, zfs_getpage, 3629789Sahrens VOPNAME_PUTPAGE, zfs_putpage, 3630789Sahrens VOPNAME_MAP, (fs_generic_func_p) zfs_map, 3631789Sahrens VOPNAME_ADDMAP, (fs_generic_func_p) zfs_addmap, 3632789Sahrens VOPNAME_DELMAP, zfs_delmap, 3633789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3634789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3635789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3636789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3637789Sahrens NULL, NULL 3638789Sahrens }; 3639789Sahrens 3640789Sahrens /* 3641789Sahrens * Symbolic link vnode operations template 3642789Sahrens */ 3643789Sahrens vnodeops_t *zfs_symvnodeops; 3644789Sahrens const fs_operation_def_t zfs_symvnodeops_template[] = { 3645789Sahrens VOPNAME_GETATTR, zfs_getattr, 3646789Sahrens VOPNAME_SETATTR, zfs_setattr, 3647789Sahrens VOPNAME_ACCESS, zfs_access, 3648789Sahrens VOPNAME_RENAME, zfs_rename, 3649789Sahrens VOPNAME_READLINK, zfs_readlink, 3650789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3651789Sahrens VOPNAME_FID, zfs_fid, 3652789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3653789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3654789Sahrens NULL, NULL 3655789Sahrens }; 3656789Sahrens 3657789Sahrens /* 3658789Sahrens * Extended attribute directory vnode operations template 3659789Sahrens * This template is identical to the directory vnodes 3660789Sahrens * operation template except for restricted operations: 3661789Sahrens * VOP_MKDIR() 3662789Sahrens * VOP_SYMLINK() 3663789Sahrens * Note that there are other restrictions embedded in: 3664789Sahrens * zfs_create() - restrict type to VREG 3665789Sahrens * zfs_link() - no links into/out of attribute space 3666789Sahrens * zfs_rename() - no moves into/out of attribute space 3667789Sahrens */ 3668789Sahrens vnodeops_t *zfs_xdvnodeops; 3669789Sahrens const fs_operation_def_t zfs_xdvnodeops_template[] = { 3670789Sahrens VOPNAME_OPEN, zfs_open, 3671789Sahrens VOPNAME_CLOSE, zfs_close, 3672789Sahrens VOPNAME_IOCTL, zfs_ioctl, 3673789Sahrens VOPNAME_GETATTR, zfs_getattr, 3674789Sahrens VOPNAME_SETATTR, zfs_setattr, 3675789Sahrens VOPNAME_ACCESS, zfs_access, 3676789Sahrens VOPNAME_LOOKUP, zfs_lookup, 3677789Sahrens VOPNAME_CREATE, zfs_create, 3678789Sahrens VOPNAME_REMOVE, zfs_remove, 3679789Sahrens VOPNAME_LINK, zfs_link, 3680789Sahrens VOPNAME_RENAME, zfs_rename, 3681789Sahrens VOPNAME_MKDIR, zfs_inval, 3682789Sahrens VOPNAME_RMDIR, zfs_rmdir, 3683789Sahrens VOPNAME_READDIR, zfs_readdir, 3684789Sahrens VOPNAME_SYMLINK, zfs_inval, 3685789Sahrens VOPNAME_FSYNC, zfs_fsync, 3686789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3687789Sahrens VOPNAME_FID, zfs_fid, 3688789Sahrens VOPNAME_SEEK, zfs_seek, 3689789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3690789Sahrens VOPNAME_GETSECATTR, zfs_getsecattr, 3691789Sahrens VOPNAME_SETSECATTR, zfs_setsecattr, 3692789Sahrens VOPNAME_VNEVENT, fs_vnevent_support, 3693789Sahrens NULL, NULL 3694789Sahrens }; 3695789Sahrens 3696789Sahrens /* 3697789Sahrens * Error vnode operations template 3698789Sahrens */ 3699789Sahrens vnodeops_t *zfs_evnodeops; 3700789Sahrens const fs_operation_def_t zfs_evnodeops_template[] = { 3701789Sahrens VOPNAME_INACTIVE, (fs_generic_func_p) zfs_inactive, 3702789Sahrens VOPNAME_PATHCONF, zfs_pathconf, 3703789Sahrens NULL, NULL 3704789Sahrens }; 3705