ubd_kern.c 32 KB

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