Add AGP support for the i852GM, i855GM, and i865G.
[dragonfly.git] / sys / kern / vfs_conf.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1999 Michael Smith
3 * All rights reserved.
4 * Copyright (c) 1999 Poul-Henning Kamp
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 *
28 * $FreeBSD: src/sys/kern/vfs_conf.c,v 1.49.2.5 2003/01/07 11:56:53 joerg Exp $
3281b7bd 29 * $DragonFly: src/sys/kern/vfs_conf.c,v 1.9 2004/05/31 17:38:08 dillon Exp $
984263bc
MD
30 */
31
32/*
33 * Locate and mount the root filesystem.
34 *
35 * The root filesystem is detailed in the kernel environment variable
36 * vfs.root.mountfrom, which is expected to be in the general format
37 *
38 * <vfsname>:[<path>]
39 * vfsname := the name of a VFS known to the kernel and capable
40 * of being mounted as root
41 * path := disk device name or other data used by the filesystem
42 * to locate its physical store
43 *
44 */
45
46#include "opt_rootdevname.h"
47
48#include <sys/param.h>
49#include <sys/kernel.h>
50#include <sys/systm.h>
51#include <sys/proc.h>
52#include <sys/vnode.h>
53#include <sys/mount.h>
54#include <sys/malloc.h>
55#include <sys/reboot.h>
56#include <sys/diskslice.h>
57#include <sys/disklabel.h>
58#include <sys/conf.h>
59#include <sys/cons.h>
335dda38 60#include <sys/device.h>
1f2de5d4 61#include <sys/paths.h>
984263bc
MD
62
63#include "opt_ddb.h"
64#ifdef DDB
65#include <ddb/ddb.h>
66#endif
67
68MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
69
70#define ROOTNAME "root_device"
71
72struct vnode *rootvnode;
73
74/*
75 * The root specifiers we will try if RB_CDROM is specified.
76 */
77static char *cdrom_rootdevnames[] = {
78 "cd9660:cd0a",
79 "cd9660:acd0a",
80 "cd9660:wcd0a",
81 NULL
82};
83
84static void vfs_mountroot(void *junk);
85static int vfs_mountroot_try(char *mountfrom);
86static int vfs_mountroot_ask(void);
87static void gets(char *cp);
88
89/* legacy find-root code */
90char *rootdevnames[2] = {NULL, NULL};
91static int setrootbyname(char *name);
92
93SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
94
95/*
96 * Find and mount the root filesystem
97 */
98static void
99vfs_mountroot(void *junk)
100{
101 int i;
102
103 /*
104 * The root filesystem information is compiled in, and we are
105 * booted with instructions to use it.
106 */
107#ifdef ROOTDEVNAME
108 if ((boothowto & RB_DFLTROOT) &&
109 !vfs_mountroot_try(ROOTDEVNAME))
110 return;
111#endif
112 /*
113 * We are booted with instructions to prompt for the root filesystem,
114 * or to use the compiled-in default when it doesn't exist.
115 */
116 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
117 if (!vfs_mountroot_ask())
118 return;
119 }
120
121 /*
122 * We've been given the generic "use CDROM as root" flag. This is
123 * necessary because one media may be used in many different
124 * devices, so we need to search for them.
125 */
126 if (boothowto & RB_CDROM) {
127 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
128 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
129 return;
130 }
131 }
132
133 /*
134 * Try to use the value read by the loader from /etc/fstab, or
135 * supplied via some other means. This is the preferred
136 * mechanism.
137 */
138 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
139 return;
140
141 /*
142 * Try values that may have been computed by the machine-dependant
143 * legacy code.
144 */
145 if (!vfs_mountroot_try(rootdevnames[0]))
146 return;
147 if (!vfs_mountroot_try(rootdevnames[1]))
148 return;
149
150 /*
151 * If we have a compiled-in default, and haven't already tried it, try
152 * it now.
153 */
154#ifdef ROOTDEVNAME
155 if (!(boothowto & RB_DFLTROOT))
156 if (!vfs_mountroot_try(ROOTDEVNAME))
157 return;
158#endif
159
160 /*
161 * Everything so far has failed, prompt on the console if we haven't
162 * already tried that.
163 */
164 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
165 return;
166 panic("Root mount failed, startup aborted.");
167}
168
169/*
170 * Mount (mountfrom) as the root filesystem.
171 */
172static int
173vfs_mountroot_try(char *mountfrom)
174{
dadab5e9 175 struct thread *td = curthread;
984263bc
MD
176 struct mount *mp;
177 char *vfsname, *path;
178 int error;
179 char patt[32];
41a01a4d 180 lwkt_tokref ilock;
984263bc
MD
181 int s;
182
183 vfsname = NULL;
184 path = NULL;
185 mp = NULL;
186 error = EINVAL;
187
188 if (mountfrom == NULL)
189 return(error); /* don't complain */
190
191 s = splcam(); /* Overkill, but annoying without it */
192 printf("Mounting root from %s\n", mountfrom);
193 splx(s);
194
195 /* parse vfs name and path */
196 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
197 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
198 vfsname[0] = path[0] = 0;
199 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
200 if (sscanf(mountfrom, patt, vfsname, path) < 1)
201 goto done;
202
203 /* allocate a root mount */
204 error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
205 &mp);
206 if (error != 0) {
207 printf("Can't allocate root mount for filesystem '%s': %d\n",
208 vfsname, error);
209 goto done;
210 }
211 mp->mnt_flag |= MNT_ROOTFS;
212
213 /* do our best to set rootdev */
214 if ((path[0] != 0) && setrootbyname(path))
215 printf("setrootbyname failed\n");
216
217 /* If the root device is a type "memory disk", mount RW */
e4c9c0c8 218 if (rootdev != NODEV && dev_is_good(rootdev) &&
335dda38 219 (dev_dflags(rootdev) & D_MEMDISK)) {
984263bc 220 mp->mnt_flag &= ~MNT_RDONLY;
335dda38 221 }
984263bc 222
dadab5e9 223 error = VFS_MOUNT(mp, NULL, NULL, NULL, td);
984263bc
MD
224
225done:
226 if (vfsname != NULL)
227 free(vfsname, M_MOUNT);
228 if (path != NULL)
229 free(path, M_MOUNT);
230 if (error != 0) {
231 if (mp != NULL) {
dadab5e9 232 vfs_unbusy(mp, td);
984263bc
MD
233 free(mp, M_MOUNT);
234 }
235 printf("Root mount failed: %d\n", error);
236 } else {
237
238 /* register with list of mounted filesystems */
41a01a4d 239 lwkt_gettoken(&ilock, &mountlist_token);
984263bc 240 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
41a01a4d 241 lwkt_reltoken(&ilock);
984263bc 242
8a8d5d85 243 /* sanity check system clock against root fs timestamp */
984263bc 244 inittodr(mp->mnt_time);
dadab5e9 245 vfs_unbusy(mp, td);
984263bc
MD
246 }
247 return(error);
248}
249
250/*
251 * Spin prompting on the console for a suitable root filesystem
252 */
253static int
254vfs_mountroot_ask(void)
255{
256 char name[128];
257 int i;
258 dev_t dev;
259
260 for(;;) {
261 printf("\nManual root filesystem specification:\n");
262 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
1f2de5d4 263 printf(" eg. ufs:%sda0s1a\n", __SYS_PATH_DEV);
984263bc
MD
264 printf(" ? List valid disk boot devices\n");
265 printf(" <empty line> Abort manual input\n");
266 printf("\nmountroot> ");
267 gets(name);
268 if (name[0] == 0)
269 return(1);
270 if (name[0] == '?') {
271 printf("Possibly valid devices for 'ufs' root:\n");
272 for (i = 0; i < NUMCDEVSW; i++) {
e4c9c0c8
MD
273 dev = udev2dev(makeudev(i, 0), 0);
274 if (dev_is_good(dev))
335dda38 275 printf(" \"%s\"", dev_dname(dev));
984263bc
MD
276 }
277 printf("\n");
278 continue;
279 }
280 if (!vfs_mountroot_try(name))
281 return(0);
282 }
283}
284
285static void
286gets(char *cp)
287{
288 char *lp;
289 int c;
290
291 lp = cp;
292 for (;;) {
293 printf("%c", c = cngetc() & 0177);
294 switch (c) {
295 case -1:
296 case '\n':
297 case '\r':
298 *lp++ = '\0';
299 return;
300 case '\b':
301 case '\177':
302 if (lp > cp) {
303 printf(" \b");
304 lp--;
305 }
306 continue;
307 case '#':
308 lp--;
309 if (lp < cp)
310 lp = cp;
311 continue;
312 case '@':
313 case 'u' & 037:
314 lp = cp;
315 printf("%c", '\n');
316 continue;
317 default:
318 *lp++ = c;
319 }
320 }
321}
322
323/*
324 * Convert a given name to the dev_t of the disk-like device
325 * it refers to.
326 */
327dev_t
e4c9c0c8
MD
328getdiskbyname(char *name)
329{
984263bc
MD
330 char *cp;
331 int cd, unit, slice, part;
332 dev_t dev;
e4c9c0c8 333 dev_t rdev;
984263bc
MD
334
335 slice = 0;
336 part = 0;
1f2de5d4
MD
337 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
338 name += sizeof(__SYS_PATH_DEV) - 1;
984263bc
MD
339 cp = name;
340 while (*cp != '\0' && (*cp < '0' || *cp > '9') && *cp != '/')
341 cp++;
342 if (cp == name) {
343 printf("missing device name\n");
344 return (NODEV);
345 }
346 if (*cp == '\0' || *cp == '/')
347 unit = -1;
348 else
349 unit = *cp - '0';
350 *cp++ = '\0';
351 for (cd = 0; cd < NUMCDEVSW; cd++) {
3281b7bd 352 dev = udev2dev(makeudev(cd, dkmakeminor(unit, slice, part)), 0);
e4c9c0c8
MD
353 if (dev_is_good(dev) && dev_dname(dev) &&
354 strcmp(dev_dname(dev), name) == 0) {
984263bc 355 goto gotit;
e4c9c0c8 356 }
984263bc
MD
357 }
358 printf("no such device '%s'\n", name);
359 return (NODEV);
360gotit:
e4c9c0c8 361 if (dev_dmaj(dev) == major(rootdev)) {
984263bc
MD
362 /* driver has already configured rootdev, e. g. vinum */
363 return (rootdev);
e4c9c0c8 364 }
984263bc
MD
365 if (unit == -1) {
366 printf("missing unit number\n");
367 return (NODEV);
368 }
369 while (*cp >= '0' && *cp <= '9')
370 unit = 10 * unit + *cp++ - '0';
371 if (*cp == 's' && cp[1] >= '0' && cp[1] <= '9') {
372 slice = cp[1] - '0' + 1;
373 cp += 2;
374 }
375 if (*cp >= 'a' && *cp <= 'h') {
376 part = *cp - 'a';
377 cp++;
378 }
379 if (*cp != '\0') {
380 printf("junk after name\n");
381 return (NODEV);
382 }
e4c9c0c8
MD
383 rdev = make_sub_dev(dev, dkmakeminor(unit, slice, part));
384 return(rdev);
984263bc
MD
385}
386
387/*
388 * Set rootdev to match (name), given that we expect it to
389 * refer to a disk-like device.
390 */
391static int
392setrootbyname(char *name)
393{
394 dev_t diskdev;
395
396 diskdev = getdiskbyname(name);
397 if (diskdev != NODEV) {
398 rootdev = diskdev;
399 return (0);
400 }
401
402 return (1);
403}
404
405#ifdef DDB
406DB_SHOW_COMMAND(disk, db_getdiskbyname)
407{
408 dev_t dev;
409
410 if (modif[0] == '\0') {
411 db_error("usage: show disk/devicename");
412 return;
413 }
414 dev = getdiskbyname(modif);
415 if (dev != NODEV)
416 db_printf("dev_t = %p\n", dev);
417 else
418 db_printf("No disk device matched.\n");
419}
420#endif