From: Petr Grillinger Subject: procfs proc_write_file bug Date: Fri, 19 Nov 2004 15:12:07 +0100 Message-ID: <419DFF37.5040401@kiv.zcu.cz> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------060207010705050507080007" Return-path: Received: from venus.vmars.tuwien.ac.at ([128.130.39.30]:47108 "EHLO venus.vmars.tuwien.ac.at") by vger.kernel.org with ESMTP id S261420AbUKSOL6 (ORCPT ); Fri, 19 Nov 2004 09:11:58 -0500 Received: from [128.130.39.96] (fit.vmars.tuwien.ac.at [128.130.39.96]) by venus.vmars.tuwien.ac.at (8.8.8/8.8.8) with ESMTP id PAA16155 for ; Fri, 19 Nov 2004 15:11:57 +0100 (MET) To: linux-fsdevel@vger.kernel.org Sender: linux-fsdevel-owner@vger.kernel.org List-Id: linux-fsdevel.vger.kernel.org This is a multi-part message in MIME format. --------------060207010705050507080007 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Hello, I needed recently to write a large file into the /proc FS, there is however a bug that makes this impossible. I am working with 2.4.25 kernel, but the bug is present even in the 2.6 line. The problem is that the proc_file_write function (in fs/proc/generic.c) does not pass the file position (loff_r *ppos) to the registered handling function - it cannot. The prototype for this function (defined in include/linux/proc_fs.h) does not include the needed argument. I have made a patch that solves this problem by adding another hook for an alternative write handler. This does not break old code using the procfs write function and allows to write large files properly. It is however not "very nice". Do you have some ideas how to solve this problem? (patch is attached) Petr Grillinger --------------060207010705050507080007 Content-Type: text/plain; name="linux-2.4.25-pg.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="linux-2.4.25-pg.patch" diff -uNrp linux-2.4.25-rtai/fs/proc/generic.c linux-2.4.25-pg/fs/proc/generic.c --- linux-2.4.25-rtai/fs/proc/generic.c 2003-11-28 19:26:21.000000000 +0100 +++ linux-2.4.25-pg/fs/proc/generic.c 2004-11-19 14:20:40.000000000 +0100 @@ -120,20 +120,38 @@ proc_file_read(struct file * file, char return retval; } -static ssize_t -proc_file_write(struct file * file, const char * buffer, - size_t count, loff_t *ppos) -{ - struct inode *inode = file->f_dentry->d_inode; - struct proc_dir_entry * dp; + +/* + * Added support for files larger than one PAGE + * 2004/11/19, Petr Grillinger, pgrillin@kiv.zcu.cz + * + * The old 'write_proc' entry doesn't allow to write files larger than one + * PAGE. A new 'write_proc2' entry can be used to implement an alternative + * handler - the new version handler gets an additional argument (ppos) which + * is a pointer to the current file position. The handler must update this + * position after each successful write operation - if there are more data from + * the user-space, it will be called again. + * + * The old interface is available with no changes. The new interface can be + * used by assigning a function address to the write_proc2 field of + * proc_dir_entry. If a new interface function is available it is used instead + * of the old one. + * + * The handler should return the number of written bytes or a negative value + * for error conditions. + */ +static ssize_t proc_file_write(struct file * file, const char * buffer, size_t + count, loff_t *ppos) { struct inode *inode = file->f_dentry->d_inode; + struct proc_dir_entry * dp; dp = (struct proc_dir_entry *) inode->u.generic_ip; - - if (!dp->write_proc) - return -EIO; - - /* FIXME: does this routine need ppos? probably... */ - return dp->write_proc(file, buffer, count, dp->data); + + if (dp->write_proc2) /* new interface */ + return dp->write_proc2(file, buffer, ppos, count, dp->data); + if (dp->write_proc) /* old interface */ + return dp->write_proc(file, buffer, count, dp->data); + + return -EIO; } diff -uNrp linux-2.4.25-rtai/include/linux/proc_fs.h linux-2.4.25-pg/include/linux/proc_fs.h --- linux-2.4.25-rtai/include/linux/proc_fs.h 2003-11-28 19:26:21.000000000 +0100 +++ linux-2.4.25-pg/include/linux/proc_fs.h 2004-11-19 14:10:56.000000000 +0100 @@ -48,6 +48,13 @@ typedef int (read_proc_t)(char *page, ch int count, int *eof, void *data); typedef int (write_proc_t)(struct file *file, const char *buffer, unsigned long count, void *data); +/* A write function must have the ppos pointer to be able to access + * larger files than one page. Not including it was a bug which + * cannot be corrected without breaking old code. That is why there + * is a new write_proc2 entry in the proc_dir_entry structure that + * uses this corrected prototype. */ +typedef int (write_proc2_t)(struct file *file, const char *buffer, + loff_t *ppos, unsigned long count, void *data); typedef int (get_info_t)(char *, char **, off_t, int); struct proc_dir_entry { @@ -67,6 +74,7 @@ struct proc_dir_entry { void *data; read_proc_t *read_proc; write_proc_t *write_proc; + write_proc2_t *write_proc2; atomic_t count; /* use count */ int deleted; /* delete flag */ kdev_t rdev; --------------060207010705050507080007--