ioctl.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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, NULL, 0);
  23. if (status < 0) {
  24. mlog_errno(status);
  25. return status;
  26. }
  27. *flags = OCFS2_I(inode)->ip_attr;
  28. ocfs2_meta_unlock(inode, 0);
  29. mlog_exit(status);
  30. return status;
  31. }
  32. static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
  33. unsigned mask)
  34. {
  35. struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
  36. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  37. struct ocfs2_journal_handle *handle = NULL;
  38. struct buffer_head *bh = NULL;
  39. unsigned oldflags;
  40. int status;
  41. mutex_lock(&inode->i_mutex);
  42. status = ocfs2_meta_lock(inode, NULL, &bh, 1);
  43. if (status < 0) {
  44. mlog_errno(status);
  45. goto bail;
  46. }
  47. status = -EROFS;
  48. if (IS_RDONLY(inode))
  49. goto bail_unlock;
  50. status = -EACCES;
  51. if ((current->fsuid != inode->i_uid) && !capable(CAP_FOWNER))
  52. goto bail_unlock;
  53. if (!S_ISDIR(inode->i_mode))
  54. flags &= ~OCFS2_DIRSYNC_FL;
  55. handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS);
  56. if (IS_ERR(handle)) {
  57. status = PTR_ERR(handle);
  58. mlog_errno(status);
  59. goto bail_unlock;
  60. }
  61. oldflags = ocfs2_inode->ip_attr;
  62. flags = flags & mask;
  63. flags |= oldflags & ~mask;
  64. /*
  65. * The IMMUTABLE and APPEND_ONLY flags can only be changed by
  66. * the relevant capability.
  67. */
  68. status = -EPERM;
  69. if ((oldflags & OCFS2_IMMUTABLE_FL) || ((flags ^ oldflags) &
  70. (OCFS2_APPEND_FL | OCFS2_IMMUTABLE_FL))) {
  71. if (!capable(CAP_LINUX_IMMUTABLE))
  72. goto bail_unlock;
  73. }
  74. ocfs2_inode->ip_attr = flags;
  75. ocfs2_set_inode_flags(inode);
  76. status = ocfs2_mark_inode_dirty(handle, inode, bh);
  77. if (status < 0)
  78. mlog_errno(status);
  79. ocfs2_commit_trans(handle);
  80. bail_unlock:
  81. ocfs2_meta_unlock(inode, 1);
  82. bail:
  83. mutex_unlock(&inode->i_mutex);
  84. if (bh)
  85. brelse(bh);
  86. mlog_exit(status);
  87. return status;
  88. }
  89. int ocfs2_ioctl(struct inode * inode, struct file * filp,
  90. unsigned int cmd, unsigned long arg)
  91. {
  92. unsigned int flags;
  93. int status;
  94. switch (cmd) {
  95. case OCFS2_IOC_GETFLAGS:
  96. status = ocfs2_get_inode_attr(inode, &flags);
  97. if (status < 0)
  98. return status;
  99. flags &= OCFS2_FL_VISIBLE;
  100. return put_user(flags, (int __user *) arg);
  101. case OCFS2_IOC_SETFLAGS:
  102. if (get_user(flags, (int __user *) arg))
  103. return -EFAULT;
  104. return ocfs2_set_inode_attr(inode, flags,
  105. OCFS2_FL_MODIFIABLE);
  106. default:
  107. return -ENOTTY;
  108. }
  109. }