ubd_kern.c 33 KB

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