acl.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. switch(type) {
  35. case ACL_TYPE_ACCESS:
  36. ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
  37. break;
  38. case ACL_TYPE_DEFAULT:
  39. ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
  40. break;
  41. default:
  42. return ERR_PTR(-EINVAL);
  43. }
  44. size = __jfs_getxattr(inode, ea_name, NULL, 0);
  45. if (size > 0) {
  46. value = kmalloc(size, GFP_KERNEL);
  47. if (!value)
  48. return ERR_PTR(-ENOMEM);
  49. size = __jfs_getxattr(inode, ea_name, value, size);
  50. }
  51. if (size < 0) {
  52. if (size == -ENODATA)
  53. acl = NULL;
  54. else
  55. acl = ERR_PTR(size);
  56. } else {
  57. acl = posix_acl_from_xattr(&init_user_ns, value, size);
  58. }
  59. kfree(value);
  60. return acl;
  61. }
  62. static int __jfs_set_acl(tid_t tid, struct inode *inode, int type,
  63. struct posix_acl *acl)
  64. {
  65. char *ea_name;
  66. int rc;
  67. int size = 0;
  68. char *value = NULL;
  69. switch (type) {
  70. case ACL_TYPE_ACCESS:
  71. ea_name = XATTR_NAME_POSIX_ACL_ACCESS;
  72. break;
  73. case ACL_TYPE_DEFAULT:
  74. ea_name = XATTR_NAME_POSIX_ACL_DEFAULT;
  75. break;
  76. default:
  77. return -EINVAL;
  78. }
  79. if (acl) {
  80. size = posix_acl_xattr_size(acl->a_count);
  81. value = kmalloc(size, GFP_KERNEL);
  82. if (!value)
  83. return -ENOMEM;
  84. rc = posix_acl_to_xattr(&init_user_ns, acl, value, size);
  85. if (rc < 0)
  86. goto out;
  87. }
  88. rc = __jfs_setxattr(tid, inode, ea_name, value, size, 0);
  89. out:
  90. kfree(value);
  91. if (!rc)
  92. set_cached_acl(inode, type, acl);
  93. return rc;
  94. }
  95. int jfs_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  96. {
  97. int rc;
  98. tid_t tid;
  99. int update_mode = 0;
  100. umode_t mode = inode->i_mode;
  101. tid = txBegin(inode->i_sb, 0);
  102. mutex_lock(&JFS_IP(inode)->commit_mutex);
  103. if (type == ACL_TYPE_ACCESS && acl) {
  104. rc = posix_acl_update_mode(inode, &mode, &acl);
  105. if (rc)
  106. goto end_tx;
  107. update_mode = 1;
  108. }
  109. rc = __jfs_set_acl(tid, inode, type, acl);
  110. if (!rc) {
  111. if (update_mode) {
  112. inode->i_mode = mode;
  113. inode->i_ctime = current_time(inode);
  114. mark_inode_dirty(inode);
  115. }
  116. rc = txCommit(tid, 1, &inode, 0);
  117. }
  118. end_tx:
  119. txEnd(tid);
  120. mutex_unlock(&JFS_IP(inode)->commit_mutex);
  121. return rc;
  122. }
  123. int jfs_init_acl(tid_t tid, struct inode *inode, struct inode *dir)
  124. {
  125. struct posix_acl *default_acl, *acl;
  126. int rc = 0;
  127. rc = posix_acl_create(dir, &inode->i_mode, &default_acl, &acl);
  128. if (rc)
  129. return rc;
  130. if (default_acl) {
  131. rc = __jfs_set_acl(tid, inode, ACL_TYPE_DEFAULT, default_acl);
  132. posix_acl_release(default_acl);
  133. }
  134. if (acl) {
  135. if (!rc)
  136. rc = __jfs_set_acl(tid, inode, ACL_TYPE_ACCESS, acl);
  137. posix_acl_release(acl);
  138. }
  139. JFS_IP(inode)->mode2 = (JFS_IP(inode)->mode2 & 0xffff0000) |
  140. inode->i_mode;
  141. return rc;
  142. }