acl.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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/slab.h>
  22. #include <linux/fs.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. struct posix_acl *jfs_get_acl(struct inode *inode, int type)
  29. {
  30. struct posix_acl *acl;
  31. char *ea_name;
  32. int size;
  33. char *value = NULL;
  34. acl = get_cached_acl(inode, type);
  35. if (acl != ACL_NOT_CACHED)
  36. return acl;
  37. switch(type) {
  38. case ACL_TYPE_ACCESS:
  39. ea_name = POSIX_ACL_XATTR_ACCESS;
  40. break;
  41. case ACL_TYPE_DEFAULT:
  42. ea_name = POSIX_ACL_XATTR_DEFAULT;
  43. break;
  44. default:
  45. return ERR_PTR(-EINVAL);
  46. }
  47. size = __jfs_getxattr(inode, ea_name, NULL, 0);
  48. if (size > 0) {
  49. value = kmalloc(size, GFP_KERNEL);
  50. if (!value)
  51. return ERR_PTR(-ENOMEM);
  52. size = __jfs_getxattr(inode, ea_name, value, size);
  53. }
  54. if (size < 0) {
  55. if (size == -ENODATA)
  56. acl = NULL;
  57. else
  58. acl = ERR_PTR(size);
  59. } else {
  60. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  61. }
  62. kfree(value);
  63. if (!IS_ERR(acl))
  64. set_cached_acl(inode, type, acl);
  65. return acl;
  66. }
  67. static int jfs_set_acl(tid_t tid, struct inode *inode, int type,
  68. struct posix_acl *acl)
  69. {
  70. char *ea_name;
  71. int rc;
  72. int size = 0;
  73. char *value = NULL;
  74. if (S_ISLNK(inode->i_mode))
  75. return -EOPNOTSUPP;
  76. switch(type) {
  77. case ACL_TYPE_ACCESS:
  78. ea_name = POSIX_ACL_XATTR_ACCESS;
  79. break;
  80. case ACL_TYPE_DEFAULT:
  81. ea_name = POSIX_ACL_XATTR_DEFAULT;
  82. if (!S_ISDIR(inode->i_mode))
  83. return acl ? -EACCES : 0;
  84. break;
  85. default:
  86. return -EINVAL;
  87. }
  88. if (acl) {
  89. size = posix_acl_xattr_size(acl->a_count);
  90. value = kmalloc(size, GFP_KERNEL);
  91. if (!value)
  92. return -ENOMEM;
  93. rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  94. if (rc < 0)
  95. goto out;
  96. }
  97. rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
  98. out:
  99. kfree(value);
  100. if (!rc)
  101. set_cached_acl(inode, type, acl);
  102. return rc;
  103. }
  104. int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
  105. {
  106. struct posix_acl *acl = NULL;
  107. int rc = 0;
  108. if (S_ISLNK(inode->i_mode))
  109. return 0;
  110. acl = jfs_get_acl(dir, ACL_TYPE_DEFAULT);
  111. if (IS_ERR(acl))
  112. return PTR_ERR(acl);
  113. if (acl) {
  114. if (S_ISDIR(inode->i_mode)) {
  115. rc = jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, acl);
  116. if (rc)
  117. goto cleanup;
  118. }
  119. rc = __posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
  120. if (rc < 0)
  121. goto cleanup; /* posix_acl_release(NULL) is no-op */
  122. if (rc > 0)
  123. rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
  124. cleanup:
  125. posix_acl_release(acl);
  126. } else
  127. inode->i_mode &= ~current_umask();
  128. JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
  129. inode->i_mode;
  130. return rc;
  131. }
  132. int jfs_acl_chmod(struct inode *inode)
  133. {
  134. struct posix_acl *acl;
  135. int rc;
  136. tid_t tid;
  137. if (S_ISLNK(inode->i_mode))
  138. return -EOPNOTSUPP;
  139. acl = jfs_get_acl(inode, ACL_TYPE_ACCESS);
  140. if (IS_ERR(acl) || !acl)
  141. return PTR_ERR(acl);
  142. rc = __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  143. if (rc)
  144. return rc;
  145. tid = txBegin(inode->i_sb, 0);
  146. mutex_lock(&JFS_IP(inode)->commit_mutex);
  147. rc = jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
  148. if (!rc)
  149. rc = txCommit(tid, 1, &inode, 0);
  150. txEnd(tid);
  151. mutex_unlock(&JFS_IP(inode)->commit_mutex);
  152. posix_acl_release(acl);
  153. return rc;
  154. }