acl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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. set_cached_acl(inode, type, acl);
  57. }
  58. kfree(value);
  59. } else if (size == -ENOENT || size == -ENODATA || size == 0) {
  60. /* FIXME, who returns -ENOENT? I think nobody */
  61. acl = NULL;
  62. set_cached_acl(inode, type, acl);
  63. } else {
  64. acl = ERR_PTR(-EIO);
  65. }
  66. return acl;
  67. }
  68. static int btrfs_xattr_acl_get(struct dentry *dentry, const char *name,
  69. void *value, size_t size, int type)
  70. {
  71. struct posix_acl *acl;
  72. int ret = 0;
  73. acl = btrfs_get_acl(dentry->d_inode, type);
  74. if (IS_ERR(acl))
  75. return PTR_ERR(acl);
  76. if (acl == NULL)
  77. return -ENODATA;
  78. ret = posix_acl_to_xattr(acl, value, size);
  79. posix_acl_release(acl);
  80. return ret;
  81. }
  82. /*
  83. * Needs to be called with fs_mutex held
  84. */
  85. static int btrfs_set_acl(struct btrfs_trans_handle *trans,
  86. struct inode *inode, struct posix_acl *acl, int type)
  87. {
  88. int ret, size = 0;
  89. const char *name;
  90. char *value = NULL;
  91. mode_t mode;
  92. if (acl) {
  93. ret = posix_acl_valid(acl);
  94. if (ret < 0)
  95. return ret;
  96. ret = 0;
  97. }
  98. switch (type) {
  99. case ACL_TYPE_ACCESS:
  100. mode = inode->i_mode;
  101. name = POSIX_ACL_XATTR_ACCESS;
  102. if (acl) {
  103. ret = posix_acl_equiv_mode(acl, &mode);
  104. if (ret < 0)
  105. return ret;
  106. inode->i_mode = mode;
  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(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 (value) {
  142. acl = posix_acl_from_xattr(value, size);
  143. if (acl == NULL) {
  144. value = NULL;
  145. size = 0;
  146. } else if (IS_ERR(acl)) {
  147. return PTR_ERR(acl);
  148. }
  149. }
  150. ret = btrfs_set_acl(NULL, dentry->d_inode, acl, type);
  151. posix_acl_release(acl);
  152. return ret;
  153. }
  154. int btrfs_check_acl(struct inode *inode, int mask)
  155. {
  156. struct posix_acl *acl;
  157. int error = -EAGAIN;
  158. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  159. if (IS_ERR(acl))
  160. return PTR_ERR(acl);
  161. if (acl) {
  162. error = posix_acl_permission(inode, acl, mask);
  163. posix_acl_release(acl);
  164. }
  165. return error;
  166. }
  167. /*
  168. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  169. * stuff has been fixed to work with that. If the locking stuff changes, we
  170. * need to re-evaluate the acl locking stuff.
  171. */
  172. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  173. struct inode *inode, struct inode *dir)
  174. {
  175. struct posix_acl *acl = NULL;
  176. int ret = 0;
  177. /* this happens with subvols */
  178. if (!dir)
  179. return 0;
  180. if (!S_ISLNK(inode->i_mode)) {
  181. if (IS_POSIXACL(dir)) {
  182. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  183. if (IS_ERR(acl))
  184. return PTR_ERR(acl);
  185. }
  186. if (!acl)
  187. inode->i_mode &= ~current_umask();
  188. }
  189. if (IS_POSIXACL(dir) && acl) {
  190. struct posix_acl *clone;
  191. mode_t mode;
  192. if (S_ISDIR(inode->i_mode)) {
  193. ret = btrfs_set_acl(trans, inode, acl,
  194. ACL_TYPE_DEFAULT);
  195. if (ret)
  196. goto failed;
  197. }
  198. clone = posix_acl_clone(acl, GFP_NOFS);
  199. ret = -ENOMEM;
  200. if (!clone)
  201. goto failed;
  202. mode = inode->i_mode;
  203. ret = posix_acl_create_masq(clone, &mode);
  204. if (ret >= 0) {
  205. inode->i_mode = mode;
  206. if (ret > 0) {
  207. /* we need an acl */
  208. ret = btrfs_set_acl(trans, inode, clone,
  209. ACL_TYPE_ACCESS);
  210. }
  211. }
  212. posix_acl_release(clone);
  213. }
  214. failed:
  215. posix_acl_release(acl);
  216. return ret;
  217. }
  218. int btrfs_acl_chmod(struct inode *inode)
  219. {
  220. struct posix_acl *acl, *clone;
  221. int ret = 0;
  222. if (S_ISLNK(inode->i_mode))
  223. return -EOPNOTSUPP;
  224. if (!IS_POSIXACL(inode))
  225. return 0;
  226. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  227. if (IS_ERR(acl) || !acl)
  228. return PTR_ERR(acl);
  229. clone = posix_acl_clone(acl, GFP_KERNEL);
  230. posix_acl_release(acl);
  231. if (!clone)
  232. return -ENOMEM;
  233. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  234. if (!ret)
  235. ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
  236. posix_acl_release(clone);
  237. return ret;
  238. }
  239. struct xattr_handler btrfs_xattr_acl_default_handler = {
  240. .prefix = POSIX_ACL_XATTR_DEFAULT,
  241. .flags = ACL_TYPE_DEFAULT,
  242. .get = btrfs_xattr_acl_get,
  243. .set = btrfs_xattr_acl_set,
  244. };
  245. struct xattr_handler btrfs_xattr_acl_access_handler = {
  246. .prefix = POSIX_ACL_XATTR_ACCESS,
  247. .flags = ACL_TYPE_ACCESS,
  248. .get = btrfs_xattr_acl_get,
  249. .set = btrfs_xattr_acl_set,
  250. };
  251. #else /* CONFIG_BTRFS_FS_POSIX_ACL */
  252. int btrfs_acl_chmod(struct inode *inode)
  253. {
  254. return 0;
  255. }
  256. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  257. struct inode *inode, struct inode *dir)
  258. {
  259. return 0;
  260. }
  261. #endif /* CONFIG_BTRFS_FS_POSIX_ACL */