]>
Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
6d66f5cd | 2 | * fs/sysfs/bin.c - sysfs binary file implementation |
1da177e4 LT |
3 | * |
4 | * Copyright (c) 2003 Patrick Mochel | |
5 | * Copyright (c) 2003 Matthew Wilcox | |
6 | * Copyright (c) 2004 Silicon Graphics, Inc. | |
6d66f5cd TH |
7 | * 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. | |
1da177e4 LT |
13 | */ |
14 | ||
15 | #undef DEBUG | |
16 | ||
17 | #include <linux/errno.h> | |
18 | #include <linux/fs.h> | |
995982ca | 19 | #include <linux/kernel.h> |
1da177e4 LT |
20 | #include <linux/kobject.h> |
21 | #include <linux/module.h> | |
22 | #include <linux/slab.h> | |
869512ab | 23 | #include <linux/mutex.h> |
1da177e4 LT |
24 | |
25 | #include <asm/uaccess.h> | |
26 | ||
27 | #include "sysfs.h" | |
28 | ||
eb361653 TH |
29 | struct bin_buffer { |
30 | struct mutex mutex; | |
31 | void *buffer; | |
0ab66088 | 32 | int mmapped; |
eb361653 TH |
33 | }; |
34 | ||
1da177e4 LT |
35 | static int |
36 | fill_read(struct dentry *dentry, char *buffer, loff_t off, size_t count) | |
37 | { | |
3e519038 | 38 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d61 TH |
39 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
40 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
0ab66088 TH |
41 | int rc; |
42 | ||
43 | /* need attr_sd for attr, its parent for kobj */ | |
44 | if (!sysfs_get_active_two(attr_sd)) | |
45 | return -ENODEV; | |
1da177e4 | 46 | |
0ab66088 TH |
47 | rc = -EIO; |
48 | if (attr->read) | |
91a69029 | 49 | rc = attr->read(kobj, attr, buffer, off, count); |
1da177e4 | 50 | |
0ab66088 TH |
51 | sysfs_put_active_two(attr_sd); |
52 | ||
53 | return rc; | |
1da177e4 LT |
54 | } |
55 | ||
56 | static ssize_t | |
93e3cd82 | 57 | read(struct file *file, char __user *userbuf, size_t bytes, loff_t *off) |
1da177e4 | 58 | { |
eb361653 | 59 | struct bin_buffer *bb = file->private_data; |
f427f5d5 | 60 | struct dentry *dentry = file->f_path.dentry; |
1da177e4 LT |
61 | int size = dentry->d_inode->i_size; |
62 | loff_t offs = *off; | |
93e3cd82 | 63 | int count = min_t(size_t, bytes, PAGE_SIZE); |
1da177e4 LT |
64 | |
65 | if (size) { | |
66 | if (offs > size) | |
67 | return 0; | |
68 | if (offs + count > size) | |
69 | count = size - offs; | |
70 | } | |
71 | ||
eb361653 TH |
72 | mutex_lock(&bb->mutex); |
73 | ||
74 | count = fill_read(dentry, bb->buffer, offs, count); | |
93e3cd82 | 75 | if (count < 0) |
eb361653 | 76 | goto out_unlock; |
1da177e4 | 77 | |
eb361653 TH |
78 | if (copy_to_user(userbuf, bb->buffer, count)) { |
79 | count = -EFAULT; | |
80 | goto out_unlock; | |
81 | } | |
1da177e4 | 82 | |
93e3cd82 | 83 | pr_debug("offs = %lld, *off = %lld, count = %d\n", offs, *off, count); |
1da177e4 LT |
84 | |
85 | *off = offs + count; | |
86 | ||
eb361653 TH |
87 | out_unlock: |
88 | mutex_unlock(&bb->mutex); | |
1da177e4 LT |
89 | return count; |
90 | } | |
91 | ||
92 | static int | |
93 | flush_write(struct dentry *dentry, char *buffer, loff_t offset, size_t count) | |
94 | { | |
3e519038 | 95 | struct sysfs_dirent *attr_sd = dentry->d_fsdata; |
b1fc3d61 TH |
96 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
97 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
0ab66088 TH |
98 | int rc; |
99 | ||
100 | /* need attr_sd for attr, its parent for kobj */ | |
101 | if (!sysfs_get_active_two(attr_sd)) | |
102 | return -ENODEV; | |
1da177e4 | 103 | |
0ab66088 TH |
104 | rc = -EIO; |
105 | if (attr->write) | |
91a69029 | 106 | rc = attr->write(kobj, attr, buffer, offset, count); |
1da177e4 | 107 | |
0ab66088 TH |
108 | sysfs_put_active_two(attr_sd); |
109 | ||
110 | return rc; | |
1da177e4 LT |
111 | } |
112 | ||
93e3cd82 TH |
113 | static ssize_t write(struct file *file, const char __user *userbuf, |
114 | size_t bytes, loff_t *off) | |
1da177e4 | 115 | { |
eb361653 | 116 | struct bin_buffer *bb = file->private_data; |
f427f5d5 | 117 | struct dentry *dentry = file->f_path.dentry; |
1da177e4 LT |
118 | int size = dentry->d_inode->i_size; |
119 | loff_t offs = *off; | |
93e3cd82 | 120 | int count = min_t(size_t, bytes, PAGE_SIZE); |
1da177e4 | 121 | |
1da177e4 LT |
122 | if (size) { |
123 | if (offs > size) | |
124 | return 0; | |
125 | if (offs + count > size) | |
126 | count = size - offs; | |
127 | } | |
128 | ||
eb361653 TH |
129 | mutex_lock(&bb->mutex); |
130 | ||
131 | if (copy_from_user(bb->buffer, userbuf, count)) { | |
132 | count = -EFAULT; | |
133 | goto out_unlock; | |
134 | } | |
1da177e4 | 135 | |
eb361653 | 136 | count = flush_write(dentry, bb->buffer, offs, count); |
1da177e4 LT |
137 | if (count > 0) |
138 | *off = offs + count; | |
eb361653 TH |
139 | |
140 | out_unlock: | |
141 | mutex_unlock(&bb->mutex); | |
1da177e4 LT |
142 | return count; |
143 | } | |
144 | ||
145 | static int mmap(struct file *file, struct vm_area_struct *vma) | |
146 | { | |
eb361653 | 147 | struct bin_buffer *bb = file->private_data; |
3e519038 | 148 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 TH |
149 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
150 | struct kobject *kobj = attr_sd->s_parent->s_dir.kobj; | |
eb361653 | 151 | int rc; |
1da177e4 | 152 | |
eb361653 | 153 | mutex_lock(&bb->mutex); |
0ab66088 TH |
154 | |
155 | /* need attr_sd for attr, its parent for kobj */ | |
156 | if (!sysfs_get_active_two(attr_sd)) | |
157 | return -ENODEV; | |
158 | ||
159 | rc = -EINVAL; | |
160 | if (attr->mmap) | |
161 | rc = attr->mmap(kobj, attr, vma); | |
162 | ||
163 | if (rc == 0 && !bb->mmapped) | |
164 | bb->mmapped = 1; | |
165 | else | |
166 | sysfs_put_active_two(attr_sd); | |
167 | ||
eb361653 TH |
168 | mutex_unlock(&bb->mutex); |
169 | ||
170 | return rc; | |
1da177e4 LT |
171 | } |
172 | ||
173 | static int open(struct inode * inode, struct file * file) | |
174 | { | |
3e519038 | 175 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
b1fc3d61 | 176 | struct bin_attribute *attr = attr_sd->s_bin_attr.bin_attr; |
eb361653 | 177 | struct bin_buffer *bb = NULL; |
0ab66088 | 178 | int error; |
1da177e4 | 179 | |
078ce640 TH |
180 | /* binary file operations requires both @sd and its parent */ |
181 | if (!sysfs_get_active_two(attr_sd)) | |
0ab66088 | 182 | return -ENODEV; |
1da177e4 | 183 | |
1da177e4 LT |
184 | error = -EACCES; |
185 | if ((file->f_mode & FMODE_WRITE) && !(attr->write || attr->mmap)) | |
7b595756 | 186 | goto err_out; |
1da177e4 | 187 | if ((file->f_mode & FMODE_READ) && !(attr->read || attr->mmap)) |
7b595756 | 188 | goto err_out; |
1da177e4 LT |
189 | |
190 | error = -ENOMEM; | |
eb361653 TH |
191 | bb = kzalloc(sizeof(*bb), GFP_KERNEL); |
192 | if (!bb) | |
7b595756 | 193 | goto err_out; |
1da177e4 | 194 | |
eb361653 TH |
195 | bb->buffer = kmalloc(PAGE_SIZE, GFP_KERNEL); |
196 | if (!bb->buffer) | |
7b595756 | 197 | goto err_out; |
eb361653 TH |
198 | |
199 | mutex_init(&bb->mutex); | |
200 | file->private_data = bb; | |
201 | ||
078ce640 TH |
202 | /* open succeeded, put active references */ |
203 | sysfs_put_active_two(attr_sd); | |
0ab66088 | 204 | return 0; |
1da177e4 | 205 | |
7b595756 | 206 | err_out: |
078ce640 | 207 | sysfs_put_active_two(attr_sd); |
0ab66088 | 208 | kfree(bb); |
1da177e4 LT |
209 | return error; |
210 | } | |
211 | ||
212 | static int release(struct inode * inode, struct file * file) | |
213 | { | |
3e519038 | 214 | struct sysfs_dirent *attr_sd = file->f_path.dentry->d_fsdata; |
eb361653 | 215 | struct bin_buffer *bb = file->private_data; |
1da177e4 | 216 | |
0ab66088 TH |
217 | if (bb->mmapped) |
218 | sysfs_put_active_two(attr_sd); | |
eb361653 TH |
219 | kfree(bb->buffer); |
220 | kfree(bb); | |
1da177e4 LT |
221 | return 0; |
222 | } | |
223 | ||
4b6f5d20 | 224 | const struct file_operations bin_fops = { |
1da177e4 LT |
225 | .read = read, |
226 | .write = write, | |
227 | .mmap = mmap, | |
228 | .llseek = generic_file_llseek, | |
229 | .open = open, | |
230 | .release = release, | |
231 | }; | |
232 | ||
233 | /** | |
234 | * sysfs_create_bin_file - create binary file for object. | |
235 | * @kobj: object. | |
236 | * @attr: attribute descriptor. | |
1da177e4 LT |
237 | */ |
238 | ||
239 | int sysfs_create_bin_file(struct kobject * kobj, struct bin_attribute * attr) | |
240 | { | |
608e266a | 241 | BUG_ON(!kobj || !kobj->sd || !attr); |
1da177e4 | 242 | |
608e266a | 243 | return sysfs_add_file(kobj->sd, &attr->attr, SYSFS_KOBJ_BIN_ATTR); |
1da177e4 LT |
244 | } |
245 | ||
246 | ||
247 | /** | |
248 | * sysfs_remove_bin_file - remove binary file for object. | |
249 | * @kobj: object. | |
250 | * @attr: attribute descriptor. | |
1da177e4 LT |
251 | */ |
252 | ||
995982ca | 253 | void sysfs_remove_bin_file(struct kobject * kobj, struct bin_attribute * attr) |
1da177e4 | 254 | { |
5f1835da | 255 | sysfs_hash_and_remove(kobj->sd, attr->attr.name); |
1da177e4 LT |
256 | } |
257 | ||
258 | EXPORT_SYMBOL_GPL(sysfs_create_bin_file); | |
259 | EXPORT_SYMBOL_GPL(sysfs_remove_bin_file); |