Commit c189bfeb authored by Patrick Mochel's avatar Patrick Mochel

sysfs: Fix binary file handling

From Manuel Estrada Sainz <ranty@debian.org>


>   - sysfs-bin-flexible-size.diff:
>       Make dynamically sized files possible.  And return the right
>       value on successful write.
>
>   - sysfs-bin-lost-dget.diff:
>       I was having trouble when calling request_firmware() from a work
>       queue, and after a little investigations it seams that this dget
>       got lost along the way. Adding it back fixed the issue.
>       Or am I causing a dentry leak now?
parent e8596243
...@@ -30,10 +30,15 @@ read(struct file * file, char * userbuf, size_t count, loff_t * off) ...@@ -30,10 +30,15 @@ read(struct file * file, char * userbuf, size_t count, loff_t * off)
loff_t offs = *off; loff_t offs = *off;
int ret; int ret;
if (offs > size) if (count > PAGE_SIZE)
return 0; count = PAGE_SIZE;
if (offs + count > size)
count = size - offs; if (size) {
if (offs > size)
return 0;
if (offs + count > size)
count = size - offs;
}
ret = fill_read(dentry, buffer, offs, count); ret = fill_read(dentry, buffer, offs, count);
if (ret < 0) if (ret < 0)
...@@ -41,7 +46,7 @@ read(struct file * file, char * userbuf, size_t count, loff_t * off) ...@@ -41,7 +46,7 @@ read(struct file * file, char * userbuf, size_t count, loff_t * off)
count = ret; count = ret;
ret = -EFAULT; ret = -EFAULT;
if (copy_to_user(userbuf, buffer + offs, count) != 0) if (copy_to_user(userbuf, buffer, count) != 0)
goto Done; goto Done;
*off = offs + count; *off = offs + count;
...@@ -69,19 +74,23 @@ static ssize_t write(struct file * file, const char * userbuf, ...@@ -69,19 +74,23 @@ static ssize_t write(struct file * file, const char * userbuf,
loff_t offs = *off; loff_t offs = *off;
int ret; int ret;
if (offs > size) if (count > PAGE_SIZE)
return 0; count = PAGE_SIZE;
if (offs + count > size) if (size) {
count = size - offs; if (offs > size)
return 0;
if (offs + count > size)
count = size - offs;
}
ret = -EFAULT; ret = -EFAULT;
if (copy_from_user(buffer + offs, userbuf, count)) if (copy_from_user(buffer, userbuf, count))
goto Done; goto Done;
count = flush_write(dentry, buffer, offs, count); count = flush_write(dentry, buffer, offs, count);
if (count > 0) if (count > 0)
*off = offs + count; *off = offs + count;
ret = 0; ret = count;
Done: Done:
return ret; return ret;
} }
...@@ -102,7 +111,7 @@ static int open(struct inode * inode, struct file * file) ...@@ -102,7 +111,7 @@ static int open(struct inode * inode, struct file * file)
goto Done; goto Done;
error = -ENOMEM; error = -ENOMEM;
file->private_data = kmalloc(attr->size, GFP_KERNEL); file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL);
if (!file->private_data) if (!file->private_data)
goto Done; goto Done;
......
...@@ -60,9 +60,10 @@ int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *)) ...@@ -60,9 +60,10 @@ int sysfs_create(struct dentry * dentry, int mode, int (*init)(struct inode *))
Proceed: Proceed:
if (init) if (init)
error = init(inode); error = init(inode);
if (!error) if (!error) {
d_instantiate(dentry, inode); d_instantiate(dentry, inode);
else dget(dentry); /* Extra count - pin the dentry in core */
} else
iput(inode); iput(inode);
Done: Done:
return error; return error;
......
...@@ -23,6 +23,9 @@ struct bin_attribute { ...@@ -23,6 +23,9 @@ struct bin_attribute {
ssize_t (*write)(struct kobject *, char *, loff_t, size_t); ssize_t (*write)(struct kobject *, char *, loff_t, size_t);
}; };
int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr);
int sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr);
struct sysfs_ops { struct sysfs_ops {
ssize_t (*show)(struct kobject *, struct attribute *,char *); ssize_t (*show)(struct kobject *, struct attribute *,char *);
ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t); ssize_t (*store)(struct kobject *,struct attribute *,const char *, size_t);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment