xfs_ioctl.c 28 KB

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