File: flatd.c

package info (click to toggle)
lam 7.1.4-8
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 56,404 kB
  • sloc: ansic: 156,541; sh: 9,991; cpp: 7,699; makefile: 5,621; perl: 488; fortran: 260; asm: 83
file content (405 lines) | stat: -rw-r--r-- 9,013 bytes parent folder | download | duplicates (11)
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (c) 2001-2003 The Trustees of Indiana University.  
 *                         All rights reserved.
 * Copyright (c) 1998-2001 University of Notre Dame. 
 *                         All rights reserved.
 * Copyright (c) 1994-1998 The Ohio State University.  
 *                         All rights reserved.
 * 
 * This file is part of the LAM/MPI software package.  For license
 * information, see the LICENSE file in the top level directory of the
 * LAM/MPI source distribution.
 * 
 * $HEADER$
 *
 * $Id: flatd.c,v 6.10 2003/05/21 19:16:14 jsquyres Exp $
 * 
 *	Function:	- OTB flat memory server
 *			- provides symbolic access to memory
 *			- "flattens" memory in a distributed system in
 *			  order to support broadcasting operations
 *			- memory blocks emulated as temporary files
 */
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/param.h>

#include <events.h>
#include <flatreq.h>
#include <net.h>
#include <portable.h>
#include <preq.h>
#include <priority.h>
#include <terror.h>
#include <lamdebug.h>
#include <laminternal.h>
#include <etc_misc.h>

/*
 * global functions
 */
void			(*(fl_init()))();
void			(*(flatd()))();
void flwipe(void);
char *flforget(int4 tag);

/*
 * local functions
 */
static void		flqload(void);
static void		flqget(void);
static void		flqclean(void);

/*
 * static variables
 */
static int		flcount;	/* total # of created files */

static struct flreq	*request;	/* request from client */
static struct flreply	*reply;		/* reply to client */

static struct nmsg	nhq;		/* request desc. from client */
static struct nmsg	nhr;		/* reply desc. to client */

static char		buf[MAXNMSGLEN];/* input data buffer for request */
static struct flregion	fltable[FLMAX];	/* flat address table */

/*
 *	fl_init
 *
 *	Function:	- flatd initialization
 */
void (*(
fl_init()))()
{
	int		i;			/* index */

	flcount = 0;
	request = (struct flreq *) nhq.nh_data;
	reply = (struct flreply *) nhr.nh_data;

	nhr.nh_flags = NREEL;
	nhr.nh_type = 0;

	_lam_atexit(flwipe);
/*
 * Initialize table.
 */
	for (i = 0; i < FLMAX; ++i) {
		fltable[i].fl_addr = EMPTY;
	}
/*
 * Attach to kernel.
 */
	if (lpattach("flatd")) lampanic("flatd (lpattach)");
/*
 * Receive first request.
 */
	nhq.nh_event = EVFLATD;
	nhq.nh_type = 0;
	nhq.nh_flags = 0;
	nhq.nh_length = MAXNMSGLEN;
	nhq.nh_msg = buf;

	if (nrecv(&nhq)) lampanic("flatd (nrecv)");

	return((void (*)()) flatd);
}

/*
 *	flatd
 *
 *	Function:	- server loop
 *			- replies message & receives next request
 */
void
(*(flatd()))()
{
	nhr.nh_node = request->flq_src_node;
	nhr.nh_event = request->flq_src_event;

	switch(request->flq_req) {

		case FLQLOAD:
		flqload();
		break;

		case FLQGET:
		case FLQFORGET:
		flqget();
		break;

		case FLQCLEAN:
		flqclean();
		break;
	}

	nhq.nh_event = EVFLATD;
	nhq.nh_type = 0;
	nhq.nh_flags = 0;
	nhq.nh_length = MAXNMSGLEN;
	nhq.nh_msg = buf;

	if (nrecv(&nhq)) lampanic("flatd (nrecv)");

	return((void (*)()) flatd);
}

/*
 *	flqload
 *
 *	Function:	- loads a file and mark it with a tag
 *			- creates file in temporary directory
 */
static void
flqload(void)
{
	int		fd;			/* load file descriptor */
	int		i;			/* favourite index */
	char		*buffer;		/* formatting buffer */
	
	nhr.nh_msg = (char *) 0;
	nhr.nh_length = 0;
/*
 * Search for an existing request.
 */
	for (i = 0; (i < FLMAX); ++i) {

		if ((fltable[i].fl_addr != EMPTY) &&
			(fltable[i].fl_tag == request->flq_tag) &&
			(fltable[i].fl_src_node == request->flq_src_node) &&
			(fltable[i].fl_src_event == request->flq_src_event)) {

			break;
		}
	}
/*
 * If it is a new request, we must create an entry and send an ACK.
 */
	if (i >= FLMAX) {

		for (i = 0; (i < FLMAX) && (fltable[i].fl_addr != EMPTY); ++i);

		if (i >= FLMAX) {
			reply->flr_reply = ENOFLDESCRIPTORS;
			if (nsend(&nhr)) lampanic("flatd (nsend)");
			return;
		}
/*
 * Make sure we can open the file before the user starts pumping data our way.
 */
		fltable[i].fl_tag = request->flq_tag;
		fltable[i].fl_src_node = request->flq_src_node;
		fltable[i].fl_src_event = request->flq_src_event;
		fltable[i].fl_byteswritten = 0;
		fltable[i].fl_status = 0;
/*
 * Create the temporary filename.
 */
		fltable[i].fl_addr = malloc(MAXPATHLEN);

		if (fltable[i].fl_addr == 0) {
			fltable[i].fl_addr = EMPTY;
			reply->flr_reply = errno;
			if (nsend(&nhr)) lampanic("flatd (nsend)");
			return;
		}

		if ((buffer = lam_get_tmpdir()) == 0) {
			free(fltable[i].fl_addr);
			fltable[i].fl_addr = EMPTY;
			reply->flr_reply = errno;
			if (nsend(&nhr)) lampanic("flatd (nsend)");
			return;
		}

		snprintf(fltable[i].fl_addr, MAXPATHLEN - 1, 
			 "%s/lam-flatd%d", buffer, flcount++);
		free(buffer);

		fd = open(fltable[i].fl_addr,
			  O_WRONLY | O_CREAT | O_APPEND, 0700);

		/* If the LAM session directory
		   (/tmp/lam-user@hostname) is on a networked
		   filesystem, O_APPEND may fail (e.g., Linux/NFS --
		   see open(2) on linux and search for "NFS").  So if
		   we failed and got errno == EIO, give it another
		   college try without the O_APPEND.  Be sure to
		   unlink it first, because Linux/NFS will create a
		   [broken] file entry in the directory, even if the
		   open failed. <sigh> */

		if (fd < 0 && errno == EIO) {
		  unlink(fltable[i].fl_addr);
		  fd = open(fltable[i].fl_addr, O_WRONLY | O_CREAT, 0700);
		}

		if (fd < 0) {
			reply->flr_reply = errno;
                        lamlog("flatd: flqload - failed to create file %s",
			       fltable[i].fl_addr);
			lamlog("flatd: flqload - errno %d", errno);

			/* On some stupid systems (e.g., Linux/NFS) a
                           file may be created even though the open
                           failed.  So be sure to unlink it (and
                           ignore if unlink fails).  <sigh> */

			unlink(fltable[i].fl_addr);
			fltable[i].fl_addr = EMPTY;
		} else {
			reply->flr_reply = 0;
			close(fd);
			lamlog("flatd: flqload - successfully created file %s",
			       fltable[i].fl_addr);
			lamlog("flatd: flqload - file descriptor %d", fd);
		}

		if (nsend(&nhr)) lampanic("flatd (nsend)");
		return;
	}
/*
 * If we get this far, then it was an existing request which must be continued.
 */
	if (fltable[i].fl_status == 0) {
		fd = open(fltable[i].fl_addr, O_WRONLY | O_APPEND, 0700);

		if (fd < 0) {
			reply->flr_reply = errno;
			fltable[i].fl_status = errno;
		}
/*
 * Write this chunk of data.
 */
		else if (write(fd, buf, nhq.nh_length) < nhq.nh_length) {
			fltable[i].fl_status = errno;
			close(fd);
			lamlog("flatd: flqload - calling unlink on \"%s\"", 
			       fltable[i].fl_addr);
			unlink(fltable[i].fl_addr);
		}
/*
 * Close even if this isn't the end of the file to conserve descriptors.
 */
		lamlog("flatd: flqload - successfully appended %d bytes to %s",
		       nhq.nh_length, fltable[i].fl_addr);
		close(fd);
	}
		
	fltable[i].fl_byteswritten += nhq.nh_length;

	if (fltable[i].fl_byteswritten == request->flq_ldlength) {
		reply->flr_reply = fltable[i].fl_status;
		if (nsend(&nhr)) lampanic("flatd (nsend)");
	}
}

/*
 *	flqget
 *
 *	Function:	- finds the address associated with a given tag
 *			- FLQFORGET also cleans out the fltable entry
 *			  associated with the tag
 */
static void
flqget(void)
{
	int		i;		/* favourite index */

	for (i = 0; (i < FLMAX) && ((fltable[i].fl_tag != request->flq_tag)
		|| (fltable[i].fl_addr == EMPTY)); ++i);

	if (i >= FLMAX) {
		reply->flr_reply = EBADTAG;
	} else {
		reply->flr_reply = 0;
		nhr.nh_msg = fltable[i].fl_addr;
		nhr.nh_length = FLPATHLEN;
	}

	if (nsend(&nhr)) lampanic("flatd (nsend)");

	if (request->flq_req == FLQFORGET) {
		free(fltable[i].fl_addr);
		fltable[i].fl_addr = EMPTY;
	}
}

/*
 *	flqclean
 *
 *	Function:	- removes tag and associated file
 */
static void
flqclean(void)
{
	int		i;

	for (i = 0; (i < FLMAX) && ((fltable[i].fl_tag != request->flq_tag)
			|| (fltable[i].fl_addr == EMPTY)); ++i);

	if (i >= FLMAX) {
		reply->flr_reply = EBADTAG;
	} else {
		reply->flr_reply = 0;
		lamlog("flatd: flqclean - calling unlink on \"%s\"", 
		       fltable[i].fl_addr);
		unlink(fltable[i].fl_addr);
		fltable[i].fl_addr = EMPTY;
	}

	if (nsend(&nhr)) lampanic("flatd (nsend)");
}

/*
 *	flforget
 *
 *	Function:	- internal process access to FLQFORGET service
 *	Accepts:	- flat tag
 *	Returns:	- local address
 */
char *
flforget(int4 tag)
{
	int		i;
	char		*temp;

	for (i = 0; (i < FLMAX) && ((fltable[i].fl_tag != tag)
			|| (fltable[i].fl_addr == EMPTY)); ++i);

	if (i >= FLMAX) {
		errno = EBADTAG;
		return(0);
	} else {
		temp = fltable[i].fl_addr;
		fltable[i].fl_addr = EMPTY;
		return(temp);
	}
}

/*
 *	flwipe
 *
 *	Function:	- clean up everything
 *			- should become a request someday
 */
void
flwipe(void)
{
	int		i;

	for (i = 0; i < FLMAX; ++i) {

		if (fltable[i].fl_addr != EMPTY) {
		  lamlog("flatd: flqwipe - calling unlink on \"%s\"", 
			 fltable[i].fl_addr);
		  unlink(fltable[i].fl_addr);
		  fltable[i].fl_addr = EMPTY;
		}
	}
}