summaryrefslogtreecommitdiff
path: root/usr/src/lib/lvm/libsvm/common/metainterfaces.c
blob: b33ab778f182166d4c35553e78c68bf7a7993ae7 (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
/*
 * 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 2006 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

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

#include <stdio.h>
#include <string.h>
#include <sys/vfstab.h>
#include <meta.h>
#include <libsvm.h>
#include <svm.h>
#include <sdssc.h>


extern int mod_unload(char *modname);
static int inited = 0;

/*
 * FUNCTION: init_metalib
 *	initialize libmeta only once.
 *
 * RETURN VALUES:
 *	0 - SUCCESS
 *     -1 - FAIL
 */

static int
init_metalib()
{
	int largc = 1;
	char *largv = "libsvm";
	md_error_t status = mdnullerror;

	if (!inited) {
		if (md_init_nosig(largc, &largv, 0, 1, &status) != 0 ||
				meta_check_root(&status) != 0) {
			return (-1);
		}
		inited = 1;
	}
	return (RET_SUCCESS);
}

/*
 * FUNCTION: reset_metalib
 *
 * INPUT: ptr to md_error_t
 */

static void
reset_metalib(md_error_t *ep)
{
	inited = 0;
	(void) close_admin(ep);
}

/*
 * FUNCTION: metahalt
 *	halt the metadb
 *
 */

static void
metahalt()
{
	mdsetname_t	*sp;
	md_error_t status = mdnullerror;

	(void) init_metalib();
	if ((sp = metasetname(MD_LOCAL_NAME, &status)) == NULL) {
		return;
	}
	if (meta_lock(sp, TRUE, &status)) {
		return;
	}
	if (metaioctl(MD_HALT, NULL, &status, NULL) != 0) {
		debug_printf("metahalt(): errno %d\n",
			status.info.md_error_info_t_u.sys_error.errnum);
	}
	(void) meta_unlock(sp, &status);
	reset_metalib(&status);
}

/*
 * FUNCTION: svm_stop
 *	Halt the SDS/SVM configuration and unload md module.
 *
 * RETURN VALUES:
 *	0 - SUCCESS
 *	RET_ERROR
 */

#define	MAX_TIMEOUT 1800
int
svm_stop()
{
	int rval = RET_SUCCESS;
	int timeval = 0;
	int sleep_int = 5;

	metahalt();

	if ((rval = mod_unload(MD_MODULE)) != 0) {
		timeval += sleep_int;
		(void) sleep(sleep_int);
		while (timeval < MAX_TIMEOUT) {
			if ((rval = mod_unload(MD_MODULE)) == 0) {
				debug_printf("svm_stop(): mod_unload succeeded."
						" Time %d\n", timeval);

				break;
			}

			debug_printf("svm_stop(): mod_unload failed. Trying "
				"in  %d s (%d)\n", sleep_int, timeval);

			timeval += sleep_int;
			(void) sleep(sleep_int);
			metahalt();
		}

		if (rval != 0) {
			rval = RET_ERROR;
			debug_printf("svm_stop(): mod_unload FAILED!\n");
		}
	}

	return (rval);
}

/*
 * FUNCTION: get_rootmetadevice
 *	parses the vfstab to return the metadevice
 *
 * INPUT:
 *	mount point
 *	mdname	- pointer to string pointer that will contain the
 *		  metadevice name. Caller must free the allocated space.
 * RETURN VALUES:
 *	mdname - md root device name
 *	0 - SUCCESS
 *	!0 - FAIL
 *		> 0 errno
 *		RET_ERROR
 */

int
get_rootmetadevice(char *mntpath, char **mdname)
{
	struct	vfstab v;
	FILE	*fp;
	int	rval = RET_SUCCESS;
	char	*cp;
	char	vfstab_name[PATH_MAX + 1];

	if (mdname == NULL)
		return (EINVAL);

	*mdname = NULL;

	if (snprintf(vfstab_name, PATH_MAX + 1, "%s%s", mntpath, VFSTAB) < 0)
		return (ENOMEM);

	debug_printf("get_rootmetadevice(): mntpath %s %s\n", mntpath,
		vfstab_name);

	if ((fp = fopen(vfstab_name, "r")) == NULL) {
		rval = errno;
		return (rval);
	}

	if ((rval = getvfsfile(fp, &v, ROOT_MNTPT)) != 0) {
		goto out;
	}


	debug_printf("get_rootmetadevice(): vfs_special %s\n", v.vfs_special);
	if (strstr(v.vfs_special, ROOT_METADEVICE) == NULL) {
		/* md device not found */
		rval = RET_ERROR;
		goto out;
	}

	/* found a match fill it and return */
	cp = v.vfs_special + strlen(ROOT_METADEVICE);

	*mdname = (char *)malloc(strlen(cp) + 1);

	if (*mdname == NULL) {
		rval = ENOMEM;
		goto out;
	}
	(void) strcpy(*mdname, cp);
	debug_printf("get_rootmetadevice(): *mdname %s rval %d\n",
							*mdname, rval);
out:
	(void) fclose(fp);
	return (rval);
}

/*
 * FUNCTION: create_diskset_links
 * 	Create the diskset name symlinks in /dev/md from the diskset
 *	names found in the set records.  These are normally created
 *	in rpc.metad when you create the set but those symlinks are
 *	sitting out on the real system disk and we're running off the
 *	devfs that got created when we booted off the install image.
 */

void
create_diskset_links()
{
	int		max_sets;
	int		i;
	md_error_t	error = mdnullerror;

	/*
	 * Resolve the function pointers for libsds_sc so that we can
	 * snarf the set records.
	 */
	(void) sdssc_bind_library();
	(void) init_metalib();

	if ((max_sets = get_max_sets(&error)) == 0) {
		debug_printf("create_diskset_links(): get_max_sets failed\n");
		mdclrerror(&error);
		return;
	}

	for (i = 1; i < max_sets; i++) {
		md_set_record	*sr;
		char		setname[MAXPATHLEN];
		char		setnum[MAXPATHLEN];

		if ((sr = metad_getsetbynum(i, &error)) == NULL) {
			mdclrerror(&error);
			continue;
		}

		(void) snprintf(setname, MAXPATHLEN, "/dev/md/%s",
		    sr->sr_setname);
		(void) snprintf(setnum, MAXPATHLEN, "shared/%d", i);
		/*
		 * Ignore failures to create the symlink.  This could
		 * happen because suninstall is restartable so the
		 * symlink might have already been created.
		 */
		(void) symlink(setnum, setname);
	}
}

/*
 * FUNCTION: svm_alloc
 * 	Return a pointer to an opaque piece of zeroed memory.
 *
 * RETURN VALUES:
 *	Non null - SUCCESS
 *	NULL - FAIL
 */

svm_info_t *
svm_alloc()
{
	return ((svm_info_t *)calloc(1, sizeof (svm_info_t)));
}

/*
 * FUNCTION: svm_free
 *
 * INPUT: pointer to struct svm_info
 */

void
svm_free(svm_info_t *svmp)
{
	int i;

	if (svmp == NULL)
		return;

	for (i = 0; i < svmp->count; i++) {
		free(svmp->md_comps[i]);
	}
	free(svmp->root_md);
	free(svmp);
}

/*
 * FUNCTION: get_mdcomponents
 *	Given "uname" metadevice, return the physical components
 *      of that metadevice.
 *
 * INPUT:
 *	uname - metadevice name
 *
 * RETURN VALUES:
 *	svmp - structure containing md name and components
 *	RET_SUCCESS
 *	RET_ERROR
 *
 */

int
get_mdcomponents(char *uname, svm_info_t **svmpp)
{

	svm_info_t	*svmp;
	md_error_t	status, *ep;
	mdname_t	*namep;
	mdnamelist_t	*nlp = NULL;
	mdnamelist_t	*p;
	mdsetname_t	*sp = NULL;
	char		*strp = NULL;
	int		rval, cnt;

	rval = RET_SUCCESS;
	cnt = 0;
	status = mdnullerror;
	ep = &status;
	svmp = *svmpp;

	(void) init_metalib();

	debug_printf("get_mdcomponents(): Enter unit name %s\n", uname);

	if (((namep = metaname(&sp, uname, META_DEVICE, ep)) == NULL) ||
					(metachkmeta(namep, ep) != 0)) {
		debug_printf("get_mdcomponents(): "
				"metaname or metachkmeta failed\n");
		mdclrerror(ep);
		return (RET_ERROR);
	}

	debug_printf("get_mdcomponents(): meta_getdevs %s\n", namep->cname);

	if ((meta_getdevs(sp, namep, &nlp, ep)) < 0) {
		debug_printf("get_mdcomponents(): "
				"comp %s - meta_getdevs failed\n", uname);
		metafreenamelist(nlp);
		mdclrerror(ep);
		return (RET_ERROR);
	}

	/* compute the number of devices */

	for (p = nlp, cnt = 0; p != NULL;  p = p->next, cnt++)
		;

	/*
	 * Need to add n -1 components since slvmp already has space
	 * for one device.
	 */

	svmp = (svm_info_t *)realloc(svmp, sizeof (svm_info_t) +
		(sizeof (char *) * (cnt - 1)));

	if (svmp == NULL) {
		debug_printf("get_mdcomponents(): realloc of svmp failed\n");
		metafreenamelist(nlp);
		return (RET_ERROR);
	}


	for (p = nlp, cnt = 0; p != NULL; p = p->next, cnt++) {
		mdname_t	*devnp = p->namep;

		if ((strp = strdup(devnp->cname)) == NULL) {
			rval = RET_ERROR;
			break;
		}
		svmp->md_comps[cnt] = strp;
	}

	/* count is set to the number of devices in the list */

	svmp->count = cnt;
	svmp->root_md = strdup(uname);
	if (rval == RET_SUCCESS && svmp->root_md != NULL) {
		debug_printf("get_mdcomponents(): root_md %s count %d \n",
			svmp->root_md, svmp->count);
		for (cnt = 0; cnt < svmp->count; cnt++)
			debug_printf("get_mdcomponents(): %s\n",
							svmp->md_comps[cnt]);
	} else {
		rval = RET_ERROR;
		svm_free(svmp);
		svmp = NULL;
		debug_printf("get_mdcomponents(): malloc failed\n");

	}


	metafreenamelist(nlp);
	*svmpp = svmp;
	return (rval);
}


/*
 * FUNCTION: svm_get_components
 *	return svm_infop with the components of a metadevice.
 *
 * INPUT:
 *	md_device - eg. /dev/md/dsk/d10, /dev/md/foo/dsk/d10, or
 *			/dev/md/shared/1/dsk/d10
 *
 * RETURN:
 *	0 - SUCCESS
 *     !0 - FAIL
 */

int
svm_get_components(char *md_device, svm_info_t **svmpp)
{
	int	len;

	/*
	 * If this is a named diskset with a shared name
	 * (e.g. /dev/md/shared/1/dsk/d10) call get_mdcomponents with
	 * the diskset and metadevice name (e.g. foo/d10).
	 * Otherwise this is a regular name (e.g. /dev/md/dsk/d10 or
	 * /dev/md/foo/dsk/d10 or d10 or foo/d10) all of which
	 * get_mdcomponents can handle directly.
	 */

	len = strlen("/dev/md/shared/");
	if (strncmp(md_device, "/dev/md/shared/", len) == 0) {
	    int		numlen;
	    int		setnum;
	    char	*cp;
	    char	*slashp;
	    char	mdname[MAXPATHLEN];
	    mdsetname_t	*sp;
	    md_error_t	error = mdnullerror;

	    cp = md_device + len;

	    if ((slashp = strstr(cp, "/")) == NULL)
		return (RET_ERROR);
	    numlen = slashp - cp;
	    if (numlen >= MAXPATHLEN - 1)
		return (RET_ERROR);

	    (void) strlcpy(mdname, cp, numlen + 1);
	    /* setnum now contains the diskset number */
	    setnum = atoi(mdname);
	    if ((sp = metasetnosetname(setnum, &error)) == NULL ||
		!mdisok(&error))
		return (RET_ERROR);

	    cp = slashp + 1;
	    /* cp now pointing at dsk/... */
	    if ((slashp = strstr(cp, "/")) == NULL)
		return (RET_ERROR);

	    (void) snprintf(mdname, MAXPATHLEN, "%s/%s", sp->setname,
		slashp + 1);
	    /* mdname now contains diskset and metadevice name e.g. foo/d10 */

	    debug_printf("svm_get_components(): mdname %s\n", mdname);
	    return (get_mdcomponents(mdname, svmpp));

	} else {
	    debug_printf("svm_get_components(): md_device %s\n", md_device);
	    return (get_mdcomponents(md_device, svmpp));
	}
}