xfs_ioctl.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558
  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 "xfs_quota.h"
  52. #include "xfs_inode_item.h"
  53. #include <linux/capability.h>
  54. #include <linux/dcache.h>
  55. #include <linux/mount.h>
  56. #include <linux/namei.h>
  57. #include <linux/pagemap.h>
  58. /*
  59. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  60. * a file or fs handle.
  61. *
  62. * XFS_IOC_PATH_TO_FSHANDLE
  63. * returns fs handle for a mount point or path within that mount point
  64. * XFS_IOC_FD_TO_HANDLE
  65. * returns full handle for a FD opened in user space
  66. * XFS_IOC_PATH_TO_HANDLE
  67. * returns full handle for a path
  68. */
  69. STATIC int
  70. xfs_find_handle(
  71. unsigned int cmd,
  72. void __user *arg)
  73. {
  74. int hsize;
  75. xfs_handle_t handle;
  76. xfs_fsop_handlereq_t hreq;
  77. struct inode *inode;
  78. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  79. return -XFS_ERROR(EFAULT);
  80. memset((char *)&handle, 0, sizeof(handle));
  81. switch (cmd) {
  82. case XFS_IOC_PATH_TO_FSHANDLE:
  83. case XFS_IOC_PATH_TO_HANDLE: {
  84. struct path path;
  85. int error = user_lpath((const char __user *)hreq.path, &path);
  86. if (error)
  87. return error;
  88. ASSERT(path.dentry);
  89. ASSERT(path.dentry->d_inode);
  90. inode = igrab(path.dentry->d_inode);
  91. path_put(&path);
  92. break;
  93. }
  94. case XFS_IOC_FD_TO_HANDLE: {
  95. struct file *file;
  96. file = fget(hreq.fd);
  97. if (!file)
  98. return -EBADF;
  99. ASSERT(file->f_path.dentry);
  100. ASSERT(file->f_path.dentry->d_inode);
  101. inode = igrab(file->f_path.dentry->d_inode);
  102. fput(file);
  103. break;
  104. }
  105. default:
  106. ASSERT(0);
  107. return -XFS_ERROR(EINVAL);
  108. }
  109. if (inode->i_sb->s_magic != XFS_SB_MAGIC) {
  110. /* we're not in XFS anymore, Toto */
  111. iput(inode);
  112. return -XFS_ERROR(EINVAL);
  113. }
  114. switch (inode->i_mode & S_IFMT) {
  115. case S_IFREG:
  116. case S_IFDIR:
  117. case S_IFLNK:
  118. break;
  119. default:
  120. iput(inode);
  121. return -XFS_ERROR(EBADF);
  122. }
  123. /* now we can grab the fsid */
  124. memcpy(&handle.ha_fsid, XFS_I(inode)->i_mount->m_fixedfsid,
  125. sizeof(xfs_fsid_t));
  126. hsize = sizeof(xfs_fsid_t);
  127. if (cmd != XFS_IOC_PATH_TO_FSHANDLE) {
  128. xfs_inode_t *ip = XFS_I(inode);
  129. int lock_mode;
  130. /* need to get access to the xfs_inode to read the generation */
  131. lock_mode = xfs_ilock_map_shared(ip);
  132. /* fill in fid section of handle from inode */
  133. handle.ha_fid.fid_len = sizeof(xfs_fid_t) -
  134. sizeof(handle.ha_fid.fid_len);
  135. handle.ha_fid.fid_pad = 0;
  136. handle.ha_fid.fid_gen = ip->i_d.di_gen;
  137. handle.ha_fid.fid_ino = ip->i_ino;
  138. xfs_iunlock_map_shared(ip, lock_mode);
  139. hsize = XFS_HSIZE(handle);
  140. }
  141. /* now copy our handle into the user buffer & write out the size */
  142. if (copy_to_user(hreq.ohandle, &handle, hsize) ||
  143. copy_to_user(hreq.ohandlen, &hsize, sizeof(__s32))) {
  144. iput(inode);
  145. return -XFS_ERROR(EFAULT);
  146. }
  147. iput(inode);
  148. return 0;
  149. }
  150. /*
  151. * Convert userspace handle data into inode.
  152. *
  153. * We use the fact that all the fsop_handlereq ioctl calls have a data
  154. * structure argument whose first component is always a xfs_fsop_handlereq_t,
  155. * so we can pass that sub structure into this handy, shared routine.
  156. *
  157. * If no error, caller must always iput the returned inode.
  158. */
  159. STATIC int
  160. xfs_vget_fsop_handlereq(
  161. xfs_mount_t *mp,
  162. struct inode *parinode, /* parent inode pointer */
  163. xfs_fsop_handlereq_t *hreq,
  164. struct inode **inode)
  165. {
  166. void __user *hanp;
  167. size_t hlen;
  168. xfs_fid_t *xfid;
  169. xfs_handle_t *handlep;
  170. xfs_handle_t handle;
  171. xfs_inode_t *ip;
  172. xfs_ino_t ino;
  173. __u32 igen;
  174. int error;
  175. /*
  176. * Only allow handle opens under a directory.
  177. */
  178. if (!S_ISDIR(parinode->i_mode))
  179. return XFS_ERROR(ENOTDIR);
  180. hanp = hreq->ihandle;
  181. hlen = hreq->ihandlen;
  182. handlep = &handle;
  183. if (hlen < sizeof(handlep->ha_fsid) || hlen > sizeof(*handlep))
  184. return XFS_ERROR(EINVAL);
  185. if (copy_from_user(handlep, hanp, hlen))
  186. return XFS_ERROR(EFAULT);
  187. if (hlen < sizeof(*handlep))
  188. memset(((char *)handlep) + hlen, 0, sizeof(*handlep) - hlen);
  189. if (hlen > sizeof(handlep->ha_fsid)) {
  190. if (handlep->ha_fid.fid_len !=
  191. (hlen - sizeof(handlep->ha_fsid) -
  192. sizeof(handlep->ha_fid.fid_len)) ||
  193. handlep->ha_fid.fid_pad)
  194. return XFS_ERROR(EINVAL);
  195. }
  196. /*
  197. * Crack the handle, obtain the inode # & generation #
  198. */
  199. xfid = (struct xfs_fid *)&handlep->ha_fid;
  200. if (xfid->fid_len == sizeof(*xfid) - sizeof(xfid->fid_len)) {
  201. ino = xfid->fid_ino;
  202. igen = xfid->fid_gen;
  203. } else {
  204. return XFS_ERROR(EINVAL);
  205. }
  206. /*
  207. * Get the XFS inode, building a Linux inode to go with it.
  208. */
  209. error = xfs_iget(mp, NULL, ino, 0, XFS_ILOCK_SHARED, &ip, 0);
  210. if (error)
  211. return error;
  212. if (ip == NULL)
  213. return XFS_ERROR(EIO);
  214. if (ip->i_d.di_gen != igen) {
  215. xfs_iput_new(ip, XFS_ILOCK_SHARED);
  216. return XFS_ERROR(ENOENT);
  217. }
  218. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  219. *inode = XFS_ITOV(ip);
  220. return 0;
  221. }
  222. STATIC int
  223. xfs_open_by_handle(
  224. xfs_mount_t *mp,
  225. void __user *arg,
  226. struct file *parfilp,
  227. struct inode *parinode)
  228. {
  229. int error;
  230. int new_fd;
  231. int permflag;
  232. struct file *filp;
  233. struct inode *inode;
  234. struct dentry *dentry;
  235. xfs_fsop_handlereq_t hreq;
  236. if (!capable(CAP_SYS_ADMIN))
  237. return -XFS_ERROR(EPERM);
  238. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  239. return -XFS_ERROR(EFAULT);
  240. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
  241. if (error)
  242. return -error;
  243. /* Restrict xfs_open_by_handle to directories & regular files. */
  244. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  245. iput(inode);
  246. return -XFS_ERROR(EINVAL);
  247. }
  248. #if BITS_PER_LONG != 32
  249. hreq.oflags |= O_LARGEFILE;
  250. #endif
  251. /* Put open permission in namei format. */
  252. permflag = hreq.oflags;
  253. if ((permflag+1) & O_ACCMODE)
  254. permflag++;
  255. if (permflag & O_TRUNC)
  256. permflag |= 2;
  257. if ((!(permflag & O_APPEND) || (permflag & O_TRUNC)) &&
  258. (permflag & FMODE_WRITE) && IS_APPEND(inode)) {
  259. iput(inode);
  260. return -XFS_ERROR(EPERM);
  261. }
  262. if ((permflag & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  263. iput(inode);
  264. return -XFS_ERROR(EACCES);
  265. }
  266. /* Can't write directories. */
  267. if ( S_ISDIR(inode->i_mode) && (permflag & FMODE_WRITE)) {
  268. iput(inode);
  269. return -XFS_ERROR(EISDIR);
  270. }
  271. if ((new_fd = get_unused_fd()) < 0) {
  272. iput(inode);
  273. return new_fd;
  274. }
  275. dentry = d_alloc_anon(inode);
  276. if (dentry == NULL) {
  277. iput(inode);
  278. put_unused_fd(new_fd);
  279. return -XFS_ERROR(ENOMEM);
  280. }
  281. /* Ensure umount returns EBUSY on umounts while this file is open. */
  282. mntget(parfilp->f_path.mnt);
  283. /* Create file pointer. */
  284. filp = dentry_open(dentry, parfilp->f_path.mnt, hreq.oflags);
  285. if (IS_ERR(filp)) {
  286. put_unused_fd(new_fd);
  287. return -XFS_ERROR(-PTR_ERR(filp));
  288. }
  289. if (inode->i_mode & S_IFREG) {
  290. /* invisible operation should not change atime */
  291. filp->f_flags |= O_NOATIME;
  292. filp->f_op = &xfs_invis_file_operations;
  293. }
  294. fd_install(new_fd, filp);
  295. return new_fd;
  296. }
  297. /*
  298. * This is a copy from fs/namei.c:vfs_readlink(), except for removing it's
  299. * unused first argument.
  300. */
  301. STATIC int
  302. do_readlink(
  303. char __user *buffer,
  304. int buflen,
  305. const char *link)
  306. {
  307. int len;
  308. len = PTR_ERR(link);
  309. if (IS_ERR(link))
  310. goto out;
  311. len = strlen(link);
  312. if (len > (unsigned) buflen)
  313. len = buflen;
  314. if (copy_to_user(buffer, link, len))
  315. len = -EFAULT;
  316. out:
  317. return len;
  318. }
  319. STATIC int
  320. xfs_readlink_by_handle(
  321. xfs_mount_t *mp,
  322. void __user *arg,
  323. struct inode *parinode)
  324. {
  325. struct inode *inode;
  326. xfs_fsop_handlereq_t hreq;
  327. __u32 olen;
  328. void *link;
  329. int error;
  330. if (!capable(CAP_SYS_ADMIN))
  331. return -XFS_ERROR(EPERM);
  332. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  333. return -XFS_ERROR(EFAULT);
  334. error = xfs_vget_fsop_handlereq(mp, parinode, &hreq, &inode);
  335. if (error)
  336. return -error;
  337. /* Restrict this handle operation to symlinks only. */
  338. if (!S_ISLNK(inode->i_mode)) {
  339. error = -XFS_ERROR(EINVAL);
  340. goto out_iput;
  341. }
  342. if (copy_from_user(&olen, hreq.ohandlen, sizeof(__u32))) {
  343. error = -XFS_ERROR(EFAULT);
  344. goto out_iput;
  345. }
  346. link = kmalloc(MAXPATHLEN+1, GFP_KERNEL);
  347. if (!link)
  348. goto out_iput;
  349. error = -xfs_readlink(XFS_I(inode), link);
  350. if (error)
  351. goto out_kfree;
  352. error = do_readlink(hreq.ohandle, olen, link);
  353. if (error)
  354. goto out_kfree;
  355. out_kfree:
  356. kfree(link);
  357. out_iput:
  358. iput(inode);
  359. return error;
  360. }
  361. STATIC int
  362. xfs_fssetdm_by_handle(
  363. xfs_mount_t *mp,
  364. void __user *arg,
  365. struct inode *parinode)
  366. {
  367. int error;
  368. struct fsdmidata fsd;
  369. xfs_fsop_setdm_handlereq_t dmhreq;
  370. struct inode *inode;
  371. if (!capable(CAP_MKNOD))
  372. return -XFS_ERROR(EPERM);
  373. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  374. return -XFS_ERROR(EFAULT);
  375. error = xfs_vget_fsop_handlereq(mp, parinode, &dmhreq.hreq, &inode);
  376. if (error)
  377. return -error;
  378. if (IS_IMMUTABLE(inode) || IS_APPEND(inode)) {
  379. error = -XFS_ERROR(EPERM);
  380. goto out;
  381. }
  382. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  383. error = -XFS_ERROR(EFAULT);
  384. goto out;
  385. }
  386. error = -xfs_set_dmattrs(XFS_I(inode), fsd.fsd_dmevmask,
  387. fsd.fsd_dmstate);
  388. out:
  389. iput(inode);
  390. return error;
  391. }
  392. STATIC int
  393. xfs_attrlist_by_handle(
  394. xfs_mount_t *mp,
  395. void __user *arg,
  396. struct inode *parinode)
  397. {
  398. int error;
  399. attrlist_cursor_kern_t *cursor;
  400. xfs_fsop_attrlist_handlereq_t al_hreq;
  401. struct inode *inode;
  402. char *kbuf;
  403. if (!capable(CAP_SYS_ADMIN))
  404. return -XFS_ERROR(EPERM);
  405. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  406. return -XFS_ERROR(EFAULT);
  407. if (al_hreq.buflen > XATTR_LIST_MAX)
  408. return -XFS_ERROR(EINVAL);
  409. /*
  410. * Reject flags, only allow namespaces.
  411. */
  412. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  413. return -XFS_ERROR(EINVAL);
  414. error = xfs_vget_fsop_handlereq(mp, parinode, &al_hreq.hreq, &inode);
  415. if (error)
  416. goto out;
  417. kbuf = kmalloc(al_hreq.buflen, GFP_KERNEL);
  418. if (!kbuf)
  419. goto out_vn_rele;
  420. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  421. error = xfs_attr_list(XFS_I(inode), kbuf, al_hreq.buflen,
  422. al_hreq.flags, cursor);
  423. if (error)
  424. goto out_kfree;
  425. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  426. error = -EFAULT;
  427. out_kfree:
  428. kfree(kbuf);
  429. out_vn_rele:
  430. iput(inode);
  431. out:
  432. return -error;
  433. }
  434. STATIC int
  435. xfs_attrmulti_attr_get(
  436. struct inode *inode,
  437. char *name,
  438. char __user *ubuf,
  439. __uint32_t *len,
  440. __uint32_t flags)
  441. {
  442. char *kbuf;
  443. int error = EFAULT;
  444. if (*len > XATTR_SIZE_MAX)
  445. return EINVAL;
  446. kbuf = kmalloc(*len, GFP_KERNEL);
  447. if (!kbuf)
  448. return ENOMEM;
  449. error = xfs_attr_get(XFS_I(inode), name, kbuf, (int *)len, flags);
  450. if (error)
  451. goto out_kfree;
  452. if (copy_to_user(ubuf, kbuf, *len))
  453. error = EFAULT;
  454. out_kfree:
  455. kfree(kbuf);
  456. return error;
  457. }
  458. STATIC int
  459. xfs_attrmulti_attr_set(
  460. struct inode *inode,
  461. char *name,
  462. const char __user *ubuf,
  463. __uint32_t len,
  464. __uint32_t flags)
  465. {
  466. char *kbuf;
  467. int error = EFAULT;
  468. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  469. return EPERM;
  470. if (len > XATTR_SIZE_MAX)
  471. return EINVAL;
  472. kbuf = kmalloc(len, GFP_KERNEL);
  473. if (!kbuf)
  474. return ENOMEM;
  475. if (copy_from_user(kbuf, ubuf, len))
  476. goto out_kfree;
  477. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  478. out_kfree:
  479. kfree(kbuf);
  480. return error;
  481. }
  482. STATIC int
  483. xfs_attrmulti_attr_remove(
  484. struct inode *inode,
  485. char *name,
  486. __uint32_t flags)
  487. {
  488. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  489. return EPERM;
  490. return xfs_attr_remove(XFS_I(inode), name, flags);
  491. }
  492. STATIC int
  493. xfs_attrmulti_by_handle(
  494. xfs_mount_t *mp,
  495. void __user *arg,
  496. struct file *parfilp,
  497. struct inode *parinode)
  498. {
  499. int error;
  500. xfs_attr_multiop_t *ops;
  501. xfs_fsop_attrmulti_handlereq_t am_hreq;
  502. struct inode *inode;
  503. unsigned int i, size;
  504. char *attr_name;
  505. if (!capable(CAP_SYS_ADMIN))
  506. return -XFS_ERROR(EPERM);
  507. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  508. return -XFS_ERROR(EFAULT);
  509. error = xfs_vget_fsop_handlereq(mp, parinode, &am_hreq.hreq, &inode);
  510. if (error)
  511. goto out;
  512. error = E2BIG;
  513. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  514. if (!size || size > 16 * PAGE_SIZE)
  515. goto out_vn_rele;
  516. error = ENOMEM;
  517. ops = kmalloc(size, GFP_KERNEL);
  518. if (!ops)
  519. goto out_vn_rele;
  520. error = EFAULT;
  521. if (copy_from_user(ops, am_hreq.ops, size))
  522. goto out_kfree_ops;
  523. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  524. if (!attr_name)
  525. goto out_kfree_ops;
  526. error = 0;
  527. for (i = 0; i < am_hreq.opcount; i++) {
  528. ops[i].am_error = strncpy_from_user(attr_name,
  529. ops[i].am_attrname, MAXNAMELEN);
  530. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  531. error = -ERANGE;
  532. if (ops[i].am_error < 0)
  533. break;
  534. switch (ops[i].am_opcode) {
  535. case ATTR_OP_GET:
  536. ops[i].am_error = xfs_attrmulti_attr_get(inode,
  537. attr_name, ops[i].am_attrvalue,
  538. &ops[i].am_length, ops[i].am_flags);
  539. break;
  540. case ATTR_OP_SET:
  541. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  542. if (ops[i].am_error)
  543. break;
  544. ops[i].am_error = xfs_attrmulti_attr_set(inode,
  545. attr_name, ops[i].am_attrvalue,
  546. ops[i].am_length, ops[i].am_flags);
  547. mnt_drop_write(parfilp->f_path.mnt);
  548. break;
  549. case ATTR_OP_REMOVE:
  550. ops[i].am_error = mnt_want_write(parfilp->f_path.mnt);
  551. if (ops[i].am_error)
  552. break;
  553. ops[i].am_error = xfs_attrmulti_attr_remove(inode,
  554. attr_name, ops[i].am_flags);
  555. mnt_drop_write(parfilp->f_path.mnt);
  556. break;
  557. default:
  558. ops[i].am_error = EINVAL;
  559. }
  560. }
  561. if (copy_to_user(am_hreq.ops, ops, size))
  562. error = XFS_ERROR(EFAULT);
  563. kfree(attr_name);
  564. out_kfree_ops:
  565. kfree(ops);
  566. out_vn_rele:
  567. iput(inode);
  568. out:
  569. return -error;
  570. }
  571. STATIC int
  572. xfs_ioc_space(
  573. struct xfs_inode *ip,
  574. struct inode *inode,
  575. struct file *filp,
  576. int ioflags,
  577. unsigned int cmd,
  578. void __user *arg)
  579. {
  580. xfs_flock64_t bf;
  581. int attr_flags = 0;
  582. int error;
  583. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  584. return -XFS_ERROR(EPERM);
  585. if (!(filp->f_mode & FMODE_WRITE))
  586. return -XFS_ERROR(EBADF);
  587. if (!S_ISREG(inode->i_mode))
  588. return -XFS_ERROR(EINVAL);
  589. if (copy_from_user(&bf, arg, sizeof(bf)))
  590. return -XFS_ERROR(EFAULT);
  591. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  592. attr_flags |= ATTR_NONBLOCK;
  593. if (ioflags & IO_INVIS)
  594. attr_flags |= ATTR_DMI;
  595. error = xfs_change_file_space(ip, cmd, &bf, filp->f_pos,
  596. NULL, attr_flags);
  597. return -error;
  598. }
  599. STATIC int
  600. xfs_ioc_bulkstat(
  601. xfs_mount_t *mp,
  602. unsigned int cmd,
  603. void __user *arg)
  604. {
  605. xfs_fsop_bulkreq_t bulkreq;
  606. int count; /* # of records returned */
  607. xfs_ino_t inlast; /* last inode number */
  608. int done;
  609. int error;
  610. /* done = 1 if there are more stats to get and if bulkstat */
  611. /* should be called again (unused here, but used in dmapi) */
  612. if (!capable(CAP_SYS_ADMIN))
  613. return -EPERM;
  614. if (XFS_FORCED_SHUTDOWN(mp))
  615. return -XFS_ERROR(EIO);
  616. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  617. return -XFS_ERROR(EFAULT);
  618. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  619. return -XFS_ERROR(EFAULT);
  620. if ((count = bulkreq.icount) <= 0)
  621. return -XFS_ERROR(EINVAL);
  622. if (bulkreq.ubuffer == NULL)
  623. return -XFS_ERROR(EINVAL);
  624. if (cmd == XFS_IOC_FSINUMBERS)
  625. error = xfs_inumbers(mp, &inlast, &count,
  626. bulkreq.ubuffer, xfs_inumbers_fmt);
  627. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  628. error = xfs_bulkstat_single(mp, &inlast,
  629. bulkreq.ubuffer, &done);
  630. else /* XFS_IOC_FSBULKSTAT */
  631. error = xfs_bulkstat(mp, &inlast, &count,
  632. (bulkstat_one_pf)xfs_bulkstat_one, NULL,
  633. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  634. BULKSTAT_FG_QUICK, &done);
  635. if (error)
  636. return -error;
  637. if (bulkreq.ocount != NULL) {
  638. if (copy_to_user(bulkreq.lastip, &inlast,
  639. sizeof(xfs_ino_t)))
  640. return -XFS_ERROR(EFAULT);
  641. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  642. return -XFS_ERROR(EFAULT);
  643. }
  644. return 0;
  645. }
  646. STATIC int
  647. xfs_ioc_fsgeometry_v1(
  648. xfs_mount_t *mp,
  649. void __user *arg)
  650. {
  651. xfs_fsop_geom_v1_t fsgeo;
  652. int error;
  653. error = xfs_fs_geometry(mp, (xfs_fsop_geom_t *)&fsgeo, 3);
  654. if (error)
  655. return -error;
  656. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  657. return -XFS_ERROR(EFAULT);
  658. return 0;
  659. }
  660. STATIC int
  661. xfs_ioc_fsgeometry(
  662. xfs_mount_t *mp,
  663. void __user *arg)
  664. {
  665. xfs_fsop_geom_t fsgeo;
  666. int error;
  667. error = xfs_fs_geometry(mp, &fsgeo, 4);
  668. if (error)
  669. return -error;
  670. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  671. return -XFS_ERROR(EFAULT);
  672. return 0;
  673. }
  674. /*
  675. * Linux extended inode flags interface.
  676. */
  677. STATIC unsigned int
  678. xfs_merge_ioc_xflags(
  679. unsigned int flags,
  680. unsigned int start)
  681. {
  682. unsigned int xflags = start;
  683. if (flags & FS_IMMUTABLE_FL)
  684. xflags |= XFS_XFLAG_IMMUTABLE;
  685. else
  686. xflags &= ~XFS_XFLAG_IMMUTABLE;
  687. if (flags & FS_APPEND_FL)
  688. xflags |= XFS_XFLAG_APPEND;
  689. else
  690. xflags &= ~XFS_XFLAG_APPEND;
  691. if (flags & FS_SYNC_FL)
  692. xflags |= XFS_XFLAG_SYNC;
  693. else
  694. xflags &= ~XFS_XFLAG_SYNC;
  695. if (flags & FS_NOATIME_FL)
  696. xflags |= XFS_XFLAG_NOATIME;
  697. else
  698. xflags &= ~XFS_XFLAG_NOATIME;
  699. if (flags & FS_NODUMP_FL)
  700. xflags |= XFS_XFLAG_NODUMP;
  701. else
  702. xflags &= ~XFS_XFLAG_NODUMP;
  703. return xflags;
  704. }
  705. STATIC unsigned int
  706. xfs_di2lxflags(
  707. __uint16_t di_flags)
  708. {
  709. unsigned int flags = 0;
  710. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  711. flags |= FS_IMMUTABLE_FL;
  712. if (di_flags & XFS_DIFLAG_APPEND)
  713. flags |= FS_APPEND_FL;
  714. if (di_flags & XFS_DIFLAG_SYNC)
  715. flags |= FS_SYNC_FL;
  716. if (di_flags & XFS_DIFLAG_NOATIME)
  717. flags |= FS_NOATIME_FL;
  718. if (di_flags & XFS_DIFLAG_NODUMP)
  719. flags |= FS_NODUMP_FL;
  720. return flags;
  721. }
  722. STATIC int
  723. xfs_ioc_fsgetxattr(
  724. xfs_inode_t *ip,
  725. int attr,
  726. void __user *arg)
  727. {
  728. struct fsxattr fa;
  729. xfs_ilock(ip, XFS_ILOCK_SHARED);
  730. fa.fsx_xflags = xfs_ip2xflags(ip);
  731. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  732. fa.fsx_projid = ip->i_d.di_projid;
  733. if (attr) {
  734. if (ip->i_afp) {
  735. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  736. fa.fsx_nextents = ip->i_afp->if_bytes /
  737. sizeof(xfs_bmbt_rec_t);
  738. else
  739. fa.fsx_nextents = ip->i_d.di_anextents;
  740. } else
  741. fa.fsx_nextents = 0;
  742. } else {
  743. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  744. fa.fsx_nextents = ip->i_df.if_bytes /
  745. sizeof(xfs_bmbt_rec_t);
  746. else
  747. fa.fsx_nextents = ip->i_d.di_nextents;
  748. }
  749. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  750. if (copy_to_user(arg, &fa, sizeof(fa)))
  751. return -EFAULT;
  752. return 0;
  753. }
  754. STATIC void
  755. xfs_set_diflags(
  756. struct xfs_inode *ip,
  757. unsigned int xflags)
  758. {
  759. unsigned int di_flags;
  760. /* can't set PREALLOC this way, just preserve it */
  761. di_flags = (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  762. if (xflags & XFS_XFLAG_IMMUTABLE)
  763. di_flags |= XFS_DIFLAG_IMMUTABLE;
  764. if (xflags & XFS_XFLAG_APPEND)
  765. di_flags |= XFS_DIFLAG_APPEND;
  766. if (xflags & XFS_XFLAG_SYNC)
  767. di_flags |= XFS_DIFLAG_SYNC;
  768. if (xflags & XFS_XFLAG_NOATIME)
  769. di_flags |= XFS_DIFLAG_NOATIME;
  770. if (xflags & XFS_XFLAG_NODUMP)
  771. di_flags |= XFS_DIFLAG_NODUMP;
  772. if (xflags & XFS_XFLAG_PROJINHERIT)
  773. di_flags |= XFS_DIFLAG_PROJINHERIT;
  774. if (xflags & XFS_XFLAG_NODEFRAG)
  775. di_flags |= XFS_DIFLAG_NODEFRAG;
  776. if (xflags & XFS_XFLAG_FILESTREAM)
  777. di_flags |= XFS_DIFLAG_FILESTREAM;
  778. if ((ip->i_d.di_mode & S_IFMT) == S_IFDIR) {
  779. if (xflags & XFS_XFLAG_RTINHERIT)
  780. di_flags |= XFS_DIFLAG_RTINHERIT;
  781. if (xflags & XFS_XFLAG_NOSYMLINKS)
  782. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  783. if (xflags & XFS_XFLAG_EXTSZINHERIT)
  784. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  785. } else if ((ip->i_d.di_mode & S_IFMT) == S_IFREG) {
  786. if (xflags & XFS_XFLAG_REALTIME)
  787. di_flags |= XFS_DIFLAG_REALTIME;
  788. if (xflags & XFS_XFLAG_EXTSIZE)
  789. di_flags |= XFS_DIFLAG_EXTSIZE;
  790. }
  791. ip->i_d.di_flags = di_flags;
  792. }
  793. #define FSX_PROJID 1
  794. #define FSX_EXTSIZE 2
  795. #define FSX_XFLAGS 4
  796. #define FSX_NONBLOCK 8
  797. STATIC int
  798. xfs_ioctl_setattr(
  799. xfs_inode_t *ip,
  800. struct fsxattr *fa,
  801. int mask)
  802. {
  803. struct xfs_mount *mp = ip->i_mount;
  804. struct xfs_trans *tp;
  805. unsigned int lock_flags = 0;
  806. struct xfs_dquot *udqp = NULL, *gdqp = NULL;
  807. struct xfs_dquot *olddquot = NULL;
  808. int code;
  809. xfs_itrace_entry(ip);
  810. if (mp->m_flags & XFS_MOUNT_RDONLY)
  811. return XFS_ERROR(EROFS);
  812. if (XFS_FORCED_SHUTDOWN(mp))
  813. return XFS_ERROR(EIO);
  814. /*
  815. * If disk quotas is on, we make sure that the dquots do exist on disk,
  816. * before we start any other transactions. Trying to do this later
  817. * is messy. We don't care to take a readlock to look at the ids
  818. * in inode here, because we can't hold it across the trans_reserve.
  819. * If the IDs do change before we take the ilock, we're covered
  820. * because the i_*dquot fields will get updated anyway.
  821. */
  822. if (XFS_IS_QUOTA_ON(mp) && (mask & FSX_PROJID)) {
  823. code = XFS_QM_DQVOPALLOC(mp, ip, ip->i_d.di_uid,
  824. ip->i_d.di_gid, fa->fsx_projid,
  825. XFS_QMOPT_PQUOTA, &udqp, &gdqp);
  826. if (code)
  827. return code;
  828. }
  829. /*
  830. * For the other attributes, we acquire the inode lock and
  831. * first do an error checking pass.
  832. */
  833. tp = xfs_trans_alloc(mp, XFS_TRANS_SETATTR_NOT_SIZE);
  834. code = xfs_trans_reserve(tp, 0, XFS_ICHANGE_LOG_RES(mp), 0, 0, 0);
  835. if (code)
  836. goto error_return;
  837. lock_flags = XFS_ILOCK_EXCL;
  838. xfs_ilock(ip, lock_flags);
  839. /*
  840. * CAP_FOWNER overrides the following restrictions:
  841. *
  842. * The user ID of the calling process must be equal
  843. * to the file owner ID, except in cases where the
  844. * CAP_FSETID capability is applicable.
  845. */
  846. if (current->fsuid != ip->i_d.di_uid && !capable(CAP_FOWNER)) {
  847. code = XFS_ERROR(EPERM);
  848. goto error_return;
  849. }
  850. /*
  851. * Do a quota reservation only if projid is actually going to change.
  852. */
  853. if (mask & FSX_PROJID) {
  854. if (XFS_IS_PQUOTA_ON(mp) &&
  855. ip->i_d.di_projid != fa->fsx_projid) {
  856. ASSERT(tp);
  857. code = XFS_QM_DQVOPCHOWNRESV(mp, tp, ip, udqp, gdqp,
  858. capable(CAP_FOWNER) ?
  859. XFS_QMOPT_FORCE_RES : 0);
  860. if (code) /* out of quota */
  861. goto error_return;
  862. }
  863. }
  864. if (mask & FSX_EXTSIZE) {
  865. /*
  866. * Can't change extent size if any extents are allocated.
  867. */
  868. if (ip->i_d.di_nextents &&
  869. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) !=
  870. fa->fsx_extsize)) {
  871. code = XFS_ERROR(EINVAL); /* EFBIG? */
  872. goto error_return;
  873. }
  874. /*
  875. * Extent size must be a multiple of the appropriate block
  876. * size, if set at all.
  877. */
  878. if (fa->fsx_extsize != 0) {
  879. xfs_extlen_t size;
  880. if (XFS_IS_REALTIME_INODE(ip) ||
  881. ((mask & FSX_XFLAGS) &&
  882. (fa->fsx_xflags & XFS_XFLAG_REALTIME))) {
  883. size = mp->m_sb.sb_rextsize <<
  884. mp->m_sb.sb_blocklog;
  885. } else {
  886. size = mp->m_sb.sb_blocksize;
  887. }
  888. if (fa->fsx_extsize % size) {
  889. code = XFS_ERROR(EINVAL);
  890. goto error_return;
  891. }
  892. }
  893. }
  894. if (mask & FSX_XFLAGS) {
  895. /*
  896. * Can't change realtime flag if any extents are allocated.
  897. */
  898. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  899. (XFS_IS_REALTIME_INODE(ip)) !=
  900. (fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  901. code = XFS_ERROR(EINVAL); /* EFBIG? */
  902. goto error_return;
  903. }
  904. /*
  905. * If realtime flag is set then must have realtime data.
  906. */
  907. if ((fa->fsx_xflags & XFS_XFLAG_REALTIME)) {
  908. if ((mp->m_sb.sb_rblocks == 0) ||
  909. (mp->m_sb.sb_rextsize == 0) ||
  910. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize)) {
  911. code = XFS_ERROR(EINVAL);
  912. goto error_return;
  913. }
  914. }
  915. /*
  916. * Can't modify an immutable/append-only file unless
  917. * we have appropriate permission.
  918. */
  919. if ((ip->i_d.di_flags &
  920. (XFS_DIFLAG_IMMUTABLE|XFS_DIFLAG_APPEND) ||
  921. (fa->fsx_xflags &
  922. (XFS_XFLAG_IMMUTABLE | XFS_XFLAG_APPEND))) &&
  923. !capable(CAP_LINUX_IMMUTABLE)) {
  924. code = XFS_ERROR(EPERM);
  925. goto error_return;
  926. }
  927. }
  928. xfs_trans_ijoin(tp, ip, lock_flags);
  929. xfs_trans_ihold(tp, ip);
  930. /*
  931. * Change file ownership. Must be the owner or privileged.
  932. * If the system was configured with the "restricted_chown"
  933. * option, the owner is not permitted to give away the file,
  934. * and can change the group id only to a group of which he
  935. * or she is a member.
  936. */
  937. if (mask & FSX_PROJID) {
  938. /*
  939. * CAP_FSETID overrides the following restrictions:
  940. *
  941. * The set-user-ID and set-group-ID bits of a file will be
  942. * cleared upon successful return from chown()
  943. */
  944. if ((ip->i_d.di_mode & (S_ISUID|S_ISGID)) &&
  945. !capable(CAP_FSETID))
  946. ip->i_d.di_mode &= ~(S_ISUID|S_ISGID);
  947. /*
  948. * Change the ownerships and register quota modifications
  949. * in the transaction.
  950. */
  951. if (ip->i_d.di_projid != fa->fsx_projid) {
  952. if (XFS_IS_PQUOTA_ON(mp)) {
  953. olddquot = XFS_QM_DQVOPCHOWN(mp, tp, ip,
  954. &ip->i_gdquot, gdqp);
  955. }
  956. ip->i_d.di_projid = fa->fsx_projid;
  957. /*
  958. * We may have to rev the inode as well as
  959. * the superblock version number since projids didn't
  960. * exist before DINODE_VERSION_2 and SB_VERSION_NLINK.
  961. */
  962. if (ip->i_d.di_version == XFS_DINODE_VERSION_1)
  963. xfs_bump_ino_vers2(tp, ip);
  964. }
  965. }
  966. if (mask & FSX_EXTSIZE)
  967. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  968. if (mask & FSX_XFLAGS)
  969. xfs_set_diflags(ip, fa->fsx_xflags);
  970. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  971. xfs_ichgtime(ip, XFS_ICHGTIME_CHG);
  972. XFS_STATS_INC(xs_ig_attrchg);
  973. /*
  974. * If this is a synchronous mount, make sure that the
  975. * transaction goes to disk before returning to the user.
  976. * This is slightly sub-optimal in that truncates require
  977. * two sync transactions instead of one for wsync filesystems.
  978. * One for the truncate and one for the timestamps since we
  979. * don't want to change the timestamps unless we're sure the
  980. * truncate worked. Truncates are less than 1% of the laddis
  981. * mix so this probably isn't worth the trouble to optimize.
  982. */
  983. if (mp->m_flags & XFS_MOUNT_WSYNC)
  984. xfs_trans_set_sync(tp);
  985. code = xfs_trans_commit(tp, 0);
  986. xfs_iunlock(ip, lock_flags);
  987. /*
  988. * Release any dquot(s) the inode had kept before chown.
  989. */
  990. XFS_QM_DQRELE(mp, olddquot);
  991. XFS_QM_DQRELE(mp, udqp);
  992. XFS_QM_DQRELE(mp, gdqp);
  993. if (code)
  994. return code;
  995. if (DM_EVENT_ENABLED(ip, DM_EVENT_ATTRIBUTE)) {
  996. XFS_SEND_NAMESP(mp, DM_EVENT_ATTRIBUTE, ip, DM_RIGHT_NULL,
  997. NULL, DM_RIGHT_NULL, NULL, NULL, 0, 0,
  998. (mask & FSX_NONBLOCK) ? DM_FLAGS_NDELAY : 0);
  999. }
  1000. vn_revalidate(XFS_ITOV(ip)); /* update flags */
  1001. return 0;
  1002. error_return:
  1003. XFS_QM_DQRELE(mp, udqp);
  1004. XFS_QM_DQRELE(mp, gdqp);
  1005. xfs_trans_cancel(tp, 0);
  1006. if (lock_flags)
  1007. xfs_iunlock(ip, lock_flags);
  1008. return code;
  1009. }
  1010. STATIC int
  1011. xfs_ioc_fssetxattr(
  1012. xfs_inode_t *ip,
  1013. struct file *filp,
  1014. void __user *arg)
  1015. {
  1016. struct fsxattr fa;
  1017. unsigned int mask;
  1018. if (copy_from_user(&fa, arg, sizeof(fa)))
  1019. return -EFAULT;
  1020. mask = FSX_XFLAGS | FSX_EXTSIZE | FSX_PROJID;
  1021. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1022. mask |= FSX_NONBLOCK;
  1023. return -xfs_ioctl_setattr(ip, &fa, mask);
  1024. }
  1025. STATIC int
  1026. xfs_ioc_getxflags(
  1027. xfs_inode_t *ip,
  1028. void __user *arg)
  1029. {
  1030. unsigned int flags;
  1031. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1032. if (copy_to_user(arg, &flags, sizeof(flags)))
  1033. return -EFAULT;
  1034. return 0;
  1035. }
  1036. STATIC int
  1037. xfs_ioc_setxflags(
  1038. xfs_inode_t *ip,
  1039. struct file *filp,
  1040. void __user *arg)
  1041. {
  1042. struct fsxattr fa;
  1043. unsigned int flags;
  1044. unsigned int mask;
  1045. if (copy_from_user(&flags, arg, sizeof(flags)))
  1046. return -EFAULT;
  1047. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1048. FS_NOATIME_FL | FS_NODUMP_FL | \
  1049. FS_SYNC_FL))
  1050. return -EOPNOTSUPP;
  1051. mask = FSX_XFLAGS;
  1052. if (filp->f_flags & (O_NDELAY|O_NONBLOCK))
  1053. mask |= FSX_NONBLOCK;
  1054. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1055. return -xfs_ioctl_setattr(ip, &fa, mask);
  1056. }
  1057. STATIC int
  1058. xfs_ioc_getbmap(
  1059. struct xfs_inode *ip,
  1060. int ioflags,
  1061. unsigned int cmd,
  1062. void __user *arg)
  1063. {
  1064. struct getbmap bm;
  1065. int iflags;
  1066. int error;
  1067. if (copy_from_user(&bm, arg, sizeof(bm)))
  1068. return -XFS_ERROR(EFAULT);
  1069. if (bm.bmv_count < 2)
  1070. return -XFS_ERROR(EINVAL);
  1071. iflags = (cmd == XFS_IOC_GETBMAPA ? BMV_IF_ATTRFORK : 0);
  1072. if (ioflags & IO_INVIS)
  1073. iflags |= BMV_IF_NO_DMAPI_READ;
  1074. error = xfs_getbmap(ip, &bm, (struct getbmap __user *)arg+1, iflags);
  1075. if (error)
  1076. return -error;
  1077. if (copy_to_user(arg, &bm, sizeof(bm)))
  1078. return -XFS_ERROR(EFAULT);
  1079. return 0;
  1080. }
  1081. STATIC int
  1082. xfs_ioc_getbmapx(
  1083. struct xfs_inode *ip,
  1084. void __user *arg)
  1085. {
  1086. struct getbmapx bmx;
  1087. struct getbmap bm;
  1088. int iflags;
  1089. int error;
  1090. if (copy_from_user(&bmx, arg, sizeof(bmx)))
  1091. return -XFS_ERROR(EFAULT);
  1092. if (bmx.bmv_count < 2)
  1093. return -XFS_ERROR(EINVAL);
  1094. /*
  1095. * Map input getbmapx structure to a getbmap
  1096. * structure for xfs_getbmap.
  1097. */
  1098. GETBMAP_CONVERT(bmx, bm);
  1099. iflags = bmx.bmv_iflags;
  1100. if (iflags & (~BMV_IF_VALID))
  1101. return -XFS_ERROR(EINVAL);
  1102. iflags |= BMV_IF_EXTENDED;
  1103. error = xfs_getbmap(ip, &bm, (struct getbmapx __user *)arg+1, iflags);
  1104. if (error)
  1105. return -error;
  1106. GETBMAP_CONVERT(bm, bmx);
  1107. if (copy_to_user(arg, &bmx, sizeof(bmx)))
  1108. return -XFS_ERROR(EFAULT);
  1109. return 0;
  1110. }
  1111. int
  1112. xfs_ioctl(
  1113. xfs_inode_t *ip,
  1114. struct file *filp,
  1115. int ioflags,
  1116. unsigned int cmd,
  1117. void __user *arg)
  1118. {
  1119. struct inode *inode = filp->f_path.dentry->d_inode;
  1120. xfs_mount_t *mp = ip->i_mount;
  1121. int error;
  1122. xfs_itrace_entry(XFS_I(inode));
  1123. switch (cmd) {
  1124. case XFS_IOC_ALLOCSP:
  1125. case XFS_IOC_FREESP:
  1126. case XFS_IOC_RESVSP:
  1127. case XFS_IOC_UNRESVSP:
  1128. case XFS_IOC_ALLOCSP64:
  1129. case XFS_IOC_FREESP64:
  1130. case XFS_IOC_RESVSP64:
  1131. case XFS_IOC_UNRESVSP64:
  1132. /*
  1133. * Only allow the sys admin to reserve space unless
  1134. * unwritten extents are enabled.
  1135. */
  1136. if (!xfs_sb_version_hasextflgbit(&mp->m_sb) &&
  1137. !capable(CAP_SYS_ADMIN))
  1138. return -EPERM;
  1139. return xfs_ioc_space(ip, inode, filp, ioflags, cmd, arg);
  1140. case XFS_IOC_DIOINFO: {
  1141. struct dioattr da;
  1142. xfs_buftarg_t *target =
  1143. XFS_IS_REALTIME_INODE(ip) ?
  1144. mp->m_rtdev_targp : mp->m_ddev_targp;
  1145. da.d_mem = da.d_miniosz = 1 << target->bt_sshift;
  1146. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1147. if (copy_to_user(arg, &da, sizeof(da)))
  1148. return -XFS_ERROR(EFAULT);
  1149. return 0;
  1150. }
  1151. case XFS_IOC_FSBULKSTAT_SINGLE:
  1152. case XFS_IOC_FSBULKSTAT:
  1153. case XFS_IOC_FSINUMBERS:
  1154. return xfs_ioc_bulkstat(mp, cmd, arg);
  1155. case XFS_IOC_FSGEOMETRY_V1:
  1156. return xfs_ioc_fsgeometry_v1(mp, arg);
  1157. case XFS_IOC_FSGEOMETRY:
  1158. return xfs_ioc_fsgeometry(mp, arg);
  1159. case XFS_IOC_GETVERSION:
  1160. return put_user(inode->i_generation, (int __user *)arg);
  1161. case XFS_IOC_FSGETXATTR:
  1162. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1163. case XFS_IOC_FSGETXATTRA:
  1164. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1165. case XFS_IOC_FSSETXATTR:
  1166. return xfs_ioc_fssetxattr(ip, filp, arg);
  1167. case XFS_IOC_GETXFLAGS:
  1168. return xfs_ioc_getxflags(ip, arg);
  1169. case XFS_IOC_SETXFLAGS:
  1170. return xfs_ioc_setxflags(ip, filp, arg);
  1171. case XFS_IOC_FSSETDM: {
  1172. struct fsdmidata dmi;
  1173. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1174. return -XFS_ERROR(EFAULT);
  1175. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1176. dmi.fsd_dmstate);
  1177. return -error;
  1178. }
  1179. case XFS_IOC_GETBMAP:
  1180. case XFS_IOC_GETBMAPA:
  1181. return xfs_ioc_getbmap(ip, ioflags, cmd, arg);
  1182. case XFS_IOC_GETBMAPX:
  1183. return xfs_ioc_getbmapx(ip, arg);
  1184. case XFS_IOC_FD_TO_HANDLE:
  1185. case XFS_IOC_PATH_TO_HANDLE:
  1186. case XFS_IOC_PATH_TO_FSHANDLE:
  1187. return xfs_find_handle(cmd, arg);
  1188. case XFS_IOC_OPEN_BY_HANDLE:
  1189. return xfs_open_by_handle(mp, arg, filp, inode);
  1190. case XFS_IOC_FSSETDM_BY_HANDLE:
  1191. return xfs_fssetdm_by_handle(mp, arg, inode);
  1192. case XFS_IOC_READLINK_BY_HANDLE:
  1193. return xfs_readlink_by_handle(mp, arg, inode);
  1194. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1195. return xfs_attrlist_by_handle(mp, arg, inode);
  1196. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1197. return xfs_attrmulti_by_handle(mp, arg, filp, inode);
  1198. case XFS_IOC_SWAPEXT: {
  1199. error = xfs_swapext((struct xfs_swapext __user *)arg);
  1200. return -error;
  1201. }
  1202. case XFS_IOC_FSCOUNTS: {
  1203. xfs_fsop_counts_t out;
  1204. error = xfs_fs_counts(mp, &out);
  1205. if (error)
  1206. return -error;
  1207. if (copy_to_user(arg, &out, sizeof(out)))
  1208. return -XFS_ERROR(EFAULT);
  1209. return 0;
  1210. }
  1211. case XFS_IOC_SET_RESBLKS: {
  1212. xfs_fsop_resblks_t inout;
  1213. __uint64_t in;
  1214. if (!capable(CAP_SYS_ADMIN))
  1215. return -EPERM;
  1216. if (copy_from_user(&inout, arg, sizeof(inout)))
  1217. return -XFS_ERROR(EFAULT);
  1218. /* input parameter is passed in resblks field of structure */
  1219. in = inout.resblks;
  1220. error = xfs_reserve_blocks(mp, &in, &inout);
  1221. if (error)
  1222. return -error;
  1223. if (copy_to_user(arg, &inout, sizeof(inout)))
  1224. return -XFS_ERROR(EFAULT);
  1225. return 0;
  1226. }
  1227. case XFS_IOC_GET_RESBLKS: {
  1228. xfs_fsop_resblks_t out;
  1229. if (!capable(CAP_SYS_ADMIN))
  1230. return -EPERM;
  1231. error = xfs_reserve_blocks(mp, NULL, &out);
  1232. if (error)
  1233. return -error;
  1234. if (copy_to_user(arg, &out, sizeof(out)))
  1235. return -XFS_ERROR(EFAULT);
  1236. return 0;
  1237. }
  1238. case XFS_IOC_FSGROWFSDATA: {
  1239. xfs_growfs_data_t in;
  1240. if (!capable(CAP_SYS_ADMIN))
  1241. return -EPERM;
  1242. if (copy_from_user(&in, arg, sizeof(in)))
  1243. return -XFS_ERROR(EFAULT);
  1244. error = xfs_growfs_data(mp, &in);
  1245. return -error;
  1246. }
  1247. case XFS_IOC_FSGROWFSLOG: {
  1248. xfs_growfs_log_t in;
  1249. if (!capable(CAP_SYS_ADMIN))
  1250. return -EPERM;
  1251. if (copy_from_user(&in, arg, sizeof(in)))
  1252. return -XFS_ERROR(EFAULT);
  1253. error = xfs_growfs_log(mp, &in);
  1254. return -error;
  1255. }
  1256. case XFS_IOC_FSGROWFSRT: {
  1257. xfs_growfs_rt_t in;
  1258. if (!capable(CAP_SYS_ADMIN))
  1259. return -EPERM;
  1260. if (copy_from_user(&in, arg, sizeof(in)))
  1261. return -XFS_ERROR(EFAULT);
  1262. error = xfs_growfs_rt(mp, &in);
  1263. return -error;
  1264. }
  1265. case XFS_IOC_FREEZE:
  1266. if (!capable(CAP_SYS_ADMIN))
  1267. return -EPERM;
  1268. if (inode->i_sb->s_frozen == SB_UNFROZEN)
  1269. freeze_bdev(inode->i_sb->s_bdev);
  1270. return 0;
  1271. case XFS_IOC_THAW:
  1272. if (!capable(CAP_SYS_ADMIN))
  1273. return -EPERM;
  1274. if (inode->i_sb->s_frozen != SB_UNFROZEN)
  1275. thaw_bdev(inode->i_sb->s_bdev, inode->i_sb);
  1276. return 0;
  1277. case XFS_IOC_GOINGDOWN: {
  1278. __uint32_t in;
  1279. if (!capable(CAP_SYS_ADMIN))
  1280. return -EPERM;
  1281. if (get_user(in, (__uint32_t __user *)arg))
  1282. return -XFS_ERROR(EFAULT);
  1283. error = xfs_fs_goingdown(mp, in);
  1284. return -error;
  1285. }
  1286. case XFS_IOC_ERROR_INJECTION: {
  1287. xfs_error_injection_t in;
  1288. if (!capable(CAP_SYS_ADMIN))
  1289. return -EPERM;
  1290. if (copy_from_user(&in, arg, sizeof(in)))
  1291. return -XFS_ERROR(EFAULT);
  1292. error = xfs_errortag_add(in.errtag, mp);
  1293. return -error;
  1294. }
  1295. case XFS_IOC_ERROR_CLEARALL:
  1296. if (!capable(CAP_SYS_ADMIN))
  1297. return -EPERM;
  1298. error = xfs_errortag_clearall(mp, 1);
  1299. return -error;
  1300. default:
  1301. return -ENOTTY;
  1302. }
  1303. }