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 <linux/smp_lock.h>
  16. #include <asm/current.h>
  17. #include <asm/uaccess.h>
  18. long ext2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  19. {
  20. struct inode *inode = filp->f_dentry->d_inode;
  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. ext2_get_inode_flags(ei);
  29. flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
  30. return put_user(flags, (int __user *) arg);
  31. case EXT2_IOC_SETFLAGS: {
  32. unsigned int oldflags;
  33. ret = mnt_want_write(filp->f_path.mnt);
  34. if (ret)
  35. return ret;
  36. if (!is_owner_or_cap(inode)) {
  37. ret = -EACCES;
  38. goto setflags_out;
  39. }
  40. if (get_user(flags, (int __user *) arg)) {
  41. ret = -EFAULT;
  42. goto setflags_out;
  43. }
  44. flags = ext2_mask_flags(inode->i_mode, flags);
  45. mutex_lock(&inode->i_mutex);
  46. /* Is it quota file? Do not allow user to mess with it */
  47. if (IS_NOQUOTA(inode)) {
  48. mutex_unlock(&inode->i_mutex);
  49. ret = -EPERM;
  50. goto setflags_out;
  51. }
  52. oldflags = ei->i_flags;
  53. /*
  54. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  55. * the relevant capability.
  56. *
  57. * This test looks nicer. Thanks to Pauline Middelink
  58. */
  59. if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
  60. if (!capable(CAP_LINUX_IMMUTABLE)) {
  61. mutex_unlock(&inode->i_mutex);
  62. ret = -EPERM;
  63. goto setflags_out;
  64. }
  65. }
  66. flags = flags & EXT2_FL_USER_MODIFIABLE;
  67. flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
  68. ei->i_flags = flags;
  69. mutex_unlock(&inode->i_mutex);
  70. ext2_set_inode_flags(inode);
  71. inode->i_ctime = CURRENT_TIME_SEC;
  72. mark_inode_dirty(inode);
  73. setflags_out:
  74. mnt_drop_write(filp->f_path.mnt);
  75. return ret;
  76. }
  77. case EXT2_IOC_GETVERSION:
  78. return put_user(inode->i_generation, (int __user *) arg);
  79. case EXT2_IOC_SETVERSION:
  80. if (!is_owner_or_cap(inode))
  81. return -EPERM;
  82. ret = mnt_want_write(filp->f_path.mnt);
  83. if (ret)
  84. return ret;
  85. if (get_user(inode->i_generation, (int __user *) arg)) {
  86. ret = -EFAULT;
  87. } else {
  88. inode->i_ctime = CURRENT_TIME_SEC;
  89. mark_inode_dirty(inode);
  90. }
  91. mnt_drop_write(filp->f_path.mnt);
  92. return ret;
  93. case EXT2_IOC_GETRSVSZ:
  94. if (test_opt(inode->i_sb, RESERVATION)
  95. && S_ISREG(inode->i_mode)
  96. && ei->i_block_alloc_info) {
  97. rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
  98. return put_user(rsv_window_size, (int __user *)arg);
  99. }
  100. return -ENOTTY;
  101. case EXT2_IOC_SETRSVSZ: {
  102. if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
  103. return -ENOTTY;
  104. if (!is_owner_or_cap(inode))
  105. return -EACCES;
  106. if (get_user(rsv_window_size, (int __user *)arg))
  107. return -EFAULT;
  108. ret = mnt_want_write(filp->f_path.mnt);
  109. if (ret)
  110. return ret;
  111. if (rsv_window_size > EXT2_MAX_RESERVE_BLOCKS)
  112. rsv_window_size = EXT2_MAX_RESERVE_BLOCKS;
  113. /*
  114. * need to allocate reservation structure for this inode
  115. * before set the window size
  116. */
  117. /*
  118. * XXX What lock should protect the rsv_goal_size?
  119. * Accessed in ext2_get_block only. ext3 uses i_truncate.
  120. */
  121. mutex_lock(&ei->truncate_mutex);
  122. if (!ei->i_block_alloc_info)
  123. ext2_init_block_alloc_info(inode);
  124. if (ei->i_block_alloc_info){
  125. struct ext2_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
  126. rsv->rsv_goal_size = rsv_window_size;
  127. }
  128. mutex_unlock(&ei->truncate_mutex);
  129. mnt_drop_write(filp->f_path.mnt);
  130. return 0;
  131. }
  132. default:
  133. return -ENOTTY;
  134. }
  135. }
  136. #ifdef CONFIG_COMPAT
  137. long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  138. {
  139. /* These are just misnamed, they actually get/put from/to user an int */
  140. switch (cmd) {
  141. case EXT2_IOC32_GETFLAGS:
  142. cmd = EXT2_IOC_GETFLAGS;
  143. break;
  144. case EXT2_IOC32_SETFLAGS:
  145. cmd = EXT2_IOC_SETFLAGS;
  146. break;
  147. case EXT2_IOC32_GETVERSION:
  148. cmd = EXT2_IOC_GETVERSION;
  149. break;
  150. case EXT2_IOC32_SETVERSION:
  151. cmd = EXT2_IOC_SETVERSION;
  152. break;
  153. default:
  154. return -ENOIOCTLCMD;
  155. }
  156. return ext2_ioctl(file, cmd, (unsigned long) compat_ptr(arg));
  157. }
  158. #endif