xfs_ioctl.c 29 KB

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