acl.c 6.5 KB

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