[PATCH] cowloop: replace procfs code with debugfs

Denis Kirjanov <kirjanov@gmail.com kirjanov at gmail.com
Sun Sep 20 10:43:31 UTC 2009


Patch replaces procfs code with debugfs
Signed-off-by: Denis Kirjanov <kirjanov at gmail.com>
---
--- cowloop.c.wo.debugfs	2009-09-19 18:15:05.000000000 +0400
+++ cowloop.c	2009-09-19 22:29:26.000000000 +0400
@@ -157,13 +157,13 @@ static char cowloop_version[] = "3.2";
 #include <linux/slab.h>
 #include <linux/semaphore.h>
 #include <linux/uaccess.h>
-#include <linux/proc_fs.h>
 #include <linux/blkdev.h>
 #include <linux/buffer_head.h>
 #include <linux/hdreg.h>
 #include <linux/genhd.h>
 #include <linux/statfs.h>
 #include <linux/kthread.h>
+#include <linux/debugfs.h>
 
 #include "cowloop.h"
 
@@ -303,9 +303,10 @@ static struct gendisk         *cowctlgd;
 static spinlock_t             cowctlrqlock; /* for req.q. of ctrl. channel   */
 
 /*
-** private directory /proc/cow
+** debugfs directory
 */
-static struct proc_dir_entry   *cowlo_procdir;
+static struct dentry	*cowlo_dbgfs_root;
+static struct dentry	*cowlo_dbgfs_files[DFLCOWS];
 
 /*
 ** function prototypes
@@ -1628,14 +1629,27 @@ cowlo_writecowraw(struct cowloop_device 
 
 
 /*
-** readproc-function: called when the corresponding /proc-file is read
+** debugfs file operations
 */
+
 static int
-cowlo_readproc(char *buf, char **start, off_t pos, int cnt, int *eof, void *p)
+cowlo_dbgfs_open(struct inode *inode, struct file *file)
 {
-	struct cowloop_device *cowdev = p;
+	file->private_data = (struct cowloop_device *)inode->i_private;
+	return 0;
+}
 
-	return sprintf(buf,
+static ssize_t
+cowlo_dbgfs_read(struct file *file, char __user *userbuf,
+		size_t count, loff_t *ppos)
+{
+	struct cowloop_device *cowdev = file->private_data;
+	unsigned long addr = get_zeroed_page(GFP_KERNEL);
+	char *buf = (char *)addr;
+	size_t pos = 0;
+	int res;
+
+	pos += sprintf(buf,
 		"   cowloop version: %9s\n\n"
 		"      device state: %s%s%s%s\n"
 		"   number of opens: %9d\n"
@@ -1665,8 +1679,18 @@ cowlo_readproc(char *buf, char **start, 
 			cowdev->nrcowblocks, MAPUNIT,
 			cowdev->cowreads,
 			cowdev->cowwrites);
+	res = simple_read_from_buffer(userbuf, count, ppos, buf, pos);
+	free_page(addr);
+
+	return res;
 }
 
+static const struct file_operations cowlo_dbgfs_fops = {
+	.owner		= THIS_MODULE,
+	.open		= cowlo_dbgfs_open,
+	.read		= cowlo_dbgfs_read,
+};
+
 /*****************************************************************************/
 /* Setup and destroy cowdevices                                              */
 /*****************************************************************************/
@@ -1814,15 +1838,15 @@ cowlo_openpair(char *rdof, char *cowf, i
 	}
 
 	/*
-	** create a file below directory /proc/cow for this new cowdevice
+	** create a file below /sys/kernel/debug/cow for this new cowdevice
 	*/
-	if (cowlo_procdir) {
+	if (cowlo_dbgfs_root) {
 		char    tmpname[64];
 
-		sprintf(tmpname, "%d", minor);
+		snprintf(tmpname, sizeof(tmpname), "%d", minor);
 
-		create_proc_read_entry(tmpname, 0 , cowlo_procdir,
-						cowlo_readproc, cowdev);
+		cowlo_dbgfs_files[minor] = debugfs_create_file(tmpname, 0,
+				cowlo_dbgfs_root, cowdev, &cowlo_dbgfs_fops);
 	}
 
 	cowdev->state   |= COWDEVOPEN;
@@ -1895,16 +1919,12 @@ cowlo_closepair(struct cowloop_device *c
 	del_gendisk(cowdev->gd);  /* revert the alloc_disk() */
 	put_disk(cowdev->gd);     /* revert the add_disk()   */
 
-	if (cowlo_procdir) {
-		char    tmpname[64];
-
+	if (cowlo_dbgfs_root) {
 		for (minor = 0; minor < maxcows; minor++) {
 			if (cowdev == cowdevall[minor])
 				break;
 		}
-		sprintf(tmpname, "%d", minor);
-
-		remove_proc_entry(tmpname, cowlo_procdir);
+		debugfs_remove(cowlo_dbgfs_files[minor]);
 	}
 
 	blk_cleanup_queue(cowdev->rqueue);
@@ -2639,10 +2659,12 @@ cowlo_init_module(void)
 	}
 
 	/*
-	** create a directory below /proc to allocate a file
+	** create a directory below /sys/kernel/debug to allocate a file
 	** for each cowdevice that is allocated later on
 	*/
-	cowlo_procdir = proc_mkdir("cow", NULL);
+	cowlo_dbgfs_root = debugfs_create_dir("cow", NULL);
+	if (!cowlo_dbgfs_root)
+		printk(KERN_ERR "cowloop: failed to create debugfs directory");
 
 	/*
 	** check if a cowdevice has to be opened during insmod/modprobe;
@@ -2668,7 +2690,7 @@ cowlo_init_module(void)
 		*/
 		retval = cowlo_openpair(rdofile, cowfile, wantrecover, 0);
 		if (retval) {
-			remove_proc_entry("cow", NULL);
+			debugfs_remove(cowlo_dbgfs_root);
 			unregister_blkdev(COWMAJOR, DEVICE_NAME);
 			goto error_out;
 		}
@@ -2679,7 +2701,7 @@ cowlo_init_module(void)
 		if ((rdofile[0] != '\0') || (cowfile[0] != '\0')) {
 			printk(KERN_ERR
 			       "cowloop - only one filename specified\n");
-			remove_proc_entry("cow", NULL);
+			debugfs_remove(cowlo_dbgfs_root);
 			unregister_blkdev(COWMAJOR, DEVICE_NAME);
 			retval = -EINVAL;
 			goto error_out;
@@ -2695,7 +2717,7 @@ cowlo_init_module(void)
 		printk(KERN_WARNING
 		       "cowloop - unable to alloc_disk for cowctl\n");
 
-		remove_proc_entry("cow", NULL);
+		debugfs_remove(cowlo_dbgfs_root);
 		(void) cowlo_closepair(cowdevall[0]);
 		unregister_blkdev(COWMAJOR, DEVICE_NAME);
 		retval = -ENOMEM;
@@ -2757,9 +2779,9 @@ cowlo_cleanup_module(void)
 	unregister_blkdev(COWMAJOR, DEVICE_NAME);
 
 	/*
-	** get rid of /proc/cow and unregister the driver
+	** get rid of debugfs stuff and unregister the driver
 	*/
-	remove_proc_entry("cow", NULL);
+	debugfs_remove(cowlo_dbgfs_root);
 
 	for (minor = 0; minor < maxcows; minor++)
 		kfree(cowdevall[minor]);



More information about the devel mailing list