ubd_kern.c 34 KB

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