ubd_kern.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444
  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/slab.h"
  32. #include "linux/vmalloc.h"
  33. #include "linux/smp_lock.h"
  34. #include "linux/blkpg.h"
  35. #include "linux/genhd.h"
  36. #include "linux/spinlock.h"
  37. #include "linux/platform_device.h"
  38. #include "linux/scatterlist.h"
  39. #include "asm/segment.h"
  40. #include "asm/uaccess.h"
  41. #include "asm/irq.h"
  42. #include "asm/types.h"
  43. #include "asm/tlbflush.h"
  44. #include "mem_user.h"
  45. #include "kern_util.h"
  46. #include "kern.h"
  47. #include "mconsole_kern.h"
  48. #include "init.h"
  49. #include "irq_user.h"
  50. #include "irq_kern.h"
  51. #include "ubd_user.h"
  52. #include "os.h"
  53. #include "mem.h"
  54. #include "mem_kern.h"
  55. #include "cow.h"
  56. enum ubd_req { UBD_READ, UBD_WRITE };
  57. struct io_thread_req {
  58. struct request *req;
  59. enum ubd_req op;
  60. int fds[2];
  61. unsigned long offsets[2];
  62. unsigned long long offset;
  63. unsigned long length;
  64. char *buffer;
  65. int sectorsize;
  66. unsigned long sector_mask;
  67. unsigned long long cow_offset;
  68. unsigned long bitmap_words[2];
  69. int error;
  70. };
  71. static inline int ubd_test_bit(__u64 bit, unsigned char *data)
  72. {
  73. __u64 n;
  74. int bits, off;
  75. bits = sizeof(data[0]) * 8;
  76. n = bit / bits;
  77. off = bit % bits;
  78. return (data[n] & (1 << off)) != 0;
  79. }
  80. static inline void ubd_set_bit(__u64 bit, unsigned char *data)
  81. {
  82. __u64 n;
  83. int bits, off;
  84. bits = sizeof(data[0]) * 8;
  85. n = bit / bits;
  86. off = bit % bits;
  87. data[n] |= (1 << off);
  88. }
  89. /*End stuff from ubd_user.h*/
  90. #define DRIVER_NAME "uml-blkdev"
  91. static DEFINE_MUTEX(ubd_lock);
  92. static int ubd_open(struct block_device *bdev, fmode_t mode);
  93. static int ubd_release(struct gendisk *disk, fmode_t mode);
  94. static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
  95. unsigned int cmd, unsigned long arg);
  96. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  97. #define MAX_DEV (16)
  98. static const struct block_device_operations ubd_blops = {
  99. .owner = THIS_MODULE,
  100. .open = ubd_open,
  101. .release = ubd_release,
  102. .ioctl = ubd_ioctl,
  103. .getgeo = ubd_getgeo,
  104. };
  105. /* Protected by ubd_lock */
  106. static int fake_major = UBD_MAJOR;
  107. static struct gendisk *ubd_gendisk[MAX_DEV];
  108. static struct gendisk *fake_gendisk[MAX_DEV];
  109. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  110. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  111. .cl = 1 })
  112. #else
  113. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  114. .cl = 1 })
  115. #endif
  116. static struct openflags global_openflags = OPEN_FLAGS;
  117. struct cow {
  118. /* backing file name */
  119. char *file;
  120. /* backing file fd */
  121. int fd;
  122. unsigned long *bitmap;
  123. unsigned long bitmap_len;
  124. int bitmap_offset;
  125. int data_offset;
  126. };
  127. #define MAX_SG 64
  128. struct ubd {
  129. struct list_head restart;
  130. /* name (and fd, below) of the file opened for writing, either the
  131. * backing or the cow file. */
  132. char *file;
  133. int count;
  134. int fd;
  135. __u64 size;
  136. struct openflags boot_openflags;
  137. struct openflags openflags;
  138. unsigned shared:1;
  139. unsigned no_cow:1;
  140. struct cow cow;
  141. struct platform_device pdev;
  142. struct request_queue *queue;
  143. spinlock_t lock;
  144. struct scatterlist sg[MAX_SG];
  145. struct request *request;
  146. int start_sg, end_sg;
  147. };
  148. #define DEFAULT_COW { \
  149. .file = NULL, \
  150. .fd = -1, \
  151. .bitmap = NULL, \
  152. .bitmap_offset = 0, \
  153. .data_offset = 0, \
  154. }
  155. #define DEFAULT_UBD { \
  156. .file = NULL, \
  157. .count = 0, \
  158. .fd = -1, \
  159. .size = -1, \
  160. .boot_openflags = OPEN_FLAGS, \
  161. .openflags = OPEN_FLAGS, \
  162. .no_cow = 0, \
  163. .shared = 0, \
  164. .cow = DEFAULT_COW, \
  165. .lock = SPIN_LOCK_UNLOCKED, \
  166. .request = NULL, \
  167. .start_sg = 0, \
  168. .end_sg = 0, \
  169. }
  170. /* Protected by ubd_lock */
  171. static struct ubd ubd_devs[MAX_DEV] = { [0 ... MAX_DEV - 1] = DEFAULT_UBD };
  172. /* Only changed by fake_ide_setup which is a setup */
  173. static int fake_ide = 0;
  174. static struct proc_dir_entry *proc_ide_root = NULL;
  175. static struct proc_dir_entry *proc_ide = NULL;
  176. static void make_proc_ide(void)
  177. {
  178. proc_ide_root = proc_mkdir("ide", NULL);
  179. proc_ide = proc_mkdir("ide0", proc_ide_root);
  180. }
  181. static int fake_ide_media_proc_show(struct seq_file *m, void *v)
  182. {
  183. seq_puts(m, "disk\n");
  184. return 0;
  185. }
  186. static int fake_ide_media_proc_open(struct inode *inode, struct file *file)
  187. {
  188. return single_open(file, fake_ide_media_proc_show, NULL);
  189. }
  190. static const struct file_operations fake_ide_media_proc_fops = {
  191. .owner = THIS_MODULE,
  192. .open = fake_ide_media_proc_open,
  193. .read = seq_read,
  194. .llseek = seq_lseek,
  195. .release = single_release,
  196. };
  197. static void make_ide_entries(const char *dev_name)
  198. {
  199. struct proc_dir_entry *dir, *ent;
  200. char name[64];
  201. if(proc_ide_root == NULL) make_proc_ide();
  202. dir = proc_mkdir(dev_name, proc_ide);
  203. if(!dir) return;
  204. ent = proc_create("media", S_IRUGO, dir, &fake_ide_media_proc_fops);
  205. if(!ent) return;
  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 != UBD_MAJOR) {
  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 LIST_HEAD(restart);
  398. /* XXX - move this inside ubd_intr. */
  399. /* Called without dev->lock held, and only in interrupt context. */
  400. static void ubd_handler(void)
  401. {
  402. struct io_thread_req *req;
  403. struct ubd *ubd;
  404. struct list_head *list, *next_ele;
  405. unsigned long flags;
  406. int n;
  407. while(1){
  408. n = os_read_file(thread_fd, &req,
  409. sizeof(struct io_thread_req *));
  410. if(n != sizeof(req)){
  411. if(n == -EAGAIN)
  412. break;
  413. printk(KERN_ERR "spurious interrupt in ubd_handler, "
  414. "err = %d\n", -n);
  415. return;
  416. }
  417. blk_end_request(req->req, 0, req->length);
  418. kfree(req);
  419. }
  420. reactivate_fd(thread_fd, UBD_IRQ);
  421. list_for_each_safe(list, next_ele, &restart){
  422. ubd = container_of(list, struct ubd, restart);
  423. list_del_init(&ubd->restart);
  424. spin_lock_irqsave(&ubd->lock, flags);
  425. do_ubd_request(ubd->queue);
  426. spin_unlock_irqrestore(&ubd->lock, flags);
  427. }
  428. }
  429. static irqreturn_t ubd_intr(int irq, void *dev)
  430. {
  431. ubd_handler();
  432. return IRQ_HANDLED;
  433. }
  434. /* Only changed by ubd_init, which is an initcall. */
  435. static int io_pid = -1;
  436. static void kill_io_thread(void)
  437. {
  438. if(io_pid != -1)
  439. os_kill_process(io_pid, 1);
  440. }
  441. __uml_exitcall(kill_io_thread);
  442. static inline int ubd_file_size(struct ubd *ubd_dev, __u64 *size_out)
  443. {
  444. char *file;
  445. file = ubd_dev->cow.file ? ubd_dev->cow.file : ubd_dev->file;
  446. return os_file_size(file, size_out);
  447. }
  448. static int read_cow_bitmap(int fd, void *buf, int offset, int len)
  449. {
  450. int err;
  451. err = os_seek_file(fd, offset);
  452. if (err < 0)
  453. return err;
  454. err = os_read_file(fd, buf, len);
  455. if (err < 0)
  456. return err;
  457. return 0;
  458. }
  459. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  460. {
  461. unsigned long modtime;
  462. unsigned long long actual;
  463. int err;
  464. err = os_file_modtime(file, &modtime);
  465. if (err < 0) {
  466. printk(KERN_ERR "Failed to get modification time of backing "
  467. "file \"%s\", err = %d\n", file, -err);
  468. return err;
  469. }
  470. err = os_file_size(file, &actual);
  471. if (err < 0) {
  472. printk(KERN_ERR "Failed to get size of backing file \"%s\", "
  473. "err = %d\n", file, -err);
  474. return err;
  475. }
  476. if (actual != size) {
  477. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  478. * the typecast.*/
  479. printk(KERN_ERR "Size mismatch (%llu vs %llu) of COW header "
  480. "vs backing file\n", (unsigned long long) size, actual);
  481. return -EINVAL;
  482. }
  483. if (modtime != mtime) {
  484. printk(KERN_ERR "mtime mismatch (%ld vs %ld) of COW header vs "
  485. "backing file\n", mtime, modtime);
  486. return -EINVAL;
  487. }
  488. return 0;
  489. }
  490. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  491. {
  492. struct uml_stat buf1, buf2;
  493. int err;
  494. if (from_cmdline == NULL)
  495. return 0;
  496. if (!strcmp(from_cmdline, from_cow))
  497. return 0;
  498. err = os_stat_file(from_cmdline, &buf1);
  499. if (err < 0) {
  500. printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cmdline,
  501. -err);
  502. return 0;
  503. }
  504. err = os_stat_file(from_cow, &buf2);
  505. if (err < 0) {
  506. printk(KERN_ERR "Couldn't stat '%s', err = %d\n", from_cow,
  507. -err);
  508. return 1;
  509. }
  510. if ((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  511. return 0;
  512. printk(KERN_ERR "Backing file mismatch - \"%s\" requested, "
  513. "\"%s\" specified in COW header of \"%s\"\n",
  514. from_cmdline, from_cow, cow);
  515. return 1;
  516. }
  517. static int open_ubd_file(char *file, struct openflags *openflags, int shared,
  518. char **backing_file_out, int *bitmap_offset_out,
  519. unsigned long *bitmap_len_out, int *data_offset_out,
  520. int *create_cow_out)
  521. {
  522. time_t mtime;
  523. unsigned long long size;
  524. __u32 version, align;
  525. char *backing_file;
  526. int fd, err, sectorsize, asked_switch, mode = 0644;
  527. fd = os_open_file(file, *openflags, mode);
  528. if (fd < 0) {
  529. if ((fd == -ENOENT) && (create_cow_out != NULL))
  530. *create_cow_out = 1;
  531. if (!openflags->w ||
  532. ((fd != -EROFS) && (fd != -EACCES)))
  533. return fd;
  534. openflags->w = 0;
  535. fd = os_open_file(file, *openflags, mode);
  536. if (fd < 0)
  537. return fd;
  538. }
  539. if (shared)
  540. printk(KERN_INFO "Not locking \"%s\" on the host\n", file);
  541. else {
  542. err = os_lock_file(fd, openflags->w);
  543. if (err < 0) {
  544. printk(KERN_ERR "Failed to lock '%s', err = %d\n",
  545. file, -err);
  546. goto out_close;
  547. }
  548. }
  549. /* Successful return case! */
  550. if (backing_file_out == NULL)
  551. return fd;
  552. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  553. &size, &sectorsize, &align, bitmap_offset_out);
  554. if (err && (*backing_file_out != NULL)) {
  555. printk(KERN_ERR "Failed to read COW header from COW file "
  556. "\"%s\", errno = %d\n", file, -err);
  557. goto out_close;
  558. }
  559. if (err)
  560. return fd;
  561. asked_switch = path_requires_switch(*backing_file_out, backing_file,
  562. file);
  563. /* Allow switching only if no mismatch. */
  564. if (asked_switch && !backing_file_mismatch(*backing_file_out, size,
  565. mtime)) {
  566. printk(KERN_ERR "Switching backing file to '%s'\n",
  567. *backing_file_out);
  568. err = write_cow_header(file, fd, *backing_file_out,
  569. sectorsize, align, &size);
  570. if (err) {
  571. printk(KERN_ERR "Switch failed, errno = %d\n", -err);
  572. goto out_close;
  573. }
  574. } else {
  575. *backing_file_out = backing_file;
  576. err = backing_file_mismatch(*backing_file_out, size, mtime);
  577. if (err)
  578. goto out_close;
  579. }
  580. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  581. bitmap_len_out, data_offset_out);
  582. return fd;
  583. out_close:
  584. os_close_file(fd);
  585. return err;
  586. }
  587. static int create_cow_file(char *cow_file, char *backing_file,
  588. struct openflags flags,
  589. int sectorsize, int alignment, int *bitmap_offset_out,
  590. unsigned long *bitmap_len_out, int *data_offset_out)
  591. {
  592. int err, fd;
  593. flags.c = 1;
  594. fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
  595. if (fd < 0) {
  596. err = fd;
  597. printk(KERN_ERR "Open of COW file '%s' failed, errno = %d\n",
  598. cow_file, -err);
  599. goto out;
  600. }
  601. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  602. bitmap_offset_out, bitmap_len_out,
  603. data_offset_out);
  604. if (!err)
  605. return fd;
  606. os_close_file(fd);
  607. out:
  608. return err;
  609. }
  610. static void ubd_close_dev(struct ubd *ubd_dev)
  611. {
  612. os_close_file(ubd_dev->fd);
  613. if(ubd_dev->cow.file == NULL)
  614. return;
  615. os_close_file(ubd_dev->cow.fd);
  616. vfree(ubd_dev->cow.bitmap);
  617. ubd_dev->cow.bitmap = NULL;
  618. }
  619. static int ubd_open_dev(struct ubd *ubd_dev)
  620. {
  621. struct openflags flags;
  622. char **back_ptr;
  623. int err, create_cow, *create_ptr;
  624. int fd;
  625. ubd_dev->openflags = ubd_dev->boot_openflags;
  626. create_cow = 0;
  627. create_ptr = (ubd_dev->cow.file != NULL) ? &create_cow : NULL;
  628. back_ptr = ubd_dev->no_cow ? NULL : &ubd_dev->cow.file;
  629. fd = open_ubd_file(ubd_dev->file, &ubd_dev->openflags, ubd_dev->shared,
  630. back_ptr, &ubd_dev->cow.bitmap_offset,
  631. &ubd_dev->cow.bitmap_len, &ubd_dev->cow.data_offset,
  632. create_ptr);
  633. if((fd == -ENOENT) && create_cow){
  634. fd = create_cow_file(ubd_dev->file, ubd_dev->cow.file,
  635. ubd_dev->openflags, 1 << 9, PAGE_SIZE,
  636. &ubd_dev->cow.bitmap_offset,
  637. &ubd_dev->cow.bitmap_len,
  638. &ubd_dev->cow.data_offset);
  639. if(fd >= 0){
  640. printk(KERN_INFO "Creating \"%s\" as COW file for "
  641. "\"%s\"\n", ubd_dev->file, ubd_dev->cow.file);
  642. }
  643. }
  644. if(fd < 0){
  645. printk("Failed to open '%s', errno = %d\n", ubd_dev->file,
  646. -fd);
  647. return fd;
  648. }
  649. ubd_dev->fd = fd;
  650. if(ubd_dev->cow.file != NULL){
  651. blk_queue_max_hw_sectors(ubd_dev->queue, 8 * sizeof(long));
  652. err = -ENOMEM;
  653. ubd_dev->cow.bitmap = vmalloc(ubd_dev->cow.bitmap_len);
  654. if(ubd_dev->cow.bitmap == NULL){
  655. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  656. goto error;
  657. }
  658. flush_tlb_kernel_vm();
  659. err = read_cow_bitmap(ubd_dev->fd, ubd_dev->cow.bitmap,
  660. ubd_dev->cow.bitmap_offset,
  661. ubd_dev->cow.bitmap_len);
  662. if(err < 0)
  663. goto error;
  664. flags = ubd_dev->openflags;
  665. flags.w = 0;
  666. err = open_ubd_file(ubd_dev->cow.file, &flags, ubd_dev->shared, NULL,
  667. NULL, NULL, NULL, NULL);
  668. if(err < 0) goto error;
  669. ubd_dev->cow.fd = err;
  670. }
  671. return 0;
  672. error:
  673. os_close_file(ubd_dev->fd);
  674. return err;
  675. }
  676. static void ubd_device_release(struct device *dev)
  677. {
  678. struct ubd *ubd_dev = dev_get_drvdata(dev);
  679. blk_cleanup_queue(ubd_dev->queue);
  680. *ubd_dev = ((struct ubd) DEFAULT_UBD);
  681. }
  682. static int ubd_disk_register(int major, u64 size, int unit,
  683. struct gendisk **disk_out)
  684. {
  685. struct gendisk *disk;
  686. disk = alloc_disk(1 << UBD_SHIFT);
  687. if(disk == NULL)
  688. return -ENOMEM;
  689. disk->major = major;
  690. disk->first_minor = unit << UBD_SHIFT;
  691. disk->fops = &ubd_blops;
  692. set_capacity(disk, size / 512);
  693. if (major == UBD_MAJOR)
  694. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  695. else
  696. sprintf(disk->disk_name, "ubd_fake%d", unit);
  697. /* sysfs register (not for ide fake devices) */
  698. if (major == UBD_MAJOR) {
  699. ubd_devs[unit].pdev.id = unit;
  700. ubd_devs[unit].pdev.name = DRIVER_NAME;
  701. ubd_devs[unit].pdev.dev.release = ubd_device_release;
  702. dev_set_drvdata(&ubd_devs[unit].pdev.dev, &ubd_devs[unit]);
  703. platform_device_register(&ubd_devs[unit].pdev);
  704. disk->driverfs_dev = &ubd_devs[unit].pdev.dev;
  705. }
  706. disk->private_data = &ubd_devs[unit];
  707. disk->queue = ubd_devs[unit].queue;
  708. add_disk(disk);
  709. *disk_out = disk;
  710. return 0;
  711. }
  712. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  713. static int ubd_add(int n, char **error_out)
  714. {
  715. struct ubd *ubd_dev = &ubd_devs[n];
  716. int err = 0;
  717. if(ubd_dev->file == NULL)
  718. goto out;
  719. err = ubd_file_size(ubd_dev, &ubd_dev->size);
  720. if(err < 0){
  721. *error_out = "Couldn't determine size of device's file";
  722. goto out;
  723. }
  724. ubd_dev->size = ROUND_BLOCK(ubd_dev->size);
  725. INIT_LIST_HEAD(&ubd_dev->restart);
  726. sg_init_table(ubd_dev->sg, MAX_SG);
  727. err = -ENOMEM;
  728. ubd_dev->queue = blk_init_queue(do_ubd_request, &ubd_dev->lock);
  729. if (ubd_dev->queue == NULL) {
  730. *error_out = "Failed to initialize device queue";
  731. goto out;
  732. }
  733. ubd_dev->queue->queuedata = ubd_dev;
  734. blk_queue_max_segments(ubd_dev->queue, MAX_SG);
  735. err = ubd_disk_register(UBD_MAJOR, ubd_dev->size, n, &ubd_gendisk[n]);
  736. if(err){
  737. *error_out = "Failed to register device";
  738. goto out_cleanup;
  739. }
  740. if (fake_major != UBD_MAJOR)
  741. ubd_disk_register(fake_major, ubd_dev->size, n,
  742. &fake_gendisk[n]);
  743. /*
  744. * Perhaps this should also be under the "if (fake_major)" above
  745. * using the fake_disk->disk_name
  746. */
  747. if (fake_ide)
  748. make_ide_entries(ubd_gendisk[n]->disk_name);
  749. err = 0;
  750. out:
  751. return err;
  752. out_cleanup:
  753. blk_cleanup_queue(ubd_dev->queue);
  754. goto out;
  755. }
  756. static int ubd_config(char *str, char **error_out)
  757. {
  758. int n, ret;
  759. /* This string is possibly broken up and stored, so it's only
  760. * freed if ubd_setup_common fails, or if only general options
  761. * were set.
  762. */
  763. str = kstrdup(str, GFP_KERNEL);
  764. if (str == NULL) {
  765. *error_out = "Failed to allocate memory";
  766. return -ENOMEM;
  767. }
  768. ret = ubd_setup_common(str, &n, error_out);
  769. if (ret)
  770. goto err_free;
  771. if (n == -1) {
  772. ret = 0;
  773. goto err_free;
  774. }
  775. mutex_lock(&ubd_lock);
  776. ret = ubd_add(n, error_out);
  777. if (ret)
  778. ubd_devs[n].file = NULL;
  779. mutex_unlock(&ubd_lock);
  780. out:
  781. return ret;
  782. err_free:
  783. kfree(str);
  784. goto out;
  785. }
  786. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  787. {
  788. struct ubd *ubd_dev;
  789. int n, len = 0;
  790. n = parse_unit(&name);
  791. if((n >= MAX_DEV) || (n < 0)){
  792. *error_out = "ubd_get_config : device number out of range";
  793. return -1;
  794. }
  795. ubd_dev = &ubd_devs[n];
  796. mutex_lock(&ubd_lock);
  797. if(ubd_dev->file == NULL){
  798. CONFIG_CHUNK(str, size, len, "", 1);
  799. goto out;
  800. }
  801. CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
  802. if(ubd_dev->cow.file != NULL){
  803. CONFIG_CHUNK(str, size, len, ",", 0);
  804. CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
  805. }
  806. else CONFIG_CHUNK(str, size, len, "", 1);
  807. out:
  808. mutex_unlock(&ubd_lock);
  809. return len;
  810. }
  811. static int ubd_id(char **str, int *start_out, int *end_out)
  812. {
  813. int n;
  814. n = parse_unit(str);
  815. *start_out = 0;
  816. *end_out = MAX_DEV - 1;
  817. return n;
  818. }
  819. static int ubd_remove(int n, char **error_out)
  820. {
  821. struct gendisk *disk = ubd_gendisk[n];
  822. struct ubd *ubd_dev;
  823. int err = -ENODEV;
  824. mutex_lock(&ubd_lock);
  825. ubd_dev = &ubd_devs[n];
  826. if(ubd_dev->file == NULL)
  827. goto out;
  828. /* you cannot remove a open disk */
  829. err = -EBUSY;
  830. if(ubd_dev->count > 0)
  831. goto out;
  832. ubd_gendisk[n] = NULL;
  833. if(disk != NULL){
  834. del_gendisk(disk);
  835. put_disk(disk);
  836. }
  837. if(fake_gendisk[n] != NULL){
  838. del_gendisk(fake_gendisk[n]);
  839. put_disk(fake_gendisk[n]);
  840. fake_gendisk[n] = NULL;
  841. }
  842. err = 0;
  843. platform_device_unregister(&ubd_dev->pdev);
  844. out:
  845. mutex_unlock(&ubd_lock);
  846. return err;
  847. }
  848. /* All these are called by mconsole in process context and without
  849. * ubd-specific locks. The structure itself is const except for .list.
  850. */
  851. static struct mc_device ubd_mc = {
  852. .list = LIST_HEAD_INIT(ubd_mc.list),
  853. .name = "ubd",
  854. .config = ubd_config,
  855. .get_config = ubd_get_config,
  856. .id = ubd_id,
  857. .remove = ubd_remove,
  858. };
  859. static int __init ubd_mc_init(void)
  860. {
  861. mconsole_register_dev(&ubd_mc);
  862. return 0;
  863. }
  864. __initcall(ubd_mc_init);
  865. static int __init ubd0_init(void)
  866. {
  867. struct ubd *ubd_dev = &ubd_devs[0];
  868. mutex_lock(&ubd_lock);
  869. if(ubd_dev->file == NULL)
  870. ubd_dev->file = "root_fs";
  871. mutex_unlock(&ubd_lock);
  872. return 0;
  873. }
  874. __initcall(ubd0_init);
  875. /* Used in ubd_init, which is an initcall */
  876. static struct platform_driver ubd_driver = {
  877. .driver = {
  878. .name = DRIVER_NAME,
  879. },
  880. };
  881. static int __init ubd_init(void)
  882. {
  883. char *error;
  884. int i, err;
  885. if (register_blkdev(UBD_MAJOR, "ubd"))
  886. return -1;
  887. if (fake_major != UBD_MAJOR) {
  888. char name[sizeof("ubd_nnn\0")];
  889. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  890. if (register_blkdev(fake_major, "ubd"))
  891. return -1;
  892. }
  893. platform_driver_register(&ubd_driver);
  894. mutex_lock(&ubd_lock);
  895. for (i = 0; i < MAX_DEV; i++){
  896. err = ubd_add(i, &error);
  897. if(err)
  898. printk(KERN_ERR "Failed to initialize ubd device %d :"
  899. "%s\n", i, error);
  900. }
  901. mutex_unlock(&ubd_lock);
  902. return 0;
  903. }
  904. late_initcall(ubd_init);
  905. static int __init ubd_driver_init(void){
  906. unsigned long stack;
  907. int err;
  908. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  909. if(global_openflags.s){
  910. printk(KERN_INFO "ubd: Synchronous mode\n");
  911. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  912. * enough. So use anyway the io thread. */
  913. }
  914. stack = alloc_stack(0, 0);
  915. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  916. &thread_fd);
  917. if(io_pid < 0){
  918. printk(KERN_ERR
  919. "ubd : Failed to start I/O thread (errno = %d) - "
  920. "falling back to synchronous I/O\n", -io_pid);
  921. io_pid = -1;
  922. return 0;
  923. }
  924. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  925. IRQF_DISABLED, "ubd", ubd_devs);
  926. if(err != 0)
  927. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  928. return 0;
  929. }
  930. device_initcall(ubd_driver_init);
  931. static int ubd_open(struct block_device *bdev, fmode_t mode)
  932. {
  933. struct gendisk *disk = bdev->bd_disk;
  934. struct ubd *ubd_dev = disk->private_data;
  935. int err = 0;
  936. lock_kernel();
  937. if(ubd_dev->count == 0){
  938. err = ubd_open_dev(ubd_dev);
  939. if(err){
  940. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  941. disk->disk_name, ubd_dev->file, -err);
  942. goto out;
  943. }
  944. }
  945. ubd_dev->count++;
  946. set_disk_ro(disk, !ubd_dev->openflags.w);
  947. /* This should no more be needed. And it didn't work anyway to exclude
  948. * read-write remounting of filesystems.*/
  949. /*if((mode & FMODE_WRITE) && !ubd_dev->openflags.w){
  950. if(--ubd_dev->count == 0) ubd_close_dev(ubd_dev);
  951. err = -EROFS;
  952. }*/
  953. out:
  954. unlock_kernel();
  955. return err;
  956. }
  957. static int ubd_release(struct gendisk *disk, fmode_t mode)
  958. {
  959. struct ubd *ubd_dev = disk->private_data;
  960. lock_kernel();
  961. if(--ubd_dev->count == 0)
  962. ubd_close_dev(ubd_dev);
  963. unlock_kernel();
  964. return 0;
  965. }
  966. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  967. __u64 *cow_offset, unsigned long *bitmap,
  968. __u64 bitmap_offset, unsigned long *bitmap_words,
  969. __u64 bitmap_len)
  970. {
  971. __u64 sector = io_offset >> 9;
  972. int i, update_bitmap = 0;
  973. for(i = 0; i < length >> 9; i++){
  974. if(cow_mask != NULL)
  975. ubd_set_bit(i, (unsigned char *) cow_mask);
  976. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  977. continue;
  978. update_bitmap = 1;
  979. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  980. }
  981. if(!update_bitmap)
  982. return;
  983. *cow_offset = sector / (sizeof(unsigned long) * 8);
  984. /* This takes care of the case where we're exactly at the end of the
  985. * device, and *cow_offset + 1 is off the end. So, just back it up
  986. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  987. * for the original diagnosis.
  988. */
  989. if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
  990. sizeof(unsigned long)) - 1))
  991. (*cow_offset)--;
  992. bitmap_words[0] = bitmap[*cow_offset];
  993. bitmap_words[1] = bitmap[*cow_offset + 1];
  994. *cow_offset *= sizeof(unsigned long);
  995. *cow_offset += bitmap_offset;
  996. }
  997. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  998. __u64 bitmap_offset, __u64 bitmap_len)
  999. {
  1000. __u64 sector = req->offset >> 9;
  1001. int i;
  1002. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  1003. panic("Operation too long");
  1004. if(req->op == UBD_READ) {
  1005. for(i = 0; i < req->length >> 9; i++){
  1006. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  1007. ubd_set_bit(i, (unsigned char *)
  1008. &req->sector_mask);
  1009. }
  1010. }
  1011. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  1012. &req->cow_offset, bitmap, bitmap_offset,
  1013. req->bitmap_words, bitmap_len);
  1014. }
  1015. /* Called with dev->lock held */
  1016. static void prepare_request(struct request *req, struct io_thread_req *io_req,
  1017. unsigned long long offset, int page_offset,
  1018. int len, struct page *page)
  1019. {
  1020. struct gendisk *disk = req->rq_disk;
  1021. struct ubd *ubd_dev = disk->private_data;
  1022. io_req->req = req;
  1023. io_req->fds[0] = (ubd_dev->cow.file != NULL) ? ubd_dev->cow.fd :
  1024. ubd_dev->fd;
  1025. io_req->fds[1] = ubd_dev->fd;
  1026. io_req->cow_offset = -1;
  1027. io_req->offset = offset;
  1028. io_req->length = len;
  1029. io_req->error = 0;
  1030. io_req->sector_mask = 0;
  1031. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  1032. io_req->offsets[0] = 0;
  1033. io_req->offsets[1] = ubd_dev->cow.data_offset;
  1034. io_req->buffer = page_address(page) + page_offset;
  1035. io_req->sectorsize = 1 << 9;
  1036. if(ubd_dev->cow.file != NULL)
  1037. cowify_req(io_req, ubd_dev->cow.bitmap,
  1038. ubd_dev->cow.bitmap_offset, ubd_dev->cow.bitmap_len);
  1039. }
  1040. /* Called with dev->lock held */
  1041. static void do_ubd_request(struct request_queue *q)
  1042. {
  1043. struct io_thread_req *io_req;
  1044. struct request *req;
  1045. sector_t sector;
  1046. int n;
  1047. while(1){
  1048. struct ubd *dev = q->queuedata;
  1049. if(dev->end_sg == 0){
  1050. struct request *req = blk_fetch_request(q);
  1051. if(req == NULL)
  1052. return;
  1053. dev->request = req;
  1054. dev->start_sg = 0;
  1055. dev->end_sg = blk_rq_map_sg(q, req, dev->sg);
  1056. }
  1057. req = dev->request;
  1058. sector = blk_rq_pos(req);
  1059. while(dev->start_sg < dev->end_sg){
  1060. struct scatterlist *sg = &dev->sg[dev->start_sg];
  1061. io_req = kmalloc(sizeof(struct io_thread_req),
  1062. GFP_ATOMIC);
  1063. if(io_req == NULL){
  1064. if(list_empty(&dev->restart))
  1065. list_add(&dev->restart, &restart);
  1066. return;
  1067. }
  1068. prepare_request(req, io_req,
  1069. (unsigned long long)sector << 9,
  1070. sg->offset, sg->length, sg_page(sg));
  1071. sector += sg->length >> 9;
  1072. n = os_write_file(thread_fd, &io_req,
  1073. sizeof(struct io_thread_req *));
  1074. if(n != sizeof(struct io_thread_req *)){
  1075. if(n != -EAGAIN)
  1076. printk("write to io thread failed, "
  1077. "errno = %d\n", -n);
  1078. else if(list_empty(&dev->restart))
  1079. list_add(&dev->restart, &restart);
  1080. kfree(io_req);
  1081. return;
  1082. }
  1083. dev->start_sg++;
  1084. }
  1085. dev->end_sg = 0;
  1086. dev->request = NULL;
  1087. }
  1088. }
  1089. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1090. {
  1091. struct ubd *ubd_dev = bdev->bd_disk->private_data;
  1092. geo->heads = 128;
  1093. geo->sectors = 32;
  1094. geo->cylinders = ubd_dev->size / (128 * 32 * 512);
  1095. return 0;
  1096. }
  1097. static int ubd_ioctl(struct block_device *bdev, fmode_t mode,
  1098. unsigned int cmd, unsigned long arg)
  1099. {
  1100. struct ubd *ubd_dev = bdev->bd_disk->private_data;
  1101. u16 ubd_id[ATA_ID_WORDS];
  1102. switch (cmd) {
  1103. struct cdrom_volctrl volume;
  1104. case HDIO_GET_IDENTITY:
  1105. memset(&ubd_id, 0, ATA_ID_WORDS * 2);
  1106. ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
  1107. ubd_id[ATA_ID_HEADS] = 128;
  1108. ubd_id[ATA_ID_SECTORS] = 32;
  1109. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  1110. sizeof(ubd_id)))
  1111. return -EFAULT;
  1112. return 0;
  1113. case CDROMVOLREAD:
  1114. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  1115. return -EFAULT;
  1116. volume.channel0 = 255;
  1117. volume.channel1 = 255;
  1118. volume.channel2 = 255;
  1119. volume.channel3 = 255;
  1120. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  1121. return -EFAULT;
  1122. return 0;
  1123. }
  1124. return -EINVAL;
  1125. }
  1126. static int update_bitmap(struct io_thread_req *req)
  1127. {
  1128. int n;
  1129. if(req->cow_offset == -1)
  1130. return 0;
  1131. n = os_seek_file(req->fds[1], req->cow_offset);
  1132. if(n < 0){
  1133. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1134. return 1;
  1135. }
  1136. n = os_write_file(req->fds[1], &req->bitmap_words,
  1137. sizeof(req->bitmap_words));
  1138. if(n != sizeof(req->bitmap_words)){
  1139. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1140. req->fds[1]);
  1141. return 1;
  1142. }
  1143. return 0;
  1144. }
  1145. static void do_io(struct io_thread_req *req)
  1146. {
  1147. char *buf;
  1148. unsigned long len;
  1149. int n, nsectors, start, end, bit;
  1150. int err;
  1151. __u64 off;
  1152. nsectors = req->length / req->sectorsize;
  1153. start = 0;
  1154. do {
  1155. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1156. end = start;
  1157. while((end < nsectors) &&
  1158. (ubd_test_bit(end, (unsigned char *)
  1159. &req->sector_mask) == bit))
  1160. end++;
  1161. off = req->offset + req->offsets[bit] +
  1162. start * req->sectorsize;
  1163. len = (end - start) * req->sectorsize;
  1164. buf = &req->buffer[start * req->sectorsize];
  1165. err = os_seek_file(req->fds[bit], off);
  1166. if(err < 0){
  1167. printk("do_io - lseek failed : err = %d\n", -err);
  1168. req->error = 1;
  1169. return;
  1170. }
  1171. if(req->op == UBD_READ){
  1172. n = 0;
  1173. do {
  1174. buf = &buf[n];
  1175. len -= n;
  1176. n = os_read_file(req->fds[bit], buf, len);
  1177. if (n < 0) {
  1178. printk("do_io - read failed, err = %d "
  1179. "fd = %d\n", -n, req->fds[bit]);
  1180. req->error = 1;
  1181. return;
  1182. }
  1183. } while((n < len) && (n != 0));
  1184. if (n < len) memset(&buf[n], 0, len - n);
  1185. } else {
  1186. n = os_write_file(req->fds[bit], buf, len);
  1187. if(n != len){
  1188. printk("do_io - write failed err = %d "
  1189. "fd = %d\n", -n, req->fds[bit]);
  1190. req->error = 1;
  1191. return;
  1192. }
  1193. }
  1194. start = end;
  1195. } while(start < nsectors);
  1196. req->error = update_bitmap(req);
  1197. }
  1198. /* Changed in start_io_thread, which is serialized by being called only
  1199. * from ubd_init, which is an initcall.
  1200. */
  1201. int kernel_fd = -1;
  1202. /* Only changed by the io thread. XXX: currently unused. */
  1203. static int io_count = 0;
  1204. int io_thread(void *arg)
  1205. {
  1206. struct io_thread_req *req;
  1207. int n;
  1208. ignore_sigwinch_sig();
  1209. while(1){
  1210. n = os_read_file(kernel_fd, &req,
  1211. sizeof(struct io_thread_req *));
  1212. if(n != sizeof(struct io_thread_req *)){
  1213. if(n < 0)
  1214. printk("io_thread - read failed, fd = %d, "
  1215. "err = %d\n", kernel_fd, -n);
  1216. else {
  1217. printk("io_thread - short read, fd = %d, "
  1218. "length = %d\n", kernel_fd, n);
  1219. }
  1220. continue;
  1221. }
  1222. io_count++;
  1223. do_io(req);
  1224. n = os_write_file(kernel_fd, &req,
  1225. sizeof(struct io_thread_req *));
  1226. if(n != sizeof(struct io_thread_req *))
  1227. printk("io_thread - write failed, fd = %d, err = %d\n",
  1228. kernel_fd, -n);
  1229. }
  1230. return 0;
  1231. }