ubd_kern.c 31 KB

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