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