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