18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * fs/f2fs/xattr.h
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (c) 2012 Samsung Electronics Co., Ltd.
68c2ecf20Sopenharmony_ci *             http://www.samsung.com/
78c2ecf20Sopenharmony_ci *
88c2ecf20Sopenharmony_ci * Portions of this code from linux/fs/ext2/xattr.h
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * On-disk format of extended attributes for the ext2 filesystem.
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
138c2ecf20Sopenharmony_ci */
148c2ecf20Sopenharmony_ci#ifndef __F2FS_XATTR_H__
158c2ecf20Sopenharmony_ci#define __F2FS_XATTR_H__
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/xattr.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Magic value in attribute blocks */
218c2ecf20Sopenharmony_ci#define F2FS_XATTR_MAGIC                0xF2F52011
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Maximum number of references to one attribute block */
248c2ecf20Sopenharmony_ci#define F2FS_XATTR_REFCOUNT_MAX         1024
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci/* Name indexes */
278c2ecf20Sopenharmony_ci#define F2FS_SYSTEM_ADVISE_NAME			"system.advise"
288c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_USER			1
298c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS	2
308c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT	3
318c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_TRUSTED		4
328c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_LUSTRE			5
338c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_SECURITY		6
348c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_ADVISE			7
358c2ecf20Sopenharmony_ci/* Should be same as EXT4_XATTR_INDEX_ENCRYPTION */
368c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_ENCRYPTION		9
378c2ecf20Sopenharmony_ci#define F2FS_XATTR_INDEX_VERITY			11
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#define F2FS_XATTR_NAME_ENCRYPTION_CONTEXT	"c"
408c2ecf20Sopenharmony_ci#define F2FS_XATTR_NAME_VERITY			"v"
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_cistruct f2fs_xattr_header {
438c2ecf20Sopenharmony_ci	__le32  h_magic;        /* magic number for identification */
448c2ecf20Sopenharmony_ci	__le32  h_refcount;     /* reference count */
458c2ecf20Sopenharmony_ci	__u32   h_reserved[4];  /* zero right now */
468c2ecf20Sopenharmony_ci};
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_cistruct f2fs_xattr_entry {
498c2ecf20Sopenharmony_ci	__u8    e_name_index;
508c2ecf20Sopenharmony_ci	__u8    e_name_len;
518c2ecf20Sopenharmony_ci	__le16  e_value_size;   /* size of attribute value */
528c2ecf20Sopenharmony_ci	char    e_name[];      /* attribute name */
538c2ecf20Sopenharmony_ci};
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define XATTR_HDR(ptr)		((struct f2fs_xattr_header *)(ptr))
568c2ecf20Sopenharmony_ci#define XATTR_ENTRY(ptr)	((struct f2fs_xattr_entry *)(ptr))
578c2ecf20Sopenharmony_ci#define XATTR_FIRST_ENTRY(ptr)	(XATTR_ENTRY(XATTR_HDR(ptr) + 1))
588c2ecf20Sopenharmony_ci#define XATTR_ROUND		(3)
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci#define XATTR_ALIGN(size)	(((size) + XATTR_ROUND) & ~XATTR_ROUND)
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
638c2ecf20Sopenharmony_ci			(entry)->e_name_len + le16_to_cpu((entry)->e_value_size)))
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define XATTR_NEXT_ENTRY(entry)	((struct f2fs_xattr_entry *)((char *)(entry) +\
668c2ecf20Sopenharmony_ci			ENTRY_SIZE(entry)))
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#define list_for_each_xattr(entry, addr) \
718c2ecf20Sopenharmony_ci		for (entry = XATTR_FIRST_ENTRY(addr);\
728c2ecf20Sopenharmony_ci				!IS_XATTR_LAST_ENTRY(entry);\
738c2ecf20Sopenharmony_ci				entry = XATTR_NEXT_ENTRY(entry))
748c2ecf20Sopenharmony_ci#define VALID_XATTR_BLOCK_SIZE	(PAGE_SIZE - sizeof(struct node_footer))
758c2ecf20Sopenharmony_ci#define XATTR_PADDING_SIZE	(sizeof(__u32))
768c2ecf20Sopenharmony_ci#define XATTR_SIZE(i)		((F2FS_I(i)->i_xattr_nid ?		\
778c2ecf20Sopenharmony_ci					VALID_XATTR_BLOCK_SIZE : 0) +	\
788c2ecf20Sopenharmony_ci						(inline_xattr_size(i)))
798c2ecf20Sopenharmony_ci#define MIN_OFFSET(i)		XATTR_ALIGN(inline_xattr_size(i) +	\
808c2ecf20Sopenharmony_ci						VALID_XATTR_BLOCK_SIZE)
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci#define MAX_VALUE_LEN(i)	(MIN_OFFSET(i) -			\
838c2ecf20Sopenharmony_ci				sizeof(struct f2fs_xattr_header) -	\
848c2ecf20Sopenharmony_ci				sizeof(struct f2fs_xattr_entry))
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci#define MAX_INLINE_XATTR_SIZE						\
878c2ecf20Sopenharmony_ci			(DEF_ADDRS_PER_INODE -				\
888c2ecf20Sopenharmony_ci			F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) -	\
898c2ecf20Sopenharmony_ci			DEF_INLINE_RESERVED_SIZE -			\
908c2ecf20Sopenharmony_ci			MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_ci/*
938c2ecf20Sopenharmony_ci * On-disk structure of f2fs_xattr
948c2ecf20Sopenharmony_ci * We use inline xattrs space + 1 block for xattr.
958c2ecf20Sopenharmony_ci *
968c2ecf20Sopenharmony_ci * +--------------------+
978c2ecf20Sopenharmony_ci * | f2fs_xattr_header  |
988c2ecf20Sopenharmony_ci * |                    |
998c2ecf20Sopenharmony_ci * +--------------------+
1008c2ecf20Sopenharmony_ci * | f2fs_xattr_entry   |
1018c2ecf20Sopenharmony_ci * | .e_name_index = 1  |
1028c2ecf20Sopenharmony_ci * | .e_name_len = 3    |
1038c2ecf20Sopenharmony_ci * | .e_value_size = 14 |
1048c2ecf20Sopenharmony_ci * | .e_name = "foo"    |
1058c2ecf20Sopenharmony_ci * | "value_of_xattr"   |<- value_offs = e_name + e_name_len
1068c2ecf20Sopenharmony_ci * +--------------------+
1078c2ecf20Sopenharmony_ci * | f2fs_xattr_entry   |
1088c2ecf20Sopenharmony_ci * | .e_name_index = 4  |
1098c2ecf20Sopenharmony_ci * | .e_name = "bar"    |
1108c2ecf20Sopenharmony_ci * +--------------------+
1118c2ecf20Sopenharmony_ci * |                    |
1128c2ecf20Sopenharmony_ci * |        Free        |
1138c2ecf20Sopenharmony_ci * |                    |
1148c2ecf20Sopenharmony_ci * +--------------------+<- MIN_OFFSET
1158c2ecf20Sopenharmony_ci * |   node_footer      |
1168c2ecf20Sopenharmony_ci * | (nid, ino, offset) |
1178c2ecf20Sopenharmony_ci * +--------------------+
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci **/
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#ifdef CONFIG_F2FS_FS_XATTR
1228c2ecf20Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_user_handler;
1238c2ecf20Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_trusted_handler;
1248c2ecf20Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_advise_handler;
1258c2ecf20Sopenharmony_ciextern const struct xattr_handler f2fs_xattr_security_handler;
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_ciextern const struct xattr_handler *f2fs_xattr_handlers[];
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ciextern int f2fs_setxattr(struct inode *, int, const char *,
1308c2ecf20Sopenharmony_ci				const void *, size_t, struct page *, int);
1318c2ecf20Sopenharmony_ciextern int f2fs_getxattr(struct inode *, int, const char *, void *,
1328c2ecf20Sopenharmony_ci						size_t, struct page *);
1338c2ecf20Sopenharmony_ciextern ssize_t f2fs_listxattr(struct dentry *, char *, size_t);
1348c2ecf20Sopenharmony_ciextern int f2fs_init_xattr_caches(struct f2fs_sb_info *);
1358c2ecf20Sopenharmony_ciextern void f2fs_destroy_xattr_caches(struct f2fs_sb_info *);
1368c2ecf20Sopenharmony_ci#else
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci#define f2fs_xattr_handlers	NULL
1398c2ecf20Sopenharmony_ci#define f2fs_listxattr		NULL
1408c2ecf20Sopenharmony_cistatic inline int f2fs_setxattr(struct inode *inode, int index,
1418c2ecf20Sopenharmony_ci		const char *name, const void *value, size_t size,
1428c2ecf20Sopenharmony_ci		struct page *page, int flags)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
1458c2ecf20Sopenharmony_ci}
1468c2ecf20Sopenharmony_cistatic inline int f2fs_getxattr(struct inode *inode, int index,
1478c2ecf20Sopenharmony_ci			const char *name, void *buffer,
1488c2ecf20Sopenharmony_ci			size_t buffer_size, struct page *dpage)
1498c2ecf20Sopenharmony_ci{
1508c2ecf20Sopenharmony_ci	return -EOPNOTSUPP;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_cistatic inline int f2fs_init_xattr_caches(struct f2fs_sb_info *sbi) { return 0; }
1538c2ecf20Sopenharmony_cistatic inline void f2fs_destroy_xattr_caches(struct f2fs_sb_info *sbi) { }
1548c2ecf20Sopenharmony_ci#endif
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#ifdef CONFIG_F2FS_FS_SECURITY
1578c2ecf20Sopenharmony_ciextern int f2fs_init_security(struct inode *, struct inode *,
1588c2ecf20Sopenharmony_ci				const struct qstr *, struct page *);
1598c2ecf20Sopenharmony_ci#else
1608c2ecf20Sopenharmony_cistatic inline int f2fs_init_security(struct inode *inode, struct inode *dir,
1618c2ecf20Sopenharmony_ci				const struct qstr *qstr, struct page *ipage)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	return 0;
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci#endif
1668c2ecf20Sopenharmony_ci#endif /* __F2FS_XATTR_H__ */
167