ubd_kern.c 34 KB

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