ubd_kern.c 33 KB

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