/arch/um/drivers/ubd_kern.c

http://github.com/mirrors/linux · C · 1623 lines · 1286 code · 243 blank · 94 comment · 238 complexity · 7786ea95ed0e4c47b438f791f0b26877 MD5 · raw file

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