ubd_kern.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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. devfs_mk_dir("ubd");
  713. if (register_blkdev(MAJOR_NR, "ubd"))
  714. return -1;
  715. ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
  716. if (!ubd_queue) {
  717. unregister_blkdev(MAJOR_NR, "ubd");
  718. return -1;
  719. }
  720. if (fake_major != MAJOR_NR) {
  721. char name[sizeof("ubd_nnn\0")];
  722. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  723. devfs_mk_dir(name);
  724. if (register_blkdev(fake_major, "ubd"))
  725. return -1;
  726. }
  727. platform_driver_register(&ubd_driver);
  728. for (i = 0; i < MAX_DEV; i++)
  729. ubd_add(i);
  730. return 0;
  731. }
  732. late_initcall(ubd_init);
  733. int ubd_driver_init(void){
  734. unsigned long stack;
  735. int err;
  736. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  737. if(global_openflags.s){
  738. printk(KERN_INFO "ubd: Synchronous mode\n");
  739. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  740. * enough. So use anyway the io thread. */
  741. }
  742. stack = alloc_stack(0, 0);
  743. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  744. &thread_fd);
  745. if(io_pid < 0){
  746. printk(KERN_ERR
  747. "ubd : Failed to start I/O thread (errno = %d) - "
  748. "falling back to synchronous I/O\n", -io_pid);
  749. io_pid = -1;
  750. return(0);
  751. }
  752. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  753. SA_INTERRUPT, "ubd", ubd_dev);
  754. if(err != 0)
  755. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  756. return 0;
  757. }
  758. device_initcall(ubd_driver_init);
  759. static int ubd_open(struct inode *inode, struct file *filp)
  760. {
  761. struct gendisk *disk = inode->i_bdev->bd_disk;
  762. struct ubd *dev = disk->private_data;
  763. int err = 0;
  764. if(dev->count == 0){
  765. err = ubd_open_dev(dev);
  766. if(err){
  767. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  768. disk->disk_name, dev->file, -err);
  769. goto out;
  770. }
  771. }
  772. dev->count++;
  773. set_disk_ro(disk, !dev->openflags.w);
  774. /* This should no more be needed. And it didn't work anyway to exclude
  775. * read-write remounting of filesystems.*/
  776. /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
  777. if(--dev->count == 0) ubd_close(dev);
  778. err = -EROFS;
  779. }*/
  780. out:
  781. return(err);
  782. }
  783. static int ubd_release(struct inode * inode, struct file * file)
  784. {
  785. struct gendisk *disk = inode->i_bdev->bd_disk;
  786. struct ubd *dev = disk->private_data;
  787. if(--dev->count == 0)
  788. ubd_close(dev);
  789. return(0);
  790. }
  791. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  792. __u64 *cow_offset, unsigned long *bitmap,
  793. __u64 bitmap_offset, unsigned long *bitmap_words,
  794. __u64 bitmap_len)
  795. {
  796. __u64 sector = io_offset >> 9;
  797. int i, update_bitmap = 0;
  798. for(i = 0; i < length >> 9; i++){
  799. if(cow_mask != NULL)
  800. ubd_set_bit(i, (unsigned char *) cow_mask);
  801. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  802. continue;
  803. update_bitmap = 1;
  804. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  805. }
  806. if(!update_bitmap)
  807. return;
  808. *cow_offset = sector / (sizeof(unsigned long) * 8);
  809. /* This takes care of the case where we're exactly at the end of the
  810. * device, and *cow_offset + 1 is off the end. So, just back it up
  811. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  812. * for the original diagnosis.
  813. */
  814. if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
  815. sizeof(unsigned long) - 1))
  816. (*cow_offset)--;
  817. bitmap_words[0] = bitmap[*cow_offset];
  818. bitmap_words[1] = bitmap[*cow_offset + 1];
  819. *cow_offset *= sizeof(unsigned long);
  820. *cow_offset += bitmap_offset;
  821. }
  822. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  823. __u64 bitmap_offset, __u64 bitmap_len)
  824. {
  825. __u64 sector = req->offset >> 9;
  826. int i;
  827. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  828. panic("Operation too long");
  829. if(req->op == UBD_READ) {
  830. for(i = 0; i < req->length >> 9; i++){
  831. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  832. ubd_set_bit(i, (unsigned char *)
  833. &req->sector_mask);
  834. }
  835. }
  836. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  837. &req->cow_offset, bitmap, bitmap_offset,
  838. req->bitmap_words, bitmap_len);
  839. }
  840. /* Called with ubd_io_lock held */
  841. static int prepare_request(struct request *req, struct io_thread_req *io_req)
  842. {
  843. struct gendisk *disk = req->rq_disk;
  844. struct ubd *dev = disk->private_data;
  845. __u64 offset;
  846. int len;
  847. if(req->rq_status == RQ_INACTIVE) return(1);
  848. /* This should be impossible now */
  849. if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
  850. printk("Write attempted on readonly ubd device %s\n",
  851. disk->disk_name);
  852. end_request(req, 0);
  853. return(1);
  854. }
  855. offset = ((__u64) req->sector) << 9;
  856. len = req->current_nr_sectors << 9;
  857. io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
  858. io_req->fds[1] = dev->fd;
  859. io_req->cow_offset = -1;
  860. io_req->offset = offset;
  861. io_req->length = len;
  862. io_req->error = 0;
  863. io_req->sector_mask = 0;
  864. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  865. io_req->offsets[0] = 0;
  866. io_req->offsets[1] = dev->cow.data_offset;
  867. io_req->buffer = req->buffer;
  868. io_req->sectorsize = 1 << 9;
  869. if(dev->cow.file != NULL)
  870. cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
  871. dev->cow.bitmap_len);
  872. return(0);
  873. }
  874. /* Called with ubd_io_lock held */
  875. static void do_ubd_request(request_queue_t *q)
  876. {
  877. struct io_thread_req io_req;
  878. struct request *req;
  879. int err, n;
  880. if(thread_fd == -1){
  881. while((req = elv_next_request(q)) != NULL){
  882. err = prepare_request(req, &io_req);
  883. if(!err){
  884. do_io(&io_req);
  885. __ubd_finish(req, io_req.error);
  886. }
  887. }
  888. }
  889. else {
  890. if(do_ubd || (req = elv_next_request(q)) == NULL)
  891. return;
  892. err = prepare_request(req, &io_req);
  893. if(!err){
  894. do_ubd = ubd_handler;
  895. n = os_write_file(thread_fd, (char *) &io_req,
  896. sizeof(io_req));
  897. if(n != sizeof(io_req))
  898. printk("write to io thread failed, "
  899. "errno = %d\n", -n);
  900. }
  901. }
  902. }
  903. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  904. {
  905. struct ubd *dev = bdev->bd_disk->private_data;
  906. geo->heads = 128;
  907. geo->sectors = 32;
  908. geo->cylinders = dev->size / (128 * 32 * 512);
  909. return 0;
  910. }
  911. static int ubd_ioctl(struct inode * inode, struct file * file,
  912. unsigned int cmd, unsigned long arg)
  913. {
  914. struct ubd *dev = inode->i_bdev->bd_disk->private_data;
  915. struct hd_driveid ubd_id = {
  916. .cyls = 0,
  917. .heads = 128,
  918. .sectors = 32,
  919. };
  920. switch (cmd) {
  921. struct cdrom_volctrl volume;
  922. case HDIO_GET_IDENTITY:
  923. ubd_id.cyls = dev->size / (128 * 32 * 512);
  924. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  925. sizeof(ubd_id)))
  926. return(-EFAULT);
  927. return(0);
  928. case CDROMVOLREAD:
  929. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  930. return(-EFAULT);
  931. volume.channel0 = 255;
  932. volume.channel1 = 255;
  933. volume.channel2 = 255;
  934. volume.channel3 = 255;
  935. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  936. return(-EFAULT);
  937. return(0);
  938. }
  939. return(-EINVAL);
  940. }
  941. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  942. {
  943. struct uml_stat buf1, buf2;
  944. int err;
  945. if(from_cmdline == NULL)
  946. return 0;
  947. if(!strcmp(from_cmdline, from_cow))
  948. return 0;
  949. err = os_stat_file(from_cmdline, &buf1);
  950. if(err < 0){
  951. printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
  952. return 0;
  953. }
  954. err = os_stat_file(from_cow, &buf2);
  955. if(err < 0){
  956. printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
  957. return 1;
  958. }
  959. if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  960. return 0;
  961. printk("Backing file mismatch - \"%s\" requested,\n"
  962. "\"%s\" specified in COW header of \"%s\"\n",
  963. from_cmdline, from_cow, cow);
  964. return 1;
  965. }
  966. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  967. {
  968. unsigned long modtime;
  969. unsigned long long actual;
  970. int err;
  971. err = os_file_modtime(file, &modtime);
  972. if(err < 0){
  973. printk("Failed to get modification time of backing file "
  974. "\"%s\", err = %d\n", file, -err);
  975. return(err);
  976. }
  977. err = os_file_size(file, &actual);
  978. if(err < 0){
  979. printk("Failed to get size of backing file \"%s\", "
  980. "err = %d\n", file, -err);
  981. return(err);
  982. }
  983. if(actual != size){
  984. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  985. * the typecast.*/
  986. printk("Size mismatch (%llu vs %llu) of COW header vs backing "
  987. "file\n", (unsigned long long) size, actual);
  988. return(-EINVAL);
  989. }
  990. if(modtime != mtime){
  991. printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
  992. "file\n", mtime, modtime);
  993. return(-EINVAL);
  994. }
  995. return(0);
  996. }
  997. int read_cow_bitmap(int fd, void *buf, int offset, int len)
  998. {
  999. int err;
  1000. err = os_seek_file(fd, offset);
  1001. if(err < 0)
  1002. return(err);
  1003. err = os_read_file(fd, buf, len);
  1004. if(err < 0)
  1005. return(err);
  1006. return(0);
  1007. }
  1008. int open_ubd_file(char *file, struct openflags *openflags, int shared,
  1009. char **backing_file_out, int *bitmap_offset_out,
  1010. unsigned long *bitmap_len_out, int *data_offset_out,
  1011. int *create_cow_out)
  1012. {
  1013. time_t mtime;
  1014. unsigned long long size;
  1015. __u32 version, align;
  1016. char *backing_file;
  1017. int fd, err, sectorsize, asked_switch, mode = 0644;
  1018. fd = os_open_file(file, *openflags, mode);
  1019. if (fd < 0) {
  1020. if ((fd == -ENOENT) && (create_cow_out != NULL))
  1021. *create_cow_out = 1;
  1022. if (!openflags->w ||
  1023. ((fd != -EROFS) && (fd != -EACCES)))
  1024. return fd;
  1025. openflags->w = 0;
  1026. fd = os_open_file(file, *openflags, mode);
  1027. if (fd < 0)
  1028. return fd;
  1029. }
  1030. if(shared)
  1031. printk("Not locking \"%s\" on the host\n", file);
  1032. else {
  1033. err = os_lock_file(fd, openflags->w);
  1034. if(err < 0){
  1035. printk("Failed to lock '%s', err = %d\n", file, -err);
  1036. goto out_close;
  1037. }
  1038. }
  1039. /* Succesful return case! */
  1040. if(backing_file_out == NULL)
  1041. return(fd);
  1042. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  1043. &size, &sectorsize, &align, bitmap_offset_out);
  1044. if(err && (*backing_file_out != NULL)){
  1045. printk("Failed to read COW header from COW file \"%s\", "
  1046. "errno = %d\n", file, -err);
  1047. goto out_close;
  1048. }
  1049. if(err)
  1050. return(fd);
  1051. asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
  1052. /* Allow switching only if no mismatch. */
  1053. if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
  1054. printk("Switching backing file to '%s'\n", *backing_file_out);
  1055. err = write_cow_header(file, fd, *backing_file_out,
  1056. sectorsize, align, &size);
  1057. if (err) {
  1058. printk("Switch failed, errno = %d\n", -err);
  1059. goto out_close;
  1060. }
  1061. } else {
  1062. *backing_file_out = backing_file;
  1063. err = backing_file_mismatch(*backing_file_out, size, mtime);
  1064. if (err)
  1065. goto out_close;
  1066. }
  1067. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  1068. bitmap_len_out, data_offset_out);
  1069. return fd;
  1070. out_close:
  1071. os_close_file(fd);
  1072. return err;
  1073. }
  1074. int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
  1075. int sectorsize, int alignment, int *bitmap_offset_out,
  1076. unsigned long *bitmap_len_out, int *data_offset_out)
  1077. {
  1078. int err, fd;
  1079. flags.c = 1;
  1080. fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
  1081. if(fd < 0){
  1082. err = fd;
  1083. printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
  1084. -err);
  1085. goto out;
  1086. }
  1087. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  1088. bitmap_offset_out, bitmap_len_out,
  1089. data_offset_out);
  1090. if(!err)
  1091. return(fd);
  1092. os_close_file(fd);
  1093. out:
  1094. return(err);
  1095. }
  1096. static int update_bitmap(struct io_thread_req *req)
  1097. {
  1098. int n;
  1099. if(req->cow_offset == -1)
  1100. return(0);
  1101. n = os_seek_file(req->fds[1], req->cow_offset);
  1102. if(n < 0){
  1103. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1104. return(1);
  1105. }
  1106. n = os_write_file(req->fds[1], &req->bitmap_words,
  1107. sizeof(req->bitmap_words));
  1108. if(n != sizeof(req->bitmap_words)){
  1109. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1110. req->fds[1]);
  1111. return(1);
  1112. }
  1113. return(0);
  1114. }
  1115. void do_io(struct io_thread_req *req)
  1116. {
  1117. char *buf;
  1118. unsigned long len;
  1119. int n, nsectors, start, end, bit;
  1120. int err;
  1121. __u64 off;
  1122. nsectors = req->length / req->sectorsize;
  1123. start = 0;
  1124. do {
  1125. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1126. end = start;
  1127. while((end < nsectors) &&
  1128. (ubd_test_bit(end, (unsigned char *)
  1129. &req->sector_mask) == bit))
  1130. end++;
  1131. off = req->offset + req->offsets[bit] +
  1132. start * req->sectorsize;
  1133. len = (end - start) * req->sectorsize;
  1134. buf = &req->buffer[start * req->sectorsize];
  1135. err = os_seek_file(req->fds[bit], off);
  1136. if(err < 0){
  1137. printk("do_io - lseek failed : err = %d\n", -err);
  1138. req->error = 1;
  1139. return;
  1140. }
  1141. if(req->op == UBD_READ){
  1142. n = 0;
  1143. do {
  1144. buf = &buf[n];
  1145. len -= n;
  1146. n = os_read_file(req->fds[bit], buf, len);
  1147. if (n < 0) {
  1148. printk("do_io - read failed, err = %d "
  1149. "fd = %d\n", -n, req->fds[bit]);
  1150. req->error = 1;
  1151. return;
  1152. }
  1153. } while((n < len) && (n != 0));
  1154. if (n < len) memset(&buf[n], 0, len - n);
  1155. } else {
  1156. n = os_write_file(req->fds[bit], buf, len);
  1157. if(n != len){
  1158. printk("do_io - write failed err = %d "
  1159. "fd = %d\n", -n, req->fds[bit]);
  1160. req->error = 1;
  1161. return;
  1162. }
  1163. }
  1164. start = end;
  1165. } while(start < nsectors);
  1166. req->error = update_bitmap(req);
  1167. }
  1168. /* Changed in start_io_thread, which is serialized by being called only
  1169. * from ubd_init, which is an initcall.
  1170. */
  1171. int kernel_fd = -1;
  1172. /* Only changed by the io thread */
  1173. int io_count = 0;
  1174. int io_thread(void *arg)
  1175. {
  1176. struct io_thread_req req;
  1177. int n;
  1178. ignore_sigwinch_sig();
  1179. while(1){
  1180. n = os_read_file(kernel_fd, &req, sizeof(req));
  1181. if(n != sizeof(req)){
  1182. if(n < 0)
  1183. printk("io_thread - read failed, fd = %d, "
  1184. "err = %d\n", kernel_fd, -n);
  1185. else {
  1186. printk("io_thread - short read, fd = %d, "
  1187. "length = %d\n", kernel_fd, n);
  1188. }
  1189. continue;
  1190. }
  1191. io_count++;
  1192. do_io(&req);
  1193. n = os_write_file(kernel_fd, &req, sizeof(req));
  1194. if(n != sizeof(req))
  1195. printk("io_thread - write failed, fd = %d, err = %d\n",
  1196. kernel_fd, -n);
  1197. }
  1198. return 0;
  1199. }