vfs_lookup.c revision 1.61 1 1.61 thorpej /* $NetBSD: vfs_lookup.c,v 1.61 2005/06/23 00:30:28 thorpej Exp $ */
2 1.13 cgd
3 1.10 cgd /*
4 1.12 mycroft * Copyright (c) 1982, 1986, 1989, 1993
5 1.12 mycroft * The Regents of the University of California. All rights reserved.
6 1.10 cgd * (c) UNIX System Laboratories, Inc.
7 1.10 cgd * All or some portions of this file are derived from material licensed
8 1.10 cgd * to the University of California by American Telephone and Telegraph
9 1.10 cgd * Co. or Unix System Laboratories, Inc. and are reproduced herein with
10 1.10 cgd * the permission of UNIX System Laboratories, Inc.
11 1.10 cgd *
12 1.10 cgd * Redistribution and use in source and binary forms, with or without
13 1.10 cgd * modification, are permitted provided that the following conditions
14 1.10 cgd * are met:
15 1.10 cgd * 1. Redistributions of source code must retain the above copyright
16 1.10 cgd * notice, this list of conditions and the following disclaimer.
17 1.10 cgd * 2. Redistributions in binary form must reproduce the above copyright
18 1.10 cgd * notice, this list of conditions and the following disclaimer in the
19 1.10 cgd * documentation and/or other materials provided with the distribution.
20 1.49 agc * 3. Neither the name of the University nor the names of its contributors
21 1.10 cgd * may be used to endorse or promote products derived from this software
22 1.10 cgd * without specific prior written permission.
23 1.10 cgd *
24 1.10 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 1.10 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.10 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.10 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 1.10 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.10 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.10 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.10 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.10 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.10 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.10 cgd * SUCH DAMAGE.
35 1.10 cgd *
36 1.26 fvdl * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
37 1.10 cgd */
38 1.38 lukem
39 1.38 lukem #include <sys/cdefs.h>
40 1.61 thorpej __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.61 2005/06/23 00:30:28 thorpej Exp $");
41 1.27 thorpej
42 1.27 thorpej #include "opt_ktrace.h"
43 1.50 cb #include "opt_systrace.h"
44 1.10 cgd
45 1.10 cgd #include <sys/param.h>
46 1.15 cgd #include <sys/systm.h>
47 1.61 thorpej #include <sys/kernel.h>
48 1.10 cgd #include <sys/syslimits.h>
49 1.10 cgd #include <sys/time.h>
50 1.10 cgd #include <sys/namei.h>
51 1.10 cgd #include <sys/vnode.h>
52 1.10 cgd #include <sys/mount.h>
53 1.10 cgd #include <sys/errno.h>
54 1.39 lukem #include <sys/filedesc.h>
55 1.39 lukem #include <sys/hash.h>
56 1.10 cgd #include <sys/malloc.h>
57 1.10 cgd #include <sys/proc.h>
58 1.40 wrstuden #include <sys/syslog.h>
59 1.12 mycroft
60 1.10 cgd #ifdef KTRACE
61 1.10 cgd #include <sys/ktrace.h>
62 1.10 cgd #endif
63 1.50 cb #ifdef SYSTRACE
64 1.50 cb #include <sys/systrace.h>
65 1.50 cb #endif
66 1.16 christos
67 1.35 thorpej struct pool pnbuf_pool; /* pathname buffer pool */
68 1.37 thorpej struct pool_cache pnbuf_cache; /* pathname buffer cache */
69 1.44 thorpej
70 1.44 thorpej MALLOC_DEFINE(M_NAMEI, "namei", "namei path buffer");
71 1.35 thorpej
72 1.10 cgd /*
73 1.61 thorpej * Substitute replacement text for 'magic' strings in symlinks.
74 1.61 thorpej * Returns 0 if successful, and returns non-zero if an error
75 1.61 thorpej * occurs. (Currently, the only possible error is running out
76 1.61 thorpej * of temporary pathname space.)
77 1.61 thorpej *
78 1.61 thorpej * Looks for "@<string>" and "@<string>/", where <string> is a
79 1.61 thorpej * recognized 'magic' string. Replaces the "@<string>" with the
80 1.61 thorpej * appropriate replacement text. (Note that in some cases the
81 1.61 thorpej * replacement text may have zero length.)
82 1.61 thorpej *
83 1.61 thorpej * This would have been table driven, but the variance in
84 1.61 thorpej * replacement strings (and replacement string lengths) made
85 1.61 thorpej * that impractical.
86 1.61 thorpej */
87 1.61 thorpej #define MATCH(str) \
88 1.61 thorpej ((i + (sizeof(str) - 1) == *len) || \
89 1.61 thorpej ((i + (sizeof(str) - 1) < *len) && \
90 1.61 thorpej (cp[i + sizeof(str) - 1] == '/'))) && \
91 1.61 thorpej !strncmp((str), &cp[i], sizeof(str) - 1)
92 1.61 thorpej
93 1.61 thorpej #define SUBSTITUTE(m, s, sl) \
94 1.61 thorpej if ((newlen + (sl)) > MAXPATHLEN) \
95 1.61 thorpej return (1); \
96 1.61 thorpej i += sizeof(m) - 1; \
97 1.61 thorpej memcpy(&tmp[newlen], (s), (sl)); \
98 1.61 thorpej newlen += (sl); \
99 1.61 thorpej change = 1;
100 1.61 thorpej
101 1.61 thorpej static int
102 1.61 thorpej symlink_magic(char *cp, int *len)
103 1.61 thorpej {
104 1.61 thorpej char tmp[MAXPATHLEN];
105 1.61 thorpej int change, i, newlen;
106 1.61 thorpej
107 1.61 thorpej for (change = i = newlen = 0; i < *len; ) {
108 1.61 thorpej if (cp[i] != '@')
109 1.61 thorpej tmp[newlen++] = cp[i++];
110 1.61 thorpej else {
111 1.61 thorpej i++;
112 1.61 thorpej /*
113 1.61 thorpej * The following checks should be ordered according
114 1.61 thorpej * to frequency of use.
115 1.61 thorpej */
116 1.61 thorpej if (MATCH("machine_arch")) {
117 1.61 thorpej SUBSTITUTE("machine_arch", MACHINE_ARCH,
118 1.61 thorpej sizeof(MACHINE_ARCH) - 1);
119 1.61 thorpej } else if (MATCH("machine")) {
120 1.61 thorpej SUBSTITUTE("machine", MACHINE,
121 1.61 thorpej sizeof(MACHINE) - 1);
122 1.61 thorpej } else if (MATCH("hostname")) {
123 1.61 thorpej SUBSTITUTE("hostname", hostname,
124 1.61 thorpej hostnamelen);
125 1.61 thorpej } else if (MATCH("osrelease")) {
126 1.61 thorpej SUBSTITUTE("osrelease", osrelease,
127 1.61 thorpej strlen(osrelease));
128 1.61 thorpej } else if (MATCH("kernel_ident")) {
129 1.61 thorpej SUBSTITUTE("kernel_ident", kernel_ident,
130 1.61 thorpej strlen(kernel_ident));
131 1.61 thorpej } else if (MATCH("domainname")) {
132 1.61 thorpej SUBSTITUTE("domainname", domainname,
133 1.61 thorpej domainnamelen);
134 1.61 thorpej } else if (MATCH("ostype")) {
135 1.61 thorpej SUBSTITUTE("ostype", ostype,
136 1.61 thorpej strlen(ostype));
137 1.61 thorpej } else
138 1.61 thorpej tmp[newlen++] = '@';
139 1.61 thorpej }
140 1.61 thorpej }
141 1.61 thorpej
142 1.61 thorpej if (! change)
143 1.61 thorpej return (0);
144 1.61 thorpej
145 1.61 thorpej memcpy(cp, tmp, newlen);
146 1.61 thorpej *len = newlen;
147 1.61 thorpej
148 1.61 thorpej return (0);
149 1.61 thorpej }
150 1.61 thorpej
151 1.61 thorpej /*
152 1.10 cgd * Convert a pathname into a pointer to a locked inode.
153 1.10 cgd *
154 1.10 cgd * The FOLLOW flag is set when symbolic links are to be followed
155 1.10 cgd * when they occur at the end of the name translation process.
156 1.10 cgd * Symbolic links are always followed for all other pathname
157 1.10 cgd * components other than the last.
158 1.10 cgd *
159 1.10 cgd * The segflg defines whether the name is to be copied from user
160 1.10 cgd * space or kernel space.
161 1.10 cgd *
162 1.10 cgd * Overall outline of namei:
163 1.10 cgd *
164 1.10 cgd * copy in name
165 1.10 cgd * get starting directory
166 1.10 cgd * while (!done && !error) {
167 1.10 cgd * call lookup to search path.
168 1.10 cgd * if symbolic link, massage name in buffer and continue
169 1.10 cgd * }
170 1.10 cgd */
171 1.12 mycroft int
172 1.60 thorpej namei(struct nameidata *ndp)
173 1.10 cgd {
174 1.30 thorpej struct cwdinfo *cwdi; /* pointer to cwd state */
175 1.33 augustss char *cp; /* pointer into pathname argument */
176 1.33 augustss struct vnode *dp; /* the directory we are searching */
177 1.10 cgd struct iovec aiov; /* uio for reading symbolic links */
178 1.10 cgd struct uio auio;
179 1.23 mycroft int error, linklen;
180 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
181 1.10 cgd
182 1.12 mycroft #ifdef DIAGNOSTIC
183 1.48 fvdl if (!cnp->cn_cred || !cnp->cn_proc)
184 1.58 christos panic("namei: bad cred/proc");
185 1.12 mycroft if (cnp->cn_nameiop & (~OPMASK))
186 1.58 christos panic("namei: nameiop contaminated with flags");
187 1.12 mycroft if (cnp->cn_flags & OPMASK)
188 1.58 christos panic("namei: flags contaminated with nameiops");
189 1.12 mycroft #endif
190 1.48 fvdl cwdi = cnp->cn_proc->p_cwdi;
191 1.10 cgd
192 1.10 cgd /*
193 1.10 cgd * Get a buffer for the name to be translated, and copy the
194 1.10 cgd * name into the buffer.
195 1.10 cgd */
196 1.12 mycroft if ((cnp->cn_flags & HASBUF) == 0)
197 1.35 thorpej cnp->cn_pnbuf = PNBUF_GET();
198 1.10 cgd if (ndp->ni_segflg == UIO_SYSSPACE)
199 1.12 mycroft error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
200 1.10 cgd MAXPATHLEN, &ndp->ni_pathlen);
201 1.10 cgd else
202 1.12 mycroft error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
203 1.10 cgd MAXPATHLEN, &ndp->ni_pathlen);
204 1.21 kleink
205 1.21 kleink /*
206 1.21 kleink * POSIX.1 requirement: "" is not a valid file name.
207 1.56 perry */
208 1.21 kleink if (!error && ndp->ni_pathlen == 1)
209 1.21 kleink error = ENOENT;
210 1.21 kleink
211 1.10 cgd if (error) {
212 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
213 1.10 cgd ndp->ni_vp = NULL;
214 1.10 cgd return (error);
215 1.10 cgd }
216 1.10 cgd ndp->ni_loopcnt = 0;
217 1.21 kleink
218 1.10 cgd #ifdef KTRACE
219 1.48 fvdl if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
220 1.48 fvdl ktrnamei(cnp->cn_proc, cnp->cn_pnbuf);
221 1.50 cb #endif
222 1.50 cb #ifdef SYSTRACE
223 1.50 cb if (ISSET(cnp->cn_proc->p_flag, P_SYSTRACE))
224 1.50 cb systrace_namei(ndp);
225 1.10 cgd #endif
226 1.10 cgd
227 1.10 cgd /*
228 1.10 cgd * Get starting point for the translation.
229 1.10 cgd */
230 1.30 thorpej if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL)
231 1.11 cgd ndp->ni_rootdir = rootvnode;
232 1.23 mycroft /*
233 1.23 mycroft * Check if starting from root directory or current directory.
234 1.23 mycroft */
235 1.23 mycroft if (cnp->cn_pnbuf[0] == '/') {
236 1.23 mycroft dp = ndp->ni_rootdir;
237 1.23 mycroft VREF(dp);
238 1.23 mycroft } else {
239 1.30 thorpej dp = cwdi->cwdi_cdir;
240 1.23 mycroft VREF(dp);
241 1.23 mycroft }
242 1.10 cgd for (;;) {
243 1.45 erh if (!dp->v_mount)
244 1.45 erh {
245 1.45 erh /* Give up if the directory is no longer mounted */
246 1.45 erh PNBUF_PUT(cnp->cn_pnbuf);
247 1.45 erh return (ENOENT);
248 1.45 erh }
249 1.12 mycroft cnp->cn_nameptr = cnp->cn_pnbuf;
250 1.10 cgd ndp->ni_startdir = dp;
251 1.16 christos if ((error = lookup(ndp)) != 0) {
252 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
253 1.10 cgd return (error);
254 1.10 cgd }
255 1.10 cgd /*
256 1.10 cgd * Check for symbolic link
257 1.10 cgd */
258 1.12 mycroft if ((cnp->cn_flags & ISSYMLINK) == 0) {
259 1.12 mycroft if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
260 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
261 1.10 cgd else
262 1.12 mycroft cnp->cn_flags |= HASBUF;
263 1.10 cgd return (0);
264 1.10 cgd }
265 1.29 wrstuden if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
266 1.26 fvdl VOP_UNLOCK(ndp->ni_dvp, 0);
267 1.10 cgd if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
268 1.10 cgd error = ELOOP;
269 1.10 cgd break;
270 1.10 cgd }
271 1.25 enami if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
272 1.25 enami error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
273 1.48 fvdl cnp->cn_proc);
274 1.25 enami if (error != 0)
275 1.25 enami break;
276 1.25 enami }
277 1.10 cgd if (ndp->ni_pathlen > 1)
278 1.35 thorpej cp = PNBUF_GET();
279 1.10 cgd else
280 1.12 mycroft cp = cnp->cn_pnbuf;
281 1.10 cgd aiov.iov_base = cp;
282 1.10 cgd aiov.iov_len = MAXPATHLEN;
283 1.10 cgd auio.uio_iov = &aiov;
284 1.10 cgd auio.uio_iovcnt = 1;
285 1.10 cgd auio.uio_offset = 0;
286 1.10 cgd auio.uio_rw = UIO_READ;
287 1.10 cgd auio.uio_segflg = UIO_SYSSPACE;
288 1.55 skrll auio.uio_procp = NULL;
289 1.10 cgd auio.uio_resid = MAXPATHLEN;
290 1.16 christos error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
291 1.16 christos if (error) {
292 1.23 mycroft badlink:
293 1.10 cgd if (ndp->ni_pathlen > 1)
294 1.35 thorpej PNBUF_PUT(cp);
295 1.10 cgd break;
296 1.10 cgd }
297 1.10 cgd linklen = MAXPATHLEN - auio.uio_resid;
298 1.23 mycroft if (linklen == 0) {
299 1.23 mycroft error = ENOENT;
300 1.23 mycroft goto badlink;
301 1.23 mycroft }
302 1.61 thorpej /*
303 1.61 thorpej * Do symlink substitution, if appropriate, and
304 1.61 thorpej * check length for potential overflow.
305 1.61 thorpej */
306 1.61 thorpej if (((ndp->ni_vp->v_mount->mnt_flag & MNT_MAGICLINKS) &&
307 1.61 thorpej symlink_magic(cp, &linklen)) ||
308 1.61 thorpej (linklen + ndp->ni_pathlen >= MAXPATHLEN)) {
309 1.10 cgd error = ENAMETOOLONG;
310 1.23 mycroft goto badlink;
311 1.10 cgd }
312 1.10 cgd if (ndp->ni_pathlen > 1) {
313 1.28 perry memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
314 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
315 1.12 mycroft cnp->cn_pnbuf = cp;
316 1.10 cgd } else
317 1.12 mycroft cnp->cn_pnbuf[linklen] = '\0';
318 1.10 cgd ndp->ni_pathlen += linklen;
319 1.10 cgd vput(ndp->ni_vp);
320 1.10 cgd dp = ndp->ni_dvp;
321 1.23 mycroft /*
322 1.23 mycroft * Check if root directory should replace current directory.
323 1.23 mycroft */
324 1.23 mycroft if (cnp->cn_pnbuf[0] == '/') {
325 1.23 mycroft vrele(dp);
326 1.23 mycroft dp = ndp->ni_rootdir;
327 1.23 mycroft VREF(dp);
328 1.23 mycroft }
329 1.10 cgd }
330 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
331 1.10 cgd vrele(ndp->ni_dvp);
332 1.10 cgd vput(ndp->ni_vp);
333 1.10 cgd ndp->ni_vp = NULL;
334 1.10 cgd return (error);
335 1.10 cgd }
336 1.10 cgd
337 1.10 cgd /*
338 1.39 lukem * Determine the namei hash (for cn_hash) for name.
339 1.39 lukem * If *ep != NULL, hash from name to ep-1.
340 1.39 lukem * If *ep == NULL, hash from name until the first NUL or '/', and
341 1.39 lukem * return the location of this termination character in *ep.
342 1.39 lukem *
343 1.39 lukem * This function returns an equivalent hash to the MI hash32_strn().
344 1.39 lukem * The latter isn't used because in the *ep == NULL case, determining
345 1.39 lukem * the length of the string to the first NUL or `/' and then calling
346 1.39 lukem * hash32_strn() involves unnecessary double-handling of the data.
347 1.39 lukem */
348 1.39 lukem uint32_t
349 1.39 lukem namei_hash(const char *name, const char **ep)
350 1.39 lukem {
351 1.39 lukem uint32_t hash;
352 1.39 lukem
353 1.39 lukem hash = HASH32_STR_INIT;
354 1.39 lukem if (*ep != NULL) {
355 1.39 lukem for (; name < *ep; name++)
356 1.59 christos hash = hash * 33 + *(const uint8_t *)name;
357 1.39 lukem } else {
358 1.39 lukem for (; *name != '\0' && *name != '/'; name++)
359 1.59 christos hash = hash * 33 + *(const uint8_t *)name;
360 1.39 lukem *ep = name;
361 1.39 lukem }
362 1.39 lukem return (hash + (hash >> 5));
363 1.39 lukem }
364 1.39 lukem
365 1.39 lukem /*
366 1.10 cgd * Search a pathname.
367 1.10 cgd * This is a very central and rather complicated routine.
368 1.10 cgd *
369 1.10 cgd * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
370 1.10 cgd * The starting directory is taken from ni_startdir. The pathname is
371 1.10 cgd * descended until done, or a symbolic link is encountered. The variable
372 1.10 cgd * ni_more is clear if the path is completed; it is set to one if a
373 1.10 cgd * symbolic link needing interpretation is encountered.
374 1.10 cgd *
375 1.10 cgd * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
376 1.10 cgd * whether the name is to be looked up, created, renamed, or deleted.
377 1.10 cgd * When CREATE, RENAME, or DELETE is specified, information usable in
378 1.10 cgd * creating, renaming, or deleting a directory entry may be calculated.
379 1.10 cgd * If flag has LOCKPARENT or'ed into it, the parent directory is returned
380 1.10 cgd * locked. If flag has WANTPARENT or'ed into it, the parent directory is
381 1.10 cgd * returned unlocked. Otherwise the parent directory is not returned. If
382 1.10 cgd * the target of the pathname exists and LOCKLEAF is or'ed into the flag
383 1.10 cgd * the target is returned locked, otherwise it is returned unlocked.
384 1.10 cgd * When creating or renaming and LOCKPARENT is specified, the target may not
385 1.10 cgd * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
386 1.56 perry *
387 1.10 cgd * Overall outline of lookup:
388 1.10 cgd *
389 1.10 cgd * dirloop:
390 1.10 cgd * identify next component of name at ndp->ni_ptr
391 1.10 cgd * handle degenerate case where name is null string
392 1.10 cgd * if .. and crossing mount points and on mounted filesys, find parent
393 1.10 cgd * call VOP_LOOKUP routine for next component name
394 1.10 cgd * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
395 1.10 cgd * component vnode returned in ni_vp (if it exists), locked.
396 1.10 cgd * if result vnode is mounted on and crossing mount points,
397 1.10 cgd * find mounted on vnode
398 1.10 cgd * if more components of name, do next level at dirloop
399 1.10 cgd * return the answer in ni_vp, locked if LOCKLEAF set
400 1.10 cgd * if LOCKPARENT set, return locked parent in ni_dvp
401 1.10 cgd * if WANTPARENT set, return unlocked parent in ni_dvp
402 1.10 cgd */
403 1.12 mycroft int
404 1.60 thorpej lookup(struct nameidata *ndp)
405 1.10 cgd {
406 1.33 augustss const char *cp; /* pointer into pathname argument */
407 1.33 augustss struct vnode *dp = 0; /* the directory we are searching */
408 1.10 cgd struct vnode *tdp; /* saved dp */
409 1.10 cgd struct mount *mp; /* mount table entry */
410 1.10 cgd int docache; /* == 0 do not cache last component */
411 1.10 cgd int wantparent; /* 1 => wantparent or lockparent flag */
412 1.12 mycroft int rdonly; /* lookup read-only flag bit */
413 1.10 cgd int error = 0;
414 1.23 mycroft int slashes;
415 1.32 wrstuden int dpunlocked = 0; /* dp has already been unlocked */
416 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
417 1.10 cgd
418 1.10 cgd /*
419 1.10 cgd * Setup: break out flag bits into variables.
420 1.10 cgd */
421 1.12 mycroft wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
422 1.12 mycroft docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
423 1.12 mycroft if (cnp->cn_nameiop == DELETE ||
424 1.12 mycroft (wantparent && cnp->cn_nameiop != CREATE))
425 1.10 cgd docache = 0;
426 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
427 1.10 cgd ndp->ni_dvp = NULL;
428 1.12 mycroft cnp->cn_flags &= ~ISSYMLINK;
429 1.10 cgd dp = ndp->ni_startdir;
430 1.10 cgd ndp->ni_startdir = NULLVP;
431 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
432 1.10 cgd
433 1.23 mycroft /*
434 1.23 mycroft * If we have a leading string of slashes, remove them, and just make
435 1.23 mycroft * sure the current node is a directory.
436 1.23 mycroft */
437 1.23 mycroft cp = cnp->cn_nameptr;
438 1.23 mycroft if (*cp == '/') {
439 1.23 mycroft do {
440 1.23 mycroft cp++;
441 1.23 mycroft } while (*cp == '/');
442 1.23 mycroft ndp->ni_pathlen -= cp - cnp->cn_nameptr;
443 1.23 mycroft cnp->cn_nameptr = cp;
444 1.23 mycroft
445 1.23 mycroft if (dp->v_type != VDIR) {
446 1.23 mycroft error = ENOTDIR;
447 1.23 mycroft goto bad;
448 1.23 mycroft }
449 1.23 mycroft
450 1.23 mycroft /*
451 1.23 mycroft * If we've exhausted the path name, then just return the
452 1.23 mycroft * current node. If the caller requested the parent node (i.e.
453 1.23 mycroft * it's a CREATE, DELETE, or RENAME), and we don't have one
454 1.23 mycroft * (because this is the root directory), then we must fail.
455 1.23 mycroft */
456 1.23 mycroft if (cnp->cn_nameptr[0] == '\0') {
457 1.23 mycroft if (ndp->ni_dvp == NULL && wantparent) {
458 1.23 mycroft error = EISDIR;
459 1.23 mycroft goto bad;
460 1.23 mycroft }
461 1.23 mycroft ndp->ni_vp = dp;
462 1.23 mycroft cnp->cn_flags |= ISLASTCN;
463 1.23 mycroft goto terminal;
464 1.23 mycroft }
465 1.23 mycroft }
466 1.23 mycroft
467 1.10 cgd dirloop:
468 1.10 cgd /*
469 1.10 cgd * Search a new directory.
470 1.10 cgd *
471 1.12 mycroft * The cn_hash value is for use by vfs_cache.
472 1.10 cgd * The last component of the filename is left accessible via
473 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
474 1.10 cgd * the name set the SAVENAME flag. When done, they assume
475 1.10 cgd * responsibility for freeing the pathname buffer.
476 1.10 cgd */
477 1.12 mycroft cnp->cn_consume = 0;
478 1.39 lukem cp = NULL;
479 1.39 lukem cnp->cn_hash = namei_hash(cnp->cn_nameptr, &cp);
480 1.12 mycroft cnp->cn_namelen = cp - cnp->cn_nameptr;
481 1.12 mycroft if (cnp->cn_namelen > NAME_MAX) {
482 1.10 cgd error = ENAMETOOLONG;
483 1.10 cgd goto bad;
484 1.10 cgd }
485 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
486 1.10 cgd { char c = *cp;
487 1.41 soren *(char *)cp = '\0';
488 1.19 christos printf("{%s}: ", cnp->cn_nameptr);
489 1.41 soren *(char *)cp = c; }
490 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
491 1.12 mycroft ndp->ni_pathlen -= cnp->cn_namelen;
492 1.10 cgd ndp->ni_next = cp;
493 1.23 mycroft /*
494 1.23 mycroft * If this component is followed by a slash, then move the pointer to
495 1.23 mycroft * the next component forward, and remember that this component must be
496 1.23 mycroft * a directory.
497 1.23 mycroft */
498 1.23 mycroft if (*cp == '/') {
499 1.23 mycroft do {
500 1.23 mycroft cp++;
501 1.23 mycroft } while (*cp == '/');
502 1.23 mycroft slashes = cp - ndp->ni_next;
503 1.23 mycroft ndp->ni_pathlen -= slashes;
504 1.23 mycroft ndp->ni_next = cp;
505 1.23 mycroft cnp->cn_flags |= REQUIREDIR;
506 1.23 mycroft } else {
507 1.23 mycroft slashes = 0;
508 1.23 mycroft cnp->cn_flags &= ~REQUIREDIR;
509 1.23 mycroft }
510 1.23 mycroft /*
511 1.23 mycroft * We do special processing on the last component, whether or not it's
512 1.23 mycroft * a directory. Cache all intervening lookups, but not the final one.
513 1.23 mycroft */
514 1.23 mycroft if (*cp == '\0') {
515 1.23 mycroft if (docache)
516 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
517 1.23 mycroft else
518 1.23 mycroft cnp->cn_flags &= ~MAKEENTRY;
519 1.23 mycroft cnp->cn_flags |= ISLASTCN;
520 1.23 mycroft } else {
521 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
522 1.23 mycroft cnp->cn_flags &= ~ISLASTCN;
523 1.23 mycroft }
524 1.12 mycroft if (cnp->cn_namelen == 2 &&
525 1.12 mycroft cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
526 1.12 mycroft cnp->cn_flags |= ISDOTDOT;
527 1.12 mycroft else
528 1.12 mycroft cnp->cn_flags &= ~ISDOTDOT;
529 1.10 cgd
530 1.10 cgd /*
531 1.10 cgd * Handle "..": two special cases.
532 1.10 cgd * 1. If at root directory (e.g. after chroot)
533 1.12 mycroft * or at absolute root directory
534 1.10 cgd * then ignore it so can't get out.
535 1.40 wrstuden * 1a. If we have somehow gotten out of a jail, warn
536 1.40 wrstuden * and also ignore it so we can't get farther out.
537 1.10 cgd * 2. If this vnode is the root of a mounted
538 1.10 cgd * filesystem, then replace it with the
539 1.10 cgd * vnode which was mounted on so we take the
540 1.10 cgd * .. in the other file system.
541 1.10 cgd */
542 1.12 mycroft if (cnp->cn_flags & ISDOTDOT) {
543 1.10 cgd for (;;) {
544 1.12 mycroft if (dp == ndp->ni_rootdir || dp == rootvnode) {
545 1.10 cgd ndp->ni_dvp = dp;
546 1.10 cgd ndp->ni_vp = dp;
547 1.10 cgd VREF(dp);
548 1.10 cgd goto nextname;
549 1.40 wrstuden }
550 1.40 wrstuden if (ndp->ni_rootdir != rootvnode) {
551 1.40 wrstuden int retval;
552 1.40 wrstuden VOP_UNLOCK(dp, 0);
553 1.48 fvdl retval = vn_isunder(dp, ndp->ni_rootdir,
554 1.48 fvdl cnp->cn_proc);
555 1.40 wrstuden vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
556 1.40 wrstuden if (!retval) {
557 1.40 wrstuden /* Oops! We got out of jail! */
558 1.40 wrstuden log(LOG_WARNING,
559 1.40 wrstuden "chrooted pid %d uid %d (%s) "
560 1.40 wrstuden "detected outside of its chroot\n",
561 1.48 fvdl cnp->cn_proc->p_pid,
562 1.48 fvdl cnp->cn_proc->p_ucred->cr_uid,
563 1.48 fvdl cnp->cn_proc->p_comm);
564 1.40 wrstuden /* Put us at the jail root. */
565 1.40 wrstuden vput(dp);
566 1.40 wrstuden dp = ndp->ni_rootdir;
567 1.40 wrstuden ndp->ni_dvp = dp;
568 1.40 wrstuden ndp->ni_vp = dp;
569 1.40 wrstuden VREF(dp);
570 1.40 wrstuden VREF(dp);
571 1.40 wrstuden vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
572 1.40 wrstuden goto nextname;
573 1.40 wrstuden }
574 1.10 cgd }
575 1.10 cgd if ((dp->v_flag & VROOT) == 0 ||
576 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT))
577 1.10 cgd break;
578 1.10 cgd tdp = dp;
579 1.10 cgd dp = dp->v_mount->mnt_vnodecovered;
580 1.10 cgd vput(tdp);
581 1.10 cgd VREF(dp);
582 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
583 1.10 cgd }
584 1.10 cgd }
585 1.10 cgd
586 1.10 cgd /*
587 1.10 cgd * We now have a segment name to search for, and a directory to search.
588 1.10 cgd */
589 1.12 mycroft unionlookup:
590 1.12 mycroft ndp->ni_dvp = dp;
591 1.26 fvdl ndp->ni_vp = NULL;
592 1.31 wrstuden cnp->cn_flags &= ~PDIRUNLOCK;
593 1.16 christos if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
594 1.10 cgd #ifdef DIAGNOSTIC
595 1.10 cgd if (ndp->ni_vp != NULL)
596 1.43 christos panic("leaf `%s' should be empty", cnp->cn_nameptr);
597 1.52 yamt #endif /* DIAGNOSTIC */
598 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
599 1.19 christos printf("not found\n");
600 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
601 1.12 mycroft if ((error == ENOENT) &&
602 1.10 cgd (dp->v_flag & VROOT) &&
603 1.10 cgd (dp->v_mount->mnt_flag & MNT_UNION)) {
604 1.10 cgd tdp = dp;
605 1.10 cgd dp = dp->v_mount->mnt_vnodecovered;
606 1.31 wrstuden if (cnp->cn_flags & PDIRUNLOCK)
607 1.31 wrstuden vrele(tdp);
608 1.31 wrstuden else
609 1.31 wrstuden vput(tdp);
610 1.10 cgd VREF(dp);
611 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
612 1.12 mycroft goto unionlookup;
613 1.10 cgd }
614 1.12 mycroft
615 1.57 wrstuden if (cnp->cn_flags & PDIRUNLOCK)
616 1.57 wrstuden dpunlocked = 1;
617 1.57 wrstuden
618 1.10 cgd if (error != EJUSTRETURN)
619 1.10 cgd goto bad;
620 1.10 cgd /*
621 1.23 mycroft * If this was not the last component, or there were trailing
622 1.51 christos * slashes, and we are not going to create a directory,
623 1.51 christos * then the name must exist.
624 1.23 mycroft */
625 1.51 christos if ((cnp->cn_flags & (REQUIREDIR | CREATEDIR)) == REQUIREDIR) {
626 1.23 mycroft error = ENOENT;
627 1.23 mycroft goto bad;
628 1.23 mycroft }
629 1.23 mycroft /*
630 1.10 cgd * If creating and at end of pathname, then can consider
631 1.10 cgd * allowing file to be created.
632 1.10 cgd */
633 1.26 fvdl if (rdonly) {
634 1.10 cgd error = EROFS;
635 1.10 cgd goto bad;
636 1.10 cgd }
637 1.10 cgd /*
638 1.10 cgd * We return with ni_vp NULL to indicate that the entry
639 1.10 cgd * doesn't currently exist, leaving a pointer to the
640 1.10 cgd * (possibly locked) directory inode in ndp->ni_dvp.
641 1.10 cgd */
642 1.12 mycroft if (cnp->cn_flags & SAVESTART) {
643 1.10 cgd ndp->ni_startdir = ndp->ni_dvp;
644 1.10 cgd VREF(ndp->ni_startdir);
645 1.10 cgd }
646 1.10 cgd return (0);
647 1.10 cgd }
648 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
649 1.19 christos printf("found\n");
650 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
651 1.10 cgd
652 1.12 mycroft /*
653 1.23 mycroft * Take into account any additional components consumed by the
654 1.23 mycroft * underlying filesystem. This will include any trailing slashes after
655 1.23 mycroft * the last component consumed.
656 1.12 mycroft */
657 1.12 mycroft if (cnp->cn_consume > 0) {
658 1.23 mycroft ndp->ni_pathlen -= cnp->cn_consume - slashes;
659 1.23 mycroft ndp->ni_next += cnp->cn_consume - slashes;
660 1.12 mycroft cnp->cn_consume = 0;
661 1.23 mycroft if (ndp->ni_next[0] == '\0')
662 1.23 mycroft cnp->cn_flags |= ISLASTCN;
663 1.12 mycroft }
664 1.12 mycroft
665 1.10 cgd dp = ndp->ni_vp;
666 1.10 cgd /*
667 1.10 cgd * Check to see if the vnode has been mounted on;
668 1.10 cgd * if so find the root of the mounted file system.
669 1.10 cgd */
670 1.10 cgd while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
671 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT) == 0) {
672 1.26 fvdl if (vfs_busy(mp, 0, 0))
673 1.12 mycroft continue;
674 1.32 wrstuden VOP_UNLOCK(dp, 0);
675 1.47 thorpej error = VFS_ROOT(mp, &tdp);
676 1.26 fvdl vfs_unbusy(mp);
677 1.32 wrstuden if (error) {
678 1.32 wrstuden dpunlocked = 1;
679 1.10 cgd goto bad2;
680 1.32 wrstuden }
681 1.32 wrstuden vrele(dp);
682 1.10 cgd ndp->ni_vp = dp = tdp;
683 1.14 mycroft }
684 1.14 mycroft
685 1.14 mycroft /*
686 1.23 mycroft * Check for symbolic link. Back up over any slashes that we skipped,
687 1.23 mycroft * as we will need them again.
688 1.14 mycroft */
689 1.23 mycroft if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
690 1.23 mycroft ndp->ni_pathlen += slashes;
691 1.23 mycroft ndp->ni_next -= slashes;
692 1.14 mycroft cnp->cn_flags |= ISSYMLINK;
693 1.14 mycroft return (0);
694 1.10 cgd }
695 1.10 cgd
696 1.23 mycroft /*
697 1.23 mycroft * Check for directory, if the component was followed by a series of
698 1.23 mycroft * slashes.
699 1.23 mycroft */
700 1.23 mycroft if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
701 1.23 mycroft error = ENOTDIR;
702 1.23 mycroft goto bad2;
703 1.23 mycroft }
704 1.23 mycroft
705 1.10 cgd nextname:
706 1.10 cgd /*
707 1.23 mycroft * Not a symbolic link. If this was not the last component, then
708 1.23 mycroft * continue at the next component, else return.
709 1.10 cgd */
710 1.23 mycroft if (!(cnp->cn_flags & ISLASTCN)) {
711 1.12 mycroft cnp->cn_nameptr = ndp->ni_next;
712 1.10 cgd vrele(ndp->ni_dvp);
713 1.10 cgd goto dirloop;
714 1.10 cgd }
715 1.23 mycroft
716 1.23 mycroft terminal:
717 1.10 cgd /*
718 1.26 fvdl * Disallow directory write attempts on read-only file systems.
719 1.10 cgd */
720 1.26 fvdl if (rdonly &&
721 1.26 fvdl (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
722 1.10 cgd /*
723 1.10 cgd * Disallow directory write attempts on read-only
724 1.10 cgd * file systems.
725 1.10 cgd */
726 1.26 fvdl error = EROFS;
727 1.26 fvdl goto bad2;
728 1.10 cgd }
729 1.23 mycroft if (ndp->ni_dvp != NULL) {
730 1.23 mycroft if (cnp->cn_flags & SAVESTART) {
731 1.23 mycroft ndp->ni_startdir = ndp->ni_dvp;
732 1.23 mycroft VREF(ndp->ni_startdir);
733 1.23 mycroft }
734 1.23 mycroft if (!wantparent)
735 1.23 mycroft vrele(ndp->ni_dvp);
736 1.10 cgd }
737 1.12 mycroft if ((cnp->cn_flags & LOCKLEAF) == 0)
738 1.26 fvdl VOP_UNLOCK(dp, 0);
739 1.10 cgd return (0);
740 1.10 cgd
741 1.10 cgd bad2:
742 1.31 wrstuden if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
743 1.31 wrstuden ((cnp->cn_flags & PDIRUNLOCK) == 0))
744 1.26 fvdl VOP_UNLOCK(ndp->ni_dvp, 0);
745 1.10 cgd vrele(ndp->ni_dvp);
746 1.10 cgd bad:
747 1.32 wrstuden if (dpunlocked)
748 1.32 wrstuden vrele(dp);
749 1.32 wrstuden else
750 1.32 wrstuden vput(dp);
751 1.10 cgd ndp->ni_vp = NULL;
752 1.12 mycroft return (error);
753 1.12 mycroft }
754 1.12 mycroft
755 1.12 mycroft /*
756 1.12 mycroft * Reacquire a path name component.
757 1.12 mycroft */
758 1.12 mycroft int
759 1.60 thorpej relookup(struct vnode *dvp, struct vnode **vpp, struct componentname *cnp)
760 1.12 mycroft {
761 1.26 fvdl struct vnode *dp = 0; /* the directory we are searching */
762 1.12 mycroft int wantparent; /* 1 => wantparent or lockparent flag */
763 1.12 mycroft int rdonly; /* lookup read-only flag bit */
764 1.12 mycroft int error = 0;
765 1.52 yamt #ifdef DEBUG
766 1.54 hannken u_long newhash; /* DEBUG: check name hash */
767 1.41 soren const char *cp; /* DEBUG: check name ptr/len */
768 1.52 yamt #endif /* DEBUG */
769 1.12 mycroft
770 1.12 mycroft /*
771 1.12 mycroft * Setup: break out flag bits into variables.
772 1.12 mycroft */
773 1.12 mycroft wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
774 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
775 1.12 mycroft cnp->cn_flags &= ~ISSYMLINK;
776 1.12 mycroft dp = dvp;
777 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
778 1.12 mycroft
779 1.12 mycroft /* dirloop: */
780 1.12 mycroft /*
781 1.12 mycroft * Search a new directory.
782 1.12 mycroft *
783 1.12 mycroft * The cn_hash value is for use by vfs_cache.
784 1.12 mycroft * The last component of the filename is left accessible via
785 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
786 1.12 mycroft * the name set the SAVENAME flag. When done, they assume
787 1.12 mycroft * responsibility for freeing the pathname buffer.
788 1.12 mycroft */
789 1.52 yamt #ifdef DEBUG
790 1.39 lukem cp = NULL;
791 1.39 lukem newhash = namei_hash(cnp->cn_nameptr, &cp);
792 1.12 mycroft if (newhash != cnp->cn_hash)
793 1.12 mycroft panic("relookup: bad hash");
794 1.12 mycroft if (cnp->cn_namelen != cp - cnp->cn_nameptr)
795 1.58 christos panic("relookup: bad len");
796 1.53 yamt while (*cp == '/')
797 1.53 yamt cp++;
798 1.12 mycroft if (*cp != 0)
799 1.12 mycroft panic("relookup: not last component");
800 1.52 yamt #endif /* DEBUG */
801 1.52 yamt #ifdef NAMEI_DIAGNOSTIC
802 1.19 christos printf("{%s}: ", cnp->cn_nameptr);
803 1.52 yamt #endif /* NAMEI_DIAGNOSTIC */
804 1.12 mycroft
805 1.12 mycroft /*
806 1.12 mycroft * Check for degenerate name (e.g. / or "")
807 1.12 mycroft * which is a way of talking about a directory,
808 1.12 mycroft * e.g. like "/." or ".".
809 1.12 mycroft */
810 1.23 mycroft if (cnp->cn_nameptr[0] == '\0')
811 1.23 mycroft panic("relookup: null name");
812 1.12 mycroft
813 1.12 mycroft if (cnp->cn_flags & ISDOTDOT)
814 1.58 christos panic("relookup: lookup on dot-dot");
815 1.12 mycroft
816 1.12 mycroft /*
817 1.12 mycroft * We now have a segment name to search for, and a directory to search.
818 1.12 mycroft */
819 1.16 christos if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
820 1.12 mycroft #ifdef DIAGNOSTIC
821 1.12 mycroft if (*vpp != NULL)
822 1.43 christos panic("leaf `%s' should be empty", cnp->cn_nameptr);
823 1.12 mycroft #endif
824 1.12 mycroft if (error != EJUSTRETURN)
825 1.12 mycroft goto bad;
826 1.12 mycroft /*
827 1.12 mycroft * If creating and at end of pathname, then can consider
828 1.12 mycroft * allowing file to be created.
829 1.12 mycroft */
830 1.26 fvdl if (rdonly) {
831 1.12 mycroft error = EROFS;
832 1.12 mycroft goto bad;
833 1.12 mycroft }
834 1.12 mycroft /* ASSERT(dvp == ndp->ni_startdir) */
835 1.12 mycroft if (cnp->cn_flags & SAVESTART)
836 1.12 mycroft VREF(dvp);
837 1.12 mycroft /*
838 1.12 mycroft * We return with ni_vp NULL to indicate that the entry
839 1.12 mycroft * doesn't currently exist, leaving a pointer to the
840 1.12 mycroft * (possibly locked) directory inode in ndp->ni_dvp.
841 1.12 mycroft */
842 1.12 mycroft return (0);
843 1.12 mycroft }
844 1.12 mycroft dp = *vpp;
845 1.12 mycroft
846 1.12 mycroft #ifdef DIAGNOSTIC
847 1.12 mycroft /*
848 1.12 mycroft * Check for symbolic link
849 1.12 mycroft */
850 1.12 mycroft if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
851 1.58 christos panic("relookup: symlink found");
852 1.12 mycroft #endif
853 1.12 mycroft
854 1.12 mycroft /*
855 1.12 mycroft * Check for read-only file systems.
856 1.12 mycroft */
857 1.26 fvdl if (rdonly &&
858 1.26 fvdl (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
859 1.26 fvdl error = EROFS;
860 1.26 fvdl goto bad2;
861 1.12 mycroft }
862 1.12 mycroft /* ASSERT(dvp == ndp->ni_startdir) */
863 1.12 mycroft if (cnp->cn_flags & SAVESTART)
864 1.12 mycroft VREF(dvp);
865 1.12 mycroft if (!wantparent)
866 1.12 mycroft vrele(dvp);
867 1.12 mycroft if ((cnp->cn_flags & LOCKLEAF) == 0)
868 1.26 fvdl VOP_UNLOCK(dp, 0);
869 1.12 mycroft return (0);
870 1.12 mycroft
871 1.12 mycroft bad2:
872 1.12 mycroft if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
873 1.26 fvdl VOP_UNLOCK(dvp, 0);
874 1.12 mycroft vrele(dvp);
875 1.12 mycroft bad:
876 1.12 mycroft vput(dp);
877 1.12 mycroft *vpp = NULL;
878 1.10 cgd return (error);
879 1.10 cgd }
880