acl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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_get_acl(struct inode *inode, int type,
  68. void *value, size_t size)
  69. {
  70. struct posix_acl *acl;
  71. int ret = 0;
  72. acl = btrfs_get_acl(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_set_acl(struct inode *inode, int type,
  134. const void *value, size_t size)
  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, inode, acl, type);
  148. posix_acl_release(acl);
  149. return ret;
  150. }
  151. static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
  152. void *value, size_t size)
  153. {
  154. return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
  155. }
  156. static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
  157. const void *value, size_t size, int flags)
  158. {
  159. return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  160. }
  161. static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
  162. void *value, size_t size)
  163. {
  164. return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
  165. }
  166. static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
  167. const void *value, size_t size, int flags)
  168. {
  169. return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  170. }
  171. int btrfs_check_acl(struct inode *inode, int mask)
  172. {
  173. struct posix_acl *acl;
  174. int error = -EAGAIN;
  175. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  176. if (IS_ERR(acl))
  177. return PTR_ERR(acl);
  178. if (acl) {
  179. error = posix_acl_permission(inode, acl, mask);
  180. posix_acl_release(acl);
  181. }
  182. return error;
  183. }
  184. /*
  185. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  186. * stuff has been fixed to work with that. If the locking stuff changes, we
  187. * need to re-evaluate the acl locking stuff.
  188. */
  189. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  190. struct inode *inode, struct inode *dir)
  191. {
  192. struct posix_acl *acl = NULL;
  193. int ret = 0;
  194. /* this happens with subvols */
  195. if (!dir)
  196. return 0;
  197. if (!S_ISLNK(inode->i_mode)) {
  198. if (IS_POSIXACL(dir)) {
  199. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  200. if (IS_ERR(acl))
  201. return PTR_ERR(acl);
  202. }
  203. if (!acl)
  204. inode->i_mode &= ~current_umask();
  205. }
  206. if (IS_POSIXACL(dir) && acl) {
  207. struct posix_acl *clone;
  208. mode_t mode;
  209. if (S_ISDIR(inode->i_mode)) {
  210. ret = btrfs_set_acl(trans, inode, acl,
  211. ACL_TYPE_DEFAULT);
  212. if (ret)
  213. goto failed;
  214. }
  215. clone = posix_acl_clone(acl, GFP_NOFS);
  216. ret = -ENOMEM;
  217. if (!clone)
  218. goto failed;
  219. mode = inode->i_mode;
  220. ret = posix_acl_create_masq(clone, &mode);
  221. if (ret >= 0) {
  222. inode->i_mode = mode;
  223. if (ret > 0) {
  224. /* we need an acl */
  225. ret = btrfs_set_acl(trans, inode, clone,
  226. ACL_TYPE_ACCESS);
  227. }
  228. }
  229. }
  230. failed:
  231. posix_acl_release(acl);
  232. return ret;
  233. }
  234. int btrfs_acl_chmod(struct inode *inode)
  235. {
  236. struct posix_acl *acl, *clone;
  237. int ret = 0;
  238. if (S_ISLNK(inode->i_mode))
  239. return -EOPNOTSUPP;
  240. if (!IS_POSIXACL(inode))
  241. return 0;
  242. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  243. if (IS_ERR(acl) || !acl)
  244. return PTR_ERR(acl);
  245. clone = posix_acl_clone(acl, GFP_KERNEL);
  246. posix_acl_release(acl);
  247. if (!clone)
  248. return -ENOMEM;
  249. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  250. if (!ret)
  251. ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
  252. posix_acl_release(clone);
  253. return ret;
  254. }
  255. struct xattr_handler btrfs_xattr_acl_default_handler = {
  256. .prefix = POSIX_ACL_XATTR_DEFAULT,
  257. .get = btrfs_xattr_acl_default_get,
  258. .set = btrfs_xattr_acl_default_set,
  259. };
  260. struct xattr_handler btrfs_xattr_acl_access_handler = {
  261. .prefix = POSIX_ACL_XATTR_ACCESS,
  262. .get = btrfs_xattr_acl_access_get,
  263. .set = btrfs_xattr_acl_access_set,
  264. };
  265. #else /* CONFIG_BTRFS_FS_POSIX_ACL */
  266. int btrfs_acl_chmod(struct inode *inode)
  267. {
  268. return 0;
  269. }
  270. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  271. struct inode *inode, struct inode *dir)
  272. {
  273. return 0;
  274. }
  275. #endif /* CONFIG_BTRFS_FS_POSIX_ACL */