acl.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_xattr.h"
  26. #include "jfs_acl.h"
  27. static struct posix_acl *jfs_get_acl(struct inode *inode, int type)
  28. {
  29. struct posix_acl *acl;
  30. char *ea_name;
  31. struct jfs_inode_info *ji = JFS_IP(inode);
  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 = &ji->i_acl;
  39. break;
  40. case ACL_TYPE_DEFAULT:
  41. ea_name = POSIX_ACL_XATTR_DEFAULT;
  42. p_acl = &ji->i_default_acl;
  43. break;
  44. default:
  45. return ERR_PTR(-EINVAL);
  46. }
  47. if (*p_acl != JFS_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(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 = POSIX_ACL_XATTR_ACCESS;
  83. p_acl = &ji->i_acl;
  84. break;
  85. case ACL_TYPE_DEFAULT:
  86. ea_name = POSIX_ACL_XATTR_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 = 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(inode, ea_name, value, size, 0);
  104. out:
  105. kfree(value);
  106. if (!rc) {
  107. if (*p_acl && (*p_acl != JFS_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. struct jfs_inode_info *ji = JFS_IP(inode);
  116. if (ji->i_acl == JFS_ACL_NOT_CACHED) {
  117. struct posix_acl *acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  118. if (IS_ERR(acl))
  119. return PTR_ERR(acl);
  120. posix_acl_release(acl);
  121. }
  122. if (ji->i_acl)
  123. return posix_acl_permission(inode, ji->i_acl, mask);
  124. return -EAGAIN;
  125. }
  126. int jfs_permission(struct inode *inode, int mask, struct nameidata *nd)
  127. {
  128. return generic_permission(inode, mask, jfs_check_acl);
  129. }
  130. int jfs_init_acl(struct inode *inode, struct inode *dir)
  131. {
  132. struct posix_acl *acl = NULL;
  133. struct posix_acl *clone;
  134. mode_t mode;
  135. int rc = 0;
  136. if (S_ISLNK(inode->i_mode))
  137. return 0;
  138. acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
  139. if (IS_ERR(acl))
  140. return PTR_ERR(acl);
  141. if (acl) {
  142. if (S_ISDIR(inode->i_mode)) {
  143. rc = jfs_set_acl(inode, ACL_TYPE_DEFAULT, acl);
  144. if (rc)
  145. goto cleanup;
  146. }
  147. clone = posix_acl_clone(acl, GFP_KERNEL);
  148. if (!clone) {
  149. rc = -ENOMEM;
  150. goto cleanup;
  151. }
  152. mode = inode->i_mode;
  153. rc = posix_acl_create_masq(clone, &mode);
  154. if (rc >= 0) {
  155. inode->i_mode = mode;
  156. if (rc > 0)
  157. rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  158. }
  159. posix_acl_release(clone);
  160. cleanup:
  161. posix_acl_release(acl);
  162. } else
  163. inode->i_mode &= ~current->fs->umask;
  164. return rc;
  165. }
  166. static int jfs_acl_chmod(struct inode *inode)
  167. {
  168. struct posix_acl *acl, *clone;
  169. int rc;
  170. if (S_ISLNK(inode->i_mode))
  171. return -EOPNOTSUPP;
  172. acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  173. if (IS_ERR(acl) || !acl)
  174. return PTR_ERR(acl);
  175. clone = posix_acl_clone(acl, GFP_KERNEL);
  176. posix_acl_release(acl);
  177. if (!clone)
  178. return -ENOMEM;
  179. rc = posix_acl_chmod_masq(clone, inode->i_mode);
  180. if (!rc)
  181. rc = jfs_set_acl(inode, ACL_TYPE_ACCESS, clone);
  182. posix_acl_release(clone);
  183. return rc;
  184. }
  185. int jfs_setattr(struct dentry *dentry, struct iattr *iattr)
  186. {
  187. struct inode *inode = dentry->d_inode;
  188. int rc;
  189. rc = inode_change_ok(inode, iattr);
  190. if (rc)
  191. return rc;
  192. if ((iattr->ia_valid & ATTR_UID && iattr->ia_uid != inode->i_uid) ||
  193. (iattr->ia_valid & ATTR_GID && iattr->ia_gid != inode->i_gid)) {
  194. if (DQUOT_TRANSFER(inode, iattr))
  195. return -EDQUOT;
  196. }
  197. rc = inode_setattr(inode, iattr);
  198. if (!rc && (iattr->ia_valid & ATTR_MODE))
  199. rc = jfs_acl_chmod(inode);
  200. return rc;
  201. }