xref: /xnu-8796.141.3/bsd/miscfs/devfs/devfs_vnops.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2000-2019 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions  * Copyright 1997,1998 Julian Elischer.  All rights reserved.
30*1b191cb5SApple OSS Distributions  * [email protected]
31*1b191cb5SApple OSS Distributions  *
32*1b191cb5SApple OSS Distributions  * Redistribution and use in source and binary forms, with or without
33*1b191cb5SApple OSS Distributions  * modification, are permitted provided that the following conditions are
34*1b191cb5SApple OSS Distributions  * met:
35*1b191cb5SApple OSS Distributions  *  1. Redistributions of source code must retain the above copyright
36*1b191cb5SApple OSS Distributions  *     notice, this list of conditions and the following disclaimer.
37*1b191cb5SApple OSS Distributions  *  2. Redistributions in binary form must reproduce the above copyright notice,
38*1b191cb5SApple OSS Distributions  *     this list of conditions and the following disclaimer in the documentation
39*1b191cb5SApple OSS Distributions  *     and/or other materials provided with the distribution.
40*1b191cb5SApple OSS Distributions  *
41*1b191cb5SApple OSS Distributions  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
42*1b191cb5SApple OSS Distributions  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
43*1b191cb5SApple OSS Distributions  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44*1b191cb5SApple OSS Distributions  * DISCLAIMED.  IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
45*1b191cb5SApple OSS Distributions  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*1b191cb5SApple OSS Distributions  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
47*1b191cb5SApple OSS Distributions  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
48*1b191cb5SApple OSS Distributions  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*1b191cb5SApple OSS Distributions  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*1b191cb5SApple OSS Distributions  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*1b191cb5SApple OSS Distributions  * SUCH DAMAGE.
52*1b191cb5SApple OSS Distributions  *
53*1b191cb5SApple OSS Distributions  * devfs_vnops.c
54*1b191cb5SApple OSS Distributions  */
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions /*
57*1b191cb5SApple OSS Distributions  * HISTORY
58*1b191cb5SApple OSS Distributions  *  Clark Warner ([email protected]) Tue Feb 10 2000
59*1b191cb5SApple OSS Distributions  *  - Added err_copyfile to the vnode operations table
60*1b191cb5SApple OSS Distributions  *  Dieter Siegmund ([email protected]) Thu Apr  8 14:08:19 PDT 1999
61*1b191cb5SApple OSS Distributions  *  - instead of duplicating specfs here, created a vnode-ops table
62*1b191cb5SApple OSS Distributions  *    that redirects most operations to specfs (as is done with ufs);
63*1b191cb5SApple OSS Distributions  *  - removed routines that made no sense
64*1b191cb5SApple OSS Distributions  *  - cleaned up reclaim: replaced devfs_vntodn() with a macro VTODN()
65*1b191cb5SApple OSS Distributions  *  - cleaned up symlink, link locking
66*1b191cb5SApple OSS Distributions  *  - added the devfs_lock to protect devfs data structures against
67*1b191cb5SApple OSS Distributions  *    driver's calling devfs_add_devswf()/etc.
68*1b191cb5SApple OSS Distributions  *  Dieter Siegmund ([email protected]) Wed Jul 14 13:37:59 PDT 1999
69*1b191cb5SApple OSS Distributions  *  - free the devfs devnode in devfs_inactive(), not just in devfs_reclaim()
70*1b191cb5SApple OSS Distributions  *    to free up kernel memory as soon as it's available
71*1b191cb5SApple OSS Distributions  *  - got rid of devfsspec_{read, write}
72*1b191cb5SApple OSS Distributions  *  Dieter Siegmund ([email protected]) Fri Sep 17 09:58:38 PDT 1999
73*1b191cb5SApple OSS Distributions  *  - update the mod/access times
74*1b191cb5SApple OSS Distributions  */
75*1b191cb5SApple OSS Distributions /*
76*1b191cb5SApple OSS Distributions  * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
77*1b191cb5SApple OSS Distributions  * support for mandatory and extensible security protections.  This notice
78*1b191cb5SApple OSS Distributions  * is included in support of clause 2.2 (b) of the Apple Public License,
79*1b191cb5SApple OSS Distributions  * Version 2.0.
80*1b191cb5SApple OSS Distributions  */
81*1b191cb5SApple OSS Distributions 
82*1b191cb5SApple OSS Distributions #include <sys/param.h>
83*1b191cb5SApple OSS Distributions #include <sys/systm.h>
84*1b191cb5SApple OSS Distributions #include <sys/namei.h>
85*1b191cb5SApple OSS Distributions #include <sys/kernel.h>
86*1b191cb5SApple OSS Distributions #include <sys/fcntl.h>
87*1b191cb5SApple OSS Distributions #include <sys/conf.h>
88*1b191cb5SApple OSS Distributions #include <sys/disklabel.h>
89*1b191cb5SApple OSS Distributions #include <sys/lock.h>
90*1b191cb5SApple OSS Distributions #include <sys/stat.h>
91*1b191cb5SApple OSS Distributions #include <sys/mount_internal.h>
92*1b191cb5SApple OSS Distributions #include <sys/proc.h>
93*1b191cb5SApple OSS Distributions #include <sys/kauth.h>
94*1b191cb5SApple OSS Distributions #include <sys/time.h>
95*1b191cb5SApple OSS Distributions #include <sys/vnode_internal.h>
96*1b191cb5SApple OSS Distributions #include <miscfs/specfs/specdev.h>
97*1b191cb5SApple OSS Distributions #include <sys/dirent.h>
98*1b191cb5SApple OSS Distributions #include <sys/vmmeter.h>
99*1b191cb5SApple OSS Distributions #include <sys/vm.h>
100*1b191cb5SApple OSS Distributions #include <sys/uio_internal.h>
101*1b191cb5SApple OSS Distributions 
102*1b191cb5SApple OSS Distributions #if CONFIG_MACF
103*1b191cb5SApple OSS Distributions #include <security/mac_framework.h>
104*1b191cb5SApple OSS Distributions #endif
105*1b191cb5SApple OSS Distributions 
106*1b191cb5SApple OSS Distributions #include "devfsdefs.h"
107*1b191cb5SApple OSS Distributions #include "devfs.h"
108*1b191cb5SApple OSS Distributions 
109*1b191cb5SApple OSS Distributions #if FDESC
110*1b191cb5SApple OSS Distributions #include "fdesc.h"
111*1b191cb5SApple OSS Distributions #endif /* FDESC */
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions static int              devfs_update(struct vnode *vp, struct timeval *access,
114*1b191cb5SApple OSS Distributions     struct timeval *modify);
115*1b191cb5SApple OSS Distributions void                    devfs_rele_node(devnode_t *);
116*1b191cb5SApple OSS Distributions static void             devfs_consider_time_update(devnode_t *dnp, uint32_t just_changed_flags);
117*1b191cb5SApple OSS Distributions static boolean_t        devfs_update_needed(long now_s, long last_s);
118*1b191cb5SApple OSS Distributions static boolean_t        devfs_is_name_protected(struct vnode *dvp, const char *name);
119*1b191cb5SApple OSS Distributions void                    dn_times_locked(devnode_t * dnp, struct timeval *t1, struct timeval *t2, struct timeval *t3, uint32_t just_changed_flags);
120*1b191cb5SApple OSS Distributions void                    dn_times_now(devnode_t *dnp, uint32_t just_changed_flags);
121*1b191cb5SApple OSS Distributions void                    dn_mark_for_delayed_times_update(devnode_t *dnp, uint32_t just_changed_flags);
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions void
dn_times_locked(devnode_t * dnp,struct timeval * t1,struct timeval * t2,struct timeval * t3,uint32_t just_changed_flags)124*1b191cb5SApple OSS Distributions dn_times_locked(devnode_t * dnp, struct timeval *t1, struct timeval *t2, struct timeval *t3, uint32_t just_changed_flags)
125*1b191cb5SApple OSS Distributions {
126*1b191cb5SApple OSS Distributions 	lck_mtx_assert(&devfs_attr_mutex, LCK_MTX_ASSERT_OWNED);
127*1b191cb5SApple OSS Distributions 
128*1b191cb5SApple OSS Distributions 	if (just_changed_flags & DEVFS_UPDATE_ACCESS) {
129*1b191cb5SApple OSS Distributions 		dnp->dn_atime.tv_sec = t1->tv_sec;
130*1b191cb5SApple OSS Distributions 		dnp->dn_atime.tv_nsec = t1->tv_usec * 1000;
131*1b191cb5SApple OSS Distributions 		dnp->dn_access = 0;
132*1b191cb5SApple OSS Distributions 	} else if (dnp->dn_access) {
133*1b191cb5SApple OSS Distributions 		dnp->dn_atime.tv_sec = MIN(t1->tv_sec, dnp->dn_atime.tv_sec + DEVFS_LAZY_UPDATE_SECONDS);
134*1b191cb5SApple OSS Distributions 		dnp->dn_atime.tv_nsec = t1->tv_usec * 1000;
135*1b191cb5SApple OSS Distributions 		dnp->dn_access = 0;
136*1b191cb5SApple OSS Distributions 	}
137*1b191cb5SApple OSS Distributions 
138*1b191cb5SApple OSS Distributions 	if (just_changed_flags & DEVFS_UPDATE_MOD) {
139*1b191cb5SApple OSS Distributions 		dnp->dn_mtime.tv_sec = t2->tv_sec;
140*1b191cb5SApple OSS Distributions 		dnp->dn_mtime.tv_nsec = t2->tv_usec * 1000;
141*1b191cb5SApple OSS Distributions 		dnp->dn_update = 0;
142*1b191cb5SApple OSS Distributions 	} else if (dnp->dn_update) {
143*1b191cb5SApple OSS Distributions 		dnp->dn_mtime.tv_sec = MIN(t2->tv_sec, dnp->dn_mtime.tv_sec + DEVFS_LAZY_UPDATE_SECONDS);
144*1b191cb5SApple OSS Distributions 		dnp->dn_mtime.tv_nsec = t2->tv_usec * 1000;
145*1b191cb5SApple OSS Distributions 		dnp->dn_update = 0;
146*1b191cb5SApple OSS Distributions 	}
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions 	if (just_changed_flags & DEVFS_UPDATE_CHANGE) {
149*1b191cb5SApple OSS Distributions 		dnp->dn_ctime.tv_sec = t3->tv_sec;
150*1b191cb5SApple OSS Distributions 		dnp->dn_ctime.tv_nsec = t3->tv_usec * 1000;
151*1b191cb5SApple OSS Distributions 		dnp->dn_change = 0;
152*1b191cb5SApple OSS Distributions 	} else if (dnp->dn_change) {
153*1b191cb5SApple OSS Distributions 		dnp->dn_ctime.tv_sec = MIN(t3->tv_sec, dnp->dn_ctime.tv_sec + DEVFS_LAZY_UPDATE_SECONDS);
154*1b191cb5SApple OSS Distributions 		dnp->dn_ctime.tv_nsec = t3->tv_usec * 1000;
155*1b191cb5SApple OSS Distributions 		dnp->dn_change = 0;
156*1b191cb5SApple OSS Distributions 	}
157*1b191cb5SApple OSS Distributions }
158*1b191cb5SApple OSS Distributions 
159*1b191cb5SApple OSS Distributions void
dn_mark_for_delayed_times_update(devnode_t * dnp,uint32_t just_changed_flags)160*1b191cb5SApple OSS Distributions dn_mark_for_delayed_times_update(devnode_t *dnp, uint32_t just_changed_flags)
161*1b191cb5SApple OSS Distributions {
162*1b191cb5SApple OSS Distributions 	if (just_changed_flags & DEVFS_UPDATE_CHANGE) {
163*1b191cb5SApple OSS Distributions 		dnp->dn_change = 1;
164*1b191cb5SApple OSS Distributions 	}
165*1b191cb5SApple OSS Distributions 	if (just_changed_flags & DEVFS_UPDATE_ACCESS) {
166*1b191cb5SApple OSS Distributions 		dnp->dn_access = 1;
167*1b191cb5SApple OSS Distributions 	}
168*1b191cb5SApple OSS Distributions 	if (just_changed_flags & DEVFS_UPDATE_MOD) {
169*1b191cb5SApple OSS Distributions 		dnp->dn_update = 1;
170*1b191cb5SApple OSS Distributions 	}
171*1b191cb5SApple OSS Distributions }
172*1b191cb5SApple OSS Distributions 
173*1b191cb5SApple OSS Distributions /*
174*1b191cb5SApple OSS Distributions  * Update times based on pending updates and optionally a set of new changes.
175*1b191cb5SApple OSS Distributions  */
176*1b191cb5SApple OSS Distributions void
dn_times_now(devnode_t * dnp,uint32_t just_changed_flags)177*1b191cb5SApple OSS Distributions dn_times_now(devnode_t * dnp, uint32_t just_changed_flags)
178*1b191cb5SApple OSS Distributions {
179*1b191cb5SApple OSS Distributions 	struct timeval now;
180*1b191cb5SApple OSS Distributions 
181*1b191cb5SApple OSS Distributions 	DEVFS_ATTR_LOCK_SPIN();
182*1b191cb5SApple OSS Distributions 	microtime(&now);
183*1b191cb5SApple OSS Distributions 	dn_times_locked(dnp, &now, &now, &now, just_changed_flags);
184*1b191cb5SApple OSS Distributions 	DEVFS_ATTR_UNLOCK();
185*1b191cb5SApple OSS Distributions }
186*1b191cb5SApple OSS Distributions 
187*1b191cb5SApple OSS Distributions /*
188*1b191cb5SApple OSS Distributions  * Critical devfs devices cannot be renamed or removed.
189*1b191cb5SApple OSS Distributions  * However, links to them may be moved/unlinked. So we block
190*1b191cb5SApple OSS Distributions  * remove/rename on a per-name basis, rather than per-node.
191*1b191cb5SApple OSS Distributions  */
192*1b191cb5SApple OSS Distributions static boolean_t
devfs_is_name_protected(struct vnode * dvp,const char * name)193*1b191cb5SApple OSS Distributions devfs_is_name_protected(struct vnode *dvp, const char *name)
194*1b191cb5SApple OSS Distributions {
195*1b191cb5SApple OSS Distributions 	/*
196*1b191cb5SApple OSS Distributions 	 * Only names in root are protected. E.g. /dev/null is protected,
197*1b191cb5SApple OSS Distributions 	 * but /dev/foo/null isn't.
198*1b191cb5SApple OSS Distributions 	 */
199*1b191cb5SApple OSS Distributions 	if (!vnode_isvroot(dvp)) {
200*1b191cb5SApple OSS Distributions 		return FALSE;
201*1b191cb5SApple OSS Distributions 	}
202*1b191cb5SApple OSS Distributions 
203*1b191cb5SApple OSS Distributions 	if ((strcmp("console", name) == 0) ||
204*1b191cb5SApple OSS Distributions 	    (strcmp("tty", name) == 0) ||
205*1b191cb5SApple OSS Distributions 	    (strcmp("null", name) == 0) ||
206*1b191cb5SApple OSS Distributions 	    (strcmp("zero", name) == 0) ||
207*1b191cb5SApple OSS Distributions 	    (strcmp("klog", name) == 0)) {
208*1b191cb5SApple OSS Distributions 		return TRUE;
209*1b191cb5SApple OSS Distributions 	}
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 	return FALSE;
212*1b191cb5SApple OSS Distributions }
213*1b191cb5SApple OSS Distributions 
214*1b191cb5SApple OSS Distributions 
215*1b191cb5SApple OSS Distributions /*
216*1b191cb5SApple OSS Distributions  * Convert a component of a pathname into a pointer to a locked node.
217*1b191cb5SApple OSS Distributions  * This is a very central and rather complicated routine.
218*1b191cb5SApple OSS Distributions  * If the file system is not maintained in a strict tree hierarchy,
219*1b191cb5SApple OSS Distributions  * this can result in a deadlock situation (see comments in code below).
220*1b191cb5SApple OSS Distributions  *
221*1b191cb5SApple OSS Distributions  * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
222*1b191cb5SApple OSS Distributions  * whether the name is to be looked up, created, renamed, or deleted.
223*1b191cb5SApple OSS Distributions  * When CREATE, RENAME, or DELETE is specified, information usable in
224*1b191cb5SApple OSS Distributions  * creating, renaming, or deleting a directory entry may be calculated.
225*1b191cb5SApple OSS Distributions  * If flag has LOCKPARENT or'ed into it and the target of the pathname
226*1b191cb5SApple OSS Distributions  * exists, lookup returns both the target and its parent directory locked.
227*1b191cb5SApple OSS Distributions  * When creating or renaming and LOCKPARENT is specified, the target may
228*1b191cb5SApple OSS Distributions  * not be ".".  When deleting and LOCKPARENT is specified, the target may
229*1b191cb5SApple OSS Distributions  * be "."., but the caller must check to ensure it does an vrele and DNUNLOCK
230*1b191cb5SApple OSS Distributions  * instead of two DNUNLOCKs.
231*1b191cb5SApple OSS Distributions  *
232*1b191cb5SApple OSS Distributions  * Overall outline of devfs_lookup:
233*1b191cb5SApple OSS Distributions  *
234*1b191cb5SApple OSS Distributions  *	check accessibility of directory
235*1b191cb5SApple OSS Distributions  *	null terminate the component (lookup leaves the whole string alone)
236*1b191cb5SApple OSS Distributions  *	look for name in cache, if found, then if at end of path
237*1b191cb5SApple OSS Distributions  *	  and deleting or creating, drop it, else return name
238*1b191cb5SApple OSS Distributions  *	search for name in directory, to found or notfound
239*1b191cb5SApple OSS Distributions  * notfound:
240*1b191cb5SApple OSS Distributions  *	if creating, return locked directory,
241*1b191cb5SApple OSS Distributions  *	else return error
242*1b191cb5SApple OSS Distributions  * found:
243*1b191cb5SApple OSS Distributions  *	if at end of path and deleting, return information to allow delete
244*1b191cb5SApple OSS Distributions  *	if at end of path and rewriting (RENAME and LOCKPARENT), lock target
245*1b191cb5SApple OSS Distributions  *	  node and return info to allow rewrite
246*1b191cb5SApple OSS Distributions  *	if not at end, add name to cache; if at end and neither creating
247*1b191cb5SApple OSS Distributions  *	  nor deleting, add name to cache
248*1b191cb5SApple OSS Distributions  * On return to lookup, remove the null termination we put in at the start.
249*1b191cb5SApple OSS Distributions  *
250*1b191cb5SApple OSS Distributions  * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent node unlocked.
251*1b191cb5SApple OSS Distributions  */
252*1b191cb5SApple OSS Distributions static int
devfs_lookup(struct vnop_lookup_args * ap)253*1b191cb5SApple OSS Distributions devfs_lookup(struct vnop_lookup_args *ap)
254*1b191cb5SApple OSS Distributions /*struct vnop_lookup_args {
255*1b191cb5SApple OSS Distributions  *       struct vnode * a_dvp; directory vnode ptr
256*1b191cb5SApple OSS Distributions  *       struct vnode ** a_vpp; where to put the result
257*1b191cb5SApple OSS Distributions  *       struct componentname * a_cnp; the name we want
258*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
259*1b191cb5SApple OSS Distributions  *  };*/
260*1b191cb5SApple OSS Distributions {
261*1b191cb5SApple OSS Distributions 	struct componentname *cnp = ap->a_cnp;
262*1b191cb5SApple OSS Distributions 	vfs_context_t ctx = cnp->cn_context;
263*1b191cb5SApple OSS Distributions 	struct proc *p = vfs_context_proc(ctx);
264*1b191cb5SApple OSS Distributions 	struct vnode *dir_vnode = ap->a_dvp;
265*1b191cb5SApple OSS Distributions 	struct vnode **result_vnode = ap->a_vpp;
266*1b191cb5SApple OSS Distributions 	devnode_t *   dir_node;       /* the directory we are searching */
267*1b191cb5SApple OSS Distributions 	devnode_t *   node = NULL;       /* the node we are searching for */
268*1b191cb5SApple OSS Distributions 	devdirent_t * nodename;
269*1b191cb5SApple OSS Distributions 	int flags = cnp->cn_flags;
270*1b191cb5SApple OSS Distributions 	int op = cnp->cn_nameiop;       /* LOOKUP, CREATE, RENAME, or DELETE */
271*1b191cb5SApple OSS Distributions 	int wantparent = flags & (LOCKPARENT | WANTPARENT);
272*1b191cb5SApple OSS Distributions 	int error = 0;
273*1b191cb5SApple OSS Distributions 	char    heldchar;       /* the char at the end of the name componet */
274*1b191cb5SApple OSS Distributions 
275*1b191cb5SApple OSS Distributions retry:
276*1b191cb5SApple OSS Distributions 
277*1b191cb5SApple OSS Distributions 	*result_vnode = NULL; /* safe not sorry */ /*XXX*/
278*1b191cb5SApple OSS Distributions 
279*1b191cb5SApple OSS Distributions 	/*  okay to look at directory vnodes ourside devfs lock as they are not aliased */
280*1b191cb5SApple OSS Distributions 	dir_node = VTODN(dir_vnode);
281*1b191cb5SApple OSS Distributions 
282*1b191cb5SApple OSS Distributions 	/*
283*1b191cb5SApple OSS Distributions 	 * Make sure that our node is a directory as well.
284*1b191cb5SApple OSS Distributions 	 */
285*1b191cb5SApple OSS Distributions 	if (dir_node->dn_type != DEV_DIR) {
286*1b191cb5SApple OSS Distributions 		return ENOTDIR;
287*1b191cb5SApple OSS Distributions 	}
288*1b191cb5SApple OSS Distributions 
289*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
290*1b191cb5SApple OSS Distributions 	/*
291*1b191cb5SApple OSS Distributions 	 * temporarily terminate string component
292*1b191cb5SApple OSS Distributions 	 */
293*1b191cb5SApple OSS Distributions 	heldchar = cnp->cn_nameptr[cnp->cn_namelen];
294*1b191cb5SApple OSS Distributions 	cnp->cn_nameptr[cnp->cn_namelen] = '\0';
295*1b191cb5SApple OSS Distributions 
296*1b191cb5SApple OSS Distributions 	nodename = dev_findname(dir_node, cnp->cn_nameptr);
297*1b191cb5SApple OSS Distributions 	/*
298*1b191cb5SApple OSS Distributions 	 * restore saved character
299*1b191cb5SApple OSS Distributions 	 */
300*1b191cb5SApple OSS Distributions 	cnp->cn_nameptr[cnp->cn_namelen] = heldchar;
301*1b191cb5SApple OSS Distributions 
302*1b191cb5SApple OSS Distributions 	if (nodename) {
303*1b191cb5SApple OSS Distributions 		/* entry exists */
304*1b191cb5SApple OSS Distributions 		node = nodename->de_dnp;
305*1b191cb5SApple OSS Distributions 
306*1b191cb5SApple OSS Distributions 		/* Do potential vnode allocation here inside the lock
307*1b191cb5SApple OSS Distributions 		 * to make sure that our device node has a non-NULL dn_vn
308*1b191cb5SApple OSS Distributions 		 * associated with it.  The device node might otherwise
309*1b191cb5SApple OSS Distributions 		 * get deleted out from under us (see devfs_dn_free()).
310*1b191cb5SApple OSS Distributions 		 */
311*1b191cb5SApple OSS Distributions 		error = devfs_dntovn(node, result_vnode, p);
312*1b191cb5SApple OSS Distributions 	}
313*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
314*1b191cb5SApple OSS Distributions 
315*1b191cb5SApple OSS Distributions 	if (error) {
316*1b191cb5SApple OSS Distributions 		if (error == EAGAIN) {
317*1b191cb5SApple OSS Distributions 			goto retry;
318*1b191cb5SApple OSS Distributions 		}
319*1b191cb5SApple OSS Distributions 		return error;
320*1b191cb5SApple OSS Distributions 	}
321*1b191cb5SApple OSS Distributions 	if (!nodename) {
322*1b191cb5SApple OSS Distributions 		/*
323*1b191cb5SApple OSS Distributions 		 * we haven't called devfs_dntovn if we get here
324*1b191cb5SApple OSS Distributions 		 * we have not taken a reference on the node.. no
325*1b191cb5SApple OSS Distributions 		 * vnode_put is necessary on these error returns
326*1b191cb5SApple OSS Distributions 		 *
327*1b191cb5SApple OSS Distributions 		 * If it doesn't exist and we're not the last component,
328*1b191cb5SApple OSS Distributions 		 * or we're at the last component, but we're not creating
329*1b191cb5SApple OSS Distributions 		 * or renaming, return ENOENT.
330*1b191cb5SApple OSS Distributions 		 */
331*1b191cb5SApple OSS Distributions 		if (!(flags & ISLASTCN) || !(op == CREATE || op == RENAME)) {
332*1b191cb5SApple OSS Distributions 			return ENOENT;
333*1b191cb5SApple OSS Distributions 		}
334*1b191cb5SApple OSS Distributions 		/*
335*1b191cb5SApple OSS Distributions 		 * We return with the directory locked, so that
336*1b191cb5SApple OSS Distributions 		 * the parameters we set up above will still be
337*1b191cb5SApple OSS Distributions 		 * valid if we actually decide to add a new entry.
338*1b191cb5SApple OSS Distributions 		 * We return ni_vp == NULL to indicate that the entry
339*1b191cb5SApple OSS Distributions 		 * does not currently exist; we leave a pointer to
340*1b191cb5SApple OSS Distributions 		 * the (locked) directory vnode in namei_data->ni_dvp.
341*1b191cb5SApple OSS Distributions 		 *
342*1b191cb5SApple OSS Distributions 		 * NB - if the directory is unlocked, then this
343*1b191cb5SApple OSS Distributions 		 * information cannot be used.
344*1b191cb5SApple OSS Distributions 		 */
345*1b191cb5SApple OSS Distributions 		return EJUSTRETURN;
346*1b191cb5SApple OSS Distributions 	}
347*1b191cb5SApple OSS Distributions 	/*
348*1b191cb5SApple OSS Distributions 	 * from this point forward, we need to vnode_put the reference
349*1b191cb5SApple OSS Distributions 	 * picked up in devfs_dntovn if we decide to return an error
350*1b191cb5SApple OSS Distributions 	 */
351*1b191cb5SApple OSS Distributions 
352*1b191cb5SApple OSS Distributions 	/*
353*1b191cb5SApple OSS Distributions 	 * If deleting, and at end of pathname, return
354*1b191cb5SApple OSS Distributions 	 * parameters which can be used to remove file.
355*1b191cb5SApple OSS Distributions 	 * If the wantparent flag isn't set, we return only
356*1b191cb5SApple OSS Distributions 	 * the directory (in namei_data->ni_dvp), otherwise we go
357*1b191cb5SApple OSS Distributions 	 * on and lock the node, being careful with ".".
358*1b191cb5SApple OSS Distributions 	 */
359*1b191cb5SApple OSS Distributions 	if (op == DELETE && (flags & ISLASTCN)) {
360*1b191cb5SApple OSS Distributions 		/*
361*1b191cb5SApple OSS Distributions 		 * we are trying to delete '.'.  What does this mean? XXX
362*1b191cb5SApple OSS Distributions 		 */
363*1b191cb5SApple OSS Distributions 		if (dir_node == node) {
364*1b191cb5SApple OSS Distributions 			if (*result_vnode) {
365*1b191cb5SApple OSS Distributions 				vnode_put(*result_vnode);
366*1b191cb5SApple OSS Distributions 				*result_vnode = NULL;
367*1b191cb5SApple OSS Distributions 			}
368*1b191cb5SApple OSS Distributions 			if (((error = vnode_get(dir_vnode)) == 0)) {
369*1b191cb5SApple OSS Distributions 				*result_vnode = dir_vnode;
370*1b191cb5SApple OSS Distributions 			}
371*1b191cb5SApple OSS Distributions 			return error;
372*1b191cb5SApple OSS Distributions 		}
373*1b191cb5SApple OSS Distributions 		return 0;
374*1b191cb5SApple OSS Distributions 	}
375*1b191cb5SApple OSS Distributions 
376*1b191cb5SApple OSS Distributions 	/*
377*1b191cb5SApple OSS Distributions 	 * If rewriting (RENAME), return the vnode and the
378*1b191cb5SApple OSS Distributions 	 * information required to rewrite the present directory
379*1b191cb5SApple OSS Distributions 	 * Must get node of directory entry to verify it's a
380*1b191cb5SApple OSS Distributions 	 * regular file, or empty directory.
381*1b191cb5SApple OSS Distributions 	 */
382*1b191cb5SApple OSS Distributions 	if (op == RENAME && wantparent && (flags & ISLASTCN)) {
383*1b191cb5SApple OSS Distributions 		/*
384*1b191cb5SApple OSS Distributions 		 * Careful about locking second node.
385*1b191cb5SApple OSS Distributions 		 * This can only occur if the target is ".".
386*1b191cb5SApple OSS Distributions 		 */
387*1b191cb5SApple OSS Distributions 		if (dir_node == node) {
388*1b191cb5SApple OSS Distributions 			error = EISDIR;
389*1b191cb5SApple OSS Distributions 			goto drop_ref;
390*1b191cb5SApple OSS Distributions 		}
391*1b191cb5SApple OSS Distributions 		return 0;
392*1b191cb5SApple OSS Distributions 	}
393*1b191cb5SApple OSS Distributions 
394*1b191cb5SApple OSS Distributions 	/*
395*1b191cb5SApple OSS Distributions 	 * Step through the translation in the name.  We do not unlock the
396*1b191cb5SApple OSS Distributions 	 * directory because we may need it again if a symbolic link
397*1b191cb5SApple OSS Distributions 	 * is relative to the current directory.  Instead we save it
398*1b191cb5SApple OSS Distributions 	 * unlocked as "saved_dir_node" XXX.  We must get the target
399*1b191cb5SApple OSS Distributions 	 * node before unlocking
400*1b191cb5SApple OSS Distributions 	 * the directory to insure that the node will not be removed
401*1b191cb5SApple OSS Distributions 	 * before we get it.  We prevent deadlock by always fetching
402*1b191cb5SApple OSS Distributions 	 * nodes from the root, moving down the directory tree. Thus
403*1b191cb5SApple OSS Distributions 	 * when following backward pointers ".." we must unlock the
404*1b191cb5SApple OSS Distributions 	 * parent directory before getting the requested directory.
405*1b191cb5SApple OSS Distributions 	 * There is a potential race condition here if both the current
406*1b191cb5SApple OSS Distributions 	 * and parent directories are removed before the lock for the
407*1b191cb5SApple OSS Distributions 	 * node associated with ".." returns.  We hope that this occurs
408*1b191cb5SApple OSS Distributions 	 * infrequently since we cannot avoid this race condition without
409*1b191cb5SApple OSS Distributions 	 * implementing a sophisticated deadlock detection algorithm.
410*1b191cb5SApple OSS Distributions 	 * Note also that this simple deadlock detection scheme will not
411*1b191cb5SApple OSS Distributions 	 * work if the file system has any hard links other than ".."
412*1b191cb5SApple OSS Distributions 	 * that point backwards in the directory structure.
413*1b191cb5SApple OSS Distributions 	 */
414*1b191cb5SApple OSS Distributions 	if ((flags & ISDOTDOT) == 0 && dir_node == node) {
415*1b191cb5SApple OSS Distributions 		if (*result_vnode) {
416*1b191cb5SApple OSS Distributions 			vnode_put(*result_vnode);
417*1b191cb5SApple OSS Distributions 			*result_vnode = NULL;
418*1b191cb5SApple OSS Distributions 		}
419*1b191cb5SApple OSS Distributions 		if ((error = vnode_get(dir_vnode))) {
420*1b191cb5SApple OSS Distributions 			return error;
421*1b191cb5SApple OSS Distributions 		}
422*1b191cb5SApple OSS Distributions 		*result_vnode = dir_vnode;
423*1b191cb5SApple OSS Distributions 	}
424*1b191cb5SApple OSS Distributions 	return 0;
425*1b191cb5SApple OSS Distributions 
426*1b191cb5SApple OSS Distributions drop_ref:
427*1b191cb5SApple OSS Distributions 	if (*result_vnode) {
428*1b191cb5SApple OSS Distributions 		vnode_put(*result_vnode);
429*1b191cb5SApple OSS Distributions 		*result_vnode = NULL;
430*1b191cb5SApple OSS Distributions 	}
431*1b191cb5SApple OSS Distributions 	return error;
432*1b191cb5SApple OSS Distributions }
433*1b191cb5SApple OSS Distributions 
434*1b191cb5SApple OSS Distributions static int
devfs_getattr(struct vnop_getattr_args * ap)435*1b191cb5SApple OSS Distributions devfs_getattr(struct vnop_getattr_args *ap)
436*1b191cb5SApple OSS Distributions /*struct vnop_getattr_args {
437*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
438*1b191cb5SApple OSS Distributions  *       struct vnode_attr *a_vap;
439*1b191cb5SApple OSS Distributions  *       kauth_cred_t a_cred;
440*1b191cb5SApple OSS Distributions  *       struct proc *a_p;
441*1b191cb5SApple OSS Distributions  *  } */
442*1b191cb5SApple OSS Distributions {
443*1b191cb5SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
444*1b191cb5SApple OSS Distributions 	struct vnode_attr *vap = ap->a_vap;
445*1b191cb5SApple OSS Distributions 	devnode_t *     file_node;
446*1b191cb5SApple OSS Distributions 	struct timeval now;
447*1b191cb5SApple OSS Distributions 
448*1b191cb5SApple OSS Distributions 
449*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
450*1b191cb5SApple OSS Distributions 	file_node = VTODN(vp);
451*1b191cb5SApple OSS Distributions 
452*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_mode, file_node->dn_mode);
453*1b191cb5SApple OSS Distributions 
454*1b191cb5SApple OSS Distributions 	/*
455*1b191cb5SApple OSS Distributions 	 * Note: for DEV_CDEV and DEV_BDEV, we return the device from
456*1b191cb5SApple OSS Distributions 	 * the vp, not the file_node; if we getting information on a
457*1b191cb5SApple OSS Distributions 	 * cloning device, we want the cloned information, not the template.
458*1b191cb5SApple OSS Distributions 	 */
459*1b191cb5SApple OSS Distributions 	switch (file_node->dn_type) {
460*1b191cb5SApple OSS Distributions 	case    DEV_DIR:
461*1b191cb5SApple OSS Distributions #if FDESC
462*1b191cb5SApple OSS Distributions 	case    DEV_DEVFD:      /* Like a directory */
463*1b191cb5SApple OSS Distributions #endif /* FDESC */
464*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_rdev, 0);
465*1b191cb5SApple OSS Distributions 		vap->va_mode |= (S_IFDIR);
466*1b191cb5SApple OSS Distributions 		break;
467*1b191cb5SApple OSS Distributions 	case    DEV_CDEV:
468*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_rdev, vp->v_rdev);
469*1b191cb5SApple OSS Distributions 		vap->va_mode |= (S_IFCHR);
470*1b191cb5SApple OSS Distributions 		break;
471*1b191cb5SApple OSS Distributions 	case    DEV_BDEV:
472*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_rdev, vp->v_rdev);
473*1b191cb5SApple OSS Distributions 		vap->va_mode |= (S_IFBLK);
474*1b191cb5SApple OSS Distributions 		break;
475*1b191cb5SApple OSS Distributions 	case    DEV_SLNK:
476*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_rdev, 0);
477*1b191cb5SApple OSS Distributions 		vap->va_mode |= (S_IFLNK);
478*1b191cb5SApple OSS Distributions 		break;
479*1b191cb5SApple OSS Distributions 	default:
480*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_rdev, 0);  /* default value only */
481*1b191cb5SApple OSS Distributions 	}
482*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_type, vp->v_type);
483*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_nlink, file_node->dn_links);
484*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_uid, file_node->dn_uid);
485*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_gid, file_node->dn_gid);
486*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_fsid, (uint32_t)VM_KERNEL_ADDRHASH(file_node->dn_dvm));
487*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_fileid, (uintptr_t)file_node->dn_ino);
488*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_data_size, file_node->dn_len);
489*1b191cb5SApple OSS Distributions 
490*1b191cb5SApple OSS Distributions 	/* return an override block size (advisory) */
491*1b191cb5SApple OSS Distributions 	if (vp->v_type == VBLK) {
492*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_iosize, BLKDEV_IOSIZE);
493*1b191cb5SApple OSS Distributions 	} else if (vp->v_type == VCHR) {
494*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_iosize, MAXPHYSIO);
495*1b191cb5SApple OSS Distributions 	} else {
496*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_iosize, (uint32_t)vp->v_mount->mnt_vfsstat.f_iosize);
497*1b191cb5SApple OSS Distributions 	}
498*1b191cb5SApple OSS Distributions 
499*1b191cb5SApple OSS Distributions 
500*1b191cb5SApple OSS Distributions 	DEVFS_ATTR_LOCK_SPIN();
501*1b191cb5SApple OSS Distributions 
502*1b191cb5SApple OSS Distributions 	microtime(&now);
503*1b191cb5SApple OSS Distributions 	dn_times_locked(file_node, &now, &now, &now, 0);
504*1b191cb5SApple OSS Distributions 
505*1b191cb5SApple OSS Distributions 	/* if the time is bogus, set it to the boot time */
506*1b191cb5SApple OSS Distributions 	if (file_node->dn_ctime.tv_sec == 0) {
507*1b191cb5SApple OSS Distributions 		file_node->dn_ctime.tv_sec = boottime_sec();
508*1b191cb5SApple OSS Distributions 		file_node->dn_ctime.tv_nsec = 0;
509*1b191cb5SApple OSS Distributions 	}
510*1b191cb5SApple OSS Distributions 	if (file_node->dn_mtime.tv_sec == 0) {
511*1b191cb5SApple OSS Distributions 		file_node->dn_mtime = file_node->dn_ctime;
512*1b191cb5SApple OSS Distributions 	}
513*1b191cb5SApple OSS Distributions 	if (file_node->dn_atime.tv_sec == 0) {
514*1b191cb5SApple OSS Distributions 		file_node->dn_atime = file_node->dn_ctime;
515*1b191cb5SApple OSS Distributions 	}
516*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_change_time, file_node->dn_ctime);
517*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_modify_time, file_node->dn_mtime);
518*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_access_time, file_node->dn_atime);
519*1b191cb5SApple OSS Distributions 
520*1b191cb5SApple OSS Distributions 	DEVFS_ATTR_UNLOCK();
521*1b191cb5SApple OSS Distributions 
522*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_gen, 0);
523*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_filerev, 0);
524*1b191cb5SApple OSS Distributions 	VATTR_RETURN(vap, va_acl, NULL);
525*1b191cb5SApple OSS Distributions 
526*1b191cb5SApple OSS Distributions 	/* Hide the root so Finder doesn't display it */
527*1b191cb5SApple OSS Distributions 	if (vnode_isvroot(vp)) {
528*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_flags, UF_HIDDEN);
529*1b191cb5SApple OSS Distributions 	} else {
530*1b191cb5SApple OSS Distributions 		VATTR_RETURN(vap, va_flags, 0);
531*1b191cb5SApple OSS Distributions 	}
532*1b191cb5SApple OSS Distributions 
533*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
534*1b191cb5SApple OSS Distributions 
535*1b191cb5SApple OSS Distributions 	return 0;
536*1b191cb5SApple OSS Distributions }
537*1b191cb5SApple OSS Distributions 
538*1b191cb5SApple OSS Distributions static int
devfs_setattr(struct vnop_setattr_args * ap)539*1b191cb5SApple OSS Distributions devfs_setattr(struct vnop_setattr_args *ap)
540*1b191cb5SApple OSS Distributions /*struct vnop_setattr_args  {
541*1b191cb5SApple OSS Distributions  *  struct vnode *a_vp;
542*1b191cb5SApple OSS Distributions  *  struct vnode_attr *a_vap;
543*1b191cb5SApple OSS Distributions  *  vfs_context_t a_context;
544*1b191cb5SApple OSS Distributions  *  } */
545*1b191cb5SApple OSS Distributions {
546*1b191cb5SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
547*1b191cb5SApple OSS Distributions 	struct vnode_attr *vap = ap->a_vap;
548*1b191cb5SApple OSS Distributions 	int error = 0;
549*1b191cb5SApple OSS Distributions 	devnode_t *     file_node;
550*1b191cb5SApple OSS Distributions 	struct timeval atimeval, mtimeval;
551*1b191cb5SApple OSS Distributions 
552*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
553*1b191cb5SApple OSS Distributions 
554*1b191cb5SApple OSS Distributions 	file_node = VTODN(vp);
555*1b191cb5SApple OSS Distributions 	/*
556*1b191cb5SApple OSS Distributions 	 * Go through the fields and update if set.
557*1b191cb5SApple OSS Distributions 	 */
558*1b191cb5SApple OSS Distributions 	if (VATTR_IS_ACTIVE(vap, va_access_time) || VATTR_IS_ACTIVE(vap, va_modify_time)) {
559*1b191cb5SApple OSS Distributions 		if (VATTR_IS_ACTIVE(vap, va_access_time)) {
560*1b191cb5SApple OSS Distributions 			file_node->dn_access = 1;
561*1b191cb5SApple OSS Distributions 		}
562*1b191cb5SApple OSS Distributions 		if (VATTR_IS_ACTIVE(vap, va_modify_time)) {
563*1b191cb5SApple OSS Distributions 			file_node->dn_change = 1;
564*1b191cb5SApple OSS Distributions 			file_node->dn_update = 1;
565*1b191cb5SApple OSS Distributions 		}
566*1b191cb5SApple OSS Distributions 		atimeval.tv_sec = vap->va_access_time.tv_sec;
567*1b191cb5SApple OSS Distributions 		atimeval.tv_usec = (suseconds_t)(vap->va_access_time.tv_nsec / 1000);
568*1b191cb5SApple OSS Distributions 		mtimeval.tv_sec = vap->va_modify_time.tv_sec;
569*1b191cb5SApple OSS Distributions 		mtimeval.tv_usec = (suseconds_t)(vap->va_modify_time.tv_nsec / 1000);
570*1b191cb5SApple OSS Distributions 
571*1b191cb5SApple OSS Distributions 		if ((error = devfs_update(vp, &atimeval, &mtimeval))) {
572*1b191cb5SApple OSS Distributions 			goto exit;
573*1b191cb5SApple OSS Distributions 		}
574*1b191cb5SApple OSS Distributions 	}
575*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_access_time);
576*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_change_time);
577*1b191cb5SApple OSS Distributions 
578*1b191cb5SApple OSS Distributions 	/*
579*1b191cb5SApple OSS Distributions 	 * Change the permissions.
580*1b191cb5SApple OSS Distributions 	 */
581*1b191cb5SApple OSS Distributions 	if (VATTR_IS_ACTIVE(vap, va_mode)) {
582*1b191cb5SApple OSS Distributions 		file_node->dn_mode &= ~07777;
583*1b191cb5SApple OSS Distributions 		file_node->dn_mode |= vap->va_mode & 07777;
584*1b191cb5SApple OSS Distributions 	}
585*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_mode);
586*1b191cb5SApple OSS Distributions 
587*1b191cb5SApple OSS Distributions 	/*
588*1b191cb5SApple OSS Distributions 	 * Change the owner.
589*1b191cb5SApple OSS Distributions 	 */
590*1b191cb5SApple OSS Distributions 	if (VATTR_IS_ACTIVE(vap, va_uid)) {
591*1b191cb5SApple OSS Distributions 		file_node->dn_uid = vap->va_uid;
592*1b191cb5SApple OSS Distributions 	}
593*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_uid);
594*1b191cb5SApple OSS Distributions 
595*1b191cb5SApple OSS Distributions 	/*
596*1b191cb5SApple OSS Distributions 	 * Change the group.
597*1b191cb5SApple OSS Distributions 	 */
598*1b191cb5SApple OSS Distributions 	if (VATTR_IS_ACTIVE(vap, va_gid)) {
599*1b191cb5SApple OSS Distributions 		file_node->dn_gid = vap->va_gid;
600*1b191cb5SApple OSS Distributions 	}
601*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_gid);
602*1b191cb5SApple OSS Distributions exit:
603*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
604*1b191cb5SApple OSS Distributions 
605*1b191cb5SApple OSS Distributions 	return error;
606*1b191cb5SApple OSS Distributions }
607*1b191cb5SApple OSS Distributions 
608*1b191cb5SApple OSS Distributions #if CONFIG_MACF
609*1b191cb5SApple OSS Distributions static int
devfs_setlabel(struct vnop_setlabel_args * ap)610*1b191cb5SApple OSS Distributions devfs_setlabel(struct vnop_setlabel_args *ap)
611*1b191cb5SApple OSS Distributions /* struct vnop_setlabel_args {
612*1b191cb5SApple OSS Distributions  *               struct vnodeop_desc *a_desc;
613*1b191cb5SApple OSS Distributions  *               struct vnode *a_vp;
614*1b191cb5SApple OSS Distributions  *               struct label *a_vl;
615*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
616*1b191cb5SApple OSS Distributions  *       } */
617*1b191cb5SApple OSS Distributions {
618*1b191cb5SApple OSS Distributions 	struct vnode *vp;
619*1b191cb5SApple OSS Distributions 	struct devnode *de;
620*1b191cb5SApple OSS Distributions 
621*1b191cb5SApple OSS Distributions 	vp = ap->a_vp;
622*1b191cb5SApple OSS Distributions 	de = VTODN(vp);
623*1b191cb5SApple OSS Distributions 
624*1b191cb5SApple OSS Distributions 	mac_vnode_label_update(ap->a_context, vp, ap->a_vl);
625*1b191cb5SApple OSS Distributions 	mac_devfs_label_update(vp->v_mount, de, vp);
626*1b191cb5SApple OSS Distributions 
627*1b191cb5SApple OSS Distributions 	return 0;
628*1b191cb5SApple OSS Distributions }
629*1b191cb5SApple OSS Distributions #endif
630*1b191cb5SApple OSS Distributions 
631*1b191cb5SApple OSS Distributions static int
devfs_read(struct vnop_read_args * ap)632*1b191cb5SApple OSS Distributions devfs_read(struct vnop_read_args *ap)
633*1b191cb5SApple OSS Distributions /* struct vnop_read_args {
634*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
635*1b191cb5SApple OSS Distributions  *       struct uio *a_uio;
636*1b191cb5SApple OSS Distributions  *       int  a_ioflag;
637*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
638*1b191cb5SApple OSS Distributions  *  } */
639*1b191cb5SApple OSS Distributions {
640*1b191cb5SApple OSS Distributions 	devnode_t * dn_p = VTODN(ap->a_vp);
641*1b191cb5SApple OSS Distributions 
642*1b191cb5SApple OSS Distributions 	switch (ap->a_vp->v_type) {
643*1b191cb5SApple OSS Distributions 	case VDIR: {
644*1b191cb5SApple OSS Distributions 		dn_p->dn_access = 1;
645*1b191cb5SApple OSS Distributions 
646*1b191cb5SApple OSS Distributions 		return VNOP_READDIR(ap->a_vp, ap->a_uio, 0, NULL, NULL, ap->a_context);
647*1b191cb5SApple OSS Distributions 	}
648*1b191cb5SApple OSS Distributions 	default: {
649*1b191cb5SApple OSS Distributions 		printf("devfs_read(): bad file type %d", ap->a_vp->v_type);
650*1b191cb5SApple OSS Distributions 		return EINVAL;
651*1b191cb5SApple OSS Distributions 	}
652*1b191cb5SApple OSS Distributions 	}
653*1b191cb5SApple OSS Distributions }
654*1b191cb5SApple OSS Distributions 
655*1b191cb5SApple OSS Distributions static int
devfs_close(struct vnop_close_args * ap)656*1b191cb5SApple OSS Distributions devfs_close(struct vnop_close_args *ap)
657*1b191cb5SApple OSS Distributions /* struct vnop_close_args {
658*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
659*1b191cb5SApple OSS Distributions  *       int  a_fflag;
660*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
661*1b191cb5SApple OSS Distributions  *  } */
662*1b191cb5SApple OSS Distributions {
663*1b191cb5SApple OSS Distributions 	struct vnode *          vp = ap->a_vp;
664*1b191cb5SApple OSS Distributions 	devnode_t *     dnp;
665*1b191cb5SApple OSS Distributions 
666*1b191cb5SApple OSS Distributions 	if (vnode_isinuse(vp, 1)) {
667*1b191cb5SApple OSS Distributions 		DEVFS_LOCK();
668*1b191cb5SApple OSS Distributions 		dnp = VTODN(vp);
669*1b191cb5SApple OSS Distributions 		if (dnp) {
670*1b191cb5SApple OSS Distributions 			dn_times_now(dnp, 0);
671*1b191cb5SApple OSS Distributions 		}
672*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
673*1b191cb5SApple OSS Distributions 	}
674*1b191cb5SApple OSS Distributions 	return 0;
675*1b191cb5SApple OSS Distributions }
676*1b191cb5SApple OSS Distributions 
677*1b191cb5SApple OSS Distributions static int
devfsspec_close(struct vnop_close_args * ap)678*1b191cb5SApple OSS Distributions devfsspec_close(struct vnop_close_args *ap)
679*1b191cb5SApple OSS Distributions /* struct vnop_close_args {
680*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
681*1b191cb5SApple OSS Distributions  *       int  a_fflag;
682*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
683*1b191cb5SApple OSS Distributions  *  } */
684*1b191cb5SApple OSS Distributions {
685*1b191cb5SApple OSS Distributions 	struct vnode *          vp = ap->a_vp;
686*1b191cb5SApple OSS Distributions 	devnode_t *     dnp;
687*1b191cb5SApple OSS Distributions 
688*1b191cb5SApple OSS Distributions 	if (vnode_isinuse(vp, 0)) {
689*1b191cb5SApple OSS Distributions 		DEVFS_LOCK();
690*1b191cb5SApple OSS Distributions 		dnp = VTODN(vp);
691*1b191cb5SApple OSS Distributions 		if (dnp) {
692*1b191cb5SApple OSS Distributions 			dn_times_now(dnp, 0);
693*1b191cb5SApple OSS Distributions 		}
694*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
695*1b191cb5SApple OSS Distributions 	}
696*1b191cb5SApple OSS Distributions 
697*1b191cb5SApple OSS Distributions 	return VOCALL(spec_vnodeop_p, VOFFSET(vnop_close), ap);
698*1b191cb5SApple OSS Distributions }
699*1b191cb5SApple OSS Distributions 
700*1b191cb5SApple OSS Distributions static boolean_t
devfs_update_needed(long now_s,long last_s)701*1b191cb5SApple OSS Distributions devfs_update_needed(long now_s, long last_s)
702*1b191cb5SApple OSS Distributions {
703*1b191cb5SApple OSS Distributions 	if (now_s > last_s) {
704*1b191cb5SApple OSS Distributions 		if (now_s - last_s >= DEVFS_LAZY_UPDATE_SECONDS) {
705*1b191cb5SApple OSS Distributions 			return TRUE;
706*1b191cb5SApple OSS Distributions 		}
707*1b191cb5SApple OSS Distributions 	}
708*1b191cb5SApple OSS Distributions 
709*1b191cb5SApple OSS Distributions 	return FALSE;
710*1b191cb5SApple OSS Distributions }
711*1b191cb5SApple OSS Distributions 
712*1b191cb5SApple OSS Distributions /*
713*1b191cb5SApple OSS Distributions  * Given a set of time updates required [to happen at some point], check
714*1b191cb5SApple OSS Distributions  * either make those changes (and resolve other pending updates) or mark
715*1b191cb5SApple OSS Distributions  * the devnode for a subsequent update.
716*1b191cb5SApple OSS Distributions  */
717*1b191cb5SApple OSS Distributions static void
devfs_consider_time_update(devnode_t * dnp,uint32_t just_changed_flags)718*1b191cb5SApple OSS Distributions devfs_consider_time_update(devnode_t *dnp, uint32_t just_changed_flags)
719*1b191cb5SApple OSS Distributions {
720*1b191cb5SApple OSS Distributions 	struct timeval          now;
721*1b191cb5SApple OSS Distributions 	long now_s;
722*1b191cb5SApple OSS Distributions 
723*1b191cb5SApple OSS Distributions 	microtime(&now);
724*1b191cb5SApple OSS Distributions 	now_s = now.tv_sec;
725*1b191cb5SApple OSS Distributions 
726*1b191cb5SApple OSS Distributions 	if (dnp->dn_change || (just_changed_flags & DEVFS_UPDATE_CHANGE)) {
727*1b191cb5SApple OSS Distributions 		if (devfs_update_needed(now_s, dnp->dn_ctime.tv_sec)) {
728*1b191cb5SApple OSS Distributions 			dn_times_now(dnp, just_changed_flags);
729*1b191cb5SApple OSS Distributions 			return;
730*1b191cb5SApple OSS Distributions 		}
731*1b191cb5SApple OSS Distributions 	}
732*1b191cb5SApple OSS Distributions 	if (dnp->dn_access || (just_changed_flags & DEVFS_UPDATE_ACCESS)) {
733*1b191cb5SApple OSS Distributions 		if (devfs_update_needed(now_s, dnp->dn_atime.tv_sec)) {
734*1b191cb5SApple OSS Distributions 			dn_times_now(dnp, just_changed_flags);
735*1b191cb5SApple OSS Distributions 			return;
736*1b191cb5SApple OSS Distributions 		}
737*1b191cb5SApple OSS Distributions 	}
738*1b191cb5SApple OSS Distributions 	if (dnp->dn_update || (just_changed_flags & DEVFS_UPDATE_MOD)) {
739*1b191cb5SApple OSS Distributions 		if (devfs_update_needed(now_s, dnp->dn_mtime.tv_sec)) {
740*1b191cb5SApple OSS Distributions 			dn_times_now(dnp, just_changed_flags);
741*1b191cb5SApple OSS Distributions 			return;
742*1b191cb5SApple OSS Distributions 		}
743*1b191cb5SApple OSS Distributions 	}
744*1b191cb5SApple OSS Distributions 
745*1b191cb5SApple OSS Distributions 	/* Not going to do anything now--mark for later update */
746*1b191cb5SApple OSS Distributions 	dn_mark_for_delayed_times_update(dnp, just_changed_flags);
747*1b191cb5SApple OSS Distributions 
748*1b191cb5SApple OSS Distributions 	return;
749*1b191cb5SApple OSS Distributions }
750*1b191cb5SApple OSS Distributions 
751*1b191cb5SApple OSS Distributions static int
devfsspec_read(struct vnop_read_args * ap)752*1b191cb5SApple OSS Distributions devfsspec_read(struct vnop_read_args *ap)
753*1b191cb5SApple OSS Distributions /* struct vnop_read_args {
754*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
755*1b191cb5SApple OSS Distributions  *       struct uio *a_uio;
756*1b191cb5SApple OSS Distributions  *       int  a_ioflag;
757*1b191cb5SApple OSS Distributions  *       kauth_cred_t a_cred;
758*1b191cb5SApple OSS Distributions  *  } */
759*1b191cb5SApple OSS Distributions {
760*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = VTODN(ap->a_vp);
761*1b191cb5SApple OSS Distributions 
762*1b191cb5SApple OSS Distributions 	devfs_consider_time_update(dnp, DEVFS_UPDATE_ACCESS);
763*1b191cb5SApple OSS Distributions 
764*1b191cb5SApple OSS Distributions 	return VOCALL(spec_vnodeop_p, VOFFSET(vnop_read), ap);
765*1b191cb5SApple OSS Distributions }
766*1b191cb5SApple OSS Distributions 
767*1b191cb5SApple OSS Distributions static int
devfsspec_write(struct vnop_write_args * ap)768*1b191cb5SApple OSS Distributions devfsspec_write(struct vnop_write_args *ap)
769*1b191cb5SApple OSS Distributions /* struct vnop_write_args  {
770*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
771*1b191cb5SApple OSS Distributions  *       struct uio *a_uio;
772*1b191cb5SApple OSS Distributions  *       int  a_ioflag;
773*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
774*1b191cb5SApple OSS Distributions  *  } */
775*1b191cb5SApple OSS Distributions {
776*1b191cb5SApple OSS Distributions 	devnode_t *     dnp = VTODN(ap->a_vp);
777*1b191cb5SApple OSS Distributions 
778*1b191cb5SApple OSS Distributions 	devfs_consider_time_update(dnp, DEVFS_UPDATE_CHANGE | DEVFS_UPDATE_MOD);
779*1b191cb5SApple OSS Distributions 
780*1b191cb5SApple OSS Distributions 	return VOCALL(spec_vnodeop_p, VOFFSET(vnop_write), ap);
781*1b191cb5SApple OSS Distributions }
782*1b191cb5SApple OSS Distributions 
783*1b191cb5SApple OSS Distributions /*
784*1b191cb5SApple OSS Distributions  *  Write data to a file or directory.
785*1b191cb5SApple OSS Distributions  */
786*1b191cb5SApple OSS Distributions static int
devfs_write(struct vnop_write_args * ap)787*1b191cb5SApple OSS Distributions devfs_write(struct vnop_write_args *ap)
788*1b191cb5SApple OSS Distributions /* struct vnop_write_args  {
789*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
790*1b191cb5SApple OSS Distributions  *       struct uio *a_uio;
791*1b191cb5SApple OSS Distributions  *       int  a_ioflag;
792*1b191cb5SApple OSS Distributions  *       kauth_cred_t a_cred;
793*1b191cb5SApple OSS Distributions  *  } */
794*1b191cb5SApple OSS Distributions {
795*1b191cb5SApple OSS Distributions 	switch (ap->a_vp->v_type) {
796*1b191cb5SApple OSS Distributions 	case VDIR:
797*1b191cb5SApple OSS Distributions 		return EISDIR;
798*1b191cb5SApple OSS Distributions 	default:
799*1b191cb5SApple OSS Distributions 		printf("devfs_write(): bad file type %d", ap->a_vp->v_type);
800*1b191cb5SApple OSS Distributions 		return EINVAL;
801*1b191cb5SApple OSS Distributions 	}
802*1b191cb5SApple OSS Distributions }
803*1b191cb5SApple OSS Distributions 
804*1b191cb5SApple OSS Distributions /*
805*1b191cb5SApple OSS Distributions  * Deviates from UFS naming convention because there is a KPI function
806*1b191cb5SApple OSS Distributions  * called devfs_remove().
807*1b191cb5SApple OSS Distributions  */
808*1b191cb5SApple OSS Distributions static int
devfs_vnop_remove(struct vnop_remove_args * ap)809*1b191cb5SApple OSS Distributions devfs_vnop_remove(struct vnop_remove_args *ap)
810*1b191cb5SApple OSS Distributions /* struct vnop_remove_args  {
811*1b191cb5SApple OSS Distributions  *       struct vnode *a_dvp;
812*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
813*1b191cb5SApple OSS Distributions  *       struct componentname *a_cnp;
814*1b191cb5SApple OSS Distributions  *  } */
815*1b191cb5SApple OSS Distributions {
816*1b191cb5SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
817*1b191cb5SApple OSS Distributions 	struct vnode *dvp = ap->a_dvp;
818*1b191cb5SApple OSS Distributions 	struct componentname *cnp = ap->a_cnp;
819*1b191cb5SApple OSS Distributions 	devnode_t *  tp;
820*1b191cb5SApple OSS Distributions 	devnode_t *  tdp;
821*1b191cb5SApple OSS Distributions 	devdirent_t * tnp;
822*1b191cb5SApple OSS Distributions 	int doingdirectory = 0;
823*1b191cb5SApple OSS Distributions 	int error = 0;
824*1b191cb5SApple OSS Distributions 
825*1b191cb5SApple OSS Distributions 	/*
826*1b191cb5SApple OSS Distributions 	 * assume that the name is null terminated as they
827*1b191cb5SApple OSS Distributions 	 * are the end of the path. Get pointers to all our
828*1b191cb5SApple OSS Distributions 	 * devfs structures.
829*1b191cb5SApple OSS Distributions 	 */
830*1b191cb5SApple OSS Distributions 
831*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
832*1b191cb5SApple OSS Distributions 
833*1b191cb5SApple OSS Distributions 	tp = VTODN(vp);
834*1b191cb5SApple OSS Distributions 	tdp = VTODN(dvp);
835*1b191cb5SApple OSS Distributions 
836*1b191cb5SApple OSS Distributions 
837*1b191cb5SApple OSS Distributions 	tnp = dev_findname(tdp, cnp->cn_nameptr);
838*1b191cb5SApple OSS Distributions 
839*1b191cb5SApple OSS Distributions 	if (tnp == NULL) {
840*1b191cb5SApple OSS Distributions 		error = ENOENT;
841*1b191cb5SApple OSS Distributions 		goto abort;
842*1b191cb5SApple OSS Distributions 	}
843*1b191cb5SApple OSS Distributions 
844*1b191cb5SApple OSS Distributions 	/*
845*1b191cb5SApple OSS Distributions 	 * Don't allow removing critical devfs devices
846*1b191cb5SApple OSS Distributions 	 */
847*1b191cb5SApple OSS Distributions 	if (devfs_is_name_protected(dvp, cnp->cn_nameptr)) {
848*1b191cb5SApple OSS Distributions 		error = EINVAL;
849*1b191cb5SApple OSS Distributions 		goto abort;
850*1b191cb5SApple OSS Distributions 	}
851*1b191cb5SApple OSS Distributions 
852*1b191cb5SApple OSS Distributions 	/*
853*1b191cb5SApple OSS Distributions 	 * Make sure that we don't try do something stupid
854*1b191cb5SApple OSS Distributions 	 */
855*1b191cb5SApple OSS Distributions 	if ((tp->dn_type) == DEV_DIR) {
856*1b191cb5SApple OSS Distributions 		/*
857*1b191cb5SApple OSS Distributions 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
858*1b191cb5SApple OSS Distributions 		 */
859*1b191cb5SApple OSS Distributions 		if ((cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.')
860*1b191cb5SApple OSS Distributions 		    || (cnp->cn_flags & ISDOTDOT)) {
861*1b191cb5SApple OSS Distributions 			error = EINVAL;
862*1b191cb5SApple OSS Distributions 			goto abort;
863*1b191cb5SApple OSS Distributions 		}
864*1b191cb5SApple OSS Distributions 		doingdirectory++;
865*1b191cb5SApple OSS Distributions 	}
866*1b191cb5SApple OSS Distributions 
867*1b191cb5SApple OSS Distributions 	/***********************************
868*1b191cb5SApple OSS Distributions 	* Start actually doing things.... *
869*1b191cb5SApple OSS Distributions 	***********************************/
870*1b191cb5SApple OSS Distributions 	devfs_consider_time_update(tdp, DEVFS_UPDATE_CHANGE | DEVFS_UPDATE_MOD);
871*1b191cb5SApple OSS Distributions 
872*1b191cb5SApple OSS Distributions 	/*
873*1b191cb5SApple OSS Distributions 	 * Target must be empty if a directory and have no links
874*1b191cb5SApple OSS Distributions 	 * to it. Also, ensure source and target are compatible
875*1b191cb5SApple OSS Distributions 	 * (both directories, or both not directories).
876*1b191cb5SApple OSS Distributions 	 */
877*1b191cb5SApple OSS Distributions 	if ((doingdirectory) && (tp->dn_links > 2)) {
878*1b191cb5SApple OSS Distributions 		error = ENOTEMPTY;
879*1b191cb5SApple OSS Distributions 		goto abort;
880*1b191cb5SApple OSS Distributions 	}
881*1b191cb5SApple OSS Distributions 	dev_free_name(tnp);
882*1b191cb5SApple OSS Distributions abort:
883*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
884*1b191cb5SApple OSS Distributions 
885*1b191cb5SApple OSS Distributions 	return error;
886*1b191cb5SApple OSS Distributions }
887*1b191cb5SApple OSS Distributions 
888*1b191cb5SApple OSS Distributions /*
889*1b191cb5SApple OSS Distributions  */
890*1b191cb5SApple OSS Distributions static int
devfs_link(struct vnop_link_args * ap)891*1b191cb5SApple OSS Distributions devfs_link(struct vnop_link_args *ap)
892*1b191cb5SApple OSS Distributions /*struct vnop_link_args  {
893*1b191cb5SApple OSS Distributions  *       struct vnode *a_tdvp;
894*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
895*1b191cb5SApple OSS Distributions  *       struct componentname *a_cnp;
896*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
897*1b191cb5SApple OSS Distributions  *  } */
898*1b191cb5SApple OSS Distributions {
899*1b191cb5SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
900*1b191cb5SApple OSS Distributions 	struct vnode *tdvp = ap->a_tdvp;
901*1b191cb5SApple OSS Distributions 	struct componentname *cnp = ap->a_cnp;
902*1b191cb5SApple OSS Distributions 	devnode_t * fp;
903*1b191cb5SApple OSS Distributions 	devnode_t * tdp;
904*1b191cb5SApple OSS Distributions 	devdirent_t * tnp;
905*1b191cb5SApple OSS Distributions 	int error = 0;
906*1b191cb5SApple OSS Distributions 
907*1b191cb5SApple OSS Distributions 	/*
908*1b191cb5SApple OSS Distributions 	 * First catch an arbitrary restriction for this FS
909*1b191cb5SApple OSS Distributions 	 */
910*1b191cb5SApple OSS Distributions 	if (cnp->cn_namelen > DEVMAXNAMESIZE) {
911*1b191cb5SApple OSS Distributions 		error = ENAMETOOLONG;
912*1b191cb5SApple OSS Distributions 		goto out1;
913*1b191cb5SApple OSS Distributions 	}
914*1b191cb5SApple OSS Distributions 
915*1b191cb5SApple OSS Distributions 	/*
916*1b191cb5SApple OSS Distributions 	 * Lock our directories and get our name pointers
917*1b191cb5SApple OSS Distributions 	 * assume that the names are null terminated as they
918*1b191cb5SApple OSS Distributions 	 * are the end of the path. Get pointers to all our
919*1b191cb5SApple OSS Distributions 	 * devfs structures.
920*1b191cb5SApple OSS Distributions 	 */
921*1b191cb5SApple OSS Distributions 	/* can lookup dnode safely for tdvp outside of devfs lock as it is not aliased */
922*1b191cb5SApple OSS Distributions 	tdp = VTODN(tdvp);
923*1b191cb5SApple OSS Distributions 
924*1b191cb5SApple OSS Distributions 	if (tdvp->v_mount != vp->v_mount) {
925*1b191cb5SApple OSS Distributions 		return EXDEV;
926*1b191cb5SApple OSS Distributions 	}
927*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
928*1b191cb5SApple OSS Distributions 
929*1b191cb5SApple OSS Distributions 	fp = VTODN(vp);
930*1b191cb5SApple OSS Distributions 
931*1b191cb5SApple OSS Distributions 	/***********************************
932*1b191cb5SApple OSS Distributions 	* Start actually doing things.... *
933*1b191cb5SApple OSS Distributions 	***********************************/
934*1b191cb5SApple OSS Distributions 	dn_times_now(fp, DEVFS_UPDATE_CHANGE);
935*1b191cb5SApple OSS Distributions 
936*1b191cb5SApple OSS Distributions 	if (!error) {
937*1b191cb5SApple OSS Distributions 		error = dev_add_name(cnp->cn_nameptr, tdp, NULL, fp, &tnp);
938*1b191cb5SApple OSS Distributions 	}
939*1b191cb5SApple OSS Distributions out1:
940*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
941*1b191cb5SApple OSS Distributions 
942*1b191cb5SApple OSS Distributions 	return error;
943*1b191cb5SApple OSS Distributions }
944*1b191cb5SApple OSS Distributions 
945*1b191cb5SApple OSS Distributions /*
946*1b191cb5SApple OSS Distributions  * Rename system call. Seems overly complicated to me...
947*1b191cb5SApple OSS Distributions  *      rename("foo", "bar");
948*1b191cb5SApple OSS Distributions  * is essentially
949*1b191cb5SApple OSS Distributions  *	unlink("bar");
950*1b191cb5SApple OSS Distributions  *	link("foo", "bar");
951*1b191cb5SApple OSS Distributions  *	unlink("foo");
952*1b191cb5SApple OSS Distributions  * but ``atomically''.
953*1b191cb5SApple OSS Distributions  *
954*1b191cb5SApple OSS Distributions  * When the target exists, both the directory
955*1b191cb5SApple OSS Distributions  * and target vnodes are locked.
956*1b191cb5SApple OSS Distributions  * the source and source-parent vnodes are referenced
957*1b191cb5SApple OSS Distributions  *
958*1b191cb5SApple OSS Distributions  *
959*1b191cb5SApple OSS Distributions  * Basic algorithm is:
960*1b191cb5SApple OSS Distributions  *
961*1b191cb5SApple OSS Distributions  * 1) Bump link count on source while we're linking it to the
962*1b191cb5SApple OSS Distributions  *    target.  This also ensure the inode won't be deleted out
963*1b191cb5SApple OSS Distributions  *    from underneath us while we work (it may be truncated by
964*1b191cb5SApple OSS Distributions  *    a concurrent `trunc' or `open' for creation).
965*1b191cb5SApple OSS Distributions  * 2) Link source to destination.  If destination already exists,
966*1b191cb5SApple OSS Distributions  *    delete it first.
967*1b191cb5SApple OSS Distributions  * 3) Unlink source reference to node if still around. If a
968*1b191cb5SApple OSS Distributions  *    directory was moved and the parent of the destination
969*1b191cb5SApple OSS Distributions  *    is different from the source, patch the ".." entry in the
970*1b191cb5SApple OSS Distributions  *    directory.
971*1b191cb5SApple OSS Distributions  */
972*1b191cb5SApple OSS Distributions static int
devfs_rename(struct vnop_rename_args * ap)973*1b191cb5SApple OSS Distributions devfs_rename(struct vnop_rename_args *ap)
974*1b191cb5SApple OSS Distributions /*struct vnop_rename_args  {
975*1b191cb5SApple OSS Distributions  *       struct vnode *a_fdvp;
976*1b191cb5SApple OSS Distributions  *       struct vnode *a_fvp;
977*1b191cb5SApple OSS Distributions  *       struct componentname *a_fcnp;
978*1b191cb5SApple OSS Distributions  *       struct vnode *a_tdvp;
979*1b191cb5SApple OSS Distributions  *       struct vnode *a_tvp;
980*1b191cb5SApple OSS Distributions  *       struct componentname *a_tcnp;
981*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
982*1b191cb5SApple OSS Distributions  *  } */
983*1b191cb5SApple OSS Distributions {
984*1b191cb5SApple OSS Distributions 	struct vnode *tvp = ap->a_tvp;
985*1b191cb5SApple OSS Distributions 	struct vnode *tdvp = ap->a_tdvp;
986*1b191cb5SApple OSS Distributions 	struct vnode *fvp = ap->a_fvp;
987*1b191cb5SApple OSS Distributions 	struct vnode *fdvp = ap->a_fdvp;
988*1b191cb5SApple OSS Distributions 	struct componentname *tcnp = ap->a_tcnp;
989*1b191cb5SApple OSS Distributions 	struct componentname *fcnp = ap->a_fcnp;
990*1b191cb5SApple OSS Distributions 	devnode_t *fp, *fdp, *tp, *tdp;
991*1b191cb5SApple OSS Distributions 	devdirent_t *fnp, *tnp;
992*1b191cb5SApple OSS Distributions 	int doingdirectory = 0;
993*1b191cb5SApple OSS Distributions 	int error = 0;
994*1b191cb5SApple OSS Distributions 
995*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
996*1b191cb5SApple OSS Distributions 	/*
997*1b191cb5SApple OSS Distributions 	 * First catch an arbitrary restriction for this FS
998*1b191cb5SApple OSS Distributions 	 */
999*1b191cb5SApple OSS Distributions 	if (tcnp->cn_namelen > DEVMAXNAMESIZE) {
1000*1b191cb5SApple OSS Distributions 		error = ENAMETOOLONG;
1001*1b191cb5SApple OSS Distributions 		goto out;
1002*1b191cb5SApple OSS Distributions 	}
1003*1b191cb5SApple OSS Distributions 
1004*1b191cb5SApple OSS Distributions 	/*
1005*1b191cb5SApple OSS Distributions 	 * assume that the names are null terminated as they
1006*1b191cb5SApple OSS Distributions 	 * are the end of the path. Get pointers to all our
1007*1b191cb5SApple OSS Distributions 	 * devfs structures.
1008*1b191cb5SApple OSS Distributions 	 */
1009*1b191cb5SApple OSS Distributions 	tdp = VTODN(tdvp);
1010*1b191cb5SApple OSS Distributions 	fdp = VTODN(fdvp);
1011*1b191cb5SApple OSS Distributions 	fp = VTODN(fvp);
1012*1b191cb5SApple OSS Distributions 
1013*1b191cb5SApple OSS Distributions 	fnp = dev_findname(fdp, fcnp->cn_nameptr);
1014*1b191cb5SApple OSS Distributions 
1015*1b191cb5SApple OSS Distributions 	if (fnp == NULL) {
1016*1b191cb5SApple OSS Distributions 		error = ENOENT;
1017*1b191cb5SApple OSS Distributions 		goto out;
1018*1b191cb5SApple OSS Distributions 	}
1019*1b191cb5SApple OSS Distributions 	tp = NULL;
1020*1b191cb5SApple OSS Distributions 	tnp = NULL;
1021*1b191cb5SApple OSS Distributions 
1022*1b191cb5SApple OSS Distributions 	if (tvp) {
1023*1b191cb5SApple OSS Distributions 		tnp = dev_findname(tdp, tcnp->cn_nameptr);
1024*1b191cb5SApple OSS Distributions 
1025*1b191cb5SApple OSS Distributions 		if (tnp == NULL) {
1026*1b191cb5SApple OSS Distributions 			error = ENOENT;
1027*1b191cb5SApple OSS Distributions 			goto out;
1028*1b191cb5SApple OSS Distributions 		}
1029*1b191cb5SApple OSS Distributions 		tp = VTODN(tvp);
1030*1b191cb5SApple OSS Distributions 	}
1031*1b191cb5SApple OSS Distributions 
1032*1b191cb5SApple OSS Distributions 	/*
1033*1b191cb5SApple OSS Distributions 	 * Make sure that we don't try do something stupid
1034*1b191cb5SApple OSS Distributions 	 */
1035*1b191cb5SApple OSS Distributions 	if ((fp->dn_type) == DEV_DIR) {
1036*1b191cb5SApple OSS Distributions 		/*
1037*1b191cb5SApple OSS Distributions 		 * Avoid ".", "..", and aliases of "." for obvious reasons.
1038*1b191cb5SApple OSS Distributions 		 */
1039*1b191cb5SApple OSS Distributions 		if ((fcnp->cn_namelen == 1 && fcnp->cn_nameptr[0] == '.')
1040*1b191cb5SApple OSS Distributions 		    || (fcnp->cn_flags & ISDOTDOT)
1041*1b191cb5SApple OSS Distributions 		    || (tcnp->cn_namelen == 1 && tcnp->cn_nameptr[0] == '.')
1042*1b191cb5SApple OSS Distributions 		    || (tcnp->cn_flags & ISDOTDOT)
1043*1b191cb5SApple OSS Distributions 		    || (tdp == fp)) {
1044*1b191cb5SApple OSS Distributions 			error = EINVAL;
1045*1b191cb5SApple OSS Distributions 			goto out;
1046*1b191cb5SApple OSS Distributions 		}
1047*1b191cb5SApple OSS Distributions 		doingdirectory++;
1048*1b191cb5SApple OSS Distributions 	}
1049*1b191cb5SApple OSS Distributions 
1050*1b191cb5SApple OSS Distributions 	/*
1051*1b191cb5SApple OSS Distributions 	 * Don't allow renaming critical devfs devices
1052*1b191cb5SApple OSS Distributions 	 */
1053*1b191cb5SApple OSS Distributions 	if (devfs_is_name_protected(fdvp, fcnp->cn_nameptr) ||
1054*1b191cb5SApple OSS Distributions 	    devfs_is_name_protected(tdvp, tcnp->cn_nameptr)) {
1055*1b191cb5SApple OSS Distributions 		error = EINVAL;
1056*1b191cb5SApple OSS Distributions 		goto out;
1057*1b191cb5SApple OSS Distributions 	}
1058*1b191cb5SApple OSS Distributions 
1059*1b191cb5SApple OSS Distributions 	/*
1060*1b191cb5SApple OSS Distributions 	 * If ".." must be changed (ie the directory gets a new
1061*1b191cb5SApple OSS Distributions 	 * parent) then the source directory must not be in the
1062*1b191cb5SApple OSS Distributions 	 * directory hierarchy above the target, as this would
1063*1b191cb5SApple OSS Distributions 	 * orphan everything below the source directory. Also
1064*1b191cb5SApple OSS Distributions 	 * the user must have write permission in the source so
1065*1b191cb5SApple OSS Distributions 	 * as to be able to change "..".
1066*1b191cb5SApple OSS Distributions 	 */
1067*1b191cb5SApple OSS Distributions 	if (doingdirectory && (tdp != fdp)) {
1068*1b191cb5SApple OSS Distributions 		devnode_t * tmp, *ntmp;
1069*1b191cb5SApple OSS Distributions 		tmp = tdp;
1070*1b191cb5SApple OSS Distributions 		do {
1071*1b191cb5SApple OSS Distributions 			if (tmp == fp) {
1072*1b191cb5SApple OSS Distributions 				/* XXX unlock stuff here probably */
1073*1b191cb5SApple OSS Distributions 				error = EINVAL;
1074*1b191cb5SApple OSS Distributions 				goto out;
1075*1b191cb5SApple OSS Distributions 			}
1076*1b191cb5SApple OSS Distributions 			ntmp = tmp;
1077*1b191cb5SApple OSS Distributions 		} while ((tmp = tmp->dn_typeinfo.Dir.parent) != ntmp);
1078*1b191cb5SApple OSS Distributions 	}
1079*1b191cb5SApple OSS Distributions 
1080*1b191cb5SApple OSS Distributions 	/***********************************
1081*1b191cb5SApple OSS Distributions 	* Start actually doing things.... *
1082*1b191cb5SApple OSS Distributions 	***********************************/
1083*1b191cb5SApple OSS Distributions 	dn_times_now(fp, DEVFS_UPDATE_CHANGE);
1084*1b191cb5SApple OSS Distributions 
1085*1b191cb5SApple OSS Distributions 	/*
1086*1b191cb5SApple OSS Distributions 	 * Check if just deleting a link name.
1087*1b191cb5SApple OSS Distributions 	 */
1088*1b191cb5SApple OSS Distributions 	if (fvp == tvp) {
1089*1b191cb5SApple OSS Distributions 		if (fvp->v_type == VDIR) {
1090*1b191cb5SApple OSS Distributions 			error = EINVAL;
1091*1b191cb5SApple OSS Distributions 			goto out;
1092*1b191cb5SApple OSS Distributions 		}
1093*1b191cb5SApple OSS Distributions 		/* Release destination completely. */
1094*1b191cb5SApple OSS Distributions 		dev_free_name(fnp);
1095*1b191cb5SApple OSS Distributions 
1096*1b191cb5SApple OSS Distributions 		DEVFS_UNLOCK();
1097*1b191cb5SApple OSS Distributions 		return 0;
1098*1b191cb5SApple OSS Distributions 	}
1099*1b191cb5SApple OSS Distributions 	/*
1100*1b191cb5SApple OSS Distributions 	 * 1) Bump link count while we're moving stuff
1101*1b191cb5SApple OSS Distributions 	 *    around.  If we crash somewhere before
1102*1b191cb5SApple OSS Distributions 	 *    completing our work,  too bad :)
1103*1b191cb5SApple OSS Distributions 	 */
1104*1b191cb5SApple OSS Distributions 	fp->dn_links++;
1105*1b191cb5SApple OSS Distributions 	/*
1106*1b191cb5SApple OSS Distributions 	 * If the target exists zap it (unless it's a non-empty directory)
1107*1b191cb5SApple OSS Distributions 	 * We could do that as well but won't
1108*1b191cb5SApple OSS Distributions 	 */
1109*1b191cb5SApple OSS Distributions 	if (tp) {
1110*1b191cb5SApple OSS Distributions 		/*
1111*1b191cb5SApple OSS Distributions 		 * Target must be empty if a directory and have no links
1112*1b191cb5SApple OSS Distributions 		 * to it. Also, ensure source and target are compatible
1113*1b191cb5SApple OSS Distributions 		 * (both directories, or both not directories).
1114*1b191cb5SApple OSS Distributions 		 */
1115*1b191cb5SApple OSS Distributions 		if ((doingdirectory) && (tp->dn_links > 2)) {
1116*1b191cb5SApple OSS Distributions 			error = ENOTEMPTY;
1117*1b191cb5SApple OSS Distributions 			goto bad;
1118*1b191cb5SApple OSS Distributions 		}
1119*1b191cb5SApple OSS Distributions 		dev_free_name(tnp);
1120*1b191cb5SApple OSS Distributions 		tp = NULL;
1121*1b191cb5SApple OSS Distributions 	}
1122*1b191cb5SApple OSS Distributions 	dev_add_name(tcnp->cn_nameptr, tdp, NULL, fp, &tnp);
1123*1b191cb5SApple OSS Distributions 	fnp->de_dnp = NULL;
1124*1b191cb5SApple OSS Distributions 	fp->dn_links--; /* one less link to it.. */
1125*1b191cb5SApple OSS Distributions 
1126*1b191cb5SApple OSS Distributions 	dev_free_name(fnp);
1127*1b191cb5SApple OSS Distributions bad:
1128*1b191cb5SApple OSS Distributions 	fp->dn_links--; /* we added one earlier*/
1129*1b191cb5SApple OSS Distributions out:
1130*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1131*1b191cb5SApple OSS Distributions 	return error;
1132*1b191cb5SApple OSS Distributions }
1133*1b191cb5SApple OSS Distributions 
1134*1b191cb5SApple OSS Distributions static int
devfs_mkdir(struct vnop_mkdir_args * ap)1135*1b191cb5SApple OSS Distributions devfs_mkdir(struct vnop_mkdir_args *ap)
1136*1b191cb5SApple OSS Distributions /*struct vnop_mkdir_args {
1137*1b191cb5SApple OSS Distributions  *       struct vnode *a_dvp;
1138*1b191cb5SApple OSS Distributions  *       struct vnode **a_vpp;
1139*1b191cb5SApple OSS Distributions  *       struct componentname *a_cnp;
1140*1b191cb5SApple OSS Distributions  *       struct vnode_attr *a_vap;
1141*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
1142*1b191cb5SApple OSS Distributions  *  } */
1143*1b191cb5SApple OSS Distributions {
1144*1b191cb5SApple OSS Distributions 	struct componentname * cnp = ap->a_cnp;
1145*1b191cb5SApple OSS Distributions 	vfs_context_t ctx = cnp->cn_context;
1146*1b191cb5SApple OSS Distributions 	struct proc *p = vfs_context_proc(ctx);
1147*1b191cb5SApple OSS Distributions 	int error = 0;
1148*1b191cb5SApple OSS Distributions 	devnode_t * dir_p;
1149*1b191cb5SApple OSS Distributions 	devdirent_t * nm_p;
1150*1b191cb5SApple OSS Distributions 	devnode_t * dev_p;
1151*1b191cb5SApple OSS Distributions 	struct vnode_attr *     vap = ap->a_vap;
1152*1b191cb5SApple OSS Distributions 	struct vnode * * vpp = ap->a_vpp;
1153*1b191cb5SApple OSS Distributions 
1154*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1155*1b191cb5SApple OSS Distributions 
1156*1b191cb5SApple OSS Distributions 	dir_p = VTODN(ap->a_dvp);
1157*1b191cb5SApple OSS Distributions 	error = dev_add_entry(cnp->cn_nameptr, dir_p, DEV_DIR,
1158*1b191cb5SApple OSS Distributions 	    NULL, NULL, NULL, &nm_p);
1159*1b191cb5SApple OSS Distributions 	if (error) {
1160*1b191cb5SApple OSS Distributions 		goto failure;
1161*1b191cb5SApple OSS Distributions 	}
1162*1b191cb5SApple OSS Distributions 	dev_p = nm_p->de_dnp;
1163*1b191cb5SApple OSS Distributions 	dev_p->dn_uid = dir_p->dn_uid;
1164*1b191cb5SApple OSS Distributions 	dev_p->dn_gid = dir_p->dn_gid;
1165*1b191cb5SApple OSS Distributions 	dev_p->dn_mode = vap->va_mode;
1166*1b191cb5SApple OSS Distributions 	dn_copy_times(dev_p, dir_p);
1167*1b191cb5SApple OSS Distributions 
1168*1b191cb5SApple OSS Distributions 	error = devfs_dntovn(dev_p, vpp, p);
1169*1b191cb5SApple OSS Distributions failure:
1170*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1171*1b191cb5SApple OSS Distributions 
1172*1b191cb5SApple OSS Distributions 	return error;
1173*1b191cb5SApple OSS Distributions }
1174*1b191cb5SApple OSS Distributions 
1175*1b191cb5SApple OSS Distributions /*
1176*1b191cb5SApple OSS Distributions  * An rmdir is a special type of remove, which we already support; we wrap
1177*1b191cb5SApple OSS Distributions  * and reexpress the arguments to call devfs_remove directly.  The only
1178*1b191cb5SApple OSS Distributions  * different argument is flags, which we do not set, since it's ignored.
1179*1b191cb5SApple OSS Distributions  */
1180*1b191cb5SApple OSS Distributions static int
devfs_rmdir(struct vnop_rmdir_args * ap)1181*1b191cb5SApple OSS Distributions devfs_rmdir(struct vnop_rmdir_args *ap)
1182*1b191cb5SApple OSS Distributions /* struct vnop_rmdir_args {
1183*1b191cb5SApple OSS Distributions  *       struct vnode *a_dvp;
1184*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
1185*1b191cb5SApple OSS Distributions  *       struct componentname *a_cnp;
1186*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
1187*1b191cb5SApple OSS Distributions  *  } */
1188*1b191cb5SApple OSS Distributions {
1189*1b191cb5SApple OSS Distributions 	struct vnop_remove_args ra;
1190*1b191cb5SApple OSS Distributions 
1191*1b191cb5SApple OSS Distributions 	ra.a_dvp = ap->a_dvp;
1192*1b191cb5SApple OSS Distributions 	ra.a_vp = ap->a_vp;
1193*1b191cb5SApple OSS Distributions 	ra.a_cnp = ap->a_cnp;
1194*1b191cb5SApple OSS Distributions 	ra.a_flags = 0;         /* XXX */
1195*1b191cb5SApple OSS Distributions 	ra.a_context = ap->a_context;
1196*1b191cb5SApple OSS Distributions 
1197*1b191cb5SApple OSS Distributions 	return devfs_vnop_remove(&ra);
1198*1b191cb5SApple OSS Distributions }
1199*1b191cb5SApple OSS Distributions 
1200*1b191cb5SApple OSS Distributions 
1201*1b191cb5SApple OSS Distributions static int
devfs_symlink(struct vnop_symlink_args * ap)1202*1b191cb5SApple OSS Distributions devfs_symlink(struct vnop_symlink_args *ap)
1203*1b191cb5SApple OSS Distributions /*struct vnop_symlink_args {
1204*1b191cb5SApple OSS Distributions  *       struct vnode *a_dvp;
1205*1b191cb5SApple OSS Distributions  *       struct vnode **a_vpp;
1206*1b191cb5SApple OSS Distributions  *       struct componentname *a_cnp;
1207*1b191cb5SApple OSS Distributions  *       struct vnode_attr *a_vap;
1208*1b191cb5SApple OSS Distributions  *       char *a_target;
1209*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
1210*1b191cb5SApple OSS Distributions  *  } */
1211*1b191cb5SApple OSS Distributions {
1212*1b191cb5SApple OSS Distributions 	int error;
1213*1b191cb5SApple OSS Distributions 	devdirent_t *newent;
1214*1b191cb5SApple OSS Distributions 
1215*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1216*1b191cb5SApple OSS Distributions 	error = devfs_make_symlink(VTODN(ap->a_dvp), ap->a_cnp->cn_nameptr, ap->a_vap->va_mode, ap->a_target, &newent);
1217*1b191cb5SApple OSS Distributions 
1218*1b191cb5SApple OSS Distributions 	if (error == 0) {
1219*1b191cb5SApple OSS Distributions 		error = devfs_dntovn(newent->de_dnp, ap->a_vpp, vfs_context_proc(ap->a_context));
1220*1b191cb5SApple OSS Distributions 	}
1221*1b191cb5SApple OSS Distributions 
1222*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1223*1b191cb5SApple OSS Distributions 
1224*1b191cb5SApple OSS Distributions 	return error;
1225*1b191cb5SApple OSS Distributions }
1226*1b191cb5SApple OSS Distributions 
1227*1b191cb5SApple OSS Distributions /* Called with devfs locked */
1228*1b191cb5SApple OSS Distributions int
devfs_make_symlink(devnode_t * dir_p,char * name,mode_t mode,char * target,devdirent_t ** newent)1229*1b191cb5SApple OSS Distributions devfs_make_symlink(devnode_t *dir_p, char *name, mode_t mode, char *target, devdirent_t **newent)
1230*1b191cb5SApple OSS Distributions {
1231*1b191cb5SApple OSS Distributions 	int error = 0;
1232*1b191cb5SApple OSS Distributions 	devnode_type_t typeinfo;
1233*1b191cb5SApple OSS Distributions 	devdirent_t * nm_p;
1234*1b191cb5SApple OSS Distributions 	devnode_t * dev_p;
1235*1b191cb5SApple OSS Distributions 
1236*1b191cb5SApple OSS Distributions 	typeinfo.Slnk.name = target;
1237*1b191cb5SApple OSS Distributions 	typeinfo.Slnk.namelen = strlen(target);
1238*1b191cb5SApple OSS Distributions 
1239*1b191cb5SApple OSS Distributions 	error = dev_add_entry(name, dir_p, DEV_SLNK,
1240*1b191cb5SApple OSS Distributions 	    &typeinfo, NULL, NULL, &nm_p);
1241*1b191cb5SApple OSS Distributions 	if (error) {
1242*1b191cb5SApple OSS Distributions 		goto failure;
1243*1b191cb5SApple OSS Distributions 	}
1244*1b191cb5SApple OSS Distributions 	dev_p = nm_p->de_dnp;
1245*1b191cb5SApple OSS Distributions 	dev_p->dn_uid = dir_p->dn_uid;
1246*1b191cb5SApple OSS Distributions 	dev_p->dn_gid = dir_p->dn_gid;
1247*1b191cb5SApple OSS Distributions 	dev_p->dn_mode = mode;
1248*1b191cb5SApple OSS Distributions 	dn_copy_times(dev_p, dir_p);
1249*1b191cb5SApple OSS Distributions 
1250*1b191cb5SApple OSS Distributions 	if (newent) {
1251*1b191cb5SApple OSS Distributions 		*newent = nm_p;
1252*1b191cb5SApple OSS Distributions 	}
1253*1b191cb5SApple OSS Distributions 
1254*1b191cb5SApple OSS Distributions failure:
1255*1b191cb5SApple OSS Distributions 
1256*1b191cb5SApple OSS Distributions 	return error;
1257*1b191cb5SApple OSS Distributions }
1258*1b191cb5SApple OSS Distributions 
1259*1b191cb5SApple OSS Distributions /*
1260*1b191cb5SApple OSS Distributions  * Mknod vnode call
1261*1b191cb5SApple OSS Distributions  */
1262*1b191cb5SApple OSS Distributions static int
devfs_mknod(struct vnop_mknod_args * ap)1263*1b191cb5SApple OSS Distributions devfs_mknod(struct vnop_mknod_args *ap)
1264*1b191cb5SApple OSS Distributions /* struct vnop_mknod_args {
1265*1b191cb5SApple OSS Distributions  *       struct vnode *a_dvp;
1266*1b191cb5SApple OSS Distributions  *       struct vnode **a_vpp;
1267*1b191cb5SApple OSS Distributions  *       struct componentname *a_cnp;
1268*1b191cb5SApple OSS Distributions  *       struct vnode_attr *a_vap;
1269*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
1270*1b191cb5SApple OSS Distributions  *  } */
1271*1b191cb5SApple OSS Distributions {
1272*1b191cb5SApple OSS Distributions 	struct componentname * cnp = ap->a_cnp;
1273*1b191cb5SApple OSS Distributions 	vfs_context_t ctx = cnp->cn_context;
1274*1b191cb5SApple OSS Distributions 	struct proc *p = vfs_context_proc(ctx);
1275*1b191cb5SApple OSS Distributions 	devnode_t *     dev_p;
1276*1b191cb5SApple OSS Distributions 	devdirent_t *   devent;
1277*1b191cb5SApple OSS Distributions 	devnode_t *     dir_p;  /* devnode for parent directory */
1278*1b191cb5SApple OSS Distributions 	struct vnode *  dvp = ap->a_dvp;
1279*1b191cb5SApple OSS Distributions 	int             error = 0;
1280*1b191cb5SApple OSS Distributions 	devnode_type_t  typeinfo;
1281*1b191cb5SApple OSS Distributions 	struct vnode_attr *     vap = ap->a_vap;
1282*1b191cb5SApple OSS Distributions 	struct vnode ** vpp = ap->a_vpp;
1283*1b191cb5SApple OSS Distributions 
1284*1b191cb5SApple OSS Distributions 	*vpp = NULL;
1285*1b191cb5SApple OSS Distributions 	if (!(vap->va_type == VBLK) && !(vap->va_type == VCHR)) {
1286*1b191cb5SApple OSS Distributions 		return EINVAL; /* only support mknod of special files */
1287*1b191cb5SApple OSS Distributions 	}
1288*1b191cb5SApple OSS Distributions 	typeinfo.dev = vap->va_rdev;
1289*1b191cb5SApple OSS Distributions 
1290*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1291*1b191cb5SApple OSS Distributions 
1292*1b191cb5SApple OSS Distributions 	dir_p = VTODN(dvp);
1293*1b191cb5SApple OSS Distributions 
1294*1b191cb5SApple OSS Distributions 	error = dev_add_entry(cnp->cn_nameptr, dir_p,
1295*1b191cb5SApple OSS Distributions 	    (vap->va_type == VBLK) ? DEV_BDEV : DEV_CDEV,
1296*1b191cb5SApple OSS Distributions 	    &typeinfo, NULL, NULL, &devent);
1297*1b191cb5SApple OSS Distributions 	if (error) {
1298*1b191cb5SApple OSS Distributions 		goto failure;
1299*1b191cb5SApple OSS Distributions 	}
1300*1b191cb5SApple OSS Distributions 	dev_p = devent->de_dnp;
1301*1b191cb5SApple OSS Distributions 	error = devfs_dntovn(dev_p, vpp, p);
1302*1b191cb5SApple OSS Distributions 	if (error) {
1303*1b191cb5SApple OSS Distributions 		goto failure;
1304*1b191cb5SApple OSS Distributions 	}
1305*1b191cb5SApple OSS Distributions 	dev_p->dn_uid = vap->va_uid;
1306*1b191cb5SApple OSS Distributions 	dev_p->dn_gid = vap->va_gid;
1307*1b191cb5SApple OSS Distributions 	dev_p->dn_mode = vap->va_mode;
1308*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_uid);
1309*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_gid);
1310*1b191cb5SApple OSS Distributions 	VATTR_SET_SUPPORTED(vap, va_mode);
1311*1b191cb5SApple OSS Distributions failure:
1312*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1313*1b191cb5SApple OSS Distributions 
1314*1b191cb5SApple OSS Distributions 	return error;
1315*1b191cb5SApple OSS Distributions }
1316*1b191cb5SApple OSS Distributions 
1317*1b191cb5SApple OSS Distributions /*
1318*1b191cb5SApple OSS Distributions  * Vnode op for readdir
1319*1b191cb5SApple OSS Distributions  */
1320*1b191cb5SApple OSS Distributions static int
devfs_readdir(struct vnop_readdir_args * ap)1321*1b191cb5SApple OSS Distributions devfs_readdir(struct vnop_readdir_args *ap)
1322*1b191cb5SApple OSS Distributions /*struct vnop_readdir_args {
1323*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
1324*1b191cb5SApple OSS Distributions  *       struct uio *a_uio;
1325*1b191cb5SApple OSS Distributions  *       int a_flags;
1326*1b191cb5SApple OSS Distributions  *       int *a_eofflag;
1327*1b191cb5SApple OSS Distributions  *       int *a_numdirent;
1328*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
1329*1b191cb5SApple OSS Distributions  *  } */
1330*1b191cb5SApple OSS Distributions {
1331*1b191cb5SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
1332*1b191cb5SApple OSS Distributions 	struct uio *uio = ap->a_uio;
1333*1b191cb5SApple OSS Distributions 	struct dirent dirent;
1334*1b191cb5SApple OSS Distributions 	devnode_t * dir_node;
1335*1b191cb5SApple OSS Distributions 	devdirent_t *   name_node;
1336*1b191cb5SApple OSS Distributions 	const char *name;
1337*1b191cb5SApple OSS Distributions 	int error = 0;
1338*1b191cb5SApple OSS Distributions 	int reclen;
1339*1b191cb5SApple OSS Distributions 	int nodenumber;
1340*1b191cb5SApple OSS Distributions 	off_t startpos, pos;
1341*1b191cb5SApple OSS Distributions 
1342*1b191cb5SApple OSS Distributions 	if (ap->a_flags & (VNODE_READDIR_EXTENDED | VNODE_READDIR_REQSEEKOFF)) {
1343*1b191cb5SApple OSS Distributions 		return EINVAL;
1344*1b191cb5SApple OSS Distributions 	}
1345*1b191cb5SApple OSS Distributions 
1346*1b191cb5SApple OSS Distributions 	/*  set up refs to dir */
1347*1b191cb5SApple OSS Distributions 	dir_node = VTODN(vp);
1348*1b191cb5SApple OSS Distributions 	if (dir_node->dn_type != DEV_DIR) {
1349*1b191cb5SApple OSS Distributions 		return ENOTDIR;
1350*1b191cb5SApple OSS Distributions 	}
1351*1b191cb5SApple OSS Distributions 	pos = 0;
1352*1b191cb5SApple OSS Distributions 	startpos = uio->uio_offset;
1353*1b191cb5SApple OSS Distributions 
1354*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1355*1b191cb5SApple OSS Distributions 
1356*1b191cb5SApple OSS Distributions 	name_node = dir_node->dn_typeinfo.Dir.dirlist;
1357*1b191cb5SApple OSS Distributions 	nodenumber = 0;
1358*1b191cb5SApple OSS Distributions 
1359*1b191cb5SApple OSS Distributions 	while ((name_node || (nodenumber < 2)) && (uio_resid(uio) > 0)) {
1360*1b191cb5SApple OSS Distributions 		switch (nodenumber) {
1361*1b191cb5SApple OSS Distributions 		case    0:
1362*1b191cb5SApple OSS Distributions 			dirent.d_fileno = dir_node->dn_ino;
1363*1b191cb5SApple OSS Distributions 			name = ".";
1364*1b191cb5SApple OSS Distributions 			dirent.d_namlen = 1;
1365*1b191cb5SApple OSS Distributions 			dirent.d_type = DT_DIR;
1366*1b191cb5SApple OSS Distributions 			break;
1367*1b191cb5SApple OSS Distributions 		case    1:
1368*1b191cb5SApple OSS Distributions 			if (dir_node->dn_typeinfo.Dir.parent) {
1369*1b191cb5SApple OSS Distributions 				dirent.d_fileno = dir_node->dn_typeinfo.Dir.parent->dn_ino;
1370*1b191cb5SApple OSS Distributions 			} else {
1371*1b191cb5SApple OSS Distributions 				dirent.d_fileno = dir_node->dn_ino;
1372*1b191cb5SApple OSS Distributions 			}
1373*1b191cb5SApple OSS Distributions 			name = "..";
1374*1b191cb5SApple OSS Distributions 			dirent.d_namlen = 2;
1375*1b191cb5SApple OSS Distributions 			dirent.d_type = DT_DIR;
1376*1b191cb5SApple OSS Distributions 			break;
1377*1b191cb5SApple OSS Distributions 		default:
1378*1b191cb5SApple OSS Distributions 			dirent.d_fileno = name_node->de_dnp->dn_ino;
1379*1b191cb5SApple OSS Distributions 			dirent.d_namlen = (__uint8_t) strlen(name_node->de_name);
1380*1b191cb5SApple OSS Distributions 			name = name_node->de_name;
1381*1b191cb5SApple OSS Distributions 			switch (name_node->de_dnp->dn_type) {
1382*1b191cb5SApple OSS Distributions 			case DEV_BDEV:
1383*1b191cb5SApple OSS Distributions 				dirent.d_type = DT_BLK;
1384*1b191cb5SApple OSS Distributions 				break;
1385*1b191cb5SApple OSS Distributions 			case DEV_CDEV:
1386*1b191cb5SApple OSS Distributions 				dirent.d_type = DT_CHR;
1387*1b191cb5SApple OSS Distributions 				break;
1388*1b191cb5SApple OSS Distributions 			case DEV_DIR:
1389*1b191cb5SApple OSS Distributions 				dirent.d_type = DT_DIR;
1390*1b191cb5SApple OSS Distributions 				break;
1391*1b191cb5SApple OSS Distributions 			case DEV_SLNK:
1392*1b191cb5SApple OSS Distributions 				dirent.d_type = DT_LNK;
1393*1b191cb5SApple OSS Distributions 				break;
1394*1b191cb5SApple OSS Distributions 			default:
1395*1b191cb5SApple OSS Distributions 				dirent.d_type = DT_UNKNOWN;
1396*1b191cb5SApple OSS Distributions 			}
1397*1b191cb5SApple OSS Distributions 		}
1398*1b191cb5SApple OSS Distributions #define GENERIC_DIRSIZ(dp) \
1399*1b191cb5SApple OSS Distributions     ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
1400*1b191cb5SApple OSS Distributions 
1401*1b191cb5SApple OSS Distributions 		reclen = dirent.d_reclen = GENERIC_DIRSIZ(&dirent);
1402*1b191cb5SApple OSS Distributions 
1403*1b191cb5SApple OSS Distributions 		if (pos >= startpos) {   /* made it to the offset yet? */
1404*1b191cb5SApple OSS Distributions 			if (uio_resid(uio) < reclen) { /* will it fit? */
1405*1b191cb5SApple OSS Distributions 				break;
1406*1b191cb5SApple OSS Distributions 			}
1407*1b191cb5SApple OSS Distributions 			strlcpy(dirent.d_name, name, DEVMAXNAMESIZE);
1408*1b191cb5SApple OSS Distributions 			if ((error = uiomove((caddr_t)&dirent,
1409*1b191cb5SApple OSS Distributions 			    dirent.d_reclen, uio)) != 0) {
1410*1b191cb5SApple OSS Distributions 				break;
1411*1b191cb5SApple OSS Distributions 			}
1412*1b191cb5SApple OSS Distributions 		}
1413*1b191cb5SApple OSS Distributions 		pos += reclen;
1414*1b191cb5SApple OSS Distributions 		if ((nodenumber > 1) && name_node) {
1415*1b191cb5SApple OSS Distributions 			name_node = name_node->de_next;
1416*1b191cb5SApple OSS Distributions 		}
1417*1b191cb5SApple OSS Distributions 		nodenumber++;
1418*1b191cb5SApple OSS Distributions 	}
1419*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1420*1b191cb5SApple OSS Distributions 	uio->uio_offset = pos;
1421*1b191cb5SApple OSS Distributions 
1422*1b191cb5SApple OSS Distributions 	devfs_consider_time_update(dir_node, DEVFS_UPDATE_ACCESS);
1423*1b191cb5SApple OSS Distributions 
1424*1b191cb5SApple OSS Distributions 	return error;
1425*1b191cb5SApple OSS Distributions }
1426*1b191cb5SApple OSS Distributions 
1427*1b191cb5SApple OSS Distributions 
1428*1b191cb5SApple OSS Distributions /*
1429*1b191cb5SApple OSS Distributions  */
1430*1b191cb5SApple OSS Distributions static int
devfs_readlink(struct vnop_readlink_args * ap)1431*1b191cb5SApple OSS Distributions devfs_readlink(struct vnop_readlink_args *ap)
1432*1b191cb5SApple OSS Distributions /*struct vnop_readlink_args {
1433*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
1434*1b191cb5SApple OSS Distributions  *       struct uio *a_uio;
1435*1b191cb5SApple OSS Distributions  *       vfs_context_t a_context;
1436*1b191cb5SApple OSS Distributions  *  } */
1437*1b191cb5SApple OSS Distributions {
1438*1b191cb5SApple OSS Distributions 	struct vnode *vp = ap->a_vp;
1439*1b191cb5SApple OSS Distributions 	struct uio *uio = ap->a_uio;
1440*1b191cb5SApple OSS Distributions 	devnode_t * lnk_node;
1441*1b191cb5SApple OSS Distributions 	int error = 0;
1442*1b191cb5SApple OSS Distributions 
1443*1b191cb5SApple OSS Distributions 	/*  set up refs to dir */
1444*1b191cb5SApple OSS Distributions 	lnk_node = VTODN(vp);
1445*1b191cb5SApple OSS Distributions 
1446*1b191cb5SApple OSS Distributions 	if (lnk_node->dn_type != DEV_SLNK) {
1447*1b191cb5SApple OSS Distributions 		error = EINVAL;
1448*1b191cb5SApple OSS Distributions 		goto out;
1449*1b191cb5SApple OSS Distributions 	}
1450*1b191cb5SApple OSS Distributions 	error = uiomove(lnk_node->dn_typeinfo.Slnk.name,
1451*1b191cb5SApple OSS Distributions 	    (int)lnk_node->dn_typeinfo.Slnk.namelen, uio);
1452*1b191cb5SApple OSS Distributions out:
1453*1b191cb5SApple OSS Distributions 	return error;
1454*1b191cb5SApple OSS Distributions }
1455*1b191cb5SApple OSS Distributions 
1456*1b191cb5SApple OSS Distributions static int
devfs_reclaim(struct vnop_reclaim_args * ap)1457*1b191cb5SApple OSS Distributions devfs_reclaim(struct vnop_reclaim_args *ap)
1458*1b191cb5SApple OSS Distributions /*struct vnop_reclaim_args {
1459*1b191cb5SApple OSS Distributions  *       struct vnode *a_vp;
1460*1b191cb5SApple OSS Distributions  *  } */
1461*1b191cb5SApple OSS Distributions {
1462*1b191cb5SApple OSS Distributions 	struct vnode *      vp = ap->a_vp;
1463*1b191cb5SApple OSS Distributions 	devnode_t *         dnp;
1464*1b191cb5SApple OSS Distributions 
1465*1b191cb5SApple OSS Distributions 	DEVFS_LOCK();
1466*1b191cb5SApple OSS Distributions 
1467*1b191cb5SApple OSS Distributions 	dnp = VTODN(vp);
1468*1b191cb5SApple OSS Distributions 
1469*1b191cb5SApple OSS Distributions 	if (dnp) {
1470*1b191cb5SApple OSS Distributions 		/* If this is a cloning device, it didn't have a dn_vn anyway */
1471*1b191cb5SApple OSS Distributions 		dnp->dn_vn = NULL;
1472*1b191cb5SApple OSS Distributions 		vnode_clearfsnode(vp);
1473*1b191cb5SApple OSS Distributions 
1474*1b191cb5SApple OSS Distributions 		/* This could delete the node, if we are the last vnode */
1475*1b191cb5SApple OSS Distributions 		devfs_rele_node(dnp);
1476*1b191cb5SApple OSS Distributions 	}
1477*1b191cb5SApple OSS Distributions 	DEVFS_UNLOCK();
1478*1b191cb5SApple OSS Distributions 
1479*1b191cb5SApple OSS Distributions 	return 0;
1480*1b191cb5SApple OSS Distributions }
1481*1b191cb5SApple OSS Distributions 
1482*1b191cb5SApple OSS Distributions 
1483*1b191cb5SApple OSS Distributions /*
1484*1b191cb5SApple OSS Distributions  * Get configurable pathname variables.
1485*1b191cb5SApple OSS Distributions  */
1486*1b191cb5SApple OSS Distributions static int
devs_vnop_pathconf(struct vnop_pathconf_args * ap)1487*1b191cb5SApple OSS Distributions devs_vnop_pathconf(
1488*1b191cb5SApple OSS Distributions 	struct vnop_pathconf_args /* {
1489*1b191cb5SApple OSS Distributions                                    *  struct vnode *a_vp;
1490*1b191cb5SApple OSS Distributions                                    *  int a_name;
1491*1b191cb5SApple OSS Distributions                                    *  int *a_retval;
1492*1b191cb5SApple OSS Distributions                                    *  vfs_context_t a_context;
1493*1b191cb5SApple OSS Distributions                                    *  } */*ap)
1494*1b191cb5SApple OSS Distributions {
1495*1b191cb5SApple OSS Distributions 	switch (ap->a_name) {
1496*1b191cb5SApple OSS Distributions 	case _PC_LINK_MAX:
1497*1b191cb5SApple OSS Distributions 		/* arbitrary limit matching HFS; devfs has no hard limit */
1498*1b191cb5SApple OSS Distributions 		*ap->a_retval = 32767;
1499*1b191cb5SApple OSS Distributions 		break;
1500*1b191cb5SApple OSS Distributions 	case _PC_NAME_MAX:
1501*1b191cb5SApple OSS Distributions 		*ap->a_retval = DEVMAXNAMESIZE - 1;     /* includes NUL */
1502*1b191cb5SApple OSS Distributions 		break;
1503*1b191cb5SApple OSS Distributions 	case _PC_PATH_MAX:
1504*1b191cb5SApple OSS Distributions 		*ap->a_retval = DEVMAXPATHSIZE - 1;     /* XXX nonconformant */
1505*1b191cb5SApple OSS Distributions 		break;
1506*1b191cb5SApple OSS Distributions 	case _PC_CHOWN_RESTRICTED:
1507*1b191cb5SApple OSS Distributions 		*ap->a_retval = 200112;         /* _POSIX_CHOWN_RESTRICTED */
1508*1b191cb5SApple OSS Distributions 		break;
1509*1b191cb5SApple OSS Distributions 	case _PC_NO_TRUNC:
1510*1b191cb5SApple OSS Distributions 		*ap->a_retval = 0;
1511*1b191cb5SApple OSS Distributions 		break;
1512*1b191cb5SApple OSS Distributions 	case _PC_CASE_SENSITIVE:
1513*1b191cb5SApple OSS Distributions 		*ap->a_retval = 1;
1514*1b191cb5SApple OSS Distributions 		break;
1515*1b191cb5SApple OSS Distributions 	case _PC_CASE_PRESERVING:
1516*1b191cb5SApple OSS Distributions 		*ap->a_retval = 1;
1517*1b191cb5SApple OSS Distributions 		break;
1518*1b191cb5SApple OSS Distributions 	default:
1519*1b191cb5SApple OSS Distributions 		return EINVAL;
1520*1b191cb5SApple OSS Distributions 	}
1521*1b191cb5SApple OSS Distributions 
1522*1b191cb5SApple OSS Distributions 	return 0;
1523*1b191cb5SApple OSS Distributions }
1524*1b191cb5SApple OSS Distributions 
1525*1b191cb5SApple OSS Distributions 
1526*1b191cb5SApple OSS Distributions 
1527*1b191cb5SApple OSS Distributions /**************************************************************************\
1528*1b191cb5SApple OSS Distributions * pseudo ops *
1529*1b191cb5SApple OSS Distributions \**************************************************************************/
1530*1b191cb5SApple OSS Distributions 
1531*1b191cb5SApple OSS Distributions /*
1532*1b191cb5SApple OSS Distributions  *
1533*1b191cb5SApple OSS Distributions  *	struct vnop_inactive_args {
1534*1b191cb5SApple OSS Distributions  *		struct vnode *a_vp;
1535*1b191cb5SApple OSS Distributions  *		vfs_context_t a_context;
1536*1b191cb5SApple OSS Distributions  *	}
1537*1b191cb5SApple OSS Distributions  */
1538*1b191cb5SApple OSS Distributions 
1539*1b191cb5SApple OSS Distributions static int
devfs_inactive(__unused struct vnop_inactive_args * ap)1540*1b191cb5SApple OSS Distributions devfs_inactive(__unused struct vnop_inactive_args *ap)
1541*1b191cb5SApple OSS Distributions {
1542*1b191cb5SApple OSS Distributions 	vnode_t vp = ap->a_vp;
1543*1b191cb5SApple OSS Distributions 	devnode_t *dnp = VTODN(vp);
1544*1b191cb5SApple OSS Distributions 
1545*1b191cb5SApple OSS Distributions 	/*
1546*1b191cb5SApple OSS Distributions 	 * Cloned vnodes are not linked in anywhere, so they
1547*1b191cb5SApple OSS Distributions 	 * can just be recycled.
1548*1b191cb5SApple OSS Distributions 	 */
1549*1b191cb5SApple OSS Distributions 	if (dnp->dn_clone != NULL) {
1550*1b191cb5SApple OSS Distributions 		vnode_recycle(vp);
1551*1b191cb5SApple OSS Distributions 	}
1552*1b191cb5SApple OSS Distributions 
1553*1b191cb5SApple OSS Distributions 	return 0;
1554*1b191cb5SApple OSS Distributions }
1555*1b191cb5SApple OSS Distributions 
1556*1b191cb5SApple OSS Distributions /*
1557*1b191cb5SApple OSS Distributions  * called with DEVFS_LOCK held
1558*1b191cb5SApple OSS Distributions  */
1559*1b191cb5SApple OSS Distributions static int
devfs_update(struct vnode * vp,struct timeval * access,struct timeval * modify)1560*1b191cb5SApple OSS Distributions devfs_update(struct vnode *vp, struct timeval *access, struct timeval *modify)
1561*1b191cb5SApple OSS Distributions {
1562*1b191cb5SApple OSS Distributions 	devnode_t * ip;
1563*1b191cb5SApple OSS Distributions 	struct timeval now;
1564*1b191cb5SApple OSS Distributions 
1565*1b191cb5SApple OSS Distributions 	ip = VTODN(vp);
1566*1b191cb5SApple OSS Distributions 	if (vp->v_mount->mnt_flag & MNT_RDONLY) {
1567*1b191cb5SApple OSS Distributions 		ip->dn_access = 0;
1568*1b191cb5SApple OSS Distributions 		ip->dn_change = 0;
1569*1b191cb5SApple OSS Distributions 		ip->dn_update = 0;
1570*1b191cb5SApple OSS Distributions 
1571*1b191cb5SApple OSS Distributions 		return 0;
1572*1b191cb5SApple OSS Distributions 	}
1573*1b191cb5SApple OSS Distributions 
1574*1b191cb5SApple OSS Distributions 	DEVFS_ATTR_LOCK_SPIN();
1575*1b191cb5SApple OSS Distributions 	microtime(&now);
1576*1b191cb5SApple OSS Distributions 	dn_times_locked(ip, access, modify, &now, DEVFS_UPDATE_ACCESS | DEVFS_UPDATE_MOD);
1577*1b191cb5SApple OSS Distributions 	DEVFS_ATTR_UNLOCK();
1578*1b191cb5SApple OSS Distributions 
1579*1b191cb5SApple OSS Distributions 	return 0;
1580*1b191cb5SApple OSS Distributions }
1581*1b191cb5SApple OSS Distributions 
1582*1b191cb5SApple OSS Distributions #define VOPFUNC int (*)(void *)
1583*1b191cb5SApple OSS Distributions 
1584*1b191cb5SApple OSS Distributions #define devfs_default_error (void (*)(void))vn_default_error
1585*1b191cb5SApple OSS Distributions 
1586*1b191cb5SApple OSS Distributions /* The following ops are used by directories and symlinks */
1587*1b191cb5SApple OSS Distributions int(**devfs_vnodeop_p)(void *);
1588*1b191cb5SApple OSS Distributions const static struct vnodeopv_entry_desc devfs_vnodeop_entries[] = {
1589*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)devfs_default_error },
1590*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)devfs_lookup },           /* lookup */
1591*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)err_create },             /* create */
1592*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_whiteout_desc, .opve_impl = (VOPFUNC)err_whiteout },         /* whiteout */
1593*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)devfs_mknod },             /* mknod */
1594*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)nop_open },                 /* open */
1595*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)devfs_close },             /* close */
1596*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)devfs_getattr },         /* getattr */
1597*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)devfs_setattr },         /* setattr */
1598*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)devfs_read },               /* read */
1599*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)devfs_write },             /* write */
1600*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)err_ioctl },               /* ioctl */
1601*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)err_select },             /* select */
1602*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)err_revoke },             /* revoke */
1603*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)err_mmap },                 /* mmap */
1604*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)nop_fsync },               /* fsync */
1605*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)devfs_vnop_remove },      /* remove */
1606*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)devfs_link },               /* link */
1607*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)devfs_rename },           /* rename */
1608*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)devfs_mkdir },             /* mkdir */
1609*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)devfs_rmdir },             /* rmdir */
1610*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)devfs_symlink },         /* symlink */
1611*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)devfs_readdir },         /* readdir */
1612*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)devfs_readlink },       /* readlink */
1613*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)devfs_inactive },       /* inactive */
1614*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)devfs_reclaim },         /* reclaim */
1615*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)err_strategy },         /* strategy */
1616*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)devs_vnop_pathconf },   /* pathconf */
1617*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)err_advlock },           /* advlock */
1618*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)err_bwrite },
1619*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein },             /* Pagein */
1620*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout },           /* Pageout */
1621*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile },         /* Copyfile */
1622*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)err_blktooff },         /* blktooff */
1623*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_offtoblk_desc, .opve_impl = (VOPFUNC)err_offtoblk },         /* offtoblk */
1624*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)err_blockmap },         /* blockmap */
1625*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1626*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_setlabel_desc, .opve_impl = (VOPFUNC)devfs_setlabel },       /* setlabel */
1627*1b191cb5SApple OSS Distributions #endif
1628*1b191cb5SApple OSS Distributions 	{ .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL }
1629*1b191cb5SApple OSS Distributions };
1630*1b191cb5SApple OSS Distributions const struct vnodeopv_desc devfs_vnodeop_opv_desc =
1631*1b191cb5SApple OSS Distributions { .opv_desc_vector_p = &devfs_vnodeop_p, .opv_desc_ops = devfs_vnodeop_entries };
1632*1b191cb5SApple OSS Distributions 
1633*1b191cb5SApple OSS Distributions /* The following ops are used by the device nodes */
1634*1b191cb5SApple OSS Distributions int(**devfs_spec_vnodeop_p)(void *);
1635*1b191cb5SApple OSS Distributions const static struct vnodeopv_entry_desc devfs_spec_vnodeop_entries[] = {
1636*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)devfs_default_error },
1637*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)spec_lookup },            /* lookup */
1638*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_create_desc, .opve_impl = (VOPFUNC)spec_create },            /* create */
1639*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_mknod_desc, .opve_impl = (VOPFUNC)spec_mknod },              /* mknod */
1640*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)spec_open },                        /* open */
1641*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)devfsspec_close },         /* close */
1642*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)devfs_getattr },         /* getattr */
1643*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)devfs_setattr },         /* setattr */
1644*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_read_desc, .opve_impl = (VOPFUNC)devfsspec_read },           /* read */
1645*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_write_desc, .opve_impl = (VOPFUNC)devfsspec_write },         /* write */
1646*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)spec_ioctl },              /* ioctl */
1647*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_select_desc, .opve_impl = (VOPFUNC)spec_select },            /* select */
1648*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)spec_revoke },            /* revoke */
1649*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_mmap_desc, .opve_impl = (VOPFUNC)spec_mmap },                        /* mmap */
1650*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)spec_fsync },              /* fsync */
1651*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_remove_desc, .opve_impl = (VOPFUNC)devfs_vnop_remove },      /* remove */
1652*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_link_desc, .opve_impl = (VOPFUNC)devfs_link },               /* link */
1653*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_rename_desc, .opve_impl = (VOPFUNC)spec_rename },            /* rename */
1654*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_mkdir_desc, .opve_impl = (VOPFUNC)spec_mkdir },              /* mkdir */
1655*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_rmdir_desc, .opve_impl = (VOPFUNC)spec_rmdir },              /* rmdir */
1656*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_symlink_desc, .opve_impl = (VOPFUNC)spec_symlink },          /* symlink */
1657*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)spec_readdir },          /* readdir */
1658*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_readlink_desc, .opve_impl = (VOPFUNC)spec_readlink },                /* readlink */
1659*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)devfs_inactive },       /* inactive */
1660*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)devfs_reclaim },         /* reclaim */
1661*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_strategy_desc, .opve_impl = (VOPFUNC)spec_strategy },                /* strategy */
1662*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)spec_pathconf },                /* pathconf */
1663*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_advlock_desc, .opve_impl = (VOPFUNC)spec_advlock },          /* advlock */
1664*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_bwrite_desc, .opve_impl = (VOPFUNC)vn_bwrite },
1665*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pagein_desc, .opve_impl = (VOPFUNC)err_pagein },             /* Pagein */
1666*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pageout_desc, .opve_impl = (VOPFUNC)err_pageout },           /* Pageout */
1667*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_copyfile_desc, .opve_impl = (VOPFUNC)err_copyfile },         /* Copyfile */
1668*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)spec_blktooff },        /* blktooff */
1669*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_blktooff_desc, .opve_impl = (VOPFUNC)spec_offtoblk  },       /* blkofftoblk */
1670*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_blockmap_desc, .opve_impl = (VOPFUNC)spec_blockmap },        /* blockmap */
1671*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1672*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_setlabel_desc, .opve_impl = (VOPFUNC)devfs_setlabel },       /* setlabel */
1673*1b191cb5SApple OSS Distributions #endif
1674*1b191cb5SApple OSS Distributions 	{ .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL }
1675*1b191cb5SApple OSS Distributions };
1676*1b191cb5SApple OSS Distributions const struct vnodeopv_desc devfs_spec_vnodeop_opv_desc =
1677*1b191cb5SApple OSS Distributions { .opv_desc_vector_p = &devfs_spec_vnodeop_p, .opv_desc_ops = devfs_spec_vnodeop_entries };
1678*1b191cb5SApple OSS Distributions 
1679*1b191cb5SApple OSS Distributions 
1680*1b191cb5SApple OSS Distributions #if FDESC
1681*1b191cb5SApple OSS Distributions int(**devfs_devfd_vnodeop_p)(void*);
1682*1b191cb5SApple OSS Distributions const static struct vnodeopv_entry_desc devfs_devfd_vnodeop_entries[] = {
1683*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)devfs_default_error },
1684*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)devfs_devfd_lookup},      /* lookup */
1685*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)nop_open },                 /* open */
1686*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)devfs_close },             /* close */
1687*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_getattr_desc, .opve_impl = (VOPFUNC)devfs_getattr },         /* getattr */
1688*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_setattr_desc, .opve_impl = (VOPFUNC)devfs_setattr },         /* setattr */
1689*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_revoke_desc, .opve_impl = (VOPFUNC)err_revoke },             /* revoke */
1690*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_fsync_desc, .opve_impl = (VOPFUNC)nop_fsync },               /* fsync */
1691*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_readdir_desc, .opve_impl = (VOPFUNC)devfs_devfd_readdir},            /* readdir */
1692*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_inactive_desc, .opve_impl = (VOPFUNC)devfs_inactive },       /* inactive */
1693*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_reclaim_desc, .opve_impl = (VOPFUNC)devfs_reclaim },         /* reclaim */
1694*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_pathconf_desc, .opve_impl = (VOPFUNC)devs_vnop_pathconf },   /* pathconf */
1695*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1696*1b191cb5SApple OSS Distributions 	{ .opve_op = &vnop_setlabel_desc, .opve_impl = (VOPFUNC)devfs_setlabel },       /* setlabel */
1697*1b191cb5SApple OSS Distributions #endif
1698*1b191cb5SApple OSS Distributions 	{ .opve_op = (struct vnodeop_desc*)NULL, .opve_impl = (int (*)(void *))NULL }
1699*1b191cb5SApple OSS Distributions };
1700*1b191cb5SApple OSS Distributions const struct vnodeopv_desc devfs_devfd_vnodeop_opv_desc =
1701*1b191cb5SApple OSS Distributions { .opv_desc_vector_p = &devfs_devfd_vnodeop_p, .opv_desc_ops = devfs_devfd_vnodeop_entries};
1702*1b191cb5SApple OSS Distributions #endif /* FDESC */
1703