ioctl.c 4.1 KB

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