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