xattr.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_DPRINTK(P9_DEBUG_VFS,
  32. "p9_client_attrwalk failed %zd\n", 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_DPRINTK(P9_DEBUG_VFS, "%s: name = %s value_len = %zu\n",
  83. __func__, 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. u64 offset = 0;
  105. int retval, msize, write_count;
  106. struct p9_fid *fid = NULL;
  107. P9_DPRINTK(P9_DEBUG_VFS, "%s: name = %s value_len = %zu flags = %d\n",
  108. __func__, name, value_len, flags);
  109. fid = v9fs_fid_clone(dentry);
  110. if (IS_ERR(fid)) {
  111. retval = PTR_ERR(fid);
  112. fid = NULL;
  113. goto error;
  114. }
  115. /*
  116. * On success fid points to xattr
  117. */
  118. retval = p9_client_xattrcreate(fid, name, value_len, flags);
  119. if (retval < 0) {
  120. P9_DPRINTK(P9_DEBUG_VFS,
  121. "p9_client_xattrcreate failed %d\n", retval);
  122. goto error;
  123. }
  124. msize = fid->clnt->msize;
  125. while (value_len) {
  126. if (value_len > (msize - P9_IOHDRSZ))
  127. write_count = msize - P9_IOHDRSZ;
  128. else
  129. write_count = value_len;
  130. write_count = p9_client_write(fid, ((char *)value)+offset,
  131. NULL, offset, write_count);
  132. if (write_count < 0) {
  133. /* error in xattr write */
  134. retval = write_count;
  135. goto error;
  136. }
  137. offset += write_count;
  138. value_len -= write_count;
  139. }
  140. /* Total read xattr bytes */
  141. retval = offset;
  142. error:
  143. if (fid)
  144. retval = p9_client_clunk(fid);
  145. return retval;
  146. }
  147. ssize_t v9fs_listxattr(struct dentry *dentry, char *buffer, size_t buffer_size)
  148. {
  149. return v9fs_xattr_get(dentry, NULL, buffer, buffer_size);
  150. }
  151. const struct xattr_handler *v9fs_xattr_handlers[] = {
  152. &v9fs_xattr_user_handler,
  153. #ifdef CONFIG_9P_FS_POSIX_ACL
  154. &v9fs_xattr_acl_access_handler,
  155. &v9fs_xattr_acl_default_handler,
  156. #endif
  157. NULL
  158. };