xfs_xattr.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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, 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, name, xflags);
  60. return -xfs_attr_set(ip, name, (void *)value, size, xflags);
  61. }
  62. static struct xattr_handler xfs_xattr_user_handler = {
  63. .prefix = XATTR_USER_PREFIX,
  64. .flags = 0, /* no flags implies user namespace */
  65. .get = xfs_xattr_get,
  66. .set = xfs_xattr_set,
  67. };
  68. static struct xattr_handler xfs_xattr_trusted_handler = {
  69. .prefix = XATTR_TRUSTED_PREFIX,
  70. .flags = ATTR_ROOT,
  71. .get = xfs_xattr_get,
  72. .set = xfs_xattr_set,
  73. };
  74. static struct xattr_handler xfs_xattr_security_handler = {
  75. .prefix = XATTR_SECURITY_PREFIX,
  76. .flags = ATTR_SECURE,
  77. .get = xfs_xattr_get,
  78. .set = xfs_xattr_set,
  79. };
  80. struct xattr_handler *xfs_xattr_handlers[] = {
  81. &xfs_xattr_user_handler,
  82. &xfs_xattr_trusted_handler,
  83. &xfs_xattr_security_handler,
  84. #ifdef CONFIG_XFS_POSIX_ACL
  85. &xfs_xattr_acl_access_handler,
  86. &xfs_xattr_acl_default_handler,
  87. #endif
  88. NULL
  89. };
  90. static unsigned int xfs_xattr_prefix_len(int flags)
  91. {
  92. if (flags & XFS_ATTR_SECURE)
  93. return sizeof("security");
  94. else if (flags & XFS_ATTR_ROOT)
  95. return sizeof("trusted");
  96. else
  97. return sizeof("user");
  98. }
  99. static const char *xfs_xattr_prefix(int flags)
  100. {
  101. if (flags & XFS_ATTR_SECURE)
  102. return xfs_xattr_security_handler.prefix;
  103. else if (flags & XFS_ATTR_ROOT)
  104. return xfs_xattr_trusted_handler.prefix;
  105. else
  106. return xfs_xattr_user_handler.prefix;
  107. }
  108. static int
  109. xfs_xattr_put_listent(struct xfs_attr_list_context *context, int flags,
  110. char *name, int namelen, int valuelen, char *value)
  111. {
  112. unsigned int prefix_len = xfs_xattr_prefix_len(flags);
  113. char *offset;
  114. int arraytop;
  115. ASSERT(context->count >= 0);
  116. /*
  117. * Only show root namespace entries if we are actually allowed to
  118. * see them.
  119. */
  120. if ((flags & XFS_ATTR_ROOT) && !capable(CAP_SYS_ADMIN))
  121. return 0;
  122. arraytop = context->count + prefix_len + namelen + 1;
  123. if (arraytop > context->firstu) {
  124. context->count = -1; /* insufficient space */
  125. return 1;
  126. }
  127. offset = (char *)context->alist + context->count;
  128. strncpy(offset, xfs_xattr_prefix(flags), prefix_len);
  129. offset += prefix_len;
  130. strncpy(offset, name, namelen); /* real name */
  131. offset += namelen;
  132. *offset = '\0';
  133. context->count += prefix_len + namelen + 1;
  134. return 0;
  135. }
  136. static int
  137. xfs_xattr_put_listent_sizes(struct xfs_attr_list_context *context, int flags,
  138. char *name, int namelen, int valuelen, char *value)
  139. {
  140. context->count += xfs_xattr_prefix_len(flags) + namelen + 1;
  141. return 0;
  142. }
  143. static int
  144. list_one_attr(const char *name, const size_t len, void *data,
  145. size_t size, ssize_t *result)
  146. {
  147. char *p = data + *result;
  148. *result += len;
  149. if (!size)
  150. return 0;
  151. if (*result > size)
  152. return -ERANGE;
  153. strcpy(p, name);
  154. return 0;
  155. }
  156. ssize_t
  157. xfs_vn_listxattr(struct dentry *dentry, char *data, size_t size)
  158. {
  159. struct xfs_attr_list_context context;
  160. struct attrlist_cursor_kern cursor = { 0 };
  161. struct inode *inode = dentry->d_inode;
  162. int error;
  163. /*
  164. * First read the regular on-disk attributes.
  165. */
  166. memset(&context, 0, sizeof(context));
  167. context.dp = XFS_I(inode);
  168. context.cursor = &cursor;
  169. context.resynch = 1;
  170. context.alist = data;
  171. context.bufsize = size;
  172. context.firstu = context.bufsize;
  173. if (size)
  174. context.put_listent = xfs_xattr_put_listent;
  175. else
  176. context.put_listent = xfs_xattr_put_listent_sizes;
  177. xfs_attr_list_int(&context);
  178. if (context.count < 0)
  179. return -ERANGE;
  180. /*
  181. * Then add the two synthetic ACL attributes.
  182. */
  183. if (posix_acl_access_exists(inode)) {
  184. error = list_one_attr(POSIX_ACL_XATTR_ACCESS,
  185. strlen(POSIX_ACL_XATTR_ACCESS) + 1,
  186. data, size, &context.count);
  187. if (error)
  188. return error;
  189. }
  190. if (posix_acl_default_exists(inode)) {
  191. error = list_one_attr(POSIX_ACL_XATTR_DEFAULT,
  192. strlen(POSIX_ACL_XATTR_DEFAULT) + 1,
  193. data, size, &context.count);
  194. if (error)
  195. return error;
  196. }
  197. return context.count;
  198. }