ioctl.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * linux/fs/ocfs2/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/mount.h>
  9. #define MLOG_MASK_PREFIX ML_INODE
  10. #include <cluster/masklog.h>
  11. #include "ocfs2.h"
  12. #include "alloc.h"
  13. #include "dlmglue.h"
  14. #include "inode.h"
  15. #include "journal.h"
  16. #include "ocfs2_fs.h"
  17. #include "ioctl.h"
  18. #include <linux/ext2_fs.h>
  19. static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
  20. {
  21. int status;
  22. status = ocfs2_meta_lock(inode, NULL, 0);
  23. if (status < 0) {
  24. mlog_errno(status);
  25. return status;
  26. }
  27. ocfs2_get_inode_flags(OCFS2_I(inode));
  28. *flags = OCFS2_I(inode)->ip_attr;
  29. ocfs2_meta_unlock(inode, 0);
  30. mlog_exit(status);
  31. return status;
  32. }
  33. static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
  34. unsigned mask)
  35. {
  36. struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
  37. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  38. handle_t *handle = NULL;
  39. struct buffer_head *bh = NULL;
  40. unsigned oldflags;
  41. int status;
  42. mutex_lock(&inode->i_mutex);
  43. status = ocfs2_meta_lock(inode, &bh, 1);
  44. if (status < 0) {
  45. mlog_errno(status);
  46. goto bail;
  47. }
  48. status = -EROFS;
  49. if (IS_RDONLY(inode))
  50. goto bail_unlock;
  51. status = -EACCES;
  52. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  53. goto bail_unlock;
  54. if (!S_ISDIR(inode->i_mode))
  55. flags &= ~OCFS2_DIRSYNC_FL;
  56. handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
  57. if (IS_ERR(handle)) {
  58. status = PTR_ERR(handle);
  59. mlog_errno(status);
  60. goto bail_unlock;
  61. }
  62. oldflags = ocfs2_inode->ip_attr;
  63. flags = flags & mask;
  64. flags |= oldflags & ~mask;
  65. /*
  66. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  67. * the relevant capability.
  68. */
  69. status = -EPERM;
  70. if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
  71. (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
  72. if (!capable(CAP_LINUX_IMMUTABLE))
  73. goto bail_unlock;
  74. }
  75. ocfs2_inode->ip_attr = flags;
  76. ocfs2_set_inode_flags(inode);
  77. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  78. if (status < 0)
  79. mlog_errno(status);
  80. ocfs2_commit_trans(osb, handle);
  81. bail_unlock:
  82. ocfs2_meta_unlock(inode, 1);
  83. bail:
  84. mutex_unlock(&inode->i_mutex);
  85. if (bh)
  86. brelse(bh);
  87. mlog_exit(status);
  88. return status;
  89. }
  90. int ocfs2_ioctl(struct inode * inode, struct file * filp,
  91. unsigned int cmd, unsigned long arg)
  92. {
  93. unsigned int flags;
  94. int status;
  95. switch (cmd) {
  96. case OCFS2_IOC_GETFLAGS:
  97. status = ocfs2_get_inode_attr(inode, &flags);
  98. if (status < 0)
  99. return status;
  100. flags &= OCFS2_FL_VISIBLE;
  101. return put_user(flags, (int __user *) arg);
  102. case OCFS2_IOC_SETFLAGS:
  103. if (get_user(flags, (int __user *) arg))
  104. return -EFAULT;
  105. return ocfs2_set_inode_attr(inode, flags,
  106. OCFS2_FL_MODIFIABLE);
  107. default:
  108. return -ENOTTY;
  109. }
  110. }
  111. #ifdef CONFIG_COMPAT
  112. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  113. {
  114. struct inode *inode = file->f_path.dentry->d_inode;
  115. int ret;
  116. switch (cmd) {
  117. case OCFS2_IOC32_GETFLAGS:
  118. cmd = OCFS2_IOC_GETFLAGS;
  119. break;
  120. case OCFS2_IOC32_SETFLAGS:
  121. cmd = OCFS2_IOC_SETFLAGS;
  122. break;
  123. default:
  124. return -ENOIOCTLCMD;
  125. }
  126. lock_kernel();
  127. ret = ocfs2_ioctl(inode, file, cmd, arg);
  128. unlock_kernel();
  129. return ret;
  130. }
  131. #endif