xfs_ioctl32.c 14 KB

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