dksubr.c revision 1.55 1 1.55 mlelstv /* $NetBSD: dksubr.c,v 1.55 2014/12/29 12:03:39 mlelstv 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.55 mlelstv __KERNEL_RCSID(0, "$NetBSD: dksubr.c,v 1.55 2014/12/29 12:03:39 mlelstv 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.55 mlelstv int s, part;
182 1.1 elric int wlabel;
183 1.18 yamt daddr_t blkno;
184 1.55 mlelstv struct disklabel *lp;
185 1.55 mlelstv struct disk *dk;
186 1.55 mlelstv uint64_t numsecs;
187 1.55 mlelstv unsigned secsize;
188 1.1 elric
189 1.1 elric DPRINTF_FOLLOW(("dk_strategy(%s, %p, %p)\n",
190 1.1 elric di->di_dkname, dksc, bp));
191 1.1 elric
192 1.1 elric if (!(dksc->sc_flags & DKF_INITED)) {
193 1.25 dan DPRINTF_FOLLOW(("dk_strategy: not inited\n"));
194 1.1 elric bp->b_error = ENXIO;
195 1.2 elric biodone(bp);
196 1.1 elric return;
197 1.1 elric }
198 1.1 elric
199 1.55 mlelstv lp = dksc->sc_dkdev.dk_label;
200 1.55 mlelstv dk = &dksc->sc_dkdev;
201 1.55 mlelstv
202 1.55 mlelstv part = DISKPART(bp->b_dev);
203 1.55 mlelstv numsecs = dk->dk_geom.dg_secperunit;
204 1.55 mlelstv secsize = dk->dk_geom.dg_secsize;
205 1.1 elric
206 1.1 elric bp->b_resid = bp->b_bcount;
207 1.1 elric
208 1.55 mlelstv /*
209 1.55 mlelstv * The transfer must be a whole number of blocks and the offset must
210 1.55 mlelstv * not be negative.
211 1.55 mlelstv */
212 1.55 mlelstv if ((bp->b_bcount % secsize) != 0 || bp->b_blkno < 0) {
213 1.55 mlelstv bp->b_error = EINVAL;
214 1.55 mlelstv biodone(bp);
215 1.55 mlelstv return;
216 1.55 mlelstv }
217 1.55 mlelstv
218 1.1 elric /* If there is nothing to do, then we are done */
219 1.1 elric if (bp->b_bcount == 0) {
220 1.1 elric biodone(bp);
221 1.1 elric return;
222 1.1 elric }
223 1.1 elric
224 1.1 elric wlabel = dksc->sc_flags & (DKF_WLABEL|DKF_LABELLING);
225 1.55 mlelstv if (part == RAW_PART) {
226 1.55 mlelstv if (bounds_check_with_mediasize(bp, DEV_BSIZE, numsecs) <= 0) {
227 1.55 mlelstv biodone(bp);
228 1.55 mlelstv return;
229 1.55 mlelstv }
230 1.55 mlelstv } else {
231 1.55 mlelstv if (bounds_check_with_label(&dksc->sc_dkdev, bp, wlabel) <= 0) {
232 1.55 mlelstv biodone(bp);
233 1.55 mlelstv return;
234 1.55 mlelstv }
235 1.1 elric }
236 1.1 elric
237 1.18 yamt blkno = bp->b_blkno;
238 1.55 mlelstv if (part != RAW_PART)
239 1.55 mlelstv blkno += lp->d_partitions[DISKPART(bp->b_dev)].p_offset;
240 1.18 yamt bp->b_rawblkno = blkno;
241 1.18 yamt
242 1.1 elric /*
243 1.1 elric * Start the unit by calling the start routine
244 1.1 elric * provided by the individual driver.
245 1.1 elric */
246 1.1 elric s = splbio();
247 1.40 yamt bufq_put(dksc->sc_bufq, bp);
248 1.50 bouyer di->di_diskstart(dksc);
249 1.1 elric splx(s);
250 1.1 elric return;
251 1.1 elric }
252 1.1 elric
253 1.1 elric int
254 1.1 elric dk_size(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
255 1.1 elric {
256 1.1 elric struct disklabel *lp;
257 1.1 elric int is_open;
258 1.1 elric int part;
259 1.1 elric int size;
260 1.1 elric
261 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
262 1.1 elric return -1;
263 1.1 elric
264 1.1 elric part = DISKPART(dev);
265 1.1 elric is_open = dksc->sc_dkdev.dk_openmask & (1 << part);
266 1.1 elric
267 1.19 christos if (!is_open && di->di_open(dev, 0, S_IFBLK, curlwp))
268 1.1 elric return -1;
269 1.1 elric
270 1.1 elric lp = dksc->sc_dkdev.dk_label;
271 1.1 elric if (lp->d_partitions[part].p_fstype != FS_SWAP)
272 1.1 elric size = -1;
273 1.1 elric else
274 1.1 elric size = lp->d_partitions[part].p_size *
275 1.1 elric (lp->d_secsize / DEV_BSIZE);
276 1.1 elric
277 1.19 christos if (!is_open && di->di_close(dev, 0, S_IFBLK, curlwp))
278 1.1 elric return 1;
279 1.1 elric
280 1.1 elric return size;
281 1.1 elric }
282 1.1 elric
283 1.1 elric int
284 1.1 elric dk_ioctl(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
285 1.28 christos u_long cmd, void *data, int flag, struct lwp *l)
286 1.1 elric {
287 1.1 elric struct disklabel *lp;
288 1.16 yamt struct disk *dk;
289 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
290 1.1 elric struct disklabel newlabel;
291 1.1 elric #endif
292 1.1 elric int error = 0;
293 1.1 elric
294 1.39 cegger DPRINTF_FOLLOW(("dk_ioctl(%s, %p, 0x%"PRIx64", 0x%lx)\n",
295 1.1 elric di->di_dkname, dksc, dev, cmd));
296 1.1 elric
297 1.1 elric /* ensure that the pseudo disk is open for writes for these commands */
298 1.1 elric switch (cmd) {
299 1.1 elric case DIOCSDINFO:
300 1.1 elric case DIOCWDINFO:
301 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
302 1.1 elric case ODIOCSDINFO:
303 1.1 elric case ODIOCWDINFO:
304 1.1 elric #endif
305 1.1 elric case DIOCWLABEL:
306 1.43 elric case DIOCAWEDGE:
307 1.43 elric case DIOCDWEDGE:
308 1.1 elric if ((flag & FWRITE) == 0)
309 1.1 elric return EBADF;
310 1.1 elric }
311 1.1 elric
312 1.1 elric /* ensure that the pseudo-disk is initialized for these */
313 1.1 elric switch (cmd) {
314 1.43 elric #ifdef DIOCGSECTORSIZE
315 1.43 elric case DIOCGSECTORSIZE:
316 1.43 elric case DIOCGMEDIASIZE:
317 1.43 elric #endif
318 1.1 elric case DIOCGDINFO:
319 1.1 elric case DIOCSDINFO:
320 1.1 elric case DIOCWDINFO:
321 1.1 elric case DIOCGPART:
322 1.1 elric case DIOCWLABEL:
323 1.1 elric case DIOCGDEFLABEL:
324 1.43 elric case DIOCAWEDGE:
325 1.43 elric case DIOCDWEDGE:
326 1.43 elric case DIOCLWEDGES:
327 1.54 mlelstv case DIOCMWEDGES:
328 1.43 elric case DIOCCACHESYNC:
329 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
330 1.1 elric case ODIOCGDINFO:
331 1.1 elric case ODIOCSDINFO:
332 1.1 elric case ODIOCWDINFO:
333 1.1 elric case ODIOCGDEFLABEL:
334 1.1 elric #endif
335 1.1 elric if ((dksc->sc_flags & DKF_INITED) == 0)
336 1.1 elric return ENXIO;
337 1.1 elric }
338 1.1 elric
339 1.1 elric switch (cmd) {
340 1.43 elric #ifdef DIOCGSECTORSIZE
341 1.43 elric case DIOCGSECTORSIZE:
342 1.47 christos *(u_int *)data = dksc->sc_dkdev.dk_geom.dg_secsize;
343 1.43 elric return 0;
344 1.43 elric case DIOCGMEDIASIZE:
345 1.43 elric *(off_t *)data =
346 1.47 christos (off_t)dksc->sc_dkdev.dk_geom.dg_secsize *
347 1.47 christos dksc->sc_dkdev.dk_geom.dg_nsectors;
348 1.43 elric return 0;
349 1.43 elric #endif
350 1.43 elric
351 1.1 elric case DIOCGDINFO:
352 1.1 elric *(struct disklabel *)data = *(dksc->sc_dkdev.dk_label);
353 1.1 elric break;
354 1.1 elric
355 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
356 1.1 elric case ODIOCGDINFO:
357 1.1 elric newlabel = *(dksc->sc_dkdev.dk_label);
358 1.1 elric if (newlabel.d_npartitions > OLDMAXPARTITIONS)
359 1.1 elric return ENOTTY;
360 1.1 elric memcpy(data, &newlabel, sizeof (struct olddisklabel));
361 1.1 elric break;
362 1.1 elric #endif
363 1.1 elric
364 1.1 elric case DIOCGPART:
365 1.1 elric ((struct partinfo *)data)->disklab = dksc->sc_dkdev.dk_label;
366 1.1 elric ((struct partinfo *)data)->part =
367 1.1 elric &dksc->sc_dkdev.dk_label->d_partitions[DISKPART(dev)];
368 1.1 elric break;
369 1.1 elric
370 1.1 elric case DIOCWDINFO:
371 1.1 elric case DIOCSDINFO:
372 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
373 1.1 elric case ODIOCWDINFO:
374 1.1 elric case ODIOCSDINFO:
375 1.1 elric #endif
376 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
377 1.1 elric if (cmd == ODIOCSDINFO || cmd == ODIOCWDINFO) {
378 1.1 elric memset(&newlabel, 0, sizeof newlabel);
379 1.1 elric memcpy(&newlabel, data, sizeof (struct olddisklabel));
380 1.1 elric lp = &newlabel;
381 1.1 elric } else
382 1.1 elric #endif
383 1.1 elric lp = (struct disklabel *)data;
384 1.1 elric
385 1.16 yamt dk = &dksc->sc_dkdev;
386 1.30 ad mutex_enter(&dk->dk_openlock);
387 1.1 elric dksc->sc_flags |= DKF_LABELLING;
388 1.1 elric
389 1.1 elric error = setdisklabel(dksc->sc_dkdev.dk_label,
390 1.1 elric lp, 0, dksc->sc_dkdev.dk_cpulabel);
391 1.1 elric if (error == 0) {
392 1.1 elric if (cmd == DIOCWDINFO
393 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
394 1.1 elric || cmd == ODIOCWDINFO
395 1.1 elric #endif
396 1.1 elric )
397 1.1 elric error = writedisklabel(DKLABELDEV(dev),
398 1.1 elric di->di_strategy, dksc->sc_dkdev.dk_label,
399 1.1 elric dksc->sc_dkdev.dk_cpulabel);
400 1.1 elric }
401 1.1 elric
402 1.1 elric dksc->sc_flags &= ~DKF_LABELLING;
403 1.30 ad mutex_exit(&dk->dk_openlock);
404 1.1 elric break;
405 1.1 elric
406 1.1 elric case DIOCWLABEL:
407 1.1 elric if (*(int *)data != 0)
408 1.1 elric dksc->sc_flags |= DKF_WLABEL;
409 1.1 elric else
410 1.1 elric dksc->sc_flags &= ~DKF_WLABEL;
411 1.1 elric break;
412 1.1 elric
413 1.1 elric case DIOCGDEFLABEL:
414 1.1 elric dk_getdefaultlabel(di, dksc, (struct disklabel *)data);
415 1.1 elric break;
416 1.1 elric
417 1.1 elric #ifdef __HAVE_OLD_DISKLABEL
418 1.1 elric case ODIOCGDEFLABEL:
419 1.1 elric dk_getdefaultlabel(di, dksc, &newlabel);
420 1.1 elric if (newlabel.d_npartitions > OLDMAXPARTITIONS)
421 1.1 elric return ENOTTY;
422 1.1 elric memcpy(data, &newlabel, sizeof (struct olddisklabel));
423 1.1 elric break;
424 1.1 elric #endif
425 1.1 elric
426 1.16 yamt case DIOCAWEDGE:
427 1.16 yamt {
428 1.16 yamt struct dkwedge_info *dkw = (void *)data;
429 1.16 yamt
430 1.16 yamt if ((flag & FWRITE) == 0)
431 1.16 yamt return (EBADF);
432 1.16 yamt
433 1.16 yamt /* If the ioctl happens here, the parent is us. */
434 1.16 yamt strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
435 1.16 yamt return (dkwedge_add(dkw));
436 1.16 yamt }
437 1.16 yamt
438 1.16 yamt case DIOCDWEDGE:
439 1.16 yamt {
440 1.16 yamt struct dkwedge_info *dkw = (void *)data;
441 1.16 yamt
442 1.16 yamt if ((flag & FWRITE) == 0)
443 1.16 yamt return (EBADF);
444 1.16 yamt
445 1.16 yamt /* If the ioctl happens here, the parent is us. */
446 1.16 yamt strcpy(dkw->dkw_parent, dksc->sc_dkdev.dk_name);
447 1.16 yamt return (dkwedge_del(dkw));
448 1.16 yamt }
449 1.16 yamt
450 1.16 yamt case DIOCLWEDGES:
451 1.16 yamt {
452 1.16 yamt struct dkwedge_list *dkwl = (void *)data;
453 1.16 yamt
454 1.20 rpaulo return (dkwedge_list(&dksc->sc_dkdev, dkwl, l));
455 1.16 yamt }
456 1.16 yamt
457 1.54 mlelstv case DIOCMWEDGES:
458 1.54 mlelstv {
459 1.54 mlelstv if ((flag & FWRITE) == 0)
460 1.54 mlelstv return (EBADF);
461 1.54 mlelstv
462 1.54 mlelstv dkwedge_discover(&dksc->sc_dkdev);
463 1.54 mlelstv return 0;
464 1.54 mlelstv }
465 1.54 mlelstv
466 1.21 yamt case DIOCGSTRATEGY:
467 1.21 yamt {
468 1.21 yamt struct disk_strategy *dks = (void *)data;
469 1.21 yamt int s;
470 1.21 yamt
471 1.21 yamt s = splbio();
472 1.21 yamt strlcpy(dks->dks_name, bufq_getstrategyname(dksc->sc_bufq),
473 1.21 yamt sizeof(dks->dks_name));
474 1.21 yamt splx(s);
475 1.21 yamt dks->dks_paramlen = 0;
476 1.21 yamt
477 1.21 yamt return 0;
478 1.21 yamt }
479 1.21 yamt
480 1.21 yamt case DIOCSSTRATEGY:
481 1.21 yamt {
482 1.21 yamt struct disk_strategy *dks = (void *)data;
483 1.21 yamt struct bufq_state *new;
484 1.21 yamt struct bufq_state *old;
485 1.21 yamt int s;
486 1.21 yamt
487 1.21 yamt if ((flag & FWRITE) == 0) {
488 1.21 yamt return EBADF;
489 1.21 yamt }
490 1.21 yamt if (dks->dks_param != NULL) {
491 1.21 yamt return EINVAL;
492 1.21 yamt }
493 1.21 yamt dks->dks_name[sizeof(dks->dks_name) - 1] = 0; /* ensure term */
494 1.21 yamt error = bufq_alloc(&new, dks->dks_name,
495 1.21 yamt BUFQ_EXACT|BUFQ_SORT_RAWBLOCK);
496 1.21 yamt if (error) {
497 1.21 yamt return error;
498 1.21 yamt }
499 1.21 yamt s = splbio();
500 1.21 yamt old = dksc->sc_bufq;
501 1.21 yamt bufq_move(new, old);
502 1.21 yamt dksc->sc_bufq = new;
503 1.21 yamt splx(s);
504 1.21 yamt bufq_free(old);
505 1.21 yamt
506 1.21 yamt return 0;
507 1.21 yamt }
508 1.21 yamt
509 1.1 elric default:
510 1.1 elric error = ENOTTY;
511 1.1 elric }
512 1.1 elric
513 1.1 elric return error;
514 1.1 elric }
515 1.1 elric
516 1.1 elric /*
517 1.1 elric * dk_dump dumps all of physical memory into the partition specified.
518 1.1 elric * This requires substantially more framework than {s,w}ddump, and hence
519 1.1 elric * is probably much more fragile.
520 1.1 elric *
521 1.1 elric * XXX: we currently do not implement this.
522 1.1 elric */
523 1.1 elric
524 1.1 elric #define DKF_READYFORDUMP (DKF_INITED|DKF_TAKEDUMP)
525 1.1 elric #define DKFF_READYFORDUMP(x) (((x) & DKF_READYFORDUMP) == DKF_READYFORDUMP)
526 1.1 elric static volatile int dk_dumping = 0;
527 1.1 elric
528 1.1 elric /* ARGSUSED */
529 1.1 elric int
530 1.27 christos dk_dump(struct dk_intf *di, struct dk_softc *dksc, dev_t dev,
531 1.28 christos daddr_t blkno, void *va, size_t size)
532 1.1 elric {
533 1.1 elric
534 1.1 elric /*
535 1.1 elric * ensure that we consider this device to be safe for dumping,
536 1.1 elric * and that the device is configured.
537 1.1 elric */
538 1.1 elric if (!DKFF_READYFORDUMP(dksc->sc_flags))
539 1.1 elric return ENXIO;
540 1.1 elric
541 1.1 elric /* ensure that we are not already dumping */
542 1.1 elric if (dk_dumping)
543 1.1 elric return EFAULT;
544 1.1 elric dk_dumping = 1;
545 1.1 elric
546 1.1 elric /* XXX: unimplemented */
547 1.1 elric
548 1.1 elric dk_dumping = 0;
549 1.1 elric
550 1.1 elric /* XXX: actually for now, we are going to leave this alone */
551 1.1 elric return ENXIO;
552 1.1 elric }
553 1.1 elric
554 1.1 elric /* ARGSUSED */
555 1.1 elric void
556 1.1 elric dk_getdefaultlabel(struct dk_intf *di, struct dk_softc *dksc,
557 1.1 elric struct disklabel *lp)
558 1.1 elric {
559 1.47 christos struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
560 1.4 elric
561 1.4 elric memset(lp, 0, sizeof(*lp));
562 1.1 elric
563 1.52 mlelstv if (dg->dg_secperunit > UINT32_MAX)
564 1.52 mlelstv lp->d_secperunit = UINT32_MAX;
565 1.52 mlelstv else
566 1.52 mlelstv lp->d_secperunit = dg->dg_secperunit;
567 1.47 christos lp->d_secsize = dg->dg_secsize;
568 1.47 christos lp->d_nsectors = dg->dg_nsectors;
569 1.47 christos lp->d_ntracks = dg->dg_ntracks;
570 1.47 christos lp->d_ncylinders = dg->dg_ncylinders;
571 1.1 elric lp->d_secpercyl = lp->d_ntracks * lp->d_nsectors;
572 1.1 elric
573 1.1 elric strncpy(lp->d_typename, di->di_dkname, sizeof(lp->d_typename));
574 1.1 elric lp->d_type = di->di_dtype;
575 1.1 elric strncpy(lp->d_packname, "fictitious", sizeof(lp->d_packname));
576 1.1 elric lp->d_rpm = 3600;
577 1.1 elric lp->d_interleave = 1;
578 1.1 elric lp->d_flags = 0;
579 1.1 elric
580 1.1 elric lp->d_partitions[RAW_PART].p_offset = 0;
581 1.52 mlelstv lp->d_partitions[RAW_PART].p_size = lp->d_secperunit;
582 1.1 elric lp->d_partitions[RAW_PART].p_fstype = FS_UNUSED;
583 1.1 elric lp->d_npartitions = RAW_PART + 1;
584 1.1 elric
585 1.1 elric lp->d_magic = DISKMAGIC;
586 1.1 elric lp->d_magic2 = DISKMAGIC;
587 1.1 elric lp->d_checksum = dkcksum(dksc->sc_dkdev.dk_label);
588 1.1 elric }
589 1.1 elric
590 1.1 elric /* ARGSUSED */
591 1.1 elric void
592 1.1 elric dk_getdisklabel(struct dk_intf *di, struct dk_softc *dksc, dev_t dev)
593 1.1 elric {
594 1.1 elric struct disklabel *lp = dksc->sc_dkdev.dk_label;
595 1.1 elric struct cpu_disklabel *clp = dksc->sc_dkdev.dk_cpulabel;
596 1.48 christos struct disk_geom *dg = &dksc->sc_dkdev.dk_geom;
597 1.1 elric struct partition *pp;
598 1.1 elric int i;
599 1.5 dsl const char *errstring;
600 1.1 elric
601 1.1 elric memset(clp, 0x0, sizeof(*clp));
602 1.1 elric dk_getdefaultlabel(di, dksc, lp);
603 1.1 elric errstring = readdisklabel(DKLABELDEV(dev), di->di_strategy,
604 1.1 elric dksc->sc_dkdev.dk_label, dksc->sc_dkdev.dk_cpulabel);
605 1.1 elric if (errstring) {
606 1.1 elric dk_makedisklabel(di, dksc);
607 1.1 elric if (dksc->sc_flags & DKF_WARNLABEL)
608 1.1 elric printf("%s: %s\n", dksc->sc_xname, errstring);
609 1.1 elric return;
610 1.1 elric }
611 1.1 elric
612 1.1 elric if ((dksc->sc_flags & DKF_LABELSANITY) == 0)
613 1.1 elric return;
614 1.1 elric
615 1.1 elric /* Sanity check */
616 1.53 mlelstv if (lp->d_secperunit < UINT32_MAX ?
617 1.53 mlelstv lp->d_secperunit != dg->dg_secperunit :
618 1.53 mlelstv lp->d_secperunit > dg->dg_secperunit)
619 1.53 mlelstv printf("WARNING: %s: total sector size in disklabel (%ju) "
620 1.53 mlelstv "!= the size of %s (%ju)\n", dksc->sc_xname,
621 1.53 mlelstv (uintmax_t)lp->d_secperunit, di->di_dkname,
622 1.53 mlelstv (uintmax_t)dg->dg_secperunit);
623 1.1 elric
624 1.1 elric for (i=0; i < lp->d_npartitions; i++) {
625 1.1 elric pp = &lp->d_partitions[i];
626 1.48 christos if (pp->p_offset + pp->p_size > dg->dg_secperunit)
627 1.1 elric printf("WARNING: %s: end of partition `%c' exceeds "
628 1.53 mlelstv "the size of %s (%ju)\n", dksc->sc_xname,
629 1.53 mlelstv 'a' + i, di->di_dkname,
630 1.53 mlelstv (uintmax_t)dg->dg_secperunit);
631 1.1 elric }
632 1.1 elric }
633 1.1 elric
634 1.1 elric /* ARGSUSED */
635 1.13 thorpej static void
636 1.27 christos dk_makedisklabel(struct dk_intf *di, struct dk_softc *dksc)
637 1.1 elric {
638 1.1 elric struct disklabel *lp = dksc->sc_dkdev.dk_label;
639 1.1 elric
640 1.1 elric lp->d_partitions[RAW_PART].p_fstype = FS_BSDFFS;
641 1.1 elric strncpy(lp->d_packname, "default label", sizeof(lp->d_packname));
642 1.1 elric lp->d_checksum = dkcksum(lp);
643 1.1 elric }
644 1.1 elric
645 1.1 elric /* This function is taken from ccd.c:1.76 --rcd */
646 1.1 elric
647 1.1 elric /*
648 1.1 elric * XXX this function looks too generic for dksubr.c, shouldn't we
649 1.1 elric * put it somewhere better?
650 1.1 elric */
651 1.1 elric
652 1.1 elric /*
653 1.1 elric * Lookup the provided name in the filesystem. If the file exists,
654 1.1 elric * is a valid block device, and isn't being used by anyone else,
655 1.1 elric * set *vpp to the file's vnode.
656 1.1 elric */
657 1.1 elric int
658 1.42 dholland dk_lookup(struct pathbuf *pb, struct lwp *l, struct vnode **vpp)
659 1.1 elric {
660 1.1 elric struct nameidata nd;
661 1.1 elric struct vnode *vp;
662 1.24 christos int error;
663 1.1 elric
664 1.24 christos if (l == NULL)
665 1.24 christos return ESRCH; /* Is ESRCH the best choice? */
666 1.24 christos
667 1.42 dholland NDINIT(&nd, LOOKUP, FOLLOW, pb);
668 1.24 christos if ((error = vn_open(&nd, FREAD | FWRITE, 0)) != 0) {
669 1.1 elric DPRINTF((DKDB_FOLLOW|DKDB_INIT),
670 1.1 elric ("dk_lookup: vn_open error = %d\n", error));
671 1.24 christos return error;
672 1.1 elric }
673 1.24 christos
674 1.1 elric vp = nd.ni_vp;
675 1.51 hannken if (vp->v_type != VBLK) {
676 1.51 hannken error = ENOTBLK;
677 1.24 christos goto out;
678 1.1 elric }
679 1.1 elric
680 1.51 hannken /* Reopen as anonymous vnode to protect against forced unmount. */
681 1.51 hannken if ((error = bdevvp(vp->v_rdev, vpp)) != 0)
682 1.24 christos goto out;
683 1.51 hannken VOP_UNLOCK(vp);
684 1.51 hannken if ((error = vn_close(vp, FREAD | FWRITE, l->l_cred)) != 0) {
685 1.51 hannken vrele(*vpp);
686 1.51 hannken return error;
687 1.51 hannken }
688 1.51 hannken if ((error = VOP_OPEN(*vpp, FREAD | FWRITE, l->l_cred)) != 0) {
689 1.51 hannken vrele(*vpp);
690 1.51 hannken return error;
691 1.24 christos }
692 1.51 hannken mutex_enter((*vpp)->v_interlock);
693 1.51 hannken (*vpp)->v_writecount++;
694 1.51 hannken mutex_exit((*vpp)->v_interlock);
695 1.24 christos
696 1.51 hannken IFDEBUG(DKDB_VNODE, vprint("dk_lookup: vnode info", *vpp));
697 1.1 elric
698 1.24 christos return 0;
699 1.24 christos out:
700 1.41 hannken VOP_UNLOCK(vp);
701 1.35 ad (void) vn_close(vp, FREAD | FWRITE, l->l_cred);
702 1.24 christos return error;
703 1.1 elric }
704 1.49 pgoyette
705 1.49 pgoyette MODULE(MODULE_CLASS_MISC, dk_subr, NULL);
706 1.49 pgoyette
707 1.49 pgoyette static int
708 1.49 pgoyette dk_subr_modcmd(modcmd_t cmd, void *arg)
709 1.49 pgoyette {
710 1.49 pgoyette switch (cmd) {
711 1.49 pgoyette case MODULE_CMD_INIT:
712 1.49 pgoyette case MODULE_CMD_FINI:
713 1.49 pgoyette return 0;
714 1.49 pgoyette case MODULE_CMD_STAT:
715 1.49 pgoyette case MODULE_CMD_AUTOUNLOAD:
716 1.49 pgoyette default:
717 1.49 pgoyette return ENOTTY;
718 1.49 pgoyette }
719 1.49 pgoyette }
720