ubd_kern.c 33 KB

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