ioctl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/ioctl.c
  4. *
  5. * Copyright (C) 1993, 1994, 1995
  6. * Remy Card ([email protected])
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. */
  10. #include "ext2.h"
  11. #include <linux/capability.h>
  12. #include <linux/time.h>
  13. #include <linux/sched.h>
  14. #include <linux/compat.h>
  15. #include <linux/mount.h>
  16. #include <asm/current.h>
  17. #include <linux/uaccess.h>
  18. #include <linux/fileattr.h>
  19. int ext2_fileattr_get(struct dentry *dentry, struct fileattr *fa)
  20. {
  21. struct ext2_inode_info *ei = EXT2_I(d_inode(dentry));
  22. fileattr_fill_flags(fa, ei->i_flags & EXT2_FL_USER_VISIBLE);
  23. return 0;
  24. }
  25. int ext2_fileattr_set(struct user_namespace *mnt_userns,
  26. struct dentry *dentry, struct fileattr *fa)
  27. {
  28. struct inode *inode = d_inode(dentry);
  29. struct ext2_inode_info *ei = EXT2_I(inode);
  30. if (fileattr_has_fsx(fa))
  31. return -EOPNOTSUPP;
  32. /* Is it quota file? Do not allow user to mess with it */
  33. if (IS_NOQUOTA(inode))
  34. return -EPERM;
  35. ei->i_flags = (ei->i_flags & ~EXT2_FL_USER_MODIFIABLE) |
  36. (fa->flags & EXT2_FL_USER_MODIFIABLE);
  37. ext2_set_inode_flags(inode);
  38. inode->i_ctime = current_time(inode);
  39. mark_inode_dirty(inode);
  40. return 0;
  41. }
  42. long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  43. {
  44. struct inode *inode = file_inode(filp);
  45. struct ext2_inode_info *ei = EXT2_I(inode);
  46. unsigned short rsv_window_size;
  47. int ret;
  48. ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
  49. switch (cmd) {
  50. case EXT2_IOC_GETVERSION:
  51. return put_user(inode->i_generation, (int __user *) arg);
  52. case EXT2_IOC_SETVERSION: {
  53. __u32 generation;
  54. if (!inode_owner_or_capable(&init_user_ns, inode))
  55. return -EPERM;
  56. ret = mnt_want_write_file(filp);
  57. if (ret)
  58. return ret;
  59. if (get_user(generation, (int __user *) arg)) {
  60. ret = -EFAULT;
  61. goto setversion_out;
  62. }
  63. inode_lock(inode);
  64. inode->i_ctime = current_time(inode);
  65. inode->i_generation = generation;
  66. inode_unlock(inode);
  67. mark_inode_dirty(inode);
  68. setversion_out:
  69. mnt_drop_write_file(filp);
  70. return ret;
  71. }
  72. case EXT2_IOC_GETRSVSZ:
  73. if (test_opt(inode->i_sb, RESERVATION)
  74. && S_ISREG(inode->i_mode)
  75. && ei->i_block_alloc_info) {
  76. rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
  77. return put_user(rsv_window_size, (int __user *)arg);
  78. }
  79. return -ENOTTY;
  80. case EXT2_IOC_SETRSVSZ: {
  81. if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
  82. return -ENOTTY;
  83. if (!inode_owner_or_capable(&init_user_ns, inode))
  84. return -EACCES;
  85. if (get_user(rsv_window_size, (int __user *)arg))
  86. return -EFAULT;
  87. ret = mnt_want_write_file(filp);
  88. if (ret)
  89. return ret;
  90. if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
  91. rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
  92. /*
  93. * need to allocate reservation structure for this inode
  94. * before set the window size
  95. */
  96. /*
  97. * XXX What lock should protect the rsv_goal_size?
  98. * Accessed in ext2_get_block only. ext3 uses i_truncate.
  99. */
  100. mutex_lock(&ei->truncate_mutex);
  101. if (!ei->i_block_alloc_info)
  102. ext2_init_block_alloc_info(inode);
  103. if (ei->i_block_alloc_info){
  104. struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
  105. rsv->rsv_goal_size = rsv_window_size;
  106. } else {
  107. ret = -ENOMEM;
  108. }
  109. mutex_unlock(&ei->truncate_mutex);
  110. mnt_drop_write_file(filp);
  111. return ret;
  112. }
  113. default:
  114. return -ENOTTY;
  115. }
  116. }
  117. #ifdef CONFIG_COMPAT
  118. long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  119. {
  120. /* These are just misnamed, they actually get/put from/to user an int */
  121. switch (cmd) {
  122. case EXT2_IOC32_GETVERSION:
  123. cmd = EXT2_IOC_GETVERSION;
  124. break;
  125. case EXT2_IOC32_SETVERSION:
  126. cmd = EXT2_IOC_SETVERSION;
  127. break;
  128. default:
  129. return -ENOIOCTLCMD;
  130. }
  131. return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  132. }
  133. #endif