acl.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * Copyright (C) International Business Machines Corp., 2002-2004
  3. * Copyright (C) Andreas Gruenbacher, 2001
  4. * Copyright (C) Linus Torvalds, 1991, 1992
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  14. * the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/sched.h>
  21. #include <linux/fs.h>
  22. #include <linux/posix_acl_xattr.h>
  23. #include "jfs_incore.h"
  24. #include "jfs_txnmgr.h"
  25. #include "jfs_xattr.h"
  26. #include "jfs_acl.h"
  27. static struct posix_acl *jfs_get_acl(struct inode *inode, int type)
  28. {
  29. struct posix_acl *acl;
  30. char *ea_name;
  31. int size;
  32. char *value = NULL;
  33. acl = get_cached_acl(inode, type);
  34. if (acl != ACL_NOT_CACHED)
  35. return acl;
  36. switch(type) {
  37. case ACL_TYPE_ACCESS:
  38. ea_name = POSIX_ACL_XATTR_ACCESS;
  39. break;
  40. case ACL_TYPE_DEFAULT:
  41. ea_name = POSIX_ACL_XATTR_DEFAULT;
  42. break;
  43. default:
  44. return ERR_PTR(-EINVAL);
  45. }
  46. size = __jfs_getxattr(inode, ea_name, NULL, 0);
  47. if (size > 0) {
  48. value = kmalloc(size, GFP_KERNEL);
  49. if (!value)
  50. return ERR_PTR(-ENOMEM);
  51. size = __jfs_getxattr(inode, ea_name, value, size);
  52. }
  53. if (size < 0) {
  54. if (size == -ENODATA)
  55. acl = NULL;
  56. else
  57. acl = ERR_PTR(size);
  58. } else {
  59. acl = posix_acl_from_xattr(value, size);
  60. }
  61. kfree(value);
  62. if (!IS_ERR(acl))
  63. set_cached_acl(inode, type, acl);
  64. return acl;
  65. }
  66. static int jfs_set_acl(tid_t tid, struct inode *inode, int type,
  67. struct posix_acl *acl)
  68. {
  69. char *ea_name;
  70. int rc;
  71. int size = 0;
  72. char *value = NULL;
  73. if (S_ISLNK(inode->i_mode))
  74. return -EOPNOTSUPP;
  75. switch(type) {
  76. case ACL_TYPE_ACCESS:
  77. ea_name = POSIX_ACL_XATTR_ACCESS;
  78. break;
  79. case ACL_TYPE_DEFAULT:
  80. ea_name = POSIX_ACL_XATTR_DEFAULT;
  81. if (!S_ISDIR(inode->i_mode))
  82. return acl ? -EACCES : 0;
  83. break;
  84. default:
  85. return -EINVAL;
  86. }
  87. if (acl) {
  88. size = posix_acl_xattr_size(acl->a_count);
  89. value = kmalloc(size, GFP_KERNEL);
  90. if (!value)
  91. return -ENOMEM;
  92. rc = posix_acl_to_xattr(acl, value, size);
  93. if (rc < 0)
  94. goto out;
  95. }
  96. rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
  97. out:
  98. kfree(value);
  99. if (!rc)
  100. set_cached_acl(inode, type, acl);
  101. return rc;
  102. }
  103. int jfs_check_acl(struct inode *inode, int mask)
  104. {
  105. struct posix_acl *acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  106. if (IS_ERR(acl))
  107. return PTR_ERR(acl);
  108. if (acl) {
  109. int error = posix_acl_permission(inode, acl, mask);
  110. posix_acl_release(acl);
  111. return error;
  112. }
  113. return -EAGAIN;
  114. }
  115. int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
  116. {
  117. struct posix_acl *acl = NULL;
  118. struct posix_acl *clone;
  119. mode_t mode;
  120. int rc = 0;
  121. if (S_ISLNK(inode->i_mode))
  122. return 0;
  123. acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
  124. if (IS_ERR(acl))
  125. return PTR_ERR(acl);
  126. if (acl) {
  127. if (S_ISDIR(inode->i_mode)) {
  128. rc = jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, acl);
  129. if (rc)
  130. goto cleanup;
  131. }
  132. clone = posix_acl_clone(acl, GFP_KERNEL);
  133. if (!clone) {
  134. rc = -ENOMEM;
  135. goto cleanup;
  136. }
  137. mode = inode->i_mode;
  138. rc = posix_acl_create_masq(clone, &mode);
  139. if (rc >= 0) {
  140. inode->i_mode = mode;
  141. if (rc > 0)
  142. rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS,
  143. clone);
  144. }
  145. posix_acl_release(clone);
  146. cleanup:
  147. posix_acl_release(acl);
  148. } else
  149. inode->i_mode &= ~current_umask();
  150. JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
  151. inode->i_mode;
  152. return rc;
  153. }
  154. int jfs_acl_chmod(struct inode *inode)
  155. {
  156. struct posix_acl *acl, *clone;
  157. int rc;
  158. if (S_ISLNK(inode->i_mode))
  159. return -EOPNOTSUPP;
  160. acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  161. if (IS_ERR(acl) || !acl)
  162. return PTR_ERR(acl);
  163. clone = posix_acl_clone(acl, GFP_KERNEL);
  164. posix_acl_release(acl);
  165. if (!clone)
  166. return -ENOMEM;
  167. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  168. if (!rc) {
  169. tid_t tid = txBegin(inode->i_sb, 0);
  170. mutex_lock(&JFS_IP(inode)->commit_mutex);
  171. rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, clone);
  172. if (!rc)
  173. rc = txCommit(tid, 1, &inode, 0);
  174. txEnd(tid);
  175. mutex_unlock(&JFS_IP(inode)->commit_mutex);
  176. }
  177. posix_acl_release(clone);
  178. return rc;
  179. }