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