dksubr.c revision 1.9 1 /* $NetBSD: dksubr.c,v 1.9 2003/06/29 22:29:58 fvdl Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997, 1998, 1999, 2002 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe and Roland C. Dowdeswell.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/stat.h>
42 #include <sys/proc.h>
43 #include <sys/ioctl.h>
44 #include <sys/device.h>
45 #include <sys/disk.h>
46 #include <sys/disklabel.h>
47 #include <sys/vnode.h>
48 #include <sys/fcntl.h>
49 #include <sys/namei.h>
50
51 #include <dev/dkvar.h>
52
53 int dkdebug = 0;
54
55 #ifdef DEBUG
56 #define DKDB_FOLLOW 0x1
57 #define DKDB_INIT 0x2
58 #define DKDB_VNODE 0x4
59
60 #define IFDEBUG(x,y) if (dkdebug & (x)) y
61 #define DPRINTF(x,y) IFDEBUG(x, printf y)
62 #define DPRINTF_FOLLOW(y) DPRINTF(DKDB_FOLLOW, y)
63 #else
64 #define IFDEBUG(x,y)
65 #define DPRINTF(x,y)
66 #define DPRINTF_FOLLOW(y)
67 #endif
68
69 #define DKLABELDEV(dev) \
70 (MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
71
72 void dk_makedisklabel(struct dk_intf *, struct dk_softc *);
73
74 void
75 dk_sc_init(struct dk_softc *dksc, void *osc, char *xname)
76 {
77
78 memset(dksc, 0x0, sizeof(*dksc));
79 dksc->sc_osc = osc;
80 strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
81 dksc->sc_dkdev.dk_name = dksc->sc_xname;
82 lockinit(&dksc->sc_lock, PRIBIO, "dklk", 0, 0);
83 }
84
85 /* ARGSUSED */
86 int
87 dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
88 int flags, int fmt, struct proc *p)
89 {
90 struct disklabel *lp = dksc->sc_dkdev.dk_label;
91 int part = DISKPART(dev);
92 int pmask = 1 << part;
93 int ret = 0;
94
95 DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%x, 0x%x)\n",
96 di->di_dkname, dksc, dev, flags));
97
98 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
99 return ret;
100
101 part = DISKPART(dev);
102 pmask = 1 << part;
103
104 /*
105 * If we're init'ed and there are no other open partitions then
106 * update the in-core disklabel.
107 */
108 if ((dksc->sc_flags & DKF_INITED) && dksc->sc_dkdev.dk_openmask == 0)
109 dk_getdisklabel(di, dksc, dev);
110
111 /* Fail if we can't find the partition. */
112 if ((part != RAW_PART) &&
113 (((dksc->sc_flags & DKF_INITED) == 0) ||
114 ((part >= lp->d_npartitions) ||
115 (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
116 ret = ENXIO;
117 goto done;
118 }
119
120 /* Mark our unit as open. */
121 switch (fmt) {
122 case S_IFCHR:
123 dksc->sc_dkdev.dk_copenmask |= pmask;
124 break;
125 case S_IFBLK:
126 dksc->sc_dkdev.dk_bopenmask |= pmask;
127 break;
128 }
129
130 dksc->sc_dkdev.dk_openmask =
131 dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
132
133 done:
134 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
135 return ret;
136 }
137
138 /* ARGSUSED */
139 int
140 dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
141 int flags, int fmt, struct proc *p)
142 {
143 int part = DISKPART(dev);
144 int pmask = 1 << part;
145 int ret;
146
147 DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%x, 0x%x)\n",
148 di->di_dkname, dksc, dev, flags));
149
150 if ((ret = lockmgr(&dksc->sc_lock, LK_EXCLUSIVE, NULL)) != 0)
151 return ret;
152
153 switch (fmt) {
154 case S_IFCHR:
155 dksc->sc_dkdev.dk_copenmask &= ~pmask;
156 break;
157 case S_IFBLK:
158 dksc->sc_dkdev.dk_bopenmask &= ~pmask;
159 break;
160 }
161 dksc->sc_dkdev.dk_openmask =
162 dksc->sc_dkdev.dk_copenmask | dksc->sc_dkdev.dk_bopenmask;
163
164 lockmgr(&dksc->sc_lock, LK_RELEASE, NULL);
165 return 0;
166 }
167
168 void
169 dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
170 {
171 int s;
172 int wlabel;
173
174 DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
175 di->di_dkname, dksc, bp));
176
177 if (!(dksc->sc_flags & DKF_INITED)) {
178 DPRINTF_FOLLOW(("dk_stragy: not inited\n"));
179 bp->b_error = ENXIO;
180 bp->b_flags |= B_ERROR;
181 biodone(bp);
182 return;
183 }
184
185 /* XXX look for some more errors, c.f. ld.c */
186
187 bp->b_resid = bp->b_bcount;
188
189 /* If there is nothing to do, then we are done */
190 if (bp->b_bcount == 0) {
191 biodone(bp);
192 return;
193 }
194
195 wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
196 if (DISKPART(bp->b_dev) != RAW_PART &&
197 bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
198 biodone(bp);
199 return;
200 }
201
202 /*
203 * Start the unit by calling the start routine
204 * provided by the individual driver.
205 */
206 s = splbio();
207 di->di_diskstart(dksc, bp);
208 splx(s);
209 return;
210 }
211
212 int
213 dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
214 {
215 struct disklabel *lp;
216 int is_open;
217 int part;
218 int size;
219
220 if ((dksc->sc_flags & DKF_INITED) == 0)
221 return -1;
222
223 part = DISKPART(dev);
224 is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
225
226 if (!is_open && di->di_open(dev, 0, S_IFBLK, curproc))
227 return -1;
228
229 lp = dksc->sc_dkdev.dk_label;
230 if (lp->d_partitions[part].p_fstype != FS_SWAP)
231 size = -1;
232 else
233 size = lp->d_partitions[part].p_size *
234 (lp->d_secsize / DEV_BSIZE);
235
236 if (!is_open && di->di_close(dev, 0, S_IFBLK, curproc))
237 return 1;
238
239 return size;
240 }
241
242 int
243 dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
244 u_long cmd, caddr_t data, int flag, struct proc *p)
245 {
246 struct disklabel *lp;
247 #ifdef __HAVE_OLD_DISKLABEL
248 struct disklabel newlabel;
249 #endif
250 int error = 0;
251
252 DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%x, 0x%lx)\n",
253 di->di_dkname, dksc, dev, cmd));
254
255 /* ensure that the pseudo disk is open for writes for these commands */
256 switch (cmd) {
257 case DIOCSDINFO:
258 case DIOCWDINFO:
259 #ifdef __HAVE_OLD_DISKLABEL
260 case ODIOCSDINFO:
261 case ODIOCWDINFO:
262 #endif
263 case DIOCWLABEL:
264 if ((flag & FWRITE) == 0)
265 return EBADF;
266 }
267
268 /* ensure that the pseudo-disk is initialized for these */
269 switch (cmd) {
270 case DIOCGDINFO:
271 case DIOCSDINFO:
272 case DIOCWDINFO:
273 case DIOCGPART:
274 case DIOCWLABEL:
275 case DIOCGDEFLABEL:
276 #ifdef __HAVE_OLD_DISKLABEL
277 case ODIOCGDINFO:
278 case ODIOCSDINFO:
279 case ODIOCWDINFO:
280 case ODIOCGDEFLABEL:
281 #endif
282 if ((dksc->sc_flags & DKF_INITED) == 0)
283 return ENXIO;
284 }
285
286 switch (cmd) {
287 case DIOCGDINFO:
288 *(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
289 break;
290
291 #ifdef __HAVE_OLD_DISKLABEL
292 case ODIOCGDINFO:
293 newlabel = *(dksc->sc_dkdev.dk_label);
294 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
295 return ENOTTY;
296 memcpy(data, &newlabel, sizeof (struct olddisklabel));
297 break;
298 #endif
299
300 case DIOCGPART:
301 ((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
302 ((struct partinfo *)data)->part =
303 &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
304 break;
305
306 case DIOCWDINFO:
307 case DIOCSDINFO:
308 #ifdef __HAVE_OLD_DISKLABEL
309 case ODIOCWDINFO:
310 case ODIOCSDINFO:
311 #endif
312 #ifdef __HAVE_OLD_DISKLABEL
313 if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
314 memset(&newlabel, 0, sizeof newlabel);
315 memcpy(&newlabel, data, sizeof (struct olddisklabel));
316 lp = &newlabel;
317 } else
318 #endif
319 lp = (struct disklabel *)data;
320
321 dksc->sc_flags |= DKF_LABELLING;
322
323 error = setdisklabel(dksc->sc_dkdev.dk_label,
324 lp, 0, dksc->sc_dkdev.dk_cpulabel);
325 if (error == 0) {
326 if (cmd == DIOCWDINFO
327 #ifdef __HAVE_OLD_DISKLABEL
328 || cmd == ODIOCWDINFO
329 #endif
330 )
331 error = writedisklabel(DKLABELDEV(dev),
332 di->di_strategy, dksc->sc_dkdev.dk_label,
333 dksc->sc_dkdev.dk_cpulabel);
334 }
335
336 dksc->sc_flags &= ~DKF_LABELLING;
337 break;
338
339 case DIOCWLABEL:
340 if (*(int *)data != 0)
341 dksc->sc_flags |= DKF_WLABEL;
342 else
343 dksc->sc_flags &= ~DKF_WLABEL;
344 break;
345
346 case DIOCGDEFLABEL:
347 dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
348 break;
349
350 #ifdef __HAVE_OLD_DISKLABEL
351 case ODIOCGDEFLABEL:
352 dk_getdefaultlabel(di, dksc, &newlabel);
353 if (newlabel.d_npartitions > OLDMAXPARTITIONS)
354 return ENOTTY;
355 memcpy(data, &newlabel, sizeof (struct olddisklabel));
356 break;
357 #endif
358
359 default:
360 error = ENOTTY;
361 }
362
363 return error;
364 }
365
366 /*
367 * dk_dump dumps all of physical memory into the partition specified.
368 * This requires substantially more framework than {s,w}ddump, and hence
369 * is probably much more fragile.
370 *
371 * XXX: we currently do not implement this.
372 */
373
374 #define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
375 #define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
376 static volatile int dk_dumping = 0;
377
378 /* ARGSUSED */
379 int
380 dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
381 daddr_t blkno, caddr_t va, size_t size)
382 {
383
384 /*
385 * ensure that we consider this device to be safe for dumping,
386 * and that the device is configured.
387 */
388 if (!DKFF_READYFORDUMP(dksc->sc_flags))
389 return ENXIO;
390
391 /* ensure that we are not already dumping */
392 if (dk_dumping)
393 return EFAULT;
394 dk_dumping = 1;
395
396 /* XXX: unimplemented */
397
398 dk_dumping = 0;
399
400 /* XXX: actually for now, we are going to leave this alone */
401 return ENXIO;
402 }
403
404 /* ARGSUSED */
405 void
406 dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
407 struct disklabel *lp)
408 {
409 struct dk_geom *pdg = &dksc->sc_geom;
410
411 memset(lp, 0, sizeof(*lp));
412
413 lp->d_secperunit = dksc->sc_size;
414 lp->d_secsize = pdg->pdg_secsize;
415 lp->d_nsectors = pdg->pdg_nsectors;
416 lp->d_ntracks = pdg->pdg_ntracks;
417 lp->d_ncylinders = pdg->pdg_ncylinders;
418 lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
419
420 strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
421 lp->d_type = di->di_dtype;
422 strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
423 lp->d_rpm = 3600;
424 lp->d_interleave = 1;
425 lp->d_flags = 0;
426
427 lp->d_partitions[RAW_PART].p_offset = 0;
428 lp->d_partitions[RAW_PART].p_size = dksc->sc_size;
429 lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
430 lp->d_npartitions = RAW_PART + 1;
431
432 lp->d_magic = DISKMAGIC;
433 lp->d_magic2 = DISKMAGIC;
434 lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
435 }
436
437 /* ARGSUSED */
438 void
439 dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
440 {
441 struct disklabel *lp = dksc->sc_dkdev.dk_label;
442 struct cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
443 struct partition *pp;
444 int i;
445 const char *errstring;
446
447 memset(clp, 0x0, sizeof(*clp));
448 dk_getdefaultlabel(di, dksc, lp);
449 errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
450 dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
451 if (errstring) {
452 dk_makedisklabel(di, dksc);
453 if (dksc->sc_flags & DKF_WARNLABEL)
454 printf("%s: %s\n", dksc->sc_xname, errstring);
455 return;
456 }
457
458 if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
459 return;
460
461 /* Sanity check */
462 if (lp->d_secperunit != dksc->sc_size)
463 printf("WARNING: %s: total sector size in disklabel (%d) "
464 "!= the size of %s (%lu)\n", dksc->sc_xname,
465 lp->d_secperunit, di->di_dkname, (u_long)dksc->sc_size);
466
467 for (i=0; i < lp->d_npartitions; i++) {
468 pp = &lp->d_partitions[i];
469 if (pp->p_offset + pp->p_size > dksc->sc_size)
470 printf("WARNING: %s: end of partition `%c' exceeds "
471 "the size of %s (%lu)\n", dksc->sc_xname,
472 'a' + i, di->di_dkname, (u_long)dksc->sc_size);
473 }
474 }
475
476 /* ARGSUSED */
477 void
478 dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
479 {
480 struct disklabel *lp = dksc->sc_dkdev.dk_label;
481
482 lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
483 strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
484 lp->d_checksum = dkcksum(lp);
485 }
486
487 /* This function is taken from ccd.c:1.76 --rcd */
488
489 /*
490 * XXX this function looks too generic for dksubr.c, shouldn't we
491 * put it somewhere better?
492 */
493
494 /*
495 * Lookup the provided name in the filesystem. If the file exists,
496 * is a valid block device, and isn't being used by anyone else,
497 * set *vpp to the file's vnode.
498 */
499 int
500 dk_lookup(path, p, vpp)
501 char *path;
502 struct proc *p;
503 struct vnode **vpp; /* result */
504 {
505 struct nameidata nd;
506 struct vnode *vp;
507 struct vattr va;
508 int error;
509
510 NDINIT(&nd, LOOKUP, FOLLOW, UIO_USERSPACE, path, p);
511 if ((error = vn_open(&nd, FREAD|FWRITE, 0)) != 0) {
512 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
513 ("dk_lookup: vn_open error = %d\n", error));
514 return (error);
515 }
516 vp = nd.ni_vp;
517
518 if (vp->v_usecount > 1) {
519 VOP_UNLOCK(vp, 0);
520 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
521 return (EBUSY);
522 }
523
524 if ((error = VOP_GETATTR(vp, &va, p->p_ucred, p)) != 0) {
525 DPRINTF((DKDB_FOLLOW|DKDB_INIT),
526 ("dk_lookup: getattr error = %d\n", error));
527 VOP_UNLOCK(vp, 0);
528 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
529 return (error);
530 }
531
532 /* XXX: eventually we should handle VREG, too. */
533 if (va.va_type != VBLK) {
534 VOP_UNLOCK(vp, 0);
535 (void)vn_close(vp, FREAD|FWRITE, p->p_ucred, p);
536 return (ENOTBLK);
537 }
538
539 IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", vp));
540
541 VOP_UNLOCK(vp, 0);
542 *vpp = vp;
543 return (0);
544 }
545