acl.c 6.4 KB

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