xfs_ioctl32.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (c) 2004-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/compat.h>
  19. #include <linux/init.h>
  20. #include <linux/ioctl.h>
  21. #include <linux/syscalls.h>
  22. #include <linux/types.h>
  23. #include <linux/fs.h>
  24. #include <asm/uaccess.h>
  25. #include "xfs.h"
  26. #include "xfs_fs.h"
  27. #include "xfs_bit.h"
  28. #include "xfs_log.h"
  29. #include "xfs_inum.h"
  30. #include "xfs_trans.h"
  31. #include "xfs_sb.h"
  32. #include "xfs_ag.h"
  33. #include "xfs_dir2.h"
  34. #include "xfs_dmapi.h"
  35. #include "xfs_mount.h"
  36. #include "xfs_bmap_btree.h"
  37. #include "xfs_attr_sf.h"
  38. #include "xfs_dir2_sf.h"
  39. #include "xfs_vnode.h"
  40. #include "xfs_dinode.h"
  41. #include "xfs_inode.h"
  42. #include "xfs_itable.h"
  43. #include "xfs_error.h"
  44. #include "xfs_dfrag.h"
  45. #include "xfs_vnodeops.h"
  46. #include "xfs_fsops.h"
  47. #include "xfs_ioctl.h"
  48. #include "xfs_ioctl32.h"
  49. #define _NATIVE_IOC(cmd, type) \
  50. _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
  51. #ifdef BROKEN_X86_ALIGNMENT
  52. STATIC int
  53. xfs_compat_flock64_copyin(
  54. xfs_flock64_t *bf,
  55. compat_xfs_flock64_t __user *arg32)
  56. {
  57. if (get_user(bf->l_type, &arg32->l_type) ||
  58. get_user(bf->l_whence, &arg32->l_whence) ||
  59. get_user(bf->l_start, &arg32->l_start) ||
  60. get_user(bf->l_len, &arg32->l_len) ||
  61. get_user(bf->l_sysid, &arg32->l_sysid) ||
  62. get_user(bf->l_pid, &arg32->l_pid) ||
  63. copy_from_user(bf->l_pad, &arg32->l_pad, 4*sizeof(u32)))
  64. return -XFS_ERROR(EFAULT);
  65. return 0;
  66. }
  67. STATIC int
  68. xfs_compat_ioc_fsgeometry_v1(
  69. struct xfs_mount *mp,
  70. compat_xfs_fsop_geom_v1_t __user *arg32)
  71. {
  72. xfs_fsop_geom_t fsgeo;
  73. int error;
  74. error = xfs_fs_geometry(mp, &fsgeo, 3);
  75. if (error)
  76. return -error;
  77. /* The 32-bit variant simply has some padding at the end */
  78. if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
  79. return -XFS_ERROR(EFAULT);
  80. return 0;
  81. }
  82. STATIC int
  83. xfs_inumbers_fmt_compat(
  84. void __user *ubuffer,
  85. const xfs_inogrp_t *buffer,
  86. long count,
  87. long *written)
  88. {
  89. compat_xfs_inogrp_t __user *p32 = ubuffer;
  90. long i;
  91. for (i = 0; i < count; i++) {
  92. if (put_user(buffer[i].xi_startino, &p32[i].xi_startino) ||
  93. put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
  94. put_user(buffer[i].xi_allocmask, &p32[i].xi_allocmask))
  95. return -XFS_ERROR(EFAULT);
  96. }
  97. *written = count * sizeof(*p32);
  98. return 0;
  99. }
  100. #else
  101. #define xfs_inumbers_fmt_compat xfs_inumbers_fmt
  102. #endif
  103. /* XFS_IOC_FSBULKSTAT and friends */
  104. STATIC int
  105. xfs_bstime_store_compat(
  106. compat_xfs_bstime_t __user *p32,
  107. const xfs_bstime_t *p)
  108. {
  109. __s32 sec32;
  110. sec32 = p->tv_sec;
  111. if (put_user(sec32, &p32->tv_sec) ||
  112. put_user(p->tv_nsec, &p32->tv_nsec))
  113. return -XFS_ERROR(EFAULT);
  114. return 0;
  115. }
  116. STATIC int
  117. xfs_bulkstat_one_fmt_compat(
  118. void __user *ubuffer,
  119. const xfs_bstat_t *buffer)
  120. {
  121. compat_xfs_bstat_t __user *p32 = ubuffer;
  122. if (put_user(buffer->bs_ino, &p32->bs_ino) ||
  123. put_user(buffer->bs_mode, &p32->bs_mode) ||
  124. put_user(buffer->bs_nlink, &p32->bs_nlink) ||
  125. put_user(buffer->bs_uid, &p32->bs_uid) ||
  126. put_user(buffer->bs_gid, &p32->bs_gid) ||
  127. put_user(buffer->bs_rdev, &p32->bs_rdev) ||
  128. put_user(buffer->bs_blksize, &p32->bs_blksize) ||
  129. put_user(buffer->bs_size, &p32->bs_size) ||
  130. xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
  131. xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
  132. xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
  133. put_user(buffer->bs_blocks, &p32->bs_blocks) ||
  134. put_user(buffer->bs_xflags, &p32->bs_xflags) ||
  135. put_user(buffer->bs_extsize, &p32->bs_extsize) ||
  136. put_user(buffer->bs_extents, &p32->bs_extents) ||
  137. put_user(buffer->bs_gen, &p32->bs_gen) ||
  138. put_user(buffer->bs_projid, &p32->bs_projid) ||
  139. put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
  140. put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
  141. put_user(buffer->bs_aextents, &p32->bs_aextents))
  142. return -XFS_ERROR(EFAULT);
  143. return sizeof(*p32);
  144. }
  145. /* copied from xfs_ioctl.c */
  146. STATIC int
  147. xfs_ioc_bulkstat_compat(
  148. xfs_mount_t *mp,
  149. unsigned int cmd,
  150. void __user *arg)
  151. {
  152. compat_xfs_fsop_bulkreq_t __user *p32 = (void __user *)arg;
  153. u32 addr;
  154. xfs_fsop_bulkreq_t bulkreq;
  155. int count; /* # of records returned */
  156. xfs_ino_t inlast; /* last inode number */
  157. int done;
  158. int error;
  159. /* done = 1 if there are more stats to get and if bulkstat */
  160. /* should be called again (unused here, but used in dmapi) */
  161. if (!capable(CAP_SYS_ADMIN))
  162. return -XFS_ERROR(EPERM);
  163. if (XFS_FORCED_SHUTDOWN(mp))
  164. return -XFS_ERROR(EIO);
  165. if (get_user(addr, &p32->lastip))
  166. return -XFS_ERROR(EFAULT);
  167. bulkreq.lastip = compat_ptr(addr);
  168. if (get_user(bulkreq.icount, &p32->icount) ||
  169. get_user(addr, &p32->ubuffer))
  170. return -XFS_ERROR(EFAULT);
  171. bulkreq.ubuffer = compat_ptr(addr);
  172. if (get_user(addr, &p32->ocount))
  173. return -XFS_ERROR(EFAULT);
  174. bulkreq.ocount = compat_ptr(addr);
  175. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  176. return -XFS_ERROR(EFAULT);
  177. if ((count = bulkreq.icount) <= 0)
  178. return -XFS_ERROR(EINVAL);
  179. if (bulkreq.ubuffer == NULL)
  180. return -XFS_ERROR(EINVAL);
  181. if (cmd == XFS_IOC_FSINUMBERS)
  182. error = xfs_inumbers(mp, &inlast, &count,
  183. bulkreq.ubuffer, xfs_inumbers_fmt_compat);
  184. else {
  185. /* declare a var to get a warning in case the type changes */
  186. bulkstat_one_fmt_pf formatter = xfs_bulkstat_one_fmt_compat;
  187. error = xfs_bulkstat(mp, &inlast, &count,
  188. xfs_bulkstat_one, formatter,
  189. sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
  190. BULKSTAT_FG_QUICK, &done);
  191. }
  192. if (error)
  193. return -error;
  194. if (bulkreq.ocount != NULL) {
  195. if (copy_to_user(bulkreq.lastip, &inlast,
  196. sizeof(xfs_ino_t)))
  197. return -XFS_ERROR(EFAULT);
  198. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  199. return -XFS_ERROR(EFAULT);
  200. }
  201. return 0;
  202. }
  203. STATIC int
  204. xfs_compat_handlereq_copyin(
  205. xfs_fsop_handlereq_t *hreq,
  206. compat_xfs_fsop_handlereq_t __user *arg32)
  207. {
  208. compat_xfs_fsop_handlereq_t hreq32;
  209. if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
  210. return -XFS_ERROR(EFAULT);
  211. hreq->fd = hreq32.fd;
  212. hreq->path = compat_ptr(hreq32.path);
  213. hreq->oflags = hreq32.oflags;
  214. hreq->ihandle = compat_ptr(hreq32.ihandle);
  215. hreq->ihandlen = hreq32.ihandlen;
  216. hreq->ohandle = compat_ptr(hreq32.ohandle);
  217. hreq->ohandlen = compat_ptr(hreq32.ohandlen);
  218. return 0;
  219. }
  220. STATIC long
  221. xfs_compat_ioctl(
  222. xfs_inode_t *ip,
  223. struct file *filp,
  224. int ioflags,
  225. unsigned cmd,
  226. void __user *arg)
  227. {
  228. struct inode *inode = filp->f_path.dentry->d_inode;
  229. xfs_mount_t *mp = ip->i_mount;
  230. int error;
  231. xfs_itrace_entry(XFS_I(inode));
  232. switch (cmd) {
  233. case XFS_IOC_DIOINFO:
  234. case XFS_IOC_FSGEOMETRY:
  235. case XFS_IOC_FSGETXATTR:
  236. case XFS_IOC_FSSETXATTR:
  237. case XFS_IOC_FSGETXATTRA:
  238. case XFS_IOC_FSSETDM:
  239. case XFS_IOC_GETBMAP:
  240. case XFS_IOC_GETBMAPA:
  241. case XFS_IOC_GETBMAPX:
  242. /* not handled
  243. case XFS_IOC_FSSETDM_BY_HANDLE:
  244. case XFS_IOC_ATTRLIST_BY_HANDLE:
  245. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  246. */
  247. case XFS_IOC_FSCOUNTS:
  248. case XFS_IOC_SET_RESBLKS:
  249. case XFS_IOC_GET_RESBLKS:
  250. case XFS_IOC_FSGROWFSDATA:
  251. case XFS_IOC_FSGROWFSLOG:
  252. case XFS_IOC_FSGROWFSRT:
  253. case XFS_IOC_FREEZE:
  254. case XFS_IOC_THAW:
  255. case XFS_IOC_GOINGDOWN:
  256. case XFS_IOC_ERROR_INJECTION:
  257. case XFS_IOC_ERROR_CLEARALL:
  258. break;
  259. case XFS_IOC_GETXFLAGS_32:
  260. case XFS_IOC_SETXFLAGS_32:
  261. case XFS_IOC_GETVERSION_32:
  262. cmd = _NATIVE_IOC(cmd, long);
  263. break;
  264. #ifdef BROKEN_X86_ALIGNMENT
  265. /* xfs_flock_t has wrong u32 vs u64 alignment */
  266. case XFS_IOC_ALLOCSP_32:
  267. case XFS_IOC_FREESP_32:
  268. case XFS_IOC_ALLOCSP64_32:
  269. case XFS_IOC_FREESP64_32:
  270. case XFS_IOC_RESVSP_32:
  271. case XFS_IOC_UNRESVSP_32:
  272. case XFS_IOC_RESVSP64_32:
  273. case XFS_IOC_UNRESVSP64_32: {
  274. struct xfs_flock64 bf;
  275. if (xfs_compat_flock64_copyin(&bf, arg))
  276. return -XFS_ERROR(EFAULT);
  277. cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
  278. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  279. }
  280. case XFS_IOC_FSGEOMETRY_V1_32:
  281. return xfs_compat_ioc_fsgeometry_v1(mp, arg);
  282. #else /* These are handled fine if no alignment issues */
  283. case XFS_IOC_ALLOCSP:
  284. case XFS_IOC_FREESP:
  285. case XFS_IOC_RESVSP:
  286. case XFS_IOC_UNRESVSP:
  287. case XFS_IOC_ALLOCSP64:
  288. case XFS_IOC_FREESP64:
  289. case XFS_IOC_RESVSP64:
  290. case XFS_IOC_UNRESVSP64:
  291. case XFS_IOC_FSGEOMETRY_V1:
  292. break;
  293. /* xfs_bstat_t still has wrong u32 vs u64 alignment */
  294. case XFS_IOC_SWAPEXT:
  295. break;
  296. #endif
  297. case XFS_IOC_FSBULKSTAT_32:
  298. case XFS_IOC_FSBULKSTAT_SINGLE_32:
  299. case XFS_IOC_FSINUMBERS_32:
  300. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_bulkreq);
  301. return xfs_ioc_bulkstat_compat(XFS_I(inode)->i_mount,
  302. cmd, (void __user*)arg);
  303. case XFS_IOC_FD_TO_HANDLE_32:
  304. case XFS_IOC_PATH_TO_HANDLE_32:
  305. case XFS_IOC_PATH_TO_FSHANDLE_32: {
  306. struct xfs_fsop_handlereq hreq;
  307. if (xfs_compat_handlereq_copyin(&hreq, arg))
  308. return -XFS_ERROR(EFAULT);
  309. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
  310. return xfs_find_handle(cmd, &hreq);
  311. }
  312. case XFS_IOC_OPEN_BY_HANDLE_32: {
  313. struct xfs_fsop_handlereq hreq;
  314. if (xfs_compat_handlereq_copyin(&hreq, arg))
  315. return -XFS_ERROR(EFAULT);
  316. return xfs_open_by_handle(mp, &hreq, filp, inode);
  317. }
  318. case XFS_IOC_READLINK_BY_HANDLE_32: {
  319. struct xfs_fsop_handlereq hreq;
  320. if (xfs_compat_handlereq_copyin(&hreq, arg))
  321. return -XFS_ERROR(EFAULT);
  322. return xfs_readlink_by_handle(mp, &hreq, inode);
  323. }
  324. default:
  325. return -XFS_ERROR(ENOIOCTLCMD);
  326. }
  327. error = xfs_ioctl(ip, filp, ioflags, cmd, arg);
  328. return error;
  329. }
  330. long
  331. xfs_file_compat_ioctl(
  332. struct file *filp,
  333. unsigned int cmd,
  334. unsigned long p)
  335. {
  336. struct inode *inode = filp->f_path.dentry->d_inode;
  337. return xfs_compat_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  338. }
  339. long
  340. xfs_file_compat_invis_ioctl(
  341. struct file *filp,
  342. unsigned int cmd,
  343. unsigned long p)
  344. {
  345. struct inode *inode = filp->f_path.dentry->d_inode;
  346. return xfs_compat_ioctl(XFS_I(inode), filp, IO_INVIS, cmd,
  347. (void __user *)p);
  348. }