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