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

/fs/xfs/xfs_ioctl.c

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