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