Skip to content
Navigation Menu
Toggle navigation
Sign in
In this repository
All GitHub Enterprise
↵
Jump to
↵
No suggested jump to results
In this repository
All GitHub Enterprise
↵
Jump to
↵
In this organization
All GitHub Enterprise
↵
Jump to
↵
In this repository
All GitHub Enterprise
↵
Jump to
↵
Sign in
Reseting focus
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
mariux64
/
linux
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Issues
2
Pull requests
0
Actions
Projects
0
Wiki
Security
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security
Insights
Files
e0edd3c
Documentation
arch
block
crypto
drivers
firmware
fs
9p
adfs
affs
afs
autofs
autofs4
befs
bfs
btrfs
cifs
coda
configfs
cramfs
debugfs
devpts
dlm
ecryptfs
efs
exportfs
ext2
ext3
ext4
fat
freevxfs
fuse
gfs2
hfs
hfsplus
hostfs
hpfs
hppfs
hugetlbfs
isofs
jbd
jbd2
jffs2
jfs
lockd
minix
ncpfs
nfs
nfs_common
nfsd
nls
notify
ntfs
ocfs2
omfs
openpromfs
partitions
proc
qnx4
ramfs
reiserfs
romfs
smbfs
squashfs
sysfs
Kconfig
Makefile
bin.c
dir.c
file.c
group.c
inode.c
mount.c
symlink.c
sysfs.h
sysv
ubifs
udf
ufs
xfs
Kconfig
Kconfig.binfmt
Makefile
aio.c
anon_inodes.c
attr.c
bad_inode.c
binfmt_aout.c
binfmt_elf.c
binfmt_elf_fdpic.c
binfmt_em86.c
binfmt_flat.c
binfmt_misc.c
binfmt_script.c
binfmt_som.c
bio-integrity.c
bio.c
block_dev.c
buffer.c
char_dev.c
compat.c
compat_binfmt_elf.c
compat_ioctl.c
dcache.c
dcookies.c
direct-io.c
dquot.c
drop_caches.c
eventfd.c
eventpoll.c
exec.c
fcntl.c
fifo.c
file.c
file_table.c
filesystems.c
fs-writeback.c
generic_acl.c
inode.c
internal.h
ioctl.c
ioprio.c
libfs.c
locks.c
mbcache.c
mpage.c
namei.c
namespace.c
nfsctl.c
no-block.c
open.c
pipe.c
pnode.c
pnode.h
posix_acl.c
quota.c
quota_tree.c
quota_tree.h
quota_v1.c
quota_v2.c
quotaio_v1.h
quotaio_v2.h
read_write.c
read_write.h
readdir.c
select.c
seq_file.c
signalfd.c
splice.c
stack.c
stat.c
super.c
sync.c
timerfd.c
utimes.c
xattr.c
xattr_acl.c
include
init
ipc
kernel
lib
mm
net
samples
scripts
security
sound
usr
virt
.gitignore
.mailmap
COPYING
CREDITS
Kbuild
MAINTAINERS
Makefile
README
REPORTING-BUGS
Breadcrumbs
linux
/
fs
/
sysfs
/
bin.c
Blame
Blame
Latest commit
History
History
443 lines (343 loc) · 9.4 KB
Breadcrumbs
linux
/
fs
/
sysfs
/
bin.c
Top
File metadata and controls
Code
Blame
443 lines (343 loc) · 9.4 KB
Raw
/* * fs/sysfs/bin.c - sysfs binary file implementation * * Copyright (c) 2003 Patrick Mochel * Copyright (c) 2003 Matthew Wilcox * Copyright (c) 2004 Silicon Graphics, Inc. * Copyright (c) 2007 SUSE Linux Products GmbH * Copyright (c) 2007 Tejun Heo <teheo@suse.de> * * This file is released under the GPLv2. * * Please see Documentation/filesystems/sysfs.txt for more information. */ #undef DEBUG #include <linux/errno.h> #include <linux/fs.h> #include <linux/kernel.h> #include <linux/kobject.h> #include <linux/module.h> #include <linux/slab.h> #include <linux/mutex.h> #include <linux/mm.h> #include <asm/uaccess.h> #include "sysfs.h" /* * There's one bin_buffer for each open file. * * filp->private_data points to bin_buffer and * sysfs_dirent->s_bin_attr.buffers points to a the bin_buffer s * sysfs_dirent->s_bin_attr.buffers is protected by sysfs_bin_lock */ static DEFINE_MUTEX(sysfs_bin_lock); struct bin_buffer { struct mutex mutex; void *buffer; int mmapped; struct vm_operations_struct *vm_ops; struct file *file; struct hlist_node list; }; static int fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count) { struct sysfs_dirent *attr_sd = dentry->d_fsdata; struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; int rc; /* need attr_sd for attr, its parent for kobj */ if (!sysfs_get_active_two(attr_sd)) return -ENODEV; rc = -EIO; if (attr->read) rc = attr->read(kobj, attr, buffer, off, count); sysfs_put_active_two(attr_sd); return rc; } static ssize_t read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off) { struct bin_buffer *bb = file->private_data; struct dentry *dentry = file->f_path.dentry; int size = dentry->d_inode->i_size; loff_t offs = *off; int count = min_t(size_t, bytes, PAGE_SIZE); char *temp; if (!bytes) return 0; if (size) { if (offs > size) return 0; if (offs + count > size) count = size - offs; } temp = kmalloc(count, GFP_KERNEL); if (!temp) return -ENOMEM; mutex_lock(&bb->mutex); count = fill_read(dentry, bb->buffer, offs, count); if (count < 0) { mutex_unlock(&bb->mutex); goto out_free; } memcpy(temp, bb->buffer, count); mutex_unlock(&bb->mutex); if (copy_to_user(userbuf, temp, count)) { count = -EFAULT; goto out_free; } pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); *off = offs + count; out_free: kfree(temp); return count; } static int flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count) { struct sysfs_dirent *attr_sd = dentry->d_fsdata; struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; int rc; /* need attr_sd for attr, its parent for kobj */ if (!sysfs_get_active_two(attr_sd)) return -ENODEV; rc = -EIO; if (attr->write) rc = attr->write(kobj, attr, buffer, offset, count); sysfs_put_active_two(attr_sd); return rc; } static ssize_t write(struct file *file, const char __user *userbuf, size_t bytes, loff_t *off) { struct bin_buffer *bb = file->private_data; struct dentry *dentry = file->f_path.dentry; int size = dentry->d_inode->i_size; loff_t offs = *off; int count = min_t(size_t, bytes, PAGE_SIZE); char *temp; if (!bytes) return 0; if (size) { if (offs > size) return 0; if (offs + count > size) count = size - offs; } temp = kmalloc(count, GFP_KERNEL); if (!temp) return -ENOMEM; if (copy_from_user(temp, userbuf, count)) { count = -EFAULT; goto out_free; } mutex_lock(&bb->mutex); memcpy(bb->buffer, temp, count); count = flush_write(dentry, bb->buffer, offs, count); mutex_unlock(&bb->mutex); if (count > 0) *off = offs + count; out_free: kfree(temp); return count; } static void bin_vma_open(struct vm_area_struct *vma) { struct file *file = vma->vm_file; struct bin_buffer *bb = file->private_data; struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; if (!bb->vm_ops || !bb->vm_ops->open) return; if (!sysfs_get_active_two(attr_sd)) return; bb->vm_ops->open(vma); sysfs_put_active_two(attr_sd); } static void bin_vma_close(struct vm_area_struct *vma) { struct file *file = vma->vm_file; struct bin_buffer *bb = file->private_data; struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; if (!bb->vm_ops || !bb->vm_ops->close) return; if (!sysfs_get_active_two(attr_sd)) return; bb->vm_ops->close(vma); sysfs_put_active_two(attr_sd); } static int bin_fault(struct vm_area_struct *vma, struct vm_fault *vmf) { struct file *file = vma->vm_file; struct bin_buffer *bb = file->private_data; struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; int ret; if (!bb->vm_ops || !bb->vm_ops->fault) return VM_FAULT_SIGBUS; if (!sysfs_get_active_two(attr_sd)) return VM_FAULT_SIGBUS; ret = bb->vm_ops->fault(vma, vmf); sysfs_put_active_two(attr_sd); return ret; } static int bin_page_mkwrite(struct vm_area_struct *vma, struct page *page) { struct file *file = vma->vm_file; struct bin_buffer *bb = file->private_data; struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; int ret; if (!bb->vm_ops || !bb->vm_ops->page_mkwrite) return -EINVAL; if (!sysfs_get_active_two(attr_sd)) return -EINVAL; ret = bb->vm_ops->page_mkwrite(vma, page); sysfs_put_active_two(attr_sd); return ret; } static int bin_access(struct vm_area_struct *vma, unsigned long addr, void *buf, int len, int write) { struct file *file = vma->vm_file; struct bin_buffer *bb = file->private_data; struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; int ret; if (!bb->vm_ops || !bb->vm_ops->access) return -EINVAL; if (!sysfs_get_active_two(attr_sd)) return -EINVAL; ret = bb->vm_ops->access(vma, addr, buf, len, write); sysfs_put_active_two(attr_sd); return ret; } static struct vm_operations_struct bin_vm_ops = { .open = bin_vma_open, .close = bin_vma_close, .fault = bin_fault, .page_mkwrite = bin_page_mkwrite, .access = bin_access, }; static int mmap(struct file *file, struct vm_area_struct *vma) { struct bin_buffer *bb = file->private_data; struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; struct vm_operations_struct *vm_ops; int rc; mutex_lock(&bb->mutex); /* need attr_sd for attr, its parent for kobj */ rc = -ENODEV; if (!sysfs_get_active_two(attr_sd)) goto out_unlock; rc = -EINVAL; if (!attr->mmap) goto out_put; rc = attr->mmap(kobj, attr, vma); vm_ops = vma->vm_ops; vma->vm_ops = &bin_vm_ops; if (rc) goto out_put; rc = -EINVAL; if (bb->mmapped && bb->vm_ops != vma->vm_ops) goto out_put; #ifdef CONFIG_NUMA rc = -EINVAL; if (vm_ops && ((vm_ops->set_policy || vm_ops->get_policy || vm_ops->migrate))) goto out_put; #endif rc = 0; bb->mmapped = 1; bb->vm_ops = vm_ops; out_put: sysfs_put_active_two(attr_sd); out_unlock: mutex_unlock(&bb->mutex); return rc; } static int open(struct inode * inode, struct file * file) { struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; struct bin_buffer *bb = NULL; int error; /* binary file operations requires both @sd and its parent */ if (!sysfs_get_active_two(attr_sd)) return -ENODEV; error = -EACCES; if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) goto err_out; if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) goto err_out; error = -ENOMEM; bb = kzalloc(sizeof(*bb), GFP_KERNEL); if (!bb) goto err_out; bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); if (!bb->buffer) goto err_out; mutex_init(&bb->mutex); bb->file = file; file->private_data = bb; mutex_lock(&sysfs_bin_lock); hlist_add_head(&bb->list, &attr_sd->s_bin_attr.buffers); mutex_unlock(&sysfs_bin_lock); /* open succeeded, put active references */ sysfs_put_active_two(attr_sd); return 0; err_out: sysfs_put_active_two(attr_sd); kfree(bb); return error; } static int release(struct inode * inode, struct file * file) { struct bin_buffer *bb = file->private_data; mutex_lock(&sysfs_bin_lock); hlist_del(&bb->list); mutex_unlock(&sysfs_bin_lock); kfree(bb->buffer); kfree(bb); return 0; } const struct file_operations bin_fops = { .read = read, .write = write, .mmap = mmap, .llseek = generic_file_llseek, .open = open, .release = release, }; void unmap_bin_file(struct sysfs_dirent *attr_sd) { struct bin_buffer *bb; struct hlist_node *tmp; if (sysfs_type(attr_sd) != SYSFS_KOBJ_BIN_ATTR) return; mutex_lock(&sysfs_bin_lock); hlist_for_each_entry(bb, tmp, &attr_sd->s_bin_attr.buffers, list) { struct inode *inode = bb->file->f_path.dentry->d_inode; unmap_mapping_range(inode->i_mapping, 0, 0, 1); } mutex_unlock(&sysfs_bin_lock); } /** * sysfs_create_bin_file - create binary file for object. * @kobj: object. * @attr: attribute descriptor. */ int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) { BUG_ON(!kobj || !kobj->sd || !attr); return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); } /** * sysfs_remove_bin_file - remove binary file for object. * @kobj: object. * @attr: attribute descriptor. */ void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) { sysfs_hash_and_remove(kobj->sd, attr->attr.name); } EXPORT_SYMBOL_GPL(sysfs_create_bin_file); EXPORT_SYMBOL_GPL(sysfs_remove_bin_file);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
You can’t perform that action at this time.