xfs_ioctl.c 28 KB

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