ubd_kern.c 34 KB

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