acl.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  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/string.h>
  24. #define MLOG_MASK_PREFIX ML_INODE
  25. #include <cluster/masklog.h>
  26. #include "ocfs2.h"
  27. #include "alloc.h"
  28. #include "dlmglue.h"
  29. #include "file.h"
  30. #include "ocfs2_fs.h"
  31. #include "xattr.h"
  32. #include "acl.h"
  33. /*
  34. * Convert from xattr value to acl struct.
  35. */
  36. static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
  37. {
  38. int n, count;
  39. struct posix_acl *acl;
  40. if (!value)
  41. return NULL;
  42. if (size < sizeof(struct posix_acl_entry))
  43. return ERR_PTR(-EINVAL);
  44. count = size / sizeof(struct posix_acl_entry);
  45. if (count < 0)
  46. return ERR_PTR(-EINVAL);
  47. if (count == 0)
  48. return NULL;
  49. acl = posix_acl_alloc(count, GFP_NOFS);
  50. if (!acl)
  51. return ERR_PTR(-ENOMEM);
  52. for (n = 0; n < count; n++) {
  53. struct ocfs2_acl_entry *entry =
  54. (struct ocfs2_acl_entry *)value;
  55. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  56. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  57. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  58. value += sizeof(struct posix_acl_entry);
  59. }
  60. return acl;
  61. }
  62. /*
  63. * Convert acl struct to xattr value.
  64. */
  65. static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
  66. {
  67. struct ocfs2_acl_entry *entry = NULL;
  68. char *ocfs2_acl;
  69. size_t n;
  70. *size = acl->a_count * sizeof(struct posix_acl_entry);
  71. ocfs2_acl = kmalloc(*size, GFP_NOFS);
  72. if (!ocfs2_acl)
  73. return ERR_PTR(-ENOMEM);
  74. entry = (struct ocfs2_acl_entry *)ocfs2_acl;
  75. for (n = 0; n < acl->a_count; n++, entry++) {
  76. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  77. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  78. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  79. }
  80. return ocfs2_acl;
  81. }
  82. static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
  83. int type,
  84. struct buffer_head *di_bh)
  85. {
  86. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  87. int name_index;
  88. char *value = NULL;
  89. struct posix_acl *acl;
  90. int retval;
  91. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  92. return NULL;
  93. switch (type) {
  94. case ACL_TYPE_ACCESS:
  95. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  96. break;
  97. case ACL_TYPE_DEFAULT:
  98. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  99. break;
  100. default:
  101. return ERR_PTR(-EINVAL);
  102. }
  103. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
  104. if (retval > 0) {
  105. value = kmalloc(retval, GFP_NOFS);
  106. if (!value)
  107. return ERR_PTR(-ENOMEM);
  108. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  109. "", value, retval);
  110. }
  111. if (retval > 0)
  112. acl = ocfs2_acl_from_xattr(value, retval);
  113. else if (retval == -ENODATA || retval == 0)
  114. acl = NULL;
  115. else
  116. acl = ERR_PTR(retval);
  117. kfree(value);
  118. return acl;
  119. }
  120. /*
  121. * Get posix acl.
  122. */
  123. static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
  124. {
  125. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  126. struct buffer_head *di_bh = NULL;
  127. struct posix_acl *acl;
  128. int ret;
  129. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  130. return NULL;
  131. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  132. if (ret < 0) {
  133. mlog_errno(ret);
  134. acl = ERR_PTR(ret);
  135. return acl;
  136. }
  137. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  138. ocfs2_inode_unlock(inode, 0);
  139. brelse(di_bh);
  140. return acl;
  141. }
  142. /*
  143. * Set the access or default ACL of an inode.
  144. */
  145. static int ocfs2_set_acl(handle_t *handle,
  146. struct inode *inode,
  147. struct buffer_head *di_bh,
  148. int type,
  149. struct posix_acl *acl,
  150. struct ocfs2_alloc_context *meta_ac,
  151. struct ocfs2_alloc_context *data_ac)
  152. {
  153. int name_index;
  154. void *value = NULL;
  155. size_t size = 0;
  156. int ret;
  157. if (S_ISLNK(inode->i_mode))
  158. return -EOPNOTSUPP;
  159. switch (type) {
  160. case ACL_TYPE_ACCESS:
  161. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  162. if (acl) {
  163. mode_t mode = inode->i_mode;
  164. ret = posix_acl_equiv_mode(acl, &mode);
  165. if (ret < 0)
  166. return ret;
  167. else {
  168. inode->i_mode = mode;
  169. if (ret == 0)
  170. acl = NULL;
  171. }
  172. }
  173. break;
  174. case ACL_TYPE_DEFAULT:
  175. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  176. if (!S_ISDIR(inode->i_mode))
  177. return acl ? -EACCES : 0;
  178. break;
  179. default:
  180. return -EINVAL;
  181. }
  182. if (acl) {
  183. value = ocfs2_acl_to_xattr(acl, &size);
  184. if (IS_ERR(value))
  185. return (int)PTR_ERR(value);
  186. }
  187. if (handle)
  188. ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
  189. "", value, size, 0,
  190. meta_ac, data_ac);
  191. else
  192. ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
  193. kfree(value);
  194. return ret;
  195. }
  196. static size_t ocfs2_xattr_list_acl_access(struct inode *inode,
  197. char *list,
  198. size_t list_len,
  199. const char *name,
  200. size_t name_len)
  201. {
  202. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  203. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  204. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  205. return 0;
  206. if (list && size <= list_len)
  207. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  208. return size;
  209. }
  210. static size_t ocfs2_xattr_list_acl_default(struct inode *inode,
  211. char *list,
  212. size_t list_len,
  213. const char *name,
  214. size_t name_len)
  215. {
  216. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  217. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  218. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  219. return 0;
  220. if (list && size <= list_len)
  221. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  222. return size;
  223. }
  224. static int ocfs2_xattr_get_acl(struct inode *inode,
  225. int type,
  226. void *buffer,
  227. size_t size)
  228. {
  229. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  230. struct posix_acl *acl;
  231. int ret;
  232. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  233. return -EOPNOTSUPP;
  234. acl = ocfs2_get_acl(inode, type);
  235. if (IS_ERR(acl))
  236. return PTR_ERR(acl);
  237. if (acl == NULL)
  238. return -ENODATA;
  239. ret = posix_acl_to_xattr(acl, buffer, size);
  240. posix_acl_release(acl);
  241. return ret;
  242. }
  243. static int ocfs2_xattr_get_acl_access(struct inode *inode,
  244. const char *name,
  245. void *buffer,
  246. size_t size)
  247. {
  248. if (strcmp(name, "") != 0)
  249. return -EINVAL;
  250. return ocfs2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  251. }
  252. static int ocfs2_xattr_get_acl_default(struct inode *inode,
  253. const char *name,
  254. void *buffer,
  255. size_t size)
  256. {
  257. if (strcmp(name, "") != 0)
  258. return -EINVAL;
  259. return ocfs2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  260. }
  261. static int ocfs2_xattr_set_acl(struct inode *inode,
  262. int type,
  263. const void *value,
  264. size_t size)
  265. {
  266. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  267. struct posix_acl *acl;
  268. int ret = 0;
  269. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  270. return -EOPNOTSUPP;
  271. if (!is_owner_or_cap(inode))
  272. return -EPERM;
  273. if (value) {
  274. acl = posix_acl_from_xattr(value, size);
  275. if (IS_ERR(acl))
  276. return PTR_ERR(acl);
  277. else if (acl) {
  278. ret = posix_acl_valid(acl);
  279. if (ret)
  280. goto cleanup;
  281. }
  282. } else
  283. acl = NULL;
  284. ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
  285. cleanup:
  286. posix_acl_release(acl);
  287. return ret;
  288. }
  289. static int ocfs2_xattr_set_acl_access(struct inode *inode,
  290. const char *name,
  291. const void *value,
  292. size_t size,
  293. int flags)
  294. {
  295. if (strcmp(name, "") != 0)
  296. return -EINVAL;
  297. return ocfs2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  298. }
  299. static int ocfs2_xattr_set_acl_default(struct inode *inode,
  300. const char *name,
  301. const void *value,
  302. size_t size,
  303. int flags)
  304. {
  305. if (strcmp(name, "") != 0)
  306. return -EINVAL;
  307. return ocfs2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  308. }
  309. struct xattr_handler ocfs2_xattr_acl_access_handler = {
  310. .prefix = POSIX_ACL_XATTR_ACCESS,
  311. .list = ocfs2_xattr_list_acl_access,
  312. .get = ocfs2_xattr_get_acl_access,
  313. .set = ocfs2_xattr_set_acl_access,
  314. };
  315. struct xattr_handler ocfs2_xattr_acl_default_handler = {
  316. .prefix = POSIX_ACL_XATTR_DEFAULT,
  317. .list = ocfs2_xattr_list_acl_default,
  318. .get = ocfs2_xattr_get_acl_default,
  319. .set = ocfs2_xattr_set_acl_default,
  320. };