ubd_kern.c 33 KB

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