1/*
2 * Copyright (c) 1997-2014 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.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Jan-Simon Pendry at Imperial College, London.
10 *
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. Neither the name of the University nor the names of its contributors
20 *    may be used to endorse or promote products derived from this software
21 *    without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 *
36 * File: am-utils/fsinfo/wr_fstab.c
37 *
38 */
39
40#ifdef HAVE_CONFIG_H
41# include <config.h>
42#endif /* HAVE_CONFIG_H */
43#include <am_defs.h>
44#include <fsi_data.h>
45#include <fsinfo.h>
46
47#define	GENERIC_OS_NAME "generic"
48
49/* forward definitions */
50static void write_aix1_dkfstab(FILE *ef, disk_fs *dp);
51static void write_aix1_dkrmount(FILE *ef, char *hn, fsmount *fp);
52static void write_aix3_dkfstab(FILE *ef, disk_fs *dp);
53static void write_aix3_dkrmount(FILE *ef, char *hn, fsmount *fp);
54static int  write_dkfstab(FILE *ef, qelem *q, void (*output) (FILE *, disk_fs *));
55static int write_dkrmount(FILE *ef, qelem *q, char *hn, void (*output) (FILE *, char *, fsmount *));
56static void write_generic_dkfstab(FILE *ef, disk_fs *dp);
57static void write_generic_dkrmount(FILE *ef, char *hn, fsmount *fp);
58static void write_ultrix_dkfstab(FILE *ef, disk_fs *dp);
59static void write_ultrix_dkrmount(FILE *ef, char *hn, fsmount *fp);
60
61/* ----------------------------------------------- */
62
63static struct os_fstab_type {
64  char *os_name;
65  void (*op_fstab) (FILE *ef, disk_fs *dp);
66  void (*op_mount) (FILE *ef, char *hn, fsmount *fp);
67} os_tabs[] = {
68
69  {
70    "aix1", write_aix1_dkfstab, write_aix1_dkrmount
71  },				/* AIX 1 */
72  {
73    "aix3", write_aix3_dkfstab, write_aix3_dkrmount
74  },				/* AIX 3 */
75  {
76    "generic", write_generic_dkfstab, write_generic_dkrmount
77  },				/* Generic */
78  {
79    "u2_0", write_ultrix_dkfstab, write_ultrix_dkrmount
80  },				/* Ultrix */
81  {
82    "u3_0", write_ultrix_dkfstab, write_ultrix_dkrmount
83  },				/* Ultrix */
84  {
85    "u4_0", write_ultrix_dkfstab, write_ultrix_dkrmount
86  },				/* Ultrix */
87  {
88    NULL, NULL, NULL
89  }
90};
91
92
93/* ---------- AIX 1 ------------------------------ */
94
95/*
96 * AIX 1 format
97 */
98static void
99write_aix1_dkfstab(FILE *ef, disk_fs *dp)
100{
101  char *hp = xstrdup(dp->d_host->h_hostname);
102  char *p = strchr(hp, '.');
103
104  if (p)
105    *p = '\0';
106
107  fprintf(ef, "\n%s:\n\tdev = %s\n\tvfs = %s\n\ttype = %s\n\tlog = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
108	  dp->d_mountpt,
109	  dp->d_dev,
110	  dp->d_fstype,
111	  dp->d_fstype,
112	  dp->d_log,
113	  dp->d_mountpt,
114	  dp->d_opts);
115  XFREE(hp);
116}
117
118
119static void
120write_aix1_dkrmount(FILE *ef, char *hn, fsmount *fp)
121{
122  char *h = xstrdup(fp->f_ref->m_dk->d_host->h_hostname);
123  char *hp = xstrdup(h);
124  char *p = strchr(hp, '.');
125
126  if (p)
127    *p = '\0';
128  domain_strip(h, hn);
129  fprintf(ef, "\n%s:\n\tsite = %s\n\tdev = %s:%s\n\tvfs = %s\n\ttype = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
130	  fp->f_localname,
131	  hp,
132	  h,
133	  fp->f_volname,
134	  fp->f_fstype,
135	  fp->f_fstype,
136	  fp->f_localname,
137	  fp->f_opts);
138
139  XFREE(hp);
140  XFREE(h);
141}
142
143
144/* ---------- AIX 3 ------------------------------ */
145
146/*
147 * AIX 3 format
148 */
149static void
150write_aix3_dkfstab(FILE *ef, disk_fs *dp)
151{
152  if (STREQ(dp->d_fstype, "jfs") &&
153      NSTREQ(dp->d_dev, "/dev/", 5) &&
154      !dp->d_log)
155    error("aix 3 needs a log device for journalled filesystem (jfs) mounts");
156
157  fprintf(ef, "\n%s:\n\tdev = %s\n\tvfs = %s\n\ttype = %s\n\tlog = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
158	  dp->d_mountpt,
159	  dp->d_dev,
160	  dp->d_fstype,
161	  dp->d_fstype,
162	  dp->d_log,
163	  dp->d_mountpt,
164	  dp->d_opts);
165}
166
167
168static void
169write_aix3_dkrmount(FILE *ef, char *hn, fsmount *fp)
170{
171  char *h = xstrdup(fp->f_ref->m_dk->d_host->h_hostname);
172
173  domain_strip(h, hn);
174  fprintf(ef, "\n%s:\n\tdev = %s:%s\n\tvfs = %s\n\ttype = %s\n\tvol = %s\n\topts = %s\n\tmount = true\n\tcheck = true\n\tfree = false\n",
175	  fp->f_localname,
176	  h,
177	  fp->f_volname,
178	  fp->f_fstype,
179	  fp->f_fstype,
180	  fp->f_localname,
181	  fp->f_opts);
182
183  XFREE(h);
184}
185
186
187/* ---------- Ultrix ----------------------------- */
188
189static void
190write_ultrix_dkfstab(FILE *ef, disk_fs *dp)
191{
192  fprintf(ef, "%s:%s:%s:%s:%d:%d\n",
193	  dp->d_dev,
194	  dp->d_mountpt,
195	  dp->d_fstype,
196	  dp->d_opts,
197	  dp->d_freq,
198	  dp->d_passno);
199}
200
201
202static void
203write_ultrix_dkrmount(FILE *ef, char *hn, fsmount *fp)
204{
205  char *h = xstrdup(fp->f_ref->m_dk->d_host->h_hostname);
206
207  domain_strip(h, hn);
208  fprintf(ef, "%s@%s:%s:%s:%s:0:0\n",
209	  fp->f_volname,
210	  h,
211	  fp->f_localname,
212	  fp->f_fstype,
213	  fp->f_opts);
214  XFREE(h);
215}
216
217
218/* ---------- Generic ---------------------------- */
219
220/*
221 * Generic (BSD, SunOS, HPUX) format
222 */
223static void
224write_generic_dkfstab(FILE *ef, disk_fs *dp)
225{
226  fprintf(ef, "%s %s %s %s %d %d\n",
227	  dp->d_dev,
228	  dp->d_mountpt,
229	  dp->d_fstype,
230	  dp->d_opts,
231	  dp->d_freq,
232	  dp->d_passno);
233}
234
235
236static void
237write_generic_dkrmount(FILE *ef, char *hn, fsmount *fp)
238{
239  char *h;
240
241  if (fp->f_ref) {
242    h = xstrdup(fp->f_ref->m_dk->d_host->h_hostname);
243  } else {
244    h = xstrdup(fp->f_from);
245  }
246  domain_strip(h, hn);
247  fprintf(ef, "%s:%s %s %s %s 0 0\n",
248	  h,
249	  fp->f_volname,
250	  fp->f_localname,
251	  fp->f_fstype,
252	  fp->f_opts);
253  XFREE(h);
254}
255
256
257static struct os_fstab_type *
258find_fstab_type(host *hp)
259{
260  struct os_fstab_type *op = NULL;
261  char *os_name = NULL;
262
263again:;
264  if (os_name == 0) {
265    if (ISSET(hp->h_mask, HF_OS))
266      os_name = hp->h_os;
267    else
268      os_name = GENERIC_OS_NAME;
269  }
270  for (op = os_tabs; op->os_name; op++)
271    if (STREQ(os_name, op->os_name))
272      return op;
273
274  os_name = GENERIC_OS_NAME;
275  goto again;
276}
277
278
279static int
280write_dkfstab(FILE *ef, qelem *q, void (*output) (FILE *, disk_fs *))
281{
282  int errors = 0;
283  disk_fs *dp;
284
285  ITER(dp, disk_fs, q)
286  if (!STREQ(dp->d_fstype, "export"))
287      (*output) (ef, dp);
288
289  return errors;
290}
291
292
293static int
294write_dkrmount(FILE *ef, qelem *q, char *hn, void (*output) (FILE *, char *, fsmount *))
295{
296  int errors = 0;
297  fsmount *fp;
298
299  ITER(fp, fsmount, q)
300    (*output) (ef, hn, fp);
301
302  return errors;
303}
304
305
306int
307write_fstab(qelem *q)
308{
309  int errors = 0;
310
311  if (fstab_pref) {
312    host *hp;
313
314    show_area_being_processed("write fstab", 4);
315    ITER(hp, host, q) {
316      if (hp->h_disk_fs || hp->h_mount) {
317	FILE *ef = pref_open(fstab_pref, hp->h_hostname, gen_hdr, hp->h_hostname);
318	if (ef) {
319	  struct os_fstab_type *op = find_fstab_type(hp);
320	  show_new(hp->h_hostname);
321	  if (hp->h_disk_fs)
322	    errors += write_dkfstab(ef, hp->h_disk_fs, op->op_fstab);
323	  else
324	    fsi_log("No local disk mounts on %s", hp->h_hostname);
325
326	  if (hp->h_mount)
327	    errors += write_dkrmount(ef, hp->h_mount, hp->h_hostname, op->op_mount);
328
329	  pref_close(ef);
330	}
331      } else {
332	error("no disk mounts on %s", hp->h_hostname);
333      }
334    }
335  }
336  return errors;
337}
338