acl.c 6.5 KB

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