ubd_kern.c 33 KB

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