acl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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. int ocfs2_check_acl(struct inode *inode, int mask)
  197. {
  198. struct posix_acl *acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
  199. if (IS_ERR(acl))
  200. return PTR_ERR(acl);
  201. if (acl) {
  202. int ret = posix_acl_permission(inode, acl, mask);
  203. posix_acl_release(acl);
  204. return ret;
  205. }
  206. return -EAGAIN;
  207. }
  208. int ocfs2_acl_chmod(struct inode *inode)
  209. {
  210. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  211. struct posix_acl *acl, *clone;
  212. int ret;
  213. if (S_ISLNK(inode->i_mode))
  214. return -EOPNOTSUPP;
  215. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  216. return 0;
  217. acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
  218. if (IS_ERR(acl) || !acl)
  219. return PTR_ERR(acl);
  220. clone = posix_acl_clone(acl, GFP_KERNEL);
  221. posix_acl_release(acl);
  222. if (!clone)
  223. return -ENOMEM;
  224. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  225. if (!ret)
  226. ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
  227. clone, NULL, NULL);
  228. posix_acl_release(clone);
  229. return ret;
  230. }
  231. /*
  232. * Initialize the ACLs of a new inode. If parent directory has default ACL,
  233. * then clone to new inode. Called from ocfs2_mknod.
  234. */
  235. int ocfs2_init_acl(handle_t *handle,
  236. struct inode *inode,
  237. struct inode *dir,
  238. struct buffer_head *di_bh,
  239. struct buffer_head *dir_bh,
  240. struct ocfs2_alloc_context *meta_ac,
  241. struct ocfs2_alloc_context *data_ac)
  242. {
  243. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  244. struct posix_acl *acl = NULL;
  245. int ret = 0;
  246. if (!S_ISLNK(inode->i_mode)) {
  247. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  248. acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
  249. dir_bh);
  250. if (IS_ERR(acl))
  251. return PTR_ERR(acl);
  252. }
  253. if (!acl)
  254. inode->i_mode &= ~current_umask();
  255. }
  256. if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
  257. struct posix_acl *clone;
  258. mode_t mode;
  259. if (S_ISDIR(inode->i_mode)) {
  260. ret = ocfs2_set_acl(handle, inode, di_bh,
  261. ACL_TYPE_DEFAULT, acl,
  262. meta_ac, data_ac);
  263. if (ret)
  264. goto cleanup;
  265. }
  266. clone = posix_acl_clone(acl, GFP_NOFS);
  267. ret = -ENOMEM;
  268. if (!clone)
  269. goto cleanup;
  270. mode = inode->i_mode;
  271. ret = posix_acl_create_masq(clone, &mode);
  272. if (ret >= 0) {
  273. inode->i_mode = mode;
  274. if (ret > 0) {
  275. ret = ocfs2_set_acl(handle, inode,
  276. di_bh, ACL_TYPE_ACCESS,
  277. clone, meta_ac, data_ac);
  278. }
  279. }
  280. posix_acl_release(clone);
  281. }
  282. cleanup:
  283. posix_acl_release(acl);
  284. return ret;
  285. }
  286. static size_t ocfs2_xattr_list_acl_access(struct inode *inode,
  287. char *list,
  288. size_t list_len,
  289. const char *name,
  290. size_t name_len)
  291. {
  292. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  293. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  294. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  295. return 0;
  296. if (list && size <= list_len)
  297. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  298. return size;
  299. }
  300. static size_t ocfs2_xattr_list_acl_default(struct inode *inode,
  301. char *list,
  302. size_t list_len,
  303. const char *name,
  304. size_t name_len)
  305. {
  306. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  307. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  308. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  309. return 0;
  310. if (list && size <= list_len)
  311. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  312. return size;
  313. }
  314. static int ocfs2_xattr_get_acl(struct inode *inode,
  315. int type,
  316. void *buffer,
  317. size_t size)
  318. {
  319. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  320. struct posix_acl *acl;
  321. int ret;
  322. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  323. return -EOPNOTSUPP;
  324. acl = ocfs2_get_acl(inode, type);
  325. if (IS_ERR(acl))
  326. return PTR_ERR(acl);
  327. if (acl == NULL)
  328. return -ENODATA;
  329. ret = posix_acl_to_xattr(acl, buffer, size);
  330. posix_acl_release(acl);
  331. return ret;
  332. }
  333. static int ocfs2_xattr_get_acl_access(struct inode *inode,
  334. const char *name,
  335. void *buffer,
  336. size_t size)
  337. {
  338. if (strcmp(name, "") != 0)
  339. return -EINVAL;
  340. return ocfs2_xattr_get_acl(inode, ACL_TYPE_ACCESS, buffer, size);
  341. }
  342. static int ocfs2_xattr_get_acl_default(struct inode *inode,
  343. const char *name,
  344. void *buffer,
  345. size_t size)
  346. {
  347. if (strcmp(name, "") != 0)
  348. return -EINVAL;
  349. return ocfs2_xattr_get_acl(inode, ACL_TYPE_DEFAULT, buffer, size);
  350. }
  351. static int ocfs2_xattr_set_acl(struct inode *inode,
  352. int type,
  353. const void *value,
  354. size_t size)
  355. {
  356. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  357. struct posix_acl *acl;
  358. int ret = 0;
  359. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  360. return -EOPNOTSUPP;
  361. if (!is_owner_or_cap(inode))
  362. return -EPERM;
  363. if (value) {
  364. acl = posix_acl_from_xattr(value, size);
  365. if (IS_ERR(acl))
  366. return PTR_ERR(acl);
  367. else if (acl) {
  368. ret = posix_acl_valid(acl);
  369. if (ret)
  370. goto cleanup;
  371. }
  372. } else
  373. acl = NULL;
  374. ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
  375. cleanup:
  376. posix_acl_release(acl);
  377. return ret;
  378. }
  379. static int ocfs2_xattr_set_acl_access(struct inode *inode,
  380. const char *name,
  381. const void *value,
  382. size_t size,
  383. int flags)
  384. {
  385. if (strcmp(name, "") != 0)
  386. return -EINVAL;
  387. return ocfs2_xattr_set_acl(inode, ACL_TYPE_ACCESS, value, size);
  388. }
  389. static int ocfs2_xattr_set_acl_default(struct inode *inode,
  390. const char *name,
  391. const void *value,
  392. size_t size,
  393. int flags)
  394. {
  395. if (strcmp(name, "") != 0)
  396. return -EINVAL;
  397. return ocfs2_xattr_set_acl(inode, ACL_TYPE_DEFAULT, value, size);
  398. }
  399. struct xattr_handler ocfs2_xattr_acl_access_handler = {
  400. .prefix = POSIX_ACL_XATTR_ACCESS,
  401. .list = ocfs2_xattr_list_acl_access,
  402. .get = ocfs2_xattr_get_acl_access,
  403. .set = ocfs2_xattr_set_acl_access,
  404. };
  405. struct xattr_handler ocfs2_xattr_acl_default_handler = {
  406. .prefix = POSIX_ACL_XATTR_DEFAULT,
  407. .list = ocfs2_xattr_list_acl_default,
  408. .get = ocfs2_xattr_get_acl_default,
  409. .set = ocfs2_xattr_set_acl_default,
  410. };