ubd_kern.c 33 KB

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