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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
|
/*
* Direct UBI block device access
*
* Author: dmitry pervushin <dimka@embeddedalley.com>
*
* Copyright 2008-2009 Freescale Semiconductor, Inc. All Rights Reserved.
* Copyright 2008 Embedded Alley Solutions, Inc. All Rights Reserved.
*
* The code contained herein is licensed under the GNU General Public
* License. You may obtain a copy of the GNU General Public License
* Version 2 or later at the following locations:
*
* http://www.opensource.org/licenses/gpl-license.html
* http://www.gnu.org/copyleft/gpl.html
*
* Based on mtdblock by:
* (C) 2000-2003 Nicolas Pitre <nico@cam.org>
* (C) 1999-2003 David Woodhouse <dwmw2@infradead.org>
*/
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/slab.h>
#include <linux/types.h>
#include <linux/vmalloc.h>
#include <linux/genhd.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/blktrans.h>
#include <linux/mtd/ubi.h>
#include <linux/mutex.h>
#include <linux/workqueue.h>
#include "mtdcore.h"
static LIST_HEAD(ubiblk_devices);
/**
* ubiblk_dev - the structure representing translation layer
*
* @m: interface to mtd_blktrans
* @ubi_num: UBI device number
* @ubi_vol: UBI volume ID
* @usecount: reference count
*
* @cache_mutex: protects access to cache_data
* @cache_data: content of the cached LEB
* @cache_offset: offset of cached data on UBI volume, in bytes
* @cache_size: cache size in bytes, usually equal to LEB size
*/
struct ubiblk_dev {
struct mtd_blktrans_dev m;
int ubi_num;
int ubi_vol;
int usecount;
struct ubi_volume_desc *ubi;
struct mutex cache_mutex;
unsigned char *cache_data;
unsigned long cache_offset;
unsigned int cache_size;
enum {
STATE_EMPTY,
STATE_CLEAN,
STATE_DIRTY
} cache_state;
struct list_head list;
struct work_struct unbind;
};
static int ubiblock_open(struct mtd_blktrans_dev *mbd);
static int ubiblock_release(struct mtd_blktrans_dev *mbd);
static int ubiblock_flush(struct mtd_blktrans_dev *mbd);
static int ubiblock_readsect(struct mtd_blktrans_dev *mbd,
unsigned long block, char *buf);
static int ubiblock_writesect(struct mtd_blktrans_dev *mbd,
unsigned long block, char *buf);
static int ubiblock_getgeo(struct mtd_blktrans_dev *mbd,
struct hd_geometry *geo);
static void *ubiblk_add(int ubi_num, int ubi_vol_id);
static void *ubiblk_add_locked(int ubi_num, int ubi_vol_id);
static int ubiblk_del(struct ubiblk_dev *u);
static int ubiblk_del_locked(struct ubiblk_dev *u);
/*
* These two routines are just to satify mtd_blkdev's requirements
*/
static void ubiblock_add_mtd(struct mtd_blktrans_ops *tr, struct mtd_info *mtd)
{
return;
}
static void ubiblock_remove_dev(struct mtd_blktrans_dev *mbd)
{
return;
}
static struct mtd_blktrans_ops ubiblock_tr = {
.name = "ubiblk",
.major = 0, /* assign dynamically */
.part_bits = 3, /* allow up to 8 parts */
.blksize = 512,
.open = ubiblock_open,
.release = ubiblock_release,
.flush = ubiblock_flush,
.readsect = ubiblock_readsect,
.writesect = ubiblock_writesect,
.getgeo = ubiblock_getgeo,
.add_mtd = ubiblock_add_mtd,
.remove_dev = ubiblock_remove_dev,
.owner = THIS_MODULE,
};
static int ubiblock_getgeo(struct mtd_blktrans_dev *bdev,
struct hd_geometry *geo)
{
return -ENOTTY;
}
/**
* ubiblk_write_cached_data - flush the cache to the UBI volume
*/
static int ubiblk_write_cached_data(struct ubiblk_dev *u)
{
int ret;
if (u->cache_state != STATE_DIRTY)
return 0;
pr_debug("%s: volume %d:%d, writing at %lx of size %x\n",
__func__, u->ubi_num, u->ubi_vol,
u->cache_offset, u->cache_size);
ret = ubi_write(u->ubi, u->cache_offset / u->cache_size,
u->cache_data, 0, u->cache_size);
pr_debug("leb_write status %d\n", ret);
u->cache_state = STATE_EMPTY;
return ret;
}
/**
* ubiblk_do_cached_write - cached write the data to the UBI volume
*
* @u: ubiblk_dev
* @pos: offset on the block device
* @len: buffer length
* @buf: buffer itself
*
* if buffer contains one or more whole sectors (=LEBs), write them to the
* volume. Otherwise, fill the cache that will be flushed later
*/
static int ubiblk_do_cached_write(struct ubiblk_dev *u, unsigned long pos,
int len, const char *buf)
{
unsigned int sect_size = u->cache_size,
size, offset;
unsigned long sect_start;
int ret = 0;
int leb;
pr_debug("%s: volume %d:%d, writing at pos %lx of size %x\n",
__func__, u->ubi_num, u->ubi_vol, pos, len);
while (len > 0) {
leb = pos / sect_size;
sect_start = leb * sect_size;
offset = pos - sect_start;
size = sect_size - offset;
if (size > len)
size = len;
if (size == sect_size) {
/*
* We are covering a whole sector. Thus there is no
* need to bother with the cache while it may still be
* useful for other partial writes.
*/
ret = ubi_leb_change(u->ubi, leb,
buf, size, UBI_UNKNOWN);
if (ret)
goto out;
} else {
/* Partial sector: need to use the cache */
if (u->cache_state == STATE_DIRTY &&
u->cache_offset != sect_start) {
ret = ubiblk_write_cached_data(u);
if (ret)
goto out;
}
if (u->cache_state == STATE_EMPTY ||
u->cache_offset != sect_start) {
/* fill the cache with the current sector */
u->cache_state = STATE_EMPTY;
ret = ubi_leb_read(u->ubi, leb,
u->cache_data, 0, u->cache_size, 0);
if (ret)
return ret;
ret = ubi_leb_unmap(u->ubi, leb);
if (ret)
return ret;
ret = ubi_leb_unmap(u->ubi, leb);
if (ret)
return ret;
u->cache_offset = sect_start;
u->cache_state = STATE_CLEAN;
}
/* write data to our local cache */
memcpy(u->cache_data + offset, buf, size);
u->cache_state = STATE_DIRTY;
}
buf += size;
pos += size;
len -= size;
}
out:
return ret;
}
/**
* ubiblk_do_cached_read - cached read the data from ubi volume
*
* @u: ubiblk_dev
* @pos: offset on the block device
* @len: buffer length
* @buf: preallocated buffer
*
* Cached LEB will be used if possible; otherwise data will be using
* ubi_leb_read
*/
static int ubiblk_do_cached_read(struct ubiblk_dev *u, unsigned long pos,
int len, char *buf)
{
unsigned int sect_size = u->cache_size;
int err = 0;
unsigned long sect_start;
unsigned offset, size;
int leb;
pr_debug("%s: read at 0x%lx, size 0x%x\n",
__func__, pos, len);
while (len > 0) {
leb = pos/sect_size;
sect_start = leb*sect_size;
offset = pos - sect_start;
size = sect_size - offset;
if (size > len)
size = len;
/*
* Check if the requested data is already cached
* Read the requested amount of data from our internal
* cache if it contains what we want, otherwise we read
* the data directly from flash.
*/
if (u->cache_state != STATE_EMPTY &&
u->cache_offset == sect_start) {
pr_debug("%s: cached, returning back from cache\n",
__func__);
memcpy(buf, u->cache_data + offset, size);
err = 0;
} else {
pr_debug("%s: pos = %ld, reading leb = %d\n",
__func__, pos, leb);
err = ubi_leb_read(u->ubi, leb, buf, offset, size, 0);
if (err)
goto out;
}
buf += size;
pos += size;
len -= size;
}
out:
return err;
}
/**
* ubiblock_writesect - write the sector
*
* Allocate the cache, if necessary and perform actual write using
* ubiblk_do_cached_write
*/
static int ubiblock_writesect(struct mtd_blktrans_dev *mbd,
unsigned long block, char *buf)
{
struct ubiblk_dev *u = container_of(mbd, struct ubiblk_dev, m);
if (unlikely(!u->cache_data)) {
u->cache_data = vmalloc(u->cache_size);
if (!u->cache_data)
return -EAGAIN;
}
return ubiblk_do_cached_write(u, block<<9, 512, buf);
}
/**
* ubiblk_readsect - read the sector
*
* Allocate the cache, if necessary, and perform actual read using
* ubiblk_do_cached_read
*/
static int ubiblock_readsect(struct mtd_blktrans_dev *mbd,
unsigned long block, char *buf)
{
struct ubiblk_dev *u = container_of(mbd, struct ubiblk_dev, m);
if (unlikely(!u->cache_data)) {
u->cache_data = vmalloc(u->cache_size);
if (!u->cache_data)
return -EAGAIN;
}
return ubiblk_do_cached_read(u, block<<9, 512, buf);
}
static int ubiblk_flush_locked(struct ubiblk_dev *u)
{
ubiblk_write_cached_data(u);
ubi_sync(u->ubi_num);
return 0;
}
static int ubiblock_flush(struct mtd_blktrans_dev *mbd)
{
struct ubiblk_dev *u = container_of(mbd, struct ubiblk_dev, m);
mutex_lock(&u->cache_mutex);
ubiblk_flush_locked(u);
mutex_unlock(&u->cache_mutex);
return 0;
}
static int ubiblock_open(struct mtd_blktrans_dev *mbd)
{
struct ubiblk_dev *u = container_of(mbd, struct ubiblk_dev, m);
if (u->usecount == 0) {
u->ubi = ubi_open_volume(u->ubi_num, u->ubi_vol,
UBI_READWRITE);
if (IS_ERR(u->ubi))
return PTR_ERR(u->ubi);
}
u->usecount++;
return 0;
}
static int ubiblock_release(struct mtd_blktrans_dev *mbd)
{
struct ubiblk_dev *u = container_of(mbd, struct ubiblk_dev, m);
if (--u->usecount == 0) {
mutex_lock(&u->cache_mutex);
ubiblk_flush_locked(u);
vfree(u->cache_data);
u->cache_data = NULL;
mutex_unlock(&u->cache_mutex);
ubi_close_volume(u->ubi);
u->ubi = NULL;
}
return 0;
}
/*
* sysfs routines. The ubiblk creates two entries under /sys/block/ubiblkX:
* - volume, R/O, which is read like "ubi0:volume_name"
* - unbind, W/O; when user writes something here, the block device is
* removed
*
* unbind schedules a work item to perform real unbind, because sysfs entry
* handler cannot delete itself :)
*/
ssize_t volume_show(struct device *dev,
struct device_attribute *attr, char *buf)
{
struct gendisk *gd = dev_to_disk(dev);
struct mtd_blktrans_dev *m = gd->private_data;
struct ubiblk_dev *u = container_of(m, struct ubiblk_dev, m);
return sprintf(buf, "%d:%d\n", u->ubi_num, u->ubi_vol);
}
static void ubiblk_unbind(struct work_struct *ws)
{
struct ubiblk_dev *u = container_of(ws, struct ubiblk_dev, unbind);
ubiblk_del(u);
}
ssize_t unbind_store(struct device *dev, struct device_attribute *attr,
const char *buf, size_t count)
{
struct gendisk *gd = dev_to_disk(dev);
struct mtd_blktrans_dev *m = gd->private_data;
struct ubiblk_dev *u = container_of(m, struct ubiblk_dev, m);
INIT_WORK(&u->unbind, ubiblk_unbind);
schedule_work(&u->unbind);
return count;
}
DEVICE_ATTR(unbind, 0644, NULL, unbind_store);
DEVICE_ATTR(volume, 0644, volume_show, NULL);
static int ubiblk_sysfs(struct gendisk *hd, int add)
{
int r = 0;
if (add) {
r = device_create_file(disk_to_dev(hd), &dev_attr_unbind);
if (r < 0)
goto out;
r = device_create_file(disk_to_dev(hd), &dev_attr_volume);
if (r < 0)
goto out1;
return 0;
}
device_remove_file(disk_to_dev(hd), &dev_attr_unbind);
out1:
device_remove_file(disk_to_dev(hd), &dev_attr_volume);
out:
return r;
}
/**
* add the FTL by registering it with mtd_blkdevs
*/
static void *ubiblk_add(int ubi_num, int ubi_vol_id)
{
void *p;
mutex_lock(&mtd_table_mutex);
p = ubiblk_add_locked(ubi_num, ubi_vol_id);
mutex_unlock(&mtd_table_mutex);
return p;
}
static void *ubiblk_add_locked(int ubi_num, int ubi_vol_id)
{
struct ubiblk_dev *u = kzalloc(sizeof(*u), GFP_KERNEL);
struct ubi_volume_info uvi;
struct ubi_volume_desc *ubi;
if (!u) {
u = ERR_PTR(-ENOMEM);
goto out;
}
ubi = ubi_open_volume(ubi_num, ubi_vol_id, UBI_READONLY);
if (IS_ERR(u->ubi)) {
pr_err("cannot open the volume\n");
u = (void *)u->ubi;
goto out;
}
ubi_get_volume_info(ubi, &uvi);
ubi_close_volume(ubi);
pr_debug("adding volume of size %d (used_size %lld), LEB size %d\n",
uvi.size, uvi.used_bytes, uvi.usable_leb_size);
u->m.mtd = NULL;
u->m.devnum = -1;
u->m.size = uvi.used_bytes >> 9;
u->m.tr = &ubiblock_tr;
u->ubi_num = ubi_num;
u->ubi_vol = ubi_vol_id;
mutex_init(&u->cache_mutex);
u->cache_state = STATE_EMPTY;
u->cache_size = uvi.usable_leb_size;
u->cache_data = NULL;
u->usecount = 0;
INIT_LIST_HEAD(&u->list);
list_add_tail(&u->list, &ubiblk_devices);
add_mtd_blktrans_dev(&u->m);
ubiblk_sysfs(u->m.blkcore_priv, true);
out:
return u;
}
static int ubiblk_del(struct ubiblk_dev *u)
{
int r;
mutex_lock(&mtd_table_mutex);
r = ubiblk_del_locked(u);
mutex_unlock(&mtd_table_mutex);
return r;
}
static int ubiblk_del_locked(struct ubiblk_dev *u)
{
if (u->usecount != 0)
return -EBUSY;
ubiblk_sysfs(u->m.blkcore_priv, false);
del_mtd_blktrans_dev(&u->m);
list_del(&u->list);
BUG_ON(u->cache_data != NULL); /* who did not free the cache ?! */
kfree(u);
return 0;
}
static struct ubiblk_dev *ubiblk_find(int num, int vol)
{
struct ubiblk_dev *pos;
list_for_each_entry(pos, &ubiblk_devices, list)
if (pos->ubi_num == num && pos->ubi_vol == vol)
return pos;
return NULL;
}
static int ubiblock_notification(struct notifier_block *blk,
unsigned long type, void *v)
{
struct ubi_notification *nt = v;
struct ubiblk_dev *u;
switch (type) {
case UBI_VOLUME_ADDED:
ubiblk_add(nt->vi.ubi_num, nt->vi.vol_id);
break;
case UBI_VOLUME_REMOVED:
u = ubiblk_find(nt->vi.ubi_num, nt->vi.vol_id);
if (u)
ubiblk_del(u);
break;
case UBI_VOLUME_RENAMED:
case UBI_VOLUME_RESIZED:
break;
}
return NOTIFY_OK;
}
static struct notifier_block ubiblock_nb = {
.notifier_call = ubiblock_notification,
};
static int __init ubiblock_init(void)
{
int r;
r = register_mtd_blktrans(&ubiblock_tr);
if (r)
goto out;
r = ubi_register_volume_notifier(&ubiblock_nb, 0);
if (r)
goto out_unreg;
return 0;
out_unreg:
deregister_mtd_blktrans(&ubiblock_tr);
out:
return 0;
}
static void __exit ubiblock_exit(void)
{
ubi_unregister_volume_notifier(&ubiblock_nb);
deregister_mtd_blktrans(&ubiblock_tr);
}
module_init(ubiblock_init);
module_exit(ubiblock_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Caching block device emulation access to UBI devices");
MODULE_AUTHOR("dmitry pervushin <dimka@embeddedalley.com>");
|