acl.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. size_t len;
  77. char *data;
  78. const char *name = gfs2_acl_name(type);
  79. if (acl) {
  80. len = posix_acl_xattr_size(acl->a_count);
  81. data = kmalloc(len, GFP_NOFS);
  82. if (data == NULL)
  83. return -ENOMEM;
  84. error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
  85. if (error < 0)
  86. goto out;
  87. } else {
  88. data = NULL;
  89. len = 0;
  90. }
  91. error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
  92. if (error)
  93. goto out;
  94. set_cached_acl(inode, type, acl);
  95. out:
  96. kfree(data);
  97. return error;
  98. }
  99. int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  100. {
  101. struct gfs2_inode *ip = GFS2_I(inode);
  102. struct gfs2_holder gh;
  103. bool need_unlock = false;
  104. int ret;
  105. umode_t mode;
  106. if (acl && acl->a_count > GFS2_ACL_MAX_ENTRIES(GFS2_SB(inode)))
  107. return -E2BIG;
  108. ret = gfs2_rsqa_alloc(ip);
  109. if (ret)
  110. return ret;
  111. if (!gfs2_glock_is_locked_by_me(ip->i_gl)) {
  112. ret = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh);
  113. if (ret)
  114. return ret;
  115. need_unlock = true;
  116. }
  117. mode = inode->i_mode;
  118. if (type == ACL_TYPE_ACCESS && acl) {
  119. ret = posix_acl_update_mode(inode, &mode, &acl);
  120. if (ret)
  121. goto unlock;
  122. }
  123. ret = __gfs2_set_acl(inode, acl, type);
  124. if (!ret && mode != inode->i_mode) {
  125. inode->i_ctime = current_time(inode);
  126. inode->i_mode = mode;
  127. mark_inode_dirty(inode);
  128. }
  129. unlock:
  130. if (need_unlock)
  131. gfs2_glock_dq_uninit(&gh);
  132. return ret;
  133. }