xfs_xattr.c 6.4 KB

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