162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * fs/f2fs/xattr.h
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci * Copyright (c) 2012 Samsung Electronics Co., Ltd.
662306a36Sopenharmony_ci *             http://www.samsung.com/
762306a36Sopenharmony_ci *
862306a36Sopenharmony_ci * Portions of this code from linux/fs/ext2/xattr.h
962306a36Sopenharmony_ci *
1062306a36Sopenharmony_ci * On-disk format of extended attributes for the ext2 filesystem.
1162306a36Sopenharmony_ci *
1262306a36Sopenharmony_ci * (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
1362306a36Sopenharmony_ci */
1462306a36Sopenharmony_ci#ifndef __F2FS_XATTR_H__
1562306a36Sopenharmony_ci#define __F2FS_XATTR_H__
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include <linux/init.h>
1862306a36Sopenharmony_ci#include <linux/xattr.h>
1962306a36Sopenharmony_ci
2062306a36Sopenharmony_ci/* Magic value in attribute blocks */
2162306a36Sopenharmony_ci#define F2FS_XATTR_MAGIC                0xF2F52011
2262306a36Sopenharmony_ci
2362306a36Sopenharmony_ci/* Maximum number of references to one attribute block */
2462306a36Sopenharmony_ci#define F2FS_XATTR_REFCOUNT_MAX         1024
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci/* Name indexes */
2762306a36Sopenharmony_ci#define F2FS_SYSTEM_ADVISE_NAME			"system.advise"
2862306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_USER			1
2962306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS	2
3062306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT	3
3162306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_TRUSTED		4
3262306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_LUSTRE			5
3362306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_SECURITY		6
3462306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_ADVISE			7
3562306a36Sopenharmony_ci/* Should be same as EXT4_XATTR_INDEX_ENCRYPTION */
3662306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_ENCRYPTION		9
3762306a36Sopenharmony_ci#define F2FS_XATTR_INDEX_VERITY			11
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#define F2FS_XATTR_NAME_ENCRYPTION_CONTEXT	"c"
4062306a36Sopenharmony_ci#define F2FS_XATTR_NAME_VERITY			"v"
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_cistruct f2fs_xattr_header {
4362306a36Sopenharmony_ci	__le32  h_magic;        /* magic number for identification */
4462306a36Sopenharmony_ci	__le32  h_refcount;     /* reference count */
4562306a36Sopenharmony_ci	__u32   h_reserved[4];  /* zero right now */
4662306a36Sopenharmony_ci};
4762306a36Sopenharmony_ci
4862306a36Sopenharmony_cistruct f2fs_xattr_entry {
4962306a36Sopenharmony_ci	__u8    e_name_index;
5062306a36Sopenharmony_ci	__u8    e_name_len;
5162306a36Sopenharmony_ci	__le16  e_value_size;   /* size of attribute value */
5262306a36Sopenharmony_ci	char    e_name[];      /* attribute name */
5362306a36Sopenharmony_ci};
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci#define XATTR_HDR(ptr)		((struct f2fs_xattr_header *)(ptr))
5662306a36Sopenharmony_ci#define XATTR_ENTRY(ptr)	((struct f2fs_xattr_entry *)(ptr))
5762306a36Sopenharmony_ci#define XATTR_FIRST_ENTRY(ptr)	(XATTR_ENTRY(XATTR_HDR(ptr) + 1))
5862306a36Sopenharmony_ci#define XATTR_ROUND		(3)
5962306a36Sopenharmony_ci
6062306a36Sopenharmony_ci#define XATTR_ALIGN(size)	(((size) + XATTR_ROUND) & ~XATTR_ROUND)
6162306a36Sopenharmony_ci
6262306a36Sopenharmony_ci#define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
6362306a36Sopenharmony_ci			(entry)->e_name_len + le16_to_cpu((entry)->e_value_size)))
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci#define XATTR_NEXT_ENTRY(entry)	((struct f2fs_xattr_entry *)((char *)(entry) +\
6662306a36Sopenharmony_ci			ENTRY_SIZE(entry)))
6762306a36Sopenharmony_ci
6862306a36Sopenharmony_ci#define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#define list_for_each_xattr(entry, addr) \
7162306a36Sopenharmony_ci		for (entry = XATTR_FIRST_ENTRY(addr);\
7262306a36Sopenharmony_ci				!IS_XATTR_LAST_ENTRY(entry);\
7362306a36Sopenharmony_ci				entry = XATTR_NEXT_ENTRY(entry))
7462306a36Sopenharmony_ci#define VALID_XATTR_BLOCK_SIZE	(PAGE_SIZE - sizeof(struct node_footer))
7562306a36Sopenharmony_ci#define XATTR_PADDING_SIZE	(sizeof(__u32))
7662306a36Sopenharmony_ci#define XATTR_SIZE(i)		((F2FS_I(i)->i_xattr_nid ?		\
7762306a36Sopenharmony_ci					VALID_XATTR_BLOCK_SIZE : 0) +	\
7862306a36Sopenharmony_ci						(inline_xattr_size(i)))
7962306a36Sopenharmony_ci#define MIN_OFFSET(i)		XATTR_ALIGN(inline_xattr_size(i) +	\
8062306a36Sopenharmony_ci						VALID_XATTR_BLOCK_SIZE)
8162306a36Sopenharmony_ci
8262306a36Sopenharmony_ci#define MAX_VALUE_LEN(i)	(MIN_OFFSET(i) -			\
8362306a36Sopenharmony_ci				sizeof(struct f2fs_xattr_header) -	\
8462306a36Sopenharmony_ci				sizeof(struct f2fs_xattr_entry))
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci#define MIN_INLINE_XATTR_SIZE (sizeof(struct f2fs_xattr_header) / sizeof(__le32))
8762306a36Sopenharmony_ci#define MAX_INLINE_XATTR_SIZE						\
8862306a36Sopenharmony_ci			(DEF_ADDRS_PER_INODE -				\
8962306a36Sopenharmony_ci			F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -	\
9062306a36Sopenharmony_ci			DEF_INLINE_RESERVED_SIZE -			\
9162306a36Sopenharmony_ci			MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
9262306a36Sopenharmony_ci
9362306a36Sopenharmony_ci/*
9462306a36Sopenharmony_ci * On-disk structure of f2fs_xattr
9562306a36Sopenharmony_ci * We use inline xattrs space + 1 block for xattr.
9662306a36Sopenharmony_ci *
9762306a36Sopenharmony_ci * +--------------------+
9862306a36Sopenharmony_ci * | f2fs_xattr_header  |
9962306a36Sopenharmony_ci * |                    |
10062306a36Sopenharmony_ci * +--------------------+
10162306a36Sopenharmony_ci * | f2fs_xattr_entry   |
10262306a36Sopenharmony_ci * | .e_name_index = 1  |
10362306a36Sopenharmony_ci * | .e_name_len = 3    |
10462306a36Sopenharmony_ci * | .e_value_size = 14 |
10562306a36Sopenharmony_ci * | .e_name = "foo"    |
10662306a36Sopenharmony_ci * | "value_of_xattr"   |<- value_offs = e_name + e_name_len
10762306a36Sopenharmony_ci * +--------------------+
10862306a36Sopenharmony_ci * | f2fs_xattr_entry   |
10962306a36Sopenharmony_ci * | .e_name_index = 4  |
11062306a36Sopenharmony_ci * | .e_name = "bar"    |
11162306a36Sopenharmony_ci * +--------------------+
11262306a36Sopenharmony_ci * |                    |
11362306a36Sopenharmony_ci * |        Free        |
11462306a36Sopenharmony_ci * |                    |
11562306a36Sopenharmony_ci * +--------------------+<- MIN_OFFSET
11662306a36Sopenharmony_ci * |   node_footer      |
11762306a36Sopenharmony_ci * | (nid, ino, offset) |
11862306a36Sopenharmony_ci * +--------------------+
11962306a36Sopenharmony_ci *
12062306a36Sopenharmony_ci **/
12162306a36Sopenharmony_ci
12262306a36Sopenharmony_ci#ifdef CONFIG_F2FS_FS_XATTR
12362306a36Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_user_handler;
12462306a36Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_trusted_handler;
12562306a36Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_advise_handler;
12662306a36Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_security_handler;
12762306a36Sopenharmony_ci
12862306a36Sopenharmony_ciextern const struct xattr_handler *f2fs_xattr_handlers[];
12962306a36Sopenharmony_ci
13062306a36Sopenharmony_ciextern int f2fs_setxattr(struct inode *, int, const char *,
13162306a36Sopenharmony_ci				const void *, size_t, struct page *, int);
13262306a36Sopenharmony_ciextern int f2fs_getxattr(struct inode *, int, const char *, void *,
13362306a36Sopenharmony_ci						size_t, struct page *);
13462306a36Sopenharmony_ciextern ssize_t f2fs_listxattr(struct dentry *, char *, size_t);
13562306a36Sopenharmony_ciextern int f2fs_init_xattr_caches(struct f2fs_sb_info *);
13662306a36Sopenharmony_ciextern void f2fs_destroy_xattr_caches(struct f2fs_sb_info *);
13762306a36Sopenharmony_ci#else
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci#define f2fs_xattr_handlers	NULL
14062306a36Sopenharmony_ci#define f2fs_listxattr		NULL
14162306a36Sopenharmony_cistatic inline int f2fs_setxattr(struct inode *inode, int index,
14262306a36Sopenharmony_ci		const char *name, const void *value, size_t size,
14362306a36Sopenharmony_ci		struct page *page, int flags)
14462306a36Sopenharmony_ci{
14562306a36Sopenharmony_ci	return -EOPNOTSUPP;
14662306a36Sopenharmony_ci}
14762306a36Sopenharmony_cistatic inline int f2fs_getxattr(struct inode *inode, int index,
14862306a36Sopenharmony_ci			const char *name, void *buffer,
14962306a36Sopenharmony_ci			size_t buffer_size, struct page *dpage)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	return -EOPNOTSUPP;
15262306a36Sopenharmony_ci}
15362306a36Sopenharmony_cistatic inline int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) { return 0; }
15462306a36Sopenharmony_cistatic inline void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) { }
15562306a36Sopenharmony_ci#endif
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci#ifdef CONFIG_F2FS_FS_SECURITY
15862306a36Sopenharmony_ciextern int f2fs_init_security(struct inode *, struct inode *,
15962306a36Sopenharmony_ci				const struct qstr *, struct page *);
16062306a36Sopenharmony_ci#else
16162306a36Sopenharmony_cistatic inline int f2fs_init_security(struct inode *inode, struct inode *dir,
16262306a36Sopenharmony_ci				const struct qstr *qstr, struct page *ipage)
16362306a36Sopenharmony_ci{
16462306a36Sopenharmony_ci	return 0;
16562306a36Sopenharmony_ci}
16662306a36Sopenharmony_ci#endif
16762306a36Sopenharmony_ci#endif /* __F2FS_XATTR_H__ */
168