ubd_kern.c 32 KB

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