xattr.c 4.0 KB

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