blob: 9345806c8853e369ba34695542081bb2e3f347db [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 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;
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 Heo93e3cd822007-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 Heo93e3cd822007-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 Heo93e3cd822007-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 Heo93e3cd822007-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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return count;
175}
176
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800177static void bin_vma_open(struct vm_area_struct *vma)
178{
179 struct file *file = vma->vm_file;
180 struct bin_buffer *bb = file->private_data;
181 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
182
183 if (!bb->vm_ops || !bb->vm_ops->open)
184 return;
185
186 if (!sysfs_get_active_two(attr_sd))
187 return;
188
189 bb->vm_ops->open(vma);
190
191 sysfs_put_active_two(attr_sd);
192}
193
194static void bin_vma_close(struct vm_area_struct *vma)
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
200 if (!bb->vm_ops || !bb->vm_ops->close)
201 return;
202
203 if (!sysfs_get_active_two(attr_sd))
204 return;
205
206 bb->vm_ops->close(vma);
207
208 sysfs_put_active_two(attr_sd);
209}
210
211static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
212{
213 struct file *file = vma->vm_file;
214 struct bin_buffer *bb = file->private_data;
215 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
216 int ret;
217
218 if (!bb->vm_ops || !bb->vm_ops->fault)
219 return VM_FAULT_SIGBUS;
220
221 if (!sysfs_get_active_two(attr_sd))
222 return VM_FAULT_SIGBUS;
223
224 ret = bb->vm_ops->fault(vma, vmf);
225
226 sysfs_put_active_two(attr_sd);
227 return ret;
228}
229
Hugh Dickins851a0392009-03-31 15:23:24 -0700230static int bin_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800231{
232 struct file *file = vma->vm_file;
233 struct bin_buffer *bb = file->private_data;
234 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
235 int ret;
236
Hugh Dickins095160a2009-03-23 01:41:27 +0000237 if (!bb->vm_ops)
Hugh Dickins851a0392009-03-31 15:23:24 -0700238 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800239
Hugh Dickins095160a2009-03-23 01:41:27 +0000240 if (!bb->vm_ops->page_mkwrite)
241 return 0;
242
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800243 if (!sysfs_get_active_two(attr_sd))
Hugh Dickins851a0392009-03-31 15:23:24 -0700244 return VM_FAULT_SIGBUS;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800245
Hugh Dickins851a0392009-03-31 15:23:24 -0700246 ret = bb->vm_ops->page_mkwrite(vma, vmf);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800247
248 sysfs_put_active_two(attr_sd);
249 return ret;
250}
251
252static int bin_access(struct vm_area_struct *vma, unsigned long addr,
253 void *buf, int len, int write)
254{
255 struct file *file = vma->vm_file;
256 struct bin_buffer *bb = file->private_data;
257 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
258 int ret;
259
260 if (!bb->vm_ops || !bb->vm_ops->access)
261 return -EINVAL;
262
263 if (!sysfs_get_active_two(attr_sd))
264 return -EINVAL;
265
266 ret = bb->vm_ops->access(vma, addr, buf, len, write);
267
268 sysfs_put_active_two(attr_sd);
269 return ret;
270}
271
Hugh Dickins095160a2009-03-23 01:41:27 +0000272#ifdef CONFIG_NUMA
273static int bin_set_policy(struct vm_area_struct *vma, struct mempolicy *new)
274{
275 struct file *file = vma->vm_file;
276 struct bin_buffer *bb = file->private_data;
277 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
278 int ret;
279
280 if (!bb->vm_ops || !bb->vm_ops->set_policy)
281 return 0;
282
283 if (!sysfs_get_active_two(attr_sd))
284 return -EINVAL;
285
286 ret = bb->vm_ops->set_policy(vma, new);
287
288 sysfs_put_active_two(attr_sd);
289 return ret;
290}
291
292static struct mempolicy *bin_get_policy(struct vm_area_struct *vma,
293 unsigned long addr)
294{
295 struct file *file = vma->vm_file;
296 struct bin_buffer *bb = file->private_data;
297 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
298 struct mempolicy *pol;
299
300 if (!bb->vm_ops || !bb->vm_ops->get_policy)
301 return vma->vm_policy;
302
303 if (!sysfs_get_active_two(attr_sd))
304 return vma->vm_policy;
305
306 pol = bb->vm_ops->get_policy(vma, addr);
307
308 sysfs_put_active_two(attr_sd);
309 return pol;
310}
311
312static int bin_migrate(struct vm_area_struct *vma, const nodemask_t *from,
313 const nodemask_t *to, unsigned long flags)
314{
315 struct file *file = vma->vm_file;
316 struct bin_buffer *bb = file->private_data;
317 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
318 int ret;
319
320 if (!bb->vm_ops || !bb->vm_ops->migrate)
321 return 0;
322
323 if (!sysfs_get_active_two(attr_sd))
324 return 0;
325
326 ret = bb->vm_ops->migrate(vma, from, to, flags);
327
328 sysfs_put_active_two(attr_sd);
329 return ret;
330}
331#endif
332
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800333static struct vm_operations_struct bin_vm_ops = {
334 .open = bin_vma_open,
335 .close = bin_vma_close,
336 .fault = bin_fault,
337 .page_mkwrite = bin_page_mkwrite,
338 .access = bin_access,
Hugh Dickins095160a2009-03-23 01:41:27 +0000339#ifdef CONFIG_NUMA
340 .set_policy = bin_set_policy,
341 .get_policy = bin_get_policy,
342 .migrate = bin_migrate,
343#endif
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800344};
345
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346static int mmap(struct file *file, struct vm_area_struct *vma)
347{
Tejun Heoeb361652007-06-14 03:45:16 +0900348 struct bin_buffer *bb = file->private_data;
Tejun Heo3e519032007-06-14 03:45:15 +0900349 struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata;
Tejun Heob1fc3d62007-09-20 16:05:11 +0900350 struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr;
351 struct kobject *kobj = attr_sd->s_parent->s_dir.kobj;
Tejun Heoeb361652007-06-14 03:45:16 +0900352 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
Tejun Heoeb361652007-06-14 03:45:16 +0900354 mutex_lock(&bb->mutex);
Tejun Heo0ab66082007-06-14 03:45:16 +0900355
356 /* need attr_sd for attr, its parent for kobj */
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800357 rc = -ENODEV;
Tejun Heo0ab66082007-06-14 03:45:16 +0900358 if (!sysfs_get_active_two(attr_sd))
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800359 goto out_unlock;
Tejun Heo0ab66082007-06-14 03:45:16 +0900360
361 rc = -EINVAL;
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800362 if (!attr->mmap)
363 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900364
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800365 rc = attr->mmap(kobj, attr, vma);
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800366 if (rc)
367 goto out_put;
Tejun Heo0ab66082007-06-14 03:45:16 +0900368
Hugh Dickins095160a2009-03-23 01:41:27 +0000369 /*
370 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
371 * to satisfy versions of X which crash if the mmap fails: that
372 * substitutes a new vm_file, and we don't then want bin_vm_ops.
373 */
374 if (vma->vm_file != file)
375 goto out_put;
376
Eric W. Biedermane0edd3c2009-03-04 11:57:20 -0800377 rc = -EINVAL;
378 if (bb->mmapped && bb->vm_ops != vma->vm_ops)
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:
386 sysfs_put_active_two(attr_sd);
387out_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
393static int open(struct inode * inode, struct file * file)
394{
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 */
401 if (!sysfs_get_active_two(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 */
428 sysfs_put_active_two(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:
Tejun Heo078ce642007-09-20 16:05:10 +0900432 sysfs_put_active_two(attr_sd);
Tejun Heo0ab66082007-06-14 03:45:16 +0900433 kfree(bb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return error;
435}
436
437static int release(struct inode * inode, struct file * file)
438{
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;
463 struct hlist_node *tmp;
464
465 if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR)
466 return;
467
468 mutex_lock(&sysfs_bin_lock);
469
470 hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) {
471 struct inode *inode = bb->file->f_path.dentry->d_inode;
472
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
485int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr)
486{
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
Randy.Dunlap995982c2006-07-10 23:05:25 -0700499void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Alan Stern5f1835d2007-08-16 16:13:06 -0400501 sysfs_hash_and_remove(kobj->sd, attr->attr.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
504EXPORT_SYMBOL_GPL(sysfs_create_bin_file);
505EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);