acl.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (C) 2007 Red Hat. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public
  6. * License v2 as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public
  14. * License along with this program; if not, write to the
  15. * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  16. * Boston, MA 021110-1307, USA.
  17. */
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/xattr.h>
  21. #include <linux/posix_acl_xattr.h>
  22. #include <linux/posix_acl.h>
  23. #include <linux/sched.h>
  24. #include <linux/slab.h>
  25. #include "ctree.h"
  26. #include "btrfs_inode.h"
  27. #include "xattr.h"
  28. struct posix_acl *btrfs_get_acl(struct inode *inode, int type)
  29. {
  30. int size;
  31. const char *name;
  32. char *value = NULL;
  33. struct posix_acl *acl;
  34. switch (type) {
  35. case ACL_TYPE_ACCESS:
  36. name = XATTR_NAME_POSIX_ACL_ACCESS;
  37. break;
  38. case ACL_TYPE_DEFAULT:
  39. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  40. break;
  41. default:
  42. BUG();
  43. }
  44. size = __btrfs_getxattr(inode, name, "", 0);
  45. if (size > 0) {
  46. value = kzalloc(size, GFP_KERNEL);
  47. if (!value)
  48. return ERR_PTR(-ENOMEM);
  49. size = __btrfs_getxattr(inode, name, value, size);
  50. }
  51. if (size > 0) {
  52. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  53. } else if (size == -ENOENT || size == -ENODATA || size == 0) {
  54. /* FIXME, who returns -ENOENT? I think nobody */
  55. acl = NULL;
  56. } else {
  57. acl = ERR_PTR(-EIO);
  58. }
  59. kfree(value);
  60. return acl;
  61. }
  62. /*
  63. * Needs to be called with fs_mutex held
  64. */
  65. static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
  66. struct inode *inode, struct posix_acl *acl, int type)
  67. {
  68. int ret, size = 0;
  69. const char *name;
  70. char *value = NULL;
  71. switch (type) {
  72. case ACL_TYPE_ACCESS:
  73. name = XATTR_NAME_POSIX_ACL_ACCESS;
  74. if (acl) {
  75. ret = posix_acl_equiv_mode(acl, &inode->i_mode);
  76. if (ret < 0)
  77. return ret;
  78. if (ret == 0)
  79. acl = NULL;
  80. }
  81. ret = 0;
  82. break;
  83. case ACL_TYPE_DEFAULT:
  84. if (!S_ISDIR(inode->i_mode))
  85. return acl ? -EINVAL : 0;
  86. name = XATTR_NAME_POSIX_ACL_DEFAULT;
  87. break;
  88. default:
  89. return -EINVAL;
  90. }
  91. if (acl) {
  92. size = posix_acl_xattr_size(acl->a_count);
  93. value = kmalloc(size, GFP_KERNEL);
  94. if (!value) {
  95. ret = -ENOMEM;
  96. goto out;
  97. }
  98. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  99. if (ret < 0)
  100. goto out;
  101. }
  102. ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
  103. out:
  104. kfree(value);
  105. if (!ret)
  106. set_cached_acl(inode, type, acl);
  107. return ret;
  108. }
  109. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  110. {
  111. return __btrfs_set_acl(NULL, inode, acl, type);
  112. }
  113. /*
  114. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  115. * stuff has been fixed to work with that. If the locking stuff changes, we
  116. * need to re-evaluate the acl locking stuff.
  117. */
  118. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  119. struct inode *inode, struct inode *dir)
  120. {
  121. struct posix_acl *default_acl, *acl;
  122. int ret = 0;
  123. /* this happens with subvols */
  124. if (!dir)
  125. return 0;
  126. ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  127. if (ret)
  128. return ret;
  129. if (default_acl) {
  130. ret = __btrfs_set_acl(trans, inode, default_acl,
  131. ACL_TYPE_DEFAULT);
  132. posix_acl_release(default_acl);
  133. }
  134. if (acl) {
  135. if (!ret)
  136. ret = __btrfs_set_acl(trans, inode, acl,
  137. ACL_TYPE_ACCESS);
  138. posix_acl_release(acl);
  139. }
  140. if (!default_acl && !acl)
  141. cache_no_acl(inode);
  142. return ret;
  143. }