acl.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. break;
  181. case ACL_TYPE_DEFAULT:
  182. name_index = EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT;
  183. if (!S_ISDIR(inode->i_mode))
  184. return acl ? -EACCES : 0;
  185. break;
  186. default:
  187. return -EINVAL;
  188. }
  189. if (acl) {
  190. value = ext4_acl_to_disk(acl, &size);
  191. if (IS_ERR(value))
  192. return (int)PTR_ERR(value);
  193. }
  194. error = ext4_xattr_set_handle(handle, inode, name_index, "",
  195. value, size, 0);
  196. kfree(value);
  197. if (!error)
  198. set_cached_acl(inode, type, acl);
  199. return error;
  200. }
  201. int
  202. ext4_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  203. {
  204. handle_t *handle;
  205. int error, retries = 0;
  206. umode_t mode = inode->i_mode;
  207. int update_mode = 0;
  208. retry:
  209. handle = ext4_journal_start(inode, EXT4_HT_XATTR,
  210. ext4_jbd2_credits_xattr(inode));
  211. if (IS_ERR(handle))
  212. return PTR_ERR(handle);
  213. if ((type == ACL_TYPE_ACCESS) && acl) {
  214. error = posix_acl_update_mode(inode, &mode, &acl);
  215. if (error)
  216. goto out_stop;
  217. update_mode = 1;
  218. }
  219. error = __ext4_set_acl(handle, inode, type, acl);
  220. if (!error && update_mode) {
  221. inode->i_mode = mode;
  222. inode->i_ctime = ext4_current_time(inode);
  223. ext4_mark_inode_dirty(handle, inode);
  224. }
  225. out_stop:
  226. ext4_journal_stop(handle);
  227. if (error == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  228. goto retry;
  229. return error;
  230. }
  231. /*
  232. * Initialize the ACLs of a new inode. Called from ext4_new_inode.
  233. *
  234. * dir->i_mutex: down
  235. * inode->i_mutex: up (access to inode is still exclusive)
  236. */
  237. int
  238. ext4_init_acl(handle_t *handle, struct inode *inode, struct inode *dir)
  239. {
  240. struct posix_acl *default_acl, *acl;
  241. int error;
  242. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  243. if (error)
  244. return error;
  245. if (default_acl) {
  246. error = __ext4_set_acl(handle, inode, ACL_TYPE_DEFAULT,
  247. default_acl);
  248. posix_acl_release(default_acl);
  249. }
  250. if (acl) {
  251. if (!error)
  252. error = __ext4_set_acl(handle, inode, ACL_TYPE_ACCESS,
  253. acl);
  254. posix_acl_release(acl);
  255. }
  256. return error;
  257. }