ioctl.c 2.7 KB

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