acl.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved.
  4. *
  5. * This copyrighted material is made available to anyone wishing to use,
  6. * modify, copy, or redistribute it subject to the terms and conditions
  7. * of the GNU General Public License version 2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/xattr.h>
  15. #include <linux/posix_acl.h>
  16. #include <linux/posix_acl_xattr.h>
  17. #include <linux/gfs2_ondisk.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "acl.h"
  21. #include "xattr.h"
  22. #include "glock.h"
  23. #include "inode.h"
  24. #include "meta_io.h"
  25. #include "trans.h"
  26. #include "util.h"
  27. static const char *gfs2_acl_name(int type)
  28. {
  29. switch (type) {
  30. case ACL_TYPE_ACCESS:
  31. return GFS2_POSIX_ACL_ACCESS;
  32. case ACL_TYPE_DEFAULT:
  33. return GFS2_POSIX_ACL_DEFAULT;
  34. }
  35. return NULL;
  36. }
  37. struct posix_acl *gfs2_get_acl(struct inode *inode, int type)
  38. {
  39. struct gfs2_inode *ip = GFS2_I(inode);
  40. struct posix_acl *acl;
  41. const char *name;
  42. char *data;
  43. int len;
  44. if (!ip->i_eattr)
  45. return NULL;
  46. name = gfs2_acl_name(type);
  47. if (name == NULL)
  48. return ERR_PTR(-EINVAL);
  49. len = gfs2_xattr_acl_get(ip, name, &data);
  50. if (len < 0)
  51. return ERR_PTR(len);
  52. if (len == 0)
  53. return NULL;
  54. acl = posix_acl_from_xattr(&init_user_ns, data, len);
  55. kfree(data);
  56. return acl;
  57. }
  58. static int gfs2_set_mode(struct inode *inode, umode_t mode)
  59. {
  60. int error = 0;
  61. if (mode != inode->i_mode) {
  62. inode->i_mode = mode;
  63. mark_inode_dirty(inode);
  64. }
  65. return error;
  66. }
  67. int gfs2_set_acl(struct inode *inode, struct posix_acl *acl, int type)
  68. {
  69. int error;
  70. int len;
  71. char *data;
  72. const char *name = gfs2_acl_name(type);
  73. BUG_ON(name == NULL);
  74. if (acl->a_count > GFS2_ACL_MAX_ENTRIES)
  75. return -EINVAL;
  76. if (type == ACL_TYPE_ACCESS) {
  77. umode_t mode = inode->i_mode;
  78. error = posix_acl_equiv_mode(acl, &mode);
  79. if (error < 0)
  80. return error;
  81. if (error == 0)
  82. acl = NULL;
  83. error = gfs2_set_mode(inode, mode);
  84. if (error)
  85. return error;
  86. }
  87. if (acl) {
  88. len = posix_acl_to_xattr(&init_user_ns, acl, NULL, 0);
  89. if (len == 0)
  90. return 0;
  91. data = kmalloc(len, GFP_NOFS);
  92. if (data == NULL)
  93. return -ENOMEM;
  94. error = posix_acl_to_xattr(&init_user_ns, acl, data, len);
  95. if (error < 0)
  96. goto out;
  97. } else {
  98. data = NULL;
  99. len = 0;
  100. }
  101. error = __gfs2_xattr_set(inode, name, data, len, 0, GFS2_EATYPE_SYS);
  102. if (error)
  103. goto out;
  104. if (acl)
  105. set_cached_acl(inode, type, acl);
  106. else
  107. forget_cached_acl(inode, type);
  108. out:
  109. kfree(data);
  110. return error;
  111. }