4 * Copyright (c) 1997-2009 Erez Zadok
5 * Copyright (c) 1989 Jan-Simon Pendry
6 * Copyright (c) 1989 Imperial College of Science, Technology & Medicine
7 * Copyright (c) 1989 The Regents of the University of California.
10 * This code is derived from software contributed to Berkeley by
11 * Jan-Simon Pendry at Imperial College, London.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgment:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
42 * File: am-utils/fsinfo/fsi_analyze.c
47 * Analyze filesystem declarations
49 * Note: most of this is magic!
54 #endif /* HAVE_CONFIG_H */
59 char *disk_fs_strings
[] =
61 "fstype", "opts", "dumpset", "passno", "freq", "mount", "log", NULL
,
64 char *mount_strings
[] =
66 "volname", "exportfs", NULL
,
69 char *fsmount_strings
[] =
71 "as", "volname", "fstype", "opts", "from", NULL
,
74 char *host_strings
[] =
76 "host", "netif", "config", "arch", "cluster", "os", NULL
,
79 char *ether_if_strings
[] =
81 "inaddr", "netmask", "hwaddr", NULL
,
86 * Strip off the trailing part of a domain
87 * to produce a short-form domain relative
88 * to the local host domain.
89 * Note that this has no effect if the domain
90 * names do not have the same number of
91 * components. If that restriction proves
92 * to be a problem then the loop needs recoding
93 * to skip from right to left and do partial
94 * matches along the way -- ie more expensive.
97 domain_strip(char *otherdom
, char *localdom
)
101 if ((p1
= strchr(otherdom
, '.')) &&
102 (p2
= strchr(localdom
, '.')) &&
103 STREQ(p1
+ 1, p2
+ 1))
109 * Take a little-endian domain name and
110 * transform into a big-endian Un*x pathname.
111 * For example: kiska.doc.ic -> ic/doc/kiska
114 compute_hostpath(char *hn
)
116 char *p
= xmalloc(MAXPATHLEN
);
118 char path
[MAXPATHLEN
];
120 xstrlcpy(p
, hn
, MAXPATHLEN
);
121 domain_strip(p
, hostname
);
128 xstrlcat(path
, d
+ 1, sizeof(path
));
129 xstrlcat(path
, "/", sizeof(path
));
131 xstrlcat(path
, p
, sizeof(path
));
135 fsi_log("hostpath of '%s' is '%s'", hn
, path
);
137 xstrlcpy(p
, path
, MAXPATHLEN
);
143 find_volname(char *nn
)
146 char *p
= strdup(nn
);
150 fsi_log("Searching for volname %s", p
);
151 de
= dict_locate(dict_of_volnames
, p
);
163 show_required(ioloc
*l
, int mask
, char *info
, char *hostname
, char *strings
[])
166 fsi_log("mask left for %s:%s is %#x", hostname
, info
, mask
);
168 for (i
= 0; strings
[i
]; i
++)
170 lerror(l
, "%s:%s needs field \"%s\"", hostname
, info
, strings
[i
]);
175 * Check and fill in "exportfs" details.
176 * Make sure the m_exported field references
177 * the most local node with an "exportfs" entry.
180 check_exportfs(qelem
*q
, fsi_mount
*e
)
185 ITER(mp
, fsi_mount
, q
) {
186 if (ISSET(mp
->m_mask
, DM_EXPORTFS
)) {
188 lwarning(mp
->m_ioloc
, "%s has duplicate exportfs data", mp
->m_name
);
190 if (!ISSET(mp
->m_mask
, DM_VOLNAME
))
191 set_mount(mp
, DM_VOLNAME
, strdup(mp
->m_name
));
197 * Recursively descend the mount tree
200 errors
+= check_exportfs(mp
->m_mount
, mp
->m_exported
);
203 * If a volume name has been specified, but this node and none
204 * of its parents has been exported, report an error.
206 if (ISSET(mp
->m_mask
, DM_VOLNAME
) && !mp
->m_exported
) {
207 lerror(mp
->m_ioloc
, "%s has a volname but no exportfs data", mp
->m_name
);
217 analyze_dkmount_tree(qelem
*q
, fsi_mount
*parent
, disk_fs
*dk
)
222 ITER(mp
, fsi_mount
, q
) {
223 fsi_log("Mount %s:", mp
->m_name
);
226 xsnprintf(n
, sizeof(n
), "%s/%s", parent
->m_name
, mp
->m_name
);
227 if (*mp
->m_name
== '/')
228 lerror(mp
->m_ioloc
, "sub-directory %s of %s starts with '/'", mp
->m_name
, parent
->m_name
);
229 else if (STREQ(mp
->m_name
, "default"))
230 lwarning(mp
->m_ioloc
, "sub-directory of %s is named \"default\"", parent
->m_name
);
231 fsi_log("Changing name %s to %s", mp
->m_name
, n
);
233 mp
->m_name
= strdup(n
);
236 mp
->m_name_len
= strlen(mp
->m_name
);
237 mp
->m_parent
= parent
;
240 analyze_dkmount_tree(mp
->m_mount
, mp
, dk
);
248 * The mount tree is a singleton list
249 * containing the top-level mount
253 analyze_dkmounts(disk_fs
*dk
, qelem
*q
)
256 fsi_mount
*mp
, *mp2
= NULL
;
260 * First scan the list of subdirs to make
261 * sure there is only one - and remember it
264 ITER(mp
, fsi_mount
, q
) {
274 lerror(dk
->d_ioloc
, "%s:%s has no mount point", dk
->d_host
->h_hostname
, dk
->d_dev
);
279 lerror(dk
->d_ioloc
, "%s:%s has more than one mount point", dk
->d_host
->h_hostname
, dk
->d_dev
);
284 * Now see if a default mount point is required
286 if (mp2
&& STREQ(mp2
->m_name
, "default")) {
287 if (ISSET(mp2
->m_mask
, DM_VOLNAME
)) {
289 compute_automount_point(nbuf
, sizeof(nbuf
), dk
->d_host
, mp2
->m_volname
);
291 mp2
->m_name
= strdup(nbuf
);
292 fsi_log("%s:%s has default mount on %s", dk
->d_host
->h_hostname
, dk
->d_dev
, mp2
->m_name
);
294 lerror(dk
->d_ioloc
, "no volname given for %s:%s", dk
->d_host
->h_hostname
, dk
->d_dev
);
300 * Fill in the disk mount point
302 if (!errors
&& mp2
&& mp2
->m_name
)
303 dk
->d_mountpt
= strdup(mp2
->m_name
);
305 dk
->d_mountpt
= strdup("error");
308 * Analyze the mount tree
310 errors
+= analyze_dkmount_tree(q
, NULL
, dk
);
313 * Analyze the export tree
315 errors
+= check_exportfs(q
, NULL
);
322 fixup_required_disk_info(disk_fs
*dp
)
327 if (ISSET(dp
->d_mask
, DF_FSTYPE
)) {
328 if (STREQ(dp
->d_fstype
, "swap")) {
331 * Fixup for a swap device
333 if (!ISSET(dp
->d_mask
, DF_PASSNO
)) {
335 BITSET(dp
->d_mask
, DF_PASSNO
);
336 } else if (dp
->d_freq
!= 0) {
337 lwarning(dp
->d_ioloc
,
338 "Pass number for %s:%s is non-zero",
339 dp
->d_host
->h_hostname
, dp
->d_dev
);
345 if (!ISSET(dp
->d_mask
, DF_FREQ
)) {
347 BITSET(dp
->d_mask
, DF_FREQ
);
348 } else if (dp
->d_freq
!= 0) {
349 lwarning(dp
->d_ioloc
,
350 "dump frequency for %s:%s is non-zero",
351 dp
->d_host
->h_hostname
, dp
->d_dev
);
357 if (!ISSET(dp
->d_mask
, DF_OPTS
))
358 set_disk_fs(dp
, DF_OPTS
, strdup("swap"));
363 if (!ISSET(dp
->d_mask
, DF_MOUNT
)) {
364 qelem
*q
= new_que();
365 fsi_mount
*m
= new_mount();
367 m
->m_name
= strdup("swap");
368 m
->m_mount
= new_que();
369 ins_que(&m
->m_q
, q
->q_back
);
371 BITSET(dp
->d_mask
, DF_MOUNT
);
373 lerror(dp
->d_ioloc
, "%s: mount field specified for swap partition", dp
->d_host
->h_hostname
);
375 } else if (STREQ(dp
->d_fstype
, "export")) {
380 if (!ISSET(dp
->d_mask
, DF_PASSNO
)) {
382 BITSET(dp
->d_mask
, DF_PASSNO
);
383 } else if (dp
->d_passno
!= 0) {
384 lwarning(dp
->d_ioloc
,
385 "pass number for %s:%s is non-zero",
386 dp
->d_host
->h_hostname
, dp
->d_dev
);
392 if (!ISSET(dp
->d_mask
, DF_FREQ
)) {
394 BITSET(dp
->d_mask
, DF_FREQ
);
395 } else if (dp
->d_freq
!= 0) {
396 lwarning(dp
->d_ioloc
,
397 "dump frequency for %s:%s is non-zero",
398 dp
->d_host
->h_hostname
, dp
->d_dev
);
404 if (!ISSET(dp
->d_mask
, DF_OPTS
))
405 set_disk_fs(dp
, DF_OPTS
, strdup("rw,defaults"));
413 fixup_required_mount_info(fsmount
*fp
, dict_ent
*de
)
415 if (!ISSET(fp
->f_mask
, FM_FROM
)) {
416 if (de
->de_count
!= 1) {
417 lerror(fp
->f_ioloc
, "ambiguous mount: %s is a replicated filesystem", fp
->f_volname
);
420 fsi_mount
*mp
= NULL
;
421 dd
= AM_FIRST(dict_data
, &de
->de_q
);
422 mp
= (fsi_mount
*) dd
->dd_data
;
426 set_fsmount(fp
, FM_FROM
, mp
->m_dk
->d_host
->h_hostname
);
427 fsi_log("set: %s comes from %s", fp
->f_volname
, fp
->f_from
);
431 if (!ISSET(fp
->f_mask
, FM_FSTYPE
)) {
432 set_fsmount(fp
, FM_FSTYPE
, strdup("nfs"));
433 fsi_log("set: fstype is %s", fp
->f_fstype
);
436 if (!ISSET(fp
->f_mask
, FM_OPTS
)) {
437 set_fsmount(fp
, FM_OPTS
, strdup("rw,nosuid,grpid,defaults"));
438 fsi_log("set: opts are %s", fp
->f_opts
);
441 if (!ISSET(fp
->f_mask
, FM_LOCALNAME
)) {
443 set_fsmount(fp
, FM_LOCALNAME
, strdup(fp
->f_volname
));
444 fsi_log("set: localname is %s", fp
->f_localname
);
446 lerror(fp
->f_ioloc
, "cannot determine localname since volname %s is not uniquely defined", fp
->f_volname
);
453 * For each disk on a host
454 * analyze the mount information
455 * and fill in any derivable
459 analyze_drives(host
*hp
)
461 qelem
*q
= hp
->h_disk_fs
;
464 ITER(dp
, disk_fs
, q
) {
466 fsi_log("Disk %s:", dp
->d_dev
);
468 fixup_required_disk_info(dp
);
469 req
= ~dp
->d_mask
& DF_REQUIRED
;
471 show_required(dp
->d_ioloc
, req
, dp
->d_dev
, hp
->h_hostname
, disk_fs_strings
);
472 analyze_dkmounts(dp
, dp
->d_mount
);
478 * Check that all static mounts make sense and
479 * that the source volumes exist.
482 analyze_mounts(host
*hp
)
484 qelem
*q
= hp
->h_mount
;
488 ITER(fp
, fsmount
, q
) {
490 char *nn
= strdup(fp
->f_volname
);
492 dict_ent
*de
= (dict_ent
*) NULL
;
496 if (ISSET(fp
->f_mask
, FM_DIRECT
)) {
502 de
= find_volname(nn
);
503 fsi_log("Mount: %s (trying %s)", fp
->f_volname
, nn
);
509 * Check that the from field is really exporting
510 * the filesystem requested.
511 * LBL: If fake mount, then don't care about
514 if (ISSET(fp
->f_mask
, FM_FROM
) && !ISSET(fp
->f_mask
, FM_DIRECT
)) {
516 fsi_mount
*mp2
= NULL
;
518 ITER(dd
, dict_data
, &de
->de_q
) {
519 fsi_mount
*mp
= (fsi_mount
*) dd
->dd_data
;
522 STREQ(mp
->m_dk
->d_host
->h_hostname
, fp
->f_from
)) {
538 p
= strrchr(nn
, '/');
545 lerror(fp
->f_ioloc
, "volname %s unknown", fp
->f_volname
);
546 } else if (matched
) {
549 fixup_required_mount_info(fp
, de
);
550 req
= ~fp
->f_mask
& FM_REQUIRED
;
552 show_required(fp
->f_ioloc
, req
, fp
->f_volname
, hp
->h_hostname
,
554 } else if (STREQ(fp
->f_localname
, "/")) {
556 netbootp
|= FM_NETROOT
;
557 } else if (STREQ(fp
->f_localname
, "swap")) {
559 netbootp
|= FM_NETSWAP
;
563 lerror(fp
->f_ioloc
, "volname %s not exported from %s", fp
->f_volname
,
564 fp
->f_from
? fp
->f_from
: "anywhere");
568 if (netbootp
&& (netbootp
!= FM_NETBOOT
))
569 lerror(hp
->h_ioloc
, "network booting requires both root and swap areas");
574 analyze_hosts(qelem
*q
)
578 show_area_being_processed("analyze hosts", 5);
584 fsi_log("disks on host %s", hp
->h_hostname
);
585 show_new("ana-host");
586 hp
->h_hostpath
= compute_hostpath(hp
->h_hostname
);
593 show_area_being_processed("analyze mounts", 5);
596 * Check static mounts
599 fsi_log("mounts on host %s", hp
->h_hostname
);
600 show_new("ana-mount");
609 * Check an automount request
612 analyze_automount(automount
*ap
)
614 dict_ent
*de
= find_volname(ap
->a_volname
);
619 if (STREQ(ap
->a_volname
, ap
->a_name
))
620 lerror(ap
->a_ioloc
, "unknown volname %s automounted", ap
->a_volname
);
622 lerror(ap
->a_ioloc
, "unknown volname %s automounted on %s", ap
->a_volname
, ap
->a_name
);
628 analyze_automount_tree(qelem
*q
, char *pref
, int lvl
)
632 ITER(ap
, automount
, q
) {
635 if (lvl
> 0 || ap
->a_mount
)
636 if (ap
->a_name
[1] && strchr(ap
->a_name
+ 1, '/'))
637 lerror(ap
->a_ioloc
, "not allowed '/' in a directory name");
638 xsnprintf(nname
, sizeof(nname
), "%s/%s", pref
, ap
->a_name
);
640 ap
->a_name
= strdup(nname
[1] == '/' ? nname
+ 1 : nname
);
641 fsi_log("automount point %s:", ap
->a_name
);
642 show_new("ana-automount");
645 analyze_automount_tree(ap
->a_mount
, ap
->a_name
, lvl
+ 1);
646 } else if (ap
->a_hardwiredfs
) {
647 fsi_log("\thardwired from %s to %s", ap
->a_volname
, ap
->a_hardwiredfs
);
648 } else if (ap
->a_volname
) {
649 fsi_log("\tautomount from %s", ap
->a_volname
);
650 analyze_automount(ap
);
651 } else if (ap
->a_symlink
) {
652 fsi_log("\tsymlink to %s", ap
->a_symlink
);
654 ap
->a_volname
= strdup(ap
->a_name
);
655 fsi_log("\timplicit automount from %s", ap
->a_volname
);
656 analyze_automount(ap
);
663 analyze_automounts(qelem
*q
)
667 show_area_being_processed("analyze automount", 5);
670 * q is a list of automounts
672 ITER(tp
, auto_tree
, q
)
673 analyze_automount_tree(tp
->t_mount
, "", 0);