1*9f182afdSjoerg /* $NetBSD: map.c,v 1.2 2015/01/21 21:48:23 joerg Exp $ */
2a53f50b9Schristos
3a53f50b9Schristos /*
48bae5d40Schristos * Copyright (c) 1997-2014 Erez Zadok
5a53f50b9Schristos * Copyright (c) 1990 Jan-Simon Pendry
6a53f50b9Schristos * Copyright (c) 1990 Imperial College of Science, Technology & Medicine
7a53f50b9Schristos * Copyright (c) 1990 The Regents of the University of California.
8a53f50b9Schristos * All rights reserved.
9a53f50b9Schristos *
10a53f50b9Schristos * This code is derived from software contributed to Berkeley by
11a53f50b9Schristos * Jan-Simon Pendry at Imperial College, London.
12a53f50b9Schristos *
13a53f50b9Schristos * Redistribution and use in source and binary forms, with or without
14a53f50b9Schristos * modification, are permitted provided that the following conditions
15a53f50b9Schristos * are met:
16a53f50b9Schristos * 1. Redistributions of source code must retain the above copyright
17a53f50b9Schristos * notice, this list of conditions and the following disclaimer.
18a53f50b9Schristos * 2. Redistributions in binary form must reproduce the above copyright
19a53f50b9Schristos * notice, this list of conditions and the following disclaimer in the
20a53f50b9Schristos * documentation and/or other materials provided with the distribution.
218bae5d40Schristos * 3. Neither the name of the University nor the names of its contributors
22a53f50b9Schristos * may be used to endorse or promote products derived from this software
23a53f50b9Schristos * without specific prior written permission.
24a53f50b9Schristos *
25a53f50b9Schristos * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26a53f50b9Schristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27a53f50b9Schristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28a53f50b9Schristos * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29a53f50b9Schristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30a53f50b9Schristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31a53f50b9Schristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32a53f50b9Schristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33a53f50b9Schristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34a53f50b9Schristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35a53f50b9Schristos * SUCH DAMAGE.
36a53f50b9Schristos *
37a53f50b9Schristos *
38a53f50b9Schristos * File: am-utils/amd/map.c
39a53f50b9Schristos *
40a53f50b9Schristos */
41a53f50b9Schristos
42a53f50b9Schristos #ifdef HAVE_CONFIG_H
43a53f50b9Schristos # include <config.h>
44a53f50b9Schristos #endif /* HAVE_CONFIG_H */
45a53f50b9Schristos #include <am_defs.h>
46a53f50b9Schristos #include <amd.h>
47a53f50b9Schristos
48a53f50b9Schristos #define smallest_t(t1, t2) (t1 != NEVER ? (t2 != NEVER ? (t1 < t2 ? t1 : t2) : t1) : t2)
49a53f50b9Schristos #define IGNORE_FLAGS (MFF_MOUNTING|MFF_UNMOUNTING|MFF_RESTART)
50a53f50b9Schristos #define new_gen() (am_gen++)
51a53f50b9Schristos
52a53f50b9Schristos /*
53a53f50b9Schristos * Generation Numbers.
54a53f50b9Schristos *
55a53f50b9Schristos * Generation numbers are allocated to every node created
56a53f50b9Schristos * by amd. When a filehandle is computed and sent to the
57a53f50b9Schristos * kernel, the generation number makes sure that it is safe
58a53f50b9Schristos * to reallocate a node slot even when the kernel has a cached
59a53f50b9Schristos * reference to its old incarnation.
60a53f50b9Schristos * No garbage collection is done, since it is assumed that
61a53f50b9Schristos * there is no way that 2^32 generation numbers could ever
62a53f50b9Schristos * be allocated by a single run of amd - there is simply
63a53f50b9Schristos * not enough cpu time available.
64a53f50b9Schristos * Famous last words... -Ion
65a53f50b9Schristos */
66a53f50b9Schristos static u_int am_gen = 2; /* Initial generation number */
67a53f50b9Schristos static int timeout_mp_id; /* Id from last call to timeout */
68a53f50b9Schristos
69a53f50b9Schristos static am_node *root_node; /* The root of the mount tree */
70a53f50b9Schristos static am_node **exported_ap = (am_node **) NULL;
71a53f50b9Schristos static int exported_ap_size = 0;
72a53f50b9Schristos static int first_free_map = 0; /* First available free slot */
73a53f50b9Schristos static int last_used_map = -1; /* Last unavailable used slot */
74a53f50b9Schristos
75a53f50b9Schristos
76a53f50b9Schristos /*
77a53f50b9Schristos * This is the default attributes field which
78a53f50b9Schristos * is copied into every new node to be created.
79a53f50b9Schristos * The individual filesystem fs_init() routines
80a53f50b9Schristos * patch the copy to represent the particular
81a53f50b9Schristos * details for the relevant filesystem type
82a53f50b9Schristos */
83a53f50b9Schristos static nfsfattr gen_fattr =
84a53f50b9Schristos {
85a53f50b9Schristos NFLNK, /* type */
86a53f50b9Schristos NFSMODE_LNK | 0777, /* mode */
87a53f50b9Schristos 1, /* nlink */
88a53f50b9Schristos 0, /* uid */
89a53f50b9Schristos 0, /* gid */
90a53f50b9Schristos 0, /* size */
91a53f50b9Schristos 4096, /* blocksize */
92a53f50b9Schristos 0, /* rdev */
93a53f50b9Schristos 1, /* blocks */
94a53f50b9Schristos 0, /* fsid */
95a53f50b9Schristos 0, /* fileid */
96a53f50b9Schristos {0, 0}, /* atime */
97a53f50b9Schristos {0, 0}, /* mtime */
98a53f50b9Schristos {0, 0}, /* ctime */
99a53f50b9Schristos };
100a53f50b9Schristos
101a53f50b9Schristos /* forward declarations */
102a53f50b9Schristos static int unmount_node(opaque_t arg);
103a53f50b9Schristos static void exported_ap_free(am_node *mp);
104a53f50b9Schristos static void remove_am(am_node *mp);
105a53f50b9Schristos static am_node *get_root_ap(char *dir);
106a53f50b9Schristos
107a53f50b9Schristos
108a53f50b9Schristos /*
109a53f50b9Schristos * Iterator functions for exported_ap[]
110a53f50b9Schristos */
111a53f50b9Schristos am_node *
get_first_exported_ap(int * index)112a53f50b9Schristos get_first_exported_ap(int *index)
113a53f50b9Schristos {
114a53f50b9Schristos *index = -1;
115a53f50b9Schristos return get_next_exported_ap(index);
116a53f50b9Schristos }
117a53f50b9Schristos
118a53f50b9Schristos
119a53f50b9Schristos am_node *
get_next_exported_ap(int * index)120a53f50b9Schristos get_next_exported_ap(int *index)
121a53f50b9Schristos {
122a53f50b9Schristos (*index)++;
123a53f50b9Schristos while (*index < exported_ap_size) {
124a53f50b9Schristos if (exported_ap[*index] != NULL)
125a53f50b9Schristos return exported_ap[*index];
126a53f50b9Schristos (*index)++;
127a53f50b9Schristos }
128a53f50b9Schristos return NULL;
129a53f50b9Schristos }
130a53f50b9Schristos
131a53f50b9Schristos
132a53f50b9Schristos /*
133a53f50b9Schristos * Get exported_ap by index
134a53f50b9Schristos */
135a53f50b9Schristos am_node *
get_exported_ap(int index)136a53f50b9Schristos get_exported_ap(int index)
137a53f50b9Schristos {
138a53f50b9Schristos if (index < 0 || index >= exported_ap_size)
139a53f50b9Schristos return 0;
140a53f50b9Schristos return exported_ap[index];
141a53f50b9Schristos }
142a53f50b9Schristos
143a53f50b9Schristos
144a53f50b9Schristos /*
145a53f50b9Schristos * Get exported_ap by path
146a53f50b9Schristos */
147a53f50b9Schristos am_node *
path_to_exported_ap(char * path)148a53f50b9Schristos path_to_exported_ap(char *path)
149a53f50b9Schristos {
150a53f50b9Schristos int index;
151a53f50b9Schristos am_node *mp;
152a53f50b9Schristos
153a53f50b9Schristos mp = get_first_exported_ap(&index);
154a53f50b9Schristos while (mp != NULL) {
155a53f50b9Schristos if (STREQ(mp->am_path, path))
156a53f50b9Schristos break;
157a53f50b9Schristos mp = get_next_exported_ap(&index);
158a53f50b9Schristos }
159a53f50b9Schristos return mp;
160a53f50b9Schristos }
161a53f50b9Schristos
162a53f50b9Schristos
163a53f50b9Schristos /*
164a53f50b9Schristos * Resize exported_ap map
165a53f50b9Schristos */
166a53f50b9Schristos static int
exported_ap_realloc_map(int nsize)167a53f50b9Schristos exported_ap_realloc_map(int nsize)
168a53f50b9Schristos {
169a53f50b9Schristos /*
170a53f50b9Schristos * this shouldn't happen, but...
171a53f50b9Schristos */
172a53f50b9Schristos if (nsize < 0 || nsize == exported_ap_size)
173a53f50b9Schristos return 0;
174a53f50b9Schristos
175a53f50b9Schristos exported_ap = (am_node **) xrealloc((voidp) exported_ap, nsize * sizeof(am_node *));
176a53f50b9Schristos
177a53f50b9Schristos if (nsize > exported_ap_size)
178a53f50b9Schristos memset((char *) (exported_ap + exported_ap_size), 0,
179a53f50b9Schristos (nsize - exported_ap_size) * sizeof(am_node *));
180a53f50b9Schristos exported_ap_size = nsize;
181a53f50b9Schristos
182a53f50b9Schristos return 1;
183a53f50b9Schristos }
184a53f50b9Schristos
185a53f50b9Schristos
186a53f50b9Schristos
187a53f50b9Schristos am_node *
get_ap_child(am_node * mp,char * fname)188a53f50b9Schristos get_ap_child(am_node *mp, char *fname)
189a53f50b9Schristos {
190a53f50b9Schristos am_node *new_mp;
1918bae5d40Schristos mntfs *mf = mp->am_al->al_mnt;
192a53f50b9Schristos
193a53f50b9Schristos /*
194a53f50b9Schristos * Allocate a new map
195a53f50b9Schristos */
196a53f50b9Schristos new_mp = exported_ap_alloc();
197a53f50b9Schristos if (new_mp) {
198a53f50b9Schristos /*
199a53f50b9Schristos * Fill it in
200a53f50b9Schristos */
201a53f50b9Schristos init_map(new_mp, fname);
202a53f50b9Schristos
203a53f50b9Schristos /*
204a53f50b9Schristos * Put it in the table
205a53f50b9Schristos */
206a53f50b9Schristos insert_am(new_mp, mp);
207a53f50b9Schristos
208a53f50b9Schristos /*
209a53f50b9Schristos * Fill in some other fields,
210a53f50b9Schristos * path and mount point.
211a53f50b9Schristos *
212a53f50b9Schristos * bugfix: do not prepend old am_path if direct map
213a53f50b9Schristos * <wls@astro.umd.edu> William Sebok
214a53f50b9Schristos */
215a53f50b9Schristos new_mp->am_path = str3cat(new_mp->am_path,
216a53f50b9Schristos (mf->mf_fsflags & FS_DIRECT)
217a53f50b9Schristos ? ""
218a53f50b9Schristos : mp->am_path,
219a53f50b9Schristos *fname == '/' ? "" : "/", fname);
220a53f50b9Schristos dlog("setting path to %s", new_mp->am_path);
221a53f50b9Schristos }
222a53f50b9Schristos
223a53f50b9Schristos return new_mp;
224a53f50b9Schristos }
225a53f50b9Schristos
226a53f50b9Schristos /*
227a53f50b9Schristos * Allocate a new mount slot and create
228a53f50b9Schristos * a new node.
229a53f50b9Schristos * Fills in the map number of the node,
230a53f50b9Schristos * but leaves everything else uninitialized.
231a53f50b9Schristos */
232a53f50b9Schristos am_node *
exported_ap_alloc(void)233a53f50b9Schristos exported_ap_alloc(void)
234a53f50b9Schristos {
235a53f50b9Schristos am_node *mp, **mpp;
236a53f50b9Schristos
237a53f50b9Schristos /*
238a53f50b9Schristos * First check if there are any slots left, realloc if needed
239a53f50b9Schristos */
240a53f50b9Schristos if (first_free_map >= exported_ap_size)
241a53f50b9Schristos if (!exported_ap_realloc_map(exported_ap_size + NEXP_AP))
242a53f50b9Schristos return 0;
243a53f50b9Schristos
244a53f50b9Schristos /*
245a53f50b9Schristos * Grab the next free slot
246a53f50b9Schristos */
247a53f50b9Schristos mpp = exported_ap + first_free_map;
248a53f50b9Schristos mp = *mpp = ALLOC(struct am_node);
249a53f50b9Schristos memset((char *) mp, 0, sizeof(struct am_node));
250a53f50b9Schristos
251a53f50b9Schristos mp->am_mapno = first_free_map++;
252a53f50b9Schristos
253a53f50b9Schristos /*
254a53f50b9Schristos * Update free pointer
255a53f50b9Schristos */
256a53f50b9Schristos while (first_free_map < exported_ap_size && exported_ap[first_free_map])
257a53f50b9Schristos first_free_map++;
258a53f50b9Schristos
259a53f50b9Schristos if (first_free_map > last_used_map)
260a53f50b9Schristos last_used_map = first_free_map - 1;
261a53f50b9Schristos
262a53f50b9Schristos return mp;
263a53f50b9Schristos }
264a53f50b9Schristos
265a53f50b9Schristos
266a53f50b9Schristos /*
267a53f50b9Schristos * Free a mount slot
268a53f50b9Schristos */
269a53f50b9Schristos static void
exported_ap_free(am_node * mp)270a53f50b9Schristos exported_ap_free(am_node *mp)
271a53f50b9Schristos {
272a53f50b9Schristos /*
273a53f50b9Schristos * Sanity check
274a53f50b9Schristos */
275a53f50b9Schristos if (!mp)
276a53f50b9Schristos return;
277a53f50b9Schristos
278a53f50b9Schristos /*
279a53f50b9Schristos * Zero the slot pointer to avoid double free's
280a53f50b9Schristos */
281a53f50b9Schristos exported_ap[mp->am_mapno] = NULL;
282a53f50b9Schristos
283a53f50b9Schristos /*
284a53f50b9Schristos * Update the free and last_used indices
285a53f50b9Schristos */
286a53f50b9Schristos if (mp->am_mapno == last_used_map)
287a53f50b9Schristos while (last_used_map >= 0 && exported_ap[last_used_map] == 0)
288a53f50b9Schristos --last_used_map;
289a53f50b9Schristos
290a53f50b9Schristos if (first_free_map > mp->am_mapno)
291a53f50b9Schristos first_free_map = mp->am_mapno;
292a53f50b9Schristos
293a53f50b9Schristos /*
294a53f50b9Schristos * Free the mount node, and zero out it's internal struct data.
295a53f50b9Schristos */
296a53f50b9Schristos memset((char *) mp, 0, sizeof(am_node));
297a53f50b9Schristos XFREE(mp);
298a53f50b9Schristos }
299a53f50b9Schristos
300a53f50b9Schristos
301a53f50b9Schristos /*
302a53f50b9Schristos * Insert mp into the correct place,
303a53f50b9Schristos * where p_mp is its parent node.
304a53f50b9Schristos * A new node gets placed as the youngest sibling
305a53f50b9Schristos * of any other children, and the parent's child
306a53f50b9Schristos * pointer is adjusted to point to the new child node.
307a53f50b9Schristos */
308a53f50b9Schristos void
insert_am(am_node * mp,am_node * p_mp)309a53f50b9Schristos insert_am(am_node *mp, am_node *p_mp)
310a53f50b9Schristos {
311a53f50b9Schristos /*
312a53f50b9Schristos * If this is going in at the root then flag it
313a53f50b9Schristos * so that it cannot be unmounted by amq.
314a53f50b9Schristos */
315a53f50b9Schristos if (p_mp == root_node)
316a53f50b9Schristos mp->am_flags |= AMF_ROOT;
317a53f50b9Schristos /*
318a53f50b9Schristos * Fill in n-way links
319a53f50b9Schristos */
320a53f50b9Schristos mp->am_parent = p_mp;
321a53f50b9Schristos mp->am_osib = p_mp->am_child;
322a53f50b9Schristos if (mp->am_osib)
323a53f50b9Schristos mp->am_osib->am_ysib = mp;
324a53f50b9Schristos p_mp->am_child = mp;
325a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
3268bae5d40Schristos if (p_mp->am_al->al_mnt->mf_flags & MFF_IS_AUTOFS)
327a53f50b9Schristos mp->am_flags |= AMF_AUTOFS;
328a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
329a53f50b9Schristos }
330a53f50b9Schristos
331a53f50b9Schristos
332a53f50b9Schristos /*
333a53f50b9Schristos * Remove am from its place in the mount tree
334a53f50b9Schristos */
335a53f50b9Schristos static void
remove_am(am_node * mp)336a53f50b9Schristos remove_am(am_node *mp)
337a53f50b9Schristos {
338a53f50b9Schristos /*
339a53f50b9Schristos * 1. Consistency check
340a53f50b9Schristos */
341a53f50b9Schristos if (mp->am_child && mp->am_parent) {
342a53f50b9Schristos plog(XLOG_WARNING, "children of \"%s\" still exist - deleting anyway", mp->am_path);
343a53f50b9Schristos }
344a53f50b9Schristos
345a53f50b9Schristos /*
346a53f50b9Schristos * 2. Update parent's child pointer
347a53f50b9Schristos */
348a53f50b9Schristos if (mp->am_parent && mp->am_parent->am_child == mp)
349a53f50b9Schristos mp->am_parent->am_child = mp->am_osib;
350a53f50b9Schristos
351a53f50b9Schristos /*
352a53f50b9Schristos * 3. Unlink from sibling chain
353a53f50b9Schristos */
354a53f50b9Schristos if (mp->am_ysib)
355a53f50b9Schristos mp->am_ysib->am_osib = mp->am_osib;
356a53f50b9Schristos if (mp->am_osib)
357a53f50b9Schristos mp->am_osib->am_ysib = mp->am_ysib;
358a53f50b9Schristos }
359a53f50b9Schristos
360a53f50b9Schristos
361a53f50b9Schristos /*
362a53f50b9Schristos * Compute a new time to live value for a node.
363a53f50b9Schristos */
364a53f50b9Schristos void
new_ttl(am_node * mp)365a53f50b9Schristos new_ttl(am_node *mp)
366a53f50b9Schristos {
367a53f50b9Schristos mp->am_timeo_w = 0;
368a53f50b9Schristos mp->am_ttl = clocktime(&mp->am_fattr.na_atime);
369a53f50b9Schristos mp->am_ttl += mp->am_timeo; /* sun's -tl option */
370a53f50b9Schristos }
371a53f50b9Schristos
372a53f50b9Schristos
373a53f50b9Schristos void
mk_fattr(nfsfattr * fattr,nfsftype vntype)374a53f50b9Schristos mk_fattr(nfsfattr *fattr, nfsftype vntype)
375a53f50b9Schristos {
376a53f50b9Schristos switch (vntype) {
377a53f50b9Schristos case NFDIR:
378a53f50b9Schristos fattr->na_type = NFDIR;
379a53f50b9Schristos fattr->na_mode = NFSMODE_DIR | 0555;
380a53f50b9Schristos fattr->na_nlink = 2;
381a53f50b9Schristos fattr->na_size = 512;
382a53f50b9Schristos break;
383a53f50b9Schristos case NFLNK:
384a53f50b9Schristos fattr->na_type = NFLNK;
385a53f50b9Schristos fattr->na_mode = NFSMODE_LNK | 0777;
386a53f50b9Schristos fattr->na_nlink = 1;
387a53f50b9Schristos fattr->na_size = 0;
388a53f50b9Schristos break;
389a53f50b9Schristos default:
390a53f50b9Schristos plog(XLOG_FATAL, "Unknown fattr type %d - ignored", vntype);
391a53f50b9Schristos break;
392a53f50b9Schristos }
393a53f50b9Schristos }
394a53f50b9Schristos
395a53f50b9Schristos
396a53f50b9Schristos /*
397a53f50b9Schristos * Initialize an allocated mount node.
398a53f50b9Schristos * It is assumed that the mount node was b-zero'd
399a53f50b9Schristos * before getting here so anything that would
400a53f50b9Schristos * be set to zero isn't done here.
401a53f50b9Schristos */
402a53f50b9Schristos void
init_map(am_node * mp,char * dir)403a53f50b9Schristos init_map(am_node *mp, char *dir)
404a53f50b9Schristos {
405a53f50b9Schristos /*
406a53f50b9Schristos * mp->am_mapno is initialized by exported_ap_alloc
407a53f50b9Schristos * other fields don't need to be set to zero.
408a53f50b9Schristos */
4098bae5d40Schristos
4108bae5d40Schristos mp->am_al = new_loc();
4118bae5d40Schristos mp->am_alarray = NULL;
4128bae5d40Schristos mp->am_name = xstrdup(dir);
4138bae5d40Schristos mp->am_path = xstrdup(dir);
414a53f50b9Schristos mp->am_gen = new_gen();
415a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
416a53f50b9Schristos mp->am_autofs_fh = NULL;
417a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
418a53f50b9Schristos
419a53f50b9Schristos mp->am_timeo = gopt.am_timeo;
420a53f50b9Schristos mp->am_attr.ns_status = NFS_OK;
421a53f50b9Schristos mp->am_fattr = gen_fattr;
422a53f50b9Schristos mp->am_fattr.na_fsid = 42;
423a53f50b9Schristos mp->am_fattr.na_fileid = mp->am_gen;
424a53f50b9Schristos clocktime(&mp->am_fattr.na_atime);
425a53f50b9Schristos /* next line copies a "struct nfstime" among several fields */
426a53f50b9Schristos mp->am_fattr.na_mtime = mp->am_fattr.na_ctime = mp->am_fattr.na_atime;
427a53f50b9Schristos
428a53f50b9Schristos new_ttl(mp);
429a53f50b9Schristos mp->am_stats.s_mtime = mp->am_fattr.na_atime.nt_seconds;
430a53f50b9Schristos mp->am_dev = -1;
431a53f50b9Schristos mp->am_rdev = -1;
4324bcd344eSchristos mp->am_fd[0] = -1;
4334bcd344eSchristos mp->am_fd[1] = -1;
4344bcd344eSchristos }
4354bcd344eSchristos
4364bcd344eSchristos
4374bcd344eSchristos void
notify_child(am_node * mp,au_etype au_etype,int au_errno,int au_signal)4384bcd344eSchristos notify_child(am_node *mp, au_etype au_etype, int au_errno, int au_signal)
4394bcd344eSchristos {
4404bcd344eSchristos amq_sync_umnt rv;
4418bae5d40Schristos int err;
4424bcd344eSchristos
4434bcd344eSchristos if (mp->am_fd[1] >= 0) { /* we have a child process */
4444bcd344eSchristos rv.au_etype = au_etype;
4454bcd344eSchristos rv.au_signal = au_signal;
4464bcd344eSchristos rv.au_errno = au_errno;
4474bcd344eSchristos
4488bae5d40Schristos err = write(mp->am_fd[1], &rv, sizeof(rv));
4498bae5d40Schristos /* XXX: do something else on err? */
4508bae5d40Schristos if (err < sizeof(rv))
4518bae5d40Schristos plog(XLOG_INFO, "notify_child: write returned %d instead of %d.",
4528bae5d40Schristos err, (int) sizeof(rv));
4534bcd344eSchristos close(mp->am_fd[1]);
4544bcd344eSchristos mp->am_fd[1] = -1;
4554bcd344eSchristos }
456a53f50b9Schristos }
457a53f50b9Schristos
458a53f50b9Schristos
459a53f50b9Schristos /*
460a53f50b9Schristos * Free a mount node.
461a53f50b9Schristos * The node must be already unmounted.
462a53f50b9Schristos */
463a53f50b9Schristos void
free_map(am_node * mp)464a53f50b9Schristos free_map(am_node *mp)
465a53f50b9Schristos {
466a53f50b9Schristos remove_am(mp);
467a53f50b9Schristos
4684bcd344eSchristos if (mp->am_fd[1] != -1)
4694bcd344eSchristos plog(XLOG_FATAL, "free_map: called prior to notifying the child for %s.",
4704bcd344eSchristos mp->am_path);
4714bcd344eSchristos
472a53f50b9Schristos XFREE(mp->am_link);
473a53f50b9Schristos XFREE(mp->am_name);
474a53f50b9Schristos XFREE(mp->am_path);
475a53f50b9Schristos XFREE(mp->am_pref);
476a53f50b9Schristos XFREE(mp->am_transp);
477a53f50b9Schristos
4788bae5d40Schristos if (mp->am_al)
4798bae5d40Schristos free_loc(mp->am_al);
480a53f50b9Schristos
4818bae5d40Schristos if (mp->am_alarray) {
4828bae5d40Schristos am_loc **temp_al;
4838bae5d40Schristos for (temp_al = mp->am_alarray; *temp_al; temp_al++)
4848bae5d40Schristos free_loc(*temp_al);
4858bae5d40Schristos XFREE(mp->am_alarray);
486a53f50b9Schristos }
487a53f50b9Schristos
488a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
489a53f50b9Schristos if (mp->am_autofs_fh)
490a53f50b9Schristos autofs_release_fh(mp);
491a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
492a53f50b9Schristos
493a53f50b9Schristos exported_ap_free(mp);
494a53f50b9Schristos }
495a53f50b9Schristos
496a53f50b9Schristos
497a53f50b9Schristos static am_node *
find_ap_recursive(char * dir,am_node * mp)498a53f50b9Schristos find_ap_recursive(char *dir, am_node *mp)
499a53f50b9Schristos {
500a53f50b9Schristos if (mp) {
501a53f50b9Schristos am_node *mp2;
502a53f50b9Schristos if (STREQ(mp->am_path, dir))
503a53f50b9Schristos return mp;
504a53f50b9Schristos
5058bae5d40Schristos if ((mp->am_al->al_mnt->mf_flags & MFF_MOUNTED) &&
5068bae5d40Schristos STREQ(mp->am_al->al_mnt->mf_mount, dir))
507a53f50b9Schristos return mp;
508a53f50b9Schristos
509a53f50b9Schristos mp2 = find_ap_recursive(dir, mp->am_osib);
510a53f50b9Schristos if (mp2)
511a53f50b9Schristos return mp2;
512a53f50b9Schristos return find_ap_recursive(dir, mp->am_child);
513a53f50b9Schristos }
514a53f50b9Schristos
515a53f50b9Schristos return 0;
516a53f50b9Schristos }
517a53f50b9Schristos
518a53f50b9Schristos
519a53f50b9Schristos /*
520a53f50b9Schristos * Find the mount node corresponding to dir. dir can match either the
521a53f50b9Schristos * automount path or, if the node is mounted, the mount location.
522a53f50b9Schristos */
523a53f50b9Schristos am_node *
find_ap(char * dir)524a53f50b9Schristos find_ap(char *dir)
525a53f50b9Schristos {
526a53f50b9Schristos int i;
527a53f50b9Schristos
528a53f50b9Schristos for (i = last_used_map; i >= 0; --i) {
529a53f50b9Schristos am_node *mp = exported_ap[i];
530a53f50b9Schristos if (mp && (mp->am_flags & AMF_ROOT)) {
531a53f50b9Schristos mp = find_ap_recursive(dir, exported_ap[i]);
532a53f50b9Schristos if (mp) {
533a53f50b9Schristos return mp;
534a53f50b9Schristos }
535a53f50b9Schristos }
536a53f50b9Schristos }
537a53f50b9Schristos
538a53f50b9Schristos return 0;
539a53f50b9Schristos }
540a53f50b9Schristos
541a53f50b9Schristos
542a53f50b9Schristos /*
543a53f50b9Schristos * Get the filehandle for a particular named directory.
544a53f50b9Schristos * This is used during the bootstrap to tell the kernel
545a53f50b9Schristos * the filehandles of the initial automount points.
546a53f50b9Schristos */
5478bae5d40Schristos am_nfs_handle_t *
get_root_nfs_fh(char * dir,am_nfs_handle_t * nfh)5488bae5d40Schristos get_root_nfs_fh(char *dir, am_nfs_handle_t *nfh)
549a53f50b9Schristos {
550a53f50b9Schristos am_node *mp = get_root_ap(dir);
551a53f50b9Schristos if (mp) {
5528bae5d40Schristos if (nfs_dispatcher == nfs_program_2)
5538bae5d40Schristos mp_to_fh(mp, &nfh->v2);
5548bae5d40Schristos else
5558bae5d40Schristos mp_to_fh3(mp, &nfh->v3);
5568bae5d40Schristos return nfh;
557a53f50b9Schristos }
558a53f50b9Schristos
559a53f50b9Schristos /*
560a53f50b9Schristos * Should never get here...
561a53f50b9Schristos */
562a53f50b9Schristos plog(XLOG_ERROR, "Can't find root filehandle for %s", dir);
563a53f50b9Schristos
564a53f50b9Schristos return 0;
565a53f50b9Schristos }
566a53f50b9Schristos
567a53f50b9Schristos
568a53f50b9Schristos static am_node *
get_root_ap(char * dir)569a53f50b9Schristos get_root_ap(char *dir)
570a53f50b9Schristos {
571a53f50b9Schristos am_node *mp = find_ap(dir);
572a53f50b9Schristos
573a53f50b9Schristos if (mp && mp->am_parent == root_node)
574a53f50b9Schristos return mp;
575a53f50b9Schristos
576a53f50b9Schristos return 0;
577a53f50b9Schristos }
578a53f50b9Schristos
579a53f50b9Schristos
580a53f50b9Schristos /*
581a53f50b9Schristos * Timeout all nodes waiting on
582a53f50b9Schristos * a given Fserver.
583a53f50b9Schristos */
584a53f50b9Schristos void
map_flush_srvr(fserver * fs)585a53f50b9Schristos map_flush_srvr(fserver *fs)
586a53f50b9Schristos {
587a53f50b9Schristos int i;
588a53f50b9Schristos int done = 0;
589a53f50b9Schristos
590a53f50b9Schristos for (i = last_used_map; i >= 0; --i) {
591a53f50b9Schristos am_node *mp = exported_ap[i];
5928bae5d40Schristos
5938bae5d40Schristos if (mp && mp->am_al->al_mnt && mp->am_al->al_mnt->mf_server == fs) {
594a53f50b9Schristos plog(XLOG_INFO, "Flushed %s; dependent on %s", mp->am_path, fs->fs_host);
595a53f50b9Schristos mp->am_ttl = clocktime(NULL);
596a53f50b9Schristos done = 1;
597a53f50b9Schristos }
598a53f50b9Schristos }
599a53f50b9Schristos if (done)
600a53f50b9Schristos reschedule_timeout_mp();
601a53f50b9Schristos }
602a53f50b9Schristos
603a53f50b9Schristos
604a53f50b9Schristos /*
605a53f50b9Schristos * Mount a top level automount node
606a53f50b9Schristos * by calling lookup in the parent
607a53f50b9Schristos * (root) node which will cause the
608a53f50b9Schristos * automount node to be automounted.
609a53f50b9Schristos */
610a53f50b9Schristos int
mount_auto_node(char * dir,opaque_t arg)611a53f50b9Schristos mount_auto_node(char *dir, opaque_t arg)
612a53f50b9Schristos {
613a53f50b9Schristos int error = 0;
614a53f50b9Schristos am_node *mp = (am_node *) arg;
615a53f50b9Schristos am_node *new_mp;
616a53f50b9Schristos
6178bae5d40Schristos new_mp = mp->am_al->al_mnt->mf_ops->lookup_child(mp, dir, &error, VLOOK_CREATE);
618a53f50b9Schristos if (new_mp && error < 0) {
619a53f50b9Schristos /*
620a53f50b9Schristos * We can't allow the fileid of the root node to change.
621a53f50b9Schristos * Should be ok to force it to 1, always.
622a53f50b9Schristos */
623a53f50b9Schristos new_mp->am_gen = new_mp->am_fattr.na_fileid = 1;
624a53f50b9Schristos
6258bae5d40Schristos (void) mp->am_al->al_mnt->mf_ops->mount_child(new_mp, &error);
626a53f50b9Schristos }
627a53f50b9Schristos
628a53f50b9Schristos if (error > 0) {
629a53f50b9Schristos errno = error; /* XXX */
630a53f50b9Schristos plog(XLOG_ERROR, "Could not mount %s: %m", dir);
631a53f50b9Schristos }
632a53f50b9Schristos return error;
633a53f50b9Schristos }
634a53f50b9Schristos
635a53f50b9Schristos
636a53f50b9Schristos /*
637a53f50b9Schristos * Cause all the top-level mount nodes
638a53f50b9Schristos * to be automounted
639a53f50b9Schristos */
640a53f50b9Schristos int
mount_exported(void)641a53f50b9Schristos mount_exported(void)
642a53f50b9Schristos {
643a53f50b9Schristos /*
644a53f50b9Schristos * Iterate over all the nodes to be started
645a53f50b9Schristos */
646a53f50b9Schristos return root_keyiter(mount_auto_node, root_node);
647a53f50b9Schristos }
648a53f50b9Schristos
649a53f50b9Schristos
650a53f50b9Schristos /*
651a53f50b9Schristos * Construct top-level node
652a53f50b9Schristos */
653a53f50b9Schristos void
make_root_node(void)654a53f50b9Schristos make_root_node(void)
655a53f50b9Schristos {
6568bae5d40Schristos mntfs *root_mf;
657a53f50b9Schristos char *rootmap = ROOT_MAP;
658a53f50b9Schristos root_node = exported_ap_alloc();
659a53f50b9Schristos
660a53f50b9Schristos /*
661a53f50b9Schristos * Allocate a new map
662a53f50b9Schristos */
663a53f50b9Schristos init_map(root_node, "");
664a53f50b9Schristos
665a53f50b9Schristos /*
666a53f50b9Schristos * Allocate a new mounted filesystem
667a53f50b9Schristos */
6688bae5d40Schristos root_mf = find_mntfs(&amfs_root_ops, (am_opts *) NULL, "", rootmap, "", "", "");
669a53f50b9Schristos
670a53f50b9Schristos /*
671a53f50b9Schristos * Replace the initial null reference
672a53f50b9Schristos */
6738bae5d40Schristos free_mntfs(root_node->am_al->al_mnt);
6748bae5d40Schristos root_node->am_al->al_mnt = root_mf;
675a53f50b9Schristos
676a53f50b9Schristos /*
677a53f50b9Schristos * Initialize the root
678a53f50b9Schristos */
6798bae5d40Schristos if (root_mf->mf_ops->fs_init)
6808bae5d40Schristos (*root_mf->mf_ops->fs_init) (root_mf);
681a53f50b9Schristos
682a53f50b9Schristos /*
683a53f50b9Schristos * Mount the root
684a53f50b9Schristos */
6858bae5d40Schristos root_mf->mf_error = root_mf->mf_ops->mount_fs(root_node, root_mf);
686a53f50b9Schristos }
687a53f50b9Schristos
688a53f50b9Schristos
689a53f50b9Schristos /*
690a53f50b9Schristos * Cause all the nodes to be unmounted by timing
691a53f50b9Schristos * them out.
692a53f50b9Schristos */
693a53f50b9Schristos void
umount_exported(void)694a53f50b9Schristos umount_exported(void)
695a53f50b9Schristos {
6968bae5d40Schristos int i, work_done;
6978bae5d40Schristos
6988bae5d40Schristos do {
6998bae5d40Schristos work_done = 0;
700a53f50b9Schristos
701a53f50b9Schristos for (i = last_used_map; i >= 0; --i) {
702a53f50b9Schristos am_node *mp = exported_ap[i];
703a53f50b9Schristos mntfs *mf;
704a53f50b9Schristos
705a53f50b9Schristos if (!mp)
706a53f50b9Schristos continue;
707a53f50b9Schristos
7088bae5d40Schristos /*
7098bae5d40Schristos * Wait for children to be removed first
7108bae5d40Schristos */
7118bae5d40Schristos if (mp->am_child)
7128bae5d40Schristos continue;
7138bae5d40Schristos
7148bae5d40Schristos mf = mp->am_al->al_mnt;
715a53f50b9Schristos if (mf->mf_flags & MFF_UNMOUNTING) {
716a53f50b9Schristos /*
717a53f50b9Schristos * If this node is being unmounted then just ignore it. However,
718a53f50b9Schristos * this could prevent amd from finishing if the unmount gets blocked
719a53f50b9Schristos * since the am_node will never be free'd. am_unmounted needs
720a53f50b9Schristos * telling about this possibility. - XXX
721a53f50b9Schristos */
722a53f50b9Schristos continue;
723a53f50b9Schristos }
724a53f50b9Schristos
725a53f50b9Schristos if (!(mf->mf_fsflags & FS_DIRECTORY))
726a53f50b9Schristos /*
727a53f50b9Schristos * When shutting down this had better
728a53f50b9Schristos * look like a directory, otherwise it
729a53f50b9Schristos * can't be unmounted!
730a53f50b9Schristos */
731a53f50b9Schristos mk_fattr(&mp->am_fattr, NFDIR);
732a53f50b9Schristos
733a53f50b9Schristos if ((--immediate_abort < 0 &&
734a53f50b9Schristos !(mp->am_flags & AMF_ROOT) && mp->am_parent) ||
735a53f50b9Schristos (mf->mf_flags & MFF_RESTART)) {
736a53f50b9Schristos
7378bae5d40Schristos work_done++;
7388bae5d40Schristos
739a53f50b9Schristos /*
740a53f50b9Schristos * Just throw this node away without bothering to unmount it. If
741a53f50b9Schristos * the server is not known to be up then don't discard the mounted
742a53f50b9Schristos * on directory or Amd might hang...
743a53f50b9Schristos */
744a53f50b9Schristos if (mf->mf_server &&
745a53f50b9Schristos (mf->mf_server->fs_flags & (FSF_DOWN | FSF_VALID)) != FSF_VALID)
746a53f50b9Schristos mf->mf_flags &= ~MFF_MKMNT;
747a53f50b9Schristos if (gopt.flags & CFM_UNMOUNT_ON_EXIT || mp->am_flags & AMF_AUTOFS) {
748a53f50b9Schristos plog(XLOG_INFO, "on-exit attempt to unmount %s", mf->mf_mount);
749a53f50b9Schristos /*
750a53f50b9Schristos * use unmount_mp, not unmount_node, so that unmounts be
751a53f50b9Schristos * backgrounded as needed.
752a53f50b9Schristos */
753a53f50b9Schristos unmount_mp((opaque_t) mp);
754a53f50b9Schristos } else {
755a53f50b9Schristos am_unmounted(mp);
756a53f50b9Schristos }
757*9f182afdSjoerg if (!(mf->mf_flags & (MFF_UNMOUNTING|MFF_MOUNTED)))
758a53f50b9Schristos exported_ap[i] = NULL;
759a53f50b9Schristos } else {
760a53f50b9Schristos /*
761a53f50b9Schristos * Any other node gets forcibly timed out.
762a53f50b9Schristos */
763a53f50b9Schristos mp->am_flags &= ~AMF_NOTIMEOUT;
7648bae5d40Schristos mp->am_al->al_mnt->mf_flags &= ~MFF_RSTKEEP;
765a53f50b9Schristos mp->am_ttl = 0;
766a53f50b9Schristos mp->am_timeo = 1;
767a53f50b9Schristos mp->am_timeo_w = 0;
768a53f50b9Schristos }
769a53f50b9Schristos }
7708bae5d40Schristos } while (work_done);
771a53f50b9Schristos }
772a53f50b9Schristos
773a53f50b9Schristos
774a53f50b9Schristos /*
775a53f50b9Schristos * Try to mount a file system. Can be called directly or in a sub-process by run_task.
776a53f50b9Schristos *
777a53f50b9Schristos * Warning: this function might be running in a child process context.
778a53f50b9Schristos * Don't expect any changes made here to survive in the parent amd process.
779a53f50b9Schristos */
780a53f50b9Schristos int
mount_node(opaque_t arg)781a53f50b9Schristos mount_node(opaque_t arg)
782a53f50b9Schristos {
783a53f50b9Schristos am_node *mp = (am_node *) arg;
7848bae5d40Schristos mntfs *mf = mp->am_al->al_mnt;
785a53f50b9Schristos int error = 0;
786a53f50b9Schristos
787a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
788a53f50b9Schristos if (mp->am_flags & AMF_AUTOFS)
789a53f50b9Schristos error = autofs_mount_fs(mp, mf);
790a53f50b9Schristos else
791a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
792a53f50b9Schristos if (!(mf->mf_flags & MFF_MOUNTED))
793a53f50b9Schristos error = mf->mf_ops->mount_fs(mp, mf);
794a53f50b9Schristos
795a53f50b9Schristos if (error > 0)
796a53f50b9Schristos dlog("mount_node: call to mf_ops->mount_fs(%s) failed: %s",
797a53f50b9Schristos mp->am_path, strerror(error));
798a53f50b9Schristos return error;
799a53f50b9Schristos }
800a53f50b9Schristos
801a53f50b9Schristos
802a53f50b9Schristos static int
unmount_node(opaque_t arg)803a53f50b9Schristos unmount_node(opaque_t arg)
804a53f50b9Schristos {
805a53f50b9Schristos am_node *mp = (am_node *) arg;
8068bae5d40Schristos mntfs *mf = mp->am_al->al_mnt;
807a53f50b9Schristos int error = 0;
808a53f50b9Schristos
809a53f50b9Schristos if (mf->mf_flags & MFF_ERROR) {
810a53f50b9Schristos /*
811a53f50b9Schristos * Just unlink
812a53f50b9Schristos */
813a53f50b9Schristos dlog("No-op unmount of error node %s", mf->mf_info);
814a53f50b9Schristos } else {
815a53f50b9Schristos dlog("Unmounting <%s> <%s> (%s) flags %x",
816a53f50b9Schristos mp->am_path, mf->mf_mount, mf->mf_info, mf->mf_flags);
817a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
818a53f50b9Schristos if (mp->am_flags & AMF_AUTOFS)
819a53f50b9Schristos error = autofs_umount_fs(mp, mf);
820a53f50b9Schristos else
821a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
822a53f50b9Schristos if (mf->mf_refc == 1)
823a53f50b9Schristos error = mf->mf_ops->umount_fs(mp, mf);
824a53f50b9Schristos }
825a53f50b9Schristos
826a53f50b9Schristos /* do this again, it might have changed */
8278bae5d40Schristos mf = mp->am_al->al_mnt;
828a53f50b9Schristos if (error) {
829a53f50b9Schristos errno = error; /* XXX */
830a53f50b9Schristos dlog("%s: unmount: %m", mf->mf_mount);
831a53f50b9Schristos }
832a53f50b9Schristos
833a53f50b9Schristos return error;
834a53f50b9Schristos }
835a53f50b9Schristos
836a53f50b9Schristos
837a53f50b9Schristos static void
free_map_if_success(int rc,int term,opaque_t arg)838a53f50b9Schristos free_map_if_success(int rc, int term, opaque_t arg)
839a53f50b9Schristos {
840a53f50b9Schristos am_node *mp = (am_node *) arg;
8418bae5d40Schristos mntfs *mf = mp->am_al->al_mnt;
842a53f50b9Schristos wchan_t wchan = get_mntfs_wchan(mf);
843a53f50b9Schristos
844a53f50b9Schristos /*
845a53f50b9Schristos * Not unmounting any more
846a53f50b9Schristos */
847a53f50b9Schristos mf->mf_flags &= ~MFF_UNMOUNTING;
848a53f50b9Schristos
849a53f50b9Schristos /*
850a53f50b9Schristos * If a timeout was deferred because the underlying filesystem
851a53f50b9Schristos * was busy then arrange for a timeout as soon as possible.
852a53f50b9Schristos */
853a53f50b9Schristos if (mf->mf_flags & MFF_WANTTIMO) {
854a53f50b9Schristos mf->mf_flags &= ~MFF_WANTTIMO;
855a53f50b9Schristos reschedule_timeout_mp();
856a53f50b9Schristos }
857a53f50b9Schristos if (term) {
8584bcd344eSchristos notify_child(mp, AMQ_UMNT_SIGNAL, 0, term);
859a53f50b9Schristos plog(XLOG_ERROR, "unmount for %s got signal %d", mp->am_path, term);
860a53f50b9Schristos #if defined(DEBUG) && defined(SIGTRAP)
861a53f50b9Schristos /*
862a53f50b9Schristos * dbx likes to put a trap on exit().
863a53f50b9Schristos * Pretend it succeeded for now...
864a53f50b9Schristos */
865a53f50b9Schristos if (term == SIGTRAP) {
866a53f50b9Schristos am_unmounted(mp);
867a53f50b9Schristos }
868a53f50b9Schristos #endif /* DEBUG */
869a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
870a53f50b9Schristos if (mp->am_flags & AMF_AUTOFS)
871a53f50b9Schristos autofs_umount_failed(mp);
872a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
873a53f50b9Schristos amd_stats.d_uerr++;
874a53f50b9Schristos } else if (rc) {
8754bcd344eSchristos notify_child(mp, AMQ_UMNT_FAILED, rc, 0);
876a53f50b9Schristos if (mf->mf_ops == &amfs_program_ops || rc == EBUSY)
877a53f50b9Schristos plog(XLOG_STATS, "\"%s\" on %s still active", mp->am_path, mf->mf_mount);
878a53f50b9Schristos else
879a53f50b9Schristos plog(XLOG_ERROR, "%s: unmount: %s", mp->am_path, strerror(rc));
880a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
8818bae5d40Schristos if (rc != ENOENT) {
882a53f50b9Schristos if (mf->mf_flags & MFF_IS_AUTOFS)
883a53f50b9Schristos autofs_get_mp(mp);
884a53f50b9Schristos if (mp->am_flags & AMF_AUTOFS)
885a53f50b9Schristos autofs_umount_failed(mp);
8868bae5d40Schristos }
887a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
888a53f50b9Schristos amd_stats.d_uerr++;
889a53f50b9Schristos } else {
8904bcd344eSchristos /*
8914bcd344eSchristos * am_unmounted() will call notify_child() appropriately.
8924bcd344eSchristos */
893a53f50b9Schristos am_unmounted(mp);
894a53f50b9Schristos }
895a53f50b9Schristos
896a53f50b9Schristos /*
897a53f50b9Schristos * Wakeup anything waiting for this unmount
898a53f50b9Schristos */
899a53f50b9Schristos wakeup(wchan);
900a53f50b9Schristos }
901a53f50b9Schristos
902a53f50b9Schristos
903a53f50b9Schristos int
unmount_mp(am_node * mp)904a53f50b9Schristos unmount_mp(am_node *mp)
905a53f50b9Schristos {
906a53f50b9Schristos int was_backgrounded = 0;
9078bae5d40Schristos mntfs *mf = mp->am_al->al_mnt;
908a53f50b9Schristos
909a53f50b9Schristos #ifdef notdef
910a53f50b9Schristos plog(XLOG_INFO, "\"%s\" on %s timed out (flags 0x%x)",
9118bae5d40Schristos mp->am_path, mf->mf_mount, (int) mf->mf_flags);
912a53f50b9Schristos #endif /* notdef */
913a53f50b9Schristos
914a53f50b9Schristos #ifndef MNT2_NFS_OPT_SYMTTL
915a53f50b9Schristos /*
916a53f50b9Schristos * This code is needed to defeat Solaris 2.4's (and newer) symlink
917a53f50b9Schristos * values cache. It forces the last-modified time of the symlink to be
918a53f50b9Schristos * current. It is not needed if the O/S has an nfs flag to turn off the
919a53f50b9Schristos * symlink-cache at mount time (such as Irix 5.x and 6.x). -Erez.
920a53f50b9Schristos *
921a53f50b9Schristos * Additionally, Linux currently ignores the nt_useconds field,
922a53f50b9Schristos * so we must update the nt_seconds field every time if clocktime(NULL)
923a53f50b9Schristos * didn't return a new number of seconds.
924a53f50b9Schristos */
925a53f50b9Schristos if (mp->am_parent) {
926a53f50b9Schristos time_t last = mp->am_parent->am_attr.ns_u.ns_attr_u.na_mtime.nt_seconds;
927a53f50b9Schristos clocktime(&mp->am_parent->am_attr.ns_u.ns_attr_u.na_mtime);
928a53f50b9Schristos /* defensive programming... can't we assert the above condition? */
929a53f50b9Schristos if (last == (time_t) mp->am_parent->am_attr.ns_u.ns_attr_u.na_mtime.nt_seconds)
930a53f50b9Schristos mp->am_parent->am_attr.ns_u.ns_attr_u.na_mtime.nt_seconds++;
931a53f50b9Schristos }
932a53f50b9Schristos #endif /* not MNT2_NFS_OPT_SYMTTL */
933a53f50b9Schristos
934a53f50b9Schristos if (mf->mf_refc == 1 && !FSRV_ISUP(mf->mf_server)) {
935a53f50b9Schristos /*
936a53f50b9Schristos * Don't try to unmount from a server that is known to be down
937a53f50b9Schristos */
938a53f50b9Schristos if (!(mf->mf_flags & MFF_LOGDOWN)) {
939a53f50b9Schristos /* Only log this once, otherwise gets a bit boring */
940a53f50b9Schristos plog(XLOG_STATS, "file server %s is down - timeout of \"%s\" ignored", mf->mf_server->fs_host, mp->am_path);
941a53f50b9Schristos mf->mf_flags |= MFF_LOGDOWN;
942a53f50b9Schristos }
9434bcd344eSchristos notify_child(mp, AMQ_UMNT_SERVER, 0, 0);
944a53f50b9Schristos return 0;
945a53f50b9Schristos }
946a53f50b9Schristos
9478bae5d40Schristos dlog("\"%s\" on %s timed out", mp->am_path, mf->mf_mount);
948a53f50b9Schristos mf->mf_flags |= MFF_UNMOUNTING;
949a53f50b9Schristos
950a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
951a53f50b9Schristos if (mf->mf_flags & MFF_IS_AUTOFS)
952a53f50b9Schristos autofs_release_mp(mp);
953a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
954a53f50b9Schristos
955a53f50b9Schristos if ((mf->mf_fsflags & FS_UBACKGROUND) &&
9568bae5d40Schristos (mf->mf_flags & MFF_MOUNTED) &&
9578bae5d40Schristos !(mf->mf_flags & MFF_ON_AUTOFS)) {
958a53f50b9Schristos dlog("Trying unmount in background");
959a53f50b9Schristos run_task(unmount_node, (opaque_t) mp,
960a53f50b9Schristos free_map_if_success, (opaque_t) mp);
961a53f50b9Schristos was_backgrounded = 1;
962a53f50b9Schristos } else {
963a53f50b9Schristos dlog("Trying unmount in foreground");
964a53f50b9Schristos free_map_if_success(unmount_node((opaque_t) mp), 0, (opaque_t) mp);
965a53f50b9Schristos dlog("unmount attempt done");
966a53f50b9Schristos }
967a53f50b9Schristos
968a53f50b9Schristos return was_backgrounded;
969a53f50b9Schristos }
970a53f50b9Schristos
971a53f50b9Schristos
972a53f50b9Schristos void
timeout_mp(opaque_t v)973a53f50b9Schristos timeout_mp(opaque_t v) /* argument not used?! */
974a53f50b9Schristos {
975a53f50b9Schristos int i;
976a53f50b9Schristos time_t t = NEVER;
977a53f50b9Schristos time_t now = clocktime(NULL);
978a53f50b9Schristos int backoff = NumChildren / 4;
979a53f50b9Schristos
980a53f50b9Schristos dlog("Timing out automount points...");
981a53f50b9Schristos
982a53f50b9Schristos for (i = last_used_map; i >= 0; --i) {
983a53f50b9Schristos am_node *mp = exported_ap[i];
984a53f50b9Schristos mntfs *mf;
985a53f50b9Schristos
986a53f50b9Schristos /*
987a53f50b9Schristos * Just continue if nothing mounted
988a53f50b9Schristos */
989a53f50b9Schristos if (!mp)
990a53f50b9Schristos continue;
991a53f50b9Schristos
992a53f50b9Schristos /*
993a53f50b9Schristos * Pick up mounted filesystem
994a53f50b9Schristos */
9958bae5d40Schristos mf = mp->am_al->al_mnt;
996a53f50b9Schristos if (!mf)
997a53f50b9Schristos continue;
998a53f50b9Schristos
999a53f50b9Schristos #ifdef HAVE_FS_AUTOFS
1000a53f50b9Schristos if (mf->mf_flags & MFF_IS_AUTOFS && mp->am_autofs_ttl != NEVER) {
1001a53f50b9Schristos if (now >= mp->am_autofs_ttl)
1002a53f50b9Schristos autofs_timeout_mp(mp);
1003a53f50b9Schristos t = smallest_t(t, mp->am_autofs_ttl);
1004a53f50b9Schristos }
1005a53f50b9Schristos #endif /* HAVE_FS_AUTOFS */
1006a53f50b9Schristos
1007a53f50b9Schristos if (mp->am_flags & AMF_NOTIMEOUT)
1008a53f50b9Schristos continue;
1009a53f50b9Schristos
1010a53f50b9Schristos /*
1011a53f50b9Schristos * Don't delete last reference to a restarted filesystem.
1012a53f50b9Schristos */
1013a53f50b9Schristos if ((mf->mf_flags & MFF_RSTKEEP) && mf->mf_refc == 1)
1014a53f50b9Schristos continue;
1015a53f50b9Schristos
1016a53f50b9Schristos /*
1017a53f50b9Schristos * If there is action on this filesystem then ignore it
1018a53f50b9Schristos */
1019a53f50b9Schristos if (!(mf->mf_flags & IGNORE_FLAGS)) {
1020a53f50b9Schristos int expired = 0;
1021a53f50b9Schristos mf->mf_flags &= ~MFF_WANTTIMO;
1022a53f50b9Schristos if (now >= mp->am_ttl) {
1023a53f50b9Schristos if (!backoff) {
1024a53f50b9Schristos expired = 1;
1025a53f50b9Schristos
1026a53f50b9Schristos /*
1027a53f50b9Schristos * Move the ttl forward to avoid thrashing effects
1028a53f50b9Schristos * on the next call to timeout!
1029a53f50b9Schristos */
1030a53f50b9Schristos /* sun's -tw option */
1031a53f50b9Schristos if (mp->am_timeo_w < 4 * gopt.am_timeo_w)
1032a53f50b9Schristos mp->am_timeo_w += gopt.am_timeo_w;
1033a53f50b9Schristos mp->am_ttl = now + mp->am_timeo_w;
1034a53f50b9Schristos
1035a53f50b9Schristos } else {
1036a53f50b9Schristos /*
1037a53f50b9Schristos * Just backoff this unmount for
1038a53f50b9Schristos * a couple of seconds to avoid
1039a53f50b9Schristos * many multiple unmounts being
1040a53f50b9Schristos * started in parallel.
1041a53f50b9Schristos */
1042a53f50b9Schristos mp->am_ttl = now + backoff + 1;
1043a53f50b9Schristos }
1044a53f50b9Schristos }
1045a53f50b9Schristos
1046a53f50b9Schristos /*
1047a53f50b9Schristos * If the next ttl is smallest, use that
1048a53f50b9Schristos */
1049a53f50b9Schristos t = smallest_t(t, mp->am_ttl);
1050a53f50b9Schristos
1051a53f50b9Schristos if (!mp->am_child && mf->mf_error >= 0 && expired) {
1052a53f50b9Schristos /*
1053a53f50b9Schristos * If the unmount was backgrounded then
1054a53f50b9Schristos * bump the backoff counter.
1055a53f50b9Schristos */
1056a53f50b9Schristos if (unmount_mp(mp)) {
1057a53f50b9Schristos backoff = 2;
1058a53f50b9Schristos }
1059a53f50b9Schristos }
1060a53f50b9Schristos } else if (mf->mf_flags & MFF_UNMOUNTING) {
1061a53f50b9Schristos mf->mf_flags |= MFF_WANTTIMO;
1062a53f50b9Schristos }
1063a53f50b9Schristos }
1064a53f50b9Schristos
1065a53f50b9Schristos if (t == NEVER) {
1066a53f50b9Schristos dlog("No further timeouts");
1067a53f50b9Schristos t = now + ONE_HOUR;
1068a53f50b9Schristos }
1069a53f50b9Schristos
1070a53f50b9Schristos /*
1071a53f50b9Schristos * Sanity check to avoid runaways.
1072a53f50b9Schristos * Absolutely should never get this but
1073a53f50b9Schristos * if you do without this trap amd will thrash.
1074a53f50b9Schristos */
1075a53f50b9Schristos if (t <= now) {
1076a53f50b9Schristos t = now + 6; /* XXX */
1077a53f50b9Schristos plog(XLOG_ERROR, "Got a zero interval in timeout_mp()!");
1078a53f50b9Schristos }
1079a53f50b9Schristos
1080a53f50b9Schristos /*
1081a53f50b9Schristos * XXX - when shutting down, make things happen faster
1082a53f50b9Schristos */
1083a53f50b9Schristos if ((int) amd_state >= (int) Finishing)
1084a53f50b9Schristos t = now + 1;
1085a53f50b9Schristos dlog("Next mount timeout in %lds", (long) (t - now));
1086a53f50b9Schristos
1087a53f50b9Schristos timeout_mp_id = timeout(t - now, timeout_mp, NULL);
1088a53f50b9Schristos }
1089a53f50b9Schristos
1090a53f50b9Schristos
1091a53f50b9Schristos /*
1092a53f50b9Schristos * Cause timeout_mp to be called soonest
1093a53f50b9Schristos */
1094a53f50b9Schristos void
reschedule_timeout_mp(void)1095a53f50b9Schristos reschedule_timeout_mp(void)
1096a53f50b9Schristos {
1097a53f50b9Schristos if (timeout_mp_id)
1098a53f50b9Schristos untimeout(timeout_mp_id);
1099a53f50b9Schristos timeout_mp_id = timeout(0, timeout_mp, NULL);
1100a53f50b9Schristos }
1101