blob: c590cabd57bbd8b76c9ca5a3aa210c93dbb9927f [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>
Greg Kroah-Hartman060cc742013-08-21 16:34:59 -070025#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "sysfs.h"
28
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080029/*
30 * There's one bin_buffer for each open file.
31 *
32 * filp->private_data points to bin_buffer and
33 * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s
34 * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock
35 */
36static DEFINE_MUTEX(sysfs_bin_lock);
37
Tejun Heoeb361652007-06-14 03:45:16 +090038struct bin_buffer {
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080039 struct mutex mutex;
40 void *buffer;
41 int mmapped;
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +040042 const struct vm_operations_struct *vm_ops;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -080043 struct file *file;
44 struct hlist_node list;
Tejun Heoeb361652007-06-14 03:45:16 +090045};
46
Linus Torvalds1da177e2005-04-16 15:20:36 -070047static int
Chris Wright2c3c8be2010-05-12 18:28:57 -070048fill_read(struct file *file, char *buffer, loff_t off, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Chris Wright2c3c8be2010-05-12 18:28:57 -070050 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +090051 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
52 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +090053 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Tejun Heo0ab66082007-06-14 03:45:16 +090055 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -080056 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +090057 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Tejun Heo0ab66082007-06-14 03:45:16 +090059 rc = -EIO;
60 if (attr->read)
Chris Wright2c3c8be2010-05-12 18:28:57 -070061 rc = attr->read(file, kobj, attr, buffer, off, count);
Tejun Heo0ab66082007-06-14 03:45:16 +090062
Eric W. Biedermane72ceb82010-02-11 15:18:38 -080063 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +090064
65 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066}
67
68static ssize_t
Tejun Heo93e3cd822007-06-14 03:45:13 +090069read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Tejun Heoeb361652007-06-14 03:45:16 +090071 struct bin_buffer *bb = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -050072 int size = file_inode(file)->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +090074 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020075 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -080077 if (!bytes)
78 return 0;
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 if (size) {
81 if (offs > size)
82 return 0;
83 if (offs + count > size)
84 count = size - offs;
85 }
86
Nick Pigginb31ca3f2008-09-12 11:24:11 +020087 temp = kmalloc(count, GFP_KERNEL);
88 if (!temp)
89 return -ENOMEM;
90
Tejun Heoeb361652007-06-14 03:45:16 +090091 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Chris Wright2c3c8be2010-05-12 18:28:57 -070093 count = fill_read(file, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020094 if (count < 0) {
95 mutex_unlock(&bb->mutex);
96 goto out_free;
97 }
Tejun Heoeb361652007-06-14 03:45:16 +090098
Nick Pigginb31ca3f2008-09-12 11:24:11 +020099 memcpy(temp, bb->buffer, count);
100
101 mutex_unlock(&bb->mutex);
102
103 if (copy_to_user(userbuf, temp, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +0900104 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200105 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +0900106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Tejun Heo93e3cd822007-06-14 03:45:13 +0900108 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 *off = offs + count;
111
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200112 out_free:
113 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return count;
115}
116
117static int
Chris Wright2c3c8be2010-05-12 18:28:57 -0700118flush_write(struct file *file, char *buffer, loff_t offset, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119{
Chris Wright2c3c8be2010-05-12 18:28:57 -0700120 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900121 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
122 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +0900123 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Tejun Heo0ab66082007-06-14 03:45:16 +0900125 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800126 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900127 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Tejun Heo0ab66082007-06-14 03:45:16 +0900129 rc = -EIO;
130 if (attr->write)
Chris Wright2c3c8be2010-05-12 18:28:57 -0700131 rc = attr->write(file, kobj, attr, buffer, offset, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900132
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800133 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900134
135 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
Tejun Heo93e3cd822007-06-14 03:45:13 +0900138static ssize_t write(struct file *file, const char __user *userbuf,
139 size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
Tejun Heoeb361652007-06-14 03:45:16 +0900141 struct bin_buffer *bb = file->private_data;
Al Viro496ad9a2013-01-23 17:07:38 -0500142 int size = file_inode(file)->i_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 loff_t offs = *off;
Tejun Heo93e3cd822007-06-14 03:45:13 +0900144 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200145 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -0800147 if (!bytes)
148 return 0;
149
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (size) {
151 if (offs > size)
152 return 0;
153 if (offs + count > size)
154 count = size - offs;
155 }
156
Li Zefan1c8542c2009-04-08 15:07:30 +0800157 temp = memdup_user(userbuf, count);
158 if (IS_ERR(temp))
159 return PTR_ERR(temp);
Tejun Heoeb361652007-06-14 03:45:16 +0900160
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200161 mutex_lock(&bb->mutex);
162
163 memcpy(bb->buffer, temp, count);
164
Chris Wright2c3c8be2010-05-12 18:28:57 -0700165 count = flush_write(file, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200166 mutex_unlock(&bb->mutex);
167
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (count > 0)
169 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900170
Catalin Marinasd5ce5b42009-07-08 11:17:34 +0100171 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return count;
173}
174
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800175static void bin_vma_open(struct vm_area_struct *vma)
176{
177 struct file *file = vma->vm_file;
178 struct bin_buffer *bb = file->private_data;
179 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
180
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700181 if (!bb->vm_ops)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800182 return;
183
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800184 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800185 return;
186
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700187 if (bb->vm_ops->open)
188 bb->vm_ops->open(vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800189
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800190 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800191}
192
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800193static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
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 int ret;
199
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700200 if (!bb->vm_ops)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800201 return VM_FAULT_SIGBUS;
202
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800203 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800204 return VM_FAULT_SIGBUS;
205
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700206 ret = VM_FAULT_SIGBUS;
207 if (bb->vm_ops->fault)
208 ret = bb->vm_ops->fault(vma, vmf);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800209
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800210 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800211 return ret;
212}
213
Hugh Dickins851a0392009-03-31 15:23:24 -0700214static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800215{
216 struct file *file = vma->vm_file;
217 struct bin_buffer *bb = file->private_data;
218 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
219 int ret;
220
Hugh Dickins095160a2009-03-23 01:41:27 +0000221 if (!bb->vm_ops)
Hugh Dickins851a0392009-03-31 15:23:24 -0700222 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800223
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800224 if (!sysfs_get_active(attr_sd))
Hugh Dickins851a0392009-03-31 15:23:24 -0700225 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800226
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700227 ret = 0;
228 if (bb->vm_ops->page_mkwrite)
229 ret = bb->vm_ops->page_mkwrite(vma, vmf);
Jan Kara14ae4172012-06-12 16:20:27 +0200230 else
231 file_update_time(file);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800232
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800233 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800234 return ret;
235}
236
237static int bin_access(struct vm_area_struct *vma, unsigned long addr,
238 void *buf, int len, int write)
239{
240 struct file *file = vma->vm_file;
241 struct bin_buffer *bb = file->private_data;
242 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
243 int ret;
244
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700245 if (!bb->vm_ops)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800246 return -EINVAL;
247
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800248 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800249 return -EINVAL;
250
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700251 ret = -EINVAL;
252 if (bb->vm_ops->access)
253 ret = bb->vm_ops->access(vma, addr, buf, len, write);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800254
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800255 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800256 return ret;
257}
258
Hugh Dickins095160a2009-03-23 01:41:27 +0000259#ifdef CONFIG_NUMA
260static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
261{
262 struct file *file = vma->vm_file;
263 struct bin_buffer *bb = file->private_data;
264 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
265 int ret;
266
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700267 if (!bb->vm_ops)
Hugh Dickins095160a2009-03-23 01:41:27 +0000268 return 0;
269
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800270 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000271 return -EINVAL;
272
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700273 ret = 0;
274 if (bb->vm_ops->set_policy)
275 ret = bb->vm_ops->set_policy(vma, new);
Hugh Dickins095160a2009-03-23 01:41:27 +0000276
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800277 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000278 return ret;
279}
280
281static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
282 unsigned long addr)
283{
284 struct file *file = vma->vm_file;
285 struct bin_buffer *bb = file->private_data;
286 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
287 struct mempolicy *pol;
288
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700289 if (!bb->vm_ops)
Hugh Dickins095160a2009-03-23 01:41:27 +0000290 return vma->vm_policy;
291
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800292 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000293 return vma->vm_policy;
294
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700295 pol = vma->vm_policy;
296 if (bb->vm_ops->get_policy)
297 pol = bb->vm_ops->get_policy(vma, addr);
Hugh Dickins095160a2009-03-23 01:41:27 +0000298
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800299 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000300 return pol;
301}
302
303static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
304 const nodemask_t *to, unsigned long flags)
305{
306 struct file *file = vma->vm_file;
307 struct bin_buffer *bb = file->private_data;
308 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
309 int ret;
310
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700311 if (!bb->vm_ops)
Hugh Dickins095160a2009-03-23 01:41:27 +0000312 return 0;
313
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800314 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000315 return 0;
316
Eric W. Biederman38f49a52010-09-20 00:57:03 -0700317 ret = 0;
318 if (bb->vm_ops->migrate)
319 ret = bb->vm_ops->migrate(vma, from, to, flags);
Hugh Dickins095160a2009-03-23 01:41:27 +0000320
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800321 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000322 return ret;
323}
324#endif
325
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400326static const struct vm_operations_struct bin_vm_ops = {
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800327 .open = bin_vma_open,
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800328 .fault = bin_fault,
329 .page_mkwrite = bin_page_mkwrite,
330 .access = bin_access,
Hugh Dickins095160a2009-03-23 01:41:27 +0000331#ifdef CONFIG_NUMA
332 .set_policy = bin_set_policy,
333 .get_policy = bin_get_policy,
334 .migrate = bin_migrate,
335#endif
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800336};
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338static int mmap(struct file *file, struct vm_area_struct *vma)
339{
Tejun Heoeb361652007-06-14 03:45:16 +0900340 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900341 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900342 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
343 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900344 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Tejun Heoeb361652007-06-14 03:45:16 +0900346 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900347
348 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800349 rc = -ENODEV;
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800350 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800351 goto out_unlock;
Tejun Heo0ab66082007-06-14 03:45:16 +0900352
353 rc = -EINVAL;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800354 if (!attr->mmap)
355 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900356
Chris Wright2c3c8be2010-05-12 18:28:57 -0700357 rc = attr->mmap(file, kobj, attr, vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800358 if (rc)
359 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900360
Hugh Dickins095160a2009-03-23 01:41:27 +0000361 /*
362 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
363 * to satisfy versions of X which crash if the mmap fails: that
364 * substitutes a new vm_file, and we don't then want bin_vm_ops.
365 */
366 if (vma->vm_file != file)
367 goto out_put;
368
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800369 rc = -EINVAL;
370 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
371 goto out_put;
372
Eric W. Biedermana6849fa2010-09-20 00:56:27 -0700373 /*
374 * It is not possible to successfully wrap close.
375 * So error if someone is trying to use close.
376 */
377 rc = -EINVAL;
378 if (vma->vm_ops && vma->vm_ops->close)
379 goto out_put;
380
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800381 rc = 0;
382 bb->mmapped = 1;
Hugh Dickins095160a2009-03-23 01:41:27 +0000383 bb->vm_ops = vma->vm_ops;
384 vma->vm_ops = &bin_vm_ops;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800385out_put:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800386 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800387out_unlock:
Tejun Heoeb361652007-06-14 03:45:16 +0900388 mutex_unlock(&bb->mutex);
389
390 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391}
392
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700393static int open(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Tejun Heo3e519032007-06-14 03:45:15 +0900395 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900396 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900397 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900398 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Tejun Heo078ce642007-09-20 16:05:10 +0900400 /* binary file operations requires both @sd and its parent */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800401 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900402 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 error = -EACCES;
405 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900406 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900408 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
410 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900411 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
412 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900413 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Tejun Heoeb361652007-06-14 03:45:16 +0900415 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
416 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900417 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900418
419 mutex_init(&bb->mutex);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800420 bb->file = file;
Tejun Heoeb361652007-06-14 03:45:16 +0900421 file->private_data = bb;
422
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800423 mutex_lock(&sysfs_bin_lock);
424 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
425 mutex_unlock(&sysfs_bin_lock);
426
Tejun Heo078ce642007-09-20 16:05:10 +0900427 /* open succeeded, put active references */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800428 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900429 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Tejun Heo7b595752007-06-14 03:45:17 +0900431 err_out:
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800432 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900433 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return error;
435}
436
Greg Kroah-Hartman1b18dc22013-08-21 16:28:26 -0700437static int release(struct inode *inode, struct file *file)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Tejun Heoeb361652007-06-14 03:45:16 +0900439 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800441 mutex_lock(&sysfs_bin_lock);
442 hlist_del(&bb->list);
443 mutex_unlock(&sysfs_bin_lock);
444
Tejun Heoeb361652007-06-14 03:45:16 +0900445 kfree(bb->buffer);
446 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 return 0;
448}
449
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800450const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 .read = read,
452 .write = write,
453 .mmap = mmap,
454 .llseek = generic_file_llseek,
455 .open = open,
456 .release = release,
457};
458
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800459
460void unmap_bin_file(struct sysfs_dirent *attr_sd)
461{
462 struct bin_buffer *bb;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800463
464 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
465 return;
466
467 mutex_lock(&sysfs_bin_lock);
468
Sasha Levinb67bfe02013-02-27 17:06:00 -0800469 hlist_for_each_entry(bb, &attr_sd->s_bin_attr.buffers, list) {
Al Viro496ad9a2013-01-23 17:07:38 -0500470 struct inode *inode = file_inode(bb->file);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800471
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 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200483int sysfs_create_bin_file(struct kobject *kobj,
484 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Tejun Heo608e2662007-06-14 04:27:22 +0900486 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Tejun Heo608e2662007-06-14 04:27:22 +0900488 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
Greg Kroah-Hartman1b866752013-08-21 16:17:47 -0700490EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492/**
493 * sysfs_remove_bin_file - remove binary file for object.
494 * @kobj: object.
495 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 */
Phil Carmody66ecb922009-12-18 15:34:20 +0200497void sysfs_remove_bin_file(struct kobject *kobj,
498 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
Eric W. Biederman3ff195b2010-03-30 11:31:26 -0700500 sysfs_hash_and_remove(kobj->sd, NULL, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);