acl.c 7.1 KB

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