xfs_xattr.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2008 Christoph Hellwig.
  4. * Portions Copyright (C) 2000-2008 Silicon Graphics, Inc.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_shared.h"
  8. #include "xfs_format.h"
  9. #include "xfs_log_format.h"
  10. #include "xfs_da_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_da_btree.h"
  15. #include "xfs_attr.h"
  16. #include "xfs_acl.h"
  17. #include "xfs_log.h"
  18. #include "xfs_xattr.h"
  19. #include <linux/posix_acl_xattr.h>
  20. /*
  21. * Get permission to use log-assisted atomic exchange of file extents.
  22. *
  23. * Callers must not be running any transactions or hold any inode locks, and
  24. * they must release the permission by calling xlog_drop_incompat_feat
  25. * when they're done.
  26. */
  27. static inline int
  28. xfs_attr_grab_log_assist(
  29. struct xfs_mount *mp)
  30. {
  31. int error = 0;
  32. /*
  33. * Protect ourselves from an idle log clearing the logged xattrs log
  34. * incompat feature bit.
  35. */
  36. xlog_use_incompat_feat(mp->m_log);
  37. /*
  38. * If log-assisted xattrs are already enabled, the caller can use the
  39. * log assisted swap functions with the log-incompat reference we got.
  40. */
  41. if (xfs_sb_version_haslogxattrs(&mp->m_sb))
  42. return 0;
  43. /* Enable log-assisted xattrs. */
  44. error = xfs_add_incompat_log_feature(mp,
  45. XFS_SB_FEAT_INCOMPAT_LOG_XATTRS);
  46. if (error)
  47. goto drop_incompat;
  48. xfs_warn_mount(mp, XFS_OPSTATE_WARNED_LARP,
  49. "EXPERIMENTAL logged extended attributes feature in use. Use at your own risk!");
  50. return 0;
  51. drop_incompat:
  52. xlog_drop_incompat_feat(mp->m_log);
  53. return error;
  54. }
  55. static inline void
  56. xfs_attr_rele_log_assist(
  57. struct xfs_mount *mp)
  58. {
  59. xlog_drop_incompat_feat(mp->m_log);
  60. }
  61. static inline bool
  62. xfs_attr_want_log_assist(
  63. struct xfs_mount *mp)
  64. {
  65. #ifdef DEBUG
  66. /* Logged xattrs require a V5 super for log_incompat */
  67. return xfs_has_crc(mp) && xfs_globals.larp;
  68. #else
  69. return false;
  70. #endif
  71. }
  72. /*
  73. * Set or remove an xattr, having grabbed the appropriate logging resources
  74. * prior to calling libxfs.
  75. */
  76. int
  77. xfs_attr_change(
  78. struct xfs_da_args *args)
  79. {
  80. struct xfs_mount *mp = args->dp->i_mount;
  81. bool use_logging = false;
  82. int error;
  83. ASSERT(!(args->op_flags & XFS_DA_OP_LOGGED));
  84. if (xfs_attr_want_log_assist(mp)) {
  85. error = xfs_attr_grab_log_assist(mp);
  86. if (error)
  87. return error;
  88. args->op_flags |= XFS_DA_OP_LOGGED;
  89. use_logging = true;
  90. }
  91. error = xfs_attr_set(args);
  92. if (use_logging)
  93. xfs_attr_rele_log_assist(mp);
  94. return error;
  95. }
  96. static int
  97. xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
  98. struct inode *inode, const char *name, void *value, size_t size)
  99. {
  100. struct xfs_da_args args = {
  101. .dp = XFS_I(inode),
  102. .attr_filter = handler->flags,
  103. .name = name,
  104. .namelen = strlen(name),
  105. .value = value,
  106. .valuelen = size,
  107. };
  108. int error;
  109. error = xfs_attr_get(&args);
  110. if (error)
  111. return error;
  112. return args.valuelen;
  113. }
  114. static int
  115. xfs_xattr_set(const struct xattr_handler *handler,
  116. struct user_namespace *mnt_userns, struct dentry *unused,
  117. struct inode *inode, const char *name, const void *value,
  118. size_t size, int flags)
  119. {
  120. struct xfs_da_args args = {
  121. .dp = XFS_I(inode),
  122. .attr_filter = handler->flags,
  123. .attr_flags = flags,
  124. .name = name,
  125. .namelen = strlen(name),
  126. .value = (void *)value,
  127. .valuelen = size,
  128. };
  129. int error;
  130. error = xfs_attr_change(&args);
  131. if (!error && (handler->flags & XFS_ATTR_ROOT))
  132. xfs_forget_acl(inode, name);
  133. return error;
  134. }
  135. static const struct xattr_handler xfs_xattr_user_handler = {
  136. .prefix = XATTR_USER_PREFIX,
  137. .flags = 0, /* no flags implies user namespace */
  138. .get = xfs_xattr_get,
  139. .set = xfs_xattr_set,
  140. };
  141. static const struct xattr_handler xfs_xattr_trusted_handler = {
  142. .prefix = XATTR_TRUSTED_PREFIX,
  143. .flags = XFS_ATTR_ROOT,
  144. .get = xfs_xattr_get,
  145. .set = xfs_xattr_set,
  146. };
  147. static const struct xattr_handler xfs_xattr_security_handler = {
  148. .prefix = XATTR_SECURITY_PREFIX,
  149. .flags = XFS_ATTR_SECURE,
  150. .get = xfs_xattr_get,
  151. .set = xfs_xattr_set,
  152. };
  153. const struct xattr_handler *xfs_xattr_handlers[] = {
  154. &xfs_xattr_user_handler,
  155. &xfs_xattr_trusted_handler,
  156. &xfs_xattr_security_handler,
  157. #ifdef CONFIG_XFS_POSIX_ACL
  158. &posix_acl_access_xattr_handler,
  159. &posix_acl_default_xattr_handler,
  160. #endif
  161. NULL
  162. };
  163. static void
  164. __xfs_xattr_put_listent(
  165. struct xfs_attr_list_context *context,
  166. char *prefix,
  167. int prefix_len,
  168. unsigned char *name,
  169. int namelen)
  170. {
  171. char *offset;
  172. int arraytop;
  173. if (context->count < 0 || context->seen_enough)
  174. return;
  175. if (!context->buffer)
  176. goto compute_size;
  177. arraytop = context->count + prefix_len + namelen + 1;
  178. if (arraytop > context->firstu) {
  179. context->count = -1; /* insufficient space */
  180. context->seen_enough = 1;
  181. return;
  182. }
  183. offset = context->buffer + context->count;
  184. strncpy(offset, prefix, prefix_len);
  185. offset += prefix_len;
  186. strncpy(offset, (char *)name, namelen); /* real name */
  187. offset += namelen;
  188. *offset = '\0';
  189. compute_size:
  190. context->count += prefix_len + namelen + 1;
  191. return;
  192. }
  193. static void
  194. xfs_xattr_put_listent(
  195. struct xfs_attr_list_context *context,
  196. int flags,
  197. unsigned char *name,
  198. int namelen,
  199. int valuelen)
  200. {
  201. char *prefix;
  202. int prefix_len;
  203. ASSERT(context->count >= 0);
  204. if (flags & XFS_ATTR_ROOT) {
  205. #ifdef CONFIG_XFS_POSIX_ACL
  206. if (namelen == SGI_ACL_FILE_SIZE &&
  207. strncmp(name, SGI_ACL_FILE,
  208. SGI_ACL_FILE_SIZE) == 0) {
  209. __xfs_xattr_put_listent(
  210. context, XATTR_SYSTEM_PREFIX,
  211. XATTR_SYSTEM_PREFIX_LEN,
  212. XATTR_POSIX_ACL_ACCESS,
  213. strlen(XATTR_POSIX_ACL_ACCESS));
  214. } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
  215. strncmp(name, SGI_ACL_DEFAULT,
  216. SGI_ACL_DEFAULT_SIZE) == 0) {
  217. __xfs_xattr_put_listent(
  218. context, XATTR_SYSTEM_PREFIX,
  219. XATTR_SYSTEM_PREFIX_LEN,
  220. XATTR_POSIX_ACL_DEFAULT,
  221. strlen(XATTR_POSIX_ACL_DEFAULT));
  222. }
  223. #endif
  224. /*
  225. * Only show root namespace entries if we are actually allowed to
  226. * see them.
  227. */
  228. if (!capable(CAP_SYS_ADMIN))
  229. return;
  230. prefix = XATTR_TRUSTED_PREFIX;
  231. prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  232. } else if (flags & XFS_ATTR_SECURE) {
  233. prefix = XATTR_SECURITY_PREFIX;
  234. prefix_len = XATTR_SECURITY_PREFIX_LEN;
  235. } else {
  236. prefix = XATTR_USER_PREFIX;
  237. prefix_len = XATTR_USER_PREFIX_LEN;
  238. }
  239. __xfs_xattr_put_listent(context, prefix, prefix_len, name,
  240. namelen);
  241. return;
  242. }
  243. ssize_t
  244. xfs_vn_listxattr(
  245. struct dentry *dentry,
  246. char *data,
  247. size_t size)
  248. {
  249. struct xfs_attr_list_context context;
  250. struct inode *inode = d_inode(dentry);
  251. int error;
  252. /*
  253. * First read the regular on-disk attributes.
  254. */
  255. memset(&context, 0, sizeof(context));
  256. context.dp = XFS_I(inode);
  257. context.resynch = 1;
  258. context.buffer = size ? data : NULL;
  259. context.bufsize = size;
  260. context.firstu = context.bufsize;
  261. context.put_listent = xfs_xattr_put_listent;
  262. error = xfs_attr_list(&context);
  263. if (error)
  264. return error;
  265. if (context.count < 0)
  266. return -ERANGE;
  267. return context.count;
  268. }