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