acl.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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. name = POSIX_ACL_XATTR_ACCESS;
  101. if (acl) {
  102. ret = posix_acl_equiv_mode(acl, &mode);
  103. if (ret < 0)
  104. return ret;
  105. inode->i_mode = mode;
  106. }
  107. ret = 0;
  108. break;
  109. case ACL_TYPE_DEFAULT:
  110. if (!S_ISDIR(inode->i_mode))
  111. return acl ? -EINVAL : 0;
  112. name = POSIX_ACL_XATTR_DEFAULT;
  113. break;
  114. default:
  115. return -EINVAL;
  116. }
  117. if (acl) {
  118. size = posix_acl_xattr_size(acl->a_count);
  119. value = kmalloc(size, GFP_NOFS);
  120. if (!value) {
  121. ret = -ENOMEM;
  122. goto out;
  123. }
  124. ret = posix_acl_to_xattr(acl, value, size);
  125. if (ret < 0)
  126. goto out;
  127. }
  128. ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
  129. out:
  130. kfree(value);
  131. if (!ret)
  132. set_cached_acl(inode, type, acl);
  133. return ret;
  134. }
  135. static int btrfs_xattr_set_acl(struct inode *inode, int type,
  136. const void *value, size_t size)
  137. {
  138. int ret;
  139. struct posix_acl *acl = NULL;
  140. if (value) {
  141. acl = posix_acl_from_xattr(value, size);
  142. if (acl == NULL) {
  143. value = NULL;
  144. size = 0;
  145. } else if (IS_ERR(acl)) {
  146. return PTR_ERR(acl);
  147. }
  148. }
  149. ret = btrfs_set_acl(NULL, inode, acl, type);
  150. posix_acl_release(acl);
  151. return ret;
  152. }
  153. static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
  154. void *value, size_t size)
  155. {
  156. return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
  157. }
  158. static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
  159. const void *value, size_t size, int flags)
  160. {
  161. return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  162. }
  163. static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
  164. void *value, size_t size)
  165. {
  166. return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
  167. }
  168. static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
  169. const void *value, size_t size, int flags)
  170. {
  171. return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  172. }
  173. int btrfs_check_acl(struct inode *inode, int mask)
  174. {
  175. struct posix_acl *acl;
  176. int error = -EAGAIN;
  177. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  178. if (IS_ERR(acl))
  179. return PTR_ERR(acl);
  180. if (acl) {
  181. error = posix_acl_permission(inode, acl, mask);
  182. posix_acl_release(acl);
  183. }
  184. return error;
  185. }
  186. /*
  187. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  188. * stuff has been fixed to work with that. If the locking stuff changes, we
  189. * need to re-evaluate the acl locking stuff.
  190. */
  191. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  192. struct inode *inode, struct inode *dir)
  193. {
  194. struct posix_acl *acl = NULL;
  195. int ret = 0;
  196. /* this happens with subvols */
  197. if (!dir)
  198. return 0;
  199. if (!S_ISLNK(inode->i_mode)) {
  200. if (IS_POSIXACL(dir)) {
  201. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  202. if (IS_ERR(acl))
  203. return PTR_ERR(acl);
  204. }
  205. if (!acl)
  206. inode->i_mode &= ~current_umask();
  207. }
  208. if (IS_POSIXACL(dir) && acl) {
  209. struct posix_acl *clone;
  210. mode_t mode;
  211. if (S_ISDIR(inode->i_mode)) {
  212. ret = btrfs_set_acl(trans, inode, acl,
  213. ACL_TYPE_DEFAULT);
  214. if (ret)
  215. goto failed;
  216. }
  217. clone = posix_acl_clone(acl, GFP_NOFS);
  218. ret = -ENOMEM;
  219. if (!clone)
  220. goto failed;
  221. mode = inode->i_mode;
  222. ret = posix_acl_create_masq(clone, &mode);
  223. if (ret >= 0) {
  224. inode->i_mode = mode;
  225. if (ret > 0) {
  226. /* we need an acl */
  227. ret = btrfs_set_acl(trans, inode, clone,
  228. ACL_TYPE_ACCESS);
  229. }
  230. }
  231. }
  232. failed:
  233. posix_acl_release(acl);
  234. return ret;
  235. }
  236. int btrfs_acl_chmod(struct inode *inode)
  237. {
  238. struct posix_acl *acl, *clone;
  239. int ret = 0;
  240. if (S_ISLNK(inode->i_mode))
  241. return -EOPNOTSUPP;
  242. if (!IS_POSIXACL(inode))
  243. return 0;
  244. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  245. if (IS_ERR(acl) || !acl)
  246. return PTR_ERR(acl);
  247. clone = posix_acl_clone(acl, GFP_KERNEL);
  248. posix_acl_release(acl);
  249. if (!clone)
  250. return -ENOMEM;
  251. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  252. if (!ret)
  253. ret = btrfs_set_acl(NULL, inode, clone, ACL_TYPE_ACCESS);
  254. posix_acl_release(clone);
  255. return ret;
  256. }
  257. struct xattr_handler btrfs_xattr_acl_default_handler = {
  258. .prefix = POSIX_ACL_XATTR_DEFAULT,
  259. .get = btrfs_xattr_acl_default_get,
  260. .set = btrfs_xattr_acl_default_set,
  261. };
  262. struct xattr_handler btrfs_xattr_acl_access_handler = {
  263. .prefix = POSIX_ACL_XATTR_ACCESS,
  264. .get = btrfs_xattr_acl_access_get,
  265. .set = btrfs_xattr_acl_access_set,
  266. };
  267. #else /* CONFIG_BTRFS_FS_POSIX_ACL */
  268. int btrfs_acl_chmod(struct inode *inode)
  269. {
  270. return 0;
  271. }
  272. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  273. struct inode *inode, struct inode *dir)
  274. {
  275. return 0;
  276. }
  277. #endif /* CONFIG_BTRFS_FS_POSIX_ACL */