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