summaryrefslogtreecommitdiff
path: root/kernel/fuse_queue.c
blob: 489d6bb9dda7681f3fe2992e0ef5ac8d83a40c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/kmem.h>
#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/mutex.h>
#include <sys/semaphore.h>
#include <sys/list.h>
#include <sys/uio.h>
#include <sys/avl.h>
#include <sys/vnode.h>
#include <sys/atomic.h>
#include <sys/sysmacros.h>
#include <sys/ksynch.h>
#include <sys/cred.h>
#include <sys/sdt.h>

#include "fuse_queue.h"

static int
fuse_avl_compare(const void *x1, const void *x2);
static void fuse_avl_destroy(avl_tree_t *tree_p);

static fuse_session_t *fuse_sessions[FUSE_MAX_SESSIONS];

static kmutex_t fuse_global_mutx;

void
fuse_global_init()
{
	(void) mutex_init(&fuse_global_mutx, NULL, MUTEX_DEFAULT,
	    (void *) NULL);
}
void
fuse_global_fini()
{
	mutex_destroy(&fuse_global_mutx);
}
static void
fuse_global_mutex_enter()
{
	(void) mutex_enter(&fuse_global_mutx);
}
static void
fuse_global_mutex_exit()
{
	(void) mutex_exit(&fuse_global_mutx);
}

/* ARGSUSED */
static void
frd_on_request_complete_wakeup(fuse_session_t *ses_p, fuse_msg_node_t *msg_p)
{
	FUSE_SESSION_MUTEX_LOCK(ses_p);
	msg_p->fmn_state = FUSE_MSG_STATE_DONE;
	cv_signal(&msg_p->fmn_cv);
	FUSE_SESSION_MUTEX_UNLOCK(ses_p);
}

fuse_session_t *
fuse_minor_get_session(minor_t ndx)
{
	return (ndx < FUSE_MAX_SESSIONS ?  fuse_sessions[ndx] : NULL);
}

minor_t
fuse_session_get_minor(fuse_session_t *se)
{
	return (se->minor);
}

void
fuse_session_set_cred(fuse_session_t *se, cred_t *cr)
{
	crhold(cr);
	se->usercred = cr;
}

static void
fuse_session_clear_cred(fuse_session_t *se)
{
	if (se->usercred != NULL)
		crfree(se->usercred);
	se->usercred = NULL;

}

void
fuse_session_set_vfs(fuse_session_t *se, vfs_t *vfs)
{
	se->vfs = vfs;
}

/* double a global lock before calling it */
void
fuse_init_session(fuse_session_t *se)
{
	(void) mutex_init(&se->session_mutx, NULL, MUTEX_DEFAULT, NULL);
	(void) sema_init(&se->session_sema, 0, NULL, SEMA_DRIVER, NULL);
	mutex_init(&se->avl_mutx, NULL, MUTEX_DEFAULT, NULL);
	list_create(&se->msg_list, sizeof (fuse_msg_node_t),
	    offsetof(fuse_msg_node_t, fmn_link));
	avl_create(&se->avl_cache, fuse_avl_compare,
	    sizeof (fuse_avl_cache_node_t),
	    offsetof(struct fuse_avl_cache_node, facn_cache_node));
	se->mounted = 0;
}

void
fuse_deinit_session(fuse_session_t *se)
{
	mutex_destroy(&se->avl_mutx);
	sema_destroy(&se->session_sema);
	mutex_destroy(&se->session_mutx);
	list_destroy(&se->msg_list);
	fuse_avl_destroy(&se->avl_cache);
	fuse_session_clear_cred(se);
}

void
fuse_free_session(fuse_session_t *se)
{
	fuse_sessions[fuse_session_get_minor(se)] = NULL;
	kmem_free(se, sizeof (fuse_session_t));
}

fuse_session_t *
fuse_alloc_session()
{
	int i;
	fuse_global_mutex_enter();

	for (i = 0; i < FUSE_MAX_SESSIONS; i++) {
		if (fuse_sessions[i] == NULL) {
			break;
		}
	}

	if (i == FUSE_MAX_SESSIONS) {
		fuse_global_mutex_exit();
		return (NULL);
	} else {
		fuse_sessions[i] = kmem_zalloc(sizeof (fuse_session_t),
		    KM_SLEEP);
		fuse_sessions[i]->minor = i;

		fuse_global_mutex_exit();
		return (fuse_sessions[i]);
	}
}

static void
fuse_init_msg(fuse_msg_node_t *msg_p)
{
	bzero(msg_p, sizeof (fuse_msg_node_t));
	sema_init(&msg_p->fmn_sema, 0, NULL, SEMA_DRIVER, (void *) NULL);
	cv_init(&msg_p->fmn_cv, NULL, CV_DEFAULT, NULL);
}
static void
fuse_deinit_msg(fuse_msg_node_t *msg_p)
{
	sema_destroy(&msg_p->fmn_sema);
	cv_destroy(&msg_p->fmn_cv);
}

fuse_msg_node_t *
fuse_alloc_msg()
{
	fuse_msg_node_t *msg_p = kmem_alloc(sizeof (fuse_msg_node_t), KM_SLEEP);
	fuse_init_msg(msg_p);
	return (msg_p);
}

void
fuse_free_msg(fuse_msg_node_t *msg_p)
{
	int i;
	fuse_deinit_msg(msg_p);
	/*
	 * Check and free if the buffer was allocated for exchange between fuse
	 * library and kernel module
	 */
	for (i = 0; i < msg_p->ipdata.iovs_used; i++) {
		if (msg_p->ipdata.iovbuf[i].memsize &&
		    msg_p->ipdata.iovbuf[i].memflag == MEM_TYPE_KMEM) {
			kmem_free(msg_p->ipdata.iovbuf[i].base,
			    msg_p->ipdata.iovbuf[i].memsize);
		}
	}

	/* Check if memory was allocated to receive fuse library response */
	if (msg_p->opdata.fouth)
		kmem_free(msg_p->opdata.fouth, sizeof (*(msg_p->opdata.fouth)));

	if (msg_p->opdata.iovbuf.memsize &&
	    msg_p->opdata.iovbuf.memflag == MEM_TYPE_KMEM)
		kmem_free(msg_p->opdata.iovbuf.base,
		    msg_p->opdata.iovbuf.memsize);

	kmem_free(msg_p, sizeof (fuse_msg_node_t));
}

void
fuse_queue_request_nowait(fuse_session_t *se, fuse_msg_node_t *req_p)
{
	FUSE_SESSION_MUTEX_LOCK(se);
	req_p->fmn_state = FUSE_MSG_STATE_QUEUE;
	/* the device write call removes the message from the queue */
	list_insert_tail(&(se->msg_list), req_p);
	FUSE_SESSION_MUTEX_UNLOCK(se);
	/* wake up the reader @ device */
	sema_v(&(se->session_sema));
}

int
fuse_queue_request_wait(fuse_session_t *se, fuse_msg_node_t *req_p)
{
	int err = 0;
	int interrupted = 0;

	req_p->frd_on_request_complete = frd_on_request_complete_wakeup;
	fuse_queue_request_nowait(se, req_p);

	FUSE_SESSION_MUTEX_LOCK(se);

	while (req_p->fmn_state != FUSE_MSG_STATE_DONE) {
		if (cv_wait_sig(&req_p->fmn_cv, &se->session_mutx) != 0) {
			continue;
		} else {
			interrupted = 1;
			break;
		}
	}
	if (interrupted == 0) {
		req_p->opdata.outdata = req_p->opdata.iovbuf.base;
		FUSE_SESSION_MUTEX_UNLOCK(se);
		return (err);
	}

	DTRACE_PROBE3(fuse_queue_request_wait_err_no_response,
	    char *, "no response from daemon",
	    fuse_session_t *, se, fuse_msg_node_t *, req_p);

	if (req_p->fmn_state == FUSE_MSG_STATE_DONE) {
		goto err;
	}
	if (req_p->fmn_state != FUSE_MSG_STATE_QUEUE)
		req_p->fmn_state = FUSE_MSG_STATE_SIG;

	while (req_p->fmn_state != FUSE_MSG_STATE_DONE)
		cv_wait(&req_p->fmn_cv, &se->session_mutx);
err:
	req_p->opdata.outdata = NULL;
	err = EINTR;
	FUSE_SESSION_MUTEX_UNLOCK(se);

	return (err);
}
/*
 *  Avl related
 */
static void
fuse_avl_destroy(avl_tree_t *tree_p)
{
	void *cookie = NULL;
	fuse_avl_cache_node_t *node;
	while ((node = avl_destroy_nodes(tree_p, &cookie)) != NULL) {
		fuse_avl_cache_node_destroy(node);
	}
	avl_destroy(tree_p);
}

fuse_avl_cache_node_t *
fuse_avl_cache_node_create(vnode_t *np, uint64_t nodeid, uint64_t par_nodeid,
    unsigned short namelen, char *name)
{
	fuse_avl_cache_node_t *nod = kmem_zalloc(
	    sizeof (fuse_avl_cache_node_t), KM_SLEEP);
	nod->facn_vnode_p = np;
	nod->facn_nodeid = nodeid;
	nod->par_nodeid = par_nodeid;

	if (namelen) {
		nod->namelen = namelen;
		nod->name = kmem_alloc(namelen, KM_SLEEP);
		(void) strlcpy(nod->name, name, namelen);
	}
	return (nod);
}

void
fuse_avl_cache_node_destroy(fuse_avl_cache_node_t *node)
{
	if (node->namelen)
		kmem_free(node->name, node->namelen);
	kmem_free(node, sizeof (fuse_avl_cache_node_t));
}

static int
fuse_avl_compare(const void *x1, const void *x2)
{
	fuse_avl_cache_node_t *new = (fuse_avl_cache_node_t *)x1;
	fuse_avl_cache_node_t *old = (fuse_avl_cache_node_t *)x2;

	/*
	 * We first check if valid nodeid is passed, if not we try to
	 * validate with the remaining fields of a node
	 */
	if (new->facn_nodeid == FUSE_NULL_ID) {
		/*
		 * JPA This is awfully wrong : the tree is based on
		 * nodeid's, it cannot help if the nodeid's are missing.
		 * A second tree would be needed. For now, avoid this
		 * situation and do a full scan instead.
		 */
		if (new->namelen == old->namelen &&
		    new->par_nodeid == old->par_nodeid) {
			/* Compare the names for a match */
			if (!(strncmp(new->name, old->name, new->namelen)))
				return (0);
		}
	} else {
		return ((new->facn_nodeid == old->facn_nodeid) ? 0 :
		    ((new->facn_nodeid < old->facn_nodeid) ? -1 : 1));
	}
	return (-1);
}


void
fuse_session_umount(fuse_session_t *sep)
{
	if (sep->vfs) {
		if (vn_vfswlock(sep->vfs->vfs_vnodecovered) == 0) {
			VFS_RELE(sep->vfs);
			(void) dounmount(sep->vfs, 0, sep->usercred);
		} else {
			VFS_RELE(sep->vfs);
		}
	}
}