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