acl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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. #ifdef CONFIG_BTRFS_FS_POSIX_ACL
  29. static struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
  30. {
  31. int size;
  32. const char *name;
  33. char *value = NULL;
  34. struct posix_acl *acl;
  35. acl = get_cached_acl(inode, type);
  36. if (acl != ACL_NOT_CACHED)
  37. return acl;
  38. switch (type) {
  39. case ACL_TYPE_ACCESS:
  40. name = POSIX_ACL_XATTR_ACCESS;
  41. break;
  42. case ACL_TYPE_DEFAULT:
  43. name = POSIX_ACL_XATTR_DEFAULT;
  44. break;
  45. default:
  46. BUG();
  47. }
  48. size = __btrfs_getxattr(inode, name, "", 0);
  49. if (size > 0) {
  50. value = kzalloc(size, GFP_NOFS);
  51. if (!value)
  52. return ERR_PTR(-ENOMEM);
  53. size = __btrfs_getxattr(inode, name, value, size);
  54. if (size > 0) {
  55. acl = posix_acl_from_xattr(value, size);
  56. if (IS_ERR(acl)) {
  57. kfree(value);
  58. return acl;
  59. }
  60. set_cached_acl(inode, type, acl);
  61. }
  62. kfree(value);
  63. } else if (size == -ENOENT || size == -ENODATA || size == 0) {
  64. /* FIXME, who returns -ENOENT? I think nobody */
  65. acl = NULL;
  66. set_cached_acl(inode, type, acl);
  67. } else {
  68. acl = ERR_PTR(-EIO);
  69. }
  70. return acl;
  71. }
  72. static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
  73. void *value, size_t size, int type)
  74. {
  75. struct posix_acl *acl;
  76. int ret = 0;
  77. acl = btrfs_get_acl(dentry->d_inode, type);
  78. if (IS_ERR(acl))
  79. return PTR_ERR(acl);
  80. if (acl == NULL)
  81. return -ENODATA;
  82. ret = posix_acl_to_xattr(acl, value, size);
  83. posix_acl_release(acl);
  84. return ret;
  85. }
  86. /*
  87. * Needs to be called with fs_mutex held
  88. */
  89. static int btrfs_set_acl(struct btrfs_trans_handle *trans,
  90. struct inode *inode, struct posix_acl *acl, int type)
  91. {
  92. int ret, size = 0;
  93. const char *name;
  94. char *value = NULL;
  95. mode_t mode;
  96. if (acl) {
  97. ret = posix_acl_valid(acl);
  98. if (ret < 0)
  99. return ret;
  100. ret = 0;
  101. }
  102. switch (type) {
  103. case ACL_TYPE_ACCESS:
  104. mode = inode->i_mode;
  105. name = POSIX_ACL_XATTR_ACCESS;
  106. if (acl) {
  107. ret = posix_acl_equiv_mode(acl, &mode);
  108. if (ret < 0)
  109. return ret;
  110. inode->i_mode = mode;
  111. }
  112. ret = 0;
  113. break;
  114. case ACL_TYPE_DEFAULT:
  115. if (!S_ISDIR(inode->i_mode))
  116. return acl ? -EINVAL : 0;
  117. name = POSIX_ACL_XATTR_DEFAULT;
  118. break;
  119. default:
  120. return -EINVAL;
  121. }
  122. if (acl) {
  123. size = posix_acl_xattr_size(acl->a_count);
  124. value = kmalloc(size, GFP_NOFS);
  125. if (!value) {
  126. ret = -ENOMEM;
  127. goto out;
  128. }
  129. ret = posix_acl_to_xattr(acl, value, size);
  130. if (ret < 0)
  131. goto out;
  132. }
  133. ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
  134. out:
  135. kfree(value);
  136. if (!ret)
  137. set_cached_acl(inode, type, acl);
  138. return ret;
  139. }
  140. static int btrfs_xattr_acl_set(struct dentry *dentry, const char *name,
  141. const void *value, size_t size, int flags, int type)
  142. {
  143. int ret;
  144. struct posix_acl *acl = NULL;
  145. if (!is_owner_or_cap(dentry->d_inode))
  146. return -EPERM;
  147. if (!IS_POSIXACL(dentry->d_inode))
  148. return -EOPNOTSUPP;
  149. if (value) {
  150. acl = posix_acl_from_xattr(value, size);
  151. if (acl == NULL) {
  152. value = NULL;
  153. size = 0;
  154. } else if (IS_ERR(acl)) {
  155. return PTR_ERR(acl);
  156. }
  157. }
  158. ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
  159. posix_acl_release(acl);
  160. return ret;
  161. }
  162. int btrfs_check_acl(struct inode *inode, int mask)
  163. {
  164. struct posix_acl *acl;
  165. int error = -EAGAIN;
  166. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  167. if (IS_ERR(acl))
  168. return PTR_ERR(acl);
  169. if (acl) {
  170. error = posix_acl_permission(inode, acl, mask);
  171. posix_acl_release(acl);
  172. }
  173. return error;
  174. }
  175. /*
  176. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  177. * stuff has been fixed to work with that. If the locking stuff changes, we
  178. * need to re-evaluate the acl locking stuff.
  179. */
  180. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  181. struct inode *inode, struct inode *dir)
  182. {
  183. struct posix_acl *acl = NULL;
  184. int ret = 0;
  185. /* this happens with subvols */
  186. if (!dir)
  187. return 0;
  188. if (!S_ISLNK(inode->i_mode)) {
  189. if (IS_POSIXACL(dir)) {
  190. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  191. if (IS_ERR(acl))
  192. return PTR_ERR(acl);
  193. }
  194. if (!acl)
  195. inode->i_mode &= ~current_umask();
  196. }
  197. if (IS_POSIXACL(dir) && acl) {
  198. struct posix_acl *clone;
  199. mode_t mode;
  200. if (S_ISDIR(inode->i_mode)) {
  201. ret = btrfs_set_acl(trans, inode, acl,
  202. ACL_TYPE_DEFAULT);
  203. if (ret)
  204. goto failed;
  205. }
  206. clone = posix_acl_clone(acl, GFP_NOFS);
  207. ret = -ENOMEM;
  208. if (!clone)
  209. goto failed;
  210. mode = inode->i_mode;
  211. ret = posix_acl_create_masq(clone, &mode);
  212. if (ret >= 0) {
  213. inode->i_mode = mode;
  214. if (ret > 0) {
  215. /* we need an acl */
  216. ret = btrfs_set_acl(trans, inode, clone,
  217. ACL_TYPE_ACCESS);
  218. }
  219. }
  220. posix_acl_release(clone);
  221. }
  222. failed:
  223. posix_acl_release(acl);
  224. return ret;
  225. }
  226. int btrfs_acl_chmod(struct inode *inode)
  227. {
  228. struct posix_acl *acl, *clone;
  229. int ret = 0;
  230. if (S_ISLNK(inode->i_mode))
  231. return -EOPNOTSUPP;
  232. if (!IS_POSIXACL(inode))
  233. return 0;
  234. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  235. if (IS_ERR(acl) || !acl)
  236. return PTR_ERR(acl);
  237. clone = posix_acl_clone(acl, GFP_KERNEL);
  238. posix_acl_release(acl);
  239. if (!clone)
  240. return -ENOMEM;
  241. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  242. if (!ret)
  243. ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
  244. posix_acl_release(clone);
  245. return ret;
  246. }
  247. const struct xattr_handler btrfs_xattr_acl_default_handler = {
  248. .prefix = POSIX_ACL_XATTR_DEFAULT,
  249. .flags = ACL_TYPE_DEFAULT,
  250. .get = btrfs_xattr_acl_get,
  251. .set = btrfs_xattr_acl_set,
  252. };
  253. const struct xattr_handler btrfs_xattr_acl_access_handler = {
  254. .prefix = POSIX_ACL_XATTR_ACCESS,
  255. .flags = ACL_TYPE_ACCESS,
  256. .get = btrfs_xattr_acl_get,
  257. .set = btrfs_xattr_acl_set,
  258. };
  259. #else /* CONFIG_BTRFS_FS_POSIX_ACL */
  260. int btrfs_acl_chmod(struct inode *inode)
  261. {
  262. return 0;
  263. }
  264. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  265. struct inode *inode, struct inode *dir)
  266. {
  267. return 0;
  268. }
  269. #endif /* CONFIG_BTRFS_FS_POSIX_ACL */