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