ubd_kern.c 34 KB

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