vfs_mount.c revision 86842
1218887Sdim/*- 2218887Sdim * Copyright (c) 1999 Michael Smith 3218887Sdim * All rights reserved. 4218887Sdim * Copyright (c) 1999 Poul-Henning Kamp 5218887Sdim * All rights reserved. 6218887Sdim * 7218887Sdim * Redistribution and use in source and binary forms, with or without 8218887Sdim * modification, are permitted provided that the following conditions 9218887Sdim * are met: 10218887Sdim * 1. Redistributions of source code must retain the above copyright 11249423Sdim * notice, this list of conditions and the following disclaimer. 12218887Sdim * 2. Redistributions in binary form must reproduce the above copyright 13218887Sdim * notice, this list of conditions and the following disclaimer in the 14218887Sdim * documentation and/or other materials provided with the distribution. 15218887Sdim * 16218887Sdim * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17226633Sdim * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18218887Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19218887Sdim * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20226633Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21226633Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22218887Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23218887Sdim * 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 86842 2001-11-24 01:34:12Z obrien $ 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 47#include <sys/param.h> 48#include <sys/systm.h> 49#include <sys/kernel.h> 50#include <sys/lock.h> 51#include <sys/mutex.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/proc.h> 61 62#include "opt_ddb.h" 63 64#ifdef DDB 65#include <ddb/ddb.h> 66#endif 67 68#include <paths.h> 69 70MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 71 72#define ROOTNAME "root_device" 73 74struct vnode *rootvnode; 75 76/* 77 * The root specifiers we will try if RB_CDROM is specified. 78 */ 79static char *cdrom_rootdevnames[] = { 80 "cd9660:cd0a", 81 "cd9660:acd0a", 82 "cd9660:wcd0a", 83 NULL 84}; 85 86static void vfs_mountroot(void *junk); 87static int vfs_mountroot_try(char *mountfrom); 88static int vfs_mountroot_ask(void); 89static void gets(char *cp); 90 91/* legacy find-root code */ 92char *rootdevnames[2] = {NULL, NULL}; 93static int setrootbyname(char *name); 94 95SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL); 96 97/* 98 * Find and mount the root filesystem 99 */ 100static void 101vfs_mountroot(void *junk) 102{ 103 int i; 104 105 /* 106 * The root filesystem information is compiled in, and we are 107 * booted with instructions to use it. 108 */ 109#ifdef ROOTDEVNAME 110 if ((boothowto & RB_DFLTROOT) && 111 !vfs_mountroot_try(ROOTDEVNAME)) 112 return; 113#endif 114 /* 115 * We are booted with instructions to prompt for the root filesystem, 116 * or to use the compiled-in default when it doesn't exist. 117 */ 118 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) { 119 if (!vfs_mountroot_ask()) 120 return; 121 } 122 123 /* 124 * We've been given the generic "use CDROM as root" flag. This is 125 * necessary because one media may be used in many different 126 * devices, so we need to search for them. 127 */ 128 if (boothowto & RB_CDROM) { 129 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 130 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 131 return; 132 } 133 } 134 135 /* 136 * Try to use the value read by the loader from /etc/fstab, or 137 * supplied via some other means. This is the preferred 138 * mechanism. 139 */ 140 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom"))) 141 return; 142 143 /* 144 * Try values that may have been computed by the machine-dependant 145 * legacy code. 146 */ 147 if (!vfs_mountroot_try(rootdevnames[0])) 148 return; 149 if (!vfs_mountroot_try(rootdevnames[1])) 150 return; 151 152 /* 153 * If we have a compiled-in default, and haven't already tried it, try 154 * it now. 155 */ 156#ifdef ROOTDEVNAME 157 if (!(boothowto & RB_DFLTROOT)) 158 if (!vfs_mountroot_try(ROOTDEVNAME)) 159 return; 160#endif 161 162 /* 163 * Everything so far has failed, prompt on the console if we haven't 164 * already tried that. 165 */ 166 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask()) 167 return; 168 panic("Root mount failed, startup aborted."); 169} 170 171/* 172 * Mount (mountfrom) as the root filesystem. 173 */ 174static int 175vfs_mountroot_try(char *mountfrom) 176{ 177 struct mount *mp; 178 char *vfsname, *path; 179 int error; 180 char patt[32]; 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 */ 218 if (rootdev != NODEV && devsw(rootdev) && 219 (devsw(rootdev)->d_flags & D_MEMDISK)) 220 mp->mnt_flag &= ~MNT_RDONLY; 221 222 /* 223 * Set the mount path to be something useful, because the 224 * filesystem code isn't responsible now for initialising 225 * f_mntonname unless they want to override the default 226 * (which is `path'.) 227 */ 228 strncpy(mp->mnt_stat.f_mntonname, "/", MNAMELEN); 229 230 error = VFS_MOUNT(mp, NULL, NULL, NULL, curthread); 231 232done: 233 if (vfsname != NULL) 234 free(vfsname, M_MOUNT); 235 if (path != NULL) 236 free(path, M_MOUNT); 237 if (error != 0) { 238 if (mp != NULL) { 239 vfs_unbusy(mp, curthread); 240 free(mp, M_MOUNT); 241 } 242 printf("Root mount failed: %d\n", error); 243 } else { 244 245 /* register with list of mounted filesystems */ 246 mtx_lock(&mountlist_mtx); 247 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 248 mtx_unlock(&mountlist_mtx); 249 250 /* sanity check system clock against root filesystem timestamp */ 251 inittodr(mp->mnt_time); 252 vfs_unbusy(mp, curthread); 253 } 254 return(error); 255} 256 257/* 258 * Spin prompting on the console for a suitable root filesystem 259 */ 260static int 261vfs_mountroot_ask(void) 262{ 263 char name[128]; 264 int i; 265 dev_t dev; 266 267 for(;;) { 268 printf("\nManual root filesystem specification:\n"); 269 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 270#if defined(__i386__) || defined(__ia64__) 271 printf(" eg. ufs:da0s1a\n"); 272#else 273 printf(" eg. ufs:da0a\n"); 274#endif 275 printf(" ? List valid disk boot devices\n"); 276 printf(" <empty line> Abort manual input\n"); 277 printf("\nmountroot> "); 278 gets(name); 279 if (name[0] == 0) 280 return(1); 281 if (name[0] == '?') { 282 printf("Possibly valid devices for 'ufs' root:\n"); 283 for (i = 0; i < NUMCDEVSW; i++) { 284 dev = makedev(i, 0); 285 if (devsw(dev) != NULL) 286 printf(" \"%s\"", devsw(dev)->d_name); 287 } 288 printf("\n"); 289 continue; 290 } 291 if (!vfs_mountroot_try(name)) 292 return(0); 293 } 294} 295 296static void 297gets(char *cp) 298{ 299 char *lp; 300 int c; 301 302 lp = cp; 303 for (;;) { 304 printf("%c", c = cngetc() & 0177); 305 switch (c) { 306 case -1: 307 case '\n': 308 case '\r': 309 *lp++ = '\0'; 310 return; 311 case '\b': 312 case '\177': 313 if (lp > cp) { 314 printf(" \b"); 315 lp--; 316 } 317 continue; 318 case '#': 319 lp--; 320 if (lp < cp) 321 lp = cp; 322 continue; 323 case '@': 324 case 'u' & 037: 325 lp = cp; 326 printf("%c", '\n'); 327 continue; 328 default: 329 *lp++ = c; 330 } 331 } 332} 333 334/* 335 * Convert a given name to the dev_t of the disk-like device 336 * it refers to. 337 */ 338dev_t 339getdiskbyname(char *name) { 340 char *cp; 341 dev_t dev; 342 343 cp = name; 344 if (!bcmp(cp, "/dev/", 5)) 345 cp += 5; 346 347 dev = NODEV; 348 EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev); 349 return (dev); 350} 351 352/* 353 * Set rootdev to match (name), given that we expect it to 354 * refer to a disk-like device. 355 */ 356static int 357setrootbyname(char *name) 358{ 359 dev_t diskdev; 360 361 diskdev = getdiskbyname(name); 362 if (diskdev != NODEV) { 363 rootdev = diskdev; 364 return (0); 365 } 366 367 return (1); 368} 369 370#ifdef DDB 371DB_SHOW_COMMAND(disk, db_getdiskbyname) 372{ 373 dev_t dev; 374 375 if (modif[0] == '\0') { 376 db_error("usage: show disk/devicename"); 377 return; 378 } 379 dev = getdiskbyname(modif); 380 if (dev != NODEV) 381 db_printf("dev_t = %p\n", dev); 382 else 383 db_printf("No disk device matched.\n"); 384} 385#endif 386