xfs_ioctl.c 29 KB

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