File: nfs_dispatch.c

package info (click to toggle)
netstd 3.07-2hamm.5
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 6,384 kB
  • ctags: 9,087
  • sloc: ansic: 72,547; cpp: 6,141; makefile: 1,681; yacc: 1,615; sh: 1,220; perl: 303; awk: 46
file content (320 lines) | stat: -rw-r--r-- 8,025 bytes parent folder | download | duplicates (2)
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
/*
 * dispatch.c	This file contains the function dispatch table.
 *
 * Authors:	Donald J. Becker, <becker@super.org>
 *		Rick Sladkey, <jrs@world.std.com>
 *		Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
 *		Olaf Kirch, <okir@monad.swb.de>
 *
 *		This software may be used for any purpose provided
 *		the above copyright notice is retained.  It is supplied
 *		as is, with no warranty expressed or implied.
 */

#include "nfsd.h"
#include "nfs_prot.h"
#include "rpcmisc.h"

/*
 * These are the global variables that hold all argument and result data.
 */
union argument_types 	argument;
union result_types	result;

/*
 * The time at which we received the request.
 * Useful for various book-keeping things
 */
time_t			nfs_dispatch_time;

/*
 * This is a dispatch table to simplify error checking,
 * and supply return attributes for NFS functions.
 */

#ifdef __STDC__
#define CONCAT(a,b)	a##b
#define CONCAT3(a,b,c)	a##b##c
#define STRING(a)	#a
#else
#define CONCAT(a,b)	a/**/b
#define CONCAT3(a,b,c)	a/**/b/**/c
#define STRING(a)	"a"
#endif

#define table_ent(res_type, arg_type, funct) {			\
	sizeof(res_type), sizeof(arg_type),			\
	(xdrproc_t) CONCAT(xdr_,res_type),			\
	(xdrproc_t) CONCAT(xdr_,arg_type),			\
	(int (*)()) CONCAT3(nfsd_nfsproc_,funct,_2),		\
	STRING(funct), CONCAT(pr_,arg_type)			\
}

/* This definition makes the first table entry use xdr_char instead of
 * xdr_void. The NULL procedure call over TCP fails for this reason
#define nil	char
 */
#define nil	void
#define xdr_nil	xdr_void
#define pr_nil	pr_void
#define pr_char	pr_void

struct dispatch_entry {
    int		res_size, arg_size;	/* sizeof the res/arg structs	*/
    xdrproc_t	xdr_result;
    xdrproc_t	xdr_argument;
    int		(*funct)();		/* function handler		*/
    char	*name;			/* name of function		*/
    char	*(*log_print)();	/* ptr to debug handler		*/
};

static char *pr_void(void);
static char *pr_nfs_fh(nfs_fh *argp);
static char *pr_sattrargs(sattrargs *argp);
static char *pr_diropargs(diropargs *argp);
static char *pr_readargs(readargs *argp);
static char *pr_writeargs(writeargs *argp);
static char *pr_createargs(createargs *argp);
static char *pr_renameargs(renameargs *argp);
static char *pr_linkargs(linkargs *argp);
static char *pr_symlinkargs(symlinkargs *argp);
static char *pr_readdirargs(readdirargs *argp);

static struct dispatch_entry dtable[] = {
	table_ent(nil,nil,null),			/* NULL */
	table_ent(attrstat,nfs_fh,getattr),		/* GETATTR */
	table_ent(attrstat,sattrargs,setattr),		/* SETATTR */
	table_ent(nil,nil,root),			/* ROOT */
	table_ent(diropres,diropargs,lookup),		/* LOOKUP */
	table_ent(readlinkres,nfs_fh,readlink),		/* READLINK */
	table_ent(readres,readargs,read),		/* READ */
	table_ent(nil,nil,writecache),			/* WRITECACHE */
	table_ent(attrstat,writeargs,write),		/* WRITE */
	table_ent(diropres,createargs,create),		/* CREATE */
	table_ent(nfsstat,diropargs,remove),		/* REMOVE */
	table_ent(nfsstat,renameargs,rename),		/* RENAME */
	table_ent(nfsstat,linkargs,link),		/* LINK */
	table_ent(nfsstat,symlinkargs,symlink),		/* SYMLINK */
	table_ent(diropres,createargs,mkdir),		/* MKDIR */
	table_ent(nfsstat,diropargs,rmdir),		/* RMDIR */
	table_ent(readdirres,readdirargs,readdir),	/* READDIR */
	table_ent(statfsres,nfs_fh,statfs),		/* STATFS */
};

#ifdef CALL_PROFILING
#define PATH_PROFILE	"/tmp/nfsd.profile"

static struct timeval	rtimes[18] = {
	{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
	{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0},
	{0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}
};
static int		calls[18] = {
	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};
static struct timeval	t0, t1;
#endif


/*
 * The main dispatch routine.
 */
void
nfs_dispatch(struct svc_req *rqstp, SVCXPRT *transp)
{
	unsigned int proc_index = rqstp->rq_proc;
	struct dispatch_entry *dent;

	_rpcsvcdirty = 1;

	if (proc_index >= (sizeof(dtable) / sizeof(dtable[0]))) {
		svcerr_noproc(transp);
		goto done;
	}
	dent = &dtable[proc_index];

#ifdef CALL_PROFILING
	gettimeofday(&t0, NULL);
#endif

	/* Initialize our variables for determining the attributes of
	 * the file system in nfsd.c */
	nfsclient = NULL;

	memset(&argument, 0, dent->arg_size);
	if (!svc_getargs(transp, (xdrproc_t) dent->xdr_argument, &argument)) {
		svcerr_decode(transp);
		goto done;
	}
	/* Clear the result structure. */
	memset(&result, 0, dent->res_size);

	/* Log the call. The if() saves us a superfluous call to the debug
	 * functions, which is a big performance win.
	 */
	if (logging_enabled(D_CALL))
		log_call(rqstp, dent->name, dent->log_print(&argument));

	/* Do the function call itself. */
	nfs_dispatch_time = time(NULL);
	result.nfsstat = (*dent->funct) (&argument, rqstp);
	Dprintf(D_CALL, "result: %d\n", result.nfsstat);

#if 0
	if (!svc_sendreply(transp, dent->xdr_result, (caddr_t) &result)) {
		svcerr_systemerr(transp);
	}
#else
	svc_sendreply(transp, dent->xdr_result, (caddr_t) &result);
#endif

	if (!svc_freeargs(transp, (xdrproc_t) dent->xdr_argument, &argument)) {
		Dprintf(L_ERROR, "unable to free RPC arguments, exiting\n");
		exit(1);
	}
#ifdef CALL_PROFILING
	gettimeofday(&t1, NULL);

	if (t1.tv_usec < t0.tv_usec) {
		rtimes[proc_index].tv_sec += t1.tv_sec - t0.tv_sec - 1;
		rtimes[proc_index].tv_usec += 1000000 + t1.tv_usec - t0.tv_usec;
	} else {
		rtimes[proc_index].tv_sec += t1.tv_sec - t0.tv_sec;
		rtimes[proc_index].tv_usec += t1.tv_usec - t0.tv_usec;
	}
	calls[proc_index]++;
#endif

done:
	_rpcsvcdirty = 0;
	if (need_reinit) {
		reinitialize(0);
	}
	if (need_flush) {
		flush_cache(0);
	}
}

#ifdef CALL_PROFILING
void
dump_stats(int sig)
{
	FILE	*fp;
	int	i;

	if ((fp = fopen(PATH_PROFILE, "w")) == NULL) {
		Dprintf(L_WARNING, "unable to write profile data to %s\n",
				PATH_PROFILE);
		return;
	}

	for (i = 0; i < 18; i++) {
		float t;

		t = (float) rtimes[i].tv_sec +
		    (float) rtimes[i].tv_usec / 1000000.0;
		fprintf(fp, "%-20s\t%5d calls %8.4f sec avg\n",
				dtable[i].name, calls[i],
				(calls[i])?  t / calls[i] : 0);
		rtimes[i].tv_sec = rtimes[i].tv_usec = 0;
		calls[i] = 0;
	}

	fclose (fp);
}
#endif


/*
 * Functions for debugging output. This is still risky, because malformed
 * requests could overwrite our data segment.
 */
static char	printbuf[2048];

static char *
pr_void(void)
{
	return ("");
}

static char *
pr_nfs_fh(nfs_fh *argp)
{
	return (fh_pr(argp));
}

static char *
pr_sattrargs(sattrargs *argp)
{
	sprintf(printbuf, "fh:%s m:%0o u/g:%d/%d size:%d atime:%#x mtime:%#x",
		fh_pr(&argp->file), argp->attributes.mode,
		argp->attributes.uid, argp->attributes.gid,
		argp->attributes.size,
	 argp->attributes.atime.seconds, argp->attributes.mtime.seconds);
	return (printbuf);
}

static char *
pr_diropargs(diropargs *argp)
{
	sprintf(printbuf, "fh:%s n:%s", fh_pr(&(argp->dir)), argp->name);
	return (printbuf);
}

static char *
pr_readargs(readargs *argp)
{
	sprintf(printbuf, "%s: %d bytes at %d",
		fh_pr(&argp->file), argp->count, argp->offset);
	return (printbuf);
}

static char *
pr_writeargs(writeargs *argp)
{
	sprintf(printbuf, "%s: %d bytes at %d",
		fh_pr(&argp->file), argp->data.data_len, argp->offset);
	return (printbuf);
}

static char *
pr_createargs(createargs *argp)
{
	sprintf(printbuf, "fh:%s n:%s m:%0o u/g:%d/%d size:%d atime:%#x mtime:%#x",
		fh_pr(&argp->where.dir), argp->where.name,
		argp->attributes.mode, argp->attributes.uid,
		argp->attributes.gid, argp->attributes.size,
		argp->attributes.atime.seconds,
		argp->attributes.mtime.seconds);
	return (printbuf);
}

static char *
pr_renameargs(renameargs *argp)
{
	sprintf(printbuf, "fh:%s n:%s -> fh:%s n:%s",
		fh_pr(&argp->from.dir), argp->from.name,
		fh_pr(&argp->to.dir),   argp->to.name);
	return (printbuf);
}

static char *
pr_linkargs(linkargs *argp)
{
	sprintf(printbuf, "fh:%s -> fh:%s n:%s",
		fh_pr(&argp->from), fh_pr(&argp->to.dir), argp->to.name);
	return (printbuf);
}

static char *
pr_symlinkargs(symlinkargs *argp)
{
	return ("");
}

static char *
pr_readdirargs(readdirargs *argp)
{
	return (fh_pr(&argp->dir));
}