ubd_kern.c 33 KB

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