summaryrefslogtreecommitdiff
path: root/usr/src/lib/libdiskmgt/common/cache.c
blob: 922847aa084c65eb96b2487fec731a338efa0aa8 (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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
/*
 * 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 2008 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <fcntl.h>
#include <libdevinfo.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <synch.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <libgen.h>
#include <syslog.h>

#include "libdiskmgt.h"
#include "disks_private.h"
#include "partition.h"

#define	ALIASES		0
#define	DEVPATHS	1

/*
 * Set DM_LIBDISKMGT_DEBUG in the environment.	Two levels of debugging:
 *    1 - errors, warnings and minimal tracing information
 *    2 - verbose information
 * All output prints on stderr.
 */
int dm_debug = 0;

/* Lock protecting the cached data */
static rwlock_t		cache_lock = DEFAULTRWLOCK;
static disk_t		*disk_listp = NULL;
static controller_t	*controller_listp = NULL;
static bus_t		*bus_listp = NULL;
static int		cache_loaded = 0;

descriptor_t		*desc_listp = NULL;

static void		clear_descriptors(void *gp);
static void		clr_ctrl_disk_ptr(controller_t *cp, disk_t *dp);
static void		clr_path_disk_ptr(path_t *pp, disk_t *dp);
static void		del_drive(disk_t *dp);
static void		del_drive_by_name(char *name);
static descriptor_t	*have_desc(int type, void *gp, char *name, char *mname);
static int		initialize();
static int		make_descriptors(int type);
static int		match_disk(disk_t *oldp, disk_t *newp);
static int		match_aliases(disk_t *d1p, disk_t *d2p);
static int		match_alias(alias_t *ap, alias_t *listp);
static descriptor_t	*new_descriptor(dm_desc_type_t type, void *op,
			    char *name, char *mname);
static void		rewalk_tree();
static void		update_desc(descriptor_t *descp, disk_t *newdisksp,
			    controller_t *newctrlp, bus_t *newbusp);
static void		update_desc_busp(descriptor_t *descp, bus_t *busp);
static void		update_desc_ctrlp(descriptor_t *descp,
			    controller_t *newstrlp);
static void		update_desc_diskp(descriptor_t *descp,
			    disk_t *newdisksp);
static void		update_desc_pathp(descriptor_t *descp,
			    controller_t *newctrlp);

/*
 * We only cache some of the data that we can obtain.  For much of the data
 * (e.g. slices & disks getting repartitioned) there are no events which would
 * enable us to cache.	As more events are added we can cache more information.
 *
 * Currently we cache the information we get from the dev tree walk.  This is
 * basically the information about the drives, aliases, devpaths, controllers
 * and paths.  We do not cache any information related to media, partitions
 * or slices.
 *
 * A fundamental part of the API design is that the application can hold on
 * to a set of descriptors for an indeterminate amount of time.	 Even if the
 * application does not hold descriptors there is a window of time between the
 * call that gets the descriptor and the use of the descriptor to get more
 * information.	 Because of this, the cache design must work even if the object
 * that the descriptor refers to no longer exists.
 *
 * Given this requirement, the code implements a two level cache.  The
 * descriptors that the application gets are really pointers into the first
 * level of the cache.	This first level contains the actual descriptors.
 * These descriptors in turn refer to the objects we build from the dev tree
 * walk which represent the drives and controllers.  This is the second level
 * in the cache.
 *
 * When we update the second level of the cache (the drives and controllers)
 * we go through the first level (the descriptors) and update the pointers
 * in those descriptors to refer to the new objects in the second level.  If
 * the object that the descriptor referred to is no longer in existence, we
 * just null out the pointer in the descriptor.	 In this way the code that
 * uses the descriptors knows that the object referred to by the descriptor
 * no longer exists.
 *
 * We keep a reference count in the descriptors.  This is incremented when
 * we hand out a pointer to the descriptor and decremented when the application
 * frees the descriptor it has.	 When the reference count goes to 0 we garbage
 * collect the descriptors.  In this way we only have to update active
 * descriptors when we refresh the cache after an event.
 *
 * An example of the flow when we create descriptors:
 *    dm_get_descriptors			libdiskmgt.c
 *	drive_get_descriptors			drive.c
 *	    cache_get_descriptors		cache.c
 *		make_descriptors		cache.c
 *		    drive_make_descriptors	drive.c
 *			cache_load_desc		cache.c
 *		{update refcnts on descriptors & return them}
 *
 * The idea behind cache_get_descriptors and cache_load_desc is that we
 * seperate the act of making the descriptor within the cache (which requires
 * us to call back out to one of the object functions - drive_make_descriptors)
 * from the act of handing out the descriptor (which requires us to increment
 * the refcnt).	 In this way we keep all of the refcnt handling centralized
 * in one function instead of forcing each object to ensure it replicates
 * the refcnt handling correctly.
 *
 * Descriptors use two different kinds of indrection to refer to their
 * corresponding object.  For objects we cache (controllers, paths & drives)
 * the descriptor keeps a pointer to that object.  For objects that we
 * dynamically build, the descriptor uses a combination of a pointer to the
 * base object (usually the drive) along with a name (e.g. the media name or
 * the alias).	For objects that are based on media (e.g. a slice) we actually
 * have to maintain a pointer (to the disk) and two names (e.g. the slice name
 * and the media name which is the secondary name).
 */

void
cache_free_alias(alias_t *aliasp)
{
	slice_t	*dp;

	free(aliasp->alias);
	free(aliasp->kstat_name);
	free(aliasp->wwn);

	/* free devpaths */
	dp = aliasp->devpaths;
	while (dp != NULL) {
		slice_t	*nextp;

		nextp = dp->next;
		free(dp->devpath);
		free(dp);
		dp = nextp;
	}

	/* free orig_paths */
	dp = aliasp->orig_paths;
	while (dp != NULL) {
		slice_t	*nextp;

		nextp = dp->next;
		free(dp->devpath);
		free(dp);
		dp = nextp;
	}

	free(aliasp);
}

void
cache_free_bus(bus_t *bp)
{
	free(bp->name);
	free(bp->btype);
	free(bp->kstat_name);
	free(bp->pname);
	free(bp->controllers);
	free(bp);
}

void
cache_free_controller(controller_t *cp)
{
	free(cp->name);
	free(cp->kstat_name);
	free(cp->disks);
	if (cp->paths != NULL) {
		int i;

		for (i = 0; cp->paths[i]; i++) {
			/* free the path since it can't exist w/o the ctrlr */
			cache_free_path(cp->paths[i]);
		}
		free(cp->paths);
	}

	free(cp);
}

void
cache_free_descriptor(descriptor_t *desc)
{
	if (!cache_is_valid_desc(desc)) {
		return;
	}

	desc->refcnt--;

	if (desc->refcnt <= 0) {
		free(desc->name);
		free(desc->secondary_name);
		if (desc->prev == NULL) {
			/* this is the first descriptor, update head ptr */
			desc_listp = desc->next;
		} else {
			desc->prev->next = desc->next;
		}
		if (desc->next != NULL) {
			desc->next->prev = desc->prev;
		}
		free(desc);
	}
}

void
cache_free_descriptors(descriptor_t **desc_list)
{
	int i;

	for (i = 0; desc_list[i]; i++) {
		cache_free_descriptor(desc_list[i]);
	}

	free(desc_list);
}

void
cache_free_disk(disk_t *dp)
{
	alias_t	*ap;

	free(dp->device_id);
	if (dp->devid != NULL) {
		devid_free(dp->devid);
	}
	free(dp->kernel_name);
	free(dp->product_id);
	free(dp->vendor_id);
	free(dp->controllers);
	/* the path objects are freed when we free the controller */
	free(dp->paths);
	ap = dp->aliases;
	while (ap != NULL) {
		alias_t	*nextp;

		nextp = ap->next;
		cache_free_alias(ap);
		ap = nextp;
	}

	free(dp);
}

void
cache_free_path(path_t *pp)
{
	free(pp->name);
	free(pp->disks);
	free(pp->states);

	if (pp->wwns) {
		int i;

		for (i = 0; pp->wwns[i]; i++) {
			free(pp->wwns[i]);
		}
		free(pp->wwns);
	}

	free(pp);
}

bus_t *
cache_get_buslist()
{
	if (initialize() != 0) {
		return (NULL);
	}

	return (bus_listp);
}

controller_t *
cache_get_controllerlist()
{
	if (initialize() != 0) {
		return (NULL);
	}

	return (controller_listp);
}

/*
 * This routine will either get the existing descriptor from the descriptor
 * cache or make make a new descriptor and put it in the descriptor cache and
 * return a pointer to that descriptor.	 We increment the refcnt when we hand
 * out the descriptor.
 */
descriptor_t *
cache_get_desc(int type, void *gp, char *name, char *secondary_name, int *errp)
{
	descriptor_t	*dp;

	*errp = 0;
	if ((dp = have_desc(type, gp, name, secondary_name)) == NULL) {
		/* make a new desc */
		if ((dp = new_descriptor(type, gp, name, secondary_name))
		    == NULL) {
			*errp = ENOMEM;
		}
	}

	if (dp != NULL) {
		dp->refcnt++;
	}

	return (dp);
}

descriptor_t **
cache_get_descriptors(int type, int *errp)
{
	descriptor_t	**descs;
	descriptor_t	*descp;
	int		cnt = 0;
	int		pos;

	if ((*errp = make_descriptors(type)) != 0) {
		return (NULL);
	}

	/* count the number of active descriptors in the descriptor cache */
	descp = desc_listp;
	while (descp != NULL) {
		if (descp->type == type && descp->p.generic != NULL) {
			cnt++;
		}
		descp = descp->next;
	}

	descs = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
	if (descs == NULL) {
		*errp = ENOMEM;
		return (NULL);
	}

	pos = 0;
	descp = desc_listp;
	while (descp != NULL) {
		if (descp->type == type && descp->p.generic != NULL) {
			/* update refcnts before handing out the descriptors */
			descp->refcnt++;
			descs[pos++] = descp;
		}
		descp = descp->next;
	}
	descs[pos] = NULL;

	*errp = 0;
	return (descs);
}

disk_t *
cache_get_disklist()
{
	if (initialize() != 0) {
		return (NULL);
	}

	return (disk_listp);
}

int
cache_is_valid_desc(descriptor_t *d)
{
	descriptor_t	*descp;

	for (descp = desc_listp; descp != NULL; descp = descp->next) {
		if (descp == d) {
			return (1);
		}
	}

	return (0);
}

/*
 * This function is called by the *_make_descriptors function
 * (e.g. drive_make_descriptors) within each of the objects.  This function
 * makes sure that the descriptor is built in the descriptor cache but
 * it does not hand out the descriptors, so the refcnt is never incremented.
 */
void
cache_load_desc(int type, void *gp, char *name, char *secondary_name, int *errp)
{
	*errp = 0;
	if (have_desc(type, gp, name, secondary_name) == NULL) {
		/* make a new desc */
		if (new_descriptor(type, gp, name, secondary_name) == NULL) {
			*errp = ENOMEM;
		}
	}
}

void
cache_rlock()
{
	(void) rw_rdlock(&cache_lock);
}

void
cache_unlock()
{
	(void) rw_unlock(&cache_lock);
}

/*
 * This function is called when we get a devtree event.	 Type is either add
 * or delete of a drive.
 *
 * For delete, we need to clean up the 2nd level structures and clean up
 * the pointers between the them.  We also clear the descriptor ptr.
 */
void
cache_update(dm_event_type_t ev_type, char *devname)
{
	char *orig_name;

	cache_wlock();

	/* update the cache */
	switch (ev_type) {
	case DM_EV_DISK_ADD:
		rewalk_tree();
		events_new_event(devname, DM_DRIVE, DM_EV_TADD);
		break;
	case DM_EV_DISK_DELETE:
		orig_name = devname;
		devname = basename(devname);
		del_drive_by_name(devname);
		events_new_event(orig_name, DM_DRIVE, DM_EV_TREMOVE);
		break;
	}

	cache_unlock();
}

void
cache_wlock()
{
	(void) rw_wrlock(&cache_lock);
}

/*
 * Clear any descriptors that point at the specified cached object.
 * We must go through the whole list since there can be multiple descriptors
 * referencing the same object (i.e. drive/media/slice descriptors all point
 * to the same drive object).  The list is usually small (0 size) so this
 * is not a big deal.
 */
static void
clear_descriptors(void *gp)
{
	descriptor_t	*descp;

	for (descp = desc_listp; descp != NULL; descp = descp->next) {
		if (descp->p.generic == gp)	{
			/* clear descriptor */
			descp->p.generic = NULL;
		}
	}
}

/* remove the ptr from the controller to the specified disk */
static void
clr_ctrl_disk_ptr(controller_t *cp, disk_t *dp)
{
	int i;

	for (i = 0; cp->disks[i]; i++) {
		if (dp == cp->disks[i]) {
			int j;

			for (j = i; cp->disks[j]; j++) {
				cp->disks[j] = cp->disks[j + 1];
			}
			return;
		}
	}
}

/* remove the ptr from the path to the specified disk */
static void
clr_path_disk_ptr(path_t *pp, disk_t *dp)
{
	int i;

	for (i = 0; pp->disks[i]; i++) {
		if (dp == pp->disks[i]) {
			int j;

			for (j = i; pp->disks[j]; j++) {
				pp->disks[j] = pp->disks[j + 1];
			}
			return;
		}
	}
}

static void
del_drive(disk_t *dp)
{
	int	i;
	disk_t	*listp;
	disk_t	*prev = NULL;

	clear_descriptors(dp);

	/* clear any ptrs from controllers to this drive */
	if (dp->controllers != NULL) {
		for (i = 0; dp->controllers[i]; i++) {
			clr_ctrl_disk_ptr(dp->controllers[i], dp);
		}
	}

	/* clear any ptrs from paths to this drive */
	if (dp->paths != NULL) {
		for (i = 0; dp->paths[i]; i++) {
			clr_path_disk_ptr(dp->paths[i], dp);
		}
	}

	/* clear drive from disk list */
	for (listp = disk_listp; listp != NULL; listp = listp->next) {
		if (dp == listp) {
			if (prev == NULL) {
				disk_listp = dp->next;
			} else {
				prev->next = dp->next;
			}

			break;
		}

		if (prev == NULL) {
			prev = disk_listp;
		} else {
			prev = prev->next;
		}
	}

	cache_free_disk(dp);
}

/*
 * Delete cached drive info when we get a devtree drive delete event.
 */
static void
del_drive_by_name(char *name)
{
	disk_t	*listp;

	for (listp = disk_listp; listp != NULL; listp = listp->next) {
		alias_t	*ap;

		for (ap = listp->aliases; ap; ap = ap->next) {
			if (libdiskmgt_str_eq(name, ap->alias)) {
				del_drive(listp);
				return;
			}
		}
	}
}

static descriptor_t *
have_desc(int type, void *gp, char *name, char *secondary_name)
{
	descriptor_t	*descp;

	if (name != NULL && name[0] == 0) {
		name = NULL;
	}

	if (secondary_name != NULL && secondary_name[0] == 0) {
		secondary_name = NULL;
	}

	descp = desc_listp;
	while (descp != NULL) {
		if (descp->type == type && descp->p.generic == gp &&
		    libdiskmgt_str_eq(descp->name, name)) {
			if (type == DM_SLICE || type == DM_PARTITION ||
			    type == DM_PATH) {
				if (libdiskmgt_str_eq(descp->secondary_name,
				    secondary_name)) {
					return (descp);
				}
			} else {
				return (descp);
			}
		}
		descp = descp->next;
	}

	return (NULL);
}

static int
initialize()
{
	struct search_args	args;

	if (cache_loaded) {
		return (0);
	}

	libdiskmgt_init_debug();

	findevs(&args);

	if (args.dev_walk_status != 0) {
		return (args.dev_walk_status);
	}

	disk_listp = args.disk_listp;
	controller_listp = args.controller_listp;
	bus_listp = args.bus_listp;

	cache_loaded = 1;

	/*
	 * Only start the event thread if we are not doing an install
	 */
	if (getenv("_LIBDISKMGT_INSTALL") == NULL) {
		if (events_start_event_watcher() != 0) {
			/*
			 * Log a message about the failure to start
			 * sysevents and continue on.
			 */
			syslog(LOG_WARNING, dgettext(TEXT_DOMAIN,
			    "libdiskmgt: sysevent thread for cache "
			    "events failed to start\n"));
		}
	}
	return (0);
}

static int
make_descriptors(int type)
{
	int	error;

	if ((error = initialize()) != 0) {
		return (error);
	}

	switch (type) {
	case DM_DRIVE:
		error = drive_make_descriptors();
		break;
	case DM_BUS:
		error = bus_make_descriptors();
		break;
	case DM_CONTROLLER:
		error = controller_make_descriptors();
		break;
	case DM_PATH:
		error = path_make_descriptors();
		break;
	case DM_ALIAS:
		error = alias_make_descriptors();
		break;
	case DM_MEDIA:
		error = media_make_descriptors();
		break;
	case DM_PARTITION:
		error = partition_make_descriptors();
		break;
	case DM_SLICE:
		error = slice_make_descriptors();
		break;
	}

	return (error);
}

static int
match_alias(alias_t *ap, alias_t *listp)
{
	if (ap->alias == NULL) {
		return (0);
	}

	while (listp != NULL) {
		if (libdiskmgt_str_eq(ap->alias, listp->alias)) {
			return (1);
		}
		listp = listp->next;
	}

	return (0);
}

static int
match_aliases(disk_t *d1p, disk_t *d2p)
{
	alias_t *ap;

	if (d1p->aliases == NULL || d2p->aliases == NULL) {
		return (0);
	}

	ap = d1p->aliases;
	while (ap != NULL) {
		if (match_alias(ap, d2p->aliases)) {
			return (1);
		}
		ap = ap->next;
	}

	return (0);
}

static int
match_disk(disk_t *oldp, disk_t *newp)
{
	if (oldp->devid != NULL) {
		if (newp->devid != NULL &&
		    devid_compare(oldp->devid, newp->devid) == 0) {
			return (1);
		}

	} else {
		/* oldp device id is null */
		if (newp->devid == NULL) {
			/* both disks have no device id, check aliases */
			if (match_aliases(oldp, newp)) {
				return (1);
			}
		}
	}

	return (0);
}

static descriptor_t *
new_descriptor(dm_desc_type_t type, void *op, char *name, char *secondary_name)
{
	descriptor_t	*d;

	if (name != NULL && name[0] == 0) {
		name = NULL;
	}

	if (secondary_name != NULL && secondary_name[0] == 0) {
		secondary_name = NULL;
	}

	d = (descriptor_t *)malloc(sizeof (descriptor_t));
	if (d == NULL) {
		return (NULL);
	}
	d->type = type;
	switch (type) {
	case DM_CONTROLLER:
		d->p.controller = op;
		break;
	case DM_BUS:
		d->p.bus = op;
		break;
	default:
		d->p.disk = op;
		break;
	}
	if (name != NULL) {
		d->name = strdup(name);
		if (d->name == NULL) {
			free(d);
			return (NULL);
		}
	} else {
		d->name = NULL;
	}

	if (type == DM_SLICE || type == DM_PARTITION) {
		if (secondary_name != NULL) {
			d->secondary_name = strdup(secondary_name);
			if (d->secondary_name == NULL) {
				free(d->name);
				free(d);
				return (NULL);
			}
		} else {
			d->secondary_name = NULL;
		}
	} else {
		d->secondary_name = NULL;
	}

	d->refcnt = 0;

	/* add this descriptor to the head of the list */
	if (desc_listp != NULL) {
		desc_listp->prev = d;
	}
	d->prev = NULL;
	d->next = desc_listp;
	desc_listp = d;

	return (d);
}

static void
rewalk_tree()
{
	struct search_args	args;
	disk_t			*free_disklistp;
	controller_t		*free_controllerlistp;
	bus_t			*free_buslistp;

	findevs(&args);

	if (args.dev_walk_status == 0) {
		descriptor_t	*descp;

		/* walk the existing descriptors and update the ptrs */
		descp = desc_listp;
		while (descp != NULL) {
			update_desc(descp, args.disk_listp,
			    args.controller_listp, args.bus_listp);
			descp = descp->next;
		}

		/* update the cached object ptrs */
		free_disklistp = disk_listp;
		free_controllerlistp = controller_listp;
		free_buslistp = bus_listp;
		disk_listp = args.disk_listp;
		controller_listp = args.controller_listp;
		bus_listp = args.bus_listp;

	} else {
		free_disklistp = args.disk_listp;
		free_controllerlistp = args.controller_listp;
		free_buslistp = args.bus_listp;
	}

	/*
	 * Free the memory from either the old cached objects or the failed
	 * update objects.
	 */
	while (free_disklistp != NULL) {
		disk_t *nextp;

		nextp = free_disklistp->next;
		cache_free_disk(free_disklistp);
		free_disklistp = nextp;
	}
	while (free_controllerlistp != NULL) {
		controller_t *nextp;

		nextp = free_controllerlistp->next;
		cache_free_controller(free_controllerlistp);
		free_controllerlistp = nextp;
	}
	while (free_buslistp != NULL) {
		bus_t *nextp;

		nextp = free_buslistp->next;
		cache_free_bus(free_buslistp);
		free_buslistp = nextp;
	}
}

/*
 * Walk the new set of cached objects and update the descriptor ptr to point
 * to the correct new object.  If there is no object any more, set the desc
 * ptr to null.
 */
static void
update_desc(descriptor_t *descp, disk_t *newdisksp, controller_t *newctrlp,
	bus_t *newbusp)
{
	/* if the descriptor is already dead, we're done */
	if (descp->p.generic == NULL) {
		return;
	}

	/*
	 * All descriptors use a disk ptr except for controller descriptors
	 * and path descriptors.
	 */

	switch (descp->type) {
	case DM_BUS:
		update_desc_busp(descp, newbusp);
		break;
	case DM_CONTROLLER:
		update_desc_ctrlp(descp, newctrlp);
		break;
	case DM_PATH:
		update_desc_pathp(descp, newctrlp);
		break;
	default:
		update_desc_diskp(descp, newdisksp);
		break;
	}
}

static void
update_desc_busp(descriptor_t *descp, bus_t *busp)
{
	/* walk the new objects and find the correct bus */
	for (; busp; busp = busp->next) {
		if (libdiskmgt_str_eq(descp->p.bus->name, busp->name)) {
			descp->p.bus = busp;
			return;
		}
	}

	/* we did not find the controller any more, clear the ptr in the desc */
	descp->p.bus = NULL;
}

static void
update_desc_ctrlp(descriptor_t *descp, controller_t *newctrlp)
{
	/* walk the new objects and find the correct controller */
	for (; newctrlp; newctrlp = newctrlp->next) {
		if (libdiskmgt_str_eq(descp->p.controller->name,
		    newctrlp->name)) {
			descp->p.controller = newctrlp;
			return;
		}
	}

	/* we did not find the controller any more, clear the ptr in the desc */
	descp->p.controller = NULL;
}

static void
update_desc_diskp(descriptor_t *descp, disk_t *newdisksp)
{
	/* walk the new objects and find the correct disk */
	for (; newdisksp; newdisksp = newdisksp->next) {
		if (match_disk(descp->p.disk, newdisksp)) {
			descp->p.disk = newdisksp;
			return;
		}
	}

	/* we did not find the disk any more, clear the ptr in the descriptor */
	descp->p.disk = NULL;
}

static void
update_desc_pathp(descriptor_t *descp, controller_t *newctrlp)
{
	/* walk the new objects and find the correct path */
	for (; newctrlp; newctrlp = newctrlp->next) {
		path_t	**pp;

		pp = newctrlp->paths;
		if (pp != NULL) {
			int i;

			for (i = 0; pp[i]; i++) {
				if (libdiskmgt_str_eq(descp->p.path->name,
				    pp[i]->name)) {
					descp->p.path = pp[i];
					return;
				}
			}
		}
	}

	/* we did not find the path any more, clear the ptr in the desc */
	descp->p.path = NULL;
}