acl.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /*
  2. * fs/f2fs/acl.c
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * Portions of this code from linux/fs/ext2/acl.c
  8. *
  9. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License version 2 as
  13. * published by the Free Software Foundation.
  14. */
  15. #include <linux/f2fs_fs.h>
  16. #include "f2fs.h"
  17. #include "xattr.h"
  18. #include "acl.h"
  19. static inline size_t f2fs_acl_size(int count)
  20. {
  21. if (count <= 4) {
  22. return sizeof(struct f2fs_acl_header) +
  23. count * sizeof(struct f2fs_acl_entry_short);
  24. } else {
  25. return sizeof(struct f2fs_acl_header) +
  26. 4 * sizeof(struct f2fs_acl_entry_short) +
  27. (count - 4) * sizeof(struct f2fs_acl_entry);
  28. }
  29. }
  30. static inline int f2fs_acl_count(size_t size)
  31. {
  32. ssize_t s;
  33. size -= sizeof(struct f2fs_acl_header);
  34. s = size - 4 * sizeof(struct f2fs_acl_entry_short);
  35. if (s < 0) {
  36. if (size % sizeof(struct f2fs_acl_entry_short))
  37. return -1;
  38. return size / sizeof(struct f2fs_acl_entry_short);
  39. } else {
  40. if (s % sizeof(struct f2fs_acl_entry))
  41. return -1;
  42. return s / sizeof(struct f2fs_acl_entry) + 4;
  43. }
  44. }
  45. static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
  46. {
  47. int i, count;
  48. struct posix_acl *acl;
  49. struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
  50. struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
  51. const char *end = value + size;
  52. if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
  53. return ERR_PTR(-EINVAL);
  54. count = f2fs_acl_count(size);
  55. if (count < 0)
  56. return ERR_PTR(-EINVAL);
  57. if (count == 0)
  58. return NULL;
  59. acl = posix_acl_alloc(count, GFP_KERNEL);
  60. if (!acl)
  61. return ERR_PTR(-ENOMEM);
  62. for (i = 0; i < count; i++) {
  63. if ((char *)entry > end)
  64. goto fail;
  65. acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
  66. acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
  67. switch (acl->a_entries[i].e_tag) {
  68. case ACL_USER_OBJ:
  69. case ACL_GROUP_OBJ:
  70. case ACL_MASK:
  71. case ACL_OTHER:
  72. entry = (struct f2fs_acl_entry *)((char *)entry +
  73. sizeof(struct f2fs_acl_entry_short));
  74. break;
  75. case ACL_USER:
  76. acl->a_entries[i].e_uid =
  77. make_kuid(&init_user_ns,
  78. le32_to_cpu(entry->e_id));
  79. entry = (struct f2fs_acl_entry *)((char *)entry +
  80. sizeof(struct f2fs_acl_entry));
  81. break;
  82. case ACL_GROUP:
  83. acl->a_entries[i].e_gid =
  84. make_kgid(&init_user_ns,
  85. le32_to_cpu(entry->e_id));
  86. entry = (struct f2fs_acl_entry *)((char *)entry +
  87. sizeof(struct f2fs_acl_entry));
  88. break;
  89. default:
  90. goto fail;
  91. }
  92. }
  93. if ((char *)entry != end)
  94. goto fail;
  95. return acl;
  96. fail:
  97. posix_acl_release(acl);
  98. return ERR_PTR(-EINVAL);
  99. }
  100. static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
  101. {
  102. struct f2fs_acl_header *f2fs_acl;
  103. struct f2fs_acl_entry *entry;
  104. int i;
  105. f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
  106. sizeof(struct f2fs_acl_entry), GFP_KERNEL);
  107. if (!f2fs_acl)
  108. return ERR_PTR(-ENOMEM);
  109. f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
  110. entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
  111. for (i = 0; i < acl->a_count; i++) {
  112. entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
  113. entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
  114. switch (acl->a_entries[i].e_tag) {
  115. case ACL_USER:
  116. entry->e_id = cpu_to_le32(
  117. from_kuid(&init_user_ns,
  118. acl->a_entries[i].e_uid));
  119. entry = (struct f2fs_acl_entry *)((char *)entry +
  120. sizeof(struct f2fs_acl_entry));
  121. break;
  122. case ACL_GROUP:
  123. entry->e_id = cpu_to_le32(
  124. from_kgid(&init_user_ns,
  125. acl->a_entries[i].e_gid));
  126. entry = (struct f2fs_acl_entry *)((char *)entry +
  127. sizeof(struct f2fs_acl_entry));
  128. break;
  129. case ACL_USER_OBJ:
  130. case ACL_GROUP_OBJ:
  131. case ACL_MASK:
  132. case ACL_OTHER:
  133. entry = (struct f2fs_acl_entry *)((char *)entry +
  134. sizeof(struct f2fs_acl_entry_short));
  135. break;
  136. default:
  137. goto fail;
  138. }
  139. }
  140. *size = f2fs_acl_size(acl->a_count);
  141. return (void *)f2fs_acl;
  142. fail:
  143. kfree(f2fs_acl);
  144. return ERR_PTR(-EINVAL);
  145. }
  146. struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
  147. {
  148. int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  149. void *value = NULL;
  150. struct posix_acl *acl;
  151. int retval;
  152. if (type == ACL_TYPE_ACCESS)
  153. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  154. retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
  155. if (retval > 0) {
  156. value = kmalloc(retval, GFP_F2FS_ZERO);
  157. if (!value)
  158. return ERR_PTR(-ENOMEM);
  159. retval = f2fs_getxattr(inode, name_index, "", value, retval);
  160. }
  161. if (retval > 0)
  162. acl = f2fs_acl_from_disk(value, retval);
  163. else if (retval == -ENODATA)
  164. acl = NULL;
  165. else
  166. acl = ERR_PTR(retval);
  167. kfree(value);
  168. if (!IS_ERR(acl))
  169. set_cached_acl(inode, type, acl);
  170. return acl;
  171. }
  172. static int __f2fs_set_acl(struct inode *inode, int type,
  173. struct posix_acl *acl, struct page *ipage)
  174. {
  175. struct f2fs_inode_info *fi = F2FS_I(inode);
  176. int name_index;
  177. void *value = NULL;
  178. size_t size = 0;
  179. int error;
  180. if (acl) {
  181. error = posix_acl_valid(acl);
  182. if (error < 0)
  183. return error;
  184. }
  185. switch (type) {
  186. case ACL_TYPE_ACCESS:
  187. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  188. if (acl) {
  189. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  190. if (error < 0)
  191. return error;
  192. set_acl_inode(fi, inode->i_mode);
  193. if (error == 0)
  194. acl = NULL;
  195. }
  196. break;
  197. case ACL_TYPE_DEFAULT:
  198. name_index = F2FS_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 = f2fs_acl_to_disk(acl, &size);
  207. if (IS_ERR(value)) {
  208. cond_clear_inode_flag(fi, FI_ACL_MODE);
  209. return (int)PTR_ERR(value);
  210. }
  211. }
  212. error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
  213. kfree(value);
  214. if (!error)
  215. set_cached_acl(inode, type, acl);
  216. cond_clear_inode_flag(fi, FI_ACL_MODE);
  217. return error;
  218. }
  219. int f2fs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  220. {
  221. return __f2fs_set_acl(inode, type, acl, NULL);
  222. }
  223. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage)
  224. {
  225. struct posix_acl *default_acl, *acl;
  226. int error = 0;
  227. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  228. if (error)
  229. return error;
  230. if (default_acl) {
  231. error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
  232. ipage);
  233. posix_acl_release(default_acl);
  234. }
  235. if (acl) {
  236. if (error)
  237. error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
  238. ipage);
  239. posix_acl_release(acl);
  240. }
  241. return error;
  242. }