2 * Copyright (c) 2000-2007 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
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.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright 1997,1998 Julian Elischer. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions are
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.
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
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
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,
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/namei.h>
85 #include <sys/kernel.h>
86 #include <sys/fcntl.h>
88 #include <sys/disklabel.h>
91 #include <sys/mount_internal.h>
93 #include <sys/kauth.h>
95 #include <sys/vnode_internal.h>
96 #include <miscfs/specfs/specdev.h>
97 #include <sys/dirent.h>
98 #include <sys/vmmeter.h>
100 #include <sys/uio_internal.h>
103 #include <security/mac_framework.h>
106 #include "devfsdefs.h"
108 static int devfs_update(struct vnode
*vp
, struct timeval
*access
,
109 struct timeval
*modify
);
113 * Convert a component of a pathname into a pointer to a locked node.
114 * This is a very central and rather complicated routine.
115 * If the file system is not maintained in a strict tree hierarchy,
116 * this can result in a deadlock situation (see comments in code below).
118 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
119 * whether the name is to be looked up, created, renamed, or deleted.
120 * When CREATE, RENAME, or DELETE is specified, information usable in
121 * creating, renaming, or deleting a directory entry may be calculated.
122 * If flag has LOCKPARENT or'ed into it and the target of the pathname
123 * exists, lookup returns both the target and its parent directory locked.
124 * When creating or renaming and LOCKPARENT is specified, the target may
125 * not be ".". When deleting and LOCKPARENT is specified, the target may
126 * be "."., but the caller must check to ensure it does an vrele and DNUNLOCK
127 * instead of two DNUNLOCKs.
129 * Overall outline of devfs_lookup:
131 * check accessibility of directory
132 * null terminate the component (lookup leaves the whole string alone)
133 * look for name in cache, if found, then if at end of path
134 * and deleting or creating, drop it, else return name
135 * search for name in directory, to found or notfound
137 * if creating, return locked directory,
140 * if at end of path and deleting, return information to allow delete
141 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
142 * node and return info to allow rewrite
143 * if not at end, add name to cache; if at end and neither creating
144 * nor deleting, add name to cache
145 * On return to lookup, remove the null termination we put in at the start.
147 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent node unlocked.
150 devfs_lookup(struct vnop_lookup_args
*ap
)
151 /*struct vnop_lookup_args {
152 struct vnode * a_dvp; directory vnode ptr
153 struct vnode ** a_vpp; where to put the result
154 struct componentname * a_cnp; the name we want
155 vfs_context_t a_context;
158 struct componentname
*cnp
= ap
->a_cnp
;
159 vfs_context_t ctx
= cnp
->cn_context
;
160 struct proc
*p
= vfs_context_proc(ctx
);
161 struct vnode
*dir_vnode
= ap
->a_dvp
;
162 struct vnode
**result_vnode
= ap
->a_vpp
;
163 devnode_t
* dir_node
; /* the directory we are searching */
164 devnode_t
* node
= NULL
; /* the node we are searching for */
165 devdirent_t
* nodename
;
166 int flags
= cnp
->cn_flags
;
167 int op
= cnp
->cn_nameiop
; /* LOOKUP, CREATE, RENAME, or DELETE */
168 int wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
170 char heldchar
; /* the char at the end of the name componet */
174 *result_vnode
= NULL
; /* safe not sorry */ /*XXX*/
176 /* okay to look at directory vnodes ourside devfs lock as they are not aliased */
177 dir_node
= VTODN(dir_vnode
);
180 * Make sure that our node is a directory as well.
182 if (dir_node
->dn_type
!= DEV_DIR
) {
188 * temporarily terminate string component
190 heldchar
= cnp
->cn_nameptr
[cnp
->cn_namelen
];
191 cnp
->cn_nameptr
[cnp
->cn_namelen
] = '\0';
193 nodename
= dev_findname(dir_node
, cnp
->cn_nameptr
);
195 * restore saved character
197 cnp
->cn_nameptr
[cnp
->cn_namelen
] = heldchar
;
201 node
= nodename
->de_dnp
;
203 /* Do potential vnode allocation here inside the lock
204 * to make sure that our device node has a non-NULL dn_vn
205 * associated with it. The device node might otherwise
206 * get deleted out from under us (see devfs_dn_free()).
208 error
= devfs_dntovn(node
, result_vnode
, p
);
219 * we haven't called devfs_dntovn if we get here
220 * we have not taken a reference on the node.. no
221 * vnode_put is necessary on these error returns
223 * If it doesn't exist and we're not the last component,
224 * or we're at the last component, but we're not creating
225 * or renaming, return ENOENT.
227 if (!(flags
& ISLASTCN
) || !(op
== CREATE
|| op
== RENAME
)) {
231 * We return with the directory locked, so that
232 * the parameters we set up above will still be
233 * valid if we actually decide to add a new entry.
234 * We return ni_vp == NULL to indicate that the entry
235 * does not currently exist; we leave a pointer to
236 * the (locked) directory vnode in namei_data->ni_dvp.
238 * NB - if the directory is unlocked, then this
239 * information cannot be used.
241 return (EJUSTRETURN
);
244 * from this point forward, we need to vnode_put the reference
245 * picked up in devfs_dntovn if we decide to return an error
249 * If deleting, and at end of pathname, return
250 * parameters which can be used to remove file.
251 * If the wantparent flag isn't set, we return only
252 * the directory (in namei_data->ni_dvp), otherwise we go
253 * on and lock the node, being careful with ".".
255 if (op
== DELETE
&& (flags
& ISLASTCN
)) {
258 * we are trying to delete '.'. What does this mean? XXX
260 if (dir_node
== node
) {
262 vnode_put(*result_vnode
);
263 *result_vnode
= NULL
;
265 if ( ((error
= vnode_get(dir_vnode
)) == 0) ) {
266 *result_vnode
= dir_vnode
;
274 * If rewriting (RENAME), return the vnode and the
275 * information required to rewrite the present directory
276 * Must get node of directory entry to verify it's a
277 * regular file, or empty directory.
279 if (op
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
282 * Careful about locking second node.
283 * This can only occur if the target is ".".
285 if (dir_node
== node
) {
293 * Step through the translation in the name. We do not unlock the
294 * directory because we may need it again if a symbolic link
295 * is relative to the current directory. Instead we save it
296 * unlocked as "saved_dir_node" XXX. We must get the target
297 * node before unlocking
298 * the directory to insure that the node will not be removed
299 * before we get it. We prevent deadlock by always fetching
300 * nodes from the root, moving down the directory tree. Thus
301 * when following backward pointers ".." we must unlock the
302 * parent directory before getting the requested directory.
303 * There is a potential race condition here if both the current
304 * and parent directories are removed before the lock for the
305 * node associated with ".." returns. We hope that this occurs
306 * infrequently since we cannot avoid this race condition without
307 * implementing a sophisticated deadlock detection algorithm.
308 * Note also that this simple deadlock detection scheme will not
309 * work if the file system has any hard links other than ".."
310 * that point backwards in the directory structure.
312 if ((flags
& ISDOTDOT
) == 0 && dir_node
== node
) {
314 vnode_put(*result_vnode
);
315 *result_vnode
= NULL
;
317 if ( (error
= vnode_get(dir_vnode
)) ) {
320 *result_vnode
= dir_vnode
;
326 vnode_put(*result_vnode
);
327 *result_vnode
= NULL
;
333 devfs_getattr(struct vnop_getattr_args
*ap
)
334 /*struct vnop_getattr_args {
336 struct vnode_attr *a_vap;
341 struct vnode
*vp
= ap
->a_vp
;
342 struct vnode_attr
*vap
= ap
->a_vap
;
343 devnode_t
* file_node
;
348 file_node
= VTODN(vp
);
351 dn_times(file_node
, &now
, &now
, &now
);
353 VATTR_RETURN(vap
, va_mode
, file_node
->dn_mode
);
356 * Note: for DEV_CDEV and DEV_BDEV, we return the device from
357 * the vp, not the file_node; if we getting information on a
358 * cloning device, we want the cloned information, not the template.
360 switch (file_node
->dn_type
)
363 VATTR_RETURN(vap
, va_rdev
, (dev_t
)file_node
->dn_dvm
);
364 vap
->va_mode
|= (S_IFDIR
);
367 VATTR_RETURN(vap
, va_rdev
, vp
->v_rdev
);
368 vap
->va_mode
|= (S_IFCHR
);
371 VATTR_RETURN(vap
, va_rdev
, vp
->v_rdev
);
372 vap
->va_mode
|= (S_IFBLK
);
375 VATTR_RETURN(vap
, va_rdev
, 0);
376 vap
->va_mode
|= (S_IFLNK
);
379 VATTR_RETURN(vap
, va_rdev
, 0); /* default value only */
381 VATTR_RETURN(vap
, va_type
, vp
->v_type
);
382 VATTR_RETURN(vap
, va_nlink
, file_node
->dn_links
);
383 VATTR_RETURN(vap
, va_uid
, file_node
->dn_uid
);
384 VATTR_RETURN(vap
, va_gid
, file_node
->dn_gid
);
385 VATTR_RETURN(vap
, va_fsid
, (uintptr_t)file_node
->dn_dvm
);
386 VATTR_RETURN(vap
, va_fileid
, (uintptr_t)file_node
);
387 VATTR_RETURN(vap
, va_data_size
, file_node
->dn_len
);
389 /* return an override block size (advisory) */
390 if (vp
->v_type
== VBLK
)
391 VATTR_RETURN(vap
, va_iosize
, BLKDEV_IOSIZE
);
392 else if (vp
->v_type
== VCHR
)
393 VATTR_RETURN(vap
, va_iosize
, MAXPHYSIO
);
395 VATTR_RETURN(vap
, va_iosize
, vp
->v_mount
->mnt_vfsstat
.f_iosize
);
396 /* if the time is bogus, set it to the boot time */
397 if (file_node
->dn_ctime
.tv_sec
== 0) {
398 file_node
->dn_ctime
.tv_sec
= boottime_sec();
399 file_node
->dn_ctime
.tv_nsec
= 0;
401 if (file_node
->dn_mtime
.tv_sec
== 0)
402 file_node
->dn_mtime
= file_node
->dn_ctime
;
403 if (file_node
->dn_atime
.tv_sec
== 0)
404 file_node
->dn_atime
= file_node
->dn_ctime
;
405 VATTR_RETURN(vap
, va_change_time
, file_node
->dn_ctime
);
406 VATTR_RETURN(vap
, va_modify_time
, file_node
->dn_mtime
);
407 VATTR_RETURN(vap
, va_access_time
, file_node
->dn_atime
);
408 VATTR_RETURN(vap
, va_gen
, 0);
409 VATTR_RETURN(vap
, va_flags
, 0);
410 VATTR_RETURN(vap
, va_filerev
, 0);
411 VATTR_RETURN(vap
, va_acl
, NULL
);
419 devfs_setattr(struct vnop_setattr_args
*ap
)
420 /*struct vnop_setattr_args {
422 struct vnode_attr *a_vap;
423 vfs_context_t a_context;
426 struct vnode
*vp
= ap
->a_vp
;
427 struct vnode_attr
*vap
= ap
->a_vap
;
429 devnode_t
* file_node
;
430 struct timeval atimeval
, mtimeval
;
434 file_node
= VTODN(vp
);
436 * Go through the fields and update if set.
438 if (VATTR_IS_ACTIVE(vap
, va_access_time
) || VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
441 if (VATTR_IS_ACTIVE(vap
, va_access_time
))
442 file_node
->dn_access
= 1;
443 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
444 file_node
->dn_change
= 1;
445 file_node
->dn_update
= 1;
447 atimeval
.tv_sec
= vap
->va_access_time
.tv_sec
;
448 atimeval
.tv_usec
= vap
->va_access_time
.tv_nsec
/ 1000;
449 mtimeval
.tv_sec
= vap
->va_modify_time
.tv_sec
;
450 mtimeval
.tv_usec
= vap
->va_modify_time
.tv_nsec
/ 1000;
452 if ( (error
= devfs_update(vp
, &atimeval
, &mtimeval
)) )
455 VATTR_SET_SUPPORTED(vap
, va_access_time
);
456 VATTR_SET_SUPPORTED(vap
, va_change_time
);
459 * Change the permissions.
461 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
462 file_node
->dn_mode
&= ~07777;
463 file_node
->dn_mode
|= vap
->va_mode
& 07777;
465 VATTR_SET_SUPPORTED(vap
, va_mode
);
470 if (VATTR_IS_ACTIVE(vap
, va_uid
))
471 file_node
->dn_uid
= vap
->va_uid
;
472 VATTR_SET_SUPPORTED(vap
, va_uid
);
477 if (VATTR_IS_ACTIVE(vap
, va_gid
))
478 file_node
->dn_gid
= vap
->va_gid
;
479 VATTR_SET_SUPPORTED(vap
, va_gid
);
488 devfs_setlabel(struct vnop_setlabel_args
*ap
)
489 /* struct vnop_setlabel_args {
490 struct vnodeop_desc *a_desc;
493 vfs_context_t a_context;
502 mac_vnode_label_update(ap
->a_context
, vp
, ap
->a_vl
);
503 mac_devfs_label_update(vp
->v_mount
, de
, vp
);
510 devfs_read(struct vnop_read_args
*ap
)
511 /* struct vnop_read_args {
515 vfs_context_t a_context;
518 devnode_t
* dn_p
= VTODN(ap
->a_vp
);
520 switch (ap
->a_vp
->v_type
) {
524 return VNOP_READDIR(ap
->a_vp
, ap
->a_uio
, 0, NULL
, NULL
, ap
->a_context
);
527 printf("devfs_read(): bad file type %d", ap
->a_vp
->v_type
);
532 return (0); /* not reached */
536 devfs_close(struct vnop_close_args
*ap
)
537 /* struct vnop_close_args {
540 vfs_context_t a_context;
543 struct vnode
* vp
= ap
->a_vp
;
544 register devnode_t
* dnp
;
547 if (vnode_isinuse(vp
, 1)) {
551 dn_times(dnp
, &now
, &now
, &now
);
558 devfsspec_close(struct vnop_close_args
*ap
)
559 /* struct vnop_close_args {
562 vfs_context_t a_context;
565 struct vnode
* vp
= ap
->a_vp
;
566 register devnode_t
* dnp
;
569 if (vnode_isinuse(vp
, 1)) {
573 dn_times(dnp
, &now
, &now
, &now
);
576 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
580 devfsspec_read(struct vnop_read_args
*ap
)
581 /* struct vnop_read_args {
588 register devnode_t
* dnp
= VTODN(ap
->a_vp
);
592 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
596 devfsspec_write(struct vnop_write_args
*ap
)
597 /* struct vnop_write_args {
601 vfs_context_t a_context;
604 register devnode_t
* dnp
= VTODN(ap
->a_vp
);
609 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
613 * Write data to a file or directory.
616 devfs_write(struct vnop_write_args
*ap
)
617 /* struct vnop_write_args {
624 switch (ap
->a_vp
->v_type
) {
628 printf("devfs_write(): bad file type %d", ap
->a_vp
->v_type
);
631 return 0; /* not reached */
635 devfs_remove(struct vnop_remove_args
*ap
)
636 /* struct vnop_remove_args {
639 struct componentname *a_cnp;
642 struct vnode
*vp
= ap
->a_vp
;
643 struct vnode
*dvp
= ap
->a_dvp
;
644 struct componentname
*cnp
= ap
->a_cnp
;
648 int doingdirectory
= 0;
652 * assume that the name is null terminated as they
653 * are the end of the path. Get pointers to all our
662 tnp
= dev_findname(tdp
, cnp
->cn_nameptr
);
670 * Make sure that we don't try do something stupid
672 if ((tp
->dn_type
) == DEV_DIR
) {
674 * Avoid ".", "..", and aliases of "." for obvious reasons.
676 if ( (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.')
677 || (cnp
->cn_flags
&ISDOTDOT
) ) {
684 /***********************************
685 * Start actually doing things.... *
686 ***********************************/
691 * Target must be empty if a directory and have no links
692 * to it. Also, ensure source and target are compatible
693 * (both directories, or both not directories).
695 if (( doingdirectory
) && (tp
->dn_links
> 2)) {
709 devfs_link(struct vnop_link_args
*ap
)
710 /*struct vnop_link_args {
711 struct vnode *a_tdvp;
713 struct componentname *a_cnp;
714 vfs_context_t a_context;
717 struct vnode
*vp
= ap
->a_vp
;
718 struct vnode
*tdvp
= ap
->a_tdvp
;
719 struct componentname
*cnp
= ap
->a_cnp
;
727 * First catch an arbitrary restriction for this FS
729 if (cnp
->cn_namelen
> DEVMAXNAMESIZE
) {
730 error
= ENAMETOOLONG
;
735 * Lock our directories and get our name pointers
736 * assume that the names are null terminated as they
737 * are the end of the path. Get pointers to all our
740 /* can lookup dnode safely for tdvp outside of devfs lock as it is not aliased */
743 if (tdvp
->v_mount
!= vp
->v_mount
) {
750 /***********************************
751 * Start actually doing things.... *
752 ***********************************/
756 error
= devfs_update(vp
, &now
, &now
);
759 error
= dev_add_name(cnp
->cn_nameptr
, tdp
, NULL
, fp
, &tnp
);
768 * Rename system call. Seems overly complicated to me...
769 * rename("foo", "bar");
772 * link("foo", "bar");
774 * but ``atomically''.
776 * When the target exists, both the directory
777 * and target vnodes are locked.
778 * the source and source-parent vnodes are referenced
781 * Basic algorithm is:
783 * 1) Bump link count on source while we're linking it to the
784 * target. This also ensure the inode won't be deleted out
785 * from underneath us while we work (it may be truncated by
786 * a concurrent `trunc' or `open' for creation).
787 * 2) Link source to destination. If destination already exists,
789 * 3) Unlink source reference to node if still around. If a
790 * directory was moved and the parent of the destination
791 * is different from the source, patch the ".." entry in the
795 devfs_rename(struct vnop_rename_args
*ap
)
796 /*struct vnop_rename_args {
797 struct vnode *a_fdvp;
799 struct componentname *a_fcnp;
800 struct vnode *a_tdvp;
802 struct componentname *a_tcnp;
803 vfs_context_t a_context;
806 struct vnode
*tvp
= ap
->a_tvp
;
807 struct vnode
*tdvp
= ap
->a_tdvp
;
808 struct vnode
*fvp
= ap
->a_fvp
;
809 struct vnode
*fdvp
= ap
->a_fdvp
;
810 struct componentname
*tcnp
= ap
->a_tcnp
;
811 struct componentname
*fcnp
= ap
->a_fcnp
;
812 devnode_t
*fp
, *fdp
, *tp
, *tdp
;
813 devdirent_t
*fnp
,*tnp
;
814 int doingdirectory
= 0;
820 * First catch an arbitrary restriction for this FS
822 if (tcnp
->cn_namelen
> DEVMAXNAMESIZE
) {
823 error
= ENAMETOOLONG
;
828 * assume that the names are null terminated as they
829 * are the end of the path. Get pointers to all our
836 fnp
= dev_findname(fdp
, fcnp
->cn_nameptr
);
846 tnp
= dev_findname(tdp
, tcnp
->cn_nameptr
);
856 * Make sure that we don't try do something stupid
858 if ((fp
->dn_type
) == DEV_DIR
) {
860 * Avoid ".", "..", and aliases of "." for obvious reasons.
862 if ((fcnp
->cn_namelen
== 1 && fcnp
->cn_nameptr
[0] == '.')
863 || (fcnp
->cn_flags
&ISDOTDOT
)
864 || (tcnp
->cn_namelen
== 1 && tcnp
->cn_nameptr
[0] == '.')
865 || (tcnp
->cn_flags
&ISDOTDOT
)
874 * If ".." must be changed (ie the directory gets a new
875 * parent) then the source directory must not be in the
876 * directory hierarchy above the target, as this would
877 * orphan everything below the source directory. Also
878 * the user must have write permission in the source so
879 * as to be able to change "..".
881 if (doingdirectory
&& (tdp
!= fdp
)) {
882 devnode_t
* tmp
, *ntmp
;
886 /* XXX unlock stuff here probably */
891 } while ((tmp
= tmp
->dn_typeinfo
.Dir
.parent
) != ntmp
);
894 /***********************************
895 * Start actually doing things.... *
896 ***********************************/
900 if ( (error
= devfs_update(fvp
, &now
, &now
)) ) {
904 * Check if just deleting a link name.
907 if (fvp
->v_type
== VDIR
) {
911 /* Release destination completely. */
918 * 1) Bump link count while we're moving stuff
919 * around. If we crash somewhere before
920 * completing our work, too bad :)
924 * If the target exists zap it (unless it's a non-empty directory)
925 * We could do that as well but won't
929 * Target must be empty if a directory and have no links
930 * to it. Also, ensure source and target are compatible
931 * (both directories, or both not directories).
933 if (( doingdirectory
) && (tp
->dn_links
> 2)) {
940 dev_add_name(tcnp
->cn_nameptr
,tdp
,NULL
,fp
,&tnp
);
942 fp
->dn_links
--; /* one less link to it.. */
946 fp
->dn_links
--; /* we added one earlier*/
953 devfs_mkdir(struct vnop_mkdir_args
*ap
)
954 /*struct vnop_mkdir_args {
956 struct vnode **a_vpp;
957 struct componentname *a_cnp;
958 struct vnode_attr *a_vap;
959 vfs_context_t a_context;
962 struct componentname
* cnp
= ap
->a_cnp
;
963 vfs_context_t ctx
= cnp
->cn_context
;
964 struct proc
*p
= vfs_context_proc(ctx
);
969 struct vnode_attr
* vap
= ap
->a_vap
;
970 struct vnode
* * vpp
= ap
->a_vpp
;
974 dir_p
= VTODN(ap
->a_dvp
);
975 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
, DEV_DIR
,
976 NULL
, NULL
, NULL
, &nm_p
);
980 dev_p
= nm_p
->de_dnp
;
981 dev_p
->dn_uid
= dir_p
->dn_uid
;
982 dev_p
->dn_gid
= dir_p
->dn_gid
;
983 dev_p
->dn_mode
= vap
->va_mode
;
984 dn_copy_times(dev_p
, dir_p
);
986 error
= devfs_dntovn(dev_p
, vpp
, p
);
994 * An rmdir is a special type of remove, which we already support; we wrap
995 * and reexpress the arguments to call devfs_remove directly. The only
996 * different argument is flags, which we do not set, since it's ignored.
999 devfs_rmdir(struct vnop_rmdir_args
*ap
)
1000 /* struct vnop_rmdir_args {
1001 struct vnode *a_dvp;
1003 struct componentname *a_cnp;
1004 vfs_context_t a_context;
1007 struct vnop_remove_args ra
;
1009 ra
.a_dvp
= ap
->a_dvp
;
1011 ra
.a_cnp
= ap
->a_cnp
;
1012 ra
.a_flags
= 0; /* XXX */
1013 ra
.a_context
= ap
->a_context
;
1015 return devfs_remove(&ra
);
1020 devfs_symlink(struct vnop_symlink_args
*ap
)
1021 /*struct vnop_symlink_args {
1022 struct vnode *a_dvp;
1023 struct vnode **a_vpp;
1024 struct componentname *a_cnp;
1025 struct vnode_attr *a_vap;
1027 vfs_context_t a_context;
1030 struct componentname
* cnp
= ap
->a_cnp
;
1031 vfs_context_t ctx
= cnp
->cn_context
;
1032 struct proc
*p
= vfs_context_proc(ctx
);
1035 devnode_type_t typeinfo
;
1038 struct vnode_attr
* vap
= ap
->a_vap
;
1039 struct vnode
* * vpp
= ap
->a_vpp
;
1041 typeinfo
.Slnk
.name
= ap
->a_target
;
1042 typeinfo
.Slnk
.namelen
= strlen(ap
->a_target
);
1046 dir_p
= VTODN(ap
->a_dvp
);
1048 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
, DEV_SLNK
,
1049 &typeinfo
, NULL
, NULL
, &nm_p
);
1053 dev_p
= nm_p
->de_dnp
;
1054 dev_p
->dn_uid
= dir_p
->dn_uid
;
1055 dev_p
->dn_gid
= dir_p
->dn_gid
;
1056 dev_p
->dn_mode
= vap
->va_mode
;
1057 dn_copy_times(dev_p
, dir_p
);
1059 error
= devfs_dntovn(dev_p
, vpp
, p
);
1070 devfs_mknod(struct vnop_mknod_args
*ap
)
1071 /* struct vnop_mknod_args {
1072 struct vnode *a_dvp;
1073 struct vnode **a_vpp;
1074 struct componentname *a_cnp;
1075 struct vnode_attr *a_vap;
1076 vfs_context_t a_context;
1079 struct componentname
* cnp
= ap
->a_cnp
;
1080 vfs_context_t ctx
= cnp
->cn_context
;
1081 struct proc
*p
= vfs_context_proc(ctx
);
1083 devdirent_t
* devent
;
1084 devnode_t
* dir_p
; /* devnode for parent directory */
1085 struct vnode
* dvp
= ap
->a_dvp
;
1087 devnode_type_t typeinfo
;
1088 struct vnode_attr
* vap
= ap
->a_vap
;
1089 struct vnode
** vpp
= ap
->a_vpp
;
1092 if (!(vap
->va_type
== VBLK
) && !(vap
->va_type
== VCHR
)) {
1093 return (EINVAL
); /* only support mknod of special files */
1095 typeinfo
.dev
= vap
->va_rdev
;
1101 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
,
1102 (vap
->va_type
== VBLK
) ? DEV_BDEV
: DEV_CDEV
,
1103 &typeinfo
, NULL
, NULL
, &devent
);
1107 dev_p
= devent
->de_dnp
;
1108 error
= devfs_dntovn(dev_p
, vpp
, p
);
1111 dev_p
->dn_uid
= vap
->va_uid
;
1112 dev_p
->dn_gid
= vap
->va_gid
;
1113 dev_p
->dn_mode
= vap
->va_mode
;
1114 VATTR_SET_SUPPORTED(vap
, va_uid
);
1115 VATTR_SET_SUPPORTED(vap
, va_gid
);
1116 VATTR_SET_SUPPORTED(vap
, va_mode
);
1124 * Vnode op for readdir
1127 devfs_readdir(struct vnop_readdir_args
*ap
)
1128 /*struct vnop_readdir_args {
1134 vfs_context_t a_context;
1137 struct vnode
*vp
= ap
->a_vp
;
1138 struct uio
*uio
= ap
->a_uio
;
1139 struct dirent dirent
;
1140 devnode_t
* dir_node
;
1141 devdirent_t
* name_node
;
1148 if (ap
->a_flags
& (VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
))
1151 /* set up refs to dir */
1152 dir_node
= VTODN(vp
);
1153 if (dir_node
->dn_type
!= DEV_DIR
)
1156 startpos
= uio
->uio_offset
;
1160 name_node
= dir_node
->dn_typeinfo
.Dir
.dirlist
;
1163 dir_node
->dn_access
= 1;
1165 while ((name_node
|| (nodenumber
< 2)) && (uio_resid(uio
) > 0))
1170 dirent
.d_fileno
= (int32_t)(void *)dir_node
;
1172 dirent
.d_namlen
= 1;
1173 dirent
.d_type
= DT_DIR
;
1176 if(dir_node
->dn_typeinfo
.Dir
.parent
)
1178 = (int32_t)dir_node
->dn_typeinfo
.Dir
.parent
;
1180 dirent
.d_fileno
= (u_int32_t
)dir_node
;
1182 dirent
.d_namlen
= 2;
1183 dirent
.d_type
= DT_DIR
;
1186 dirent
.d_fileno
= (int32_t)(void *)name_node
->de_dnp
;
1187 dirent
.d_namlen
= strlen(name_node
->de_name
);
1188 name
= name_node
->de_name
;
1189 switch(name_node
->de_dnp
->dn_type
) {
1191 dirent
.d_type
= DT_BLK
;
1194 dirent
.d_type
= DT_CHR
;
1197 dirent
.d_type
= DT_DIR
;
1200 dirent
.d_type
= DT_LNK
;
1203 dirent
.d_type
= DT_UNKNOWN
;
1206 #define GENERIC_DIRSIZ(dp) \
1207 ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
1209 reclen
= dirent
.d_reclen
= GENERIC_DIRSIZ(&dirent
);
1211 if(pos
>= startpos
) /* made it to the offset yet? */
1213 if (uio_resid(uio
) < reclen
) /* will it fit? */
1215 strlcpy(dirent
.d_name
, name
, DEVMAXNAMESIZE
);
1216 if ((error
= uiomove ((caddr_t
)&dirent
,
1217 dirent
.d_reclen
, uio
)) != 0)
1221 if((nodenumber
>1) && name_node
)
1222 name_node
= name_node
->de_next
;
1226 uio
->uio_offset
= pos
;
1235 devfs_readlink(struct vnop_readlink_args
*ap
)
1236 /*struct vnop_readlink_args {
1239 vfs_context_t a_context;
1242 struct vnode
*vp
= ap
->a_vp
;
1243 struct uio
*uio
= ap
->a_uio
;
1244 devnode_t
* lnk_node
;
1247 /* set up refs to dir */
1248 lnk_node
= VTODN(vp
);
1250 if (lnk_node
->dn_type
!= DEV_SLNK
) {
1254 error
= uiomove(lnk_node
->dn_typeinfo
.Slnk
.name
,
1255 lnk_node
->dn_typeinfo
.Slnk
.namelen
, uio
);
1261 devfs_reclaim(struct vnop_reclaim_args
*ap
)
1262 /*struct vnop_reclaim_args {
1266 struct vnode
* vp
= ap
->a_vp
;
1275 * do the same as devfs_inactive in case it is not called
1276 * before us (can that ever happen?)
1281 if (dnp
->dn_delete
) {
1292 * Get configurable pathname variables.
1296 struct vnop_pathconf_args
/* {
1300 vfs_context_t a_context;
1303 switch (ap
->a_name
) {
1305 /* arbitrary limit matching HFS; devfs has no hard limit */
1306 *ap
->a_retval
= 32767;
1309 *ap
->a_retval
= DEVMAXNAMESIZE
- 1; /* includes NUL */
1312 *ap
->a_retval
= DEVMAXPATHSIZE
- 1; /* XXX nonconformant */
1314 case _PC_CHOWN_RESTRICTED
:
1315 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
1320 case _PC_CASE_SENSITIVE
:
1323 case _PC_CASE_PRESERVING
:
1335 /**************************************************************************\
1337 \**************************************************************************/
1341 * struct vnop_inactive_args {
1342 * struct vnode *a_vp;
1343 * vfs_context_t a_context;
1348 devfs_inactive(__unused
struct vnop_inactive_args
*ap
)
1354 * called with DEVFS_LOCK held
1357 devfs_update(struct vnode
*vp
, struct timeval
*access
, struct timeval
*modify
)
1363 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1371 dn_times(ip
, access
, modify
, &now
);
1376 #define VOPFUNC int (*)(void *)
1378 /* The following ops are used by directories and symlinks */
1379 int (**devfs_vnodeop_p
)(void *);
1380 static struct vnodeopv_entry_desc devfs_vnodeop_entries
[] = {
1381 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1382 { &vnop_lookup_desc
, (VOPFUNC
)devfs_lookup
}, /* lookup */
1383 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
1384 { &vnop_whiteout_desc
, (VOPFUNC
)err_whiteout
}, /* whiteout */
1385 { &vnop_mknod_desc
, (VOPFUNC
)devfs_mknod
}, /* mknod */
1386 { &vnop_open_desc
, (VOPFUNC
)nop_open
}, /* open */
1387 { &vnop_close_desc
, (VOPFUNC
)devfs_close
}, /* close */
1388 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1389 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1390 { &vnop_read_desc
, (VOPFUNC
)devfs_read
}, /* read */
1391 { &vnop_write_desc
, (VOPFUNC
)devfs_write
}, /* write */
1392 { &vnop_ioctl_desc
, (VOPFUNC
)err_ioctl
}, /* ioctl */
1393 { &vnop_select_desc
, (VOPFUNC
)err_select
}, /* select */
1394 { &vnop_revoke_desc
, (VOPFUNC
)err_revoke
}, /* revoke */
1395 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
1396 { &vnop_fsync_desc
, (VOPFUNC
)nop_fsync
}, /* fsync */
1397 { &vnop_remove_desc
, (VOPFUNC
)devfs_remove
}, /* remove */
1398 { &vnop_link_desc
, (VOPFUNC
)devfs_link
}, /* link */
1399 { &vnop_rename_desc
, (VOPFUNC
)devfs_rename
}, /* rename */
1400 { &vnop_mkdir_desc
, (VOPFUNC
)devfs_mkdir
}, /* mkdir */
1401 { &vnop_rmdir_desc
, (VOPFUNC
)devfs_rmdir
}, /* rmdir */
1402 { &vnop_symlink_desc
, (VOPFUNC
)devfs_symlink
}, /* symlink */
1403 { &vnop_readdir_desc
, (VOPFUNC
)devfs_readdir
}, /* readdir */
1404 { &vnop_readlink_desc
, (VOPFUNC
)devfs_readlink
}, /* readlink */
1405 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1406 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1407 { &vnop_strategy_desc
, (VOPFUNC
)err_strategy
}, /* strategy */
1408 { &vnop_pathconf_desc
, (VOPFUNC
)devs_vnop_pathconf
}, /* pathconf */
1409 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
1410 { &vnop_bwrite_desc
, (VOPFUNC
)err_bwrite
},
1411 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
1412 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
1413 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1414 { &vnop_blktooff_desc
, (VOPFUNC
)err_blktooff
}, /* blktooff */
1415 { &vnop_offtoblk_desc
, (VOPFUNC
)err_offtoblk
}, /* offtoblk */
1416 { &vnop_blockmap_desc
, (VOPFUNC
)err_blockmap
}, /* blockmap */
1418 { &vnop_setlabel_desc
, (VOPFUNC
)devfs_setlabel
}, /* setlabel */
1420 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1422 struct vnodeopv_desc devfs_vnodeop_opv_desc
=
1423 { &devfs_vnodeop_p
, devfs_vnodeop_entries
};
1425 /* The following ops are used by the device nodes */
1426 int (**devfs_spec_vnodeop_p
)(void *);
1427 static struct vnodeopv_entry_desc devfs_spec_vnodeop_entries
[] = {
1428 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1429 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
1430 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
1431 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
1432 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
1433 { &vnop_close_desc
, (VOPFUNC
)devfsspec_close
}, /* close */
1434 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1435 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1436 { &vnop_read_desc
, (VOPFUNC
)devfsspec_read
}, /* read */
1437 { &vnop_write_desc
, (VOPFUNC
)devfsspec_write
}, /* write */
1438 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
1439 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
1440 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
1441 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
1442 { &vnop_fsync_desc
, (VOPFUNC
)spec_fsync
}, /* fsync */
1443 { &vnop_remove_desc
, (VOPFUNC
)devfs_remove
}, /* remove */
1444 { &vnop_link_desc
, (VOPFUNC
)devfs_link
}, /* link */
1445 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
1446 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
1447 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
1448 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
1449 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
1450 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
1451 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1452 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1453 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
1454 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
1455 { &vnop_advlock_desc
, (VOPFUNC
)spec_advlock
}, /* advlock */
1456 { &vnop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
1457 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
1458 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
1459 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1460 { &vnop_blktooff_desc
, (VOPFUNC
)spec_blktooff
}, /* blktooff */
1461 { &vnop_blktooff_desc
, (VOPFUNC
)spec_offtoblk
}, /* blkofftoblk */
1462 { &vnop_blockmap_desc
, (VOPFUNC
)spec_blockmap
}, /* blockmap */
1464 { &vnop_setlabel_desc
, (VOPFUNC
)devfs_setlabel
}, /* setlabel */
1466 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1468 struct vnodeopv_desc devfs_spec_vnodeop_opv_desc
=
1469 { &devfs_spec_vnodeop_p
, devfs_spec_vnodeop_entries
};