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