Merge from vendor branch LIBARCHIVE:
[dragonfly.git] / contrib / amd / amd / am_ops.c
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.
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. 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.
26  *
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.
38  *
39  *      %W% (Berkeley) %G%
40  *
41  * $Id: am_ops.c,v 1.4 1999/03/13 17:03:26 ezk Exp $
42  *
43  */
44
45 #ifdef HAVE_CONFIG_H
46 # include <config.h>
47 #endif /* HAVE_CONFIG_H */
48 #include <am_defs.h>
49 #include <amd.h>
50
51
52 /*
53  * The order of these entries matters, since lookups in this table are done
54  * on a first-match basis.  The entries below are a mixture of native
55  * filesystems supported by the OS (HAVE_FS_FOO), and some meta-filesystems
56  * supported by amd (HAVE_AM_FS_FOO).  The order is set here in expected
57  * match-hit such that more popular filesystems are listed first (nfs is the
58  * most popular, followed by a symlink F/S)
59  */
60 static am_ops *vops[] =
61 {
62 #ifdef HAVE_FS_NFS
63   &nfs_ops,                     /* network F/S (version 2) */
64 #endif /* HAVE_FS_NFS */
65 #ifdef HAVE_AM_FS_LINK
66   &amfs_link_ops,               /* symlink F/S */
67 #endif /* HAVE_AM_FS_LINK */
68
69   /*
70    * Other amd-supported meta-filesystems.
71    */
72 #ifdef HAVE_AM_FS_NFSX
73   &amfs_nfsx_ops,               /* multiple-nfs F/S */
74 #endif /* HAVE_AM_FS_NFSX */
75 #ifdef HAVE_AM_FS_NFSL
76   &amfs_nfsl_ops,               /* NFS with local link existence check */
77 #endif /* HAVE_AM_FS_NFSL */
78 #ifdef HAVE_AM_FS_HOST
79   &amfs_host_ops,               /* multiple exported nfs F/S */
80 #endif /* HAVE_AM_FS_HOST */
81 #ifdef HAVE_AM_FS_LINKX
82   &amfs_linkx_ops,              /* symlink F/S with link target verify */
83 #endif /* HAVE_AM_FS_LINKX */
84 #ifdef HAVE_AM_FS_PROGRAM
85   &amfs_program_ops,            /* program F/S */
86 #endif /* HAVE_AM_FS_PROGRAM */
87 #ifdef HAVE_AM_FS_UNION
88   &amfs_union_ops,              /* union F/S */
89 #endif /* HAVE_AM_FS_UNION */
90 #ifdef HAVE_AM_FS_INHERIT
91   &amfs_inherit_ops,            /* inheritance F/S */
92 #endif /* HAVE_AM_FS_INHERIT */
93
94   /*
95    * A few more native filesystems.
96    */
97 #ifdef HAVE_FS_UFS
98   &ufs_ops,                     /* Unix F/S */
99 #endif /* HAVE_FS_UFS */
100 #ifdef HAVE_FS_XFS
101   &xfs_ops,                     /* Unix (irix) F/S */
102 #endif /* HAVE_FS_XFS */
103 #ifdef HAVE_FS_EFS
104   &efs_ops,                     /* Unix (irix) F/S */
105 #endif /* HAVE_FS_EFS */
106 #ifdef HAVE_FS_LOFS
107   &lofs_ops,                    /* loopback F/S */
108 #endif /* HAVE_FS_LOFS */
109 #ifdef HAVE_FS_CDFS
110   &cdfs_ops,                    /* CDROM/HSFS/ISO9960 F/S */
111 #endif /* HAVE_FS_CDFS */
112 #ifdef HAVE_FS_PCFS
113   &pcfs_ops,                    /* Floppy/MSDOS F/S */
114 #endif /* HAVE_FS_PCFS */
115 #ifdef HAVE_FS_CACHEFS
116   &cachefs_ops,                 /* caching F/S */
117 #endif /* HAVE_FS_CACHEFS */
118 #ifdef HAVE_FS_NULLFS
119 /* FILL IN */                   /* null (loopback) F/S */
120 #endif /* HAVE_FS_NULLFS */
121 #ifdef HAVE_FS_UNIONFS
122 /* FILL IN */                   /* union (bsd44) F/S */
123 #endif /* HAVE_FS_UNIONFS */
124 #ifdef HAVE_FS_UMAPFS
125 /* FILL IN */                   /* uid/gid mapping F/S */
126 #endif /* HAVE_FS_UMAPFS */
127
128   /*
129    * These 5 should be last, in the order:
130    *    (1) amfs_auto
131    *    (2) amfs_direct
132    *    (3) amfs_toplvl
133    *    (4) autofs
134    *    (5) amfs_error
135    */
136 #ifdef HAVE_AM_FS_AUTO
137   &amfs_auto_ops,               /* Automounter F/S */
138 #endif /* HAVE_AM_FS_AUTO */
139 #ifdef HAVE_AM_FS_DIRECT
140   &amfs_direct_ops,             /* direct-mount F/S */
141 #endif /* HAVE_AM_FS_DIRECT */
142 #ifdef HAVE_AM_FS_TOPLVL
143   &amfs_toplvl_ops,             /* top-level mount F/S */
144 #endif /* HAVE_AM_FS_TOPLVL */
145 #ifdef HAVE_FS_AUTOFS
146   &autofs_ops,                  /* autofs mount F/S */
147 #endif /* HAVE_FS_AUTOFS */
148 #ifdef HAVE_AM_FS_ERROR
149   &amfs_error_ops,              /* error F/S */
150 #endif /* HAVE_AM_FS_ERROR */
151   0
152 };
153
154
155 void
156 ops_showamfstypes(char *buf)
157 {
158   struct am_ops **ap;
159   int l = 0;
160
161   buf[0] = '\0';
162   for (ap = vops; *ap; ap++) {
163     strcat(buf, (*ap)->fs_type);
164     if (ap[1])
165       strcat(buf, ", ");
166     l += strlen((*ap)->fs_type) + 2;
167     if (l > 62) {
168       l = 0;
169       strcat(buf, "\n      ");
170     }
171   }
172 }
173
174
175 static void
176 ops_show1(char *buf, int *lp, const char *name)
177 {
178   strcat(buf, name);
179   strcat(buf, ", ");
180   *lp += strlen(name) + 2;
181   if (*lp > 60) {
182     strcat(buf, "\t\n");
183     *lp = 0;
184   }
185 }
186
187
188 void
189 ops_showfstypes(char *buf)
190 {
191   int l = 0;
192
193   buf[0] = '\0';
194
195 #ifdef MNTTAB_TYPE_AUTOFS
196   ops_show1(buf, &l, MNTTAB_TYPE_AUTOFS);
197 #endif /* MNTTAB_TYPE_AUTOFS */
198
199 #ifdef MNTTAB_TYPE_CACHEFS
200   ops_show1(buf, &l, MNTTAB_TYPE_CACHEFS);
201 #endif /* MNTTAB_TYPE_CACHEFS */
202
203 #ifdef MNTTAB_TYPE_CDFS
204   ops_show1(buf, &l, MNTTAB_TYPE_CDFS);
205 #endif /* MNTTAB_TYPE_CDFS */
206
207 #ifdef MNTTAB_TYPE_CFS
208   ops_show1(buf, &l, MNTTAB_TYPE_CFS);
209 #endif /* MNTTAB_TYPE_CFS */
210
211 #ifdef MNTTAB_TYPE_LOFS
212   ops_show1(buf, &l, MNTTAB_TYPE_LOFS);
213 #endif /* MNTTAB_TYPE_LOFS */
214
215 #ifdef MNTTAB_TYPE_EFS
216   ops_show1(buf, &l, MNTTAB_TYPE_EFS);
217 #endif /* MNTTAB_TYPE_EFS */
218
219 #ifdef MNTTAB_TYPE_MFS
220   ops_show1(buf, &l, MNTTAB_TYPE_MFS);
221 #endif /* MNTTAB_TYPE_MFS */
222
223 #ifdef MNTTAB_TYPE_NFS
224   ops_show1(buf, &l, MNTTAB_TYPE_NFS);
225 #endif /* MNTTAB_TYPE_NFS */
226
227 #ifdef MNTTAB_TYPE_NFS3
228   ops_show1(buf, &l, "nfs3");   /* always hard-code as nfs3 */
229 #endif /* MNTTAB_TYPE_NFS3 */
230
231 #ifdef MNTTAB_TYPE_NULLFS
232   ops_show1(buf, &l, MNTTAB_TYPE_NULLFS);
233 #endif /* MNTTAB_TYPE_NULLFS */
234
235 #ifdef MNTTAB_TYPE_PCFS
236   ops_show1(buf, &l, MNTTAB_TYPE_PCFS);
237 #endif /* MNTTAB_TYPE_PCFS */
238
239 #ifdef MNTTAB_TYPE_TFS
240   ops_show1(buf, &l, MNTTAB_TYPE_TFS);
241 #endif /* MNTTAB_TYPE_TFS */
242
243 #ifdef MNTTAB_TYPE_TMPFS
244   ops_show1(buf, &l, MNTTAB_TYPE_TMPFS);
245 #endif /* MNTTAB_TYPE_TMPFS */
246
247 #ifdef MNTTAB_TYPE_UFS
248   ops_show1(buf, &l, MNTTAB_TYPE_UFS);
249 #endif /* MNTTAB_TYPE_UFS */
250
251 #ifdef MNTTAB_TYPE_UMAPFS
252   ops_show1(buf, &l, MNTTAB_TYPE_UMAPFS);
253 #endif /* MNTTAB_TYPE_UMAPFS */
254
255 #ifdef MNTTAB_TYPE_UNIONFS
256   ops_show1(buf, &l, MNTTAB_TYPE_UNIONFS);
257 #endif /* MNTTAB_TYPE_UNIONFS */
258
259 #ifdef MNTTAB_TYPE_XFS
260   ops_show1(buf, &l, MNTTAB_TYPE_XFS);
261 #endif /* MNTTAB_TYPE_XFS */
262
263   /* terminate with a period, newline, and NULL */
264   if (buf[strlen(buf)-1] == '\n')
265     buf[strlen(buf) - 4] = '\0';
266   else
267     buf[strlen(buf) - 2] = '\0';
268   strcat(buf, ".\n");
269 }
270
271
272 /*
273  * return string option which is the reverse of opt.
274  * nosuid -> suid
275  * quota -> noquota
276  * ro -> rw
277  * etc.
278  * may return pointer to static buffer or subpointer within opt.
279  */
280 static char *
281 reverse_option(const char *opt)
282 {
283   static char buf[80];
284
285   /* sanity check */
286   if (!opt)
287     return NULL;
288
289   /* check special cases */
290   /* XXX: if this gets too long, rewrite the code more flexibly */
291   if (STREQ(opt, "ro")) return "rw";
292   if (STREQ(opt, "rw")) return "ro";
293   if (STREQ(opt, "bg")) return "fg";
294   if (STREQ(opt, "fg")) return "bg";
295   if (STREQ(opt, "soft")) return "hard";
296   if (STREQ(opt, "hard")) return "soft";
297
298   /* check if string starts with 'no' and chop it */
299   if (NSTREQ(opt, "no", 2)) {
300     strcpy(buf, &opt[2]);
301   } else {
302     /* finally return a string prepended with 'no' */
303     strcpy(buf, "no");
304     strcat(buf, opt);
305   }
306   return buf;
307 }
308
309
310 /*
311  * start with an empty string. for each opts1 option that is not
312  * in opts2, add it to the string (make sure the reverse of it
313  * isn't in either). finally add opts2. return new string.
314  * Both opts1 and opts2 must not be null!
315  * Caller must eventually free the string being returned.
316  */
317 static char *
318 merge_opts(const char *opts1, const char *opts2)
319 {
320   mntent_t mnt2;                /* place holder for opts2 */
321   char *newstr;                 /* new string to return (malloc'ed) */
322   char *tmpstr;                 /* temp */
323   char *eq;                     /* pointer to '=' within temp */
324   char oneopt[80];              /* one option w/o value if any */
325   char *revoneopt;              /* reverse of oneopt */
326   int len = strlen(opts1) + strlen(opts2) + 2; /* space for "," and NULL */
327   char *s1 = strdup(opts1);     /* copy of opts1 to munge */
328
329   /* initialization */
330   mnt2.mnt_opts = (char *) opts2;
331   newstr = xmalloc(len);
332   newstr[0] = '\0';
333
334   for (tmpstr = strtok(s1, ",");
335        tmpstr;
336        tmpstr = strtok(NULL, ",")) {
337     /* copy option to temp buffer */
338     strncpy(oneopt, tmpstr, 80);
339     oneopt[79] = '\0';
340     /* if option has a value such as rsize=1024, chop the value part */
341     if ((eq = strchr(oneopt, '=')))
342       eq[1] = '\0';
343     /* find reverse option of oneopt */
344     revoneopt = reverse_option(oneopt);
345     /* if option orits reverse exist in opts2, ignore it */
346     if (hasmntopt(&mnt2, oneopt) || hasmntopt(&mnt2, revoneopt))
347       continue;
348     /* add option to returned string */
349     if (newstr && newstr[0]) {
350       strcat(newstr, ",");
351       strcat(newstr, tmpstr);
352     } else {
353       strcpy(newstr, tmpstr);
354     }
355   }
356
357   /* finally, append opts2 itself */
358   if (newstr && newstr[0]) {
359     strcat(newstr, ",");
360     strcat(newstr, opts2);
361   } else {
362     strcpy(newstr, opts2);
363   }
364
365   XFREE(s1);
366   return newstr;
367 }
368
369
370 am_ops *
371 ops_match(am_opts *fo, char *key, char *g_key, char *path, char *keym, char *map)
372 {
373   am_ops **vp;
374   am_ops *rop = 0;
375
376   /*
377    * First crack the global opts and the local opts
378    */
379   if (!eval_fs_opts(fo, key, g_key, path, keym, map)) {
380     rop = &amfs_error_ops;
381   } else if (fo->opt_type == 0) {
382     plog(XLOG_USER, "No fs type specified (key = \"%s\", map = \"%s\")", keym, map);
383     rop = &amfs_error_ops;
384   } else {
385     /*
386      * Next find the correct filesystem type
387      */
388     for (vp = vops; (rop = *vp); vp++)
389       if (STREQ(rop->fs_type, fo->opt_type))
390         break;
391     if (!rop) {
392       plog(XLOG_USER, "fs type \"%s\" not recognized", fo->opt_type);
393       rop = &amfs_error_ops;
394     }
395   }
396
397   /*
398    * Make sure we have a default mount option.
399    * Otherwise skip past any leading '-'.
400    */
401   if (fo->opt_opts == 0)
402     fo->opt_opts = strdup("rw,defaults");
403   else if (*fo->opt_opts == '-') {
404     /*
405      * We cannot simply do fo->opt_opts++ here since the opts
406      * module will try to free the pointer fo->opt_opts later.
407      * So just reallocate the thing -- stolcke 11/11/94
408      */
409     char *old = fo->opt_opts;
410     fo->opt_opts = strdup(old + 1);
411     XFREE(old);
412   }
413
414   /*
415    * If addopts option was used, then append it to the
416    * current options and remote mount options.
417    */
418   if (fo->opt_addopts) {
419     if (STREQ(fo->opt_opts, fo->opt_remopts)) {
420       /* optimize things for the common case where opts==remopts */
421       char *mergedstr;
422       mergedstr = merge_opts(fo->opt_opts, fo->opt_addopts);
423       plog(XLOG_USER, "merge rem/opts \"%s\" add \"%s\" => \"%s\"",
424            fo->opt_opts, fo->opt_addopts, mergedstr);
425       XFREE(fo->opt_opts);
426       XFREE(fo->opt_remopts);
427       fo->opt_opts = mergedstr;
428       fo->opt_remopts = strdup(mergedstr);
429     } else {
430       char *mergedstr, *remmergedstr;
431       mergedstr = merge_opts(fo->opt_opts, fo->opt_addopts);
432       plog(XLOG_USER, "merge opts \"%s\" add \"%s\" => \"%s\"",
433            fo->opt_opts, fo->opt_addopts, mergedstr);
434       XFREE(fo->opt_opts);
435       fo->opt_opts = mergedstr;
436       remmergedstr = merge_opts(fo->opt_remopts, fo->opt_addopts);
437       plog(XLOG_USER, "merge remopts \"%s\" add \"%s\" => \"%s\"",
438            fo->opt_remopts, fo->opt_addopts, remmergedstr);
439       XFREE(fo->opt_remopts);
440       fo->opt_remopts = remmergedstr;
441     }
442   }
443
444   /*
445    * Check the filesystem is happy
446    */
447   if (fo->fs_mtab)
448     XFREE(fo->fs_mtab);
449
450   if ((fo->fs_mtab = (*rop->fs_match) (fo)))
451     return rop;
452
453   /*
454    * Return error file system
455    */
456   fo->fs_mtab = (*amfs_error_ops.fs_match) (fo);
457   return &amfs_error_ops;
458 }