acl.c 9.2 KB

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