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