xfs_ioctl.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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_bit.h"
  21. #include "xfs_log.h"
  22. #include "xfs_inum.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_sb.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_dir2.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_dmapi.h"
  29. #include "xfs_mount.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_alloc_btree.h"
  32. #include "xfs_ialloc_btree.h"
  33. #include "xfs_attr_sf.h"
  34. #include "xfs_dir2_sf.h"
  35. #include "xfs_dinode.h"
  36. #include "xfs_inode.h"
  37. #include "xfs_btree.h"
  38. #include "xfs_ialloc.h"
  39. #include "xfs_rtalloc.h"
  40. #include "xfs_itable.h"
  41. #include "xfs_error.h"
  42. #include "xfs_rw.h"
  43. #include "xfs_acl.h"
  44. #include "xfs_cap.h"
  45. #include "xfs_mac.h"
  46. #include "xfs_attr.h"
  47. #include "xfs_bmap.h"
  48. #include "xfs_buf_item.h"
  49. #include "xfs_utils.h"
  50. #include "xfs_dfrag.h"
  51. #include "xfs_fsops.h"
  52. #include <linux/capability.h>
  53. #include <linux/dcache.h>
  54. #include <linux/mount.h>
  55. #include <linux/namei.h>
  56. #include <linux/pagemap.h>
  57. /*
  58. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  59. * a file or fs handle.
  60. *
  61. * XFS_IOC_PATH_TO_FSHANDLE
  62. * returns fs handle for a mount point or path within that mount point
  63. * XFS_IOC_FD_TO_HANDLE
  64. * returns full handle for a FD opened in user space
  65. * XFS_IOC_PATH_TO_HANDLE
  66. * returns full handle for a path
  67. */
  68. STATIC int
  69. xfs_find_handle(
  70. unsigned int cmd,
  71. void __user *arg)
  72. {
  73. int hsize;
  74. xfs_handle_t handle;
  75. xfs_fsop_handlereq_t hreq;
  76. struct inode *inode;
  77. bhv_vnode_t *vp;
  78. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  79. return -XFS_ERROR(EFAULT);
  80. memset((char *)&handle, 0, sizeof(handle));
  81. switch (cmd) {
  82. case XFS_IOC_PATH_TO_FSHANDLE:
  83. case XFS_IOC_PATH_TO_HANDLE: {
  84. struct nameidata nd;
  85. int error;
  86. error = user_path_walk_link((const char __user *)hreq.path, &nd);
  87. if (error)
  88. return error;
  89. ASSERT(nd.dentry);
  90. ASSERT(nd.dentry->d_inode);
  91. inode = igrab(nd.dentry->d_inode);
  92. path_release(&nd);
  93. break;
  94. }
  95. case XFS_IOC_FD_TO_HANDLE: {
  96. struct file *file;
  97. file = fget(hreq.fd);
  98. if (!file)
  99. return -EBADF;
  100. ASSERT(file->f_dentry);
  101. ASSERT(file->f_dentry->d_inode);
  102. inode = igrab(file->f_dentry->d_inode);
  103. fput(file);
  104. break;
  105. }
  106. default:
  107. ASSERT(0);
  108. return -XFS_ERROR(EINVAL);
  109. }
  110. if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
  111. /* we're not in XFS anymore, Toto */
  112. iput(inode);
  113. return -XFS_ERROR(EINVAL);
  114. }
  115. switch (inode->i_mode & S_IFMT) {
  116. case S_IFREG:
  117. case S_IFDIR:
  118. case S_IFLNK:
  119. break;
  120. default:
  121. iput(inode);
  122. return -XFS_ERROR(EBADF);
  123. }
  124. /* we need the vnode */
  125. vp = vn_from_inode(inode);
  126. /* now we can grab the fsid */
  127. memcpy(&handle.ha_fsid, vp->v_vfsp->vfs_altfsid, sizeof(xfs_fsid_t));
  128. hsize = sizeof(xfs_fsid_t);
  129. if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
  130. xfs_inode_t *ip;
  131. int lock_mode;
  132. /* need to get access to the xfs_inode to read the generation */
  133. ip = xfs_vtoi(vp);
  134. ASSERT(ip);
  135. lock_mode = xfs_ilock_map_shared(ip);
  136. /* fill in fid section of handle from inode */
  137. handle.ha_fid.xfs_fid_len = sizeof(xfs_fid_t) -
  138. sizeof(handle.ha_fid.xfs_fid_len);
  139. handle.ha_fid.xfs_fid_pad = 0;
  140. handle.ha_fid.xfs_fid_gen = ip->i_d.di_gen;
  141. handle.ha_fid.xfs_fid_ino = ip->i_ino;
  142. xfs_iunlock_map_shared(ip, lock_mode);
  143. hsize = XFS_HSIZE(handle);
  144. }
  145. /* now copy our handle into the user buffer & write out the size */
  146. if (copy_to_user(hreq.ohandle, &handle, hsize) ||
  147. copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
  148. iput(inode);
  149. return -XFS_ERROR(EFAULT);
  150. }
  151. iput(inode);
  152. return 0;
  153. }
  154. /*
  155. * Convert userspace handle data into vnode (and inode).
  156. * We [ab]use the fact that all the fsop_handlereq ioctl calls
  157. * have a data structure argument whose first component is always
  158. * a xfs_fsop_handlereq_t, so we can cast to and from this type.
  159. * This allows us to optimise the copy_from_user calls and gives
  160. * a handy, shared routine.
  161. *
  162. * If no error, caller must always VN_RELE the returned vp.
  163. */
  164. STATIC int
  165. xfs_vget_fsop_handlereq(
  166. xfs_mount_t *mp,
  167. struct inode *parinode, /* parent inode pointer */
  168. xfs_fsop_handlereq_t *hreq,
  169. bhv_vnode_t **vp,
  170. struct inode **inode)
  171. {
  172. void __user *hanp;
  173. size_t hlen;
  174. xfs_fid_t *xfid;
  175. xfs_handle_t *handlep;
  176. xfs_handle_t handle;
  177. xfs_inode_t *ip;
  178. struct inode *inodep;
  179. bhv_vnode_t *vpp;
  180. xfs_ino_t ino;
  181. __u32 igen;
  182. int error;
  183. /*
  184. * Only allow handle opens under a directory.
  185. */
  186. if (!S_ISDIR(parinode->i_mode))
  187. return XFS_ERROR(ENOTDIR);
  188. hanp = hreq->ihandle;
  189. hlen = hreq->ihandlen;
  190. handlep = &handle;
  191. if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
  192. return XFS_ERROR(EINVAL);
  193. if (copy_from_user(handlep, hanp, hlen))
  194. return XFS_ERROR(EFAULT);
  195. if (hlen < sizeof(*handlep))
  196. memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
  197. if (hlen > sizeof(handlep->ha_fsid)) {
  198. if (handlep->ha_fid.xfs_fid_len !=
  199. (hlen - sizeof(handlep->ha_fsid)
  200. - sizeof(handlep->ha_fid.xfs_fid_len))
  201. || handlep->ha_fid.xfs_fid_pad)
  202. return XFS_ERROR(EINVAL);
  203. }
  204. /*
  205. * Crack the handle, obtain the inode # & generation #
  206. */
  207. xfid = (struct xfs_fid *)&handlep->ha_fid;
  208. if (xfid->xfs_fid_len == sizeof(*xfid) - sizeof(xfid->xfs_fid_len)) {
  209. ino = xfid->xfs_fid_ino;
  210. igen = xfid->xfs_fid_gen;
  211. } else {
  212. return XFS_ERROR(EINVAL);
  213. }
  214. /*
  215. * Get the XFS inode, building a vnode to go with it.
  216. */
  217. error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
  218. if (error)
  219. return error;
  220. if (ip == NULL)
  221. return XFS_ERROR(EIO);
  222. if (ip->i_d.di_mode == 0 || ip->i_d.di_gen != igen) {
  223. xfs_iput_new(ip, XFS_ILOCK_SHARED);
  224. return XFS_ERROR(ENOENT);
  225. }
  226. vpp = XFS_ITOV(ip);
  227. inodep = vn_to_inode(vpp);
  228. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  229. *vp = vpp;
  230. *inode = inodep;
  231. return 0;
  232. }
  233. STATIC int
  234. xfs_open_by_handle(
  235. xfs_mount_t *mp,
  236. void __user *arg,
  237. struct file *parfilp,
  238. struct inode *parinode)
  239. {
  240. int error;
  241. int new_fd;
  242. int permflag;
  243. struct file *filp;
  244. struct inode *inode;
  245. struct dentry *dentry;
  246. bhv_vnode_t *vp;
  247. xfs_fsop_handlereq_t hreq;
  248. if (!capable(CAP_SYS_ADMIN))
  249. return -XFS_ERROR(EPERM);
  250. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  251. return -XFS_ERROR(EFAULT);
  252. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
  253. if (error)
  254. return -error;
  255. /* Restrict xfs_open_by_handle to directories & regular files. */
  256. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  257. iput(inode);
  258. return -XFS_ERROR(EINVAL);
  259. }
  260. #if BITS_PER_LONG != 32
  261. hreq.oflags |= O_LARGEFILE;
  262. #endif
  263. /* Put open permission in namei format. */
  264. permflag = hreq.oflags;
  265. if ((permflag+1) & O_ACCMODE)
  266. permflag++;
  267. if (permflag & O_TRUNC)
  268. permflag |= 2;
  269. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  270. (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
  271. iput(inode);
  272. return -XFS_ERROR(EPERM);
  273. }
  274. if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  275. iput(inode);
  276. return -XFS_ERROR(EACCES);
  277. }
  278. /* Can't write directories. */
  279. if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
  280. iput(inode);
  281. return -XFS_ERROR(EISDIR);
  282. }
  283. if ((new_fd = get_unused_fd()) < 0) {
  284. iput(inode);
  285. return new_fd;
  286. }
  287. dentry = d_alloc_anon(inode);
  288. if (dentry == NULL) {
  289. iput(inode);
  290. put_unused_fd(new_fd);
  291. return -XFS_ERROR(ENOMEM);
  292. }
  293. /* Ensure umount returns EBUSY on umounts while this file is open. */
  294. mntget(parfilp->f_vfsmnt);
  295. /* Create file pointer. */
  296. filp = dentry_open(dentry, parfilp->f_vfsmnt, hreq.oflags);
  297. if (IS_ERR(filp)) {
  298. put_unused_fd(new_fd);
  299. return -XFS_ERROR(-PTR_ERR(filp));
  300. }
  301. if (inode->i_mode & S_IFREG)
  302. filp->f_op = &xfs_invis_file_operations;
  303. fd_install(new_fd, filp);
  304. return new_fd;
  305. }
  306. STATIC int
  307. xfs_readlink_by_handle(
  308. xfs_mount_t *mp,
  309. void __user *arg,
  310. struct file *parfilp,
  311. struct inode *parinode)
  312. {
  313. int error;
  314. struct iovec aiov;
  315. struct uio auio;
  316. struct inode *inode;
  317. xfs_fsop_handlereq_t hreq;
  318. bhv_vnode_t *vp;
  319. __u32 olen;
  320. if (!capable(CAP_SYS_ADMIN))
  321. return -XFS_ERROR(EPERM);
  322. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  323. return -XFS_ERROR(EFAULT);
  324. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
  325. if (error)
  326. return -error;
  327. /* Restrict this handle operation to symlinks only. */
  328. if (!S_ISLNK(inode->i_mode)) {
  329. VN_RELE(vp);
  330. return -XFS_ERROR(EINVAL);
  331. }
  332. if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
  333. VN_RELE(vp);
  334. return -XFS_ERROR(EFAULT);
  335. }
  336. aiov.iov_len = olen;
  337. aiov.iov_base = hreq.ohandle;
  338. auio.uio_iov = &aiov;
  339. auio.uio_iovcnt = 1;
  340. auio.uio_offset = 0;
  341. auio.uio_segflg = UIO_USERSPACE;
  342. auio.uio_resid = olen;
  343. error = bhv_vop_readlink(vp, &auio, IO_INVIS, NULL);
  344. VN_RELE(vp);
  345. if (error)
  346. return -error;
  347. return (olen - auio.uio_resid);
  348. }
  349. STATIC int
  350. xfs_fssetdm_by_handle(
  351. xfs_mount_t *mp,
  352. void __user *arg,
  353. struct file *parfilp,
  354. struct inode *parinode)
  355. {
  356. int error;
  357. struct fsdmidata fsd;
  358. xfs_fsop_setdm_handlereq_t dmhreq;
  359. struct inode *inode;
  360. bhv_desc_t *bdp;
  361. bhv_vnode_t *vp;
  362. if (!capable(CAP_MKNOD))
  363. return -XFS_ERROR(EPERM);
  364. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  365. return -XFS_ERROR(EFAULT);
  366. error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &vp, &inode);
  367. if (error)
  368. return -error;
  369. if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
  370. VN_RELE(vp);
  371. return -XFS_ERROR(EPERM);
  372. }
  373. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  374. VN_RELE(vp);
  375. return -XFS_ERROR(EFAULT);
  376. }
  377. bdp = bhv_base_unlocked(VN_BHV_HEAD(vp));
  378. error = xfs_set_dmattrs(bdp, fsd.fsd_dmevmask, fsd.fsd_dmstate, NULL);
  379. VN_RELE(vp);
  380. if (error)
  381. return -error;
  382. return 0;
  383. }
  384. STATIC int
  385. xfs_attrlist_by_handle(
  386. xfs_mount_t *mp,
  387. void __user *arg,
  388. struct file *parfilp,
  389. struct inode *parinode)
  390. {
  391. int error;
  392. attrlist_cursor_kern_t *cursor;
  393. xfs_fsop_attrlist_handlereq_t al_hreq;
  394. struct inode *inode;
  395. bhv_vnode_t *vp;
  396. char *kbuf;
  397. if (!capable(CAP_SYS_ADMIN))
  398. return -XFS_ERROR(EPERM);
  399. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  400. return -XFS_ERROR(EFAULT);
  401. if (al_hreq.buflen > XATTR_LIST_MAX)
  402. return -XFS_ERROR(EINVAL);
  403. error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq,
  404. &vp, &inode);
  405. if (error)
  406. goto out;
  407. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  408. if (!kbuf)
  409. goto out_vn_rele;
  410. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  411. error = bhv_vop_attr_list(vp, kbuf, al_hreq.buflen, al_hreq.flags,
  412. cursor, NULL);
  413. if (error)
  414. goto out_kfree;
  415. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  416. error = -EFAULT;
  417. out_kfree:
  418. kfree(kbuf);
  419. out_vn_rele:
  420. VN_RELE(vp);
  421. out:
  422. return -error;
  423. }
  424. STATIC int
  425. xfs_attrmulti_attr_get(
  426. bhv_vnode_t *vp,
  427. char *name,
  428. char __user *ubuf,
  429. __uint32_t *len,
  430. __uint32_t flags)
  431. {
  432. char *kbuf;
  433. int error = EFAULT;
  434. if (*len > XATTR_SIZE_MAX)
  435. return EINVAL;
  436. kbuf = kmalloc(*len, GFP_KERNEL);
  437. if (!kbuf)
  438. return ENOMEM;
  439. error = bhv_vop_attr_get(vp, name, kbuf, len, flags, NULL);
  440. if (error)
  441. goto out_kfree;
  442. if (copy_to_user(ubuf, kbuf, *len))
  443. error = EFAULT;
  444. out_kfree:
  445. kfree(kbuf);
  446. return error;
  447. }
  448. STATIC int
  449. xfs_attrmulti_attr_set(
  450. bhv_vnode_t *vp,
  451. char *name,
  452. const char __user *ubuf,
  453. __uint32_t len,
  454. __uint32_t flags)
  455. {
  456. char *kbuf;
  457. int error = EFAULT;
  458. if (IS_RDONLY(&vp->v_inode))
  459. return -EROFS;
  460. if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
  461. return EPERM;
  462. if (len > XATTR_SIZE_MAX)
  463. return EINVAL;
  464. kbuf = kmalloc(len, GFP_KERNEL);
  465. if (!kbuf)
  466. return ENOMEM;
  467. if (copy_from_user(kbuf, ubuf, len))
  468. goto out_kfree;
  469. error = bhv_vop_attr_set(vp, name, kbuf, len, flags, NULL);
  470. out_kfree:
  471. kfree(kbuf);
  472. return error;
  473. }
  474. STATIC int
  475. xfs_attrmulti_attr_remove(
  476. bhv_vnode_t *vp,
  477. char *name,
  478. __uint32_t flags)
  479. {
  480. if (IS_RDONLY(&vp->v_inode))
  481. return -EROFS;
  482. if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
  483. return EPERM;
  484. return bhv_vop_attr_remove(vp, name, flags, NULL);
  485. }
  486. STATIC int
  487. xfs_attrmulti_by_handle(
  488. xfs_mount_t *mp,
  489. void __user *arg,
  490. struct file *parfilp,
  491. struct inode *parinode)
  492. {
  493. int error;
  494. xfs_attr_multiop_t *ops;
  495. xfs_fsop_attrmulti_handlereq_t am_hreq;
  496. struct inode *inode;
  497. bhv_vnode_t *vp;
  498. unsigned int i, size;
  499. char *attr_name;
  500. if (!capable(CAP_SYS_ADMIN))
  501. return -XFS_ERROR(EPERM);
  502. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  503. return -XFS_ERROR(EFAULT);
  504. error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &vp, &inode);
  505. if (error)
  506. goto out;
  507. error = E2BIG;
  508. size = am_hreq.opcount * sizeof(attr_multiop_t);
  509. if (!size || size > 16 * PAGE_SIZE)
  510. goto out_vn_rele;
  511. error = ENOMEM;
  512. ops = kmalloc(size, GFP_KERNEL);
  513. if (!ops)
  514. goto out_vn_rele;
  515. error = EFAULT;
  516. if (copy_from_user(ops, am_hreq.ops, size))
  517. goto out_kfree_ops;
  518. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  519. if (!attr_name)
  520. goto out_kfree_ops;
  521. error = 0;
  522. for (i = 0; i < am_hreq.opcount; i++) {
  523. ops[i].am_error = strncpy_from_user(attr_name,
  524. ops[i].am_attrname, MAXNAMELEN);
  525. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  526. error = -ERANGE;
  527. if (ops[i].am_error < 0)
  528. break;
  529. switch (ops[i].am_opcode) {
  530. case ATTR_OP_GET:
  531. ops[i].am_error = xfs_attrmulti_attr_get(vp,
  532. attr_name, ops[i].am_attrvalue,
  533. &ops[i].am_length, ops[i].am_flags);
  534. break;
  535. case ATTR_OP_SET:
  536. ops[i].am_error = xfs_attrmulti_attr_set(vp,
  537. attr_name, ops[i].am_attrvalue,
  538. ops[i].am_length, ops[i].am_flags);
  539. break;
  540. case ATTR_OP_REMOVE:
  541. ops[i].am_error = xfs_attrmulti_attr_remove(vp,
  542. attr_name, ops[i].am_flags);
  543. break;
  544. default:
  545. ops[i].am_error = EINVAL;
  546. }
  547. }
  548. if (copy_to_user(am_hreq.ops, ops, size))
  549. error = XFS_ERROR(EFAULT);
  550. kfree(attr_name);
  551. out_kfree_ops:
  552. kfree(ops);
  553. out_vn_rele:
  554. VN_RELE(vp);
  555. out:
  556. return -error;
  557. }
  558. /* prototypes for a few of the stack-hungry cases that have
  559. * their own functions. Functions are defined after their use
  560. * so gcc doesn't get fancy and inline them with -03 */
  561. STATIC int
  562. xfs_ioc_space(
  563. bhv_desc_t *bdp,
  564. struct inode *inode,
  565. struct file *filp,
  566. int flags,
  567. unsigned int cmd,
  568. void __user *arg);
  569. STATIC int
  570. xfs_ioc_bulkstat(
  571. xfs_mount_t *mp,
  572. unsigned int cmd,
  573. void __user *arg);
  574. STATIC int
  575. xfs_ioc_fsgeometry_v1(
  576. xfs_mount_t *mp,
  577. void __user *arg);
  578. STATIC int
  579. xfs_ioc_fsgeometry(
  580. xfs_mount_t *mp,
  581. void __user *arg);
  582. STATIC int
  583. xfs_ioc_xattr(
  584. bhv_vnode_t *vp,
  585. xfs_inode_t *ip,
  586. struct file *filp,
  587. unsigned int cmd,
  588. void __user *arg);
  589. STATIC int
  590. xfs_ioc_getbmap(
  591. bhv_desc_t *bdp,
  592. struct file *filp,
  593. int flags,
  594. unsigned int cmd,
  595. void __user *arg);
  596. STATIC int
  597. xfs_ioc_getbmapx(
  598. bhv_desc_t *bdp,
  599. void __user *arg);
  600. int
  601. xfs_ioctl(
  602. bhv_desc_t *bdp,
  603. struct inode *inode,
  604. struct file *filp,
  605. int ioflags,
  606. unsigned int cmd,
  607. void __user *arg)
  608. {
  609. int error;
  610. bhv_vnode_t *vp;
  611. xfs_inode_t *ip;
  612. xfs_mount_t *mp;
  613. vp = vn_from_inode(inode);
  614. vn_trace_entry(vp, "xfs_ioctl", (inst_t *)__return_address);
  615. ip = XFS_BHVTOI(bdp);
  616. mp = ip->i_mount;
  617. switch (cmd) {
  618. case XFS_IOC_ALLOCSP:
  619. case XFS_IOC_FREESP:
  620. case XFS_IOC_RESVSP:
  621. case XFS_IOC_UNRESVSP:
  622. case XFS_IOC_ALLOCSP64:
  623. case XFS_IOC_FREESP64:
  624. case XFS_IOC_RESVSP64:
  625. case XFS_IOC_UNRESVSP64:
  626. /*
  627. * Only allow the sys admin to reserve space unless
  628. * unwritten extents are enabled.
  629. */
  630. if (!XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) &&
  631. !capable(CAP_SYS_ADMIN))
  632. return -EPERM;
  633. return xfs_ioc_space(bdp, inode, filp, ioflags, cmd, arg);
  634. case XFS_IOC_DIOINFO: {
  635. struct dioattr da;
  636. xfs_buftarg_t *target =
  637. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
  638. mp->m_rtdev_targp : mp->m_ddev_targp;
  639. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  640. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  641. if (copy_to_user(arg, &da, sizeof(da)))
  642. return -XFS_ERROR(EFAULT);
  643. return 0;
  644. }
  645. case XFS_IOC_FSBULKSTAT_SINGLE:
  646. case XFS_IOC_FSBULKSTAT:
  647. case XFS_IOC_FSINUMBERS:
  648. return xfs_ioc_bulkstat(mp, cmd, arg);
  649. case XFS_IOC_FSGEOMETRY_V1:
  650. return xfs_ioc_fsgeometry_v1(mp, arg);
  651. case XFS_IOC_FSGEOMETRY:
  652. return xfs_ioc_fsgeometry(mp, arg);
  653. case XFS_IOC_GETVERSION:
  654. return put_user(inode->i_generation, (int __user *)arg);
  655. case XFS_IOC_GETXFLAGS:
  656. case XFS_IOC_SETXFLAGS:
  657. case XFS_IOC_FSGETXATTR:
  658. case XFS_IOC_FSSETXATTR:
  659. case XFS_IOC_FSGETXATTRA:
  660. return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
  661. case XFS_IOC_FSSETDM: {
  662. struct fsdmidata dmi;
  663. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  664. return -XFS_ERROR(EFAULT);
  665. error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
  666. NULL);
  667. return -error;
  668. }
  669. case XFS_IOC_GETBMAP:
  670. case XFS_IOC_GETBMAPA:
  671. return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
  672. case XFS_IOC_GETBMAPX:
  673. return xfs_ioc_getbmapx(bdp, arg);
  674. case XFS_IOC_FD_TO_HANDLE:
  675. case XFS_IOC_PATH_TO_HANDLE:
  676. case XFS_IOC_PATH_TO_FSHANDLE:
  677. return xfs_find_handle(cmd, arg);
  678. case XFS_IOC_OPEN_BY_HANDLE:
  679. return xfs_open_by_handle(mp, arg, filp, inode);
  680. case XFS_IOC_FSSETDM_BY_HANDLE:
  681. return xfs_fssetdm_by_handle(mp, arg, filp, inode);
  682. case XFS_IOC_READLINK_BY_HANDLE:
  683. return xfs_readlink_by_handle(mp, arg, filp, inode);
  684. case XFS_IOC_ATTRLIST_BY_HANDLE:
  685. return xfs_attrlist_by_handle(mp, arg, filp, inode);
  686. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  687. return xfs_attrmulti_by_handle(mp, arg, filp, inode);
  688. case XFS_IOC_SWAPEXT: {
  689. error = xfs_swapext((struct xfs_swapext __user *)arg);
  690. return -error;
  691. }
  692. case XFS_IOC_FSCOUNTS: {
  693. xfs_fsop_counts_t out;
  694. error = xfs_fs_counts(mp, &out);
  695. if (error)
  696. return -error;
  697. if (copy_to_user(arg, &out, sizeof(out)))
  698. return -XFS_ERROR(EFAULT);
  699. return 0;
  700. }
  701. case XFS_IOC_SET_RESBLKS: {
  702. xfs_fsop_resblks_t inout;
  703. __uint64_t in;
  704. if (!capable(CAP_SYS_ADMIN))
  705. return -EPERM;
  706. if (copy_from_user(&inout, arg, sizeof(inout)))
  707. return -XFS_ERROR(EFAULT);
  708. /* input parameter is passed in resblks field of structure */
  709. in = inout.resblks;
  710. error = xfs_reserve_blocks(mp, &in, &inout);
  711. if (error)
  712. return -error;
  713. if (copy_to_user(arg, &inout, sizeof(inout)))
  714. return -XFS_ERROR(EFAULT);
  715. return 0;
  716. }
  717. case XFS_IOC_GET_RESBLKS: {
  718. xfs_fsop_resblks_t out;
  719. if (!capable(CAP_SYS_ADMIN))
  720. return -EPERM;
  721. error = xfs_reserve_blocks(mp, NULL, &out);
  722. if (error)
  723. return -error;
  724. if (copy_to_user(arg, &out, sizeof(out)))
  725. return -XFS_ERROR(EFAULT);
  726. return 0;
  727. }
  728. case XFS_IOC_FSGROWFSDATA: {
  729. xfs_growfs_data_t in;
  730. if (!capable(CAP_SYS_ADMIN))
  731. return -EPERM;
  732. if (copy_from_user(&in, arg, sizeof(in)))
  733. return -XFS_ERROR(EFAULT);
  734. error = xfs_growfs_data(mp, &in);
  735. return -error;
  736. }
  737. case XFS_IOC_FSGROWFSLOG: {
  738. xfs_growfs_log_t in;
  739. if (!capable(CAP_SYS_ADMIN))
  740. return -EPERM;
  741. if (copy_from_user(&in, arg, sizeof(in)))
  742. return -XFS_ERROR(EFAULT);
  743. error = xfs_growfs_log(mp, &in);
  744. return -error;
  745. }
  746. case XFS_IOC_FSGROWFSRT: {
  747. xfs_growfs_rt_t in;
  748. if (!capable(CAP_SYS_ADMIN))
  749. return -EPERM;
  750. if (copy_from_user(&in, arg, sizeof(in)))
  751. return -XFS_ERROR(EFAULT);
  752. error = xfs_growfs_rt(mp, &in);
  753. return -error;
  754. }
  755. case XFS_IOC_FREEZE:
  756. if (!capable(CAP_SYS_ADMIN))
  757. return -EPERM;
  758. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  759. freeze_bdev(inode->i_sb->s_bdev);
  760. return 0;
  761. case XFS_IOC_THAW:
  762. if (!capable(CAP_SYS_ADMIN))
  763. return -EPERM;
  764. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  765. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  766. return 0;
  767. case XFS_IOC_GOINGDOWN: {
  768. __uint32_t in;
  769. if (!capable(CAP_SYS_ADMIN))
  770. return -EPERM;
  771. if (get_user(in, (__uint32_t __user *)arg))
  772. return -XFS_ERROR(EFAULT);
  773. error = xfs_fs_goingdown(mp, in);
  774. return -error;
  775. }
  776. case XFS_IOC_ERROR_INJECTION: {
  777. xfs_error_injection_t in;
  778. if (!capable(CAP_SYS_ADMIN))
  779. return -EPERM;
  780. if (copy_from_user(&in, arg, sizeof(in)))
  781. return -XFS_ERROR(EFAULT);
  782. error = xfs_errortag_add(in.errtag, mp);
  783. return -error;
  784. }
  785. case XFS_IOC_ERROR_CLEARALL:
  786. if (!capable(CAP_SYS_ADMIN))
  787. return -EPERM;
  788. error = xfs_errortag_clearall(mp);
  789. return -error;
  790. default:
  791. return -ENOTTY;
  792. }
  793. }
  794. STATIC int
  795. xfs_ioc_space(
  796. bhv_desc_t *bdp,
  797. struct inode *inode,
  798. struct file *filp,
  799. int ioflags,
  800. unsigned int cmd,
  801. void __user *arg)
  802. {
  803. xfs_flock64_t bf;
  804. int attr_flags = 0;
  805. int error;
  806. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  807. return -XFS_ERROR(EPERM);
  808. if (!(filp->f_mode & FMODE_WRITE))
  809. return -XFS_ERROR(EBADF);
  810. if (!S_ISREG(inode->i_mode))
  811. return -XFS_ERROR(EINVAL);
  812. if (copy_from_user(&bf, arg, sizeof(bf)))
  813. return -XFS_ERROR(EFAULT);
  814. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  815. attr_flags |= ATTR_NONBLOCK;
  816. if (ioflags & IO_INVIS)
  817. attr_flags |= ATTR_DMI;
  818. error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
  819. NULL, attr_flags);
  820. return -error;
  821. }
  822. STATIC int
  823. xfs_ioc_bulkstat(
  824. xfs_mount_t *mp,
  825. unsigned int cmd,
  826. void __user *arg)
  827. {
  828. xfs_fsop_bulkreq_t bulkreq;
  829. int count; /* # of records returned */
  830. xfs_ino_t inlast; /* last inode number */
  831. int done;
  832. int error;
  833. /* done = 1 if there are more stats to get and if bulkstat */
  834. /* should be called again (unused here, but used in dmapi) */
  835. if (!capable(CAP_SYS_ADMIN))
  836. return -EPERM;
  837. if (XFS_FORCED_SHUTDOWN(mp))
  838. return -XFS_ERROR(EIO);
  839. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  840. return -XFS_ERROR(EFAULT);
  841. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  842. return -XFS_ERROR(EFAULT);
  843. if ((count = bulkreq.icount) <= 0)
  844. return -XFS_ERROR(EINVAL);
  845. if (cmd == XFS_IOC_FSINUMBERS)
  846. error = xfs_inumbers(mp, &inlast, &count,
  847. bulkreq.ubuffer);
  848. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  849. error = xfs_bulkstat_single(mp, &inlast,
  850. bulkreq.ubuffer, &done);
  851. else { /* XFS_IOC_FSBULKSTAT */
  852. if (count == 1 && inlast != 0) {
  853. inlast++;
  854. error = xfs_bulkstat_single(mp, &inlast,
  855. bulkreq.ubuffer, &done);
  856. } else {
  857. error = xfs_bulkstat(mp, &inlast, &count,
  858. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  859. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  860. BULKSTAT_FG_QUICK, &done);
  861. }
  862. }
  863. if (error)
  864. return -error;
  865. if (bulkreq.ocount != NULL) {
  866. if (copy_to_user(bulkreq.lastip, &inlast,
  867. sizeof(xfs_ino_t)))
  868. return -XFS_ERROR(EFAULT);
  869. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  870. return -XFS_ERROR(EFAULT);
  871. }
  872. return 0;
  873. }
  874. STATIC int
  875. xfs_ioc_fsgeometry_v1(
  876. xfs_mount_t *mp,
  877. void __user *arg)
  878. {
  879. xfs_fsop_geom_v1_t fsgeo;
  880. int error;
  881. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  882. if (error)
  883. return -error;
  884. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  885. return -XFS_ERROR(EFAULT);
  886. return 0;
  887. }
  888. STATIC int
  889. xfs_ioc_fsgeometry(
  890. xfs_mount_t *mp,
  891. void __user *arg)
  892. {
  893. xfs_fsop_geom_t fsgeo;
  894. int error;
  895. error = xfs_fs_geometry(mp, &fsgeo, 4);
  896. if (error)
  897. return -error;
  898. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  899. return -XFS_ERROR(EFAULT);
  900. return 0;
  901. }
  902. /*
  903. * Linux extended inode flags interface.
  904. */
  905. #define LINUX_XFLAG_SYNC 0x00000008 /* Synchronous updates */
  906. #define LINUX_XFLAG_IMMUTABLE 0x00000010 /* Immutable file */
  907. #define LINUX_XFLAG_APPEND 0x00000020 /* writes to file may only append */
  908. #define LINUX_XFLAG_NODUMP 0x00000040 /* do not dump file */
  909. #define LINUX_XFLAG_NOATIME 0x00000080 /* do not update atime */
  910. STATIC unsigned int
  911. xfs_merge_ioc_xflags(
  912. unsigned int flags,
  913. unsigned int start)
  914. {
  915. unsigned int xflags = start;
  916. if (flags & LINUX_XFLAG_IMMUTABLE)
  917. xflags |= XFS_XFLAG_IMMUTABLE;
  918. else
  919. xflags &= ~XFS_XFLAG_IMMUTABLE;
  920. if (flags & LINUX_XFLAG_APPEND)
  921. xflags |= XFS_XFLAG_APPEND;
  922. else
  923. xflags &= ~XFS_XFLAG_APPEND;
  924. if (flags & LINUX_XFLAG_SYNC)
  925. xflags |= XFS_XFLAG_SYNC;
  926. else
  927. xflags &= ~XFS_XFLAG_SYNC;
  928. if (flags & LINUX_XFLAG_NOATIME)
  929. xflags |= XFS_XFLAG_NOATIME;
  930. else
  931. xflags &= ~XFS_XFLAG_NOATIME;
  932. if (flags & LINUX_XFLAG_NODUMP)
  933. xflags |= XFS_XFLAG_NODUMP;
  934. else
  935. xflags &= ~XFS_XFLAG_NODUMP;
  936. return xflags;
  937. }
  938. STATIC unsigned int
  939. xfs_di2lxflags(
  940. __uint16_t di_flags)
  941. {
  942. unsigned int flags = 0;
  943. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  944. flags |= LINUX_XFLAG_IMMUTABLE;
  945. if (di_flags & XFS_DIFLAG_APPEND)
  946. flags |= LINUX_XFLAG_APPEND;
  947. if (di_flags & XFS_DIFLAG_SYNC)
  948. flags |= LINUX_XFLAG_SYNC;
  949. if (di_flags & XFS_DIFLAG_NOATIME)
  950. flags |= LINUX_XFLAG_NOATIME;
  951. if (di_flags & XFS_DIFLAG_NODUMP)
  952. flags |= LINUX_XFLAG_NODUMP;
  953. return flags;
  954. }
  955. STATIC int
  956. xfs_ioc_xattr(
  957. bhv_vnode_t *vp,
  958. xfs_inode_t *ip,
  959. struct file *filp,
  960. unsigned int cmd,
  961. void __user *arg)
  962. {
  963. struct fsxattr fa;
  964. struct bhv_vattr *vattr;
  965. int error = 0;
  966. int attr_flags;
  967. unsigned int flags;
  968. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  969. if (unlikely(!vattr))
  970. return -ENOMEM;
  971. switch (cmd) {
  972. case XFS_IOC_FSGETXATTR: {
  973. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
  974. XFS_AT_NEXTENTS | XFS_AT_PROJID;
  975. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  976. if (unlikely(error)) {
  977. error = -error;
  978. break;
  979. }
  980. fa.fsx_xflags = vattr->va_xflags;
  981. fa.fsx_extsize = vattr->va_extsize;
  982. fa.fsx_nextents = vattr->va_nextents;
  983. fa.fsx_projid = vattr->va_projid;
  984. if (copy_to_user(arg, &fa, sizeof(fa))) {
  985. error = -EFAULT;
  986. break;
  987. }
  988. break;
  989. }
  990. case XFS_IOC_FSSETXATTR: {
  991. if (copy_from_user(&fa, arg, sizeof(fa))) {
  992. error = -EFAULT;
  993. break;
  994. }
  995. attr_flags = 0;
  996. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  997. attr_flags |= ATTR_NONBLOCK;
  998. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
  999. vattr->va_xflags = fa.fsx_xflags;
  1000. vattr->va_extsize = fa.fsx_extsize;
  1001. vattr->va_projid = fa.fsx_projid;
  1002. error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
  1003. if (likely(!error))
  1004. __vn_revalidate(vp, vattr); /* update flags */
  1005. error = -error;
  1006. break;
  1007. }
  1008. case XFS_IOC_FSGETXATTRA: {
  1009. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
  1010. XFS_AT_ANEXTENTS | XFS_AT_PROJID;
  1011. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  1012. if (unlikely(error)) {
  1013. error = -error;
  1014. break;
  1015. }
  1016. fa.fsx_xflags = vattr->va_xflags;
  1017. fa.fsx_extsize = vattr->va_extsize;
  1018. fa.fsx_nextents = vattr->va_anextents;
  1019. fa.fsx_projid = vattr->va_projid;
  1020. if (copy_to_user(arg, &fa, sizeof(fa))) {
  1021. error = -EFAULT;
  1022. break;
  1023. }
  1024. break;
  1025. }
  1026. case XFS_IOC_GETXFLAGS: {
  1027. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1028. if (copy_to_user(arg, &flags, sizeof(flags)))
  1029. error = -EFAULT;
  1030. break;
  1031. }
  1032. case XFS_IOC_SETXFLAGS: {
  1033. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1034. error = -EFAULT;
  1035. break;
  1036. }
  1037. if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
  1038. LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
  1039. LINUX_XFLAG_SYNC)) {
  1040. error = -EOPNOTSUPP;
  1041. break;
  1042. }
  1043. attr_flags = 0;
  1044. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1045. attr_flags |= ATTR_NONBLOCK;
  1046. vattr->va_mask = XFS_AT_XFLAGS;
  1047. vattr->va_xflags = xfs_merge_ioc_xflags(flags,
  1048. xfs_ip2xflags(ip));
  1049. error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
  1050. if (likely(!error))
  1051. __vn_revalidate(vp, vattr); /* update flags */
  1052. error = -error;
  1053. break;
  1054. }
  1055. default:
  1056. error = -ENOTTY;
  1057. break;
  1058. }
  1059. kfree(vattr);
  1060. return error;
  1061. }
  1062. STATIC int
  1063. xfs_ioc_getbmap(
  1064. bhv_desc_t *bdp,
  1065. struct file *filp,
  1066. int ioflags,
  1067. unsigned int cmd,
  1068. void __user *arg)
  1069. {
  1070. struct getbmap bm;
  1071. int iflags;
  1072. int error;
  1073. if (copy_from_user(&bm, arg, sizeof(bm)))
  1074. return -XFS_ERROR(EFAULT);
  1075. if (bm.bmv_count < 2)
  1076. return -XFS_ERROR(EINVAL);
  1077. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1078. if (ioflags & IO_INVIS)
  1079. iflags |= BMV_IF_NO_DMAPI_READ;
  1080. error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
  1081. if (error)
  1082. return -error;
  1083. if (copy_to_user(arg, &bm, sizeof(bm)))
  1084. return -XFS_ERROR(EFAULT);
  1085. return 0;
  1086. }
  1087. STATIC int
  1088. xfs_ioc_getbmapx(
  1089. bhv_desc_t *bdp,
  1090. void __user *arg)
  1091. {
  1092. struct getbmapx bmx;
  1093. struct getbmap bm;
  1094. int iflags;
  1095. int error;
  1096. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1097. return -XFS_ERROR(EFAULT);
  1098. if (bmx.bmv_count < 2)
  1099. return -XFS_ERROR(EINVAL);
  1100. /*
  1101. * Map input getbmapx structure to a getbmap
  1102. * structure for xfs_getbmap.
  1103. */
  1104. GETBMAP_CONVERT(bmx, bm);
  1105. iflags = bmx.bmv_iflags;
  1106. if (iflags & (~BMV_IF_VALID))
  1107. return -XFS_ERROR(EINVAL);
  1108. iflags |= BMV_IF_EXTENDED;
  1109. error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
  1110. if (error)
  1111. return -error;
  1112. GETBMAP_CONVERT(bm, bmx);
  1113. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  1114. return -XFS_ERROR(EFAULT);
  1115. return 0;
  1116. }