diff mbox

linux-user: add name_to_handle_at/open_by_handle_at

Message ID 1440714454-29878-1-git-send-email-laurent@vivier.eu
State New
Headers show

Commit Message

Laurent Vivier Aug. 27, 2015, 10:27 p.m. UTC
This patch allows to run example given by open_by_handle_at(2):

      The following shell session demonstrates the use of these two programs:

           $ echo 'Can you please think about it?' > cecilia.txt
           $ ./t_name_to_handle_at cecilia.txt > fh
           $ ./t_open_by_handle_at < fh
           open_by_handle_at: Operation not permitted
           $ sudo ./t_open_by_handle_at < fh      # Need CAP_SYS_ADMIN
           Read 31 bytes
           $ rm cecilia.txt

       Now  we delete and (quickly) re-create the file so that it has the same
       content and (by chance) the  same  inode.[...]

           $ stat --printf="%i\n" cecilia.txt     # Display inode number
           4072121
           $ rm cecilia.txt
           $ echo 'Can you please think about it?' > cecilia.txt
           $ stat --printf="%i\n" cecilia.txt     # Check inode number
           4072121
           $ sudo ./t_open_by_handle_at < fh
           open_by_handle_at: Stale NFS file handle

See the man page for source code.

Signed-off-by: Laurent Vivier <laurent@vivier.eu>
---
 linux-user/syscall.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 97 insertions(+)

Comments

Peter Maydell Sept. 1, 2015, 11:07 a.m. UTC | #1
On 27 August 2015 at 23:27, Laurent Vivier <laurent@vivier.eu> wrote:
> This patch allows to run example given by open_by_handle_at(2):
>
>       The following shell session demonstrates the use of these two programs:
>
>            $ echo 'Can you please think about it?' > cecilia.txt
>            $ ./t_name_to_handle_at cecilia.txt > fh
>            $ ./t_open_by_handle_at < fh
>            open_by_handle_at: Operation not permitted
>            $ sudo ./t_open_by_handle_at < fh      # Need CAP_SYS_ADMIN
>            Read 31 bytes
>            $ rm cecilia.txt
>
>        Now  we delete and (quickly) re-create the file so that it has the same
>        content and (by chance) the  same  inode.[...]
>
>            $ stat --printf="%i\n" cecilia.txt     # Display inode number
>            4072121
>            $ rm cecilia.txt
>            $ echo 'Can you please think about it?' > cecilia.txt
>            $ stat --printf="%i\n" cecilia.txt     # Check inode number
>            4072121
>            $ sudo ./t_open_by_handle_at < fh
>            open_by_handle_at: Stale NFS file handle
>
> See the man page for source code.
>
> Signed-off-by: Laurent Vivier <laurent@vivier.eu>
> ---
>  linux-user/syscall.c | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 97 insertions(+)
>
> diff --git a/linux-user/syscall.c b/linux-user/syscall.c
> index f62c698..725ed66 100644
> --- a/linux-user/syscall.c
> +++ b/linux-user/syscall.c
> @@ -5246,6 +5246,93 @@ static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
>          return -TARGET_ENOSYS;
>      }
>  }
> +#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
> +static abi_long do_name_to_handle_at(abi_long arg1, abi_long arg2,
> +                                     abi_long arg3, abi_long arg4,
> +                                     abi_long arg5)

Since this is in its own function you have the opportunity to
give the input parameters more meaningful names than arg1..arg5.

> +{
> +    struct file_handle *target_fh;
> +    struct file_handle *fh;
> +    int mount_id = 0;
> +    abi_long ret;
> +    char *name;
> +    unsigned int size;
> +
> +    if (get_user_s32(size, arg3)) {
> +        return -TARGET_EFAULT;
> +    }
> +
> +    name = lock_user_string(arg2);
> +    if (!name) {
> +        return -TARGET_EFAULT;
> +    }
> +
> +    target_fh = lock_user(VERIFY_WRITE, arg3,
> +                          sizeof(struct file_handle) + size, 0);
> +    if (!target_fh) {
> +        unlock_user(name, arg2, 0);
> +        return -TARGET_EFAULT;
> +    }
> +
> +    fh = g_malloc0(sizeof(struct file_handle) + size);
> +    fh->handle_bytes = size;

I was going to suggest just using target_fh, since we know
the host and guest have the same sized struct here. But I
guess they might have different alignment restrictions.

> +
> +    ret = get_errno(name_to_handle_at(arg1, path(name), fh, &mount_id, arg5));
> +    unlock_user(name, arg2, 0);
> +
> +    /* man name_to_handle_at(2):
> +     * Other than the use of the handle_bytes field, the caller should treat
> +     * the file_handle structure as an opaque data type
> +     */
> +
> +    memcpy(target_fh, fh, fh->handle_bytes);
> +    target_fh->handle_bytes = tswap32(fh->handle_bytes);

You need to swap the handle_type field too (to match the
swap you do in do_open_by_handle_at()).
(I think swapping in both places is better than doing so in
neither.)

Otherwise looks good.

thanks
-- PMM
diff mbox

Patch

diff --git a/linux-user/syscall.c b/linux-user/syscall.c
index f62c698..725ed66 100644
--- a/linux-user/syscall.c
+++ b/linux-user/syscall.c
@@ -5246,6 +5246,93 @@  static int do_futex(target_ulong uaddr, int op, int val, target_ulong timeout,
         return -TARGET_ENOSYS;
     }
 }
+#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
+static abi_long do_name_to_handle_at(abi_long arg1, abi_long arg2,
+                                     abi_long arg3, abi_long arg4,
+                                     abi_long arg5)
+{
+    struct file_handle *target_fh;
+    struct file_handle *fh;
+    int mount_id = 0;
+    abi_long ret;
+    char *name;
+    unsigned int size;
+
+    if (get_user_s32(size, arg3)) {
+        return -TARGET_EFAULT;
+    }
+
+    name = lock_user_string(arg2);
+    if (!name) {
+        return -TARGET_EFAULT;
+    }
+
+    target_fh = lock_user(VERIFY_WRITE, arg3,
+                          sizeof(struct file_handle) + size, 0);
+    if (!target_fh) {
+        unlock_user(name, arg2, 0);
+        return -TARGET_EFAULT;
+    }
+
+    fh = g_malloc0(sizeof(struct file_handle) + size);
+    fh->handle_bytes = size;
+
+    ret = get_errno(name_to_handle_at(arg1, path(name), fh, &mount_id, arg5));
+    unlock_user(name, arg2, 0);
+
+    /* man name_to_handle_at(2):
+     * Other than the use of the handle_bytes field, the caller should treat
+     * the file_handle structure as an opaque data type
+     */
+
+    memcpy(target_fh, fh, fh->handle_bytes);
+    target_fh->handle_bytes = tswap32(fh->handle_bytes);
+    g_free(fh);
+    unlock_user(target_fh, arg3, size);
+
+    if (put_user_s32(mount_id, arg4)) {
+        return -TARGET_EFAULT;
+    }
+
+    return ret;
+
+}
+#endif
+
+#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
+static abi_long do_open_by_handle_at(abi_long arg1, abi_long arg2,
+                                     abi_long arg3)
+{
+    struct file_handle *target_fh;
+    struct file_handle *fh;
+    unsigned int size;
+    abi_long ret;
+
+    if (get_user_s32(size, arg2)) {
+        return -TARGET_EFAULT;
+    }
+
+    target_fh = lock_user(VERIFY_READ, arg2,
+                          sizeof(struct file_handle) + size, 1);
+    if (!target_fh) {
+        return -TARGET_EFAULT;
+    }
+
+    fh = g_malloc0(sizeof(struct file_handle) + size);
+    memcpy(fh, target_fh, size);
+    fh->handle_bytes = size;
+    fh->handle_type = tswap32(target_fh->handle_type);
+
+    ret = get_errno(open_by_handle_at(arg1, fh,
+                    target_to_host_bitmask(arg3, fcntl_flags_tbl)));
+
+    g_free(fh);
+
+    unlock_user(target_fh, arg2, sizeof(struct file_handle) + size);
+
+    return ret;
+}
+#endif
 
 /* Map host to target signal numbers for the wait family of syscalls.
    Assume all other status bits are the same.  */
@@ -5655,6 +5742,16 @@  abi_long do_syscall(void *cpu_env, int num, abi_long arg1,
                                   arg4));
         unlock_user(p, arg2, 0);
         break;
+#if defined(TARGET_NR_name_to_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
+    case TARGET_NR_name_to_handle_at:
+        ret = do_name_to_handle_at(arg1, arg2, arg3, arg4, arg5);
+        break;
+#endif
+#if defined(TARGET_NR_open_by_handle_at) && defined(CONFIG_OPEN_BY_HANDLE)
+    case TARGET_NR_open_by_handle_at:
+        ret = do_open_by_handle_at(arg1, arg2, arg3);
+        break;
+#endif
     case TARGET_NR_close:
         ret = get_errno(close(arg1));
         break;