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