acl.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * linux/fs/ceph/acl.c
  3. *
  4. * Copyright (C) 2013 Guangliang Zhao, <lucienchao@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public
  8. * License v2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public
  16. * License along with this program; if not, write to the
  17. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18. * Boston, MA 021110-1307, USA.
  19. */
  20. #include <linux/ceph/ceph_debug.h>
  21. #include <linux/fs.h>
  22. #include <linux/string.h>
  23. #include <linux/xattr.h>
  24. #include <linux/posix_acl_xattr.h>
  25. #include <linux/posix_acl.h>
  26. #include <linux/sched.h>
  27. #include <linux/slab.h>
  28. #include "super.h"
  29. static inline void ceph_set_cached_acl(struct inode *inode,
  30. int type, struct posix_acl *acl)
  31. {
  32. struct ceph_inode_info *ci = ceph_inode(inode);
  33. spin_lock(&ci->i_ceph_lock);
  34. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0))
  35. set_cached_acl(inode, type, acl);
  36. spin_unlock(&ci->i_ceph_lock);
  37. }
  38. static inline struct posix_acl *ceph_get_cached_acl(struct inode *inode,
  39. int type)
  40. {
  41. struct ceph_inode_info *ci = ceph_inode(inode);
  42. struct posix_acl *acl = ACL_NOT_CACHED;
  43. spin_lock(&ci->i_ceph_lock);
  44. if (__ceph_caps_issued_mask(ci, CEPH_CAP_XATTR_SHARED, 0))
  45. acl = get_cached_acl(inode, type);
  46. spin_unlock(&ci->i_ceph_lock);
  47. return acl;
  48. }
  49. struct posix_acl *ceph_get_acl(struct inode *inode, int type)
  50. {
  51. int size;
  52. const char *name;
  53. char *value = NULL;
  54. struct posix_acl *acl;
  55. switch (type) {
  56. case ACL_TYPE_ACCESS:
  57. name = POSIX_ACL_XATTR_ACCESS;
  58. break;
  59. case ACL_TYPE_DEFAULT:
  60. name = POSIX_ACL_XATTR_DEFAULT;
  61. break;
  62. default:
  63. BUG();
  64. }
  65. size = __ceph_getxattr(inode, name, "", 0);
  66. if (size > 0) {
  67. value = kzalloc(size, GFP_NOFS);
  68. if (!value)
  69. return ERR_PTR(-ENOMEM);
  70. size = __ceph_getxattr(inode, name, value, size);
  71. }
  72. if (size > 0)
  73. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  74. else if (size == -ERANGE || size == -ENODATA || size == 0)
  75. acl = NULL;
  76. else
  77. acl = ERR_PTR(-EIO);
  78. kfree(value);
  79. if (!IS_ERR(acl))
  80. ceph_set_cached_acl(inode, type, acl);
  81. return acl;
  82. }
  83. int ceph_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  84. {
  85. int ret = 0, size = 0;
  86. const char *name = NULL;
  87. char *value = NULL;
  88. struct iattr newattrs;
  89. umode_t new_mode = inode->i_mode, old_mode = inode->i_mode;
  90. struct dentry *dentry;
  91. switch (type) {
  92. case ACL_TYPE_ACCESS:
  93. name = POSIX_ACL_XATTR_ACCESS;
  94. if (acl) {
  95. ret = posix_acl_equiv_mode(acl, &new_mode);
  96. if (ret < 0)
  97. goto out;
  98. if (ret == 0)
  99. acl = NULL;
  100. }
  101. break;
  102. case ACL_TYPE_DEFAULT:
  103. if (!S_ISDIR(inode->i_mode)) {
  104. ret = acl ? -EINVAL : 0;
  105. goto out;
  106. }
  107. name = POSIX_ACL_XATTR_DEFAULT;
  108. break;
  109. default:
  110. ret = -EINVAL;
  111. goto out;
  112. }
  113. if (acl) {
  114. size = posix_acl_xattr_size(acl->a_count);
  115. value = kmalloc(size, GFP_NOFS);
  116. if (!value) {
  117. ret = -ENOMEM;
  118. goto out;
  119. }
  120. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  121. if (ret < 0)
  122. goto out_free;
  123. }
  124. dentry = d_find_alias(inode);
  125. if (new_mode != old_mode) {
  126. newattrs.ia_mode = new_mode;
  127. newattrs.ia_valid = ATTR_MODE;
  128. ret = ceph_setattr(dentry, &newattrs);
  129. if (ret)
  130. goto out_dput;
  131. }
  132. ret = __ceph_setxattr(dentry, name, value, size, 0);
  133. if (ret) {
  134. if (new_mode != old_mode) {
  135. newattrs.ia_mode = old_mode;
  136. newattrs.ia_valid = ATTR_MODE;
  137. ceph_setattr(dentry, &newattrs);
  138. }
  139. goto out_dput;
  140. }
  141. ceph_set_cached_acl(inode, type, acl);
  142. out_dput:
  143. dput(dentry);
  144. out_free:
  145. kfree(value);
  146. out:
  147. return ret;
  148. }
  149. int ceph_init_acl(struct dentry *dentry, struct inode *inode, struct inode *dir)
  150. {
  151. struct posix_acl *default_acl, *acl;
  152. int error;
  153. error = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  154. if (error)
  155. return error;
  156. if (!default_acl && !acl)
  157. cache_no_acl(inode);
  158. if (default_acl) {
  159. error = ceph_set_acl(inode, default_acl, ACL_TYPE_DEFAULT);
  160. posix_acl_release(default_acl);
  161. }
  162. if (acl) {
  163. if (!error)
  164. error = ceph_set_acl(inode, acl, ACL_TYPE_ACCESS);
  165. posix_acl_release(acl);
  166. }
  167. return error;
  168. }