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