xfs_xattr.c 6.5 KB

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