xfs_ioctl.c 35 KB

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