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