xref: /netbsd-src/lib/libperfuse/perfuse_if.h (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /*  $NetBSD: perfuse_if.h,v 1.7 2010/09/07 02:11:04 manu Exp $ */
2 
3 /*-
4  *  Copyright (c) 2010 Emmanuel Dreyfus. All rights reserved.
5  *
6  *  Redistribution and use in source and binary forms, with or without
7  *  modification, are permitted provided that the following conditions
8  *  are met:
9  *  1. Redistributions of source code must retain the above copyright
10  *     notice, this list of conditions and the following disclaimer.
11  *  2. Redistributions in binary form must reproduce the above copyright
12  *     notice, this list of conditions and the following disclaimer in the
13  *     documentation and/or other materials provided with the distribution.
14  *
15  *  THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
16  *  ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
17  *  TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18  *  PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
19  *  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  *  POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #ifndef _REFUSE_PERFUSE_H
29 #define _REFUSE_PERFUSE_H
30 
31 #ifndef _PATH_PERFUSED
32 #define _PATH_PERFUSED "/usr/sbin/perfused"
33 #endif /* _PATH_PERFUSED */
34 #define _PATH_FUSE "/dev/fuse"
35 #define FUSE_COMMFD_ENV "_FUSE_COMMFD"
36 #define PERFUSE_MOUNT_MAGIC "noFuseRq"
37 #define PERFUSE_UNKNOWN_INO 0xffffffff
38 
39 /*
40  * Diagnostic flags. This global is used only for DPRINTF/DERR/DWARN
41  */
42 extern int perfuse_diagflags;
43 #define PDF_FOREGROUND	0x001	/* we run in foreground */
44 #define PDF_FUSE	0x002	/* Display FUSE reqeusts and reply */
45 #define PDF_DUMP	0x004	/* Dump FUSE frames */
46 #define PDF_PUFFS	0x008	/* Display PUFFS requets and reply */
47 #define PDF_FH		0x010	/* File handles */
48 #define PDF_RECLAIM	0x020	/* Reclaimed files */
49 #define PDF_READDIR	0x040	/* readdir operations */
50 #define PDF_REQUEUE	0x080	/* reueued messages */
51 #define PDF_SYNC	0x100	/* fsync and dirty flags */
52 #define PDF_MISC	0x200	/* Miscelaneous messages */
53 #define PDF_SYSLOG	0x400	/* use syslog */
54 
55 /*
56  * Diagnostic functions
57  */
58 #define DPRINTF(fmt, ...) do {						\
59 	if (perfuse_diagflags & PDF_SYSLOG)				\
60 		syslog(LOG_INFO, fmt, ## __VA_ARGS__);			\
61 									\
62 	if (perfuse_diagflags & PDF_FOREGROUND)				\
63 		(void)printf(fmt, ## __VA_ARGS__);			\
64 } while (0 /* CONSTCOND */)
65 
66 #define DERRX(status, fmt, ...) do {					\
67 	if (perfuse_diagflags & PDF_SYSLOG)				\
68 		syslog(LOG_ERR, fmt, ## __VA_ARGS__);			\
69 									\
70 	if (perfuse_diagflags & PDF_FOREGROUND) {			\
71 		(void)fprintf(stderr,  fmt, ## __VA_ARGS__);		\
72 		abort();						\
73 	} else {							\
74 		errx(status, fmt, ## __VA_ARGS__);			\
75 	}								\
76 } while (0 /* CONSTCOND */)
77 
78 #define DERR(status, fmt, ...) do {					\
79 	char fmterr[BUFSIZ];						\
80 	char strerrbuf[BUFSIZ];						\
81 									\
82 	(void)strerror_r(errno, strerrbuf, sizeof(strerrbuf));		\
83 	(void)sprintf(fmterr, "%s: %s\n", fmt, strerrbuf);		\
84 									\
85 	if (perfuse_diagflags & PDF_SYSLOG)				\
86 		syslog(LOG_ERR, fmterr, ## __VA_ARGS__);		\
87 									\
88 	if (perfuse_diagflags & PDF_FOREGROUND) {			\
89 		(void)fprintf(stderr,  fmterr, ## __VA_ARGS__);		\
90 		abort();						\
91 	} else {							\
92 		errx(status, fmt, ## __VA_ARGS__);			\
93 	}								\
94 } while (0 /* CONSTCOND */)
95 
96 #define DWARNX(fmt, ...) do {						\
97 	if (perfuse_diagflags & PDF_SYSLOG)				\
98 		syslog(LOG_WARNING, fmt, ## __VA_ARGS__);		\
99 									\
100 	warnx(fmt, ## __VA_ARGS__);					\
101 } while (0 /* CONSTCOND */)
102 
103 #define DWARN(fmt, ...) do {						\
104 	char fmterr[BUFSIZ];						\
105 	char strerrbuf[BUFSIZ];						\
106 									\
107 	(void)strerror_r(errno, strerrbuf, sizeof(strerrbuf));		\
108 	(void)sprintf(fmterr, "%s: %s\n", fmt, strerrbuf);		\
109 									\
110 	if (perfuse_diagflags & PDF_SYSLOG)				\
111 		syslog(LOG_WARNING, fmterr, ## __VA_ARGS__);		\
112 									\
113 	warn(fmterr, ## __VA_ARGS__);					\
114 } while (0 /* CONSTCOND */)
115 
116 /*
117  * frame handling callbacks
118  */
119 #ifndef PEFUSE_MSG_T
120 #define PEFUSE_MSG_T struct perfuse_framebuf
121 #endif
122 typedef PEFUSE_MSG_T perfuse_msg_t;
123 
124 #define PERFUSE_UNSPEC_REPLY_LEN (size_t)-1
125 
126 enum perfuse_xchg_pb_reply { wait_reply, no_reply };
127 typedef perfuse_msg_t *(*perfuse_new_msg_fn)(struct puffs_usermount *,
128     puffs_cookie_t, int, size_t, const struct puffs_cred *);
129 typedef int (*perfuse_xchg_msg_fn)(struct puffs_usermount *,
130     perfuse_msg_t *, size_t, enum perfuse_xchg_pb_reply);
131 typedef void (*perfuse_destroy_msg_fn)(perfuse_msg_t *);
132 typedef struct fuse_out_header *(*perfuse_get_outhdr_fn)(perfuse_msg_t *);
133 typedef struct fuse_in_header *(*perfuse_get_inhdr_fn)(perfuse_msg_t *);
134 typedef char *(*perfuse_get_inpayload_fn)(perfuse_msg_t *);
135 typedef char *(*perfuse_get_outpayload_fn)(perfuse_msg_t *);
136 
137 struct perfuse_callbacks {
138 	perfuse_new_msg_fn pc_new_msg;
139 	perfuse_xchg_msg_fn pc_xchg_msg;
140 	perfuse_destroy_msg_fn pc_destroy_msg;
141 	perfuse_get_inhdr_fn pc_get_inhdr;
142 	perfuse_get_inpayload_fn pc_get_inpayload;
143 	perfuse_get_outhdr_fn pc_get_outhdr;
144 	perfuse_get_outpayload_fn pc_get_outpayload;
145 };
146 
147 /*
148  * mount request
149  */
150 struct perfuse_mount_out {
151         uint32_t pmo_len;
152         int32_t pmo_error;
153         uint64_t pmo_unique;
154 	char pmo_magic[sizeof(PERFUSE_MOUNT_MAGIC)];
155 	uint32_t pmo_source_len;
156 	uint32_t pmo_target_len;
157 	uint32_t pmo_filesystemtype_len;
158 	uint32_t pmo_mountflags;
159 	uint32_t pmo_data_len;
160 };
161 
162 struct perfuse_mount_info {
163 	const char *pmi_source;
164 	const char *pmi_target;
165 	const char *pmi_filesystemtype;
166 	int pmi_mountflags;
167 	void *pmi_data;
168 	uid_t pmi_uid;
169 };
170 
171 /*
172  * Duplicated fro fuse.h to avoid making it public
173  */
174 #define FUSE_MIN_BUFSIZE 0x21000
175 #define FUSE_PREF_BUFSIZE (PAGE_SIZE + 0x1000)
176 #define FUSE_BUFSIZE MAX(FUSE_PREF_BUFSIZE, FUSE_MIN_BUFSIZE)
177 
178 struct fuse_in_header {
179 	uint32_t	len;
180 	uint32_t	opcode;
181 	uint64_t	unique;
182 	uint64_t	nodeid;
183 	uint32_t	uid;
184 	uint32_t	gid;
185 	uint32_t	pid;
186 	uint32_t	padding;
187 };
188 
189 struct fuse_out_header {
190 	uint32_t	len;
191 	int32_t	error;
192 	uint64_t	unique;
193 };
194 
195 __BEGIN_DECLS
196 
197 struct puffs_usermount *perfuse_init(struct perfuse_callbacks *,
198     struct perfuse_mount_info *);
199 void perfuse_setspecific(struct puffs_usermount *, void *);
200 void *perfuse_getspecific(struct puffs_usermount *);
201 uint64_t perfuse_next_unique(struct puffs_usermount *);
202 uint64_t perfuse_get_ino(struct puffs_usermount *, puffs_cookie_t);
203 int perfuse_inloop(struct puffs_usermount *);
204 const char *perfuse_opname(int);
205 void perfuse_fs_init(struct puffs_usermount *);
206 int perfuse_mainloop(struct puffs_usermount *);
207 int perfuse_unmount(struct puffs_usermount *);
208 
209 #endif /* _REFUSE_PERFUSE_H */
210