ubd_kern.c 32 KB

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