kernel: Remove some unused variables in RAID and disk drivers.
[dragonfly.git] / sys / dev / raid / vinum / vinumio.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1997, 1998
3 * Nan Yang Computer Services Limited. All rights reserved.
4 *
5 * This software is distributed under the so-called ``Berkeley
6 * License'':
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by Nan Yang Computer
19 * Services Limited.
20 * 4. Neither the name of the Company nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
23 *
24 * This software is provided ``as is'', and any express or implied
25 * warranties, including, but not limited to, the implied warranties of
26 * merchantability and fitness for a particular purpose are disclaimed.
27 * In no event shall the company or contributors be liable for any
28 * direct, indirect, incidental, special, exemplary, or consequential
29 * damages (including, but not limited to, procurement of substitute
30 * goods or services; loss of use, data, or profits; or business
31 * interruption) however caused and on any theory of liability, whether
32 * in contract, strict liability, or tort (including negligence or
33 * otherwise) arising in any way out of the use of this software, even if
34 * advised of the possibility of such damage.
35 *
36 * $Id: vinumio.c,v 1.30 2000/05/10 23:23:30 grog Exp grog $
37 * $FreeBSD: src/sys/dev/vinum/vinumio.c,v 1.52.2.6 2002/05/02 08:43:44 grog Exp $
38 */
39
1f2de5d4
MD
40#include "vinumhdr.h"
41#include "request.h"
984263bc 42#include <vm/vm_zone.h>
c1a85d27 43#include <sys/nlookup.h>
984263bc
MD
44
45static char *sappend(char *txt, char *s);
46static int drivecmp(const void *va, const void *vb);
47
48/*
49 * Open the device associated with the drive, and set drive's vp.
50 * Return an error number
51 */
52int
53open_drive(struct drive *drive, struct proc *p, int verbose)
54{
c1a85d27
MD
55 struct nlookupdata nd;
56 int error;
984263bc
MD
57
58 /*
c1a85d27 59 * Fail if already open
984263bc 60 */
c1a85d27
MD
61 if (drive->flags & VF_OPEN)
62 return EBUSY;
984263bc 63
c1a85d27
MD
64 if (rootdev) {
65 /*
66 * Open via filesystem (future)
67 */
68 error = nlookup_init(&nd, drive->devicename, UIO_SYSSPACE, NLC_FOLLOW);
69 if (error)
70 return error;
71 error = vn_open(&nd, NULL, FREAD|FWRITE, 0);
72 drive->vp = nd.nl_open_vp;
73 nd.nl_open_vp = NULL;
74 nlookup_done(&nd);
75 } else {
76 /*
77 * Open via synthesized vnode backed by disk device
78 */
79 error = vn_opendisk(drive->devicename, FREAD|FWRITE, &drive->vp);
80 if (error)
81 return error;
82 }
2f4ec5c1 83
c1a85d27
MD
84 if (error == 0 && drive->vp == NULL)
85 error = ENODEV;
984263bc 86
83d36d43 87 /*
c1a85d27
MD
88 * A huge amount of pollution all over vinum requires that our low
89 * level drive be a device.
83d36d43 90 */
c1a85d27
MD
91 if (error == 0 && drive->vp->v_type != VCHR) {
92 vn_close(drive->vp, FREAD|FWRITE);
93 drive->vp = NULL;
94 error = ENODEV;
95 }
96 if (error) {
97 drive->state = drive_down;
98 if (verbose) {
984263bc
MD
99 log(LOG_WARNING,
100 "vinum open_drive %s: failed with error %d\n",
c1a85d27
MD
101 drive->devicename, error);
102 }
103 } else {
104 drive->dev = drive->vp->v_rdev;
105 drive->flags |= VF_OPEN;
106 }
107 drive->lasterror = error;
108 return error;
984263bc
MD
109}
110
111/*
112 * Set some variables in the drive struct
113 * in more convenient form. Return error indication
114 */
115int
116set_drive_parms(struct drive *drive)
117{
118 drive->blocksize = BLKDEV_IOSIZE; /* do we need this? */
119 drive->secsperblock = drive->blocksize /* number of sectors per block */
2ec8fb79 120 / drive->partinfo.media_blksize;
984263bc
MD
121
122 /* Now update the label part */
123 bcopy(hostname, drive->label.sysname, VINUMHOSTNAMELEN); /* put in host name */
124 getmicrotime(&drive->label.date_of_birth); /* and current time */
2ec8fb79 125 drive->label.drive_size = drive->partinfo.media_size;
984263bc
MD
126#if VINUMDEBUG
127 if (debug & DEBUG_BIGDRIVE) /* pretend we're 100 times as big */
128 drive->label.drive_size *= 100;
129#endif
130
131 /* number of sectors available for subdisks */
132 drive->sectors_available = drive->label.drive_size / DEV_BSIZE - DATASTART;
133
134 /*
135 * Bug in 3.0 as of January 1998: you can open
136 * non-existent slices. They have a length of 0.
137 */
138 if (drive->label.drive_size < MINVINUMSLICE) { /* too small to worry about */
139 set_drive_state(drive->driveno, drive_down, setstate_force);
140 drive->lasterror = ENOSPC;
141 return ENOSPC;
142 }
143 drive->freelist_size = INITIAL_DRIVE_FREELIST; /* initial number of entries */
144 drive->freelist = (struct drive_freelist *)
145 Malloc(INITIAL_DRIVE_FREELIST * sizeof(struct drive_freelist));
146 if (drive->freelist == NULL) /* can't malloc, dammit */
147 return ENOSPC;
148 drive->freelist_entries = 1; /* just (almost) the complete drive */
149 drive->freelist[0].offset = DATASTART; /* starts here */
150 drive->freelist[0].sectors = (drive->label.drive_size >> DEV_BSHIFT) - DATASTART; /* and it's this long */
151 if (drive->label.name[0] != '\0') /* got a name */
152 set_drive_state(drive->driveno, drive_up, setstate_force); /* our drive is accessible */
153 else /* we know about it, but that's all */
154 drive->state = drive_referenced;
155 return 0;
156}
157
158/*
159 * Initialize a drive: open the device and add device
160 * information
161 */
162int
163init_drive(struct drive *drive, int verbose)
164{
165 if (drive->devicename[0] != '/') {
166 drive->lasterror = EINVAL;
5ad175c9
MD
167 log(LOG_ERR, "vinum: Can't open drive without drive name (%s)\n",
168 drive->devicename);
984263bc
MD
169 return EINVAL;
170 }
171 drive->lasterror = open_drive(drive, curproc, verbose); /* open the drive */
172 if (drive->lasterror)
173 return drive->lasterror;
174
c1a85d27 175 drive->lasterror = VOP_IOCTL(drive->vp, DIOCGPART,
87baaf0c
MD
176 (caddr_t)&drive->partinfo, FREAD|FWRITE,
177 proc0.p_ucred, NULL);
984263bc
MD
178 if (drive->lasterror) {
179 if (verbose)
180 log(LOG_WARNING,
181 "vinum open_drive %s: Can't get partition information, drive->lasterror %d\n",
182 drive->devicename,
183 drive->lasterror);
184 close_drive(drive);
185 return drive->lasterror;
186 }
2ec8fb79 187 if (drive->partinfo.fstype != FS_VINUM &&
18cb7add 188 !kuuid_is_vinum(&drive->partinfo.fstype_uuid)
2ec8fb79 189 ) {
984263bc
MD
190 drive->lasterror = EFTYPE;
191 if (verbose)
192 log(LOG_WARNING,
193 "vinum open_drive %s: Wrong partition type for vinum\n",
194 drive->devicename);
195 close_drive(drive);
196 return EFTYPE;
197 }
198 return set_drive_parms(drive); /* set various odds and ends */
199}
200
201/* Close a drive if it's open. */
202void
203close_drive(struct drive *drive)
204{
205 LOCKDRIVE(drive); /* keep the daemon out */
206 if (drive->flags & VF_OPEN)
207 close_locked_drive(drive); /* and close it */
208 if (drive->state > drive_down) /* if it's up */
209 drive->state = drive_down; /* make sure it's down */
210 unlockdrive(drive);
211}
212
213/*
214 * Real drive close code, called with drive already locked.
215 * We have also checked that the drive is open. No errors.
216 */
217void
218close_locked_drive(struct drive *drive)
219{
220 /*
221 * If we can't access the drive, we can't flush
222 * the queues, which spec_close() will try to
223 * do. Get rid of them here first.
224 */
c1a85d27
MD
225 if (drive->vp) {
226 drive->lasterror = vn_close(drive->vp, FREAD|FWRITE);
227 drive->vp = NULL;
228 }
229 drive->flags &= ~VF_OPEN;
984263bc
MD
230}
231
232/*
233 * Remove drive from the configuration.
234 * Caller must ensure that it isn't active.
235 */
236void
237remove_drive(int driveno)
238{
239 struct drive *drive = &vinum_conf.drive[driveno];
240 struct vinum_hdr *vhdr; /* buffer for header */
241 int error;
242
243 if (drive->state > drive_referenced) { /* real drive */
244 if (drive->state == drive_up) {
245 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffer */
246 CHECKALLOC(vhdr, "Can't allocate memory");
247 error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
248 if (error)
249 drive->lasterror = error;
250 else {
251 vhdr->magic = VINUM_NOMAGIC; /* obliterate the magic, but leave the rest */
252 write_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
253 }
254 Free(vhdr);
255 }
256 free_drive(drive); /* close it and free resources */
257 save_config(); /* and save the updated configuration */
258 }
259}
260
261/*
262 * Transfer drive data. Usually called from one of these defines;
10f3fee5
MD
263 * #define read_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_READ)
264 * #define write_drive(a, b, c, d) driveio (a, b, c, d, BUF_CMD_WRITE)
984263bc
MD
265 *
266 * length and offset are in bytes, but must be multiples of sector
267 * size. The function *does not check* for this condition, and
268 * truncates ruthlessly.
269 * Return error number
270 */
271int
10f3fee5 272driveio(struct drive *drive, char *buf, size_t length, off_t offset, buf_cmd_t cmd)
984263bc
MD
273{
274 int error;
275 struct buf *bp;
3591bbc6 276 caddr_t saveaddr;
984263bc
MD
277
278 error = 0; /* to keep the compiler happy */
279 while (length) { /* divide into small enough blocks */
0d0607d0 280 int len = umin(length, MAXBSIZE); /* maximum block device transfer is MAXBSIZE */
984263bc
MD
281
282 bp = geteblk(len); /* get a buffer header */
10f3fee5 283 bp->b_cmd = cmd;
54078292 284 bp->b_bio1.bio_offset = offset; /* disk offset */
ae8e83e6
MD
285 bp->b_bio1.bio_done = biodone_sync;
286 bp->b_bio1.bio_flags |= BIO_SYNC;
3591bbc6 287 saveaddr = bp->b_data;
984263bc
MD
288 bp->b_data = buf;
289 bp->b_bcount = len;
c1a85d27 290 vn_strategy(drive->vp, &bp->b_bio1);
ae8e83e6 291 error = biowait(&bp->b_bio1, (cmd == BUF_CMD_READ ? "drvrd" : "drvwr"));
3591bbc6 292 bp->b_data = saveaddr;
984263bc
MD
293 bp->b_flags |= B_INVAL | B_AGE;
294 bp->b_flags &= ~B_ERROR;
295 brelse(bp);
296 if (error)
297 break;
298 length -= len; /* update pointers */
299 buf += len;
300 offset += len;
301 }
302 return error;
303}
304
305/*
306 * Check a drive for a vinum header. If found,
307 * update the drive information. We come here
308 * with a partially populated drive structure
309 * which includes the device name.
310 *
311 * Return information on what we found.
312 *
313 * This function is called from two places: check_drive,
314 * which wants to find out whether the drive is a
315 * Vinum drive, and config_drive, which asserts that
316 * it is a vinum drive. In the first case, we don't
317 * print error messages (verbose==0), in the second
318 * we do (verbose==1).
319 */
320enum drive_label_info
321read_drive_label(struct drive *drive, int verbose)
322{
323 int error;
5ad175c9
MD
324 int result;
325 struct vinum_hdr *vhdr;
984263bc 326
d4845858 327 error = init_drive(drive, verbose); /* find the drive */
984263bc
MD
328 if (error) /* find the drive */
329 return DL_CANT_OPEN; /* not ours */
330
331 vhdr = (struct vinum_hdr *) Malloc(VINUMHEADERLEN); /* allocate buffers */
332 CHECKALLOC(vhdr, "Can't allocate memory");
333
334 drive->state = drive_up; /* be optimistic */
335 error = read_drive(drive, (void *) vhdr, VINUMHEADERLEN, VINUM_LABEL_OFFSET);
336 if (vhdr->magic == VINUM_MAGIC) { /* ours! */
337 if (drive->label.name[0] /* we have a name for this drive */
338 &&(strcmp(drive->label.name, vhdr->label.name))) { /* but it doesn't match the real name */
339 drive->lasterror = EINVAL;
340 result = DL_WRONG_DRIVE; /* it's the wrong drive */
341 drive->state = drive_unallocated; /* put it back, it's not ours */
342 } else
343 result = DL_OURS;
344 /*
345 * We copy the drive anyway so that we have
346 * the correct name in the drive info. This
347 * may not be the name specified
348 */
349 drive->label = vhdr->label; /* put in the label information */
350 } else if (vhdr->magic == VINUM_NOMAGIC) /* was ours, but we gave it away */
351 result = DL_DELETED_LABEL; /* and return the info */
352 else
353 result = DL_NOT_OURS; /* we could have it, but we don't yet */
354 Free(vhdr); /* that's all. */
355 return result;
356}
357
358/*
359 * Check a drive for a vinum header. If found,
360 * read configuration information from the drive and
361 * incorporate the data into the configuration.
362 *
363 * Return drive number.
364 */
365struct drive *
366check_drive(char *devicename)
367{
368 int driveno;
369 int i;
370 struct drive *drive;
371
372 driveno = find_drive_by_dev(devicename, 1); /* if entry doesn't exist, create it */
373 drive = &vinum_conf.drive[driveno]; /* and get a pointer */
374
375 if (read_drive_label(drive, 0) == DL_OURS) { /* one of ours */
376 for (i = 0; i < vinum_conf.drives_allocated; i++) { /* see if the name already exists */
377 if ((i != driveno) /* not this drive */
378 &&(DRIVE[i].state != drive_unallocated) /* and it's allocated */
379 &&(strcmp(DRIVE[i].label.name,
380 DRIVE[driveno].label.name) == 0)) { /* and it has the same name */
381 struct drive *mydrive = &DRIVE[i];
382
383 if (mydrive->devicename[0] == '/') { /* we know a device name for it */
384 /*
385 * set an error, but don't take the
386 * drive down: that would cause unneeded
387 * error messages.
388 */
389 drive->lasterror = EEXIST;
390 break;
391 } else { /* it's just a place holder, */
392 int sdno;
393
394 for (sdno = 0; sdno < vinum_conf.subdisks_allocated; sdno++) { /* look at each subdisk */
395 if ((SD[sdno].driveno == i) /* it's pointing to this one, */
396 &&(SD[sdno].state != sd_unallocated)) { /* and it's a real subdisk */
397 SD[sdno].driveno = drive->driveno; /* point to the one we found */
398 update_sd_state(sdno); /* and update its state */
399 }
400 }
401 bzero(mydrive, sizeof(struct drive)); /* don't deallocate it, just remove it */
402 }
403 }
404 }
405 } else {
406 if (drive->lasterror == 0)
407 drive->lasterror = ENODEV;
408 close_drive(drive);
409 drive->state = drive_down;
410 }
411 return drive;
412}
413
414static char *
415sappend(char *txt, char *s)
416{
417 while ((*s++ = *txt++) != 0);
418 return s - 1;
419}
420
421void
422format_config(char *config, int len)
423{
424 int i;
425 int j;
426 char *s = config;
427 char *configend = &config[len];
428
429 bzero(config, len);
430
431 /* First write the volume configuration */
432 for (i = 0; i < vinum_conf.volumes_allocated; i++) {
433 struct volume *vol;
434
435 vol = &vinum_conf.volume[i];
436 if ((vol->state > volume_uninit)
437 && (vol->name[0] != '\0')) { /* paranoia */
f8c7a42d 438 ksnprintf(s,
984263bc
MD
439 configend - s,
440 "volume %s state %s",
441 vol->name,
442 volume_state(vol->state));
443 while (*s)
444 s++; /* find the end */
445 if (vol->preferred_plex >= 0) /* preferences, */
f8c7a42d 446 ksnprintf(s,
984263bc
MD
447 configend - s,
448 " readpol prefer %s",
449 vinum_conf.plex[vol->preferred_plex].name);
450 while (*s)
451 s++; /* find the end */
452 s = sappend("\n", s);
453 }
454 }
455
456 /* Then the plex configuration */
457 for (i = 0; i < vinum_conf.plexes_allocated; i++) {
458 struct plex *plex;
459
460 plex = &vinum_conf.plex[i];
461 if ((plex->state > plex_referenced)
462 && (plex->name[0] != '\0')) { /* paranoia */
f8c7a42d 463 ksnprintf(s,
984263bc
MD
464 configend - s,
465 "plex name %s state %s org %s ",
466 plex->name,
467 plex_state(plex->state),
468 plex_org(plex->organization));
469 while (*s)
470 s++; /* find the end */
471 if (isstriped(plex)) {
f8c7a42d 472 ksnprintf(s,
984263bc
MD
473 configend - s,
474 "%ds ",
475 (int) plex->stripesize);
476 while (*s)
477 s++; /* find the end */
478 }
479 if (plex->volno >= 0) /* we have a volume */
f8c7a42d 480 ksnprintf(s,
984263bc
MD
481 configend - s,
482 "vol %s ",
483 vinum_conf.volume[plex->volno].name);
484 while (*s)
485 s++; /* find the end */
486 for (j = 0; j < plex->subdisks; j++) {
f8c7a42d 487 ksnprintf(s,
984263bc
MD
488 configend - s,
489 " sd %s",
490 vinum_conf.sd[plex->sdnos[j]].name);
491 }
492 s = sappend("\n", s);
493 }
494 }
495
496 /* And finally the subdisk configuration */
497 for (i = 0; i < vinum_conf.subdisks_allocated; i++) {
498 struct sd *sd;
499 char *drivename;
500
501 sd = &SD[i];
502 if ((sd->state != sd_referenced)
503 && (sd->state != sd_unallocated)
504 && (sd->name[0] != '\0')) { /* paranoia */
505 drivename = vinum_conf.drive[sd->driveno].label.name;
506 /*
507 * XXX We've seen cases of dead subdisks
508 * which don't have a drive. If we let them
509 * through here, the drive name is null, so
510 * they get the drive named 'plex'.
511 *
512 * This is a breakage limiter, not a fix.
513 */
514 if (drivename[0] == '\0')
515 drivename = "*invalid*";
f8c7a42d 516 ksnprintf(s,
984263bc
MD
517 configend - s,
518 "sd name %s drive %s plex %s len %llus driveoffset %llus state %s",
519 sd->name,
520 drivename,
521 vinum_conf.plex[sd->plexno].name,
522 (unsigned long long) sd->sectors,
523 (unsigned long long) sd->driveoffset,
524 sd_state(sd->state));
525 while (*s)
526 s++; /* find the end */
527 if (sd->plexno >= 0)
f8c7a42d 528 ksnprintf(s,
984263bc
MD
529 configend - s,
530 " plexoffset %llds",
531 (long long) sd->plexoffset);
532 else
f8c7a42d 533 ksnprintf(s, configend - s, " detached");
984263bc
MD
534 while (*s)
535 s++; /* find the end */
536 if (sd->flags & VF_RETRYERRORS) {
f8c7a42d 537 ksnprintf(s, configend - s, " retryerrors");
984263bc
MD
538 while (*s)
539 s++; /* find the end */
540 }
f8c7a42d 541 ksnprintf(s, configend - s, " \n");
984263bc
MD
542 while (*s)
543 s++; /* find the end */
544 }
545 }
546 if (s > &config[len - 2])
547 panic("vinum: configuration data overflow");
548}
549
550/*
551