xref: /freebsd/sys/fs/ext2fs/ext2_extattr.h (revision fd5e3f3ec6c6248e892c9e7b2f17da3bfe7b6837)
1 /*-
2  * Copyright (c) 2017, Fedor Uporov
3  * All rights reserved.
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  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28 
29 #ifndef _FS_EXT2FS_EXT2_EXTARTTR_H_
30 #define	_FS_EXT2FS_EXT2_EXTARTTR_H_
31 
32 /* Linux xattr name indexes */
33 #define	EXT4_XATTR_INDEX_USER			1
34 #define	EXT4_XATTR_INDEX_POSIX_ACL_ACCESS	2
35 #define	EXT4_XATTR_INDEX_POSIX_ACL_DEFAULT	3
36 #define	EXT4_XATTR_INDEX_TRUSTED		4
37 #define	EXT4_XATTR_INDEX_LUSTRE			5
38 #define	EXT4_XATTR_INDEX_SECURITY		6
39 #define	EXT4_XATTR_INDEX_SYSTEM			7
40 #define	EXT4_XATTR_INDEX_RICHACL		8
41 #define	EXT4_XATTR_INDEX_ENCRYPTION		9
42 
43 /* Magic value in attribute blocks */
44 #define EXTATTR_MAGIC 0xEA020000
45 
46 /* Max EA name length */
47 #define EXT2_EXTATTR_NAMELEN_MAX		255
48 
49 /* EA hash constants */
50 #define EXT2_EXTATTR_NAME_HASH_SHIFT		5
51 #define EXT2_EXTATTR_VALUE_HASH_SHIFT		16
52 #define EXT2_EXTATTR_BLOCK_HASH_SHIFT		16
53 
54 
55 struct ext2fs_extattr_header {
56 	int32_t	h_magic;	/* magic number for identification */
57 	int32_t	h_refcount;	/* reference count */
58 	int32_t	h_blocks;	/* number of disk blocks used */
59 	int32_t	h_hash;		/* hash value of all attributes */
60 	uint32_t h_reserved[4];	/* zero right now */
61 };
62 
63 struct ext2fs_extattr_dinode_header {
64 	int32_t	h_magic;	/* magic number for identification */
65 };
66 
67 struct ext2fs_extattr_entry {
68 	uint8_t	e_name_len;		/* length of name */
69 	uint8_t	e_name_index;		/* attribute name index */
70 	uint16_t	e_value_offs;	/* offset in disk block of value */
71 	uint32_t	e_value_block;	/* disk block attribute is stored on (n/i) */
72 	uint32_t	e_value_size;	/* size of attribute value */
73 	uint32_t	e_hash;		/* hash value of name and value */
74 	char	e_name[0];		/* attribute name */
75 };
76 
77 #define EXT2_IFIRST(hdr) ((struct ext2fs_extattr_entry *)((hdr)+1))
78 
79 #define EXT2_HDR(bh) ((struct ext2fs_extattr_header *)((bh)->b_data))
80 #define EXT2_ENTRY(ptr) ((struct ext2fs_extattr_entry *)(ptr))
81 #define EXT2_FIRST_ENTRY(bh) EXT2_ENTRY(EXT2_HDR(bh)+1)
82 #define EXT2_IS_LAST_ENTRY(entry) (*(uint32_t *)(entry) == 0)
83 
84 #define EXT2_EXTATTR_PAD_BITS		2
85 #define EXT2_EXTATTR_PAD		(1<<EXT2_EXTATTR_PAD_BITS)
86 #define EXT2_EXTATTR_ROUND		(EXT2_EXTATTR_PAD-1)
87 #define EXT2_EXTATTR_LEN(name_len) \
88 	(((name_len) + EXT2_EXTATTR_ROUND + \
89 	    sizeof(struct ext2fs_extattr_entry)) & ~EXT2_EXTATTR_ROUND)
90 
91 #define EXT2_EXTATTR_SIZE(size) \
92     (((size) + EXT2_EXTATTR_ROUND) & ~EXT2_EXTATTR_ROUND)
93 
94 #define EXT2_EXTATTR_NEXT(entry) \
95 	( (struct ext2fs_extattr_entry *)( \
96 	    (char *)(entry) + EXT2_EXTATTR_LEN((entry)->e_name_len)) )
97 
98 int ext2_extattr_inode_delete(struct inode *ip, int attrnamespace,
99     const char *name);
100 
101 int ext2_extattr_block_delete(struct inode *ip, int attrnamespace,
102     const char *name);
103 
104 int ext2_extattr_free(struct inode *ip);
105 int ext2_extattr_inode_list(struct inode *ip, int attrnamespace,
106     struct uio *uio, size_t *size);
107 
108 int ext2_extattr_block_list(struct inode *ip, int attrnamespace,
109     struct uio *uio, size_t *size);
110 
111 int ext2_extattr_inode_get(struct inode *ip, int attrnamespace,
112     const char *name, struct uio *uio, size_t *size);
113 
114 int ext2_extattr_block_get(struct inode *ip, int attrnamespace,
115     const char *name, struct uio *uio, size_t *size);
116 
117 int ext2_extattr_inode_set(struct inode *ip, int attrnamespace,
118     const char *name, struct uio *uio);
119 
120 int ext2_extattr_block_set(struct inode *ip, int attrnamespace,
121     const char *name, struct uio *uio);
122 
123 int ext2_extattr_valid_attrname(int attrnamespace, const char *attrname);
124 
125 #endif	/* !_FS_EXT2FS_EXT2_EXTARTTR_H_ */
126