xfs_acl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2008, Christoph Hellwig
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_format.h"
  20. #include "xfs_log_format.h"
  21. #include "xfs_trans_resv.h"
  22. #include "xfs_mount.h"
  23. #include "xfs_inode.h"
  24. #include "xfs_acl.h"
  25. #include "xfs_attr.h"
  26. #include "xfs_trace.h"
  27. #include <linux/slab.h>
  28. #include <linux/xattr.h>
  29. #include <linux/posix_acl_xattr.h>
  30. /*
  31. * Locking scheme:
  32. * - all ACL updates are protected by inode->i_mutex, which is taken before
  33. * calling into this file.
  34. */
  35. STATIC struct posix_acl *
  36. xfs_acl_from_disk(
  37. struct xfs_acl *aclp,
  38. int max_entries)
  39. {
  40. struct posix_acl_entry *acl_e;
  41. struct posix_acl *acl;
  42. struct xfs_acl_entry *ace;
  43. unsigned int count, i;
  44. count = be32_to_cpu(aclp->acl_cnt);
  45. if (count > max_entries)
  46. return ERR_PTR(-EFSCORRUPTED);
  47. acl = posix_acl_alloc(count, GFP_KERNEL);
  48. if (!acl)
  49. return ERR_PTR(-ENOMEM);
  50. for (i = 0; i < count; i++) {
  51. acl_e = &acl->a_entries[i];
  52. ace = &aclp->acl_entry[i];
  53. /*
  54. * The tag is 32 bits on disk and 16 bits in core.
  55. *
  56. * Because every access to it goes through the core
  57. * format first this is not a problem.
  58. */
  59. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  60. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  61. switch (acl_e->e_tag) {
  62. case ACL_USER:
  63. acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
  64. break;
  65. case ACL_GROUP:
  66. acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
  67. break;
  68. case ACL_USER_OBJ:
  69. case ACL_GROUP_OBJ:
  70. case ACL_MASK:
  71. case ACL_OTHER:
  72. break;
  73. default:
  74. goto fail;
  75. }
  76. }
  77. return acl;
  78. fail:
  79. posix_acl_release(acl);
  80. return ERR_PTR(-EINVAL);
  81. }
  82. STATIC void
  83. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  84. {
  85. const struct posix_acl_entry *acl_e;
  86. struct xfs_acl_entry *ace;
  87. int i;
  88. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  89. for (i = 0; i < acl->a_count; i++) {
  90. ace = &aclp->acl_entry[i];
  91. acl_e = &acl->a_entries[i];
  92. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  93. switch (acl_e->e_tag) {
  94. case ACL_USER:
  95. ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
  96. break;
  97. case ACL_GROUP:
  98. ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
  99. break;
  100. default:
  101. ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
  102. break;
  103. }
  104. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  105. }
  106. }
  107. struct posix_acl *
  108. xfs_get_acl(struct inode *inode, int type)
  109. {
  110. struct xfs_inode *ip = XFS_I(inode);
  111. struct posix_acl *acl = NULL;
  112. struct xfs_acl *xfs_acl;
  113. unsigned char *ea_name;
  114. int error;
  115. int len;
  116. trace_xfs_get_acl(ip);
  117. switch (type) {
  118. case ACL_TYPE_ACCESS:
  119. ea_name = SGI_ACL_FILE;
  120. break;
  121. case ACL_TYPE_DEFAULT:
  122. ea_name = SGI_ACL_DEFAULT;
  123. break;
  124. default:
  125. BUG();
  126. }
  127. /*
  128. * If we have a cached ACLs value just return it, not need to
  129. * go out to the disk.
  130. */
  131. len = XFS_ACL_MAX_SIZE(ip->i_mount);
  132. xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
  133. if (!xfs_acl)
  134. return ERR_PTR(-ENOMEM);
  135. error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
  136. &len, ATTR_ROOT);
  137. if (error) {
  138. /*
  139. * If the attribute doesn't exist make sure we have a negative
  140. * cache entry, for any other error assume it is transient and
  141. * leave the cache entry as ACL_NOT_CACHED.
  142. */
  143. if (error == -ENOATTR)
  144. goto out_update_cache;
  145. goto out;
  146. }
  147. acl = xfs_acl_from_disk(xfs_acl, XFS_ACL_MAX_ENTRIES(ip->i_mount));
  148. if (IS_ERR(acl))
  149. goto out;
  150. out_update_cache:
  151. set_cached_acl(inode, type, acl);
  152. out:
  153. kmem_free(xfs_acl);
  154. return acl;
  155. }
  156. STATIC int
  157. __xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  158. {
  159. struct xfs_inode *ip = XFS_I(inode);
  160. unsigned char *ea_name;
  161. int error;
  162. switch (type) {
  163. case ACL_TYPE_ACCESS:
  164. ea_name = SGI_ACL_FILE;
  165. break;
  166. case ACL_TYPE_DEFAULT:
  167. if (!S_ISDIR(inode->i_mode))
  168. return acl ? -EACCES : 0;
  169. ea_name = SGI_ACL_DEFAULT;
  170. break;
  171. default:
  172. return -EINVAL;
  173. }
  174. if (acl) {
  175. struct xfs_acl *xfs_acl;
  176. int len = XFS_ACL_MAX_SIZE(ip->i_mount);
  177. xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
  178. if (!xfs_acl)
  179. return -ENOMEM;
  180. xfs_acl_to_disk(xfs_acl, acl);
  181. /* subtract away the unused acl entries */
  182. len -= sizeof(struct xfs_acl_entry) *
  183. (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
  184. error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
  185. len, ATTR_ROOT);
  186. kmem_free(xfs_acl);
  187. } else {
  188. /*
  189. * A NULL ACL argument means we want to remove the ACL.
  190. */
  191. error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  192. /*
  193. * If the attribute didn't exist to start with that's fine.
  194. */
  195. if (error == -ENOATTR)
  196. error = 0;
  197. }
  198. if (!error)
  199. set_cached_acl(inode, type, acl);
  200. return error;
  201. }
  202. static int
  203. xfs_set_mode(struct inode *inode, umode_t mode)
  204. {
  205. int error = 0;
  206. if (mode != inode->i_mode) {
  207. struct iattr iattr;
  208. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  209. iattr.ia_mode = mode;
  210. iattr.ia_ctime = current_fs_time(inode->i_sb);
  211. error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  212. }
  213. return error;
  214. }
  215. static int
  216. xfs_acl_exists(struct inode *inode, unsigned char *name)
  217. {
  218. int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
  219. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  220. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  221. }
  222. int
  223. posix_acl_access_exists(struct inode *inode)
  224. {
  225. return xfs_acl_exists(inode, SGI_ACL_FILE);
  226. }
  227. int
  228. posix_acl_default_exists(struct inode *inode)
  229. {
  230. if (!S_ISDIR(inode->i_mode))
  231. return 0;
  232. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  233. }
  234. int
  235. xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  236. {
  237. int error = 0;
  238. if (!acl)
  239. goto set_acl;
  240. error = -E2BIG;
  241. if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
  242. return error;
  243. if (type == ACL_TYPE_ACCESS) {
  244. umode_t mode = inode->i_mode;
  245. error = posix_acl_equiv_mode(acl, &mode);
  246. if (error <= 0) {
  247. acl = NULL;
  248. if (error < 0)
  249. return error;
  250. }
  251. error = xfs_set_mode(inode, mode);
  252. if (error)
  253. return error;
  254. }
  255. set_acl:
  256. return __xfs_set_acl(inode, type, acl);
  257. }