0cc4998ba441d4ae24df93021842fca48863b190
[linux-flexiantxendom0-3.2.10.git] / drivers / mtd / mtd_blkdevs.c
1 /*
2  * $Id: mtd_blkdevs.c,v 1.12 2003/05/21 01:00:59 dwmw2 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/blk.h>
19 #include <linux/blkpg.h>
20 #include <linux/spinlock.h>
21 #include <linux/init.h>
22 #include <asm/semaphore.h>
23 #include <linux/devfs_fs_kernel.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;
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         while (!tr->blkcore_priv->exiting) {
97                 struct request *req;
98                 struct mtd_blktrans_dev *dev;
99                 int res = 0;
100                 DECLARE_WAITQUEUE(wait, current);
101
102                 spin_lock_irq(rq->queue_lock);
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                         continue;
116                 }
117
118                 dev = req->rq_disk->private_data;
119                 tr = dev->tr;
120
121                 spin_unlock_irq(rq->queue_lock);
122
123                 down(&dev->sem);
124                 res = do_blktrans_request(tr, dev, req);
125                 up(&dev->sem);
126
127                 spin_lock_irq(rq->queue_lock);
128
129                 end_request(req, res);
130         }
131         complete_and_exit(&tr->blkcore_priv->thread_dead, 0);
132 }
133
134 static void mtd_blktrans_request(struct request_queue *rq)
135 {
136         struct mtd_blktrans_ops *tr = rq->queuedata;
137         wake_up(&tr->blkcore_priv->thread_wq);
138 }
139
140
141 int blktrans_open(struct inode *i, struct file *f)
142 {
143         struct mtd_blktrans_dev *dev;
144         struct mtd_blktrans_ops *tr;
145         int ret = -ENODEV;
146
147         dev = i->i_bdev->bd_disk->private_data;
148         tr = dev->tr;
149
150         if (!try_module_get(dev->mtd->owner))
151                 goto out;
152
153         if (!try_module_get(tr->owner))
154                 goto out_tr;
155
156         /* FIXME: Locking. A hot pluggable device can go away 
157            (del_mtd_device can be called for it) without its module
158            being unloaded. */
159         dev->mtd->usecount++;
160
161         ret = 0;
162         if (tr->open && (ret = tr->open(dev, i, f))) {
163                 dev->mtd->usecount--;
164                 module_put(dev->mtd->owner);
165         out_tr:
166                 module_put(tr->owner);
167         }
168  out:
169         return ret;
170 }
171
172 int blktrans_release(struct inode *i, struct file *f)
173 {
174         struct mtd_blktrans_dev *dev;
175         struct mtd_blktrans_ops *tr;
176         int ret = 0;
177
178         dev = i->i_bdev->bd_disk->private_data;
179         tr = dev->tr;
180
181         if (tr->release)
182                 ret = tr->release(dev, i, f);
183
184         if (!ret) {
185                 dev->mtd->usecount--;
186                 module_put(dev->mtd->owner);
187                 module_put(tr->owner);
188         }
189
190         return ret;
191 }
192
193
194 static int blktrans_ioctl(struct inode *inode, struct file *file, 
195                               unsigned int cmd, unsigned long arg)
196 {
197         struct mtd_blktrans_dev *dev;
198         struct mtd_blktrans_ops *tr;
199         int ret = -ENOTTY;
200
201         dev = inode->i_bdev->bd_disk->private_data;
202         tr = dev->tr;
203
204         if (tr->ioctl)
205                 ret = tr->ioctl(dev, inode, file, cmd, arg);
206
207         if (ret == -ENOTTY && (cmd == BLKROSET || cmd == BLKFLSBUF)) {
208                 /* The core code did the work, we had nothing to do. */
209                 ret = 0;
210         }
211         return ret;
212 }
213
214 struct block_device_operations mtd_blktrans_ops = {
215         .owner          = THIS_MODULE,
216         .open           = blktrans_open,
217         .release        = blktrans_release,
218         .ioctl          = blktrans_ioctl,
219 };
220
221 int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new)
222 {
223         struct mtd_blktrans_ops *tr = new->tr;
224         struct list_head *this;
225         int last_devnum = -1;
226         struct gendisk *gd;
227
228         if (!down_trylock(&mtd_table_mutex)) {
229                 up(&mtd_table_mutex);
230                 BUG();
231         }
232
233         list_for_each(this, &tr->devs) {
234                 struct mtd_blktrans_dev *d = list_entry(this, struct mtd_blktrans_dev, list);
235                 if (new->devnum == -1) {
236                         /* Use first free number */
237                         if (d->devnum != last_devnum+1) {
238                                 /* Found a free devnum. Plug it in here */
239                                 new->devnum = last_devnum+1;
240                                 list_add_tail(&new->list, &d->list);
241                                 goto added;
242                         }
243                 } else if (d->devnum == new->devnum) {
244                         /* Required number taken */
245                         return -EBUSY;
246                 } else if (d->devnum > new->devnum) {
247                         /* Required number was free */
248                         list_add_tail(&new->list, &d->list);
249                         goto added;
250                 } 
251                 last_devnum = d->devnum;
252         }
253         if (new->devnum == -1)
254                 new->devnum = last_devnum+1;
255
256         if ((new->devnum << tr->part_bits) > 256) {
257                 return -EBUSY;
258         }
259
260         init_MUTEX(&new->sem);
261         list_add_tail(&new->list, &tr->devs);
262  added:
263         if (!tr->writesect)
264                 new->readonly = 1;
265
266         gd = alloc_disk(1 << tr->part_bits);
267         if (!gd) {
268                 list_del(&new->list);
269                 return -ENOMEM;
270         }
271         gd->major = tr->major;
272         gd->first_minor = (new->devnum) << tr->part_bits;
273         gd->fops = &mtd_blktrans_ops;
274         
275         snprintf(gd->disk_name, sizeof(gd->disk_name),
276                  "%s%c", tr->name, (tr->part_bits?'a':'0') + new->devnum);
277         snprintf(gd->devfs_name, sizeof(gd->devfs_name),
278                  "%s/%c", tr->name, (tr->part_bits?'a':'0') + new->devnum);
279
280         set_capacity(gd, new->size);
281         gd->private_data = new;
282         new->blkcore_priv = gd;
283         gd->queue = &tr->blkcore_priv->rq;
284
285         if (new->readonly)
286                 set_disk_ro(gd, 1);
287
288         add_disk(gd);
289         
290         return 0;
291 }
292
293 int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old)
294 {
295         if (!down_trylock(&mtd_table_mutex)) {
296                 up(&mtd_table_mutex);
297                 BUG();
298         }
299
300         list_del(&old->list);
301
302         del_gendisk(old->blkcore_priv);
303         put_disk(old->blkcore_priv);
304                 
305         return 0;
306 }
307
308 void blktrans_notify_remove(struct mtd_info *mtd)
309 {
310         struct list_head *this, *this2, *next;
311
312         list_for_each(this, &blktrans_majors) {
313                 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
314
315                 list_for_each_safe(this2, next, &tr->devs) {
316                         struct mtd_blktrans_dev *dev = list_entry(this2, struct mtd_blktrans_dev, list);
317
318                         if (dev->mtd == mtd)
319                                 tr->remove_dev(dev);
320                 }
321         }
322 }
323
324 void blktrans_notify_add(struct mtd_info *mtd)
325 {
326         struct list_head *this;
327
328         if (mtd->type == MTD_ABSENT)
329                 return;
330
331         printk("%s:%s %d: count %d\n", __FILE__, __func__, __LINE__, atomic_read(&mtd_table_mutex.count));
332
333         list_for_each(this, &blktrans_majors) {
334                 struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list);
335
336                 tr->add_mtd(tr, mtd);
337         }
338
339 }
340
341 static struct mtd_notifier blktrans_notifier = {
342         .add = blktrans_notify_add,
343         .remove = blktrans_notify_remove,
344 };
345       
346 int register_mtd_blktrans(struct mtd_blktrans_ops *tr)
347 {
348         int ret, i;
349
350         /* Register the notifier if/when the first device type is 
351            registered, to prevent the link/init ordering from fucking
352            us over. */
353         if (!blktrans_notifier.list.next)
354                 register_mtd_user(&blktrans_notifier);
355
356         tr->blkcore_priv = kmalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL);
357         if (!tr->blkcore_priv)
358                 return -ENOMEM;
359
360         memset(tr->blkcore_priv, 0, sizeof(*tr->blkcore_priv));
361
362         down(&mtd_table_mutex);
363
364         ret = register_blkdev(tr->major, tr->name);
365         if (ret) {
366                 printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n",
367                        tr->name, tr->major, ret);
368                 kfree(tr->blkcore_priv);
369                 up(&mtd_table_mutex);
370                 return ret;
371         }
372         spin_lock_init(&tr->blkcore_priv->queue_lock);
373         init_completion(&tr->blkcore_priv->thread_dead);
374         init_waitqueue_head(&tr->blkcore_priv->thread_wq);
375
376         blk_init_queue(&tr->blkcore_priv->rq, mtd_blktrans_request, 
377                        &tr->blkcore_priv->queue_lock);
378         tr->blkcore_priv->rq.queuedata = tr;
379
380         ret = kernel_thread(mtd_blktrans_thread, tr, 
381                             CLONE_FS|CLONE_FILES|CLONE_SIGHAND);
382         if (ret < 0) {
383                 blk_cleanup_queue(&tr->blkcore_priv->rq);
384                 unregister_blkdev(tr->major, tr->name);
385                 kfree(tr->blkcore_priv);
386                 up(&mtd_table_mutex);
387                 return ret;
388         } 
389
390         devfs_mk_dir(tr->name);
391
392         INIT_LIST_HEAD(&tr->devs);
393         list_add(&tr->list, &blktrans_majors);
394
395         printk("%s:%s %d: count %d\n", __FILE__, __func__, __LINE__, atomic_read(&mtd_table_mutex.count));
396
397         for (i=0; i<MAX_MTD_DEVICES; i++) {
398                 if (mtd_table[i] && mtd_table[i]->type != MTD_ABSENT)
399                         tr->add_mtd(tr, mtd_table[i]);
400         }
401
402         up(&mtd_table_mutex);
403
404         return 0;
405 }
406
407 int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr)
408 {
409         struct list_head *this, *next;
410
411         down(&mtd_table_mutex);
412
413         /* Clean up the kernel thread */
414         tr->blkcore_priv->exiting = 1;
415         wake_up(&tr->blkcore_priv->thread_wq);
416         wait_for_completion(&tr->blkcore_priv->thread_dead);
417
418         /* Remove it from the list of active majors */
419         list_del(&tr->list);
420
421         list_for_each_safe(this, next, &tr->devs) {
422                 struct mtd_blktrans_dev *dev = list_entry(this, struct mtd_blktrans_dev, list);
423                 tr->remove_dev(dev);
424         }
425
426         devfs_remove(tr->name);
427         blk_cleanup_queue(&tr->blkcore_priv->rq);
428         unregister_blkdev(tr->major, tr->name);
429
430         up(&mtd_table_mutex);
431
432         kfree(tr->blkcore_priv);
433
434         if (!list_empty(&tr->devs))
435                 BUG();
436         return 0;
437 }
438
439 static void __exit mtd_blktrans_exit(void)
440 {
441         /* No race here -- if someone's currently in register_mtd_blktrans
442            we're screwed anyway. */
443         if (blktrans_notifier.list.next)
444                 unregister_mtd_user(&blktrans_notifier);
445 }
446
447 module_exit(mtd_blktrans_exit);
448
449 EXPORT_SYMBOL_GPL(register_mtd_blktrans);
450 EXPORT_SYMBOL_GPL(deregister_mtd_blktrans);
451 EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev);
452 EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev);
453
454 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
455 MODULE_LICENSE("GPL");
456 MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'");