xfs_xattr.c 6.1 KB

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