]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
e5568f75 | 2 | * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved. |
1c79356b A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
6 | * The contents of this file constitute Original Code as defined in and |
7 | * are subject to the Apple Public Source License Version 1.1 (the | |
8 | * "License"). You may not use this file except in compliance with the | |
9 | * License. Please obtain a copy of the License at | |
10 | * http://www.apple.com/publicsource and read it before using this file. | |
1c79356b | 11 | * |
e5568f75 A |
12 | * This Original Code and all software distributed under the License are |
13 | * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
16 | * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the |
17 | * License for the specific language governing rights and limitations | |
18 | * under the License. | |
1c79356b A |
19 | * |
20 | * @APPLE_LICENSE_HEADER_END@ | |
21 | */ | |
22 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
23 | /* | |
24 | * Copyright (c) 1982, 1986, 1989, 1993 | |
25 | * The Regents of the University of California. All rights reserved. | |
26 | * (c) UNIX System Laboratories, Inc. | |
27 | * All or some portions of this file are derived from material licensed | |
28 | * to the University of California by American Telephone and Telegraph | |
29 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
30 | * the permission of UNIX System Laboratories, Inc. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are 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 | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95 | |
61 | */ | |
62 | ||
63 | #include <sys/param.h> | |
55e303ae | 64 | #include <sys/systm.h> |
1c79356b A |
65 | #include <sys/syslimits.h> |
66 | #include <sys/time.h> | |
67 | #include <sys/namei.h> | |
68 | #include <sys/vm.h> | |
91447636 A |
69 | #include <sys/vnode_internal.h> |
70 | #include <sys/mount_internal.h> | |
1c79356b A |
71 | #include <sys/errno.h> |
72 | #include <sys/malloc.h> | |
73 | #include <sys/filedesc.h> | |
91447636 | 74 | #include <sys/proc_internal.h> |
1c79356b A |
75 | #include <sys/kdebug.h> |
76 | #include <sys/unistd.h> /* For _PC_NAME_MAX */ | |
91447636 A |
77 | #include <sys/uio_internal.h> |
78 | #include <sys/kauth.h> | |
e5568f75 A |
79 | |
80 | #include <bsm/audit_kernel.h> | |
1c79356b A |
81 | |
82 | #if KTRACE | |
83 | #include <sys/ktrace.h> | |
84 | #endif | |
85 | ||
91447636 A |
86 | |
87 | static void kdebug_lookup(struct vnode *dp, struct componentname *cnp); | |
55e303ae | 88 | |
1c79356b A |
89 | /* |
90 | * Convert a pathname into a pointer to a locked inode. | |
91 | * | |
92 | * The FOLLOW flag is set when symbolic links are to be followed | |
93 | * when they occur at the end of the name translation process. | |
94 | * Symbolic links are always followed for all other pathname | |
95 | * components other than the last. | |
96 | * | |
97 | * The segflg defines whether the name is to be copied from user | |
98 | * space or kernel space. | |
99 | * | |
100 | * Overall outline of namei: | |
101 | * | |
102 | * copy in name | |
103 | * get starting directory | |
104 | * while (!done && !error) { | |
105 | * call lookup to search path. | |
106 | * if symbolic link, massage name in buffer and continue | |
107 | * } | |
108 | */ | |
91447636 | 109 | |
1c79356b A |
110 | int |
111 | namei(ndp) | |
112 | register struct nameidata *ndp; | |
113 | { | |
114 | register struct filedesc *fdp; /* pointer to file descriptor state */ | |
115 | register char *cp; /* pointer into pathname argument */ | |
116 | register struct vnode *dp; /* the directory we are searching */ | |
91447636 A |
117 | uio_t auio; |
118 | int error; | |
1c79356b | 119 | struct componentname *cnp = &ndp->ni_cnd; |
91447636 A |
120 | vfs_context_t ctx = cnp->cn_context; |
121 | struct proc *p = vfs_context_proc(ctx); | |
55e303ae | 122 | char *tmppn; |
91447636 | 123 | char uio_buf[ UIO_SIZEOF(1) ]; |
1c79356b | 124 | |
1c79356b | 125 | #if DIAGNOSTIC |
91447636 | 126 | if (!vfs_context_ucred(ctx) || !p) |
1c79356b A |
127 | panic ("namei: bad cred/proc"); |
128 | if (cnp->cn_nameiop & (~OPMASK)) | |
129 | panic ("namei: nameiop contaminated with flags"); | |
130 | if (cnp->cn_flags & OPMASK) | |
131 | panic ("namei: flags contaminated with nameiops"); | |
132 | #endif | |
55e303ae | 133 | fdp = p->p_fd; |
1c79356b A |
134 | |
135 | /* | |
136 | * Get a buffer for the name to be translated, and copy the | |
137 | * name into the buffer. | |
138 | */ | |
139 | if ((cnp->cn_flags & HASBUF) == 0) { | |
91447636 A |
140 | cnp->cn_pnbuf = &ndp->ni_pathbuf; |
141 | cnp->cn_pnlen = PATHBUFLEN; | |
1c79356b | 142 | } |
91447636 A |
143 | #if LP64_DEBUG |
144 | if (IS_VALID_UIO_SEGFLG(ndp->ni_segflg) == 0) { | |
145 | panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__); | |
146 | } | |
147 | #endif /* LP64_DEBUG */ | |
148 | ||
149 | retry_copy: | |
150 | if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) | |
1c79356b | 151 | error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, |
91447636 A |
152 | cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen); |
153 | else | |
154 | error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf, | |
155 | cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen); | |
156 | ||
157 | if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) { | |
158 | MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, | |
159 | MAXPATHLEN, M_NAMEI, M_WAITOK); | |
160 | ||
161 | cnp->cn_flags |= HASBUF; | |
162 | cnp->cn_pnlen = MAXPATHLEN; | |
163 | ||
164 | goto retry_copy; | |
165 | } | |
166 | if (error) | |
167 | goto error_out; | |
55e303ae A |
168 | |
169 | /* If we are auditing the kernel pathname, save the user pathname */ | |
170 | if (cnp->cn_flags & AUDITVNPATH1) | |
171 | AUDIT_ARG(upath, p, cnp->cn_pnbuf, ARG_UPATH1); | |
172 | if (cnp->cn_flags & AUDITVNPATH2) | |
173 | AUDIT_ARG(upath, p, cnp->cn_pnbuf, ARG_UPATH2); | |
174 | ||
1c79356b A |
175 | /* |
176 | * Do not allow empty pathnames | |
177 | */ | |
91447636 | 178 | if (*cnp->cn_pnbuf == '\0') { |
1c79356b | 179 | error = ENOENT; |
91447636 | 180 | goto error_out; |
1c79356b A |
181 | } |
182 | ndp->ni_loopcnt = 0; | |
183 | #if KTRACE | |
55e303ae A |
184 | if (KTRPOINT(p, KTR_NAMEI)) |
185 | ktrnamei(p->p_tracep, cnp->cn_pnbuf); | |
1c79356b A |
186 | #endif |
187 | ||
188 | /* | |
91447636 | 189 | * determine the starting point for the translation. |
1c79356b | 190 | */ |
91447636 A |
191 | if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) { |
192 | if ( !(fdp->fd_flags & FD_CHROOT)) | |
193 | ndp->ni_rootdir = rootvnode; | |
55e303ae | 194 | } |
91447636 | 195 | cnp->cn_nameptr = cnp->cn_pnbuf; |
55e303ae | 196 | |
91447636 A |
197 | ndp->ni_usedvp = NULLVP; |
198 | ||
199 | if (*(cnp->cn_nameptr) == '/') { | |
200 | while (*(cnp->cn_nameptr) == '/') { | |
201 | cnp->cn_nameptr++; | |
202 | ndp->ni_pathlen--; | |
1c79356b | 203 | } |
91447636 A |
204 | dp = ndp->ni_rootdir; |
205 | } else if (cnp->cn_flags & USEDVP) { | |
206 | dp = ndp->ni_dvp; | |
207 | ndp->ni_usedvp = dp; | |
208 | } else | |
209 | dp = fdp->fd_cdir; | |
210 | ||
211 | if (dp == NULLVP) { | |
212 | error = ENOENT; | |
213 | goto error_out; | |
214 | } | |
215 | ndp->ni_dvp = NULLVP; | |
216 | ndp->ni_vp = NULLVP; | |
217 | ||
218 | for (;;) { | |
219 | int need_newpathbuf; | |
220 | int linklen; | |
221 | ||
1c79356b | 222 | ndp->ni_startdir = dp; |
91447636 A |
223 | |
224 | if ( (error = lookup(ndp)) ) { | |
225 | goto error_out; | |
1c79356b A |
226 | } |
227 | /* | |
228 | * Check for symbolic link | |
229 | */ | |
230 | if ((cnp->cn_flags & ISSYMLINK) == 0) { | |
1c79356b A |
231 | return (0); |
232 | } | |
91447636 A |
233 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
234 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
235 | unlock_fsnode(ndp->ni_dvp, NULL); | |
236 | } | |
1c79356b A |
237 | if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { |
238 | error = ELOOP; | |
239 | break; | |
240 | } | |
91447636 A |
241 | if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF)) |
242 | need_newpathbuf = 1; | |
243 | else | |
244 | need_newpathbuf = 0; | |
245 | ||
246 | if (need_newpathbuf) { | |
1c79356b A |
247 | MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); |
248 | } else { | |
249 | cp = cnp->cn_pnbuf; | |
250 | } | |
91447636 A |
251 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf)); |
252 | ||
253 | uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN); | |
254 | ||
255 | error = VNOP_READLINK(ndp->ni_vp, auio, ctx); | |
256 | if (error) { | |
257 | if (need_newpathbuf) | |
55e303ae | 258 | FREE_ZONE(cp, MAXPATHLEN, M_NAMEI); |
1c79356b A |
259 | break; |
260 | } | |
91447636 A |
261 | // LP64todo - fix this |
262 | linklen = MAXPATHLEN - uio_resid(auio); | |
263 | if (linklen + ndp->ni_pathlen > MAXPATHLEN) { | |
264 | if (need_newpathbuf) | |
55e303ae | 265 | FREE_ZONE(cp, MAXPATHLEN, M_NAMEI); |
91447636 | 266 | |
1c79356b A |
267 | error = ENAMETOOLONG; |
268 | break; | |
269 | } | |
91447636 | 270 | if (need_newpathbuf) { |
55e303ae | 271 | long len = cnp->cn_pnlen; |
91447636 | 272 | |
55e303ae | 273 | tmppn = cnp->cn_pnbuf; |
1c79356b | 274 | bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); |
1c79356b A |
275 | cnp->cn_pnbuf = cp; |
276 | cnp->cn_pnlen = MAXPATHLEN; | |
91447636 A |
277 | |
278 | if ( (cnp->cn_flags & HASBUF) ) | |
279 | FREE_ZONE(tmppn, len, M_NAMEI); | |
280 | else | |
281 | cnp->cn_flags |= HASBUF; | |
1c79356b A |
282 | } else |
283 | cnp->cn_pnbuf[linklen] = '\0'; | |
91447636 | 284 | |
1c79356b | 285 | ndp->ni_pathlen += linklen; |
91447636 A |
286 | cnp->cn_nameptr = cnp->cn_pnbuf; |
287 | ||
288 | /* | |
289 | * starting point for 'relative' | |
290 | * symbolic link path | |
291 | */ | |
1c79356b | 292 | dp = ndp->ni_dvp; |
91447636 A |
293 | /* |
294 | * get rid of references returned via 'lookup' | |
295 | */ | |
296 | vnode_put(ndp->ni_vp); | |
297 | vnode_put(ndp->ni_dvp); | |
298 | ||
299 | ndp->ni_vp = NULLVP; | |
300 | ndp->ni_dvp = NULLVP; | |
55e303ae | 301 | |
91447636 A |
302 | /* |
303 | * Check if symbolic link restarts us at the root | |
304 | */ | |
305 | if (*(cnp->cn_nameptr) == '/') { | |
306 | while (*(cnp->cn_nameptr) == '/') { | |
307 | cnp->cn_nameptr++; | |
308 | ndp->ni_pathlen--; | |
309 | } | |
310 | if ((dp = ndp->ni_rootdir) == NULLVP) { | |
311 | error = ENOENT; | |
312 | goto error_out; | |
313 | } | |
314 | } | |
315 | } | |
316 | /* | |
317 | * only come here if we fail to handle a SYMLINK... | |
318 | * if either ni_dvp or ni_vp is non-NULL, then | |
319 | * we need to drop the iocount that was picked | |
320 | * up in the lookup routine | |
321 | */ | |
322 | if (ndp->ni_dvp) | |
323 | vnode_put(ndp->ni_dvp); | |
324 | if (ndp->ni_vp) | |
325 | vnode_put(ndp->ni_vp); | |
326 | error_out: | |
327 | if ( (cnp->cn_flags & HASBUF) ) { | |
328 | cnp->cn_flags &= ~HASBUF; | |
329 | FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI); | |
330 | } | |
55e303ae | 331 | cnp->cn_pnbuf = NULL; |
91447636 | 332 | ndp->ni_vp = NULLVP; |
55e303ae | 333 | |
1c79356b A |
334 | return (error); |
335 | } | |
336 | ||
91447636 | 337 | |
1c79356b A |
338 | /* |
339 | * Search a pathname. | |
340 | * This is a very central and rather complicated routine. | |
341 | * | |
342 | * The pathname is pointed to by ni_ptr and is of length ni_pathlen. | |
343 | * The starting directory is taken from ni_startdir. The pathname is | |
344 | * descended until done, or a symbolic link is encountered. The variable | |
345 | * ni_more is clear if the path is completed; it is set to one if a | |
346 | * symbolic link needing interpretation is encountered. | |
347 | * | |
348 | * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on | |
349 | * whether the name is to be looked up, created, renamed, or deleted. | |
350 | * When CREATE, RENAME, or DELETE is specified, information usable in | |
351 | * creating, renaming, or deleting a directory entry may be calculated. | |
352 | * If flag has LOCKPARENT or'ed into it, the parent directory is returned | |
353 | * locked. If flag has WANTPARENT or'ed into it, the parent directory is | |
354 | * returned unlocked. Otherwise the parent directory is not returned. If | |
355 | * the target of the pathname exists and LOCKLEAF is or'ed into the flag | |
356 | * the target is returned locked, otherwise it is returned unlocked. | |
357 | * When creating or renaming and LOCKPARENT is specified, the target may not | |
358 | * be ".". When deleting and LOCKPARENT is specified, the target may be ".". | |
359 | * | |
360 | * Overall outline of lookup: | |
361 | * | |
362 | * dirloop: | |
363 | * identify next component of name at ndp->ni_ptr | |
364 | * handle degenerate case where name is null string | |
365 | * if .. and crossing mount points and on mounted filesys, find parent | |
91447636 | 366 | * call VNOP_LOOKUP routine for next component name |
1c79356b A |
367 | * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set |
368 | * component vnode returned in ni_vp (if it exists), locked. | |
369 | * if result vnode is mounted on and crossing mount points, | |
370 | * find mounted on vnode | |
371 | * if more components of name, do next level at dirloop | |
372 | * return the answer in ni_vp, locked if LOCKLEAF set | |
373 | * if LOCKPARENT set, return locked parent in ni_dvp | |
374 | * if WANTPARENT set, return unlocked parent in ni_dvp | |
375 | */ | |
376 | int | |
377 | lookup(ndp) | |
378 | register struct nameidata *ndp; | |
379 | { | |
91447636 A |
380 | register char *cp; /* pointer into pathname argument */ |
381 | vnode_t tdp; /* saved dp */ | |
382 | vnode_t dp; /* the directory we are searching */ | |
383 | mount_t mp; /* mount table entry */ | |
384 | int docache = 1; /* == 0 do not cache last component */ | |
1c79356b A |
385 | int wantparent; /* 1 => wantparent or lockparent flag */ |
386 | int rdonly; /* lookup read-only flag bit */ | |
55e303ae | 387 | int trailing_slash = 0; |
91447636 | 388 | int dp_authorized = 0; |
1c79356b A |
389 | int error = 0; |
390 | struct componentname *cnp = &ndp->ni_cnd; | |
91447636 | 391 | vfs_context_t ctx = cnp->cn_context; |
1c79356b A |
392 | |
393 | /* | |
394 | * Setup: break out flag bits into variables. | |
395 | */ | |
91447636 A |
396 | if (cnp->cn_flags & (NOCACHE | DOWHITEOUT)) { |
397 | if ((cnp->cn_flags & NOCACHE) || (cnp->cn_nameiop == DELETE)) | |
398 | docache = 0; | |
399 | } | |
1c79356b | 400 | wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); |
1c79356b | 401 | rdonly = cnp->cn_flags & RDONLY; |
1c79356b | 402 | cnp->cn_flags &= ~ISSYMLINK; |
91447636 A |
403 | cnp->cn_consume = 0; |
404 | ||
1c79356b A |
405 | dp = ndp->ni_startdir; |
406 | ndp->ni_startdir = NULLVP; | |
1c79356b | 407 | |
91447636 | 408 | cp = cnp->cn_nameptr; |
55e303ae | 409 | |
91447636 A |
410 | if (*cp == '\0') { |
411 | if ( (vnode_getwithref(dp)) ) { | |
412 | dp = NULLVP; | |
413 | error = ENOENT; | |
55e303ae A |
414 | goto bad; |
415 | } | |
91447636 | 416 | goto emptyname; |
1c79356b | 417 | } |
91447636 A |
418 | dirloop: |
419 | ndp->ni_vp = NULLVP; | |
9bccf70c | 420 | |
91447636 A |
421 | if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &trailing_slash, &dp_authorized)) ) { |
422 | dp = NULLVP; | |
423 | goto bad; | |
424 | } | |
425 | if ((cnp->cn_flags & ISLASTCN)) { | |
426 | if (docache) | |
427 | cnp->cn_flags |= MAKEENTRY; | |
428 | } else | |
429 | cnp->cn_flags |= MAKEENTRY; | |
430 | ||
431 | dp = ndp->ni_dvp; | |
432 | ||
433 | if (ndp->ni_vp != NULLVP) { | |
434 | /* | |
435 | * cache_lookup_path returned a non-NULL ni_vp then, | |
436 | * we're guaranteed that the dp is a VDIR, it's | |
437 | * been authorized, and vp is not ".." | |
438 | */ | |
439 | goto returned_from_lookup_path; | |
9bccf70c | 440 | } |
1c79356b | 441 | |
1c79356b A |
442 | /* |
443 | * Handle "..": two special cases. | |
444 | * 1. If at root directory (e.g. after chroot) | |
445 | * or at absolute root directory | |
446 | * then ignore it so can't get out. | |
447 | * 2. If this vnode is the root of a mounted | |
448 | * filesystem, then replace it with the | |
449 | * vnode which was mounted on so we take the | |
450 | * .. in the other file system. | |
451 | */ | |
91447636 | 452 | if ( (cnp->cn_flags & ISDOTDOT) ) { |
1c79356b | 453 | for (;;) { |
91447636 A |
454 | if (dp == ndp->ni_rootdir || dp == rootvnode) { |
455 | ndp->ni_dvp = dp; | |
1c79356b | 456 | ndp->ni_vp = dp; |
91447636 A |
457 | /* |
458 | * we're pinned at the root | |
459 | * we've already got one reference on 'dp' | |
460 | * courtesy of cache_lookup_path... take | |
461 | * another one for the ".." | |
462 | * if we fail to get the new reference, we'll | |
463 | * drop our original down in 'bad' | |
464 | */ | |
465 | if ( (vnode_get(dp)) ) { | |
466 | error = ENOENT; | |
467 | goto bad; | |
468 | } | |
1c79356b A |
469 | goto nextname; |
470 | } | |
471 | if ((dp->v_flag & VROOT) == 0 || | |
472 | (cnp->cn_flags & NOCROSSMOUNT)) | |
91447636 | 473 | break; |
0b4e3aa0 | 474 | if (dp->v_mount == NULL) { /* forced umount */ |
91447636 | 475 | error = EBADF; |
0b4e3aa0 A |
476 | goto bad; |
477 | } | |
1c79356b | 478 | tdp = dp; |
91447636 A |
479 | dp = tdp->v_mount->mnt_vnodecovered; |
480 | ||
481 | vnode_put(tdp); | |
482 | ||
483 | if ( (vnode_getwithref(dp)) ) { | |
484 | dp = NULLVP; | |
485 | error = ENOENT; | |
486 | goto bad; | |
487 | } | |
488 | ndp->ni_dvp = dp; | |
489 | dp_authorized = 0; | |
1c79356b A |
490 | } |
491 | } | |
492 | ||
493 | /* | |
494 | * We now have a segment name to search for, and a directory to search. | |
495 | */ | |
496 | unionlookup: | |
91447636 A |
497 | ndp->ni_vp = NULLVP; |
498 | ||
499 | if (dp->v_type != VDIR) { | |
500 | error = ENOTDIR; | |
501 | goto lookup_error; | |
502 | } | |
503 | if ( !(dp_authorized || (cnp->cn_flags & DONOTAUTH)) ) { | |
504 | if ( (error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx)) ) | |
505 | goto lookup_error; | |
506 | } | |
507 | if ( (error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx)) ) { | |
508 | lookup_error: | |
1c79356b | 509 | if ((error == ENOENT) && |
0b4e3aa0 | 510 | (dp->v_flag & VROOT) && (dp->v_mount != NULL) && |
1c79356b | 511 | (dp->v_mount->mnt_flag & MNT_UNION)) { |
91447636 A |
512 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
513 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
514 | unlock_fsnode(dp, NULL); | |
515 | } | |
1c79356b | 516 | tdp = dp; |
91447636 A |
517 | dp = tdp->v_mount->mnt_vnodecovered; |
518 | ||
519 | vnode_put(tdp); | |
520 | ||
521 | if ( (vnode_getwithref(dp)) ) { | |
522 | dp = NULLVP; | |
523 | error = ENOENT; | |
524 | goto bad; | |
525 | } | |
526 | ndp->ni_dvp = dp; | |
527 | dp_authorized = 0; | |
1c79356b A |
528 | goto unionlookup; |
529 | } | |
530 | ||
531 | if (error != EJUSTRETURN) | |
532 | goto bad; | |
91447636 A |
533 | |
534 | if (ndp->ni_vp != NULLVP) | |
535 | panic("leaf should be empty"); | |
536 | ||
1c79356b A |
537 | /* |
538 | * If creating and at end of pathname, then can consider | |
539 | * allowing file to be created. | |
540 | */ | |
541 | if (rdonly) { | |
542 | error = EROFS; | |
543 | goto bad; | |
544 | } | |
91447636 | 545 | if ((cnp->cn_flags & ISLASTCN) && trailing_slash && !(cnp->cn_flags & WILLBEDIR)) { |
9bccf70c A |
546 | error = ENOENT; |
547 | goto bad; | |
548 | } | |
1c79356b A |
549 | /* |
550 | * We return with ni_vp NULL to indicate that the entry | |
551 | * doesn't currently exist, leaving a pointer to the | |
91447636 | 552 | * referenced directory vnode in ndp->ni_dvp. |
1c79356b A |
553 | */ |
554 | if (cnp->cn_flags & SAVESTART) { | |
91447636 A |
555 | if ( (vnode_get(ndp->ni_dvp)) ) { |
556 | error = ENOENT; | |
557 | goto bad; | |
558 | } | |
1c79356b | 559 | ndp->ni_startdir = ndp->ni_dvp; |
1c79356b | 560 | } |
91447636 A |
561 | if (!wantparent) |
562 | vnode_put(ndp->ni_dvp); | |
563 | ||
1c79356b A |
564 | if (kdebug_enable) |
565 | kdebug_lookup(ndp->ni_dvp, cnp); | |
566 | return (0); | |
567 | } | |
91447636 A |
568 | returned_from_lookup_path: |
569 | dp = ndp->ni_vp; | |
1c79356b A |
570 | |
571 | /* | |
572 | * Take into account any additional components consumed by | |
573 | * the underlying filesystem. | |
574 | */ | |
575 | if (cnp->cn_consume > 0) { | |
576 | cnp->cn_nameptr += cnp->cn_consume; | |
577 | ndp->ni_next += cnp->cn_consume; | |
578 | ndp->ni_pathlen -= cnp->cn_consume; | |
579 | cnp->cn_consume = 0; | |
55e303ae | 580 | } else { |
91447636 A |
581 | if (dp->v_name == NULL || dp->v_parent == NULLVP) { |
582 | int isdot_or_dotdot; | |
583 | int update_flags = 0; | |
55e303ae | 584 | |
91447636 | 585 | isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT); |
55e303ae | 586 | |
91447636 A |
587 | if (isdot_or_dotdot == 0) { |
588 | if (dp->v_name == NULL) | |
589 | update_flags |= VNODE_UPDATE_NAME; | |
590 | if (ndp->ni_dvp != NULLVP && dp->v_parent == NULLVP) | |
591 | update_flags |= VNODE_UPDATE_PARENT; | |
592 | ||
593 | if (update_flags) | |
594 | vnode_update_identity(dp, ndp->ni_dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags); | |
595 | } | |
596 | } | |
597 | ||
598 | if ( (cnp->cn_flags & MAKEENTRY) && (dp->v_flag & VNCACHEABLE) && LIST_FIRST(&dp->v_nclinks) == NULL) { | |
599 | /* | |
600 | * missing from name cache, but should | |
601 | * be in it... this can happen if volfs | |
602 | * causes the vnode to be created or the | |
603 | * name cache entry got recycled but the | |
604 | * vnode didn't... | |
605 | * check to make sure that ni_dvp is valid | |
606 | * cache_lookup_path may return a NULL | |
607 | */ | |
608 | if (ndp->ni_dvp != NULL) | |
609 | cache_enter(ndp->ni_dvp, dp, cnp); | |
55e303ae | 610 | } |
1c79356b A |
611 | } |
612 | ||
1c79356b | 613 | /* |
91447636 | 614 | * Check to see if the vnode has been mounted on... |
1c79356b A |
615 | * if so find the root of the mounted file system. |
616 | */ | |
91447636 A |
617 | check_mounted_on: |
618 | if ((dp->v_type == VDIR) && dp->v_mountedhere && | |
619 | ((cnp->cn_flags & NOCROSSMOUNT) == 0)) { | |
620 | ||
621 | vnode_lock(dp); | |
622 | ||
623 | if ((dp->v_type == VDIR) && (mp = dp->v_mountedhere)) { | |
624 | ||
625 | mp->mnt_crossref++; | |
626 | vnode_unlock(dp); | |
627 | ||
628 | if (vfs_busy(mp, 0)) { | |
629 | mount_dropcrossref(mp, dp, 0); | |
630 | goto check_mounted_on; | |
631 | } | |
632 | error = VFS_ROOT(mp, &tdp, ctx); | |
633 | /* | |
634 | * mount_dropcrossref does a vnode_put | |
635 | * on dp if the 3rd arg is non-zero | |
636 | */ | |
637 | mount_dropcrossref(mp, dp, 1); | |
638 | dp = NULL; | |
639 | vfs_unbusy(mp); | |
640 | ||
641 | if (error) { | |
642 | goto bad2; | |
643 | } | |
644 | ndp->ni_vp = dp = tdp; | |
645 | ||
646 | goto check_mounted_on; | |
647 | } | |
648 | vnode_unlock(dp); | |
1c79356b A |
649 | } |
650 | ||
651 | /* | |
652 | * Check for symbolic link | |
653 | */ | |
654 | if ((dp->v_type == VLNK) && | |
91447636 | 655 | ((cnp->cn_flags & FOLLOW) || trailing_slash || *ndp->ni_next == '/')) { |
1c79356b A |
656 | cnp->cn_flags |= ISSYMLINK; |
657 | return (0); | |
658 | } | |
659 | ||
9bccf70c A |
660 | /* |
661 | * Check for bogus trailing slashes. | |
662 | */ | |
55e303ae A |
663 | if (trailing_slash) { |
664 | if (dp->v_type != VDIR) { | |
665 | error = ENOTDIR; | |
666 | goto bad2; | |
667 | } | |
668 | trailing_slash = 0; | |
91447636 | 669 | } |
9bccf70c | 670 | |
1c79356b A |
671 | nextname: |
672 | /* | |
673 | * Not a symbolic link. If more pathname, | |
674 | * continue at next component, else return. | |
675 | */ | |
676 | if (*ndp->ni_next == '/') { | |
55e303ae A |
677 | cnp->cn_nameptr = ndp->ni_next + 1; |
678 | ndp->ni_pathlen--; | |
1c79356b A |
679 | while (*cnp->cn_nameptr == '/') { |
680 | cnp->cn_nameptr++; | |
681 | ndp->ni_pathlen--; | |
682 | } | |
91447636 A |
683 | vnode_put(ndp->ni_dvp); |
684 | ||
685 | cp = cnp->cn_nameptr; | |
686 | ||
687 | if (*cp == '\0') | |
688 | goto emptyname; | |
689 | ||
690 | vnode_put(dp); | |
1c79356b A |
691 | goto dirloop; |
692 | } | |
693 | ||
694 | /* | |
695 | * Disallow directory write attempts on read-only file systems. | |
696 | */ | |
697 | if (rdonly && | |
698 | (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { | |
699 | error = EROFS; | |
700 | goto bad2; | |
701 | } | |
702 | if (cnp->cn_flags & SAVESTART) { | |
91447636 A |
703 | /* |
704 | * note that we already hold a reference | |
705 | * on both dp and ni_dvp, but for some reason | |
706 | * can't get another one... in this case we | |
707 | * need to do vnode_put on dp in 'bad2' | |
708 | */ | |
709 | if ( (vnode_get(ndp->ni_dvp)) ) { | |
710 | error = ENOENT; | |
711 | goto bad2; | |
712 | } | |
1c79356b | 713 | ndp->ni_startdir = ndp->ni_dvp; |
1c79356b | 714 | } |
91447636 A |
715 | if (!wantparent && ndp->ni_dvp) |
716 | vnode_put(ndp->ni_dvp); | |
717 | ||
55e303ae A |
718 | if (cnp->cn_flags & AUDITVNPATH1) |
719 | AUDIT_ARG(vnpath, dp, ARG_VNODE1); | |
720 | else if (cnp->cn_flags & AUDITVNPATH2) | |
721 | AUDIT_ARG(vnpath, dp, ARG_VNODE2); | |
91447636 | 722 | |
1c79356b A |
723 | if (kdebug_enable) |
724 | kdebug_lookup(dp, cnp); | |
725 | return (0); | |
726 | ||
55e303ae | 727 | emptyname: |
91447636 | 728 | cnp->cn_namelen = 0; |
55e303ae A |
729 | /* |
730 | * A degenerate name (e.g. / or "") which is a way of | |
731 | * talking about a directory, e.g. like "/." or ".". | |
732 | */ | |
733 | if (dp->v_type != VDIR) { | |
734 | error = ENOTDIR; | |
735 | goto bad; | |
736 | } | |
737 | if (cnp->cn_nameiop != LOOKUP) { | |
738 | error = EISDIR; | |
739 | goto bad; | |
740 | } | |
741 | if (wantparent) { | |
91447636 A |
742 | /* |
743 | * note that we already hold a reference | |
744 | * on dp, but for some reason can't | |
745 | * get another one... in this case we | |
746 | * need to do vnode_put on dp in 'bad' | |
747 | */ | |
748 | if ( (vnode_get(dp)) ) { | |
749 | error = ENOENT; | |
750 | goto bad; | |
751 | } | |
55e303ae | 752 | ndp->ni_dvp = dp; |
55e303ae A |
753 | } |
754 | cnp->cn_flags &= ~ISDOTDOT; | |
755 | cnp->cn_flags |= ISLASTCN; | |
756 | ndp->ni_next = cp; | |
757 | ndp->ni_vp = dp; | |
91447636 | 758 | |
55e303ae A |
759 | if (cnp->cn_flags & AUDITVNPATH1) |
760 | AUDIT_ARG(vnpath, dp, ARG_VNODE1); | |
761 | else if (cnp->cn_flags & AUDITVNPATH2) | |
762 | AUDIT_ARG(vnpath, dp, ARG_VNODE2); | |
55e303ae A |
763 | if (cnp->cn_flags & SAVESTART) |
764 | panic("lookup: SAVESTART"); | |
765 | return (0); | |
766 | ||
1c79356b | 767 | bad2: |
91447636 A |
768 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
769 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
770 | unlock_fsnode(ndp->ni_dvp, NULL); | |
771 | } | |
772 | if (ndp->ni_dvp) | |
773 | vnode_put(ndp->ni_dvp); | |
774 | if (dp) | |
775 | vnode_put(dp); | |
776 | ndp->ni_vp = NULLVP; | |
777 | ||
778 | if (kdebug_enable) | |
779 | kdebug_lookup(dp, cnp); | |
780 | return (error); | |
781 | ||
1c79356b | 782 | bad: |
91447636 A |
783 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
784 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
785 | unlock_fsnode(ndp->ni_dvp, NULL); | |
786 | } | |
787 | if (dp) | |
788 | vnode_put(dp); | |
789 | ndp->ni_vp = NULLVP; | |
790 | ||
1c79356b A |
791 | if (kdebug_enable) |
792 | kdebug_lookup(dp, cnp); | |
793 | return (error); | |
794 | } | |
795 | ||
796 | /* | |
797 | * relookup - lookup a path name component | |
798 | * Used by lookup to re-aquire things. | |
799 | */ | |
800 | int | |
801 | relookup(dvp, vpp, cnp) | |
802 | struct vnode *dvp, **vpp; | |
803 | struct componentname *cnp; | |
804 | { | |
1c79356b | 805 | struct vnode *dp = 0; /* the directory we are searching */ |
1c79356b A |
806 | int wantparent; /* 1 => wantparent or lockparent flag */ |
807 | int rdonly; /* lookup read-only flag bit */ | |
808 | int error = 0; | |
809 | #ifdef NAMEI_DIAGNOSTIC | |
55e303ae | 810 | int i, newhash; /* DEBUG: check name hash */ |
1c79356b A |
811 | char *cp; /* DEBUG: check name ptr/len */ |
812 | #endif | |
91447636 | 813 | vfs_context_t ctx = cnp->cn_context;; |
1c79356b A |
814 | |
815 | /* | |
816 | * Setup: break out flag bits into variables. | |
817 | */ | |
818 | wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); | |
1c79356b A |
819 | rdonly = cnp->cn_flags & RDONLY; |
820 | cnp->cn_flags &= ~ISSYMLINK; | |
1c79356b | 821 | |
91447636 A |
822 | if (cnp->cn_flags & NOCACHE) |
823 | cnp->cn_flags &= ~MAKEENTRY; | |
824 | else | |
825 | cnp->cn_flags |= MAKEENTRY; | |
826 | ||
827 | dp = dvp; | |
1c79356b A |
828 | |
829 | /* | |
830 | * Check for degenerate name (e.g. / or "") | |
831 | * which is a way of talking about a directory, | |
832 | * e.g. like "/." or ".". | |
833 | */ | |
834 | if (cnp->cn_nameptr[0] == '\0') { | |
835 | if (cnp->cn_nameiop != LOOKUP || wantparent) { | |
836 | error = EISDIR; | |
837 | goto bad; | |
838 | } | |
839 | if (dp->v_type != VDIR) { | |
840 | error = ENOTDIR; | |
841 | goto bad; | |
842 | } | |
91447636 A |
843 | if ( (vnode_get(dp)) ) { |
844 | error = ENOENT; | |
845 | goto bad; | |
846 | } | |
1c79356b | 847 | *vpp = dp; |
91447636 | 848 | |
1c79356b A |
849 | if (cnp->cn_flags & SAVESTART) |
850 | panic("lookup: SAVESTART"); | |
851 | return (0); | |
852 | } | |
1c79356b A |
853 | /* |
854 | * We now have a segment name to search for, and a directory to search. | |
855 | */ | |
91447636 A |
856 | if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) { |
857 | if (error != EJUSTRETURN) | |
858 | goto bad; | |
1c79356b A |
859 | #if DIAGNOSTIC |
860 | if (*vpp != NULL) | |
861 | panic("leaf should be empty"); | |
862 | #endif | |
1c79356b A |
863 | /* |
864 | * If creating and at end of pathname, then can consider | |
865 | * allowing file to be created. | |
866 | */ | |
867 | if (rdonly) { | |
868 | error = EROFS; | |
869 | goto bad; | |
870 | } | |
1c79356b A |
871 | /* |
872 | * We return with ni_vp NULL to indicate that the entry | |
873 | * doesn't currently exist, leaving a pointer to the | |
874 | * (possibly locked) directory inode in ndp->ni_dvp. | |
875 | */ | |
876 | return (0); | |
877 | } | |
878 | dp = *vpp; | |
879 | ||
880 | #if DIAGNOSTIC | |
881 | /* | |
882 | * Check for symbolic link | |
883 | */ | |
884 | if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) | |
885 | panic ("relookup: symlink found.\n"); | |
886 | #endif | |
887 | ||
888 | /* | |
889 | * Disallow directory write attempts on read-only file systems. | |
890 | */ | |
891 | if (rdonly && | |
892 | (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { | |
893 | error = EROFS; | |
894 | goto bad2; | |
895 | } | |
896 | /* ASSERT(dvp == ndp->ni_startdir) */ | |
1c79356b | 897 | |
1c79356b A |
898 | return (0); |
899 | ||
900 | bad2: | |
91447636 A |
901 | vnode_put(dp); |
902 | bad: | |
1c79356b | 903 | *vpp = NULL; |
91447636 | 904 | |
1c79356b A |
905 | return (error); |
906 | } | |
907 | ||
91447636 A |
908 | /* |
909 | * Free pathname buffer | |
910 | */ | |
911 | void | |
912 | nameidone(struct nameidata *ndp) | |
913 | { | |
914 | if ((ndp->ni_cnd.cn_flags & FSNODELOCKHELD)) { | |
915 | ndp->ni_cnd.cn_flags &= ~FSNODELOCKHELD; | |
916 | unlock_fsnode(ndp->ni_dvp, NULL); | |
917 | } | |
918 | if (ndp->ni_cnd.cn_flags & HASBUF) { | |
919 | char *tmp = ndp->ni_cnd.cn_pnbuf; | |
920 | ||
921 | ndp->ni_cnd.cn_pnbuf = NULL; | |
922 | ndp->ni_cnd.cn_flags &= ~HASBUF; | |
923 | FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI); | |
924 | } | |
925 | } | |
926 | ||
1c79356b | 927 | |
0b4e3aa0 | 928 | #define NUMPARMS 23 |
1c79356b | 929 | |
55e303ae | 930 | static void |
1c79356b | 931 | kdebug_lookup(dp, cnp) |
55e303ae | 932 | struct vnode *dp; |
1c79356b A |
933 | struct componentname *cnp; |
934 | { | |
91447636 | 935 | register unsigned int i, n; |
1c79356b | 936 | register int dbg_namelen; |
0b4e3aa0 | 937 | register int save_dbg_namelen; |
1c79356b A |
938 | register char *dbg_nameptr; |
939 | long dbg_parms[NUMPARMS]; | |
940 | char dbg_buf[4]; | |
941 | static char *dbg_filler = ">>>>"; | |
942 | ||
943 | /* Collect the pathname for tracing */ | |
944 | dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen; | |
945 | dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen; | |
946 | ||
947 | if (dbg_namelen > sizeof(dbg_parms)) | |
948 | dbg_namelen = sizeof(dbg_parms); | |
949 | dbg_nameptr -= dbg_namelen; | |
0b4e3aa0 | 950 | save_dbg_namelen = dbg_namelen; |
1c79356b A |
951 | |
952 | i = 0; | |
953 | ||
954 | while (dbg_namelen > 0) { | |
955 | if (dbg_namelen >= 4) { | |
956 | dbg_parms[i++] = *(long *)dbg_nameptr; | |
957 | dbg_nameptr += sizeof(long); | |
958 | dbg_namelen -= sizeof(long); | |
959 | } else { | |
960 | for (n = 0; n < dbg_namelen; n++) | |
961 | dbg_buf[n] = *dbg_nameptr++; | |
962 | while (n <= 3) { | |
963 | if (*dbg_nameptr) | |
964 | dbg_buf[n++] = '>'; | |
965 | else | |
966 | dbg_buf[n++] = 0; | |
967 | } | |
968 | dbg_parms[i++] = *(long *)&dbg_buf[0]; | |
969 | ||
970 | break; | |
971 | } | |
972 | } | |
973 | while (i < NUMPARMS) { | |
974 | if (*dbg_nameptr) | |
975 | dbg_parms[i++] = *(long *)dbg_filler; | |
976 | else | |
977 | dbg_parms[i++] = 0; | |
978 | } | |
0b4e3aa0 | 979 | |
9bccf70c A |
980 | /* |
981 | In the event that we collect multiple, consecutive pathname | |
982 | entries, we must mark the start of the path's string. | |
983 | */ | |
984 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW,36)) | DBG_FUNC_START, | |
55e303ae | 985 | (unsigned int)dp, dbg_parms[0], dbg_parms[1], dbg_parms[2], 0); |
0b4e3aa0 A |
986 | |
987 | for (dbg_namelen = save_dbg_namelen-12, i=3; | |
988 | dbg_namelen > 0; | |
9bccf70c | 989 | dbg_namelen -=(4 * sizeof(long)), i+= 4) |
0b4e3aa0 A |
990 | { |
991 | KERNEL_DEBUG_CONSTANT((FSDBG_CODE(DBG_FSRW,36)) | DBG_FUNC_NONE, | |
9bccf70c | 992 | dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0); |
0b4e3aa0 | 993 | } |
1c79356b | 994 | } |