xfs_ioctl.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_rtalloc.h"
  15. #include "xfs_iwalk.h"
  16. #include "xfs_itable.h"
  17. #include "xfs_error.h"
  18. #include "xfs_da_format.h"
  19. #include "xfs_da_btree.h"
  20. #include "xfs_attr.h"
  21. #include "xfs_bmap.h"
  22. #include "xfs_bmap_util.h"
  23. #include "xfs_fsops.h"
  24. #include "xfs_discard.h"
  25. #include "xfs_quota.h"
  26. #include "xfs_trace.h"
  27. #include "xfs_icache.h"
  28. #include "xfs_trans.h"
  29. #include "xfs_btree.h"
  30. #include <linux/fsmap.h>
  31. #include "xfs_fsmap.h"
  32. #include "scrub/xfs_scrub.h"
  33. #include "xfs_sb.h"
  34. #include "xfs_ag.h"
  35. #include "xfs_health.h"
  36. #include "xfs_reflink.h"
  37. #include "xfs_ioctl.h"
  38. #include "xfs_xattr.h"
  39. #include "xfs_rtbitmap.h"
  40. #include "xfs_file.h"
  41. #include "xfs_exchrange.h"
  42. #include "xfs_handle.h"
  43. #include <linux/mount.h>
  44. #include <linux/fileattr.h>
  45. /* Return 0 on success or positive error */
  46. int
  47. xfs_fsbulkstat_one_fmt(
  48. struct xfs_ibulk *breq,
  49. const struct xfs_bulkstat *bstat)
  50. {
  51. struct xfs_bstat bs1;
  52. xfs_bulkstat_to_bstat(breq->mp, &bs1, bstat);
  53. if (copy_to_user(breq->ubuffer, &bs1, sizeof(bs1)))
  54. return -EFAULT;
  55. return xfs_ibulk_advance(breq, sizeof(struct xfs_bstat));
  56. }
  57. int
  58. xfs_fsinumbers_fmt(
  59. struct xfs_ibulk *breq,
  60. const struct xfs_inumbers *igrp)
  61. {
  62. struct xfs_inogrp ig1;
  63. xfs_inumbers_to_inogrp(&ig1, igrp);
  64. if (copy_to_user(breq->ubuffer, &ig1, sizeof(struct xfs_inogrp)))
  65. return -EFAULT;
  66. return xfs_ibulk_advance(breq, sizeof(struct xfs_inogrp));
  67. }
  68. STATIC int
  69. xfs_ioc_fsbulkstat(
  70. struct file *file,
  71. unsigned int cmd,
  72. void __user *arg)
  73. {
  74. struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
  75. struct xfs_fsop_bulkreq bulkreq;
  76. struct xfs_ibulk breq = {
  77. .mp = mp,
  78. .idmap = file_mnt_idmap(file),
  79. .ocount = 0,
  80. };
  81. xfs_ino_t lastino;
  82. int error;
  83. /* done = 1 if there are more stats to get and if bulkstat */
  84. /* should be called again (unused here, but used in dmapi) */
  85. if (!capable(CAP_SYS_ADMIN))
  86. return -EPERM;
  87. if (xfs_is_shutdown(mp))
  88. return -EIO;
  89. if (copy_from_user(&bulkreq, arg, sizeof(struct xfs_fsop_bulkreq)))
  90. return -EFAULT;
  91. if (copy_from_user(&lastino, bulkreq.lastip, sizeof(__s64)))
  92. return -EFAULT;
  93. if (bulkreq.icount <= 0)
  94. return -EINVAL;
  95. if (bulkreq.ubuffer == NULL)
  96. return -EINVAL;
  97. breq.ubuffer = bulkreq.ubuffer;
  98. breq.icount = bulkreq.icount;
  99. /*
  100. * FSBULKSTAT_SINGLE expects that *lastip contains the inode number
  101. * that we want to stat. However, FSINUMBERS and FSBULKSTAT expect
  102. * that *lastip contains either zero or the number of the last inode to
  103. * be examined by the previous call and return results starting with
  104. * the next inode after that. The new bulk request back end functions
  105. * take the inode to start with, so we have to compute the startino
  106. * parameter from lastino to maintain correct function. lastino == 0
  107. * is a special case because it has traditionally meant "first inode
  108. * in filesystem".
  109. */
  110. if (cmd == XFS_IOC_FSINUMBERS) {
  111. breq.startino = lastino ? lastino + 1 : 0;
  112. error = xfs_inumbers(&breq, xfs_fsinumbers_fmt);
  113. lastino = breq.startino - 1;
  114. } else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE) {
  115. breq.startino = lastino;
  116. breq.icount = 1;
  117. error = xfs_bulkstat_one(&breq, xfs_fsbulkstat_one_fmt);
  118. } else { /* XFS_IOC_FSBULKSTAT */
  119. breq.startino = lastino ? lastino + 1 : 0;
  120. error = xfs_bulkstat(&breq, xfs_fsbulkstat_one_fmt);
  121. lastino = breq.startino - 1;
  122. }
  123. if (error)
  124. return error;
  125. if (bulkreq.lastip != NULL &&
  126. copy_to_user(bulkreq.lastip, &lastino, sizeof(xfs_ino_t)))
  127. return -EFAULT;
  128. if (bulkreq.ocount != NULL &&
  129. copy_to_user(bulkreq.ocount, &breq.ocount, sizeof(__s32)))
  130. return -EFAULT;
  131. return 0;
  132. }
  133. /* Return 0 on success or positive error */
  134. static int
  135. xfs_bulkstat_fmt(
  136. struct xfs_ibulk *breq,
  137. const struct xfs_bulkstat *bstat)
  138. {
  139. if (copy_to_user(breq->ubuffer, bstat, sizeof(struct xfs_bulkstat)))
  140. return -EFAULT;
  141. return xfs_ibulk_advance(breq, sizeof(struct xfs_bulkstat));
  142. }
  143. /*
  144. * Check the incoming bulk request @hdr from userspace and initialize the
  145. * internal @breq bulk request appropriately. Returns 0 if the bulk request
  146. * should proceed; -ECANCELED if there's nothing to do; or the usual
  147. * negative error code.
  148. */
  149. static int
  150. xfs_bulk_ireq_setup(
  151. struct xfs_mount *mp,
  152. const struct xfs_bulk_ireq *hdr,
  153. struct xfs_ibulk *breq,
  154. void __user *ubuffer)
  155. {
  156. if (hdr->icount == 0 ||
  157. (hdr->flags & ~XFS_BULK_IREQ_FLAGS_ALL) ||
  158. memchr_inv(hdr->reserved, 0, sizeof(hdr->reserved)))
  159. return -EINVAL;
  160. breq->startino = hdr->ino;
  161. breq->ubuffer = ubuffer;
  162. breq->icount = hdr->icount;
  163. breq->ocount = 0;
  164. breq->flags = 0;
  165. /*
  166. * The @ino parameter is a special value, so we must look it up here.
  167. * We're not allowed to have IREQ_AGNO, and we only return one inode
  168. * worth of data.
  169. */
  170. if (hdr->flags & XFS_BULK_IREQ_SPECIAL) {
  171. if (hdr->flags & XFS_BULK_IREQ_AGNO)
  172. return -EINVAL;
  173. switch (hdr->ino) {
  174. case XFS_BULK_IREQ_SPECIAL_ROOT:
  175. breq->startino = mp->m_sb.sb_rootino;
  176. break;
  177. default:
  178. return -EINVAL;
  179. }
  180. breq->icount = 1;
  181. }
  182. /*
  183. * The IREQ_AGNO flag means that we only want results from a given AG.
  184. * If @hdr->ino is zero, we start iterating in that AG. If @hdr->ino is
  185. * beyond the specified AG then we return no results.
  186. */
  187. if (hdr->flags & XFS_BULK_IREQ_AGNO) {
  188. if (hdr->agno >= mp->m_sb.sb_agcount)
  189. return -EINVAL;
  190. if (breq->startino == 0)
  191. breq->startino = XFS_AGINO_TO_INO(mp, hdr->agno, 0);
  192. else if (XFS_INO_TO_AGNO(mp, breq->startino) < hdr->agno)
  193. return -EINVAL;
  194. breq->flags |= XFS_IBULK_SAME_AG;
  195. /* Asking for an inode past the end of the AG? We're done! */
  196. if (XFS_INO_TO_AGNO(mp, breq->startino) > hdr->agno)
  197. return -ECANCELED;
  198. } else if (hdr->agno)
  199. return -EINVAL;
  200. /* Asking for an inode past the end of the FS? We're done! */
  201. if (XFS_INO_TO_AGNO(mp, breq->startino) >= mp->m_sb.sb_agcount)
  202. return -ECANCELED;
  203. if (hdr->flags & XFS_BULK_IREQ_NREXT64)
  204. breq->flags |= XFS_IBULK_NREXT64;
  205. return 0;
  206. }
  207. /*
  208. * Update the userspace bulk request @hdr to reflect the end state of the
  209. * internal bulk request @breq.
  210. */
  211. static void
  212. xfs_bulk_ireq_teardown(
  213. struct xfs_bulk_ireq *hdr,
  214. struct xfs_ibulk *breq)
  215. {
  216. hdr->ino = breq->startino;
  217. hdr->ocount = breq->ocount;
  218. }
  219. /* Handle the v5 bulkstat ioctl. */
  220. STATIC int
  221. xfs_ioc_bulkstat(
  222. struct file *file,
  223. unsigned int cmd,
  224. struct xfs_bulkstat_req __user *arg)
  225. {
  226. struct xfs_mount *mp = XFS_I(file_inode(file))->i_mount;
  227. struct xfs_bulk_ireq hdr;
  228. struct xfs_ibulk breq = {
  229. .mp = mp,
  230. .idmap = file_mnt_idmap(file),
  231. };
  232. int error;
  233. if (!capable(CAP_SYS_ADMIN))
  234. return -EPERM;
  235. if (xfs_is_shutdown(mp))
  236. return -EIO;
  237. if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
  238. return -EFAULT;
  239. error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->bulkstat);
  240. if (error == -ECANCELED)
  241. goto out_teardown;
  242. if (error < 0)
  243. return error;
  244. error = xfs_bulkstat(&breq, xfs_bulkstat_fmt);
  245. if (error)
  246. return error;
  247. out_teardown:
  248. xfs_bulk_ireq_teardown(&hdr, &breq);
  249. if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
  250. return -EFAULT;
  251. return 0;
  252. }
  253. STATIC int
  254. xfs_inumbers_fmt(
  255. struct xfs_ibulk *breq,
  256. const struct xfs_inumbers *igrp)
  257. {
  258. if (copy_to_user(breq->ubuffer, igrp, sizeof(struct xfs_inumbers)))
  259. return -EFAULT;
  260. return xfs_ibulk_advance(breq, sizeof(struct xfs_inumbers));
  261. }
  262. /* Handle the v5 inumbers ioctl. */
  263. STATIC int
  264. xfs_ioc_inumbers(
  265. struct xfs_mount *mp,
  266. unsigned int cmd,
  267. struct xfs_inumbers_req __user *arg)
  268. {
  269. struct xfs_bulk_ireq hdr;
  270. struct xfs_ibulk breq = {
  271. .mp = mp,
  272. };
  273. int error;
  274. if (!capable(CAP_SYS_ADMIN))
  275. return -EPERM;
  276. if (xfs_is_shutdown(mp))
  277. return -EIO;
  278. if (copy_from_user(&hdr, &arg->hdr, sizeof(hdr)))
  279. return -EFAULT;
  280. error = xfs_bulk_ireq_setup(mp, &hdr, &breq, arg->inumbers);
  281. if (error == -ECANCELED)
  282. goto out_teardown;
  283. if (error < 0)
  284. return error;
  285. error = xfs_inumbers(&breq, xfs_inumbers_fmt);
  286. if (error)
  287. return error;
  288. out_teardown:
  289. xfs_bulk_ireq_teardown(&hdr, &breq);
  290. if (copy_to_user(&arg->hdr, &hdr, sizeof(hdr)))
  291. return -EFAULT;
  292. return 0;
  293. }
  294. STATIC int
  295. xfs_ioc_fsgeometry(
  296. struct xfs_mount *mp,
  297. void __user *arg,
  298. int struct_version)
  299. {
  300. struct xfs_fsop_geom fsgeo;
  301. size_t len;
  302. xfs_fs_geometry(mp, &fsgeo, struct_version);
  303. if (struct_version <= 3)
  304. len = sizeof(struct xfs_fsop_geom_v1);
  305. else if (struct_version == 4)
  306. len = sizeof(struct xfs_fsop_geom_v4);
  307. else {
  308. xfs_fsop_geom_health(mp, &fsgeo);
  309. len = sizeof(fsgeo);
  310. }
  311. if (copy_to_user(arg, &fsgeo, len))
  312. return -EFAULT;
  313. return 0;
  314. }
  315. STATIC int
  316. xfs_ioc_ag_geometry(
  317. struct xfs_mount *mp,
  318. void __user *arg)
  319. {
  320. struct xfs_perag *pag;
  321. struct xfs_ag_geometry ageo;
  322. int error;
  323. if (copy_from_user(&ageo, arg, sizeof(ageo)))
  324. return -EFAULT;
  325. if (ageo.ag_flags)
  326. return -EINVAL;
  327. if (memchr_inv(&ageo.ag_reserved, 0, sizeof(ageo.ag_reserved)))
  328. return -EINVAL;
  329. pag = xfs_perag_get(mp, ageo.ag_number);
  330. if (!pag)
  331. return -EINVAL;
  332. error = xfs_ag_get_geometry(pag, &ageo);
  333. xfs_perag_put(pag);
  334. if (error)
  335. return error;
  336. if (copy_to_user(arg, &ageo, sizeof(ageo)))
  337. return -EFAULT;
  338. return 0;
  339. }
  340. /*
  341. * Linux extended inode flags interface.
  342. */
  343. static void
  344. xfs_fill_fsxattr(
  345. struct xfs_inode *ip,
  346. int whichfork,
  347. struct fileattr *fa)
  348. {
  349. struct xfs_mount *mp = ip->i_mount;
  350. struct xfs_ifork *ifp = xfs_ifork_ptr(ip, whichfork);
  351. fileattr_fill_xflags(fa, xfs_ip2xflags(ip));
  352. if (ip->i_diflags & XFS_DIFLAG_EXTSIZE) {
  353. fa->fsx_extsize = XFS_FSB_TO_B(mp, ip->i_extsize);
  354. } else if (ip->i_diflags & XFS_DIFLAG_EXTSZINHERIT) {
  355. /*
  356. * Don't let a misaligned extent size hint on a directory
  357. * escape to userspace if it won't pass the setattr checks
  358. * later.
  359. */
  360. if ((ip->i_diflags & XFS_DIFLAG_RTINHERIT) &&
  361. xfs_extlen_to_rtxmod(mp, ip->i_extsize) > 0) {
  362. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE |
  363. FS_XFLAG_EXTSZINHERIT);
  364. fa->fsx_extsize = 0;
  365. } else {
  366. fa->fsx_extsize = XFS_FSB_TO_B(mp, ip->i_extsize);
  367. }
  368. }
  369. if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
  370. fa->fsx_cowextsize = XFS_FSB_TO_B(mp, ip->i_cowextsize);
  371. fa->fsx_projid = ip->i_projid;
  372. if (ifp && !xfs_need_iread_extents(ifp))
  373. fa->fsx_nextents = xfs_iext_count(ifp);
  374. else
  375. fa->fsx_nextents = xfs_ifork_nextents(ifp);
  376. }
  377. STATIC int
  378. xfs_ioc_fsgetxattra(
  379. xfs_inode_t *ip,
  380. void __user *arg)
  381. {
  382. struct fileattr fa;
  383. xfs_ilock(ip, XFS_ILOCK_SHARED);
  384. xfs_fill_fsxattr(ip, XFS_ATTR_FORK, &fa);
  385. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  386. return copy_fsxattr_to_user(&fa, arg);
  387. }
  388. int
  389. xfs_fileattr_get(
  390. struct dentry *dentry,
  391. struct fileattr *fa)
  392. {
  393. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  394. if (d_is_special(dentry))
  395. return -ENOTTY;
  396. xfs_ilock(ip, XFS_ILOCK_SHARED);
  397. xfs_fill_fsxattr(ip, XFS_DATA_FORK, fa);
  398. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  399. return 0;
  400. }
  401. static int
  402. xfs_ioctl_setattr_xflags(
  403. struct xfs_trans *tp,
  404. struct xfs_inode *ip,
  405. struct fileattr *fa)
  406. {
  407. struct xfs_mount *mp = ip->i_mount;
  408. bool rtflag = (fa->fsx_xflags & FS_XFLAG_REALTIME);
  409. uint64_t i_flags2;
  410. if (rtflag != XFS_IS_REALTIME_INODE(ip)) {
  411. /* Can't change realtime flag if any extents are allocated. */
  412. if (xfs_inode_has_filedata(ip))
  413. return -EINVAL;
  414. /*
  415. * If S_DAX is enabled on this file, we can only switch the
  416. * device if both support fsdax. We can't update S_DAX because
  417. * there might be other threads walking down the access paths.
  418. */
  419. if (IS_DAX(VFS_I(ip)) &&
  420. (mp->m_ddev_targp->bt_daxdev == NULL ||
  421. (mp->m_rtdev_targp &&
  422. mp->m_rtdev_targp->bt_daxdev == NULL)))
  423. return -EINVAL;
  424. }
  425. if (rtflag) {
  426. /* If realtime flag is set then must have realtime device */
  427. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  428. xfs_extlen_to_rtxmod(mp, ip->i_extsize))
  429. return -EINVAL;
  430. /* Clear reflink if we are actually able to set the rt flag. */
  431. if (xfs_is_reflink_inode(ip))
  432. ip->i_diflags2 &= ~XFS_DIFLAG2_REFLINK;
  433. }
  434. /* diflags2 only valid for v3 inodes. */
  435. i_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  436. if (i_flags2 && !xfs_has_v3inodes(mp))
  437. return -EINVAL;
  438. ip->i_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
  439. ip->i_diflags2 = i_flags2;
  440. xfs_diflags_to_iflags(ip, false);
  441. /*
  442. * Make the stable writes flag match that of the device the inode
  443. * resides on when flipping the RT flag.
  444. */
  445. if (rtflag != XFS_IS_REALTIME_INODE(ip) && S_ISREG(VFS_I(ip)->i_mode))
  446. xfs_update_stable_writes(ip);
  447. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  448. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  449. XFS_STATS_INC(mp, xs_ig_attrchg);
  450. return 0;
  451. }
  452. static void
  453. xfs_ioctl_setattr_prepare_dax(
  454. struct xfs_inode *ip,
  455. struct fileattr *fa)
  456. {
  457. struct xfs_mount *mp = ip->i_mount;
  458. struct inode *inode = VFS_I(ip);
  459. if (S_ISDIR(inode->i_mode))
  460. return;
  461. if (xfs_has_dax_always(mp) || xfs_has_dax_never(mp))
  462. return;
  463. if (((fa->fsx_xflags & FS_XFLAG_DAX) &&
  464. !(ip->i_diflags2 & XFS_DIFLAG2_DAX)) ||
  465. (!(fa->fsx_xflags & FS_XFLAG_DAX) &&
  466. (ip->i_diflags2 & XFS_DIFLAG2_DAX)))
  467. d_mark_dontcache(inode);
  468. }
  469. /*
  470. * Set up the transaction structure for the setattr operation, checking that we
  471. * have permission to do so. On success, return a clean transaction and the
  472. * inode locked exclusively ready for further operation specific checks. On
  473. * failure, return an error without modifying or locking the inode.
  474. */
  475. static struct xfs_trans *
  476. xfs_ioctl_setattr_get_trans(
  477. struct xfs_inode *ip,
  478. struct xfs_dquot *pdqp)
  479. {
  480. struct xfs_mount *mp = ip->i_mount;
  481. struct xfs_trans *tp;
  482. int error = -EROFS;
  483. if (xfs_is_readonly(mp))
  484. goto out_error;
  485. error = -EIO;
  486. if (xfs_is_shutdown(mp))
  487. goto out_error;
  488. error = xfs_trans_alloc_ichange(ip, NULL, NULL, pdqp,
  489. has_capability_noaudit(current, CAP_FOWNER), &tp);
  490. if (error)
  491. goto out_error;
  492. if (xfs_has_wsync(mp))
  493. xfs_trans_set_sync(tp);
  494. return tp;
  495. out_error:
  496. return ERR_PTR(error);
  497. }
  498. /*
  499. * Validate a proposed extent size hint. For regular files, the hint can only
  500. * be changed if no extents are allocated.
  501. */
  502. static int
  503. xfs_ioctl_setattr_check_extsize(
  504. struct xfs_inode *ip,
  505. struct fileattr *fa)
  506. {
  507. struct xfs_mount *mp = ip->i_mount;
  508. xfs_failaddr_t failaddr;
  509. uint16_t new_diflags;
  510. if (!fa->fsx_valid)
  511. return 0;
  512. if (S_ISREG(VFS_I(ip)->i_mode) && xfs_inode_has_filedata(ip) &&
  513. XFS_FSB_TO_B(mp, ip->i_extsize) != fa->fsx_extsize)
  514. return -EINVAL;
  515. if (fa->fsx_extsize & mp->m_blockmask)
  516. return -EINVAL;
  517. new_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
  518. /*
  519. * Inode verifiers do not check that the extent size hint is an integer
  520. * multiple of the rt extent size on a directory with both rtinherit
  521. * and extszinherit flags set. Don't let sysadmins misconfigure
  522. * directories.
  523. */
  524. if ((new_diflags & XFS_DIFLAG_RTINHERIT) &&
  525. (new_diflags & XFS_DIFLAG_EXTSZINHERIT)) {
  526. unsigned int rtextsize_bytes;
  527. rtextsize_bytes = XFS_FSB_TO_B(mp, mp->m_sb.sb_rextsize);
  528. if (fa->fsx_extsize % rtextsize_bytes)
  529. return -EINVAL;
  530. }
  531. failaddr = xfs_inode_validate_extsize(ip->i_mount,
  532. XFS_B_TO_FSB(mp, fa->fsx_extsize),
  533. VFS_I(ip)->i_mode, new_diflags);
  534. return failaddr != NULL ? -EINVAL : 0;
  535. }
  536. static int
  537. xfs_ioctl_setattr_check_cowextsize(
  538. struct xfs_inode *ip,
  539. struct fileattr *fa)
  540. {
  541. struct xfs_mount *mp = ip->i_mount;
  542. xfs_failaddr_t failaddr;
  543. uint64_t new_diflags2;
  544. uint16_t new_diflags;
  545. if (!fa->fsx_valid)
  546. return 0;
  547. if (fa->fsx_cowextsize & mp->m_blockmask)
  548. return -EINVAL;
  549. new_diflags = xfs_flags2diflags(ip, fa->fsx_xflags);
  550. new_diflags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  551. failaddr = xfs_inode_validate_cowextsize(ip->i_mount,
  552. XFS_B_TO_FSB(mp, fa->fsx_cowextsize),
  553. VFS_I(ip)->i_mode, new_diflags, new_diflags2);
  554. return failaddr != NULL ? -EINVAL : 0;
  555. }
  556. static int
  557. xfs_ioctl_setattr_check_projid(
  558. struct xfs_inode *ip,
  559. struct fileattr *fa)
  560. {
  561. if (!fa->fsx_valid)
  562. return 0;
  563. /* Disallow 32bit project ids if 32bit IDs are not enabled. */
  564. if (fa->fsx_projid > (uint16_t)-1 &&
  565. !xfs_has_projid32(ip->i_mount))
  566. return -EINVAL;
  567. return 0;
  568. }
  569. int
  570. xfs_fileattr_set(
  571. struct mnt_idmap *idmap,
  572. struct dentry *dentry,
  573. struct fileattr *fa)
  574. {
  575. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  576. struct xfs_mount *mp = ip->i_mount;
  577. struct xfs_trans *tp;
  578. struct xfs_dquot *pdqp = NULL;
  579. struct xfs_dquot *olddquot = NULL;
  580. int error;
  581. trace_xfs_ioctl_setattr(ip);
  582. if (d_is_special(dentry))
  583. return -ENOTTY;
  584. if (!fa->fsx_valid) {
  585. if (fa->flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL |
  586. FS_NOATIME_FL | FS_NODUMP_FL |
  587. FS_SYNC_FL | FS_DAX_FL | FS_PROJINHERIT_FL))
  588. return -EOPNOTSUPP;
  589. }
  590. error = xfs_ioctl_setattr_check_projid(ip, fa);
  591. if (error)
  592. return error;
  593. /*
  594. * If disk quotas is on, we make sure that the dquots do exist on disk,
  595. * before we start any other transactions. Trying to do this later
  596. * is messy. We don't care to take a readlock to look at the ids
  597. * in inode here, because we can't hold it across the trans_reserve.
  598. * If the IDs do change before we take the ilock, we're covered
  599. * because the i_*dquot fields will get updated anyway.
  600. */
  601. if (fa->fsx_valid && XFS_IS_QUOTA_ON(mp)) {
  602. error = xfs_qm_vop_dqalloc(ip, VFS_I(ip)->i_uid,
  603. VFS_I(ip)->i_gid, fa->fsx_projid,
  604. XFS_QMOPT_PQUOTA, NULL, NULL, &pdqp);
  605. if (error)
  606. return error;
  607. }
  608. xfs_ioctl_setattr_prepare_dax(ip, fa);
  609. tp = xfs_ioctl_setattr_get_trans(ip, pdqp);
  610. if (IS_ERR(tp)) {
  611. error = PTR_ERR(tp);
  612. goto error_free_dquots;
  613. }
  614. error = xfs_ioctl_setattr_check_extsize(ip, fa);
  615. if (error)
  616. goto error_trans_cancel;
  617. error = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  618. if (error)
  619. goto error_trans_cancel;
  620. error = xfs_ioctl_setattr_xflags(tp, ip, fa);
  621. if (error)
  622. goto error_trans_cancel;
  623. if (!fa->fsx_valid)
  624. goto skip_xattr;
  625. /*
  626. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  627. * overrides the following restrictions:
  628. *
  629. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  630. * successful return from chown()
  631. */
  632. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  633. !capable_wrt_inode_uidgid(idmap, VFS_I(ip), CAP_FSETID))
  634. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  635. /* Change the ownerships and register project quota modifications */
  636. if (ip->i_projid != fa->fsx_projid) {
  637. if (XFS_IS_PQUOTA_ON(mp)) {
  638. olddquot = xfs_qm_vop_chown(tp, ip,
  639. &ip->i_pdquot, pdqp);
  640. }
  641. ip->i_projid = fa->fsx_projid;
  642. }
  643. /*
  644. * Only set the extent size hint if we've already determined that the
  645. * extent size hint should be set on the inode. If no extent size flags
  646. * are set on the inode then unconditionally clear the extent size hint.
  647. */
  648. if (ip->i_diflags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  649. ip->i_extsize = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  650. else
  651. ip->i_extsize = 0;
  652. if (xfs_has_v3inodes(mp)) {
  653. if (ip->i_diflags2 & XFS_DIFLAG2_COWEXTSIZE)
  654. ip->i_cowextsize = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  655. else
  656. ip->i_cowextsize = 0;
  657. }
  658. skip_xattr:
  659. error = xfs_trans_commit(tp);
  660. /*
  661. * Release any dquot(s) the inode had kept before chown.
  662. */
  663. xfs_qm_dqrele(olddquot);
  664. xfs_qm_dqrele(pdqp);
  665. return error;
  666. error_trans_cancel:
  667. xfs_trans_cancel(tp);
  668. error_free_dquots:
  669. xfs_qm_dqrele(pdqp);
  670. return error;
  671. }
  672. static bool
  673. xfs_getbmap_format(
  674. struct kgetbmap *p,
  675. struct getbmapx __user *u,
  676. size_t recsize)
  677. {
  678. if (put_user(p->bmv_offset, &u->bmv_offset) ||
  679. put_user(p->bmv_block, &u->bmv_block) ||
  680. put_user(p->bmv_length, &u->bmv_length) ||
  681. put_user(0, &u->bmv_count) ||
  682. put_user(0, &u->bmv_entries))
  683. return false;
  684. if (recsize < sizeof(struct getbmapx))
  685. return true;
  686. if (put_user(0, &u->bmv_iflags) ||
  687. put_user(p->bmv_oflags, &u->bmv_oflags) ||
  688. put_user(0, &u->bmv_unused1) ||
  689. put_user(0, &u->bmv_unused2))
  690. return false;
  691. return true;
  692. }
  693. STATIC int
  694. xfs_ioc_getbmap(
  695. struct file *file,
  696. unsigned int cmd,
  697. void __user *arg)
  698. {
  699. struct getbmapx bmx = { 0 };
  700. struct kgetbmap *buf;
  701. size_t recsize;
  702. int error, i;
  703. switch (cmd) {
  704. case XFS_IOC_GETBMAPA:
  705. bmx.bmv_iflags = BMV_IF_ATTRFORK;
  706. fallthrough;
  707. case XFS_IOC_GETBMAP:
  708. /* struct getbmap is a strict subset of struct getbmapx. */
  709. recsize = sizeof(struct getbmap);
  710. break;
  711. case XFS_IOC_GETBMAPX:
  712. recsize = sizeof(struct getbmapx);
  713. break;
  714. default:
  715. return -EINVAL;
  716. }
  717. if (copy_from_user(&bmx, arg, recsize))
  718. return -EFAULT;
  719. if (bmx.bmv_count < 2)
  720. return -EINVAL;
  721. if (bmx.bmv_count >= INT_MAX / recsize)
  722. return -ENOMEM;
  723. buf = kvcalloc(bmx.bmv_count, sizeof(*buf), GFP_KERNEL);
  724. if (!buf)
  725. return -ENOMEM;
  726. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
  727. if (error)
  728. goto out_free_buf;
  729. error = -EFAULT;
  730. if (copy_to_user(arg, &bmx, recsize))
  731. goto out_free_buf;
  732. arg += recsize;
  733. for (i = 0; i < bmx.bmv_entries; i++) {
  734. if (!xfs_getbmap_format(buf + i, arg, recsize))
  735. goto out_free_buf;
  736. arg += recsize;
  737. }
  738. error = 0;
  739. out_free_buf:
  740. kvfree(buf);
  741. return error;
  742. }
  743. int
  744. xfs_ioc_swapext(
  745. xfs_swapext_t *sxp)
  746. {
  747. xfs_inode_t *ip, *tip;
  748. struct fd f, tmp;
  749. int error = 0;
  750. /* Pull information for the target fd */
  751. f = fdget((int)sxp->sx_fdtarget);
  752. if (!fd_file(f)) {
  753. error = -EINVAL;
  754. goto out;
  755. }
  756. if (!(fd_file(f)->f_mode & FMODE_WRITE) ||
  757. !(fd_file(f)->f_mode & FMODE_READ) ||
  758. (fd_file(f)->f_flags & O_APPEND)) {
  759. error = -EBADF;
  760. goto out_put_file;
  761. }
  762. tmp = fdget((int)sxp->sx_fdtmp);
  763. if (!fd_file(tmp)) {
  764. error = -EINVAL;
  765. goto out_put_file;
  766. }
  767. if (!(fd_file(tmp)->f_mode & FMODE_WRITE) ||
  768. !(fd_file(tmp)->f_mode & FMODE_READ) ||
  769. (fd_file(tmp)->f_flags & O_APPEND)) {
  770. error = -EBADF;
  771. goto out_put_tmp_file;
  772. }
  773. if (IS_SWAPFILE(file_inode(fd_file(f))) ||
  774. IS_SWAPFILE(file_inode(fd_file(tmp)))) {
  775. error = -EINVAL;
  776. goto out_put_tmp_file;
  777. }
  778. /*
  779. * We need to ensure that the fds passed in point to XFS inodes
  780. * before we cast and access them as XFS structures as we have no
  781. * control over what the user passes us here.
  782. */
  783. if (fd_file(f)->f_op != &xfs_file_operations ||
  784. fd_file(tmp)->f_op != &xfs_file_operations) {
  785. error = -EINVAL;
  786. goto out_put_tmp_file;
  787. }
  788. ip = XFS_I(file_inode(fd_file(f)));
  789. tip = XFS_I(file_inode(fd_file(tmp)));
  790. if (ip->i_mount != tip->i_mount) {
  791. error = -EINVAL;
  792. goto out_put_tmp_file;
  793. }
  794. if (ip->i_ino == tip->i_ino) {
  795. error = -EINVAL;
  796. goto out_put_tmp_file;
  797. }
  798. if (xfs_is_shutdown(ip->i_mount)) {
  799. error = -EIO;
  800. goto out_put_tmp_file;
  801. }
  802. error = xfs_swap_extents(ip, tip, sxp);
  803. out_put_tmp_file:
  804. fdput(tmp);
  805. out_put_file:
  806. fdput(f);
  807. out:
  808. return error;
  809. }
  810. static int
  811. xfs_ioc_getlabel(
  812. struct xfs_mount *mp,
  813. char __user *user_label)
  814. {
  815. struct xfs_sb *sbp = &mp->m_sb;
  816. char label[XFSLABEL_MAX + 1];
  817. /* Paranoia */
  818. BUILD_BUG_ON(sizeof(sbp->sb_fname) > FSLABEL_MAX);
  819. /* 1 larger than sb_fname, so this ensures a trailing NUL char */
  820. memset(label, 0, sizeof(label));
  821. spin_lock(&mp->m_sb_lock);
  822. strncpy(label, sbp->sb_fname, XFSLABEL_MAX);
  823. spin_unlock(&mp->m_sb_lock);
  824. if (copy_to_user(user_label, label, sizeof(label)))
  825. return -EFAULT;
  826. return 0;
  827. }
  828. static int
  829. xfs_ioc_setlabel(
  830. struct file *filp,
  831. struct xfs_mount *mp,
  832. char __user *newlabel)
  833. {
  834. struct xfs_sb *sbp = &mp->m_sb;
  835. char label[XFSLABEL_MAX + 1];
  836. size_t len;
  837. int error;
  838. if (!capable(CAP_SYS_ADMIN))
  839. return -EPERM;
  840. /*
  841. * The generic ioctl allows up to FSLABEL_MAX chars, but XFS is much
  842. * smaller, at 12 bytes. We copy one more to be sure we find the
  843. * (required) NULL character to test the incoming label length.
  844. * NB: The on disk label doesn't need to be null terminated.
  845. */
  846. if (copy_from_user(label, newlabel, XFSLABEL_MAX + 1))
  847. return -EFAULT;
  848. len = strnlen(label, XFSLABEL_MAX + 1);
  849. if (len > sizeof(sbp->sb_fname))
  850. return -EINVAL;
  851. error = mnt_want_write_file(filp);
  852. if (error)
  853. return error;
  854. spin_lock(&mp->m_sb_lock);
  855. memset(sbp->sb_fname, 0, sizeof(sbp->sb_fname));
  856. memcpy(sbp->sb_fname, label, len);
  857. spin_unlock(&mp->m_sb_lock);
  858. /*
  859. * Now we do several things to satisfy userspace.
  860. * In addition to normal logging of the primary superblock, we also
  861. * immediately write these changes to sector zero for the primary, then
  862. * update all backup supers (as xfs_db does for a label change), then
  863. * invalidate the block device page cache. This is so that any prior
  864. * buffered reads from userspace (i.e. from blkid) are invalidated,
  865. * and userspace will see the newly-written label.
  866. */
  867. error = xfs_sync_sb_buf(mp);
  868. if (error)
  869. goto out;
  870. /*
  871. * growfs also updates backup supers so lock against that.
  872. */
  873. mutex_lock(&mp->m_growlock);
  874. error = xfs_update_secondary_sbs(mp);
  875. mutex_unlock(&mp->m_growlock);
  876. invalidate_bdev(mp->m_ddev_targp->bt_bdev);
  877. out:
  878. mnt_drop_write_file(filp);
  879. return error;
  880. }
  881. static inline int
  882. xfs_fs_eofblocks_from_user(
  883. struct xfs_fs_eofblocks *src,
  884. struct xfs_icwalk *dst)
  885. {
  886. if (src->eof_version != XFS_EOFBLOCKS_VERSION)
  887. return -EINVAL;
  888. if (src->eof_flags & ~XFS_EOF_FLAGS_VALID)
  889. return -EINVAL;
  890. if (memchr_inv(&src->pad32, 0, sizeof(src->pad32)) ||
  891. memchr_inv(src->pad64, 0, sizeof(src->pad64)))
  892. return -EINVAL;
  893. dst->icw_flags = 0;
  894. if (src->eof_flags & XFS_EOF_FLAGS_SYNC)
  895. dst->icw_flags |= XFS_ICWALK_FLAG_SYNC;
  896. if (src->eof_flags & XFS_EOF_FLAGS_UID)
  897. dst->icw_flags |= XFS_ICWALK_FLAG_UID;
  898. if (src->eof_flags & XFS_EOF_FLAGS_GID)
  899. dst->icw_flags |= XFS_ICWALK_FLAG_GID;
  900. if (src->eof_flags & XFS_EOF_FLAGS_PRID)
  901. dst->icw_flags |= XFS_ICWALK_FLAG_PRID;
  902. if (src->eof_flags & XFS_EOF_FLAGS_MINFILESIZE)
  903. dst->icw_flags |= XFS_ICWALK_FLAG_MINFILESIZE;
  904. dst->icw_prid = src->eof_prid;
  905. dst->icw_min_file_size = src->eof_min_file_size;
  906. dst->icw_uid = INVALID_UID;
  907. if (src->eof_flags & XFS_EOF_FLAGS_UID) {
  908. dst->icw_uid = make_kuid(current_user_ns(), src->eof_uid);
  909. if (!uid_valid(dst->icw_uid))
  910. return -EINVAL;
  911. }
  912. dst->icw_gid = INVALID_GID;
  913. if (src->eof_flags & XFS_EOF_FLAGS_GID) {
  914. dst->icw_gid = make_kgid(current_user_ns(), src->eof_gid);
  915. if (!gid_valid(dst->icw_gid))
  916. return -EINVAL;
  917. }
  918. return 0;
  919. }
  920. static int
  921. xfs_ioctl_getset_resblocks(
  922. struct file *filp,
  923. unsigned int cmd,
  924. void __user *arg)
  925. {
  926. struct xfs_mount *mp = XFS_I(file_inode(filp))->i_mount;
  927. struct xfs_fsop_resblks fsop = { };
  928. int error;
  929. if (!capable(CAP_SYS_ADMIN))
  930. return -EPERM;
  931. if (cmd == XFS_IOC_SET_RESBLKS) {
  932. if (xfs_is_readonly(mp))
  933. return -EROFS;
  934. if (copy_from_user(&fsop, arg, sizeof(fsop)))
  935. return -EFAULT;
  936. error = mnt_want_write_file(filp);
  937. if (error)
  938. return error;
  939. error = xfs_reserve_blocks(mp, fsop.resblks);
  940. mnt_drop_write_file(filp);
  941. if (error)
  942. return error;
  943. }
  944. spin_lock(&mp->m_sb_lock);
  945. fsop.resblks = mp->m_resblks;
  946. fsop.resblks_avail = mp->m_resblks_avail;
  947. spin_unlock(&mp->m_sb_lock);
  948. if (copy_to_user(arg, &fsop, sizeof(fsop)))
  949. return -EFAULT;
  950. return 0;
  951. }
  952. static int
  953. xfs_ioctl_fs_counts(
  954. struct xfs_mount *mp,
  955. struct xfs_fsop_counts __user *uarg)
  956. {
  957. struct xfs_fsop_counts out = {
  958. .allocino = percpu_counter_read_positive(&mp->m_icount),
  959. .freeino = percpu_counter_read_positive(&mp->m_ifree),
  960. .freedata = percpu_counter_read_positive(&mp->m_fdblocks) -
  961. xfs_fdblocks_unavailable(mp),
  962. .freertx = percpu_counter_read_positive(&mp->m_frextents),
  963. };
  964. if (copy_to_user(uarg, &out, sizeof(out)))
  965. return -EFAULT;
  966. return 0;
  967. }
  968. /*
  969. * These long-unused ioctls were removed from the official ioctl API in 5.17,
  970. * but retain these definitions so that we can log warnings about them.
  971. */
  972. #define XFS_IOC_ALLOCSP _IOW ('X', 10, struct xfs_flock64)
  973. #define XFS_IOC_FREESP _IOW ('X', 11, struct xfs_flock64)
  974. #define XFS_IOC_ALLOCSP64 _IOW ('X', 36, struct xfs_flock64)
  975. #define XFS_IOC_FREESP64 _IOW ('X', 37, struct xfs_flock64)
  976. /*
  977. * Note: some of the ioctl's return positive numbers as a
  978. * byte count indicating success, such as readlink_by_handle.
  979. * So we don't "sign flip" like most other routines. This means
  980. * true errors need to be returned as a negative value.
  981. */
  982. long
  983. xfs_file_ioctl(
  984. struct file *filp,
  985. unsigned int cmd,
  986. unsigned long p)
  987. {
  988. struct inode *inode = file_inode(filp);
  989. struct xfs_inode *ip = XFS_I(inode);
  990. struct xfs_mount *mp = ip->i_mount;
  991. void __user *arg = (void __user *)p;
  992. int error;
  993. trace_xfs_file_ioctl(ip);
  994. switch (cmd) {
  995. case FITRIM:
  996. return xfs_ioc_trim(mp, arg);
  997. case FS_IOC_GETFSLABEL:
  998. return xfs_ioc_getlabel(mp, arg);
  999. case FS_IOC_SETFSLABEL:
  1000. return xfs_ioc_setlabel(filp, mp, arg);
  1001. case XFS_IOC_ALLOCSP:
  1002. case XFS_IOC_FREESP:
  1003. case XFS_IOC_ALLOCSP64:
  1004. case XFS_IOC_FREESP64:
  1005. xfs_warn_once(mp,
  1006. "%s should use fallocate; XFS_IOC_{ALLOC,FREE}SP ioctl unsupported",
  1007. current->comm);
  1008. return -ENOTTY;
  1009. case XFS_IOC_DIOINFO: {
  1010. struct xfs_buftarg *target = xfs_inode_buftarg(ip);
  1011. struct dioattr da;
  1012. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1013. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1014. if (copy_to_user(arg, &da, sizeof(da)))
  1015. return -EFAULT;
  1016. return 0;
  1017. }
  1018. case XFS_IOC_FSBULKSTAT_SINGLE:
  1019. case XFS_IOC_FSBULKSTAT:
  1020. case XFS_IOC_FSINUMBERS:
  1021. return xfs_ioc_fsbulkstat(filp, cmd, arg);
  1022. case XFS_IOC_BULKSTAT:
  1023. return xfs_ioc_bulkstat(filp, cmd, arg);
  1024. case XFS_IOC_INUMBERS:
  1025. return xfs_ioc_inumbers(mp, cmd, arg);
  1026. case XFS_IOC_FSGEOMETRY_V1:
  1027. return xfs_ioc_fsgeometry(mp, arg, 3);
  1028. case XFS_IOC_FSGEOMETRY_V4:
  1029. return xfs_ioc_fsgeometry(mp, arg, 4);
  1030. case XFS_IOC_FSGEOMETRY:
  1031. return xfs_ioc_fsgeometry(mp, arg, 5);
  1032. case XFS_IOC_AG_GEOMETRY:
  1033. return xfs_ioc_ag_geometry(mp, arg);
  1034. case XFS_IOC_GETVERSION:
  1035. return put_user(inode->i_generation, (int __user *)arg);
  1036. case XFS_IOC_FSGETXATTRA:
  1037. return xfs_ioc_fsgetxattra(ip, arg);
  1038. case XFS_IOC_GETPARENTS:
  1039. return xfs_ioc_getparents(filp, arg);
  1040. case XFS_IOC_GETPARENTS_BY_HANDLE:
  1041. return xfs_ioc_getparents_by_handle(filp, arg);
  1042. case XFS_IOC_GETBMAP:
  1043. case XFS_IOC_GETBMAPA:
  1044. case XFS_IOC_GETBMAPX:
  1045. return xfs_ioc_getbmap(filp, cmd, arg);
  1046. case FS_IOC_GETFSMAP:
  1047. return xfs_ioc_getfsmap(ip, arg);
  1048. case XFS_IOC_SCRUBV_METADATA:
  1049. return xfs_ioc_scrubv_metadata(filp, arg);
  1050. case XFS_IOC_SCRUB_METADATA:
  1051. return xfs_ioc_scrub_metadata(filp, arg);
  1052. case XFS_IOC_FD_TO_HANDLE:
  1053. case XFS_IOC_PATH_TO_HANDLE:
  1054. case XFS_IOC_PATH_TO_FSHANDLE: {
  1055. xfs_fsop_handlereq_t hreq;
  1056. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1057. return -EFAULT;
  1058. return xfs_find_handle(cmd, &hreq);
  1059. }
  1060. case XFS_IOC_OPEN_BY_HANDLE: {
  1061. xfs_fsop_handlereq_t hreq;
  1062. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1063. return -EFAULT;
  1064. return xfs_open_by_handle(filp, &hreq);
  1065. }
  1066. case XFS_IOC_READLINK_BY_HANDLE: {
  1067. xfs_fsop_handlereq_t hreq;
  1068. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1069. return -EFAULT;
  1070. return xfs_readlink_by_handle(filp, &hreq);
  1071. }
  1072. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1073. return xfs_attrlist_by_handle(filp, arg);
  1074. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1075. return xfs_attrmulti_by_handle(filp, arg);
  1076. case XFS_IOC_SWAPEXT: {
  1077. struct xfs_swapext sxp;
  1078. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1079. return -EFAULT;
  1080. error = mnt_want_write_file(filp);
  1081. if (error)
  1082. return error;
  1083. error = xfs_ioc_swapext(&sxp);
  1084. mnt_drop_write_file(filp);
  1085. return error;
  1086. }
  1087. case XFS_IOC_FSCOUNTS:
  1088. return xfs_ioctl_fs_counts(mp, arg);
  1089. case XFS_IOC_SET_RESBLKS:
  1090. case XFS_IOC_GET_RESBLKS:
  1091. return xfs_ioctl_getset_resblocks(filp, cmd, arg);
  1092. case XFS_IOC_FSGROWFSDATA: {
  1093. struct xfs_growfs_data in;
  1094. if (copy_from_user(&in, arg, sizeof(in)))
  1095. return -EFAULT;
  1096. error = mnt_want_write_file(filp);
  1097. if (error)
  1098. return error;
  1099. error = xfs_growfs_data(mp, &in);
  1100. mnt_drop_write_file(filp);
  1101. return error;
  1102. }
  1103. case XFS_IOC_FSGROWFSLOG: {
  1104. struct xfs_growfs_log in;
  1105. if (copy_from_user(&in, arg, sizeof(in)))
  1106. return -EFAULT;
  1107. error = mnt_want_write_file(filp);
  1108. if (error)
  1109. return error;
  1110. error = xfs_growfs_log(mp, &in);
  1111. mnt_drop_write_file(filp);
  1112. return error;
  1113. }
  1114. case XFS_IOC_FSGROWFSRT: {
  1115. xfs_growfs_rt_t in;
  1116. if (copy_from_user(&in, arg, sizeof(in)))
  1117. return -EFAULT;
  1118. error = mnt_want_write_file(filp);
  1119. if (error)
  1120. return error;
  1121. error = xfs_growfs_rt(mp, &in);
  1122. mnt_drop_write_file(filp);
  1123. return error;
  1124. }
  1125. case XFS_IOC_GOINGDOWN: {
  1126. uint32_t in;
  1127. if (!capable(CAP_SYS_ADMIN))
  1128. return -EPERM;
  1129. if (get_user(in, (uint32_t __user *)arg))
  1130. return -EFAULT;
  1131. return xfs_fs_goingdown(mp, in);
  1132. }
  1133. case XFS_IOC_ERROR_INJECTION: {
  1134. xfs_error_injection_t in;
  1135. if (!capable(CAP_SYS_ADMIN))
  1136. return -EPERM;
  1137. if (copy_from_user(&in, arg, sizeof(in)))
  1138. return -EFAULT;
  1139. return xfs_errortag_add(mp, in.errtag);
  1140. }
  1141. case XFS_IOC_ERROR_CLEARALL:
  1142. if (!capable(CAP_SYS_ADMIN))
  1143. return -EPERM;
  1144. return xfs_errortag_clearall(mp);
  1145. case XFS_IOC_FREE_EOFBLOCKS: {
  1146. struct xfs_fs_eofblocks eofb;
  1147. struct xfs_icwalk icw;
  1148. if (!capable(CAP_SYS_ADMIN))
  1149. return -EPERM;
  1150. if (xfs_is_readonly(mp))
  1151. return -EROFS;
  1152. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1153. return -EFAULT;
  1154. error = xfs_fs_eofblocks_from_user(&eofb, &icw);
  1155. if (error)
  1156. return error;
  1157. trace_xfs_ioc_free_eofblocks(mp, &icw, _RET_IP_);
  1158. sb_start_write(mp->m_super);
  1159. error = xfs_blockgc_free_space(mp, &icw);
  1160. sb_end_write(mp->m_super);
  1161. return error;
  1162. }
  1163. case XFS_IOC_EXCHANGE_RANGE:
  1164. return xfs_ioc_exchange_range(filp, arg);
  1165. case XFS_IOC_START_COMMIT:
  1166. return xfs_ioc_start_commit(filp, arg);
  1167. case XFS_IOC_COMMIT_RANGE:
  1168. return xfs_ioc_commit_range(filp, arg);
  1169. default:
  1170. return -ENOTTY;
  1171. }
  1172. }