acl.c 9.5 KB

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