xfs_ioctl.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110
  1. /*
  2. * Copyright (c) 2000-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 "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_inode.h"
  26. #include "xfs_ioctl.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_rtalloc.h"
  29. #include "xfs_itable.h"
  30. #include "xfs_error.h"
  31. #include "xfs_attr.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_fsops.h"
  35. #include "xfs_discard.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_export.h"
  38. #include "xfs_trace.h"
  39. #include "xfs_icache.h"
  40. #include "xfs_symlink.h"
  41. #include "xfs_trans.h"
  42. #include "xfs_pnfs.h"
  43. #include "xfs_acl.h"
  44. #include "xfs_btree.h"
  45. #include <linux/fsmap.h>
  46. #include "xfs_fsmap.h"
  47. #include "scrub/xfs_scrub.h"
  48. #include <linux/capability.h>
  49. #include <linux/cred.h>
  50. #include <linux/dcache.h>
  51. #include <linux/mount.h>
  52. #include <linux/namei.h>
  53. #include <linux/pagemap.h>
  54. #include <linux/slab.h>
  55. #include <linux/exportfs.h>
  56. /*
  57. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  58. * a file or fs handle.
  59. *
  60. * XFS_IOC_PATH_TO_FSHANDLE
  61. * returns fs handle for a mount point or path within that mount point
  62. * XFS_IOC_FD_TO_HANDLE
  63. * returns full handle for a FD opened in user space
  64. * XFS_IOC_PATH_TO_HANDLE
  65. * returns full handle for a path
  66. */
  67. int
  68. xfs_find_handle(
  69. unsigned int cmd,
  70. xfs_fsop_handlereq_t *hreq)
  71. {
  72. int hsize;
  73. xfs_handle_t handle;
  74. struct inode *inode;
  75. struct fd f = {NULL};
  76. struct path path;
  77. int error;
  78. struct xfs_inode *ip;
  79. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  80. f = fdget(hreq->fd);
  81. if (!f.file)
  82. return -EBADF;
  83. inode = file_inode(f.file);
  84. } else {
  85. error = user_lpath((const char __user *)hreq->path, &path);
  86. if (error)
  87. return error;
  88. inode = d_inode(path.dentry);
  89. }
  90. ip = XFS_I(inode);
  91. /*
  92. * We can only generate handles for inodes residing on a XFS filesystem,
  93. * and only for regular files, directories or symbolic links.
  94. */
  95. error = -EINVAL;
  96. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  97. goto out_put;
  98. error = -EBADF;
  99. if (!S_ISREG(inode->i_mode) &&
  100. !S_ISDIR(inode->i_mode) &&
  101. !S_ISLNK(inode->i_mode))
  102. goto out_put;
  103. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  104. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  105. /*
  106. * This handle only contains an fsid, zero the rest.
  107. */
  108. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  109. hsize = sizeof(xfs_fsid_t);
  110. } else {
  111. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  112. sizeof(handle.ha_fid.fid_len);
  113. handle.ha_fid.fid_pad = 0;
  114. handle.ha_fid.fid_gen = inode->i_generation;
  115. handle.ha_fid.fid_ino = ip->i_ino;
  116. hsize = sizeof(xfs_handle_t);
  117. }
  118. error = -EFAULT;
  119. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  120. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  121. goto out_put;
  122. error = 0;
  123. out_put:
  124. if (cmd == XFS_IOC_FD_TO_HANDLE)
  125. fdput(f);
  126. else
  127. path_put(&path);
  128. return error;
  129. }
  130. /*
  131. * No need to do permission checks on the various pathname components
  132. * as the handle operations are privileged.
  133. */
  134. STATIC int
  135. xfs_handle_acceptable(
  136. void *context,
  137. struct dentry *dentry)
  138. {
  139. return 1;
  140. }
  141. /*
  142. * Convert userspace handle data into a dentry.
  143. */
  144. struct dentry *
  145. xfs_handle_to_dentry(
  146. struct file *parfilp,
  147. void __user *uhandle,
  148. u32 hlen)
  149. {
  150. xfs_handle_t handle;
  151. struct xfs_fid64 fid;
  152. /*
  153. * Only allow handle opens under a directory.
  154. */
  155. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  156. return ERR_PTR(-ENOTDIR);
  157. if (hlen != sizeof(xfs_handle_t))
  158. return ERR_PTR(-EINVAL);
  159. if (copy_from_user(&handle, uhandle, hlen))
  160. return ERR_PTR(-EFAULT);
  161. if (handle.ha_fid.fid_len !=
  162. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  163. return ERR_PTR(-EINVAL);
  164. memset(&fid, 0, sizeof(struct fid));
  165. fid.ino = handle.ha_fid.fid_ino;
  166. fid.gen = handle.ha_fid.fid_gen;
  167. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  168. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  169. xfs_handle_acceptable, NULL);
  170. }
  171. STATIC struct dentry *
  172. xfs_handlereq_to_dentry(
  173. struct file *parfilp,
  174. xfs_fsop_handlereq_t *hreq)
  175. {
  176. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  177. }
  178. int
  179. xfs_open_by_handle(
  180. struct file *parfilp,
  181. xfs_fsop_handlereq_t *hreq)
  182. {
  183. const struct cred *cred = current_cred();
  184. int error;
  185. int fd;
  186. int permflag;
  187. struct file *filp;
  188. struct inode *inode;
  189. struct dentry *dentry;
  190. fmode_t fmode;
  191. struct path path;
  192. if (!capable(CAP_SYS_ADMIN))
  193. return -EPERM;
  194. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  195. if (IS_ERR(dentry))
  196. return PTR_ERR(dentry);
  197. inode = d_inode(dentry);
  198. /* Restrict xfs_open_by_handle to directories & regular files. */
  199. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  200. error = -EPERM;
  201. goto out_dput;
  202. }
  203. #if BITS_PER_LONG != 32
  204. hreq->oflags |= O_LARGEFILE;
  205. #endif
  206. permflag = hreq->oflags;
  207. fmode = OPEN_FMODE(permflag);
  208. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  209. (fmode & FMODE_WRITE) && IS_APPEND(inode)) {
  210. error = -EPERM;
  211. goto out_dput;
  212. }
  213. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  214. error = -EPERM;
  215. goto out_dput;
  216. }
  217. /* Can't write directories. */
  218. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  219. error = -EISDIR;
  220. goto out_dput;
  221. }
  222. fd = get_unused_fd_flags(0);
  223. if (fd < 0) {
  224. error = fd;
  225. goto out_dput;
  226. }
  227. path.mnt = parfilp->f_path.mnt;
  228. path.dentry = dentry;
  229. filp = dentry_open(&path, hreq->oflags, cred);
  230. dput(dentry);
  231. if (IS_ERR(filp)) {
  232. put_unused_fd(fd);
  233. return PTR_ERR(filp);
  234. }
  235. if (S_ISREG(inode->i_mode)) {
  236. filp->f_flags |= O_NOATIME;
  237. filp->f_mode |= FMODE_NOCMTIME;
  238. }
  239. fd_install(fd, filp);
  240. return fd;
  241. out_dput:
  242. dput(dentry);
  243. return error;
  244. }
  245. int
  246. xfs_readlink_by_handle(
  247. struct file *parfilp,
  248. xfs_fsop_handlereq_t *hreq)
  249. {
  250. struct dentry *dentry;
  251. __u32 olen;
  252. int error;
  253. if (!capable(CAP_SYS_ADMIN))
  254. return -EPERM;
  255. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  256. if (IS_ERR(dentry))
  257. return PTR_ERR(dentry);
  258. /* Restrict this handle operation to symlinks only. */
  259. if (!d_is_symlink(dentry)) {
  260. error = -EINVAL;
  261. goto out_dput;
  262. }
  263. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  264. error = -EFAULT;
  265. goto out_dput;
  266. }
  267. error = vfs_readlink(dentry, hreq->ohandle, olen);
  268. out_dput:
  269. dput(dentry);
  270. return error;
  271. }
  272. int
  273. xfs_set_dmattrs(
  274. xfs_inode_t *ip,
  275. u_int evmask,
  276. u_int16_t state)
  277. {
  278. xfs_mount_t *mp = ip->i_mount;
  279. xfs_trans_t *tp;
  280. int error;
  281. if (!capable(CAP_SYS_ADMIN))
  282. return -EPERM;
  283. if (XFS_FORCED_SHUTDOWN(mp))
  284. return -EIO;
  285. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  286. if (error)
  287. return error;
  288. xfs_ilock(ip, XFS_ILOCK_EXCL);
  289. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  290. ip->i_d.di_dmevmask = evmask;
  291. ip->i_d.di_dmstate = state;
  292. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  293. error = xfs_trans_commit(tp);
  294. return error;
  295. }
  296. STATIC int
  297. xfs_fssetdm_by_handle(
  298. struct file *parfilp,
  299. void __user *arg)
  300. {
  301. int error;
  302. struct fsdmidata fsd;
  303. xfs_fsop_setdm_handlereq_t dmhreq;
  304. struct dentry *dentry;
  305. if (!capable(CAP_MKNOD))
  306. return -EPERM;
  307. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  308. return -EFAULT;
  309. error = mnt_want_write_file(parfilp);
  310. if (error)
  311. return error;
  312. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  313. if (IS_ERR(dentry)) {
  314. mnt_drop_write_file(parfilp);
  315. return PTR_ERR(dentry);
  316. }
  317. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  318. error = -EPERM;
  319. goto out;
  320. }
  321. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  322. error = -EFAULT;
  323. goto out;
  324. }
  325. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  326. fsd.fsd_dmstate);
  327. out:
  328. mnt_drop_write_file(parfilp);
  329. dput(dentry);
  330. return error;
  331. }
  332. STATIC int
  333. xfs_attrlist_by_handle(
  334. struct file *parfilp,
  335. void __user *arg)
  336. {
  337. int error = -ENOMEM;
  338. attrlist_cursor_kern_t *cursor;
  339. struct xfs_fsop_attrlist_handlereq __user *p = arg;
  340. xfs_fsop_attrlist_handlereq_t al_hreq;
  341. struct dentry *dentry;
  342. char *kbuf;
  343. if (!capable(CAP_SYS_ADMIN))
  344. return -EPERM;
  345. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  346. return -EFAULT;
  347. if (al_hreq.buflen < sizeof(struct attrlist) ||
  348. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  349. return -EINVAL;
  350. /*
  351. * Reject flags, only allow namespaces.
  352. */
  353. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  354. return -EINVAL;
  355. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  356. if (IS_ERR(dentry))
  357. return PTR_ERR(dentry);
  358. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  359. if (!kbuf)
  360. goto out_dput;
  361. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  362. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  363. al_hreq.flags, cursor);
  364. if (error)
  365. goto out_kfree;
  366. if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
  367. error = -EFAULT;
  368. goto out_kfree;
  369. }
  370. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  371. error = -EFAULT;
  372. out_kfree:
  373. kmem_free(kbuf);
  374. out_dput:
  375. dput(dentry);
  376. return error;
  377. }
  378. int
  379. xfs_attrmulti_attr_get(
  380. struct inode *inode,
  381. unsigned char *name,
  382. unsigned char __user *ubuf,
  383. uint32_t *len,
  384. uint32_t flags)
  385. {
  386. unsigned char *kbuf;
  387. int error = -EFAULT;
  388. if (*len > XFS_XATTR_SIZE_MAX)
  389. return -EINVAL;
  390. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  391. if (!kbuf)
  392. return -ENOMEM;
  393. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  394. if (error)
  395. goto out_kfree;
  396. if (copy_to_user(ubuf, kbuf, *len))
  397. error = -EFAULT;
  398. out_kfree:
  399. kmem_free(kbuf);
  400. return error;
  401. }
  402. int
  403. xfs_attrmulti_attr_set(
  404. struct inode *inode,
  405. unsigned char *name,
  406. const unsigned char __user *ubuf,
  407. uint32_t len,
  408. uint32_t flags)
  409. {
  410. unsigned char *kbuf;
  411. int error;
  412. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  413. return -EPERM;
  414. if (len > XFS_XATTR_SIZE_MAX)
  415. return -EINVAL;
  416. kbuf = memdup_user(ubuf, len);
  417. if (IS_ERR(kbuf))
  418. return PTR_ERR(kbuf);
  419. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  420. if (!error)
  421. xfs_forget_acl(inode, name, flags);
  422. kfree(kbuf);
  423. return error;
  424. }
  425. int
  426. xfs_attrmulti_attr_remove(
  427. struct inode *inode,
  428. unsigned char *name,
  429. uint32_t flags)
  430. {
  431. int error;
  432. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  433. return -EPERM;
  434. error = xfs_attr_remove(XFS_I(inode), name, flags);
  435. if (!error)
  436. xfs_forget_acl(inode, name, flags);
  437. return error;
  438. }
  439. STATIC int
  440. xfs_attrmulti_by_handle(
  441. struct file *parfilp,
  442. void __user *arg)
  443. {
  444. int error;
  445. xfs_attr_multiop_t *ops;
  446. xfs_fsop_attrmulti_handlereq_t am_hreq;
  447. struct dentry *dentry;
  448. unsigned int i, size;
  449. unsigned char *attr_name;
  450. if (!capable(CAP_SYS_ADMIN))
  451. return -EPERM;
  452. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  453. return -EFAULT;
  454. /* overflow check */
  455. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  456. return -E2BIG;
  457. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  458. if (IS_ERR(dentry))
  459. return PTR_ERR(dentry);
  460. error = -E2BIG;
  461. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  462. if (!size || size > 16 * PAGE_SIZE)
  463. goto out_dput;
  464. ops = memdup_user(am_hreq.ops, size);
  465. if (IS_ERR(ops)) {
  466. error = PTR_ERR(ops);
  467. goto out_dput;
  468. }
  469. error = -ENOMEM;
  470. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  471. if (!attr_name)
  472. goto out_kfree_ops;
  473. error = 0;
  474. for (i = 0; i < am_hreq.opcount; i++) {
  475. ops[i].am_error = strncpy_from_user((char *)attr_name,
  476. ops[i].am_attrname, MAXNAMELEN);
  477. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  478. error = -ERANGE;
  479. if (ops[i].am_error < 0)
  480. break;
  481. switch (ops[i].am_opcode) {
  482. case ATTR_OP_GET:
  483. ops[i].am_error = xfs_attrmulti_attr_get(
  484. d_inode(dentry), attr_name,
  485. ops[i].am_attrvalue, &ops[i].am_length,
  486. ops[i].am_flags);
  487. break;
  488. case ATTR_OP_SET:
  489. ops[i].am_error = mnt_want_write_file(parfilp);
  490. if (ops[i].am_error)
  491. break;
  492. ops[i].am_error = xfs_attrmulti_attr_set(
  493. d_inode(dentry), attr_name,
  494. ops[i].am_attrvalue, ops[i].am_length,
  495. ops[i].am_flags);
  496. mnt_drop_write_file(parfilp);
  497. break;
  498. case ATTR_OP_REMOVE:
  499. ops[i].am_error = mnt_want_write_file(parfilp);
  500. if (ops[i].am_error)
  501. break;
  502. ops[i].am_error = xfs_attrmulti_attr_remove(
  503. d_inode(dentry), attr_name,
  504. ops[i].am_flags);
  505. mnt_drop_write_file(parfilp);
  506. break;
  507. default:
  508. ops[i].am_error = -EINVAL;
  509. }
  510. }
  511. if (copy_to_user(am_hreq.ops, ops, size))
  512. error = -EFAULT;
  513. kfree(attr_name);
  514. out_kfree_ops:
  515. kfree(ops);
  516. out_dput:
  517. dput(dentry);
  518. return error;
  519. }
  520. int
  521. xfs_ioc_space(
  522. struct file *filp,
  523. unsigned int cmd,
  524. xfs_flock64_t *bf)
  525. {
  526. struct inode *inode = file_inode(filp);
  527. struct xfs_inode *ip = XFS_I(inode);
  528. struct iattr iattr;
  529. enum xfs_prealloc_flags flags = 0;
  530. uint iolock = XFS_IOLOCK_EXCL;
  531. int error;
  532. /*
  533. * Only allow the sys admin to reserve space unless
  534. * unwritten extents are enabled.
  535. */
  536. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  537. !capable(CAP_SYS_ADMIN))
  538. return -EPERM;
  539. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  540. return -EPERM;
  541. if (!(filp->f_mode & FMODE_WRITE))
  542. return -EBADF;
  543. if (!S_ISREG(inode->i_mode))
  544. return -EINVAL;
  545. if (filp->f_flags & O_DSYNC)
  546. flags |= XFS_PREALLOC_SYNC;
  547. if (filp->f_mode & FMODE_NOCMTIME)
  548. flags |= XFS_PREALLOC_INVISIBLE;
  549. error = mnt_want_write_file(filp);
  550. if (error)
  551. return error;
  552. xfs_ilock(ip, iolock);
  553. error = xfs_break_layouts(inode, &iolock);
  554. if (error)
  555. goto out_unlock;
  556. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  557. iolock |= XFS_MMAPLOCK_EXCL;
  558. switch (bf->l_whence) {
  559. case 0: /*SEEK_SET*/
  560. break;
  561. case 1: /*SEEK_CUR*/
  562. bf->l_start += filp->f_pos;
  563. break;
  564. case 2: /*SEEK_END*/
  565. bf->l_start += XFS_ISIZE(ip);
  566. break;
  567. default:
  568. error = -EINVAL;
  569. goto out_unlock;
  570. }
  571. /*
  572. * length of <= 0 for resv/unresv/zero is invalid. length for
  573. * alloc/free is ignored completely and we have no idea what userspace
  574. * might have set it to, so set it to zero to allow range
  575. * checks to pass.
  576. */
  577. switch (cmd) {
  578. case XFS_IOC_ZERO_RANGE:
  579. case XFS_IOC_RESVSP:
  580. case XFS_IOC_RESVSP64:
  581. case XFS_IOC_UNRESVSP:
  582. case XFS_IOC_UNRESVSP64:
  583. if (bf->l_len <= 0) {
  584. error = -EINVAL;
  585. goto out_unlock;
  586. }
  587. break;
  588. default:
  589. bf->l_len = 0;
  590. break;
  591. }
  592. if (bf->l_start < 0 ||
  593. bf->l_start > inode->i_sb->s_maxbytes ||
  594. bf->l_start + bf->l_len < 0 ||
  595. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  596. error = -EINVAL;
  597. goto out_unlock;
  598. }
  599. switch (cmd) {
  600. case XFS_IOC_ZERO_RANGE:
  601. flags |= XFS_PREALLOC_SET;
  602. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  603. break;
  604. case XFS_IOC_RESVSP:
  605. case XFS_IOC_RESVSP64:
  606. flags |= XFS_PREALLOC_SET;
  607. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  608. XFS_BMAPI_PREALLOC);
  609. break;
  610. case XFS_IOC_UNRESVSP:
  611. case XFS_IOC_UNRESVSP64:
  612. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  613. break;
  614. case XFS_IOC_ALLOCSP:
  615. case XFS_IOC_ALLOCSP64:
  616. case XFS_IOC_FREESP:
  617. case XFS_IOC_FREESP64:
  618. flags |= XFS_PREALLOC_CLEAR;
  619. if (bf->l_start > XFS_ISIZE(ip)) {
  620. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  621. bf->l_start - XFS_ISIZE(ip), 0);
  622. if (error)
  623. goto out_unlock;
  624. }
  625. iattr.ia_valid = ATTR_SIZE;
  626. iattr.ia_size = bf->l_start;
  627. error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
  628. break;
  629. default:
  630. ASSERT(0);
  631. error = -EINVAL;
  632. }
  633. if (error)
  634. goto out_unlock;
  635. error = xfs_update_prealloc_flags(ip, flags);
  636. out_unlock:
  637. xfs_iunlock(ip, iolock);
  638. mnt_drop_write_file(filp);
  639. return error;
  640. }
  641. STATIC int
  642. xfs_ioc_bulkstat(
  643. xfs_mount_t *mp,
  644. unsigned int cmd,
  645. void __user *arg)
  646. {
  647. xfs_fsop_bulkreq_t bulkreq;
  648. int count; /* # of records returned */
  649. xfs_ino_t inlast; /* last inode number */
  650. int done;
  651. int error;
  652. /* done = 1 if there are more stats to get and if bulkstat */
  653. /* should be called again (unused here, but used in dmapi) */
  654. if (!capable(CAP_SYS_ADMIN))
  655. return -EPERM;
  656. if (XFS_FORCED_SHUTDOWN(mp))
  657. return -EIO;
  658. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  659. return -EFAULT;
  660. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  661. return -EFAULT;
  662. if ((count = bulkreq.icount) <= 0)
  663. return -EINVAL;
  664. if (bulkreq.ubuffer == NULL)
  665. return -EINVAL;
  666. if (cmd == XFS_IOC_FSINUMBERS)
  667. error = xfs_inumbers(mp, &inlast, &count,
  668. bulkreq.ubuffer, xfs_inumbers_fmt);
  669. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  670. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  671. sizeof(xfs_bstat_t), NULL, &done);
  672. else /* XFS_IOC_FSBULKSTAT */
  673. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  674. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  675. &done);
  676. if (error)
  677. return error;
  678. if (bulkreq.ocount != NULL) {
  679. if (copy_to_user(bulkreq.lastip, &inlast,
  680. sizeof(xfs_ino_t)))
  681. return -EFAULT;
  682. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  683. return -EFAULT;
  684. }
  685. return 0;
  686. }
  687. STATIC int
  688. xfs_ioc_fsgeometry_v1(
  689. xfs_mount_t *mp,
  690. void __user *arg)
  691. {
  692. xfs_fsop_geom_t fsgeo;
  693. int error;
  694. error = xfs_fs_geometry(mp, &fsgeo, 3);
  695. if (error)
  696. return error;
  697. /*
  698. * Caller should have passed an argument of type
  699. * xfs_fsop_geom_v1_t. This is a proper subset of the
  700. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  701. */
  702. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  703. return -EFAULT;
  704. return 0;
  705. }
  706. STATIC int
  707. xfs_ioc_fsgeometry(
  708. xfs_mount_t *mp,
  709. void __user *arg)
  710. {
  711. xfs_fsop_geom_t fsgeo;
  712. int error;
  713. error = xfs_fs_geometry(mp, &fsgeo, 4);
  714. if (error)
  715. return error;
  716. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  717. return -EFAULT;
  718. return 0;
  719. }
  720. /*
  721. * Linux extended inode flags interface.
  722. */
  723. STATIC unsigned int
  724. xfs_merge_ioc_xflags(
  725. unsigned int flags,
  726. unsigned int start)
  727. {
  728. unsigned int xflags = start;
  729. if (flags & FS_IMMUTABLE_FL)
  730. xflags |= FS_XFLAG_IMMUTABLE;
  731. else
  732. xflags &= ~FS_XFLAG_IMMUTABLE;
  733. if (flags & FS_APPEND_FL)
  734. xflags |= FS_XFLAG_APPEND;
  735. else
  736. xflags &= ~FS_XFLAG_APPEND;
  737. if (flags & FS_SYNC_FL)
  738. xflags |= FS_XFLAG_SYNC;
  739. else
  740. xflags &= ~FS_XFLAG_SYNC;
  741. if (flags & FS_NOATIME_FL)
  742. xflags |= FS_XFLAG_NOATIME;
  743. else
  744. xflags &= ~FS_XFLAG_NOATIME;
  745. if (flags & FS_NODUMP_FL)
  746. xflags |= FS_XFLAG_NODUMP;
  747. else
  748. xflags &= ~FS_XFLAG_NODUMP;
  749. return xflags;
  750. }
  751. STATIC unsigned int
  752. xfs_di2lxflags(
  753. uint16_t di_flags)
  754. {
  755. unsigned int flags = 0;
  756. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  757. flags |= FS_IMMUTABLE_FL;
  758. if (di_flags & XFS_DIFLAG_APPEND)
  759. flags |= FS_APPEND_FL;
  760. if (di_flags & XFS_DIFLAG_SYNC)
  761. flags |= FS_SYNC_FL;
  762. if (di_flags & XFS_DIFLAG_NOATIME)
  763. flags |= FS_NOATIME_FL;
  764. if (di_flags & XFS_DIFLAG_NODUMP)
  765. flags |= FS_NODUMP_FL;
  766. return flags;
  767. }
  768. STATIC int
  769. xfs_ioc_fsgetxattr(
  770. xfs_inode_t *ip,
  771. int attr,
  772. void __user *arg)
  773. {
  774. struct fsxattr fa;
  775. memset(&fa, 0, sizeof(struct fsxattr));
  776. xfs_ilock(ip, XFS_ILOCK_SHARED);
  777. fa.fsx_xflags = xfs_ip2xflags(ip);
  778. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  779. fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
  780. ip->i_mount->m_sb.sb_blocklog;
  781. fa.fsx_projid = xfs_get_projid(ip);
  782. if (attr) {
  783. if (ip->i_afp) {
  784. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  785. fa.fsx_nextents = xfs_iext_count(ip->i_afp);
  786. else
  787. fa.fsx_nextents = ip->i_d.di_anextents;
  788. } else
  789. fa.fsx_nextents = 0;
  790. } else {
  791. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  792. fa.fsx_nextents = xfs_iext_count(&ip->i_df);
  793. else
  794. fa.fsx_nextents = ip->i_d.di_nextents;
  795. }
  796. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  797. if (copy_to_user(arg, &fa, sizeof(fa)))
  798. return -EFAULT;
  799. return 0;
  800. }
  801. STATIC uint16_t
  802. xfs_flags2diflags(
  803. struct xfs_inode *ip,
  804. unsigned int xflags)
  805. {
  806. /* can't set PREALLOC this way, just preserve it */
  807. uint16_t di_flags =
  808. (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  809. if (xflags & FS_XFLAG_IMMUTABLE)
  810. di_flags |= XFS_DIFLAG_IMMUTABLE;
  811. if (xflags & FS_XFLAG_APPEND)
  812. di_flags |= XFS_DIFLAG_APPEND;
  813. if (xflags & FS_XFLAG_SYNC)
  814. di_flags |= XFS_DIFLAG_SYNC;
  815. if (xflags & FS_XFLAG_NOATIME)
  816. di_flags |= XFS_DIFLAG_NOATIME;
  817. if (xflags & FS_XFLAG_NODUMP)
  818. di_flags |= XFS_DIFLAG_NODUMP;
  819. if (xflags & FS_XFLAG_NODEFRAG)
  820. di_flags |= XFS_DIFLAG_NODEFRAG;
  821. if (xflags & FS_XFLAG_FILESTREAM)
  822. di_flags |= XFS_DIFLAG_FILESTREAM;
  823. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  824. if (xflags & FS_XFLAG_RTINHERIT)
  825. di_flags |= XFS_DIFLAG_RTINHERIT;
  826. if (xflags & FS_XFLAG_NOSYMLINKS)
  827. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  828. if (xflags & FS_XFLAG_EXTSZINHERIT)
  829. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  830. if (xflags & FS_XFLAG_PROJINHERIT)
  831. di_flags |= XFS_DIFLAG_PROJINHERIT;
  832. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  833. if (xflags & FS_XFLAG_REALTIME)
  834. di_flags |= XFS_DIFLAG_REALTIME;
  835. if (xflags & FS_XFLAG_EXTSIZE)
  836. di_flags |= XFS_DIFLAG_EXTSIZE;
  837. }
  838. return di_flags;
  839. }
  840. STATIC uint64_t
  841. xfs_flags2diflags2(
  842. struct xfs_inode *ip,
  843. unsigned int xflags)
  844. {
  845. uint64_t di_flags2 =
  846. (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
  847. if (xflags & FS_XFLAG_DAX)
  848. di_flags2 |= XFS_DIFLAG2_DAX;
  849. if (xflags & FS_XFLAG_COWEXTSIZE)
  850. di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  851. return di_flags2;
  852. }
  853. STATIC void
  854. xfs_diflags_to_linux(
  855. struct xfs_inode *ip)
  856. {
  857. struct inode *inode = VFS_I(ip);
  858. unsigned int xflags = xfs_ip2xflags(ip);
  859. if (xflags & FS_XFLAG_IMMUTABLE)
  860. inode->i_flags |= S_IMMUTABLE;
  861. else
  862. inode->i_flags &= ~S_IMMUTABLE;
  863. if (xflags & FS_XFLAG_APPEND)
  864. inode->i_flags |= S_APPEND;
  865. else
  866. inode->i_flags &= ~S_APPEND;
  867. if (xflags & FS_XFLAG_SYNC)
  868. inode->i_flags |= S_SYNC;
  869. else
  870. inode->i_flags &= ~S_SYNC;
  871. if (xflags & FS_XFLAG_NOATIME)
  872. inode->i_flags |= S_NOATIME;
  873. else
  874. inode->i_flags &= ~S_NOATIME;
  875. #if 0 /* disabled until the flag switching races are sorted out */
  876. if (xflags & FS_XFLAG_DAX)
  877. inode->i_flags |= S_DAX;
  878. else
  879. inode->i_flags &= ~S_DAX;
  880. #endif
  881. }
  882. static int
  883. xfs_ioctl_setattr_xflags(
  884. struct xfs_trans *tp,
  885. struct xfs_inode *ip,
  886. struct fsxattr *fa)
  887. {
  888. struct xfs_mount *mp = ip->i_mount;
  889. uint64_t di_flags2;
  890. /* Can't change realtime flag if any extents are allocated. */
  891. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  892. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  893. return -EINVAL;
  894. /* If realtime flag is set then must have realtime device */
  895. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  896. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  897. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  898. return -EINVAL;
  899. }
  900. /* Clear reflink if we are actually able to set the rt flag. */
  901. if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
  902. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  903. /* Don't allow us to set DAX mode for a reflinked file for now. */
  904. if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
  905. return -EINVAL;
  906. /*
  907. * Can't modify an immutable/append-only file unless
  908. * we have appropriate permission.
  909. */
  910. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  911. (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
  912. !capable(CAP_LINUX_IMMUTABLE))
  913. return -EPERM;
  914. /* diflags2 only valid for v3 inodes. */
  915. di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  916. if (di_flags2 && ip->i_d.di_version < 3)
  917. return -EINVAL;
  918. ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
  919. ip->i_d.di_flags2 = di_flags2;
  920. xfs_diflags_to_linux(ip);
  921. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  922. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  923. XFS_STATS_INC(mp, xs_ig_attrchg);
  924. return 0;
  925. }
  926. /*
  927. * If we are changing DAX flags, we have to ensure the file is clean and any
  928. * cached objects in the address space are invalidated and removed. This
  929. * requires us to lock out other IO and page faults similar to a truncate
  930. * operation. The locks need to be held until the transaction has been committed
  931. * so that the cache invalidation is atomic with respect to the DAX flag
  932. * manipulation.
  933. */
  934. static int
  935. xfs_ioctl_setattr_dax_invalidate(
  936. struct xfs_inode *ip,
  937. struct fsxattr *fa,
  938. int *join_flags)
  939. {
  940. struct inode *inode = VFS_I(ip);
  941. struct super_block *sb = inode->i_sb;
  942. int error;
  943. *join_flags = 0;
  944. /*
  945. * It is only valid to set the DAX flag on regular files and
  946. * directories on filesystems where the block size is equal to the page
  947. * size. On directories it serves as an inherit hint.
  948. */
  949. if (fa->fsx_xflags & FS_XFLAG_DAX) {
  950. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  951. return -EINVAL;
  952. if (bdev_dax_supported(sb, sb->s_blocksize) < 0)
  953. return -EINVAL;
  954. }
  955. /* If the DAX state is not changing, we have nothing to do here. */
  956. if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
  957. return 0;
  958. if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
  959. return 0;
  960. /* lock, flush and invalidate mapping in preparation for flag change */
  961. xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  962. error = filemap_write_and_wait(inode->i_mapping);
  963. if (error)
  964. goto out_unlock;
  965. error = invalidate_inode_pages2(inode->i_mapping);
  966. if (error)
  967. goto out_unlock;
  968. *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
  969. return 0;
  970. out_unlock:
  971. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  972. return error;
  973. }
  974. /*
  975. * Set up the transaction structure for the setattr operation, checking that we
  976. * have permission to do so. On success, return a clean transaction and the
  977. * inode locked exclusively ready for further operation specific checks. On
  978. * failure, return an error without modifying or locking the inode.
  979. *
  980. * The inode might already be IO locked on call. If this is the case, it is
  981. * indicated in @join_flags and we take full responsibility for ensuring they
  982. * are unlocked from now on. Hence if we have an error here, we still have to
  983. * unlock them. Otherwise, once they are joined to the transaction, they will
  984. * be unlocked on commit/cancel.
  985. */
  986. static struct xfs_trans *
  987. xfs_ioctl_setattr_get_trans(
  988. struct xfs_inode *ip,
  989. int join_flags)
  990. {
  991. struct xfs_mount *mp = ip->i_mount;
  992. struct xfs_trans *tp;
  993. int error = -EROFS;
  994. if (mp->m_flags & XFS_MOUNT_RDONLY)
  995. goto out_unlock;
  996. error = -EIO;
  997. if (XFS_FORCED_SHUTDOWN(mp))
  998. goto out_unlock;
  999. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  1000. if (error)
  1001. return ERR_PTR(error);
  1002. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1003. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
  1004. join_flags = 0;
  1005. /*
  1006. * CAP_FOWNER overrides the following restrictions:
  1007. *
  1008. * The user ID of the calling process must be equal to the file owner
  1009. * ID, except in cases where the CAP_FSETID capability is applicable.
  1010. */
  1011. if (!inode_owner_or_capable(VFS_I(ip))) {
  1012. error = -EPERM;
  1013. goto out_cancel;
  1014. }
  1015. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1016. xfs_trans_set_sync(tp);
  1017. return tp;
  1018. out_cancel:
  1019. xfs_trans_cancel(tp);
  1020. out_unlock:
  1021. if (join_flags)
  1022. xfs_iunlock(ip, join_flags);
  1023. return ERR_PTR(error);
  1024. }
  1025. /*
  1026. * extent size hint validation is somewhat cumbersome. Rules are:
  1027. *
  1028. * 1. extent size hint is only valid for directories and regular files
  1029. * 2. FS_XFLAG_EXTSIZE is only valid for regular files
  1030. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  1031. * 4. can only be changed on regular files if no extents are allocated
  1032. * 5. can be changed on directories at any time
  1033. * 6. extsize hint of 0 turns off hints, clears inode flags.
  1034. * 7. Extent size must be a multiple of the appropriate block size.
  1035. * 8. for non-realtime files, the extent size hint must be limited
  1036. * to half the AG size to avoid alignment extending the extent beyond the
  1037. * limits of the AG.
  1038. */
  1039. static int
  1040. xfs_ioctl_setattr_check_extsize(
  1041. struct xfs_inode *ip,
  1042. struct fsxattr *fa)
  1043. {
  1044. struct xfs_mount *mp = ip->i_mount;
  1045. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
  1046. return -EINVAL;
  1047. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  1048. !S_ISDIR(VFS_I(ip)->i_mode))
  1049. return -EINVAL;
  1050. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
  1051. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  1052. return -EINVAL;
  1053. if (fa->fsx_extsize != 0) {
  1054. xfs_extlen_t size;
  1055. xfs_fsblock_t extsize_fsb;
  1056. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1057. if (extsize_fsb > MAXEXTLEN)
  1058. return -EINVAL;
  1059. if (XFS_IS_REALTIME_INODE(ip) ||
  1060. (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
  1061. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  1062. } else {
  1063. size = mp->m_sb.sb_blocksize;
  1064. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  1065. return -EINVAL;
  1066. }
  1067. if (fa->fsx_extsize % size)
  1068. return -EINVAL;
  1069. } else
  1070. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  1071. return 0;
  1072. }
  1073. /*
  1074. * CoW extent size hint validation rules are:
  1075. *
  1076. * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
  1077. * The inode does not have to have any shared blocks, but it must be a v3.
  1078. * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
  1079. * for a directory, the hint is propagated to new files.
  1080. * 3. Can be changed on files & directories at any time.
  1081. * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
  1082. * 5. Extent size must be a multiple of the appropriate block size.
  1083. * 6. The extent size hint must be limited to half the AG size to avoid
  1084. * alignment extending the extent beyond the limits of the AG.
  1085. */
  1086. static int
  1087. xfs_ioctl_setattr_check_cowextsize(
  1088. struct xfs_inode *ip,
  1089. struct fsxattr *fa)
  1090. {
  1091. struct xfs_mount *mp = ip->i_mount;
  1092. if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
  1093. return 0;
  1094. if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
  1095. ip->i_d.di_version != 3)
  1096. return -EINVAL;
  1097. if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
  1098. return -EINVAL;
  1099. if (fa->fsx_cowextsize != 0) {
  1100. xfs_extlen_t size;
  1101. xfs_fsblock_t cowextsize_fsb;
  1102. cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  1103. if (cowextsize_fsb > MAXEXTLEN)
  1104. return -EINVAL;
  1105. size = mp->m_sb.sb_blocksize;
  1106. if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
  1107. return -EINVAL;
  1108. if (fa->fsx_cowextsize % size)
  1109. return -EINVAL;
  1110. } else
  1111. fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
  1112. return 0;
  1113. }
  1114. static int
  1115. xfs_ioctl_setattr_check_projid(
  1116. struct xfs_inode *ip,
  1117. struct fsxattr *fa)
  1118. {
  1119. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1120. if (fa->fsx_projid > (uint16_t)-1 &&
  1121. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1122. return -EINVAL;
  1123. /*
  1124. * Project Quota ID state is only allowed to change from within the init
  1125. * namespace. Enforce that restriction only if we are trying to change
  1126. * the quota ID state. Everything else is allowed in user namespaces.
  1127. */
  1128. if (current_user_ns() == &init_user_ns)
  1129. return 0;
  1130. if (xfs_get_projid(ip) != fa->fsx_projid)
  1131. return -EINVAL;
  1132. if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
  1133. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1134. return -EINVAL;
  1135. return 0;
  1136. }
  1137. STATIC int
  1138. xfs_ioctl_setattr(
  1139. xfs_inode_t *ip,
  1140. struct fsxattr *fa)
  1141. {
  1142. struct xfs_mount *mp = ip->i_mount;
  1143. struct xfs_trans *tp;
  1144. struct xfs_dquot *udqp = NULL;
  1145. struct xfs_dquot *pdqp = NULL;
  1146. struct xfs_dquot *olddquot = NULL;
  1147. int code;
  1148. int join_flags = 0;
  1149. trace_xfs_ioctl_setattr(ip);
  1150. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1151. if (code)
  1152. return code;
  1153. /*
  1154. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1155. * before we start any other transactions. Trying to do this later
  1156. * is messy. We don't care to take a readlock to look at the ids
  1157. * in inode here, because we can't hold it across the trans_reserve.
  1158. * If the IDs do change before we take the ilock, we're covered
  1159. * because the i_*dquot fields will get updated anyway.
  1160. */
  1161. if (XFS_IS_QUOTA_ON(mp)) {
  1162. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1163. ip->i_d.di_gid, fa->fsx_projid,
  1164. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1165. if (code)
  1166. return code;
  1167. }
  1168. /*
  1169. * Changing DAX config may require inode locking for mapping
  1170. * invalidation. These need to be held all the way to transaction commit
  1171. * or cancel time, so need to be passed through to
  1172. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1173. * appropriately.
  1174. */
  1175. code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
  1176. if (code)
  1177. goto error_free_dquots;
  1178. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1179. if (IS_ERR(tp)) {
  1180. code = PTR_ERR(tp);
  1181. goto error_free_dquots;
  1182. }
  1183. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1184. xfs_get_projid(ip) != fa->fsx_projid) {
  1185. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1186. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1187. if (code) /* out of quota */
  1188. goto error_trans_cancel;
  1189. }
  1190. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1191. if (code)
  1192. goto error_trans_cancel;
  1193. code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  1194. if (code)
  1195. goto error_trans_cancel;
  1196. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1197. if (code)
  1198. goto error_trans_cancel;
  1199. /*
  1200. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1201. * overrides the following restrictions:
  1202. *
  1203. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1204. * successful return from chown()
  1205. */
  1206. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1207. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1208. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1209. /* Change the ownerships and register project quota modifications */
  1210. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1211. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1212. olddquot = xfs_qm_vop_chown(tp, ip,
  1213. &ip->i_pdquot, pdqp);
  1214. }
  1215. ASSERT(ip->i_d.di_version > 1);
  1216. xfs_set_projid(ip, fa->fsx_projid);
  1217. }
  1218. /*
  1219. * Only set the extent size hint if we've already determined that the
  1220. * extent size hint should be set on the inode. If no extent size flags
  1221. * are set on the inode then unconditionally clear the extent size hint.
  1222. */
  1223. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1224. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1225. else
  1226. ip->i_d.di_extsize = 0;
  1227. if (ip->i_d.di_version == 3 &&
  1228. (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1229. ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
  1230. mp->m_sb.sb_blocklog;
  1231. else
  1232. ip->i_d.di_cowextsize = 0;
  1233. code = xfs_trans_commit(tp);
  1234. /*
  1235. * Release any dquot(s) the inode had kept before chown.
  1236. */
  1237. xfs_qm_dqrele(olddquot);
  1238. xfs_qm_dqrele(udqp);
  1239. xfs_qm_dqrele(pdqp);
  1240. return code;
  1241. error_trans_cancel:
  1242. xfs_trans_cancel(tp);
  1243. error_free_dquots:
  1244. xfs_qm_dqrele(udqp);
  1245. xfs_qm_dqrele(pdqp);
  1246. return code;
  1247. }
  1248. STATIC int
  1249. xfs_ioc_fssetxattr(
  1250. xfs_inode_t *ip,
  1251. struct file *filp,
  1252. void __user *arg)
  1253. {
  1254. struct fsxattr fa;
  1255. int error;
  1256. if (copy_from_user(&fa, arg, sizeof(fa)))
  1257. return -EFAULT;
  1258. error = mnt_want_write_file(filp);
  1259. if (error)
  1260. return error;
  1261. error = xfs_ioctl_setattr(ip, &fa);
  1262. mnt_drop_write_file(filp);
  1263. return error;
  1264. }
  1265. STATIC int
  1266. xfs_ioc_getxflags(
  1267. xfs_inode_t *ip,
  1268. void __user *arg)
  1269. {
  1270. unsigned int flags;
  1271. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1272. if (copy_to_user(arg, &flags, sizeof(flags)))
  1273. return -EFAULT;
  1274. return 0;
  1275. }
  1276. STATIC int
  1277. xfs_ioc_setxflags(
  1278. struct xfs_inode *ip,
  1279. struct file *filp,
  1280. void __user *arg)
  1281. {
  1282. struct xfs_trans *tp;
  1283. struct fsxattr fa;
  1284. unsigned int flags;
  1285. int join_flags = 0;
  1286. int error;
  1287. if (copy_from_user(&flags, arg, sizeof(flags)))
  1288. return -EFAULT;
  1289. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1290. FS_NOATIME_FL | FS_NODUMP_FL | \
  1291. FS_SYNC_FL))
  1292. return -EOPNOTSUPP;
  1293. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1294. error = mnt_want_write_file(filp);
  1295. if (error)
  1296. return error;
  1297. /*
  1298. * Changing DAX config may require inode locking for mapping
  1299. * invalidation. These need to be held all the way to transaction commit
  1300. * or cancel time, so need to be passed through to
  1301. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1302. * appropriately.
  1303. */
  1304. error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
  1305. if (error)
  1306. goto out_drop_write;
  1307. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1308. if (IS_ERR(tp)) {
  1309. error = PTR_ERR(tp);
  1310. goto out_drop_write;
  1311. }
  1312. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1313. if (error) {
  1314. xfs_trans_cancel(tp);
  1315. goto out_drop_write;
  1316. }
  1317. error = xfs_trans_commit(tp);
  1318. out_drop_write:
  1319. mnt_drop_write_file(filp);
  1320. return error;
  1321. }
  1322. static bool
  1323. xfs_getbmap_format(
  1324. struct kgetbmap *p,
  1325. struct getbmapx __user *u,
  1326. size_t recsize)
  1327. {
  1328. if (put_user(p->bmv_offset, &u->bmv_offset) ||
  1329. put_user(p->bmv_block, &u->bmv_block) ||
  1330. put_user(p->bmv_length, &u->bmv_length) ||
  1331. put_user(0, &u->bmv_count) ||
  1332. put_user(0, &u->bmv_entries))
  1333. return false;
  1334. if (recsize < sizeof(struct getbmapx))
  1335. return true;
  1336. if (put_user(0, &u->bmv_iflags) ||
  1337. put_user(p->bmv_oflags, &u->bmv_oflags) ||
  1338. put_user(0, &u->bmv_unused1) ||
  1339. put_user(0, &u->bmv_unused2))
  1340. return false;
  1341. return true;
  1342. }
  1343. STATIC int
  1344. xfs_ioc_getbmap(
  1345. struct file *file,
  1346. unsigned int cmd,
  1347. void __user *arg)
  1348. {
  1349. struct getbmapx bmx = { 0 };
  1350. struct kgetbmap *buf;
  1351. size_t recsize;
  1352. int error, i;
  1353. switch (cmd) {
  1354. case XFS_IOC_GETBMAPA:
  1355. bmx.bmv_iflags = BMV_IF_ATTRFORK;
  1356. /*FALLTHRU*/
  1357. case XFS_IOC_GETBMAP:
  1358. if (file->f_mode & FMODE_NOCMTIME)
  1359. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1360. /* struct getbmap is a strict subset of struct getbmapx. */
  1361. recsize = sizeof(struct getbmap);
  1362. break;
  1363. case XFS_IOC_GETBMAPX:
  1364. recsize = sizeof(struct getbmapx);
  1365. break;
  1366. default:
  1367. return -EINVAL;
  1368. }
  1369. if (copy_from_user(&bmx, arg, recsize))
  1370. return -EFAULT;
  1371. if (bmx.bmv_count < 2)
  1372. return -EINVAL;
  1373. if (bmx.bmv_count > ULONG_MAX / recsize)
  1374. return -ENOMEM;
  1375. buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
  1376. if (!buf)
  1377. return -ENOMEM;
  1378. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
  1379. if (error)
  1380. goto out_free_buf;
  1381. error = -EFAULT;
  1382. if (copy_to_user(arg, &bmx, recsize))
  1383. goto out_free_buf;
  1384. arg += recsize;
  1385. for (i = 0; i < bmx.bmv_entries; i++) {
  1386. if (!xfs_getbmap_format(buf + i, arg, recsize))
  1387. goto out_free_buf;
  1388. arg += recsize;
  1389. }
  1390. error = 0;
  1391. out_free_buf:
  1392. kmem_free(buf);
  1393. return 0;
  1394. }
  1395. struct getfsmap_info {
  1396. struct xfs_mount *mp;
  1397. struct fsmap_head __user *data;
  1398. unsigned int idx;
  1399. __u32 last_flags;
  1400. };
  1401. STATIC int
  1402. xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
  1403. {
  1404. struct getfsmap_info *info = priv;
  1405. struct fsmap fm;
  1406. trace_xfs_getfsmap_mapping(info->mp, xfm);
  1407. info->last_flags = xfm->fmr_flags;
  1408. xfs_fsmap_from_internal(&fm, xfm);
  1409. if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
  1410. sizeof(struct fsmap)))
  1411. return -EFAULT;
  1412. return 0;
  1413. }
  1414. STATIC int
  1415. xfs_ioc_getfsmap(
  1416. struct xfs_inode *ip,
  1417. struct fsmap_head __user *arg)
  1418. {
  1419. struct getfsmap_info info = { NULL };
  1420. struct xfs_fsmap_head xhead = {0};
  1421. struct fsmap_head head;
  1422. bool aborted = false;
  1423. int error;
  1424. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  1425. return -EFAULT;
  1426. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  1427. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  1428. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  1429. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  1430. sizeof(head.fmh_keys[1].fmr_reserved)))
  1431. return -EINVAL;
  1432. xhead.fmh_iflags = head.fmh_iflags;
  1433. xhead.fmh_count = head.fmh_count;
  1434. xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
  1435. xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
  1436. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1437. trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
  1438. info.mp = ip->i_mount;
  1439. info.data = arg;
  1440. error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
  1441. if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
  1442. error = 0;
  1443. aborted = true;
  1444. } else if (error)
  1445. return error;
  1446. /* If we didn't abort, set the "last" flag in the last fmx */
  1447. if (!aborted && info.idx) {
  1448. info.last_flags |= FMR_OF_LAST;
  1449. if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
  1450. &info.last_flags, sizeof(info.last_flags)))
  1451. return -EFAULT;
  1452. }
  1453. /* copy back header */
  1454. head.fmh_entries = xhead.fmh_entries;
  1455. head.fmh_oflags = xhead.fmh_oflags;
  1456. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  1457. return -EFAULT;
  1458. return 0;
  1459. }
  1460. STATIC int
  1461. xfs_ioc_scrub_metadata(
  1462. struct xfs_inode *ip,
  1463. void __user *arg)
  1464. {
  1465. struct xfs_scrub_metadata scrub;
  1466. int error;
  1467. if (!capable(CAP_SYS_ADMIN))
  1468. return -EPERM;
  1469. if (copy_from_user(&scrub, arg, sizeof(scrub)))
  1470. return -EFAULT;
  1471. error = xfs_scrub_metadata(ip, &scrub);
  1472. if (error)
  1473. return error;
  1474. if (copy_to_user(arg, &scrub, sizeof(scrub)))
  1475. return -EFAULT;
  1476. return 0;
  1477. }
  1478. int
  1479. xfs_ioc_swapext(
  1480. xfs_swapext_t *sxp)
  1481. {
  1482. xfs_inode_t *ip, *tip;
  1483. struct fd f, tmp;
  1484. int error = 0;
  1485. /* Pull information for the target fd */
  1486. f = fdget((int)sxp->sx_fdtarget);
  1487. if (!f.file) {
  1488. error = -EINVAL;
  1489. goto out;
  1490. }
  1491. if (!(f.file->f_mode & FMODE_WRITE) ||
  1492. !(f.file->f_mode & FMODE_READ) ||
  1493. (f.file->f_flags & O_APPEND)) {
  1494. error = -EBADF;
  1495. goto out_put_file;
  1496. }
  1497. tmp = fdget((int)sxp->sx_fdtmp);
  1498. if (!tmp.file) {
  1499. error = -EINVAL;
  1500. goto out_put_file;
  1501. }
  1502. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1503. !(tmp.file->f_mode & FMODE_READ) ||
  1504. (tmp.file->f_flags & O_APPEND)) {
  1505. error = -EBADF;
  1506. goto out_put_tmp_file;
  1507. }
  1508. if (IS_SWAPFILE(file_inode(f.file)) ||
  1509. IS_SWAPFILE(file_inode(tmp.file))) {
  1510. error = -EINVAL;
  1511. goto out_put_tmp_file;
  1512. }
  1513. /*
  1514. * We need to ensure that the fds passed in point to XFS inodes
  1515. * before we cast and access them as XFS structures as we have no
  1516. * control over what the user passes us here.
  1517. */
  1518. if (f.file->f_op != &xfs_file_operations ||
  1519. tmp.file->f_op != &xfs_file_operations) {
  1520. error = -EINVAL;
  1521. goto out_put_tmp_file;
  1522. }
  1523. ip = XFS_I(file_inode(f.file));
  1524. tip = XFS_I(file_inode(tmp.file));
  1525. if (ip->i_mount != tip->i_mount) {
  1526. error = -EINVAL;
  1527. goto out_put_tmp_file;
  1528. }
  1529. if (ip->i_ino == tip->i_ino) {
  1530. error = -EINVAL;
  1531. goto out_put_tmp_file;
  1532. }
  1533. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1534. error = -EIO;
  1535. goto out_put_tmp_file;
  1536. }
  1537. error = xfs_swap_extents(ip, tip, sxp);
  1538. out_put_tmp_file:
  1539. fdput(tmp);
  1540. out_put_file:
  1541. fdput(f);
  1542. out:
  1543. return error;
  1544. }
  1545. /*
  1546. * Note: some of the ioctl's return positive numbers as a
  1547. * byte count indicating success, such as readlink_by_handle.
  1548. * So we don't "sign flip" like most other routines. This means
  1549. * true errors need to be returned as a negative value.
  1550. */
  1551. long
  1552. xfs_file_ioctl(
  1553. struct file *filp,
  1554. unsigned int cmd,
  1555. unsigned long p)
  1556. {
  1557. struct inode *inode = file_inode(filp);
  1558. struct xfs_inode *ip = XFS_I(inode);
  1559. struct xfs_mount *mp = ip->i_mount;
  1560. void __user *arg = (void __user *)p;
  1561. int error;
  1562. trace_xfs_file_ioctl(ip);
  1563. switch (cmd) {
  1564. case FITRIM:
  1565. return xfs_ioc_trim(mp, arg);
  1566. case XFS_IOC_ALLOCSP:
  1567. case XFS_IOC_FREESP:
  1568. case XFS_IOC_RESVSP:
  1569. case XFS_IOC_UNRESVSP:
  1570. case XFS_IOC_ALLOCSP64:
  1571. case XFS_IOC_FREESP64:
  1572. case XFS_IOC_RESVSP64:
  1573. case XFS_IOC_UNRESVSP64:
  1574. case XFS_IOC_ZERO_RANGE: {
  1575. xfs_flock64_t bf;
  1576. if (copy_from_user(&bf, arg, sizeof(bf)))
  1577. return -EFAULT;
  1578. return xfs_ioc_space(filp, cmd, &bf);
  1579. }
  1580. case XFS_IOC_DIOINFO: {
  1581. struct dioattr da;
  1582. xfs_buftarg_t *target =
  1583. XFS_IS_REALTIME_INODE(ip) ?
  1584. mp->m_rtdev_targp : mp->m_ddev_targp;
  1585. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1586. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1587. if (copy_to_user(arg, &da, sizeof(da)))
  1588. return -EFAULT;
  1589. return 0;
  1590. }
  1591. case XFS_IOC_FSBULKSTAT_SINGLE:
  1592. case XFS_IOC_FSBULKSTAT:
  1593. case XFS_IOC_FSINUMBERS:
  1594. return xfs_ioc_bulkstat(mp, cmd, arg);
  1595. case XFS_IOC_FSGEOMETRY_V1:
  1596. return xfs_ioc_fsgeometry_v1(mp, arg);
  1597. case XFS_IOC_FSGEOMETRY:
  1598. return xfs_ioc_fsgeometry(mp, arg);
  1599. case XFS_IOC_GETVERSION:
  1600. return put_user(inode->i_generation, (int __user *)arg);
  1601. case XFS_IOC_FSGETXATTR:
  1602. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1603. case XFS_IOC_FSGETXATTRA:
  1604. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1605. case XFS_IOC_FSSETXATTR:
  1606. return xfs_ioc_fssetxattr(ip, filp, arg);
  1607. case XFS_IOC_GETXFLAGS:
  1608. return xfs_ioc_getxflags(ip, arg);
  1609. case XFS_IOC_SETXFLAGS:
  1610. return xfs_ioc_setxflags(ip, filp, arg);
  1611. case XFS_IOC_FSSETDM: {
  1612. struct fsdmidata dmi;
  1613. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1614. return -EFAULT;
  1615. error = mnt_want_write_file(filp);
  1616. if (error)
  1617. return error;
  1618. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1619. dmi.fsd_dmstate);
  1620. mnt_drop_write_file(filp);
  1621. return error;
  1622. }
  1623. case XFS_IOC_GETBMAP:
  1624. case XFS_IOC_GETBMAPA:
  1625. case XFS_IOC_GETBMAPX:
  1626. return xfs_ioc_getbmap(filp, cmd, arg);
  1627. case FS_IOC_GETFSMAP:
  1628. return xfs_ioc_getfsmap(ip, arg);
  1629. case XFS_IOC_SCRUB_METADATA:
  1630. return xfs_ioc_scrub_metadata(ip, arg);
  1631. case XFS_IOC_FD_TO_HANDLE:
  1632. case XFS_IOC_PATH_TO_HANDLE:
  1633. case XFS_IOC_PATH_TO_FSHANDLE: {
  1634. xfs_fsop_handlereq_t hreq;
  1635. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1636. return -EFAULT;
  1637. return xfs_find_handle(cmd, &hreq);
  1638. }
  1639. case XFS_IOC_OPEN_BY_HANDLE: {
  1640. xfs_fsop_handlereq_t hreq;
  1641. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1642. return -EFAULT;
  1643. return xfs_open_by_handle(filp, &hreq);
  1644. }
  1645. case XFS_IOC_FSSETDM_BY_HANDLE:
  1646. return xfs_fssetdm_by_handle(filp, arg);
  1647. case XFS_IOC_READLINK_BY_HANDLE: {
  1648. xfs_fsop_handlereq_t hreq;
  1649. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1650. return -EFAULT;
  1651. return xfs_readlink_by_handle(filp, &hreq);
  1652. }
  1653. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1654. return xfs_attrlist_by_handle(filp, arg);
  1655. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1656. return xfs_attrmulti_by_handle(filp, arg);
  1657. case XFS_IOC_SWAPEXT: {
  1658. struct xfs_swapext sxp;
  1659. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1660. return -EFAULT;
  1661. error = mnt_want_write_file(filp);
  1662. if (error)
  1663. return error;
  1664. error = xfs_ioc_swapext(&sxp);
  1665. mnt_drop_write_file(filp);
  1666. return error;
  1667. }
  1668. case XFS_IOC_FSCOUNTS: {
  1669. xfs_fsop_counts_t out;
  1670. error = xfs_fs_counts(mp, &out);
  1671. if (error)
  1672. return error;
  1673. if (copy_to_user(arg, &out, sizeof(out)))
  1674. return -EFAULT;
  1675. return 0;
  1676. }
  1677. case XFS_IOC_SET_RESBLKS: {
  1678. xfs_fsop_resblks_t inout;
  1679. uint64_t in;
  1680. if (!capable(CAP_SYS_ADMIN))
  1681. return -EPERM;
  1682. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1683. return -EROFS;
  1684. if (copy_from_user(&inout, arg, sizeof(inout)))
  1685. return -EFAULT;
  1686. error = mnt_want_write_file(filp);
  1687. if (error)
  1688. return error;
  1689. /* input parameter is passed in resblks field of structure */
  1690. in = inout.resblks;
  1691. error = xfs_reserve_blocks(mp, &in, &inout);
  1692. mnt_drop_write_file(filp);
  1693. if (error)
  1694. return error;
  1695. if (copy_to_user(arg, &inout, sizeof(inout)))
  1696. return -EFAULT;
  1697. return 0;
  1698. }
  1699. case XFS_IOC_GET_RESBLKS: {
  1700. xfs_fsop_resblks_t out;
  1701. if (!capable(CAP_SYS_ADMIN))
  1702. return -EPERM;
  1703. error = xfs_reserve_blocks(mp, NULL, &out);
  1704. if (error)
  1705. return error;
  1706. if (copy_to_user(arg, &out, sizeof(out)))
  1707. return -EFAULT;
  1708. return 0;
  1709. }
  1710. case XFS_IOC_FSGROWFSDATA: {
  1711. xfs_growfs_data_t in;
  1712. if (copy_from_user(&in, arg, sizeof(in)))
  1713. return -EFAULT;
  1714. error = mnt_want_write_file(filp);
  1715. if (error)
  1716. return error;
  1717. error = xfs_growfs_data(mp, &in);
  1718. mnt_drop_write_file(filp);
  1719. return error;
  1720. }
  1721. case XFS_IOC_FSGROWFSLOG: {
  1722. xfs_growfs_log_t in;
  1723. if (copy_from_user(&in, arg, sizeof(in)))
  1724. return -EFAULT;
  1725. error = mnt_want_write_file(filp);
  1726. if (error)
  1727. return error;
  1728. error = xfs_growfs_log(mp, &in);
  1729. mnt_drop_write_file(filp);
  1730. return error;
  1731. }
  1732. case XFS_IOC_FSGROWFSRT: {
  1733. xfs_growfs_rt_t in;
  1734. if (copy_from_user(&in, arg, sizeof(in)))
  1735. return -EFAULT;
  1736. error = mnt_want_write_file(filp);
  1737. if (error)
  1738. return error;
  1739. error = xfs_growfs_rt(mp, &in);
  1740. mnt_drop_write_file(filp);
  1741. return error;
  1742. }
  1743. case XFS_IOC_GOINGDOWN: {
  1744. uint32_t in;
  1745. if (!capable(CAP_SYS_ADMIN))
  1746. return -EPERM;
  1747. if (get_user(in, (uint32_t __user *)arg))
  1748. return -EFAULT;
  1749. return xfs_fs_goingdown(mp, in);
  1750. }
  1751. case XFS_IOC_ERROR_INJECTION: {
  1752. xfs_error_injection_t in;
  1753. if (!capable(CAP_SYS_ADMIN))
  1754. return -EPERM;
  1755. if (copy_from_user(&in, arg, sizeof(in)))
  1756. return -EFAULT;
  1757. return xfs_errortag_add(mp, in.errtag);
  1758. }
  1759. case XFS_IOC_ERROR_CLEARALL:
  1760. if (!capable(CAP_SYS_ADMIN))
  1761. return -EPERM;
  1762. return xfs_errortag_clearall(mp);
  1763. case XFS_IOC_FREE_EOFBLOCKS: {
  1764. struct xfs_fs_eofblocks eofb;
  1765. struct xfs_eofblocks keofb;
  1766. if (!capable(CAP_SYS_ADMIN))
  1767. return -EPERM;
  1768. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1769. return -EROFS;
  1770. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1771. return -EFAULT;
  1772. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1773. if (error)
  1774. return error;
  1775. return xfs_icache_free_eofblocks(mp, &keofb);
  1776. }
  1777. default:
  1778. return -ENOTTY;
  1779. }
  1780. }