Merge branch 'master' of /home/tglx/work/mtd/git/linux-2.6.git/
[sfrench/cifs-2.6.git] / drivers / mtd / mtd_blkdevs.c
1 /*
2  * $Id: mtd_blkdevs.c,v 1.26 2005/07/29 19:42:04 tpoynor Exp $
3  *
4  * (C) 2003 David Woodhouse <dwmw2@infradead.org>
5  *
6  * Interface to Linux 2.5 block layer for MTD 'translation layers'.
7  *
8  */
9
10 #include <linux/kernel.h>
11 #include <linux/slab.h>
12 #include <linux/module.h>
13 #include <linux/list.h>
14 #include <linux/fs.h>
15 #include <linux/mtd/blktrans.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/blkdev.h>
18 #include <linux/blkpg.h>
19 #include <linux/spinlock.h>
20 #include <linux/hdreg.h>
21 #include <linux/init.h>
22 #include <asm/semaphore.h>
23 #include <asm/uaccess.h>
24
25 static LIST_HEAD(blktrans_majors);
26
27 extern struct semaphore mtd_table_mutex;
28 extern struct mtd_info *mtd_table[];
29
30 struct mtd_blkcore_priv {
31         struct completion thread_dead;
32         int exiting;
33         wait_queue_head_t thread_wq;
34         struct request_queue *rq;
35         spinlock_t queue_lock;
36 };
37
38 static int do_blktrans_request(struct mtd_blktrans_ops *tr,
39                                struct mtd_blktrans_dev *dev,
40                                struct request *req)
41 {
42         unsigned long block, nsect;
43         char *buf;
44
45         block = req->sector;
46         nsect = req->current_nr_sectors;
47         buf = req->buffer;
48
49         if (!(req->flags & REQ_CMD))
50                 return 0;
51
52         if (block + nsect > get_capacity(req->rq_disk))
53                 return 0;
54
55         switch(rq_data_dir(req)) {
56         case READ:
57                 for (; nsect > 0; nsect--, block++, buf += 512)
58                         if (tr->readsect(dev, block, buf))
59                                 return 0;
60                 return 1;
61
62         case WRITE:
63                 if (!tr->writesect)
64                         return 0;
65
66                 for (; nsect > 0; nsect--, block++, buf += 512)
67                         if (tr->writesect(dev, block, buf))
68                                 return 0;
69                 return 1;
70
71         default:
72                 printk(KERN_NOTICE "Unknown request %ld\n", rq_data_dir(req));
73                 return 0;
74         }
75 }
76
77 static int mtd_blktrans_thread(void *arg)
78 {
79         struct mtd_blktrans_ops *tr = arg;
80         struct request_queue *rq = tr->blkcore_priv->rq;
81
82         /* we might get involved when memory gets low, so use PF_MEMALLOC */
83         current->flags |= PF_MEMALLOC | PF_NOFREEZE;
84
85         daemonize("%sd", tr->name);
86
87         /* daemonize() doesn't do this for us since some kernel threads
88            actually want to deal with signals. We can't just call 
89            exit_sighand() since that'll cause an oops when we finally
90            do exit. */
91         spin_lock_irq(&current->sighand->siglock);
92         sigfillset(&current->blocked);
93         recalc_sigpending();
94         spin_unlock_irq(&current->sighand->siglock);
95
96         spin_lock_irq(rq->queue_lock);
97                 
98         while (!tr->blkcore_priv->exiting) {
99                 struct request *req;
100                 struct mtd_blktrans_dev *dev;
101                 int res = 0;
102                 DECLARE_WAITQUEUE(wait, current);
103
104                 req = elv_next_request(rq);
105
106                 if (!req) {
107                         add_wait_queue(&tr->blkcore_priv->thread_wq, &wait);
108                         set_current_state(TASK_INTERRUPTIBLE);
109
110                         spin_unlock_irq(rq->queue_lock);
111
112                         schedule();
113                         remove_wait_queue(&tr->blkcore_priv->thread_wq, &wait);
114
115                         spin_lock_irq(rq->queue_lock);
116
117                         continue;
118                 }
119
120                 dev = req->rq_disk->private_data;
121                 tr = dev->tr;
122
123                 spin_unlock_irq(rq->queue_lock);
124
125                 down(&dev->sem);
126                 res = do_blktrans_request(tr, dev, req);
127                 up(&dev->sem);
128
129                 spin_lock_irq(rq->queue_lock);
130
131                 end_request(req, res);
132         }
133         spin_unlock_irq(rq->queue_lock);
134
135         complete_and_exit(&tr->blkcore_priv->thread_dead, 0);
136 }
137
138 static void mtd_blktrans_request(struct request_queue *rq)
139 {
140         struct mtd_blktrans_ops *tr = rq->queuedata;
141         wake_up(&tr->blkcore_priv->thread_wq);
142 }
143
144
145 static int blktrans_open(struct inode *i, struct file *f)
146 {
147         struct mtd_blktrans_dev *dev;
148         struct mtd_blktrans_ops *tr;
149         int ret = -ENODEV;
150
151         dev = i->i_bdev->bd_disk->private_data;
152         tr = dev->tr;
153
154         if (!try_module_get(dev->mtd->owner))
155                 goto out;
156
157         if (!try_module_get(tr->owner))
158                 goto out_tr;
159
160         /* FIXME: Locking. A hot pluggable device can go away 
161            (del_mtd_device can be called for it) without its module
162            being unloaded. */
163         dev->mtd->usecount++;
164
165         ret = 0;
166         if (tr->open && (ret = tr->open(dev))) {
167                 dev->mtd->usecount--;
168                 module_put(dev->mtd->owner);
169         out_tr:
170                 module_put(tr->owner);
171         }
172  out:
173         return ret;
174 }
175
176 static int blktrans_release(struct inode *i, struct file *f)
177 {
178         struct mtd_blktrans_dev *dev;
179         struct mtd_blktrans_ops *tr;
180         int ret = 0;
181
182         dev = i->i_bdev->bd_disk->private_data;
183         tr = dev->tr;
184
185         if (tr->release)
186                 ret = tr->release(dev);
187
188         if (!ret) {
189                 dev->mtd->usecount--;
190                 module_put(dev->mtd->owner);
191                 module_put(tr->owner);
192         }
193
194         return ret;
195 }
196
197
198 static int blktrans_ioctl(struct inode *inode, struct file *file, 
199                               unsigned int cmd, unsigned long arg)
200 {
201         struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data;
202         struct mtd_blktrans_ops *tr = dev->tr;
203
204         switch (cmd) {
205         case BLKFLSBUF:
206                 if (tr->flush)
207                         return tr->flush(dev);
208                 /* The core code did the work, we had nothing to do. */
209                 return 0;
210
211         case HDIO_GETGEO:
212                 if (tr->getgeo) {
213                         struct hd_geometry g;
214                         int ret;
215
216                         memset(&g, 0, sizeof(g));
217                         ret = tr->getgeo(dev, &g);
218                         if (ret)
219                                 return ret;
220
221                         g.start = get_start_sect(inode->i_bdev);
222                         if (copy_to_user((void __user *)arg, &g, sizeof(g)))
223                                 return -EFAULT;
224                         return 0;
225                 } /* else */
226         default:
227                 return -ENOTTY;
228         }
229 }
230
231 struct block_device_operations mtd_blktrans_ops = {
232         .owner          = THIS_MODULE,
233         .open           = blktrans_open,
234         .release        = blktrans_release,
235         .ioctl          = blktrans_ioctl,
236 };
237
238 int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
239 {
240         struct mtd_blktrans_ops *tr = new->tr;
241         struct list_head *this;
242         int last_devnum = -1;
243         struct gendisk *gd;
244
245         if (!down_trylock(&mtd_table_mutex)) {
246                 up(&mtd_table_mutex);
247                 BUG();
248         }
249
250         list_for_each(this, &tr->devs) {
251                 struct mtd_blktrans_dev *d = list_entry(this, struct mtd_blktrans_dev, list);
252                 if (new->devnum == -1) {
253                         /* Use first free number */
254                         if (d->devnum != last_devnum+1) {
255                                 /* Found a free devnum. Plug it in here */
256                                 new->devnum = last_devnum+1;
257                                 list_add_tail(&new->list, &d->list);
258                                 goto added;
259                         }
260                 } else if (d->devnum == new->devnum) {
261                         /* Required number taken */
262                         return -EBUSY;
263                 } else if (d->devnum > new->devnum) {
264                         /* Required number was free */
265                         list_add_tail(&new->list, &d->list);
266                         goto added;
267                 } 
268                 last_devnum = d->devnum;
269         }
270         if (new->devnum == -1)
271                 new->devnum = last_devnum+1;
272
273         if ((new->devnum << tr->part_bits) > 256) {
274                 return -EBUSY;
275         }
276
277         init_MUTEX(&new->sem);
278         list_add_tail(&new->list, &tr->devs);
279  added:
280         if (!tr->writesect)
281                 new->readonly = 1;
282
283         gd = alloc_disk(1 << tr->part_bits);
284         if (!gd) {
285                 list_del(&new->list);
286                 return -ENOMEM;
287         }
288         gd->major = tr->major;
289         gd->first_minor = (new->devnum) << tr->part_bits;
290         gd->fops = &mtd_blktrans_ops;
291         
292         if (tr->part_bits)
293                 if (new->devnum < 26)
294                         snprintf(gd->disk_name, sizeof(gd->disk_name),
295                                  "%s%c", tr->name, 'a' + new->devnum);
296                 else
297                         snprintf(gd->disk_name, sizeof(gd->disk_name),
298                                  "%s%c%c", tr->name,
299                                  'a' - 1 + new->devnum / 26,
300                                  'a' + new->devnum % 26);
301         else
302                 snprintf(gd->disk_name, sizeof(gd->disk_name),
303                          "%s%d", tr->name, new->devnum);
304
305         /* 2.5 has capacity in units of 512 bytes while still
306            having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */
307         set_capacity(gd, (new->size * new->blksize) >> 9);
308
309         gd->private_data = new;
310         new->blkcore_priv = gd;
311         gd->queue = tr->blkcore_priv->rq;
312
313         if (new->readonly)
314                 set_disk_ro(gd, 1);
315
316         add_disk(gd);
317         
318         return 0;
319 }
320
321 int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
322 {
323         if (!down_trylock(&mtd_table_mutex)) {
324                 up(&mtd_table_mutex);
325                 BUG();
326         }
327
328         list_del(&old->list);
329
330         del_gendisk(old->blkcore_priv);
331         put_disk(old->blkcore_priv);
332                 
333         return 0;
334 }
335
336 static void blktrans_notify_remove(struct mtd_info *mtd)
337 {
338         struct list_head *this, *this2, *next;
339
340         list_for_each(this, &blktrans_majors) {
341                 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
342
343                 list_for_each_safe(this2, next, &tr->devs) {
344                         struct mtd_blktrans_dev *dev = list_entry(this2, struct mtd_blktrans_dev, list);
345
346                         if (dev->mtd == mtd)
347                                 tr->remove_dev(dev);
348                 }
349         }
350 }
351
352 static void blktrans_notify_add(struct mtd_info *mtd)
353 {
354         struct list_head *this;
355
356         if (mtd->type == MTD_ABSENT)
357                 return;
358
359         list_for_each(this, &blktrans_majors) {
360                 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
361
362                 tr->add_mtd(tr, mtd);
363         }
364
365 }
366
367 static struct mtd_notifier blktrans_notifier = {
368         .add = blktrans_notify_add,
369         .remove = blktrans_notify_remove,
370 };
371       
372 int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
373 {
374         int ret, i;
375
376         /* Register the notifier if/when the first device type is 
377            registered, to prevent the link/init ordering from fucking
378            us over. */
379         if (!blktrans_notifier.list.next)
380                 register_mtd_user(&blktrans_notifier);
381
382         tr->blkcore_priv = kmalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
383         if (!tr->blkcore_priv)
384                 return -ENOMEM;
385
386         memset(tr->blkcore_priv, 0, sizeof(*tr->blkcore_priv));
387
388         down(&mtd_table_mutex);
389
390         ret = register_blkdev(tr->major, tr->name);
391         if (ret) {
392                 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
393                        tr->name, tr->major, ret);
394                 kfree(tr->blkcore_priv);
395                 up(&mtd_table_mutex);
396                 return ret;
397         }
398         spin_lock_init(&tr->blkcore_priv->queue_lock);
399         init_completion(&tr->blkcore_priv->thread_dead);
400         init_waitqueue_head(&tr->blkcore_priv->thread_wq);
401
402         tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock);
403         if (!tr->blkcore_priv->rq) {
404                 unregister_blkdev(tr->major, tr->name);
405                 kfree(tr->blkcore_priv);
406                 up(&mtd_table_mutex);
407                 return -ENOMEM;
408         }
409
410         tr->blkcore_priv->rq->queuedata = tr;
411
412         ret = kernel_thread(mtd_blktrans_thread, tr, CLONE_KERNEL);
413         if (ret < 0) {
414                 blk_cleanup_queue(tr->blkcore_priv->rq);
415                 unregister_blkdev(tr->major, tr->name);
416                 kfree(tr->blkcore_priv);
417                 up(&mtd_table_mutex);
418                 return ret;
419         } 
420
421         INIT_LIST_HEAD(&tr->devs);
422         list_add(&tr->list, &blktrans_majors);
423
424         for (i=0; i<MAX_MTD_DEVICES; i++) {
425                 if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
426                         tr->add_mtd(tr, mtd_table[i]);
427         }
428
429         up(&mtd_table_mutex);
430
431         return 0;
432 }
433
434 int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
435 {
436         struct list_head *this, *next;
437
438         down(&mtd_table_mutex);
439
440         /* Clean up the kernel thread */
441         tr->blkcore_priv->exiting = 1;
442         wake_up(&tr->blkcore_priv->thread_wq);
443         wait_for_completion(&tr->blkcore_priv->thread_dead);
444
445         /* Remove it from the list of active majors */
446         list_del(&tr->list);
447
448         list_for_each_safe(this, next, &tr->devs) {
449                 struct mtd_blktrans_dev *dev = list_entry(this, struct mtd_blktrans_dev, list);
450                 tr->remove_dev(dev);
451         }
452
453         blk_cleanup_queue(tr->blkcore_priv->rq);
454         unregister_blkdev(tr->major, tr->name);
455
456         up(&mtd_table_mutex);
457
458         kfree(tr->blkcore_priv);
459
460         if (!list_empty(&tr->devs))
461                 BUG();
462         return 0;
463 }
464
465 static void __exit mtd_blktrans_exit(void)
466 {
467         /* No race here -- if someone's currently in register_mtd_blktrans
468            we're screwed anyway. */
469         if (blktrans_notifier.list.next)
470                 unregister_mtd_user(&blktrans_notifier);
471 }
472
473 module_exit(mtd_blktrans_exit);
474
475 EXPORT_SYMBOL_GPL(register_mtd_blktrans);
476 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
477 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
478 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
479
480 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
481 MODULE_LICENSE("GPL");
482 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");