ioctl.c 4.0 KB

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