ubd_kern.c 32 KB

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