original development tree for Linux kernel GTP module; now long in mainline.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
6.5 KiB

  1. #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>
  8. #include <linux/fs_struct.h>
  9. #include <linux/fsnotify.h>
  10. #include <linux/personality.h>
  11. #include <asm/uaccess.h>
  12. #include "internal.h"
  13. #include "mount.h"
  14. static long do_sys_name_to_handle(struct path *path,
  15. struct file_handle __user *ufh,
  16. int __user *mnt_id)
  17. {
  18. long retval;
  19. struct file_handle f_handle;
  20. int handle_dwords, handle_bytes;
  21. struct file_handle *handle = NULL;
  22. /*
  23. * We need to make sure whether the file system
  24. * support decoding of the file handle
  25. */
  26. if (!path->dentry->d_sb->s_export_op ||
  27. !path->dentry->d_sb->s_export_op->fh_to_dentry)
  28. return -EOPNOTSUPP;
  29. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle)))
  30. return -EFAULT;
  31. if (f_handle.handle_bytes > MAX_HANDLE_SZ)
  32. return -EINVAL;
  33. handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
  34. GFP_KERNEL);
  35. if (!handle)
  36. return -ENOMEM;
  37. /* convert handle size to multiple of sizeof(u32) */
  38. handle_dwords = f_handle.handle_bytes >> 2;
  39. /* we ask for a non connected handle */
  40. retval = exportfs_encode_fh(path->dentry,
  41. (struct fid *)handle->f_handle,
  42. &handle_dwords, 0);
  43. handle->handle_type = retval;
  44. /* convert handle size to bytes */
  45. handle_bytes = handle_dwords * sizeof(u32);
  46. handle->handle_bytes = handle_bytes;
  47. if ((handle->handle_bytes > f_handle.handle_bytes) ||
  48. (retval == FILEID_INVALID) || (retval == -ENOSPC)) {
  49. /* As per old exportfs_encode_fh documentation
  50. * we could return ENOSPC to indicate overflow
  51. * But file system returned 255 always. So handle
  52. * both the values
  53. */
  54. /*
  55. * set the handle size to zero so we copy only
  56. * non variable part of the file_handle
  57. */
  58. handle_bytes = 0;
  59. retval = -EOVERFLOW;
  60. } else
  61. retval = 0;
  62. /* copy the mount id */
  63. if (copy_to_user(mnt_id, &real_mount(path->mnt)->mnt_id,
  64. sizeof(*mnt_id)) ||
  65. copy_to_user(ufh, handle,
  66. sizeof(struct file_handle) + handle_bytes))
  67. retval = -EFAULT;
  68. kfree(handle);
  69. return retval;
  70. }
  71. /**
  72. * sys_name_to_handle_at: convert name to handle
  73. * @dfd: directory relative to which name is interpreted if not absolute
  74. * @name: name that should be converted to handle.
  75. * @handle: resulting file handle
  76. * @mnt_id: mount id of the file system containing the file
  77. * @flag: flag value to indicate whether to follow symlink or not
  78. *
  79. * @handle->handle_size indicate the space available to store the
  80. * variable part of the file handle in bytes. If there is not
  81. * enough space, the field is updated to return the minimum
  82. * value required.
  83. */
  84. SYSCALL_DEFINE5(name_to_handle_at, int, dfd, const char __user *, name,
  85. struct file_handle __user *, handle, int __user *, mnt_id,
  86. int, flag)
  87. {
  88. struct path path;
  89. int lookup_flags;
  90. int err;
  91. if ((flag & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0)
  92. return -EINVAL;
  93. lookup_flags = (flag & AT_SYMLINK_FOLLOW) ? LOOKUP_FOLLOW : 0;
  94. if (flag & AT_EMPTY_PATH)
  95. lookup_flags |= LOOKUP_EMPTY;
  96. err = user_path_at(dfd, name, lookup_flags, &path);
  97. if (!err) {
  98. err = do_sys_name_to_handle(&path, handle, mnt_id);
  99. path_put(&path);
  100. }
  101. return err;
  102. }
  103. static struct vfsmount *get_vfsmount_from_fd(int fd)
  104. {
  105. struct vfsmount *mnt;
  106. if (fd == AT_FDCWD) {
  107. struct fs_struct *fs = current->fs;
  108. spin_lock(&fs->lock);
  109. mnt = mntget(fs->pwd.mnt);
  110. spin_unlock(&fs->lock);
  111. } else {
  112. struct fd f = fdget(fd);
  113. if (!f.file)
  114. return ERR_PTR(-EBADF);
  115. mnt = mntget(f.file->f_path.mnt);
  116. fdput(f);
  117. }
  118. return mnt;
  119. }
  120. static int vfs_dentry_acceptable(void *context, struct dentry *dentry)
  121. {
  122. return 1;
  123. }
  124. static int do_handle_to_path(int mountdirfd, struct file_handle *handle,
  125. struct path *path)
  126. {
  127. int retval = 0;
  128. int handle_dwords;
  129. path->mnt = get_vfsmount_from_fd(mountdirfd);
  130. if (IS_ERR(path->mnt)) {
  131. retval = PTR_ERR(path->mnt);
  132. goto out_err;
  133. }
  134. /* change the handle size to multiple of sizeof(u32) */
  135. handle_dwords = handle->handle_bytes >> 2;
  136. path->dentry = exportfs_decode_fh(path->mnt,
  137. (struct fid *)handle->f_handle,
  138. handle_dwords, handle->handle_type,
  139. vfs_dentry_acceptable, NULL);
  140. if (IS_ERR(path->dentry)) {
  141. retval = PTR_ERR(path->dentry);
  142. goto out_mnt;
  143. }
  144. return 0;
  145. out_mnt:
  146. mntput(path->mnt);
  147. out_err:
  148. return retval;
  149. }
  150. static int handle_to_path(int mountdirfd, struct file_handle __user *ufh,
  151. struct path *path)
  152. {
  153. int retval = 0;
  154. struct file_handle f_handle;
  155. struct file_handle *handle = NULL;
  156. /*
  157. * With handle we don't look at the execute bit on the
  158. * the directory. Ideally we would like CAP_DAC_SEARCH.
  159. * But we don't have that
  160. */
  161. if (!capable(CAP_DAC_READ_SEARCH)) {
  162. retval = -EPERM;
  163. goto out_err;
  164. }
  165. if (copy_from_user(&f_handle, ufh, sizeof(struct file_handle))) {
  166. retval = -EFAULT;
  167. goto out_err;
  168. }
  169. if ((f_handle.handle_bytes > MAX_HANDLE_SZ) ||
  170. (f_handle.handle_bytes == 0)) {
  171. retval = -EINVAL;
  172. goto out_err;
  173. }
  174. handle = kmalloc(sizeof(struct file_handle) + f_handle.handle_bytes,
  175. GFP_KERNEL);
  176. if (!handle) {
  177. retval = -ENOMEM;
  178. goto out_err;
  179. }
  180. /* copy the full handle */
  181. if (copy_from_user(handle, ufh,
  182. sizeof(struct file_handle) +
  183. f_handle.handle_bytes)) {
  184. retval = -EFAULT;
  185. goto out_handle;
  186. }
  187. retval = do_handle_to_path(mountdirfd, handle, path);
  188. out_handle:
  189. kfree(handle);
  190. out_err:
  191. return retval;
  192. }
  193. long do_handle_open(int mountdirfd,
  194. struct file_handle __user *ufh, int open_flag)
  195. {
  196. long retval = 0;
  197. struct path path;
  198. struct file *file;
  199. int fd;
  200. retval = handle_to_path(mountdirfd, ufh, &path);
  201. if (retval)
  202. return retval;
  203. fd = get_unused_fd_flags(open_flag);
  204. if (fd < 0) {
  205. path_put(&path);
  206. return fd;
  207. }
  208. file = file_open_root(path.dentry, path.mnt, "", open_flag);
  209. if (IS_ERR(file)) {
  210. put_unused_fd(fd);
  211. retval = PTR_ERR(file);
  212. } else {
  213. retval = fd;
  214. fsnotify_open(file);
  215. fd_install(fd, file);
  216. }
  217. path_put(&path);
  218. return retval;
  219. }
  220. /**
  221. * sys_open_by_handle_at: Open the file handle
  222. * @mountdirfd: directory file descriptor
  223. * @handle: file handle to be opened
  224. * @flag: open flags.
  225. *
  226. * @mountdirfd indicate the directory file descriptor
  227. * of the mount point. file handle is decoded relative
  228. * to the vfsmount pointed by the @mountdirfd. @flags
  229. * value is same as the open(2) flags.
  230. */
  231. SYSCALL_DEFINE3(open_by_handle_at, int, mountdirfd,
  232. struct file_handle __user *, handle,
  233. int, flags)
  234. {
  235. long ret;
  236. if (force_o_largefile())
  237. flags |= O_LARGEFILE;
  238. ret = do_handle_open(mountdirfd, handle, flags);
  239. return ret;
  240. }