From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:59430) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UjSfW-0006DM-Lw for qemu-devel@nongnu.org; Mon, 03 Jun 2013 07:11:20 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1UjSfR-0003dO-RH for qemu-devel@nongnu.org; Mon, 03 Jun 2013 07:11:18 -0400 Received: from moutng.kundenserver.de ([212.227.17.9]:64636) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1UjSfR-0003dB-EV for qemu-devel@nongnu.org; Mon, 03 Jun 2013 07:11:13 -0400 Date: Mon, 3 Jun 2013 13:11:00 +0200 (CEST) From: Laurent Vivier Message-ID: <214105268.35618.1370257860687.open-xchange@ox-webdesk.1and1.fr> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_Part_35617_1198013401.1370257860614" Subject: Re: [Qemu-devel] [PATCH] linux-user: Allow getdents to be provided by getdents64 Reply-To: Laurent Vivier List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org, Peter Maydell Cc: Riku Voipio , Claudio Fontana , patches@linaro.org ------=_Part_35617_1198013401.1370257860614 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Tested on m68k on x86_64 as described in the patch comment, in a a debian-e= tch linux container. Works fine, except the drec_len differs between getdents() and getdents64()= . See comment below. > Le 2 juin 2013 =C3=A0 19:10, Peter Maydell a = =C3=A9crit : > > > Newer architectures may only implement the getdents64 syscall, not > getdents. Provide an implementation of getdents in terms of getdents64 > so that we can run getdents-using targets on a getdents64-only host. > > Signed-off-by: Peter Maydell > --- > Guess which exciting new architecture doesn't have getdents :-) > Tested on i386 by temporarily nobbling the #ifdefs so we use the > via-getdents64 path rather than via-getdents, and with the test program > from the getdents(2) manpage as the guest binary. > > linux-user/syscall.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++= ++- > 1 file changed, 59 insertions(+), 1 deletion(-) > > diff --git a/linux-user/syscall.c b/linux-user/syscall.c > index 053fa14..15c771a 100644 > --- a/linux-user/syscall.c > +++ b/linux-user/syscall.c > @@ -208,8 +208,11 @@ static int gettid(void) { > return -ENOSYS; > } > #endif > +#ifdef __NR_getdents > _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, > count); > -#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) > +#endif > +#if !defined(__NR_getdents) || \ > + (defined(TARGET_NR_getdents64) && defined(__NR_getdents64)) > _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, u= int, > count); > #endif > #if defined(TARGET_NR__llseek) && defined(__NR_llseek) > @@ -6963,6 +6966,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_lon= g > arg1, > break; > #endif > case TARGET_NR_getdents: > +#ifdef __NR_getdents > #if TARGET_ABI_BITS =3D=3D 32 && HOST_LONG_BITS =3D=3D 64 > { > struct target_dirent *target_dirp; > @@ -7035,6 +7039,60 @@ abi_long do_syscall(void *cpu_env, int num, abi_lo= ng > arg1, > unlock_user(dirp, arg2, ret); > } > #endif > +#else > + /* Implement getdents in terms of getdents64 */ > + { > + struct linux_dirent64 *dirp; > + abi_long count =3D arg3; > + > + dirp =3D lock_user(VERIFY_WRITE, arg2, count, 0); > + if (!dirp) { > + goto efault; > + } > + ret =3D get_errno(sys_getdents64(arg1, dirp, count)); > + if (!is_error(ret)) { > + /* Convert the dirent64 structs to target dirent. We do this > + * in-place, since we can guarantee that a target_dirent is no > + * larger than a dirent64; however this means we have to be > + * careful to read everything before writing in the new format. > + */ > + struct linux_dirent64 *de; > + struct target_dirent *tde; > + int len =3D ret; > + int tlen =3D 0; > + > + de =3D dirp; > + tde =3D (struct target_dirent *)dirp; > + while (len > 0) { > + int namelen, treclen; > + int reclen =3D de->d_reclen; > + uint64_t ino =3D de->d_ino; > + int64_t off =3D de->d_off; > + uint8_t type =3D de->d_type; > + > + namelen =3D strlen(de->d_name); > + treclen =3D offsetof(struct target_dirent, d_name) + namelen + 2; > + treclen =3D QEMU_ALIGN_UP(treclen, sizeof(abi_long)); You should use ABI_LONG_ALIGNMENT instead of sizeof(abi_long), as some targ= ets (at least m68k) don't align on long size. > + > + tde->d_ino =3D tswapal(ino); > + tde->d_off =3D tswapal(off); > + tde->d_reclen =3D tswap16(treclen); > + memmove(tde->d_name, de->d_name, namelen + 1); > + /* The target_dirent type is in what was formerly a padding > + * byte at the end of the structure: > + */ > + *(((char *)tde) + treclen - 1) =3D type; > + > + de =3D (struct linux_dirent64 *)((char *)de + reclen); > + tde =3D (struct target_dirent *)((char *)tde + treclen); > + len -=3D reclen; > + tlen +=3D treclen; > + } > + ret =3D tlen; > + } > + unlock_user(dirp, arg2, ret); > + } > +#endif > break; > #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64) > case TARGET_NR_getdents64: ------=_Part_35617_1198013401.1370257860614 MIME-Version: 1.0 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: 7bit
Tested on m68k on x86_64 as described in the patch comment, in a a debian-etch linux container.
 
Works fine, except the drec_len differs between getdents() and getdents64().
 
See comment below.
 
> Le 2 juin 2013 à 19:10, Peter Maydell <peter.maydell@linaro.org> a écrit :
>
>
> Newer architectures may only implement the getdents64 syscall, not
> getdents. Provide an implementation of getdents in terms of getdents64
> so that we can run getdents-using targets on a getdents64-only host.
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> Guess which exciting new architecture doesn't have getdents :-)
> Tested on i386 by temporarily nobbling the #ifdefs so we use the
> via-getdents64 path rather than via-getdents, and with the test program
> from the getdents(2) manpage as the guest binary.
>
> linux-user/syscall.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++-
> 1 file changed, 59 insertions(+), 1 deletion(-)
>
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index 053fa14..15c771a 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -208,8 +208,11 @@ static int gettid(void) {
> return -ENOSYS;
> }
> #endif
> +#ifdef __NR_getdents
> _syscall3(int, sys_getdents, uint, fd, struct linux_dirent *, dirp, uint, count);
> -#if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
> +#endif
> +#if !defined(__NR_getdents) || \
> + (defined(TARGET_NR_getdents64) && defined(__NR_getdents64))
> _syscall3(int, sys_getdents64, uint, fd, struct linux_dirent64 *, dirp, uint, count);
> #endif
> #if defined(TARGET_NR__llseek) && defined(__NR_llseek)
> @@ -6963,6 +6966,7 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
> break;
> #endif
> case TARGET_NR_getdents:
> +#ifdef __NR_getdents
> #if TARGET_ABI_BITS == 32 && HOST_LONG_BITS == 64
> {
> struct target_dirent *target_dirp;
> @@ -7035,6 +7039,60 @@ abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
> unlock_user(dirp, arg2, ret);
> }
> #endif
> +#else
> + /* Implement getdents in terms of getdents64 */
> + {
> + struct linux_dirent64 *dirp;
> + abi_long count = arg3;
> +
> + dirp = lock_user(VERIFY_WRITE, arg2, count, 0);
> + if (!dirp) {
> + goto efault;
> + }
> + ret = get_errno(sys_getdents64(arg1, dirp, count));
> + if (!is_error(ret)) {
> + /* Convert the dirent64 structs to target dirent. We do this
> + * in-place, since we can guarantee that a target_dirent is no
> + * larger than a dirent64; however this means we have to be
> + * careful to read everything before writing in the new format.
> + */
> + struct linux_dirent64 *de;
> + struct target_dirent *tde;
> + int len = ret;
> + int tlen = 0;
> +
> + de = dirp;
> + tde = (struct target_dirent *)dirp;
> + while (len > 0) {
> + int namelen, treclen;
> + int reclen = de->d_reclen;
> + uint64_t ino = de->d_ino;
> + int64_t off = de->d_off;
> + uint8_t type = de->d_type;
> +
> + namelen = strlen(de->d_name);
> + treclen = offsetof(struct target_dirent, d_name) + namelen + 2;
> + treclen = QEMU_ALIGN_UP(treclen, sizeof(abi_long));
 
You should use ABI_LONG_ALIGNMENT instead of sizeof(abi_long), as some targets (at least m68k) don't align on long size.

> +
> + tde->d_ino = tswapal(ino);
> + tde->d_off = tswapal(off);
> + tde->d_reclen = tswap16(treclen);
> + memmove(tde->d_name, de->d_name, namelen + 1);
> + /* The target_dirent type is in what was formerly a padding
> + * byte at the end of the structure:
> + */
> + *(((char *)tde) + treclen - 1) = type;
> +
> + de = (struct linux_dirent64 *)((char *)de + reclen);
> + tde = (struct target_dirent *)((char *)tde + treclen);
> + len -= reclen;
> + tlen += treclen;
> + }
> + ret = tlen;
> + }
> + unlock_user(dirp, arg2, ret);
> + }
> +#endif
> break;
> #if defined(TARGET_NR_getdents64) && defined(__NR_getdents64)
> case TARGET_NR_getdents64:
------=_Part_35617_1198013401.1370257860614--