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
|
/*
* 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"
/*
* initialize metadevices
*/
#include <meta.h>
#include <sys/lvm/mdio.h>
#include <libdevinfo.h>
int
parse_interlace(
char *uname, /* Meta Device name (eg d0) */
char *str, /* String to Parse */
diskaddr_t *interlacep,
md_error_t *ep
)
{
diskaddr_t num;
char c;
int cnt;
/* parse interlace */
if ((cnt = sscanf(str, "%llu%c", &num, &c)) < 1) {
return (meta_cook_syntax(ep, MDE_BAD_INTERLACE,
uname, 1, &str));
} else if (cnt == 1) {
if (num & (DEV_BSIZE - 1)) {
return (meta_cook_syntax(ep, MDE_BAD_INTERLACE,
uname, 1, &str));
}
num = lbtodb(num);
} else switch (c) {
case 'b':
case 'B':
num *= DEV_BSIZE / DEV_BSIZE;
break;
case 'k':
case 'K':
num *= 1024 / DEV_BSIZE;
break;
case 'm':
case 'M':
num *= 1024 * 1024 / DEV_BSIZE;
break;
default:
return (meta_cook_syntax(ep, MDE_BAD_INTERLACE,
NULL, 1, &str));
}
/* return success */
*interlacep = num;
return (0);
}
/*
* cook up syntax error
*/
int
meta_cook_syntax(
md_error_t *ep,
md_void_errno_t errcode,
char *uname,
int argc,
char *argv[]
)
{
int rval;
/* if we have a token, concat it to uname */
if ((argc > 0) && (argv[0] != NULL) && (argv[0][0] != '\0')) {
char *p;
if ((uname != NULL) && (uname[0] != '\0')) {
p = Malloc(strlen(uname) + 2
+ 1 + strlen(argv[0]) + 1 + 1);
(void) strcpy(p, uname);
(void) strcat(p, ": ");
} else {
p = Malloc(1 + strlen(argv[0]) + 1 + 1);
p[0] = '\0';
}
(void) strcat(p, "\"");
(void) strcat(p, argv[0]);
(void) strcat(p, "\"");
rval = mderror(ep, errcode, p);
Free(p);
} else {
rval = mderror(ep, errcode, uname);
}
return (rval);
}
int
meta_check_devicesize(
diskaddr_t total_blocks
)
{
int rval = MD_CRO_32BIT;
if (total_blocks > MD_MAX_BLKS_FOR_SMALL_DEVS) {
rval = MD_CRO_64BIT;
}
return (rval);
}
/*
* setup metadevice geometry
*/
/*ARGSUSED*/
int
meta_setup_geom(
md_unit_t *md,
mdname_t *np,
mdgeom_t *geomp,
uint_t write_reinstruct,
uint_t read_reinstruct,
uint_t round_cyl,
md_error_t *ep
)
{
diskaddr_t cylsize = geomp->nhead * geomp->nsect;
diskaddr_t total_blocks;
if (round_cyl) {
total_blocks = rounddown(md->c.un_actual_tb, cylsize);
} else {
total_blocks = md->c.un_actual_tb;
}
md->c.un_total_blocks = total_blocks;
md->c.un_nhead = geomp->nhead;
md->c.un_nsect = geomp->nsect;
md->c.un_rpm = geomp->rpm;
md->c.un_wr_reinstruct = write_reinstruct;
md->c.un_rd_reinstruct = read_reinstruct;
return (0);
}
/*
* adjust metadevice geometry
*/
/*ARGSUSED*/
int
meta_adjust_geom(
md_unit_t *md,
mdname_t *np,
uint_t write_reinstruct,
uint_t read_reinstruct,
uint_t round_cyl,
md_error_t *ep
)
{
diskaddr_t cylsize = md->c.un_nhead * md->c.un_nsect;
diskaddr_t total_blocks;
if (round_cyl) {
total_blocks = rounddown(md->c.un_actual_tb, cylsize);
} else {
total_blocks = md->c.un_actual_tb;
}
md->c.un_total_blocks = total_blocks;
if (write_reinstruct > md->c.un_wr_reinstruct)
md->c.un_wr_reinstruct = write_reinstruct;
if (read_reinstruct > md->c.un_rd_reinstruct)
md->c.un_rd_reinstruct = read_reinstruct;
return (0);
}
/*
* Function: meta_init_make_device
* Purpose:
* Create the device node <uname> by constructing the necessary
* md_mkdev_params_t structure. We have to handle relative names
* (e.g. "d80") and fully-qualified names (e.g. "/dev/md/red/dsk/d80").
* The field that we need is the unit number of the metadevice (80 in
* the above examples).
* Input: spp set structure
* uname unit-name (fully qualified or relative)
* Output: ep error return structure
* Returns: > 0 success and return 'key'
* -1 Error. <ep> contains error reason
*/
mdkey_t
meta_init_make_device(
mdsetname_t **spp,
char *uname,
md_error_t *ep
)
{
md_mkdev_params_t params;
mdkey_t rval = 0;
char *p;
int len = strlen(uname);
(void) memset(¶ms, 0, sizeof (params));
MD_SETDRIVERNAME(¶ms, "md", (*spp)->setno);
/*
* This ioctl call causes kernel to allocate a unit number
* and populate /devices for the named metadevice
*/
if (metaioctl(MD_IOCMAKE_DEV, ¶ms, ¶ms.mde, NULL) != 0) {
return (mdstealerror(ep, ¶ms.mde));
}
/*
* Now we have minor number so add it to the namespace
* and return the key
*/
if ((rval = add_self_name(*spp, uname, ¶ms, ep)) <= 0) {
if (mdisok(ep))
(void) mderror(ep, MDE_UNIT_NOT_FOUND, NULL);
return (-1);
}
/* Make sure the /dev link is created */
if (meta_update_devtree(MD_MKMIN((*spp)->setno, params.un)) != 0) {
/*
* Delete name entry we just created
*/
(void) del_self_name(*spp, rval, ep);
p = Malloc(len + 3);
(void) snprintf(p, len + 3, "\"%s\"", uname);
rval = mderror(ep, MDE_UNIT_NOT_FOUND, p);
Free(p);
}
return (rval);
}
/*
* FUNCTION: is_metadb_cmd()
* INPUT: argc - number of command line arguments
* argv - pointer to array of command line arguments
* OUTPUT: none
* RETURNS: TRUE if a metadb is to be created, FALSE otherwise
* PURPOSE: parses enough of the command line to determine if a metadb
* create is being attempted
*/
static boolean_t
is_metadb_cmd(
int argc,
char *argv[]
)
{
ulong_t num;
int len;
/* look for match */
if (argc > 0 && (sscanf(argv[0], "mddb%lu%n", &num, &len) == 1) &&
(strlen(argv[0]) == len) && ((long)num >= 0)) {
return (B_TRUE);
}
return (B_FALSE);
}
/*
* FUNCTION: is_stripe_cmd()
* INPUT: argc - number of command line arguments
* argv - pointer to array of command line arguments
* OUTPUT: none
* RETURNS: TRUE if a stripe is to be created, FALSE otherwise
* PURPOSE: parses enough of the command line to determine if a stripe
* create is being attempted
*/
static boolean_t
is_stripe_cmd(
int argc,
char *argv[]
)
{
uint_t nrow;
if (argc > 1 && (sscanf(argv[1], "%u", &nrow) != 1) || ((int)nrow < 0))
return (B_FALSE);
return (B_TRUE);
}
/*
* FUNCTION: meta_get_init_type()
* INPUT: argc - number of command line arguments
* argv - pointer to array of command line arguments
* OUTPUT: none
* RETURNS: type of metadevice or hot spare pools being initialized
* PURPOSE: parses enough of the command line to determine what type
* of metainit is being attempted
*/
mdinittypes_t
meta_get_init_type(
int argc,
char *argv[]
)
{
char *arg = argv[1];
mdinittypes_t init_type;
if (argc == 1) /* must be a hot spare pool w/o devices */
return (TAB_HSP);
init_type = TAB_UNKNOWN;
if (arg != NULL) {
if (strcmp(arg, "-m") == 0) {
init_type = TAB_MIRROR;
} else if (strcmp(arg, "-r") == 0) {
init_type = TAB_RAID;
} else if (strcmp(arg, "-p") == 0) {
init_type = TAB_SP;
} else if (strcmp(arg, "-t") == 0) {
init_type = TAB_TRANS;
} else if (is_metadb_cmd(argc, argv)) {
init_type = TAB_MDDB;
} else if (is_stripe_cmd(argc, argv)) {
init_type = TAB_STRIPE;
} else { /* assume that it is a hsp */
init_type = TAB_HSP;
}
}
return (init_type);
}
/*
* initialize named device or hotspare pool
*/
int
meta_init_name(
mdsetname_t **spp,
int argc,
char *argv[],
char *cname, /* canonical name */
mdcmdopts_t options,
md_error_t *ep
)
{
mdinittypes_t init_type;
char *p;
int rval;
char *uname = argv[0];
mdkey_t key = MD_KEYWILD;
minor_t mnum;
md_error_t t_e = mdnullerror;
assert(argc > 0);
assert(*spp != NULL);
/* determine type of metadevice or hot spare pool being created */
init_type = meta_get_init_type(argc, argv);
/*
* Metatrans is eof
*/
if (init_type == TAB_TRANS)
return (mderror(ep, MDE_EOF_TRANS, NULL));
/* hotspare pool */
if (init_type == TAB_HSP)
return (meta_init_hsp(spp, argc, argv, options, ep));
/*
* We are creating metadevice so make sure the name
* has not been used
*/
if (is_existing_meta_hsp(*spp, cname)) {
/*
* The name has been used by hsp
*/
if (is_existing_hsp(*spp, cname)) {
return (mderror(ep, MDE_NAME_IN_USE, cname));
}
/*
* If path exists but unit is not created
* then meta_init_make_device will correct
* that. If unit also exists then it
* will return a conflict error
*/
if (init_type != TAB_UNKNOWN) {
/* Create device node */
if ((key = meta_init_make_device(spp, uname,
&t_e)) <= 0) {
return (mdstealerror(ep, &t_e));
}
}
}
/* metadevice */
if (argc >= 2 && init_type != TAB_UNKNOWN) {
/*
* We need to create the device node if the specified metadevice
* does not already exist in the database. The actual creation
* is undertaken by the md driver and the links propagated by
* devfsadm.
*/
if (key == MD_KEYWILD) {
if ((key = meta_init_make_device(spp, uname,
&t_e)) <= 0)
return (mdstealerror(ep, &t_e));
}
switch (init_type) {
case TAB_MIRROR:
rval = meta_init_mirror(spp, argc, argv, options, ep);
break;
case TAB_RAID:
rval = meta_init_raid(spp, argc, argv, options, ep);
break;
case TAB_SP:
rval = meta_init_sp(spp, argc, argv, options, ep);
break;
case TAB_STRIPE:
rval = meta_init_stripe(spp, argc, argv, options, ep);
break;
}
if (rval == -1 || !(options & MDCMD_DOIT)) {
/*
* Remove the device node created before
*/
if ((meta_getnmentbykey((*spp)->setno, MD_SIDEWILD,
key, NULL, &mnum, NULL, ep) != NULL) &&
MD_MIN2UNIT(mnum) < MD_MAXUNITS) {
(void) metaioctl(MD_IOCREM_DEV, &mnum, &t_e, NULL);
}
/*
* Del what we added before
*/
(void) del_self_name(*spp, key, &t_e);
}
return (rval);
}
/* unknown type */
p = Malloc(1 + strlen(uname) + 1 + 1);
(void) strcpy(p, "\"");
(void) strcat(p, uname);
(void) strcat(p, "\"");
rval = mderror(ep, MDE_SYNTAX, p);
Free(p);
return (rval);
}
|