1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * fs/f2fs/xattr.h
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 *
8 * Portions of this code from linux/fs/ext2/xattr.h
9 *
10 * On-disk format of extended attributes for the ext2 filesystem.
11 *
12 * (C) 2001 Andreas Gruenbacher, <a.gruenbacher@computer.org>
13 */
14 #ifndef __F2FS_XATTR_H__
15 #define __F2FS_XATTR_H__
16
17 #include <linux/init.h>
18 #include <linux/xattr.h>
19
20 /* Magic value in attribute blocks */
21 #define F2FS_XATTR_MAGIC 0xF2F52011
22
23 /* Maximum number of references to one attribute block */
24 #define F2FS_XATTR_REFCOUNT_MAX 1024
25
26 /* Name indexes */
27 #define F2FS_SYSTEM_ADVISE_NAME "system.advise"
28 #define F2FS_XATTR_INDEX_USER 1
29 #define F2FS_XATTR_INDEX_POSIX_ACL_ACCESS 2
30 #define F2FS_XATTR_INDEX_POSIX_ACL_DEFAULT 3
31 #define F2FS_XATTR_INDEX_TRUSTED 4
32 #define F2FS_XATTR_INDEX_LUSTRE 5
33 #define F2FS_XATTR_INDEX_SECURITY 6
34 #define F2FS_XATTR_INDEX_ADVISE 7
35 /* Should be same as EXT4_XATTR_INDEX_ENCRYPTION */
36 #define F2FS_XATTR_INDEX_ENCRYPTION 9
37 #define F2FS_XATTR_INDEX_VERITY 11
38
39 #define F2FS_XATTR_NAME_ENCRYPTION_CONTEXT "c"
40 #define F2FS_XATTR_NAME_VERITY "v"
41
42 struct f2fs_xattr_header {
43 __le32 h_magic; /* magic number for identification */
44 __le32 h_refcount; /* reference count */
45 __u32 h_reserved[4]; /* zero right now */
46 };
47
48 struct f2fs_xattr_entry {
49 __u8 e_name_index;
50 __u8 e_name_len;
51 __le16 e_value_size; /* size of attribute value */
52 char e_name[]; /* attribute name */
53 };
54
55 #define XATTR_HDR(ptr) ((struct f2fs_xattr_header *)(ptr))
56 #define XATTR_ENTRY(ptr) ((struct f2fs_xattr_entry *)(ptr))
57 #define XATTR_FIRST_ENTRY(ptr) (XATTR_ENTRY(XATTR_HDR(ptr) + 1))
58 #define XATTR_ROUND (3)
59
60 #define XATTR_ALIGN(size) (((size) + XATTR_ROUND) & ~XATTR_ROUND)
61
62 #define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
63 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size)))
64
65 #define XATTR_NEXT_ENTRY(entry) ((struct f2fs_xattr_entry *)((char *)(entry) +\
66 ENTRY_SIZE(entry)))
67
68 #define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
69
70 #define list_for_each_xattr(entry, addr) \
71 for (entry = XATTR_FIRST_ENTRY(addr);\
72 !IS_XATTR_LAST_ENTRY(entry);\
73 entry = XATTR_NEXT_ENTRY(entry))
74 #define VALID_XATTR_BLOCK_SIZE (PAGE_SIZE - sizeof(struct node_footer))
75 #define XATTR_PADDING_SIZE (sizeof(__u32))
76 #define XATTR_SIZE(i) ((F2FS_I(i)->i_xattr_nid ? \
77 VALID_XATTR_BLOCK_SIZE : 0) + \
78 (inline_xattr_size(i)))
79 #define MIN_OFFSET(i) XATTR_ALIGN(inline_xattr_size(i) + \
80 VALID_XATTR_BLOCK_SIZE)
81
82 #define MAX_VALUE_LEN(i) (MIN_OFFSET(i) - \
83 sizeof(struct f2fs_xattr_header) - \
84 sizeof(struct f2fs_xattr_entry))
85
86 #define MIN_INLINE_XATTR_SIZE (sizeof(struct f2fs_xattr_header) / sizeof(__le32))
87 #define MAX_INLINE_XATTR_SIZE \
88 (DEF_ADDRS_PER_INODE - \
89 F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) - \
90 DEF_INLINE_RESERVED_SIZE - \
91 MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
92 #define DEFAULT_XATTR_SLAB_SIZE (DEFAULT_INLINE_XATTR_ADDRS * \
93 sizeof(__le32) + XATTR_PADDING_SIZE)
94
95 /*
96 * On-disk structure of f2fs_xattr
97 * We use inline xattrs space + 1 block for xattr.
98 *
99 * +--------------------+
100 * | f2fs_xattr_header |
101 * | |
102 * +--------------------+
103 * | f2fs_xattr_entry |
104 * | .e_name_index = 1 |
105 * | .e_name_len = 3 |
106 * | .e_value_size = 14 |
107 * | .e_name = "foo" |
108 * | "value_of_xattr" |<- value_offs = e_name + e_name_len
109 * +--------------------+
110 * | f2fs_xattr_entry |
111 * | .e_name_index = 4 |
112 * | .e_name = "bar" |
113 * +--------------------+
114 * | |
115 * | Free |
116 * | |
117 * +--------------------+<- MIN_OFFSET
118 * | node_footer |
119 * | (nid, ino, offset) |
120 * +--------------------+
121 *
122 **/
123
124 #ifdef CONFIG_F2FS_FS_XATTR
125 extern const struct xattr_handler f2fs_xattr_user_handler;
126 extern const struct xattr_handler f2fs_xattr_trusted_handler;
127 extern const struct xattr_handler f2fs_xattr_advise_handler;
128 extern const struct xattr_handler f2fs_xattr_security_handler;
129
130 extern const struct xattr_handler * const f2fs_xattr_handlers[];
131
132 int f2fs_setxattr(struct inode *, int, const char *, const void *,
133 size_t, struct folio *, int);
134 int f2fs_getxattr(struct inode *, int, const char *, void *,
135 size_t, struct folio *);
136 ssize_t f2fs_listxattr(struct dentry *, char *, size_t);
137 int __init f2fs_init_xattr_cache(void);
138 void f2fs_destroy_xattr_cache(void);
139 #else
140
141 #define f2fs_xattr_handlers NULL
142 #define f2fs_listxattr NULL
f2fs_setxattr(struct inode * inode,int index,const char * name,const void * value,size_t size,struct folio * folio,int flags)143 static inline int f2fs_setxattr(struct inode *inode, int index,
144 const char *name, const void *value, size_t size,
145 struct folio *folio, int flags)
146 {
147 return -EOPNOTSUPP;
148 }
f2fs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size,struct folio * dfolio)149 static inline int f2fs_getxattr(struct inode *inode, int index,
150 const char *name, void *buffer,
151 size_t buffer_size, struct folio *dfolio)
152 {
153 return -EOPNOTSUPP;
154 }
f2fs_init_xattr_cache(void)155 static inline int __init f2fs_init_xattr_cache(void) { return 0; }
f2fs_destroy_xattr_cache(void)156 static inline void f2fs_destroy_xattr_cache(void) { }
157 #endif
158
159 #ifdef CONFIG_F2FS_FS_SECURITY
160 int f2fs_init_security(struct inode *, struct inode *,
161 const struct qstr *, struct folio *);
162 #else
f2fs_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct folio * ifolio)163 static inline int f2fs_init_security(struct inode *inode, struct inode *dir,
164 const struct qstr *qstr, struct folio *ifolio)
165 {
166 return 0;
167 }
168 #endif
169 #endif /* __F2FS_XATTR_H__ */
170