acl.c 7.4 KB

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