aboutsummaryrefslogtreecommitdiffstats
path: root/bad/ndevfs.patch
blob: 5af0742a2813d04ea1bcb5e4435c7ac2b6513125 (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
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
From foo@baz.org Sun Jun  5 14:33:21 2005
Date: Mon, 20 Jun 2005 21:15:16 -0700
To: Greg KH <gregkh@suse.de>
From: Greg Kroah-Hartman <gregkh@suse.de>
Subject: ndevfs: a nano-devfs like fs

You don't really want to run this.  But if you did, here's a simple hack
showing how easy it is to do it.

Note, this patch will NOT be merged into mainline, so don't get your
panties into a bind...


Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

---
 drivers/base/class.c   |    7 +
 fs/Kconfig             |    3 
 fs/Makefile            |    1 
 fs/ndevfs/Makefile     |    4 
 fs/ndevfs/inode.c      |  249 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/partitions/check.c  |    6 +
 include/linux/ndevfs.h |   13 ++
 7 files changed, 282 insertions(+), 1 deletion(-)

--- gregkh-2.6.orig/drivers/base/class.c
+++ gregkh-2.6/drivers/base/class.c
@@ -18,6 +18,7 @@
 #include <linux/kdev_t.h>
 #include <linux/err.h>
 #include <linux/slab.h>
+#include <linux/ndevfs.h>
 #include "base.h"
 
 #define to_class_attr(_attr) container_of(_attr, struct class_attribute, attr)
@@ -577,6 +578,8 @@ int class_device_add(struct class_device
 		attr->show = show_dev;
 		class_device_create_file(class_dev, attr);
 		class_dev->devt_attr = attr;
+
+		ndevfs_create(class_dev->class_id, class_dev->devt, 1);
 	}
 
 	class_device_add_attrs(class_dev);
@@ -700,8 +703,10 @@ void class_device_del(struct class_devic
 		sysfs_remove_link(&class_dev->dev->kobj, class_name);
 	}
 	class_device_remove_file(class_dev, &class_dev->uevent_attr);
-	if (class_dev->devt_attr)
+	if (class_dev->devt_attr) {
 		class_device_remove_file(class_dev, class_dev->devt_attr);
+		ndevfs_remove(class_dev->class_id);
+	}
 	class_device_remove_attrs(class_dev);
 	class_device_remove_groups(class_dev);
 
--- gregkh-2.6.orig/fs/Kconfig
+++ gregkh-2.6/fs/Kconfig
@@ -1815,6 +1815,9 @@ config 9P_FS
 
 	  If unsure, say N.
 
+config NDEV_FS
+	bool "Nano Device File System"
+
 endmenu
 
 menu "Partition Types"
--- gregkh-2.6.orig/fs/Makefile
+++ gregkh-2.6/fs/Makefile
@@ -101,3 +101,4 @@ obj-$(CONFIG_HPPFS)		+= hppfs/
 obj-$(CONFIG_DEBUG_FS)		+= debugfs/
 obj-$(CONFIG_CONFIGFS_FS)	+= configfs/
 obj-$(CONFIG_OCFS2_FS)		+= ocfs2/
+obj-$(CONFIG_NDEV_FS)		+= ndevfs/
--- /dev/null
+++ gregkh-2.6/fs/ndevfs/Makefile
@@ -0,0 +1,4 @@
+ndevfs-objs	:= inode.o
+
+obj-$(CONFIG_NDEV_FS)	+= ndevfs.o
+
--- /dev/null
+++ gregkh-2.6/fs/ndevfs/inode.c
@@ -0,0 +1,249 @@
+/*
+ *  inode.c - part of ndevfs, a tiny little device file system
+ *
+ *  Copyright (C) 2004,2005 Greg Kroah-Hartman <greg@kroah.com>
+ *
+ *	This program is free software; you can redistribute it and/or
+ *	modify it under the terms of the GNU General Public License version
+ *	2 as published by the Free Software Foundation.
+ *
+ * Written for all of the people out there who just hate userspace solutions.
+ *
+ */
+
+/* uncomment to get debug messages */
+#define DEBUG
+
+#include <linux/config.h>
+#include <linux/module.h>
+#include <linux/fs.h>
+#include <linux/mount.h>
+#include <linux/pagemap.h>
+#include <linux/init.h>
+#include <linux/namei.h>
+#include <linux/device.h>
+#include <linux/ndevfs.h>
+
+#define MAGIC	0x64756d62
+
+struct entry {
+	struct list_head node;
+	struct dentry *dentry;
+	char name[BUS_ID_SIZE];
+};
+static LIST_HEAD(entries);
+
+static struct vfsmount *mount;
+static int mount_count;
+
+static struct file_operations stupid_file_ops = {
+	.read	= generic_file_read,
+	.write	= generic_file_write,
+	.mmap	= generic_file_mmap,
+	.fsync	= simple_sync_file,
+	.llseek	= generic_file_llseek,
+};
+
+static struct inode *get_inode(struct super_block *sb, int mode, dev_t dev)
+{
+	struct inode *inode = new_inode(sb);
+
+	if (inode) {
+		inode->i_mode = mode;
+		inode->i_uid = 0;
+		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;
+		switch (mode & S_IFMT) {
+		default:
+			init_special_inode(inode, mode, dev);
+			break;
+		case S_IFREG:
+			inode->i_fop = &stupid_file_ops;
+			break;
+		case S_IFDIR:
+			inode->i_op = &simple_dir_inode_operations;
+			inode->i_fop = &simple_dir_operations;
+
+			/* directory inodes start off with i_nlink == 2 (for "." entry) */
+			inode->i_nlink++;
+			break;
+		}
+	}
+	return inode;
+}
+
+/* SMP-safe */
+static int mknod(struct inode *dir, struct dentry *dentry,
+			 int mode, dev_t dev)
+{
+	struct inode *inode = get_inode(dir->i_sb, mode, dev);
+	int error = -EPERM;
+
+	if (dentry->d_inode)
+		return -EEXIST;
+
+	if (inode) {
+		d_instantiate(dentry, inode);
+		dget(dentry);
+		error = 0;
+	}
+	return error;
+}
+
+static inline int positive(struct dentry *dentry)
+{
+	return dentry->d_inode && !d_unhashed(dentry);
+}
+
+static int fill_super(struct super_block *sb, void *data, int silent)
+{
+	static struct tree_descr files[] = {{""}};
+
+	return simple_fill_super(sb, MAGIC, files);
+}
+
+static struct super_block *get_sb(struct file_system_type *fs_type,
+				  int flags, const char *dev_name,
+				  void *data)
+{
+	return get_sb_single(fs_type, flags, data, fill_super);
+}
+
+static void remove(struct dentry *dentry)
+{
+	struct dentry *parent;
+
+	if (!dentry)
+		return;
+
+	parent = dentry->d_parent;
+	if (!parent || !parent->d_inode)
+		return;
+
+	mutex_lock(&parent->d_inode->i_mutex);
+	if (positive(dentry)) {
+		if (dentry->d_inode) {
+			if (S_ISDIR(dentry->d_inode->i_mode))
+				simple_rmdir(parent->d_inode, dentry);
+			else
+				simple_unlink(parent->d_inode, dentry);
+		dput(dentry);
+		}
+	}
+	mutex_unlock(&parent->d_inode->i_mutex);
+	simple_release_fs(&mount, &mount_count);
+}
+
+/**
+ * ndevfs_create - create a device node in ndevfs
+ *
+ * @name: the name to create
+ * @dev: the dev_t of the node
+ * @is_char: if the node is a char device or not
+ */
+void ndevfs_create(const char *name, dev_t dev, int is_char)
+{
+	struct dentry *parent;
+	struct dentry *dentry;
+	struct entry *entry;
+	int err;
+	int mode = S_IRUSR | S_IWUSR;
+
+	pr_debug("ndevfs: creating file '%s' with major %d and minor %d\n",
+		 name, MAJOR(dev), MINOR(dev));
+
+	if (is_char)
+		mode |= S_IFCHR;
+	else
+		mode |= S_IFBLK;
+
+	err = simple_pin_fs("ndevfs", &mount, &mount_count);
+	if (err)
+		return;
+
+	/* everything is at the root fs, no directories allowed */
+	if (mount && mount->mnt_sb) {
+		parent = mount->mnt_sb->s_root;
+	} else {
+		pr_debug("%s: no parent?\n", __FUNCTION__);
+		goto error;
+	}
+
+	mutex_lock(&parent->d_inode->i_mutex);
+	dentry = lookup_one_len(name, parent, strlen(name));
+	if (!IS_ERR(dentry))
+		err = mknod(parent->d_inode, dentry, mode, dev);
+	else
+		err = PTR_ERR(dentry);
+	mutex_unlock(&parent->d_inode->i_mutex);
+
+	if (err)
+		goto error;
+
+	entry = kmalloc(sizeof(struct entry), GFP_KERNEL);
+	if (!entry) {
+		remove(dentry);
+		err = -ENOMEM;
+		goto error;
+	}
+	entry->dentry = dentry;
+	strcpy(&entry->name[0], name);
+	list_add(&entry->node, &entries);
+	return;
+
+error:
+	pr_debug("%s failed with error %d\n", __FUNCTION__, err);
+	simple_release_fs(&mount, &mount_count);
+}
+EXPORT_SYMBOL_GPL(ndevfs_create);
+
+/**
+ * ndevfs_remove - removes the node from the fs
+ *
+ * @name: the name to remove.
+ */
+void ndevfs_remove(const char *name)
+{
+	struct entry *entry;
+	struct dentry *dentry = NULL;
+
+	pr_debug("ndevfs: removing file '%s'\n", name);
+
+	list_for_each_entry(entry, &entries, node) {
+		if (strcmp(name, &entry->name[0]) == 0) {
+			dentry = entry->dentry;
+			break;
+		}
+	}
+	if (!dentry) {
+		pr_debug("%s: can't find %s\n", __FUNCTION__, name);
+		return;
+	}
+	remove (dentry);
+}
+EXPORT_SYMBOL_GPL(ndevfs_remove);
+
+static struct file_system_type fs_type = {
+	.owner =	THIS_MODULE,
+	.name =		"ndevfs",
+	.get_sb =	get_sb,
+	.kill_sb =	kill_litter_super,
+};
+
+static int __init ndevfs_init(void)
+{
+	return register_filesystem(&fs_type);
+}
+
+static void __exit ndevfs_exit(void)
+{
+	simple_release_fs(&mount, &mount_count);
+	unregister_filesystem(&fs_type);
+}
+
+core_initcall(ndevfs_init);
+module_exit(ndevfs_exit);
+MODULE_LICENSE("GPL");
+
--- gregkh-2.6.orig/fs/partitions/check.c
+++ gregkh-2.6/fs/partitions/check.c
@@ -18,6 +18,7 @@
 #include <linux/fs.h>
 #include <linux/kmod.h>
 #include <linux/ctype.h>
+#include <linux/ndevfs.h>
 
 #include "check.h"
 
@@ -321,6 +322,7 @@ void delete_partition(struct gendisk *di
 	p->sectors[0] = p->sectors[1] = 0;
 	if (p->holder_dir)
 		kobject_unregister(p->holder_dir);
+	ndevfs_remove(kobject_name(&p->kobj));
 	kobject_uevent(&p->kobj, KOBJ_REMOVE);
 	kobject_del(&p->kobj);
 	kobject_put(&p->kobj);
@@ -349,6 +351,7 @@ void add_partition(struct gendisk *disk,
 	kobject_add(&p->kobj);
 	if (!disk->part_uevent_suppress)
 		kobject_uevent(&p->kobj, KOBJ_ADD);
+	ndevfs_create(kobject_name(&p->kobj), MKDEV(disk->major, p->partno), 0);
 	partition_sysfs_add_subdir(p);
 	disk->part[part-1] = p;
 }
@@ -404,6 +407,8 @@ void register_disk(struct gendisk *disk)
 		return;
 	disk_sysfs_symlinks(disk);
  	disk_sysfs_add_subdirs(disk);
+	ndevfs_create(kobject_name(&disk->kobj),
+		      MKDEV(disk->major, disk->first_minor), 0);
 
 	/* No minors to use for partitions */
 	if (disk->minors == 1)
@@ -524,6 +529,7 @@ void del_gendisk(struct gendisk *disk)
 		}
 		put_device(disk->driverfs_dev);
 	}
+	ndevfs_remove(kobject_name(&disk->kobj));
 	kobject_uevent(&disk->kobj, KOBJ_REMOVE);
 	kobject_del(&disk->kobj);
 }
--- /dev/null
+++ gregkh-2.6/include/linux/ndevfs.h
@@ -0,0 +1,13 @@
+#ifndef _NDEVFS_H_
+#define _NDEVFS_H_
+
+#if defined(CONFIG_NDEV_FS)
+extern void ndevfs_create(const char *name, dev_t dev, int is_char);
+extern void ndevfs_remove(const char *name);
+#else
+static inline void ndevfs_create(const char *name, dev_t dev, int is_char) {}
+static inline void ndevfs_remove(const char *name) {}
+#endif
+
+#endif
+