acl.c 12 KB

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