ubd_kern.c 34 KB

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