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"
113 static int devfs_update(struct vnode
*vp
, struct timeval
*access
,
114 struct timeval
*modify
);
115 void devfs_rele_node(devnode_t
*);
119 * Convert a component of a pathname into a pointer to a locked node.
120 * This is a very central and rather complicated routine.
121 * If the file system is not maintained in a strict tree hierarchy,
122 * this can result in a deadlock situation (see comments in code below).
124 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
125 * whether the name is to be looked up, created, renamed, or deleted.
126 * When CREATE, RENAME, or DELETE is specified, information usable in
127 * creating, renaming, or deleting a directory entry may be calculated.
128 * If flag has LOCKPARENT or'ed into it and the target of the pathname
129 * exists, lookup returns both the target and its parent directory locked.
130 * When creating or renaming and LOCKPARENT is specified, the target may
131 * not be ".". When deleting and LOCKPARENT is specified, the target may
132 * be "."., but the caller must check to ensure it does an vrele and DNUNLOCK
133 * instead of two DNUNLOCKs.
135 * Overall outline of devfs_lookup:
137 * check accessibility of directory
138 * null terminate the component (lookup leaves the whole string alone)
139 * look for name in cache, if found, then if at end of path
140 * and deleting or creating, drop it, else return name
141 * search for name in directory, to found or notfound
143 * if creating, return locked directory,
146 * if at end of path and deleting, return information to allow delete
147 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
148 * node and return info to allow rewrite
149 * if not at end, add name to cache; if at end and neither creating
150 * nor deleting, add name to cache
151 * On return to lookup, remove the null termination we put in at the start.
153 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent node unlocked.
156 devfs_lookup(struct vnop_lookup_args
*ap
)
157 /*struct vnop_lookup_args {
158 struct vnode * a_dvp; directory vnode ptr
159 struct vnode ** a_vpp; where to put the result
160 struct componentname * a_cnp; the name we want
161 vfs_context_t a_context;
164 struct componentname
*cnp
= ap
->a_cnp
;
165 vfs_context_t ctx
= cnp
->cn_context
;
166 struct proc
*p
= vfs_context_proc(ctx
);
167 struct vnode
*dir_vnode
= ap
->a_dvp
;
168 struct vnode
**result_vnode
= ap
->a_vpp
;
169 devnode_t
* dir_node
; /* the directory we are searching */
170 devnode_t
* node
= NULL
; /* the node we are searching for */
171 devdirent_t
* nodename
;
172 int flags
= cnp
->cn_flags
;
173 int op
= cnp
->cn_nameiop
; /* LOOKUP, CREATE, RENAME, or DELETE */
174 int wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
176 char heldchar
; /* the char at the end of the name componet */
180 *result_vnode
= NULL
; /* safe not sorry */ /*XXX*/
182 /* okay to look at directory vnodes ourside devfs lock as they are not aliased */
183 dir_node
= VTODN(dir_vnode
);
186 * Make sure that our node is a directory as well.
188 if (dir_node
->dn_type
!= DEV_DIR
) {
194 * temporarily terminate string component
196 heldchar
= cnp
->cn_nameptr
[cnp
->cn_namelen
];
197 cnp
->cn_nameptr
[cnp
->cn_namelen
] = '\0';
199 nodename
= dev_findname(dir_node
, cnp
->cn_nameptr
);
201 * restore saved character
203 cnp
->cn_nameptr
[cnp
->cn_namelen
] = heldchar
;
207 node
= nodename
->de_dnp
;
209 /* Do potential vnode allocation here inside the lock
210 * to make sure that our device node has a non-NULL dn_vn
211 * associated with it. The device node might otherwise
212 * get deleted out from under us (see devfs_dn_free()).
214 error
= devfs_dntovn(node
, result_vnode
, p
);
225 * we haven't called devfs_dntovn if we get here
226 * we have not taken a reference on the node.. no
227 * vnode_put is necessary on these error returns
229 * If it doesn't exist and we're not the last component,
230 * or we're at the last component, but we're not creating
231 * or renaming, return ENOENT.
233 if (!(flags
& ISLASTCN
) || !(op
== CREATE
|| op
== RENAME
)) {
237 * We return with the directory locked, so that
238 * the parameters we set up above will still be
239 * valid if we actually decide to add a new entry.
240 * We return ni_vp == NULL to indicate that the entry
241 * does not currently exist; we leave a pointer to
242 * the (locked) directory vnode in namei_data->ni_dvp.
244 * NB - if the directory is unlocked, then this
245 * information cannot be used.
247 return (EJUSTRETURN
);
250 * from this point forward, we need to vnode_put the reference
251 * picked up in devfs_dntovn if we decide to return an error
255 * If deleting, and at end of pathname, return
256 * parameters which can be used to remove file.
257 * If the wantparent flag isn't set, we return only
258 * the directory (in namei_data->ni_dvp), otherwise we go
259 * on and lock the node, being careful with ".".
261 if (op
== DELETE
&& (flags
& ISLASTCN
)) {
264 * we are trying to delete '.'. What does this mean? XXX
266 if (dir_node
== node
) {
268 vnode_put(*result_vnode
);
269 *result_vnode
= NULL
;
271 if ( ((error
= vnode_get(dir_vnode
)) == 0) ) {
272 *result_vnode
= dir_vnode
;
280 * If rewriting (RENAME), return the vnode and the
281 * information required to rewrite the present directory
282 * Must get node of directory entry to verify it's a
283 * regular file, or empty directory.
285 if (op
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
288 * Careful about locking second node.
289 * This can only occur if the target is ".".
291 if (dir_node
== node
) {
299 * Step through the translation in the name. We do not unlock the
300 * directory because we may need it again if a symbolic link
301 * is relative to the current directory. Instead we save it
302 * unlocked as "saved_dir_node" XXX. We must get the target
303 * node before unlocking
304 * the directory to insure that the node will not be removed
305 * before we get it. We prevent deadlock by always fetching
306 * nodes from the root, moving down the directory tree. Thus
307 * when following backward pointers ".." we must unlock the
308 * parent directory before getting the requested directory.
309 * There is a potential race condition here if both the current
310 * and parent directories are removed before the lock for the
311 * node associated with ".." returns. We hope that this occurs
312 * infrequently since we cannot avoid this race condition without
313 * implementing a sophisticated deadlock detection algorithm.
314 * Note also that this simple deadlock detection scheme will not
315 * work if the file system has any hard links other than ".."
316 * that point backwards in the directory structure.
318 if ((flags
& ISDOTDOT
) == 0 && dir_node
== node
) {
320 vnode_put(*result_vnode
);
321 *result_vnode
= NULL
;
323 if ( (error
= vnode_get(dir_vnode
)) ) {
326 *result_vnode
= dir_vnode
;
332 vnode_put(*result_vnode
);
333 *result_vnode
= NULL
;
339 devfs_getattr(struct vnop_getattr_args
*ap
)
340 /*struct vnop_getattr_args {
342 struct vnode_attr *a_vap;
347 struct vnode
*vp
= ap
->a_vp
;
348 struct vnode_attr
*vap
= ap
->a_vap
;
349 devnode_t
* file_node
;
354 file_node
= VTODN(vp
);
357 dn_times(file_node
, &now
, &now
, &now
);
359 VATTR_RETURN(vap
, va_mode
, file_node
->dn_mode
);
362 * Note: for DEV_CDEV and DEV_BDEV, we return the device from
363 * the vp, not the file_node; if we getting information on a
364 * cloning device, we want the cloned information, not the template.
366 switch (file_node
->dn_type
)
370 case DEV_DEVFD
: /* Like a directory */
372 VATTR_RETURN(vap
, va_rdev
, 0);
373 vap
->va_mode
|= (S_IFDIR
);
376 VATTR_RETURN(vap
, va_rdev
, vp
->v_rdev
);
377 vap
->va_mode
|= (S_IFCHR
);
380 VATTR_RETURN(vap
, va_rdev
, vp
->v_rdev
);
381 vap
->va_mode
|= (S_IFBLK
);
384 VATTR_RETURN(vap
, va_rdev
, 0);
385 vap
->va_mode
|= (S_IFLNK
);
388 VATTR_RETURN(vap
, va_rdev
, 0); /* default value only */
390 VATTR_RETURN(vap
, va_type
, vp
->v_type
);
391 VATTR_RETURN(vap
, va_nlink
, file_node
->dn_links
);
392 VATTR_RETURN(vap
, va_uid
, file_node
->dn_uid
);
393 VATTR_RETURN(vap
, va_gid
, file_node
->dn_gid
);
394 VATTR_RETURN(vap
, va_fsid
, (uintptr_t)file_node
->dn_dvm
);
395 VATTR_RETURN(vap
, va_fileid
, (uintptr_t)file_node
->dn_ino
);
396 VATTR_RETURN(vap
, va_data_size
, file_node
->dn_len
);
398 /* return an override block size (advisory) */
399 if (vp
->v_type
== VBLK
)
400 VATTR_RETURN(vap
, va_iosize
, BLKDEV_IOSIZE
);
401 else if (vp
->v_type
== VCHR
)
402 VATTR_RETURN(vap
, va_iosize
, MAXPHYSIO
);
404 VATTR_RETURN(vap
, va_iosize
, vp
->v_mount
->mnt_vfsstat
.f_iosize
);
405 /* if the time is bogus, set it to the boot time */
406 if (file_node
->dn_ctime
.tv_sec
== 0) {
407 file_node
->dn_ctime
.tv_sec
= boottime_sec();
408 file_node
->dn_ctime
.tv_nsec
= 0;
410 if (file_node
->dn_mtime
.tv_sec
== 0)
411 file_node
->dn_mtime
= file_node
->dn_ctime
;
412 if (file_node
->dn_atime
.tv_sec
== 0)
413 file_node
->dn_atime
= file_node
->dn_ctime
;
414 VATTR_RETURN(vap
, va_change_time
, file_node
->dn_ctime
);
415 VATTR_RETURN(vap
, va_modify_time
, file_node
->dn_mtime
);
416 VATTR_RETURN(vap
, va_access_time
, file_node
->dn_atime
);
417 VATTR_RETURN(vap
, va_gen
, 0);
418 VATTR_RETURN(vap
, va_filerev
, 0);
419 VATTR_RETURN(vap
, va_acl
, NULL
);
421 /* Hide the root so Finder doesn't display it */
422 if (vnode_isvroot(vp
)) {
423 VATTR_RETURN(vap
, va_flags
, UF_HIDDEN
);
425 VATTR_RETURN(vap
, va_flags
, 0);
434 devfs_setattr(struct vnop_setattr_args
*ap
)
435 /*struct vnop_setattr_args {
437 struct vnode_attr *a_vap;
438 vfs_context_t a_context;
441 struct vnode
*vp
= ap
->a_vp
;
442 struct vnode_attr
*vap
= ap
->a_vap
;
444 devnode_t
* file_node
;
445 struct timeval atimeval
, mtimeval
;
449 file_node
= VTODN(vp
);
451 * Go through the fields and update if set.
453 if (VATTR_IS_ACTIVE(vap
, va_access_time
) || VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
456 if (VATTR_IS_ACTIVE(vap
, va_access_time
))
457 file_node
->dn_access
= 1;
458 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
459 file_node
->dn_change
= 1;
460 file_node
->dn_update
= 1;
462 atimeval
.tv_sec
= vap
->va_access_time
.tv_sec
;
463 atimeval
.tv_usec
= vap
->va_access_time
.tv_nsec
/ 1000;
464 mtimeval
.tv_sec
= vap
->va_modify_time
.tv_sec
;
465 mtimeval
.tv_usec
= vap
->va_modify_time
.tv_nsec
/ 1000;
467 if ( (error
= devfs_update(vp
, &atimeval
, &mtimeval
)) )
470 VATTR_SET_SUPPORTED(vap
, va_access_time
);
471 VATTR_SET_SUPPORTED(vap
, va_change_time
);
474 * Change the permissions.
476 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
477 file_node
->dn_mode
&= ~07777;
478 file_node
->dn_mode
|= vap
->va_mode
& 07777;
480 VATTR_SET_SUPPORTED(vap
, va_mode
);
485 if (VATTR_IS_ACTIVE(vap
, va_uid
))
486 file_node
->dn_uid
= vap
->va_uid
;
487 VATTR_SET_SUPPORTED(vap
, va_uid
);
492 if (VATTR_IS_ACTIVE(vap
, va_gid
))
493 file_node
->dn_gid
= vap
->va_gid
;
494 VATTR_SET_SUPPORTED(vap
, va_gid
);
503 devfs_setlabel(struct vnop_setlabel_args
*ap
)
504 /* struct vnop_setlabel_args {
505 struct vnodeop_desc *a_desc;
508 vfs_context_t a_context;
517 mac_vnode_label_update(ap
->a_context
, vp
, ap
->a_vl
);
518 mac_devfs_label_update(vp
->v_mount
, de
, vp
);
525 devfs_read(struct vnop_read_args
*ap
)
526 /* struct vnop_read_args {
530 vfs_context_t a_context;
533 devnode_t
* dn_p
= VTODN(ap
->a_vp
);
535 switch (ap
->a_vp
->v_type
) {
539 return VNOP_READDIR(ap
->a_vp
, ap
->a_uio
, 0, NULL
, NULL
, ap
->a_context
);
542 printf("devfs_read(): bad file type %d", ap
->a_vp
->v_type
);
547 return (0); /* not reached */
551 devfs_close(struct vnop_close_args
*ap
)
552 /* struct vnop_close_args {
555 vfs_context_t a_context;
558 struct vnode
* vp
= ap
->a_vp
;
559 register devnode_t
* dnp
;
562 if (vnode_isinuse(vp
, 1)) {
566 dn_times(dnp
, &now
, &now
, &now
);
573 devfsspec_close(struct vnop_close_args
*ap
)
574 /* struct vnop_close_args {
577 vfs_context_t a_context;
580 struct vnode
* vp
= ap
->a_vp
;
581 register devnode_t
* dnp
;
584 if (vnode_isinuse(vp
, 0)) {
588 dn_times(dnp
, &now
, &now
, &now
);
592 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
596 devfsspec_read(struct vnop_read_args
*ap
)
597 /* struct vnop_read_args {
604 register devnode_t
* dnp
= VTODN(ap
->a_vp
);
608 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
612 devfsspec_write(struct vnop_write_args
*ap
)
613 /* struct vnop_write_args {
617 vfs_context_t a_context;
620 register devnode_t
* dnp
= VTODN(ap
->a_vp
);
625 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
629 * Write data to a file or directory.
632 devfs_write(struct vnop_write_args
*ap
)
633 /* struct vnop_write_args {
640 switch (ap
->a_vp
->v_type
) {
644 printf("devfs_write(): bad file type %d", ap
->a_vp
->v_type
);
647 return 0; /* not reached */
651 * Deviates from UFS naming convention because there is a KPI function
652 * called devfs_remove().
655 devfs_vnop_remove(struct vnop_remove_args
*ap
)
656 /* struct vnop_remove_args {
659 struct componentname *a_cnp;
662 struct vnode
*vp
= ap
->a_vp
;
663 struct vnode
*dvp
= ap
->a_dvp
;
664 struct componentname
*cnp
= ap
->a_cnp
;
668 int doingdirectory
= 0;
672 * assume that the name is null terminated as they
673 * are the end of the path. Get pointers to all our
682 tnp
= dev_findname(tdp
, cnp
->cn_nameptr
);
690 * Make sure that we don't try do something stupid
692 if ((tp
->dn_type
) == DEV_DIR
) {
694 * Avoid ".", "..", and aliases of "." for obvious reasons.
696 if ( (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.')
697 || (cnp
->cn_flags
&ISDOTDOT
) ) {
704 /***********************************
705 * Start actually doing things.... *
706 ***********************************/
711 * Target must be empty if a directory and have no links
712 * to it. Also, ensure source and target are compatible
713 * (both directories, or both not directories).
715 if (( doingdirectory
) && (tp
->dn_links
> 2)) {
729 devfs_link(struct vnop_link_args
*ap
)
730 /*struct vnop_link_args {
731 struct vnode *a_tdvp;
733 struct componentname *a_cnp;
734 vfs_context_t a_context;
737 struct vnode
*vp
= ap
->a_vp
;
738 struct vnode
*tdvp
= ap
->a_tdvp
;
739 struct componentname
*cnp
= ap
->a_cnp
;
747 * First catch an arbitrary restriction for this FS
749 if (cnp
->cn_namelen
> DEVMAXNAMESIZE
) {
750 error
= ENAMETOOLONG
;
755 * Lock our directories and get our name pointers
756 * assume that the names are null terminated as they
757 * are the end of the path. Get pointers to all our
760 /* can lookup dnode safely for tdvp outside of devfs lock as it is not aliased */
763 if (tdvp
->v_mount
!= vp
->v_mount
) {
770 /***********************************
771 * Start actually doing things.... *
772 ***********************************/
776 error
= devfs_update(vp
, &now
, &now
);
779 error
= dev_add_name(cnp
->cn_nameptr
, tdp
, NULL
, fp
, &tnp
);
788 * Rename system call. Seems overly complicated to me...
789 * rename("foo", "bar");
792 * link("foo", "bar");
794 * but ``atomically''.
796 * When the target exists, both the directory
797 * and target vnodes are locked.
798 * the source and source-parent vnodes are referenced
801 * Basic algorithm is:
803 * 1) Bump link count on source while we're linking it to the
804 * target. This also ensure the inode won't be deleted out
805 * from underneath us while we work (it may be truncated by
806 * a concurrent `trunc' or `open' for creation).
807 * 2) Link source to destination. If destination already exists,
809 * 3) Unlink source reference to node if still around. If a
810 * directory was moved and the parent of the destination
811 * is different from the source, patch the ".." entry in the
815 devfs_rename(struct vnop_rename_args
*ap
)
816 /*struct vnop_rename_args {
817 struct vnode *a_fdvp;
819 struct componentname *a_fcnp;
820 struct vnode *a_tdvp;
822 struct componentname *a_tcnp;
823 vfs_context_t a_context;
826 struct vnode
*tvp
= ap
->a_tvp
;
827 struct vnode
*tdvp
= ap
->a_tdvp
;
828 struct vnode
*fvp
= ap
->a_fvp
;
829 struct vnode
*fdvp
= ap
->a_fdvp
;
830 struct componentname
*tcnp
= ap
->a_tcnp
;
831 struct componentname
*fcnp
= ap
->a_fcnp
;
832 devnode_t
*fp
, *fdp
, *tp
, *tdp
;
833 devdirent_t
*fnp
,*tnp
;
834 int doingdirectory
= 0;
840 * First catch an arbitrary restriction for this FS
842 if (tcnp
->cn_namelen
> DEVMAXNAMESIZE
) {
843 error
= ENAMETOOLONG
;
848 * assume that the names are null terminated as they
849 * are the end of the path. Get pointers to all our
856 fnp
= dev_findname(fdp
, fcnp
->cn_nameptr
);
866 tnp
= dev_findname(tdp
, tcnp
->cn_nameptr
);
876 * Make sure that we don't try do something stupid
878 if ((fp
->dn_type
) == DEV_DIR
) {
880 * Avoid ".", "..", and aliases of "." for obvious reasons.
882 if ((fcnp
->cn_namelen
== 1 && fcnp
->cn_nameptr
[0] == '.')
883 || (fcnp
->cn_flags
&ISDOTDOT
)
884 || (tcnp
->cn_namelen
== 1 && tcnp
->cn_nameptr
[0] == '.')
885 || (tcnp
->cn_flags
&ISDOTDOT
)
894 * If ".." must be changed (ie the directory gets a new
895 * parent) then the source directory must not be in the
896 * directory hierarchy above the target, as this would
897 * orphan everything below the source directory. Also
898 * the user must have write permission in the source so
899 * as to be able to change "..".
901 if (doingdirectory
&& (tdp
!= fdp
)) {
902 devnode_t
* tmp
, *ntmp
;
906 /* XXX unlock stuff here probably */
911 } while ((tmp
= tmp
->dn_typeinfo
.Dir
.parent
) != ntmp
);
914 /***********************************
915 * Start actually doing things.... *
916 ***********************************/
920 if ( (error
= devfs_update(fvp
, &now
, &now
)) ) {
924 * Check if just deleting a link name.
927 if (fvp
->v_type
== VDIR
) {
931 /* Release destination completely. */
938 * 1) Bump link count while we're moving stuff
939 * around. If we crash somewhere before
940 * completing our work, too bad :)
944 * If the target exists zap it (unless it's a non-empty directory)
945 * We could do that as well but won't
949 * Target must be empty if a directory and have no links
950 * to it. Also, ensure source and target are compatible
951 * (both directories, or both not directories).
953 if (( doingdirectory
) && (tp
->dn_links
> 2)) {
960 dev_add_name(tcnp
->cn_nameptr
,tdp
,NULL
,fp
,&tnp
);
962 fp
->dn_links
--; /* one less link to it.. */
966 fp
->dn_links
--; /* we added one earlier*/
973 devfs_mkdir(struct vnop_mkdir_args
*ap
)
974 /*struct vnop_mkdir_args {
976 struct vnode **a_vpp;
977 struct componentname *a_cnp;
978 struct vnode_attr *a_vap;
979 vfs_context_t a_context;
982 struct componentname
* cnp
= ap
->a_cnp
;
983 vfs_context_t ctx
= cnp
->cn_context
;
984 struct proc
*p
= vfs_context_proc(ctx
);
989 struct vnode_attr
* vap
= ap
->a_vap
;
990 struct vnode
* * vpp
= ap
->a_vpp
;
994 dir_p
= VTODN(ap
->a_dvp
);
995 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
, DEV_DIR
,
996 NULL
, NULL
, NULL
, &nm_p
);
1000 dev_p
= nm_p
->de_dnp
;
1001 dev_p
->dn_uid
= dir_p
->dn_uid
;
1002 dev_p
->dn_gid
= dir_p
->dn_gid
;
1003 dev_p
->dn_mode
= vap
->va_mode
;
1004 dn_copy_times(dev_p
, dir_p
);
1006 error
= devfs_dntovn(dev_p
, vpp
, p
);
1014 * An rmdir is a special type of remove, which we already support; we wrap
1015 * and reexpress the arguments to call devfs_remove directly. The only
1016 * different argument is flags, which we do not set, since it's ignored.
1019 devfs_rmdir(struct vnop_rmdir_args
*ap
)
1020 /* struct vnop_rmdir_args {
1021 struct vnode *a_dvp;
1023 struct componentname *a_cnp;
1024 vfs_context_t a_context;
1027 struct vnop_remove_args ra
;
1029 ra
.a_dvp
= ap
->a_dvp
;
1031 ra
.a_cnp
= ap
->a_cnp
;
1032 ra
.a_flags
= 0; /* XXX */
1033 ra
.a_context
= ap
->a_context
;
1035 return devfs_vnop_remove(&ra
);
1040 devfs_symlink(struct vnop_symlink_args
*ap
)
1041 /*struct vnop_symlink_args {
1042 struct vnode *a_dvp;
1043 struct vnode **a_vpp;
1044 struct componentname *a_cnp;
1045 struct vnode_attr *a_vap;
1047 vfs_context_t a_context;
1051 devdirent_t
*newent
;
1054 error
= devfs_make_symlink(VTODN(ap
->a_dvp
), ap
->a_cnp
->cn_nameptr
, ap
->a_vap
->va_mode
, ap
->a_target
, &newent
);
1057 error
= devfs_dntovn(newent
->de_dnp
, ap
->a_vpp
, vfs_context_proc(ap
->a_context
));
1066 /* Called with devfs locked */
1068 devfs_make_symlink(devnode_t
*dir_p
, char *name
, int mode
, char *target
, devdirent_t
**newent
)
1071 devnode_type_t typeinfo
;
1075 typeinfo
.Slnk
.name
= target
;
1076 typeinfo
.Slnk
.namelen
= strlen(target
);
1078 error
= dev_add_entry(name
, dir_p
, DEV_SLNK
,
1079 &typeinfo
, NULL
, NULL
, &nm_p
);
1083 dev_p
= nm_p
->de_dnp
;
1084 dev_p
->dn_uid
= dir_p
->dn_uid
;
1085 dev_p
->dn_gid
= dir_p
->dn_gid
;
1086 dev_p
->dn_mode
= mode
;
1087 dn_copy_times(dev_p
, dir_p
);
1102 devfs_mknod(struct vnop_mknod_args
*ap
)
1103 /* struct vnop_mknod_args {
1104 struct vnode *a_dvp;
1105 struct vnode **a_vpp;
1106 struct componentname *a_cnp;
1107 struct vnode_attr *a_vap;
1108 vfs_context_t a_context;
1111 struct componentname
* cnp
= ap
->a_cnp
;
1112 vfs_context_t ctx
= cnp
->cn_context
;
1113 struct proc
*p
= vfs_context_proc(ctx
);
1115 devdirent_t
* devent
;
1116 devnode_t
* dir_p
; /* devnode for parent directory */
1117 struct vnode
* dvp
= ap
->a_dvp
;
1119 devnode_type_t typeinfo
;
1120 struct vnode_attr
* vap
= ap
->a_vap
;
1121 struct vnode
** vpp
= ap
->a_vpp
;
1124 if (!(vap
->va_type
== VBLK
) && !(vap
->va_type
== VCHR
)) {
1125 return (EINVAL
); /* only support mknod of special files */
1127 typeinfo
.dev
= vap
->va_rdev
;
1133 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
,
1134 (vap
->va_type
== VBLK
) ? DEV_BDEV
: DEV_CDEV
,
1135 &typeinfo
, NULL
, NULL
, &devent
);
1139 dev_p
= devent
->de_dnp
;
1140 error
= devfs_dntovn(dev_p
, vpp
, p
);
1143 dev_p
->dn_uid
= vap
->va_uid
;
1144 dev_p
->dn_gid
= vap
->va_gid
;
1145 dev_p
->dn_mode
= vap
->va_mode
;
1146 VATTR_SET_SUPPORTED(vap
, va_uid
);
1147 VATTR_SET_SUPPORTED(vap
, va_gid
);
1148 VATTR_SET_SUPPORTED(vap
, va_mode
);
1156 * Vnode op for readdir
1159 devfs_readdir(struct vnop_readdir_args
*ap
)
1160 /*struct vnop_readdir_args {
1166 vfs_context_t a_context;
1169 struct vnode
*vp
= ap
->a_vp
;
1170 struct uio
*uio
= ap
->a_uio
;
1171 struct dirent dirent
;
1172 devnode_t
* dir_node
;
1173 devdirent_t
* name_node
;
1180 if (ap
->a_flags
& (VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
))
1183 /* set up refs to dir */
1184 dir_node
= VTODN(vp
);
1185 if (dir_node
->dn_type
!= DEV_DIR
)
1188 startpos
= uio
->uio_offset
;
1192 name_node
= dir_node
->dn_typeinfo
.Dir
.dirlist
;
1195 dir_node
->dn_access
= 1;
1197 while ((name_node
|| (nodenumber
< 2)) && (uio_resid(uio
) > 0))
1202 dirent
.d_fileno
= dir_node
->dn_ino
;
1204 dirent
.d_namlen
= 1;
1205 dirent
.d_type
= DT_DIR
;
1208 if(dir_node
->dn_typeinfo
.Dir
.parent
)
1209 dirent
.d_fileno
= dir_node
->dn_typeinfo
.Dir
.parent
->dn_ino
;
1211 dirent
.d_fileno
= dir_node
->dn_ino
;
1213 dirent
.d_namlen
= 2;
1214 dirent
.d_type
= DT_DIR
;
1217 dirent
.d_fileno
= name_node
->de_dnp
->dn_ino
;
1218 dirent
.d_namlen
= strlen(name_node
->de_name
);
1219 name
= name_node
->de_name
;
1220 switch(name_node
->de_dnp
->dn_type
) {
1222 dirent
.d_type
= DT_BLK
;
1225 dirent
.d_type
= DT_CHR
;
1228 dirent
.d_type
= DT_DIR
;
1231 dirent
.d_type
= DT_LNK
;
1234 dirent
.d_type
= DT_UNKNOWN
;
1237 #define GENERIC_DIRSIZ(dp) \
1238 ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
1240 reclen
= dirent
.d_reclen
= GENERIC_DIRSIZ(&dirent
);
1242 if(pos
>= startpos
) /* made it to the offset yet? */
1244 if (uio_resid(uio
) < reclen
) /* will it fit? */
1246 strlcpy(dirent
.d_name
, name
, DEVMAXNAMESIZE
);
1247 if ((error
= uiomove ((caddr_t
)&dirent
,
1248 dirent
.d_reclen
, uio
)) != 0)
1252 if((nodenumber
>1) && name_node
)
1253 name_node
= name_node
->de_next
;
1257 uio
->uio_offset
= pos
;
1266 devfs_readlink(struct vnop_readlink_args
*ap
)
1267 /*struct vnop_readlink_args {
1270 vfs_context_t a_context;
1273 struct vnode
*vp
= ap
->a_vp
;
1274 struct uio
*uio
= ap
->a_uio
;
1275 devnode_t
* lnk_node
;
1278 /* set up refs to dir */
1279 lnk_node
= VTODN(vp
);
1281 if (lnk_node
->dn_type
!= DEV_SLNK
) {
1285 error
= uiomove(lnk_node
->dn_typeinfo
.Slnk
.name
,
1286 lnk_node
->dn_typeinfo
.Slnk
.namelen
, uio
);
1292 devfs_reclaim(struct vnop_reclaim_args
*ap
)
1293 /*struct vnop_reclaim_args {
1297 struct vnode
* vp
= ap
->a_vp
;
1305 /* If this is a cloning device, it didn't have a dn_vn anyway */
1307 vnode_clearfsnode(vp
);
1309 /* This could delete the node, if we are the last vnode */
1310 devfs_rele_node(dnp
);
1319 * Get configurable pathname variables.
1323 struct vnop_pathconf_args
/* {
1327 vfs_context_t a_context;
1330 switch (ap
->a_name
) {
1332 /* arbitrary limit matching HFS; devfs has no hard limit */
1333 *ap
->a_retval
= 32767;
1336 *ap
->a_retval
= DEVMAXNAMESIZE
- 1; /* includes NUL */
1339 *ap
->a_retval
= DEVMAXPATHSIZE
- 1; /* XXX nonconformant */
1341 case _PC_CHOWN_RESTRICTED
:
1342 *ap
->a_retval
= 200112; /* _POSIX_CHOWN_RESTRICTED */
1347 case _PC_CASE_SENSITIVE
:
1350 case _PC_CASE_PRESERVING
:
1362 /**************************************************************************\
1364 \**************************************************************************/
1368 * struct vnop_inactive_args {
1369 * struct vnode *a_vp;
1370 * vfs_context_t a_context;
1375 devfs_inactive(__unused
struct vnop_inactive_args
*ap
)
1377 vnode_t vp
= ap
->a_vp
;
1378 devnode_t
*dnp
= VTODN(vp
);
1381 * Cloned vnodes are not linked in anywhere, so they
1382 * can just be recycled.
1384 if (dnp
->dn_clone
!= NULL
) {
1392 * called with DEVFS_LOCK held
1395 devfs_update(struct vnode
*vp
, struct timeval
*access
, struct timeval
*modify
)
1401 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1409 dn_times(ip
, access
, modify
, &now
);
1414 #define VOPFUNC int (*)(void *)
1416 /* The following ops are used by directories and symlinks */
1417 int (**devfs_vnodeop_p
)(void *);
1418 static struct vnodeopv_entry_desc devfs_vnodeop_entries
[] = {
1419 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1420 { &vnop_lookup_desc
, (VOPFUNC
)devfs_lookup
}, /* lookup */
1421 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
1422 { &vnop_whiteout_desc
, (VOPFUNC
)err_whiteout
}, /* whiteout */
1423 { &vnop_mknod_desc
, (VOPFUNC
)devfs_mknod
}, /* mknod */
1424 { &vnop_open_desc
, (VOPFUNC
)nop_open
}, /* open */
1425 { &vnop_close_desc
, (VOPFUNC
)devfs_close
}, /* close */
1426 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1427 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1428 { &vnop_read_desc
, (VOPFUNC
)devfs_read
}, /* read */
1429 { &vnop_write_desc
, (VOPFUNC
)devfs_write
}, /* write */
1430 { &vnop_ioctl_desc
, (VOPFUNC
)err_ioctl
}, /* ioctl */
1431 { &vnop_select_desc
, (VOPFUNC
)err_select
}, /* select */
1432 { &vnop_revoke_desc
, (VOPFUNC
)err_revoke
}, /* revoke */
1433 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
1434 { &vnop_fsync_desc
, (VOPFUNC
)nop_fsync
}, /* fsync */
1435 { &vnop_remove_desc
, (VOPFUNC
)devfs_vnop_remove
}, /* remove */
1436 { &vnop_link_desc
, (VOPFUNC
)devfs_link
}, /* link */
1437 { &vnop_rename_desc
, (VOPFUNC
)devfs_rename
}, /* rename */
1438 { &vnop_mkdir_desc
, (VOPFUNC
)devfs_mkdir
}, /* mkdir */
1439 { &vnop_rmdir_desc
, (VOPFUNC
)devfs_rmdir
}, /* rmdir */
1440 { &vnop_symlink_desc
, (VOPFUNC
)devfs_symlink
}, /* symlink */
1441 { &vnop_readdir_desc
, (VOPFUNC
)devfs_readdir
}, /* readdir */
1442 { &vnop_readlink_desc
, (VOPFUNC
)devfs_readlink
}, /* readlink */
1443 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1444 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1445 { &vnop_strategy_desc
, (VOPFUNC
)err_strategy
}, /* strategy */
1446 { &vnop_pathconf_desc
, (VOPFUNC
)devs_vnop_pathconf
}, /* pathconf */
1447 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
1448 { &vnop_bwrite_desc
, (VOPFUNC
)err_bwrite
},
1449 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
1450 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
1451 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1452 { &vnop_blktooff_desc
, (VOPFUNC
)err_blktooff
}, /* blktooff */
1453 { &vnop_offtoblk_desc
, (VOPFUNC
)err_offtoblk
}, /* offtoblk */
1454 { &vnop_blockmap_desc
, (VOPFUNC
)err_blockmap
}, /* blockmap */
1456 { &vnop_setlabel_desc
, (VOPFUNC
)devfs_setlabel
}, /* setlabel */
1458 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1460 struct vnodeopv_desc devfs_vnodeop_opv_desc
=
1461 { &devfs_vnodeop_p
, devfs_vnodeop_entries
};
1463 /* The following ops are used by the device nodes */
1464 int (**devfs_spec_vnodeop_p
)(void *);
1465 static struct vnodeopv_entry_desc devfs_spec_vnodeop_entries
[] = {
1466 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1467 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
1468 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
1469 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
1470 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
1471 { &vnop_close_desc
, (VOPFUNC
)devfsspec_close
}, /* close */
1472 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1473 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1474 { &vnop_read_desc
, (VOPFUNC
)devfsspec_read
}, /* read */
1475 { &vnop_write_desc
, (VOPFUNC
)devfsspec_write
}, /* write */
1476 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
1477 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
1478 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
1479 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
1480 { &vnop_fsync_desc
, (VOPFUNC
)spec_fsync
}, /* fsync */
1481 { &vnop_remove_desc
, (VOPFUNC
)devfs_vnop_remove
}, /* remove */
1482 { &vnop_link_desc
, (VOPFUNC
)devfs_link
}, /* link */
1483 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
1484 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
1485 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
1486 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
1487 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
1488 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
1489 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1490 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1491 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
1492 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
1493 { &vnop_advlock_desc
, (VOPFUNC
)spec_advlock
}, /* advlock */
1494 { &vnop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
1495 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
1496 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
1497 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1498 { &vnop_blktooff_desc
, (VOPFUNC
)spec_blktooff
}, /* blktooff */
1499 { &vnop_blktooff_desc
, (VOPFUNC
)spec_offtoblk
}, /* blkofftoblk */
1500 { &vnop_blockmap_desc
, (VOPFUNC
)spec_blockmap
}, /* blockmap */
1502 { &vnop_setlabel_desc
, (VOPFUNC
)devfs_setlabel
}, /* setlabel */
1504 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1506 struct vnodeopv_desc devfs_spec_vnodeop_opv_desc
=
1507 { &devfs_spec_vnodeop_p
, devfs_spec_vnodeop_entries
};
1511 int (**devfs_devfd_vnodeop_p
)(void*);
1512 static struct vnodeopv_entry_desc devfs_devfd_vnodeop_entries
[] = {
1513 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1514 { &vnop_lookup_desc
, (VOPFUNC
)devfs_devfd_lookup
}, /* lookup */
1515 { &vnop_open_desc
, (VOPFUNC
)nop_open
}, /* open */
1516 { &vnop_close_desc
, (VOPFUNC
)devfs_close
}, /* close */
1517 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1518 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1519 { &vnop_revoke_desc
, (VOPFUNC
)err_revoke
}, /* revoke */
1520 { &vnop_fsync_desc
, (VOPFUNC
)nop_fsync
}, /* fsync */
1521 { &vnop_readdir_desc
, (VOPFUNC
)devfs_devfd_readdir
}, /* readdir */
1522 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1523 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1524 { &vnop_pathconf_desc
, (VOPFUNC
)devs_vnop_pathconf
}, /* pathconf */
1526 { &vnop_setlabel_desc
, (VOPFUNC
)devfs_setlabel
}, /* setlabel */
1528 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1530 struct vnodeopv_desc devfs_devfd_vnodeop_opv_desc
=
1531 { &devfs_devfd_vnodeop_p
, devfs_devfd_vnodeop_entries
};