acl.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* -*- mode: c; c-basic-offset: 8; -*-
  3. * vim: noexpandtab sw=8 ts=8 sts=0:
  4. *
  5. * acl.c
  6. *
  7. * Copyright (C) 2004, 2008 Oracle. All rights reserved.
  8. *
  9. * CREDITS:
  10. * Lots of code in this file is copy from linux/fs/ext3/acl.c.
  11. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  12. */
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/slab.h>
  16. #include <linux/string.h>
  17. #include <cluster/masklog.h>
  18. #include "ocfs2.h"
  19. #include "alloc.h"
  20. #include "dlmglue.h"
  21. #include "file.h"
  22. #include "inode.h"
  23. #include "journal.h"
  24. #include "ocfs2_fs.h"
  25. #include "xattr.h"
  26. #include "acl.h"
  27. /*
  28. * Convert from xattr value to acl struct.
  29. */
  30. static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
  31. {
  32. int n, count;
  33. struct posix_acl *acl;
  34. if (!value)
  35. return NULL;
  36. if (size < sizeof(struct posix_acl_entry))
  37. return ERR_PTR(-EINVAL);
  38. count = size / sizeof(struct posix_acl_entry);
  39. acl = posix_acl_alloc(count, GFP_NOFS);
  40. if (!acl)
  41. return ERR_PTR(-ENOMEM);
  42. for (n = 0; n < count; n++) {
  43. struct ocfs2_acl_entry *entry =
  44. (struct ocfs2_acl_entry *)value;
  45. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  46. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  47. switch(acl->a_entries[n].e_tag) {
  48. case ACL_USER:
  49. acl->a_entries[n].e_uid =
  50. make_kuid(&init_user_ns,
  51. le32_to_cpu(entry->e_id));
  52. break;
  53. case ACL_GROUP:
  54. acl->a_entries[n].e_gid =
  55. make_kgid(&init_user_ns,
  56. le32_to_cpu(entry->e_id));
  57. break;
  58. default:
  59. break;
  60. }
  61. value += sizeof(struct posix_acl_entry);
  62. }
  63. return acl;
  64. }
  65. /*
  66. * Convert acl struct to xattr value.
  67. */
  68. static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
  69. {
  70. struct ocfs2_acl_entry *entry = NULL;
  71. char *ocfs2_acl;
  72. size_t n;
  73. *size = acl->a_count * sizeof(struct posix_acl_entry);
  74. ocfs2_acl = kmalloc(*size, GFP_NOFS);
  75. if (!ocfs2_acl)
  76. return ERR_PTR(-ENOMEM);
  77. entry = (struct ocfs2_acl_entry *)ocfs2_acl;
  78. for (n = 0; n < acl->a_count; n++, entry++) {
  79. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  80. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  81. switch(acl->a_entries[n].e_tag) {
  82. case ACL_USER:
  83. entry->e_id = cpu_to_le32(
  84. from_kuid(&init_user_ns,
  85. acl->a_entries[n].e_uid));
  86. break;
  87. case ACL_GROUP:
  88. entry->e_id = cpu_to_le32(
  89. from_kgid(&init_user_ns,
  90. acl->a_entries[n].e_gid));
  91. break;
  92. default:
  93. entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  94. break;
  95. }
  96. }
  97. return ocfs2_acl;
  98. }
  99. static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
  100. int type,
  101. struct buffer_head *di_bh)
  102. {
  103. int name_index;
  104. char *value = NULL;
  105. struct posix_acl *acl;
  106. int retval;
  107. switch (type) {
  108. case ACL_TYPE_ACCESS:
  109. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  110. break;
  111. case ACL_TYPE_DEFAULT:
  112. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  113. break;
  114. default:
  115. return ERR_PTR(-EINVAL);
  116. }
  117. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
  118. if (retval > 0) {
  119. value = kmalloc(retval, GFP_NOFS);
  120. if (!value)
  121. return ERR_PTR(-ENOMEM);
  122. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  123. "", value, retval);
  124. }
  125. if (retval > 0)
  126. acl = ocfs2_acl_from_xattr(value, retval);
  127. else if (retval == -ENODATA || retval == 0)
  128. acl = NULL;
  129. else
  130. acl = ERR_PTR(retval);
  131. kfree(value);
  132. return acl;
  133. }
  134. /*
  135. * Helper function to set i_mode in memory and disk. Some call paths
  136. * will not have di_bh or a journal handle to pass, in which case it
  137. * will create it's own.
  138. */
  139. static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
  140. handle_t *handle, umode_t new_mode)
  141. {
  142. int ret, commit_handle = 0;
  143. struct ocfs2_dinode *di;
  144. if (di_bh == NULL) {
  145. ret = ocfs2_read_inode_block(inode, &di_bh);
  146. if (ret) {
  147. mlog_errno(ret);
  148. goto out;
  149. }
  150. } else
  151. get_bh(di_bh);
  152. if (handle == NULL) {
  153. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
  154. OCFS2_INODE_UPDATE_CREDITS);
  155. if (IS_ERR(handle)) {
  156. ret = PTR_ERR(handle);
  157. mlog_errno(ret);
  158. goto out_brelse;
  159. }
  160. commit_handle = 1;
  161. }
  162. di = (struct ocfs2_dinode *)di_bh->b_data;
  163. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  164. OCFS2_JOURNAL_ACCESS_WRITE);
  165. if (ret) {
  166. mlog_errno(ret);
  167. goto out_commit;
  168. }
  169. inode->i_mode = new_mode;
  170. inode->i_ctime = current_time(inode);
  171. di->i_mode = cpu_to_le16(inode->i_mode);
  172. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  173. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  174. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  175. ocfs2_journal_dirty(handle, di_bh);
  176. out_commit:
  177. if (commit_handle)
  178. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  179. out_brelse:
  180. brelse(di_bh);
  181. out:
  182. return ret;
  183. }
  184. /*
  185. * Set the access or default ACL of an inode.
  186. */
  187. static int ocfs2_set_acl(handle_t *handle,
  188. struct inode *inode,
  189. struct buffer_head *di_bh,
  190. int type,
  191. struct posix_acl *acl,
  192. struct ocfs2_alloc_context *meta_ac,
  193. struct ocfs2_alloc_context *data_ac)
  194. {
  195. int name_index;
  196. void *value = NULL;
  197. size_t size = 0;
  198. int ret;
  199. if (S_ISLNK(inode->i_mode))
  200. return -EOPNOTSUPP;
  201. switch (type) {
  202. case ACL_TYPE_ACCESS:
  203. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  204. break;
  205. case ACL_TYPE_DEFAULT:
  206. name_index = OCFS2_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 = ocfs2_acl_to_xattr(acl, &size);
  215. if (IS_ERR(value))
  216. return (int)PTR_ERR(value);
  217. }
  218. if (handle)
  219. ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
  220. "", value, size, 0,
  221. meta_ac, data_ac);
  222. else
  223. ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
  224. kfree(value);
  225. if (!ret)
  226. set_cached_acl(inode, type, acl);
  227. return ret;
  228. }
  229. int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  230. {
  231. struct buffer_head *bh = NULL;
  232. int status, had_lock;
  233. struct ocfs2_lock_holder oh;
  234. had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
  235. if (had_lock < 0)
  236. return had_lock;
  237. if (type == ACL_TYPE_ACCESS && acl) {
  238. umode_t mode;
  239. status = posix_acl_update_mode(inode, &mode, &acl);
  240. if (status)
  241. goto unlock;
  242. status = ocfs2_acl_set_mode(inode, bh, NULL, mode);
  243. if (status)
  244. goto unlock;
  245. }
  246. status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
  247. unlock:
  248. ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
  249. brelse(bh);
  250. return status;
  251. }
  252. struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
  253. {
  254. struct ocfs2_super *osb;
  255. struct buffer_head *di_bh = NULL;
  256. struct posix_acl *acl;
  257. int had_lock;
  258. struct ocfs2_lock_holder oh;
  259. osb = OCFS2_SB(inode->i_sb);
  260. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  261. return NULL;
  262. had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
  263. if (had_lock < 0)
  264. return ERR_PTR(had_lock);
  265. down_read(&OCFS2_I(inode)->ip_xattr_sem);
  266. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  267. up_read(&OCFS2_I(inode)->ip_xattr_sem);
  268. ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
  269. brelse(di_bh);
  270. return acl;
  271. }
  272. int ocfs2_acl_chmod(struct inode *inode, struct buffer_head *bh)
  273. {
  274. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  275. struct posix_acl *acl;
  276. int ret;
  277. if (S_ISLNK(inode->i_mode))
  278. return -EOPNOTSUPP;
  279. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  280. return 0;
  281. down_read(&OCFS2_I(inode)->ip_xattr_sem);
  282. acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, bh);
  283. up_read(&OCFS2_I(inode)->ip_xattr_sem);
  284. if (IS_ERR_OR_NULL(acl))
  285. return PTR_ERR_OR_ZERO(acl);
  286. ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  287. if (ret)
  288. return ret;
  289. ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
  290. acl, NULL, NULL);
  291. posix_acl_release(acl);
  292. return ret;
  293. }
  294. /*
  295. * Initialize the ACLs of a new inode. If parent directory has default ACL,
  296. * then clone to new inode. Called from ocfs2_mknod.
  297. */
  298. int ocfs2_init_acl(handle_t *handle,
  299. struct inode *inode,
  300. struct inode *dir,
  301. struct buffer_head *di_bh,
  302. struct buffer_head *dir_bh,
  303. struct ocfs2_alloc_context *meta_ac,
  304. struct ocfs2_alloc_context *data_ac)
  305. {
  306. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  307. struct posix_acl *acl = NULL;
  308. int ret = 0, ret2;
  309. umode_t mode;
  310. if (!S_ISLNK(inode->i_mode)) {
  311. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  312. down_read(&OCFS2_I(dir)->ip_xattr_sem);
  313. acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
  314. dir_bh);
  315. up_read(&OCFS2_I(dir)->ip_xattr_sem);
  316. if (IS_ERR(acl))
  317. return PTR_ERR(acl);
  318. }
  319. if (!acl) {
  320. mode = inode->i_mode & ~current_umask();
  321. ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  322. if (ret) {
  323. mlog_errno(ret);
  324. goto cleanup;
  325. }
  326. }
  327. }
  328. if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
  329. if (S_ISDIR(inode->i_mode)) {
  330. ret = ocfs2_set_acl(handle, inode, di_bh,
  331. ACL_TYPE_DEFAULT, acl,
  332. meta_ac, data_ac);
  333. if (ret)
  334. goto cleanup;
  335. }
  336. mode = inode->i_mode;
  337. ret = __posix_acl_create(&acl, GFP_NOFS, &mode);
  338. if (ret < 0)
  339. return ret;
  340. ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  341. if (ret2) {
  342. mlog_errno(ret2);
  343. ret = ret2;
  344. goto cleanup;
  345. }
  346. if (ret > 0) {
  347. ret = ocfs2_set_acl(handle, inode,
  348. di_bh, ACL_TYPE_ACCESS,
  349. acl, meta_ac, data_ac);
  350. }
  351. }
  352. cleanup:
  353. posix_acl_release(acl);
  354. return ret;
  355. }