ubd_kern.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  1. /*
  2. * Copyright (C) 2000 Jeff Dike ([email protected])
  3. * Licensed under the GPL
  4. */
  5. /* 2001-09-28...2002-04-17
  6. * Partition stuff by [email protected]
  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_pread_file(fd, buf, len, offset);
  462. if (err < 0)
  463. return err;
  464. return 0;
  465. }
  466. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  467. {
  468. unsigned long modtime;
  469. unsigned long long actual;
  470. int err;
  471. err = os_file_modtime(file, &modtime);
  472. if (err < 0) {
  473. printk(KERN_ERR "Failed to get modification time of backing "
  474. "file \"%s\", err = %d\n", file, -err);
  475. return err;
  476. }
  477. err = os_file_size(file, &actual);
  478. if (err < 0) {
  479. printk(KERN_ERR "Failed to get size of backing file \"%s\", "
  480. "err = %d\n", file, -err);
  481. return err;
  482. }
  483. if (actual != size) {
  484. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  485. * the typecast.*/
  486. printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
  487. "vs backing file\n", (unsigned long long) size, actual);
  488. return -EINVAL;
  489. }
  490. if (modtime != mtime) {
  491. printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs "
  492. "backing file\n", mtime, modtime);
  493. return -EINVAL;
  494. }
  495. return 0;
  496. }
  497. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  498. {
  499. struct uml_stat buf1, buf2;
  500. int err;
  501. if (from_cmdline == NULL)
  502. return 0;
  503. if (!strcmp(from_cmdline, from_cow))
  504. return 0;
  505. err = os_stat_file(from_cmdline, &buf1);
  506. if (err < 0) {
  507. printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
  508. -err);
  509. return 0;
  510. }
  511. err = os_stat_file(from_cow, &buf2);
  512. if (err < 0) {
  513. printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
  514. -err);
  515. return 1;
  516. }
  517. if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  518. return 0;
  519. printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
  520. "\"%s\" specified in COW header of \"%s\"\n",
  521. from_cmdline, from_cow, cow);
  522. return 1;
  523. }
  524. static int open_ubd_file(char *file, struct openflags *openflags, int shared,
  525. char **backing_file_out, int *bitmap_offset_out,
  526. unsigned long *bitmap_len_out, int *data_offset_out,
  527. int *create_cow_out)
  528. {
  529. time_t mtime;
  530. unsigned long long size;
  531. __u32 version, align;
  532. char *backing_file;
  533. int fd, err, sectorsize, asked_switch, mode = 0644;
  534. fd = os_open_file(file, *openflags, mode);
  535. if (fd < 0) {
  536. if ((fd == -ENOENT) && (create_cow_out != NULL))
  537. *create_cow_out = 1;
  538. if (!openflags->w ||
  539. ((fd != -EROFS) && (fd != -EACCES)))
  540. return fd;
  541. openflags->w = 0;
  542. fd = os_open_file(file, *openflags, mode);
  543. if (fd < 0)
  544. return fd;
  545. }
  546. if (shared)
  547. printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
  548. else {
  549. err = os_lock_file(fd, openflags->w);
  550. if (err < 0) {
  551. printk(KERN_ERR "Failed to lock '%s', err = %d\n",
  552. file, -err);
  553. goto out_close;
  554. }
  555. }
  556. /* Successful return case! */
  557. if (backing_file_out == NULL)
  558. return fd;
  559. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  560. &size, &sectorsize, &align, bitmap_offset_out);
  561. if (err && (*backing_file_out != NULL)) {
  562. printk(KERN_ERR "Failed to read COW header from COW file "
  563. "\"%s\", errno = %d\n", file, -err);
  564. goto out_close;
  565. }
  566. if (err)
  567. return fd;
  568. asked_switch = path_requires_switch(*backing_file_out, backing_file,
  569. file);
  570. /* Allow switching only if no mismatch. */
  571. if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
  572. mtime)) {
  573. printk(KERN_ERR "Switching backing file to '%s'\n",
  574. *backing_file_out);
  575. err = write_cow_header(file, fd, *backing_file_out,
  576. sectorsize, align, &size);
  577. if (err) {
  578. printk(KERN_ERR "Switch failed, errno = %d\n", -err);
  579. goto out_close;
  580. }
  581. } else {
  582. *backing_file_out = backing_file;
  583. err = backing_file_mismatch(*backing_file_out, size, mtime);
  584. if (err)
  585. goto out_close;
  586. }
  587. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  588. bitmap_len_out, data_offset_out);
  589. return fd;
  590. out_close:
  591. os_close_file(fd);
  592. return err;
  593. }
  594. static int create_cow_file(char *cow_file, char *backing_file,
  595. struct openflags flags,
  596. int sectorsize, int alignment, int *bitmap_offset_out,
  597. unsigned long *bitmap_len_out, int *data_offset_out)
  598. {
  599. int err, fd;
  600. flags.c = 1;
  601. fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
  602. if (fd < 0) {
  603. err = fd;
  604. printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
  605. cow_file, -err);
  606. goto out;
  607. }
  608. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  609. bitmap_offset_out, bitmap_len_out,
  610. data_offset_out);
  611. if (!err)
  612. return fd;
  613. os_close_file(fd);
  614. out:
  615. return err;
  616. }
  617. static void ubd_close_dev(struct ubd *ubd_dev)
  618. {
  619. os_close_file(ubd_dev->fd);
  620. if(ubd_dev->cow.file == NULL)
  621. return;
  622. os_close_file(ubd_dev->cow.fd);
  623. vfree(ubd_dev->cow.bitmap);
  624. ubd_dev->cow.bitmap = NULL;
  625. }
  626. static int ubd_open_dev(struct ubd *ubd_dev)
  627. {
  628. struct openflags flags;
  629. char **back_ptr;
  630. int err, create_cow, *create_ptr;
  631. int fd;
  632. ubd_dev->openflags = ubd_dev->boot_openflags;
  633. create_cow = 0;
  634. create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
  635. back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
  636. fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
  637. back_ptr, &ubd_dev->cow.bitmap_offset,
  638. &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
  639. create_ptr);
  640. if((fd == -ENOENT) && create_cow){
  641. fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
  642. ubd_dev->openflags, 1 << 9, PAGE_SIZE,
  643. &ubd_dev->cow.bitmap_offset,
  644. &ubd_dev->cow.bitmap_len,
  645. &ubd_dev->cow.data_offset);
  646. if(fd >= 0){
  647. printk(KERN_INFO "Creating \"%s\" as COW file for "
  648. "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
  649. }
  650. }
  651. if(fd < 0){
  652. printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
  653. -fd);
  654. return fd;
  655. }
  656. ubd_dev->fd = fd;
  657. if(ubd_dev->cow.file != NULL){
  658. blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
  659. err = -ENOMEM;
  660. ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
  661. if(ubd_dev->cow.bitmap == NULL){
  662. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  663. goto error;
  664. }
  665. flush_tlb_kernel_vm();
  666. err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
  667. ubd_dev->cow.bitmap_offset,
  668. ubd_dev->cow.bitmap_len);
  669. if(err < 0)
  670. goto error;
  671. flags = ubd_dev->openflags;
  672. flags.w = 0;
  673. err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
  674. NULL, NULL, NULL, NULL);
  675. if(err < 0) goto error;
  676. ubd_dev->cow.fd = err;
  677. }
  678. return 0;
  679. error:
  680. os_close_file(ubd_dev->fd);
  681. return err;
  682. }
  683. static void ubd_device_release(struct device *dev)
  684. {
  685. struct ubd *ubd_dev = dev_get_drvdata(dev);
  686. blk_cleanup_queue(ubd_dev->queue);
  687. *ubd_dev = ((struct ubd) DEFAULT_UBD);
  688. }
  689. static int ubd_disk_register(int major, u64 size, int unit,
  690. struct gendisk **disk_out)
  691. {
  692. struct device *parent = NULL;
  693. struct gendisk *disk;
  694. disk = alloc_disk(1 << UBD_SHIFT);
  695. if(disk == NULL)
  696. return -ENOMEM;
  697. disk->major = major;
  698. disk->first_minor = unit << UBD_SHIFT;
  699. disk->fops = &ubd_blops;
  700. set_capacity(disk, size / 512);
  701. if (major == UBD_MAJOR)
  702. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  703. else
  704. sprintf(disk->disk_name, "ubd_fake%d", unit);
  705. /* sysfs register (not for ide fake devices) */
  706. if (major == UBD_MAJOR) {
  707. ubd_devs[unit].pdev.id = unit;
  708. ubd_devs[unit].pdev.name = DRIVER_NAME;
  709. ubd_devs[unit].pdev.dev.release = ubd_device_release;
  710. dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
  711. platform_device_register(&ubd_devs[unit].pdev);
  712. parent = &ubd_devs[unit].pdev.dev;
  713. }
  714. disk->private_data = &ubd_devs[unit];
  715. disk->queue = ubd_devs[unit].queue;
  716. device_add_disk(parent, disk);
  717. *disk_out = disk;
  718. return 0;
  719. }
  720. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  721. static int ubd_add(int n, char **error_out)
  722. {
  723. struct ubd *ubd_dev = &ubd_devs[n];
  724. int err = 0;
  725. if(ubd_dev->file == NULL)
  726. goto out;
  727. err = ubd_file_size(ubd_dev, &ubd_dev->size);
  728. if(err < 0){
  729. *error_out = "Couldn't determine size of device's file";
  730. goto out;
  731. }
  732. ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
  733. INIT_LIST_HEAD(&ubd_dev->restart);
  734. sg_init_table(ubd_dev->sg, MAX_SG);
  735. err = -ENOMEM;
  736. ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
  737. if (ubd_dev->queue == NULL) {
  738. *error_out = "Failed to initialize device queue";
  739. goto out;
  740. }
  741. ubd_dev->queue->queuedata = ubd_dev;
  742. blk_queue_write_cache(ubd_dev->queue, true, false);
  743. blk_queue_max_segments(ubd_dev->queue, MAX_SG);
  744. err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
  745. if(err){
  746. *error_out = "Failed to register device";
  747. goto out_cleanup;
  748. }
  749. if (fake_major != UBD_MAJOR)
  750. ubd_disk_register(fake_major, ubd_dev->size, n,
  751. &fake_gendisk[n]);
  752. /*
  753. * Perhaps this should also be under the "if (fake_major)" above
  754. * using the fake_disk->disk_name
  755. */
  756. if (fake_ide)
  757. make_ide_entries(ubd_gendisk[n]->disk_name);
  758. err = 0;
  759. out:
  760. return err;
  761. out_cleanup:
  762. blk_cleanup_queue(ubd_dev->queue);
  763. goto out;
  764. }
  765. static int ubd_config(char *str, char **error_out)
  766. {
  767. int n, ret;
  768. /* This string is possibly broken up and stored, so it's only
  769. * freed if ubd_setup_common fails, or if only general options
  770. * were set.
  771. */
  772. str = kstrdup(str, GFP_KERNEL);
  773. if (str == NULL) {
  774. *error_out = "Failed to allocate memory";
  775. return -ENOMEM;
  776. }
  777. ret = ubd_setup_common(str, &n, error_out);
  778. if (ret)
  779. goto err_free;
  780. if (n == -1) {
  781. ret = 0;
  782. goto err_free;
  783. }
  784. mutex_lock(&ubd_lock);
  785. ret = ubd_add(n, error_out);
  786. if (ret)
  787. ubd_devs[n].file = NULL;
  788. mutex_unlock(&ubd_lock);
  789. out:
  790. return ret;
  791. err_free:
  792. kfree(str);
  793. goto out;
  794. }
  795. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  796. {
  797. struct ubd *ubd_dev;
  798. int n, len = 0;
  799. n = parse_unit(&name);
  800. if((n >= MAX_DEV) || (n < 0)){
  801. *error_out = "ubd_get_config : device number out of range";
  802. return -1;
  803. }
  804. ubd_dev = &ubd_devs[n];
  805. mutex_lock(&ubd_lock);
  806. if(ubd_dev->file == NULL){
  807. CONFIG_CHUNK(str, size, len, "", 1);
  808. goto out;
  809. }
  810. CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
  811. if(ubd_dev->cow.file != NULL){
  812. CONFIG_CHUNK(str, size, len, ",", 0);
  813. CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
  814. }
  815. else CONFIG_CHUNK(str, size, len, "", 1);
  816. out:
  817. mutex_unlock(&ubd_lock);
  818. return len;
  819. }
  820. static int ubd_id(char **str, int *start_out, int *end_out)
  821. {
  822. int n;
  823. n = parse_unit(str);
  824. *start_out = 0;
  825. *end_out = MAX_DEV - 1;
  826. return n;
  827. }
  828. static int ubd_remove(int n, char **error_out)
  829. {
  830. struct gendisk *disk = ubd_gendisk[n];
  831. struct ubd *ubd_dev;
  832. int err = -ENODEV;
  833. mutex_lock(&ubd_lock);
  834. ubd_dev = &ubd_devs[n];
  835. if(ubd_dev->file == NULL)
  836. goto out;
  837. /* you cannot remove a open disk */
  838. err = -EBUSY;
  839. if(ubd_dev->count > 0)
  840. goto out;
  841. ubd_gendisk[n] = NULL;
  842. if(disk != NULL){
  843. del_gendisk(disk);
  844. put_disk(disk);
  845. }
  846. if(fake_gendisk[n] != NULL){
  847. del_gendisk(fake_gendisk[n]);
  848. put_disk(fake_gendisk[n]);
  849. fake_gendisk[n] = NULL;
  850. }
  851. err = 0;
  852. platform_device_unregister(&ubd_dev->pdev);
  853. out:
  854. mutex_unlock(&ubd_lock);
  855. return err;
  856. }
  857. /* All these are called by mconsole in process context and without
  858. * ubd-specific locks. The structure itself is const except for .list.
  859. */
  860. static struct mc_device ubd_mc = {
  861. .list = LIST_HEAD_INIT(ubd_mc.list),
  862. .name = "ubd",
  863. .config = ubd_config,
  864. .get_config = ubd_get_config,
  865. .id = ubd_id,
  866. .remove = ubd_remove,
  867. };
  868. static int __init ubd_mc_init(void)
  869. {
  870. mconsole_register_dev(&ubd_mc);
  871. return 0;
  872. }
  873. __initcall(ubd_mc_init);
  874. static int __init ubd0_init(void)
  875. {
  876. struct ubd *ubd_dev = &ubd_devs[0];
  877. mutex_lock(&ubd_lock);
  878. if(ubd_dev->file == NULL)
  879. ubd_dev->file = "root_fs";
  880. mutex_unlock(&ubd_lock);
  881. return 0;
  882. }
  883. __initcall(ubd0_init);
  884. /* Used in ubd_init, which is an initcall */
  885. static struct platform_driver ubd_driver = {
  886. .driver = {
  887. .name = DRIVER_NAME,
  888. },
  889. };
  890. static int __init ubd_init(void)
  891. {
  892. char *error;
  893. int i, err;
  894. if (register_blkdev(UBD_MAJOR, "ubd"))
  895. return -1;
  896. if (fake_major != UBD_MAJOR) {
  897. char name[sizeof("ubd_nnn\0")];
  898. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  899. if (register_blkdev(fake_major, "ubd"))
  900. return -1;
  901. }
  902. platform_driver_register(&ubd_driver);
  903. mutex_lock(&ubd_lock);
  904. for (i = 0; i < MAX_DEV; i++){
  905. err = ubd_add(i, &error);
  906. if(err)
  907. printk(KERN_ERR "Failed to initialize ubd device %d :"
  908. "%s\n", i, error);
  909. }
  910. mutex_unlock(&ubd_lock);
  911. return 0;
  912. }
  913. late_initcall(ubd_init);
  914. static int __init ubd_driver_init(void){
  915. unsigned long stack;
  916. int err;
  917. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  918. if(global_openflags.s){
  919. printk(KERN_INFO "ubd: Synchronous mode\n");
  920. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  921. * enough. So use anyway the io thread. */
  922. }
  923. stack = alloc_stack(0, 0);
  924. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  925. &thread_fd);
  926. if(io_pid < 0){
  927. printk(KERN_ERR
  928. "ubd : Failed to start I/O thread (errno = %d) - "
  929. "falling back to synchronous I/O\n", -io_pid);
  930. io_pid = -1;
  931. return 0;
  932. }
  933. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  934. 0, "ubd", ubd_devs);
  935. if(err != 0)
  936. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  937. return 0;
  938. }
  939. device_initcall(ubd_driver_init);
  940. static int ubd_open(struct block_device *bdev, fmode_t mode)
  941. {
  942. struct gendisk *disk = bdev->bd_disk;
  943. struct ubd *ubd_dev = disk->private_data;
  944. int err = 0;
  945. mutex_lock(&ubd_mutex);
  946. if(ubd_dev->count == 0){
  947. err = ubd_open_dev(ubd_dev);
  948. if(err){
  949. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  950. disk->disk_name, ubd_dev->file, -err);
  951. goto out;
  952. }
  953. }
  954. ubd_dev->count++;
  955. set_disk_ro(disk, !ubd_dev->openflags.w);
  956. /* This should no more be needed. And it didn't work anyway to exclude
  957. * read-write remounting of filesystems.*/
  958. /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
  959. if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
  960. err = -EROFS;
  961. }*/
  962. out:
  963. mutex_unlock(&ubd_mutex);
  964. return err;
  965. }
  966. static void ubd_release(struct gendisk *disk, fmode_t mode)
  967. {
  968. struct ubd *ubd_dev = disk->private_data;
  969. mutex_lock(&ubd_mutex);
  970. if(--ubd_dev->count == 0)
  971. ubd_close_dev(ubd_dev);
  972. mutex_unlock(&ubd_mutex);
  973. }
  974. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  975. __u64 *cow_offset, unsigned long *bitmap,
  976. __u64 bitmap_offset, unsigned long *bitmap_words,
  977. __u64 bitmap_len)
  978. {
  979. __u64 sector = io_offset >> 9;
  980. int i, update_bitmap = 0;
  981. for(i = 0; i < length >> 9; i++){
  982. if(cow_mask != NULL)
  983. ubd_set_bit(i, (unsigned char *) cow_mask);
  984. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  985. continue;
  986. update_bitmap = 1;
  987. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  988. }
  989. if(!update_bitmap)
  990. return;
  991. *cow_offset = sector / (sizeof(unsigned long) * 8);
  992. /* This takes care of the case where we're exactly at the end of the
  993. * device, and *cow_offset + 1 is off the end. So, just back it up
  994. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  995. * for the original diagnosis.
  996. */
  997. if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
  998. sizeof(unsigned long)) - 1))
  999. (*cow_offset)--;
  1000. bitmap_words[0] = bitmap[*cow_offset];
  1001. bitmap_words[1] = bitmap[*cow_offset + 1];
  1002. *cow_offset *= sizeof(unsigned long);
  1003. *cow_offset += bitmap_offset;
  1004. }
  1005. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  1006. __u64 bitmap_offset, __u64 bitmap_len)
  1007. {
  1008. __u64 sector = req->offset >> 9;
  1009. int i;
  1010. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  1011. panic("Operation too long");
  1012. if(req->op == UBD_READ) {
  1013. for(i = 0; i < req->length >> 9; i++){
  1014. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  1015. ubd_set_bit(i, (unsigned char *)
  1016. &req->sector_mask);
  1017. }
  1018. }
  1019. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  1020. &req->cow_offset, bitmap, bitmap_offset,
  1021. req->bitmap_words, bitmap_len);
  1022. }
  1023. /* Called with dev->lock held */
  1024. static void prepare_request(struct request *req, struct io_thread_req *io_req,
  1025. unsigned long long offset, int page_offset,
  1026. int len, struct page *page)
  1027. {
  1028. struct gendisk *disk = req->rq_disk;
  1029. struct ubd *ubd_dev = disk->private_data;
  1030. io_req->req = req;
  1031. io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
  1032. ubd_dev->fd;
  1033. io_req->fds[1] = ubd_dev->fd;
  1034. io_req->cow_offset = -1;
  1035. io_req->offset = offset;
  1036. io_req->length = len;
  1037. io_req->error = 0;
  1038. io_req->sector_mask = 0;
  1039. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  1040. io_req->offsets[0] = 0;
  1041. io_req->offsets[1] = ubd_dev->cow.data_offset;
  1042. io_req->buffer = page_address(page) + page_offset;
  1043. io_req->sectorsize = 1 << 9;
  1044. if(ubd_dev->cow.file != NULL)
  1045. cowify_req(io_req, ubd_dev->cow.bitmap,
  1046. ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len);
  1047. }
  1048. /* Called with dev->lock held */
  1049. static void prepare_flush_request(struct request *req,
  1050. struct io_thread_req *io_req)
  1051. {
  1052. struct gendisk *disk = req->rq_disk;
  1053. struct ubd *ubd_dev = disk->private_data;
  1054. io_req->req = req;
  1055. io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
  1056. ubd_dev->fd;
  1057. io_req->op = UBD_FLUSH;
  1058. }
  1059. static bool submit_request(struct io_thread_req *io_req, struct ubd *dev)
  1060. {
  1061. int n = os_write_file(thread_fd, &io_req,
  1062. sizeof(io_req));
  1063. if (n != sizeof(io_req)) {
  1064. if (n != -EAGAIN)
  1065. printk("write to io thread failed, "
  1066. "errno = %d\n", -n);
  1067. else if (list_empty(&dev->restart))
  1068. list_add(&dev->restart, &restart);
  1069. kfree(io_req);
  1070. return false;
  1071. }
  1072. return true;
  1073. }
  1074. /* Called with dev->lock held */
  1075. static void do_ubd_request(struct request_queue *q)
  1076. {
  1077. struct io_thread_req *io_req;
  1078. struct request *req;
  1079. while(1){
  1080. struct ubd *dev = q->queuedata;
  1081. if(dev->request == NULL){
  1082. struct request *req = blk_fetch_request(q);
  1083. if(req == NULL)
  1084. return;
  1085. dev->request = req;
  1086. dev->rq_pos = blk_rq_pos(req);
  1087. dev->start_sg = 0;
  1088. dev->end_sg = blk_rq_map_sg(q, req, dev->sg);
  1089. }
  1090. req = dev->request;
  1091. if (req_op(req) == REQ_OP_FLUSH) {
  1092. io_req = kmalloc(sizeof(struct io_thread_req),
  1093. GFP_ATOMIC);
  1094. if (io_req == NULL) {
  1095. if (list_empty(&dev->restart))
  1096. list_add(&dev->restart, &restart);
  1097. return;
  1098. }
  1099. prepare_flush_request(req, io_req);
  1100. if (submit_request(io_req, dev) == false)
  1101. return;
  1102. }
  1103. while(dev->start_sg < dev->end_sg){
  1104. struct scatterlist *sg = &dev->sg[dev->start_sg];
  1105. io_req = kmalloc(sizeof(struct io_thread_req),
  1106. GFP_ATOMIC);
  1107. if(io_req == NULL){
  1108. if(list_empty(&dev->restart))
  1109. list_add(&dev->restart, &restart);
  1110. return;
  1111. }
  1112. prepare_request(req, io_req,
  1113. (unsigned long long)dev->rq_pos << 9,
  1114. sg->offset, sg->length, sg_page(sg));
  1115. if (submit_request(io_req, dev) == false)
  1116. return;
  1117. dev->rq_pos += sg->length >> 9;
  1118. dev->start_sg++;
  1119. }
  1120. dev->end_sg = 0;
  1121. dev->request = NULL;
  1122. }
  1123. }
  1124. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1125. {
  1126. struct ubd *ubd_dev = bdev->bd_disk->private_data;
  1127. geo->heads = 128;
  1128. geo->sectors = 32;
  1129. geo->cylinders = ubd_dev->size / (128 * 32 * 512);
  1130. return 0;
  1131. }
  1132. static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
  1133. unsigned int cmd, unsigned long arg)
  1134. {
  1135. struct ubd *ubd_dev = bdev->bd_disk->private_data;
  1136. u16 ubd_id[ATA_ID_WORDS];
  1137. switch (cmd) {
  1138. struct cdrom_volctrl volume;
  1139. case HDIO_GET_IDENTITY:
  1140. memset(&ubd_id, 0, ATA_ID_WORDS * 2);
  1141. ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
  1142. ubd_id[ATA_ID_HEADS] = 128;
  1143. ubd_id[ATA_ID_SECTORS] = 32;
  1144. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  1145. sizeof(ubd_id)))
  1146. return -EFAULT;
  1147. return 0;
  1148. case CDROMVOLREAD:
  1149. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  1150. return -EFAULT;
  1151. volume.channel0 = 255;
  1152. volume.channel1 = 255;
  1153. volume.channel2 = 255;
  1154. volume.channel3 = 255;
  1155. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  1156. return -EFAULT;
  1157. return 0;
  1158. }
  1159. return -EINVAL;
  1160. }
  1161. static int update_bitmap(struct io_thread_req *req)
  1162. {
  1163. int n;
  1164. if(req->cow_offset == -1)
  1165. return 0;
  1166. n = os_pwrite_file(req->fds[1], &req->bitmap_words,
  1167. sizeof(req->bitmap_words), req->cow_offset);
  1168. if(n != sizeof(req->bitmap_words)){
  1169. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1170. req->fds[1]);
  1171. return 1;
  1172. }
  1173. return 0;
  1174. }
  1175. static void do_io(struct io_thread_req *req)
  1176. {
  1177. char *buf;
  1178. unsigned long len;
  1179. int n, nsectors, start, end, bit;
  1180. __u64 off;
  1181. if (req->op == UBD_FLUSH) {
  1182. /* fds[0] is always either the rw image or our cow file */
  1183. n = os_sync_file(req->fds[0]);
  1184. if (n != 0) {
  1185. printk("do_io - sync failed err = %d "
  1186. "fd = %d\n", -n, req->fds[0]);
  1187. req->error = 1;
  1188. }
  1189. return;
  1190. }
  1191. nsectors = req->length / req->sectorsize;
  1192. start = 0;
  1193. do {
  1194. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1195. end = start;
  1196. while((end < nsectors) &&
  1197. (ubd_test_bit(end, (unsigned char *)
  1198. &req->sector_mask) == bit))
  1199. end++;
  1200. off = req->offset + req->offsets[bit] +
  1201. start * req->sectorsize;
  1202. len = (end - start) * req->sectorsize;
  1203. buf = &req->buffer[start * req->sectorsize];
  1204. if(req->op == UBD_READ){
  1205. n = 0;
  1206. do {
  1207. buf = &buf[n];
  1208. len -= n;
  1209. n = os_pread_file(req->fds[bit], buf, len, off);
  1210. if (n < 0) {
  1211. printk("do_io - read failed, err = %d "
  1212. "fd = %d\n", -n, req->fds[bit]);
  1213. req->error = 1;
  1214. return;
  1215. }
  1216. } while((n < len) && (n != 0));
  1217. if (n < len) memset(&buf[n], 0, len - n);
  1218. } else {
  1219. n = os_pwrite_file(req->fds[bit], buf, len, off);
  1220. if(n != len){
  1221. printk("do_io - write failed err = %d "
  1222. "fd = %d\n", -n, req->fds[bit]);
  1223. req->error = 1;
  1224. return;
  1225. }
  1226. }
  1227. start = end;
  1228. } while(start < nsectors);
  1229. req->error = update_bitmap(req);
  1230. }
  1231. /* Changed in start_io_thread, which is serialized by being called only
  1232. * from ubd_init, which is an initcall.
  1233. */
  1234. int kernel_fd = -1;
  1235. /* Only changed by the io thread. XXX: currently unused. */
  1236. static int io_count = 0;
  1237. int io_thread(void *arg)
  1238. {
  1239. struct io_thread_req *req;
  1240. int n;
  1241. os_fix_helper_signals();
  1242. while(1){
  1243. n = os_read_file(kernel_fd, &req,
  1244. sizeof(struct io_thread_req *));
  1245. if(n != sizeof(struct io_thread_req *)){
  1246. if(n < 0)
  1247. printk("io_thread - read failed, fd = %d, "
  1248. "err = %d\n", kernel_fd, -n);
  1249. else {
  1250. printk("io_thread - short read, fd = %d, "
  1251. "length = %d\n", kernel_fd, n);
  1252. }
  1253. continue;
  1254. }
  1255. io_count++;
  1256. do_io(req);
  1257. n = os_write_file(kernel_fd, &req,
  1258. sizeof(struct io_thread_req *));
  1259. if(n != sizeof(struct io_thread_req *))
  1260. printk("io_thread - write failed, fd = %d, err = %d\n",
  1261. kernel_fd, -n);
  1262. }
  1263. return 0;
  1264. }