xfs_ioctl.c 35 KB

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