xfs_ioctl.c 27 KB

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