acl.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. int name_index;
  87. char *value = NULL;
  88. struct posix_acl *acl;
  89. int retval;
  90. switch (type) {
  91. case ACL_TYPE_ACCESS:
  92. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  93. break;
  94. case ACL_TYPE_DEFAULT:
  95. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  96. break;
  97. default:
  98. return ERR_PTR(-EINVAL);
  99. }
  100. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
  101. if (retval > 0) {
  102. value = kmalloc(retval, GFP_NOFS);
  103. if (!value)
  104. return ERR_PTR(-ENOMEM);
  105. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  106. "", value, retval);
  107. }
  108. if (retval > 0)
  109. acl = ocfs2_acl_from_xattr(value, retval);
  110. else if (retval == -ENODATA || retval == 0)
  111. acl = NULL;
  112. else
  113. acl = ERR_PTR(retval);
  114. kfree(value);
  115. return acl;
  116. }
  117. /*
  118. * Get posix acl.
  119. */
  120. static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
  121. {
  122. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  123. struct buffer_head *di_bh = NULL;
  124. struct posix_acl *acl;
  125. int ret;
  126. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  127. return NULL;
  128. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  129. if (ret < 0) {
  130. mlog_errno(ret);
  131. acl = ERR_PTR(ret);
  132. return acl;
  133. }
  134. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  135. ocfs2_inode_unlock(inode, 0);
  136. brelse(di_bh);
  137. return acl;
  138. }
  139. /*
  140. * Set the access or default ACL of an inode.
  141. */
  142. static int ocfs2_set_acl(handle_t *handle,
  143. struct inode *inode,
  144. struct buffer_head *di_bh,
  145. int type,
  146. struct posix_acl *acl,
  147. struct ocfs2_alloc_context *meta_ac,
  148. struct ocfs2_alloc_context *data_ac)
  149. {
  150. int name_index;
  151. void *value = NULL;
  152. size_t size = 0;
  153. int ret;
  154. if (S_ISLNK(inode->i_mode))
  155. return -EOPNOTSUPP;
  156. switch (type) {
  157. case ACL_TYPE_ACCESS:
  158. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  159. if (acl) {
  160. mode_t mode = inode->i_mode;
  161. ret = posix_acl_equiv_mode(acl, &mode);
  162. if (ret < 0)
  163. return ret;
  164. else {
  165. inode->i_mode = mode;
  166. if (ret == 0)
  167. acl = NULL;
  168. }
  169. }
  170. break;
  171. case ACL_TYPE_DEFAULT:
  172. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  173. if (!S_ISDIR(inode->i_mode))
  174. return acl ? -EACCES : 0;
  175. break;
  176. default:
  177. return -EINVAL;
  178. }
  179. if (acl) {
  180. value = ocfs2_acl_to_xattr(acl, &size);
  181. if (IS_ERR(value))
  182. return (int)PTR_ERR(value);
  183. }
  184. if (handle)
  185. ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
  186. "", value, size, 0,
  187. meta_ac, data_ac);
  188. else
  189. ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
  190. kfree(value);
  191. return ret;
  192. }
  193. int ocfs2_check_acl(struct inode *inode, int mask)
  194. {
  195. struct posix_acl *acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
  196. if (IS_ERR(acl))
  197. return PTR_ERR(acl);
  198. if (acl) {
  199. int ret = posix_acl_permission(inode, acl, mask);
  200. posix_acl_release(acl);
  201. return ret;
  202. }
  203. return -EAGAIN;
  204. }
  205. int ocfs2_acl_chmod(struct inode *inode)
  206. {
  207. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  208. struct posix_acl *acl, *clone;
  209. int ret;
  210. if (S_ISLNK(inode->i_mode))
  211. return -EOPNOTSUPP;
  212. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  213. return 0;
  214. acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
  215. if (IS_ERR(acl) || !acl)
  216. return PTR_ERR(acl);
  217. clone = posix_acl_clone(acl, GFP_KERNEL);
  218. posix_acl_release(acl);
  219. if (!clone)
  220. return -ENOMEM;
  221. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  222. if (!ret)
  223. ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
  224. clone, NULL, NULL);
  225. posix_acl_release(clone);
  226. return ret;
  227. }
  228. /*
  229. * Initialize the ACLs of a new inode. If parent directory has default ACL,
  230. * then clone to new inode. Called from ocfs2_mknod.
  231. */
  232. int ocfs2_init_acl(handle_t *handle,
  233. struct inode *inode,
  234. struct inode *dir,
  235. struct buffer_head *di_bh,
  236. struct buffer_head *dir_bh,
  237. struct ocfs2_alloc_context *meta_ac,
  238. struct ocfs2_alloc_context *data_ac)
  239. {
  240. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  241. struct posix_acl *acl = NULL;
  242. int ret = 0;
  243. if (!S_ISLNK(inode->i_mode)) {
  244. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  245. acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
  246. dir_bh);
  247. if (IS_ERR(acl))
  248. return PTR_ERR(acl);
  249. }
  250. if (!acl)
  251. inode->i_mode &= ~current_umask();
  252. }
  253. if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
  254. struct posix_acl *clone;
  255. mode_t mode;
  256. if (S_ISDIR(inode->i_mode)) {
  257. ret = ocfs2_set_acl(handle, inode, di_bh,
  258. ACL_TYPE_DEFAULT, acl,
  259. meta_ac, data_ac);
  260. if (ret)
  261. goto cleanup;
  262. }
  263. clone = posix_acl_clone(acl, GFP_NOFS);
  264. ret = -ENOMEM;
  265. if (!clone)
  266. goto cleanup;
  267. mode = inode->i_mode;
  268. ret = posix_acl_create_masq(clone, &mode);
  269. if (ret >= 0) {
  270. inode->i_mode = mode;
  271. if (ret > 0) {
  272. ret = ocfs2_set_acl(handle, inode,
  273. di_bh, ACL_TYPE_ACCESS,
  274. clone, meta_ac, data_ac);
  275. }
  276. }
  277. posix_acl_release(clone);
  278. }
  279. cleanup:
  280. posix_acl_release(acl);
  281. return ret;
  282. }
  283. static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
  284. char *list,
  285. size_t list_len,
  286. const char *name,
  287. size_t name_len,
  288. int type)
  289. {
  290. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  291. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  292. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  293. return 0;
  294. if (list && size <= list_len)
  295. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  296. return size;
  297. }
  298. static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
  299. char *list,
  300. size_t list_len,
  301. const char *name,
  302. size_t name_len,
  303. int type)
  304. {
  305. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  306. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  307. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  308. return 0;
  309. if (list && size <= list_len)
  310. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  311. return size;
  312. }
  313. static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
  314. void *buffer, size_t size, int type)
  315. {
  316. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  317. struct posix_acl *acl;
  318. int ret;
  319. if (strcmp(name, "") != 0)
  320. return -EINVAL;
  321. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  322. return -EOPNOTSUPP;
  323. acl = ocfs2_get_acl(dentry->d_inode, type);
  324. if (IS_ERR(acl))
  325. return PTR_ERR(acl);
  326. if (acl == NULL)
  327. return -ENODATA;
  328. ret = posix_acl_to_xattr(acl, buffer, size);
  329. posix_acl_release(acl);
  330. return ret;
  331. }
  332. static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
  333. const void *value, size_t size, int flags, int type)
  334. {
  335. struct inode *inode = dentry->d_inode;
  336. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  337. struct posix_acl *acl;
  338. int ret = 0;
  339. if (strcmp(name, "") != 0)
  340. return -EINVAL;
  341. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  342. return -EOPNOTSUPP;
  343. if (!is_owner_or_cap(inode))
  344. return -EPERM;
  345. if (value) {
  346. acl = posix_acl_from_xattr(value, size);
  347. if (IS_ERR(acl))
  348. return PTR_ERR(acl);
  349. else if (acl) {
  350. ret = posix_acl_valid(acl);
  351. if (ret)
  352. goto cleanup;
  353. }
  354. } else
  355. acl = NULL;
  356. ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
  357. cleanup:
  358. posix_acl_release(acl);
  359. return ret;
  360. }
  361. struct xattr_handler ocfs2_xattr_acl_access_handler = {
  362. .prefix = POSIX_ACL_XATTR_ACCESS,
  363. .flags = ACL_TYPE_ACCESS,
  364. .list = ocfs2_xattr_list_acl_access,
  365. .get = ocfs2_xattr_get_acl,
  366. .set = ocfs2_xattr_set_acl,
  367. };
  368. struct xattr_handler ocfs2_xattr_acl_default_handler = {
  369. .prefix = POSIX_ACL_XATTR_DEFAULT,
  370. .flags = ACL_TYPE_DEFAULT,
  371. .list = ocfs2_xattr_list_acl_default,
  372. .get = ocfs2_xattr_get_acl,
  373. .set = ocfs2_xattr_set_acl,
  374. };