acl.c 6.8 KB

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