acl.c 7.6 KB

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