vfs_mount.c revision 65051
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: head/sys/kern/vfs_mount.c 65051 2000-08-24 15:36:55Z phk $ 29 */ 30 31/* 32 * Locate and mount the root filesystem. 33 * 34 * The root filesystem is detailed in the kernel environment variable 35 * vfs.root.mountfrom, which is expected to be in the general format 36 * 37 * <vfsname>:[<path>] 38 * vfsname := the name of a VFS known to the kernel and capable 39 * of being mounted as root 40 * path := disk device name or other data used by the filesystem 41 * to locate its physical store 42 * 43 */ 44 45#include "opt_rootdevname.h" 46#include "opt_devfs.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 61#include "opt_ddb.h" 62#ifdef DDB 63#include <ddb/ddb.h> 64#endif 65 66#ifdef DEVFS 67#include <sys/eventhandler.h> 68#include <fs/devfs/devfs.h> 69#endif 70 71MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 72 73#define ROOTNAME "root_device" 74 75struct vnode *rootvnode; 76 77/* 78 * The root specifiers we will try if RB_CDROM is specified. 79 */ 80static char *cdrom_rootdevnames[] = { 81 "cd9660:cd0a", 82 "cd9660:acd0a", 83 "cd9660:wcd0a", 84 NULL 85}; 86 87static void vfs_mountroot(void *junk); 88static int vfs_mountroot_try(char *mountfrom); 89static int vfs_mountroot_ask(void); 90static void gets(char *cp); 91 92/* legacy find-root code */ 93char *rootdevnames[2] = {NULL, NULL}; 94static int setrootbyname(char *name); 95 96SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL); 97 98/* 99 * Find and mount the root filesystem 100 */ 101static void 102vfs_mountroot(void *junk) 103{ 104 int i; 105 106 /* 107 * The root filesystem information is compiled in, and we are 108 * booted with instructions to use it. 109 */ 110#ifdef ROOTDEVNAME 111 if ((boothowto & RB_DFLTROOT) && 112 !vfs_mountroot_try(ROOTDEVNAME)) 113 return; 114#endif 115 /* 116 * We are booted with instructions to prompt for the root filesystem, 117 * or to use the compiled-in default when it doesn't exist. 118 */ 119 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) { 120 if (!vfs_mountroot_ask()) 121 return; 122 } 123 124 /* 125 * We've been given the generic "use CDROM as root" flag. This is 126 * necessary because one media may be used in many different 127 * devices, so we need to search for them. 128 */ 129 if (boothowto & RB_CDROM) { 130 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 131 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 132 return; 133 } 134 } 135 136 /* 137 * Try to use the value read by the loader from /etc/fstab, or 138 * supplied via some other means. This is the preferred 139 * mechanism. 140 */ 141 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom"))) 142 return; 143 144 /* 145 * Try values that may have been computed by the machine-dependant 146 * legacy code. 147 */ 148 if (!vfs_mountroot_try(rootdevnames[0])) 149 return; 150 if (!vfs_mountroot_try(rootdevnames[1])) 151 return; 152 153 /* 154 * If we have a compiled-in default, and haven't already tried it, try 155 * it now. 156 */ 157#ifdef ROOTDEVNAME 158 if (!(boothowto & RB_DFLTROOT)) 159 if (!vfs_mountroot_try(ROOTDEVNAME)) 160 return; 161#endif 162 163 /* 164 * Everything so far has failed, prompt on the console if we haven't 165 * already tried that. 166 */ 167 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask()) 168 return; 169 panic("Root mount failed, startup aborted."); 170} 171 172/* 173 * Mount (mountfrom) as the root filesystem. 174 */ 175static int 176vfs_mountroot_try(char *mountfrom) 177{ 178 struct mount *mp; 179 char *vfsname, *path; 180 int error; 181 char patt[32]; 182 int s; 183 184 vfsname = NULL; 185 path = NULL; 186 mp = NULL; 187 error = EINVAL; 188 189 if (mountfrom == NULL) 190 return(error); /* don't complain */ 191 192 s = splcam(); /* Overkill, but annoying without it */ 193 printf("Mounting root from %s\n", mountfrom); 194 splx(s); 195 196 /* parse vfs name and path */ 197 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 198 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 199 vfsname[0] = path[0] = 0; 200 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 201 if (sscanf(mountfrom, patt, vfsname, path) < 1) 202 goto done; 203 204 /* allocate a root mount */ 205 error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME, 206 &mp); 207 if (error != 0) { 208 printf("Can't allocate root mount for filesystem '%s': %d\n", 209 vfsname, error); 210 goto done; 211 } 212 mp->mnt_flag |= MNT_ROOTFS; 213 214 /* do our best to set rootdev */ 215 if ((path[0] != 0) && setrootbyname(path)) 216 printf("setrootbyname failed\n"); 217 218 /* If the root device is a type "memory disk", mount RW */ 219 if (rootdev != NODEV && devsw(rootdev) && 220 (devsw(rootdev)->d_flags & D_MEMDISK)) 221 mp->mnt_flag &= ~MNT_RDONLY; 222 223 error = VFS_MOUNT(mp, NULL, NULL, NULL, curproc); 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) { 232 vfs_unbusy(mp, curproc); 233 free(mp, M_MOUNT); 234 } 235 printf("Root mount failed: %d\n", error); 236 } else { 237 238 /* register with list of mounted filesystems */ 239 simple_lock(&mountlist_slock); 240 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 241 simple_unlock(&mountlist_slock); 242 243 /* sanity check system clock against root filesystem timestamp */ 244 inittodr(mp->mnt_time); 245 vfs_unbusy(mp, curproc); 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"); 263 printf(" eg. ufs:/dev/da0s1a\n"); 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++) { 273 dev = makedev(i, 0); 274 if (devsw(dev) != NULL) 275 printf(" \"%s\"", devsw(dev)->d_name); 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 328getdiskbyname(char *name) { 329#ifdef DEVFS 330 char *cp; 331 dev_t dev; 332 333 cp = name; 334 if (!bcmp(cp, "/dev/", 5)) 335 cp += 5; 336 337 dev = NODEV; 338 EVENTHANDLER_INVOKE(devfs_clone, cp, strlen(cp), &dev); 339 return (dev); 340 341#else 342 char *cp; 343 int cd, unit, slice, part; 344 dev_t dev; 345 346 slice = 0; 347 part = 0; 348 cp = rindex(name, '/'); 349 if (cp != NULL) { 350 name = cp + 1; 351 } 352 cp = name; 353 while (cp != '\0' && (*cp < '0' || *cp > '9')) 354 cp++; 355 if (cp == name) { 356 printf("missing device name\n"); 357 return (NODEV); 358 } 359 if (*cp == '\0') { 360 printf("missing unit number\n"); 361 return (NODEV); 362 } 363 unit = *cp - '0'; 364 *cp++ = '\0'; 365 for (cd = 0; cd < NUMCDEVSW; cd++) { 366 dev = makedev(cd, 0); 367 if (devsw(dev) != NULL && 368 strcmp(devsw(dev)->d_name, name) == 0) 369 goto gotit; 370 } 371 printf("no such device '%s'\n", name); 372 return (NODEV); 373gotit: 374 while (*cp >= '0' && *cp <= '9') 375 unit += 10 * unit + *cp++ - '0'; 376 if (*cp == 's' && cp[1] >= '0' && cp[1] <= '9') { 377 slice = cp[1] - '0' + 1; 378 cp += 2; 379 } 380 if (*cp >= 'a' && *cp <= 'h') { 381 part = *cp - 'a'; 382 cp++; 383 } 384 if (*cp != '\0') { 385 printf("junk after name\n"); 386 return (NODEV); 387 } 388 return (makedev(cd, dkmakeminor(unit, slice, part))); 389#endif 390} 391 392/* 393 * Set rootdev to match (name), given that we expect it to 394 * refer to a disk-like device. 395 */ 396static int 397setrootbyname(char *name) 398{ 399 dev_t diskdev; 400 401 diskdev = getdiskbyname(name); 402 if (diskdev != NODEV) { 403 rootdev = diskdev; 404 return (0); 405 } 406 407 return (1); 408} 409 410#ifdef DDB 411DB_SHOW_COMMAND(disk, db_getdiskbyname) 412{ 413 dev_t dev; 414 415 if (modif[0] == '\0') { 416 db_error("usage: show disk/devicename"); 417 return; 418 } 419 dev = getdiskbyname(modif); 420 if (dev != NODEV) 421 db_printf("dev_t = %p\n", dev); 422 else 423 db_printf("No disk device matched.\n"); 424} 425#endif 426