acl.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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. acl = posix_acl_alloc(count, GFP_NOFS);
  48. if (!acl)
  49. return ERR_PTR(-ENOMEM);
  50. for (n = 0; n < count; n++) {
  51. struct ocfs2_acl_entry *entry =
  52. (struct ocfs2_acl_entry *)value;
  53. acl->a_entries[n].e_tag = le16_to_cpu(entry->e_tag);
  54. acl->a_entries[n].e_perm = le16_to_cpu(entry->e_perm);
  55. switch(acl->a_entries[n].e_tag) {
  56. case ACL_USER:
  57. acl->a_entries[n].e_uid =
  58. make_kuid(&init_user_ns,
  59. le32_to_cpu(entry->e_id));
  60. break;
  61. case ACL_GROUP:
  62. acl->a_entries[n].e_gid =
  63. make_kgid(&init_user_ns,
  64. le32_to_cpu(entry->e_id));
  65. break;
  66. default:
  67. break;
  68. }
  69. value += sizeof(struct posix_acl_entry);
  70. }
  71. return acl;
  72. }
  73. /*
  74. * Convert acl struct to xattr value.
  75. */
  76. static void *ocfs2_acl_to_xattr(const struct posix_acl *acl, size_t *size)
  77. {
  78. struct ocfs2_acl_entry *entry = NULL;
  79. char *ocfs2_acl;
  80. size_t n;
  81. *size = acl->a_count * sizeof(struct posix_acl_entry);
  82. ocfs2_acl = kmalloc(*size, GFP_NOFS);
  83. if (!ocfs2_acl)
  84. return ERR_PTR(-ENOMEM);
  85. entry = (struct ocfs2_acl_entry *)ocfs2_acl;
  86. for (n = 0; n < acl->a_count; n++, entry++) {
  87. entry->e_tag = cpu_to_le16(acl->a_entries[n].e_tag);
  88. entry->e_perm = cpu_to_le16(acl->a_entries[n].e_perm);
  89. switch(acl->a_entries[n].e_tag) {
  90. case ACL_USER:
  91. entry->e_id = cpu_to_le32(
  92. from_kuid(&init_user_ns,
  93. acl->a_entries[n].e_uid));
  94. break;
  95. case ACL_GROUP:
  96. entry->e_id = cpu_to_le32(
  97. from_kgid(&init_user_ns,
  98. acl->a_entries[n].e_gid));
  99. break;
  100. default:
  101. entry->e_id = cpu_to_le32(ACL_UNDEFINED_ID);
  102. break;
  103. }
  104. }
  105. return ocfs2_acl;
  106. }
  107. static struct posix_acl *ocfs2_get_acl_nolock(struct inode *inode,
  108. int type,
  109. struct buffer_head *di_bh)
  110. {
  111. int name_index;
  112. char *value = NULL;
  113. struct posix_acl *acl;
  114. int retval;
  115. switch (type) {
  116. case ACL_TYPE_ACCESS:
  117. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  118. break;
  119. case ACL_TYPE_DEFAULT:
  120. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  121. break;
  122. default:
  123. return ERR_PTR(-EINVAL);
  124. }
  125. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index, "", NULL, 0);
  126. if (retval > 0) {
  127. value = kmalloc(retval, GFP_NOFS);
  128. if (!value)
  129. return ERR_PTR(-ENOMEM);
  130. retval = ocfs2_xattr_get_nolock(inode, di_bh, name_index,
  131. "", value, retval);
  132. }
  133. if (retval > 0)
  134. acl = ocfs2_acl_from_xattr(value, retval);
  135. else if (retval == -ENODATA || retval == 0)
  136. acl = NULL;
  137. else
  138. acl = ERR_PTR(retval);
  139. kfree(value);
  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. inode->i_ctime = current_time(inode);
  179. di->i_mode = cpu_to_le16(inode->i_mode);
  180. di->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
  181. di->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
  182. ocfs2_update_inode_fsync_trans(handle, inode, 0);
  183. ocfs2_journal_dirty(handle, di_bh);
  184. out_commit:
  185. if (commit_handle)
  186. ocfs2_commit_trans(OCFS2_SB(inode->i_sb), handle);
  187. out_brelse:
  188. brelse(di_bh);
  189. out:
  190. return ret;
  191. }
  192. /*
  193. * Set the access or default ACL of an inode.
  194. */
  195. static int ocfs2_set_acl(handle_t *handle,
  196. struct inode *inode,
  197. struct buffer_head *di_bh,
  198. int type,
  199. struct posix_acl *acl,
  200. struct ocfs2_alloc_context *meta_ac,
  201. struct ocfs2_alloc_context *data_ac)
  202. {
  203. int name_index;
  204. void *value = NULL;
  205. size_t size = 0;
  206. int ret;
  207. if (S_ISLNK(inode->i_mode))
  208. return -EOPNOTSUPP;
  209. switch (type) {
  210. case ACL_TYPE_ACCESS:
  211. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_ACCESS;
  212. break;
  213. case ACL_TYPE_DEFAULT:
  214. name_index = OCFS2_XATTR_INDEX_POSIX_ACL_DEFAULT;
  215. if (!S_ISDIR(inode->i_mode))
  216. return acl ? -EACCES : 0;
  217. break;
  218. default:
  219. return -EINVAL;
  220. }
  221. if (acl) {
  222. value = ocfs2_acl_to_xattr(acl, &size);
  223. if (IS_ERR(value))
  224. return (int)PTR_ERR(value);
  225. }
  226. if (handle)
  227. ret = ocfs2_xattr_set_handle(handle, inode, di_bh, name_index,
  228. "", value, size, 0,
  229. meta_ac, data_ac);
  230. else
  231. ret = ocfs2_xattr_set(inode, name_index, "", value, size, 0);
  232. kfree(value);
  233. return ret;
  234. }
  235. int ocfs2_iop_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  236. {
  237. struct buffer_head *bh = NULL;
  238. int status, had_lock;
  239. struct ocfs2_lock_holder oh;
  240. had_lock = ocfs2_inode_lock_tracker(inode, &bh, 1, &oh);
  241. if (had_lock < 0)
  242. return had_lock;
  243. if (type == ACL_TYPE_ACCESS && acl) {
  244. umode_t mode;
  245. status = posix_acl_update_mode(inode, &mode, &acl);
  246. if (status)
  247. goto unlock;
  248. status = ocfs2_acl_set_mode(inode, bh, NULL, mode);
  249. if (status)
  250. goto unlock;
  251. }
  252. status = ocfs2_set_acl(NULL, inode, bh, type, acl, NULL, NULL);
  253. unlock:
  254. ocfs2_inode_unlock_tracker(inode, 1, &oh, had_lock);
  255. brelse(bh);
  256. return status;
  257. }
  258. struct posix_acl *ocfs2_iop_get_acl(struct inode *inode, int type)
  259. {
  260. struct ocfs2_super *osb;
  261. struct buffer_head *di_bh = NULL;
  262. struct posix_acl *acl;
  263. int had_lock;
  264. struct ocfs2_lock_holder oh;
  265. osb = OCFS2_SB(inode->i_sb);
  266. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  267. return NULL;
  268. had_lock = ocfs2_inode_lock_tracker(inode, &di_bh, 0, &oh);
  269. if (had_lock < 0)
  270. return ERR_PTR(had_lock);
  271. down_read(&OCFS2_I(inode)->ip_xattr_sem);
  272. acl = ocfs2_get_acl_nolock(inode, type, di_bh);
  273. up_read(&OCFS2_I(inode)->ip_xattr_sem);
  274. ocfs2_inode_unlock_tracker(inode, 0, &oh, had_lock);
  275. brelse(di_bh);
  276. return acl;
  277. }
  278. int ocfs2_acl_chmod(struct inode *inode, struct buffer_head *bh)
  279. {
  280. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  281. struct posix_acl *acl;
  282. int ret;
  283. if (S_ISLNK(inode->i_mode))
  284. return -EOPNOTSUPP;
  285. if (!(osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL))
  286. return 0;
  287. down_read(&OCFS2_I(inode)->ip_xattr_sem);
  288. acl = ocfs2_get_acl_nolock(inode, ACL_TYPE_ACCESS, bh);
  289. up_read(&OCFS2_I(inode)->ip_xattr_sem);
  290. if (IS_ERR_OR_NULL(acl))
  291. return PTR_ERR_OR_ZERO(acl);
  292. ret = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  293. if (ret)
  294. return ret;
  295. ret = ocfs2_set_acl(NULL, inode, NULL, ACL_TYPE_ACCESS,
  296. acl, NULL, NULL);
  297. posix_acl_release(acl);
  298. return ret;
  299. }
  300. /*
  301. * Initialize the ACLs of a new inode. If parent directory has default ACL,
  302. * then clone to new inode. Called from ocfs2_mknod.
  303. */
  304. int ocfs2_init_acl(handle_t *handle,
  305. struct inode *inode,
  306. struct inode *dir,
  307. struct buffer_head *di_bh,
  308. struct buffer_head *dir_bh,
  309. struct ocfs2_alloc_context *meta_ac,
  310. struct ocfs2_alloc_context *data_ac)
  311. {
  312. struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
  313. struct posix_acl *acl = NULL;
  314. int ret = 0, ret2;
  315. umode_t mode;
  316. if (!S_ISLNK(inode->i_mode)) {
  317. if (osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) {
  318. down_read(&OCFS2_I(dir)->ip_xattr_sem);
  319. acl = ocfs2_get_acl_nolock(dir, ACL_TYPE_DEFAULT,
  320. dir_bh);
  321. up_read(&OCFS2_I(dir)->ip_xattr_sem);
  322. if (IS_ERR(acl))
  323. return PTR_ERR(acl);
  324. }
  325. if (!acl) {
  326. mode = inode->i_mode & ~current_umask();
  327. ret = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  328. if (ret) {
  329. mlog_errno(ret);
  330. goto cleanup;
  331. }
  332. }
  333. }
  334. if ((osb->s_mount_opt & OCFS2_MOUNT_POSIX_ACL) && acl) {
  335. if (S_ISDIR(inode->i_mode)) {
  336. ret = ocfs2_set_acl(handle, inode, di_bh,
  337. ACL_TYPE_DEFAULT, acl,
  338. meta_ac, data_ac);
  339. if (ret)
  340. goto cleanup;
  341. }
  342. mode = inode->i_mode;
  343. ret = __posix_acl_create(&acl, GFP_NOFS, &mode);
  344. if (ret < 0)
  345. return ret;
  346. ret2 = ocfs2_acl_set_mode(inode, di_bh, handle, mode);
  347. if (ret2) {
  348. mlog_errno(ret2);
  349. ret = ret2;
  350. goto cleanup;
  351. }
  352. if (ret > 0) {
  353. ret = ocfs2_set_acl(handle, inode,
  354. di_bh, ACL_TYPE_ACCESS,
  355. acl, meta_ac, data_ac);
  356. }
  357. }
  358. cleanup:
  359. posix_acl_release(acl);
  360. return ret;
  361. }