Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * bin.c - binary file operations for sysfs. |
| 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Matthew Wilcox |
| 6 | * Copyright (c) 2004 Silicon Graphics, Inc. |
| 7 | */ |
| 8 | |
| 9 | #undef DEBUG |
| 10 | |
| 11 | #include <linux/errno.h> |
| 12 | #include <linux/fs.h> |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 13 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/kobject.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/slab.h> |
| 17 | |
| 18 | #include <asm/uaccess.h> |
Oliver Neukum | 94bebf4 | 2006-12-20 10:52:44 +0100 | [diff] [blame] | 19 | #include <asm/semaphore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | #include "sysfs.h" |
| 22 | |
| 23 | static int |
| 24 | fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count) |
| 25 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 26 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
| 27 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | struct kobject * kobj = to_kobj(dentry->d_parent); |
| 29 | |
| 30 | if (!attr->read) |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 31 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
| 33 | return attr->read(kobj, buffer, off, count); |
| 34 | } |
| 35 | |
| 36 | static ssize_t |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 37 | read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | { |
| 39 | char *buffer = file->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 40 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | int size = dentry->d_inode->i_size; |
| 42 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 43 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | if (size) { |
| 46 | if (offs > size) |
| 47 | return 0; |
| 48 | if (offs + count > size) |
| 49 | count = size - offs; |
| 50 | } |
| 51 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 52 | count = fill_read(dentry, buffer, offs, count); |
| 53 | if (count < 0) |
| 54 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
| 56 | if (copy_to_user(userbuf, buffer, count)) |
| 57 | return -EFAULT; |
| 58 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 59 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | |
| 61 | *off = offs + count; |
| 62 | |
| 63 | return count; |
| 64 | } |
| 65 | |
| 66 | static int |
| 67 | flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count) |
| 68 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 69 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
| 70 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | struct kobject *kobj = to_kobj(dentry->d_parent); |
| 72 | |
| 73 | if (!attr->write) |
Dmitry Torokhov | c76d0ab | 2005-04-29 01:22:00 -0500 | [diff] [blame] | 74 | return -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
| 76 | return attr->write(kobj, buffer, offset, count); |
| 77 | } |
| 78 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 79 | static ssize_t write(struct file *file, const char __user *userbuf, |
| 80 | size_t bytes, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
| 82 | char *buffer = file->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 83 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | int size = dentry->d_inode->i_size; |
| 85 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 86 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | if (size) { |
| 89 | if (offs > size) |
| 90 | return 0; |
| 91 | if (offs + count > size) |
| 92 | count = size - offs; |
| 93 | } |
| 94 | |
| 95 | if (copy_from_user(buffer, userbuf, count)) |
| 96 | return -EFAULT; |
| 97 | |
| 98 | count = flush_write(dentry, buffer, offs, count); |
| 99 | if (count > 0) |
| 100 | *off = offs + count; |
| 101 | return count; |
| 102 | } |
| 103 | |
| 104 | static int mmap(struct file *file, struct vm_area_struct *vma) |
| 105 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 106 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 107 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
| 108 | struct kobject *kobj = to_kobj(file->f_path.dentry->d_parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | if (!attr->mmap) |
| 111 | return -EINVAL; |
| 112 | |
| 113 | return attr->mmap(kobj, attr, vma); |
| 114 | } |
| 115 | |
| 116 | static int open(struct inode * inode, struct file * file) |
| 117 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 118 | struct kobject *kobj = sysfs_get_kobject(file->f_path.dentry->d_parent); |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 119 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 120 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | int error = -EINVAL; |
| 122 | |
| 123 | if (!kobj || !attr) |
| 124 | goto Done; |
| 125 | |
| 126 | /* Grab the module reference for this attribute if we have one */ |
| 127 | error = -ENODEV; |
| 128 | if (!try_module_get(attr->attr.owner)) |
| 129 | goto Done; |
| 130 | |
| 131 | error = -EACCES; |
| 132 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) |
| 133 | goto Error; |
| 134 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
| 135 | goto Error; |
| 136 | |
| 137 | error = -ENOMEM; |
| 138 | file->private_data = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 139 | if (!file->private_data) |
| 140 | goto Error; |
| 141 | |
| 142 | error = 0; |
| 143 | goto Done; |
| 144 | |
| 145 | Error: |
| 146 | module_put(attr->attr.owner); |
| 147 | Done: |
Mariusz Kozlowski | f750653 | 2007-01-02 13:41:10 +0100 | [diff] [blame] | 148 | if (error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | kobject_put(kobj); |
| 150 | return error; |
| 151 | } |
| 152 | |
| 153 | static int release(struct inode * inode, struct file * file) |
| 154 | { |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 155 | struct kobject * kobj = to_kobj(file->f_path.dentry->d_parent); |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame^] | 156 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 157 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | u8 * buffer = file->private_data; |
| 159 | |
Mariusz Kozlowski | f750653 | 2007-01-02 13:41:10 +0100 | [diff] [blame] | 160 | kobject_put(kobj); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | module_put(attr->attr.owner); |
| 162 | kfree(buffer); |
| 163 | return 0; |
| 164 | } |
| 165 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 166 | const struct file_operations bin_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | .read = read, |
| 168 | .write = write, |
| 169 | .mmap = mmap, |
| 170 | .llseek = generic_file_llseek, |
| 171 | .open = open, |
| 172 | .release = release, |
| 173 | }; |
| 174 | |
| 175 | /** |
| 176 | * sysfs_create_bin_file - create binary file for object. |
| 177 | * @kobj: object. |
| 178 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | */ |
| 180 | |
| 181 | int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
| 182 | { |
| 183 | BUG_ON(!kobj || !kobj->dentry || !attr); |
| 184 | |
| 185 | return sysfs_add_file(kobj->dentry, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | /** |
| 190 | * sysfs_remove_bin_file - remove binary file for object. |
| 191 | * @kobj: object. |
| 192 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | */ |
| 194 | |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 195 | void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | { |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 197 | if (sysfs_hash_and_remove(kobj->dentry, attr->attr.name) < 0) { |
| 198 | printk(KERN_ERR "%s: " |
| 199 | "bad dentry or inode or no such file: \"%s\"\n", |
| 200 | __FUNCTION__, attr->attr.name); |
| 201 | dump_stack(); |
| 202 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); |
| 206 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |