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