ubd_kern.c 34 KB

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