don't log utmp errors; may not have mounted file systems yet
[unix-history] / usr / src / sys / kern / vfs_lookup.c
CommitLineData
da7c5cc6 1/*
6d0f0ece
KM
2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
6d0f0ece
KM
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16 *
26ae96bd 17 * @(#)vfs_lookup.c 7.9 (Berkeley) %G%
da7c5cc6 18 */
10873320 19
94368568 20#include "param.h"
6d0f0ece
KM
21#include "time.h"
22#include "namei.h"
23#include "vnode.h"
94368568 24#include "mount.h"
6d0f0ece 25#include "errno.h"
c3a74062 26#include "malloc.h"
6d0f0ece 27
658f5fdc 28#ifdef KTRACE
6d0f0ece 29#include "user.h"
658f5fdc
MT
30#include "proc.h"
31#include "ktrace.h"
32#endif
10873320
BJ
33
34/*
7f69b0e6 35 * Convert a pathname into a pointer to a locked inode.
6bd0bb92 36 * This is a very central and rather complicated routine.
7f69b0e6 37 *
6d0f0ece
KM
38 * The flag argument is LOOKUP, CREATE, RENAME, or DELETE depending on
39 * whether the name is to be looked up, created, renamed, or deleted.
40 * When CREATE, RENAME, or DELETE is specified, information usable in
41 * creating, renaming, or deleting a directory entry may be calculated.
42 * If flag has LOCKPARENT or'ed into it and the target of the pathname
43 * exists, namei returns both the target and its parent directory locked.
44 * When creating or renaming and LOCKPARENT is specified, the target may not
45 * be ".". When deleting and LOCKPARENT is specified, the target may be ".".
4f083fd7 46 *
d870be74 47 * The FOLLOW flag is set when symbolic links are to be followed
4f083fd7 48 * when they occur at the end of the name translation process.
7f69b0e6
KM
49 * Symbolic links are always followed for all other pathname
50 * components other than the last.
51 *
52 * The segflg defines whether the name is to be copied from user
53 * space or kernel space.
10873320 54 *
f93197fc 55 * Overall outline of namei:
6bd0bb92
BJ
56 *
57 * copy in name
58 * get starting directory
59 * dirloop:
d870be74 60 * copy next component of name to ndp->ni_dent
6bd0bb92 61 * handle degenerate case where name is null string
6d0f0ece
KM
62 * if .. and on mounted filesys, find parent
63 * call lookup routine for next component name
64 * directory vnode returned in ni_dvp, unlocked unless LOCKPARENT set
65 * component vnode returned in ni_vp (if it exists), locked.
6bd0bb92 66 * if symbolic link, massage name in buffer and continue at dirloop
6d0f0ece 67 * if result inode is mounted on, find mounted on vnode
6bd0bb92 68 * if more components of name, do next level at dirloop
6d0f0ece
KM
69 * return the answer in ni_vp as locked vnode;
70 * if LOCKPARENT set, return locked parent in ni_dvp
4f083fd7 71 *
6d0f0ece 72 * NOTE: (LOOKUP | LOCKPARENT) currently returns the parent vnode unlocked.
10873320 73 */
d870be74
KM
74namei(ndp)
75 register struct nameidata *ndp;
10873320 76{
6bd0bb92 77 register char *cp; /* pointer into pathname argument */
6d0f0ece
KM
78 register struct vnode *dp = 0; /* the directory we are searching */
79 register int i; /* Temp counter */
80 struct vnode *tdp; /* saved dp */
81 struct mount *mp; /* mount table entry */
87c05e6e 82 int docache; /* == 0 do not cache last component */
6d0f0ece
KM
83 int flag; /* LOOKUP, CREATE, RENAME or DELETE */
84 int wantparent; /* 1 => wantparent or lockparent flag */
85 int error = 0;
10873320 86
6d0f0ece
KM
87 flag = ndp->ni_nameiop & OPFLAG;
88 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
d870be74 89 docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
6d0f0ece 90 if (flag == DELETE || wantparent)
f93197fc 91 docache = 0;
f5039631 92 /*
6bd0bb92
BJ
93 * Get a buffer for the name to be translated, and copy the
94 * name into the buffer.
f5039631 95 */
6d0f0ece 96 MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
d870be74 97 if (ndp->ni_segflg == UIO_SYSSPACE)
6d0f0ece
KM
98 error = copystr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN,
99 &ndp->ni_pathlen);
d870be74 100 else
6d0f0ece
KM
101 error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN,
102 &ndp->ni_pathlen);
d870be74 103 if (error) {
6d0f0ece
KM
104 free(ndp->ni_pnbuf, M_NAMEI);
105 return (error);
d870be74 106 }
6d0f0ece
KM
107 ndp->ni_ptr = ndp->ni_pnbuf;
108 ndp->ni_loopcnt = 0;
109 dp = ndp->ni_cdir;
110 dp->v_count++;
658f5fdc
MT
111#ifdef KTRACE
112 if (KTRPOINT(u.u_procp, KTR_NAMEI))
6d0f0ece 113 ktrnamei(u.u_procp->p_tracep, ndp->ni_pnbuf);
658f5fdc 114#endif
6bd0bb92 115
6d0f0ece 116start:
10873320 117 /*
6bd0bb92 118 * Get starting directory.
6d0f0ece 119 * Done at start of translation and after symbolic link.
10873320 120 */
6d0f0ece
KM
121 if (*ndp->ni_ptr == '/') {
122 vrele(dp);
123 while (*ndp->ni_ptr == '/') {
124 ndp->ni_ptr++;
125 ndp->ni_pathlen--;
126 }
127 if ((dp = ndp->ni_rdir) == NULL)
10873320 128 dp = rootdir;
6d0f0ece
KM
129 dp->v_count++;
130 }
131 VOP_LOCK(dp);
9e7c949b 132 ndp->ni_endoff = 0;
6bd0bb92 133
10873320 134 /*
6bd0bb92 135 * We come to dirloop to search a new directory.
10873320 136 */
6bd0bb92 137dirloop:
6bd0bb92 138 /*
d870be74 139 * Copy next component of name to ndp->ni_dent.
6d0f0ece
KM
140 * XXX kern_exec looks at d_name
141 * ??? The ni_hash value may be useful for vfs_cache
142 * XXX There must be the last component of the filename left
143 * somewhere accessible via. ndp for NFS (and any other stateless file
144 * systems) in case they are doing a CREATE. The "Towards a..." noted
145 * that ni_ptr would be left pointing to the last component, but since
146 * the ni_pnbuf gets free'd, that is not a good idea.
6bd0bb92 147 */
26ae96bd
KM
148#ifdef notdef
149 for (cp = ndp->ni_ptr; *cp != 0 && *cp != '/'; cp++) {
150 if ((*cp & 0200) == 0)
151 continue;
152 if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
153 error = EINVAL;
154 goto bad;
155 }
156 }
157 ndp->ni_namelen = cp - ndp->ni_ptr;
158 if (ndp->ni_namelen >= MAXNAMLEN) {
159 error = ENAMETOOLONG;
160 goto bad;
161 }
162 ndp->ni_pathlen -= ndp->ni_namelen;
163#ifdef NAMEI_DIAGNOSTIC
164 { char c = *cp;
165 *cp = '\0';
166 printf("{%s}: ", ndp->ni_ptr);
167 *cp = c; }
168#endif
169#else fornow
6d0f0ece
KM
170 ndp->ni_hash = 0;
171 for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) {
6459ebe0 172 if (i >= MAXNAMLEN) {
6d0f0ece 173 error = ENAMETOOLONG;
d870be74
KM
174 goto bad;
175 }
067da729
KM
176 if (*cp & 0200)
177 if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
6d0f0ece 178 error = EINVAL;
067da729
KM
179 goto bad;
180 }
d870be74 181 ndp->ni_dent.d_name[i++] = *cp;
6d0f0ece 182 ndp->ni_hash += (unsigned char)*cp * i;
6459ebe0 183 }
6d0f0ece 184 ndp->ni_namelen = i;
d870be74
KM
185 ndp->ni_dent.d_namlen = i;
186 ndp->ni_dent.d_name[i] = '\0';
6d0f0ece
KM
187 ndp->ni_pathlen -= i;
188#ifdef NAMEI_DIAGNOSTIC
189 printf("{%s}: ", ndp->ni_dent.d_name);
190#endif
26ae96bd 191#endif fornow
6d0f0ece
KM
192 ndp->ni_next = cp;
193 ndp->ni_makeentry = 1;
87c05e6e 194 if (*cp == '\0' && docache == 0)
6d0f0ece
KM
195 ndp->ni_makeentry = 0;
196 ndp->ni_isdotdot = (ndp->ni_namelen == 2 &&
197 ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
6bd0bb92
BJ
198
199 /*
200 * Check for degenerate name (e.g. / or "")
201 * which is a way of talking about a directory,
202 * e.g. like "/." or ".".
203 */
6d0f0ece
KM
204 if (ndp->ni_ptr[0] == '\0') {
205 if (flag != LOOKUP || wantparent) {
206 error = EISDIR;
6bd0bb92 207 goto bad;
f5039631 208 }
6d0f0ece
KM
209 free(ndp->ni_pnbuf, M_NAMEI);
210 if (!(ndp->ni_nameiop & LOCKLEAF))
211 VOP_UNLOCK(dp);
212 ndp->ni_vp = dp;
213 return (0);
f5039631 214 }
6bd0bb92 215
e47da406 216 /*
6d0f0ece
KM
217 * Handle "..": two special cases.
218 * 1. If at root directory (e.g. after chroot)
219 * then ignore it so can't get out.
220 * 2. If this vnode is the root of a mounted
221 * file system, then replace it with the
222 * vnode which was mounted on so we take the
223 * .. in the other file system.
e47da406 224 */
6d0f0ece 225 if (ndp->ni_isdotdot) {
e47da406 226 for (;;) {
6d0f0ece
KM
227 if (dp == ndp->ni_rdir || dp == rootdir) {
228 ndp->ni_dvp = dp;
229 dp->v_count++;
230 goto nextname;
e47da406 231 }
6d0f0ece 232 if ((dp->v_flag & VROOT) == 0)
e47da406 233 break;
6d0f0ece
KM
234 tdp = dp;
235 dp = dp->v_mount->m_vnodecovered;
236 vput(tdp);
237 VOP_LOCK(dp);
238 dp->v_count++;
e47da406
KM
239 }
240 }
241
f93197fc
KM
242 /*
243 * We now have a segment name to search for, and a directory to search.
6459ebe0 244 */
6d0f0ece
KM
245 if (error = VOP_LOOKUP(dp, ndp)) {
246 if (ndp->ni_vp != NULL)
247 panic("leaf should be empty");
658f5fdc 248#ifdef NAMEI_DIAGNOSTIC
6d0f0ece 249 printf("not found\n");
658f5fdc 250#endif
f5039631 251 /*
6d0f0ece
KM
252 * If creating and at end of pathname, then can consider
253 * allowing file to be created.
f5039631 254 */
6d0f0ece
KM
255 if (ndp->ni_dvp->v_mount->m_flag & M_RDONLY)
256 error = EROFS;
e46d82a6
KM
257 if (flag == LOOKUP || flag == DELETE ||
258 error != ENOENT || *cp != 0)
6bd0bb92 259 goto bad;
f5039631 260 /*
6d0f0ece
KM
261 * We return with ni_vp NULL to indicate that the entry
262 * doesn't currently exist, leaving a pointer to the
263 * (possibly locked) directory inode in ndp->ni_dvp.
f5039631 264 */
6d0f0ece
KM
265 FREE(ndp->ni_pnbuf, M_NAMEI);
266 return (0); /* should this be ENOENT? */
6bd0bb92 267 }
658f5fdc 268#ifdef NAMEI_DIAGNOSTIC
6d0f0ece 269 printf("found\n");
658f5fdc 270#endif
6bd0bb92
BJ
271
272 /*
6d0f0ece 273 * Check for symbolic link
4f083fd7 274 */
6d0f0ece
KM
275 dp = ndp->ni_vp;
276 if ((dp->v_type == VLNK) &&
277 ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) {
278 struct iovec aiov;
279 struct uio auio;
280 int linklen;
281
282 if (++ndp->ni_loopcnt > MAXSYMLINKS) {
283 error = ELOOP;
284 goto bad2;
4f083fd7 285 }
6d0f0ece
KM
286 if (ndp->ni_pathlen == 1)
287 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
288 else
289 cp = ndp->ni_pnbuf;
290 aiov.iov_base = cp;
291 aiov.iov_len = MAXPATHLEN;
292 auio.uio_iov = &aiov;
293 auio.uio_iovcnt = 1;
294 auio.uio_offset = 0;
295 auio.uio_rw = UIO_READ;
296 auio.uio_segflg = UIO_SYSSPACE;
297 auio.uio_resid = MAXPATHLEN;
298 if (error = VOP_READLINK(dp, &auio, ndp->ni_cred)) {
299 if (ndp->ni_pathlen == 1)
300 free(cp, M_NAMEI);
bde63aa5 301 goto bad2;
6d0f0ece
KM
302 }
303 linklen = MAXPATHLEN - auio.uio_resid;
304 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
305 if (ndp->ni_pathlen == 1)
306 free(cp, M_NAMEI);
307 error = ENAMETOOLONG;
bde63aa5 308 goto bad2;
f93197fc 309 }
6d0f0ece
KM
310 if (ndp->ni_pathlen == 1) {
311 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
312 FREE(ndp->ni_pnbuf, M_NAMEI);
313 ndp->ni_pnbuf = cp;
314 } else
315 ndp->ni_pnbuf[linklen] = '\0';
316 ndp->ni_ptr = cp;
317 ndp->ni_pathlen += linklen;
318 vput(dp);
319 dp = ndp->ni_dvp;
320 goto start;
f93197fc
KM
321 }
322
6bd0bb92 323 /*
6d0f0ece
KM
324 * Check to see if the vnode has been mounted on;
325 * if so find the root of the mounted file system.
6bd0bb92 326 */
6d0f0ece
KM
327mntloop:
328 while (dp->v_type == VDIR && (mp = dp->v_mountedhere)) {
329 while(mp->m_flag & M_MLOCK) {
330 mp->m_flag |= M_MWAIT;
331 sleep((caddr_t)mp, PVFS);
332 goto mntloop;
6bd0bb92 333 }
6d0f0ece
KM
334 error = VFS_ROOT(dp->v_mountedhere, &tdp);
335 if (error)
6bd0bb92 336 goto bad2;
6d0f0ece
KM
337 vput(dp);
338 ndp->ni_vp = dp = tdp;
10873320 339 }
6bd0bb92 340
6d0f0ece 341nextname:
10873320 342 /*
6bd0bb92
BJ
343 * Not a symbolic link. If more pathname,
344 * continue at next component, else return.
10873320 345 */
6d0f0ece
KM
346 ndp->ni_ptr = ndp->ni_next;
347 if (*ndp->ni_ptr == '/') {
348 while (*ndp->ni_ptr == '/') {
349 ndp->ni_ptr++;
350 ndp->ni_pathlen--;
6bd0bb92 351 }
6d0f0ece
KM
352 vrele(ndp->ni_dvp);
353 goto dirloop;
6bd0bb92
BJ
354 }
355 /*
6d0f0ece 356 * Check for read-only file systems and executing texts
6bd0bb92 357 */
6d0f0ece
KM
358 if (flag != LOOKUP && (error = vn_access(dp, VWRITE, ndp->ni_cred)))
359 goto bad2;
360 if (!wantparent)
361 vrele(ndp->ni_dvp);
362 if ((ndp->ni_nameiop & LOCKLEAF) == 0)
363 VOP_UNLOCK(dp);
364 FREE(ndp->ni_pnbuf, M_NAMEI);
365 return (0);
4f083fd7 366
6d0f0ece
KM
367bad2:
368 vrele(ndp->ni_dvp);
369bad:
370 vput(dp);
371 ndp->ni_vp = NULL;
372 FREE(ndp->ni_pnbuf, M_NAMEI);
b1aa93b9
KM
373 return (error);
374}