eaops.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 v.2.
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/slab.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/completion.h>
  13. #include <linux/buffer_head.h>
  14. #include <linux/xattr.h>
  15. #include <linux/gfs2_ondisk.h>
  16. #include <asm/uaccess.h>
  17. #include "gfs2.h"
  18. #include "lm_interface.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, 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 = strchr(name, '.') + 1;
  37. } else if (strncmp(name, "user.", 5) == 0) {
  38. type = GFS2_EATYPE_USR;
  39. if (truncated_name)
  40. *truncated_name = strchr(name, '.') + 1;
  41. } else {
  42. type = GFS2_EATYPE_UNUSED;
  43. if (truncated_name)
  44. *truncated_name = NULL;
  45. }
  46. return type;
  47. }
  48. static int user_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  49. {
  50. struct inode *inode = ip->i_vnode;
  51. int error = permission(inode, MAY_READ, NULL);
  52. if (error)
  53. return error;
  54. return gfs2_ea_get_i(ip, er);
  55. }
  56. static int user_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  57. {
  58. struct inode *inode = ip->i_vnode;
  59. if (S_ISREG(inode->i_mode) ||
  60. (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) {
  61. int error = permission(inode, MAY_WRITE, NULL);
  62. if (error)
  63. return error;
  64. } else
  65. return -EPERM;
  66. return gfs2_ea_set_i(ip, er);
  67. }
  68. static int user_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  69. {
  70. struct inode *inode = ip->i_vnode;
  71. if (S_ISREG(inode->i_mode) ||
  72. (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) {
  73. int error = permission(inode, MAY_WRITE, NULL);
  74. if (error)
  75. return error;
  76. } else
  77. return -EPERM;
  78. return gfs2_ea_remove_i(ip, er);
  79. }
  80. static int system_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  81. {
  82. if (!GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) &&
  83. !GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len) &&
  84. !capable(CAP_SYS_ADMIN))
  85. return -EPERM;
  86. if (ip->i_sbd->sd_args.ar_posix_acl == 0 &&
  87. (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) ||
  88. GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)))
  89. return -EOPNOTSUPP;
  90. return gfs2_ea_get_i(ip, er);
  91. }
  92. static int system_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  93. {
  94. int remove = 0;
  95. int error;
  96. if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
  97. if (!(er->er_flags & GFS2_ERF_MODE)) {
  98. er->er_mode = ip->i_di.di_mode;
  99. er->er_flags |= GFS2_ERF_MODE;
  100. }
  101. error = gfs2_acl_validate_set(ip, 1, er,
  102. &remove, &er->er_mode);
  103. if (error)
  104. return error;
  105. error = gfs2_ea_set_i(ip, er);
  106. if (error)
  107. return error;
  108. if (remove)
  109. gfs2_ea_remove_i(ip, er);
  110. return 0;
  111. } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
  112. error = gfs2_acl_validate_set(ip, 0, er,
  113. &remove, NULL);
  114. if (error)
  115. return error;
  116. if (!remove)
  117. error = gfs2_ea_set_i(ip, er);
  118. else {
  119. error = gfs2_ea_remove_i(ip, er);
  120. if (error == -ENODATA)
  121. error = 0;
  122. }
  123. return error;
  124. }
  125. return -EPERM;
  126. }
  127. static int system_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  128. {
  129. if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
  130. int error = gfs2_acl_validate_remove(ip, 1);
  131. if (error)
  132. return error;
  133. } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
  134. int error = gfs2_acl_validate_remove(ip, 0);
  135. if (error)
  136. return error;
  137. } else
  138. return -EPERM;
  139. return gfs2_ea_remove_i(ip, er);
  140. }
  141. static struct gfs2_eattr_operations gfs2_user_eaops = {
  142. .eo_get = user_eo_get,
  143. .eo_set = user_eo_set,
  144. .eo_remove = user_eo_remove,
  145. .eo_name = "user",
  146. };
  147. struct gfs2_eattr_operations gfs2_system_eaops = {
  148. .eo_get = system_eo_get,
  149. .eo_set = system_eo_set,
  150. .eo_remove = system_eo_remove,
  151. .eo_name = "system",
  152. };
  153. struct gfs2_eattr_operations *gfs2_ea_ops[] = {
  154. NULL,
  155. &gfs2_user_eaops,
  156. &gfs2_system_eaops,
  157. };