xfs_ioctl.c 27 KB

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