ubd_kern.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  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/atomic.h"
  36. #include "asm/segment.h"
  37. #include "asm/uaccess.h"
  38. #include "asm/irq.h"
  39. #include "asm/types.h"
  40. #include "asm/tlbflush.h"
  41. #include "user_util.h"
  42. #include "mem_user.h"
  43. #include "kern_util.h"
  44. #include "kern.h"
  45. #include "mconsole_kern.h"
  46. #include "init.h"
  47. #include "irq_user.h"
  48. #include "irq_kern.h"
  49. #include "ubd_user.h"
  50. #include "os.h"
  51. #include "mem.h"
  52. #include "mem_kern.h"
  53. #include "cow.h"
  54. #include "aio.h"
  55. enum ubd_req { UBD_READ, UBD_WRITE };
  56. struct io_thread_req {
  57. enum aio_type op;
  58. int fds[2];
  59. unsigned long offsets[2];
  60. unsigned long long offset;
  61. unsigned long length;
  62. char *buffer;
  63. int sectorsize;
  64. int bitmap_offset;
  65. long bitmap_start;
  66. long bitmap_end;
  67. int error;
  68. };
  69. extern int open_ubd_file(char *file, struct openflags *openflags,
  70. char **backing_file_out, int *bitmap_offset_out,
  71. unsigned long *bitmap_len_out, int *data_offset_out,
  72. int *create_cow_out);
  73. extern int create_cow_file(char *cow_file, char *backing_file,
  74. struct openflags flags, int sectorsize,
  75. int alignment, int *bitmap_offset_out,
  76. unsigned long *bitmap_len_out,
  77. int *data_offset_out);
  78. extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
  79. extern void do_io(struct io_thread_req *req, struct request *r,
  80. unsigned long *bitmap);
  81. static inline int ubd_test_bit(__u64 bit, void *data)
  82. {
  83. unsigned char *buffer = data;
  84. __u64 n;
  85. int bits, off;
  86. bits = sizeof(buffer[0]) * 8;
  87. n = bit / bits;
  88. off = bit % bits;
  89. return((buffer[n] & (1 << off)) != 0);
  90. }
  91. static inline void ubd_set_bit(__u64 bit, void *data)
  92. {
  93. unsigned char *buffer = data;
  94. __u64 n;
  95. int bits, off;
  96. bits = sizeof(buffer[0]) * 8;
  97. n = bit / bits;
  98. off = bit % bits;
  99. buffer[n] |= (1 << off);
  100. }
  101. /*End stuff from ubd_user.h*/
  102. #define DRIVER_NAME "uml-blkdev"
  103. static DEFINE_SPINLOCK(ubd_io_lock);
  104. static DEFINE_SPINLOCK(ubd_lock);
  105. static int ubd_open(struct inode * inode, struct file * filp);
  106. static int ubd_release(struct inode * inode, struct file * file);
  107. static int ubd_ioctl(struct inode * inode, struct file * file,
  108. unsigned int cmd, unsigned long arg);
  109. #define MAX_DEV (8)
  110. static struct block_device_operations ubd_blops = {
  111. .owner = THIS_MODULE,
  112. .open = ubd_open,
  113. .release = ubd_release,
  114. .ioctl = ubd_ioctl,
  115. };
  116. /* Protected by the queue_lock */
  117. static request_queue_t *ubd_queue;
  118. /* Protected by ubd_lock */
  119. static int fake_major = MAJOR_NR;
  120. static struct gendisk *ubd_gendisk[MAX_DEV];
  121. static struct gendisk *fake_gendisk[MAX_DEV];
  122. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  123. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  124. .cl = 1 })
  125. #else
  126. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  127. .cl = 1 })
  128. #endif
  129. /* Not protected - changed only in ubd_setup_common and then only to
  130. * to enable O_SYNC.
  131. */
  132. static struct openflags global_openflags = OPEN_FLAGS;
  133. struct cow {
  134. /* This is the backing file, actually */
  135. char *file;
  136. int fd;
  137. unsigned long *bitmap;
  138. unsigned long bitmap_len;
  139. int bitmap_offset;
  140. int data_offset;
  141. };
  142. #define MAX_SG 64
  143. struct ubd {
  144. char *file;
  145. int count;
  146. int fd;
  147. __u64 size;
  148. struct openflags boot_openflags;
  149. struct openflags openflags;
  150. int no_cow;
  151. struct cow cow;
  152. struct platform_device pdev;
  153. struct scatterlist sg[MAX_SG];
  154. };
  155. #define DEFAULT_COW { \
  156. .file = NULL, \
  157. .fd = -1, \
  158. .bitmap = NULL, \
  159. .bitmap_offset = 0, \
  160. .data_offset = 0, \
  161. }
  162. #define DEFAULT_UBD { \
  163. .file = NULL, \
  164. .count = 0, \
  165. .fd = -1, \
  166. .size = -1, \
  167. .boot_openflags = OPEN_FLAGS, \
  168. .openflags = OPEN_FLAGS, \
  169. .no_cow = 0, \
  170. .cow = DEFAULT_COW, \
  171. }
  172. struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
  173. static int ubd0_init(void)
  174. {
  175. struct ubd *dev = &ubd_dev[0];
  176. if(dev->file == NULL)
  177. dev->file = "root_fs";
  178. return(0);
  179. }
  180. __initcall(ubd0_init);
  181. /* Only changed by fake_ide_setup which is a setup */
  182. static int fake_ide = 0;
  183. static struct proc_dir_entry *proc_ide_root = NULL;
  184. static struct proc_dir_entry *proc_ide = NULL;
  185. static void make_proc_ide(void)
  186. {
  187. proc_ide_root = proc_mkdir("ide", NULL);
  188. proc_ide = proc_mkdir("ide0", proc_ide_root);
  189. }
  190. static int proc_ide_read_media(char *page, char **start, off_t off, int count,
  191. int *eof, void *data)
  192. {
  193. int len;
  194. strcpy(page, "disk\n");
  195. len = strlen("disk\n");
  196. len -= off;
  197. if (len < count){
  198. *eof = 1;
  199. if (len <= 0) return 0;
  200. }
  201. else len = count;
  202. *start = page + off;
  203. return len;
  204. }
  205. static void make_ide_entries(char *dev_name)
  206. {
  207. struct proc_dir_entry *dir, *ent;
  208. char name[64];
  209. if(proc_ide_root == NULL) make_proc_ide();
  210. dir = proc_mkdir(dev_name, proc_ide);
  211. if(!dir) return;
  212. ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
  213. if(!ent) return;
  214. ent->nlink = 1;
  215. ent->data = NULL;
  216. ent->read_proc = proc_ide_read_media;
  217. ent->write_proc = NULL;
  218. sprintf(name,"ide0/%s", dev_name);
  219. proc_symlink(dev_name, proc_ide_root, name);
  220. }
  221. static int fake_ide_setup(char *str)
  222. {
  223. fake_ide = 1;
  224. return(1);
  225. }
  226. __setup("fake_ide", fake_ide_setup);
  227. __uml_help(fake_ide_setup,
  228. "fake_ide\n"
  229. " Create ide0 entries that map onto ubd devices.\n\n"
  230. );
  231. static int parse_unit(char **ptr)
  232. {
  233. char *str = *ptr, *end;
  234. int n = -1;
  235. if(isdigit(*str)) {
  236. n = simple_strtoul(str, &end, 0);
  237. if(end == str)
  238. return(-1);
  239. *ptr = end;
  240. }
  241. else if (('a' <= *str) && (*str <= 'h')) {
  242. n = *str - 'a';
  243. str++;
  244. *ptr = str;
  245. }
  246. return(n);
  247. }
  248. static int ubd_setup_common(char *str, int *index_out)
  249. {
  250. struct ubd *dev;
  251. struct openflags flags = global_openflags;
  252. char *backing_file;
  253. int n, err, i;
  254. if(index_out) *index_out = -1;
  255. n = *str;
  256. if(n == '='){
  257. char *end;
  258. int major;
  259. str++;
  260. if(!strcmp(str, "sync")){
  261. global_openflags = of_sync(global_openflags);
  262. return(0);
  263. }
  264. major = simple_strtoul(str, &end, 0);
  265. if((*end != '\0') || (end == str)){
  266. printk(KERN_ERR
  267. "ubd_setup : didn't parse major number\n");
  268. return(1);
  269. }
  270. err = 1;
  271. spin_lock(&ubd_lock);
  272. if(fake_major != MAJOR_NR){
  273. printk(KERN_ERR "Can't assign a fake major twice\n");
  274. goto out1;
  275. }
  276. fake_major = major;
  277. printk(KERN_INFO "Setting extra ubd major number to %d\n",
  278. major);
  279. err = 0;
  280. out1:
  281. spin_unlock(&ubd_lock);
  282. return(err);
  283. }
  284. n = parse_unit(&str);
  285. if(n < 0){
  286. printk(KERN_ERR "ubd_setup : couldn't parse unit number "
  287. "'%s'\n", str);
  288. return(1);
  289. }
  290. if(n >= MAX_DEV){
  291. printk(KERN_ERR "ubd_setup : index %d out of range "
  292. "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
  293. return(1);
  294. }
  295. err = 1;
  296. spin_lock(&ubd_lock);
  297. dev = &ubd_dev[n];
  298. if(dev->file != NULL){
  299. printk(KERN_ERR "ubd_setup : device already configured\n");
  300. goto out;
  301. }
  302. if (index_out)
  303. *index_out = n;
  304. for (i = 0; i < 4; i++) {
  305. switch (*str) {
  306. case 'r':
  307. flags.w = 0;
  308. break;
  309. case 's':
  310. flags.s = 1;
  311. break;
  312. case 'd':
  313. dev->no_cow = 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 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. static int in_ubd;
  406. /* Changed by ubd_handler, which is serialized because interrupts only
  407. * happen on CPU 0.
  408. */
  409. int intr_count = 0;
  410. static void ubd_end_request(struct request *req, int bytes, int uptodate)
  411. {
  412. if (!end_that_request_first(req, uptodate, bytes >> 9)) {
  413. add_disk_randomness(req->rq_disk);
  414. end_that_request_last(req);
  415. }
  416. }
  417. /* call ubd_finish if you need to serialize */
  418. static void __ubd_finish(struct request *req, int bytes)
  419. {
  420. if(bytes < 0){
  421. ubd_end_request(req, 0, 0);
  422. return;
  423. }
  424. ubd_end_request(req, bytes, 1);
  425. }
  426. static inline void ubd_finish(struct request *req, int bytes)
  427. {
  428. spin_lock(&ubd_io_lock);
  429. __ubd_finish(req, bytes);
  430. spin_unlock(&ubd_io_lock);
  431. }
  432. struct bitmap_io {
  433. atomic_t count;
  434. struct aio_context aio;
  435. };
  436. struct ubd_aio {
  437. struct aio_context aio;
  438. struct request *req;
  439. int len;
  440. struct bitmap_io *bitmap;
  441. void *bitmap_buf;
  442. };
  443. static int ubd_reply_fd = -1;
  444. static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
  445. {
  446. struct aio_thread_reply reply;
  447. struct ubd_aio *aio;
  448. struct request *req;
  449. int err, n, fd = (int) (long) dev;
  450. while(1){
  451. err = os_read_file(fd, &reply, sizeof(reply));
  452. if(err == -EAGAIN)
  453. break;
  454. if(err < 0){
  455. printk("ubd_aio_handler - read returned err %d\n",
  456. -err);
  457. break;
  458. }
  459. aio = container_of(reply.data, struct ubd_aio, aio);
  460. n = reply.err;
  461. if(n == 0){
  462. req = aio->req;
  463. req->nr_sectors -= aio->len >> 9;
  464. if((aio->bitmap != NULL) &&
  465. (atomic_dec_and_test(&aio->bitmap->count))){
  466. aio->aio = aio->bitmap->aio;
  467. aio->len = 0;
  468. kfree(aio->bitmap);
  469. aio->bitmap = NULL;
  470. submit_aio(&aio->aio);
  471. }
  472. else {
  473. if((req->nr_sectors == 0) &&
  474. (aio->bitmap == NULL)){
  475. int len = req->hard_nr_sectors << 9;
  476. ubd_finish(req, len);
  477. }
  478. if(aio->bitmap_buf != NULL)
  479. kfree(aio->bitmap_buf);
  480. kfree(aio);
  481. }
  482. }
  483. else if(n < 0){
  484. ubd_finish(aio->req, n);
  485. if(aio->bitmap != NULL)
  486. kfree(aio->bitmap);
  487. if(aio->bitmap_buf != NULL)
  488. kfree(aio->bitmap_buf);
  489. kfree(aio);
  490. }
  491. }
  492. reactivate_fd(fd, UBD_IRQ);
  493. do_ubd_request(ubd_queue);
  494. return(IRQ_HANDLED);
  495. }
  496. static int ubd_file_size(struct ubd *dev, __u64 *size_out)
  497. {
  498. char *file;
  499. file = dev->cow.file ? dev->cow.file : dev->file;
  500. return(os_file_size(file, size_out));
  501. }
  502. static void ubd_close(struct ubd *dev)
  503. {
  504. os_close_file(dev->fd);
  505. if(dev->cow.file == NULL)
  506. return;
  507. os_close_file(dev->cow.fd);
  508. vfree(dev->cow.bitmap);
  509. dev->cow.bitmap = NULL;
  510. }
  511. static int ubd_open_dev(struct ubd *dev)
  512. {
  513. struct openflags flags;
  514. char **back_ptr;
  515. int err, create_cow, *create_ptr;
  516. dev->openflags = dev->boot_openflags;
  517. create_cow = 0;
  518. create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
  519. back_ptr = dev->no_cow ? NULL : &dev->cow.file;
  520. dev->fd = open_ubd_file(dev->file, &dev->openflags, back_ptr,
  521. &dev->cow.bitmap_offset, &dev->cow.bitmap_len,
  522. &dev->cow.data_offset, create_ptr);
  523. if((dev->fd == -ENOENT) && create_cow){
  524. dev->fd = create_cow_file(dev->file, dev->cow.file,
  525. dev->openflags, 1 << 9, PAGE_SIZE,
  526. &dev->cow.bitmap_offset,
  527. &dev->cow.bitmap_len,
  528. &dev->cow.data_offset);
  529. if(dev->fd >= 0){
  530. printk(KERN_INFO "Creating \"%s\" as COW file for "
  531. "\"%s\"\n", dev->file, dev->cow.file);
  532. }
  533. }
  534. if(dev->fd < 0){
  535. printk("Failed to open '%s', errno = %d\n", dev->file,
  536. -dev->fd);
  537. return(dev->fd);
  538. }
  539. if(dev->cow.file != NULL){
  540. err = -ENOMEM;
  541. dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
  542. if(dev->cow.bitmap == NULL){
  543. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  544. goto error;
  545. }
  546. flush_tlb_kernel_vm();
  547. err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
  548. dev->cow.bitmap_offset,
  549. dev->cow.bitmap_len);
  550. if(err < 0)
  551. goto error;
  552. flags = dev->openflags;
  553. flags.w = 0;
  554. err = open_ubd_file(dev->cow.file, &flags, NULL, NULL, NULL,
  555. NULL, NULL);
  556. if(err < 0) goto error;
  557. dev->cow.fd = err;
  558. }
  559. return(0);
  560. error:
  561. os_close_file(dev->fd);
  562. return(err);
  563. }
  564. static int ubd_new_disk(int major, u64 size, int unit,
  565. struct gendisk **disk_out)
  566. {
  567. struct gendisk *disk;
  568. char from[sizeof("ubd/nnnnn\0")], to[sizeof("discnnnnn/disc\0")];
  569. int err;
  570. disk = alloc_disk(1 << UBD_SHIFT);
  571. if(disk == NULL)
  572. return(-ENOMEM);
  573. disk->major = major;
  574. disk->first_minor = unit << UBD_SHIFT;
  575. disk->fops = &ubd_blops;
  576. set_capacity(disk, size / 512);
  577. if(major == MAJOR_NR){
  578. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  579. sprintf(disk->devfs_name, "ubd/disc%d", unit);
  580. sprintf(from, "ubd/%d", unit);
  581. sprintf(to, "disc%d/disc", unit);
  582. err = devfs_mk_symlink(from, to);
  583. if(err)
  584. printk("ubd_new_disk failed to make link from %s to "
  585. "%s, error = %d\n", from, to, err);
  586. }
  587. else {
  588. sprintf(disk->disk_name, "ubd_fake%d", unit);
  589. sprintf(disk->devfs_name, "ubd_fake/disc%d", unit);
  590. }
  591. /* sysfs register (not for ide fake devices) */
  592. if (major == MAJOR_NR) {
  593. ubd_dev[unit].pdev.id = unit;
  594. ubd_dev[unit].pdev.name = DRIVER_NAME;
  595. platform_device_register(&ubd_dev[unit].pdev);
  596. disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
  597. }
  598. disk->private_data = &ubd_dev[unit];
  599. disk->queue = ubd_queue;
  600. add_disk(disk);
  601. *disk_out = disk;
  602. return 0;
  603. }
  604. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  605. static int ubd_add(int n)
  606. {
  607. struct ubd *dev = &ubd_dev[n];
  608. int err;
  609. err = -ENODEV;
  610. if(dev->file == NULL)
  611. goto out;
  612. if (ubd_open_dev(dev))
  613. goto out;
  614. err = ubd_file_size(dev, &dev->size);
  615. if(err < 0)
  616. goto out_close;
  617. dev->size = ROUND_BLOCK(dev->size);
  618. err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
  619. if(err)
  620. goto out_close;
  621. if(fake_major != MAJOR_NR)
  622. ubd_new_disk(fake_major, dev->size, n,
  623. &fake_gendisk[n]);
  624. /* perhaps this should also be under the "if (fake_major)" above */
  625. /* using the fake_disk->disk_name and also the fakehd_set name */
  626. if (fake_ide)
  627. make_ide_entries(ubd_gendisk[n]->disk_name);
  628. err = 0;
  629. out_close:
  630. ubd_close(dev);
  631. out:
  632. return err;
  633. }
  634. static int ubd_config(char *str)
  635. {
  636. int n, err;
  637. str = uml_strdup(str);
  638. if(str == NULL){
  639. printk(KERN_ERR "ubd_config failed to strdup string\n");
  640. return(1);
  641. }
  642. err = ubd_setup_common(str, &n);
  643. if(err){
  644. kfree(str);
  645. return(-1);
  646. }
  647. if(n == -1) return(0);
  648. spin_lock(&ubd_lock);
  649. err = ubd_add(n);
  650. if(err)
  651. ubd_dev[n].file = NULL;
  652. spin_unlock(&ubd_lock);
  653. return(err);
  654. }
  655. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  656. {
  657. struct ubd *dev;
  658. int n, len = 0;
  659. n = parse_unit(&name);
  660. if((n >= MAX_DEV) || (n < 0)){
  661. *error_out = "ubd_get_config : device number out of range";
  662. return(-1);
  663. }
  664. dev = &ubd_dev[n];
  665. spin_lock(&ubd_lock);
  666. if(dev->file == NULL){
  667. CONFIG_CHUNK(str, size, len, "", 1);
  668. goto out;
  669. }
  670. CONFIG_CHUNK(str, size, len, dev->file, 0);
  671. if(dev->cow.file != NULL){
  672. CONFIG_CHUNK(str, size, len, ",", 0);
  673. CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
  674. }
  675. else CONFIG_CHUNK(str, size, len, "", 1);
  676. out:
  677. spin_unlock(&ubd_lock);
  678. return(len);
  679. }
  680. static int ubd_id(char **str, int *start_out, int *end_out)
  681. {
  682. int n;
  683. n = parse_unit(str);
  684. *start_out = 0;
  685. *end_out = MAX_DEV - 1;
  686. return n;
  687. }
  688. static int ubd_remove(int n)
  689. {
  690. struct ubd *dev;
  691. int err = -ENODEV;
  692. spin_lock(&ubd_lock);
  693. if(ubd_gendisk[n] == NULL)
  694. goto out;
  695. dev = &ubd_dev[n];
  696. if(dev->file == NULL)
  697. goto out;
  698. /* you cannot remove a open disk */
  699. err = -EBUSY;
  700. if(dev->count > 0)
  701. goto out;
  702. del_gendisk(ubd_gendisk[n]);
  703. put_disk(ubd_gendisk[n]);
  704. ubd_gendisk[n] = NULL;
  705. if(fake_gendisk[n] != NULL){
  706. del_gendisk(fake_gendisk[n]);
  707. put_disk(fake_gendisk[n]);
  708. fake_gendisk[n] = NULL;
  709. }
  710. platform_device_unregister(&dev->pdev);
  711. *dev = ((struct ubd) DEFAULT_UBD);
  712. err = 0;
  713. out:
  714. spin_unlock(&ubd_lock);
  715. return err;
  716. }
  717. static struct mc_device ubd_mc = {
  718. .name = "ubd",
  719. .config = ubd_config,
  720. .get_config = ubd_get_config,
  721. .id = ubd_id,
  722. .remove = ubd_remove,
  723. };
  724. static int ubd_mc_init(void)
  725. {
  726. mconsole_register_dev(&ubd_mc);
  727. return 0;
  728. }
  729. __initcall(ubd_mc_init);
  730. static struct device_driver ubd_driver = {
  731. .name = DRIVER_NAME,
  732. .bus = &platform_bus_type,
  733. };
  734. int ubd_init(void)
  735. {
  736. int i;
  737. ubd_reply_fd = init_aio_irq(UBD_IRQ, "ubd", ubd_intr);
  738. if(ubd_reply_fd < 0)
  739. printk("Setting up ubd AIO failed, err = %d\n", ubd_reply_fd);
  740. devfs_mk_dir("ubd");
  741. if (register_blkdev(MAJOR_NR, "ubd"))
  742. return -1;
  743. ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
  744. if (!ubd_queue) {
  745. unregister_blkdev(MAJOR_NR, "ubd");
  746. return -1;
  747. }
  748. blk_queue_max_hw_segments(ubd_queue, MAX_SG);
  749. if (fake_major != MAJOR_NR) {
  750. char name[sizeof("ubd_nnn\0")];
  751. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  752. devfs_mk_dir(name);
  753. if (register_blkdev(fake_major, "ubd"))
  754. return -1;
  755. }
  756. driver_register(&ubd_driver);
  757. for (i = 0; i < MAX_DEV; i++)
  758. ubd_add(i);
  759. return 0;
  760. }
  761. late_initcall(ubd_init);
  762. static int ubd_open(struct inode *inode, struct file *filp)
  763. {
  764. struct gendisk *disk = inode->i_bdev->bd_disk;
  765. struct ubd *dev = disk->private_data;
  766. int err = 0;
  767. if(dev->count == 0){
  768. err = ubd_open_dev(dev);
  769. if(err){
  770. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  771. disk->disk_name, dev->file, -err);
  772. goto out;
  773. }
  774. }
  775. dev->count++;
  776. set_disk_ro(disk, !dev->openflags.w);
  777. /* This should no more be needed. And it didn't work anyway to exclude
  778. * read-write remounting of filesystems.*/
  779. /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
  780. if(--dev->count == 0) ubd_close(dev);
  781. err = -EROFS;
  782. }*/
  783. out:
  784. return(err);
  785. }
  786. static int ubd_release(struct inode * inode, struct file * file)
  787. {
  788. struct gendisk *disk = inode->i_bdev->bd_disk;
  789. struct ubd *dev = disk->private_data;
  790. if(--dev->count == 0)
  791. ubd_close(dev);
  792. return(0);
  793. }
  794. static void cowify_bitmap(struct io_thread_req *req, unsigned long *bitmap)
  795. {
  796. __u64 sector = req->offset / req->sectorsize;
  797. int i;
  798. for(i = 0; i < req->length / req->sectorsize; i++){
  799. if(ubd_test_bit(sector + i, bitmap))
  800. continue;
  801. if(req->bitmap_start == -1)
  802. req->bitmap_start = sector + i;
  803. req->bitmap_end = sector + i + 1;
  804. ubd_set_bit(sector + i, bitmap);
  805. }
  806. }
  807. /* Called with ubd_io_lock held */
  808. static int prepare_request(struct request *req, struct io_thread_req *io_req,
  809. unsigned long long offset, int page_offset,
  810. int len, struct page *page)
  811. {
  812. struct gendisk *disk = req->rq_disk;
  813. struct ubd *dev = disk->private_data;
  814. /* This should be impossible now */
  815. if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
  816. printk("Write attempted on readonly ubd device %s\n",
  817. disk->disk_name);
  818. ubd_end_request(req, 0, 0);
  819. return(1);
  820. }
  821. io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
  822. io_req->fds[1] = dev->fd;
  823. io_req->offset = offset;
  824. io_req->length = len;
  825. io_req->error = 0;
  826. io_req->op = (rq_data_dir(req) == READ) ? AIO_READ : AIO_WRITE;
  827. io_req->offsets[0] = 0;
  828. io_req->offsets[1] = dev->cow.data_offset;
  829. io_req->buffer = page_address(page) + page_offset;
  830. io_req->sectorsize = 1 << 9;
  831. io_req->bitmap_offset = dev->cow.bitmap_offset;
  832. io_req->bitmap_start = -1;
  833. io_req->bitmap_end = -1;
  834. if((dev->cow.file != NULL) && (io_req->op == UBD_WRITE))
  835. cowify_bitmap(io_req, dev->cow.bitmap);
  836. return(0);
  837. }
  838. /* Called with ubd_io_lock held */
  839. static void do_ubd_request(request_queue_t *q)
  840. {
  841. struct io_thread_req io_req;
  842. struct request *req;
  843. __u64 sector;
  844. int err;
  845. if(in_ubd)
  846. return;
  847. in_ubd = 1;
  848. while((req = elv_next_request(q)) != NULL){
  849. struct gendisk *disk = req->rq_disk;
  850. struct ubd *dev = disk->private_data;
  851. int n, i;
  852. blkdev_dequeue_request(req);
  853. sector = req->sector;
  854. n = blk_rq_map_sg(q, req, dev->sg);
  855. for(i = 0; i < n; i++){
  856. struct scatterlist *sg = &dev->sg[i];
  857. err = prepare_request(req, &io_req, sector << 9,
  858. sg->offset, sg->length,
  859. sg->page);
  860. if(err)
  861. continue;
  862. sector += sg->length >> 9;
  863. do_io(&io_req, req, dev->cow.bitmap);
  864. }
  865. }
  866. in_ubd = 0;
  867. }
  868. static int ubd_ioctl(struct inode * inode, struct file * file,
  869. unsigned int cmd, unsigned long arg)
  870. {
  871. struct hd_geometry __user *loc = (struct hd_geometry __user *) arg;
  872. struct ubd *dev = inode->i_bdev->bd_disk->private_data;
  873. struct hd_driveid ubd_id = {
  874. .cyls = 0,
  875. .heads = 128,
  876. .sectors = 32,
  877. };
  878. switch (cmd) {
  879. struct hd_geometry g;
  880. struct cdrom_volctrl volume;
  881. case HDIO_GETGEO:
  882. if(!loc) return(-EINVAL);
  883. g.heads = 128;
  884. g.sectors = 32;
  885. g.cylinders = dev->size / (128 * 32 * 512);
  886. g.start = get_start_sect(inode->i_bdev);
  887. return(copy_to_user(loc, &g, sizeof(g)) ? -EFAULT : 0);
  888. case HDIO_GET_IDENTITY:
  889. ubd_id.cyls = dev->size / (128 * 32 * 512);
  890. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  891. sizeof(ubd_id)))
  892. return(-EFAULT);
  893. return(0);
  894. case CDROMVOLREAD:
  895. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  896. return(-EFAULT);
  897. volume.channel0 = 255;
  898. volume.channel1 = 255;
  899. volume.channel2 = 255;
  900. volume.channel3 = 255;
  901. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  902. return(-EFAULT);
  903. return(0);
  904. }
  905. return(-EINVAL);
  906. }
  907. static int same_backing_files(char *from_cmdline, char *from_cow, char *cow)
  908. {
  909. struct uml_stat buf1, buf2;
  910. int err;
  911. if(from_cmdline == NULL) return(1);
  912. if(!strcmp(from_cmdline, from_cow)) return(1);
  913. err = os_stat_file(from_cmdline, &buf1);
  914. if(err < 0){
  915. printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
  916. return(1);
  917. }
  918. err = os_stat_file(from_cow, &buf2);
  919. if(err < 0){
  920. printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
  921. return(1);
  922. }
  923. if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  924. return(1);
  925. printk("Backing file mismatch - \"%s\" requested,\n"
  926. "\"%s\" specified in COW header of \"%s\"\n",
  927. from_cmdline, from_cow, cow);
  928. return(0);
  929. }
  930. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  931. {
  932. unsigned long modtime;
  933. long long actual;
  934. int err;
  935. err = os_file_modtime(file, &modtime);
  936. if(err < 0){
  937. printk("Failed to get modification time of backing file "
  938. "\"%s\", err = %d\n", file, -err);
  939. return(err);
  940. }
  941. err = os_file_size(file, &actual);
  942. if(err < 0){
  943. printk("Failed to get size of backing file \"%s\", "
  944. "err = %d\n", file, -err);
  945. return(err);
  946. }
  947. if(actual != size){
  948. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  949. * the typecast.*/
  950. printk("Size mismatch (%llu vs %llu) of COW header vs backing "
  951. "file\n", (unsigned long long) size, actual);
  952. return(-EINVAL);
  953. }
  954. if(modtime != mtime){
  955. printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
  956. "file\n", mtime, modtime);
  957. return(-EINVAL);
  958. }
  959. return(0);
  960. }
  961. int read_cow_bitmap(int fd, void *buf, int offset, int len)
  962. {
  963. int err;
  964. err = os_seek_file(fd, offset);
  965. if(err < 0)
  966. return(err);
  967. err = os_read_file(fd, buf, len);
  968. if(err < 0)
  969. return(err);
  970. return(0);
  971. }
  972. int open_ubd_file(char *file, struct openflags *openflags,
  973. char **backing_file_out, int *bitmap_offset_out,
  974. unsigned long *bitmap_len_out, int *data_offset_out,
  975. int *create_cow_out)
  976. {
  977. time_t mtime;
  978. unsigned long long size;
  979. __u32 version, align;
  980. char *backing_file;
  981. int fd, err, sectorsize, same, mode = 0644;
  982. fd = os_open_file(file, *openflags, mode);
  983. if(fd < 0){
  984. if((fd == -ENOENT) && (create_cow_out != NULL))
  985. *create_cow_out = 1;
  986. if(!openflags->w ||
  987. ((fd != -EROFS) && (fd != -EACCES))) return(fd);
  988. openflags->w = 0;
  989. fd = os_open_file(file, *openflags, mode);
  990. if(fd < 0)
  991. return(fd);
  992. }
  993. err = os_lock_file(fd, openflags->w);
  994. if(err < 0){
  995. printk("Failed to lock '%s', err = %d\n", file, -err);
  996. goto out_close;
  997. }
  998. if(backing_file_out == NULL) return(fd);
  999. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  1000. &size, &sectorsize, &align, bitmap_offset_out);
  1001. if(err && (*backing_file_out != NULL)){
  1002. printk("Failed to read COW header from COW file \"%s\", "
  1003. "errno = %d\n", file, -err);
  1004. goto out_close;
  1005. }
  1006. if(err) return(fd);
  1007. if(backing_file_out == NULL) return(fd);
  1008. same = same_backing_files(*backing_file_out, backing_file, file);
  1009. if(!same && !backing_file_mismatch(*backing_file_out, size, mtime)){
  1010. printk("Switching backing file to '%s'\n", *backing_file_out);
  1011. err = write_cow_header(file, fd, *backing_file_out,
  1012. sectorsize, align, &size);
  1013. if(err){
  1014. printk("Switch failed, errno = %d\n", -err);
  1015. return(err);
  1016. }
  1017. }
  1018. else {
  1019. *backing_file_out = backing_file;
  1020. err = backing_file_mismatch(*backing_file_out, size, mtime);
  1021. if(err) goto out_close;
  1022. }
  1023. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  1024. bitmap_len_out, data_offset_out);
  1025. return(fd);
  1026. out_close:
  1027. os_close_file(fd);
  1028. return(err);
  1029. }
  1030. int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
  1031. int sectorsize, int alignment, int *bitmap_offset_out,
  1032. unsigned long *bitmap_len_out, int *data_offset_out)
  1033. {
  1034. int err, fd;
  1035. flags.c = 1;
  1036. fd = open_ubd_file(cow_file, &flags, NULL, NULL, NULL, NULL, NULL);
  1037. if(fd < 0){
  1038. err = fd;
  1039. printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
  1040. -err);
  1041. goto out;
  1042. }
  1043. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  1044. bitmap_offset_out, bitmap_len_out,
  1045. data_offset_out);
  1046. if(!err)
  1047. return(fd);
  1048. os_close_file(fd);
  1049. out:
  1050. return(err);
  1051. }
  1052. void do_io(struct io_thread_req *req, struct request *r, unsigned long *bitmap)
  1053. {
  1054. struct ubd_aio *aio;
  1055. struct bitmap_io *bitmap_io = NULL;
  1056. char *buf;
  1057. void *bitmap_buf = NULL;
  1058. unsigned long len, sector;
  1059. int nsectors, start, end, bit, err;
  1060. __u64 off;
  1061. if(req->bitmap_start != -1){
  1062. /* Round up to the nearest word */
  1063. int round = sizeof(unsigned long);
  1064. len = (req->bitmap_end - req->bitmap_start +
  1065. round * 8 - 1) / (round * 8);
  1066. len *= round;
  1067. off = req->bitmap_start / (8 * round);
  1068. off *= round;
  1069. bitmap_io = kmalloc(sizeof(*bitmap_io), GFP_KERNEL);
  1070. if(bitmap_io == NULL){
  1071. printk("Failed to kmalloc bitmap IO\n");
  1072. req->error = 1;
  1073. return;
  1074. }
  1075. bitmap_buf = kmalloc(len, GFP_KERNEL);
  1076. if(bitmap_buf == NULL){
  1077. printk("do_io : kmalloc of bitmap chunk "
  1078. "failed\n");
  1079. kfree(bitmap_io);
  1080. req->error = 1;
  1081. return;
  1082. }
  1083. memcpy(bitmap_buf, &bitmap[off / sizeof(bitmap[0])], len);
  1084. *bitmap_io = ((struct bitmap_io)
  1085. { .count = ATOMIC_INIT(0),
  1086. .aio = INIT_AIO(AIO_WRITE, req->fds[1],
  1087. bitmap_buf, len,
  1088. req->bitmap_offset + off,
  1089. ubd_reply_fd) } );
  1090. }
  1091. nsectors = req->length / req->sectorsize;
  1092. start = 0;
  1093. end = nsectors;
  1094. bit = 0;
  1095. do {
  1096. if(bitmap != NULL){
  1097. sector = req->offset / req->sectorsize;
  1098. bit = ubd_test_bit(sector + start, bitmap);
  1099. end = start;
  1100. while((end < nsectors) &&
  1101. (ubd_test_bit(sector + end, bitmap) == bit))
  1102. end++;
  1103. }
  1104. off = req->offsets[bit] + req->offset +
  1105. start * req->sectorsize;
  1106. len = (end - start) * req->sectorsize;
  1107. buf = &req->buffer[start * req->sectorsize];
  1108. aio = kmalloc(sizeof(*aio), GFP_KERNEL);
  1109. if(aio == NULL){
  1110. req->error = 1;
  1111. return;
  1112. }
  1113. *aio = ((struct ubd_aio)
  1114. { .aio = INIT_AIO(req->op, req->fds[bit], buf,
  1115. len, off, ubd_reply_fd),
  1116. .len = len,
  1117. .req = r,
  1118. .bitmap = bitmap_io,
  1119. .bitmap_buf = bitmap_buf });
  1120. if(aio->bitmap != NULL)
  1121. atomic_inc(&aio->bitmap->count);
  1122. err = submit_aio(&aio->aio);
  1123. if(err){
  1124. printk("do_io - submit_aio failed, "
  1125. "err = %d\n", err);
  1126. req->error = 1;
  1127. return;
  1128. }
  1129. start = end;
  1130. } while(start < nsectors);
  1131. }