xref: /illumos-gate/usr/src/uts/common/fs/dev/sdev_vnops.c (revision f8927fa6)
1facf4a8dSllai /*
2facf4a8dSllai  * CDDL HEADER START
3facf4a8dSllai  *
4facf4a8dSllai  * The contents of this file are subject to the terms of the
5facf4a8dSllai  * Common Development and Distribution License (the "License").
6facf4a8dSllai  * You may not use this file except in compliance with the License.
7facf4a8dSllai  *
8facf4a8dSllai  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9facf4a8dSllai  * or http://www.opensolaris.org/os/licensing.
10facf4a8dSllai  * See the License for the specific language governing permissions
11facf4a8dSllai  * and limitations under the License.
12facf4a8dSllai  *
13facf4a8dSllai  * When distributing Covered Code, include this CDDL HEADER in each
14facf4a8dSllai  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15facf4a8dSllai  * If applicable, add the following below this CDDL HEADER, with the
16facf4a8dSllai  * fields enclosed by brackets "[]" replaced with your own identifying
17facf4a8dSllai  * information: Portions Copyright [yyyy] [name of copyright owner]
18facf4a8dSllai  *
19facf4a8dSllai  * CDDL HEADER END
20facf4a8dSllai  */
21facf4a8dSllai /*
22134a1f4eSCasper H.S. Dik  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23facf4a8dSllai  */
249e5aa9d8SRobert Mustacchi /*
25*f8927fa6SRobert Mustacchi  * Copyright 2018, Joyent, Inc.
269e5aa9d8SRobert Mustacchi  */
27facf4a8dSllai 
28facf4a8dSllai /*
29facf4a8dSllai  * vnode ops for the /dev filesystem
30facf4a8dSllai  *
31facf4a8dSllai  * - VDIR, VCHR, CBLK, and VLNK are considered must supported files
32facf4a8dSllai  * - VREG and VDOOR are used for some internal implementations in
33facf4a8dSllai  *    the global zone, e.g. devname and devfsadm communication
34facf4a8dSllai  * - other file types are unusual in this namespace and
35facf4a8dSllai  *    not supported for now
36facf4a8dSllai  */
37facf4a8dSllai 
389e5aa9d8SRobert Mustacchi /*
399e5aa9d8SRobert Mustacchi  * sdev has a few basic goals:
409e5aa9d8SRobert Mustacchi  *   o Provide /dev for the global zone as well as various non-global zones.
419e5aa9d8SRobert Mustacchi  *   o Provide the basic functionality that devfsadm might need (mknod,
429e5aa9d8SRobert Mustacchi  *     symlinks, etc.)
439e5aa9d8SRobert Mustacchi  *   o Allow persistent permissions on files in /dev.
449e5aa9d8SRobert Mustacchi  *   o Allow for dynamic directories and nodes for use by various services (pts,
459e5aa9d8SRobert Mustacchi  *     zvol, net, etc.)
469e5aa9d8SRobert Mustacchi  *
479e5aa9d8SRobert Mustacchi  * The sdev file system is primarily made up of sdev_node_t's which is sdev's
489e5aa9d8SRobert Mustacchi  * counterpart to the vnode_t. There are two different classes of sdev_node_t's
499e5aa9d8SRobert Mustacchi  * that we generally care about, dynamic and otherwise.
509e5aa9d8SRobert Mustacchi  *
519e5aa9d8SRobert Mustacchi  * Persisting Information
529e5aa9d8SRobert Mustacchi  * ----------------------
539e5aa9d8SRobert Mustacchi  *
549e5aa9d8SRobert Mustacchi  * When sdev is mounted, it keeps track of the underlying file system it is
559e5aa9d8SRobert Mustacchi  * mounted over. In certain situations, sdev will go and create entries in that
569e5aa9d8SRobert Mustacchi  * underlying file system. These underlying 'back end' nodes are used as proxies
579e5aa9d8SRobert Mustacchi  * for various changes in permissions. While specific sets of nodes, such as
589e5aa9d8SRobert Mustacchi  * dynamic ones, are exempt, this process stores permission changes against
599e5aa9d8SRobert Mustacchi  * these back end nodes. The point of all of this is to allow for these settings
609e5aa9d8SRobert Mustacchi  * to persist across host and zone reboots. As an example, consider the entry
619e5aa9d8SRobert Mustacchi  * /dev/dsk/c0t0d0 which is a character device and that / is in UFS. Upon
629e5aa9d8SRobert Mustacchi  * changing the permissions on c0t0d0 you'd have the following logical
639e5aa9d8SRobert Mustacchi  * relationships:
649e5aa9d8SRobert Mustacchi  *
659e5aa9d8SRobert Mustacchi  *    +------------------+   sdev_vnode     +--------------+
669e5aa9d8SRobert Mustacchi  *    | sdev_node_t      |<---------------->| vnode_t      |
679e5aa9d8SRobert Mustacchi  *    | /dev/dsk/c0t0d0  |<---------------->| for sdev     |
689e5aa9d8SRobert Mustacchi  *    +------------------+                  +--------------+
699e5aa9d8SRobert Mustacchi  *           |
709e5aa9d8SRobert Mustacchi  *           | sdev_attrvp
719e5aa9d8SRobert Mustacchi  *           |
729e5aa9d8SRobert Mustacchi  *           |    +---------------------+
739e5aa9d8SRobert Mustacchi  *           +--->| vnode_t for UFS|ZFS |
749e5aa9d8SRobert Mustacchi  *                | /dev/dsk/c0t0d0     |
759e5aa9d8SRobert Mustacchi  *                +---------------------+
769e5aa9d8SRobert Mustacchi  *
779e5aa9d8SRobert Mustacchi  * sdev is generally in memory. Therefore when a lookup happens and there is no
789e5aa9d8SRobert Mustacchi  * entry already inside of a directory cache, it will next check the backing
799e5aa9d8SRobert Mustacchi  * store. If the backing store exists, we will reconstitute the sdev_node based
809e5aa9d8SRobert Mustacchi  * on the information that we persisted. When we create the backing store node,
819e5aa9d8SRobert Mustacchi  * we use the struct vattr information that we already have in sdev_node_t.
829e5aa9d8SRobert Mustacchi  * Because of this, we already know if the entry was previously a symlink,
839e5aa9d8SRobert Mustacchi  * directory, or some other kind of type. Note that not all types of nodes are
849e5aa9d8SRobert Mustacchi  * supported. Currently only VDIR, VCHR, VBLK, VREG, VDOOR, and VLNK are
859e5aa9d8SRobert Mustacchi  * eligible to be persisted.
869e5aa9d8SRobert Mustacchi  *
879e5aa9d8SRobert Mustacchi  * When the sdev_node is created and the lookup is done, we grab a hold on the
889e5aa9d8SRobert Mustacchi  * underlying vnode as part of the call to VOP_LOOKUP. That reference is held
899e5aa9d8SRobert Mustacchi  * until the sdev_node becomes inactive. Once its reference count reaches one
909e5aa9d8SRobert Mustacchi  * and the VOP_INACTIVE callback fires leading to the destruction of the node,
919e5aa9d8SRobert Mustacchi  * the reference on the underlying vnode will be released.
929e5aa9d8SRobert Mustacchi  *
939e5aa9d8SRobert Mustacchi  * The backing store node will be deleted only when the node itself is deleted
949e5aa9d8SRobert Mustacchi  * through the means of a VOP_REMOVE, VOP_RMDIR, or similar call.
959e5aa9d8SRobert Mustacchi  *
969e5aa9d8SRobert Mustacchi  * Not everything can be persisted, see The Rules section for more details.
979e5aa9d8SRobert Mustacchi  *
989e5aa9d8SRobert Mustacchi  * Dynamic Nodes
999e5aa9d8SRobert Mustacchi  * -------------
1009e5aa9d8SRobert Mustacchi  *
1019e5aa9d8SRobert Mustacchi  * Dynamic nodes allow for specific interactions with various kernel subsystems
1029e5aa9d8SRobert Mustacchi  * when looking up directory entries. This allows the lookup and readdir
1039e5aa9d8SRobert Mustacchi  * functions to check against the kernel subsystem's for validity. eg. does a
1049e5aa9d8SRobert Mustacchi  * zvol or nic still exist.
1059e5aa9d8SRobert Mustacchi  *
1069e5aa9d8SRobert Mustacchi  * More specifically, when we create various directories we check if the
1079e5aa9d8SRobert Mustacchi  * directory name matches that of one of the names in the vtab[] (sdev_subr.c).
1089e5aa9d8SRobert Mustacchi  * If it does, we swap out the vnode operations into a new set which combine the
1099e5aa9d8SRobert Mustacchi  * normal sdev vnode operations with the dynamic set here.
1109e5aa9d8SRobert Mustacchi  *
1119e5aa9d8SRobert Mustacchi  * In addition, various dynamic nodes implement a verification entry point. This
1129e5aa9d8SRobert Mustacchi  * verification entry is used as a part of lookup and readdir. The goal for
1139e5aa9d8SRobert Mustacchi  * these dynamic nodes is to allow them to check with the underlying subsystems
1149e5aa9d8SRobert Mustacchi  * to ensure that these devices are still present, or if they have gone away, to
1159e5aa9d8SRobert Mustacchi  * remove them from the results. This is indicated by using the SDEV_VTOR flag
1169e5aa9d8SRobert Mustacchi  * in vtab[].
1179e5aa9d8SRobert Mustacchi  *
1189e5aa9d8SRobert Mustacchi  * Dynamic nodes have additional restrictions placed upon them. They may only
1199e5aa9d8SRobert Mustacchi  * appear at the top level directory of the file system. In addition, users
1209e5aa9d8SRobert Mustacchi  * cannot create dirents below any leve of a dynamic node aside from its special
1219e5aa9d8SRobert Mustacchi  * vnops.
1229e5aa9d8SRobert Mustacchi  *
1239e5aa9d8SRobert Mustacchi  * Profiles
1249e5aa9d8SRobert Mustacchi  * --------
1259e5aa9d8SRobert Mustacchi  *
1269e5aa9d8SRobert Mustacchi  * Profiles exist for the purpose of non-global zones. They work with the zone
1279e5aa9d8SRobert Mustacchi  * brands and zoneadmd to set up a filter of allowed devices that can appear in
1289e5aa9d8SRobert Mustacchi  * a non-global zone's /dev. These are sent to sdev by means of libdevinfo and a
1299e5aa9d8SRobert Mustacchi  * modctl system call. Specifically it allows one to add patterns of device
1309e5aa9d8SRobert Mustacchi  * paths to include and exclude. It allows for a collection of symlinks to be
1319e5aa9d8SRobert Mustacchi  * added and it allows for remapping names.
1329e5aa9d8SRobert Mustacchi  *
1339e5aa9d8SRobert Mustacchi  * When operating in a non-global zone, several of the sdev vnops are redirected
1349e5aa9d8SRobert Mustacchi  * to the profile versions. These impose additional restrictions such as
1359e5aa9d8SRobert Mustacchi  * enforcing that a non-global zone's /dev is read only.
1369e5aa9d8SRobert Mustacchi  *
1379e5aa9d8SRobert Mustacchi  * sdev_node_t States
1389e5aa9d8SRobert Mustacchi  * ------------------
1399e5aa9d8SRobert Mustacchi  *
1409e5aa9d8SRobert Mustacchi  * A given sdev_node_t has a field called the sdev_state which describes where
1419e5aa9d8SRobert Mustacchi  * in the sdev life cycle it is. There are three primary states: SDEV_INIT,
1429e5aa9d8SRobert Mustacchi  * SDEV_READY, and SDEV_ZOMBIE.
1439e5aa9d8SRobert Mustacchi  *
1449e5aa9d8SRobert Mustacchi  *	SDEV_INIT: When a new /dev file is first looked up, a sdev_node
1459e5aa9d8SRobert Mustacchi  *		   is allocated, initialized and added to the directory's
1469e5aa9d8SRobert Mustacchi  *		   sdev_node cache. A node at this state will also
1479e5aa9d8SRobert Mustacchi  *		   have the SDEV_LOOKUP flag set.
1489e5aa9d8SRobert Mustacchi  *
1499e5aa9d8SRobert Mustacchi  *		   Other threads that are trying to look up a node at
1509e5aa9d8SRobert Mustacchi  *		   this state will be blocked until the SDEV_LOOKUP flag
1519e5aa9d8SRobert Mustacchi  *		   is cleared.
1529e5aa9d8SRobert Mustacchi  *
1539e5aa9d8SRobert Mustacchi  *		   When the SDEV_LOOKUP flag is cleared, the node may
1549e5aa9d8SRobert Mustacchi  *		   transition into the SDEV_READY state for a successful
1559e5aa9d8SRobert Mustacchi  *		   lookup or the node is removed from the directory cache
1569e5aa9d8SRobert Mustacchi  *		   and destroyed if the named node can not be found.
1579e5aa9d8SRobert Mustacchi  *		   An ENOENT error is returned for the second case.
1589e5aa9d8SRobert Mustacchi  *
1599e5aa9d8SRobert Mustacchi  *	SDEV_READY: A /dev file has been successfully looked up and
1609e5aa9d8SRobert Mustacchi  *		    associated with a vnode. The /dev file is available
1619e5aa9d8SRobert Mustacchi  *		    for the supported /dev file system operations.
1629e5aa9d8SRobert Mustacchi  *
1639e5aa9d8SRobert Mustacchi  *	SDEV_ZOMBIE: Deletion of a /dev file has been explicitly issued
1649e5aa9d8SRobert Mustacchi  *		    to an SDEV_READY node. The node is transitioned into
1659e5aa9d8SRobert Mustacchi  *		    the SDEV_ZOMBIE state if the vnode reference count
1669e5aa9d8SRobert Mustacchi  *		    is still held. A SDEV_ZOMBIE node does not support
1679e5aa9d8SRobert Mustacchi  *		    any of the /dev file system operations. A SDEV_ZOMBIE
1689e5aa9d8SRobert Mustacchi  *		    node is immediately removed from the directory cache
1699e5aa9d8SRobert Mustacchi  *		    and destroyed once the reference count reaches zero.
1709e5aa9d8SRobert Mustacchi  *
1719e5aa9d8SRobert Mustacchi  * Historically nodes that were marked SDEV_ZOMBIE were not removed from the
1729e5aa9d8SRobert Mustacchi  * underlying directory caches. This has been the source of numerous bugs and
1739e5aa9d8SRobert Mustacchi  * thus to better mimic what happens on a real file system, it is no longer the
1749e5aa9d8SRobert Mustacchi  * case.
1759e5aa9d8SRobert Mustacchi  *
1769e5aa9d8SRobert Mustacchi  * The following state machine describes the life cycle of a given node and its
1779e5aa9d8SRobert Mustacchi  * associated states:
1789e5aa9d8SRobert Mustacchi  *
1799e5aa9d8SRobert Mustacchi  * node is . . . . .
1809e5aa9d8SRobert Mustacchi  * allocated via   .     +-------------+         . . . . . . . vnode_t refcount
1819e5aa9d8SRobert Mustacchi  * sdev_nodeinit() .     | Unallocated |         .             reaches zero and
1829e5aa9d8SRobert Mustacchi  *        +--------*-----|   Memory    |<--------*---+         sdev_inactive is
1839e5aa9d8SRobert Mustacchi  *        |              +-------------+             |         called.
1849e5aa9d8SRobert Mustacchi  *        |       +------------^                     |         called.
1859e5aa9d8SRobert Mustacchi  *        v       |                                  |
1869e5aa9d8SRobert Mustacchi  *  +-----------+ * . . sdev_nodeready()      +-------------+
1879e5aa9d8SRobert Mustacchi  *  | SDEV_INIT | |     or related setup      | SDEV_ZOMBIE |
1889e5aa9d8SRobert Mustacchi  *  +-----------+ |     failure               +-------------+
1899e5aa9d8SRobert Mustacchi  *        |       |                                  ^
1909e5aa9d8SRobert Mustacchi  *        |       |      +------------+              |
1919e5aa9d8SRobert Mustacchi  *        +-*----------->| SDEV_READY |--------*-----+
1929e5aa9d8SRobert Mustacchi  *          .            +------------+        .          The node is no longer
1939e5aa9d8SRobert Mustacchi  *          . . node successfully              . . . . .  valid or we've been
1949e5aa9d8SRobert Mustacchi  *              inserted into the                         asked to remove it.
1959e5aa9d8SRobert Mustacchi  *              directory cache                           This happens via
1969e5aa9d8SRobert Mustacchi  *              and sdev_nodready()                       sdev_dirdelete().
1979e5aa9d8SRobert Mustacchi  *              call successful.
1989e5aa9d8SRobert Mustacchi  *
1999e5aa9d8SRobert Mustacchi  * Adding and Removing Dirents, Zombie Nodes
2009e5aa9d8SRobert Mustacchi  * -----------------------------------------
2019e5aa9d8SRobert Mustacchi  *
2029e5aa9d8SRobert Mustacchi  * As part of doing a lookup, readdir, or an explicit creation operation like
2039e5aa9d8SRobert Mustacchi  * mkdir or create, nodes may be created. Every directory has an avl tree which
2049e5aa9d8SRobert Mustacchi  * contains its children, the sdev_entries tree. This is only used if the type
2059e5aa9d8SRobert Mustacchi  * is VDIR. Access to this is controlled by the sdev_node_t's contents_lock and
2069e5aa9d8SRobert Mustacchi  * it is managed through sdev_cache_update().
2079e5aa9d8SRobert Mustacchi  *
2089e5aa9d8SRobert Mustacchi  * Every sdev_node_t has a field sdev_state, which describes the current state
2099e5aa9d8SRobert Mustacchi  * of the node. A node is generally speaking in the SDEV_READY state. When it is
2109e5aa9d8SRobert Mustacchi  * there, it can be looked up, accessed, and operations performed on it. When a
2119e5aa9d8SRobert Mustacchi  * node is going to be removed from the directory cache it is marked as a
2129e5aa9d8SRobert Mustacchi  * zombie. Once a node becomes a zombie, no other file system operations will
2139e5aa9d8SRobert Mustacchi  * succeed and it will continue to exist as a node until the vnode count on the
2149e5aa9d8SRobert Mustacchi  * node reaches zero. At that point, the node will be freed.  However, once a
2159e5aa9d8SRobert Mustacchi  * node has been marked as a zombie, it will be removed immediately from the
2169e5aa9d8SRobert Mustacchi  * directory cache such that no one else may find it again.  This means that
2179e5aa9d8SRobert Mustacchi  * someone else can insert a new entry into that directory with the same name
2189e5aa9d8SRobert Mustacchi  * and without a problem.
2199e5aa9d8SRobert Mustacchi  *
2209e5aa9d8SRobert Mustacchi  * To remove a node, see the section on that in The Rules.
2219e5aa9d8SRobert Mustacchi  *
2229e5aa9d8SRobert Mustacchi  * The Rules
2239e5aa9d8SRobert Mustacchi  * ---------
2249e5aa9d8SRobert Mustacchi  * These are the rules to live by when working in sdev. These are not
2259e5aa9d8SRobert Mustacchi  * exhaustive.
2269e5aa9d8SRobert Mustacchi  *
2279e5aa9d8SRobert Mustacchi  * - Set 1: Working with Backing Nodes
2289e5aa9d8SRobert Mustacchi  *   o If there is a SDEV_READY sdev_node_t, it knows about its backing node.
2299e5aa9d8SRobert Mustacchi  *   o If we find a backing node when looking up an sdev_node_t for the first
2309e5aa9d8SRobert Mustacchi  *     time, we use its attributes to build our sdev_node_t.
2319e5aa9d8SRobert Mustacchi  *   o If there is a found backing node, or we create a backing node, that's
2329e5aa9d8SRobert Mustacchi  *     when we grab the hold on its vnode.
2339e5aa9d8SRobert Mustacchi  *   o If we mark an sdev_node_t a ZOMBIE, we must remove its backing node from
2349e5aa9d8SRobert Mustacchi  *     the underlying file system. It must not be searchable or findable.
2359e5aa9d8SRobert Mustacchi  *   o We release our hold on the backing node vnode when we destroy the
2369e5aa9d8SRobert Mustacchi  *     sdev_node_t.
2379e5aa9d8SRobert Mustacchi  *
2389e5aa9d8SRobert Mustacchi  * - Set 2: Locking rules for sdev (not exhaustive)
2399e5aa9d8SRobert Mustacchi  *   o The majority of nodes contain an sdev_contents rw lock. You must hold it
2409e5aa9d8SRobert Mustacchi  *     for read or write if manipulating its contents appropriately.
2419e5aa9d8SRobert Mustacchi  *   o You must lock your parent before yourself.
2429e5aa9d8SRobert Mustacchi  *   o If you need your vnode's v_lock and the sdev_contents rw lock, you must
2439e5aa9d8SRobert Mustacchi  *     grab the v_lock before the sdev_contents rw_lock.
2449e5aa9d8SRobert Mustacchi  *   o If you release a lock on the node as a part of upgrading it, you must
2459e5aa9d8SRobert Mustacchi  *     verify that the node has not become a zombie as a part of this process.
2469e5aa9d8SRobert Mustacchi  *
2479e5aa9d8SRobert Mustacchi  * - Set 3: Zombie Status and What it Means
2489e5aa9d8SRobert Mustacchi  *   o If you encounter a node that is a ZOMBIE, that means that it has been
2499e5aa9d8SRobert Mustacchi  *     unlinked from the backing store.
2509e5aa9d8SRobert Mustacchi  *   o If you release your contents lock and acquire it again (say as part of
2519e5aa9d8SRobert Mustacchi  *     trying to grab a write lock) you must check that the node has not become
2529e5aa9d8SRobert Mustacchi  *     a zombie.
2539e5aa9d8SRobert Mustacchi  *   o You should VERIFY that a looked up node is not a zombie. This follows
2549e5aa9d8SRobert Mustacchi  *     from the following logic. To mark something as a zombie means that it is
2559e5aa9d8SRobert Mustacchi  *     removed from the parents directory cache. To do that, you must have a
2569e5aa9d8SRobert Mustacchi  *     write lock on the parent's sdev_contents. To lookup through that
2579e5aa9d8SRobert Mustacchi  *     directory you must have a read lock. This then becomes a simple ordering
2589e5aa9d8SRobert Mustacchi  *     problem. If you've been granted the lock then the other operation cannot
2599e5aa9d8SRobert Mustacchi  *     be in progress or must have already succeeded.
2609e5aa9d8SRobert Mustacchi  *
2619e5aa9d8SRobert Mustacchi  * - Set 4: Removing Directory Entries (aka making nodes Zombies)
2629e5aa9d8SRobert Mustacchi  *   o Write lock must be held on the directory
2639e5aa9d8SRobert Mustacchi  *   o Write lock must be held on the node
2649e5aa9d8SRobert Mustacchi  *   o Remove the sdev_node_t from its parent cache
2659e5aa9d8SRobert Mustacchi  *   o Remove the corresponding backing store node, if it exists, eg. use
2669e5aa9d8SRobert Mustacchi  *     VOP_REMOVE or VOP_RMDIR.
2679e5aa9d8SRobert Mustacchi  *   o You must NOT make any change in the vnode reference count! Nodes should
2689e5aa9d8SRobert Mustacchi  *     only be cleaned up through VOP_INACTIVE callbacks.
2699e5aa9d8SRobert Mustacchi  *   o VOP_INACTIVE is the only one responsible for doing the final vn_rele of
2709e5aa9d8SRobert Mustacchi  *     the backing store vnode that was grabbed during lookup.
2719e5aa9d8SRobert Mustacchi  *
2729e5aa9d8SRobert Mustacchi  * - Set 5: What Nodes may be Persisted
2739e5aa9d8SRobert Mustacchi  *   o The root, /dev is always persisted
2749e5aa9d8SRobert Mustacchi  *   o Any node in vtab which is marked SDEV_DYNAMIC, may not be persisted
2759e5aa9d8SRobert Mustacchi  *     unless it is also marked SDEV_PERSIST
2769e5aa9d8SRobert Mustacchi  *   o Anything whose parent directory is marked SDEV_PERSIST will pass that
2779e5aa9d8SRobert Mustacchi  *     along to the child as long as it does not contradict the above rules
2789e5aa9d8SRobert Mustacchi  */
2799e5aa9d8SRobert Mustacchi 
280facf4a8dSllai #include <sys/types.h>
281facf4a8dSllai #include <sys/param.h>
282facf4a8dSllai #include <sys/t_lock.h>
283facf4a8dSllai #include <sys/systm.h>
284facf4a8dSllai #include <sys/sysmacros.h>
285facf4a8dSllai #include <sys/user.h>
286facf4a8dSllai #include <sys/time.h>
287facf4a8dSllai #include <sys/vfs.h>
288facf4a8dSllai #include <sys/vnode.h>
289aa59c4cbSrsb #include <sys/vfs_opreg.h>
290facf4a8dSllai #include <sys/file.h>
291facf4a8dSllai #include <sys/fcntl.h>
292facf4a8dSllai #include <sys/flock.h>
293facf4a8dSllai #include <sys/kmem.h>
294facf4a8dSllai #include <sys/uio.h>
295facf4a8dSllai #include <sys/errno.h>
296facf4a8dSllai #include <sys/stat.h>
297facf4a8dSllai #include <sys/cred.h>
298facf4a8dSllai #include <sys/dirent.h>
299facf4a8dSllai #include <sys/pathname.h>
300facf4a8dSllai #include <sys/cmn_err.h>
301facf4a8dSllai #include <sys/debug.h>
302facf4a8dSllai #include <sys/policy.h>
303facf4a8dSllai #include <vm/hat.h>
304facf4a8dSllai #include <vm/seg_vn.h>
305facf4a8dSllai #include <vm/seg_map.h>
306facf4a8dSllai #include <vm/seg.h>
307facf4a8dSllai #include <vm/as.h>
308facf4a8dSllai #include <vm/page.h>
309facf4a8dSllai #include <sys/proc.h>
310facf4a8dSllai #include <sys/mode.h>
311facf4a8dSllai #include <sys/sunndi.h>
312facf4a8dSllai #include <sys/ptms.h>
313facf4a8dSllai #include <fs/fs_subr.h>
314facf4a8dSllai #include <sys/fs/dv_node.h>
315facf4a8dSllai #include <sys/fs/sdev_impl.h>
316facf4a8dSllai 
317facf4a8dSllai /*ARGSUSED*/
318facf4a8dSllai static int
sdev_open(struct vnode ** vpp,int flag,struct cred * cred,caller_context_t * ct)319da6c28aaSamw sdev_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
320facf4a8dSllai {
321facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(*vpp);
322facf4a8dSllai 	struct sdev_node *ddv = dv->sdev_dotdot;
323facf4a8dSllai 	int error = 0;
324facf4a8dSllai 
325facf4a8dSllai 	if ((*vpp)->v_type == VDIR)
326facf4a8dSllai 		return (0);
327facf4a8dSllai 
328facf4a8dSllai 	if (!SDEV_IS_GLOBAL(dv))
329facf4a8dSllai 		return (ENOTSUP);
330facf4a8dSllai 
331681d9761SEric Taylor 	if ((*vpp)->v_type == VLNK)
332681d9761SEric Taylor 		return (ENOENT);
333facf4a8dSllai 	ASSERT((*vpp)->v_type == VREG);
334facf4a8dSllai 	if ((*vpp)->v_type != VREG)
335facf4a8dSllai 		return (ENOTSUP);
336facf4a8dSllai 
337facf4a8dSllai 	ASSERT(ddv);
338facf4a8dSllai 	rw_enter(&ddv->sdev_contents, RW_READER);
339facf4a8dSllai 	if (dv->sdev_attrvp == NULL) {
340facf4a8dSllai 		rw_exit(&ddv->sdev_contents);
341facf4a8dSllai 		return (ENOENT);
342facf4a8dSllai 	}
343da6c28aaSamw 	error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred, ct);
344facf4a8dSllai 	rw_exit(&ddv->sdev_contents);
345facf4a8dSllai 	return (error);
346facf4a8dSllai }
347facf4a8dSllai 
348facf4a8dSllai /*ARGSUSED1*/
349facf4a8dSllai static int
sdev_close(struct vnode * vp,int flag,int count,offset_t offset,struct cred * cred,caller_context_t * ct)350facf4a8dSllai sdev_close(struct vnode *vp, int flag, int count,
351da6c28aaSamw     offset_t offset, struct cred *cred, caller_context_t *ct)
352facf4a8dSllai {
353facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(vp);
354facf4a8dSllai 
355facf4a8dSllai 	if (vp->v_type == VDIR) {
356facf4a8dSllai 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
357facf4a8dSllai 		cleanshares(vp, ttoproc(curthread)->p_pid);
358facf4a8dSllai 		return (0);
359facf4a8dSllai 	}
360facf4a8dSllai 
361facf4a8dSllai 	if (!SDEV_IS_GLOBAL(dv))
362facf4a8dSllai 		return (ENOTSUP);
363facf4a8dSllai 
364facf4a8dSllai 	ASSERT(vp->v_type == VREG);
365facf4a8dSllai 	if (vp->v_type != VREG)
366facf4a8dSllai 		return (ENOTSUP);
367facf4a8dSllai 
368facf4a8dSllai 	ASSERT(dv->sdev_attrvp);
369da6c28aaSamw 	return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred, ct));
370facf4a8dSllai }
371facf4a8dSllai 
372facf4a8dSllai /*ARGSUSED*/
373facf4a8dSllai static int
sdev_read(struct vnode * vp,struct uio * uio,int ioflag,struct cred * cred,struct caller_context * ct)374facf4a8dSllai sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
375*f8927fa6SRobert Mustacchi     struct caller_context *ct)
376facf4a8dSllai {
377facf4a8dSllai 	struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp);
378facf4a8dSllai 	int	error;
379facf4a8dSllai 
380facf4a8dSllai 	if (!SDEV_IS_GLOBAL(dv))
381facf4a8dSllai 		return (EINVAL);
382facf4a8dSllai 
383facf4a8dSllai 	if (vp->v_type == VDIR)
384facf4a8dSllai 		return (EISDIR);
385facf4a8dSllai 
386facf4a8dSllai 	/* only supporting regular files in /dev */
387facf4a8dSllai 	ASSERT(vp->v_type == VREG);
388facf4a8dSllai 	if (vp->v_type != VREG)
389facf4a8dSllai 		return (EINVAL);
390facf4a8dSllai 
391facf4a8dSllai 	ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents));
392facf4a8dSllai 	ASSERT(dv->sdev_attrvp);
393da6c28aaSamw 	(void) VOP_RWLOCK(dv->sdev_attrvp, 0, ct);
394facf4a8dSllai 	error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct);
395da6c28aaSamw 	VOP_RWUNLOCK(dv->sdev_attrvp, 0, ct);
396facf4a8dSllai 	return (error);
397facf4a8dSllai }
398facf4a8dSllai 
399facf4a8dSllai /*ARGSUSED*/
400facf4a8dSllai static int
sdev_write(struct vnode * vp,struct uio * uio,int ioflag,struct cred * cred,struct caller_context * ct)401facf4a8dSllai sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
402*f8927fa6SRobert Mustacchi     struct caller_context *ct)
403facf4a8dSllai {
404facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(vp);
405facf4a8dSllai 	int	error = 0;
406facf4a8dSllai 
407facf4a8dSllai 	if (!SDEV_IS_GLOBAL(dv))
408facf4a8dSllai 		return (EINVAL);
409facf4a8dSllai 
410facf4a8dSllai 	if (vp->v_type == VDIR)
411facf4a8dSllai 		return (EISDIR);
412facf4a8dSllai 
413facf4a8dSllai 	/* only supporting regular files in /dev */
414facf4a8dSllai 	ASSERT(vp->v_type == VREG);
415facf4a8dSllai 	if (vp->v_type != VREG)
416facf4a8dSllai 		return (EINVAL);
417facf4a8dSllai 
418facf4a8dSllai 	ASSERT(dv->sdev_attrvp);
419facf4a8dSllai 
420da6c28aaSamw 	(void) VOP_RWLOCK(dv->sdev_attrvp, 1, ct);
421facf4a8dSllai 	error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct);
422da6c28aaSamw 	VOP_RWUNLOCK(dv->sdev_attrvp, 1, ct);
423facf4a8dSllai 	if (error == 0) {
424facf4a8dSllai 		sdev_update_timestamps(dv->sdev_attrvp, kcred,
425facf4a8dSllai 		    AT_MTIME);
426facf4a8dSllai 	}
427facf4a8dSllai 	return (error);
428facf4a8dSllai }
429facf4a8dSllai 
430facf4a8dSllai /*ARGSUSED*/
431facf4a8dSllai static int
sdev_ioctl(struct vnode * vp,int cmd,intptr_t arg,int flag,struct cred * cred,int * rvalp,caller_context_t * ct)432facf4a8dSllai sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
433da6c28aaSamw     struct cred *cred, int *rvalp,  caller_context_t *ct)
434facf4a8dSllai {
435facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(vp);
436facf4a8dSllai 
437facf4a8dSllai 	if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR))
438facf4a8dSllai 		return (ENOTTY);
439facf4a8dSllai 
440facf4a8dSllai 	ASSERT(vp->v_type == VREG);
441facf4a8dSllai 	if (vp->v_type != VREG)
442facf4a8dSllai 		return (EINVAL);
443facf4a8dSllai 
444facf4a8dSllai 	ASSERT(dv->sdev_attrvp);
445da6c28aaSamw 	return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp, ct));
446facf4a8dSllai }
447facf4a8dSllai 
448facf4a8dSllai static int
sdev_getattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cr,caller_context_t * ct)449da6c28aaSamw sdev_getattr(struct vnode *vp, struct vattr *vap, int flags,
450da6c28aaSamw     struct cred *cr, caller_context_t *ct)
451facf4a8dSllai {
452facf4a8dSllai 	int			error = 0;
453facf4a8dSllai 	struct sdev_node	*dv = VTOSDEV(vp);
454facf4a8dSllai 	struct sdev_node	*parent = dv->sdev_dotdot;
455facf4a8dSllai 
456facf4a8dSllai 	ASSERT(parent);
457facf4a8dSllai 
458facf4a8dSllai 	rw_enter(&parent->sdev_contents, RW_READER);
459facf4a8dSllai 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
460facf4a8dSllai 
461facf4a8dSllai 	/*
462facf4a8dSllai 	 * search order:
463facf4a8dSllai 	 * 	- for persistent nodes (SDEV_PERSIST): backstore
464facf4a8dSllai 	 *	- for non-persistent nodes: module ops if global, then memory
465facf4a8dSllai 	 */
466facf4a8dSllai 	if (dv->sdev_attrvp) {
467facf4a8dSllai 		rw_exit(&parent->sdev_contents);
468da6c28aaSamw 		error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr, ct);
469facf4a8dSllai 		sdev_vattr_merge(dv, vap);
470facf4a8dSllai 	} else {
471facf4a8dSllai 		ASSERT(dv->sdev_attr);
472facf4a8dSllai 		*vap = *dv->sdev_attr;
473facf4a8dSllai 		sdev_vattr_merge(dv, vap);
474facf4a8dSllai 		rw_exit(&parent->sdev_contents);
475facf4a8dSllai 	}
476facf4a8dSllai 
477facf4a8dSllai 	return (error);
478facf4a8dSllai }
479facf4a8dSllai 
480cbcfaf83Sjg /*ARGSUSED4*/
481facf4a8dSllai static int
sdev_setattr(struct vnode * vp,struct vattr * vap,int flags,struct cred * cred,caller_context_t * ctp)482cbcfaf83Sjg sdev_setattr(struct vnode *vp, struct vattr *vap, int flags,
483cbcfaf83Sjg     struct cred *cred, caller_context_t *ctp)
484facf4a8dSllai {
485facf4a8dSllai 	return (devname_setattr_func(vp, vap, flags, cred, NULL, 0));
486facf4a8dSllai }
487facf4a8dSllai 
488facf4a8dSllai static int
sdev_getsecattr(struct vnode * vp,struct vsecattr * vsap,int flags,struct cred * cr,caller_context_t * ct)489facf4a8dSllai sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
490da6c28aaSamw     struct cred *cr, caller_context_t *ct)
491facf4a8dSllai {
492facf4a8dSllai 	int	error;
493facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(vp);
494facf4a8dSllai 	struct vnode *avp = dv->sdev_attrvp;
495facf4a8dSllai 
496facf4a8dSllai 	if (avp == NULL) {
497facf4a8dSllai 		/* return fs_fab_acl() if flavor matches, else do nothing */
498facf4a8dSllai 		if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED &&
499facf4a8dSllai 		    (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) ||
500facf4a8dSllai 		    (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED &&
501facf4a8dSllai 		    (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE))))
502da6c28aaSamw 			return (fs_fab_acl(vp, vsap, flags, cr, ct));
503facf4a8dSllai 
504facf4a8dSllai 		return (ENOSYS);
505facf4a8dSllai 	}
506facf4a8dSllai 
507da6c28aaSamw 	(void) VOP_RWLOCK(avp, 1, ct);
508da6c28aaSamw 	error = VOP_GETSECATTR(avp, vsap, flags, cr, ct);
509da6c28aaSamw 	VOP_RWUNLOCK(avp, 1, ct);
510facf4a8dSllai 	return (error);
511facf4a8dSllai }
512facf4a8dSllai 
513facf4a8dSllai static int
sdev_setsecattr(struct vnode * vp,struct vsecattr * vsap,int flags,struct cred * cr,caller_context_t * ct)514facf4a8dSllai sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
515da6c28aaSamw     struct cred *cr, caller_context_t *ct)
516facf4a8dSllai {
517facf4a8dSllai 	int	error;
518facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(vp);
519facf4a8dSllai 	struct vnode *avp = dv->sdev_attrvp;
520facf4a8dSllai 
521facf4a8dSllai 	if (dv->sdev_state == SDEV_ZOMBIE)
522facf4a8dSllai 		return (0);
523facf4a8dSllai 
524facf4a8dSllai 	if (avp == NULL) {
525facf4a8dSllai 		if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv))
526facf4a8dSllai 			return (fs_nosys());
527facf4a8dSllai 		ASSERT(dv->sdev_attr);
528facf4a8dSllai 		/*
529facf4a8dSllai 		 * if coming in directly, the acl system call will
530facf4a8dSllai 		 * have held the read-write lock via VOP_RWLOCK()
531facf4a8dSllai 		 * If coming in via specfs, specfs will have
532facf4a8dSllai 		 * held the rw lock on the realvp i.e. us.
533facf4a8dSllai 		 */
534facf4a8dSllai 		ASSERT(RW_WRITE_HELD(&dv->sdev_contents));
535facf4a8dSllai 		sdev_vattr_merge(dv, dv->sdev_attr);
536da6c28aaSamw 		error = sdev_shadow_node(dv, cr);
537facf4a8dSllai 		if (error) {
538facf4a8dSllai 			return (fs_nosys());
539facf4a8dSllai 		}
540facf4a8dSllai 
541facf4a8dSllai 		ASSERT(dv->sdev_attrvp);
542facf4a8dSllai 		/* clean out the memory copy if any */
543facf4a8dSllai 		if (dv->sdev_attr) {
544facf4a8dSllai 			kmem_free(dv->sdev_attr, sizeof (struct vattr));
545facf4a8dSllai 			dv->sdev_attr = NULL;
546facf4a8dSllai 		}
547facf4a8dSllai 		avp = dv->sdev_attrvp;
548facf4a8dSllai 	}
549facf4a8dSllai 	ASSERT(avp);
550facf4a8dSllai 
551da6c28aaSamw 	(void) VOP_RWLOCK(avp, V_WRITELOCK_TRUE, ct);
552da6c28aaSamw 	error = VOP_SETSECATTR(avp, vsap, flags, cr, ct);
553da6c28aaSamw 	VOP_RWUNLOCK(avp, V_WRITELOCK_TRUE, ct);
554facf4a8dSllai 	return (error);
555facf4a8dSllai }
556facf4a8dSllai 
557de442498SRobert Mustacchi /*
558de442498SRobert Mustacchi  * There are two different unlocked routines. This one is not static as it is
559de442498SRobert Mustacchi  * used as part of the secpolicy_vnode_setattr calls in sdev_subr.c. Because it
560de442498SRobert Mustacchi  * is used in that function it has to have a specific signature.
561de442498SRobert Mustacchi  */
562facf4a8dSllai int
sdev_unlocked_access(void * vdv,int mode,struct cred * cr)563facf4a8dSllai sdev_unlocked_access(void *vdv, int mode, struct cred *cr)
564facf4a8dSllai {
565facf4a8dSllai 	struct sdev_node	*dv = vdv;
566facf4a8dSllai 	int			shift = 0;
567facf4a8dSllai 	uid_t			owner = dv->sdev_attr->va_uid;
568facf4a8dSllai 
569facf4a8dSllai 	if (crgetuid(cr) != owner) {
570facf4a8dSllai 		shift += 3;
571facf4a8dSllai 		if (groupmember(dv->sdev_attr->va_gid, cr) == 0)
572facf4a8dSllai 			shift += 3;
573facf4a8dSllai 	}
574facf4a8dSllai 
575134a1f4eSCasper H.S. Dik 	return (secpolicy_vnode_access2(cr, SDEVTOV(dv), owner,
576134a1f4eSCasper H.S. Dik 	    dv->sdev_attr->va_mode << shift, mode));
577facf4a8dSllai }
578facf4a8dSllai 
579facf4a8dSllai static int
sdev_self_access(sdev_node_t * dv,int mode,int flags,struct cred * cr,caller_context_t * ct)580de442498SRobert Mustacchi sdev_self_access(sdev_node_t *dv, int mode, int flags, struct cred *cr,
581da6c28aaSamw     caller_context_t *ct)
582facf4a8dSllai {
583de442498SRobert Mustacchi 	int ret;
584facf4a8dSllai 
585*f8927fa6SRobert Mustacchi 	ASSERT(RW_READ_HELD(&dv->sdev_contents));
586facf4a8dSllai 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
587*f8927fa6SRobert Mustacchi 
588facf4a8dSllai 	if (dv->sdev_attrvp) {
589da6c28aaSamw 		ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr, ct);
590facf4a8dSllai 	} else if (dv->sdev_attr) {
591facf4a8dSllai 		ret = sdev_unlocked_access(dv, mode, cr);
592facf4a8dSllai 		if (ret)
593facf4a8dSllai 			ret = EACCES;
594facf4a8dSllai 	}
595de442498SRobert Mustacchi 
596de442498SRobert Mustacchi 	return (ret);
597de442498SRobert Mustacchi }
598de442498SRobert Mustacchi 
599de442498SRobert Mustacchi static int
sdev_access(struct vnode * vp,int mode,int flags,struct cred * cr,caller_context_t * ct)600de442498SRobert Mustacchi sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr,
601de442498SRobert Mustacchi     caller_context_t *ct)
602de442498SRobert Mustacchi {
603de442498SRobert Mustacchi 	struct sdev_node *dv = VTOSDEV(vp);
604de442498SRobert Mustacchi 	int ret;
605de442498SRobert Mustacchi 
606de442498SRobert Mustacchi 	rw_enter(&dv->sdev_contents, RW_READER);
607de442498SRobert Mustacchi 	ret = sdev_self_access(dv, mode, flags, cr, ct);
608368fc941SRobert Mustacchi 	rw_exit(&dv->sdev_contents);
609facf4a8dSllai 
610facf4a8dSllai 	return (ret);
611facf4a8dSllai }
612facf4a8dSllai 
613facf4a8dSllai /*
614facf4a8dSllai  * Lookup
615facf4a8dSllai  */
616facf4a8dSllai /*ARGSUSED3*/
617facf4a8dSllai static int
sdev_lookup(struct vnode * dvp,char * nm,struct vnode ** vpp,struct pathname * pnp,int flags,struct vnode * rdir,struct cred * cred,caller_context_t * ct,int * direntflags,pathname_t * realpnp)618facf4a8dSllai sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
619da6c28aaSamw     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
620da6c28aaSamw     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
621facf4a8dSllai {
6226b938478Sjg 	struct sdev_node *parent;
6236b938478Sjg 	int error;
624facf4a8dSllai 
625facf4a8dSllai 	parent = VTOSDEV(dvp);
626facf4a8dSllai 	ASSERT(parent);
627facf4a8dSllai 
6286b938478Sjg 	/* execute access is required to search the directory */
629da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
6306b938478Sjg 		return (error);
6316b938478Sjg 
632facf4a8dSllai 	if (!SDEV_IS_GLOBAL(parent))
633facf4a8dSllai 		return (prof_lookup(dvp, nm, vpp, cred));
634facf4a8dSllai 	return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0));
635facf4a8dSllai }
636facf4a8dSllai 
637facf4a8dSllai /*ARGSUSED2*/
638facf4a8dSllai static int
sdev_create(struct vnode * dvp,char * nm,struct vattr * vap,vcexcl_t excl,int mode,struct vnode ** vpp,struct cred * cred,int flag,caller_context_t * ct,vsecattr_t * vsecp)639facf4a8dSllai sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
640da6c28aaSamw     int mode, struct vnode **vpp, struct cred *cred, int flag,
641da6c28aaSamw     caller_context_t *ct, vsecattr_t *vsecp)
642facf4a8dSllai {
643facf4a8dSllai 	struct vnode		*vp = NULL;
644facf4a8dSllai 	struct vnode		*avp;
645facf4a8dSllai 	struct sdev_node	*parent;
646facf4a8dSllai 	struct sdev_node	*self = NULL;
647facf4a8dSllai 	int			error = 0;
648facf4a8dSllai 	vtype_t			type = vap->va_type;
649facf4a8dSllai 
6500bfaec69Sllai 	ASSERT(type != VNON && type != VBAD);
651facf4a8dSllai 
652facf4a8dSllai 	if ((type == VFIFO) || (type == VSOCK) ||
653facf4a8dSllai 	    (type == VPROC) || (type == VPORT))
654facf4a8dSllai 		return (ENOTSUP);
655facf4a8dSllai 
656facf4a8dSllai 	parent = VTOSDEV(dvp);
657facf4a8dSllai 	ASSERT(parent);
658facf4a8dSllai 
659facf4a8dSllai 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
660facf4a8dSllai 	if (parent->sdev_state == SDEV_ZOMBIE) {
661facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
662facf4a8dSllai 		return (ENOENT);
663facf4a8dSllai 	}
664facf4a8dSllai 
665defc4c8aSRyan Zezeski 	/*
666defc4c8aSRyan Zezeski 	 * Nodes cannot be created in NGZ context.
667defc4c8aSRyan Zezeski 	 */
668facf4a8dSllai 	if (!SDEV_IS_GLOBAL(parent)) {
669facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
670defc4c8aSRyan Zezeski 		error = prof_lookup(dvp, nm, vpp, cred);
671defc4c8aSRyan Zezeski 
672defc4c8aSRyan Zezeski 		/*
673defc4c8aSRyan Zezeski 		 * In this case, we can't create a vnode but we can
674defc4c8aSRyan Zezeski 		 * open an existing one. However, we still want to
675defc4c8aSRyan Zezeski 		 * enforce the open(2) error semantics as if this was
676defc4c8aSRyan Zezeski 		 * a regular sdev_create() in GZ context. Since we
677defc4c8aSRyan Zezeski 		 * know the vnode already exists (error == 0) we a)
678defc4c8aSRyan Zezeski 		 * return EEXIST if exclusive access was requested, or
679defc4c8aSRyan Zezeski 		 * b) return EISDIR if write access was requested on a
680defc4c8aSRyan Zezeski 		 * directory. Otherwise, we return the value from
681defc4c8aSRyan Zezeski 		 * prof_lookup() as is.
682defc4c8aSRyan Zezeski 		 */
683defc4c8aSRyan Zezeski 		if (error == 0) {
684defc4c8aSRyan Zezeski 			if (excl == EXCL) {
685defc4c8aSRyan Zezeski 				error = EEXIST;
686defc4c8aSRyan Zezeski 			} else if (((*vpp)->v_type == VDIR) &&
687defc4c8aSRyan Zezeski 			    (mode & VWRITE)) {
688defc4c8aSRyan Zezeski 				error = EISDIR;
689defc4c8aSRyan Zezeski 			}
690defc4c8aSRyan Zezeski 
691defc4c8aSRyan Zezeski 			if (error != 0)
692defc4c8aSRyan Zezeski 				VN_RELE(*vpp);
693defc4c8aSRyan Zezeski 		}
694defc4c8aSRyan Zezeski 
695defc4c8aSRyan Zezeski 
696defc4c8aSRyan Zezeski 		return (error);
697facf4a8dSllai 	}
698facf4a8dSllai 	rw_exit(&parent->sdev_dotdot->sdev_contents);
699facf4a8dSllai 
7006b938478Sjg 	/* execute access is required to search the directory */
7011c4bb543SJerry Gilliam 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
7026b938478Sjg 		return (error);
7036b938478Sjg 
704facf4a8dSllai 	/* check existing name */
705da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
706da6c28aaSamw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
707facf4a8dSllai 
708facf4a8dSllai 	/* name found */
709facf4a8dSllai 	if (error == 0) {
710facf4a8dSllai 		ASSERT(vp);
711facf4a8dSllai 		if (excl == EXCL) {
712facf4a8dSllai 			error = EEXIST;
713facf4a8dSllai 		} else if ((vp->v_type == VDIR) && (mode & VWRITE)) {
714facf4a8dSllai 			/* allowing create/read-only an existing directory */
715facf4a8dSllai 			error = EISDIR;
716facf4a8dSllai 		} else {
717174e0b3dSjg 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
718facf4a8dSllai 		}
719facf4a8dSllai 
720facf4a8dSllai 		if (error) {
721facf4a8dSllai 			VN_RELE(vp);
722facf4a8dSllai 			return (error);
723facf4a8dSllai 		}
724facf4a8dSllai 
725facf4a8dSllai 		/* truncation first */
726facf4a8dSllai 		if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) &&
727facf4a8dSllai 		    (vap->va_size == 0)) {
728facf4a8dSllai 			ASSERT(parent->sdev_attrvp);
729facf4a8dSllai 			error = VOP_CREATE(parent->sdev_attrvp,
730da6c28aaSamw 			    nm, vap, excl, mode, &avp, cred, flag, ct, vsecp);
731facf4a8dSllai 
732facf4a8dSllai 			if (error) {
733facf4a8dSllai 				VN_RELE(vp);
734facf4a8dSllai 				return (error);
735facf4a8dSllai 			}
736facf4a8dSllai 		}
737facf4a8dSllai 
738facf4a8dSllai 		sdev_update_timestamps(vp, kcred,
739facf4a8dSllai 		    AT_CTIME|AT_MTIME|AT_ATIME);
740facf4a8dSllai 		*vpp = vp;
741facf4a8dSllai 		return (0);
742facf4a8dSllai 	}
743facf4a8dSllai 
744facf4a8dSllai 	/* bail out early */
745facf4a8dSllai 	if (error != ENOENT)
746facf4a8dSllai 		return (error);
747facf4a8dSllai 
7481c4bb543SJerry Gilliam 	/* verify write access - compliance specifies ENXIO */
7491c4bb543SJerry Gilliam 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0) {
7501c4bb543SJerry Gilliam 		if (error == EACCES)
7511c4bb543SJerry Gilliam 			error = ENXIO;
7521c4bb543SJerry Gilliam 		return (error);
7531c4bb543SJerry Gilliam 	}
7541c4bb543SJerry Gilliam 
755facf4a8dSllai 	/*
756facf4a8dSllai 	 * For memory-based (ROFS) directory:
757facf4a8dSllai 	 * 	- either disallow node creation;
758facf4a8dSllai 	 *	- or implement VOP_CREATE of its own
759facf4a8dSllai 	 */
760facf4a8dSllai 	rw_enter(&parent->sdev_contents, RW_WRITER);
761facf4a8dSllai 	if (!SDEV_IS_PERSIST(parent)) {
762facf4a8dSllai 		rw_exit(&parent->sdev_contents);
763facf4a8dSllai 		return (ENOTSUP);
764facf4a8dSllai 	}
765facf4a8dSllai 	ASSERT(parent->sdev_attrvp);
766facf4a8dSllai 	error = sdev_mknode(parent, nm, &self, vap, NULL, NULL,
767facf4a8dSllai 	    cred, SDEV_READY);
768facf4a8dSllai 	if (error) {
769facf4a8dSllai 		rw_exit(&parent->sdev_contents);
770facf4a8dSllai 		if (self)
771facf4a8dSllai 			SDEV_RELE(self);
772facf4a8dSllai 		return (error);
773facf4a8dSllai 	}
774facf4a8dSllai 	rw_exit(&parent->sdev_contents);
775facf4a8dSllai 
776facf4a8dSllai 	ASSERT(self);
777facf4a8dSllai 	/* take care the timestamps for the node and its parent */
778facf4a8dSllai 	sdev_update_timestamps(SDEVTOV(self), kcred,
779facf4a8dSllai 	    AT_CTIME|AT_MTIME|AT_ATIME);
780facf4a8dSllai 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
781facf4a8dSllai 	if (SDEV_IS_GLOBAL(parent))
782facf4a8dSllai 		atomic_inc_ulong(&parent->sdev_gdir_gen);
783facf4a8dSllai 
784facf4a8dSllai 	/* wake up other threads blocked on looking up this node */
785facf4a8dSllai 	mutex_enter(&self->sdev_lookup_lock);
786facf4a8dSllai 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
787facf4a8dSllai 	mutex_exit(&self->sdev_lookup_lock);
788facf4a8dSllai 	error = sdev_to_vp(self, vpp);
789facf4a8dSllai 	return (error);
790facf4a8dSllai }
791facf4a8dSllai 
792facf4a8dSllai static int
sdev_remove(struct vnode * dvp,char * nm,struct cred * cred,caller_context_t * ct,int flags)793da6c28aaSamw sdev_remove(struct vnode *dvp, char *nm, struct cred *cred,
794da6c28aaSamw     caller_context_t *ct, int flags)
795facf4a8dSllai {
796facf4a8dSllai 	int	error;
797facf4a8dSllai 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
798facf4a8dSllai 	struct vnode *vp = NULL;
799facf4a8dSllai 	struct sdev_node *dv = NULL;
800facf4a8dSllai 	int len;
8016c6a4562SJerry Gilliam 	int bkstore;
802facf4a8dSllai 
803facf4a8dSllai 	/* bail out early */
804facf4a8dSllai 	len = strlen(nm);
805facf4a8dSllai 	if (nm[0] == '.') {
806facf4a8dSllai 		if (len == 1) {
807facf4a8dSllai 			return (EINVAL);
808facf4a8dSllai 		} else if (len == 2 && nm[1] == '.') {
809facf4a8dSllai 			return (EEXIST);
810facf4a8dSllai 		}
811facf4a8dSllai 	}
812facf4a8dSllai 
813facf4a8dSllai 	ASSERT(parent);
814facf4a8dSllai 	rw_enter(&parent->sdev_contents, RW_READER);
815facf4a8dSllai 	if (!SDEV_IS_GLOBAL(parent)) {
816facf4a8dSllai 		rw_exit(&parent->sdev_contents);
817facf4a8dSllai 		return (ENOTSUP);
818facf4a8dSllai 	}
819facf4a8dSllai 
8206b938478Sjg 	/* execute access is required to search the directory */
821de442498SRobert Mustacchi 	if ((error = sdev_self_access(parent, VEXEC, 0, cred, ct)) != 0) {
8226b938478Sjg 		rw_exit(&parent->sdev_contents);
8236b938478Sjg 		return (error);
8246b938478Sjg 	}
8256b938478Sjg 
826facf4a8dSllai 	/* check existence first */
827facf4a8dSllai 	dv = sdev_cache_lookup(parent, nm);
828facf4a8dSllai 	if (dv == NULL) {
829facf4a8dSllai 		rw_exit(&parent->sdev_contents);
830facf4a8dSllai 		return (ENOENT);
831facf4a8dSllai 	}
832facf4a8dSllai 
833facf4a8dSllai 	vp = SDEVTOV(dv);
834facf4a8dSllai 	if ((dv->sdev_state == SDEV_INIT) ||
835facf4a8dSllai 	    (dv->sdev_state == SDEV_ZOMBIE)) {
836facf4a8dSllai 		rw_exit(&parent->sdev_contents);
837facf4a8dSllai 		VN_RELE(vp);
838facf4a8dSllai 		return (ENOENT);
839facf4a8dSllai 	}
840facf4a8dSllai 
8416b938478Sjg 	/* write access is required to remove an entry */
842de442498SRobert Mustacchi 	if ((error = sdev_self_access(parent, VWRITE, 0, cred, ct)) != 0) {
8436b938478Sjg 		rw_exit(&parent->sdev_contents);
8446b938478Sjg 		VN_RELE(vp);
8456b938478Sjg 		return (error);
8466b938478Sjg 	}
8476b938478Sjg 
8486c6a4562SJerry Gilliam 	bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0;
8496c6a4562SJerry Gilliam 	if (!rw_tryupgrade(&parent->sdev_contents)) {
8506c6a4562SJerry Gilliam 		rw_exit(&parent->sdev_contents);
8516c6a4562SJerry Gilliam 		rw_enter(&parent->sdev_contents, RW_WRITER);
8529e5aa9d8SRobert Mustacchi 		/* Make sure we didn't become a zombie */
8539e5aa9d8SRobert Mustacchi 		if (parent->sdev_state == SDEV_ZOMBIE) {
8549e5aa9d8SRobert Mustacchi 			rw_exit(&parent->sdev_contents);
8559e5aa9d8SRobert Mustacchi 			VN_RELE(vp);
8569e5aa9d8SRobert Mustacchi 			return (ENOENT);
8579e5aa9d8SRobert Mustacchi 		}
8586c6a4562SJerry Gilliam 	}
8596c6a4562SJerry Gilliam 
8606c6a4562SJerry Gilliam 	/* we do not support unlinking a non-empty directory */
8616c6a4562SJerry Gilliam 	if (vp->v_type == VDIR && dv->sdev_nlink > 2) {
8626c6a4562SJerry Gilliam 		rw_exit(&parent->sdev_contents);
8636c6a4562SJerry Gilliam 		VN_RELE(vp);
8646c6a4562SJerry Gilliam 		return (EBUSY);
8656c6a4562SJerry Gilliam 	}
8666c6a4562SJerry Gilliam 
867facf4a8dSllai 	/*
868facf4a8dSllai 	 * sdev_dirdelete does the real job of:
869facf4a8dSllai 	 *  - make sure no open ref count
870facf4a8dSllai 	 *  - destroying the sdev_node
871facf4a8dSllai 	 *  - releasing the hold on attrvp
872facf4a8dSllai 	 */
8739e5aa9d8SRobert Mustacchi 	sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE);
8749e5aa9d8SRobert Mustacchi 	VN_RELE(vp);
875facf4a8dSllai 	rw_exit(&parent->sdev_contents);
876facf4a8dSllai 
8779e5aa9d8SRobert Mustacchi 	/*
8789e5aa9d8SRobert Mustacchi 	 * best efforts clean up the backing store
8799e5aa9d8SRobert Mustacchi 	 */
8809e5aa9d8SRobert Mustacchi 	if (bkstore) {
8819e5aa9d8SRobert Mustacchi 		ASSERT(parent->sdev_attrvp);
8829e5aa9d8SRobert Mustacchi 		error = VOP_REMOVE(parent->sdev_attrvp, nm, cred,
8839e5aa9d8SRobert Mustacchi 		    ct, flags);
884facf4a8dSllai 		/*
8859e5aa9d8SRobert Mustacchi 		 * do not report BUSY error
8869e5aa9d8SRobert Mustacchi 		 * because the backing store ref count is released
8879e5aa9d8SRobert Mustacchi 		 * when the last ref count on the sdev_node is
8889e5aa9d8SRobert Mustacchi 		 * released.
889facf4a8dSllai 		 */
8909e5aa9d8SRobert Mustacchi 		if (error == EBUSY) {
8919e5aa9d8SRobert Mustacchi 			sdcmn_err2(("sdev_remove: device %s is still on"
8929e5aa9d8SRobert Mustacchi 			    "disk %s\n", nm, parent->sdev_path));
893facf4a8dSllai 			error = 0;
8949e5aa9d8SRobert Mustacchi 		}
895facf4a8dSllai 	}
896facf4a8dSllai 
897facf4a8dSllai 	return (error);
898facf4a8dSllai }
899facf4a8dSllai 
900facf4a8dSllai /*
901facf4a8dSllai  * Some restrictions for this file system:
902facf4a8dSllai  *  - both oldnm and newnm are in the scope of /dev file system,
903facf4a8dSllai  *    to simply the namespace management model.
904facf4a8dSllai  */
905da6c28aaSamw /*ARGSUSED6*/
906facf4a8dSllai static int
sdev_rename(struct vnode * odvp,char * onm,struct vnode * ndvp,char * nnm,struct cred * cred,caller_context_t * ct,int flags)907facf4a8dSllai sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm,
908da6c28aaSamw     struct cred *cred, caller_context_t *ct, int flags)
909facf4a8dSllai {
910facf4a8dSllai 	struct sdev_node	*fromparent = NULL;
911facf4a8dSllai 	struct vattr		vattr;
912facf4a8dSllai 	struct sdev_node	*toparent;
913facf4a8dSllai 	struct sdev_node	*fromdv = NULL;	/* source node */
9140bfaec69Sllai 	struct vnode 		*ovp = NULL;	/* source vnode */
915facf4a8dSllai 	struct sdev_node	*todv = NULL;	/* destination node */
9160bfaec69Sllai 	struct vnode 		*nvp = NULL;	/* destination vnode */
917facf4a8dSllai 	int			samedir = 0;	/* set if odvp == ndvp */
918facf4a8dSllai 	struct vnode		*realvp;
919facf4a8dSllai 	int error = 0;
920facf4a8dSllai 	dev_t fsid;
921facf4a8dSllai 	int bkstore = 0;
9220bfaec69Sllai 	vtype_t type;
923facf4a8dSllai 
924facf4a8dSllai 	/* prevent modifying "." and ".." */
925facf4a8dSllai 	if ((onm[0] == '.' &&
9260bfaec69Sllai 	    (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
9270bfaec69Sllai 	    (nnm[0] == '.' &&
9280bfaec69Sllai 	    (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) {
929facf4a8dSllai 		return (EINVAL);
930facf4a8dSllai 	}
931facf4a8dSllai 
932facf4a8dSllai 	fromparent = VTOSDEV(odvp);
933facf4a8dSllai 	toparent = VTOSDEV(ndvp);
934facf4a8dSllai 
935facf4a8dSllai 	/* ZOMBIE parent doesn't allow new node creation */
936facf4a8dSllai 	rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER);
937facf4a8dSllai 	if (fromparent->sdev_state == SDEV_ZOMBIE) {
938facf4a8dSllai 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
939facf4a8dSllai 		return (ENOENT);
940facf4a8dSllai 	}
941facf4a8dSllai 
942facf4a8dSllai 	/* renaming only supported for global device nodes */
943facf4a8dSllai 	if (!SDEV_IS_GLOBAL(fromparent)) {
944facf4a8dSllai 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
945facf4a8dSllai 		return (ENOTSUP);
946facf4a8dSllai 	}
947facf4a8dSllai 	rw_exit(&fromparent->sdev_dotdot->sdev_contents);
948facf4a8dSllai 
949facf4a8dSllai 	rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER);
950facf4a8dSllai 	if (toparent->sdev_state == SDEV_ZOMBIE) {
951facf4a8dSllai 		rw_exit(&toparent->sdev_dotdot->sdev_contents);
952facf4a8dSllai 		return (ENOENT);
953facf4a8dSllai 	}
954facf4a8dSllai 	rw_exit(&toparent->sdev_dotdot->sdev_contents);
955facf4a8dSllai 
9560bfaec69Sllai 	/*
9576b938478Sjg 	 * acquire the global lock to prevent
9580bfaec69Sllai 	 * mount/unmount/other rename activities.
9590bfaec69Sllai 	 */
9600bfaec69Sllai 	mutex_enter(&sdev_lock);
9610bfaec69Sllai 
962facf4a8dSllai 	/* check existence of the source node */
963da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
964da6c28aaSamw 	error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred, ct,
965da6c28aaSamw 	    NULL, NULL);
966facf4a8dSllai 	if (error) {
967facf4a8dSllai 		sdcmn_err2(("sdev_rename: the source node %s exists\n",
968facf4a8dSllai 		    onm));
9690bfaec69Sllai 		mutex_exit(&sdev_lock);
970facf4a8dSllai 		return (error);
971facf4a8dSllai 	}
972facf4a8dSllai 
973da6c28aaSamw 	if (VOP_REALVP(ovp, &realvp, ct) == 0) {
974facf4a8dSllai 		VN_HOLD(realvp);
975facf4a8dSllai 		VN_RELE(ovp);
976facf4a8dSllai 		ovp = realvp;
977facf4a8dSllai 	}
978facf4a8dSllai 
979facf4a8dSllai 	/* check existence of destination */
980da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
981da6c28aaSamw 	error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred, ct,
982da6c28aaSamw 	    NULL, NULL);
983facf4a8dSllai 	if (error && (error != ENOENT)) {
9840bfaec69Sllai 		mutex_exit(&sdev_lock);
985facf4a8dSllai 		VN_RELE(ovp);
986facf4a8dSllai 		return (error);
987facf4a8dSllai 	}
988facf4a8dSllai 
989da6c28aaSamw 	if (nvp && (VOP_REALVP(nvp, &realvp, ct) == 0)) {
990facf4a8dSllai 		VN_HOLD(realvp);
991facf4a8dSllai 		VN_RELE(nvp);
992facf4a8dSllai 		nvp = realvp;
993facf4a8dSllai 	}
994facf4a8dSllai 
995facf4a8dSllai 	/*
9960bfaec69Sllai 	 * make sure the source and the destination are
9970bfaec69Sllai 	 * in the same dev filesystem
998facf4a8dSllai 	 */
999facf4a8dSllai 	if (odvp != ndvp) {
1000facf4a8dSllai 		vattr.va_mask = AT_FSID;
1001da6c28aaSamw 		if (error = VOP_GETATTR(odvp, &vattr, 0, cred, ct)) {
10020bfaec69Sllai 			mutex_exit(&sdev_lock);
1003facf4a8dSllai 			VN_RELE(ovp);
10049e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
10059e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
1006facf4a8dSllai 			return (error);
1007facf4a8dSllai 		}
1008facf4a8dSllai 		fsid = vattr.va_fsid;
1009facf4a8dSllai 		vattr.va_mask = AT_FSID;
1010da6c28aaSamw 		if (error = VOP_GETATTR(ndvp, &vattr, 0, cred, ct)) {
10110bfaec69Sllai 			mutex_exit(&sdev_lock);
1012facf4a8dSllai 			VN_RELE(ovp);
10139e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
10149e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
1015facf4a8dSllai 			return (error);
1016facf4a8dSllai 		}
1017facf4a8dSllai 		if (fsid != vattr.va_fsid) {
10180bfaec69Sllai 			mutex_exit(&sdev_lock);
1019facf4a8dSllai 			VN_RELE(ovp);
10209e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
10219e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
1022facf4a8dSllai 			return (EXDEV);
1023facf4a8dSllai 		}
1024facf4a8dSllai 	}
1025facf4a8dSllai 
1026facf4a8dSllai 	/* make sure the old entry can be deleted */
1027da6c28aaSamw 	error = VOP_ACCESS(odvp, VWRITE, 0, cred, ct);
1028facf4a8dSllai 	if (error) {
10290bfaec69Sllai 		mutex_exit(&sdev_lock);
1030facf4a8dSllai 		VN_RELE(ovp);
10319e5aa9d8SRobert Mustacchi 		if (nvp != NULL)
10329e5aa9d8SRobert Mustacchi 			VN_RELE(nvp);
1033facf4a8dSllai 		return (error);
1034facf4a8dSllai 	}
1035facf4a8dSllai 
1036facf4a8dSllai 	/* make sure the destination allows creation */
1037facf4a8dSllai 	samedir = (fromparent == toparent);
1038facf4a8dSllai 	if (!samedir) {
1039da6c28aaSamw 		error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred, ct);
1040facf4a8dSllai 		if (error) {
10410bfaec69Sllai 			mutex_exit(&sdev_lock);
1042facf4a8dSllai 			VN_RELE(ovp);
10439e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
10449e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
1045facf4a8dSllai 			return (error);
1046facf4a8dSllai 		}
1047facf4a8dSllai 	}
1048facf4a8dSllai 
1049facf4a8dSllai 	fromdv = VTOSDEV(ovp);
1050facf4a8dSllai 	ASSERT(fromdv);
1051facf4a8dSllai 
10520bfaec69Sllai 	/* destination file exists */
10539e5aa9d8SRobert Mustacchi 	if (nvp != NULL) {
10540bfaec69Sllai 		todv = VTOSDEV(nvp);
10550bfaec69Sllai 		ASSERT(todv);
1056facf4a8dSllai 	}
1057facf4a8dSllai 
10589e5aa9d8SRobert Mustacchi 	if ((fromdv->sdev_flags & SDEV_DYNAMIC) != 0 ||
10599e5aa9d8SRobert Mustacchi 	    (todv != NULL && (todv->sdev_flags & SDEV_DYNAMIC) != 0)) {
10609e5aa9d8SRobert Mustacchi 		mutex_exit(&sdev_lock);
10619e5aa9d8SRobert Mustacchi 		if (nvp != NULL)
10629e5aa9d8SRobert Mustacchi 			VN_RELE(nvp);
10639e5aa9d8SRobert Mustacchi 		VN_RELE(ovp);
10649e5aa9d8SRobert Mustacchi 		return (EACCES);
10659e5aa9d8SRobert Mustacchi 	}
10669e5aa9d8SRobert Mustacchi 
1067facf4a8dSllai 	/*
10689e5aa9d8SRobert Mustacchi 	 * link source to new target in the memory. Regardless of failure, we
10699e5aa9d8SRobert Mustacchi 	 * must rele our hold on nvp.
1070facf4a8dSllai 	 */
10710bfaec69Sllai 	error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred);
10729e5aa9d8SRobert Mustacchi 	if (nvp != NULL)
10739e5aa9d8SRobert Mustacchi 		VN_RELE(nvp);
10740bfaec69Sllai 	if (error) {
10750bfaec69Sllai 		sdcmn_err2(("sdev_rename: renaming %s to %s failed "
10760bfaec69Sllai 		    " with error %d\n", onm, nnm, error));
10770bfaec69Sllai 		mutex_exit(&sdev_lock);
1078facf4a8dSllai 		VN_RELE(ovp);
1079facf4a8dSllai 		return (error);
10800bfaec69Sllai 	}
1081facf4a8dSllai 
10820bfaec69Sllai 	/*
10830bfaec69Sllai 	 * unlink from source
10840bfaec69Sllai 	 */
10850bfaec69Sllai 	rw_enter(&fromparent->sdev_contents, RW_READER);
10860bfaec69Sllai 	fromdv = sdev_cache_lookup(fromparent, onm);
10870bfaec69Sllai 	if (fromdv == NULL) {
10880bfaec69Sllai 		rw_exit(&fromparent->sdev_contents);
10890bfaec69Sllai 		mutex_exit(&sdev_lock);
10909e5aa9d8SRobert Mustacchi 		VN_RELE(ovp);
10910bfaec69Sllai 		sdcmn_err2(("sdev_rename: the source is deleted already\n"));
10920bfaec69Sllai 		return (0);
10930bfaec69Sllai 	}
1094facf4a8dSllai 
10950bfaec69Sllai 	if (fromdv->sdev_state == SDEV_ZOMBIE) {
10960bfaec69Sllai 		rw_exit(&fromparent->sdev_contents);
10970bfaec69Sllai 		mutex_exit(&sdev_lock);
10980bfaec69Sllai 		VN_RELE(SDEVTOV(fromdv));
10999e5aa9d8SRobert Mustacchi 		VN_RELE(ovp);
11000bfaec69Sllai 		sdcmn_err2(("sdev_rename: the source is being deleted\n"));
11010bfaec69Sllai 		return (0);
11020bfaec69Sllai 	}
11030bfaec69Sllai 	rw_exit(&fromparent->sdev_contents);
11040bfaec69Sllai 	ASSERT(SDEVTOV(fromdv) == ovp);
11050bfaec69Sllai 	VN_RELE(ovp);
11060bfaec69Sllai 
11070bfaec69Sllai 	/* clean out the directory contents before it can be removed */
11080bfaec69Sllai 	type = SDEVTOV(fromdv)->v_type;
11090bfaec69Sllai 	if (type == VDIR) {
11100bfaec69Sllai 		error = sdev_cleandir(fromdv, NULL, 0);
11110bfaec69Sllai 		sdcmn_err2(("sdev_rename: cleandir finished with %d\n",
11120bfaec69Sllai 		    error));
11130bfaec69Sllai 		if (error == EBUSY)
11140bfaec69Sllai 			error = 0;
11150bfaec69Sllai 	}
1116facf4a8dSllai 
11170bfaec69Sllai 	rw_enter(&fromparent->sdev_contents, RW_WRITER);
11180bfaec69Sllai 	bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0;
11199e5aa9d8SRobert Mustacchi 	sdev_cache_update(fromparent, &fromdv, onm,
11200bfaec69Sllai 	    SDEV_CACHE_DELETE);
11219e5aa9d8SRobert Mustacchi 	VN_RELE(SDEVTOV(fromdv));
11220bfaec69Sllai 
11230bfaec69Sllai 	/* best effforts clean up the backing store */
11240bfaec69Sllai 	if (bkstore) {
11250bfaec69Sllai 		ASSERT(fromparent->sdev_attrvp);
11260bfaec69Sllai 		if (type != VDIR) {
1127da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_REMOVE */
11280bfaec69Sllai 			error = VOP_REMOVE(fromparent->sdev_attrvp,
1129da6c28aaSamw 			    onm, kcred, ct, 0);
11300bfaec69Sllai 		} else {
1131da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_RMDIR */
11320bfaec69Sllai 			error = VOP_RMDIR(fromparent->sdev_attrvp,
1133da6c28aaSamw 			    onm, fromparent->sdev_attrvp, kcred, ct, 0);
1134facf4a8dSllai 		}
1135facf4a8dSllai 
11360bfaec69Sllai 		if (error) {
11370bfaec69Sllai 			sdcmn_err2(("sdev_rename: device %s is "
11380bfaec69Sllai 			    "still on disk %s\n", onm,
11390bfaec69Sllai 			    fromparent->sdev_path));
11400bfaec69Sllai 			error = 0;
11410bfaec69Sllai 		}
1142facf4a8dSllai 	}
11430bfaec69Sllai 	rw_exit(&fromparent->sdev_contents);
11440bfaec69Sllai 	mutex_exit(&sdev_lock);
1145facf4a8dSllai 
11460bfaec69Sllai 	/* once reached to this point, the rename is regarded successful */
11470bfaec69Sllai 	return (0);
1148facf4a8dSllai }
1149facf4a8dSllai 
1150facf4a8dSllai /*
1151facf4a8dSllai  * dev-fs version of "ln -s path dev-name"
1152facf4a8dSllai  *	tnm - path, e.g. /devices/... or /dev/...
1153facf4a8dSllai  *	lnm - dev_name
1154facf4a8dSllai  */
1155da6c28aaSamw /*ARGSUSED6*/
1156facf4a8dSllai static int
sdev_symlink(struct vnode * dvp,char * lnm,struct vattr * tva,char * tnm,struct cred * cred,caller_context_t * ct,int flags)1157facf4a8dSllai sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva,
1158da6c28aaSamw     char *tnm, struct cred *cred, caller_context_t *ct, int flags)
1159facf4a8dSllai {
1160facf4a8dSllai 	int error;
1161facf4a8dSllai 	struct vnode *vp = NULL;
1162facf4a8dSllai 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1163facf4a8dSllai 	struct sdev_node *self = (struct sdev_node *)NULL;
1164facf4a8dSllai 
1165facf4a8dSllai 	ASSERT(parent);
1166facf4a8dSllai 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1167facf4a8dSllai 	if (parent->sdev_state == SDEV_ZOMBIE) {
1168facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1169facf4a8dSllai 		sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n",
1170facf4a8dSllai 		    parent->sdev_name));
1171facf4a8dSllai 		return (ENOENT);
1172facf4a8dSllai 	}
1173facf4a8dSllai 
1174facf4a8dSllai 	if (!SDEV_IS_GLOBAL(parent)) {
1175facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1176facf4a8dSllai 		return (ENOTSUP);
1177facf4a8dSllai 	}
1178facf4a8dSllai 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1179facf4a8dSllai 
11806b938478Sjg 	/* execute access is required to search a directory */
1181da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
11826b938478Sjg 		return (error);
11836b938478Sjg 
1184facf4a8dSllai 	/* find existing name */
1185da6c28aaSamw /* XXXci - We may need to translate the C-I flags here */
1186da6c28aaSamw 	error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
1187facf4a8dSllai 	if (error == 0) {
1188facf4a8dSllai 		ASSERT(vp);
1189facf4a8dSllai 		VN_RELE(vp);
1190facf4a8dSllai 		sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm));
1191facf4a8dSllai 		return (EEXIST);
1192facf4a8dSllai 	}
11936b938478Sjg 	if (error != ENOENT)
11946b938478Sjg 		return (error);
1195facf4a8dSllai 
11966b938478Sjg 	/* write access is required to create a symlink */
1197da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0)
1198facf4a8dSllai 		return (error);
1199facf4a8dSllai 
1200facf4a8dSllai 	/* put it into memory cache */
1201facf4a8dSllai 	rw_enter(&parent->sdev_contents, RW_WRITER);
1202facf4a8dSllai 	error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm,
1203facf4a8dSllai 	    cred, SDEV_READY);
1204facf4a8dSllai 	if (error) {
1205facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1206facf4a8dSllai 		sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm));
1207facf4a8dSllai 		if (self)
1208facf4a8dSllai 			SDEV_RELE(self);
1209facf4a8dSllai 
1210facf4a8dSllai 		return (error);
1211facf4a8dSllai 	}
1212facf4a8dSllai 	ASSERT(self && (self->sdev_state == SDEV_READY));
1213facf4a8dSllai 	rw_exit(&parent->sdev_contents);
1214facf4a8dSllai 
1215facf4a8dSllai 	/* take care the timestamps for the node and its parent */
1216facf4a8dSllai 	sdev_update_timestamps(SDEVTOV(self), kcred,
1217facf4a8dSllai 	    AT_CTIME|AT_MTIME|AT_ATIME);
1218facf4a8dSllai 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
1219facf4a8dSllai 	if (SDEV_IS_GLOBAL(parent))
1220facf4a8dSllai 		atomic_inc_ulong(&parent->sdev_gdir_gen);
1221facf4a8dSllai 
1222facf4a8dSllai 	/* wake up other threads blocked on looking up this node */
1223facf4a8dSllai 	mutex_enter(&self->sdev_lookup_lock);
1224facf4a8dSllai 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
1225facf4a8dSllai 	mutex_exit(&self->sdev_lookup_lock);
1226facf4a8dSllai 	SDEV_RELE(self);	/* don't return with vnode held */
1227facf4a8dSllai 	return (0);
1228facf4a8dSllai }
1229facf4a8dSllai 
1230da6c28aaSamw /*ARGSUSED6*/
1231facf4a8dSllai static int
sdev_mkdir(struct vnode * dvp,char * nm,struct vattr * va,struct vnode ** vpp,struct cred * cred,caller_context_t * ct,int flags,vsecattr_t * vsecp)1232facf4a8dSllai sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp,
1233da6c28aaSamw     struct cred *cred, caller_context_t *ct, int flags, vsecattr_t *vsecp)
1234facf4a8dSllai {
1235facf4a8dSllai 	int error;
1236facf4a8dSllai 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1237facf4a8dSllai 	struct sdev_node *self = NULL;
1238facf4a8dSllai 	struct vnode	*vp = NULL;
1239facf4a8dSllai 
1240facf4a8dSllai 	ASSERT(parent && parent->sdev_dotdot);
1241facf4a8dSllai 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1242facf4a8dSllai 	if (parent->sdev_state == SDEV_ZOMBIE) {
1243facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1244facf4a8dSllai 		return (ENOENT);
1245facf4a8dSllai 	}
1246facf4a8dSllai 
1247facf4a8dSllai 	/* non-global do not allow pure directory creation */
1248facf4a8dSllai 	if (!SDEV_IS_GLOBAL(parent)) {
1249facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1250facf4a8dSllai 		return (prof_lookup(dvp, nm, vpp, cred));
1251facf4a8dSllai 	}
1252facf4a8dSllai 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1253facf4a8dSllai 
12546b938478Sjg 	/* execute access is required to search the directory */
1255da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) {
12566b938478Sjg 		return (error);
12576b938478Sjg 	}
12586b938478Sjg 
1259facf4a8dSllai 	/* find existing name */
1260da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
1261da6c28aaSamw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
1262facf4a8dSllai 	if (error == 0) {
1263facf4a8dSllai 		VN_RELE(vp);
1264facf4a8dSllai 		return (EEXIST);
1265facf4a8dSllai 	}
1266facf4a8dSllai 	if (error != ENOENT)
1267facf4a8dSllai 		return (error);
1268facf4a8dSllai 
12696b938478Sjg 	/* require write access to create a directory */
1270da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) {
12716b938478Sjg 		return (error);
12726b938478Sjg 	}
12736b938478Sjg 
1274facf4a8dSllai 	/* put it into memory */
1275facf4a8dSllai 	rw_enter(&parent->sdev_contents, RW_WRITER);
1276facf4a8dSllai 	error = sdev_mknode(parent, nm, &self,
1277facf4a8dSllai 	    va, NULL, NULL, cred, SDEV_READY);
1278facf4a8dSllai 	if (error) {
1279facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1280facf4a8dSllai 		if (self)
1281facf4a8dSllai 			SDEV_RELE(self);
1282facf4a8dSllai 		return (error);
1283facf4a8dSllai 	}
1284facf4a8dSllai 	ASSERT(self && (self->sdev_state == SDEV_READY));
1285facf4a8dSllai 	rw_exit(&parent->sdev_contents);
1286facf4a8dSllai 
1287facf4a8dSllai 	/* take care the timestamps for the node and its parent */
1288facf4a8dSllai 	sdev_update_timestamps(SDEVTOV(self), kcred,
1289facf4a8dSllai 	    AT_CTIME|AT_MTIME|AT_ATIME);
1290facf4a8dSllai 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
1291facf4a8dSllai 	if (SDEV_IS_GLOBAL(parent))
1292facf4a8dSllai 		atomic_inc_ulong(&parent->sdev_gdir_gen);
1293facf4a8dSllai 
1294facf4a8dSllai 	/* wake up other threads blocked on looking up this node */
1295facf4a8dSllai 	mutex_enter(&self->sdev_lookup_lock);
1296facf4a8dSllai 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
1297facf4a8dSllai 	mutex_exit(&self->sdev_lookup_lock);
1298facf4a8dSllai 	*vpp = SDEVTOV(self);
1299facf4a8dSllai 	return (0);
1300facf4a8dSllai }
1301facf4a8dSllai 
1302facf4a8dSllai /*
1303facf4a8dSllai  * allowing removing an empty directory under /dev
1304facf4a8dSllai  */
1305facf4a8dSllai /*ARGSUSED*/
1306facf4a8dSllai static int
sdev_rmdir(struct vnode * dvp,char * nm,struct vnode * cdir,struct cred * cred,caller_context_t * ct,int flags)1307da6c28aaSamw sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred,
1308da6c28aaSamw     caller_context_t *ct, int flags)
1309facf4a8dSllai {
1310facf4a8dSllai 	int error = 0;
1311facf4a8dSllai 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1312facf4a8dSllai 	struct sdev_node *self = NULL;
1313facf4a8dSllai 	struct vnode *vp = NULL;
1314facf4a8dSllai 
1315facf4a8dSllai 	/* bail out early */
1316facf4a8dSllai 	if (strcmp(nm, ".") == 0)
1317facf4a8dSllai 		return (EINVAL);
1318facf4a8dSllai 	if (strcmp(nm, "..") == 0)
1319facf4a8dSllai 		return (EEXIST); /* should be ENOTEMPTY */
1320facf4a8dSllai 
1321facf4a8dSllai 	/* no destruction of non-global node */
1322facf4a8dSllai 	ASSERT(parent && parent->sdev_dotdot);
1323facf4a8dSllai 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1324facf4a8dSllai 	if (!SDEV_IS_GLOBAL(parent)) {
1325facf4a8dSllai 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1326facf4a8dSllai 		return (ENOTSUP);
1327facf4a8dSllai 	}
1328facf4a8dSllai 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1329facf4a8dSllai 
13306b938478Sjg 	/* execute access is required to search the directory */
1331681d9761SEric Taylor 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0)
13326b938478Sjg 		return (error);
13336b938478Sjg 
1334facf4a8dSllai 	/* check existing name */
1335facf4a8dSllai 	rw_enter(&parent->sdev_contents, RW_WRITER);
1336facf4a8dSllai 	self = sdev_cache_lookup(parent, nm);
1337facf4a8dSllai 	if (self == NULL) {
1338facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1339facf4a8dSllai 		return (ENOENT);
1340facf4a8dSllai 	}
1341facf4a8dSllai 
1342facf4a8dSllai 	vp = SDEVTOV(self);
1343facf4a8dSllai 	if ((self->sdev_state == SDEV_INIT) ||
1344facf4a8dSllai 	    (self->sdev_state == SDEV_ZOMBIE)) {
1345facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1346facf4a8dSllai 		VN_RELE(vp);
1347facf4a8dSllai 		return (ENOENT);
1348facf4a8dSllai 	}
1349facf4a8dSllai 
1350facf4a8dSllai 	/* some sanity checks */
1351facf4a8dSllai 	if (vp == dvp || vp == cdir) {
1352facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1353facf4a8dSllai 		VN_RELE(vp);
1354facf4a8dSllai 		return (EINVAL);
1355facf4a8dSllai 	}
1356facf4a8dSllai 
1357facf4a8dSllai 	if (vp->v_type != VDIR) {
1358facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1359facf4a8dSllai 		VN_RELE(vp);
1360facf4a8dSllai 		return (ENOTDIR);
1361facf4a8dSllai 	}
1362facf4a8dSllai 
1363facf4a8dSllai 	if (vn_vfswlock(vp)) {
1364facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1365facf4a8dSllai 		VN_RELE(vp);
1366facf4a8dSllai 		return (EBUSY);
1367facf4a8dSllai 	}
1368facf4a8dSllai 
1369facf4a8dSllai 	if (vn_mountedvfs(vp) != NULL) {
1370facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1371facf4a8dSllai 		vn_vfsunlock(vp);
1372facf4a8dSllai 		VN_RELE(vp);
1373facf4a8dSllai 		return (EBUSY);
1374facf4a8dSllai 	}
1375facf4a8dSllai 
1376facf4a8dSllai 	self = VTOSDEV(vp);
1377facf4a8dSllai 	/* bail out on a non-empty directory */
1378facf4a8dSllai 	rw_enter(&self->sdev_contents, RW_READER);
1379facf4a8dSllai 	if (self->sdev_nlink > 2) {
1380facf4a8dSllai 		rw_exit(&self->sdev_contents);
1381facf4a8dSllai 		rw_exit(&parent->sdev_contents);
1382facf4a8dSllai 		vn_vfsunlock(vp);
1383facf4a8dSllai 		VN_RELE(vp);
1384facf4a8dSllai 		return (ENOTEMPTY);
1385facf4a8dSllai 	}
1386facf4a8dSllai 	rw_exit(&self->sdev_contents);
1387facf4a8dSllai 
1388facf4a8dSllai 	/* unlink it from the directory cache */
13899e5aa9d8SRobert Mustacchi 	sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE);
1390facf4a8dSllai 	rw_exit(&parent->sdev_contents);
1391facf4a8dSllai 	vn_vfsunlock(vp);
13929e5aa9d8SRobert Mustacchi 	VN_RELE(vp);
1393facf4a8dSllai 
13949e5aa9d8SRobert Mustacchi 	/* best effort to clean up the backing store */
13959e5aa9d8SRobert Mustacchi 	if (SDEV_IS_PERSIST(parent)) {
13969e5aa9d8SRobert Mustacchi 		ASSERT(parent->sdev_attrvp);
13979e5aa9d8SRobert Mustacchi 		error = VOP_RMDIR(parent->sdev_attrvp, nm,
13989e5aa9d8SRobert Mustacchi 		    parent->sdev_attrvp, kcred, ct, flags);
1399facf4a8dSllai 
14009e5aa9d8SRobert Mustacchi 		if (error)
1401facf4a8dSllai 			sdcmn_err2(("sdev_rmdir: cleaning device %s is on"
1402facf4a8dSllai 			    " disk error %d\n", parent->sdev_path, error));
1403facf4a8dSllai 		if (error == EBUSY)
1404facf4a8dSllai 			error = 0;
14059e5aa9d8SRobert Mustacchi 
1406facf4a8dSllai 	}
1407facf4a8dSllai 
1408facf4a8dSllai 	return (error);
1409facf4a8dSllai }
1410facf4a8dSllai 
1411facf4a8dSllai /*
1412facf4a8dSllai  * read the contents of a symbolic link
1413facf4a8dSllai  */
1414facf4a8dSllai static int
sdev_readlink(struct vnode * vp,struct uio * uiop,struct cred * cred,caller_context_t * ct)1415da6c28aaSamw sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred,
1416da6c28aaSamw     caller_context_t *ct)
1417facf4a8dSllai {
1418facf4a8dSllai 	struct sdev_node *dv;
1419facf4a8dSllai 	int	error = 0;
1420facf4a8dSllai 
1421facf4a8dSllai 	ASSERT(vp->v_type == VLNK);
1422facf4a8dSllai 
1423facf4a8dSllai 	dv = VTOSDEV(vp);
1424facf4a8dSllai 
1425facf4a8dSllai 	if (dv->sdev_attrvp) {
1426facf4a8dSllai 		/* non-NULL attrvp implys a persisted node at READY state */
1427da6c28aaSamw 		return (VOP_READLINK(dv->sdev_attrvp, uiop, cred, ct));
1428facf4a8dSllai 	} else if (dv->sdev_symlink != NULL) {
1429facf4a8dSllai 		/* memory nodes, e.g. local nodes */
1430facf4a8dSllai 		rw_enter(&dv->sdev_contents, RW_READER);
1431facf4a8dSllai 		sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink));
1432facf4a8dSllai 		error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink),
1433facf4a8dSllai 		    UIO_READ, uiop);
1434facf4a8dSllai 		rw_exit(&dv->sdev_contents);
1435facf4a8dSllai 		return (error);
1436facf4a8dSllai 	}
1437facf4a8dSllai 
1438facf4a8dSllai 	return (ENOENT);
1439facf4a8dSllai }
1440facf4a8dSllai 
1441da6c28aaSamw /*ARGSUSED4*/
1442facf4a8dSllai static int
sdev_readdir(struct vnode * vp,struct uio * uiop,struct cred * cred,int * eofp,caller_context_t * ct,int flags)1443*f8927fa6SRobert Mustacchi sdev_readdir(struct vnode *vp, struct uio *uiop, struct cred *cred, int *eofp,
1444da6c28aaSamw     caller_context_t *ct, int flags)
1445facf4a8dSllai {
1446*f8927fa6SRobert Mustacchi 	struct sdev_node *dv = VTOSDEV(vp);
14476b938478Sjg 	int error;
14486b938478Sjg 
1449*f8927fa6SRobert Mustacchi 	VERIFY(RW_READ_HELD(&dv->sdev_contents));
1450*f8927fa6SRobert Mustacchi 
14518d3cb697SBryan Cantrill 	/*
1452*f8927fa6SRobert Mustacchi 	 * We can't recursively take ->sdev_contents via an indirect
1453*f8927fa6SRobert Mustacchi 	 * VOP_ACCESS(), but we don't need to use that anyway.
14548d3cb697SBryan Cantrill 	 */
1455*f8927fa6SRobert Mustacchi 	if ((error = sdev_self_access(dv, VEXEC, 0, cred, ct)) != 0)
1456*f8927fa6SRobert Mustacchi 		return (error);
1457b774fca8Sszhou 
1458*f8927fa6SRobert Mustacchi 	if (!SDEV_IS_GLOBAL(dv))
1459*f8927fa6SRobert Mustacchi 		prof_filldir(dv);
1460*f8927fa6SRobert Mustacchi 	return (devname_readdir_func(vp, uiop, cred, eofp, SDEV_BROWSE));
1461facf4a8dSllai }
1462facf4a8dSllai 
1463facf4a8dSllai /*ARGSUSED1*/
1464facf4a8dSllai static void
sdev_inactive(struct vnode * vp,struct cred * cred,caller_context_t * ct)1465da6c28aaSamw sdev_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
1466facf4a8dSllai {
1467d62bc4baSyz 	devname_inactive_func(vp, cred, NULL);
1468facf4a8dSllai }
1469facf4a8dSllai 
1470da6c28aaSamw /*ARGSUSED2*/
1471facf4a8dSllai static int
sdev_fid(struct vnode * vp,struct fid * fidp,caller_context_t * ct)1472da6c28aaSamw sdev_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
1473facf4a8dSllai {
1474facf4a8dSllai 	struct sdev_node	*dv = VTOSDEV(vp);
1475facf4a8dSllai 	struct sdev_fid	*sdev_fid;
1476facf4a8dSllai 
1477facf4a8dSllai 	if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) {
1478facf4a8dSllai 		fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t);
1479facf4a8dSllai 		return (ENOSPC);
1480facf4a8dSllai 	}
1481facf4a8dSllai 
1482facf4a8dSllai 	sdev_fid = (struct sdev_fid *)fidp;
1483facf4a8dSllai 	bzero(sdev_fid, sizeof (struct sdev_fid));
1484facf4a8dSllai 	sdev_fid->sdevfid_len =
1485facf4a8dSllai 	    (int)sizeof (struct sdev_fid) - sizeof (ushort_t);
1486facf4a8dSllai 	sdev_fid->sdevfid_ino = dv->sdev_ino;
1487facf4a8dSllai 
1488facf4a8dSllai 	return (0);
1489facf4a8dSllai }
1490facf4a8dSllai 
1491facf4a8dSllai /*
1492facf4a8dSllai  * This pair of routines bracket all VOP_READ, VOP_WRITE
1493facf4a8dSllai  * and VOP_READDIR requests.  The contents lock stops things
1494facf4a8dSllai  * moving around while we're looking at them.
1495facf4a8dSllai  */
1496cbcfaf83Sjg /*ARGSUSED2*/
1497cbcfaf83Sjg static int
sdev_rwlock(struct vnode * vp,int write_flag,caller_context_t * ctp)1498cbcfaf83Sjg sdev_rwlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
1499facf4a8dSllai {
1500cbcfaf83Sjg 	rw_enter(&VTOSDEV(vp)->sdev_contents,
1501cbcfaf83Sjg 	    write_flag ? RW_WRITER : RW_READER);
1502cbcfaf83Sjg 	return (write_flag ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE);
1503facf4a8dSllai }
1504facf4a8dSllai 
1505facf4a8dSllai /*ARGSUSED1*/
1506facf4a8dSllai static void
sdev_rwunlock(struct vnode * vp,int write_flag,caller_context_t * ctp)1507cbcfaf83Sjg sdev_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
1508facf4a8dSllai {
1509facf4a8dSllai 	rw_exit(&VTOSDEV(vp)->sdev_contents);
1510facf4a8dSllai }
1511facf4a8dSllai 
1512facf4a8dSllai /*ARGSUSED1*/
1513facf4a8dSllai static int
sdev_seek(struct vnode * vp,offset_t ooff,offset_t * noffp,caller_context_t * ct)1514da6c28aaSamw sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
1515da6c28aaSamw     caller_context_t *ct)
1516facf4a8dSllai {
1517facf4a8dSllai 	struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp;
1518facf4a8dSllai 
1519facf4a8dSllai 	ASSERT(vp->v_type != VCHR &&
1520facf4a8dSllai 	    vp->v_type != VBLK && vp->v_type != VLNK);
1521facf4a8dSllai 
1522facf4a8dSllai 	if (vp->v_type == VDIR)
1523da6c28aaSamw 		return (fs_seek(vp, ooff, noffp, ct));
1524facf4a8dSllai 
1525facf4a8dSllai 	ASSERT(attrvp);
1526da6c28aaSamw 	return (VOP_SEEK(attrvp, ooff, noffp, ct));
1527facf4a8dSllai }
1528facf4a8dSllai 
1529facf4a8dSllai /*ARGSUSED1*/
1530facf4a8dSllai static int
sdev_frlock(struct vnode * vp,int cmd,struct flock64 * bfp,int flag,offset_t offset,struct flk_callback * flk_cbp,struct cred * cr,caller_context_t * ct)1531facf4a8dSllai sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
1532da6c28aaSamw     offset_t offset, struct flk_callback *flk_cbp, struct cred *cr,
1533da6c28aaSamw     caller_context_t *ct)
1534facf4a8dSllai {
1535facf4a8dSllai 	int error;
1536facf4a8dSllai 	struct sdev_node *dv = VTOSDEV(vp);
1537facf4a8dSllai 
1538facf4a8dSllai 	ASSERT(dv);
1539facf4a8dSllai 	ASSERT(dv->sdev_attrvp);
1540facf4a8dSllai 	error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset,
1541da6c28aaSamw 	    flk_cbp, cr, ct);
1542facf4a8dSllai 
1543facf4a8dSllai 	return (error);
1544facf4a8dSllai }
1545facf4a8dSllai 
1546facf4a8dSllai static int
sdev_pathconf(vnode_t * vp,int cmd,ulong_t * valp,cred_t * cr,caller_context_t * ct)1547da6c28aaSamw sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
1548da6c28aaSamw     caller_context_t *ct)
1549facf4a8dSllai {
1550facf4a8dSllai 	switch (cmd) {
1551facf4a8dSllai 	case _PC_ACL_ENABLED:
1552facf4a8dSllai 		*valp = SDEV_ACL_FLAVOR(vp);
1553facf4a8dSllai 		return (0);
1554facf4a8dSllai 	}
1555facf4a8dSllai 
1556da6c28aaSamw 	return (fs_pathconf(vp, cmd, valp, cr, ct));
1557facf4a8dSllai }
1558facf4a8dSllai 
1559facf4a8dSllai vnodeops_t *sdev_vnodeops;
1560facf4a8dSllai 
1561facf4a8dSllai const fs_operation_def_t sdev_vnodeops_tbl[] = {
1562aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = sdev_open },
1563aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = sdev_close },
1564aa59c4cbSrsb 	VOPNAME_READ,		{ .vop_read = sdev_read },
1565aa59c4cbSrsb 	VOPNAME_WRITE,		{ .vop_write = sdev_write },
1566aa59c4cbSrsb 	VOPNAME_IOCTL,		{ .vop_ioctl = sdev_ioctl },
1567aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = sdev_getattr },
1568aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .vop_setattr = sdev_setattr },
1569aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = sdev_access },
1570aa59c4cbSrsb 	VOPNAME_LOOKUP,		{ .vop_lookup = sdev_lookup },
1571aa59c4cbSrsb 	VOPNAME_CREATE,		{ .vop_create = sdev_create },
1572aa59c4cbSrsb 	VOPNAME_RENAME,		{ .vop_rename = sdev_rename },
1573aa59c4cbSrsb 	VOPNAME_REMOVE,		{ .vop_remove = sdev_remove },
1574aa59c4cbSrsb 	VOPNAME_MKDIR,		{ .vop_mkdir = sdev_mkdir },
1575aa59c4cbSrsb 	VOPNAME_RMDIR,		{ .vop_rmdir = sdev_rmdir },
1576aa59c4cbSrsb 	VOPNAME_READDIR,	{ .vop_readdir = sdev_readdir },
1577aa59c4cbSrsb 	VOPNAME_SYMLINK,	{ .vop_symlink = sdev_symlink },
1578aa59c4cbSrsb 	VOPNAME_READLINK,	{ .vop_readlink = sdev_readlink },
1579aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = sdev_inactive },
1580aa59c4cbSrsb 	VOPNAME_FID,		{ .vop_fid = sdev_fid },
1581aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = sdev_rwlock },
1582aa59c4cbSrsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = sdev_rwunlock },
1583aa59c4cbSrsb 	VOPNAME_SEEK,		{ .vop_seek = sdev_seek },
1584aa59c4cbSrsb 	VOPNAME_FRLOCK,		{ .vop_frlock = sdev_frlock },
1585aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = sdev_pathconf },
1586aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = sdev_setsecattr },
1587aa59c4cbSrsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = sdev_getsecattr },
1588aa59c4cbSrsb 	NULL,			NULL
1589facf4a8dSllai };
1590facf4a8dSllai 
1591facf4a8dSllai int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl);
1592