acl.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. umode_t mode = inode->i_mode;
  186. switch (type) {
  187. case ACL_TYPE_ACCESS:
  188. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  189. if (acl && !ipage) {
  190. error = posix_acl_update_mode(inode, &mode, &acl);
  191. if (error)
  192. return error;
  193. set_acl_inode(inode, mode);
  194. }
  195. break;
  196. case ACL_TYPE_DEFAULT:
  197. name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  198. if (!S_ISDIR(inode->i_mode))
  199. return acl ? -EACCES : 0;
  200. break;
  201. default:
  202. return -EINVAL;
  203. }
  204. if (acl) {
  205. value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
  206. if (IS_ERR(value)) {
  207. clear_inode_flag(inode, FI_ACL_MODE);
  208. return PTR_ERR(value);
  209. }
  210. }
  211. error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
  212. kfree(value);
  213. if (!error)
  214. set_cached_acl(inode, type, acl);
  215. clear_inode_flag(inode, FI_ACL_MODE);
  216. return error;
  217. }
  218. int f2fs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  219. {
  220. if (unlikely(f2fs_cp_error(F2FS_I_SB(inode))))
  221. return -EIO;
  222. return __f2fs_set_acl(inode, type, acl, NULL);
  223. }
  224. /*
  225. * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
  226. * are copied from posix_acl.c
  227. */
  228. static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
  229. gfp_t flags)
  230. {
  231. struct posix_acl *clone = NULL;
  232. if (acl) {
  233. int size = sizeof(struct posix_acl) + acl->a_count *
  234. sizeof(struct posix_acl_entry);
  235. clone = kmemdup(acl, size, flags);
  236. if (clone)
  237. refcount_set(&clone->a_refcount, 1);
  238. }
  239. return clone;
  240. }
  241. static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  242. {
  243. struct posix_acl_entry *pa, *pe;
  244. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  245. umode_t mode = *mode_p;
  246. int not_equiv = 0;
  247. /* assert(atomic_read(acl->a_refcount) == 1); */
  248. FOREACH_ACL_ENTRY(pa, acl, pe) {
  249. switch(pa->e_tag) {
  250. case ACL_USER_OBJ:
  251. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  252. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  253. break;
  254. case ACL_USER:
  255. case ACL_GROUP:
  256. not_equiv = 1;
  257. break;
  258. case ACL_GROUP_OBJ:
  259. group_obj = pa;
  260. break;
  261. case ACL_OTHER:
  262. pa->e_perm &= mode | ~S_IRWXO;
  263. mode &= pa->e_perm | ~S_IRWXO;
  264. break;
  265. case ACL_MASK:
  266. mask_obj = pa;
  267. not_equiv = 1;
  268. break;
  269. default:
  270. return -EIO;
  271. }
  272. }
  273. if (mask_obj) {
  274. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  275. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  276. } else {
  277. if (!group_obj)
  278. return -EIO;
  279. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  280. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  281. }
  282. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  283. return not_equiv;
  284. }
  285. static int f2fs_acl_create(struct inode *dir, umode_t *mode,
  286. struct posix_acl **default_acl, struct posix_acl **acl,
  287. struct page *dpage)
  288. {
  289. struct posix_acl *p;
  290. struct posix_acl *clone;
  291. int ret;
  292. *acl = NULL;
  293. *default_acl = NULL;
  294. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  295. return 0;
  296. p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
  297. if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  298. *mode &= ~current_umask();
  299. return 0;
  300. }
  301. if (IS_ERR(p))
  302. return PTR_ERR(p);
  303. clone = f2fs_acl_clone(p, GFP_NOFS);
  304. if (!clone) {
  305. ret = -ENOMEM;
  306. goto release_acl;
  307. }
  308. ret = f2fs_acl_create_masq(clone, mode);
  309. if (ret < 0)
  310. goto release_clone;
  311. if (ret == 0)
  312. posix_acl_release(clone);
  313. else
  314. *acl = clone;
  315. if (!S_ISDIR(*mode))
  316. posix_acl_release(p);
  317. else
  318. *default_acl = p;
  319. return 0;
  320. release_clone:
  321. posix_acl_release(clone);
  322. release_acl:
  323. posix_acl_release(p);
  324. return ret;
  325. }
  326. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
  327. struct page *dpage)
  328. {
  329. struct posix_acl *default_acl = NULL, *acl = NULL;
  330. int error = 0;
  331. error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
  332. if (error)
  333. return error;
  334. f2fs_mark_inode_dirty_sync(inode, true);
  335. if (default_acl) {
  336. error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
  337. ipage);
  338. posix_acl_release(default_acl);
  339. }
  340. if (acl) {
  341. if (!error)
  342. error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
  343. ipage);
  344. posix_acl_release(acl);
  345. }
  346. return error;
  347. }