xref: /linux/fs/erofs/xattr.h (revision a5488f29835c0eb5561b46e71c23f6c39aab6c83)
147e4937aSGao Xiang /* SPDX-License-Identifier: GPL-2.0-only */
247e4937aSGao Xiang /*
347e4937aSGao Xiang  * Copyright (C) 2017-2018 HUAWEI, Inc.
4592e7cd0SAlexander A. Klimov  *             https://www.huawei.com/
547e4937aSGao Xiang  */
647e4937aSGao Xiang #ifndef __EROFS_XATTR_H
747e4937aSGao Xiang #define __EROFS_XATTR_H
847e4937aSGao Xiang 
947e4937aSGao Xiang #include "internal.h"
1047e4937aSGao Xiang #include <linux/posix_acl_xattr.h>
1147e4937aSGao Xiang #include <linux/xattr.h>
1247e4937aSGao Xiang 
1347e4937aSGao Xiang /* Attribute not found */
1447e4937aSGao Xiang #define ENOATTR         ENODATA
1547e4937aSGao Xiang 
1647e4937aSGao Xiang static inline unsigned int inlinexattr_header_size(struct inode *inode)
1747e4937aSGao Xiang {
18a5876e24SGao Xiang 	return sizeof(struct erofs_xattr_ibody_header) +
19a5876e24SGao Xiang 		sizeof(u32) * EROFS_I(inode)->xattr_shared_count;
2047e4937aSGao Xiang }
2147e4937aSGao Xiang 
2247e4937aSGao Xiang static inline erofs_blk_t xattrblock_addr(struct erofs_sb_info *sbi,
2347e4937aSGao Xiang 					  unsigned int xattr_id)
2447e4937aSGao Xiang {
2547e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
2647e4937aSGao Xiang 	return sbi->xattr_blkaddr +
2747e4937aSGao Xiang 		xattr_id * sizeof(__u32) / EROFS_BLKSIZ;
2847e4937aSGao Xiang #else
2947e4937aSGao Xiang 	return 0;
3047e4937aSGao Xiang #endif
3147e4937aSGao Xiang }
3247e4937aSGao Xiang 
3347e4937aSGao Xiang static inline unsigned int xattrblock_offset(struct erofs_sb_info *sbi,
3447e4937aSGao Xiang 					     unsigned int xattr_id)
3547e4937aSGao Xiang {
3647e4937aSGao Xiang 	return (xattr_id * sizeof(__u32)) % EROFS_BLKSIZ;
3747e4937aSGao Xiang }
3847e4937aSGao Xiang 
3947e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_XATTR
4047e4937aSGao Xiang extern const struct xattr_handler erofs_xattr_user_handler;
4147e4937aSGao Xiang extern const struct xattr_handler erofs_xattr_trusted_handler;
4247e4937aSGao Xiang extern const struct xattr_handler erofs_xattr_security_handler;
4347e4937aSGao Xiang 
44*a5488f29SChristian Brauner static inline const char *erofs_xattr_prefix(unsigned int idx,
45*a5488f29SChristian Brauner 					     struct dentry *dentry)
4647e4937aSGao Xiang {
47*a5488f29SChristian Brauner 	const struct xattr_handler *handler = NULL;
48*a5488f29SChristian Brauner 
4947e4937aSGao Xiang 	static const struct xattr_handler *xattr_handler_map[] = {
5047e4937aSGao Xiang 		[EROFS_XATTR_INDEX_USER] = &erofs_xattr_user_handler,
5147e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
52a55861c8SVladimir Zapolskiy 		[EROFS_XATTR_INDEX_POSIX_ACL_ACCESS] =
53a55861c8SVladimir Zapolskiy 			&posix_acl_access_xattr_handler,
5447e4937aSGao Xiang 		[EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT] =
5547e4937aSGao Xiang 			&posix_acl_default_xattr_handler,
5647e4937aSGao Xiang #endif
5747e4937aSGao Xiang 		[EROFS_XATTR_INDEX_TRUSTED] = &erofs_xattr_trusted_handler,
5847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
5947e4937aSGao Xiang 		[EROFS_XATTR_INDEX_SECURITY] = &erofs_xattr_security_handler,
6047e4937aSGao Xiang #endif
6147e4937aSGao Xiang 	};
6247e4937aSGao Xiang 
63*a5488f29SChristian Brauner 	if (idx && idx < ARRAY_SIZE(xattr_handler_map))
64*a5488f29SChristian Brauner 		handler = xattr_handler_map[idx];
65*a5488f29SChristian Brauner 
66*a5488f29SChristian Brauner 	if (!xattr_handler_can_list(handler, dentry))
67*a5488f29SChristian Brauner 		return NULL;
68*a5488f29SChristian Brauner 
69*a5488f29SChristian Brauner 	return xattr_prefix(handler);
7047e4937aSGao Xiang }
7147e4937aSGao Xiang 
7247e4937aSGao Xiang extern const struct xattr_handler *erofs_xattr_handlers[];
7347e4937aSGao Xiang 
7447e4937aSGao Xiang int erofs_getxattr(struct inode *, int, const char *, void *, size_t);
7547e4937aSGao Xiang ssize_t erofs_listxattr(struct dentry *, char *, size_t);
7647e4937aSGao Xiang #else
7747e4937aSGao Xiang static inline int erofs_getxattr(struct inode *inode, int index,
7847e4937aSGao Xiang 				 const char *name, void *buffer,
7947e4937aSGao Xiang 				 size_t buffer_size)
8047e4937aSGao Xiang {
8147e4937aSGao Xiang 	return -EOPNOTSUPP;
8247e4937aSGao Xiang }
8347e4937aSGao Xiang 
84e7cda1eeSChengguang Xu #define erofs_listxattr (NULL)
85e7cda1eeSChengguang Xu #define erofs_xattr_handlers (NULL)
8647e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_XATTR */
8747e4937aSGao Xiang 
8847e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
890cad6246SMiklos Szeredi struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu);
9047e4937aSGao Xiang #else
9147e4937aSGao Xiang #define erofs_get_acl	(NULL)
9247e4937aSGao Xiang #endif
9347e4937aSGao Xiang 
9447e4937aSGao Xiang #endif
95