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
38 #define F2FS_XATTR_NAME_ENCRYPTION_CONTEXT "c"
39
40 struct f2fs_xattr_header {
41 __le32 h_magic; /* magic number for identification */
42 __le32 h_refcount; /* reference count */
43 __u32 h_reserved[4]; /* zero right now */
44 };
45
46 struct f2fs_xattr_entry {
47 __u8 e_name_index;
48 __u8 e_name_len;
49 __le16 e_value_size; /* size of attribute value */
50 char e_name[0]; /* attribute name */
51 };
52
53 #define XATTR_HDR(ptr) ((struct f2fs_xattr_header *)(ptr))
54 #define XATTR_ENTRY(ptr) ((struct f2fs_xattr_entry *)(ptr))
55 #define XATTR_FIRST_ENTRY(ptr) (XATTR_ENTRY(XATTR_HDR(ptr) + 1))
56 #define XATTR_ROUND (3)
57
58 #define XATTR_ALIGN(size) (((size) + XATTR_ROUND) & ~XATTR_ROUND)
59
60 #define ENTRY_SIZE(entry) (XATTR_ALIGN(sizeof(struct f2fs_xattr_entry) + \
61 (entry)->e_name_len + le16_to_cpu((entry)->e_value_size)))
62
63 #define XATTR_NEXT_ENTRY(entry) ((struct f2fs_xattr_entry *)((char *)(entry) +\
64 ENTRY_SIZE(entry)))
65
66 #define IS_XATTR_LAST_ENTRY(entry) (*(__u32 *)(entry) == 0)
67
68 #define list_for_each_xattr(entry, addr) \
69 for (entry = XATTR_FIRST_ENTRY(addr);\
70 !IS_XATTR_LAST_ENTRY(entry);\
71 entry = XATTR_NEXT_ENTRY(entry))
72 #define VALID_XATTR_BLOCK_SIZE (PAGE_SIZE - sizeof(struct node_footer))
73 #define XATTR_PADDING_SIZE (sizeof(__u32))
74 #define MIN_OFFSET(i) XATTR_ALIGN(inline_xattr_size(i) + \
75 VALID_XATTR_BLOCK_SIZE)
76
77 #define MAX_VALUE_LEN(i) (MIN_OFFSET(i) - \
78 sizeof(struct f2fs_xattr_header) - \
79 sizeof(struct f2fs_xattr_entry))
80
81 #define MAX_INLINE_XATTR_SIZE \
82 (DEF_ADDRS_PER_INODE - \
83 F2FS_TOTAL_EXTRA_ATTR_SIZE / sizeof(__le32) - \
84 DEF_INLINE_RESERVED_SIZE - \
85 MIN_INLINE_DENTRY_SIZE / sizeof(__le32))
86
87 /*
88 * On-disk structure of f2fs_xattr
89 * We use inline xattrs space + 1 block for xattr.
90 *
91 * +--------------------+
92 * | f2fs_xattr_header |
93 * | |
94 * +--------------------+
95 * | f2fs_xattr_entry |
96 * | .e_name_index = 1 |
97 * | .e_name_len = 3 |
98 * | .e_value_size = 14 |
99 * | .e_name = "foo" |
100 * | "value_of_xattr" |<- value_offs = e_name + e_name_len
101 * +--------------------+
102 * | f2fs_xattr_entry |
103 * | .e_name_index = 4 |
104 * | .e_name = "bar" |
105 * +--------------------+
106 * | |
107 * | Free |
108 * | |
109 * +--------------------+<- MIN_OFFSET
110 * | node_footer |
111 * | (nid, ino, offset) |
112 * +--------------------+
113 *
114 **/
115
116 #ifdef CONFIG_F2FS_FS_XATTR
117 extern const struct xattr_handler f2fs_xattr_user_handler;
118 extern const struct xattr_handler f2fs_xattr_trusted_handler;
119 extern const struct xattr_handler f2fs_xattr_advise_handler;
120 extern const struct xattr_handler f2fs_xattr_security_handler;
121
122 extern const struct xattr_handler *f2fs_xattr_handlers[];
123
124 extern int f2fs_setxattr(struct inode *, int, const char *,
125 const void *, size_t, struct page *, int);
126 extern int f2fs_getxattr(struct inode *, int, const char *, void *,
127 size_t, struct page *);
128 extern ssize_t f2fs_listxattr(struct dentry *, char *, size_t);
129 #else
130
131 #define f2fs_xattr_handlers NULL
f2fs_setxattr(struct inode * inode,int index,const char * name,const void * value,size_t size,struct page * page,int flags)132 static inline int f2fs_setxattr(struct inode *inode, int index,
133 const char *name, const void *value, size_t size,
134 struct page *page, int flags)
135 {
136 return -EOPNOTSUPP;
137 }
f2fs_getxattr(struct inode * inode,int index,const char * name,void * buffer,size_t buffer_size,struct page * dpage)138 static inline int f2fs_getxattr(struct inode *inode, int index,
139 const char *name, void *buffer,
140 size_t buffer_size, struct page *dpage)
141 {
142 return -EOPNOTSUPP;
143 }
f2fs_listxattr(struct dentry * dentry,char * buffer,size_t buffer_size)144 static inline ssize_t f2fs_listxattr(struct dentry *dentry, char *buffer,
145 size_t buffer_size)
146 {
147 return -EOPNOTSUPP;
148 }
149 #endif
150
151 #ifdef CONFIG_F2FS_FS_SECURITY
152 extern int f2fs_init_security(struct inode *, struct inode *,
153 const struct qstr *, struct page *);
154 #else
f2fs_init_security(struct inode * inode,struct inode * dir,const struct qstr * qstr,struct page * ipage)155 static inline int f2fs_init_security(struct inode *inode, struct inode *dir,
156 const struct qstr *qstr, struct page *ipage)
157 {
158 return 0;
159 }
160 #endif
161 #endif /* __F2FS_XATTR_H__ */
162