acl.c 6.2 KB

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