2 * fs/sysfs/bin.c - sysfs binary file implementation
4 * Copyright (c) 2003 Patrick Mochel
5 * Copyright (c) 2003 Matthew Wilcox
6 * Copyright (c) 2004 Silicon Graphics, Inc.
7 * Copyright (c) 2007 SUSE Linux Products GmbH
8 * Copyright (c) 2007 Tejun Heo <teheo@suse.de>
10 * This file is released under the GPLv2.
12 * Please see Documentation/filesystems/sysfs.txt for more information.
17 #include <linux/errno.h>
19 #include <linux/kernel.h>
20 #include <linux/kobject.h>
21 #include <linux/module.h>
22 #include <linux/slab.h>
23 #include <linux/mutex.h>
25 #include <linux/uaccess.h>
30 * There's one bin_buffer for each open file.
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
36 static DEFINE_MUTEX(sysfs_bin_lock
);
42 const struct vm_operations_struct
*vm_ops
;
44 struct hlist_node list
;
48 fill_read(struct file
*file
, char *buffer
, loff_t off
, size_t count
)
50 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
51 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
52 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
55 /* need attr_sd for attr, its parent for kobj */
56 if (!sysfs_get_active(attr_sd
))
61 rc
= attr
->read(file
, kobj
, attr
, buffer
, off
, count
);
63 sysfs_put_active(attr_sd
);
69 read(struct file
*file
, char __user
*userbuf
, size_t bytes
, loff_t
*off
)
71 struct bin_buffer
*bb
= file
->private_data
;
72 int size
= file_inode(file
)->i_size
;
74 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
83 if (offs
+ count
> size
)
87 temp
= kmalloc(count
, GFP_KERNEL
);
91 mutex_lock(&bb
->mutex
);
93 count
= fill_read(file
, bb
->buffer
, offs
, count
);
95 mutex_unlock(&bb
->mutex
);
99 memcpy(temp
, bb
->buffer
, count
);
101 mutex_unlock(&bb
->mutex
);
103 if (copy_to_user(userbuf
, temp
, count
)) {
108 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs
, *off
, count
);
118 flush_write(struct file
*file
, char *buffer
, loff_t offset
, size_t count
)
120 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
121 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
122 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
125 /* need attr_sd for attr, its parent for kobj */
126 if (!sysfs_get_active(attr_sd
))
131 rc
= attr
->write(file
, kobj
, attr
, buffer
, offset
, count
);
133 sysfs_put_active(attr_sd
);
138 static ssize_t
write(struct file
*file
, const char __user
*userbuf
,
139 size_t bytes
, loff_t
*off
)
141 struct bin_buffer
*bb
= file
->private_data
;
142 int size
= file_inode(file
)->i_size
;
144 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
153 if (offs
+ count
> size
)
157 temp
= memdup_user(userbuf
, count
);
159 return PTR_ERR(temp
);
161 mutex_lock(&bb
->mutex
);
163 memcpy(bb
->buffer
, temp
, count
);
165 count
= flush_write(file
, bb
->buffer
, offs
, count
);
166 mutex_unlock(&bb
->mutex
);
175 static void bin_vma_open(struct vm_area_struct
*vma
)
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
;
184 if (!sysfs_get_active(attr_sd
))
187 if (bb
->vm_ops
->open
)
188 bb
->vm_ops
->open(vma
);
190 sysfs_put_active(attr_sd
);
193 static int bin_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
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
;
201 return VM_FAULT_SIGBUS
;
203 if (!sysfs_get_active(attr_sd
))
204 return VM_FAULT_SIGBUS
;
206 ret
= VM_FAULT_SIGBUS
;
207 if (bb
->vm_ops
->fault
)
208 ret
= bb
->vm_ops
->fault(vma
, vmf
);
210 sysfs_put_active(attr_sd
);
214 static int bin_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
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
;
222 return VM_FAULT_SIGBUS
;
224 if (!sysfs_get_active(attr_sd
))
225 return VM_FAULT_SIGBUS
;
228 if (bb
->vm_ops
->page_mkwrite
)
229 ret
= bb
->vm_ops
->page_mkwrite(vma
, vmf
);
231 file_update_time(file
);
233 sysfs_put_active(attr_sd
);
237 static int bin_access(struct vm_area_struct
*vma
, unsigned long addr
,
238 void *buf
, int len
, int write
)
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
;
248 if (!sysfs_get_active(attr_sd
))
252 if (bb
->vm_ops
->access
)
253 ret
= bb
->vm_ops
->access(vma
, addr
, buf
, len
, write
);
255 sysfs_put_active(attr_sd
);
260 static int bin_set_policy(struct vm_area_struct
*vma
, struct mempolicy
*new)
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
;
270 if (!sysfs_get_active(attr_sd
))
274 if (bb
->vm_ops
->set_policy
)
275 ret
= bb
->vm_ops
->set_policy(vma
, new);
277 sysfs_put_active(attr_sd
);
281 static struct mempolicy
*bin_get_policy(struct vm_area_struct
*vma
,
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
;
290 return vma
->vm_policy
;
292 if (!sysfs_get_active(attr_sd
))
293 return vma
->vm_policy
;
295 pol
= vma
->vm_policy
;
296 if (bb
->vm_ops
->get_policy
)
297 pol
= bb
->vm_ops
->get_policy(vma
, addr
);
299 sysfs_put_active(attr_sd
);
303 static int bin_migrate(struct vm_area_struct
*vma
, const nodemask_t
*from
,
304 const nodemask_t
*to
, unsigned long flags
)
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
;
314 if (!sysfs_get_active(attr_sd
))
318 if (bb
->vm_ops
->migrate
)
319 ret
= bb
->vm_ops
->migrate(vma
, from
, to
, flags
);
321 sysfs_put_active(attr_sd
);
326 static const struct vm_operations_struct bin_vm_ops
= {
327 .open
= bin_vma_open
,
329 .page_mkwrite
= bin_page_mkwrite
,
330 .access
= bin_access
,
332 .set_policy
= bin_set_policy
,
333 .get_policy
= bin_get_policy
,
334 .migrate
= bin_migrate
,
338 static int mmap(struct file
*file
, struct vm_area_struct
*vma
)
340 struct bin_buffer
*bb
= file
->private_data
;
341 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
342 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
343 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
346 mutex_lock(&bb
->mutex
);
348 /* need attr_sd for attr, its parent for kobj */
350 if (!sysfs_get_active(attr_sd
))
357 rc
= attr
->mmap(file
, kobj
, attr
, vma
);
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.
366 if (vma
->vm_file
!= file
)
370 if (bb
->mmapped
&& bb
->vm_ops
!= vma
->vm_ops
)
374 * It is not possible to successfully wrap close.
375 * So error if someone is trying to use close.
378 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
383 bb
->vm_ops
= vma
->vm_ops
;
384 vma
->vm_ops
= &bin_vm_ops
;
386 sysfs_put_active(attr_sd
);
388 mutex_unlock(&bb
->mutex
);
393 static int open(struct inode
*inode
, struct file
*file
)
395 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
396 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
397 struct bin_buffer
*bb
= NULL
;
400 /* binary file operations requires both @sd and its parent */
401 if (!sysfs_get_active(attr_sd
))
405 if ((file
->f_mode
& FMODE_WRITE
) && !(attr
->write
|| attr
->mmap
))
407 if ((file
->f_mode
& FMODE_READ
) && !(attr
->read
|| attr
->mmap
))
411 bb
= kzalloc(sizeof(*bb
), GFP_KERNEL
);
415 bb
->buffer
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
419 mutex_init(&bb
->mutex
);
421 file
->private_data
= bb
;
423 mutex_lock(&sysfs_bin_lock
);
424 hlist_add_head(&bb
->list
, &attr_sd
->s_bin_attr
.buffers
);
425 mutex_unlock(&sysfs_bin_lock
);
427 /* open succeeded, put active references */
428 sysfs_put_active(attr_sd
);
432 sysfs_put_active(attr_sd
);
437 static int release(struct inode
*inode
, struct file
*file
)
439 struct bin_buffer
*bb
= file
->private_data
;
441 mutex_lock(&sysfs_bin_lock
);
442 hlist_del(&bb
->list
);
443 mutex_unlock(&sysfs_bin_lock
);
450 const struct file_operations bin_fops
= {
454 .llseek
= generic_file_llseek
,
460 void unmap_bin_file(struct sysfs_dirent
*attr_sd
)
462 struct bin_buffer
*bb
;
464 if (sysfs_type(attr_sd
) != SYSFS_KOBJ_BIN_ATTR
)
467 mutex_lock(&sysfs_bin_lock
);
469 hlist_for_each_entry(bb
, &attr_sd
->s_bin_attr
.buffers
, list
) {
470 struct inode
*inode
= file_inode(bb
->file
);
472 unmap_mapping_range(inode
->i_mapping
, 0, 0, 1);
475 mutex_unlock(&sysfs_bin_lock
);
479 * sysfs_create_bin_file - create binary file for object.
481 * @attr: attribute descriptor.
483 int sysfs_create_bin_file(struct kobject
*kobj
,
484 const struct bin_attribute
*attr
)
486 BUG_ON(!kobj
|| !kobj
->sd
|| !attr
);
488 return sysfs_add_file(kobj
->sd
, &attr
->attr
, SYSFS_KOBJ_BIN_ATTR
);
490 EXPORT_SYMBOL_GPL(sysfs_create_bin_file
);
493 * sysfs_remove_bin_file - remove binary file for object.
495 * @attr: attribute descriptor.
497 void sysfs_remove_bin_file(struct kobject
*kobj
,
498 const struct bin_attribute
*attr
)
500 sysfs_hash_and_remove(kobj
->sd
, NULL
, attr
->attr
.name
);
502 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file
);