acl.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/xattr.h>
  15. #include <linux/posix_acl.h>
  16. #include <linux/posix_acl_xattr.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "acl.h"
  21. #include "xattr.h"
  22. #include "glock.h"
  23. #include "inode.h"
  24. #include "meta_io.h"
  25. #include "trans.h"
  26. #include "util.h"
  27. static const char *gfs2_acl_name(int type)
  28. {
  29. switch (type) {
  30. case ACL_TYPE_ACCESS:
  31. return GFS2_POSIX_ACL_ACCESS;
  32. case ACL_TYPE_DEFAULT:
  33. return GFS2_POSIX_ACL_DEFAULT;
  34. }
  35. return NULL;
  36. }
  37. static struct posix_acl *gfs2_acl_get(struct gfs2_inode *ip, int type)
  38. {
  39. struct posix_acl *acl;
  40. const char *name;
  41. char *data;
  42. int len;
  43. if (!ip->i_eattr)
  44. return NULL;
  45. name = gfs2_acl_name(type);
  46. if (name == NULL)
  47. return ERR_PTR(-EINVAL);
  48. len = gfs2_xattr_acl_get(ip, name, &data);
  49. if (len < 0)
  50. return ERR_PTR(len);
  51. if (len == 0)
  52. return NULL;
  53. acl = posix_acl_from_xattr(data, len);
  54. kfree(data);
  55. return acl;
  56. }
  57. /**
  58. * gfs2_check_acl - Check an ACL to see if we're allowed to do something
  59. * @inode: the file we want to do something to
  60. * @mask: what we want to do
  61. *
  62. * Returns: errno
  63. */
  64. int gfs2_check_acl(struct inode *inode, int mask)
  65. {
  66. struct posix_acl *acl;
  67. int error;
  68. acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
  69. if (IS_ERR(acl))
  70. return PTR_ERR(acl);
  71. if (acl) {
  72. error = posix_acl_permission(inode, acl, mask);
  73. posix_acl_release(acl);
  74. return error;
  75. }
  76. return -EAGAIN;
  77. }
  78. static int gfs2_set_mode(struct inode *inode, mode_t mode)
  79. {
  80. int error = 0;
  81. if (mode != inode->i_mode) {
  82. struct iattr iattr;
  83. iattr.ia_valid = ATTR_MODE;
  84. iattr.ia_mode = mode;
  85. error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
  86. }
  87. return error;
  88. }
  89. static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
  90. {
  91. int error;
  92. int len;
  93. char *data;
  94. const char *name = gfs2_acl_name(type);
  95. BUG_ON(name == NULL);
  96. len = posix_acl_to_xattr(acl, NULL, 0);
  97. if (len == 0)
  98. return 0;
  99. data = kmalloc(len, GFP_NOFS);
  100. if (data == NULL)
  101. return -ENOMEM;
  102. error = posix_acl_to_xattr(acl, data, len);
  103. if (error < 0)
  104. goto out;
  105. error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, data, len, 0);
  106. out:
  107. kfree(data);
  108. return error;
  109. }
  110. int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
  111. {
  112. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  113. struct posix_acl *acl, *clone;
  114. mode_t mode = inode->i_mode;
  115. int error = 0;
  116. if (!sdp->sd_args.ar_posix_acl)
  117. return 0;
  118. if (S_ISLNK(inode->i_mode))
  119. return 0;
  120. acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
  121. if (IS_ERR(acl))
  122. return PTR_ERR(acl);
  123. if (!acl) {
  124. mode &= ~current_umask();
  125. if (mode != inode->i_mode)
  126. error = gfs2_set_mode(inode, mode);
  127. return error;
  128. }
  129. if (S_ISDIR(inode->i_mode)) {
  130. error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
  131. if (error)
  132. goto out;
  133. }
  134. clone = posix_acl_clone(acl, GFP_NOFS);
  135. error = -ENOMEM;
  136. if (!clone)
  137. goto out;
  138. posix_acl_release(acl);
  139. acl = clone;
  140. error = posix_acl_create_masq(acl, &mode);
  141. if (error < 0)
  142. goto out;
  143. if (error == 0)
  144. goto munge;
  145. error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
  146. if (error)
  147. goto out;
  148. munge:
  149. error = gfs2_set_mode(inode, mode);
  150. out:
  151. posix_acl_release(acl);
  152. return error;
  153. }
  154. int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
  155. {
  156. struct posix_acl *acl, *clone;
  157. char *data;
  158. unsigned int len;
  159. int error;
  160. acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
  161. if (IS_ERR(acl))
  162. return PTR_ERR(acl);
  163. if (!acl)
  164. return gfs2_setattr_simple(ip, attr);
  165. clone = posix_acl_clone(acl, GFP_NOFS);
  166. error = -ENOMEM;
  167. if (!clone)
  168. goto out;
  169. posix_acl_release(acl);
  170. acl = clone;
  171. error = posix_acl_chmod_masq(acl, attr->ia_mode);
  172. if (!error) {
  173. len = posix_acl_to_xattr(acl, NULL, 0);
  174. data = kmalloc(len, GFP_NOFS);
  175. error = -ENOMEM;
  176. if (data == NULL)
  177. goto out;
  178. posix_acl_to_xattr(acl, data, len);
  179. error = gfs2_xattr_acl_chmod(ip, attr, data);
  180. kfree(data);
  181. }
  182. out:
  183. posix_acl_release(acl);
  184. return error;
  185. }
  186. static int gfs2_acl_type(const char *name)
  187. {
  188. if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
  189. return ACL_TYPE_ACCESS;
  190. if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
  191. return ACL_TYPE_DEFAULT;
  192. return -EINVAL;
  193. }
  194. static int gfs2_xattr_system_get(struct inode *inode, const char *name,
  195. void *buffer, size_t size)
  196. {
  197. int type;
  198. type = gfs2_acl_type(name);
  199. if (type < 0)
  200. return type;
  201. return gfs2_xattr_get(inode, GFS2_EATYPE_SYS, name, buffer, size);
  202. }
  203. static int gfs2_xattr_system_set(struct inode *inode, const char *name,
  204. const void *value, size_t size, int flags)
  205. {
  206. struct gfs2_sbd *sdp = GFS2_SB(inode);
  207. struct posix_acl *acl = NULL;
  208. int error = 0, type;
  209. if (!sdp->sd_args.ar_posix_acl)
  210. return -EOPNOTSUPP;
  211. type = gfs2_acl_type(name);
  212. if (type < 0)
  213. return type;
  214. if (flags & XATTR_CREATE)
  215. return -EINVAL;
  216. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  217. return value ? -EACCES : 0;
  218. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  219. return -EPERM;
  220. if (S_ISLNK(inode->i_mode))
  221. return -EOPNOTSUPP;
  222. if (!value)
  223. goto set_acl;
  224. acl = posix_acl_from_xattr(value, size);
  225. if (!acl) {
  226. /*
  227. * acl_set_file(3) may request that we set default ACLs with
  228. * zero length -- defend (gracefully) against that here.
  229. */
  230. goto out;
  231. }
  232. if (IS_ERR(acl)) {
  233. error = PTR_ERR(acl);
  234. goto out;
  235. }
  236. error = posix_acl_valid(acl);
  237. if (error)
  238. goto out_release;
  239. error = -EINVAL;
  240. if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
  241. goto out_release;
  242. if (type == ACL_TYPE_ACCESS) {
  243. mode_t mode = inode->i_mode;
  244. error = posix_acl_equiv_mode(acl, &mode);
  245. if (error <= 0) {
  246. posix_acl_release(acl);
  247. acl = NULL;
  248. if (error < 0)
  249. return error;
  250. }
  251. error = gfs2_set_mode(inode, mode);
  252. if (error)
  253. goto out_release;
  254. }
  255. set_acl:
  256. error = gfs2_xattr_set(inode, GFS2_EATYPE_SYS, name, value, size, 0);
  257. out_release:
  258. posix_acl_release(acl);
  259. out:
  260. return error;
  261. }
  262. struct xattr_handler gfs2_xattr_system_handler = {
  263. .prefix = XATTR_SYSTEM_PREFIX,
  264. .get = gfs2_xattr_system_get,
  265. .set = gfs2_xattr_system_set,
  266. };