xfs_acl.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 and
  144. * leave the cache entry as ACL_NOT_CACHED.
  145. */
  146. if (error == -ENOATTR)
  147. goto out_update_cache;
  148. acl = ERR_PTR(error);
  149. goto out;
  150. }
  151. acl = xfs_acl_from_disk(xfs_acl, len, XFS_ACL_MAX_ENTRIES(ip->i_mount));
  152. if (IS_ERR(acl))
  153. goto out;
  154. out_update_cache:
  155. set_cached_acl(inode, type, acl);
  156. out:
  157. kmem_free(xfs_acl);
  158. return acl;
  159. }
  160. STATIC int
  161. __xfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  162. {
  163. struct xfs_inode *ip = XFS_I(inode);
  164. unsigned char *ea_name;
  165. int error;
  166. switch (type) {
  167. case ACL_TYPE_ACCESS:
  168. ea_name = SGI_ACL_FILE;
  169. break;
  170. case ACL_TYPE_DEFAULT:
  171. if (!S_ISDIR(inode->i_mode))
  172. return acl ? -EACCES : 0;
  173. ea_name = SGI_ACL_DEFAULT;
  174. break;
  175. default:
  176. return -EINVAL;
  177. }
  178. if (acl) {
  179. struct xfs_acl *xfs_acl;
  180. int len = XFS_ACL_MAX_SIZE(ip->i_mount);
  181. xfs_acl = kmem_zalloc_large(len, KM_SLEEP);
  182. if (!xfs_acl)
  183. return -ENOMEM;
  184. xfs_acl_to_disk(xfs_acl, acl);
  185. /* subtract away the unused acl entries */
  186. len -= sizeof(struct xfs_acl_entry) *
  187. (XFS_ACL_MAX_ENTRIES(ip->i_mount) - acl->a_count);
  188. error = xfs_attr_set(ip, ea_name, (unsigned char *)xfs_acl,
  189. len, ATTR_ROOT);
  190. kmem_free(xfs_acl);
  191. } else {
  192. /*
  193. * A NULL ACL argument means we want to remove the ACL.
  194. */
  195. error = xfs_attr_remove(ip, ea_name, ATTR_ROOT);
  196. /*
  197. * If the attribute didn't exist to start with that's fine.
  198. */
  199. if (error == -ENOATTR)
  200. error = 0;
  201. }
  202. if (!error)
  203. set_cached_acl(inode, type, acl);
  204. return error;
  205. }
  206. static int
  207. xfs_set_mode(struct inode *inode, umode_t mode)
  208. {
  209. int error = 0;
  210. if (mode != inode->i_mode) {
  211. struct iattr iattr;
  212. iattr.ia_valid = ATTR_MODE | ATTR_CTIME;
  213. iattr.ia_mode = mode;
  214. iattr.ia_ctime = current_fs_time(inode->i_sb);
  215. error = xfs_setattr_nonsize(XFS_I(inode), &iattr, XFS_ATTR_NOACL);
  216. }
  217. return error;
  218. }
  219. static int
  220. xfs_acl_exists(struct inode *inode, unsigned char *name)
  221. {
  222. int len = XFS_ACL_MAX_SIZE(XFS_M(inode->i_sb));
  223. return (xfs_attr_get(XFS_I(inode), name, NULL, &len,
  224. ATTR_ROOT|ATTR_KERNOVAL) == 0);
  225. }
  226. int
  227. posix_acl_access_exists(struct inode *inode)
  228. {
  229. return xfs_acl_exists(inode, SGI_ACL_FILE);
  230. }
  231. int
  232. posix_acl_default_exists(struct inode *inode)
  233. {
  234. if (!S_ISDIR(inode->i_mode))
  235. return 0;
  236. return xfs_acl_exists(inode, SGI_ACL_DEFAULT);
  237. }
  238. int
  239. xfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  240. {
  241. int error = 0;
  242. if (!acl)
  243. goto set_acl;
  244. error = -E2BIG;
  245. if (acl->a_count > XFS_ACL_MAX_ENTRIES(XFS_M(inode->i_sb)))
  246. return error;
  247. if (type == ACL_TYPE_ACCESS) {
  248. umode_t mode;
  249. error = posix_acl_update_mode(inode, &mode, &acl);
  250. if (error)
  251. return error;
  252. error = xfs_set_mode(inode, mode);
  253. if (error)
  254. return error;
  255. }
  256. set_acl:
  257. return __xfs_set_acl(inode, type, acl);
  258. }