ioctl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "resize.h"
  20. #include <linux/ext2_fs.h>
  21. static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
  22. {
  23. int status;
  24. status = ocfs2_inode_lock(inode, NULL, 0);
  25. if (status < 0) {
  26. mlog_errno(status);
  27. return status;
  28. }
  29. ocfs2_get_inode_flags(OCFS2_I(inode));
  30. *flags = OCFS2_I(inode)->ip_attr;
  31. ocfs2_inode_unlock(inode, 0);
  32. mlog_exit(status);
  33. return status;
  34. }
  35. static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
  36. unsigned mask)
  37. {
  38. struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
  39. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  40. handle_t *handle = NULL;
  41. struct buffer_head *bh = NULL;
  42. unsigned oldflags;
  43. int status;
  44. mutex_lock(&inode->i_mutex);
  45. status = ocfs2_inode_lock(inode, &bh, 1);
  46. if (status < 0) {
  47. mlog_errno(status);
  48. goto bail;
  49. }
  50. status = -EACCES;
  51. if (!is_owner_or_cap(inode))
  52. goto bail_unlock;
  53. if (!S_ISDIR(inode->i_mode))
  54. flags &= ~OCFS2_DIRSYNC_FL;
  55. handle = ocfs2_start_trans(osb, 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(osb, handle);
  80. bail_unlock:
  81. ocfs2_inode_unlock(inode, 1);
  82. bail:
  83. mutex_unlock(&inode->i_mutex);
  84. brelse(bh);
  85. mlog_exit(status);
  86. return status;
  87. }
  88. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  89. {
  90. struct inode *inode = filp->f_path.dentry->d_inode;
  91. unsigned int flags;
  92. int new_clusters;
  93. int status;
  94. struct ocfs2_space_resv sr;
  95. struct ocfs2_new_group_input input;
  96. switch (cmd) {
  97. case OCFS2_IOC_GETFLAGS:
  98. status = ocfs2_get_inode_attr(inode, &flags);
  99. if (status < 0)
  100. return status;
  101. flags &= OCFS2_FL_VISIBLE;
  102. return put_user(flags, (int __user *) arg);
  103. case OCFS2_IOC_SETFLAGS:
  104. if (get_user(flags, (int __user *) arg))
  105. return -EFAULT;
  106. status = mnt_want_write(filp->f_path.mnt);
  107. if (status)
  108. return status;
  109. status = ocfs2_set_inode_attr(inode, flags,
  110. OCFS2_FL_MODIFIABLE);
  111. mnt_drop_write(filp->f_path.mnt);
  112. return status;
  113. case OCFS2_IOC_RESVSP:
  114. case OCFS2_IOC_RESVSP64:
  115. case OCFS2_IOC_UNRESVSP:
  116. case OCFS2_IOC_UNRESVSP64:
  117. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  118. return -EFAULT;
  119. return ocfs2_change_file_space(filp, cmd, &sr);
  120. case OCFS2_IOC_GROUP_EXTEND:
  121. if (!capable(CAP_SYS_RESOURCE))
  122. return -EPERM;
  123. if (get_user(new_clusters, (int __user *)arg))
  124. return -EFAULT;
  125. return ocfs2_group_extend(inode, new_clusters);
  126. case OCFS2_IOC_GROUP_ADD:
  127. case OCFS2_IOC_GROUP_ADD64:
  128. if (!capable(CAP_SYS_RESOURCE))
  129. return -EPERM;
  130. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  131. return -EFAULT;
  132. return ocfs2_group_add(inode, &input);
  133. default:
  134. return -ENOTTY;
  135. }
  136. }
  137. #ifdef CONFIG_COMPAT
  138. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  139. {
  140. switch (cmd) {
  141. case OCFS2_IOC32_GETFLAGS:
  142. cmd = OCFS2_IOC_GETFLAGS;
  143. break;
  144. case OCFS2_IOC32_SETFLAGS:
  145. cmd = OCFS2_IOC_SETFLAGS;
  146. break;
  147. case OCFS2_IOC_RESVSP:
  148. case OCFS2_IOC_RESVSP64:
  149. case OCFS2_IOC_UNRESVSP:
  150. case OCFS2_IOC_UNRESVSP64:
  151. case OCFS2_IOC_GROUP_EXTEND:
  152. case OCFS2_IOC_GROUP_ADD:
  153. case OCFS2_IOC_GROUP_ADD64:
  154. break;
  155. default:
  156. return -ENOIOCTLCMD;
  157. }
  158. return ocfs2_ioctl(file, cmd, arg);
  159. }
  160. #endif