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