1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2018 by Delphix. All rights reserved.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/zfeature.h>
#include <sys/zfs_ioctl.h>
#include <sys/zfs_sysfs.h>
#include <sys/kmem.h>
#include <sys/fs/zfs.h>
#include <linux/kobject.h>
#include "zfs_prop.h"
#if !defined(_KERNEL)
#error kernel builds only
#endif
/*
* ZFS Module sysfs support
*
* This extends our sysfs '/sys/module/zfs' entry to include feature
* and property attributes. The primary consumer of this information
* is user processes, like the zfs CLI, that need to know what the
* current loaded ZFS module supports. The libzfs binary will consult
* this information when instantiating the zfs|zpool property tables
* and the pool features table.
*
* The added top-level directories are:
* /sys/module/zfs
* ├── features.kernel
* ├── features.pool
* ├── properties.dataset
* └── properties.pool
*
* The local interface for the zfs kobjects includes:
* zfs_kobj_init()
* zfs_kobj_add()
* zfs_kobj_release()
* zfs_kobj_add_attr()
* zfs_kobj_fini()
*/
/*
* A zfs_mod_kobj_t represents a zfs kobject under '/sys/module/zfs'
*/
struct zfs_mod_kobj;
typedef struct zfs_mod_kobj zfs_mod_kobj_t;
struct zfs_mod_kobj {
struct kobject zko_kobj;
struct kobj_type zko_kobj_type;
struct sysfs_ops zko_sysfs_ops;
size_t zko_attr_count;
struct attribute *zko_attr_list; /* allocated */
struct attribute **zko_default_attrs; /* allocated */
size_t zko_child_count;
zfs_mod_kobj_t *zko_children; /* allocated */
};
#define ATTR_TABLE_SIZE(cnt) (sizeof (struct attribute) * (cnt))
/* Note +1 for NULL terminator slot */
#define DEFAULT_ATTR_SIZE(cnt) (sizeof (struct attribute *) * (cnt + 1))
#define CHILD_TABLE_SIZE(cnt) (sizeof (zfs_mod_kobj_t) * (cnt))
/*
* These are the top-level kobjects under '/sys/module/zfs/'
*/
static zfs_mod_kobj_t kernel_features_kobj;
static zfs_mod_kobj_t pool_features_kobj;
static zfs_mod_kobj_t dataset_props_kobj;
static zfs_mod_kobj_t pool_props_kobj;
/*
* The show function is used to provide the content
* of an attribute into a PAGE_SIZE buffer.
*/
typedef ssize_t (*sysfs_show_func)(struct kobject *, struct attribute *,
char *);
static void
zfs_kobj_fini(zfs_mod_kobj_t *zkobj)
{
/* finialize any child kobjects */
if (zkobj->zko_child_count != 0) {
ASSERT(zkobj->zko_children);
for (int i = 0; i < zkobj->zko_child_count; i++)
zfs_kobj_fini(&zkobj->zko_children[i]);
}
/* kobject_put() will call zfs_kobj_release() to release memory */
kobject_del(&zkobj->zko_kobj);
kobject_put(&zkobj->zko_kobj);
}
static void
zfs_kobj_release(struct kobject *kobj)
{
zfs_mod_kobj_t *zkobj = container_of(kobj, zfs_mod_kobj_t, zko_kobj);
if (zkobj->zko_attr_count != 0) {
ASSERT(zkobj->zko_attr_list);
ASSERT(zkobj->zko_default_attrs);
kmem_free(zkobj->zko_attr_list,
ATTR_TABLE_SIZE(zkobj->zko_attr_count));
kmem_free(zkobj->zko_default_attrs,
DEFAULT_ATTR_SIZE(zkobj->zko_attr_count));
zkobj->zko_attr_count = 0;
zkobj->zko_attr_list = NULL;
zkobj->zko_default_attrs = NULL;
}
if (zkobj->zko_child_count != 0) {
ASSERT(zkobj->zko_children);
kmem_free(zkobj->zko_children,
CHILD_TABLE_SIZE(zkobj->zko_child_count));
zkobj->zko_child_count = 0;
zkobj->zko_children = NULL;
}
}
static void
zfs_kobj_add_attr(zfs_mod_kobj_t *zkobj, int attr_num, const char *attr_name)
{
VERIFY3U(attr_num, <, zkobj->zko_attr_count);
ASSERT(zkobj->zko_attr_list);
ASSERT(zkobj->zko_default_attrs);
zkobj->zko_attr_list[attr_num].name = attr_name;
zkobj->zko_attr_list[attr_num].mode = 0444;
zkobj->zko_default_attrs[attr_num] = &zkobj->zko_attr_list[attr_num];
}
static int
zfs_kobj_init(zfs_mod_kobj_t *zkobj, int attr_cnt, int child_cnt,
sysfs_show_func show_func)
{
/*
* Initialize object's attributes. Count can be zero.
*/
if (attr_cnt > 0) {
zkobj->zko_attr_list = kmem_zalloc(ATTR_TABLE_SIZE(attr_cnt),
KM_SLEEP);
if (zkobj->zko_attr_list == NULL)
return (ENOMEM);
}
/* this will always have at least one slot for NULL termination */
zkobj->zko_default_attrs = kmem_zalloc(DEFAULT_ATTR_SIZE(attr_cnt),
KM_SLEEP);
if (zkobj->zko_default_attrs == NULL) {
if (zkobj->zko_attr_list) {
kmem_free(zkobj->zko_attr_list,
ATTR_TABLE_SIZE(attr_cnt));
}
return (ENOMEM);
}
zkobj->zko_attr_count = attr_cnt;
zkobj->zko_kobj_type.default_attrs = zkobj->zko_default_attrs;
if (child_cnt > 0) {
zkobj->zko_children = kmem_zalloc(CHILD_TABLE_SIZE(child_cnt),
KM_SLEEP);
if (zkobj->zko_children == NULL) {
if (attr_cnt > 0) {
kmem_free(zkobj->zko_attr_list,
DEFAULT_ATTR_SIZE(attr_cnt));
kmem_free(zkobj->zko_attr_list,
ATTR_TABLE_SIZE(attr_cnt));
}
return (ENOMEM);
}
zkobj->zko_child_count = child_cnt;
}
zkobj->zko_sysfs_ops.show = show_func;
zkobj->zko_kobj_type.sysfs_ops = &zkobj->zko_sysfs_ops;
zkobj->zko_kobj_type.release = zfs_kobj_release;
return (0);
}
static int
zfs_kobj_add(zfs_mod_kobj_t *zkobj, struct kobject *parent, const char *name)
{
/* zko_default_attrs must be NULL terminated */
ASSERT(zkobj->zko_default_attrs != NULL);
ASSERT(zkobj->zko_default_attrs[zkobj->zko_attr_count] == NULL);
kobject_init(&zkobj->zko_kobj, &zkobj->zko_kobj_type);
return (kobject_add(&zkobj->zko_kobj, parent, name));
}
/*
* Each zfs property has these common attributes
*/
static const char *zprop_attrs[] = {
"type",
"readonly",
"setonce",
"visible",
"values",
"default",
"datasets" /* zfs properties only */
};
#define ZFS_PROP_ATTR_COUNT ARRAY_SIZE(zprop_attrs)
#define ZPOOL_PROP_ATTR_COUNT (ZFS_PROP_ATTR_COUNT - 1)
static const char *zprop_types[] = {
"number",
"string",
"index",
};
typedef struct zfs_type_map {
zfs_type_t ztm_type;
const char *ztm_name;
} zfs_type_map_t;
static zfs_type_map_t type_map[] = {
{ZFS_TYPE_FILESYSTEM, "filesystem"},
{ZFS_TYPE_SNAPSHOT, "snapshot"},
{ZFS_TYPE_VOLUME, "volume"},
{ZFS_TYPE_BOOKMARK, "bookmark"}
};
/*
* Show the content for a zfs property attribute
*/
static ssize_t
zprop_sysfs_show(const char *attr_name, const zprop_desc_t *property,
char *buf, size_t buflen)
{
const char *show_str;
/* For dataset properties list the dataset types that apply */
if (strcmp(attr_name, "datasets") == 0 &&
property->pd_types != ZFS_TYPE_POOL) {
int len = 0;
for (int i = 0; i < ARRAY_SIZE(type_map); i++) {
if (type_map[i].ztm_type & property->pd_types) {
len += snprintf(buf + len, buflen - len, "%s ",
type_map[i].ztm_name);
}
}
len += snprintf(buf + len, buflen - len, "\n");
return (len);
}
if (strcmp(attr_name, "type") == 0) {
show_str = zprop_types[property->pd_proptype];
} else if (strcmp(attr_name, "readonly") == 0) {
show_str = property->pd_attr == PROP_READONLY ? "1" : "0";
} else if (strcmp(attr_name, "setonce") == 0) {
show_str = property->pd_attr == PROP_ONETIME ? "1" : "0";
} else if (strcmp(attr_name, "visible") == 0) {
show_str = property->pd_visible ? "1" : "0";
} else if (strcmp(attr_name, "values") == 0) {
show_str = property->pd_values ? property->pd_values : "";
} else if (strcmp(attr_name, "default") == 0) {
char number[32];
switch (property->pd_proptype) {
case PROP_TYPE_NUMBER:
(void) snprintf(number, sizeof (number), "%llu",
(u_longlong_t)property->pd_numdefault);
show_str = number;
break;
case PROP_TYPE_STRING:
show_str = property->pd_strdefault ?
property->pd_strdefault : "";
break;
case PROP_TYPE_INDEX:
(void) zprop_index_to_string(property->pd_propnum,
property->pd_numdefault, &show_str,
property->pd_types);
break;
default:
return (0);
}
} else {
return (0);
}
return (snprintf(buf, buflen, "%s\n", show_str));
}
static ssize_t
dataset_property_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
zfs_prop_t prop = zfs_name_to_prop(kobject_name(kobj));
zprop_desc_t *prop_tbl = zfs_prop_get_table();
ssize_t len;
ASSERT3U(prop, <, ZFS_NUM_PROPS);
len = zprop_sysfs_show(attr->name, &prop_tbl[prop], buf, PAGE_SIZE);
return (len);
}
static ssize_t
pool_property_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
zpool_prop_t prop = zpool_name_to_prop(kobject_name(kobj));
zprop_desc_t *prop_tbl = zpool_prop_get_table();
ssize_t len;
ASSERT3U(prop, <, ZPOOL_NUM_PROPS);
len = zprop_sysfs_show(attr->name, &prop_tbl[prop], buf, PAGE_SIZE);
return (len);
}
/*
* ZFS kernel feature attributes for '/sys/module/zfs/features.kernel'
*
* This list is intended for kernel features that don't have a pool feature
* association or that extend existing user kernel interfaces.
*
* A user processes can easily check if the running zfs kernel module
* supports the new feature.
*
* For example, the initial channel_program feature was extended to support
* async calls (i.e. a sync flag). If this mechanism were in place at that
* time, we could have added a 'channel_program_async' to this list.
*/
static const char *zfs_features[] = {
/* --> Add new kernel features here (post ZoL 0.8.0) */
};
#define ZFS_FEATURE_COUNT ARRAY_SIZE(zfs_features)
static ssize_t
kernel_feature_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
return (snprintf(buf, PAGE_SIZE, "supported\n"));
}
static int
zfs_kernel_features_init(zfs_mod_kobj_t *zfs_kobj, struct kobject *parent)
{
int err;
err = zfs_kobj_init(zfs_kobj, ZFS_FEATURE_COUNT, 0,
kernel_feature_show);
if (err)
return (err);
for (int f = 0; f < ZFS_FEATURE_COUNT; f++)
zfs_kobj_add_attr(zfs_kobj, f, zfs_features[f]);
err = zfs_kobj_add(zfs_kobj, parent, ZFS_SYSFS_KERNEL_FEATURES);
if (err)
zfs_kobj_release(&zfs_kobj->zko_kobj);
return (err);
}
/*
* Each pool feature has these common attributes
*/
static const char *pool_feature_attrs[] = {
"description",
"guid",
"uname",
"readonly_compatible",
"required_for_mos",
"activate_on_enable",
"per_dataset"
};
#define ZPOOL_FEATURE_ATTR_COUNT ARRAY_SIZE(pool_feature_attrs)
/*
* Show the content for the given zfs pool feature attribute
*/
static ssize_t
pool_feature_show(struct kobject *kobj, struct attribute *attr, char *buf)
{
spa_feature_t fid;
if (zfeature_lookup_guid(kobject_name(kobj), &fid) != 0)
return (0);
ASSERT3U(fid, <, SPA_FEATURES);
zfeature_flags_t flags = spa_feature_table[fid].fi_flags;
const char *show_str = NULL;
if (strcmp(attr->name, "description") == 0) {
show_str = spa_feature_table[fid].fi_desc;
} else if (strcmp(attr->name, "guid") == 0) {
show_str = spa_feature_table[fid].fi_guid;
} else if (strcmp(attr->name, "uname") == 0) {
show_str = spa_feature_table[fid].fi_uname;
} else if (strcmp(attr->name, "readonly_compatible") == 0) {
show_str = flags & ZFEATURE_FLAG_READONLY_COMPAT ? "1" : "0";
} else if (strcmp(attr->name, "required_for_mos") == 0) {
show_str = flags & ZFEATURE_FLAG_MOS ? "1" : "0";
} else if (strcmp(attr->name, "activate_on_enable") == 0) {
show_str = flags & ZFEATURE_FLAG_ACTIVATE_ON_ENABLE ? "1" : "0";
} else if (strcmp(attr->name, "per_dataset") == 0) {
show_str = flags & ZFEATURE_FLAG_PER_DATASET ? "1" : "0";
}
if (show_str == NULL)
return (0);
return (snprintf(buf, PAGE_SIZE, "%s\n", show_str));
}
static void
pool_feature_to_kobj(zfs_mod_kobj_t *parent, spa_feature_t fid,
const char *name)
{
zfs_mod_kobj_t *zfs_kobj = &parent->zko_children[fid];
ASSERT3U(fid, <, SPA_FEATURES);
ASSERT(name);
int err = zfs_kobj_init(zfs_kobj, ZPOOL_FEATURE_ATTR_COUNT, 0,
pool_feature_show);
if (err)
return;
for (int i = 0; i < ZPOOL_FEATURE_ATTR_COUNT; i++)
zfs_kobj_add_attr(zfs_kobj, i, pool_feature_attrs[i]);
err = zfs_kobj_add(zfs_kobj, &parent->zko_kobj, name);
if (err)
zfs_kobj_release(&zfs_kobj->zko_kobj);
}
static int
zfs_pool_features_init(zfs_mod_kobj_t *zfs_kobj, struct kobject *parent)
{
/*
* Create a parent kobject to host pool features.
*
* '/sys/module/zfs/features.pool'
*/
int err = zfs_kobj_init(zfs_kobj, 0, SPA_FEATURES, pool_feature_show);
if (err)
return (err);
err = zfs_kobj_add(zfs_kobj, parent, ZFS_SYSFS_POOL_FEATURES);
if (err) {
zfs_kobj_release(&zfs_kobj->zko_kobj);
return (err);
}
/*
* Now create a kobject for each feature.
*
* '/sys/module/zfs/features.pool/<feature>'
*/
for (spa_feature_t i = 0; i < SPA_FEATURES; i++)
pool_feature_to_kobj(zfs_kobj, i, spa_feature_table[i].fi_guid);
return (0);
}
typedef struct prop_to_kobj_arg {
zprop_desc_t *p2k_table;
zfs_mod_kobj_t *p2k_parent;
sysfs_show_func p2k_show_func;
int p2k_attr_count;
} prop_to_kobj_arg_t;
static int
zprop_to_kobj(int prop, void *args)
{
prop_to_kobj_arg_t *data = args;
zfs_mod_kobj_t *parent = data->p2k_parent;
zfs_mod_kobj_t *zfs_kobj = &parent->zko_children[prop];
const char *name = data->p2k_table[prop].pd_name;
int err;
ASSERT(name);
err = zfs_kobj_init(zfs_kobj, data->p2k_attr_count, 0,
data->p2k_show_func);
if (err)
return (ZPROP_CONT);
for (int i = 0; i < data->p2k_attr_count; i++)
zfs_kobj_add_attr(zfs_kobj, i, zprop_attrs[i]);
err = zfs_kobj_add(zfs_kobj, &parent->zko_kobj, name);
if (err)
zfs_kobj_release(&zfs_kobj->zko_kobj);
return (ZPROP_CONT);
}
static int
zfs_sysfs_properties_init(zfs_mod_kobj_t *zfs_kobj, struct kobject *parent,
zfs_type_t type)
{
prop_to_kobj_arg_t context;
const char *name;
int err;
/*
* Create a parent kobject to host properties.
*
* '/sys/module/zfs/properties.<type>'
*/
if (type == ZFS_TYPE_POOL) {
name = ZFS_SYSFS_POOL_PROPERTIES;
context.p2k_table = zpool_prop_get_table();
context.p2k_attr_count = ZPOOL_PROP_ATTR_COUNT;
context.p2k_parent = zfs_kobj;
context.p2k_show_func = pool_property_show;
err = zfs_kobj_init(zfs_kobj, 0, ZPOOL_NUM_PROPS,
pool_property_show);
} else {
name = ZFS_SYSFS_DATASET_PROPERTIES;
context.p2k_table = zfs_prop_get_table();
context.p2k_attr_count = ZFS_PROP_ATTR_COUNT;
context.p2k_parent = zfs_kobj;
context.p2k_show_func = dataset_property_show;
err = zfs_kobj_init(zfs_kobj, 0, ZFS_NUM_PROPS,
dataset_property_show);
}
if (err)
return (err);
err = zfs_kobj_add(zfs_kobj, parent, name);
if (err) {
zfs_kobj_release(&zfs_kobj->zko_kobj);
return (err);
}
/*
* Create a kobject for each property.
*
* '/sys/module/zfs/properties.<type>/<property>'
*/
(void) zprop_iter_common(zprop_to_kobj, &context, B_TRUE,
B_FALSE, type);
return (err);
}
void
zfs_sysfs_init(void)
{
struct kobject *parent =
&(((struct module *)(THIS_MODULE))->mkobj).kobj;
int err;
ASSERT(parent != NULL);
err = zfs_kernel_features_init(&kernel_features_kobj, parent);
if (err)
return;
err = zfs_pool_features_init(&pool_features_kobj, parent);
if (err) {
zfs_kobj_fini(&kernel_features_kobj);
return;
}
err = zfs_sysfs_properties_init(&pool_props_kobj, parent,
ZFS_TYPE_POOL);
if (err) {
zfs_kobj_fini(&kernel_features_kobj);
zfs_kobj_fini(&pool_features_kobj);
return;
}
err = zfs_sysfs_properties_init(&dataset_props_kobj, parent,
ZFS_TYPE_FILESYSTEM);
if (err) {
zfs_kobj_fini(&kernel_features_kobj);
zfs_kobj_fini(&pool_features_kobj);
zfs_kobj_fini(&pool_props_kobj);
return;
}
}
void
zfs_sysfs_fini(void)
{
/*
* Remove top-level kobjects; each will remove any children kobjects
*/
zfs_kobj_fini(&kernel_features_kobj);
zfs_kobj_fini(&pool_features_kobj);
zfs_kobj_fini(&dataset_props_kobj);
zfs_kobj_fini(&pool_props_kobj);
}
|