acl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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, struct posix_acl *acl)
  181. {
  182. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  183. struct f2fs_inode_info *fi = F2FS_I(inode);
  184. int name_index;
  185. void *value = NULL;
  186. size_t size = 0;
  187. int error;
  188. if (!test_opt(sbi, POSIX_ACL))
  189. return 0;
  190. if (S_ISLNK(inode->i_mode))
  191. return -EOPNOTSUPP;
  192. switch (type) {
  193. case ACL_TYPE_ACCESS:
  194. name_index = F2FS_XATTR_INDEX_POSIX_ACL_ACCESS;
  195. if (acl) {
  196. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  197. if (error < 0)
  198. return error;
  199. set_acl_inode(fi, inode->i_mode);
  200. if (error == 0)
  201. acl = NULL;
  202. }
  203. break;
  204. case ACL_TYPE_DEFAULT:
  205. name_index = F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT;
  206. if (!S_ISDIR(inode->i_mode))
  207. return acl ? -EACCES : 0;
  208. break;
  209. default:
  210. return -EINVAL;
  211. }
  212. if (acl) {
  213. value = f2fs_acl_to_disk(acl, &size);
  214. if (IS_ERR(value)) {
  215. cond_clear_inode_flag(fi, FI_ACL_MODE);
  216. return (int)PTR_ERR(value);
  217. }
  218. }
  219. error = f2fs_setxattr(inode, name_index, "", value, size);
  220. kfree(value);
  221. if (!error)
  222. set_cached_acl(inode, type, acl);
  223. cond_clear_inode_flag(fi, FI_ACL_MODE);
  224. return error;
  225. }
  226. int f2fs_init_acl(struct inode *inode, struct inode *dir)
  227. {
  228. struct posix_acl *acl = NULL;
  229. struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
  230. int error = 0;
  231. if (!S_ISLNK(inode->i_mode)) {
  232. if (test_opt(sbi, POSIX_ACL)) {
  233. acl = f2fs_get_acl(dir, ACL_TYPE_DEFAULT);
  234. if (IS_ERR(acl))
  235. return PTR_ERR(acl);
  236. }
  237. if (!acl)
  238. inode->i_mode &= ~current_umask();
  239. }
  240. if (test_opt(sbi, POSIX_ACL) && acl) {
  241. if (S_ISDIR(inode->i_mode)) {
  242. error = f2fs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  243. if (error)
  244. goto cleanup;
  245. }
  246. error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
  247. if (error < 0)
  248. return error;
  249. if (error > 0)
  250. error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
  251. }
  252. cleanup:
  253. posix_acl_release(acl);
  254. return error;
  255. }
  256. int f2fs_acl_chmod(struct inode *inode)
  257. {
  258. struct f2fs_sb_info *sbi = F2FS_SB(inode->i_sb);
  259. struct posix_acl *acl;
  260. int error;
  261. mode_t mode = get_inode_mode(inode);
  262. if (!test_opt(sbi, POSIX_ACL))
  263. return 0;
  264. if (S_ISLNK(mode))
  265. return -EOPNOTSUPP;
  266. acl = f2fs_get_acl(inode, ACL_TYPE_ACCESS);
  267. if (IS_ERR(acl) || !acl)
  268. return PTR_ERR(acl);
  269. error = posix_acl_chmod(&acl, GFP_KERNEL, mode);
  270. if (error)
  271. return error;
  272. error = f2fs_set_acl(inode, ACL_TYPE_ACCESS, acl);
  273. posix_acl_release(acl);
  274. return error;
  275. }
  276. static size_t f2fs_xattr_list_acl(struct dentry *dentry, char *list,
  277. size_t list_size, const char *name, size_t name_len, int type)
  278. {
  279. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  280. const char *xname = POSIX_ACL_XATTR_DEFAULT;
  281. size_t size;
  282. if (!test_opt(sbi, POSIX_ACL))
  283. return 0;
  284. if (type == ACL_TYPE_ACCESS)
  285. xname = POSIX_ACL_XATTR_ACCESS;
  286. size = strlen(xname) + 1;
  287. if (list && size <= list_size)
  288. memcpy(list, xname, size);
  289. return size;
  290. }
  291. static int f2fs_xattr_get_acl(struct dentry *dentry, const char *name,
  292. void *buffer, size_t size, int type)
  293. {
  294. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  295. struct posix_acl *acl;
  296. int error;
  297. if (strcmp(name, "") != 0)
  298. return -EINVAL;
  299. if (!test_opt(sbi, POSIX_ACL))
  300. return -EOPNOTSUPP;
  301. acl = f2fs_get_acl(dentry->d_inode, type);
  302. if (IS_ERR(acl))
  303. return PTR_ERR(acl);
  304. if (!acl)
  305. return -ENODATA;
  306. error = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
  307. posix_acl_release(acl);
  308. return error;
  309. }
  310. static int f2fs_xattr_set_acl(struct dentry *dentry, const char *name,
  311. const void *value, size_t size, int flags, int type)
  312. {
  313. struct f2fs_sb_info *sbi = F2FS_SB(dentry->d_sb);
  314. struct inode *inode = dentry->d_inode;
  315. struct posix_acl *acl = NULL;
  316. int error;
  317. if (strcmp(name, "") != 0)
  318. return -EINVAL;
  319. if (!test_opt(sbi, POSIX_ACL))
  320. return -EOPNOTSUPP;
  321. if (!inode_owner_or_capable(inode))
  322. return -EPERM;
  323. if (value) {
  324. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  325. if (IS_ERR(acl))
  326. return PTR_ERR(acl);
  327. if (acl) {
  328. error = posix_acl_valid(acl);
  329. if (error)
  330. goto release_and_out;
  331. }
  332. } else {
  333. acl = NULL;
  334. }
  335. error = f2fs_set_acl(inode, type, acl);
  336. release_and_out:
  337. posix_acl_release(acl);
  338. return error;
  339. }
  340. const struct xattr_handler f2fs_xattr_acl_default_handler = {
  341. .prefix = POSIX_ACL_XATTR_DEFAULT,
  342. .flags = ACL_TYPE_DEFAULT,
  343. .list = f2fs_xattr_list_acl,
  344. .get = f2fs_xattr_get_acl,
  345. .set = f2fs_xattr_set_acl,
  346. };
  347. const struct xattr_handler f2fs_xattr_acl_access_handler = {
  348. .prefix = POSIX_ACL_XATTR_ACCESS,
  349. .flags = ACL_TYPE_ACCESS,
  350. .list = f2fs_xattr_list_acl,
  351. .get = f2fs_xattr_get_acl,
  352. .set = f2fs_xattr_set_acl,
  353. };