vfs_getcwd.c revision 1.14.2.3 1 /* $NetBSD: vfs_getcwd.c,v 1.14.2.3 2002/05/29 21:33:18 nathanw Exp $ */
2
3 /*-
4 * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Bill Sommerfeld.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: vfs_getcwd.c,v 1.14.2.3 2002/05/29 21:33:18 nathanw Exp $");
41
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/namei.h>
45 #include <sys/filedesc.h>
46 #include <sys/kernel.h>
47 #include <sys/file.h>
48 #include <sys/stat.h>
49 #include <sys/vnode.h>
50 #include <sys/mount.h>
51 #include <sys/lwp.h>
52 #include <sys/proc.h>
53 #include <sys/uio.h>
54 #include <sys/malloc.h>
55 #include <sys/dirent.h>
56 #include <ufs/ufs/dir.h> /* XXX only for DIRBLKSIZ */
57
58 #include <sys/sa.h>
59 #include <sys/syscallargs.h>
60
61 static int
62 getcwd_scandir __P((struct vnode **, struct vnode **,
63 char **, char *, struct proc *));
64 static int
65 getcwd_getcache __P((struct vnode **, struct vnode **,
66 char **, char *));
67 int
68 getcwd_common __P((struct vnode *, struct vnode *,
69 char **, char *, int, int, struct proc *));
70
71 #define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
72
73 /*
74 * Vnode variable naming conventions in this file:
75 *
76 * rvp: the current root we're aiming towards.
77 * lvp, *lvpp: the "lower" vnode
78 * uvp, *uvpp: the "upper" vnode.
79 *
80 * Since all the vnodes we're dealing with are directories, and the
81 * lookups are going *up* in the filesystem rather than *down*, the
82 * usual "pvp" (parent) or "dvp" (directory) naming conventions are
83 * too confusing.
84 */
85
86 /*
87 * XXX Will infinite loop in certain cases if a directory read reliably
88 * returns EINVAL on last block.
89 * XXX is EINVAL the right thing to return if a directory is malformed?
90 */
91
92 /*
93 * XXX Untested vs. mount -o union; probably does the wrong thing.
94 */
95
96 /*
97 * Find parent vnode of *lvpp, return in *uvpp
98 *
99 * If we care about the name, scan it looking for name of directory
100 * entry pointing at lvp.
101 *
102 * Place the name in the buffer which starts at bufp, immediately
103 * before *bpp, and move bpp backwards to point at the start of it.
104 *
105 * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
106 * On exit, *uvpp is either NULL or is a locked vnode reference.
107 */
108 static int
109 getcwd_scandir(lvpp, uvpp, bpp, bufp, p)
110 struct vnode **lvpp;
111 struct vnode **uvpp;
112 char **bpp;
113 char *bufp;
114 struct proc *p;
115 {
116 int error = 0;
117 int eofflag;
118 off_t off;
119 int tries;
120 struct uio uio;
121 struct iovec iov;
122 char *dirbuf = NULL;
123 int dirbuflen;
124 ino_t fileno;
125 struct vattr va;
126 struct vnode *uvp = NULL;
127 struct vnode *lvp = *lvpp;
128 struct componentname cn;
129 int len, reclen;
130 tries = 0;
131
132 /*
133 * If we want the filename, get some info we need while the
134 * current directory is still locked.
135 */
136 if (bufp != NULL) {
137 error = VOP_GETATTR(lvp, &va, p->p_ucred, p);
138 if (error) {
139 vput(lvp);
140 *lvpp = NULL;
141 *uvpp = NULL;
142 return error;
143 }
144 }
145
146 /*
147 * Ok, we have to do it the hard way..
148 * Next, get parent vnode using lookup of ..
149 */
150 cn.cn_nameiop = LOOKUP;
151 cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
152 cn.cn_proc = p;
153 cn.cn_cred = p->p_ucred;
154 cn.cn_pnbuf = NULL;
155 cn.cn_nameptr = "..";
156 cn.cn_namelen = 2;
157 cn.cn_hash = 0;
158 cn.cn_consume = 0;
159
160 /*
161 * At this point, lvp is locked and will be unlocked by the lookup.
162 * On successful return, *uvpp will be locked
163 */
164 error = VOP_LOOKUP(lvp, uvpp, &cn);
165 if (error) {
166 vput(lvp);
167 *lvpp = NULL;
168 *uvpp = NULL;
169 return error;
170 }
171 uvp = *uvpp;
172
173 /* If we don't care about the pathname, we're done */
174 if (bufp == NULL) {
175 vrele(lvp);
176 *lvpp = NULL;
177 return 0;
178 }
179
180 fileno = va.va_fileid;
181
182 dirbuflen = DIRBLKSIZ;
183 if (dirbuflen < va.va_blocksize)
184 dirbuflen = va.va_blocksize;
185 dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
186
187 #if 0
188 unionread:
189 #endif
190 off = 0;
191 do {
192 /* call VOP_READDIR of parent */
193 iov.iov_base = dirbuf;
194 iov.iov_len = dirbuflen;
195
196 uio.uio_iov = &iov;
197 uio.uio_iovcnt = 1;
198 uio.uio_offset = off;
199 uio.uio_resid = dirbuflen;
200 uio.uio_segflg = UIO_SYSSPACE;
201 uio.uio_rw = UIO_READ;
202 uio.uio_procp = p;
203
204 eofflag = 0;
205
206 error = VOP_READDIR(uvp, &uio, p->p_ucred, &eofflag, 0, 0);
207
208 off = uio.uio_offset;
209
210 /*
211 * Try again if NFS tosses its cookies.
212 * XXX this can still loop forever if the directory is busted
213 * such that the second or subsequent page of it always
214 * returns EINVAL
215 */
216 if ((error == EINVAL) && (tries < 3)) {
217 off = 0;
218 tries++;
219 continue; /* once more, with feeling */
220 }
221
222 if (!error) {
223 char *cpos;
224 struct dirent *dp;
225
226 cpos = dirbuf;
227 tries = 0;
228
229 /* scan directory page looking for matching vnode */
230 for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
231 dp = (struct dirent *) cpos;
232 reclen = dp->d_reclen;
233
234 /* check for malformed directory.. */
235 if (reclen < DIRENT_MINSIZE) {
236 error = EINVAL;
237 goto out;
238 }
239 /*
240 * XXX should perhaps do VOP_LOOKUP to
241 * check that we got back to the right place,
242 * but getting the locking games for that
243 * right would be heinous.
244 */
245 if ((dp->d_type != DT_WHT) &&
246 (dp->d_fileno == fileno)) {
247 char *bp = *bpp;
248 bp -= dp->d_namlen;
249
250 if (bp <= bufp) {
251 error = ERANGE;
252 goto out;
253 }
254 memcpy(bp, dp->d_name, dp->d_namlen);
255 error = 0;
256 *bpp = bp;
257 goto out;
258 }
259 cpos += reclen;
260 }
261 } else
262 goto out;
263 } while (!eofflag);
264 #if 0
265 /*
266 * Deal with mount -o union, which unions only the
267 * root directory of the mount.
268 */
269 if ((uvp->v_flag & VROOT) &&
270 (uvp->v_mount->mnt_flag & MNT_UNION)) {
271 struct vnode *tvp = uvp;
272 uvp = uvp->v_mount->mnt_vnodecovered;
273 vput(tvp);
274 VREF(uvp);
275 *uvpp = uvp;
276 error = vn_lock(uvp, LK_EXCLUSIVE | LK_RETRY);
277 if (error != 0) {
278 vrele(uvp);
279 *uvpp = uvp = NULL;
280 goto out;
281 }
282 goto unionread;
283 }
284 #endif
285 error = ENOENT;
286
287 out:
288 vrele(lvp);
289 *lvpp = NULL;
290 free(dirbuf, M_TEMP);
291 return error;
292 }
293
294 /*
295 * Look in the vnode-to-name reverse cache to see if
296 * we can find things the easy way.
297 *
298 * XXX vget failure path is untested.
299 *
300 * On entry, *lvpp is a locked vnode reference.
301 * On exit, one of the following is the case:
302 * 0) Both *lvpp and *uvpp are NULL and failure is returned.
303 * 1) *uvpp is NULL, *lvpp remains locked and -1 is returned (cache miss)
304 * 2) *uvpp is a locked vnode reference, *lvpp is vput and NULL'ed
305 * and 0 is returned (cache hit)
306 */
307
308 static int
309 getcwd_getcache(lvpp, uvpp, bpp, bufp)
310 struct vnode **lvpp, **uvpp;
311 char **bpp;
312 char *bufp;
313 {
314 struct vnode *lvp, *uvp = NULL;
315 int error;
316 int vpid;
317
318 lvp = *lvpp;
319
320 /*
321 * This returns 0 on a cache hit, -1 on a clean cache miss,
322 * or an errno on other failure.
323 */
324 error = cache_revlookup(lvp, uvpp, bpp, bufp);
325 if (error) {
326 if (error != -1) {
327 vput(lvp);
328 *lvpp = NULL;
329 *uvpp = NULL;
330 }
331 return error;
332 }
333 uvp = *uvpp;
334 vpid = uvp->v_id;
335
336 /*
337 * Since we're going up, we have to release the current lock
338 * before we take the parent lock.
339 */
340
341 VOP_UNLOCK(lvp, 0);
342
343 error = vget(uvp, LK_EXCLUSIVE | LK_RETRY);
344 if (error != 0)
345 *uvpp = NULL;
346 /*
347 * Verify that vget succeeded, and check that vnode capability
348 * didn't change while we were waiting for the lock.
349 */
350 if (error || (vpid != uvp->v_id)) {
351 /*
352 * Oops, we missed. If the vget failed, or the
353 * capability changed, try to get our lock back; if
354 * that works, tell caller to try things the hard way,
355 * otherwise give up.
356 */
357 if (!error) vput(uvp);
358 *uvpp = NULL;
359
360 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
361
362 if (!error)
363 return -1;
364 }
365 vrele(lvp);
366 *lvpp = NULL;
367
368 return error;
369 }
370
371 /*
372 * common routine shared by sys___getcwd() and vn_isunder()
373 */
374
375 #define GETCWD_CHECK_ACCESS 0x0001
376
377 int
378 getcwd_common (lvp, rvp, bpp, bufp, limit, flags, p)
379 struct vnode *lvp;
380 struct vnode *rvp;
381 char **bpp;
382 char *bufp;
383 int limit;
384 int flags;
385 struct proc *p;
386 {
387 struct cwdinfo *cwdi = p->p_cwdi;
388 struct vnode *uvp = NULL;
389 char *bp = NULL;
390 int error;
391 int perms = VEXEC;
392
393 if (rvp == NULL) {
394 rvp = cwdi->cwdi_rdir;
395 if (rvp == NULL)
396 rvp = rootvnode;
397 }
398
399 VREF(rvp);
400 VREF(lvp);
401
402 /*
403 * Error handling invariant:
404 * Before a `goto out':
405 * lvp is either NULL, or locked and held.
406 * uvp is either NULL, or locked and held.
407 */
408
409 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
410 if (error) {
411 vrele(lvp);
412 lvp = NULL;
413 goto out;
414 }
415 if (bufp)
416 bp = *bpp;
417 /*
418 * this loop will terminate when one of the following happens:
419 * - we hit the root
420 * - getdirentries or lookup fails
421 * - we run out of space in the buffer.
422 */
423 if (lvp == rvp) {
424 if (bp)
425 *(--bp) = '/';
426 goto out;
427 }
428 do {
429 if (lvp->v_type != VDIR) {
430 error = ENOTDIR;
431 goto out;
432 }
433
434 /*
435 * access check here is optional, depending on
436 * whether or not caller cares.
437 */
438 if (flags & GETCWD_CHECK_ACCESS) {
439 error = VOP_ACCESS(lvp, perms, p->p_ucred, p);
440 if (error)
441 goto out;
442 perms = VEXEC|VREAD;
443 }
444
445 /*
446 * step up if we're a covered vnode..
447 */
448 while (lvp->v_flag & VROOT) {
449 struct vnode *tvp;
450
451 if (lvp == rvp)
452 goto out;
453
454 tvp = lvp;
455 lvp = lvp->v_mount->mnt_vnodecovered;
456 vput(tvp);
457 /*
458 * hodie natus est radici frater
459 */
460 if (lvp == NULL) {
461 error = ENOENT;
462 goto out;
463 }
464 VREF(lvp);
465 error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
466 if (error != 0) {
467 vrele(lvp);
468 lvp = NULL;
469 goto out;
470 }
471 }
472 /*
473 * Look in the name cache; if that fails, look in the
474 * directory..
475 */
476 error = getcwd_getcache(&lvp, &uvp, &bp, bufp);
477 if (error == -1)
478 error = getcwd_scandir(&lvp, &uvp, &bp, bufp, p);
479 if (error)
480 goto out;
481 #if DIAGNOSTIC
482 if (lvp != NULL)
483 panic("getcwd: oops, forgot to null lvp");
484 if (bufp && (bp <= bufp)) {
485 panic("getcwd: oops, went back too far");
486 }
487 #endif
488 if (bp)
489 *(--bp) = '/';
490 lvp = uvp;
491 uvp = NULL;
492 limit--;
493 } while ((lvp != rvp) && (limit > 0));
494
495 out:
496 if (bpp)
497 *bpp = bp;
498 if (uvp)
499 vput(uvp);
500 if (lvp)
501 vput(lvp);
502 vrele(rvp);
503 return error;
504 }
505
506 /*
507 * Check if one directory can be found inside another in the directory
508 * hierarchy.
509 *
510 * Intended to be used in chroot, chdir, fchdir, etc., to ensure that
511 * chroot() actually means something.
512 */
513 int
514 vn_isunder(lvp, rvp, p)
515 struct vnode *lvp;
516 struct vnode *rvp;
517 struct proc *p;
518 {
519 int error;
520
521 error = getcwd_common (lvp, rvp, NULL, NULL, MAXPATHLEN/2, 0, p);
522
523 if (!error)
524 return 1;
525 else
526 return 0;
527 }
528
529 /*
530 * Returns true if proc p1's root directory equal to or under p2's
531 * root directory.
532 *
533 * Intended to be used from ptrace/procfs sorts of things.
534 */
535
536 int
537 proc_isunder (p1, p2)
538 struct proc *p1;
539 struct proc *p2;
540 {
541 struct vnode *r1 = p1->p_cwdi->cwdi_rdir;
542 struct vnode *r2 = p2->p_cwdi->cwdi_rdir;
543
544 if (r1 == NULL)
545 return (r2 == NULL);
546 else if (r2 == NULL)
547 return 1;
548 else
549 return vn_isunder(r1, r2, p2);
550 }
551
552 /*
553 * Find pathname of process's current directory.
554 *
555 * Use vfs vnode-to-name reverse cache; if that fails, fall back
556 * to reading directory contents.
557 */
558
559 int
560 sys___getcwd(l, v, retval)
561 struct lwp *l;
562 void *v;
563 register_t *retval;
564 {
565 struct sys___getcwd_args /* {
566 syscallarg(char *) bufp;
567 syscallarg(size_t) length;
568 } */ *uap = v;
569
570 int error;
571 char *path;
572 char *bp, *bend;
573 int len = SCARG(uap, length);
574 int lenused;
575
576 if (len > MAXPATHLEN*4)
577 len = MAXPATHLEN*4;
578 else if (len < 2)
579 return ERANGE;
580
581 path = (char *)malloc(len, M_TEMP, M_WAITOK);
582 if (!path)
583 return ENOMEM;
584
585 bp = &path[len];
586 bend = bp;
587 *(--bp) = '\0';
588
589 /*
590 * 5th argument here is "max number of vnodes to traverse".
591 * Since each entry takes up at least 2 bytes in the output buffer,
592 * limit it to N/2 vnodes for an N byte buffer.
593 */
594 error = getcwd_common (l->l_proc->p_cwdi->cwdi_cdir, NULL, &bp, path,
595 len/2, GETCWD_CHECK_ACCESS, l->l_proc);
596
597 if (error)
598 goto out;
599 lenused = bend - bp;
600 *retval = lenused;
601 /* put the result into user buffer */
602 error = copyout(bp, SCARG(uap, bufp), lenused);
603
604 out:
605 free(path, M_TEMP);
606 return error;
607 }
608