eaops.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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/slab.h>
  10. #include <linux/spinlock.h>
  11. #include <linux/completion.h>
  12. #include <linux/buffer_head.h>
  13. #include <linux/capability.h>
  14. #include <linux/xattr.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <linux/lm_interface.h>
  17. #include <asm/uaccess.h>
  18. #include "gfs2.h"
  19. #include "incore.h"
  20. #include "acl.h"
  21. #include "eaops.h"
  22. #include "eattr.h"
  23. #include "util.h"
  24. /**
  25. * gfs2_ea_name2type - get the type of the ea, and truncate type from the name
  26. * @namep: ea name, possibly with type appended
  27. *
  28. * Returns: GFS2_EATYPE_XXX
  29. */
  30. unsigned int gfs2_ea_name2type(const char *name, const char **truncated_name)
  31. {
  32. unsigned int type;
  33. if (strncmp(name, "system.", 7) == 0) {
  34. type = GFS2_EATYPE_SYS;
  35. if (truncated_name)
  36. *truncated_name = name + sizeof("system.") - 1;
  37. } else if (strncmp(name, "user.", 5) == 0) {
  38. type = GFS2_EATYPE_USR;
  39. if (truncated_name)
  40. *truncated_name = name + sizeof("user.") - 1;
  41. } else if (strncmp(name, "security.", 9) == 0) {
  42. type = GFS2_EATYPE_SECURITY;
  43. if (truncated_name)
  44. *truncated_name = name + sizeof("security.") - 1;
  45. } else {
  46. type = GFS2_EATYPE_UNUSED;
  47. if (truncated_name)
  48. *truncated_name = NULL;
  49. }
  50. return type;
  51. }
  52. static int user_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  53. {
  54. struct inode *inode = &ip->i_inode;
  55. int error = permission(inode, MAY_READ, NULL);
  56. if (error)
  57. return error;
  58. return gfs2_ea_get_i(ip, er);
  59. }
  60. static int user_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  61. {
  62. struct inode *inode = &ip->i_inode;
  63. if (S_ISREG(inode->i_mode) ||
  64. (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) {
  65. int error = permission(inode, MAY_WRITE, NULL);
  66. if (error)
  67. return error;
  68. } else
  69. return -EPERM;
  70. return gfs2_ea_set_i(ip, er);
  71. }
  72. static int user_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  73. {
  74. struct inode *inode = &ip->i_inode;
  75. if (S_ISREG(inode->i_mode) ||
  76. (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) {
  77. int error = permission(inode, MAY_WRITE, NULL);
  78. if (error)
  79. return error;
  80. } else
  81. return -EPERM;
  82. return gfs2_ea_remove_i(ip, er);
  83. }
  84. static int system_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  85. {
  86. if (!GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) &&
  87. !GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len) &&
  88. !capable(CAP_SYS_ADMIN))
  89. return -EPERM;
  90. if (GFS2_SB(&ip->i_inode)->sd_args.ar_posix_acl == 0 &&
  91. (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) ||
  92. GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)))
  93. return -EOPNOTSUPP;
  94. return gfs2_ea_get_i(ip, er);
  95. }
  96. static int system_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  97. {
  98. int remove = 0;
  99. int error;
  100. if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
  101. if (!(er->er_flags & GFS2_ERF_MODE)) {
  102. er->er_mode = ip->i_inode.i_mode;
  103. er->er_flags |= GFS2_ERF_MODE;
  104. }
  105. error = gfs2_acl_validate_set(ip, 1, er,
  106. &remove, &er->er_mode);
  107. if (error)
  108. return error;
  109. error = gfs2_ea_set_i(ip, er);
  110. if (error)
  111. return error;
  112. if (remove)
  113. gfs2_ea_remove_i(ip, er);
  114. return 0;
  115. } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
  116. error = gfs2_acl_validate_set(ip, 0, er,
  117. &remove, NULL);
  118. if (error)
  119. return error;
  120. if (!remove)
  121. error = gfs2_ea_set_i(ip, er);
  122. else {
  123. error = gfs2_ea_remove_i(ip, er);
  124. if (error == -ENODATA)
  125. error = 0;
  126. }
  127. return error;
  128. }
  129. return -EPERM;
  130. }
  131. static int system_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  132. {
  133. if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
  134. int error = gfs2_acl_validate_remove(ip, 1);
  135. if (error)
  136. return error;
  137. } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
  138. int error = gfs2_acl_validate_remove(ip, 0);
  139. if (error)
  140. return error;
  141. } else
  142. return -EPERM;
  143. return gfs2_ea_remove_i(ip, er);
  144. }
  145. static int security_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  146. {
  147. struct inode *inode = &ip->i_inode;
  148. int error = permission(inode, MAY_READ, NULL);
  149. if (error)
  150. return error;
  151. return gfs2_ea_get_i(ip, er);
  152. }
  153. static int security_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  154. {
  155. struct inode *inode = &ip->i_inode;
  156. int error = permission(inode, MAY_WRITE, NULL);
  157. if (error)
  158. return error;
  159. return gfs2_ea_set_i(ip, er);
  160. }
  161. static int security_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  162. {
  163. struct inode *inode = &ip->i_inode;
  164. int error = permission(inode, MAY_WRITE, NULL);
  165. if (error)
  166. return error;
  167. return gfs2_ea_remove_i(ip, er);
  168. }
  169. static const struct gfs2_eattr_operations gfs2_user_eaops = {
  170. .eo_get = user_eo_get,
  171. .eo_set = user_eo_set,
  172. .eo_remove = user_eo_remove,
  173. .eo_name = "user",
  174. };
  175. const struct gfs2_eattr_operations gfs2_system_eaops = {
  176. .eo_get = system_eo_get,
  177. .eo_set = system_eo_set,
  178. .eo_remove = system_eo_remove,
  179. .eo_name = "system",
  180. };
  181. static const struct gfs2_eattr_operations gfs2_security_eaops = {
  182. .eo_get = security_eo_get,
  183. .eo_set = security_eo_set,
  184. .eo_remove = security_eo_remove,
  185. .eo_name = "security",
  186. };
  187. const struct gfs2_eattr_operations *gfs2_ea_ops[] = {
  188. NULL,
  189. &gfs2_user_eaops,
  190. &gfs2_system_eaops,
  191. &gfs2_security_eaops,
  192. };