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