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