xfs_xattr.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "xfs_quota.h"
  20. #include <linux/posix_acl_xattr.h>
  21. /*
  22. * Get permission to use log-assisted atomic exchange of file extents.
  23. * Callers must not be running any transactions or hold any ILOCKs.
  24. */
  25. static inline int
  26. xfs_attr_grab_log_assist(
  27. struct xfs_mount *mp)
  28. {
  29. int error = 0;
  30. /* xattr update log intent items are already enabled */
  31. if (xfs_is_using_logged_xattrs(mp))
  32. return 0;
  33. /*
  34. * Check if the filesystem featureset is new enough to set this log
  35. * incompat feature bit. Strictly speaking, the minimum requirement is
  36. * a V5 filesystem for the superblock field, but we'll require rmap
  37. * or reflink to avoid having to deal with really old kernels.
  38. */
  39. if (!xfs_has_reflink(mp) && !xfs_has_rmapbt(mp))
  40. return -EOPNOTSUPP;
  41. /* Enable log-assisted xattrs. */
  42. error = xfs_add_incompat_log_feature(mp,
  43. XFS_SB_FEAT_INCOMPAT_LOG_XATTRS);
  44. if (error)
  45. return error;
  46. xfs_set_using_logged_xattrs(mp);
  47. xfs_warn_mount(mp, XFS_OPSTATE_WARNED_LARP,
  48. "EXPERIMENTAL logged extended attributes feature in use. Use at your own risk!");
  49. return 0;
  50. }
  51. static inline bool
  52. xfs_attr_want_log_assist(
  53. struct xfs_mount *mp)
  54. {
  55. #ifdef DEBUG
  56. /* Logged xattrs require a V5 super for log_incompat */
  57. return xfs_has_crc(mp) && xfs_globals.larp;
  58. #else
  59. return false;
  60. #endif
  61. }
  62. /*
  63. * Set or remove an xattr, having grabbed the appropriate logging resources
  64. * prior to calling libxfs. Callers of this function are only required to
  65. * initialize the inode, attr_filter, name, namelen, value, and valuelen fields
  66. * of @args.
  67. */
  68. int
  69. xfs_attr_change(
  70. struct xfs_da_args *args,
  71. enum xfs_attr_update op)
  72. {
  73. struct xfs_mount *mp = args->dp->i_mount;
  74. int error;
  75. if (xfs_is_shutdown(mp))
  76. return -EIO;
  77. error = xfs_qm_dqattach(args->dp);
  78. if (error)
  79. return error;
  80. /*
  81. * We have no control over the attribute names that userspace passes us
  82. * to remove, so we have to allow the name lookup prior to attribute
  83. * removal to fail as well.
  84. */
  85. args->op_flags = XFS_DA_OP_OKNOENT;
  86. if (xfs_attr_want_log_assist(mp)) {
  87. error = xfs_attr_grab_log_assist(mp);
  88. if (error)
  89. return error;
  90. args->op_flags |= XFS_DA_OP_LOGGED;
  91. }
  92. args->owner = args->dp->i_ino;
  93. args->geo = mp->m_attr_geo;
  94. args->whichfork = XFS_ATTR_FORK;
  95. xfs_attr_sethash(args);
  96. /*
  97. * Some xattrs must be resistant to allocation failure at ENOSPC, e.g.
  98. * creating an inode with ACLs or security attributes requires the
  99. * allocation of the xattr holding that information to succeed. Hence
  100. * we allow xattrs in the VFS TRUSTED, SYSTEM, POSIX_ACL and SECURITY
  101. * (LSM xattr) namespaces to dip into the reserve block pool to allow
  102. * manipulation of these xattrs when at ENOSPC. These VFS xattr
  103. * namespaces translate to the XFS_ATTR_ROOT and XFS_ATTR_SECURE on-disk
  104. * namespaces.
  105. *
  106. * For most of these cases, these special xattrs will fit in the inode
  107. * itself and so consume no extra space or only require temporary extra
  108. * space while an overwrite is being made. Hence the use of the reserved
  109. * pool is largely to avoid the worst case reservation from preventing
  110. * the xattr from being created at ENOSPC.
  111. */
  112. return xfs_attr_set(args, op,
  113. args->attr_filter & (XFS_ATTR_ROOT | XFS_ATTR_SECURE));
  114. }
  115. static int
  116. xfs_xattr_get(const struct xattr_handler *handler, struct dentry *unused,
  117. struct inode *inode, const char *name, void *value, size_t size)
  118. {
  119. struct xfs_da_args args = {
  120. .dp = XFS_I(inode),
  121. .attr_filter = handler->flags,
  122. .name = name,
  123. .namelen = strlen(name),
  124. .value = value,
  125. .valuelen = size,
  126. };
  127. int error;
  128. if (xfs_ifork_zapped(XFS_I(inode), XFS_ATTR_FORK))
  129. return -EIO;
  130. error = xfs_attr_get(&args);
  131. if (error)
  132. return error;
  133. return args.valuelen;
  134. }
  135. static inline enum xfs_attr_update
  136. xfs_xattr_flags_to_op(
  137. int flags,
  138. const void *value)
  139. {
  140. if (!value)
  141. return XFS_ATTRUPDATE_REMOVE;
  142. if (flags & XATTR_CREATE)
  143. return XFS_ATTRUPDATE_CREATE;
  144. if (flags & XATTR_REPLACE)
  145. return XFS_ATTRUPDATE_REPLACE;
  146. return XFS_ATTRUPDATE_UPSERT;
  147. }
  148. static int
  149. xfs_xattr_set(const struct xattr_handler *handler,
  150. struct mnt_idmap *idmap, struct dentry *unused,
  151. struct inode *inode, const char *name, const void *value,
  152. size_t size, int flags)
  153. {
  154. struct xfs_da_args args = {
  155. .dp = XFS_I(inode),
  156. .attr_filter = handler->flags,
  157. .name = name,
  158. .namelen = strlen(name),
  159. .value = (void *)value,
  160. .valuelen = size,
  161. };
  162. int error;
  163. error = xfs_attr_change(&args, xfs_xattr_flags_to_op(flags, value));
  164. if (!error && (handler->flags & XFS_ATTR_ROOT))
  165. xfs_forget_acl(inode, name);
  166. return error;
  167. }
  168. static const struct xattr_handler xfs_xattr_user_handler = {
  169. .prefix = XATTR_USER_PREFIX,
  170. .flags = 0, /* no flags implies user namespace */
  171. .get = xfs_xattr_get,
  172. .set = xfs_xattr_set,
  173. };
  174. static const struct xattr_handler xfs_xattr_trusted_handler = {
  175. .prefix = XATTR_TRUSTED_PREFIX,
  176. .flags = XFS_ATTR_ROOT,
  177. .get = xfs_xattr_get,
  178. .set = xfs_xattr_set,
  179. };
  180. static const struct xattr_handler xfs_xattr_security_handler = {
  181. .prefix = XATTR_SECURITY_PREFIX,
  182. .flags = XFS_ATTR_SECURE,
  183. .get = xfs_xattr_get,
  184. .set = xfs_xattr_set,
  185. };
  186. const struct xattr_handler * const xfs_xattr_handlers[] = {
  187. &xfs_xattr_user_handler,
  188. &xfs_xattr_trusted_handler,
  189. &xfs_xattr_security_handler,
  190. NULL
  191. };
  192. static void
  193. __xfs_xattr_put_listent(
  194. struct xfs_attr_list_context *context,
  195. char *prefix,
  196. int prefix_len,
  197. unsigned char *name,
  198. int namelen)
  199. {
  200. char *offset;
  201. int arraytop;
  202. if (context->count < 0 || context->seen_enough)
  203. return;
  204. if (!context->buffer)
  205. goto compute_size;
  206. arraytop = context->count + prefix_len + namelen + 1;
  207. if (arraytop > context->firstu) {
  208. context->count = -1; /* insufficient space */
  209. context->seen_enough = 1;
  210. return;
  211. }
  212. offset = context->buffer + context->count;
  213. memcpy(offset, prefix, prefix_len);
  214. offset += prefix_len;
  215. strncpy(offset, (char *)name, namelen); /* real name */
  216. offset += namelen;
  217. *offset = '\0';
  218. compute_size:
  219. context->count += prefix_len + namelen + 1;
  220. return;
  221. }
  222. static void
  223. xfs_xattr_put_listent(
  224. struct xfs_attr_list_context *context,
  225. int flags,
  226. unsigned char *name,
  227. int namelen,
  228. void *value,
  229. int valuelen)
  230. {
  231. char *prefix;
  232. int prefix_len;
  233. ASSERT(context->count >= 0);
  234. /* Don't expose private xattr namespaces. */
  235. if (flags & XFS_ATTR_PRIVATE_NSP_MASK)
  236. return;
  237. if (flags & XFS_ATTR_ROOT) {
  238. #ifdef CONFIG_XFS_POSIX_ACL
  239. if (namelen == SGI_ACL_FILE_SIZE &&
  240. strncmp(name, SGI_ACL_FILE,
  241. SGI_ACL_FILE_SIZE) == 0) {
  242. __xfs_xattr_put_listent(
  243. context, XATTR_SYSTEM_PREFIX,
  244. XATTR_SYSTEM_PREFIX_LEN,
  245. XATTR_POSIX_ACL_ACCESS,
  246. strlen(XATTR_POSIX_ACL_ACCESS));
  247. } else if (namelen == SGI_ACL_DEFAULT_SIZE &&
  248. strncmp(name, SGI_ACL_DEFAULT,
  249. SGI_ACL_DEFAULT_SIZE) == 0) {
  250. __xfs_xattr_put_listent(
  251. context, XATTR_SYSTEM_PREFIX,
  252. XATTR_SYSTEM_PREFIX_LEN,
  253. XATTR_POSIX_ACL_DEFAULT,
  254. strlen(XATTR_POSIX_ACL_DEFAULT));
  255. }
  256. #endif
  257. /*
  258. * Only show root namespace entries if we are actually allowed to
  259. * see them.
  260. */
  261. if (!capable(CAP_SYS_ADMIN))
  262. return;
  263. prefix = XATTR_TRUSTED_PREFIX;
  264. prefix_len = XATTR_TRUSTED_PREFIX_LEN;
  265. } else if (flags & XFS_ATTR_SECURE) {
  266. prefix = XATTR_SECURITY_PREFIX;
  267. prefix_len = XATTR_SECURITY_PREFIX_LEN;
  268. } else {
  269. prefix = XATTR_USER_PREFIX;
  270. prefix_len = XATTR_USER_PREFIX_LEN;
  271. }
  272. __xfs_xattr_put_listent(context, prefix, prefix_len, name,
  273. namelen);
  274. return;
  275. }
  276. ssize_t
  277. xfs_vn_listxattr(
  278. struct dentry *dentry,
  279. char *data,
  280. size_t size)
  281. {
  282. struct xfs_attr_list_context context;
  283. struct inode *inode = d_inode(dentry);
  284. int error;
  285. if (xfs_ifork_zapped(XFS_I(inode), XFS_ATTR_FORK))
  286. return -EIO;
  287. /*
  288. * First read the regular on-disk attributes.
  289. */
  290. memset(&context, 0, sizeof(context));
  291. context.dp = XFS_I(inode);
  292. context.resynch = 1;
  293. context.buffer = size ? data : NULL;
  294. context.bufsize = size;
  295. context.firstu = context.bufsize;
  296. context.put_listent = xfs_xattr_put_listent;
  297. error = xfs_attr_list(&context);
  298. if (error)
  299. return error;
  300. if (context.count < 0)
  301. return -ERANGE;
  302. return context.count;
  303. }