xfs_xattr.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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_format.h"
  20. #include "xfs_log_format.h"
  21. #include "xfs_trans_resv.h"
  22. #include "xfs_mount.h"
  23. #include "xfs_da_format.h"
  24. #include "xfs_inode.h"
  25. #include "xfs_attr.h"
  26. #include "xfs_attr_leaf.h"
  27. #include "xfs_acl.h"
  28. #include <linux/posix_acl_xattr.h>
  29. #include <linux/xattr.h>
  30. static int
  31. xfs_xattr_get(struct dentry *dentry, const char *name,
  32. void *value, size_t size, int xflags)
  33. {
  34. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  35. int error, asize = size;
  36. if (strcmp(name, "") == 0)
  37. return -EINVAL;
  38. /* Convert Linux syscall to XFS internal ATTR flags */
  39. if (!size) {
  40. xflags |= ATTR_KERNOVAL;
  41. value = NULL;
  42. }
  43. error = xfs_attr_get(ip, (unsigned char *)name, value, &asize, xflags);
  44. if (error)
  45. return error;
  46. return asize;
  47. }
  48. static int
  49. xfs_xattr_set(struct dentry *dentry, const char *name, const void *value,
  50. size_t size, int flags, int xflags)
  51. {
  52. struct xfs_inode *ip = XFS_I(d_inode(dentry));
  53. if (strcmp(name, "") == 0)
  54. return -EINVAL;
  55. /* Convert Linux syscall to XFS internal ATTR flags */
  56. if (flags & XATTR_CREATE)
  57. xflags |= ATTR_CREATE;
  58. if (flags & XATTR_REPLACE)
  59. xflags |= ATTR_REPLACE;
  60. if (!value)
  61. return xfs_attr_remove(ip, (unsigned char *)name, xflags);
  62. return xfs_attr_set(ip, (unsigned char *)name,
  63. (void *)value, size, xflags);
  64. }
  65. static const struct xattr_handler xfs_xattr_user_handler = {
  66. .prefix = XATTR_USER_PREFIX,
  67. .flags = 0, /* no flags implies user namespace */
  68. .get = xfs_xattr_get,
  69. .set = xfs_xattr_set,
  70. };
  71. static const struct xattr_handler xfs_xattr_trusted_handler = {
  72. .prefix = XATTR_TRUSTED_PREFIX,
  73. .flags = ATTR_ROOT,
  74. .get = xfs_xattr_get,
  75. .set = xfs_xattr_set,
  76. };
  77. static const struct xattr_handler xfs_xattr_security_handler = {
  78. .prefix = XATTR_SECURITY_PREFIX,
  79. .flags = ATTR_SECURE,
  80. .get = xfs_xattr_get,
  81. .set = xfs_xattr_set,
  82. };
  83. const struct xattr_handler *xfs_xattr_handlers[] = {
  84. &xfs_xattr_user_handler,
  85. &xfs_xattr_trusted_handler,
  86. &xfs_xattr_security_handler,
  87. #ifdef CONFIG_XFS_POSIX_ACL
  88. &posix_acl_access_xattr_handler,
  89. &posix_acl_default_xattr_handler,
  90. #endif
  91. NULL
  92. };
  93. static unsigned int xfs_xattr_prefix_len(int flags)
  94. {
  95. if (flags & XFS_ATTR_SECURE)
  96. return sizeof("security");
  97. else if (flags & XFS_ATTR_ROOT)
  98. return sizeof("trusted");
  99. else
  100. return sizeof("user");
  101. }
  102. static const char *xfs_xattr_prefix(int flags)
  103. {
  104. if (flags & XFS_ATTR_SECURE)
  105. return xfs_xattr_security_handler.prefix;
  106. else if (flags & XFS_ATTR_ROOT)
  107. return xfs_xattr_trusted_handler.prefix;
  108. else
  109. return xfs_xattr_user_handler.prefix;
  110. }
  111. static int
  112. xfs_xattr_put_listent(
  113. struct xfs_attr_list_context *context,
  114. int flags,
  115. unsigned char *name,
  116. int namelen,
  117. int valuelen,
  118. unsigned char *value)
  119. {
  120. unsigned int prefix_len = xfs_xattr_prefix_len(flags);
  121. char *offset;
  122. int arraytop;
  123. ASSERT(context->count >= 0);
  124. /*
  125. * Only show root namespace entries if we are actually allowed to
  126. * see them.
  127. */
  128. if ((flags & XFS_ATTR_ROOT) && !capable(CAP_SYS_ADMIN))
  129. return 0;
  130. arraytop = context->count + prefix_len + namelen + 1;
  131. if (arraytop > context->firstu) {
  132. context->count = -1; /* insufficient space */
  133. return 1;
  134. }
  135. offset = (char *)context->alist + context->count;
  136. strncpy(offset, xfs_xattr_prefix(flags), prefix_len);
  137. offset += prefix_len;
  138. strncpy(offset, (char *)name, namelen); /* real name */
  139. offset += namelen;
  140. *offset = '\0';
  141. context->count += prefix_len + namelen + 1;
  142. return 0;
  143. }
  144. static int
  145. xfs_xattr_put_listent_sizes(
  146. struct xfs_attr_list_context *context,
  147. int flags,
  148. unsigned char *name,
  149. int namelen,
  150. int valuelen,
  151. unsigned char *value)
  152. {
  153. context->count += xfs_xattr_prefix_len(flags) + namelen + 1;
  154. return 0;
  155. }
  156. static int
  157. list_one_attr(const char *name, const size_t len, void *data,
  158. size_t size, ssize_t *result)
  159. {
  160. char *p = data + *result;
  161. *result += len;
  162. if (!size)
  163. return 0;
  164. if (*result > size)
  165. return -ERANGE;
  166. strcpy(p, name);
  167. return 0;
  168. }
  169. ssize_t
  170. xfs_vn_listxattr(struct dentry *dentry, char *data, size_t size)
  171. {
  172. struct xfs_attr_list_context context;
  173. struct attrlist_cursor_kern cursor = { 0 };
  174. struct inode *inode = d_inode(dentry);
  175. int error;
  176. /*
  177. * First read the regular on-disk attributes.
  178. */
  179. memset(&context, 0, sizeof(context));
  180. context.dp = XFS_I(inode);
  181. context.cursor = &cursor;
  182. context.resynch = 1;
  183. context.alist = data;
  184. context.bufsize = size;
  185. context.firstu = context.bufsize;
  186. if (size)
  187. context.put_listent = xfs_xattr_put_listent;
  188. else
  189. context.put_listent = xfs_xattr_put_listent_sizes;
  190. xfs_attr_list_int(&context);
  191. if (context.count < 0)
  192. return -ERANGE;
  193. /*
  194. * Then add the two synthetic ACL attributes.
  195. */
  196. if (posix_acl_access_exists(inode)) {
  197. error = list_one_attr(POSIX_ACL_XATTR_ACCESS,
  198. strlen(POSIX_ACL_XATTR_ACCESS) + 1,
  199. data, size, &context.count);
  200. if (error)
  201. return error;
  202. }
  203. if (posix_acl_default_exists(inode)) {
  204. error = list_one_attr(POSIX_ACL_XATTR_DEFAULT,
  205. strlen(POSIX_ACL_XATTR_DEFAULT) + 1,
  206. data, size, &context.count);
  207. if (error)
  208. return error;
  209. }
  210. return context.count;
  211. }