xref: /titanic_51/usr/src/uts/common/fs/dev/sdev_vnops.c (revision de442498e34e37fe9b61cfe5beaacdecd06c6a1c)
1facf4a8dSllai1 /*
2facf4a8dSllai1  * CDDL HEADER START
3facf4a8dSllai1  *
4facf4a8dSllai1  * The contents of this file are subject to the terms of the
5facf4a8dSllai1  * Common Development and Distribution License (the "License").
6facf4a8dSllai1  * You may not use this file except in compliance with the License.
7facf4a8dSllai1  *
8facf4a8dSllai1  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9facf4a8dSllai1  * or http://www.opensolaris.org/os/licensing.
10facf4a8dSllai1  * See the License for the specific language governing permissions
11facf4a8dSllai1  * and limitations under the License.
12facf4a8dSllai1  *
13facf4a8dSllai1  * When distributing Covered Code, include this CDDL HEADER in each
14facf4a8dSllai1  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15facf4a8dSllai1  * If applicable, add the following below this CDDL HEADER, with the
16facf4a8dSllai1  * fields enclosed by brackets "[]" replaced with your own identifying
17facf4a8dSllai1  * information: Portions Copyright [yyyy] [name of copyright owner]
18facf4a8dSllai1  *
19facf4a8dSllai1  * CDDL HEADER END
20facf4a8dSllai1  */
21facf4a8dSllai1 /*
22134a1f4eSCasper H.S. Dik  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23facf4a8dSllai1  */
249e5aa9d8SRobert Mustacchi /*
259e5aa9d8SRobert Mustacchi  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
269e5aa9d8SRobert Mustacchi  */
27facf4a8dSllai1 
28facf4a8dSllai1 /*
29facf4a8dSllai1  * vnode ops for the /dev filesystem
30facf4a8dSllai1  *
31facf4a8dSllai1  * - VDIR, VCHR, CBLK, and VLNK are considered must supported files
32facf4a8dSllai1  * - VREG and VDOOR are used for some internal implementations in
33facf4a8dSllai1  *    the global zone, e.g. devname and devfsadm communication
34facf4a8dSllai1  * - other file types are unusual in this namespace and
35facf4a8dSllai1  *    not supported for now
36facf4a8dSllai1  */
37facf4a8dSllai1 
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 
280facf4a8dSllai1 #include <sys/types.h>
281facf4a8dSllai1 #include <sys/param.h>
282facf4a8dSllai1 #include <sys/t_lock.h>
283facf4a8dSllai1 #include <sys/systm.h>
284facf4a8dSllai1 #include <sys/sysmacros.h>
285facf4a8dSllai1 #include <sys/user.h>
286facf4a8dSllai1 #include <sys/time.h>
287facf4a8dSllai1 #include <sys/vfs.h>
288facf4a8dSllai1 #include <sys/vnode.h>
289aa59c4cbSrsb #include <sys/vfs_opreg.h>
290facf4a8dSllai1 #include <sys/file.h>
291facf4a8dSllai1 #include <sys/fcntl.h>
292facf4a8dSllai1 #include <sys/flock.h>
293facf4a8dSllai1 #include <sys/kmem.h>
294facf4a8dSllai1 #include <sys/uio.h>
295facf4a8dSllai1 #include <sys/errno.h>
296facf4a8dSllai1 #include <sys/stat.h>
297facf4a8dSllai1 #include <sys/cred.h>
298facf4a8dSllai1 #include <sys/dirent.h>
299facf4a8dSllai1 #include <sys/pathname.h>
300facf4a8dSllai1 #include <sys/cmn_err.h>
301facf4a8dSllai1 #include <sys/debug.h>
302facf4a8dSllai1 #include <sys/policy.h>
303facf4a8dSllai1 #include <vm/hat.h>
304facf4a8dSllai1 #include <vm/seg_vn.h>
305facf4a8dSllai1 #include <vm/seg_map.h>
306facf4a8dSllai1 #include <vm/seg.h>
307facf4a8dSllai1 #include <vm/as.h>
308facf4a8dSllai1 #include <vm/page.h>
309facf4a8dSllai1 #include <sys/proc.h>
310facf4a8dSllai1 #include <sys/mode.h>
311facf4a8dSllai1 #include <sys/sunndi.h>
312facf4a8dSllai1 #include <sys/ptms.h>
313facf4a8dSllai1 #include <fs/fs_subr.h>
314facf4a8dSllai1 #include <sys/fs/dv_node.h>
315facf4a8dSllai1 #include <sys/fs/sdev_impl.h>
316facf4a8dSllai1 
317facf4a8dSllai1 /*ARGSUSED*/
318facf4a8dSllai1 static int
319da6c28aaSamw sdev_open(struct vnode **vpp, int flag, struct cred *cred, caller_context_t *ct)
320facf4a8dSllai1 {
321facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(*vpp);
322facf4a8dSllai1 	struct sdev_node *ddv = dv->sdev_dotdot;
323facf4a8dSllai1 	int error = 0;
324facf4a8dSllai1 
325facf4a8dSllai1 	if ((*vpp)->v_type == VDIR)
326facf4a8dSllai1 		return (0);
327facf4a8dSllai1 
328facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(dv))
329facf4a8dSllai1 		return (ENOTSUP);
330facf4a8dSllai1 
331681d9761SEric Taylor 	if ((*vpp)->v_type == VLNK)
332681d9761SEric Taylor 		return (ENOENT);
333facf4a8dSllai1 	ASSERT((*vpp)->v_type == VREG);
334facf4a8dSllai1 	if ((*vpp)->v_type != VREG)
335facf4a8dSllai1 		return (ENOTSUP);
336facf4a8dSllai1 
337facf4a8dSllai1 	ASSERT(ddv);
338facf4a8dSllai1 	rw_enter(&ddv->sdev_contents, RW_READER);
339facf4a8dSllai1 	if (dv->sdev_attrvp == NULL) {
340facf4a8dSllai1 		rw_exit(&ddv->sdev_contents);
341facf4a8dSllai1 		return (ENOENT);
342facf4a8dSllai1 	}
343da6c28aaSamw 	error = VOP_OPEN(&(dv->sdev_attrvp), flag, cred, ct);
344facf4a8dSllai1 	rw_exit(&ddv->sdev_contents);
345facf4a8dSllai1 	return (error);
346facf4a8dSllai1 }
347facf4a8dSllai1 
348facf4a8dSllai1 /*ARGSUSED1*/
349facf4a8dSllai1 static int
350facf4a8dSllai1 sdev_close(struct vnode *vp, int flag, int count,
351da6c28aaSamw     offset_t offset, struct cred *cred, caller_context_t *ct)
352facf4a8dSllai1 {
353facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(vp);
354facf4a8dSllai1 
355facf4a8dSllai1 	if (vp->v_type == VDIR) {
356facf4a8dSllai1 		cleanlocks(vp, ttoproc(curthread)->p_pid, 0);
357facf4a8dSllai1 		cleanshares(vp, ttoproc(curthread)->p_pid);
358facf4a8dSllai1 		return (0);
359facf4a8dSllai1 	}
360facf4a8dSllai1 
361facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(dv))
362facf4a8dSllai1 		return (ENOTSUP);
363facf4a8dSllai1 
364facf4a8dSllai1 	ASSERT(vp->v_type == VREG);
365facf4a8dSllai1 	if (vp->v_type != VREG)
366facf4a8dSllai1 		return (ENOTSUP);
367facf4a8dSllai1 
368facf4a8dSllai1 	ASSERT(dv->sdev_attrvp);
369da6c28aaSamw 	return (VOP_CLOSE(dv->sdev_attrvp, flag, count, offset, cred, ct));
370facf4a8dSllai1 }
371facf4a8dSllai1 
372facf4a8dSllai1 /*ARGSUSED*/
373facf4a8dSllai1 static int
374facf4a8dSllai1 sdev_read(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
375facf4a8dSllai1 	struct caller_context *ct)
376facf4a8dSllai1 {
377facf4a8dSllai1 	struct sdev_node *dv = (struct sdev_node *)VTOSDEV(vp);
378facf4a8dSllai1 	int	error;
379facf4a8dSllai1 
380facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(dv))
381facf4a8dSllai1 		return (EINVAL);
382facf4a8dSllai1 
383facf4a8dSllai1 	if (vp->v_type == VDIR)
384facf4a8dSllai1 		return (EISDIR);
385facf4a8dSllai1 
386facf4a8dSllai1 	/* only supporting regular files in /dev */
387facf4a8dSllai1 	ASSERT(vp->v_type == VREG);
388facf4a8dSllai1 	if (vp->v_type != VREG)
389facf4a8dSllai1 		return (EINVAL);
390facf4a8dSllai1 
391facf4a8dSllai1 	ASSERT(RW_READ_HELD(&VTOSDEV(vp)->sdev_contents));
392facf4a8dSllai1 	ASSERT(dv->sdev_attrvp);
393da6c28aaSamw 	(void) VOP_RWLOCK(dv->sdev_attrvp, 0, ct);
394facf4a8dSllai1 	error = VOP_READ(dv->sdev_attrvp, uio, ioflag, cred, ct);
395da6c28aaSamw 	VOP_RWUNLOCK(dv->sdev_attrvp, 0, ct);
396facf4a8dSllai1 	return (error);
397facf4a8dSllai1 }
398facf4a8dSllai1 
399facf4a8dSllai1 /*ARGSUSED*/
400facf4a8dSllai1 static int
401facf4a8dSllai1 sdev_write(struct vnode *vp, struct uio *uio, int ioflag, struct cred *cred,
402facf4a8dSllai1 	struct caller_context *ct)
403facf4a8dSllai1 {
404facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(vp);
405facf4a8dSllai1 	int	error = 0;
406facf4a8dSllai1 
407facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(dv))
408facf4a8dSllai1 		return (EINVAL);
409facf4a8dSllai1 
410facf4a8dSllai1 	if (vp->v_type == VDIR)
411facf4a8dSllai1 		return (EISDIR);
412facf4a8dSllai1 
413facf4a8dSllai1 	/* only supporting regular files in /dev */
414facf4a8dSllai1 	ASSERT(vp->v_type == VREG);
415facf4a8dSllai1 	if (vp->v_type != VREG)
416facf4a8dSllai1 		return (EINVAL);
417facf4a8dSllai1 
418facf4a8dSllai1 	ASSERT(dv->sdev_attrvp);
419facf4a8dSllai1 
420da6c28aaSamw 	(void) VOP_RWLOCK(dv->sdev_attrvp, 1, ct);
421facf4a8dSllai1 	error = VOP_WRITE(dv->sdev_attrvp, uio, ioflag, cred, ct);
422da6c28aaSamw 	VOP_RWUNLOCK(dv->sdev_attrvp, 1, ct);
423facf4a8dSllai1 	if (error == 0) {
424facf4a8dSllai1 		sdev_update_timestamps(dv->sdev_attrvp, kcred,
425facf4a8dSllai1 		    AT_MTIME);
426facf4a8dSllai1 	}
427facf4a8dSllai1 	return (error);
428facf4a8dSllai1 }
429facf4a8dSllai1 
430facf4a8dSllai1 /*ARGSUSED*/
431facf4a8dSllai1 static int
432facf4a8dSllai1 sdev_ioctl(struct vnode *vp, int cmd, intptr_t arg, int flag,
433da6c28aaSamw     struct cred *cred, int *rvalp,  caller_context_t *ct)
434facf4a8dSllai1 {
435facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(vp);
436facf4a8dSllai1 
437facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(dv) || (vp->v_type == VDIR))
438facf4a8dSllai1 		return (ENOTTY);
439facf4a8dSllai1 
440facf4a8dSllai1 	ASSERT(vp->v_type == VREG);
441facf4a8dSllai1 	if (vp->v_type != VREG)
442facf4a8dSllai1 		return (EINVAL);
443facf4a8dSllai1 
444facf4a8dSllai1 	ASSERT(dv->sdev_attrvp);
445da6c28aaSamw 	return (VOP_IOCTL(dv->sdev_attrvp, cmd, arg, flag, cred, rvalp, ct));
446facf4a8dSllai1 }
447facf4a8dSllai1 
448facf4a8dSllai1 static int
449da6c28aaSamw sdev_getattr(struct vnode *vp, struct vattr *vap, int flags,
450da6c28aaSamw     struct cred *cr, caller_context_t *ct)
451facf4a8dSllai1 {
452facf4a8dSllai1 	int			error = 0;
453facf4a8dSllai1 	struct sdev_node	*dv = VTOSDEV(vp);
454facf4a8dSllai1 	struct sdev_node	*parent = dv->sdev_dotdot;
455facf4a8dSllai1 
456facf4a8dSllai1 	ASSERT(parent);
457facf4a8dSllai1 
458facf4a8dSllai1 	rw_enter(&parent->sdev_contents, RW_READER);
459facf4a8dSllai1 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
460facf4a8dSllai1 
461facf4a8dSllai1 	/*
462facf4a8dSllai1 	 * search order:
463facf4a8dSllai1 	 * 	- for persistent nodes (SDEV_PERSIST): backstore
464facf4a8dSllai1 	 *	- for non-persistent nodes: module ops if global, then memory
465facf4a8dSllai1 	 */
466facf4a8dSllai1 	if (dv->sdev_attrvp) {
467facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
468da6c28aaSamw 		error = VOP_GETATTR(dv->sdev_attrvp, vap, flags, cr, ct);
469facf4a8dSllai1 		sdev_vattr_merge(dv, vap);
470facf4a8dSllai1 	} else {
471facf4a8dSllai1 		ASSERT(dv->sdev_attr);
472facf4a8dSllai1 		*vap = *dv->sdev_attr;
473facf4a8dSllai1 		sdev_vattr_merge(dv, vap);
474facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
475facf4a8dSllai1 	}
476facf4a8dSllai1 
477facf4a8dSllai1 	return (error);
478facf4a8dSllai1 }
479facf4a8dSllai1 
480cbcfaf83Sjg /*ARGSUSED4*/
481facf4a8dSllai1 static int
482cbcfaf83Sjg sdev_setattr(struct vnode *vp, struct vattr *vap, int flags,
483cbcfaf83Sjg     struct cred *cred, caller_context_t *ctp)
484facf4a8dSllai1 {
485facf4a8dSllai1 	return (devname_setattr_func(vp, vap, flags, cred, NULL, 0));
486facf4a8dSllai1 }
487facf4a8dSllai1 
488facf4a8dSllai1 static int
489facf4a8dSllai1 sdev_getsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
490da6c28aaSamw     struct cred *cr, caller_context_t *ct)
491facf4a8dSllai1 {
492facf4a8dSllai1 	int	error;
493facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(vp);
494facf4a8dSllai1 	struct vnode *avp = dv->sdev_attrvp;
495facf4a8dSllai1 
496facf4a8dSllai1 	if (avp == NULL) {
497facf4a8dSllai1 		/* return fs_fab_acl() if flavor matches, else do nothing */
498facf4a8dSllai1 		if ((SDEV_ACL_FLAVOR(vp) == _ACL_ACLENT_ENABLED &&
499facf4a8dSllai1 		    (vsap->vsa_mask & (VSA_ACLCNT | VSA_DFACLCNT))) ||
500facf4a8dSllai1 		    (SDEV_ACL_FLAVOR(vp) == _ACL_ACE_ENABLED &&
501facf4a8dSllai1 		    (vsap->vsa_mask & (VSA_ACECNT | VSA_ACE))))
502da6c28aaSamw 			return (fs_fab_acl(vp, vsap, flags, cr, ct));
503facf4a8dSllai1 
504facf4a8dSllai1 		return (ENOSYS);
505facf4a8dSllai1 	}
506facf4a8dSllai1 
507da6c28aaSamw 	(void) VOP_RWLOCK(avp, 1, ct);
508da6c28aaSamw 	error = VOP_GETSECATTR(avp, vsap, flags, cr, ct);
509da6c28aaSamw 	VOP_RWUNLOCK(avp, 1, ct);
510facf4a8dSllai1 	return (error);
511facf4a8dSllai1 }
512facf4a8dSllai1 
513facf4a8dSllai1 static int
514facf4a8dSllai1 sdev_setsecattr(struct vnode *vp, struct vsecattr *vsap, int flags,
515da6c28aaSamw     struct cred *cr, caller_context_t *ct)
516facf4a8dSllai1 {
517facf4a8dSllai1 	int	error;
518facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(vp);
519facf4a8dSllai1 	struct vnode *avp = dv->sdev_attrvp;
520facf4a8dSllai1 
521facf4a8dSllai1 	if (dv->sdev_state == SDEV_ZOMBIE)
522facf4a8dSllai1 		return (0);
523facf4a8dSllai1 
524facf4a8dSllai1 	if (avp == NULL) {
525facf4a8dSllai1 		if (SDEV_IS_GLOBAL(dv) && !SDEV_IS_PERSIST(dv))
526facf4a8dSllai1 			return (fs_nosys());
527facf4a8dSllai1 		ASSERT(dv->sdev_attr);
528facf4a8dSllai1 		/*
529facf4a8dSllai1 		 * if coming in directly, the acl system call will
530facf4a8dSllai1 		 * have held the read-write lock via VOP_RWLOCK()
531facf4a8dSllai1 		 * If coming in via specfs, specfs will have
532facf4a8dSllai1 		 * held the rw lock on the realvp i.e. us.
533facf4a8dSllai1 		 */
534facf4a8dSllai1 		ASSERT(RW_WRITE_HELD(&dv->sdev_contents));
535facf4a8dSllai1 		sdev_vattr_merge(dv, dv->sdev_attr);
536facf4a8dSllai1 		error = sdev_shadow_node(dv, cr);
537facf4a8dSllai1 		if (error) {
538facf4a8dSllai1 			return (fs_nosys());
539facf4a8dSllai1 		}
540facf4a8dSllai1 
541facf4a8dSllai1 		ASSERT(dv->sdev_attrvp);
542facf4a8dSllai1 		/* clean out the memory copy if any */
543facf4a8dSllai1 		if (dv->sdev_attr) {
544facf4a8dSllai1 			kmem_free(dv->sdev_attr, sizeof (struct vattr));
545facf4a8dSllai1 			dv->sdev_attr = NULL;
546facf4a8dSllai1 		}
547facf4a8dSllai1 		avp = dv->sdev_attrvp;
548facf4a8dSllai1 	}
549facf4a8dSllai1 	ASSERT(avp);
550facf4a8dSllai1 
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);
554facf4a8dSllai1 	return (error);
555facf4a8dSllai1 }
556facf4a8dSllai1 
557*de442498SRobert Mustacchi /*
558*de442498SRobert Mustacchi  * There are two different unlocked routines. This one is not static as it is
559*de442498SRobert Mustacchi  * used as part of the secpolicy_vnode_setattr calls in sdev_subr.c. Because it
560*de442498SRobert Mustacchi  * is used in that function it has to have a specific signature.
561*de442498SRobert Mustacchi  */
562facf4a8dSllai1 int
563facf4a8dSllai1 sdev_unlocked_access(void *vdv, int mode, struct cred *cr)
564facf4a8dSllai1 {
565facf4a8dSllai1 	struct sdev_node	*dv = vdv;
566facf4a8dSllai1 	int			shift = 0;
567facf4a8dSllai1 	uid_t			owner = dv->sdev_attr->va_uid;
568facf4a8dSllai1 
569facf4a8dSllai1 	if (crgetuid(cr) != owner) {
570facf4a8dSllai1 		shift += 3;
571facf4a8dSllai1 		if (groupmember(dv->sdev_attr->va_gid, cr) == 0)
572facf4a8dSllai1 			shift += 3;
573facf4a8dSllai1 	}
574facf4a8dSllai1 
575134a1f4eSCasper H.S. Dik 	return (secpolicy_vnode_access2(cr, SDEVTOV(dv), owner,
576134a1f4eSCasper H.S. Dik 	    dv->sdev_attr->va_mode << shift, mode));
577facf4a8dSllai1 }
578facf4a8dSllai1 
579facf4a8dSllai1 static int
580*de442498SRobert Mustacchi sdev_self_access(sdev_node_t *dv, int mode, int flags, struct cred *cr,
581da6c28aaSamw     caller_context_t *ct)
582facf4a8dSllai1 {
583*de442498SRobert Mustacchi 	int ret;
584facf4a8dSllai1 
585facf4a8dSllai1 	ASSERT(dv->sdev_attr || dv->sdev_attrvp);
586facf4a8dSllai1 	if (dv->sdev_attrvp) {
587da6c28aaSamw 		ret = VOP_ACCESS(dv->sdev_attrvp, mode, flags, cr, ct);
588facf4a8dSllai1 	} else if (dv->sdev_attr) {
589facf4a8dSllai1 		ret = sdev_unlocked_access(dv, mode, cr);
590facf4a8dSllai1 		if (ret)
591facf4a8dSllai1 			ret = EACCES;
592facf4a8dSllai1 	}
593*de442498SRobert Mustacchi 
594*de442498SRobert Mustacchi 	return (ret);
595*de442498SRobert Mustacchi }
596*de442498SRobert Mustacchi 
597*de442498SRobert Mustacchi static int
598*de442498SRobert Mustacchi sdev_access(struct vnode *vp, int mode, int flags, struct cred *cr,
599*de442498SRobert Mustacchi     caller_context_t *ct)
600*de442498SRobert Mustacchi {
601*de442498SRobert Mustacchi 	struct sdev_node *dv = VTOSDEV(vp);
602*de442498SRobert Mustacchi 	int ret;
603*de442498SRobert Mustacchi 
604*de442498SRobert Mustacchi 	rw_enter(&dv->sdev_contents, RW_READER);
605*de442498SRobert Mustacchi 	ret = sdev_self_access(dv, mode, flags, cr, ct);
606368fc941SRobert Mustacchi 	rw_exit(&dv->sdev_contents);
607facf4a8dSllai1 
608facf4a8dSllai1 	return (ret);
609facf4a8dSllai1 }
610facf4a8dSllai1 
611facf4a8dSllai1 /*
612facf4a8dSllai1  * Lookup
613facf4a8dSllai1  */
614facf4a8dSllai1 /*ARGSUSED3*/
615facf4a8dSllai1 static int
616facf4a8dSllai1 sdev_lookup(struct vnode *dvp, char *nm, struct vnode **vpp,
617da6c28aaSamw     struct pathname *pnp, int flags, struct vnode *rdir, struct cred *cred,
618da6c28aaSamw     caller_context_t *ct, int *direntflags, pathname_t *realpnp)
619facf4a8dSllai1 {
620facf4a8dSllai1 	struct sdev_node *parent;
6216b938478Sjg 	int error;
622facf4a8dSllai1 
623facf4a8dSllai1 	parent = VTOSDEV(dvp);
624facf4a8dSllai1 	ASSERT(parent);
625facf4a8dSllai1 
6266b938478Sjg 	/* execute access is required to search the directory */
627da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
6286b938478Sjg 		return (error);
6296b938478Sjg 
630facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(parent))
631facf4a8dSllai1 		return (prof_lookup(dvp, nm, vpp, cred));
632facf4a8dSllai1 	return (devname_lookup_func(parent, nm, vpp, cred, NULL, 0));
633facf4a8dSllai1 }
634facf4a8dSllai1 
635facf4a8dSllai1 /*ARGSUSED2*/
636facf4a8dSllai1 static int
637facf4a8dSllai1 sdev_create(struct vnode *dvp, char *nm, struct vattr *vap, vcexcl_t excl,
638da6c28aaSamw     int mode, struct vnode **vpp, struct cred *cred, int flag,
639da6c28aaSamw     caller_context_t *ct, vsecattr_t *vsecp)
640facf4a8dSllai1 {
641facf4a8dSllai1 	struct vnode		*vp = NULL;
642facf4a8dSllai1 	struct vnode		*avp;
643facf4a8dSllai1 	struct sdev_node	*parent;
644facf4a8dSllai1 	struct sdev_node	*self = NULL;
645facf4a8dSllai1 	int			error = 0;
646facf4a8dSllai1 	vtype_t			type = vap->va_type;
647facf4a8dSllai1 
6480bfaec69Sllai1 	ASSERT(type != VNON && type != VBAD);
649facf4a8dSllai1 
650facf4a8dSllai1 	if ((type == VFIFO) || (type == VSOCK) ||
651facf4a8dSllai1 	    (type == VPROC) || (type == VPORT))
652facf4a8dSllai1 		return (ENOTSUP);
653facf4a8dSllai1 
654facf4a8dSllai1 	parent = VTOSDEV(dvp);
655facf4a8dSllai1 	ASSERT(parent);
656facf4a8dSllai1 
657facf4a8dSllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
658facf4a8dSllai1 	if (parent->sdev_state == SDEV_ZOMBIE) {
659facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
660facf4a8dSllai1 		return (ENOENT);
661facf4a8dSllai1 	}
662facf4a8dSllai1 
663facf4a8dSllai1 	/* non-global do not allow pure node creation */
664facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(parent)) {
665facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
666facf4a8dSllai1 		return (prof_lookup(dvp, nm, vpp, cred));
667facf4a8dSllai1 	}
668facf4a8dSllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
669facf4a8dSllai1 
6706b938478Sjg 	/* execute access is required to search the directory */
6711c4bb543SJerry Gilliam 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
6726b938478Sjg 		return (error);
6736b938478Sjg 
674facf4a8dSllai1 	/* check existing name */
675da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
676da6c28aaSamw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
677facf4a8dSllai1 
678facf4a8dSllai1 	/* name found */
679facf4a8dSllai1 	if (error == 0) {
680facf4a8dSllai1 		ASSERT(vp);
681facf4a8dSllai1 		if (excl == EXCL) {
682facf4a8dSllai1 			error = EEXIST;
683facf4a8dSllai1 		} else if ((vp->v_type == VDIR) && (mode & VWRITE)) {
684facf4a8dSllai1 			/* allowing create/read-only an existing directory */
685facf4a8dSllai1 			error = EISDIR;
686facf4a8dSllai1 		} else {
687174e0b3dSjg 			error = VOP_ACCESS(vp, mode, 0, cred, ct);
688facf4a8dSllai1 		}
689facf4a8dSllai1 
690facf4a8dSllai1 		if (error) {
691facf4a8dSllai1 			VN_RELE(vp);
692facf4a8dSllai1 			return (error);
693facf4a8dSllai1 		}
694facf4a8dSllai1 
695facf4a8dSllai1 		/* truncation first */
696facf4a8dSllai1 		if ((vp->v_type == VREG) && (vap->va_mask & AT_SIZE) &&
697facf4a8dSllai1 		    (vap->va_size == 0)) {
698facf4a8dSllai1 			ASSERT(parent->sdev_attrvp);
699facf4a8dSllai1 			error = VOP_CREATE(parent->sdev_attrvp,
700da6c28aaSamw 			    nm, vap, excl, mode, &avp, cred, flag, ct, vsecp);
701facf4a8dSllai1 
702facf4a8dSllai1 			if (error) {
703facf4a8dSllai1 				VN_RELE(vp);
704facf4a8dSllai1 				return (error);
705facf4a8dSllai1 			}
706facf4a8dSllai1 		}
707facf4a8dSllai1 
708facf4a8dSllai1 		sdev_update_timestamps(vp, kcred,
709facf4a8dSllai1 		    AT_CTIME|AT_MTIME|AT_ATIME);
710facf4a8dSllai1 		*vpp = vp;
711facf4a8dSllai1 		return (0);
712facf4a8dSllai1 	}
713facf4a8dSllai1 
714facf4a8dSllai1 	/* bail out early */
715facf4a8dSllai1 	if (error != ENOENT)
716facf4a8dSllai1 		return (error);
717facf4a8dSllai1 
7181c4bb543SJerry Gilliam 	/* verify write access - compliance specifies ENXIO */
7191c4bb543SJerry Gilliam 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0) {
7201c4bb543SJerry Gilliam 		if (error == EACCES)
7211c4bb543SJerry Gilliam 			error = ENXIO;
7221c4bb543SJerry Gilliam 		return (error);
7231c4bb543SJerry Gilliam 	}
7241c4bb543SJerry Gilliam 
725facf4a8dSllai1 	/*
726facf4a8dSllai1 	 * For memory-based (ROFS) directory:
727facf4a8dSllai1 	 * 	- either disallow node creation;
728facf4a8dSllai1 	 *	- or implement VOP_CREATE of its own
729facf4a8dSllai1 	 */
730facf4a8dSllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
731facf4a8dSllai1 	if (!SDEV_IS_PERSIST(parent)) {
732facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
733facf4a8dSllai1 		return (ENOTSUP);
734facf4a8dSllai1 	}
735facf4a8dSllai1 	ASSERT(parent->sdev_attrvp);
736facf4a8dSllai1 	error = sdev_mknode(parent, nm, &self, vap, NULL, NULL,
737facf4a8dSllai1 	    cred, SDEV_READY);
738facf4a8dSllai1 	if (error) {
739facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
740facf4a8dSllai1 		if (self)
741facf4a8dSllai1 			SDEV_RELE(self);
742facf4a8dSllai1 		return (error);
743facf4a8dSllai1 	}
744facf4a8dSllai1 	rw_exit(&parent->sdev_contents);
745facf4a8dSllai1 
746facf4a8dSllai1 	ASSERT(self);
747facf4a8dSllai1 	/* take care the timestamps for the node and its parent */
748facf4a8dSllai1 	sdev_update_timestamps(SDEVTOV(self), kcred,
749facf4a8dSllai1 	    AT_CTIME|AT_MTIME|AT_ATIME);
750facf4a8dSllai1 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
751facf4a8dSllai1 	if (SDEV_IS_GLOBAL(parent))
752facf4a8dSllai1 		atomic_inc_ulong(&parent->sdev_gdir_gen);
753facf4a8dSllai1 
754facf4a8dSllai1 	/* wake up other threads blocked on looking up this node */
755facf4a8dSllai1 	mutex_enter(&self->sdev_lookup_lock);
756facf4a8dSllai1 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
757facf4a8dSllai1 	mutex_exit(&self->sdev_lookup_lock);
758facf4a8dSllai1 	error = sdev_to_vp(self, vpp);
759facf4a8dSllai1 	return (error);
760facf4a8dSllai1 }
761facf4a8dSllai1 
762facf4a8dSllai1 static int
763da6c28aaSamw sdev_remove(struct vnode *dvp, char *nm, struct cred *cred,
764da6c28aaSamw     caller_context_t *ct, int flags)
765facf4a8dSllai1 {
766facf4a8dSllai1 	int	error;
767facf4a8dSllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
768facf4a8dSllai1 	struct vnode *vp = NULL;
769facf4a8dSllai1 	struct sdev_node *dv = NULL;
770facf4a8dSllai1 	int len;
7716c6a4562SJerry Gilliam 	int bkstore;
772facf4a8dSllai1 
773facf4a8dSllai1 	/* bail out early */
774facf4a8dSllai1 	len = strlen(nm);
775facf4a8dSllai1 	if (nm[0] == '.') {
776facf4a8dSllai1 		if (len == 1) {
777facf4a8dSllai1 			return (EINVAL);
778facf4a8dSllai1 		} else if (len == 2 && nm[1] == '.') {
779facf4a8dSllai1 			return (EEXIST);
780facf4a8dSllai1 		}
781facf4a8dSllai1 	}
782facf4a8dSllai1 
783facf4a8dSllai1 	ASSERT(parent);
784facf4a8dSllai1 	rw_enter(&parent->sdev_contents, RW_READER);
785facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(parent)) {
786facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
787facf4a8dSllai1 		return (ENOTSUP);
788facf4a8dSllai1 	}
789facf4a8dSllai1 
7906b938478Sjg 	/* execute access is required to search the directory */
791*de442498SRobert Mustacchi 	if ((error = sdev_self_access(parent, VEXEC, 0, cred, ct)) != 0) {
7926b938478Sjg 		rw_exit(&parent->sdev_contents);
7936b938478Sjg 		return (error);
7946b938478Sjg 	}
7956b938478Sjg 
796facf4a8dSllai1 	/* check existence first */
797facf4a8dSllai1 	dv = sdev_cache_lookup(parent, nm);
798facf4a8dSllai1 	if (dv == NULL) {
799facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
800facf4a8dSllai1 		return (ENOENT);
801facf4a8dSllai1 	}
802facf4a8dSllai1 
803facf4a8dSllai1 	vp = SDEVTOV(dv);
804facf4a8dSllai1 	if ((dv->sdev_state == SDEV_INIT) ||
805facf4a8dSllai1 	    (dv->sdev_state == SDEV_ZOMBIE)) {
806facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
807facf4a8dSllai1 		VN_RELE(vp);
808facf4a8dSllai1 		return (ENOENT);
809facf4a8dSllai1 	}
810facf4a8dSllai1 
8116b938478Sjg 	/* write access is required to remove an entry */
812*de442498SRobert Mustacchi 	if ((error = sdev_self_access(parent, VWRITE, 0, cred, ct)) != 0) {
8136b938478Sjg 		rw_exit(&parent->sdev_contents);
8146b938478Sjg 		VN_RELE(vp);
8156b938478Sjg 		return (error);
8166b938478Sjg 	}
8176b938478Sjg 
8186c6a4562SJerry Gilliam 	bkstore = SDEV_IS_PERSIST(dv) ? 1 : 0;
8196c6a4562SJerry Gilliam 	if (!rw_tryupgrade(&parent->sdev_contents)) {
8206c6a4562SJerry Gilliam 		rw_exit(&parent->sdev_contents);
8216c6a4562SJerry Gilliam 		rw_enter(&parent->sdev_contents, RW_WRITER);
8229e5aa9d8SRobert Mustacchi 		/* Make sure we didn't become a zombie */
8239e5aa9d8SRobert Mustacchi 		if (parent->sdev_state == SDEV_ZOMBIE) {
8249e5aa9d8SRobert Mustacchi 			rw_exit(&parent->sdev_contents);
8259e5aa9d8SRobert Mustacchi 			VN_RELE(vp);
8269e5aa9d8SRobert Mustacchi 			return (ENOENT);
8279e5aa9d8SRobert Mustacchi 		}
8286c6a4562SJerry Gilliam 	}
8296c6a4562SJerry Gilliam 
8306c6a4562SJerry Gilliam 	/* we do not support unlinking a non-empty directory */
8316c6a4562SJerry Gilliam 	if (vp->v_type == VDIR && dv->sdev_nlink > 2) {
8326c6a4562SJerry Gilliam 		rw_exit(&parent->sdev_contents);
8336c6a4562SJerry Gilliam 		VN_RELE(vp);
8346c6a4562SJerry Gilliam 		return (EBUSY);
8356c6a4562SJerry Gilliam 	}
8366c6a4562SJerry Gilliam 
837facf4a8dSllai1 	/*
838facf4a8dSllai1 	 * sdev_dirdelete does the real job of:
839facf4a8dSllai1 	 *  - make sure no open ref count
840facf4a8dSllai1 	 *  - destroying the sdev_node
841facf4a8dSllai1 	 *  - releasing the hold on attrvp
842facf4a8dSllai1 	 */
8439e5aa9d8SRobert Mustacchi 	sdev_cache_update(parent, &dv, nm, SDEV_CACHE_DELETE);
844facf4a8dSllai1 	VN_RELE(vp);
8459e5aa9d8SRobert Mustacchi 	rw_exit(&parent->sdev_contents);
846facf4a8dSllai1 
847facf4a8dSllai1 	/*
848facf4a8dSllai1 	 * best efforts clean up the backing store
849facf4a8dSllai1 	 */
850facf4a8dSllai1 	if (bkstore) {
851facf4a8dSllai1 		ASSERT(parent->sdev_attrvp);
852da6c28aaSamw 		error = VOP_REMOVE(parent->sdev_attrvp, nm, cred,
853da6c28aaSamw 		    ct, flags);
854facf4a8dSllai1 		/*
855facf4a8dSllai1 		 * do not report BUSY error
856facf4a8dSllai1 		 * because the backing store ref count is released
857facf4a8dSllai1 		 * when the last ref count on the sdev_node is
858facf4a8dSllai1 		 * released.
859facf4a8dSllai1 		 */
860facf4a8dSllai1 		if (error == EBUSY) {
861facf4a8dSllai1 			sdcmn_err2(("sdev_remove: device %s is still on"
862facf4a8dSllai1 			    "disk %s\n", nm, parent->sdev_path));
863facf4a8dSllai1 			error = 0;
864facf4a8dSllai1 		}
865facf4a8dSllai1 	}
866facf4a8dSllai1 
867facf4a8dSllai1 	return (error);
868facf4a8dSllai1 }
869facf4a8dSllai1 
870facf4a8dSllai1 /*
871facf4a8dSllai1  * Some restrictions for this file system:
872facf4a8dSllai1  *  - both oldnm and newnm are in the scope of /dev file system,
873facf4a8dSllai1  *    to simply the namespace management model.
874facf4a8dSllai1  */
875da6c28aaSamw /*ARGSUSED6*/
876facf4a8dSllai1 static int
877facf4a8dSllai1 sdev_rename(struct vnode *odvp, char *onm, struct vnode *ndvp, char *nnm,
878da6c28aaSamw     struct cred *cred, caller_context_t *ct, int flags)
879facf4a8dSllai1 {
880facf4a8dSllai1 	struct sdev_node	*fromparent = NULL;
881facf4a8dSllai1 	struct vattr		vattr;
882facf4a8dSllai1 	struct sdev_node	*toparent;
883facf4a8dSllai1 	struct sdev_node	*fromdv = NULL;	/* source node */
8840bfaec69Sllai1 	struct vnode 		*ovp = NULL;	/* source vnode */
885facf4a8dSllai1 	struct sdev_node	*todv = NULL;	/* destination node */
8860bfaec69Sllai1 	struct vnode 		*nvp = NULL;	/* destination vnode */
887facf4a8dSllai1 	int			samedir = 0;	/* set if odvp == ndvp */
888facf4a8dSllai1 	struct vnode		*realvp;
889facf4a8dSllai1 	int error = 0;
890facf4a8dSllai1 	dev_t fsid;
891facf4a8dSllai1 	int bkstore = 0;
8920bfaec69Sllai1 	vtype_t type;
893facf4a8dSllai1 
894facf4a8dSllai1 	/* prevent modifying "." and ".." */
895facf4a8dSllai1 	if ((onm[0] == '.' &&
8960bfaec69Sllai1 	    (onm[1] == '\0' || (onm[1] == '.' && onm[2] == '\0'))) ||
8970bfaec69Sllai1 	    (nnm[0] == '.' &&
8980bfaec69Sllai1 	    (nnm[1] == '\0' || (nnm[1] == '.' && nnm[2] == '\0')))) {
899facf4a8dSllai1 		return (EINVAL);
900facf4a8dSllai1 	}
901facf4a8dSllai1 
902facf4a8dSllai1 	fromparent = VTOSDEV(odvp);
903facf4a8dSllai1 	toparent = VTOSDEV(ndvp);
904facf4a8dSllai1 
905facf4a8dSllai1 	/* ZOMBIE parent doesn't allow new node creation */
906facf4a8dSllai1 	rw_enter(&fromparent->sdev_dotdot->sdev_contents, RW_READER);
907facf4a8dSllai1 	if (fromparent->sdev_state == SDEV_ZOMBIE) {
908facf4a8dSllai1 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
909facf4a8dSllai1 		return (ENOENT);
910facf4a8dSllai1 	}
911facf4a8dSllai1 
912facf4a8dSllai1 	/* renaming only supported for global device nodes */
913facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(fromparent)) {
914facf4a8dSllai1 		rw_exit(&fromparent->sdev_dotdot->sdev_contents);
915facf4a8dSllai1 		return (ENOTSUP);
916facf4a8dSllai1 	}
917facf4a8dSllai1 	rw_exit(&fromparent->sdev_dotdot->sdev_contents);
918facf4a8dSllai1 
919facf4a8dSllai1 	rw_enter(&toparent->sdev_dotdot->sdev_contents, RW_READER);
920facf4a8dSllai1 	if (toparent->sdev_state == SDEV_ZOMBIE) {
921facf4a8dSllai1 		rw_exit(&toparent->sdev_dotdot->sdev_contents);
922facf4a8dSllai1 		return (ENOENT);
923facf4a8dSllai1 	}
924facf4a8dSllai1 	rw_exit(&toparent->sdev_dotdot->sdev_contents);
925facf4a8dSllai1 
9260bfaec69Sllai1 	/*
9276b938478Sjg 	 * acquire the global lock to prevent
9280bfaec69Sllai1 	 * mount/unmount/other rename activities.
9290bfaec69Sllai1 	 */
9300bfaec69Sllai1 	mutex_enter(&sdev_lock);
9310bfaec69Sllai1 
932facf4a8dSllai1 	/* check existence of the source node */
933da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
934da6c28aaSamw 	error = VOP_LOOKUP(odvp, onm, &ovp, NULL, 0, NULL, cred, ct,
935da6c28aaSamw 	    NULL, NULL);
936facf4a8dSllai1 	if (error) {
937facf4a8dSllai1 		sdcmn_err2(("sdev_rename: the source node %s exists\n",
938facf4a8dSllai1 		    onm));
9390bfaec69Sllai1 		mutex_exit(&sdev_lock);
940facf4a8dSllai1 		return (error);
941facf4a8dSllai1 	}
942facf4a8dSllai1 
943da6c28aaSamw 	if (VOP_REALVP(ovp, &realvp, ct) == 0) {
944facf4a8dSllai1 		VN_HOLD(realvp);
945facf4a8dSllai1 		VN_RELE(ovp);
946facf4a8dSllai1 		ovp = realvp;
947facf4a8dSllai1 	}
948facf4a8dSllai1 
949facf4a8dSllai1 	/* check existence of destination */
950da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
951da6c28aaSamw 	error = VOP_LOOKUP(ndvp, nnm, &nvp, NULL, 0, NULL, cred, ct,
952da6c28aaSamw 	    NULL, NULL);
953facf4a8dSllai1 	if (error && (error != ENOENT)) {
9540bfaec69Sllai1 		mutex_exit(&sdev_lock);
955facf4a8dSllai1 		VN_RELE(ovp);
956facf4a8dSllai1 		return (error);
957facf4a8dSllai1 	}
958facf4a8dSllai1 
959da6c28aaSamw 	if (nvp && (VOP_REALVP(nvp, &realvp, ct) == 0)) {
960facf4a8dSllai1 		VN_HOLD(realvp);
961facf4a8dSllai1 		VN_RELE(nvp);
962facf4a8dSllai1 		nvp = realvp;
963facf4a8dSllai1 	}
964facf4a8dSllai1 
965facf4a8dSllai1 	/*
9660bfaec69Sllai1 	 * make sure the source and the destination are
9670bfaec69Sllai1 	 * in the same dev filesystem
968facf4a8dSllai1 	 */
969facf4a8dSllai1 	if (odvp != ndvp) {
970facf4a8dSllai1 		vattr.va_mask = AT_FSID;
971da6c28aaSamw 		if (error = VOP_GETATTR(odvp, &vattr, 0, cred, ct)) {
9720bfaec69Sllai1 			mutex_exit(&sdev_lock);
973facf4a8dSllai1 			VN_RELE(ovp);
9749e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
9759e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
976facf4a8dSllai1 			return (error);
977facf4a8dSllai1 		}
978facf4a8dSllai1 		fsid = vattr.va_fsid;
979facf4a8dSllai1 		vattr.va_mask = AT_FSID;
980da6c28aaSamw 		if (error = VOP_GETATTR(ndvp, &vattr, 0, cred, ct)) {
9810bfaec69Sllai1 			mutex_exit(&sdev_lock);
982facf4a8dSllai1 			VN_RELE(ovp);
9839e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
9849e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
985facf4a8dSllai1 			return (error);
986facf4a8dSllai1 		}
987facf4a8dSllai1 		if (fsid != vattr.va_fsid) {
9880bfaec69Sllai1 			mutex_exit(&sdev_lock);
989facf4a8dSllai1 			VN_RELE(ovp);
9909e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
9919e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
992facf4a8dSllai1 			return (EXDEV);
993facf4a8dSllai1 		}
994facf4a8dSllai1 	}
995facf4a8dSllai1 
996facf4a8dSllai1 	/* make sure the old entry can be deleted */
997da6c28aaSamw 	error = VOP_ACCESS(odvp, VWRITE, 0, cred, ct);
998facf4a8dSllai1 	if (error) {
9990bfaec69Sllai1 		mutex_exit(&sdev_lock);
1000facf4a8dSllai1 		VN_RELE(ovp);
10019e5aa9d8SRobert Mustacchi 		if (nvp != NULL)
10029e5aa9d8SRobert Mustacchi 			VN_RELE(nvp);
1003facf4a8dSllai1 		return (error);
1004facf4a8dSllai1 	}
1005facf4a8dSllai1 
1006facf4a8dSllai1 	/* make sure the destination allows creation */
1007facf4a8dSllai1 	samedir = (fromparent == toparent);
1008facf4a8dSllai1 	if (!samedir) {
1009da6c28aaSamw 		error = VOP_ACCESS(ndvp, VEXEC|VWRITE, 0, cred, ct);
1010facf4a8dSllai1 		if (error) {
10110bfaec69Sllai1 			mutex_exit(&sdev_lock);
1012facf4a8dSllai1 			VN_RELE(ovp);
10139e5aa9d8SRobert Mustacchi 			if (nvp != NULL)
10149e5aa9d8SRobert Mustacchi 				VN_RELE(nvp);
1015facf4a8dSllai1 			return (error);
1016facf4a8dSllai1 		}
1017facf4a8dSllai1 	}
1018facf4a8dSllai1 
1019facf4a8dSllai1 	fromdv = VTOSDEV(ovp);
1020facf4a8dSllai1 	ASSERT(fromdv);
1021facf4a8dSllai1 
10220bfaec69Sllai1 	/* destination file exists */
10239e5aa9d8SRobert Mustacchi 	if (nvp != NULL) {
10240bfaec69Sllai1 		todv = VTOSDEV(nvp);
10250bfaec69Sllai1 		ASSERT(todv);
1026facf4a8dSllai1 	}
1027facf4a8dSllai1 
10289e5aa9d8SRobert Mustacchi 	if ((fromdv->sdev_flags & SDEV_DYNAMIC) != 0 ||
10299e5aa9d8SRobert Mustacchi 	    (todv != NULL && (todv->sdev_flags & SDEV_DYNAMIC) != 0)) {
10309e5aa9d8SRobert Mustacchi 		mutex_exit(&sdev_lock);
10319e5aa9d8SRobert Mustacchi 		if (nvp != NULL)
10329e5aa9d8SRobert Mustacchi 			VN_RELE(nvp);
10339e5aa9d8SRobert Mustacchi 		VN_RELE(ovp);
10349e5aa9d8SRobert Mustacchi 		return (EACCES);
10359e5aa9d8SRobert Mustacchi 	}
10369e5aa9d8SRobert Mustacchi 
1037facf4a8dSllai1 	/*
10389e5aa9d8SRobert Mustacchi 	 * link source to new target in the memory. Regardless of failure, we
10399e5aa9d8SRobert Mustacchi 	 * must rele our hold on nvp.
1040facf4a8dSllai1 	 */
10410bfaec69Sllai1 	error = sdev_rnmnode(fromparent, fromdv, toparent, &todv, nnm, cred);
10429e5aa9d8SRobert Mustacchi 	if (nvp != NULL)
10439e5aa9d8SRobert Mustacchi 		VN_RELE(nvp);
1044facf4a8dSllai1 	if (error) {
1045facf4a8dSllai1 		sdcmn_err2(("sdev_rename: renaming %s to %s failed "
10460bfaec69Sllai1 		    " with error %d\n", onm, nnm, error));
10470bfaec69Sllai1 		mutex_exit(&sdev_lock);
1048facf4a8dSllai1 		VN_RELE(ovp);
1049facf4a8dSllai1 		return (error);
1050facf4a8dSllai1 	}
1051facf4a8dSllai1 
10520bfaec69Sllai1 	/*
10530bfaec69Sllai1 	 * unlink from source
10540bfaec69Sllai1 	 */
10550bfaec69Sllai1 	rw_enter(&fromparent->sdev_contents, RW_READER);
10560bfaec69Sllai1 	fromdv = sdev_cache_lookup(fromparent, onm);
10570bfaec69Sllai1 	if (fromdv == NULL) {
10580bfaec69Sllai1 		rw_exit(&fromparent->sdev_contents);
10590bfaec69Sllai1 		mutex_exit(&sdev_lock);
10609e5aa9d8SRobert Mustacchi 		VN_RELE(ovp);
10610bfaec69Sllai1 		sdcmn_err2(("sdev_rename: the source is deleted already\n"));
10620bfaec69Sllai1 		return (0);
10630bfaec69Sllai1 	}
10640bfaec69Sllai1 
10650bfaec69Sllai1 	if (fromdv->sdev_state == SDEV_ZOMBIE) {
10660bfaec69Sllai1 		rw_exit(&fromparent->sdev_contents);
10670bfaec69Sllai1 		mutex_exit(&sdev_lock);
10680bfaec69Sllai1 		VN_RELE(SDEVTOV(fromdv));
10699e5aa9d8SRobert Mustacchi 		VN_RELE(ovp);
10700bfaec69Sllai1 		sdcmn_err2(("sdev_rename: the source is being deleted\n"));
10710bfaec69Sllai1 		return (0);
10720bfaec69Sllai1 	}
10730bfaec69Sllai1 	rw_exit(&fromparent->sdev_contents);
10740bfaec69Sllai1 	ASSERT(SDEVTOV(fromdv) == ovp);
10750bfaec69Sllai1 	VN_RELE(ovp);
10760bfaec69Sllai1 
10770bfaec69Sllai1 	/* clean out the directory contents before it can be removed */
10780bfaec69Sllai1 	type = SDEVTOV(fromdv)->v_type;
10790bfaec69Sllai1 	if (type == VDIR) {
10800bfaec69Sllai1 		error = sdev_cleandir(fromdv, NULL, 0);
10810bfaec69Sllai1 		sdcmn_err2(("sdev_rename: cleandir finished with %d\n",
10820bfaec69Sllai1 		    error));
10830bfaec69Sllai1 		if (error == EBUSY)
10840bfaec69Sllai1 			error = 0;
10850bfaec69Sllai1 	}
10860bfaec69Sllai1 
1087facf4a8dSllai1 	rw_enter(&fromparent->sdev_contents, RW_WRITER);
10880bfaec69Sllai1 	bkstore = SDEV_IS_PERSIST(fromdv) ? 1 : 0;
10899e5aa9d8SRobert Mustacchi 	sdev_cache_update(fromparent, &fromdv, onm,
1090facf4a8dSllai1 	    SDEV_CACHE_DELETE);
10919e5aa9d8SRobert Mustacchi 	VN_RELE(SDEVTOV(fromdv));
1092facf4a8dSllai1 
1093facf4a8dSllai1 	/* best effforts clean up the backing store */
1094facf4a8dSllai1 	if (bkstore) {
1095facf4a8dSllai1 		ASSERT(fromparent->sdev_attrvp);
10960bfaec69Sllai1 		if (type != VDIR) {
1097da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_REMOVE */
1098facf4a8dSllai1 			error = VOP_REMOVE(fromparent->sdev_attrvp,
1099da6c28aaSamw 			    onm, kcred, ct, 0);
11000bfaec69Sllai1 		} else {
1101da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_RMDIR */
11020bfaec69Sllai1 			error = VOP_RMDIR(fromparent->sdev_attrvp,
1103da6c28aaSamw 			    onm, fromparent->sdev_attrvp, kcred, ct, 0);
11040bfaec69Sllai1 		}
11050bfaec69Sllai1 
1106facf4a8dSllai1 		if (error) {
1107facf4a8dSllai1 			sdcmn_err2(("sdev_rename: device %s is "
1108facf4a8dSllai1 			    "still on disk %s\n", onm,
1109facf4a8dSllai1 			    fromparent->sdev_path));
1110facf4a8dSllai1 			error = 0;
1111facf4a8dSllai1 		}
1112facf4a8dSllai1 	}
11130bfaec69Sllai1 	rw_exit(&fromparent->sdev_contents);
11140bfaec69Sllai1 	mutex_exit(&sdev_lock);
1115facf4a8dSllai1 
11160bfaec69Sllai1 	/* once reached to this point, the rename is regarded successful */
11170bfaec69Sllai1 	return (0);
1118facf4a8dSllai1 }
1119facf4a8dSllai1 
1120facf4a8dSllai1 /*
1121facf4a8dSllai1  * dev-fs version of "ln -s path dev-name"
1122facf4a8dSllai1  *	tnm - path, e.g. /devices/... or /dev/...
1123facf4a8dSllai1  *	lnm - dev_name
1124facf4a8dSllai1  */
1125da6c28aaSamw /*ARGSUSED6*/
1126facf4a8dSllai1 static int
1127facf4a8dSllai1 sdev_symlink(struct vnode *dvp, char *lnm, struct vattr *tva,
1128da6c28aaSamw     char *tnm, struct cred *cred, caller_context_t *ct, int flags)
1129facf4a8dSllai1 {
1130facf4a8dSllai1 	int error;
1131facf4a8dSllai1 	struct vnode *vp = NULL;
1132facf4a8dSllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1133facf4a8dSllai1 	struct sdev_node *self = (struct sdev_node *)NULL;
1134facf4a8dSllai1 
1135facf4a8dSllai1 	ASSERT(parent);
1136facf4a8dSllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1137facf4a8dSllai1 	if (parent->sdev_state == SDEV_ZOMBIE) {
1138facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1139facf4a8dSllai1 		sdcmn_err2(("sdev_symlink: parent %s is ZOMBIED \n",
1140facf4a8dSllai1 		    parent->sdev_name));
1141facf4a8dSllai1 		return (ENOENT);
1142facf4a8dSllai1 	}
1143facf4a8dSllai1 
1144facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(parent)) {
1145facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1146facf4a8dSllai1 		return (ENOTSUP);
1147facf4a8dSllai1 	}
1148facf4a8dSllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1149facf4a8dSllai1 
11506b938478Sjg 	/* execute access is required to search a directory */
1151da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
11526b938478Sjg 		return (error);
11536b938478Sjg 
1154facf4a8dSllai1 	/* find existing name */
1155da6c28aaSamw /* XXXci - We may need to translate the C-I flags here */
1156da6c28aaSamw 	error = VOP_LOOKUP(dvp, lnm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
1157facf4a8dSllai1 	if (error == 0) {
1158facf4a8dSllai1 		ASSERT(vp);
1159facf4a8dSllai1 		VN_RELE(vp);
1160facf4a8dSllai1 		sdcmn_err2(("sdev_symlink: node %s already exists\n", lnm));
1161facf4a8dSllai1 		return (EEXIST);
1162facf4a8dSllai1 	}
11636b938478Sjg 	if (error != ENOENT)
1164facf4a8dSllai1 		return (error);
11656b938478Sjg 
11666b938478Sjg 	/* write access is required to create a symlink */
1167da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0)
11686b938478Sjg 		return (error);
1169facf4a8dSllai1 
1170facf4a8dSllai1 	/* put it into memory cache */
1171facf4a8dSllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
1172facf4a8dSllai1 	error = sdev_mknode(parent, lnm, &self, tva, NULL, (void *)tnm,
1173facf4a8dSllai1 	    cred, SDEV_READY);
1174facf4a8dSllai1 	if (error) {
1175facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1176facf4a8dSllai1 		sdcmn_err2(("sdev_symlink: node %s creation failed\n", lnm));
1177facf4a8dSllai1 		if (self)
1178facf4a8dSllai1 			SDEV_RELE(self);
1179facf4a8dSllai1 
1180facf4a8dSllai1 		return (error);
1181facf4a8dSllai1 	}
1182facf4a8dSllai1 	ASSERT(self && (self->sdev_state == SDEV_READY));
1183facf4a8dSllai1 	rw_exit(&parent->sdev_contents);
1184facf4a8dSllai1 
1185facf4a8dSllai1 	/* take care the timestamps for the node and its parent */
1186facf4a8dSllai1 	sdev_update_timestamps(SDEVTOV(self), kcred,
1187facf4a8dSllai1 	    AT_CTIME|AT_MTIME|AT_ATIME);
1188facf4a8dSllai1 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
1189facf4a8dSllai1 	if (SDEV_IS_GLOBAL(parent))
1190facf4a8dSllai1 		atomic_inc_ulong(&parent->sdev_gdir_gen);
1191facf4a8dSllai1 
1192facf4a8dSllai1 	/* wake up other threads blocked on looking up this node */
1193facf4a8dSllai1 	mutex_enter(&self->sdev_lookup_lock);
1194facf4a8dSllai1 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
1195facf4a8dSllai1 	mutex_exit(&self->sdev_lookup_lock);
1196facf4a8dSllai1 	SDEV_RELE(self);	/* don't return with vnode held */
1197facf4a8dSllai1 	return (0);
1198facf4a8dSllai1 }
1199facf4a8dSllai1 
1200da6c28aaSamw /*ARGSUSED6*/
1201facf4a8dSllai1 static int
1202facf4a8dSllai1 sdev_mkdir(struct vnode *dvp, char *nm, struct vattr *va, struct vnode **vpp,
1203da6c28aaSamw     struct cred *cred, caller_context_t *ct, int flags, vsecattr_t *vsecp)
1204facf4a8dSllai1 {
1205facf4a8dSllai1 	int error;
1206facf4a8dSllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1207facf4a8dSllai1 	struct sdev_node *self = NULL;
1208facf4a8dSllai1 	struct vnode	*vp = NULL;
1209facf4a8dSllai1 
1210facf4a8dSllai1 	ASSERT(parent && parent->sdev_dotdot);
1211facf4a8dSllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1212facf4a8dSllai1 	if (parent->sdev_state == SDEV_ZOMBIE) {
1213facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1214facf4a8dSllai1 		return (ENOENT);
1215facf4a8dSllai1 	}
1216facf4a8dSllai1 
1217facf4a8dSllai1 	/* non-global do not allow pure directory creation */
1218facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(parent)) {
1219facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1220facf4a8dSllai1 		return (prof_lookup(dvp, nm, vpp, cred));
1221facf4a8dSllai1 	}
1222facf4a8dSllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1223facf4a8dSllai1 
12246b938478Sjg 	/* execute access is required to search the directory */
1225da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0) {
12266b938478Sjg 		return (error);
12276b938478Sjg 	}
12286b938478Sjg 
1229facf4a8dSllai1 	/* find existing name */
1230da6c28aaSamw /* XXXci - We may need to translate the C-I flags on VOP_LOOKUP */
1231da6c28aaSamw 	error = VOP_LOOKUP(dvp, nm, &vp, NULL, 0, NULL, cred, ct, NULL, NULL);
1232facf4a8dSllai1 	if (error == 0) {
1233facf4a8dSllai1 		VN_RELE(vp);
1234facf4a8dSllai1 		return (EEXIST);
1235facf4a8dSllai1 	}
1236facf4a8dSllai1 	if (error != ENOENT)
1237facf4a8dSllai1 		return (error);
1238facf4a8dSllai1 
12396b938478Sjg 	/* require write access to create a directory */
1240da6c28aaSamw 	if ((error = VOP_ACCESS(dvp, VWRITE, 0, cred, ct)) != 0) {
12416b938478Sjg 		return (error);
12426b938478Sjg 	}
12436b938478Sjg 
1244facf4a8dSllai1 	/* put it into memory */
1245facf4a8dSllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
1246facf4a8dSllai1 	error = sdev_mknode(parent, nm, &self,
1247facf4a8dSllai1 	    va, NULL, NULL, cred, SDEV_READY);
1248facf4a8dSllai1 	if (error) {
1249facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1250facf4a8dSllai1 		if (self)
1251facf4a8dSllai1 			SDEV_RELE(self);
1252facf4a8dSllai1 		return (error);
1253facf4a8dSllai1 	}
1254facf4a8dSllai1 	ASSERT(self && (self->sdev_state == SDEV_READY));
1255facf4a8dSllai1 	rw_exit(&parent->sdev_contents);
1256facf4a8dSllai1 
1257facf4a8dSllai1 	/* take care the timestamps for the node and its parent */
1258facf4a8dSllai1 	sdev_update_timestamps(SDEVTOV(self), kcred,
1259facf4a8dSllai1 	    AT_CTIME|AT_MTIME|AT_ATIME);
1260facf4a8dSllai1 	sdev_update_timestamps(dvp, kcred, AT_MTIME|AT_ATIME);
1261facf4a8dSllai1 	if (SDEV_IS_GLOBAL(parent))
1262facf4a8dSllai1 		atomic_inc_ulong(&parent->sdev_gdir_gen);
1263facf4a8dSllai1 
1264facf4a8dSllai1 	/* wake up other threads blocked on looking up this node */
1265facf4a8dSllai1 	mutex_enter(&self->sdev_lookup_lock);
1266facf4a8dSllai1 	SDEV_UNBLOCK_OTHERS(self, SDEV_LOOKUP);
1267facf4a8dSllai1 	mutex_exit(&self->sdev_lookup_lock);
1268facf4a8dSllai1 	*vpp = SDEVTOV(self);
1269facf4a8dSllai1 	return (0);
1270facf4a8dSllai1 }
1271facf4a8dSllai1 
1272facf4a8dSllai1 /*
1273facf4a8dSllai1  * allowing removing an empty directory under /dev
1274facf4a8dSllai1  */
1275facf4a8dSllai1 /*ARGSUSED*/
1276facf4a8dSllai1 static int
1277da6c28aaSamw sdev_rmdir(struct vnode *dvp, char *nm, struct vnode *cdir, struct cred *cred,
1278da6c28aaSamw     caller_context_t *ct, int flags)
1279facf4a8dSllai1 {
1280facf4a8dSllai1 	int error = 0;
1281facf4a8dSllai1 	struct sdev_node *parent = (struct sdev_node *)VTOSDEV(dvp);
1282facf4a8dSllai1 	struct sdev_node *self = NULL;
1283facf4a8dSllai1 	struct vnode *vp = NULL;
1284facf4a8dSllai1 
1285facf4a8dSllai1 	/* bail out early */
1286facf4a8dSllai1 	if (strcmp(nm, ".") == 0)
1287facf4a8dSllai1 		return (EINVAL);
1288facf4a8dSllai1 	if (strcmp(nm, "..") == 0)
1289facf4a8dSllai1 		return (EEXIST); /* should be ENOTEMPTY */
1290facf4a8dSllai1 
1291facf4a8dSllai1 	/* no destruction of non-global node */
1292facf4a8dSllai1 	ASSERT(parent && parent->sdev_dotdot);
1293facf4a8dSllai1 	rw_enter(&parent->sdev_dotdot->sdev_contents, RW_READER);
1294facf4a8dSllai1 	if (!SDEV_IS_GLOBAL(parent)) {
1295facf4a8dSllai1 		rw_exit(&parent->sdev_dotdot->sdev_contents);
1296facf4a8dSllai1 		return (ENOTSUP);
1297facf4a8dSllai1 	}
1298facf4a8dSllai1 	rw_exit(&parent->sdev_dotdot->sdev_contents);
1299facf4a8dSllai1 
13006b938478Sjg 	/* execute access is required to search the directory */
1301681d9761SEric Taylor 	if ((error = VOP_ACCESS(dvp, VEXEC|VWRITE, 0, cred, ct)) != 0)
13026b938478Sjg 		return (error);
13036b938478Sjg 
1304facf4a8dSllai1 	/* check existing name */
1305facf4a8dSllai1 	rw_enter(&parent->sdev_contents, RW_WRITER);
1306facf4a8dSllai1 	self = sdev_cache_lookup(parent, nm);
1307facf4a8dSllai1 	if (self == NULL) {
1308facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1309facf4a8dSllai1 		return (ENOENT);
1310facf4a8dSllai1 	}
1311facf4a8dSllai1 
1312facf4a8dSllai1 	vp = SDEVTOV(self);
1313facf4a8dSllai1 	if ((self->sdev_state == SDEV_INIT) ||
1314facf4a8dSllai1 	    (self->sdev_state == SDEV_ZOMBIE)) {
1315facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1316facf4a8dSllai1 		VN_RELE(vp);
1317facf4a8dSllai1 		return (ENOENT);
1318facf4a8dSllai1 	}
1319facf4a8dSllai1 
1320facf4a8dSllai1 	/* some sanity checks */
1321facf4a8dSllai1 	if (vp == dvp || vp == cdir) {
1322facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1323facf4a8dSllai1 		VN_RELE(vp);
1324facf4a8dSllai1 		return (EINVAL);
1325facf4a8dSllai1 	}
1326facf4a8dSllai1 
1327facf4a8dSllai1 	if (vp->v_type != VDIR) {
1328facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1329facf4a8dSllai1 		VN_RELE(vp);
1330facf4a8dSllai1 		return (ENOTDIR);
1331facf4a8dSllai1 	}
1332facf4a8dSllai1 
1333facf4a8dSllai1 	if (vn_vfswlock(vp)) {
1334facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1335facf4a8dSllai1 		VN_RELE(vp);
1336facf4a8dSllai1 		return (EBUSY);
1337facf4a8dSllai1 	}
1338facf4a8dSllai1 
1339facf4a8dSllai1 	if (vn_mountedvfs(vp) != NULL) {
1340facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1341facf4a8dSllai1 		vn_vfsunlock(vp);
1342facf4a8dSllai1 		VN_RELE(vp);
1343facf4a8dSllai1 		return (EBUSY);
1344facf4a8dSllai1 	}
1345facf4a8dSllai1 
1346facf4a8dSllai1 	self = VTOSDEV(vp);
1347facf4a8dSllai1 	/* bail out on a non-empty directory */
1348facf4a8dSllai1 	rw_enter(&self->sdev_contents, RW_READER);
1349facf4a8dSllai1 	if (self->sdev_nlink > 2) {
1350facf4a8dSllai1 		rw_exit(&self->sdev_contents);
1351facf4a8dSllai1 		rw_exit(&parent->sdev_contents);
1352facf4a8dSllai1 		vn_vfsunlock(vp);
1353facf4a8dSllai1 		VN_RELE(vp);
1354facf4a8dSllai1 		return (ENOTEMPTY);
1355facf4a8dSllai1 	}
1356facf4a8dSllai1 	rw_exit(&self->sdev_contents);
1357facf4a8dSllai1 
1358facf4a8dSllai1 	/* unlink it from the directory cache */
13599e5aa9d8SRobert Mustacchi 	sdev_cache_update(parent, &self, nm, SDEV_CACHE_DELETE);
1360facf4a8dSllai1 	rw_exit(&parent->sdev_contents);
1361facf4a8dSllai1 	vn_vfsunlock(vp);
1362facf4a8dSllai1 	VN_RELE(vp);
1363facf4a8dSllai1 
1364facf4a8dSllai1 	/* best effort to clean up the backing store */
1365facf4a8dSllai1 	if (SDEV_IS_PERSIST(parent)) {
1366facf4a8dSllai1 		ASSERT(parent->sdev_attrvp);
1367facf4a8dSllai1 		error = VOP_RMDIR(parent->sdev_attrvp, nm,
1368da6c28aaSamw 		    parent->sdev_attrvp, kcred, ct, flags);
13699e5aa9d8SRobert Mustacchi 
13709e5aa9d8SRobert Mustacchi 		if (error)
1371facf4a8dSllai1 			sdcmn_err2(("sdev_rmdir: cleaning device %s is on"
1372facf4a8dSllai1 			    " disk error %d\n", parent->sdev_path, error));
1373facf4a8dSllai1 		if (error == EBUSY)
1374facf4a8dSllai1 			error = 0;
13759e5aa9d8SRobert Mustacchi 
1376facf4a8dSllai1 	}
1377facf4a8dSllai1 
1378facf4a8dSllai1 	return (error);
1379facf4a8dSllai1 }
1380facf4a8dSllai1 
1381facf4a8dSllai1 /*
1382facf4a8dSllai1  * read the contents of a symbolic link
1383facf4a8dSllai1  */
1384facf4a8dSllai1 static int
1385da6c28aaSamw sdev_readlink(struct vnode *vp, struct uio *uiop, struct cred *cred,
1386da6c28aaSamw     caller_context_t *ct)
1387facf4a8dSllai1 {
1388facf4a8dSllai1 	struct sdev_node *dv;
1389facf4a8dSllai1 	int	error = 0;
1390facf4a8dSllai1 
1391facf4a8dSllai1 	ASSERT(vp->v_type == VLNK);
1392facf4a8dSllai1 
1393facf4a8dSllai1 	dv = VTOSDEV(vp);
1394facf4a8dSllai1 
1395facf4a8dSllai1 	if (dv->sdev_attrvp) {
1396facf4a8dSllai1 		/* non-NULL attrvp implys a persisted node at READY state */
1397da6c28aaSamw 		return (VOP_READLINK(dv->sdev_attrvp, uiop, cred, ct));
1398facf4a8dSllai1 	} else if (dv->sdev_symlink != NULL) {
1399facf4a8dSllai1 		/* memory nodes, e.g. local nodes */
1400facf4a8dSllai1 		rw_enter(&dv->sdev_contents, RW_READER);
1401facf4a8dSllai1 		sdcmn_err2(("sdev_readlink link is %s\n", dv->sdev_symlink));
1402facf4a8dSllai1 		error = uiomove(dv->sdev_symlink, strlen(dv->sdev_symlink),
1403facf4a8dSllai1 		    UIO_READ, uiop);
1404facf4a8dSllai1 		rw_exit(&dv->sdev_contents);
1405facf4a8dSllai1 		return (error);
1406facf4a8dSllai1 	}
1407facf4a8dSllai1 
1408facf4a8dSllai1 	return (ENOENT);
1409facf4a8dSllai1 }
1410facf4a8dSllai1 
1411da6c28aaSamw /*ARGSUSED4*/
1412facf4a8dSllai1 static int
1413da6c28aaSamw sdev_readdir(struct vnode *dvp, struct uio *uiop, struct cred *cred, int *eofp,
1414da6c28aaSamw     caller_context_t *ct, int flags)
1415facf4a8dSllai1 {
1416b774fca8Sszhou 	struct sdev_node *parent = VTOSDEV(dvp);
14176b938478Sjg 	int error;
14186b938478Sjg 
14198d3cb697SBryan Cantrill 	/*
14208d3cb697SBryan Cantrill 	 * We must check that we have execute access to search the directory --
14218d3cb697SBryan Cantrill 	 * but because our sdev_contents lock is already held as a reader (the
14228d3cb697SBryan Cantrill 	 * caller must have done a VOP_RWLOCK()), we call directly into the
14238d3cb697SBryan Cantrill 	 * underlying access routine if sdev_attr is non-NULL.
14248d3cb697SBryan Cantrill 	 */
14258d3cb697SBryan Cantrill 	if (parent->sdev_attr != NULL) {
14268d3cb697SBryan Cantrill 		VERIFY(RW_READ_HELD(&parent->sdev_contents));
14278d3cb697SBryan Cantrill 
14288d3cb697SBryan Cantrill 		if (sdev_unlocked_access(parent, VEXEC, cred) != 0)
14298d3cb697SBryan Cantrill 			return (EACCES);
14308d3cb697SBryan Cantrill 	} else {
1431da6c28aaSamw 		if ((error = VOP_ACCESS(dvp, VEXEC, 0, cred, ct)) != 0)
14326b938478Sjg 			return (error);
14338d3cb697SBryan Cantrill 	}
1434b774fca8Sszhou 
1435b774fca8Sszhou 	ASSERT(parent);
1436b774fca8Sszhou 	if (!SDEV_IS_GLOBAL(parent))
1437b774fca8Sszhou 		prof_filldir(parent);
1438facf4a8dSllai1 	return (devname_readdir_func(dvp, uiop, cred, eofp, SDEV_BROWSE));
1439facf4a8dSllai1 }
1440facf4a8dSllai1 
1441facf4a8dSllai1 /*ARGSUSED1*/
1442facf4a8dSllai1 static void
1443da6c28aaSamw sdev_inactive(struct vnode *vp, struct cred *cred, caller_context_t *ct)
1444facf4a8dSllai1 {
1445d62bc4baSyz147064 	devname_inactive_func(vp, cred, NULL);
1446facf4a8dSllai1 }
1447facf4a8dSllai1 
1448da6c28aaSamw /*ARGSUSED2*/
1449facf4a8dSllai1 static int
1450da6c28aaSamw sdev_fid(struct vnode *vp, struct fid *fidp, caller_context_t *ct)
1451facf4a8dSllai1 {
1452facf4a8dSllai1 	struct sdev_node	*dv = VTOSDEV(vp);
1453facf4a8dSllai1 	struct sdev_fid	*sdev_fid;
1454facf4a8dSllai1 
1455facf4a8dSllai1 	if (fidp->fid_len < (sizeof (struct sdev_fid) - sizeof (ushort_t))) {
1456facf4a8dSllai1 		fidp->fid_len = sizeof (struct sdev_fid) - sizeof (ushort_t);
1457facf4a8dSllai1 		return (ENOSPC);
1458facf4a8dSllai1 	}
1459facf4a8dSllai1 
1460facf4a8dSllai1 	sdev_fid = (struct sdev_fid *)fidp;
1461facf4a8dSllai1 	bzero(sdev_fid, sizeof (struct sdev_fid));
1462facf4a8dSllai1 	sdev_fid->sdevfid_len =
1463facf4a8dSllai1 	    (int)sizeof (struct sdev_fid) - sizeof (ushort_t);
1464facf4a8dSllai1 	sdev_fid->sdevfid_ino = dv->sdev_ino;
1465facf4a8dSllai1 
1466facf4a8dSllai1 	return (0);
1467facf4a8dSllai1 }
1468facf4a8dSllai1 
1469facf4a8dSllai1 /*
1470facf4a8dSllai1  * This pair of routines bracket all VOP_READ, VOP_WRITE
1471facf4a8dSllai1  * and VOP_READDIR requests.  The contents lock stops things
1472facf4a8dSllai1  * moving around while we're looking at them.
1473facf4a8dSllai1  */
1474cbcfaf83Sjg /*ARGSUSED2*/
1475cbcfaf83Sjg static int
1476cbcfaf83Sjg sdev_rwlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
1477facf4a8dSllai1 {
1478cbcfaf83Sjg 	rw_enter(&VTOSDEV(vp)->sdev_contents,
1479cbcfaf83Sjg 	    write_flag ? RW_WRITER : RW_READER);
1480cbcfaf83Sjg 	return (write_flag ? V_WRITELOCK_TRUE : V_WRITELOCK_FALSE);
1481facf4a8dSllai1 }
1482facf4a8dSllai1 
1483facf4a8dSllai1 /*ARGSUSED1*/
1484facf4a8dSllai1 static void
1485cbcfaf83Sjg sdev_rwunlock(struct vnode *vp, int write_flag, caller_context_t *ctp)
1486facf4a8dSllai1 {
1487facf4a8dSllai1 	rw_exit(&VTOSDEV(vp)->sdev_contents);
1488facf4a8dSllai1 }
1489facf4a8dSllai1 
1490facf4a8dSllai1 /*ARGSUSED1*/
1491facf4a8dSllai1 static int
1492da6c28aaSamw sdev_seek(struct vnode *vp, offset_t ooff, offset_t *noffp,
1493da6c28aaSamw     caller_context_t *ct)
1494facf4a8dSllai1 {
1495facf4a8dSllai1 	struct vnode *attrvp = VTOSDEV(vp)->sdev_attrvp;
1496facf4a8dSllai1 
1497facf4a8dSllai1 	ASSERT(vp->v_type != VCHR &&
1498facf4a8dSllai1 	    vp->v_type != VBLK && vp->v_type != VLNK);
1499facf4a8dSllai1 
1500facf4a8dSllai1 	if (vp->v_type == VDIR)
1501da6c28aaSamw 		return (fs_seek(vp, ooff, noffp, ct));
1502facf4a8dSllai1 
1503facf4a8dSllai1 	ASSERT(attrvp);
1504da6c28aaSamw 	return (VOP_SEEK(attrvp, ooff, noffp, ct));
1505facf4a8dSllai1 }
1506facf4a8dSllai1 
1507facf4a8dSllai1 /*ARGSUSED1*/
1508facf4a8dSllai1 static int
1509facf4a8dSllai1 sdev_frlock(struct vnode *vp, int cmd, struct flock64 *bfp, int flag,
1510da6c28aaSamw     offset_t offset, struct flk_callback *flk_cbp, struct cred *cr,
1511da6c28aaSamw     caller_context_t *ct)
1512facf4a8dSllai1 {
1513facf4a8dSllai1 	int error;
1514facf4a8dSllai1 	struct sdev_node *dv = VTOSDEV(vp);
1515facf4a8dSllai1 
1516facf4a8dSllai1 	ASSERT(dv);
1517facf4a8dSllai1 	ASSERT(dv->sdev_attrvp);
1518facf4a8dSllai1 	error = VOP_FRLOCK(dv->sdev_attrvp, cmd, bfp, flag, offset,
1519da6c28aaSamw 	    flk_cbp, cr, ct);
1520facf4a8dSllai1 
1521facf4a8dSllai1 	return (error);
1522facf4a8dSllai1 }
1523facf4a8dSllai1 
1524facf4a8dSllai1 static int
1525da6c28aaSamw sdev_pathconf(vnode_t *vp, int cmd, ulong_t *valp, cred_t *cr,
1526da6c28aaSamw     caller_context_t *ct)
1527facf4a8dSllai1 {
1528facf4a8dSllai1 	switch (cmd) {
1529facf4a8dSllai1 	case _PC_ACL_ENABLED:
1530facf4a8dSllai1 		*valp = SDEV_ACL_FLAVOR(vp);
1531facf4a8dSllai1 		return (0);
1532facf4a8dSllai1 	}
1533facf4a8dSllai1 
1534da6c28aaSamw 	return (fs_pathconf(vp, cmd, valp, cr, ct));
1535facf4a8dSllai1 }
1536facf4a8dSllai1 
1537facf4a8dSllai1 vnodeops_t *sdev_vnodeops;
1538facf4a8dSllai1 
1539facf4a8dSllai1 const fs_operation_def_t sdev_vnodeops_tbl[] = {
1540aa59c4cbSrsb 	VOPNAME_OPEN,		{ .vop_open = sdev_open },
1541aa59c4cbSrsb 	VOPNAME_CLOSE,		{ .vop_close = sdev_close },
1542aa59c4cbSrsb 	VOPNAME_READ,		{ .vop_read = sdev_read },
1543aa59c4cbSrsb 	VOPNAME_WRITE,		{ .vop_write = sdev_write },
1544aa59c4cbSrsb 	VOPNAME_IOCTL,		{ .vop_ioctl = sdev_ioctl },
1545aa59c4cbSrsb 	VOPNAME_GETATTR,	{ .vop_getattr = sdev_getattr },
1546aa59c4cbSrsb 	VOPNAME_SETATTR,	{ .vop_setattr = sdev_setattr },
1547aa59c4cbSrsb 	VOPNAME_ACCESS,		{ .vop_access = sdev_access },
1548aa59c4cbSrsb 	VOPNAME_LOOKUP,		{ .vop_lookup = sdev_lookup },
1549aa59c4cbSrsb 	VOPNAME_CREATE,		{ .vop_create = sdev_create },
1550aa59c4cbSrsb 	VOPNAME_RENAME,		{ .vop_rename = sdev_rename },
1551aa59c4cbSrsb 	VOPNAME_REMOVE,		{ .vop_remove = sdev_remove },
1552aa59c4cbSrsb 	VOPNAME_MKDIR,		{ .vop_mkdir = sdev_mkdir },
1553aa59c4cbSrsb 	VOPNAME_RMDIR,		{ .vop_rmdir = sdev_rmdir },
1554aa59c4cbSrsb 	VOPNAME_READDIR,	{ .vop_readdir = sdev_readdir },
1555aa59c4cbSrsb 	VOPNAME_SYMLINK,	{ .vop_symlink = sdev_symlink },
1556aa59c4cbSrsb 	VOPNAME_READLINK,	{ .vop_readlink = sdev_readlink },
1557aa59c4cbSrsb 	VOPNAME_INACTIVE,	{ .vop_inactive = sdev_inactive },
1558aa59c4cbSrsb 	VOPNAME_FID,		{ .vop_fid = sdev_fid },
1559aa59c4cbSrsb 	VOPNAME_RWLOCK,		{ .vop_rwlock = sdev_rwlock },
1560aa59c4cbSrsb 	VOPNAME_RWUNLOCK,	{ .vop_rwunlock = sdev_rwunlock },
1561aa59c4cbSrsb 	VOPNAME_SEEK,		{ .vop_seek = sdev_seek },
1562aa59c4cbSrsb 	VOPNAME_FRLOCK,		{ .vop_frlock = sdev_frlock },
1563aa59c4cbSrsb 	VOPNAME_PATHCONF,	{ .vop_pathconf = sdev_pathconf },
1564aa59c4cbSrsb 	VOPNAME_SETSECATTR,	{ .vop_setsecattr = sdev_setsecattr },
1565aa59c4cbSrsb 	VOPNAME_GETSECATTR,	{ .vop_getsecattr = sdev_getsecattr },
1566facf4a8dSllai1 	NULL,			NULL
1567facf4a8dSllai1 };
1568facf4a8dSllai1 
1569facf4a8dSllai1 int sdev_vnodeops_tbl_size = sizeof (sdev_vnodeops_tbl);
1570