PageRenderTime 28ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/fs/xfs/xfs_ioctl.c

https://bitbucket.org/emiliolopez/linux
C | 2114 lines | 1577 code | 330 blank | 207 comment | 329 complexity | 45ce01e4f9613b6ca56f5ed02c85898b MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0
  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_shared.h"
  21. #include "xfs_format.h"
  22. #include "xfs_log_format.h"
  23. #include "xfs_trans_resv.h"
  24. #include "xfs_mount.h"
  25. #include "xfs_inode.h"
  26. #include "xfs_ioctl.h"
  27. #include "xfs_alloc.h"
  28. #include "xfs_rtalloc.h"
  29. #include "xfs_itable.h"
  30. #include "xfs_error.h"
  31. #include "xfs_attr.h"
  32. #include "xfs_bmap.h"
  33. #include "xfs_bmap_util.h"
  34. #include "xfs_fsops.h"
  35. #include "xfs_discard.h"
  36. #include "xfs_quota.h"
  37. #include "xfs_export.h"
  38. #include "xfs_trace.h"
  39. #include "xfs_icache.h"
  40. #include "xfs_symlink.h"
  41. #include "xfs_trans.h"
  42. #include "xfs_pnfs.h"
  43. #include "xfs_acl.h"
  44. #include "xfs_btree.h"
  45. #include <linux/fsmap.h>
  46. #include "xfs_fsmap.h"
  47. #include "scrub/xfs_scrub.h"
  48. #include <linux/capability.h>
  49. #include <linux/cred.h>
  50. #include <linux/dcache.h>
  51. #include <linux/mount.h>
  52. #include <linux/namei.h>
  53. #include <linux/pagemap.h>
  54. #include <linux/slab.h>
  55. #include <linux/exportfs.h>
  56. /*
  57. * xfs_find_handle maps from userspace xfs_fsop_handlereq structure to
  58. * a file or fs handle.
  59. *
  60. * XFS_IOC_PATH_TO_FSHANDLE
  61. * returns fs handle for a mount point or path within that mount point
  62. * XFS_IOC_FD_TO_HANDLE
  63. * returns full handle for a FD opened in user space
  64. * XFS_IOC_PATH_TO_HANDLE
  65. * returns full handle for a path
  66. */
  67. int
  68. xfs_find_handle(
  69. unsigned int cmd,
  70. xfs_fsop_handlereq_t *hreq)
  71. {
  72. int hsize;
  73. xfs_handle_t handle;
  74. struct inode *inode;
  75. struct fd f = {NULL};
  76. struct path path;
  77. int error;
  78. struct xfs_inode *ip;
  79. if (cmd == XFS_IOC_FD_TO_HANDLE) {
  80. f = fdget(hreq->fd);
  81. if (!f.file)
  82. return -EBADF;
  83. inode = file_inode(f.file);
  84. } else {
  85. error = user_lpath((const char __user *)hreq->path, &path);
  86. if (error)
  87. return error;
  88. inode = d_inode(path.dentry);
  89. }
  90. ip = XFS_I(inode);
  91. /*
  92. * We can only generate handles for inodes residing on a XFS filesystem,
  93. * and only for regular files, directories or symbolic links.
  94. */
  95. error = -EINVAL;
  96. if (inode->i_sb->s_magic != XFS_SB_MAGIC)
  97. goto out_put;
  98. error = -EBADF;
  99. if (!S_ISREG(inode->i_mode) &&
  100. !S_ISDIR(inode->i_mode) &&
  101. !S_ISLNK(inode->i_mode))
  102. goto out_put;
  103. memcpy(&handle.ha_fsid, ip->i_mount->m_fixedfsid, sizeof(xfs_fsid_t));
  104. if (cmd == XFS_IOC_PATH_TO_FSHANDLE) {
  105. /*
  106. * This handle only contains an fsid, zero the rest.
  107. */
  108. memset(&handle.ha_fid, 0, sizeof(handle.ha_fid));
  109. hsize = sizeof(xfs_fsid_t);
  110. } else {
  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 = inode->i_generation;
  115. handle.ha_fid.fid_ino = ip->i_ino;
  116. hsize = sizeof(xfs_handle_t);
  117. }
  118. error = -EFAULT;
  119. if (copy_to_user(hreq->ohandle, &handle, hsize) ||
  120. copy_to_user(hreq->ohandlen, &hsize, sizeof(__s32)))
  121. goto out_put;
  122. error = 0;
  123. out_put:
  124. if (cmd == XFS_IOC_FD_TO_HANDLE)
  125. fdput(f);
  126. else
  127. path_put(&path);
  128. return error;
  129. }
  130. /*
  131. * No need to do permission checks on the various pathname components
  132. * as the handle operations are privileged.
  133. */
  134. STATIC int
  135. xfs_handle_acceptable(
  136. void *context,
  137. struct dentry *dentry)
  138. {
  139. return 1;
  140. }
  141. /*
  142. * Convert userspace handle data into a dentry.
  143. */
  144. struct dentry *
  145. xfs_handle_to_dentry(
  146. struct file *parfilp,
  147. void __user *uhandle,
  148. u32 hlen)
  149. {
  150. xfs_handle_t handle;
  151. struct xfs_fid64 fid;
  152. /*
  153. * Only allow handle opens under a directory.
  154. */
  155. if (!S_ISDIR(file_inode(parfilp)->i_mode))
  156. return ERR_PTR(-ENOTDIR);
  157. if (hlen != sizeof(xfs_handle_t))
  158. return ERR_PTR(-EINVAL);
  159. if (copy_from_user(&handle, uhandle, hlen))
  160. return ERR_PTR(-EFAULT);
  161. if (handle.ha_fid.fid_len !=
  162. sizeof(handle.ha_fid) - sizeof(handle.ha_fid.fid_len))
  163. return ERR_PTR(-EINVAL);
  164. memset(&fid, 0, sizeof(struct fid));
  165. fid.ino = handle.ha_fid.fid_ino;
  166. fid.gen = handle.ha_fid.fid_gen;
  167. return exportfs_decode_fh(parfilp->f_path.mnt, (struct fid *)&fid, 3,
  168. FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG,
  169. xfs_handle_acceptable, NULL);
  170. }
  171. STATIC struct dentry *
  172. xfs_handlereq_to_dentry(
  173. struct file *parfilp,
  174. xfs_fsop_handlereq_t *hreq)
  175. {
  176. return xfs_handle_to_dentry(parfilp, hreq->ihandle, hreq->ihandlen);
  177. }
  178. int
  179. xfs_open_by_handle(
  180. struct file *parfilp,
  181. xfs_fsop_handlereq_t *hreq)
  182. {
  183. const struct cred *cred = current_cred();
  184. int error;
  185. int fd;
  186. int permflag;
  187. struct file *filp;
  188. struct inode *inode;
  189. struct dentry *dentry;
  190. fmode_t fmode;
  191. struct path path;
  192. if (!capable(CAP_SYS_ADMIN))
  193. return -EPERM;
  194. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  195. if (IS_ERR(dentry))
  196. return PTR_ERR(dentry);
  197. inode = d_inode(dentry);
  198. /* Restrict xfs_open_by_handle to directories & regular files. */
  199. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode))) {
  200. 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 = -EPERM;
  211. goto out_dput;
  212. }
  213. if ((fmode & FMODE_WRITE) && IS_IMMUTABLE(inode)) {
  214. error = -EPERM;
  215. goto out_dput;
  216. }
  217. /* Can't write directories. */
  218. if (S_ISDIR(inode->i_mode) && (fmode & FMODE_WRITE)) {
  219. error = -EISDIR;
  220. goto out_dput;
  221. }
  222. fd = get_unused_fd_flags(0);
  223. if (fd < 0) {
  224. error = fd;
  225. goto out_dput;
  226. }
  227. path.mnt = parfilp->f_path.mnt;
  228. path.dentry = dentry;
  229. filp = dentry_open(&path, hreq->oflags, cred);
  230. dput(dentry);
  231. if (IS_ERR(filp)) {
  232. put_unused_fd(fd);
  233. return PTR_ERR(filp);
  234. }
  235. if (S_ISREG(inode->i_mode)) {
  236. filp->f_flags |= O_NOATIME;
  237. filp->f_mode |= FMODE_NOCMTIME;
  238. }
  239. fd_install(fd, filp);
  240. return fd;
  241. out_dput:
  242. dput(dentry);
  243. return error;
  244. }
  245. int
  246. xfs_readlink_by_handle(
  247. struct file *parfilp,
  248. xfs_fsop_handlereq_t *hreq)
  249. {
  250. struct dentry *dentry;
  251. __u32 olen;
  252. int error;
  253. if (!capable(CAP_SYS_ADMIN))
  254. return -EPERM;
  255. dentry = xfs_handlereq_to_dentry(parfilp, hreq);
  256. if (IS_ERR(dentry))
  257. return PTR_ERR(dentry);
  258. /* Restrict this handle operation to symlinks only. */
  259. if (!d_is_symlink(dentry)) {
  260. error = -EINVAL;
  261. goto out_dput;
  262. }
  263. if (copy_from_user(&olen, hreq->ohandlen, sizeof(__u32))) {
  264. error = -EFAULT;
  265. goto out_dput;
  266. }
  267. error = vfs_readlink(dentry, hreq->ohandle, olen);
  268. out_dput:
  269. dput(dentry);
  270. return error;
  271. }
  272. int
  273. xfs_set_dmattrs(
  274. xfs_inode_t *ip,
  275. uint evmask,
  276. uint16_t state)
  277. {
  278. xfs_mount_t *mp = ip->i_mount;
  279. xfs_trans_t *tp;
  280. int error;
  281. if (!capable(CAP_SYS_ADMIN))
  282. return -EPERM;
  283. if (XFS_FORCED_SHUTDOWN(mp))
  284. return -EIO;
  285. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  286. if (error)
  287. return error;
  288. xfs_ilock(ip, XFS_ILOCK_EXCL);
  289. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL);
  290. ip->i_d.di_dmevmask = evmask;
  291. ip->i_d.di_dmstate = state;
  292. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  293. error = xfs_trans_commit(tp);
  294. return error;
  295. }
  296. STATIC int
  297. xfs_fssetdm_by_handle(
  298. struct file *parfilp,
  299. void __user *arg)
  300. {
  301. int error;
  302. struct fsdmidata fsd;
  303. xfs_fsop_setdm_handlereq_t dmhreq;
  304. struct dentry *dentry;
  305. if (!capable(CAP_MKNOD))
  306. return -EPERM;
  307. if (copy_from_user(&dmhreq, arg, sizeof(xfs_fsop_setdm_handlereq_t)))
  308. return -EFAULT;
  309. error = mnt_want_write_file(parfilp);
  310. if (error)
  311. return error;
  312. dentry = xfs_handlereq_to_dentry(parfilp, &dmhreq.hreq);
  313. if (IS_ERR(dentry)) {
  314. mnt_drop_write_file(parfilp);
  315. return PTR_ERR(dentry);
  316. }
  317. if (IS_IMMUTABLE(d_inode(dentry)) || IS_APPEND(d_inode(dentry))) {
  318. error = -EPERM;
  319. goto out;
  320. }
  321. if (copy_from_user(&fsd, dmhreq.data, sizeof(fsd))) {
  322. error = -EFAULT;
  323. goto out;
  324. }
  325. error = xfs_set_dmattrs(XFS_I(d_inode(dentry)), fsd.fsd_dmevmask,
  326. fsd.fsd_dmstate);
  327. out:
  328. mnt_drop_write_file(parfilp);
  329. dput(dentry);
  330. return error;
  331. }
  332. STATIC int
  333. xfs_attrlist_by_handle(
  334. struct file *parfilp,
  335. void __user *arg)
  336. {
  337. int error = -ENOMEM;
  338. attrlist_cursor_kern_t *cursor;
  339. struct xfs_fsop_attrlist_handlereq __user *p = arg;
  340. xfs_fsop_attrlist_handlereq_t al_hreq;
  341. struct dentry *dentry;
  342. char *kbuf;
  343. if (!capable(CAP_SYS_ADMIN))
  344. return -EPERM;
  345. if (copy_from_user(&al_hreq, arg, sizeof(xfs_fsop_attrlist_handlereq_t)))
  346. return -EFAULT;
  347. if (al_hreq.buflen < sizeof(struct attrlist) ||
  348. al_hreq.buflen > XFS_XATTR_LIST_MAX)
  349. return -EINVAL;
  350. /*
  351. * Reject flags, only allow namespaces.
  352. */
  353. if (al_hreq.flags & ~(ATTR_ROOT | ATTR_SECURE))
  354. return -EINVAL;
  355. dentry = xfs_handlereq_to_dentry(parfilp, &al_hreq.hreq);
  356. if (IS_ERR(dentry))
  357. return PTR_ERR(dentry);
  358. kbuf = kmem_zalloc_large(al_hreq.buflen, KM_SLEEP);
  359. if (!kbuf)
  360. goto out_dput;
  361. cursor = (attrlist_cursor_kern_t *)&al_hreq.pos;
  362. error = xfs_attr_list(XFS_I(d_inode(dentry)), kbuf, al_hreq.buflen,
  363. al_hreq.flags, cursor);
  364. if (error)
  365. goto out_kfree;
  366. if (copy_to_user(&p->pos, cursor, sizeof(attrlist_cursor_kern_t))) {
  367. error = -EFAULT;
  368. goto out_kfree;
  369. }
  370. if (copy_to_user(al_hreq.buffer, kbuf, al_hreq.buflen))
  371. error = -EFAULT;
  372. out_kfree:
  373. kmem_free(kbuf);
  374. out_dput:
  375. dput(dentry);
  376. return error;
  377. }
  378. int
  379. xfs_attrmulti_attr_get(
  380. struct inode *inode,
  381. unsigned char *name,
  382. unsigned char __user *ubuf,
  383. uint32_t *len,
  384. uint32_t flags)
  385. {
  386. unsigned char *kbuf;
  387. int error = -EFAULT;
  388. if (*len > XFS_XATTR_SIZE_MAX)
  389. return -EINVAL;
  390. kbuf = kmem_zalloc_large(*len, KM_SLEEP);
  391. if (!kbuf)
  392. return -ENOMEM;
  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. kmem_free(kbuf);
  400. return error;
  401. }
  402. int
  403. xfs_attrmulti_attr_set(
  404. struct inode *inode,
  405. unsigned char *name,
  406. const unsigned char __user *ubuf,
  407. uint32_t len,
  408. uint32_t flags)
  409. {
  410. unsigned char *kbuf;
  411. int error;
  412. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  413. return -EPERM;
  414. if (len > XFS_XATTR_SIZE_MAX)
  415. return -EINVAL;
  416. kbuf = memdup_user(ubuf, len);
  417. if (IS_ERR(kbuf))
  418. return PTR_ERR(kbuf);
  419. error = xfs_attr_set(XFS_I(inode), name, kbuf, len, flags);
  420. if (!error)
  421. xfs_forget_acl(inode, name, flags);
  422. kfree(kbuf);
  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. int error;
  432. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  433. return -EPERM;
  434. error = xfs_attr_remove(XFS_I(inode), name, flags);
  435. if (!error)
  436. xfs_forget_acl(inode, name, flags);
  437. return error;
  438. }
  439. STATIC int
  440. xfs_attrmulti_by_handle(
  441. struct file *parfilp,
  442. void __user *arg)
  443. {
  444. int error;
  445. xfs_attr_multiop_t *ops;
  446. xfs_fsop_attrmulti_handlereq_t am_hreq;
  447. struct dentry *dentry;
  448. unsigned int i, size;
  449. unsigned char *attr_name;
  450. if (!capable(CAP_SYS_ADMIN))
  451. return -EPERM;
  452. if (copy_from_user(&am_hreq, arg, sizeof(xfs_fsop_attrmulti_handlereq_t)))
  453. return -EFAULT;
  454. /* overflow check */
  455. if (am_hreq.opcount >= INT_MAX / sizeof(xfs_attr_multiop_t))
  456. return -E2BIG;
  457. dentry = xfs_handlereq_to_dentry(parfilp, &am_hreq.hreq);
  458. if (IS_ERR(dentry))
  459. return PTR_ERR(dentry);
  460. error = -E2BIG;
  461. size = am_hreq.opcount * sizeof(xfs_attr_multiop_t);
  462. if (!size || size > 16 * PAGE_SIZE)
  463. goto out_dput;
  464. ops = memdup_user(am_hreq.ops, size);
  465. if (IS_ERR(ops)) {
  466. error = PTR_ERR(ops);
  467. goto out_dput;
  468. }
  469. error = -ENOMEM;
  470. attr_name = kmalloc(MAXNAMELEN, GFP_KERNEL);
  471. if (!attr_name)
  472. goto out_kfree_ops;
  473. error = 0;
  474. for (i = 0; i < am_hreq.opcount; i++) {
  475. ops[i].am_error = strncpy_from_user((char *)attr_name,
  476. ops[i].am_attrname, MAXNAMELEN);
  477. if (ops[i].am_error == 0 || ops[i].am_error == MAXNAMELEN)
  478. error = -ERANGE;
  479. if (ops[i].am_error < 0)
  480. break;
  481. switch (ops[i].am_opcode) {
  482. case ATTR_OP_GET:
  483. ops[i].am_error = xfs_attrmulti_attr_get(
  484. d_inode(dentry), attr_name,
  485. ops[i].am_attrvalue, &ops[i].am_length,
  486. ops[i].am_flags);
  487. break;
  488. case ATTR_OP_SET:
  489. ops[i].am_error = mnt_want_write_file(parfilp);
  490. if (ops[i].am_error)
  491. break;
  492. ops[i].am_error = xfs_attrmulti_attr_set(
  493. d_inode(dentry), attr_name,
  494. ops[i].am_attrvalue, ops[i].am_length,
  495. ops[i].am_flags);
  496. mnt_drop_write_file(parfilp);
  497. break;
  498. case ATTR_OP_REMOVE:
  499. ops[i].am_error = mnt_want_write_file(parfilp);
  500. if (ops[i].am_error)
  501. break;
  502. ops[i].am_error = xfs_attrmulti_attr_remove(
  503. d_inode(dentry), attr_name,
  504. ops[i].am_flags);
  505. mnt_drop_write_file(parfilp);
  506. break;
  507. default:
  508. ops[i].am_error = -EINVAL;
  509. }
  510. }
  511. if (copy_to_user(am_hreq.ops, ops, size))
  512. error = -EFAULT;
  513. kfree(attr_name);
  514. out_kfree_ops:
  515. kfree(ops);
  516. out_dput:
  517. dput(dentry);
  518. return error;
  519. }
  520. int
  521. xfs_ioc_space(
  522. struct file *filp,
  523. unsigned int cmd,
  524. xfs_flock64_t *bf)
  525. {
  526. struct inode *inode = file_inode(filp);
  527. struct xfs_inode *ip = XFS_I(inode);
  528. struct iattr iattr;
  529. enum xfs_prealloc_flags flags = 0;
  530. uint iolock = XFS_IOLOCK_EXCL;
  531. int error;
  532. /*
  533. * Only allow the sys admin to reserve space unless
  534. * unwritten extents are enabled.
  535. */
  536. if (!xfs_sb_version_hasextflgbit(&ip->i_mount->m_sb) &&
  537. !capable(CAP_SYS_ADMIN))
  538. return -EPERM;
  539. if (inode->i_flags & (S_IMMUTABLE|S_APPEND))
  540. return -EPERM;
  541. if (!(filp->f_mode & FMODE_WRITE))
  542. return -EBADF;
  543. if (!S_ISREG(inode->i_mode))
  544. return -EINVAL;
  545. if (filp->f_flags & O_DSYNC)
  546. flags |= XFS_PREALLOC_SYNC;
  547. if (filp->f_mode & FMODE_NOCMTIME)
  548. flags |= XFS_PREALLOC_INVISIBLE;
  549. error = mnt_want_write_file(filp);
  550. if (error)
  551. return error;
  552. xfs_ilock(ip, iolock);
  553. error = xfs_break_layouts(inode, &iolock);
  554. if (error)
  555. goto out_unlock;
  556. xfs_ilock(ip, XFS_MMAPLOCK_EXCL);
  557. iolock |= XFS_MMAPLOCK_EXCL;
  558. switch (bf->l_whence) {
  559. case 0: /*SEEK_SET*/
  560. break;
  561. case 1: /*SEEK_CUR*/
  562. bf->l_start += filp->f_pos;
  563. break;
  564. case 2: /*SEEK_END*/
  565. bf->l_start += XFS_ISIZE(ip);
  566. break;
  567. default:
  568. error = -EINVAL;
  569. goto out_unlock;
  570. }
  571. /*
  572. * length of <= 0 for resv/unresv/zero is invalid. length for
  573. * alloc/free is ignored completely and we have no idea what userspace
  574. * might have set it to, so set it to zero to allow range
  575. * checks to pass.
  576. */
  577. switch (cmd) {
  578. case XFS_IOC_ZERO_RANGE:
  579. case XFS_IOC_RESVSP:
  580. case XFS_IOC_RESVSP64:
  581. case XFS_IOC_UNRESVSP:
  582. case XFS_IOC_UNRESVSP64:
  583. if (bf->l_len <= 0) {
  584. error = -EINVAL;
  585. goto out_unlock;
  586. }
  587. break;
  588. default:
  589. bf->l_len = 0;
  590. break;
  591. }
  592. if (bf->l_start < 0 ||
  593. bf->l_start > inode->i_sb->s_maxbytes ||
  594. bf->l_start + bf->l_len < 0 ||
  595. bf->l_start + bf->l_len >= inode->i_sb->s_maxbytes) {
  596. error = -EINVAL;
  597. goto out_unlock;
  598. }
  599. switch (cmd) {
  600. case XFS_IOC_ZERO_RANGE:
  601. flags |= XFS_PREALLOC_SET;
  602. error = xfs_zero_file_space(ip, bf->l_start, bf->l_len);
  603. break;
  604. case XFS_IOC_RESVSP:
  605. case XFS_IOC_RESVSP64:
  606. flags |= XFS_PREALLOC_SET;
  607. error = xfs_alloc_file_space(ip, bf->l_start, bf->l_len,
  608. XFS_BMAPI_PREALLOC);
  609. break;
  610. case XFS_IOC_UNRESVSP:
  611. case XFS_IOC_UNRESVSP64:
  612. error = xfs_free_file_space(ip, bf->l_start, bf->l_len);
  613. break;
  614. case XFS_IOC_ALLOCSP:
  615. case XFS_IOC_ALLOCSP64:
  616. case XFS_IOC_FREESP:
  617. case XFS_IOC_FREESP64:
  618. flags |= XFS_PREALLOC_CLEAR;
  619. if (bf->l_start > XFS_ISIZE(ip)) {
  620. error = xfs_alloc_file_space(ip, XFS_ISIZE(ip),
  621. bf->l_start - XFS_ISIZE(ip), 0);
  622. if (error)
  623. goto out_unlock;
  624. }
  625. iattr.ia_valid = ATTR_SIZE;
  626. iattr.ia_size = bf->l_start;
  627. error = xfs_vn_setattr_size(file_dentry(filp), &iattr);
  628. break;
  629. default:
  630. ASSERT(0);
  631. error = -EINVAL;
  632. }
  633. if (error)
  634. goto out_unlock;
  635. error = xfs_update_prealloc_flags(ip, flags);
  636. out_unlock:
  637. xfs_iunlock(ip, iolock);
  638. mnt_drop_write_file(filp);
  639. return error;
  640. }
  641. STATIC int
  642. xfs_ioc_bulkstat(
  643. xfs_mount_t *mp,
  644. unsigned int cmd,
  645. void __user *arg)
  646. {
  647. xfs_fsop_bulkreq_t bulkreq;
  648. int count; /* # of records returned */
  649. xfs_ino_t inlast; /* last inode number */
  650. int done;
  651. int error;
  652. /* done = 1 if there are more stats to get and if bulkstat */
  653. /* should be called again (unused here, but used in dmapi) */
  654. if (!capable(CAP_SYS_ADMIN))
  655. return -EPERM;
  656. if (XFS_FORCED_SHUTDOWN(mp))
  657. return -EIO;
  658. if (copy_from_user(&bulkreq, arg, sizeof(xfs_fsop_bulkreq_t)))
  659. return -EFAULT;
  660. if (copy_from_user(&inlast, bulkreq.lastip, sizeof(__s64)))
  661. return -EFAULT;
  662. if ((count = bulkreq.icount) <= 0)
  663. return -EINVAL;
  664. if (bulkreq.ubuffer == NULL)
  665. return -EINVAL;
  666. if (cmd == XFS_IOC_FSINUMBERS)
  667. error = xfs_inumbers(mp, &inlast, &count,
  668. bulkreq.ubuffer, xfs_inumbers_fmt);
  669. else if (cmd == XFS_IOC_FSBULKSTAT_SINGLE)
  670. error = xfs_bulkstat_one(mp, inlast, bulkreq.ubuffer,
  671. sizeof(xfs_bstat_t), NULL, &done);
  672. else /* XFS_IOC_FSBULKSTAT */
  673. error = xfs_bulkstat(mp, &inlast, &count, xfs_bulkstat_one,
  674. sizeof(xfs_bstat_t), bulkreq.ubuffer,
  675. &done);
  676. if (error)
  677. return error;
  678. if (bulkreq.ocount != NULL) {
  679. if (copy_to_user(bulkreq.lastip, &inlast,
  680. sizeof(xfs_ino_t)))
  681. return -EFAULT;
  682. if (copy_to_user(bulkreq.ocount, &count, sizeof(count)))
  683. return -EFAULT;
  684. }
  685. return 0;
  686. }
  687. STATIC int
  688. xfs_ioc_fsgeometry_v1(
  689. xfs_mount_t *mp,
  690. void __user *arg)
  691. {
  692. xfs_fsop_geom_t fsgeo;
  693. int error;
  694. error = xfs_fs_geometry(mp, &fsgeo, 3);
  695. if (error)
  696. return error;
  697. /*
  698. * Caller should have passed an argument of type
  699. * xfs_fsop_geom_v1_t. This is a proper subset of the
  700. * xfs_fsop_geom_t that xfs_fs_geometry() fills in.
  701. */
  702. if (copy_to_user(arg, &fsgeo, sizeof(xfs_fsop_geom_v1_t)))
  703. return -EFAULT;
  704. return 0;
  705. }
  706. STATIC int
  707. xfs_ioc_fsgeometry(
  708. xfs_mount_t *mp,
  709. void __user *arg)
  710. {
  711. xfs_fsop_geom_t fsgeo;
  712. int error;
  713. error = xfs_fs_geometry(mp, &fsgeo, 4);
  714. if (error)
  715. return error;
  716. if (copy_to_user(arg, &fsgeo, sizeof(fsgeo)))
  717. return -EFAULT;
  718. return 0;
  719. }
  720. /*
  721. * Linux extended inode flags interface.
  722. */
  723. STATIC unsigned int
  724. xfs_merge_ioc_xflags(
  725. unsigned int flags,
  726. unsigned int start)
  727. {
  728. unsigned int xflags = start;
  729. if (flags & FS_IMMUTABLE_FL)
  730. xflags |= FS_XFLAG_IMMUTABLE;
  731. else
  732. xflags &= ~FS_XFLAG_IMMUTABLE;
  733. if (flags & FS_APPEND_FL)
  734. xflags |= FS_XFLAG_APPEND;
  735. else
  736. xflags &= ~FS_XFLAG_APPEND;
  737. if (flags & FS_SYNC_FL)
  738. xflags |= FS_XFLAG_SYNC;
  739. else
  740. xflags &= ~FS_XFLAG_SYNC;
  741. if (flags & FS_NOATIME_FL)
  742. xflags |= FS_XFLAG_NOATIME;
  743. else
  744. xflags &= ~FS_XFLAG_NOATIME;
  745. if (flags & FS_NODUMP_FL)
  746. xflags |= FS_XFLAG_NODUMP;
  747. else
  748. xflags &= ~FS_XFLAG_NODUMP;
  749. return xflags;
  750. }
  751. STATIC unsigned int
  752. xfs_di2lxflags(
  753. uint16_t di_flags)
  754. {
  755. unsigned int flags = 0;
  756. if (di_flags & XFS_DIFLAG_IMMUTABLE)
  757. flags |= FS_IMMUTABLE_FL;
  758. if (di_flags & XFS_DIFLAG_APPEND)
  759. flags |= FS_APPEND_FL;
  760. if (di_flags & XFS_DIFLAG_SYNC)
  761. flags |= FS_SYNC_FL;
  762. if (di_flags & XFS_DIFLAG_NOATIME)
  763. flags |= FS_NOATIME_FL;
  764. if (di_flags & XFS_DIFLAG_NODUMP)
  765. flags |= FS_NODUMP_FL;
  766. return flags;
  767. }
  768. STATIC int
  769. xfs_ioc_fsgetxattr(
  770. xfs_inode_t *ip,
  771. int attr,
  772. void __user *arg)
  773. {
  774. struct fsxattr fa;
  775. memset(&fa, 0, sizeof(struct fsxattr));
  776. xfs_ilock(ip, XFS_ILOCK_SHARED);
  777. fa.fsx_xflags = xfs_ip2xflags(ip);
  778. fa.fsx_extsize = ip->i_d.di_extsize << ip->i_mount->m_sb.sb_blocklog;
  779. fa.fsx_cowextsize = ip->i_d.di_cowextsize <<
  780. ip->i_mount->m_sb.sb_blocklog;
  781. fa.fsx_projid = xfs_get_projid(ip);
  782. if (attr) {
  783. if (ip->i_afp) {
  784. if (ip->i_afp->if_flags & XFS_IFEXTENTS)
  785. fa.fsx_nextents = xfs_iext_count(ip->i_afp);
  786. else
  787. fa.fsx_nextents = ip->i_d.di_anextents;
  788. } else
  789. fa.fsx_nextents = 0;
  790. } else {
  791. if (ip->i_df.if_flags & XFS_IFEXTENTS)
  792. fa.fsx_nextents = xfs_iext_count(&ip->i_df);
  793. else
  794. fa.fsx_nextents = ip->i_d.di_nextents;
  795. }
  796. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  797. if (copy_to_user(arg, &fa, sizeof(fa)))
  798. return -EFAULT;
  799. return 0;
  800. }
  801. STATIC uint16_t
  802. xfs_flags2diflags(
  803. struct xfs_inode *ip,
  804. unsigned int xflags)
  805. {
  806. /* can't set PREALLOC this way, just preserve it */
  807. uint16_t di_flags =
  808. (ip->i_d.di_flags & XFS_DIFLAG_PREALLOC);
  809. if (xflags & FS_XFLAG_IMMUTABLE)
  810. di_flags |= XFS_DIFLAG_IMMUTABLE;
  811. if (xflags & FS_XFLAG_APPEND)
  812. di_flags |= XFS_DIFLAG_APPEND;
  813. if (xflags & FS_XFLAG_SYNC)
  814. di_flags |= XFS_DIFLAG_SYNC;
  815. if (xflags & FS_XFLAG_NOATIME)
  816. di_flags |= XFS_DIFLAG_NOATIME;
  817. if (xflags & FS_XFLAG_NODUMP)
  818. di_flags |= XFS_DIFLAG_NODUMP;
  819. if (xflags & FS_XFLAG_NODEFRAG)
  820. di_flags |= XFS_DIFLAG_NODEFRAG;
  821. if (xflags & FS_XFLAG_FILESTREAM)
  822. di_flags |= XFS_DIFLAG_FILESTREAM;
  823. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  824. if (xflags & FS_XFLAG_RTINHERIT)
  825. di_flags |= XFS_DIFLAG_RTINHERIT;
  826. if (xflags & FS_XFLAG_NOSYMLINKS)
  827. di_flags |= XFS_DIFLAG_NOSYMLINKS;
  828. if (xflags & FS_XFLAG_EXTSZINHERIT)
  829. di_flags |= XFS_DIFLAG_EXTSZINHERIT;
  830. if (xflags & FS_XFLAG_PROJINHERIT)
  831. di_flags |= XFS_DIFLAG_PROJINHERIT;
  832. } else if (S_ISREG(VFS_I(ip)->i_mode)) {
  833. if (xflags & FS_XFLAG_REALTIME)
  834. di_flags |= XFS_DIFLAG_REALTIME;
  835. if (xflags & FS_XFLAG_EXTSIZE)
  836. di_flags |= XFS_DIFLAG_EXTSIZE;
  837. }
  838. return di_flags;
  839. }
  840. STATIC uint64_t
  841. xfs_flags2diflags2(
  842. struct xfs_inode *ip,
  843. unsigned int xflags)
  844. {
  845. uint64_t di_flags2 =
  846. (ip->i_d.di_flags2 & XFS_DIFLAG2_REFLINK);
  847. if (xflags & FS_XFLAG_DAX)
  848. di_flags2 |= XFS_DIFLAG2_DAX;
  849. if (xflags & FS_XFLAG_COWEXTSIZE)
  850. di_flags2 |= XFS_DIFLAG2_COWEXTSIZE;
  851. return di_flags2;
  852. }
  853. STATIC void
  854. xfs_diflags_to_linux(
  855. struct xfs_inode *ip)
  856. {
  857. struct inode *inode = VFS_I(ip);
  858. unsigned int xflags = xfs_ip2xflags(ip);
  859. if (xflags & FS_XFLAG_IMMUTABLE)
  860. inode->i_flags |= S_IMMUTABLE;
  861. else
  862. inode->i_flags &= ~S_IMMUTABLE;
  863. if (xflags & FS_XFLAG_APPEND)
  864. inode->i_flags |= S_APPEND;
  865. else
  866. inode->i_flags &= ~S_APPEND;
  867. if (xflags & FS_XFLAG_SYNC)
  868. inode->i_flags |= S_SYNC;
  869. else
  870. inode->i_flags &= ~S_SYNC;
  871. if (xflags & FS_XFLAG_NOATIME)
  872. inode->i_flags |= S_NOATIME;
  873. else
  874. inode->i_flags &= ~S_NOATIME;
  875. #if 0 /* disabled until the flag switching races are sorted out */
  876. if (xflags & FS_XFLAG_DAX)
  877. inode->i_flags |= S_DAX;
  878. else
  879. inode->i_flags &= ~S_DAX;
  880. #endif
  881. }
  882. static int
  883. xfs_ioctl_setattr_xflags(
  884. struct xfs_trans *tp,
  885. struct xfs_inode *ip,
  886. struct fsxattr *fa)
  887. {
  888. struct xfs_mount *mp = ip->i_mount;
  889. uint64_t di_flags2;
  890. /* Can't change realtime flag if any extents are allocated. */
  891. if ((ip->i_d.di_nextents || ip->i_delayed_blks) &&
  892. XFS_IS_REALTIME_INODE(ip) != (fa->fsx_xflags & FS_XFLAG_REALTIME))
  893. return -EINVAL;
  894. /* If realtime flag is set then must have realtime device */
  895. if (fa->fsx_xflags & FS_XFLAG_REALTIME) {
  896. if (mp->m_sb.sb_rblocks == 0 || mp->m_sb.sb_rextsize == 0 ||
  897. (ip->i_d.di_extsize % mp->m_sb.sb_rextsize))
  898. return -EINVAL;
  899. }
  900. /* Clear reflink if we are actually able to set the rt flag. */
  901. if ((fa->fsx_xflags & FS_XFLAG_REALTIME) && xfs_is_reflink_inode(ip))
  902. ip->i_d.di_flags2 &= ~XFS_DIFLAG2_REFLINK;
  903. /* Don't allow us to set DAX mode for a reflinked file for now. */
  904. if ((fa->fsx_xflags & FS_XFLAG_DAX) && xfs_is_reflink_inode(ip))
  905. return -EINVAL;
  906. /*
  907. * Can't modify an immutable/append-only file unless
  908. * we have appropriate permission.
  909. */
  910. if (((ip->i_d.di_flags & (XFS_DIFLAG_IMMUTABLE | XFS_DIFLAG_APPEND)) ||
  911. (fa->fsx_xflags & (FS_XFLAG_IMMUTABLE | FS_XFLAG_APPEND))) &&
  912. !capable(CAP_LINUX_IMMUTABLE))
  913. return -EPERM;
  914. /* diflags2 only valid for v3 inodes. */
  915. di_flags2 = xfs_flags2diflags2(ip, fa->fsx_xflags);
  916. if (di_flags2 && ip->i_d.di_version < 3)
  917. return -EINVAL;
  918. ip->i_d.di_flags = xfs_flags2diflags(ip, fa->fsx_xflags);
  919. ip->i_d.di_flags2 = di_flags2;
  920. xfs_diflags_to_linux(ip);
  921. xfs_trans_ichgtime(tp, ip, XFS_ICHGTIME_CHG);
  922. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  923. XFS_STATS_INC(mp, xs_ig_attrchg);
  924. return 0;
  925. }
  926. /*
  927. * If we are changing DAX flags, we have to ensure the file is clean and any
  928. * cached objects in the address space are invalidated and removed. This
  929. * requires us to lock out other IO and page faults similar to a truncate
  930. * operation. The locks need to be held until the transaction has been committed
  931. * so that the cache invalidation is atomic with respect to the DAX flag
  932. * manipulation.
  933. */
  934. static int
  935. xfs_ioctl_setattr_dax_invalidate(
  936. struct xfs_inode *ip,
  937. struct fsxattr *fa,
  938. int *join_flags)
  939. {
  940. struct inode *inode = VFS_I(ip);
  941. struct super_block *sb = inode->i_sb;
  942. int error;
  943. *join_flags = 0;
  944. /*
  945. * It is only valid to set the DAX flag on regular files and
  946. * directories on filesystems where the block size is equal to the page
  947. * size. On directories it serves as an inherit hint.
  948. */
  949. if (fa->fsx_xflags & FS_XFLAG_DAX) {
  950. if (!(S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode)))
  951. return -EINVAL;
  952. if (bdev_dax_supported(sb, sb->s_blocksize) < 0)
  953. return -EINVAL;
  954. }
  955. /* If the DAX state is not changing, we have nothing to do here. */
  956. if ((fa->fsx_xflags & FS_XFLAG_DAX) && IS_DAX(inode))
  957. return 0;
  958. if (!(fa->fsx_xflags & FS_XFLAG_DAX) && !IS_DAX(inode))
  959. return 0;
  960. /* lock, flush and invalidate mapping in preparation for flag change */
  961. xfs_ilock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  962. error = filemap_write_and_wait(inode->i_mapping);
  963. if (error)
  964. goto out_unlock;
  965. error = invalidate_inode_pages2(inode->i_mapping);
  966. if (error)
  967. goto out_unlock;
  968. *join_flags = XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL;
  969. return 0;
  970. out_unlock:
  971. xfs_iunlock(ip, XFS_MMAPLOCK_EXCL | XFS_IOLOCK_EXCL);
  972. return error;
  973. }
  974. /*
  975. * Set up the transaction structure for the setattr operation, checking that we
  976. * have permission to do so. On success, return a clean transaction and the
  977. * inode locked exclusively ready for further operation specific checks. On
  978. * failure, return an error without modifying or locking the inode.
  979. *
  980. * The inode might already be IO locked on call. If this is the case, it is
  981. * indicated in @join_flags and we take full responsibility for ensuring they
  982. * are unlocked from now on. Hence if we have an error here, we still have to
  983. * unlock them. Otherwise, once they are joined to the transaction, they will
  984. * be unlocked on commit/cancel.
  985. */
  986. static struct xfs_trans *
  987. xfs_ioctl_setattr_get_trans(
  988. struct xfs_inode *ip,
  989. int join_flags)
  990. {
  991. struct xfs_mount *mp = ip->i_mount;
  992. struct xfs_trans *tp;
  993. int error = -EROFS;
  994. if (mp->m_flags & XFS_MOUNT_RDONLY)
  995. goto out_unlock;
  996. error = -EIO;
  997. if (XFS_FORCED_SHUTDOWN(mp))
  998. goto out_unlock;
  999. error = xfs_trans_alloc(mp, &M_RES(mp)->tr_ichange, 0, 0, 0, &tp);
  1000. if (error)
  1001. return ERR_PTR(error);
  1002. xfs_ilock(ip, XFS_ILOCK_EXCL);
  1003. xfs_trans_ijoin(tp, ip, XFS_ILOCK_EXCL | join_flags);
  1004. join_flags = 0;
  1005. /*
  1006. * CAP_FOWNER overrides the following restrictions:
  1007. *
  1008. * The user ID of the calling process must be equal to the file owner
  1009. * ID, except in cases where the CAP_FSETID capability is applicable.
  1010. */
  1011. if (!inode_owner_or_capable(VFS_I(ip))) {
  1012. error = -EPERM;
  1013. goto out_cancel;
  1014. }
  1015. if (mp->m_flags & XFS_MOUNT_WSYNC)
  1016. xfs_trans_set_sync(tp);
  1017. return tp;
  1018. out_cancel:
  1019. xfs_trans_cancel(tp);
  1020. out_unlock:
  1021. if (join_flags)
  1022. xfs_iunlock(ip, join_flags);
  1023. return ERR_PTR(error);
  1024. }
  1025. /*
  1026. * extent size hint validation is somewhat cumbersome. Rules are:
  1027. *
  1028. * 1. extent size hint is only valid for directories and regular files
  1029. * 2. FS_XFLAG_EXTSIZE is only valid for regular files
  1030. * 3. FS_XFLAG_EXTSZINHERIT is only valid for directories.
  1031. * 4. can only be changed on regular files if no extents are allocated
  1032. * 5. can be changed on directories at any time
  1033. * 6. extsize hint of 0 turns off hints, clears inode flags.
  1034. * 7. Extent size must be a multiple of the appropriate block size.
  1035. * 8. for non-realtime files, the extent size hint must be limited
  1036. * to half the AG size to avoid alignment extending the extent beyond the
  1037. * limits of the AG.
  1038. *
  1039. * Please keep this function in sync with xfs_scrub_inode_extsize.
  1040. */
  1041. static int
  1042. xfs_ioctl_setattr_check_extsize(
  1043. struct xfs_inode *ip,
  1044. struct fsxattr *fa)
  1045. {
  1046. struct xfs_mount *mp = ip->i_mount;
  1047. if ((fa->fsx_xflags & FS_XFLAG_EXTSIZE) && !S_ISREG(VFS_I(ip)->i_mode))
  1048. return -EINVAL;
  1049. if ((fa->fsx_xflags & FS_XFLAG_EXTSZINHERIT) &&
  1050. !S_ISDIR(VFS_I(ip)->i_mode))
  1051. return -EINVAL;
  1052. if (S_ISREG(VFS_I(ip)->i_mode) && ip->i_d.di_nextents &&
  1053. ((ip->i_d.di_extsize << mp->m_sb.sb_blocklog) != fa->fsx_extsize))
  1054. return -EINVAL;
  1055. if (fa->fsx_extsize != 0) {
  1056. xfs_extlen_t size;
  1057. xfs_fsblock_t extsize_fsb;
  1058. extsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_extsize);
  1059. if (extsize_fsb > MAXEXTLEN)
  1060. return -EINVAL;
  1061. if (XFS_IS_REALTIME_INODE(ip) ||
  1062. (fa->fsx_xflags & FS_XFLAG_REALTIME)) {
  1063. size = mp->m_sb.sb_rextsize << mp->m_sb.sb_blocklog;
  1064. } else {
  1065. size = mp->m_sb.sb_blocksize;
  1066. if (extsize_fsb > mp->m_sb.sb_agblocks / 2)
  1067. return -EINVAL;
  1068. }
  1069. if (fa->fsx_extsize % size)
  1070. return -EINVAL;
  1071. } else
  1072. fa->fsx_xflags &= ~(FS_XFLAG_EXTSIZE | FS_XFLAG_EXTSZINHERIT);
  1073. return 0;
  1074. }
  1075. /*
  1076. * CoW extent size hint validation rules are:
  1077. *
  1078. * 1. CoW extent size hint can only be set if reflink is enabled on the fs.
  1079. * The inode does not have to have any shared blocks, but it must be a v3.
  1080. * 2. FS_XFLAG_COWEXTSIZE is only valid for directories and regular files;
  1081. * for a directory, the hint is propagated to new files.
  1082. * 3. Can be changed on files & directories at any time.
  1083. * 4. CoW extsize hint of 0 turns off hints, clears inode flags.
  1084. * 5. Extent size must be a multiple of the appropriate block size.
  1085. * 6. The extent size hint must be limited to half the AG size to avoid
  1086. * alignment extending the extent beyond the limits of the AG.
  1087. *
  1088. * Please keep this function in sync with xfs_scrub_inode_cowextsize.
  1089. */
  1090. static int
  1091. xfs_ioctl_setattr_check_cowextsize(
  1092. struct xfs_inode *ip,
  1093. struct fsxattr *fa)
  1094. {
  1095. struct xfs_mount *mp = ip->i_mount;
  1096. if (!(fa->fsx_xflags & FS_XFLAG_COWEXTSIZE))
  1097. return 0;
  1098. if (!xfs_sb_version_hasreflink(&ip->i_mount->m_sb) ||
  1099. ip->i_d.di_version != 3)
  1100. return -EINVAL;
  1101. if (!S_ISREG(VFS_I(ip)->i_mode) && !S_ISDIR(VFS_I(ip)->i_mode))
  1102. return -EINVAL;
  1103. if (fa->fsx_cowextsize != 0) {
  1104. xfs_extlen_t size;
  1105. xfs_fsblock_t cowextsize_fsb;
  1106. cowextsize_fsb = XFS_B_TO_FSB(mp, fa->fsx_cowextsize);
  1107. if (cowextsize_fsb > MAXEXTLEN)
  1108. return -EINVAL;
  1109. size = mp->m_sb.sb_blocksize;
  1110. if (cowextsize_fsb > mp->m_sb.sb_agblocks / 2)
  1111. return -EINVAL;
  1112. if (fa->fsx_cowextsize % size)
  1113. return -EINVAL;
  1114. } else
  1115. fa->fsx_xflags &= ~FS_XFLAG_COWEXTSIZE;
  1116. return 0;
  1117. }
  1118. static int
  1119. xfs_ioctl_setattr_check_projid(
  1120. struct xfs_inode *ip,
  1121. struct fsxattr *fa)
  1122. {
  1123. /* Disallow 32bit project ids if projid32bit feature is not enabled. */
  1124. if (fa->fsx_projid > (uint16_t)-1 &&
  1125. !xfs_sb_version_hasprojid32bit(&ip->i_mount->m_sb))
  1126. return -EINVAL;
  1127. /*
  1128. * Project Quota ID state is only allowed to change from within the init
  1129. * namespace. Enforce that restriction only if we are trying to change
  1130. * the quota ID state. Everything else is allowed in user namespaces.
  1131. */
  1132. if (current_user_ns() == &init_user_ns)
  1133. return 0;
  1134. if (xfs_get_projid(ip) != fa->fsx_projid)
  1135. return -EINVAL;
  1136. if ((fa->fsx_xflags & FS_XFLAG_PROJINHERIT) !=
  1137. (ip->i_d.di_flags & XFS_DIFLAG_PROJINHERIT))
  1138. return -EINVAL;
  1139. return 0;
  1140. }
  1141. STATIC int
  1142. xfs_ioctl_setattr(
  1143. xfs_inode_t *ip,
  1144. struct fsxattr *fa)
  1145. {
  1146. struct xfs_mount *mp = ip->i_mount;
  1147. struct xfs_trans *tp;
  1148. struct xfs_dquot *udqp = NULL;
  1149. struct xfs_dquot *pdqp = NULL;
  1150. struct xfs_dquot *olddquot = NULL;
  1151. int code;
  1152. int join_flags = 0;
  1153. trace_xfs_ioctl_setattr(ip);
  1154. code = xfs_ioctl_setattr_check_projid(ip, fa);
  1155. if (code)
  1156. return code;
  1157. /*
  1158. * If disk quotas is on, we make sure that the dquots do exist on disk,
  1159. * before we start any other transactions. Trying to do this later
  1160. * is messy. We don't care to take a readlock to look at the ids
  1161. * in inode here, because we can't hold it across the trans_reserve.
  1162. * If the IDs do change before we take the ilock, we're covered
  1163. * because the i_*dquot fields will get updated anyway.
  1164. */
  1165. if (XFS_IS_QUOTA_ON(mp)) {
  1166. code = xfs_qm_vop_dqalloc(ip, ip->i_d.di_uid,
  1167. ip->i_d.di_gid, fa->fsx_projid,
  1168. XFS_QMOPT_PQUOTA, &udqp, NULL, &pdqp);
  1169. if (code)
  1170. return code;
  1171. }
  1172. /*
  1173. * Changing DAX config may require inode locking for mapping
  1174. * invalidation. These need to be held all the way to transaction commit
  1175. * or cancel time, so need to be passed through to
  1176. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1177. * appropriately.
  1178. */
  1179. code = xfs_ioctl_setattr_dax_invalidate(ip, fa, &join_flags);
  1180. if (code)
  1181. goto error_free_dquots;
  1182. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1183. if (IS_ERR(tp)) {
  1184. code = PTR_ERR(tp);
  1185. goto error_free_dquots;
  1186. }
  1187. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp) &&
  1188. xfs_get_projid(ip) != fa->fsx_projid) {
  1189. code = xfs_qm_vop_chown_reserve(tp, ip, udqp, NULL, pdqp,
  1190. capable(CAP_FOWNER) ? XFS_QMOPT_FORCE_RES : 0);
  1191. if (code) /* out of quota */
  1192. goto error_trans_cancel;
  1193. }
  1194. code = xfs_ioctl_setattr_check_extsize(ip, fa);
  1195. if (code)
  1196. goto error_trans_cancel;
  1197. code = xfs_ioctl_setattr_check_cowextsize(ip, fa);
  1198. if (code)
  1199. goto error_trans_cancel;
  1200. code = xfs_ioctl_setattr_xflags(tp, ip, fa);
  1201. if (code)
  1202. goto error_trans_cancel;
  1203. /*
  1204. * Change file ownership. Must be the owner or privileged. CAP_FSETID
  1205. * overrides the following restrictions:
  1206. *
  1207. * The set-user-ID and set-group-ID bits of a file will be cleared upon
  1208. * successful return from chown()
  1209. */
  1210. if ((VFS_I(ip)->i_mode & (S_ISUID|S_ISGID)) &&
  1211. !capable_wrt_inode_uidgid(VFS_I(ip), CAP_FSETID))
  1212. VFS_I(ip)->i_mode &= ~(S_ISUID|S_ISGID);
  1213. /* Change the ownerships and register project quota modifications */
  1214. if (xfs_get_projid(ip) != fa->fsx_projid) {
  1215. if (XFS_IS_QUOTA_RUNNING(mp) && XFS_IS_PQUOTA_ON(mp)) {
  1216. olddquot = xfs_qm_vop_chown(tp, ip,
  1217. &ip->i_pdquot, pdqp);
  1218. }
  1219. ASSERT(ip->i_d.di_version > 1);
  1220. xfs_set_projid(ip, fa->fsx_projid);
  1221. }
  1222. /*
  1223. * Only set the extent size hint if we've already determined that the
  1224. * extent size hint should be set on the inode. If no extent size flags
  1225. * are set on the inode then unconditionally clear the extent size hint.
  1226. */
  1227. if (ip->i_d.di_flags & (XFS_DIFLAG_EXTSIZE | XFS_DIFLAG_EXTSZINHERIT))
  1228. ip->i_d.di_extsize = fa->fsx_extsize >> mp->m_sb.sb_blocklog;
  1229. else
  1230. ip->i_d.di_extsize = 0;
  1231. if (ip->i_d.di_version == 3 &&
  1232. (ip->i_d.di_flags2 & XFS_DIFLAG2_COWEXTSIZE))
  1233. ip->i_d.di_cowextsize = fa->fsx_cowextsize >>
  1234. mp->m_sb.sb_blocklog;
  1235. else
  1236. ip->i_d.di_cowextsize = 0;
  1237. code = xfs_trans_commit(tp);
  1238. /*
  1239. * Release any dquot(s) the inode had kept before chown.
  1240. */
  1241. xfs_qm_dqrele(olddquot);
  1242. xfs_qm_dqrele(udqp);
  1243. xfs_qm_dqrele(pdqp);
  1244. return code;
  1245. error_trans_cancel:
  1246. xfs_trans_cancel(tp);
  1247. error_free_dquots:
  1248. xfs_qm_dqrele(udqp);
  1249. xfs_qm_dqrele(pdqp);
  1250. return code;
  1251. }
  1252. STATIC int
  1253. xfs_ioc_fssetxattr(
  1254. xfs_inode_t *ip,
  1255. struct file *filp,
  1256. void __user *arg)
  1257. {
  1258. struct fsxattr fa;
  1259. int error;
  1260. if (copy_from_user(&fa, arg, sizeof(fa)))
  1261. return -EFAULT;
  1262. error = mnt_want_write_file(filp);
  1263. if (error)
  1264. return error;
  1265. error = xfs_ioctl_setattr(ip, &fa);
  1266. mnt_drop_write_file(filp);
  1267. return error;
  1268. }
  1269. STATIC int
  1270. xfs_ioc_getxflags(
  1271. xfs_inode_t *ip,
  1272. void __user *arg)
  1273. {
  1274. unsigned int flags;
  1275. flags = xfs_di2lxflags(ip->i_d.di_flags);
  1276. if (copy_to_user(arg, &flags, sizeof(flags)))
  1277. return -EFAULT;
  1278. return 0;
  1279. }
  1280. STATIC int
  1281. xfs_ioc_setxflags(
  1282. struct xfs_inode *ip,
  1283. struct file *filp,
  1284. void __user *arg)
  1285. {
  1286. struct xfs_trans *tp;
  1287. struct fsxattr fa;
  1288. unsigned int flags;
  1289. int join_flags = 0;
  1290. int error;
  1291. if (copy_from_user(&flags, arg, sizeof(flags)))
  1292. return -EFAULT;
  1293. if (flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | \
  1294. FS_NOATIME_FL | FS_NODUMP_FL | \
  1295. FS_SYNC_FL))
  1296. return -EOPNOTSUPP;
  1297. fa.fsx_xflags = xfs_merge_ioc_xflags(flags, xfs_ip2xflags(ip));
  1298. error = mnt_want_write_file(filp);
  1299. if (error)
  1300. return error;
  1301. /*
  1302. * Changing DAX config may require inode locking for mapping
  1303. * invalidation. These need to be held all the way to transaction commit
  1304. * or cancel time, so need to be passed through to
  1305. * xfs_ioctl_setattr_get_trans() so it can apply them to the join call
  1306. * appropriately.
  1307. */
  1308. error = xfs_ioctl_setattr_dax_invalidate(ip, &fa, &join_flags);
  1309. if (error)
  1310. goto out_drop_write;
  1311. tp = xfs_ioctl_setattr_get_trans(ip, join_flags);
  1312. if (IS_ERR(tp)) {
  1313. error = PTR_ERR(tp);
  1314. goto out_drop_write;
  1315. }
  1316. error = xfs_ioctl_setattr_xflags(tp, ip, &fa);
  1317. if (error) {
  1318. xfs_trans_cancel(tp);
  1319. goto out_drop_write;
  1320. }
  1321. error = xfs_trans_commit(tp);
  1322. out_drop_write:
  1323. mnt_drop_write_file(filp);
  1324. return error;
  1325. }
  1326. static bool
  1327. xfs_getbmap_format(
  1328. struct kgetbmap *p,
  1329. struct getbmapx __user *u,
  1330. size_t recsize)
  1331. {
  1332. if (put_user(p->bmv_offset, &u->bmv_offset) ||
  1333. put_user(p->bmv_block, &u->bmv_block) ||
  1334. put_user(p->bmv_length, &u->bmv_length) ||
  1335. put_user(0, &u->bmv_count) ||
  1336. put_user(0, &u->bmv_entries))
  1337. return false;
  1338. if (recsize < sizeof(struct getbmapx))
  1339. return true;
  1340. if (put_user(0, &u->bmv_iflags) ||
  1341. put_user(p->bmv_oflags, &u->bmv_oflags) ||
  1342. put_user(0, &u->bmv_unused1) ||
  1343. put_user(0, &u->bmv_unused2))
  1344. return false;
  1345. return true;
  1346. }
  1347. STATIC int
  1348. xfs_ioc_getbmap(
  1349. struct file *file,
  1350. unsigned int cmd,
  1351. void __user *arg)
  1352. {
  1353. struct getbmapx bmx = { 0 };
  1354. struct kgetbmap *buf;
  1355. size_t recsize;
  1356. int error, i;
  1357. switch (cmd) {
  1358. case XFS_IOC_GETBMAPA:
  1359. bmx.bmv_iflags = BMV_IF_ATTRFORK;
  1360. /*FALLTHRU*/
  1361. case XFS_IOC_GETBMAP:
  1362. if (file->f_mode & FMODE_NOCMTIME)
  1363. bmx.bmv_iflags |= BMV_IF_NO_DMAPI_READ;
  1364. /* struct getbmap is a strict subset of struct getbmapx. */
  1365. recsize = sizeof(struct getbmap);
  1366. break;
  1367. case XFS_IOC_GETBMAPX:
  1368. recsize = sizeof(struct getbmapx);
  1369. break;
  1370. default:
  1371. return -EINVAL;
  1372. }
  1373. if (copy_from_user(&bmx, arg, recsize))
  1374. return -EFAULT;
  1375. if (bmx.bmv_count < 2)
  1376. return -EINVAL;
  1377. if (bmx.bmv_count > ULONG_MAX / recsize)
  1378. return -ENOMEM;
  1379. buf = kmem_zalloc_large(bmx.bmv_count * sizeof(*buf), 0);
  1380. if (!buf)
  1381. return -ENOMEM;
  1382. error = xfs_getbmap(XFS_I(file_inode(file)), &bmx, buf);
  1383. if (error)
  1384. goto out_free_buf;
  1385. error = -EFAULT;
  1386. if (copy_to_user(arg, &bmx, recsize))
  1387. goto out_free_buf;
  1388. arg += recsize;
  1389. for (i = 0; i < bmx.bmv_entries; i++) {
  1390. if (!xfs_getbmap_format(buf + i, arg, recsize))
  1391. goto out_free_buf;
  1392. arg += recsize;
  1393. }
  1394. error = 0;
  1395. out_free_buf:
  1396. kmem_free(buf);
  1397. return 0;
  1398. }
  1399. struct getfsmap_info {
  1400. struct xfs_mount *mp;
  1401. struct fsmap_head __user *data;
  1402. unsigned int idx;
  1403. __u32 last_flags;
  1404. };
  1405. STATIC int
  1406. xfs_getfsmap_format(struct xfs_fsmap *xfm, void *priv)
  1407. {
  1408. struct getfsmap_info *info = priv;
  1409. struct fsmap fm;
  1410. trace_xfs_getfsmap_mapping(info->mp, xfm);
  1411. info->last_flags = xfm->fmr_flags;
  1412. xfs_fsmap_from_internal(&fm, xfm);
  1413. if (copy_to_user(&info->data->fmh_recs[info->idx++], &fm,
  1414. sizeof(struct fsmap)))
  1415. return -EFAULT;
  1416. return 0;
  1417. }
  1418. STATIC int
  1419. xfs_ioc_getfsmap(
  1420. struct xfs_inode *ip,
  1421. struct fsmap_head __user *arg)
  1422. {
  1423. struct getfsmap_info info = { NULL };
  1424. struct xfs_fsmap_head xhead = {0};
  1425. struct fsmap_head head;
  1426. bool aborted = false;
  1427. int error;
  1428. if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
  1429. return -EFAULT;
  1430. if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
  1431. memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
  1432. sizeof(head.fmh_keys[0].fmr_reserved)) ||
  1433. memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
  1434. sizeof(head.fmh_keys[1].fmr_reserved)))
  1435. return -EINVAL;
  1436. xhead.fmh_iflags = head.fmh_iflags;
  1437. xhead.fmh_count = head.fmh_count;
  1438. xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
  1439. xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
  1440. trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
  1441. trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
  1442. info.mp = ip->i_mount;
  1443. info.data = arg;
  1444. error = xfs_getfsmap(ip->i_mount, &xhead, xfs_getfsmap_format, &info);
  1445. if (error == XFS_BTREE_QUERY_RANGE_ABORT) {
  1446. error = 0;
  1447. aborted = true;
  1448. } else if (error)
  1449. return error;
  1450. /* If we didn't abort, set the "last" flag in the last fmx */
  1451. if (!aborted && info.idx) {
  1452. info.last_flags |= FMR_OF_LAST;
  1453. if (copy_to_user(&info.data->fmh_recs[info.idx - 1].fmr_flags,
  1454. &info.last_flags, sizeof(info.last_flags)))
  1455. return -EFAULT;
  1456. }
  1457. /* copy back header */
  1458. head.fmh_entries = xhead.fmh_entries;
  1459. head.fmh_oflags = xhead.fmh_oflags;
  1460. if (copy_to_user(arg, &head, sizeof(struct fsmap_head)))
  1461. return -EFAULT;
  1462. return 0;
  1463. }
  1464. STATIC int
  1465. xfs_ioc_scrub_metadata(
  1466. struct xfs_inode *ip,
  1467. void __user *arg)
  1468. {
  1469. struct xfs_scrub_metadata scrub;
  1470. int error;
  1471. if (!capable(CAP_SYS_ADMIN))
  1472. return -EPERM;
  1473. if (copy_from_user(&scrub, arg, sizeof(scrub)))
  1474. return -EFAULT;
  1475. error = xfs_scrub_metadata(ip, &scrub);
  1476. if (error)
  1477. return error;
  1478. if (copy_to_user(arg, &scrub, sizeof(scrub)))
  1479. return -EFAULT;
  1480. return 0;
  1481. }
  1482. int
  1483. xfs_ioc_swapext(
  1484. xfs_swapext_t *sxp)
  1485. {
  1486. xfs_inode_t *ip, *tip;
  1487. struct fd f, tmp;
  1488. int error = 0;
  1489. /* Pull information for the target fd */
  1490. f = fdget((int)sxp->sx_fdtarget);
  1491. if (!f.file) {
  1492. error = -EINVAL;
  1493. goto out;
  1494. }
  1495. if (!(f.file->f_mode & FMODE_WRITE) ||
  1496. !(f.file->f_mode & FMODE_READ) ||
  1497. (f.file->f_flags & O_APPEND)) {
  1498. error = -EBADF;
  1499. goto out_put_file;
  1500. }
  1501. tmp = fdget((int)sxp->sx_fdtmp);
  1502. if (!tmp.file) {
  1503. error = -EINVAL;
  1504. goto out_put_file;
  1505. }
  1506. if (!(tmp.file->f_mode & FMODE_WRITE) ||
  1507. !(tmp.file->f_mode & FMODE_READ) ||
  1508. (tmp.file->f_flags & O_APPEND)) {
  1509. error = -EBADF;
  1510. goto out_put_tmp_file;
  1511. }
  1512. if (IS_SWAPFILE(file_inode(f.file)) ||
  1513. IS_SWAPFILE(file_inode(tmp.file))) {
  1514. error = -EINVAL;
  1515. goto out_put_tmp_file;
  1516. }
  1517. /*
  1518. * We need to ensure that the fds passed in point to XFS inodes
  1519. * before we cast and access them as XFS structures as we have no
  1520. * control over what the user passes us here.
  1521. */
  1522. if (f.file->f_op != &xfs_file_operations ||
  1523. tmp.file->f_op != &xfs_file_operations) {
  1524. error = -EINVAL;
  1525. goto out_put_tmp_file;
  1526. }
  1527. ip = XFS_I(file_inode(f.file));
  1528. tip = XFS_I(file_inode(tmp.file));
  1529. if (ip->i_mount != tip->i_mount) {
  1530. error = -EINVAL;
  1531. goto out_put_tmp_file;
  1532. }
  1533. if (ip->i_ino == tip->i_ino) {
  1534. error = -EINVAL;
  1535. goto out_put_tmp_file;
  1536. }
  1537. if (XFS_FORCED_SHUTDOWN(ip->i_mount)) {
  1538. error = -EIO;
  1539. goto out_put_tmp_file;
  1540. }
  1541. error = xfs_swap_extents(ip, tip, sxp);
  1542. out_put_tmp_file:
  1543. fdput(tmp);
  1544. out_put_file:
  1545. fdput(f);
  1546. out:
  1547. return error;
  1548. }
  1549. /*
  1550. * Note: some of the ioctl's return positive numbers as a
  1551. * byte count indicating success, such as readlink_by_handle.
  1552. * So we don't "sign flip" like most other routines. This means
  1553. * true errors need to be returned as a negative value.
  1554. */
  1555. long
  1556. xfs_file_ioctl(
  1557. struct file *filp,
  1558. unsigned int cmd,
  1559. unsigned long p)
  1560. {
  1561. struct inode *inode = file_inode(filp);
  1562. struct xfs_inode *ip = XFS_I(inode);
  1563. struct xfs_mount *mp = ip->i_mount;
  1564. void __user *arg = (void __user *)p;
  1565. int error;
  1566. trace_xfs_file_ioctl(ip);
  1567. switch (cmd) {
  1568. case FITRIM:
  1569. return xfs_ioc_trim(mp, arg);
  1570. case XFS_IOC_ALLOCSP:
  1571. case XFS_IOC_FREESP:
  1572. case XFS_IOC_RESVSP:
  1573. case XFS_IOC_UNRESVSP:
  1574. case XFS_IOC_ALLOCSP64:
  1575. case XFS_IOC_FREESP64:
  1576. case XFS_IOC_RESVSP64:
  1577. case XFS_IOC_UNRESVSP64:
  1578. case XFS_IOC_ZERO_RANGE: {
  1579. xfs_flock64_t bf;
  1580. if (copy_from_user(&bf, arg, sizeof(bf)))
  1581. return -EFAULT;
  1582. return xfs_ioc_space(filp, cmd, &bf);
  1583. }
  1584. case XFS_IOC_DIOINFO: {
  1585. struct dioattr da;
  1586. xfs_buftarg_t *target =
  1587. XFS_IS_REALTIME_INODE(ip) ?
  1588. mp->m_rtdev_targp : mp->m_ddev_targp;
  1589. da.d_mem = da.d_miniosz = target->bt_logical_sectorsize;
  1590. da.d_maxiosz = INT_MAX & ~(da.d_miniosz - 1);
  1591. if (copy_to_user(arg, &da, sizeof(da)))
  1592. return -EFAULT;
  1593. return 0;
  1594. }
  1595. case XFS_IOC_FSBULKSTAT_SINGLE:
  1596. case XFS_IOC_FSBULKSTAT:
  1597. case XFS_IOC_FSINUMBERS:
  1598. return xfs_ioc_bulkstat(mp, cmd, arg);
  1599. case XFS_IOC_FSGEOMETRY_V1:
  1600. return xfs_ioc_fsgeometry_v1(mp, arg);
  1601. case XFS_IOC_FSGEOMETRY:
  1602. return xfs_ioc_fsgeometry(mp, arg);
  1603. case XFS_IOC_GETVERSION:
  1604. return put_user(inode->i_generation, (int __user *)arg);
  1605. case XFS_IOC_FSGETXATTR:
  1606. return xfs_ioc_fsgetxattr(ip, 0, arg);
  1607. case XFS_IOC_FSGETXATTRA:
  1608. return xfs_ioc_fsgetxattr(ip, 1, arg);
  1609. case XFS_IOC_FSSETXATTR:
  1610. return xfs_ioc_fssetxattr(ip, filp, arg);
  1611. case XFS_IOC_GETXFLAGS:
  1612. return xfs_ioc_getxflags(ip, arg);
  1613. case XFS_IOC_SETXFLAGS:
  1614. return xfs_ioc_setxflags(ip, filp, arg);
  1615. case XFS_IOC_FSSETDM: {
  1616. struct fsdmidata dmi;
  1617. if (copy_from_user(&dmi, arg, sizeof(dmi)))
  1618. return -EFAULT;
  1619. error = mnt_want_write_file(filp);
  1620. if (error)
  1621. return error;
  1622. error = xfs_set_dmattrs(ip, dmi.fsd_dmevmask,
  1623. dmi.fsd_dmstate);
  1624. mnt_drop_write_file(filp);
  1625. return error;
  1626. }
  1627. case XFS_IOC_GETBMAP:
  1628. case XFS_IOC_GETBMAPA:
  1629. case XFS_IOC_GETBMAPX:
  1630. return xfs_ioc_getbmap(filp, cmd, arg);
  1631. case FS_IOC_GETFSMAP:
  1632. return xfs_ioc_getfsmap(ip, arg);
  1633. case XFS_IOC_SCRUB_METADATA:
  1634. return xfs_ioc_scrub_metadata(ip, arg);
  1635. case XFS_IOC_FD_TO_HANDLE:
  1636. case XFS_IOC_PATH_TO_HANDLE:
  1637. case XFS_IOC_PATH_TO_FSHANDLE: {
  1638. xfs_fsop_handlereq_t hreq;
  1639. if (copy_from_user(&hreq, arg, sizeof(hreq)))
  1640. return -EFAULT;
  1641. return xfs_find_handle(cmd, &hreq);
  1642. }
  1643. case XFS_IOC_OPEN_BY_HANDLE: {
  1644. xfs_fsop_handlereq_t hreq;
  1645. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1646. return -EFAULT;
  1647. return xfs_open_by_handle(filp, &hreq);
  1648. }
  1649. case XFS_IOC_FSSETDM_BY_HANDLE:
  1650. return xfs_fssetdm_by_handle(filp, arg);
  1651. case XFS_IOC_READLINK_BY_HANDLE: {
  1652. xfs_fsop_handlereq_t hreq;
  1653. if (copy_from_user(&hreq, arg, sizeof(xfs_fsop_handlereq_t)))
  1654. return -EFAULT;
  1655. return xfs_readlink_by_handle(filp, &hreq);
  1656. }
  1657. case XFS_IOC_ATTRLIST_BY_HANDLE:
  1658. return xfs_attrlist_by_handle(filp, arg);
  1659. case XFS_IOC_ATTRMULTI_BY_HANDLE:
  1660. return xfs_attrmulti_by_handle(filp, arg);
  1661. case XFS_IOC_SWAPEXT: {
  1662. struct xfs_swapext sxp;
  1663. if (copy_from_user(&sxp, arg, sizeof(xfs_swapext_t)))
  1664. return -EFAULT;
  1665. error = mnt_want_write_file(filp);
  1666. if (error)
  1667. return error;
  1668. error = xfs_ioc_swapext(&sxp);
  1669. mnt_drop_write_file(filp);
  1670. return error;
  1671. }
  1672. case XFS_IOC_FSCOUNTS: {
  1673. xfs_fsop_counts_t out;
  1674. error = xfs_fs_counts(mp, &out);
  1675. if (error)
  1676. return error;
  1677. if (copy_to_user(arg, &out, sizeof(out)))
  1678. return -EFAULT;
  1679. return 0;
  1680. }
  1681. case XFS_IOC_SET_RESBLKS: {
  1682. xfs_fsop_resblks_t inout;
  1683. uint64_t in;
  1684. if (!capable(CAP_SYS_ADMIN))
  1685. return -EPERM;
  1686. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1687. return -EROFS;
  1688. if (copy_from_user(&inout, arg, sizeof(inout)))
  1689. return -EFAULT;
  1690. error = mnt_want_write_file(filp);
  1691. if (error)
  1692. return error;
  1693. /* input parameter is passed in resblks field of structure */
  1694. in = inout.resblks;
  1695. error = xfs_reserve_blocks(mp, &in, &inout);
  1696. mnt_drop_write_file(filp);
  1697. if (error)
  1698. return error;
  1699. if (copy_to_user(arg, &inout, sizeof(inout)))
  1700. return -EFAULT;
  1701. return 0;
  1702. }
  1703. case XFS_IOC_GET_RESBLKS: {
  1704. xfs_fsop_resblks_t out;
  1705. if (!capable(CAP_SYS_ADMIN))
  1706. return -EPERM;
  1707. error = xfs_reserve_blocks(mp, NULL, &out);
  1708. if (error)
  1709. return error;
  1710. if (copy_to_user(arg, &out, sizeof(out)))
  1711. return -EFAULT;
  1712. return 0;
  1713. }
  1714. case XFS_IOC_FSGROWFSDATA: {
  1715. xfs_growfs_data_t in;
  1716. if (copy_from_user(&in, arg, sizeof(in)))
  1717. return -EFAULT;
  1718. error = mnt_want_write_file(filp);
  1719. if (error)
  1720. return error;
  1721. error = xfs_growfs_data(mp, &in);
  1722. mnt_drop_write_file(filp);
  1723. return error;
  1724. }
  1725. case XFS_IOC_FSGROWFSLOG: {
  1726. xfs_growfs_log_t in;
  1727. if (copy_from_user(&in, arg, sizeof(in)))
  1728. return -EFAULT;
  1729. error = mnt_want_write_file(filp);
  1730. if (error)
  1731. return error;
  1732. error = xfs_growfs_log(mp, &in);
  1733. mnt_drop_write_file(filp);
  1734. return error;
  1735. }
  1736. case XFS_IOC_FSGROWFSRT: {
  1737. xfs_growfs_rt_t in;
  1738. if (copy_from_user(&in, arg, sizeof(in)))
  1739. return -EFAULT;
  1740. error = mnt_want_write_file(filp);
  1741. if (error)
  1742. return error;
  1743. error = xfs_growfs_rt(mp, &in);
  1744. mnt_drop_write_file(filp);
  1745. return error;
  1746. }
  1747. case XFS_IOC_GOINGDOWN: {
  1748. uint32_t in;
  1749. if (!capable(CAP_SYS_ADMIN))
  1750. return -EPERM;
  1751. if (get_user(in, (uint32_t __user *)arg))
  1752. return -EFAULT;
  1753. return xfs_fs_goingdown(mp, in);
  1754. }
  1755. case XFS_IOC_ERROR_INJECTION: {
  1756. xfs_error_injection_t in;
  1757. if (!capable(CAP_SYS_ADMIN))
  1758. return -EPERM;
  1759. if (copy_from_user(&in, arg, sizeof(in)))
  1760. return -EFAULT;
  1761. return xfs_errortag_add(mp, in.errtag);
  1762. }
  1763. case XFS_IOC_ERROR_CLEARALL:
  1764. if (!capable(CAP_SYS_ADMIN))
  1765. return -EPERM;
  1766. return xfs_errortag_clearall(mp);
  1767. case XFS_IOC_FREE_EOFBLOCKS: {
  1768. struct xfs_fs_eofblocks eofb;
  1769. struct xfs_eofblocks keofb;
  1770. if (!capable(CAP_SYS_ADMIN))
  1771. return -EPERM;
  1772. if (mp->m_flags & XFS_MOUNT_RDONLY)
  1773. return -EROFS;
  1774. if (copy_from_user(&eofb, arg, sizeof(eofb)))
  1775. return -EFAULT;
  1776. error = xfs_fs_eofblocks_from_user(&eofb, &keofb);
  1777. if (error)
  1778. return error;
  1779. return xfs_icache_free_eofblocks(mp, &keofb);
  1780. }
  1781. default:
  1782. return -ENOTTY;
  1783. }
  1784. }