acl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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_NOFS);
  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(struct f2fs_sb_info *sbi,
  101. const struct posix_acl *acl, size_t *size)
  102. {
  103. struct f2fs_acl_header *f2fs_acl;
  104. struct f2fs_acl_entry *entry;
  105. int i;
  106. f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
  107. acl->a_count * sizeof(struct f2fs_acl_entry),
  108. GFP_NOFS);
  109. if (!f2fs_acl)
  110. return ERR_PTR(-ENOMEM);
  111. f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
  112. entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
  113. for (i = 0; i < acl->a_count; i++) {
  114. entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
  115. entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
  116. switch (acl->a_entries[i].e_tag) {
  117. case ACL_USER:
  118. entry->e_id = cpu_to_le32(
  119. from_kuid(&init_user_ns,
  120. acl->a_entries[i].e_uid));
  121. entry = (struct f2fs_acl_entry *)((char *)entry +
  122. sizeof(struct f2fs_acl_entry));
  123. break;
  124. case ACL_GROUP:
  125. entry->e_id = cpu_to_le32(
  126. from_kgid(&init_user_ns,
  127. acl->a_entries[i].e_gid));
  128. entry = (struct f2fs_acl_entry *)((char *)entry +
  129. sizeof(struct f2fs_acl_entry));
  130. break;
  131. case ACL_USER_OBJ:
  132. case ACL_GROUP_OBJ:
  133. case ACL_MASK:
  134. case ACL_OTHER:
  135. entry = (struct f2fs_acl_entry *)((char *)entry +
  136. sizeof(struct f2fs_acl_entry_short));
  137. break;
  138. default:
  139. goto fail;
  140. }
  141. }
  142. *size = f2fs_acl_size(acl->a_count);
  143. return (void *)f2fs_acl;
  144. fail:
  145. kfree(f2fs_acl);
  146. return ERR_PTR(-EINVAL);
  147. }
  148. static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
  149. struct page *dpage)
  150. {
  151. int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  152. void *value = NULL;
  153. struct posix_acl *acl;
  154. int retval;
  155. if (type == ACL_TYPE_ACCESS)
  156. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  157. retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
  158. if (retval > 0) {
  159. value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
  160. if (!value)
  161. return ERR_PTR(-ENOMEM);
  162. retval = f2fs_getxattr(inode, name_index, "", value,
  163. retval, dpage);
  164. }
  165. if (retval > 0)
  166. acl = f2fs_acl_from_disk(value, retval);
  167. else if (retval == -ENODATA)
  168. acl = NULL;
  169. else
  170. acl = ERR_PTR(retval);
  171. kfree(value);
  172. return acl;
  173. }
  174. struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
  175. {
  176. return __f2fs_get_acl(inode, type, NULL);
  177. }
  178. static int __f2fs_set_acl(struct inode *inode, int type,
  179. struct posix_acl *acl, struct page *ipage)
  180. {
  181. int name_index;
  182. void *value = NULL;
  183. size_t size = 0;
  184. int error;
  185. switch (type) {
  186. case ACL_TYPE_ACCESS:
  187. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  188. if (acl && !ipage) {
  189. error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  190. if (error)
  191. return error;
  192. set_acl_inode(inode, inode->i_mode);
  193. }
  194. break;
  195. case ACL_TYPE_DEFAULT:
  196. name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  197. if (!S_ISDIR(inode->i_mode))
  198. return acl ? -EACCES : 0;
  199. break;
  200. default:
  201. return -EINVAL;
  202. }
  203. if (acl) {
  204. value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
  205. if (IS_ERR(value)) {
  206. clear_inode_flag(inode, FI_ACL_MODE);
  207. return (int)PTR_ERR(value);
  208. }
  209. }
  210. error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
  211. kfree(value);
  212. if (!error)
  213. set_cached_acl(inode, type, acl);
  214. clear_inode_flag(inode, FI_ACL_MODE);
  215. return error;
  216. }
  217. int f2fs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  218. {
  219. return __f2fs_set_acl(inode, type, acl, NULL);
  220. }
  221. /*
  222. * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
  223. * are copied from posix_acl.c
  224. */
  225. static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
  226. gfp_t flags)
  227. {
  228. struct posix_acl *clone = NULL;
  229. if (acl) {
  230. int size = sizeof(struct posix_acl) + acl->a_count *
  231. sizeof(struct posix_acl_entry);
  232. clone = kmemdup(acl, size, flags);
  233. if (clone)
  234. atomic_set(&clone->a_refcount, 1);
  235. }
  236. return clone;
  237. }
  238. static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  239. {
  240. struct posix_acl_entry *pa, *pe;
  241. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  242. umode_t mode = *mode_p;
  243. int not_equiv = 0;
  244. /* assert(atomic_read(acl->a_refcount) == 1); */
  245. FOREACH_ACL_ENTRY(pa, acl, pe) {
  246. switch(pa->e_tag) {
  247. case ACL_USER_OBJ:
  248. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  249. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  250. break;
  251. case ACL_USER:
  252. case ACL_GROUP:
  253. not_equiv = 1;
  254. break;
  255. case ACL_GROUP_OBJ:
  256. group_obj = pa;
  257. break;
  258. case ACL_OTHER:
  259. pa->e_perm &= mode | ~S_IRWXO;
  260. mode &= pa->e_perm | ~S_IRWXO;
  261. break;
  262. case ACL_MASK:
  263. mask_obj = pa;
  264. not_equiv = 1;
  265. break;
  266. default:
  267. return -EIO;
  268. }
  269. }
  270. if (mask_obj) {
  271. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  272. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  273. } else {
  274. if (!group_obj)
  275. return -EIO;
  276. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  277. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  278. }
  279. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  280. return not_equiv;
  281. }
  282. static int f2fs_acl_create(struct inode *dir, umode_t *mode,
  283. struct posix_acl **default_acl, struct posix_acl **acl,
  284. struct page *dpage)
  285. {
  286. struct posix_acl *p;
  287. struct posix_acl *clone;
  288. int ret;
  289. *acl = NULL;
  290. *default_acl = NULL;
  291. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  292. return 0;
  293. p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
  294. if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  295. *mode &= ~current_umask();
  296. return 0;
  297. }
  298. if (IS_ERR(p))
  299. return PTR_ERR(p);
  300. clone = f2fs_acl_clone(p, GFP_NOFS);
  301. if (!clone)
  302. goto no_mem;
  303. ret = f2fs_acl_create_masq(clone, mode);
  304. if (ret < 0)
  305. goto no_mem_clone;
  306. if (ret == 0)
  307. posix_acl_release(clone);
  308. else
  309. *acl = clone;
  310. if (!S_ISDIR(*mode))
  311. posix_acl_release(p);
  312. else
  313. *default_acl = p;
  314. return 0;
  315. no_mem_clone:
  316. posix_acl_release(clone);
  317. no_mem:
  318. posix_acl_release(p);
  319. return -ENOMEM;
  320. }
  321. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
  322. struct page *dpage)
  323. {
  324. struct posix_acl *default_acl = NULL, *acl = NULL;
  325. int error = 0;
  326. error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
  327. if (error)
  328. return error;
  329. f2fs_mark_inode_dirty_sync(inode);
  330. if (default_acl) {
  331. error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
  332. ipage);
  333. posix_acl_release(default_acl);
  334. }
  335. if (acl) {
  336. if (!error)
  337. error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
  338. ipage);
  339. posix_acl_release(acl);
  340. }
  341. return error;
  342. }