acl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/xattr.h>
  15. #include <linux/posix_acl.h>
  16. #include <linux/posix_acl_xattr.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "acl.h"
  21. #include "xattr.h"
  22. #include "glock.h"
  23. #include "inode.h"
  24. #include "meta_io.h"
  25. #include "rgrp.h"
  26. #include "trans.h"
  27. #include "util.h"
  28. static const char *gfs2_acl_name(int type)
  29. {
  30. switch (type) {
  31. case ACL_TYPE_ACCESS:
  32. return XATTR_POSIX_ACL_ACCESS;
  33. case ACL_TYPE_DEFAULT:
  34. return XATTR_POSIX_ACL_DEFAULT;
  35. }
  36. return NULL;
  37. }
  38. static struct posix_acl *__gfs2_get_acl(struct inode *inode, int type)
  39. {
  40. struct gfs2_inode *ip = GFS2_I(inode);
  41. struct posix_acl *acl;
  42. const char *name;
  43. char *data;
  44. int len;
  45. if (!ip->i_eattr)
  46. return NULL;
  47. name = gfs2_acl_name(type);
  48. len = gfs2_xattr_acl_get(ip, name, &data);
  49. if (len <= 0)
  50. return ERR_PTR(len);
  51. acl = posix_acl_from_xattr(&init_user_ns, data, len);
  52. kfree(data);
  53. return acl;
  54. }
  55. struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
  56. {
  57. struct gfs2_inode *ip = GFS2_I(inode);
  58. struct gfs2_holder gh;
  59. bool need_unlock = false;
  60. struct posix_acl *acl;
  61. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  62. int ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED,
  63. LM_FLAG_ANY, &gh);
  64. if (ret)
  65. return ERR_PTR(ret);
  66. need_unlock = true;
  67. }
  68. acl = __gfs2_get_acl(inode, type);
  69. if (need_unlock)
  70. gfs2_glock_dq_uninit(&gh);
  71. return acl;
  72. }
  73. int __gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  74. {
  75. int error;
  76. int len;
  77. char *data;
  78. const char *name = gfs2_acl_name(type);
  79. if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
  80. return -E2BIG;
  81. if (type == ACL_TYPE_ACCESS) {
  82. umode_t mode = inode->i_mode;
  83. error = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  84. if (error)
  85. return error;
  86. if (mode != inode->i_mode)
  87. mark_inode_dirty(inode);
  88. }
  89. if (acl) {
  90. len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
  91. if (len == 0)
  92. return 0;
  93. data = kmalloc(len, GFP_NOFS);
  94. if (data == NULL)
  95. return -ENOMEM;
  96. error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
  97. if (error < 0)
  98. goto out;
  99. } else {
  100. data = NULL;
  101. len = 0;
  102. }
  103. error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
  104. if (error)
  105. goto out;
  106. set_cached_acl(inode, type, acl);
  107. out:
  108. kfree(data);
  109. return error;
  110. }
  111. int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  112. {
  113. struct gfs2_inode *ip = GFS2_I(inode);
  114. struct gfs2_holder gh;
  115. bool need_unlock = false;
  116. int ret;
  117. ret = gfs2_rsqa_alloc(ip);
  118. if (ret)
  119. return ret;
  120. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  121. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  122. if (ret)
  123. return ret;
  124. need_unlock = true;
  125. }
  126. ret = __gfs2_set_acl(inode, acl, type);
  127. if (need_unlock)
  128. gfs2_glock_dq_uninit(&gh);
  129. return ret;
  130. }