date and time created 89/07/02 20:11:52 by mckusick
[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 *
7655c64a 17 * @(#)vfs_lookup.c 7.15 (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 */
206be3f9 85 int lockparent; /* 1 => lockparent flag */
6d0f0ece 86 int error = 0;
10873320 87
6d0f0ece
KM
88 flag = ndp->ni_nameiop & OPFLAG;
89 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
206be3f9 90 lockparent = ndp->ni_nameiop & LOCKPARENT;
d870be74 91 docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
6d0f0ece 92 if (flag == DELETE || wantparent)
f93197fc 93 docache = 0;
f5039631 94 /*
6bd0bb92
BJ
95 * Get a buffer for the name to be translated, and copy the
96 * name into the buffer.
f5039631 97 */
6d0f0ece 98 MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
d870be74 99 if (ndp->ni_segflg == UIO_SYSSPACE)
6d0f0ece
KM
100 error = copystr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN,
101 &ndp->ni_pathlen);
d870be74 102 else
6d0f0ece
KM
103 error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf, MAXPATHLEN,
104 &ndp->ni_pathlen);
d870be74 105 if (error) {
6d0f0ece 106 free(ndp->ni_pnbuf, M_NAMEI);
51610be6 107 ndp->ni_vp = NULL;
6d0f0ece 108 return (error);
d870be74 109 }
6d0f0ece
KM
110 ndp->ni_ptr = ndp->ni_pnbuf;
111 ndp->ni_loopcnt = 0;
112 dp = ndp->ni_cdir;
8fe1c702 113 VREF(dp);
658f5fdc
MT
114#ifdef KTRACE
115 if (KTRPOINT(u.u_procp, KTR_NAMEI))
6d0f0ece 116 ktrnamei(u.u_procp->p_tracep, ndp->ni_pnbuf);
658f5fdc 117#endif
6bd0bb92 118
6d0f0ece 119start:
10873320 120 /*
6bd0bb92 121 * Get starting directory.
6d0f0ece 122 * Done at start of translation and after symbolic link.
10873320 123 */
6d0f0ece
KM
124 if (*ndp->ni_ptr == '/') {
125 vrele(dp);
126 while (*ndp->ni_ptr == '/') {
127 ndp->ni_ptr++;
128 ndp->ni_pathlen--;
129 }
130 if ((dp = ndp->ni_rdir) == NULL)
10873320 131 dp = rootdir;
8fe1c702 132 VREF(dp);
6d0f0ece
KM
133 }
134 VOP_LOCK(dp);
9e7c949b 135 ndp->ni_endoff = 0;
6bd0bb92 136
10873320 137 /*
6bd0bb92 138 * We come to dirloop to search a new directory.
10873320 139 */
6bd0bb92 140dirloop:
6bd0bb92 141 /*
d870be74 142 * Copy next component of name to ndp->ni_dent.
6d0f0ece
KM
143 * XXX kern_exec looks at d_name
144 * ??? The ni_hash value may be useful for vfs_cache
145 * XXX There must be the last component of the filename left
146 * somewhere accessible via. ndp for NFS (and any other stateless file
147 * systems) in case they are doing a CREATE. The "Towards a..." noted
148 * that ni_ptr would be left pointing to the last component, but since
149 * the ni_pnbuf gets free'd, that is not a good idea.
6bd0bb92 150 */
26ae96bd
KM
151#ifdef notdef
152 for (cp = ndp->ni_ptr; *cp != 0 && *cp != '/'; cp++) {
153 if ((*cp & 0200) == 0)
154 continue;
155 if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
156 error = EINVAL;
157 goto bad;
158 }
159 }
160 ndp->ni_namelen = cp - ndp->ni_ptr;
161 if (ndp->ni_namelen >= MAXNAMLEN) {
162 error = ENAMETOOLONG;
163 goto bad;
164 }
165 ndp->ni_pathlen -= ndp->ni_namelen;
166#ifdef NAMEI_DIAGNOSTIC
167 { char c = *cp;
168 *cp = '\0';
169 printf("{%s}: ", ndp->ni_ptr);
170 *cp = c; }
171#endif
172#else fornow
6d0f0ece
KM
173 ndp->ni_hash = 0;
174 for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) {
6459ebe0 175 if (i >= MAXNAMLEN) {
6d0f0ece 176 error = ENAMETOOLONG;
d870be74
KM
177 goto bad;
178 }
067da729
KM
179 if (*cp & 0200)
180 if ((*cp&0377) == ('/'|0200) || flag != DELETE) {
6d0f0ece 181 error = EINVAL;
067da729
KM
182 goto bad;
183 }
d870be74 184 ndp->ni_dent.d_name[i++] = *cp;
6d0f0ece 185 ndp->ni_hash += (unsigned char)*cp * i;
6459ebe0 186 }
6d0f0ece 187 ndp->ni_namelen = i;
d870be74
KM
188 ndp->ni_dent.d_namlen = i;
189 ndp->ni_dent.d_name[i] = '\0';
6d0f0ece
KM
190 ndp->ni_pathlen -= i;
191#ifdef NAMEI_DIAGNOSTIC
192 printf("{%s}: ", ndp->ni_dent.d_name);
193#endif
26ae96bd 194#endif fornow
6d0f0ece
KM
195 ndp->ni_next = cp;
196 ndp->ni_makeentry = 1;
87c05e6e 197 if (*cp == '\0' && docache == 0)
6d0f0ece
KM
198 ndp->ni_makeentry = 0;
199 ndp->ni_isdotdot = (ndp->ni_namelen == 2 &&
200 ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
6bd0bb92
BJ
201
202 /*
203 * Check for degenerate name (e.g. / or "")
204 * which is a way of talking about a directory,
205 * e.g. like "/." or ".".
206 */
6d0f0ece
KM
207 if (ndp->ni_ptr[0] == '\0') {
208 if (flag != LOOKUP || wantparent) {
209 error = EISDIR;
6bd0bb92 210 goto bad;
f5039631 211 }
6d0f0ece
KM
212 free(ndp->ni_pnbuf, M_NAMEI);
213 if (!(ndp->ni_nameiop & LOCKLEAF))
214 VOP_UNLOCK(dp);
215 ndp->ni_vp = dp;
216 return (0);
f5039631 217 }
6bd0bb92 218
e47da406 219 /*
6d0f0ece
KM
220 * Handle "..": two special cases.
221 * 1. If at root directory (e.g. after chroot)
222 * then ignore it so can't get out.
223 * 2. If this vnode is the root of a mounted
224 * file system, then replace it with the
225 * vnode which was mounted on so we take the
226 * .. in the other file system.
e47da406 227 */
6d0f0ece 228 if (ndp->ni_isdotdot) {
e47da406 229 for (;;) {
6d0f0ece
KM
230 if (dp == ndp->ni_rdir || dp == rootdir) {
231 ndp->ni_dvp = dp;
7655c64a 232 ndp->ni_vp = dp;
8fe1c702 233 VREF(dp);
6d0f0ece 234 goto nextname;
e47da406 235 }
6d0f0ece 236 if ((dp->v_flag & VROOT) == 0)
e47da406 237 break;
6d0f0ece
KM
238 tdp = dp;
239 dp = dp->v_mount->m_vnodecovered;
240 vput(tdp);
8fe1c702 241 VREF(dp);
7655c64a 242 VOP_LOCK(dp);
e47da406
KM
243 }
244 }
245
f93197fc
KM
246 /*
247 * We now have a segment name to search for, and a directory to search.
6459ebe0 248 */
6d0f0ece
KM
249 if (error = VOP_LOOKUP(dp, ndp)) {
250 if (ndp->ni_vp != NULL)
251 panic("leaf should be empty");
658f5fdc 252#ifdef NAMEI_DIAGNOSTIC
6d0f0ece 253 printf("not found\n");
658f5fdc 254#endif
f5039631 255 /*
6d0f0ece
KM
256 * If creating and at end of pathname, then can consider
257 * allowing file to be created.
f5039631 258 */
6d0f0ece
KM
259 if (ndp->ni_dvp->v_mount->m_flag & M_RDONLY)
260 error = EROFS;
e46d82a6
KM
261 if (flag == LOOKUP || flag == DELETE ||
262 error != ENOENT || *cp != 0)
6bd0bb92 263 goto bad;
f5039631 264 /*
6d0f0ece
KM
265 * We return with ni_vp NULL to indicate that the entry
266 * doesn't currently exist, leaving a pointer to the
267 * (possibly locked) directory inode in ndp->ni_dvp.
f5039631 268 */
6d0f0ece
KM
269 FREE(ndp->ni_pnbuf, M_NAMEI);
270 return (0); /* should this be ENOENT? */
6bd0bb92 271 }
658f5fdc 272#ifdef NAMEI_DIAGNOSTIC
6d0f0ece 273 printf("found\n");
658f5fdc 274#endif
6bd0bb92
BJ
275
276 /*
6d0f0ece 277 * Check for symbolic link
4f083fd7 278 */
6d0f0ece
KM
279 dp = ndp->ni_vp;
280 if ((dp->v_type == VLNK) &&
281 ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) {
282 struct iovec aiov;
283 struct uio auio;
284 int linklen;
285
286 if (++ndp->ni_loopcnt > MAXSYMLINKS) {
287 error = ELOOP;
288 goto bad2;
4f083fd7 289 }
0607f104 290 if (ndp->ni_pathlen > 1)
6d0f0ece
KM
291 MALLOC(cp, char *, MAXPATHLEN, M_NAMEI, M_WAITOK);
292 else
293 cp = ndp->ni_pnbuf;
294 aiov.iov_base = cp;
295 aiov.iov_len = MAXPATHLEN;
296 auio.uio_iov = &aiov;
297 auio.uio_iovcnt = 1;
298 auio.uio_offset = 0;
299 auio.uio_rw = UIO_READ;
300 auio.uio_segflg = UIO_SYSSPACE;
301 auio.uio_resid = MAXPATHLEN;
302 if (error = VOP_READLINK(dp, &auio, ndp->ni_cred)) {
0607f104 303 if (ndp->ni_pathlen > 1)
6d0f0ece 304 free(cp, M_NAMEI);
bde63aa5 305 goto bad2;
6d0f0ece
KM
306 }
307 linklen = MAXPATHLEN - auio.uio_resid;
308 if (linklen + ndp->ni_pathlen >= MAXPATHLEN) {
0607f104 309 if (ndp->ni_pathlen > 1)
6d0f0ece
KM
310 free(cp, M_NAMEI);
311 error = ENAMETOOLONG;
bde63aa5 312 goto bad2;
f93197fc 313 }
0607f104
KM
314 if (ndp->ni_pathlen > 1) {
315 bcopy(ndp->ni_next, cp + linklen, ndp->ni_pathlen);
6d0f0ece
KM
316 FREE(ndp->ni_pnbuf, M_NAMEI);
317 ndp->ni_pnbuf = cp;
206be3f9 318 } else
0607f104 319 ndp->ni_pnbuf[linklen] = '\0';
6d0f0ece
KM
320 ndp->ni_ptr = cp;
321 ndp->ni_pathlen += linklen;
322 vput(dp);
323 dp = ndp->ni_dvp;
206be3f9
KM
324 if (lockparent && *ndp->ni_next == '\0')
325 VOP_UNLOCK(dp);
6d0f0ece 326 goto start;
f93197fc
KM
327 }
328
6bd0bb92 329 /*
6d0f0ece
KM
330 * Check to see if the vnode has been mounted on;
331 * if so find the root of the mounted file system.
6bd0bb92 332 */
6d0f0ece
KM
333mntloop:
334 while (dp->v_type == VDIR && (mp = dp->v_mountedhere)) {
335 while(mp->m_flag & M_MLOCK) {
336 mp->m_flag |= M_MWAIT;
337 sleep((caddr_t)mp, PVFS);
338 goto mntloop;
6bd0bb92 339 }
6d0f0ece
KM
340 error = VFS_ROOT(dp->v_mountedhere, &tdp);
341 if (error)
6bd0bb92 342 goto bad2;
6d0f0ece
KM
343 vput(dp);
344 ndp->ni_vp = dp = tdp;
10873320 345 }
6bd0bb92 346
6d0f0ece 347nextname:
10873320 348 /*
6bd0bb92
BJ
349 * Not a symbolic link. If more pathname,
350 * continue at next component, else return.
10873320 351 */
6d0f0ece
KM
352 ndp->ni_ptr = ndp->ni_next;
353 if (*ndp->ni_ptr == '/') {
354 while (*ndp->ni_ptr == '/') {
355 ndp->ni_ptr++;
356 ndp->ni_pathlen--;
6bd0bb92 357 }
6d0f0ece
KM
358 vrele(ndp->ni_dvp);
359 goto dirloop;
6bd0bb92
BJ
360 }
361 /*
6d0f0ece 362 * Check for read-only file systems and executing texts
6bd0bb92 363 */
dfc0e8dd
KM
364 if (flag != LOOKUP) {
365 /*
366 * Disallow write attempts on read-only file systems;
367 * unless the file is a socket or a block or character
368 * device resident on the file system.
369 */
370 if ((dp->v_mount->m_flag & M_RDONLY) &&
371 dp->v_type != VCHR &&
372 dp->v_type != VBLK &&
373 dp->v_type != VSOCK) {
374 error = EROFS;
375 goto bad2;
376 }
377 /*
378 * If there's shared text associated with
379 * the inode, try to free it up once. If
380 * we fail, we can't allow writing.
381 */
382 if (dp->v_flag & VTEXT)
383 xrele(dp);
384 if (dp->v_flag & VTEXT) {
385 error = ETXTBSY;
386 goto bad2;
387 }
388 if (wantparent && flag != CREATE &&
389 (ndp->ni_dvp->v_mount->m_flag & M_RDONLY)) {
390 error = EROFS;
391 goto bad2;
392 }
393 }
6d0f0ece
KM
394 if (!wantparent)
395 vrele(ndp->ni_dvp);
396 if ((ndp->ni_nameiop & LOCKLEAF) == 0)
397 VOP_UNLOCK(dp);
398 FREE(ndp->ni_pnbuf, M_NAMEI);
399 return (0);
4f083fd7 400
6d0f0ece 401bad2:
206be3f9
KM
402 if (lockparent && *ndp->ni_next == '\0')
403 VOP_UNLOCK(ndp->ni_dvp);
6d0f0ece
KM
404 vrele(ndp->ni_dvp);
405bad:
406 vput(dp);
407 ndp->ni_vp = NULL;
408 FREE(ndp->ni_pnbuf, M_NAMEI);
b1aa93b9
KM
409 return (error);
410}