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