1/*
2 * $Id: blkmtd.c,v 1.1.1.1 2008/10/15 03:26:35 james26_jang Exp $
3 *
4 * blkmtd.c - use a block device as a fake MTD
5 *
6 * Author: Simon Evans <spse@secret.org.uk>
7 *
8 * Copyright (C) 2001 Simon Evans
9 *
10 * Licence: GPL
11 *
12 * How it works:
13 *       The driver uses raw/io to read/write the device and the page
14 *       cache to cache access. Writes update the page cache with the
15 *       new data but make a copy of the new page(s) and then a kernel
16 *       thread writes pages out to the device in the background. This
17 *       ensures that writes are order even if a page is updated twice.
18 *       Also, since pages in the page cache are never marked as dirty,
19 *       we dont have to worry about writepage() being called on some
20 *       random page which may not be in the write order.
21 *
22 *       Erases are handled like writes, so the callback is called after
23 *       the page cache has been updated. Sync()ing will wait until it is
24 *       all done.
25 *
26 *       It can be loaded Read-Only to prevent erases and writes to the
27 *       medium.
28 *
29 * Todo:
30 *       Make the write queue size dynamic so this it is not too big on
31 *       small memory systems and too small on large memory systems.
32 *
33 *       Page cache usage may still be a bit wrong. Check we are doing
34 *       everything properly.
35 *
36 *       Somehow allow writes to dirty the page cache so we dont use too
37 *       much memory making copies of outgoing pages. Need to handle case
38 *       where page x is written to, then page y, then page x again before
39 *       any of them have been committed to disk.
40 *
41 *       Reading should read multiple pages at once rather than using
42 *       readpage() for each one. This is easy and will be fixed asap.
43 */
44
45
46#include <linux/config.h>
47#include <linux/module.h>
48
49#include <linux/fs.h>
50#include <linux/pagemap.h>
51#include <linux/iobuf.h>
52#include <linux/slab.h>
53#include <linux/pagemap.h>
54#include <linux/mtd/compatmac.h>
55#include <linux/mtd/mtd.h>
56
57#ifdef CONFIG_MTD_DEBUG
58#ifdef CONFIG_PROC_FS
59#  include <linux/proc_fs.h>
60#  define BLKMTD_PROC_DEBUG
61   static struct proc_dir_entry *blkmtd_proc;
62#endif
63#endif
64
65
66/* Default erase size in K, always make it a multiple of PAGE_SIZE */
67#define CONFIG_MTD_BLKDEV_ERASESIZE 128
68#define VERSION "1.7"
69extern int *blk_size[];
70extern int *blksize_size[];
71
72/* Info for the block device */
73typedef struct mtd_raw_dev_data_s {
74  struct block_device *binding;
75  int sector_size, sector_bits;
76  int partial_last_page;   // 0 if device ends on page boundary, else page no of last page
77  int last_page_sectors;   // Number of sectors in last page if partial_last_page != 0
78  size_t totalsize;
79  int readonly;
80  struct address_space as;
81  struct mtd_info mtd_info;
82} mtd_raw_dev_data_t;
83
84/* Info for each queue item in the write queue */
85typedef struct mtdblkdev_write_queue_s {
86  mtd_raw_dev_data_t *rawdevice;
87  struct page **pages;
88  int pagenr;
89  int pagecnt;
90  int iserase;
91} mtdblkdev_write_queue_t;
92
93
94/* Our erase page - always remains locked. */
95static struct page *erase_page;
96
97/* Static info about the MTD, used in cleanup_module */
98static mtd_raw_dev_data_t *mtd_rawdevice;
99
100/* Write queue fixed size */
101#define WRITE_QUEUE_SZ 512
102
103/* Storage for the write queue */
104static mtdblkdev_write_queue_t *write_queue;
105static int write_queue_sz = WRITE_QUEUE_SZ;
106static int volatile write_queue_head;
107static int volatile write_queue_tail;
108static int volatile write_queue_cnt;
109static spinlock_t mbd_writeq_lock = SPIN_LOCK_UNLOCKED;
110
111/* Tell the write thread to finish */
112static volatile int write_task_finish;
113
114/* ipc with the write thread */
115#if LINUX_VERSION_CODE > KERNEL_VERSION(2,3,0)
116static DECLARE_MUTEX_LOCKED(thread_sem);
117static DECLARE_WAIT_QUEUE_HEAD(thr_wq);
118static DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq);
119#else
120static struct semaphore thread_sem = MUTEX_LOCKED;
121DECLARE_WAIT_QUEUE_HEAD(thr_wq);
122DECLARE_WAIT_QUEUE_HEAD(mtbd_sync_wq);
123#endif
124
125
126/* Module parameters passed by insmod/modprobe */
127char *device;    /* the block device to use */
128int erasesz;     /* optional default erase size */
129int ro;          /* optional read only flag */
130int bs;          /* optionally force the block size (avoid using) */
131int count;       /* optionally force the block count (avoid using) */
132int wqs;         /* optionally set the write queue size */
133
134
135#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
136MODULE_LICENSE("GPL");
137MODULE_AUTHOR("Simon Evans <spse@secret.org.uk>");
138MODULE_DESCRIPTION("Emulate an MTD using a block device");
139MODULE_PARM(device, "s");
140MODULE_PARM_DESC(device, "block device to use");
141MODULE_PARM(erasesz, "i");
142MODULE_PARM_DESC(erasesz, "optional erase size to use in KB. eg 4=4K.");
143MODULE_PARM(ro, "i");
144MODULE_PARM_DESC(ro, "1=Read only, writes and erases cause errors");
145MODULE_PARM(bs, "i");
146MODULE_PARM_DESC(bs, "force the block size in bytes");
147MODULE_PARM(count, "i");
148MODULE_PARM_DESC(count, "force the block count");
149MODULE_PARM(wqs, "i");
150#endif
151
152
153/* Page cache stuff */
154
155/* writepage() - should never be called - catch it anyway */
156static int blkmtd_writepage(struct page *page)
157{
158  printk("blkmtd: writepage called!!!\n");
159  return -EIO;
160}
161
162
163/* readpage() - reads one page from the block device */
164static int blkmtd_readpage(mtd_raw_dev_data_t *rawdevice, struct page *page)
165{
166  int err;
167  int sectornr, sectors, i;
168  struct kiobuf *iobuf;
169  kdev_t dev;
170  unsigned long *blocks;
171
172  if(!rawdevice) {
173    printk("blkmtd: readpage: PANIC file->private_data == NULL\n");
174    return -EIO;
175  }
176  dev = to_kdev_t(rawdevice->binding->bd_dev);
177
178  DEBUG(2, "blkmtd: readpage called, dev = `%s' page = %p index = %ld\n",
179	bdevname(dev), page, page->index);
180
181  if(Page_Uptodate(page)) {
182    DEBUG(2, "blkmtd: readpage page %ld is already upto date\n", page->index);
183    UnlockPage(page);
184    return 0;
185  }
186
187  ClearPageUptodate(page);
188  ClearPageError(page);
189
190  /* see if page is in the outgoing write queue */
191  spin_lock(&mbd_writeq_lock);
192  if(write_queue_cnt) {
193    int i = write_queue_tail;
194    while(i != write_queue_head) {
195      mtdblkdev_write_queue_t *item = &write_queue[i];
196      if(page->index >= item->pagenr && page->index < item->pagenr+item->pagecnt) {
197	/* yes it is */
198	int index = page->index - item->pagenr;
199
200	DEBUG(2, "blkmtd: readpage: found page %ld in outgoing write queue\n",
201	      page->index);
202	if(item->iserase) {
203	  memset(page_address(page), 0xff, PAGE_SIZE);
204	} else {
205	  memcpy(page_address(page), page_address(item->pages[index]), PAGE_SIZE);
206	}
207	SetPageUptodate(page);
208	flush_dcache_page(page);
209	UnlockPage(page);
210	spin_unlock(&mbd_writeq_lock);
211	return 0;
212      }
213      i++;
214      i %= write_queue_sz;
215    }
216  }
217  spin_unlock(&mbd_writeq_lock);
218
219
220  DEBUG(3, "blkmtd: readpage: getting kiovec\n");
221  err = alloc_kiovec(1, &iobuf);
222  if (err) {
223    printk("blkmtd: cant allocate kiobuf\n");
224    SetPageError(page);
225    return err;
226  }
227
228  /* Pre 2.4.4 doesn't have space for the block list in the kiobuf */
229#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
230  blocks = kmalloc(KIO_MAX_SECTORS * sizeof(unsigned long));
231  if(blocks == NULL) {
232    printk("blkmtd: cant allocate iobuf blocks\n");
233    free_kiovec(1, &iobuf);
234    SetPageError(page);
235    return -ENOMEM;
236  }
237#else
238  blocks = iobuf->blocks;
239#endif
240
241  iobuf->offset = 0;
242  iobuf->nr_pages = 1;
243  iobuf->length = PAGE_SIZE;
244  iobuf->locked = 1;
245  iobuf->maplist[0] = page;
246  sectornr = page->index << (PAGE_SHIFT - rawdevice->sector_bits);
247  sectors = 1 << (PAGE_SHIFT - rawdevice->sector_bits);
248  if(rawdevice->partial_last_page && page->index == rawdevice->partial_last_page) {
249    DEBUG(3, "blkmtd: handling partial last page\n");
250    sectors = rawdevice->last_page_sectors;
251  }
252  DEBUG(3, "blkmtd: readpage: sectornr = %d sectors = %d\n", sectornr, sectors);
253  for(i = 0; i < sectors; i++) {
254    blocks[i] = sectornr++;
255  }
256  /* If only a partial page read in, clear the rest of the page */
257  if(rawdevice->partial_last_page && page->index == rawdevice->partial_last_page) {
258    int offset = rawdevice->last_page_sectors << rawdevice->sector_bits;
259    int count = PAGE_SIZE-offset;
260    DEBUG(3, "blkmtd: clear last partial page: offset = %d count = %d\n", offset, count);
261    memset(page_address(page)+offset, 0, count);
262    sectors = rawdevice->last_page_sectors;
263  }
264
265
266  DEBUG(3, "bklmtd: readpage: starting brw_kiovec\n");
267  err = brw_kiovec(READ, 1, &iobuf, dev, blocks, rawdevice->sector_size);
268  DEBUG(3, "blkmtd: readpage: finished, err = %d\n", err);
269  iobuf->locked = 0;
270  free_kiovec(1, &iobuf);
271
272#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
273  kfree(blocks);
274#endif
275
276  if(err != PAGE_SIZE) {
277    printk("blkmtd: readpage: error reading page %ld\n", page->index);
278    memset(page_address(page), 0, PAGE_SIZE);
279    SetPageError(page);
280    err = -EIO;
281  } else {
282    DEBUG(3, "blkmtd: readpage: setting page upto date\n");
283    SetPageUptodate(page);
284    err = 0;
285  }
286  flush_dcache_page(page);
287  UnlockPage(page);
288  DEBUG(2, "blkmtd: readpage: finished, err = %d\n", err);
289  return 0;
290}
291
292
293static struct address_space_operations blkmtd_aops = {
294  writepage:     blkmtd_writepage,
295  readpage:      NULL,
296};
297
298
299/* This is the kernel thread that empties the write queue to disk */
300static int write_queue_task(void *data)
301{
302  int err;
303  struct task_struct *tsk = current;
304  struct kiobuf *iobuf;
305  unsigned long *blocks;
306
307  DECLARE_WAITQUEUE(wait, tsk);
308  DEBUG(1, "blkmtd: writetask: starting (pid = %d)\n", tsk->pid);
309  daemonize();
310  strcpy(tsk->comm, "blkmtdd");
311  tsk->tty = NULL;
312  spin_lock_irq(&tsk->sigmask_lock);
313  sigfillset(&tsk->blocked);
314  recalc_sigpending(tsk);
315  spin_unlock_irq(&tsk->sigmask_lock);
316  exit_sighand(tsk);
317
318  if(alloc_kiovec(1, &iobuf)) {
319    printk("blkmtd: write_queue_task cant allocate kiobuf\n");
320    return 0;
321  }
322
323  /* Pre 2.4.4 doesn't have space for the block list in the kiobuf */
324#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
325  blocks = kmalloc(KIO_MAX_SECTORS * sizeof(unsigned long));
326  if(blocks == NULL) {
327    printk("blkmtd: write_queue_task cant allocate iobuf blocks\n");
328    free_kiovec(1, &iobuf);
329    return 0;
330  }
331#else
332  blocks = iobuf->blocks;
333#endif
334
335  DEBUG(2, "blkmtd: writetask: entering main loop\n");
336  add_wait_queue(&thr_wq, &wait);
337
338  while(1) {
339    spin_lock(&mbd_writeq_lock);
340
341    if(!write_queue_cnt) {
342      /* If nothing in the queue, wake up anyone wanting to know when there
343	 is space in the queue then sleep for 2*HZ */
344      spin_unlock(&mbd_writeq_lock);
345      DEBUG(4, "blkmtd: writetask: queue empty\n");
346      if(waitqueue_active(&mtbd_sync_wq))
347	 wake_up(&mtbd_sync_wq);
348      interruptible_sleep_on_timeout(&thr_wq, 2*HZ);
349      DEBUG(4, "blkmtd: writetask: woken up\n");
350      if(write_task_finish)
351	break;
352    } else {
353      /* we have stuff to write */
354      mtdblkdev_write_queue_t *item = &write_queue[write_queue_tail];
355      struct page **pages = item->pages;
356
357      int i;
358      int sectornr = item->pagenr << (PAGE_SHIFT - item->rawdevice->sector_bits);
359      int sectorcnt = item->pagecnt << (PAGE_SHIFT - item->rawdevice->sector_bits);
360      int max_sectors = KIO_MAX_SECTORS >> (item->rawdevice->sector_bits - 9);
361      kdev_t dev = to_kdev_t(item->rawdevice->binding->bd_dev);
362
363      /* If we are writing to the last page on the device and it doesn't end
364       * on a page boundary, subtract the number of sectors that dont exist.
365       */
366      if(item->rawdevice->partial_last_page &&
367	 (item->pagenr + item->pagecnt -1) == item->rawdevice->partial_last_page) {
368	sectorcnt -= (1 << (PAGE_SHIFT - item->rawdevice->sector_bits));
369	sectorcnt += item->rawdevice->last_page_sectors;
370      }
371
372      DEBUG(3, "blkmtd: writetask: got %d queue items\n", write_queue_cnt);
373      set_current_state(TASK_RUNNING);
374      spin_unlock(&mbd_writeq_lock);
375
376      DEBUG(2, "blkmtd: writetask: writing pagenr = %d pagecnt = %d sectornr = %d sectorcnt = %d\n",
377	    item->pagenr, item->pagecnt, sectornr, sectorcnt);
378
379      iobuf->offset = 0;
380      iobuf->locked = 1;
381
382      /* Loop through all the pages to be written in the queue item, remembering
383	 we can only write KIO_MAX_SECTORS at a time */
384
385      while(sectorcnt) {
386	int cursectors = (sectorcnt < max_sectors) ? sectorcnt : max_sectors;
387	int cpagecnt = (cursectors << item->rawdevice->sector_bits) + PAGE_SIZE-1;
388	cpagecnt >>= PAGE_SHIFT;
389
390	for(i = 0; i < cpagecnt; i++) {
391	  if(item->iserase) {
392	    iobuf->maplist[i] = erase_page;
393	  } else {
394	    iobuf->maplist[i] = *(pages++);
395	  }
396	}
397
398	for(i = 0; i < cursectors; i++) {
399	  blocks[i] = sectornr++;
400	}
401
402	iobuf->nr_pages = cpagecnt;
403	iobuf->length = cursectors << item->rawdevice->sector_bits;
404	DEBUG(3, "blkmtd: write_task: about to kiovec\n");
405	err = brw_kiovec(WRITE, 1, &iobuf, dev, blocks, item->rawdevice->sector_size);
406	DEBUG(3, "bklmtd: write_task: done, err = %d\n", err);
407	if(err != (cursectors << item->rawdevice->sector_bits)) {
408	  /* if an error occured - set this to exit the loop */
409	  sectorcnt = 0;
410	} else {
411	  sectorcnt -= cursectors;
412	}
413      }
414
415      /* free up the pages used in the write and list of pages used in the write
416	 queue item */
417      iobuf->locked = 0;
418      spin_lock(&mbd_writeq_lock);
419      write_queue_cnt--;
420      write_queue_tail++;
421      write_queue_tail %= write_queue_sz;
422      if(!item->iserase) {
423	for(i = 0 ; i < item->pagecnt; i++) {
424	  UnlockPage(item->pages[i]);
425	  __free_pages(item->pages[i], 0);
426	}
427	kfree(item->pages);
428      }
429      item->pages = NULL;
430      spin_unlock(&mbd_writeq_lock);
431      /* Tell others there is some space in the write queue */
432      if(waitqueue_active(&mtbd_sync_wq))
433	wake_up(&mtbd_sync_wq);
434    }
435  }
436  remove_wait_queue(&thr_wq, &wait);
437  DEBUG(1, "blkmtd: writetask: exiting\n");
438  free_kiovec(1, &iobuf);
439
440#if LINUX_VERSION_CODE < KERNEL_VERSION(2,4,4)
441  kfree(blocks);
442#endif
443
444  /* Tell people we have exitd */
445  up(&thread_sem);
446  return 0;
447}
448
449
450/* Add a range of pages into the outgoing write queue, making copies of them */
451static int queue_page_write(mtd_raw_dev_data_t *rawdevice, struct page **pages,
452			    int pagenr, int pagecnt, int iserase)
453{
454  struct page *outpage;
455  struct page **new_pages = NULL;
456  mtdblkdev_write_queue_t *item;
457  int i;
458  DECLARE_WAITQUEUE(wait, current);
459  DEBUG(2, "blkmtd: queue_page_write: adding pagenr = %d pagecnt = %d\n", pagenr, pagecnt);
460
461  if(!pagecnt)
462    return 0;
463
464  if(pages == NULL && !iserase)
465    return -EINVAL;
466
467  /* create a array for the list of pages */
468  if(!iserase) {
469    new_pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
470    if(new_pages == NULL)
471      return -ENOMEM;
472
473    /* make copies of the pages in the page cache */
474    for(i = 0; i < pagecnt; i++) {
475      outpage = alloc_pages(GFP_KERNEL, 0);
476      if(!outpage) {
477	while(i--) {
478	  UnlockPage(new_pages[i]);
479	  __free_pages(new_pages[i], 0);
480	}
481	kfree(new_pages);
482	return -ENOMEM;
483      }
484      lock_page(outpage);
485      memcpy(page_address(outpage), page_address(pages[i]), PAGE_SIZE);
486      new_pages[i] = outpage;
487    }
488  }
489
490  /* wait until there is some space in the write queue */
491 test_lock:
492  spin_lock(&mbd_writeq_lock);
493  if(write_queue_cnt == write_queue_sz) {
494    spin_unlock(&mbd_writeq_lock);
495    DEBUG(3, "blkmtd: queue_page: Queue full\n");
496    current->state = TASK_UNINTERRUPTIBLE;
497    add_wait_queue(&mtbd_sync_wq, &wait);
498    wake_up_interruptible(&thr_wq);
499    schedule();
500    current->state = TASK_RUNNING;
501    remove_wait_queue(&mtbd_sync_wq, &wait);
502    DEBUG(3, "blkmtd: queue_page_write: Queue has %d items in it\n", write_queue_cnt);
503    goto test_lock;
504  }
505
506  DEBUG(3, "blkmtd: queue_page_write: qhead: %d qtail: %d qcnt: %d\n",
507	write_queue_head, write_queue_tail, write_queue_cnt);
508
509  /* fix up the queue item */
510  item = &write_queue[write_queue_head];
511  item->pages = new_pages;
512  item->pagenr = pagenr;
513  item->pagecnt = pagecnt;
514  item->rawdevice = rawdevice;
515  item->iserase = iserase;
516
517  write_queue_head++;
518  write_queue_head %= write_queue_sz;
519  write_queue_cnt++;
520  DEBUG(3, "blkmtd: queue_page_write: qhead: %d qtail: %d qcnt: %d\n",
521	write_queue_head, write_queue_tail, write_queue_cnt);
522  spin_unlock(&mbd_writeq_lock);
523  DEBUG(2, "blkmtd: queue_page_write: finished\n");
524  return 0;
525}
526
527
528/* erase a specified part of the device */
529static int blkmtd_erase(struct mtd_info *mtd, struct erase_info *instr)
530{
531  mtd_raw_dev_data_t *rawdevice = mtd->priv;
532  struct mtd_erase_region_info *einfo = mtd->eraseregions;
533  int numregions = mtd->numeraseregions;
534  size_t from;
535  u_long len;
536  int err = 0;
537
538  /* check readonly */
539  if(rawdevice->readonly) {
540    printk("blkmtd: error: trying to erase readonly device %s\n", device);
541    instr->state = MTD_ERASE_FAILED;
542    goto erase_callback;
543  }
544
545  instr->state = MTD_ERASING;
546  from = instr->addr;
547  len = instr->len;
548
549  /* check erase region has valid start and length */
550  DEBUG(2, "blkmtd: erase: dev = `%s' from = 0x%x len = 0x%lx\n",
551	bdevname(rawdevice->binding->bd_dev), from, len);
552  while(numregions) {
553    DEBUG(3, "blkmtd: checking erase region = 0x%08X size = 0x%X num = 0x%x\n",
554	  einfo->offset, einfo->erasesize, einfo->numblocks);
555    if(from >= einfo->offset && from < einfo->offset + (einfo->erasesize * einfo->numblocks)) {
556      if(len == einfo->erasesize && ( (from - einfo->offset) % einfo->erasesize == 0))
557	break;
558    }
559    numregions--;
560    einfo++;
561  }
562
563  if(!numregions) {
564    /* Not a valid erase block */
565    printk("blkmtd: erase: invalid erase request 0x%lX @ 0x%08X\n", len, from);
566    instr->state = MTD_ERASE_FAILED;
567    err = -EIO;
568  }
569
570  if(instr->state != MTD_ERASE_FAILED) {
571    /* start the erase */
572    int pagenr, pagecnt;
573    struct page *page, **pages;
574    int i = 0;
575
576    /* Handle the last page of the device not being whole */
577    if(len < PAGE_SIZE)
578      len = PAGE_SIZE;
579
580    pagenr = from >> PAGE_SHIFT;
581    pagecnt = len >> PAGE_SHIFT;
582    DEBUG(3, "blkmtd: erase: pagenr = %d pagecnt = %d\n", pagenr, pagecnt);
583
584    pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
585    if(pages == NULL) {
586      err = -ENOMEM;
587      instr->state = MTD_ERASE_FAILED;
588      goto erase_out;
589    }
590
591
592    while(pagecnt) {
593      /* get the page via the page cache */
594      DEBUG(3, "blkmtd: erase: doing grab_cache_page() for page %d\n", pagenr);
595      page = grab_cache_page(&rawdevice->as, pagenr);
596      if(!page) {
597	DEBUG(3, "blkmtd: erase: grab_cache_page() failed for page %d\n", pagenr);
598	kfree(pages);
599	err = -EIO;
600	instr->state = MTD_ERASE_FAILED;
601	goto erase_out;
602      }
603      memset(page_address(page), 0xff, PAGE_SIZE);
604      pages[i] = page;
605      pagecnt--;
606      pagenr++;
607      i++;
608    }
609    DEBUG(3, "blkmtd: erase: queuing page write\n");
610    err = queue_page_write(rawdevice, NULL, from >> PAGE_SHIFT, len >> PAGE_SHIFT, 1);
611    pagecnt = len >> PAGE_SHIFT;
612    if(!err) {
613      while(pagecnt--) {
614	SetPageUptodate(pages[pagecnt]);
615	UnlockPage(pages[pagecnt]);
616	page_cache_release(pages[pagecnt]);
617	flush_dcache_page(pages[pagecnt]);
618      }
619      kfree(pages);
620      instr->state = MTD_ERASE_DONE;
621    } else {
622      while(pagecnt--) {
623	SetPageError(pages[pagecnt]);
624	page_cache_release(pages[pagecnt]);
625      }
626      kfree(pages);
627      instr->state = MTD_ERASE_FAILED;
628    }
629  }
630 erase_out:
631  DEBUG(3, "blkmtd: erase: checking callback\n");
632 erase_callback:
633  if (instr->callback) {
634    (*(instr->callback))(instr);
635  }
636  DEBUG(2, "blkmtd: erase: finished (err = %d)\n", err);
637  return err;
638}
639
640
641/* read a range of the data via the page cache */
642static int blkmtd_read(struct mtd_info *mtd, loff_t from, size_t len,
643	     size_t *retlen, u_char *buf)
644{
645  mtd_raw_dev_data_t *rawdevice = mtd->priv;
646  int err = 0;
647  int offset;
648  int pagenr, pages;
649
650  *retlen = 0;
651
652  DEBUG(2, "blkmtd: read: dev = `%s' from = %ld len = %d buf = %p\n",
653	bdevname(rawdevice->binding->bd_dev), (long int)from, len, buf);
654
655  pagenr = from >> PAGE_SHIFT;
656  offset = from - (pagenr << PAGE_SHIFT);
657
658  pages = (offset+len+PAGE_SIZE-1) >> PAGE_SHIFT;
659  DEBUG(3, "blkmtd: read: pagenr = %d offset = %d, pages = %d\n", pagenr, offset, pages);
660
661  /* just loop through each page, getting it via readpage() - slow but easy */
662  while(pages) {
663    struct page *page;
664    int cpylen;
665    DEBUG(3, "blkmtd: read: looking for page: %d\n", pagenr);
666    page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
667    if(IS_ERR(page)) {
668      return PTR_ERR(page);
669    }
670    wait_on_page(page);
671    if(!Page_Uptodate(page)) {
672      /* error reading page */
673      printk("blkmtd: read: page not uptodate\n");
674      page_cache_release(page);
675      return -EIO;
676    }
677
678    cpylen = (PAGE_SIZE > len) ? len : PAGE_SIZE;
679    if(offset+cpylen > PAGE_SIZE)
680      cpylen = PAGE_SIZE-offset;
681
682    memcpy(buf + *retlen, page_address(page) + offset, cpylen);
683    offset = 0;
684    len -= cpylen;
685    *retlen += cpylen;
686    pagenr++;
687    pages--;
688    page_cache_release(page);
689  }
690
691  DEBUG(2, "blkmtd: end read: retlen = %d, err = %d\n", *retlen, err);
692  return err;
693}
694
695
696/* write a range of the data via the page cache.
697 *
698 * Basic operation. break the write into three parts.
699 *
700 * 1. From a page unaligned start up until the next page boundary
701 * 2. Page sized, page aligned blocks
702 * 3. From end of last aligned block to end of range
703 *
704 * 1,3 are read via the page cache and readpage() since these are partial
705 * pages, 2 we just grab pages from the page cache, not caring if they are
706 * already in memory or not since they will be completly overwritten.
707 *
708 */
709
710static int blkmtd_write(struct mtd_info *mtd, loff_t to, size_t len,
711	      size_t *retlen, const u_char *buf)
712{
713  mtd_raw_dev_data_t *rawdevice = mtd->priv;
714  int err = 0;
715  int offset;
716  int pagenr;
717  size_t len1 = 0, len2 = 0, len3 = 0;
718  struct page **pages;
719  int pagecnt = 0;
720
721  *retlen = 0;
722  DEBUG(2, "blkmtd: write: dev = `%s' to = %ld len = %d buf = %p\n",
723	bdevname(rawdevice->binding->bd_dev), (long int)to, len, buf);
724
725  /* handle readonly and out of range numbers */
726
727  if(rawdevice->readonly) {
728    printk("blkmtd: error: trying to write to a readonly device %s\n", device);
729    return -EROFS;
730  }
731
732  if(to >= rawdevice->totalsize) {
733    return -ENOSPC;
734  }
735
736  if(to + len > rawdevice->totalsize) {
737    len = (rawdevice->totalsize - to);
738  }
739
740
741  pagenr = to >> PAGE_SHIFT;
742  offset = to - (pagenr << PAGE_SHIFT);
743
744  /* see if we have to do a partial write at the start */
745  if(offset) {
746    if((offset + len) > PAGE_SIZE) {
747      len1 = PAGE_SIZE - offset;
748      len -= len1;
749    } else {
750      len1 = len;
751      len = 0;
752    }
753  }
754
755  /* calculate the length of the other two regions */
756  len3 = len & ~PAGE_MASK;
757  len -= len3;
758  len2 = len;
759
760
761  if(len1)
762    pagecnt++;
763  if(len2)
764    pagecnt += len2 >> PAGE_SHIFT;
765  if(len3)
766    pagecnt++;
767
768  DEBUG(3, "blkmtd: write: len1 = %d len2 = %d len3 = %d pagecnt = %d\n", len1, len2, len3, pagecnt);
769
770  /* get space for list of pages */
771  pages = kmalloc(pagecnt * sizeof(struct page *), GFP_KERNEL);
772  if(pages == NULL) {
773    return -ENOMEM;
774  }
775  pagecnt = 0;
776
777  if(len1) {
778    /* do partial start region */
779    struct page *page;
780
781    DEBUG(3, "blkmtd: write: doing partial start, page = %d len = %d offset = %d\n", pagenr, len1, offset);
782    page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
783
784    if(IS_ERR(page)) {
785      kfree(pages);
786      return PTR_ERR(page);
787    }
788    memcpy(page_address(page)+offset, buf, len1);
789    pages[pagecnt++] = page;
790    buf += len1;
791    *retlen = len1;
792    err = 0;
793    pagenr++;
794  }
795
796  /* Now do the main loop to a page aligned, n page sized output */
797  if(len2) {
798    int pagesc = len2 >> PAGE_SHIFT;
799    DEBUG(3, "blkmtd: write: whole pages start = %d, count = %d\n", pagenr, pagesc);
800    while(pagesc) {
801      struct page *page;
802
803      /* see if page is in the page cache */
804      DEBUG(3, "blkmtd: write: grabbing page %d from page cache\n", pagenr);
805      page = grab_cache_page(&rawdevice->as, pagenr);
806      DEBUG(3, "blkmtd: write: got page %d from page cache\n", pagenr);
807      if(!page) {
808	printk("blkmtd: write: cant grab cache page %d\n", pagenr);
809	err = -EIO;
810	goto write_err;
811      }
812      memcpy(page_address(page), buf, PAGE_SIZE);
813      pages[pagecnt++] = page;
814      UnlockPage(page);
815      SetPageUptodate(page);
816      pagenr++;
817      pagesc--;
818      buf += PAGE_SIZE;
819      *retlen += PAGE_SIZE;
820    }
821  }
822
823
824  if(len3) {
825    /* do the third region */
826    struct page *page;
827    DEBUG(3, "blkmtd: write: doing partial end, page = %d len = %d\n", pagenr, len3);
828    page = read_cache_page(&rawdevice->as, pagenr, (filler_t *)blkmtd_readpage, rawdevice);
829    if(IS_ERR(page)) {
830      err = PTR_ERR(page);
831      goto write_err;
832    }
833    memcpy(page_address(page), buf, len3);
834    DEBUG(3, "blkmtd: write: writing out partial end\n");
835    pages[pagecnt++] = page;
836    *retlen += len3;
837    err = 0;
838  }
839  DEBUG(2, "blkmtd: write: end, retlen = %d, err = %d\n", *retlen, err);
840  /* submit it to the write task */
841  err = queue_page_write(rawdevice, pages, to >> PAGE_SHIFT, pagecnt, 0);
842  if(!err) {
843    while(pagecnt--) {
844      SetPageUptodate(pages[pagecnt]);
845      flush_dcache_page(pages[pagecnt]);
846      page_cache_release(pages[pagecnt]);
847    }
848    kfree(pages);
849    return 0;
850  }
851
852 write_err:
853  while(--pagecnt) {
854    SetPageError(pages[pagecnt]);
855    page_cache_release(pages[pagecnt]);
856  }
857  kfree(pages);
858  return err;
859}
860
861
862/* sync the device - wait until the write queue is empty */
863static void blkmtd_sync(struct mtd_info *mtd)
864{
865  DECLARE_WAITQUEUE(wait, current);
866  mtd_raw_dev_data_t *rawdevice = mtd->priv;
867  if(rawdevice->readonly)
868    return;
869
870  DEBUG(2, "blkmtd: sync: called\n");
871
872 stuff_inq:
873  spin_lock(&mbd_writeq_lock);
874  if(write_queue_cnt) {
875    spin_unlock(&mbd_writeq_lock);
876    current->state = TASK_UNINTERRUPTIBLE;
877    add_wait_queue(&mtbd_sync_wq, &wait);
878    DEBUG(3, "blkmtd: sync: waking up task\n");
879    wake_up_interruptible(&thr_wq);
880    schedule();
881    current->state = TASK_RUNNING;
882    remove_wait_queue(&mtbd_sync_wq, &wait);
883    DEBUG(3, "blkmtd: sync: waking up after write task\n");
884    goto stuff_inq;
885  }
886  spin_unlock(&mbd_writeq_lock);
887
888  DEBUG(2, "blkmtd: sync: finished\n");
889}
890
891
892#ifdef BLKMTD_PROC_DEBUG
893/* procfs stuff */
894static int blkmtd_proc_read(char *page, char **start, off_t off, int count, int *eof, void *data)
895{
896  int clean = 0, dirty = 0, locked = 0;
897  struct list_head *temp;
898  int i, len, pages = 0, cnt;
899  MOD_INC_USE_COUNT;
900  spin_lock(&mbd_writeq_lock);
901  cnt = write_queue_cnt;
902  i = write_queue_tail;
903  while(cnt) {
904    if(!write_queue[i].iserase)
905      pages += write_queue[i].pagecnt;
906    i++;
907    i %= write_queue_sz;
908    cnt--;
909  }
910
911  /* Count the size of the page lists */
912  list_for_each(temp, &mtd_rawdevice->as.clean_pages) {
913    clean++;
914  }
915  list_for_each(temp, &mtd_rawdevice->as.dirty_pages) {
916    dirty++;
917  }
918  list_for_each(temp, &mtd_rawdevice->as.locked_pages) {
919    locked++;
920  }
921
922  len = sprintf(page, "Write queue head: %d\nWrite queue tail: %d\n"
923		"Write queue count: %d\nPages in queue: %d (%dK)\n"
924		"Clean Pages: %d\nDirty Pages: %d\nLocked Pages: %d\n"
925		"nrpages: %ld\n",
926		write_queue_head, write_queue_tail, write_queue_cnt,
927		pages, pages << (PAGE_SHIFT-10), clean, dirty, locked,
928		mtd_rawdevice->as.nrpages);
929  if(len <= count)
930    *eof = 1;
931  spin_unlock(&mbd_writeq_lock);
932  MOD_DEC_USE_COUNT;
933  return len;
934}
935#endif
936
937
938/* Cleanup and exit - sync the device and kill of the kernel thread */
939static void __exit cleanup_blkmtd(void)
940{
941#ifdef BLKMTD_PROC_DEBUG
942  if(blkmtd_proc) {
943    remove_proc_entry("blkmtd_debug", NULL);
944  }
945#endif
946
947  if (mtd_rawdevice) {
948    /* sync the device */
949    if (!mtd_rawdevice->readonly) {
950      blkmtd_sync(&mtd_rawdevice->mtd_info);
951      write_task_finish = 1;
952      wake_up_interruptible(&thr_wq);
953      down(&thread_sem);
954    }
955    del_mtd_device(&mtd_rawdevice->mtd_info);
956    if(mtd_rawdevice->binding != NULL)
957      blkdev_put(mtd_rawdevice->binding, BDEV_RAW);
958
959    if(mtd_rawdevice->mtd_info.eraseregions)
960      kfree(mtd_rawdevice->mtd_info.eraseregions);
961    if(mtd_rawdevice->mtd_info.name)
962      kfree(mtd_rawdevice->mtd_info.name);
963
964    kfree(mtd_rawdevice);
965  }
966  if(write_queue)
967    kfree(write_queue);
968
969  if(erase_page) {
970    UnlockPage(erase_page);
971    __free_pages(erase_page, 0);
972  }
973  printk("blkmtd: unloaded for %s\n", device);
974}
975
976extern struct module __this_module;
977
978#ifndef MODULE
979
980/* Handle kernel boot params */
981
982
983static int __init param_blkmtd_device(char *str)
984{
985  device = str;
986  return 1;
987}
988
989
990static int __init param_blkmtd_erasesz(char *str)
991{
992  erasesz = simple_strtol(str, NULL, 0);
993  return 1;
994}
995
996
997static int __init param_blkmtd_ro(char *str)
998{
999  ro = simple_strtol(str, NULL, 0);
1000  return 1;
1001}
1002
1003
1004static int __init param_blkmtd_bs(char *str)
1005{
1006  bs = simple_strtol(str, NULL, 0);
1007  return 1;
1008}
1009
1010
1011static int __init param_blkmtd_count(char *str)
1012{
1013  count = simple_strtol(str, NULL, 0);
1014  return 1;
1015}
1016
1017__setup("blkmtd_device=", param_blkmtd_device);
1018__setup("blkmtd_erasesz=", param_blkmtd_erasesz);
1019__setup("blkmtd_ro=", param_blkmtd_ro);
1020__setup("blkmtd_bs=", param_blkmtd_bs);
1021__setup("blkmtd_count=", param_blkmtd_count);
1022
1023#endif
1024
1025
1026/* for a given size and initial erase size, calculate the number and size of each
1027   erase region */
1028static int __init calc_erase_regions(struct mtd_erase_region_info *info, size_t erase_size, size_t total_size)
1029{
1030  int count = 0;
1031  int offset = 0;
1032  int regions = 0;
1033
1034   while(total_size) {
1035     count = total_size / erase_size;
1036     if(count) {
1037       total_size = total_size % erase_size;
1038       if(info) {
1039	 info->offset = offset;
1040	 info->erasesize = erase_size;
1041	 info->numblocks = count;
1042	 info++;
1043       }
1044       offset += (count * erase_size);
1045       regions++;
1046     }
1047     while(erase_size > total_size)
1048       erase_size >>= 1;
1049   }
1050   return regions;
1051}
1052
1053
1054extern kdev_t name_to_kdev_t(char *line) __init;
1055
1056/* Startup */
1057static int __init init_blkmtd(void)
1058{
1059#ifdef MODULE
1060  struct file *file = NULL;
1061  struct inode *inode;
1062#endif
1063
1064  int maj, min;
1065  int i, blocksize, blocksize_bits;
1066  loff_t size = 0;
1067  int readonly = 0;
1068  int erase_size = CONFIG_MTD_BLKDEV_ERASESIZE;
1069  kdev_t rdev;
1070  int err;
1071  int mode;
1072  int regions;
1073
1074  /* Check args */
1075  if(device == 0) {
1076    printk("blkmtd: error, missing `device' name\n");
1077    return -EINVAL;
1078  }
1079
1080  if(ro)
1081    readonly = 1;
1082
1083  if(erasesz)
1084    erase_size = erasesz;
1085
1086  if(wqs) {
1087    if(wqs < 16)
1088      wqs = 16;
1089    if(wqs > 4*WRITE_QUEUE_SZ)
1090      wqs = 4*WRITE_QUEUE_SZ;
1091    write_queue_sz = wqs;
1092  }
1093
1094  DEBUG(1, "blkmtd: device = `%s' erase size = %dK readonly = %s queue size = %d\n",
1095	device, erase_size, readonly ? "yes" : "no", write_queue_sz);
1096  /* Get a handle on the device */
1097  mode = (readonly) ? O_RDONLY : O_RDWR;
1098
1099#ifdef MODULE
1100
1101  file = filp_open(device, mode, 0);
1102  if(IS_ERR(file)) {
1103    printk("blkmtd: error, cant open device %s\n", device);
1104    DEBUG(2, "blkmtd: filp_open returned %ld\n", PTR_ERR(file));
1105    return 1;
1106  }
1107
1108  /* determine is this is a block device and if so get its major and minor
1109     numbers */
1110  inode = file->f_dentry->d_inode;
1111  if(!S_ISBLK(inode->i_mode)) {
1112    printk("blkmtd: %s not a block device\n", device);
1113    filp_close(file, NULL);
1114    return 1;
1115  }
1116  rdev = inode->i_rdev;
1117  filp_close(file, NULL);
1118#else
1119  rdev = name_to_kdev_t(device);
1120#endif
1121
1122  maj = MAJOR(rdev);
1123  min = MINOR(rdev);
1124  DEBUG(1, "blkmtd: found a block device major = %d, minor = %d\n", maj, min);
1125
1126  if(!rdev) {
1127    printk("blkmtd: bad block device: `%s'\n", device);
1128    return 1;
1129  }
1130
1131  if(maj == MTD_BLOCK_MAJOR) {
1132    printk("blkmtd: attempting to use an MTD device as a block device\n");
1133    return 1;
1134  }
1135
1136  DEBUG(1, "blkmtd: devname = %s\n", bdevname(rdev));
1137  blocksize = BLOCK_SIZE;
1138
1139  if(bs) {
1140    blocksize = bs;
1141  } else {
1142    if (blksize_size[maj] && blksize_size[maj][min]) {
1143      DEBUG(2, "blkmtd: blksize_size = %d\n", blksize_size[maj][min]);
1144      blocksize = blksize_size[maj][min];
1145    }
1146  }
1147  i = blocksize;
1148  blocksize_bits = 0;
1149  while(i != 1) {
1150    blocksize_bits++;
1151    i >>= 1;
1152  }
1153
1154  if(count) {
1155    size = count;
1156  } else {
1157    if (blk_size[maj]) {
1158      size = ((loff_t) blk_size[maj][min] << BLOCK_SIZE_BITS) >> blocksize_bits;
1159    }
1160  }
1161  size *= blocksize;
1162  DEBUG(1, "blkmtd: size = %ld\n", (long int)size);
1163
1164  if(size == 0) {
1165    printk("blkmtd: cant determine size\n");
1166    return 1;
1167  }
1168
1169  mtd_rawdevice = (mtd_raw_dev_data_t *)kmalloc(sizeof(mtd_raw_dev_data_t), GFP_KERNEL);
1170  if(mtd_rawdevice == NULL) {
1171    err = -ENOMEM;
1172    goto init_err;
1173  }
1174  memset(mtd_rawdevice, 0, sizeof(mtd_raw_dev_data_t));
1175  /* get the block device */
1176  mtd_rawdevice->binding = bdget(kdev_t_to_nr(MKDEV(maj, min)));
1177  err = blkdev_get(mtd_rawdevice->binding, mode, 0, BDEV_RAW);
1178  if (err) {
1179    goto init_err;
1180  }
1181  mtd_rawdevice->totalsize = size;
1182  mtd_rawdevice->sector_size = blocksize;
1183  mtd_rawdevice->sector_bits = blocksize_bits;
1184  mtd_rawdevice->readonly = readonly;
1185
1186  /* See if device ends on page boundary */
1187  if(size % PAGE_SIZE) {
1188    mtd_rawdevice->partial_last_page = size >> PAGE_SHIFT;
1189    mtd_rawdevice->last_page_sectors = (size & (PAGE_SIZE-1)) >> blocksize_bits;
1190  }
1191
1192  DEBUG(2, "sector_size = %d, sector_bits = %d, partial_last_page = %d last_page_sectors = %d\n",
1193	mtd_rawdevice->sector_size, mtd_rawdevice->sector_bits,
1194	mtd_rawdevice->partial_last_page, mtd_rawdevice->last_page_sectors);
1195
1196  /* Setup the MTD structure */
1197  /* make the name contain the block device in */
1198  mtd_rawdevice->mtd_info.name = kmalloc(9 + strlen(device), GFP_KERNEL);
1199  if(mtd_rawdevice->mtd_info.name == NULL)
1200    goto init_err;
1201
1202  sprintf(mtd_rawdevice->mtd_info.name, "blkmtd: %s", device);
1203  if(readonly) {
1204    mtd_rawdevice->mtd_info.type = MTD_ROM;
1205    mtd_rawdevice->mtd_info.flags = MTD_CAP_ROM;
1206    mtd_rawdevice->mtd_info.erasesize = erase_size << 10;
1207  } else {
1208    mtd_rawdevice->mtd_info.type = MTD_RAM;
1209    mtd_rawdevice->mtd_info.flags = MTD_CAP_RAM;
1210    mtd_rawdevice->mtd_info.erasesize = erase_size << 10;
1211  }
1212  mtd_rawdevice->mtd_info.size = size;
1213  mtd_rawdevice->mtd_info.erase = blkmtd_erase;
1214  mtd_rawdevice->mtd_info.read = blkmtd_read;
1215  mtd_rawdevice->mtd_info.write = blkmtd_write;
1216  mtd_rawdevice->mtd_info.sync = blkmtd_sync;
1217  mtd_rawdevice->mtd_info.point = 0;
1218  mtd_rawdevice->mtd_info.unpoint = 0;
1219
1220  mtd_rawdevice->mtd_info.priv = mtd_rawdevice;
1221  regions = calc_erase_regions(NULL, erase_size << 10, size);
1222  DEBUG(1, "blkmtd: init: found %d erase regions\n", regions);
1223  mtd_rawdevice->mtd_info.eraseregions = kmalloc(regions * sizeof(struct mtd_erase_region_info), GFP_KERNEL);
1224  if(mtd_rawdevice->mtd_info.eraseregions == NULL) {
1225    err = -ENOMEM;
1226    goto init_err;
1227  }
1228  mtd_rawdevice->mtd_info.numeraseregions = regions;
1229  calc_erase_regions(mtd_rawdevice->mtd_info.eraseregions, erase_size << 10, size);
1230
1231  /* setup the page cache info */
1232
1233  mtd_rawdevice->as.nrpages = 0;
1234  INIT_LIST_HEAD(&mtd_rawdevice->as.clean_pages);
1235  INIT_LIST_HEAD(&mtd_rawdevice->as.dirty_pages);
1236  INIT_LIST_HEAD(&mtd_rawdevice->as.locked_pages);
1237  mtd_rawdevice->as.host = NULL;
1238  spin_lock_init(&(mtd_rawdevice->as.i_shared_lock));
1239
1240  mtd_rawdevice->as.a_ops = &blkmtd_aops;
1241  mtd_rawdevice->as.i_mmap = NULL;
1242  mtd_rawdevice->as.i_mmap_shared = NULL;
1243  mtd_rawdevice->as.gfp_mask = GFP_KERNEL;
1244
1245#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,2,0)
1246  mtd_rawdevice->mtd_info.module = THIS_MODULE;
1247#endif
1248  if (add_mtd_device(&mtd_rawdevice->mtd_info)) {
1249    err = -EIO;
1250    goto init_err;
1251  }
1252
1253  if(!mtd_rawdevice->readonly) {
1254    /* Allocate the write queue */
1255    write_queue = kmalloc(write_queue_sz * sizeof(mtdblkdev_write_queue_t), GFP_KERNEL);
1256    if(!write_queue) {
1257      err = -ENOMEM;
1258      goto init_err;
1259    }
1260    /* Set up the erase page */
1261    erase_page = alloc_pages(GFP_KERNEL, 0);
1262    if(erase_page == NULL) {
1263      err = -ENOMEM;
1264      goto init_err;
1265    }
1266    memset(page_address(erase_page), 0xff, PAGE_SIZE);
1267    lock_page(erase_page);
1268
1269    init_waitqueue_head(&thr_wq);
1270    init_waitqueue_head(&mtbd_sync_wq);
1271    DEBUG(3, "blkmtd: init: kernel task @ %p\n", write_queue_task);
1272    DEBUG(2, "blkmtd: init: starting kernel task\n");
1273    kernel_thread(write_queue_task, NULL, CLONE_FS | CLONE_FILES | CLONE_SIGHAND);
1274    DEBUG(2, "blkmtd: init: started\n");
1275    printk("blkmtd loaded: version = %s using %s erase_size = %dK %s\n",
1276	   VERSION, device, erase_size, (readonly) ? "(read-only)" : "");
1277  }
1278
1279#ifdef BLKMTD_PROC_DEBUG
1280  /* create proc entry */
1281  DEBUG(2, "Creating /proc/blkmtd_debug\n");
1282  blkmtd_proc = create_proc_read_entry("blkmtd_debug", 0444,
1283				       NULL, blkmtd_proc_read, NULL);
1284  if(blkmtd_proc == NULL) {
1285    printk("Cant create /proc/blkmtd_debug\n");
1286  } else {
1287    blkmtd_proc->owner = THIS_MODULE;
1288  }
1289#endif
1290
1291  /* Everything is ok if we got here */
1292  return 0;
1293
1294 init_err:
1295
1296  if(mtd_rawdevice) {
1297    if(mtd_rawdevice->mtd_info.eraseregions)
1298      kfree(mtd_rawdevice->mtd_info.eraseregions);
1299    if(mtd_rawdevice->mtd_info.name)
1300      kfree(mtd_rawdevice->mtd_info.name);
1301    if(mtd_rawdevice->binding)
1302      blkdev_put(mtd_rawdevice->binding, BDEV_RAW);
1303    kfree(mtd_rawdevice);
1304  }
1305
1306  if(write_queue) {
1307    kfree(write_queue);
1308    write_queue = NULL;
1309  }
1310
1311  if(erase_page)
1312    __free_pages(erase_page, 0);
1313  return err;
1314}
1315
1316module_init(init_blkmtd);
1317module_exit(cleanup_blkmtd);
1318