ubd_kern.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  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 block_device *bdev, 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"
  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 int io_pid = -1;
  416. static void kill_io_thread(void)
  417. {
  418. if(io_pid != -1)
  419. os_kill_process(io_pid, 1);
  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.flags = BLK_MQ_F_SHOULD_MERGE;
  745. ubd_dev->tag_set.driver_data = ubd_dev;
  746. ubd_dev->tag_set.nr_hw_queues = 1;
  747. err = blk_mq_alloc_tag_set(&ubd_dev->tag_set);
  748. if (err)
  749. goto out_close;
  750. disk = blk_mq_alloc_disk(&ubd_dev->tag_set, &lim, ubd_dev);
  751. if (IS_ERR(disk)) {
  752. err = PTR_ERR(disk);
  753. goto out_cleanup_tags;
  754. }
  755. disk->major = UBD_MAJOR;
  756. disk->first_minor = n << UBD_SHIFT;
  757. disk->minors = 1 << UBD_SHIFT;
  758. disk->fops = &ubd_blops;
  759. set_capacity(disk, ubd_dev->size / 512);
  760. sprintf(disk->disk_name, "ubd%c", 'a' + n);
  761. disk->private_data = ubd_dev;
  762. set_disk_ro(disk, !ubd_dev->openflags.w);
  763. ubd_dev->pdev.id = n;
  764. ubd_dev->pdev.name = DRIVER_NAME;
  765. ubd_dev->pdev.dev.release = ubd_device_release;
  766. dev_set_drvdata(&ubd_dev->pdev.dev, ubd_dev);
  767. platform_device_register(&ubd_dev->pdev);
  768. err = device_add_disk(&ubd_dev->pdev.dev, disk, ubd_attr_groups);
  769. if (err)
  770. goto out_cleanup_disk;
  771. ubd_dev->disk = disk;
  772. return 0;
  773. out_cleanup_disk:
  774. put_disk(disk);
  775. out_cleanup_tags:
  776. blk_mq_free_tag_set(&ubd_dev->tag_set);
  777. out_close:
  778. ubd_close_dev(ubd_dev);
  779. out:
  780. return err;
  781. }
  782. static int ubd_config(char *str, char **error_out)
  783. {
  784. int n, ret;
  785. /* This string is possibly broken up and stored, so it's only
  786. * freed if ubd_setup_common fails, or if only general options
  787. * were set.
  788. */
  789. str = kstrdup(str, GFP_KERNEL);
  790. if (str == NULL) {
  791. *error_out = "Failed to allocate memory";
  792. return -ENOMEM;
  793. }
  794. ret = ubd_setup_common(str, &n, error_out);
  795. if (ret)
  796. goto err_free;
  797. if (n == -1) {
  798. ret = 0;
  799. goto err_free;
  800. }
  801. mutex_lock(&ubd_lock);
  802. ret = ubd_add(n, error_out);
  803. if (ret)
  804. ubd_devs[n].file = NULL;
  805. mutex_unlock(&ubd_lock);
  806. out:
  807. return ret;
  808. err_free:
  809. kfree(str);
  810. goto out;
  811. }
  812. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  813. {
  814. struct ubd *ubd_dev;
  815. int n, len = 0;
  816. n = parse_unit(&name);
  817. if((n >= MAX_DEV) || (n < 0)){
  818. *error_out = "ubd_get_config : device number out of range";
  819. return -1;
  820. }
  821. ubd_dev = &ubd_devs[n];
  822. mutex_lock(&ubd_lock);
  823. if(ubd_dev->file == NULL){
  824. CONFIG_CHUNK(str, size, len, "", 1);
  825. goto out;
  826. }
  827. CONFIG_CHUNK(str, size, len, ubd_dev->file, 0);
  828. if(ubd_dev->cow.file != NULL){
  829. CONFIG_CHUNK(str, size, len, ",", 0);
  830. CONFIG_CHUNK(str, size, len, ubd_dev->cow.file, 1);
  831. }
  832. else CONFIG_CHUNK(str, size, len, "", 1);
  833. out:
  834. mutex_unlock(&ubd_lock);
  835. return len;
  836. }
  837. static int ubd_id(char **str, int *start_out, int *end_out)
  838. {
  839. int n;
  840. n = parse_unit(str);
  841. *start_out = 0;
  842. *end_out = MAX_DEV - 1;
  843. return n;
  844. }
  845. static int ubd_remove(int n, char **error_out)
  846. {
  847. struct ubd *ubd_dev;
  848. int err = -ENODEV;
  849. mutex_lock(&ubd_lock);
  850. ubd_dev = &ubd_devs[n];
  851. if(ubd_dev->file == NULL)
  852. goto out;
  853. if (ubd_dev->disk) {
  854. /* you cannot remove a open disk */
  855. err = -EBUSY;
  856. if (disk_openers(ubd_dev->disk))
  857. goto out;
  858. del_gendisk(ubd_dev->disk);
  859. ubd_close_dev(ubd_dev);
  860. put_disk(ubd_dev->disk);
  861. }
  862. err = 0;
  863. platform_device_unregister(&ubd_dev->pdev);
  864. out:
  865. mutex_unlock(&ubd_lock);
  866. return err;
  867. }
  868. /* All these are called by mconsole in process context and without
  869. * ubd-specific locks. The structure itself is const except for .list.
  870. */
  871. static struct mc_device ubd_mc = {
  872. .list = LIST_HEAD_INIT(ubd_mc.list),
  873. .name = "ubd",
  874. .config = ubd_config,
  875. .get_config = ubd_get_config,
  876. .id = ubd_id,
  877. .remove = ubd_remove,
  878. };
  879. static int __init ubd_mc_init(void)
  880. {
  881. mconsole_register_dev(&ubd_mc);
  882. return 0;
  883. }
  884. __initcall(ubd_mc_init);
  885. static int __init ubd0_init(void)
  886. {
  887. struct ubd *ubd_dev = &ubd_devs[0];
  888. mutex_lock(&ubd_lock);
  889. if(ubd_dev->file == NULL)
  890. ubd_dev->file = "root_fs";
  891. mutex_unlock(&ubd_lock);
  892. return 0;
  893. }
  894. __initcall(ubd0_init);
  895. /* Used in ubd_init, which is an initcall */
  896. static struct platform_driver ubd_driver = {
  897. .driver = {
  898. .name = DRIVER_NAME,
  899. },
  900. };
  901. static int __init ubd_init(void)
  902. {
  903. char *error;
  904. int i, err;
  905. if (register_blkdev(UBD_MAJOR, "ubd"))
  906. return -1;
  907. irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
  908. sizeof(struct io_thread_req *),
  909. GFP_KERNEL
  910. );
  911. irq_remainder = 0;
  912. if (irq_req_buffer == NULL) {
  913. printk(KERN_ERR "Failed to initialize ubd buffering\n");
  914. return -ENOMEM;
  915. }
  916. io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
  917. sizeof(struct io_thread_req *),
  918. GFP_KERNEL
  919. );
  920. io_remainder = 0;
  921. if (io_req_buffer == NULL) {
  922. printk(KERN_ERR "Failed to initialize ubd buffering\n");
  923. return -ENOMEM;
  924. }
  925. platform_driver_register(&ubd_driver);
  926. mutex_lock(&ubd_lock);
  927. for (i = 0; i < MAX_DEV; i++){
  928. err = ubd_add(i, &error);
  929. if(err)
  930. printk(KERN_ERR "Failed to initialize ubd device %d :"
  931. "%s\n", i, error);
  932. }
  933. mutex_unlock(&ubd_lock);
  934. return 0;
  935. }
  936. late_initcall(ubd_init);
  937. static int __init ubd_driver_init(void){
  938. unsigned long stack;
  939. int err;
  940. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  941. if(global_openflags.s){
  942. printk(KERN_INFO "ubd: Synchronous mode\n");
  943. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  944. * enough. So use anyway the io thread. */
  945. }
  946. stack = alloc_stack(0, 0);
  947. io_pid = start_io_thread(stack + PAGE_SIZE, &thread_fd);
  948. if(io_pid < 0){
  949. printk(KERN_ERR
  950. "ubd : Failed to start I/O thread (errno = %d) - "
  951. "falling back to synchronous I/O\n", -io_pid);
  952. io_pid = -1;
  953. return 0;
  954. }
  955. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  956. 0, "ubd", ubd_devs);
  957. if(err < 0)
  958. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  959. return 0;
  960. }
  961. device_initcall(ubd_driver_init);
  962. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  963. __u64 *cow_offset, unsigned long *bitmap,
  964. __u64 bitmap_offset, unsigned long *bitmap_words,
  965. __u64 bitmap_len)
  966. {
  967. __u64 sector = io_offset >> SECTOR_SHIFT;
  968. int i, update_bitmap = 0;
  969. for (i = 0; i < length >> SECTOR_SHIFT; i++) {
  970. if(cow_mask != NULL)
  971. ubd_set_bit(i, (unsigned char *) cow_mask);
  972. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  973. continue;
  974. update_bitmap = 1;
  975. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  976. }
  977. if(!update_bitmap)
  978. return;
  979. *cow_offset = sector / (sizeof(unsigned long) * 8);
  980. /* This takes care of the case where we're exactly at the end of the
  981. * device, and *cow_offset + 1 is off the end. So, just back it up
  982. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  983. * for the original diagnosis.
  984. */
  985. if (*cow_offset == (DIV_ROUND_UP(bitmap_len,
  986. sizeof(unsigned long)) - 1))
  987. (*cow_offset)--;
  988. bitmap_words[0] = bitmap[*cow_offset];
  989. bitmap_words[1] = bitmap[*cow_offset + 1];
  990. *cow_offset *= sizeof(unsigned long);
  991. *cow_offset += bitmap_offset;
  992. }
  993. static void cowify_req(struct io_thread_req *req, struct io_desc *segment,
  994. unsigned long offset, unsigned long *bitmap,
  995. __u64 bitmap_offset, __u64 bitmap_len)
  996. {
  997. __u64 sector = offset >> SECTOR_SHIFT;
  998. int i;
  999. if (segment->length > (sizeof(segment->sector_mask) * 8) << SECTOR_SHIFT)
  1000. panic("Operation too long");
  1001. if (req_op(req->req) == REQ_OP_READ) {
  1002. for (i = 0; i < segment->length >> SECTOR_SHIFT; i++) {
  1003. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  1004. ubd_set_bit(i, (unsigned char *)
  1005. &segment->sector_mask);
  1006. }
  1007. } else {
  1008. cowify_bitmap(offset, segment->length, &segment->sector_mask,
  1009. &segment->cow_offset, bitmap, bitmap_offset,
  1010. segment->bitmap_words, bitmap_len);
  1011. }
  1012. }
  1013. static void ubd_map_req(struct ubd *dev, struct io_thread_req *io_req,
  1014. struct request *req)
  1015. {
  1016. struct bio_vec bvec;
  1017. struct req_iterator iter;
  1018. int i = 0;
  1019. unsigned long byte_offset = io_req->offset;
  1020. enum req_op op = req_op(req);
  1021. if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD) {
  1022. io_req->io_desc[0].buffer = NULL;
  1023. io_req->io_desc[0].length = blk_rq_bytes(req);
  1024. } else {
  1025. rq_for_each_segment(bvec, req, iter) {
  1026. BUG_ON(i >= io_req->desc_cnt);
  1027. io_req->io_desc[i].buffer = bvec_virt(&bvec);
  1028. io_req->io_desc[i].length = bvec.bv_len;
  1029. i++;
  1030. }
  1031. }
  1032. if (dev->cow.file) {
  1033. for (i = 0; i < io_req->desc_cnt; i++) {
  1034. cowify_req(io_req, &io_req->io_desc[i], byte_offset,
  1035. dev->cow.bitmap, dev->cow.bitmap_offset,
  1036. dev->cow.bitmap_len);
  1037. byte_offset += io_req->io_desc[i].length;
  1038. }
  1039. }
  1040. }
  1041. static struct io_thread_req *ubd_alloc_req(struct ubd *dev, struct request *req,
  1042. int desc_cnt)
  1043. {
  1044. struct io_thread_req *io_req;
  1045. int i;
  1046. io_req = kmalloc(sizeof(*io_req) +
  1047. (desc_cnt * sizeof(struct io_desc)),
  1048. GFP_ATOMIC);
  1049. if (!io_req)
  1050. return NULL;
  1051. io_req->req = req;
  1052. if (dev->cow.file)
  1053. io_req->fds[0] = dev->cow.fd;
  1054. else
  1055. io_req->fds[0] = dev->fd;
  1056. io_req->error = 0;
  1057. io_req->sectorsize = SECTOR_SIZE;
  1058. io_req->fds[1] = dev->fd;
  1059. io_req->offset = (u64) blk_rq_pos(req) << SECTOR_SHIFT;
  1060. io_req->offsets[0] = 0;
  1061. io_req->offsets[1] = dev->cow.data_offset;
  1062. for (i = 0 ; i < desc_cnt; i++) {
  1063. io_req->io_desc[i].sector_mask = 0;
  1064. io_req->io_desc[i].cow_offset = -1;
  1065. }
  1066. return io_req;
  1067. }
  1068. static int ubd_submit_request(struct ubd *dev, struct request *req)
  1069. {
  1070. int segs = 0;
  1071. struct io_thread_req *io_req;
  1072. int ret;
  1073. enum req_op op = req_op(req);
  1074. if (op == REQ_OP_FLUSH)
  1075. segs = 0;
  1076. else if (op == REQ_OP_WRITE_ZEROES || op == REQ_OP_DISCARD)
  1077. segs = 1;
  1078. else
  1079. segs = blk_rq_nr_phys_segments(req);
  1080. io_req = ubd_alloc_req(dev, req, segs);
  1081. if (!io_req)
  1082. return -ENOMEM;
  1083. io_req->desc_cnt = segs;
  1084. if (segs)
  1085. ubd_map_req(dev, io_req, req);
  1086. ret = os_write_file(thread_fd, &io_req, sizeof(io_req));
  1087. if (ret != sizeof(io_req)) {
  1088. if (ret != -EAGAIN)
  1089. pr_err("write to io thread failed: %d\n", -ret);
  1090. kfree(io_req);
  1091. }
  1092. return ret;
  1093. }
  1094. static blk_status_t ubd_queue_rq(struct blk_mq_hw_ctx *hctx,
  1095. const struct blk_mq_queue_data *bd)
  1096. {
  1097. struct ubd *ubd_dev = hctx->queue->queuedata;
  1098. struct request *req = bd->rq;
  1099. int ret = 0, res = BLK_STS_OK;
  1100. blk_mq_start_request(req);
  1101. spin_lock_irq(&ubd_dev->lock);
  1102. switch (req_op(req)) {
  1103. case REQ_OP_FLUSH:
  1104. case REQ_OP_READ:
  1105. case REQ_OP_WRITE:
  1106. case REQ_OP_DISCARD:
  1107. case REQ_OP_WRITE_ZEROES:
  1108. ret = ubd_submit_request(ubd_dev, req);
  1109. break;
  1110. default:
  1111. WARN_ON_ONCE(1);
  1112. res = BLK_STS_NOTSUPP;
  1113. }
  1114. spin_unlock_irq(&ubd_dev->lock);
  1115. if (ret < 0) {
  1116. if (ret == -ENOMEM)
  1117. res = BLK_STS_RESOURCE;
  1118. else
  1119. res = BLK_STS_DEV_RESOURCE;
  1120. }
  1121. return res;
  1122. }
  1123. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  1124. {
  1125. struct ubd *ubd_dev = bdev->bd_disk->private_data;
  1126. geo->heads = 128;
  1127. geo->sectors = 32;
  1128. geo->cylinders = ubd_dev->size / (128 * 32 * 512);
  1129. return 0;
  1130. }
  1131. static int ubd_ioctl(struct block_device *bdev, blk_mode_t mode,
  1132. unsigned int cmd, unsigned long arg)
  1133. {
  1134. struct ubd *ubd_dev = bdev->bd_disk->private_data;
  1135. u16 ubd_id[ATA_ID_WORDS];
  1136. switch (cmd) {
  1137. struct cdrom_volctrl volume;
  1138. case HDIO_GET_IDENTITY:
  1139. memset(&ubd_id, 0, ATA_ID_WORDS * 2);
  1140. ubd_id[ATA_ID_CYLS] = ubd_dev->size / (128 * 32 * 512);
  1141. ubd_id[ATA_ID_HEADS] = 128;
  1142. ubd_id[ATA_ID_SECTORS] = 32;
  1143. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  1144. sizeof(ubd_id)))
  1145. return -EFAULT;
  1146. return 0;
  1147. case CDROMVOLREAD:
  1148. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  1149. return -EFAULT;
  1150. volume.channel0 = 255;
  1151. volume.channel1 = 255;
  1152. volume.channel2 = 255;
  1153. volume.channel3 = 255;
  1154. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  1155. return -EFAULT;
  1156. return 0;
  1157. }
  1158. return -EINVAL;
  1159. }
  1160. static int map_error(int error_code)
  1161. {
  1162. switch (error_code) {
  1163. case 0:
  1164. return BLK_STS_OK;
  1165. case ENOSYS:
  1166. case EOPNOTSUPP:
  1167. return BLK_STS_NOTSUPP;
  1168. case ENOSPC:
  1169. return BLK_STS_NOSPC;
  1170. }
  1171. return BLK_STS_IOERR;
  1172. }
  1173. /*
  1174. * Everything from here onwards *IS NOT PART OF THE KERNEL*
  1175. *
  1176. * The following functions are part of UML hypervisor code.
  1177. * All functions from here onwards are executed as a helper
  1178. * thread and are not allowed to execute any kernel functions.
  1179. *
  1180. * Any communication must occur strictly via shared memory and IPC.
  1181. *
  1182. * Do not add printks, locks, kernel memory operations, etc - it
  1183. * will result in unpredictable behaviour and/or crashes.
  1184. */
  1185. static int update_bitmap(struct io_thread_req *req, struct io_desc *segment)
  1186. {
  1187. int n;
  1188. if (segment->cow_offset == -1)
  1189. return map_error(0);
  1190. n = os_pwrite_file(req->fds[1], &segment->bitmap_words,
  1191. sizeof(segment->bitmap_words), segment->cow_offset);
  1192. if (n != sizeof(segment->bitmap_words))
  1193. return map_error(-n);
  1194. return map_error(0);
  1195. }
  1196. static void do_io(struct io_thread_req *req, struct io_desc *desc)
  1197. {
  1198. char *buf = NULL;
  1199. unsigned long len;
  1200. int n, nsectors, start, end, bit;
  1201. __u64 off;
  1202. /* FLUSH is really a special case, we cannot "case" it with others */
  1203. if (req_op(req->req) == REQ_OP_FLUSH) {
  1204. /* fds[0] is always either the rw image or our cow file */
  1205. req->error = map_error(-os_sync_file(req->fds[0]));
  1206. return;
  1207. }
  1208. nsectors = desc->length / req->sectorsize;
  1209. start = 0;
  1210. do {
  1211. bit = ubd_test_bit(start, (unsigned char *) &desc->sector_mask);
  1212. end = start;
  1213. while((end < nsectors) &&
  1214. (ubd_test_bit(end, (unsigned char *) &desc->sector_mask) == bit))
  1215. end++;
  1216. off = req->offset + req->offsets[bit] +
  1217. start * req->sectorsize;
  1218. len = (end - start) * req->sectorsize;
  1219. if (desc->buffer != NULL)
  1220. buf = &desc->buffer[start * req->sectorsize];
  1221. switch (req_op(req->req)) {
  1222. case REQ_OP_READ:
  1223. n = 0;
  1224. do {
  1225. buf = &buf[n];
  1226. len -= n;
  1227. n = os_pread_file(req->fds[bit], buf, len, off);
  1228. if (n < 0) {
  1229. req->error = map_error(-n);
  1230. return;
  1231. }
  1232. } while((n < len) && (n != 0));
  1233. if (n < len) memset(&buf[n], 0, len - n);
  1234. break;
  1235. case REQ_OP_WRITE:
  1236. n = os_pwrite_file(req->fds[bit], buf, len, off);
  1237. if(n != len){
  1238. req->error = map_error(-n);
  1239. return;
  1240. }
  1241. break;
  1242. case REQ_OP_DISCARD:
  1243. n = os_falloc_punch(req->fds[bit], off, len);
  1244. if (n) {
  1245. req->error = map_error(-n);
  1246. return;
  1247. }
  1248. break;
  1249. case REQ_OP_WRITE_ZEROES:
  1250. n = os_falloc_zeroes(req->fds[bit], off, len);
  1251. if (n) {
  1252. req->error = map_error(-n);
  1253. return;
  1254. }
  1255. break;
  1256. default:
  1257. WARN_ON_ONCE(1);
  1258. req->error = BLK_STS_NOTSUPP;
  1259. return;
  1260. }
  1261. start = end;
  1262. } while(start < nsectors);
  1263. req->offset += len;
  1264. req->error = update_bitmap(req, desc);
  1265. }
  1266. /* Changed in start_io_thread, which is serialized by being called only
  1267. * from ubd_init, which is an initcall.
  1268. */
  1269. int kernel_fd = -1;
  1270. /* Only changed by the io thread. XXX: currently unused. */
  1271. static int io_count;
  1272. int io_thread(void *arg)
  1273. {
  1274. int n, count, written, res;
  1275. os_fix_helper_signals();
  1276. while(1){
  1277. n = bulk_req_safe_read(
  1278. kernel_fd,
  1279. io_req_buffer,
  1280. &io_remainder,
  1281. &io_remainder_size,
  1282. UBD_REQ_BUFFER_SIZE
  1283. );
  1284. if (n <= 0) {
  1285. if (n == -EAGAIN)
  1286. ubd_read_poll(-1);
  1287. continue;
  1288. }
  1289. for (count = 0; count < n/sizeof(struct io_thread_req *); count++) {
  1290. struct io_thread_req *req = (*io_req_buffer)[count];
  1291. int i;
  1292. io_count++;
  1293. for (i = 0; !req->error && i < req->desc_cnt; i++)
  1294. do_io(req, &(req->io_desc[i]));
  1295. }
  1296. written = 0;
  1297. do {
  1298. res = os_write_file(kernel_fd,
  1299. ((char *) io_req_buffer) + written,
  1300. n - written);
  1301. if (res >= 0) {
  1302. written += res;
  1303. }
  1304. if (written < n) {
  1305. ubd_write_poll(-1);
  1306. }
  1307. } while (written < n);
  1308. }
  1309. return 0;
  1310. }