xfs_ioctl.c 35 KB

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