acl.c 6.0 KB

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