blob: 4e321f7353fa3e329134087ad8728a19a097a541 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Tejun Heo6d66f5c2007-09-20 17:31:38 +09002 * fs/sysfs/bin.c - sysfs binary file implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
Tejun Heo6d66f5c2007-09-20 17:31:38 +09007 * 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 Torvalds1da177e2005-04-16 15:20:36 -070013 */
14
15#undef DEBUG
16
17#include <linux/errno.h>
18#include <linux/fs.h>
Randy.Dunlap995982c2006-07-10 23:05:25 -070019#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/kobject.h>
21#include <linux/module.h>
22#include <linux/slab.h>
Dave Young869512a2007-07-26 14:53:53 +000023#include <linux/mutex.h>
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080024#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <asm/uaccess.h>
27
28#include "sysfs.h"
29
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080030/*
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 */
37static DEFINE_MUTEX(sysfs_bin_lock);
38
Tejun Heoeb361652007-06-14 03:45:16 +090039struct bin_buffer {
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080040 struct mutex mutex;
41 void *buffer;
42 int mmapped;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040043 const struct vm_operations_struct *vm_ops;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080044 struct file *file;
45 struct hlist_node list;
Tejun Heoeb361652007-06-14 03:45:16 +090046};
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048static int
Chris Wright2c3c8be2010-05-12 18:28:57 -070049fill_read(struct file *file, char *buffer, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070050{
Chris Wright2c3c8be2010-05-12 18:28:57 -070051 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090052 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
53 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +090054 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Tejun Heo0ab66082007-06-14 03:45:16 +090056 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -080057 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +090058 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
Tejun Heo0ab66082007-06-14 03:45:16 +090060 rc = -EIO;
61 if (attr->read)
Chris Wright2c3c8be2010-05-12 18:28:57 -070062 rc = attr->read(file, kobj, attr, buffer, off, count);
Tejun Heo0ab66082007-06-14 03:45:16 +090063
Eric W. Biedermane72ceb82010-02-11 15:18:38 -080064 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +090065
66 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
69static ssize_t
Tejun Heo93e3cd822007-06-14 03:45:13 +090070read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Tejun Heoeb361652007-06-14 03:45:16 +090072 struct bin_buffer *bb = file->private_data;
Chris Wright2c3c8be2010-05-12 18:28:57 -070073 int size = file->f_path.dentry->d_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +090075 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020076 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -080078 if (!bytes)
79 return 0;
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (size) {
82 if (offs > size)
83 return 0;
84 if (offs + count > size)
85 count = size - offs;
86 }
87
Nick Pigginb31ca3f2008-09-12 11:24:11 +020088 temp = kmalloc(count, GFP_KERNEL);
89 if (!temp)
90 return -ENOMEM;
91
Tejun Heoeb361652007-06-14 03:45:16 +090092 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Chris Wright2c3c8be2010-05-12 18:28:57 -070094 count = fill_read(file, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020095 if (count < 0) {
96 mutex_unlock(&bb->mutex);
97 goto out_free;
98 }
Tejun Heoeb361652007-06-14 03:45:16 +090099
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200100 memcpy(temp, bb->buffer, count);
101
102 mutex_unlock(&bb->mutex);
103
104 if (copy_to_user(userbuf, temp, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +0900105 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200106 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +0900107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Tejun Heo93e3cd822007-06-14 03:45:13 +0900109 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 *off = offs + count;
112
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200113 out_free:
114 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 return count;
116}
117
118static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700119flush_write(struct file *file, char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
Chris Wright2c3c8be2010-05-12 18:28:57 -0700121 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900122 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
123 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +0900124 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Tejun Heo0ab66082007-06-14 03:45:16 +0900126 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800127 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900128 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Tejun Heo0ab66082007-06-14 03:45:16 +0900130 rc = -EIO;
131 if (attr->write)
Chris Wright2c3c8be2010-05-12 18:28:57 -0700132 rc = attr->write(file, kobj, attr, buffer, offset, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900133
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800134 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900135
136 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Tejun Heo93e3cd822007-06-14 03:45:13 +0900139static ssize_t write(struct file *file, const char __user *userbuf,
140 size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Tejun Heoeb361652007-06-14 03:45:16 +0900142 struct bin_buffer *bb = file->private_data;
Chris Wright2c3c8be2010-05-12 18:28:57 -0700143 int size = file->f_path.dentry->d_inode->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +0900145 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200146 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -0800148 if (!bytes)
149 return 0;
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 if (size) {
152 if (offs > size)
153 return 0;
154 if (offs + count > size)
155 count = size - offs;
156 }
157
Li Zefan1c8542c2009-04-08 15:07:30 +0800158 temp = memdup_user(userbuf, count);
159 if (IS_ERR(temp))
160 return PTR_ERR(temp);
Tejun Heoeb361652007-06-14 03:45:16 +0900161
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200162 mutex_lock(&bb->mutex);
163
164 memcpy(bb->buffer, temp, count);
165
Chris Wright2c3c8be2010-05-12 18:28:57 -0700166 count = flush_write(file, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200167 mutex_unlock(&bb->mutex);
168
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (count > 0)
170 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900171
Catalin Marinasd5ce5b42009-07-08 11:17:34 +0100172 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return count;
174}
175
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800176static void bin_vma_open(struct vm_area_struct *vma)
177{
178 struct file *file = vma->vm_file;
179 struct bin_buffer *bb = file->private_data;
180 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
181
182 if (!bb->vm_ops || !bb->vm_ops->open)
183 return;
184
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800185 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800186 return;
187
188 bb->vm_ops->open(vma);
189
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800190 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800191}
192
193static void bin_vma_close(struct vm_area_struct *vma)
194{
195 struct file *file = vma->vm_file;
196 struct bin_buffer *bb = file->private_data;
197 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
198
199 if (!bb->vm_ops || !bb->vm_ops->close)
200 return;
201
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800202 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800203 return;
204
205 bb->vm_ops->close(vma);
206
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800207 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800208}
209
210static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
211{
212 struct file *file = vma->vm_file;
213 struct bin_buffer *bb = file->private_data;
214 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
215 int ret;
216
217 if (!bb->vm_ops || !bb->vm_ops->fault)
218 return VM_FAULT_SIGBUS;
219
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800220 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800221 return VM_FAULT_SIGBUS;
222
223 ret = bb->vm_ops->fault(vma, vmf);
224
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800225 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800226 return ret;
227}
228
Hugh Dickins851a0392009-03-31 15:23:24 -0700229static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800230{
231 struct file *file = vma->vm_file;
232 struct bin_buffer *bb = file->private_data;
233 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
234 int ret;
235
Hugh Dickins095160a2009-03-23 01:41:27 +0000236 if (!bb->vm_ops)
Hugh Dickins851a0392009-03-31 15:23:24 -0700237 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800238
Hugh Dickins095160a2009-03-23 01:41:27 +0000239 if (!bb->vm_ops->page_mkwrite)
240 return 0;
241
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800242 if (!sysfs_get_active(attr_sd))
Hugh Dickins851a0392009-03-31 15:23:24 -0700243 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800244
Hugh Dickins851a0392009-03-31 15:23:24 -0700245 ret = bb->vm_ops->page_mkwrite(vma, vmf);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800246
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800247 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800248 return ret;
249}
250
251static int bin_access(struct vm_area_struct *vma, unsigned long addr,
252 void *buf, int len, int write)
253{
254 struct file *file = vma->vm_file;
255 struct bin_buffer *bb = file->private_data;
256 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
257 int ret;
258
259 if (!bb->vm_ops || !bb->vm_ops->access)
260 return -EINVAL;
261
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800262 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800263 return -EINVAL;
264
265 ret = bb->vm_ops->access(vma, addr, buf, len, write);
266
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800267 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800268 return ret;
269}
270
Hugh Dickins095160a2009-03-23 01:41:27 +0000271#ifdef CONFIG_NUMA
272static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
273{
274 struct file *file = vma->vm_file;
275 struct bin_buffer *bb = file->private_data;
276 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
277 int ret;
278
279 if (!bb->vm_ops || !bb->vm_ops->set_policy)
280 return 0;
281
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800282 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000283 return -EINVAL;
284
285 ret = bb->vm_ops->set_policy(vma, new);
286
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800287 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000288 return ret;
289}
290
291static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
292 unsigned long addr)
293{
294 struct file *file = vma->vm_file;
295 struct bin_buffer *bb = file->private_data;
296 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
297 struct mempolicy *pol;
298
299 if (!bb->vm_ops || !bb->vm_ops->get_policy)
300 return vma->vm_policy;
301
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800302 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000303 return vma->vm_policy;
304
305 pol = bb->vm_ops->get_policy(vma, addr);
306
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800307 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000308 return pol;
309}
310
311static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
312 const nodemask_t *to, unsigned long flags)
313{
314 struct file *file = vma->vm_file;
315 struct bin_buffer *bb = file->private_data;
316 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
317 int ret;
318
319 if (!bb->vm_ops || !bb->vm_ops->migrate)
320 return 0;
321
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800322 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000323 return 0;
324
325 ret = bb->vm_ops->migrate(vma, from, to, flags);
326
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800327 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000328 return ret;
329}
330#endif
331
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400332static const struct vm_operations_struct bin_vm_ops = {
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800333 .open = bin_vma_open,
334 .close = bin_vma_close,
335 .fault = bin_fault,
336 .page_mkwrite = bin_page_mkwrite,
337 .access = bin_access,
Hugh Dickins095160a2009-03-23 01:41:27 +0000338#ifdef CONFIG_NUMA
339 .set_policy = bin_set_policy,
340 .get_policy = bin_get_policy,
341 .migrate = bin_migrate,
342#endif
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800343};
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345static int mmap(struct file *file, struct vm_area_struct *vma)
346{
Tejun Heoeb361652007-06-14 03:45:16 +0900347 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900348 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900349 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
350 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900351 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Tejun Heoeb361652007-06-14 03:45:16 +0900353 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900354
355 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800356 rc = -ENODEV;
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800357 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800358 goto out_unlock;
Tejun Heo0ab66082007-06-14 03:45:16 +0900359
360 rc = -EINVAL;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800361 if (!attr->mmap)
362 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900363
Chris Wright2c3c8be2010-05-12 18:28:57 -0700364 rc = attr->mmap(file, kobj, attr, vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800365 if (rc)
366 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900367
Hugh Dickins095160a2009-03-23 01:41:27 +0000368 /*
369 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
370 * to satisfy versions of X which crash if the mmap fails: that
371 * substitutes a new vm_file, and we don't then want bin_vm_ops.
372 */
373 if (vma->vm_file != file)
374 goto out_put;
375
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800376 rc = -EINVAL;
377 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
378 goto out_put;
379
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800380 rc = 0;
381 bb->mmapped = 1;
Hugh Dickins095160a2009-03-23 01:41:27 +0000382 bb->vm_ops = vma->vm_ops;
383 vma->vm_ops = &bin_vm_ops;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800384out_put:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800385 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800386out_unlock:
Tejun Heoeb361652007-06-14 03:45:16 +0900387 mutex_unlock(&bb->mutex);
388
389 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390}
391
392static int open(struct inode * inode, struct file * file)
393{
Tejun Heo3e519032007-06-14 03:45:15 +0900394 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900395 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900396 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900397 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Tejun Heo078ce642007-09-20 16:05:10 +0900399 /* binary file operations requires both @sd and its parent */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800400 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900401 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 error = -EACCES;
404 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900405 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900407 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900410 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
411 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900412 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Tejun Heoeb361652007-06-14 03:45:16 +0900414 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
415 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900416 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900417
418 mutex_init(&bb->mutex);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800419 bb->file = file;
Tejun Heoeb361652007-06-14 03:45:16 +0900420 file->private_data = bb;
421
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800422 mutex_lock(&sysfs_bin_lock);
423 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
424 mutex_unlock(&sysfs_bin_lock);
425
Tejun Heo078ce642007-09-20 16:05:10 +0900426 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800427 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900428 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Tejun Heo7b595752007-06-14 03:45:17 +0900430 err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800431 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900432 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 return error;
434}
435
436static int release(struct inode * inode, struct file * file)
437{
Tejun Heoeb361652007-06-14 03:45:16 +0900438 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800440 mutex_lock(&sysfs_bin_lock);
441 hlist_del(&bb->list);
442 mutex_unlock(&sysfs_bin_lock);
443
Tejun Heoeb361652007-06-14 03:45:16 +0900444 kfree(bb->buffer);
445 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447}
448
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800449const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 .read = read,
451 .write = write,
452 .mmap = mmap,
453 .llseek = generic_file_llseek,
454 .open = open,
455 .release = release,
456};
457
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800458
459void unmap_bin_file(struct sysfs_dirent *attr_sd)
460{
461 struct bin_buffer *bb;
462 struct hlist_node *tmp;
463
464 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
465 return;
466
467 mutex_lock(&sysfs_bin_lock);
468
469 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
470 struct inode *inode = bb->file->f_path.dentry->d_inode;
471
472 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
473 }
474
475 mutex_unlock(&sysfs_bin_lock);
476}
477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/**
479 * sysfs_create_bin_file - create binary file for object.
480 * @kobj: object.
481 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 */
483
Phil Carmody66ecb922009-12-18 15:34:20 +0200484int sysfs_create_bin_file(struct kobject *kobj,
485 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Tejun Heo608e2662007-06-14 04:27:22 +0900487 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Tejun Heo608e2662007-06-14 04:27:22 +0900489 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
492
493/**
494 * sysfs_remove_bin_file - remove binary file for object.
495 * @kobj: object.
496 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 */
498
Phil Carmody66ecb922009-12-18 15:34:20 +0200499void sysfs_remove_bin_file(struct kobject *kobj,
500 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700502 sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
504
505EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
506EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);