ubd_kern.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675
  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/config.h"
  21. #include "linux/module.h"
  22. #include "linux/blkdev.h"
  23. #include "linux/hdreg.h"
  24. #include "linux/init.h"
  25. #include "linux/devfs_fs_kernel.h"
  26. #include "linux/cdrom.h"
  27. #include "linux/proc_fs.h"
  28. #include "linux/ctype.h"
  29. #include "linux/capability.h"
  30. #include "linux/mm.h"
  31. #include "linux/vmalloc.h"
  32. #include "linux/blkpg.h"
  33. #include "linux/genhd.h"
  34. #include "linux/spinlock.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 "user_util.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 "2_5compat.h"
  50. #include "os.h"
  51. #include "mem.h"
  52. #include "mem_kern.h"
  53. #include "cow.h"
  54. enum ubd_req { UBD_READ, UBD_WRITE, UBD_MMAP };
  55. struct io_thread_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 map_fd;
  67. unsigned long long map_offset;
  68. int error;
  69. };
  70. extern int open_ubd_file(char *file, struct openflags *openflags,
  71. char **backing_file_out, int *bitmap_offset_out,
  72. unsigned long *bitmap_len_out, int *data_offset_out,
  73. int *create_cow_out);
  74. extern int create_cow_file(char *cow_file, char *backing_file,
  75. struct openflags flags, int sectorsize,
  76. int alignment, int *bitmap_offset_out,
  77. unsigned long *bitmap_len_out,
  78. int *data_offset_out);
  79. extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
  80. extern void do_io(struct io_thread_req *req);
  81. static inline int ubd_test_bit(__u64 bit, unsigned char *data)
  82. {
  83. __u64 n;
  84. int bits, off;
  85. bits = sizeof(data[0]) * 8;
  86. n = bit / bits;
  87. off = bit % bits;
  88. return((data[n] & (1 << off)) != 0);
  89. }
  90. static inline void ubd_set_bit(__u64 bit, unsigned char *data)
  91. {
  92. __u64 n;
  93. int bits, off;
  94. bits = sizeof(data[0]) * 8;
  95. n = bit / bits;
  96. off = bit % bits;
  97. data[n] |= (1 << off);
  98. }
  99. /*End stuff from ubd_user.h*/
  100. #define DRIVER_NAME "uml-blkdev"
  101. static DEFINE_SPINLOCK(ubd_io_lock);
  102. static DEFINE_SPINLOCK(ubd_lock);
  103. static void (*do_ubd)(void);
  104. static int ubd_open(struct inode * inode, struct file * filp);
  105. static int ubd_release(struct inode * inode, struct file * file);
  106. static int ubd_ioctl(struct inode * inode, struct file * file,
  107. unsigned int cmd, unsigned long arg);
  108. #define MAX_DEV (8)
  109. /* Changed in early boot */
  110. static int ubd_do_mmap = 0;
  111. #define UBD_MMAP_BLOCK_SIZE PAGE_SIZE
  112. static struct block_device_operations ubd_blops = {
  113. .owner = THIS_MODULE,
  114. .open = ubd_open,
  115. .release = ubd_release,
  116. .ioctl = ubd_ioctl,
  117. };
  118. /* Protected by the queue_lock */
  119. static request_queue_t *ubd_queue;
  120. /* Protected by ubd_lock */
  121. static int fake_major = MAJOR_NR;
  122. static struct gendisk *ubd_gendisk[MAX_DEV];
  123. static struct gendisk *fake_gendisk[MAX_DEV];
  124. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  125. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  126. .cl = 1 })
  127. #else
  128. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  129. .cl = 1 })
  130. #endif
  131. /* Not protected - changed only in ubd_setup_common and then only to
  132. * to enable O_SYNC.
  133. */
  134. static struct openflags global_openflags = OPEN_FLAGS;
  135. struct cow {
  136. /* This is the backing file, actually */
  137. char *file;
  138. int fd;
  139. unsigned long *bitmap;
  140. unsigned long bitmap_len;
  141. int bitmap_offset;
  142. int data_offset;
  143. };
  144. struct ubd {
  145. char *file;
  146. int count;
  147. int fd;
  148. __u64 size;
  149. struct openflags boot_openflags;
  150. struct openflags openflags;
  151. int no_cow;
  152. struct cow cow;
  153. struct platform_device pdev;
  154. int map_writes;
  155. int map_reads;
  156. int nomap_writes;
  157. int nomap_reads;
  158. int write_maps;
  159. };
  160. #define DEFAULT_COW { \
  161. .file = NULL, \
  162. .fd = -1, \
  163. .bitmap = NULL, \
  164. .bitmap_offset = 0, \
  165. .data_offset = 0, \
  166. }
  167. #define DEFAULT_UBD { \
  168. .file = NULL, \
  169. .count = 0, \
  170. .fd = -1, \
  171. .size = -1, \
  172. .boot_openflags = OPEN_FLAGS, \
  173. .openflags = OPEN_FLAGS, \
  174. .no_cow = 0, \
  175. .cow = DEFAULT_COW, \
  176. .map_writes = 0, \
  177. .map_reads = 0, \
  178. .nomap_writes = 0, \
  179. .nomap_reads = 0, \
  180. .write_maps = 0, \
  181. }
  182. struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
  183. static int ubd0_init(void)
  184. {
  185. struct ubd *dev = &ubd_dev[0];
  186. if(dev->file == NULL)
  187. dev->file = "root_fs";
  188. return(0);
  189. }
  190. __initcall(ubd0_init);
  191. /* Only changed by fake_ide_setup which is a setup */
  192. static int fake_ide = 0;
  193. static struct proc_dir_entry *proc_ide_root = NULL;
  194. static struct proc_dir_entry *proc_ide = NULL;
  195. static void make_proc_ide(void)
  196. {
  197. proc_ide_root = proc_mkdir("ide", NULL);
  198. proc_ide = proc_mkdir("ide0", proc_ide_root);
  199. }
  200. static int proc_ide_read_media(char *page, char **start, off_t off, int count,
  201. int *eof, void *data)
  202. {
  203. int len;
  204. strcpy(page, "disk\n");
  205. len = strlen("disk\n");
  206. len -= off;
  207. if (len < count){
  208. *eof = 1;
  209. if (len <= 0) return 0;
  210. }
  211. else len = count;
  212. *start = page + off;
  213. return len;
  214. }
  215. static void make_ide_entries(char *dev_name)
  216. {
  217. struct proc_dir_entry *dir, *ent;
  218. char name[64];
  219. if(proc_ide_root == NULL) make_proc_ide();
  220. dir = proc_mkdir(dev_name, proc_ide);
  221. if(!dir) return;
  222. ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
  223. if(!ent) return;
  224. ent->nlink = 1;
  225. ent->data = NULL;
  226. ent->read_proc = proc_ide_read_media;
  227. ent->write_proc = NULL;
  228. sprintf(name,"ide0/%s", dev_name);
  229. proc_symlink(dev_name, proc_ide_root, name);
  230. }
  231. static int fake_ide_setup(char *str)
  232. {
  233. fake_ide = 1;
  234. return(1);
  235. }
  236. __setup("fake_ide", fake_ide_setup);
  237. __uml_help(fake_ide_setup,
  238. "fake_ide\n"
  239. " Create ide0 entries that map onto ubd devices.\n\n"
  240. );
  241. static int parse_unit(char **ptr)
  242. {
  243. char *str = *ptr, *end;
  244. int n = -1;
  245. if(isdigit(*str)) {
  246. n = simple_strtoul(str, &end, 0);
  247. if(end == str)
  248. return(-1);
  249. *ptr = end;
  250. }
  251. else if (('a' <= *str) && (*str <= 'h')) {
  252. n = *str - 'a';
  253. str++;
  254. *ptr = str;
  255. }
  256. return(n);
  257. }
  258. static int ubd_setup_common(char *str, int *index_out)
  259. {
  260. struct ubd *dev;
  261. struct openflags flags = global_openflags;
  262. char *backing_file;
  263. int n, err, i;
  264. if(index_out) *index_out = -1;
  265. n = *str;
  266. if(n == '='){
  267. char *end;
  268. int major;
  269. str++;
  270. if(!strcmp(str, "mmap")){
  271. CHOOSE_MODE(printk("mmap not supported by the ubd "
  272. "driver in tt mode\n"),
  273. ubd_do_mmap = 1);
  274. return(0);
  275. }
  276. if(!strcmp(str, "sync")){
  277. global_openflags = of_sync(global_openflags);
  278. return(0);
  279. }
  280. major = simple_strtoul(str, &end, 0);
  281. if((*end != '\0') || (end == str)){
  282. printk(KERN_ERR
  283. "ubd_setup : didn't parse major number\n");
  284. return(1);
  285. }
  286. err = 1;
  287. spin_lock(&ubd_lock);
  288. if(fake_major != MAJOR_NR){
  289. printk(KERN_ERR "Can't assign a fake major twice\n");
  290. goto out1;
  291. }
  292. fake_major = major;
  293. printk(KERN_INFO "Setting extra ubd major number to %d\n",
  294. major);
  295. err = 0;
  296. out1:
  297. spin_unlock(&ubd_lock);
  298. return(err);
  299. }
  300. n = parse_unit(&str);
  301. if(n < 0){
  302. printk(KERN_ERR "ubd_setup : couldn't parse unit number "
  303. "'%s'\n", str);
  304. return(1);
  305. }
  306. if(n >= MAX_DEV){
  307. printk(KERN_ERR "ubd_setup : index %d out of range "
  308. "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
  309. return(1);
  310. }
  311. err = 1;
  312. spin_lock(&ubd_lock);
  313. dev = &ubd_dev[n];
  314. if(dev->file != NULL){
  315. printk(KERN_ERR "ubd_setup : device already configured\n");
  316. goto out;
  317. }
  318. if (index_out)
  319. *index_out = n;
  320. for (i = 0; i < 4; i++) {
  321. switch (*str) {
  322. case 'r':
  323. flags.w = 0;
  324. break;
  325. case 's':
  326. flags.s = 1;
  327. break;
  328. case 'd':
  329. dev->no_cow = 1;
  330. break;
  331. case '=':
  332. str++;
  333. goto break_loop;
  334. default:
  335. printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r,s or d)\n");
  336. goto out;
  337. }
  338. str++;
  339. }
  340. if (*str == '=')
  341. printk(KERN_ERR "ubd_setup : Too many flags specified\n");
  342. else
  343. printk(KERN_ERR "ubd_setup : Expected '='\n");
  344. goto out;
  345. break_loop:
  346. err = 0;
  347. backing_file = strchr(str, ',');
  348. if (!backing_file) {
  349. backing_file = strchr(str, ':');
  350. }
  351. if(backing_file){
  352. if(dev->no_cow)
  353. printk(KERN_ERR "Can't specify both 'd' and a "
  354. "cow file\n");
  355. else {
  356. *backing_file = '\0';
  357. backing_file++;
  358. }
  359. }
  360. dev->file = str;
  361. dev->cow.file = backing_file;
  362. dev->boot_openflags = flags;
  363. out:
  364. spin_unlock(&ubd_lock);
  365. return(err);
  366. }
  367. static int ubd_setup(char *str)
  368. {
  369. ubd_setup_common(str, NULL);
  370. return(1);
  371. }
  372. __setup("ubd", ubd_setup);
  373. __uml_help(ubd_setup,
  374. "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
  375. " This is used to associate a device with a file in the underlying\n"
  376. " filesystem. When specifying two filenames, the first one is the\n"
  377. " COW name and the second is the backing file name. As separator you can\n"
  378. " use either a ':' or a ',': the first one allows writing things like;\n"
  379. " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
  380. " while with a ',' the shell would not expand the 2nd '~'.\n"
  381. " When using only one filename, UML will detect whether to thread it like\n"
  382. " a COW file or a backing file. To override this detection, add the 'd'\n"
  383. " flag:\n"
  384. " ubd0d=BackingFile\n"
  385. " Usually, there is a filesystem in the file, but \n"
  386. " that's not required. Swap devices containing swap files can be\n"
  387. " specified like this. Also, a file which doesn't contain a\n"
  388. " filesystem can have its contents read in the virtual \n"
  389. " machine by running 'dd' on the device. <n> must be in the range\n"
  390. " 0 to 7. Appending an 'r' to the number will cause that device\n"
  391. " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
  392. " an 's' will cause data to be written to disk on the host immediately.\n\n"
  393. );
  394. static int udb_setup(char *str)
  395. {
  396. printk("udb%s specified on command line is almost certainly a ubd -> "
  397. "udb TYPO\n", str);
  398. return(1);
  399. }
  400. __setup("udb", udb_setup);
  401. __uml_help(udb_setup,
  402. "udb\n"
  403. " This option is here solely to catch ubd -> udb typos, which can be\n\n"
  404. " to impossible to catch visually unless you specifically look for\n\n"
  405. " them. The only result of any option starting with 'udb' is an error\n\n"
  406. " in the boot output.\n\n"
  407. );
  408. static int fakehd_set = 0;
  409. static int fakehd(char *str)
  410. {
  411. printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
  412. fakehd_set = 1;
  413. return 1;
  414. }
  415. __setup("fakehd", fakehd);
  416. __uml_help(fakehd,
  417. "fakehd\n"
  418. " Change the ubd device name to \"hd\".\n\n"
  419. );
  420. static void do_ubd_request(request_queue_t * q);
  421. /* Only changed by ubd_init, which is an initcall. */
  422. int thread_fd = -1;
  423. /* Changed by ubd_handler, which is serialized because interrupts only
  424. * happen on CPU 0.
  425. */
  426. int intr_count = 0;
  427. /* call ubd_finish if you need to serialize */
  428. static void __ubd_finish(struct request *req, int error)
  429. {
  430. int nsect;
  431. if(error){
  432. end_request(req, 0);
  433. return;
  434. }
  435. nsect = req->current_nr_sectors;
  436. req->sector += nsect;
  437. req->buffer += nsect << 9;
  438. req->errors = 0;
  439. req->nr_sectors -= nsect;
  440. req->current_nr_sectors = 0;
  441. end_request(req, 1);
  442. }
  443. static inline void ubd_finish(struct request *req, int error)
  444. {
  445. spin_lock(&ubd_io_lock);
  446. __ubd_finish(req, error);
  447. spin_unlock(&ubd_io_lock);
  448. }
  449. /* Called without ubd_io_lock held */
  450. static void ubd_handler(void)
  451. {
  452. struct io_thread_req req;
  453. struct request *rq = elv_next_request(ubd_queue);
  454. int n, err;
  455. do_ubd = NULL;
  456. intr_count++;
  457. n = os_read_file(thread_fd, &req, sizeof(req));
  458. if(n != sizeof(req)){
  459. printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
  460. "err = %d\n", os_getpid(), -n);
  461. spin_lock(&ubd_io_lock);
  462. end_request(rq, 0);
  463. spin_unlock(&ubd_io_lock);
  464. return;
  465. }
  466. if((req.op != UBD_MMAP) &&
  467. ((req.offset != ((__u64) (rq->sector)) << 9) ||
  468. (req.length != (rq->current_nr_sectors) << 9)))
  469. panic("I/O op mismatch");
  470. if(req.map_fd != -1){
  471. err = physmem_subst_mapping(req.buffer, req.map_fd,
  472. req.map_offset, 1);
  473. if(err)
  474. printk("ubd_handler - physmem_subst_mapping failed, "
  475. "err = %d\n", -err);
  476. }
  477. ubd_finish(rq, req.error);
  478. reactivate_fd(thread_fd, UBD_IRQ);
  479. do_ubd_request(ubd_queue);
  480. }
  481. static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
  482. {
  483. ubd_handler();
  484. return(IRQ_HANDLED);
  485. }
  486. /* Only changed by ubd_init, which is an initcall. */
  487. static int io_pid = -1;
  488. void kill_io_thread(void)
  489. {
  490. if(io_pid != -1)
  491. os_kill_process(io_pid, 1);
  492. }
  493. __uml_exitcall(kill_io_thread);
  494. static int ubd_file_size(struct ubd *dev, __u64 *size_out)
  495. {
  496. char *file;
  497. file = dev->cow.file ? dev->cow.file : dev->file;
  498. return(os_file_size(file, size_out));
  499. }
  500. static void ubd_close(struct ubd *dev)
  501. {
  502. if(ubd_do_mmap)
  503. physmem_forget_descriptor(dev->fd);
  504. os_close_file(dev->fd);
  505. if(dev->cow.file == NULL)
  506. return;
  507. if(ubd_do_mmap)
  508. physmem_forget_descriptor(dev->cow.fd);
  509. os_close_file(dev->cow.fd);
  510. vfree(dev->cow.bitmap);
  511. dev->cow.bitmap = NULL;
  512. }
  513. static int ubd_open_dev(struct ubd *dev)
  514. {
  515. struct openflags flags;
  516. char **back_ptr;
  517. int err, create_cow, *create_ptr;
  518. dev->openflags = dev->boot_openflags;
  519. create_cow = 0;
  520. create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
  521. back_ptr = dev->no_cow ? NULL : &dev->cow.file;
  522. dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
  523. &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
  524. &dev->cow.data_offset, create_ptr);
  525. if((dev->fd == -ENOENT) && create_cow){
  526. dev->fd = create_cow_file(dev->file, dev->cow.file,
  527. dev->openflags, 1 << 9, PAGE_SIZE,
  528. &dev->cow.bitmap_offset,
  529. &dev->cow.bitmap_len,
  530. &dev->cow.data_offset);
  531. if(dev->fd >= 0){
  532. printk(KERN_INFO "Creating \"%s\" as COW file for "
  533. "\"%s\"\n", dev->file, dev->cow.file);
  534. }
  535. }
  536. if(dev->fd < 0){
  537. printk("Failed to open '%s', errno = %d\n", dev->file,
  538. -dev->fd);
  539. return(dev->fd);
  540. }
  541. if(dev->cow.file != NULL){
  542. err = -ENOMEM;
  543. dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
  544. if(dev->cow.bitmap == NULL){
  545. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  546. goto error;
  547. }
  548. flush_tlb_kernel_vm();
  549. err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
  550. dev->cow.bitmap_offset,
  551. dev->cow.bitmap_len);
  552. if(err < 0)
  553. goto error;
  554. flags = dev->openflags;
  555. flags.w = 0;
  556. err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
  557. NULL, NULL);
  558. if(err < 0) goto error;
  559. dev->cow.fd = err;
  560. }
  561. return(0);
  562. error:
  563. os_close_file(dev->fd);
  564. return(err);
  565. }
  566. static int ubd_new_disk(int major, u64 size, int unit,
  567. struct gendisk **disk_out)
  568. {
  569. struct gendisk *disk;
  570. char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
  571. int err;
  572. disk = alloc_disk(1 << UBD_SHIFT);
  573. if(disk == NULL)
  574. return(-ENOMEM);
  575. disk->major = major;
  576. disk->first_minor = unit << UBD_SHIFT;
  577. disk->fops = &ubd_blops;
  578. set_capacity(disk, size / 512);
  579. if(major == MAJOR_NR){
  580. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  581. sprintf(disk->devfs_name, "ubd/disc%d", unit);
  582. sprintf(from, "ubd/%d", unit);
  583. sprintf(to, "disc%d/disc", unit);
  584. err = devfs_mk_symlink(from, to);
  585. if(err)
  586. printk("ubd_new_disk failed to make link from %s to "
  587. "%s, error = %d\n", from, to, err);
  588. }
  589. else {
  590. sprintf(disk->disk_name, "ubd_fake%d", unit);
  591. sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
  592. }
  593. /* sysfs register (not for ide fake devices) */
  594. if (major == MAJOR_NR) {
  595. ubd_dev[unit].pdev.id = unit;
  596. ubd_dev[unit].pdev.name = DRIVER_NAME;
  597. platform_device_register(&ubd_dev[unit].pdev);
  598. disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
  599. }
  600. disk->private_data = &ubd_dev[unit];
  601. disk->queue = ubd_queue;
  602. add_disk(disk);
  603. *disk_out = disk;
  604. return 0;
  605. }
  606. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  607. static int ubd_add(int n)
  608. {
  609. struct ubd *dev = &ubd_dev[n];
  610. int err;
  611. if(dev->file == NULL)
  612. return(-ENODEV);
  613. if (ubd_open_dev(dev))
  614. return(-ENODEV);
  615. err = ubd_file_size(dev, &dev->size);
  616. if(err < 0)
  617. return(err);
  618. dev->size = ROUND_BLOCK(dev->size);
  619. err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
  620. if(err)
  621. return(err);
  622. if(fake_major != MAJOR_NR)
  623. ubd_new_disk(fake_major, 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. ubd_close(dev);
  630. return 0;
  631. }
  632. static int ubd_config(char *str)
  633. {
  634. int n, err;
  635. str = uml_strdup(str);
  636. if(str == NULL){
  637. printk(KERN_ERR "ubd_config failed to strdup string\n");
  638. return(1);
  639. }
  640. err = ubd_setup_common(str, &n);
  641. if(err){
  642. kfree(str);
  643. return(-1);
  644. }
  645. if(n == -1) return(0);
  646. spin_lock(&ubd_lock);
  647. err = ubd_add(n);
  648. if(err)
  649. ubd_dev[n].file = NULL;
  650. spin_unlock(&ubd_lock);
  651. return(err);
  652. }
  653. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  654. {
  655. struct ubd *dev;
  656. int n, len = 0;
  657. n = parse_unit(&name);
  658. if((n >= MAX_DEV) || (n < 0)){
  659. *error_out = "ubd_get_config : device number out of range";
  660. return(-1);
  661. }
  662. dev = &ubd_dev[n];
  663. spin_lock(&ubd_lock);
  664. if(dev->file == NULL){
  665. CONFIG_CHUNK(str, size, len, "", 1);
  666. goto out;
  667. }
  668. CONFIG_CHUNK(str, size, len, dev->file, 0);
  669. if(dev->cow.file != NULL){
  670. CONFIG_CHUNK(str, size, len, ",", 0);
  671. CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
  672. }
  673. else CONFIG_CHUNK(str, size, len, "", 1);
  674. out:
  675. spin_unlock(&ubd_lock);
  676. return(len);
  677. }
  678. static int ubd_remove(char *str)
  679. {
  680. struct ubd *dev;
  681. int n, err = -ENODEV;
  682. n = parse_unit(&str);
  683. if((n < 0) || (n >= MAX_DEV))
  684. return(err);
  685. dev = &ubd_dev[n];
  686. if(dev->count > 0)
  687. return(-EBUSY); /* you cannot remove a open disk */
  688. err = 0;
  689. spin_lock(&ubd_lock);
  690. if(ubd_gendisk[n] == NULL)
  691. goto out;
  692. del_gendisk(ubd_gendisk[n]);
  693. put_disk(ubd_gendisk[n]);
  694. ubd_gendisk[n] = NULL;
  695. if(fake_gendisk[n] != NULL){
  696. del_gendisk(fake_gendisk[n]);
  697. put_disk(fake_gendisk[n]);
  698. fake_gendisk[n] = NULL;
  699. }
  700. platform_device_unregister(&dev->pdev);
  701. *dev = ((struct ubd) DEFAULT_UBD);
  702. err = 0;
  703. out:
  704. spin_unlock(&ubd_lock);
  705. return(err);
  706. }
  707. static struct mc_device ubd_mc = {
  708. .name = "ubd",
  709. .config = ubd_config,
  710. .get_config = ubd_get_config,
  711. .remove = ubd_remove,
  712. };
  713. static int ubd_mc_init(void)
  714. {
  715. mconsole_register_dev(&ubd_mc);
  716. return 0;
  717. }
  718. __initcall(ubd_mc_init);
  719. static struct device_driver ubd_driver = {
  720. .name = DRIVER_NAME,
  721. .bus = &platform_bus_type,
  722. };
  723. int ubd_init(void)
  724. {
  725. int i;
  726. devfs_mk_dir("ubd");
  727. if (register_blkdev(MAJOR_NR, "ubd"))
  728. return -1;
  729. ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
  730. if (!ubd_queue) {
  731. unregister_blkdev(MAJOR_NR, "ubd");
  732. return -1;
  733. }
  734. if (fake_major != MAJOR_NR) {
  735. char name[sizeof("ubd_nnn\0")];
  736. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  737. devfs_mk_dir(name);
  738. if (register_blkdev(fake_major, "ubd"))
  739. return -1;
  740. }
  741. driver_register(&ubd_driver);
  742. for (i = 0; i < MAX_DEV; i++)
  743. ubd_add(i);
  744. return 0;
  745. }
  746. late_initcall(ubd_init);
  747. int ubd_driver_init(void){
  748. unsigned long stack;
  749. int err;
  750. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  751. if(global_openflags.s){
  752. printk(KERN_INFO "ubd: Synchronous mode\n");
  753. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  754. * enough. So use anyway the io thread. */
  755. }
  756. stack = alloc_stack(0, 0);
  757. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  758. &thread_fd);
  759. if(io_pid < 0){
  760. printk(KERN_ERR
  761. "ubd : Failed to start I/O thread (errno = %d) - "
  762. "falling back to synchronous I/O\n", -io_pid);
  763. io_pid = -1;
  764. return(0);
  765. }
  766. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  767. SA_INTERRUPT, "ubd", ubd_dev);
  768. if(err != 0)
  769. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  770. return(err);
  771. }
  772. device_initcall(ubd_driver_init);
  773. static int ubd_open(struct inode *inode, struct file *filp)
  774. {
  775. struct gendisk *disk = inode->i_bdev->bd_disk;
  776. struct ubd *dev = disk->private_data;
  777. int err = 0;
  778. if(dev->count == 0){
  779. err = ubd_open_dev(dev);
  780. if(err){
  781. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  782. disk->disk_name, dev->file, -err);
  783. goto out;
  784. }
  785. }
  786. dev->count++;
  787. set_disk_ro(disk, !dev->openflags.w);
  788. /* This should no more be needed. And it didn't work anyway to exclude
  789. * read-write remounting of filesystems.*/
  790. /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
  791. if(--dev->count == 0) ubd_close(dev);
  792. err = -EROFS;
  793. }*/
  794. out:
  795. return(err);
  796. }
  797. static int ubd_release(struct inode * inode, struct file * file)
  798. {
  799. struct gendisk *disk = inode->i_bdev->bd_disk;
  800. struct ubd *dev = disk->private_data;
  801. if(--dev->count == 0)
  802. ubd_close(dev);
  803. return(0);
  804. }
  805. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  806. __u64 *cow_offset, unsigned long *bitmap,
  807. __u64 bitmap_offset, unsigned long *bitmap_words,
  808. __u64 bitmap_len)
  809. {
  810. __u64 sector = io_offset >> 9;
  811. int i, update_bitmap = 0;
  812. for(i = 0; i < length >> 9; i++){
  813. if(cow_mask != NULL)
  814. ubd_set_bit(i, (unsigned char *) cow_mask);
  815. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  816. continue;
  817. update_bitmap = 1;
  818. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  819. }
  820. if(!update_bitmap)
  821. return;
  822. *cow_offset = sector / (sizeof(unsigned long) * 8);
  823. /* This takes care of the case where we're exactly at the end of the
  824. * device, and *cow_offset + 1 is off the end. So, just back it up
  825. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  826. * for the original diagnosis.
  827. */
  828. if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
  829. sizeof(unsigned long) - 1))
  830. (*cow_offset)--;
  831. bitmap_words[0] = bitmap[*cow_offset];
  832. bitmap_words[1] = bitmap[*cow_offset + 1];
  833. *cow_offset *= sizeof(unsigned long);
  834. *cow_offset += bitmap_offset;
  835. }
  836. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  837. __u64 bitmap_offset, __u64 bitmap_len)
  838. {
  839. __u64 sector = req->offset >> 9;
  840. int i;
  841. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  842. panic("Operation too long");
  843. if(req->op == UBD_READ) {
  844. for(i = 0; i < req->length >> 9; i++){
  845. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  846. ubd_set_bit(i, (unsigned char *)
  847. &req->sector_mask);
  848. }
  849. }
  850. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  851. &req->cow_offset, bitmap, bitmap_offset,
  852. req->bitmap_words, bitmap_len);
  853. }
  854. static int mmap_fd(struct request *req, struct ubd *dev, __u64 offset)
  855. {
  856. __u64 sector;
  857. unsigned char *bitmap;
  858. int bit, i;
  859. /* mmap must have been requested on the command line */
  860. if(!ubd_do_mmap)
  861. return(-1);
  862. /* The buffer must be page aligned */
  863. if(((unsigned long) req->buffer % UBD_MMAP_BLOCK_SIZE) != 0)
  864. return(-1);
  865. /* The request must be a page long */
  866. if((req->current_nr_sectors << 9) != PAGE_SIZE)
  867. return(-1);
  868. if(dev->cow.file == NULL)
  869. return(dev->fd);
  870. sector = offset >> 9;
  871. bitmap = (unsigned char *) dev->cow.bitmap;
  872. bit = ubd_test_bit(sector, bitmap);
  873. for(i = 1; i < req->current_nr_sectors; i++){
  874. if(ubd_test_bit(sector + i, bitmap) != bit)
  875. return(-1);
  876. }
  877. if(bit || (rq_data_dir(req) == WRITE))
  878. offset += dev->cow.data_offset;
  879. /* The data on disk must be page aligned */
  880. if((offset % UBD_MMAP_BLOCK_SIZE) != 0)
  881. return(-1);
  882. return(bit ? dev->fd : dev->cow.fd);
  883. }
  884. static int prepare_mmap_request(struct ubd *dev, int fd, __u64 offset,
  885. struct request *req,
  886. struct io_thread_req *io_req)
  887. {
  888. int err;
  889. if(rq_data_dir(req) == WRITE){
  890. /* Writes are almost no-ops since the new data is already in the
  891. * host page cache
  892. */
  893. dev->map_writes++;
  894. if(dev->cow.file != NULL)
  895. cowify_bitmap(io_req->offset, io_req->length,
  896. &io_req->sector_mask, &io_req->cow_offset,
  897. dev->cow.bitmap, dev->cow.bitmap_offset,
  898. io_req->bitmap_words,
  899. dev->cow.bitmap_len);
  900. }
  901. else {
  902. int w;
  903. if((dev->cow.file != NULL) && (fd == dev->cow.fd))
  904. w = 0;
  905. else w = dev->openflags.w;
  906. if((dev->cow.file != NULL) && (fd == dev->fd))
  907. offset += dev->cow.data_offset;
  908. err = physmem_subst_mapping(req->buffer, fd, offset, w);
  909. if(err){
  910. printk("physmem_subst_mapping failed, err = %d\n",
  911. -err);
  912. return(1);
  913. }
  914. dev->map_reads++;
  915. }
  916. io_req->op = UBD_MMAP;
  917. io_req->buffer = req->buffer;
  918. return(0);
  919. }
  920. /* Called with ubd_io_lock held */
  921. static int prepare_request(struct request *req, struct io_thread_req *io_req)
  922. {
  923. struct gendisk *disk = req->rq_disk;
  924. struct ubd *dev = disk->private_data;
  925. __u64 offset;
  926. int len, fd;
  927. if(req->rq_status == RQ_INACTIVE) return(1);
  928. /* This should be impossible now */
  929. if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
  930. printk("Write attempted on readonly ubd device %s\n",
  931. disk->disk_name);
  932. end_request(req, 0);
  933. return(1);
  934. }
  935. offset = ((__u64) req->sector) << 9;
  936. len = req->current_nr_sectors << 9;
  937. io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
  938. io_req->fds[1] = dev->fd;
  939. io_req->map_fd = -1;
  940. io_req->cow_offset = -1;
  941. io_req->offset = offset;
  942. io_req->length = len;
  943. io_req->error = 0;
  944. io_req->sector_mask = 0;
  945. fd = mmap_fd(req, dev, io_req->offset);
  946. if(fd > 0){
  947. /* If mmapping is otherwise OK, but the first access to the
  948. * page is a write, then it's not mapped in yet. So we have
  949. * to write the data to disk first, then we can map the disk
  950. * page in and continue normally from there.
  951. */
  952. if((rq_data_dir(req) == WRITE) && !is_remapped(req->buffer)){
  953. io_req->map_fd = dev->fd;
  954. io_req->map_offset = io_req->offset +
  955. dev->cow.data_offset;
  956. dev->write_maps++;
  957. }
  958. else return(prepare_mmap_request(dev, fd, io_req->offset, req,
  959. io_req));
  960. }
  961. if(rq_data_dir(req) == READ)
  962. dev->nomap_reads++;
  963. else dev->nomap_writes++;
  964. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  965. io_req->offsets[0] = 0;
  966. io_req->offsets[1] = dev->cow.data_offset;
  967. io_req->buffer = req->buffer;
  968. io_req->sectorsize = 1 << 9;
  969. if(dev->cow.file != NULL)
  970. cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
  971. dev->cow.bitmap_len);
  972. return(0);
  973. }
  974. /* Called with ubd_io_lock held */
  975. static void do_ubd_request(request_queue_t *q)
  976. {
  977. struct io_thread_req io_req;
  978. struct request *req;
  979. int err, n;
  980. if(thread_fd == -1){
  981. while((req = elv_next_request(q)) != NULL){
  982. err = prepare_request(req, &io_req);
  983. if(!err){
  984. do_io(&io_req);
  985. __ubd_finish(req, io_req.error);
  986. }
  987. }
  988. }
  989. else {
  990. if(do_ubd || (req = elv_next_request(q)) == NULL)
  991. return;
  992. err = prepare_request(req, &io_req);
  993. if(!err){
  994. do_ubd = ubd_handler;
  995. n = os_write_file(thread_fd, (char *) &io_req,
  996. sizeof(io_req));
  997. if(n != sizeof(io_req))
  998. printk("write to io thread failed, "
  999. "errno = %d\n", -n);
  1000. }
  1001. }
  1002. }
  1003. static int ubd_ioctl(struct inode * inode, struct file * file,
  1004. unsigned int cmd, unsigned long arg)
  1005. {
  1006. struct hd_geometry __user *loc = (struct hd_geometry __user *) arg;
  1007. struct ubd *dev = inode->i_bdev->bd_disk->private_data;
  1008. struct hd_driveid ubd_id = {
  1009. .cyls = 0,
  1010. .heads = 128,
  1011. .sectors = 32,
  1012. };
  1013. switch (cmd) {
  1014. struct hd_geometry g;
  1015. struct cdrom_volctrl volume;
  1016. case HDIO_GETGEO:
  1017. if(!loc) return(-EINVAL);
  1018. g.heads = 128;
  1019. g.sectors = 32;
  1020. g.cylinders = dev->size / (128 * 32 * 512);
  1021. g.start = get_start_sect(inode->i_bdev);
  1022. return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
  1023. case HDIO_GET_IDENTITY:
  1024. ubd_id.cyls = dev->size / (128 * 32 * 512);
  1025. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  1026. sizeof(ubd_id)))
  1027. return(-EFAULT);
  1028. return(0);
  1029. case CDROMVOLREAD:
  1030. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  1031. return(-EFAULT);
  1032. volume.channel0 = 255;
  1033. volume.channel1 = 255;
  1034. volume.channel2 = 255;
  1035. volume.channel3 = 255;
  1036. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  1037. return(-EFAULT);
  1038. return(0);
  1039. }
  1040. return(-EINVAL);
  1041. }
  1042. static int ubd_check_remapped(int fd, unsigned long address, int is_write,
  1043. __u64 offset)
  1044. {
  1045. __u64 bitmap_offset;
  1046. unsigned long new_bitmap[2];
  1047. int i, err, n;
  1048. /* If it's not a write access, we can't do anything about it */
  1049. if(!is_write)
  1050. return(0);
  1051. /* We have a write */
  1052. for(i = 0; i < sizeof(ubd_dev) / sizeof(ubd_dev[0]); i++){
  1053. struct ubd *dev = &ubd_dev[i];
  1054. if((dev->fd != fd) && (dev->cow.fd != fd))
  1055. continue;
  1056. /* It's a write to a ubd device */
  1057. /* This should be impossible now */
  1058. if(!dev->openflags.w){
  1059. /* It's a write access on a read-only device - probably
  1060. * shouldn't happen. If the kernel is trying to change
  1061. * something with no intention of writing it back out,
  1062. * then this message will clue us in that this needs
  1063. * fixing
  1064. */
  1065. printk("Write access to mapped page from readonly ubd "
  1066. "device %d\n", i);
  1067. return(0);
  1068. }
  1069. /* It's a write to a writeable ubd device - it must be COWed
  1070. * because, otherwise, the page would have been mapped in
  1071. * writeable
  1072. */
  1073. if(!dev->cow.file)
  1074. panic("Write fault on writeable non-COW ubd device %d",
  1075. i);
  1076. /* It should also be an access to the backing file since the
  1077. * COW pages should be mapped in read-write
  1078. */
  1079. if(fd == dev->fd)
  1080. panic("Write fault on a backing page of ubd "
  1081. "device %d\n", i);
  1082. /* So, we do the write, copying the backing data to the COW
  1083. * file...
  1084. */
  1085. err = os_seek_file(dev->fd, offset + dev->cow.data_offset);
  1086. if(err < 0)
  1087. panic("Couldn't seek to %lld in COW file of ubd "
  1088. "device %d, err = %d",
  1089. offset + dev->cow.data_offset, i, -err);
  1090. n = os_write_file(dev->fd, (void *) address, PAGE_SIZE);
  1091. if(n != PAGE_SIZE)
  1092. panic("Couldn't copy data to COW file of ubd "
  1093. "device %d, err = %d", i, -n);
  1094. /* ... updating the COW bitmap... */
  1095. cowify_bitmap(offset, PAGE_SIZE, NULL, &bitmap_offset,
  1096. dev->cow.bitmap, dev->cow.bitmap_offset,
  1097. new_bitmap, dev->cow.bitmap_len);
  1098. err = os_seek_file(dev->fd, bitmap_offset);
  1099. if(err < 0)
  1100. panic("Couldn't seek to %lld in COW file of ubd "
  1101. "device %d, err = %d", bitmap_offset, i, -err);
  1102. n = os_write_file(dev->fd, new_bitmap, sizeof(new_bitmap));
  1103. if(n != sizeof(new_bitmap))
  1104. panic("Couldn't update bitmap of ubd device %d, "
  1105. "err = %d", i, -n);
  1106. /* Maybe we can map the COW page in, and maybe we can't. If
  1107. * it is a pre-V3 COW file, we can't, since the alignment will
  1108. * be wrong. If it is a V3 or later COW file which has been
  1109. * moved to a system with a larger page size, then maybe we
  1110. * can't, depending on the exact location of the page.
  1111. */
  1112. offset += dev->cow.data_offset;
  1113. /* Remove the remapping, putting the original anonymous page
  1114. * back. If the COW file can be mapped in, that is done.
  1115. * Otherwise, the COW page is read in.
  1116. */
  1117. if(!physmem_remove_mapping((void *) address))
  1118. panic("Address 0x%lx not remapped by ubd device %d",
  1119. address, i);
  1120. if((offset % UBD_MMAP_BLOCK_SIZE) == 0)
  1121. physmem_subst_mapping((void *) address, dev->fd,
  1122. offset, 1);
  1123. else {
  1124. err = os_seek_file(dev->fd, offset);
  1125. if(err < 0)
  1126. panic("Couldn't seek to %lld in COW file of "
  1127. "ubd device %d, err = %d", offset, i,
  1128. -err);
  1129. n = os_read_file(dev->fd, (void *) address, PAGE_SIZE);
  1130. if(n != PAGE_SIZE)
  1131. panic("Failed to read page from offset %llx of "
  1132. "COW file of ubd device %d, err = %d",
  1133. offset, i, -n);
  1134. }
  1135. return(1);
  1136. }
  1137. /* It's not a write on a ubd device */
  1138. return(0);
  1139. }
  1140. static struct remapper ubd_remapper = {
  1141. .list = LIST_HEAD_INIT(ubd_remapper.list),
  1142. .proc = ubd_check_remapped,
  1143. };
  1144. static int ubd_remapper_setup(void)
  1145. {
  1146. if(ubd_do_mmap)
  1147. register_remapper(&ubd_remapper);
  1148. return(0);
  1149. }
  1150. __initcall(ubd_remapper_setup);
  1151. static int same_backing_files(char *from_cmdline, char *from_cow, char *cow)
  1152. {
  1153. struct uml_stat buf1, buf2;
  1154. int err;
  1155. if(from_cmdline == NULL) return(1);
  1156. if(!strcmp(from_cmdline, from_cow)) return(1);
  1157. err = os_stat_file(from_cmdline, &buf1);
  1158. if(err < 0){
  1159. printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
  1160. return(1);
  1161. }
  1162. err = os_stat_file(from_cow, &buf2);
  1163. if(err < 0){
  1164. printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
  1165. return(1);
  1166. }
  1167. if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  1168. return(1);
  1169. printk("Backing file mismatch - \"%s\" requested,\n"
  1170. "\"%s\" specified in COW header of \"%s\"\n",
  1171. from_cmdline, from_cow, cow);
  1172. return(0);
  1173. }
  1174. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  1175. {
  1176. unsigned long modtime;
  1177. long long actual;
  1178. int err;
  1179. err = os_file_modtime(file, &modtime);
  1180. if(err < 0){
  1181. printk("Failed to get modification time of backing file "
  1182. "\"%s\", err = %d\n", file, -err);
  1183. return(err);
  1184. }
  1185. err = os_file_size(file, &actual);
  1186. if(err < 0){
  1187. printk("Failed to get size of backing file \"%s\", "
  1188. "err = %d\n", file, -err);
  1189. return(err);
  1190. }
  1191. if(actual != size){
  1192. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  1193. * the typecast.*/
  1194. printk("Size mismatch (%llu vs %llu) of COW header vs backing "
  1195. "file\n", (unsigned long long) size, actual);
  1196. return(-EINVAL);
  1197. }
  1198. if(modtime != mtime){
  1199. printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
  1200. "file\n", mtime, modtime);
  1201. return(-EINVAL);
  1202. }
  1203. return(0);
  1204. }
  1205. int read_cow_bitmap(int fd, void *buf, int offset, int len)
  1206. {
  1207. int err;
  1208. err = os_seek_file(fd, offset);
  1209. if(err < 0)
  1210. return(err);
  1211. err = os_read_file(fd, buf, len);
  1212. if(err < 0)
  1213. return(err);
  1214. return(0);
  1215. }
  1216. int open_ubd_file(char *file, struct openflags *openflags,
  1217. char **backing_file_out, int *bitmap_offset_out,
  1218. unsigned long *bitmap_len_out, int *data_offset_out,
  1219. int *create_cow_out)
  1220. {
  1221. time_t mtime;
  1222. unsigned long long size;
  1223. __u32 version, align;
  1224. char *backing_file;
  1225. int fd, err, sectorsize, same, mode = 0644;
  1226. fd = os_open_file(file, *openflags, mode);
  1227. if(fd < 0){
  1228. if((fd == -ENOENT) && (create_cow_out != NULL))
  1229. *create_cow_out = 1;
  1230. if(!openflags->w ||
  1231. ((fd != -EROFS) && (fd != -EACCES))) return(fd);
  1232. openflags->w = 0;
  1233. fd = os_open_file(file, *openflags, mode);
  1234. if(fd < 0)
  1235. return(fd);
  1236. }
  1237. err = os_lock_file(fd, openflags->w);
  1238. if(err < 0){
  1239. printk("Failed to lock '%s', err = %d\n", file, -err);
  1240. goto out_close;
  1241. }
  1242. if(backing_file_out == NULL) return(fd);
  1243. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  1244. &size, &sectorsize, &align, bitmap_offset_out);
  1245. if(err && (*backing_file_out != NULL)){
  1246. printk("Failed to read COW header from COW file \"%s\", "
  1247. "errno = %d\n", file, -err);
  1248. goto out_close;
  1249. }
  1250. if(err) return(fd);
  1251. if(backing_file_out == NULL) return(fd);
  1252. same = same_backing_files(*backing_file_out, backing_file, file);
  1253. if(!same && !backing_file_mismatch(*backing_file_out, size, mtime)){
  1254. printk("Switching backing file to '%s'\n", *backing_file_out);
  1255. err = write_cow_header(file, fd, *backing_file_out,
  1256. sectorsize, align, &size);
  1257. if(err){
  1258. printk("Switch failed, errno = %d\n", -err);
  1259. return(err);
  1260. }
  1261. }
  1262. else {
  1263. *backing_file_out = backing_file;
  1264. err = backing_file_mismatch(*backing_file_out, size, mtime);
  1265. if(err) goto out_close;
  1266. }
  1267. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  1268. bitmap_len_out, data_offset_out);
  1269. return(fd);
  1270. out_close:
  1271. os_close_file(fd);
  1272. return(err);
  1273. }
  1274. int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
  1275. int sectorsize, int alignment, int *bitmap_offset_out,
  1276. unsigned long *bitmap_len_out, int *data_offset_out)
  1277. {
  1278. int err, fd;
  1279. flags.c = 1;
  1280. fd = open_ubd_file(cow_file, &flags, NULL, NULL, NULL, NULL, NULL);
  1281. if(fd < 0){
  1282. err = fd;
  1283. printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
  1284. -err);
  1285. goto out;
  1286. }
  1287. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  1288. bitmap_offset_out, bitmap_len_out,
  1289. data_offset_out);
  1290. if(!err)
  1291. return(fd);
  1292. os_close_file(fd);
  1293. out:
  1294. return(err);
  1295. }
  1296. static int update_bitmap(struct io_thread_req *req)
  1297. {
  1298. int n;
  1299. if(req->cow_offset == -1)
  1300. return(0);
  1301. n = os_seek_file(req->fds[1], req->cow_offset);
  1302. if(n < 0){
  1303. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1304. return(1);
  1305. }
  1306. n = os_write_file(req->fds[1], &req->bitmap_words,
  1307. sizeof(req->bitmap_words));
  1308. if(n != sizeof(req->bitmap_words)){
  1309. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1310. req->fds[1]);
  1311. return(1);
  1312. }
  1313. return(0);
  1314. }
  1315. void do_io(struct io_thread_req *req)
  1316. {
  1317. char *buf;
  1318. unsigned long len;
  1319. int n, nsectors, start, end, bit;
  1320. int err;
  1321. __u64 off;
  1322. if(req->op == UBD_MMAP){
  1323. /* Touch the page to force the host to do any necessary IO to
  1324. * get it into memory
  1325. */
  1326. n = *((volatile int *) req->buffer);
  1327. req->error = update_bitmap(req);
  1328. return;
  1329. }
  1330. nsectors = req->length / req->sectorsize;
  1331. start = 0;
  1332. do {
  1333. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1334. end = start;
  1335. while((end < nsectors) &&
  1336. (ubd_test_bit(end, (unsigned char *)
  1337. &req->sector_mask) == bit))
  1338. end++;
  1339. off = req->offset + req->offsets[bit] +
  1340. start * req->sectorsize;
  1341. len = (end - start) * req->sectorsize;
  1342. buf = &req->buffer[start * req->sectorsize];
  1343. err = os_seek_file(req->fds[bit], off);
  1344. if(err < 0){
  1345. printk("do_io - lseek failed : err = %d\n", -err);
  1346. req->error = 1;
  1347. return;
  1348. }
  1349. if(req->op == UBD_READ){
  1350. n = 0;
  1351. do {
  1352. buf = &buf[n];
  1353. len -= n;
  1354. n = os_read_file(req->fds[bit], buf, len);
  1355. if (n < 0) {
  1356. printk("do_io - read failed, err = %d "
  1357. "fd = %d\n", -n, req->fds[bit]);
  1358. req->error = 1;
  1359. return;
  1360. }
  1361. } while((n < len) && (n != 0));
  1362. if (n < len) memset(&buf[n], 0, len - n);
  1363. } else {
  1364. n = os_write_file(req->fds[bit], buf, len);
  1365. if(n != len){
  1366. printk("do_io - write failed err = %d "
  1367. "fd = %d\n", -n, req->fds[bit]);
  1368. req->error = 1;
  1369. return;
  1370. }
  1371. }
  1372. start = end;
  1373. } while(start < nsectors);
  1374. req->error = update_bitmap(req);
  1375. }
  1376. /* Changed in start_io_thread, which is serialized by being called only
  1377. * from ubd_init, which is an initcall.
  1378. */
  1379. int kernel_fd = -1;
  1380. /* Only changed by the io thread */
  1381. int io_count = 0;
  1382. int io_thread(void *arg)
  1383. {
  1384. struct io_thread_req req;
  1385. int n;
  1386. ignore_sigwinch_sig();
  1387. while(1){
  1388. n = os_read_file(kernel_fd, &req, sizeof(req));
  1389. if(n != sizeof(req)){
  1390. if(n < 0)
  1391. printk("io_thread - read failed, fd = %d, "
  1392. "err = %d\n", kernel_fd, -n);
  1393. else {
  1394. printk("io_thread - short read, fd = %d, "
  1395. "length = %d\n", kernel_fd, n);
  1396. }
  1397. continue;
  1398. }
  1399. io_count++;
  1400. do_io(&req);
  1401. n = os_write_file(kernel_fd, &req, sizeof(req));
  1402. if(n != sizeof(req))
  1403. printk("io_thread - write failed, fd = %d, err = %d\n",
  1404. kernel_fd, -n);
  1405. }
  1406. }
  1407. /*
  1408. * Overrides for Emacs so that we follow Linus's tabbing style.
  1409. * Emacs will notice this stuff at the end of the file and automatically
  1410. * adjust the settings for this buffer only. This must remain at the end
  1411. * of the file.
  1412. * ---------------------------------------------------------------------------
  1413. * Local variables:
  1414. * c-file-style: "linux"
  1415. * End:
  1416. */