xfs_xattr.c 5.7 KB

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