blob: 93e0c0281d4511cc1319bcb0fdb2ed05eb1453a4 [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;
43 struct vm_operations_struct *vm_ops;
44 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 */
57 if (!sysfs_get_active_two(attr_sd))
58 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
64 sysfs_put_active_two(attr_sd);
65
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 */
128 if (!sysfs_get_active_two(attr_sd))
129 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
135 sysfs_put_active_two(attr_sd);
136
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
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200160 temp = kmalloc(count, GFP_KERNEL);
161 if (!temp)
162 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200164 if (copy_from_user(temp, userbuf, count)) {
Tejun Heoeb361652007-06-14 03:45:16 +0900165 count = -EFAULT;
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200166 goto out_free;
Tejun Heoeb361652007-06-14 03:45:16 +0900167 }
168
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200169 mutex_lock(&bb->mutex);
170
171 memcpy(bb->buffer, temp, count);
172
Tejun Heoeb361652007-06-14 03:45:16 +0900173 count = flush_write(dentry, bb->buffer, offs, count);
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200174 mutex_unlock(&bb->mutex);
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (count > 0)
177 *off = offs + count;
Tejun Heoeb361652007-06-14 03:45:16 +0900178
Nick Pigginb31ca3f2008-09-12 11:24:11 +0200179out_free:
180 kfree(temp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return count;
182}
183
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800184static 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
201static 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
218static 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
Hugh Dickins851a0392009-03-31 15:23:24 -0700237static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800238{
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
Hugh Dickins095160a2009-03-23 01:41:27 +0000244 if (!bb->vm_ops)
Hugh Dickins851a0392009-03-31 15:23:24 -0700245 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800246
Hugh Dickins095160a2009-03-23 01:41:27 +0000247 if (!bb->vm_ops->page_mkwrite)
248 return 0;
249
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800250 if (!sysfs_get_active_two(attr_sd))
Hugh Dickins851a0392009-03-31 15:23:24 -0700251 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800252
Hugh Dickins851a0392009-03-31 15:23:24 -0700253 ret = bb->vm_ops->page_mkwrite(vma, vmf);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800254
255 sysfs_put_active_two(attr_sd);
256 return ret;
257}
258
259static int bin_access(struct vm_area_struct *vma, unsigned long addr,
260 void *buf, int len, int write)
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
267 if (!bb->vm_ops || !bb->vm_ops->access)
268 return -EINVAL;
269
270 if (!sysfs_get_active_two(attr_sd))
271 return -EINVAL;
272
273 ret = bb->vm_ops->access(vma, addr, buf, len, write);
274
275 sysfs_put_active_two(attr_sd);
276 return ret;
277}
278
Hugh Dickins095160a2009-03-23 01:41:27 +0000279#ifdef CONFIG_NUMA
280static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
281{
282 struct file *file = vma->vm_file;
283 struct bin_buffer *bb = file->private_data;
284 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
285 int ret;
286
287 if (!bb->vm_ops || !bb->vm_ops->set_policy)
288 return 0;
289
290 if (!sysfs_get_active_two(attr_sd))
291 return -EINVAL;
292
293 ret = bb->vm_ops->set_policy(vma, new);
294
295 sysfs_put_active_two(attr_sd);
296 return ret;
297}
298
299static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
300 unsigned long addr)
301{
302 struct file *file = vma->vm_file;
303 struct bin_buffer *bb = file->private_data;
304 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
305 struct mempolicy *pol;
306
307 if (!bb->vm_ops || !bb->vm_ops->get_policy)
308 return vma->vm_policy;
309
310 if (!sysfs_get_active_two(attr_sd))
311 return vma->vm_policy;
312
313 pol = bb->vm_ops->get_policy(vma, addr);
314
315 sysfs_put_active_two(attr_sd);
316 return pol;
317}
318
319static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
320 const nodemask_t *to, unsigned long flags)
321{
322 struct file *file = vma->vm_file;
323 struct bin_buffer *bb = file->private_data;
324 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
325 int ret;
326
327 if (!bb->vm_ops || !bb->vm_ops->migrate)
328 return 0;
329
330 if (!sysfs_get_active_two(attr_sd))
331 return 0;
332
333 ret = bb->vm_ops->migrate(vma, from, to, flags);
334
335 sysfs_put_active_two(attr_sd);
336 return ret;
337}
338#endif
339
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800340static struct vm_operations_struct bin_vm_ops = {
341 .open = bin_vma_open,
342 .close = bin_vma_close,
343 .fault = bin_fault,
344 .page_mkwrite = bin_page_mkwrite,
345 .access = bin_access,
Hugh Dickins095160a2009-03-23 01:41:27 +0000346#ifdef CONFIG_NUMA
347 .set_policy = bin_set_policy,
348 .get_policy = bin_get_policy,
349 .migrate = bin_migrate,
350#endif
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800351};
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353static int mmap(struct file *file, struct vm_area_struct *vma)
354{
Tejun Heoeb361652007-06-14 03:45:16 +0900355 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900356 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900357 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
358 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900359 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Tejun Heoeb361652007-06-14 03:45:16 +0900361 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900362
363 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800364 rc = -ENODEV;
Tejun Heo0ab66082007-06-14 03:45:16 +0900365 if (!sysfs_get_active_two(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800366 goto out_unlock;
Tejun Heo0ab66082007-06-14 03:45:16 +0900367
368 rc = -EINVAL;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800369 if (!attr->mmap)
370 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900371
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800372 rc = attr->mmap(kobj, attr, vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800373 if (rc)
374 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900375
Hugh Dickins095160a2009-03-23 01:41:27 +0000376 /*
377 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
378 * to satisfy versions of X which crash if the mmap fails: that
379 * substitutes a new vm_file, and we don't then want bin_vm_ops.
380 */
381 if (vma->vm_file != file)
382 goto out_put;
383
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800384 rc = -EINVAL;
385 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
386 goto out_put;
387
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800388 rc = 0;
389 bb->mmapped = 1;
Hugh Dickins095160a2009-03-23 01:41:27 +0000390 bb->vm_ops = vma->vm_ops;
391 vma->vm_ops = &bin_vm_ops;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800392out_put:
393 sysfs_put_active_two(attr_sd);
394out_unlock:
Tejun Heoeb361652007-06-14 03:45:16 +0900395 mutex_unlock(&bb->mutex);
396
397 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400static int open(struct inode * inode, struct file * file)
401{
Tejun Heo3e519032007-06-14 03:45:15 +0900402 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900403 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
Tejun Heoeb361652007-06-14 03:45:16 +0900404 struct bin_buffer *bb = NULL;
Tejun Heo0ab66082007-06-14 03:45:16 +0900405 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Tejun Heo078ce642007-09-20 16:05:10 +0900407 /* binary file operations requires both @sd and its parent */
408 if (!sysfs_get_active_two(attr_sd))
Tejun Heo0ab66082007-06-14 03:45:16 +0900409 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 error = -EACCES;
412 if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900413 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap))
Tejun Heo7b595752007-06-14 03:45:17 +0900415 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
417 error = -ENOMEM;
Tejun Heoeb361652007-06-14 03:45:16 +0900418 bb = kzalloc(sizeof(*bb), GFP_KERNEL);
419 if (!bb)
Tejun Heo7b595752007-06-14 03:45:17 +0900420 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421
Tejun Heoeb361652007-06-14 03:45:16 +0900422 bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL);
423 if (!bb->buffer)
Tejun Heo7b595752007-06-14 03:45:17 +0900424 goto err_out;
Tejun Heoeb361652007-06-14 03:45:16 +0900425
426 mutex_init(&bb->mutex);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800427 bb->file = file;
Tejun Heoeb361652007-06-14 03:45:16 +0900428 file->private_data = bb;
429
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800430 mutex_lock(&sysfs_bin_lock);
431 hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers);
432 mutex_unlock(&sysfs_bin_lock);
433
Tejun Heo078ce642007-09-20 16:05:10 +0900434 /* open succeeded, put active references */
435 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900436 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
Tejun Heo7b595752007-06-14 03:45:17 +0900438 err_out:
Tejun Heo078ce642007-09-20 16:05:10 +0900439 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900440 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 return error;
442}
443
444static int release(struct inode * inode, struct file * file)
445{
Tejun Heoeb361652007-06-14 03:45:16 +0900446 struct bin_buffer *bb = file->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800448 mutex_lock(&sysfs_bin_lock);
449 hlist_del(&bb->list);
450 mutex_unlock(&sysfs_bin_lock);
451
Tejun Heoeb361652007-06-14 03:45:16 +0900452 kfree(bb->buffer);
453 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 return 0;
455}
456
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800457const struct file_operations bin_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 .read = read,
459 .write = write,
460 .mmap = mmap,
461 .llseek = generic_file_llseek,
462 .open = open,
463 .release = release,
464};
465
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800466
467void unmap_bin_file(struct sysfs_dirent *attr_sd)
468{
469 struct bin_buffer *bb;
470 struct hlist_node *tmp;
471
472 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
473 return;
474
475 mutex_lock(&sysfs_bin_lock);
476
477 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
478 struct inode *inode = bb->file->f_path.dentry->d_inode;
479
480 unmap_mapping_range(inode->i_mapping, 0, 0, 1);
481 }
482
483 mutex_unlock(&sysfs_bin_lock);
484}
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486/**
487 * sysfs_create_bin_file - create binary file for object.
488 * @kobj: object.
489 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 */
491
492int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
493{
Tejun Heo608e2662007-06-14 04:27:22 +0900494 BUG_ON(!kobj || !kobj->sd || !attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Tejun Heo608e2662007-06-14 04:27:22 +0900496 return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497}
498
499
500/**
501 * sysfs_remove_bin_file - remove binary file for object.
502 * @kobj: object.
503 * @attr: attribute descriptor.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
505
Randy.Dunlap995982c2006-07-10 23:05:25 -0700506void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Alan Stern5f1835d2007-08-16 16:13:06 -0400508 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509}
510
511EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
512EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);