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>
26 #include <asm/uaccess.h>
31 * There's one bin_buffer for each open file.
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
37 static DEFINE_MUTEX(sysfs_bin_lock
);
43 const struct vm_operations_struct
*vm_ops
;
45 struct hlist_node list
;
49 fill_read(struct file
*file
, char *buffer
, loff_t off
, size_t count
)
51 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
52 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
53 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
56 /* need attr_sd for attr, its parent for kobj */
57 if (!sysfs_get_active(attr_sd
))
62 rc
= attr
->read(file
, kobj
, attr
, buffer
, off
, count
);
64 sysfs_put_active(attr_sd
);
70 read(struct file
*file
, char __user
*userbuf
, size_t bytes
, loff_t
*off
)
72 struct bin_buffer
*bb
= file
->private_data
;
73 int size
= file_inode(file
)->i_size
;
75 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
84 if (offs
+ count
> size
)
88 temp
= kmalloc(count
, GFP_KERNEL
);
92 mutex_lock(&bb
->mutex
);
94 count
= fill_read(file
, bb
->buffer
, offs
, count
);
96 mutex_unlock(&bb
->mutex
);
100 memcpy(temp
, bb
->buffer
, count
);
102 mutex_unlock(&bb
->mutex
);
104 if (copy_to_user(userbuf
, temp
, count
)) {
109 pr_debug("offs = %lld, *off = %lld, count = %d\n", offs
, *off
, count
);
119 flush_write(struct file
*file
, char *buffer
, loff_t offset
, size_t count
)
121 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
122 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
123 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
126 /* need attr_sd for attr, its parent for kobj */
127 if (!sysfs_get_active(attr_sd
))
132 rc
= attr
->write(file
, kobj
, attr
, buffer
, offset
, count
);
134 sysfs_put_active(attr_sd
);
139 static ssize_t
write(struct file
*file
, const char __user
*userbuf
,
140 size_t bytes
, loff_t
*off
)
142 struct bin_buffer
*bb
= file
->private_data
;
143 int size
= file_inode(file
)->i_size
;
145 int count
= min_t(size_t, bytes
, PAGE_SIZE
);
154 if (offs
+ count
> size
)
158 temp
= memdup_user(userbuf
, count
);
160 return PTR_ERR(temp
);
162 mutex_lock(&bb
->mutex
);
164 memcpy(bb
->buffer
, temp
, count
);
166 count
= flush_write(file
, bb
->buffer
, offs
, count
);
167 mutex_unlock(&bb
->mutex
);
176 static void bin_vma_open(struct vm_area_struct
*vma
)
178 struct file
*file
= vma
->vm_file
;
179 struct bin_buffer
*bb
= file
->private_data
;
180 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
185 if (!sysfs_get_active(attr_sd
))
188 if (bb
->vm_ops
->open
)
189 bb
->vm_ops
->open(vma
);
191 sysfs_put_active(attr_sd
);
194 static int bin_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
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
;
202 return VM_FAULT_SIGBUS
;
204 if (!sysfs_get_active(attr_sd
))
205 return VM_FAULT_SIGBUS
;
207 ret
= VM_FAULT_SIGBUS
;
208 if (bb
->vm_ops
->fault
)
209 ret
= bb
->vm_ops
->fault(vma
, vmf
);
211 sysfs_put_active(attr_sd
);
215 static int bin_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
217 struct file
*file
= vma
->vm_file
;
218 struct bin_buffer
*bb
= file
->private_data
;
219 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
223 return VM_FAULT_SIGBUS
;
225 if (!sysfs_get_active(attr_sd
))
226 return VM_FAULT_SIGBUS
;
229 if (bb
->vm_ops
->page_mkwrite
)
230 ret
= bb
->vm_ops
->page_mkwrite(vma
, vmf
);
232 file_update_time(file
);
234 sysfs_put_active(attr_sd
);
238 static int bin_access(struct vm_area_struct
*vma
, unsigned long addr
,
239 void *buf
, int len
, int write
)
241 struct file
*file
= vma
->vm_file
;
242 struct bin_buffer
*bb
= file
->private_data
;
243 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
249 if (!sysfs_get_active(attr_sd
))
253 if (bb
->vm_ops
->access
)
254 ret
= bb
->vm_ops
->access(vma
, addr
, buf
, len
, write
);
256 sysfs_put_active(attr_sd
);
261 static int bin_set_policy(struct vm_area_struct
*vma
, struct mempolicy
*new)
263 struct file
*file
= vma
->vm_file
;
264 struct bin_buffer
*bb
= file
->private_data
;
265 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
271 if (!sysfs_get_active(attr_sd
))
275 if (bb
->vm_ops
->set_policy
)
276 ret
= bb
->vm_ops
->set_policy(vma
, new);
278 sysfs_put_active(attr_sd
);
282 static struct mempolicy
*bin_get_policy(struct vm_area_struct
*vma
,
285 struct file
*file
= vma
->vm_file
;
286 struct bin_buffer
*bb
= file
->private_data
;
287 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
288 struct mempolicy
*pol
;
291 return vma
->vm_policy
;
293 if (!sysfs_get_active(attr_sd
))
294 return vma
->vm_policy
;
296 pol
= vma
->vm_policy
;
297 if (bb
->vm_ops
->get_policy
)
298 pol
= bb
->vm_ops
->get_policy(vma
, addr
);
300 sysfs_put_active(attr_sd
);
304 static int bin_migrate(struct vm_area_struct
*vma
, const nodemask_t
*from
,
305 const nodemask_t
*to
, unsigned long flags
)
307 struct file
*file
= vma
->vm_file
;
308 struct bin_buffer
*bb
= file
->private_data
;
309 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
315 if (!sysfs_get_active(attr_sd
))
319 if (bb
->vm_ops
->migrate
)
320 ret
= bb
->vm_ops
->migrate(vma
, from
, to
, flags
);
322 sysfs_put_active(attr_sd
);
327 static const struct vm_operations_struct bin_vm_ops
= {
328 .open
= bin_vma_open
,
330 .page_mkwrite
= bin_page_mkwrite
,
331 .access
= bin_access
,
333 .set_policy
= bin_set_policy
,
334 .get_policy
= bin_get_policy
,
335 .migrate
= bin_migrate
,
339 static int mmap(struct file
*file
, struct vm_area_struct
*vma
)
341 struct bin_buffer
*bb
= file
->private_data
;
342 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
343 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
344 struct kobject
*kobj
= attr_sd
->s_parent
->s_dir
.kobj
;
347 mutex_lock(&bb
->mutex
);
349 /* need attr_sd for attr, its parent for kobj */
351 if (!sysfs_get_active(attr_sd
))
358 rc
= attr
->mmap(file
, kobj
, attr
, vma
);
363 * PowerPC's pci_mmap of legacy_mem uses shmem_zero_setup()
364 * to satisfy versions of X which crash if the mmap fails: that
365 * substitutes a new vm_file, and we don't then want bin_vm_ops.
367 if (vma
->vm_file
!= file
)
371 if (bb
->mmapped
&& bb
->vm_ops
!= vma
->vm_ops
)
375 * It is not possible to successfully wrap close.
376 * So error if someone is trying to use close.
379 if (vma
->vm_ops
&& vma
->vm_ops
->close
)
384 bb
->vm_ops
= vma
->vm_ops
;
385 vma
->vm_ops
= &bin_vm_ops
;
387 sysfs_put_active(attr_sd
);
389 mutex_unlock(&bb
->mutex
);
394 static int open(struct inode
* inode
, struct file
* file
)
396 struct sysfs_dirent
*attr_sd
= file
->f_path
.dentry
->d_fsdata
;
397 struct bin_attribute
*attr
= attr_sd
->s_bin_attr
.bin_attr
;
398 struct bin_buffer
*bb
= NULL
;
401 /* binary file operations requires both @sd and its parent */
402 if (!sysfs_get_active(attr_sd
))
406 if ((file
->f_mode
& FMODE_WRITE
) && !(attr
->write
|| attr
->mmap
))
408 if ((file
->f_mode
& FMODE_READ
) && !(attr
->read
|| attr
->mmap
))
412 bb
= kzalloc(sizeof(*bb
), GFP_KERNEL
);
416 bb
->buffer
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
420 mutex_init(&bb
->mutex
);
422 file
->private_data
= bb
;
424 mutex_lock(&sysfs_bin_lock
);
425 hlist_add_head(&bb
->list
, &attr_sd
->s_bin_attr
.buffers
);
426 mutex_unlock(&sysfs_bin_lock
);
428 /* open succeeded, put active references */
429 sysfs_put_active(attr_sd
);
433 sysfs_put_active(attr_sd
);
438 static int release(struct inode
* inode
, struct file
* file
)
440 struct bin_buffer
*bb
= file
->private_data
;
442 mutex_lock(&sysfs_bin_lock
);
443 hlist_del(&bb
->list
);
444 mutex_unlock(&sysfs_bin_lock
);
451 const struct file_operations bin_fops
= {
455 .llseek
= generic_file_llseek
,
461 void unmap_bin_file(struct sysfs_dirent
*attr_sd
)
463 struct bin_buffer
*bb
;
465 if (sysfs_type(attr_sd
) != SYSFS_KOBJ_BIN_ATTR
)
468 mutex_lock(&sysfs_bin_lock
);
470 hlist_for_each_entry(bb
, &attr_sd
->s_bin_attr
.buffers
, list
) {
471 struct inode
*inode
= file_inode(bb
->file
);
473 unmap_mapping_range(inode
->i_mapping
, 0, 0, 1);
476 mutex_unlock(&sysfs_bin_lock
);
480 * sysfs_create_bin_file - create binary file for object.
482 * @attr: attribute descriptor.
485 int sysfs_create_bin_file(struct kobject
*kobj
,
486 const struct bin_attribute
*attr
)
488 BUG_ON(!kobj
|| !kobj
->sd
|| !attr
);
490 return sysfs_add_file(kobj
->sd
, &attr
->attr
, SYSFS_KOBJ_BIN_ATTR
);
495 * sysfs_remove_bin_file - remove binary file for object.
497 * @attr: attribute descriptor.
500 void sysfs_remove_bin_file(struct kobject
*kobj
,
501 const struct bin_attribute
*attr
)
503 sysfs_hash_and_remove(kobj
->sd
, NULL
, attr
->attr
.name
);
506 EXPORT_SYMBOL_GPL(sysfs_create_bin_file
);
507 EXPORT_SYMBOL_GPL(sysfs_remove_bin_file
);