xattr.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 <net/9p/9p.h>
  18. #include <net/9p/client.h>
  19. #include "fid.h"
  20. #include "xattr.h"
  21. ssize_t v9fs_fid_xattr_get(struct p9_fid *fid, const char *name,
  22. void *buffer, size_t buffer_size)
  23. {
  24. ssize_t retval;
  25. int msize, read_count;
  26. u64 offset = 0, attr_size;
  27. struct p9_fid *attr_fid;
  28. attr_fid = p9_client_xattrwalk(fid, name, &attr_size);
  29. if (IS_ERR(attr_fid)) {
  30. retval = PTR_ERR(attr_fid);
  31. p9_debug(P9_DEBUG_VFS, "p9_client_attrwalk failed %zd\n",
  32. retval);
  33. attr_fid = NULL;
  34. goto error;
  35. }
  36. if (!buffer_size) {
  37. /* request to get the attr_size */
  38. retval = attr_size;
  39. goto error;
  40. }
  41. if (attr_size > buffer_size) {
  42. retval = -ERANGE;
  43. goto error;
  44. }
  45. msize = attr_fid->clnt->msize;
  46. while (attr_size) {
  47. if (attr_size > (msize - P9_IOHDRSZ))
  48. read_count = msize - P9_IOHDRSZ;
  49. else
  50. read_count = attr_size;
  51. read_count = p9_client_read(attr_fid, ((char *)buffer)+offset,
  52. NULL, offset, read_count);
  53. if (read_count < 0) {
  54. /* error in xattr read */
  55. retval = read_count;
  56. goto error;
  57. }
  58. offset += read_count;
  59. attr_size -= read_count;
  60. }
  61. /* Total read xattr bytes */
  62. retval = offset;
  63. error:
  64. if (attr_fid)
  65. p9_client_clunk(attr_fid);
  66. return retval;
  67. }
  68. /*
  69. * v9fs_xattr_get()
  70. *
  71. * Copy an extended attribute into the buffer
  72. * provided, or compute the buffer size required.
  73. * Buffer is NULL to compute the size of the buffer required.
  74. *
  75. * Returns a negative error number on failure, or the number of bytes
  76. * used / required on success.
  77. */
  78. ssize_t v9fs_xattr_get(struct dentry *dentry, const char *name,
  79. void *buffer, size_t buffer_size)
  80. {
  81. struct p9_fid *fid;
  82. p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu\n",
  83. name, buffer_size);
  84. fid = v9fs_fid_lookup(dentry);
  85. if (IS_ERR(fid))
  86. return PTR_ERR(fid);
  87. return v9fs_fid_xattr_get(fid, name, buffer, buffer_size);
  88. }
  89. /*
  90. * v9fs_xattr_set()
  91. *
  92. * Create, replace or remove an extended attribute for this inode. Buffer
  93. * is NULL to remove an existing extended attribute, and non-NULL to
  94. * either replace an existing extended attribute, or create a new extended
  95. * attribute. The flags XATTR_REPLACE and XATTR_CREATE
  96. * specify that an extended attribute must exist and must not exist
  97. * previous to the call, respectively.
  98. *
  99. * Returns 0, or a negative error number on failure.
  100. */
  101. int v9fs_xattr_set(struct dentry *dentry, const char *name,
  102. const void *value, size_t value_len, int flags)
  103. {
  104. struct p9_fid *fid = v9fs_fid_lookup(dentry);
  105. if (IS_ERR(fid))
  106. return PTR_ERR(fid);
  107. return v9fs_fid_xattr_set(fid, name, value, value_len, flags);
  108. }
  109. int v9fs_fid_xattr_set(struct p9_fid *fid, const char *name,
  110. const void *value, size_t value_len, int flags)
  111. {
  112. u64 offset = 0;
  113. int retval, msize, write_count;
  114. p9_debug(P9_DEBUG_VFS, "name = %s value_len = %zu flags = %d\n",
  115. name, value_len, flags);
  116. /* Clone it */
  117. fid = p9_client_walk(fid, 0, NULL, 1);
  118. if (IS_ERR(fid))
  119. return PTR_ERR(fid);
  120. /*
  121. * On success fid points to xattr
  122. */
  123. retval = p9_client_xattrcreate(fid, name, value_len, flags);
  124. if (retval < 0) {
  125. p9_debug(P9_DEBUG_VFS, "p9_client_xattrcreate failed %d\n",
  126. retval);
  127. p9_client_clunk(fid);
  128. return retval;
  129. }
  130. msize = fid->clnt->msize;
  131. while (value_len) {
  132. if (value_len > (msize - P9_IOHDRSZ))
  133. write_count = msize - P9_IOHDRSZ;
  134. else
  135. write_count = value_len;
  136. write_count = p9_client_write(fid, ((char *)value)+offset,
  137. NULL, offset, write_count);
  138. if (write_count < 0) {
  139. /* error in xattr write */
  140. retval = write_count;
  141. break;
  142. }
  143. offset += write_count;
  144. value_len -= write_count;
  145. }
  146. return p9_client_clunk(fid);
  147. }
  148. ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  149. {
  150. return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
  151. }
  152. const struct xattr_handler *v9fs_xattr_handlers[] = {
  153. &v9fs_xattr_user_handler,
  154. #ifdef CONFIG_9P_FS_POSIX_ACL
  155. &v9fs_xattr_acl_access_handler,
  156. &v9fs_xattr_acl_default_handler,
  157. #endif
  158. NULL
  159. };