ioctl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * linux/fs/ext2/ioctl.c
  4. *
  5. * Copyright (C) 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  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. long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  19. {
  20. struct inode *inode = file_inode(filp);
  21. struct ext2_inode_info *ei = EXT2_I(inode);
  22. unsigned int flags;
  23. unsigned short rsv_window_size;
  24. int ret;
  25. ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
  26. switch (cmd) {
  27. case EXT2_IOC_GETFLAGS:
  28. flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
  29. return put_user(flags, (int __user *) arg);
  30. case EXT2_IOC_SETFLAGS: {
  31. unsigned int oldflags;
  32. ret = mnt_want_write_file(filp);
  33. if (ret)
  34. return ret;
  35. if (!inode_owner_or_capable(inode)) {
  36. ret = -EACCES;
  37. goto setflags_out;
  38. }
  39. if (get_user(flags, (int __user *) arg)) {
  40. ret = -EFAULT;
  41. goto setflags_out;
  42. }
  43. flags = ext2_mask_flags(inode->i_mode, flags);
  44. inode_lock(inode);
  45. /* Is it quota file? Do not allow user to mess with it */
  46. if (IS_NOQUOTA(inode)) {
  47. inode_unlock(inode);
  48. ret = -EPERM;
  49. goto setflags_out;
  50. }
  51. oldflags = ei->i_flags;
  52. /*
  53. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  54. * the relevant capability.
  55. *
  56. * This test looks nicer. Thanks to Pauline Middelink
  57. */
  58. if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
  59. if (!capable(CAP_LINUX_IMMUTABLE)) {
  60. inode_unlock(inode);
  61. ret = -EPERM;
  62. goto setflags_out;
  63. }
  64. }
  65. flags = flags & EXT2_FL_USER_MODIFIABLE;
  66. flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
  67. ei->i_flags = flags;
  68. ext2_set_inode_flags(inode);
  69. inode->i_ctime = current_time(inode);
  70. inode_unlock(inode);
  71. mark_inode_dirty(inode);
  72. setflags_out:
  73. mnt_drop_write_file(filp);
  74. return ret;
  75. }
  76. case EXT2_IOC_GETVERSION:
  77. return put_user(inode->i_generation, (int __user *) arg);
  78. case EXT2_IOC_SETVERSION: {
  79. __u32 generation;
  80. if (!inode_owner_or_capable(inode))
  81. return -EPERM;
  82. ret = mnt_want_write_file(filp);
  83. if (ret)
  84. return ret;
  85. if (get_user(generation, (int __user *) arg)) {
  86. ret = -EFAULT;
  87. goto setversion_out;
  88. }
  89. inode_lock(inode);
  90. inode->i_ctime = current_time(inode);
  91. inode->i_generation = generation;
  92. inode_unlock(inode);
  93. mark_inode_dirty(inode);
  94. setversion_out:
  95. mnt_drop_write_file(filp);
  96. return ret;
  97. }
  98. case EXT2_IOC_GETRSVSZ:
  99. if (test_opt(inode->i_sb, RESERVATION)
  100. && S_ISREG(inode->i_mode)
  101. && ei->i_block_alloc_info) {
  102. rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
  103. return put_user(rsv_window_size, (int __user *)arg);
  104. }
  105. return -ENOTTY;
  106. case EXT2_IOC_SETRSVSZ: {
  107. if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
  108. return -ENOTTY;
  109. if (!inode_owner_or_capable(inode))
  110. return -EACCES;
  111. if (get_user(rsv_window_size, (int __user *)arg))
  112. return -EFAULT;
  113. ret = mnt_want_write_file(filp);
  114. if (ret)
  115. return ret;
  116. if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
  117. rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
  118. /*
  119. * need to allocate reservation structure for this inode
  120. * before set the window size
  121. */
  122. /*
  123. * XXX What lock should protect the rsv_goal_size?
  124. * Accessed in ext2_get_block only. ext3 uses i_truncate.
  125. */
  126. mutex_lock(&ei->truncate_mutex);
  127. if (!ei->i_block_alloc_info)
  128. ext2_init_block_alloc_info(inode);
  129. if (ei->i_block_alloc_info){
  130. struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
  131. rsv->rsv_goal_size = rsv_window_size;
  132. }
  133. mutex_unlock(&ei->truncate_mutex);
  134. mnt_drop_write_file(filp);
  135. return 0;
  136. }
  137. default:
  138. return -ENOTTY;
  139. }
  140. }
  141. #ifdef CONFIG_COMPAT
  142. long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  143. {
  144. /* These are just misnamed, they actually get/put from/to user an int */
  145. switch (cmd) {
  146. case EXT2_IOC32_GETFLAGS:
  147. cmd = EXT2_IOC_GETFLAGS;
  148. break;
  149. case EXT2_IOC32_SETFLAGS:
  150. cmd = EXT2_IOC_SETFLAGS;
  151. break;
  152. case EXT2_IOC32_GETVERSION:
  153. cmd = EXT2_IOC_GETVERSION;
  154. break;
  155. case EXT2_IOC32_SETVERSION:
  156. cmd = EXT2_IOC_SETVERSION;
  157. break;
  158. default:
  159. return -ENOIOCTLCMD;
  160. }
  161. return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  162. }
  163. #endif