acl.c 6.7 KB

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