xfs_xattr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 void
  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;
  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;
  144. }
  145. static void
  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. __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. } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
  167. strncmp(name, SGI_ACL_DEFAULT,
  168. SGI_ACL_DEFAULT_SIZE) == 0) {
  169. __xfs_xattr_put_listent(
  170. context, XATTR_SYSTEM_PREFIX,
  171. XATTR_SYSTEM_PREFIX_LEN,
  172. XATTR_POSIX_ACL_DEFAULT,
  173. strlen(XATTR_POSIX_ACL_DEFAULT));
  174. }
  175. #endif
  176. /*
  177. * Only show root namespace entries if we are actually allowed to
  178. * see them.
  179. */
  180. if (!capable(CAP_SYS_ADMIN))
  181. return;
  182. prefix = XATTR_TRUSTED_PREFIX;
  183. prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  184. } else if (flags & XFS_ATTR_SECURE) {
  185. prefix = XATTR_SECURITY_PREFIX;
  186. prefix_len = XATTR_SECURITY_PREFIX_LEN;
  187. } else {
  188. prefix = XATTR_USER_PREFIX;
  189. prefix_len = XATTR_USER_PREFIX_LEN;
  190. }
  191. __xfs_xattr_put_listent(context, prefix, prefix_len, name,
  192. namelen);
  193. return;
  194. }
  195. ssize_t
  196. xfs_vn_listxattr(
  197. struct dentry *dentry,
  198. char *data,
  199. size_t size)
  200. {
  201. struct xfs_attr_list_context context;
  202. struct attrlist_cursor_kern cursor = { 0 };
  203. struct inode *inode = d_inode(dentry);
  204. int error;
  205. /*
  206. * First read the regular on-disk attributes.
  207. */
  208. memset(&context, 0, sizeof(context));
  209. context.dp = XFS_I(inode);
  210. context.cursor = &cursor;
  211. context.resynch = 1;
  212. context.alist = size ? data : NULL;
  213. context.bufsize = size;
  214. context.firstu = context.bufsize;
  215. context.put_listent = xfs_xattr_put_listent;
  216. error = xfs_attr_list_int(&context);
  217. if (error)
  218. return error;
  219. if (context.count < 0)
  220. return -ERANGE;
  221. return context.count;
  222. }