acl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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. #define MLOG_MASK_PREFIX ML_INODE
  26. #include <cluster/masklog.h>
  27. #include "ocfs2.h"
  28. #include "alloc.h"
  29. #include "dlmglue.h"
  30. #include "file.h"
  31. #include "inode.h"
  32. #include "journal.h"
  33. #include "ocfs2_fs.h"
  34. #include "xattr.h"
  35. #include "acl.h"
  36. /*
  37. * Convert from xattr value to acl struct.
  38. */
  39. static struct posix_acl *ocfs2_acl_from_xattr(const void *value, size_t size)
  40. {
  41. int n, count;
  42. struct posix_acl *acl;
  43. if (!value)
  44. return NULL;
  45. if (size < sizeof(struct posix_acl_entry))
  46. return ERR_PTR(-EINVAL);
  47. count = size / sizeof(struct posix_acl_entry);
  48. if (count < 0)
  49. return ERR_PTR(-EINVAL);
  50. if (count == 0)
  51. return NULL;
  52. acl = posix_acl_alloc(count, GFP_NOFS);
  53. if (!acl)
  54. return ERR_PTR(-ENOMEM);
  55. for (n = 0; n < count; n++) {
  56. struct ocfs2_acl_entry *entry =
  57. (struct ocfs2_acl_entry *)value;
  58. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  59. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  60. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  61. value += sizeof(struct posix_acl_entry);
  62. }
  63. return acl;
  64. }
  65. /*
  66. * Convert acl struct to xattr value.
  67. */
  68. static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
  69. {
  70. struct ocfs2_acl_entry *entry = NULL;
  71. char *ocfs2_acl;
  72. size_t n;
  73. *size = acl->a_count * sizeof(struct posix_acl_entry);
  74. ocfs2_acl = kmalloc(*size, GFP_NOFS);
  75. if (!ocfs2_acl)
  76. return ERR_PTR(-ENOMEM);
  77. entry = (struct ocfs2_acl_entry *)ocfs2_acl;
  78. for (n = 0; n < acl->a_count; n++, entry++) {
  79. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  80. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  81. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  82. }
  83. return ocfs2_acl;
  84. }
  85. static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
  86. int type,
  87. struct buffer_head *di_bh)
  88. {
  89. int name_index;
  90. char *value = NULL;
  91. struct posix_acl *acl;
  92. int retval;
  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. * 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. di->i_mode = cpu_to_le16(inode->i_mode);
  179. ocfs2_journal_dirty(handle, di_bh);
  180. out_commit:
  181. if (commit_handle)
  182. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  183. out_brelse:
  184. brelse(di_bh);
  185. out:
  186. return ret;
  187. }
  188. /*
  189. * Set the access or default ACL of an inode.
  190. */
  191. static int ocfs2_set_acl(handle_t *handle,
  192. struct inode *inode,
  193. struct buffer_head *di_bh,
  194. int type,
  195. struct posix_acl *acl,
  196. struct ocfs2_alloc_context *meta_ac,
  197. struct ocfs2_alloc_context *data_ac)
  198. {
  199. int name_index;
  200. void *value = NULL;
  201. size_t size = 0;
  202. int ret;
  203. if (S_ISLNK(inode->i_mode))
  204. return -EOPNOTSUPP;
  205. switch (type) {
  206. case ACL_TYPE_ACCESS:
  207. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  208. if (acl) {
  209. mode_t mode = inode->i_mode;
  210. ret = posix_acl_equiv_mode(acl, &mode);
  211. if (ret < 0)
  212. return ret;
  213. else {
  214. if (ret == 0)
  215. acl = NULL;
  216. ret = ocfs2_acl_set_mode(inode, di_bh,
  217. handle, mode);
  218. if (ret)
  219. return ret;
  220. }
  221. }
  222. break;
  223. case ACL_TYPE_DEFAULT:
  224. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  225. if (!S_ISDIR(inode->i_mode))
  226. return acl ? -EACCES : 0;
  227. break;
  228. default:
  229. return -EINVAL;
  230. }
  231. if (acl) {
  232. value = ocfs2_acl_to_xattr(acl, &size);
  233. if (IS_ERR(value))
  234. return (int)PTR_ERR(value);
  235. }
  236. if (handle)
  237. ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
  238. "", value, size, 0,
  239. meta_ac, data_ac);
  240. else
  241. ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
  242. kfree(value);
  243. return ret;
  244. }
  245. int ocfs2_check_acl(struct inode *inode, int mask)
  246. {
  247. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  248. struct buffer_head *di_bh = NULL;
  249. struct posix_acl *acl;
  250. int ret = -EAGAIN;
  251. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  252. return ret;
  253. ret = ocfs2_read_inode_block(inode, &di_bh);
  254. if (ret < 0) {
  255. mlog_errno(ret);
  256. return ret;
  257. }
  258. acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, di_bh);
  259. brelse(di_bh);
  260. if (IS_ERR(acl)) {
  261. mlog_errno(PTR_ERR(acl));
  262. return PTR_ERR(acl);
  263. }
  264. if (acl) {
  265. ret = posix_acl_permission(inode, acl, mask);
  266. posix_acl_release(acl);
  267. return ret;
  268. }
  269. return -EAGAIN;
  270. }
  271. int ocfs2_acl_chmod(struct inode *inode)
  272. {
  273. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  274. struct posix_acl *acl, *clone;
  275. int ret;
  276. if (S_ISLNK(inode->i_mode))
  277. return -EOPNOTSUPP;
  278. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  279. return 0;
  280. acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
  281. if (IS_ERR(acl) || !acl)
  282. return PTR_ERR(acl);
  283. clone = posix_acl_clone(acl, GFP_KERNEL);
  284. posix_acl_release(acl);
  285. if (!clone)
  286. return -ENOMEM;
  287. ret = posix_acl_chmod_masq(clone, inode->i_mode);
  288. if (!ret)
  289. ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
  290. clone, NULL, NULL);
  291. posix_acl_release(clone);
  292. return ret;
  293. }
  294. /*
  295. * Initialize the ACLs of a new inode. If parent directory has default ACL,
  296. * then clone to new inode. Called from ocfs2_mknod.
  297. */
  298. int ocfs2_init_acl(handle_t *handle,
  299. struct inode *inode,
  300. struct inode *dir,
  301. struct buffer_head *di_bh,
  302. struct buffer_head *dir_bh,
  303. struct ocfs2_alloc_context *meta_ac,
  304. struct ocfs2_alloc_context *data_ac)
  305. {
  306. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  307. struct posix_acl *acl = NULL;
  308. int ret = 0, ret2;
  309. mode_t mode;
  310. if (!S_ISLNK(inode->i_mode)) {
  311. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  312. acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
  313. dir_bh);
  314. if (IS_ERR(acl))
  315. return PTR_ERR(acl);
  316. }
  317. if (!acl) {
  318. mode = inode->i_mode & ~current_umask();
  319. ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  320. if (ret) {
  321. mlog_errno(ret);
  322. goto cleanup;
  323. }
  324. }
  325. }
  326. if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
  327. struct posix_acl *clone;
  328. if (S_ISDIR(inode->i_mode)) {
  329. ret = ocfs2_set_acl(handle, inode, di_bh,
  330. ACL_TYPE_DEFAULT, acl,
  331. meta_ac, data_ac);
  332. if (ret)
  333. goto cleanup;
  334. }
  335. clone = posix_acl_clone(acl, GFP_NOFS);
  336. ret = -ENOMEM;
  337. if (!clone)
  338. goto cleanup;
  339. mode = inode->i_mode;
  340. ret = posix_acl_create_masq(clone, &mode);
  341. if (ret >= 0) {
  342. ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  343. if (ret2) {
  344. mlog_errno(ret2);
  345. ret = ret2;
  346. goto cleanup;
  347. }
  348. if (ret > 0) {
  349. ret = ocfs2_set_acl(handle, inode,
  350. di_bh, ACL_TYPE_ACCESS,
  351. clone, meta_ac, data_ac);
  352. }
  353. }
  354. posix_acl_release(clone);
  355. }
  356. cleanup:
  357. posix_acl_release(acl);
  358. return ret;
  359. }
  360. static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
  361. char *list,
  362. size_t list_len,
  363. const char *name,
  364. size_t name_len,
  365. int type)
  366. {
  367. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  368. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  369. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  370. return 0;
  371. if (list && size <= list_len)
  372. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  373. return size;
  374. }
  375. static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
  376. char *list,
  377. size_t list_len,
  378. const char *name,
  379. size_t name_len,
  380. int type)
  381. {
  382. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  383. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  384. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  385. return 0;
  386. if (list && size <= list_len)
  387. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  388. return size;
  389. }
  390. static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
  391. void *buffer, size_t size, int type)
  392. {
  393. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  394. struct posix_acl *acl;
  395. int ret;
  396. if (strcmp(name, "") != 0)
  397. return -EINVAL;
  398. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  399. return -EOPNOTSUPP;
  400. acl = ocfs2_get_acl(dentry->d_inode, type);
  401. if (IS_ERR(acl))
  402. return PTR_ERR(acl);
  403. if (acl == NULL)
  404. return -ENODATA;
  405. ret = posix_acl_to_xattr(acl, buffer, size);
  406. posix_acl_release(acl);
  407. return ret;
  408. }
  409. static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
  410. const void *value, size_t size, int flags, int type)
  411. {
  412. struct inode *inode = dentry->d_inode;
  413. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  414. struct posix_acl *acl;
  415. int ret = 0;
  416. if (strcmp(name, "") != 0)
  417. return -EINVAL;
  418. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  419. return -EOPNOTSUPP;
  420. if (!is_owner_or_cap(inode))
  421. return -EPERM;
  422. if (value) {
  423. acl = posix_acl_from_xattr(value, size);
  424. if (IS_ERR(acl))
  425. return PTR_ERR(acl);
  426. else if (acl) {
  427. ret = posix_acl_valid(acl);
  428. if (ret)
  429. goto cleanup;
  430. }
  431. } else
  432. acl = NULL;
  433. ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
  434. cleanup:
  435. posix_acl_release(acl);
  436. return ret;
  437. }
  438. const struct xattr_handler ocfs2_xattr_acl_access_handler = {
  439. .prefix = POSIX_ACL_XATTR_ACCESS,
  440. .flags = ACL_TYPE_ACCESS,
  441. .list = ocfs2_xattr_list_acl_access,
  442. .get = ocfs2_xattr_get_acl,
  443. .set = ocfs2_xattr_set_acl,
  444. };
  445. const struct xattr_handler ocfs2_xattr_acl_default_handler = {
  446. .prefix = POSIX_ACL_XATTR_DEFAULT,
  447. .flags = ACL_TYPE_DEFAULT,
  448. .list = ocfs2_xattr_list_acl_default,
  449. .get = ocfs2_xattr_get_acl,
  450. .set = ocfs2_xattr_set_acl,
  451. };