acl.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * linux/fs/ext4/acl.c
  3. *
  4. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  5. */
  6. #include "ext4_jbd2.h"
  7. #include "ext4.h"
  8. #include "xattr.h"
  9. #include "acl.h"
  10. /*
  11. * Convert from filesystem to in-memory representation.
  12. */
  13. static struct posix_acl *
  14. ext4_acl_from_disk(const void *value, size_t size)
  15. {
  16. const char *end = (char *)value + size;
  17. int n, count;
  18. struct posix_acl *acl;
  19. if (!value)
  20. return NULL;
  21. if (size < sizeof(ext4_acl_header))
  22. return ERR_PTR(-EINVAL);
  23. if (((ext4_acl_header *)value)->a_version !=
  24. cpu_to_le32(EXT4_ACL_VERSION))
  25. return ERR_PTR(-EINVAL);
  26. value = (char *)value + sizeof(ext4_acl_header);
  27. count = ext4_acl_count(size);
  28. if (count < 0)
  29. return ERR_PTR(-EINVAL);
  30. if (count == 0)
  31. return NULL;
  32. acl = posix_acl_alloc(count, GFP_NOFS);
  33. if (!acl)
  34. return ERR_PTR(-ENOMEM);
  35. for (n = 0; n < count; n++) {
  36. ext4_acl_entry *entry =
  37. (ext4_acl_entry *)value;
  38. if ((char *)value + sizeof(ext4_acl_entry_short) > end)
  39. goto fail;
  40. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  41. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  42. switch (acl->a_entries[n].e_tag) {
  43. case ACL_USER_OBJ:
  44. case ACL_GROUP_OBJ:
  45. case ACL_MASK:
  46. case ACL_OTHER:
  47. value = (char *)value +
  48. sizeof(ext4_acl_entry_short);
  49. break;
  50. case ACL_USER:
  51. value = (char *)value + sizeof(ext4_acl_entry);
  52. if ((char *)value > end)
  53. goto fail;
  54. acl->a_entries[n].e_uid =
  55. make_kuid(&init_user_ns,
  56. le32_to_cpu(entry->e_id));
  57. break;
  58. case ACL_GROUP:
  59. value = (char *)value + sizeof(ext4_acl_entry);
  60. if ((char *)value > end)
  61. goto fail;
  62. acl->a_entries[n].e_gid =
  63. make_kgid(&init_user_ns,
  64. le32_to_cpu(entry->e_id));
  65. break;
  66. default:
  67. goto fail;
  68. }
  69. }
  70. if (value != end)
  71. goto fail;
  72. return acl;
  73. fail:
  74. posix_acl_release(acl);
  75. return ERR_PTR(-EINVAL);
  76. }
  77. /*
  78. * Convert from in-memory to filesystem representation.
  79. */
  80. static void *
  81. ext4_acl_to_disk(const struct posix_acl *acl, size_t *size)
  82. {
  83. ext4_acl_header *ext_acl;
  84. char *e;
  85. size_t n;
  86. *size = ext4_acl_size(acl->a_count);
  87. ext_acl = kmalloc(sizeof(ext4_acl_header) + acl->a_count *
  88. sizeof(ext4_acl_entry), GFP_NOFS);
  89. if (!ext_acl)
  90. return ERR_PTR(-ENOMEM);
  91. ext_acl->a_version = cpu_to_le32(EXT4_ACL_VERSION);
  92. e = (char *)ext_acl + sizeof(ext4_acl_header);
  93. for (n = 0; n < acl->a_count; n++) {
  94. const struct posix_acl_entry *acl_e = &acl->a_entries[n];
  95. ext4_acl_entry *entry = (ext4_acl_entry *)e;
  96. entry->e_tag = cpu_to_le16(acl_e->e_tag);
  97. entry->e_perm = cpu_to_le16(acl_e->e_perm);
  98. switch (acl_e->e_tag) {
  99. case ACL_USER:
  100. entry->e_id = cpu_to_le32(
  101. from_kuid(&init_user_ns, acl_e->e_uid));
  102. e += sizeof(ext4_acl_entry);
  103. break;
  104. case ACL_GROUP:
  105. entry->e_id = cpu_to_le32(
  106. from_kgid(&init_user_ns, acl_e->e_gid));
  107. e += sizeof(ext4_acl_entry);
  108. break;
  109. case ACL_USER_OBJ:
  110. case ACL_GROUP_OBJ:
  111. case ACL_MASK:
  112. case ACL_OTHER:
  113. e += sizeof(ext4_acl_entry_short);
  114. break;
  115. default:
  116. goto fail;
  117. }
  118. }
  119. return (char *)ext_acl;
  120. fail:
  121. kfree(ext_acl);
  122. return ERR_PTR(-EINVAL);
  123. }
  124. /*
  125. * Inode operation get_posix_acl().
  126. *
  127. * inode->i_mutex: don't care
  128. */
  129. struct posix_acl *
  130. ext4_get_acl(struct inode *inode, int type)
  131. {
  132. int name_index;
  133. char *value = NULL;
  134. struct posix_acl *acl;
  135. int retval;
  136. switch (type) {
  137. case ACL_TYPE_ACCESS:
  138. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  139. break;
  140. case ACL_TYPE_DEFAULT:
  141. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  142. break;
  143. default:
  144. BUG();
  145. }
  146. retval = ext4_xattr_get(inode, name_index, "", NULL, 0);
  147. if (retval > 0) {
  148. value = kmalloc(retval, GFP_NOFS);
  149. if (!value)
  150. return ERR_PTR(-ENOMEM);
  151. retval = ext4_xattr_get(inode, name_index, "", value, retval);
  152. }
  153. if (retval > 0)
  154. acl = ext4_acl_from_disk(value, retval);
  155. else if (retval == -ENODATA || retval == -ENOSYS)
  156. acl = NULL;
  157. else
  158. acl = ERR_PTR(retval);
  159. kfree(value);
  160. if (!IS_ERR(acl))
  161. set_cached_acl(inode, type, acl);
  162. return acl;
  163. }
  164. /*
  165. * Set the access or default ACL of an inode.
  166. *
  167. * inode->i_mutex: down unless called from ext4_new_inode
  168. */
  169. static int
  170. __ext4_set_acl(handle_t *handle, struct inode *inode, int type,
  171. struct posix_acl *acl)
  172. {
  173. int name_index;
  174. void *value = NULL;
  175. size_t size = 0;
  176. int error;
  177. switch (type) {
  178. case ACL_TYPE_ACCESS:
  179. name_index = EXT4_XATTR_INDEX_POSIX_ACL_ACCESS;
  180. if (acl) {
  181. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  182. if (error < 0)
  183. return error;
  184. else {
  185. inode->i_ctime = ext4_current_time(inode);
  186. ext4_mark_inode_dirty(handle, inode);
  187. if (error == 0)
  188. acl = NULL;
  189. }
  190. }
  191. break;
  192. case ACL_TYPE_DEFAULT:
  193. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  194. if (!S_ISDIR(inode->i_mode))
  195. return acl ? -EACCES : 0;
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. if (acl) {
  201. value = ext4_acl_to_disk(acl, &size);
  202. if (IS_ERR(value))
  203. return (int)PTR_ERR(value);
  204. }
  205. error = ext4_xattr_set_handle(handle, inode, name_index, "",
  206. value, size, 0);
  207. kfree(value);
  208. if (!error)
  209. set_cached_acl(inode, type, acl);
  210. return error;
  211. }
  212. int
  213. ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  214. {
  215. handle_t *handle;
  216. int error, retries = 0;
  217. retry:
  218. handle = ext4_journal_start(inode, EXT4_HT_XATTR,
  219. ext4_jbd2_credits_xattr(inode));
  220. if (IS_ERR(handle))
  221. return PTR_ERR(handle);
  222. error = __ext4_set_acl(handle, inode, type, acl);
  223. ext4_journal_stop(handle);
  224. if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  225. goto retry;
  226. return error;
  227. }
  228. /*
  229. * Initialize the ACLs of a new inode. Called from ext4_new_inode.
  230. *
  231. * dir->i_mutex: down
  232. * inode->i_mutex: up (access to inode is still exclusive)
  233. */
  234. int
  235. ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  236. {
  237. struct posix_acl *default_acl, *acl;
  238. int error;
  239. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  240. if (error)
  241. return error;
  242. if (default_acl) {
  243. error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
  244. default_acl);
  245. posix_acl_release(default_acl);
  246. }
  247. if (acl) {
  248. if (!error)
  249. error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
  250. acl);
  251. posix_acl_release(acl);
  252. }
  253. return error;
  254. }