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 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 23 | struct bin_buffer { |
| 24 | struct mutex mutex; |
| 25 | void *buffer; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 26 | int mmapped; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 27 | }; |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | static int |
| 30 | fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count) |
| 31 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 32 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
| 33 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 34 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
| 35 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 37 | /* need attr_sd for attr, its parent for kobj */ |
| 38 | if (!sysfs_get_active_two(attr_sd)) |
| 39 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 41 | rc = -EIO; |
| 42 | if (attr->read) |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 43 | rc = attr->read(kobj, attr, buffer, off, count); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 44 | |
| 45 | sysfs_put_active_two(attr_sd); |
| 46 | |
| 47 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static ssize_t |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 51 | 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] | 52 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 53 | struct bin_buffer *bb = file->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 54 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | int size = dentry->d_inode->i_size; |
| 56 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 57 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | if (size) { |
| 60 | if (offs > size) |
| 61 | return 0; |
| 62 | if (offs + count > size) |
| 63 | count = size - offs; |
| 64 | } |
| 65 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 66 | mutex_lock(&bb->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 68 | count = fill_read(dentry, bb->buffer, offs, count); |
| 69 | if (count < 0) |
| 70 | goto out_unlock; |
| 71 | |
| 72 | if (copy_to_user(userbuf, bb->buffer, count)) { |
| 73 | count = -EFAULT; |
| 74 | goto out_unlock; |
| 75 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 77 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | *off = offs + count; |
| 80 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 81 | out_unlock: |
| 82 | mutex_unlock(&bb->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | return count; |
| 84 | } |
| 85 | |
| 86 | static int |
| 87 | flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count) |
| 88 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 89 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
| 90 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 91 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
| 92 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 94 | /* need attr_sd for attr, its parent for kobj */ |
| 95 | if (!sysfs_get_active_two(attr_sd)) |
| 96 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 98 | rc = -EIO; |
| 99 | if (attr->write) |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 100 | rc = attr->write(kobj, attr, buffer, offset, count); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 101 | |
| 102 | sysfs_put_active_two(attr_sd); |
| 103 | |
| 104 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 107 | static ssize_t write(struct file *file, const char __user *userbuf, |
| 108 | size_t bytes, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 110 | struct bin_buffer *bb = file->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 111 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | int size = dentry->d_inode->i_size; |
| 113 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 114 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (size) { |
| 117 | if (offs > size) |
| 118 | return 0; |
| 119 | if (offs + count > size) |
| 120 | count = size - offs; |
| 121 | } |
| 122 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 123 | mutex_lock(&bb->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 125 | if (copy_from_user(bb->buffer, userbuf, count)) { |
| 126 | count = -EFAULT; |
| 127 | goto out_unlock; |
| 128 | } |
| 129 | |
| 130 | count = flush_write(dentry, bb->buffer, offs, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | if (count > 0) |
| 132 | *off = offs + count; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 133 | |
| 134 | out_unlock: |
| 135 | mutex_unlock(&bb->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | return count; |
| 137 | } |
| 138 | |
| 139 | static int mmap(struct file *file, struct vm_area_struct *vma) |
| 140 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 141 | struct bin_buffer *bb = file->private_data; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 142 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 143 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 144 | struct kobject *kobj = attr_sd->s_parent->s_elem.dir.kobj; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 145 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 147 | mutex_lock(&bb->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 148 | |
| 149 | /* need attr_sd for attr, its parent for kobj */ |
| 150 | if (!sysfs_get_active_two(attr_sd)) |
| 151 | return -ENODEV; |
| 152 | |
| 153 | rc = -EINVAL; |
| 154 | if (attr->mmap) |
| 155 | rc = attr->mmap(kobj, attr, vma); |
| 156 | |
| 157 | if (rc == 0 && !bb->mmapped) |
| 158 | bb->mmapped = 1; |
| 159 | else |
| 160 | sysfs_put_active_two(attr_sd); |
| 161 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 162 | mutex_unlock(&bb->mutex); |
| 163 | |
| 164 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static int open(struct inode * inode, struct file * file) |
| 168 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 169 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 170 | struct bin_attribute *attr = attr_sd->s_elem.bin_attr.bin_attr; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 171 | struct bin_buffer *bb = NULL; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 172 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 174 | /* need attr_sd for attr */ |
| 175 | if (!sysfs_get_active(attr_sd)) |
| 176 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | error = -EACCES; |
| 179 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 180 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 182 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | |
| 184 | error = -ENOMEM; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 185 | bb = kzalloc(sizeof(*bb), GFP_KERNEL); |
| 186 | if (!bb) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 187 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 189 | bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 190 | if (!bb->buffer) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 191 | goto err_out; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 192 | |
| 193 | mutex_init(&bb->mutex); |
| 194 | file->private_data = bb; |
| 195 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 196 | /* open succeeded, put active reference and pin attr_sd */ |
| 197 | sysfs_put_active(attr_sd); |
| 198 | sysfs_get(attr_sd); |
| 199 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 201 | err_out: |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 202 | sysfs_put_active(attr_sd); |
| 203 | kfree(bb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | return error; |
| 205 | } |
| 206 | |
| 207 | static int release(struct inode * inode, struct file * file) |
| 208 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 209 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 210 | struct bin_buffer *bb = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 212 | if (bb->mmapped) |
| 213 | sysfs_put_active_two(attr_sd); |
| 214 | sysfs_put(attr_sd); |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 215 | kfree(bb->buffer); |
| 216 | kfree(bb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | return 0; |
| 218 | } |
| 219 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 220 | const struct file_operations bin_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | .read = read, |
| 222 | .write = write, |
| 223 | .mmap = mmap, |
| 224 | .llseek = generic_file_llseek, |
| 225 | .open = open, |
| 226 | .release = release, |
| 227 | }; |
| 228 | |
| 229 | /** |
| 230 | * sysfs_create_bin_file - create binary file for object. |
| 231 | * @kobj: object. |
| 232 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | */ |
| 234 | |
| 235 | int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
| 236 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 237 | BUG_ON(!kobj || !kobj->sd || !attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 239 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | |
| 243 | /** |
| 244 | * sysfs_remove_bin_file - remove binary file for object. |
| 245 | * @kobj: object. |
| 246 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | */ |
| 248 | |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 249 | void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | { |
Alan Stern | 5f1835d | 2007-08-16 16:13:06 -0400 | [diff] [blame^] | 251 | sysfs_hash_and_remove(kobj->sd, attr->attr.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); |
| 255 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |