xfs_ioctl32.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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_alloc.h"
  48. #include "xfs_rtalloc.h"
  49. #include "xfs_ioctl.h"
  50. #include "xfs_ioctl32.h"
  51. #define _NATIVE_IOC(cmd, type) \
  52. _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
  53. #ifdef BROKEN_X86_ALIGNMENT
  54. STATIC int
  55. xfs_compat_flock64_copyin(
  56. xfs_flock64_t *bf,
  57. compat_xfs_flock64_t __user *arg32)
  58. {
  59. if (get_user(bf->l_type, &arg32->l_type) ||
  60. get_user(bf->l_whence, &arg32->l_whence) ||
  61. get_user(bf->l_start, &arg32->l_start) ||
  62. get_user(bf->l_len, &arg32->l_len) ||
  63. get_user(bf->l_sysid, &arg32->l_sysid) ||
  64. get_user(bf->l_pid, &arg32->l_pid) ||
  65. copy_from_user(bf->l_pad, &arg32->l_pad, 4*sizeof(u32)))
  66. return -XFS_ERROR(EFAULT);
  67. return 0;
  68. }
  69. STATIC int
  70. xfs_compat_ioc_fsgeometry_v1(
  71. struct xfs_mount *mp,
  72. compat_xfs_fsop_geom_v1_t __user *arg32)
  73. {
  74. xfs_fsop_geom_t fsgeo;
  75. int error;
  76. error = xfs_fs_geometry(mp, &fsgeo, 3);
  77. if (error)
  78. return -error;
  79. /* The 32-bit variant simply has some padding at the end */
  80. if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
  81. return -XFS_ERROR(EFAULT);
  82. return 0;
  83. }
  84. STATIC int
  85. xfs_compat_growfs_data_copyin(
  86. struct xfs_growfs_data *in,
  87. compat_xfs_growfs_data_t __user *arg32)
  88. {
  89. if (get_user(in->newblocks, &arg32->newblocks) ||
  90. get_user(in->imaxpct, &arg32->imaxpct))
  91. return -XFS_ERROR(EFAULT);
  92. return 0;
  93. }
  94. STATIC int
  95. xfs_compat_growfs_rt_copyin(
  96. struct xfs_growfs_rt *in,
  97. compat_xfs_growfs_rt_t __user *arg32)
  98. {
  99. if (get_user(in->newblocks, &arg32->newblocks) ||
  100. get_user(in->extsize, &arg32->extsize))
  101. return -XFS_ERROR(EFAULT);
  102. return 0;
  103. }
  104. STATIC int
  105. xfs_inumbers_fmt_compat(
  106. void __user *ubuffer,
  107. const xfs_inogrp_t *buffer,
  108. long count,
  109. long *written)
  110. {
  111. compat_xfs_inogrp_t __user *p32 = ubuffer;
  112. long i;
  113. for (i = 0; i < count; i++) {
  114. if (put_user(buffer[i].xi_startino, &p32[i].xi_startino) ||
  115. put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
  116. put_user(buffer[i].xi_allocmask, &p32[i].xi_allocmask))
  117. return -XFS_ERROR(EFAULT);
  118. }
  119. *written = count * sizeof(*p32);
  120. return 0;
  121. }
  122. #else
  123. #define xfs_inumbers_fmt_compat xfs_inumbers_fmt
  124. #endif
  125. STATIC int
  126. xfs_ioctl32_bstime_copyin(
  127. xfs_bstime_t *bstime,
  128. compat_xfs_bstime_t __user *bstime32)
  129. {
  130. compat_time_t sec32; /* tv_sec differs on 64 vs. 32 */
  131. if (get_user(sec32, &bstime32->tv_sec) ||
  132. get_user(bstime->tv_nsec, &bstime32->tv_nsec))
  133. return -XFS_ERROR(EFAULT);
  134. bstime->tv_sec = sec32;
  135. return 0;
  136. }
  137. /* xfs_bstat_t has differing alignment on intel, & bstime_t sizes everywhere */
  138. STATIC int
  139. xfs_ioctl32_bstat_copyin(
  140. xfs_bstat_t *bstat,
  141. compat_xfs_bstat_t __user *bstat32)
  142. {
  143. if (get_user(bstat->bs_ino, &bstat32->bs_ino) ||
  144. get_user(bstat->bs_mode, &bstat32->bs_mode) ||
  145. get_user(bstat->bs_nlink, &bstat32->bs_nlink) ||
  146. get_user(bstat->bs_uid, &bstat32->bs_uid) ||
  147. get_user(bstat->bs_gid, &bstat32->bs_gid) ||
  148. get_user(bstat->bs_rdev, &bstat32->bs_rdev) ||
  149. get_user(bstat->bs_blksize, &bstat32->bs_blksize) ||
  150. get_user(bstat->bs_size, &bstat32->bs_size) ||
  151. xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
  152. xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
  153. xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
  154. get_user(bstat->bs_blocks, &bstat32->bs_size) ||
  155. get_user(bstat->bs_xflags, &bstat32->bs_size) ||
  156. get_user(bstat->bs_extsize, &bstat32->bs_extsize) ||
  157. get_user(bstat->bs_extents, &bstat32->bs_extents) ||
  158. get_user(bstat->bs_gen, &bstat32->bs_gen) ||
  159. get_user(bstat->bs_projid, &bstat32->bs_projid) ||
  160. get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask) ||
  161. get_user(bstat->bs_dmstate, &bstat32->bs_dmstate) ||
  162. get_user(bstat->bs_aextents, &bstat32->bs_aextents))
  163. return -XFS_ERROR(EFAULT);
  164. return 0;
  165. }
  166. /* XFS_IOC_FSBULKSTAT and friends */
  167. STATIC int
  168. xfs_bstime_store_compat(
  169. compat_xfs_bstime_t __user *p32,
  170. const xfs_bstime_t *p)
  171. {
  172. __s32 sec32;
  173. sec32 = p->tv_sec;
  174. if (put_user(sec32, &p32->tv_sec) ||
  175. put_user(p->tv_nsec, &p32->tv_nsec))
  176. return -XFS_ERROR(EFAULT);
  177. return 0;
  178. }
  179. STATIC int
  180. xfs_bulkstat_one_fmt_compat(
  181. void __user *ubuffer,
  182. const xfs_bstat_t *buffer)
  183. {
  184. compat_xfs_bstat_t __user *p32 = ubuffer;
  185. if (put_user(buffer->bs_ino, &p32->bs_ino) ||
  186. put_user(buffer->bs_mode, &p32->bs_mode) ||
  187. put_user(buffer->bs_nlink, &p32->bs_nlink) ||
  188. put_user(buffer->bs_uid, &p32->bs_uid) ||
  189. put_user(buffer->bs_gid, &p32->bs_gid) ||
  190. put_user(buffer->bs_rdev, &p32->bs_rdev) ||
  191. put_user(buffer->bs_blksize, &p32->bs_blksize) ||
  192. put_user(buffer->bs_size, &p32->bs_size) ||
  193. xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
  194. xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
  195. xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
  196. put_user(buffer->bs_blocks, &p32->bs_blocks) ||
  197. put_user(buffer->bs_xflags, &p32->bs_xflags) ||
  198. put_user(buffer->bs_extsize, &p32->bs_extsize) ||
  199. put_user(buffer->bs_extents, &p32->bs_extents) ||
  200. put_user(buffer->bs_gen, &p32->bs_gen) ||
  201. put_user(buffer->bs_projid, &p32->bs_projid) ||
  202. put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
  203. put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
  204. put_user(buffer->bs_aextents, &p32->bs_aextents))
  205. return -XFS_ERROR(EFAULT);
  206. return sizeof(*p32);
  207. }
  208. /* copied from xfs_ioctl.c */
  209. STATIC int
  210. xfs_ioc_bulkstat_compat(
  211. xfs_mount_t *mp,
  212. unsigned int cmd,
  213. void __user *arg)
  214. {
  215. compat_xfs_fsop_bulkreq_t __user *p32 = (void __user *)arg;
  216. u32 addr;
  217. xfs_fsop_bulkreq_t bulkreq;
  218. int count; /* # of records returned */
  219. xfs_ino_t inlast; /* last inode number */
  220. int done;
  221. int error;
  222. /* done = 1 if there are more stats to get and if bulkstat */
  223. /* should be called again (unused here, but used in dmapi) */
  224. if (!capable(CAP_SYS_ADMIN))
  225. return -XFS_ERROR(EPERM);
  226. if (XFS_FORCED_SHUTDOWN(mp))
  227. return -XFS_ERROR(EIO);
  228. if (get_user(addr, &p32->lastip))
  229. return -XFS_ERROR(EFAULT);
  230. bulkreq.lastip = compat_ptr(addr);
  231. if (get_user(bulkreq.icount, &p32->icount) ||
  232. get_user(addr, &p32->ubuffer))
  233. return -XFS_ERROR(EFAULT);
  234. bulkreq.ubuffer = compat_ptr(addr);
  235. if (get_user(addr, &p32->ocount))
  236. return -XFS_ERROR(EFAULT);
  237. bulkreq.ocount = compat_ptr(addr);
  238. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  239. return -XFS_ERROR(EFAULT);
  240. if ((count = bulkreq.icount) <= 0)
  241. return -XFS_ERROR(EINVAL);
  242. if (bulkreq.ubuffer == NULL)
  243. return -XFS_ERROR(EINVAL);
  244. if (cmd == XFS_IOC_FSINUMBERS)
  245. error = xfs_inumbers(mp, &inlast, &count,
  246. bulkreq.ubuffer, xfs_inumbers_fmt_compat);
  247. else {
  248. /* declare a var to get a warning in case the type changes */
  249. bulkstat_one_fmt_pf formatter = xfs_bulkstat_one_fmt_compat;
  250. error = xfs_bulkstat(mp, &inlast, &count,
  251. xfs_bulkstat_one, formatter,
  252. sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
  253. BULKSTAT_FG_QUICK, &done);
  254. }
  255. if (error)
  256. return -error;
  257. if (bulkreq.ocount != NULL) {
  258. if (copy_to_user(bulkreq.lastip, &inlast,
  259. sizeof(xfs_ino_t)))
  260. return -XFS_ERROR(EFAULT);
  261. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  262. return -XFS_ERROR(EFAULT);
  263. }
  264. return 0;
  265. }
  266. STATIC int
  267. xfs_compat_handlereq_copyin(
  268. xfs_fsop_handlereq_t *hreq,
  269. compat_xfs_fsop_handlereq_t __user *arg32)
  270. {
  271. compat_xfs_fsop_handlereq_t hreq32;
  272. if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
  273. return -XFS_ERROR(EFAULT);
  274. hreq->fd = hreq32.fd;
  275. hreq->path = compat_ptr(hreq32.path);
  276. hreq->oflags = hreq32.oflags;
  277. hreq->ihandle = compat_ptr(hreq32.ihandle);
  278. hreq->ihandlen = hreq32.ihandlen;
  279. hreq->ohandle = compat_ptr(hreq32.ohandle);
  280. hreq->ohandlen = compat_ptr(hreq32.ohandlen);
  281. return 0;
  282. }
  283. STATIC long
  284. xfs_compat_ioctl(
  285. xfs_inode_t *ip,
  286. struct file *filp,
  287. int ioflags,
  288. unsigned cmd,
  289. void __user *arg)
  290. {
  291. struct inode *inode = filp->f_path.dentry->d_inode;
  292. xfs_mount_t *mp = ip->i_mount;
  293. int error;
  294. xfs_itrace_entry(XFS_I(inode));
  295. switch (cmd) {
  296. case XFS_IOC_DIOINFO:
  297. case XFS_IOC_FSGEOMETRY:
  298. case XFS_IOC_FSGETXATTR:
  299. case XFS_IOC_FSSETXATTR:
  300. case XFS_IOC_FSGETXATTRA:
  301. case XFS_IOC_FSSETDM:
  302. case XFS_IOC_GETBMAP:
  303. case XFS_IOC_GETBMAPA:
  304. case XFS_IOC_GETBMAPX:
  305. /* not handled
  306. case XFS_IOC_FSSETDM_BY_HANDLE:
  307. case XFS_IOC_ATTRLIST_BY_HANDLE:
  308. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  309. */
  310. case XFS_IOC_FSCOUNTS:
  311. case XFS_IOC_SET_RESBLKS:
  312. case XFS_IOC_GET_RESBLKS:
  313. case XFS_IOC_FSGROWFSDATA:
  314. case XFS_IOC_FSGROWFSLOG:
  315. case XFS_IOC_FSGROWFSRT:
  316. case XFS_IOC_FREEZE:
  317. case XFS_IOC_THAW:
  318. case XFS_IOC_GOINGDOWN:
  319. case XFS_IOC_ERROR_INJECTION:
  320. case XFS_IOC_ERROR_CLEARALL:
  321. break;
  322. case XFS_IOC_GETXFLAGS_32:
  323. case XFS_IOC_SETXFLAGS_32:
  324. case XFS_IOC_GETVERSION_32:
  325. cmd = _NATIVE_IOC(cmd, long);
  326. break;
  327. case XFS_IOC_SWAPEXT: {
  328. struct xfs_swapext sxp;
  329. struct compat_xfs_swapext __user *sxu = arg;
  330. /* Bulk copy in up to the sx_stat field, then grab bstat */
  331. if (copy_from_user(&sxp, sxu,
  332. offsetof(xfs_swapext_t, sx_stat)) ||
  333. xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
  334. return -XFS_ERROR(EFAULT);
  335. error = xfs_swapext(&sxp);
  336. return -error;
  337. }
  338. #ifdef BROKEN_X86_ALIGNMENT
  339. /* xfs_flock_t has wrong u32 vs u64 alignment */
  340. case XFS_IOC_ALLOCSP_32:
  341. case XFS_IOC_FREESP_32:
  342. case XFS_IOC_ALLOCSP64_32:
  343. case XFS_IOC_FREESP64_32:
  344. case XFS_IOC_RESVSP_32:
  345. case XFS_IOC_UNRESVSP_32:
  346. case XFS_IOC_RESVSP64_32:
  347. case XFS_IOC_UNRESVSP64_32: {
  348. struct xfs_flock64 bf;
  349. if (xfs_compat_flock64_copyin(&bf, arg))
  350. return -XFS_ERROR(EFAULT);
  351. cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
  352. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, &bf);
  353. }
  354. case XFS_IOC_FSGEOMETRY_V1_32:
  355. return xfs_compat_ioc_fsgeometry_v1(mp, arg);
  356. case XFS_IOC_FSGROWFSDATA_32: {
  357. struct xfs_growfs_data in;
  358. if (xfs_compat_growfs_data_copyin(&in, arg))
  359. return -XFS_ERROR(EFAULT);
  360. error = xfs_growfs_data(mp, &in);
  361. return -error;
  362. }
  363. case XFS_IOC_FSGROWFSRT_32: {
  364. struct xfs_growfs_rt in;
  365. if (xfs_compat_growfs_rt_copyin(&in, arg))
  366. return -XFS_ERROR(EFAULT);
  367. error = xfs_growfs_rt(mp, &in);
  368. return -error;
  369. }
  370. #else /* These are handled fine if no alignment issues */
  371. case XFS_IOC_ALLOCSP:
  372. case XFS_IOC_FREESP:
  373. case XFS_IOC_RESVSP:
  374. case XFS_IOC_UNRESVSP:
  375. case XFS_IOC_ALLOCSP64:
  376. case XFS_IOC_FREESP64:
  377. case XFS_IOC_RESVSP64:
  378. case XFS_IOC_UNRESVSP64:
  379. case XFS_IOC_FSGEOMETRY_V1:
  380. break;
  381. #endif
  382. case XFS_IOC_FSBULKSTAT_32:
  383. case XFS_IOC_FSBULKSTAT_SINGLE_32:
  384. case XFS_IOC_FSINUMBERS_32:
  385. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_bulkreq);
  386. return xfs_ioc_bulkstat_compat(XFS_I(inode)->i_mount,
  387. cmd, (void __user*)arg);
  388. case XFS_IOC_FD_TO_HANDLE_32:
  389. case XFS_IOC_PATH_TO_HANDLE_32:
  390. case XFS_IOC_PATH_TO_FSHANDLE_32: {
  391. struct xfs_fsop_handlereq hreq;
  392. if (xfs_compat_handlereq_copyin(&hreq, arg))
  393. return -XFS_ERROR(EFAULT);
  394. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
  395. return xfs_find_handle(cmd, &hreq);
  396. }
  397. case XFS_IOC_OPEN_BY_HANDLE_32: {
  398. struct xfs_fsop_handlereq hreq;
  399. if (xfs_compat_handlereq_copyin(&hreq, arg))
  400. return -XFS_ERROR(EFAULT);
  401. return xfs_open_by_handle(mp, &hreq, filp, inode);
  402. }
  403. case XFS_IOC_READLINK_BY_HANDLE_32: {
  404. struct xfs_fsop_handlereq hreq;
  405. if (xfs_compat_handlereq_copyin(&hreq, arg))
  406. return -XFS_ERROR(EFAULT);
  407. return xfs_readlink_by_handle(mp, &hreq, inode);
  408. }
  409. default:
  410. return -XFS_ERROR(ENOIOCTLCMD);
  411. }
  412. error = xfs_ioctl(ip, filp, ioflags, cmd, arg);
  413. return error;
  414. }
  415. long
  416. xfs_file_compat_ioctl(
  417. struct file *filp,
  418. unsigned int cmd,
  419. unsigned long p)
  420. {
  421. struct inode *inode = filp->f_path.dentry->d_inode;
  422. return xfs_compat_ioctl(XFS_I(inode), filp, 0, cmd, (void __user *)p);
  423. }
  424. long
  425. xfs_file_compat_invis_ioctl(
  426. struct file *filp,
  427. unsigned int cmd,
  428. unsigned long p)
  429. {
  430. struct inode *inode = filp->f_path.dentry->d_inode;
  431. return xfs_compat_ioctl(XFS_I(inode), filp, IO_INVIS, cmd,
  432. (void __user *)p);
  433. }