acl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /*
  2. * Copyright (C) 2007 Red Hat. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/xattr.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include <linux/posix_acl.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include "ctree.h"
  26. #include "btrfs_inode.h"
  27. #include "xattr.h"
  28. struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
  29. {
  30. int size;
  31. const char *name;
  32. char *value = NULL;
  33. struct posix_acl *acl;
  34. if (!IS_POSIXACL(inode))
  35. return NULL;
  36. acl = get_cached_acl(inode, type);
  37. if (acl != ACL_NOT_CACHED)
  38. return acl;
  39. switch (type) {
  40. case ACL_TYPE_ACCESS:
  41. name = POSIX_ACL_XATTR_ACCESS;
  42. break;
  43. case ACL_TYPE_DEFAULT:
  44. name = POSIX_ACL_XATTR_DEFAULT;
  45. break;
  46. default:
  47. BUG();
  48. }
  49. size = __btrfs_getxattr(inode, name, "", 0);
  50. if (size > 0) {
  51. value = kzalloc(size, GFP_NOFS);
  52. if (!value)
  53. return ERR_PTR(-ENOMEM);
  54. size = __btrfs_getxattr(inode, name, value, size);
  55. }
  56. if (size > 0) {
  57. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  58. } else if (size == -ENOENT || size == -ENODATA || size == 0) {
  59. /* FIXME, who returns -ENOENT? I think nobody */
  60. acl = NULL;
  61. } else {
  62. acl = ERR_PTR(-EIO);
  63. }
  64. kfree(value);
  65. if (!IS_ERR(acl))
  66. set_cached_acl(inode, type, acl);
  67. return acl;
  68. }
  69. static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
  70. void *value, size_t size, int type)
  71. {
  72. struct posix_acl *acl;
  73. int ret = 0;
  74. if (!IS_POSIXACL(dentry->d_inode))
  75. return -EOPNOTSUPP;
  76. acl = btrfs_get_acl(dentry->d_inode, type);
  77. if (IS_ERR(acl))
  78. return PTR_ERR(acl);
  79. if (acl == NULL)
  80. return -ENODATA;
  81. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  82. posix_acl_release(acl);
  83. return ret;
  84. }
  85. /*
  86. * Needs to be called with fs_mutex held
  87. */
  88. static int btrfs_set_acl(struct btrfs_trans_handle *trans,
  89. struct inode *inode, struct posix_acl *acl, int type)
  90. {
  91. int ret, size = 0;
  92. const char *name;
  93. char *value = NULL;
  94. if (acl) {
  95. ret = posix_acl_valid(acl);
  96. if (ret < 0)
  97. return ret;
  98. ret = 0;
  99. }
  100. switch (type) {
  101. case ACL_TYPE_ACCESS:
  102. name = POSIX_ACL_XATTR_ACCESS;
  103. if (acl) {
  104. ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  105. if (ret)
  106. return ret;
  107. }
  108. ret = 0;
  109. break;
  110. case ACL_TYPE_DEFAULT:
  111. if (!S_ISDIR(inode->i_mode))
  112. return acl ? -EINVAL : 0;
  113. name = POSIX_ACL_XATTR_DEFAULT;
  114. break;
  115. default:
  116. return -EINVAL;
  117. }
  118. if (acl) {
  119. size = posix_acl_xattr_size(acl->a_count);
  120. value = kmalloc(size, GFP_NOFS);
  121. if (!value) {
  122. ret = -ENOMEM;
  123. goto out;
  124. }
  125. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  126. if (ret < 0)
  127. goto out;
  128. }
  129. ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
  130. out:
  131. kfree(value);
  132. if (!ret)
  133. set_cached_acl(inode, type, acl);
  134. return ret;
  135. }
  136. static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
  137. const void *value, size_t size, int flags, int type)
  138. {
  139. int ret;
  140. struct posix_acl *acl = NULL;
  141. if (!inode_owner_or_capable(dentry->d_inode))
  142. return -EPERM;
  143. if (!IS_POSIXACL(dentry->d_inode))
  144. return -EOPNOTSUPP;
  145. if (value) {
  146. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  147. if (IS_ERR(acl))
  148. return PTR_ERR(acl);
  149. if (acl) {
  150. ret = posix_acl_valid(acl);
  151. if (ret)
  152. goto out;
  153. }
  154. }
  155. ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
  156. out:
  157. posix_acl_release(acl);
  158. return ret;
  159. }
  160. /*
  161. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  162. * stuff has been fixed to work with that. If the locking stuff changes, we
  163. * need to re-evaluate the acl locking stuff.
  164. */
  165. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  166. struct inode *inode, struct inode *dir)
  167. {
  168. struct posix_acl *acl = NULL;
  169. int ret = 0;
  170. /* this happens with subvols */
  171. if (!dir)
  172. return 0;
  173. if (!S_ISLNK(inode->i_mode)) {
  174. if (IS_POSIXACL(dir)) {
  175. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  176. if (IS_ERR(acl))
  177. return PTR_ERR(acl);
  178. }
  179. if (!acl)
  180. inode->i_mode &= ~current_umask();
  181. }
  182. if (IS_POSIXACL(dir) && acl) {
  183. if (S_ISDIR(inode->i_mode)) {
  184. ret = btrfs_set_acl(trans, inode, acl,
  185. ACL_TYPE_DEFAULT);
  186. if (ret)
  187. goto failed;
  188. }
  189. ret = __posix_acl_create(&acl, GFP_NOFS, &inode->i_mode);
  190. if (ret < 0)
  191. return ret;
  192. if (ret > 0) {
  193. /* we need an acl */
  194. ret = btrfs_set_acl(trans, inode, acl, ACL_TYPE_ACCESS);
  195. }
  196. }
  197. failed:
  198. posix_acl_release(acl);
  199. return ret;
  200. }
  201. int btrfs_acl_chmod(struct inode *inode)
  202. {
  203. struct posix_acl *acl;
  204. int ret = 0;
  205. if (S_ISLNK(inode->i_mode))
  206. return -EOPNOTSUPP;
  207. if (!IS_POSIXACL(inode))
  208. return 0;
  209. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  210. if (IS_ERR_OR_NULL(acl))
  211. return PTR_ERR(acl);
  212. ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  213. if (ret)
  214. return ret;
  215. ret = btrfs_set_acl(NULL, inode, acl, ACL_TYPE_ACCESS);
  216. posix_acl_release(acl);
  217. return ret;
  218. }
  219. const struct xattr_handler btrfs_xattr_acl_default_handler = {
  220. .prefix = POSIX_ACL_XATTR_DEFAULT,
  221. .flags = ACL_TYPE_DEFAULT,
  222. .get = btrfs_xattr_acl_get,
  223. .set = btrfs_xattr_acl_set,
  224. };
  225. const struct xattr_handler btrfs_xattr_acl_access_handler = {
  226. .prefix = POSIX_ACL_XATTR_ACCESS,
  227. .flags = ACL_TYPE_ACCESS,
  228. .get = btrfs_xattr_acl_get,
  229. .set = btrfs_xattr_acl_set,
  230. };