xfs_acl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. const struct xfs_acl *aclp,
  38. int len,
  39. int max_entries)
  40. {
  41. struct posix_acl_entry *acl_e;
  42. struct posix_acl *acl;
  43. const struct xfs_acl_entry *ace;
  44. unsigned int count, i;
  45. if (len < sizeof(*aclp))
  46. return ERR_PTR(-EFSCORRUPTED);
  47. count = be32_to_cpu(aclp->acl_cnt);
  48. if (count > max_entries || XFS_ACL_SIZE(count) != len)
  49. return ERR_PTR(-EFSCORRUPTED);
  50. acl = posix_acl_alloc(count, GFP_KERNEL);
  51. if (!acl)
  52. return ERR_PTR(-ENOMEM);
  53. for (i = 0; i < count; i++) {
  54. acl_e = &acl->a_entries[i];
  55. ace = &aclp->acl_entry[i];
  56. /*
  57. * The tag is 32 bits on disk and 16 bits in core.
  58. *
  59. * Because every access to it goes through the core
  60. * format first this is not a problem.
  61. */
  62. acl_e->e_tag = be32_to_cpu(ace->ae_tag);
  63. acl_e->e_perm = be16_to_cpu(ace->ae_perm);
  64. switch (acl_e->e_tag) {
  65. case ACL_USER:
  66. acl_e->e_uid = xfs_uid_to_kuid(be32_to_cpu(ace->ae_id));
  67. break;
  68. case ACL_GROUP:
  69. acl_e->e_gid = xfs_gid_to_kgid(be32_to_cpu(ace->ae_id));
  70. break;
  71. case ACL_USER_OBJ:
  72. case ACL_GROUP_OBJ:
  73. case ACL_MASK:
  74. case ACL_OTHER:
  75. break;
  76. default:
  77. goto fail;
  78. }
  79. }
  80. return acl;
  81. fail:
  82. posix_acl_release(acl);
  83. return ERR_PTR(-EINVAL);
  84. }
  85. STATIC void
  86. xfs_acl_to_disk(struct xfs_acl *aclp, const struct posix_acl *acl)
  87. {
  88. const struct posix_acl_entry *acl_e;
  89. struct xfs_acl_entry *ace;
  90. int i;
  91. aclp->acl_cnt = cpu_to_be32(acl->a_count);
  92. for (i = 0; i < acl->a_count; i++) {
  93. ace = &aclp->acl_entry[i];
  94. acl_e = &acl->a_entries[i];
  95. ace->ae_tag = cpu_to_be32(acl_e->e_tag);
  96. switch (acl_e->e_tag) {
  97. case ACL_USER:
  98. ace->ae_id = cpu_to_be32(xfs_kuid_to_uid(acl_e->e_uid));
  99. break;
  100. case ACL_GROUP:
  101. ace->ae_id = cpu_to_be32(xfs_kgid_to_gid(acl_e->e_gid));
  102. break;
  103. default:
  104. ace->ae_id = cpu_to_be32(ACL_UNDEFINED_ID);
  105. break;
  106. }
  107. ace->ae_perm = cpu_to_be16(acl_e->e_perm);
  108. }
  109. }
  110. struct posix_acl *
  111. xfs_get_acl(struct inode *inode, int type)
  112. {
  113. struct xfs_inode *ip = XFS_I(inode);
  114. struct posix_acl *acl = NULL;
  115. struct xfs_acl *xfs_acl;
  116. unsigned char *ea_name;
  117. int error;
  118. int len;
  119. trace_xfs_get_acl(ip);
  120. switch (type) {
  121. case ACL_TYPE_ACCESS:
  122. ea_name = SGI_ACL_FILE;
  123. break;
  124. case ACL_TYPE_DEFAULT:
  125. ea_name = SGI_ACL_DEFAULT;
  126. break;
  127. default:
  128. BUG();
  129. }
  130. /*
  131. * If we have a cached ACLs value just return it, not need to
  132. * go out to the disk.
  133. */
  134. len = XFS_ACL_MAX_SIZE(ip->i_mount);
  135. xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
  136. if (!xfs_acl)
  137. return ERR_PTR(-ENOMEM);
  138. error = xfs_attr_get(ip, ea_name, (unsigned char *)xfs_acl,
  139. &len, ATTR_ROOT);
  140. if (error) {
  141. /*
  142. * If the attribute doesn't exist make sure we have a negative
  143. * cache entry, for any other error assume it is transient.
  144. */
  145. if (error != -ENOATTR)
  146. acl = ERR_PTR(error);
  147. } else {
  148. acl = xfs_acl_from_disk(xfs_acl, len,
  149. XFS_ACL_MAX_ENTRIES(ip->i_mount));
  150. }
  151. kmem_free(xfs_acl);
  152. return acl;
  153. }
  154. int
  155. __xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  156. {
  157. struct xfs_inode *ip = XFS_I(inode);
  158. unsigned char *ea_name;
  159. int error;
  160. switch (type) {
  161. case ACL_TYPE_ACCESS:
  162. ea_name = SGI_ACL_FILE;
  163. break;
  164. case ACL_TYPE_DEFAULT:
  165. if (!S_ISDIR(inode->i_mode))
  166. return acl ? -EACCES : 0;
  167. ea_name = SGI_ACL_DEFAULT;
  168. break;
  169. default:
  170. return -EINVAL;
  171. }
  172. if (acl) {
  173. struct xfs_acl *xfs_acl;
  174. int len = XFS_ACL_MAX_SIZE(ip->i_mount);
  175. xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
  176. if (!xfs_acl)
  177. return -ENOMEM;
  178. xfs_acl_to_disk(xfs_acl, acl);
  179. /* subtract away the unused acl entries */
  180. len -= sizeof(struct xfs_acl_entry) *
  181. (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
  182. error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
  183. len, ATTR_ROOT);
  184. kmem_free(xfs_acl);
  185. } else {
  186. /*
  187. * A NULL ACL argument means we want to remove the ACL.
  188. */
  189. error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  190. /*
  191. * If the attribute didn't exist to start with that's fine.
  192. */
  193. if (error == -ENOATTR)
  194. error = 0;
  195. }
  196. if (!error)
  197. set_cached_acl(inode, type, acl);
  198. return error;
  199. }
  200. static int
  201. xfs_set_mode(struct inode *inode, umode_t mode)
  202. {
  203. int error = 0;
  204. if (mode != inode->i_mode) {
  205. struct iattr iattr;
  206. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  207. iattr.ia_mode = mode;
  208. iattr.ia_ctime = current_time(inode);
  209. error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  210. }
  211. return error;
  212. }
  213. int
  214. xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  215. {
  216. umode_t mode;
  217. bool set_mode = false;
  218. int error = 0;
  219. if (!acl)
  220. goto set_acl;
  221. error = -E2BIG;
  222. if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
  223. return error;
  224. if (type == ACL_TYPE_ACCESS) {
  225. error = posix_acl_update_mode(inode, &mode, &acl);
  226. if (error)
  227. return error;
  228. set_mode = true;
  229. }
  230. set_acl:
  231. error = __xfs_set_acl(inode, acl, type);
  232. if (error)
  233. return error;
  234. /*
  235. * We set the mode after successfully updating the ACL xattr because the
  236. * xattr update can fail at ENOSPC and we don't want to change the mode
  237. * if the ACL update hasn't been applied.
  238. */
  239. if (set_mode)
  240. error = xfs_set_mode(inode, mode);
  241. return error;
  242. }