xfs_ioctl.c 29 KB

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