vfs_lookup.c revision 1.16 1 1.16 christos /* $NetBSD: vfs_lookup.c,v 1.16 1996/02/04 02:18:25 christos 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.14 mycroft * @(#)vfs_lookup.c 8.6 (Berkeley) 11/21/94
41 1.10 cgd */
42 1.10 cgd
43 1.10 cgd #include <sys/param.h>
44 1.15 cgd #include <sys/systm.h>
45 1.10 cgd #include <sys/syslimits.h>
46 1.10 cgd #include <sys/time.h>
47 1.10 cgd #include <sys/namei.h>
48 1.10 cgd #include <sys/vnode.h>
49 1.10 cgd #include <sys/mount.h>
50 1.10 cgd #include <sys/errno.h>
51 1.10 cgd #include <sys/malloc.h>
52 1.10 cgd #include <sys/filedesc.h>
53 1.10 cgd #include <sys/proc.h>
54 1.12 mycroft
55 1.10 cgd #ifdef KTRACE
56 1.10 cgd #include <sys/ktrace.h>
57 1.10 cgd #endif
58 1.10 cgd
59 1.16 christos #include <kern/kern_extern.h>
60 1.16 christos
61 1.10 cgd /*
62 1.10 cgd * Convert a pathname into a pointer to a locked inode.
63 1.10 cgd *
64 1.10 cgd * The FOLLOW flag is set when symbolic links are to be followed
65 1.10 cgd * when they occur at the end of the name translation process.
66 1.10 cgd * Symbolic links are always followed for all other pathname
67 1.10 cgd * components other than the last.
68 1.10 cgd *
69 1.10 cgd * The segflg defines whether the name is to be copied from user
70 1.10 cgd * space or kernel space.
71 1.10 cgd *
72 1.10 cgd * Overall outline of namei:
73 1.10 cgd *
74 1.10 cgd * copy in name
75 1.10 cgd * get starting directory
76 1.10 cgd * while (!done && !error) {
77 1.10 cgd * call lookup to search path.
78 1.10 cgd * if symbolic link, massage name in buffer and continue
79 1.10 cgd * }
80 1.10 cgd */
81 1.12 mycroft int
82 1.12 mycroft namei(ndp)
83 1.10 cgd register struct nameidata *ndp;
84 1.10 cgd {
85 1.10 cgd register struct filedesc *fdp; /* pointer to file descriptor state */
86 1.10 cgd register char *cp; /* pointer into pathname argument */
87 1.10 cgd register struct vnode *dp; /* the directory we are searching */
88 1.10 cgd struct iovec aiov; /* uio for reading symbolic links */
89 1.10 cgd struct uio auio;
90 1.10 cgd int error, linklen;
91 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
92 1.10 cgd
93 1.12 mycroft ndp->ni_cnd.cn_cred = ndp->ni_cnd.cn_proc->p_ucred;
94 1.12 mycroft #ifdef DIAGNOSTIC
95 1.12 mycroft if (!cnp->cn_cred || !cnp->cn_proc)
96 1.12 mycroft panic ("namei: bad cred/proc");
97 1.12 mycroft if (cnp->cn_nameiop & (~OPMASK))
98 1.12 mycroft panic ("namei: nameiop contaminated with flags");
99 1.12 mycroft if (cnp->cn_flags & OPMASK)
100 1.12 mycroft panic ("namei: flags contaminated with nameiops");
101 1.12 mycroft #endif
102 1.12 mycroft fdp = cnp->cn_proc->p_fd;
103 1.10 cgd
104 1.10 cgd /*
105 1.10 cgd * Get a buffer for the name to be translated, and copy the
106 1.10 cgd * name into the buffer.
107 1.10 cgd */
108 1.12 mycroft if ((cnp->cn_flags & HASBUF) == 0)
109 1.12 mycroft MALLOC(cnp->cn_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
110 1.10 cgd if (ndp->ni_segflg == UIO_SYSSPACE)
111 1.12 mycroft error = copystr(ndp->ni_dirp, cnp->cn_pnbuf,
112 1.10 cgd MAXPATHLEN, &ndp->ni_pathlen);
113 1.10 cgd else
114 1.12 mycroft error = copyinstr(ndp->ni_dirp, cnp->cn_pnbuf,
115 1.10 cgd MAXPATHLEN, &ndp->ni_pathlen);
116 1.10 cgd if (error) {
117 1.12 mycroft free(cnp->cn_pnbuf, M_NAMEI);
118 1.10 cgd ndp->ni_vp = NULL;
119 1.10 cgd return (error);
120 1.10 cgd }
121 1.10 cgd ndp->ni_loopcnt = 0;
122 1.10 cgd #ifdef KTRACE
123 1.12 mycroft if (KTRPOINT(cnp->cn_proc, KTR_NAMEI))
124 1.12 mycroft ktrnamei(cnp->cn_proc->p_tracep, cnp->cn_pnbuf);
125 1.10 cgd #endif
126 1.10 cgd
127 1.10 cgd /*
128 1.10 cgd * Get starting point for the translation.
129 1.10 cgd */
130 1.10 cgd if ((ndp->ni_rootdir = fdp->fd_rdir) == NULL)
131 1.11 cgd ndp->ni_rootdir = rootvnode;
132 1.10 cgd dp = fdp->fd_cdir;
133 1.10 cgd VREF(dp);
134 1.10 cgd for (;;) {
135 1.10 cgd /*
136 1.10 cgd * Check if root directory should replace current directory.
137 1.10 cgd * Done at start of translation and after symbolic link.
138 1.10 cgd */
139 1.12 mycroft cnp->cn_nameptr = cnp->cn_pnbuf;
140 1.12 mycroft if (*(cnp->cn_nameptr) == '/') {
141 1.10 cgd vrele(dp);
142 1.12 mycroft while (*(cnp->cn_nameptr) == '/') {
143 1.12 mycroft cnp->cn_nameptr++;
144 1.10 cgd ndp->ni_pathlen--;
145 1.10 cgd }
146 1.10 cgd dp = ndp->ni_rootdir;
147 1.10 cgd VREF(dp);
148 1.10 cgd }
149 1.10 cgd ndp->ni_startdir = dp;
150 1.16 christos if ((error = lookup(ndp)) != 0) {
151 1.12 mycroft FREE(cnp->cn_pnbuf, M_NAMEI);
152 1.10 cgd return (error);
153 1.10 cgd }
154 1.10 cgd /*
155 1.10 cgd * Check for symbolic link
156 1.10 cgd */
157 1.12 mycroft if ((cnp->cn_flags & ISSYMLINK) == 0) {
158 1.12 mycroft if ((cnp->cn_flags & (SAVENAME | SAVESTART)) == 0)
159 1.12 mycroft FREE(cnp->cn_pnbuf, M_NAMEI);
160 1.10 cgd else
161 1.12 mycroft cnp->cn_flags |= HASBUF;
162 1.10 cgd return (0);
163 1.10 cgd }
164 1.12 mycroft if ((cnp->cn_flags & LOCKPARENT) && ndp->ni_pathlen == 1)
165 1.10 cgd VOP_UNLOCK(ndp->ni_dvp);
166 1.10 cgd if (ndp->ni_loopcnt++ >= MAXSYMLINKS) {
167 1.10 cgd error = ELOOP;
168 1.10 cgd break;
169 1.10 cgd }
170 1.10 cgd if (ndp->ni_pathlen > 1)
171 1.10 cgd MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
172 1.10 cgd else
173 1.12 mycroft cp = cnp->cn_pnbuf;
174 1.10 cgd aiov.iov_base = cp;
175 1.10 cgd aiov.iov_len = MAXPATHLEN;
176 1.10 cgd auio.uio_iov = &aiov;
177 1.10 cgd auio.uio_iovcnt = 1;
178 1.10 cgd auio.uio_offset = 0;
179 1.10 cgd auio.uio_rw = UIO_READ;
180 1.10 cgd auio.uio_segflg = UIO_SYSSPACE;
181 1.10 cgd auio.uio_procp = (struct proc *)0;
182 1.10 cgd auio.uio_resid = MAXPATHLEN;
183 1.16 christos error = VOP_READLINK(ndp->ni_vp, &auio, cnp->cn_cred);
184 1.16 christos if (error) {
185 1.10 cgd if (ndp->ni_pathlen > 1)
186 1.10 cgd free(cp, M_NAMEI);
187 1.10 cgd break;
188 1.10 cgd }
189 1.10 cgd linklen = MAXPATHLEN - auio.uio_resid;
190 1.10 cgd if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
191 1.10 cgd if (ndp->ni_pathlen > 1)
192 1.10 cgd free(cp, M_NAMEI);
193 1.10 cgd error = ENAMETOOLONG;
194 1.10 cgd break;
195 1.10 cgd }
196 1.10 cgd if (ndp->ni_pathlen > 1) {
197 1.10 cgd bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
198 1.12 mycroft FREE(cnp->cn_pnbuf, M_NAMEI);
199 1.12 mycroft cnp->cn_pnbuf = cp;
200 1.10 cgd } else
201 1.12 mycroft cnp->cn_pnbuf[linklen] = '\0';
202 1.10 cgd ndp->ni_pathlen += linklen;
203 1.10 cgd vput(ndp->ni_vp);
204 1.10 cgd dp = ndp->ni_dvp;
205 1.10 cgd }
206 1.12 mycroft FREE(cnp->cn_pnbuf, M_NAMEI);
207 1.10 cgd vrele(ndp->ni_dvp);
208 1.10 cgd vput(ndp->ni_vp);
209 1.10 cgd ndp->ni_vp = NULL;
210 1.10 cgd return (error);
211 1.10 cgd }
212 1.10 cgd
213 1.10 cgd /*
214 1.10 cgd * Search a pathname.
215 1.10 cgd * This is a very central and rather complicated routine.
216 1.10 cgd *
217 1.10 cgd * The pathname is pointed to by ni_ptr and is of length ni_pathlen.
218 1.10 cgd * The starting directory is taken from ni_startdir. The pathname is
219 1.10 cgd * descended until done, or a symbolic link is encountered. The variable
220 1.10 cgd * ni_more is clear if the path is completed; it is set to one if a
221 1.10 cgd * symbolic link needing interpretation is encountered.
222 1.10 cgd *
223 1.10 cgd * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
224 1.10 cgd * whether the name is to be looked up, created, renamed, or deleted.
225 1.10 cgd * When CREATE, RENAME, or DELETE is specified, information usable in
226 1.10 cgd * creating, renaming, or deleting a directory entry may be calculated.
227 1.10 cgd * If flag has LOCKPARENT or'ed into it, the parent directory is returned
228 1.10 cgd * locked. If flag has WANTPARENT or'ed into it, the parent directory is
229 1.10 cgd * returned unlocked. Otherwise the parent directory is not returned. If
230 1.10 cgd * the target of the pathname exists and LOCKLEAF is or'ed into the flag
231 1.10 cgd * the target is returned locked, otherwise it is returned unlocked.
232 1.10 cgd * When creating or renaming and LOCKPARENT is specified, the target may not
233 1.10 cgd * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
234 1.10 cgd *
235 1.10 cgd * Overall outline of lookup:
236 1.10 cgd *
237 1.10 cgd * dirloop:
238 1.10 cgd * identify next component of name at ndp->ni_ptr
239 1.10 cgd * handle degenerate case where name is null string
240 1.10 cgd * if .. and crossing mount points and on mounted filesys, find parent
241 1.10 cgd * call VOP_LOOKUP routine for next component name
242 1.10 cgd * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
243 1.10 cgd * component vnode returned in ni_vp (if it exists), locked.
244 1.10 cgd * if result vnode is mounted on and crossing mount points,
245 1.10 cgd * find mounted on vnode
246 1.10 cgd * if more components of name, do next level at dirloop
247 1.10 cgd * return the answer in ni_vp, locked if LOCKLEAF set
248 1.10 cgd * if LOCKPARENT set, return locked parent in ni_dvp
249 1.10 cgd * if WANTPARENT set, return unlocked parent in ni_dvp
250 1.10 cgd */
251 1.12 mycroft int
252 1.12 mycroft lookup(ndp)
253 1.10 cgd register struct nameidata *ndp;
254 1.10 cgd {
255 1.10 cgd register char *cp; /* pointer into pathname argument */
256 1.10 cgd register struct vnode *dp = 0; /* the directory we are searching */
257 1.10 cgd struct vnode *tdp; /* saved dp */
258 1.10 cgd struct mount *mp; /* mount table entry */
259 1.10 cgd int docache; /* == 0 do not cache last component */
260 1.10 cgd int wantparent; /* 1 => wantparent or lockparent flag */
261 1.12 mycroft int rdonly; /* lookup read-only flag bit */
262 1.10 cgd int error = 0;
263 1.12 mycroft struct componentname *cnp = &ndp->ni_cnd;
264 1.10 cgd
265 1.10 cgd /*
266 1.10 cgd * Setup: break out flag bits into variables.
267 1.10 cgd */
268 1.12 mycroft wantparent = cnp->cn_flags & (LOCKPARENT | WANTPARENT);
269 1.12 mycroft docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
270 1.12 mycroft if (cnp->cn_nameiop == DELETE ||
271 1.12 mycroft (wantparent && cnp->cn_nameiop != CREATE))
272 1.10 cgd docache = 0;
273 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
274 1.10 cgd ndp->ni_dvp = NULL;
275 1.12 mycroft cnp->cn_flags &= ~ISSYMLINK;
276 1.10 cgd dp = ndp->ni_startdir;
277 1.10 cgd ndp->ni_startdir = NULLVP;
278 1.10 cgd VOP_LOCK(dp);
279 1.10 cgd
280 1.10 cgd dirloop:
281 1.10 cgd /*
282 1.10 cgd * Search a new directory.
283 1.10 cgd *
284 1.12 mycroft * The cn_hash value is for use by vfs_cache.
285 1.10 cgd * The last component of the filename is left accessible via
286 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
287 1.10 cgd * the name set the SAVENAME flag. When done, they assume
288 1.10 cgd * responsibility for freeing the pathname buffer.
289 1.10 cgd */
290 1.12 mycroft cnp->cn_consume = 0;
291 1.12 mycroft cnp->cn_hash = 0;
292 1.12 mycroft for (cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
293 1.12 mycroft cnp->cn_hash += (unsigned char)*cp;
294 1.12 mycroft cnp->cn_namelen = cp - cnp->cn_nameptr;
295 1.12 mycroft if (cnp->cn_namelen > NAME_MAX) {
296 1.10 cgd error = ENAMETOOLONG;
297 1.10 cgd goto bad;
298 1.10 cgd }
299 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
300 1.10 cgd { char c = *cp;
301 1.10 cgd *cp = '\0';
302 1.12 mycroft printf("{%s}: ", cnp->cn_nameptr);
303 1.10 cgd *cp = c; }
304 1.10 cgd #endif
305 1.12 mycroft ndp->ni_pathlen -= cnp->cn_namelen;
306 1.10 cgd ndp->ni_next = cp;
307 1.12 mycroft cnp->cn_flags |= MAKEENTRY;
308 1.10 cgd if (*cp == '\0' && docache == 0)
309 1.12 mycroft cnp->cn_flags &= ~MAKEENTRY;
310 1.12 mycroft if (cnp->cn_namelen == 2 &&
311 1.12 mycroft cnp->cn_nameptr[1] == '.' && cnp->cn_nameptr[0] == '.')
312 1.12 mycroft cnp->cn_flags |= ISDOTDOT;
313 1.12 mycroft else
314 1.12 mycroft cnp->cn_flags &= ~ISDOTDOT;
315 1.12 mycroft if (*ndp->ni_next == 0)
316 1.12 mycroft cnp->cn_flags |= ISLASTCN;
317 1.12 mycroft else
318 1.12 mycroft cnp->cn_flags &= ~ISLASTCN;
319 1.12 mycroft
320 1.10 cgd
321 1.10 cgd /*
322 1.10 cgd * Check for degenerate name (e.g. / or "")
323 1.10 cgd * which is a way of talking about a directory,
324 1.10 cgd * e.g. like "/." or ".".
325 1.10 cgd */
326 1.12 mycroft if (cnp->cn_nameptr[0] == '\0') {
327 1.12 mycroft if (dp->v_type != VDIR) {
328 1.12 mycroft error = ENOTDIR;
329 1.12 mycroft goto bad;
330 1.12 mycroft }
331 1.12 mycroft if (cnp->cn_nameiop != LOOKUP) {
332 1.10 cgd error = EISDIR;
333 1.10 cgd goto bad;
334 1.10 cgd }
335 1.12 mycroft if (wantparent) {
336 1.12 mycroft ndp->ni_dvp = dp;
337 1.12 mycroft VREF(dp);
338 1.10 cgd }
339 1.12 mycroft ndp->ni_vp = dp;
340 1.12 mycroft if (!(cnp->cn_flags & (LOCKPARENT | LOCKLEAF)))
341 1.10 cgd VOP_UNLOCK(dp);
342 1.12 mycroft if (cnp->cn_flags & SAVESTART)
343 1.10 cgd panic("lookup: SAVESTART");
344 1.10 cgd return (0);
345 1.10 cgd }
346 1.10 cgd
347 1.10 cgd /*
348 1.10 cgd * Handle "..": two special cases.
349 1.10 cgd * 1. If at root directory (e.g. after chroot)
350 1.12 mycroft * or at absolute root directory
351 1.10 cgd * then ignore it so can't get out.
352 1.10 cgd * 2. If this vnode is the root of a mounted
353 1.10 cgd * filesystem, then replace it with the
354 1.10 cgd * vnode which was mounted on so we take the
355 1.10 cgd * .. in the other file system.
356 1.10 cgd */
357 1.12 mycroft if (cnp->cn_flags & ISDOTDOT) {
358 1.10 cgd for (;;) {
359 1.12 mycroft if (dp == ndp->ni_rootdir || dp == rootvnode) {
360 1.10 cgd ndp->ni_dvp = dp;
361 1.10 cgd ndp->ni_vp = dp;
362 1.10 cgd VREF(dp);
363 1.10 cgd goto nextname;
364 1.10 cgd }
365 1.10 cgd if ((dp->v_flag & VROOT) == 0 ||
366 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT))
367 1.10 cgd break;
368 1.10 cgd tdp = dp;
369 1.10 cgd dp = dp->v_mount->mnt_vnodecovered;
370 1.10 cgd vput(tdp);
371 1.10 cgd VREF(dp);
372 1.10 cgd VOP_LOCK(dp);
373 1.10 cgd }
374 1.10 cgd }
375 1.10 cgd
376 1.10 cgd /*
377 1.10 cgd * We now have a segment name to search for, and a directory to search.
378 1.10 cgd */
379 1.12 mycroft unionlookup:
380 1.12 mycroft ndp->ni_dvp = dp;
381 1.16 christos if ((error = VOP_LOOKUP(dp, &ndp->ni_vp, cnp)) != 0) {
382 1.10 cgd #ifdef DIAGNOSTIC
383 1.10 cgd if (ndp->ni_vp != NULL)
384 1.10 cgd panic("leaf should be empty");
385 1.10 cgd #endif
386 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
387 1.10 cgd printf("not found\n");
388 1.10 cgd #endif
389 1.12 mycroft if ((error == ENOENT) &&
390 1.10 cgd (dp->v_flag & VROOT) &&
391 1.10 cgd (dp->v_mount->mnt_flag & MNT_UNION)) {
392 1.10 cgd tdp = dp;
393 1.10 cgd dp = dp->v_mount->mnt_vnodecovered;
394 1.10 cgd vput(tdp);
395 1.10 cgd VREF(dp);
396 1.10 cgd VOP_LOCK(dp);
397 1.12 mycroft goto unionlookup;
398 1.10 cgd }
399 1.12 mycroft
400 1.10 cgd if (error != EJUSTRETURN)
401 1.10 cgd goto bad;
402 1.10 cgd /*
403 1.10 cgd * If creating and at end of pathname, then can consider
404 1.10 cgd * allowing file to be created.
405 1.10 cgd */
406 1.12 mycroft if (rdonly || (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY)) {
407 1.10 cgd error = EROFS;
408 1.10 cgd goto bad;
409 1.10 cgd }
410 1.10 cgd /*
411 1.10 cgd * We return with ni_vp NULL to indicate that the entry
412 1.10 cgd * doesn't currently exist, leaving a pointer to the
413 1.10 cgd * (possibly locked) directory inode in ndp->ni_dvp.
414 1.10 cgd */
415 1.12 mycroft if (cnp->cn_flags & SAVESTART) {
416 1.10 cgd ndp->ni_startdir = ndp->ni_dvp;
417 1.10 cgd VREF(ndp->ni_startdir);
418 1.10 cgd }
419 1.10 cgd return (0);
420 1.10 cgd }
421 1.10 cgd #ifdef NAMEI_DIAGNOSTIC
422 1.10 cgd printf("found\n");
423 1.10 cgd #endif
424 1.10 cgd
425 1.12 mycroft /*
426 1.12 mycroft * Take into account any additional components consumed by
427 1.12 mycroft * the underlying filesystem.
428 1.12 mycroft */
429 1.12 mycroft if (cnp->cn_consume > 0) {
430 1.12 mycroft cnp->cn_nameptr += cnp->cn_consume;
431 1.12 mycroft ndp->ni_next += cnp->cn_consume;
432 1.12 mycroft ndp->ni_pathlen -= cnp->cn_consume;
433 1.12 mycroft cnp->cn_consume = 0;
434 1.12 mycroft }
435 1.12 mycroft
436 1.10 cgd dp = ndp->ni_vp;
437 1.10 cgd /*
438 1.10 cgd * Check to see if the vnode has been mounted on;
439 1.10 cgd * if so find the root of the mounted file system.
440 1.10 cgd */
441 1.10 cgd while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
442 1.12 mycroft (cnp->cn_flags & NOCROSSMOUNT) == 0) {
443 1.12 mycroft if (mp->mnt_flag & MNT_MLOCK) {
444 1.10 cgd mp->mnt_flag |= MNT_MWAIT;
445 1.12 mycroft sleep((caddr_t)mp, PVFS);
446 1.12 mycroft continue;
447 1.10 cgd }
448 1.16 christos if ((error = VFS_ROOT(dp->v_mountedhere, &tdp)) != 0)
449 1.10 cgd goto bad2;
450 1.10 cgd vput(dp);
451 1.10 cgd ndp->ni_vp = dp = tdp;
452 1.14 mycroft }
453 1.14 mycroft
454 1.14 mycroft /*
455 1.14 mycroft * Check for symbolic link
456 1.14 mycroft */
457 1.14 mycroft if ((dp->v_type == VLNK) &&
458 1.14 mycroft ((cnp->cn_flags & FOLLOW) || *ndp->ni_next == '/')) {
459 1.14 mycroft cnp->cn_flags |= ISSYMLINK;
460 1.14 mycroft return (0);
461 1.10 cgd }
462 1.10 cgd
463 1.10 cgd nextname:
464 1.10 cgd /*
465 1.10 cgd * Not a symbolic link. If more pathname,
466 1.10 cgd * continue at next component, else return.
467 1.10 cgd */
468 1.10 cgd if (*ndp->ni_next == '/') {
469 1.12 mycroft cnp->cn_nameptr = ndp->ni_next;
470 1.12 mycroft while (*cnp->cn_nameptr == '/') {
471 1.12 mycroft cnp->cn_nameptr++;
472 1.10 cgd ndp->ni_pathlen--;
473 1.10 cgd }
474 1.10 cgd vrele(ndp->ni_dvp);
475 1.10 cgd goto dirloop;
476 1.10 cgd }
477 1.10 cgd /*
478 1.10 cgd * Check for read-only file systems.
479 1.10 cgd */
480 1.12 mycroft if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
481 1.10 cgd /*
482 1.10 cgd * Disallow directory write attempts on read-only
483 1.10 cgd * file systems.
484 1.10 cgd */
485 1.12 mycroft if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
486 1.12 mycroft (wantparent &&
487 1.12 mycroft (ndp->ni_dvp->v_mount->mnt_flag & MNT_RDONLY))) {
488 1.10 cgd error = EROFS;
489 1.10 cgd goto bad2;
490 1.10 cgd }
491 1.10 cgd }
492 1.12 mycroft if (cnp->cn_flags & SAVESTART) {
493 1.10 cgd ndp->ni_startdir = ndp->ni_dvp;
494 1.10 cgd VREF(ndp->ni_startdir);
495 1.10 cgd }
496 1.10 cgd if (!wantparent)
497 1.10 cgd vrele(ndp->ni_dvp);
498 1.12 mycroft if ((cnp->cn_flags & LOCKLEAF) == 0)
499 1.10 cgd VOP_UNLOCK(dp);
500 1.10 cgd return (0);
501 1.10 cgd
502 1.10 cgd bad2:
503 1.12 mycroft if ((cnp->cn_flags & LOCKPARENT) && *ndp->ni_next == '\0')
504 1.10 cgd VOP_UNLOCK(ndp->ni_dvp);
505 1.10 cgd vrele(ndp->ni_dvp);
506 1.10 cgd bad:
507 1.10 cgd vput(dp);
508 1.10 cgd ndp->ni_vp = NULL;
509 1.12 mycroft return (error);
510 1.12 mycroft }
511 1.12 mycroft
512 1.12 mycroft /*
513 1.12 mycroft * Reacquire a path name component.
514 1.12 mycroft */
515 1.12 mycroft int
516 1.12 mycroft relookup(dvp, vpp, cnp)
517 1.12 mycroft struct vnode *dvp, **vpp;
518 1.12 mycroft struct componentname *cnp;
519 1.12 mycroft {
520 1.12 mycroft register struct vnode *dp = 0; /* the directory we are searching */
521 1.12 mycroft int docache; /* == 0 do not cache last component */
522 1.12 mycroft int wantparent; /* 1 => wantparent or lockparent flag */
523 1.12 mycroft int rdonly; /* lookup read-only flag bit */
524 1.12 mycroft int error = 0;
525 1.12 mycroft #ifdef NAMEI_DIAGNOSTIC
526 1.12 mycroft int newhash; /* DEBUG: check name hash */
527 1.12 mycroft char *cp; /* DEBUG: check name ptr/len */
528 1.12 mycroft #endif
529 1.12 mycroft
530 1.12 mycroft /*
531 1.12 mycroft * Setup: break out flag bits into variables.
532 1.12 mycroft */
533 1.12 mycroft wantparent = cnp->cn_flags & (LOCKPARENT|WANTPARENT);
534 1.12 mycroft docache = (cnp->cn_flags & NOCACHE) ^ NOCACHE;
535 1.12 mycroft if (cnp->cn_nameiop == DELETE ||
536 1.12 mycroft (wantparent && cnp->cn_nameiop != CREATE))
537 1.12 mycroft docache = 0;
538 1.12 mycroft rdonly = cnp->cn_flags & RDONLY;
539 1.12 mycroft cnp->cn_flags &= ~ISSYMLINK;
540 1.12 mycroft dp = dvp;
541 1.12 mycroft VOP_LOCK(dp);
542 1.12 mycroft
543 1.12 mycroft /* dirloop: */
544 1.12 mycroft /*
545 1.12 mycroft * Search a new directory.
546 1.12 mycroft *
547 1.12 mycroft * The cn_hash value is for use by vfs_cache.
548 1.12 mycroft * The last component of the filename is left accessible via
549 1.12 mycroft * cnp->cn_nameptr for callers that need the name. Callers needing
550 1.12 mycroft * the name set the SAVENAME flag. When done, they assume
551 1.12 mycroft * responsibility for freeing the pathname buffer.
552 1.12 mycroft */
553 1.12 mycroft #ifdef NAMEI_DIAGNOSTIC
554 1.12 mycroft for (newhash = 0, cp = cnp->cn_nameptr; *cp != 0 && *cp != '/'; cp++)
555 1.12 mycroft newhash += (unsigned char)*cp;
556 1.12 mycroft if (newhash != cnp->cn_hash)
557 1.12 mycroft panic("relookup: bad hash");
558 1.12 mycroft if (cnp->cn_namelen != cp - cnp->cn_nameptr)
559 1.12 mycroft panic ("relookup: bad len");
560 1.12 mycroft if (*cp != 0)
561 1.12 mycroft panic("relookup: not last component");
562 1.12 mycroft printf("{%s}: ", cnp->cn_nameptr);
563 1.12 mycroft #endif
564 1.12 mycroft
565 1.12 mycroft /*
566 1.12 mycroft * Check for degenerate name (e.g. / or "")
567 1.12 mycroft * which is a way of talking about a directory,
568 1.12 mycroft * e.g. like "/." or ".".
569 1.12 mycroft */
570 1.12 mycroft if (cnp->cn_nameptr[0] == '\0') {
571 1.12 mycroft if (dp->v_type != VDIR) {
572 1.12 mycroft error = ENOTDIR;
573 1.12 mycroft goto bad;
574 1.12 mycroft }
575 1.12 mycroft if (cnp->cn_nameiop != LOOKUP || wantparent) {
576 1.12 mycroft error = EISDIR;
577 1.12 mycroft goto bad;
578 1.12 mycroft }
579 1.12 mycroft if (!(cnp->cn_flags & LOCKLEAF))
580 1.12 mycroft VOP_UNLOCK(dp);
581 1.12 mycroft *vpp = dp;
582 1.12 mycroft if (cnp->cn_flags & SAVESTART)
583 1.12 mycroft panic("lookup: SAVESTART");
584 1.12 mycroft return (0);
585 1.12 mycroft }
586 1.12 mycroft
587 1.12 mycroft if (cnp->cn_flags & ISDOTDOT)
588 1.12 mycroft panic ("relookup: lookup on dot-dot");
589 1.12 mycroft
590 1.12 mycroft /*
591 1.12 mycroft * We now have a segment name to search for, and a directory to search.
592 1.12 mycroft */
593 1.16 christos if ((error = VOP_LOOKUP(dp, vpp, cnp)) != 0) {
594 1.12 mycroft #ifdef DIAGNOSTIC
595 1.12 mycroft if (*vpp != NULL)
596 1.12 mycroft panic("leaf should be empty");
597 1.12 mycroft #endif
598 1.12 mycroft if (error != EJUSTRETURN)
599 1.12 mycroft goto bad;
600 1.12 mycroft /*
601 1.12 mycroft * If creating and at end of pathname, then can consider
602 1.12 mycroft * allowing file to be created.
603 1.12 mycroft */
604 1.12 mycroft if (rdonly || (dvp->v_mount->mnt_flag & MNT_RDONLY)) {
605 1.12 mycroft error = EROFS;
606 1.12 mycroft goto bad;
607 1.12 mycroft }
608 1.12 mycroft /* ASSERT(dvp == ndp->ni_startdir) */
609 1.12 mycroft if (cnp->cn_flags & SAVESTART)
610 1.12 mycroft VREF(dvp);
611 1.12 mycroft /*
612 1.12 mycroft * We return with ni_vp NULL to indicate that the entry
613 1.12 mycroft * doesn't currently exist, leaving a pointer to the
614 1.12 mycroft * (possibly locked) directory inode in ndp->ni_dvp.
615 1.12 mycroft */
616 1.12 mycroft return (0);
617 1.12 mycroft }
618 1.12 mycroft dp = *vpp;
619 1.12 mycroft
620 1.12 mycroft #ifdef DIAGNOSTIC
621 1.12 mycroft /*
622 1.12 mycroft * Check for symbolic link
623 1.12 mycroft */
624 1.12 mycroft if (dp->v_type == VLNK && (cnp->cn_flags & FOLLOW))
625 1.12 mycroft panic ("relookup: symlink found.\n");
626 1.12 mycroft #endif
627 1.12 mycroft
628 1.12 mycroft /*
629 1.12 mycroft * Check for read-only file systems.
630 1.12 mycroft */
631 1.12 mycroft if (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME) {
632 1.12 mycroft /*
633 1.12 mycroft * Disallow directory write attempts on read-only
634 1.12 mycroft * file systems.
635 1.12 mycroft */
636 1.12 mycroft if (rdonly || (dp->v_mount->mnt_flag & MNT_RDONLY) ||
637 1.12 mycroft (wantparent &&
638 1.12 mycroft (dvp->v_mount->mnt_flag & MNT_RDONLY))) {
639 1.12 mycroft error = EROFS;
640 1.12 mycroft goto bad2;
641 1.12 mycroft }
642 1.12 mycroft }
643 1.12 mycroft /* ASSERT(dvp == ndp->ni_startdir) */
644 1.12 mycroft if (cnp->cn_flags & SAVESTART)
645 1.12 mycroft VREF(dvp);
646 1.12 mycroft
647 1.12 mycroft if (!wantparent)
648 1.12 mycroft vrele(dvp);
649 1.12 mycroft if ((cnp->cn_flags & LOCKLEAF) == 0)
650 1.12 mycroft VOP_UNLOCK(dp);
651 1.12 mycroft return (0);
652 1.12 mycroft
653 1.12 mycroft bad2:
654 1.12 mycroft if ((cnp->cn_flags & LOCKPARENT) && (cnp->cn_flags & ISLASTCN))
655 1.12 mycroft VOP_UNLOCK(dvp);
656 1.12 mycroft vrele(dvp);
657 1.12 mycroft bad:
658 1.12 mycroft vput(dp);
659 1.12 mycroft *vpp = NULL;
660 1.10 cgd return (error);
661 1.10 cgd }
662