1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2012 by Delphix. All rights reserved.
27  */
28 
29 /*
30  * Common routines used by zfs and zpool property management.
31  */
32 
33 #include <sys/zio.h>
34 #include <sys/spa.h>
35 #include <sys/zfs_acl.h>
36 #include <sys/zfs_ioctl.h>
37 #include <sys/zfs_sysfs.h>
38 #include <sys/zfs_znode.h>
39 #include <sys/fs/zfs.h>
40 
41 #include "zfs_prop.h"
42 #include "zfs_deleg.h"
43 
44 #if !defined(_KERNEL)
45 #include <stdlib.h>
46 #include <string.h>
47 #include <ctype.h>
48 #include <sys/stat.h>
49 #endif
50 
51 static zprop_desc_t *
52 zprop_get_proptable(zfs_type_t type)
53 {
54 	if (type == ZFS_TYPE_POOL)
55 		return (zpool_prop_get_table());
56 	else if (type == ZFS_TYPE_VDEV)
57 		return (vdev_prop_get_table());
58 	else
59 		return (zfs_prop_get_table());
60 }
61 
62 static int
63 zprop_get_numprops(zfs_type_t type)
64 {
65 	if (type == ZFS_TYPE_POOL)
66 		return (ZPOOL_NUM_PROPS);
67 	else if (type == ZFS_TYPE_VDEV)
68 		return (VDEV_NUM_PROPS);
69 	else
70 		return (ZFS_NUM_PROPS);
71 }
72 
73 static boolean_t
74 zfs_mod_supported_prop(const char *name, zfs_type_t type)
75 {
76 /*
77  * The zfs module spa_feature_table[], whether in-kernel or in libzpool,
78  * always supports all the properties. libzfs needs to query the running
79  * module, via sysfs, to determine which properties are supported.
80  *
81  * The equivalent _can_ be done on FreeBSD by way of the sysctl
82  * tree, but this has not been done yet.
83  */
84 #if defined(_KERNEL) || defined(LIB_ZPOOL_BUILD) || defined(__FreeBSD__)
85 	return (B_TRUE);
86 #else
87 	return (zfs_mod_supported(type == ZFS_TYPE_POOL ?
88 	    ZFS_SYSFS_POOL_PROPERTIES : (type == ZFS_TYPE_VDEV ?
89 	    ZFS_SYSFS_VDEV_PROPERTIES : ZFS_SYSFS_DATASET_PROPERTIES), name));
90 #endif
91 }
92 
93 void
94 zprop_register_impl(int prop, const char *name, zprop_type_t type,
95     uint64_t numdefault, const char *strdefault, zprop_attr_t attr,
96     int objset_types, const char *values, const char *colname,
97     boolean_t rightalign, boolean_t visible, const zprop_index_t *idx_tbl)
98 {
99 	zprop_desc_t *prop_tbl = zprop_get_proptable(objset_types);
100 	zprop_desc_t *pd;
101 
102 	pd = &prop_tbl[prop];
103 
104 	ASSERT(pd->pd_name == NULL || pd->pd_name == name);
105 	ASSERT(name != NULL);
106 	ASSERT(colname != NULL);
107 
108 	pd->pd_name = name;
109 	pd->pd_propnum = prop;
110 	pd->pd_proptype = type;
111 	pd->pd_numdefault = numdefault;
112 	pd->pd_strdefault = strdefault;
113 	pd->pd_attr = attr;
114 	pd->pd_types = objset_types;
115 	pd->pd_values = values;
116 	pd->pd_colname = colname;
117 	pd->pd_rightalign = rightalign;
118 	pd->pd_visible = visible;
119 	pd->pd_zfs_mod_supported = zfs_mod_supported_prop(name, objset_types);
120 	pd->pd_table = idx_tbl;
121 	pd->pd_table_size = 0;
122 	while (idx_tbl && (idx_tbl++)->pi_name != NULL)
123 		pd->pd_table_size++;
124 }
125 
126 void
127 zprop_register_string(int prop, const char *name, const char *def,
128     zprop_attr_t attr, int objset_types, const char *values,
129     const char *colname)
130 {
131 	zprop_register_impl(prop, name, PROP_TYPE_STRING, 0, def, attr,
132 	    objset_types, values, colname, B_FALSE, B_TRUE, NULL);
133 
134 }
135 
136 void
137 zprop_register_number(int prop, const char *name, uint64_t def,
138     zprop_attr_t attr, int objset_types, const char *values,
139     const char *colname)
140 {
141 	zprop_register_impl(prop, name, PROP_TYPE_NUMBER, def, NULL, attr,
142 	    objset_types, values, colname, B_TRUE, B_TRUE, NULL);
143 }
144 
145 void
146 zprop_register_index(int prop, const char *name, uint64_t def,
147     zprop_attr_t attr, int objset_types, const char *values,
148     const char *colname, const zprop_index_t *idx_tbl)
149 {
150 	zprop_register_impl(prop, name, PROP_TYPE_INDEX, def, NULL, attr,
151 	    objset_types, values, colname, B_FALSE, B_TRUE, idx_tbl);
152 }
153 
154 void
155 zprop_register_hidden(int prop, const char *name, zprop_type_t type,
156     zprop_attr_t attr, int objset_types, const char *colname)
157 {
158 	zprop_register_impl(prop, name, type, 0, NULL, attr,
159 	    objset_types, NULL, colname,
160 	    type == PROP_TYPE_NUMBER, B_FALSE, NULL);
161 }
162 
163 
164 /*
165  * A comparison function we can use to order indexes into property tables.
166  */
167 static int
168 zprop_compare(const void *arg1, const void *arg2)
169 {
170 	const zprop_desc_t *p1 = *((zprop_desc_t **)arg1);
171 	const zprop_desc_t *p2 = *((zprop_desc_t **)arg2);
172 	boolean_t p1ro, p2ro;
173 
174 	p1ro = (p1->pd_attr == PROP_READONLY);
175 	p2ro = (p2->pd_attr == PROP_READONLY);
176 
177 	if (p1ro == p2ro)
178 		return (strcmp(p1->pd_name, p2->pd_name));
179 
180 	return (p1ro ? -1 : 1);
181 }
182 
183 /*
184  * Iterate over all properties in the given property table, calling back
185  * into the specified function for each property. We will continue to
186  * iterate until we either reach the end or the callback function returns
187  * something other than ZPROP_CONT.
188  */
189 int
190 zprop_iter_common(zprop_func func, void *cb, boolean_t show_all,
191     boolean_t ordered, zfs_type_t type)
192 {
193 	int i, num_props, size, prop;
194 	zprop_desc_t *prop_tbl;
195 	zprop_desc_t **order;
196 
197 	prop_tbl = zprop_get_proptable(type);
198 	num_props = zprop_get_numprops(type);
199 	size = num_props * sizeof (zprop_desc_t *);
200 
201 #if defined(_KERNEL)
202 	order = kmem_alloc(size, KM_SLEEP);
203 #else
204 	if ((order = malloc(size)) == NULL)
205 		return (ZPROP_CONT);
206 #endif
207 
208 	for (int j = 0; j < num_props; j++)
209 		order[j] = &prop_tbl[j];
210 
211 	if (ordered) {
212 		qsort((void *)order, num_props, sizeof (zprop_desc_t *),
213 		    zprop_compare);
214 	}
215 
216 	prop = ZPROP_CONT;
217 	for (i = 0; i < num_props; i++) {
218 		if ((order[i]->pd_visible || show_all) &&
219 		    order[i]->pd_zfs_mod_supported &&
220 		    (func(order[i]->pd_propnum, cb) != ZPROP_CONT)) {
221 			prop = order[i]->pd_propnum;
222 			break;
223 		}
224 	}
225 
226 #if defined(_KERNEL)
227 	kmem_free(order, size);
228 #else
229 	free(order);
230 #endif
231 	return (prop);
232 }
233 
234 static boolean_t
235 propname_match(const char *p, size_t len, zprop_desc_t *prop_entry)
236 {
237 	const char *propname = prop_entry->pd_name;
238 #ifndef _KERNEL
239 	const char *colname = prop_entry->pd_colname;
240 	int c;
241 #endif
242 
243 	ASSERT(propname != NULL);
244 
245 	if (len == strlen(propname) &&
246 	    strncmp(p, propname, len) == 0)
247 		return (B_TRUE);
248 
249 #ifndef _KERNEL
250 	if (colname == NULL || len != strlen(colname))
251 		return (B_FALSE);
252 
253 	for (c = 0; c < len; c++)
254 		if (p[c] != tolower(colname[c]))
255 			break;
256 
257 	return (colname[c] == '\0');
258 #else
259 	return (B_FALSE);
260 #endif
261 }
262 
263 typedef struct name_to_prop_cb {
264 	const char *propname;
265 	zprop_desc_t *prop_tbl;
266 } name_to_prop_cb_t;
267 
268 static int
269 zprop_name_to_prop_cb(int prop, void *cb_data)
270 {
271 	name_to_prop_cb_t *data = cb_data;
272 
273 	if (propname_match(data->propname, strlen(data->propname),
274 	    &data->prop_tbl[prop]))
275 		return (prop);
276 
277 	return (ZPROP_CONT);
278 }
279 
280 int
281 zprop_name_to_prop(const char *propname, zfs_type_t type)
282 {
283 	int prop;
284 	name_to_prop_cb_t cb_data;
285 
286 	cb_data.propname = propname;
287 	cb_data.prop_tbl = zprop_get_proptable(type);
288 
289 	prop = zprop_iter_common(zprop_name_to_prop_cb, &cb_data,
290 	    B_TRUE, B_FALSE, type);
291 
292 	return (prop == ZPROP_CONT ? ZPROP_INVAL : prop);
293 }
294 
295 int
296 zprop_string_to_index(int prop, const char *string, uint64_t *index,
297     zfs_type_t type)
298 {
299 	zprop_desc_t *prop_tbl;
300 	const zprop_index_t *idx_tbl;
301 	int i;
302 
303 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
304 		return (-1);
305 
306 	ASSERT(prop < zprop_get_numprops(type));
307 	prop_tbl = zprop_get_proptable(type);
308 	if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
309 		return (-1);
310 
311 	for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
312 		if (strcmp(string, idx_tbl[i].pi_name) == 0) {
313 			*index = idx_tbl[i].pi_value;
314 			return (0);
315 		}
316 	}
317 
318 	return (-1);
319 }
320 
321 int
322 zprop_index_to_string(int prop, uint64_t index, const char **string,
323     zfs_type_t type)
324 {
325 	zprop_desc_t *prop_tbl;
326 	const zprop_index_t *idx_tbl;
327 	int i;
328 
329 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
330 		return (-1);
331 
332 	ASSERT(prop < zprop_get_numprops(type));
333 	prop_tbl = zprop_get_proptable(type);
334 	if ((idx_tbl = prop_tbl[prop].pd_table) == NULL)
335 		return (-1);
336 
337 	for (i = 0; idx_tbl[i].pi_name != NULL; i++) {
338 		if (idx_tbl[i].pi_value == index) {
339 			*string = idx_tbl[i].pi_name;
340 			return (0);
341 		}
342 	}
343 
344 	return (-1);
345 }
346 
347 /*
348  * Return a random valid property value.  Used by ztest.
349  */
350 uint64_t
351 zprop_random_value(int prop, uint64_t seed, zfs_type_t type)
352 {
353 	zprop_desc_t *prop_tbl;
354 	const zprop_index_t *idx_tbl;
355 
356 	ASSERT((uint_t)prop < zprop_get_numprops(type));
357 	prop_tbl = zprop_get_proptable(type);
358 	idx_tbl = prop_tbl[prop].pd_table;
359 
360 	if (idx_tbl == NULL)
361 		return (seed);
362 
363 	return (idx_tbl[seed % prop_tbl[prop].pd_table_size].pi_value);
364 }
365 
366 const char *
367 zprop_values(int prop, zfs_type_t type)
368 {
369 	zprop_desc_t *prop_tbl;
370 
371 	ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
372 	ASSERT(prop < zprop_get_numprops(type));
373 
374 	prop_tbl = zprop_get_proptable(type);
375 
376 	return (prop_tbl[prop].pd_values);
377 }
378 
379 /*
380  * Returns TRUE if the property applies to any of the given dataset types.
381  *
382  * If headcheck is set, the check is being made against the head dataset
383  * type of a snapshot which requires to return B_TRUE when the property
384  * is only valid for snapshots.
385  */
386 boolean_t
387 zprop_valid_for_type(int prop, zfs_type_t type, boolean_t headcheck)
388 {
389 	zprop_desc_t *prop_tbl;
390 
391 	if (prop == ZPROP_INVAL || prop == ZPROP_CONT)
392 		return (B_FALSE);
393 
394 	ASSERT(prop < zprop_get_numprops(type));
395 	prop_tbl = zprop_get_proptable(type);
396 	if (headcheck && prop_tbl[prop].pd_types == ZFS_TYPE_SNAPSHOT)
397 		return (B_TRUE);
398 	return ((prop_tbl[prop].pd_types & type) != 0);
399 }
400 
401 /*
402  * For user property names, we allow all lowercase alphanumeric characters, plus
403  * a few useful punctuation characters.
404  */
405 int
406 zprop_valid_char(char c)
407 {
408 	return ((c >= 'a' && c <= 'z') ||
409 	    (c >= '0' && c <= '9') ||
410 	    c == '-' || c == '_' || c == '.' || c == ':');
411 }
412 
413 #ifndef _KERNEL
414 
415 /*
416  * Determines the minimum width for the column, and indicates whether it's fixed
417  * or not.  Only string columns are non-fixed.
418  */
419 size_t
420 zprop_width(int prop, boolean_t *fixed, zfs_type_t type)
421 {
422 	zprop_desc_t *prop_tbl, *pd;
423 	const zprop_index_t *idx;
424 	size_t ret;
425 	int i;
426 
427 	ASSERT(prop != ZPROP_INVAL && prop != ZPROP_CONT);
428 	ASSERT(prop < zprop_get_numprops(type));
429 
430 	prop_tbl = zprop_get_proptable(type);
431 	pd = &prop_tbl[prop];
432 
433 	*fixed = B_TRUE;
434 
435 	/*
436 	 * Start with the width of the column name.
437 	 */
438 	ret = strlen(pd->pd_colname);
439 
440 	/*
441 	 * For fixed-width values, make sure the width is large enough to hold
442 	 * any possible value.
443 	 */
444 	switch (pd->pd_proptype) {
445 	case PROP_TYPE_NUMBER:
446 		/*
447 		 * The maximum length of a human-readable number is 5 characters
448 		 * ("20.4M", for example).
449 		 */
450 		if (ret < 5)
451 			ret = 5;
452 		/*
453 		 * 'creation' is handled specially because it's a number
454 		 * internally, but displayed as a date string.
455 		 */
456 		if (prop == ZFS_PROP_CREATION)
457 			*fixed = B_FALSE;
458 		/*
459 		 * 'health' is handled specially because it's a number
460 		 * internally, but displayed as a fixed 8 character string.
461 		 */
462 		if (prop == ZPOOL_PROP_HEALTH)
463 			ret = 8;
464 		break;
465 	case PROP_TYPE_INDEX:
466 		idx = prop_tbl[prop].pd_table;
467 		for (i = 0; idx[i].pi_name != NULL; i++) {
468 			if (strlen(idx[i].pi_name) > ret)
469 				ret = strlen(idx[i].pi_name);
470 		}
471 		break;
472 
473 	case PROP_TYPE_STRING:
474 		*fixed = B_FALSE;
475 		break;
476 	}
477 
478 	return (ret);
479 }
480 
481 #endif
482 
483 #if defined(_KERNEL)
484 /* Common routines to initialize property tables */
485 EXPORT_SYMBOL(zprop_register_impl);
486 EXPORT_SYMBOL(zprop_register_string);
487 EXPORT_SYMBOL(zprop_register_number);
488 EXPORT_SYMBOL(zprop_register_index);
489 EXPORT_SYMBOL(zprop_register_hidden);
490 
491 /* Common routines for zfs and zpool property management */
492 EXPORT_SYMBOL(zprop_iter_common);
493 EXPORT_SYMBOL(zprop_name_to_prop);
494 EXPORT_SYMBOL(zprop_string_to_index);
495 EXPORT_SYMBOL(zprop_index_to_string);
496 EXPORT_SYMBOL(zprop_random_value);
497 EXPORT_SYMBOL(zprop_values);
498 EXPORT_SYMBOL(zprop_valid_for_type);
499 EXPORT_SYMBOL(zprop_valid_char);
500 #endif
501