ioctl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * linux/fs/ext2/ioctl.c
  3. *
  4. * Copyright (C) 1993, 1994, 1995
  5. * Remy Card (card@masi.ibp.fr)
  6. * Laboratoire MASI - Institut Blaise Pascal
  7. * Universite Pierre et Marie Curie (Paris VI)
  8. */
  9. #include "ext2.h"
  10. #include <linux/capability.h>
  11. #include <linux/time.h>
  12. #include <linux/sched.h>
  13. #include <linux/compat.h>
  14. #include <linux/mount.h>
  15. #include <asm/current.h>
  16. #include <asm/uaccess.h>
  17. long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  18. {
  19. struct inode *inode = filp->f_dentry->d_inode;
  20. struct ext2_inode_info *ei = EXT2_I(inode);
  21. unsigned int flags;
  22. unsigned short rsv_window_size;
  23. int ret;
  24. ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
  25. switch (cmd) {
  26. case EXT2_IOC_GETFLAGS:
  27. ext2_get_inode_flags(ei);
  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(filp->f_path.mnt);
  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. mutex_lock(&inode->i_mutex);
  45. /* Is it quota file? Do not allow user to mess with it */
  46. if (IS_NOQUOTA(inode)) {
  47. mutex_unlock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  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. mutex_unlock(&inode->i_mutex);
  69. ext2_set_inode_flags(inode);
  70. inode->i_ctime = CURRENT_TIME_SEC;
  71. mark_inode_dirty(inode);
  72. setflags_out:
  73. mnt_drop_write(filp->f_path.mnt);
  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. if (!inode_owner_or_capable(inode))
  80. return -EPERM;
  81. ret = mnt_want_write(filp->f_path.mnt);
  82. if (ret)
  83. return ret;
  84. if (get_user(inode->i_generation, (int __user *) arg)) {
  85. ret = -EFAULT;
  86. } else {
  87. inode->i_ctime = CURRENT_TIME_SEC;
  88. mark_inode_dirty(inode);
  89. }
  90. mnt_drop_write(filp->f_path.mnt);
  91. return ret;
  92. case EXT2_IOC_GETRSVSZ:
  93. if (test_opt(inode->i_sb, RESERVATION)
  94. && S_ISREG(inode->i_mode)
  95. && ei->i_block_alloc_info) {
  96. rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
  97. return put_user(rsv_window_size, (int __user *)arg);
  98. }
  99. return -ENOTTY;
  100. case EXT2_IOC_SETRSVSZ: {
  101. if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
  102. return -ENOTTY;
  103. if (!inode_owner_or_capable(inode))
  104. return -EACCES;
  105. if (get_user(rsv_window_size, (int __user *)arg))
  106. return -EFAULT;
  107. ret = mnt_want_write(filp->f_path.mnt);
  108. if (ret)
  109. return ret;
  110. if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
  111. rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
  112. /*
  113. * need to allocate reservation structure for this inode
  114. * before set the window size
  115. */
  116. /*
  117. * XXX What lock should protect the rsv_goal_size?
  118. * Accessed in ext2_get_block only. ext3 uses i_truncate.
  119. */
  120. mutex_lock(&ei->truncate_mutex);
  121. if (!ei->i_block_alloc_info)
  122. ext2_init_block_alloc_info(inode);
  123. if (ei->i_block_alloc_info){
  124. struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
  125. rsv->rsv_goal_size = rsv_window_size;
  126. }
  127. mutex_unlock(&ei->truncate_mutex);
  128. mnt_drop_write(filp->f_path.mnt);
  129. return 0;
  130. }
  131. default:
  132. return -ENOTTY;
  133. }
  134. }
  135. #ifdef CONFIG_COMPAT
  136. long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  137. {
  138. /* These are just misnamed, they actually get/put from/to user an int */
  139. switch (cmd) {
  140. case EXT2_IOC32_GETFLAGS:
  141. cmd = EXT2_IOC_GETFLAGS;
  142. break;
  143. case EXT2_IOC32_SETFLAGS:
  144. cmd = EXT2_IOC_SETFLAGS;
  145. break;
  146. case EXT2_IOC32_GETVERSION:
  147. cmd = EXT2_IOC_GETVERSION;
  148. break;
  149. case EXT2_IOC32_SETVERSION:
  150. cmd = EXT2_IOC_SETVERSION;
  151. break;
  152. default:
  153. return -ENOIOCTLCMD;
  154. }
  155. return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  156. }
  157. #endif