]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* Copyright (c) 1995 NeXT Computer, Inc. All Rights Reserved */ | |
29 | /* | |
30 | * Copyright (c) 1982, 1986, 1989, 1993 | |
31 | * The Regents of the University of California. All rights reserved. | |
32 | * (c) UNIX System Laboratories, Inc. | |
33 | * All or some portions of this file are derived from material licensed | |
34 | * to the University of California by American Telephone and Telegraph | |
35 | * Co. or Unix System Laboratories, Inc. and are reproduced herein with | |
36 | * the permission of UNIX System Laboratories, Inc. | |
37 | * | |
38 | * Redistribution and use in source and binary forms, with or without | |
39 | * modification, are permitted provided that the following conditions | |
40 | * are met: | |
41 | * 1. Redistributions of source code must retain the above copyright | |
42 | * notice, this list of conditions and the following disclaimer. | |
43 | * 2. Redistributions in binary form must reproduce the above copyright | |
44 | * notice, this list of conditions and the following disclaimer in the | |
45 | * documentation and/or other materials provided with the distribution. | |
46 | * 3. All advertising materials mentioning features or use of this software | |
47 | * must display the following acknowledgement: | |
48 | * This product includes software developed by the University of | |
49 | * California, Berkeley and its contributors. | |
50 | * 4. Neither the name of the University nor the names of its contributors | |
51 | * may be used to endorse or promote products derived from this software | |
52 | * without specific prior written permission. | |
53 | * | |
54 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
55 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
56 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
57 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
58 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
59 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
60 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
61 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
62 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
63 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
64 | * SUCH DAMAGE. | |
65 | * | |
66 | * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95 | |
67 | */ | |
2d21ac55 A |
68 | /* |
69 | * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce | |
70 | * support for mandatory and extensible security protections. This notice | |
71 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
72 | * Version 2.0. | |
73 | */ | |
1c79356b A |
74 | |
75 | #include <sys/param.h> | |
55e303ae | 76 | #include <sys/systm.h> |
1c79356b A |
77 | #include <sys/syslimits.h> |
78 | #include <sys/time.h> | |
79 | #include <sys/namei.h> | |
80 | #include <sys/vm.h> | |
91447636 A |
81 | #include <sys/vnode_internal.h> |
82 | #include <sys/mount_internal.h> | |
1c79356b A |
83 | #include <sys/errno.h> |
84 | #include <sys/malloc.h> | |
85 | #include <sys/filedesc.h> | |
91447636 | 86 | #include <sys/proc_internal.h> |
1c79356b A |
87 | #include <sys/kdebug.h> |
88 | #include <sys/unistd.h> /* For _PC_NAME_MAX */ | |
91447636 A |
89 | #include <sys/uio_internal.h> |
90 | #include <sys/kauth.h> | |
e5568f75 A |
91 | |
92 | #include <bsm/audit_kernel.h> | |
1c79356b | 93 | |
2d21ac55 A |
94 | #if CONFIG_MACF |
95 | #include <security/mac_framework.h> | |
96 | #endif | |
97 | ||
98 | #if NAMEDRSRCFORK | |
99 | #include <sys/xattr.h> | |
1c79356b | 100 | #endif |
2d21ac55 A |
101 | /* |
102 | * The minimum volfs-style pathname is 9. | |
103 | * Example: "/.vol/1/2" | |
104 | */ | |
105 | #define VOLFS_MIN_PATH_LEN 9 | |
1c79356b | 106 | |
91447636 A |
107 | |
108 | static void kdebug_lookup(struct vnode *dp, struct componentname *cnp); | |
55e303ae | 109 | |
2d21ac55 A |
110 | #if CONFIG_VOLFS |
111 | static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx); | |
112 | #endif | |
113 | ||
1c79356b A |
114 | /* |
115 | * Convert a pathname into a pointer to a locked inode. | |
116 | * | |
117 | * The FOLLOW flag is set when symbolic links are to be followed | |
118 | * when they occur at the end of the name translation process. | |
119 | * Symbolic links are always followed for all other pathname | |
120 | * components other than the last. | |
121 | * | |
122 | * The segflg defines whether the name is to be copied from user | |
123 | * space or kernel space. | |
124 | * | |
125 | * Overall outline of namei: | |
126 | * | |
127 | * copy in name | |
128 | * get starting directory | |
129 | * while (!done && !error) { | |
130 | * call lookup to search path. | |
131 | * if symbolic link, massage name in buffer and continue | |
132 | * } | |
2d21ac55 A |
133 | * |
134 | * Returns: 0 Success | |
135 | * ENOENT No such file or directory | |
136 | * ELOOP Too many levels of symbolic links | |
137 | * ENAMETOOLONG Filename too long | |
138 | * copyinstr:EFAULT Bad address | |
139 | * copyinstr:ENAMETOOLONG Filename too long | |
140 | * lookup:EBADF Bad file descriptor | |
141 | * lookup:EROFS | |
142 | * lookup:EACCES | |
143 | * lookup:EPERM | |
4a3eedf9 A |
144 | * lookup:ERECYCLE vnode was recycled from underneath us in lookup. |
145 | * This means we should re-drive lookup from this point. | |
146 | * lookup: ??? | |
2d21ac55 | 147 | * VNOP_READLINK:??? |
1c79356b A |
148 | */ |
149 | int | |
2d21ac55 | 150 | namei(struct nameidata *ndp) |
1c79356b | 151 | { |
2d21ac55 A |
152 | struct filedesc *fdp; /* pointer to file descriptor state */ |
153 | char *cp; /* pointer into pathname argument */ | |
154 | struct vnode *dp; /* the directory we are searching */ | |
4a3eedf9 A |
155 | struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to |
156 | heavy vnode pressure */ | |
157 | u_long cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */ | |
91447636 A |
158 | uio_t auio; |
159 | int error; | |
1c79356b | 160 | struct componentname *cnp = &ndp->ni_cnd; |
91447636 | 161 | vfs_context_t ctx = cnp->cn_context; |
2d21ac55 A |
162 | proc_t p = vfs_context_proc(ctx); |
163 | /* XXX ut should be from context */ | |
164 | uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread()); | |
55e303ae | 165 | char *tmppn; |
91447636 | 166 | char uio_buf[ UIO_SIZEOF(1) ]; |
1c79356b | 167 | |
1c79356b | 168 | #if DIAGNOSTIC |
91447636 | 169 | if (!vfs_context_ucred(ctx) || !p) |
1c79356b A |
170 | panic ("namei: bad cred/proc"); |
171 | if (cnp->cn_nameiop & (~OPMASK)) | |
172 | panic ("namei: nameiop contaminated with flags"); | |
173 | if (cnp->cn_flags & OPMASK) | |
174 | panic ("namei: flags contaminated with nameiops"); | |
175 | #endif | |
55e303ae | 176 | fdp = p->p_fd; |
1c79356b | 177 | |
4a3eedf9 A |
178 | vnode_recycled: |
179 | ||
1c79356b A |
180 | /* |
181 | * Get a buffer for the name to be translated, and copy the | |
182 | * name into the buffer. | |
183 | */ | |
184 | if ((cnp->cn_flags & HASBUF) == 0) { | |
2d21ac55 | 185 | cnp->cn_pnbuf = ndp->ni_pathbuf; |
91447636 | 186 | cnp->cn_pnlen = PATHBUFLEN; |
1c79356b | 187 | } |
91447636 A |
188 | #if LP64_DEBUG |
189 | if (IS_VALID_UIO_SEGFLG(ndp->ni_segflg) == 0) { | |
190 | panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__); | |
191 | } | |
192 | #endif /* LP64_DEBUG */ | |
193 | ||
194 | retry_copy: | |
2d21ac55 | 195 | if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) { |
1c79356b | 196 | error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, |
91447636 | 197 | cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen); |
2d21ac55 | 198 | } else { |
91447636 A |
199 | error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf, |
200 | cnp->cn_pnlen, (size_t *)&ndp->ni_pathlen); | |
2d21ac55 | 201 | } |
91447636 | 202 | if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) { |
2d21ac55 A |
203 | MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
204 | if (cnp->cn_pnbuf == NULL) { | |
205 | error = ENOMEM; | |
206 | goto error_out; | |
207 | } | |
91447636 A |
208 | |
209 | cnp->cn_flags |= HASBUF; | |
210 | cnp->cn_pnlen = MAXPATHLEN; | |
211 | ||
212 | goto retry_copy; | |
213 | } | |
214 | if (error) | |
215 | goto error_out; | |
55e303ae | 216 | |
2d21ac55 A |
217 | #if CONFIG_VOLFS |
218 | /* | |
219 | * Check for legacy volfs style pathnames. | |
220 | * | |
221 | * For compatibility reasons we currently allow these paths, | |
222 | * but future versions of the OS may not support them. | |
223 | */ | |
224 | if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN && | |
225 | cnp->cn_pnbuf[0] == '/' && | |
226 | cnp->cn_pnbuf[1] == '.' && | |
227 | cnp->cn_pnbuf[2] == 'v' && | |
228 | cnp->cn_pnbuf[3] == 'o' && | |
229 | cnp->cn_pnbuf[4] == 'l' && | |
230 | cnp->cn_pnbuf[5] == '/' ) { | |
231 | char * realpath; | |
232 | int realpath_err; | |
233 | /* Attempt to resolve a legacy volfs style pathname. */ | |
234 | MALLOC_ZONE(realpath, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); | |
235 | if (realpath) { | |
236 | if ((realpath_err= vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) { | |
237 | FREE_ZONE(realpath, MAXPATHLEN, M_NAMEI); | |
238 | if (realpath_err == ENOSPC){ | |
239 | error = ENAMETOOLONG; | |
240 | goto error_out; | |
241 | } | |
242 | } else { | |
243 | if (cnp->cn_flags & HASBUF) { | |
244 | FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI); | |
245 | } | |
246 | cnp->cn_pnbuf = realpath; | |
247 | cnp->cn_pnlen = MAXPATHLEN; | |
248 | ndp->ni_pathlen = strlen(realpath) + 1; | |
249 | cnp->cn_flags |= HASBUF | CN_VOLFSPATH; | |
250 | } | |
251 | } | |
252 | } | |
253 | #endif /* CONFIG_VOLFS */ | |
254 | ||
55e303ae A |
255 | /* If we are auditing the kernel pathname, save the user pathname */ |
256 | if (cnp->cn_flags & AUDITVNPATH1) | |
2d21ac55 | 257 | AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1); |
55e303ae | 258 | if (cnp->cn_flags & AUDITVNPATH2) |
2d21ac55 | 259 | AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2); |
55e303ae | 260 | |
1c79356b A |
261 | /* |
262 | * Do not allow empty pathnames | |
263 | */ | |
91447636 | 264 | if (*cnp->cn_pnbuf == '\0') { |
1c79356b | 265 | error = ENOENT; |
2d21ac55 | 266 | goto error_out; |
1c79356b A |
267 | } |
268 | ndp->ni_loopcnt = 0; | |
1c79356b A |
269 | |
270 | /* | |
91447636 | 271 | * determine the starting point for the translation. |
1c79356b | 272 | */ |
91447636 A |
273 | if ((ndp->ni_rootdir = fdp->fd_rdir) == NULLVP) { |
274 | if ( !(fdp->fd_flags & FD_CHROOT)) | |
275 | ndp->ni_rootdir = rootvnode; | |
55e303ae | 276 | } |
91447636 | 277 | cnp->cn_nameptr = cnp->cn_pnbuf; |
55e303ae | 278 | |
91447636 A |
279 | ndp->ni_usedvp = NULLVP; |
280 | ||
281 | if (*(cnp->cn_nameptr) == '/') { | |
282 | while (*(cnp->cn_nameptr) == '/') { | |
283 | cnp->cn_nameptr++; | |
284 | ndp->ni_pathlen--; | |
1c79356b | 285 | } |
91447636 A |
286 | dp = ndp->ni_rootdir; |
287 | } else if (cnp->cn_flags & USEDVP) { | |
288 | dp = ndp->ni_dvp; | |
289 | ndp->ni_usedvp = dp; | |
290 | } else | |
2d21ac55 | 291 | dp = vfs_context_cwd(ctx); |
91447636 | 292 | |
2d21ac55 | 293 | if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) { |
91447636 A |
294 | error = ENOENT; |
295 | goto error_out; | |
296 | } | |
297 | ndp->ni_dvp = NULLVP; | |
298 | ndp->ni_vp = NULLVP; | |
299 | ||
300 | for (;;) { | |
301 | int need_newpathbuf; | |
302 | int linklen; | |
303 | ||
1c79356b | 304 | ndp->ni_startdir = dp; |
91447636 A |
305 | |
306 | if ( (error = lookup(ndp)) ) { | |
307 | goto error_out; | |
1c79356b A |
308 | } |
309 | /* | |
310 | * Check for symbolic link | |
311 | */ | |
312 | if ((cnp->cn_flags & ISSYMLINK) == 0) { | |
1c79356b A |
313 | return (0); |
314 | } | |
91447636 A |
315 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
316 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
317 | unlock_fsnode(ndp->ni_dvp, NULL); | |
318 | } | |
1c79356b A |
319 | if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { |
320 | error = ELOOP; | |
321 | break; | |
322 | } | |
2d21ac55 A |
323 | #if CONFIG_MACF |
324 | if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0) | |
325 | break; | |
326 | #endif /* MAC */ | |
91447636 A |
327 | if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF)) |
328 | need_newpathbuf = 1; | |
329 | else | |
330 | need_newpathbuf = 0; | |
331 | ||
332 | if (need_newpathbuf) { | |
1c79356b | 333 | MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); |
2d21ac55 A |
334 | if (cp == NULL) { |
335 | error = ENOMEM; | |
336 | break; | |
337 | } | |
1c79356b A |
338 | } else { |
339 | cp = cnp->cn_pnbuf; | |
340 | } | |
91447636 A |
341 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, &uio_buf[0], sizeof(uio_buf)); |
342 | ||
343 | uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN); | |
344 | ||
345 | error = VNOP_READLINK(ndp->ni_vp, auio, ctx); | |
346 | if (error) { | |
347 | if (need_newpathbuf) | |
55e303ae | 348 | FREE_ZONE(cp, MAXPATHLEN, M_NAMEI); |
1c79356b A |
349 | break; |
350 | } | |
91447636 A |
351 | // LP64todo - fix this |
352 | linklen = MAXPATHLEN - uio_resid(auio); | |
353 | if (linklen + ndp->ni_pathlen > MAXPATHLEN) { | |
354 | if (need_newpathbuf) | |
55e303ae | 355 | FREE_ZONE(cp, MAXPATHLEN, M_NAMEI); |
91447636 | 356 | |
1c79356b A |
357 | error = ENAMETOOLONG; |
358 | break; | |
359 | } | |
91447636 | 360 | if (need_newpathbuf) { |
55e303ae | 361 | long len = cnp->cn_pnlen; |
91447636 | 362 | |
55e303ae | 363 | tmppn = cnp->cn_pnbuf; |
1c79356b | 364 | bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); |
1c79356b A |
365 | cnp->cn_pnbuf = cp; |
366 | cnp->cn_pnlen = MAXPATHLEN; | |
91447636 A |
367 | |
368 | if ( (cnp->cn_flags & HASBUF) ) | |
369 | FREE_ZONE(tmppn, len, M_NAMEI); | |
370 | else | |
371 | cnp->cn_flags |= HASBUF; | |
1c79356b A |
372 | } else |
373 | cnp->cn_pnbuf[linklen] = '\0'; | |
91447636 | 374 | |
1c79356b | 375 | ndp->ni_pathlen += linklen; |
91447636 A |
376 | cnp->cn_nameptr = cnp->cn_pnbuf; |
377 | ||
378 | /* | |
379 | * starting point for 'relative' | |
380 | * symbolic link path | |
381 | */ | |
1c79356b | 382 | dp = ndp->ni_dvp; |
91447636 A |
383 | /* |
384 | * get rid of references returned via 'lookup' | |
385 | */ | |
386 | vnode_put(ndp->ni_vp); | |
2d21ac55 | 387 | vnode_put(ndp->ni_dvp); |
91447636 A |
388 | |
389 | ndp->ni_vp = NULLVP; | |
390 | ndp->ni_dvp = NULLVP; | |
55e303ae | 391 | |
91447636 A |
392 | /* |
393 | * Check if symbolic link restarts us at the root | |
394 | */ | |
395 | if (*(cnp->cn_nameptr) == '/') { | |
396 | while (*(cnp->cn_nameptr) == '/') { | |
397 | cnp->cn_nameptr++; | |
398 | ndp->ni_pathlen--; | |
399 | } | |
400 | if ((dp = ndp->ni_rootdir) == NULLVP) { | |
401 | error = ENOENT; | |
402 | goto error_out; | |
403 | } | |
404 | } | |
405 | } | |
406 | /* | |
407 | * only come here if we fail to handle a SYMLINK... | |
408 | * if either ni_dvp or ni_vp is non-NULL, then | |
409 | * we need to drop the iocount that was picked | |
410 | * up in the lookup routine | |
411 | */ | |
412 | if (ndp->ni_dvp) | |
413 | vnode_put(ndp->ni_dvp); | |
414 | if (ndp->ni_vp) | |
415 | vnode_put(ndp->ni_vp); | |
416 | error_out: | |
417 | if ( (cnp->cn_flags & HASBUF) ) { | |
2d21ac55 | 418 | cnp->cn_flags &= ~HASBUF; |
91447636 A |
419 | FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI); |
420 | } | |
55e303ae | 421 | cnp->cn_pnbuf = NULL; |
91447636 | 422 | ndp->ni_vp = NULLVP; |
4a3eedf9 A |
423 | if (error == ERECYCLE){ |
424 | /* vnode was recycled underneath us. re-drive lookup to start at | |
425 | the beginning again, since recycling invalidated last lookup*/ | |
426 | ndp->ni_cnd.cn_flags = cnpflags; | |
427 | ndp->ni_dvp = usedvp; | |
428 | goto vnode_recycled; | |
429 | } | |
430 | ||
55e303ae | 431 | |
1c79356b A |
432 | return (error); |
433 | } | |
434 | ||
91447636 | 435 | |
1c79356b A |
436 | /* |
437 | * Search a pathname. | |
438 | * This is a very central and rather complicated routine. | |
439 | * | |
440 | * The pathname is pointed to by ni_ptr and is of length ni_pathlen. | |
441 | * The starting directory is taken from ni_startdir. The pathname is | |
442 | * descended until done, or a symbolic link is encountered. The variable | |
443 | * ni_more is clear if the path is completed; it is set to one if a | |
444 | * symbolic link needing interpretation is encountered. | |
445 | * | |
446 | * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on | |
447 | * whether the name is to be looked up, created, renamed, or deleted. | |
448 | * When CREATE, RENAME, or DELETE is specified, information usable in | |
449 | * creating, renaming, or deleting a directory entry may be calculated. | |
450 | * If flag has LOCKPARENT or'ed into it, the parent directory is returned | |
451 | * locked. If flag has WANTPARENT or'ed into it, the parent directory is | |
452 | * returned unlocked. Otherwise the parent directory is not returned. If | |
453 | * the target of the pathname exists and LOCKLEAF is or'ed into the flag | |
454 | * the target is returned locked, otherwise it is returned unlocked. | |
455 | * When creating or renaming and LOCKPARENT is specified, the target may not | |
456 | * be ".". When deleting and LOCKPARENT is specified, the target may be ".". | |
457 | * | |
458 | * Overall outline of lookup: | |
459 | * | |
460 | * dirloop: | |
461 | * identify next component of name at ndp->ni_ptr | |
462 | * handle degenerate case where name is null string | |
463 | * if .. and crossing mount points and on mounted filesys, find parent | |
91447636 | 464 | * call VNOP_LOOKUP routine for next component name |
1c79356b A |
465 | * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set |
466 | * component vnode returned in ni_vp (if it exists), locked. | |
467 | * if result vnode is mounted on and crossing mount points, | |
468 | * find mounted on vnode | |
469 | * if more components of name, do next level at dirloop | |
470 | * return the answer in ni_vp, locked if LOCKLEAF set | |
471 | * if LOCKPARENT set, return locked parent in ni_dvp | |
472 | * if WANTPARENT set, return unlocked parent in ni_dvp | |
2d21ac55 A |
473 | * |
474 | * Returns: 0 Success | |
475 | * ENOENT No such file or directory | |
476 | * EBADF Bad file descriptor | |
477 | * ENOTDIR Not a directory | |
478 | * EROFS Read-only file system [CREATE] | |
479 | * EISDIR Is a directory [CREATE] | |
4a3eedf9 | 480 | * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again) |
2d21ac55 A |
481 | * vnode_authorize:EROFS |
482 | * vnode_authorize:EACCES | |
483 | * vnode_authorize:EPERM | |
484 | * vnode_authorize:??? | |
485 | * VNOP_LOOKUP:ENOENT No such file or directory | |
486 | * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL) | |
487 | * VNOP_LOOKUP:??? | |
488 | * VFS_ROOT:ENOTSUP | |
489 | * VFS_ROOT:ENOENT | |
490 | * VFS_ROOT:??? | |
1c79356b A |
491 | */ |
492 | int | |
2d21ac55 | 493 | lookup(struct nameidata *ndp) |
1c79356b | 494 | { |
2d21ac55 | 495 | char *cp; /* pointer into pathname argument */ |
91447636 A |
496 | vnode_t tdp; /* saved dp */ |
497 | vnode_t dp; /* the directory we are searching */ | |
498 | mount_t mp; /* mount table entry */ | |
499 | int docache = 1; /* == 0 do not cache last component */ | |
1c79356b A |
500 | int wantparent; /* 1 => wantparent or lockparent flag */ |
501 | int rdonly; /* lookup read-only flag bit */ | |
55e303ae | 502 | int trailing_slash = 0; |
91447636 | 503 | int dp_authorized = 0; |
1c79356b A |
504 | int error = 0; |
505 | struct componentname *cnp = &ndp->ni_cnd; | |
91447636 | 506 | vfs_context_t ctx = cnp->cn_context; |
2d21ac55 A |
507 | int mounted_on_depth = 0; |
508 | int dont_cache_mp = 0; | |
509 | vnode_t mounted_on_dp = NULLVP; | |
510 | int current_mount_generation = 0; | |
511 | int vbusyflags = 0; | |
512 | int nc_generation = 0; | |
4a3eedf9 | 513 | vnode_t last_dp = NULLVP; |
1c79356b A |
514 | |
515 | /* | |
516 | * Setup: break out flag bits into variables. | |
517 | */ | |
91447636 A |
518 | if (cnp->cn_flags & (NOCACHE | DOWHITEOUT)) { |
519 | if ((cnp->cn_flags & NOCACHE) || (cnp->cn_nameiop == DELETE)) | |
520 | docache = 0; | |
521 | } | |
1c79356b | 522 | wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); |
1c79356b | 523 | rdonly = cnp->cn_flags & RDONLY; |
1c79356b | 524 | cnp->cn_flags &= ~ISSYMLINK; |
91447636 A |
525 | cnp->cn_consume = 0; |
526 | ||
1c79356b A |
527 | dp = ndp->ni_startdir; |
528 | ndp->ni_startdir = NULLVP; | |
1c79356b | 529 | |
2d21ac55 A |
530 | if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) |
531 | vbusyflags = LK_NOWAIT; | |
91447636 | 532 | cp = cnp->cn_nameptr; |
55e303ae | 533 | |
91447636 A |
534 | if (*cp == '\0') { |
535 | if ( (vnode_getwithref(dp)) ) { | |
536 | dp = NULLVP; | |
537 | error = ENOENT; | |
55e303ae A |
538 | goto bad; |
539 | } | |
91447636 | 540 | goto emptyname; |
1c79356b | 541 | } |
91447636 A |
542 | dirloop: |
543 | ndp->ni_vp = NULLVP; | |
9bccf70c | 544 | |
4a3eedf9 | 545 | if ( (error = cache_lookup_path(ndp, cnp, dp, ctx, &trailing_slash, &dp_authorized, last_dp)) ) { |
91447636 A |
546 | dp = NULLVP; |
547 | goto bad; | |
548 | } | |
549 | if ((cnp->cn_flags & ISLASTCN)) { | |
550 | if (docache) | |
551 | cnp->cn_flags |= MAKEENTRY; | |
552 | } else | |
553 | cnp->cn_flags |= MAKEENTRY; | |
554 | ||
555 | dp = ndp->ni_dvp; | |
556 | ||
557 | if (ndp->ni_vp != NULLVP) { | |
558 | /* | |
559 | * cache_lookup_path returned a non-NULL ni_vp then, | |
560 | * we're guaranteed that the dp is a VDIR, it's | |
561 | * been authorized, and vp is not ".." | |
2d21ac55 A |
562 | * |
563 | * make sure we don't try to enter the name back into | |
564 | * the cache if this vp is purged before we get to that | |
565 | * check since we won't have serialized behind whatever | |
566 | * activity is occurring in the FS that caused the purge | |
91447636 | 567 | */ |
2d21ac55 A |
568 | if (dp != NULLVP) |
569 | nc_generation = dp->v_nc_generation - 1; | |
570 | ||
91447636 | 571 | goto returned_from_lookup_path; |
9bccf70c | 572 | } |
1c79356b | 573 | |
1c79356b A |
574 | /* |
575 | * Handle "..": two special cases. | |
576 | * 1. If at root directory (e.g. after chroot) | |
577 | * or at absolute root directory | |
578 | * then ignore it so can't get out. | |
579 | * 2. If this vnode is the root of a mounted | |
580 | * filesystem, then replace it with the | |
581 | * vnode which was mounted on so we take the | |
582 | * .. in the other file system. | |
583 | */ | |
91447636 | 584 | if ( (cnp->cn_flags & ISDOTDOT) ) { |
1c79356b | 585 | for (;;) { |
91447636 A |
586 | if (dp == ndp->ni_rootdir || dp == rootvnode) { |
587 | ndp->ni_dvp = dp; | |
1c79356b | 588 | ndp->ni_vp = dp; |
91447636 A |
589 | /* |
590 | * we're pinned at the root | |
591 | * we've already got one reference on 'dp' | |
592 | * courtesy of cache_lookup_path... take | |
593 | * another one for the ".." | |
594 | * if we fail to get the new reference, we'll | |
595 | * drop our original down in 'bad' | |
596 | */ | |
597 | if ( (vnode_get(dp)) ) { | |
598 | error = ENOENT; | |
599 | goto bad; | |
600 | } | |
1c79356b A |
601 | goto nextname; |
602 | } | |
603 | if ((dp->v_flag & VROOT) == 0 || | |
604 | (cnp->cn_flags & NOCROSSMOUNT)) | |
91447636 | 605 | break; |
0b4e3aa0 | 606 | if (dp->v_mount == NULL) { /* forced umount */ |
91447636 | 607 | error = EBADF; |
0b4e3aa0 A |
608 | goto bad; |
609 | } | |
1c79356b | 610 | tdp = dp; |
91447636 A |
611 | dp = tdp->v_mount->mnt_vnodecovered; |
612 | ||
613 | vnode_put(tdp); | |
614 | ||
615 | if ( (vnode_getwithref(dp)) ) { | |
616 | dp = NULLVP; | |
617 | error = ENOENT; | |
618 | goto bad; | |
619 | } | |
620 | ndp->ni_dvp = dp; | |
621 | dp_authorized = 0; | |
1c79356b A |
622 | } |
623 | } | |
624 | ||
625 | /* | |
626 | * We now have a segment name to search for, and a directory to search. | |
627 | */ | |
628 | unionlookup: | |
91447636 A |
629 | ndp->ni_vp = NULLVP; |
630 | ||
631 | if (dp->v_type != VDIR) { | |
632 | error = ENOTDIR; | |
633 | goto lookup_error; | |
634 | } | |
2d21ac55 A |
635 | if ( (cnp->cn_flags & DONOTAUTH) != DONOTAUTH ) { |
636 | if (!dp_authorized) { | |
637 | error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx); | |
638 | if (error) | |
639 | goto lookup_error; | |
640 | } | |
641 | #if CONFIG_MACF | |
642 | error = mac_vnode_check_lookup(ctx, dp, cnp); | |
643 | if (error) | |
644 | goto lookup_error; | |
645 | #endif /* CONFIG_MACF */ | |
91447636 | 646 | } |
2d21ac55 A |
647 | |
648 | nc_generation = dp->v_nc_generation; | |
649 | ||
91447636 A |
650 | if ( (error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx)) ) { |
651 | lookup_error: | |
1c79356b | 652 | if ((error == ENOENT) && |
0b4e3aa0 | 653 | (dp->v_flag & VROOT) && (dp->v_mount != NULL) && |
1c79356b | 654 | (dp->v_mount->mnt_flag & MNT_UNION)) { |
91447636 A |
655 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
656 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
657 | unlock_fsnode(dp, NULL); | |
658 | } | |
1c79356b | 659 | tdp = dp; |
91447636 A |
660 | dp = tdp->v_mount->mnt_vnodecovered; |
661 | ||
662 | vnode_put(tdp); | |
663 | ||
664 | if ( (vnode_getwithref(dp)) ) { | |
665 | dp = NULLVP; | |
666 | error = ENOENT; | |
667 | goto bad; | |
668 | } | |
669 | ndp->ni_dvp = dp; | |
670 | dp_authorized = 0; | |
1c79356b A |
671 | goto unionlookup; |
672 | } | |
673 | ||
674 | if (error != EJUSTRETURN) | |
675 | goto bad; | |
91447636 A |
676 | |
677 | if (ndp->ni_vp != NULLVP) | |
678 | panic("leaf should be empty"); | |
679 | ||
1c79356b A |
680 | /* |
681 | * If creating and at end of pathname, then can consider | |
682 | * allowing file to be created. | |
683 | */ | |
684 | if (rdonly) { | |
685 | error = EROFS; | |
686 | goto bad; | |
687 | } | |
91447636 | 688 | if ((cnp->cn_flags & ISLASTCN) && trailing_slash && !(cnp->cn_flags & WILLBEDIR)) { |
9bccf70c A |
689 | error = ENOENT; |
690 | goto bad; | |
691 | } | |
1c79356b A |
692 | /* |
693 | * We return with ni_vp NULL to indicate that the entry | |
694 | * doesn't currently exist, leaving a pointer to the | |
91447636 | 695 | * referenced directory vnode in ndp->ni_dvp. |
1c79356b A |
696 | */ |
697 | if (cnp->cn_flags & SAVESTART) { | |
91447636 A |
698 | if ( (vnode_get(ndp->ni_dvp)) ) { |
699 | error = ENOENT; | |
700 | goto bad; | |
701 | } | |
1c79356b | 702 | ndp->ni_startdir = ndp->ni_dvp; |
1c79356b | 703 | } |
91447636 A |
704 | if (!wantparent) |
705 | vnode_put(ndp->ni_dvp); | |
706 | ||
1c79356b A |
707 | if (kdebug_enable) |
708 | kdebug_lookup(ndp->ni_dvp, cnp); | |
709 | return (0); | |
710 | } | |
91447636 A |
711 | returned_from_lookup_path: |
712 | dp = ndp->ni_vp; | |
1c79356b A |
713 | |
714 | /* | |
715 | * Take into account any additional components consumed by | |
716 | * the underlying filesystem. | |
717 | */ | |
718 | if (cnp->cn_consume > 0) { | |
719 | cnp->cn_nameptr += cnp->cn_consume; | |
720 | ndp->ni_next += cnp->cn_consume; | |
721 | ndp->ni_pathlen -= cnp->cn_consume; | |
722 | cnp->cn_consume = 0; | |
55e303ae | 723 | } else { |
91447636 A |
724 | if (dp->v_name == NULL || dp->v_parent == NULLVP) { |
725 | int isdot_or_dotdot; | |
726 | int update_flags = 0; | |
55e303ae | 727 | |
91447636 | 728 | isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT); |
55e303ae | 729 | |
91447636 A |
730 | if (isdot_or_dotdot == 0) { |
731 | if (dp->v_name == NULL) | |
732 | update_flags |= VNODE_UPDATE_NAME; | |
733 | if (ndp->ni_dvp != NULLVP && dp->v_parent == NULLVP) | |
734 | update_flags |= VNODE_UPDATE_PARENT; | |
735 | ||
736 | if (update_flags) | |
737 | vnode_update_identity(dp, ndp->ni_dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags); | |
738 | } | |
739 | } | |
91447636 A |
740 | if ( (cnp->cn_flags & MAKEENTRY) && (dp->v_flag & VNCACHEABLE) && LIST_FIRST(&dp->v_nclinks) == NULL) { |
741 | /* | |
742 | * missing from name cache, but should | |
743 | * be in it... this can happen if volfs | |
744 | * causes the vnode to be created or the | |
745 | * name cache entry got recycled but the | |
746 | * vnode didn't... | |
747 | * check to make sure that ni_dvp is valid | |
748 | * cache_lookup_path may return a NULL | |
2d21ac55 A |
749 | * do a quick check to see if the generation of the |
750 | * directory matches our snapshot... this will get | |
751 | * rechecked behind the name cache lock, but if it | |
752 | * already fails to match, no need to go any further | |
91447636 | 753 | */ |
2d21ac55 A |
754 | if (ndp->ni_dvp != NULLVP && (nc_generation == ndp->ni_dvp->v_nc_generation)) |
755 | cache_enter_with_gen(ndp->ni_dvp, dp, cnp, nc_generation); | |
55e303ae | 756 | } |
1c79356b A |
757 | } |
758 | ||
2d21ac55 A |
759 | mounted_on_dp = dp; |
760 | mounted_on_depth = 0; | |
761 | dont_cache_mp = 0; | |
762 | current_mount_generation = mount_generation; | |
1c79356b | 763 | /* |
91447636 | 764 | * Check to see if the vnode has been mounted on... |
1c79356b A |
765 | * if so find the root of the mounted file system. |
766 | */ | |
91447636 A |
767 | check_mounted_on: |
768 | if ((dp->v_type == VDIR) && dp->v_mountedhere && | |
769 | ((cnp->cn_flags & NOCROSSMOUNT) == 0)) { | |
770 | ||
771 | vnode_lock(dp); | |
772 | ||
773 | if ((dp->v_type == VDIR) && (mp = dp->v_mountedhere)) { | |
2d21ac55 | 774 | struct uthread *uth = (struct uthread *)get_bsdthread_info(current_thread()); |
91447636 A |
775 | |
776 | mp->mnt_crossref++; | |
777 | vnode_unlock(dp); | |
778 | ||
2d21ac55 A |
779 | |
780 | if (vfs_busy(mp, vbusyflags)) { | |
91447636 | 781 | mount_dropcrossref(mp, dp, 0); |
2d21ac55 A |
782 | if (vbusyflags == LK_NOWAIT) { |
783 | error = ENOENT; | |
784 | goto bad2; | |
785 | } | |
91447636 A |
786 | goto check_mounted_on; |
787 | } | |
2d21ac55 A |
788 | |
789 | /* | |
790 | * XXX - if this is the last component of the | |
791 | * pathname, and it's either not a lookup operation | |
792 | * or the NOTRIGGER flag is set for the operation, | |
793 | * set a uthread flag to let VFS_ROOT() for autofs | |
794 | * know it shouldn't trigger a mount. | |
795 | */ | |
796 | if ((cnp->cn_flags & ISLASTCN) && | |
797 | (cnp->cn_nameiop != LOOKUP || | |
798 | (cnp->cn_flags & NOTRIGGER))) { | |
799 | uth->uu_notrigger = 1; | |
800 | dont_cache_mp = 1; | |
801 | } | |
91447636 | 802 | error = VFS_ROOT(mp, &tdp, ctx); |
2d21ac55 A |
803 | /* XXX - clear the uthread flag */ |
804 | uth->uu_notrigger = 0; | |
91447636 A |
805 | /* |
806 | * mount_dropcrossref does a vnode_put | |
807 | * on dp if the 3rd arg is non-zero | |
808 | */ | |
809 | mount_dropcrossref(mp, dp, 1); | |
810 | dp = NULL; | |
811 | vfs_unbusy(mp); | |
812 | ||
813 | if (error) { | |
814 | goto bad2; | |
815 | } | |
816 | ndp->ni_vp = dp = tdp; | |
2d21ac55 | 817 | mounted_on_depth++; |
91447636 A |
818 | |
819 | goto check_mounted_on; | |
820 | } | |
821 | vnode_unlock(dp); | |
1c79356b A |
822 | } |
823 | ||
2d21ac55 A |
824 | #if CONFIG_MACF |
825 | if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) { | |
826 | error = vnode_label(vnode_mount(dp), NULL, dp, NULL, | |
827 | VNODE_LABEL_NEEDREF, ctx); | |
828 | if (error) | |
829 | goto bad2; | |
830 | } | |
831 | #endif | |
832 | ||
833 | if (mounted_on_depth && !dont_cache_mp) { | |
834 | mp = mounted_on_dp->v_mountedhere; | |
835 | ||
836 | if (mp) { | |
837 | mount_lock(mp); | |
838 | mp->mnt_realrootvp_vid = dp->v_id; | |
839 | mp->mnt_realrootvp = dp; | |
840 | mp->mnt_generation = current_mount_generation; | |
841 | mount_unlock(mp); | |
842 | } | |
843 | } | |
844 | ||
1c79356b A |
845 | /* |
846 | * Check for symbolic link | |
847 | */ | |
848 | if ((dp->v_type == VLNK) && | |
91447636 | 849 | ((cnp->cn_flags & FOLLOW) || trailing_slash || *ndp->ni_next == '/')) { |
1c79356b A |
850 | cnp->cn_flags |= ISSYMLINK; |
851 | return (0); | |
852 | } | |
853 | ||
9bccf70c A |
854 | /* |
855 | * Check for bogus trailing slashes. | |
856 | */ | |
55e303ae A |
857 | if (trailing_slash) { |
858 | if (dp->v_type != VDIR) { | |
859 | error = ENOTDIR; | |
860 | goto bad2; | |
861 | } | |
862 | trailing_slash = 0; | |
91447636 | 863 | } |
9bccf70c | 864 | |
1c79356b A |
865 | nextname: |
866 | /* | |
867 | * Not a symbolic link. If more pathname, | |
868 | * continue at next component, else return. | |
869 | */ | |
870 | if (*ndp->ni_next == '/') { | |
55e303ae A |
871 | cnp->cn_nameptr = ndp->ni_next + 1; |
872 | ndp->ni_pathlen--; | |
1c79356b A |
873 | while (*cnp->cn_nameptr == '/') { |
874 | cnp->cn_nameptr++; | |
875 | ndp->ni_pathlen--; | |
876 | } | |
91447636 A |
877 | vnode_put(ndp->ni_dvp); |
878 | ||
879 | cp = cnp->cn_nameptr; | |
880 | ||
881 | if (*cp == '\0') | |
882 | goto emptyname; | |
883 | ||
4a3eedf9 A |
884 | /* |
885 | * cache_lookup_path is now responsible for dropping io ref on dp | |
886 | * when it is called again in the dirloop. This ensures we hold | |
887 | * a ref on dp until we complete the next round of lookup. | |
888 | */ | |
889 | last_dp = dp; | |
1c79356b A |
890 | goto dirloop; |
891 | } | |
892 | ||
893 | /* | |
894 | * Disallow directory write attempts on read-only file systems. | |
895 | */ | |
896 | if (rdonly && | |
897 | (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { | |
898 | error = EROFS; | |
899 | goto bad2; | |
900 | } | |
901 | if (cnp->cn_flags & SAVESTART) { | |
91447636 A |
902 | /* |
903 | * note that we already hold a reference | |
904 | * on both dp and ni_dvp, but for some reason | |
905 | * can't get another one... in this case we | |
906 | * need to do vnode_put on dp in 'bad2' | |
907 | */ | |
908 | if ( (vnode_get(ndp->ni_dvp)) ) { | |
909 | error = ENOENT; | |
910 | goto bad2; | |
911 | } | |
1c79356b | 912 | ndp->ni_startdir = ndp->ni_dvp; |
1c79356b | 913 | } |
2d21ac55 | 914 | if (!wantparent && ndp->ni_dvp) { |
91447636 | 915 | vnode_put(ndp->ni_dvp); |
2d21ac55 A |
916 | ndp->ni_dvp = NULLVP; |
917 | } | |
91447636 | 918 | |
55e303ae A |
919 | if (cnp->cn_flags & AUDITVNPATH1) |
920 | AUDIT_ARG(vnpath, dp, ARG_VNODE1); | |
921 | else if (cnp->cn_flags & AUDITVNPATH2) | |
922 | AUDIT_ARG(vnpath, dp, ARG_VNODE2); | |
91447636 | 923 | |
2d21ac55 A |
924 | #if NAMEDRSRCFORK |
925 | /* | |
926 | * Caller wants the resource fork. | |
927 | */ | |
928 | if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) { | |
929 | vnode_t svp = NULLVP; | |
930 | enum nsoperation nsop; | |
931 | ||
932 | if (dp->v_type != VREG) { | |
933 | error = ENOENT; | |
934 | goto bad2; | |
935 | } | |
936 | switch (cnp->cn_nameiop) { | |
937 | case DELETE: | |
938 | nsop = NS_DELETE; | |
939 | break; | |
940 | case CREATE: | |
941 | nsop = NS_CREATE; | |
942 | break; | |
943 | case LOOKUP: | |
944 | /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */ | |
945 | if (cnp->cn_flags & CN_ALLOWRSRCFORK) { | |
946 | nsop = NS_OPEN; | |
947 | } else { | |
948 | error = EPERM; | |
949 | goto bad2; | |
950 | } | |
951 | break; | |
952 | default: | |
953 | error = EPERM; | |
954 | goto bad2; | |
955 | } | |
956 | /* Ask the file system for the resource fork. */ | |
957 | error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, 0, ctx); | |
958 | ||
959 | /* During a create, it OK for stream vnode to be missing. */ | |
960 | if (error == ENOATTR || error == ENOENT) { | |
961 | error = (nsop == NS_CREATE) ? 0 : ENOENT; | |
962 | } | |
963 | if (error) { | |
964 | goto bad2; | |
965 | } | |
966 | /* The "parent" of the stream is the file. */ | |
967 | if (wantparent) { | |
968 | if (ndp->ni_dvp) { | |
969 | if (ndp->ni_cnd.cn_flags & FSNODELOCKHELD) { | |
970 | ndp->ni_cnd.cn_flags &= ~FSNODELOCKHELD; | |
971 | unlock_fsnode(ndp->ni_dvp, NULL); | |
972 | } | |
973 | vnode_put(ndp->ni_dvp); | |
974 | } | |
975 | ndp->ni_dvp = dp; | |
976 | } else { | |
977 | vnode_put(dp); | |
978 | } | |
979 | ndp->ni_vp = dp = svp; /* on create this may be null */ | |
980 | ||
981 | /* Restore the truncated pathname buffer (for audits). */ | |
982 | if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') { | |
983 | ndp->ni_next[0] = '/'; | |
984 | } | |
985 | cnp->cn_flags &= ~MAKEENTRY; | |
986 | } | |
987 | #endif | |
1c79356b A |
988 | if (kdebug_enable) |
989 | kdebug_lookup(dp, cnp); | |
990 | return (0); | |
991 | ||
55e303ae | 992 | emptyname: |
91447636 | 993 | cnp->cn_namelen = 0; |
55e303ae A |
994 | /* |
995 | * A degenerate name (e.g. / or "") which is a way of | |
996 | * talking about a directory, e.g. like "/." or ".". | |
997 | */ | |
998 | if (dp->v_type != VDIR) { | |
999 | error = ENOTDIR; | |
1000 | goto bad; | |
1001 | } | |
1002 | if (cnp->cn_nameiop != LOOKUP) { | |
1003 | error = EISDIR; | |
1004 | goto bad; | |
1005 | } | |
1006 | if (wantparent) { | |
91447636 A |
1007 | /* |
1008 | * note that we already hold a reference | |
1009 | * on dp, but for some reason can't | |
1010 | * get another one... in this case we | |
1011 | * need to do vnode_put on dp in 'bad' | |
1012 | */ | |
1013 | if ( (vnode_get(dp)) ) { | |
1014 | error = ENOENT; | |
1015 | goto bad; | |
1016 | } | |
55e303ae | 1017 | ndp->ni_dvp = dp; |
55e303ae A |
1018 | } |
1019 | cnp->cn_flags &= ~ISDOTDOT; | |
1020 | cnp->cn_flags |= ISLASTCN; | |
1021 | ndp->ni_next = cp; | |
1022 | ndp->ni_vp = dp; | |
91447636 | 1023 | |
55e303ae A |
1024 | if (cnp->cn_flags & AUDITVNPATH1) |
1025 | AUDIT_ARG(vnpath, dp, ARG_VNODE1); | |
1026 | else if (cnp->cn_flags & AUDITVNPATH2) | |
1027 | AUDIT_ARG(vnpath, dp, ARG_VNODE2); | |
55e303ae A |
1028 | if (cnp->cn_flags & SAVESTART) |
1029 | panic("lookup: SAVESTART"); | |
1030 | return (0); | |
1031 | ||
1c79356b | 1032 | bad2: |
91447636 A |
1033 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
1034 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
1035 | unlock_fsnode(ndp->ni_dvp, NULL); | |
1036 | } | |
1037 | if (ndp->ni_dvp) | |
1038 | vnode_put(ndp->ni_dvp); | |
1039 | if (dp) | |
1040 | vnode_put(dp); | |
1041 | ndp->ni_vp = NULLVP; | |
1042 | ||
1043 | if (kdebug_enable) | |
1044 | kdebug_lookup(dp, cnp); | |
1045 | return (error); | |
1046 | ||
1c79356b | 1047 | bad: |
91447636 A |
1048 | if ((cnp->cn_flags & FSNODELOCKHELD)) { |
1049 | cnp->cn_flags &= ~FSNODELOCKHELD; | |
1050 | unlock_fsnode(ndp->ni_dvp, NULL); | |
1051 | } | |
1052 | if (dp) | |
1053 | vnode_put(dp); | |
1054 | ndp->ni_vp = NULLVP; | |
1055 | ||
1c79356b A |
1056 | if (kdebug_enable) |
1057 | kdebug_lookup(dp, cnp); | |
1058 | return (error); | |
1059 | } | |
1060 | ||
1061 | /* | |
1062 | * relookup - lookup a path name component | |
1063 | * Used by lookup to re-aquire things. | |
1064 | */ | |
1065 | int | |
2d21ac55 | 1066 | relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) |
1c79356b | 1067 | { |
2d21ac55 | 1068 | struct vnode *dp = NULL; /* the directory we are searching */ |
1c79356b A |
1069 | int wantparent; /* 1 => wantparent or lockparent flag */ |
1070 | int rdonly; /* lookup read-only flag bit */ | |
1071 | int error = 0; | |
1072 | #ifdef NAMEI_DIAGNOSTIC | |
55e303ae | 1073 | int i, newhash; /* DEBUG: check name hash */ |
1c79356b A |
1074 | char *cp; /* DEBUG: check name ptr/len */ |
1075 | #endif | |
91447636 | 1076 | vfs_context_t ctx = cnp->cn_context;; |
1c79356b A |
1077 | |
1078 | /* | |
1079 | * Setup: break out flag bits into variables. | |
1080 | */ | |
1081 | wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT); | |
1c79356b A |
1082 | rdonly = cnp->cn_flags & RDONLY; |
1083 | cnp->cn_flags &= ~ISSYMLINK; | |
1c79356b | 1084 | |
91447636 A |
1085 | if (cnp->cn_flags & NOCACHE) |
1086 | cnp->cn_flags &= ~MAKEENTRY; | |
1087 | else | |
1088 | cnp->cn_flags |= MAKEENTRY; | |
1089 | ||
1090 | dp = dvp; | |
1c79356b A |
1091 | |
1092 | /* | |
1093 | * Check for degenerate name (e.g. / or "") | |
1094 | * which is a way of talking about a directory, | |
1095 | * e.g. like "/." or ".". | |
1096 | */ | |
1097 | if (cnp->cn_nameptr[0] == '\0') { | |
1098 | if (cnp->cn_nameiop != LOOKUP || wantparent) { | |
1099 | error = EISDIR; | |
1100 | goto bad; | |
1101 | } | |
1102 | if (dp->v_type != VDIR) { | |
1103 | error = ENOTDIR; | |
1104 | goto bad; | |
1105 | } | |
91447636 A |
1106 | if ( (vnode_get(dp)) ) { |
1107 | error = ENOENT; | |
1108 | goto bad; | |
1109 | } | |
1c79356b | 1110 | *vpp = dp; |
91447636 | 1111 | |
1c79356b A |
1112 | if (cnp->cn_flags & SAVESTART) |
1113 | panic("lookup: SAVESTART"); | |
1114 | return (0); | |
1115 | } | |
1c79356b A |
1116 | /* |
1117 | * We now have a segment name to search for, and a directory to search. | |
1118 | */ | |
91447636 A |
1119 | if ( (error = VNOP_LOOKUP(dp, vpp, cnp, ctx)) ) { |
1120 | if (error != EJUSTRETURN) | |
1121 | goto bad; | |
1c79356b A |
1122 | #if DIAGNOSTIC |
1123 | if (*vpp != NULL) | |
1124 | panic("leaf should be empty"); | |
1125 | #endif | |
1c79356b A |
1126 | /* |
1127 | * If creating and at end of pathname, then can consider | |
1128 | * allowing file to be created. | |
1129 | */ | |
1130 | if (rdonly) { | |
1131 | error = EROFS; | |
1132 | goto bad; | |
1133 | } | |
1c79356b A |
1134 | /* |
1135 | * We return with ni_vp NULL to indicate that the entry | |
1136 | * doesn't currently exist, leaving a pointer to the | |
1137 | * (possibly locked) directory inode in ndp->ni_dvp. | |
1138 | */ | |
1139 | return (0); | |
1140 | } | |
1141 | dp = *vpp; | |
1142 | ||
1143 | #if DIAGNOSTIC | |
1144 | /* | |
1145 | * Check for symbolic link | |
1146 | */ | |
1147 | if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) | |
1148 | panic ("relookup: symlink found.\n"); | |
1149 | #endif | |
1150 | ||
1151 | /* | |
1152 | * Disallow directory write attempts on read-only file systems. | |
1153 | */ | |
1154 | if (rdonly && | |
1155 | (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { | |
1156 | error = EROFS; | |
1157 | goto bad2; | |
1158 | } | |
1159 | /* ASSERT(dvp == ndp->ni_startdir) */ | |
1c79356b | 1160 | |
1c79356b A |
1161 | return (0); |
1162 | ||
1163 | bad2: | |
91447636 A |
1164 | vnode_put(dp); |
1165 | bad: | |
1c79356b | 1166 | *vpp = NULL; |
91447636 | 1167 | |
1c79356b A |
1168 | return (error); |
1169 | } | |
1170 | ||
91447636 A |
1171 | /* |
1172 | * Free pathname buffer | |
1173 | */ | |
1174 | void | |
1175 | nameidone(struct nameidata *ndp) | |
1176 | { | |
1177 | if ((ndp->ni_cnd.cn_flags & FSNODELOCKHELD)) { | |
1178 | ndp->ni_cnd.cn_flags &= ~FSNODELOCKHELD; | |
1179 | unlock_fsnode(ndp->ni_dvp, NULL); | |
1180 | } | |
1181 | if (ndp->ni_cnd.cn_flags & HASBUF) { | |
1182 | char *tmp = ndp->ni_cnd.cn_pnbuf; | |
1183 | ||
1184 | ndp->ni_cnd.cn_pnbuf = NULL; | |
1185 | ndp->ni_cnd.cn_flags &= ~HASBUF; | |
1186 | FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI); | |
1187 | } | |
1188 | } | |
1189 | ||
1c79356b | 1190 | |
0b4e3aa0 | 1191 | #define NUMPARMS 23 |
1c79356b | 1192 | |
2d21ac55 A |
1193 | /* |
1194 | * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used | |
1195 | * by fs_usage. The path up to and including the current component name are | |
1196 | * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path | |
1197 | * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged. | |
1198 | * That is, the truncation removes the leading portion of the path. | |
1199 | * | |
1200 | * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one | |
1201 | * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END | |
1202 | * (in addition to DBG_FUNC_START if it is also the first). There may be | |
1203 | * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END. | |
1204 | * | |
1205 | * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of | |
1206 | * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname | |
1207 | * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass | |
1208 | * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT | |
1209 | * (because not all of the 12 or 16 bytes are needed for the remainder of the | |
1210 | * path) is set to zero bytes, or '>' if there is more path beyond the | |
1211 | * current component name (usually because an intermediate component was not | |
1212 | * found). | |
1213 | * | |
1214 | * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form | |
1215 | * 12+N*16, there will be no padding. | |
1216 | * | |
1217 | * TODO: If there is more path beyond the current component name, should we | |
1218 | * force some padding? For example, a lookup for /foo_bar_baz/spam that | |
1219 | * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with | |
1220 | * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>". | |
1221 | */ | |
55e303ae | 1222 | static void |
2d21ac55 | 1223 | kdebug_lookup(struct vnode *dp, struct componentname *cnp) |
1c79356b | 1224 | { |
2d21ac55 A |
1225 | unsigned int i; |
1226 | int code; | |
1227 | int dbg_namelen; | |
1228 | char *dbg_nameptr; | |
1c79356b | 1229 | long dbg_parms[NUMPARMS]; |
1c79356b A |
1230 | |
1231 | /* Collect the pathname for tracing */ | |
1232 | dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen; | |
1233 | dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen; | |
1234 | ||
2d21ac55 A |
1235 | if (dbg_namelen > (int)sizeof(dbg_parms)) |
1236 | dbg_namelen = sizeof(dbg_parms); | |
1c79356b | 1237 | dbg_nameptr -= dbg_namelen; |
2d21ac55 A |
1238 | |
1239 | /* Copy the (possibly truncated) path itself */ | |
1240 | memcpy(dbg_parms, dbg_nameptr, dbg_namelen); | |
1241 | ||
1242 | /* Pad with '\0' or '>' */ | |
1243 | if (dbg_namelen < (int)sizeof(dbg_parms)) { | |
1244 | memset((char *)dbg_parms + dbg_namelen, | |
1245 | *(cnp->cn_nameptr + cnp->cn_namelen) ? '>' : 0, | |
1246 | sizeof(dbg_parms) - dbg_namelen); | |
1c79356b | 1247 | } |
2d21ac55 | 1248 | |
9bccf70c | 1249 | /* |
0c530ab8 | 1250 | * In the event that we collect multiple, consecutive pathname |
2d21ac55 | 1251 | * entries, we must mark the start of the path's string and the end. |
0c530ab8 A |
1252 | */ |
1253 | code = (FSDBG_CODE(DBG_FSRW,36)) | DBG_FUNC_START; | |
1254 | ||
2d21ac55 A |
1255 | if (dbg_namelen <= 12) |
1256 | code |= DBG_FUNC_END; | |
0c530ab8 A |
1257 | |
1258 | KERNEL_DEBUG_CONSTANT(code, (unsigned int)dp, dbg_parms[0], dbg_parms[1], dbg_parms[2], 0); | |
1259 | ||
1260 | code &= ~DBG_FUNC_START; | |
1261 | ||
2d21ac55 A |
1262 | for (i=3, dbg_namelen -= 12; dbg_namelen > 0; i+=4, dbg_namelen -= 16) { |
1263 | if (dbg_namelen <= 16) | |
1264 | code |= DBG_FUNC_END; | |
0c530ab8 | 1265 | |
2d21ac55 A |
1266 | KERNEL_DEBUG_CONSTANT(code, dbg_parms[i], dbg_parms[i+1], dbg_parms[i+2], dbg_parms[i+3], 0); |
1267 | } | |
1268 | } | |
0c530ab8 | 1269 | |
2d21ac55 A |
1270 | /* |
1271 | * Obtain the real path from a legacy volfs style path. | |
1272 | * | |
1273 | * Valid formats of input path: | |
1274 | * | |
1275 | * "555/@" | |
1276 | * "555/2" | |
1277 | * "555/123456" | |
1278 | * "555/123456/foobar" | |
1279 | * | |
1280 | * Where: | |
1281 | * 555 represents the volfs file system id | |
1282 | * '@' and '2' are aliases to the root of a file system | |
1283 | * 123456 represents a file id | |
1284 | * "foobar" represents a file name | |
1285 | */ | |
1286 | #if CONFIG_VOLFS | |
1287 | static int | |
1288 | vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx) | |
1289 | { | |
1290 | vnode_t vp; | |
1291 | struct mount *mp = NULL; | |
1292 | char *str; | |
1293 | char ch; | |
1294 | unsigned long id; | |
1295 | ino64_t ino; | |
1296 | int error; | |
1297 | int length; | |
1298 | ||
1299 | /* Get file system id and move str to next component. */ | |
1300 | id = strtoul(path, &str, 10); | |
1301 | if (id == 0 || str[0] != '/') { | |
1302 | return (EINVAL); | |
1303 | } | |
1304 | while (*str == '/') { | |
1305 | str++; | |
0c530ab8 | 1306 | } |
2d21ac55 A |
1307 | ch = *str; |
1308 | ||
1309 | mp = mount_lookupby_volfsid(id, 1); | |
1310 | if (mp == NULL) { | |
1311 | return (EINVAL); /* unexpected failure */ | |
1312 | } | |
1313 | /* Check for an alias to a file system root. */ | |
1314 | if (ch == '@' && str[1] == '\0') { | |
1315 | ino = 2; | |
1316 | str++; | |
1317 | } else { | |
1318 | /* Get file id and move str to next component. */ | |
1319 | ino = strtouq(str, &str, 10); | |
1320 | } | |
1321 | ||
1322 | /* Get the target vnode. */ | |
1323 | if (ino == 2) { | |
1324 | error = VFS_ROOT(mp, &vp, ctx); | |
1325 | } else { | |
1326 | error = VFS_VGET(mp, ino, &vp, ctx); | |
1327 | } | |
1328 | vfs_unbusy(mp); | |
1329 | if (error) { | |
1330 | goto out; | |
1331 | } | |
1332 | realpath[0] = '\0'; | |
1333 | ||
1334 | /* Get the absolute path to this vnode. */ | |
1335 | error = build_path(vp, realpath, bufsize, &length, 0, ctx); | |
1336 | vnode_put(vp); | |
1337 | ||
1338 | if (error == 0 && *str != '\0') { | |
1339 | int attempt = strlcat(realpath, str, MAXPATHLEN); | |
1340 | if (attempt > MAXPATHLEN){ | |
1341 | error = ENAMETOOLONG; | |
1342 | } | |
1343 | } | |
1344 | out: | |
1345 | return (error); | |
1c79356b | 1346 | } |
2d21ac55 | 1347 | #endif |