ubd_kern.c 36 KB

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