xfs_ioctl.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. /*
  408. * Reject flags, only allow namespaces.
  409. */
  410. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  411. return -XFS_ERROR(EINVAL);
  412. error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
  413. if (error)
  414. goto out;
  415. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  416. if (!kbuf)
  417. goto out_vn_rele;
  418. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  419. error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
  420. al_hreq.flags, cursor);
  421. if (error)
  422. goto out_kfree;
  423. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  424. error = -EFAULT;
  425. out_kfree:
  426. kfree(kbuf);
  427. out_vn_rele:
  428. iput(inode);
  429. out:
  430. return -error;
  431. }
  432. STATIC int
  433. xfs_attrmulti_attr_get(
  434. struct inode *inode,
  435. char *name,
  436. char __user *ubuf,
  437. __uint32_t *len,
  438. __uint32_t flags)
  439. {
  440. char *kbuf;
  441. int error = EFAULT;
  442. if (*len > XATTR_SIZE_MAX)
  443. return EINVAL;
  444. kbuf = kmalloc(*len, GFP_KERNEL);
  445. if (!kbuf)
  446. return ENOMEM;
  447. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  448. if (error)
  449. goto out_kfree;
  450. if (copy_to_user(ubuf, kbuf, *len))
  451. error = EFAULT;
  452. out_kfree:
  453. kfree(kbuf);
  454. return error;
  455. }
  456. STATIC int
  457. xfs_attrmulti_attr_set(
  458. struct inode *inode,
  459. char *name,
  460. const char __user *ubuf,
  461. __uint32_t len,
  462. __uint32_t flags)
  463. {
  464. char *kbuf;
  465. int error = EFAULT;
  466. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  467. return EPERM;
  468. if (len > XATTR_SIZE_MAX)
  469. return EINVAL;
  470. kbuf = kmalloc(len, GFP_KERNEL);
  471. if (!kbuf)
  472. return ENOMEM;
  473. if (copy_from_user(kbuf, ubuf, len))
  474. goto out_kfree;
  475. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  476. out_kfree:
  477. kfree(kbuf);
  478. return error;
  479. }
  480. STATIC int
  481. xfs_attrmulti_attr_remove(
  482. struct inode *inode,
  483. char *name,
  484. __uint32_t flags)
  485. {
  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 file *parfilp,
  495. struct inode *parinode)
  496. {
  497. int error;
  498. xfs_attr_multiop_t *ops;
  499. xfs_fsop_attrmulti_handlereq_t am_hreq;
  500. struct inode *inode;
  501. unsigned int i, size;
  502. char *attr_name;
  503. if (!capable(CAP_SYS_ADMIN))
  504. return -XFS_ERROR(EPERM);
  505. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  506. return -XFS_ERROR(EFAULT);
  507. error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
  508. if (error)
  509. goto out;
  510. error = E2BIG;
  511. size = am_hreq.opcount * sizeof(attr_multiop_t);
  512. if (!size || size > 16 * PAGE_SIZE)
  513. goto out_vn_rele;
  514. error = ENOMEM;
  515. ops = kmalloc(size, GFP_KERNEL);
  516. if (!ops)
  517. goto out_vn_rele;
  518. error = EFAULT;
  519. if (copy_from_user(ops, am_hreq.ops, size))
  520. goto out_kfree_ops;
  521. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  522. if (!attr_name)
  523. goto out_kfree_ops;
  524. error = 0;
  525. for (i = 0; i < am_hreq.opcount; i++) {
  526. ops[i].am_error = strncpy_from_user(attr_name,
  527. ops[i].am_attrname, MAXNAMELEN);
  528. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  529. error = -ERANGE;
  530. if (ops[i].am_error < 0)
  531. break;
  532. switch (ops[i].am_opcode) {
  533. case ATTR_OP_GET:
  534. ops[i].am_error = xfs_attrmulti_attr_get(inode,
  535. attr_name, ops[i].am_attrvalue,
  536. &ops[i].am_length, ops[i].am_flags);
  537. break;
  538. case ATTR_OP_SET:
  539. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  540. if (ops[i].am_error)
  541. break;
  542. ops[i].am_error = xfs_attrmulti_attr_set(inode,
  543. attr_name, ops[i].am_attrvalue,
  544. ops[i].am_length, ops[i].am_flags);
  545. mnt_drop_write(parfilp->f_path.mnt);
  546. break;
  547. case ATTR_OP_REMOVE:
  548. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  549. if (ops[i].am_error)
  550. break;
  551. ops[i].am_error = xfs_attrmulti_attr_remove(inode,
  552. attr_name, ops[i].am_flags);
  553. mnt_drop_write(parfilp->f_path.mnt);
  554. break;
  555. default:
  556. ops[i].am_error = EINVAL;
  557. }
  558. }
  559. if (copy_to_user(am_hreq.ops, ops, size))
  560. error = XFS_ERROR(EFAULT);
  561. kfree(attr_name);
  562. out_kfree_ops:
  563. kfree(ops);
  564. out_vn_rele:
  565. iput(inode);
  566. out:
  567. return -error;
  568. }
  569. STATIC int
  570. xfs_ioc_space(
  571. struct xfs_inode *ip,
  572. struct inode *inode,
  573. struct file *filp,
  574. int ioflags,
  575. unsigned int cmd,
  576. void __user *arg)
  577. {
  578. xfs_flock64_t bf;
  579. int attr_flags = 0;
  580. int error;
  581. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  582. return -XFS_ERROR(EPERM);
  583. if (!(filp->f_mode & FMODE_WRITE))
  584. return -XFS_ERROR(EBADF);
  585. if (!S_ISREG(inode->i_mode))
  586. return -XFS_ERROR(EINVAL);
  587. if (copy_from_user(&bf, arg, sizeof(bf)))
  588. return -XFS_ERROR(EFAULT);
  589. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  590. attr_flags |= ATTR_NONBLOCK;
  591. if (ioflags & IO_INVIS)
  592. attr_flags |= ATTR_DMI;
  593. error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
  594. NULL, attr_flags);
  595. return -error;
  596. }
  597. STATIC int
  598. xfs_ioc_bulkstat(
  599. xfs_mount_t *mp,
  600. unsigned int cmd,
  601. void __user *arg)
  602. {
  603. xfs_fsop_bulkreq_t bulkreq;
  604. int count; /* # of records returned */
  605. xfs_ino_t inlast; /* last inode number */
  606. int done;
  607. int error;
  608. /* done = 1 if there are more stats to get and if bulkstat */
  609. /* should be called again (unused here, but used in dmapi) */
  610. if (!capable(CAP_SYS_ADMIN))
  611. return -EPERM;
  612. if (XFS_FORCED_SHUTDOWN(mp))
  613. return -XFS_ERROR(EIO);
  614. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  615. return -XFS_ERROR(EFAULT);
  616. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  617. return -XFS_ERROR(EFAULT);
  618. if ((count = bulkreq.icount) <= 0)
  619. return -XFS_ERROR(EINVAL);
  620. if (bulkreq.ubuffer == NULL)
  621. return -XFS_ERROR(EINVAL);
  622. if (cmd == XFS_IOC_FSINUMBERS)
  623. error = xfs_inumbers(mp, &inlast, &count,
  624. bulkreq.ubuffer, xfs_inumbers_fmt);
  625. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  626. error = xfs_bulkstat_single(mp, &inlast,
  627. bulkreq.ubuffer, &done);
  628. else /* XFS_IOC_FSBULKSTAT */
  629. error = xfs_bulkstat(mp, &inlast, &count,
  630. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  631. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  632. BULKSTAT_FG_QUICK, &done);
  633. if (error)
  634. return -error;
  635. if (bulkreq.ocount != NULL) {
  636. if (copy_to_user(bulkreq.lastip, &inlast,
  637. sizeof(xfs_ino_t)))
  638. return -XFS_ERROR(EFAULT);
  639. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  640. return -XFS_ERROR(EFAULT);
  641. }
  642. return 0;
  643. }
  644. STATIC int
  645. xfs_ioc_fsgeometry_v1(
  646. xfs_mount_t *mp,
  647. void __user *arg)
  648. {
  649. xfs_fsop_geom_v1_t fsgeo;
  650. int error;
  651. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  652. if (error)
  653. return -error;
  654. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  655. return -XFS_ERROR(EFAULT);
  656. return 0;
  657. }
  658. STATIC int
  659. xfs_ioc_fsgeometry(
  660. xfs_mount_t *mp,
  661. void __user *arg)
  662. {
  663. xfs_fsop_geom_t fsgeo;
  664. int error;
  665. error = xfs_fs_geometry(mp, &fsgeo, 4);
  666. if (error)
  667. return -error;
  668. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  669. return -XFS_ERROR(EFAULT);
  670. return 0;
  671. }
  672. /*
  673. * Linux extended inode flags interface.
  674. */
  675. STATIC unsigned int
  676. xfs_merge_ioc_xflags(
  677. unsigned int flags,
  678. unsigned int start)
  679. {
  680. unsigned int xflags = start;
  681. if (flags & FS_IMMUTABLE_FL)
  682. xflags |= XFS_XFLAG_IMMUTABLE;
  683. else
  684. xflags &= ~XFS_XFLAG_IMMUTABLE;
  685. if (flags & FS_APPEND_FL)
  686. xflags |= XFS_XFLAG_APPEND;
  687. else
  688. xflags &= ~XFS_XFLAG_APPEND;
  689. if (flags & FS_SYNC_FL)
  690. xflags |= XFS_XFLAG_SYNC;
  691. else
  692. xflags &= ~XFS_XFLAG_SYNC;
  693. if (flags & FS_NOATIME_FL)
  694. xflags |= XFS_XFLAG_NOATIME;
  695. else
  696. xflags &= ~XFS_XFLAG_NOATIME;
  697. if (flags & FS_NODUMP_FL)
  698. xflags |= XFS_XFLAG_NODUMP;
  699. else
  700. xflags &= ~XFS_XFLAG_NODUMP;
  701. return xflags;
  702. }
  703. STATIC unsigned int
  704. xfs_di2lxflags(
  705. __uint16_t di_flags)
  706. {
  707. unsigned int flags = 0;
  708. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  709. flags |= FS_IMMUTABLE_FL;
  710. if (di_flags & XFS_DIFLAG_APPEND)
  711. flags |= FS_APPEND_FL;
  712. if (di_flags & XFS_DIFLAG_SYNC)
  713. flags |= FS_SYNC_FL;
  714. if (di_flags & XFS_DIFLAG_NOATIME)
  715. flags |= FS_NOATIME_FL;
  716. if (di_flags & XFS_DIFLAG_NODUMP)
  717. flags |= FS_NODUMP_FL;
  718. return flags;
  719. }
  720. STATIC int
  721. xfs_ioc_fsgetxattr(
  722. xfs_inode_t *ip,
  723. int attr,
  724. void __user *arg)
  725. {
  726. struct fsxattr fa;
  727. xfs_ilock(ip, XFS_ILOCK_SHARED);
  728. fa.fsx_xflags = xfs_ip2xflags(ip);
  729. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  730. fa.fsx_projid = ip->i_d.di_projid;
  731. if (attr) {
  732. if (ip->i_afp) {
  733. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  734. fa.fsx_nextents = ip->i_afp->if_bytes /
  735. sizeof(xfs_bmbt_rec_t);
  736. else
  737. fa.fsx_nextents = ip->i_d.di_anextents;
  738. } else
  739. fa.fsx_nextents = 0;
  740. } else {
  741. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  742. fa.fsx_nextents = ip->i_df.if_bytes /
  743. sizeof(xfs_bmbt_rec_t);
  744. else
  745. fa.fsx_nextents = ip->i_d.di_nextents;
  746. }
  747. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  748. if (copy_to_user(arg, &fa, sizeof(fa)))
  749. return -EFAULT;
  750. return 0;
  751. }
  752. STATIC int
  753. xfs_ioc_fssetxattr(
  754. xfs_inode_t *ip,
  755. struct file *filp,
  756. void __user *arg)
  757. {
  758. struct fsxattr fa;
  759. struct bhv_vattr *vattr;
  760. int error;
  761. int attr_flags;
  762. if (copy_from_user(&fa, arg, sizeof(fa)))
  763. return -EFAULT;
  764. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  765. if (unlikely(!vattr))
  766. return -ENOMEM;
  767. attr_flags = 0;
  768. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  769. attr_flags |= ATTR_NONBLOCK;
  770. vattr->va_mask = XFS_AT_XFLAGS | XFS_AT_EXTSIZE | XFS_AT_PROJID;
  771. vattr->va_xflags = fa.fsx_xflags;
  772. vattr->va_extsize = fa.fsx_extsize;
  773. vattr->va_projid = fa.fsx_projid;
  774. error = -xfs_setattr(ip, vattr, attr_flags, NULL);
  775. if (!error)
  776. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  777. kfree(vattr);
  778. return 0;
  779. }
  780. STATIC int
  781. xfs_ioc_getxflags(
  782. xfs_inode_t *ip,
  783. void __user *arg)
  784. {
  785. unsigned int flags;
  786. flags = xfs_di2lxflags(ip->i_d.di_flags);
  787. if (copy_to_user(arg, &flags, sizeof(flags)))
  788. return -EFAULT;
  789. return 0;
  790. }
  791. STATIC int
  792. xfs_ioc_setxflags(
  793. xfs_inode_t *ip,
  794. struct file *filp,
  795. void __user *arg)
  796. {
  797. struct bhv_vattr *vattr;
  798. unsigned int flags;
  799. int attr_flags;
  800. int error;
  801. if (copy_from_user(&flags, arg, sizeof(flags)))
  802. return -EFAULT;
  803. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  804. FS_NOATIME_FL | FS_NODUMP_FL | \
  805. FS_SYNC_FL))
  806. return -EOPNOTSUPP;
  807. vattr = kmalloc(sizeof(*vattr), GFP_KERNEL);
  808. if (unlikely(!vattr))
  809. return -ENOMEM;
  810. attr_flags = 0;
  811. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  812. attr_flags |= ATTR_NONBLOCK;
  813. vattr->va_mask = XFS_AT_XFLAGS;
  814. vattr->va_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  815. error = -xfs_setattr(ip, vattr, attr_flags, NULL);
  816. if (likely(!error))
  817. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  818. kfree(vattr);
  819. return error;
  820. }
  821. STATIC int
  822. xfs_ioc_getbmap(
  823. struct xfs_inode *ip,
  824. int ioflags,
  825. unsigned int cmd,
  826. void __user *arg)
  827. {
  828. struct getbmap bm;
  829. int iflags;
  830. int error;
  831. if (copy_from_user(&bm, arg, sizeof(bm)))
  832. return -XFS_ERROR(EFAULT);
  833. if (bm.bmv_count < 2)
  834. return -XFS_ERROR(EINVAL);
  835. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  836. if (ioflags & IO_INVIS)
  837. iflags |= BMV_IF_NO_DMAPI_READ;
  838. error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
  839. if (error)
  840. return -error;
  841. if (copy_to_user(arg, &bm, sizeof(bm)))
  842. return -XFS_ERROR(EFAULT);
  843. return 0;
  844. }
  845. STATIC int
  846. xfs_ioc_getbmapx(
  847. struct xfs_inode *ip,
  848. void __user *arg)
  849. {
  850. struct getbmapx bmx;
  851. struct getbmap bm;
  852. int iflags;
  853. int error;
  854. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  855. return -XFS_ERROR(EFAULT);
  856. if (bmx.bmv_count < 2)
  857. return -XFS_ERROR(EINVAL);
  858. /*
  859. * Map input getbmapx structure to a getbmap
  860. * structure for xfs_getbmap.
  861. */
  862. GETBMAP_CONVERT(bmx, bm);
  863. iflags = bmx.bmv_iflags;
  864. if (iflags & (~BMV_IF_VALID))
  865. return -XFS_ERROR(EINVAL);
  866. iflags |= BMV_IF_EXTENDED;
  867. error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
  868. if (error)
  869. return -error;
  870. GETBMAP_CONVERT(bm, bmx);
  871. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  872. return -XFS_ERROR(EFAULT);
  873. return 0;
  874. }
  875. int
  876. xfs_ioctl(
  877. xfs_inode_t *ip,
  878. struct file *filp,
  879. int ioflags,
  880. unsigned int cmd,
  881. void __user *arg)
  882. {
  883. struct inode *inode = filp->f_path.dentry->d_inode;
  884. xfs_mount_t *mp = ip->i_mount;
  885. int error;
  886. xfs_itrace_entry(XFS_I(inode));
  887. switch (cmd) {
  888. case XFS_IOC_ALLOCSP:
  889. case XFS_IOC_FREESP:
  890. case XFS_IOC_RESVSP:
  891. case XFS_IOC_UNRESVSP:
  892. case XFS_IOC_ALLOCSP64:
  893. case XFS_IOC_FREESP64:
  894. case XFS_IOC_RESVSP64:
  895. case XFS_IOC_UNRESVSP64:
  896. /*
  897. * Only allow the sys admin to reserve space unless
  898. * unwritten extents are enabled.
  899. */
  900. if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
  901. !capable(CAP_SYS_ADMIN))
  902. return -EPERM;
  903. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
  904. case XFS_IOC_DIOINFO: {
  905. struct dioattr da;
  906. xfs_buftarg_t *target =
  907. XFS_IS_REALTIME_INODE(ip) ?
  908. mp->m_rtdev_targp : mp->m_ddev_targp;
  909. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  910. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  911. if (copy_to_user(arg, &da, sizeof(da)))
  912. return -XFS_ERROR(EFAULT);
  913. return 0;
  914. }
  915. case XFS_IOC_FSBULKSTAT_SINGLE:
  916. case XFS_IOC_FSBULKSTAT:
  917. case XFS_IOC_FSINUMBERS:
  918. return xfs_ioc_bulkstat(mp, cmd, arg);
  919. case XFS_IOC_FSGEOMETRY_V1:
  920. return xfs_ioc_fsgeometry_v1(mp, arg);
  921. case XFS_IOC_FSGEOMETRY:
  922. return xfs_ioc_fsgeometry(mp, arg);
  923. case XFS_IOC_GETVERSION:
  924. return put_user(inode->i_generation, (int __user *)arg);
  925. case XFS_IOC_FSGETXATTR:
  926. return xfs_ioc_fsgetxattr(ip, 0, arg);
  927. case XFS_IOC_FSGETXATTRA:
  928. return xfs_ioc_fsgetxattr(ip, 1, arg);
  929. case XFS_IOC_FSSETXATTR:
  930. return xfs_ioc_fssetxattr(ip, filp, arg);
  931. case XFS_IOC_GETXFLAGS:
  932. return xfs_ioc_getxflags(ip, arg);
  933. case XFS_IOC_SETXFLAGS:
  934. return xfs_ioc_setxflags(ip, filp, arg);
  935. case XFS_IOC_FSSETDM: {
  936. struct fsdmidata dmi;
  937. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  938. return -XFS_ERROR(EFAULT);
  939. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  940. dmi.fsd_dmstate);
  941. return -error;
  942. }
  943. case XFS_IOC_GETBMAP:
  944. case XFS_IOC_GETBMAPA:
  945. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  946. case XFS_IOC_GETBMAPX:
  947. return xfs_ioc_getbmapx(ip, arg);
  948. case XFS_IOC_FD_TO_HANDLE:
  949. case XFS_IOC_PATH_TO_HANDLE:
  950. case XFS_IOC_PATH_TO_FSHANDLE:
  951. return xfs_find_handle(cmd, arg);
  952. case XFS_IOC_OPEN_BY_HANDLE:
  953. return xfs_open_by_handle(mp, arg, filp, inode);
  954. case XFS_IOC_FSSETDM_BY_HANDLE:
  955. return xfs_fssetdm_by_handle(mp, arg, inode);
  956. case XFS_IOC_READLINK_BY_HANDLE:
  957. return xfs_readlink_by_handle(mp, arg, inode);
  958. case XFS_IOC_ATTRLIST_BY_HANDLE:
  959. return xfs_attrlist_by_handle(mp, arg, inode);
  960. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  961. return xfs_attrmulti_by_handle(mp, arg, filp, inode);
  962. case XFS_IOC_SWAPEXT: {
  963. error = xfs_swapext((struct xfs_swapext __user *)arg);
  964. return -error;
  965. }
  966. case XFS_IOC_FSCOUNTS: {
  967. xfs_fsop_counts_t out;
  968. error = xfs_fs_counts(mp, &out);
  969. if (error)
  970. return -error;
  971. if (copy_to_user(arg, &out, sizeof(out)))
  972. return -XFS_ERROR(EFAULT);
  973. return 0;
  974. }
  975. case XFS_IOC_SET_RESBLKS: {
  976. xfs_fsop_resblks_t inout;
  977. __uint64_t in;
  978. if (!capable(CAP_SYS_ADMIN))
  979. return -EPERM;
  980. if (copy_from_user(&inout, arg, sizeof(inout)))
  981. return -XFS_ERROR(EFAULT);
  982. /* input parameter is passed in resblks field of structure */
  983. in = inout.resblks;
  984. error = xfs_reserve_blocks(mp, &in, &inout);
  985. if (error)
  986. return -error;
  987. if (copy_to_user(arg, &inout, sizeof(inout)))
  988. return -XFS_ERROR(EFAULT);
  989. return 0;
  990. }
  991. case XFS_IOC_GET_RESBLKS: {
  992. xfs_fsop_resblks_t out;
  993. if (!capable(CAP_SYS_ADMIN))
  994. return -EPERM;
  995. error = xfs_reserve_blocks(mp, NULL, &out);
  996. if (error)
  997. return -error;
  998. if (copy_to_user(arg, &out, sizeof(out)))
  999. return -XFS_ERROR(EFAULT);
  1000. return 0;
  1001. }
  1002. case XFS_IOC_FSGROWFSDATA: {
  1003. xfs_growfs_data_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_data(mp, &in);
  1009. return -error;
  1010. }
  1011. case XFS_IOC_FSGROWFSLOG: {
  1012. xfs_growfs_log_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_log(mp, &in);
  1018. return -error;
  1019. }
  1020. case XFS_IOC_FSGROWFSRT: {
  1021. xfs_growfs_rt_t in;
  1022. if (!capable(CAP_SYS_ADMIN))
  1023. return -EPERM;
  1024. if (copy_from_user(&in, arg, sizeof(in)))
  1025. return -XFS_ERROR(EFAULT);
  1026. error = xfs_growfs_rt(mp, &in);
  1027. return -error;
  1028. }
  1029. case XFS_IOC_FREEZE:
  1030. if (!capable(CAP_SYS_ADMIN))
  1031. return -EPERM;
  1032. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  1033. freeze_bdev(inode->i_sb->s_bdev);
  1034. return 0;
  1035. case XFS_IOC_THAW:
  1036. if (!capable(CAP_SYS_ADMIN))
  1037. return -EPERM;
  1038. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  1039. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  1040. return 0;
  1041. case XFS_IOC_GOINGDOWN: {
  1042. __uint32_t in;
  1043. if (!capable(CAP_SYS_ADMIN))
  1044. return -EPERM;
  1045. if (get_user(in, (__uint32_t __user *)arg))
  1046. return -XFS_ERROR(EFAULT);
  1047. error = xfs_fs_goingdown(mp, in);
  1048. return -error;
  1049. }
  1050. case XFS_IOC_ERROR_INJECTION: {
  1051. xfs_error_injection_t in;
  1052. if (!capable(CAP_SYS_ADMIN))
  1053. return -EPERM;
  1054. if (copy_from_user(&in, arg, sizeof(in)))
  1055. return -XFS_ERROR(EFAULT);
  1056. error = xfs_errortag_add(in.errtag, mp);
  1057. return -error;
  1058. }
  1059. case XFS_IOC_ERROR_CLEARALL:
  1060. if (!capable(CAP_SYS_ADMIN))
  1061. return -EPERM;
  1062. error = xfs_errortag_clearall(mp, 1);
  1063. return -error;
  1064. default:
  1065. return -ENOTTY;
  1066. }
  1067. }