1/*	$NetBSD: autofs.h,v 1.5 2023/05/17 06:44:38 tkusumi Exp $	*/
2
3/*-
4 * Copyright (c) 2017 The NetBSD Foundation, Inc.
5 * Copyright (c) 2016 The DragonFly Project
6 * Copyright (c) 2014 The FreeBSD Foundation
7 * All rights reserved.
8 *
9 * This code is derived from software contributed to The NetBSD Foundation
10 * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11 *
12 * This software was developed by Edward Tomasz Napierala under sponsorship
13 * from the FreeBSD Foundation.
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 *    notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 *    notice, this list of conditions and the following disclaimer in the
22 *    documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 * $FreeBSD$
37 */
38
39#ifndef _SYS_FS_AUTOFS_AUTOFS_H_
40#define	_SYS_FS_AUTOFS_AUTOFS_H_
41
42#include <sys/param.h>
43#include <sys/kernel.h>
44#include <sys/module.h>
45#include <sys/types.h>
46#include <sys/systm.h>
47#include <sys/kmem.h>
48#include <sys/tree.h>
49#include <sys/mutex.h>
50#include <sys/condvar.h>
51#include <sys/mount.h>
52#include <sys/vnode.h>
53#include <sys/timespec.h>
54#include <sys/callout.h>
55#include <sys/workqueue.h>
56#include <sys/conf.h>
57#include <sys/proc.h>
58
59#include "autofs_ioctl.h"
60
61#define AUTOFS_ROOTINO	((ino_t)1)
62#define VFSTOAUTOFS(mp)	((struct autofs_mount *)((mp)->mnt_data))
63#define VTOI(vp)	((struct autofs_node *)((vp)->v_data))
64
65extern int (**autofs_vnodeop_p)(void *);
66extern const struct vnodeopv_desc autofs_vnodeop_opv_desc;
67
68extern struct cdevsw autofs_ops;
69
70extern struct pool autofs_request_pool;
71extern struct pool autofs_node_pool;
72extern struct autofs_softc *autofs_softc;
73extern struct workqueue	*autofs_tmo_wq;
74
75extern int autofs_debug;
76extern int autofs_mount_on_stat;
77extern int autofs_timeout;
78extern int autofs_cache;
79extern int autofs_retry_attempts;
80extern int autofs_retry_delay;
81extern int autofs_interruptible;
82
83#define	AUTOFS_DEBUG(X, ...)				\
84	do {						\
85		if (autofs_debug > 1)			\
86			printf("%s: " X "\n",		\
87			    __func__, ## __VA_ARGS__);	\
88	} while (0)
89
90#define	AUTOFS_WARN(X, ...)				\
91	do {						\
92		if (autofs_debug > 0) {			\
93			printf("WARNING: %s: " X "\n",	\
94			    __func__, ## __VA_ARGS__);	\
95		}					\
96	} while (0)
97
98struct autofs_node {
99	RB_ENTRY(autofs_node)		an_entry;
100	char				*an_name;
101	ino_t				an_ino;
102	struct autofs_node		*an_parent;
103	RB_HEAD(autofs_node_tree,
104	    autofs_node)		an_children;
105	struct autofs_mount		*an_mount;
106	struct vnode			*an_vnode;
107	bool				an_cached;
108	bool				an_wildcards;
109	struct callout			an_callout;
110	int				an_retries;
111	struct timespec			an_ctime;
112};
113
114struct autofs_mount {
115	struct autofs_node		*am_root;
116	struct mount			*am_mp;
117	kmutex_t			am_lock;
118	char				am_from[AUTOFS_MAXPATHLEN];
119	char				am_on[AUTOFS_MAXPATHLEN];
120	char				am_options[AUTOFS_MAXPATHLEN];
121	char				am_prefix[AUTOFS_MAXPATHLEN];
122	ino_t				am_last_ino;
123};
124
125struct autofs_request {
126	struct work			ar_wk; /* must be first */
127	TAILQ_ENTRY(autofs_request)	ar_next;
128	struct autofs_mount		*ar_mount;
129	int				ar_id;
130	bool				ar_done;
131	int				ar_error;
132	bool				ar_wildcards;
133	bool				ar_in_progress;
134	char				ar_from[AUTOFS_MAXPATHLEN];
135	char				ar_path[AUTOFS_MAXPATHLEN];
136	char				ar_prefix[AUTOFS_MAXPATHLEN];
137	char				ar_key[AUTOFS_MAXPATHLEN];
138	char				ar_options[AUTOFS_MAXPATHLEN];
139	struct callout			ar_callout;
140	volatile unsigned int		ar_refcount;
141};
142
143struct autofs_softc {
144	kcondvar_t			sc_cv;
145	kmutex_t			sc_lock;
146	TAILQ_HEAD(, autofs_request)	sc_requests;
147	bool				sc_dev_opened;
148	pid_t				sc_dev_sid;
149	int				sc_last_request_id;
150};
151
152int	autofs_trigger(struct autofs_node *anp, const char *component,
153	    int componentlen);
154bool	autofs_cached(struct autofs_node *anp, const char *component,
155	    int componentlen);
156void	autofs_flush(struct autofs_mount *amp);
157bool	autofs_ignore_thread(void);
158void	autofs_timeout_wq(struct work *wk, void *arg);
159int	autofs_node_new(struct autofs_node *parent, struct autofs_mount *amp,
160	    const char *name, int namelen, struct autofs_node **anpp);
161int	autofs_node_find(struct autofs_node *parent,
162	    const char *name, int namelen, struct autofs_node **anpp);
163void	autofs_node_delete(struct autofs_node *anp);
164
165static __inline void
166autofs_node_cache(struct autofs_node *anp)
167{
168
169	anp->an_cached = true;
170}
171
172static __inline void
173autofs_node_uncache(struct autofs_node *anp)
174{
175
176	anp->an_cached = false;
177}
178
179static __inline char *
180autofs_strndup(const char *name, int nlen, km_flag_t fl)
181{
182	return nlen > 0 ? kmem_strndup(name, nlen, fl) : kmem_strdup(name, fl);
183}
184
185RB_PROTOTYPE(autofs_node_tree, autofs_node, an_entry, autofs_node_cmp);
186
187#endif /* !_SYS_FS_AUTOFS_AUTOFS_H_ */
188