acl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 = POSIX_ACL_XATTR_ACCESS;
  37. break;
  38. case ACL_TYPE_DEFAULT:
  39. name = POSIX_ACL_XATTR_DEFAULT;
  40. break;
  41. default:
  42. BUG();
  43. }
  44. size = __btrfs_getxattr(inode, name, "", 0);
  45. if (size > 0) {
  46. value = kzalloc(size, GFP_NOFS);
  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. if (!IS_ERR(acl))
  61. set_cached_acl(inode, type, acl);
  62. return acl;
  63. }
  64. /*
  65. * Needs to be called with fs_mutex held
  66. */
  67. static int __btrfs_set_acl(struct btrfs_trans_handle *trans,
  68. struct inode *inode, struct posix_acl *acl, int type)
  69. {
  70. int ret, size = 0;
  71. const char *name;
  72. char *value = NULL;
  73. switch (type) {
  74. case ACL_TYPE_ACCESS:
  75. name = POSIX_ACL_XATTR_ACCESS;
  76. if (acl) {
  77. ret = posix_acl_equiv_mode(acl, &inode->i_mode);
  78. if (ret < 0)
  79. return ret;
  80. if (ret == 0)
  81. acl = NULL;
  82. }
  83. ret = 0;
  84. break;
  85. case ACL_TYPE_DEFAULT:
  86. if (!S_ISDIR(inode->i_mode))
  87. return acl ? -EINVAL : 0;
  88. name = POSIX_ACL_XATTR_DEFAULT;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. if (acl) {
  94. size = posix_acl_xattr_size(acl->a_count);
  95. value = kmalloc(size, GFP_NOFS);
  96. if (!value) {
  97. ret = -ENOMEM;
  98. goto out;
  99. }
  100. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  101. if (ret < 0)
  102. goto out;
  103. }
  104. ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
  105. out:
  106. kfree(value);
  107. if (!ret)
  108. set_cached_acl(inode, type, acl);
  109. return ret;
  110. }
  111. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  112. {
  113. return __btrfs_set_acl(NULL, inode, acl, type);
  114. }
  115. /*
  116. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  117. * stuff has been fixed to work with that. If the locking stuff changes, we
  118. * need to re-evaluate the acl locking stuff.
  119. */
  120. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  121. struct inode *inode, struct inode *dir)
  122. {
  123. struct posix_acl *default_acl, *acl;
  124. int ret = 0;
  125. /* this happens with subvols */
  126. if (!dir)
  127. return 0;
  128. ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  129. if (ret)
  130. return ret;
  131. if (default_acl) {
  132. ret = __btrfs_set_acl(trans, inode, default_acl,
  133. ACL_TYPE_DEFAULT);
  134. posix_acl_release(default_acl);
  135. }
  136. if (acl) {
  137. if (!ret)
  138. ret = __btrfs_set_acl(trans, inode, acl,
  139. ACL_TYPE_ACCESS);
  140. posix_acl_release(acl);
  141. }
  142. if (!default_acl && !acl)
  143. cache_no_acl(inode);
  144. return ret;
  145. }