2 * Copyright (c) 1997-2014 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. 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.
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
36 * File: am-utils/amd/mntfs.c
42 #endif /* HAVE_CONFIG_H */
46 qelem mfhead = {&mfhead, &mfhead};
56 dup_mntfs(loc->al_mnt);
64 if (mf->mf_refc == 0) {
66 untimeout(mf->mf_cid);
76 init_mntfs(mntfs *mf, am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
79 mf->mf_fsflags = ops->nfs_fs_flags;
82 mf->mf_fo = copy_opts(mo);
84 mf->mf_mount = xstrdup(mp);
85 mf->mf_info = xstrdup(info);
86 mf->mf_auto = xstrdup(auto_opts);
87 mf->mf_mopts = xstrdup(mopts);
88 mf->mf_remopts = xstrdup(remopts);
89 mf->mf_loopdev = NULL;
94 mf->mf_private = NULL;
98 mf->mf_server = (*ops->ffserver) (mf);
100 mf->mf_server = NULL;
105 alloc_mntfs(am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
107 mntfs *mf = ALLOC(struct mntfs);
109 init_mntfs(mf, ops, mo, mp, info, auto_opts, mopts, remopts);
110 ins_que(&mf->mf_q, &mfhead);
117 /* find a matching mntfs in our list */
119 locate_mntfs(am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
123 dlog("Locating mntfs reference to (%s,%s)", mp, info);
125 ITER(mf, mntfs, &mfhead) {
127 * For backwards compatibility purposes, we treat already-mounted
128 * filesystems differently and only require a match of their mount point,
129 * not of their server info. After all, there is little we can do if
130 * the user asks us to mount two different things onto the same mount: one
131 * will always cover the other one.
133 if (STREQ(mf->mf_mount, mp) &&
134 ((mf->mf_flags & MFF_MOUNTED && !(mf->mf_fsflags & FS_DIRECT))
135 || (STREQ(mf->mf_info, info) && mf->mf_ops == ops))) {
137 * Handle cases where error ops are involved
139 if (ops == &amfs_error_ops) {
141 * If the existing ops are not amfs_error_ops
144 if (mf->mf_ops != &amfs_error_ops)
146 return dup_mntfs(mf);
149 dlog("mf->mf_flags = %#x", mf->mf_flags);
151 if ((mf->mf_flags & MFF_RESTART) && amd_state < Finishing) {
153 * Restart a previously mounted filesystem.
155 dlog("Restarting filesystem %s", mf->mf_mount);
158 * If we are restarting an amd internal filesystem,
159 * we need to initialize it a bit.
161 * We know it's internal because it is marked as toplvl.
163 if (mf->mf_ops == &amfs_toplvl_ops) {
165 mf->mf_info = strealloc(mf->mf_info, info);
166 ops->mounted(mf); /* XXX: not right, but will do for now */
172 if (!(mf->mf_flags & (MFF_MOUNTED | MFF_MOUNTING | MFF_UNMOUNTING))) {
174 mf->mf_flags &= ~MFF_ERROR;
176 mf->mf_auto = strealloc(mf->mf_auto, auto_opts);
177 mf->mf_mopts = strealloc(mf->mf_mopts, mopts);
178 mf->mf_remopts = strealloc(mf->mf_remopts, remopts);
179 mf->mf_info = strealloc(mf->mf_info, info);
181 if (mf->mf_private && mf->mf_prfree) {
182 mf->mf_prfree(mf->mf_private);
183 mf->mf_private = NULL;
186 fs = ops->ffserver ? (*ops->ffserver) (mf) : (fserver *) NULL;
188 free_srvr(mf->mf_server);
191 return dup_mntfs(mf);
192 } /* end of "if (STREQ(mf-> ..." */
199 /* find a matching mntfs in our list, create a new one if none is found */
201 find_mntfs(am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
203 mntfs *mf = locate_mntfs(ops, mo, mp, info, auto_opts, mopts, remopts);
207 return alloc_mntfs(ops, mo, mp, info, auto_opts, mopts, remopts);
214 return alloc_mntfs(&amfs_error_ops, (am_opts *) NULL, "//nil//", ".", "", "", "");
220 am_loc *loc = CALLOC(struct am_loc);
222 loc->al_mnt = new_mntfs();
229 uninit_mntfs(mntfs *mf)
232 free_opts(mf->mf_fo);
237 XFREE(mf->mf_remopts);
239 if (mf->mf_private && mf->mf_prfree)
240 (*mf->mf_prfree) (mf->mf_private);
245 * Clean up the file server
248 free_srvr(mf->mf_server);
251 * Don't do a callback on this mount
254 untimeout(mf->mf_cid);
261 discard_mntfs(voidp v)
281 free_opts(loc->al_fo);
292 mf = AM_FIRST(mntfs, &mfhead);
293 while (mf != HEAD(mntfs, &mfhead)) {
295 mf = NEXT(mntfs, mf);
296 if (mf2->mf_refc == 0 && mf2->mf_cid)
302 free_loc(opaque_t arg)
304 am_loc *loc = (am_loc *) arg;
305 dlog("free_loc %p", loc);
307 if (loc->al_refc <= 0) {
308 plog(XLOG_ERROR, "IGNORING free_loc for 0x%p", loc);
313 free_mntfs(loc->al_mnt);
314 if (--loc->al_refc == 0) {
320 free_mntfs(opaque_t arg)
322 mntfs *mf = (mntfs *) arg;
324 dlog("free_mntfs <%s> type %s mf_refc %d flags %x",
325 mf->mf_mount, mf->mf_ops->fs_type, mf->mf_refc, mf->mf_flags);
328 * We shouldn't ever be called to free something that has
329 * a non-positive refcount. Something is badly wrong if
330 * we have been! Ignore the request for now...
332 if (mf->mf_refc <= 0) {
333 plog(XLOG_ERROR, "IGNORING free_mntfs for <%s>: refc %d, flags %x (bug?)",
334 mf->mf_mount, mf->mf_refc, mf->mf_flags);
338 /* don't discard last reference of a restarted/kept mntfs */
339 if (mf->mf_refc == 1 && mf->mf_flags & MFF_RSTKEEP) {
340 plog(XLOG_ERROR, "IGNORING free_mntfs for <%s>: refc %d, flags %x (restarted)",
341 mf->mf_mount, mf->mf_refc, mf->mf_flags);
345 if (--mf->mf_refc == 0) {
346 if (mf->mf_flags & MFF_MOUNTED) {
348 mf->mf_flags &= ~MFF_MOUNTED;
351 * Record for posterity
353 quoted = strchr(mf->mf_info, ' ') != 0; /* cheap */
354 plog(XLOG_INFO, "%s%s%s %sed fstype %s from %s",
358 mf->mf_error ? "discard" : "unmount",
359 mf->mf_ops->fs_type, mf->mf_mount);
362 if (mf->mf_fsflags & FS_DISCARD) {
363 dlog("Immediately discarding mntfs for %s", mf->mf_mount);
368 if (mf->mf_flags & MFF_RESTART) {
369 dlog("Discarding remount hook for %s", mf->mf_mount);
371 dlog("Discarding last mntfs reference to %s fstype %s",
372 mf->mf_mount, mf->mf_ops->fs_type);
374 if (mf->mf_flags & (MFF_MOUNTED | MFF_MOUNTING | MFF_UNMOUNTING))
375 dlog("mntfs reference for %s still active", mf->mf_mount);
376 mf->mf_cid = timeout(ALLOWED_MOUNT_TIME, discard_mntfs, (voidp) mf);
383 realloc_mntfs(mntfs *mf, am_ops *ops, am_opts *mo, char *mp, char *info, char *auto_opts, char *mopts, char *remopts)
387 if (mf->mf_refc == 1 &&
388 mf->mf_flags & MFF_RESTART &&
389 STREQ(mf->mf_mount, mp)) {
391 * If we are inheriting then just return
398 * Re-use the existing mntfs if it is mounted.
399 * This traps a race in nfsx.
401 if (mf->mf_ops != &amfs_error_ops &&
402 (mf->mf_flags & MFF_MOUNTED) &&
403 !FSRV_ISDOWN(mf->mf_server)) {
407 mf2 = find_mntfs(ops, mo, mp, info, auto_opts, mopts, remopts);