xfs_xattr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * Copyright (C) 2008 Christoph Hellwig.
  3. * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "xfs.h"
  19. #include "xfs_log_format.h"
  20. #include "xfs_da_btree.h"
  21. #include "xfs_bmap_btree.h"
  22. #include "xfs_inode.h"
  23. #include "xfs_attr.h"
  24. #include "xfs_attr_leaf.h"
  25. #include "xfs_acl.h"
  26. #include "xfs_vnodeops.h"
  27. #include <linux/posix_acl_xattr.h>
  28. #include <linux/xattr.h>
  29. static int
  30. xfs_xattr_get(struct dentry *dentry, const char *name,
  31. void *value, size_t size, int xflags)
  32. {
  33. struct xfs_inode *ip = XFS_I(dentry->d_inode);
  34. int error, asize = size;
  35. if (strcmp(name, "") == 0)
  36. return -EINVAL;
  37. /* Convert Linux syscall to XFS internal ATTR flags */
  38. if (!size) {
  39. xflags |= ATTR_KERNOVAL;
  40. value = NULL;
  41. }
  42. error = -xfs_attr_get(ip, (unsigned char *)name, value, &asize, xflags);
  43. if (error)
  44. return error;
  45. return asize;
  46. }
  47. static int
  48. xfs_xattr_set(struct dentry *dentry, const char *name, const void *value,
  49. size_t size, int flags, int xflags)
  50. {
  51. struct xfs_inode *ip = XFS_I(dentry->d_inode);
  52. if (strcmp(name, "") == 0)
  53. return -EINVAL;
  54. /* Convert Linux syscall to XFS internal ATTR flags */
  55. if (flags & XATTR_CREATE)
  56. xflags |= ATTR_CREATE;
  57. if (flags & XATTR_REPLACE)
  58. xflags |= ATTR_REPLACE;
  59. if (!value)
  60. return -xfs_attr_remove(ip, (unsigned char *)name, xflags);
  61. return -xfs_attr_set(ip, (unsigned char *)name,
  62. (void *)value, size, xflags);
  63. }
  64. static const struct xattr_handler xfs_xattr_user_handler = {
  65. .prefix = XATTR_USER_PREFIX,
  66. .flags = 0, /* no flags implies user namespace */
  67. .get = xfs_xattr_get,
  68. .set = xfs_xattr_set,
  69. };
  70. static const struct xattr_handler xfs_xattr_trusted_handler = {
  71. .prefix = XATTR_TRUSTED_PREFIX,
  72. .flags = ATTR_ROOT,
  73. .get = xfs_xattr_get,
  74. .set = xfs_xattr_set,
  75. };
  76. static const struct xattr_handler xfs_xattr_security_handler = {
  77. .prefix = XATTR_SECURITY_PREFIX,
  78. .flags = ATTR_SECURE,
  79. .get = xfs_xattr_get,
  80. .set = xfs_xattr_set,
  81. };
  82. const struct xattr_handler *xfs_xattr_handlers[] = {
  83. &xfs_xattr_user_handler,
  84. &xfs_xattr_trusted_handler,
  85. &xfs_xattr_security_handler,
  86. #ifdef CONFIG_XFS_POSIX_ACL
  87. &xfs_xattr_acl_access_handler,
  88. &xfs_xattr_acl_default_handler,
  89. #endif
  90. NULL
  91. };
  92. static unsigned int xfs_xattr_prefix_len(int flags)
  93. {
  94. if (flags & XFS_ATTR_SECURE)
  95. return sizeof("security");
  96. else if (flags & XFS_ATTR_ROOT)
  97. return sizeof("trusted");
  98. else
  99. return sizeof("user");
  100. }
  101. static const char *xfs_xattr_prefix(int flags)
  102. {
  103. if (flags & XFS_ATTR_SECURE)
  104. return xfs_xattr_security_handler.prefix;
  105. else if (flags & XFS_ATTR_ROOT)
  106. return xfs_xattr_trusted_handler.prefix;
  107. else
  108. return xfs_xattr_user_handler.prefix;
  109. }
  110. static int
  111. xfs_xattr_put_listent(
  112. struct xfs_attr_list_context *context,
  113. int flags,
  114. unsigned char *name,
  115. int namelen,
  116. int valuelen,
  117. unsigned char *value)
  118. {
  119. unsigned int prefix_len = xfs_xattr_prefix_len(flags);
  120. char *offset;
  121. int arraytop;
  122. ASSERT(context->count >= 0);
  123. /*
  124. * Only show root namespace entries if we are actually allowed to
  125. * see them.
  126. */
  127. if ((flags & XFS_ATTR_ROOT) && !capable(CAP_SYS_ADMIN))
  128. return 0;
  129. arraytop = context->count + prefix_len + namelen + 1;
  130. if (arraytop > context->firstu) {
  131. context->count = -1; /* insufficient space */
  132. return 1;
  133. }
  134. offset = (char *)context->alist + context->count;
  135. strncpy(offset, xfs_xattr_prefix(flags), prefix_len);
  136. offset += prefix_len;
  137. strncpy(offset, (char *)name, namelen); /* real name */
  138. offset += namelen;
  139. *offset = '\0';
  140. context->count += prefix_len + namelen + 1;
  141. return 0;
  142. }
  143. static int
  144. xfs_xattr_put_listent_sizes(
  145. struct xfs_attr_list_context *context,
  146. int flags,
  147. unsigned char *name,
  148. int namelen,
  149. int valuelen,
  150. unsigned char *value)
  151. {
  152. context->count += xfs_xattr_prefix_len(flags) + namelen + 1;
  153. return 0;
  154. }
  155. static int
  156. list_one_attr(const char *name, const size_t len, void *data,
  157. size_t size, ssize_t *result)
  158. {
  159. char *p = data + *result;
  160. *result += len;
  161. if (!size)
  162. return 0;
  163. if (*result > size)
  164. return -ERANGE;
  165. strcpy(p, name);
  166. return 0;
  167. }
  168. ssize_t
  169. xfs_vn_listxattr(struct dentry *dentry, char *data, size_t size)
  170. {
  171. struct xfs_attr_list_context context;
  172. struct attrlist_cursor_kern cursor = { 0 };
  173. struct inode *inode = dentry->d_inode;
  174. int error;
  175. /*
  176. * First read the regular on-disk attributes.
  177. */
  178. memset(&context, 0, sizeof(context));
  179. context.dp = XFS_I(inode);
  180. context.cursor = &cursor;
  181. context.resynch = 1;
  182. context.alist = data;
  183. context.bufsize = size;
  184. context.firstu = context.bufsize;
  185. if (size)
  186. context.put_listent = xfs_xattr_put_listent;
  187. else
  188. context.put_listent = xfs_xattr_put_listent_sizes;
  189. xfs_attr_list_int(&context);
  190. if (context.count < 0)
  191. return -ERANGE;
  192. /*
  193. * Then add the two synthetic ACL attributes.
  194. */
  195. if (posix_acl_access_exists(inode)) {
  196. error = list_one_attr(POSIX_ACL_XATTR_ACCESS,
  197. strlen(POSIX_ACL_XATTR_ACCESS) + 1,
  198. data, size, &context.count);
  199. if (error)
  200. return error;
  201. }
  202. if (posix_acl_default_exists(inode)) {
  203. error = list_one_attr(POSIX_ACL_XATTR_DEFAULT,
  204. strlen(POSIX_ACL_XATTR_DEFAULT) + 1,
  205. data, size, &context.count);
  206. if (error)
  207. return error;
  208. }
  209. return context.count;
  210. }