ubd_kern.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461
  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 "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 = MAJOR_NR;
  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 != MAJOR_NR){
  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 == MAJOR_NR)
  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 == MAJOR_NR) {
  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(MAJOR_NR, 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 != MAJOR_NR)
  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(MAJOR_NR, "ubd"))
  903. return -1;
  904. if (fake_major != MAJOR_NR) {
  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. struct hd_driveid ubd_id = {
  1116. .cyls = 0,
  1117. .heads = 128,
  1118. .sectors = 32,
  1119. };
  1120. switch (cmd) {
  1121. struct cdrom_volctrl volume;
  1122. case HDIO_GET_IDENTITY:
  1123. ubd_id.cyls = ubd_dev->size / (128 * 32 * 512);
  1124. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  1125. sizeof(ubd_id)))
  1126. return -EFAULT;
  1127. return 0;
  1128. case CDROMVOLREAD:
  1129. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  1130. return -EFAULT;
  1131. volume.channel0 = 255;
  1132. volume.channel1 = 255;
  1133. volume.channel2 = 255;
  1134. volume.channel3 = 255;
  1135. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  1136. return -EFAULT;
  1137. return 0;
  1138. }
  1139. return -EINVAL;
  1140. }
  1141. static int update_bitmap(struct io_thread_req *req)
  1142. {
  1143. int n;
  1144. if(req->cow_offset == -1)
  1145. return 0;
  1146. n = os_seek_file(req->fds[1], req->cow_offset);
  1147. if(n < 0){
  1148. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1149. return 1;
  1150. }
  1151. n = os_write_file(req->fds[1], &req->bitmap_words,
  1152. sizeof(req->bitmap_words));
  1153. if(n != sizeof(req->bitmap_words)){
  1154. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1155. req->fds[1]);
  1156. return 1;
  1157. }
  1158. return 0;
  1159. }
  1160. static void do_io(struct io_thread_req *req)
  1161. {
  1162. char *buf;
  1163. unsigned long len;
  1164. int n, nsectors, start, end, bit;
  1165. int err;
  1166. __u64 off;
  1167. nsectors = req->length / req->sectorsize;
  1168. start = 0;
  1169. do {
  1170. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1171. end = start;
  1172. while((end < nsectors) &&
  1173. (ubd_test_bit(end, (unsigned char *)
  1174. &req->sector_mask) == bit))
  1175. end++;
  1176. off = req->offset + req->offsets[bit] +
  1177. start * req->sectorsize;
  1178. len = (end - start) * req->sectorsize;
  1179. buf = &req->buffer[start * req->sectorsize];
  1180. err = os_seek_file(req->fds[bit], off);
  1181. if(err < 0){
  1182. printk("do_io - lseek failed : err = %d\n", -err);
  1183. req->error = 1;
  1184. return;
  1185. }
  1186. if(req->op == UBD_READ){
  1187. n = 0;
  1188. do {
  1189. buf = &buf[n];
  1190. len -= n;
  1191. n = os_read_file(req->fds[bit], buf, len);
  1192. if (n < 0) {
  1193. printk("do_io - read failed, err = %d "
  1194. "fd = %d\n", -n, req->fds[bit]);
  1195. req->error = 1;
  1196. return;
  1197. }
  1198. } while((n < len) && (n != 0));
  1199. if (n < len) memset(&buf[n], 0, len - n);
  1200. } else {
  1201. n = os_write_file(req->fds[bit], buf, len);
  1202. if(n != len){
  1203. printk("do_io - write failed err = %d "
  1204. "fd = %d\n", -n, req->fds[bit]);
  1205. req->error = 1;
  1206. return;
  1207. }
  1208. }
  1209. start = end;
  1210. } while(start < nsectors);
  1211. req->error = update_bitmap(req);
  1212. }
  1213. /* Changed in start_io_thread, which is serialized by being called only
  1214. * from ubd_init, which is an initcall.
  1215. */
  1216. int kernel_fd = -1;
  1217. /* Only changed by the io thread. XXX: currently unused. */
  1218. static int io_count = 0;
  1219. int io_thread(void *arg)
  1220. {
  1221. struct io_thread_req *req;
  1222. int n;
  1223. ignore_sigwinch_sig();
  1224. while(1){
  1225. n = os_read_file(kernel_fd, &req,
  1226. sizeof(struct io_thread_req *));
  1227. if(n != sizeof(struct io_thread_req *)){
  1228. if(n < 0)
  1229. printk("io_thread - read failed, fd = %d, "
  1230. "err = %d\n", kernel_fd, -n);
  1231. else {
  1232. printk("io_thread - short read, fd = %d, "
  1233. "length = %d\n", kernel_fd, n);
  1234. }
  1235. continue;
  1236. }
  1237. io_count++;
  1238. do_io(req);
  1239. n = os_write_file(kernel_fd, &req,
  1240. sizeof(struct io_thread_req *));
  1241. if(n != sizeof(struct io_thread_req *))
  1242. printk("io_thread - write failed, fd = %d, err = %d\n",
  1243. kernel_fd, -n);
  1244. }
  1245. return 0;
  1246. }