acl.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. case ACL_GROUP:
  77. acl->a_entries[i].e_id = le32_to_cpu(entry->e_id);
  78. entry = (struct f2fs_acl_entry *)((char *)entry +
  79. sizeof(struct f2fs_acl_entry));
  80. break;
  81. default:
  82. goto fail;
  83. }
  84. }
  85. if ((char *)entry != end)
  86. goto fail;
  87. return acl;
  88. fail:
  89. posix_acl_release(acl);
  90. return ERR_PTR(-EINVAL);
  91. }
  92. static void *f2fs_acl_to_disk(struct f2fs_sb_info *sbi,
  93. const struct posix_acl *acl, size_t *size)
  94. {
  95. struct f2fs_acl_header *f2fs_acl;
  96. struct f2fs_acl_entry *entry;
  97. int i;
  98. f2fs_acl = f2fs_kmalloc(sbi, sizeof(struct f2fs_acl_header) +
  99. acl->a_count * sizeof(struct f2fs_acl_entry),
  100. GFP_NOFS);
  101. if (!f2fs_acl)
  102. return ERR_PTR(-ENOMEM);
  103. f2fs_acl->a_version = cpu_to_le32(F2FS_ACL_VERSION);
  104. entry = (struct f2fs_acl_entry *)(f2fs_acl + 1);
  105. for (i = 0; i < acl->a_count; i++) {
  106. entry->e_tag = cpu_to_le16(acl->a_entries[i].e_tag);
  107. entry->e_perm = cpu_to_le16(acl->a_entries[i].e_perm);
  108. switch (acl->a_entries[i].e_tag) {
  109. case ACL_USER:
  110. case ACL_GROUP:
  111. entry->e_id = cpu_to_le32(acl->a_entries[i].e_id);
  112. entry = (struct f2fs_acl_entry *)((char *)entry +
  113. sizeof(struct f2fs_acl_entry));
  114. break;
  115. case ACL_USER_OBJ:
  116. case ACL_GROUP_OBJ:
  117. case ACL_MASK:
  118. case ACL_OTHER:
  119. entry = (struct f2fs_acl_entry *)((char *)entry +
  120. sizeof(struct f2fs_acl_entry_short));
  121. break;
  122. default:
  123. goto fail;
  124. }
  125. }
  126. *size = f2fs_acl_size(acl->a_count);
  127. return (void *)f2fs_acl;
  128. fail:
  129. kfree(f2fs_acl);
  130. return ERR_PTR(-EINVAL);
  131. }
  132. static struct posix_acl *__f2fs_get_acl(struct inode *inode, int type,
  133. struct page *dpage)
  134. {
  135. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  136. int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  137. void *value = NULL;
  138. struct posix_acl *acl;
  139. int retval;
  140. if (!test_opt(sbi, POSIX_ACL))
  141. return NULL;
  142. acl = get_cached_acl(inode, type);
  143. if (acl != ACL_NOT_CACHED)
  144. return acl;
  145. if (type == ACL_TYPE_ACCESS)
  146. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  147. retval = f2fs_getxattr(inode, name_index, "", NULL, 0, dpage);
  148. if (retval > 0) {
  149. value = f2fs_kmalloc(F2FS_I_SB(inode), retval, GFP_F2FS_ZERO);
  150. if (!value)
  151. return ERR_PTR(-ENOMEM);
  152. retval = f2fs_getxattr(inode, name_index, "", value,
  153. retval, dpage);
  154. }
  155. if (retval > 0)
  156. acl = f2fs_acl_from_disk(value, retval);
  157. else if (retval == -ENODATA)
  158. acl = NULL;
  159. else
  160. acl = ERR_PTR(retval);
  161. kfree(value);
  162. if (!IS_ERR(acl))
  163. set_cached_acl(inode, type, acl);
  164. return acl;
  165. }
  166. struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
  167. {
  168. return __f2fs_get_acl(inode, type, NULL);
  169. }
  170. static int f2fs_set_acl(struct inode *inode, int type,
  171. struct posix_acl *acl, struct page *ipage)
  172. {
  173. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  174. int name_index;
  175. void *value = NULL;
  176. size_t size = 0;
  177. int error;
  178. if (!test_opt(sbi, POSIX_ACL))
  179. return 0;
  180. if (S_ISLNK(inode->i_mode))
  181. return -EOPNOTSUPP;
  182. switch (type) {
  183. case ACL_TYPE_ACCESS:
  184. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  185. if (acl) {
  186. error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  187. if (error)
  188. return error;
  189. set_acl_inode(inode, inode->i_mode);
  190. }
  191. break;
  192. case ACL_TYPE_DEFAULT:
  193. name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  194. if (!S_ISDIR(inode->i_mode))
  195. return acl ? -EACCES : 0;
  196. break;
  197. default:
  198. return -EINVAL;
  199. }
  200. f2fs_mark_inode_dirty_sync(inode, true);
  201. if (acl) {
  202. value = f2fs_acl_to_disk(F2FS_I_SB(inode), acl, &size);
  203. if (IS_ERR(value)) {
  204. clear_inode_flag(inode, FI_ACL_MODE);
  205. return (int)PTR_ERR(value);
  206. }
  207. }
  208. error = f2fs_setxattr(inode, name_index, "", value, size, ipage, 0);
  209. kfree(value);
  210. if (!error)
  211. set_cached_acl(inode, type, acl);
  212. clear_inode_flag(inode, FI_ACL_MODE);
  213. return error;
  214. }
  215. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage,
  216. struct page *dpage)
  217. {
  218. struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
  219. struct posix_acl *acl = NULL;
  220. int error = 0;
  221. if (!S_ISLNK(inode->i_mode)) {
  222. if (test_opt(sbi, POSIX_ACL)) {
  223. acl = __f2fs_get_acl(dir, ACL_TYPE_DEFAULT, dpage);
  224. if (IS_ERR(acl))
  225. return PTR_ERR(acl);
  226. }
  227. if (!acl)
  228. inode->i_mode &= ~current_umask();
  229. }
  230. if (!test_opt(sbi, POSIX_ACL) || !acl)
  231. goto cleanup;
  232. if (S_ISDIR(inode->i_mode)) {
  233. error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl, ipage);
  234. if (error)
  235. goto cleanup;
  236. }
  237. error = __posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
  238. if (error < 0)
  239. return error;
  240. if (error > 0)
  241. error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, ipage);
  242. f2fs_mark_inode_dirty_sync(inode, true);
  243. cleanup:
  244. posix_acl_release(acl);
  245. return error;
  246. }
  247. int f2fs_acl_chmod(struct inode *inode)
  248. {
  249. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  250. struct posix_acl *acl;
  251. int error;
  252. umode_t mode = get_inode_mode(inode);
  253. if (!test_opt(sbi, POSIX_ACL))
  254. return 0;
  255. if (S_ISLNK(mode))
  256. return -EOPNOTSUPP;
  257. acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
  258. if (IS_ERR(acl) || !acl)
  259. return PTR_ERR(acl);
  260. error = __posix_acl_chmod(&acl, GFP_KERNEL, mode);
  261. if (error)
  262. return error;
  263. error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, NULL);
  264. posix_acl_release(acl);
  265. return error;
  266. }
  267. static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
  268. size_t list_size, const char *name, size_t name_len, int type)
  269. {
  270. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  271. const char *xname = POSIX_ACL_XATTR_DEFAULT;
  272. size_t size;
  273. if (!test_opt(sbi, POSIX_ACL))
  274. return 0;
  275. if (type == ACL_TYPE_ACCESS)
  276. xname = POSIX_ACL_XATTR_ACCESS;
  277. size = strlen(xname) + 1;
  278. if (list && size <= list_size)
  279. memcpy(list, xname, size);
  280. return size;
  281. }
  282. static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
  283. void *buffer, size_t size, int type)
  284. {
  285. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  286. struct posix_acl *acl;
  287. int error;
  288. if (strcmp(name, "") != 0)
  289. return -EINVAL;
  290. if (!test_opt(sbi, POSIX_ACL))
  291. return -EOPNOTSUPP;
  292. acl = f2fs_get_acl(d_inode(dentry), type);
  293. if (IS_ERR(acl))
  294. return PTR_ERR(acl);
  295. if (!acl)
  296. return -ENODATA;
  297. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  298. posix_acl_release(acl);
  299. return error;
  300. }
  301. static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
  302. const void *value, size_t size, int flags, int type)
  303. {
  304. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  305. struct inode *inode = d_inode(dentry);
  306. struct posix_acl *acl = NULL;
  307. int error;
  308. if (strcmp(name, "") != 0)
  309. return -EINVAL;
  310. if (!test_opt(sbi, POSIX_ACL))
  311. return -EOPNOTSUPP;
  312. if (!inode_owner_or_capable(inode))
  313. return -EPERM;
  314. if (value) {
  315. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  316. if (IS_ERR(acl))
  317. return PTR_ERR(acl);
  318. if (acl) {
  319. error = posix_acl_valid(acl);
  320. if (error)
  321. goto release_and_out;
  322. }
  323. } else {
  324. acl = NULL;
  325. }
  326. error = f2fs_set_acl(inode, type, acl, NULL);
  327. release_and_out:
  328. posix_acl_release(acl);
  329. return error;
  330. }
  331. const struct xattr_handler f2fs_xattr_acl_default_handler = {
  332. .prefix = POSIX_ACL_XATTR_DEFAULT,
  333. .flags = ACL_TYPE_DEFAULT,
  334. .list = f2fs_xattr_list_acl,
  335. .get = f2fs_xattr_get_acl,
  336. .set = f2fs_xattr_set_acl,
  337. };
  338. const struct xattr_handler f2fs_xattr_acl_access_handler = {
  339. .prefix = POSIX_ACL_XATTR_ACCESS,
  340. .flags = ACL_TYPE_ACCESS,
  341. .list = f2fs_xattr_list_acl,
  342. .get = f2fs_xattr_get_acl,
  343. .set = f2fs_xattr_set_acl,
  344. };