acl.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. /* -*- mode: c; c-basic-offset: 8; -*-
  2. * vim: noexpandtab sw=8 ts=8 sts=0:
  3. *
  4. * acl.c
  5. *
  6. * Copyright (C) 2004, 2008 Oracle. All rights reserved.
  7. *
  8. * CREDITS:
  9. * Lots of code in this file is copy from linux/fs/ext3/acl.c.
  10. * Copyright (C) 2001-2003 Andreas Gruenbacher, <agruen@suse.de>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public
  14. * License version 2 as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/slab.h>
  24. #include <linux/string.h>
  25. #include <cluster/masklog.h>
  26. #include "ocfs2.h"
  27. #include "alloc.h"
  28. #include "dlmglue.h"
  29. #include "file.h"
  30. #include "inode.h"
  31. #include "journal.h"
  32. #include "ocfs2_fs.h"
  33. #include "xattr.h"
  34. #include "acl.h"
  35. /*
  36. * Convert from xattr value to acl struct.
  37. */
  38. static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
  39. {
  40. int n, count;
  41. struct posix_acl *acl;
  42. if (!value)
  43. return NULL;
  44. if (size < sizeof(struct posix_acl_entry))
  45. return ERR_PTR(-EINVAL);
  46. count = size / sizeof(struct posix_acl_entry);
  47. acl = posix_acl_alloc(count, GFP_NOFS);
  48. if (!acl)
  49. return ERR_PTR(-ENOMEM);
  50. for (n = 0; n < count; n++) {
  51. struct ocfs2_acl_entry *entry =
  52. (struct ocfs2_acl_entry *)value;
  53. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  54. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  55. switch(acl->a_entries[n].e_tag) {
  56. case ACL_USER:
  57. acl->a_entries[n].e_uid =
  58. make_kuid(&init_user_ns,
  59. le32_to_cpu(entry->e_id));
  60. break;
  61. case ACL_GROUP:
  62. acl->a_entries[n].e_gid =
  63. make_kgid(&init_user_ns,
  64. le32_to_cpu(entry->e_id));
  65. break;
  66. default:
  67. break;
  68. }
  69. value += sizeof(struct posix_acl_entry);
  70. }
  71. return acl;
  72. }
  73. /*
  74. * Convert acl struct to xattr value.
  75. */
  76. static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
  77. {
  78. struct ocfs2_acl_entry *entry = NULL;
  79. char *ocfs2_acl;
  80. size_t n;
  81. *size = acl->a_count * sizeof(struct posix_acl_entry);
  82. ocfs2_acl = kmalloc(*size, GFP_NOFS);
  83. if (!ocfs2_acl)
  84. return ERR_PTR(-ENOMEM);
  85. entry = (struct ocfs2_acl_entry *)ocfs2_acl;
  86. for (n = 0; n < acl->a_count; n++, entry++) {
  87. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  88. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  89. switch(acl->a_entries[n].e_tag) {
  90. case ACL_USER:
  91. entry->e_id = cpu_to_le32(
  92. from_kuid(&init_user_ns,
  93. acl->a_entries[n].e_uid));
  94. break;
  95. case ACL_GROUP:
  96. entry->e_id = cpu_to_le32(
  97. from_kgid(&init_user_ns,
  98. acl->a_entries[n].e_gid));
  99. break;
  100. default:
  101. entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  102. break;
  103. }
  104. }
  105. return ocfs2_acl;
  106. }
  107. static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
  108. int type,
  109. struct buffer_head *di_bh)
  110. {
  111. int name_index;
  112. char *value = NULL;
  113. struct posix_acl *acl;
  114. int retval;
  115. switch (type) {
  116. case ACL_TYPE_ACCESS:
  117. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  118. break;
  119. case ACL_TYPE_DEFAULT:
  120. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  121. break;
  122. default:
  123. return ERR_PTR(-EINVAL);
  124. }
  125. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
  126. if (retval > 0) {
  127. value = kmalloc(retval, GFP_NOFS);
  128. if (!value)
  129. return ERR_PTR(-ENOMEM);
  130. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  131. "", value, retval);
  132. }
  133. if (retval > 0)
  134. acl = ocfs2_acl_from_xattr(value, retval);
  135. else if (retval == -ENODATA || retval == 0)
  136. acl = NULL;
  137. else
  138. acl = ERR_PTR(retval);
  139. kfree(value);
  140. return acl;
  141. }
  142. /*
  143. * Helper function to set i_mode in memory and disk. Some call paths
  144. * will not have di_bh or a journal handle to pass, in which case it
  145. * will create it's own.
  146. */
  147. static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
  148. handle_t *handle, umode_t new_mode)
  149. {
  150. int ret, commit_handle = 0;
  151. struct ocfs2_dinode *di;
  152. if (di_bh == NULL) {
  153. ret = ocfs2_read_inode_block(inode, &di_bh);
  154. if (ret) {
  155. mlog_errno(ret);
  156. goto out;
  157. }
  158. } else
  159. get_bh(di_bh);
  160. if (handle == NULL) {
  161. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
  162. OCFS2_INODE_UPDATE_CREDITS);
  163. if (IS_ERR(handle)) {
  164. ret = PTR_ERR(handle);
  165. mlog_errno(ret);
  166. goto out_brelse;
  167. }
  168. commit_handle = 1;
  169. }
  170. di = (struct ocfs2_dinode *)di_bh->b_data;
  171. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  172. OCFS2_JOURNAL_ACCESS_WRITE);
  173. if (ret) {
  174. mlog_errno(ret);
  175. goto out_commit;
  176. }
  177. inode->i_mode = new_mode;
  178. inode->i_ctime = CURRENT_TIME;
  179. di->i_mode = cpu_to_le16(inode->i_mode);
  180. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  181. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  182. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  183. ocfs2_journal_dirty(handle, di_bh);
  184. out_commit:
  185. if (commit_handle)
  186. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  187. out_brelse:
  188. brelse(di_bh);
  189. out:
  190. return ret;
  191. }
  192. /*
  193. * Set the access or default ACL of an inode.
  194. */
  195. int ocfs2_set_acl(handle_t *handle,
  196. struct inode *inode,
  197. struct buffer_head *di_bh,
  198. int type,
  199. struct posix_acl *acl,
  200. struct ocfs2_alloc_context *meta_ac,
  201. struct ocfs2_alloc_context *data_ac)
  202. {
  203. int name_index;
  204. void *value = NULL;
  205. size_t size = 0;
  206. int ret;
  207. if (S_ISLNK(inode->i_mode))
  208. return -EOPNOTSUPP;
  209. switch (type) {
  210. case ACL_TYPE_ACCESS:
  211. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  212. if (acl) {
  213. umode_t mode = inode->i_mode;
  214. ret = posix_acl_equiv_mode(acl, &mode);
  215. if (ret < 0)
  216. return ret;
  217. if (ret == 0)
  218. acl = NULL;
  219. ret = ocfs2_acl_set_mode(inode, di_bh,
  220. handle, mode);
  221. if (ret)
  222. return ret;
  223. }
  224. break;
  225. case ACL_TYPE_DEFAULT:
  226. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  227. if (!S_ISDIR(inode->i_mode))
  228. return acl ? -EACCES : 0;
  229. break;
  230. default:
  231. return -EINVAL;
  232. }
  233. if (acl) {
  234. value = ocfs2_acl_to_xattr(acl, &size);
  235. if (IS_ERR(value))
  236. return (int)PTR_ERR(value);
  237. }
  238. if (handle)
  239. ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
  240. "", value, size, 0,
  241. meta_ac, data_ac);
  242. else
  243. ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
  244. kfree(value);
  245. return ret;
  246. }
  247. int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  248. {
  249. return ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
  250. }
  251. struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
  252. {
  253. struct ocfs2_super *osb;
  254. struct buffer_head *di_bh = NULL;
  255. struct posix_acl *acl;
  256. int ret = -EAGAIN;
  257. osb = OCFS2_SB(inode->i_sb);
  258. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  259. return NULL;
  260. ret = ocfs2_read_inode_block(inode, &di_bh);
  261. if (ret < 0)
  262. return ERR_PTR(ret);
  263. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  264. brelse(di_bh);
  265. return acl;
  266. }