libfuse: lib/cuse_lowlevel.c Source File

libfuse

cuse_lowlevel.c
1/*
2 CUSE: Character device in Userspace
3 Copyright (C) 2008 SUSE Linux Products GmbH
4 Copyright (C) 2008 Tejun Heo <teheo@suse.de>
5
6 This program can be distributed under the terms of the GNU LGPLv2.
7 See the file COPYING.LIB.
8*/
9
10#include "fuse_config.h"
11#include "cuse_lowlevel.h"
12#include "fuse_kernel.h"
13#include "fuse_i.h"
14#include "fuse_opt.h"
15
16#include <stdio.h>
17#include <string.h>
18#include <stdlib.h>
19#include <stddef.h>
20#include <errno.h>
21#include <unistd.h>
22
23struct cuse_data {
24 struct cuse_lowlevel_ops clop;
25 unsigned max_read;
26 unsigned dev_major;
27 unsigned dev_minor;
28 unsigned flags;
29 unsigned dev_info_len;
30 char dev_info[];
31};
32
33static struct cuse_lowlevel_ops *req_clop(fuse_req_t req)
34{
35 return &req->se->cuse_data->clop;
36}
37
38static void cuse_fll_open(fuse_req_t req, fuse_ino_t ino,
39 struct fuse_file_info *fi)
40{
41 (void)ino;
42 req_clop(req)->open(req, fi);
43}
44
45static void cuse_fll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
46 off_t off, struct fuse_file_info *fi)
47{
48 (void)ino;
49 req_clop(req)->read(req, size, off, fi);
50}
51
52static void cuse_fll_write(fuse_req_t req, fuse_ino_t ino, const char *buf,
53 size_t size, off_t off, struct fuse_file_info *fi)
54{
55 (void)ino;
56 req_clop(req)->write(req, buf, size, off, fi);
57}
58
59static void cuse_fll_flush(fuse_req_t req, fuse_ino_t ino,
60 struct fuse_file_info *fi)
61{
62 (void)ino;
63 req_clop(req)->flush(req, fi);
64}
65
66static void cuse_fll_release(fuse_req_t req, fuse_ino_t ino,
67 struct fuse_file_info *fi)
68{
69 (void)ino;
70 req_clop(req)->release(req, fi);
71}
72
73static void cuse_fll_fsync(fuse_req_t req, fuse_ino_t ino, int datasync,
74 struct fuse_file_info *fi)
75{
76 (void)ino;
77 req_clop(req)->fsync(req, datasync, fi);
78}
79
80static void cuse_fll_ioctl(fuse_req_t req, fuse_ino_t ino, unsigned int cmd, void *arg,
81 struct fuse_file_info *fi, unsigned int flags,
82 const void *in_buf, size_t in_bufsz, size_t out_bufsz)
83{
84 (void)ino;
85 req_clop(req)->ioctl(req, cmd, arg, fi, flags, in_buf, in_bufsz,
86 out_bufsz);
87}
88
89static void cuse_fll_poll(fuse_req_t req, fuse_ino_t ino,
90 struct fuse_file_info *fi, struct fuse_pollhandle *ph)
91{
92 (void)ino;
93 req_clop(req)->poll(req, fi, ph);
94}
95
96static size_t cuse_pack_info(int argc, const char **argv, char *buf)
97{
98 size_t size = 0;
99 int i;
100
101 for (i = 0; i < argc; i++) {
102 size_t len;
103
104 len = strlen(argv[i]) + 1;
105 size += len;
106 if (buf) {
107 memcpy(buf, argv[i], len);
108 buf += len;
109 }
110 }
111
112 return size;
113}
114
115static struct cuse_data *cuse_prep_data(const struct cuse_info *ci,
116 const struct cuse_lowlevel_ops *clop)
117{
118 struct cuse_data *cd;
119 size_t dev_info_len;
120
121 dev_info_len = cuse_pack_info(ci->dev_info_argc, ci->dev_info_argv,
122 NULL);
123
124 if (dev_info_len > CUSE_INIT_INFO_MAX) {
125 fuse_log(FUSE_LOG_ERR, "cuse: dev_info (%zu) too large, limit=%u\n",
126 dev_info_len, CUSE_INIT_INFO_MAX);
127 return NULL;
128 }
129
130 cd = calloc(1, sizeof(*cd) + dev_info_len);
131 if (!cd) {
132 fuse_log(FUSE_LOG_ERR, "cuse: failed to allocate cuse_data\n");
133 return NULL;
134 }
135
136 memcpy(&cd->clop, clop, sizeof(cd->clop));
137 cd->max_read = 131072;
138 cd->dev_major = ci->dev_major;
139 cd->dev_minor = ci->dev_minor;
140 cd->dev_info_len = dev_info_len;
141 cd->flags = ci->flags;
142 cuse_pack_info(ci->dev_info_argc, ci->dev_info_argv, cd->dev_info);
143
144 return cd;
145}
146
147struct fuse_session *cuse_lowlevel_new(struct fuse_args *args,
148 const struct cuse_info *ci,
149 const struct cuse_lowlevel_ops *clop,
150 void *userdata)
151{
152 struct fuse_lowlevel_ops lop;
153 struct cuse_data *cd;
154 struct fuse_session *se;
155
156 cd = cuse_prep_data(ci, clop);
157 if (!cd)
158 return NULL;
159
160 memset(&lop, 0, sizeof(lop));
161 lop.init = clop->init;
162 lop.destroy = clop->destroy;
163 lop.open = clop->open ? cuse_fll_open : NULL;
164 lop.read = clop->read ? cuse_fll_read : NULL;
165 lop.write = clop->write ? cuse_fll_write : NULL;
166 lop.flush = clop->flush ? cuse_fll_flush : NULL;
167 lop.release = clop->release ? cuse_fll_release : NULL;
168 lop.fsync = clop->fsync ? cuse_fll_fsync : NULL;
169 lop.ioctl = clop->ioctl ? cuse_fll_ioctl : NULL;
170 lop.poll = clop->poll ? cuse_fll_poll : NULL;
171
172 se = fuse_session_new(args, &lop, sizeof(lop), userdata);
173 if (!se) {
174 free(cd);
175 return NULL;
176 }
177 se->cuse_data = cd;
178
179 return se;
180}
181
182static int cuse_reply_init(fuse_req_t req, struct cuse_init_out *arg,
183 char *dev_info, unsigned dev_info_len)
184{
185 struct iovec iov[3];
186
187 iov[1].iov_base = arg;
188 iov[1].iov_len = sizeof(struct cuse_init_out);
189 iov[2].iov_base = dev_info;
190 iov[2].iov_len = dev_info_len;
191
192 return fuse_send_reply_iov_nofree(req, 0, iov, 3);
193}
194
195void cuse_lowlevel_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg)
196{
197 struct fuse_init_in *arg = (struct fuse_init_in *) inarg;
198 struct cuse_init_out outarg;
199 struct fuse_session *se = req->se;
200 struct cuse_data *cd = se->cuse_data;
201 size_t bufsize = se->bufsize;
202 struct cuse_lowlevel_ops *clop = req_clop(req);
203
204 (void) nodeid;
205 if (se->debug) {
206 fuse_log(FUSE_LOG_DEBUG, "CUSE_INIT: %u.%u\n", arg->major, arg->minor);
207 fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags);
208 }
209 se->conn.proto_major = arg->major;
210 se->conn.proto_minor = arg->minor;
211
212 /* XXX This is not right.*/
213 se->conn.capable_ext = 0;
214 se->conn.want_ext = 0;
215
216 if (arg->major < 7) {
217 fuse_log(FUSE_LOG_ERR, "cuse: unsupported protocol version: %u.%u\n",
218 arg->major, arg->minor);
219 fuse_reply_err(req, EPROTO);
220 return;
221 }
222
223 if (bufsize < FUSE_MIN_READ_BUFFER) {
224 fuse_log(FUSE_LOG_ERR, "cuse: warning: buffer size too small: %zu\n",
225 bufsize);
226 bufsize = FUSE_MIN_READ_BUFFER;
227 }
228
229 bufsize -= 4096;
230 if (bufsize < se->conn.max_write)
231 se->conn.max_write = bufsize;
232
233 se->got_init = 1;
234 if (se->op.init)
235 se->op.init(se->userdata, &se->conn);
236
237 memset(&outarg, 0, sizeof(outarg));
238 outarg.major = FUSE_KERNEL_VERSION;
239 outarg.minor = FUSE_KERNEL_MINOR_VERSION;
240 outarg.flags = cd->flags;
241 outarg.max_read = cd->max_read;
242 outarg.max_write = se->conn.max_write;
243 outarg.dev_major = cd->dev_major;
244 outarg.dev_minor = cd->dev_minor;
245
246 if (se->debug) {
247 fuse_log(FUSE_LOG_DEBUG, " CUSE_INIT: %u.%u\n",
248 outarg.major, outarg.minor);
249 fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags);
250 fuse_log(FUSE_LOG_DEBUG, " max_read=0x%08x\n", outarg.max_read);
251 fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write);
252 fuse_log(FUSE_LOG_DEBUG, " dev_major=%u\n", outarg.dev_major);
253 fuse_log(FUSE_LOG_DEBUG, " dev_minor=%u\n", outarg.dev_minor);
254 fuse_log(FUSE_LOG_DEBUG, " dev_info: %.*s\n", cd->dev_info_len,
255 cd->dev_info);
256 }
257
258 cuse_reply_init(req, &outarg, cd->dev_info, cd->dev_info_len);
259
260 if (clop->init_done)
261 clop->init_done(se->userdata);
262
263 fuse_free_req(req);
264}
265
266struct fuse_session *cuse_lowlevel_setup(int argc, char *argv[],
267 const struct cuse_info *ci,
268 const struct cuse_lowlevel_ops *clop,
269 int *multithreaded, void *userdata)
270{
271 const char *devname = "/dev/cuse";
272 static const struct fuse_opt kill_subtype_opts[] = {
273 FUSE_OPT_KEY("subtype=", FUSE_OPT_KEY_DISCARD),
274 FUSE_OPT_END
275 };
276 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
277 struct fuse_session *se;
278 struct fuse_cmdline_opts opts;
279 int fd;
280 int res;
281
282 if (fuse_parse_cmdline(&args, &opts) == -1)
283 return NULL;
284 *multithreaded = !opts.singlethread;
285
286 /* Remove subtype= option */
287 res = fuse_opt_parse(&args, NULL, kill_subtype_opts, NULL);
288 if (res == -1)
289 goto out1;
290
291 /*
292 * Make sure file descriptors 0, 1 and 2 are open, otherwise chaos
293 * would ensue.
294 */
295 do {
296 fd = open("/dev/null", O_RDWR);
297 if (fd > 2)
298 close(fd);
299 } while (fd >= 0 && fd <= 2);
300
301 se = cuse_lowlevel_new(&args, ci, clop, userdata);
302 if (se == NULL)
303 goto out1;
304
305 fd = open(devname, O_RDWR);
306 if (fd == -1) {
307 if (errno == ENODEV || errno == ENOENT)
308 fuse_log(FUSE_LOG_ERR, "cuse: device not found, try 'modprobe cuse' first\n");
309 else
310 fuse_log(FUSE_LOG_ERR, "cuse: failed to open %s: %s\n",
311 devname, strerror(errno));
312 goto err_se;
313 }
314 se->fd = fd;
315
316 res = fuse_set_signal_handlers(se);
317 if (res == -1)
318 goto err_se;
319
320 res = fuse_daemonize(opts.foreground);
321 if (res == -1)
322 goto err_sig;
323
324 fuse_opt_free_args(&args);
325 return se;
326
327err_sig:
328 fuse_remove_signal_handlers(se);
329err_se:
330 fuse_session_destroy(se);
331out1:
332 free(opts.mountpoint);
333 fuse_opt_free_args(&args);
334 return NULL;
335}
336
337void cuse_lowlevel_teardown(struct fuse_session *se)
338{
339 fuse_remove_signal_handlers(se);
340 fuse_session_destroy(se);
341}
342
343int cuse_lowlevel_main(int argc, char *argv[], const struct cuse_info *ci,
344 const struct cuse_lowlevel_ops *clop, void *userdata)
345{
346 struct fuse_session *se;
347 int multithreaded;
348 int res;
349
350 se = cuse_lowlevel_setup(argc, argv, ci, clop, &multithreaded,
351 userdata);
352 if (se == NULL)
353 return 1;
354
355 if (multithreaded) {
356 struct fuse_loop_config *config = fuse_loop_cfg_create();
357 res = fuse_session_loop_mt(se, config);
358 fuse_loop_cfg_destroy(config);
359 }
360 else
361 res = fuse_session_loop(se);
362
363 cuse_lowlevel_teardown(se);
364 if (res == -1)
365 return 1;
366
367 return 0;
368}
fuse_set_signal_handlers
int fuse_set_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:138
fuse_remove_signal_handlers
void fuse_remove_signal_handlers(struct fuse_session *se)
Definition fuse_signals.c:185
fuse_daemonize
int fuse_daemonize(int foreground)
Definition helper.c:253
fuse_log
void fuse_log(enum fuse_log_level level, const char *fmt,...)
Definition fuse_log.c:77
fuse_session_destroy
void fuse_session_destroy(struct fuse_session *se)
Definition fuse_lowlevel.c:3010
fuse_reply_err
int fuse_reply_err(fuse_req_t req, int err)
Definition fuse_lowlevel.c:331
fuse_req_t
struct fuse_req * fuse_req_t
Definition fuse_lowlevel.h:50
fuse_session_loop
int fuse_session_loop(struct fuse_session *se)
Definition fuse_loop.c:19
fuse_ino_t
uint64_t fuse_ino_t
Definition fuse_lowlevel.h:47
fuse_opt.h
fuse_opt_free_args
void fuse_opt_free_args(struct fuse_args *args)
Definition fuse_opt.c:34
FUSE_OPT_KEY
#define FUSE_OPT_KEY(templ, key)
Definition fuse_opt.h:98
fuse_opt_parse
int fuse_opt_parse(struct fuse_args *args, void *data, const struct fuse_opt opts[], fuse_opt_proc_t proc)
Definition fuse_opt.c:398
FUSE_OPT_KEY_DISCARD
#define FUSE_OPT_KEY_DISCARD
Definition fuse_opt.h:153
FUSE_ARGS_INIT
#define FUSE_ARGS_INIT(argc, argv)
Definition fuse_opt.h:123
FUSE_OPT_END
#define FUSE_OPT_END
Definition fuse_opt.h:104
fuse_args
Definition fuse_opt.h:109
fuse_args::argc
int argc
Definition fuse_opt.h:111
fuse_args::argv
char ** argv
Definition fuse_opt.h:114
fuse_cmdline_opts
Definition fuse_lowlevel.h:2003
fuse_file_info
Definition fuse_common.h:56
fuse_loop_config
Definition fuse_common.h:147
fuse_lowlevel_ops
Definition fuse_lowlevel.h:206
fuse_opt
Definition fuse_opt.h:77