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/kernel.h"
  21. #include "linux/module.h"
  22. #include "linux/blkdev.h"
  23. #include "linux/hdreg.h"
  24. #include "linux/init.h"
  25. #include "linux/cdrom.h"
  26. #include "linux/proc_fs.h"
  27. #include "linux/ctype.h"
  28. #include "linux/capability.h"
  29. #include "linux/mm.h"
  30. #include "linux/vmalloc.h"
  31. #include "linux/blkpg.h"
  32. #include "linux/genhd.h"
  33. #include "linux/spinlock.h"
  34. #include "linux/platform_device.h"
  35. #include "linux/scatterlist.h"
  36. #include "asm/segment.h"
  37. #include "asm/uaccess.h"
  38. #include "asm/irq.h"
  39. #include "asm/types.h"
  40. #include "asm/tlbflush.h"
  41. #include "mem_user.h"
  42. #include "kern_util.h"
  43. #include "kern.h"
  44. #include "mconsole_kern.h"
  45. #include "init.h"
  46. #include "irq_user.h"
  47. #include "irq_kern.h"
  48. #include "ubd_user.h"
  49. #include "os.h"
  50. #include "mem.h"
  51. #include "mem_kern.h"
  52. #include "cow.h"
  53. enum ubd_req { UBD_READ, UBD_WRITE };
  54. struct io_thread_req {
  55. struct request *req;
  56. enum ubd_req op;
  57. int fds[2];
  58. unsigned long offsets[2];
  59. unsigned long long offset;
  60. unsigned long length;
  61. char *buffer;
  62. int sectorsize;
  63. unsigned long sector_mask;
  64. unsigned long long cow_offset;
  65. unsigned long bitmap_words[2];
  66. int error;
  67. };
  68. extern int open_ubd_file(char *file, struct openflags *openflags, int shared,
  69. char **backing_file_out, int *bitmap_offset_out,
  70. unsigned long *bitmap_len_out, int *data_offset_out,
  71. int *create_cow_out);
  72. extern int create_cow_file(char *cow_file, char *backing_file,
  73. struct openflags flags, int sectorsize,
  74. int alignment, int *bitmap_offset_out,
  75. unsigned long *bitmap_len_out,
  76. int *data_offset_out);
  77. extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
  78. extern void do_io(struct io_thread_req *req);
  79. static inline int ubd_test_bit(__u64 bit, unsigned char *data)
  80. {
  81. __u64 n;
  82. int bits, off;
  83. bits = sizeof(data[0]) * 8;
  84. n = bit / bits;
  85. off = bit % bits;
  86. return (data[n] & (1 << off)) != 0;
  87. }
  88. static inline void ubd_set_bit(__u64 bit, unsigned char *data)
  89. {
  90. __u64 n;
  91. int bits, off;
  92. bits = sizeof(data[0]) * 8;
  93. n = bit / bits;
  94. off = bit % bits;
  95. data[n] |= (1 << off);
  96. }
  97. /*End stuff from ubd_user.h*/
  98. #define DRIVER_NAME "uml-blkdev"
  99. static DEFINE_MUTEX(ubd_lock);
  100. static int ubd_open(struct inode * inode, struct file * filp);
  101. static int ubd_release(struct inode * inode, struct file * file);
  102. static int ubd_ioctl(struct inode * inode, struct file * file,
  103. unsigned int cmd, unsigned long arg);
  104. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  105. #define MAX_DEV (16)
  106. static struct block_device_operations ubd_blops = {
  107. .owner = THIS_MODULE,
  108. .open = ubd_open,
  109. .release = ubd_release,
  110. .ioctl = ubd_ioctl,
  111. .getgeo = ubd_getgeo,
  112. };
  113. /* Protected by ubd_lock */
  114. static int fake_major = MAJOR_NR;
  115. static struct gendisk *ubd_gendisk[MAX_DEV];
  116. static struct gendisk *fake_gendisk[MAX_DEV];
  117. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  118. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  119. .cl = 1 })
  120. #else
  121. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  122. .cl = 1 })
  123. #endif
  124. static struct openflags global_openflags = OPEN_FLAGS;
  125. struct cow {
  126. /* backing file name */
  127. char *file;
  128. /* backing file fd */
  129. int fd;
  130. unsigned long *bitmap;
  131. unsigned long bitmap_len;
  132. int bitmap_offset;
  133. int data_offset;
  134. };
  135. #define MAX_SG 64
  136. struct ubd {
  137. struct list_head restart;
  138. /* name (and fd, below) of the file opened for writing, either the
  139. * backing or the cow file. */
  140. char *file;
  141. int count;
  142. int fd;
  143. __u64 size;
  144. struct openflags boot_openflags;
  145. struct openflags openflags;
  146. unsigned shared:1;
  147. unsigned no_cow:1;
  148. struct cow cow;
  149. struct platform_device pdev;
  150. struct request_queue *queue;
  151. spinlock_t lock;
  152. struct scatterlist sg[MAX_SG];
  153. struct request *request;
  154. int start_sg, end_sg;
  155. };
  156. #define DEFAULT_COW { \
  157. .file = NULL, \
  158. .fd = -1, \
  159. .bitmap = NULL, \
  160. .bitmap_offset = 0, \
  161. .data_offset = 0, \
  162. }
  163. #define DEFAULT_UBD { \
  164. .file = NULL, \
  165. .count = 0, \
  166. .fd = -1, \
  167. .size = -1, \
  168. .boot_openflags = OPEN_FLAGS, \
  169. .openflags = OPEN_FLAGS, \
  170. .no_cow = 0, \
  171. .shared = 0, \
  172. .cow = DEFAULT_COW, \
  173. .lock = SPIN_LOCK_UNLOCKED, \
  174. .request = NULL, \
  175. .start_sg = 0, \
  176. .end_sg = 0, \
  177. }
  178. /* Protected by ubd_lock */
  179. struct ubd ubd_devs[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
  180. /* Only changed by fake_ide_setup which is a setup */
  181. static int fake_ide = 0;
  182. static struct proc_dir_entry *proc_ide_root = NULL;
  183. static struct proc_dir_entry *proc_ide = NULL;
  184. static void make_proc_ide(void)
  185. {
  186. proc_ide_root = proc_mkdir("ide", NULL);
  187. proc_ide = proc_mkdir("ide0", proc_ide_root);
  188. }
  189. static int proc_ide_read_media(char *page, char **start, off_t off, int count,
  190. int *eof, void *data)
  191. {
  192. int len;
  193. strcpy(page, "disk\n");
  194. len = strlen("disk\n");
  195. len -= off;
  196. if (len < count){
  197. *eof = 1;
  198. if (len <= 0) return 0;
  199. }
  200. else len = count;
  201. *start = page + off;
  202. return len;
  203. }
  204. static void make_ide_entries(char *dev_name)
  205. {
  206. struct proc_dir_entry *dir, *ent;
  207. char name[64];
  208. if(proc_ide_root == NULL) make_proc_ide();
  209. dir = proc_mkdir(dev_name, proc_ide);
  210. if(!dir) return;
  211. ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
  212. if(!ent) return;
  213. ent->data = NULL;
  214. ent->read_proc = proc_ide_read_media;
  215. ent->write_proc = NULL;
  216. sprintf(name,"ide0/%s", dev_name);
  217. proc_symlink(dev_name, proc_ide_root, name);
  218. }
  219. static int fake_ide_setup(char *str)
  220. {
  221. fake_ide = 1;
  222. return 1;
  223. }
  224. __setup("fake_ide", fake_ide_setup);
  225. __uml_help(fake_ide_setup,
  226. "fake_ide\n"
  227. " Create ide0 entries that map onto ubd devices.\n\n"
  228. );
  229. static int parse_unit(char **ptr)
  230. {
  231. char *str = *ptr, *end;
  232. int n = -1;
  233. if(isdigit(*str)) {
  234. n = simple_strtoul(str, &end, 0);
  235. if(end == str)
  236. return -1;
  237. *ptr = end;
  238. }
  239. else if (('a' <= *str) && (*str <= 'z')) {
  240. n = *str - 'a';
  241. str++;
  242. *ptr = str;
  243. }
  244. return n;
  245. }
  246. /* If *index_out == -1 at exit, the passed option was a general one;
  247. * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
  248. * should not be freed on exit.
  249. */
  250. static int ubd_setup_common(char *str, int *index_out, char **error_out)
  251. {
  252. struct ubd *ubd_dev;
  253. struct openflags flags = global_openflags;
  254. char *backing_file;
  255. int n, err = 0, i;
  256. if(index_out) *index_out = -1;
  257. n = *str;
  258. if(n == '='){
  259. char *end;
  260. int major;
  261. str++;
  262. if(!strcmp(str, "sync")){
  263. global_openflags = of_sync(global_openflags);
  264. goto out1;
  265. }
  266. err = -EINVAL;
  267. major = simple_strtoul(str, &end, 0);
  268. if((*end != '\0') || (end == str)){
  269. *error_out = "Didn't parse major number";
  270. goto out1;
  271. }
  272. mutex_lock(&ubd_lock);
  273. if(fake_major != MAJOR_NR){
  274. *error_out = "Can't assign a fake major twice";
  275. goto out1;
  276. }
  277. fake_major = major;
  278. printk(KERN_INFO "Setting extra ubd major number to %d\n",
  279. major);
  280. err = 0;
  281. out1:
  282. mutex_unlock(&ubd_lock);
  283. return err;
  284. }
  285. n = parse_unit(&str);
  286. if(n < 0){
  287. *error_out = "Couldn't parse device number";
  288. return -EINVAL;
  289. }
  290. if(n >= MAX_DEV){
  291. *error_out = "Device number out of range";
  292. return 1;
  293. }
  294. err = -EBUSY;
  295. mutex_lock(&ubd_lock);
  296. ubd_dev = &ubd_devs[n];
  297. if(ubd_dev->file != NULL){
  298. *error_out = "Device is already configured";
  299. goto out;
  300. }
  301. if (index_out)
  302. *index_out = n;
  303. err = -EINVAL;
  304. for (i = 0; i < sizeof("rscd="); i++) {
  305. switch (*str) {
  306. case 'r':
  307. flags.w = 0;
  308. break;
  309. case 's':
  310. flags.s = 1;
  311. break;
  312. case 'd':
  313. ubd_dev->no_cow = 1;
  314. break;
  315. case 'c':
  316. ubd_dev->shared = 1;
  317. break;
  318. case '=':
  319. str++;
  320. goto break_loop;
  321. default:
  322. *error_out = "Expected '=' or flag letter "
  323. "(r, s, c, or d)";
  324. goto out;
  325. }
  326. str++;
  327. }
  328. if (*str == '=')
  329. *error_out = "Too many flags specified";
  330. else
  331. *error_out = "Missing '='";
  332. goto out;
  333. break_loop:
  334. backing_file = strchr(str, ',');
  335. if (backing_file == NULL)
  336. backing_file = strchr(str, ':');
  337. if(backing_file != NULL){
  338. if(ubd_dev->no_cow){
  339. *error_out = "Can't specify both 'd' and a cow file";
  340. goto out;
  341. }
  342. else {
  343. *backing_file = '\0';
  344. backing_file++;
  345. }
  346. }
  347. err = 0;
  348. ubd_dev->file = str;
  349. ubd_dev->cow.file = backing_file;
  350. ubd_dev->boot_openflags = flags;
  351. out:
  352. mutex_unlock(&ubd_lock);
  353. return err;
  354. }
  355. static int ubd_setup(char *str)
  356. {
  357. char *error;
  358. int err;
  359. err = ubd_setup_common(str, NULL, &error);
  360. if(err)
  361. printk(KERN_ERR "Failed to initialize device with \"%s\" : "
  362. "%s\n", str, error);
  363. return 1;
  364. }
  365. __setup("ubd", ubd_setup);
  366. __uml_help(ubd_setup,
  367. "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
  368. " This is used to associate a device with a file in the underlying\n"
  369. " filesystem. When specifying two filenames, the first one is the\n"
  370. " COW name and the second is the backing file name. As separator you can\n"
  371. " use either a ':' or a ',': the first one allows writing things like;\n"
  372. " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
  373. " while with a ',' the shell would not expand the 2nd '~'.\n"
  374. " When using only one filename, UML will detect whether to treat it like\n"
  375. " a COW file or a backing file. To override this detection, add the 'd'\n"
  376. " flag:\n"
  377. " ubd0d=BackingFile\n"
  378. " Usually, there is a filesystem in the file, but \n"
  379. " that's not required. Swap devices containing swap files can be\n"
  380. " specified like this. Also, a file which doesn't contain a\n"
  381. " filesystem can have its contents read in the virtual \n"
  382. " machine by running 'dd' on the device. <n> must be in the range\n"
  383. " 0 to 7. Appending an 'r' to the number will cause that device\n"
  384. " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
  385. " an 's' will cause data to be written to disk on the host immediately.\n\n"
  386. );
  387. static int udb_setup(char *str)
  388. {
  389. printk("udb%s specified on command line is almost certainly a ubd -> "
  390. "udb TYPO\n", str);
  391. return 1;
  392. }
  393. __setup("udb", udb_setup);
  394. __uml_help(udb_setup,
  395. "udb\n"
  396. " This option is here solely to catch ubd -> udb typos, which can be\n"
  397. " to impossible to catch visually unless you specifically look for\n"
  398. " them. The only result of any option starting with 'udb' is an error\n"
  399. " in the boot output.\n\n"
  400. );
  401. static int fakehd_set = 0;
  402. static int fakehd(char *str)
  403. {
  404. printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
  405. fakehd_set = 1;
  406. return 1;
  407. }
  408. __setup("fakehd", fakehd);
  409. __uml_help(fakehd,
  410. "fakehd\n"
  411. " Change the ubd device name to \"hd\".\n\n"
  412. );
  413. static void do_ubd_request(struct request_queue * q);
  414. /* Only changed by ubd_init, which is an initcall. */
  415. int thread_fd = -1;
  416. static void ubd_end_request(struct request *req, int bytes, int error)
  417. {
  418. blk_end_request(req, error, bytes);
  419. }
  420. /* Callable only from interrupt context - otherwise you need to do
  421. * spin_lock_irq()/spin_lock_irqsave() */
  422. static inline void ubd_finish(struct request *req, int bytes)
  423. {
  424. if(bytes < 0){
  425. ubd_end_request(req, 0, -EIO);
  426. return;
  427. }
  428. ubd_end_request(req, bytes, 0);
  429. }
  430. static LIST_HEAD(restart);
  431. /* XXX - move this inside ubd_intr. */
  432. /* Called without dev->lock held, and only in interrupt context. */
  433. static void ubd_handler(void)
  434. {
  435. struct io_thread_req *req;
  436. struct request *rq;
  437. struct ubd *ubd;
  438. struct list_head *list, *next_ele;
  439. unsigned long flags;
  440. int n;
  441. while(1){
  442. n = os_read_file(thread_fd, &req,
  443. sizeof(struct io_thread_req *));
  444. if(n != sizeof(req)){
  445. if(n == -EAGAIN)
  446. break;
  447. printk(KERN_ERR "spurious interrupt in ubd_handler, "
  448. "err = %d\n", -n);
  449. return;
  450. }
  451. rq = req->req;
  452. rq->nr_sectors -= req->length >> 9;
  453. if(rq->nr_sectors == 0)
  454. ubd_finish(rq, rq->hard_nr_sectors << 9);
  455. kfree(req);
  456. }
  457. reactivate_fd(thread_fd, UBD_IRQ);
  458. list_for_each_safe(list, next_ele, &restart){
  459. ubd = container_of(list, struct ubd, restart);
  460. list_del_init(&ubd->restart);
  461. spin_lock_irqsave(&ubd->lock, flags);
  462. do_ubd_request(ubd->queue);
  463. spin_unlock_irqrestore(&ubd->lock, flags);
  464. }
  465. }
  466. static irqreturn_t ubd_intr(int irq, void *dev)
  467. {
  468. ubd_handler();
  469. return IRQ_HANDLED;
  470. }
  471. /* Only changed by ubd_init, which is an initcall. */
  472. static int io_pid = -1;
  473. void kill_io_thread(void)
  474. {
  475. if(io_pid != -1)
  476. os_kill_process(io_pid, 1);
  477. }
  478. __uml_exitcall(kill_io_thread);
  479. static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
  480. {
  481. char *file;
  482. file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
  483. return os_file_size(file, size_out);
  484. }
  485. static void ubd_close_dev(struct ubd *ubd_dev)
  486. {
  487. os_close_file(ubd_dev->fd);
  488. if(ubd_dev->cow.file == NULL)
  489. return;
  490. os_close_file(ubd_dev->cow.fd);
  491. vfree(ubd_dev->cow.bitmap);
  492. ubd_dev->cow.bitmap = NULL;
  493. }
  494. static int ubd_open_dev(struct ubd *ubd_dev)
  495. {
  496. struct openflags flags;
  497. char **back_ptr;
  498. int err, create_cow, *create_ptr;
  499. int fd;
  500. ubd_dev->openflags = ubd_dev->boot_openflags;
  501. create_cow = 0;
  502. create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
  503. back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
  504. fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
  505. back_ptr, &ubd_dev->cow.bitmap_offset,
  506. &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
  507. create_ptr);
  508. if((fd == -ENOENT) && create_cow){
  509. fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
  510. ubd_dev->openflags, 1 << 9, PAGE_SIZE,
  511. &ubd_dev->cow.bitmap_offset,
  512. &ubd_dev->cow.bitmap_len,
  513. &ubd_dev->cow.data_offset);
  514. if(fd >= 0){
  515. printk(KERN_INFO "Creating \"%s\" as COW file for "
  516. "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
  517. }
  518. }
  519. if(fd < 0){
  520. printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
  521. -fd);
  522. return fd;
  523. }
  524. ubd_dev->fd = fd;
  525. if(ubd_dev->cow.file != NULL){
  526. blk_queue_max_sectors(ubd_dev->queue, 8 * sizeof(long));
  527. err = -ENOMEM;
  528. ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
  529. if(ubd_dev->cow.bitmap == NULL){
  530. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  531. goto error;
  532. }
  533. flush_tlb_kernel_vm();
  534. err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
  535. ubd_dev->cow.bitmap_offset,
  536. ubd_dev->cow.bitmap_len);
  537. if(err < 0)
  538. goto error;
  539. flags = ubd_dev->openflags;
  540. flags.w = 0;
  541. err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
  542. NULL, NULL, NULL, NULL);
  543. if(err < 0) goto error;
  544. ubd_dev->cow.fd = err;
  545. }
  546. return 0;
  547. error:
  548. os_close_file(ubd_dev->fd);
  549. return err;
  550. }
  551. static void ubd_device_release(struct device *dev)
  552. {
  553. struct ubd *ubd_dev = dev->driver_data;
  554. blk_cleanup_queue(ubd_dev->queue);
  555. *ubd_dev = ((struct ubd) DEFAULT_UBD);
  556. }
  557. static int ubd_disk_register(int major, u64 size, int unit,
  558. struct gendisk **disk_out)
  559. {
  560. struct gendisk *disk;
  561. disk = alloc_disk(1 << UBD_SHIFT);
  562. if(disk == NULL)
  563. return -ENOMEM;
  564. disk->major = major;
  565. disk->first_minor = unit << UBD_SHIFT;
  566. disk->fops = &ubd_blops;
  567. set_capacity(disk, size / 512);
  568. if(major == MAJOR_NR)
  569. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  570. else
  571. sprintf(disk->disk_name, "ubd_fake%d", unit);
  572. /* sysfs register (not for ide fake devices) */
  573. if (major == MAJOR_NR) {
  574. ubd_devs[unit].pdev.id = unit;
  575. ubd_devs[unit].pdev.name = DRIVER_NAME;
  576. ubd_devs[unit].pdev.dev.release = ubd_device_release;
  577. ubd_devs[unit].pdev.dev.driver_data = &ubd_devs[unit];
  578. platform_device_register(&ubd_devs[unit].pdev);
  579. disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
  580. }
  581. disk->private_data = &ubd_devs[unit];
  582. disk->queue = ubd_devs[unit].queue;
  583. add_disk(disk);
  584. *disk_out = disk;
  585. return 0;
  586. }
  587. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  588. static int ubd_add(int n, char **error_out)
  589. {
  590. struct ubd *ubd_dev = &ubd_devs[n];
  591. int err = 0;
  592. if(ubd_dev->file == NULL)
  593. goto out;
  594. err = ubd_file_size(ubd_dev, &ubd_dev->size);
  595. if(err < 0){
  596. *error_out = "Couldn't determine size of device's file";
  597. goto out;
  598. }
  599. ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
  600. INIT_LIST_HEAD(&ubd_dev->restart);
  601. sg_init_table(ubd_dev->sg, MAX_SG);
  602. err = -ENOMEM;
  603. ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
  604. if (ubd_dev->queue == NULL) {
  605. *error_out = "Failed to initialize device queue";
  606. goto out;
  607. }
  608. ubd_dev->queue->queuedata = ubd_dev;
  609. blk_queue_max_hw_segments(ubd_dev->queue, MAX_SG);
  610. err = ubd_disk_register(MAJOR_NR, ubd_dev->size, n, &ubd_gendisk[n]);
  611. if(err){
  612. *error_out = "Failed to register device";
  613. goto out_cleanup;
  614. }
  615. if(fake_major != MAJOR_NR)
  616. ubd_disk_register(fake_major, ubd_dev->size, n,
  617. &fake_gendisk[n]);
  618. /* perhaps this should also be under the "if (fake_major)" above */
  619. /* using the fake_disk->disk_name and also the fakehd_set name */
  620. if (fake_ide)
  621. make_ide_entries(ubd_gendisk[n]->disk_name);
  622. err = 0;
  623. out:
  624. return err;
  625. out_cleanup:
  626. blk_cleanup_queue(ubd_dev->queue);
  627. goto out;
  628. }
  629. static int ubd_config(char *str, char **error_out)
  630. {
  631. int n, ret;
  632. /* This string is possibly broken up and stored, so it's only
  633. * freed if ubd_setup_common fails, or if only general options
  634. * were set.
  635. */
  636. str = kstrdup(str, GFP_KERNEL);
  637. if (str == NULL) {
  638. *error_out = "Failed to allocate memory";
  639. return -ENOMEM;
  640. }
  641. ret = ubd_setup_common(str, &n, error_out);
  642. if (ret)
  643. goto err_free;
  644. if (n == -1) {
  645. ret = 0;
  646. goto err_free;
  647. }
  648. mutex_lock(&ubd_lock);
  649. ret = ubd_add(n, error_out);
  650. if (ret)
  651. ubd_devs[n].file = NULL;
  652. mutex_unlock(&ubd_lock);
  653. out:
  654. return ret;
  655. err_free:
  656. kfree(str);
  657. goto out;
  658. }
  659. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  660. {
  661. struct ubd *ubd_dev;
  662. int n, len = 0;
  663. n = parse_unit(&name);
  664. if((n >= MAX_DEV) || (n < 0)){
  665. *error_out = "ubd_get_config : device number out of range";
  666. return -1;
  667. }
  668. ubd_dev = &ubd_devs[n];
  669. mutex_lock(&ubd_lock);
  670. if(ubd_dev->file == NULL){
  671. CONFIG_CHUNK(str, size, len, "", 1);
  672. goto out;
  673. }
  674. CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
  675. if(ubd_dev->cow.file != NULL){
  676. CONFIG_CHUNK(str, size, len, ",", 0);
  677. CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
  678. }
  679. else CONFIG_CHUNK(str, size, len, "", 1);
  680. out:
  681. mutex_unlock(&ubd_lock);
  682. return len;
  683. }
  684. static int ubd_id(char **str, int *start_out, int *end_out)
  685. {
  686. int n;
  687. n = parse_unit(str);
  688. *start_out = 0;
  689. *end_out = MAX_DEV - 1;
  690. return n;
  691. }
  692. static int ubd_remove(int n, char **error_out)
  693. {
  694. struct gendisk *disk = ubd_gendisk[n];
  695. struct ubd *ubd_dev;
  696. int err = -ENODEV;
  697. mutex_lock(&ubd_lock);
  698. ubd_dev = &ubd_devs[n];
  699. if(ubd_dev->file == NULL)
  700. goto out;
  701. /* you cannot remove a open disk */
  702. err = -EBUSY;
  703. if(ubd_dev->count > 0)
  704. goto out;
  705. ubd_gendisk[n] = NULL;
  706. if(disk != NULL){
  707. del_gendisk(disk);
  708. put_disk(disk);
  709. }
  710. if(fake_gendisk[n] != NULL){
  711. del_gendisk(fake_gendisk[n]);
  712. put_disk(fake_gendisk[n]);
  713. fake_gendisk[n] = NULL;
  714. }
  715. err = 0;
  716. platform_device_unregister(&ubd_dev->pdev);
  717. out:
  718. mutex_unlock(&ubd_lock);
  719. return err;
  720. }
  721. /* All these are called by mconsole in process context and without
  722. * ubd-specific locks. The structure itself is const except for .list.
  723. */
  724. static struct mc_device ubd_mc = {
  725. .list = LIST_HEAD_INIT(ubd_mc.list),
  726. .name = "ubd",
  727. .config = ubd_config,
  728. .get_config = ubd_get_config,
  729. .id = ubd_id,
  730. .remove = ubd_remove,
  731. };
  732. static int __init ubd_mc_init(void)
  733. {
  734. mconsole_register_dev(&ubd_mc);
  735. return 0;
  736. }
  737. __initcall(ubd_mc_init);
  738. static int __init ubd0_init(void)
  739. {
  740. struct ubd *ubd_dev = &ubd_devs[0];
  741. mutex_lock(&ubd_lock);
  742. if(ubd_dev->file == NULL)
  743. ubd_dev->file = "root_fs";
  744. mutex_unlock(&ubd_lock);
  745. return 0;
  746. }
  747. __initcall(ubd0_init);
  748. /* Used in ubd_init, which is an initcall */
  749. static struct platform_driver ubd_driver = {
  750. .driver = {
  751. .name = DRIVER_NAME,
  752. },
  753. };
  754. static int __init ubd_init(void)
  755. {
  756. char *error;
  757. int i, err;
  758. if (register_blkdev(MAJOR_NR, "ubd"))
  759. return -1;
  760. if (fake_major != MAJOR_NR) {
  761. char name[sizeof("ubd_nnn\0")];
  762. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  763. if (register_blkdev(fake_major, "ubd"))
  764. return -1;
  765. }
  766. platform_driver_register(&ubd_driver);
  767. mutex_lock(&ubd_lock);
  768. for (i = 0; i < MAX_DEV; i++){
  769. err = ubd_add(i, &error);
  770. if(err)
  771. printk(KERN_ERR "Failed to initialize ubd device %d :"
  772. "%s\n", i, error);
  773. }
  774. mutex_unlock(&ubd_lock);
  775. return 0;
  776. }
  777. late_initcall(ubd_init);
  778. static int __init ubd_driver_init(void){
  779. unsigned long stack;
  780. int err;
  781. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  782. if(global_openflags.s){
  783. printk(KERN_INFO "ubd: Synchronous mode\n");
  784. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  785. * enough. So use anyway the io thread. */
  786. }
  787. stack = alloc_stack(0, 0);
  788. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  789. &thread_fd);
  790. if(io_pid < 0){
  791. printk(KERN_ERR
  792. "ubd : Failed to start I/O thread (errno = %d) - "
  793. "falling back to synchronous I/O\n", -io_pid);
  794. io_pid = -1;
  795. return 0;
  796. }
  797. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  798. IRQF_DISABLED, "ubd", ubd_devs);
  799. if(err != 0)
  800. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  801. return 0;
  802. }
  803. device_initcall(ubd_driver_init);
  804. static int ubd_open(struct inode *inode, struct file *filp)
  805. {
  806. struct gendisk *disk = inode->i_bdev->bd_disk;
  807. struct ubd *ubd_dev = disk->private_data;
  808. int err = 0;
  809. if(ubd_dev->count == 0){
  810. err = ubd_open_dev(ubd_dev);
  811. if(err){
  812. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  813. disk->disk_name, ubd_dev->file, -err);
  814. goto out;
  815. }
  816. }
  817. ubd_dev->count++;
  818. set_disk_ro(disk, !ubd_dev->openflags.w);
  819. /* This should no more be needed. And it didn't work anyway to exclude
  820. * read-write remounting of filesystems.*/
  821. /*if((filp->f_mode & FMODE_WRITE) && !ubd_dev->openflags.w){
  822. if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
  823. err = -EROFS;
  824. }*/
  825. out:
  826. return err;
  827. }
  828. static int ubd_release(struct inode * inode, struct file * file)
  829. {
  830. struct gendisk *disk = inode->i_bdev->bd_disk;
  831. struct ubd *ubd_dev = disk->private_data;
  832. if(--ubd_dev->count == 0)
  833. ubd_close_dev(ubd_dev);
  834. return 0;
  835. }
  836. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  837. __u64 *cow_offset, unsigned long *bitmap,
  838. __u64 bitmap_offset, unsigned long *bitmap_words,
  839. __u64 bitmap_len)
  840. {
  841. __u64 sector = io_offset >> 9;
  842. int i, update_bitmap = 0;
  843. for(i = 0; i < length >> 9; i++){
  844. if(cow_mask != NULL)
  845. ubd_set_bit(i, (unsigned char *) cow_mask);
  846. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  847. continue;
  848. update_bitmap = 1;
  849. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  850. }
  851. if(!update_bitmap)
  852. return;
  853. *cow_offset = sector / (sizeof(unsigned long) * 8);
  854. /* This takes care of the case where we're exactly at the end of the
  855. * device, and *cow_offset + 1 is off the end. So, just back it up
  856. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  857. * for the original diagnosis.
  858. */
  859. if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
  860. sizeof(unsigned long) - 1))
  861. (*cow_offset)--;
  862. bitmap_words[0] = bitmap[*cow_offset];
  863. bitmap_words[1] = bitmap[*cow_offset + 1];
  864. *cow_offset *= sizeof(unsigned long);
  865. *cow_offset += bitmap_offset;
  866. }
  867. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  868. __u64 bitmap_offset, __u64 bitmap_len)
  869. {
  870. __u64 sector = req->offset >> 9;
  871. int i;
  872. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  873. panic("Operation too long");
  874. if(req->op == UBD_READ) {
  875. for(i = 0; i < req->length >> 9; i++){
  876. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  877. ubd_set_bit(i, (unsigned char *)
  878. &req->sector_mask);
  879. }
  880. }
  881. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  882. &req->cow_offset, bitmap, bitmap_offset,
  883. req->bitmap_words, bitmap_len);
  884. }
  885. /* Called with dev->lock held */
  886. static void prepare_request(struct request *req, struct io_thread_req *io_req,
  887. unsigned long long offset, int page_offset,
  888. int len, struct page *page)
  889. {
  890. struct gendisk *disk = req->rq_disk;
  891. struct ubd *ubd_dev = disk->private_data;
  892. io_req->req = req;
  893. io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
  894. ubd_dev->fd;
  895. io_req->fds[1] = ubd_dev->fd;
  896. io_req->cow_offset = -1;
  897. io_req->offset = offset;
  898. io_req->length = len;
  899. io_req->error = 0;
  900. io_req->sector_mask = 0;
  901. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  902. io_req->offsets[0] = 0;
  903. io_req->offsets[1] = ubd_dev->cow.data_offset;
  904. io_req->buffer = page_address(page) + page_offset;
  905. io_req->sectorsize = 1 << 9;
  906. if(ubd_dev->cow.file != NULL)
  907. cowify_req(io_req, ubd_dev->cow.bitmap,
  908. ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len);
  909. }
  910. /* Called with dev->lock held */
  911. static void do_ubd_request(struct request_queue *q)
  912. {
  913. struct io_thread_req *io_req;
  914. struct request *req;
  915. int n, last_sectors;
  916. while(1){
  917. struct ubd *dev = q->queuedata;
  918. if(dev->end_sg == 0){
  919. struct request *req = elv_next_request(q);
  920. if(req == NULL)
  921. return;
  922. dev->request = req;
  923. blkdev_dequeue_request(req);
  924. dev->start_sg = 0;
  925. dev->end_sg = blk_rq_map_sg(q, req, dev->sg);
  926. }
  927. req = dev->request;
  928. last_sectors = 0;
  929. while(dev->start_sg < dev->end_sg){
  930. struct scatterlist *sg = &dev->sg[dev->start_sg];
  931. req->sector += last_sectors;
  932. io_req = kmalloc(sizeof(struct io_thread_req),
  933. GFP_ATOMIC);
  934. if(io_req == NULL){
  935. if(list_empty(&dev->restart))
  936. list_add(&dev->restart, &restart);
  937. return;
  938. }
  939. prepare_request(req, io_req,
  940. (unsigned long long) req->sector << 9,
  941. sg->offset, sg->length, sg_page(sg));
  942. last_sectors = sg->length >> 9;
  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. kfree(io_req);
  952. return;
  953. }
  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. }