xfs_ioctl.c 47 KB

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