generic_acl.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*
  2. * (C) 2005 Andreas Gruenbacher <agruen@suse.de>
  3. *
  4. * This file is released under the GPL.
  5. *
  6. * Generic ACL support for in-memory filesystems.
  7. */
  8. #include <linux/sched.h>
  9. #include <linux/gfp.h>
  10. #include <linux/fs.h>
  11. #include <linux/generic_acl.h>
  12. #include <linux/posix_acl.h>
  13. #include <linux/posix_acl_xattr.h>
  14. static size_t
  15. generic_acl_list(struct dentry *dentry, char *list, size_t list_size,
  16. const char *name, size_t name_len, int type)
  17. {
  18. struct posix_acl *acl;
  19. const char *xname;
  20. size_t size;
  21. acl = get_cached_acl(dentry->d_inode, type);
  22. if (!acl)
  23. return 0;
  24. posix_acl_release(acl);
  25. switch (type) {
  26. case ACL_TYPE_ACCESS:
  27. xname = POSIX_ACL_XATTR_ACCESS;
  28. break;
  29. case ACL_TYPE_DEFAULT:
  30. xname = POSIX_ACL_XATTR_DEFAULT;
  31. break;
  32. default:
  33. return 0;
  34. }
  35. size = strlen(xname) + 1;
  36. if (list && size <= list_size)
  37. memcpy(list, xname, size);
  38. return size;
  39. }
  40. static int
  41. generic_acl_get(struct dentry *dentry, const char *name, void *buffer,
  42. size_t size, int type)
  43. {
  44. struct posix_acl *acl;
  45. int error;
  46. if (strcmp(name, "") != 0)
  47. return -EINVAL;
  48. acl = get_cached_acl(dentry->d_inode, type);
  49. if (!acl)
  50. return -ENODATA;
  51. error = posix_acl_to_xattr(acl, buffer, size);
  52. posix_acl_release(acl);
  53. return error;
  54. }
  55. static int
  56. generic_acl_set(struct dentry *dentry, const char *name, const void *value,
  57. size_t size, int flags, int type)
  58. {
  59. struct inode *inode = dentry->d_inode;
  60. struct posix_acl *acl = NULL;
  61. int error;
  62. if (strcmp(name, "") != 0)
  63. return -EINVAL;
  64. if (S_ISLNK(inode->i_mode))
  65. return -EOPNOTSUPP;
  66. if (!inode_owner_or_capable(inode))
  67. return -EPERM;
  68. if (value) {
  69. acl = posix_acl_from_xattr(value, size);
  70. if (IS_ERR(acl))
  71. return PTR_ERR(acl);
  72. }
  73. if (acl) {
  74. error = posix_acl_valid(acl);
  75. if (error)
  76. goto failed;
  77. switch (type) {
  78. case ACL_TYPE_ACCESS:
  79. error = posix_acl_equiv_mode(acl, &inode->i_mode);
  80. if (error < 0)
  81. goto failed;
  82. inode->i_ctime = CURRENT_TIME;
  83. if (error == 0) {
  84. posix_acl_release(acl);
  85. acl = NULL;
  86. }
  87. break;
  88. case ACL_TYPE_DEFAULT:
  89. if (!S_ISDIR(inode->i_mode)) {
  90. error = -EINVAL;
  91. goto failed;
  92. }
  93. break;
  94. }
  95. }
  96. set_cached_acl(inode, type, acl);
  97. error = 0;
  98. failed:
  99. posix_acl_release(acl);
  100. return error;
  101. }
  102. /**
  103. * generic_acl_init - Take care of acl inheritance at @inode create time
  104. *
  105. * Files created inside a directory with a default ACL inherit the
  106. * directory's default ACL.
  107. */
  108. int
  109. generic_acl_init(struct inode *inode, struct inode *dir)
  110. {
  111. struct posix_acl *acl = NULL;
  112. int error;
  113. if (!S_ISLNK(inode->i_mode))
  114. acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
  115. if (acl) {
  116. if (S_ISDIR(inode->i_mode))
  117. set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
  118. error = posix_acl_create(&acl, GFP_KERNEL, &inode->i_mode);
  119. if (error < 0)
  120. return error;
  121. if (error > 0)
  122. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  123. } else {
  124. inode->i_mode &= ~current_umask();
  125. }
  126. error = 0;
  127. posix_acl_release(acl);
  128. return error;
  129. }
  130. /**
  131. * generic_acl_chmod - change the access acl of @inode upon chmod()
  132. *
  133. * A chmod also changes the permissions of the owner, group/mask, and
  134. * other ACL entries.
  135. */
  136. int
  137. generic_acl_chmod(struct inode *inode)
  138. {
  139. struct posix_acl *acl;
  140. int error = 0;
  141. if (S_ISLNK(inode->i_mode))
  142. return -EOPNOTSUPP;
  143. acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  144. if (acl) {
  145. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  146. if (error)
  147. return error;
  148. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  149. posix_acl_release(acl);
  150. }
  151. return error;
  152. }
  153. const struct xattr_handler generic_acl_access_handler = {
  154. .prefix = POSIX_ACL_XATTR_ACCESS,
  155. .flags = ACL_TYPE_ACCESS,
  156. .list = generic_acl_list,
  157. .get = generic_acl_get,
  158. .set = generic_acl_set,
  159. };
  160. const struct xattr_handler generic_acl_default_handler = {
  161. .prefix = POSIX_ACL_XATTR_DEFAULT,
  162. .flags = ACL_TYPE_DEFAULT,
  163. .list = generic_acl_list,
  164. .get = generic_acl_get,
  165. .set = generic_acl_set,
  166. };