xattr.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /*
  2. * Copyright IBM Corporation, 2010
  3. * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2.1 of the GNU Lesser General Public License
  7. * as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  12. *
  13. */
  14. #include <linux/module.h>
  15. #include <linux/fs.h>
  16. #include <linux/sched.h>
  17. #include <linux/uio.h>
  18. #include <net/9p/9p.h>
  19. #include <net/9p/client.h>
  20. #include "fid.h"
  21. #include "xattr.h"
  22. ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
  23. void *buffer, size_t buffer_size)
  24. {
  25. ssize_t retval;
  26. u64 attr_size;
  27. struct p9_fid *attr_fid;
  28. struct kvec kvec = {.iov_base = buffer, .iov_len = buffer_size};
  29. struct iov_iter to;
  30. int err;
  31. iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buffer_size);
  32. attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
  33. if (IS_ERR(attr_fid)) {
  34. retval = PTR_ERR(attr_fid);
  35. p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
  36. retval);
  37. return retval;
  38. }
  39. if (attr_size > buffer_size) {
  40. if (!buffer_size) /* request to get the attr_size */
  41. retval = attr_size;
  42. else
  43. retval = -ERANGE;
  44. } else {
  45. iov_iter_truncate(&to, attr_size);
  46. retval = p9_client_read(attr_fid, 0, &to, &err);
  47. if (err)
  48. retval = err;
  49. }
  50. p9_client_clunk(attr_fid);
  51. return retval;
  52. }
  53. /*
  54. * v9fs_xattr_get()
  55. *
  56. * Copy an extended attribute into the buffer
  57. * provided, or compute the buffer size required.
  58. * Buffer is NULL to compute the size of the buffer required.
  59. *
  60. * Returns a negative error number on failure, or the number of bytes
  61. * used / required on success.
  62. */
  63. ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
  64. void *buffer, size_t buffer_size)
  65. {
  66. struct p9_fid *fid;
  67. p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
  68. name, buffer_size);
  69. fid = v9fs_fid_lookup(dentry);
  70. if (IS_ERR(fid))
  71. return PTR_ERR(fid);
  72. return v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
  73. }
  74. /*
  75. * v9fs_xattr_set()
  76. *
  77. * Create, replace or remove an extended attribute for this inode. Buffer
  78. * is NULL to remove an existing extended attribute, and non-NULL to
  79. * either replace an existing extended attribute, or create a new extended
  80. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  81. * specify that an extended attribute must exist and must not exist
  82. * previous to the call, respectively.
  83. *
  84. * Returns 0, or a negative error number on failure.
  85. */
  86. int v9fs_xattr_set(struct dentry *dentry, const char *name,
  87. const void *value, size_t value_len, int flags)
  88. {
  89. struct p9_fid *fid = v9fs_fid_lookup(dentry);
  90. if (IS_ERR(fid))
  91. return PTR_ERR(fid);
  92. return v9fs_fid_xattr_set(fid, name, value, value_len, flags);
  93. }
  94. int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
  95. const void *value, size_t value_len, int flags)
  96. {
  97. struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
  98. struct iov_iter from;
  99. int retval, err;
  100. iov_iter_kvec(&from, WRITE | ITER_KVEC, &kvec, 1, value_len);
  101. p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
  102. name, value_len, flags);
  103. /* Clone it */
  104. fid = p9_client_walk(fid, 0, NULL, 1);
  105. if (IS_ERR(fid))
  106. return PTR_ERR(fid);
  107. /*
  108. * On success fid points to xattr
  109. */
  110. retval = p9_client_xattrcreate(fid, name, value_len, flags);
  111. if (retval < 0)
  112. p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
  113. retval);
  114. else
  115. p9_client_write(fid, 0, &from, &retval);
  116. err = p9_client_clunk(fid);
  117. if (!retval && err)
  118. retval = err;
  119. return retval;
  120. }
  121. ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  122. {
  123. return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
  124. }
  125. static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
  126. struct dentry *dentry, const char *name,
  127. void *buffer, size_t size)
  128. {
  129. const char *full_name = xattr_full_name(handler, name);
  130. if (strcmp(name, "") == 0)
  131. return -EINVAL;
  132. return v9fs_xattr_get(dentry, full_name, buffer, size);
  133. }
  134. static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
  135. struct dentry *dentry, const char *name,
  136. const void *value, size_t size, int flags)
  137. {
  138. const char *full_name = xattr_full_name(handler, name);
  139. if (strcmp(name, "") == 0)
  140. return -EINVAL;
  141. return v9fs_xattr_set(dentry, full_name, value, size, flags);
  142. }
  143. static struct xattr_handler v9fs_xattr_user_handler = {
  144. .prefix = XATTR_USER_PREFIX,
  145. .get = v9fs_xattr_handler_get,
  146. .set = v9fs_xattr_handler_set,
  147. };
  148. static struct xattr_handler v9fs_xattr_trusted_handler = {
  149. .prefix = XATTR_TRUSTED_PREFIX,
  150. .get = v9fs_xattr_handler_get,
  151. .set = v9fs_xattr_handler_set,
  152. };
  153. #ifdef CONFIG_9P_FS_SECURITY
  154. static struct xattr_handler v9fs_xattr_security_handler = {
  155. .prefix = XATTR_SECURITY_PREFIX,
  156. .get = v9fs_xattr_handler_get,
  157. .set = v9fs_xattr_handler_set,
  158. };
  159. #endif
  160. const struct xattr_handler *v9fs_xattr_handlers[] = {
  161. &v9fs_xattr_user_handler,
  162. &v9fs_xattr_trusted_handler,
  163. #ifdef CONFIG_9P_FS_POSIX_ACL
  164. &v9fs_xattr_acl_access_handler,
  165. &v9fs_xattr_acl_default_handler,
  166. #endif
  167. #ifdef CONFIG_9P_FS_SECURITY
  168. &v9fs_xattr_security_handler,
  169. #endif
  170. NULL
  171. };