generic_acl.c 3.8 KB

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