Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * linux/fs/nfsd/nfsctl.c
 *
 * Syscall interface to knfsd.
 *
 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
 */

#include <linux/config.h>
#include <linux/module.h>

#include <linux/linkage.h>
#include <linux/time.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/fcntl.h>
#include <linux/net.h>
#include <linux/in.h>
#include <linux/unistd.h>
#include <linux/slab.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/pagemap.h>
#include <linux/init.h>

#include <linux/nfs.h>
#include <linux/sunrpc/svc.h>
#include <linux/nfsd/nfsd.h>
#include <linux/nfsd/cache.h>
#include <linux/nfsd/xdr.h>
#include <linux/nfsd/syscall.h>
#include <linux/nfsd/interface.h>

#include <asm/uaccess.h>

/*
 *	We have a single directory with 8 nodes in it.
 */
enum {
	NFSD_Root = 1,
	NFSD_Svc,
	NFSD_Add,
	NFSD_Del,
	NFSD_Export,
	NFSD_Unexport,
	NFSD_Getfd,
	NFSD_Getfs,
	NFSD_List,
};

/*
 * write() for these nodes.
 */
static ssize_t write_svc(struct file *file, const char *buf, size_t size);
static ssize_t write_add(struct file *file, const char *buf, size_t size);
static ssize_t write_del(struct file *file, const char *buf, size_t size);
static ssize_t write_export(struct file *file, const char *buf, size_t size);
static ssize_t write_unexport(struct file *file, const char *buf, size_t size);
static ssize_t write_getfd(struct file *file, const char *buf, size_t size);
static ssize_t write_getfs(struct file *file, const char *buf, size_t size);

static ssize_t (*write_op[])(struct file *, const char *, size_t) = {
	[NFSD_Svc] = write_svc,
	[NFSD_Add] = write_add,
	[NFSD_Del] = write_del,
	[NFSD_Export] = write_export,
	[NFSD_Unexport] = write_unexport,
	[NFSD_Getfd] = write_getfd,
	[NFSD_Getfs] = write_getfs,
};

static ssize_t fs_write(struct file *file, const char *buf, size_t size, loff_t *pos)
{
	ino_t ino =  file->f_dentry->d_inode->i_ino;
	if (ino >= sizeof(write_op)/sizeof(write_op[0]) || !write_op[ino])
		return -EINVAL;
	return write_op[ino](file, buf, size);
}

/*
 * read(), open() and release() for getfs and getfd (read/write ones).
 * IO on these is a simple transaction - you open() the file, write() to it
 * and that generates a (stored) response.  After that read() will simply
 * access that response.
 */

static ssize_t TA_read(struct file *file, char *buf, size_t size, loff_t *pos)
{
	if (!file->private_data)
		return 0;
	if (*pos >= file->f_dentry->d_inode->i_size)
		return 0;
	if (*pos + size > file->f_dentry->d_inode->i_size)
		size = file->f_dentry->d_inode->i_size - *pos;
	if (copy_to_user(buf, file->private_data + *pos, size))
		return -EFAULT;
	*pos += size;
	return size;
}

static int TA_open(struct inode *inode, struct file *file)
{
	file->private_data = NULL;
	return 0;
}

static int TA_release(struct inode *inode, struct file *file)
{
	void *p = file->private_data;
	file->private_data = NULL;
	kfree(p);
	return 0;
}

static struct file_operations writer_ops = {
	.write	= fs_write,
};

static struct file_operations reader_ops = {
	.write		= fs_write,
	.read		= TA_read,
	.open		= TA_open,
	.release	= TA_release,
};

extern struct seq_operations nfs_exports_op;
static int exports_open(struct inode *inode, struct file *file)
{
	int res;
	char *namebuf = kmalloc(PAGE_SIZE, GFP_KERNEL);
	if (namebuf == NULL)
		return -ENOMEM;

	res = seq_open(file, &nfs_exports_op);
	if (res)
		kfree(namebuf);
	else
		((struct seq_file *)file->private_data)->private = namebuf;

	return res;
}
static int exports_release(struct inode *inode, struct file *file)
{
	struct seq_file *m = (struct seq_file *)file->private_data;
	kfree(m->private);
	m->private = NULL;
	return seq_release(inode, file);
}

static struct file_operations exports_operations = {
	.open		= exports_open,
	.read		= seq_read,
	.llseek		= seq_lseek,
	.release	= exports_release,
};

/*
 *	Description of fs contents.
 */
static struct { char *name; struct file_operations *ops; int mode; } files[] = {
	[NFSD_Svc] = {"svc", &writer_ops, S_IWUSR},
	[NFSD_Add] = {"add", &writer_ops, S_IWUSR},
	[NFSD_Del] = {"del", &writer_ops, S_IWUSR},
	[NFSD_Export] = {"export", &writer_ops, S_IWUSR},
	[NFSD_Unexport] = {"unexport", &writer_ops, S_IWUSR},
	[NFSD_Getfd] = {"getfd", &reader_ops, S_IWUSR|S_IRUSR},
	[NFSD_Getfs] = {"getfs", &reader_ops, S_IWUSR|S_IRUSR},
	[NFSD_List] = {"exports", &exports_operations, S_IRUGO},
};

/*----------------------------------------------------------------------------*/
/*
 * payload - write methods
 */

static ssize_t write_svc(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_svc data;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	return nfsd_svc(data.svc_port, data.svc_nthreads);
}

static ssize_t write_add(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_client data;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	return exp_addclient(&data);
}

static ssize_t write_del(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_client data;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	return exp_delclient(&data);
}

static ssize_t write_export(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_export data;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	return exp_export(&data);
}

static ssize_t write_unexport(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_export data;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	return exp_unexport(&data);
}

static ssize_t write_getfs(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_fsparm data;
	struct sockaddr_in *sin;
	struct auth_domain *clp;
	int err = 0;
	struct knfsd_fh *res;

	if (file->private_data)
		return -EINVAL;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	if (data.gd_addr.sa_family != AF_INET)
		return -EPROTONOSUPPORT;
	sin = (struct sockaddr_in *)&data.gd_addr;
	if (data.gd_maxlen > NFS3_FHSIZE)
		data.gd_maxlen = NFS3_FHSIZE;
	res = kmalloc(sizeof(struct knfsd_fh), GFP_KERNEL);
	if (!res)
		return -ENOMEM;
	memset(res, 0, sizeof(struct knfsd_fh));
	exp_readlock();
	if (!(clp = auth_unix_lookup(sin->sin_addr)))
		err = -EPERM;
	else {
		err = exp_rootfh(clp, data.gd_path, res, data.gd_maxlen);
		auth_domain_put(clp);
	}
	exp_readunlock();

	down(&file->f_dentry->d_inode->i_sem);
	if (file->private_data)
		err = -EINVAL;
	if (err)
		kfree(res);
	else {
		file->f_dentry->d_inode->i_size = res->fh_size + (int)&((struct knfsd_fh*)0)->fh_base;
		file->private_data = res;
		err = sizeof(data);
	}
	up(&file->f_dentry->d_inode->i_sem);

	return err;
}

static ssize_t write_getfd(struct file *file, const char *buf, size_t size)
{
	struct nfsctl_fdparm data;
	struct sockaddr_in *sin;
	struct auth_domain *clp;
	int err = 0;
	struct knfsd_fh fh;
	char *res;

	if (file->private_data)
		return -EINVAL;
	if (size < sizeof(data))
		return -EINVAL;
	if (copy_from_user(&data, buf, size))
		return -EFAULT;
	if (data.gd_addr.sa_family != AF_INET)
		return -EPROTONOSUPPORT;
	if (data.gd_version < 2 || data.gd_version > NFSSVC_MAXVERS)
		return -EINVAL;
	res = kmalloc(NFS_FHSIZE, GFP_KERNEL);
	if (!res)
		return -ENOMEM;
	sin = (struct sockaddr_in *)&data.gd_addr;
	exp_readlock();
	if (!(clp = auth_unix_lookup(sin->sin_addr)))
		err = -EPERM;
	else {
		err = exp_rootfh(clp, data.gd_path, &fh, NFS_FHSIZE);
		auth_domain_put(clp);
	}
	exp_readunlock();

	down(&file->f_dentry->d_inode->i_sem);
	if (file->private_data)
		err = -EINVAL;
	if (!err && fh.fh_size > NFS_FHSIZE)
		err = -EINVAL;
	if (err)
		kfree(res);
	else {
		memset(res,0, NFS_FHSIZE);
		memcpy(res, &fh.fh_base, fh.fh_size);
		file->f_dentry->d_inode->i_size = NFS_FHSIZE;
		file->private_data = res;
		err = sizeof(data);
	}
	up(&file->f_dentry->d_inode->i_sem);

	return err;
}

/*----------------------------------------------------------------------------*/
/*
 *	populating the filesystem.
 */

static struct super_operations s_ops = {
	.statfs		= simple_statfs,
};

static int nfsd_fill_super(struct super_block * sb, void * data, int silent)
{
	struct inode *inode;
	struct dentry *root;
	struct dentry *dentry;
	int i;

	sb->s_blocksize = PAGE_CACHE_SIZE;
	sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
	sb->s_magic = 0x6e667364;
	sb->s_op = &s_ops;

	inode = new_inode(sb);
	if (!inode)
		return -ENOMEM;
	inode->i_mode = S_IFDIR | 0755;
	inode->i_uid = inode->i_gid = 0;
	inode->i_blksize = PAGE_CACHE_SIZE;
	inode->i_blocks = 0;
	inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
	inode->i_op = &simple_dir_inode_operations;
	inode->i_fop = &simple_dir_operations;
	root = d_alloc_root(inode);
	if (!root) {
		iput(inode);
		return -ENOMEM;
	}
	for (i = NFSD_Svc; i <= NFSD_List; i++) {
		struct qstr name;
		name.name = files[i].name;
		name.len = strlen(name.name);
		name.hash = full_name_hash(name.name, name.len);
		dentry = d_alloc(root, &name);
		if (!dentry)
			goto out;
		inode = new_inode(sb);
		if (!inode)
			goto out;
		inode->i_mode = S_IFREG | files[i].mode;
		inode->i_uid = inode->i_gid = 0;
		inode->i_blksize = PAGE_CACHE_SIZE;
		inode->i_blocks = 0;
		inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
		inode->i_fop = files[i].ops;
		inode->i_ino = i;
		d_add(dentry, inode);
	}
	sb->s_root = root;
	return 0;

out:
	d_genocide(root);
	dput(root);
	return -ENOMEM;
}

static struct super_block *nfsd_get_sb(struct file_system_type *fs_type,
	int flags, char *dev_name, void *data)
{
	return get_sb_single(fs_type, flags, data, nfsd_fill_super);
}

static struct file_system_type nfsd_fs_type = {
	.owner		= THIS_MODULE,
	.name		= "nfsd",
	.get_sb		= nfsd_get_sb,
	.kill_sb	= kill_litter_super,
};

static int __init init_nfsd(void)
{
	printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n");

	nfsd_stat_init();	/* Statistics */
	nfsd_cache_init();	/* RPC reply cache */
	nfsd_export_init();	/* Exports table */
	nfsd_lockd_init();	/* lockd->nfsd callbacks */
	if (proc_mkdir("fs/nfs", 0)) {
		struct proc_dir_entry *entry;
		entry = create_proc_entry("fs/nfs/exports", 0, NULL);
		if (entry)
			entry->proc_fops =  &exports_operations;
	}
	register_filesystem(&nfsd_fs_type);
	return 0;
}

static void __exit exit_nfsd(void)
{
	nfsd_export_shutdown();
	nfsd_cache_shutdown();
	remove_proc_entry("fs/nfs/exports", NULL);
	remove_proc_entry("fs/nfs", NULL);
	nfsd_stat_shutdown();
	nfsd_lockd_shutdown();
	unregister_filesystem(&nfsd_fs_type);
}

MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
MODULE_LICENSE("GPL");
module_init(init_nfsd)
module_exit(exit_nfsd)