dksubr.c revision 1.51.2.3 1 1.51.2.3 snj /* $NetBSD: dksubr.c,v 1.51.2.3 2015/06/01 19:19:44 snj Exp $ */
2 1.1 elric
3 1.1 elric /*-
4 1.34 ad * Copyright (c) 1996, 1997, 1998, 1999, 2002, 2008 The NetBSD Foundation, Inc.
5 1.1 elric * All rights reserved.
6 1.1 elric *
7 1.1 elric * This code is derived from software contributed to The NetBSD Foundation
8 1.1 elric * by Jason R. Thorpe and Roland C. Dowdeswell.
9 1.1 elric *
10 1.1 elric * Redistribution and use in source and binary forms, with or without
11 1.1 elric * modification, are permitted provided that the following conditions
12 1.1 elric * are met:
13 1.1 elric * 1. Redistributions of source code must retain the above copyright
14 1.1 elric * notice, this list of conditions and the following disclaimer.
15 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 elric * notice, this list of conditions and the following disclaimer in the
17 1.1 elric * documentation and/or other materials provided with the distribution.
18 1.1 elric *
19 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 elric * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 elric * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 elric * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 elric * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 elric * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 elric * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 elric * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 elric * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 elric * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 elric * POSSIBILITY OF SUCH DAMAGE.
30 1.1 elric */
31 1.10 lukem
32 1.10 lukem #include <sys/cdefs.h>
33 1.51.2.3 snj __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.51.2.3 2015/06/01 19:19:44 snj Exp $");
34 1.1 elric
35 1.1 elric #include <sys/param.h>
36 1.1 elric #include <sys/systm.h>
37 1.1 elric #include <sys/stat.h>
38 1.1 elric #include <sys/proc.h>
39 1.1 elric #include <sys/ioctl.h>
40 1.1 elric #include <sys/device.h>
41 1.1 elric #include <sys/disk.h>
42 1.1 elric #include <sys/disklabel.h>
43 1.14 yamt #include <sys/buf.h>
44 1.14 yamt #include <sys/bufq.h>
45 1.1 elric #include <sys/vnode.h>
46 1.1 elric #include <sys/fcntl.h>
47 1.1 elric #include <sys/namei.h>
48 1.49 pgoyette #include <sys/module.h>
49 1.1 elric
50 1.1 elric #include <dev/dkvar.h>
51 1.51 hannken #include <miscfs/specfs/specdev.h> /* for v_rdev */
52 1.1 elric
53 1.44 elric int dkdebug = 0;
54 1.1 elric
55 1.1 elric #ifdef DEBUG
56 1.1 elric #define DKDB_FOLLOW 0x1
57 1.1 elric #define DKDB_INIT 0x2
58 1.1 elric #define DKDB_VNODE 0x4
59 1.1 elric
60 1.1 elric #define IFDEBUG(x,y) if (dkdebug & (x)) y
61 1.1 elric #define DPRINTF(x,y) IFDEBUG(x, printf y)
62 1.1 elric #define DPRINTF_FOLLOW(y) DPRINTF(DKDB_FOLLOW, y)
63 1.1 elric #else
64 1.1 elric #define IFDEBUG(x,y)
65 1.1 elric #define DPRINTF(x,y)
66 1.1 elric #define DPRINTF_FOLLOW(y)
67 1.1 elric #endif
68 1.1 elric
69 1.49 pgoyette static int dk_subr_modcmd(modcmd_t, void *);
70 1.49 pgoyette
71 1.1 elric #define DKLABELDEV(dev) \
72 1.1 elric (MAKEDISKDEV(major((dev)), DISKUNIT((dev)), RAW_PART))
73 1.1 elric
74 1.13 thorpej static void dk_makedisklabel(struct dk_intf *, struct dk_softc *);
75 1.1 elric
76 1.1 elric void
77 1.43 elric dk_sc_init(struct dk_softc *dksc, const char *xname)
78 1.1 elric {
79 1.1 elric
80 1.1 elric memset(dksc, 0x0, sizeof(*dksc));
81 1.1 elric strncpy(dksc->sc_xname, xname, DK_XNAME_SIZE);
82 1.1 elric dksc->sc_dkdev.dk_name = dksc->sc_xname;
83 1.1 elric }
84 1.1 elric
85 1.1 elric /* ARGSUSED */
86 1.1 elric int
87 1.1 elric dk_open(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
88 1.27 christos int flags, int fmt, struct lwp *l)
89 1.1 elric {
90 1.1 elric struct disklabel *lp = dksc->sc_dkdev.dk_label;
91 1.1 elric int part = DISKPART(dev);
92 1.1 elric int pmask = 1 << part;
93 1.1 elric int ret = 0;
94 1.16 yamt struct disk *dk = &dksc->sc_dkdev;
95 1.1 elric
96 1.39 cegger DPRINTF_FOLLOW(("dk_open(%s, %p, 0x%"PRIx64", 0x%x)\n",
97 1.1 elric di->di_dkname, dksc, dev, flags));
98 1.1 elric
99 1.30 ad mutex_enter(&dk->dk_openlock);
100 1.1 elric part = DISKPART(dev);
101 1.16 yamt
102 1.16 yamt /*
103 1.16 yamt * If there are wedges, and this is not RAW_PART, then we
104 1.16 yamt * need to fail.
105 1.16 yamt */
106 1.16 yamt if (dk->dk_nwedges != 0 && part != RAW_PART) {
107 1.16 yamt ret = EBUSY;
108 1.16 yamt goto done;
109 1.16 yamt }
110 1.16 yamt
111 1.1 elric pmask = 1 << part;
112 1.1 elric
113 1.1 elric /*
114 1.1 elric * If we're init'ed and there are no other open partitions then
115 1.1 elric * update the in-core disklabel.
116 1.1 elric */
117 1.16 yamt if ((dksc->sc_flags & DKF_INITED)) {
118 1.16 yamt if (dk->dk_openmask == 0) {
119 1.16 yamt dk_getdisklabel(di, dksc, dev);
120 1.16 yamt }
121 1.16 yamt /* XXX re-discover wedges? */
122 1.16 yamt }
123 1.1 elric
124 1.1 elric /* Fail if we can't find the partition. */
125 1.1 elric if ((part != RAW_PART) &&
126 1.1 elric (((dksc->sc_flags & DKF_INITED) == 0) ||
127 1.1 elric ((part >= lp->d_npartitions) ||
128 1.1 elric (lp->d_partitions[part].p_fstype == FS_UNUSED)))) {
129 1.1 elric ret = ENXIO;
130 1.1 elric goto done;
131 1.1 elric }
132 1.1 elric
133 1.1 elric /* Mark our unit as open. */
134 1.1 elric switch (fmt) {
135 1.1 elric case S_IFCHR:
136 1.16 yamt dk->dk_copenmask |= pmask;
137 1.1 elric break;
138 1.1 elric case S_IFBLK:
139 1.16 yamt dk->dk_bopenmask |= pmask;
140 1.1 elric break;
141 1.1 elric }
142 1.1 elric
143 1.16 yamt dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
144 1.1 elric
145 1.1 elric done:
146 1.30 ad mutex_exit(&dk->dk_openlock);
147 1.1 elric return ret;
148 1.1 elric }
149 1.1 elric
150 1.1 elric /* ARGSUSED */
151 1.1 elric int
152 1.27 christos dk_close(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
153 1.27 christos int flags, int fmt, struct lwp *l)
154 1.1 elric {
155 1.1 elric int part = DISKPART(dev);
156 1.1 elric int pmask = 1 << part;
157 1.16 yamt struct disk *dk = &dksc->sc_dkdev;
158 1.1 elric
159 1.39 cegger DPRINTF_FOLLOW(("dk_close(%s, %p, 0x%"PRIx64", 0x%x)\n",
160 1.1 elric di->di_dkname, dksc, dev, flags));
161 1.1 elric
162 1.30 ad mutex_enter(&dk->dk_openlock);
163 1.1 elric
164 1.1 elric switch (fmt) {
165 1.1 elric case S_IFCHR:
166 1.16 yamt dk->dk_copenmask &= ~pmask;
167 1.1 elric break;
168 1.1 elric case S_IFBLK:
169 1.16 yamt dk->dk_bopenmask &= ~pmask;
170 1.1 elric break;
171 1.1 elric }
172 1.16 yamt dk->dk_openmask = dk->dk_copenmask | dk->dk_bopenmask;
173 1.1 elric
174 1.30 ad mutex_exit(&dk->dk_openlock);
175 1.1 elric return 0;
176 1.1 elric }
177 1.1 elric
178 1.1 elric void
179 1.1 elric dk_strategy(struct dk_intf *di, struct dk_softc *dksc, struct buf *bp)
180 1.1 elric {
181 1.1 elric int s;
182 1.1 elric int wlabel;
183 1.18 yamt daddr_t blkno;
184 1.1 elric
185 1.1 elric DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
186 1.1 elric di->di_dkname, dksc, bp));
187 1.1 elric
188 1.1 elric if (!(dksc->sc_flags & DKF_INITED)) {
189 1.25 dan DPRINTF_FOLLOW(("dk_strategy: not inited\n"));
190 1.1 elric bp->b_error = ENXIO;
191 1.2 elric biodone(bp);
192 1.1 elric return;
193 1.1 elric }
194 1.1 elric
195 1.1 elric /* XXX look for some more errors, c.f. ld.c */
196 1.1 elric
197 1.1 elric bp->b_resid = bp->b_bcount;
198 1.1 elric
199 1.1 elric /* If there is nothing to do, then we are done */
200 1.1 elric if (bp->b_bcount == 0) {
201 1.1 elric biodone(bp);
202 1.1 elric return;
203 1.1 elric }
204 1.1 elric
205 1.1 elric wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
206 1.1 elric if (DISKPART(bp->b_dev) != RAW_PART &&
207 1.6 thorpej bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
208 1.1 elric biodone(bp);
209 1.1 elric return;
210 1.1 elric }
211 1.1 elric
212 1.18 yamt blkno = bp->b_blkno;
213 1.18 yamt if (DISKPART(bp->b_dev) != RAW_PART) {
214 1.18 yamt struct partition *pp;
215 1.18 yamt
216 1.18 yamt pp =
217 1.18 yamt &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(bp->b_dev)];
218 1.18 yamt blkno += pp->p_offset;
219 1.18 yamt }
220 1.18 yamt bp->b_rawblkno = blkno;
221 1.18 yamt
222 1.1 elric /*
223 1.1 elric * Start the unit by calling the start routine
224 1.1 elric * provided by the individual driver.
225 1.1 elric */
226 1.1 elric s = splbio();
227 1.40 yamt bufq_put(dksc->sc_bufq, bp);
228 1.50 bouyer di->di_diskstart(dksc);
229 1.1 elric splx(s);
230 1.1 elric return;
231 1.1 elric }
232 1.1 elric
233 1.1 elric int
234 1.1 elric dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
235 1.1 elric {
236 1.1 elric struct disklabel *lp;
237 1.1 elric int is_open;
238 1.1 elric int part;
239 1.1 elric int size;
240 1.1 elric
241 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
242 1.1 elric return -1;
243 1.1 elric
244 1.1 elric part = DISKPART(dev);
245 1.1 elric is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
246 1.1 elric
247 1.19 christos if (!is_open && di->di_open(dev, 0, S_IFBLK, curlwp))
248 1.1 elric return -1;
249 1.1 elric
250 1.1 elric lp = dksc->sc_dkdev.dk_label;
251 1.1 elric if (lp->d_partitions[part].p_fstype != FS_SWAP)
252 1.1 elric size = -1;
253 1.1 elric else
254 1.1 elric size = lp->d_partitions[part].p_size *
255 1.1 elric (lp->d_secsize / DEV_BSIZE);
256 1.1 elric
257 1.19 christos if (!is_open && di->di_close(dev, 0, S_IFBLK, curlwp))
258 1.1 elric return 1;
259 1.1 elric
260 1.1 elric return size;
261 1.1 elric }
262 1.1 elric
263 1.1 elric int
264 1.1 elric dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
265 1.28 christos u_long cmd, void *data, int flag, struct lwp *l)
266 1.1 elric {
267 1.1 elric struct disklabel *lp;
268 1.16 yamt struct disk *dk;
269 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
270 1.1 elric struct disklabel newlabel;
271 1.1 elric #endif
272 1.1 elric int error = 0;
273 1.1 elric
274 1.39 cegger DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%"PRIx64", 0x%lx)\n",
275 1.1 elric di->di_dkname, dksc, dev, cmd));
276 1.1 elric
277 1.1 elric /* ensure that the pseudo disk is open for writes for these commands */
278 1.1 elric switch (cmd) {
279 1.1 elric case DIOCSDINFO:
280 1.1 elric case DIOCWDINFO:
281 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
282 1.1 elric case ODIOCSDINFO:
283 1.1 elric case ODIOCWDINFO:
284 1.1 elric #endif
285 1.1 elric case DIOCWLABEL:
286 1.43 elric case DIOCAWEDGE:
287 1.43 elric case DIOCDWEDGE:
288 1.1 elric if ((flag & FWRITE) == 0)
289 1.1 elric return EBADF;
290 1.1 elric }
291 1.1 elric
292 1.1 elric /* ensure that the pseudo-disk is initialized for these */
293 1.1 elric switch (cmd) {
294 1.1 elric case DIOCGDINFO:
295 1.1 elric case DIOCSDINFO:
296 1.1 elric case DIOCWDINFO:
297 1.1 elric case DIOCGPART:
298 1.1 elric case DIOCWLABEL:
299 1.1 elric case DIOCGDEFLABEL:
300 1.43 elric case DIOCAWEDGE:
301 1.43 elric case DIOCDWEDGE:
302 1.43 elric case DIOCLWEDGES:
303 1.51.2.1 martin case DIOCMWEDGES:
304 1.43 elric case DIOCCACHESYNC:
305 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
306 1.1 elric case ODIOCGDINFO:
307 1.1 elric case ODIOCSDINFO:
308 1.1 elric case ODIOCWDINFO:
309 1.1 elric case ODIOCGDEFLABEL:
310 1.1 elric #endif
311 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
312 1.1 elric return ENXIO;
313 1.1 elric }
314 1.1 elric
315 1.1 elric switch (cmd) {
316 1.1 elric case DIOCGDINFO:
317 1.1 elric *(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
318 1.1 elric break;
319 1.1 elric
320 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
321 1.1 elric case ODIOCGDINFO:
322 1.1 elric newlabel = *(dksc->sc_dkdev.dk_label);
323 1.1 elric if (newlabel.d_npartitions > OLDMAXPARTITIONS)
324 1.1 elric return ENOTTY;
325 1.1 elric memcpy(data, &newlabel, sizeof (struct olddisklabel));
326 1.1 elric break;
327 1.1 elric #endif
328 1.1 elric
329 1.1 elric case DIOCGPART:
330 1.1 elric ((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
331 1.1 elric ((struct partinfo *)data)->part =
332 1.1 elric &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
333 1.1 elric break;
334 1.1 elric
335 1.1 elric case DIOCWDINFO:
336 1.1 elric case DIOCSDINFO:
337 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
338 1.1 elric case ODIOCWDINFO:
339 1.1 elric case ODIOCSDINFO:
340 1.1 elric #endif
341 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
342 1.1 elric if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
343 1.1 elric memset(&newlabel, 0, sizeof newlabel);
344 1.1 elric memcpy(&newlabel, data, sizeof (struct olddisklabel));
345 1.1 elric lp = &newlabel;
346 1.1 elric } else
347 1.1 elric #endif
348 1.1 elric lp = (struct disklabel *)data;
349 1.1 elric
350 1.16 yamt dk = &dksc->sc_dkdev;
351 1.30 ad mutex_enter(&dk->dk_openlock);
352 1.1 elric dksc->sc_flags |= DKF_LABELLING;
353 1.1 elric
354 1.1 elric error = setdisklabel(dksc->sc_dkdev.dk_label,
355 1.1 elric lp, 0, dksc->sc_dkdev.dk_cpulabel);
356 1.1 elric if (error == 0) {
357 1.1 elric if (cmd == DIOCWDINFO
358 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
359 1.1 elric || cmd == ODIOCWDINFO
360 1.1 elric #endif
361 1.1 elric )
362 1.1 elric error = writedisklabel(DKLABELDEV(dev),
363 1.1 elric di->di_strategy, dksc->sc_dkdev.dk_label,
364 1.1 elric dksc->sc_dkdev.dk_cpulabel);
365 1.1 elric }
366 1.1 elric
367 1.1 elric dksc->sc_flags &= ~DKF_LABELLING;
368 1.30 ad mutex_exit(&dk->dk_openlock);
369 1.1 elric break;
370 1.1 elric
371 1.1 elric case DIOCWLABEL:
372 1.1 elric if (*(int *)data != 0)
373 1.1 elric dksc->sc_flags |= DKF_WLABEL;
374 1.1 elric else
375 1.1 elric dksc->sc_flags &= ~DKF_WLABEL;
376 1.1 elric break;
377 1.1 elric
378 1.1 elric case DIOCGDEFLABEL:
379 1.1 elric dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
380 1.1 elric break;
381 1.1 elric
382 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
383 1.1 elric case ODIOCGDEFLABEL:
384 1.1 elric dk_getdefaultlabel(di, dksc, &newlabel);
385 1.1 elric if (newlabel.d_npartitions > OLDMAXPARTITIONS)
386 1.1 elric return ENOTTY;
387 1.1 elric memcpy(data, &newlabel, sizeof (struct olddisklabel));
388 1.1 elric break;
389 1.1 elric #endif
390 1.1 elric
391 1.16 yamt case DIOCAWEDGE:
392 1.16 yamt {
393 1.16 yamt struct dkwedge_info *dkw = (void *)data;
394 1.16 yamt
395 1.16 yamt if ((flag & FWRITE) == 0)
396 1.16 yamt return (EBADF);
397 1.16 yamt
398 1.16 yamt /* If the ioctl happens here, the parent is us. */
399 1.16 yamt strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
400 1.16 yamt return (dkwedge_add(dkw));
401 1.16 yamt }
402 1.16 yamt
403 1.16 yamt case DIOCDWEDGE:
404 1.16 yamt {
405 1.16 yamt struct dkwedge_info *dkw = (void *)data;
406 1.16 yamt
407 1.16 yamt if ((flag & FWRITE) == 0)
408 1.16 yamt return (EBADF);
409 1.16 yamt
410 1.16 yamt /* If the ioctl happens here, the parent is us. */
411 1.16 yamt strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
412 1.16 yamt return (dkwedge_del(dkw));
413 1.16 yamt }
414 1.16 yamt
415 1.16 yamt case DIOCLWEDGES:
416 1.16 yamt {
417 1.16 yamt struct dkwedge_list *dkwl = (void *)data;
418 1.16 yamt
419 1.20 rpaulo return (dkwedge_list(&dksc->sc_dkdev, dkwl, l));
420 1.16 yamt }
421 1.16 yamt
422 1.51.2.1 martin case DIOCMWEDGES:
423 1.51.2.1 martin {
424 1.51.2.1 martin if ((flag & FWRITE) == 0)
425 1.51.2.1 martin return (EBADF);
426 1.51.2.1 martin
427 1.51.2.1 martin dkwedge_discover(&dksc->sc_dkdev);
428 1.51.2.1 martin return 0;
429 1.51.2.1 martin }
430 1.51.2.1 martin
431 1.21 yamt case DIOCGSTRATEGY:
432 1.21 yamt {
433 1.21 yamt struct disk_strategy *dks = (void *)data;
434 1.21 yamt int s;
435 1.21 yamt
436 1.21 yamt s = splbio();
437 1.21 yamt strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
438 1.21 yamt sizeof(dks->dks_name));
439 1.21 yamt splx(s);
440 1.21 yamt dks->dks_paramlen = 0;
441 1.21 yamt
442 1.21 yamt return 0;
443 1.21 yamt }
444 1.21 yamt
445 1.21 yamt case DIOCSSTRATEGY:
446 1.21 yamt {
447 1.21 yamt struct disk_strategy *dks = (void *)data;
448 1.21 yamt struct bufq_state *new;
449 1.21 yamt struct bufq_state *old;
450 1.21 yamt int s;
451 1.21 yamt
452 1.21 yamt if ((flag & FWRITE) == 0) {
453 1.21 yamt return EBADF;
454 1.21 yamt }
455 1.21 yamt if (dks->dks_param != NULL) {
456 1.21 yamt return EINVAL;
457 1.21 yamt }
458 1.21 yamt dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
459 1.21 yamt error = bufq_alloc(&new, dks->dks_name,
460 1.21 yamt BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
461 1.21 yamt if (error) {
462 1.21 yamt return error;
463 1.21 yamt }
464 1.21 yamt s = splbio();
465 1.21 yamt old = dksc->sc_bufq;
466 1.21 yamt bufq_move(new, old);
467 1.21 yamt dksc->sc_bufq = new;
468 1.21 yamt splx(s);
469 1.21 yamt bufq_free(old);
470 1.21 yamt
471 1.21 yamt return 0;
472 1.21 yamt }
473 1.21 yamt
474 1.1 elric default:
475 1.1 elric error = ENOTTY;
476 1.1 elric }
477 1.1 elric
478 1.1 elric return error;
479 1.1 elric }
480 1.1 elric
481 1.1 elric /*
482 1.1 elric * dk_dump dumps all of physical memory into the partition specified.
483 1.1 elric * This requires substantially more framework than {s,w}ddump, and hence
484 1.1 elric * is probably much more fragile.
485 1.1 elric *
486 1.1 elric * XXX: we currently do not implement this.
487 1.1 elric */
488 1.1 elric
489 1.1 elric #define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
490 1.1 elric #define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
491 1.1 elric static volatile int dk_dumping = 0;
492 1.1 elric
493 1.1 elric /* ARGSUSED */
494 1.1 elric int
495 1.27 christos dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
496 1.28 christos daddr_t blkno, void *va, size_t size)
497 1.1 elric {
498 1.1 elric
499 1.1 elric /*
500 1.1 elric * ensure that we consider this device to be safe for dumping,
501 1.1 elric * and that the device is configured.
502 1.1 elric */
503 1.1 elric if (!DKFF_READYFORDUMP(dksc->sc_flags))
504 1.1 elric return ENXIO;
505 1.1 elric
506 1.1 elric /* ensure that we are not already dumping */
507 1.1 elric if (dk_dumping)
508 1.1 elric return EFAULT;
509 1.1 elric dk_dumping = 1;
510 1.1 elric
511 1.1 elric /* XXX: unimplemented */
512 1.1 elric
513 1.1 elric dk_dumping = 0;
514 1.1 elric
515 1.1 elric /* XXX: actually for now, we are going to leave this alone */
516 1.1 elric return ENXIO;
517 1.1 elric }
518 1.1 elric
519 1.1 elric /* ARGSUSED */
520 1.1 elric void
521 1.1 elric dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
522 1.1 elric struct disklabel *lp)
523 1.1 elric {
524 1.47 christos struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
525 1.4 elric
526 1.4 elric memset(lp, 0, sizeof(*lp));
527 1.1 elric
528 1.51.2.2 martin if (dg->dg_secperunit > UINT32_MAX)
529 1.51.2.2 martin lp->d_secperunit = UINT32_MAX;
530 1.51.2.2 martin else
531 1.51.2.2 martin lp->d_secperunit = dg->dg_secperunit;
532 1.47 christos lp->d_secsize = dg->dg_secsize;
533 1.47 christos lp->d_nsectors = dg->dg_nsectors;
534 1.47 christos lp->d_ntracks = dg->dg_ntracks;
535 1.47 christos lp->d_ncylinders = dg->dg_ncylinders;
536 1.1 elric lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
537 1.1 elric
538 1.1 elric strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
539 1.1 elric lp->d_type = di->di_dtype;
540 1.1 elric strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
541 1.1 elric lp->d_rpm = 3600;
542 1.1 elric lp->d_interleave = 1;
543 1.1 elric lp->d_flags = 0;
544 1.1 elric
545 1.1 elric lp->d_partitions[RAW_PART].p_offset = 0;
546 1.51.2.2 martin lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
547 1.1 elric lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
548 1.1 elric lp->d_npartitions = RAW_PART + 1;
549 1.1 elric
550 1.1 elric lp->d_magic = DISKMAGIC;
551 1.1 elric lp->d_magic2 = DISKMAGIC;
552 1.1 elric lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
553 1.1 elric }
554 1.1 elric
555 1.1 elric /* ARGSUSED */
556 1.1 elric void
557 1.1 elric dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
558 1.1 elric {
559 1.1 elric struct disklabel *lp = dksc->sc_dkdev.dk_label;
560 1.1 elric struct cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
561 1.48 christos struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
562 1.1 elric struct partition *pp;
563 1.1 elric int i;
564 1.5 dsl const char *errstring;
565 1.1 elric
566 1.1 elric memset(clp, 0x0, sizeof(*clp));
567 1.1 elric dk_getdefaultlabel(di, dksc, lp);
568 1.1 elric errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
569 1.1 elric dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
570 1.1 elric if (errstring) {
571 1.1 elric dk_makedisklabel(di, dksc);
572 1.1 elric if (dksc->sc_flags & DKF_WARNLABEL)
573 1.1 elric printf("%s: %s\n", dksc->sc_xname, errstring);
574 1.1 elric return;
575 1.1 elric }
576 1.1 elric
577 1.1 elric if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
578 1.1 elric return;
579 1.1 elric
580 1.1 elric /* Sanity check */
581 1.51.2.2 martin if (lp->d_secperunit < UINT32_MAX ?
582 1.51.2.2 martin lp->d_secperunit != dg->dg_secperunit :
583 1.51.2.2 martin lp->d_secperunit > dg->dg_secperunit)
584 1.51.2.2 martin printf("WARNING: %s: total sector size in disklabel (%ju) "
585 1.51.2.2 martin "!= the size of %s (%ju)\n", dksc->sc_xname,
586 1.51.2.2 martin (uintmax_t)lp->d_secperunit, di->di_dkname,
587 1.51.2.2 martin (uintmax_t)dg->dg_secperunit);
588 1.1 elric
589 1.1 elric for (i=0; i < lp->d_npartitions; i++) {
590 1.1 elric pp = &lp->d_partitions[i];
591 1.48 christos if (pp->p_offset + pp->p_size > dg->dg_secperunit)
592 1.1 elric printf("WARNING: %s: end of partition `%c' exceeds "
593 1.51.2.2 martin "the size of %s (%ju)\n", dksc->sc_xname,
594 1.51.2.2 martin 'a' + i, di->di_dkname,
595 1.51.2.2 martin (uintmax_t)dg->dg_secperunit);
596 1.1 elric }
597 1.1 elric }
598 1.1 elric
599 1.1 elric /* ARGSUSED */
600 1.13 thorpej static void
601 1.27 christos dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
602 1.1 elric {
603 1.1 elric struct disklabel *lp = dksc->sc_dkdev.dk_label;
604 1.1 elric
605 1.1 elric lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
606 1.1 elric strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
607 1.1 elric lp->d_checksum = dkcksum(lp);
608 1.1 elric }
609 1.1 elric
610 1.1 elric /* This function is taken from ccd.c:1.76 --rcd */
611 1.1 elric
612 1.1 elric /*
613 1.1 elric * XXX this function looks too generic for dksubr.c, shouldn't we
614 1.1 elric * put it somewhere better?
615 1.1 elric */
616 1.1 elric
617 1.1 elric /*
618 1.1 elric * Lookup the provided name in the filesystem. If the file exists,
619 1.1 elric * is a valid block device, and isn't being used by anyone else,
620 1.1 elric * set *vpp to the file's vnode.
621 1.1 elric */
622 1.1 elric int
623 1.42 dholland dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
624 1.1 elric {
625 1.1 elric struct nameidata nd;
626 1.1 elric struct vnode *vp;
627 1.24 christos int error;
628 1.1 elric
629 1.24 christos if (l == NULL)
630 1.24 christos return ESRCH; /* Is ESRCH the best choice? */
631 1.24 christos
632 1.42 dholland NDINIT(&nd, LOOKUP, FOLLOW, pb);
633 1.24 christos if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
634 1.1 elric DPRINTF((DKDB_FOLLOW|DKDB_INIT),
635 1.1 elric ("dk_lookup: vn_open error = %d\n", error));
636 1.24 christos return error;
637 1.1 elric }
638 1.24 christos
639 1.1 elric vp = nd.ni_vp;
640 1.51 hannken if (vp->v_type != VBLK) {
641 1.51 hannken error = ENOTBLK;
642 1.24 christos goto out;
643 1.1 elric }
644 1.1 elric
645 1.51 hannken /* Reopen as anonymous vnode to protect against forced unmount. */
646 1.51 hannken if ((error = bdevvp(vp->v_rdev, vpp)) != 0)
647 1.24 christos goto out;
648 1.51 hannken VOP_UNLOCK(vp);
649 1.51 hannken if ((error = vn_close(vp, FREAD | FWRITE, l->l_cred)) != 0) {
650 1.51 hannken vrele(*vpp);
651 1.51 hannken return error;
652 1.51 hannken }
653 1.51 hannken if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
654 1.51 hannken vrele(*vpp);
655 1.51 hannken return error;
656 1.24 christos }
657 1.51 hannken mutex_enter((*vpp)->v_interlock);
658 1.51 hannken (*vpp)->v_writecount++;
659 1.51 hannken mutex_exit((*vpp)->v_interlock);
660 1.24 christos
661 1.51 hannken IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", *vpp));
662 1.1 elric
663 1.24 christos return 0;
664 1.24 christos out:
665 1.41 hannken VOP_UNLOCK(vp);
666 1.35 ad (void) vn_close(vp, FREAD | FWRITE, l->l_cred);
667 1.24 christos return error;
668 1.1 elric }
669 1.49 pgoyette
670 1.49 pgoyette MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
671 1.49 pgoyette
672 1.49 pgoyette static int
673 1.49 pgoyette dk_subr_modcmd(modcmd_t cmd, void *arg)
674 1.49 pgoyette {
675 1.49 pgoyette switch (cmd) {
676 1.49 pgoyette case MODULE_CMD_INIT:
677 1.49 pgoyette case MODULE_CMD_FINI:
678 1.49 pgoyette return 0;
679 1.49 pgoyette case MODULE_CMD_STAT:
680 1.49 pgoyette case MODULE_CMD_AUTOUNLOAD:
681 1.49 pgoyette default:
682 1.49 pgoyette return ENOTTY;
683 1.49 pgoyette }
684 1.49 pgoyette }
685