ubd_kern.c 34 KB

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