xattr.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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, &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. return v9fs_fid_xattr_set(fid, name, value, value_len, flags);
  91. }
  92. int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
  93. const void *value, size_t value_len, int flags)
  94. {
  95. struct kvec kvec = {.iov_base = (void *)value, .iov_len = value_len};
  96. struct iov_iter from;
  97. int retval, err;
  98. iov_iter_kvec(&from, WRITE, &kvec, 1, value_len);
  99. p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
  100. name, value_len, flags);
  101. /* Clone it */
  102. fid = clone_fid(fid);
  103. if (IS_ERR(fid))
  104. return PTR_ERR(fid);
  105. /*
  106. * On success fid points to xattr
  107. */
  108. retval = p9_client_xattrcreate(fid, name, value_len, flags);
  109. if (retval < 0)
  110. p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
  111. retval);
  112. else
  113. p9_client_write(fid, 0, &from, &retval);
  114. err = p9_client_clunk(fid);
  115. if (!retval && err)
  116. retval = err;
  117. return retval;
  118. }
  119. ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  120. {
  121. return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
  122. }
  123. static int v9fs_xattr_handler_get(const struct xattr_handler *handler,
  124. struct dentry *dentry, struct inode *inode,
  125. const char *name, void *buffer, size_t size,
  126. int flags)
  127. {
  128. const char *full_name = xattr_full_name(handler, name);
  129. return v9fs_xattr_get(dentry, full_name, buffer, size);
  130. }
  131. static int v9fs_xattr_handler_set(const struct xattr_handler *handler,
  132. struct dentry *dentry, struct inode *inode,
  133. const char *name, const void *value,
  134. size_t size, int flags)
  135. {
  136. const char *full_name = xattr_full_name(handler, name);
  137. return v9fs_xattr_set(dentry, full_name, value, size, flags);
  138. }
  139. static struct xattr_handler v9fs_xattr_user_handler = {
  140. .prefix = XATTR_USER_PREFIX,
  141. .get = v9fs_xattr_handler_get,
  142. .set = v9fs_xattr_handler_set,
  143. };
  144. static struct xattr_handler v9fs_xattr_trusted_handler = {
  145. .prefix = XATTR_TRUSTED_PREFIX,
  146. .get = v9fs_xattr_handler_get,
  147. .set = v9fs_xattr_handler_set,
  148. };
  149. #ifdef CONFIG_9P_FS_SECURITY
  150. static struct xattr_handler v9fs_xattr_security_handler = {
  151. .prefix = XATTR_SECURITY_PREFIX,
  152. .get = v9fs_xattr_handler_get,
  153. .set = v9fs_xattr_handler_set,
  154. };
  155. #endif
  156. const struct xattr_handler *v9fs_xattr_handlers[] = {
  157. &v9fs_xattr_user_handler,
  158. &v9fs_xattr_trusted_handler,
  159. #ifdef CONFIG_9P_FS_POSIX_ACL
  160. &v9fs_xattr_acl_access_handler,
  161. &v9fs_xattr_acl_default_handler,
  162. #endif
  163. #ifdef CONFIG_9P_FS_SECURITY
  164. &v9fs_xattr_security_handler,
  165. #endif
  166. NULL
  167. };