1 #include <linux/syscalls.h>
2 #include <linux/slab.h>
4 #include <linux/file.h>
5 #include <linux/mount.h>
6 #include <linux/namei.h>
7 #include <linux/exportfs.h>
8 #include <linux/fs_struct.h>
9 #include <linux/fsnotify.h>
10 #include <linux/personality.h>
11 #include <asm/uaccess.h>
15 static long do_sys_name_to_handle(struct path
*path
,
16 struct file_handle __user
*ufh
,
20 struct file_handle f_handle
;
21 int handle_dwords
, handle_bytes
;
22 struct file_handle
*handle
= NULL
;
25 * We need t make sure wether the file system
26 * support decoding of the file handle
28 if (!path
->dentry
->d_sb
->s_export_op
||
29 !path
->dentry
->d_sb
->s_export_op
->fh_to_dentry
)
32 if (copy_from_user(&f_handle
, ufh
, sizeof(struct file_handle
)))
35 if (f_handle
.handle_bytes
> MAX_HANDLE_SZ
)
38 handle
= kmalloc(sizeof(struct file_handle
) + f_handle
.handle_bytes
,
43 /* convert handle size to multiple of sizeof(u32) */
44 handle_dwords
= f_handle
.handle_bytes
>> 2;
46 /* we ask for a non connected handle */
47 retval
= exportfs_encode_fh(path
->dentry
,
48 (struct fid
*)handle
->f_handle
,
50 handle
->handle_type
= retval
;
51 /* convert handle size to bytes */
52 handle_bytes
= handle_dwords
* sizeof(u32
);
53 handle
->handle_bytes
= handle_bytes
;
54 if ((handle
->handle_bytes
> f_handle
.handle_bytes
) ||
55 (retval
== 255) || (retval
== -ENOSPC
)) {
56 /* As per old exportfs_encode_fh documentation
57 * we could return ENOSPC to indicate overflow
58 * But file system returned 255 always. So handle
62 * set the handle size to zero so we copy only
63 * non variable part of the file_handle
69 /* copy the mount id */
70 if (copy_to_user(mnt_id
, &real_mount(path
->mnt
)->mnt_id
,
72 copy_to_user(ufh
, handle
,
73 sizeof(struct file_handle
) + handle_bytes
))
80 * sys_name_to_handle_at: convert name to handle
81 * @dfd: directory relative to which name is interpreted if not absolute
82 * @name: name that should be converted to handle.
83 * @handle: resulting file handle
84 * @mnt_id: mount id of the file system containing the file
85 * @flag: flag value to indicate whether to follow symlink or not
87 * @handle->handle_size indicate the space available to store the
88 * variable part of the file handle in bytes. If there is not
89 * enough space, the field is updated to return the minimum
92 SYSCALL_DEFINE5(name_to_handle_at
, int, dfd
, const char __user
*, name
,
93 struct file_handle __user
*, handle
, int __user
*, mnt_id
,
100 if ((flag
& ~(AT_SYMLINK_FOLLOW
| AT_EMPTY_PATH
)) != 0)
103 lookup_flags
= (flag
& AT_SYMLINK_FOLLOW
) ? LOOKUP_FOLLOW
: 0;
104 if (flag
& AT_EMPTY_PATH
)
105 lookup_flags
|= LOOKUP_EMPTY
;
106 err
= user_path_at(dfd
, name
, lookup_flags
, &path
);
108 err
= do_sys_name_to_handle(&path
, handle
, mnt_id
);
114 static struct vfsmount
*get_vfsmount_from_fd(int fd
)
118 if (fd
== AT_FDCWD
) {
119 struct fs_struct
*fs
= current
->fs
;
120 spin_lock(&fs
->lock
);
123 spin_unlock(&fs
->lock
);
126 struct file
*file
= fget_light(fd
, &fput_needed
);
128 return ERR_PTR(-EBADF
);
131 fput_light(file
, fput_needed
);
136 static int vfs_dentry_acceptable(void *context
, struct dentry
*dentry
)
141 static int do_handle_to_path(int mountdirfd
, struct file_handle
*handle
,
147 path
->mnt
= get_vfsmount_from_fd(mountdirfd
);
148 if (IS_ERR(path
->mnt
)) {
149 retval
= PTR_ERR(path
->mnt
);
152 /* change the handle size to multiple of sizeof(u32) */
153 handle_dwords
= handle
->handle_bytes
>> 2;
154 path
->dentry
= exportfs_decode_fh(path
->mnt
,
155 (struct fid
*)handle
->f_handle
,
156 handle_dwords
, handle
->handle_type
,
157 vfs_dentry_acceptable
, NULL
);
158 if (IS_ERR(path
->dentry
)) {
159 retval
= PTR_ERR(path
->dentry
);
169 static int handle_to_path(int mountdirfd
, struct file_handle __user
*ufh
,
173 struct file_handle f_handle
;
174 struct file_handle
*handle
= NULL
;
177 * With handle we don't look at the execute bit on the
178 * the directory. Ideally we would like CAP_DAC_SEARCH.
179 * But we don't have that
181 if (!capable(CAP_DAC_READ_SEARCH
)) {
185 if (copy_from_user(&f_handle
, ufh
, sizeof(struct file_handle
))) {
189 if ((f_handle
.handle_bytes
> MAX_HANDLE_SZ
) ||
190 (f_handle
.handle_bytes
== 0)) {
194 handle
= kmalloc(sizeof(struct file_handle
) + f_handle
.handle_bytes
,
200 /* copy the full handle */
201 if (copy_from_user(handle
, ufh
,
202 sizeof(struct file_handle
) +
203 f_handle
.handle_bytes
)) {
208 retval
= do_handle_to_path(mountdirfd
, handle
, path
);
216 long do_handle_open(int mountdirfd
,
217 struct file_handle __user
*ufh
, int open_flag
)
224 retval
= handle_to_path(mountdirfd
, ufh
, &path
);
228 fd
= get_unused_fd_flags(open_flag
);
233 file
= file_open_root(path
.dentry
, path
.mnt
, "", open_flag
);
236 retval
= PTR_ERR(file
);
240 fd_install(fd
, file
);
247 * sys_open_by_handle_at: Open the file handle
248 * @mountdirfd: directory file descriptor
249 * @handle: file handle to be opened
252 * @mountdirfd indicate the directory file descriptor
253 * of the mount point. file handle is decoded relative
254 * to the vfsmount pointed by the @mountdirfd. @flags
255 * value is same as the open(2) flags.
257 SYSCALL_DEFINE3(open_by_handle_at
, int, mountdirfd
,
258 struct file_handle __user
*, handle
,
263 if (force_o_largefile())
264 flags
|= O_LARGEFILE
;
266 ret
= do_handle_open(mountdirfd
, handle
, flags
);