summaryrefslogtreecommitdiffstats
path: root/usr.sbin/bsnmpd/modules/snmp_hostres/hostres_partition_tbl.c
blob: 65c0012f816d5c615a07ba0efcd9fd4dc5a1f8f0 (plain)
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
618
619
620
621
622
623
624
625
626
627
628
629
630
/*-
 * Copyright (c) 2005-2006 The FreeBSD Project
 * All rights reserved.
 *
 * Author: Victor Cruceru <soc-victor@freebsd.org>
 *
 * Redistribution of this software and documentation and use in source and
 * binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code or documentation must retain the above
 *    copyright notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD$
 */

/*
 * Host Resources MIB: hrPartitionTable implementation for SNMPd.
 */

#include <sys/types.h>
#include <sys/limits.h>

#include <assert.h>
#include <err.h>
#include <inttypes.h>
#include <libgeom.h>
#include <paths.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <sysexits.h>

#include "hostres_snmp.h"
#include "hostres_oid.h"
#include "hostres_tree.h"

#ifdef PC98
#define	HR_FREEBSD_PART_TYPE	0xc494
#else
#define	HR_FREEBSD_PART_TYPE	165
#endif

/* Maximum length for label and id including \0 */
#define	PART_STR_MLEN	(128 + 1)

/*
 * One row in the hrPartitionTable
 */
struct partition_entry {
	asn_subid_t	index[2];
	u_char		*label;	/* max allocated len will be PART_STR_MLEN */
	u_char		*id;	/* max allocated len will be PART_STR_MLEN */
	int32_t		size;
	int32_t		fs_Index;
	TAILQ_ENTRY(partition_entry) link;
#define	HR_PARTITION_FOUND		0x001
	uint32_t	flags;
};
TAILQ_HEAD(partition_tbl, partition_entry);

/*
 * This table is used to get a consistent indexing. It saves the name -> index
 * mapping while we rebuild the partition table.
 */
struct partition_map_entry {
	int32_t		index;	/* partition_entry::index */
	u_char		*id;	/* max allocated len will be PART_STR_MLEN */

	/*
	 * next may be NULL if the respective partition_entry
	 * is (temporally) gone.
	 */
	struct partition_entry	*entry;
	STAILQ_ENTRY(partition_map_entry) link;
};
STAILQ_HEAD(partition_map, partition_map_entry);

/* Mapping table for consistent indexing */
static struct partition_map partition_map =
    STAILQ_HEAD_INITIALIZER(partition_map);

/* THE partition table. */
static struct partition_tbl partition_tbl =
    TAILQ_HEAD_INITIALIZER(partition_tbl);

/* next int available for indexing the hrPartitionTable */
static uint32_t next_partition_index = 1;

/*
 * Partition_entry_cmp is used for INSERT_OBJECT_FUNC_LINK
 * macro.
 */
static int
partition_entry_cmp(const struct partition_entry *a,
    const struct partition_entry *b)
{
	assert(a != NULL);
	assert(b != NULL);

	if (a->index[0] < b->index[0])
		return (-1);

	if (a->index[0] > b->index[0])
		return (+1);

	if (a->index[1] < b->index[1])
		return (-1);

	if (a->index[1] > b->index[1])
		return (+1);

	return (0);
}

/*
 * Partition_idx_cmp is used for NEXT_OBJECT_FUNC and FIND_OBJECT_FUNC
 * macros
 */
static int
partition_idx_cmp(const struct asn_oid *oid, u_int sub,
    const struct partition_entry *entry)
{
	u_int i;

	for (i = 0; i < 2 && i < oid->len - sub; i++) {
		if (oid->subs[sub + i] < entry->index[i])
			return (-1);
		if (oid->subs[sub + i] > entry->index[i])
			return (+1);
	}
	if (oid->len - sub < 2)
		return (-1);
	if (oid->len - sub > 2)
		return (+1);

	return (0);
}

/**
 * Create a new partition table entry
 */
static struct partition_entry *
partition_entry_create(int32_t ds_index, const char *chunk_name)
{
	struct partition_entry *entry;
	struct partition_map_entry *map;
	size_t id_len;

	/* sanity checks */
	assert(chunk_name != NULL);
	if (chunk_name == NULL || chunk_name[0] == '\0')
		return (NULL);

	/* check whether we already have seen this partition */
	STAILQ_FOREACH(map, &partition_map, link)
		if (strcmp(map->id, chunk_name) == 0)
			break;

	if (map == NULL) {
		/* new object - get a new index and create a map */

		if (next_partition_index > INT_MAX) {
			/* Unrecoverable error - die clean and quicly*/
		        syslog(LOG_ERR, "%s: hrPartitionTable index wrap",
			    __func__);
			errx(EX_SOFTWARE, "hrPartitionTable index wrap");
		}

		if ((map = malloc(sizeof(*map))) == NULL) {
			syslog(LOG_ERR, "hrPartitionTable: %s: %m", __func__);
			return (NULL);
		}

		id_len = strlen(chunk_name) + 1;
		if (id_len > PART_STR_MLEN)
			id_len = PART_STR_MLEN;

		if ((map->id = malloc(id_len)) == NULL) {
			free(map);
			return (NULL);
		}

		map->index = next_partition_index++;

		strlcpy(map->id, chunk_name, id_len);

		map->entry = NULL;

		STAILQ_INSERT_TAIL(&partition_map, map, link);

		HRDBG("%s added into hrPartitionMap at index=%d",
		    chunk_name, map->index);

	} else {
		HRDBG("%s exists in hrPartitionMap index=%d",
		    chunk_name, map->index);
	}

	if ((entry = malloc(sizeof(*entry))) == NULL) {
		syslog(LOG_WARNING, "hrPartitionTable: %s: %m", __func__);
		return (NULL);
	}
	memset(entry, 0, sizeof(*entry));

	/* create the index */
	entry->index[0] = ds_index;
	entry->index[1] = map->index;

	map->entry = entry;

	if ((entry->id = strdup(map->id)) == NULL) {
		free(entry);
		return (NULL);
	}

	/*
	 * reuse id_len from here till the end of this function
	 * for partition_entry::label
	 */
	id_len = strlen(_PATH_DEV) + strlen(chunk_name) + 1;

	if (id_len > PART_STR_MLEN)
		id_len = PART_STR_MLEN;

	if ((entry->label = malloc(id_len )) == NULL) {
		free(entry->id);
		free(entry);
		return (NULL);
	}

	snprintf(entry->label, id_len, "%s%s", _PATH_DEV, chunk_name);

	INSERT_OBJECT_FUNC_LINK(entry, &partition_tbl, link,
	    partition_entry_cmp);

	return (entry);
}

/**
 * Delete a partition table entry but keep the map entry intact.
 */
static void
partition_entry_delete(struct partition_entry *entry)
{
	struct partition_map_entry *map;

	assert(entry != NULL);

	TAILQ_REMOVE(&partition_tbl, entry, link);
	STAILQ_FOREACH(map, &partition_map, link)
		if (map->entry == entry) {
			map->entry = NULL;
			break;
		}
	free(entry->id);
	free(entry->label);
	free(entry);
}

/**
 * Find a partition table entry by name. If none is found, return NULL.
 */
static struct partition_entry *
partition_entry_find_by_name(const char *name)
{
	struct partition_entry *entry =  NULL;

	TAILQ_FOREACH(entry, &partition_tbl, link)
		if (strcmp(entry->id, name) == 0)
			return (entry);

	return (NULL);
}

/**
 * Find a partition table entry by label. If none is found, return NULL.
 */
static struct partition_entry *
partition_entry_find_by_label(const char *name)
{
	struct partition_entry *entry =  NULL;

	TAILQ_FOREACH(entry, &partition_tbl, link)
		if (strcmp(entry->label, name) == 0)
			return (entry);

	return (NULL);
}

/**
 * Process a chunk from libgeom(4). A chunk is either a slice or a partition.
 * If necessary create a new partition table entry for it. In any case
 * set the size field of the entry and set the FOUND flag.
 */
static void
handle_chunk(int32_t ds_index, const char *chunk_name, off_t chunk_size)
{
	struct partition_entry *entry;
	daddr_t k_size;

	assert(chunk_name != NULL);
	assert(chunk_name[0] != '\0');
	if (chunk_name == NULL || chunk_name == '\0')
		return;

	HRDBG("ANALYZE chunk %s", chunk_name);

	if ((entry = partition_entry_find_by_name(chunk_name)) == NULL)
		if ((entry = partition_entry_create(ds_index,
		    chunk_name)) == NULL)
			return;

	entry->flags |= HR_PARTITION_FOUND;

	/* actual size may overflow the SNMP type */
	k_size = chunk_size / 1024;
	entry->size = (k_size > (off_t)INT_MAX ? INT_MAX : k_size);
}

/**
 * Start refreshing the partition table. A call to this function will
 * be followed by a call to handleDiskStorage() for every disk, followed
 * by a single call to the post_refresh function.
 */
void
partition_tbl_pre_refresh(void)
{
	struct partition_entry *entry;

	/* mark each entry as missing */
	TAILQ_FOREACH(entry, &partition_tbl, link)
		entry->flags &= ~HR_PARTITION_FOUND;
}

/**
 * Try to find a geom(4) class by its name. Returns a pointer to that
 * class if found NULL otherways.
 */
static struct gclass *
find_class(struct gmesh *mesh, const char *name)
{
	struct gclass *classp;

	LIST_FOREACH(classp, &mesh->lg_class, lg_class)
		if (strcmp(classp->lg_name, name) == 0)
			return (classp);
	return (NULL);
}

/**
 * Process all MBR-type partitions from the given disk.
 */
static void
get_mbr(struct gclass *classp, int32_t ds_index, const char *disk_dev_name)
{
	struct ggeom *gp;
	struct gprovider *pp;
	struct gconfig *conf;
	long part_type;

	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
		/* We are only interested in partitions from this disk */
		if (strcmp(gp->lg_name, disk_dev_name) != 0)
			continue;

		/*
		 * Find all the non-BSD providers (these are handled in get_bsd)
		 */
		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
			LIST_FOREACH(conf, &pp->lg_config, lg_config) {
				if (conf->lg_name == NULL ||
				    conf->lg_val == NULL ||
				    strcmp(conf->lg_name, "type") != 0)
					continue;

				/*
				 * We are not interested in BSD partitions
				 * (ie ad0s1 is not interesting at this point).
				 * We'll take care of them in detail (slice
				 * by slice) in get_bsd.
				 */
				part_type = strtol(conf->lg_val, NULL, 10);
				if (part_type == HR_FREEBSD_PART_TYPE)
					break;
				HRDBG("-> MBR PROVIDER Name: %s", pp->lg_name);
				HRDBG("Mediasize: %jd",
				    (intmax_t)pp->lg_mediasize / 1024);
				HRDBG("Sectorsize: %u", pp->lg_sectorsize);
				HRDBG("Mode: %s", pp->lg_mode);
				HRDBG("CONFIG: %s: %s",
				    conf->lg_name, conf->lg_val);

				handle_chunk(ds_index, pp->lg_name,
				    pp->lg_mediasize);
			}
		}
	}
}

/**
 * Process all BSD-type partitions from the given disk.
 */
static void
get_bsd_sun(struct gclass *classp, int32_t ds_index, const char *disk_dev_name)
{
	struct ggeom *gp;
	struct gprovider *pp;

	LIST_FOREACH(gp, &classp->lg_geom, lg_geom) {
		/*
		 * We are only interested in those geoms starting with
		 * the disk_dev_name passed as parameter to this function.
		 */
		if (strncmp(gp->lg_name, disk_dev_name,
		    strlen(disk_dev_name)) != 0)
			continue;

		LIST_FOREACH(pp, &gp->lg_provider, lg_provider) {
			if (pp->lg_name == NULL)
				continue;
			handle_chunk(ds_index, pp->lg_name, pp->lg_mediasize);
		}
	}
}

/**
 * Called from the DiskStorage table for every row. Open the GEOM(4) framework
 * and process all the partitions in it.
 * ds_index is the index into the DiskStorage table.
 * This is done in two steps: for non BSD partitions the geom class "MBR" is
 * used, for our BSD slices the "BSD" geom class.
 */
void
partition_tbl_handle_disk(int32_t ds_index, const char *disk_dev_name)
{
	struct gmesh mesh;	/* GEOM userland tree */
	struct gclass *classp;
	int error;

	assert(disk_dev_name != NULL);
	assert(ds_index > 0);

	HRDBG("===> getting partitions for %s <===", disk_dev_name);

	/* try to construct the GEOM tree */
	if ((error = geom_gettree(&mesh)) != 0) {
		syslog(LOG_WARNING, "cannot get GEOM tree: %m");
		return;
	}

	/*
	 * First try the GEOM "MBR" class.
	 * This is needed for non-BSD slices (aka partitions)
	 * on PC architectures.
	 */
	if ((classp = find_class(&mesh, "MBR")) != NULL) {
		get_mbr(classp, ds_index, disk_dev_name);
	} else {
		HRDBG("cannot find \"MBR\" geom class");
	}

	/*
	 * Get the "BSD" GEOM class.
	 * Here we'll find all the info needed about the BSD slices.
	 */
	if ((classp = find_class(&mesh, "BSD")) != NULL) {
		get_bsd_sun(classp, ds_index, disk_dev_name);
	} else {
		/* no problem on sparc64 */
		HRDBG("cannot find \"BSD\" geom class");
	}

	/*
	 * Get the "SUN" GEOM class.
	 * Here we'll find all the info needed about the BSD slices.
	 */
	if ((classp = find_class(&mesh, "SUN")) != NULL) {
		get_bsd_sun(classp, ds_index, disk_dev_name);
	} else {
		/* no problem on i386 */
		HRDBG("cannot find \"SUN\" geom class");
	}

	geom_deletetree(&mesh);
}

/**
 * Finish refreshing the table.
 */
void
partition_tbl_post_refresh(void)
{
	struct partition_entry *e, *etmp;

	/*
	 * Purge items that disappeared
	 */
	TAILQ_FOREACH_SAFE(e, &partition_tbl, link, etmp)
		if (!(e->flags & HR_PARTITION_FOUND))
			partition_entry_delete(e);
}

/*
 * Finalization routine for hrPartitionTable
 * It destroys the lists and frees any allocated heap memory
 */
void
fini_partition_tbl(void)
{
	struct partition_map_entry *m;

	while ((m = STAILQ_FIRST(&partition_map)) != NULL) {
		STAILQ_REMOVE_HEAD(&partition_map, link);
		if(m->entry != NULL) {
			TAILQ_REMOVE(&partition_tbl, m->entry, link);
			free(m->entry->id);
			free(m->entry->label);
			free(m->entry);
		}
		free(m->id);
		free(m);
	}
	assert(TAILQ_EMPTY(&partition_tbl));
}

/**
 * Called from the file system code to insert the file system table index
 * into the partition table entry. Note, that an partition table entry exists
 * only for local file systems.
 */
void
handle_partition_fs_index(const char *name, int32_t fs_idx)
{
	struct partition_entry *entry;

	if ((entry = partition_entry_find_by_label(name)) == NULL) {
		HRDBG("%s IS MISSING from hrPartitionTable", name);
		return;
	}
	HRDBG("%s [FS index = %d] IS in hrPartitionTable", name, fs_idx);
	entry->fs_Index = fs_idx;
}

/*
 * This is the implementation for a generated (by our SNMP tool)
 * function prototype, see hostres_tree.h
 * It handles the SNMP operations for hrPartitionTable
 */
int
op_hrPartitionTable(struct snmp_context *ctx __unused, struct snmp_value *value,
    u_int sub, u_int iidx __unused, enum snmp_op op)
{
	struct partition_entry *entry;

	/*
	 * Refresh the disk storage table (which refreshes the partition
	 * table) if necessary.
	 */
	refresh_disk_storage_tbl(0);

	switch (op) {

	case SNMP_OP_GETNEXT:
		if ((entry = NEXT_OBJECT_FUNC(&partition_tbl,
		    &value->var, sub, partition_idx_cmp)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);

		value->var.len = sub + 2;
		value->var.subs[sub] = entry->index[0];
		value->var.subs[sub + 1] = entry->index[1];

		goto get;

	case SNMP_OP_GET:
		if ((entry = FIND_OBJECT_FUNC(&partition_tbl,
		    &value->var, sub, partition_idx_cmp)) == NULL)
			return (SNMP_ERR_NOSUCHNAME);
		goto get;

	case SNMP_OP_SET:
		if ((entry = FIND_OBJECT_FUNC(&partition_tbl,
		    &value->var, sub, partition_idx_cmp)) == NULL)
			return (SNMP_ERR_NOT_WRITEABLE);
		return (SNMP_ERR_NO_CREATION);

	case SNMP_OP_ROLLBACK:
	case SNMP_OP_COMMIT:
		abort();
	}
	abort();

  get:
	switch (value->var.subs[sub - 1]) {

	case LEAF_hrPartitionIndex:
		value->v.integer = entry->index[1];
		return (SNMP_ERR_NOERROR);

	case LEAF_hrPartitionLabel:
		return (string_get(value, entry->label, -1));

	case LEAF_hrPartitionID:
		return(string_get(value, entry->id, -1));

	case LEAF_hrPartitionSize:
		value->v.integer = entry->size;
		return (SNMP_ERR_NOERROR);

	case LEAF_hrPartitionFSIndex:
		value->v.integer = entry->fs_Index;
		return (SNMP_ERR_NOERROR);
	}
	abort();
}
OpenPOWER on IntegriCloud