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