acl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 "jfs_incore.h"
  24. #include "jfs_xattr.h"
  25. #include "jfs_acl.h"
  26. static struct posix_acl *jfs_get_acl(struct inode *inode, int type)
  27. {
  28. struct posix_acl *acl;
  29. char *ea_name;
  30. struct jfs_inode_info *ji = JFS_IP(inode);
  31. struct posix_acl **p_acl;
  32. int size;
  33. char *value = NULL;
  34. switch(type) {
  35. case ACL_TYPE_ACCESS:
  36. ea_name = XATTR_NAME_ACL_ACCESS;
  37. p_acl = &ji->i_acl;
  38. break;
  39. case ACL_TYPE_DEFAULT:
  40. ea_name = XATTR_NAME_ACL_DEFAULT;
  41. p_acl = &ji->i_default_acl;
  42. break;
  43. default:
  44. return ERR_PTR(-EINVAL);
  45. }
  46. if (*p_acl != JFS_ACL_NOT_CACHED)
  47. return posix_acl_dup(*p_acl);
  48. size = __jfs_getxattr(inode, ea_name, NULL, 0);
  49. if (size > 0) {
  50. value = kmalloc(size, GFP_KERNEL);
  51. if (!value)
  52. return ERR_PTR(-ENOMEM);
  53. size = __jfs_getxattr(inode, ea_name, value, size);
  54. }
  55. if (size < 0) {
  56. if (size == -ENODATA) {
  57. *p_acl = NULL;
  58. acl = NULL;
  59. } else
  60. acl = ERR_PTR(size);
  61. } else {
  62. acl = posix_acl_from_xattr(value, size);
  63. if (!IS_ERR(acl))
  64. *p_acl = posix_acl_dup(acl);
  65. }
  66. kfree(value);
  67. return acl;
  68. }
  69. static int jfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  70. {
  71. char *ea_name;
  72. struct jfs_inode_info *ji = JFS_IP(inode);
  73. struct posix_acl **p_acl;
  74. int rc;
  75. int size = 0;
  76. char *value = NULL;
  77. if (S_ISLNK(inode->i_mode))
  78. return -EOPNOTSUPP;
  79. switch(type) {
  80. case ACL_TYPE_ACCESS:
  81. ea_name = XATTR_NAME_ACL_ACCESS;
  82. p_acl = &ji->i_acl;
  83. break;
  84. case ACL_TYPE_DEFAULT:
  85. ea_name = XATTR_NAME_ACL_DEFAULT;
  86. p_acl = &ji->i_default_acl;
  87. if (!S_ISDIR(inode->i_mode))
  88. return acl ? -EACCES : 0;
  89. break;
  90. default:
  91. return -EINVAL;
  92. }
  93. if (acl) {
  94. size = xattr_acl_size(acl->a_count);
  95. value = kmalloc(size, GFP_KERNEL);
  96. if (!value)
  97. return -ENOMEM;
  98. rc = posix_acl_to_xattr(acl, value, size);
  99. if (rc < 0)
  100. goto out;
  101. }
  102. rc = __jfs_setxattr(inode, ea_name, value, size, 0);
  103. out:
  104. kfree(value);
  105. if (!rc) {
  106. if (*p_acl && (*p_acl != JFS_ACL_NOT_CACHED))
  107. posix_acl_release(*p_acl);
  108. *p_acl = posix_acl_dup(acl);
  109. }
  110. return rc;
  111. }
  112. static int jfs_check_acl(struct inode *inode, int mask)
  113. {
  114. struct jfs_inode_info *ji = JFS_IP(inode);
  115. if (ji->i_acl == JFS_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 (ji->i_acl)
  122. return posix_acl_permission(inode, ji->i_acl, mask);
  123. return -EAGAIN;
  124. }
  125. int jfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  126. {
  127. return generic_permission(inode, mask, jfs_check_acl);
  128. }
  129. int jfs_init_acl(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(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(inode, ACL_TYPE_ACCESS, clone);
  157. }
  158. posix_acl_release(clone);
  159. cleanup:
  160. posix_acl_release(acl);
  161. } else
  162. inode->i_mode &= ~current->fs->umask;
  163. return rc;
  164. }
  165. static int jfs_acl_chmod(struct inode *inode)
  166. {
  167. struct posix_acl *acl, *clone;
  168. int rc;
  169. if (S_ISLNK(inode->i_mode))
  170. return -EOPNOTSUPP;
  171. acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  172. if (IS_ERR(acl) || !acl)
  173. return PTR_ERR(acl);
  174. clone = posix_acl_clone(acl, GFP_KERNEL);
  175. posix_acl_release(acl);
  176. if (!clone)
  177. return -ENOMEM;
  178. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  179. if (!rc)
  180. rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  181. posix_acl_release(clone);
  182. return rc;
  183. }
  184. int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
  185. {
  186. struct inode *inode = dentry->d_inode;
  187. int rc;
  188. rc = inode_change_ok(inode, iattr);
  189. if (rc)
  190. return rc;
  191. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  192. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  193. if (DQUOT_TRANSFER(inode, iattr))
  194. return -EDQUOT;
  195. }
  196. rc = inode_setattr(inode, iattr);
  197. if (!rc && (iattr->ia_valid & ATTR_MODE))
  198. rc = jfs_acl_chmod(inode);
  199. return rc;
  200. }