blob: f7c18e97d85286c731d52b5f127c37df816e8046 [file] [log] [blame]
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +05301#include <linux/syscalls.h>
2#include <linux/slab.h>
3#include <linux/fs.h>
4#include <linux/file.h>
5#include <linux/mount.h>
6#include <linux/namei.h>
7#include <linux/exportfs.h>
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +05308#include <linux/fs_struct.h>
9#include <linux/fsnotify.h>
Jeff Mahoneyed5afea2011-04-14 15:22:16 -070010#include <linux/personality.h>
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053011#include <asm/uaccess.h>
12#include "internal.h"
Al Viro15169fe2011-11-25 00:50:41 -050013#include "mount.h"
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053014
15static long do_sys_name_to_handle(struct path *path,
16 struct file_handle __user *ufh,
17 int __user *mnt_id)
18{
19 long retval;
20 struct file_handle f_handle;
21 int handle_dwords, handle_bytes;
22 struct file_handle *handle = NULL;
23
24 /*
25 * We need t make sure wether the file system
26 * support decoding of the file handle
27 */
Al Virod8c95842011-12-07 18:16:57 -050028 if (!path->dentry->d_sb->s_export_op ||
29 !path->dentry->d_sb->s_export_op->fh_to_dentry)
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053030 return -EOPNOTSUPP;
31
32 if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
33 return -EFAULT;
34
35 if (f_handle.handle_bytes > MAX_HANDLE_SZ)
36 return -EINVAL;
37
38 handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
39 GFP_KERNEL);
40 if (!handle)
41 return -ENOMEM;
42
43 /* convert handle size to multiple of sizeof(u32) */
44 handle_dwords = f_handle.handle_bytes >> 2;
45
46 /* we ask for a non connected handle */
47 retval = exportfs_encode_fh(path->dentry,
48 (struct fid *)handle->f_handle,
49 &handle_dwords, 0);
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
59 * both the values
60 */
61 /*
62 * set the handle size to zero so we copy only
63 * non variable part of the file_handle
64 */
65 handle_bytes = 0;
66 retval = -EOVERFLOW;
67 } else
68 retval = 0;
69 /* copy the mount id */
Al Viro15169fe2011-11-25 00:50:41 -050070 if (copy_to_user(mnt_id, &real_mount(path->mnt)->mnt_id,
71 sizeof(*mnt_id)) ||
Aneesh Kumar K.V990d6c22011-01-29 18:43:26 +053072 copy_to_user(ufh, handle,
73 sizeof(struct file_handle) + handle_bytes))
74 retval = -EFAULT;
75 kfree(handle);
76 return retval;
77}
78
79/**
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
86 *
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
90 * value required.
91 */
92SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
93 struct file_handle __user *, handle, int __user *, mnt_id,
94 int, flag)
95{
96 struct path path;
97 int lookup_flags;
98 int err;
99
100 if ((flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
101 return -EINVAL;
102
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);
107 if (!err) {
108 err = do_sys_name_to_handle(&path, handle, mnt_id);
109 path_put(&path);
110 }
111 return err;
112}
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530113
114static struct vfsmount *get_vfsmount_from_fd(int fd)
115{
116 struct path path;
117
118 if (fd == AT_FDCWD) {
119 struct fs_struct *fs = current->fs;
120 spin_lock(&fs->lock);
121 path = fs->pwd;
122 mntget(path.mnt);
123 spin_unlock(&fs->lock);
124 } else {
125 int fput_needed;
126 struct file *file = fget_light(fd, &fput_needed);
127 if (!file)
128 return ERR_PTR(-EBADF);
129 path = file->f_path;
130 mntget(path.mnt);
131 fput_light(file, fput_needed);
132 }
133 return path.mnt;
134}
135
136static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
137{
138 return 1;
139}
140
141static int do_handle_to_path(int mountdirfd, struct file_handle *handle,
142 struct path *path)
143{
144 int retval = 0;
145 int handle_dwords;
146
147 path->mnt = get_vfsmount_from_fd(mountdirfd);
148 if (IS_ERR(path->mnt)) {
149 retval = PTR_ERR(path->mnt);
150 goto out_err;
151 }
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);
160 goto out_mnt;
161 }
162 return 0;
163out_mnt:
164 mntput(path->mnt);
165out_err:
166 return retval;
167}
168
169static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
170 struct path *path)
171{
172 int retval = 0;
173 struct file_handle f_handle;
174 struct file_handle *handle = NULL;
175
176 /*
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
180 */
181 if (!capable(CAP_DAC_READ_SEARCH)) {
182 retval = -EPERM;
183 goto out_err;
184 }
185 if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
186 retval = -EFAULT;
187 goto out_err;
188 }
189 if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
190 (f_handle.handle_bytes == 0)) {
191 retval = -EINVAL;
192 goto out_err;
193 }
194 handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
195 GFP_KERNEL);
196 if (!handle) {
197 retval = -ENOMEM;
198 goto out_err;
199 }
200 /* copy the full handle */
Sasha Levin1f6efb12015-01-28 15:30:43 -0500201 *handle = f_handle;
202 if (copy_from_user(&handle->f_handle,
203 &ufh->f_handle,
Aneesh Kumar K.Vbecfd1f2011-01-29 18:43:26 +0530204 f_handle.handle_bytes)) {
205 retval = -EFAULT;
206 goto out_handle;
207 }
208
209 retval = do_handle_to_path(mountdirfd, handle, path);
210
211out_handle:
212 kfree(handle);
213out_err:
214 return retval;
215}
216
217long do_handle_open(int mountdirfd,
218 struct file_handle __user *ufh, int open_flag)
219{
220 long retval = 0;
221 struct path path;
222 struct file *file;
223 int fd;
224
225 retval = handle_to_path(mountdirfd, ufh, &path);
226 if (retval)
227 return retval;
228
229 fd = get_unused_fd_flags(open_flag);
230 if (fd < 0) {
231 path_put(&path);
232 return fd;
233 }
234 file = file_open_root(path.dentry, path.mnt, "", open_flag);
235 if (IS_ERR(file)) {
236 put_unused_fd(fd);
237 retval = PTR_ERR(file);
238 } else {
239 retval = fd;
240 fsnotify_open(file);
241 fd_install(fd, file);
242 }
243 path_put(&path);
244 return retval;
245}
246
247/**
248 * sys_open_by_handle_at: Open the file handle
249 * @mountdirfd: directory file descriptor
250 * @handle: file handle to be opened
251 * @flag: open flags.
252 *
253 * @mountdirfd indicate the directory file descriptor
254 * of the mount point. file handle is decoded relative
255 * to the vfsmount pointed by the @mountdirfd. @flags
256 * value is same as the open(2) flags.
257 */
258SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
259 struct file_handle __user *, handle,
260 int, flags)
261{
262 long ret;
263
264 if (force_o_largefile())
265 flags |= O_LARGEFILE;
266
267 ret = do_handle_open(mountdirfd, handle, flags);
268 return ret;
269}