ioctl.c 3.9 KB

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