acl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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. if (value)
  143. kfree(value);
  144. if (!ret)
  145. btrfs_update_cached_acl(inode, p_acl, acl);
  146. return ret;
  147. }
  148. static int btrfs_xattr_set_acl(struct inode *inode, int type,
  149. const void *value, size_t size)
  150. {
  151. int ret = 0;
  152. struct posix_acl *acl = NULL;
  153. if (value) {
  154. acl = posix_acl_from_xattr(value, size);
  155. if (acl == NULL) {
  156. value = NULL;
  157. size = 0;
  158. } else if (IS_ERR(acl)) {
  159. return PTR_ERR(acl);
  160. }
  161. }
  162. ret = btrfs_set_acl(inode, acl, type);
  163. posix_acl_release(acl);
  164. return ret;
  165. }
  166. static int btrfs_xattr_acl_access_get(struct inode *inode, const char *name,
  167. void *value, size_t size)
  168. {
  169. return btrfs_xattr_get_acl(inode, ACL_TYPE_ACCESS, value, size);
  170. }
  171. static int btrfs_xattr_acl_access_set(struct inode *inode, const char *name,
  172. const void *value, size_t size, int flags)
  173. {
  174. return btrfs_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  175. }
  176. static int btrfs_xattr_acl_default_get(struct inode *inode, const char *name,
  177. void *value, size_t size)
  178. {
  179. return btrfs_xattr_get_acl(inode, ACL_TYPE_DEFAULT, value, size);
  180. }
  181. static int btrfs_xattr_acl_default_set(struct inode *inode, const char *name,
  182. const void *value, size_t size, int flags)
  183. {
  184. return btrfs_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  185. }
  186. int btrfs_check_acl(struct inode *inode, int mask)
  187. {
  188. struct posix_acl *acl;
  189. int error = -EAGAIN;
  190. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  191. if (IS_ERR(acl))
  192. return PTR_ERR(acl);
  193. if (acl) {
  194. error = posix_acl_permission(inode, acl, mask);
  195. posix_acl_release(acl);
  196. }
  197. return error;
  198. }
  199. /*
  200. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  201. * stuff has been fixed to work with that. If the locking stuff changes, we
  202. * need to re-evaluate the acl locking stuff.
  203. */
  204. int btrfs_init_acl(struct inode *inode, struct inode *dir)
  205. {
  206. struct posix_acl *acl = NULL;
  207. int ret = 0;
  208. /* this happens with subvols */
  209. if (!dir)
  210. return 0;
  211. if (!S_ISLNK(inode->i_mode)) {
  212. if (IS_POSIXACL(dir)) {
  213. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  214. if (IS_ERR(acl))
  215. return PTR_ERR(acl);
  216. }
  217. if (!acl)
  218. inode->i_mode &= ~current->fs->umask;
  219. }
  220. if (IS_POSIXACL(dir) && acl) {
  221. struct posix_acl *clone;
  222. mode_t mode;
  223. if (S_ISDIR(inode->i_mode)) {
  224. ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
  225. if (ret)
  226. goto failed;
  227. }
  228. clone = posix_acl_clone(acl, GFP_NOFS);
  229. ret = -ENOMEM;
  230. if (!clone)
  231. goto failed;
  232. mode = inode->i_mode;
  233. ret = posix_acl_create_masq(clone, &mode);
  234. if (ret >= 0) {
  235. inode->i_mode = mode;
  236. if (ret > 0) {
  237. /* we need an acl */
  238. ret = btrfs_set_acl(inode, clone,
  239. ACL_TYPE_ACCESS);
  240. }
  241. }
  242. }
  243. failed:
  244. posix_acl_release(acl);
  245. return ret;
  246. }
  247. int btrfs_acl_chmod(struct inode *inode)
  248. {
  249. struct posix_acl *acl, *clone;
  250. int ret = 0;
  251. if (S_ISLNK(inode->i_mode))
  252. return -EOPNOTSUPP;
  253. if (!IS_POSIXACL(inode))
  254. return 0;
  255. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  256. if (IS_ERR(acl) || !acl)
  257. return PTR_ERR(acl);
  258. clone = posix_acl_clone(acl, GFP_KERNEL);
  259. posix_acl_release(acl);
  260. if (!clone)
  261. return -ENOMEM;
  262. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  263. if (!ret)
  264. ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
  265. posix_acl_release(clone);
  266. return ret;
  267. }
  268. struct xattr_handler btrfs_xattr_acl_default_handler = {
  269. .prefix = POSIX_ACL_XATTR_DEFAULT,
  270. .get = btrfs_xattr_acl_default_get,
  271. .set = btrfs_xattr_acl_default_set,
  272. };
  273. struct xattr_handler btrfs_xattr_acl_access_handler = {
  274. .prefix = POSIX_ACL_XATTR_ACCESS,
  275. .get = btrfs_xattr_acl_access_get,
  276. .set = btrfs_xattr_acl_access_set,
  277. };
  278. #else /* CONFIG_FS_POSIX_ACL */
  279. int btrfs_acl_chmod(struct inode *inode)
  280. {
  281. return 0;
  282. }
  283. int btrfs_init_acl(struct inode *inode, struct inode *dir)
  284. {
  285. return 0;
  286. }
  287. int btrfs_check_acl(struct inode *inode, int mask)
  288. {
  289. return 0;
  290. }
  291. #endif /* CONFIG_FS_POSIX_ACL */