acl.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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(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_NOFS);
  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. static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
  147. struct page *dpage)
  148. {
  149. int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  150. void *value = NULL;
  151. struct posix_acl *acl;
  152. int retval;
  153. if (type == ACL_TYPE_ACCESS)
  154. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  155. retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
  156. if (retval > 0) {
  157. value = kmalloc(retval, GFP_F2FS_ZERO);
  158. if (!value)
  159. return ERR_PTR(-ENOMEM);
  160. retval = f2fs_getxattr(inode, name_index, "", value,
  161. retval, dpage);
  162. }
  163. if (retval > 0)
  164. acl = f2fs_acl_from_disk(value, retval);
  165. else if (retval == -ENODATA)
  166. acl = NULL;
  167. else
  168. acl = ERR_PTR(retval);
  169. kfree(value);
  170. if (!IS_ERR(acl))
  171. set_cached_acl(inode, type, acl);
  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. struct f2fs_inode_info *fi = F2FS_I(inode);
  182. int name_index;
  183. void *value = NULL;
  184. size_t size = 0;
  185. int error;
  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, &inode->i_mode, &acl);
  191. if (error)
  192. return error;
  193. set_acl_inode(fi, inode->i_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(acl, &size);
  206. if (IS_ERR(value)) {
  207. clear_inode_flag(fi, FI_ACL_MODE);
  208. return (int)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(fi, FI_ACL_MODE);
  216. return error;
  217. }
  218. int f2fs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  219. {
  220. return __f2fs_set_acl(inode, type, acl, NULL);
  221. }
  222. /*
  223. * Most part of f2fs_acl_clone, f2fs_acl_create_masq, f2fs_acl_create
  224. * are copied from posix_acl.c
  225. */
  226. static struct posix_acl *f2fs_acl_clone(const struct posix_acl *acl,
  227. gfp_t flags)
  228. {
  229. struct posix_acl *clone = NULL;
  230. if (acl) {
  231. int size = sizeof(struct posix_acl) + acl->a_count *
  232. sizeof(struct posix_acl_entry);
  233. clone = kmemdup(acl, size, flags);
  234. if (clone)
  235. atomic_set(&clone->a_refcount, 1);
  236. }
  237. return clone;
  238. }
  239. static int f2fs_acl_create_masq(struct posix_acl *acl, umode_t *mode_p)
  240. {
  241. struct posix_acl_entry *pa, *pe;
  242. struct posix_acl_entry *group_obj = NULL, *mask_obj = NULL;
  243. umode_t mode = *mode_p;
  244. int not_equiv = 0;
  245. /* assert(atomic_read(acl->a_refcount) == 1); */
  246. FOREACH_ACL_ENTRY(pa, acl, pe) {
  247. switch(pa->e_tag) {
  248. case ACL_USER_OBJ:
  249. pa->e_perm &= (mode >> 6) | ~S_IRWXO;
  250. mode &= (pa->e_perm << 6) | ~S_IRWXU;
  251. break;
  252. case ACL_USER:
  253. case ACL_GROUP:
  254. not_equiv = 1;
  255. break;
  256. case ACL_GROUP_OBJ:
  257. group_obj = pa;
  258. break;
  259. case ACL_OTHER:
  260. pa->e_perm &= mode | ~S_IRWXO;
  261. mode &= pa->e_perm | ~S_IRWXO;
  262. break;
  263. case ACL_MASK:
  264. mask_obj = pa;
  265. not_equiv = 1;
  266. break;
  267. default:
  268. return -EIO;
  269. }
  270. }
  271. if (mask_obj) {
  272. mask_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  273. mode &= (mask_obj->e_perm << 3) | ~S_IRWXG;
  274. } else {
  275. if (!group_obj)
  276. return -EIO;
  277. group_obj->e_perm &= (mode >> 3) | ~S_IRWXO;
  278. mode &= (group_obj->e_perm << 3) | ~S_IRWXG;
  279. }
  280. *mode_p = (*mode_p & ~S_IRWXUGO) | mode;
  281. return not_equiv;
  282. }
  283. static int f2fs_acl_create(struct inode *dir, umode_t *mode,
  284. struct posix_acl **default_acl, struct posix_acl **acl,
  285. struct page *dpage)
  286. {
  287. struct posix_acl *p;
  288. struct posix_acl *clone;
  289. int ret;
  290. *acl = NULL;
  291. *default_acl = NULL;
  292. if (S_ISLNK(*mode) || !IS_POSIXACL(dir))
  293. return 0;
  294. p = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
  295. if (!p || p == ERR_PTR(-EOPNOTSUPP)) {
  296. *mode &= ~current_umask();
  297. return 0;
  298. }
  299. if (IS_ERR(p))
  300. return PTR_ERR(p);
  301. clone = f2fs_acl_clone(p, GFP_NOFS);
  302. if (!clone) {
  303. ret = -ENOMEM;
  304. goto release_acl;
  305. }
  306. ret = f2fs_acl_create_masq(clone, mode);
  307. if (ret < 0)
  308. goto release_clone;
  309. if (ret == 0)
  310. posix_acl_release(clone);
  311. else
  312. *acl = clone;
  313. if (!S_ISDIR(*mode))
  314. posix_acl_release(p);
  315. else
  316. *default_acl = p;
  317. return 0;
  318. release_clone:
  319. posix_acl_release(clone);
  320. release_acl:
  321. posix_acl_release(p);
  322. return ret;
  323. }
  324. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
  325. struct page *dpage)
  326. {
  327. struct posix_acl *default_acl = NULL, *acl = NULL;
  328. int error = 0;
  329. error = f2fs_acl_create(dir, &inode->i_mode, &default_acl, &acl, dpage);
  330. if (error)
  331. return error;
  332. if (default_acl) {
  333. error = __f2fs_set_acl(inode, ACL_TYPE_DEFAULT, default_acl,
  334. ipage);
  335. posix_acl_release(default_acl);
  336. }
  337. if (acl) {
  338. if (!error)
  339. error = __f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl,
  340. ipage);
  341. posix_acl_release(acl);
  342. }
  343. return error;
  344. }