generic_acl.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. mode_t mode;
  75. error = posix_acl_valid(acl);
  76. if (error)
  77. goto failed;
  78. switch (type) {
  79. case ACL_TYPE_ACCESS:
  80. mode = inode->i_mode;
  81. error = posix_acl_equiv_mode(acl, &mode);
  82. if (error < 0)
  83. goto failed;
  84. inode->i_mode = mode;
  85. inode->i_ctime = CURRENT_TIME;
  86. if (error == 0) {
  87. posix_acl_release(acl);
  88. acl = NULL;
  89. }
  90. break;
  91. case ACL_TYPE_DEFAULT:
  92. if (!S_ISDIR(inode->i_mode)) {
  93. error = -EINVAL;
  94. goto failed;
  95. }
  96. break;
  97. }
  98. }
  99. set_cached_acl(inode, type, acl);
  100. error = 0;
  101. failed:
  102. posix_acl_release(acl);
  103. return error;
  104. }
  105. /**
  106. * generic_acl_init - Take care of acl inheritance at @inode create time
  107. *
  108. * Files created inside a directory with a default ACL inherit the
  109. * directory's default ACL.
  110. */
  111. int
  112. generic_acl_init(struct inode *inode, struct inode *dir)
  113. {
  114. struct posix_acl *acl = NULL;
  115. mode_t mode = inode->i_mode;
  116. int error;
  117. inode->i_mode = mode & ~current_umask();
  118. if (!S_ISLNK(inode->i_mode))
  119. acl = get_cached_acl(dir, ACL_TYPE_DEFAULT);
  120. if (acl) {
  121. if (S_ISDIR(inode->i_mode))
  122. set_cached_acl(inode, ACL_TYPE_DEFAULT, acl);
  123. error = posix_acl_create(&acl, GFP_KERNEL, &mode);
  124. if (error < 0)
  125. return error;
  126. inode->i_mode = mode;
  127. if (error > 0)
  128. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  129. }
  130. error = 0;
  131. posix_acl_release(acl);
  132. return error;
  133. }
  134. /**
  135. * generic_acl_chmod - change the access acl of @inode upon chmod()
  136. *
  137. * A chmod also changes the permissions of the owner, group/mask, and
  138. * other ACL entries.
  139. */
  140. int
  141. generic_acl_chmod(struct inode *inode)
  142. {
  143. struct posix_acl *acl;
  144. int error = 0;
  145. if (S_ISLNK(inode->i_mode))
  146. return -EOPNOTSUPP;
  147. acl = get_cached_acl(inode, ACL_TYPE_ACCESS);
  148. if (acl) {
  149. error = posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
  150. if (error)
  151. return error;
  152. set_cached_acl(inode, ACL_TYPE_ACCESS, acl);
  153. posix_acl_release(acl);
  154. }
  155. return error;
  156. }
  157. const struct xattr_handler generic_acl_access_handler = {
  158. .prefix = POSIX_ACL_XATTR_ACCESS,
  159. .flags = ACL_TYPE_ACCESS,
  160. .list = generic_acl_list,
  161. .get = generic_acl_get,
  162. .set = generic_acl_set,
  163. };
  164. const struct xattr_handler generic_acl_default_handler = {
  165. .prefix = POSIX_ACL_XATTR_DEFAULT,
  166. .flags = ACL_TYPE_DEFAULT,
  167. .list = generic_acl_list,
  168. .get = generic_acl_get,
  169. .set = generic_acl_set,
  170. };