eaops.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  3. * Copyright (C) 2004-2005 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/semaphore.h>
  17. #include <asm/uaccess.h>
  18. #include "gfs2.h"
  19. #include "lm_interface.h"
  20. #include "incore.h"
  21. #include "acl.h"
  22. #include "eaops.h"
  23. #include "eattr.h"
  24. #include "util.h"
  25. /**
  26. * gfs2_ea_name2type - get the type of the ea, and truncate type from the name
  27. * @namep: ea name, possibly with type appended
  28. *
  29. * Returns: GFS2_EATYPE_XXX
  30. */
  31. unsigned int gfs2_ea_name2type(const char *name, char **truncated_name)
  32. {
  33. unsigned int type;
  34. if (strncmp(name, "system.", 7) == 0) {
  35. type = GFS2_EATYPE_SYS;
  36. if (truncated_name)
  37. *truncated_name = strchr(name, '.') + 1;
  38. } else if (strncmp(name, "user.", 5) == 0) {
  39. type = GFS2_EATYPE_USR;
  40. if (truncated_name)
  41. *truncated_name = strchr(name, '.') + 1;
  42. } else {
  43. type = GFS2_EATYPE_UNUSED;
  44. if (truncated_name)
  45. *truncated_name = NULL;
  46. }
  47. return type;
  48. }
  49. static int user_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  50. {
  51. struct inode *inode = ip->i_vnode;
  52. int error = permission(inode, MAY_READ, NULL);
  53. if (error)
  54. return error;
  55. return gfs2_ea_get_i(ip, er);
  56. }
  57. static int user_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  58. {
  59. struct inode *inode = ip->i_vnode;
  60. if (S_ISREG(inode->i_mode) ||
  61. (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) {
  62. int error = permission(inode, MAY_WRITE, NULL);
  63. if (error)
  64. return error;
  65. } else
  66. return -EPERM;
  67. return gfs2_ea_set_i(ip, er);
  68. }
  69. static int user_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  70. {
  71. struct inode *inode = ip->i_vnode;
  72. if (S_ISREG(inode->i_mode) ||
  73. (S_ISDIR(inode->i_mode) && !(inode->i_mode & S_ISVTX))) {
  74. int error = permission(inode, MAY_WRITE, NULL);
  75. if (error)
  76. return error;
  77. } else
  78. return -EPERM;
  79. return gfs2_ea_remove_i(ip, er);
  80. }
  81. static int system_eo_get(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  82. {
  83. if (!GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) &&
  84. !GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len) &&
  85. !capable(CAP_SYS_ADMIN))
  86. return -EPERM;
  87. if (ip->i_sbd->sd_args.ar_posix_acl == 0 &&
  88. (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len) ||
  89. GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)))
  90. return -EOPNOTSUPP;
  91. return gfs2_ea_get_i(ip, er);
  92. }
  93. static int system_eo_set(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  94. {
  95. int remove = 0;
  96. int error;
  97. if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
  98. if (!(er->er_flags & GFS2_ERF_MODE)) {
  99. er->er_mode = ip->i_di.di_mode;
  100. er->er_flags |= GFS2_ERF_MODE;
  101. }
  102. error = gfs2_acl_validate_set(ip, 1, er,
  103. &remove, &er->er_mode);
  104. if (error)
  105. return error;
  106. error = gfs2_ea_set_i(ip, er);
  107. if (error)
  108. return error;
  109. if (remove)
  110. gfs2_ea_remove_i(ip, er);
  111. return 0;
  112. } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
  113. error = gfs2_acl_validate_set(ip, 0, er,
  114. &remove, NULL);
  115. if (error)
  116. return error;
  117. if (!remove)
  118. error = gfs2_ea_set_i(ip, er);
  119. else {
  120. error = gfs2_ea_remove_i(ip, er);
  121. if (error == -ENODATA)
  122. error = 0;
  123. }
  124. return error;
  125. }
  126. return -EPERM;
  127. }
  128. static int system_eo_remove(struct gfs2_inode *ip, struct gfs2_ea_request *er)
  129. {
  130. if (GFS2_ACL_IS_ACCESS(er->er_name, er->er_name_len)) {
  131. int error = gfs2_acl_validate_remove(ip, 1);
  132. if (error)
  133. return error;
  134. } else if (GFS2_ACL_IS_DEFAULT(er->er_name, er->er_name_len)) {
  135. int error = gfs2_acl_validate_remove(ip, 0);
  136. if (error)
  137. return error;
  138. } else
  139. return -EPERM;
  140. return gfs2_ea_remove_i(ip, er);
  141. }
  142. struct gfs2_eattr_operations gfs2_user_eaops = {
  143. .eo_get = user_eo_get,
  144. .eo_set = user_eo_set,
  145. .eo_remove = user_eo_remove,
  146. .eo_name = "user",
  147. };
  148. struct gfs2_eattr_operations gfs2_system_eaops = {
  149. .eo_get = system_eo_get,
  150. .eo_set = system_eo_set,
  151. .eo_remove = system_eo_remove,
  152. .eo_name = "system",
  153. };
  154. struct gfs2_eattr_operations *gfs2_ea_ops[] = {
  155. NULL,
  156. &gfs2_user_eaops,
  157. &gfs2_system_eaops,
  158. };