2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
24 * Copyright 1997,1998 Julian Elischer. All rights reserved.
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions are
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright notice,
33 * this list of conditions and the following disclaimer in the documentation
34 * and/or other materials provided with the distribution.
36 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER ``AS IS'' AND ANY EXPRESS
37 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
38 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39 * DISCLAIMED. IN NO EVENT SHALL THE HOLDER OR CONTRIBUTORS BE LIABLE FOR
40 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
42 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
43 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
44 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
45 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * Clark Warner (warner_c@apple.com) Tue Feb 10 2000
54 * - Added err_copyfile to the vnode operations table
55 * Dieter Siegmund (dieter@apple.com) Thu Apr 8 14:08:19 PDT 1999
56 * - instead of duplicating specfs here, created a vnode-ops table
57 * that redirects most operations to specfs (as is done with ufs);
58 * - removed routines that made no sense
59 * - cleaned up reclaim: replaced devfs_vntodn() with a macro VTODN()
60 * - cleaned up symlink, link locking
61 * - added the devfs_lock to protect devfs data structures against
62 * driver's calling devfs_add_devswf()/etc.
63 * Dieter Siegmund (dieter@apple.com) Wed Jul 14 13:37:59 PDT 1999
64 * - free the devfs devnode in devfs_inactive(), not just in devfs_reclaim()
65 * to free up kernel memory as soon as it's available
66 * - got rid of devfsspec_{read, write}
67 * Dieter Siegmund (dieter@apple.com) Fri Sep 17 09:58:38 PDT 1999
68 * - update the mod/access times
71 #include <sys/param.h>
72 #include <sys/systm.h>
73 #include <sys/namei.h>
74 #include <sys/kernel.h>
75 #include <sys/fcntl.h>
77 #include <sys/disklabel.h>
80 #include <sys/mount_internal.h>
82 #include <sys/kauth.h>
84 #include <sys/vnode_internal.h>
85 #include <miscfs/specfs/specdev.h>
86 #include <sys/dirent.h>
87 #include <sys/vmmeter.h>
89 #include <sys/uio_internal.h>
91 #include "devfsdefs.h"
93 static int devfs_update(struct vnode
*vp
, struct timeval
*access
,
94 struct timeval
*modify
);
98 * Convert a component of a pathname into a pointer to a locked node.
99 * This is a very central and rather complicated routine.
100 * If the file system is not maintained in a strict tree hierarchy,
101 * this can result in a deadlock situation (see comments in code below).
103 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
104 * whether the name is to be looked up, created, renamed, or deleted.
105 * When CREATE, RENAME, or DELETE is specified, information usable in
106 * creating, renaming, or deleting a directory entry may be calculated.
107 * If flag has LOCKPARENT or'ed into it and the target of the pathname
108 * exists, lookup returns both the target and its parent directory locked.
109 * When creating or renaming and LOCKPARENT is specified, the target may
110 * not be ".". When deleting and LOCKPARENT is specified, the target may
111 * be "."., but the caller must check to ensure it does an vrele and DNUNLOCK
112 * instead of two DNUNLOCKs.
114 * Overall outline of devfs_lookup:
116 * check accessibility of directory
117 * null terminate the component (lookup leaves the whole string alone)
118 * look for name in cache, if found, then if at end of path
119 * and deleting or creating, drop it, else return name
120 * search for name in directory, to found or notfound
122 * if creating, return locked directory,
125 * if at end of path and deleting, return information to allow delete
126 * if at end of path and rewriting (RENAME and LOCKPARENT), lock target
127 * node and return info to allow rewrite
128 * if not at end, add name to cache; if at end and neither creating
129 * nor deleting, add name to cache
130 * On return to lookup, remove the null termination we put in at the start.
132 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent node unlocked.
135 devfs_lookup(struct vnop_lookup_args
*ap
)
136 /*struct vnop_lookup_args {
137 struct vnode * a_dvp; directory vnode ptr
138 struct vnode ** a_vpp; where to put the result
139 struct componentname * a_cnp; the name we want
140 vfs_context_t a_context;
143 struct componentname
*cnp
= ap
->a_cnp
;
144 vfs_context_t ctx
= cnp
->cn_context
;
145 struct proc
*p
= vfs_context_proc(ctx
);
146 struct vnode
*dir_vnode
= ap
->a_dvp
;
147 struct vnode
**result_vnode
= ap
->a_vpp
;
148 devnode_t
* dir_node
; /* the directory we are searching */
149 devnode_t
* node
= NULL
; /* the node we are searching for */
150 devdirent_t
* nodename
;
151 int flags
= cnp
->cn_flags
;
152 int op
= cnp
->cn_nameiop
; /* LOOKUP, CREATE, RENAME, or DELETE */
153 int wantparent
= flags
& (LOCKPARENT
|WANTPARENT
);
155 char heldchar
; /* the char at the end of the name componet */
159 *result_vnode
= NULL
; /* safe not sorry */ /*XXX*/
161 //if (dir_vnode->v_usecount == 0)
162 //printf("devfs_lookup: dir had no refs ");
163 dir_node
= VTODN(dir_vnode
);
166 * Make sure that our node is a directory as well.
168 if (dir_node
->dn_type
!= DEV_DIR
) {
174 * temporarily terminate string component
176 heldchar
= cnp
->cn_nameptr
[cnp
->cn_namelen
];
177 cnp
->cn_nameptr
[cnp
->cn_namelen
] = '\0';
179 nodename
= dev_findname(dir_node
, cnp
->cn_nameptr
);
181 * restore saved character
183 cnp
->cn_nameptr
[cnp
->cn_namelen
] = heldchar
;
187 node
= nodename
->de_dnp
;
189 /* Do potential vnode allocation here inside the lock
190 * to make sure that our device node has a non-NULL dn_vn
191 * associated with it. The device node might otherwise
192 * get deleted out from under us (see devfs_dn_free()).
194 error
= devfs_dntovn(node
, result_vnode
, p
);
205 * we haven't called devfs_dntovn if we get here
206 * we have not taken a reference on the node.. no
207 * vnode_put is necessary on these error returns
209 * If it doesn't exist and we're not the last component,
210 * or we're at the last component, but we're not creating
211 * or renaming, return ENOENT.
213 if (!(flags
& ISLASTCN
) || !(op
== CREATE
|| op
== RENAME
)) {
217 * We return with the directory locked, so that
218 * the parameters we set up above will still be
219 * valid if we actually decide to add a new entry.
220 * We return ni_vp == NULL to indicate that the entry
221 * does not currently exist; we leave a pointer to
222 * the (locked) directory vnode in namei_data->ni_dvp.
224 * NB - if the directory is unlocked, then this
225 * information cannot be used.
227 return (EJUSTRETURN
);
230 * from this point forward, we need to vnode_put the reference
231 * picked up in devfs_dntovn if we decide to return an error
235 * If deleting, and at end of pathname, return
236 * parameters which can be used to remove file.
237 * If the wantparent flag isn't set, we return only
238 * the directory (in namei_data->ni_dvp), otherwise we go
239 * on and lock the node, being careful with ".".
241 if (op
== DELETE
&& (flags
& ISLASTCN
)) {
244 * we are trying to delete '.'. What does this mean? XXX
246 if (dir_node
== node
) {
248 vnode_put(*result_vnode
);
249 *result_vnode
= NULL
;
251 if ( ((error
= vnode_get(dir_vnode
)) == 0) ) {
252 *result_vnode
= dir_vnode
;
260 * If rewriting (RENAME), return the vnode and the
261 * information required to rewrite the present directory
262 * Must get node of directory entry to verify it's a
263 * regular file, or empty directory.
265 if (op
== RENAME
&& wantparent
&& (flags
& ISLASTCN
)) {
268 * Careful about locking second node.
269 * This can only occur if the target is ".".
271 if (dir_node
== node
) {
279 * Step through the translation in the name. We do not unlock the
280 * directory because we may need it again if a symbolic link
281 * is relative to the current directory. Instead we save it
282 * unlocked as "saved_dir_node" XXX. We must get the target
283 * node before unlocking
284 * the directory to insure that the node will not be removed
285 * before we get it. We prevent deadlock by always fetching
286 * nodes from the root, moving down the directory tree. Thus
287 * when following backward pointers ".." we must unlock the
288 * parent directory before getting the requested directory.
289 * There is a potential race condition here if both the current
290 * and parent directories are removed before the lock for the
291 * node associated with ".." returns. We hope that this occurs
292 * infrequently since we cannot avoid this race condition without
293 * implementing a sophisticated deadlock detection algorithm.
294 * Note also that this simple deadlock detection scheme will not
295 * work if the file system has any hard links other than ".."
296 * that point backwards in the directory structure.
298 if ((flags
& ISDOTDOT
) == 0 && dir_node
== node
) {
300 vnode_put(*result_vnode
);
301 *result_vnode
= NULL
;
303 if ( (error
= vnode_get(dir_vnode
)) ) {
306 *result_vnode
= dir_vnode
;
312 vnode_put(*result_vnode
);
313 *result_vnode
= NULL
;
319 devfs_getattr(struct vnop_getattr_args
*ap
)
320 /*struct vnop_getattr_args {
322 struct vnode_attr *a_vap;
327 struct vnode
*vp
= ap
->a_vp
;
328 struct vnode_attr
*vap
= ap
->a_vap
;
329 devnode_t
* file_node
;
332 file_node
= VTODN(vp
);
337 dn_times(file_node
, &now
, &now
, &now
);
339 VATTR_RETURN(vap
, va_mode
, file_node
->dn_mode
);
341 switch (file_node
->dn_type
)
344 VATTR_RETURN(vap
, va_rdev
, (dev_t
)file_node
->dn_dvm
);
345 vap
->va_mode
|= (S_IFDIR
);
348 VATTR_RETURN(vap
, va_rdev
, file_node
->dn_typeinfo
.dev
);
349 vap
->va_mode
|= (S_IFCHR
);
352 VATTR_RETURN(vap
, va_rdev
, file_node
->dn_typeinfo
.dev
);
353 vap
->va_mode
|= (S_IFBLK
);
356 VATTR_RETURN(vap
, va_rdev
, 0);
357 vap
->va_mode
|= (S_IFLNK
);
360 VATTR_RETURN(vap
, va_rdev
, 0); /* default value only */
362 VATTR_RETURN(vap
, va_type
, vp
->v_type
);
363 VATTR_RETURN(vap
, va_nlink
, file_node
->dn_links
);
364 VATTR_RETURN(vap
, va_uid
, file_node
->dn_uid
);
365 VATTR_RETURN(vap
, va_gid
, file_node
->dn_gid
);
366 VATTR_RETURN(vap
, va_fsid
, (uintptr_t)file_node
->dn_dvm
);
367 VATTR_RETURN(vap
, va_fileid
, (uintptr_t)file_node
);
368 VATTR_RETURN(vap
, va_data_size
, file_node
->dn_len
);
370 /* return an override block size (advisory) */
371 if (vp
->v_type
== VBLK
)
372 VATTR_RETURN(vap
, va_iosize
, BLKDEV_IOSIZE
);
373 else if (vp
->v_type
== VCHR
)
374 VATTR_RETURN(vap
, va_iosize
, MAXPHYSIO
);
376 VATTR_RETURN(vap
, va_iosize
, vp
->v_mount
->mnt_vfsstat
.f_iosize
);
377 /* if the time is bogus, set it to the boot time */
378 if (file_node
->dn_ctime
.tv_sec
== 0) {
379 file_node
->dn_ctime
.tv_sec
= boottime_sec();
380 file_node
->dn_ctime
.tv_nsec
= 0;
382 if (file_node
->dn_mtime
.tv_sec
== 0)
383 file_node
->dn_mtime
= file_node
->dn_ctime
;
384 if (file_node
->dn_atime
.tv_sec
== 0)
385 file_node
->dn_atime
= file_node
->dn_ctime
;
386 VATTR_RETURN(vap
, va_change_time
, file_node
->dn_ctime
);
387 VATTR_RETURN(vap
, va_modify_time
, file_node
->dn_mtime
);
388 VATTR_RETURN(vap
, va_access_time
, file_node
->dn_atime
);
389 VATTR_RETURN(vap
, va_gen
, 0);
390 VATTR_RETURN(vap
, va_flags
, 0);
391 VATTR_RETURN(vap
, va_filerev
, 0);
392 VATTR_RETURN(vap
, va_acl
, NULL
);
400 devfs_setattr(struct vnop_setattr_args
*ap
)
401 /*struct vnop_setattr_args {
403 struct vnode_attr *a_vap;
404 vfs_context_t a_context;
407 struct vnode
*vp
= ap
->a_vp
;
408 struct vnode_attr
*vap
= ap
->a_vap
;
409 kauth_cred_t cred
= vfs_context_ucred(ap
->a_context
);
410 struct proc
*p
= vfs_context_proc(ap
->a_context
);
412 devnode_t
* file_node
;
413 struct timeval atimeval
, mtimeval
;
415 file_node
= VTODN(vp
);
419 * Go through the fields and update if set.
421 if (VATTR_IS_ACTIVE(vap
, va_access_time
) || VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
424 if (VATTR_IS_ACTIVE(vap
, va_access_time
))
425 file_node
->dn_access
= 1;
426 if (VATTR_IS_ACTIVE(vap
, va_modify_time
)) {
427 file_node
->dn_change
= 1;
428 file_node
->dn_update
= 1;
430 atimeval
.tv_sec
= vap
->va_access_time
.tv_sec
;
431 atimeval
.tv_usec
= vap
->va_access_time
.tv_nsec
/ 1000;
432 mtimeval
.tv_sec
= vap
->va_modify_time
.tv_sec
;
433 mtimeval
.tv_usec
= vap
->va_modify_time
.tv_nsec
/ 1000;
435 if ( (error
= devfs_update(vp
, &atimeval
, &mtimeval
)) )
438 VATTR_SET_SUPPORTED(vap
, va_access_time
);
439 VATTR_SET_SUPPORTED(vap
, va_change_time
);
442 * Change the permissions.
444 if (VATTR_IS_ACTIVE(vap
, va_mode
)) {
445 file_node
->dn_mode
&= ~07777;
446 file_node
->dn_mode
|= vap
->va_mode
& 07777;
448 VATTR_SET_SUPPORTED(vap
, va_mode
);
453 if (VATTR_IS_ACTIVE(vap
, va_uid
))
454 file_node
->dn_uid
= vap
->va_uid
;
455 VATTR_SET_SUPPORTED(vap
, va_uid
);
460 if (VATTR_IS_ACTIVE(vap
, va_gid
))
461 file_node
->dn_gid
= vap
->va_gid
;
462 VATTR_SET_SUPPORTED(vap
, va_gid
);
470 devfs_read(struct vnop_read_args
*ap
)
471 /* struct vnop_read_args {
475 vfs_context_t a_context;
478 devnode_t
* dn_p
= VTODN(ap
->a_vp
);
480 switch (ap
->a_vp
->v_type
) {
484 return VNOP_READDIR(ap
->a_vp
, ap
->a_uio
, 0, NULL
, NULL
, ap
->a_context
);
487 printf("devfs_read(): bad file type %d", ap
->a_vp
->v_type
);
492 return (0); /* not reached */
496 devfs_close(struct vnop_close_args
*ap
)
497 /* struct vnop_close_args {
500 vfs_context_t a_context;
503 struct vnode
* vp
= ap
->a_vp
;
504 register devnode_t
* dnp
= VTODN(vp
);
507 if (vnode_isinuse(vp
, 1)) {
510 dn_times(dnp
, &now
, &now
, &now
);
517 devfsspec_close(struct vnop_close_args
*ap
)
518 /* struct vnop_close_args {
521 vfs_context_t a_context;
524 struct vnode
* vp
= ap
->a_vp
;
525 register devnode_t
* dnp
= VTODN(vp
);
528 if (vnode_isinuse(vp
, 1)) {
531 dn_times(dnp
, &now
, &now
, &now
);
534 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_close
), ap
));
538 devfsspec_read(struct vnop_read_args
*ap
)
539 /* struct vnop_read_args {
546 register devnode_t
* dnp
= VTODN(ap
->a_vp
);
550 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_read
), ap
));
554 devfsspec_write(struct vnop_write_args
*ap
)
555 /* struct vnop_write_args {
559 vfs_context_t a_context;
562 register devnode_t
* dnp
= VTODN(ap
->a_vp
);
567 return (VOCALL (spec_vnodeop_p
, VOFFSET(vnop_write
), ap
));
571 * Write data to a file or directory.
574 devfs_write(struct vnop_write_args
*ap
)
575 /* struct vnop_write_args {
582 switch (ap
->a_vp
->v_type
) {
586 printf("devfs_write(): bad file type %d", ap
->a_vp
->v_type
);
589 return 0; /* not reached */
593 devfs_remove(struct vnop_remove_args
*ap
)
594 /* struct vnop_remove_args {
597 struct componentname *a_cnp;
600 struct vnode
*vp
= ap
->a_vp
;
601 struct vnode
*dvp
= ap
->a_dvp
;
602 struct componentname
*cnp
= ap
->a_cnp
;
603 vfs_context_t ctx
= cnp
->cn_context
;
607 int doingdirectory
= 0;
609 uid_t ouruid
= kauth_cred_getuid(vfs_context_ucred(ctx
));
612 * assume that the name is null terminated as they
613 * are the end of the path. Get pointers to all our
621 tnp
= dev_findname(tdp
, cnp
->cn_nameptr
);
629 * Make sure that we don't try do something stupid
631 if ((tp
->dn_type
) == DEV_DIR
) {
633 * Avoid ".", "..", and aliases of "." for obvious reasons.
635 if ( (cnp
->cn_namelen
== 1 && cnp
->cn_nameptr
[0] == '.')
636 || (cnp
->cn_flags
&ISDOTDOT
) ) {
643 /***********************************
644 * Start actually doing things.... *
645 ***********************************/
650 * Target must be empty if a directory and have no links
651 * to it. Also, ensure source and target are compatible
652 * (both directories, or both not directories).
654 if (( doingdirectory
) && (tp
->dn_links
> 2)) {
668 devfs_link(struct vnop_link_args
*ap
)
669 /*struct vnop_link_args {
670 struct vnode *a_tdvp;
672 struct componentname *a_cnp;
673 vfs_context_t a_context;
676 struct vnode
*vp
= ap
->a_vp
;
677 struct vnode
*tdvp
= ap
->a_tdvp
;
678 struct componentname
*cnp
= ap
->a_cnp
;
686 * First catch an arbitrary restriction for this FS
688 if (cnp
->cn_namelen
> DEVMAXNAMESIZE
) {
689 error
= ENAMETOOLONG
;
694 * Lock our directories and get our name pointers
695 * assume that the names are null terminated as they
696 * are the end of the path. Get pointers to all our
702 if (tdvp
->v_mount
!= vp
->v_mount
) {
708 /***********************************
709 * Start actually doing things.... *
710 ***********************************/
714 error
= devfs_update(vp
, &now
, &now
);
717 error
= dev_add_name(cnp
->cn_nameptr
, tdp
, NULL
, fp
, &tnp
);
726 * Rename system call. Seems overly complicated to me...
727 * rename("foo", "bar");
730 * link("foo", "bar");
732 * but ``atomically''.
734 * When the target exists, both the directory
735 * and target vnodes are locked.
736 * the source and source-parent vnodes are referenced
739 * Basic algorithm is:
741 * 1) Bump link count on source while we're linking it to the
742 * target. This also ensure the inode won't be deleted out
743 * from underneath us while we work (it may be truncated by
744 * a concurrent `trunc' or `open' for creation).
745 * 2) Link source to destination. If destination already exists,
747 * 3) Unlink source reference to node if still around. If a
748 * directory was moved and the parent of the destination
749 * is different from the source, patch the ".." entry in the
753 devfs_rename(struct vnop_rename_args
*ap
)
754 /*struct vnop_rename_args {
755 struct vnode *a_fdvp;
757 struct componentname *a_fcnp;
758 struct vnode *a_tdvp;
760 struct componentname *a_tcnp;
761 vfs_context_t a_context;
764 struct vnode
*tvp
= ap
->a_tvp
;
765 struct vnode
*tdvp
= ap
->a_tdvp
;
766 struct vnode
*fvp
= ap
->a_fvp
;
767 struct vnode
*fdvp
= ap
->a_fdvp
;
768 struct componentname
*tcnp
= ap
->a_tcnp
;
769 struct componentname
*fcnp
= ap
->a_fcnp
;
770 devnode_t
*fp
, *fdp
, *tp
, *tdp
;
771 devdirent_t
*fnp
,*tnp
;
772 int doingdirectory
= 0;
778 * First catch an arbitrary restriction for this FS
780 if (tcnp
->cn_namelen
> DEVMAXNAMESIZE
) {
781 error
= ENAMETOOLONG
;
786 * assume that the names are null terminated as they
787 * are the end of the path. Get pointers to all our
794 fnp
= dev_findname(fdp
, fcnp
->cn_nameptr
);
804 tnp
= dev_findname(tdp
, tcnp
->cn_nameptr
);
814 * Make sure that we don't try do something stupid
816 if ((fp
->dn_type
) == DEV_DIR
) {
818 * Avoid ".", "..", and aliases of "." for obvious reasons.
820 if ((fcnp
->cn_namelen
== 1 && fcnp
->cn_nameptr
[0] == '.')
821 || (fcnp
->cn_flags
&ISDOTDOT
)
822 || (tcnp
->cn_namelen
== 1 && tcnp
->cn_nameptr
[0] == '.')
823 || (tcnp
->cn_flags
&ISDOTDOT
)
832 * If ".." must be changed (ie the directory gets a new
833 * parent) then the source directory must not be in the
834 * directory hierarchy above the target, as this would
835 * orphan everything below the source directory. Also
836 * the user must have write permission in the source so
837 * as to be able to change "..".
839 if (doingdirectory
&& (tdp
!= fdp
)) {
840 devnode_t
* tmp
, *ntmp
;
844 /* XXX unlock stuff here probably */
849 } while ((tmp
= tmp
->dn_typeinfo
.Dir
.parent
) != ntmp
);
852 /***********************************
853 * Start actually doing things.... *
854 ***********************************/
858 if ( (error
= devfs_update(fvp
, &now
, &now
)) ) {
862 * Check if just deleting a link name.
865 if (fvp
->v_type
== VDIR
) {
869 /* Release destination completely. */
876 * 1) Bump link count while we're moving stuff
877 * around. If we crash somewhere before
878 * completing our work, too bad :)
882 * If the target exists zap it (unless it's a non-empty directory)
883 * We could do that as well but won't
886 int ouruid
= kauth_cred_getuid(vfs_context_ucred(tcnp
->cn_context
));
888 * Target must be empty if a directory and have no links
889 * to it. Also, ensure source and target are compatible
890 * (both directories, or both not directories).
892 if (( doingdirectory
) && (tp
->dn_links
> 2)) {
899 dev_add_name(tcnp
->cn_nameptr
,tdp
,NULL
,fp
,&tnp
);
901 fp
->dn_links
--; /* one less link to it.. */
905 fp
->dn_links
--; /* we added one earlier*/
912 devfs_symlink(struct vnop_symlink_args
*ap
)
913 /*struct vnop_symlink_args {
915 struct vnode **a_vpp;
916 struct componentname *a_cnp;
917 struct vnode_attr *a_vap;
919 vfs_context_t a_context;
922 struct componentname
* cnp
= ap
->a_cnp
;
923 vfs_context_t ctx
= cnp
->cn_context
;
924 struct proc
*p
= vfs_context_proc(ctx
);
927 devnode_type_t typeinfo
;
930 struct vnode_attr
* vap
= ap
->a_vap
;
931 struct vnode
* * vpp
= ap
->a_vpp
;
933 dir_p
= VTODN(ap
->a_dvp
);
934 typeinfo
.Slnk
.name
= ap
->a_target
;
935 typeinfo
.Slnk
.namelen
= strlen(ap
->a_target
);
938 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
, DEV_SLNK
,
939 &typeinfo
, NULL
, NULL
, &nm_p
);
943 dev_p
= nm_p
->de_dnp
;
944 dev_p
->dn_uid
= dir_p
->dn_uid
;
945 dev_p
->dn_gid
= dir_p
->dn_gid
;
946 dev_p
->dn_mode
= vap
->va_mode
;
947 dn_copy_times(dev_p
, dir_p
);
949 error
= devfs_dntovn(dev_p
, vpp
, p
);
960 devfs_mknod(struct vnop_mknod_args
*ap
)
961 /* struct vnop_mknod_args {
963 struct vnode **a_vpp;
964 struct componentname *a_cnp;
965 struct vnode_attr *a_vap;
966 vfs_context_t a_context;
969 struct componentname
* cnp
= ap
->a_cnp
;
970 vfs_context_t ctx
= cnp
->cn_context
;
971 struct proc
*p
= vfs_context_proc(ctx
);
973 devdirent_t
* devent
;
974 devnode_t
* dir_p
; /* devnode for parent directory */
975 struct vnode
* dvp
= ap
->a_dvp
;
977 devnode_type_t typeinfo
;
978 struct vnode_attr
* vap
= ap
->a_vap
;
979 struct vnode
** vpp
= ap
->a_vpp
;
982 if (!(vap
->va_type
== VBLK
) && !(vap
->va_type
== VCHR
)) {
983 return (EINVAL
); /* only support mknod of special files */
986 typeinfo
.dev
= vap
->va_rdev
;
989 error
= dev_add_entry(cnp
->cn_nameptr
, dir_p
,
990 (vap
->va_type
== VBLK
) ? DEV_BDEV
: DEV_CDEV
,
991 &typeinfo
, NULL
, NULL
, &devent
);
995 dev_p
= devent
->de_dnp
;
996 error
= devfs_dntovn(dev_p
, vpp
, p
);
999 dev_p
->dn_uid
= vap
->va_uid
;
1000 dev_p
->dn_gid
= vap
->va_gid
;
1001 dev_p
->dn_mode
= vap
->va_mode
;
1002 VATTR_SET_SUPPORTED(vap
, va_uid
);
1003 VATTR_SET_SUPPORTED(vap
, va_gid
);
1004 VATTR_SET_SUPPORTED(vap
, va_mode
);
1012 * Vnode op for readdir
1015 devfs_readdir(struct vnop_readdir_args
*ap
)
1016 /*struct vnop_readdir_args {
1022 vfs_context_t a_context;
1025 struct vnode
*vp
= ap
->a_vp
;
1026 struct uio
*uio
= ap
->a_uio
;
1027 struct dirent dirent
;
1028 devnode_t
* dir_node
;
1029 devdirent_t
* name_node
;
1036 if (ap
->a_flags
& (VNODE_READDIR_EXTENDED
| VNODE_READDIR_REQSEEKOFF
))
1039 /* set up refs to dir */
1040 dir_node
= VTODN(vp
);
1041 if (dir_node
->dn_type
!= DEV_DIR
)
1044 startpos
= uio
->uio_offset
;
1048 name_node
= dir_node
->dn_typeinfo
.Dir
.dirlist
;
1051 dir_node
->dn_access
= 1;
1053 while ((name_node
|| (nodenumber
< 2)) && (uio_resid(uio
) > 0))
1058 dirent
.d_fileno
= (int32_t)(void *)dir_node
;
1060 dirent
.d_namlen
= 1;
1061 dirent
.d_type
= DT_DIR
;
1064 if(dir_node
->dn_typeinfo
.Dir
.parent
)
1066 = (int32_t)dir_node
->dn_typeinfo
.Dir
.parent
;
1068 dirent
.d_fileno
= (u_int32_t
)dir_node
;
1070 dirent
.d_namlen
= 2;
1071 dirent
.d_type
= DT_DIR
;
1074 dirent
.d_fileno
= (int32_t)(void *)name_node
->de_dnp
;
1075 dirent
.d_namlen
= strlen(name_node
->de_name
);
1076 name
= name_node
->de_name
;
1077 switch(name_node
->de_dnp
->dn_type
) {
1079 dirent
.d_type
= DT_BLK
;
1082 dirent
.d_type
= DT_CHR
;
1085 dirent
.d_type
= DT_DIR
;
1088 dirent
.d_type
= DT_LNK
;
1091 dirent
.d_type
= DT_UNKNOWN
;
1094 #define GENERIC_DIRSIZ(dp) \
1095 ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
1097 reclen
= dirent
.d_reclen
= GENERIC_DIRSIZ(&dirent
);
1099 if(pos
>= startpos
) /* made it to the offset yet? */
1101 if (uio_resid(uio
) < reclen
) /* will it fit? */
1103 strcpy( dirent
.d_name
,name
);
1104 if ((error
= uiomove ((caddr_t
)&dirent
,
1105 dirent
.d_reclen
, uio
)) != 0)
1109 if((nodenumber
>1) && name_node
)
1110 name_node
= name_node
->de_next
;
1114 uio
->uio_offset
= pos
;
1123 devfs_readlink(struct vnop_readlink_args
*ap
)
1124 /*struct vnop_readlink_args {
1127 vfs_context_t a_context;
1130 struct vnode
*vp
= ap
->a_vp
;
1131 struct uio
*uio
= ap
->a_uio
;
1132 devnode_t
* lnk_node
;
1135 /* set up refs to dir */
1136 lnk_node
= VTODN(vp
);
1138 if (lnk_node
->dn_type
!= DEV_SLNK
) {
1142 error
= uiomove(lnk_node
->dn_typeinfo
.Slnk
.name
,
1143 lnk_node
->dn_typeinfo
.Slnk
.namelen
, uio
);
1149 devfs_reclaim(struct vnop_reclaim_args
*ap
)
1150 /*struct vnop_reclaim_args {
1154 struct vnode
* vp
= ap
->a_vp
;
1155 devnode_t
* dnp
= VTODN(vp
);
1161 * do the same as devfs_inactive in case it is not called
1162 * before us (can that ever happen?)
1167 if (dnp
->dn_delete
) {
1178 * Get configurable pathname variables.
1182 struct vnop_pathconf_args
/* {
1186 vfs_context_t a_context;
1189 switch (ap
->a_name
) {
1191 /* arbitrary limit matching HFS; devfs has no hard limit */
1192 *ap
->a_retval
= 32767;
1195 *ap
->a_retval
= DEVMAXNAMESIZE
- 1; /* includes NUL */
1198 *ap
->a_retval
= DEVMAXPATHSIZE
- 1; /* XXX nonconformant */
1200 case _PC_CHOWN_RESTRICTED
:
1206 case _PC_CASE_SENSITIVE
:
1209 case _PC_CASE_PRESERVING
:
1221 /**************************************************************************\
1223 \**************************************************************************/
1227 * struct vnop_inactive_args {
1228 * struct vnode *a_vp;
1229 * vfs_context_t a_context;
1234 devfs_inactive(__unused
struct vnop_inactive_args
*ap
)
1240 * called with DEVFS_LOCK held
1243 devfs_update(struct vnode
*vp
, struct timeval
*access
, struct timeval
*modify
)
1249 if (vp
->v_mount
->mnt_flag
& MNT_RDONLY
) {
1257 dn_times(ip
, access
, modify
, &now
);
1262 #define VOPFUNC int (*)(void *)
1264 /* The following ops are used by directories and symlinks */
1265 int (**devfs_vnodeop_p
)(void *);
1266 static struct vnodeopv_entry_desc devfs_vnodeop_entries
[] = {
1267 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1268 { &vnop_lookup_desc
, (VOPFUNC
)devfs_lookup
}, /* lookup */
1269 { &vnop_create_desc
, (VOPFUNC
)err_create
}, /* create */
1270 { &vnop_whiteout_desc
, (VOPFUNC
)err_whiteout
}, /* whiteout */
1271 { &vnop_mknod_desc
, (VOPFUNC
)devfs_mknod
}, /* mknod */
1272 { &vnop_open_desc
, (VOPFUNC
)nop_open
}, /* open */
1273 { &vnop_close_desc
, (VOPFUNC
)devfs_close
}, /* close */
1274 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1275 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1276 { &vnop_read_desc
, (VOPFUNC
)devfs_read
}, /* read */
1277 { &vnop_write_desc
, (VOPFUNC
)devfs_write
}, /* write */
1278 { &vnop_ioctl_desc
, (VOPFUNC
)err_ioctl
}, /* ioctl */
1279 { &vnop_select_desc
, (VOPFUNC
)err_select
}, /* select */
1280 { &vnop_revoke_desc
, (VOPFUNC
)err_revoke
}, /* revoke */
1281 { &vnop_mmap_desc
, (VOPFUNC
)err_mmap
}, /* mmap */
1282 { &vnop_fsync_desc
, (VOPFUNC
)nop_fsync
}, /* fsync */
1283 { &vnop_remove_desc
, (VOPFUNC
)devfs_remove
}, /* remove */
1284 { &vnop_link_desc
, (VOPFUNC
)devfs_link
}, /* link */
1285 { &vnop_rename_desc
, (VOPFUNC
)devfs_rename
}, /* rename */
1286 { &vnop_mkdir_desc
, (VOPFUNC
)err_mkdir
}, /* mkdir */
1287 { &vnop_rmdir_desc
, (VOPFUNC
)err_rmdir
}, /* rmdir */
1288 { &vnop_symlink_desc
, (VOPFUNC
)devfs_symlink
}, /* symlink */
1289 { &vnop_readdir_desc
, (VOPFUNC
)devfs_readdir
}, /* readdir */
1290 { &vnop_readlink_desc
, (VOPFUNC
)devfs_readlink
}, /* readlink */
1291 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1292 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1293 { &vnop_strategy_desc
, (VOPFUNC
)err_strategy
}, /* strategy */
1294 { &vnop_pathconf_desc
, (VOPFUNC
)devs_vnop_pathconf
}, /* pathconf */
1295 { &vnop_advlock_desc
, (VOPFUNC
)err_advlock
}, /* advlock */
1296 { &vnop_bwrite_desc
, (VOPFUNC
)err_bwrite
},
1297 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
1298 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
1299 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1300 { &vnop_blktooff_desc
, (VOPFUNC
)err_blktooff
}, /* blktooff */
1301 { &vnop_offtoblk_desc
, (VOPFUNC
)err_offtoblk
}, /* offtoblk */
1302 { &vnop_blockmap_desc
, (VOPFUNC
)err_blockmap
}, /* blockmap */
1303 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1305 struct vnodeopv_desc devfs_vnodeop_opv_desc
=
1306 { &devfs_vnodeop_p
, devfs_vnodeop_entries
};
1308 /* The following ops are used by the device nodes */
1309 int (**devfs_spec_vnodeop_p
)(void *);
1310 static struct vnodeopv_entry_desc devfs_spec_vnodeop_entries
[] = {
1311 { &vnop_default_desc
, (VOPFUNC
)vn_default_error
},
1312 { &vnop_lookup_desc
, (VOPFUNC
)spec_lookup
}, /* lookup */
1313 { &vnop_create_desc
, (VOPFUNC
)spec_create
}, /* create */
1314 { &vnop_mknod_desc
, (VOPFUNC
)spec_mknod
}, /* mknod */
1315 { &vnop_open_desc
, (VOPFUNC
)spec_open
}, /* open */
1316 { &vnop_close_desc
, (VOPFUNC
)devfsspec_close
}, /* close */
1317 { &vnop_getattr_desc
, (VOPFUNC
)devfs_getattr
}, /* getattr */
1318 { &vnop_setattr_desc
, (VOPFUNC
)devfs_setattr
}, /* setattr */
1319 { &vnop_read_desc
, (VOPFUNC
)devfsspec_read
}, /* read */
1320 { &vnop_write_desc
, (VOPFUNC
)devfsspec_write
}, /* write */
1321 { &vnop_ioctl_desc
, (VOPFUNC
)spec_ioctl
}, /* ioctl */
1322 { &vnop_select_desc
, (VOPFUNC
)spec_select
}, /* select */
1323 { &vnop_revoke_desc
, (VOPFUNC
)spec_revoke
}, /* revoke */
1324 { &vnop_mmap_desc
, (VOPFUNC
)spec_mmap
}, /* mmap */
1325 { &vnop_fsync_desc
, (VOPFUNC
)spec_fsync
}, /* fsync */
1326 { &vnop_remove_desc
, (VOPFUNC
)devfs_remove
}, /* remove */
1327 { &vnop_link_desc
, (VOPFUNC
)devfs_link
}, /* link */
1328 { &vnop_rename_desc
, (VOPFUNC
)spec_rename
}, /* rename */
1329 { &vnop_mkdir_desc
, (VOPFUNC
)spec_mkdir
}, /* mkdir */
1330 { &vnop_rmdir_desc
, (VOPFUNC
)spec_rmdir
}, /* rmdir */
1331 { &vnop_symlink_desc
, (VOPFUNC
)spec_symlink
}, /* symlink */
1332 { &vnop_readdir_desc
, (VOPFUNC
)spec_readdir
}, /* readdir */
1333 { &vnop_readlink_desc
, (VOPFUNC
)spec_readlink
}, /* readlink */
1334 { &vnop_inactive_desc
, (VOPFUNC
)devfs_inactive
}, /* inactive */
1335 { &vnop_reclaim_desc
, (VOPFUNC
)devfs_reclaim
}, /* reclaim */
1336 { &vnop_strategy_desc
, (VOPFUNC
)spec_strategy
}, /* strategy */
1337 { &vnop_pathconf_desc
, (VOPFUNC
)spec_pathconf
}, /* pathconf */
1338 { &vnop_advlock_desc
, (VOPFUNC
)spec_advlock
}, /* advlock */
1339 { &vnop_bwrite_desc
, (VOPFUNC
)vn_bwrite
},
1340 { &vnop_devblocksize_desc
, (VOPFUNC
)spec_devblocksize
}, /* devblocksize */
1341 { &vnop_pagein_desc
, (VOPFUNC
)err_pagein
}, /* Pagein */
1342 { &vnop_pageout_desc
, (VOPFUNC
)err_pageout
}, /* Pageout */
1343 { &vnop_copyfile_desc
, (VOPFUNC
)err_copyfile
}, /* Copyfile */
1344 { &vnop_blktooff_desc
, (VOPFUNC
)spec_blktooff
}, /* blktooff */
1345 { &vnop_blktooff_desc
, (VOPFUNC
)spec_offtoblk
}, /* blkofftoblk */
1346 { &vnop_blockmap_desc
, (VOPFUNC
)spec_blockmap
}, /* blockmap */
1347 { (struct vnodeop_desc
*)NULL
, (int(*)())NULL
}
1349 struct vnodeopv_desc devfs_spec_vnodeop_opv_desc
=
1350 { &devfs_spec_vnodeop_p
, devfs_spec_vnodeop_entries
};