xfs_xattr.c 5.7 KB

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