acl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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_BTRFS_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_acl_get(struct dentry *dentry, const char *name,
  68. void *value, size_t size, int type)
  69. {
  70. struct posix_acl *acl;
  71. int ret = 0;
  72. acl = btrfs_get_acl(dentry->d_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_acl_set(struct dentry *dentry, const char *name,
  133. const void *value, size_t size, int flags, int type)
  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(dentry->d_inode, acl, type);
  147. posix_acl_release(acl);
  148. return ret;
  149. }
  150. int btrfs_check_acl(struct inode *inode, int mask)
  151. {
  152. struct posix_acl *acl;
  153. int error = -EAGAIN;
  154. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  155. if (IS_ERR(acl))
  156. return PTR_ERR(acl);
  157. if (acl) {
  158. error = posix_acl_permission(inode, acl, mask);
  159. posix_acl_release(acl);
  160. }
  161. return error;
  162. }
  163. /*
  164. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  165. * stuff has been fixed to work with that. If the locking stuff changes, we
  166. * need to re-evaluate the acl locking stuff.
  167. */
  168. int btrfs_init_acl(struct inode *inode, struct inode *dir)
  169. {
  170. struct posix_acl *acl = NULL;
  171. int ret = 0;
  172. /* this happens with subvols */
  173. if (!dir)
  174. return 0;
  175. if (!S_ISLNK(inode->i_mode)) {
  176. if (IS_POSIXACL(dir)) {
  177. acl = btrfs_get_acl(dir, ACL_TYPE_DEFAULT);
  178. if (IS_ERR(acl))
  179. return PTR_ERR(acl);
  180. }
  181. if (!acl)
  182. inode->i_mode &= ~current_umask();
  183. }
  184. if (IS_POSIXACL(dir) && acl) {
  185. struct posix_acl *clone;
  186. mode_t mode;
  187. if (S_ISDIR(inode->i_mode)) {
  188. ret = btrfs_set_acl(inode, acl, ACL_TYPE_DEFAULT);
  189. if (ret)
  190. goto failed;
  191. }
  192. clone = posix_acl_clone(acl, GFP_NOFS);
  193. ret = -ENOMEM;
  194. if (!clone)
  195. goto failed;
  196. mode = inode->i_mode;
  197. ret = posix_acl_create_masq(clone, &mode);
  198. if (ret >= 0) {
  199. inode->i_mode = mode;
  200. if (ret > 0) {
  201. /* we need an acl */
  202. ret = btrfs_set_acl(inode, clone,
  203. ACL_TYPE_ACCESS);
  204. }
  205. }
  206. }
  207. failed:
  208. posix_acl_release(acl);
  209. return ret;
  210. }
  211. int btrfs_acl_chmod(struct inode *inode)
  212. {
  213. struct posix_acl *acl, *clone;
  214. int ret = 0;
  215. if (S_ISLNK(inode->i_mode))
  216. return -EOPNOTSUPP;
  217. if (!IS_POSIXACL(inode))
  218. return 0;
  219. acl = btrfs_get_acl(inode, ACL_TYPE_ACCESS);
  220. if (IS_ERR(acl) || !acl)
  221. return PTR_ERR(acl);
  222. clone = posix_acl_clone(acl, GFP_KERNEL);
  223. posix_acl_release(acl);
  224. if (!clone)
  225. return -ENOMEM;
  226. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  227. if (!ret)
  228. ret = btrfs_set_acl(inode, clone, ACL_TYPE_ACCESS);
  229. posix_acl_release(clone);
  230. return ret;
  231. }
  232. struct xattr_handler btrfs_xattr_acl_default_handler = {
  233. .prefix = POSIX_ACL_XATTR_DEFAULT,
  234. .flags = ACL_TYPE_DEFAULT,
  235. .get = btrfs_xattr_acl_get,
  236. .set = btrfs_xattr_acl_set,
  237. };
  238. struct xattr_handler btrfs_xattr_acl_access_handler = {
  239. .prefix = POSIX_ACL_XATTR_ACCESS,
  240. .flags = ACL_TYPE_ACCESS,
  241. .get = btrfs_xattr_acl_get,
  242. .set = btrfs_xattr_acl_set,
  243. };
  244. #else /* CONFIG_BTRFS_FS_POSIX_ACL */
  245. int btrfs_acl_chmod(struct inode *inode)
  246. {
  247. return 0;
  248. }
  249. int btrfs_init_acl(struct inode *inode, struct inode *dir)
  250. {
  251. return 0;
  252. }
  253. #endif /* CONFIG_BTRFS_FS_POSIX_ACL */