ubd_kern.c 34 KB

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