File: openfile_netio.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (319 lines) | stat: -rw-r--r-- 7,589 bytes parent folder | download
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
/*
 * Copyright (c) 1997-1998 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
/*
 * Generic oskit_openfile adapter for netio devices
 * in an oskit device environment.
 */
#include <oskit/dev/dev.h>

#include <oskit/fs/openfile.h>
#include <oskit/io/netio.h>
#include <oskit/com/queue.h>
#include <oskit/error.h>
#include <malloc.h>
#include <stddef.h>
#include <string.h>

struct openfile {
	oskit_openfile_t 	ofi;
	oskit_u32_t 		count;
	oskit_oflags_t		flags;
	oskit_netio_t		*send_nio;
	oskit_netio_t		*recv_nio;
	oskit_file_t		*file;
	oskit_queue_t		*queue;
	oskit_listener_t	*notify_on_dump;
	osenv_sleeprec_t		sr;
};

#if 0
#define DMARK osenv_log(OSENV_LOG_DEBUG, __FILE__ ":%d: " __FUNCTION__ "\n", __LINE__)
#else
#define DMARK ((void)0)
#endif

static oskit_error_t
openfile_query(oskit_openfile_t *f,
	       const oskit_iid_t *iid, void **out_ihandle)
{
	struct openfile *of = (void *) f;
	osenv_assert(of->count);

        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_stream_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_openfile_iid, sizeof(*iid)) == 0) {
                *out_ihandle = of;
                ++of->count;
                return 0;
        }

        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
}

static OSKIT_COMDECL_U 
openfile_addref(oskit_openfile_t *f)
{
	struct openfile *of = (void *) f;
	osenv_assert (of->count);
	return ++of->count;
}

static oskit_u32_t
openfile_release(oskit_openfile_t *f)
{
	struct openfile *of = (void *) f;

	osenv_assert(of->count);

	if (--of->count)
		return of->count;

	oskit_queue_release(of->queue);
	if (of->file)
		oskit_file_release(of->file);
	if (of->notify_on_dump)
		oskit_listener_addref(of->notify_on_dump);
	osenv_mem_free(of, 0, sizeof *of);
	return 0;
}


/*** Operations inherited from oskit_stream interface ***/

static OSKIT_COMDECL
openfile_read(oskit_openfile_t *f, void *buf, oskit_u32_t len,
	      oskit_u32_t *out_actual)
{
	struct openfile *of = (void *) f;
	oskit_bufio_t	*p;
	oskit_error_t   rc;
	DMARK;

	if ((of->flags & OSKIT_O_ACCMODE) == OSKIT_O_WRONLY)
		return OSKIT_EBADF;

	/* XXX: handle "O_NONBLOCK" here sometime ... */

	/* wait on a condition variable... */
	osenv_intr_disable();
	while (oskit_queue_size(of->queue) == 0) {
		osenv_sleep_init(&of->sr);
		osenv_intr_enable();
		osenv_sleep(&of->sr);
		osenv_intr_disable();
	}

	rc = oskit_queue_dequeue(of->queue, &p);
	osenv_assert(rc == sizeof p);

	osenv_intr_enable();

	/* copy it out and release it */
	rc = oskit_bufio_read(p, buf, 0, len, out_actual);
	oskit_bufio_release(p);

	return rc;
}

static OSKIT_COMDECL
openfile_write(oskit_openfile_t *f, const void *buf,
	       oskit_u32_t len, oskit_u32_t *out_actual)
{
	struct openfile *of = (void *) f;
	oskit_error_t rc;
	oskit_bufio_t *b;

	if ((of->flags & OSKIT_O_ACCMODE) == OSKIT_O_RDONLY)
		return OSKIT_EBADF;

	b = oskit_bufio_create(len);
	rc = oskit_bufio_write(b, buf, 0, len, out_actual);
	if (!rc) 
		rc = oskit_netio_push(of->send_nio, b, len);
	oskit_bufio_release(b);
	return rc;
}

static OSKIT_COMDECL
openfile_seek(oskit_openfile_t *f, oskit_s64_t offset,
	      oskit_seek_t whence, oskit_u64_t *out_newpos)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_setsize(oskit_openfile_t *f, oskit_u64_t new_size)
{
	return OSKIT_E_NOTIMPL;
}


static OSKIT_COMDECL
openfile_copy_to(oskit_openfile_t *f, oskit_stream_t *dst, oskit_u64_t size,
		 oskit_u64_t *out_read, oskit_u64_t *out_written)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_commit(oskit_openfile_t *f, oskit_u32_t commit_flags)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_revert(oskit_openfile_t *f)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_lock_region(oskit_openfile_t *f, oskit_u64_t offset, 
		     oskit_u64_t size, oskit_u32_t lock_type)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_unlock_region(oskit_openfile_t *f, oskit_u64_t offset, 
		       oskit_u64_t size, oskit_u32_t lock_type)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_stat(oskit_openfile_t *f, oskit_stream_stat_t *out_stat,
	      oskit_u32_t stat_flags)
{
	return OSKIT_E_NOTIMPL;
}

static OSKIT_COMDECL
openfile_clone(oskit_openfile_t *f, oskit_openfile_t **out_stream)
{
	return OSKIT_E_NOTIMPL;
}

/*** Operations specific to oskit_openfile ***/

static OSKIT_COMDECL
openfile_getfile(oskit_openfile_t *f, struct oskit_file **out_file)
{
	struct openfile *of = (void *) f;

	if (!of->file)
		return OSKIT_E_NOTIMPL;

	oskit_file_addref(of->file);
	*out_file = of->file;
	return 0;
}

static struct oskit_openfile_ops openfile_ops = {
    openfile_query,
    openfile_addref,
    openfile_release,
    openfile_read,
    openfile_write,
    openfile_seek,
    openfile_setsize,
    openfile_copy_to,
    openfile_commit,
    openfile_revert,
    openfile_lock_region,
    openfile_unlock_region,
    openfile_stat,
    openfile_clone,
    openfile_getfile
};

/*
 * I think we can ignore pkt_size
 */
static oskit_error_t 
net_receive(void *data, oskit_bufio_t *b, oskit_size_t pkt_size)
{
	struct openfile *of = data;
	oskit_error_t rc = oskit_queue_enqueue(of->queue, &b, sizeof b);

	if (rc) {
		if (of->notify_on_dump)
			oskit_listener_notify(of->notify_on_dump, 
				(oskit_iunknown_t *)b);
	} else {
		oskit_bufio_addref(b);
		osenv_wakeup(&of->sr, OSENV_SLEEP_WAKEUP);
	}
	return 0;
}

/*
 * Sole user entry point for this module.
 *
 * Please refer to oskit/dev/soa.h for more explanation.
 *
 * The style in oskit/fs/soa.h would have called this
 * oskit_soa_openfile_from_netio, but we look to the future.
 */
OSKIT_COMDECL 
create_openfile_from_netio(oskit_netio_t *(*exchange)(void *, oskit_netio_t *f),
		void *exchange_arg,
	        int queuelength,
		oskit_oflags_t flags,
		oskit_listener_t        *notify_on_dump,
		struct oskit_file	*file,
		struct oskit_openfile **out_openfile)
{
	struct openfile *of;

	DMARK;
	if (flags &~ (OSKIT_O_ACCMODE|OSKIT_O_APPEND))
		return OSKIT_ENOTSUP;

	DMARK;
	of = osenv_mem_alloc (sizeof *of, 0, 0);
	if (! of) {
		return OSKIT_ENOMEM;
	}
	memset(of, 0, sizeof *of);

	of->queue = create_bounded_queue_with_fixed_size_items(
		queuelength, 
		sizeof (oskit_bufio_t *), 
		0, /* don't need notifications after all */
		0  /* don't drop last */);

	if (!of->queue) {
		osenv_mem_free (of, 0, sizeof *of);
		return OSKIT_ENOMEM;
	}

	of->ofi.ops = &openfile_ops;
	of->count = 1;
	of->flags = flags;
	if ((of->file = file) != NULL)
		oskit_file_addref(file);

	if ((of->notify_on_dump = notify_on_dump) != NULL)
		oskit_listener_addref(notify_on_dump);

	of->recv_nio = oskit_netio_create(net_receive, of);
	of->send_nio = exchange(exchange_arg, of->recv_nio);
	*out_openfile = &of->ofi;
	return 0;
}