acl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/quotaops.h>
  23. #include <linux/posix_acl_xattr.h>
  24. #include "jfs_incore.h"
  25. #include "jfs_txnmgr.h"
  26. #include "jfs_xattr.h"
  27. #include "jfs_acl.h"
  28. static struct posix_acl *jfs_get_acl(struct inode *inode, int type)
  29. {
  30. struct posix_acl *acl;
  31. char *ea_name;
  32. int size;
  33. char *value = NULL;
  34. acl = get_cached_acl(inode, type);
  35. if (acl != ACL_NOT_CACHED)
  36. return acl;
  37. switch(type) {
  38. case ACL_TYPE_ACCESS:
  39. ea_name = POSIX_ACL_XATTR_ACCESS;
  40. break;
  41. case ACL_TYPE_DEFAULT:
  42. ea_name = POSIX_ACL_XATTR_DEFAULT;
  43. break;
  44. default:
  45. return ERR_PTR(-EINVAL);
  46. }
  47. size = __jfs_getxattr(inode, ea_name, NULL, 0);
  48. if (size > 0) {
  49. value = kmalloc(size, GFP_KERNEL);
  50. if (!value)
  51. return ERR_PTR(-ENOMEM);
  52. size = __jfs_getxattr(inode, ea_name, value, size);
  53. }
  54. if (size < 0) {
  55. if (size == -ENODATA)
  56. acl = NULL;
  57. else
  58. acl = ERR_PTR(size);
  59. } else {
  60. acl = posix_acl_from_xattr(value, size);
  61. }
  62. kfree(value);
  63. if (!IS_ERR(acl)) {
  64. set_cached_acl(inode, type, acl);
  65. posix_acl_release(acl);
  66. }
  67. return acl;
  68. }
  69. static int jfs_set_acl(tid_t tid, struct inode *inode, int type,
  70. struct posix_acl *acl)
  71. {
  72. char *ea_name;
  73. int rc;
  74. int size = 0;
  75. char *value = NULL;
  76. if (S_ISLNK(inode->i_mode))
  77. return -EOPNOTSUPP;
  78. switch(type) {
  79. case ACL_TYPE_ACCESS:
  80. ea_name = POSIX_ACL_XATTR_ACCESS;
  81. break;
  82. case ACL_TYPE_DEFAULT:
  83. ea_name = POSIX_ACL_XATTR_DEFAULT;
  84. if (!S_ISDIR(inode->i_mode))
  85. return acl ? -EACCES : 0;
  86. break;
  87. default:
  88. return -EINVAL;
  89. }
  90. if (acl) {
  91. size = posix_acl_xattr_size(acl->a_count);
  92. value = kmalloc(size, GFP_KERNEL);
  93. if (!value)
  94. return -ENOMEM;
  95. rc = posix_acl_to_xattr(acl, value, size);
  96. if (rc < 0)
  97. goto out;
  98. }
  99. rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
  100. out:
  101. kfree(value);
  102. if (!rc)
  103. set_cached_acl(inode, type, acl);
  104. return rc;
  105. }
  106. static int jfs_check_acl(struct inode *inode, int mask)
  107. {
  108. struct posix_acl *acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  109. if (IS_ERR(acl))
  110. return PTR_ERR(acl);
  111. if (acl) {
  112. int error = posix_acl_permission(inode, acl, mask);
  113. posix_acl_release(acl);
  114. return error;
  115. }
  116. return -EAGAIN;
  117. }
  118. int jfs_permission(struct inode *inode, int mask)
  119. {
  120. return generic_permission(inode, mask, jfs_check_acl);
  121. }
  122. int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
  123. {
  124. struct posix_acl *acl = NULL;
  125. struct posix_acl *clone;
  126. mode_t mode;
  127. int rc = 0;
  128. if (S_ISLNK(inode->i_mode))
  129. return 0;
  130. acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
  131. if (IS_ERR(acl))
  132. return PTR_ERR(acl);
  133. if (acl) {
  134. if (S_ISDIR(inode->i_mode)) {
  135. rc = jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, acl);
  136. if (rc)
  137. goto cleanup;
  138. }
  139. clone = posix_acl_clone(acl, GFP_KERNEL);
  140. if (!clone) {
  141. rc = -ENOMEM;
  142. goto cleanup;
  143. }
  144. mode = inode->i_mode;
  145. rc = posix_acl_create_masq(clone, &mode);
  146. if (rc >= 0) {
  147. inode->i_mode = mode;
  148. if (rc > 0)
  149. rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS,
  150. clone);
  151. }
  152. posix_acl_release(clone);
  153. cleanup:
  154. posix_acl_release(acl);
  155. } else
  156. inode->i_mode &= ~current_umask();
  157. JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
  158. inode->i_mode;
  159. return rc;
  160. }
  161. static int jfs_acl_chmod(struct inode *inode)
  162. {
  163. struct posix_acl *acl, *clone;
  164. int rc;
  165. if (S_ISLNK(inode->i_mode))
  166. return -EOPNOTSUPP;
  167. acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  168. if (IS_ERR(acl) || !acl)
  169. return PTR_ERR(acl);
  170. clone = posix_acl_clone(acl, GFP_KERNEL);
  171. posix_acl_release(acl);
  172. if (!clone)
  173. return -ENOMEM;
  174. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  175. if (!rc) {
  176. tid_t tid = txBegin(inode->i_sb, 0);
  177. mutex_lock(&JFS_IP(inode)->commit_mutex);
  178. rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, clone);
  179. if (!rc)
  180. rc = txCommit(tid, 1, &inode, 0);
  181. txEnd(tid);
  182. mutex_unlock(&JFS_IP(inode)->commit_mutex);
  183. }
  184. posix_acl_release(clone);
  185. return rc;
  186. }
  187. int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
  188. {
  189. struct inode *inode = dentry->d_inode;
  190. int rc;
  191. rc = inode_change_ok(inode, iattr);
  192. if (rc)
  193. return rc;
  194. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  195. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  196. if (vfs_dq_transfer(inode, iattr))
  197. return -EDQUOT;
  198. }
  199. rc = inode_setattr(inode, iattr);
  200. if (!rc && (iattr->ia_valid & ATTR_MODE))
  201. rc = jfs_acl_chmod(inode);
  202. return rc;
  203. }