9p-xattr.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. * 9p xattr callback
  3. *
  4. * Copyright IBM, Corp. 2010
  5. *
  6. * Authors:
  7. * Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. *
  12. */
  13. /*
  14. * Not so fast! You might want to read the 9p developer docs first:
  15. * https://wiki.qemu.org/Documentation/9p
  16. */
  17. #include "qemu/osdep.h"
  18. #include "9p.h"
  19. #include "fsdev/file-op-9p.h"
  20. #include "9p-xattr.h"
  21. #include "9p-util.h"
  22. #include "9p-local.h"
  23. static XattrOperations *get_xattr_operations(XattrOperations **h,
  24. const char *name)
  25. {
  26. XattrOperations *xops;
  27. for (xops = *(h)++; xops != NULL; xops = *(h)++) {
  28. if (!strncmp(name, xops->name, strlen(xops->name))) {
  29. return xops;
  30. }
  31. }
  32. return NULL;
  33. }
  34. ssize_t v9fs_get_xattr(FsContext *ctx, const char *path,
  35. const char *name, void *value, size_t size)
  36. {
  37. XattrOperations *xops = get_xattr_operations(ctx->xops, name);
  38. if (xops) {
  39. return xops->getxattr(ctx, path, name, value, size);
  40. }
  41. errno = EOPNOTSUPP;
  42. return -1;
  43. }
  44. ssize_t pt_listxattr(FsContext *ctx, const char *path,
  45. char *name, void *value, size_t size)
  46. {
  47. int name_size = strlen(name) + 1;
  48. if (!value) {
  49. return name_size;
  50. }
  51. if (size < name_size) {
  52. errno = ERANGE;
  53. return -1;
  54. }
  55. /* no need for strncpy: name_size is strlen(name)+1 */
  56. memcpy(value, name, name_size);
  57. return name_size;
  58. }
  59. /*
  60. * Get the list and pass to each layer to find out whether
  61. * to send the data or not
  62. */
  63. ssize_t v9fs_list_xattr(FsContext *ctx, const char *path,
  64. void *value, size_t vsize)
  65. {
  66. ssize_t size = 0;
  67. void *ovalue = value;
  68. XattrOperations *xops;
  69. char *orig_value, *orig_value_start;
  70. ssize_t xattr_len, parsed_len = 0, attr_len;
  71. char *dirpath, *name;
  72. int dirfd;
  73. /* Get the actual len */
  74. dirpath = g_path_get_dirname(path);
  75. dirfd = local_opendir_nofollow(ctx, dirpath);
  76. g_free(dirpath);
  77. if (dirfd == -1) {
  78. return -1;
  79. }
  80. name = g_path_get_basename(path);
  81. xattr_len = flistxattrat_nofollow(dirfd, name, value, 0);
  82. if (xattr_len <= 0) {
  83. g_free(name);
  84. close_preserve_errno(dirfd);
  85. return xattr_len;
  86. }
  87. /* Now fetch the xattr and find the actual size */
  88. orig_value = g_malloc(xattr_len);
  89. xattr_len = flistxattrat_nofollow(dirfd, name, orig_value, xattr_len);
  90. g_free(name);
  91. close_preserve_errno(dirfd);
  92. if (xattr_len < 0) {
  93. g_free(orig_value);
  94. return -1;
  95. }
  96. /* store the orig pointer */
  97. orig_value_start = orig_value;
  98. while (xattr_len > parsed_len) {
  99. xops = get_xattr_operations(ctx->xops, orig_value);
  100. if (!xops) {
  101. goto next_entry;
  102. }
  103. if (!value) {
  104. size += xops->listxattr(ctx, path, orig_value, value, vsize);
  105. } else {
  106. size = xops->listxattr(ctx, path, orig_value, value, vsize);
  107. if (size < 0) {
  108. goto err_out;
  109. }
  110. value += size;
  111. vsize -= size;
  112. }
  113. next_entry:
  114. /* Got the next entry */
  115. attr_len = strlen(orig_value) + 1;
  116. parsed_len += attr_len;
  117. orig_value += attr_len;
  118. }
  119. if (value) {
  120. size = value - ovalue;
  121. }
  122. err_out:
  123. g_free(orig_value_start);
  124. return size;
  125. }
  126. int v9fs_set_xattr(FsContext *ctx, const char *path, const char *name,
  127. void *value, size_t size, int flags)
  128. {
  129. XattrOperations *xops = get_xattr_operations(ctx->xops, name);
  130. if (xops) {
  131. return xops->setxattr(ctx, path, name, value, size, flags);
  132. }
  133. errno = EOPNOTSUPP;
  134. return -1;
  135. }
  136. int v9fs_remove_xattr(FsContext *ctx,
  137. const char *path, const char *name)
  138. {
  139. XattrOperations *xops = get_xattr_operations(ctx->xops, name);
  140. if (xops) {
  141. return xops->removexattr(ctx, path, name);
  142. }
  143. errno = EOPNOTSUPP;
  144. return -1;
  145. }
  146. ssize_t local_getxattr_nofollow(FsContext *ctx, const char *path,
  147. const char *name, void *value, size_t size)
  148. {
  149. char *dirpath = g_path_get_dirname(path);
  150. char *filename = g_path_get_basename(path);
  151. int dirfd;
  152. ssize_t ret = -1;
  153. dirfd = local_opendir_nofollow(ctx, dirpath);
  154. if (dirfd == -1) {
  155. goto out;
  156. }
  157. ret = fgetxattrat_nofollow(dirfd, filename, name, value, size);
  158. close_preserve_errno(dirfd);
  159. out:
  160. g_free(dirpath);
  161. g_free(filename);
  162. return ret;
  163. }
  164. ssize_t pt_getxattr(FsContext *ctx, const char *path, const char *name,
  165. void *value, size_t size)
  166. {
  167. return local_getxattr_nofollow(ctx, path, name, value, size);
  168. }
  169. ssize_t local_setxattr_nofollow(FsContext *ctx, const char *path,
  170. const char *name, void *value, size_t size,
  171. int flags)
  172. {
  173. char *dirpath = g_path_get_dirname(path);
  174. char *filename = g_path_get_basename(path);
  175. int dirfd;
  176. ssize_t ret = -1;
  177. dirfd = local_opendir_nofollow(ctx, dirpath);
  178. if (dirfd == -1) {
  179. goto out;
  180. }
  181. ret = fsetxattrat_nofollow(dirfd, filename, name, value, size, flags);
  182. close_preserve_errno(dirfd);
  183. out:
  184. g_free(dirpath);
  185. g_free(filename);
  186. return ret;
  187. }
  188. int pt_setxattr(FsContext *ctx, const char *path, const char *name, void *value,
  189. size_t size, int flags)
  190. {
  191. return local_setxattr_nofollow(ctx, path, name, value, size, flags);
  192. }
  193. ssize_t local_removexattr_nofollow(FsContext *ctx, const char *path,
  194. const char *name)
  195. {
  196. char *dirpath = g_path_get_dirname(path);
  197. char *filename = g_path_get_basename(path);
  198. int dirfd;
  199. ssize_t ret = -1;
  200. dirfd = local_opendir_nofollow(ctx, dirpath);
  201. if (dirfd == -1) {
  202. goto out;
  203. }
  204. ret = fremovexattrat_nofollow(dirfd, filename, name);
  205. close_preserve_errno(dirfd);
  206. out:
  207. g_free(dirpath);
  208. g_free(filename);
  209. return ret;
  210. }
  211. int pt_removexattr(FsContext *ctx, const char *path, const char *name)
  212. {
  213. return local_removexattr_nofollow(ctx, path, name);
  214. }
  215. ssize_t notsup_getxattr(FsContext *ctx, const char *path, const char *name,
  216. void *value, size_t size)
  217. {
  218. errno = ENOTSUP;
  219. return -1;
  220. }
  221. int notsup_setxattr(FsContext *ctx, const char *path, const char *name,
  222. void *value, size_t size, int flags)
  223. {
  224. errno = ENOTSUP;
  225. return -1;
  226. }
  227. ssize_t notsup_listxattr(FsContext *ctx, const char *path, char *name,
  228. void *value, size_t size)
  229. {
  230. return 0;
  231. }
  232. int notsup_removexattr(FsContext *ctx, const char *path, const char *name)
  233. {
  234. errno = ENOTSUP;
  235. return -1;
  236. }
  237. XattrOperations *mapped_xattr_ops[] = {
  238. &mapped_user_xattr,
  239. &mapped_pacl_xattr,
  240. &mapped_dacl_xattr,
  241. NULL,
  242. };
  243. XattrOperations *passthrough_xattr_ops[] = {
  244. &passthrough_user_xattr,
  245. &passthrough_acl_xattr,
  246. NULL,
  247. };
  248. /* for .user none model should be same as passthrough */
  249. XattrOperations *none_xattr_ops[] = {
  250. &passthrough_user_xattr,
  251. &none_acl_xattr,
  252. NULL,
  253. };