ioctl.c 3.5 KB

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