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