ubd_kern.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /*
  2. * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. /* 2001-09-28...2002-04-17
  6. * Partition stuff by James_McMechan@hotmail.com
  7. * old style ubd by setting UBD_SHIFT to 0
  8. * 2002-09-27...2002-10-18 massive tinkering for 2.5
  9. * partitions have changed in 2.5
  10. * 2003-01-29 more tinkering for 2.5.59-1
  11. * This should now address the sysfs problems and has
  12. * the symlink for devfs to allow for booting with
  13. * the common /dev/ubd/discX/... names rather than
  14. * only /dev/ubdN/discN this version also has lots of
  15. * clean ups preparing for ubd-many.
  16. * James McMechan
  17. */
  18. #define MAJOR_NR UBD_MAJOR
  19. #define UBD_SHIFT 4
  20. #include "linux/config.h"
  21. #include "linux/module.h"
  22. #include "linux/blkdev.h"
  23. #include "linux/hdreg.h"
  24. #include "linux/init.h"
  25. #include "linux/devfs_fs_kernel.h"
  26. #include "linux/cdrom.h"
  27. #include "linux/proc_fs.h"
  28. #include "linux/ctype.h"
  29. #include "linux/capability.h"
  30. #include "linux/mm.h"
  31. #include "linux/vmalloc.h"
  32. #include "linux/blkpg.h"
  33. #include "linux/genhd.h"
  34. #include "linux/spinlock.h"
  35. #include "linux/platform_device.h"
  36. #include "asm/segment.h"
  37. #include "asm/uaccess.h"
  38. #include "asm/irq.h"
  39. #include "asm/types.h"
  40. #include "asm/tlbflush.h"
  41. #include "user_util.h"
  42. #include "mem_user.h"
  43. #include "kern_util.h"
  44. #include "kern.h"
  45. #include "mconsole_kern.h"
  46. #include "init.h"
  47. #include "irq_user.h"
  48. #include "irq_kern.h"
  49. #include "ubd_user.h"
  50. #include "os.h"
  51. #include "mem.h"
  52. #include "mem_kern.h"
  53. #include "cow.h"
  54. enum ubd_req { UBD_READ, UBD_WRITE };
  55. struct io_thread_req {
  56. enum ubd_req op;
  57. int fds[2];
  58. unsigned long offsets[2];
  59. unsigned long long offset;
  60. unsigned long length;
  61. char *buffer;
  62. int sectorsize;
  63. unsigned long sector_mask;
  64. unsigned long long cow_offset;
  65. unsigned long bitmap_words[2];
  66. int error;
  67. };
  68. extern int open_ubd_file(char *file, struct openflags *openflags,
  69. char **backing_file_out, int *bitmap_offset_out,
  70. unsigned long *bitmap_len_out, int *data_offset_out,
  71. int *create_cow_out);
  72. extern int create_cow_file(char *cow_file, char *backing_file,
  73. struct openflags flags, int sectorsize,
  74. int alignment, int *bitmap_offset_out,
  75. unsigned long *bitmap_len_out,
  76. int *data_offset_out);
  77. extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
  78. extern void do_io(struct io_thread_req *req);
  79. static inline int ubd_test_bit(__u64 bit, unsigned char *data)
  80. {
  81. __u64 n;
  82. int bits, off;
  83. bits = sizeof(data[0]) * 8;
  84. n = bit / bits;
  85. off = bit % bits;
  86. return((data[n] & (1 << off)) != 0);
  87. }
  88. static inline void ubd_set_bit(__u64 bit, unsigned char *data)
  89. {
  90. __u64 n;
  91. int bits, off;
  92. bits = sizeof(data[0]) * 8;
  93. n = bit / bits;
  94. off = bit % bits;
  95. data[n] |= (1 << off);
  96. }
  97. /*End stuff from ubd_user.h*/
  98. #define DRIVER_NAME "uml-blkdev"
  99. static DEFINE_SPINLOCK(ubd_io_lock);
  100. static DEFINE_SPINLOCK(ubd_lock);
  101. static void (*do_ubd)(void);
  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. #define MAX_DEV (8)
  108. static struct block_device_operations ubd_blops = {
  109. .owner = THIS_MODULE,
  110. .open = ubd_open,
  111. .release = ubd_release,
  112. .ioctl = ubd_ioctl,
  113. .getgeo = ubd_getgeo,
  114. };
  115. /* Protected by the queue_lock */
  116. static request_queue_t *ubd_queue;
  117. /* Protected by ubd_lock */
  118. static int fake_major = MAJOR_NR;
  119. static struct gendisk *ubd_gendisk[MAX_DEV];
  120. static struct gendisk *fake_gendisk[MAX_DEV];
  121. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  122. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  123. .cl = 1 })
  124. #else
  125. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  126. .cl = 1 })
  127. #endif
  128. /* Not protected - changed only in ubd_setup_common and then only to
  129. * to enable O_SYNC.
  130. */
  131. static struct openflags global_openflags = OPEN_FLAGS;
  132. struct cow {
  133. /* This is the backing file, actually */
  134. char *file;
  135. int fd;
  136. unsigned long *bitmap;
  137. unsigned long bitmap_len;
  138. int bitmap_offset;
  139. int data_offset;
  140. };
  141. struct ubd {
  142. char *file;
  143. int count;
  144. int fd;
  145. __u64 size;
  146. struct openflags boot_openflags;
  147. struct openflags openflags;
  148. int no_cow;
  149. struct cow cow;
  150. struct platform_device pdev;
  151. };
  152. #define DEFAULT_COW { \
  153. .file = NULL, \
  154. .fd = -1, \
  155. .bitmap = NULL, \
  156. .bitmap_offset = 0, \
  157. .data_offset = 0, \
  158. }
  159. #define DEFAULT_UBD { \
  160. .file = NULL, \
  161. .count = 0, \
  162. .fd = -1, \
  163. .size = -1, \
  164. .boot_openflags = OPEN_FLAGS, \
  165. .openflags = OPEN_FLAGS, \
  166. .no_cow = 0, \
  167. .cow = DEFAULT_COW, \
  168. }
  169. struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
  170. static int ubd0_init(void)
  171. {
  172. struct ubd *dev = &ubd_dev[0];
  173. if(dev->file == NULL)
  174. dev->file = "root_fs";
  175. return(0);
  176. }
  177. __initcall(ubd0_init);
  178. /* Only changed by fake_ide_setup which is a setup */
  179. static int fake_ide = 0;
  180. static struct proc_dir_entry *proc_ide_root = NULL;
  181. static struct proc_dir_entry *proc_ide = NULL;
  182. static void make_proc_ide(void)
  183. {
  184. proc_ide_root = proc_mkdir("ide", NULL);
  185. proc_ide = proc_mkdir("ide0", proc_ide_root);
  186. }
  187. static int proc_ide_read_media(char *page, char **start, off_t off, int count,
  188. int *eof, void *data)
  189. {
  190. int len;
  191. strcpy(page, "disk\n");
  192. len = strlen("disk\n");
  193. len -= off;
  194. if (len < count){
  195. *eof = 1;
  196. if (len <= 0) return 0;
  197. }
  198. else len = count;
  199. *start = page + off;
  200. return len;
  201. }
  202. static void make_ide_entries(char *dev_name)
  203. {
  204. struct proc_dir_entry *dir, *ent;
  205. char name[64];
  206. if(proc_ide_root == NULL) make_proc_ide();
  207. dir = proc_mkdir(dev_name, proc_ide);
  208. if(!dir) return;
  209. ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
  210. if(!ent) return;
  211. ent->nlink = 1;
  212. ent->data = NULL;
  213. ent->read_proc = proc_ide_read_media;
  214. ent->write_proc = NULL;
  215. sprintf(name,"ide0/%s", dev_name);
  216. proc_symlink(dev_name, proc_ide_root, name);
  217. }
  218. static int fake_ide_setup(char *str)
  219. {
  220. fake_ide = 1;
  221. return(1);
  222. }
  223. __setup("fake_ide", fake_ide_setup);
  224. __uml_help(fake_ide_setup,
  225. "fake_ide\n"
  226. " Create ide0 entries that map onto ubd devices.\n\n"
  227. );
  228. static int parse_unit(char **ptr)
  229. {
  230. char *str = *ptr, *end;
  231. int n = -1;
  232. if(isdigit(*str)) {
  233. n = simple_strtoul(str, &end, 0);
  234. if(end == str)
  235. return(-1);
  236. *ptr = end;
  237. }
  238. else if (('a' <= *str) && (*str <= 'h')) {
  239. n = *str - 'a';
  240. str++;
  241. *ptr = str;
  242. }
  243. return(n);
  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, i;
  251. if(index_out) *index_out = -1;
  252. n = *str;
  253. if(n == '='){
  254. char *end;
  255. int major;
  256. str++;
  257. if(!strcmp(str, "sync")){
  258. global_openflags = of_sync(global_openflags);
  259. return(0);
  260. }
  261. major = simple_strtoul(str, &end, 0);
  262. if((*end != '\0') || (end == str)){
  263. printk(KERN_ERR
  264. "ubd_setup : didn't parse major number\n");
  265. return(1);
  266. }
  267. err = 1;
  268. spin_lock(&ubd_lock);
  269. if(fake_major != MAJOR_NR){
  270. printk(KERN_ERR "Can't assign a fake major twice\n");
  271. goto out1;
  272. }
  273. fake_major = major;
  274. printk(KERN_INFO "Setting extra ubd major number to %d\n",
  275. major);
  276. err = 0;
  277. out1:
  278. spin_unlock(&ubd_lock);
  279. return(err);
  280. }
  281. n = parse_unit(&str);
  282. if(n < 0){
  283. printk(KERN_ERR "ubd_setup : couldn't parse unit number "
  284. "'%s'\n", str);
  285. return(1);
  286. }
  287. if(n >= MAX_DEV){
  288. printk(KERN_ERR "ubd_setup : index %d out of range "
  289. "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
  290. return(1);
  291. }
  292. err = 1;
  293. spin_lock(&ubd_lock);
  294. dev = &ubd_dev[n];
  295. if(dev->file != NULL){
  296. printk(KERN_ERR "ubd_setup : device already configured\n");
  297. goto out;
  298. }
  299. if (index_out)
  300. *index_out = n;
  301. for (i = 0; i < 4; i++) {
  302. switch (*str) {
  303. case 'r':
  304. flags.w = 0;
  305. break;
  306. case 's':
  307. flags.s = 1;
  308. break;
  309. case 'd':
  310. dev->no_cow = 1;
  311. break;
  312. case '=':
  313. str++;
  314. goto break_loop;
  315. default:
  316. printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r,s or d)\n");
  317. goto out;
  318. }
  319. str++;
  320. }
  321. if (*str == '=')
  322. printk(KERN_ERR "ubd_setup : Too many flags specified\n");
  323. else
  324. printk(KERN_ERR "ubd_setup : Expected '='\n");
  325. goto out;
  326. break_loop:
  327. err = 0;
  328. backing_file = strchr(str, ',');
  329. if (!backing_file) {
  330. backing_file = strchr(str, ':');
  331. }
  332. if(backing_file){
  333. if(dev->no_cow)
  334. printk(KERN_ERR "Can't specify both 'd' and a "
  335. "cow file\n");
  336. else {
  337. *backing_file = '\0';
  338. backing_file++;
  339. }
  340. }
  341. dev->file = str;
  342. dev->cow.file = backing_file;
  343. dev->boot_openflags = flags;
  344. out:
  345. spin_unlock(&ubd_lock);
  346. return(err);
  347. }
  348. static int ubd_setup(char *str)
  349. {
  350. ubd_setup_common(str, NULL);
  351. return(1);
  352. }
  353. __setup("ubd", ubd_setup);
  354. __uml_help(ubd_setup,
  355. "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
  356. " This is used to associate a device with a file in the underlying\n"
  357. " filesystem. When specifying two filenames, the first one is the\n"
  358. " COW name and the second is the backing file name. As separator you can\n"
  359. " use either a ':' or a ',': the first one allows writing things like;\n"
  360. " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
  361. " while with a ',' the shell would not expand the 2nd '~'.\n"
  362. " When using only one filename, UML will detect whether to thread it like\n"
  363. " a COW file or a backing file. To override this detection, add the 'd'\n"
  364. " flag:\n"
  365. " ubd0d=BackingFile\n"
  366. " Usually, there is a filesystem in the file, but \n"
  367. " that's not required. Swap devices containing swap files can be\n"
  368. " specified like this. Also, a file which doesn't contain a\n"
  369. " filesystem can have its contents read in the virtual \n"
  370. " machine by running 'dd' on the device. <n> must be in the range\n"
  371. " 0 to 7. Appending an 'r' to the number will cause that device\n"
  372. " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
  373. " an 's' will cause data to be written to disk on the host immediately.\n\n"
  374. );
  375. static int udb_setup(char *str)
  376. {
  377. printk("udb%s specified on command line is almost certainly a ubd -> "
  378. "udb TYPO\n", str);
  379. return(1);
  380. }
  381. __setup("udb", udb_setup);
  382. __uml_help(udb_setup,
  383. "udb\n"
  384. " This option is here solely to catch ubd -> udb typos, which can be\n"
  385. " to impossible to catch visually unless you specifically look for\n"
  386. " them. The only result of any option starting with 'udb' is an error\n"
  387. " in the boot output.\n\n"
  388. );
  389. static int fakehd_set = 0;
  390. static int fakehd(char *str)
  391. {
  392. printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
  393. fakehd_set = 1;
  394. return 1;
  395. }
  396. __setup("fakehd", fakehd);
  397. __uml_help(fakehd,
  398. "fakehd\n"
  399. " Change the ubd device name to \"hd\".\n\n"
  400. );
  401. static void do_ubd_request(request_queue_t * q);
  402. /* Only changed by ubd_init, which is an initcall. */
  403. int thread_fd = -1;
  404. /* Changed by ubd_handler, which is serialized because interrupts only
  405. * happen on CPU 0.
  406. */
  407. int intr_count = 0;
  408. /* call ubd_finish if you need to serialize */
  409. static void __ubd_finish(struct request *req, int error)
  410. {
  411. int nsect;
  412. if(error){
  413. end_request(req, 0);
  414. return;
  415. }
  416. nsect = req->current_nr_sectors;
  417. req->sector += nsect;
  418. req->buffer += nsect << 9;
  419. req->errors = 0;
  420. req->nr_sectors -= nsect;
  421. req->current_nr_sectors = 0;
  422. end_request(req, 1);
  423. }
  424. static inline void ubd_finish(struct request *req, int error)
  425. {
  426. spin_lock(&ubd_io_lock);
  427. __ubd_finish(req, error);
  428. spin_unlock(&ubd_io_lock);
  429. }
  430. /* Called without ubd_io_lock held */
  431. static void ubd_handler(void)
  432. {
  433. struct io_thread_req req;
  434. struct request *rq = elv_next_request(ubd_queue);
  435. int n;
  436. do_ubd = NULL;
  437. intr_count++;
  438. n = os_read_file(thread_fd, &req, sizeof(req));
  439. if(n != sizeof(req)){
  440. printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
  441. "err = %d\n", os_getpid(), -n);
  442. spin_lock(&ubd_io_lock);
  443. end_request(rq, 0);
  444. spin_unlock(&ubd_io_lock);
  445. return;
  446. }
  447. ubd_finish(rq, req.error);
  448. reactivate_fd(thread_fd, UBD_IRQ);
  449. do_ubd_request(ubd_queue);
  450. }
  451. static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
  452. {
  453. ubd_handler();
  454. return(IRQ_HANDLED);
  455. }
  456. /* Only changed by ubd_init, which is an initcall. */
  457. static int io_pid = -1;
  458. void kill_io_thread(void)
  459. {
  460. if(io_pid != -1)
  461. os_kill_process(io_pid, 1);
  462. }
  463. __uml_exitcall(kill_io_thread);
  464. static int ubd_file_size(struct ubd *dev, __u64 *size_out)
  465. {
  466. char *file;
  467. file = dev->cow.file ? dev->cow.file : dev->file;
  468. return(os_file_size(file, size_out));
  469. }
  470. static void ubd_close(struct ubd *dev)
  471. {
  472. os_close_file(dev->fd);
  473. if(dev->cow.file == NULL)
  474. return;
  475. os_close_file(dev->cow.fd);
  476. vfree(dev->cow.bitmap);
  477. dev->cow.bitmap = NULL;
  478. }
  479. static int ubd_open_dev(struct ubd *dev)
  480. {
  481. struct openflags flags;
  482. char **back_ptr;
  483. int err, create_cow, *create_ptr;
  484. dev->openflags = dev->boot_openflags;
  485. create_cow = 0;
  486. create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
  487. back_ptr = dev->no_cow ? NULL : &dev->cow.file;
  488. dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
  489. &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
  490. &dev->cow.data_offset, create_ptr);
  491. if((dev->fd == -ENOENT) && create_cow){
  492. dev->fd = create_cow_file(dev->file, dev->cow.file,
  493. dev->openflags, 1 << 9, PAGE_SIZE,
  494. &dev->cow.bitmap_offset,
  495. &dev->cow.bitmap_len,
  496. &dev->cow.data_offset);
  497. if(dev->fd >= 0){
  498. printk(KERN_INFO "Creating \"%s\" as COW file for "
  499. "\"%s\"\n", dev->file, dev->cow.file);
  500. }
  501. }
  502. if(dev->fd < 0){
  503. printk("Failed to open '%s', errno = %d\n", dev->file,
  504. -dev->fd);
  505. return(dev->fd);
  506. }
  507. if(dev->cow.file != NULL){
  508. err = -ENOMEM;
  509. dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
  510. if(dev->cow.bitmap == NULL){
  511. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  512. goto error;
  513. }
  514. flush_tlb_kernel_vm();
  515. err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
  516. dev->cow.bitmap_offset,
  517. dev->cow.bitmap_len);
  518. if(err < 0)
  519. goto error;
  520. flags = dev->openflags;
  521. flags.w = 0;
  522. err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
  523. NULL, NULL);
  524. if(err < 0) goto error;
  525. dev->cow.fd = err;
  526. }
  527. return(0);
  528. error:
  529. os_close_file(dev->fd);
  530. return(err);
  531. }
  532. static int ubd_new_disk(int major, u64 size, int unit,
  533. struct gendisk **disk_out)
  534. {
  535. struct gendisk *disk;
  536. char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
  537. int err;
  538. disk = alloc_disk(1 << UBD_SHIFT);
  539. if(disk == NULL)
  540. return(-ENOMEM);
  541. disk->major = major;
  542. disk->first_minor = unit << UBD_SHIFT;
  543. disk->fops = &ubd_blops;
  544. set_capacity(disk, size / 512);
  545. if(major == MAJOR_NR){
  546. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  547. sprintf(disk->devfs_name, "ubd/disc%d", unit);
  548. sprintf(from, "ubd/%d", unit);
  549. sprintf(to, "disc%d/disc", unit);
  550. err = devfs_mk_symlink(from, to);
  551. if(err)
  552. printk("ubd_new_disk failed to make link from %s to "
  553. "%s, error = %d\n", from, to, err);
  554. }
  555. else {
  556. sprintf(disk->disk_name, "ubd_fake%d", unit);
  557. sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
  558. }
  559. /* sysfs register (not for ide fake devices) */
  560. if (major == MAJOR_NR) {
  561. ubd_dev[unit].pdev.id = unit;
  562. ubd_dev[unit].pdev.name = DRIVER_NAME;
  563. platform_device_register(&ubd_dev[unit].pdev);
  564. disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
  565. }
  566. disk->private_data = &ubd_dev[unit];
  567. disk->queue = ubd_queue;
  568. add_disk(disk);
  569. *disk_out = disk;
  570. return 0;
  571. }
  572. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  573. static int ubd_add(int n)
  574. {
  575. struct ubd *dev = &ubd_dev[n];
  576. int err;
  577. err = -ENODEV;
  578. if(dev->file == NULL)
  579. goto out;
  580. if (ubd_open_dev(dev))
  581. goto out;
  582. err = ubd_file_size(dev, &dev->size);
  583. if(err < 0)
  584. goto out_close;
  585. dev->size = ROUND_BLOCK(dev->size);
  586. err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
  587. if(err)
  588. goto out_close;
  589. if(fake_major != MAJOR_NR)
  590. ubd_new_disk(fake_major, dev->size, n,
  591. &fake_gendisk[n]);
  592. /* perhaps this should also be under the "if (fake_major)" above */
  593. /* using the fake_disk->disk_name and also the fakehd_set name */
  594. if (fake_ide)
  595. make_ide_entries(ubd_gendisk[n]->disk_name);
  596. err = 0;
  597. out_close:
  598. ubd_close(dev);
  599. out:
  600. return err;
  601. }
  602. static int ubd_config(char *str)
  603. {
  604. int n, err;
  605. str = kstrdup(str, GFP_KERNEL);
  606. if(str == NULL){
  607. printk(KERN_ERR "ubd_config failed to strdup string\n");
  608. return(1);
  609. }
  610. err = ubd_setup_common(str, &n);
  611. if(err){
  612. kfree(str);
  613. return(-1);
  614. }
  615. if(n == -1) return(0);
  616. spin_lock(&ubd_lock);
  617. err = ubd_add(n);
  618. if(err)
  619. ubd_dev[n].file = NULL;
  620. spin_unlock(&ubd_lock);
  621. return(err);
  622. }
  623. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  624. {
  625. struct ubd *dev;
  626. int n, len = 0;
  627. n = parse_unit(&name);
  628. if((n >= MAX_DEV) || (n < 0)){
  629. *error_out = "ubd_get_config : device number out of range";
  630. return(-1);
  631. }
  632. dev = &ubd_dev[n];
  633. spin_lock(&ubd_lock);
  634. if(dev->file == NULL){
  635. CONFIG_CHUNK(str, size, len, "", 1);
  636. goto out;
  637. }
  638. CONFIG_CHUNK(str, size, len, dev->file, 0);
  639. if(dev->cow.file != NULL){
  640. CONFIG_CHUNK(str, size, len, ",", 0);
  641. CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
  642. }
  643. else CONFIG_CHUNK(str, size, len, "", 1);
  644. out:
  645. spin_unlock(&ubd_lock);
  646. return(len);
  647. }
  648. static int ubd_id(char **str, int *start_out, int *end_out)
  649. {
  650. int n;
  651. n = parse_unit(str);
  652. *start_out = 0;
  653. *end_out = MAX_DEV - 1;
  654. return n;
  655. }
  656. static int ubd_remove(int n)
  657. {
  658. struct ubd *dev;
  659. int err = -ENODEV;
  660. spin_lock(&ubd_lock);
  661. if(ubd_gendisk[n] == NULL)
  662. goto out;
  663. dev = &ubd_dev[n];
  664. if(dev->file == NULL)
  665. goto out;
  666. /* you cannot remove a open disk */
  667. err = -EBUSY;
  668. if(dev->count > 0)
  669. goto out;
  670. del_gendisk(ubd_gendisk[n]);
  671. put_disk(ubd_gendisk[n]);
  672. ubd_gendisk[n] = NULL;
  673. if(fake_gendisk[n] != NULL){
  674. del_gendisk(fake_gendisk[n]);
  675. put_disk(fake_gendisk[n]);
  676. fake_gendisk[n] = NULL;
  677. }
  678. platform_device_unregister(&dev->pdev);
  679. *dev = ((struct ubd) DEFAULT_UBD);
  680. err = 0;
  681. out:
  682. spin_unlock(&ubd_lock);
  683. return err;
  684. }
  685. static struct mc_device ubd_mc = {
  686. .name = "ubd",
  687. .config = ubd_config,
  688. .get_config = ubd_get_config,
  689. .id = ubd_id,
  690. .remove = ubd_remove,
  691. };
  692. static int ubd_mc_init(void)
  693. {
  694. mconsole_register_dev(&ubd_mc);
  695. return 0;
  696. }
  697. __initcall(ubd_mc_init);
  698. static struct platform_driver ubd_driver = {
  699. .driver = {
  700. .name = DRIVER_NAME,
  701. },
  702. };
  703. int ubd_init(void)
  704. {
  705. int i;
  706. devfs_mk_dir("ubd");
  707. if (register_blkdev(MAJOR_NR, "ubd"))
  708. return -1;
  709. ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
  710. if (!ubd_queue) {
  711. unregister_blkdev(MAJOR_NR, "ubd");
  712. return -1;
  713. }
  714. if (fake_major != MAJOR_NR) {
  715. char name[sizeof("ubd_nnn\0")];
  716. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  717. devfs_mk_dir(name);
  718. if (register_blkdev(fake_major, "ubd"))
  719. return -1;
  720. }
  721. platform_driver_register(&ubd_driver);
  722. for (i = 0; i < MAX_DEV; i++)
  723. ubd_add(i);
  724. return 0;
  725. }
  726. late_initcall(ubd_init);
  727. int ubd_driver_init(void){
  728. unsigned long stack;
  729. int err;
  730. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  731. if(global_openflags.s){
  732. printk(KERN_INFO "ubd: Synchronous mode\n");
  733. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  734. * enough. So use anyway the io thread. */
  735. }
  736. stack = alloc_stack(0, 0);
  737. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  738. &thread_fd);
  739. if(io_pid < 0){
  740. printk(KERN_ERR
  741. "ubd : Failed to start I/O thread (errno = %d) - "
  742. "falling back to synchronous I/O\n", -io_pid);
  743. io_pid = -1;
  744. return(0);
  745. }
  746. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  747. SA_INTERRUPT, "ubd", ubd_dev);
  748. if(err != 0)
  749. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  750. return(err);
  751. }
  752. device_initcall(ubd_driver_init);
  753. static int ubd_open(struct inode *inode, struct file *filp)
  754. {
  755. struct gendisk *disk = inode->i_bdev->bd_disk;
  756. struct ubd *dev = disk->private_data;
  757. int err = 0;
  758. if(dev->count == 0){
  759. err = ubd_open_dev(dev);
  760. if(err){
  761. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  762. disk->disk_name, dev->file, -err);
  763. goto out;
  764. }
  765. }
  766. dev->count++;
  767. set_disk_ro(disk, !dev->openflags.w);
  768. /* This should no more be needed. And it didn't work anyway to exclude
  769. * read-write remounting of filesystems.*/
  770. /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
  771. if(--dev->count == 0) ubd_close(dev);
  772. err = -EROFS;
  773. }*/
  774. out:
  775. return(err);
  776. }
  777. static int ubd_release(struct inode * inode, struct file * file)
  778. {
  779. struct gendisk *disk = inode->i_bdev->bd_disk;
  780. struct ubd *dev = disk->private_data;
  781. if(--dev->count == 0)
  782. ubd_close(dev);
  783. return(0);
  784. }
  785. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  786. __u64 *cow_offset, unsigned long *bitmap,
  787. __u64 bitmap_offset, unsigned long *bitmap_words,
  788. __u64 bitmap_len)
  789. {
  790. __u64 sector = io_offset >> 9;
  791. int i, update_bitmap = 0;
  792. for(i = 0; i < length >> 9; i++){
  793. if(cow_mask != NULL)
  794. ubd_set_bit(i, (unsigned char *) cow_mask);
  795. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  796. continue;
  797. update_bitmap = 1;
  798. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  799. }
  800. if(!update_bitmap)
  801. return;
  802. *cow_offset = sector / (sizeof(unsigned long) * 8);
  803. /* This takes care of the case where we're exactly at the end of the
  804. * device, and *cow_offset + 1 is off the end. So, just back it up
  805. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  806. * for the original diagnosis.
  807. */
  808. if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
  809. sizeof(unsigned long) - 1))
  810. (*cow_offset)--;
  811. bitmap_words[0] = bitmap[*cow_offset];
  812. bitmap_words[1] = bitmap[*cow_offset + 1];
  813. *cow_offset *= sizeof(unsigned long);
  814. *cow_offset += bitmap_offset;
  815. }
  816. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  817. __u64 bitmap_offset, __u64 bitmap_len)
  818. {
  819. __u64 sector = req->offset >> 9;
  820. int i;
  821. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  822. panic("Operation too long");
  823. if(req->op == UBD_READ) {
  824. for(i = 0; i < req->length >> 9; i++){
  825. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  826. ubd_set_bit(i, (unsigned char *)
  827. &req->sector_mask);
  828. }
  829. }
  830. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  831. &req->cow_offset, bitmap, bitmap_offset,
  832. req->bitmap_words, bitmap_len);
  833. }
  834. /* Called with ubd_io_lock held */
  835. static int prepare_request(struct request *req, struct io_thread_req *io_req)
  836. {
  837. struct gendisk *disk = req->rq_disk;
  838. struct ubd *dev = disk->private_data;
  839. __u64 offset;
  840. int len;
  841. if(req->rq_status == RQ_INACTIVE) return(1);
  842. /* This should be impossible now */
  843. if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
  844. printk("Write attempted on readonly ubd device %s\n",
  845. disk->disk_name);
  846. end_request(req, 0);
  847. return(1);
  848. }
  849. offset = ((__u64) req->sector) << 9;
  850. len = req->current_nr_sectors << 9;
  851. io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
  852. io_req->fds[1] = dev->fd;
  853. io_req->cow_offset = -1;
  854. io_req->offset = offset;
  855. io_req->length = len;
  856. io_req->error = 0;
  857. io_req->sector_mask = 0;
  858. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  859. io_req->offsets[0] = 0;
  860. io_req->offsets[1] = dev->cow.data_offset;
  861. io_req->buffer = req->buffer;
  862. io_req->sectorsize = 1 << 9;
  863. if(dev->cow.file != NULL)
  864. cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
  865. dev->cow.bitmap_len);
  866. return(0);
  867. }
  868. /* Called with ubd_io_lock held */
  869. static void do_ubd_request(request_queue_t *q)
  870. {
  871. struct io_thread_req io_req;
  872. struct request *req;
  873. int err, n;
  874. if(thread_fd == -1){
  875. while((req = elv_next_request(q)) != NULL){
  876. err = prepare_request(req, &io_req);
  877. if(!err){
  878. do_io(&io_req);
  879. __ubd_finish(req, io_req.error);
  880. }
  881. }
  882. }
  883. else {
  884. if(do_ubd || (req = elv_next_request(q)) == NULL)
  885. return;
  886. err = prepare_request(req, &io_req);
  887. if(!err){
  888. do_ubd = ubd_handler;
  889. n = os_write_file(thread_fd, (char *) &io_req,
  890. sizeof(io_req));
  891. if(n != sizeof(io_req))
  892. printk("write to io thread failed, "
  893. "errno = %d\n", -n);
  894. }
  895. }
  896. }
  897. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  898. {
  899. struct ubd *dev = bdev->bd_disk->private_data;
  900. geo->heads = 128;
  901. geo->sectors = 32;
  902. geo->cylinders = dev->size / (128 * 32 * 512);
  903. return 0;
  904. }
  905. static int ubd_ioctl(struct inode * inode, struct file * file,
  906. unsigned int cmd, unsigned long arg)
  907. {
  908. struct ubd *dev = inode->i_bdev->bd_disk->private_data;
  909. struct hd_driveid ubd_id = {
  910. .cyls = 0,
  911. .heads = 128,
  912. .sectors = 32,
  913. };
  914. switch (cmd) {
  915. struct cdrom_volctrl volume;
  916. case HDIO_GET_IDENTITY:
  917. ubd_id.cyls = dev->size / (128 * 32 * 512);
  918. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  919. sizeof(ubd_id)))
  920. return(-EFAULT);
  921. return(0);
  922. case CDROMVOLREAD:
  923. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  924. return(-EFAULT);
  925. volume.channel0 = 255;
  926. volume.channel1 = 255;
  927. volume.channel2 = 255;
  928. volume.channel3 = 255;
  929. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  930. return(-EFAULT);
  931. return(0);
  932. }
  933. return(-EINVAL);
  934. }
  935. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  936. {
  937. struct uml_stat buf1, buf2;
  938. int err;
  939. if(from_cmdline == NULL)
  940. return 0;
  941. if(!strcmp(from_cmdline, from_cow))
  942. return 0;
  943. err = os_stat_file(from_cmdline, &buf1);
  944. if(err < 0){
  945. printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
  946. return 0;
  947. }
  948. err = os_stat_file(from_cow, &buf2);
  949. if(err < 0){
  950. printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
  951. return 1;
  952. }
  953. if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  954. return 0;
  955. printk("Backing file mismatch - \"%s\" requested,\n"
  956. "\"%s\" specified in COW header of \"%s\"\n",
  957. from_cmdline, from_cow, cow);
  958. return 1;
  959. }
  960. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  961. {
  962. unsigned long modtime;
  963. long long actual;
  964. int err;
  965. err = os_file_modtime(file, &modtime);
  966. if(err < 0){
  967. printk("Failed to get modification time of backing file "
  968. "\"%s\", err = %d\n", file, -err);
  969. return(err);
  970. }
  971. err = os_file_size(file, &actual);
  972. if(err < 0){
  973. printk("Failed to get size of backing file \"%s\", "
  974. "err = %d\n", file, -err);
  975. return(err);
  976. }
  977. if(actual != size){
  978. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  979. * the typecast.*/
  980. printk("Size mismatch (%llu vs %llu) of COW header vs backing "
  981. "file\n", (unsigned long long) size, actual);
  982. return(-EINVAL);
  983. }
  984. if(modtime != mtime){
  985. printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
  986. "file\n", mtime, modtime);
  987. return(-EINVAL);
  988. }
  989. return(0);
  990. }
  991. int read_cow_bitmap(int fd, void *buf, int offset, int len)
  992. {
  993. int err;
  994. err = os_seek_file(fd, offset);
  995. if(err < 0)
  996. return(err);
  997. err = os_read_file(fd, buf, len);
  998. if(err < 0)
  999. return(err);
  1000. return(0);
  1001. }
  1002. int open_ubd_file(char *file, struct openflags *openflags,
  1003. char **backing_file_out, int *bitmap_offset_out,
  1004. unsigned long *bitmap_len_out, int *data_offset_out,
  1005. int *create_cow_out)
  1006. {
  1007. time_t mtime;
  1008. unsigned long long size;
  1009. __u32 version, align;
  1010. char *backing_file;
  1011. int fd, err, sectorsize, asked_switch, mode = 0644;
  1012. fd = os_open_file(file, *openflags, mode);
  1013. if (fd < 0) {
  1014. if ((fd == -ENOENT) && (create_cow_out != NULL))
  1015. *create_cow_out = 1;
  1016. if (!openflags->w ||
  1017. ((fd != -EROFS) && (fd != -EACCES)))
  1018. return fd;
  1019. openflags->w = 0;
  1020. fd = os_open_file(file, *openflags, mode);
  1021. if (fd < 0)
  1022. return fd;
  1023. }
  1024. err = os_lock_file(fd, openflags->w);
  1025. if(err < 0){
  1026. printk("Failed to lock '%s', err = %d\n", file, -err);
  1027. goto out_close;
  1028. }
  1029. /* Succesful return case! */
  1030. if(backing_file_out == NULL)
  1031. return(fd);
  1032. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  1033. &size, &sectorsize, &align, bitmap_offset_out);
  1034. if(err && (*backing_file_out != NULL)){
  1035. printk("Failed to read COW header from COW file \"%s\", "
  1036. "errno = %d\n", file, -err);
  1037. goto out_close;
  1038. }
  1039. if(err)
  1040. return(fd);
  1041. asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
  1042. /* Allow switching only if no mismatch. */
  1043. if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
  1044. printk("Switching backing file to '%s'\n", *backing_file_out);
  1045. err = write_cow_header(file, fd, *backing_file_out,
  1046. sectorsize, align, &size);
  1047. if (err) {
  1048. printk("Switch failed, errno = %d\n", -err);
  1049. goto out_close;
  1050. }
  1051. } else {
  1052. *backing_file_out = backing_file;
  1053. err = backing_file_mismatch(*backing_file_out, size, mtime);
  1054. if (err)
  1055. goto out_close;
  1056. }
  1057. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  1058. bitmap_len_out, data_offset_out);
  1059. return fd;
  1060. out_close:
  1061. os_close_file(fd);
  1062. return err;
  1063. }
  1064. int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
  1065. int sectorsize, int alignment, int *bitmap_offset_out,
  1066. unsigned long *bitmap_len_out, int *data_offset_out)
  1067. {
  1068. int err, fd;
  1069. flags.c = 1;
  1070. fd = open_ubd_file(cow_file, &flags, NULL, NULL, NULL, NULL, NULL);
  1071. if(fd < 0){
  1072. err = fd;
  1073. printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
  1074. -err);
  1075. goto out;
  1076. }
  1077. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  1078. bitmap_offset_out, bitmap_len_out,
  1079. data_offset_out);
  1080. if(!err)
  1081. return(fd);
  1082. os_close_file(fd);
  1083. out:
  1084. return(err);
  1085. }
  1086. static int update_bitmap(struct io_thread_req *req)
  1087. {
  1088. int n;
  1089. if(req->cow_offset == -1)
  1090. return(0);
  1091. n = os_seek_file(req->fds[1], req->cow_offset);
  1092. if(n < 0){
  1093. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1094. return(1);
  1095. }
  1096. n = os_write_file(req->fds[1], &req->bitmap_words,
  1097. sizeof(req->bitmap_words));
  1098. if(n != sizeof(req->bitmap_words)){
  1099. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1100. req->fds[1]);
  1101. return(1);
  1102. }
  1103. return(0);
  1104. }
  1105. void do_io(struct io_thread_req *req)
  1106. {
  1107. char *buf;
  1108. unsigned long len;
  1109. int n, nsectors, start, end, bit;
  1110. int err;
  1111. __u64 off;
  1112. nsectors = req->length / req->sectorsize;
  1113. start = 0;
  1114. do {
  1115. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1116. end = start;
  1117. while((end < nsectors) &&
  1118. (ubd_test_bit(end, (unsigned char *)
  1119. &req->sector_mask) == bit))
  1120. end++;
  1121. off = req->offset + req->offsets[bit] +
  1122. start * req->sectorsize;
  1123. len = (end - start) * req->sectorsize;
  1124. buf = &req->buffer[start * req->sectorsize];
  1125. err = os_seek_file(req->fds[bit], off);
  1126. if(err < 0){
  1127. printk("do_io - lseek failed : err = %d\n", -err);
  1128. req->error = 1;
  1129. return;
  1130. }
  1131. if(req->op == UBD_READ){
  1132. n = 0;
  1133. do {
  1134. buf = &buf[n];
  1135. len -= n;
  1136. n = os_read_file(req->fds[bit], buf, len);
  1137. if (n < 0) {
  1138. printk("do_io - read failed, err = %d "
  1139. "fd = %d\n", -n, req->fds[bit]);
  1140. req->error = 1;
  1141. return;
  1142. }
  1143. } while((n < len) && (n != 0));
  1144. if (n < len) memset(&buf[n], 0, len - n);
  1145. } else {
  1146. n = os_write_file(req->fds[bit], buf, len);
  1147. if(n != len){
  1148. printk("do_io - write failed err = %d "
  1149. "fd = %d\n", -n, req->fds[bit]);
  1150. req->error = 1;
  1151. return;
  1152. }
  1153. }
  1154. start = end;
  1155. } while(start < nsectors);
  1156. req->error = update_bitmap(req);
  1157. }
  1158. /* Changed in start_io_thread, which is serialized by being called only
  1159. * from ubd_init, which is an initcall.
  1160. */
  1161. int kernel_fd = -1;
  1162. /* Only changed by the io thread */
  1163. int io_count = 0;
  1164. int io_thread(void *arg)
  1165. {
  1166. struct io_thread_req req;
  1167. int n;
  1168. ignore_sigwinch_sig();
  1169. while(1){
  1170. n = os_read_file(kernel_fd, &req, sizeof(req));
  1171. if(n != sizeof(req)){
  1172. if(n < 0)
  1173. printk("io_thread - read failed, fd = %d, "
  1174. "err = %d\n", kernel_fd, -n);
  1175. else {
  1176. printk("io_thread - short read, fd = %d, "
  1177. "length = %d\n", kernel_fd, n);
  1178. }
  1179. continue;
  1180. }
  1181. io_count++;
  1182. do_io(&req);
  1183. n = os_write_file(kernel_fd, &req, sizeof(req));
  1184. if(n != sizeof(req))
  1185. printk("io_thread - write failed, fd = %d, err = %d\n",
  1186. kernel_fd, -n);
  1187. }
  1188. return 0;
  1189. }