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