xfs_ioctl.c 29 KB

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