shmem_acl.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * mm/shmem_acl.c
  3. *
  4. * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
  5. *
  6. * This file is released under the GPL.
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/shmem_fs.h>
  10. #include <linux/xattr.h>
  11. #include <linux/generic_acl.h>
  12. /**
  13. * shmem_get_acl - generic_acl_operations->getacl() operation
  14. */
  15. static struct posix_acl *
  16. shmem_get_acl(struct inode *inode, int type)
  17. {
  18. struct posix_acl *acl = NULL;
  19. spin_lock(&inode->i_lock);
  20. switch(type) {
  21. case ACL_TYPE_ACCESS:
  22. acl = posix_acl_dup(SHMEM_I(inode)->i_acl);
  23. break;
  24. case ACL_TYPE_DEFAULT:
  25. acl = posix_acl_dup(SHMEM_I(inode)->i_default_acl);
  26. break;
  27. }
  28. spin_unlock(&inode->i_lock);
  29. return acl;
  30. }
  31. /**
  32. * shmem_set_acl - generic_acl_operations->setacl() operation
  33. */
  34. static void
  35. shmem_set_acl(struct inode *inode, int type, struct posix_acl *acl)
  36. {
  37. struct posix_acl *free = NULL;
  38. spin_lock(&inode->i_lock);
  39. switch(type) {
  40. case ACL_TYPE_ACCESS:
  41. free = SHMEM_I(inode)->i_acl;
  42. SHMEM_I(inode)->i_acl = posix_acl_dup(acl);
  43. break;
  44. case ACL_TYPE_DEFAULT:
  45. free = SHMEM_I(inode)->i_default_acl;
  46. SHMEM_I(inode)->i_default_acl = posix_acl_dup(acl);
  47. break;
  48. }
  49. spin_unlock(&inode->i_lock);
  50. posix_acl_release(free);
  51. }
  52. struct generic_acl_operations shmem_acl_ops = {
  53. .getacl = shmem_get_acl,
  54. .setacl = shmem_set_acl,
  55. };
  56. /**
  57. * shmem_list_acl_access, shmem_get_acl_access, shmem_set_acl_access,
  58. * shmem_xattr_acl_access_handler - plumbing code to implement the
  59. * system.posix_acl_access xattr using the generic acl functions.
  60. */
  61. static size_t
  62. shmem_list_acl_access(struct inode *inode, char *list, size_t list_size,
  63. const char *name, size_t name_len)
  64. {
  65. return generic_acl_list(inode, &shmem_acl_ops, ACL_TYPE_ACCESS,
  66. list, list_size);
  67. }
  68. static int
  69. shmem_get_acl_access(struct inode *inode, const char *name, void *buffer,
  70. size_t size)
  71. {
  72. if (strcmp(name, "") != 0)
  73. return -EINVAL;
  74. return generic_acl_get(inode, &shmem_acl_ops, ACL_TYPE_ACCESS, buffer,
  75. size);
  76. }
  77. static int
  78. shmem_set_acl_access(struct inode *inode, const char *name, const void *value,
  79. size_t size, int flags)
  80. {
  81. if (strcmp(name, "") != 0)
  82. return -EINVAL;
  83. return generic_acl_set(inode, &shmem_acl_ops, ACL_TYPE_ACCESS, value,
  84. size);
  85. }
  86. struct xattr_handler shmem_xattr_acl_access_handler = {
  87. .prefix = POSIX_ACL_XATTR_ACCESS,
  88. .list = shmem_list_acl_access,
  89. .get = shmem_get_acl_access,
  90. .set = shmem_set_acl_access,
  91. };
  92. /**
  93. * shmem_list_acl_default, shmem_get_acl_default, shmem_set_acl_default,
  94. * shmem_xattr_acl_default_handler - plumbing code to implement the
  95. * system.posix_acl_default xattr using the generic acl functions.
  96. */
  97. static size_t
  98. shmem_list_acl_default(struct inode *inode, char *list, size_t list_size,
  99. const char *name, size_t name_len)
  100. {
  101. return generic_acl_list(inode, &shmem_acl_ops, ACL_TYPE_DEFAULT,
  102. list, list_size);
  103. }
  104. static int
  105. shmem_get_acl_default(struct inode *inode, const char *name, void *buffer,
  106. size_t size)
  107. {
  108. if (strcmp(name, "") != 0)
  109. return -EINVAL;
  110. return generic_acl_get(inode, &shmem_acl_ops, ACL_TYPE_DEFAULT, buffer,
  111. size);
  112. }
  113. static int
  114. shmem_set_acl_default(struct inode *inode, const char *name, const void *value,
  115. size_t size, int flags)
  116. {
  117. if (strcmp(name, "") != 0)
  118. return -EINVAL;
  119. return generic_acl_set(inode, &shmem_acl_ops, ACL_TYPE_DEFAULT, value,
  120. size);
  121. }
  122. struct xattr_handler shmem_xattr_acl_default_handler = {
  123. .prefix = POSIX_ACL_XATTR_DEFAULT,
  124. .list = shmem_list_acl_default,
  125. .get = shmem_get_acl_default,
  126. .set = shmem_set_acl_default,
  127. };
  128. /**
  129. * shmem_acl_init - Inizialize the acl(s) of a new inode
  130. */
  131. int
  132. shmem_acl_init(struct inode *inode, struct inode *dir)
  133. {
  134. return generic_acl_init(inode, dir, &shmem_acl_ops);
  135. }
  136. /**
  137. * shmem_acl_destroy_inode - destroy acls hanging off the in-memory inode
  138. *
  139. * This is done before destroying the actual inode.
  140. */
  141. void
  142. shmem_acl_destroy_inode(struct inode *inode)
  143. {
  144. if (SHMEM_I(inode)->i_acl)
  145. posix_acl_release(SHMEM_I(inode)->i_acl);
  146. SHMEM_I(inode)->i_acl = NULL;
  147. if (SHMEM_I(inode)->i_default_acl)
  148. posix_acl_release(SHMEM_I(inode)->i_default_acl);
  149. SHMEM_I(inode)->i_default_acl = NULL;
  150. }
  151. /**
  152. * shmem_check_acl - check_acl() callback for generic_permission()
  153. */
  154. static int
  155. shmem_check_acl(struct inode *inode, int mask)
  156. {
  157. struct posix_acl *acl = shmem_get_acl(inode, ACL_TYPE_ACCESS);
  158. if (acl) {
  159. int error = posix_acl_permission(inode, acl, mask);
  160. posix_acl_release(acl);
  161. return error;
  162. }
  163. return -EAGAIN;
  164. }
  165. /**
  166. * shmem_permission - permission() inode operation
  167. */
  168. int
  169. shmem_permission(struct inode *inode, int mask, struct nameidata *nd)
  170. {
  171. return generic_permission(inode, mask, shmem_check_acl);
  172. }