xfs_ioctl.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353
  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. bhv_vnode_t *vp,
  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, vp, 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. case XFS_IOC_GETXFLAGS:
  655. case XFS_IOC_SETXFLAGS:
  656. case XFS_IOC_FSGETXATTR:
  657. case XFS_IOC_FSSETXATTR:
  658. case XFS_IOC_FSGETXATTRA:
  659. return xfs_ioc_xattr(vp, ip, filp, cmd, arg);
  660. case XFS_IOC_FSSETDM: {
  661. struct fsdmidata dmi;
  662. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  663. return -XFS_ERROR(EFAULT);
  664. error = xfs_set_dmattrs(bdp, dmi.fsd_dmevmask, dmi.fsd_dmstate,
  665. NULL);
  666. return -error;
  667. }
  668. case XFS_IOC_GETBMAP:
  669. case XFS_IOC_GETBMAPA:
  670. return xfs_ioc_getbmap(bdp, filp, ioflags, cmd, arg);
  671. case XFS_IOC_GETBMAPX:
  672. return xfs_ioc_getbmapx(bdp, arg);
  673. case XFS_IOC_FD_TO_HANDLE:
  674. case XFS_IOC_PATH_TO_HANDLE:
  675. case XFS_IOC_PATH_TO_FSHANDLE:
  676. return xfs_find_handle(cmd, arg);
  677. case XFS_IOC_OPEN_BY_HANDLE:
  678. return xfs_open_by_handle(mp, arg, filp, inode);
  679. case XFS_IOC_FSSETDM_BY_HANDLE:
  680. return xfs_fssetdm_by_handle(mp, arg, filp, inode);
  681. case XFS_IOC_READLINK_BY_HANDLE:
  682. return xfs_readlink_by_handle(mp, arg, filp, inode);
  683. case XFS_IOC_ATTRLIST_BY_HANDLE:
  684. return xfs_attrlist_by_handle(mp, arg, filp, inode);
  685. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  686. return xfs_attrmulti_by_handle(mp, arg, filp, inode);
  687. case XFS_IOC_SWAPEXT: {
  688. error = xfs_swapext((struct xfs_swapext __user *)arg);
  689. return -error;
  690. }
  691. case XFS_IOC_FSCOUNTS: {
  692. xfs_fsop_counts_t out;
  693. error = xfs_fs_counts(mp, &out);
  694. if (error)
  695. return -error;
  696. if (copy_to_user(arg, &out, sizeof(out)))
  697. return -XFS_ERROR(EFAULT);
  698. return 0;
  699. }
  700. case XFS_IOC_SET_RESBLKS: {
  701. xfs_fsop_resblks_t inout;
  702. __uint64_t in;
  703. if (!capable(CAP_SYS_ADMIN))
  704. return -EPERM;
  705. if (copy_from_user(&inout, arg, sizeof(inout)))
  706. return -XFS_ERROR(EFAULT);
  707. /* input parameter is passed in resblks field of structure */
  708. in = inout.resblks;
  709. error = xfs_reserve_blocks(mp, &in, &inout);
  710. if (error)
  711. return -error;
  712. if (copy_to_user(arg, &inout, sizeof(inout)))
  713. return -XFS_ERROR(EFAULT);
  714. return 0;
  715. }
  716. case XFS_IOC_GET_RESBLKS: {
  717. xfs_fsop_resblks_t out;
  718. if (!capable(CAP_SYS_ADMIN))
  719. return -EPERM;
  720. error = xfs_reserve_blocks(mp, NULL, &out);
  721. if (error)
  722. return -error;
  723. if (copy_to_user(arg, &out, sizeof(out)))
  724. return -XFS_ERROR(EFAULT);
  725. return 0;
  726. }
  727. case XFS_IOC_FSGROWFSDATA: {
  728. xfs_growfs_data_t in;
  729. if (!capable(CAP_SYS_ADMIN))
  730. return -EPERM;
  731. if (copy_from_user(&in, arg, sizeof(in)))
  732. return -XFS_ERROR(EFAULT);
  733. error = xfs_growfs_data(mp, &in);
  734. return -error;
  735. }
  736. case XFS_IOC_FSGROWFSLOG: {
  737. xfs_growfs_log_t in;
  738. if (!capable(CAP_SYS_ADMIN))
  739. return -EPERM;
  740. if (copy_from_user(&in, arg, sizeof(in)))
  741. return -XFS_ERROR(EFAULT);
  742. error = xfs_growfs_log(mp, &in);
  743. return -error;
  744. }
  745. case XFS_IOC_FSGROWFSRT: {
  746. xfs_growfs_rt_t in;
  747. if (!capable(CAP_SYS_ADMIN))
  748. return -EPERM;
  749. if (copy_from_user(&in, arg, sizeof(in)))
  750. return -XFS_ERROR(EFAULT);
  751. error = xfs_growfs_rt(mp, &in);
  752. return -error;
  753. }
  754. case XFS_IOC_FREEZE:
  755. if (!capable(CAP_SYS_ADMIN))
  756. return -EPERM;
  757. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  758. freeze_bdev(inode->i_sb->s_bdev);
  759. return 0;
  760. case XFS_IOC_THAW:
  761. if (!capable(CAP_SYS_ADMIN))
  762. return -EPERM;
  763. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  764. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  765. return 0;
  766. case XFS_IOC_GOINGDOWN: {
  767. __uint32_t in;
  768. if (!capable(CAP_SYS_ADMIN))
  769. return -EPERM;
  770. if (get_user(in, (__uint32_t __user *)arg))
  771. return -XFS_ERROR(EFAULT);
  772. error = xfs_fs_goingdown(mp, in);
  773. return -error;
  774. }
  775. case XFS_IOC_ERROR_INJECTION: {
  776. xfs_error_injection_t in;
  777. if (!capable(CAP_SYS_ADMIN))
  778. return -EPERM;
  779. if (copy_from_user(&in, arg, sizeof(in)))
  780. return -XFS_ERROR(EFAULT);
  781. error = xfs_errortag_add(in.errtag, mp);
  782. return -error;
  783. }
  784. case XFS_IOC_ERROR_CLEARALL:
  785. if (!capable(CAP_SYS_ADMIN))
  786. return -EPERM;
  787. error = xfs_errortag_clearall(mp);
  788. return -error;
  789. default:
  790. return -ENOTTY;
  791. }
  792. }
  793. STATIC int
  794. xfs_ioc_space(
  795. bhv_desc_t *bdp,
  796. bhv_vnode_t *vp,
  797. struct file *filp,
  798. int ioflags,
  799. unsigned int cmd,
  800. void __user *arg)
  801. {
  802. xfs_flock64_t bf;
  803. int attr_flags = 0;
  804. int error;
  805. if (vp->v_inode.i_flags & (S_IMMUTABLE|S_APPEND))
  806. return -XFS_ERROR(EPERM);
  807. if (!(filp->f_mode & FMODE_WRITE))
  808. return -XFS_ERROR(EBADF);
  809. if (!VN_ISREG(vp))
  810. return -XFS_ERROR(EINVAL);
  811. if (copy_from_user(&bf, arg, sizeof(bf)))
  812. return -XFS_ERROR(EFAULT);
  813. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  814. attr_flags |= ATTR_NONBLOCK;
  815. if (ioflags & IO_INVIS)
  816. attr_flags |= ATTR_DMI;
  817. error = xfs_change_file_space(bdp, cmd, &bf, filp->f_pos,
  818. NULL, attr_flags);
  819. return -error;
  820. }
  821. STATIC int
  822. xfs_ioc_bulkstat(
  823. xfs_mount_t *mp,
  824. unsigned int cmd,
  825. void __user *arg)
  826. {
  827. xfs_fsop_bulkreq_t bulkreq;
  828. int count; /* # of records returned */
  829. xfs_ino_t inlast; /* last inode number */
  830. int done;
  831. int error;
  832. /* done = 1 if there are more stats to get and if bulkstat */
  833. /* should be called again (unused here, but used in dmapi) */
  834. if (!capable(CAP_SYS_ADMIN))
  835. return -EPERM;
  836. if (XFS_FORCED_SHUTDOWN(mp))
  837. return -XFS_ERROR(EIO);
  838. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  839. return -XFS_ERROR(EFAULT);
  840. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  841. return -XFS_ERROR(EFAULT);
  842. if ((count = bulkreq.icount) <= 0)
  843. return -XFS_ERROR(EINVAL);
  844. if (cmd == XFS_IOC_FSINUMBERS)
  845. error = xfs_inumbers(mp, &inlast, &count,
  846. bulkreq.ubuffer);
  847. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  848. error = xfs_bulkstat_single(mp, &inlast,
  849. bulkreq.ubuffer, &done);
  850. else { /* XFS_IOC_FSBULKSTAT */
  851. if (count == 1 && inlast != 0) {
  852. inlast++;
  853. error = xfs_bulkstat_single(mp, &inlast,
  854. bulkreq.ubuffer, &done);
  855. } else {
  856. error = xfs_bulkstat(mp, &inlast, &count,
  857. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  858. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  859. BULKSTAT_FG_QUICK, &done);
  860. }
  861. }
  862. if (error)
  863. return -error;
  864. if (bulkreq.ocount != NULL) {
  865. if (copy_to_user(bulkreq.lastip, &inlast,
  866. sizeof(xfs_ino_t)))
  867. return -XFS_ERROR(EFAULT);
  868. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  869. return -XFS_ERROR(EFAULT);
  870. }
  871. return 0;
  872. }
  873. STATIC int
  874. xfs_ioc_fsgeometry_v1(
  875. xfs_mount_t *mp,
  876. void __user *arg)
  877. {
  878. xfs_fsop_geom_v1_t fsgeo;
  879. int error;
  880. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  881. if (error)
  882. return -error;
  883. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  884. return -XFS_ERROR(EFAULT);
  885. return 0;
  886. }
  887. STATIC int
  888. xfs_ioc_fsgeometry(
  889. xfs_mount_t *mp,
  890. void __user *arg)
  891. {
  892. xfs_fsop_geom_t fsgeo;
  893. int error;
  894. error = xfs_fs_geometry(mp, &fsgeo, 4);
  895. if (error)
  896. return -error;
  897. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  898. return -XFS_ERROR(EFAULT);
  899. return 0;
  900. }
  901. /*
  902. * Linux extended inode flags interface.
  903. */
  904. #define LINUX_XFLAG_SYNC 0x00000008 /* Synchronous updates */
  905. #define LINUX_XFLAG_IMMUTABLE 0x00000010 /* Immutable file */
  906. #define LINUX_XFLAG_APPEND 0x00000020 /* writes to file may only append */
  907. #define LINUX_XFLAG_NODUMP 0x00000040 /* do not dump file */
  908. #define LINUX_XFLAG_NOATIME 0x00000080 /* do not update atime */
  909. STATIC unsigned int
  910. xfs_merge_ioc_xflags(
  911. unsigned int flags,
  912. unsigned int start)
  913. {
  914. unsigned int xflags = start;
  915. if (flags & LINUX_XFLAG_IMMUTABLE)
  916. xflags |= XFS_XFLAG_IMMUTABLE;
  917. else
  918. xflags &= ~XFS_XFLAG_IMMUTABLE;
  919. if (flags & LINUX_XFLAG_APPEND)
  920. xflags |= XFS_XFLAG_APPEND;
  921. else
  922. xflags &= ~XFS_XFLAG_APPEND;
  923. if (flags & LINUX_XFLAG_SYNC)
  924. xflags |= XFS_XFLAG_SYNC;
  925. else
  926. xflags &= ~XFS_XFLAG_SYNC;
  927. if (flags & LINUX_XFLAG_NOATIME)
  928. xflags |= XFS_XFLAG_NOATIME;
  929. else
  930. xflags &= ~XFS_XFLAG_NOATIME;
  931. if (flags & LINUX_XFLAG_NODUMP)
  932. xflags |= XFS_XFLAG_NODUMP;
  933. else
  934. xflags &= ~XFS_XFLAG_NODUMP;
  935. return xflags;
  936. }
  937. STATIC unsigned int
  938. xfs_di2lxflags(
  939. __uint16_t di_flags)
  940. {
  941. unsigned int flags = 0;
  942. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  943. flags |= LINUX_XFLAG_IMMUTABLE;
  944. if (di_flags & XFS_DIFLAG_APPEND)
  945. flags |= LINUX_XFLAG_APPEND;
  946. if (di_flags & XFS_DIFLAG_SYNC)
  947. flags |= LINUX_XFLAG_SYNC;
  948. if (di_flags & XFS_DIFLAG_NOATIME)
  949. flags |= LINUX_XFLAG_NOATIME;
  950. if (di_flags & XFS_DIFLAG_NODUMP)
  951. flags |= LINUX_XFLAG_NODUMP;
  952. return flags;
  953. }
  954. STATIC int
  955. xfs_ioc_xattr(
  956. bhv_vnode_t *vp,
  957. xfs_inode_t *ip,
  958. struct file *filp,
  959. unsigned int cmd,
  960. void __user *arg)
  961. {
  962. struct fsxattr fa;
  963. struct bhv_vattr *vattr;
  964. int error = 0;
  965. int attr_flags;
  966. unsigned int flags;
  967. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  968. if (unlikely(!vattr))
  969. return -ENOMEM;
  970. switch (cmd) {
  971. case XFS_IOC_FSGETXATTR: {
  972. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
  973. XFS_AT_NEXTENTS | XFS_AT_PROJID;
  974. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  975. if (unlikely(error)) {
  976. error = -error;
  977. break;
  978. }
  979. fa.fsx_xflags = vattr->va_xflags;
  980. fa.fsx_extsize = vattr->va_extsize;
  981. fa.fsx_nextents = vattr->va_nextents;
  982. fa.fsx_projid = vattr->va_projid;
  983. if (copy_to_user(arg, &fa, sizeof(fa))) {
  984. error = -EFAULT;
  985. break;
  986. }
  987. break;
  988. }
  989. case XFS_IOC_FSSETXATTR: {
  990. if (copy_from_user(&fa, arg, sizeof(fa))) {
  991. error = -EFAULT;
  992. break;
  993. }
  994. attr_flags = 0;
  995. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  996. attr_flags |= ATTR_NONBLOCK;
  997. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
  998. vattr->va_xflags = fa.fsx_xflags;
  999. vattr->va_extsize = fa.fsx_extsize;
  1000. vattr->va_projid = fa.fsx_projid;
  1001. error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
  1002. if (likely(!error))
  1003. __vn_revalidate(vp, vattr); /* update flags */
  1004. error = -error;
  1005. break;
  1006. }
  1007. case XFS_IOC_FSGETXATTRA: {
  1008. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | \
  1009. XFS_AT_ANEXTENTS | XFS_AT_PROJID;
  1010. error = bhv_vop_getattr(vp, vattr, 0, NULL);
  1011. if (unlikely(error)) {
  1012. error = -error;
  1013. break;
  1014. }
  1015. fa.fsx_xflags = vattr->va_xflags;
  1016. fa.fsx_extsize = vattr->va_extsize;
  1017. fa.fsx_nextents = vattr->va_anextents;
  1018. fa.fsx_projid = vattr->va_projid;
  1019. if (copy_to_user(arg, &fa, sizeof(fa))) {
  1020. error = -EFAULT;
  1021. break;
  1022. }
  1023. break;
  1024. }
  1025. case XFS_IOC_GETXFLAGS: {
  1026. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1027. if (copy_to_user(arg, &flags, sizeof(flags)))
  1028. error = -EFAULT;
  1029. break;
  1030. }
  1031. case XFS_IOC_SETXFLAGS: {
  1032. if (copy_from_user(&flags, arg, sizeof(flags))) {
  1033. error = -EFAULT;
  1034. break;
  1035. }
  1036. if (flags & ~(LINUX_XFLAG_IMMUTABLE | LINUX_XFLAG_APPEND | \
  1037. LINUX_XFLAG_NOATIME | LINUX_XFLAG_NODUMP | \
  1038. LINUX_XFLAG_SYNC)) {
  1039. error = -EOPNOTSUPP;
  1040. break;
  1041. }
  1042. attr_flags = 0;
  1043. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1044. attr_flags |= ATTR_NONBLOCK;
  1045. vattr->va_mask = XFS_AT_XFLAGS;
  1046. vattr->va_xflags = xfs_merge_ioc_xflags(flags,
  1047. xfs_ip2xflags(ip));
  1048. error = bhv_vop_setattr(vp, vattr, attr_flags, NULL);
  1049. if (likely(!error))
  1050. __vn_revalidate(vp, vattr); /* update flags */
  1051. error = -error;
  1052. break;
  1053. }
  1054. case XFS_IOC_GETVERSION: {
  1055. flags = vn_to_inode(vp)->i_generation;
  1056. if (copy_to_user(arg, &flags, sizeof(flags)))
  1057. error = -EFAULT;
  1058. break;
  1059. }
  1060. default:
  1061. error = -ENOTTY;
  1062. break;
  1063. }
  1064. kfree(vattr);
  1065. return error;
  1066. }
  1067. STATIC int
  1068. xfs_ioc_getbmap(
  1069. bhv_desc_t *bdp,
  1070. struct file *filp,
  1071. int ioflags,
  1072. unsigned int cmd,
  1073. void __user *arg)
  1074. {
  1075. struct getbmap bm;
  1076. int iflags;
  1077. int error;
  1078. if (copy_from_user(&bm, arg, sizeof(bm)))
  1079. return -XFS_ERROR(EFAULT);
  1080. if (bm.bmv_count < 2)
  1081. return -XFS_ERROR(EINVAL);
  1082. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1083. if (ioflags & IO_INVIS)
  1084. iflags |= BMV_IF_NO_DMAPI_READ;
  1085. error = xfs_getbmap(bdp, &bm, (struct getbmap __user *)arg+1, iflags);
  1086. if (error)
  1087. return -error;
  1088. if (copy_to_user(arg, &bm, sizeof(bm)))
  1089. return -XFS_ERROR(EFAULT);
  1090. return 0;
  1091. }
  1092. STATIC int
  1093. xfs_ioc_getbmapx(
  1094. bhv_desc_t *bdp,
  1095. void __user *arg)
  1096. {
  1097. struct getbmapx bmx;
  1098. struct getbmap bm;
  1099. int iflags;
  1100. int error;
  1101. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1102. return -XFS_ERROR(EFAULT);
  1103. if (bmx.bmv_count < 2)
  1104. return -XFS_ERROR(EINVAL);
  1105. /*
  1106. * Map input getbmapx structure to a getbmap
  1107. * structure for xfs_getbmap.
  1108. */
  1109. GETBMAP_CONVERT(bmx, bm);
  1110. iflags = bmx.bmv_iflags;
  1111. if (iflags & (~BMV_IF_VALID))
  1112. return -XFS_ERROR(EINVAL);
  1113. iflags |= BMV_IF_EXTENDED;
  1114. error = xfs_getbmap(bdp, &bm, (struct getbmapx __user *)arg+1, iflags);
  1115. if (error)
  1116. return -error;
  1117. GETBMAP_CONVERT(bm, bmx);
  1118. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  1119. return -XFS_ERROR(EFAULT);
  1120. return 0;
  1121. }