Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux...
[pandora-kernel.git] / drivers / mtd / mtd_blkdevs.c
1 /*
2  * Interface to Linux block layer for MTD 'translation layers'.
3  *
4  * Copyright © 2003-2010 David Woodhouse <dwmw2@infradead.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <linux/kernel.h>
23 #include <linux/slab.h>
24 #include <linux/module.h>
25 #include <linux/list.h>
26 #include <linux/fs.h>
27 #include <linux/mtd/blktrans.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/blkdev.h>
30 #include <linux/blkpg.h>
31 #include <linux/spinlock.h>
32 #include <linux/hdreg.h>
33 #include <linux/init.h>
34 #include <linux/mutex.h>
35 #include <linux/kthread.h>
36 #include <asm/uaccess.h>
37
38 #include "mtdcore.h"
39
40 static DEFINE_MUTEX(mtd_blkdevs_mutex);
41 static LIST_HEAD(blktrans_majors);
42 static DEFINE_MUTEX(blktrans_ref_mutex);
43
44 void blktrans_dev_release(struct kref *kref)
45 {
46         struct mtd_blktrans_dev *dev =
47                 container_of(kref, struct mtd_blktrans_dev, ref);
48
49         dev->disk->private_data = NULL;
50         blk_cleanup_queue(dev->rq);
51         put_disk(dev->disk);
52         list_del(&dev->list);
53         kfree(dev);
54 }
55
56 static struct mtd_blktrans_dev *blktrans_dev_get(struct gendisk *disk)
57 {
58         struct mtd_blktrans_dev *dev;
59
60         mutex_lock(&blktrans_ref_mutex);
61         dev = disk->private_data;
62
63         if (!dev)
64                 goto unlock;
65         kref_get(&dev->ref);
66 unlock:
67         mutex_unlock(&blktrans_ref_mutex);
68         return dev;
69 }
70
71 void blktrans_dev_put(struct mtd_blktrans_dev *dev)
72 {
73         mutex_lock(&blktrans_ref_mutex);
74         kref_put(&dev->ref, blktrans_dev_release);
75         mutex_unlock(&blktrans_ref_mutex);
76 }
77
78
79 static int do_blktrans_request(struct mtd_blktrans_ops *tr,
80                                struct mtd_blktrans_dev *dev,
81                                struct request *req)
82 {
83         unsigned long block, nsect;
84         char *buf;
85
86         block = blk_rq_pos(req) << 9 >> tr->blkshift;
87         nsect = blk_rq_cur_bytes(req) >> tr->blkshift;
88
89         buf = req->buffer;
90
91         if (req->cmd_type != REQ_TYPE_FS)
92                 return -EIO;
93
94         if (blk_rq_pos(req) + blk_rq_cur_sectors(req) >
95             get_capacity(req->rq_disk))
96                 return -EIO;
97
98         if (req->cmd_flags & REQ_DISCARD)
99                 return tr->discard(dev, block, nsect);
100
101         switch(rq_data_dir(req)) {
102         case READ:
103                 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
104                         if (tr->readsect(dev, block, buf))
105                                 return -EIO;
106                 rq_flush_dcache_pages(req);
107                 return 0;
108         case WRITE:
109                 if (!tr->writesect)
110                         return -EIO;
111
112                 rq_flush_dcache_pages(req);
113                 for (; nsect > 0; nsect--, block++, buf += tr->blksize)
114                         if (tr->writesect(dev, block, buf))
115                                 return -EIO;
116                 return 0;
117         default:
118                 printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req));
119                 return -EIO;
120         }
121 }
122
123 static int mtd_blktrans_thread(void *arg)
124 {
125         struct mtd_blktrans_dev *dev = arg;
126         struct request_queue *rq = dev->rq;
127         struct request *req = NULL;
128
129         spin_lock_irq(rq->queue_lock);
130
131         while (!kthread_should_stop()) {
132                 int res;
133
134                 if (!req && !(req = blk_fetch_request(rq))) {
135                         set_current_state(TASK_INTERRUPTIBLE);
136
137                         if (kthread_should_stop())
138                                 set_current_state(TASK_RUNNING);
139
140                         spin_unlock_irq(rq->queue_lock);
141                         schedule();
142                         spin_lock_irq(rq->queue_lock);
143                         continue;
144                 }
145
146                 spin_unlock_irq(rq->queue_lock);
147
148                 mutex_lock(&dev->lock);
149                 res = do_blktrans_request(dev->tr, dev, req);
150                 mutex_unlock(&dev->lock);
151
152                 spin_lock_irq(rq->queue_lock);
153
154                 if (!__blk_end_request_cur(req, res))
155                         req = NULL;
156         }
157
158         if (req)
159                 __blk_end_request_all(req, -EIO);
160
161         spin_unlock_irq(rq->queue_lock);
162
163         return 0;
164 }
165
166 static void mtd_blktrans_request(struct request_queue *rq)
167 {
168         struct mtd_blktrans_dev *dev;
169         struct request *req = NULL;
170
171         dev = rq->queuedata;
172
173         if (!dev)
174                 while ((req = blk_fetch_request(rq)) != NULL)
175                         __blk_end_request_all(req, -ENODEV);
176         else
177                 wake_up_process(dev->thread);
178 }
179
180 static int blktrans_open(struct block_device *bdev, fmode_t mode)
181 {
182         struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
183         int ret = 0;
184
185         if (!dev)
186                 return -ERESTARTSYS; /* FIXME: busy loop! -arnd*/
187
188         mutex_lock(&mtd_blkdevs_mutex);
189         mutex_lock(&dev->lock);
190
191         if (dev->open++)
192                 goto unlock;
193
194         kref_get(&dev->ref);
195         __module_get(dev->tr->owner);
196
197         if (dev->mtd) {
198                 ret = dev->tr->open ? dev->tr->open(dev) : 0;
199                 __get_mtd_device(dev->mtd);
200         }
201
202 unlock:
203         mutex_unlock(&dev->lock);
204         blktrans_dev_put(dev);
205         mutex_unlock(&mtd_blkdevs_mutex);
206         return ret;
207 }
208
209 static int blktrans_release(struct gendisk *disk, fmode_t mode)
210 {
211         struct mtd_blktrans_dev *dev = blktrans_dev_get(disk);
212         int ret = 0;
213
214         if (!dev)
215                 return ret;
216
217         mutex_lock(&mtd_blkdevs_mutex);
218         mutex_lock(&dev->lock);
219
220         if (--dev->open)
221                 goto unlock;
222
223         kref_put(&dev->ref, blktrans_dev_release);
224         module_put(dev->tr->owner);
225
226         if (dev->mtd) {
227                 ret = dev->tr->release ? dev->tr->release(dev) : 0;
228                 __put_mtd_device(dev->mtd);
229         }
230 unlock:
231         mutex_unlock(&dev->lock);
232         blktrans_dev_put(dev);
233         mutex_unlock(&mtd_blkdevs_mutex);
234         return ret;
235 }
236
237 static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo)
238 {
239         struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
240         int ret = -ENXIO;
241
242         if (!dev)
243                 return ret;
244
245         mutex_lock(&dev->lock);
246
247         if (!dev->mtd)
248                 goto unlock;
249
250         ret = dev->tr->getgeo ? dev->tr->getgeo(dev, geo) : 0;
251 unlock:
252         mutex_unlock(&dev->lock);
253         blktrans_dev_put(dev);
254         return ret;
255 }
256
257 static int blktrans_ioctl(struct block_device *bdev, fmode_t mode,
258                               unsigned int cmd, unsigned long arg)
259 {
260         struct mtd_blktrans_dev *dev = blktrans_dev_get(bdev->bd_disk);
261         int ret = -ENXIO;
262
263         if (!dev)
264                 return ret;
265
266         mutex_lock(&mtd_blkdevs_mutex);
267         mutex_lock(&dev->lock);
268
269         if (!dev->mtd)
270                 goto unlock;
271
272         switch (cmd) {
273         case BLKFLSBUF:
274                 ret = dev->tr->flush ? dev->tr->flush(dev) : 0;
275                 break;
276         default:
277                 ret = -ENOTTY;
278         }
279 unlock:
280         mutex_unlock(&dev->lock);
281         mutex_unlock(&mtd_blkdevs_mutex);
282         blktrans_dev_put(dev);
283         return ret;
284 }
285
286 static const struct block_device_operations mtd_blktrans_ops = {
287         .owner          = THIS_MODULE,
288         .open           = blktrans_open,
289         .release        = blktrans_release,
290         .ioctl          = blktrans_ioctl,
291         .getgeo         = blktrans_getgeo,
292 };
293
294 int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
295 {
296         struct mtd_blktrans_ops *tr = new->tr;
297         struct mtd_blktrans_dev *d;
298         int last_devnum = -1;
299         struct gendisk *gd;
300         int ret;
301
302         if (mutex_trylock(&mtd_table_mutex)) {
303                 mutex_unlock(&mtd_table_mutex);
304                 BUG();
305         }
306
307         mutex_lock(&blktrans_ref_mutex);
308         list_for_each_entry(d, &tr->devs, list) {
309                 if (new->devnum == -1) {
310                         /* Use first free number */
311                         if (d->devnum != last_devnum+1) {
312                                 /* Found a free devnum. Plug it in here */
313                                 new->devnum = last_devnum+1;
314                                 list_add_tail(&new->list, &d->list);
315                                 goto added;
316                         }
317                 } else if (d->devnum == new->devnum) {
318                         /* Required number taken */
319                         mutex_unlock(&blktrans_ref_mutex);
320                         return -EBUSY;
321                 } else if (d->devnum > new->devnum) {
322                         /* Required number was free */
323                         list_add_tail(&new->list, &d->list);
324                         goto added;
325                 }
326                 last_devnum = d->devnum;
327         }
328
329         ret = -EBUSY;
330         if (new->devnum == -1)
331                 new->devnum = last_devnum+1;
332
333         /* Check that the device and any partitions will get valid
334          * minor numbers and that the disk naming code below can cope
335          * with this number. */
336         if (new->devnum > (MINORMASK >> tr->part_bits) ||
337             (tr->part_bits && new->devnum >= 27 * 26)) {
338                 mutex_unlock(&blktrans_ref_mutex);
339                 goto error1;
340         }
341
342         list_add_tail(&new->list, &tr->devs);
343  added:
344         mutex_unlock(&blktrans_ref_mutex);
345
346         mutex_init(&new->lock);
347         kref_init(&new->ref);
348         if (!tr->writesect)
349                 new->readonly = 1;
350
351         /* Create gendisk */
352         ret = -ENOMEM;
353         gd = alloc_disk(1 << tr->part_bits);
354
355         if (!gd)
356                 goto error2;
357
358         new->disk = gd;
359         gd->private_data = new;
360         gd->major = tr->major;
361         gd->first_minor = (new->devnum) << tr->part_bits;
362         gd->fops = &mtd_blktrans_ops;
363
364         if (tr->part_bits)
365                 if (new->devnum < 26)
366                         snprintf(gd->disk_name, sizeof(gd->disk_name),
367                                  "%s%c", tr->name, 'a' + new->devnum);
368                 else
369                         snprintf(gd->disk_name, sizeof(gd->disk_name),
370                                  "%s%c%c", tr->name,
371                                  'a' - 1 + new->devnum / 26,
372                                  'a' + new->devnum % 26);
373         else
374                 snprintf(gd->disk_name, sizeof(gd->disk_name),
375                          "%s%d", tr->name, new->devnum);
376
377         set_capacity(gd, (new->size * tr->blksize) >> 9);
378
379         /* Create the request queue */
380         spin_lock_init(&new->queue_lock);
381         new->rq = blk_init_queue(mtd_blktrans_request, &new->queue_lock);
382
383         if (!new->rq)
384                 goto error3;
385
386         new->rq->queuedata = new;
387         blk_queue_logical_block_size(new->rq, tr->blksize);
388
389         if (tr->discard)
390                 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
391                                         new->rq);
392
393         gd->queue = new->rq;
394
395         /* Create processing thread */
396         /* TODO: workqueue ? */
397         new->thread = kthread_run(mtd_blktrans_thread, new,
398                         "%s%d", tr->name, new->mtd->index);
399         if (IS_ERR(new->thread)) {
400                 ret = PTR_ERR(new->thread);
401                 goto error4;
402         }
403         gd->driverfs_dev = &new->mtd->dev;
404
405         if (new->readonly)
406                 set_disk_ro(gd, 1);
407
408         add_disk(gd);
409
410         if (new->disk_attributes) {
411                 ret = sysfs_create_group(&disk_to_dev(gd)->kobj,
412                                         new->disk_attributes);
413                 WARN_ON(ret);
414         }
415         return 0;
416 error4:
417         blk_cleanup_queue(new->rq);
418 error3:
419         put_disk(new->disk);
420 error2:
421         list_del(&new->list);
422 error1:
423         kfree(new);
424         return ret;
425 }
426
427 int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
428 {
429         unsigned long flags;
430
431         if (mutex_trylock(&mtd_table_mutex)) {
432                 mutex_unlock(&mtd_table_mutex);
433                 BUG();
434         }
435
436         if (old->disk_attributes)
437                 sysfs_remove_group(&disk_to_dev(old->disk)->kobj,
438                                                 old->disk_attributes);
439
440         /* Stop new requests to arrive */
441         del_gendisk(old->disk);
442
443
444         /* Stop the thread */
445         kthread_stop(old->thread);
446
447         /* Kill current requests */
448         spin_lock_irqsave(&old->queue_lock, flags);
449         old->rq->queuedata = NULL;
450         blk_start_queue(old->rq);
451         spin_unlock_irqrestore(&old->queue_lock, flags);
452
453         /* If the device is currently open, tell trans driver to close it,
454                 then put mtd device, and don't touch it again */
455         mutex_lock(&old->lock);
456         if (old->open) {
457                 if (old->tr->release)
458                         old->tr->release(old);
459                 __put_mtd_device(old->mtd);
460         }
461
462         old->mtd = NULL;
463
464         mutex_unlock(&old->lock);
465         blktrans_dev_put(old);
466         return 0;
467 }
468
469 static void blktrans_notify_remove(struct mtd_info *mtd)
470 {
471         struct mtd_blktrans_ops *tr;
472         struct mtd_blktrans_dev *dev, *next;
473
474         list_for_each_entry(tr, &blktrans_majors, list)
475                 list_for_each_entry_safe(dev, next, &tr->devs, list)
476                         if (dev->mtd == mtd)
477                                 tr->remove_dev(dev);
478 }
479
480 static void blktrans_notify_add(struct mtd_info *mtd)
481 {
482         struct mtd_blktrans_ops *tr;
483
484         if (mtd->type == MTD_ABSENT)
485                 return;
486
487         list_for_each_entry(tr, &blktrans_majors, list)
488                 tr->add_mtd(tr, mtd);
489 }
490
491 static struct mtd_notifier blktrans_notifier = {
492         .add = blktrans_notify_add,
493         .remove = blktrans_notify_remove,
494 };
495
496 int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
497 {
498         struct mtd_info *mtd;
499         int ret;
500
501         /* Register the notifier if/when the first device type is
502            registered, to prevent the link/init ordering from fucking
503            us over. */
504         if (!blktrans_notifier.list.next)
505                 register_mtd_user(&blktrans_notifier);
506
507
508         mutex_lock(&mtd_table_mutex);
509
510         ret = register_blkdev(tr->major, tr->name);
511         if (ret < 0) {
512                 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
513                        tr->name, tr->major, ret);
514                 mutex_unlock(&mtd_table_mutex);
515                 return ret;
516         }
517
518         if (ret)
519                 tr->major = ret;
520
521         tr->blkshift = ffs(tr->blksize) - 1;
522
523         INIT_LIST_HEAD(&tr->devs);
524         list_add(&tr->list, &blktrans_majors);
525
526         mtd_for_each_device(mtd)
527                 if (mtd->type != MTD_ABSENT)
528                         tr->add_mtd(tr, mtd);
529
530         mutex_unlock(&mtd_table_mutex);
531         return 0;
532 }
533
534 int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
535 {
536         struct mtd_blktrans_dev *dev, *next;
537
538         mutex_lock(&mtd_table_mutex);
539
540         /* Remove it from the list of active majors */
541         list_del(&tr->list);
542
543         list_for_each_entry_safe(dev, next, &tr->devs, list)
544                 tr->remove_dev(dev);
545
546         unregister_blkdev(tr->major, tr->name);
547         mutex_unlock(&mtd_table_mutex);
548
549         BUG_ON(!list_empty(&tr->devs));
550         return 0;
551 }
552
553 static void __exit mtd_blktrans_exit(void)
554 {
555         /* No race here -- if someone's currently in register_mtd_blktrans
556            we're screwed anyway. */
557         if (blktrans_notifier.list.next)
558                 unregister_mtd_user(&blktrans_notifier);
559 }
560
561 module_exit(mtd_blktrans_exit);
562
563 EXPORT_SYMBOL_GPL(register_mtd_blktrans);
564 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
565 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
566 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
567
568 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
569 MODULE_LICENSE("GPL");
570 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");