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