ubd_kern.c 35 KB

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