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