ubd_kern.c 35 KB

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