xfs_ioctl32.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2004-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include <linux/mount.h>
  7. #include <linux/fsmap.h>
  8. #include "xfs.h"
  9. #include "xfs_fs.h"
  10. #include "xfs_shared.h"
  11. #include "xfs_format.h"
  12. #include "xfs_log_format.h"
  13. #include "xfs_trans_resv.h"
  14. #include "xfs_mount.h"
  15. #include "xfs_inode.h"
  16. #include "xfs_iwalk.h"
  17. #include "xfs_itable.h"
  18. #include "xfs_fsops.h"
  19. #include "xfs_rtalloc.h"
  20. #include "xfs_da_format.h"
  21. #include "xfs_da_btree.h"
  22. #include "xfs_attr.h"
  23. #include "xfs_ioctl.h"
  24. #include "xfs_ioctl32.h"
  25. #include "xfs_trace.h"
  26. #include "xfs_sb.h"
  27. #define _NATIVE_IOC(cmd, type) \
  28. _IOC(_IOC_DIR(cmd), _IOC_TYPE(cmd), _IOC_NR(cmd), sizeof(type))
  29. #ifdef BROKEN_X86_ALIGNMENT
  30. STATIC int
  31. xfs_compat_ioc_fsgeometry_v1(
  32. struct xfs_mount *mp,
  33. compat_xfs_fsop_geom_v1_t __user *arg32)
  34. {
  35. struct xfs_fsop_geom fsgeo;
  36. xfs_fs_geometry(mp, &fsgeo, 3);
  37. /* The 32-bit variant simply has some padding at the end */
  38. if (copy_to_user(arg32, &fsgeo, sizeof(struct compat_xfs_fsop_geom_v1)))
  39. return -EFAULT;
  40. return 0;
  41. }
  42. STATIC int
  43. xfs_compat_growfs_data_copyin(
  44. struct xfs_growfs_data *in,
  45. compat_xfs_growfs_data_t __user *arg32)
  46. {
  47. if (get_user(in->newblocks, &arg32->newblocks) ||
  48. get_user(in->imaxpct, &arg32->imaxpct))
  49. return -EFAULT;
  50. return 0;
  51. }
  52. STATIC int
  53. xfs_compat_growfs_rt_copyin(
  54. struct xfs_growfs_rt *in,
  55. compat_xfs_growfs_rt_t __user *arg32)
  56. {
  57. if (get_user(in->newblocks, &arg32->newblocks) ||
  58. get_user(in->extsize, &arg32->extsize))
  59. return -EFAULT;
  60. return 0;
  61. }
  62. STATIC int
  63. xfs_fsinumbers_fmt_compat(
  64. struct xfs_ibulk *breq,
  65. const struct xfs_inumbers *ig)
  66. {
  67. struct compat_xfs_inogrp __user *p32 = breq->ubuffer;
  68. struct xfs_inogrp ig1;
  69. struct xfs_inogrp *igrp = &ig1;
  70. xfs_inumbers_to_inogrp(&ig1, ig);
  71. if (put_user(igrp->xi_startino, &p32->xi_startino) ||
  72. put_user(igrp->xi_alloccount, &p32->xi_alloccount) ||
  73. put_user(igrp->xi_allocmask, &p32->xi_allocmask))
  74. return -EFAULT;
  75. return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_inogrp));
  76. }
  77. #else
  78. #define xfs_fsinumbers_fmt_compat xfs_fsinumbers_fmt
  79. #endif /* BROKEN_X86_ALIGNMENT */
  80. STATIC int
  81. xfs_ioctl32_bstime_copyin(
  82. xfs_bstime_t *bstime,
  83. compat_xfs_bstime_t __user *bstime32)
  84. {
  85. old_time32_t sec32; /* tv_sec differs on 64 vs. 32 */
  86. if (get_user(sec32, &bstime32->tv_sec) ||
  87. get_user(bstime->tv_nsec, &bstime32->tv_nsec))
  88. return -EFAULT;
  89. bstime->tv_sec = sec32;
  90. return 0;
  91. }
  92. /*
  93. * struct xfs_bstat has differing alignment on intel, & bstime_t sizes
  94. * everywhere
  95. */
  96. STATIC int
  97. xfs_ioctl32_bstat_copyin(
  98. struct xfs_bstat *bstat,
  99. struct compat_xfs_bstat __user *bstat32)
  100. {
  101. if (get_user(bstat->bs_ino, &bstat32->bs_ino) ||
  102. get_user(bstat->bs_mode, &bstat32->bs_mode) ||
  103. get_user(bstat->bs_nlink, &bstat32->bs_nlink) ||
  104. get_user(bstat->bs_uid, &bstat32->bs_uid) ||
  105. get_user(bstat->bs_gid, &bstat32->bs_gid) ||
  106. get_user(bstat->bs_rdev, &bstat32->bs_rdev) ||
  107. get_user(bstat->bs_blksize, &bstat32->bs_blksize) ||
  108. get_user(bstat->bs_size, &bstat32->bs_size) ||
  109. xfs_ioctl32_bstime_copyin(&bstat->bs_atime, &bstat32->bs_atime) ||
  110. xfs_ioctl32_bstime_copyin(&bstat->bs_mtime, &bstat32->bs_mtime) ||
  111. xfs_ioctl32_bstime_copyin(&bstat->bs_ctime, &bstat32->bs_ctime) ||
  112. get_user(bstat->bs_blocks, &bstat32->bs_size) ||
  113. get_user(bstat->bs_xflags, &bstat32->bs_size) ||
  114. get_user(bstat->bs_extsize, &bstat32->bs_extsize) ||
  115. get_user(bstat->bs_extents, &bstat32->bs_extents) ||
  116. get_user(bstat->bs_gen, &bstat32->bs_gen) ||
  117. get_user(bstat->bs_projid_lo, &bstat32->bs_projid_lo) ||
  118. get_user(bstat->bs_projid_hi, &bstat32->bs_projid_hi) ||
  119. get_user(bstat->bs_forkoff, &bstat32->bs_forkoff) ||
  120. get_user(bstat->bs_dmevmask, &bstat32->bs_dmevmask) ||
  121. get_user(bstat->bs_dmstate, &bstat32->bs_dmstate) ||
  122. get_user(bstat->bs_aextents, &bstat32->bs_aextents))
  123. return -EFAULT;
  124. return 0;
  125. }
  126. /* XFS_IOC_FSBULKSTAT and friends */
  127. STATIC int
  128. xfs_bstime_store_compat(
  129. compat_xfs_bstime_t __user *p32,
  130. const xfs_bstime_t *p)
  131. {
  132. __s32 sec32;
  133. sec32 = p->tv_sec;
  134. if (put_user(sec32, &p32->tv_sec) ||
  135. put_user(p->tv_nsec, &p32->tv_nsec))
  136. return -EFAULT;
  137. return 0;
  138. }
  139. /* Return 0 on success or positive error (to xfs_bulkstat()) */
  140. STATIC int
  141. xfs_fsbulkstat_one_fmt_compat(
  142. struct xfs_ibulk *breq,
  143. const struct xfs_bulkstat *bstat)
  144. {
  145. struct compat_xfs_bstat __user *p32 = breq->ubuffer;
  146. struct xfs_bstat bs1;
  147. struct xfs_bstat *buffer = &bs1;
  148. xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
  149. if (put_user(buffer->bs_ino, &p32->bs_ino) ||
  150. put_user(buffer->bs_mode, &p32->bs_mode) ||
  151. put_user(buffer->bs_nlink, &p32->bs_nlink) ||
  152. put_user(buffer->bs_uid, &p32->bs_uid) ||
  153. put_user(buffer->bs_gid, &p32->bs_gid) ||
  154. put_user(buffer->bs_rdev, &p32->bs_rdev) ||
  155. put_user(buffer->bs_blksize, &p32->bs_blksize) ||
  156. put_user(buffer->bs_size, &p32->bs_size) ||
  157. xfs_bstime_store_compat(&p32->bs_atime, &buffer->bs_atime) ||
  158. xfs_bstime_store_compat(&p32->bs_mtime, &buffer->bs_mtime) ||
  159. xfs_bstime_store_compat(&p32->bs_ctime, &buffer->bs_ctime) ||
  160. put_user(buffer->bs_blocks, &p32->bs_blocks) ||
  161. put_user(buffer->bs_xflags, &p32->bs_xflags) ||
  162. put_user(buffer->bs_extsize, &p32->bs_extsize) ||
  163. put_user(buffer->bs_extents, &p32->bs_extents) ||
  164. put_user(buffer->bs_gen, &p32->bs_gen) ||
  165. put_user(buffer->bs_projid, &p32->bs_projid) ||
  166. put_user(buffer->bs_projid_hi, &p32->bs_projid_hi) ||
  167. put_user(buffer->bs_forkoff, &p32->bs_forkoff) ||
  168. put_user(buffer->bs_dmevmask, &p32->bs_dmevmask) ||
  169. put_user(buffer->bs_dmstate, &p32->bs_dmstate) ||
  170. put_user(buffer->bs_aextents, &p32->bs_aextents))
  171. return -EFAULT;
  172. return xfs_ibulk_advance(breq, sizeof(struct compat_xfs_bstat));
  173. }
  174. /* copied from xfs_ioctl.c */
  175. STATIC int
  176. xfs_compat_ioc_fsbulkstat(
  177. struct file *file,
  178. unsigned int cmd,
  179. struct compat_xfs_fsop_bulkreq __user *p32)
  180. {
  181. struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
  182. u32 addr;
  183. struct xfs_fsop_bulkreq bulkreq;
  184. struct xfs_ibulk breq = {
  185. .mp = mp,
  186. .mnt_userns = file_mnt_user_ns(file),
  187. .ocount = 0,
  188. };
  189. xfs_ino_t lastino;
  190. int error;
  191. /*
  192. * Output structure handling functions. Depending on the command,
  193. * either the xfs_bstat and xfs_inogrp structures are written out
  194. * to userpace memory via bulkreq.ubuffer. Normally the compat
  195. * functions and structure size are the correct ones to use ...
  196. */
  197. inumbers_fmt_pf inumbers_func = xfs_fsinumbers_fmt_compat;
  198. bulkstat_one_fmt_pf bs_one_func = xfs_fsbulkstat_one_fmt_compat;
  199. #ifdef CONFIG_X86_X32_ABI
  200. if (in_x32_syscall()) {
  201. /*
  202. * ... but on x32 the input xfs_fsop_bulkreq has pointers
  203. * which must be handled in the "compat" (32-bit) way, while
  204. * the xfs_bstat and xfs_inogrp structures follow native 64-
  205. * bit layout convention. So adjust accordingly, otherwise
  206. * the data written out in compat layout will not match what
  207. * x32 userspace expects.
  208. */
  209. inumbers_func = xfs_fsinumbers_fmt;
  210. bs_one_func = xfs_fsbulkstat_one_fmt;
  211. }
  212. #endif
  213. /* done = 1 if there are more stats to get and if bulkstat */
  214. /* should be called again (unused here, but used in dmapi) */
  215. if (!capable(CAP_SYS_ADMIN))
  216. return -EPERM;
  217. if (xfs_is_shutdown(mp))
  218. return -EIO;
  219. if (get_user(addr, &p32->lastip))
  220. return -EFAULT;
  221. bulkreq.lastip = compat_ptr(addr);
  222. if (get_user(bulkreq.icount, &p32->icount) ||
  223. get_user(addr, &p32->ubuffer))
  224. return -EFAULT;
  225. bulkreq.ubuffer = compat_ptr(addr);
  226. if (get_user(addr, &p32->ocount))
  227. return -EFAULT;
  228. bulkreq.ocount = compat_ptr(addr);
  229. if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
  230. return -EFAULT;
  231. if (bulkreq.icount <= 0)
  232. return -EINVAL;
  233. if (bulkreq.ubuffer == NULL)
  234. return -EINVAL;
  235. breq.ubuffer = bulkreq.ubuffer;
  236. breq.icount = bulkreq.icount;
  237. /*
  238. * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
  239. * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
  240. * that *lastip contains either zero or the number of the last inode to
  241. * be examined by the previous call and return results starting with
  242. * the next inode after that. The new bulk request back end functions
  243. * take the inode to start with, so we have to compute the startino
  244. * parameter from lastino to maintain correct function. lastino == 0
  245. * is a special case because it has traditionally meant "first inode
  246. * in filesystem".
  247. */
  248. if (cmd == XFS_IOC_FSINUMBERS_32) {
  249. breq.startino = lastino ? lastino + 1 : 0;
  250. error = xfs_inumbers(&breq, inumbers_func);
  251. lastino = breq.startino - 1;
  252. } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE_32) {
  253. breq.startino = lastino;
  254. breq.icount = 1;
  255. error = xfs_bulkstat_one(&breq, bs_one_func);
  256. lastino = breq.startino;
  257. } else if (cmd == XFS_IOC_FSBULKSTAT_32) {
  258. breq.startino = lastino ? lastino + 1 : 0;
  259. error = xfs_bulkstat(&breq, bs_one_func);
  260. lastino = breq.startino - 1;
  261. } else {
  262. error = -EINVAL;
  263. }
  264. if (error)
  265. return error;
  266. if (bulkreq.lastip != NULL &&
  267. copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
  268. return -EFAULT;
  269. if (bulkreq.ocount != NULL &&
  270. copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
  271. return -EFAULT;
  272. return 0;
  273. }
  274. STATIC int
  275. xfs_compat_handlereq_copyin(
  276. xfs_fsop_handlereq_t *hreq,
  277. compat_xfs_fsop_handlereq_t __user *arg32)
  278. {
  279. compat_xfs_fsop_handlereq_t hreq32;
  280. if (copy_from_user(&hreq32, arg32, sizeof(compat_xfs_fsop_handlereq_t)))
  281. return -EFAULT;
  282. hreq->fd = hreq32.fd;
  283. hreq->path = compat_ptr(hreq32.path);
  284. hreq->oflags = hreq32.oflags;
  285. hreq->ihandle = compat_ptr(hreq32.ihandle);
  286. hreq->ihandlen = hreq32.ihandlen;
  287. hreq->ohandle = compat_ptr(hreq32.ohandle);
  288. hreq->ohandlen = compat_ptr(hreq32.ohandlen);
  289. return 0;
  290. }
  291. STATIC struct dentry *
  292. xfs_compat_handlereq_to_dentry(
  293. struct file *parfilp,
  294. compat_xfs_fsop_handlereq_t *hreq)
  295. {
  296. return xfs_handle_to_dentry(parfilp,
  297. compat_ptr(hreq->ihandle), hreq->ihandlen);
  298. }
  299. STATIC int
  300. xfs_compat_attrlist_by_handle(
  301. struct file *parfilp,
  302. compat_xfs_fsop_attrlist_handlereq_t __user *p)
  303. {
  304. compat_xfs_fsop_attrlist_handlereq_t al_hreq;
  305. struct dentry *dentry;
  306. int error;
  307. if (!capable(CAP_SYS_ADMIN))
  308. return -EPERM;
  309. if (copy_from_user(&al_hreq, p, sizeof(al_hreq)))
  310. return -EFAULT;
  311. dentry = xfs_compat_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  312. if (IS_ERR(dentry))
  313. return PTR_ERR(dentry);
  314. error = xfs_ioc_attr_list(XFS_I(d_inode(dentry)),
  315. compat_ptr(al_hreq.buffer), al_hreq.buflen,
  316. al_hreq.flags, &p->pos);
  317. dput(dentry);
  318. return error;
  319. }
  320. STATIC int
  321. xfs_compat_attrmulti_by_handle(
  322. struct file *parfilp,
  323. void __user *arg)
  324. {
  325. int error;
  326. compat_xfs_attr_multiop_t *ops;
  327. compat_xfs_fsop_attrmulti_handlereq_t am_hreq;
  328. struct dentry *dentry;
  329. unsigned int i, size;
  330. if (!capable(CAP_SYS_ADMIN))
  331. return -EPERM;
  332. if (copy_from_user(&am_hreq, arg,
  333. sizeof(compat_xfs_fsop_attrmulti_handlereq_t)))
  334. return -EFAULT;
  335. /* overflow check */
  336. if (am_hreq.opcount >= INT_MAX / sizeof(compat_xfs_attr_multiop_t))
  337. return -E2BIG;
  338. dentry = xfs_compat_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  339. if (IS_ERR(dentry))
  340. return PTR_ERR(dentry);
  341. error = -E2BIG;
  342. size = am_hreq.opcount * sizeof(compat_xfs_attr_multiop_t);
  343. if (!size || size > 16 * PAGE_SIZE)
  344. goto out_dput;
  345. ops = memdup_user(compat_ptr(am_hreq.ops), size);
  346. if (IS_ERR(ops)) {
  347. error = PTR_ERR(ops);
  348. goto out_dput;
  349. }
  350. error = 0;
  351. for (i = 0; i < am_hreq.opcount; i++) {
  352. ops[i].am_error = xfs_ioc_attrmulti_one(parfilp,
  353. d_inode(dentry), ops[i].am_opcode,
  354. compat_ptr(ops[i].am_attrname),
  355. compat_ptr(ops[i].am_attrvalue),
  356. &ops[i].am_length, ops[i].am_flags);
  357. }
  358. if (copy_to_user(compat_ptr(am_hreq.ops), ops, size))
  359. error = -EFAULT;
  360. kfree(ops);
  361. out_dput:
  362. dput(dentry);
  363. return error;
  364. }
  365. long
  366. xfs_file_compat_ioctl(
  367. struct file *filp,
  368. unsigned cmd,
  369. unsigned long p)
  370. {
  371. struct inode *inode = file_inode(filp);
  372. struct xfs_inode *ip = XFS_I(inode);
  373. void __user *arg = compat_ptr(p);
  374. int error;
  375. trace_xfs_file_compat_ioctl(ip);
  376. switch (cmd) {
  377. #if defined(BROKEN_X86_ALIGNMENT)
  378. case XFS_IOC_FSGEOMETRY_V1_32:
  379. return xfs_compat_ioc_fsgeometry_v1(ip->i_mount, arg);
  380. case XFS_IOC_FSGROWFSDATA_32: {
  381. struct xfs_growfs_data in;
  382. if (xfs_compat_growfs_data_copyin(&in, arg))
  383. return -EFAULT;
  384. error = mnt_want_write_file(filp);
  385. if (error)
  386. return error;
  387. error = xfs_growfs_data(ip->i_mount, &in);
  388. mnt_drop_write_file(filp);
  389. return error;
  390. }
  391. case XFS_IOC_FSGROWFSRT_32: {
  392. struct xfs_growfs_rt in;
  393. if (xfs_compat_growfs_rt_copyin(&in, arg))
  394. return -EFAULT;
  395. error = mnt_want_write_file(filp);
  396. if (error)
  397. return error;
  398. error = xfs_growfs_rt(ip->i_mount, &in);
  399. mnt_drop_write_file(filp);
  400. return error;
  401. }
  402. #endif
  403. /* long changes size, but xfs only copiese out 32 bits */
  404. case XFS_IOC_GETVERSION_32:
  405. cmd = _NATIVE_IOC(cmd, long);
  406. return xfs_file_ioctl(filp, cmd, p);
  407. case XFS_IOC_SWAPEXT_32: {
  408. struct xfs_swapext sxp;
  409. struct compat_xfs_swapext __user *sxu = arg;
  410. /* Bulk copy in up to the sx_stat field, then copy bstat */
  411. if (copy_from_user(&sxp, sxu,
  412. offsetof(struct xfs_swapext, sx_stat)) ||
  413. xfs_ioctl32_bstat_copyin(&sxp.sx_stat, &sxu->sx_stat))
  414. return -EFAULT;
  415. error = mnt_want_write_file(filp);
  416. if (error)
  417. return error;
  418. error = xfs_ioc_swapext(&sxp);
  419. mnt_drop_write_file(filp);
  420. return error;
  421. }
  422. case XFS_IOC_FSBULKSTAT_32:
  423. case XFS_IOC_FSBULKSTAT_SINGLE_32:
  424. case XFS_IOC_FSINUMBERS_32:
  425. return xfs_compat_ioc_fsbulkstat(filp, cmd, arg);
  426. case XFS_IOC_FD_TO_HANDLE_32:
  427. case XFS_IOC_PATH_TO_HANDLE_32:
  428. case XFS_IOC_PATH_TO_FSHANDLE_32: {
  429. struct xfs_fsop_handlereq hreq;
  430. if (xfs_compat_handlereq_copyin(&hreq, arg))
  431. return -EFAULT;
  432. cmd = _NATIVE_IOC(cmd, struct xfs_fsop_handlereq);
  433. return xfs_find_handle(cmd, &hreq);
  434. }
  435. case XFS_IOC_OPEN_BY_HANDLE_32: {
  436. struct xfs_fsop_handlereq hreq;
  437. if (xfs_compat_handlereq_copyin(&hreq, arg))
  438. return -EFAULT;
  439. return xfs_open_by_handle(filp, &hreq);
  440. }
  441. case XFS_IOC_READLINK_BY_HANDLE_32: {
  442. struct xfs_fsop_handlereq hreq;
  443. if (xfs_compat_handlereq_copyin(&hreq, arg))
  444. return -EFAULT;
  445. return xfs_readlink_by_handle(filp, &hreq);
  446. }
  447. case XFS_IOC_ATTRLIST_BY_HANDLE_32:
  448. return xfs_compat_attrlist_by_handle(filp, arg);
  449. case XFS_IOC_ATTRMULTI_BY_HANDLE_32:
  450. return xfs_compat_attrmulti_by_handle(filp, arg);
  451. default:
  452. /* try the native version */
  453. return xfs_file_ioctl(filp, cmd, (unsigned long)arg);
  454. }
  455. }