acl.c 5.4 KB

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