acl.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. if (count < 0)
  48. return ERR_PTR(-EINVAL);
  49. if (count == 0)
  50. return NULL;
  51. acl = posix_acl_alloc(count, GFP_NOFS);
  52. if (!acl)
  53. return ERR_PTR(-ENOMEM);
  54. for (n = 0; n < count; n++) {
  55. struct ocfs2_acl_entry *entry =
  56. (struct ocfs2_acl_entry *)value;
  57. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  58. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  59. acl->a_entries[n].e_id = le32_to_cpu(entry->e_id);
  60. value += sizeof(struct posix_acl_entry);
  61. }
  62. return acl;
  63. }
  64. /*
  65. * Convert acl struct to xattr value.
  66. */
  67. static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
  68. {
  69. struct ocfs2_acl_entry *entry = NULL;
  70. char *ocfs2_acl;
  71. size_t n;
  72. *size = acl->a_count * sizeof(struct posix_acl_entry);
  73. ocfs2_acl = kmalloc(*size, GFP_NOFS);
  74. if (!ocfs2_acl)
  75. return ERR_PTR(-ENOMEM);
  76. entry = (struct ocfs2_acl_entry *)ocfs2_acl;
  77. for (n = 0; n < acl->a_count; n++, entry++) {
  78. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  79. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  80. entry->e_id = cpu_to_le32(acl->a_entries[n].e_id);
  81. }
  82. return ocfs2_acl;
  83. }
  84. static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
  85. int type,
  86. struct buffer_head *di_bh)
  87. {
  88. int name_index;
  89. char *value = NULL;
  90. struct posix_acl *acl;
  91. int retval;
  92. switch (type) {
  93. case ACL_TYPE_ACCESS:
  94. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  95. break;
  96. case ACL_TYPE_DEFAULT:
  97. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  98. break;
  99. default:
  100. return ERR_PTR(-EINVAL);
  101. }
  102. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
  103. if (retval > 0) {
  104. value = kmalloc(retval, GFP_NOFS);
  105. if (!value)
  106. return ERR_PTR(-ENOMEM);
  107. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  108. "", value, retval);
  109. }
  110. if (retval > 0)
  111. acl = ocfs2_acl_from_xattr(value, retval);
  112. else if (retval == -ENODATA || retval == 0)
  113. acl = NULL;
  114. else
  115. acl = ERR_PTR(retval);
  116. kfree(value);
  117. return acl;
  118. }
  119. /*
  120. * Get posix acl.
  121. */
  122. static struct posix_acl *ocfs2_get_acl(struct inode *inode, int type)
  123. {
  124. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  125. struct buffer_head *di_bh = NULL;
  126. struct posix_acl *acl;
  127. int ret;
  128. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  129. return NULL;
  130. ret = ocfs2_inode_lock(inode, &di_bh, 0);
  131. if (ret < 0) {
  132. mlog_errno(ret);
  133. acl = ERR_PTR(ret);
  134. return acl;
  135. }
  136. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  137. ocfs2_inode_unlock(inode, 0);
  138. brelse(di_bh);
  139. return acl;
  140. }
  141. /*
  142. * Helper function to set i_mode in memory and disk. Some call paths
  143. * will not have di_bh or a journal handle to pass, in which case it
  144. * will create it's own.
  145. */
  146. static int ocfs2_acl_set_mode(struct inode *inode, struct buffer_head *di_bh,
  147. handle_t *handle, umode_t new_mode)
  148. {
  149. int ret, commit_handle = 0;
  150. struct ocfs2_dinode *di;
  151. if (di_bh == NULL) {
  152. ret = ocfs2_read_inode_block(inode, &di_bh);
  153. if (ret) {
  154. mlog_errno(ret);
  155. goto out;
  156. }
  157. } else
  158. get_bh(di_bh);
  159. if (handle == NULL) {
  160. handle = ocfs2_start_trans(OCFS2_SB(inode->i_sb),
  161. OCFS2_INODE_UPDATE_CREDITS);
  162. if (IS_ERR(handle)) {
  163. ret = PTR_ERR(handle);
  164. mlog_errno(ret);
  165. goto out_brelse;
  166. }
  167. commit_handle = 1;
  168. }
  169. di = (struct ocfs2_dinode *)di_bh->b_data;
  170. ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
  171. OCFS2_JOURNAL_ACCESS_WRITE);
  172. if (ret) {
  173. mlog_errno(ret);
  174. goto out_commit;
  175. }
  176. inode->i_mode = new_mode;
  177. inode->i_ctime = CURRENT_TIME;
  178. di->i_mode = cpu_to_le16(inode->i_mode);
  179. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  180. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  181. ocfs2_journal_dirty(handle, di_bh);
  182. out_commit:
  183. if (commit_handle)
  184. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  185. out_brelse:
  186. brelse(di_bh);
  187. out:
  188. return ret;
  189. }
  190. /*
  191. * Set the access or default ACL of an inode.
  192. */
  193. static int ocfs2_set_acl(handle_t *handle,
  194. struct inode *inode,
  195. struct buffer_head *di_bh,
  196. int type,
  197. struct posix_acl *acl,
  198. struct ocfs2_alloc_context *meta_ac,
  199. struct ocfs2_alloc_context *data_ac)
  200. {
  201. int name_index;
  202. void *value = NULL;
  203. size_t size = 0;
  204. int ret;
  205. if (S_ISLNK(inode->i_mode))
  206. return -EOPNOTSUPP;
  207. switch (type) {
  208. case ACL_TYPE_ACCESS:
  209. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  210. if (acl) {
  211. umode_t mode = inode->i_mode;
  212. ret = posix_acl_equiv_mode(acl, &mode);
  213. if (ret < 0)
  214. return ret;
  215. else {
  216. if (ret == 0)
  217. acl = NULL;
  218. ret = ocfs2_acl_set_mode(inode, di_bh,
  219. handle, mode);
  220. if (ret)
  221. return ret;
  222. }
  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. struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
  248. {
  249. struct ocfs2_super *osb;
  250. struct buffer_head *di_bh = NULL;
  251. struct posix_acl *acl;
  252. int ret = -EAGAIN;
  253. osb = OCFS2_SB(inode->i_sb);
  254. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  255. return NULL;
  256. ret = ocfs2_read_inode_block(inode, &di_bh);
  257. if (ret < 0)
  258. return ERR_PTR(ret);
  259. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  260. brelse(di_bh);
  261. return acl;
  262. }
  263. int ocfs2_acl_chmod(struct inode *inode)
  264. {
  265. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  266. struct posix_acl *acl;
  267. int ret;
  268. if (S_ISLNK(inode->i_mode))
  269. return -EOPNOTSUPP;
  270. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  271. return 0;
  272. acl = ocfs2_get_acl(inode, ACL_TYPE_ACCESS);
  273. if (IS_ERR(acl) || !acl)
  274. return PTR_ERR(acl);
  275. ret = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  276. if (ret)
  277. return ret;
  278. ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
  279. acl, NULL, NULL);
  280. posix_acl_release(acl);
  281. return ret;
  282. }
  283. /*
  284. * Initialize the ACLs of a new inode. If parent directory has default ACL,
  285. * then clone to new inode. Called from ocfs2_mknod.
  286. */
  287. int ocfs2_init_acl(handle_t *handle,
  288. struct inode *inode,
  289. struct inode *dir,
  290. struct buffer_head *di_bh,
  291. struct buffer_head *dir_bh,
  292. struct ocfs2_alloc_context *meta_ac,
  293. struct ocfs2_alloc_context *data_ac)
  294. {
  295. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  296. struct posix_acl *acl = NULL;
  297. int ret = 0, ret2;
  298. umode_t mode;
  299. if (!S_ISLNK(inode->i_mode)) {
  300. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  301. acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
  302. dir_bh);
  303. if (IS_ERR(acl))
  304. return PTR_ERR(acl);
  305. }
  306. if (!acl) {
  307. mode = inode->i_mode & ~current_umask();
  308. ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  309. if (ret) {
  310. mlog_errno(ret);
  311. goto cleanup;
  312. }
  313. }
  314. }
  315. if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
  316. if (S_ISDIR(inode->i_mode)) {
  317. ret = ocfs2_set_acl(handle, inode, di_bh,
  318. ACL_TYPE_DEFAULT, acl,
  319. meta_ac, data_ac);
  320. if (ret)
  321. goto cleanup;
  322. }
  323. mode = inode->i_mode;
  324. ret = posix_acl_create(&acl, GFP_NOFS, &mode);
  325. if (ret < 0)
  326. return ret;
  327. ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  328. if (ret2) {
  329. mlog_errno(ret2);
  330. ret = ret2;
  331. goto cleanup;
  332. }
  333. if (ret > 0) {
  334. ret = ocfs2_set_acl(handle, inode,
  335. di_bh, ACL_TYPE_ACCESS,
  336. acl, meta_ac, data_ac);
  337. }
  338. }
  339. cleanup:
  340. posix_acl_release(acl);
  341. return ret;
  342. }
  343. static size_t ocfs2_xattr_list_acl_access(struct dentry *dentry,
  344. char *list,
  345. size_t list_len,
  346. const char *name,
  347. size_t name_len,
  348. int type)
  349. {
  350. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  351. const size_t size = sizeof(POSIX_ACL_XATTR_ACCESS);
  352. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  353. return 0;
  354. if (list && size <= list_len)
  355. memcpy(list, POSIX_ACL_XATTR_ACCESS, size);
  356. return size;
  357. }
  358. static size_t ocfs2_xattr_list_acl_default(struct dentry *dentry,
  359. char *list,
  360. size_t list_len,
  361. const char *name,
  362. size_t name_len,
  363. int type)
  364. {
  365. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  366. const size_t size = sizeof(POSIX_ACL_XATTR_DEFAULT);
  367. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  368. return 0;
  369. if (list && size <= list_len)
  370. memcpy(list, POSIX_ACL_XATTR_DEFAULT, size);
  371. return size;
  372. }
  373. static int ocfs2_xattr_get_acl(struct dentry *dentry, const char *name,
  374. void *buffer, size_t size, int type)
  375. {
  376. struct ocfs2_super *osb = OCFS2_SB(dentry->d_sb);
  377. struct posix_acl *acl;
  378. int ret;
  379. if (strcmp(name, "") != 0)
  380. return -EINVAL;
  381. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  382. return -EOPNOTSUPP;
  383. acl = ocfs2_get_acl(dentry->d_inode, type);
  384. if (IS_ERR(acl))
  385. return PTR_ERR(acl);
  386. if (acl == NULL)
  387. return -ENODATA;
  388. ret = posix_acl_to_xattr(acl, buffer, size);
  389. posix_acl_release(acl);
  390. return ret;
  391. }
  392. static int ocfs2_xattr_set_acl(struct dentry *dentry, const char *name,
  393. const void *value, size_t size, int flags, int type)
  394. {
  395. struct inode *inode = dentry->d_inode;
  396. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  397. struct posix_acl *acl;
  398. int ret = 0;
  399. if (strcmp(name, "") != 0)
  400. return -EINVAL;
  401. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  402. return -EOPNOTSUPP;
  403. if (!inode_owner_or_capable(inode))
  404. return -EPERM;
  405. if (value) {
  406. acl = posix_acl_from_xattr(value, size);
  407. if (IS_ERR(acl))
  408. return PTR_ERR(acl);
  409. else if (acl) {
  410. ret = posix_acl_valid(acl);
  411. if (ret)
  412. goto cleanup;
  413. }
  414. } else
  415. acl = NULL;
  416. ret = ocfs2_set_acl(NULL, inode, NULL, type, acl, NULL, NULL);
  417. cleanup:
  418. posix_acl_release(acl);
  419. return ret;
  420. }
  421. const struct xattr_handler ocfs2_xattr_acl_access_handler = {
  422. .prefix = POSIX_ACL_XATTR_ACCESS,
  423. .flags = ACL_TYPE_ACCESS,
  424. .list = ocfs2_xattr_list_acl_access,
  425. .get = ocfs2_xattr_get_acl,
  426. .set = ocfs2_xattr_set_acl,
  427. };
  428. const struct xattr_handler ocfs2_xattr_acl_default_handler = {
  429. .prefix = POSIX_ACL_XATTR_DEFAULT,
  430. .flags = ACL_TYPE_DEFAULT,
  431. .list = ocfs2_xattr_list_acl_default,
  432. .get = ocfs2_xattr_get_acl,
  433. .set = ocfs2_xattr_set_acl,
  434. };