acl.c 7.3 KB

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