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