ioctl.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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/compat.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 "refcounttree.h"
  22. #include <linux/ext2_fs.h>
  23. static int ocfs2_get_inode_attr(struct inode *inode, unsigned *flags)
  24. {
  25. int status;
  26. status = ocfs2_inode_lock(inode, NULL, 0);
  27. if (status < 0) {
  28. mlog_errno(status);
  29. return status;
  30. }
  31. ocfs2_get_inode_flags(OCFS2_I(inode));
  32. *flags = OCFS2_I(inode)->ip_attr;
  33. ocfs2_inode_unlock(inode, 0);
  34. mlog_exit(status);
  35. return status;
  36. }
  37. static int ocfs2_set_inode_attr(struct inode *inode, unsigned flags,
  38. unsigned mask)
  39. {
  40. struct ocfs2_inode_info *ocfs2_inode = OCFS2_I(inode);
  41. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  42. handle_t *handle = NULL;
  43. struct buffer_head *bh = NULL;
  44. unsigned oldflags;
  45. int status;
  46. mutex_lock(&inode->i_mutex);
  47. status = ocfs2_inode_lock(inode, &bh, 1);
  48. if (status < 0) {
  49. mlog_errno(status);
  50. goto bail;
  51. }
  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_inode_unlock(inode, 1);
  84. bail:
  85. mutex_unlock(&inode->i_mutex);
  86. brelse(bh);
  87. mlog_exit(status);
  88. return status;
  89. }
  90. long ocfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
  91. {
  92. struct inode *inode = filp->f_path.dentry->d_inode;
  93. unsigned int flags;
  94. int new_clusters;
  95. int status;
  96. struct ocfs2_space_resv sr;
  97. struct ocfs2_new_group_input input;
  98. struct reflink_arguments args;
  99. const char *old_path, *new_path;
  100. bool preserve;
  101. switch (cmd) {
  102. case OCFS2_IOC_GETFLAGS:
  103. status = ocfs2_get_inode_attr(inode, &flags);
  104. if (status < 0)
  105. return status;
  106. flags &= OCFS2_FL_VISIBLE;
  107. return put_user(flags, (int __user *) arg);
  108. case OCFS2_IOC_SETFLAGS:
  109. if (get_user(flags, (int __user *) arg))
  110. return -EFAULT;
  111. status = mnt_want_write(filp->f_path.mnt);
  112. if (status)
  113. return status;
  114. status = ocfs2_set_inode_attr(inode, flags,
  115. OCFS2_FL_MODIFIABLE);
  116. mnt_drop_write(filp->f_path.mnt);
  117. return status;
  118. case OCFS2_IOC_RESVSP:
  119. case OCFS2_IOC_RESVSP64:
  120. case OCFS2_IOC_UNRESVSP:
  121. case OCFS2_IOC_UNRESVSP64:
  122. if (copy_from_user(&sr, (int __user *) arg, sizeof(sr)))
  123. return -EFAULT;
  124. return ocfs2_change_file_space(filp, cmd, &sr);
  125. case OCFS2_IOC_GROUP_EXTEND:
  126. if (!capable(CAP_SYS_RESOURCE))
  127. return -EPERM;
  128. if (get_user(new_clusters, (int __user *)arg))
  129. return -EFAULT;
  130. return ocfs2_group_extend(inode, new_clusters);
  131. case OCFS2_IOC_GROUP_ADD:
  132. case OCFS2_IOC_GROUP_ADD64:
  133. if (!capable(CAP_SYS_RESOURCE))
  134. return -EPERM;
  135. if (copy_from_user(&input, (int __user *) arg, sizeof(input)))
  136. return -EFAULT;
  137. return ocfs2_group_add(inode, &input);
  138. case OCFS2_IOC_REFLINK:
  139. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  140. sizeof(args)))
  141. return -EFAULT;
  142. old_path = (const char *)(unsigned long)args.old_path;
  143. new_path = (const char *)(unsigned long)args.new_path;
  144. preserve = (args.preserve != 0);
  145. return ocfs2_reflink_ioctl(inode, old_path, new_path, preserve);
  146. default:
  147. return -ENOTTY;
  148. }
  149. }
  150. #ifdef CONFIG_COMPAT
  151. long ocfs2_compat_ioctl(struct file *file, unsigned cmd, unsigned long arg)
  152. {
  153. bool preserve;
  154. struct reflink_arguments args;
  155. struct inode *inode = file->f_path.dentry->d_inode;
  156. switch (cmd) {
  157. case OCFS2_IOC32_GETFLAGS:
  158. cmd = OCFS2_IOC_GETFLAGS;
  159. break;
  160. case OCFS2_IOC32_SETFLAGS:
  161. cmd = OCFS2_IOC_SETFLAGS;
  162. break;
  163. case OCFS2_IOC_RESVSP:
  164. case OCFS2_IOC_RESVSP64:
  165. case OCFS2_IOC_UNRESVSP:
  166. case OCFS2_IOC_UNRESVSP64:
  167. case OCFS2_IOC_GROUP_EXTEND:
  168. case OCFS2_IOC_GROUP_ADD:
  169. case OCFS2_IOC_GROUP_ADD64:
  170. break;
  171. case OCFS2_IOC_REFLINK:
  172. if (copy_from_user(&args, (struct reflink_arguments *)arg,
  173. sizeof(args)))
  174. return -EFAULT;
  175. preserve = (args.preserve != 0);
  176. return ocfs2_reflink_ioctl(inode, compat_ptr(args.old_path),
  177. compat_ptr(args.new_path), preserve);
  178. default:
  179. return -ENOIOCTLCMD;
  180. }
  181. return ocfs2_ioctl(file, cmd, arg);
  182. }
  183. #endif