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/mutex.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 DEFINE_MUTEX(ubd_mutex); /* replaces BKL, might not be needed */
  93. static int ubd_open(struct block_device *bdev, fmode_t mode);
  94. static int ubd_release(struct gendisk *disk, fmode_t mode);
  95. static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
  96. unsigned int cmd, unsigned long arg);
  97. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  98. #define MAX_DEV (16)
  99. static const struct block_device_operations ubd_blops = {
  100. .owner = THIS_MODULE,
  101. .open = ubd_open,
  102. .release = ubd_release,
  103. .ioctl = ubd_ioctl,
  104. .getgeo = ubd_getgeo,
  105. };
  106. /* Protected by ubd_lock */
  107. static int fake_major = UBD_MAJOR;
  108. static struct gendisk *ubd_gendisk[MAX_DEV];
  109. static struct gendisk *fake_gendisk[MAX_DEV];
  110. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  111. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  112. .cl = 1 })
  113. #else
  114. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  115. .cl = 1 })
  116. #endif
  117. static struct openflags global_openflags = OPEN_FLAGS;
  118. struct cow {
  119. /* backing file name */
  120. char *file;
  121. /* backing file fd */
  122. int fd;
  123. unsigned long *bitmap;
  124. unsigned long bitmap_len;
  125. int bitmap_offset;
  126. int data_offset;
  127. };
  128. #define MAX_SG 64
  129. struct ubd {
  130. struct list_head restart;
  131. /* name (and fd, below) of the file opened for writing, either the
  132. * backing or the cow file. */
  133. char *file;
  134. int count;
  135. int fd;
  136. __u64 size;
  137. struct openflags boot_openflags;
  138. struct openflags openflags;
  139. unsigned shared:1;
  140. unsigned no_cow:1;
  141. struct cow cow;
  142. struct platform_device pdev;
  143. struct request_queue *queue;
  144. spinlock_t lock;
  145. struct scatterlist sg[MAX_SG];
  146. struct request *request;
  147. int start_sg, end_sg;
  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. }
  171. /* Protected by ubd_lock */
  172. static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
  173. /* Only changed by fake_ide_setup which is a setup */
  174. static int fake_ide = 0;
  175. static struct proc_dir_entry *proc_ide_root = NULL;
  176. static struct proc_dir_entry *proc_ide = NULL;
  177. static void make_proc_ide(void)
  178. {
  179. proc_ide_root = proc_mkdir("ide", NULL);
  180. proc_ide = proc_mkdir("ide0", proc_ide_root);
  181. }
  182. static int fake_ide_media_proc_show(struct seq_file *m, void *v)
  183. {
  184. seq_puts(m, "disk\n");
  185. return 0;
  186. }
  187. static int fake_ide_media_proc_open(struct inode *inode, struct file *file)
  188. {
  189. return single_open(file, fake_ide_media_proc_show, NULL);
  190. }
  191. static const struct file_operations fake_ide_media_proc_fops = {
  192. .owner = THIS_MODULE,
  193. .open = fake_ide_media_proc_open,
  194. .read = seq_read,
  195. .llseek = seq_lseek,
  196. .release = single_release,
  197. };
  198. static void make_ide_entries(const 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 = proc_create("media", S_IRUGO, dir, &fake_ide_media_proc_fops);
  206. if(!ent) return;
  207. snprintf(name, sizeof(name), "ide0/%s", dev_name);
  208. proc_symlink(dev_name, proc_ide_root, name);
  209. }
  210. static int fake_ide_setup(char *str)
  211. {
  212. fake_ide = 1;
  213. return 1;
  214. }
  215. __setup("fake_ide", fake_ide_setup);
  216. __uml_help(fake_ide_setup,
  217. "fake_ide\n"
  218. " Create ide0 entries that map onto ubd devices.\n\n"
  219. );
  220. static int parse_unit(char **ptr)
  221. {
  222. char *str = *ptr, *end;
  223. int n = -1;
  224. if(isdigit(*str)) {
  225. n = simple_strtoul(str, &end, 0);
  226. if(end == str)
  227. return -1;
  228. *ptr = end;
  229. }
  230. else if (('a' <= *str) && (*str <= 'z')) {
  231. n = *str - 'a';
  232. str++;
  233. *ptr = str;
  234. }
  235. return n;
  236. }
  237. /* If *index_out == -1 at exit, the passed option was a general one;
  238. * otherwise, the str pointer is used (and owned) inside ubd_devs array, so it
  239. * should not be freed on exit.
  240. */
  241. static int ubd_setup_common(char *str, int *index_out, char **error_out)
  242. {
  243. struct ubd *ubd_dev;
  244. struct openflags flags = global_openflags;
  245. char *backing_file;
  246. int n, err = 0, i;
  247. if(index_out) *index_out = -1;
  248. n = *str;
  249. if(n == '='){
  250. char *end;
  251. int major;
  252. str++;
  253. if(!strcmp(str, "sync")){
  254. global_openflags = of_sync(global_openflags);
  255. goto out1;
  256. }
  257. err = -EINVAL;
  258. major = simple_strtoul(str, &end, 0);
  259. if((*end != '\0') || (end == str)){
  260. *error_out = "Didn't parse major number";
  261. goto out1;
  262. }
  263. mutex_lock(&ubd_lock);
  264. if (fake_major != UBD_MAJOR) {
  265. *error_out = "Can't assign a fake major twice";
  266. goto out1;
  267. }
  268. fake_major = major;
  269. printk(KERN_INFO "Setting extra ubd major number to %d\n",
  270. major);
  271. err = 0;
  272. out1:
  273. mutex_unlock(&ubd_lock);
  274. return err;
  275. }
  276. n = parse_unit(&str);
  277. if(n < 0){
  278. *error_out = "Couldn't parse device number";
  279. return -EINVAL;
  280. }
  281. if(n >= MAX_DEV){
  282. *error_out = "Device number out of range";
  283. return 1;
  284. }
  285. err = -EBUSY;
  286. mutex_lock(&ubd_lock);
  287. ubd_dev = &ubd_devs[n];
  288. if(ubd_dev->file != NULL){
  289. *error_out = "Device is already configured";
  290. goto out;
  291. }
  292. if (index_out)
  293. *index_out = n;
  294. err = -EINVAL;
  295. for (i = 0; i < sizeof("rscd="); i++) {
  296. switch (*str) {
  297. case 'r':
  298. flags.w = 0;
  299. break;
  300. case 's':
  301. flags.s = 1;
  302. break;
  303. case 'd':
  304. ubd_dev->no_cow = 1;
  305. break;
  306. case 'c':
  307. ubd_dev->shared = 1;
  308. break;
  309. case '=':
  310. str++;
  311. goto break_loop;
  312. default:
  313. *error_out = "Expected '=' or flag letter "
  314. "(r, s, c, or d)";
  315. goto out;
  316. }
  317. str++;
  318. }
  319. if (*str == '=')
  320. *error_out = "Too many flags specified";
  321. else
  322. *error_out = "Missing '='";
  323. goto out;
  324. break_loop:
  325. backing_file = strchr(str, ',');
  326. if (backing_file == NULL)
  327. backing_file = strchr(str, ':');
  328. if(backing_file != NULL){
  329. if(ubd_dev->no_cow){
  330. *error_out = "Can't specify both 'd' and a cow file";
  331. goto out;
  332. }
  333. else {
  334. *backing_file = '\0';
  335. backing_file++;
  336. }
  337. }
  338. err = 0;
  339. ubd_dev->file = str;
  340. ubd_dev->cow.file = backing_file;
  341. ubd_dev->boot_openflags = flags;
  342. out:
  343. mutex_unlock(&ubd_lock);
  344. return err;
  345. }
  346. static int ubd_setup(char *str)
  347. {
  348. char *error;
  349. int err;
  350. err = ubd_setup_common(str, NULL, &error);
  351. if(err)
  352. printk(KERN_ERR "Failed to initialize device with \"%s\" : "
  353. "%s\n", str, error);
  354. return 1;
  355. }
  356. __setup("ubd", ubd_setup);
  357. __uml_help(ubd_setup,
  358. "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
  359. " This is used to associate a device with a file in the underlying\n"
  360. " filesystem. When specifying two filenames, the first one is the\n"
  361. " COW name and the second is the backing file name. As separator you can\n"
  362. " use either a ':' or a ',': the first one allows writing things like;\n"
  363. " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
  364. " while with a ',' the shell would not expand the 2nd '~'.\n"
  365. " When using only one filename, UML will detect whether to treat it like\n"
  366. " a COW file or a backing file. To override this detection, add the 'd'\n"
  367. " flag:\n"
  368. " ubd0d=BackingFile\n"
  369. " Usually, there is a filesystem in the file, but \n"
  370. " that's not required. Swap devices containing swap files can be\n"
  371. " specified like this. Also, a file which doesn't contain a\n"
  372. " filesystem can have its contents read in the virtual \n"
  373. " machine by running 'dd' on the device. <n> must be in the range\n"
  374. " 0 to 7. Appending an 'r' to the number will cause that device\n"
  375. " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
  376. " an 's' will cause data to be written to disk on the host immediately.\n"
  377. " 'c' will cause the device to be treated as being shared between multiple\n"
  378. " UMLs and file locking will be turned off - this is appropriate for a\n"
  379. " cluster filesystem and inappropriate at almost all other times.\n\n"
  380. );
  381. static int udb_setup(char *str)
  382. {
  383. printk("udb%s specified on command line is almost certainly a ubd -> "
  384. "udb TYPO\n", str);
  385. return 1;
  386. }
  387. __setup("udb", udb_setup);
  388. __uml_help(udb_setup,
  389. "udb\n"
  390. " This option is here solely to catch ubd -> udb typos, which can be\n"
  391. " to impossible to catch visually unless you specifically look for\n"
  392. " them. The only result of any option starting with 'udb' is an error\n"
  393. " in the boot output.\n\n"
  394. );
  395. static void do_ubd_request(struct request_queue * q);
  396. /* Only changed by ubd_init, which is an initcall. */
  397. static int thread_fd = -1;
  398. static LIST_HEAD(restart);
  399. /* XXX - move this inside ubd_intr. */
  400. /* Called without dev->lock held, and only in interrupt context. */
  401. static void ubd_handler(void)
  402. {
  403. struct io_thread_req *req;
  404. struct ubd *ubd;
  405. struct list_head *list, *next_ele;
  406. unsigned long flags;
  407. int n;
  408. while(1){
  409. n = os_read_file(thread_fd, &req,
  410. sizeof(struct io_thread_req *));
  411. if(n != sizeof(req)){
  412. if(n == -EAGAIN)
  413. break;
  414. printk(KERN_ERR "spurious interrupt in ubd_handler, "
  415. "err = %d\n", -n);
  416. return;
  417. }
  418. blk_end_request(req->req, 0, req->length);
  419. kfree(req);
  420. }
  421. reactivate_fd(thread_fd, UBD_IRQ);
  422. list_for_each_safe(list, next_ele, &restart){
  423. ubd = container_of(list, struct ubd, restart);
  424. list_del_init(&ubd->restart);
  425. spin_lock_irqsave(&ubd->lock, flags);
  426. do_ubd_request(ubd->queue);
  427. spin_unlock_irqrestore(&ubd->lock, flags);
  428. }
  429. }
  430. static irqreturn_t ubd_intr(int irq, void *dev)
  431. {
  432. ubd_handler();
  433. return IRQ_HANDLED;
  434. }
  435. /* Only changed by ubd_init, which is an initcall. */
  436. static int io_pid = -1;
  437. static void kill_io_thread(void)
  438. {
  439. if(io_pid != -1)
  440. os_kill_process(io_pid, 1);
  441. }
  442. __uml_exitcall(kill_io_thread);
  443. static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
  444. {
  445. char *file;
  446. file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
  447. return os_file_size(file, size_out);
  448. }
  449. static int read_cow_bitmap(int fd, void *buf, int offset, int len)
  450. {
  451. int err;
  452. err = os_seek_file(fd, offset);
  453. if (err < 0)
  454. return err;
  455. err = os_read_file(fd, buf, len);
  456. if (err < 0)
  457. return err;
  458. return 0;
  459. }
  460. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  461. {
  462. unsigned long modtime;
  463. unsigned long long actual;
  464. int err;
  465. err = os_file_modtime(file, &modtime);
  466. if (err < 0) {
  467. printk(KERN_ERR "Failed to get modification time of backing "
  468. "file \"%s\", err = %d\n", file, -err);
  469. return err;
  470. }
  471. err = os_file_size(file, &actual);
  472. if (err < 0) {
  473. printk(KERN_ERR "Failed to get size of backing file \"%s\", "
  474. "err = %d\n", file, -err);
  475. return err;
  476. }
  477. if (actual != size) {
  478. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  479. * the typecast.*/
  480. printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
  481. "vs backing file\n", (unsigned long long) size, actual);
  482. return -EINVAL;
  483. }
  484. if (modtime != mtime) {
  485. printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs "
  486. "backing file\n", mtime, modtime);
  487. return -EINVAL;
  488. }
  489. return 0;
  490. }
  491. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  492. {
  493. struct uml_stat buf1, buf2;
  494. int err;
  495. if (from_cmdline == NULL)
  496. return 0;
  497. if (!strcmp(from_cmdline, from_cow))
  498. return 0;
  499. err = os_stat_file(from_cmdline, &buf1);
  500. if (err < 0) {
  501. printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
  502. -err);
  503. return 0;
  504. }
  505. err = os_stat_file(from_cow, &buf2);
  506. if (err < 0) {
  507. printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
  508. -err);
  509. return 1;
  510. }
  511. if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  512. return 0;
  513. printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
  514. "\"%s\" specified in COW header of \"%s\"\n",
  515. from_cmdline, from_cow, cow);
  516. return 1;
  517. }
  518. static int open_ubd_file(char *file, struct openflags *openflags, int shared,
  519. char **backing_file_out, int *bitmap_offset_out,
  520. unsigned long *bitmap_len_out, int *data_offset_out,
  521. int *create_cow_out)
  522. {
  523. time_t mtime;
  524. unsigned long long size;
  525. __u32 version, align;
  526. char *backing_file;
  527. int fd, err, sectorsize, asked_switch, mode = 0644;
  528. fd = os_open_file(file, *openflags, mode);
  529. if (fd < 0) {
  530. if ((fd == -ENOENT) && (create_cow_out != NULL))
  531. *create_cow_out = 1;
  532. if (!openflags->w ||
  533. ((fd != -EROFS) && (fd != -EACCES)))
  534. return fd;
  535. openflags->w = 0;
  536. fd = os_open_file(file, *openflags, mode);
  537. if (fd < 0)
  538. return fd;
  539. }
  540. if (shared)
  541. printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
  542. else {
  543. err = os_lock_file(fd, openflags->w);
  544. if (err < 0) {
  545. printk(KERN_ERR "Failed to lock '%s', err = %d\n",
  546. file, -err);
  547. goto out_close;
  548. }
  549. }
  550. /* Successful return case! */
  551. if (backing_file_out == NULL)
  552. return fd;
  553. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  554. &size, &sectorsize, &align, bitmap_offset_out);
  555. if (err && (*backing_file_out != NULL)) {
  556. printk(KERN_ERR "Failed to read COW header from COW file "
  557. "\"%s\", errno = %d\n", file, -err);
  558. goto out_close;
  559. }
  560. if (err)
  561. return fd;
  562. asked_switch = path_requires_switch(*backing_file_out, backing_file,
  563. file);
  564. /* Allow switching only if no mismatch. */
  565. if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
  566. mtime)) {
  567. printk(KERN_ERR "Switching backing file to '%s'\n",
  568. *backing_file_out);
  569. err = write_cow_header(file, fd, *backing_file_out,
  570. sectorsize, align, &size);
  571. if (err) {
  572. printk(KERN_ERR "Switch failed, errno = %d\n", -err);
  573. goto out_close;
  574. }
  575. } else {
  576. *backing_file_out = backing_file;
  577. err = backing_file_mismatch(*backing_file_out, size, mtime);
  578. if (err)
  579. goto out_close;
  580. }
  581. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  582. bitmap_len_out, data_offset_out);
  583. return fd;
  584. out_close:
  585. os_close_file(fd);
  586. return err;
  587. }
  588. static int create_cow_file(char *cow_file, char *backing_file,
  589. struct openflags flags,
  590. int sectorsize, int alignment, int *bitmap_offset_out,
  591. unsigned long *bitmap_len_out, int *data_offset_out)
  592. {
  593. int err, fd;
  594. flags.c = 1;
  595. fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
  596. if (fd < 0) {
  597. err = fd;
  598. printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
  599. cow_file, -err);
  600. goto out;
  601. }
  602. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  603. bitmap_offset_out, bitmap_len_out,
  604. data_offset_out);
  605. if (!err)
  606. return fd;
  607. os_close_file(fd);
  608. out:
  609. return err;
  610. }
  611. static void ubd_close_dev(struct ubd *ubd_dev)
  612. {
  613. os_close_file(ubd_dev->fd);
  614. if(ubd_dev->cow.file == NULL)
  615. return;
  616. os_close_file(ubd_dev->cow.fd);
  617. vfree(ubd_dev->cow.bitmap);
  618. ubd_dev->cow.bitmap = NULL;
  619. }
  620. static int ubd_open_dev(struct ubd *ubd_dev)
  621. {
  622. struct openflags flags;
  623. char **back_ptr;
  624. int err, create_cow, *create_ptr;
  625. int fd;
  626. ubd_dev->openflags = ubd_dev->boot_openflags;
  627. create_cow = 0;
  628. create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
  629. back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
  630. fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
  631. back_ptr, &ubd_dev->cow.bitmap_offset,
  632. &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
  633. create_ptr);
  634. if((fd == -ENOENT) && create_cow){
  635. fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
  636. ubd_dev->openflags, 1 << 9, PAGE_SIZE,
  637. &ubd_dev->cow.bitmap_offset,
  638. &ubd_dev->cow.bitmap_len,
  639. &ubd_dev->cow.data_offset);
  640. if(fd >= 0){
  641. printk(KERN_INFO "Creating \"%s\" as COW file for "
  642. "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
  643. }
  644. }
  645. if(fd < 0){
  646. printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
  647. -fd);
  648. return fd;
  649. }
  650. ubd_dev->fd = fd;
  651. if(ubd_dev->cow.file != NULL){
  652. blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
  653. err = -ENOMEM;
  654. ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
  655. if(ubd_dev->cow.bitmap == NULL){
  656. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  657. goto error;
  658. }
  659. flush_tlb_kernel_vm();
  660. err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
  661. ubd_dev->cow.bitmap_offset,
  662. ubd_dev->cow.bitmap_len);
  663. if(err < 0)
  664. goto error;
  665. flags = ubd_dev->openflags;
  666. flags.w = 0;
  667. err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
  668. NULL, NULL, NULL, NULL);
  669. if(err < 0) goto error;
  670. ubd_dev->cow.fd = err;
  671. }
  672. return 0;
  673. error:
  674. os_close_file(ubd_dev->fd);
  675. return err;
  676. }
  677. static void ubd_device_release(struct device *dev)
  678. {
  679. struct ubd *ubd_dev = dev_get_drvdata(dev);
  680. blk_cleanup_queue(ubd_dev->queue);
  681. *ubd_dev = ((struct ubd) DEFAULT_UBD);
  682. }
  683. static int ubd_disk_register(int major, u64 size, int unit,
  684. struct gendisk **disk_out)
  685. {
  686. struct gendisk *disk;
  687. disk = alloc_disk(1 << UBD_SHIFT);
  688. if(disk == NULL)
  689. return -ENOMEM;
  690. disk->major = major;
  691. disk->first_minor = unit << UBD_SHIFT;
  692. disk->fops = &ubd_blops;
  693. set_capacity(disk, size / 512);
  694. if (major == UBD_MAJOR)
  695. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  696. else
  697. sprintf(disk->disk_name, "ubd_fake%d", unit);
  698. /* sysfs register (not for ide fake devices) */
  699. if (major == UBD_MAJOR) {
  700. ubd_devs[unit].pdev.id = unit;
  701. ubd_devs[unit].pdev.name = DRIVER_NAME;
  702. ubd_devs[unit].pdev.dev.release = ubd_device_release;
  703. dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
  704. platform_device_register(&ubd_devs[unit].pdev);
  705. disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
  706. }
  707. disk->private_data = &ubd_devs[unit];
  708. disk->queue = ubd_devs[unit].queue;
  709. add_disk(disk);
  710. *disk_out = disk;
  711. return 0;
  712. }
  713. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  714. static int ubd_add(int n, char **error_out)
  715. {
  716. struct ubd *ubd_dev = &ubd_devs[n];
  717. int err = 0;
  718. if(ubd_dev->file == NULL)
  719. goto out;
  720. err = ubd_file_size(ubd_dev, &ubd_dev->size);
  721. if(err < 0){
  722. *error_out = "Couldn't determine size of device's file";
  723. goto out;
  724. }
  725. ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
  726. INIT_LIST_HEAD(&ubd_dev->restart);
  727. sg_init_table(ubd_dev->sg, MAX_SG);
  728. err = -ENOMEM;
  729. ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
  730. if (ubd_dev->queue == NULL) {
  731. *error_out = "Failed to initialize device queue";
  732. goto out;
  733. }
  734. ubd_dev->queue->queuedata = ubd_dev;
  735. blk_queue_max_segments(ubd_dev->queue, MAX_SG);
  736. err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
  737. if(err){
  738. *error_out = "Failed to register device";
  739. goto out_cleanup;
  740. }
  741. if (fake_major != UBD_MAJOR)
  742. ubd_disk_register(fake_major, ubd_dev->size, n,
  743. &fake_gendisk[n]);
  744. /*
  745. * Perhaps this should also be under the "if (fake_major)" above
  746. * using the fake_disk->disk_name
  747. */
  748. if (fake_ide)
  749. make_ide_entries(ubd_gendisk[n]->disk_name);
  750. err = 0;
  751. out:
  752. return err;
  753. out_cleanup:
  754. blk_cleanup_queue(ubd_dev->queue);
  755. goto out;
  756. }
  757. static int ubd_config(char *str, char **error_out)
  758. {
  759. int n, ret;
  760. /* This string is possibly broken up and stored, so it's only
  761. * freed if ubd_setup_common fails, or if only general options
  762. * were set.
  763. */
  764. str = kstrdup(str, GFP_KERNEL);
  765. if (str == NULL) {
  766. *error_out = "Failed to allocate memory";
  767. return -ENOMEM;
  768. }
  769. ret = ubd_setup_common(str, &n, error_out);
  770. if (ret)
  771. goto err_free;
  772. if (n == -1) {
  773. ret = 0;
  774. goto err_free;
  775. }
  776. mutex_lock(&ubd_lock);
  777. ret = ubd_add(n, error_out);
  778. if (ret)
  779. ubd_devs[n].file = NULL;
  780. mutex_unlock(&ubd_lock);
  781. out:
  782. return ret;
  783. err_free:
  784. kfree(str);
  785. goto out;
  786. }
  787. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  788. {
  789. struct ubd *ubd_dev;
  790. int n, len = 0;
  791. n = parse_unit(&name);
  792. if((n >= MAX_DEV) || (n < 0)){
  793. *error_out = "ubd_get_config : device number out of range";
  794. return -1;
  795. }
  796. ubd_dev = &ubd_devs[n];
  797. mutex_lock(&ubd_lock);
  798. if(ubd_dev->file == NULL){
  799. CONFIG_CHUNK(str, size, len, "", 1);
  800. goto out;
  801. }
  802. CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
  803. if(ubd_dev->cow.file != NULL){
  804. CONFIG_CHUNK(str, size, len, ",", 0);
  805. CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
  806. }
  807. else CONFIG_CHUNK(str, size, len, "", 1);
  808. out:
  809. mutex_unlock(&ubd_lock);
  810. return len;
  811. }
  812. static int ubd_id(char **str, int *start_out, int *end_out)
  813. {
  814. int n;
  815. n = parse_unit(str);
  816. *start_out = 0;
  817. *end_out = MAX_DEV - 1;
  818. return n;
  819. }
  820. static int ubd_remove(int n, char **error_out)
  821. {
  822. struct gendisk *disk = ubd_gendisk[n];
  823. struct ubd *ubd_dev;
  824. int err = -ENODEV;
  825. mutex_lock(&ubd_lock);
  826. ubd_dev = &ubd_devs[n];
  827. if(ubd_dev->file == NULL)
  828. goto out;
  829. /* you cannot remove a open disk */
  830. err = -EBUSY;
  831. if(ubd_dev->count > 0)
  832. goto out;
  833. ubd_gendisk[n] = NULL;
  834. if(disk != NULL){
  835. del_gendisk(disk);
  836. put_disk(disk);
  837. }
  838. if(fake_gendisk[n] != NULL){
  839. del_gendisk(fake_gendisk[n]);
  840. put_disk(fake_gendisk[n]);
  841. fake_gendisk[n] = NULL;
  842. }
  843. err = 0;
  844. platform_device_unregister(&ubd_dev->pdev);
  845. out:
  846. mutex_unlock(&ubd_lock);
  847. return err;
  848. }
  849. /* All these are called by mconsole in process context and without
  850. * ubd-specific locks. The structure itself is const except for .list.
  851. */
  852. static struct mc_device ubd_mc = {
  853. .list = LIST_HEAD_INIT(ubd_mc.list),
  854. .name = "ubd",
  855. .config = ubd_config,
  856. .get_config = ubd_get_config,
  857. .id = ubd_id,
  858. .remove = ubd_remove,
  859. };
  860. static int __init ubd_mc_init(void)
  861. {
  862. mconsole_register_dev(&ubd_mc);
  863. return 0;
  864. }
  865. __initcall(ubd_mc_init);
  866. static int __init ubd0_init(void)
  867. {
  868. struct ubd *ubd_dev = &ubd_devs[0];
  869. mutex_lock(&ubd_lock);
  870. if(ubd_dev->file == NULL)
  871. ubd_dev->file = "root_fs";
  872. mutex_unlock(&ubd_lock);
  873. return 0;
  874. }
  875. __initcall(ubd0_init);
  876. /* Used in ubd_init, which is an initcall */
  877. static struct platform_driver ubd_driver = {
  878. .driver = {
  879. .name = DRIVER_NAME,
  880. },
  881. };
  882. static int __init ubd_init(void)
  883. {
  884. char *error;
  885. int i, err;
  886. if (register_blkdev(UBD_MAJOR, "ubd"))
  887. return -1;
  888. if (fake_major != UBD_MAJOR) {
  889. char name[sizeof("ubd_nnn\0")];
  890. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  891. if (register_blkdev(fake_major, "ubd"))
  892. return -1;
  893. }
  894. platform_driver_register(&ubd_driver);
  895. mutex_lock(&ubd_lock);
  896. for (i = 0; i < MAX_DEV; i++){
  897. err = ubd_add(i, &error);
  898. if(err)
  899. printk(KERN_ERR "Failed to initialize ubd device %d :"
  900. "%s\n", i, error);
  901. }
  902. mutex_unlock(&ubd_lock);
  903. return 0;
  904. }
  905. late_initcall(ubd_init);
  906. static int __init ubd_driver_init(void){
  907. unsigned long stack;
  908. int err;
  909. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  910. if(global_openflags.s){
  911. printk(KERN_INFO "ubd: Synchronous mode\n");
  912. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  913. * enough. So use anyway the io thread. */
  914. }
  915. stack = alloc_stack(0, 0);
  916. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  917. &thread_fd);
  918. if(io_pid < 0){
  919. printk(KERN_ERR
  920. "ubd : Failed to start I/O thread (errno = %d) - "
  921. "falling back to synchronous I/O\n", -io_pid);
  922. io_pid = -1;
  923. return 0;
  924. }
  925. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  926. IRQF_DISABLED, "ubd", ubd_devs);
  927. if(err != 0)
  928. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  929. return 0;
  930. }
  931. device_initcall(ubd_driver_init);
  932. static int ubd_open(struct block_device *bdev, fmode_t mode)
  933. {
  934. struct gendisk *disk = bdev->bd_disk;
  935. struct ubd *ubd_dev = disk->private_data;
  936. int err = 0;
  937. mutex_lock(&ubd_mutex);
  938. if(ubd_dev->count == 0){
  939. err = ubd_open_dev(ubd_dev);
  940. if(err){
  941. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  942. disk->disk_name, ubd_dev->file, -err);
  943. goto out;
  944. }
  945. }
  946. ubd_dev->count++;
  947. set_disk_ro(disk, !ubd_dev->openflags.w);
  948. /* This should no more be needed. And it didn't work anyway to exclude
  949. * read-write remounting of filesystems.*/
  950. /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
  951. if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
  952. err = -EROFS;
  953. }*/
  954. out:
  955. mutex_unlock(&ubd_mutex);
  956. return err;
  957. }
  958. static int ubd_release(struct gendisk *disk, fmode_t mode)
  959. {
  960. struct ubd *ubd_dev = disk->private_data;
  961. mutex_lock(&ubd_mutex);
  962. if(--ubd_dev->count == 0)
  963. ubd_close_dev(ubd_dev);
  964. mutex_unlock(&ubd_mutex);
  965. return 0;
  966. }
  967. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  968. __u64 *cow_offset, unsigned long *bitmap,
  969. __u64 bitmap_offset, unsigned long *bitmap_words,
  970. __u64 bitmap_len)
  971. {
  972. __u64 sector = io_offset >> 9;
  973. int i, update_bitmap = 0;
  974. for(i = 0; i < length >> 9; i++){
  975. if(cow_mask != NULL)
  976. ubd_set_bit(i, (unsigned char *) cow_mask);
  977. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  978. continue;
  979. update_bitmap = 1;
  980. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  981. }
  982. if(!update_bitmap)
  983. return;
  984. *cow_offset = sector / (sizeof(unsigned long) * 8);
  985. /* This takes care of the case where we're exactly at the end of the
  986. * device, and *cow_offset + 1 is off the end. So, just back it up
  987. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  988. * for the original diagnosis.
  989. */
  990. if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
  991. sizeof(unsigned long)) - 1))
  992. (*cow_offset)--;
  993. bitmap_words[0] = bitmap[*cow_offset];
  994. bitmap_words[1] = bitmap[*cow_offset + 1];
  995. *cow_offset *= sizeof(unsigned long);
  996. *cow_offset += bitmap_offset;
  997. }
  998. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  999. __u64 bitmap_offset, __u64 bitmap_len)
  1000. {
  1001. __u64 sector = req->offset >> 9;
  1002. int i;
  1003. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  1004. panic("Operation too long");
  1005. if(req->op == UBD_READ) {
  1006. for(i = 0; i < req->length >> 9; i++){
  1007. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  1008. ubd_set_bit(i, (unsigned char *)
  1009. &req->sector_mask);
  1010. }
  1011. }
  1012. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  1013. &req->cow_offset, bitmap, bitmap_offset,
  1014. req->bitmap_words, bitmap_len);
  1015. }
  1016. /* Called with dev->lock held */
  1017. static void prepare_request(struct request *req, struct io_thread_req *io_req,
  1018. unsigned long long offset, int page_offset,
  1019. int len, struct page *page)
  1020. {
  1021. struct gendisk *disk = req->rq_disk;
  1022. struct ubd *ubd_dev = disk->private_data;
  1023. io_req->req = req;
  1024. io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
  1025. ubd_dev->fd;
  1026. io_req->fds[1] = ubd_dev->fd;
  1027. io_req->cow_offset = -1;
  1028. io_req->offset = offset;
  1029. io_req->length = len;
  1030. io_req->error = 0;
  1031. io_req->sector_mask = 0;
  1032. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  1033. io_req->offsets[0] = 0;
  1034. io_req->offsets[1] = ubd_dev->cow.data_offset;
  1035. io_req->buffer = page_address(page) + page_offset;
  1036. io_req->sectorsize = 1 << 9;
  1037. if(ubd_dev->cow.file != NULL)
  1038. cowify_req(io_req, ubd_dev->cow.bitmap,
  1039. ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len);
  1040. }
  1041. /* Called with dev->lock held */
  1042. static void do_ubd_request(struct request_queue *q)
  1043. {
  1044. struct io_thread_req *io_req;
  1045. struct request *req;
  1046. sector_t sector;
  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->start_sg = 0;
  1056. dev->end_sg = blk_rq_map_sg(q, req, dev->sg);
  1057. }
  1058. req = dev->request;
  1059. sector = blk_rq_pos(req);
  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)sector << 9,
  1071. sg->offset, sg->length, sg_page(sg));
  1072. sector += sg->length >> 9;
  1073. n = os_write_file(thread_fd, &io_req,
  1074. sizeof(struct io_thread_req *));
  1075. if(n != sizeof(struct io_thread_req *)){
  1076. if(n != -EAGAIN)
  1077. printk("write to io thread failed, "
  1078. "errno = %d\n", -n);
  1079. else if(list_empty(&dev->restart))
  1080. list_add(&dev->restart, &restart);
  1081. kfree(io_req);
  1082. return;
  1083. }
  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. }