ubd_kern.c 37 KB

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