acl.c 6.1 KB

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