Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 2 | * $Id: mtd_blkdevs.c,v 1.27 2005/11/07 11:14:20 gleixner Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 19 | #include <linux/freezer.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #include <linux/spinlock.h> |
| 21 | #include <linux/hdreg.h> |
| 22 | #include <linux/init.h> |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 23 | #include <linux/mutex.h> |
Eric W. Biederman | 99f9b24 | 2007-04-19 01:58:33 -0600 | [diff] [blame] | 24 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include <asm/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 27 | #include "mtdcore.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Ben Dooks | 356d70f | 2007-05-28 20:28:34 +0100 | [diff] [blame] | 29 | static LIST_HEAD(blktrans_majors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | struct mtd_blkcore_priv { |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 32 | struct task_struct *thread; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | struct request_queue *rq; |
| 34 | spinlock_t queue_lock; |
| 35 | }; |
| 36 | |
| 37 | static int do_blktrans_request(struct mtd_blktrans_ops *tr, |
| 38 | struct mtd_blktrans_dev *dev, |
| 39 | struct request *req) |
| 40 | { |
| 41 | unsigned long block, nsect; |
| 42 | char *buf; |
| 43 | |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 44 | block = req->sector << 9 >> tr->blkshift; |
| 45 | nsect = req->current_nr_sectors << 9 >> tr->blkshift; |
| 46 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | buf = req->buffer; |
| 48 | |
Jens Axboe | 4aff5e2 | 2006-08-10 08:44:47 +0200 | [diff] [blame] | 49 | if (!blk_fs_request(req)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | return 0; |
| 51 | |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 52 | if (req->sector + req->current_nr_sectors > get_capacity(req->rq_disk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | return 0; |
| 54 | |
| 55 | switch(rq_data_dir(req)) { |
| 56 | case READ: |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 57 | for (; nsect > 0; nsect--, block++, buf += tr->blksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 66 | for (; nsect > 0; nsect--, block++, buf += tr->blksize) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | if (tr->writesect(dev, block, buf)) |
| 68 | return 0; |
| 69 | return 1; |
| 70 | |
| 71 | default: |
Jeff Garzik | 9a29230 | 2006-10-01 12:16:00 -0400 | [diff] [blame] | 72 | printk(KERN_NOTICE "Unknown request %u\n", rq_data_dir(req)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Rafael J. Wysocki | 8314418 | 2007-07-17 04:03:35 -0700 | [diff] [blame] | 83 | current->flags |= PF_MEMALLOC; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | spin_lock_irq(rq->queue_lock); |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 86 | while (!kthread_should_stop()) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | struct request *req; |
| 88 | struct mtd_blktrans_dev *dev; |
| 89 | int res = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | |
| 91 | req = elv_next_request(rq); |
| 92 | |
| 93 | if (!req) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | set_current_state(TASK_INTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | spin_unlock_irq(rq->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | schedule(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | spin_lock_irq(rq->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | continue; |
| 99 | } |
| 100 | |
| 101 | dev = req->rq_disk->private_data; |
| 102 | tr = dev->tr; |
| 103 | |
| 104 | spin_unlock_irq(rq->queue_lock); |
| 105 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 106 | mutex_lock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | res = do_blktrans_request(tr, dev, req); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 108 | mutex_unlock(&dev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | spin_lock_irq(rq->queue_lock); |
| 111 | |
| 112 | end_request(req, res); |
| 113 | } |
| 114 | spin_unlock_irq(rq->queue_lock); |
| 115 | |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 116 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | static void mtd_blktrans_request(struct request_queue *rq) |
| 120 | { |
| 121 | struct mtd_blktrans_ops *tr = rq->queuedata; |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 122 | wake_up_process(tr->blkcore_priv->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | |
| 126 | static int blktrans_open(struct inode *i, struct file *f) |
| 127 | { |
| 128 | struct mtd_blktrans_dev *dev; |
| 129 | struct mtd_blktrans_ops *tr; |
| 130 | int ret = -ENODEV; |
| 131 | |
| 132 | dev = i->i_bdev->bd_disk->private_data; |
| 133 | tr = dev->tr; |
| 134 | |
| 135 | if (!try_module_get(dev->mtd->owner)) |
| 136 | goto out; |
| 137 | |
| 138 | if (!try_module_get(tr->owner)) |
| 139 | goto out_tr; |
| 140 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 141 | /* FIXME: Locking. A hot pluggable device can go away |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | (del_mtd_device can be called for it) without its module |
| 143 | being unloaded. */ |
| 144 | dev->mtd->usecount++; |
| 145 | |
| 146 | ret = 0; |
| 147 | if (tr->open && (ret = tr->open(dev))) { |
| 148 | dev->mtd->usecount--; |
| 149 | module_put(dev->mtd->owner); |
| 150 | out_tr: |
| 151 | module_put(tr->owner); |
| 152 | } |
| 153 | out: |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static int blktrans_release(struct inode *i, struct file *f) |
| 158 | { |
| 159 | struct mtd_blktrans_dev *dev; |
| 160 | struct mtd_blktrans_ops *tr; |
| 161 | int ret = 0; |
| 162 | |
| 163 | dev = i->i_bdev->bd_disk->private_data; |
| 164 | tr = dev->tr; |
| 165 | |
| 166 | if (tr->release) |
| 167 | ret = tr->release(dev); |
| 168 | |
| 169 | if (!ret) { |
| 170 | dev->mtd->usecount--; |
| 171 | module_put(dev->mtd->owner); |
| 172 | module_put(tr->owner); |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 178 | static int blktrans_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
| 179 | { |
| 180 | struct mtd_blktrans_dev *dev = bdev->bd_disk->private_data; |
| 181 | |
| 182 | if (dev->tr->getgeo) |
| 183 | return dev->tr->getgeo(dev, geo); |
| 184 | return -ENOTTY; |
| 185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 187 | static int blktrans_ioctl(struct inode *inode, struct file *file, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | unsigned int cmd, unsigned long arg) |
| 189 | { |
| 190 | struct mtd_blktrans_dev *dev = inode->i_bdev->bd_disk->private_data; |
| 191 | struct mtd_blktrans_ops *tr = dev->tr; |
| 192 | |
| 193 | switch (cmd) { |
| 194 | case BLKFLSBUF: |
| 195 | if (tr->flush) |
| 196 | return tr->flush(dev); |
| 197 | /* The core code did the work, we had nothing to do. */ |
| 198 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | default: |
| 200 | return -ENOTTY; |
| 201 | } |
| 202 | } |
| 203 | |
Ben Dooks | 8f026f7 | 2007-05-28 19:51:59 +0100 | [diff] [blame] | 204 | static struct block_device_operations mtd_blktrans_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | .owner = THIS_MODULE, |
| 206 | .open = blktrans_open, |
| 207 | .release = blktrans_release, |
| 208 | .ioctl = blktrans_ioctl, |
Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 209 | .getgeo = blktrans_getgeo, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | }; |
| 211 | |
| 212 | int add_mtd_blktrans_dev(struct mtd_blktrans_dev *new) |
| 213 | { |
| 214 | struct mtd_blktrans_ops *tr = new->tr; |
| 215 | struct list_head *this; |
| 216 | int last_devnum = -1; |
| 217 | struct gendisk *gd; |
| 218 | |
Jean Delvare | b3561ea | 2007-05-08 00:30:46 -0700 | [diff] [blame] | 219 | if (mutex_trylock(&mtd_table_mutex)) { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 220 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | BUG(); |
| 222 | } |
| 223 | |
| 224 | list_for_each(this, &tr->devs) { |
| 225 | struct mtd_blktrans_dev *d = list_entry(this, struct mtd_blktrans_dev, list); |
| 226 | if (new->devnum == -1) { |
| 227 | /* Use first free number */ |
| 228 | if (d->devnum != last_devnum+1) { |
| 229 | /* Found a free devnum. Plug it in here */ |
| 230 | new->devnum = last_devnum+1; |
| 231 | list_add_tail(&new->list, &d->list); |
| 232 | goto added; |
| 233 | } |
| 234 | } else if (d->devnum == new->devnum) { |
| 235 | /* Required number taken */ |
| 236 | return -EBUSY; |
| 237 | } else if (d->devnum > new->devnum) { |
| 238 | /* Required number was free */ |
| 239 | list_add_tail(&new->list, &d->list); |
| 240 | goto added; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 241 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | last_devnum = d->devnum; |
| 243 | } |
| 244 | if (new->devnum == -1) |
| 245 | new->devnum = last_devnum+1; |
| 246 | |
| 247 | if ((new->devnum << tr->part_bits) > 256) { |
| 248 | return -EBUSY; |
| 249 | } |
| 250 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 251 | mutex_init(&new->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | list_add_tail(&new->list, &tr->devs); |
| 253 | added: |
| 254 | if (!tr->writesect) |
| 255 | new->readonly = 1; |
| 256 | |
| 257 | gd = alloc_disk(1 << tr->part_bits); |
| 258 | if (!gd) { |
| 259 | list_del(&new->list); |
| 260 | return -ENOMEM; |
| 261 | } |
| 262 | gd->major = tr->major; |
| 263 | gd->first_minor = (new->devnum) << tr->part_bits; |
| 264 | gd->fops = &mtd_blktrans_ops; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 265 | |
Todd Poynor | 65a8de3 | 2005-07-29 20:42:07 +0100 | [diff] [blame] | 266 | if (tr->part_bits) |
| 267 | if (new->devnum < 26) |
| 268 | snprintf(gd->disk_name, sizeof(gd->disk_name), |
| 269 | "%s%c", tr->name, 'a' + new->devnum); |
| 270 | else |
| 271 | snprintf(gd->disk_name, sizeof(gd->disk_name), |
| 272 | "%s%c%c", tr->name, |
| 273 | 'a' - 1 + new->devnum / 26, |
| 274 | 'a' + new->devnum % 26); |
| 275 | else |
| 276 | snprintf(gd->disk_name, sizeof(gd->disk_name), |
| 277 | "%s%d", tr->name, new->devnum); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | |
| 279 | /* 2.5 has capacity in units of 512 bytes while still |
| 280 | having BLOCK_SIZE_BITS set to 10. Just to keep us amused. */ |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 281 | set_capacity(gd, (new->size * tr->blksize) >> 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 282 | |
| 283 | gd->private_data = new; |
| 284 | new->blkcore_priv = gd; |
| 285 | gd->queue = tr->blkcore_priv->rq; |
| 286 | |
| 287 | if (new->readonly) |
| 288 | set_disk_ro(gd, 1); |
| 289 | |
| 290 | add_disk(gd); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 291 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | int del_mtd_blktrans_dev(struct mtd_blktrans_dev *old) |
| 296 | { |
Jean Delvare | b3561ea | 2007-05-08 00:30:46 -0700 | [diff] [blame] | 297 | if (mutex_trylock(&mtd_table_mutex)) { |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 298 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | BUG(); |
| 300 | } |
| 301 | |
| 302 | list_del(&old->list); |
| 303 | |
| 304 | del_gendisk(old->blkcore_priv); |
| 305 | put_disk(old->blkcore_priv); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 306 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | return 0; |
| 308 | } |
| 309 | |
| 310 | static void blktrans_notify_remove(struct mtd_info *mtd) |
| 311 | { |
| 312 | struct list_head *this, *this2, *next; |
| 313 | |
| 314 | list_for_each(this, &blktrans_majors) { |
| 315 | struct mtd_blktrans_ops *tr = list_entry(this, struct mtd_blktrans_ops, list); |
| 316 | |
| 317 | list_for_each_safe(this2, next, &tr->devs) { |
| 318 | struct mtd_blktrans_dev *dev = list_entry(this2, struct mtd_blktrans_dev, list); |
| 319 | |
| 320 | if (dev->mtd == mtd) |
| 321 | tr->remove_dev(dev); |
| 322 | } |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static void blktrans_notify_add(struct mtd_info *mtd) |
| 327 | { |
| 328 | struct list_head *this; |
| 329 | |
| 330 | if (mtd->type == MTD_ABSENT) |
| 331 | return; |
| 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 | }; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | int register_mtd_blktrans(struct mtd_blktrans_ops *tr) |
| 347 | { |
| 348 | int ret, i; |
| 349 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 350 | /* Register the notifier if/when the first device type is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 356 | tr->blkcore_priv = kzalloc(sizeof(*tr->blkcore_priv), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | if (!tr->blkcore_priv) |
| 358 | return -ENOMEM; |
| 359 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 360 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | |
| 362 | ret = register_blkdev(tr->major, tr->name); |
| 363 | if (ret) { |
| 364 | printk(KERN_WARNING "Unable to register %s block device on major %d: %d\n", |
| 365 | tr->name, tr->major, ret); |
| 366 | kfree(tr->blkcore_priv); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 367 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | return ret; |
| 369 | } |
| 370 | spin_lock_init(&tr->blkcore_priv->queue_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | tr->blkcore_priv->rq = blk_init_queue(mtd_blktrans_request, &tr->blkcore_priv->queue_lock); |
| 373 | if (!tr->blkcore_priv->rq) { |
| 374 | unregister_blkdev(tr->major, tr->name); |
| 375 | kfree(tr->blkcore_priv); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 376 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | return -ENOMEM; |
| 378 | } |
| 379 | |
| 380 | tr->blkcore_priv->rq->queuedata = tr; |
Richard Purdie | 1918767 | 2006-10-27 09:09:33 +0100 | [diff] [blame] | 381 | blk_queue_hardsect_size(tr->blkcore_priv->rq, tr->blksize); |
| 382 | tr->blkshift = ffs(tr->blksize) - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 384 | tr->blkcore_priv->thread = kthread_run(mtd_blktrans_thread, tr, |
| 385 | "%sd", tr->name); |
| 386 | if (IS_ERR(tr->blkcore_priv->thread)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | blk_cleanup_queue(tr->blkcore_priv->rq); |
| 388 | unregister_blkdev(tr->major, tr->name); |
| 389 | kfree(tr->blkcore_priv); |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 390 | mutex_unlock(&mtd_table_mutex); |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 391 | return PTR_ERR(tr->blkcore_priv->thread); |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 392 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | INIT_LIST_HEAD(&tr->devs); |
| 395 | list_add(&tr->list, &blktrans_majors); |
| 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 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 402 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | int deregister_mtd_blktrans(struct mtd_blktrans_ops *tr) |
| 408 | { |
| 409 | struct list_head *this, *next; |
| 410 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 411 | mutex_lock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | |
| 413 | /* Clean up the kernel thread */ |
Christoph Hellwig | 3e67fe4 | 2007-04-22 20:40:57 +0100 | [diff] [blame] | 414 | kthread_stop(tr->blkcore_priv->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
| 416 | /* Remove it from the list of active majors */ |
| 417 | list_del(&tr->list); |
| 418 | |
| 419 | list_for_each_safe(this, next, &tr->devs) { |
| 420 | struct mtd_blktrans_dev *dev = list_entry(this, struct mtd_blktrans_dev, list); |
| 421 | tr->remove_dev(dev); |
| 422 | } |
| 423 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | blk_cleanup_queue(tr->blkcore_priv->rq); |
| 425 | unregister_blkdev(tr->major, tr->name); |
| 426 | |
Ingo Molnar | 48b1926 | 2006-03-31 02:29:41 -0800 | [diff] [blame] | 427 | mutex_unlock(&mtd_table_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
| 429 | kfree(tr->blkcore_priv); |
| 430 | |
Eric Sesterhenn | 373ebfb | 2006-03-26 18:15:12 +0200 | [diff] [blame] | 431 | BUG_ON(!list_empty(&tr->devs)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | static void __exit mtd_blktrans_exit(void) |
| 436 | { |
| 437 | /* No race here -- if someone's currently in register_mtd_blktrans |
| 438 | we're screwed anyway. */ |
| 439 | if (blktrans_notifier.list.next) |
| 440 | unregister_mtd_user(&blktrans_notifier); |
| 441 | } |
| 442 | |
| 443 | module_exit(mtd_blktrans_exit); |
| 444 | |
| 445 | EXPORT_SYMBOL_GPL(register_mtd_blktrans); |
| 446 | EXPORT_SYMBOL_GPL(deregister_mtd_blktrans); |
| 447 | EXPORT_SYMBOL_GPL(add_mtd_blktrans_dev); |
| 448 | EXPORT_SYMBOL_GPL(del_mtd_blktrans_dev); |
| 449 | |
| 450 | MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>"); |
| 451 | MODULE_LICENSE("GPL"); |
| 452 | MODULE_DESCRIPTION("Common interface to block layer for MTD 'translation layers'"); |