remove setjmp
[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 *
54fb9dc2 17 * @(#)vfs_lookup.c 7.21 (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 */
d5cea2aa
KM
86 int getbuf; /* 1 => Malloc a pathname buffer */
87 int rdonly; /* mounted read-only flag bit(s) */
6d0f0ece 88 int error = 0;
10873320 89
d5cea2aa
KM
90 /*
91 * Setup: break out flag bits into variables.
92 */
dd4eee4b 93 ndp->ni_dvp = NULL;
6d0f0ece
KM
94 flag = ndp->ni_nameiop & OPFLAG;
95 wantparent = ndp->ni_nameiop & (LOCKPARENT|WANTPARENT);
206be3f9 96 lockparent = ndp->ni_nameiop & LOCKPARENT;
d870be74 97 docache = (ndp->ni_nameiop & NOCACHE) ^ NOCACHE;
d5cea2aa 98 getbuf = (ndp->ni_nameiop & HASBUF) ^ HASBUF;
6d0f0ece 99 if (flag == DELETE || wantparent)
f93197fc 100 docache = 0;
54fb9dc2 101 rdonly = MNT_RDONLY;
d5cea2aa 102 if (ndp->ni_nameiop & REMOTE)
54fb9dc2 103 rdonly |= MNT_EXRDONLY;
f5039631 104 /*
6bd0bb92
BJ
105 * Get a buffer for the name to be translated, and copy the
106 * name into the buffer.
f5039631 107 */
d5cea2aa
KM
108 if (getbuf) {
109 MALLOC(ndp->ni_pnbuf, caddr_t, MAXPATHLEN, M_NAMEI, M_WAITOK);
110 if (ndp->ni_segflg == UIO_SYSSPACE)
111 error = copystr(ndp->ni_dirp, ndp->ni_pnbuf,
112 MAXPATHLEN, &ndp->ni_pathlen);
113 else
114 error = copyinstr(ndp->ni_dirp, ndp->ni_pnbuf,
115 MAXPATHLEN, &ndp->ni_pathlen);
116 if (error) {
117 free(ndp->ni_pnbuf, M_NAMEI);
118 ndp->ni_vp = NULL;
119 return (error);
120 }
121 ndp->ni_ptr = ndp->ni_pnbuf;
d870be74 122 }
6d0f0ece
KM
123 ndp->ni_loopcnt = 0;
124 dp = ndp->ni_cdir;
8fe1c702 125 VREF(dp);
658f5fdc
MT
126#ifdef KTRACE
127 if (KTRPOINT(u.u_procp, KTR_NAMEI))
6d0f0ece 128 ktrnamei(u.u_procp->p_tracep, ndp->ni_pnbuf);
658f5fdc 129#endif
6bd0bb92 130
6d0f0ece 131start:
10873320 132 /*
6bd0bb92 133 * Get starting directory.
6d0f0ece 134 * Done at start of translation and after symbolic link.
10873320 135 */
6d0f0ece
KM
136 if (*ndp->ni_ptr == '/') {
137 vrele(dp);
138 while (*ndp->ni_ptr == '/') {
139 ndp->ni_ptr++;
140 ndp->ni_pathlen--;
141 }
142 if ((dp = ndp->ni_rdir) == NULL)
10873320 143 dp = rootdir;
8fe1c702 144 VREF(dp);
6d0f0ece
KM
145 }
146 VOP_LOCK(dp);
9e7c949b 147 ndp->ni_endoff = 0;
6bd0bb92 148
10873320 149 /*
6bd0bb92 150 * We come to dirloop to search a new directory.
10873320 151 */
6bd0bb92 152dirloop:
6bd0bb92 153 /*
d870be74 154 * Copy next component of name to ndp->ni_dent.
6d0f0ece
KM
155 * XXX kern_exec looks at d_name
156 * ??? The ni_hash value may be useful for vfs_cache
157 * XXX There must be the last component of the filename left
158 * somewhere accessible via. ndp for NFS (and any other stateless file
159 * systems) in case they are doing a CREATE. The "Towards a..." noted
160 * that ni_ptr would be left pointing to the last component, but since
161 * the ni_pnbuf gets free'd, that is not a good idea.
6bd0bb92 162 */
d5cea2aa
KM
163 if (getbuf) {
164 ndp->ni_hash = 0;
165 for (cp = ndp->ni_ptr, i = 0; *cp != 0 && *cp != '/'; cp++) {
166 if (i >= MAXNAMLEN) {
167 error = ENAMETOOLONG;
067da729
KM
168 goto bad;
169 }
d5cea2aa
KM
170 if (*cp & 0200)
171 if ((*cp&0377) == ('/'|0200) ||
172 flag != DELETE) {
173 error = EINVAL;
174 goto bad;
175 }
176 ndp->ni_dent.d_name[i++] = *cp;
177 ndp->ni_hash += (unsigned char)*cp * i;
178 }
179 ndp->ni_namelen = i;
180 ndp->ni_dent.d_namlen = i;
181 ndp->ni_dent.d_name[i] = '\0';
182 ndp->ni_pathlen -= i;
183 ndp->ni_next = cp;
6d0f0ece 184#ifdef NAMEI_DIAGNOSTIC
d5cea2aa 185 printf("{%s}: ", ndp->ni_dent.d_name);
6d0f0ece 186#endif
d5cea2aa
KM
187 }
188 cp = ndp->ni_next;
6d0f0ece 189 ndp->ni_makeentry = 1;
87c05e6e 190 if (*cp == '\0' && docache == 0)
6d0f0ece
KM
191 ndp->ni_makeentry = 0;
192 ndp->ni_isdotdot = (ndp->ni_namelen == 2 &&
193 ndp->ni_dent.d_name[1] == '.' && ndp->ni_dent.d_name[0] == '.');
6bd0bb92
BJ
194
195 /*
196 * Check for degenerate name (e.g. / or "")
197 * which is a way of talking about a directory,
198 * e.g. like "/." or ".".
199 */
6d0f0ece
KM
200 if (ndp->ni_ptr[0] == '\0') {
201 if (flag != LOOKUP || wantparent) {
202 error = EISDIR;
6bd0bb92 203 goto bad;
f5039631 204 }
d5cea2aa
KM
205 if (getbuf)
206 free(ndp->ni_pnbuf, M_NAMEI);
6d0f0ece
KM
207 if (!(ndp->ni_nameiop & LOCKLEAF))
208 VOP_UNLOCK(dp);
209 ndp->ni_vp = dp;
210 return (0);
f5039631 211 }
6bd0bb92 212
e47da406 213 /*
6d0f0ece
KM
214 * Handle "..": two special cases.
215 * 1. If at root directory (e.g. after chroot)
216 * then ignore it so can't get out.
217 * 2. If this vnode is the root of a mounted
218 * file system, then replace it with the
219 * vnode which was mounted on so we take the
220 * .. in the other file system.
e47da406 221 */
6d0f0ece 222 if (ndp->ni_isdotdot) {
e47da406 223 for (;;) {
6d0f0ece
KM
224 if (dp == ndp->ni_rdir || dp == rootdir) {
225 ndp->ni_dvp = dp;
7655c64a 226 ndp->ni_vp = dp;
8fe1c702 227 VREF(dp);
6d0f0ece 228 goto nextname;
e47da406 229 }
d5cea2aa
KM
230 if ((dp->v_flag & VROOT) == 0 ||
231 (ndp->ni_nameiop & NOCROSSMOUNT))
e47da406 232 break;
6d0f0ece 233 tdp = dp;
54fb9dc2 234 dp = dp->v_mount->mnt_vnodecovered;
6d0f0ece 235 vput(tdp);
8fe1c702 236 VREF(dp);
7655c64a 237 VOP_LOCK(dp);
e47da406
KM
238 }
239 }
240
f93197fc
KM
241 /*
242 * We now have a segment name to search for, and a directory to search.
6459ebe0 243 */
6d0f0ece
KM
244 if (error = VOP_LOOKUP(dp, ndp)) {
245 if (ndp->ni_vp != NULL)
246 panic("leaf should be empty");
658f5fdc 247#ifdef NAMEI_DIAGNOSTIC
6d0f0ece 248 printf("not found\n");
658f5fdc 249#endif
1f6ef9f5
KM
250 if (flag == LOOKUP || flag == DELETE ||
251 error != ENOENT || *cp != 0)
252 goto bad;
f5039631 253 /*
6d0f0ece
KM
254 * If creating and at end of pathname, then can consider
255 * allowing file to be created.
f5039631 256 */
54fb9dc2 257 if (ndp->ni_dvp->v_mount->mnt_flag & rdonly) {
6d0f0ece 258 error = EROFS;
6bd0bb92 259 goto bad;
1f6ef9f5 260 }
f5039631 261 /*
6d0f0ece
KM
262 * We return with ni_vp NULL to indicate that the entry
263 * doesn't currently exist, leaving a pointer to the
264 * (possibly locked) directory inode in ndp->ni_dvp.
f5039631 265 */
d5cea2aa
KM
266 if (getbuf)
267 FREE(ndp->ni_pnbuf, M_NAMEI);
6d0f0ece 268 return (0); /* should this be ENOENT? */
6bd0bb92 269 }
658f5fdc 270#ifdef NAMEI_DIAGNOSTIC
6d0f0ece 271 printf("found\n");
658f5fdc 272#endif
6bd0bb92
BJ
273
274 /*
6d0f0ece 275 * Check for symbolic link
4f083fd7 276 */
6d0f0ece
KM
277 dp = ndp->ni_vp;
278 if ((dp->v_type == VLNK) &&
279 ((ndp->ni_nameiop & FOLLOW) || *ndp->ni_next == '/')) {
280 struct iovec aiov;
281 struct uio auio;
282 int linklen;
283
d5cea2aa
KM
284 if (!getbuf)
285 panic("namei: unexpected symlink");
6d0f0ece
KM
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 320 ndp->ni_ptr = cp;
6d0f0ece
KM
321 vput(dp);
322 dp = ndp->ni_dvp;
ff4672c1 323 if (lockparent && ndp->ni_pathlen == 1)
206be3f9 324 VOP_UNLOCK(dp);
ff4672c1 325 ndp->ni_pathlen += linklen;
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 333mntloop:
d5cea2aa
KM
334 while (dp->v_type == VDIR && (mp = dp->v_mountedhere) &&
335 (ndp->ni_nameiop & NOCROSSMOUNT) == 0) {
54fb9dc2
KM
336 while(mp->mnt_flag & MNT_MLOCK) {
337 mp->mnt_flag |= MNT_MWAIT;
6d0f0ece
KM
338 sleep((caddr_t)mp, PVFS);
339 goto mntloop;
6bd0bb92 340 }
6d0f0ece
KM
341 error = VFS_ROOT(dp->v_mountedhere, &tdp);
342 if (error)
6bd0bb92 343 goto bad2;
6d0f0ece
KM
344 vput(dp);
345 ndp->ni_vp = dp = tdp;
10873320 346 }
6bd0bb92 347
6d0f0ece 348nextname:
10873320 349 /*
6bd0bb92
BJ
350 * Not a symbolic link. If more pathname,
351 * continue at next component, else return.
10873320 352 */
6d0f0ece
KM
353 ndp->ni_ptr = ndp->ni_next;
354 if (*ndp->ni_ptr == '/') {
355 while (*ndp->ni_ptr == '/') {
356 ndp->ni_ptr++;
357 ndp->ni_pathlen--;
6bd0bb92 358 }
6d0f0ece
KM
359 vrele(ndp->ni_dvp);
360 goto dirloop;
6bd0bb92
BJ
361 }
362 /*
01633fea 363 * Check for read-only file systems.
6bd0bb92 364 */
01633fea 365 if (flag == DELETE || flag == RENAME) {
dfc0e8dd 366 /*
01633fea
KM
367 * Disallow directory write attempts on read-only
368 * file systems.
dfc0e8dd 369 */
54fb9dc2
KM
370 if ((dp->v_mount->mnt_flag & rdonly) ||
371 (wantparent && (ndp->ni_dvp->v_mount->mnt_flag & rdonly))) {
dfc0e8dd
KM
372 error = EROFS;
373 goto bad2;
374 }
375 }
6d0f0ece
KM
376 if (!wantparent)
377 vrele(ndp->ni_dvp);
378 if ((ndp->ni_nameiop & LOCKLEAF) == 0)
379 VOP_UNLOCK(dp);
d5cea2aa
KM
380 if (getbuf)
381 FREE(ndp->ni_pnbuf, M_NAMEI);
6d0f0ece 382 return (0);
4f083fd7 383
6d0f0ece 384bad2:
206be3f9
KM
385 if (lockparent && *ndp->ni_next == '\0')
386 VOP_UNLOCK(ndp->ni_dvp);
6d0f0ece
KM
387 vrele(ndp->ni_dvp);
388bad:
389 vput(dp);
390 ndp->ni_vp = NULL;
d5cea2aa
KM
391 if (getbuf)
392 FREE(ndp->ni_pnbuf, M_NAMEI);
b1aa93b9
KM
393 return (error);
394}