xfs_ioctl.c 28 KB

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