acl.c 6.1 KB

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