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