vfs_lookup.c revision 1.37.2.1 1 1.37.2.1 thorpej /* $NetBSD: vfs_lookup.c,v 1.37.2.1 2001/11/12 21:19:02 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.10 cgd * 3. All advertising materials mentioning features or use of this software
21 1.10 cgd * must display the following acknowledgement:
22 1.10 cgd * This product includes software developed by the University of
23 1.10 cgd * California, Berkeley and its contributors.
24 1.10 cgd * 4. Neither the name of the University nor the names of its contributors
25 1.10 cgd * may be used to endorse or promote products derived from this software
26 1.10 cgd * without specific prior written permission.
27 1.10 cgd *
28 1.10 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.10 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.10 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.10 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.10 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.10 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.10 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.10 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.10 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.10 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.10 cgd * SUCH DAMAGE.
39 1.10 cgd *
40 1.26 fvdl * @(#)vfs_lookup.c 8.10 (Berkeley) 5/27/95
41 1.10 cgd */
42 1.37.2.1 thorpej
43 1.37.2.1 thorpej #include <sys/cdefs.h>
44 1.37.2.1 thorpej __KERNEL_RCSID(0, "$NetBSD: vfs_lookup.c,v 1.37.2.1 2001/11/12 21:19:02 thorpej Exp $");
45 1.27 thorpej
46 1.27 thorpej #include "opt_ktrace.h"
47 1.10 cgd
48 1.10 cgd #include <sys/param.h>
49 1.15 cgd #include <sys/systm.h>
50 1.10 cgd #include <sys/syslimits.h>
51 1.10 cgd #include <sys/time.h>
52 1.10 cgd #include <sys/namei.h>
53 1.10 cgd #include <sys/vnode.h>
54 1.10 cgd #include <sys/mount.h>
55 1.10 cgd #include <sys/errno.h>
56 1.10 cgd #include <sys/malloc.h>
57 1.10 cgd #include <sys/filedesc.h>
58 1.10 cgd #include <sys/proc.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.16 christos
64 1.35 thorpej struct pool pnbuf_pool; /* pathname buffer pool */
65 1.37 thorpej struct pool_cache pnbuf_cache; /* pathname buffer cache */
66 1.35 thorpej
67 1.10 cgd /*
68 1.10 cgd * Convert a pathname into a pointer to a locked inode.
69 1.10 cgd *
70 1.10 cgd * The FOLLOW flag is set when symbolic links are to be followed
71 1.10 cgd * when they occur at the end of the name translation process.
72 1.10 cgd * Symbolic links are always followed for all other pathname
73 1.10 cgd * components other than the last.
74 1.10 cgd *
75 1.10 cgd * The segflg defines whether the name is to be copied from user
76 1.10 cgd * space or kernel space.
77 1.10 cgd *
78 1.10 cgd * Overall outline of namei:
79 1.10 cgd *
80 1.10 cgd * copy in name
81 1.10 cgd * get starting directory
82 1.10 cgd * while (!done && !error) {
83 1.10 cgd * call lookup to search path.
84 1.10 cgd * if symbolic link, massage name in buffer and continue
85 1.10 cgd * }
86 1.10 cgd */
87 1.12 mycroft int
88 1.12 mycroft namei(ndp)
89 1.33 augustss struct nameidata *ndp;
90 1.10 cgd {
91 1.30 thorpej struct cwdinfo *cwdi; /* pointer to cwd state */
92 1.33 augustss char *cp; /* pointer into pathname argument */
93 1.33 augustss struct vnode *dp; /* the directory we are searching */
94 1.10 cgd struct iovec aiov; /* uio for reading symbolic links */
95 1.10 cgd struct uio auio;
96 1.23 mycroft int error, linklen;
97 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
98 1.10 cgd
99 1.12 mycroft #ifdef DIAGNOSTIC
100 1.12 mycroft if (!cnp->cn_cred || !cnp->cn_proc)
101 1.12 mycroft panic ("namei: bad cred/proc");
102 1.12 mycroft if (cnp->cn_nameiop & (~OPMASK))
103 1.12 mycroft panic ("namei: nameiop contaminated with flags");
104 1.12 mycroft if (cnp->cn_flags & OPMASK)
105 1.12 mycroft panic ("namei: flags contaminated with nameiops");
106 1.12 mycroft #endif
107 1.30 thorpej cwdi = cnp->cn_proc->p_cwdi;
108 1.10 cgd
109 1.10 cgd /*
110 1.10 cgd * Get a buffer for the name to be translated, and copy the
111 1.10 cgd * name into the buffer.
112 1.10 cgd */
113 1.12 mycroft if ((cnp->cn_flags & HASBUF) == 0)
114 1.35 thorpej cnp->cn_pnbuf = PNBUF_GET();
115 1.10 cgd if (ndp->ni_segflg == UIO_SYSSPACE)
116 1.12 mycroft error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
117 1.10 cgd MAXPATHLEN, &ndp->ni_pathlen);
118 1.10 cgd else
119 1.12 mycroft error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
120 1.10 cgd MAXPATHLEN, &ndp->ni_pathlen);
121 1.21 kleink
122 1.21 kleink /*
123 1.21 kleink * POSIX.1 requirement: "" is not a valid file name.
124 1.21 kleink */
125 1.21 kleink if (!error && ndp->ni_pathlen == 1)
126 1.21 kleink error = ENOENT;
127 1.21 kleink
128 1.10 cgd if (error) {
129 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
130 1.10 cgd ndp->ni_vp = NULL;
131 1.10 cgd return (error);
132 1.10 cgd }
133 1.10 cgd ndp->ni_loopcnt = 0;
134 1.21 kleink
135 1.10 cgd #ifdef KTRACE
136 1.12 mycroft if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
137 1.34 sommerfe ktrnamei(cnp->cn_proc, cnp->cn_pnbuf);
138 1.10 cgd #endif
139 1.10 cgd
140 1.10 cgd /*
141 1.10 cgd * Get starting point for the translation.
142 1.10 cgd */
143 1.30 thorpej if ((ndp->ni_rootdir = cwdi->cwdi_rdir) == NULL)
144 1.11 cgd ndp->ni_rootdir = rootvnode;
145 1.23 mycroft /*
146 1.23 mycroft * Check if starting from root directory or current directory.
147 1.23 mycroft */
148 1.23 mycroft if (cnp->cn_pnbuf[0] == '/') {
149 1.23 mycroft dp = ndp->ni_rootdir;
150 1.23 mycroft VREF(dp);
151 1.23 mycroft } else {
152 1.30 thorpej dp = cwdi->cwdi_cdir;
153 1.23 mycroft VREF(dp);
154 1.23 mycroft }
155 1.10 cgd for (;;) {
156 1.12 mycroft cnp->cn_nameptr = cnp->cn_pnbuf;
157 1.10 cgd ndp->ni_startdir = dp;
158 1.16 christos if ((error = lookup(ndp)) != 0) {
159 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
160 1.10 cgd return (error);
161 1.10 cgd }
162 1.10 cgd /*
163 1.10 cgd * Check for symbolic link
164 1.10 cgd */
165 1.12 mycroft if ((cnp->cn_flags & ISSYMLINK) == 0) {
166 1.12 mycroft if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
167 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
168 1.10 cgd else
169 1.12 mycroft cnp->cn_flags |= HASBUF;
170 1.10 cgd return (0);
171 1.10 cgd }
172 1.29 wrstuden if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
173 1.26 fvdl VOP_UNLOCK(ndp->ni_dvp, 0);
174 1.10 cgd if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
175 1.10 cgd error = ELOOP;
176 1.10 cgd break;
177 1.10 cgd }
178 1.25 enami if (ndp->ni_vp->v_mount->mnt_flag & MNT_SYMPERM) {
179 1.25 enami error = VOP_ACCESS(ndp->ni_vp, VEXEC, cnp->cn_cred,
180 1.25 enami cnp->cn_proc);
181 1.25 enami if (error != 0)
182 1.25 enami break;
183 1.25 enami }
184 1.10 cgd if (ndp->ni_pathlen > 1)
185 1.35 thorpej cp = PNBUF_GET();
186 1.10 cgd else
187 1.12 mycroft cp = cnp->cn_pnbuf;
188 1.10 cgd aiov.iov_base = cp;
189 1.10 cgd aiov.iov_len = MAXPATHLEN;
190 1.10 cgd auio.uio_iov = &aiov;
191 1.10 cgd auio.uio_iovcnt = 1;
192 1.10 cgd auio.uio_offset = 0;
193 1.10 cgd auio.uio_rw = UIO_READ;
194 1.10 cgd auio.uio_segflg = UIO_SYSSPACE;
195 1.10 cgd auio.uio_procp = (struct proc *)0;
196 1.10 cgd auio.uio_resid = MAXPATHLEN;
197 1.16 christos error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
198 1.16 christos if (error) {
199 1.23 mycroft badlink:
200 1.10 cgd if (ndp->ni_pathlen > 1)
201 1.35 thorpej PNBUF_PUT(cp);
202 1.10 cgd break;
203 1.10 cgd }
204 1.10 cgd linklen = MAXPATHLEN - auio.uio_resid;
205 1.23 mycroft if (linklen == 0) {
206 1.23 mycroft error = ENOENT;
207 1.23 mycroft goto badlink;
208 1.23 mycroft }
209 1.10 cgd if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
210 1.10 cgd error = ENAMETOOLONG;
211 1.23 mycroft goto badlink;
212 1.10 cgd }
213 1.10 cgd if (ndp->ni_pathlen > 1) {
214 1.28 perry memcpy(cp + linklen, ndp->ni_next, ndp->ni_pathlen);
215 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
216 1.12 mycroft cnp->cn_pnbuf = cp;
217 1.10 cgd } else
218 1.12 mycroft cnp->cn_pnbuf[linklen] = '\0';
219 1.10 cgd ndp->ni_pathlen += linklen;
220 1.10 cgd vput(ndp->ni_vp);
221 1.10 cgd dp = ndp->ni_dvp;
222 1.23 mycroft /*
223 1.23 mycroft * Check if root directory should replace current directory.
224 1.23 mycroft */
225 1.23 mycroft if (cnp->cn_pnbuf[0] == '/') {
226 1.23 mycroft vrele(dp);
227 1.23 mycroft dp = ndp->ni_rootdir;
228 1.23 mycroft VREF(dp);
229 1.23 mycroft }
230 1.10 cgd }
231 1.35 thorpej PNBUF_PUT(cnp->cn_pnbuf);
232 1.10 cgd vrele(ndp->ni_dvp);
233 1.10 cgd vput(ndp->ni_vp);
234 1.10 cgd ndp->ni_vp = NULL;
235 1.10 cgd return (error);
236 1.10 cgd }
237 1.10 cgd
238 1.10 cgd /*
239 1.10 cgd * Search a pathname.
240 1.10 cgd * This is a very central and rather complicated routine.
241 1.10 cgd *
242 1.10 cgd * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
243 1.10 cgd * The starting directory is taken from ni_startdir. The pathname is
244 1.10 cgd * descended until done, or a symbolic link is encountered. The variable
245 1.10 cgd * ni_more is clear if the path is completed; it is set to one if a
246 1.10 cgd * symbolic link needing interpretation is encountered.
247 1.10 cgd *
248 1.10 cgd * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
249 1.10 cgd * whether the name is to be looked up, created, renamed, or deleted.
250 1.10 cgd * When CREATE, RENAME, or DELETE is specified, information usable in
251 1.10 cgd * creating, renaming, or deleting a directory entry may be calculated.
252 1.10 cgd * If flag has LOCKPARENT or'ed into it, the parent directory is returned
253 1.10 cgd * locked. If flag has WANTPARENT or'ed into it, the parent directory is
254 1.10 cgd * returned unlocked. Otherwise the parent directory is not returned. If
255 1.10 cgd * the target of the pathname exists and LOCKLEAF is or'ed into the flag
256 1.10 cgd * the target is returned locked, otherwise it is returned unlocked.
257 1.10 cgd * When creating or renaming and LOCKPARENT is specified, the target may not
258 1.10 cgd * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
259 1.10 cgd *
260 1.10 cgd * Overall outline of lookup:
261 1.10 cgd *
262 1.10 cgd * dirloop:
263 1.10 cgd * identify next component of name at ndp->ni_ptr
264 1.10 cgd * handle degenerate case where name is null string
265 1.10 cgd * if .. and crossing mount points and on mounted filesys, find parent
266 1.10 cgd * call VOP_LOOKUP routine for next component name
267 1.10 cgd * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
268 1.10 cgd * component vnode returned in ni_vp (if it exists), locked.
269 1.10 cgd * if result vnode is mounted on and crossing mount points,
270 1.10 cgd * find mounted on vnode
271 1.10 cgd * if more components of name, do next level at dirloop
272 1.10 cgd * return the answer in ni_vp, locked if LOCKLEAF set
273 1.10 cgd * if LOCKPARENT set, return locked parent in ni_dvp
274 1.10 cgd * if WANTPARENT set, return unlocked parent in ni_dvp
275 1.10 cgd */
276 1.12 mycroft int
277 1.12 mycroft lookup(ndp)
278 1.33 augustss struct nameidata *ndp;
279 1.10 cgd {
280 1.33 augustss const char *cp; /* pointer into pathname argument */
281 1.33 augustss struct vnode *dp = 0; /* the directory we are searching */
282 1.10 cgd struct vnode *tdp; /* saved dp */
283 1.10 cgd struct mount *mp; /* mount table entry */
284 1.10 cgd int docache; /* == 0 do not cache last component */
285 1.10 cgd int wantparent; /* 1 => wantparent or lockparent flag */
286 1.12 mycroft int rdonly; /* lookup read-only flag bit */
287 1.10 cgd int error = 0;
288 1.23 mycroft int slashes;
289 1.32 wrstuden int dpunlocked = 0; /* dp has already been unlocked */
290 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
291 1.10 cgd
292 1.10 cgd /*
293 1.10 cgd * Setup: break out flag bits into variables.
294 1.10 cgd */
295 1.12 mycroft wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
296 1.12 mycroft docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
297 1.12 mycroft if (cnp->cn_nameiop == DELETE ||
298 1.12 mycroft (wantparent && cnp->cn_nameiop != CREATE))
299 1.10 cgd docache = 0;
300 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
301 1.10 cgd ndp->ni_dvp = NULL;
302 1.12 mycroft cnp->cn_flags &= ~ISSYMLINK;
303 1.10 cgd dp = ndp->ni_startdir;
304 1.10 cgd ndp->ni_startdir = NULLVP;
305 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
306 1.10 cgd
307 1.23 mycroft /*
308 1.23 mycroft * If we have a leading string of slashes, remove them, and just make
309 1.23 mycroft * sure the current node is a directory.
310 1.23 mycroft */
311 1.23 mycroft cp = cnp->cn_nameptr;
312 1.23 mycroft if (*cp == '/') {
313 1.23 mycroft do {
314 1.23 mycroft cp++;
315 1.23 mycroft } while (*cp == '/');
316 1.23 mycroft ndp->ni_pathlen -= cp - cnp->cn_nameptr;
317 1.23 mycroft cnp->cn_nameptr = cp;
318 1.23 mycroft
319 1.23 mycroft if (dp->v_type != VDIR) {
320 1.23 mycroft error = ENOTDIR;
321 1.23 mycroft goto bad;
322 1.23 mycroft }
323 1.23 mycroft
324 1.23 mycroft /*
325 1.23 mycroft * If we've exhausted the path name, then just return the
326 1.23 mycroft * current node. If the caller requested the parent node (i.e.
327 1.23 mycroft * it's a CREATE, DELETE, or RENAME), and we don't have one
328 1.23 mycroft * (because this is the root directory), then we must fail.
329 1.23 mycroft */
330 1.23 mycroft if (cnp->cn_nameptr[0] == '\0') {
331 1.23 mycroft if (ndp->ni_dvp == NULL && wantparent) {
332 1.23 mycroft error = EISDIR;
333 1.23 mycroft goto bad;
334 1.23 mycroft }
335 1.23 mycroft ndp->ni_vp = dp;
336 1.23 mycroft cnp->cn_flags |= ISLASTCN;
337 1.23 mycroft goto terminal;
338 1.23 mycroft }
339 1.23 mycroft }
340 1.23 mycroft
341 1.10 cgd dirloop:
342 1.10 cgd /*
343 1.10 cgd * Search a new directory.
344 1.10 cgd *
345 1.12 mycroft * The cn_hash value is for use by vfs_cache.
346 1.10 cgd * The last component of the filename is left accessible via
347 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
348 1.10 cgd * the name set the SAVENAME flag. When done, they assume
349 1.10 cgd * responsibility for freeing the pathname buffer.
350 1.10 cgd */
351 1.12 mycroft cnp->cn_consume = 0;
352 1.12 mycroft cnp->cn_hash = 0;
353 1.23 mycroft for (cp = cnp->cn_nameptr; *cp != '\0' && *cp != '/'; cp++)
354 1.12 mycroft cnp->cn_hash += (unsigned char)*cp;
355 1.12 mycroft cnp->cn_namelen = cp - cnp->cn_nameptr;
356 1.12 mycroft if (cnp->cn_namelen > NAME_MAX) {
357 1.10 cgd error = ENAMETOOLONG;
358 1.10 cgd goto bad;
359 1.10 cgd }
360 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
361 1.10 cgd { char c = *cp;
362 1.10 cgd *cp = '\0';
363 1.19 christos printf("{%s}: ", cnp->cn_nameptr);
364 1.10 cgd *cp = c; }
365 1.10 cgd #endif
366 1.12 mycroft ndp->ni_pathlen -= cnp->cn_namelen;
367 1.10 cgd ndp->ni_next = cp;
368 1.23 mycroft /*
369 1.23 mycroft * If this component is followed by a slash, then move the pointer to
370 1.23 mycroft * the next component forward, and remember that this component must be
371 1.23 mycroft * a directory.
372 1.23 mycroft */
373 1.23 mycroft if (*cp == '/') {
374 1.23 mycroft do {
375 1.23 mycroft cp++;
376 1.23 mycroft } while (*cp == '/');
377 1.23 mycroft slashes = cp - ndp->ni_next;
378 1.23 mycroft ndp->ni_pathlen -= slashes;
379 1.23 mycroft ndp->ni_next = cp;
380 1.23 mycroft cnp->cn_flags |= REQUIREDIR;
381 1.23 mycroft } else {
382 1.23 mycroft slashes = 0;
383 1.23 mycroft cnp->cn_flags &= ~REQUIREDIR;
384 1.23 mycroft }
385 1.23 mycroft /*
386 1.23 mycroft * We do special processing on the last component, whether or not it's
387 1.23 mycroft * a directory. Cache all intervening lookups, but not the final one.
388 1.23 mycroft */
389 1.23 mycroft if (*cp == '\0') {
390 1.23 mycroft if (docache)
391 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
392 1.23 mycroft else
393 1.23 mycroft cnp->cn_flags &= ~MAKEENTRY;
394 1.23 mycroft cnp->cn_flags |= ISLASTCN;
395 1.23 mycroft } else {
396 1.23 mycroft cnp->cn_flags |= MAKEENTRY;
397 1.23 mycroft cnp->cn_flags &= ~ISLASTCN;
398 1.23 mycroft }
399 1.12 mycroft if (cnp->cn_namelen == 2 &&
400 1.12 mycroft cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
401 1.12 mycroft cnp->cn_flags |= ISDOTDOT;
402 1.12 mycroft else
403 1.12 mycroft cnp->cn_flags &= ~ISDOTDOT;
404 1.10 cgd
405 1.10 cgd /*
406 1.10 cgd * Handle "..": two special cases.
407 1.10 cgd * 1. If at root directory (e.g. after chroot)
408 1.12 mycroft * or at absolute root directory
409 1.10 cgd * then ignore it so can't get out.
410 1.10 cgd * 2. If this vnode is the root of a mounted
411 1.10 cgd * filesystem, then replace it with the
412 1.10 cgd * vnode which was mounted on so we take the
413 1.10 cgd * .. in the other file system.
414 1.10 cgd */
415 1.12 mycroft if (cnp->cn_flags & ISDOTDOT) {
416 1.10 cgd for (;;) {
417 1.12 mycroft if (dp == ndp->ni_rootdir || dp == rootvnode) {
418 1.10 cgd ndp->ni_dvp = dp;
419 1.10 cgd ndp->ni_vp = dp;
420 1.10 cgd VREF(dp);
421 1.10 cgd goto nextname;
422 1.10 cgd }
423 1.10 cgd if ((dp->v_flag & VROOT) == 0 ||
424 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT))
425 1.10 cgd break;
426 1.10 cgd tdp = dp;
427 1.10 cgd dp = dp->v_mount->mnt_vnodecovered;
428 1.10 cgd vput(tdp);
429 1.10 cgd VREF(dp);
430 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
431 1.10 cgd }
432 1.10 cgd }
433 1.10 cgd
434 1.10 cgd /*
435 1.10 cgd * We now have a segment name to search for, and a directory to search.
436 1.10 cgd */
437 1.12 mycroft unionlookup:
438 1.12 mycroft ndp->ni_dvp = dp;
439 1.26 fvdl ndp->ni_vp = NULL;
440 1.31 wrstuden cnp->cn_flags &= ~PDIRUNLOCK;
441 1.16 christos if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
442 1.10 cgd #ifdef DIAGNOSTIC
443 1.10 cgd if (ndp->ni_vp != NULL)
444 1.10 cgd panic("leaf should be empty");
445 1.10 cgd #endif
446 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
447 1.19 christos printf("not found\n");
448 1.10 cgd #endif
449 1.12 mycroft if ((error == ENOENT) &&
450 1.10 cgd (dp->v_flag & VROOT) &&
451 1.10 cgd (dp->v_mount->mnt_flag & MNT_UNION)) {
452 1.10 cgd tdp = dp;
453 1.10 cgd dp = dp->v_mount->mnt_vnodecovered;
454 1.31 wrstuden if (cnp->cn_flags & PDIRUNLOCK)
455 1.31 wrstuden vrele(tdp);
456 1.31 wrstuden else
457 1.31 wrstuden vput(tdp);
458 1.10 cgd VREF(dp);
459 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
460 1.12 mycroft goto unionlookup;
461 1.10 cgd }
462 1.12 mycroft
463 1.10 cgd if (error != EJUSTRETURN)
464 1.10 cgd goto bad;
465 1.10 cgd /*
466 1.23 mycroft * If this was not the last component, or there were trailing
467 1.23 mycroft * slashes, then the name must exist.
468 1.23 mycroft */
469 1.23 mycroft if (cnp->cn_flags & REQUIREDIR) {
470 1.23 mycroft error = ENOENT;
471 1.23 mycroft goto bad;
472 1.23 mycroft }
473 1.23 mycroft /*
474 1.10 cgd * If creating and at end of pathname, then can consider
475 1.10 cgd * allowing file to be created.
476 1.10 cgd */
477 1.26 fvdl if (rdonly) {
478 1.10 cgd error = EROFS;
479 1.10 cgd goto bad;
480 1.10 cgd }
481 1.10 cgd /*
482 1.10 cgd * We return with ni_vp NULL to indicate that the entry
483 1.10 cgd * doesn't currently exist, leaving a pointer to the
484 1.10 cgd * (possibly locked) directory inode in ndp->ni_dvp.
485 1.10 cgd */
486 1.12 mycroft if (cnp->cn_flags & SAVESTART) {
487 1.10 cgd ndp->ni_startdir = ndp->ni_dvp;
488 1.10 cgd VREF(ndp->ni_startdir);
489 1.10 cgd }
490 1.10 cgd return (0);
491 1.10 cgd }
492 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
493 1.19 christos printf("found\n");
494 1.10 cgd #endif
495 1.10 cgd
496 1.12 mycroft /*
497 1.23 mycroft * Take into account any additional components consumed by the
498 1.23 mycroft * underlying filesystem. This will include any trailing slashes after
499 1.23 mycroft * the last component consumed.
500 1.12 mycroft */
501 1.12 mycroft if (cnp->cn_consume > 0) {
502 1.23 mycroft ndp->ni_pathlen -= cnp->cn_consume - slashes;
503 1.23 mycroft ndp->ni_next += cnp->cn_consume - slashes;
504 1.12 mycroft cnp->cn_consume = 0;
505 1.23 mycroft if (ndp->ni_next[0] == '\0')
506 1.23 mycroft cnp->cn_flags |= ISLASTCN;
507 1.12 mycroft }
508 1.12 mycroft
509 1.10 cgd dp = ndp->ni_vp;
510 1.10 cgd /*
511 1.10 cgd * Check to see if the vnode has been mounted on;
512 1.10 cgd * if so find the root of the mounted file system.
513 1.10 cgd */
514 1.10 cgd while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
515 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT) == 0) {
516 1.26 fvdl if (vfs_busy(mp, 0, 0))
517 1.12 mycroft continue;
518 1.32 wrstuden VOP_UNLOCK(dp, 0);
519 1.26 fvdl error = VFS_ROOT(mp, &tdp);
520 1.26 fvdl vfs_unbusy(mp);
521 1.32 wrstuden if (error) {
522 1.32 wrstuden dpunlocked = 1;
523 1.10 cgd goto bad2;
524 1.32 wrstuden }
525 1.32 wrstuden vrele(dp);
526 1.10 cgd ndp->ni_vp = dp = tdp;
527 1.14 mycroft }
528 1.14 mycroft
529 1.14 mycroft /*
530 1.23 mycroft * Check for symbolic link. Back up over any slashes that we skipped,
531 1.23 mycroft * as we will need them again.
532 1.14 mycroft */
533 1.23 mycroft if ((dp->v_type == VLNK) && (cnp->cn_flags & (FOLLOW|REQUIREDIR))) {
534 1.23 mycroft ndp->ni_pathlen += slashes;
535 1.23 mycroft ndp->ni_next -= slashes;
536 1.14 mycroft cnp->cn_flags |= ISSYMLINK;
537 1.14 mycroft return (0);
538 1.10 cgd }
539 1.10 cgd
540 1.23 mycroft /*
541 1.23 mycroft * Check for directory, if the component was followed by a series of
542 1.23 mycroft * slashes.
543 1.23 mycroft */
544 1.23 mycroft if ((dp->v_type != VDIR) && (cnp->cn_flags & REQUIREDIR)) {
545 1.23 mycroft error = ENOTDIR;
546 1.23 mycroft goto bad2;
547 1.23 mycroft }
548 1.23 mycroft
549 1.10 cgd nextname:
550 1.10 cgd /*
551 1.23 mycroft * Not a symbolic link. If this was not the last component, then
552 1.23 mycroft * continue at the next component, else return.
553 1.10 cgd */
554 1.23 mycroft if (!(cnp->cn_flags & ISLASTCN)) {
555 1.12 mycroft cnp->cn_nameptr = ndp->ni_next;
556 1.10 cgd vrele(ndp->ni_dvp);
557 1.10 cgd goto dirloop;
558 1.10 cgd }
559 1.23 mycroft
560 1.23 mycroft terminal:
561 1.10 cgd /*
562 1.26 fvdl * Disallow directory write attempts on read-only file systems.
563 1.10 cgd */
564 1.26 fvdl if (rdonly &&
565 1.26 fvdl (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
566 1.10 cgd /*
567 1.10 cgd * Disallow directory write attempts on read-only
568 1.10 cgd * file systems.
569 1.10 cgd */
570 1.26 fvdl error = EROFS;
571 1.26 fvdl goto bad2;
572 1.10 cgd }
573 1.23 mycroft if (ndp->ni_dvp != NULL) {
574 1.23 mycroft if (cnp->cn_flags & SAVESTART) {
575 1.23 mycroft ndp->ni_startdir = ndp->ni_dvp;
576 1.23 mycroft VREF(ndp->ni_startdir);
577 1.23 mycroft }
578 1.23 mycroft if (!wantparent)
579 1.23 mycroft vrele(ndp->ni_dvp);
580 1.10 cgd }
581 1.12 mycroft if ((cnp->cn_flags & LOCKLEAF) == 0)
582 1.26 fvdl VOP_UNLOCK(dp, 0);
583 1.10 cgd return (0);
584 1.10 cgd
585 1.10 cgd bad2:
586 1.31 wrstuden if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN) &&
587 1.31 wrstuden ((cnp->cn_flags & PDIRUNLOCK) == 0))
588 1.26 fvdl VOP_UNLOCK(ndp->ni_dvp, 0);
589 1.10 cgd vrele(ndp->ni_dvp);
590 1.10 cgd bad:
591 1.32 wrstuden if (dpunlocked)
592 1.32 wrstuden vrele(dp);
593 1.32 wrstuden else
594 1.32 wrstuden vput(dp);
595 1.10 cgd ndp->ni_vp = NULL;
596 1.12 mycroft return (error);
597 1.12 mycroft }
598 1.12 mycroft
599 1.12 mycroft /*
600 1.12 mycroft * Reacquire a path name component.
601 1.12 mycroft */
602 1.12 mycroft int
603 1.12 mycroft relookup(dvp, vpp, cnp)
604 1.12 mycroft struct vnode *dvp, **vpp;
605 1.12 mycroft struct componentname *cnp;
606 1.12 mycroft {
607 1.26 fvdl struct vnode *dp = 0; /* the directory we are searching */
608 1.12 mycroft int docache; /* == 0 do not cache last component */
609 1.12 mycroft int wantparent; /* 1 => wantparent or lockparent flag */
610 1.12 mycroft int rdonly; /* lookup read-only flag bit */
611 1.12 mycroft int error = 0;
612 1.12 mycroft #ifdef NAMEI_DIAGNOSTIC
613 1.12 mycroft int newhash; /* DEBUG: check name hash */
614 1.12 mycroft char *cp; /* DEBUG: check name ptr/len */
615 1.12 mycroft #endif
616 1.12 mycroft
617 1.12 mycroft /*
618 1.12 mycroft * Setup: break out flag bits into variables.
619 1.12 mycroft */
620 1.12 mycroft wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
621 1.12 mycroft docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
622 1.12 mycroft if (cnp->cn_nameiop == DELETE ||
623 1.12 mycroft (wantparent && cnp->cn_nameiop != CREATE))
624 1.12 mycroft docache = 0;
625 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
626 1.12 mycroft cnp->cn_flags &= ~ISSYMLINK;
627 1.12 mycroft dp = dvp;
628 1.26 fvdl vn_lock(dp, LK_EXCLUSIVE | LK_RETRY);
629 1.12 mycroft
630 1.12 mycroft /* dirloop: */
631 1.12 mycroft /*
632 1.12 mycroft * Search a new directory.
633 1.12 mycroft *
634 1.12 mycroft * The cn_hash value is for use by vfs_cache.
635 1.12 mycroft * The last component of the filename is left accessible via
636 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
637 1.12 mycroft * the name set the SAVENAME flag. When done, they assume
638 1.12 mycroft * responsibility for freeing the pathname buffer.
639 1.12 mycroft */
640 1.12 mycroft #ifdef NAMEI_DIAGNOSTIC
641 1.12 mycroft for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
642 1.12 mycroft newhash += (unsigned char)*cp;
643 1.12 mycroft if (newhash != cnp->cn_hash)
644 1.12 mycroft panic("relookup: bad hash");
645 1.12 mycroft if (cnp->cn_namelen != cp - cnp->cn_nameptr)
646 1.12 mycroft panic ("relookup: bad len");
647 1.12 mycroft if (*cp != 0)
648 1.12 mycroft panic("relookup: not last component");
649 1.19 christos printf("{%s}: ", cnp->cn_nameptr);
650 1.12 mycroft #endif
651 1.12 mycroft
652 1.12 mycroft /*
653 1.12 mycroft * Check for degenerate name (e.g. / or "")
654 1.12 mycroft * which is a way of talking about a directory,
655 1.12 mycroft * e.g. like "/." or ".".
656 1.12 mycroft */
657 1.23 mycroft if (cnp->cn_nameptr[0] == '\0')
658 1.23 mycroft panic("relookup: null name");
659 1.12 mycroft
660 1.12 mycroft if (cnp->cn_flags & ISDOTDOT)
661 1.12 mycroft panic ("relookup: lookup on dot-dot");
662 1.12 mycroft
663 1.12 mycroft /*
664 1.12 mycroft * We now have a segment name to search for, and a directory to search.
665 1.12 mycroft */
666 1.16 christos if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
667 1.12 mycroft #ifdef DIAGNOSTIC
668 1.12 mycroft if (*vpp != NULL)
669 1.12 mycroft panic("leaf should be empty");
670 1.12 mycroft #endif
671 1.12 mycroft if (error != EJUSTRETURN)
672 1.12 mycroft goto bad;
673 1.12 mycroft /*
674 1.12 mycroft * If creating and at end of pathname, then can consider
675 1.12 mycroft * allowing file to be created.
676 1.12 mycroft */
677 1.26 fvdl if (rdonly) {
678 1.12 mycroft error = EROFS;
679 1.12 mycroft goto bad;
680 1.12 mycroft }
681 1.12 mycroft /* ASSERT(dvp == ndp->ni_startdir) */
682 1.12 mycroft if (cnp->cn_flags & SAVESTART)
683 1.12 mycroft VREF(dvp);
684 1.12 mycroft /*
685 1.12 mycroft * We return with ni_vp NULL to indicate that the entry
686 1.12 mycroft * doesn't currently exist, leaving a pointer to the
687 1.12 mycroft * (possibly locked) directory inode in ndp->ni_dvp.
688 1.12 mycroft */
689 1.12 mycroft return (0);
690 1.12 mycroft }
691 1.12 mycroft dp = *vpp;
692 1.12 mycroft
693 1.12 mycroft #ifdef DIAGNOSTIC
694 1.12 mycroft /*
695 1.12 mycroft * Check for symbolic link
696 1.12 mycroft */
697 1.12 mycroft if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
698 1.12 mycroft panic ("relookup: symlink found.\n");
699 1.12 mycroft #endif
700 1.12 mycroft
701 1.12 mycroft /*
702 1.12 mycroft * Check for read-only file systems.
703 1.12 mycroft */
704 1.26 fvdl if (rdonly &&
705 1.26 fvdl (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
706 1.26 fvdl error = EROFS;
707 1.26 fvdl goto bad2;
708 1.12 mycroft }
709 1.12 mycroft /* ASSERT(dvp == ndp->ni_startdir) */
710 1.12 mycroft if (cnp->cn_flags & SAVESTART)
711 1.12 mycroft VREF(dvp);
712 1.12 mycroft if (!wantparent)
713 1.12 mycroft vrele(dvp);
714 1.12 mycroft if ((cnp->cn_flags & LOCKLEAF) == 0)
715 1.26 fvdl VOP_UNLOCK(dp, 0);
716 1.12 mycroft return (0);
717 1.12 mycroft
718 1.12 mycroft bad2:
719 1.12 mycroft if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
720 1.26 fvdl VOP_UNLOCK(dvp, 0);
721 1.12 mycroft vrele(dvp);
722 1.12 mycroft bad:
723 1.12 mycroft vput(dp);
724 1.12 mycroft *vpp = NULL;
725 1.10 cgd return (error);
726 1.10 cgd }
727