xfs_ioctl32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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_vfs.h"
  40. #include "xfs_vnode.h"
  41. #include "xfs_dinode.h"
  42. #include "xfs_inode.h"
  43. #include "xfs_itable.h"
  44. #include "xfs_error.h"
  45. #include "xfs_dfrag.h"
  46. #include "xfs_vnodeops.h"
  47. #include "xfs_ioctl32.h"
  48. #define _NATIVE_IOC(cmd, type) \
  49. _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
  50. #if defined(CONFIG_IA64) || defined(CONFIG_X86_64)
  51. #define BROKEN_X86_ALIGNMENT
  52. #define _PACKED __attribute__((packed))
  53. /* on ia32 l_start is on a 32-bit boundary */
  54. typedef struct xfs_flock64_32 {
  55. __s16 l_type;
  56. __s16 l_whence;
  57. __s64 l_start __attribute__((packed));
  58. /* len == 0 means until end of file */
  59. __s64 l_len __attribute__((packed));
  60. __s32 l_sysid;
  61. __u32 l_pid;
  62. __s32 l_pad[4]; /* reserve area */
  63. } xfs_flock64_32_t;
  64. #define XFS_IOC_ALLOCSP_32 _IOW ('X', 10, struct xfs_flock64_32)
  65. #define XFS_IOC_FREESP_32 _IOW ('X', 11, struct xfs_flock64_32)
  66. #define XFS_IOC_ALLOCSP64_32 _IOW ('X', 36, struct xfs_flock64_32)
  67. #define XFS_IOC_FREESP64_32 _IOW ('X', 37, struct xfs_flock64_32)
  68. #define XFS_IOC_RESVSP_32 _IOW ('X', 40, struct xfs_flock64_32)
  69. #define XFS_IOC_UNRESVSP_32 _IOW ('X', 41, struct xfs_flock64_32)
  70. #define XFS_IOC_RESVSP64_32 _IOW ('X', 42, struct xfs_flock64_32)
  71. #define XFS_IOC_UNRESVSP64_32 _IOW ('X', 43, struct xfs_flock64_32)
  72. /* just account for different alignment */
  73. STATIC unsigned long
  74. xfs_ioctl32_flock(
  75. unsigned long arg)
  76. {
  77. xfs_flock64_32_t __user *p32 = (void __user *)arg;
  78. xfs_flock64_t __user *p = compat_alloc_user_space(sizeof(*p));
  79. if (copy_in_user(&p->l_type, &p32->l_type, sizeof(s16)) ||
  80. copy_in_user(&p->l_whence, &p32->l_whence, sizeof(s16)) ||
  81. copy_in_user(&p->l_start, &p32->l_start, sizeof(s64)) ||
  82. copy_in_user(&p->l_len, &p32->l_len, sizeof(s64)) ||
  83. copy_in_user(&p->l_sysid, &p32->l_sysid, sizeof(s32)) ||
  84. copy_in_user(&p->l_pid, &p32->l_pid, sizeof(u32)) ||
  85. copy_in_user(&p->l_pad, &p32->l_pad, 4*sizeof(u32)))
  86. return -EFAULT;
  87. return (unsigned long)p;
  88. }
  89. typedef struct compat_xfs_fsop_geom_v1 {
  90. __u32 blocksize; /* filesystem (data) block size */
  91. __u32 rtextsize; /* realtime extent size */
  92. __u32 agblocks; /* fsblocks in an AG */
  93. __u32 agcount; /* number of allocation groups */
  94. __u32 logblocks; /* fsblocks in the log */
  95. __u32 sectsize; /* (data) sector size, bytes */
  96. __u32 inodesize; /* inode size in bytes */
  97. __u32 imaxpct; /* max allowed inode space(%) */
  98. __u64 datablocks; /* fsblocks in data subvolume */
  99. __u64 rtblocks; /* fsblocks in realtime subvol */
  100. __u64 rtextents; /* rt extents in realtime subvol*/
  101. __u64 logstart; /* starting fsblock of the log */
  102. unsigned char uuid[16]; /* unique id of the filesystem */
  103. __u32 sunit; /* stripe unit, fsblocks */
  104. __u32 swidth; /* stripe width, fsblocks */
  105. __s32 version; /* structure version */
  106. __u32 flags; /* superblock version flags */
  107. __u32 logsectsize; /* log sector size, bytes */
  108. __u32 rtsectsize; /* realtime sector size, bytes */
  109. __u32 dirblocksize; /* directory block size, bytes */
  110. } __attribute__((packed)) compat_xfs_fsop_geom_v1_t;
  111. #define XFS_IOC_FSGEOMETRY_V1_32 \
  112. _IOR ('X', 100, struct compat_xfs_fsop_geom_v1)
  113. STATIC unsigned long xfs_ioctl32_geom_v1(unsigned long arg)
  114. {
  115. compat_xfs_fsop_geom_v1_t __user *p32 = (void __user *)arg;
  116. xfs_fsop_geom_v1_t __user *p = compat_alloc_user_space(sizeof(*p));
  117. if (copy_in_user(p, p32, sizeof(*p32)))
  118. return -EFAULT;
  119. return (unsigned long)p;
  120. }
  121. typedef struct compat_xfs_inogrp {
  122. __u64 xi_startino; /* starting inode number */
  123. __s32 xi_alloccount; /* # bits set in allocmask */
  124. __u64 xi_allocmask; /* mask of allocated inodes */
  125. } __attribute__((packed)) compat_xfs_inogrp_t;
  126. STATIC int xfs_inumbers_fmt_compat(
  127. void __user *ubuffer,
  128. const xfs_inogrp_t *buffer,
  129. long count,
  130. long *written)
  131. {
  132. compat_xfs_inogrp_t __user *p32 = ubuffer;
  133. long i;
  134. for (i = 0; i < count; i++) {
  135. if (put_user(buffer[i].xi_startino, &p32[i].xi_startino) ||
  136. put_user(buffer[i].xi_alloccount, &p32[i].xi_alloccount) ||
  137. put_user(buffer[i].xi_allocmask, &p32[i].xi_allocmask))
  138. return -EFAULT;
  139. }
  140. *written = count * sizeof(*p32);
  141. return 0;
  142. }
  143. #else
  144. #define xfs_inumbers_fmt_compat xfs_inumbers_fmt
  145. #define _PACKED
  146. #endif
  147. /* XFS_IOC_FSBULKSTAT and friends */
  148. typedef struct compat_xfs_bstime {
  149. __s32 tv_sec; /* seconds */
  150. __s32 tv_nsec; /* and nanoseconds */
  151. } compat_xfs_bstime_t;
  152. STATIC int xfs_bstime_store_compat(
  153. compat_xfs_bstime_t __user *p32,
  154. const xfs_bstime_t *p)
  155. {
  156. __s32 sec32;
  157. sec32 = p->tv_sec;
  158. if (put_user(sec32, &p32->tv_sec) ||
  159. put_user(p->tv_nsec, &p32->tv_nsec))
  160. return -EFAULT;
  161. return 0;
  162. }
  163. typedef struct compat_xfs_bstat {
  164. __u64 bs_ino; /* inode number */
  165. __u16 bs_mode; /* type and mode */
  166. __u16 bs_nlink; /* number of links */
  167. __u32 bs_uid; /* user id */
  168. __u32 bs_gid; /* group id */
  169. __u32 bs_rdev; /* device value */
  170. __s32 bs_blksize; /* block size */
  171. __s64 bs_size; /* file size */
  172. compat_xfs_bstime_t bs_atime; /* access time */
  173. compat_xfs_bstime_t bs_mtime; /* modify time */
  174. compat_xfs_bstime_t bs_ctime; /* inode change time */
  175. int64_t bs_blocks; /* number of blocks */
  176. __u32 bs_xflags; /* extended flags */
  177. __s32 bs_extsize; /* extent size */
  178. __s32 bs_extents; /* number of extents */
  179. __u32 bs_gen; /* generation count */
  180. __u16 bs_projid; /* project id */
  181. unsigned char bs_pad[14]; /* pad space, unused */
  182. __u32 bs_dmevmask; /* DMIG event mask */
  183. __u16 bs_dmstate; /* DMIG state info */
  184. __u16 bs_aextents; /* attribute number of extents */
  185. } _PACKED compat_xfs_bstat_t;
  186. STATIC int xfs_bulkstat_one_fmt_compat(
  187. void __user *ubuffer,
  188. const xfs_bstat_t *buffer)
  189. {
  190. compat_xfs_bstat_t __user *p32 = ubuffer;
  191. if (put_user(buffer->bs_ino, &p32->bs_ino) ||
  192. put_user(buffer->bs_mode, &p32->bs_mode) ||
  193. put_user(buffer->bs_nlink, &p32->bs_nlink) ||
  194. put_user(buffer->bs_uid, &p32->bs_uid) ||
  195. put_user(buffer->bs_gid, &p32->bs_gid) ||
  196. put_user(buffer->bs_rdev, &p32->bs_rdev) ||
  197. put_user(buffer->bs_blksize, &p32->bs_blksize) ||
  198. put_user(buffer->bs_size, &p32->bs_size) ||
  199. xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
  200. xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
  201. xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
  202. put_user(buffer->bs_blocks, &p32->bs_blocks) ||
  203. put_user(buffer->bs_xflags, &p32->bs_xflags) ||
  204. put_user(buffer->bs_extsize, &p32->bs_extsize) ||
  205. put_user(buffer->bs_extents, &p32->bs_extents) ||
  206. put_user(buffer->bs_gen, &p32->bs_gen) ||
  207. put_user(buffer->bs_projid, &p32->bs_projid) ||
  208. put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
  209. put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
  210. put_user(buffer->bs_aextents, &p32->bs_aextents))
  211. return -EFAULT;
  212. return sizeof(*p32);
  213. }
  214. typedef struct compat_xfs_fsop_bulkreq {
  215. compat_uptr_t lastip; /* last inode # pointer */
  216. __s32 icount; /* count of entries in buffer */
  217. compat_uptr_t ubuffer; /* user buffer for inode desc. */
  218. compat_uptr_t ocount; /* output count pointer */
  219. } compat_xfs_fsop_bulkreq_t;
  220. #define XFS_IOC_FSBULKSTAT_32 \
  221. _IOWR('X', 101, struct compat_xfs_fsop_bulkreq)
  222. #define XFS_IOC_FSBULKSTAT_SINGLE_32 \
  223. _IOWR('X', 102, struct compat_xfs_fsop_bulkreq)
  224. #define XFS_IOC_FSINUMBERS_32 \
  225. _IOWR('X', 103, struct compat_xfs_fsop_bulkreq)
  226. /* copied from xfs_ioctl.c */
  227. STATIC int
  228. xfs_ioc_bulkstat_compat(
  229. xfs_mount_t *mp,
  230. unsigned int cmd,
  231. void __user *arg)
  232. {
  233. compat_xfs_fsop_bulkreq_t __user *p32 = (void __user *)arg;
  234. u32 addr;
  235. xfs_fsop_bulkreq_t bulkreq;
  236. int count; /* # of records returned */
  237. xfs_ino_t inlast; /* last inode number */
  238. int done;
  239. int error;
  240. /* done = 1 if there are more stats to get and if bulkstat */
  241. /* should be called again (unused here, but used in dmapi) */
  242. if (!capable(CAP_SYS_ADMIN))
  243. return -EPERM;
  244. if (XFS_FORCED_SHUTDOWN(mp))
  245. return -XFS_ERROR(EIO);
  246. if (get_user(addr, &p32->lastip))
  247. return -EFAULT;
  248. bulkreq.lastip = compat_ptr(addr);
  249. if (get_user(bulkreq.icount, &p32->icount) ||
  250. get_user(addr, &p32->ubuffer))
  251. return -EFAULT;
  252. bulkreq.ubuffer = compat_ptr(addr);
  253. if (get_user(addr, &p32->ocount))
  254. return -EFAULT;
  255. bulkreq.ocount = compat_ptr(addr);
  256. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  257. return -XFS_ERROR(EFAULT);
  258. if ((count = bulkreq.icount) <= 0)
  259. return -XFS_ERROR(EINVAL);
  260. if (bulkreq.ubuffer == NULL)
  261. return -XFS_ERROR(EINVAL);
  262. if (cmd == XFS_IOC_FSINUMBERS)
  263. error = xfs_inumbers(mp, &inlast, &count,
  264. bulkreq.ubuffer, xfs_inumbers_fmt_compat);
  265. else {
  266. /* declare a var to get a warning in case the type changes */
  267. bulkstat_one_fmt_pf formatter = xfs_bulkstat_one_fmt_compat;
  268. error = xfs_bulkstat(mp, &inlast, &count,
  269. xfs_bulkstat_one, formatter,
  270. sizeof(compat_xfs_bstat_t), bulkreq.ubuffer,
  271. BULKSTAT_FG_QUICK, &done);
  272. }
  273. if (error)
  274. return -error;
  275. if (bulkreq.ocount != NULL) {
  276. if (copy_to_user(bulkreq.lastip, &inlast,
  277. sizeof(xfs_ino_t)))
  278. return -XFS_ERROR(EFAULT);
  279. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  280. return -XFS_ERROR(EFAULT);
  281. }
  282. return 0;
  283. }
  284. typedef struct compat_xfs_fsop_handlereq {
  285. __u32 fd; /* fd for FD_TO_HANDLE */
  286. compat_uptr_t path; /* user pathname */
  287. __u32 oflags; /* open flags */
  288. compat_uptr_t ihandle; /* user supplied handle */
  289. __u32 ihandlen; /* user supplied length */
  290. compat_uptr_t ohandle; /* user buffer for handle */
  291. compat_uptr_t ohandlen; /* user buffer length */
  292. } compat_xfs_fsop_handlereq_t;
  293. #define XFS_IOC_PATH_TO_FSHANDLE_32 \
  294. _IOWR('X', 104, struct compat_xfs_fsop_handlereq)
  295. #define XFS_IOC_PATH_TO_HANDLE_32 \
  296. _IOWR('X', 105, struct compat_xfs_fsop_handlereq)
  297. #define XFS_IOC_FD_TO_HANDLE_32 \
  298. _IOWR('X', 106, struct compat_xfs_fsop_handlereq)
  299. #define XFS_IOC_OPEN_BY_HANDLE_32 \
  300. _IOWR('X', 107, struct compat_xfs_fsop_handlereq)
  301. #define XFS_IOC_READLINK_BY_HANDLE_32 \
  302. _IOWR('X', 108, struct compat_xfs_fsop_handlereq)
  303. STATIC unsigned long xfs_ioctl32_fshandle(unsigned long arg)
  304. {
  305. compat_xfs_fsop_handlereq_t __user *p32 = (void __user *)arg;
  306. xfs_fsop_handlereq_t __user *p = compat_alloc_user_space(sizeof(*p));
  307. u32 addr;
  308. if (copy_in_user(&p->fd, &p32->fd, sizeof(__u32)) ||
  309. get_user(addr, &p32->path) ||
  310. put_user(compat_ptr(addr), &p->path) ||
  311. copy_in_user(&p->oflags, &p32->oflags, sizeof(__u32)) ||
  312. get_user(addr, &p32->ihandle) ||
  313. put_user(compat_ptr(addr), &p->ihandle) ||
  314. copy_in_user(&p->ihandlen, &p32->ihandlen, sizeof(__u32)) ||
  315. get_user(addr, &p32->ohandle) ||
  316. put_user(compat_ptr(addr), &p->ohandle) ||
  317. get_user(addr, &p32->ohandlen) ||
  318. put_user(compat_ptr(addr), &p->ohandlen))
  319. return -EFAULT;
  320. return (unsigned long)p;
  321. }
  322. STATIC long
  323. xfs_compat_ioctl(
  324. int mode,
  325. struct file *file,
  326. unsigned cmd,
  327. unsigned long arg)
  328. {
  329. struct inode *inode = file->f_path.dentry->d_inode;
  330. int error;
  331. switch (cmd) {
  332. case XFS_IOC_DIOINFO:
  333. case XFS_IOC_FSGEOMETRY:
  334. case XFS_IOC_FSGETXATTR:
  335. case XFS_IOC_FSSETXATTR:
  336. case XFS_IOC_FSGETXATTRA:
  337. case XFS_IOC_FSSETDM:
  338. case XFS_IOC_GETBMAP:
  339. case XFS_IOC_GETBMAPA:
  340. case XFS_IOC_GETBMAPX:
  341. /* not handled
  342. case XFS_IOC_FSSETDM_BY_HANDLE:
  343. case XFS_IOC_ATTRLIST_BY_HANDLE:
  344. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  345. */
  346. case XFS_IOC_FSCOUNTS:
  347. case XFS_IOC_SET_RESBLKS:
  348. case XFS_IOC_GET_RESBLKS:
  349. case XFS_IOC_FSGROWFSDATA:
  350. case XFS_IOC_FSGROWFSLOG:
  351. case XFS_IOC_FSGROWFSRT:
  352. case XFS_IOC_FREEZE:
  353. case XFS_IOC_THAW:
  354. case XFS_IOC_GOINGDOWN:
  355. case XFS_IOC_ERROR_INJECTION:
  356. case XFS_IOC_ERROR_CLEARALL:
  357. break;
  358. case XFS_IOC32_GETXFLAGS:
  359. case XFS_IOC32_SETXFLAGS:
  360. case XFS_IOC32_GETVERSION:
  361. cmd = _NATIVE_IOC(cmd, long);
  362. break;
  363. #ifdef BROKEN_X86_ALIGNMENT
  364. /* xfs_flock_t has wrong u32 vs u64 alignment */
  365. case XFS_IOC_ALLOCSP_32:
  366. case XFS_IOC_FREESP_32:
  367. case XFS_IOC_ALLOCSP64_32:
  368. case XFS_IOC_FREESP64_32:
  369. case XFS_IOC_RESVSP_32:
  370. case XFS_IOC_UNRESVSP_32:
  371. case XFS_IOC_RESVSP64_32:
  372. case XFS_IOC_UNRESVSP64_32:
  373. arg = xfs_ioctl32_flock(arg);
  374. cmd = _NATIVE_IOC(cmd, struct xfs_flock64);
  375. break;
  376. case XFS_IOC_FSGEOMETRY_V1_32:
  377. arg = xfs_ioctl32_geom_v1(arg);
  378. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_geom_v1);
  379. break;
  380. #else /* These are handled fine if no alignment issues */
  381. case XFS_IOC_ALLOCSP:
  382. case XFS_IOC_FREESP:
  383. case XFS_IOC_RESVSP:
  384. case XFS_IOC_UNRESVSP:
  385. case XFS_IOC_ALLOCSP64:
  386. case XFS_IOC_FREESP64:
  387. case XFS_IOC_RESVSP64:
  388. case XFS_IOC_UNRESVSP64:
  389. case XFS_IOC_FSGEOMETRY_V1:
  390. break;
  391. /* xfs_bstat_t still has wrong u32 vs u64 alignment */
  392. case XFS_IOC_SWAPEXT:
  393. break;
  394. #endif
  395. case XFS_IOC_FSBULKSTAT_32:
  396. case XFS_IOC_FSBULKSTAT_SINGLE_32:
  397. case XFS_IOC_FSINUMBERS_32:
  398. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_bulkreq);
  399. return xfs_ioc_bulkstat_compat(XFS_I(inode)->i_mount,
  400. cmd, (void __user*)arg);
  401. case XFS_IOC_FD_TO_HANDLE_32:
  402. case XFS_IOC_PATH_TO_HANDLE_32:
  403. case XFS_IOC_PATH_TO_FSHANDLE_32:
  404. case XFS_IOC_OPEN_BY_HANDLE_32:
  405. case XFS_IOC_READLINK_BY_HANDLE_32:
  406. arg = xfs_ioctl32_fshandle(arg);
  407. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
  408. break;
  409. default:
  410. return -ENOIOCTLCMD;
  411. }
  412. error = xfs_ioctl(XFS_I(inode), file, mode, cmd, (void __user *)arg);
  413. xfs_iflags_set(XFS_I(inode), XFS_IMODIFIED);
  414. return error;
  415. }
  416. long
  417. xfs_file_compat_ioctl(
  418. struct file *file,
  419. unsigned cmd,
  420. unsigned long arg)
  421. {
  422. return xfs_compat_ioctl(0, file, cmd, arg);
  423. }
  424. long
  425. xfs_file_compat_invis_ioctl(
  426. struct file *file,
  427. unsigned cmd,
  428. unsigned long arg)
  429. {
  430. return xfs_compat_ioctl(IO_INVIS, file, cmd, arg);
  431. }