2 * Copyright (c) 1997-2006 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.
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
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
40 * File: am-utils/fsinfo/fsi_analyze.c
45 * Analyze filesystem declarations
47 * Note: most of this is magic!
52 #endif /* HAVE_CONFIG_H */
57 char *disk_fs_strings[] =
59 "fstype", "opts", "dumpset", "passno", "freq", "mount", "log", 0,
62 char *mount_strings[] =
64 "volname", "exportfs", 0,
67 char *fsmount_strings[] =
69 "as", "volname", "fstype", "opts", "from", 0,
72 char *host_strings[] =
74 "host", "netif", "config", "arch", "cluster", "os", 0,
77 char *ether_if_strings[] =
79 "inaddr", "netmask", "hwaddr", 0,
84 * Strip off the trailing part of a domain
85 * to produce a short-form domain relative
86 * to the local host domain.
87 * Note that this has no effect if the domain
88 * names do not have the same number of
89 * components. If that restriction proves
90 * to be a problem then the loop needs recoding
91 * to skip from right to left and do partial
92 * matches along the way -- ie more expensive.
95 domain_strip(char *otherdom, char *localdom)
99 if ((p1 = strchr(otherdom, '.')) &&
100 (p2 = strchr(localdom, '.')) &&
101 STREQ(p1 + 1, p2 + 1))
107 * Take a little-endian domain name and
108 * transform into a big-endian Un*x pathname.
109 * For example: kiska.doc.ic -> ic/doc/kiska
112 compute_hostpath(char *hn)
114 char *p = xmalloc(MAXPATHLEN);
116 char path[MAXPATHLEN];
118 xstrlcpy(p, hn, MAXPATHLEN);
119 domain_strip(p, hostname);
126 xstrlcat(path, d + 1, sizeof(path));
127 xstrlcat(path, "/", sizeof(path));
129 xstrlcat(path, p, sizeof(path));
133 fsi_log("hostpath of '%s' is '%s'", hn, path);
135 xstrlcpy(p, path, MAXPATHLEN);
141 find_volname(char *nn)
144 char *p = strdup(nn);
148 fsi_log("Searching for volname %s", p);
149 de = dict_locate(dict_of_volnames, p);
161 show_required(ioloc *l, int mask, char *info, char *hostname, char *strings[])
164 fsi_log("mask left for %s:%s is %#x", hostname, info, mask);
166 for (i = 0; strings[i]; 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.
178 check_exportfs(qelem *q, fsi_mount *e)
183 ITER(mp, fsi_mount, q) {
184 if (ISSET(mp->m_mask, DM_EXPORTFS)) {
186 lwarning(mp->m_ioloc, "%s has duplicate exportfs data", mp->m_name);
188 if (!ISSET(mp->m_mask, DM_VOLNAME))
189 set_mount(mp, DM_VOLNAME, strdup(mp->m_name));
195 * Recursively descend the mount tree
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);
215 analyze_dkmount_tree(qelem *q, fsi_mount *parent, disk_fs *dk)
220 ITER(mp, fsi_mount, q) {
221 fsi_log("Mount %s:", mp->m_name);
224 xsnprintf(n, sizeof(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 fsi_log("Changing name %s to %s", mp->m_name, n);
231 mp->m_name = strdup(n);
234 mp->m_name_len = strlen(mp->m_name);
235 mp->m_parent = parent;
238 analyze_dkmount_tree(mp->m_mount, mp, dk);
246 * The mount tree is a singleton list
247 * containing the top-level mount
251 analyze_dkmounts(disk_fs *dk, qelem *q)
254 fsi_mount *mp, *mp2 = 0;
258 * First scan the list of subdirs to make
259 * sure there is only one - and remember it
262 ITER(mp, fsi_mount, q) {
272 lerror(dk->d_ioloc, "%s:%s has no mount point", dk->d_host->h_hostname, dk->d_dev);
277 lerror(dk->d_ioloc, "%s:%s has more than one mount point", dk->d_host->h_hostname, dk->d_dev);
282 * Now see if a default mount point is required
284 if (mp2 && STREQ(mp2->m_name, "default")) {
285 if (ISSET(mp2->m_mask, DM_VOLNAME)) {
287 compute_automount_point(nbuf, sizeof(nbuf), dk->d_host, mp2->m_volname);
289 mp2->m_name = strdup(nbuf);
290 fsi_log("%s:%s has default mount on %s", dk->d_host->h_hostname, dk->d_dev, mp2->m_name);
292 lerror(dk->d_ioloc, "no volname given for %s:%s", dk->d_host->h_hostname, dk->d_dev);
298 * Fill in the disk mount point
300 if (!errors && mp2 && mp2->m_name)
301 dk->d_mountpt = strdup(mp2->m_name);
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);
320 fixup_required_disk_info(disk_fs *dp)
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)) {
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);
343 if (!ISSET(dp->d_mask, DF_FREQ)) {
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);
355 if (!ISSET(dp->d_mask, DF_OPTS))
356 set_disk_fs(dp, DF_OPTS, strdup("swap"));
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);
369 BITSET(dp->d_mask, DF_MOUNT);
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")) {
378 if (!ISSET(dp->d_mask, DF_PASSNO)) {
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);
390 if (!ISSET(dp->d_mask, DF_FREQ)) {
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);
402 if (!ISSET(dp->d_mask, DF_OPTS))
403 set_disk_fs(dp, DF_OPTS, strdup("rw,defaults"));
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);
419 dd = AM_FIRST(dict_data, &de->de_q);
420 mp = (fsi_mount *) dd->dd_data;
424 set_fsmount(fp, FM_FROM, mp->m_dk->d_host->h_hostname);
425 fsi_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 fsi_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 fsi_log("set: opts are %s", fp->f_opts);
439 if (!ISSET(fp->f_mask, FM_LOCALNAME)) {
441 set_fsmount(fp, FM_LOCALNAME, strdup(fp->f_volname));
442 fsi_log("set: localname is %s", fp->f_localname);
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
457 analyze_drives(host *hp)
459 qelem *q = hp->h_disk_fs;
462 ITER(dp, disk_fs, q) {
464 fsi_log("Disk %s:", dp->d_dev);
466 fixup_required_disk_info(dp);
467 req = ~dp->d_mask & DF_REQUIRED;
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.
480 analyze_mounts(host *hp)
482 qelem *q = hp->h_mount;
486 ITER(fp, fsmount, q) {
488 char *nn = strdup(fp->f_volname);
490 dict_ent *de = (dict_ent *) NULL;
494 if (ISSET(fp->f_mask, FM_DIRECT)) {
500 de = find_volname(nn);
501 fsi_log("Mount: %s (trying %s)", fp->f_volname, nn);
507 * Check that the from field is really exporting
508 * the filesystem requested.
509 * LBL: If fake mount, then don't care about
512 if (ISSET(fp->f_mask, FM_FROM) && !ISSET(fp->f_mask, FM_DIRECT)) {
516 ITER(dd, dict_data, &de->de_q) {
517 fsi_mount *mp = (fsi_mount *) dd->dd_data;
520 STREQ(mp->m_dk->d_host->h_hostname, fp->f_from)) {
536 p = strrchr(nn, '/');
543 lerror(fp->f_ioloc, "volname %s unknown", fp->f_volname);
544 } else if (matched) {
547 fixup_required_mount_info(fp, de);
548 req = ~fp->f_mask & FM_REQUIRED;
550 show_required(fp->f_ioloc, req, fp->f_volname, hp->h_hostname,
552 } else if (STREQ(fp->f_localname, "/")) {
554 netbootp |= FM_NETROOT;
555 } else if (STREQ(fp->f_localname, "swap")) {
557 netbootp |= FM_NETSWAP;
561 lerror(fp->f_ioloc, "volname %s not exported from %s", fp->f_volname,
562 fp->f_from ? fp->f_from : "anywhere");
566 if (netbootp && (netbootp != FM_NETBOOT))
567 lerror(hp->h_ioloc, "network booting requires both root and swap areas");
572 analyze_hosts(qelem *q)
576 show_area_being_processed("analyze hosts", 5);
582 fsi_log("disks on host %s", hp->h_hostname);
583 show_new("ana-host");
584 hp->h_hostpath = compute_hostpath(hp->h_hostname);
591 show_area_being_processed("analyze mounts", 5);
594 * Check static mounts
597 fsi_log("mounts on host %s", hp->h_hostname);
598 show_new("ana-mount");
607 * Check an automount request
610 analyze_automount(automount *ap)
612 dict_ent *de = find_volname(ap->a_volname);
617 if (STREQ(ap->a_volname, ap->a_name))
618 lerror(ap->a_ioloc, "unknown volname %s automounted", ap->a_volname);
620 lerror(ap->a_ioloc, "unknown volname %s automounted on %s", ap->a_volname, ap->a_name);
626 analyze_automount_tree(qelem *q, char *pref, int lvl)
630 ITER(ap, automount, q) {
633 if (lvl > 0 || ap->a_mount)
634 if (ap->a_name[1] && strchr(ap->a_name + 1, '/'))
635 lerror(ap->a_ioloc, "not allowed '/' in a directory name");
636 xsnprintf(nname, sizeof(nname), "%s/%s", pref, ap->a_name);
638 ap->a_name = strdup(nname[1] == '/' ? nname + 1 : nname);
639 fsi_log("automount point %s:", ap->a_name);
640 show_new("ana-automount");
643 analyze_automount_tree(ap->a_mount, ap->a_name, lvl + 1);
644 } else if (ap->a_hardwiredfs) {
645 fsi_log("\thardwired from %s to %s", ap->a_volname, ap->a_hardwiredfs);
646 } else if (ap->a_volname) {
647 fsi_log("\tautomount from %s", ap->a_volname);
648 analyze_automount(ap);
649 } else if (ap->a_symlink) {
650 fsi_log("\tsymlink to %s", ap->a_symlink);
652 ap->a_volname = strdup(ap->a_name);
653 fsi_log("\timplicit automount from %s", ap->a_volname);
654 analyze_automount(ap);
661 analyze_automounts(qelem *q)
665 show_area_being_processed("analyze automount", 5);
668 * q is a list of automounts
670 ITER(tp, auto_tree, q)
671 analyze_automount_tree(tp->t_mount, "", 0);