PageRenderTime 1263ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xfs/xfs_ioctl.c

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