ioctl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * linux/fs/jfs/ioctl.c
  3. *
  4. * Copyright (C) 2006 Herbert Poetzl
  5. * adapted from Remy Card's ext2/ioctl.c
  6. */
  7. #include <linux/fs.h>
  8. #include <linux/ctype.h>
  9. #include <linux/capability.h>
  10. #include <linux/mount.h>
  11. #include <linux/time.h>
  12. #include <linux/sched.h>
  13. #include <asm/current.h>
  14. #include <asm/uaccess.h>
  15. #include "jfs_incore.h"
  16. #include "jfs_dinode.h"
  17. #include "jfs_inode.h"
  18. static struct {
  19. long jfs_flag;
  20. long ext2_flag;
  21. } jfs_map[] = {
  22. {JFS_NOATIME_FL, FS_NOATIME_FL},
  23. {JFS_DIRSYNC_FL, FS_DIRSYNC_FL},
  24. {JFS_SYNC_FL, FS_SYNC_FL},
  25. {JFS_SECRM_FL, FS_SECRM_FL},
  26. {JFS_UNRM_FL, FS_UNRM_FL},
  27. {JFS_APPEND_FL, FS_APPEND_FL},
  28. {JFS_IMMUTABLE_FL, FS_IMMUTABLE_FL},
  29. {0, 0},
  30. };
  31. static long jfs_map_ext2(unsigned long flags, int from)
  32. {
  33. int index=0;
  34. long mapped=0;
  35. while (jfs_map[index].jfs_flag) {
  36. if (from) {
  37. if (jfs_map[index].ext2_flag & flags)
  38. mapped |= jfs_map[index].jfs_flag;
  39. } else {
  40. if (jfs_map[index].jfs_flag & flags)
  41. mapped |= jfs_map[index].ext2_flag;
  42. }
  43. index++;
  44. }
  45. return mapped;
  46. }
  47. long jfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  48. {
  49. struct inode *inode = filp->f_dentry->d_inode;
  50. struct jfs_inode_info *jfs_inode = JFS_IP(inode);
  51. unsigned int flags;
  52. switch (cmd) {
  53. case JFS_IOC_GETFLAGS:
  54. jfs_get_inode_flags(jfs_inode);
  55. flags = jfs_inode->mode2 & JFS_FL_USER_VISIBLE;
  56. flags = jfs_map_ext2(flags, 0);
  57. return put_user(flags, (int __user *) arg);
  58. case JFS_IOC_SETFLAGS: {
  59. unsigned int oldflags;
  60. int err;
  61. err = mnt_want_write(filp->f_path.mnt);
  62. if (err)
  63. return err;
  64. if (!inode_owner_or_capable(inode)) {
  65. err = -EACCES;
  66. goto setflags_out;
  67. }
  68. if (get_user(flags, (int __user *) arg)) {
  69. err = -EFAULT;
  70. goto setflags_out;
  71. }
  72. flags = jfs_map_ext2(flags, 1);
  73. if (!S_ISDIR(inode->i_mode))
  74. flags &= ~JFS_DIRSYNC_FL;
  75. /* Is it quota file? Do not allow user to mess with it */
  76. if (IS_NOQUOTA(inode)) {
  77. err = -EPERM;
  78. goto setflags_out;
  79. }
  80. /* Lock against other parallel changes of flags */
  81. mutex_lock(&inode->i_mutex);
  82. jfs_get_inode_flags(jfs_inode);
  83. oldflags = jfs_inode->mode2;
  84. /*
  85. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  86. * the relevant capability.
  87. */
  88. if ((oldflags & JFS_IMMUTABLE_FL) ||
  89. ((flags ^ oldflags) &
  90. (JFS_APPEND_FL | JFS_IMMUTABLE_FL))) {
  91. if (!capable(CAP_LINUX_IMMUTABLE)) {
  92. mutex_unlock(&inode->i_mutex);
  93. err = -EPERM;
  94. goto setflags_out;
  95. }
  96. }
  97. flags = flags & JFS_FL_USER_MODIFIABLE;
  98. flags |= oldflags & ~JFS_FL_USER_MODIFIABLE;
  99. jfs_inode->mode2 = flags;
  100. jfs_set_inode_flags(inode);
  101. mutex_unlock(&inode->i_mutex);
  102. inode->i_ctime = CURRENT_TIME_SEC;
  103. mark_inode_dirty(inode);
  104. setflags_out:
  105. mnt_drop_write(filp->f_path.mnt);
  106. return err;
  107. }
  108. default:
  109. return -ENOTTY;
  110. }
  111. }
  112. #ifdef CONFIG_COMPAT
  113. long jfs_compat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  114. {
  115. /* While these ioctl numbers defined with 'long' and have different
  116. * numbers than the 64bit ABI,
  117. * the actual implementation only deals with ints and is compatible.
  118. */
  119. switch (cmd) {
  120. case JFS_IOC_GETFLAGS32:
  121. cmd = JFS_IOC_GETFLAGS;
  122. break;
  123. case JFS_IOC_SETFLAGS32:
  124. cmd = JFS_IOC_SETFLAGS;
  125. break;
  126. }
  127. return jfs_ioctl(filp, cmd, arg);
  128. }
  129. #endif