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