acl.c 11 KB

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