acl.c 6.6 KB

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