ioctl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. ext2_debug ("cmd = %u, arg = %lu\n", cmd, arg);
  23. switch (cmd) {
  24. case EXT2_IOC_GETFLAGS:
  25. flags = ei->i_flags & EXT2_FL_USER_VISIBLE;
  26. return put_user(flags, (int __user *) arg);
  27. case EXT2_IOC_SETFLAGS: {
  28. unsigned int oldflags;
  29. if (IS_RDONLY(inode))
  30. return -EROFS;
  31. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  32. return -EACCES;
  33. if (get_user(flags, (int __user *) arg))
  34. return -EFAULT;
  35. if (!S_ISDIR(inode->i_mode))
  36. flags &= ~EXT2_DIRSYNC_FL;
  37. mutex_lock(&inode->i_mutex);
  38. oldflags = ei->i_flags;
  39. /*
  40. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  41. * the relevant capability.
  42. *
  43. * This test looks nicer. Thanks to Pauline Middelink
  44. */
  45. if ((flags ^ oldflags) & (EXT2_APPEND_FL | EXT2_IMMUTABLE_FL)) {
  46. if (!capable(CAP_LINUX_IMMUTABLE)) {
  47. mutex_unlock(&inode->i_mutex);
  48. return -EPERM;
  49. }
  50. }
  51. flags = flags & EXT2_FL_USER_MODIFIABLE;
  52. flags |= oldflags & ~EXT2_FL_USER_MODIFIABLE;
  53. ei->i_flags = flags;
  54. mutex_unlock(&inode->i_mutex);
  55. ext2_set_inode_flags(inode);
  56. inode->i_ctime = CURRENT_TIME_SEC;
  57. mark_inode_dirty(inode);
  58. return 0;
  59. }
  60. case EXT2_IOC_GETVERSION:
  61. return put_user(inode->i_generation, (int __user *) arg);
  62. case EXT2_IOC_SETVERSION:
  63. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  64. return -EPERM;
  65. if (IS_RDONLY(inode))
  66. return -EROFS;
  67. if (get_user(inode->i_generation, (int __user *) arg))
  68. return -EFAULT;
  69. inode->i_ctime = CURRENT_TIME_SEC;
  70. mark_inode_dirty(inode);
  71. return 0;
  72. default:
  73. return -ENOTTY;
  74. }
  75. }
  76. #ifdef CONFIG_COMPAT
  77. long ext2_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  78. {
  79. struct inode *inode = file->f_path.dentry->d_inode;
  80. int ret;
  81. /* These are just misnamed, they actually get/put from/to user an int */
  82. switch (cmd) {
  83. case EXT2_IOC32_GETFLAGS:
  84. cmd = EXT2_IOC_GETFLAGS;
  85. break;
  86. case EXT2_IOC32_SETFLAGS:
  87. cmd = EXT2_IOC_SETFLAGS;
  88. break;
  89. case EXT2_IOC32_GETVERSION:
  90. cmd = EXT2_IOC_GETVERSION;
  91. break;
  92. case EXT2_IOC32_SETVERSION:
  93. cmd = EXT2_IOC_SETVERSION;
  94. break;
  95. default:
  96. return -ENOIOCTLCMD;
  97. }
  98. lock_kernel();
  99. ret = ext2_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg));
  100. unlock_kernel();
  101. return ret;
  102. }
  103. #endif