xref: /freebsd/sys/contrib/openzfs/include/sys/sa.h (revision 61145dc2b94f12f6a47344fb9aac702321880e43)
1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3  * CDDL HEADER START
4  *
5  * The contents of this file are subject to the terms of the
6  * Common Development and Distribution License (the "License").
7  * You may not use this file except in compliance with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or https://opensource.org/licenses/CDDL-1.0.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25 
26 #ifndef	_SYS_SA_H
27 #define	_SYS_SA_H
28 
29 #include <sys/dmu.h>
30 
31 /*
32  * Currently available byteswap functions.
33  * If it all possible new attributes should used
34  * one of the already defined byteswap functions.
35  * If a new byteswap function is added then the
36  * ZPL/Pool version will need to be bumped.
37  */
38 
39 typedef enum sa_bswap_type {
40 	SA_UINT64_ARRAY,
41 	SA_UINT32_ARRAY,
42 	SA_UINT16_ARRAY,
43 	SA_UINT8_ARRAY,
44 	SA_ACL,
45 } sa_bswap_type_t;
46 
47 typedef uint16_t	sa_attr_type_t;
48 
49 /*
50  * Attribute to register support for.
51  */
52 typedef struct sa_attr_reg {
53 	const char		*sa_name;	/* attribute name */
54 	uint16_t		sa_length;
55 	sa_bswap_type_t		sa_byteswap;	/* bswap function enum */
56 	sa_attr_type_t		sa_attr; /* filled in during registration */
57 } sa_attr_reg_t;
58 
59 
60 typedef void (sa_data_locator_t)(void **, uint32_t *, uint32_t,
61     boolean_t, void *userptr);
62 
63 /*
64  * array of attributes to store.
65  *
66  * This array should be treated as opaque/private data.
67  * The SA_BULK_ADD_ATTR() macro should be used for manipulating
68  * the array.
69  *
70  * When sa_replace_all_by_template() is used the attributes
71  * will be stored in the order defined in the array, except that
72  * the attributes may be split between the bonus and the spill buffer
73  *
74  */
75 typedef struct sa_bulk_attr {
76 	void			*sa_data;
77 	sa_data_locator_t	*sa_data_func;
78 	uint16_t		sa_length;
79 	sa_attr_type_t		sa_attr;
80 	/* the following are private to the sa framework */
81 	void			*sa_addr;
82 	uint16_t		sa_buftype;
83 	uint16_t		sa_size;
84 } sa_bulk_attr_t;
85 
86 /*
87  * The on-disk format of sa_hdr_phys_t limits SA lengths to 16-bit values.
88  */
89 #define	SA_ATTR_MAX_LEN UINT16_MAX
90 
91 /*
92  * special macro for adding entries for bulk attr support
93  * bulk - sa_bulk_attr_t
94  * count - integer that will be incremented during each add
95  * attr - attribute to manipulate
96  * func - function for accessing data.
97  * data - pointer to data.
98  * len - length of data
99  */
100 
101 #define	SA_ADD_BULK_ATTR(b, idx, attr, func, data, len) \
102 { \
103 	ASSERT3U(len, <=, SA_ATTR_MAX_LEN); \
104 	b[idx].sa_attr = attr;\
105 	b[idx].sa_data_func = func; \
106 	b[idx].sa_data = data; \
107 	b[idx++].sa_length = len; \
108 }
109 
110 typedef struct sa_os sa_os_t;
111 
112 typedef enum sa_handle_type {
113 	SA_HDL_SHARED,
114 	SA_HDL_PRIVATE
115 } sa_handle_type_t;
116 
117 struct sa_handle;
118 typedef void *sa_lookup_tab_t;
119 typedef struct sa_handle sa_handle_t;
120 
121 typedef void (sa_update_cb_t)(sa_handle_t *, dmu_tx_t *tx);
122 
123 int sa_handle_get(objset_t *, uint64_t, void *userp,
124     sa_handle_type_t, sa_handle_t **);
125 int sa_handle_get_from_db(objset_t *, dmu_buf_t *, void *userp,
126     sa_handle_type_t, sa_handle_t **);
127 void sa_handle_destroy(sa_handle_t *);
128 int sa_buf_hold(objset_t *, uint64_t, const void *, dmu_buf_t **);
129 void sa_buf_rele(dmu_buf_t *, const void *);
130 int sa_lookup(sa_handle_t *, sa_attr_type_t, void *buf, uint32_t buflen);
131 int sa_update(sa_handle_t *, sa_attr_type_t, void *buf,
132     uint32_t buflen, dmu_tx_t *);
133 int sa_remove(sa_handle_t *, sa_attr_type_t, dmu_tx_t *);
134 int sa_bulk_lookup(sa_handle_t *, sa_bulk_attr_t *, int count);
135 int sa_bulk_lookup_locked(sa_handle_t *, sa_bulk_attr_t *, int count);
136 int sa_bulk_update(sa_handle_t *, sa_bulk_attr_t *, int count, dmu_tx_t *);
137 int sa_size(sa_handle_t *, sa_attr_type_t, int *);
138 void sa_object_info(sa_handle_t *, dmu_object_info_t *);
139 void sa_object_size(sa_handle_t *, uint32_t *, u_longlong_t *);
140 void *sa_get_userdata(sa_handle_t *);
141 void sa_set_userp(sa_handle_t *, void *);
142 dmu_buf_t *sa_get_db(sa_handle_t *);
143 uint64_t sa_handle_object(sa_handle_t *);
144 boolean_t sa_attr_would_spill(sa_handle_t *, sa_attr_type_t, int size);
145 void sa_spill_rele(sa_handle_t *);
146 void sa_register_update_callback(objset_t *, sa_update_cb_t *);
147 int sa_setup(objset_t *, uint64_t, const sa_attr_reg_t *, int,
148     sa_attr_type_t **);
149 void sa_tear_down(objset_t *);
150 int sa_replace_all_by_template(sa_handle_t *, sa_bulk_attr_t *,
151     int, dmu_tx_t *);
152 int sa_replace_all_by_template_locked(sa_handle_t *, sa_bulk_attr_t *,
153     int, dmu_tx_t *);
154 boolean_t sa_enabled(objset_t *);
155 void sa_cache_init(void);
156 void sa_cache_fini(void);
157 int sa_set_sa_object(objset_t *, uint64_t);
158 int sa_hdrsize(void *);
159 void sa_handle_lock(sa_handle_t *);
160 void sa_handle_unlock(sa_handle_t *);
161 
162 #ifdef _KERNEL
163 int sa_lookup_uio(sa_handle_t *, sa_attr_type_t, zfs_uio_t *);
164 int sa_add_projid(sa_handle_t *, dmu_tx_t *, uint64_t);
165 #endif
166 
167 #ifdef	__cplusplus
168 extern "C" {
169 #endif
170 
171 
172 #ifdef	__cplusplus
173 }
174 #endif
175 
176 #endif	/* _SYS_SA_H */
177