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