rename functions that clash with reserved math procedures to avoid gcc3.4
[dragonfly.git] / sys / kern / vfs_conf.c
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 $
29  *      $DragonFly: src/sys/kern/vfs_conf.c,v 1.11 2004/07/20 03:08:23 dillon Exp $
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>
60 #include <sys/device.h>
61 #include <sys/paths.h>
62
63 #include "opt_ddb.h"
64 #ifdef DDB
65 #include <ddb/ddb.h>
66 #endif
67
68 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
69
70 #define ROOTNAME        "root_device"
71
72 struct vnode    *rootvnode;
73
74 /* 
75  * The root specifiers we will try if RB_CDROM is specified.  Note that
76  * the ATA driver will accept acd*a and acd*c, but the SCSI driver
77  * will only accept cd*c, so use 'c'.
78  */
79 static char *cdrom_rootdevnames[] = {
80         "cd9660:cd0c",
81         "cd9660:acd0c",
82         "cd9660:cd1c",
83         "cd9660:acd1c",
84         NULL
85 };
86
87 static void     vfs_mountroot(void *junk);
88 static int      vfs_mountroot_try(const char *mountfrom);
89 static int      vfs_mountroot_ask(void);
90 static void     gets(char *cp);
91
92 /* legacy find-root code */
93 char            *rootdevnames[2] = {NULL, NULL};
94 static int      setrootbyname(char *name);
95
96 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
97         
98 /*
99  * Find and mount the root filesystem
100  */
101 static void
102 vfs_mountroot(void *junk)
103 {
104         int     i;
105         dev_t   save_rootdev = rootdev;
106         
107         /* 
108          * The root filesystem information is compiled in, and we are
109          * booted with instructions to use it.
110          */
111 #ifdef ROOTDEVNAME
112         if ((boothowto & RB_DFLTROOT) && 
113             !vfs_mountroot_try(ROOTDEVNAME))
114                 return;
115 #endif
116         /* 
117          * We are booted with instructions to prompt for the root filesystem,
118          * or to use the compiled-in default when it doesn't exist.
119          */
120         if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
121                 if (!vfs_mountroot_ask())
122                         return;
123         }
124
125         /*
126          * We've been given the generic "use CDROM as root" flag.  This is
127          * necessary because one media may be used in many different
128          * devices, so we need to search for them.
129          */
130         if (boothowto & RB_CDROM) {
131                 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
132                         if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
133                                 return;
134                 }
135         }
136
137         /*
138          * Try to use the value read by the loader from /etc/fstab, or
139          * supplied via some other means.  This is the preferred 
140          * mechanism.
141          */
142         if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
143                 return;
144
145         /*
146          * If a vfs set rootdev, try it (XXX VINUM HACK!)
147          */
148         if (save_rootdev != NODEV) {
149                 rootdev = save_rootdev;
150                 if (!vfs_mountroot_try(""))
151                         return;
152         }
153
154         /* 
155          * Try values that may have been computed by the machine-dependant
156          * legacy code.
157          */
158         if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
159                 return;
160         if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
161                 return;
162
163         /*
164          * If we have a compiled-in default, and haven't already tried it, try
165          * it now.
166          */
167 #ifdef ROOTDEVNAME
168         if (!(boothowto & RB_DFLTROOT))
169                 if (!vfs_mountroot_try(ROOTDEVNAME))
170                         return;
171 #endif
172
173         /* 
174          * Everything so far has failed, prompt on the console if we haven't
175          * already tried that.
176          */
177         if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
178                 return;
179         panic("Root mount failed, startup aborted.");
180 }
181
182 /*
183  * Mount (mountfrom) as the root filesystem.
184  */
185 static int
186 vfs_mountroot_try(const char *mountfrom)
187 {
188         struct thread   *td = curthread;
189         struct mount    *mp;
190         char            *vfsname, *devname;
191         int             error;
192         char            patt[32];
193         lwkt_tokref     ilock;
194         int             s;
195
196         vfsname = NULL;
197         devname = NULL;
198         mp      = NULL;
199         error   = EINVAL;
200
201         if (mountfrom == NULL)
202                 return(error);          /* don't complain */
203
204         s = splcam();                   /* Overkill, but annoying without it */
205         printf("Mounting root from %s\n", mountfrom);
206         splx(s);
207
208         /* parse vfs name and devname */
209         vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
210         devname = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
211         vfsname[0] = devname[0] = 0;
212         sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
213         if (sscanf(mountfrom, patt, vfsname, devname) < 1)
214                 goto done;
215
216         /* allocate a root mount */
217         error = vfs_rootmountalloc(vfsname, 
218                         devname[0] != 0 ? devname : ROOTNAME, &mp);
219         if (error != 0) {
220                 printf("Can't allocate root mount for filesystem '%s': %d\n",
221                        vfsname, error);
222                 goto done;
223         }
224         mp->mnt_flag |= MNT_ROOTFS;
225
226         /* do our best to set rootdev */
227         if ((devname[0] != 0) && setrootbyname(devname))
228                 printf("setrootbyname failed\n");
229
230         /* If the root device is a type "memory disk", mount RW */
231         if (rootdev != NODEV && dev_is_good(rootdev) &&
232             (dev_dflags(rootdev) & D_MEMDISK)) {
233                 mp->mnt_flag &= ~MNT_RDONLY;
234         }
235
236         error = VFS_MOUNT(mp, NULL, NULL, NULL, td);
237
238 done:
239         if (vfsname != NULL)
240                 free(vfsname, M_MOUNT);
241         if (devname != NULL)
242                 free(devname, M_MOUNT);
243         if (error != 0) {
244                 if (mp != NULL) {
245                         vfs_unbusy(mp, td);
246                         free(mp, M_MOUNT);
247                 }
248                 printf("Root mount failed: %d\n", error);
249         } else {
250
251                 /* register with list of mounted filesystems */
252                 lwkt_gettoken(&ilock, &mountlist_token);
253                 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
254                 lwkt_reltoken(&ilock);
255
256                 /* sanity check system clock against root fs timestamp */
257                 inittodr(mp->mnt_time);
258                 vfs_unbusy(mp, td);
259         }
260         return(error);
261 }
262
263 /*
264  * Spin prompting on the console for a suitable root filesystem
265  */
266 static int
267 vfs_mountroot_ask(void)
268 {
269         char name[128];
270         int i;
271         dev_t dev;
272
273         for(;;) {
274                 printf("\nManual root filesystem specification:\n");
275                 printf("  <fstype>:<device>  Mount <device> using filesystem <fstype>\n");
276                 printf("                       eg. ufs:da0s1a\n");
277                 printf("  ?                  List valid disk boot devices\n");
278                 printf("  <empty line>       Abort manual input\n");
279                 printf("\nmountroot> ");
280                 gets(name);
281                 if (name[0] == 0)
282                         return(1);
283                 if (name[0] == '?') {
284                         printf("Possibly valid devices for 'ufs' root:\n");
285                         for (i = 0; i < NUMCDEVSW; i++) {
286                                 dev = udev2dev(makeudev(i, 0), 0);
287                                 if (dev_is_good(dev))
288                                         printf(" \"%s\"", dev_dname(dev));
289                         }
290                         printf("\n");
291                         continue;
292                 }
293                 if (!vfs_mountroot_try(name))
294                         return(0);
295         }
296 }
297
298 static void
299 gets(char *cp)
300 {
301         char *lp;
302         int c;
303
304         lp = cp;
305         for (;;) {
306                 printf("%c", c = cngetc() & 0177);
307                 switch (c) {
308                 case -1:
309                 case '\n':
310                 case '\r':
311                         *lp++ = '\0';
312                         return;
313                 case '\b':
314                 case '\177':
315                         if (lp > cp) {
316                                 printf(" \b");
317                                 lp--;
318                         }
319                         continue;
320                 case '#':
321                         lp--;
322                         if (lp < cp)
323                                 lp = cp;
324                         continue;
325                 case '@':
326                 case 'u' & 037:
327                         lp = cp;
328                         printf("%c", '\n');
329                         continue;
330                 default:
331                         *lp++ = c;
332                 }
333         }
334 }
335
336 /*
337  * Convert a given name to the dev_t of the disk-like device
338  * it refers to.
339  */
340 dev_t
341 getdiskbyname(const char *name) 
342 {
343         char *cp;
344         int nlen;
345         int cd, unit, slice, part;
346         dev_t dev;
347         dev_t rdev;
348
349         /*
350          * Get the base name of the device
351          */
352         if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
353                 name += sizeof(__SYS_PATH_DEV) - 1;
354         cp = __DECONST(char *, name);
355         while (*cp == '/')
356                 ++cp;
357         while (*cp >= 'a' && *cp <= 'z')
358                 ++cp;
359         if (cp == name) {
360                 printf("missing device name\n");
361                 return (NODEV);
362         }
363         nlen = cp - name;
364
365         /*
366          * Get the unit.
367          */
368         unit = strtol(cp, &cp, 10);
369         if (name + nlen == (const char *)cp || unit < 0 || unit > DKMAXUNIT) {
370                 printf("bad unit: %d\n", unit);
371                 return (NODEV);
372         }
373
374         /*
375          * Get the slice.  Note that if no partition or partition 'a' is
376          * specified, and no slice is specified, we will try both 'ad0a'
377          * (which is what you get when slice is 0), and also 'ad0' (the
378          * whole-disk partition, slice == 1).
379          */
380         if (*cp == 's') {
381                 slice = cp[1] - '0';
382                 if (slice < 1 || slice > 9) {
383                         printf("bad slice number\n");
384                         return(NODEV);
385                 }
386                 ++slice;        /* slice #1 starts at 2 */
387                 cp += 2;
388         } else {
389                 slice = 0;
390         }
391
392         /*
393          * Get the partition.
394          */
395         if (*cp >= 'a' && *cp <= 'p') {
396                 part = *cp - 'a';
397                 ++cp;
398         } else {
399                 part = 0;
400         }
401
402         if (*cp != '\0') {
403                 printf("junk after name\n");
404                 return (NODEV);
405         }
406
407         /*
408          * Locate the device
409          */
410         for (cd = 0; cd < NUMCDEVSW; cd++) {
411                 const char *dname;
412
413                 dev = udev2dev(makeudev(cd, dkmakeminor(unit, slice, part)), 0);
414                 if (dev_is_good(dev) && (dname = dev_dname(dev)) != NULL) {
415                         if (strlen(dname) == nlen &&
416                             strncmp(dname, name, nlen) == 0) {
417                                 break;
418                         }
419                 }
420         }
421         if (cd == NUMCDEVSW) {
422                 printf("no such device '%*.*s'\n", nlen, nlen, name);
423                 return (NODEV);
424         }
425
426         /*
427          * FOUND DEVICE
428          */
429         rdev = make_sub_dev(dev, dkmakeminor(unit, slice, part));
430         return(rdev);
431 }
432
433 /*
434  * Set rootdev to match (name), given that we expect it to
435  * refer to a disk-like device.
436  */
437 static int
438 setrootbyname(char *name)
439 {
440         dev_t diskdev;
441
442         diskdev = getdiskbyname(name);
443         if (diskdev != NODEV) {
444                 rootdev = diskdev;
445                 return (0);
446         }
447
448         return (1);
449 }
450
451 #ifdef DDB
452 DB_SHOW_COMMAND(disk, db_getdiskbyname)
453 {
454         dev_t dev;
455
456         if (modif[0] == '\0') {
457                 db_error("usage: show disk/devicename");
458                 return;
459         }
460         dev = getdiskbyname(modif);
461         if (dev != NODEV)
462                 db_printf("dev_t = %p\n", dev);
463         else
464                 db_printf("No disk device matched.\n");
465 }
466 #endif