acl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. #define get_inode_mode(i) ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
  20. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  21. static inline size_t f2fs_acl_size(int count)
  22. {
  23. if (count <= 4) {
  24. return sizeof(struct f2fs_acl_header) +
  25. count * sizeof(struct f2fs_acl_entry_short);
  26. } else {
  27. return sizeof(struct f2fs_acl_header) +
  28. 4 * sizeof(struct f2fs_acl_entry_short) +
  29. (count - 4) * sizeof(struct f2fs_acl_entry);
  30. }
  31. }
  32. static inline int f2fs_acl_count(size_t size)
  33. {
  34. ssize_t s;
  35. size -= sizeof(struct f2fs_acl_header);
  36. s = size - 4 * sizeof(struct f2fs_acl_entry_short);
  37. if (s < 0) {
  38. if (size % sizeof(struct f2fs_acl_entry_short))
  39. return -1;
  40. return size / sizeof(struct f2fs_acl_entry_short);
  41. } else {
  42. if (s % sizeof(struct f2fs_acl_entry))
  43. return -1;
  44. return s / sizeof(struct f2fs_acl_entry) + 4;
  45. }
  46. }
  47. static struct posix_acl *f2fs_acl_from_disk(const char *value, size_t size)
  48. {
  49. int i, count;
  50. struct posix_acl *acl;
  51. struct f2fs_acl_header *hdr = (struct f2fs_acl_header *)value;
  52. struct f2fs_acl_entry *entry = (struct f2fs_acl_entry *)(hdr + 1);
  53. const char *end = value + size;
  54. if (hdr->a_version != cpu_to_le32(F2FS_ACL_VERSION))
  55. return ERR_PTR(-EINVAL);
  56. count = f2fs_acl_count(size);
  57. if (count < 0)
  58. return ERR_PTR(-EINVAL);
  59. if (count == 0)
  60. return NULL;
  61. acl = posix_acl_alloc(count, GFP_KERNEL);
  62. if (!acl)
  63. return ERR_PTR(-ENOMEM);
  64. for (i = 0; i < count; i++) {
  65. if ((char *)entry > end)
  66. goto fail;
  67. acl->a_entries[i].e_tag = le16_to_cpu(entry->e_tag);
  68. acl->a_entries[i].e_perm = le16_to_cpu(entry->e_perm);
  69. switch (acl->a_entries[i].e_tag) {
  70. case ACL_USER_OBJ:
  71. case ACL_GROUP_OBJ:
  72. case ACL_MASK:
  73. case ACL_OTHER:
  74. entry = (struct f2fs_acl_entry *)((char *)entry +
  75. sizeof(struct f2fs_acl_entry_short));
  76. break;
  77. case ACL_USER:
  78. acl->a_entries[i].e_uid =
  79. make_kuid(&init_user_ns,
  80. le32_to_cpu(entry->e_id));
  81. entry = (struct f2fs_acl_entry *)((char *)entry +
  82. sizeof(struct f2fs_acl_entry));
  83. break;
  84. case ACL_GROUP:
  85. acl->a_entries[i].e_gid =
  86. make_kgid(&init_user_ns,
  87. le32_to_cpu(entry->e_id));
  88. entry = (struct f2fs_acl_entry *)((char *)entry +
  89. sizeof(struct f2fs_acl_entry));
  90. break;
  91. default:
  92. goto fail;
  93. }
  94. }
  95. if ((char *)entry != end)
  96. goto fail;
  97. return acl;
  98. fail:
  99. posix_acl_release(acl);
  100. return ERR_PTR(-EINVAL);
  101. }
  102. static void *f2fs_acl_to_disk(const struct posix_acl *acl, size_t *size)
  103. {
  104. struct f2fs_acl_header *f2fs_acl;
  105. struct f2fs_acl_entry *entry;
  106. int i;
  107. f2fs_acl = kmalloc(sizeof(struct f2fs_acl_header) + acl->a_count *
  108. sizeof(struct f2fs_acl_entry), GFP_KERNEL);
  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. struct posix_acl *f2fs_get_acl(struct inode *inode, int type)
  149. {
  150. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  151. int name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  152. void *value = NULL;
  153. struct posix_acl *acl;
  154. int retval;
  155. if (!test_opt(sbi, POSIX_ACL))
  156. return NULL;
  157. acl = get_cached_acl(inode, type);
  158. if (acl != ACL_NOT_CACHED)
  159. return acl;
  160. if (type == ACL_TYPE_ACCESS)
  161. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  162. retval = f2fs_getxattr(inode, name_index, "", NULL, 0);
  163. if (retval > 0) {
  164. value = kmalloc(retval, GFP_KERNEL);
  165. if (!value)
  166. return ERR_PTR(-ENOMEM);
  167. retval = f2fs_getxattr(inode, name_index, "", value, retval);
  168. }
  169. if (retval > 0)
  170. acl = f2fs_acl_from_disk(value, retval);
  171. else if (retval == -ENODATA)
  172. acl = NULL;
  173. else
  174. acl = ERR_PTR(retval);
  175. kfree(value);
  176. if (!IS_ERR(acl))
  177. set_cached_acl(inode, type, acl);
  178. return acl;
  179. }
  180. static int f2fs_set_acl(struct inode *inode, int type,
  181. struct posix_acl *acl, struct page *ipage)
  182. {
  183. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  184. struct f2fs_inode_info *fi = F2FS_I(inode);
  185. int name_index;
  186. void *value = NULL;
  187. size_t size = 0;
  188. int error;
  189. if (!test_opt(sbi, POSIX_ACL))
  190. return 0;
  191. if (S_ISLNK(inode->i_mode))
  192. return -EOPNOTSUPP;
  193. switch (type) {
  194. case ACL_TYPE_ACCESS:
  195. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  196. if (acl) {
  197. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  198. if (error < 0)
  199. return error;
  200. set_acl_inode(fi, inode->i_mode);
  201. if (error == 0)
  202. acl = NULL;
  203. }
  204. break;
  205. case ACL_TYPE_DEFAULT:
  206. name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  207. if (!S_ISDIR(inode->i_mode))
  208. return acl ? -EACCES : 0;
  209. break;
  210. default:
  211. return -EINVAL;
  212. }
  213. if (acl) {
  214. value = f2fs_acl_to_disk(acl, &size);
  215. if (IS_ERR(value)) {
  216. cond_clear_inode_flag(fi, FI_ACL_MODE);
  217. return (int)PTR_ERR(value);
  218. }
  219. }
  220. error = f2fs_setxattr(inode, name_index, "", value, size, ipage);
  221. kfree(value);
  222. if (!error)
  223. set_cached_acl(inode, type, acl);
  224. cond_clear_inode_flag(fi, FI_ACL_MODE);
  225. return error;
  226. }
  227. int f2fs_init_acl(struct inode *inode, struct inode *dir, struct page *ipage)
  228. {
  229. struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
  230. struct posix_acl *acl = NULL;
  231. int error = 0;
  232. if (!S_ISLNK(inode->i_mode)) {
  233. if (test_opt(sbi, POSIX_ACL)) {
  234. acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT);
  235. if (IS_ERR(acl))
  236. return PTR_ERR(acl);
  237. }
  238. if (!acl)
  239. inode->i_mode &= ~current_umask();
  240. }
  241. if (!test_opt(sbi, POSIX_ACL) || !acl)
  242. goto cleanup;
  243. if (S_ISDIR(inode->i_mode)) {
  244. error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl, ipage);
  245. if (error)
  246. goto cleanup;
  247. }
  248. error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
  249. if (error < 0)
  250. return error;
  251. if (error > 0)
  252. error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, ipage);
  253. cleanup:
  254. posix_acl_release(acl);
  255. return error;
  256. }
  257. int f2fs_acl_chmod(struct inode *inode)
  258. {
  259. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  260. struct posix_acl *acl;
  261. int error;
  262. umode_t mode = get_inode_mode(inode);
  263. if (!test_opt(sbi, POSIX_ACL))
  264. return 0;
  265. if (S_ISLNK(mode))
  266. return -EOPNOTSUPP;
  267. acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
  268. if (IS_ERR(acl) || !acl)
  269. return PTR_ERR(acl);
  270. error = posix_acl_chmod(&acl, GFP_KERNEL, mode);
  271. if (error)
  272. return error;
  273. error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl, NULL);
  274. posix_acl_release(acl);
  275. return error;
  276. }
  277. static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
  278. size_t list_size, const char *name, size_t name_len, int type)
  279. {
  280. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  281. const char *xname = POSIX_ACL_XATTR_DEFAULT;
  282. size_t size;
  283. if (!test_opt(sbi, POSIX_ACL))
  284. return 0;
  285. if (type == ACL_TYPE_ACCESS)
  286. xname = POSIX_ACL_XATTR_ACCESS;
  287. size = strlen(xname) + 1;
  288. if (list && size <= list_size)
  289. memcpy(list, xname, size);
  290. return size;
  291. }
  292. static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
  293. void *buffer, size_t size, int type)
  294. {
  295. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  296. struct posix_acl *acl;
  297. int error;
  298. if (strcmp(name, "") != 0)
  299. return -EINVAL;
  300. if (!test_opt(sbi, POSIX_ACL))
  301. return -EOPNOTSUPP;
  302. acl = f2fs_get_acl(dentry->d_inode, type);
  303. if (IS_ERR(acl))
  304. return PTR_ERR(acl);
  305. if (!acl)
  306. return -ENODATA;
  307. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  308. posix_acl_release(acl);
  309. return error;
  310. }
  311. static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
  312. const void *value, size_t size, int flags, int type)
  313. {
  314. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  315. struct inode *inode = dentry->d_inode;
  316. struct posix_acl *acl = NULL;
  317. int error;
  318. if (strcmp(name, "") != 0)
  319. return -EINVAL;
  320. if (!test_opt(sbi, POSIX_ACL))
  321. return -EOPNOTSUPP;
  322. if (!inode_owner_or_capable(inode))
  323. return -EPERM;
  324. if (value) {
  325. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  326. if (IS_ERR(acl))
  327. return PTR_ERR(acl);
  328. if (acl) {
  329. error = posix_acl_valid(acl);
  330. if (error)
  331. goto release_and_out;
  332. }
  333. } else {
  334. acl = NULL;
  335. }
  336. error = f2fs_set_acl(inode, type, acl, NULL);
  337. release_and_out:
  338. posix_acl_release(acl);
  339. return error;
  340. }
  341. const struct xattr_handler f2fs_xattr_acl_default_handler = {
  342. .prefix = POSIX_ACL_XATTR_DEFAULT,
  343. .flags = ACL_TYPE_DEFAULT,
  344. .list = f2fs_xattr_list_acl,
  345. .get = f2fs_xattr_get_acl,
  346. .set = f2fs_xattr_set_acl,
  347. };
  348. const struct xattr_handler f2fs_xattr_acl_access_handler = {
  349. .prefix = POSIX_ACL_XATTR_ACCESS,
  350. .flags = ACL_TYPE_ACCESS,
  351. .list = f2fs_xattr_list_acl,
  352. .get = f2fs_xattr_get_acl,
  353. .set = f2fs_xattr_set_acl,
  354. };