ubd_kern.c 34 KB

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