This source file includes following definitions.
- to_frag
- fill_read_buffer
- configfs_read_file
- configfs_read_bin_file
- fill_write_buffer
- flush_write_buffer
- configfs_write_file
- configfs_write_bin_file
- __configfs_open_file
- configfs_release
- configfs_open_file
- configfs_open_bin_file
- configfs_release_bin_file
- configfs_create_file
- configfs_create_bin_file
1
2
3
4
5
6
7
8
9
10
11
12
13 #include <linux/fs.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/mutex.h>
17 #include <linux/vmalloc.h>
18 #include <linux/uaccess.h>
19
20 #include <linux/configfs.h>
21 #include "configfs_internal.h"
22
23
24
25
26
27
28
29 #define SIMPLE_ATTR_SIZE 4096
30
31 struct configfs_buffer {
32 size_t count;
33 loff_t pos;
34 char * page;
35 struct configfs_item_operations * ops;
36 struct mutex mutex;
37 int needs_read_fill;
38 bool read_in_progress;
39 bool write_in_progress;
40 char *bin_buffer;
41 int bin_buffer_size;
42 int cb_max_size;
43 struct config_item *item;
44 struct module *owner;
45 union {
46 struct configfs_attribute *attr;
47 struct configfs_bin_attribute *bin_attr;
48 };
49 };
50
51 static inline struct configfs_fragment *to_frag(struct file *file)
52 {
53 struct configfs_dirent *sd = file->f_path.dentry->d_fsdata;
54
55 return sd->s_frag;
56 }
57
58 static int fill_read_buffer(struct file *file, struct configfs_buffer *buffer)
59 {
60 struct configfs_fragment *frag = to_frag(file);
61 ssize_t count = -ENOENT;
62
63 if (!buffer->page)
64 buffer->page = (char *) get_zeroed_page(GFP_KERNEL);
65 if (!buffer->page)
66 return -ENOMEM;
67
68 down_read(&frag->frag_sem);
69 if (!frag->frag_dead)
70 count = buffer->attr->show(buffer->item, buffer->page);
71 up_read(&frag->frag_sem);
72
73 if (count < 0)
74 return count;
75 if (WARN_ON_ONCE(count > (ssize_t)SIMPLE_ATTR_SIZE))
76 return -EIO;
77 buffer->needs_read_fill = 0;
78 buffer->count = count;
79 return 0;
80 }
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 static ssize_t
102 configfs_read_file(struct file *file, char __user *buf, size_t count, loff_t *ppos)
103 {
104 struct configfs_buffer *buffer = file->private_data;
105 ssize_t retval = 0;
106
107 mutex_lock(&buffer->mutex);
108 if (buffer->needs_read_fill) {
109 retval = fill_read_buffer(file, buffer);
110 if (retval)
111 goto out;
112 }
113 pr_debug("%s: count = %zd, ppos = %lld, buf = %s\n",
114 __func__, count, *ppos, buffer->page);
115 retval = simple_read_from_buffer(buf, count, ppos, buffer->page,
116 buffer->count);
117 out:
118 mutex_unlock(&buffer->mutex);
119 return retval;
120 }
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142 static ssize_t
143 configfs_read_bin_file(struct file *file, char __user *buf,
144 size_t count, loff_t *ppos)
145 {
146 struct configfs_fragment *frag = to_frag(file);
147 struct configfs_buffer *buffer = file->private_data;
148 ssize_t retval = 0;
149 ssize_t len = min_t(size_t, count, PAGE_SIZE);
150
151 mutex_lock(&buffer->mutex);
152
153
154 if (buffer->write_in_progress) {
155 retval = -ETXTBSY;
156 goto out;
157 }
158 buffer->read_in_progress = true;
159
160 if (buffer->needs_read_fill) {
161
162 down_read(&frag->frag_sem);
163 if (!frag->frag_dead)
164 len = buffer->bin_attr->read(buffer->item, NULL, 0);
165 else
166 len = -ENOENT;
167 up_read(&frag->frag_sem);
168 if (len <= 0) {
169 retval = len;
170 goto out;
171 }
172
173
174 if (buffer->cb_max_size && len > buffer->cb_max_size) {
175 retval = -EFBIG;
176 goto out;
177 }
178
179 buffer->bin_buffer = vmalloc(len);
180 if (buffer->bin_buffer == NULL) {
181 retval = -ENOMEM;
182 goto out;
183 }
184 buffer->bin_buffer_size = len;
185
186
187 down_read(&frag->frag_sem);
188 if (!frag->frag_dead)
189 len = buffer->bin_attr->read(buffer->item,
190 buffer->bin_buffer, len);
191 else
192 len = -ENOENT;
193 up_read(&frag->frag_sem);
194 if (len < 0) {
195 retval = len;
196 vfree(buffer->bin_buffer);
197 buffer->bin_buffer_size = 0;
198 buffer->bin_buffer = NULL;
199 goto out;
200 }
201
202 buffer->needs_read_fill = 0;
203 }
204
205 retval = simple_read_from_buffer(buf, count, ppos, buffer->bin_buffer,
206 buffer->bin_buffer_size);
207 out:
208 mutex_unlock(&buffer->mutex);
209 return retval;
210 }
211
212
213
214
215
216
217
218
219
220
221
222
223 static int
224 fill_write_buffer(struct configfs_buffer * buffer, const char __user * buf, size_t count)
225 {
226 int error;
227
228 if (!buffer->page)
229 buffer->page = (char *)__get_free_pages(GFP_KERNEL, 0);
230 if (!buffer->page)
231 return -ENOMEM;
232
233 if (count >= SIMPLE_ATTR_SIZE)
234 count = SIMPLE_ATTR_SIZE - 1;
235 error = copy_from_user(buffer->page,buf,count);
236 buffer->needs_read_fill = 1;
237
238
239 buffer->page[count] = 0;
240 return error ? -EFAULT : count;
241 }
242
243 static int
244 flush_write_buffer(struct file *file, struct configfs_buffer *buffer, size_t count)
245 {
246 struct configfs_fragment *frag = to_frag(file);
247 int res = -ENOENT;
248
249 down_read(&frag->frag_sem);
250 if (!frag->frag_dead)
251 res = buffer->attr->store(buffer->item, buffer->page, count);
252 up_read(&frag->frag_sem);
253 return res;
254 }
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274 static ssize_t
275 configfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
276 {
277 struct configfs_buffer *buffer = file->private_data;
278 ssize_t len;
279
280 mutex_lock(&buffer->mutex);
281 len = fill_write_buffer(buffer, buf, count);
282 if (len > 0)
283 len = flush_write_buffer(file, buffer, len);
284 if (len > 0)
285 *ppos += len;
286 mutex_unlock(&buffer->mutex);
287 return len;
288 }
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 static ssize_t
304 configfs_write_bin_file(struct file *file, const char __user *buf,
305 size_t count, loff_t *ppos)
306 {
307 struct configfs_buffer *buffer = file->private_data;
308 void *tbuf = NULL;
309 ssize_t len;
310
311 mutex_lock(&buffer->mutex);
312
313
314 if (buffer->read_in_progress) {
315 len = -ETXTBSY;
316 goto out;
317 }
318 buffer->write_in_progress = true;
319
320
321 if (*ppos + count > buffer->bin_buffer_size) {
322
323 if (buffer->cb_max_size &&
324 *ppos + count > buffer->cb_max_size) {
325 len = -EFBIG;
326 goto out;
327 }
328
329 tbuf = vmalloc(*ppos + count);
330 if (tbuf == NULL) {
331 len = -ENOMEM;
332 goto out;
333 }
334
335
336 if (buffer->bin_buffer) {
337 memcpy(tbuf, buffer->bin_buffer,
338 buffer->bin_buffer_size);
339 vfree(buffer->bin_buffer);
340 }
341
342
343 memset(tbuf + buffer->bin_buffer_size, 0,
344 *ppos + count - buffer->bin_buffer_size);
345 buffer->bin_buffer = tbuf;
346 buffer->bin_buffer_size = *ppos + count;
347 }
348
349 len = simple_write_to_buffer(buffer->bin_buffer,
350 buffer->bin_buffer_size, ppos, buf, count);
351 out:
352 mutex_unlock(&buffer->mutex);
353 return len;
354 }
355
356 static int __configfs_open_file(struct inode *inode, struct file *file, int type)
357 {
358 struct dentry *dentry = file->f_path.dentry;
359 struct configfs_fragment *frag = to_frag(file);
360 struct configfs_attribute *attr;
361 struct configfs_buffer *buffer;
362 int error;
363
364 error = -ENOMEM;
365 buffer = kzalloc(sizeof(struct configfs_buffer), GFP_KERNEL);
366 if (!buffer)
367 goto out;
368
369 error = -ENOENT;
370 down_read(&frag->frag_sem);
371 if (unlikely(frag->frag_dead))
372 goto out_free_buffer;
373
374 error = -EINVAL;
375 buffer->item = to_item(dentry->d_parent);
376 if (!buffer->item)
377 goto out_free_buffer;
378
379 attr = to_attr(dentry);
380 if (!attr)
381 goto out_put_item;
382
383 if (type & CONFIGFS_ITEM_BIN_ATTR) {
384 buffer->bin_attr = to_bin_attr(dentry);
385 buffer->cb_max_size = buffer->bin_attr->cb_max_size;
386 } else {
387 buffer->attr = attr;
388 }
389
390 buffer->owner = attr->ca_owner;
391
392 error = -ENODEV;
393 if (!try_module_get(buffer->owner))
394 goto out_put_item;
395
396 error = -EACCES;
397 if (!buffer->item->ci_type)
398 goto out_put_module;
399
400 buffer->ops = buffer->item->ci_type->ct_item_ops;
401
402
403
404
405
406 if (file->f_mode & FMODE_WRITE) {
407 if (!(inode->i_mode & S_IWUGO))
408 goto out_put_module;
409 if ((type & CONFIGFS_ITEM_ATTR) && !attr->store)
410 goto out_put_module;
411 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->write)
412 goto out_put_module;
413 }
414
415
416
417
418
419 if (file->f_mode & FMODE_READ) {
420 if (!(inode->i_mode & S_IRUGO))
421 goto out_put_module;
422 if ((type & CONFIGFS_ITEM_ATTR) && !attr->show)
423 goto out_put_module;
424 if ((type & CONFIGFS_ITEM_BIN_ATTR) && !buffer->bin_attr->read)
425 goto out_put_module;
426 }
427
428 mutex_init(&buffer->mutex);
429 buffer->needs_read_fill = 1;
430 buffer->read_in_progress = false;
431 buffer->write_in_progress = false;
432 file->private_data = buffer;
433 up_read(&frag->frag_sem);
434 return 0;
435
436 out_put_module:
437 module_put(buffer->owner);
438 out_put_item:
439 config_item_put(buffer->item);
440 out_free_buffer:
441 up_read(&frag->frag_sem);
442 kfree(buffer);
443 out:
444 return error;
445 }
446
447 static int configfs_release(struct inode *inode, struct file *filp)
448 {
449 struct configfs_buffer *buffer = filp->private_data;
450
451 module_put(buffer->owner);
452 if (buffer->page)
453 free_page((unsigned long)buffer->page);
454 mutex_destroy(&buffer->mutex);
455 kfree(buffer);
456 return 0;
457 }
458
459 static int configfs_open_file(struct inode *inode, struct file *filp)
460 {
461 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_ATTR);
462 }
463
464 static int configfs_open_bin_file(struct inode *inode, struct file *filp)
465 {
466 return __configfs_open_file(inode, filp, CONFIGFS_ITEM_BIN_ATTR);
467 }
468
469 static int configfs_release_bin_file(struct inode *inode, struct file *file)
470 {
471 struct configfs_buffer *buffer = file->private_data;
472
473 buffer->read_in_progress = false;
474
475 if (buffer->write_in_progress) {
476 struct configfs_fragment *frag = to_frag(file);
477 buffer->write_in_progress = false;
478
479 down_read(&frag->frag_sem);
480 if (!frag->frag_dead) {
481
482 buffer->bin_attr->write(buffer->item,
483 buffer->bin_buffer,
484 buffer->bin_buffer_size);
485 }
486 up_read(&frag->frag_sem);
487
488 vfree(buffer->bin_buffer);
489 buffer->bin_buffer = NULL;
490 buffer->bin_buffer_size = 0;
491 buffer->needs_read_fill = 1;
492 }
493
494 configfs_release(inode, file);
495 return 0;
496 }
497
498
499 const struct file_operations configfs_file_operations = {
500 .read = configfs_read_file,
501 .write = configfs_write_file,
502 .llseek = generic_file_llseek,
503 .open = configfs_open_file,
504 .release = configfs_release,
505 };
506
507 const struct file_operations configfs_bin_file_operations = {
508 .read = configfs_read_bin_file,
509 .write = configfs_write_bin_file,
510 .llseek = NULL,
511 .open = configfs_open_bin_file,
512 .release = configfs_release_bin_file,
513 };
514
515
516
517
518
519
520
521 int configfs_create_file(struct config_item * item, const struct configfs_attribute * attr)
522 {
523 struct dentry *dir = item->ci_dentry;
524 struct configfs_dirent *parent_sd = dir->d_fsdata;
525 umode_t mode = (attr->ca_mode & S_IALLUGO) | S_IFREG;
526 int error = 0;
527
528 inode_lock_nested(d_inode(dir), I_MUTEX_NORMAL);
529 error = configfs_make_dirent(parent_sd, NULL, (void *) attr, mode,
530 CONFIGFS_ITEM_ATTR, parent_sd->s_frag);
531 inode_unlock(d_inode(dir));
532
533 return error;
534 }
535
536
537
538
539
540
541
542 int configfs_create_bin_file(struct config_item *item,
543 const struct configfs_bin_attribute *bin_attr)
544 {
545 struct dentry *dir = item->ci_dentry;
546 struct configfs_dirent *parent_sd = dir->d_fsdata;
547 umode_t mode = (bin_attr->cb_attr.ca_mode & S_IALLUGO) | S_IFREG;
548 int error = 0;
549
550 inode_lock_nested(dir->d_inode, I_MUTEX_NORMAL);
551 error = configfs_make_dirent(parent_sd, NULL, (void *) bin_attr, mode,
552 CONFIGFS_ITEM_BIN_ATTR, parent_sd->s_frag);
553 inode_unlock(dir->d_inode);
554
555 return error;
556 }