| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 |  * Copyright (C) 2000 Jeff Dike (jdike@karaya.com) | 
 | 3 |  * Licensed under the GPL | 
 | 4 |  */ | 
 | 5 |  | 
 | 6 | /* 2001-09-28...2002-04-17 | 
 | 7 |  * Partition stuff by James_McMechan@hotmail.com | 
 | 8 |  * old style ubd by setting UBD_SHIFT to 0 | 
 | 9 |  * 2002-09-27...2002-10-18 massive tinkering for 2.5 | 
 | 10 |  * partitions have changed in 2.5 | 
 | 11 |  * 2003-01-29 more tinkering for 2.5.59-1 | 
 | 12 |  * This should now address the sysfs problems and has | 
 | 13 |  * the symlink for devfs to allow for booting with | 
 | 14 |  * the common /dev/ubd/discX/... names rather than | 
 | 15 |  * only /dev/ubdN/discN this version also has lots of | 
 | 16 |  * clean ups preparing for ubd-many. | 
 | 17 |  * James McMechan | 
 | 18 |  */ | 
 | 19 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | #define UBD_SHIFT 4 | 
 | 21 |  | 
| Jeff Dike | e16f535 | 2007-06-08 13:46:54 -0700 | [diff] [blame] | 22 | #include "linux/kernel.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | #include "linux/module.h" | 
 | 24 | #include "linux/blkdev.h" | 
| Bartlomiej Zolnierkiewicz | 73855e1 | 2009-04-01 21:42:21 +0200 | [diff] [blame] | 25 | #include "linux/ata.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include "linux/hdreg.h" | 
 | 27 | #include "linux/init.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include "linux/cdrom.h" | 
 | 29 | #include "linux/proc_fs.h" | 
| Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 30 | #include "linux/seq_file.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include "linux/ctype.h" | 
 | 32 | #include "linux/capability.h" | 
 | 33 | #include "linux/mm.h" | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 34 | #include "linux/slab.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include "linux/vmalloc.h" | 
| Arnd Bergmann | 9a181c5 | 2010-09-11 18:38:03 +0200 | [diff] [blame] | 36 | #include "linux/mutex.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include "linux/blkpg.h" | 
 | 38 | #include "linux/genhd.h" | 
 | 39 | #include "linux/spinlock.h" | 
| Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 40 | #include "linux/platform_device.h" | 
| WANG Cong | 23464ff | 2007-10-24 13:07:11 +0200 | [diff] [blame] | 41 | #include "linux/scatterlist.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | #include "asm/segment.h" | 
 | 43 | #include "asm/uaccess.h" | 
 | 44 | #include "asm/irq.h" | 
 | 45 | #include "asm/types.h" | 
 | 46 | #include "asm/tlbflush.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include "mem_user.h" | 
 | 48 | #include "kern_util.h" | 
 | 49 | #include "kern.h" | 
 | 50 | #include "mconsole_kern.h" | 
 | 51 | #include "init.h" | 
 | 52 | #include "irq_user.h" | 
 | 53 | #include "irq_kern.h" | 
 | 54 | #include "ubd_user.h" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #include "os.h" | 
 | 56 | #include "mem.h" | 
 | 57 | #include "mem_kern.h" | 
 | 58 | #include "cow.h" | 
 | 59 |  | 
| Jeff Dike | 7b9014c | 2005-05-20 13:59:11 -0700 | [diff] [blame] | 60 | enum ubd_req { UBD_READ, UBD_WRITE }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 |  | 
 | 62 | struct io_thread_req { | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 63 | 	struct request *req; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 64 | 	enum ubd_req op; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | 	int fds[2]; | 
 | 66 | 	unsigned long offsets[2]; | 
 | 67 | 	unsigned long long offset; | 
 | 68 | 	unsigned long length; | 
 | 69 | 	char *buffer; | 
 | 70 | 	int sectorsize; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 71 | 	unsigned long sector_mask; | 
 | 72 | 	unsigned long long cow_offset; | 
 | 73 | 	unsigned long bitmap_words[2]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | 	int error; | 
 | 75 | }; | 
 | 76 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 77 | static inline int ubd_test_bit(__u64 bit, unsigned char *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | { | 
 | 79 | 	__u64 n; | 
 | 80 | 	int bits, off; | 
 | 81 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 82 | 	bits = sizeof(data[0]) * 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | 	n = bit / bits; | 
 | 84 | 	off = bit % bits; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 85 | 	return (data[n] & (1 << off)) != 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | } | 
 | 87 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 88 | static inline void ubd_set_bit(__u64 bit, unsigned char *data) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | { | 
 | 90 | 	__u64 n; | 
 | 91 | 	int bits, off; | 
 | 92 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 93 | 	bits = sizeof(data[0]) * 8; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | 	n = bit / bits; | 
 | 95 | 	off = bit % bits; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 96 | 	data[n] |= (1 << off); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | } | 
 | 98 | /*End stuff from ubd_user.h*/ | 
 | 99 |  | 
 | 100 | #define DRIVER_NAME "uml-blkdev" | 
 | 101 |  | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 102 | static DEFINE_MUTEX(ubd_lock); | 
| Arnd Bergmann | 9a181c5 | 2010-09-11 18:38:03 +0200 | [diff] [blame] | 103 | static DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 |  | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 105 | static int ubd_open(struct block_device *bdev, fmode_t mode); | 
 | 106 | static int ubd_release(struct gendisk *disk, fmode_t mode); | 
 | 107 | static int ubd_ioctl(struct block_device *bdev, fmode_t mode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | 		     unsigned int cmd, unsigned long arg); | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 109 | static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 |  | 
| Paolo 'Blaisorblade' Giarrusso | 97d88ac | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 111 | #define MAX_DEV (16) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 |  | 
| Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 113 | static const struct block_device_operations ubd_blops = { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 |         .owner		= THIS_MODULE, | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 115 |         .open		= ubd_open, | 
 | 116 |         .release	= ubd_release, | 
 | 117 |         .ioctl		= ubd_ioctl, | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 118 | 	.getgeo		= ubd_getgeo, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | }; | 
 | 120 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 121 | /* Protected by ubd_lock */ | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 122 | static int fake_major = UBD_MAJOR; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | static struct gendisk *ubd_gendisk[MAX_DEV]; | 
 | 124 | static struct gendisk *fake_gendisk[MAX_DEV]; | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 125 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | #ifdef CONFIG_BLK_DEV_UBD_SYNC | 
 | 127 | #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \ | 
 | 128 | 					 .cl = 1 }) | 
 | 129 | #else | 
 | 130 | #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \ | 
 | 131 | 					 .cl = 1 }) | 
 | 132 | #endif | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | static struct openflags global_openflags = OPEN_FLAGS; | 
 | 134 |  | 
 | 135 | struct cow { | 
| Paolo 'Blaisorblade' Giarrusso | 2a9d32f | 2006-10-30 22:07:04 -0800 | [diff] [blame] | 136 | 	/* backing file name */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | 	char *file; | 
| Paolo 'Blaisorblade' Giarrusso | 2a9d32f | 2006-10-30 22:07:04 -0800 | [diff] [blame] | 138 | 	/* backing file fd */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | 	int fd; | 
 | 140 | 	unsigned long *bitmap; | 
 | 141 | 	unsigned long bitmap_len; | 
 | 142 | 	int bitmap_offset; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 143 | 	int data_offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | }; | 
 | 145 |  | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 146 | #define MAX_SG 64 | 
 | 147 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | struct ubd { | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 149 | 	struct list_head restart; | 
| Paolo 'Blaisorblade' Giarrusso | 2a9d32f | 2006-10-30 22:07:04 -0800 | [diff] [blame] | 150 | 	/* name (and fd, below) of the file opened for writing, either the | 
 | 151 | 	 * backing or the cow file. */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | 	char *file; | 
 | 153 | 	int count; | 
 | 154 | 	int fd; | 
 | 155 | 	__u64 size; | 
 | 156 | 	struct openflags boot_openflags; | 
 | 157 | 	struct openflags openflags; | 
| Paolo 'Blaisorblade' Giarrusso | 84e945e | 2006-10-30 22:07:10 -0800 | [diff] [blame] | 158 | 	unsigned shared:1; | 
 | 159 | 	unsigned no_cow:1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | 	struct cow cow; | 
 | 161 | 	struct platform_device pdev; | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 162 | 	struct request_queue *queue; | 
 | 163 | 	spinlock_t lock; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 164 | 	struct scatterlist sg[MAX_SG]; | 
 | 165 | 	struct request *request; | 
 | 166 | 	int start_sg, end_sg; | 
| Tejun Heo | 4752690 | 2010-10-15 12:56:21 +0200 | [diff] [blame] | 167 | 	sector_t rq_pos; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | }; | 
 | 169 |  | 
 | 170 | #define DEFAULT_COW { \ | 
 | 171 | 	.file =			NULL, \ | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 172 | 	.fd =			-1,	\ | 
 | 173 | 	.bitmap =		NULL, \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | 	.bitmap_offset =	0, \ | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 175 | 	.data_offset =		0, \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | } | 
 | 177 |  | 
 | 178 | #define DEFAULT_UBD { \ | 
 | 179 | 	.file = 		NULL, \ | 
 | 180 | 	.count =		0, \ | 
 | 181 | 	.fd =			-1, \ | 
 | 182 | 	.size =			-1, \ | 
 | 183 | 	.boot_openflags =	OPEN_FLAGS, \ | 
 | 184 | 	.openflags =		OPEN_FLAGS, \ | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 185 | 	.no_cow =               0, \ | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 186 | 	.shared =		0, \ | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 187 | 	.cow =			DEFAULT_COW, \ | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 188 | 	.lock =			SPIN_LOCK_UNLOCKED,	\ | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 189 | 	.request =		NULL, \ | 
 | 190 | 	.start_sg =		0, \ | 
 | 191 | 	.end_sg =		0, \ | 
| Tejun Heo | 4752690 | 2010-10-15 12:56:21 +0200 | [diff] [blame] | 192 | 	.rq_pos =		0, \ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } | 
 | 194 |  | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 195 | /* Protected by ubd_lock */ | 
| WANG Cong | 5dc62b1 | 2008-04-28 02:13:58 -0700 | [diff] [blame] | 196 | static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD }; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | /* Only changed by fake_ide_setup which is a setup */ | 
 | 199 | static int fake_ide = 0; | 
 | 200 | static struct proc_dir_entry *proc_ide_root = NULL; | 
 | 201 | static struct proc_dir_entry *proc_ide = NULL; | 
 | 202 |  | 
 | 203 | static void make_proc_ide(void) | 
 | 204 | { | 
 | 205 | 	proc_ide_root = proc_mkdir("ide", NULL); | 
 | 206 | 	proc_ide = proc_mkdir("ide0", proc_ide_root); | 
 | 207 | } | 
 | 208 |  | 
| Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 209 | static int fake_ide_media_proc_show(struct seq_file *m, void *v) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { | 
| Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 211 | 	seq_puts(m, "disk\n"); | 
 | 212 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | } | 
 | 214 |  | 
| Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 215 | static int fake_ide_media_proc_open(struct inode *inode, struct file *file) | 
 | 216 | { | 
 | 217 | 	return single_open(file, fake_ide_media_proc_show, NULL); | 
 | 218 | } | 
 | 219 |  | 
 | 220 | static const struct file_operations fake_ide_media_proc_fops = { | 
 | 221 | 	.owner		= THIS_MODULE, | 
 | 222 | 	.open		= fake_ide_media_proc_open, | 
 | 223 | 	.read		= seq_read, | 
 | 224 | 	.llseek		= seq_lseek, | 
 | 225 | 	.release	= single_release, | 
 | 226 | }; | 
 | 227 |  | 
| WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 228 | static void make_ide_entries(const char *dev_name) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | { | 
 | 230 | 	struct proc_dir_entry *dir, *ent; | 
 | 231 | 	char name[64]; | 
 | 232 |  | 
 | 233 | 	if(proc_ide_root == NULL) make_proc_ide(); | 
 | 234 |  | 
 | 235 | 	dir = proc_mkdir(dev_name, proc_ide); | 
 | 236 | 	if(!dir) return; | 
 | 237 |  | 
| Alexey Dobriyan | 6613c5e | 2009-12-14 18:00:11 -0800 | [diff] [blame] | 238 | 	ent = proc_create("media", S_IRUGO, dir, &fake_ide_media_proc_fops); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 239 | 	if(!ent) return; | 
| WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 240 | 	snprintf(name, sizeof(name), "ide0/%s", dev_name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | 	proc_symlink(dev_name, proc_ide_root, name); | 
 | 242 | } | 
 | 243 |  | 
 | 244 | static int fake_ide_setup(char *str) | 
 | 245 | { | 
 | 246 | 	fake_ide = 1; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 247 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | } | 
 | 249 |  | 
 | 250 | __setup("fake_ide", fake_ide_setup); | 
 | 251 |  | 
 | 252 | __uml_help(fake_ide_setup, | 
 | 253 | "fake_ide\n" | 
 | 254 | "    Create ide0 entries that map onto ubd devices.\n\n" | 
 | 255 | ); | 
 | 256 |  | 
 | 257 | static int parse_unit(char **ptr) | 
 | 258 | { | 
 | 259 | 	char *str = *ptr, *end; | 
 | 260 | 	int n = -1; | 
 | 261 |  | 
 | 262 | 	if(isdigit(*str)) { | 
 | 263 | 		n = simple_strtoul(str, &end, 0); | 
 | 264 | 		if(end == str) | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 265 | 			return -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | 		*ptr = end; | 
 | 267 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | 97d88ac | 2006-10-30 22:07:03 -0800 | [diff] [blame] | 268 | 	else if (('a' <= *str) && (*str <= 'z')) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | 		n = *str - 'a'; | 
 | 270 | 		str++; | 
 | 271 | 		*ptr = str; | 
 | 272 | 	} | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 273 | 	return n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | } | 
 | 275 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 276 | /* If *index_out == -1 at exit, the passed option was a general one; | 
 | 277 |  * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it | 
 | 278 |  * should not be freed on exit. | 
 | 279 |  */ | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 280 | static int ubd_setup_common(char *str, int *index_out, char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 282 | 	struct ubd *ubd_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | 	struct openflags flags = global_openflags; | 
 | 284 | 	char *backing_file; | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 285 | 	int n, err = 0, i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 |  | 
 | 287 | 	if(index_out) *index_out = -1; | 
 | 288 | 	n = *str; | 
 | 289 | 	if(n == '='){ | 
 | 290 | 		char *end; | 
 | 291 | 		int major; | 
 | 292 |  | 
 | 293 | 		str++; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | 		if(!strcmp(str, "sync")){ | 
 | 295 | 			global_openflags = of_sync(global_openflags); | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 296 | 			goto out1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | 		} | 
 | 298 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 299 | 		err = -EINVAL; | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 300 | 		major = simple_strtoul(str, &end, 0); | 
 | 301 | 		if((*end != '\0') || (end == str)){ | 
 | 302 | 			*error_out = "Didn't parse major number"; | 
 | 303 | 			goto out1; | 
 | 304 | 		} | 
 | 305 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 306 | 		mutex_lock(&ubd_lock); | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 307 | 		if (fake_major != UBD_MAJOR) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 308 | 			*error_out = "Can't assign a fake major twice"; | 
 | 309 | 			goto out1; | 
 | 310 | 		} | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 311 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 312 | 		fake_major = major; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 |  | 
 | 314 | 		printk(KERN_INFO "Setting extra ubd major number to %d\n", | 
 | 315 | 		       major); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 316 | 		err = 0; | 
 | 317 | 	out1: | 
 | 318 | 		mutex_unlock(&ubd_lock); | 
 | 319 | 		return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | 	} | 
 | 321 |  | 
 | 322 | 	n = parse_unit(&str); | 
 | 323 | 	if(n < 0){ | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 324 | 		*error_out = "Couldn't parse device number"; | 
 | 325 | 		return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | 	} | 
 | 327 | 	if(n >= MAX_DEV){ | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 328 | 		*error_out = "Device number out of range"; | 
 | 329 | 		return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | 	} | 
 | 331 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 332 | 	err = -EBUSY; | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 333 | 	mutex_lock(&ubd_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 335 | 	ubd_dev = &ubd_devs[n]; | 
 | 336 | 	if(ubd_dev->file != NULL){ | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 337 | 		*error_out = "Device is already configured"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 338 | 		goto out; | 
 | 339 | 	} | 
 | 340 |  | 
 | 341 | 	if (index_out) | 
 | 342 | 		*index_out = n; | 
 | 343 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 344 | 	err = -EINVAL; | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 345 | 	for (i = 0; i < sizeof("rscd="); i++) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | 		switch (*str) { | 
 | 347 | 		case 'r': | 
 | 348 | 			flags.w = 0; | 
 | 349 | 			break; | 
 | 350 | 		case 's': | 
 | 351 | 			flags.s = 1; | 
 | 352 | 			break; | 
 | 353 | 		case 'd': | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 354 | 			ubd_dev->no_cow = 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | 			break; | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 356 | 		case 'c': | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 357 | 			ubd_dev->shared = 1; | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 358 | 			break; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | 		case '=': | 
 | 360 | 			str++; | 
 | 361 | 			goto break_loop; | 
 | 362 | 		default: | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 363 | 			*error_out = "Expected '=' or flag letter " | 
 | 364 | 				"(r, s, c, or d)"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | 			goto out; | 
 | 366 | 		} | 
 | 367 | 		str++; | 
 | 368 | 	} | 
 | 369 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 370 | 	if (*str == '=') | 
 | 371 | 		*error_out = "Too many flags specified"; | 
 | 372 | 	else | 
 | 373 | 		*error_out = "Missing '='"; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | 	goto out; | 
 | 375 |  | 
 | 376 | break_loop: | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | 	backing_file = strchr(str, ','); | 
 | 378 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 379 | 	if (backing_file == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | 		backing_file = strchr(str, ':'); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 382 | 	if(backing_file != NULL){ | 
 | 383 | 		if(ubd_dev->no_cow){ | 
 | 384 | 			*error_out = "Can't specify both 'd' and a cow file"; | 
 | 385 | 			goto out; | 
 | 386 | 		} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | 		else { | 
 | 388 | 			*backing_file = '\0'; | 
 | 389 | 			backing_file++; | 
 | 390 | 		} | 
 | 391 | 	} | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 392 | 	err = 0; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 393 | 	ubd_dev->file = str; | 
 | 394 | 	ubd_dev->cow.file = backing_file; | 
 | 395 | 	ubd_dev->boot_openflags = flags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | out: | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 397 | 	mutex_unlock(&ubd_lock); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 398 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | } | 
 | 400 |  | 
 | 401 | static int ubd_setup(char *str) | 
 | 402 | { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 403 | 	char *error; | 
 | 404 | 	int err; | 
 | 405 |  | 
 | 406 | 	err = ubd_setup_common(str, NULL, &error); | 
 | 407 | 	if(err) | 
 | 408 | 		printk(KERN_ERR "Failed to initialize device with \"%s\" : " | 
 | 409 | 		       "%s\n", str, error); | 
 | 410 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } | 
 | 412 |  | 
 | 413 | __setup("ubd", ubd_setup); | 
 | 414 | __uml_help(ubd_setup, | 
 | 415 | "ubd<n><flags>=<filename>[(:|,)<filename2>]\n" | 
 | 416 | "    This is used to associate a device with a file in the underlying\n" | 
 | 417 | "    filesystem. When specifying two filenames, the first one is the\n" | 
 | 418 | "    COW name and the second is the backing file name. As separator you can\n" | 
 | 419 | "    use either a ':' or a ',': the first one allows writing things like;\n" | 
 | 420 | "	ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n" | 
 | 421 | "    while with a ',' the shell would not expand the 2nd '~'.\n" | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 422 | "    When using only one filename, UML will detect whether to treat it like\n" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | "    a COW file or a backing file. To override this detection, add the 'd'\n" | 
 | 424 | "    flag:\n" | 
 | 425 | "	ubd0d=BackingFile\n" | 
 | 426 | "    Usually, there is a filesystem in the file, but \n" | 
 | 427 | "    that's not required. Swap devices containing swap files can be\n" | 
 | 428 | "    specified like this. Also, a file which doesn't contain a\n" | 
 | 429 | "    filesystem can have its contents read in the virtual \n" | 
 | 430 | "    machine by running 'dd' on the device. <n> must be in the range\n" | 
 | 431 | "    0 to 7. Appending an 'r' to the number will cause that device\n" | 
 | 432 | "    to be mounted read-only. For example ubd1r=./ext_fs. Appending\n" | 
| Jeff Dike | 20ede45 | 2008-02-04 22:30:37 -0800 | [diff] [blame] | 433 | "    an 's' will cause data to be written to disk on the host immediately.\n" | 
 | 434 | "    'c' will cause the device to be treated as being shared between multiple\n" | 
 | 435 | "    UMLs and file locking will be turned off - this is appropriate for a\n" | 
 | 436 | "    cluster filesystem and inappropriate at almost all other times.\n\n" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | ); | 
 | 438 |  | 
| Jeff Dike | 8299ca5 | 2008-02-04 22:30:48 -0800 | [diff] [blame] | 439 | static int udb_setup(char *str) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | { | 
 | 441 | 	printk("udb%s specified on command line is almost certainly a ubd -> " | 
 | 442 | 	       "udb TYPO\n", str); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 443 | 	return 1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | } | 
 | 445 |  | 
 | 446 | __setup("udb", udb_setup); | 
 | 447 | __uml_help(udb_setup, | 
 | 448 | "udb\n" | 
| Jeff Dike | 0894e27 | 2005-05-28 15:51:55 -0700 | [diff] [blame] | 449 | "    This option is here solely to catch ubd -> udb typos, which can be\n" | 
 | 450 | "    to impossible to catch visually unless you specifically look for\n" | 
 | 451 | "    them.  The only result of any option starting with 'udb' is an error\n" | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | "    in the boot output.\n\n" | 
 | 453 | ); | 
 | 454 |  | 
| Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 455 | static void do_ubd_request(struct request_queue * q); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 456 |  | 
 | 457 | /* Only changed by ubd_init, which is an initcall. */ | 
| WANG Cong | 5dc62b1 | 2008-04-28 02:13:58 -0700 | [diff] [blame] | 458 | static int thread_fd = -1; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 459 | static LIST_HEAD(restart); | 
 | 460 |  | 
| Paolo 'Blaisorblade' Giarrusso | 2fe30a3 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 461 | /* XXX - move this inside ubd_intr. */ | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 462 | /* Called without dev->lock held, and only in interrupt context. */ | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 463 | static void ubd_handler(void) | 
 | 464 | { | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 465 | 	struct io_thread_req *req; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 466 | 	struct ubd *ubd; | 
 | 467 | 	struct list_head *list, *next_ele; | 
 | 468 | 	unsigned long flags; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 469 | 	int n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 |  | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 471 | 	while(1){ | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 472 | 		n = os_read_file(thread_fd, &req, | 
 | 473 | 				 sizeof(struct io_thread_req *)); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 474 | 		if(n != sizeof(req)){ | 
 | 475 | 			if(n == -EAGAIN) | 
 | 476 | 				break; | 
 | 477 | 			printk(KERN_ERR "spurious interrupt in ubd_handler, " | 
 | 478 | 			       "err = %d\n", -n); | 
 | 479 | 			return; | 
 | 480 | 		} | 
 | 481 |  | 
| Tejun Heo | 4d6c84d | 2009-04-28 13:06:09 +0900 | [diff] [blame] | 482 | 		blk_end_request(req->req, 0, req->length); | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 483 | 		kfree(req); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 484 | 	} | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 485 | 	reactivate_fd(thread_fd, UBD_IRQ); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 486 |  | 
 | 487 | 	list_for_each_safe(list, next_ele, &restart){ | 
 | 488 | 		ubd = container_of(list, struct ubd, restart); | 
 | 489 | 		list_del_init(&ubd->restart); | 
 | 490 | 		spin_lock_irqsave(&ubd->lock, flags); | 
 | 491 | 		do_ubd_request(ubd->queue); | 
 | 492 | 		spin_unlock_irqrestore(&ubd->lock, flags); | 
 | 493 | 	} | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 494 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 |  | 
| Al Viro | 7bea96f | 2006-10-08 22:49:34 +0100 | [diff] [blame] | 496 | static irqreturn_t ubd_intr(int irq, void *dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | { | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 498 | 	ubd_handler(); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 499 | 	return IRQ_HANDLED; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } | 
 | 501 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 502 | /* Only changed by ubd_init, which is an initcall. */ | 
 | 503 | static int io_pid = -1; | 
 | 504 |  | 
| WANG Cong | 5dc62b1 | 2008-04-28 02:13:58 -0700 | [diff] [blame] | 505 | static void kill_io_thread(void) | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 506 | { | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 507 | 	if(io_pid != -1) | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 508 | 		os_kill_process(io_pid, 1); | 
 | 509 | } | 
 | 510 |  | 
 | 511 | __uml_exitcall(kill_io_thread); | 
 | 512 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 513 | static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | { | 
 | 515 | 	char *file; | 
 | 516 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 517 | 	file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 518 | 	return os_file_size(file, size_out); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } | 
 | 520 |  | 
| WANG Cong | 5dc62b1 | 2008-04-28 02:13:58 -0700 | [diff] [blame] | 521 | static int read_cow_bitmap(int fd, void *buf, int offset, int len) | 
 | 522 | { | 
 | 523 | 	int err; | 
 | 524 |  | 
 | 525 | 	err = os_seek_file(fd, offset); | 
 | 526 | 	if (err < 0) | 
 | 527 | 		return err; | 
 | 528 |  | 
 | 529 | 	err = os_read_file(fd, buf, len); | 
 | 530 | 	if (err < 0) | 
 | 531 | 		return err; | 
 | 532 |  | 
 | 533 | 	return 0; | 
 | 534 | } | 
 | 535 |  | 
 | 536 | static int backing_file_mismatch(char *file, __u64 size, time_t mtime) | 
 | 537 | { | 
 | 538 | 	unsigned long modtime; | 
 | 539 | 	unsigned long long actual; | 
 | 540 | 	int err; | 
 | 541 |  | 
 | 542 | 	err = os_file_modtime(file, &modtime); | 
 | 543 | 	if (err < 0) { | 
 | 544 | 		printk(KERN_ERR "Failed to get modification time of backing " | 
 | 545 | 		       "file \"%s\", err = %d\n", file, -err); | 
 | 546 | 		return err; | 
 | 547 | 	} | 
 | 548 |  | 
 | 549 | 	err = os_file_size(file, &actual); | 
 | 550 | 	if (err < 0) { | 
 | 551 | 		printk(KERN_ERR "Failed to get size of backing file \"%s\", " | 
 | 552 | 		       "err = %d\n", file, -err); | 
 | 553 | 		return err; | 
 | 554 | 	} | 
 | 555 |  | 
 | 556 | 	if (actual != size) { | 
 | 557 | 		/*__u64 can be a long on AMD64 and with %lu GCC complains; so | 
 | 558 | 		 * the typecast.*/ | 
 | 559 | 		printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header " | 
 | 560 | 		       "vs backing file\n", (unsigned long long) size, actual); | 
 | 561 | 		return -EINVAL; | 
 | 562 | 	} | 
 | 563 | 	if (modtime != mtime) { | 
 | 564 | 		printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs " | 
 | 565 | 		       "backing file\n", mtime, modtime); | 
 | 566 | 		return -EINVAL; | 
 | 567 | 	} | 
 | 568 | 	return 0; | 
 | 569 | } | 
 | 570 |  | 
 | 571 | static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow) | 
 | 572 | { | 
 | 573 | 	struct uml_stat buf1, buf2; | 
 | 574 | 	int err; | 
 | 575 |  | 
 | 576 | 	if (from_cmdline == NULL) | 
 | 577 | 		return 0; | 
 | 578 | 	if (!strcmp(from_cmdline, from_cow)) | 
 | 579 | 		return 0; | 
 | 580 |  | 
 | 581 | 	err = os_stat_file(from_cmdline, &buf1); | 
 | 582 | 	if (err < 0) { | 
 | 583 | 		printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline, | 
 | 584 | 		       -err); | 
 | 585 | 		return 0; | 
 | 586 | 	} | 
 | 587 | 	err = os_stat_file(from_cow, &buf2); | 
 | 588 | 	if (err < 0) { | 
 | 589 | 		printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow, | 
 | 590 | 		       -err); | 
 | 591 | 		return 1; | 
 | 592 | 	} | 
 | 593 | 	if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino)) | 
 | 594 | 		return 0; | 
 | 595 |  | 
 | 596 | 	printk(KERN_ERR "Backing file mismatch - \"%s\" requested, " | 
 | 597 | 	       "\"%s\" specified in COW header of \"%s\"\n", | 
 | 598 | 	       from_cmdline, from_cow, cow); | 
 | 599 | 	return 1; | 
 | 600 | } | 
 | 601 |  | 
 | 602 | static int open_ubd_file(char *file, struct openflags *openflags, int shared, | 
 | 603 | 		  char **backing_file_out, int *bitmap_offset_out, | 
 | 604 | 		  unsigned long *bitmap_len_out, int *data_offset_out, | 
 | 605 | 		  int *create_cow_out) | 
 | 606 | { | 
 | 607 | 	time_t mtime; | 
 | 608 | 	unsigned long long size; | 
 | 609 | 	__u32 version, align; | 
 | 610 | 	char *backing_file; | 
 | 611 | 	int fd, err, sectorsize, asked_switch, mode = 0644; | 
 | 612 |  | 
 | 613 | 	fd = os_open_file(file, *openflags, mode); | 
 | 614 | 	if (fd < 0) { | 
 | 615 | 		if ((fd == -ENOENT) && (create_cow_out != NULL)) | 
 | 616 | 			*create_cow_out = 1; | 
 | 617 | 		if (!openflags->w || | 
 | 618 | 		    ((fd != -EROFS) && (fd != -EACCES))) | 
 | 619 | 			return fd; | 
 | 620 | 		openflags->w = 0; | 
 | 621 | 		fd = os_open_file(file, *openflags, mode); | 
 | 622 | 		if (fd < 0) | 
 | 623 | 			return fd; | 
 | 624 | 	} | 
 | 625 |  | 
 | 626 | 	if (shared) | 
 | 627 | 		printk(KERN_INFO "Not locking \"%s\" on the host\n", file); | 
 | 628 | 	else { | 
 | 629 | 		err = os_lock_file(fd, openflags->w); | 
 | 630 | 		if (err < 0) { | 
 | 631 | 			printk(KERN_ERR "Failed to lock '%s', err = %d\n", | 
 | 632 | 			       file, -err); | 
 | 633 | 			goto out_close; | 
 | 634 | 		} | 
 | 635 | 	} | 
 | 636 |  | 
 | 637 | 	/* Successful return case! */ | 
 | 638 | 	if (backing_file_out == NULL) | 
 | 639 | 		return fd; | 
 | 640 |  | 
 | 641 | 	err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime, | 
 | 642 | 			      &size, §orsize, &align, bitmap_offset_out); | 
 | 643 | 	if (err && (*backing_file_out != NULL)) { | 
 | 644 | 		printk(KERN_ERR "Failed to read COW header from COW file " | 
 | 645 | 		       "\"%s\", errno = %d\n", file, -err); | 
 | 646 | 		goto out_close; | 
 | 647 | 	} | 
 | 648 | 	if (err) | 
 | 649 | 		return fd; | 
 | 650 |  | 
 | 651 | 	asked_switch = path_requires_switch(*backing_file_out, backing_file, | 
 | 652 | 					    file); | 
 | 653 |  | 
 | 654 | 	/* Allow switching only if no mismatch. */ | 
 | 655 | 	if (asked_switch && !backing_file_mismatch(*backing_file_out, size, | 
 | 656 | 						   mtime)) { | 
 | 657 | 		printk(KERN_ERR "Switching backing file to '%s'\n", | 
 | 658 | 		       *backing_file_out); | 
 | 659 | 		err = write_cow_header(file, fd, *backing_file_out, | 
 | 660 | 				       sectorsize, align, &size); | 
 | 661 | 		if (err) { | 
 | 662 | 			printk(KERN_ERR "Switch failed, errno = %d\n", -err); | 
 | 663 | 			goto out_close; | 
 | 664 | 		} | 
 | 665 | 	} else { | 
 | 666 | 		*backing_file_out = backing_file; | 
 | 667 | 		err = backing_file_mismatch(*backing_file_out, size, mtime); | 
 | 668 | 		if (err) | 
 | 669 | 			goto out_close; | 
 | 670 | 	} | 
 | 671 |  | 
 | 672 | 	cow_sizes(version, size, sectorsize, align, *bitmap_offset_out, | 
 | 673 | 		  bitmap_len_out, data_offset_out); | 
 | 674 |  | 
 | 675 | 	return fd; | 
 | 676 |  out_close: | 
 | 677 | 	os_close_file(fd); | 
 | 678 | 	return err; | 
 | 679 | } | 
 | 680 |  | 
 | 681 | static int create_cow_file(char *cow_file, char *backing_file, | 
 | 682 | 		    struct openflags flags, | 
 | 683 | 		    int sectorsize, int alignment, int *bitmap_offset_out, | 
 | 684 | 		    unsigned long *bitmap_len_out, int *data_offset_out) | 
 | 685 | { | 
 | 686 | 	int err, fd; | 
 | 687 |  | 
 | 688 | 	flags.c = 1; | 
 | 689 | 	fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL); | 
 | 690 | 	if (fd < 0) { | 
 | 691 | 		err = fd; | 
 | 692 | 		printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n", | 
 | 693 | 		       cow_file, -err); | 
 | 694 | 		goto out; | 
 | 695 | 	} | 
 | 696 |  | 
 | 697 | 	err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment, | 
 | 698 | 			    bitmap_offset_out, bitmap_len_out, | 
 | 699 | 			    data_offset_out); | 
 | 700 | 	if (!err) | 
 | 701 | 		return fd; | 
 | 702 | 	os_close_file(fd); | 
 | 703 |  out: | 
 | 704 | 	return err; | 
 | 705 | } | 
 | 706 |  | 
| Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 707 | static void ubd_close_dev(struct ubd *ubd_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 709 | 	os_close_file(ubd_dev->fd); | 
 | 710 | 	if(ubd_dev->cow.file == NULL) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | 		return; | 
 | 712 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 713 | 	os_close_file(ubd_dev->cow.fd); | 
 | 714 | 	vfree(ubd_dev->cow.bitmap); | 
 | 715 | 	ubd_dev->cow.bitmap = NULL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 716 | } | 
 | 717 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 718 | static int ubd_open_dev(struct ubd *ubd_dev) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | { | 
 | 720 | 	struct openflags flags; | 
 | 721 | 	char **back_ptr; | 
 | 722 | 	int err, create_cow, *create_ptr; | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 723 | 	int fd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 724 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 725 | 	ubd_dev->openflags = ubd_dev->boot_openflags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | 	create_cow = 0; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 727 | 	create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL; | 
 | 728 | 	back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file; | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 729 |  | 
 | 730 | 	fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared, | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 731 | 				back_ptr, &ubd_dev->cow.bitmap_offset, | 
 | 732 | 				&ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset, | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 733 | 				create_ptr); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 734 |  | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 735 | 	if((fd == -ENOENT) && create_cow){ | 
 | 736 | 		fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file, | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 737 | 					  ubd_dev->openflags, 1 << 9, PAGE_SIZE, | 
 | 738 | 					  &ubd_dev->cow.bitmap_offset, | 
 | 739 | 					  &ubd_dev->cow.bitmap_len, | 
 | 740 | 					  &ubd_dev->cow.data_offset); | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 741 | 		if(fd >= 0){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | 			printk(KERN_INFO "Creating \"%s\" as COW file for " | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 743 | 			       "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | 		} | 
 | 745 | 	} | 
 | 746 |  | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 747 | 	if(fd < 0){ | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 748 | 		printk("Failed to open '%s', errno = %d\n", ubd_dev->file, | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 749 | 		       -fd); | 
 | 750 | 		return fd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | 0bf16bf | 2006-10-30 22:07:11 -0800 | [diff] [blame] | 752 | 	ubd_dev->fd = fd; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 754 | 	if(ubd_dev->cow.file != NULL){ | 
| Martin K. Petersen | 086fa5f | 2010-02-26 00:20:38 -0500 | [diff] [blame] | 755 | 		blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long)); | 
| Jeff Dike | f4768ff | 2007-08-22 14:01:53 -0700 | [diff] [blame] | 756 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | 		err = -ENOMEM; | 
| Jesper Juhl | da2486b | 2007-10-16 01:27:19 -0700 | [diff] [blame] | 758 | 		ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len); | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 759 | 		if(ubd_dev->cow.bitmap == NULL){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | 			printk(KERN_ERR "Failed to vmalloc COW bitmap\n"); | 
 | 761 | 			goto error; | 
 | 762 | 		} | 
 | 763 | 		flush_tlb_kernel_vm(); | 
 | 764 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 765 | 		err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap, | 
 | 766 | 				      ubd_dev->cow.bitmap_offset, | 
 | 767 | 				      ubd_dev->cow.bitmap_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | 		if(err < 0) | 
 | 769 | 			goto error; | 
 | 770 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 771 | 		flags = ubd_dev->openflags; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | 		flags.w = 0; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 773 | 		err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL, | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 774 | 				    NULL, NULL, NULL, NULL); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | 		if(err < 0) goto error; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 776 | 		ubd_dev->cow.fd = err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | 	} | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 778 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 779 |  error: | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 780 | 	os_close_file(ubd_dev->fd); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 781 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | } | 
 | 783 |  | 
| Jeff Dike | 2e3f525 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 784 | static void ubd_device_release(struct device *dev) | 
 | 785 | { | 
| Greg Kroah-Hartman | 8691b97 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 786 | 	struct ubd *ubd_dev = dev_get_drvdata(dev); | 
| Jeff Dike | 2e3f525 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 787 |  | 
 | 788 | 	blk_cleanup_queue(ubd_dev->queue); | 
 | 789 | 	*ubd_dev = ((struct ubd) DEFAULT_UBD); | 
 | 790 | } | 
 | 791 |  | 
| Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 792 | static int ubd_disk_register(int major, u64 size, int unit, | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 793 | 			     struct gendisk **disk_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | { | 
 | 795 | 	struct gendisk *disk; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 |  | 
 | 797 | 	disk = alloc_disk(1 << UBD_SHIFT); | 
 | 798 | 	if(disk == NULL) | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 799 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 |  | 
 | 801 | 	disk->major = major; | 
 | 802 | 	disk->first_minor = unit << UBD_SHIFT; | 
 | 803 | 	disk->fops = &ubd_blops; | 
 | 804 | 	set_capacity(disk, size / 512); | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 805 | 	if (major == UBD_MAJOR) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | 		sprintf(disk->disk_name, "ubd%c", 'a' + unit); | 
| Greg Kroah-Hartman | ce7b0f4 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 807 | 	else | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | 		sprintf(disk->disk_name, "ubd_fake%d", unit); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 |  | 
 | 810 | 	/* sysfs register (not for ide fake devices) */ | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 811 | 	if (major == UBD_MAJOR) { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 812 | 		ubd_devs[unit].pdev.id   = unit; | 
 | 813 | 		ubd_devs[unit].pdev.name = DRIVER_NAME; | 
| Jeff Dike | 2e3f525 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 814 | 		ubd_devs[unit].pdev.dev.release = ubd_device_release; | 
| Greg Kroah-Hartman | 8691b97 | 2009-05-04 12:40:54 -0700 | [diff] [blame] | 815 | 		dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]); | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 816 | 		platform_device_register(&ubd_devs[unit].pdev); | 
 | 817 | 		disk->driverfs_dev = &ubd_devs[unit].pdev.dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 818 | 	} | 
 | 819 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 820 | 	disk->private_data = &ubd_devs[unit]; | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 821 | 	disk->queue = ubd_devs[unit].queue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 822 | 	add_disk(disk); | 
 | 823 |  | 
 | 824 | 	*disk_out = disk; | 
 | 825 | 	return 0; | 
 | 826 | } | 
 | 827 |  | 
 | 828 | #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9)) | 
 | 829 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 830 | static int ubd_add(int n, char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 832 | 	struct ubd *ubd_dev = &ubd_devs[n]; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 833 | 	int err = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 835 | 	if(ubd_dev->file == NULL) | 
| Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 836 | 		goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 837 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 838 | 	err = ubd_file_size(ubd_dev, &ubd_dev->size); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 839 | 	if(err < 0){ | 
 | 840 | 		*error_out = "Couldn't determine size of device's file"; | 
| Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame] | 841 | 		goto out; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 842 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 843 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 844 | 	ubd_dev->size = ROUND_BLOCK(ubd_dev->size); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 |  | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 846 | 	INIT_LIST_HEAD(&ubd_dev->restart); | 
| WANG Cong | 4f40c05 | 2007-11-05 14:50:59 -0800 | [diff] [blame] | 847 | 	sg_init_table(ubd_dev->sg, MAX_SG); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 848 |  | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 849 | 	err = -ENOMEM; | 
 | 850 | 	ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock); | 
 | 851 | 	if (ubd_dev->queue == NULL) { | 
 | 852 | 		*error_out = "Failed to initialize device queue"; | 
| Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame] | 853 | 		goto out; | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 854 | 	} | 
 | 855 | 	ubd_dev->queue->queuedata = ubd_dev; | 
 | 856 |  | 
| Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 857 | 	blk_queue_max_segments(ubd_dev->queue, MAX_SG); | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 858 | 	err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]); | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 859 | 	if(err){ | 
 | 860 | 		*error_out = "Failed to register device"; | 
 | 861 | 		goto out_cleanup; | 
 | 862 | 	} | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 863 |  | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 864 | 	if (fake_major != UBD_MAJOR) | 
| Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 865 | 		ubd_disk_register(fake_major, ubd_dev->size, n, | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 866 | 				  &fake_gendisk[n]); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 867 |  | 
| Jeff Dike | 83380cc | 2008-02-04 22:31:18 -0800 | [diff] [blame] | 868 | 	/* | 
 | 869 | 	 * Perhaps this should also be under the "if (fake_major)" above | 
 | 870 | 	 * using the fake_disk->disk_name | 
 | 871 | 	 */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 872 | 	if (fake_ide) | 
 | 873 | 		make_ide_entries(ubd_gendisk[n]->disk_name); | 
 | 874 |  | 
| Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 875 | 	err = 0; | 
| Jeff Dike | ec7cf78 | 2005-09-03 15:57:29 -0700 | [diff] [blame] | 876 | out: | 
 | 877 | 	return err; | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 878 |  | 
 | 879 | out_cleanup: | 
 | 880 | 	blk_cleanup_queue(ubd_dev->queue); | 
 | 881 | 	goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 882 | } | 
 | 883 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 884 | static int ubd_config(char *str, char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 885 | { | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 886 | 	int n, ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 887 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 888 | 	/* This string is possibly broken up and stored, so it's only | 
 | 889 | 	 * freed if ubd_setup_common fails, or if only general options | 
 | 890 | 	 * were set. | 
 | 891 | 	 */ | 
| Jeff Dike | 970d6e3 | 2006-01-06 00:18:48 -0800 | [diff] [blame] | 892 | 	str = kstrdup(str, GFP_KERNEL); | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 893 | 	if (str == NULL) { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 894 | 		*error_out = "Failed to allocate memory"; | 
 | 895 | 		return -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 896 | 	} | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 897 |  | 
 | 898 | 	ret = ubd_setup_common(str, &n, error_out); | 
 | 899 | 	if (ret) | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 900 | 		goto err_free; | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 901 |  | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 902 | 	if (n == -1) { | 
 | 903 | 		ret = 0; | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 904 | 		goto err_free; | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 905 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 906 |  | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 907 | 	mutex_lock(&ubd_lock); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 908 | 	ret = ubd_add(n, error_out); | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 909 | 	if (ret) | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 910 | 		ubd_devs[n].file = NULL; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 911 | 	mutex_unlock(&ubd_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 |  | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 913 | out: | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 914 | 	return ret; | 
| Paolo 'Blaisorblade' Giarrusso | e7f6552 | 2006-10-30 22:07:09 -0800 | [diff] [blame] | 915 |  | 
 | 916 | err_free: | 
 | 917 | 	kfree(str); | 
 | 918 | 	goto out; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | } | 
 | 920 |  | 
 | 921 | static int ubd_get_config(char *name, char *str, int size, char **error_out) | 
 | 922 | { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 923 | 	struct ubd *ubd_dev; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | 	int n, len = 0; | 
 | 925 |  | 
 | 926 | 	n = parse_unit(&name); | 
 | 927 | 	if((n >= MAX_DEV) || (n < 0)){ | 
 | 928 | 		*error_out = "ubd_get_config : device number out of range"; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 929 | 		return -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | 	} | 
 | 931 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 932 | 	ubd_dev = &ubd_devs[n]; | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 933 | 	mutex_lock(&ubd_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 935 | 	if(ubd_dev->file == NULL){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | 		CONFIG_CHUNK(str, size, len, "", 1); | 
 | 937 | 		goto out; | 
 | 938 | 	} | 
 | 939 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 940 | 	CONFIG_CHUNK(str, size, len, ubd_dev->file, 0); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 941 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 942 | 	if(ubd_dev->cow.file != NULL){ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 943 | 		CONFIG_CHUNK(str, size, len, ",", 0); | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 944 | 		CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | 	} | 
 | 946 | 	else CONFIG_CHUNK(str, size, len, "", 1); | 
 | 947 |  | 
 | 948 |  out: | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 949 | 	mutex_unlock(&ubd_lock); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 950 | 	return len; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } | 
 | 952 |  | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 953 | static int ubd_id(char **str, int *start_out, int *end_out) | 
 | 954 | { | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 955 | 	int n; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 956 |  | 
 | 957 | 	n = parse_unit(str); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 958 | 	*start_out = 0; | 
 | 959 | 	*end_out = MAX_DEV - 1; | 
 | 960 | 	return n; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 961 | } | 
 | 962 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 963 | static int ubd_remove(int n, char **error_out) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | { | 
| Jeff Dike | 2e3f525 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 965 | 	struct gendisk *disk = ubd_gendisk[n]; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 966 | 	struct ubd *ubd_dev; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 967 | 	int err = -ENODEV; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 |  | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 969 | 	mutex_lock(&ubd_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 971 | 	ubd_dev = &ubd_devs[n]; | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 972 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 973 | 	if(ubd_dev->file == NULL) | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 974 | 		goto out; | 
 | 975 |  | 
 | 976 | 	/* you cannot remove a open disk */ | 
 | 977 | 	err = -EBUSY; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 978 | 	if(ubd_dev->count > 0) | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 979 | 		goto out; | 
 | 980 |  | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 981 | 	ubd_gendisk[n] = NULL; | 
| Jeff Dike | b47d2de | 2007-05-06 14:51:01 -0700 | [diff] [blame] | 982 | 	if(disk != NULL){ | 
 | 983 | 		del_gendisk(disk); | 
 | 984 | 		put_disk(disk); | 
 | 985 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 |  | 
 | 987 | 	if(fake_gendisk[n] != NULL){ | 
 | 988 | 		del_gendisk(fake_gendisk[n]); | 
 | 989 | 		put_disk(fake_gendisk[n]); | 
 | 990 | 		fake_gendisk[n] = NULL; | 
 | 991 | 	} | 
 | 992 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 993 | 	err = 0; | 
| Jeff Dike | 2e3f525 | 2007-05-06 14:51:29 -0700 | [diff] [blame] | 994 | 	platform_device_unregister(&ubd_dev->pdev); | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 995 | out: | 
| Paolo 'Blaisorblade' Giarrusso | d7fb2c3 | 2006-10-30 22:07:07 -0800 | [diff] [blame] | 996 | 	mutex_unlock(&ubd_lock); | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 997 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | } | 
 | 999 |  | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 1000 | /* All these are called by mconsole in process context and without | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 1001 |  * ubd-specific locks.  The structure itself is const except for .list. | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 1002 |  */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1003 | static struct mc_device ubd_mc = { | 
| Jeff Dike | 84f48d4 | 2007-02-10 01:44:01 -0800 | [diff] [blame] | 1004 | 	.list		= LIST_HEAD_INIT(ubd_mc.list), | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1005 | 	.name		= "ubd", | 
 | 1006 | 	.config		= ubd_config, | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1007 | 	.get_config	= ubd_get_config, | 
| Jeff Dike | 29d56cf | 2005-06-25 14:55:25 -0700 | [diff] [blame] | 1008 | 	.id		= ubd_id, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | 	.remove		= ubd_remove, | 
 | 1010 | }; | 
 | 1011 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1012 | static int __init ubd_mc_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | { | 
 | 1014 | 	mconsole_register_dev(&ubd_mc); | 
 | 1015 | 	return 0; | 
 | 1016 | } | 
 | 1017 |  | 
 | 1018 | __initcall(ubd_mc_init); | 
 | 1019 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1020 | static int __init ubd0_init(void) | 
 | 1021 | { | 
 | 1022 | 	struct ubd *ubd_dev = &ubd_devs[0]; | 
 | 1023 |  | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 1024 | 	mutex_lock(&ubd_lock); | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1025 | 	if(ubd_dev->file == NULL) | 
 | 1026 | 		ubd_dev->file = "root_fs"; | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 1027 | 	mutex_unlock(&ubd_lock); | 
 | 1028 |  | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1029 | 	return 0; | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1030 | } | 
 | 1031 |  | 
 | 1032 | __initcall(ubd0_init); | 
 | 1033 |  | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 1034 | /* Used in ubd_init, which is an initcall */ | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1035 | static struct platform_driver ubd_driver = { | 
 | 1036 | 	.driver = { | 
 | 1037 | 		.name  = DRIVER_NAME, | 
 | 1038 | 	}, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | }; | 
 | 1040 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1041 | static int __init ubd_init(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1042 | { | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 1043 | 	char *error; | 
 | 1044 | 	int i, err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1045 |  | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 1046 | 	if (register_blkdev(UBD_MAJOR, "ubd")) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | 		return -1; | 
 | 1048 |  | 
| Christoph Hellwig | 792dd4f | 2009-03-31 15:23:39 -0700 | [diff] [blame] | 1049 | 	if (fake_major != UBD_MAJOR) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | 		char name[sizeof("ubd_nnn\0")]; | 
 | 1051 |  | 
 | 1052 | 		snprintf(name, sizeof(name), "ubd_%d", fake_major); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | 		if (register_blkdev(fake_major, "ubd")) | 
 | 1054 | 			return -1; | 
 | 1055 | 	} | 
| Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 1056 | 	platform_driver_register(&ubd_driver); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1057 | 	mutex_lock(&ubd_lock); | 
| Jeff Dike | f28169d | 2007-02-10 01:43:53 -0800 | [diff] [blame] | 1058 | 	for (i = 0; i < MAX_DEV; i++){ | 
 | 1059 | 		err = ubd_add(i, &error); | 
 | 1060 | 		if(err) | 
 | 1061 | 			printk(KERN_ERR "Failed to initialize ubd device %d :" | 
 | 1062 | 			       "%s\n", i, error); | 
 | 1063 | 	} | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1064 | 	mutex_unlock(&ubd_lock); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | 	return 0; | 
 | 1066 | } | 
 | 1067 |  | 
 | 1068 | late_initcall(ubd_init); | 
 | 1069 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1070 | static int __init ubd_driver_init(void){ | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1071 | 	unsigned long stack; | 
 | 1072 | 	int err; | 
 | 1073 |  | 
 | 1074 | 	/* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/ | 
 | 1075 | 	if(global_openflags.s){ | 
 | 1076 | 		printk(KERN_INFO "ubd: Synchronous mode\n"); | 
 | 1077 | 		/* Letting ubd=sync be like using ubd#s= instead of ubd#= is | 
 | 1078 | 		 * enough. So use anyway the io thread. */ | 
 | 1079 | 	} | 
 | 1080 | 	stack = alloc_stack(0, 0); | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1081 | 	io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *), | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1082 | 				 &thread_fd); | 
 | 1083 | 	if(io_pid < 0){ | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1084 | 		printk(KERN_ERR | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1085 | 		       "ubd : Failed to start I/O thread (errno = %d) - " | 
 | 1086 | 		       "falling back to synchronous I/O\n", -io_pid); | 
 | 1087 | 		io_pid = -1; | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1088 | 		return 0; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1089 | 	} | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1090 | 	err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr, | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1091 | 			     IRQF_DISABLED, "ubd", ubd_devs); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1092 | 	if(err != 0) | 
 | 1093 | 		printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err); | 
| Jeff Dike | f4c57a7 | 2006-03-31 02:30:10 -0800 | [diff] [blame] | 1094 | 	return 0; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1095 | } | 
 | 1096 |  | 
 | 1097 | device_initcall(ubd_driver_init); | 
 | 1098 |  | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 1099 | static int ubd_open(struct block_device *bdev, fmode_t mode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 1101 | 	struct gendisk *disk = bdev->bd_disk; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1102 | 	struct ubd *ubd_dev = disk->private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1103 | 	int err = 0; | 
 | 1104 |  | 
| Arnd Bergmann | 9a181c5 | 2010-09-11 18:38:03 +0200 | [diff] [blame] | 1105 | 	mutex_lock(&ubd_mutex); | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1106 | 	if(ubd_dev->count == 0){ | 
 | 1107 | 		err = ubd_open_dev(ubd_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1108 | 		if(err){ | 
 | 1109 | 			printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n", | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1110 | 			       disk->disk_name, ubd_dev->file, -err); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | 			goto out; | 
 | 1112 | 		} | 
 | 1113 | 	} | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1114 | 	ubd_dev->count++; | 
 | 1115 | 	set_disk_ro(disk, !ubd_dev->openflags.w); | 
| Paolo 'Blaisorblade' Giarrusso | 2c49be9 | 2005-05-01 08:58:57 -0700 | [diff] [blame] | 1116 |  | 
 | 1117 | 	/* This should no more be needed. And it didn't work anyway to exclude | 
 | 1118 | 	 * read-write remounting of filesystems.*/ | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 1119 | 	/*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){ | 
| Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 1120 | 	        if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | 	        err = -EROFS; | 
| Paolo 'Blaisorblade' Giarrusso | 2c49be9 | 2005-05-01 08:58:57 -0700 | [diff] [blame] | 1122 | 	}*/ | 
| Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1123 | out: | 
| Arnd Bergmann | 9a181c5 | 2010-09-11 18:38:03 +0200 | [diff] [blame] | 1124 | 	mutex_unlock(&ubd_mutex); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1125 | 	return err; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | } | 
 | 1127 |  | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 1128 | static int ubd_release(struct gendisk *disk, fmode_t mode) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1130 | 	struct ubd *ubd_dev = disk->private_data; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 |  | 
| Arnd Bergmann | 9a181c5 | 2010-09-11 18:38:03 +0200 | [diff] [blame] | 1132 | 	mutex_lock(&ubd_mutex); | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1133 | 	if(--ubd_dev->count == 0) | 
| Paolo 'Blaisorblade' Giarrusso | 5f75a4f | 2006-10-30 22:07:06 -0800 | [diff] [blame] | 1134 | 		ubd_close_dev(ubd_dev); | 
| Arnd Bergmann | 9a181c5 | 2010-09-11 18:38:03 +0200 | [diff] [blame] | 1135 | 	mutex_unlock(&ubd_mutex); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1136 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | } | 
 | 1138 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1139 | static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask, | 
 | 1140 | 			  __u64 *cow_offset, unsigned long *bitmap, | 
 | 1141 | 			  __u64 bitmap_offset, unsigned long *bitmap_words, | 
 | 1142 | 			  __u64 bitmap_len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | { | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1144 | 	__u64 sector = io_offset >> 9; | 
 | 1145 | 	int i, update_bitmap = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1146 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1147 | 	for(i = 0; i < length >> 9; i++){ | 
 | 1148 | 		if(cow_mask != NULL) | 
 | 1149 | 			ubd_set_bit(i, (unsigned char *) cow_mask); | 
 | 1150 | 		if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) | 
 | 1151 | 			continue; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1153 | 		update_bitmap = 1; | 
 | 1154 | 		ubd_set_bit(sector + i, (unsigned char *) bitmap); | 
 | 1155 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1156 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1157 | 	if(!update_bitmap) | 
 | 1158 | 		return; | 
 | 1159 |  | 
 | 1160 | 	*cow_offset = sector / (sizeof(unsigned long) * 8); | 
 | 1161 |  | 
 | 1162 | 	/* This takes care of the case where we're exactly at the end of the | 
 | 1163 | 	 * device, and *cow_offset + 1 is off the end.  So, just back it up | 
 | 1164 | 	 * by one word.  Thanks to Lynn Kerby for the fix and James McMechan | 
 | 1165 | 	 * for the original diagnosis. | 
 | 1166 | 	 */ | 
| Jiri Olsa | 6d07424 | 2008-05-12 14:01:56 -0700 | [diff] [blame] | 1167 | 	if (*cow_offset == (DIV_ROUND_UP(bitmap_len, | 
 | 1168 | 					 sizeof(unsigned long)) - 1)) | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1169 | 		(*cow_offset)--; | 
 | 1170 |  | 
 | 1171 | 	bitmap_words[0] = bitmap[*cow_offset]; | 
 | 1172 | 	bitmap_words[1] = bitmap[*cow_offset + 1]; | 
 | 1173 |  | 
 | 1174 | 	*cow_offset *= sizeof(unsigned long); | 
 | 1175 | 	*cow_offset += bitmap_offset; | 
 | 1176 | } | 
 | 1177 |  | 
 | 1178 | static void cowify_req(struct io_thread_req *req, unsigned long *bitmap, | 
 | 1179 | 		       __u64 bitmap_offset, __u64 bitmap_len) | 
 | 1180 | { | 
 | 1181 | 	__u64 sector = req->offset >> 9; | 
 | 1182 | 	int i; | 
 | 1183 |  | 
 | 1184 | 	if(req->length > (sizeof(req->sector_mask) * 8) << 9) | 
 | 1185 | 		panic("Operation too long"); | 
 | 1186 |  | 
 | 1187 | 	if(req->op == UBD_READ) { | 
 | 1188 | 		for(i = 0; i < req->length >> 9; i++){ | 
 | 1189 | 			if(ubd_test_bit(sector + i, (unsigned char *) bitmap)) | 
| Jeff Dike | 6c29256c | 2006-03-27 01:14:37 -0800 | [diff] [blame] | 1190 | 				ubd_set_bit(i, (unsigned char *) | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1191 | 					    &req->sector_mask); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1192 | 		} | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1193 | 	} | 
 | 1194 | 	else cowify_bitmap(req->offset, req->length, &req->sector_mask, | 
 | 1195 | 			   &req->cow_offset, bitmap, bitmap_offset, | 
 | 1196 | 			   req->bitmap_words, bitmap_len); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1197 | } | 
 | 1198 |  | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 1199 | /* Called with dev->lock held */ | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1200 | static void prepare_request(struct request *req, struct io_thread_req *io_req, | 
 | 1201 | 			    unsigned long long offset, int page_offset, | 
 | 1202 | 			    int len, struct page *page) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | { | 
 | 1204 | 	struct gendisk *disk = req->rq_disk; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1205 | 	struct ubd *ubd_dev = disk->private_data; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1206 |  | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 1207 | 	io_req->req = req; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1208 | 	io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd : | 
 | 1209 | 		ubd_dev->fd; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1210 | 	io_req->fds[1] = ubd_dev->fd; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1211 | 	io_req->cow_offset = -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | 	io_req->offset = offset; | 
 | 1213 | 	io_req->length = len; | 
 | 1214 | 	io_req->error = 0; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1215 | 	io_req->sector_mask = 0; | 
 | 1216 |  | 
 | 1217 | 	io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | 	io_req->offsets[0] = 0; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1219 | 	io_req->offsets[1] = ubd_dev->cow.data_offset; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1220 | 	io_req->buffer = page_address(page) + page_offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | 	io_req->sectorsize = 1 << 9; | 
 | 1222 |  | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1223 | 	if(ubd_dev->cow.file != NULL) | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1224 | 		cowify_req(io_req, ubd_dev->cow.bitmap, | 
 | 1225 | 			   ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1226 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | } | 
 | 1228 |  | 
| Jeff Dike | 62f96cb | 2007-02-10 01:44:16 -0800 | [diff] [blame] | 1229 | /* Called with dev->lock held */ | 
| Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 1230 | static void do_ubd_request(struct request_queue *q) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | { | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1232 | 	struct io_thread_req *io_req; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | 	struct request *req; | 
| Tejun Heo | f81f2f7 | 2009-04-28 13:06:10 +0900 | [diff] [blame] | 1234 | 	int n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 |  | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1236 | 	while(1){ | 
| Jeff Dike | 2a9529a | 2007-03-29 01:20:27 -0700 | [diff] [blame] | 1237 | 		struct ubd *dev = q->queuedata; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1238 | 		if(dev->end_sg == 0){ | 
| Tejun Heo | 9934c8c | 2009-05-08 11:54:16 +0900 | [diff] [blame] | 1239 | 			struct request *req = blk_fetch_request(q); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1240 | 			if(req == NULL) | 
 | 1241 | 				return; | 
 | 1242 |  | 
 | 1243 | 			dev->request = req; | 
| Tejun Heo | 4752690 | 2010-10-15 12:56:21 +0200 | [diff] [blame] | 1244 | 			dev->rq_pos = blk_rq_pos(req); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1245 | 			dev->start_sg = 0; | 
 | 1246 | 			dev->end_sg = blk_rq_map_sg(q, req, dev->sg); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1247 | 		} | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1248 |  | 
 | 1249 | 		req = dev->request; | 
 | 1250 | 		while(dev->start_sg < dev->end_sg){ | 
 | 1251 | 			struct scatterlist *sg = &dev->sg[dev->start_sg]; | 
 | 1252 |  | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1253 | 			io_req = kmalloc(sizeof(struct io_thread_req), | 
| Peter Zijlstra | 990c558 | 2007-05-06 14:51:38 -0700 | [diff] [blame] | 1254 | 					 GFP_ATOMIC); | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1255 | 			if(io_req == NULL){ | 
 | 1256 | 				if(list_empty(&dev->restart)) | 
 | 1257 | 					list_add(&dev->restart, &restart); | 
 | 1258 | 				return; | 
 | 1259 | 			} | 
 | 1260 | 			prepare_request(req, io_req, | 
| Tejun Heo | 4752690 | 2010-10-15 12:56:21 +0200 | [diff] [blame] | 1261 | 					(unsigned long long)dev->rq_pos << 9, | 
| Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 1262 | 					sg->offset, sg->length, sg_page(sg)); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1263 |  | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 1264 | 			n = os_write_file(thread_fd, &io_req, | 
 | 1265 | 					  sizeof(struct io_thread_req *)); | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1266 | 			if(n != sizeof(struct io_thread_req *)){ | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1267 | 				if(n != -EAGAIN) | 
 | 1268 | 					printk("write to io thread failed, " | 
 | 1269 | 					       "errno = %d\n", -n); | 
 | 1270 | 				else if(list_empty(&dev->restart)) | 
 | 1271 | 					list_add(&dev->restart, &restart); | 
| Miklos Szeredi | 12429bf | 2007-11-28 16:21:52 -0800 | [diff] [blame] | 1272 | 				kfree(io_req); | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1273 | 				return; | 
 | 1274 | 			} | 
 | 1275 |  | 
| Tejun Heo | 4752690 | 2010-10-15 12:56:21 +0200 | [diff] [blame] | 1276 | 			dev->rq_pos += sg->length >> 9; | 
| Jeff Dike | a0044bd | 2007-05-06 14:51:36 -0700 | [diff] [blame] | 1277 | 			dev->start_sg++; | 
 | 1278 | 		} | 
 | 1279 | 		dev->end_sg = 0; | 
 | 1280 | 		dev->request = NULL; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1281 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | } | 
 | 1283 |  | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1284 | static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo) | 
 | 1285 | { | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1286 | 	struct ubd *ubd_dev = bdev->bd_disk->private_data; | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1287 |  | 
 | 1288 | 	geo->heads = 128; | 
 | 1289 | 	geo->sectors = 32; | 
| Paolo 'Blaisorblade' Giarrusso | 7d314e3 | 2006-10-30 22:07:05 -0800 | [diff] [blame] | 1290 | 	geo->cylinders = ubd_dev->size / (128 * 32 * 512); | 
| Christoph Hellwig | a885c8c | 2006-01-08 01:02:50 -0800 | [diff] [blame] | 1291 | 	return 0; | 
 | 1292 | } | 
 | 1293 |  | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 1294 | static int ubd_ioctl(struct block_device *bdev, fmode_t mode, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | 		     unsigned int cmd, unsigned long arg) | 
 | 1296 | { | 
| Al Viro | a625c99 | 2008-03-02 09:16:26 -0500 | [diff] [blame] | 1297 | 	struct ubd *ubd_dev = bdev->bd_disk->private_data; | 
| Bartlomiej Zolnierkiewicz | 73855e1 | 2009-04-01 21:42:21 +0200 | [diff] [blame] | 1298 | 	u16 ubd_id[ATA_ID_WORDS]; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 |  | 
 | 1300 | 	switch (cmd) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | 		struct cdrom_volctrl volume; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | 	case HDIO_GET_IDENTITY: | 
| Bartlomiej Zolnierkiewicz | 73855e1 | 2009-04-01 21:42:21 +0200 | [diff] [blame] | 1303 | 		memset(&ubd_id, 0, ATA_ID_WORDS * 2); | 
 | 1304 | 		ubd_id[ATA_ID_CYLS]	= ubd_dev->size / (128 * 32 * 512); | 
 | 1305 | 		ubd_id[ATA_ID_HEADS]	= 128; | 
 | 1306 | 		ubd_id[ATA_ID_SECTORS]	= 32; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | 		if(copy_to_user((char __user *) arg, (char *) &ubd_id, | 
 | 1308 | 				 sizeof(ubd_id))) | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1309 | 			return -EFAULT; | 
 | 1310 | 		return 0; | 
| Jeff Dike | b8831a1 | 2007-02-10 01:44:17 -0800 | [diff] [blame] | 1311 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | 	case CDROMVOLREAD: | 
 | 1313 | 		if(copy_from_user(&volume, (char __user *) arg, sizeof(volume))) | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1314 | 			return -EFAULT; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1315 | 		volume.channel0 = 255; | 
 | 1316 | 		volume.channel1 = 255; | 
 | 1317 | 		volume.channel2 = 255; | 
 | 1318 | 		volume.channel3 = 255; | 
 | 1319 | 		if(copy_to_user((char __user *) arg, &volume, sizeof(volume))) | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1320 | 			return -EFAULT; | 
 | 1321 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1322 | 	} | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1323 | 	return -EINVAL; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1324 | } | 
 | 1325 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1326 | static int update_bitmap(struct io_thread_req *req) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | { | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1328 | 	int n; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1329 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1330 | 	if(req->cow_offset == -1) | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1331 | 		return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1332 |  | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1333 | 	n = os_seek_file(req->fds[1], req->cow_offset); | 
 | 1334 | 	if(n < 0){ | 
 | 1335 | 		printk("do_io - bitmap lseek failed : err = %d\n", -n); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1336 | 		return 1; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1337 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 |  | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 1339 | 	n = os_write_file(req->fds[1], &req->bitmap_words, | 
 | 1340 | 			  sizeof(req->bitmap_words)); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1341 | 	if(n != sizeof(req->bitmap_words)){ | 
 | 1342 | 		printk("do_io - bitmap update failed, err = %d fd = %d\n", -n, | 
 | 1343 | 		       req->fds[1]); | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1344 | 		return 1; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1345 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 |  | 
| Jeff Dike | dc764e5 | 2007-05-06 14:51:41 -0700 | [diff] [blame] | 1347 | 	return 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1349 |  | 
| WANG Cong | 5dc62b1 | 2008-04-28 02:13:58 -0700 | [diff] [blame] | 1350 | static void do_io(struct io_thread_req *req) | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1351 | { | 
 | 1352 | 	char *buf; | 
 | 1353 | 	unsigned long len; | 
 | 1354 | 	int n, nsectors, start, end, bit; | 
 | 1355 | 	int err; | 
 | 1356 | 	__u64 off; | 
 | 1357 |  | 
 | 1358 | 	nsectors = req->length / req->sectorsize; | 
 | 1359 | 	start = 0; | 
 | 1360 | 	do { | 
 | 1361 | 		bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask); | 
 | 1362 | 		end = start; | 
 | 1363 | 		while((end < nsectors) && | 
 | 1364 | 		      (ubd_test_bit(end, (unsigned char *) | 
 | 1365 | 				    &req->sector_mask) == bit)) | 
 | 1366 | 			end++; | 
 | 1367 |  | 
 | 1368 | 		off = req->offset + req->offsets[bit] + | 
 | 1369 | 			start * req->sectorsize; | 
 | 1370 | 		len = (end - start) * req->sectorsize; | 
 | 1371 | 		buf = &req->buffer[start * req->sectorsize]; | 
 | 1372 |  | 
 | 1373 | 		err = os_seek_file(req->fds[bit], off); | 
 | 1374 | 		if(err < 0){ | 
 | 1375 | 			printk("do_io - lseek failed : err = %d\n", -err); | 
 | 1376 | 			req->error = 1; | 
 | 1377 | 			return; | 
 | 1378 | 		} | 
 | 1379 | 		if(req->op == UBD_READ){ | 
 | 1380 | 			n = 0; | 
 | 1381 | 			do { | 
 | 1382 | 				buf = &buf[n]; | 
 | 1383 | 				len -= n; | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 1384 | 				n = os_read_file(req->fds[bit], buf, len); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1385 | 				if (n < 0) { | 
 | 1386 | 					printk("do_io - read failed, err = %d " | 
 | 1387 | 					       "fd = %d\n", -n, req->fds[bit]); | 
 | 1388 | 					req->error = 1; | 
 | 1389 | 					return; | 
 | 1390 | 				} | 
 | 1391 | 			} while((n < len) && (n != 0)); | 
 | 1392 | 			if (n < len) memset(&buf[n], 0, len - n); | 
 | 1393 | 		} else { | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 1394 | 			n = os_write_file(req->fds[bit], buf, len); | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1395 | 			if(n != len){ | 
 | 1396 | 				printk("do_io - write failed err = %d " | 
 | 1397 | 				       "fd = %d\n", -n, req->fds[bit]); | 
 | 1398 | 				req->error = 1; | 
 | 1399 | 				return; | 
 | 1400 | 			} | 
 | 1401 | 		} | 
 | 1402 |  | 
 | 1403 | 		start = end; | 
 | 1404 | 	} while(start < nsectors); | 
 | 1405 |  | 
 | 1406 | 	req->error = update_bitmap(req); | 
 | 1407 | } | 
 | 1408 |  | 
 | 1409 | /* Changed in start_io_thread, which is serialized by being called only | 
 | 1410 |  * from ubd_init, which is an initcall. | 
 | 1411 |  */ | 
 | 1412 | int kernel_fd = -1; | 
 | 1413 |  | 
| Paolo 'Blaisorblade' Giarrusso | d8d7c28 | 2006-10-30 22:07:12 -0800 | [diff] [blame] | 1414 | /* Only changed by the io thread. XXX: currently unused. */ | 
 | 1415 | static int io_count = 0; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1416 |  | 
 | 1417 | int io_thread(void *arg) | 
 | 1418 | { | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1419 | 	struct io_thread_req *req; | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1420 | 	int n; | 
 | 1421 |  | 
 | 1422 | 	ignore_sigwinch_sig(); | 
 | 1423 | 	while(1){ | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 1424 | 		n = os_read_file(kernel_fd, &req, | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1425 | 				 sizeof(struct io_thread_req *)); | 
 | 1426 | 		if(n != sizeof(struct io_thread_req *)){ | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1427 | 			if(n < 0) | 
 | 1428 | 				printk("io_thread - read failed, fd = %d, " | 
 | 1429 | 				       "err = %d\n", kernel_fd, -n); | 
 | 1430 | 			else { | 
 | 1431 | 				printk("io_thread - short read, fd = %d, " | 
 | 1432 | 				       "length = %d\n", kernel_fd, n); | 
 | 1433 | 			} | 
 | 1434 | 			continue; | 
 | 1435 | 		} | 
 | 1436 | 		io_count++; | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1437 | 		do_io(req); | 
| Jeff Dike | a6ea4cc | 2007-05-06 14:51:43 -0700 | [diff] [blame] | 1438 | 		n = os_write_file(kernel_fd, &req, | 
| Jeff Dike | 2adcec2 | 2007-05-06 14:51:37 -0700 | [diff] [blame] | 1439 | 				  sizeof(struct io_thread_req *)); | 
 | 1440 | 		if(n != sizeof(struct io_thread_req *)) | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1441 | 			printk("io_thread - write failed, fd = %d, err = %d\n", | 
 | 1442 | 			       kernel_fd, -n); | 
 | 1443 | 	} | 
| Jeff Dike | 91acb21 | 2005-10-10 23:10:32 -0400 | [diff] [blame] | 1444 |  | 
| Jeff Dike | 1b57e9c | 2006-01-06 00:18:49 -0800 | [diff] [blame] | 1445 | 	return 0; | 
 | 1446 | } |