ioctl.c 4.3 KB

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