]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
3e170ce0 | 2 | * Copyright (c) 2000-2015 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
0a7de745 | 5 | * |
2d21ac55 A |
6 | * This file contains Original Code and/or Modifications of Original Code |
7 | * as defined in and that are subject to the Apple Public Source License | |
8 | * Version 2.0 (the 'License'). You may not use this file except in | |
9 | * compliance with the License. The rights granted to you under the License | |
10 | * may not be used to create, or enable the creation or redistribution of, | |
11 | * unlawful or unlicensed copies of an Apple operating system, or to | |
12 | * circumvent, violate, or enable the circumvention or violation of, any | |
13 | * terms of an Apple operating system software license agreement. | |
0a7de745 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
0a7de745 | 17 | * |
2d21ac55 A |
18 | * The Original Code and all software distributed under the License are |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
22 | * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. |
23 | * Please see the License for the specific language governing rights and | |
24 | * limitations under the License. | |
0a7de745 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* 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 | 87 | #include <sys/kdebug.h> |
0a7de745 | 88 | #include <sys/unistd.h> /* For _PC_NAME_MAX */ |
91447636 A |
89 | #include <sys/uio_internal.h> |
90 | #include <sys/kauth.h> | |
39236c6e | 91 | #include <kern/kalloc.h> |
b0d623f7 | 92 | #include <security/audit/audit.h> |
3e170ce0 | 93 | #include <sys/dtrace.h> /* to get the prototype for strstr() in sys/dtrace_glue.h */ |
2d21ac55 A |
94 | #if CONFIG_MACF |
95 | #include <security/mac_framework.h> | |
96 | #endif | |
97 | ||
cb323159 A |
98 | #include <sys/paths.h> |
99 | ||
2d21ac55 A |
100 | #if NAMEDRSRCFORK |
101 | #include <sys/xattr.h> | |
1c79356b | 102 | #endif |
2d21ac55 A |
103 | /* |
104 | * The minimum volfs-style pathname is 9. | |
105 | * Example: "/.vol/1/2" | |
106 | */ | |
107 | #define VOLFS_MIN_PATH_LEN 9 | |
1c79356b | 108 | |
91447636 | 109 | |
2d21ac55 A |
110 | #if CONFIG_VOLFS |
111 | static int vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx); | |
39236c6e | 112 | #define MAX_VOLFS_RESTARTS 5 |
2d21ac55 A |
113 | #endif |
114 | ||
0a7de745 | 115 | static int lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, int vbusyflags, vfs_context_t ctx); |
bca245ac | 116 | static int lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx); |
0a7de745 A |
117 | static int lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx); |
118 | static void lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation); | |
119 | static int lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly, | |
120 | int vbusyflags, int *keep_going, int nc_generation, | |
121 | int wantparent, int atroot, vfs_context_t ctx); | |
122 | static int lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent); | |
6d2010ae | 123 | |
fe8ab488 | 124 | #if NAMEDRSRCFORK |
0a7de745 | 125 | static int lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx); |
fe8ab488 | 126 | #endif |
39236c6e A |
127 | |
128 | ||
129 | ||
1c79356b A |
130 | /* |
131 | * Convert a pathname into a pointer to a locked inode. | |
132 | * | |
133 | * The FOLLOW flag is set when symbolic links are to be followed | |
134 | * when they occur at the end of the name translation process. | |
135 | * Symbolic links are always followed for all other pathname | |
136 | * components other than the last. | |
137 | * | |
138 | * The segflg defines whether the name is to be copied from user | |
139 | * space or kernel space. | |
140 | * | |
141 | * Overall outline of namei: | |
142 | * | |
143 | * copy in name | |
144 | * get starting directory | |
145 | * while (!done && !error) { | |
146 | * call lookup to search path. | |
147 | * if symbolic link, massage name in buffer and continue | |
148 | * } | |
2d21ac55 A |
149 | * |
150 | * Returns: 0 Success | |
151 | * ENOENT No such file or directory | |
152 | * ELOOP Too many levels of symbolic links | |
153 | * ENAMETOOLONG Filename too long | |
154 | * copyinstr:EFAULT Bad address | |
155 | * copyinstr:ENAMETOOLONG Filename too long | |
156 | * lookup:EBADF Bad file descriptor | |
157 | * lookup:EROFS | |
158 | * lookup:EACCES | |
159 | * lookup:EPERM | |
4a3eedf9 A |
160 | * lookup:ERECYCLE vnode was recycled from underneath us in lookup. |
161 | * This means we should re-drive lookup from this point. | |
162 | * lookup: ??? | |
2d21ac55 | 163 | * VNOP_READLINK:??? |
1c79356b A |
164 | */ |
165 | int | |
2d21ac55 | 166 | namei(struct nameidata *ndp) |
1c79356b | 167 | { |
0a7de745 A |
168 | struct filedesc *fdp; /* pointer to file descriptor state */ |
169 | struct vnode *dp; /* the directory we are searching */ | |
4a3eedf9 | 170 | struct vnode *usedvp = ndp->ni_dvp; /* store pointer to vp in case we must loop due to |
0a7de745 | 171 | * heavy vnode pressure */ |
4a3eedf9 | 172 | u_long cnpflags = ndp->ni_cnd.cn_flags; /* store in case we have to restore after loop */ |
91447636 | 173 | int error; |
1c79356b | 174 | struct componentname *cnp = &ndp->ni_cnd; |
91447636 | 175 | vfs_context_t ctx = cnp->cn_context; |
2d21ac55 | 176 | proc_t p = vfs_context_proc(ctx); |
b0d623f7 | 177 | #if CONFIG_AUDIT |
2d21ac55 A |
178 | /* XXX ut should be from context */ |
179 | uthread_t ut = (struct uthread *)get_bsdthread_info(current_thread()); | |
b0d623f7 | 180 | #endif |
6d2010ae | 181 | |
39236c6e A |
182 | #if CONFIG_VOLFS |
183 | int volfs_restarts = 0; | |
184 | #endif | |
5ba3f43e | 185 | size_t bytes_copied = 0; |
bca245ac A |
186 | bool take_proc_lock = !(ndp->ni_flag & NAMEI_NOPROCLOCK); |
187 | bool proc_lock_taken = false; | |
39236c6e | 188 | |
6d2010ae | 189 | fdp = p->p_fd; |
1c79356b | 190 | |
1c79356b | 191 | #if DIAGNOSTIC |
0a7de745 A |
192 | if (!vfs_context_ucred(ctx) || !p) { |
193 | panic("namei: bad cred/proc"); | |
194 | } | |
195 | if (cnp->cn_nameiop & (~OPMASK)) { | |
196 | panic("namei: nameiop contaminated with flags"); | |
197 | } | |
198 | if (cnp->cn_flags & OPMASK) { | |
199 | panic("namei: flags contaminated with nameiops"); | |
200 | } | |
1c79356b | 201 | #endif |
6d2010ae A |
202 | |
203 | /* | |
204 | * A compound VNOP found something that needs further processing: | |
205 | * either a trigger vnode, a covered directory, or a symlink. | |
206 | */ | |
207 | if (ndp->ni_flag & NAMEI_CONTLOOKUP) { | |
208 | int rdonly, vbusyflags, keep_going, wantparent; | |
209 | ||
210 | rdonly = cnp->cn_flags & RDONLY; | |
211 | vbusyflags = ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) ? LK_NOWAIT : 0; | |
212 | keep_going = 0; | |
213 | wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); | |
214 | ||
215 | ndp->ni_flag &= ~(NAMEI_CONTLOOKUP); | |
216 | ||
0a7de745 A |
217 | error = lookup_handle_found_vnode(ndp, &ndp->ni_cnd, rdonly, vbusyflags, |
218 | &keep_going, ndp->ni_ncgeneration, wantparent, 0, ctx); | |
219 | if (error) { | |
6d2010ae | 220 | goto out_drop; |
0a7de745 | 221 | } |
6d2010ae A |
222 | if (keep_going) { |
223 | if ((cnp->cn_flags & ISSYMLINK) == 0) { | |
224 | panic("We need to keep going on a continued lookup, but for vp type %d (tag %d)\n", ndp->ni_vp->v_type, ndp->ni_vp->v_tag); | |
225 | } | |
226 | goto continue_symlink; | |
227 | } | |
228 | ||
229 | return 0; | |
6d2010ae | 230 | } |
1c79356b | 231 | |
4a3eedf9 A |
232 | vnode_recycled: |
233 | ||
1c79356b A |
234 | /* |
235 | * Get a buffer for the name to be translated, and copy the | |
236 | * name into the buffer. | |
237 | */ | |
238 | if ((cnp->cn_flags & HASBUF) == 0) { | |
2d21ac55 | 239 | cnp->cn_pnbuf = ndp->ni_pathbuf; |
91447636 | 240 | cnp->cn_pnlen = PATHBUFLEN; |
1c79356b | 241 | } |
91447636 | 242 | #if LP64_DEBUG |
b0d623f7 | 243 | if ((UIO_SEG_IS_USER_SPACE(ndp->ni_segflg) == 0) |
0a7de745 A |
244 | && (ndp->ni_segflg != UIO_SYSSPACE) |
245 | && (ndp->ni_segflg != UIO_SYSSPACE32)) { | |
246 | panic("%s :%d - invalid ni_segflg\n", __FILE__, __LINE__); | |
91447636 A |
247 | } |
248 | #endif /* LP64_DEBUG */ | |
249 | ||
250 | retry_copy: | |
2d21ac55 | 251 | if (UIO_SEG_IS_USER_SPACE(ndp->ni_segflg)) { |
1c79356b | 252 | error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf, |
0a7de745 | 253 | cnp->cn_pnlen, &bytes_copied); |
2d21ac55 | 254 | } else { |
91447636 | 255 | error = copystr(CAST_DOWN(void *, ndp->ni_dirp), cnp->cn_pnbuf, |
0a7de745 | 256 | cnp->cn_pnlen, &bytes_copied); |
2d21ac55 | 257 | } |
91447636 | 258 | if (error == ENAMETOOLONG && !(cnp->cn_flags & HASBUF)) { |
2d21ac55 A |
259 | MALLOC_ZONE(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); |
260 | if (cnp->cn_pnbuf == NULL) { | |
261 | error = ENOMEM; | |
262 | goto error_out; | |
263 | } | |
91447636 A |
264 | |
265 | cnp->cn_flags |= HASBUF; | |
266 | cnp->cn_pnlen = MAXPATHLEN; | |
5ba3f43e | 267 | bytes_copied = 0; |
0a7de745 | 268 | |
91447636 A |
269 | goto retry_copy; |
270 | } | |
0a7de745 A |
271 | if (error) { |
272 | goto error_out; | |
273 | } | |
5ba3f43e A |
274 | ndp->ni_pathlen = bytes_copied; |
275 | bytes_copied = 0; | |
55e303ae | 276 | |
39236c6e A |
277 | /* |
278 | * Since the name cache may contain positive entries of | |
279 | * the incorrect case, force lookup() to bypass the cache | |
280 | * and call directly into the filesystem for each path | |
281 | * component. Note: the FS may still consult the cache, | |
282 | * but can apply rules to validate the results. | |
283 | */ | |
0a7de745 | 284 | if (proc_is_forcing_hfs_case_sensitivity(p)) { |
39236c6e | 285 | cnp->cn_flags |= CN_SKIPNAMECACHE; |
0a7de745 | 286 | } |
39236c6e | 287 | |
2d21ac55 | 288 | #if CONFIG_VOLFS |
0a7de745 | 289 | /* |
2d21ac55 A |
290 | * Check for legacy volfs style pathnames. |
291 | * | |
292 | * For compatibility reasons we currently allow these paths, | |
293 | * but future versions of the OS may not support them. | |
294 | */ | |
295 | if (ndp->ni_pathlen >= VOLFS_MIN_PATH_LEN && | |
296 | cnp->cn_pnbuf[0] == '/' && | |
297 | cnp->cn_pnbuf[1] == '.' && | |
298 | cnp->cn_pnbuf[2] == 'v' && | |
299 | cnp->cn_pnbuf[3] == 'o' && | |
300 | cnp->cn_pnbuf[4] == 'l' && | |
0a7de745 | 301 | cnp->cn_pnbuf[5] == '/') { |
2d21ac55 A |
302 | char * realpath; |
303 | int realpath_err; | |
304 | /* Attempt to resolve a legacy volfs style pathname. */ | |
305 | MALLOC_ZONE(realpath, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK); | |
306 | if (realpath) { | |
0a7de745 | 307 | /* |
b0d623f7 A |
308 | * We only error out on the ENAMETOOLONG cases where we know that |
309 | * vfs_getrealpath translation succeeded but the path could not fit into | |
310 | * MAXPATHLEN characters. In other failure cases, we may be dealing with a path | |
311 | * that legitimately looks like /.vol/1234/567 and is not meant to be translated | |
312 | */ | |
0a7de745 | 313 | if ((realpath_err = vfs_getrealpath(&cnp->cn_pnbuf[6], realpath, MAXPATHLEN, ctx))) { |
2d21ac55 | 314 | FREE_ZONE(realpath, MAXPATHLEN, M_NAMEI); |
0a7de745 | 315 | if (realpath_err == ENOSPC || realpath_err == ENAMETOOLONG) { |
2d21ac55 A |
316 | error = ENAMETOOLONG; |
317 | goto error_out; | |
318 | } | |
319 | } else { | |
320 | if (cnp->cn_flags & HASBUF) { | |
321 | FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI); | |
322 | } | |
323 | cnp->cn_pnbuf = realpath; | |
324 | cnp->cn_pnlen = MAXPATHLEN; | |
325 | ndp->ni_pathlen = strlen(realpath) + 1; | |
326 | cnp->cn_flags |= HASBUF | CN_VOLFSPATH; | |
327 | } | |
328 | } | |
329 | } | |
b0d623f7 | 330 | #endif /* CONFIG_VOLFS */ |
2d21ac55 | 331 | |
b0d623f7 | 332 | #if CONFIG_AUDIT |
55e303ae | 333 | /* If we are auditing the kernel pathname, save the user pathname */ |
0a7de745 A |
334 | if (cnp->cn_flags & AUDITVNPATH1) { |
335 | AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH1); | |
336 | } | |
337 | if (cnp->cn_flags & AUDITVNPATH2) { | |
338 | AUDIT_ARG(upath, ut->uu_cdir, cnp->cn_pnbuf, ARG_UPATH2); | |
339 | } | |
b0d623f7 | 340 | #endif /* CONFIG_AUDIT */ |
55e303ae | 341 | |
1c79356b A |
342 | /* |
343 | * Do not allow empty pathnames | |
344 | */ | |
91447636 | 345 | if (*cnp->cn_pnbuf == '\0') { |
1c79356b | 346 | error = ENOENT; |
2d21ac55 | 347 | goto error_out; |
1c79356b A |
348 | } |
349 | ndp->ni_loopcnt = 0; | |
1c79356b A |
350 | |
351 | /* | |
91447636 | 352 | * determine the starting point for the translation. |
94ff46dc | 353 | * |
bca245ac A |
354 | * We hold the proc_dirs lock across the lookup so that the |
355 | * process rootdir and cwd are stable (i.e. the usecounts | |
356 | * on them are mainatained for the duration of the lookup) | |
1c79356b | 357 | */ |
bca245ac A |
358 | if (take_proc_lock) { |
359 | assert(proc_lock_taken == false); | |
360 | proc_dirs_lock_shared(p); | |
361 | proc_lock_taken = true; | |
362 | } | |
4ba76501 A |
363 | if (!(fdp->fd_flags & FD_CHROOT)) { |
364 | ndp->ni_rootdir = rootvnode; | |
365 | } else { | |
366 | ndp->ni_rootdir = fdp->fd_rdir; | |
367 | } | |
94ff46dc | 368 | |
4ba76501 | 369 | if (!ndp->ni_rootdir) { |
0a7de745 | 370 | if (!(fdp->fd_flags & FD_CHROOT)) { |
4ba76501 | 371 | printf("rootvnode is not set\n"); |
94ff46dc | 372 | } else { |
94ff46dc | 373 | /* This should be a panic */ |
4ba76501 | 374 | printf("fdp->fd_rdir is not set\n"); |
0a7de745 | 375 | } |
4ba76501 A |
376 | error = ENOENT; |
377 | goto error_out; | |
55e303ae | 378 | } |
94ff46dc | 379 | |
91447636 | 380 | cnp->cn_nameptr = cnp->cn_pnbuf; |
55e303ae | 381 | |
91447636 A |
382 | ndp->ni_usedvp = NULLVP; |
383 | ||
384 | if (*(cnp->cn_nameptr) == '/') { | |
0a7de745 A |
385 | while (*(cnp->cn_nameptr) == '/') { |
386 | cnp->cn_nameptr++; | |
91447636 | 387 | ndp->ni_pathlen--; |
1c79356b | 388 | } |
91447636 A |
389 | dp = ndp->ni_rootdir; |
390 | } else if (cnp->cn_flags & USEDVP) { | |
0a7de745 | 391 | dp = ndp->ni_dvp; |
91447636 | 392 | ndp->ni_usedvp = dp; |
0a7de745 | 393 | } else { |
bca245ac | 394 | dp = vfs_context_cwd(ctx); |
0a7de745 | 395 | } |
91447636 | 396 | |
2d21ac55 | 397 | if (dp == NULLVP || (dp->v_lflag & VL_DEAD)) { |
94ff46dc | 398 | dp = NULLVP; |
0a7de745 | 399 | error = ENOENT; |
91447636 A |
400 | goto error_out; |
401 | } | |
94ff46dc | 402 | |
91447636 A |
403 | ndp->ni_dvp = NULLVP; |
404 | ndp->ni_vp = NULLVP; | |
405 | ||
406 | for (;;) { | |
5ba3f43e A |
407 | #if CONFIG_MACF |
408 | /* | |
409 | * Give MACF policies a chance to reject the lookup | |
410 | * before performing any filesystem operations. | |
411 | * This hook is called before resolving the path and | |
412 | * again each time a symlink is encountered. | |
413 | * NB: policies receive path information as supplied | |
414 | * by the caller and thus cannot be trusted. | |
415 | */ | |
416 | error = mac_vnode_check_lookup_preflight(ctx, dp, cnp->cn_nameptr, cnp->cn_namelen); | |
417 | if (error) { | |
418 | goto error_out; | |
419 | } | |
420 | #endif | |
1c79356b | 421 | ndp->ni_startdir = dp; |
94ff46dc | 422 | dp = NULLVP; |
91447636 | 423 | |
0a7de745 | 424 | if ((error = lookup(ndp))) { |
91447636 | 425 | goto error_out; |
1c79356b | 426 | } |
3e170ce0 | 427 | |
1c79356b A |
428 | /* |
429 | * Check for symbolic link | |
430 | */ | |
431 | if ((cnp->cn_flags & ISSYMLINK) == 0) { | |
bca245ac A |
432 | if (proc_lock_taken) { |
433 | proc_dirs_unlock_shared(p); | |
434 | proc_lock_taken = false; | |
94ff46dc | 435 | } |
0a7de745 | 436 | return 0; |
1c79356b | 437 | } |
91447636 | 438 | |
6d2010ae | 439 | continue_symlink: |
bca245ac A |
440 | /* Gives us a new path to process, and a starting dir */ |
441 | error = lookup_handle_symlink(ndp, &dp, ctx); | |
6d2010ae | 442 | if (error != 0) { |
1c79356b A |
443 | break; |
444 | } | |
91447636 A |
445 | } |
446 | /* | |
447 | * only come here if we fail to handle a SYMLINK... | |
448 | * if either ni_dvp or ni_vp is non-NULL, then | |
449 | * we need to drop the iocount that was picked | |
450 | * up in the lookup routine | |
451 | */ | |
6d2010ae | 452 | out_drop: |
0a7de745 A |
453 | if (ndp->ni_dvp) { |
454 | vnode_put(ndp->ni_dvp); | |
455 | } | |
456 | if (ndp->ni_vp) { | |
457 | vnode_put(ndp->ni_vp); | |
458 | } | |
459 | error_out: | |
bca245ac A |
460 | if (proc_lock_taken) { |
461 | proc_dirs_unlock_shared(p); | |
462 | proc_lock_taken = false; | |
463 | } | |
0a7de745 | 464 | if ((cnp->cn_flags & HASBUF)) { |
2d21ac55 | 465 | cnp->cn_flags &= ~HASBUF; |
91447636 A |
466 | FREE_ZONE(cnp->cn_pnbuf, cnp->cn_pnlen, M_NAMEI); |
467 | } | |
55e303ae | 468 | cnp->cn_pnbuf = NULL; |
91447636 | 469 | ndp->ni_vp = NULLVP; |
6d2010ae | 470 | ndp->ni_dvp = NULLVP; |
39236c6e A |
471 | |
472 | #if CONFIG_VOLFS | |
473 | /* | |
0a7de745 | 474 | * Deal with volfs fallout. |
39236c6e A |
475 | * |
476 | * At this point, if we were originally given a volfs path that | |
477 | * looks like /.vol/123/456, then we would have had to convert it into | |
478 | * a full path. Assuming that part worked properly, we will now attempt | |
0a7de745 A |
479 | * to conduct a lookup of the item in the namespace. Under normal |
480 | * circumstances, if a user looked up /tmp/foo and it was not there, it | |
481 | * would be permissible to return ENOENT. | |
39236c6e A |
482 | * |
483 | * However, we may not want to do that here. Specifically, the volfs path | |
484 | * uniquely identifies a certain item in the namespace regardless of where it | |
485 | * lives. If the item has moved in between the time we constructed the | |
486 | * path and now, when we're trying to do a lookup/authorization on the full | |
0a7de745 A |
487 | * path, we may have gotten an ENOENT. |
488 | * | |
489 | * At this point we can no longer tell if the path no longer exists | |
490 | * or if the item in question no longer exists. It could have been renamed | |
491 | * away, in which case the /.vol identifier is still valid. | |
39236c6e | 492 | * |
39236c6e A |
493 | * Do this dance a maximum of MAX_VOLFS_RESTARTS times. |
494 | */ | |
495 | if ((error == ENOENT) && (ndp->ni_cnd.cn_flags & CN_VOLFSPATH)) { | |
496 | if (volfs_restarts < MAX_VOLFS_RESTARTS) { | |
497 | volfs_restarts++; | |
498 | goto vnode_recycled; | |
499 | } | |
500 | } | |
501 | #endif | |
502 | ||
0a7de745 A |
503 | if (error == ERECYCLE) { |
504 | /* vnode was recycled underneath us. re-drive lookup to start at | |
505 | * the beginning again, since recycling invalidated last lookup*/ | |
4a3eedf9 A |
506 | ndp->ni_cnd.cn_flags = cnpflags; |
507 | ndp->ni_dvp = usedvp; | |
508 | goto vnode_recycled; | |
509 | } | |
510 | ||
55e303ae | 511 | |
0a7de745 | 512 | return error; |
1c79356b A |
513 | } |
514 | ||
0a7de745 | 515 | int |
6d2010ae A |
516 | namei_compound_available(vnode_t dp, struct nameidata *ndp) |
517 | { | |
518 | if ((ndp->ni_flag & NAMEI_COMPOUNDOPEN) != 0) { | |
519 | return vnode_compound_open_available(dp); | |
520 | } | |
91447636 | 521 | |
6d2010ae A |
522 | return 0; |
523 | } | |
5ba3f43e | 524 | |
fe8ab488 | 525 | static int |
6d2010ae | 526 | lookup_authorize_search(vnode_t dp, struct componentname *cnp, int dp_authorized_in_cache, vfs_context_t ctx) |
1c79356b | 527 | { |
39236c6e A |
528 | #if !CONFIG_MACF |
529 | #pragma unused(cnp) | |
530 | #endif | |
531 | ||
6d2010ae | 532 | int error; |
1c79356b | 533 | |
6d2010ae A |
534 | if (!dp_authorized_in_cache) { |
535 | error = vnode_authorize(dp, NULL, KAUTH_VNODE_SEARCH, ctx); | |
0a7de745 | 536 | if (error) { |
6d2010ae | 537 | return error; |
0a7de745 | 538 | } |
91447636 | 539 | } |
6d2010ae A |
540 | #if CONFIG_MACF |
541 | error = mac_vnode_check_lookup(ctx, dp, cnp); | |
0a7de745 | 542 | if (error) { |
6d2010ae | 543 | return error; |
0a7de745 | 544 | } |
6d2010ae | 545 | #endif /* CONFIG_MACF */ |
91447636 | 546 | |
6d2010ae A |
547 | return 0; |
548 | } | |
1c79356b | 549 | |
0a7de745 A |
550 | static void |
551 | lookup_consider_update_cache(vnode_t dvp, vnode_t vp, struct componentname *cnp, int nc_generation) | |
6d2010ae A |
552 | { |
553 | int isdot_or_dotdot; | |
554 | isdot_or_dotdot = (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') || (cnp->cn_flags & ISDOTDOT); | |
55e303ae | 555 | |
6d2010ae A |
556 | if (vp->v_name == NULL || vp->v_parent == NULLVP) { |
557 | int update_flags = 0; | |
558 | ||
559 | if (isdot_or_dotdot == 0) { | |
0a7de745 | 560 | if (vp->v_name == NULL) { |
6d2010ae | 561 | update_flags |= VNODE_UPDATE_NAME; |
0a7de745 A |
562 | } |
563 | if (dvp != NULLVP && vp->v_parent == NULLVP) { | |
6d2010ae | 564 | update_flags |= VNODE_UPDATE_PARENT; |
0a7de745 | 565 | } |
6d2010ae | 566 | |
0a7de745 | 567 | if (update_flags) { |
6d2010ae | 568 | vnode_update_identity(vp, dvp, cnp->cn_nameptr, cnp->cn_namelen, cnp->cn_hash, update_flags); |
0a7de745 | 569 | } |
55e303ae | 570 | } |
1c79356b | 571 | } |
0a7de745 | 572 | if ((cnp->cn_flags & MAKEENTRY) && (vp->v_flag & VNCACHEABLE) && LIST_FIRST(&vp->v_nclinks) == NULL) { |
6d2010ae A |
573 | /* |
574 | * missing from name cache, but should | |
575 | * be in it... this can happen if volfs | |
576 | * causes the vnode to be created or the | |
577 | * name cache entry got recycled but the | |
578 | * vnode didn't... | |
579 | * check to make sure that ni_dvp is valid | |
580 | * cache_lookup_path may return a NULL | |
581 | * do a quick check to see if the generation of the | |
582 | * directory matches our snapshot... this will get | |
583 | * rechecked behind the name cache lock, but if it | |
584 | * already fails to match, no need to go any further | |
585 | */ | |
0a7de745 | 586 | if (dvp != NULLVP && (nc_generation == dvp->v_nc_generation) && (!isdot_or_dotdot)) { |
6d2010ae | 587 | cache_enter_with_gen(dvp, vp, cnp, nc_generation); |
0a7de745 | 588 | } |
6d2010ae | 589 | } |
6d2010ae A |
590 | } |
591 | ||
592 | #if NAMEDRSRCFORK | |
593 | /* | |
594 | * Can change ni_dvp and ni_vp. On success, returns with iocounts on stream vnode (always) and | |
0a7de745 | 595 | * data fork if requested. On failure, returns with iocount data fork (always) and its parent directory |
6d2010ae A |
596 | * (if one was provided). |
597 | */ | |
fe8ab488 | 598 | static int |
6d2010ae A |
599 | lookup_handle_rsrc_fork(vnode_t dp, struct nameidata *ndp, struct componentname *cnp, int wantparent, vfs_context_t ctx) |
600 | { | |
601 | vnode_t svp = NULLVP; | |
602 | enum nsoperation nsop; | |
5ba3f43e | 603 | int nsflags; |
6d2010ae A |
604 | int error; |
605 | ||
606 | if (dp->v_type != VREG) { | |
607 | error = ENOENT; | |
608 | goto out; | |
91447636 | 609 | } |
6d2010ae | 610 | switch (cnp->cn_nameiop) { |
0a7de745 A |
611 | case DELETE: |
612 | if (cnp->cn_flags & CN_ALLOWRSRCFORK) { | |
613 | nsop = NS_DELETE; | |
614 | } else { | |
615 | error = EPERM; | |
616 | goto out; | |
617 | } | |
618 | break; | |
619 | case CREATE: | |
620 | if (cnp->cn_flags & CN_ALLOWRSRCFORK) { | |
621 | nsop = NS_CREATE; | |
622 | } else { | |
623 | error = EPERM; | |
624 | goto out; | |
625 | } | |
626 | break; | |
627 | case LOOKUP: | |
628 | /* Make sure our lookup of "/..namedfork/rsrc" is allowed. */ | |
629 | if (cnp->cn_flags & CN_ALLOWRSRCFORK) { | |
630 | nsop = NS_OPEN; | |
631 | } else { | |
6d2010ae A |
632 | error = EPERM; |
633 | goto out; | |
0a7de745 A |
634 | } |
635 | break; | |
636 | default: | |
637 | error = EPERM; | |
638 | goto out; | |
6d2010ae | 639 | } |
5ba3f43e A |
640 | |
641 | nsflags = 0; | |
0a7de745 | 642 | if (cnp->cn_flags & CN_RAW_ENCRYPTED) { |
5ba3f43e | 643 | nsflags |= NS_GETRAWENCRYPTED; |
0a7de745 | 644 | } |
5ba3f43e | 645 | |
6d2010ae | 646 | /* Ask the file system for the resource fork. */ |
5ba3f43e | 647 | error = vnode_getnamedstream(dp, &svp, XATTR_RESOURCEFORK_NAME, nsop, nsflags, ctx); |
91447636 | 648 | |
6d2010ae A |
649 | /* During a create, it OK for stream vnode to be missing. */ |
650 | if (error == ENOATTR || error == ENOENT) { | |
651 | error = (nsop == NS_CREATE) ? 0 : ENOENT; | |
0a7de745 | 652 | } |
6d2010ae A |
653 | if (error) { |
654 | goto out; | |
655 | } | |
656 | /* The "parent" of the stream is the file. */ | |
657 | if (wantparent) { | |
658 | if (ndp->ni_dvp) { | |
6d2010ae A |
659 | vnode_put(ndp->ni_dvp); |
660 | } | |
661 | ndp->ni_dvp = dp; | |
662 | } else { | |
663 | vnode_put(dp); | |
664 | } | |
665 | ndp->ni_vp = svp; /* on create this may be null */ | |
91447636 | 666 | |
6d2010ae A |
667 | /* Restore the truncated pathname buffer (for audits). */ |
668 | if (ndp->ni_pathlen == 1 && ndp->ni_next[0] == '\0') { | |
cb323159 A |
669 | /* |
670 | * While we replaced only '/' with '\0' and would ordinarily | |
671 | * need to just switch that back, the buffer in which we did | |
672 | * this may not be what the pathname buffer is now when symlinks | |
673 | * are involved. If we just restore the "/" we will make the | |
674 | * string not terminated anymore, so be safe and restore the | |
675 | * entire suffix. | |
676 | */ | |
677 | strncpy(ndp->ni_next, _PATH_RSRCFORKSPEC, sizeof(_PATH_RSRCFORKSPEC)); | |
678 | cnp->cn_nameptr = ndp->ni_next + 1; | |
679 | cnp->cn_namelen = sizeof(_PATH_RSRCFORKSPEC) - 1; | |
680 | ndp->ni_next += cnp->cn_namelen; | |
681 | if (ndp->ni_next[0] != '\0') { | |
682 | panic("Incorrect termination of path in %s", __FUNCTION__); | |
683 | } | |
6d2010ae A |
684 | } |
685 | cnp->cn_flags &= ~MAKEENTRY; | |
2d21ac55 | 686 | |
6d2010ae A |
687 | return 0; |
688 | out: | |
689 | return error; | |
690 | } | |
691 | #endif /* NAMEDRSRCFORK */ | |
692 | ||
693 | /* | |
694 | * iocounts in: | |
0a7de745 A |
695 | * --One on ni_vp. One on ni_dvp if there is more path, or we didn't come through the |
696 | * cache, or we came through the cache and the caller doesn't want the parent. | |
6d2010ae A |
697 | * |
698 | * iocounts out: | |
699 | * --Leaves us in the correct state for the next step, whatever that might be. | |
700 | * --If we find a symlink, returns with iocounts on both ni_vp and ni_dvp. | |
701 | * --If we are to look up another component, then we have an iocount on ni_vp and | |
0a7de745 | 702 | * nothing else. |
6d2010ae A |
703 | * --If we are done, returns an iocount on ni_vp, and possibly on ni_dvp depending on nameidata flags. |
704 | * --In the event of an error, may return with ni_dvp NULL'ed out (in which case, iocount | |
705 | * was dropped). | |
706 | */ | |
0a7de745 A |
707 | static int |
708 | lookup_handle_found_vnode(struct nameidata *ndp, struct componentname *cnp, int rdonly, | |
709 | int vbusyflags, int *keep_going, int nc_generation, | |
710 | int wantparent, int atroot, vfs_context_t ctx) | |
6d2010ae A |
711 | { |
712 | vnode_t dp; | |
713 | int error; | |
714 | char *cp; | |
715 | ||
716 | dp = ndp->ni_vp; | |
717 | *keep_going = 0; | |
718 | ||
719 | if (ndp->ni_vp == NULLVP) { | |
720 | panic("NULL ni_vp in %s\n", __FUNCTION__); | |
721 | } | |
722 | ||
723 | if (atroot) { | |
724 | goto nextname; | |
725 | } | |
726 | ||
6d2010ae A |
727 | /* |
728 | * Take into account any additional components consumed by | |
729 | * the underlying filesystem. | |
730 | */ | |
731 | if (cnp->cn_consume > 0) { | |
732 | cnp->cn_nameptr += cnp->cn_consume; | |
733 | ndp->ni_next += cnp->cn_consume; | |
734 | ndp->ni_pathlen -= cnp->cn_consume; | |
735 | cnp->cn_consume = 0; | |
736 | } else { | |
737 | lookup_consider_update_cache(ndp->ni_dvp, dp, cnp, nc_generation); | |
738 | } | |
739 | ||
740 | /* | |
741 | * Check to see if the vnode has been mounted on... | |
742 | * if so find the root of the mounted file system. | |
743 | * Updates ndp->ni_vp. | |
744 | */ | |
745 | error = lookup_traverse_mountpoints(ndp, cnp, dp, vbusyflags, ctx); | |
746 | dp = ndp->ni_vp; | |
747 | if (error) { | |
748 | goto out; | |
749 | } | |
750 | ||
751 | #if CONFIG_MACF | |
752 | if (vfs_flags(vnode_mount(dp)) & MNT_MULTILABEL) { | |
753 | error = vnode_label(vnode_mount(dp), NULL, dp, NULL, 0, ctx); | |
0a7de745 | 754 | if (error) { |
6d2010ae | 755 | goto out; |
0a7de745 | 756 | } |
6d2010ae A |
757 | } |
758 | #endif | |
759 | ||
760 | /* | |
761 | * Check for symbolic link | |
762 | */ | |
763 | if ((dp->v_type == VLNK) && | |
764 | ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) { | |
765 | cnp->cn_flags |= ISSYMLINK; | |
766 | *keep_going = 1; | |
0a7de745 | 767 | return 0; |
6d2010ae A |
768 | } |
769 | ||
770 | /* | |
771 | * Check for bogus trailing slashes. | |
772 | */ | |
773 | if ((ndp->ni_flag & NAMEI_TRAILINGSLASH)) { | |
774 | if (dp->v_type != VDIR) { | |
775 | error = ENOTDIR; | |
776 | goto out; | |
777 | } | |
778 | ndp->ni_flag &= ~(NAMEI_TRAILINGSLASH); | |
0a7de745 | 779 | } |
4b17d6b6 A |
780 | |
781 | #if NAMEDSTREAMS | |
0a7de745 | 782 | /* |
4b17d6b6 A |
783 | * Deny namei/lookup requests to resolve paths that point to shadow files. |
784 | * Access to shadow files must be conducted by explicit calls to VNOP_LOOKUP | |
785 | * directly, and not use lookup/namei | |
786 | */ | |
0a7de745 | 787 | if (vnode_isshadow(dp)) { |
4b17d6b6 A |
788 | error = ENOENT; |
789 | goto out; | |
790 | } | |
791 | #endif | |
792 | ||
6d2010ae A |
793 | nextname: |
794 | /* | |
795 | * Not a symbolic link. If more pathname, | |
796 | * continue at next component, else return. | |
797 | * | |
0a7de745 | 798 | * Definitely have a dvp if there's another slash |
6d2010ae A |
799 | */ |
800 | if (*ndp->ni_next == '/') { | |
801 | cnp->cn_nameptr = ndp->ni_next + 1; | |
802 | ndp->ni_pathlen--; | |
803 | while (*cnp->cn_nameptr == '/') { | |
804 | cnp->cn_nameptr++; | |
805 | ndp->ni_pathlen--; | |
806 | } | |
807 | ||
808 | cp = cnp->cn_nameptr; | |
809 | vnode_put(ndp->ni_dvp); | |
810 | ndp->ni_dvp = NULLVP; | |
811 | ||
812 | if (*cp == '\0') { | |
813 | goto emptyname; | |
814 | } | |
815 | ||
816 | *keep_going = 1; | |
817 | return 0; | |
818 | } | |
0a7de745 | 819 | |
6d2010ae A |
820 | /* |
821 | * Disallow directory write attempts on read-only file systems. | |
822 | */ | |
823 | if (rdonly && | |
824 | (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { | |
825 | error = EROFS; | |
826 | goto out; | |
827 | } | |
0a7de745 | 828 | |
6d2010ae A |
829 | /* If SAVESTART is set, we should have a dvp */ |
830 | if (cnp->cn_flags & SAVESTART) { | |
0a7de745 | 831 | /* |
6d2010ae A |
832 | * note that we already hold a reference |
833 | * on both dp and ni_dvp, but for some reason | |
834 | * can't get another one... in this case we | |
835 | * need to do vnode_put on dp in 'bad2' | |
836 | */ | |
0a7de745 A |
837 | if ((vnode_get(ndp->ni_dvp))) { |
838 | error = ENOENT; | |
6d2010ae A |
839 | goto out; |
840 | } | |
841 | ndp->ni_startdir = ndp->ni_dvp; | |
842 | } | |
843 | if (!wantparent && ndp->ni_dvp) { | |
844 | vnode_put(ndp->ni_dvp); | |
845 | ndp->ni_dvp = NULLVP; | |
846 | } | |
847 | ||
0a7de745 | 848 | if (cnp->cn_flags & AUDITVNPATH1) { |
6d2010ae | 849 | AUDIT_ARG(vnpath, dp, ARG_VNODE1); |
0a7de745 | 850 | } else if (cnp->cn_flags & AUDITVNPATH2) { |
6d2010ae | 851 | AUDIT_ARG(vnpath, dp, ARG_VNODE2); |
0a7de745 | 852 | } |
6d2010ae A |
853 | |
854 | #if NAMEDRSRCFORK | |
855 | /* | |
856 | * Caller wants the resource fork. | |
857 | */ | |
858 | if ((cnp->cn_flags & CN_WANTSRSRCFORK) && (dp != NULLVP)) { | |
859 | error = lookup_handle_rsrc_fork(dp, ndp, cnp, wantparent, ctx); | |
0a7de745 | 860 | if (error != 0) { |
6d2010ae | 861 | goto out; |
0a7de745 | 862 | } |
6d2010ae A |
863 | |
864 | dp = ndp->ni_vp; | |
865 | } | |
866 | #endif | |
0a7de745 A |
867 | if (kdebug_enable) { |
868 | kdebug_lookup(ndp->ni_vp, cnp); | |
869 | } | |
6d2010ae A |
870 | |
871 | return 0; | |
872 | ||
873 | emptyname: | |
874 | error = lookup_handle_emptyname(ndp, cnp, wantparent); | |
0a7de745 | 875 | if (error != 0) { |
6d2010ae | 876 | goto out; |
0a7de745 | 877 | } |
6d2010ae A |
878 | |
879 | return 0; | |
880 | out: | |
881 | return error; | |
6d2010ae A |
882 | } |
883 | ||
884 | /* | |
885 | * Comes in iocount on ni_vp. May overwrite ni_dvp, but doesn't interpret incoming value. | |
886 | */ | |
0a7de745 | 887 | static int |
6d2010ae A |
888 | lookup_handle_emptyname(struct nameidata *ndp, struct componentname *cnp, int wantparent) |
889 | { | |
890 | vnode_t dp; | |
891 | int error = 0; | |
892 | ||
893 | dp = ndp->ni_vp; | |
894 | cnp->cn_namelen = 0; | |
895 | /* | |
896 | * A degenerate name (e.g. / or "") which is a way of | |
897 | * talking about a directory, e.g. like "/." or ".". | |
898 | */ | |
899 | if (dp->v_type != VDIR) { | |
900 | error = ENOTDIR; | |
901 | goto out; | |
902 | } | |
903 | if (cnp->cn_nameiop != LOOKUP) { | |
904 | error = EISDIR; | |
905 | goto out; | |
906 | } | |
907 | if (wantparent) { | |
0a7de745 | 908 | /* |
6d2010ae A |
909 | * note that we already hold a reference |
910 | * on dp, but for some reason can't | |
911 | * get another one... in this case we | |
912 | * need to do vnode_put on dp in 'bad' | |
913 | */ | |
0a7de745 A |
914 | if ((vnode_get(dp))) { |
915 | error = ENOENT; | |
6d2010ae A |
916 | goto out; |
917 | } | |
918 | ndp->ni_dvp = dp; | |
919 | } | |
920 | cnp->cn_flags &= ~ISDOTDOT; | |
921 | cnp->cn_flags |= ISLASTCN; | |
922 | ndp->ni_next = cnp->cn_nameptr; | |
923 | ndp->ni_vp = dp; | |
924 | ||
0a7de745 | 925 | if (cnp->cn_flags & AUDITVNPATH1) { |
6d2010ae | 926 | AUDIT_ARG(vnpath, dp, ARG_VNODE1); |
0a7de745 | 927 | } else if (cnp->cn_flags & AUDITVNPATH2) { |
6d2010ae | 928 | AUDIT_ARG(vnpath, dp, ARG_VNODE2); |
0a7de745 A |
929 | } |
930 | if (cnp->cn_flags & SAVESTART) { | |
6d2010ae | 931 | panic("lookup: SAVESTART"); |
0a7de745 | 932 | } |
6d2010ae A |
933 | |
934 | return 0; | |
935 | out: | |
936 | return error; | |
937 | } | |
938 | /* | |
939 | * Search a pathname. | |
940 | * This is a very central and rather complicated routine. | |
941 | * | |
942 | * The pathname is pointed to by ni_ptr and is of length ni_pathlen. | |
943 | * The starting directory is taken from ni_startdir. The pathname is | |
944 | * descended until done, or a symbolic link is encountered. The variable | |
945 | * ni_more is clear if the path is completed; it is set to one if a | |
946 | * symbolic link needing interpretation is encountered. | |
947 | * | |
948 | * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on | |
949 | * whether the name is to be looked up, created, renamed, or deleted. | |
950 | * When CREATE, RENAME, or DELETE is specified, information usable in | |
951 | * creating, renaming, or deleting a directory entry may be calculated. | |
952 | * If flag has LOCKPARENT or'ed into it, the parent directory is returned | |
953 | * locked. If flag has WANTPARENT or'ed into it, the parent directory is | |
954 | * returned unlocked. Otherwise the parent directory is not returned. If | |
955 | * the target of the pathname exists and LOCKLEAF is or'ed into the flag | |
956 | * the target is returned locked, otherwise it is returned unlocked. | |
957 | * When creating or renaming and LOCKPARENT is specified, the target may not | |
958 | * be ".". When deleting and LOCKPARENT is specified, the target may be ".". | |
0a7de745 | 959 | * |
6d2010ae A |
960 | * Overall outline of lookup: |
961 | * | |
962 | * dirloop: | |
963 | * identify next component of name at ndp->ni_ptr | |
964 | * handle degenerate case where name is null string | |
965 | * if .. and crossing mount points and on mounted filesys, find parent | |
966 | * call VNOP_LOOKUP routine for next component name | |
967 | * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set | |
968 | * component vnode returned in ni_vp (if it exists), locked. | |
969 | * if result vnode is mounted on and crossing mount points, | |
970 | * find mounted on vnode | |
971 | * if more components of name, do next level at dirloop | |
972 | * return the answer in ni_vp, locked if LOCKLEAF set | |
973 | * if LOCKPARENT set, return locked parent in ni_dvp | |
974 | * if WANTPARENT set, return unlocked parent in ni_dvp | |
975 | * | |
976 | * Returns: 0 Success | |
977 | * ENOENT No such file or directory | |
978 | * EBADF Bad file descriptor | |
979 | * ENOTDIR Not a directory | |
980 | * EROFS Read-only file system [CREATE] | |
981 | * EISDIR Is a directory [CREATE] | |
982 | * cache_lookup_path:ERECYCLE (vnode was recycled from underneath us, redrive lookup again) | |
983 | * vnode_authorize:EROFS | |
984 | * vnode_authorize:EACCES | |
985 | * vnode_authorize:EPERM | |
986 | * vnode_authorize:??? | |
987 | * VNOP_LOOKUP:ENOENT No such file or directory | |
988 | * VNOP_LOOKUP:EJUSTRETURN Restart system call (INTERNAL) | |
989 | * VNOP_LOOKUP:??? | |
990 | * VFS_ROOT:ENOTSUP | |
991 | * VFS_ROOT:ENOENT | |
992 | * VFS_ROOT:??? | |
993 | */ | |
994 | int | |
995 | lookup(struct nameidata *ndp) | |
996 | { | |
0a7de745 A |
997 | char *cp; /* pointer into pathname argument */ |
998 | vnode_t tdp; /* saved dp */ | |
999 | vnode_t dp; /* the directory we are searching */ | |
1000 | int docache = 1; /* == 0 do not cache last component */ | |
1001 | int wantparent; /* 1 => wantparent or lockparent flag */ | |
1002 | int rdonly; /* lookup read-only flag bit */ | |
6d2010ae A |
1003 | int dp_authorized = 0; |
1004 | int error = 0; | |
1005 | struct componentname *cnp = &ndp->ni_cnd; | |
1006 | vfs_context_t ctx = cnp->cn_context; | |
1007 | int vbusyflags = 0; | |
1008 | int nc_generation = 0; | |
1009 | vnode_t last_dp = NULLVP; | |
1010 | int keep_going; | |
1011 | int atroot; | |
1012 | ||
1013 | /* | |
1014 | * Setup: break out flag bits into variables. | |
1015 | */ | |
fe8ab488 | 1016 | if (cnp->cn_flags & NOCACHE) { |
0a7de745 | 1017 | docache = 0; |
6d2010ae A |
1018 | } |
1019 | wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); | |
1020 | rdonly = cnp->cn_flags & RDONLY; | |
1021 | cnp->cn_flags &= ~ISSYMLINK; | |
1022 | cnp->cn_consume = 0; | |
1023 | ||
1024 | dp = ndp->ni_startdir; | |
1025 | ndp->ni_startdir = NULLVP; | |
1026 | ||
0a7de745 A |
1027 | if ((cnp->cn_flags & CN_NBMOUNTLOOK) != 0) { |
1028 | vbusyflags = LK_NOWAIT; | |
1029 | } | |
6d2010ae A |
1030 | cp = cnp->cn_nameptr; |
1031 | ||
1032 | if (*cp == '\0') { | |
0a7de745 | 1033 | if ((vnode_getwithref(dp))) { |
6d2010ae | 1034 | dp = NULLVP; |
0a7de745 | 1035 | error = ENOENT; |
6d2010ae A |
1036 | goto bad; |
1037 | } | |
1038 | ndp->ni_vp = dp; | |
1039 | error = lookup_handle_emptyname(ndp, cnp, wantparent); | |
1040 | if (error) { | |
1041 | goto bad; | |
1042 | } | |
1043 | ||
1044 | return 0; | |
1045 | } | |
0a7de745 | 1046 | dirloop: |
6d2010ae A |
1047 | atroot = 0; |
1048 | ndp->ni_vp = NULLVP; | |
1049 | ||
0a7de745 | 1050 | if ((error = cache_lookup_path(ndp, cnp, dp, ctx, &dp_authorized, last_dp))) { |
6d2010ae A |
1051 | dp = NULLVP; |
1052 | goto bad; | |
1053 | } | |
1054 | if ((cnp->cn_flags & ISLASTCN)) { | |
0a7de745 A |
1055 | if (docache) { |
1056 | cnp->cn_flags |= MAKEENTRY; | |
1057 | } | |
1058 | } else { | |
1059 | cnp->cn_flags |= MAKEENTRY; | |
1060 | } | |
6d2010ae A |
1061 | |
1062 | dp = ndp->ni_dvp; | |
1063 | ||
1064 | if (ndp->ni_vp != NULLVP) { | |
0a7de745 | 1065 | /* |
6d2010ae | 1066 | * cache_lookup_path returned a non-NULL ni_vp then, |
0a7de745 | 1067 | * we're guaranteed that the dp is a VDIR, it's |
6d2010ae A |
1068 | * been authorized, and vp is not ".." |
1069 | * | |
1070 | * make sure we don't try to enter the name back into | |
1071 | * the cache if this vp is purged before we get to that | |
1072 | * check since we won't have serialized behind whatever | |
1073 | * activity is occurring in the FS that caused the purge | |
1074 | */ | |
0a7de745 A |
1075 | if (dp != NULLVP) { |
1076 | nc_generation = dp->v_nc_generation - 1; | |
1077 | } | |
6d2010ae | 1078 | |
0a7de745 | 1079 | goto returned_from_lookup_path; |
9bccf70c | 1080 | } |
1c79356b | 1081 | |
1c79356b A |
1082 | /* |
1083 | * Handle "..": two special cases. | |
1084 | * 1. If at root directory (e.g. after chroot) | |
1085 | * or at absolute root directory | |
1086 | * then ignore it so can't get out. | |
1087 | * 2. If this vnode is the root of a mounted | |
1088 | * filesystem, then replace it with the | |
1089 | * vnode which was mounted on so we take the | |
1090 | * .. in the other file system. | |
1091 | */ | |
0a7de745 | 1092 | if ((cnp->cn_flags & ISDOTDOT)) { |
39037602 A |
1093 | /* |
1094 | * if this is a chroot'ed process, check if the current | |
1095 | * directory is still a subdirectory of the process's | |
1096 | * root directory. | |
1097 | */ | |
1098 | if (ndp->ni_rootdir && (ndp->ni_rootdir != rootvnode) && | |
0a7de745 | 1099 | dp != ndp->ni_rootdir) { |
39037602 A |
1100 | int sdir_error; |
1101 | int is_subdir = FALSE; | |
1102 | ||
1103 | sdir_error = vnode_issubdir(dp, ndp->ni_rootdir, | |
1104 | &is_subdir, vfs_context_kernel()); | |
1105 | ||
1106 | /* | |
1107 | * If we couldn't determine if dp is a subdirectory of | |
1108 | * ndp->ni_rootdir (sdir_error != 0), we let the request | |
1109 | * proceed. | |
1110 | */ | |
1111 | if (!sdir_error && !is_subdir) { | |
1112 | vnode_put(dp); | |
1113 | dp = ndp->ni_rootdir; | |
1114 | /* | |
1115 | * There's a ref on the process's root directory | |
1116 | * but we can't use vnode_getwithref here as | |
1117 | * there is nothing preventing that ref being | |
1118 | * released by another thread. | |
1119 | */ | |
1120 | if (vnode_get(dp)) { | |
1121 | error = ENOENT; | |
1122 | goto bad; | |
1123 | } | |
1124 | } | |
1125 | } | |
1126 | ||
1c79356b | 1127 | for (;;) { |
0a7de745 A |
1128 | if (dp == ndp->ni_rootdir || dp == rootvnode) { |
1129 | ndp->ni_dvp = dp; | |
1c79356b | 1130 | ndp->ni_vp = dp; |
91447636 A |
1131 | /* |
1132 | * we're pinned at the root | |
1133 | * we've already got one reference on 'dp' | |
1134 | * courtesy of cache_lookup_path... take | |
1135 | * another one for the ".." | |
1136 | * if we fail to get the new reference, we'll | |
1137 | * drop our original down in 'bad' | |
1138 | */ | |
0a7de745 | 1139 | if ((vnode_get(dp))) { |
91447636 A |
1140 | error = ENOENT; |
1141 | goto bad; | |
1142 | } | |
6d2010ae A |
1143 | atroot = 1; |
1144 | goto returned_from_lookup_path; | |
1c79356b A |
1145 | } |
1146 | if ((dp->v_flag & VROOT) == 0 || | |
0a7de745 A |
1147 | (cnp->cn_flags & NOCROSSMOUNT)) { |
1148 | break; | |
1149 | } | |
1150 | if (dp->v_mount == NULL) { /* forced umount */ | |
1151 | error = EBADF; | |
0b4e3aa0 A |
1152 | goto bad; |
1153 | } | |
1c79356b | 1154 | tdp = dp; |
91447636 A |
1155 | dp = tdp->v_mount->mnt_vnodecovered; |
1156 | ||
0a7de745 | 1157 | if ((vnode_getwithref(dp))) { |
ea3f0419 | 1158 | vnode_put(tdp); |
0a7de745 | 1159 | dp = NULLVP; |
91447636 A |
1160 | error = ENOENT; |
1161 | goto bad; | |
1162 | } | |
ea3f0419 A |
1163 | |
1164 | vnode_put(tdp); | |
1165 | ||
91447636 A |
1166 | ndp->ni_dvp = dp; |
1167 | dp_authorized = 0; | |
1c79356b A |
1168 | } |
1169 | } | |
1170 | ||
1171 | /* | |
1172 | * We now have a segment name to search for, and a directory to search. | |
1173 | */ | |
1174 | unionlookup: | |
91447636 A |
1175 | ndp->ni_vp = NULLVP; |
1176 | ||
1177 | if (dp->v_type != VDIR) { | |
0a7de745 A |
1178 | error = ENOTDIR; |
1179 | goto lookup_error; | |
91447636 | 1180 | } |
0a7de745 | 1181 | if ((cnp->cn_flags & DONOTAUTH) != DONOTAUTH) { |
6d2010ae A |
1182 | error = lookup_authorize_search(dp, cnp, dp_authorized, ctx); |
1183 | if (error) { | |
2d21ac55 | 1184 | goto lookup_error; |
6d2010ae A |
1185 | } |
1186 | } | |
1187 | ||
1188 | /* | |
1189 | * Now that we've authorized a lookup, can bail out if the filesystem | |
1190 | * will be doing a batched operation. Return an iocount on dvp. | |
1191 | */ | |
1192 | #if NAMEDRSRCFORK | |
0a7de745 A |
1193 | if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp) && !(cnp->cn_flags & CN_WANTSRSRCFORK)) { |
1194 | #else | |
6d2010ae A |
1195 | if ((cnp->cn_flags & ISLASTCN) && namei_compound_available(dp, ndp)) { |
1196 | #endif /* NAMEDRSRCFORK */ | |
1197 | ndp->ni_flag |= NAMEI_UNFINISHED; | |
1198 | ndp->ni_ncgeneration = dp->v_nc_generation; | |
1199 | return 0; | |
91447636 | 1200 | } |
2d21ac55 | 1201 | |
0a7de745 | 1202 | nc_generation = dp->v_nc_generation; |
2d21ac55 | 1203 | |
39236c6e | 1204 | /* |
0a7de745 | 1205 | * Note: |
39236c6e A |
1206 | * Filesystems that support hardlinks may want to call vnode_update_identity |
1207 | * if the lookup operation below will modify the in-core vnode to belong to a new point | |
1208 | * in the namespace. VFS cannot infer whether or not the look up operation makes the vnode | |
1209 | * name change or change parents. Without this, the lookup may make update | |
1210 | * filesystem-specific in-core metadata but fail to update the v_parent or v_name | |
1211 | * fields in the vnode. If VFS were to do this, it would be necessary to call | |
1212 | * vnode_update_identity on every lookup operation -- expensive! | |
1213 | * | |
1214 | * However, even with this in place, multiple lookups may occur in between this lookup | |
1215 | * and the subsequent vnop, so, at best, we could only guarantee that you would get a | |
0a7de745 | 1216 | * valid path back, and not necessarily the one that you wanted. |
39236c6e | 1217 | * |
0a7de745 | 1218 | * Example: |
39236c6e | 1219 | * /tmp/a == /foo/b |
0a7de745 A |
1220 | * |
1221 | * If you are now looking up /foo/b and the vnode for this link represents /tmp/a, | |
1222 | * vnode_update_identity will fix the parentage so that you can get /foo/b back | |
1223 | * through the v_parent chain (preventing you from getting /tmp/b back). It would | |
39236c6e A |
1224 | * not fix whether or not you should or should not get /tmp/a vs. /foo/b. |
1225 | */ | |
6d2010ae | 1226 | |
39236c6e | 1227 | error = VNOP_LOOKUP(dp, &ndp->ni_vp, cnp, ctx); |
6d2010ae | 1228 | |
0a7de745 | 1229 | if (error) { |
91447636 | 1230 | lookup_error: |
1c79356b | 1231 | if ((error == ENOENT) && |
39236c6e | 1232 | (dp->v_mount != NULL) && |
1c79356b A |
1233 | (dp->v_mount->mnt_flag & MNT_UNION)) { |
1234 | tdp = dp; | |
39236c6e | 1235 | error = lookup_traverse_union(tdp, &dp, ctx); |
91447636 | 1236 | vnode_put(tdp); |
39236c6e | 1237 | if (error) { |
0a7de745 | 1238 | dp = NULLVP; |
91447636 A |
1239 | goto bad; |
1240 | } | |
39236c6e | 1241 | |
91447636 A |
1242 | ndp->ni_dvp = dp; |
1243 | dp_authorized = 0; | |
1c79356b A |
1244 | goto unionlookup; |
1245 | } | |
1246 | ||
0a7de745 | 1247 | if (error != EJUSTRETURN) { |
1c79356b | 1248 | goto bad; |
0a7de745 | 1249 | } |
91447636 | 1250 | |
0a7de745 | 1251 | if (ndp->ni_vp != NULLVP) { |
91447636 | 1252 | panic("leaf should be empty"); |
0a7de745 | 1253 | } |
91447636 | 1254 | |
39236c6e | 1255 | #if NAMEDRSRCFORK |
0a7de745 | 1256 | /* |
39236c6e | 1257 | * At this point, error should be EJUSTRETURN. |
0a7de745 A |
1258 | * |
1259 | * If CN_WANTSRSRCFORK is set, that implies that the | |
39236c6e | 1260 | * underlying filesystem could not find the "parent" of the |
0a7de745 | 1261 | * resource fork (the data fork), and we are doing a lookup |
39236c6e A |
1262 | * for a CREATE event. |
1263 | * | |
1264 | * However, this should be converted to an error, as the | |
1265 | * failure to find this parent should disallow further | |
0a7de745 | 1266 | * progress to try and acquire a resource fork vnode. |
39236c6e A |
1267 | */ |
1268 | if (cnp->cn_flags & CN_WANTSRSRCFORK) { | |
1269 | error = ENOENT; | |
1270 | goto bad; | |
1271 | } | |
1272 | #endif | |
1273 | ||
6d2010ae | 1274 | error = lookup_validate_creation_path(ndp); |
0a7de745 | 1275 | if (error) { |
9bccf70c | 1276 | goto bad; |
0a7de745 | 1277 | } |
1c79356b A |
1278 | /* |
1279 | * We return with ni_vp NULL to indicate that the entry | |
1280 | * doesn't currently exist, leaving a pointer to the | |
91447636 | 1281 | * referenced directory vnode in ndp->ni_dvp. |
1c79356b A |
1282 | */ |
1283 | if (cnp->cn_flags & SAVESTART) { | |
0a7de745 | 1284 | if ((vnode_get(ndp->ni_dvp))) { |
91447636 A |
1285 | error = ENOENT; |
1286 | goto bad; | |
1287 | } | |
1c79356b | 1288 | ndp->ni_startdir = ndp->ni_dvp; |
1c79356b | 1289 | } |
0a7de745 A |
1290 | if (!wantparent) { |
1291 | vnode_put(ndp->ni_dvp); | |
1292 | } | |
91447636 | 1293 | |
0a7de745 A |
1294 | if (kdebug_enable) { |
1295 | kdebug_lookup(ndp->ni_dvp, cnp); | |
1296 | } | |
1297 | return 0; | |
1c79356b | 1298 | } |
91447636 | 1299 | returned_from_lookup_path: |
6d2010ae A |
1300 | /* We'll always have an iocount on ni_vp when this finishes. */ |
1301 | error = lookup_handle_found_vnode(ndp, cnp, rdonly, vbusyflags, &keep_going, nc_generation, wantparent, atroot, ctx); | |
1302 | if (error != 0) { | |
0a7de745 | 1303 | goto bad2; |
1c79356b A |
1304 | } |
1305 | ||
6d2010ae A |
1306 | if (keep_going) { |
1307 | dp = ndp->ni_vp; | |
2d21ac55 | 1308 | |
6d2010ae A |
1309 | /* namei() will handle symlinks */ |
1310 | if ((dp->v_type == VLNK) && | |
0a7de745 A |
1311 | ((cnp->cn_flags & FOLLOW) || (ndp->ni_flag & NAMEI_TRAILINGSLASH) || *ndp->ni_next == '/')) { |
1312 | return 0; | |
2d21ac55 | 1313 | } |
1c79356b | 1314 | |
6d2010ae | 1315 | /* |
0a7de745 | 1316 | * Otherwise, there's more path to process. |
6d2010ae A |
1317 | * cache_lookup_path is now responsible for dropping io ref on dp |
1318 | * when it is called again in the dirloop. This ensures we hold | |
1319 | * a ref on dp until we complete the next round of lookup. | |
91447636 | 1320 | */ |
6d2010ae A |
1321 | last_dp = dp; |
1322 | ||
1323 | goto dirloop; | |
55e303ae | 1324 | } |
91447636 | 1325 | |
0a7de745 | 1326 | return 0; |
1c79356b | 1327 | bad2: |
0a7de745 | 1328 | if (ndp->ni_dvp) { |
6d2010ae | 1329 | vnode_put(ndp->ni_dvp); |
0a7de745 | 1330 | } |
6d2010ae A |
1331 | |
1332 | vnode_put(ndp->ni_vp); | |
91447636 A |
1333 | ndp->ni_vp = NULLVP; |
1334 | ||
0a7de745 A |
1335 | if (kdebug_enable) { |
1336 | kdebug_lookup(dp, cnp); | |
1337 | } | |
1338 | return error; | |
91447636 | 1339 | |
1c79356b | 1340 | bad: |
0a7de745 A |
1341 | if (dp) { |
1342 | vnode_put(dp); | |
1343 | } | |
91447636 A |
1344 | ndp->ni_vp = NULLVP; |
1345 | ||
0a7de745 A |
1346 | if (kdebug_enable) { |
1347 | kdebug_lookup(dp, cnp); | |
1348 | } | |
1349 | return error; | |
1c79356b A |
1350 | } |
1351 | ||
39236c6e A |
1352 | /* |
1353 | * Given a vnode in a union mount, traverse to the equivalent | |
1354 | * vnode in the underlying mount. | |
1355 | */ | |
1356 | int | |
1357 | lookup_traverse_union(vnode_t dvp, vnode_t *new_dvp, vfs_context_t ctx) | |
1358 | { | |
1359 | char *path = NULL, *pp; | |
1360 | const char *name, *np; | |
1361 | int len; | |
1362 | int error = 0; | |
1363 | struct nameidata nd; | |
1364 | vnode_t vp = dvp; | |
1365 | ||
1366 | *new_dvp = NULL; | |
1367 | ||
1368 | if (vp && vp->v_flag & VROOT) { | |
1369 | *new_dvp = vp->v_mount->mnt_vnodecovered; | |
0a7de745 | 1370 | if (vnode_getwithref(*new_dvp)) { |
39236c6e | 1371 | return ENOENT; |
0a7de745 | 1372 | } |
39236c6e A |
1373 | return 0; |
1374 | } | |
1375 | ||
1376 | path = (char *) kalloc(MAXPATHLEN); | |
1377 | if (path == NULL) { | |
1378 | error = ENOMEM; | |
1379 | goto done; | |
1380 | } | |
1381 | ||
1382 | /* | |
1383 | * Walk back up to the mountpoint following the | |
1384 | * v_parent chain and build a slash-separated path. | |
1385 | * Then lookup that path starting with the covered vnode. | |
1386 | */ | |
1387 | pp = path + (MAXPATHLEN - 1); | |
1388 | *pp = '\0'; | |
1389 | ||
1390 | while (1) { | |
1391 | name = vnode_getname(vp); | |
1392 | if (name == NULL) { | |
1393 | printf("lookup_traverse_union: null parent name: .%s\n", pp); | |
1394 | error = ENOENT; | |
1395 | goto done; | |
1396 | } | |
1397 | len = strlen(name); | |
0a7de745 | 1398 | if ((len + 1) > (pp - path)) { // Enough space for this name ? |
39236c6e A |
1399 | error = ENAMETOOLONG; |
1400 | vnode_putname(name); | |
1401 | goto done; | |
1402 | } | |
0a7de745 | 1403 | for (np = name + len; len > 0; len--) { // Copy name backwards |
39236c6e | 1404 | *--pp = *--np; |
0a7de745 | 1405 | } |
39236c6e A |
1406 | vnode_putname(name); |
1407 | vp = vp->v_parent; | |
0a7de745 | 1408 | if (vp == NULLVP || vp->v_flag & VROOT) { |
39236c6e | 1409 | break; |
0a7de745 | 1410 | } |
39236c6e A |
1411 | *--pp = '/'; |
1412 | } | |
1413 | ||
1414 | /* Evaluate the path in the underlying mount */ | |
1415 | NDINIT(&nd, LOOKUP, OP_LOOKUP, USEDVP, UIO_SYSSPACE, CAST_USER_ADDR_T(pp), ctx); | |
1416 | nd.ni_dvp = dvp->v_mount->mnt_vnodecovered; | |
1417 | error = namei(&nd); | |
0a7de745 | 1418 | if (error == 0) { |
39236c6e | 1419 | *new_dvp = nd.ni_vp; |
0a7de745 | 1420 | } |
39236c6e A |
1421 | nameidone(&nd); |
1422 | done: | |
0a7de745 | 1423 | if (path) { |
39236c6e | 1424 | kfree(path, MAXPATHLEN); |
0a7de745 | 1425 | } |
39236c6e A |
1426 | return error; |
1427 | } | |
1428 | ||
0a7de745 | 1429 | int |
6d2010ae A |
1430 | lookup_validate_creation_path(struct nameidata *ndp) |
1431 | { | |
1432 | struct componentname *cnp = &ndp->ni_cnd; | |
1433 | ||
1434 | /* | |
1435 | * If creating and at end of pathname, then can consider | |
1436 | * allowing file to be created. | |
1437 | */ | |
1438 | if (cnp->cn_flags & RDONLY) { | |
1439 | return EROFS; | |
1440 | } | |
1441 | if ((cnp->cn_flags & ISLASTCN) && (ndp->ni_flag & NAMEI_TRAILINGSLASH) && !(cnp->cn_flags & WILLBEDIR)) { | |
1442 | return ENOENT; | |
1443 | } | |
0a7de745 | 1444 | |
6d2010ae A |
1445 | return 0; |
1446 | } | |
1447 | ||
1448 | /* | |
1449 | * Modifies only ni_vp. Always returns with ni_vp still valid (iocount held). | |
1450 | */ | |
fe8ab488 | 1451 | static int |
0a7de745 A |
1452 | lookup_traverse_mountpoints(struct nameidata *ndp, struct componentname *cnp, vnode_t dp, |
1453 | int vbusyflags, vfs_context_t ctx) | |
6d2010ae A |
1454 | { |
1455 | mount_t mp; | |
1456 | vnode_t tdp; | |
1457 | int error = 0; | |
6d2010ae | 1458 | uint32_t depth = 0; |
0a7de745 | 1459 | vnode_t mounted_on_dp; |
6d2010ae | 1460 | int current_mount_generation = 0; |
3e170ce0 A |
1461 | #if CONFIG_TRIGGERS |
1462 | vnode_t triggered_dp = NULLVP; | |
1463 | int retry_cnt = 0; | |
1464 | #define MAX_TRIGGER_RETRIES 1 | |
1465 | #endif | |
0a7de745 A |
1466 | |
1467 | if (dp->v_type != VDIR || cnp->cn_flags & NOCROSSMOUNT) { | |
3e170ce0 | 1468 | return 0; |
0a7de745 | 1469 | } |
39236c6e | 1470 | |
3e170ce0 | 1471 | mounted_on_dp = dp; |
6d2010ae | 1472 | #if CONFIG_TRIGGERS |
3e170ce0 | 1473 | restart: |
6d2010ae | 1474 | #endif |
3e170ce0 | 1475 | current_mount_generation = mount_generation; |
6d2010ae | 1476 | |
3e170ce0 A |
1477 | while (dp->v_mountedhere) { |
1478 | vnode_lock_spin(dp); | |
1479 | if ((mp = dp->v_mountedhere)) { | |
6d2010ae A |
1480 | mp->mnt_crossref++; |
1481 | vnode_unlock(dp); | |
3e170ce0 A |
1482 | } else { |
1483 | vnode_unlock(dp); | |
1484 | break; | |
1485 | } | |
6d2010ae | 1486 | |
3e170ce0 A |
1487 | if (ISSET(mp->mnt_lflag, MNT_LFORCE)) { |
1488 | mount_dropcrossref(mp, dp, 0); | |
0a7de745 | 1489 | break; // don't traverse into a forced unmount |
3e170ce0 | 1490 | } |
6d2010ae | 1491 | |
6d2010ae | 1492 | |
3e170ce0 | 1493 | if (vfs_busy(mp, vbusyflags)) { |
6d2010ae | 1494 | mount_dropcrossref(mp, dp, 0); |
3e170ce0 A |
1495 | if (vbusyflags == LK_NOWAIT) { |
1496 | error = ENOENT; | |
6d2010ae A |
1497 | goto out; |
1498 | } | |
1499 | ||
3e170ce0 A |
1500 | continue; |
1501 | } | |
6d2010ae | 1502 | |
3e170ce0 | 1503 | error = VFS_ROOT(mp, &tdp, ctx); |
6d2010ae | 1504 | |
3e170ce0 A |
1505 | mount_dropcrossref(mp, dp, 0); |
1506 | vfs_unbusy(mp); | |
1507 | ||
1508 | if (error) { | |
1509 | goto out; | |
1510 | } | |
1511 | ||
1512 | vnode_put(dp); | |
1513 | ndp->ni_vp = dp = tdp; | |
1514 | if (dp->v_type != VDIR) { | |
1515 | #if DEVELOPMENT || DEBUG | |
1516 | panic("%s : Root of filesystem not a directory\n", | |
1517 | __FUNCTION__); | |
1518 | #else | |
6d2010ae | 1519 | break; |
3e170ce0 | 1520 | #endif |
6d2010ae | 1521 | } |
3e170ce0 | 1522 | depth++; |
6d2010ae A |
1523 | } |
1524 | ||
3e170ce0 A |
1525 | #if CONFIG_TRIGGERS |
1526 | /* | |
1527 | * The triggered_dp check here is required but is susceptible to a | |
1528 | * (unlikely) race in which trigger mount is done from here and is | |
1529 | * unmounted before we get past vfs_busy above. We retry to deal with | |
1530 | * that case but it has the side effect of unwanted retries for | |
1531 | * "special" processes which don't want to trigger mounts. | |
1532 | */ | |
1533 | if (dp->v_resolve && retry_cnt < MAX_TRIGGER_RETRIES) { | |
1534 | error = vnode_trigger_resolve(dp, ndp, ctx); | |
0a7de745 | 1535 | if (error) { |
3e170ce0 | 1536 | goto out; |
0a7de745 A |
1537 | } |
1538 | if (dp == triggered_dp) { | |
3e170ce0 | 1539 | retry_cnt += 1; |
0a7de745 | 1540 | } else { |
3e170ce0 | 1541 | retry_cnt = 0; |
0a7de745 | 1542 | } |
3e170ce0 A |
1543 | triggered_dp = dp; |
1544 | goto restart; | |
1545 | } | |
1546 | #endif /* CONFIG_TRIGGERS */ | |
1547 | ||
fe8ab488 | 1548 | if (depth) { |
0a7de745 | 1549 | mp = mounted_on_dp->v_mountedhere; |
6d2010ae A |
1550 | |
1551 | if (mp) { | |
0a7de745 | 1552 | mount_lock_spin(mp); |
6d2010ae A |
1553 | mp->mnt_realrootvp_vid = dp->v_id; |
1554 | mp->mnt_realrootvp = dp; | |
1555 | mp->mnt_generation = current_mount_generation; | |
1556 | mount_unlock(mp); | |
1557 | } | |
1558 | } | |
1559 | ||
1560 | return 0; | |
1561 | ||
1562 | out: | |
1563 | return error; | |
1564 | } | |
1565 | ||
1566 | /* | |
1567 | * Takes ni_vp and ni_dvp non-NULL. Returns with *new_dp set to the location | |
bca245ac | 1568 | * at which to start a lookup with a resolved path, and all other iocounts dropped. |
6d2010ae | 1569 | */ |
0a7de745 | 1570 | static int |
bca245ac | 1571 | lookup_handle_symlink(struct nameidata *ndp, vnode_t *new_dp, vfs_context_t ctx) |
6d2010ae A |
1572 | { |
1573 | int error; | |
0a7de745 | 1574 | char *cp; /* pointer into pathname argument */ |
6d2010ae | 1575 | uio_t auio; |
39037602 A |
1576 | union { |
1577 | union { | |
1578 | struct user_iovec s_uiovec; | |
1579 | struct kern_iovec s_kiovec; | |
1580 | } u_iovec; | |
1581 | struct uio s_uio; | |
0a7de745 | 1582 | char uio_buf[UIO_SIZEOF(1)]; |
39037602 | 1583 | } u_uio_buf; /* union only for aligning uio_buf correctly */ |
6d2010ae A |
1584 | int need_newpathbuf; |
1585 | u_int linklen; | |
1586 | struct componentname *cnp = &ndp->ni_cnd; | |
1587 | vnode_t dp; | |
1588 | char *tmppn; | |
cb323159 | 1589 | u_int rsrclen = (cnp->cn_flags & CN_WANTSRSRCFORK) ? sizeof(_PATH_RSRCFORKSPEC) : 0; |
6d2010ae | 1590 | |
6d2010ae A |
1591 | if (ndp->ni_loopcnt++ >= MAXSYMLINKS) { |
1592 | return ELOOP; | |
1593 | } | |
1594 | #if CONFIG_MACF | |
0a7de745 | 1595 | if ((error = mac_vnode_check_readlink(ctx, ndp->ni_vp)) != 0) { |
6d2010ae | 1596 | return error; |
0a7de745 | 1597 | } |
6d2010ae | 1598 | #endif /* MAC */ |
0a7de745 | 1599 | if (ndp->ni_pathlen > 1 || !(cnp->cn_flags & HASBUF)) { |
6d2010ae | 1600 | need_newpathbuf = 1; |
0a7de745 | 1601 | } else { |
6d2010ae | 1602 | need_newpathbuf = 0; |
0a7de745 | 1603 | } |
6d2010ae A |
1604 | |
1605 | if (need_newpathbuf) { | |
1606 | MALLOC_ZONE(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK); | |
1607 | if (cp == NULL) { | |
1608 | return ENOMEM; | |
1609 | } | |
1610 | } else { | |
1611 | cp = cnp->cn_pnbuf; | |
1612 | } | |
39037602 A |
1613 | auio = uio_createwithbuffer(1, 0, UIO_SYSSPACE, UIO_READ, |
1614 | &u_uio_buf.uio_buf[0], sizeof(u_uio_buf.uio_buf)); | |
6d2010ae A |
1615 | |
1616 | uio_addiov(auio, CAST_USER_ADDR_T(cp), MAXPATHLEN); | |
1617 | ||
1618 | error = VNOP_READLINK(ndp->ni_vp, auio, ctx); | |
1619 | if (error) { | |
0a7de745 | 1620 | if (need_newpathbuf) { |
6d2010ae | 1621 | FREE_ZONE(cp, MAXPATHLEN, M_NAMEI); |
0a7de745 | 1622 | } |
6d2010ae A |
1623 | return error; |
1624 | } | |
1625 | ||
0a7de745 | 1626 | /* |
6d2010ae A |
1627 | * Safe to set unsigned with a [larger] signed type here |
1628 | * because 0 <= uio_resid <= MAXPATHLEN and MAXPATHLEN | |
1629 | * is only 1024. | |
1630 | */ | |
1631 | linklen = MAXPATHLEN - (u_int)uio_resid(auio); | |
cb323159 | 1632 | if (linklen + ndp->ni_pathlen + rsrclen > MAXPATHLEN) { |
0a7de745 | 1633 | if (need_newpathbuf) { |
6d2010ae | 1634 | FREE_ZONE(cp, MAXPATHLEN, M_NAMEI); |
0a7de745 | 1635 | } |
6d2010ae A |
1636 | |
1637 | return ENAMETOOLONG; | |
1638 | } | |
1639 | if (need_newpathbuf) { | |
1640 | long len = cnp->cn_pnlen; | |
1641 | ||
1642 | tmppn = cnp->cn_pnbuf; | |
1643 | bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen); | |
1644 | cnp->cn_pnbuf = cp; | |
1645 | cnp->cn_pnlen = MAXPATHLEN; | |
1646 | ||
0a7de745 | 1647 | if ((cnp->cn_flags & HASBUF)) { |
6d2010ae | 1648 | FREE_ZONE(tmppn, len, M_NAMEI); |
0a7de745 | 1649 | } else { |
6d2010ae | 1650 | cnp->cn_flags |= HASBUF; |
0a7de745 A |
1651 | } |
1652 | } else { | |
6d2010ae | 1653 | cnp->cn_pnbuf[linklen] = '\0'; |
0a7de745 | 1654 | } |
6d2010ae A |
1655 | |
1656 | ndp->ni_pathlen += linklen; | |
1657 | cnp->cn_nameptr = cnp->cn_pnbuf; | |
1658 | ||
1659 | /* | |
1660 | * starting point for 'relative' | |
1661 | * symbolic link path | |
1662 | */ | |
1663 | dp = ndp->ni_dvp; | |
1664 | ||
1665 | /* | |
1666 | * get rid of references returned via 'lookup' | |
1667 | */ | |
1668 | vnode_put(ndp->ni_vp); | |
bca245ac A |
1669 | vnode_put(ndp->ni_dvp); /* ALWAYS have a dvp for a symlink */ |
1670 | ||
6d2010ae | 1671 | ndp->ni_vp = NULLVP; |
bca245ac | 1672 | ndp->ni_dvp = NULLVP; |
6d2010ae A |
1673 | |
1674 | /* | |
1675 | * Check if symbolic link restarts us at the root | |
1676 | */ | |
1677 | if (*(cnp->cn_nameptr) == '/') { | |
1678 | while (*(cnp->cn_nameptr) == '/') { | |
1679 | cnp->cn_nameptr++; | |
1680 | ndp->ni_pathlen--; | |
1681 | } | |
1682 | if ((dp = ndp->ni_rootdir) == NULLVP) { | |
1683 | return ENOENT; | |
1684 | } | |
1685 | } | |
1686 | ||
1687 | *new_dp = dp; | |
1688 | ||
1689 | return 0; | |
1690 | } | |
1691 | ||
1c79356b A |
1692 | /* |
1693 | * relookup - lookup a path name component | |
1694 | * Used by lookup to re-aquire things. | |
1695 | */ | |
1696 | int | |
2d21ac55 | 1697 | relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp) |
1c79356b | 1698 | { |
0a7de745 A |
1699 | struct vnode *dp = NULL; /* the directory we are searching */ |
1700 | int wantparent; /* 1 => wantparent or lockparent flag */ | |
1701 | int rdonly; /* lookup read-only flag bit */ | |
1c79356b A |
1702 | int error = 0; |
1703 | #ifdef NAMEI_DIAGNOSTIC | |
0a7de745 A |
1704 | int i, newhash; /* DEBUG: check name hash */ |
1705 | char *cp; /* DEBUG: check name ptr/len */ | |
1c79356b | 1706 | #endif |
91447636 | 1707 | vfs_context_t ctx = cnp->cn_context;; |
1c79356b A |
1708 | |
1709 | /* | |
1710 | * Setup: break out flag bits into variables. | |
1711 | */ | |
0a7de745 | 1712 | wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT); |
1c79356b A |
1713 | rdonly = cnp->cn_flags & RDONLY; |
1714 | cnp->cn_flags &= ~ISSYMLINK; | |
1c79356b | 1715 | |
0a7de745 A |
1716 | if (cnp->cn_flags & NOCACHE) { |
1717 | cnp->cn_flags &= ~MAKEENTRY; | |
1718 | } else { | |
1719 | cnp->cn_flags |= MAKEENTRY; | |
1720 | } | |
91447636 A |
1721 | |
1722 | dp = dvp; | |
1c79356b A |
1723 | |
1724 | /* | |
1725 | * Check for degenerate name (e.g. / or "") | |
1726 | * which is a way of talking about a directory, | |
1727 | * e.g. like "/." or ".". | |
1728 | */ | |
1729 | if (cnp->cn_nameptr[0] == '\0') { | |
1730 | if (cnp->cn_nameiop != LOOKUP || wantparent) { | |
1731 | error = EISDIR; | |
1732 | goto bad; | |
1733 | } | |
1734 | if (dp->v_type != VDIR) { | |
1735 | error = ENOTDIR; | |
1736 | goto bad; | |
1737 | } | |
0a7de745 A |
1738 | if ((vnode_get(dp))) { |
1739 | error = ENOENT; | |
91447636 A |
1740 | goto bad; |
1741 | } | |
1c79356b | 1742 | *vpp = dp; |
91447636 | 1743 | |
0a7de745 | 1744 | if (cnp->cn_flags & SAVESTART) { |
1c79356b | 1745 | panic("lookup: SAVESTART"); |
0a7de745 A |
1746 | } |
1747 | return 0; | |
1c79356b | 1748 | } |
1c79356b A |
1749 | /* |
1750 | * We now have a segment name to search for, and a directory to search. | |
1751 | */ | |
0a7de745 A |
1752 | if ((error = VNOP_LOOKUP(dp, vpp, cnp, ctx))) { |
1753 | if (error != EJUSTRETURN) { | |
91447636 | 1754 | goto bad; |
0a7de745 | 1755 | } |
1c79356b | 1756 | #if DIAGNOSTIC |
0a7de745 | 1757 | if (*vpp != NULL) { |
1c79356b | 1758 | panic("leaf should be empty"); |
0a7de745 | 1759 | } |
1c79356b | 1760 | #endif |
1c79356b A |
1761 | /* |
1762 | * If creating and at end of pathname, then can consider | |
1763 | * allowing file to be created. | |
1764 | */ | |
1765 | if (rdonly) { | |
1766 | error = EROFS; | |
1767 | goto bad; | |
1768 | } | |
1c79356b A |
1769 | /* |
1770 | * We return with ni_vp NULL to indicate that the entry | |
1771 | * doesn't currently exist, leaving a pointer to the | |
1772 | * (possibly locked) directory inode in ndp->ni_dvp. | |
1773 | */ | |
0a7de745 | 1774 | return 0; |
1c79356b A |
1775 | } |
1776 | dp = *vpp; | |
1777 | ||
1778 | #if DIAGNOSTIC | |
1779 | /* | |
1780 | * Check for symbolic link | |
1781 | */ | |
0a7de745 A |
1782 | if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW)) { |
1783 | panic("relookup: symlink found.\n"); | |
1784 | } | |
1c79356b A |
1785 | #endif |
1786 | ||
1787 | /* | |
1788 | * Disallow directory write attempts on read-only file systems. | |
1789 | */ | |
1790 | if (rdonly && | |
1791 | (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) { | |
1792 | error = EROFS; | |
1793 | goto bad2; | |
1794 | } | |
1795 | /* ASSERT(dvp == ndp->ni_startdir) */ | |
0a7de745 A |
1796 | |
1797 | return 0; | |
1c79356b A |
1798 | |
1799 | bad2: | |
91447636 | 1800 | vnode_put(dp); |
0a7de745 | 1801 | bad: |
1c79356b | 1802 | *vpp = NULL; |
91447636 | 1803 | |
0a7de745 | 1804 | return error; |
1c79356b A |
1805 | } |
1806 | ||
6d2010ae A |
1807 | /* |
1808 | * Free pathname buffer | |
1809 | */ | |
1810 | void | |
1811 | nameidone(struct nameidata *ndp) | |
1812 | { | |
91447636 A |
1813 | if (ndp->ni_cnd.cn_flags & HASBUF) { |
1814 | char *tmp = ndp->ni_cnd.cn_pnbuf; | |
1815 | ||
1816 | ndp->ni_cnd.cn_pnbuf = NULL; | |
1817 | ndp->ni_cnd.cn_flags &= ~HASBUF; | |
1818 | FREE_ZONE(tmp, ndp->ni_cnd.cn_pnlen, M_NAMEI); | |
1819 | } | |
1820 | } | |
1821 | ||
1c79356b | 1822 | |
2d21ac55 A |
1823 | /* |
1824 | * Log (part of) a pathname using the KERNEL_DEBUG_CONSTANT mechanism, as used | |
1825 | * by fs_usage. The path up to and including the current component name are | |
1826 | * logged. Up to NUMPARMS*4 bytes of pathname will be logged. If the path | |
1827 | * to be logged is longer than that, then the last NUMPARMS*4 bytes are logged. | |
1828 | * That is, the truncation removes the leading portion of the path. | |
1829 | * | |
1830 | * The logging is done via multiple KERNEL_DEBUG_CONSTANT calls. The first one | |
1831 | * is marked with DBG_FUNC_START. The last one is marked with DBG_FUNC_END | |
1832 | * (in addition to DBG_FUNC_START if it is also the first). There may be | |
1833 | * intermediate ones with neither DBG_FUNC_START nor DBG_FUNC_END. | |
1834 | * | |
1835 | * The first KERNEL_DEBUG_CONSTANT passes the vnode pointer and 12 bytes of | |
1836 | * pathname. The remaining KERNEL_DEBUG_CONSTANT calls add 16 bytes of pathname | |
1837 | * each. The minimum number of KERNEL_DEBUG_CONSTANT calls required to pass | |
1838 | * the path are used. Any excess padding in the final KERNEL_DEBUG_CONSTANT | |
1839 | * (because not all of the 12 or 16 bytes are needed for the remainder of the | |
1840 | * path) is set to zero bytes, or '>' if there is more path beyond the | |
1841 | * current component name (usually because an intermediate component was not | |
1842 | * found). | |
1843 | * | |
1844 | * NOTE: If the path length is greater than NUMPARMS*4, or is not of the form | |
1845 | * 12+N*16, there will be no padding. | |
1846 | * | |
1847 | * TODO: If there is more path beyond the current component name, should we | |
1848 | * force some padding? For example, a lookup for /foo_bar_baz/spam that | |
1849 | * fails because /foo_bar_baz is not found will only log "/foo_bar_baz", with | |
1850 | * no '>' padding. But /foo_bar/spam would log "/foo_bar>>>>". | |
1851 | */ | |
316670eb | 1852 | #if (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) |
39236c6e A |
1853 | |
1854 | void | |
d9a64523 | 1855 | kdebug_vfs_lookup(long *dbg_parms, int dbg_namelen, void *dp, uint32_t flags) |
1c79356b | 1856 | { |
2d21ac55 | 1857 | int code; |
39236c6e | 1858 | unsigned int i; |
d9a64523 A |
1859 | bool lookup = flags & KDBG_VFS_LOOKUP_FLAG_LOOKUP; |
1860 | bool noprocfilt = flags & KDBG_VFS_LOOKUP_FLAG_NOPROCFILT; | |
39236c6e A |
1861 | |
1862 | /* | |
1863 | * In the event that we collect multiple, consecutive pathname | |
1864 | * entries, we must mark the start of the path's string and the end. | |
1865 | */ | |
d9a64523 | 1866 | if (lookup) { |
04b8595b | 1867 | code = VFS_LOOKUP | DBG_FUNC_START; |
d9a64523 | 1868 | } else { |
04b8595b | 1869 | code = VFS_LOOKUP_DONE | DBG_FUNC_START; |
d9a64523 | 1870 | } |
39236c6e | 1871 | |
0a7de745 | 1872 | if (dbg_namelen <= (int)(3 * sizeof(long))) { |
39236c6e | 1873 | code |= DBG_FUNC_END; |
0a7de745 | 1874 | } |
39236c6e | 1875 | |
d9a64523 A |
1876 | if (noprocfilt) { |
1877 | KDBG_RELEASE_NOPROCFILT(code, kdebug_vnode(dp), dbg_parms[0], | |
0a7de745 | 1878 | dbg_parms[1], dbg_parms[2]); |
d9a64523 A |
1879 | } else { |
1880 | KDBG_RELEASE(code, kdebug_vnode(dp), dbg_parms[0], dbg_parms[1], | |
0a7de745 | 1881 | dbg_parms[2]); |
d9a64523 | 1882 | } |
39236c6e A |
1883 | |
1884 | code &= ~DBG_FUNC_START; | |
1885 | ||
0a7de745 A |
1886 | for (i = 3, dbg_namelen -= (3 * sizeof(long)); dbg_namelen > 0; i += 4, dbg_namelen -= (4 * sizeof(long))) { |
1887 | if (dbg_namelen <= (int)(4 * sizeof(long))) { | |
39236c6e | 1888 | code |= DBG_FUNC_END; |
0a7de745 | 1889 | } |
39236c6e | 1890 | |
d9a64523 A |
1891 | if (noprocfilt) { |
1892 | KDBG_RELEASE_NOPROCFILT(code, dbg_parms[i], dbg_parms[i + 1], | |
0a7de745 | 1893 | dbg_parms[i + 2], dbg_parms[i + 3]); |
d9a64523 A |
1894 | } else { |
1895 | KDBG_RELEASE(code, dbg_parms[i], dbg_parms[i + 1], dbg_parms[i + 2], | |
0a7de745 | 1896 | dbg_parms[i + 3]); |
d9a64523 | 1897 | } |
39236c6e A |
1898 | } |
1899 | } | |
1900 | ||
d9a64523 A |
1901 | void |
1902 | kdebug_lookup_gen_events(long *dbg_parms, int dbg_namelen, void *dp, | |
cb323159 | 1903 | bool lookup) |
d9a64523 A |
1904 | { |
1905 | kdebug_vfs_lookup(dbg_parms, dbg_namelen, dp, | |
0a7de745 | 1906 | lookup ? KDBG_VFS_LOOKUP_FLAG_LOOKUP : 0); |
d9a64523 A |
1907 | } |
1908 | ||
1909 | void | |
39236c6e A |
1910 | kdebug_lookup(vnode_t dp, struct componentname *cnp) |
1911 | { | |
2d21ac55 A |
1912 | int dbg_namelen; |
1913 | char *dbg_nameptr; | |
1c79356b | 1914 | long dbg_parms[NUMPARMS]; |
1c79356b A |
1915 | |
1916 | /* Collect the pathname for tracing */ | |
1917 | dbg_namelen = (cnp->cn_nameptr - cnp->cn_pnbuf) + cnp->cn_namelen; | |
1918 | dbg_nameptr = cnp->cn_nameptr + cnp->cn_namelen; | |
1919 | ||
0a7de745 | 1920 | if (dbg_namelen > (int)sizeof(dbg_parms)) { |
2d21ac55 | 1921 | dbg_namelen = sizeof(dbg_parms); |
0a7de745 | 1922 | } |
1c79356b | 1923 | dbg_nameptr -= dbg_namelen; |
0a7de745 | 1924 | |
2d21ac55 A |
1925 | /* Copy the (possibly truncated) path itself */ |
1926 | memcpy(dbg_parms, dbg_nameptr, dbg_namelen); | |
0a7de745 | 1927 | |
2d21ac55 A |
1928 | /* Pad with '\0' or '>' */ |
1929 | if (dbg_namelen < (int)sizeof(dbg_parms)) { | |
1930 | memset((char *)dbg_parms + dbg_namelen, | |
0a7de745 A |
1931 | *(cnp->cn_nameptr + cnp->cn_namelen) ? '>' : 0, |
1932 | sizeof(dbg_parms) - dbg_namelen); | |
1c79356b | 1933 | } |
d9a64523 | 1934 | kdebug_vfs_lookup(dbg_parms, dbg_namelen, (void *)dp, |
0a7de745 | 1935 | KDBG_VFS_LOOKUP_FLAG_LOOKUP); |
d9a64523 | 1936 | } |
0c530ab8 | 1937 | |
39236c6e | 1938 | #else /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */ |
0c530ab8 | 1939 | |
39236c6e | 1940 | void |
d9a64523 | 1941 | kdebug_vfs_lookup(long *dbg_parms __unused, int dbg_namelen __unused, |
0a7de745 | 1942 | void *dp __unused, __unused uint32_t flags) |
39236c6e | 1943 | { |
2d21ac55 | 1944 | } |
39236c6e | 1945 | |
6d2010ae A |
1946 | static void |
1947 | kdebug_lookup(struct vnode *dp __unused, struct componentname *cnp __unused) | |
1948 | { | |
1949 | } | |
316670eb | 1950 | #endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_IST) */ |
6d2010ae A |
1951 | |
1952 | int | |
1953 | vfs_getbyid(fsid_t *fsid, ino64_t ino, vnode_t *vpp, vfs_context_t ctx) | |
1954 | { | |
1955 | mount_t mp; | |
1956 | int error; | |
0a7de745 | 1957 | |
6d2010ae A |
1958 | mp = mount_lookupby_volfsid(fsid->val[0], 1); |
1959 | if (mp == NULL) { | |
1960 | return EINVAL; | |
1961 | } | |
1962 | ||
1963 | /* Get the target vnode. */ | |
1964 | if (ino == 2) { | |
1965 | error = VFS_ROOT(mp, vpp, ctx); | |
1966 | } else { | |
1967 | error = VFS_VGET(mp, ino, vpp, ctx); | |
1968 | } | |
0c530ab8 | 1969 | |
6d2010ae A |
1970 | vfs_unbusy(mp); |
1971 | return error; | |
1972 | } | |
2d21ac55 A |
1973 | /* |
1974 | * Obtain the real path from a legacy volfs style path. | |
1975 | * | |
1976 | * Valid formats of input path: | |
1977 | * | |
1978 | * "555/@" | |
1979 | * "555/2" | |
1980 | * "555/123456" | |
1981 | * "555/123456/foobar" | |
1982 | * | |
1983 | * Where: | |
1984 | * 555 represents the volfs file system id | |
1985 | * '@' and '2' are aliases to the root of a file system | |
1986 | * 123456 represents a file id | |
1987 | * "foobar" represents a file name | |
1988 | */ | |
1989 | #if CONFIG_VOLFS | |
1990 | static int | |
1991 | vfs_getrealpath(const char * path, char * realpath, size_t bufsize, vfs_context_t ctx) | |
1992 | { | |
1993 | vnode_t vp; | |
1994 | struct mount *mp = NULL; | |
1995 | char *str; | |
1996 | char ch; | |
b0d623f7 | 1997 | uint32_t id; |
2d21ac55 A |
1998 | ino64_t ino; |
1999 | int error; | |
2000 | int length; | |
2001 | ||
2002 | /* Get file system id and move str to next component. */ | |
2003 | id = strtoul(path, &str, 10); | |
2004 | if (id == 0 || str[0] != '/') { | |
0a7de745 | 2005 | return EINVAL; |
2d21ac55 A |
2006 | } |
2007 | while (*str == '/') { | |
2008 | str++; | |
0c530ab8 | 2009 | } |
2d21ac55 A |
2010 | ch = *str; |
2011 | ||
2012 | mp = mount_lookupby_volfsid(id, 1); | |
2013 | if (mp == NULL) { | |
0a7de745 | 2014 | return EINVAL; /* unexpected failure */ |
2d21ac55 A |
2015 | } |
2016 | /* Check for an alias to a file system root. */ | |
2017 | if (ch == '@' && str[1] == '\0') { | |
2018 | ino = 2; | |
2019 | str++; | |
2020 | } else { | |
2021 | /* Get file id and move str to next component. */ | |
0a7de745 | 2022 | ino = strtouq(str, &str, 10); |
2d21ac55 A |
2023 | } |
2024 | ||
2025 | /* Get the target vnode. */ | |
2026 | if (ino == 2) { | |
cb323159 A |
2027 | struct vfs_attr vfsattr; |
2028 | int use_vfs_root = TRUE; | |
2029 | ||
2030 | VFSATTR_INIT(&vfsattr); | |
2031 | VFSATTR_WANTED(&vfsattr, f_capabilities); | |
2032 | if (vfs_getattr(mp, &vfsattr, vfs_context_kernel()) == 0 && | |
2033 | VFSATTR_IS_SUPPORTED(&vfsattr, f_capabilities)) { | |
2034 | if ((vfsattr.f_capabilities.capabilities[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS) && | |
2035 | (vfsattr.f_capabilities.valid[VOL_CAPABILITIES_FORMAT] & VOL_CAP_FMT_VOL_GROUPS)) { | |
2036 | use_vfs_root = FALSE; | |
2037 | } | |
2038 | } | |
2039 | ||
2040 | if (use_vfs_root) { | |
2041 | error = VFS_ROOT(mp, &vp, ctx); | |
2042 | } else { | |
2043 | error = VFS_VGET(mp, ino, &vp, ctx); | |
2044 | } | |
2d21ac55 A |
2045 | } else { |
2046 | error = VFS_VGET(mp, ino, &vp, ctx); | |
2047 | } | |
2048 | vfs_unbusy(mp); | |
2049 | if (error) { | |
2050 | goto out; | |
2051 | } | |
2052 | realpath[0] = '\0'; | |
2053 | ||
2054 | /* Get the absolute path to this vnode. */ | |
2055 | error = build_path(vp, realpath, bufsize, &length, 0, ctx); | |
2056 | vnode_put(vp); | |
2057 | ||
2058 | if (error == 0 && *str != '\0') { | |
2059 | int attempt = strlcat(realpath, str, MAXPATHLEN); | |
0a7de745 | 2060 | if (attempt > MAXPATHLEN) { |
2d21ac55 A |
2061 | error = ENAMETOOLONG; |
2062 | } | |
2063 | } | |
2064 | out: | |
0a7de745 | 2065 | return error; |
1c79356b | 2066 | } |
2d21ac55 | 2067 | #endif |
6d2010ae A |
2068 | |
2069 | void | |
2070 | lookup_compound_vnop_post_hook(int error, vnode_t dvp, vnode_t vp, struct nameidata *ndp, int did_create) | |
2071 | { | |
2072 | if (error == 0 && vp == NULLVP) { | |
2073 | panic("NULL vp with error == 0.\n"); | |
2074 | } | |
2075 | ||
0a7de745 | 2076 | /* |
6d2010ae A |
2077 | * We don't want to do any of this if we didn't use the compound vnop |
2078 | * to perform the lookup... i.e. if we're allowing and using the legacy pattern, | |
2079 | * where we did a full lookup. | |
2080 | */ | |
2081 | if ((ndp->ni_flag & NAMEI_COMPOUND_OP_MASK) == 0) { | |
2082 | return; | |
2083 | } | |
2084 | ||
0a7de745 | 2085 | /* |
6d2010ae A |
2086 | * If we're going to continue the lookup, we'll handle |
2087 | * all lookup-related updates at that time. | |
2088 | */ | |
2089 | if (error == EKEEPLOOKING) { | |
2090 | return; | |
2091 | } | |
2092 | ||
2093 | /* | |
2094 | * Only audit or update cache for *found* vnodes. For creation | |
2095 | * neither would happen in the non-compound-vnop case. | |
2096 | */ | |
2097 | if ((vp != NULLVP) && !did_create) { | |
0a7de745 A |
2098 | /* |
2099 | * If MAKEENTRY isn't set, and we've done a successful compound VNOP, | |
6d2010ae A |
2100 | * then we certainly don't want to update cache or identity. |
2101 | */ | |
2102 | if ((error != 0) || (ndp->ni_cnd.cn_flags & MAKEENTRY)) { | |
2103 | lookup_consider_update_cache(dvp, vp, &ndp->ni_cnd, ndp->ni_ncgeneration); | |
2104 | } | |
0a7de745 | 2105 | if (ndp->ni_cnd.cn_flags & AUDITVNPATH1) { |
6d2010ae | 2106 | AUDIT_ARG(vnpath, vp, ARG_VNODE1); |
0a7de745 | 2107 | } else if (ndp->ni_cnd.cn_flags & AUDITVNPATH2) { |
6d2010ae | 2108 | AUDIT_ARG(vnpath, vp, ARG_VNODE2); |
0a7de745 | 2109 | } |
6d2010ae A |
2110 | } |
2111 | ||
0a7de745 A |
2112 | /* |
2113 | * If you created (whether you opened or not), cut a lookup tracepoint | |
6d2010ae A |
2114 | * for the parent dir (as would happen without a compound vnop). Note: we may need |
2115 | * a vnode despite failure in this case! | |
2116 | * | |
2117 | * If you did not create: | |
0a7de745 A |
2118 | * Found child (succeeded or not): cut a tracepoint for the child. |
2119 | * Did not find child: cut a tracepoint with the parent. | |
6d2010ae A |
2120 | */ |
2121 | if (kdebug_enable) { | |
0a7de745 | 2122 | kdebug_lookup(vp ? vp : dvp, &ndp->ni_cnd); |
6d2010ae A |
2123 | } |
2124 | } |