xfs_ioctl.c 35 KB

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