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