acl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. if (value)
  67. kfree(value);
  68. return acl;
  69. }
  70. static int jfs_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  71. {
  72. char *ea_name;
  73. struct jfs_inode_info *ji = JFS_IP(inode);
  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 = XATTR_NAME_ACL_ACCESS;
  83. p_acl = &ji->i_acl;
  84. break;
  85. case ACL_TYPE_DEFAULT:
  86. ea_name = XATTR_NAME_ACL_DEFAULT;
  87. p_acl = &ji->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 = xattr_acl_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(inode, ea_name, value, size, 0);
  104. out:
  105. if (value)
  106. kfree(value);
  107. if (!rc) {
  108. if (*p_acl && (*p_acl != JFS_ACL_NOT_CACHED))
  109. posix_acl_release(*p_acl);
  110. *p_acl = posix_acl_dup(acl);
  111. }
  112. return rc;
  113. }
  114. static int jfs_check_acl(struct inode *inode, int mask)
  115. {
  116. struct jfs_inode_info *ji = JFS_IP(inode);
  117. if (ji->i_acl == JFS_ACL_NOT_CACHED) {
  118. struct posix_acl *acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  119. if (IS_ERR(acl))
  120. return PTR_ERR(acl);
  121. posix_acl_release(acl);
  122. }
  123. if (ji->i_acl)
  124. return posix_acl_permission(inode, ji->i_acl, mask);
  125. return -EAGAIN;
  126. }
  127. int jfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  128. {
  129. return generic_permission(inode, mask, jfs_check_acl);
  130. }
  131. int jfs_init_acl(struct inode *inode, struct inode *dir)
  132. {
  133. struct posix_acl *acl = NULL;
  134. struct posix_acl *clone;
  135. mode_t mode;
  136. int rc = 0;
  137. if (S_ISLNK(inode->i_mode))
  138. return 0;
  139. acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
  140. if (IS_ERR(acl))
  141. return PTR_ERR(acl);
  142. if (acl) {
  143. if (S_ISDIR(inode->i_mode)) {
  144. rc = jfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  145. if (rc)
  146. goto cleanup;
  147. }
  148. clone = posix_acl_clone(acl, GFP_KERNEL);
  149. if (!clone) {
  150. rc = -ENOMEM;
  151. goto cleanup;
  152. }
  153. mode = inode->i_mode;
  154. rc = posix_acl_create_masq(clone, &mode);
  155. if (rc >= 0) {
  156. inode->i_mode = mode;
  157. if (rc > 0)
  158. rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  159. }
  160. posix_acl_release(clone);
  161. cleanup:
  162. posix_acl_release(acl);
  163. } else
  164. inode->i_mode &= ~current->fs->umask;
  165. return rc;
  166. }
  167. static int jfs_acl_chmod(struct inode *inode)
  168. {
  169. struct posix_acl *acl, *clone;
  170. int rc;
  171. if (S_ISLNK(inode->i_mode))
  172. return -EOPNOTSUPP;
  173. acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  174. if (IS_ERR(acl) || !acl)
  175. return PTR_ERR(acl);
  176. clone = posix_acl_clone(acl, GFP_KERNEL);
  177. posix_acl_release(acl);
  178. if (!clone)
  179. return -ENOMEM;
  180. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  181. if (!rc)
  182. rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  183. posix_acl_release(clone);
  184. return rc;
  185. }
  186. int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
  187. {
  188. struct inode *inode = dentry->d_inode;
  189. int rc;
  190. rc = inode_change_ok(inode, iattr);
  191. if (rc)
  192. return rc;
  193. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  194. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  195. if (DQUOT_TRANSFER(inode, iattr))
  196. return -EDQUOT;
  197. }
  198. rc = inode_setattr(inode, iattr);
  199. if (!rc && (iattr->ia_valid & ATTR_MODE))
  200. rc = jfs_acl_chmod(inode);
  201. return rc;
  202. }