xfs_ioctl.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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_path.dentry);
  101. ASSERT(file->f_path.dentry->d_inode);
  102. inode = igrab(file->f_path.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_path.mnt);
  295. /* Create file pointer. */
  296. filp = dentry_open(dentry, parfilp->f_path.mnt, 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. /* invisible operation should not change atime */
  303. filp->f_flags |= O_NOATIME;
  304. filp->f_op = &xfs_invis_file_operations;
  305. }
  306. fd_install(new_fd, filp);
  307. return new_fd;
  308. }
  309. STATIC int
  310. xfs_readlink_by_handle(
  311. xfs_mount_t *mp,
  312. void __user *arg,
  313. struct file *parfilp,
  314. struct inode *parinode)
  315. {
  316. int error;
  317. struct iovec aiov;
  318. struct uio auio;
  319. struct inode *inode;
  320. xfs_fsop_handlereq_t hreq;
  321. bhv_vnode_t *vp;
  322. __u32 olen;
  323. if (!capable(CAP_SYS_ADMIN))
  324. return -XFS_ERROR(EPERM);
  325. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  326. return -XFS_ERROR(EFAULT);
  327. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &vp, &inode);
  328. if (error)
  329. return -error;
  330. /* Restrict this handle operation to symlinks only. */
  331. if (!S_ISLNK(inode->i_mode)) {
  332. VN_RELE(vp);
  333. return -XFS_ERROR(EINVAL);
  334. }
  335. if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
  336. VN_RELE(vp);
  337. return -XFS_ERROR(EFAULT);
  338. }
  339. aiov.iov_len = olen;
  340. aiov.iov_base = hreq.ohandle;
  341. auio.uio_iov = &aiov;
  342. auio.uio_iovcnt = 1;
  343. auio.uio_offset = 0;
  344. auio.uio_segflg = UIO_USERSPACE;
  345. auio.uio_resid = olen;
  346. error = bhv_vop_readlink(vp, &auio, IO_INVIS, NULL);
  347. VN_RELE(vp);
  348. if (error)
  349. return -error;
  350. return (olen - auio.uio_resid);
  351. }
  352. STATIC int
  353. xfs_fssetdm_by_handle(
  354. xfs_mount_t *mp,
  355. void __user *arg,
  356. struct file *parfilp,
  357. struct inode *parinode)
  358. {
  359. int error;
  360. struct fsdmidata fsd;
  361. xfs_fsop_setdm_handlereq_t dmhreq;
  362. struct inode *inode;
  363. bhv_desc_t *bdp;
  364. bhv_vnode_t *vp;
  365. if (!capable(CAP_MKNOD))
  366. return -XFS_ERROR(EPERM);
  367. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  368. return -XFS_ERROR(EFAULT);
  369. error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &vp, &inode);
  370. if (error)
  371. return -error;
  372. if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
  373. VN_RELE(vp);
  374. return -XFS_ERROR(EPERM);
  375. }
  376. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  377. VN_RELE(vp);
  378. return -XFS_ERROR(EFAULT);
  379. }
  380. bdp = bhv_base_unlocked(VN_BHV_HEAD(vp));
  381. error = xfs_set_dmattrs(bdp, fsd.fsd_dmevmask, fsd.fsd_dmstate, NULL);
  382. VN_RELE(vp);
  383. if (error)
  384. return -error;
  385. return 0;
  386. }
  387. STATIC int
  388. xfs_attrlist_by_handle(
  389. xfs_mount_t *mp,
  390. void __user *arg,
  391. struct file *parfilp,
  392. struct inode *parinode)
  393. {
  394. int error;
  395. attrlist_cursor_kern_t *cursor;
  396. xfs_fsop_attrlist_handlereq_t al_hreq;
  397. struct inode *inode;
  398. bhv_vnode_t *vp;
  399. char *kbuf;
  400. if (!capable(CAP_SYS_ADMIN))
  401. return -XFS_ERROR(EPERM);
  402. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  403. return -XFS_ERROR(EFAULT);
  404. if (al_hreq.buflen > XATTR_LIST_MAX)
  405. return -XFS_ERROR(EINVAL);
  406. error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq,
  407. &vp, &inode);
  408. if (error)
  409. goto out;
  410. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  411. if (!kbuf)
  412. goto out_vn_rele;
  413. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  414. error = bhv_vop_attr_list(vp, kbuf, al_hreq.buflen, al_hreq.flags,
  415. cursor, NULL);
  416. if (error)
  417. goto out_kfree;
  418. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  419. error = -EFAULT;
  420. out_kfree:
  421. kfree(kbuf);
  422. out_vn_rele:
  423. VN_RELE(vp);
  424. out:
  425. return -error;
  426. }
  427. STATIC int
  428. xfs_attrmulti_attr_get(
  429. bhv_vnode_t *vp,
  430. char *name,
  431. char __user *ubuf,
  432. __uint32_t *len,
  433. __uint32_t flags)
  434. {
  435. char *kbuf;
  436. int error = EFAULT;
  437. if (*len > XATTR_SIZE_MAX)
  438. return EINVAL;
  439. kbuf = kmalloc(*len, GFP_KERNEL);
  440. if (!kbuf)
  441. return ENOMEM;
  442. error = bhv_vop_attr_get(vp, name, kbuf, len, flags, NULL);
  443. if (error)
  444. goto out_kfree;
  445. if (copy_to_user(ubuf, kbuf, *len))
  446. error = EFAULT;
  447. out_kfree:
  448. kfree(kbuf);
  449. return error;
  450. }
  451. STATIC int
  452. xfs_attrmulti_attr_set(
  453. bhv_vnode_t *vp,
  454. char *name,
  455. const char __user *ubuf,
  456. __uint32_t len,
  457. __uint32_t flags)
  458. {
  459. char *kbuf;
  460. int error = EFAULT;
  461. if (IS_RDONLY(&vp->v_inode))
  462. return -EROFS;
  463. if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
  464. return EPERM;
  465. if (len > XATTR_SIZE_MAX)
  466. return EINVAL;
  467. kbuf = kmalloc(len, GFP_KERNEL);
  468. if (!kbuf)
  469. return ENOMEM;
  470. if (copy_from_user(kbuf, ubuf, len))
  471. goto out_kfree;
  472. error = bhv_vop_attr_set(vp, name, kbuf, len, flags, NULL);
  473. out_kfree:
  474. kfree(kbuf);
  475. return error;
  476. }
  477. STATIC int
  478. xfs_attrmulti_attr_remove(
  479. bhv_vnode_t *vp,
  480. char *name,
  481. __uint32_t flags)
  482. {
  483. if (IS_RDONLY(&vp->v_inode))
  484. return -EROFS;
  485. if (IS_IMMUTABLE(&vp->v_inode) || IS_APPEND(&vp->v_inode))
  486. return EPERM;
  487. return bhv_vop_attr_remove(vp, name, flags, NULL);
  488. }
  489. STATIC int
  490. xfs_attrmulti_by_handle(
  491. xfs_mount_t *mp,
  492. void __user *arg,
  493. struct file *parfilp,
  494. struct inode *parinode)
  495. {
  496. int error;
  497. xfs_attr_multiop_t *ops;
  498. xfs_fsop_attrmulti_handlereq_t am_hreq;
  499. struct inode *inode;
  500. bhv_vnode_t *vp;
  501. unsigned int i, size;
  502. char *attr_name;
  503. if (!capable(CAP_SYS_ADMIN))
  504. return -XFS_ERROR(EPERM);
  505. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  506. return -XFS_ERROR(EFAULT);
  507. error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &vp, &inode);
  508. if (error)
  509. goto out;
  510. error = E2BIG;
  511. size = am_hreq.opcount * sizeof(attr_multiop_t);
  512. if (!size || size > 16 * PAGE_SIZE)
  513. goto out_vn_rele;
  514. error = ENOMEM;
  515. ops = kmalloc(size, GFP_KERNEL);
  516. if (!ops)
  517. goto out_vn_rele;
  518. error = EFAULT;
  519. if (copy_from_user(ops, am_hreq.ops, size))
  520. goto out_kfree_ops;
  521. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  522. if (!attr_name)
  523. goto out_kfree_ops;
  524. error = 0;
  525. for (i = 0; i < am_hreq.opcount; i++) {
  526. ops[i].am_error = strncpy_from_user(attr_name,
  527. ops[i].am_attrname, MAXNAMELEN);
  528. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  529. error = -ERANGE;
  530. if (ops[i].am_error < 0)
  531. break;
  532. switch (ops[i].am_opcode) {
  533. case ATTR_OP_GET:
  534. ops[i].am_error = xfs_attrmulti_attr_get(vp,
  535. attr_name, ops[i].am_attrvalue,
  536. &ops[i].am_length, ops[i].am_flags);
  537. break;
  538. case ATTR_OP_SET:
  539. ops[i].am_error = xfs_attrmulti_attr_set(vp,
  540. attr_name, ops[i].am_attrvalue,
  541. ops[i].am_length, ops[i].am_flags);
  542. break;
  543. case ATTR_OP_REMOVE:
  544. ops[i].am_error = xfs_attrmulti_attr_remove(vp,
  545. attr_name, ops[i].am_flags);
  546. break;
  547. default:
  548. ops[i].am_error = EINVAL;
  549. }
  550. }
  551. if (copy_to_user(am_hreq.ops, ops, size))
  552. error = XFS_ERROR(EFAULT);
  553. kfree(attr_name);
  554. out_kfree_ops:
  555. kfree(ops);
  556. out_vn_rele:
  557. VN_RELE(vp);
  558. out:
  559. return -error;
  560. }
  561. /* prototypes for a few of the stack-hungry cases that have
  562. * their own functions. Functions are defined after their use
  563. * so gcc doesn't get fancy and inline them with -03 */
  564. STATIC int
  565. xfs_ioc_space(
  566. bhv_desc_t *bdp,
  567. struct inode *inode,
  568. struct file *filp,
  569. int flags,
  570. unsigned int cmd,
  571. void __user *arg);
  572. STATIC int
  573. xfs_ioc_bulkstat(
  574. xfs_mount_t *mp,
  575. unsigned int cmd,
  576. void __user *arg);
  577. STATIC int
  578. xfs_ioc_fsgeometry_v1(
  579. xfs_mount_t *mp,
  580. void __user *arg);
  581. STATIC int
  582. xfs_ioc_fsgeometry(
  583. xfs_mount_t *mp,
  584. void __user *arg);
  585. STATIC int
  586. xfs_ioc_xattr(
  587. bhv_vnode_t *vp,
  588. xfs_inode_t *ip,
  589. struct file *filp,
  590. unsigned int cmd,
  591. void __user *arg);
  592. STATIC int
  593. xfs_ioc_getbmap(
  594. bhv_desc_t *bdp,
  595. struct file *filp,
  596. int flags,
  597. unsigned int cmd,
  598. void __user *arg);
  599. STATIC int
  600. xfs_ioc_getbmapx(
  601. bhv_desc_t *bdp,
  602. void __user *arg);
  603. int
  604. xfs_ioctl(
  605. bhv_desc_t *bdp,
  606. struct inode *inode,
  607. struct file *filp,
  608. int ioflags,
  609. unsigned int cmd,
  610. void __user *arg)
  611. {
  612. int error;
  613. bhv_vnode_t *vp;
  614. xfs_inode_t *ip;
  615. xfs_mount_t *mp;
  616. vp = vn_from_inode(inode);
  617. vn_trace_entry(vp, "xfs_ioctl", (inst_t *)__return_address);
  618. ip = XFS_BHVTOI(bdp);
  619. mp = ip->i_mount;
  620. switch (cmd) {
  621. case XFS_IOC_ALLOCSP:
  622. case XFS_IOC_FREESP:
  623. case XFS_IOC_RESVSP:
  624. case XFS_IOC_UNRESVSP:
  625. case XFS_IOC_ALLOCSP64:
  626. case XFS_IOC_FREESP64:
  627. case XFS_IOC_RESVSP64:
  628. case XFS_IOC_UNRESVSP64:
  629. /*
  630. * Only allow the sys admin to reserve space unless
  631. * unwritten extents are enabled.
  632. */
  633. if (!XFS_SB_VERSION_HASEXTFLGBIT(&mp->m_sb) &&
  634. !capable(CAP_SYS_ADMIN))
  635. return -EPERM;
  636. return xfs_ioc_space(bdp, inode, filp, ioflags, cmd, arg);
  637. case XFS_IOC_DIOINFO: {
  638. struct dioattr da;
  639. xfs_buftarg_t *target =
  640. (ip->i_d.di_flags & XFS_DIFLAG_REALTIME) ?
  641. mp->m_rtdev_targp : mp->m_ddev_targp;
  642. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  643. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  644. if (copy_to_user(arg, &da, sizeof(da)))
  645. return -XFS_ERROR(EFAULT);
  646. return 0;
  647. }
  648. case XFS_IOC_FSBULKSTAT_SINGLE:
  649. case XFS_IOC_FSBULKSTAT:
  650. case XFS_IOC_FSINUMBERS:
  651. return xfs_ioc_bulkstat(mp, cmd, arg);
  652. case XFS_IOC_FSGEOMETRY_V1:
  653. return xfs_ioc_fsgeometry_v1(mp, arg);
  654. case XFS_IOC_FSGEOMETRY:
  655. return xfs_ioc_fsgeometry(mp, arg);
  656. case XFS_IOC_GETVERSION:
  657. return put_user(inode->i_generation, (int __user *)arg);
  658. case XFS_IOC_GETXFLAGS:
  659. case XFS_IOC_SETXFLAGS:
  660. case XFS_IOC_FSGETXATTR:
  661. case XFS_IOC_FSSETXATTR:
  662. case XFS_IOC_FSGETXATTRA:
  663. return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
  664. case XFS_IOC_FSSETDM: {
  665. struct fsdmidata dmi;
  666. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  667. return -XFS_ERROR(EFAULT);
  668. error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
  669. NULL);
  670. return -error;
  671. }
  672. case XFS_IOC_GETBMAP:
  673. case XFS_IOC_GETBMAPA:
  674. return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
  675. case XFS_IOC_GETBMAPX:
  676. return xfs_ioc_getbmapx(bdp, arg);
  677. case XFS_IOC_FD_TO_HANDLE:
  678. case XFS_IOC_PATH_TO_HANDLE:
  679. case XFS_IOC_PATH_TO_FSHANDLE:
  680. return xfs_find_handle(cmd, arg);
  681. case XFS_IOC_OPEN_BY_HANDLE:
  682. return xfs_open_by_handle(mp, arg, filp, inode);
  683. case XFS_IOC_FSSETDM_BY_HANDLE:
  684. return xfs_fssetdm_by_handle(mp, arg, filp, inode);
  685. case XFS_IOC_READLINK_BY_HANDLE:
  686. return xfs_readlink_by_handle(mp, arg, filp, inode);
  687. case XFS_IOC_ATTRLIST_BY_HANDLE:
  688. return xfs_attrlist_by_handle(mp, arg, filp, inode);
  689. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  690. return xfs_attrmulti_by_handle(mp, arg, filp, inode);
  691. case XFS_IOC_SWAPEXT: {
  692. error = xfs_swapext((struct xfs_swapext __user *)arg);
  693. return -error;
  694. }
  695. case XFS_IOC_FSCOUNTS: {
  696. xfs_fsop_counts_t out;
  697. error = xfs_fs_counts(mp, &out);
  698. if (error)
  699. return -error;
  700. if (copy_to_user(arg, &out, sizeof(out)))
  701. return -XFS_ERROR(EFAULT);
  702. return 0;
  703. }
  704. case XFS_IOC_SET_RESBLKS: {
  705. xfs_fsop_resblks_t inout;
  706. __uint64_t in;
  707. if (!capable(CAP_SYS_ADMIN))
  708. return -EPERM;
  709. if (copy_from_user(&inout, arg, sizeof(inout)))
  710. return -XFS_ERROR(EFAULT);
  711. /* input parameter is passed in resblks field of structure */
  712. in = inout.resblks;
  713. error = xfs_reserve_blocks(mp, &in, &inout);
  714. if (error)
  715. return -error;
  716. if (copy_to_user(arg, &inout, sizeof(inout)))
  717. return -XFS_ERROR(EFAULT);
  718. return 0;
  719. }
  720. case XFS_IOC_GET_RESBLKS: {
  721. xfs_fsop_resblks_t out;
  722. if (!capable(CAP_SYS_ADMIN))
  723. return -EPERM;
  724. error = xfs_reserve_blocks(mp, NULL, &out);
  725. if (error)
  726. return -error;
  727. if (copy_to_user(arg, &out, sizeof(out)))
  728. return -XFS_ERROR(EFAULT);
  729. return 0;
  730. }
  731. case XFS_IOC_FSGROWFSDATA: {
  732. xfs_growfs_data_t in;
  733. if (!capable(CAP_SYS_ADMIN))
  734. return -EPERM;
  735. if (copy_from_user(&in, arg, sizeof(in)))
  736. return -XFS_ERROR(EFAULT);
  737. error = xfs_growfs_data(mp, &in);
  738. return -error;
  739. }
  740. case XFS_IOC_FSGROWFSLOG: {
  741. xfs_growfs_log_t in;
  742. if (!capable(CAP_SYS_ADMIN))
  743. return -EPERM;
  744. if (copy_from_user(&in, arg, sizeof(in)))
  745. return -XFS_ERROR(EFAULT);
  746. error = xfs_growfs_log(mp, &in);
  747. return -error;
  748. }
  749. case XFS_IOC_FSGROWFSRT: {
  750. xfs_growfs_rt_t in;
  751. if (!capable(CAP_SYS_ADMIN))
  752. return -EPERM;
  753. if (copy_from_user(&in, arg, sizeof(in)))
  754. return -XFS_ERROR(EFAULT);
  755. error = xfs_growfs_rt(mp, &in);
  756. return -error;
  757. }
  758. case XFS_IOC_FREEZE:
  759. if (!capable(CAP_SYS_ADMIN))
  760. return -EPERM;
  761. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  762. freeze_bdev(inode->i_sb->s_bdev);
  763. return 0;
  764. case XFS_IOC_THAW:
  765. if (!capable(CAP_SYS_ADMIN))
  766. return -EPERM;
  767. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  768. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  769. return 0;
  770. case XFS_IOC_GOINGDOWN: {
  771. __uint32_t in;
  772. if (!capable(CAP_SYS_ADMIN))
  773. return -EPERM;
  774. if (get_user(in, (__uint32_t __user *)arg))
  775. return -XFS_ERROR(EFAULT);
  776. error = xfs_fs_goingdown(mp, in);
  777. return -error;
  778. }
  779. case XFS_IOC_ERROR_INJECTION: {
  780. xfs_error_injection_t in;
  781. if (!capable(CAP_SYS_ADMIN))
  782. return -EPERM;
  783. if (copy_from_user(&in, arg, sizeof(in)))
  784. return -XFS_ERROR(EFAULT);
  785. error = xfs_errortag_add(in.errtag, mp);
  786. return -error;
  787. }
  788. case XFS_IOC_ERROR_CLEARALL:
  789. if (!capable(CAP_SYS_ADMIN))
  790. return -EPERM;
  791. error = xfs_errortag_clearall(mp);
  792. return -error;
  793. default:
  794. return -ENOTTY;
  795. }
  796. }
  797. STATIC int
  798. xfs_ioc_space(
  799. bhv_desc_t *bdp,
  800. struct inode *inode,
  801. struct file *filp,
  802. int ioflags,
  803. unsigned int cmd,
  804. void __user *arg)
  805. {
  806. xfs_flock64_t bf;
  807. int attr_flags = 0;
  808. int error;
  809. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  810. return -XFS_ERROR(EPERM);
  811. if (!(filp->f_mode & FMODE_WRITE))
  812. return -XFS_ERROR(EBADF);
  813. if (!S_ISREG(inode->i_mode))
  814. return -XFS_ERROR(EINVAL);
  815. if (copy_from_user(&bf, arg, sizeof(bf)))
  816. return -XFS_ERROR(EFAULT);
  817. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  818. attr_flags |= ATTR_NONBLOCK;
  819. if (ioflags & IO_INVIS)
  820. attr_flags |= ATTR_DMI;
  821. error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
  822. NULL, attr_flags);
  823. return -error;
  824. }
  825. STATIC int
  826. xfs_ioc_bulkstat(
  827. xfs_mount_t *mp,
  828. unsigned int cmd,
  829. void __user *arg)
  830. {
  831. xfs_fsop_bulkreq_t bulkreq;
  832. int count; /* # of records returned */
  833. xfs_ino_t inlast; /* last inode number */
  834. int done;
  835. int error;
  836. /* done = 1 if there are more stats to get and if bulkstat */
  837. /* should be called again (unused here, but used in dmapi) */
  838. if (!capable(CAP_SYS_ADMIN))
  839. return -EPERM;
  840. if (XFS_FORCED_SHUTDOWN(mp))
  841. return -XFS_ERROR(EIO);
  842. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  843. return -XFS_ERROR(EFAULT);
  844. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  845. return -XFS_ERROR(EFAULT);
  846. if ((count = bulkreq.icount) <= 0)
  847. return -XFS_ERROR(EINVAL);
  848. if (cmd == XFS_IOC_FSINUMBERS)
  849. error = xfs_inumbers(mp, &inlast, &count,
  850. bulkreq.ubuffer);
  851. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  852. error = xfs_bulkstat_single(mp, &inlast,
  853. bulkreq.ubuffer, &done);
  854. else { /* XFS_IOC_FSBULKSTAT */
  855. if (count == 1 && inlast != 0) {
  856. inlast++;
  857. error = xfs_bulkstat_single(mp, &inlast,
  858. bulkreq.ubuffer, &done);
  859. } else {
  860. error = xfs_bulkstat(mp, &inlast, &count,
  861. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  862. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  863. BULKSTAT_FG_QUICK, &done);
  864. }
  865. }
  866. if (error)
  867. return -error;
  868. if (bulkreq.ocount != NULL) {
  869. if (copy_to_user(bulkreq.lastip, &inlast,
  870. sizeof(xfs_ino_t)))
  871. return -XFS_ERROR(EFAULT);
  872. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  873. return -XFS_ERROR(EFAULT);
  874. }
  875. return 0;
  876. }
  877. STATIC int
  878. xfs_ioc_fsgeometry_v1(
  879. xfs_mount_t *mp,
  880. void __user *arg)
  881. {
  882. xfs_fsop_geom_v1_t fsgeo;
  883. int error;
  884. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  885. if (error)
  886. return -error;
  887. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  888. return -XFS_ERROR(EFAULT);
  889. return 0;
  890. }
  891. STATIC int
  892. xfs_ioc_fsgeometry(
  893. xfs_mount_t *mp,
  894. void __user *arg)
  895. {
  896. xfs_fsop_geom_t fsgeo;
  897. int error;
  898. error = xfs_fs_geometry(mp, &fsgeo, 4);
  899. if (error)
  900. return -error;
  901. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  902. return -XFS_ERROR(EFAULT);
  903. return 0;
  904. }
  905. /*
  906. * Linux extended inode flags interface.
  907. */
  908. #define LINUX_XFLAG_SYNC 0x00000008 /* Synchronous updates */
  909. #define LINUX_XFLAG_IMMUTABLE 0x00000010 /* Immutable file */
  910. #define LINUX_XFLAG_APPEND 0x00000020 /* writes to file may only append */
  911. #define LINUX_XFLAG_NODUMP 0x00000040 /* do not dump file */
  912. #define LINUX_XFLAG_NOATIME 0x00000080 /* do not update atime */
  913. STATIC unsigned int
  914. xfs_merge_ioc_xflags(
  915. unsigned int flags,
  916. unsigned int start)
  917. {
  918. unsigned int xflags = start;
  919. if (flags & LINUX_XFLAG_IMMUTABLE)
  920. xflags |= XFS_XFLAG_IMMUTABLE;
  921. else
  922. xflags &= ~XFS_XFLAG_IMMUTABLE;
  923. if (flags & LINUX_XFLAG_APPEND)
  924. xflags |= XFS_XFLAG_APPEND;
  925. else
  926. xflags &= ~XFS_XFLAG_APPEND;
  927. if (flags & LINUX_XFLAG_SYNC)
  928. xflags |= XFS_XFLAG_SYNC;
  929. else
  930. xflags &= ~XFS_XFLAG_SYNC;
  931. if (flags & LINUX_XFLAG_NOATIME)
  932. xflags |= XFS_XFLAG_NOATIME;
  933. else
  934. xflags &= ~XFS_XFLAG_NOATIME;
  935. if (flags & LINUX_XFLAG_NODUMP)
  936. xflags |= XFS_XFLAG_NODUMP;
  937. else
  938. xflags &= ~XFS_XFLAG_NODUMP;
  939. return xflags;
  940. }
  941. STATIC unsigned int
  942. xfs_di2lxflags(
  943. __uint16_t di_flags)
  944. {
  945. unsigned int flags = 0;
  946. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  947. flags |= LINUX_XFLAG_IMMUTABLE;
  948. if (di_flags & XFS_DIFLAG_APPEND)
  949. flags |= LINUX_XFLAG_APPEND;
  950. if (di_flags & XFS_DIFLAG_SYNC)
  951. flags |= LINUX_XFLAG_SYNC;
  952. if (di_flags & XFS_DIFLAG_NOATIME)
  953. flags |= LINUX_XFLAG_NOATIME;
  954. if (di_flags & XFS_DIFLAG_NODUMP)
  955. flags |= LINUX_XFLAG_NODUMP;
  956. return flags;
  957. }
  958. STATIC int
  959. xfs_ioc_xattr(
  960. bhv_vnode_t *vp,
  961. xfs_inode_t *ip,
  962. struct file *filp,
  963. unsigned int cmd,
  964. void __user *arg)
  965. {
  966. struct fsxattr fa;
  967. struct bhv_vattr *vattr;
  968. int error = 0;
  969. int attr_flags;
  970. unsigned int flags;
  971. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  972. if (unlikely(!vattr))
  973. return -ENOMEM;
  974. switch (cmd) {
  975. case XFS_IOC_FSGETXATTR: {
  976. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
  977. XFS_AT_NEXTENTS | XFS_AT_PROJID;
  978. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  979. if (unlikely(error)) {
  980. error = -error;
  981. break;
  982. }
  983. fa.fsx_xflags = vattr->va_xflags;
  984. fa.fsx_extsize = vattr->va_extsize;
  985. fa.fsx_nextents = vattr->va_nextents;
  986. fa.fsx_projid = vattr->va_projid;
  987. if (copy_to_user(arg, &fa, sizeof(fa))) {
  988. error = -EFAULT;
  989. break;
  990. }
  991. break;
  992. }
  993. case XFS_IOC_FSSETXATTR: {
  994. if (copy_from_user(&fa, arg, sizeof(fa))) {
  995. error = -EFAULT;
  996. break;
  997. }
  998. attr_flags = 0;
  999. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1000. attr_flags |= ATTR_NONBLOCK;
  1001. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
  1002. vattr->va_xflags = fa.fsx_xflags;
  1003. vattr->va_extsize = fa.fsx_extsize;
  1004. vattr->va_projid = fa.fsx_projid;
  1005. error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
  1006. if (likely(!error))
  1007. __vn_revalidate(vp, vattr); /* update flags */
  1008. error = -error;
  1009. break;
  1010. }
  1011. case XFS_IOC_FSGETXATTRA: {
  1012. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
  1013. XFS_AT_ANEXTENTS | XFS_AT_PROJID;
  1014. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  1015. if (unlikely(error)) {
  1016. error = -error;
  1017. break;
  1018. }
  1019. fa.fsx_xflags = vattr->va_xflags;
  1020. fa.fsx_extsize = vattr->va_extsize;
  1021. fa.fsx_nextents = vattr->va_anextents;
  1022. fa.fsx_projid = vattr->va_projid;
  1023. if (copy_to_user(arg, &fa, sizeof(fa))) {
  1024. error = -EFAULT;
  1025. break;
  1026. }
  1027. break;
  1028. }
  1029. case XFS_IOC_GETXFLAGS: {
  1030. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1031. if (copy_to_user(arg, &flags, sizeof(flags)))
  1032. error = -EFAULT;
  1033. break;
  1034. }
  1035. case XFS_IOC_SETXFLAGS: {
  1036. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1037. error = -EFAULT;
  1038. break;
  1039. }
  1040. if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
  1041. LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
  1042. LINUX_XFLAG_SYNC)) {
  1043. error = -EOPNOTSUPP;
  1044. break;
  1045. }
  1046. attr_flags = 0;
  1047. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1048. attr_flags |= ATTR_NONBLOCK;
  1049. vattr->va_mask = XFS_AT_XFLAGS;
  1050. vattr->va_xflags = xfs_merge_ioc_xflags(flags,
  1051. xfs_ip2xflags(ip));
  1052. error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
  1053. if (likely(!error))
  1054. __vn_revalidate(vp, vattr); /* update flags */
  1055. error = -error;
  1056. break;
  1057. }
  1058. default:
  1059. error = -ENOTTY;
  1060. break;
  1061. }
  1062. kfree(vattr);
  1063. return error;
  1064. }
  1065. STATIC int
  1066. xfs_ioc_getbmap(
  1067. bhv_desc_t *bdp,
  1068. struct file *filp,
  1069. int ioflags,
  1070. unsigned int cmd,
  1071. void __user *arg)
  1072. {
  1073. struct getbmap bm;
  1074. int iflags;
  1075. int error;
  1076. if (copy_from_user(&bm, arg, sizeof(bm)))
  1077. return -XFS_ERROR(EFAULT);
  1078. if (bm.bmv_count < 2)
  1079. return -XFS_ERROR(EINVAL);
  1080. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1081. if (ioflags & IO_INVIS)
  1082. iflags |= BMV_IF_NO_DMAPI_READ;
  1083. error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
  1084. if (error)
  1085. return -error;
  1086. if (copy_to_user(arg, &bm, sizeof(bm)))
  1087. return -XFS_ERROR(EFAULT);
  1088. return 0;
  1089. }
  1090. STATIC int
  1091. xfs_ioc_getbmapx(
  1092. bhv_desc_t *bdp,
  1093. void __user *arg)
  1094. {
  1095. struct getbmapx bmx;
  1096. struct getbmap bm;
  1097. int iflags;
  1098. int error;
  1099. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1100. return -XFS_ERROR(EFAULT);
  1101. if (bmx.bmv_count < 2)
  1102. return -XFS_ERROR(EINVAL);
  1103. /*
  1104. * Map input getbmapx structure to a getbmap
  1105. * structure for xfs_getbmap.
  1106. */
  1107. GETBMAP_CONVERT(bmx, bm);
  1108. iflags = bmx.bmv_iflags;
  1109. if (iflags & (~BMV_IF_VALID))
  1110. return -XFS_ERROR(EINVAL);
  1111. iflags |= BMV_IF_EXTENDED;
  1112. error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
  1113. if (error)
  1114. return -error;
  1115. GETBMAP_CONVERT(bm, bmx);
  1116. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  1117. return -XFS_ERROR(EFAULT);
  1118. return 0;
  1119. }