ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[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  */
12
13 #define MAJOR_NR UBD_MAJOR
14 #define UBD_SHIFT 4
15
16 #include "linux/config.h"
17 #include "linux/module.h"
18 #include "linux/blkdev.h"
19 #include "linux/hdreg.h"
20 #include "linux/init.h"
21 #include "linux/devfs_fs_kernel.h"
22 #include "linux/cdrom.h"
23 #include "linux/proc_fs.h"
24 #include "linux/ctype.h"
25 #include "linux/capability.h"
26 #include "linux/mm.h"
27 #include "linux/vmalloc.h"
28 #include "linux/blkpg.h"
29 #include "linux/genhd.h"
30 #include "linux/spinlock.h"
31 #include "asm/segment.h"
32 #include "asm/uaccess.h"
33 #include "asm/irq.h"
34 #include "asm/types.h"
35 #include "asm/tlbflush.h"
36 #include "user_util.h"
37 #include "mem_user.h"
38 #include "kern_util.h"
39 #include "kern.h"
40 #include "mconsole_kern.h"
41 #include "init.h"
42 #include "irq_user.h"
43 #include "ubd_user.h"
44 #include "2_5compat.h"
45 #include "os.h"
46
47 static spinlock_t ubd_io_lock = SPIN_LOCK_UNLOCKED;
48 static spinlock_t ubd_lock = SPIN_LOCK_UNLOCKED;
49
50 static void (*do_ubd)(void);
51
52 static int ubd_open(struct inode * inode, struct file * filp);
53 static int ubd_release(struct inode * inode, struct file * file);
54 static int ubd_ioctl(struct inode * inode, struct file * file,
55                      unsigned int cmd, unsigned long arg);
56
57 #define MAX_DEV (8)
58
59 static struct block_device_operations ubd_blops = {
60         .owner          = THIS_MODULE,
61         .open           = ubd_open,
62         .release        = ubd_release,
63         .ioctl          = ubd_ioctl,
64 };
65
66 /* Protected by the queue_lock */
67 static request_queue_t *ubd_queue;
68
69 /* Protected by ubd_lock */
70 static int fake_major = 0;
71
72 static struct gendisk *ubd_gendisk[MAX_DEV];
73 static struct gendisk *fake_gendisk[MAX_DEV];
74  
75 #ifdef CONFIG_BLK_DEV_UBD_SYNC
76 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
77                                          .cl = 1 })
78 #else
79 #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
80                                          .cl = 1 })
81 #endif
82
83 /* Not protected - changed only in ubd_setup_common and then only to
84  * to enable O_SYNC.
85  */
86 static struct openflags global_openflags = OPEN_FLAGS;
87
88 struct cow {
89         char *file;
90         int fd;
91         unsigned long *bitmap;
92         unsigned long bitmap_len;
93         int bitmap_offset;
94         int data_offset;
95 };
96
97 struct ubd {
98         char *file;
99         int is_dir;
100         int count;
101         int fd;
102         __u64 size;
103         struct openflags boot_openflags;
104         struct openflags openflags;
105         struct cow cow;
106 };
107
108 #define DEFAULT_COW { \
109         .file =                 NULL, \
110         .fd =                   -1, \
111         .bitmap =               NULL, \
112         .bitmap_offset =        0, \
113         .data_offset =          0, \
114 }
115
116 #define DEFAULT_UBD { \
117         .file =                 NULL, \
118         .is_dir =               0, \
119         .count =                0, \
120         .fd =                   -1, \
121         .size =                 -1, \
122         .boot_openflags =       OPEN_FLAGS, \
123         .openflags =            OPEN_FLAGS, \
124         .cow =                  DEFAULT_COW, \
125 }
126
127 struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
128
129 static int ubd0_init(void)
130 {
131         if(ubd_dev[0].file == NULL)
132                 ubd_dev[0].file = "root_fs";
133         return(0);
134 }
135
136 __initcall(ubd0_init);
137
138 /* Only changed by fake_ide_setup which is a setup */
139 static int fake_ide = 0;
140 static struct proc_dir_entry *proc_ide_root = NULL;
141 static struct proc_dir_entry *proc_ide = NULL;
142
143 static void make_proc_ide(void)
144 {
145         proc_ide_root = proc_mkdir("ide", 0);
146         proc_ide = proc_mkdir("ide0", proc_ide_root);
147 }
148
149 static int proc_ide_read_media(char *page, char **start, off_t off, int count,
150                                int *eof, void *data)
151 {
152         int len;
153
154         strcpy(page, "disk\n");
155         len = strlen("disk\n");
156         len -= off;
157         if (len < count){
158                 *eof = 1;
159                 if (len <= 0) return 0;
160         }
161         else len = count;
162         *start = page + off;
163         return len;
164 }
165
166 static void make_ide_entries(char *dev_name)
167 {
168         struct proc_dir_entry *dir, *ent;
169         char name[64];
170
171         if(proc_ide_root == NULL) make_proc_ide();
172
173         dir = proc_mkdir(dev_name, proc_ide);
174         if(!dir) return;
175
176         ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
177         if(!ent) return;
178         ent->nlink = 1;
179         ent->data = NULL;
180         ent->read_proc = proc_ide_read_media;
181         ent->write_proc = NULL;
182         sprintf(name,"ide0/%s", dev_name);
183         proc_symlink(dev_name, proc_ide_root, name);
184 }
185
186 static int fake_ide_setup(char *str)
187 {
188         fake_ide = 1;
189         return(1);
190 }
191
192 __setup("fake_ide", fake_ide_setup);
193
194 __uml_help(fake_ide_setup,
195 "fake_ide\n"
196 "    Create ide0 entries that map onto ubd devices.\n\n"
197 );
198
199 static int ubd_setup_common(char *str, int *index_out)
200 {
201         struct openflags flags = global_openflags;
202         char *backing_file;
203         int n, err;
204
205         if(index_out) *index_out = -1;
206         n = *str++;
207         if(n == '='){
208                 static int fake_major_allowed = 1;
209                 char *end;
210                 int major;
211
212                 if(!strcmp(str, "sync")){
213                         global_openflags.s = 1;
214                         return(0);
215                 }
216                 major = simple_strtoul(str, &end, 0);
217                 if((*end != '\0') || (end == str)){
218                         printk(KERN_ERR 
219                                "ubd_setup : didn't parse major number\n");
220                         return(1);
221                 }
222
223                 if(!fake_major_allowed){
224                         printk(KERN_ERR "Can't assign a fake major twice\n");
225                         return(1);
226                 }
227
228                 err = 1;
229                 spin_lock(&ubd_lock);
230                 if(!fake_major_allowed){
231                         printk(KERN_ERR "Can't assign a fake major twice\n");
232                         goto out1;
233                 }
234  
235                 fake_major = major;
236                 fake_major_allowed = 0;
237
238                 printk(KERN_INFO "Setting extra ubd major number to %d\n",
239                        major);
240                 err = 0;
241         out1:
242                 spin_unlock(&ubd_lock);
243                 return(err);
244         }
245
246         if(n < '0'){
247                 printk(KERN_ERR "ubd_setup : index out of range\n"); }
248
249         if((n >= '0') && (n <= '9')) n -= '0';
250         else if((n >= 'a') && (n <= 'z')) n -= 'a';
251         else {
252                 printk(KERN_ERR "ubd_setup : device syntax invalid\n");
253                 return(1);
254         }
255         if(n >= MAX_DEV){
256                 printk(KERN_ERR "ubd_setup : index out of range "
257                        "(%d devices)\n", MAX_DEV);      
258                 return(1);
259         }
260
261         err = 1;
262         spin_lock(&ubd_lock);
263
264         if(ubd_dev[n].file != NULL){
265                 printk(KERN_ERR "ubd_setup : device already configured\n");
266                 goto out2;
267         }
268
269         if(index_out) *index_out = n;
270
271         if (*str == 'r'){
272                 flags.w = 0;
273                 str++;
274         }
275         if (*str == 's'){
276                 flags.s = 1;
277                 str++;
278         }
279         if(*str++ != '='){
280                 printk(KERN_ERR "ubd_setup : Expected '='\n");
281                 goto out2;
282         }
283
284         err = 0;
285         backing_file = strchr(str, ',');
286         if(backing_file){
287                 *backing_file = '\0';
288                 backing_file++;
289         }
290         ubd_dev[n].file = str;
291         if(ubd_is_dir(ubd_dev[n].file))
292                 ubd_dev[n].is_dir = 1;
293         ubd_dev[n].cow.file = backing_file;
294         ubd_dev[n].boot_openflags = flags;
295  out2:
296         spin_unlock(&ubd_lock);
297         return(err);
298 }
299
300 static int ubd_setup(char *str)
301 {
302         ubd_setup_common(str, NULL);
303         return(1);
304 }
305
306 __setup("ubd", ubd_setup);
307 __uml_help(ubd_setup,
308 "ubd<n>=<filename>\n"
309 "    This is used to associate a device with a file in the underlying\n"
310 "    filesystem. Usually, there is a filesystem in the file, but \n"
311 "    that's not required. Swap devices containing swap files can be\n"
312 "    specified like this. Also, a file which doesn't contain a\n"
313 "    filesystem can have its contents read in the virtual \n"
314 "    machine by running dd on the device. n must be in the range\n"
315 "    0 to 7. Appending an 'r' to the number will cause that device\n"
316 "    to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
317 "    an 's' (has to be _after_ 'r', if there is one) will cause data\n"
318 "    to be written to disk on the host immediately.\n\n"
319 );
320
321 static int fakehd_set = 0;
322 static int fakehd(char *str)
323 {
324         printk(KERN_INFO 
325                "fakehd : Changing ubd name to \"hd\".\n");
326         fakehd_set = 1;
327         return 1;
328 }
329
330 __setup("fakehd", fakehd);
331 __uml_help(fakehd,
332 "fakehd\n"
333 "    Change the ubd device name to \"hd\".\n\n"
334 );
335
336 static void do_ubd_request(request_queue_t * q);
337
338 /* Only changed by ubd_init, which is an initcall. */
339 int thread_fd = -1;
340
341 /* Changed by ubd_handler, which is serialized because interrupts only
342  * happen on CPU 0.
343  */
344 int intr_count = 0;
345
346 static void ubd_finish(struct request *req, int error)
347 {
348         int nsect;
349
350         if(error){
351                 spin_lock(&ubd_io_lock);
352                 end_request(req, 0);
353                 spin_unlock(&ubd_io_lock);
354                 return;
355         }
356         nsect = req->current_nr_sectors;
357         req->sector += nsect;
358         req->buffer += nsect << 9;
359         req->errors = 0;
360         req->nr_sectors -= nsect;
361         req->current_nr_sectors = 0;
362         spin_lock(&ubd_io_lock);
363         end_request(req, 1);
364         spin_unlock(&ubd_io_lock);
365 }
366
367 static void ubd_handler(void)
368 {
369         struct io_thread_req req;
370         struct request *rq = elv_next_request(ubd_queue);
371         int n;
372
373         do_ubd = NULL;
374         intr_count++;
375         n = read_ubd_fs(thread_fd, &req, sizeof(req));
376         if(n != sizeof(req)){
377                 printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
378                        "errno = %d\n", os_getpid(), -n);
379                 spin_lock(&ubd_io_lock);
380                 end_request(rq, 0);
381                 spin_unlock(&ubd_io_lock);
382                 return;
383         }
384         
385         if((req.offset != ((__u64) (rq->sector)) << 9) ||
386            (req.length != (rq->current_nr_sectors) << 9))
387                 panic("I/O op mismatch");
388         
389         ubd_finish(rq, req.error);
390         reactivate_fd(thread_fd, UBD_IRQ);      
391         do_ubd_request(ubd_queue);
392 }
393
394 static void ubd_intr(int irq, void *dev, struct pt_regs *unused)
395 {
396         ubd_handler();
397 }
398
399 /* Only changed by ubd_init, which is an initcall. */
400 static int io_pid = -1;
401
402 void kill_io_thread(void)
403 {
404         if(io_pid != -1) 
405                 os_kill_process(io_pid, 1);
406 }
407
408 __uml_exitcall(kill_io_thread);
409
410 static int ubd_file_size(struct ubd *dev, __u64 *size_out)
411 {
412         char *file;
413
414         file = dev->cow.file ? dev->cow.file : dev->file;
415         return(os_file_size(file, size_out));
416 }
417
418 static void ubd_close(struct ubd *dev)
419 {
420         os_close_file(dev->fd);
421         if(dev->cow.file == NULL)
422                 return;
423
424         os_close_file(dev->cow.fd);
425         vfree(dev->cow.bitmap);
426         dev->cow.bitmap = NULL;
427 }
428
429 static int ubd_open_dev(struct ubd *dev)
430 {
431         struct openflags flags;
432         int err, n, create_cow, *create_ptr;
433
434         create_cow = 0;
435         create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
436         dev->fd = open_ubd_file(dev->file, &dev->openflags, &dev->cow.file,
437                                 &dev->cow.bitmap_offset, &dev->cow.bitmap_len, 
438                                 &dev->cow.data_offset, create_ptr);
439
440         if((dev->fd == -ENOENT) && create_cow){
441                 n = dev - ubd_dev;
442                 dev->fd = create_cow_file(dev->file, dev->cow.file, 
443                                           dev->openflags, 1 << 9,
444                                           &dev->cow.bitmap_offset, 
445                                           &dev->cow.bitmap_len,
446                                           &dev->cow.data_offset);
447                 if(dev->fd >= 0){
448                         printk(KERN_INFO "Creating \"%s\" as COW file for "
449                                "\"%s\"\n", dev->file, dev->cow.file);
450                 }
451         }
452
453         if(dev->fd < 0) return(dev->fd);
454
455         if(dev->cow.file != NULL){
456                 err = -ENOMEM;
457                 dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
458                 if(dev->cow.bitmap == NULL) goto error;
459                 flush_tlb_kernel_vm();
460
461                 err = read_cow_bitmap(dev->fd, dev->cow.bitmap, 
462                                       dev->cow.bitmap_offset, 
463                                       dev->cow.bitmap_len);
464                 if(err) goto error;
465
466                 flags = dev->openflags;
467                 flags.w = 0;
468                 err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL, 
469                                     NULL, NULL);
470                 if(err < 0) goto error;
471                 dev->cow.fd = err;
472         }
473         return(0);
474  error:
475         os_close_file(dev->fd);
476         return(err);
477 }
478
479 static int ubd_new_disk(int major, u64 size, int unit,
480                         struct gendisk **disk_out)
481                         
482 {
483         struct gendisk *disk;
484
485         disk = alloc_disk(1 << UBD_SHIFT);
486         if (!disk)
487                 return -ENOMEM;
488
489         disk->major = major;
490         disk->first_minor = unit << UBD_SHIFT;
491         disk->fops = &ubd_blops;
492         set_capacity(disk, size / 512);
493         sprintf(disk->disk_name, "ubd");
494         sprintf(disk->devfs_name, "ubd/disc%d", unit);
495
496         disk->private_data = &ubd_dev[unit];
497         disk->queue = ubd_queue;
498         add_disk(disk);
499
500         *disk_out = disk;
501         return 0;
502 }
503
504 static int ubd_add(int n)
505 {
506         struct ubd *dev = &ubd_dev[n];
507         int err;
508
509         if(dev->is_dir)
510                 return(-EISDIR);
511
512         if (!dev->file)
513                 return(-ENODEV);
514
515         if (ubd_open_dev(dev))
516                 return(-ENODEV);
517
518         err = ubd_file_size(dev, &dev->size);
519         if(err)
520                 return(err);
521
522         err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
523         if(err) 
524                 return(err);
525  
526         if(fake_major)
527                 ubd_new_disk(fake_major, dev->size, n, 
528                              &fake_gendisk[n]);
529
530         /* perhaps this should also be under the "if (fake_major)" above */
531         /* using the fake_disk->disk_name and also the fakehd_set name */
532         if (fake_ide)
533                 make_ide_entries(ubd_gendisk[n]->disk_name);
534
535         ubd_close(dev);
536         return 0;
537 }
538
539 static int ubd_config(char *str)
540 {
541         int n, err;
542
543         str = uml_strdup(str);
544         if(str == NULL){
545                 printk(KERN_ERR "ubd_config failed to strdup string\n");
546                 return(1);
547         }
548         err = ubd_setup_common(str, &n);
549         if(err){
550                 kfree(str);
551                 return(-1);
552         }
553         if(n == -1) return(0);
554
555         spin_lock(&ubd_lock);
556         err = ubd_add(n);
557         if(err)
558                 ubd_dev[n].file = NULL;
559         spin_unlock(&ubd_lock);
560
561         return(err);
562 }
563
564 static int ubd_get_config(char *dev, char *str, int size, char **error_out)
565 {
566         struct ubd *ubd;
567         char *end;
568         int major, n = 0;
569
570         major = simple_strtoul(dev, &end, 0);
571         if((*end != '\0') || (end == dev)){
572                 *error_out = "ubd_get_config : didn't parse major number";
573                 return(-1);
574         }
575
576         if((major >= MAX_DEV) || (major < 0)){
577                 *error_out = "ubd_get_config : major number out of range";
578                 return(-1);
579         }
580
581         ubd = &ubd_dev[major];
582         spin_lock(&ubd_lock);
583
584         if(ubd->file == NULL){
585                 CONFIG_CHUNK(str, size, n, "", 1);
586                 goto out;
587         }
588
589         CONFIG_CHUNK(str, size, n, ubd->file, 0);
590
591         if(ubd->cow.file != NULL){
592                 CONFIG_CHUNK(str, size, n, ",", 0);
593                 CONFIG_CHUNK(str, size, n, ubd->cow.file, 1);
594         }
595         else CONFIG_CHUNK(str, size, n, "", 1);
596
597  out:
598         spin_unlock(&ubd_lock);
599         return(n);
600 }
601
602 static int ubd_remove(char *str)
603 {
604         struct ubd *dev;
605         int n, err = -ENODEV;
606
607         if(!isdigit(*str))
608                 return(err);    /* it should be a number 0-7/a-h */
609
610         n = *str - '0';
611         if(n >= MAX_DEV) 
612                 return(err);
613
614         dev = &ubd_dev[n];
615         if(dev->count > 0)
616                 return(-EBUSY); /* you cannot remove a open disk */
617
618         err = 0;
619         spin_lock(&ubd_lock);
620
621         if(ubd_gendisk[n] == NULL)
622                 goto out;
623
624         del_gendisk(ubd_gendisk[n]);
625         put_disk(ubd_gendisk[n]);
626         ubd_gendisk[n] = NULL;
627
628         if(fake_gendisk[n] != NULL){
629                 del_gendisk(fake_gendisk[n]);
630                 put_disk(fake_gendisk[n]);
631                 fake_gendisk[n] = NULL;
632         }
633
634         *dev = ((struct ubd) DEFAULT_UBD);
635         err = 0;
636  out:
637         spin_unlock(&ubd_lock);
638         return(err);
639 }
640
641 static struct mc_device ubd_mc = {
642         .name           = "ubd",
643         .config         = ubd_config,
644         .get_config     = ubd_get_config,
645         .remove         = ubd_remove,
646 };
647
648 static int ubd_mc_init(void)
649 {
650         mconsole_register_dev(&ubd_mc);
651         return 0;
652 }
653
654 __initcall(ubd_mc_init);
655
656 int ubd_init(void)
657 {
658         int i;
659
660         devfs_mk_dir("ubd");
661         if (register_blkdev(MAJOR_NR, "ubd"))
662                 return -1;
663
664         ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
665         if (!ubd_queue) {
666                 unregister_blkdev(MAJOR_NR, "ubd");
667                 return -1;
668         }
669                 
670         elevator_init(ubd_queue, &elevator_noop);
671
672         if (fake_major != 0) {
673                 char name[sizeof("ubd_nnn\0")];
674
675                 snprintf(name, sizeof(name), "ubd_%d", fake_major);
676                 devfs_mk_dir(name);
677                 if (register_blkdev(fake_major, "ubd"))
678                         return -1;
679         }
680         for (i = 0; i < MAX_DEV; i++) 
681                 ubd_add(i);
682         return 0;
683 }
684
685 late_initcall(ubd_init);
686
687 int ubd_driver_init(void){
688         unsigned long stack;
689         int err;
690
691         if(global_openflags.s){
692                 printk(KERN_INFO "ubd : Synchronous mode\n");
693                 return(0);
694         }
695         stack = alloc_stack(0, 0);
696         io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *), 
697                                  &thread_fd);
698         if(io_pid < 0){
699                 printk(KERN_ERR 
700                        "ubd : Failed to start I/O thread (errno = %d) - "
701                        "falling back to synchronous I/O\n", -io_pid);
702                 return(0);
703         }
704         err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr, 
705                              SA_INTERRUPT, "ubd", ubd_dev);
706         if(err != 0) printk(KERN_ERR 
707                             "um_request_irq failed - errno = %d\n", -err);
708         return(err);
709 }
710
711 device_initcall(ubd_driver_init);
712
713 static int ubd_open(struct inode *inode, struct file *filp)
714 {
715         struct gendisk *disk = inode->i_bdev->bd_disk;
716         struct ubd *dev = disk->private_data;
717         int err = -EISDIR;
718
719         if(dev->is_dir == 1)
720                 goto out;
721
722         err = 0;
723         if(dev->count == 0){
724                 dev->openflags = dev->boot_openflags;
725
726                 err = ubd_open_dev(dev);
727                 if(err){
728                         printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
729                                disk->disk_name, dev->file, -err);
730                         goto out;
731                 }
732         }
733         dev->count++;
734         if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
735                 if(--dev->count == 0) ubd_close(dev);
736                 err = -EROFS;
737         }
738  out:
739         return(err);
740 }
741
742 static int ubd_release(struct inode * inode, struct file * file)
743 {
744         struct gendisk *disk = inode->i_bdev->bd_disk;
745         struct ubd *dev = disk->private_data;
746
747         if(--dev->count == 0)
748                 ubd_close(dev);
749         return(0);
750 }
751
752 void cowify_req(struct io_thread_req *req, struct ubd *dev)
753 {
754         int i, update_bitmap, sector = req->offset >> 9;
755
756         if(req->length > (sizeof(req->sector_mask) * 8) << 9)
757                 panic("Operation too long");
758         if(req->op == UBD_READ) {
759                 for(i = 0; i < req->length >> 9; i++){
760                         if(ubd_test_bit(sector + i, (unsigned char *) 
761                                         dev->cow.bitmap)){
762                                 ubd_set_bit(i, (unsigned char *) 
763                                             &req->sector_mask);
764                         }
765                 }
766         } 
767         else {
768                 update_bitmap = 0;
769                 for(i = 0; i < req->length >> 9; i++){
770                         ubd_set_bit(i, (unsigned char *) 
771                                     &req->sector_mask);
772                         if(!ubd_test_bit(sector + i, (unsigned char *) 
773                                          dev->cow.bitmap))
774                                 update_bitmap = 1;
775                         ubd_set_bit(sector + i, (unsigned char *) 
776                                     dev->cow.bitmap);
777                 }
778                 if(update_bitmap){
779                         req->cow_offset = sector / (sizeof(unsigned long) * 8);
780                         req->bitmap_words[0] = 
781                                 dev->cow.bitmap[req->cow_offset];
782                         req->bitmap_words[1] = 
783                                 dev->cow.bitmap[req->cow_offset + 1];
784                         req->cow_offset *= sizeof(unsigned long);
785                         req->cow_offset += dev->cow.bitmap_offset;
786                 }
787         }
788 }
789
790 static int prepare_request(struct request *req, struct io_thread_req *io_req)
791 {
792         struct gendisk *disk = req->rq_disk;
793         struct ubd *dev = disk->private_data;
794         __u64 block;
795         int nsect;
796
797         if(req->rq_status == RQ_INACTIVE) return(1);
798
799         if(dev->is_dir){
800                 strcpy(req->buffer, "HOSTFS:");
801                 strcat(req->buffer, dev->file);
802                 spin_lock(&ubd_io_lock);
803                 end_request(req, 1);
804                 spin_unlock(&ubd_io_lock);
805                 return(1);
806         }
807
808         if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
809                 printk("Write attempted on readonly ubd device %s\n", 
810                        disk->disk_name);
811                 spin_lock(&ubd_io_lock);
812                 end_request(req, 0);
813                 spin_unlock(&ubd_io_lock);
814                 return(1);
815         }
816
817         block = req->sector;
818         nsect = req->current_nr_sectors;
819
820         io_req->op = rq_data_dir(req) == READ ? UBD_READ : UBD_WRITE;
821         io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
822         io_req->fds[1] = dev->fd;
823         io_req->offsets[0] = 0;
824         io_req->offsets[1] = dev->cow.data_offset;
825         io_req->offset = ((__u64) block) << 9;
826         io_req->length = nsect << 9;
827         io_req->buffer = req->buffer;
828         io_req->sectorsize = 1 << 9;
829         io_req->sector_mask = 0;
830         io_req->cow_offset = -1;
831         io_req->error = 0;
832
833         if(dev->cow.file != NULL) cowify_req(io_req, dev);
834         return(0);
835 }
836
837 static void do_ubd_request(request_queue_t *q)
838 {
839         struct io_thread_req io_req;
840         struct request *req;
841         int err, n;
842
843         if(thread_fd == -1){
844                 while(!list_empty(&q->queue_head)){
845                         req = elv_next_request(q);
846                         err = prepare_request(req, &io_req);
847                         if(!err){
848                                 do_io(&io_req);
849                                 ubd_finish(req, io_req.error);
850                         }
851                 }
852         }
853         else {
854                 if(do_ubd || list_empty(&q->queue_head)) return;
855                 req = elv_next_request(q);
856                 err = prepare_request(req, &io_req);
857                 if(!err){
858                         do_ubd = ubd_handler;
859                         n = write_ubd_fs(thread_fd, (char *) &io_req, 
860                                          sizeof(io_req));
861                         if(n != sizeof(io_req))
862                                 printk("write to io thread failed, "
863                                        "errno = %d\n", -n);
864                 }
865         }
866 }
867
868 static int ubd_ioctl(struct inode * inode, struct file * file,
869                      unsigned int cmd, unsigned long arg)
870 {
871         struct hd_geometry *loc = (struct hd_geometry *) arg;
872         struct ubd *dev = inode->i_bdev->bd_disk->private_data;
873         int err;
874         struct hd_driveid ubd_id = {
875                 .cyls           = 0,
876                 .heads          = 128,
877                 .sectors        = 32,
878         };
879
880         switch (cmd) {
881                 struct hd_geometry g;
882                 struct cdrom_volctrl volume;
883         case HDIO_GETGEO:
884                 if(!loc) return(-EINVAL);
885                 g.heads = 128;
886                 g.sectors = 32;
887                 g.cylinders = dev->size / (128 * 32 * 512);
888                 g.start = 2;
889                 return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
890
891         case HDIO_SET_UNMASKINTR:
892                 if(!capable(CAP_SYS_ADMIN)) return(-EACCES);
893                 if((arg > 1) || (inode->i_bdev->bd_contains != inode->i_bdev))
894                         return(-EINVAL);
895                 return(0);
896
897         case HDIO_GET_UNMASKINTR:
898                 if(!arg)  return(-EINVAL);
899                 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
900                 if(err)
901                         return(err);
902                 return(0);
903
904         case HDIO_GET_MULTCOUNT:
905                 if(!arg)  return(-EINVAL);
906                 err = verify_area(VERIFY_WRITE, (long *) arg, sizeof(long));
907                 if(err)
908                         return(err);
909                 return(0);
910
911         case HDIO_SET_MULTCOUNT:
912                 if(!capable(CAP_SYS_ADMIN)) return(-EACCES);
913                 if(inode->i_bdev->bd_contains != inode->i_bdev)
914                         return(-EINVAL);
915                 return(0);
916
917         case HDIO_GET_IDENTITY:
918                 ubd_id.cyls = dev->size / (128 * 32 * 512);
919                 if(copy_to_user((char *) arg, (char *) &ubd_id, 
920                                  sizeof(ubd_id)))
921                         return(-EFAULT);
922                 return(0);
923                 
924         case CDROMVOLREAD:
925                 if(copy_from_user(&volume, (char *) arg, sizeof(volume)))
926                         return(-EFAULT);
927                 volume.channel0 = 255;
928                 volume.channel1 = 255;
929                 volume.channel2 = 255;
930                 volume.channel3 = 255;
931                 if(copy_to_user((char *) arg, &volume, sizeof(volume)))
932                         return(-EFAULT);
933                 return(0);
934         }
935         return(-EINVAL);
936 }
937
938 /*
939  * Overrides for Emacs so that we follow Linus's tabbing style.
940  * Emacs will notice this stuff at the end of the file and automatically
941  * adjust the settings for this buffer only.  This must remain at the end
942  * of the file.
943  * ---------------------------------------------------------------------------
944  * Local variables:
945  * c-file-style: "linux"
946  * End:
947  */