acl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. break;
  77. case ACL_TYPE_DEFAULT:
  78. if (!S_ISDIR(inode->i_mode))
  79. return acl ? -EINVAL : 0;
  80. name = POSIX_ACL_XATTR_DEFAULT;
  81. break;
  82. default:
  83. return -EINVAL;
  84. }
  85. if (acl) {
  86. size = posix_acl_xattr_size(acl->a_count);
  87. value = kmalloc(size, GFP_NOFS);
  88. if (!value) {
  89. ret = -ENOMEM;
  90. goto out;
  91. }
  92. ret = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  93. if (ret < 0)
  94. goto out;
  95. }
  96. ret = __btrfs_setxattr(trans, inode, name, value, size, 0);
  97. out:
  98. kfree(value);
  99. if (!ret)
  100. set_cached_acl(inode, type, acl);
  101. return ret;
  102. }
  103. int btrfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  104. {
  105. int ret;
  106. umode_t old_mode = inode->i_mode;
  107. if (type == ACL_TYPE_ACCESS && acl) {
  108. ret = posix_acl_update_mode(inode, &inode->i_mode, &acl);
  109. if (ret)
  110. return ret;
  111. }
  112. ret = __btrfs_set_acl(NULL, inode, acl, type);
  113. if (ret)
  114. inode->i_mode = old_mode;
  115. return ret;
  116. }
  117. /*
  118. * btrfs_init_acl is already generally called under fs_mutex, so the locking
  119. * stuff has been fixed to work with that. If the locking stuff changes, we
  120. * need to re-evaluate the acl locking stuff.
  121. */
  122. int btrfs_init_acl(struct btrfs_trans_handle *trans,
  123. struct inode *inode, struct inode *dir)
  124. {
  125. struct posix_acl *default_acl, *acl;
  126. int ret = 0;
  127. /* this happens with subvols */
  128. if (!dir)
  129. return 0;
  130. ret = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  131. if (ret)
  132. return ret;
  133. if (default_acl) {
  134. ret = __btrfs_set_acl(trans, inode, default_acl,
  135. ACL_TYPE_DEFAULT);
  136. posix_acl_release(default_acl);
  137. }
  138. if (acl) {
  139. if (!ret)
  140. ret = __btrfs_set_acl(trans, inode, acl,
  141. ACL_TYPE_ACCESS);
  142. posix_acl_release(acl);
  143. }
  144. if (!default_acl && !acl)
  145. cache_no_acl(inode);
  146. return ret;
  147. }