ubd_kern.c 37 KB

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