Deleted Added
full compact
vfs_mount.c (69793) vfs_mount.c (72200)
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 *
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 69793 2000-12-09 09:35:55Z obrien $
28 * $FreeBSD: head/sys/kern/vfs_mount.c 72200 2001-02-09 06:11:45Z bmilekic $
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/kernel.h>
49#include <sys/systm.h>
50#include <sys/vnode.h>
51#include <sys/mount.h>
52#include <sys/malloc.h>
53#include <sys/reboot.h>
54#include <sys/diskslice.h>
55#include <sys/disklabel.h>
56#include <sys/conf.h>
57#include <sys/cons.h>
58#include <paths.h>
59
60#include "opt_ddb.h"
61#ifdef DDB
62#include <ddb/ddb.h>
63#endif
64
65MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
66
67#define ROOTNAME "root_device"
68
69struct vnode *rootvnode;
70
71/*
72 * The root specifiers we will try if RB_CDROM is specified.
73 */
74static char *cdrom_rootdevnames[] = {
75 "cd9660:cd0a",
76 "cd9660:acd0a",
77 "cd9660:wcd0a",
78 NULL
79};
80
81static void vfs_mountroot(void *junk);
82static int vfs_mountroot_try(char *mountfrom);
83static int vfs_mountroot_ask(void);
84static void gets(char *cp);
85
86/* legacy find-root code */
87char *rootdevnames[2] = {NULL, NULL};
88static int setrootbyname(char *name);
89
90SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
91
92/*
93 * Find and mount the root filesystem
94 */
95static void
96vfs_mountroot(void *junk)
97{
98 int i;
99
100 /*
101 * The root filesystem information is compiled in, and we are
102 * booted with instructions to use it.
103 */
104#ifdef ROOTDEVNAME
105 if ((boothowto & RB_DFLTROOT) &&
106 !vfs_mountroot_try(ROOTDEVNAME))
107 return;
108#endif
109 /*
110 * We are booted with instructions to prompt for the root filesystem,
111 * or to use the compiled-in default when it doesn't exist.
112 */
113 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
114 if (!vfs_mountroot_ask())
115 return;
116 }
117
118 /*
119 * We've been given the generic "use CDROM as root" flag. This is
120 * necessary because one media may be used in many different
121 * devices, so we need to search for them.
122 */
123 if (boothowto & RB_CDROM) {
124 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
125 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
126 return;
127 }
128 }
129
130 /*
131 * Try to use the value read by the loader from /etc/fstab, or
132 * supplied via some other means. This is the preferred
133 * mechanism.
134 */
135 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
136 return;
137
138 /*
139 * Try values that may have been computed by the machine-dependant
140 * legacy code.
141 */
142 if (!vfs_mountroot_try(rootdevnames[0]))
143 return;
144 if (!vfs_mountroot_try(rootdevnames[1]))
145 return;
146
147 /*
148 * If we have a compiled-in default, and haven't already tried it, try
149 * it now.
150 */
151#ifdef ROOTDEVNAME
152 if (!(boothowto & RB_DFLTROOT))
153 if (!vfs_mountroot_try(ROOTDEVNAME))
154 return;
155#endif
156
157 /*
158 * Everything so far has failed, prompt on the console if we haven't
159 * already tried that.
160 */
161 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
162 return;
163 panic("Root mount failed, startup aborted.");
164}
165
166/*
167 * Mount (mountfrom) as the root filesystem.
168 */
169static int
170vfs_mountroot_try(char *mountfrom)
171{
172 struct mount *mp;
173 char *vfsname, *path;
174 int error;
175 char patt[32];
176 int s;
177
178 vfsname = NULL;
179 path = NULL;
180 mp = NULL;
181 error = EINVAL;
182
183 if (mountfrom == NULL)
184 return(error); /* don't complain */
185
186 s = splcam(); /* Overkill, but annoying without it */
187 printf("Mounting root from %s\n", mountfrom);
188 splx(s);
189
190 /* parse vfs name and path */
191 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
192 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
193 vfsname[0] = path[0] = 0;
194 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
195 if (sscanf(mountfrom, patt, vfsname, path) < 1)
196 goto done;
197
198 /* allocate a root mount */
199 error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
200 &mp);
201 if (error != 0) {
202 printf("Can't allocate root mount for filesystem '%s': %d\n",
203 vfsname, error);
204 goto done;
205 }
206 mp->mnt_flag |= MNT_ROOTFS;
207
208 /* do our best to set rootdev */
209 if ((path[0] != 0) && setrootbyname(path))
210 printf("setrootbyname failed\n");
211
212 /* If the root device is a type "memory disk", mount RW */
213 if (rootdev != NODEV && devsw(rootdev) &&
214 (devsw(rootdev)->d_flags & D_MEMDISK))
215 mp->mnt_flag &= ~MNT_RDONLY;
216
217 error = VFS_MOUNT(mp, NULL, NULL, NULL, curproc);
218
219done:
220 if (vfsname != NULL)
221 free(vfsname, M_MOUNT);
222 if (path != NULL)
223 free(path, M_MOUNT);
224 if (error != 0) {
225 if (mp != NULL) {
226 vfs_unbusy(mp, curproc);
227 free(mp, M_MOUNT);
228 }
229 printf("Root mount failed: %d\n", error);
230 } else {
231
232 /* register with list of mounted filesystems */
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/kernel.h>
49#include <sys/systm.h>
50#include <sys/vnode.h>
51#include <sys/mount.h>
52#include <sys/malloc.h>
53#include <sys/reboot.h>
54#include <sys/diskslice.h>
55#include <sys/disklabel.h>
56#include <sys/conf.h>
57#include <sys/cons.h>
58#include <paths.h>
59
60#include "opt_ddb.h"
61#ifdef DDB
62#include <ddb/ddb.h>
63#endif
64
65MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
66
67#define ROOTNAME "root_device"
68
69struct vnode *rootvnode;
70
71/*
72 * The root specifiers we will try if RB_CDROM is specified.
73 */
74static char *cdrom_rootdevnames[] = {
75 "cd9660:cd0a",
76 "cd9660:acd0a",
77 "cd9660:wcd0a",
78 NULL
79};
80
81static void vfs_mountroot(void *junk);
82static int vfs_mountroot_try(char *mountfrom);
83static int vfs_mountroot_ask(void);
84static void gets(char *cp);
85
86/* legacy find-root code */
87char *rootdevnames[2] = {NULL, NULL};
88static int setrootbyname(char *name);
89
90SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
91
92/*
93 * Find and mount the root filesystem
94 */
95static void
96vfs_mountroot(void *junk)
97{
98 int i;
99
100 /*
101 * The root filesystem information is compiled in, and we are
102 * booted with instructions to use it.
103 */
104#ifdef ROOTDEVNAME
105 if ((boothowto & RB_DFLTROOT) &&
106 !vfs_mountroot_try(ROOTDEVNAME))
107 return;
108#endif
109 /*
110 * We are booted with instructions to prompt for the root filesystem,
111 * or to use the compiled-in default when it doesn't exist.
112 */
113 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
114 if (!vfs_mountroot_ask())
115 return;
116 }
117
118 /*
119 * We've been given the generic "use CDROM as root" flag. This is
120 * necessary because one media may be used in many different
121 * devices, so we need to search for them.
122 */
123 if (boothowto & RB_CDROM) {
124 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
125 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
126 return;
127 }
128 }
129
130 /*
131 * Try to use the value read by the loader from /etc/fstab, or
132 * supplied via some other means. This is the preferred
133 * mechanism.
134 */
135 if (!vfs_mountroot_try(getenv("vfs.root.mountfrom")))
136 return;
137
138 /*
139 * Try values that may have been computed by the machine-dependant
140 * legacy code.
141 */
142 if (!vfs_mountroot_try(rootdevnames[0]))
143 return;
144 if (!vfs_mountroot_try(rootdevnames[1]))
145 return;
146
147 /*
148 * If we have a compiled-in default, and haven't already tried it, try
149 * it now.
150 */
151#ifdef ROOTDEVNAME
152 if (!(boothowto & RB_DFLTROOT))
153 if (!vfs_mountroot_try(ROOTDEVNAME))
154 return;
155#endif
156
157 /*
158 * Everything so far has failed, prompt on the console if we haven't
159 * already tried that.
160 */
161 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
162 return;
163 panic("Root mount failed, startup aborted.");
164}
165
166/*
167 * Mount (mountfrom) as the root filesystem.
168 */
169static int
170vfs_mountroot_try(char *mountfrom)
171{
172 struct mount *mp;
173 char *vfsname, *path;
174 int error;
175 char patt[32];
176 int s;
177
178 vfsname = NULL;
179 path = NULL;
180 mp = NULL;
181 error = EINVAL;
182
183 if (mountfrom == NULL)
184 return(error); /* don't complain */
185
186 s = splcam(); /* Overkill, but annoying without it */
187 printf("Mounting root from %s\n", mountfrom);
188 splx(s);
189
190 /* parse vfs name and path */
191 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
192 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK);
193 vfsname[0] = path[0] = 0;
194 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
195 if (sscanf(mountfrom, patt, vfsname, path) < 1)
196 goto done;
197
198 /* allocate a root mount */
199 error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME,
200 &mp);
201 if (error != 0) {
202 printf("Can't allocate root mount for filesystem '%s': %d\n",
203 vfsname, error);
204 goto done;
205 }
206 mp->mnt_flag |= MNT_ROOTFS;
207
208 /* do our best to set rootdev */
209 if ((path[0] != 0) && setrootbyname(path))
210 printf("setrootbyname failed\n");
211
212 /* If the root device is a type "memory disk", mount RW */
213 if (rootdev != NODEV && devsw(rootdev) &&
214 (devsw(rootdev)->d_flags & D_MEMDISK))
215 mp->mnt_flag &= ~MNT_RDONLY;
216
217 error = VFS_MOUNT(mp, NULL, NULL, NULL, curproc);
218
219done:
220 if (vfsname != NULL)
221 free(vfsname, M_MOUNT);
222 if (path != NULL)
223 free(path, M_MOUNT);
224 if (error != 0) {
225 if (mp != NULL) {
226 vfs_unbusy(mp, curproc);
227 free(mp, M_MOUNT);
228 }
229 printf("Root mount failed: %d\n", error);
230 } else {
231
232 /* register with list of mounted filesystems */
233 mtx_enter(&mountlist_mtx, MTX_DEF);
233 mtx_lock(&mountlist_mtx);
234 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
234 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
235 mtx_exit(&mountlist_mtx, MTX_DEF);
235 mtx_unlock(&mountlist_mtx);
236
237 /* sanity check system clock against root filesystem timestamp */
238 inittodr(mp->mnt_time);
239 vfs_unbusy(mp, curproc);
240 }
241 return(error);
242}
243
244/*
245 * Spin prompting on the console for a suitable root filesystem
246 */
247static int
248vfs_mountroot_ask(void)
249{
250 char name[128];
251 int i;
252 dev_t dev;
253
254 for(;;) {
255 printf("\nManual root filesystem specification:\n");
256 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
257 printf(" eg. ufs:%sda0s1a\n", _PATH_DEV);
258 printf(" ? List valid disk boot devices\n");
259 printf(" <empty line> Abort manual input\n");
260 printf("\nmountroot> ");
261 gets(name);
262 if (name[0] == 0)
263 return(1);
264 if (name[0] == '?') {
265 printf("Possibly valid devices for 'ufs' root:\n");
266 for (i = 0; i < NUMCDEVSW; i++) {
267 dev = makedev(i, 0);
268 if (devsw(dev) != NULL)
269 printf(" \"%s\"", devsw(dev)->d_name);
270 }
271 printf("\n");
272 continue;
273 }
274 if (!vfs_mountroot_try(name))
275 return(0);
276 }
277}
278
279static void
280gets(char *cp)
281{
282 char *lp;
283 int c;
284
285 lp = cp;
286 for (;;) {
287 printf("%c", c = cngetc() & 0177);
288 switch (c) {
289 case -1:
290 case '\n':
291 case '\r':
292 *lp++ = '\0';
293 return;
294 case '\b':
295 case '\177':
296 if (lp > cp) {
297 printf(" \b");
298 lp--;
299 }
300 continue;
301 case '#':
302 lp--;
303 if (lp < cp)
304 lp = cp;
305 continue;
306 case '@':
307 case 'u' & 037:
308 lp = cp;
309 printf("%c", '\n');
310 continue;
311 default:
312 *lp++ = c;
313 }
314 }
315}
316
317/*
318 * Convert a given name to the dev_t of the disk-like device
319 * it refers to.
320 */
321dev_t
322getdiskbyname(char *name) {
323 char *cp;
324 dev_t dev;
325
326 cp = name;
327 if (!bcmp(cp, "/dev/", 5))
328 cp += 5;
329
330 dev = NODEV;
331 EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev);
332 return (dev);
333}
334
335/*
336 * Set rootdev to match (name), given that we expect it to
337 * refer to a disk-like device.
338 */
339static int
340setrootbyname(char *name)
341{
342 dev_t diskdev;
343
344 diskdev = getdiskbyname(name);
345 if (diskdev != NODEV) {
346 rootdev = diskdev;
347 return (0);
348 }
349
350 return (1);
351}
352
353#ifdef DDB
354DB_SHOW_COMMAND(disk, db_getdiskbyname)
355{
356 dev_t dev;
357
358 if (modif[0] == '\0') {
359 db_error("usage: show disk/devicename");
360 return;
361 }
362 dev = getdiskbyname(modif);
363 if (dev != NODEV)
364 db_printf("dev_t = %p\n", dev);
365 else
366 db_printf("No disk device matched.\n");
367}
368#endif
236
237 /* sanity check system clock against root filesystem timestamp */
238 inittodr(mp->mnt_time);
239 vfs_unbusy(mp, curproc);
240 }
241 return(error);
242}
243
244/*
245 * Spin prompting on the console for a suitable root filesystem
246 */
247static int
248vfs_mountroot_ask(void)
249{
250 char name[128];
251 int i;
252 dev_t dev;
253
254 for(;;) {
255 printf("\nManual root filesystem specification:\n");
256 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n");
257 printf(" eg. ufs:%sda0s1a\n", _PATH_DEV);
258 printf(" ? List valid disk boot devices\n");
259 printf(" <empty line> Abort manual input\n");
260 printf("\nmountroot> ");
261 gets(name);
262 if (name[0] == 0)
263 return(1);
264 if (name[0] == '?') {
265 printf("Possibly valid devices for 'ufs' root:\n");
266 for (i = 0; i < NUMCDEVSW; i++) {
267 dev = makedev(i, 0);
268 if (devsw(dev) != NULL)
269 printf(" \"%s\"", devsw(dev)->d_name);
270 }
271 printf("\n");
272 continue;
273 }
274 if (!vfs_mountroot_try(name))
275 return(0);
276 }
277}
278
279static void
280gets(char *cp)
281{
282 char *lp;
283 int c;
284
285 lp = cp;
286 for (;;) {
287 printf("%c", c = cngetc() & 0177);
288 switch (c) {
289 case -1:
290 case '\n':
291 case '\r':
292 *lp++ = '\0';
293 return;
294 case '\b':
295 case '\177':
296 if (lp > cp) {
297 printf(" \b");
298 lp--;
299 }
300 continue;
301 case '#':
302 lp--;
303 if (lp < cp)
304 lp = cp;
305 continue;
306 case '@':
307 case 'u' & 037:
308 lp = cp;
309 printf("%c", '\n');
310 continue;
311 default:
312 *lp++ = c;
313 }
314 }
315}
316
317/*
318 * Convert a given name to the dev_t of the disk-like device
319 * it refers to.
320 */
321dev_t
322getdiskbyname(char *name) {
323 char *cp;
324 dev_t dev;
325
326 cp = name;
327 if (!bcmp(cp, "/dev/", 5))
328 cp += 5;
329
330 dev = NODEV;
331 EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev);
332 return (dev);
333}
334
335/*
336 * Set rootdev to match (name), given that we expect it to
337 * refer to a disk-like device.
338 */
339static int
340setrootbyname(char *name)
341{
342 dev_t diskdev;
343
344 diskdev = getdiskbyname(name);
345 if (diskdev != NODEV) {
346 rootdev = diskdev;
347 return (0);
348 }
349
350 return (1);
351}
352
353#ifdef DDB
354DB_SHOW_COMMAND(disk, db_getdiskbyname)
355{
356 dev_t dev;
357
358 if (modif[0] == '\0') {
359 db_error("usage: show disk/devicename");
360 return;
361 }
362 dev = getdiskbyname(modif);
363 if (dev != NODEV)
364 db_printf("dev_t = %p\n", dev);
365 else
366 db_printf("No disk device matched.\n");
367}
368#endif