blob: 15c68f9489ae616f556c0e323ff7190d2f7cd088 [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;
Al Viro496ad9a2013-01-23 17:07:38 -050073 int size = file_inode(file)->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;
Al Viro496ad9a2013-01-23 17:07:38 -0500143 int size = file_inode(file)->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
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700182 if (!bb->vm_ops)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800183 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
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700188 if (bb->vm_ops->open)
189 bb->vm_ops->open(vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800190
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800191 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800192}
193
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800194static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
195{
196 struct file *file = vma->vm_file;
197 struct bin_buffer *bb = file->private_data;
198 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
199 int ret;
200
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700201 if (!bb->vm_ops)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800202 return VM_FAULT_SIGBUS;
203
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800204 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800205 return VM_FAULT_SIGBUS;
206
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700207 ret = VM_FAULT_SIGBUS;
208 if (bb->vm_ops->fault)
209 ret = bb->vm_ops->fault(vma, vmf);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800210
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800211 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800212 return ret;
213}
214
Hugh Dickins851a0392009-03-31 15:23:24 -0700215static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800216{
217 struct file *file = vma->vm_file;
218 struct bin_buffer *bb = file->private_data;
219 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
220 int ret;
221
Hugh Dickins095160a2009-03-23 01:41:27 +0000222 if (!bb->vm_ops)
Hugh Dickins851a0392009-03-31 15:23:24 -0700223 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800224
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800225 if (!sysfs_get_active(attr_sd))
Hugh Dickins851a0392009-03-31 15:23:24 -0700226 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800227
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700228 ret = 0;
229 if (bb->vm_ops->page_mkwrite)
230 ret = bb->vm_ops->page_mkwrite(vma, vmf);
Jan Kara14ae4172012-06-12 16:20:27 +0200231 else
232 file_update_time(file);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800233
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800234 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800235 return ret;
236}
237
238static int bin_access(struct vm_area_struct *vma, unsigned long addr,
239 void *buf, int len, int write)
240{
241 struct file *file = vma->vm_file;
242 struct bin_buffer *bb = file->private_data;
243 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
244 int ret;
245
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700246 if (!bb->vm_ops)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800247 return -EINVAL;
248
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800249 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800250 return -EINVAL;
251
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700252 ret = -EINVAL;
253 if (bb->vm_ops->access)
254 ret = bb->vm_ops->access(vma, addr, buf, len, write);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800255
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800256 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800257 return ret;
258}
259
Hugh Dickins095160a2009-03-23 01:41:27 +0000260#ifdef CONFIG_NUMA
261static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
262{
263 struct file *file = vma->vm_file;
264 struct bin_buffer *bb = file->private_data;
265 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
266 int ret;
267
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700268 if (!bb->vm_ops)
Hugh Dickins095160a2009-03-23 01:41:27 +0000269 return 0;
270
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800271 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000272 return -EINVAL;
273
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700274 ret = 0;
275 if (bb->vm_ops->set_policy)
276 ret = bb->vm_ops->set_policy(vma, new);
Hugh Dickins095160a2009-03-23 01:41:27 +0000277
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800278 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000279 return ret;
280}
281
282static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
283 unsigned long addr)
284{
285 struct file *file = vma->vm_file;
286 struct bin_buffer *bb = file->private_data;
287 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
288 struct mempolicy *pol;
289
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700290 if (!bb->vm_ops)
Hugh Dickins095160a2009-03-23 01:41:27 +0000291 return vma->vm_policy;
292
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800293 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000294 return vma->vm_policy;
295
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700296 pol = vma->vm_policy;
297 if (bb->vm_ops->get_policy)
298 pol = bb->vm_ops->get_policy(vma, addr);
Hugh Dickins095160a2009-03-23 01:41:27 +0000299
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800300 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000301 return pol;
302}
303
304static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
305 const nodemask_t *to, unsigned long flags)
306{
307 struct file *file = vma->vm_file;
308 struct bin_buffer *bb = file->private_data;
309 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
310 int ret;
311
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700312 if (!bb->vm_ops)
Hugh Dickins095160a2009-03-23 01:41:27 +0000313 return 0;
314
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800315 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000316 return 0;
317
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700318 ret = 0;
319 if (bb->vm_ops->migrate)
320 ret = bb->vm_ops->migrate(vma, from, to, flags);
Hugh Dickins095160a2009-03-23 01:41:27 +0000321
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800322 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000323 return ret;
324}
325#endif
326
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400327static const struct vm_operations_struct bin_vm_ops = {
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800328 .open = bin_vma_open,
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800329 .fault = bin_fault,
330 .page_mkwrite = bin_page_mkwrite,
331 .access = bin_access,
Hugh Dickins095160a2009-03-23 01:41:27 +0000332#ifdef CONFIG_NUMA
333 .set_policy = bin_set_policy,
334 .get_policy = bin_get_policy,
335 .migrate = bin_migrate,
336#endif
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800337};
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339static int mmap(struct file *file, struct vm_area_struct *vma)
340{
Tejun Heoeb361652007-06-14 03:45:16 +0900341 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900342 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900343 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
344 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900345 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Tejun Heoeb361652007-06-14 03:45:16 +0900347 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900348
349 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800350 rc = -ENODEV;
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800351 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800352 goto out_unlock;
Tejun Heo0ab66082007-06-14 03:45:16 +0900353
354 rc = -EINVAL;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800355 if (!attr->mmap)
356 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900357
Chris Wright2c3c8be2010-05-12 18:28:57 -0700358 rc = attr->mmap(file, kobj, attr, vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800359 if (rc)
360 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900361
Hugh Dickins095160a2009-03-23 01:41:27 +0000362 /*
363 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
364 * to satisfy versions of X which crash if the mmap fails: that
365 * substitutes a new vm_file, and we don't then want bin_vm_ops.
366 */
367 if (vma->vm_file != file)
368 goto out_put;
369
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800370 rc = -EINVAL;
371 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
372 goto out_put;
373
Eric W. Biedermana6849fa2010-09-20 00:56:27 -0700374 /*
375 * It is not possible to successfully wrap close.
376 * So error if someone is trying to use close.
377 */
378 rc = -EINVAL;
379 if (vma->vm_ops && vma->vm_ops->close)
380 goto out_put;
381
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800382 rc = 0;
383 bb->mmapped = 1;
Hugh Dickins095160a2009-03-23 01:41:27 +0000384 bb->vm_ops = vma->vm_ops;
385 vma->vm_ops = &bin_vm_ops;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800386out_put:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800387 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800388out_unlock:
Tejun Heoeb361652007-06-14 03:45:16 +0900389 mutex_unlock(&bb->mutex);
390
391 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394static int open(struct inode * inode, struct file * file)
395{
Tejun Heo3e519032007-06-14 03:45:15 +0900396 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900397 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900398 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900399 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Tejun Heo078ce642007-09-20 16:05:10 +0900401 /* binary file operations requires both @sd and its parent */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800402 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900403 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 error = -EACCES;
406 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900407 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900409 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900412 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
413 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900414 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Tejun Heoeb361652007-06-14 03:45:16 +0900416 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
417 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900418 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900419
420 mutex_init(&bb->mutex);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800421 bb->file = file;
Tejun Heoeb361652007-06-14 03:45:16 +0900422 file->private_data = bb;
423
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800424 mutex_lock(&sysfs_bin_lock);
425 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
426 mutex_unlock(&sysfs_bin_lock);
427
Tejun Heo078ce642007-09-20 16:05:10 +0900428 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800429 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900430 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Tejun Heo7b595752007-06-14 03:45:17 +0900432 err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800433 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900434 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 return error;
436}
437
438static int release(struct inode * inode, struct file * file)
439{
Tejun Heoeb361652007-06-14 03:45:16 +0900440 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800442 mutex_lock(&sysfs_bin_lock);
443 hlist_del(&bb->list);
444 mutex_unlock(&sysfs_bin_lock);
445
Tejun Heoeb361652007-06-14 03:45:16 +0900446 kfree(bb->buffer);
447 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 return 0;
449}
450
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800451const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 .read = read,
453 .write = write,
454 .mmap = mmap,
455 .llseek = generic_file_llseek,
456 .open = open,
457 .release = release,
458};
459
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800460
461void unmap_bin_file(struct sysfs_dirent *attr_sd)
462{
463 struct bin_buffer *bb;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800464
465 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
466 return;
467
468 mutex_lock(&sysfs_bin_lock);
469
Sasha Levinb67bfe02013-02-27 17:06:00 -0800470 hlist_for_each_entry(bb, &attr_sd->s_bin_attr.buffers, list) {
Al Viro496ad9a2013-01-23 17:07:38 -0500471 struct inode *inode = file_inode(bb->file);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800472
473 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
474 }
475
476 mutex_unlock(&sysfs_bin_lock);
477}
478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479/**
480 * sysfs_create_bin_file - create binary file for object.
481 * @kobj: object.
482 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 */
484
Phil Carmody66ecb922009-12-18 15:34:20 +0200485int sysfs_create_bin_file(struct kobject *kobj,
486 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Tejun Heo608e2662007-06-14 04:27:22 +0900488 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Tejun Heo608e2662007-06-14 04:27:22 +0900490 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493
494/**
495 * sysfs_remove_bin_file - remove binary file for object.
496 * @kobj: object.
497 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 */
499
Phil Carmody66ecb922009-12-18 15:34:20 +0200500void sysfs_remove_bin_file(struct kobject *kobj,
501 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700503 sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504}
505
506EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
507EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);