xref: /illumos-gate/usr/src/common/mdesc/mdesc_diff.c (revision c40a6cd7)
1*1ae08745Sheppo /*
2*1ae08745Sheppo  * CDDL HEADER START
3*1ae08745Sheppo  *
4*1ae08745Sheppo  * The contents of this file are subject to the terms of the
5*1ae08745Sheppo  * Common Development and Distribution License (the "License").
6*1ae08745Sheppo  * You may not use this file except in compliance with the License.
7*1ae08745Sheppo  *
8*1ae08745Sheppo  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*1ae08745Sheppo  * or http://www.opensolaris.org/os/licensing.
10*1ae08745Sheppo  * See the License for the specific language governing permissions
11*1ae08745Sheppo  * and limitations under the License.
12*1ae08745Sheppo  *
13*1ae08745Sheppo  * When distributing Covered Code, include this CDDL HEADER in each
14*1ae08745Sheppo  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*1ae08745Sheppo  * If applicable, add the following below this CDDL HEADER, with the
16*1ae08745Sheppo  * fields enclosed by brackets "[]" replaced with your own identifying
17*1ae08745Sheppo  * information: Portions Copyright [yyyy] [name of copyright owner]
18*1ae08745Sheppo  *
19*1ae08745Sheppo  * CDDL HEADER END
20*1ae08745Sheppo  */
21*1ae08745Sheppo 
22*1ae08745Sheppo /*
23*1ae08745Sheppo  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24*1ae08745Sheppo  * Use is subject to license terms.
25*1ae08745Sheppo  */
26*1ae08745Sheppo 
27*1ae08745Sheppo #include <sys/types.h>
28*1ae08745Sheppo #ifdef _KERNEL
29*1ae08745Sheppo #include <sys/systm.h>
30*1ae08745Sheppo #else /* _KERNEL */
31*1ae08745Sheppo #include <string.h>
32*1ae08745Sheppo #include <strings.h>
33*1ae08745Sheppo #endif /* _KERNEL */
34*1ae08745Sheppo #include <sys/note.h>
35*1ae08745Sheppo #include <sys/mdesc.h>
36*1ae08745Sheppo #include <sys/mdesc_impl.h>
37*1ae08745Sheppo 
38*1ae08745Sheppo #define	MDD_FREE_CHECK(mdp, ptr, sz)		\
39*1ae08745Sheppo 	do {					\
40*1ae08745Sheppo 		if (ptr) mdp->freep(ptr, sz);	\
41*1ae08745Sheppo 	_NOTE(CONSTCOND) } while (0)
42*1ae08745Sheppo 
43*1ae08745Sheppo #define	MD_DIFF_MAGIC			0x4D445F4449464621ull	/* 'MD_DIFF!' */
44*1ae08745Sheppo #define	MD_DIFF_NOMATCH			(-1)
45*1ae08745Sheppo #define	MD_DIFF_MATCH			(1)
46*1ae08745Sheppo 
47*1ae08745Sheppo typedef struct {
48*1ae08745Sheppo 	mde_cookie_t	*mdep;
49*1ae08745Sheppo 	uint_t		nelem;
50*1ae08745Sheppo } md_diff_t;
51*1ae08745Sheppo 
52*1ae08745Sheppo typedef struct {
53*1ae08745Sheppo 	uint64_t	mdd_magic;
54*1ae08745Sheppo 	md_diff_t	added;
55*1ae08745Sheppo 	md_diff_t	removed;
56*1ae08745Sheppo 	md_diff_t	match1;
57*1ae08745Sheppo 	md_diff_t	match2;
58*1ae08745Sheppo 	void 		*(*allocp)(size_t);
59*1ae08745Sheppo 	void		(*freep)(void *, size_t);
60*1ae08745Sheppo } md_diff_impl_t;
61*1ae08745Sheppo 
62*1ae08745Sheppo /*
63*1ae08745Sheppo  * Internal utility functions
64*1ae08745Sheppo  */
65*1ae08745Sheppo static int mdd_scan_for_nodes(md_t *mdp, mde_cookie_t start,
66*1ae08745Sheppo     char *compnodep, int *countp, mde_cookie_t **nodespp);
67*1ae08745Sheppo 
68*1ae08745Sheppo static boolean_t mdd_any_dup_nodes(md_impl_t *mdp, md_prop_match_t *pmp,
69*1ae08745Sheppo     int count, mde_cookie_t *nodesp);
70*1ae08745Sheppo 
71*1ae08745Sheppo static int mdd_node_list_match(md_impl_t *md1, md_impl_t *md2,
72*1ae08745Sheppo     md_element_t *match_nodep, mde_cookie_t *match_listp,
73*1ae08745Sheppo     uint8_t *match_seenp, int start, int end, md_prop_match_t *match_elemsp);
74*1ae08745Sheppo 
75*1ae08745Sheppo static int mdd_node_compare(md_impl_t *mdap, md_impl_t *mdbp,
76*1ae08745Sheppo     md_element_t *nodeap, md_element_t *nodebp, md_prop_match_t *match_elemsp);
77*1ae08745Sheppo 
78*1ae08745Sheppo /*
79*1ae08745Sheppo  * Given two DAGs and information about how to uniquely identify
80*1ae08745Sheppo  * the nodes of interest, determine which nodes have been added
81*1ae08745Sheppo  * to the second MD, removed from the first MD, or exist in both
82*1ae08745Sheppo  * MDs. This information is recorded and can be accessed using the
83*1ae08745Sheppo  * opaque cookie returned to the caller.
84*1ae08745Sheppo  */
85*1ae08745Sheppo md_diff_cookie_t
md_diff_init(md_t * md1p,mde_cookie_t start1,md_t * md2p,mde_cookie_t start2,char * compnodep,md_prop_match_t * match_fieldsp)86*1ae08745Sheppo md_diff_init(md_t *md1p, mde_cookie_t start1, md_t *md2p, mde_cookie_t start2,
87*1ae08745Sheppo     char *compnodep, md_prop_match_t *match_fieldsp)
88*1ae08745Sheppo {
89*1ae08745Sheppo 	int		idx;
90*1ae08745Sheppo 	md_impl_t	*md1 = (md_impl_t *)md1p;
91*1ae08745Sheppo 	md_impl_t	*md2 = (md_impl_t *)md2p;
92*1ae08745Sheppo 	mde_cookie_t	*md1nodesp = NULL;
93*1ae08745Sheppo 	mde_cookie_t	*md2nodesp = NULL;
94*1ae08745Sheppo 	int		md1count = 0;
95*1ae08745Sheppo 	int		md2count = 0;
96*1ae08745Sheppo 	uint8_t		*seenp = NULL;
97*1ae08745Sheppo 
98*1ae08745Sheppo 	/* variables used to gather results */
99*1ae08745Sheppo 	md_diff_impl_t	*diff_res;
100*1ae08745Sheppo 	mde_cookie_t	*mde_add_scr;
101*1ae08745Sheppo 	mde_cookie_t	*mde_rem_scr;
102*1ae08745Sheppo 	mde_cookie_t	*mde_match1_scr;
103*1ae08745Sheppo 	mde_cookie_t	*mde_match2_scr;
104*1ae08745Sheppo 	int		nadd = 0;
105*1ae08745Sheppo 	int		nrem = 0;
106*1ae08745Sheppo 	int		nmatch = 0;
107*1ae08745Sheppo 
108*1ae08745Sheppo 	/* sanity check params */
109*1ae08745Sheppo 	if ((md1p == NULL) || (md2p == NULL))
110*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
111*1ae08745Sheppo 
112*1ae08745Sheppo 	if ((start1 == MDE_INVAL_ELEM_COOKIE) ||
113*1ae08745Sheppo 	    (start2 == MDE_INVAL_ELEM_COOKIE))
114*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
115*1ae08745Sheppo 
116*1ae08745Sheppo 	if ((compnodep == NULL) || (match_fieldsp == NULL))
117*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
118*1ae08745Sheppo 
119*1ae08745Sheppo 	/*
120*1ae08745Sheppo 	 * Prepare an array of the matching nodes from the first MD.
121*1ae08745Sheppo 	 */
122*1ae08745Sheppo 	if (mdd_scan_for_nodes(md1p,
123*1ae08745Sheppo 	    start1, compnodep, &md1count, &md1nodesp) == -1)
124*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
125*1ae08745Sheppo 
126*1ae08745Sheppo 	/* sanity check that all nodes are unique */
127*1ae08745Sheppo 	if (md1nodesp &&
128*1ae08745Sheppo 	    mdd_any_dup_nodes(md1, match_fieldsp, md1count, md1nodesp)) {
129*1ae08745Sheppo 		MDD_FREE_CHECK(md1, md1nodesp, sizeof (mde_cookie_t) *
130*1ae08745Sheppo 		    md1count);
131*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
132*1ae08745Sheppo 	}
133*1ae08745Sheppo 
134*1ae08745Sheppo 
135*1ae08745Sheppo 	/*
136*1ae08745Sheppo 	 * Prepare an array of the matching nodes from the second MD.
137*1ae08745Sheppo 	 */
138*1ae08745Sheppo 	if (mdd_scan_for_nodes(md2p,
139*1ae08745Sheppo 	    start2, compnodep, &md2count, &md2nodesp) == -1)
140*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
141*1ae08745Sheppo 
142*1ae08745Sheppo 	/* sanity check that all nodes are unique */
143*1ae08745Sheppo 	if (md2nodesp &&
144*1ae08745Sheppo 	    mdd_any_dup_nodes(md2, match_fieldsp, md2count, md2nodesp)) {
145*1ae08745Sheppo 		MDD_FREE_CHECK(md1, md1nodesp, sizeof (mde_cookie_t) *
146*1ae08745Sheppo 		    md1count);
147*1ae08745Sheppo 		MDD_FREE_CHECK(md2, md2nodesp, sizeof (mde_cookie_t) *
148*1ae08745Sheppo 		    md2count);
149*1ae08745Sheppo 		return (MD_INVAL_DIFF_COOKIE);
150*1ae08745Sheppo 	}
151*1ae08745Sheppo 
152*1ae08745Sheppo 	/* setup our result structure */
153*1ae08745Sheppo 	diff_res = md1->allocp(sizeof (md_diff_impl_t));
154*1ae08745Sheppo 	bzero(diff_res, sizeof (md_diff_impl_t));
155*1ae08745Sheppo 	diff_res->allocp = md1->allocp;
156*1ae08745Sheppo 	diff_res->freep = md1->freep;
157*1ae08745Sheppo 	diff_res->mdd_magic = MD_DIFF_MAGIC;
158*1ae08745Sheppo 
159*1ae08745Sheppo 	/*
160*1ae08745Sheppo 	 * Special cases for empty lists
161*1ae08745Sheppo 	 */
162*1ae08745Sheppo 	if ((md1count == 0) && (md2count != 0)) {
163*1ae08745Sheppo 		/* all the nodes found were added */
164*1ae08745Sheppo 		diff_res->added.mdep = md2nodesp;
165*1ae08745Sheppo 		diff_res->added.nelem = md2count;
166*1ae08745Sheppo 		return ((mde_cookie_t)diff_res);
167*1ae08745Sheppo 	}
168*1ae08745Sheppo 
169*1ae08745Sheppo 	if ((md1count != 0) && (md2count == 0)) {
170*1ae08745Sheppo 		/* all the nodes found were removed */
171*1ae08745Sheppo 		diff_res->removed.mdep = md1nodesp;
172*1ae08745Sheppo 		diff_res->removed.nelem = md1count;
173*1ae08745Sheppo 		return ((mde_cookie_t)diff_res);
174*1ae08745Sheppo 	}
175*1ae08745Sheppo 
176*1ae08745Sheppo 	if ((md1count == 0) && (md2count == 0))
177*1ae08745Sheppo 		/* no nodes found */
178*1ae08745Sheppo 		return ((mde_cookie_t)diff_res);
179*1ae08745Sheppo 
180*1ae08745Sheppo 	/*
181*1ae08745Sheppo 	 * Both lists have some elements. Allocate some scratch
182*1ae08745Sheppo 	 * buffers to sort them into our three categories, added,
183*1ae08745Sheppo 	 * removed, and matched pairs.
184*1ae08745Sheppo 	 */
185*1ae08745Sheppo 	mde_add_scr = diff_res->allocp(sizeof (mde_cookie_t) * md2count);
186*1ae08745Sheppo 	mde_rem_scr = diff_res->allocp(sizeof (mde_cookie_t) * md1count);
187*1ae08745Sheppo 	mde_match1_scr = diff_res->allocp(sizeof (mde_cookie_t) * md1count);
188*1ae08745Sheppo 	mde_match2_scr = diff_res->allocp(sizeof (mde_cookie_t) * md2count);
189*1ae08745Sheppo 
190*1ae08745Sheppo 	/* array of seen flags only needed for md2 */
191*1ae08745Sheppo 	seenp = (uint8_t *)diff_res->allocp(sizeof (uint8_t) * md2count);
192*1ae08745Sheppo 	bzero(seenp, sizeof (uint8_t) * md2count);
193*1ae08745Sheppo 
194*1ae08745Sheppo 	/*
195*1ae08745Sheppo 	 * Make a pass through the md1 node array. Make note of
196*1ae08745Sheppo 	 * any nodes not in the md2 array, indicating that they
197*1ae08745Sheppo 	 * have been removed. Also keep track of the nodes that
198*1ae08745Sheppo 	 * are present in both arrays for the matched pair results.
199*1ae08745Sheppo 	 */
200*1ae08745Sheppo 	for (idx = 0; idx < md1count; idx++) {
201*1ae08745Sheppo 
202*1ae08745Sheppo 		md_element_t *elem = &(md1->mdep[md1nodesp[idx]]);
203*1ae08745Sheppo 
204*1ae08745Sheppo 		int match = mdd_node_list_match(md1, md2, elem, md2nodesp,
205*1ae08745Sheppo 		    seenp, 0, md2count - 1, match_fieldsp);
206*1ae08745Sheppo 
207*1ae08745Sheppo 		if (match == MD_DIFF_NOMATCH)
208*1ae08745Sheppo 			/* record deleted node */
209*1ae08745Sheppo 			mde_rem_scr[nrem++] = md1nodesp[idx];
210*1ae08745Sheppo 		else {
211*1ae08745Sheppo 			/* record matched node pair */
212*1ae08745Sheppo 			mde_match1_scr[nmatch] = md1nodesp[idx];
213*1ae08745Sheppo 			mde_match2_scr[nmatch] = md2nodesp[match];
214*1ae08745Sheppo 			nmatch++;
215*1ae08745Sheppo 
216*1ae08745Sheppo 			/* mark that this match has been recorded */
217*1ae08745Sheppo 			seenp[match] = 1;
218*1ae08745Sheppo 		}
219*1ae08745Sheppo 	}
220*1ae08745Sheppo 
221*1ae08745Sheppo 	/*
222*1ae08745Sheppo 	 * Make a pass through the md2 array. Any nodes that have
223*1ae08745Sheppo 	 * not been marked as seen have been added.
224*1ae08745Sheppo 	 */
225*1ae08745Sheppo 	for (idx = 0; idx < md2count; idx++) {
226*1ae08745Sheppo 		if (!seenp[idx])
227*1ae08745Sheppo 			/* record added node */
228*1ae08745Sheppo 			mde_add_scr[nadd++] = md2nodesp[idx];
229*1ae08745Sheppo 	}
230*1ae08745Sheppo 
231*1ae08745Sheppo 	/* fill in the added node list */
232*1ae08745Sheppo 	if (nadd) {
233*1ae08745Sheppo 		int addsz = sizeof (mde_cookie_t) * nadd;
234*1ae08745Sheppo 		diff_res->added.mdep = (mde_cookie_t *)diff_res->allocp(addsz);
235*1ae08745Sheppo 
236*1ae08745Sheppo 		bcopy(mde_add_scr, diff_res->added.mdep, addsz);
237*1ae08745Sheppo 
238*1ae08745Sheppo 		diff_res->added.nelem = nadd;
239*1ae08745Sheppo 	}
240*1ae08745Sheppo 
241*1ae08745Sheppo 	/* fill in the removed node list */
242*1ae08745Sheppo 	if (nrem) {
243*1ae08745Sheppo 		int remsz = sizeof (mde_cookie_t) * nrem;
244*1ae08745Sheppo 		diff_res->removed.mdep =
245*1ae08745Sheppo 		    (mde_cookie_t *)diff_res->allocp(remsz);
246*1ae08745Sheppo 
247*1ae08745Sheppo 		bcopy(mde_rem_scr, diff_res->removed.mdep, remsz);
248*1ae08745Sheppo 		diff_res->removed.nelem = nrem;
249*1ae08745Sheppo 	}
250*1ae08745Sheppo 
251*1ae08745Sheppo 	/* fill in the matching node lists */
252*1ae08745Sheppo 	if (nmatch) {
253*1ae08745Sheppo 		int matchsz = sizeof (mde_cookie_t) * nmatch;
254*1ae08745Sheppo 		diff_res->match1.mdep =
255*1ae08745Sheppo 		    (mde_cookie_t *)diff_res->allocp(matchsz);
256*1ae08745Sheppo 		diff_res->match2.mdep =
257*1ae08745Sheppo 		    (mde_cookie_t *)diff_res->allocp(matchsz);
258*1ae08745Sheppo 
259*1ae08745Sheppo 		bcopy(mde_match1_scr, diff_res->match1.mdep, matchsz);
260*1ae08745Sheppo 		bcopy(mde_match2_scr, diff_res->match2.mdep, matchsz);
261*1ae08745Sheppo 		diff_res->match1.nelem = nmatch;
262*1ae08745Sheppo 		diff_res->match2.nelem = nmatch;
263*1ae08745Sheppo 	}
264*1ae08745Sheppo 
265*1ae08745Sheppo 	/* clean up */
266*1ae08745Sheppo 	md1->freep(md1nodesp, sizeof (mde_cookie_t) * md1count);
267*1ae08745Sheppo 	md2->freep(md2nodesp, sizeof (mde_cookie_t) * md2count);
268*1ae08745Sheppo 
269*1ae08745Sheppo 	diff_res->freep(mde_add_scr, sizeof (mde_cookie_t) * md2count);
270*1ae08745Sheppo 	diff_res->freep(mde_rem_scr, sizeof (mde_cookie_t) * md1count);
271*1ae08745Sheppo 	diff_res->freep(mde_match1_scr, sizeof (mde_cookie_t) * md1count);
272*1ae08745Sheppo 	diff_res->freep(mde_match2_scr, sizeof (mde_cookie_t) * md2count);
273*1ae08745Sheppo 
274*1ae08745Sheppo 	diff_res->freep(seenp, sizeof (uint8_t) * md2count);
275*1ae08745Sheppo 
276*1ae08745Sheppo 	return ((md_diff_cookie_t)diff_res);
277*1ae08745Sheppo }
278*1ae08745Sheppo 
279*1ae08745Sheppo /*
280*1ae08745Sheppo  * Returns an array of the nodes added to the second MD in a
281*1ae08745Sheppo  * previous md_diff_init() call. Returns the number of elements
282*1ae08745Sheppo  * in the returned array. If the value is zero, the pointer
283*1ae08745Sheppo  * passed back will be NULL.
284*1ae08745Sheppo  */
285*1ae08745Sheppo int
md_diff_added(md_diff_cookie_t mdd,mde_cookie_t ** mde_addedp)286*1ae08745Sheppo md_diff_added(md_diff_cookie_t mdd, mde_cookie_t **mde_addedp)
287*1ae08745Sheppo {
288*1ae08745Sheppo 	md_diff_impl_t	*mddp = (md_diff_impl_t *)mdd;
289*1ae08745Sheppo 
290*1ae08745Sheppo 	if ((mddp == NULL) || (mddp->mdd_magic != MD_DIFF_MAGIC))
291*1ae08745Sheppo 		return (-1);
292*1ae08745Sheppo 
293*1ae08745Sheppo 	*mde_addedp = mddp->added.mdep;
294*1ae08745Sheppo 
295*1ae08745Sheppo 	return (mddp->added.nelem);
296*1ae08745Sheppo }
297*1ae08745Sheppo 
298*1ae08745Sheppo /*
299*1ae08745Sheppo  * Returns an array of the nodes removed from the first MD in a
300*1ae08745Sheppo  * previous md_diff_init() call. Returns the number of elements
301*1ae08745Sheppo  * in the returned array. If the value is zero, the pointer
302*1ae08745Sheppo  * passed back will be NULL.
303*1ae08745Sheppo  */
304*1ae08745Sheppo int
md_diff_removed(md_diff_cookie_t mdd,mde_cookie_t ** mde_removedp)305*1ae08745Sheppo md_diff_removed(md_diff_cookie_t mdd, mde_cookie_t **mde_removedp)
306*1ae08745Sheppo {
307*1ae08745Sheppo 	md_diff_impl_t	*mddp = (md_diff_impl_t *)mdd;
308*1ae08745Sheppo 
309*1ae08745Sheppo 	if ((mddp == NULL) || (mddp->mdd_magic != MD_DIFF_MAGIC))
310*1ae08745Sheppo 		return (-1);
311*1ae08745Sheppo 
312*1ae08745Sheppo 	*mde_removedp = mddp->removed.mdep;
313*1ae08745Sheppo 
314*1ae08745Sheppo 	return (mddp->removed.nelem);
315*1ae08745Sheppo }
316*1ae08745Sheppo 
317*1ae08745Sheppo /*
318*1ae08745Sheppo  * Returns a pair of parallel arrays that contain nodes that were
319*1ae08745Sheppo  * considered matching based on the match criteria passed in to
320*1ae08745Sheppo  * a previous md_diff_init() call. Returns the number of elements
321*1ae08745Sheppo  * in the arrays. If the value is zero, both pointers passed back
322*1ae08745Sheppo  * will be NULL.
323*1ae08745Sheppo  */
324*1ae08745Sheppo int
md_diff_matched(md_diff_cookie_t mdd,mde_cookie_t ** mde_match1p,mde_cookie_t ** mde_match2p)325*1ae08745Sheppo md_diff_matched(md_diff_cookie_t mdd, mde_cookie_t **mde_match1p,
326*1ae08745Sheppo     mde_cookie_t **mde_match2p)
327*1ae08745Sheppo {
328*1ae08745Sheppo 	md_diff_impl_t	*mddp = (md_diff_impl_t *)mdd;
329*1ae08745Sheppo 
330*1ae08745Sheppo 	if ((mddp == NULL) || (mddp->mdd_magic != MD_DIFF_MAGIC))
331*1ae08745Sheppo 		return (-1);
332*1ae08745Sheppo 
333*1ae08745Sheppo 	*mde_match1p = mddp->match1.mdep;
334*1ae08745Sheppo 	*mde_match2p = mddp->match2.mdep;
335*1ae08745Sheppo 
336*1ae08745Sheppo 	return (mddp->match1.nelem);
337*1ae08745Sheppo }
338*1ae08745Sheppo 
339*1ae08745Sheppo /*
340*1ae08745Sheppo  * Deallocate any storage used to store results of a previous
341*1ae08745Sheppo  * md_diff_init() call. Returns 0 on success and -1 on failure.
342*1ae08745Sheppo  */
343*1ae08745Sheppo int
md_diff_fini(md_diff_cookie_t mdd)344*1ae08745Sheppo md_diff_fini(md_diff_cookie_t mdd)
345*1ae08745Sheppo {
346*1ae08745Sheppo 	md_diff_impl_t	*mddp = (md_diff_impl_t *)mdd;
347*1ae08745Sheppo 
348*1ae08745Sheppo 	if ((mddp == NULL) || (mddp->mdd_magic != MD_DIFF_MAGIC))
349*1ae08745Sheppo 		return (-1);
350*1ae08745Sheppo 
351*1ae08745Sheppo 	mddp->mdd_magic = 0;
352*1ae08745Sheppo 
353*1ae08745Sheppo 	MDD_FREE_CHECK(mddp, mddp->added.mdep, mddp->added.nelem *
354*1ae08745Sheppo 	    sizeof (mde_cookie_t));
355*1ae08745Sheppo 
356*1ae08745Sheppo 	MDD_FREE_CHECK(mddp, mddp->removed.mdep, mddp->removed.nelem *
357*1ae08745Sheppo 	    sizeof (mde_cookie_t));
358*1ae08745Sheppo 
359*1ae08745Sheppo 	MDD_FREE_CHECK(mddp, mddp->match1.mdep, mddp->match1.nelem *
360*1ae08745Sheppo 	    sizeof (mde_cookie_t));
361*1ae08745Sheppo 
362*1ae08745Sheppo 	MDD_FREE_CHECK(mddp, mddp->match2.mdep, mddp->match2.nelem *
363*1ae08745Sheppo 	    sizeof (mde_cookie_t));
364*1ae08745Sheppo 
365*1ae08745Sheppo 	mddp->freep(mddp, sizeof (md_diff_impl_t));
366*1ae08745Sheppo 
367*1ae08745Sheppo 	return (0);
368*1ae08745Sheppo }
369*1ae08745Sheppo 
370*1ae08745Sheppo /*
371*1ae08745Sheppo  * Walk the "fwd" DAG in an MD and return an array of nodes that are
372*1ae08745Sheppo  * of the specified type. The start param is used to start the walk
373*1ae08745Sheppo  * from an arbitrary location in the DAG. Returns an array of nodes
374*1ae08745Sheppo  * as well as a count of the number of nodes in the array.  If the
375*1ae08745Sheppo  * count is zero, the node pointer will be passed back as NULL.
376*1ae08745Sheppo  *
377*1ae08745Sheppo  * Returns: 0 success; -1 failure
378*1ae08745Sheppo  */
379*1ae08745Sheppo static int
mdd_scan_for_nodes(md_t * mdp,mde_cookie_t start,char * compnodep,int * countp,mde_cookie_t ** nodespp)380*1ae08745Sheppo mdd_scan_for_nodes(md_t *mdp,
381*1ae08745Sheppo     mde_cookie_t start, char *compnodep, int *countp, mde_cookie_t **nodespp)
382*1ae08745Sheppo {
383*1ae08745Sheppo 	mde_str_cookie_t	cname;
384*1ae08745Sheppo 	mde_str_cookie_t	aname;
385*1ae08745Sheppo 	md_impl_t		*mdip = (md_impl_t *)mdp;
386*1ae08745Sheppo 
387*1ae08745Sheppo 	if (mdip == NULL)
388*1ae08745Sheppo 		return (-1);
389*1ae08745Sheppo 
390*1ae08745Sheppo 	cname = md_find_name(mdp, compnodep);
391*1ae08745Sheppo 	aname = md_find_name(mdp, "fwd");
392*1ae08745Sheppo 
393*1ae08745Sheppo 	/* get the number of nodes of interest in the DAG */
394*1ae08745Sheppo 	*countp = md_scan_dag(mdp, start, cname, aname, NULL);
395*1ae08745Sheppo 	if (*countp == 0) {
396*1ae08745Sheppo 		*nodespp = NULL;
397*1ae08745Sheppo 		return (0);
398*1ae08745Sheppo 	}
399*1ae08745Sheppo 
400*1ae08745Sheppo 	/* allocate the storage */
401*1ae08745Sheppo 	*nodespp = mdip->allocp(sizeof (mde_cookie_t) * (*countp));
402*1ae08745Sheppo 
403*1ae08745Sheppo 	/* populate our array with the matching nodes */
404*1ae08745Sheppo 	(void) md_scan_dag(mdp, start, cname, aname, *nodespp);
405*1ae08745Sheppo 
406*1ae08745Sheppo 	return (0);
407*1ae08745Sheppo }
408*1ae08745Sheppo 
409*1ae08745Sheppo /*
410*1ae08745Sheppo  * Walk an array of nodes and check if there are any duplicate
411*1ae08745Sheppo  * nodes. A duplicate is determined based on the specified match
412*1ae08745Sheppo  * criteria. Returns B_TRUE if there are any duplicates and B_FALSE
413*1ae08745Sheppo  * otherwise.
414*1ae08745Sheppo  */
415*1ae08745Sheppo static boolean_t
mdd_any_dup_nodes(md_impl_t * mdp,md_prop_match_t * pmp,int count,mde_cookie_t * nodesp)416*1ae08745Sheppo mdd_any_dup_nodes(md_impl_t *mdp, md_prop_match_t *pmp, int count,
417*1ae08745Sheppo     mde_cookie_t *nodesp)
418*1ae08745Sheppo {
419*1ae08745Sheppo 	int		idx;
420*1ae08745Sheppo 	int		match;
421*1ae08745Sheppo 	md_element_t	*elem;
422*1ae08745Sheppo 
423*1ae08745Sheppo 	ASSERT(count > 0 || nodesp == NULL);
424*1ae08745Sheppo 
425*1ae08745Sheppo 	for (idx = 0; idx < count; idx++) {
426*1ae08745Sheppo 		elem = &(mdp->mdep[nodesp[idx]]);
427*1ae08745Sheppo 
428*1ae08745Sheppo 		match = mdd_node_list_match(mdp, mdp, elem, nodesp, NULL,
429*1ae08745Sheppo 		    idx + 1, count - 1, pmp);
430*1ae08745Sheppo 
431*1ae08745Sheppo 		if (match != MD_DIFF_NOMATCH)
432*1ae08745Sheppo 			return (B_TRUE);
433*1ae08745Sheppo 	}
434*1ae08745Sheppo 
435*1ae08745Sheppo 	return (B_FALSE);
436*1ae08745Sheppo }
437*1ae08745Sheppo 
438*1ae08745Sheppo /*
439*1ae08745Sheppo  * Given a node and a array of nodes, compare the node to all elements
440*1ae08745Sheppo  * in the specified start-end range of the array. If the node matches
441*1ae08745Sheppo  * one of the nodes in the array, return the index of that node. Otherwise
442*1ae08745Sheppo  * return MD_DIFF_NOMATCH.
443*1ae08745Sheppo  *
444*1ae08745Sheppo  * The optional seen array parameter can be used to optimize repeated
445*1ae08745Sheppo  * calls to this function. If the seen array indicates that an element
446*1ae08745Sheppo  * has already been matched, the full comparison is not necessary.
447*1ae08745Sheppo  */
448*1ae08745Sheppo static int
mdd_node_list_match(md_impl_t * md1,md_impl_t * md2,md_element_t * match_nodep,mde_cookie_t * match_listp,uint8_t * match_seenp,int start,int end,md_prop_match_t * match_elemsp)449*1ae08745Sheppo mdd_node_list_match(md_impl_t *md1, md_impl_t *md2, md_element_t *match_nodep,
450*1ae08745Sheppo     mde_cookie_t *match_listp, uint8_t *match_seenp, int start, int end,
451*1ae08745Sheppo     md_prop_match_t *match_elemsp)
452*1ae08745Sheppo {
453*1ae08745Sheppo 	int		match;
454*1ae08745Sheppo 	int		idx;
455*1ae08745Sheppo 	md_element_t	*elem;
456*1ae08745Sheppo 
457*1ae08745Sheppo 	for (idx = start; idx <= end; idx++) {
458*1ae08745Sheppo 
459*1ae08745Sheppo 		if ((match_seenp != NULL) && (match_seenp[idx]))
460*1ae08745Sheppo 			continue;
461*1ae08745Sheppo 
462*1ae08745Sheppo 		elem = &(md2->mdep[match_listp[idx]]);
463*1ae08745Sheppo 
464*1ae08745Sheppo 		match = mdd_node_compare(md1, md2, match_nodep, elem,
465*1ae08745Sheppo 		    match_elemsp);
466*1ae08745Sheppo 		if (match == MD_DIFF_MATCH)
467*1ae08745Sheppo 			return (idx);
468*1ae08745Sheppo 	}
469*1ae08745Sheppo 
470*1ae08745Sheppo 	return (MD_DIFF_NOMATCH);
471*1ae08745Sheppo }
472*1ae08745Sheppo 
473*1ae08745Sheppo /*
474*1ae08745Sheppo  * Given two nodes and a list of properties, compare the nodes.
475*1ae08745Sheppo  * A match is concluded if both nodes have all of the specified
476*1ae08745Sheppo  * properties and all the values of those properties are the
477*1ae08745Sheppo  * same. Returns MD_DIFF_NOMATCH if the nodes do not match and
478*1ae08745Sheppo  * MD_DIFF_MATCH otherwise.
479*1ae08745Sheppo  */
480*1ae08745Sheppo static int
mdd_node_compare(md_impl_t * mdap,md_impl_t * mdbp,md_element_t * nodeap,md_element_t * nodebp,md_prop_match_t * match_elemsp)481*1ae08745Sheppo mdd_node_compare(md_impl_t *mdap, md_impl_t *mdbp, md_element_t *nodeap,
482*1ae08745Sheppo     md_element_t *nodebp, md_prop_match_t *match_elemsp)
483*1ae08745Sheppo {
484*1ae08745Sheppo 	md_element_t	*ap;
485*1ae08745Sheppo 	md_element_t	*bp;
486*1ae08745Sheppo 	boolean_t	nodea_interest;
487*1ae08745Sheppo 	boolean_t	nodeb_interest;
488*1ae08745Sheppo 	int		idx;
489*1ae08745Sheppo 
490*1ae08745Sheppo 	/* make sure we are starting at the beginning of the nodes */
491*1ae08745Sheppo 	if ((MDE_TAG(nodeap) != MDET_NODE) || (MDE_TAG(nodebp) != MDET_NODE))
492*1ae08745Sheppo 		return (MD_DIFF_NOMATCH);
493*1ae08745Sheppo 
494*1ae08745Sheppo 	for (idx = 0; match_elemsp[idx].type != MDET_LIST_END; idx++) {
495*1ae08745Sheppo 
496*1ae08745Sheppo 		int type;
497*1ae08745Sheppo 
498*1ae08745Sheppo 		nodea_interest = B_FALSE;
499*1ae08745Sheppo 		nodeb_interest = B_FALSE;
500*1ae08745Sheppo 
501*1ae08745Sheppo 		type = match_elemsp[idx].type;
502*1ae08745Sheppo 
503*1ae08745Sheppo 		/*
504*1ae08745Sheppo 		 * Check node A for the property of interest
505*1ae08745Sheppo 		 */
506*1ae08745Sheppo 		for (ap = nodeap; MDE_TAG(ap) != MDET_NODE_END; ap++) {
507*1ae08745Sheppo 			char *elemname;
508*1ae08745Sheppo 
509*1ae08745Sheppo 			if (MDE_TAG(ap) != type)
510*1ae08745Sheppo 				continue;
511*1ae08745Sheppo 
512*1ae08745Sheppo 			elemname = mdap->namep + MDE_NAME(ap);
513*1ae08745Sheppo 
514*1ae08745Sheppo 			if (strcmp(elemname, match_elemsp[idx].namep) == 0) {
515*1ae08745Sheppo 				/* found the property of interest */
516*1ae08745Sheppo 				nodea_interest = B_TRUE;
517*1ae08745Sheppo 				break;
518*1ae08745Sheppo 			}
519*1ae08745Sheppo 		}
520*1ae08745Sheppo 
521*1ae08745Sheppo 		/* node A is not of interest */
522*1ae08745Sheppo 		if (!nodea_interest)
523*1ae08745Sheppo 			return (MD_DIFF_NOMATCH);
524*1ae08745Sheppo 
525*1ae08745Sheppo 		/*
526*1ae08745Sheppo 		 * Check node B for the property of interest
527*1ae08745Sheppo 		 */
528*1ae08745Sheppo 		for (bp = nodebp; MDE_TAG(bp) != MDET_NODE_END; bp++) {
529*1ae08745Sheppo 			char *elemname;
530*1ae08745Sheppo 
531*1ae08745Sheppo 			if (MDE_TAG(bp) != type)
532*1ae08745Sheppo 				continue;
533*1ae08745Sheppo 
534*1ae08745Sheppo 			elemname = mdbp->namep + MDE_NAME(bp);
535*1ae08745Sheppo 
536*1ae08745Sheppo 			if (strcmp(elemname, match_elemsp[idx].namep) == 0) {
537*1ae08745Sheppo 				nodeb_interest = B_TRUE;
538*1ae08745Sheppo 				break;
539*1ae08745Sheppo 			}
540*1ae08745Sheppo 		}
541*1ae08745Sheppo 
542*1ae08745Sheppo 		/* node B is not of interest */
543*1ae08745Sheppo 		if (!nodeb_interest)
544*1ae08745Sheppo 			return (MD_DIFF_NOMATCH);
545*1ae08745Sheppo 
546*1ae08745Sheppo 		/*
547*1ae08745Sheppo 		 * Both nodes have the property of interest. The
548*1ae08745Sheppo 		 * nodes are not a match unless the value of that
549*1ae08745Sheppo 		 * property match
550*1ae08745Sheppo 		 */
551*1ae08745Sheppo 		switch (type) {
552*1ae08745Sheppo 		case MDET_PROP_VAL:
553*1ae08745Sheppo 			if (MDE_PROP_VALUE(ap) != MDE_PROP_VALUE(bp))
554*1ae08745Sheppo 				return (MD_DIFF_NOMATCH);
555*1ae08745Sheppo 			break;
556*1ae08745Sheppo 
557*1ae08745Sheppo 		case MDET_PROP_STR: {
558*1ae08745Sheppo 			char *stra = (char *)(mdap->datap +
559*1ae08745Sheppo 			    MDE_PROP_DATA_OFFSET(ap));
560*1ae08745Sheppo 			char *strb = (char *)(mdbp->datap +
561*1ae08745Sheppo 			    MDE_PROP_DATA_OFFSET(bp));
562*1ae08745Sheppo 
563*1ae08745Sheppo 			if (strcmp(stra, strb) != 0)
564*1ae08745Sheppo 				return (MD_DIFF_NOMATCH);
565*1ae08745Sheppo 			break;
566*1ae08745Sheppo 		}
567*1ae08745Sheppo 
568*1ae08745Sheppo 		case MDET_PROP_DAT: {
569*1ae08745Sheppo 
570*1ae08745Sheppo 			caddr_t dataa;
571*1ae08745Sheppo 			caddr_t datab;
572*1ae08745Sheppo 
573*1ae08745Sheppo 			if (MDE_PROP_DATA_LEN(ap) != MDE_PROP_DATA_LEN(bp))
574*1ae08745Sheppo 				return (MD_DIFF_NOMATCH);
575*1ae08745Sheppo 
576*1ae08745Sheppo 			dataa = (caddr_t)(mdap->datap +
577*1ae08745Sheppo 			    MDE_PROP_DATA_OFFSET(ap));
578*1ae08745Sheppo 			datab = (caddr_t)(mdbp->datap +
579*1ae08745Sheppo 			    MDE_PROP_DATA_OFFSET(bp));
580*1ae08745Sheppo 
581*1ae08745Sheppo 			if (memcmp(dataa, datab, MDE_PROP_DATA_LEN(ap)) != 0)
582*1ae08745Sheppo 				return (MD_DIFF_NOMATCH);
583*1ae08745Sheppo 
584*1ae08745Sheppo 			break;
585*1ae08745Sheppo 		}
586*1ae08745Sheppo 
587*1ae08745Sheppo 		default:
588*1ae08745Sheppo 			/* unsupported prop type */
589*1ae08745Sheppo 			return (MD_DIFF_NOMATCH);
590*1ae08745Sheppo 		}
591*1ae08745Sheppo 	}
592*1ae08745Sheppo 
593*1ae08745Sheppo 	/*
594*1ae08745Sheppo 	 * All the specified properties exist in both
595*1ae08745Sheppo 	 * nodes and have the same value. The two nodes
596*1ae08745Sheppo 	 * match.
597*1ae08745Sheppo 	 */
598*1ae08745Sheppo 
599*1ae08745Sheppo 	return (MD_DIFF_MATCH);
600*1ae08745Sheppo }
601