acl.c 6.3 KB

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