2 * Copyright (c) 1997-1999 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
41 * $Id: mntfs.c,v 1.2 1999/01/10 21:53:47 ezk Exp $
47 #endif /* HAVE_CONFIG_H */
51 qelem mfhead
= {&mfhead
, &mfhead
};
59 if (mf
->mf_refc
== 0) {
61 untimeout(mf
->mf_cid
);
71 init_mntfs(mntfs
*mf
, am_ops
*ops
, am_opts
*mo
, char *mp
, char *info
, char *auto_opts
, char *mopts
, char *remopts
)
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
);
88 mf
->mf_server
= (*ops
->ffserver
) (mf
);
95 alloc_mntfs(am_ops
*ops
, am_opts
*mo
, char *mp
, char *info
, char *auto_opts
, char *mopts
, char *remopts
)
97 mntfs
*mf
= ALLOC(struct mntfs
);
99 init_mntfs(mf
, ops
, mo
, mp
, info
, auto_opts
, mopts
, remopts
);
100 ins_que(&mf
->mf_q
, &mfhead
);
108 find_mntfs(am_ops
*ops
, am_opts
*mo
, char *mp
, char *info
, char *auto_opts
, char *mopts
, char *remopts
)
113 dlog("Locating mntfs reference to %s", mp
);
116 ITER(mf
, mntfs
, &mfhead
) {
117 if (STREQ(mf
->mf_mount
, mp
)) {
119 * Handle cases where error ops are involved
121 if (ops
== &amfs_error_ops
) {
123 * If the existing ops are not amfs_error_ops
126 if (mf
->mf_ops
!= &amfs_error_ops
)
129 return dup_mntfs(mf
);
130 } else { /* ops != &amfs_error_ops */
132 * If the existing ops are amfs_error_ops
135 if (mf
->mf_ops
== &amfs_error_ops
)
139 if ((mf
->mf_flags
& MFF_RESTART
) && amd_state
== Run
) {
141 * Restart a previously mounted filesystem.
143 mntfs
*mf2
= alloc_mntfs(&amfs_inherit_ops
, mo
, mp
, info
, auto_opts
, mopts
, remopts
);
145 dlog("Restarting filesystem %s", mf
->mf_mount
);
149 * Remember who we are restarting
151 mf2
->mf_private
= (voidp
) dup_mntfs(mf
);
152 mf2
->mf_prfree
= free_mntfs
;
157 if (!(mf
->mf_flags
& (MFF_MOUNTED
| MFF_MOUNTING
| MFF_UNMOUNTING
))) {
159 mf
->mf_flags
&= ~MFF_ERROR
;
161 mf
->mf_auto
= strealloc(mf
->mf_auto
, auto_opts
);
162 mf
->mf_mopts
= strealloc(mf
->mf_mopts
, mopts
);
163 mf
->mf_remopts
= strealloc(mf
->mf_remopts
, remopts
);
164 mf
->mf_info
= strealloc(mf
->mf_info
, info
);
166 if (mf
->mf_private
&& mf
->mf_prfree
) {
167 (*mf
->mf_prfree
) (mf
->mf_private
);
171 fs
= ops
->ffserver
? (*ops
->ffserver
) (mf
) : (fserver
*) NULL
;
173 free_srvr(mf
->mf_server
);
176 return dup_mntfs(mf
);
180 return alloc_mntfs(ops
, mo
, mp
, info
, auto_opts
, mopts
, remopts
);
187 return alloc_mntfs(&amfs_error_ops
, (am_opts
*) 0, "//nil//", ".", "", "", "");
192 uninit_mntfs(mntfs
*mf
, int rmd
)
199 XFREE(mf
->mf_remopts
);
202 if (mf
->mf_private
&& mf
->mf_prfree
)
203 (*mf
->mf_prfree
) (mf
->mf_private
);
206 * Clean up any directories that were made
208 if (rmd
&& (mf
->mf_flags
& MFF_MKMNT
))
209 rmdirs(mf
->mf_mount
);
210 /* free mf_mount _AFTER_ removing the directories */
215 * Clean up the file server
218 free_srvr(mf
->mf_server
);
221 * Don't do a callback on this mount
224 untimeout(mf
->mf_cid
);
231 discard_mntfs(voidp v
)
240 uninit_mntfs(mf
, TRUE
);
252 mf
= AM_FIRST(mntfs
, &mfhead
);
253 while (mf
!= HEAD(mntfs
, &mfhead
)) {
255 mf
= NEXT(mntfs
, mf
);
256 if (mf2
->mf_refc
== 0 && mf2
->mf_cid
)
267 if (--mf
->mf_refc
== 0) {
268 if (mf
->mf_flags
& MFF_MOUNTED
) {
270 mf
->mf_flags
&= ~MFF_MOUNTED
;
273 * Record for posterity
275 quoted
= strchr(mf
->mf_info
, ' ') != 0; /* cheap */
276 plog(XLOG_INFO
, "%s%s%s %sed fstype %s from %s",
280 mf
->mf_error
? "discard" : "unmount",
281 mf
->mf_ops
->fs_type
, mf
->mf_mount
);
284 if (mf
->mf_ops
->fs_flags
& FS_DISCARD
) {
286 dlog("Immediately discarding mntfs for %s", mf
->mf_mount
);
293 if (mf
->mf_flags
& MFF_RESTART
) {
294 dlog("Discarding remount hook for %s", mf
->mf_mount
);
296 dlog("Discarding last mntfs reference to %s fstype %s",
297 mf
->mf_mount
, mf
->mf_ops
->fs_type
);
299 if (mf
->mf_flags
& (MFF_MOUNTED
| MFF_MOUNTING
| MFF_UNMOUNTING
))
300 dlog("mntfs reference for %s still active", mf
->mf_mount
);
302 mf
->mf_cid
= timeout(ALLOWED_MOUNT_TIME
, discard_mntfs
, (voidp
) mf
);
309 realloc_mntfs(mntfs
*mf
, am_ops
*ops
, am_opts
*mo
, char *mp
, char *info
, char *auto_opts
, char *mopts
, char *remopts
)
313 if (mf
->mf_refc
== 1 && mf
->mf_ops
== &amfs_inherit_ops
&& STREQ(mf
->mf_mount
, mp
)) {
315 * If we are inheriting then just return
322 * Re-use the existing mntfs if it is mounted.
323 * This traps a race in nfsx.
325 if (mf
->mf_ops
!= &amfs_error_ops
&&
326 (mf
->mf_flags
& MFF_MOUNTED
) &&
327 !FSRV_ISDOWN(mf
->mf_server
)) {
332 mf2
= find_mntfs(ops
, mo
, mp
, info
, auto_opts
, mopts
, remopts
);