xref: /linux/fs/erofs/xattr.h (revision d549b741740e63e87e661754e2d1b336fdc51d50)
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 
44a5488f29SChristian Brauner static inline const char *erofs_xattr_prefix(unsigned int idx,
45a5488f29SChristian Brauner 					     struct dentry *dentry)
4647e4937aSGao Xiang {
47a5488f29SChristian Brauner 	const struct xattr_handler *handler = NULL;
48a5488f29SChristian 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
52*d549b741SChristian Brauner 		[EROFS_XATTR_INDEX_POSIX_ACL_ACCESS] = &nop_posix_acl_access,
53*d549b741SChristian Brauner 		[EROFS_XATTR_INDEX_POSIX_ACL_DEFAULT] = &nop_posix_acl_default,
5447e4937aSGao Xiang #endif
5547e4937aSGao Xiang 		[EROFS_XATTR_INDEX_TRUSTED] = &erofs_xattr_trusted_handler,
5647e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_SECURITY
5747e4937aSGao Xiang 		[EROFS_XATTR_INDEX_SECURITY] = &erofs_xattr_security_handler,
5847e4937aSGao Xiang #endif
5947e4937aSGao Xiang 	};
6047e4937aSGao Xiang 
61a5488f29SChristian Brauner 	if (idx && idx < ARRAY_SIZE(xattr_handler_map))
62a5488f29SChristian Brauner 		handler = xattr_handler_map[idx];
63a5488f29SChristian Brauner 
64a5488f29SChristian Brauner 	if (!xattr_handler_can_list(handler, dentry))
65a5488f29SChristian Brauner 		return NULL;
66a5488f29SChristian Brauner 
67a5488f29SChristian Brauner 	return xattr_prefix(handler);
6847e4937aSGao Xiang }
6947e4937aSGao Xiang 
7047e4937aSGao Xiang extern const struct xattr_handler *erofs_xattr_handlers[];
7147e4937aSGao Xiang 
7247e4937aSGao Xiang int erofs_getxattr(struct inode *, int, const char *, void *, size_t);
7347e4937aSGao Xiang ssize_t erofs_listxattr(struct dentry *, char *, size_t);
7447e4937aSGao Xiang #else
7547e4937aSGao Xiang static inline int erofs_getxattr(struct inode *inode, int index,
7647e4937aSGao Xiang 				 const char *name, void *buffer,
7747e4937aSGao Xiang 				 size_t buffer_size)
7847e4937aSGao Xiang {
7947e4937aSGao Xiang 	return -EOPNOTSUPP;
8047e4937aSGao Xiang }
8147e4937aSGao Xiang 
82e7cda1eeSChengguang Xu #define erofs_listxattr (NULL)
83e7cda1eeSChengguang Xu #define erofs_xattr_handlers (NULL)
8447e4937aSGao Xiang #endif	/* !CONFIG_EROFS_FS_XATTR */
8547e4937aSGao Xiang 
8647e4937aSGao Xiang #ifdef CONFIG_EROFS_FS_POSIX_ACL
870cad6246SMiklos Szeredi struct posix_acl *erofs_get_acl(struct inode *inode, int type, bool rcu);
8847e4937aSGao Xiang #else
8947e4937aSGao Xiang #define erofs_get_acl	(NULL)
9047e4937aSGao Xiang #endif
9147e4937aSGao Xiang 
9247e4937aSGao Xiang #endif
93