Fix a number of core kernel issues related to HAMMER operation.
[dragonfly.git] / contrib / amd / fsinfo / fsi_analyze.c
blob7ba7a954f28673ae473e6b5ddf7ed8d6f4324955
1 /*
2 * Copyright (c) 1997-1999 Erez Zadok
3 * Copyright (c) 1989 Jan-Simon Pendry
4 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1989 The Regents of the University of California.
6 * All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgment:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
39 * %W% (Berkeley) %G%
41 * $Id: fsi_analyze.c,v 1.2 1999/01/10 21:54:27 ezk Exp $
46 * Analyze filesystem declarations
48 * Note: most of this is magic!
51 #ifdef HAVE_CONFIG_H
52 # include <config.h>
53 #endif /* HAVE_CONFIG_H */
54 #include <am_defs.h>
55 #include <fsi_data.h>
56 #include <fsinfo.h>
58 char *disk_fs_strings[] =
60 "fstype", "opts", "dumpset", "passno", "freq", "mount", "log", 0,
63 char *mount_strings[] =
65 "volname", "exportfs", 0,
68 char *fsmount_strings[] =
70 "as", "volname", "fstype", "opts", "from", 0,
73 char *host_strings[] =
75 "host", "netif", "config", "arch", "cluster", "os", 0,
78 char *ether_if_strings[] =
80 "inaddr", "netmask", "hwaddr", 0,
85 * Strip off the trailing part of a domain
86 * to produce a short-form domain relative
87 * to the local host domain.
88 * Note that this has no effect if the domain
89 * names do not have the same number of
90 * components. If that restriction proves
91 * to be a problem then the loop needs recoding
92 * to skip from right to left and do partial
93 * matches along the way -- ie more expensive.
95 void
96 domain_strip(char *otherdom, char *localdom)
98 char *p1, *p2;
100 if ((p1 = strchr(otherdom, '.')) &&
101 (p2 = strchr(localdom, '.')) &&
102 STREQ(p1 + 1, p2 + 1))
103 *p1 = '\0';
108 * Take a little-endian domain name and
109 * transform into a big-endian Un*x pathname.
110 * For example: kiska.doc.ic -> ic/doc/kiska
112 static char *
113 compute_hostpath(char *hn)
115 char *p = strdup(hn);
116 char *d;
117 char path[MAXPATHLEN];
119 domain_strip(p, hostname);
120 path[0] = '\0';
122 do {
123 d = strrchr(p, '.');
124 if (d) {
125 *d = 0;
126 strcat(path, d + 1);
127 strcat(path, "/");
128 } else {
129 strcat(path, p);
131 } while (d);
133 log("hostpath of '%s' is '%s'", hn, path);
135 strcpy(p, path);
136 return p;
140 static dict_ent *
141 find_volname(char *nn)
143 dict_ent *de;
144 char *p = strdup(nn);
145 char *q;
147 do {
148 log("Searching for volname %s", p);
149 de = dict_locate(dict_of_volnames, p);
150 q = strrchr(p, '/');
151 if (q)
152 *q = '\0';
153 } while (!de && q);
155 XFREE(p);
156 return de;
160 static void
161 show_required(ioloc *l, int mask, char *info, char *hostname, char *strings[])
163 int i;
164 log("mask left for %s:%s is %#x", hostname, info, mask);
166 for (i = 0; strings[i]; i++)
167 if (ISSET(mask, i))
168 lerror(l, "%s:%s needs field \"%s\"", hostname, info, strings[i]);
173 * Check and fill in "exportfs" details.
174 * Make sure the m_exported field references
175 * the most local node with an "exportfs" entry.
177 static int
178 check_exportfs(qelem *q, fsi_mount *e)
180 fsi_mount *mp;
181 int errors = 0;
183 ITER(mp, fsi_mount, q) {
184 if (ISSET(mp->m_mask, DM_EXPORTFS)) {
185 if (e)
186 lwarning(mp->m_ioloc, "%s has duplicate exportfs data", mp->m_name);
187 mp->m_exported = mp;
188 if (!ISSET(mp->m_mask, DM_VOLNAME))
189 set_mount(mp, DM_VOLNAME, strdup(mp->m_name));
190 } else {
191 mp->m_exported = e;
195 * Recursively descend the mount tree
197 if (mp->m_mount)
198 errors += check_exportfs(mp->m_mount, mp->m_exported);
201 * If a volume name has been specified, but this node and none
202 * of its parents has been exported, report an error.
204 if (ISSET(mp->m_mask, DM_VOLNAME) && !mp->m_exported) {
205 lerror(mp->m_ioloc, "%s has a volname but no exportfs data", mp->m_name);
206 errors++;
210 return errors;
214 static int
215 analyze_dkmount_tree(qelem *q, fsi_mount *parent, disk_fs *dk)
217 fsi_mount *mp;
218 int errors = 0;
220 ITER(mp, fsi_mount, q) {
221 log("Mount %s:", mp->m_name);
222 if (parent) {
223 char n[MAXPATHLEN];
224 sprintf(n, "%s/%s", parent->m_name, mp->m_name);
225 if (*mp->m_name == '/')
226 lerror(mp->m_ioloc, "sub-directory %s of %s starts with '/'", mp->m_name, parent->m_name);
227 else if (STREQ(mp->m_name, "default"))
228 lwarning(mp->m_ioloc, "sub-directory of %s is named \"default\"", parent->m_name);
229 log("Changing name %s to %s", mp->m_name, n);
230 XFREE(mp->m_name);
231 mp->m_name = strdup(n);
234 mp->m_name_len = strlen(mp->m_name);
235 mp->m_parent = parent;
236 mp->m_dk = dk;
237 if (mp->m_mount)
238 analyze_dkmount_tree(mp->m_mount, mp, dk);
241 return errors;
246 * The mount tree is a singleton list
247 * containing the top-level mount
248 * point for a disk.
250 static int
251 analyze_dkmounts(disk_fs *dk, qelem *q)
253 int errors = 0;
254 fsi_mount *mp, *mp2 = 0;
255 int i = 0;
258 * First scan the list of subdirs to make
259 * sure there is only one - and remember it
261 if (q) {
262 ITER(mp, fsi_mount, q) {
263 mp2 = mp;
264 i++;
269 * Check...
271 if (i < 1) {
272 lerror(dk->d_ioloc, "%s:%s has no mount point", dk->d_host->h_hostname, dk->d_dev);
273 return 1;
276 if (i > 1) {
277 lerror(dk->d_ioloc, "%s:%s has more than one mount point", dk->d_host->h_hostname, dk->d_dev);
278 errors++;
282 * Now see if a default mount point is required
284 if (STREQ(mp2->m_name, "default")) {
285 if (ISSET(mp2->m_mask, DM_VOLNAME)) {
286 char nbuf[1024];
287 compute_automount_point(nbuf, dk->d_host, mp2->m_volname);
288 XFREE(mp2->m_name);
289 mp2->m_name = strdup(nbuf);
290 log("%s:%s has default mount on %s", dk->d_host->h_hostname, dk->d_dev, mp2->m_name);
291 } else {
292 lerror(dk->d_ioloc, "no volname given for %s:%s", dk->d_host->h_hostname, dk->d_dev);
293 errors++;
298 * Fill in the disk mount point
300 if (!errors && mp2 && mp2->m_name)
301 dk->d_mountpt = strdup(mp2->m_name);
302 else
303 dk->d_mountpt = strdup("error");
306 * Analyze the mount tree
308 errors += analyze_dkmount_tree(q, 0, dk);
311 * Analyze the export tree
313 errors += check_exportfs(q, 0);
315 return errors;
319 static void
320 fixup_required_disk_info(disk_fs *dp)
323 * "fstype"
325 if (ISSET(dp->d_mask, DF_FSTYPE)) {
326 if (STREQ(dp->d_fstype, "swap")) {
329 * Fixup for a swap device
331 if (!ISSET(dp->d_mask, DF_PASSNO)) {
332 dp->d_passno = 0;
333 BITSET(dp->d_mask, DF_PASSNO);
334 } else if (dp->d_freq != 0) {
335 lwarning(dp->d_ioloc,
336 "Pass number for %s:%s is non-zero",
337 dp->d_host->h_hostname, dp->d_dev);
341 * "freq"
343 if (!ISSET(dp->d_mask, DF_FREQ)) {
344 dp->d_freq = 0;
345 BITSET(dp->d_mask, DF_FREQ);
346 } else if (dp->d_freq != 0) {
347 lwarning(dp->d_ioloc,
348 "dump frequency for %s:%s is non-zero",
349 dp->d_host->h_hostname, dp->d_dev);
353 * "opts"
355 if (!ISSET(dp->d_mask, DF_OPTS))
356 set_disk_fs(dp, DF_OPTS, strdup("swap"));
359 * "mount"
361 if (!ISSET(dp->d_mask, DF_MOUNT)) {
362 qelem *q = new_que();
363 fsi_mount *m = new_mount();
365 m->m_name = strdup("swap");
366 m->m_mount = new_que();
367 ins_que(&m->m_q, q->q_back);
368 dp->d_mount = q;
369 BITSET(dp->d_mask, DF_MOUNT);
370 } else {
371 lerror(dp->d_ioloc, "%s: mount field specified for swap partition", dp->d_host->h_hostname);
373 } else if (STREQ(dp->d_fstype, "export")) {
376 * "passno"
378 if (!ISSET(dp->d_mask, DF_PASSNO)) {
379 dp->d_passno = 0;
380 BITSET(dp->d_mask, DF_PASSNO);
381 } else if (dp->d_passno != 0) {
382 lwarning(dp->d_ioloc,
383 "pass number for %s:%s is non-zero",
384 dp->d_host->h_hostname, dp->d_dev);
388 * "freq"
390 if (!ISSET(dp->d_mask, DF_FREQ)) {
391 dp->d_freq = 0;
392 BITSET(dp->d_mask, DF_FREQ);
393 } else if (dp->d_freq != 0) {
394 lwarning(dp->d_ioloc,
395 "dump frequency for %s:%s is non-zero",
396 dp->d_host->h_hostname, dp->d_dev);
400 * "opts"
402 if (!ISSET(dp->d_mask, DF_OPTS))
403 set_disk_fs(dp, DF_OPTS, strdup("rw,defaults"));
410 static void
411 fixup_required_mount_info(fsmount *fp, dict_ent *de)
413 if (!ISSET(fp->f_mask, FM_FROM)) {
414 if (de->de_count != 1) {
415 lerror(fp->f_ioloc, "ambiguous mount: %s is a replicated filesystem", fp->f_volname);
416 } else {
417 dict_data *dd;
418 fsi_mount *mp = 0;
419 dd = AM_FIRST(dict_data, &de->de_q);
420 mp = (fsi_mount *) dd->dd_data;
421 if (!mp)
422 abort();
423 fp->f_ref = mp;
424 set_fsmount(fp, FM_FROM, mp->m_dk->d_host->h_hostname);
425 log("set: %s comes from %s", fp->f_volname, fp->f_from);
429 if (!ISSET(fp->f_mask, FM_FSTYPE)) {
430 set_fsmount(fp, FM_FSTYPE, strdup("nfs"));
431 log("set: fstype is %s", fp->f_fstype);
434 if (!ISSET(fp->f_mask, FM_OPTS)) {
435 set_fsmount(fp, FM_OPTS, strdup("rw,nosuid,grpid,defaults"));
436 log("set: opts are %s", fp->f_opts);
439 if (!ISSET(fp->f_mask, FM_LOCALNAME)) {
440 if (fp->f_ref) {
441 set_fsmount(fp, FM_LOCALNAME, strdup(fp->f_volname));
442 log("set: localname is %s", fp->f_localname);
443 } else {
444 lerror(fp->f_ioloc, "cannot determine localname since volname %s is not uniquely defined", fp->f_volname);
451 * For each disk on a host
452 * analyze the mount information
453 * and fill in any derivable
454 * details.
456 static void
457 analyze_drives(host *hp)
459 qelem *q = hp->h_disk_fs;
460 disk_fs *dp;
462 ITER(dp, disk_fs, q) {
463 int req;
464 log("Disk %s:", dp->d_dev);
465 dp->d_host = hp;
466 fixup_required_disk_info(dp);
467 req = ~dp->d_mask & DF_REQUIRED;
468 if (req)
469 show_required(dp->d_ioloc, req, dp->d_dev, hp->h_hostname, disk_fs_strings);
470 analyze_dkmounts(dp, dp->d_mount);
476 * Check that all static mounts make sense and
477 * that the source volumes exist.
479 static void
480 analyze_mounts(host *hp)
482 qelem *q = hp->h_mount;
483 fsmount *fp;
484 int netbootp = 0;
486 ITER(fp, fsmount, q) {
487 char *p;
488 char *nn = strdup(fp->f_volname);
489 int req;
490 dict_ent *de = (dict_ent *) NULL;
491 int found = 0;
492 int matched = 0;
494 if (ISSET(fp->f_mask, FM_DIRECT)) {
495 found = 1;
496 matched = 1;
497 } else
498 do {
499 p = 0;
500 de = find_volname(nn);
501 log("Mount: %s (trying %s)", fp->f_volname, nn);
503 if (de) {
504 found = 1;
507 * Check that the from field is really exporting
508 * the filesystem requested.
509 * LBL: If fake mount, then don't care about
510 * consistency check.
512 if (ISSET(fp->f_mask, FM_FROM) && !ISSET(fp->f_mask, FM_DIRECT)) {
513 dict_data *dd;
514 fsi_mount *mp2 = 0;
516 ITER(dd, dict_data, &de->de_q) {
517 fsi_mount *mp = (fsi_mount *) dd->dd_data;
519 if (STREQ(mp->m_dk->d_host->h_hostname, fp->f_from)) {
520 mp2 = mp;
521 break;
525 if (mp2) {
526 fp->f_ref = mp2;
527 matched = 1;
528 break;
530 } else {
531 matched = 1;
532 break;
535 p = strrchr(nn, '/');
536 if (p)
537 *p = 0;
538 } while (de && p);
539 XFREE(nn);
541 if (!found) {
542 lerror(fp->f_ioloc, "volname %s unknown", fp->f_volname);
543 } else if (matched) {
545 fixup_required_mount_info(fp, de);
546 req = ~fp->f_mask & FM_REQUIRED;
547 if (req) {
548 show_required(fp->f_ioloc, req, fp->f_volname, hp->h_hostname,
549 fsmount_strings);
550 } else if (STREQ(fp->f_localname, "/")) {
551 hp->h_netroot = fp;
552 netbootp |= FM_NETROOT;
553 } else if (STREQ(fp->f_localname, "swap")) {
554 hp->h_netswap = fp;
555 netbootp |= FM_NETSWAP;
558 } else {
559 lerror(fp->f_ioloc, "volname %s not exported from %s", fp->f_volname,
560 fp->f_from ? fp->f_from : "anywhere");
564 if (netbootp && (netbootp != FM_NETBOOT))
565 lerror(hp->h_ioloc, "network booting requires both root and swap areas");
569 void
570 analyze_hosts(qelem *q)
572 host *hp;
574 show_area_being_processed("analyze hosts", 5);
577 * Check all drives
579 ITER(hp, host, q) {
580 log("disks on host %s", hp->h_hostname);
581 show_new("ana-host");
582 hp->h_hostpath = compute_hostpath(hp->h_hostname);
584 if (hp->h_disk_fs)
585 analyze_drives(hp);
589 show_area_being_processed("analyze mounts", 5);
592 * Check static mounts
594 ITER(hp, host, q) {
595 log("mounts on host %s", hp->h_hostname);
596 show_new("ana-mount");
597 if (hp->h_mount)
598 analyze_mounts(hp);
605 * Check an automount request
607 static void
608 analyze_automount(automount *ap)
610 dict_ent *de = find_volname(ap->a_volname);
612 if (de) {
613 ap->a_mounted = de;
614 } else {
615 if (STREQ(ap->a_volname, ap->a_name))
616 lerror(ap->a_ioloc, "unknown volname %s automounted", ap->a_volname);
617 else
618 lerror(ap->a_ioloc, "unknown volname %s automounted on %s", ap->a_volname, ap->a_name);
623 static void
624 analyze_automount_tree(qelem *q, char *pref, int lvl)
626 automount *ap;
628 ITER(ap, automount, q) {
629 char nname[1024];
631 if (lvl > 0 || ap->a_mount)
632 if (ap->a_name[1] && strchr(ap->a_name + 1, '/'))
633 lerror(ap->a_ioloc, "not allowed '/' in a directory name");
634 sprintf(nname, "%s/%s", pref, ap->a_name);
635 XFREE(ap->a_name);
636 ap->a_name = strdup(nname[1] == '/' ? nname + 1 : nname);
637 log("automount point %s:", ap->a_name);
638 show_new("ana-automount");
640 if (ap->a_mount) {
641 analyze_automount_tree(ap->a_mount, ap->a_name, lvl + 1);
642 } else if (ap->a_hardwiredfs) {
643 log("\thardwired from %s to %s", ap->a_volname, ap->a_hardwiredfs);
644 } else if (ap->a_volname) {
645 log("\tautomount from %s", ap->a_volname);
646 analyze_automount(ap);
647 } else if (ap->a_symlink) {
648 log("\tsymlink to %s", ap->a_symlink);
649 } else {
650 ap->a_volname = strdup(ap->a_name);
651 log("\timplicit automount from %s", ap->a_volname);
652 analyze_automount(ap);
658 void
659 analyze_automounts(qelem *q)
661 auto_tree *tp;
663 show_area_being_processed("analyze automount", 5);
666 * q is a list of automounts
668 ITER(tp, auto_tree, q)
669 analyze_automount_tree(tp->t_mount, "", 0);