Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 2 | * fs/sysfs/bin.c - sysfs binary file implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003 Patrick Mochel |
| 5 | * Copyright (c) 2003 Matthew Wilcox |
| 6 | * Copyright (c) 2004 Silicon Graphics, Inc. |
Tejun Heo | 6d66f5c | 2007-09-20 17:31:38 +0900 | [diff] [blame] | 7 | * Copyright (c) 2007 SUSE Linux Products GmbH |
| 8 | * Copyright (c) 2007 Tejun Heo <teheo@suse.de> |
| 9 | * |
| 10 | * This file is released under the GPLv2. |
| 11 | * |
| 12 | * Please see Documentation/filesystems/sysfs.txt for more information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #undef DEBUG |
| 16 | |
| 17 | #include <linux/errno.h> |
| 18 | #include <linux/fs.h> |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/kobject.h> |
| 21 | #include <linux/module.h> |
| 22 | #include <linux/slab.h> |
Dave Young | 869512a | 2007-07-26 14:53:53 +0000 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 24 | #include <linux/mm.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
| 26 | #include <asm/uaccess.h> |
| 27 | |
| 28 | #include "sysfs.h" |
| 29 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 30 | /* |
| 31 | * There's one bin_buffer for each open file. |
| 32 | * |
| 33 | * filp->private_data points to bin_buffer and |
| 34 | * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s |
| 35 | * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock |
| 36 | */ |
| 37 | static DEFINE_MUTEX(sysfs_bin_lock); |
| 38 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 39 | struct bin_buffer { |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 40 | struct mutex mutex; |
| 41 | void *buffer; |
| 42 | int mmapped; |
| 43 | struct vm_operations_struct *vm_ops; |
| 44 | struct file *file; |
| 45 | struct hlist_node list; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 46 | }; |
| 47 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | static int |
| 49 | fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count) |
| 50 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 51 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 52 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
| 53 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 54 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 56 | /* need attr_sd for attr, its parent for kobj */ |
| 57 | if (!sysfs_get_active_two(attr_sd)) |
| 58 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 60 | rc = -EIO; |
| 61 | if (attr->read) |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 62 | rc = attr->read(kobj, attr, buffer, off, count); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 63 | |
| 64 | sysfs_put_active_two(attr_sd); |
| 65 | |
| 66 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static ssize_t |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 70 | 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] | 71 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 72 | struct bin_buffer *bb = file->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 73 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | int size = dentry->d_inode->i_size; |
| 75 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 76 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 77 | char *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Greg Kroah-Hartman | 4503efd | 2009-01-20 15:51:16 -0800 | [diff] [blame] | 79 | if (!bytes) |
| 80 | return 0; |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | if (size) { |
| 83 | if (offs > size) |
| 84 | return 0; |
| 85 | if (offs + count > size) |
| 86 | count = size - offs; |
| 87 | } |
| 88 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 89 | temp = kmalloc(count, GFP_KERNEL); |
| 90 | if (!temp) |
| 91 | return -ENOMEM; |
| 92 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 93 | mutex_lock(&bb->mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 95 | count = fill_read(dentry, bb->buffer, offs, count); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 96 | if (count < 0) { |
| 97 | mutex_unlock(&bb->mutex); |
| 98 | goto out_free; |
| 99 | } |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 100 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 101 | memcpy(temp, bb->buffer, count); |
| 102 | |
| 103 | mutex_unlock(&bb->mutex); |
| 104 | |
| 105 | if (copy_to_user(userbuf, temp, count)) { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 106 | count = -EFAULT; |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 107 | goto out_free; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 110 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | *off = offs + count; |
| 113 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 114 | out_free: |
| 115 | kfree(temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | return count; |
| 117 | } |
| 118 | |
| 119 | static int |
| 120 | flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count) |
| 121 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 122 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 123 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
| 124 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 125 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 127 | /* need attr_sd for attr, its parent for kobj */ |
| 128 | if (!sysfs_get_active_two(attr_sd)) |
| 129 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 131 | rc = -EIO; |
| 132 | if (attr->write) |
Zhang Rui | 91a6902 | 2007-06-09 13:57:22 +0800 | [diff] [blame] | 133 | rc = attr->write(kobj, attr, buffer, offset, count); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 134 | |
| 135 | sysfs_put_active_two(attr_sd); |
| 136 | |
| 137 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 140 | static ssize_t write(struct file *file, const char __user *userbuf, |
| 141 | size_t bytes, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 143 | struct bin_buffer *bb = file->private_data; |
Josef "Jeff" Sipek | f427f5d | 2006-12-08 02:36:36 -0800 | [diff] [blame] | 144 | struct dentry *dentry = file->f_path.dentry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | int size = dentry->d_inode->i_size; |
| 146 | loff_t offs = *off; |
Tejun Heo | 93e3cd82 | 2007-06-14 03:45:13 +0900 | [diff] [blame] | 147 | int count = min_t(size_t, bytes, PAGE_SIZE); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 148 | char *temp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 149 | |
Greg Kroah-Hartman | 4503efd | 2009-01-20 15:51:16 -0800 | [diff] [blame] | 150 | if (!bytes) |
| 151 | return 0; |
| 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | if (size) { |
| 154 | if (offs > size) |
| 155 | return 0; |
| 156 | if (offs + count > size) |
| 157 | count = size - offs; |
| 158 | } |
| 159 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 160 | temp = kmalloc(count, GFP_KERNEL); |
| 161 | if (!temp) |
| 162 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 164 | if (copy_from_user(temp, userbuf, count)) { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 165 | count = -EFAULT; |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 166 | goto out_free; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 167 | } |
| 168 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 169 | mutex_lock(&bb->mutex); |
| 170 | |
| 171 | memcpy(bb->buffer, temp, count); |
| 172 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 173 | count = flush_write(dentry, bb->buffer, offs, count); |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 174 | mutex_unlock(&bb->mutex); |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | if (count > 0) |
| 177 | *off = offs + count; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 178 | |
Nick Piggin | b31ca3f | 2008-09-12 11:24:11 +0200 | [diff] [blame] | 179 | out_free: |
| 180 | kfree(temp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | return count; |
| 182 | } |
| 183 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 184 | static void bin_vma_open(struct vm_area_struct *vma) |
| 185 | { |
| 186 | struct file *file = vma->vm_file; |
| 187 | struct bin_buffer *bb = file->private_data; |
| 188 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 189 | |
| 190 | if (!bb->vm_ops || !bb->vm_ops->open) |
| 191 | return; |
| 192 | |
| 193 | if (!sysfs_get_active_two(attr_sd)) |
| 194 | return; |
| 195 | |
| 196 | bb->vm_ops->open(vma); |
| 197 | |
| 198 | sysfs_put_active_two(attr_sd); |
| 199 | } |
| 200 | |
| 201 | static void bin_vma_close(struct vm_area_struct *vma) |
| 202 | { |
| 203 | struct file *file = vma->vm_file; |
| 204 | struct bin_buffer *bb = file->private_data; |
| 205 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 206 | |
| 207 | if (!bb->vm_ops || !bb->vm_ops->close) |
| 208 | return; |
| 209 | |
| 210 | if (!sysfs_get_active_two(attr_sd)) |
| 211 | return; |
| 212 | |
| 213 | bb->vm_ops->close(vma); |
| 214 | |
| 215 | sysfs_put_active_two(attr_sd); |
| 216 | } |
| 217 | |
| 218 | static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 219 | { |
| 220 | struct file *file = vma->vm_file; |
| 221 | struct bin_buffer *bb = file->private_data; |
| 222 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 223 | int ret; |
| 224 | |
| 225 | if (!bb->vm_ops || !bb->vm_ops->fault) |
| 226 | return VM_FAULT_SIGBUS; |
| 227 | |
| 228 | if (!sysfs_get_active_two(attr_sd)) |
| 229 | return VM_FAULT_SIGBUS; |
| 230 | |
| 231 | ret = bb->vm_ops->fault(vma, vmf); |
| 232 | |
| 233 | sysfs_put_active_two(attr_sd); |
| 234 | return ret; |
| 235 | } |
| 236 | |
| 237 | static int bin_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 238 | { |
| 239 | struct file *file = vma->vm_file; |
| 240 | struct bin_buffer *bb = file->private_data; |
| 241 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 242 | int ret; |
| 243 | |
| 244 | if (!bb->vm_ops || !bb->vm_ops->page_mkwrite) |
| 245 | return -EINVAL; |
| 246 | |
| 247 | if (!sysfs_get_active_two(attr_sd)) |
| 248 | return -EINVAL; |
| 249 | |
| 250 | ret = bb->vm_ops->page_mkwrite(vma, page); |
| 251 | |
| 252 | sysfs_put_active_two(attr_sd); |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static int bin_access(struct vm_area_struct *vma, unsigned long addr, |
| 257 | void *buf, int len, int write) |
| 258 | { |
| 259 | struct file *file = vma->vm_file; |
| 260 | struct bin_buffer *bb = file->private_data; |
| 261 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
| 262 | int ret; |
| 263 | |
| 264 | if (!bb->vm_ops || !bb->vm_ops->access) |
| 265 | return -EINVAL; |
| 266 | |
| 267 | if (!sysfs_get_active_two(attr_sd)) |
| 268 | return -EINVAL; |
| 269 | |
| 270 | ret = bb->vm_ops->access(vma, addr, buf, len, write); |
| 271 | |
| 272 | sysfs_put_active_two(attr_sd); |
| 273 | return ret; |
| 274 | } |
| 275 | |
| 276 | static struct vm_operations_struct bin_vm_ops = { |
| 277 | .open = bin_vma_open, |
| 278 | .close = bin_vma_close, |
| 279 | .fault = bin_fault, |
| 280 | .page_mkwrite = bin_page_mkwrite, |
| 281 | .access = bin_access, |
| 282 | }; |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | static int mmap(struct file *file, struct vm_area_struct *vma) |
| 285 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 286 | struct bin_buffer *bb = file->private_data; |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 287 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 288 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
| 289 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 290 | struct vm_operations_struct *vm_ops; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 291 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 293 | mutex_lock(&bb->mutex); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 294 | |
| 295 | /* need attr_sd for attr, its parent for kobj */ |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 296 | rc = -ENODEV; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 297 | if (!sysfs_get_active_two(attr_sd)) |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 298 | goto out_unlock; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 299 | |
| 300 | rc = -EINVAL; |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 301 | if (!attr->mmap) |
| 302 | goto out_put; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 303 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 304 | rc = attr->mmap(kobj, attr, vma); |
| 305 | vm_ops = vma->vm_ops; |
| 306 | vma->vm_ops = &bin_vm_ops; |
| 307 | if (rc) |
| 308 | goto out_put; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 309 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 310 | rc = -EINVAL; |
| 311 | if (bb->mmapped && bb->vm_ops != vma->vm_ops) |
| 312 | goto out_put; |
| 313 | |
| 314 | #ifdef CONFIG_NUMA |
| 315 | rc = -EINVAL; |
| 316 | if (vm_ops && ((vm_ops->set_policy || vm_ops->get_policy || vm_ops->migrate))) |
| 317 | goto out_put; |
| 318 | #endif |
| 319 | |
| 320 | rc = 0; |
| 321 | bb->mmapped = 1; |
| 322 | bb->vm_ops = vm_ops; |
| 323 | out_put: |
| 324 | sysfs_put_active_two(attr_sd); |
| 325 | out_unlock: |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 326 | mutex_unlock(&bb->mutex); |
| 327 | |
| 328 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | static int open(struct inode * inode, struct file * file) |
| 332 | { |
Tejun Heo | 3e51903 | 2007-06-14 03:45:15 +0900 | [diff] [blame] | 333 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
Tejun Heo | b1fc3d6 | 2007-09-20 16:05:11 +0900 | [diff] [blame] | 334 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 335 | struct bin_buffer *bb = NULL; |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 336 | int error; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Tejun Heo | 078ce64 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 338 | /* binary file operations requires both @sd and its parent */ |
| 339 | if (!sysfs_get_active_two(attr_sd)) |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 340 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | error = -EACCES; |
| 343 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 344 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 346 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | error = -ENOMEM; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 349 | bb = kzalloc(sizeof(*bb), GFP_KERNEL); |
| 350 | if (!bb) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 351 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 353 | bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 354 | if (!bb->buffer) |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 355 | goto err_out; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 356 | |
| 357 | mutex_init(&bb->mutex); |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 358 | bb->file = file; |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 359 | file->private_data = bb; |
| 360 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 361 | mutex_lock(&sysfs_bin_lock); |
| 362 | hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers); |
| 363 | mutex_unlock(&sysfs_bin_lock); |
| 364 | |
Tejun Heo | 078ce64 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 365 | /* open succeeded, put active references */ |
| 366 | sysfs_put_active_two(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 367 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | |
Tejun Heo | 7b59575 | 2007-06-14 03:45:17 +0900 | [diff] [blame] | 369 | err_out: |
Tejun Heo | 078ce64 | 2007-09-20 16:05:10 +0900 | [diff] [blame] | 370 | sysfs_put_active_two(attr_sd); |
Tejun Heo | 0ab6608 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 371 | kfree(bb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return error; |
| 373 | } |
| 374 | |
| 375 | static int release(struct inode * inode, struct file * file) |
| 376 | { |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 377 | struct bin_buffer *bb = file->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 379 | mutex_lock(&sysfs_bin_lock); |
| 380 | hlist_del(&bb->list); |
| 381 | mutex_unlock(&sysfs_bin_lock); |
| 382 | |
Tejun Heo | eb36165 | 2007-06-14 03:45:16 +0900 | [diff] [blame] | 383 | kfree(bb->buffer); |
| 384 | kfree(bb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 388 | const struct file_operations bin_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | .read = read, |
| 390 | .write = write, |
| 391 | .mmap = mmap, |
| 392 | .llseek = generic_file_llseek, |
| 393 | .open = open, |
| 394 | .release = release, |
| 395 | }; |
| 396 | |
Eric W. Biederman | e0edd3c | 2009-03-04 11:57:20 -0800 | [diff] [blame^] | 397 | |
| 398 | void unmap_bin_file(struct sysfs_dirent *attr_sd) |
| 399 | { |
| 400 | struct bin_buffer *bb; |
| 401 | struct hlist_node *tmp; |
| 402 | |
| 403 | if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR) |
| 404 | return; |
| 405 | |
| 406 | mutex_lock(&sysfs_bin_lock); |
| 407 | |
| 408 | hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) { |
| 409 | struct inode *inode = bb->file->f_path.dentry->d_inode; |
| 410 | |
| 411 | unmap_mapping_range(inode->i_mapping, 0, 0, 1); |
| 412 | } |
| 413 | |
| 414 | mutex_unlock(&sysfs_bin_lock); |
| 415 | } |
| 416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | /** |
| 418 | * sysfs_create_bin_file - create binary file for object. |
| 419 | * @kobj: object. |
| 420 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | */ |
| 422 | |
| 423 | int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
| 424 | { |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 425 | BUG_ON(!kobj || !kobj->sd || !attr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
Tejun Heo | 608e266 | 2007-06-14 04:27:22 +0900 | [diff] [blame] | 427 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
| 430 | |
| 431 | /** |
| 432 | * sysfs_remove_bin_file - remove binary file for object. |
| 433 | * @kobj: object. |
| 434 | * @attr: attribute descriptor. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 435 | */ |
| 436 | |
Randy.Dunlap | 995982c | 2006-07-10 23:05:25 -0700 | [diff] [blame] | 437 | void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | { |
Alan Stern | 5f1835d | 2007-08-16 16:13:06 -0400 | [diff] [blame] | 439 | sysfs_hash_and_remove(kobj->sd, attr->attr.name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); |
| 443 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |