ioctl.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * linux/fs/ext3/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 <linux/fs.h>
  10. #include <linux/jbd.h>
  11. #include <linux/capability.h>
  12. #include <linux/ext3_fs.h>
  13. #include <linux/ext3_jbd.h>
  14. #include <linux/time.h>
  15. #include <linux/compat.h>
  16. #include <linux/smp_lock.h>
  17. #include <asm/uaccess.h>
  18. int ext3_ioctl (struct inode * inode, struct file * filp, unsigned int cmd,
  19. unsigned long arg)
  20. {
  21. struct ext3_inode_info *ei = EXT3_I(inode);
  22. unsigned int flags;
  23. unsigned short rsv_window_size;
  24. ext3_debug ("cmd = %u, arg = %lu\n", cmd, arg);
  25. switch (cmd) {
  26. case EXT3_IOC_GETFLAGS:
  27. ext3_get_inode_flags(ei);
  28. flags = ei->i_flags & EXT3_FL_USER_VISIBLE;
  29. return put_user(flags, (int __user *) arg);
  30. case EXT3_IOC_SETFLAGS: {
  31. handle_t *handle = NULL;
  32. int err;
  33. struct ext3_iloc iloc;
  34. unsigned int oldflags;
  35. unsigned int jflag;
  36. if (IS_RDONLY(inode))
  37. return -EROFS;
  38. if (!is_owner_or_cap(inode))
  39. return -EACCES;
  40. if (get_user(flags, (int __user *) arg))
  41. return -EFAULT;
  42. if (!S_ISDIR(inode->i_mode))
  43. flags &= ~EXT3_DIRSYNC_FL;
  44. mutex_lock(&inode->i_mutex);
  45. oldflags = ei->i_flags;
  46. /* The JOURNAL_DATA flag is modifiable only by root */
  47. jflag = flags & EXT3_JOURNAL_DATA_FL;
  48. /*
  49. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  50. * the relevant capability.
  51. *
  52. * This test looks nicer. Thanks to Pauline Middelink
  53. */
  54. if ((flags ^ oldflags) & (EXT3_APPEND_FL | EXT3_IMMUTABLE_FL)) {
  55. if (!capable(CAP_LINUX_IMMUTABLE)) {
  56. mutex_unlock(&inode->i_mutex);
  57. return -EPERM;
  58. }
  59. }
  60. /*
  61. * The JOURNAL_DATA flag can only be changed by
  62. * the relevant capability.
  63. */
  64. if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL)) {
  65. if (!capable(CAP_SYS_RESOURCE)) {
  66. mutex_unlock(&inode->i_mutex);
  67. return -EPERM;
  68. }
  69. }
  70. handle = ext3_journal_start(inode, 1);
  71. if (IS_ERR(handle)) {
  72. mutex_unlock(&inode->i_mutex);
  73. return PTR_ERR(handle);
  74. }
  75. if (IS_SYNC(inode))
  76. handle->h_sync = 1;
  77. err = ext3_reserve_inode_write(handle, inode, &iloc);
  78. if (err)
  79. goto flags_err;
  80. flags = flags & EXT3_FL_USER_MODIFIABLE;
  81. flags |= oldflags & ~EXT3_FL_USER_MODIFIABLE;
  82. ei->i_flags = flags;
  83. ext3_set_inode_flags(inode);
  84. inode->i_ctime = CURRENT_TIME_SEC;
  85. err = ext3_mark_iloc_dirty(handle, inode, &iloc);
  86. flags_err:
  87. ext3_journal_stop(handle);
  88. if (err) {
  89. mutex_unlock(&inode->i_mutex);
  90. return err;
  91. }
  92. if ((jflag ^ oldflags) & (EXT3_JOURNAL_DATA_FL))
  93. err = ext3_change_inode_journal_flag(inode, jflag);
  94. mutex_unlock(&inode->i_mutex);
  95. return err;
  96. }
  97. case EXT3_IOC_GETVERSION:
  98. case EXT3_IOC_GETVERSION_OLD:
  99. return put_user(inode->i_generation, (int __user *) arg);
  100. case EXT3_IOC_SETVERSION:
  101. case EXT3_IOC_SETVERSION_OLD: {
  102. handle_t *handle;
  103. struct ext3_iloc iloc;
  104. __u32 generation;
  105. int err;
  106. if (!is_owner_or_cap(inode))
  107. return -EPERM;
  108. if (IS_RDONLY(inode))
  109. return -EROFS;
  110. if (get_user(generation, (int __user *) arg))
  111. return -EFAULT;
  112. handle = ext3_journal_start(inode, 1);
  113. if (IS_ERR(handle))
  114. return PTR_ERR(handle);
  115. err = ext3_reserve_inode_write(handle, inode, &iloc);
  116. if (err == 0) {
  117. inode->i_ctime = CURRENT_TIME_SEC;
  118. inode->i_generation = generation;
  119. err = ext3_mark_iloc_dirty(handle, inode, &iloc);
  120. }
  121. ext3_journal_stop(handle);
  122. return err;
  123. }
  124. #ifdef CONFIG_JBD_DEBUG
  125. case EXT3_IOC_WAIT_FOR_READONLY:
  126. /*
  127. * This is racy - by the time we're woken up and running,
  128. * the superblock could be released. And the module could
  129. * have been unloaded. So sue me.
  130. *
  131. * Returns 1 if it slept, else zero.
  132. */
  133. {
  134. struct super_block *sb = inode->i_sb;
  135. DECLARE_WAITQUEUE(wait, current);
  136. int ret = 0;
  137. set_current_state(TASK_INTERRUPTIBLE);
  138. add_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
  139. if (timer_pending(&EXT3_SB(sb)->turn_ro_timer)) {
  140. schedule();
  141. ret = 1;
  142. }
  143. remove_wait_queue(&EXT3_SB(sb)->ro_wait_queue, &wait);
  144. return ret;
  145. }
  146. #endif
  147. case EXT3_IOC_GETRSVSZ:
  148. if (test_opt(inode->i_sb, RESERVATION)
  149. && S_ISREG(inode->i_mode)
  150. && ei->i_block_alloc_info) {
  151. rsv_window_size = ei->i_block_alloc_info->rsv_window_node.rsv_goal_size;
  152. return put_user(rsv_window_size, (int __user *)arg);
  153. }
  154. return -ENOTTY;
  155. case EXT3_IOC_SETRSVSZ: {
  156. if (!test_opt(inode->i_sb, RESERVATION) ||!S_ISREG(inode->i_mode))
  157. return -ENOTTY;
  158. if (IS_RDONLY(inode))
  159. return -EROFS;
  160. if (!is_owner_or_cap(inode))
  161. return -EACCES;
  162. if (get_user(rsv_window_size, (int __user *)arg))
  163. return -EFAULT;
  164. if (rsv_window_size > EXT3_MAX_RESERVE_BLOCKS)
  165. rsv_window_size = EXT3_MAX_RESERVE_BLOCKS;
  166. /*
  167. * need to allocate reservation structure for this inode
  168. * before set the window size
  169. */
  170. mutex_lock(&ei->truncate_mutex);
  171. if (!ei->i_block_alloc_info)
  172. ext3_init_block_alloc_info(inode);
  173. if (ei->i_block_alloc_info){
  174. struct ext3_reserve_window_node *rsv = &ei->i_block_alloc_info->rsv_window_node;
  175. rsv->rsv_goal_size = rsv_window_size;
  176. }
  177. mutex_unlock(&ei->truncate_mutex);
  178. return 0;
  179. }
  180. case EXT3_IOC_GROUP_EXTEND: {
  181. ext3_fsblk_t n_blocks_count;
  182. struct super_block *sb = inode->i_sb;
  183. int err;
  184. if (!capable(CAP_SYS_RESOURCE))
  185. return -EPERM;
  186. if (IS_RDONLY(inode))
  187. return -EROFS;
  188. if (get_user(n_blocks_count, (__u32 __user *)arg))
  189. return -EFAULT;
  190. err = ext3_group_extend(sb, EXT3_SB(sb)->s_es, n_blocks_count);
  191. journal_lock_updates(EXT3_SB(sb)->s_journal);
  192. journal_flush(EXT3_SB(sb)->s_journal);
  193. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  194. return err;
  195. }
  196. case EXT3_IOC_GROUP_ADD: {
  197. struct ext3_new_group_data input;
  198. struct super_block *sb = inode->i_sb;
  199. int err;
  200. if (!capable(CAP_SYS_RESOURCE))
  201. return -EPERM;
  202. if (IS_RDONLY(inode))
  203. return -EROFS;
  204. if (copy_from_user(&input, (struct ext3_new_group_input __user *)arg,
  205. sizeof(input)))
  206. return -EFAULT;
  207. err = ext3_group_add(sb, &input);
  208. journal_lock_updates(EXT3_SB(sb)->s_journal);
  209. journal_flush(EXT3_SB(sb)->s_journal);
  210. journal_unlock_updates(EXT3_SB(sb)->s_journal);
  211. return err;
  212. }
  213. default:
  214. return -ENOTTY;
  215. }
  216. }
  217. #ifdef CONFIG_COMPAT
  218. long ext3_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  219. {
  220. struct inode *inode = file->f_path.dentry->d_inode;
  221. int ret;
  222. /* These are just misnamed, they actually get/put from/to user an int */
  223. switch (cmd) {
  224. case EXT3_IOC32_GETFLAGS:
  225. cmd = EXT3_IOC_GETFLAGS;
  226. break;
  227. case EXT3_IOC32_SETFLAGS:
  228. cmd = EXT3_IOC_SETFLAGS;
  229. break;
  230. case EXT3_IOC32_GETVERSION:
  231. cmd = EXT3_IOC_GETVERSION;
  232. break;
  233. case EXT3_IOC32_SETVERSION:
  234. cmd = EXT3_IOC_SETVERSION;
  235. break;
  236. case EXT3_IOC32_GROUP_EXTEND:
  237. cmd = EXT3_IOC_GROUP_EXTEND;
  238. break;
  239. case EXT3_IOC32_GETVERSION_OLD:
  240. cmd = EXT3_IOC_GETVERSION_OLD;
  241. break;
  242. case EXT3_IOC32_SETVERSION_OLD:
  243. cmd = EXT3_IOC_SETVERSION_OLD;
  244. break;
  245. #ifdef CONFIG_JBD_DEBUG
  246. case EXT3_IOC32_WAIT_FOR_READONLY:
  247. cmd = EXT3_IOC_WAIT_FOR_READONLY;
  248. break;
  249. #endif
  250. case EXT3_IOC32_GETRSVSZ:
  251. cmd = EXT3_IOC_GETRSVSZ;
  252. break;
  253. case EXT3_IOC32_SETRSVSZ:
  254. cmd = EXT3_IOC_SETRSVSZ;
  255. break;
  256. case EXT3_IOC_GROUP_ADD:
  257. break;
  258. default:
  259. return -ENOIOCTLCMD;
  260. }
  261. lock_kernel();
  262. ret = ext3_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
  263. unlock_kernel();
  264. return ret;
  265. }
  266. #endif