acl.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. acl = get_cached_acl(&ip->i_inode, type);
  46. if (acl != ACL_NOT_CACHED)
  47. return acl;
  48. name = gfs2_acl_name(type);
  49. if (name == NULL)
  50. return ERR_PTR(-EINVAL);
  51. len = gfs2_xattr_acl_get(ip, name, &data);
  52. if (len < 0)
  53. return ERR_PTR(len);
  54. if (len == 0)
  55. return NULL;
  56. acl = posix_acl_from_xattr(data, len);
  57. kfree(data);
  58. return acl;
  59. }
  60. /**
  61. * gfs2_check_acl - Check an ACL to see if we're allowed to do something
  62. * @inode: the file we want to do something to
  63. * @mask: what we want to do
  64. *
  65. * Returns: errno
  66. */
  67. int gfs2_check_acl(struct inode *inode, int mask, unsigned int flags)
  68. {
  69. struct posix_acl *acl;
  70. int error;
  71. if (flags & IPERM_FLAG_RCU) {
  72. if (!negative_cached_acl(inode, ACL_TYPE_ACCESS))
  73. return -ECHILD;
  74. return -EAGAIN;
  75. }
  76. acl = gfs2_acl_get(GFS2_I(inode), ACL_TYPE_ACCESS);
  77. if (IS_ERR(acl))
  78. return PTR_ERR(acl);
  79. if (acl) {
  80. error = posix_acl_permission(inode, acl, mask);
  81. posix_acl_release(acl);
  82. return error;
  83. }
  84. return -EAGAIN;
  85. }
  86. static int gfs2_set_mode(struct inode *inode, mode_t mode)
  87. {
  88. int error = 0;
  89. if (mode != inode->i_mode) {
  90. struct iattr iattr;
  91. iattr.ia_valid = ATTR_MODE;
  92. iattr.ia_mode = mode;
  93. error = gfs2_setattr_simple(GFS2_I(inode), &iattr);
  94. }
  95. return error;
  96. }
  97. static int gfs2_acl_set(struct inode *inode, int type, struct posix_acl *acl)
  98. {
  99. int error;
  100. int len;
  101. char *data;
  102. const char *name = gfs2_acl_name(type);
  103. BUG_ON(name == NULL);
  104. len = posix_acl_to_xattr(acl, NULL, 0);
  105. if (len == 0)
  106. return 0;
  107. data = kmalloc(len, GFP_NOFS);
  108. if (data == NULL)
  109. return -ENOMEM;
  110. error = posix_acl_to_xattr(acl, data, len);
  111. if (error < 0)
  112. goto out;
  113. error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
  114. if (!error)
  115. set_cached_acl(inode, type, acl);
  116. out:
  117. kfree(data);
  118. return error;
  119. }
  120. int gfs2_acl_create(struct gfs2_inode *dip, struct inode *inode)
  121. {
  122. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  123. struct posix_acl *acl, *clone;
  124. mode_t mode = inode->i_mode;
  125. int error = 0;
  126. if (!sdp->sd_args.ar_posix_acl)
  127. return 0;
  128. if (S_ISLNK(inode->i_mode))
  129. return 0;
  130. acl = gfs2_acl_get(dip, ACL_TYPE_DEFAULT);
  131. if (IS_ERR(acl))
  132. return PTR_ERR(acl);
  133. if (!acl) {
  134. mode &= ~current_umask();
  135. if (mode != inode->i_mode)
  136. error = gfs2_set_mode(inode, mode);
  137. return error;
  138. }
  139. if (S_ISDIR(inode->i_mode)) {
  140. error = gfs2_acl_set(inode, ACL_TYPE_DEFAULT, acl);
  141. if (error)
  142. goto out;
  143. }
  144. clone = posix_acl_clone(acl, GFP_NOFS);
  145. error = -ENOMEM;
  146. if (!clone)
  147. goto out;
  148. posix_acl_release(acl);
  149. acl = clone;
  150. error = posix_acl_create_masq(acl, &mode);
  151. if (error < 0)
  152. goto out;
  153. if (error == 0)
  154. goto munge;
  155. error = gfs2_acl_set(inode, ACL_TYPE_ACCESS, acl);
  156. if (error)
  157. goto out;
  158. munge:
  159. error = gfs2_set_mode(inode, mode);
  160. out:
  161. posix_acl_release(acl);
  162. return error;
  163. }
  164. int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
  165. {
  166. struct posix_acl *acl, *clone;
  167. char *data;
  168. unsigned int len;
  169. int error;
  170. acl = gfs2_acl_get(ip, ACL_TYPE_ACCESS);
  171. if (IS_ERR(acl))
  172. return PTR_ERR(acl);
  173. if (!acl)
  174. return gfs2_setattr_simple(ip, attr);
  175. clone = posix_acl_clone(acl, GFP_NOFS);
  176. error = -ENOMEM;
  177. if (!clone)
  178. goto out;
  179. posix_acl_release(acl);
  180. acl = clone;
  181. error = posix_acl_chmod_masq(acl, attr->ia_mode);
  182. if (!error) {
  183. len = posix_acl_to_xattr(acl, NULL, 0);
  184. data = kmalloc(len, GFP_NOFS);
  185. error = -ENOMEM;
  186. if (data == NULL)
  187. goto out;
  188. posix_acl_to_xattr(acl, data, len);
  189. error = gfs2_xattr_acl_chmod(ip, attr, data);
  190. kfree(data);
  191. set_cached_acl(&ip->i_inode, ACL_TYPE_ACCESS, acl);
  192. }
  193. out:
  194. posix_acl_release(acl);
  195. return error;
  196. }
  197. static int gfs2_acl_type(const char *name)
  198. {
  199. if (strcmp(name, GFS2_POSIX_ACL_ACCESS) == 0)
  200. return ACL_TYPE_ACCESS;
  201. if (strcmp(name, GFS2_POSIX_ACL_DEFAULT) == 0)
  202. return ACL_TYPE_DEFAULT;
  203. return -EINVAL;
  204. }
  205. static int gfs2_xattr_system_get(struct dentry *dentry, const char *name,
  206. void *buffer, size_t size, int xtype)
  207. {
  208. struct inode *inode = dentry->d_inode;
  209. struct gfs2_sbd *sdp = GFS2_SB(inode);
  210. struct posix_acl *acl;
  211. int type;
  212. int error;
  213. if (!sdp->sd_args.ar_posix_acl)
  214. return -EOPNOTSUPP;
  215. type = gfs2_acl_type(name);
  216. if (type < 0)
  217. return type;
  218. acl = gfs2_acl_get(GFS2_I(inode), type);
  219. if (IS_ERR(acl))
  220. return PTR_ERR(acl);
  221. if (acl == NULL)
  222. return -ENODATA;
  223. error = posix_acl_to_xattr(acl, buffer, size);
  224. posix_acl_release(acl);
  225. return error;
  226. }
  227. static int gfs2_xattr_system_set(struct dentry *dentry, const char *name,
  228. const void *value, size_t size, int flags,
  229. int xtype)
  230. {
  231. struct inode *inode = dentry->d_inode;
  232. struct gfs2_sbd *sdp = GFS2_SB(inode);
  233. struct posix_acl *acl = NULL;
  234. int error = 0, type;
  235. if (!sdp->sd_args.ar_posix_acl)
  236. return -EOPNOTSUPP;
  237. type = gfs2_acl_type(name);
  238. if (type < 0)
  239. return type;
  240. if (flags & XATTR_CREATE)
  241. return -EINVAL;
  242. if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
  243. return value ? -EACCES : 0;
  244. if ((current_fsuid() != inode->i_uid) && !capable(CAP_FOWNER))
  245. return -EPERM;
  246. if (S_ISLNK(inode->i_mode))
  247. return -EOPNOTSUPP;
  248. if (!value)
  249. goto set_acl;
  250. acl = posix_acl_from_xattr(value, size);
  251. if (!acl) {
  252. /*
  253. * acl_set_file(3) may request that we set default ACLs with
  254. * zero length -- defend (gracefully) against that here.
  255. */
  256. goto out;
  257. }
  258. if (IS_ERR(acl)) {
  259. error = PTR_ERR(acl);
  260. goto out;
  261. }
  262. error = posix_acl_valid(acl);
  263. if (error)
  264. goto out_release;
  265. error = -EINVAL;
  266. if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
  267. goto out_release;
  268. if (type == ACL_TYPE_ACCESS) {
  269. mode_t mode = inode->i_mode;
  270. error = posix_acl_equiv_mode(acl, &mode);
  271. if (error <= 0) {
  272. posix_acl_release(acl);
  273. acl = NULL;
  274. if (error < 0)
  275. return error;
  276. }
  277. error = gfs2_set_mode(inode, mode);
  278. if (error)
  279. goto out_release;
  280. }
  281. set_acl:
  282. error = __gfs2_xattr_set(inode, name, value, size, 0, GFS2_EATYPE_SYS);
  283. if (!error) {
  284. if (acl)
  285. set_cached_acl(inode, type, acl);
  286. else
  287. forget_cached_acl(inode, type);
  288. }
  289. out_release:
  290. posix_acl_release(acl);
  291. out:
  292. return error;
  293. }
  294. const struct xattr_handler gfs2_xattr_system_handler = {
  295. .prefix = XATTR_SYSTEM_PREFIX,
  296. .flags = GFS2_EATYPE_SYS,
  297. .get = gfs2_xattr_system_get,
  298. .set = gfs2_xattr_system_set,
  299. };