1 /*
2  * Copyright (c) 2015 Grzegorz Kostka (kostka.grzegorz@gmail.com)
3  * Copyright (c) 2015 Kaho Ng (ngkaho1234@gmail.com)
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in the
13  *   documentation and/or other materials provided with the distribution.
14  * - The name of the author may not be used to endorse or promote products
15  *   derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /** @addtogroup lwext4
30  * @{
31  */
32 /**
33  * @file  ext4_xattr.h
34  * @brief Extended Attribute manipulation.
35  */
36 
37 #ifndef EXT4_XATTR_H_
38 #define EXT4_XATTR_H_
39 
40 #include <ext2fs.h>
41 #include <linux/rbtree.h>
42 
43 /* Extended Attribute(EA) */
44 
45 /* Magic value in attribute blocks */
46 #define EXT4_XATTR_MAGIC		0xEA020000
47 
48 /* Maximum number of references to one attribute block */
49 #define EXT4_XATTR_REFCOUNT_MAX		1024
50 
51 /* Name indexes */
52 #define EXT4_XATTR_INDEX_USER			1
53 #define EXT4_XATTR_INDEX_POSIX_ACL_ACCESS	2
54 #define EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT	3
55 #define EXT4_XATTR_INDEX_TRUSTED		4
56 #define	EXT4_XATTR_INDEX_LUSTRE			5
57 #define EXT4_XATTR_INDEX_SECURITY	        6
58 #define EXT4_XATTR_INDEX_SYSTEM			7
59 #define EXT4_XATTR_INDEX_RICHACL		8
60 #define EXT4_XATTR_INDEX_ENCRYPTION		9
61 
62 #pragma pack(push, 1)
63 
64 struct ext4_xattr_header {
65 	__le32 h_magic;	/* magic number for identification */
66 	__le32 h_refcount;	/* reference count */
67 	__le32 h_blocks;	/* number of disk blocks used */
68 	__le32 h_hash;		/* hash value of all attributes */
69 	__le32 h_checksum;	/* crc32c(uuid+id+xattrblock) */
70 				/* id = inum if refcount=1, blknum otherwise */
71 	__le32 h_reserved[3];	/* zero right now */
72 };
73 
74 struct ext4_xattr_ibody_header {
75 	__le32 h_magic;	/* magic number for identification */
76 };
77 
78 struct ext4_xattr_entry {
79 	__u8 e_name_len;	/* length of name */
80 	__u8 e_name_index;	/* attribute name index */
81 	__le16 e_value_offs;	/* offset in disk block of value */
82 	__le32 e_value_block;	/* disk block attribute is stored on (n/i) */
83 	__le32 e_value_size;	/* size of attribute value */
84 	__le32 e_hash;		/* hash value of name and value */
85 };
86 
87 #pragma pack(pop)
88 
89 #define EXT4_GOOD_OLD_INODE_SIZE	EXT2_GOOD_OLD_INODE_SIZE
90 
91 #define EXT4_XATTR_PAD_BITS		2
92 #define EXT4_XATTR_PAD		(1<<EXT4_XATTR_PAD_BITS)
93 #define EXT4_XATTR_ROUND		(EXT4_XATTR_PAD-1)
94 #define EXT4_XATTR_LEN(name_len) \
95 	(((name_len) + EXT4_XATTR_ROUND + \
96 	sizeof(struct ext4_xattr_entry)) & ~EXT4_XATTR_ROUND)
97 #define EXT4_XATTR_NEXT(entry) \
98 	((struct ext4_xattr_entry *)( \
99 	 (char *)(entry) + EXT4_XATTR_LEN((entry)->e_name_len)))
100 #define EXT4_XATTR_SIZE(size) \
101 	(((size) + EXT4_XATTR_ROUND) & ~EXT4_XATTR_ROUND)
102 #define EXT4_XATTR_NAME(entry) \
103 	((char *)((entry) + 1))
104 
105 #define EXT4_XATTR_IHDR(raw_inode) \
106 	((struct ext4_xattr_ibody_header *) \
107 		((char *)raw_inode + \
108 		EXT4_GOOD_OLD_INODE_SIZE + \
109 		(raw_inode)->i_extra_isize))
110 #define EXT4_XATTR_IFIRST(hdr) \
111 	((struct ext4_xattr_entry *)((hdr)+1))
112 
113 #define EXT4_XATTR_BHDR(block) \
114 	((struct ext4_xattr_header *)((block)->b_data))
115 #define EXT4_XATTR_ENTRY(ptr) \
116 	((struct ext4_xattr_entry *)(ptr))
117 #define EXT4_XATTR_BFIRST(block) \
118 	EXT4_XATTR_ENTRY(EXT4_XATTR_BHDR(block)+1)
119 #define EXT4_XATTR_IS_LAST_ENTRY(entry) \
120 	(*(__le32 *)(entry) == 0)
121 
122 #define EXT4_ZERO_XATTR_VALUE ((void *)-1)
123 
124 
125 struct ext4_xattr_item {
126 	/* This attribute should be stored in inode body */
127 	BOOL in_inode;
128 	BOOL is_data;
129 
130 	__u8 name_index;
131 	char  *name;
132 	size_t name_len;
133 	void  *data;
134 	size_t data_size;
135 
136 	struct rb_node node;
137 	struct list_head list_node;
138 };
139 
140 struct ext4_xattr_ref {
141 	PEXT2_IRP_CONTEXT IrpContext;
142 	BOOL block_loaded;
143 	struct buffer_head *block_bh;
144 	PEXT2_MCB inode_ref;
145 
146 	PEXT2_INODE OnDiskInode;
147 	BOOL IsOnDiskInodeDirty;
148 
149 	BOOL   dirty;
150 	size_t ea_size;
151 	size_t inode_size_rem;
152 	size_t block_size_rem;
153 	PEXT2_VCB fs;
154 
155 	void *iter_arg;
156 	struct ext4_xattr_item *iter_from;
157 
158 	struct rb_root root;
159 	struct list_head ordered_list;
160 };
161 
162 #define EXT4_XATTR_ITERATE_CONT 0
163 #define EXT4_XATTR_ITERATE_STOP 1
164 #define EXT4_XATTR_ITERATE_PAUSE 2
165 
166 int ext4_fs_get_xattr_ref(PEXT2_IRP_CONTEXT IrpContext, PEXT2_VCB fs, PEXT2_MCB inode_ref,
167 	struct ext4_xattr_ref *ref);
168 
169 int ext4_fs_put_xattr_ref(struct ext4_xattr_ref *ref);
170 
171 int ext4_fs_set_xattr(struct ext4_xattr_ref *ref, __u8 name_index,
172 		      const char *name, size_t name_len, const void *data,
173 		      size_t data_size, BOOL replace);
174 
175 int ext4_fs_set_xattr_ordered(struct ext4_xattr_ref *ref, __u8 name_index,
176 	const char *name, size_t name_len, const void *data,
177 	size_t data_size);
178 
179 int ext4_fs_remove_xattr(struct ext4_xattr_ref *ref, __u8 name_index,
180 			 const char *name, size_t name_len);
181 
182 int ext4_fs_get_xattr(struct ext4_xattr_ref *ref, __u8 name_index,
183 		      const char *name, size_t name_len, void *buf,
184 		      size_t buf_size, size_t *data_size);
185 
186 void ext4_fs_xattr_iterate(struct ext4_xattr_ref *ref,
187 	int(*iter)(struct ext4_xattr_ref *ref,
188 		struct ext4_xattr_item *item,
189 		BOOL is_last));
190 
191 void ext4_fs_xattr_iterate_reset(struct ext4_xattr_ref *ref);
192 
193 const char *ext4_extract_xattr_name(const char *full_name, size_t full_name_len,
194 			      __u8 *name_index, size_t *name_len,
195 			      BOOL *found);
196 
197 const char *ext4_get_xattr_name_prefix(__u8 name_index,
198 				       size_t *ret_prefix_len);
199 
200 void ext4_xattr_purge_items(struct ext4_xattr_ref *xattr_ref);
201 
202 #endif
203 /**
204  * @}
205  */
206