acl.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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/posix_acl.h>
  15. #include <linux/posix_acl_xattr.h>
  16. #include <linux/gfs2_ondisk.h>
  17. #include "gfs2.h"
  18. #include "incore.h"
  19. #include "acl.h"
  20. #include "xattr.h"
  21. #include "glock.h"
  22. #include "inode.h"
  23. #include "meta_io.h"
  24. #include "trans.h"
  25. #include "util.h"
  26. #define ACL_ACCESS 1
  27. #define ACL_DEFAULT 0
  28. int gfs2_acl_validate_set(struct gfs2_inode *ip, int access,
  29. struct gfs2_ea_request *er, int *remove, mode_t *mode)
  30. {
  31. struct posix_acl *acl;
  32. int error;
  33. error = gfs2_acl_validate_remove(ip, access);
  34. if (error)
  35. return error;
  36. if (!er->er_data)
  37. return -EINVAL;
  38. acl = posix_acl_from_xattr(er->er_data, er->er_data_len);
  39. if (IS_ERR(acl))
  40. return PTR_ERR(acl);
  41. if (!acl) {
  42. *remove = 1;
  43. return 0;
  44. }
  45. error = posix_acl_valid(acl);
  46. if (error)
  47. goto out;
  48. if (access) {
  49. error = posix_acl_equiv_mode(acl, mode);
  50. if (!error)
  51. *remove = 1;
  52. else if (error > 0)
  53. error = 0;
  54. }
  55. out:
  56. posix_acl_release(acl);
  57. return error;
  58. }
  59. int gfs2_acl_validate_remove(struct gfs2_inode *ip, int access)
  60. {
  61. if (!GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl)
  62. return -EOPNOTSUPP;
  63. if (!is_owner_or_cap(&ip->i_inode))
  64. return -EPERM;
  65. if (S_ISLNK(ip->i_inode.i_mode))
  66. return -EOPNOTSUPP;
  67. if (!access && !S_ISDIR(ip->i_inode.i_mode))
  68. return -EACCES;
  69. return 0;
  70. }
  71. static int acl_get(struct gfs2_inode *ip, const char *name,
  72. struct posix_acl **acl, struct gfs2_ea_location *el,
  73. char **datap, unsigned int *lenp)
  74. {
  75. char *data;
  76. unsigned int len;
  77. int error;
  78. el->el_bh = NULL;
  79. if (!ip->i_eattr)
  80. return 0;
  81. error = gfs2_ea_find(ip, GFS2_EATYPE_SYS, name, el);
  82. if (error)
  83. return error;
  84. if (!el->el_ea)
  85. return 0;
  86. if (!GFS2_EA_DATA_LEN(el->el_ea))
  87. goto out;
  88. len = GFS2_EA_DATA_LEN(el->el_ea);
  89. data = kmalloc(len, GFP_NOFS);
  90. error = -ENOMEM;
  91. if (!data)
  92. goto out;
  93. error = gfs2_ea_get_copy(ip, el, data, len);
  94. if (error < 0)
  95. goto out_kfree;
  96. error = 0;
  97. if (acl) {
  98. *acl = posix_acl_from_xattr(data, len);
  99. if (IS_ERR(*acl))
  100. error = PTR_ERR(*acl);
  101. }
  102. out_kfree:
  103. if (error || !datap) {
  104. kfree(data);
  105. } else {
  106. *datap = data;
  107. *lenp = len;
  108. }
  109. out:
  110. return error;
  111. }
  112. /**
  113. * gfs2_check_acl - Check an ACL to see if we're allowed to do something
  114. * @inode: the file we want to do something to
  115. * @mask: what we want to do
  116. *
  117. * Returns: errno
  118. */
  119. int gfs2_check_acl(struct inode *inode, int mask)
  120. {
  121. struct gfs2_ea_location el;
  122. struct posix_acl *acl = NULL;
  123. int error;
  124. error = acl_get(GFS2_I(inode), GFS2_POSIX_ACL_ACCESS, &acl, &el, NULL, NULL);
  125. brelse(el.el_bh);
  126. if (error)
  127. return error;
  128. if (acl) {
  129. error = posix_acl_permission(inode, acl, mask);
  130. posix_acl_release(acl);
  131. return error;
  132. }
  133. return -EAGAIN;
  134. }
  135. static int munge_mode(struct gfs2_inode *ip, mode_t mode)
  136. {
  137. struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode);
  138. struct buffer_head *dibh;
  139. int error;
  140. error = gfs2_trans_begin(sdp, RES_DINODE, 0);
  141. if (error)
  142. return error;
  143. error = gfs2_meta_inode_buffer(ip, &dibh);
  144. if (!error) {
  145. gfs2_assert_withdraw(sdp,
  146. (ip->i_inode.i_mode & S_IFMT) == (mode & S_IFMT));
  147. ip->i_inode.i_mode = mode;
  148. gfs2_trans_add_bh(ip->i_gl, dibh, 1);
  149. gfs2_dinode_out(ip, dibh->b_data);
  150. brelse(dibh);
  151. }
  152. gfs2_trans_end(sdp);
  153. return 0;
  154. }
  155. int gfs2_acl_create(struct gfs2_inode *dip, struct gfs2_inode *ip)
  156. {
  157. struct gfs2_ea_location el;
  158. struct gfs2_sbd *sdp = GFS2_SB(&dip->i_inode);
  159. struct posix_acl *acl = NULL, *clone;
  160. mode_t mode = ip->i_inode.i_mode;
  161. char *data = NULL;
  162. unsigned int len;
  163. int error;
  164. if (!sdp->sd_args.ar_posix_acl)
  165. return 0;
  166. if (S_ISLNK(ip->i_inode.i_mode))
  167. return 0;
  168. error = acl_get(dip, GFS2_POSIX_ACL_DEFAULT, &acl, &el, &data, &len);
  169. brelse(el.el_bh);
  170. if (error)
  171. return error;
  172. if (!acl) {
  173. mode &= ~current_umask();
  174. if (mode != ip->i_inode.i_mode)
  175. error = munge_mode(ip, mode);
  176. return error;
  177. }
  178. clone = posix_acl_clone(acl, GFP_NOFS);
  179. error = -ENOMEM;
  180. if (!clone)
  181. goto out;
  182. posix_acl_release(acl);
  183. acl = clone;
  184. if (S_ISDIR(ip->i_inode.i_mode)) {
  185. error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
  186. GFS2_POSIX_ACL_DEFAULT, data, len, 0);
  187. if (error)
  188. goto out;
  189. }
  190. error = posix_acl_create_masq(acl, &mode);
  191. if (error < 0)
  192. goto out;
  193. if (error == 0)
  194. goto munge;
  195. posix_acl_to_xattr(acl, data, len);
  196. error = gfs2_xattr_set(&ip->i_inode, GFS2_EATYPE_SYS,
  197. GFS2_POSIX_ACL_ACCESS, data, len, 0);
  198. if (error)
  199. goto out;
  200. munge:
  201. error = munge_mode(ip, mode);
  202. out:
  203. posix_acl_release(acl);
  204. kfree(data);
  205. return error;
  206. }
  207. int gfs2_acl_chmod(struct gfs2_inode *ip, struct iattr *attr)
  208. {
  209. struct posix_acl *acl = NULL, *clone;
  210. struct gfs2_ea_location el;
  211. char *data;
  212. unsigned int len;
  213. int error;
  214. error = acl_get(ip, GFS2_POSIX_ACL_ACCESS, &acl, &el, &data, &len);
  215. if (error)
  216. goto out_brelse;
  217. if (!acl)
  218. return gfs2_setattr_simple(ip, attr);
  219. clone = posix_acl_clone(acl, GFP_NOFS);
  220. error = -ENOMEM;
  221. if (!clone)
  222. goto out;
  223. posix_acl_release(acl);
  224. acl = clone;
  225. error = posix_acl_chmod_masq(acl, attr->ia_mode);
  226. if (!error) {
  227. posix_acl_to_xattr(acl, data, len);
  228. error = gfs2_ea_acl_chmod(ip, &el, attr, data);
  229. }
  230. out:
  231. posix_acl_release(acl);
  232. kfree(data);
  233. out_brelse:
  234. brelse(el.el_bh);
  235. return error;
  236. }