acl.c 5.6 KB

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