2 * Copyright (c) 1997-2006 Erez Zadok
3 * Copyright (c) 1990 Jan-Simon Pendry
4 * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
5 * Copyright (c) 1990 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/amd/mntfs.c
46 #endif /* HAVE_CONFIG_H */
50 qelem mfhead = {&mfhead, &mfhead};
58 if (mf->mf_refc == 0) {
60 untimeout(mf->mf_cid);
70 init_mntfs(mntfs *mf, am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
73 mf->mf_fsflags = ops->nfs_fs_flags;
75 mf->mf_mount = strdup(mp);
76 mf->mf_info = strdup(info);
77 mf->mf_auto = strdup(auto_opts);
78 mf->mf_mopts = strdup(mopts);
79 mf->mf_remopts = strdup(remopts);
80 mf->mf_loopdev = NULL;
89 mf->mf_server = (*ops->ffserver) (mf);
96 alloc_mntfs(am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
98 mntfs *mf = ALLOC(struct mntfs);
100 init_mntfs(mf, ops, mo, mp, info, auto_opts, mopts, remopts);
101 ins_que(&mf->mf_q, &mfhead);
108 /* find a matching mntfs in our list */
110 locate_mntfs(am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
114 dlog("Locating mntfs reference to (%s,%s)", mp, info);
116 ITER(mf, mntfs, &mfhead) {
118 * For backwards compatibility purposes, we treat already-mounted
119 * filesystems differently and only require a match of their mount point,
120 * not of their server info. After all, there is little we can do if
121 * the user asks us to mount two different things onto the same mount: one
122 * will always cover the other one.
124 if (STREQ(mf->mf_mount, mp) &&
125 ((mf->mf_flags & MFF_MOUNTED && !(mf->mf_fsflags & FS_DIRECT))
126 || (STREQ(mf->mf_info, info) && mf->mf_ops == ops))) {
128 * Handle cases where error ops are involved
130 if (ops == &amfs_error_ops) {
132 * If the existing ops are not amfs_error_ops
135 if (mf->mf_ops != &amfs_error_ops)
137 return dup_mntfs(mf);
140 dlog("mf->mf_flags = %#x", mf->mf_flags);
142 if ((mf->mf_flags & MFF_RESTART) && amd_state < Finishing) {
144 * Restart a previously mounted filesystem.
146 dlog("Restarting filesystem %s", mf->mf_mount);
149 * If we are restarting an amd internal filesystem,
150 * we need to initialize it a bit.
152 * We know it's internal because it is marked as toplvl.
154 if (mf->mf_ops == &amfs_toplvl_ops) {
156 mf->mf_info = strealloc(mf->mf_info, info);
157 ops->mounted(mf); /* XXX: not right, but will do for now */
163 if (!(mf->mf_flags & (MFF_MOUNTED | MFF_MOUNTING | MFF_UNMOUNTING))) {
165 mf->mf_flags &= ~MFF_ERROR;
167 mf->mf_auto = strealloc(mf->mf_auto, auto_opts);
168 mf->mf_mopts = strealloc(mf->mf_mopts, mopts);
169 mf->mf_remopts = strealloc(mf->mf_remopts, remopts);
170 mf->mf_info = strealloc(mf->mf_info, info);
172 if (mf->mf_private && mf->mf_prfree) {
173 mf->mf_prfree(mf->mf_private);
177 fs = ops->ffserver ? (*ops->ffserver) (mf) : (fserver *) NULL;
179 free_srvr(mf->mf_server);
182 return dup_mntfs(mf);
183 } /* end of "if (STREQ(mf-> ..." */
190 /* find a matching mntfs in our list, create a new one if none is found */
192 find_mntfs(am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
194 mntfs *mf = locate_mntfs(ops, mo, mp, info, auto_opts, mopts, remopts);
198 return alloc_mntfs(ops, mo, mp, info, auto_opts, mopts, remopts);
205 return alloc_mntfs(&amfs_error_ops, (am_opts *) 0, "//nil//", ".", "", "", "");
210 uninit_mntfs(mntfs *mf)
217 XFREE(mf->mf_remopts);
220 if (mf->mf_private && mf->mf_prfree)
221 (*mf->mf_prfree) (mf->mf_private);
227 * Clean up the file server
230 free_srvr(mf->mf_server);
233 * Don't do a callback on this mount
236 untimeout(mf->mf_cid);
243 discard_mntfs(voidp v)
264 mf = AM_FIRST(mntfs, &mfhead);
265 while (mf != HEAD(mntfs, &mfhead)) {
267 mf = NEXT(mntfs, mf);
268 if (mf2->mf_refc == 0 && mf2->mf_cid)
275 free_mntfs(opaque_t arg)
277 mntfs *mf = (mntfs *) arg;
279 dlog("free_mntfs <%s> type %s mf_refc %d flags %x",
280 mf->mf_mount, mf->mf_ops->fs_type, mf->mf_refc, mf->mf_flags);
283 * We shouldn't ever be called to free something that has
284 * a non-positive refcount. Something is badly wrong if
285 * we have been! Ignore the request for now...
287 if (mf->mf_refc <= 0) {
288 plog(XLOG_ERROR, "IGNORING free_mntfs for <%s>: refc %d, flags %x (bug?)",
289 mf->mf_mount, mf->mf_refc, mf->mf_flags);
293 /* don't discard last reference of a restarted/kept mntfs */
294 if (mf->mf_refc == 1 && mf->mf_flags & MFF_RSTKEEP) {
295 plog(XLOG_ERROR, "IGNORING free_mntfs for <%s>: refc %d, flags %x (restarted)",
296 mf->mf_mount, mf->mf_refc, mf->mf_flags);
300 if (--mf->mf_refc == 0) {
301 if (mf->mf_flags & MFF_MOUNTED) {
303 mf->mf_flags &= ~MFF_MOUNTED;
306 * Record for posterity
308 quoted = strchr(mf->mf_info, ' ') != 0; /* cheap */
309 plog(XLOG_INFO, "%s%s%s %sed fstype %s from %s",
313 mf->mf_error ? "discard" : "unmount",
314 mf->mf_ops->fs_type, mf->mf_mount);
317 if (mf->mf_fsflags & FS_DISCARD) {
318 dlog("Immediately discarding mntfs for %s", mf->mf_mount);
323 if (mf->mf_flags & MFF_RESTART) {
324 dlog("Discarding remount hook for %s", mf->mf_mount);
326 dlog("Discarding last mntfs reference to %s fstype %s",
327 mf->mf_mount, mf->mf_ops->fs_type);
329 if (mf->mf_flags & (MFF_MOUNTED | MFF_MOUNTING | MFF_UNMOUNTING))
330 dlog("mntfs reference for %s still active", mf->mf_mount);
331 mf->mf_cid = timeout(ALLOWED_MOUNT_TIME, discard_mntfs, (voidp) mf);
338 realloc_mntfs(mntfs *mf, am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
342 if (mf->mf_refc == 1 &&
343 mf->mf_flags & MFF_RESTART &&
344 STREQ(mf->mf_mount, mp)) {
346 * If we are inheriting then just return
353 * Re-use the existing mntfs if it is mounted.
354 * This traps a race in nfsx.
356 if (mf->mf_ops != &amfs_error_ops &&
357 (mf->mf_flags & MFF_MOUNTED) &&
358 !FSRV_ISDOWN(mf->mf_server)) {
363 mf2 = find_mntfs(ops, mo, mp, info, auto_opts, mopts, remopts);