This commit was manufactured by cvs2svn to create tag
[linux-2.6.git] / arch / um / drivers / ubd_kern.c
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 MAJOR_NR UBD_MAJOR
21 #define UBD_SHIFT 4
22
23 #include "linux/config.h"
24 #include "linux/module.h"
25 #include "linux/blkdev.h"
26 #include "linux/hdreg.h"
27 #include "linux/init.h"
28 #include "linux/devfs_fs_kernel.h"
29 #include "linux/cdrom.h"
30 #include "linux/proc_fs.h"
31 #include "linux/ctype.h"
32 #include "linux/capability.h"
33 #include "linux/mm.h"
34 #include "linux/vmalloc.h"
35 #include "linux/blkpg.h"
36 #include "linux/genhd.h"
37 #include "linux/spinlock.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 "user_util.h"
44 #include "mem_user.h"
45 #include "kern_util.h"
46 #include "kern.h"
47 #include "mconsole_kern.h"
48 #include "init.h"
49 #include "irq_user.h"
50 #include "irq_kern.h"
51 #include "ubd_user.h"
52 #include "2_5compat.h"
53 #include "os.h"
54 #include "mem.h"
55 #include "mem_kern.h"
56
57 static spinlock_t ubd_io_lock = SPIN_LOCK_UNLOCKED;
58 static spinlock_t ubd_lock = SPIN_LOCK_UNLOCKED;
59
60 static void (*do_ubd)(void);
61
62 static int ubd_open(struct inode * inode, struct file * filp);
63 static int ubd_release(struct inode * inode, struct file * file);
64 static int ubd_ioctl(struct inode * inode, struct file * file,
65                      unsigned int cmd, unsigned long arg);
66
67 #define MAX_DEV (8)
68
69 /* Changed in early boot */
70 static int ubd_do_mmap = 0;
71 #define UBD_MMAP_BLOCK_SIZE PAGE_SIZE
72
73 static struct block_device_operations ubd_blops = {
74         .owner          = THIS_MODULE,
75         .open           = ubd_open,
76         .release        = ubd_release,
77         .ioctl          = ubd_ioctl,
78 };
79
80 /* Protected by the queue_lock */
81 static request_queue_t *ubd_queue;
82
83 /* Protected by ubd_lock */
84 static int fake_major = MAJOR_NR;
85
86 static struct gendisk *ubd_gendisk[MAX_DEV];
87 static struct gendisk *fake_gendisk[MAX_DEV];
88  
89 #ifdef CONFIG_BLK_DEV_UBD_SYNC
90 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
91                                          .cl = 1 })
92 #else
93 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
94                                          .cl = 1 })
95 #endif
96
97 /* Not protected - changed only in ubd_setup_common and then only to
98  * to enable O_SYNC.
99  */
100 static struct openflags global_openflags = OPEN_FLAGS;
101
102 struct cow {
103         char *file;
104         int fd;
105         unsigned long *bitmap;
106         unsigned long bitmap_len;
107         int bitmap_offset;
108         int data_offset;
109 };
110
111 struct ubd {
112         char *file;
113         int count;
114         int fd;
115         __u64 size;
116         struct openflags boot_openflags;
117         struct openflags openflags;
118         int no_cow;
119         struct cow cow;
120
121         int map_writes;
122         int map_reads;
123         int nomap_writes;
124         int nomap_reads;
125         int write_maps;
126 };
127
128 #define DEFAULT_COW { \
129         .file =                 NULL, \
130         .fd =                   -1, \
131         .bitmap =               NULL, \
132         .bitmap_offset =        0, \
133         .data_offset =          0, \
134 }
135
136 #define DEFAULT_UBD { \
137         .file =                 NULL, \
138         .count =                0, \
139         .fd =                   -1, \
140         .size =                 -1, \
141         .boot_openflags =       OPEN_FLAGS, \
142         .openflags =            OPEN_FLAGS, \
143         .no_cow =               0, \
144         .cow =                  DEFAULT_COW, \
145         .map_writes             = 0, \
146         .map_reads              = 0, \
147         .nomap_writes           = 0, \
148         .nomap_reads            = 0, \
149         .write_maps             = 0, \
150 }
151
152 struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
153
154 static int ubd0_init(void)
155 {
156         struct ubd *dev = &ubd_dev[0];
157
158         if(dev->file == NULL)
159                 dev->file = "root_fs";
160         return(0);
161 }
162
163 __initcall(ubd0_init);
164
165 /* Only changed by fake_ide_setup which is a setup */
166 static int fake_ide = 0;
167 static struct proc_dir_entry *proc_ide_root = NULL;
168 static struct proc_dir_entry *proc_ide = NULL;
169
170 static void make_proc_ide(void)
171 {
172         proc_ide_root = proc_mkdir("ide", 0);
173         proc_ide = proc_mkdir("ide0", proc_ide_root);
174 }
175
176 static int proc_ide_read_media(char *page, char **start, off_t off, int count,
177                                int *eof, void *data)
178 {
179         int len;
180
181         strcpy(page, "disk\n");
182         len = strlen("disk\n");
183         len -= off;
184         if (len < count){
185                 *eof = 1;
186                 if (len <= 0) return 0;
187         }
188         else len = count;
189         *start = page + off;
190         return len;
191 }
192
193 static void make_ide_entries(char *dev_name)
194 {
195         struct proc_dir_entry *dir, *ent;
196         char name[64];
197
198         if(proc_ide_root == NULL) make_proc_ide();
199
200         dir = proc_mkdir(dev_name, proc_ide);
201         if(!dir) return;
202
203         ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
204         if(!ent) return;
205         ent->nlink = 1;
206         ent->data = NULL;
207         ent->read_proc = proc_ide_read_media;
208         ent->write_proc = NULL;
209         sprintf(name,"ide0/%s", dev_name);
210         proc_symlink(dev_name, proc_ide_root, name);
211 }
212
213 static int fake_ide_setup(char *str)
214 {
215         fake_ide = 1;
216         return(1);
217 }
218
219 __setup("fake_ide", fake_ide_setup);
220
221 __uml_help(fake_ide_setup,
222 "fake_ide\n"
223 "    Create ide0 entries that map onto ubd devices.\n\n"
224 );
225
226 static int parse_unit(char **ptr)
227 {
228         char *str = *ptr, *end;
229         int n = -1;
230
231         if(isdigit(*str)) {
232                 n = simple_strtoul(str, &end, 0);
233                 if(end == str)
234                         return(-1);
235                 *ptr = end;
236         }
237         else if (('a' <= *str) && (*str <= 'h')) {
238                 n = *str - 'a';
239                 str++;
240                 *ptr = str;
241         }
242         return(n);
243 }
244
245 static int ubd_setup_common(char *str, int *index_out)
246 {
247         struct ubd *dev;
248         struct openflags flags = global_openflags;
249         char *backing_file;
250         int n, err;
251
252         if(index_out) *index_out = -1;
253         n = *str;
254         if(n == '='){
255                 char *end;
256                 int major;
257
258                 str++;
259                 if(!strcmp(str, "mmap")){
260                         CHOOSE_MODE(printk("mmap not supported by the ubd "
261                                            "driver in tt mode\n"),
262                                     ubd_do_mmap = 1);
263                         return(0);
264                 }
265
266                 if(!strcmp(str, "sync")){
267                         global_openflags.s = 1;
268                         return(0);
269                 }
270                 major = simple_strtoul(str, &end, 0);
271                 if((*end != '\0') || (end == str)){
272                         printk(KERN_ERR 
273                                "ubd_setup : didn't parse major number\n");
274                         return(1);
275                 }
276
277                 err = 1;
278                 spin_lock(&ubd_lock);
279                 if(fake_major != MAJOR_NR){
280                         printk(KERN_ERR "Can't assign a fake major twice\n");
281                         goto out1;
282                 }
283  
284                 fake_major = major;
285
286                 printk(KERN_INFO "Setting extra ubd major number to %d\n",
287                        major);
288                 err = 0;
289         out1:
290                 spin_unlock(&ubd_lock);
291                 return(err);
292         }
293
294         n = parse_unit(&str);
295         if(n < 0){
296                 printk(KERN_ERR "ubd_setup : couldn't parse unit number "
297                        "'%s'\n", str);
298                 return(1);
299         }
300         if(n >= MAX_DEV){
301                 printk(KERN_ERR "ubd_setup : index %d out of range "
302                        "(%d devices)\n", n, MAX_DEV);
303                 return(1);
304         }
305
306         err = 1;
307         spin_lock(&ubd_lock);
308
309         dev = &ubd_dev[n];
310         if(dev->file != NULL){
311                 printk(KERN_ERR "ubd_setup : device already configured\n");
312                 goto out2;
313         }
314
315         if(index_out) *index_out = n;
316
317         if (*str == 'r'){
318                 flags.w = 0;
319                 str++;
320         }
321         if (*str == 's'){
322                 flags.s = 1;
323                 str++;
324         }
325         if (*str == 'd'){
326                 dev->no_cow = 1;
327                 str++;
328         }
329
330         if(*str++ != '='){
331                 printk(KERN_ERR "ubd_setup : Expected '='\n");
332                 goto out2;
333         }
334
335         err = 0;
336         backing_file = strchr(str, ',');
337         if(backing_file){
338                 if(dev->no_cow)
339                         printk(KERN_ERR "Can't specify both 'd' and a "
340                                "cow file\n");
341                 else {
342                         *backing_file = '\0';
343                         backing_file++;
344                 }
345         }
346         dev->file = str;
347         dev->cow.file = backing_file;
348         dev->boot_openflags = flags;
349  out2:
350         spin_unlock(&ubd_lock);
351         return(err);
352 }
353
354 static int ubd_setup(char *str)
355 {
356         ubd_setup_common(str, NULL);
357         return(1);
358 }
359
360 __setup("ubd", ubd_setup);
361 __uml_help(ubd_setup,
362 "ubd<n>=<filename>\n"
363 "    This is used to associate a device with a file in the underlying\n"
364 "    filesystem. Usually, there is a filesystem in the file, but \n"
365 "    that's not required. Swap devices containing swap files can be\n"
366 "    specified like this. Also, a file which doesn't contain a\n"
367 "    filesystem can have its contents read in the virtual \n"
368 "    machine by running dd on the device. n must be in the range\n"
369 "    0 to 7. Appending an 'r' to the number will cause that device\n"
370 "    to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
371 "    an 's' (has to be _after_ 'r', if there is one) will cause data\n"
372 "    to be written to disk on the host immediately.\n\n"
373 );
374
375 static int fakehd_set = 0;
376 static int fakehd(char *str)
377 {
378         printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
379         fakehd_set = 1;
380         return 1;
381 }
382
383 __setup("fakehd", fakehd);
384 __uml_help(fakehd,
385 "fakehd\n"
386 "    Change the ubd device name to \"hd\".\n\n"
387 );
388
389 static void do_ubd_request(request_queue_t * q);
390
391 /* Only changed by ubd_init, which is an initcall. */
392 int thread_fd = -1;
393
394 /* Changed by ubd_handler, which is serialized because interrupts only
395  * happen on CPU 0.
396  */
397 int intr_count = 0;
398
399 static void ubd_finish(struct request *req, int error)
400 {
401         int nsect;
402
403         if(error){
404                 spin_lock(&ubd_io_lock);
405                 end_request(req, 0);
406                 spin_unlock(&ubd_io_lock);
407                 return;
408         }
409         nsect = req->current_nr_sectors;
410         req->sector += nsect;
411         req->buffer += nsect << 9;
412         req->errors = 0;
413         req->nr_sectors -= nsect;
414         req->current_nr_sectors = 0;
415         spin_lock(&ubd_io_lock);
416         end_request(req, 1);
417         spin_unlock(&ubd_io_lock);
418 }
419
420 static void ubd_handler(void)
421 {
422         struct io_thread_req req;
423         struct request *rq = elv_next_request(ubd_queue);
424         int n, err;
425
426         do_ubd = NULL;
427         intr_count++;
428         n = read_ubd_fs(thread_fd, &req, sizeof(req));
429         if(n != sizeof(req)){
430                 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
431                        "err = %d\n", os_getpid(), -n);
432                 spin_lock(&ubd_io_lock);
433                 end_request(rq, 0);
434                 spin_unlock(&ubd_io_lock);
435                 return;
436         }
437         
438         if((req.op != UBD_MMAP) && 
439            ((req.offset != ((__u64) (rq->sector)) << 9) ||
440             (req.length != (rq->current_nr_sectors) << 9)))
441                 panic("I/O op mismatch");
442         
443         if(req.map_fd != -1){
444                 err = physmem_subst_mapping(req.buffer, req.map_fd, 
445                                             req.map_offset, 1);
446                 if(err)
447                         printk("ubd_handler - physmem_subst_mapping failed, "
448                                "err = %d\n", -err);
449         }
450
451         ubd_finish(rq, req.error);
452         reactivate_fd(thread_fd, UBD_IRQ);      
453         do_ubd_request(ubd_queue);
454 }
455
456 static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
457 {
458         ubd_handler();
459         return(IRQ_HANDLED);
460 }
461
462 /* Only changed by ubd_init, which is an initcall. */
463 static int io_pid = -1;
464
465 void kill_io_thread(void)
466 {
467         if(io_pid != -1) 
468                 os_kill_process(io_pid, 1);
469 }
470
471 __uml_exitcall(kill_io_thread);
472
473 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
474 {
475         char *file;
476
477         file = dev->cow.file ? dev->cow.file : dev->file;
478         return(os_file_size(file, size_out));
479 }
480
481 static void ubd_close(struct ubd *dev)
482 {
483         if(ubd_do_mmap)
484                 physmem_forget_descriptor(dev->fd);
485         os_close_file(dev->fd);
486         if(dev->cow.file == NULL)
487                 return;
488
489         if(ubd_do_mmap)
490                 physmem_forget_descriptor(dev->cow.fd);
491         os_close_file(dev->cow.fd);
492         vfree(dev->cow.bitmap);
493         dev->cow.bitmap = NULL;
494 }
495
496 static int ubd_open_dev(struct ubd *dev)
497 {
498         struct openflags flags;
499         char **back_ptr;
500         int err, create_cow, *create_ptr;
501
502         dev->openflags = dev->boot_openflags;
503         create_cow = 0;
504         create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
505         back_ptr = dev->no_cow ? NULL : &dev->cow.file;
506         dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
507                                 &dev->cow.bitmap_offset, &dev->cow.bitmap_len, 
508                                 &dev->cow.data_offset, create_ptr);
509
510         if((dev->fd == -ENOENT) && create_cow){
511                 dev->fd = create_cow_file(dev->file, dev->cow.file, 
512                                           dev->openflags, 1 << 9, PAGE_SIZE,
513                                           &dev->cow.bitmap_offset, 
514                                           &dev->cow.bitmap_len,
515                                           &dev->cow.data_offset);
516                 if(dev->fd >= 0){
517                         printk(KERN_INFO "Creating \"%s\" as COW file for "
518                                "\"%s\"\n", dev->file, dev->cow.file);
519                 }
520         }
521
522         if(dev->fd < 0) return(dev->fd);
523
524         if(dev->cow.file != NULL){
525                 err = -ENOMEM;
526                 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
527                 if(dev->cow.bitmap == NULL){
528                         printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
529                         goto error;
530                 }
531                 flush_tlb_kernel_vm();
532
533                 err = read_cow_bitmap(dev->fd, dev->cow.bitmap, 
534                                       dev->cow.bitmap_offset, 
535                                       dev->cow.bitmap_len);
536                 if(err < 0)
537                         goto error;
538
539                 flags = dev->openflags;
540                 flags.w = 0;
541                 err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL, 
542                                     NULL, NULL);
543                 if(err < 0) goto error;
544                 dev->cow.fd = err;
545         }
546         return(0);
547  error:
548         os_close_file(dev->fd);
549         return(err);
550 }
551
552 static int ubd_new_disk(int major, u64 size, int unit,
553                         struct gendisk **disk_out)
554                         
555 {
556         struct gendisk *disk;
557         char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
558         int err;
559
560         disk = alloc_disk(1 << UBD_SHIFT);
561         if(disk == NULL)
562                 return(-ENOMEM);
563
564         disk->major = major;
565         disk->first_minor = unit << UBD_SHIFT;
566         disk->fops = &ubd_blops;
567         set_capacity(disk, size / 512);
568         if(major == MAJOR_NR){
569                 sprintf(disk->disk_name, "ubd%c", 'a' + unit);
570                 sprintf(disk->devfs_name, "ubd/disc%d", unit);
571                 sprintf(from, "ubd/%d", unit);
572                 sprintf(to, "disc%d/disc", unit);
573                 err = devfs_mk_symlink(from, to);
574                 if(err)
575                         printk("ubd_new_disk failed to make link from %s to "
576                                "%s, error = %d\n", from, to, err);
577         }
578         else {
579                 sprintf(disk->disk_name, "ubd_fake%d", unit);
580                 sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
581         }
582
583         disk->private_data = &ubd_dev[unit];
584         disk->queue = ubd_queue;
585         add_disk(disk);
586
587         *disk_out = disk;
588         return 0;
589 }
590
591 static int ubd_add(int n)
592 {
593         struct ubd *dev = &ubd_dev[n];
594         int err;
595
596         if(dev->file == NULL)
597                 return(-ENODEV);
598
599         if (ubd_open_dev(dev))
600                 return(-ENODEV);
601
602         err = ubd_file_size(dev, &dev->size);
603         if(err < 0)
604                 return(err);
605
606         err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
607         if(err) 
608                 return(err);
609  
610         if(fake_major != MAJOR_NR)
611                 ubd_new_disk(fake_major, dev->size, n, 
612                              &fake_gendisk[n]);
613
614         /* perhaps this should also be under the "if (fake_major)" above */
615         /* using the fake_disk->disk_name and also the fakehd_set name */
616         if (fake_ide)
617                 make_ide_entries(ubd_gendisk[n]->disk_name);
618
619         ubd_close(dev);
620         return 0;
621 }
622
623 static int ubd_config(char *str)
624 {
625         int n, err;
626
627         str = uml_strdup(str);
628         if(str == NULL){
629                 printk(KERN_ERR "ubd_config failed to strdup string\n");
630                 return(1);
631         }
632         err = ubd_setup_common(str, &n);
633         if(err){
634                 kfree(str);
635                 return(-1);
636         }
637         if(n == -1) return(0);
638
639         spin_lock(&ubd_lock);
640         err = ubd_add(n);
641         if(err)
642                 ubd_dev[n].file = NULL;
643         spin_unlock(&ubd_lock);
644
645         return(err);
646 }
647
648 static int ubd_get_config(char *name, char *str, int size, char **error_out)
649 {
650         struct ubd *dev;
651         char *end;
652         int n, len = 0;
653
654         n = simple_strtoul(name, &end, 0);
655         if((*end != '\0') || (end == name)){
656                 *error_out = "ubd_get_config : didn't parse device number";
657                 return(-1);
658         }
659
660         if((n >= MAX_DEV) || (n < 0)){
661                 *error_out = "ubd_get_config : device number out of range";
662                 return(-1);
663         }
664
665         dev = &ubd_dev[n];
666         spin_lock(&ubd_lock);
667
668         if(dev->file == NULL){
669                 CONFIG_CHUNK(str, size, len, "", 1);
670                 goto out;
671         }
672
673         CONFIG_CHUNK(str, size, len, dev->file, 0);
674
675         if(dev->cow.file != NULL){
676                 CONFIG_CHUNK(str, size, len, ",", 0);
677                 CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
678         }
679         else CONFIG_CHUNK(str, size, len, "", 1);
680
681  out:
682         spin_unlock(&ubd_lock);
683         return(len);
684 }
685
686 static int ubd_remove(char *str)
687 {
688         struct ubd *dev;
689         int n, err = -ENODEV;
690
691         n = parse_unit(&str);
692
693         if((n < 0) || (n >= MAX_DEV))
694                 return(err);
695
696         dev = &ubd_dev[n];
697         if(dev->count > 0)
698                 return(-EBUSY); /* you cannot remove a open disk */
699
700         err = 0;
701         spin_lock(&ubd_lock);
702
703         if(ubd_gendisk[n] == NULL)
704                 goto out;
705
706         del_gendisk(ubd_gendisk[n]);
707         put_disk(ubd_gendisk[n]);
708         ubd_gendisk[n] = NULL;
709
710         if(fake_gendisk[n] != NULL){
711                 del_gendisk(fake_gendisk[n]);
712                 put_disk(fake_gendisk[n]);
713                 fake_gendisk[n] = NULL;
714         }
715
716         *dev = ((struct ubd) DEFAULT_UBD);
717         err = 0;
718  out:
719         spin_unlock(&ubd_lock);
720         return(err);
721 }
722
723 static struct mc_device ubd_mc = {
724         .name           = "ubd",
725         .config         = ubd_config,
726         .get_config     = ubd_get_config,
727         .remove         = ubd_remove,
728 };
729
730 static int ubd_mc_init(void)
731 {
732         mconsole_register_dev(&ubd_mc);
733         return 0;
734 }
735
736 __initcall(ubd_mc_init);
737
738 int ubd_init(void)
739 {
740         int i;
741
742         devfs_mk_dir("ubd");
743         if (register_blkdev(MAJOR_NR, "ubd"))
744                 return -1;
745
746         ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
747         if (!ubd_queue) {
748                 unregister_blkdev(MAJOR_NR, "ubd");
749                 return -1;
750         }
751                 
752         elevator_init(ubd_queue, &elevator_noop);
753
754         if (fake_major != MAJOR_NR) {
755                 char name[sizeof("ubd_nnn\0")];
756
757                 snprintf(name, sizeof(name), "ubd_%d", fake_major);
758                 devfs_mk_dir(name);
759                 if (register_blkdev(fake_major, "ubd"))
760                         return -1;
761         }
762         for (i = 0; i < MAX_DEV; i++) 
763                 ubd_add(i);
764         return 0;
765 }
766
767 late_initcall(ubd_init);
768
769 int ubd_driver_init(void){
770         unsigned long stack;
771         int err;
772
773         if(global_openflags.s){
774                 printk(KERN_INFO "ubd : Synchronous mode\n");
775                 return(0);
776         }
777         stack = alloc_stack(0, 0);
778         io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *), 
779                                  &thread_fd);
780         if(io_pid < 0){
781                 io_pid = -1;
782                 printk(KERN_ERR 
783                        "ubd : Failed to start I/O thread (errno = %d) - "
784                        "falling back to synchronous I/O\n", -io_pid);
785                 return(0);
786         }
787         err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr, 
788                              SA_INTERRUPT, "ubd", ubd_dev);
789         if(err != 0) 
790                 printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
791         return(err);
792 }
793
794 device_initcall(ubd_driver_init);
795
796 static int ubd_open(struct inode *inode, struct file *filp)
797 {
798         struct gendisk *disk = inode->i_bdev->bd_disk;
799         struct ubd *dev = disk->private_data;
800         int err = 0;
801
802         if(dev->count == 0){
803                 err = ubd_open_dev(dev);
804                 if(err){
805                         printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
806                                disk->disk_name, dev->file, -err);
807                         goto out;
808                 }
809         }
810         dev->count++;
811         if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
812                 if(--dev->count == 0) ubd_close(dev);
813                 err = -EROFS;
814         }
815  out:
816         return(err);
817 }
818
819 static int ubd_release(struct inode * inode, struct file * file)
820 {
821         struct gendisk *disk = inode->i_bdev->bd_disk;
822         struct ubd *dev = disk->private_data;
823
824         if(--dev->count == 0)
825                 ubd_close(dev);
826         return(0);
827 }
828
829 static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask, 
830                           __u64 *cow_offset, unsigned long *bitmap, 
831                           __u64 bitmap_offset, unsigned long *bitmap_words,
832                           __u64 bitmap_len)
833 {
834         __u64 sector = io_offset >> 9;
835         int i, update_bitmap = 0;
836
837         for(i = 0; i < length >> 9; i++){
838                 if(cow_mask != NULL)
839                         ubd_set_bit(i, (unsigned char *) cow_mask);
840                 if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
841                         continue;
842
843                 update_bitmap = 1;
844                 ubd_set_bit(sector + i, (unsigned char *) bitmap);
845         }
846
847         if(!update_bitmap)
848                 return;
849
850         *cow_offset = sector / (sizeof(unsigned long) * 8);
851
852         /* This takes care of the case where we're exactly at the end of the
853          * device, and *cow_offset + 1 is off the end.  So, just back it up
854          * by one word.  Thanks to Lynn Kerby for the fix and James McMechan
855          * for the original diagnosis.
856          */
857         if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) / 
858                            sizeof(unsigned long) - 1))
859                 (*cow_offset)--;
860
861         bitmap_words[0] = bitmap[*cow_offset];
862         bitmap_words[1] = bitmap[*cow_offset + 1];
863
864         *cow_offset *= sizeof(unsigned long);
865         *cow_offset += bitmap_offset;
866 }
867
868 static void cowify_req(struct io_thread_req *req, unsigned long *bitmap, 
869                        __u64 bitmap_offset, __u64 bitmap_len)
870 {
871         __u64 sector = req->offset >> 9;
872         int i;
873
874         if(req->length > (sizeof(req->sector_mask) * 8) << 9)
875                 panic("Operation too long");
876
877         if(req->op == UBD_READ) {
878                 for(i = 0; i < req->length >> 9; i++){
879                         if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
880                                 ubd_set_bit(i, (unsigned char *) 
881                                             &req->sector_mask);
882                 }
883         }
884         else cowify_bitmap(req->offset, req->length, &req->sector_mask,
885                            &req->cow_offset, bitmap, bitmap_offset, 
886                            req->bitmap_words, bitmap_len);
887 }
888
889 static int mmap_fd(struct request *req, struct ubd *dev, __u64 offset)
890 {
891         __u64 sector;
892         unsigned char *bitmap;
893         int bit, i;
894
895         /* mmap must have been requested on the command line */
896         if(!ubd_do_mmap)
897                 return(-1);
898
899         /* The buffer must be page aligned */
900         if(((unsigned long) req->buffer % UBD_MMAP_BLOCK_SIZE) != 0)
901                 return(-1);
902
903         /* The request must be a page long */
904         if((req->current_nr_sectors << 9) != PAGE_SIZE)
905                 return(-1);
906
907         if(dev->cow.file == NULL)
908                 return(dev->fd);
909
910         sector = offset >> 9;
911         bitmap = (unsigned char *) dev->cow.bitmap;
912         bit = ubd_test_bit(sector, bitmap);
913
914         for(i = 1; i < req->current_nr_sectors; i++){
915                 if(ubd_test_bit(sector + i, bitmap) != bit)
916                         return(-1);
917         }
918
919         if(bit || (rq_data_dir(req) == WRITE))
920                 offset += dev->cow.data_offset;
921
922         /* The data on disk must be page aligned */
923         if((offset % UBD_MMAP_BLOCK_SIZE) != 0)
924                 return(-1);
925
926         return(bit ? dev->fd : dev->cow.fd);
927 }
928
929 static int prepare_mmap_request(struct ubd *dev, int fd, __u64 offset, 
930                                 struct request *req, 
931                                 struct io_thread_req *io_req)
932 {
933         int err;
934
935         if(rq_data_dir(req) == WRITE){
936                 /* Writes are almost no-ops since the new data is already in the
937                  * host page cache
938                  */
939                 dev->map_writes++;
940                 if(dev->cow.file != NULL)
941                         cowify_bitmap(io_req->offset, io_req->length, 
942                                       &io_req->sector_mask, &io_req->cow_offset,
943                                       dev->cow.bitmap, dev->cow.bitmap_offset,
944                                       io_req->bitmap_words, 
945                                       dev->cow.bitmap_len);
946         }
947         else {
948                 int w;
949
950                 if((dev->cow.file != NULL) && (fd == dev->cow.fd))
951                         w = 0;
952                 else w = dev->openflags.w;
953
954                 if((dev->cow.file != NULL) && (fd == dev->fd))
955                         offset += dev->cow.data_offset;
956
957                 err = physmem_subst_mapping(req->buffer, fd, offset, w);
958                 if(err){
959                         printk("physmem_subst_mapping failed, err = %d\n", 
960                                -err);
961                         return(1);
962                 }
963                 dev->map_reads++;
964         }
965         io_req->op = UBD_MMAP;
966         io_req->buffer = req->buffer;
967         return(0);
968 }
969
970 static int prepare_request(struct request *req, struct io_thread_req *io_req)
971 {
972         struct gendisk *disk = req->rq_disk;
973         struct ubd *dev = disk->private_data;
974         __u64 offset;
975         int len, fd;
976
977         if(req->rq_status == RQ_INACTIVE) return(1);
978
979         if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
980                 printk("Write attempted on readonly ubd device %s\n", 
981                        disk->disk_name);
982                 spin_lock(&ubd_io_lock);
983                 end_request(req, 0);
984                 spin_unlock(&ubd_io_lock);
985                 return(1);
986         }
987
988         offset = ((__u64) req->sector) << 9;
989         len = req->current_nr_sectors << 9;
990
991         io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
992         io_req->fds[1] = dev->fd;
993         io_req->map_fd = -1;
994         io_req->cow_offset = -1;
995         io_req->offset = offset;
996         io_req->length = len;
997         io_req->error = 0;
998         io_req->sector_mask = 0;
999
1000         fd = mmap_fd(req, dev, io_req->offset);
1001         if(fd > 0){
1002                 /* If mmapping is otherwise OK, but the first access to the 
1003                  * page is a write, then it's not mapped in yet.  So we have 
1004                  * to write the data to disk first, then we can map the disk
1005                  * page in and continue normally from there.
1006                  */
1007                 if((rq_data_dir(req) == WRITE) && !is_remapped(req->buffer)){
1008                         io_req->map_fd = dev->fd;
1009                         io_req->map_offset = io_req->offset + 
1010                                 dev->cow.data_offset;
1011                         dev->write_maps++;
1012                 }
1013                 else return(prepare_mmap_request(dev, fd, io_req->offset, req, 
1014                                                  io_req));
1015         }
1016
1017         if(rq_data_dir(req) == READ)
1018                 dev->nomap_reads++;
1019         else dev->nomap_writes++;
1020
1021         io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
1022         io_req->offsets[0] = 0;
1023         io_req->offsets[1] = dev->cow.data_offset;
1024         io_req->buffer = req->buffer;
1025         io_req->sectorsize = 1 << 9;
1026
1027         if(dev->cow.file != NULL) 
1028                 cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
1029                            dev->cow.bitmap_len);
1030
1031         return(0);
1032 }
1033
1034 static void do_ubd_request(request_queue_t *q)
1035 {
1036         struct io_thread_req io_req;
1037         struct request *req;
1038         int err, n;
1039
1040         if(thread_fd == -1){
1041                 while(!elv_queue_empty(q)){
1042                         req = elv_next_request(q);
1043                         err = prepare_request(req, &io_req);
1044                         if(!err){
1045                                 do_io(&io_req);
1046                                 ubd_finish(req, io_req.error);
1047                         }
1048                 }
1049         }
1050         else {
1051                 if(do_ubd || elv_queue_empty(q))
1052                         return;
1053                 req = elv_next_request(q);
1054                 err = prepare_request(req, &io_req);
1055                 if(!err){
1056                         do_ubd = ubd_handler;
1057                         n = write_ubd_fs(thread_fd, (char *) &io_req, 
1058                                          sizeof(io_req));
1059                         if(n != sizeof(io_req))
1060                                 printk("write to io thread failed, "
1061                                        "errno = %d\n", -n);
1062                 }
1063         }
1064 }
1065
1066 static int ubd_ioctl(struct inode * inode, struct file * file,
1067                      unsigned int cmd, unsigned long arg)
1068 {
1069         struct hd_geometry *loc = (struct hd_geometry *) arg;
1070         struct ubd *dev = inode->i_bdev->bd_disk->private_data;
1071         int err;
1072         struct hd_driveid ubd_id = {
1073                 .cyls           = 0,
1074                 .heads          = 128,
1075                 .sectors        = 32,
1076         };
1077
1078         switch (cmd) {
1079                 struct hd_geometry g;
1080                 struct cdrom_volctrl volume;
1081         case HDIO_GETGEO:
1082                 if(!loc) return(-EINVAL);
1083                 g.heads = 128;
1084                 g.sectors = 32;
1085                 g.cylinders = dev->size / (128 * 32 * 512);
1086                 g.start = get_start_sect(inode->i_bdev);
1087                 return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
1088
1089         case HDIO_SET_UNMASKINTR:
1090                 if(!capable(CAP_SYS_ADMIN)) return(-EACCES);
1091                 if((arg > 1) || (inode->i_bdev->bd_contains != inode->i_bdev))
1092                         return(-EINVAL);
1093                 return(0);
1094
1095         case HDIO_GET_UNMASKINTR:
1096                 if(!arg)  return(-EINVAL);
1097                 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
1098                 if(err)
1099                         return(err);
1100                 return(0);
1101
1102         case HDIO_GET_MULTCOUNT:
1103                 if(!arg)  return(-EINVAL);
1104                 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
1105                 if(err)
1106                         return(err);
1107                 return(0);
1108
1109         case HDIO_SET_MULTCOUNT:
1110                 if(!capable(CAP_SYS_ADMIN)) return(-EACCES);
1111                 if(inode->i_bdev->bd_contains != inode->i_bdev)
1112                         return(-EINVAL);
1113                 return(0);
1114
1115         case HDIO_GET_IDENTITY:
1116                 ubd_id.cyls = dev->size / (128 * 32 * 512);
1117                 if(copy_to_user((char *) arg, (char *) &ubd_id, 
1118                                  sizeof(ubd_id)))
1119                         return(-EFAULT);
1120                 return(0);
1121                 
1122         case CDROMVOLREAD:
1123                 if(copy_from_user(&volume, (char *) arg, sizeof(volume)))
1124                         return(-EFAULT);
1125                 volume.channel0 = 255;
1126                 volume.channel1 = 255;
1127                 volume.channel2 = 255;
1128                 volume.channel3 = 255;
1129                 if(copy_to_user((char *) arg, &volume, sizeof(volume)))
1130                         return(-EFAULT);
1131                 return(0);
1132         }
1133         return(-EINVAL);
1134 }
1135
1136 static int ubd_check_remapped(int fd, unsigned long address, int is_write,
1137                               __u64 offset)
1138 {
1139         __u64 bitmap_offset;
1140         unsigned long new_bitmap[2];
1141         int i, err, n;
1142
1143         /* If it's not a write access, we can't do anything about it */
1144         if(!is_write)
1145                 return(0);
1146
1147         /* We have a write */
1148         for(i = 0; i < sizeof(ubd_dev) / sizeof(ubd_dev[0]); i++){
1149                 struct ubd *dev = &ubd_dev[i];
1150
1151                 if((dev->fd != fd) && (dev->cow.fd != fd))
1152                         continue;
1153
1154                 /* It's a write to a ubd device */
1155
1156                 if(!dev->openflags.w){
1157                         /* It's a write access on a read-only device - probably
1158                          * shouldn't happen.  If the kernel is trying to change
1159                          * something with no intention of writing it back out,
1160                          * then this message will clue us in that this needs
1161                          * fixing
1162                          */
1163                         printk("Write access to mapped page from readonly ubd "
1164                                "device %d\n", i);
1165                         return(0);
1166                 }
1167
1168                 /* It's a write to a writeable ubd device - it must be COWed
1169                  * because, otherwise, the page would have been mapped in 
1170                  * writeable
1171                  */
1172
1173                 if(!dev->cow.file)
1174                         panic("Write fault on writeable non-COW ubd device %d",
1175                               i);
1176
1177                 /* It should also be an access to the backing file since the 
1178                  * COW pages should be mapped in read-write
1179                  */
1180
1181                 if(fd == dev->fd)
1182                         panic("Write fault on a backing page of ubd "
1183                               "device %d\n", i);
1184
1185                 /* So, we do the write, copying the backing data to the COW 
1186                  * file... 
1187                  */
1188
1189                 err = os_seek_file(dev->fd, offset + dev->cow.data_offset);
1190                 if(err < 0)
1191                         panic("Couldn't seek to %lld in COW file of ubd "
1192                               "device %d, err = %d", 
1193                               offset + dev->cow.data_offset, i, -err);
1194
1195                 n = os_write_file(dev->fd, (void *) address, PAGE_SIZE);
1196                 if(n != PAGE_SIZE)
1197                         panic("Couldn't copy data to COW file of ubd "
1198                               "device %d, err = %d", i, -n);
1199
1200                 /* ... updating the COW bitmap... */
1201
1202                 cowify_bitmap(offset, PAGE_SIZE, NULL, &bitmap_offset, 
1203                               dev->cow.bitmap, dev->cow.bitmap_offset, 
1204                               new_bitmap, dev->cow.bitmap_len);
1205
1206                 err = os_seek_file(dev->fd, bitmap_offset);
1207                 if(err < 0)
1208                         panic("Couldn't seek to %lld in COW file of ubd "
1209                               "device %d, err = %d", bitmap_offset, i, -err);
1210
1211                 n = os_write_file(dev->fd, new_bitmap, sizeof(new_bitmap));
1212                 if(n != sizeof(new_bitmap))
1213                         panic("Couldn't update bitmap  of ubd device %d, "
1214                               "err = %d", i, -n);
1215                 
1216                 /* Maybe we can map the COW page in, and maybe we can't.  If
1217                  * it is a pre-V3 COW file, we can't, since the alignment will 
1218                  * be wrong.  If it is a V3 or later COW file which has been 
1219                  * moved to a system with a larger page size, then maybe we 
1220                  * can't, depending on the exact location of the page.
1221                  */
1222
1223                 offset += dev->cow.data_offset;
1224
1225                 /* Remove the remapping, putting the original anonymous page
1226                  * back.  If the COW file can be mapped in, that is done.
1227                  * Otherwise, the COW page is read in.
1228                  */
1229
1230                 if(!physmem_remove_mapping((void *) address))
1231                         panic("Address 0x%lx not remapped by ubd device %d", 
1232                               address, i);
1233                 if((offset % UBD_MMAP_BLOCK_SIZE) == 0)
1234                         physmem_subst_mapping((void *) address, dev->fd, 
1235                                               offset, 1);
1236                 else {
1237                         err = os_seek_file(dev->fd, offset);
1238                         if(err < 0)
1239                                 panic("Couldn't seek to %lld in COW file of "
1240                                       "ubd device %d, err = %d", offset, i, 
1241                                       -err);
1242
1243                         n = os_read_file(dev->fd, (void *) address, PAGE_SIZE);
1244                         if(n != PAGE_SIZE)
1245                                 panic("Failed to read page from offset %llx of "
1246                                       "COW file of ubd device %d, err = %d",
1247                                       offset, i, -n);
1248                 }
1249
1250                 return(1);
1251         }
1252
1253         /* It's not a write on a ubd device */
1254         return(0);
1255 }
1256
1257 static struct remapper ubd_remapper = {
1258         .list   = LIST_HEAD_INIT(ubd_remapper.list),
1259         .proc   = ubd_check_remapped,
1260 };
1261
1262 static int ubd_remapper_setup(void)
1263 {
1264         if(ubd_do_mmap)
1265                 register_remapper(&ubd_remapper);
1266
1267         return(0);
1268 }
1269
1270 __initcall(ubd_remapper_setup);
1271
1272 /*
1273  * Overrides for Emacs so that we follow Linus's tabbing style.
1274  * Emacs will notice this stuff at the end of the file and automatically
1275  * adjust the settings for this buffer only.  This must remain at the end
1276  * of the file.
1277  * ---------------------------------------------------------------------------
1278  * Local variables:
1279  * c-file-style: "linux"
1280  * End:
1281  */