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