eaops.c 5.4 KB

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