blob: e9d293593e52f79bc51bef2bbc352898b9bdd1ee [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
49fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count)
50{
Tejun Heo3e519032007-06-14 03:45:15 +090051 struct sysfs_dirent *attr_sd = 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)
Zhang Rui91a69022007-06-09 13:57:22 +080062 rc = attr->read(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 Heo93e3cd82007-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;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -080073 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 int size = dentry->d_inode->i_size;
75 loff_t offs = *off;
Tejun Heo93e3cd82007-06-14 03:45:13 +090076 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020077 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -080079 if (!bytes)
80 return 0;
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 if (size) {
83 if (offs > size)
84 return 0;
85 if (offs + count > size)
86 count = size - offs;
87 }
88
Nick Pigginb31ca3f2008-09-12 11:24:11 +020089 temp = kmalloc(count, GFP_KERNEL);
90 if (!temp)
91 return -ENOMEM;
92
Tejun Heoeb361652007-06-14 03:45:16 +090093 mutex_lock(&bb->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Tejun Heoeb361652007-06-14 03:45:16 +090095 count = fill_read(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +020096 if (count < 0) {
97 mutex_unlock(&bb->mutex);
98 goto out_free;
99 }
Tejun Heoeb361652007-06-14 03:45:16 +0900100
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200101 memcpy(temp, bb->buffer, count);
102
103 mutex_unlock(&bb->mutex);
104
105 if (copy_to_user(userbuf, temp, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +0900106 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200107 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +0900108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Tejun Heo93e3cd82007-06-14 03:45:13 +0900110 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 *off = offs + count;
113
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200114 out_free:
115 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 return count;
117}
118
119static int
120flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count)
121{
Tejun Heo3e519032007-06-14 03:45:15 +0900122 struct sysfs_dirent *attr_sd = dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900123 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
124 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heo0ab66082007-06-14 03:45:16 +0900125 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Tejun Heo0ab66082007-06-14 03:45:16 +0900127 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800128 if (!sysfs_get_active(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900129 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Tejun Heo0ab66082007-06-14 03:45:16 +0900131 rc = -EIO;
132 if (attr->write)
Zhang Rui91a69022007-06-09 13:57:22 +0800133 rc = attr->write(kobj, attr, buffer, offset, count);
Tejun Heo0ab66082007-06-14 03:45:16 +0900134
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800135 sysfs_put_active(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900136
137 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Tejun Heo93e3cd82007-06-14 03:45:13 +0900140static ssize_t write(struct file *file, const char __user *userbuf,
141 size_t bytes, loff_t *off)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
Tejun Heoeb361652007-06-14 03:45:16 +0900143 struct bin_buffer *bb = file->private_data;
Josef "Jeff" Sipekf427f5d2006-12-08 02:36:36 -0800144 struct dentry *dentry = file->f_path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 int size = dentry->d_inode->i_size;
146 loff_t offs = *off;
Tejun Heo93e3cd82007-06-14 03:45:13 +0900147 int count = min_t(size_t, bytes, PAGE_SIZE);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200148 char *temp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Greg Kroah-Hartman4503efd2009-01-20 15:51:16 -0800150 if (!bytes)
151 return 0;
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 if (size) {
154 if (offs > size)
155 return 0;
156 if (offs + count > size)
157 count = size - offs;
158 }
159
Li Zefan1c8542c2009-04-08 15:07:30 +0800160 temp = memdup_user(userbuf, count);
161 if (IS_ERR(temp))
162 return PTR_ERR(temp);
Tejun Heoeb361652007-06-14 03:45:16 +0900163
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200164 mutex_lock(&bb->mutex);
165
166 memcpy(bb->buffer, temp, count);
167
Tejun Heoeb361652007-06-14 03:45:16 +0900168 count = flush_write(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200169 mutex_unlock(&bb->mutex);
170
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (count > 0)
172 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900173
Catalin Marinasd5ce5b42009-07-08 11:17:34 +0100174 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 return count;
176}
177
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800178static void bin_vma_open(struct vm_area_struct *vma)
179{
180 struct file *file = vma->vm_file;
181 struct bin_buffer *bb = file->private_data;
182 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
183
184 if (!bb->vm_ops || !bb->vm_ops->open)
185 return;
186
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800187 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800188 return;
189
190 bb->vm_ops->open(vma);
191
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800192 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800193}
194
195static void bin_vma_close(struct vm_area_struct *vma)
196{
197 struct file *file = vma->vm_file;
198 struct bin_buffer *bb = file->private_data;
199 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
200
201 if (!bb->vm_ops || !bb->vm_ops->close)
202 return;
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;
206
207 bb->vm_ops->close(vma);
208
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800209 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800210}
211
212static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
213{
214 struct file *file = vma->vm_file;
215 struct bin_buffer *bb = file->private_data;
216 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
217 int ret;
218
219 if (!bb->vm_ops || !bb->vm_ops->fault)
220 return VM_FAULT_SIGBUS;
221
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800222 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800223 return VM_FAULT_SIGBUS;
224
225 ret = bb->vm_ops->fault(vma, vmf);
226
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800227 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800228 return ret;
229}
230
Hugh Dickins851a0392009-03-31 15:23:24 -0700231static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800232{
233 struct file *file = vma->vm_file;
234 struct bin_buffer *bb = file->private_data;
235 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
236 int ret;
237
Hugh Dickins095160a2009-03-23 01:41:27 +0000238 if (!bb->vm_ops)
Hugh Dickins851a0392009-03-31 15:23:24 -0700239 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800240
Hugh Dickins095160a2009-03-23 01:41:27 +0000241 if (!bb->vm_ops->page_mkwrite)
242 return 0;
243
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800244 if (!sysfs_get_active(attr_sd))
Hugh Dickins851a0392009-03-31 15:23:24 -0700245 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800246
Hugh Dickins851a0392009-03-31 15:23:24 -0700247 ret = bb->vm_ops->page_mkwrite(vma, vmf);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800248
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800249 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800250 return ret;
251}
252
253static int bin_access(struct vm_area_struct *vma, unsigned long addr,
254 void *buf, int len, int write)
255{
256 struct file *file = vma->vm_file;
257 struct bin_buffer *bb = file->private_data;
258 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
259 int ret;
260
261 if (!bb->vm_ops || !bb->vm_ops->access)
262 return -EINVAL;
263
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800264 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800265 return -EINVAL;
266
267 ret = bb->vm_ops->access(vma, addr, buf, len, write);
268
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800269 sysfs_put_active(attr_sd);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800270 return ret;
271}
272
Hugh Dickins095160a2009-03-23 01:41:27 +0000273#ifdef CONFIG_NUMA
274static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
275{
276 struct file *file = vma->vm_file;
277 struct bin_buffer *bb = file->private_data;
278 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
279 int ret;
280
281 if (!bb->vm_ops || !bb->vm_ops->set_policy)
282 return 0;
283
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800284 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000285 return -EINVAL;
286
287 ret = bb->vm_ops->set_policy(vma, new);
288
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800289 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000290 return ret;
291}
292
293static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
294 unsigned long addr)
295{
296 struct file *file = vma->vm_file;
297 struct bin_buffer *bb = file->private_data;
298 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
299 struct mempolicy *pol;
300
301 if (!bb->vm_ops || !bb->vm_ops->get_policy)
302 return vma->vm_policy;
303
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800304 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000305 return vma->vm_policy;
306
307 pol = bb->vm_ops->get_policy(vma, addr);
308
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800309 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000310 return pol;
311}
312
313static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
314 const nodemask_t *to, unsigned long flags)
315{
316 struct file *file = vma->vm_file;
317 struct bin_buffer *bb = file->private_data;
318 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
319 int ret;
320
321 if (!bb->vm_ops || !bb->vm_ops->migrate)
322 return 0;
323
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800324 if (!sysfs_get_active(attr_sd))
Hugh Dickins095160a2009-03-23 01:41:27 +0000325 return 0;
326
327 ret = bb->vm_ops->migrate(vma, from, to, flags);
328
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800329 sysfs_put_active(attr_sd);
Hugh Dickins095160a2009-03-23 01:41:27 +0000330 return ret;
331}
332#endif
333
Alexey Dobriyanf0f37e22009-09-27 22:29:37 +0400334static const struct vm_operations_struct bin_vm_ops = {
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800335 .open = bin_vma_open,
336 .close = bin_vma_close,
337 .fault = bin_fault,
338 .page_mkwrite = bin_page_mkwrite,
339 .access = bin_access,
Hugh Dickins095160a2009-03-23 01:41:27 +0000340#ifdef CONFIG_NUMA
341 .set_policy = bin_set_policy,
342 .get_policy = bin_get_policy,
343 .migrate = bin_migrate,
344#endif
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800345};
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347static int mmap(struct file *file, struct vm_area_struct *vma)
348{
Tejun Heoeb361652007-06-14 03:45:16 +0900349 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900350 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900351 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
352 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900353 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Tejun Heoeb361652007-06-14 03:45:16 +0900355 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900356
357 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800358 rc = -ENODEV;
Eric W. Biedermane72ceb82010-02-11 15:18:38 -0800359 if (!sysfs_get_active(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800360 goto out_unlock;
Tejun Heo0ab66082007-06-14 03:45:16 +0900361
362 rc = -EINVAL;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800363 if (!attr->mmap)
364 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900365
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800366 rc = attr->mmap(kobj, attr, vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800367 if (rc)
368 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900369
Hugh Dickins095160a2009-03-23 01:41:27 +0000370 /*
371 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
372 * to satisfy versions of X which crash if the mmap fails: that
373 * substitutes a new vm_file, and we don't then want bin_vm_ops.
374 */
375 if (vma->vm_file != file)
376 goto out_put;
377
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800378 rc = -EINVAL;
379 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
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;
464 struct hlist_node *tmp;
465
466 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
467 return;
468
469 mutex_lock(&sysfs_bin_lock);
470
471 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
472 struct inode *inode = bb->file->f_path.dentry->d_inode;
473
474 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
475 }
476
477 mutex_unlock(&sysfs_bin_lock);
478}
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/**
481 * sysfs_create_bin_file - create binary file for object.
482 * @kobj: object.
483 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 */
485
Phil Carmody66ecb922009-12-18 15:34:20 +0200486int sysfs_create_bin_file(struct kobject *kobj,
487 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Tejun Heo608e2662007-06-14 04:27:22 +0900489 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Tejun Heo608e2662007-06-14 04:27:22 +0900491 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
494
495/**
496 * sysfs_remove_bin_file - remove binary file for object.
497 * @kobj: object.
498 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 */
500
Phil Carmody66ecb922009-12-18 15:34:20 +0200501void sysfs_remove_bin_file(struct kobject *kobj,
502 const struct bin_attribute *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Alan Stern5f1835d2007-08-16 16:13:06 -0400504 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505}
506
507EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
508EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);