linux_getcwd.c revision 276654
182518Sgallatin/* $OpenBSD: linux_getcwd.c,v 1.2 2001/05/16 12:50:21 ho Exp $ */
282518Sgallatin/* $NetBSD: vfs_getcwd.c,v 1.3.2.3 1999/07/11 10:24:09 sommerfeld Exp $ */
382518Sgallatin/*-
482518Sgallatin * Copyright (c) 1999 The NetBSD Foundation, Inc.
582518Sgallatin * All rights reserved.
682518Sgallatin *
782518Sgallatin * This code is derived from software contributed to The NetBSD Foundation
882518Sgallatin * by Bill Sommerfeld.
982518Sgallatin *
1082518Sgallatin * Redistribution and use in source and binary forms, with or without
1182518Sgallatin * modification, are permitted provided that the following conditions
1282518Sgallatin * are met:
1382518Sgallatin * 1. Redistributions of source code must retain the above copyright
1482518Sgallatin *    notice, this list of conditions and the following disclaimer.
1582518Sgallatin * 2. Redistributions in binary form must reproduce the above copyright
1682518Sgallatin *    notice, this list of conditions and the following disclaimer in the
1782518Sgallatin *    documentation and/or other materials provided with the distribution.
1882518Sgallatin *
1982518Sgallatin * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2082518Sgallatin * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2182518Sgallatin * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2282518Sgallatin * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2382518Sgallatin * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2482518Sgallatin * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2582518Sgallatin * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2682518Sgallatin * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2782518Sgallatin * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2882518Sgallatin * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2982518Sgallatin * POSSIBILITY OF SUCH DAMAGE.
3082518Sgallatin */
31116173Sobrien
32116173Sobrien#include <sys/cdefs.h>
33116173Sobrien__FBSDID("$FreeBSD: head/sys/compat/linux/linux_getcwd.c 276654 2015-01-04 10:34:02Z dchagin $");
34116173Sobrien
35156874Sru#include "opt_compat.h"
3682518Sgallatin
3782518Sgallatin#include <sys/param.h>
3882518Sgallatin#include <sys/systm.h>
39112430Sphk#include <sys/namei.h>
40112430Sphk#include <sys/filedesc.h>
41112430Sphk#include <sys/kernel.h>
42112430Sphk#include <sys/file.h>
43112430Sphk#include <sys/stat.h>
44102872Siedowse#include <sys/syscallsubr.h>
45112430Sphk#include <sys/vnode.h>
46112430Sphk#include <sys/mount.h>
4782518Sgallatin#include <sys/proc.h>
48112430Sphk#include <sys/uio.h>
49112430Sphk#include <sys/malloc.h>
50112430Sphk#include <sys/dirent.h>
51112430Sphk#include <ufs/ufs/dir.h>	/* XXX only for DIRBLKSIZ */
5282518Sgallatin
53140214Sobrien#ifdef COMPAT_LINUX32
54140214Sobrien#include <machine/../linux32/linux.h>
55140214Sobrien#include <machine/../linux32/linux32_proto.h>
56140214Sobrien#else
5782518Sgallatin#include <machine/../linux/linux.h>
5882518Sgallatin#include <machine/../linux/linux_proto.h>
59133816Stjr#endif
60112430Sphk#include <compat/linux/linux_util.h>
6182518Sgallatin
62163606Srwatson#include <security/mac/mac_framework.h>
63163606Srwatson
64112430Sphkstatic int
65112430Sphklinux_getcwd_scandir(struct vnode **, struct vnode **,
66112430Sphk    char **, char *, struct thread *);
67112430Sphkstatic int
68112430Sphklinux_getcwd_common(struct vnode *, struct vnode *,
69112430Sphk		   char **, char *, int, int, struct thread *);
70112430Sphk
71112430Sphk#define DIRENT_MINSIZE (sizeof(struct dirent) - (MAXNAMLEN+1) + 4)
72112430Sphk
7382518Sgallatin/*
74112430Sphk * Vnode variable naming conventions in this file:
75112430Sphk *
76112430Sphk * rvp: the current root we're aiming towards.
77112430Sphk * lvp, *lvpp: the "lower" vnode
78112430Sphk * uvp, *uvpp: the "upper" vnode.
79112430Sphk *
80112430Sphk * Since all the vnodes we're dealing with are directories, and the
81112430Sphk * lookups are going *up* in the filesystem rather than *down*, the
82112430Sphk * usual "pvp" (parent) or "dvp" (directory) naming conventions are
83112430Sphk * too confusing.
84112430Sphk */
85112430Sphk
86112430Sphk/*
87112430Sphk * XXX Will infinite loop in certain cases if a directory read reliably
88112430Sphk *	returns EINVAL on last block.
89112430Sphk * XXX is EINVAL the right thing to return if a directory is malformed?
90112430Sphk */
91112430Sphk
92112430Sphk/*
93112430Sphk * XXX Untested vs. mount -o union; probably does the wrong thing.
94112430Sphk */
95112430Sphk
96112430Sphk/*
97112430Sphk * Find parent vnode of *lvpp, return in *uvpp
98112430Sphk *
99112430Sphk * If we care about the name, scan it looking for name of directory
100112430Sphk * entry pointing at lvp.
101112430Sphk *
102112430Sphk * Place the name in the buffer which starts at bufp, immediately
103112430Sphk * before *bpp, and move bpp backwards to point at the start of it.
104112430Sphk *
105112430Sphk * On entry, *lvpp is a locked vnode reference; on exit, it is vput and NULL'ed
106112430Sphk * On exit, *uvpp is either NULL or is a locked vnode reference.
107112430Sphk */
108112430Sphkstatic int
109112430Sphklinux_getcwd_scandir(lvpp, uvpp, bpp, bufp, td)
110112430Sphk	struct vnode **lvpp;
111112430Sphk	struct vnode **uvpp;
112112430Sphk	char **bpp;
113112430Sphk	char *bufp;
114112430Sphk	struct thread *td;
115112430Sphk{
116112430Sphk	int     error = 0;
117112430Sphk	int     eofflag;
118112430Sphk	off_t   off;
119112430Sphk	int     tries;
120112430Sphk	struct uio uio;
121112430Sphk	struct iovec iov;
122112430Sphk	char   *dirbuf = NULL;
123112430Sphk	int	dirbuflen;
124112430Sphk	ino_t   fileno;
125112430Sphk	struct vattr va;
126112430Sphk	struct vnode *uvp = NULL;
127112430Sphk	struct vnode *lvp = *lvpp;
128112430Sphk	struct componentname cn;
129112430Sphk	int len, reclen;
130112430Sphk	tries = 0;
131112430Sphk
132112430Sphk	/*
133112430Sphk	 * If we want the filename, get some info we need while the
134112430Sphk	 * current directory is still locked.
135112430Sphk	 */
136112430Sphk	if (bufp != NULL) {
137182371Sattilio		error = VOP_GETATTR(lvp, &va, td->td_ucred);
138112430Sphk		if (error) {
139112430Sphk			vput(lvp);
140112430Sphk			*lvpp = NULL;
141112430Sphk			*uvpp = NULL;
142112430Sphk			return error;
143112430Sphk		}
144112430Sphk	}
145112430Sphk
146112430Sphk	/*
147112430Sphk	 * Ok, we have to do it the hard way..
148112430Sphk	 * Next, get parent vnode using lookup of ..
149112430Sphk	 */
150112430Sphk	cn.cn_nameiop = LOOKUP;
151112430Sphk	cn.cn_flags = ISLASTCN | ISDOTDOT | RDONLY;
152112430Sphk	cn.cn_thread = td;
153112430Sphk	cn.cn_cred = td->td_ucred;
154112430Sphk	cn.cn_pnbuf = NULL;
155112430Sphk	cn.cn_nameptr = "..";
156112430Sphk	cn.cn_namelen = 2;
157112430Sphk	cn.cn_consume = 0;
158188588Sjhb	cn.cn_lkflags = LK_SHARED;
159112430Sphk
160112430Sphk	/*
161112430Sphk	 * At this point, lvp is locked and will be unlocked by the lookup.
162112430Sphk	 * On successful return, *uvpp will be locked
163112430Sphk	 */
164122861Srwatson#ifdef MAC
165172930Srwatson	error = mac_vnode_check_lookup(td->td_ucred, lvp, &cn);
166122861Srwatson	if (error == 0)
167122861Srwatson#endif
168122861Srwatson		error = VOP_LOOKUP(lvp, uvpp, &cn);
169112430Sphk	if (error) {
170112430Sphk		vput(lvp);
171112430Sphk		*lvpp = NULL;
172112430Sphk		*uvpp = NULL;
173112430Sphk		return error;
174112430Sphk	}
175112430Sphk	uvp = *uvpp;
176112430Sphk
177112430Sphk	/* If we don't care about the pathname, we're done */
178112430Sphk	if (bufp == NULL) {
179145006Sjeff		vput(lvp);
180112430Sphk		*lvpp = NULL;
181112430Sphk		return 0;
182112430Sphk	}
183112430Sphk
184112430Sphk	fileno = va.va_fileid;
185112430Sphk
186112430Sphk	dirbuflen = DIRBLKSIZ;
187112430Sphk	if (dirbuflen < va.va_blocksize)
188112430Sphk		dirbuflen = va.va_blocksize;
189276654Sdchagin	dirbuf = malloc(dirbuflen, M_TEMP, M_WAITOK);
190112430Sphk
191112430Sphk#if 0
192112430Sphkunionread:
193112430Sphk#endif
194112430Sphk	off = 0;
195112430Sphk	do {
196112430Sphk		/* call VOP_READDIR of parent */
197112430Sphk		iov.iov_base = dirbuf;
198112430Sphk		iov.iov_len = dirbuflen;
199112430Sphk
200112430Sphk		uio.uio_iov = &iov;
201112430Sphk		uio.uio_iovcnt = 1;
202112430Sphk		uio.uio_offset = off;
203112430Sphk		uio.uio_resid = dirbuflen;
204112430Sphk		uio.uio_segflg = UIO_SYSSPACE;
205112430Sphk		uio.uio_rw = UIO_READ;
206112430Sphk		uio.uio_td = td;
207112430Sphk
208112430Sphk		eofflag = 0;
209112430Sphk
210112430Sphk#ifdef MAC
211172930Srwatson		error = mac_vnode_check_readdir(td->td_ucred, uvp);
212112430Sphk		if (error == 0)
213112430Sphk#endif /* MAC */
214112430Sphk			error = VOP_READDIR(uvp, &uio, td->td_ucred, &eofflag,
215112430Sphk			    0, 0);
216112430Sphk
217112430Sphk		off = uio.uio_offset;
218112430Sphk
219112430Sphk		/*
220112430Sphk		 * Try again if NFS tosses its cookies.
221112430Sphk		 * XXX this can still loop forever if the directory is busted
222112430Sphk		 * such that the second or subsequent page of it always
223112430Sphk		 * returns EINVAL
224112430Sphk		 */
225112430Sphk		if ((error == EINVAL) && (tries < 3)) {
226112430Sphk			off = 0;
227112430Sphk			tries++;
228112430Sphk			continue;	/* once more, with feeling */
229112430Sphk		}
230112430Sphk
231112430Sphk		if (!error) {
232112430Sphk			char   *cpos;
233112430Sphk			struct dirent *dp;
234112430Sphk
235112430Sphk			cpos = dirbuf;
236112430Sphk			tries = 0;
237112430Sphk
238112430Sphk			/* scan directory page looking for matching vnode */
239112430Sphk			for (len = (dirbuflen - uio.uio_resid); len > 0; len -= reclen) {
240112430Sphk				dp = (struct dirent *) cpos;
241112430Sphk				reclen = dp->d_reclen;
242112430Sphk
243112430Sphk				/* check for malformed directory.. */
244112430Sphk				if (reclen < DIRENT_MINSIZE) {
245112430Sphk					error = EINVAL;
246112430Sphk					goto out;
247112430Sphk				}
248112430Sphk				/*
249112430Sphk				 * XXX should perhaps do VOP_LOOKUP to
250112430Sphk				 * check that we got back to the right place,
251112430Sphk				 * but getting the locking games for that
252112430Sphk				 * right would be heinous.
253112430Sphk				 */
254112430Sphk				if ((dp->d_type != DT_WHT) &&
255112430Sphk				    (dp->d_fileno == fileno)) {
256112430Sphk					char *bp = *bpp;
257112430Sphk					bp -= dp->d_namlen;
258112430Sphk
259112430Sphk					if (bp <= bufp) {
260112430Sphk						error = ERANGE;
261112430Sphk						goto out;
262112430Sphk					}
263112430Sphk					bcopy(dp->d_name, bp, dp->d_namlen);
264112430Sphk					error = 0;
265112430Sphk					*bpp = bp;
266112430Sphk					goto out;
267112430Sphk				}
268112430Sphk				cpos += reclen;
269112430Sphk			}
270112430Sphk		}
271112430Sphk	} while (!eofflag);
272112430Sphk	error = ENOENT;
273112430Sphk
274112430Sphkout:
275145006Sjeff	vput(lvp);
276112430Sphk	*lvpp = NULL;
277112430Sphk	free(dirbuf, M_TEMP);
278112430Sphk	return error;
279112430Sphk}
280112430Sphk
281112430Sphk
282112430Sphk/*
283112430Sphk * common routine shared by sys___getcwd() and linux_vn_isunder()
284112430Sphk */
285112430Sphk
286112430Sphk#define GETCWD_CHECK_ACCESS 0x0001
287112430Sphk
288112430Sphkstatic int
289112430Sphklinux_getcwd_common (lvp, rvp, bpp, bufp, limit, flags, td)
290112430Sphk	struct vnode *lvp;
291112430Sphk	struct vnode *rvp;
292112430Sphk	char **bpp;
293112430Sphk	char *bufp;
294112430Sphk	int limit;
295112430Sphk	int flags;
296112430Sphk	struct thread *td;
297112430Sphk{
298112430Sphk	struct filedesc *fdp = td->td_proc->p_fd;
299112430Sphk	struct vnode *uvp = NULL;
300112430Sphk	char *bp = NULL;
301112430Sphk	int error;
302184413Strasz	accmode_t accmode = VEXEC;
303112430Sphk
304112430Sphk	if (rvp == NULL) {
305112430Sphk		rvp = fdp->fd_rdir;
306112430Sphk		if (rvp == NULL)
307112430Sphk			rvp = rootvnode;
308112430Sphk	}
309112430Sphk
310112430Sphk	VREF(rvp);
311112430Sphk	VREF(lvp);
312112430Sphk
313112430Sphk	/*
314112430Sphk	 * Error handling invariant:
315112430Sphk	 * Before a `goto out':
316112430Sphk	 *	lvp is either NULL, or locked and held.
317112430Sphk	 *	uvp is either NULL, or locked and held.
318112430Sphk	 */
319112430Sphk
320175202Sattilio	error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
321155033Sjeff	if (error != 0)
322155033Sjeff		panic("vn_lock LK_RETRY returned error %d", error);
323112430Sphk	if (bufp)
324112430Sphk		bp = *bpp;
325112430Sphk	/*
326112430Sphk	 * this loop will terminate when one of the following happens:
327112430Sphk	 *	- we hit the root
328112430Sphk	 *	- getdirentries or lookup fails
329112430Sphk	 *	- we run out of space in the buffer.
330112430Sphk	 */
331112430Sphk	if (lvp == rvp) {
332112430Sphk		if (bp)
333112430Sphk			*(--bp) = '/';
334112430Sphk		goto out;
335112430Sphk	}
336112430Sphk	do {
337112430Sphk		if (lvp->v_type != VDIR) {
338112430Sphk			error = ENOTDIR;
339112430Sphk			goto out;
340112430Sphk		}
341112430Sphk
342112430Sphk		/*
343112430Sphk		 * access check here is optional, depending on
344112430Sphk		 * whether or not caller cares.
345112430Sphk		 */
346112430Sphk		if (flags & GETCWD_CHECK_ACCESS) {
347184413Strasz			error = VOP_ACCESS(lvp, accmode, td->td_ucred, td);
348112430Sphk			if (error)
349112430Sphk				goto out;
350184413Strasz			accmode = VEXEC|VREAD;
351112430Sphk		}
352112430Sphk
353112430Sphk		/*
354112430Sphk		 * step up if we're a covered vnode..
355112430Sphk		 */
356112430Sphk		while (lvp->v_vflag & VV_ROOT) {
357112430Sphk			struct vnode *tvp;
358112430Sphk
359112430Sphk			if (lvp == rvp)
360112430Sphk				goto out;
361112430Sphk
362112430Sphk			tvp = lvp;
363112430Sphk			lvp = lvp->v_mount->mnt_vnodecovered;
364112430Sphk			vput(tvp);
365112430Sphk			/*
366112430Sphk			 * hodie natus est radici frater
367112430Sphk			 */
368112430Sphk			if (lvp == NULL) {
369112430Sphk				error = ENOENT;
370112430Sphk				goto out;
371112430Sphk			}
372112430Sphk			VREF(lvp);
373175202Sattilio			error = vn_lock(lvp, LK_EXCLUSIVE | LK_RETRY);
374155033Sjeff			if (error != 0)
375155033Sjeff				panic("vn_lock LK_RETRY returned %d", error);
376112430Sphk		}
377112430Sphk		error = linux_getcwd_scandir(&lvp, &uvp, &bp, bufp, td);
378112430Sphk		if (error)
379112430Sphk			goto out;
380153072Sru#ifdef DIAGNOSTIC
381112430Sphk		if (lvp != NULL)
382112430Sphk			panic("getcwd: oops, forgot to null lvp");
383112430Sphk		if (bufp && (bp <= bufp)) {
384112430Sphk			panic("getcwd: oops, went back too far");
385112430Sphk		}
386112430Sphk#endif
387112430Sphk		if (bp)
388112430Sphk			*(--bp) = '/';
389112430Sphk		lvp = uvp;
390112430Sphk		uvp = NULL;
391112430Sphk		limit--;
392112430Sphk	} while ((lvp != rvp) && (limit > 0));
393112430Sphk
394112430Sphkout:
395112430Sphk	if (bpp)
396112430Sphk		*bpp = bp;
397112430Sphk	if (uvp)
398112430Sphk		vput(uvp);
399112430Sphk	if (lvp)
400112430Sphk		vput(lvp);
401112430Sphk	vrele(rvp);
402112430Sphk	return error;
403112430Sphk}
404112430Sphk
405112430Sphk
406112430Sphk/*
40782518Sgallatin * Find pathname of process's current directory.
40882518Sgallatin *
40982518Sgallatin * Use vfs vnode-to-name reverse cache; if that fails, fall back
410112430Sphk * to reading directory contents.
41182518Sgallatin */
41282518Sgallatin
41382518Sgallatinint
41483366Sjulianlinux_getcwd(struct thread *td, struct linux_getcwd_args *args)
41582518Sgallatin{
416276654Sdchagin	char *bp, *bend, *path;
417112430Sphk	int error, len, lenused;
418112430Sphk
419112430Sphk#ifdef DEBUG
420164379Skib	if (ldebug(getcwd))
421164383Skib		printf(ARGS(getcwd, "%p, %ld"), args->buf, (long)args->bufsize);
422112430Sphk#endif
423112430Sphk
424112430Sphk	len = args->bufsize;
425112430Sphk
426112430Sphk	if (len > MAXPATHLEN*4)
427112430Sphk		len = MAXPATHLEN*4;
428112430Sphk	else if (len < 2)
429112430Sphk		return ERANGE;
430112430Sphk
431276654Sdchagin	path = malloc(len, M_TEMP, M_WAITOK);
432112430Sphk
433276654Sdchagin	error = kern___getcwd(td, path, UIO_SYSSPACE, len);
434112430Sphk	if (!error) {
435112430Sphk		lenused = strlen(path) + 1;
436112430Sphk		if (lenused <= args->bufsize) {
437112430Sphk			td->td_retval[0] = lenused;
438112430Sphk			error = copyout(path, args->buf, lenused);
439112430Sphk		}
440112430Sphk		else
441112430Sphk			error = ERANGE;
442112430Sphk	} else {
443112430Sphk		bp = &path[len];
444112430Sphk		bend = bp;
445112430Sphk		*(--bp) = '\0';
446112430Sphk
447112430Sphk		/*
448112430Sphk		 * 5th argument here is "max number of vnodes to traverse".
449112430Sphk		 * Since each entry takes up at least 2 bytes in the output buffer,
450112430Sphk		 * limit it to N/2 vnodes for an N byte buffer.
451112430Sphk		 */
452112430Sphk
453112430Sphk		error = linux_getcwd_common (td->td_proc->p_fd->fd_cdir, NULL,
454112430Sphk		    &bp, path, len/2, GETCWD_CHECK_ACCESS, td);
455112430Sphk		if (error)
456112430Sphk			goto out;
457112430Sphk		lenused = bend - bp;
458112430Sphk		td->td_retval[0] = lenused;
459112430Sphk		/* put the result into user buffer */
460112430Sphk		error = copyout(bp, args->buf, lenused);
461112430Sphk	}
462112430Sphkout:
463112430Sphk	free(path, M_TEMP);
464112430Sphk	return (error);
46582518Sgallatin}
46682518Sgallatin
467