Skip to contentSkip to conversation

Rework file::show_fdinfo method to use seq-files engine

PATCH In review Cyrill Gorcunov 3 participants 10 messages Oct 30, 2013 – Oct 31, 2013

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

cover Rework file::show_fdinfo method to use seq-files engine

Hi, in criu we intensively use additional information provided by /proc/<pid>/fdinfo/<fd> particular to the object opened file represents. The output is printed out by file_operations::show_fdinfo method and the implementation of this method is pretty simple. Still I think better would be to convert this method to seq_operations pointer where all suppliers (epoll, eventfd and such) provide own seq-file operations. While this makes code a little more complex this will decrease the amount of kernel memory needed for output (because we won't need to provide all information in one pass but by chunks).

Please take a look, thanks!

#7 lore raw

PATCH 4/6 signalfd: Use sequential fdinfo engine

Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/signalfd.c |   30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
fs/signalfd.c +27 -3
Index: linux-2.6.git/fs/signalfd.c
===================================================================
@@ -230,9 +230,26 @@ static ssize_t signalfd_read(struct file
 }
 
 #ifdef CONFIG_PROC_FS
-static int signalfd_show_fdinfo(struct seq_file *m, struct file *f)
+static void *seq_start(struct seq_file *m, loff_t *pos)
 {
-	struct signalfd_ctx *ctx = f->private_data;
+	if (*pos == 0)
+		return m->private;
+	return NULL;
+}
+
+static void seq_stop(struct seq_file *m, void *v)
+{
+}
+
+static void *seq_next(struct seq_file *m, void *p, loff_t *pos)
+{
+	++*pos;
+	return NULL;
+}
+
+static int seq_show(struct seq_file *m, void *v)
+{
+	struct signalfd_ctx *ctx = ((struct file *)v)->private_data;
 	sigset_t sigmask;
 
 	sigmask = ctx->sigmask;
@@ -241,11 +258,18 @@ static int signalfd_show_fdinfo(struct s
 
 	return 0;
 }
+
+static struct seq_operations signalfd_fdinfo_ops = {
+	.start		= seq_start,
+	.next		= seq_next,
+	.stop		= seq_stop,
+	.show		= seq_show,
+};
 #endif
 
 static const struct file_operations signalfd_fops = {
 #ifdef CONFIG_PROC_FS
-	.show_fdinfo	= signalfd_show_fdinfo,
+	.fdinfo_ops	= &signalfd_fdinfo_ops,
 #endif
 	.release	= signalfd_release,
 	.poll		= signalfd_poll,
#1 lore raw

PATCH 1/6 procfs: Introduce sequential fdinfo engine

In reply to Cyrill Gorcunov

At moment the fdinfo operations (ie the output from /proc/$pid/fdinfo/$fd) are generating output in one pass, which makes useless memory pressue if the reader/user provides a buffer with a small size.

Instead, provide proc_fdinfo structure where each file hook own seq_operations for fdinfo output thus memory won't be needlessly allocated if the recepient buffer is small or the reader is seeking some particular position.

Once all callers to fdinfo are converted (addressed in next patches) the former @show_fdinfo will be deleted.

Note the patch doesn't change current output it's rather preparing the ground for next patches.

Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/proc/fd.c       |  162 ++++++++++++++++++++++++++++++++++++++++++-----------
 include/linux/fs.h |    1 
 2 files changed, 130 insertions(+), 33 deletions(-)
fs/proc/fd.c +129 -33
Index: linux-2.6.git/fs/proc/fd.c
===================================================================
@@ -14,60 +14,156 @@
 #include "internal.h"
 #include "fd.h"
 
+struct proc_fdinfo {
+	struct seq_file		seq;		/* Make sure it's first */
+	struct seq_operations	*fdinfo_ops;
+	loff_t			pos;
+	struct file		*f_file;
+	unsigned int		f_flags;
+};
+
+#define seq_info(m)	(container_of(m, struct proc_fdinfo, seq))
+
+static void *seq_start(struct seq_file *m, loff_t *pos)
+{
+	struct proc_fdinfo *info = seq_info(m);
+
+	info->pos = *pos;
+	if (*pos == 0)
+		return info;
+	return info->fdinfo_ops ? info->fdinfo_ops->start(m, pos) : NULL;
+}
+
+static void seq_stop(struct seq_file *m, void *v)
+{
+	struct proc_fdinfo *info = seq_info(m);
+
+	if (info->pos > 0 && info->fdinfo_ops)
+		info->fdinfo_ops->stop(m, v);
+}
+
+static void *seq_next(struct seq_file *m, void *p, loff_t *pos)
+{
+	struct proc_fdinfo *info = seq_info(m);
+	void *v = NULL;
+
+	if (info->fdinfo_ops) {
+		int ret = 0;
+
+		if (*pos == 0) {
+			v = info->fdinfo_ops->start(m, pos);
+			if (v) {
+				ret = info->fdinfo_ops->show(m, v);
+				p = v;
+			} else
+				ret = -1;
+		}
+
+		if (!ret)
+			v = info->fdinfo_ops->next(m, p, pos);
+		else
+			++*pos;
+	} else
+		++*pos;
+
+	info->pos = *pos;
+	return v;
+}
+
 static int seq_show(struct seq_file *m, void *v)
 {
+	struct proc_fdinfo *info = seq_info(m);
+
+	if (info->fdinfo_ops && info->pos > 0)
+		return info->fdinfo_ops->show(m, v);
+
+	seq_printf(m, "pos:\t%lli\nflags:\t0%o\n",
+		   (long long)info->f_file->f_pos, info->f_flags);
+
+	/* Legacy interface */
+	if (info->f_file->f_op->show_fdinfo)
+		return info->f_file->f_op->show_fdinfo(m, info->f_file);
+
+	return 0;
+}
+
+static const struct seq_operations fdinfo_seq_ops = {
+	.start	= seq_start,
+	.stop	= seq_stop,
+	.next	= seq_next,
+	.show	= seq_show,
+};
+
+static int seq_fdinfo_open(struct inode *inode, struct file *file)
+{
 	struct files_struct *files = NULL;
-	int f_flags = 0, ret = -ENOENT;
-	struct file *file = NULL;
+	struct proc_fdinfo *info;
 	struct task_struct *task;
+	int ret;
 
-	task = get_proc_task(m->private);
-	if (!task)
-		return -ENOENT;
-
-	files = get_files_struct(task);
-	put_task_struct(task);
-
-	if (files) {
-		int fd = proc_fd(m->private);
-
-		spin_lock(&files->file_lock);
-		file = fcheck_files(files, fd);
-		if (file) {
-			struct fdtable *fdt = files_fdtable(files);
-
-			f_flags = file->f_flags;
-			if (close_on_exec(fd, fdt))
-				f_flags |= O_CLOEXEC;
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+	file->private_data = &info->seq;
 
-			get_file(file);
-			ret = 0;
+	ret = seq_open(file, &fdinfo_seq_ops);
+	if (!ret) {
+		ret = -ENOENT;
+
+		task = get_proc_task(inode);
+		if (task) {
+			files = get_files_struct(task);
+			put_task_struct(task);
+		}
+
+		if (files) {
+			struct seq_file *m = file->private_data;
+			int fd = proc_fd(inode);
+
+			spin_lock(&files->file_lock);
+			info->f_file = fcheck_files(files, fd);
+			if (info->f_file) {
+				struct fdtable *fdt = files_fdtable(files);
+
+				info->f_flags = info->f_file->f_flags & ~O_CLOEXEC;
+				if (close_on_exec(fd, fdt))
+					info->f_flags |= O_CLOEXEC;
+
+				info->fdinfo_ops = info->f_file->f_op->fdinfo_ops;
+				m->private = info->f_file;
+
+				get_file(info->f_file);
+				ret = 0;
+			}
+			spin_unlock(&files->file_lock);
+			put_files_struct(files);
 		}
-		spin_unlock(&files->file_lock);
-		put_files_struct(files);
 	}
 
-	if (!ret) {
-                seq_printf(m, "pos:\t%lli\nflags:\t0%o\n",
-			   (long long)file->f_pos, f_flags);
-		if (file->f_op->show_fdinfo)
-			ret = file->f_op->show_fdinfo(m, file);
-		fput(file);
+	if (ret) {
+		if (info->f_file)
+			fput(info->f_file);
+		file->private_data = NULL;
+		kfree(info);
 	}
 
 	return ret;
 }
 
-static int seq_fdinfo_open(struct inode *inode, struct file *file)
+static int seq_fdinfo_release(struct inode *inode, struct file *file)
 {
-	return single_open(file, seq_show, inode);
+	struct seq_file *m = file->private_data;
+	struct proc_fdinfo *info = seq_info(m);
+
+	fput(info->f_file);
+	return seq_release(inode, file);
 }
 
 static const struct file_operations proc_fdinfo_file_operations = {
 	.open		= seq_fdinfo_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
-	.release	= single_release,
+	.release	= seq_fdinfo_release,
 };
 
 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
include/linux/fs.h +1 -0
Index: linux-2.6.git/include/linux/fs.h
===================================================================
@@ -1552,6 +1552,7 @@ struct file_operations {
 	long (*fallocate)(struct file *file, int mode, loff_t offset,
 			  loff_t len);
 	int (*show_fdinfo)(struct seq_file *m, struct file *f);
+	struct seq_operations *fdinfo_ops;
 };
 
 struct inode_operations {
#6 lore raw

PATCH 3/6 eventfd: Use sequential fdinfo engine

In reply to Cyrill Gorcunov
Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/eventfd.c |   30 +++++++++++++++++++++++++++---
 1 file changed, 27 insertions(+), 3 deletions(-)
fs/eventfd.c +27 -3
Index: linux-2.6.git/fs/eventfd.c
===================================================================
@@ -287,9 +287,26 @@ static ssize_t eventfd_write(struct file
 }
 
 #ifdef CONFIG_PROC_FS
-static int eventfd_show_fdinfo(struct seq_file *m, struct file *f)
+static void *seq_start(struct seq_file *m, loff_t *pos)
 {
-	struct eventfd_ctx *ctx = f->private_data;
+	if (*pos == 0)
+		return m->private;
+	return NULL;
+}
+
+static void seq_stop(struct seq_file *m, void *v)
+{
+}
+
+static void *seq_next(struct seq_file *m, void *p, loff_t *pos)
+{
+	++*pos;
+	return NULL;
+}
+
+static int seq_show(struct seq_file *m, void *v)
+{
+	struct eventfd_ctx *ctx = ((struct file *)v)->private_data;
 	int ret;
 
 	spin_lock_irq(&ctx->wqh.lock);
@@ -299,11 +316,18 @@ static int eventfd_show_fdinfo(struct se
 
 	return ret;
 }
+
+static struct seq_operations eventfd_fdinfo_ops = {
+	.start		= seq_start,
+	.next		= seq_next,
+	.stop		= seq_stop,
+	.show		= seq_show,
+};
 #endif
 
 static const struct file_operations eventfd_fops = {
 #ifdef CONFIG_PROC_FS
-	.show_fdinfo	= eventfd_show_fdinfo,
+	.fdinfo_ops	= &eventfd_fdinfo_ops,
 #endif
 	.release	= eventfd_release,
 	.poll		= eventfd_poll,
#9 lore raw

PATCH 6/6 procfs: Drop legacy show_fdinfo file operation

In reply to Cyrill Gorcunov

All former users of show_fdinfo are converted to sequential output engine thus we don't need it anymore.

Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/proc/fd.c       |    5 -----
 include/linux/fs.h |    1 -
 2 files changed, 6 deletions(-)
fs/proc/fd.c +0 -5
Index: linux-2.6.git/fs/proc/fd.c
===================================================================
@@ -79,11 +79,6 @@ static int seq_show(struct seq_file *m,
 
 	seq_printf(m, "pos:\t%lli\nflags:\t0%o\n",
 		   (long long)info->f_file->f_pos, info->f_flags);
-
-	/* Legacy interface */
-	if (info->f_file->f_op->show_fdinfo)
-		return info->f_file->f_op->show_fdinfo(m, info->f_file);
-
 	return 0;
 }
 
include/linux/fs.h +0 -1
Index: linux-2.6.git/include/linux/fs.h
===================================================================
@@ -1551,7 +1551,6 @@ struct file_operations {
 	int (*setlease)(struct file *, long, struct file_lock **);
 	long (*fallocate)(struct file *file, int mode, loff_t offset,
 			  loff_t len);
-	int (*show_fdinfo)(struct seq_file *m, struct file *f);
 	struct seq_operations *fdinfo_ops;
 };
 
#8 lore raw

PATCH 5/6 fsnotify: Use sequential fdinfo engine

In reply to Cyrill Gorcunov
Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/notify/fanotify/fanotify_user.c |    4 -
 fs/notify/fdinfo.c                 |  121 +++++++++++++++++++++++++------------
 fs/notify/fdinfo.h                 |   12 ---
 fs/notify/inotify/inotify_user.c   |    4 -
 4 files changed, 94 insertions(+), 47 deletions(-)
fs/notify/fanotify/fanotify_user.c +3 -1
Index: linux-2.6.git/fs/notify/fanotify/fanotify_user.c
===================================================================
@@ -432,7 +432,9 @@ static long fanotify_ioctl(struct file *
 }
 
 static const struct file_operations fanotify_fops = {
-	.show_fdinfo	= fanotify_show_fdinfo,
+#ifdef CONFIG_PROC_FS
+	.fdinfo_ops	= &fanotify_fdinfo_ops,
+#endif
 	.poll		= fanotify_poll,
 	.read		= fanotify_read,
 	.write		= fanotify_write,
fs/notify/fdinfo.c +85 -36

171 lines not on this page · raw message

fs/notify/fdinfo.h +3 -9
Index: linux-2.6.git/fs/notify/fdinfo.h
===================================================================
@@ -4,24 +4,18 @@
 #include <linux/errno.h>
 #include <linux/proc_fs.h>
 
-struct seq_file;
-struct file;
+struct seq_operations;
 
 #ifdef CONFIG_PROC_FS
 
 #ifdef CONFIG_INOTIFY_USER
-extern int inotify_show_fdinfo(struct seq_file *m, struct file *f);
+extern struct seq_operations inotify_fdinfo_ops;
 #endif
 
 #ifdef CONFIG_FANOTIFY
-extern int fanotify_show_fdinfo(struct seq_file *m, struct file *f);
+extern struct seq_operations fanotify_fdinfo_ops;
 #endif
 
-#else /* CONFIG_PROC_FS */
-
-#define inotify_show_fdinfo	NULL
-#define fanotify_show_fdinfo	NULL
-
 #endif /* CONFIG_PROC_FS */
 
 #endif /* __FSNOTIFY_FDINFO_H__ */
fs/notify/inotify/inotify_user.c +3 -1
Index: linux-2.6.git/fs/notify/inotify/inotify_user.c
===================================================================
@@ -327,7 +327,9 @@ static long inotify_ioctl(struct file *f
 }
 
 static const struct file_operations inotify_fops = {
-	.show_fdinfo	= inotify_show_fdinfo,
+#ifdef CONFIG_PROC_FS
+	.fdinfo_ops	= &inotify_fdinfo_ops,
+#endif
 	.poll		= inotify_poll,
 	.read		= inotify_read,
 	.fasync		= fsnotify_fasync,
#5 lore raw

PATCH 2/6 epoll: Use sequential fdinfo engine

In reply to Cyrill Gorcunov
Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Tvrtko Ursulin <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/eventpoll.c |   45 +++++++++++++++++++++++++++++++++------------
 1 file changed, 33 insertions(+), 12 deletions(-)
fs/eventpoll.c +33 -12
Index: linux-2.6.git/fs/eventpoll.c
===================================================================
@@ -834,32 +834,53 @@ static unsigned int ep_eventpoll_poll(st
 }
 
 #ifdef CONFIG_PROC_FS
-static int ep_show_fdinfo(struct seq_file *m, struct file *f)
+static void *seq_start(struct seq_file *m, loff_t *pos)
 {
-	struct eventpoll *ep = f->private_data;
+	struct eventpoll *ep = ((struct file *)m->private)->private_data;
 	struct rb_node *rbp;
-	int ret = 0;
+	loff_t num = *pos;
 
 	mutex_lock(&ep->mtx);
 	for (rbp = rb_first(&ep->rbr); rbp; rbp = rb_next(rbp)) {
-		struct epitem *epi = rb_entry(rbp, struct epitem, rbn);
-
-		ret = seq_printf(m, "tfd: %8d events: %8x data: %16llx\n",
-				 epi->ffd.fd, epi->event.events,
-				 (long long)epi->event.data);
-		if (ret)
-			break;
+		if (num-- == 0)
+			return rbp;
 	}
+	return NULL;
+}
+
+static void seq_stop(struct seq_file *m, void *v)
+{
+	struct eventpoll *ep = ((struct file *)m->private)->private_data;
 	mutex_unlock(&ep->mtx);
+}
 
-	return ret;
+static void *seq_next(struct seq_file *m, void *p, loff_t *pos)
+{
+	++*pos;
+	return (void *)rb_next(p);
 }
+
+static int seq_show(struct seq_file *m, void *v)
+{
+	struct epitem *epi = rb_entry(v, struct epitem, rbn);
+
+	return seq_printf(m, "tfd: %8d events: %8x data: %16llx\n",
+			  epi->ffd.fd, epi->event.events,
+			  (long long)epi->event.data);
+}
+
+static struct seq_operations ep_fdinfo_ops = {
+	.start		= seq_start,
+	.next		= seq_next,
+	.stop		= seq_stop,
+	.show		= seq_show,
+};
 #endif
 
 /* File callbacks that implement the eventpoll file behaviour */
 static const struct file_operations eventpoll_fops = {
 #ifdef CONFIG_PROC_FS
-	.show_fdinfo	= ep_show_fdinfo,
+	.fdinfo_ops	= &ep_fdinfo_ops,
 #endif
 	.release	= ep_eventpoll_release,
 	.poll		= ep_eventpoll_poll,
#2 lore raw

Re: [patch 1/6] procfs: Introduce sequential fdinfo engine

In reply to Cyrill Gorcunov
Quoted 3 lines · Cyrill Gorcunov

At moment the fdinfo operations (ie the output from /proc/$pid/fdinfo/$fd) are generating output in one pass, which makes useless memory pressue if the reader/user provides a buffer with a small size.

cat(1) uses 64 KB buffer.
The output doesn't exceed one page anyway.

#3 lore raw

Re: [patch 1/6] procfs: Introduce sequential fdinfo engine

Quoted 7 lines · Alexey Dobriyan
Quoted 3 lines · Cyrill Gorcunov

At moment the fdinfo operations (ie the output from /proc/$pid/fdinfo/$fd) are generating output in one pass, which makes useless memory pressue if the reader/user provides a buffer with a small size.

cat(1) uses 64 KB buffer.
The output doesn't exceed one page anyway.

Yes, good point. I probably need to update changelog (forgot that i'm using single_open here). What if we meet a file with that big number of epoll or say notifies assigned that the fdinfo won't fit a page? I didn't meet such scenario yet, but I think it's possible?

#4 lore raw

Re: [patch 1/6] procfs: Introduce sequential fdinfo engine

Quoted 11 lines · Cyrill Gorcunov
Quoted 6 lines
Quoted 3 lines

At moment the fdinfo operations (ie the output from /proc/$pid/fdinfo/$fd) are generating output in one pass, which makes useless memory pressue if the reader/user provides a buffer with a small size.

cat(1) uses 64 KB buffer.
The output doesn't exceed one page anyway.

Yes, good point. I probably need to update changelog (forgot that i'm using single_open here). What if we meet a file with that big number of epoll or say notifies assigned that the fdinfo won't fit a page? I didn't meet such scenario yet, but I think it's possible?

Alexey, would the following changelog sound better? I've been testing the program which creates 100 pipes, which in turn are watched with epoll (I don't think someone ever need to do that actully) then tried to read fdinfo and it failed. Instead if I'm using this series I can see all 100 pipes in the output.
---
From: Cyrill Gorcunov <[email protected]>
Subject: procfs: Introduce sequential fdinfo engine

At moment the fdinfo operations (ie the output from /proc/$pid/fdinfo/$fd) are generating output in one pass, allocating one page buffer as maximum. But in case of lots of event polls watchees (100 and more) read procedure will fail because one page is not enough to provide all information associated with watch descriptors.

Instead, provide proc_fdinfo structure where each file hook own seq_operations for fdinfo output and the seq-file engine will be able to traverse over the all records and provide the reader complete information increasing buffer size if needed.

Once all callers to fdinfo are converted (addressed in next patches) the former @show_fdinfo will be deleted.

Note the patch doesn't change current output it's rather preparing the ground for next patches.

Cc Pavel Emelyanov <[email protected]>
Cc Oleg Nesterov <[email protected]>
Cc Andrey Vagin <[email protected]>
Cc Al Viro <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc James Bottomley <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Matthew Helsley <[email protected]>
Cc "J. Bruce Fields" <[email protected]>
Cc "Aneesh Kumar K.V" <[email protected]>
Cc Alexey Dobriyan <[email protected]>
Cc Andrew Morton <[email protected]>
---
 fs/proc/fd.c       |  162 ++++++++++++++++++++++++++++++++++++++++++-----------
 include/linux/fs.h |    1 
 2 files changed, 130 insertions(+), 33 deletions(-)
fs/proc/fd.c +129 -33
Index: linux-2.6.git/fs/proc/fd.c
===================================================================
@@ -14,60 +14,156 @@
 #include "internal.h"
 #include "fd.h"
 
+struct proc_fdinfo {
+	struct seq_file		seq;		/* Make sure it's first */
+	struct seq_operations	*fdinfo_ops;
+	loff_t			pos;
+	struct file		*f_file;
+	unsigned int		f_flags;
+};
+
+#define seq_info(m)	(container_of(m, struct proc_fdinfo, seq))
+
+static void *seq_start(struct seq_file *m, loff_t *pos)
+{
+	struct proc_fdinfo *info = seq_info(m);
+
+	info->pos = *pos;
+	if (*pos == 0)
+		return info;
+	return info->fdinfo_ops ? info->fdinfo_ops->start(m, pos) : NULL;
+}
+
+static void seq_stop(struct seq_file *m, void *v)
+{
+	struct proc_fdinfo *info = seq_info(m);
+
+	if (info->pos > 0 && info->fdinfo_ops)
+		info->fdinfo_ops->stop(m, v);
+}
+
+static void *seq_next(struct seq_file *m, void *p, loff_t *pos)
+{
+	struct proc_fdinfo *info = seq_info(m);
+	void *v = NULL;
+
+	if (info->fdinfo_ops) {
+		int ret = 0;
+
+		if (*pos == 0) {
+			v = info->fdinfo_ops->start(m, pos);
+			if (v) {
+				ret = info->fdinfo_ops->show(m, v);
+				p = v;
+			} else
+				ret = -1;
+		}
+
+		if (!ret)
+			v = info->fdinfo_ops->next(m, p, pos);
+		else
+			++*pos;
+	} else
+		++*pos;
+
+	info->pos = *pos;
+	return v;
+}
+
 static int seq_show(struct seq_file *m, void *v)
 {
+	struct proc_fdinfo *info = seq_info(m);
+
+	if (info->fdinfo_ops && info->pos > 0)
+		return info->fdinfo_ops->show(m, v);
+
+	seq_printf(m, "pos:\t%lli\nflags:\t0%o\n",
+		   (long long)info->f_file->f_pos, info->f_flags);
+
+	/* Legacy interface */
+	if (info->f_file->f_op->show_fdinfo)
+		return info->f_file->f_op->show_fdinfo(m, info->f_file);
+
+	return 0;
+}
+
+static const struct seq_operations fdinfo_seq_ops = {
+	.start	= seq_start,
+	.stop	= seq_stop,
+	.next	= seq_next,
+	.show	= seq_show,
+};
+
+static int seq_fdinfo_open(struct inode *inode, struct file *file)
+{
 	struct files_struct *files = NULL;
-	int f_flags = 0, ret = -ENOENT;
-	struct file *file = NULL;
+	struct proc_fdinfo *info;
 	struct task_struct *task;
+	int ret;
 
-	task = get_proc_task(m->private);
-	if (!task)
-		return -ENOENT;
-
-	files = get_files_struct(task);
-	put_task_struct(task);
-
-	if (files) {
-		int fd = proc_fd(m->private);
-
-		spin_lock(&files->file_lock);
-		file = fcheck_files(files, fd);
-		if (file) {
-			struct fdtable *fdt = files_fdtable(files);
-
-			f_flags = file->f_flags;
-			if (close_on_exec(fd, fdt))
-				f_flags |= O_CLOEXEC;
+	info = kzalloc(sizeof(*info), GFP_KERNEL);
+	if (!info)
+		return -ENOMEM;
+	file->private_data = &info->seq;
 
-			get_file(file);
-			ret = 0;
+	ret = seq_open(file, &fdinfo_seq_ops);
+	if (!ret) {
+		ret = -ENOENT;
+
+		task = get_proc_task(inode);
+		if (task) {
+			files = get_files_struct(task);
+			put_task_struct(task);
+		}
+
+		if (files) {
+			struct seq_file *m = file->private_data;
+			int fd = proc_fd(inode);
+
+			spin_lock(&files->file_lock);
+			info->f_file = fcheck_files(files, fd);
+			if (info->f_file) {
+				struct fdtable *fdt = files_fdtable(files);
+
+				info->f_flags = info->f_file->f_flags & ~O_CLOEXEC;
+				if (close_on_exec(fd, fdt))
+					info->f_flags |= O_CLOEXEC;
+
+				info->fdinfo_ops = info->f_file->f_op->fdinfo_ops;
+				m->private = info->f_file;
+
+				get_file(info->f_file);
+				ret = 0;
+			}
+			spin_unlock(&files->file_lock);
+			put_files_struct(files);
 		}
-		spin_unlock(&files->file_lock);
-		put_files_struct(files);
 	}
 
-	if (!ret) {
-                seq_printf(m, "pos:\t%lli\nflags:\t0%o\n",
-			   (long long)file->f_pos, f_flags);
-		if (file->f_op->show_fdinfo)
-			ret = file->f_op->show_fdinfo(m, file);
-		fput(file);
+	if (ret) {
+		if (info->f_file)
+			fput(info->f_file);
+		file->private_data = NULL;
+		kfree(info);
 	}
 
 	return ret;
 }
 
-static int seq_fdinfo_open(struct inode *inode, struct file *file)
+static int seq_fdinfo_release(struct inode *inode, struct file *file)
 {
-	return single_open(file, seq_show, inode);
+	struct seq_file *m = file->private_data;
+	struct proc_fdinfo *info = seq_info(m);
+
+	fput(info->f_file);
+	return seq_release(inode, file);
 }
 
 static const struct file_operations proc_fdinfo_file_operations = {
 	.open		= seq_fdinfo_open,
 	.read		= seq_read,
 	.llseek		= seq_lseek,
-	.release	= single_release,
+	.release	= seq_fdinfo_release,
 };
 
 static int tid_fd_revalidate(struct dentry *dentry, unsigned int flags)
include/linux/fs.h +1 -0
Index: linux-2.6.git/include/linux/fs.h
===================================================================
@@ -1552,6 +1552,7 @@ struct file_operations {
 	long (*fallocate)(struct file *file, int mode, loff_t offset,
 			  loff_t len);
 	int (*show_fdinfo)(struct seq_file *m, struct file *f);
+	struct seq_operations *fdinfo_ops;
 };
 
 struct inode_operations {