ubd_kern.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /*
  2. * Copyright (C) 2000 Jeff Dike (jdike@karaya.com)
  3. * Licensed under the GPL
  4. */
  5. /* 2001-09-28...2002-04-17
  6. * Partition stuff by James_McMechan@hotmail.com
  7. * old style ubd by setting UBD_SHIFT to 0
  8. * 2002-09-27...2002-10-18 massive tinkering for 2.5
  9. * partitions have changed in 2.5
  10. * 2003-01-29 more tinkering for 2.5.59-1
  11. * This should now address the sysfs problems and has
  12. * the symlink for devfs to allow for booting with
  13. * the common /dev/ubd/discX/... names rather than
  14. * only /dev/ubdN/discN this version also has lots of
  15. * clean ups preparing for ubd-many.
  16. * James McMechan
  17. */
  18. #define MAJOR_NR UBD_MAJOR
  19. #define UBD_SHIFT 4
  20. #include "linux/config.h"
  21. #include "linux/module.h"
  22. #include "linux/blkdev.h"
  23. #include "linux/hdreg.h"
  24. #include "linux/init.h"
  25. #include "linux/cdrom.h"
  26. #include "linux/proc_fs.h"
  27. #include "linux/ctype.h"
  28. #include "linux/capability.h"
  29. #include "linux/mm.h"
  30. #include "linux/vmalloc.h"
  31. #include "linux/blkpg.h"
  32. #include "linux/genhd.h"
  33. #include "linux/spinlock.h"
  34. #include "linux/platform_device.h"
  35. #include "asm/segment.h"
  36. #include "asm/uaccess.h"
  37. #include "asm/irq.h"
  38. #include "asm/types.h"
  39. #include "asm/tlbflush.h"
  40. #include "user_util.h"
  41. #include "mem_user.h"
  42. #include "kern_util.h"
  43. #include "kern.h"
  44. #include "mconsole_kern.h"
  45. #include "init.h"
  46. #include "irq_user.h"
  47. #include "irq_kern.h"
  48. #include "ubd_user.h"
  49. #include "os.h"
  50. #include "mem.h"
  51. #include "mem_kern.h"
  52. #include "cow.h"
  53. enum ubd_req { UBD_READ, UBD_WRITE };
  54. struct io_thread_req {
  55. enum ubd_req op;
  56. int fds[2];
  57. unsigned long offsets[2];
  58. unsigned long long offset;
  59. unsigned long length;
  60. char *buffer;
  61. int sectorsize;
  62. unsigned long sector_mask;
  63. unsigned long long cow_offset;
  64. unsigned long bitmap_words[2];
  65. int error;
  66. };
  67. extern int open_ubd_file(char *file, struct openflags *openflags, int shared,
  68. char **backing_file_out, int *bitmap_offset_out,
  69. unsigned long *bitmap_len_out, int *data_offset_out,
  70. int *create_cow_out);
  71. extern int create_cow_file(char *cow_file, char *backing_file,
  72. struct openflags flags, int sectorsize,
  73. int alignment, int *bitmap_offset_out,
  74. unsigned long *bitmap_len_out,
  75. int *data_offset_out);
  76. extern int read_cow_bitmap(int fd, void *buf, int offset, int len);
  77. extern void do_io(struct io_thread_req *req);
  78. static inline int ubd_test_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. return((data[n] & (1 << off)) != 0);
  86. }
  87. static inline void ubd_set_bit(__u64 bit, unsigned char *data)
  88. {
  89. __u64 n;
  90. int bits, off;
  91. bits = sizeof(data[0]) * 8;
  92. n = bit / bits;
  93. off = bit % bits;
  94. data[n] |= (1 << off);
  95. }
  96. /*End stuff from ubd_user.h*/
  97. #define DRIVER_NAME "uml-blkdev"
  98. static DEFINE_SPINLOCK(ubd_io_lock);
  99. static DEFINE_SPINLOCK(ubd_lock);
  100. static void (*do_ubd)(void);
  101. static int ubd_open(struct inode * inode, struct file * filp);
  102. static int ubd_release(struct inode * inode, struct file * file);
  103. static int ubd_ioctl(struct inode * inode, struct file * file,
  104. unsigned int cmd, unsigned long arg);
  105. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo);
  106. #define MAX_DEV (8)
  107. static struct block_device_operations ubd_blops = {
  108. .owner = THIS_MODULE,
  109. .open = ubd_open,
  110. .release = ubd_release,
  111. .ioctl = ubd_ioctl,
  112. .getgeo = ubd_getgeo,
  113. };
  114. /* Protected by the queue_lock */
  115. static request_queue_t *ubd_queue;
  116. /* Protected by ubd_lock */
  117. static int fake_major = MAJOR_NR;
  118. static struct gendisk *ubd_gendisk[MAX_DEV];
  119. static struct gendisk *fake_gendisk[MAX_DEV];
  120. #ifdef CONFIG_BLK_DEV_UBD_SYNC
  121. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 1, .c = 0, \
  122. .cl = 1 })
  123. #else
  124. #define OPEN_FLAGS ((struct openflags) { .r = 1, .w = 1, .s = 0, .c = 0, \
  125. .cl = 1 })
  126. #endif
  127. /* Not protected - changed only in ubd_setup_common and then only to
  128. * to enable O_SYNC.
  129. */
  130. static struct openflags global_openflags = OPEN_FLAGS;
  131. struct cow {
  132. /* This is the backing file, actually */
  133. char *file;
  134. int fd;
  135. unsigned long *bitmap;
  136. unsigned long bitmap_len;
  137. int bitmap_offset;
  138. int data_offset;
  139. };
  140. struct ubd {
  141. char *file;
  142. int count;
  143. int fd;
  144. __u64 size;
  145. struct openflags boot_openflags;
  146. struct openflags openflags;
  147. int shared;
  148. int no_cow;
  149. struct cow cow;
  150. struct platform_device pdev;
  151. };
  152. #define DEFAULT_COW { \
  153. .file = NULL, \
  154. .fd = -1, \
  155. .bitmap = NULL, \
  156. .bitmap_offset = 0, \
  157. .data_offset = 0, \
  158. }
  159. #define DEFAULT_UBD { \
  160. .file = NULL, \
  161. .count = 0, \
  162. .fd = -1, \
  163. .size = -1, \
  164. .boot_openflags = OPEN_FLAGS, \
  165. .openflags = OPEN_FLAGS, \
  166. .no_cow = 0, \
  167. .shared = 0, \
  168. .cow = DEFAULT_COW, \
  169. }
  170. struct ubd ubd_dev[MAX_DEV] = { [ 0 ... MAX_DEV - 1 ] = DEFAULT_UBD };
  171. static int ubd0_init(void)
  172. {
  173. struct ubd *dev = &ubd_dev[0];
  174. if(dev->file == NULL)
  175. dev->file = "root_fs";
  176. return(0);
  177. }
  178. __initcall(ubd0_init);
  179. /* Only changed by fake_ide_setup which is a setup */
  180. static int fake_ide = 0;
  181. static struct proc_dir_entry *proc_ide_root = NULL;
  182. static struct proc_dir_entry *proc_ide = NULL;
  183. static void make_proc_ide(void)
  184. {
  185. proc_ide_root = proc_mkdir("ide", NULL);
  186. proc_ide = proc_mkdir("ide0", proc_ide_root);
  187. }
  188. static int proc_ide_read_media(char *page, char **start, off_t off, int count,
  189. int *eof, void *data)
  190. {
  191. int len;
  192. strcpy(page, "disk\n");
  193. len = strlen("disk\n");
  194. len -= off;
  195. if (len < count){
  196. *eof = 1;
  197. if (len <= 0) return 0;
  198. }
  199. else len = count;
  200. *start = page + off;
  201. return len;
  202. }
  203. static void make_ide_entries(char *dev_name)
  204. {
  205. struct proc_dir_entry *dir, *ent;
  206. char name[64];
  207. if(proc_ide_root == NULL) make_proc_ide();
  208. dir = proc_mkdir(dev_name, proc_ide);
  209. if(!dir) return;
  210. ent = create_proc_entry("media", S_IFREG|S_IRUGO, dir);
  211. if(!ent) return;
  212. ent->nlink = 1;
  213. ent->data = NULL;
  214. ent->read_proc = proc_ide_read_media;
  215. ent->write_proc = NULL;
  216. sprintf(name,"ide0/%s", dev_name);
  217. proc_symlink(dev_name, proc_ide_root, name);
  218. }
  219. static int fake_ide_setup(char *str)
  220. {
  221. fake_ide = 1;
  222. return(1);
  223. }
  224. __setup("fake_ide", fake_ide_setup);
  225. __uml_help(fake_ide_setup,
  226. "fake_ide\n"
  227. " Create ide0 entries that map onto ubd devices.\n\n"
  228. );
  229. static int parse_unit(char **ptr)
  230. {
  231. char *str = *ptr, *end;
  232. int n = -1;
  233. if(isdigit(*str)) {
  234. n = simple_strtoul(str, &end, 0);
  235. if(end == str)
  236. return(-1);
  237. *ptr = end;
  238. }
  239. else if (('a' <= *str) && (*str <= 'h')) {
  240. n = *str - 'a';
  241. str++;
  242. *ptr = str;
  243. }
  244. return(n);
  245. }
  246. static int ubd_setup_common(char *str, int *index_out)
  247. {
  248. struct ubd *dev;
  249. struct openflags flags = global_openflags;
  250. char *backing_file;
  251. int n, err, i;
  252. if(index_out) *index_out = -1;
  253. n = *str;
  254. if(n == '='){
  255. char *end;
  256. int major;
  257. str++;
  258. if(!strcmp(str, "sync")){
  259. global_openflags = of_sync(global_openflags);
  260. return(0);
  261. }
  262. major = simple_strtoul(str, &end, 0);
  263. if((*end != '\0') || (end == str)){
  264. printk(KERN_ERR
  265. "ubd_setup : didn't parse major number\n");
  266. return(1);
  267. }
  268. err = 1;
  269. spin_lock(&ubd_lock);
  270. if(fake_major != MAJOR_NR){
  271. printk(KERN_ERR "Can't assign a fake major twice\n");
  272. goto out1;
  273. }
  274. fake_major = major;
  275. printk(KERN_INFO "Setting extra ubd major number to %d\n",
  276. major);
  277. err = 0;
  278. out1:
  279. spin_unlock(&ubd_lock);
  280. return(err);
  281. }
  282. n = parse_unit(&str);
  283. if(n < 0){
  284. printk(KERN_ERR "ubd_setup : couldn't parse unit number "
  285. "'%s'\n", str);
  286. return(1);
  287. }
  288. if(n >= MAX_DEV){
  289. printk(KERN_ERR "ubd_setup : index %d out of range "
  290. "(%d devices, from 0 to %d)\n", n, MAX_DEV, MAX_DEV - 1);
  291. return(1);
  292. }
  293. err = 1;
  294. spin_lock(&ubd_lock);
  295. dev = &ubd_dev[n];
  296. if(dev->file != NULL){
  297. printk(KERN_ERR "ubd_setup : device already configured\n");
  298. goto out;
  299. }
  300. if (index_out)
  301. *index_out = n;
  302. for (i = 0; i < sizeof("rscd="); i++) {
  303. switch (*str) {
  304. case 'r':
  305. flags.w = 0;
  306. break;
  307. case 's':
  308. flags.s = 1;
  309. break;
  310. case 'd':
  311. dev->no_cow = 1;
  312. break;
  313. case 'c':
  314. dev->shared = 1;
  315. break;
  316. case '=':
  317. str++;
  318. goto break_loop;
  319. default:
  320. printk(KERN_ERR "ubd_setup : Expected '=' or flag letter (r, s, c, or d)\n");
  321. goto out;
  322. }
  323. str++;
  324. }
  325. if (*str == '=')
  326. printk(KERN_ERR "ubd_setup : Too many flags specified\n");
  327. else
  328. printk(KERN_ERR "ubd_setup : Expected '='\n");
  329. goto out;
  330. break_loop:
  331. err = 0;
  332. backing_file = strchr(str, ',');
  333. if (!backing_file) {
  334. backing_file = strchr(str, ':');
  335. }
  336. if(backing_file){
  337. if(dev->no_cow)
  338. printk(KERN_ERR "Can't specify both 'd' and a "
  339. "cow file\n");
  340. else {
  341. *backing_file = '\0';
  342. backing_file++;
  343. }
  344. }
  345. dev->file = str;
  346. dev->cow.file = backing_file;
  347. dev->boot_openflags = flags;
  348. out:
  349. spin_unlock(&ubd_lock);
  350. return(err);
  351. }
  352. static int ubd_setup(char *str)
  353. {
  354. ubd_setup_common(str, NULL);
  355. return(1);
  356. }
  357. __setup("ubd", ubd_setup);
  358. __uml_help(ubd_setup,
  359. "ubd<n><flags>=<filename>[(:|,)<filename2>]\n"
  360. " This is used to associate a device with a file in the underlying\n"
  361. " filesystem. When specifying two filenames, the first one is the\n"
  362. " COW name and the second is the backing file name. As separator you can\n"
  363. " use either a ':' or a ',': the first one allows writing things like;\n"
  364. " ubd0=~/Uml/root_cow:~/Uml/root_backing_file\n"
  365. " while with a ',' the shell would not expand the 2nd '~'.\n"
  366. " When using only one filename, UML will detect whether to thread it like\n"
  367. " a COW file or a backing file. To override this detection, add the 'd'\n"
  368. " flag:\n"
  369. " ubd0d=BackingFile\n"
  370. " Usually, there is a filesystem in the file, but \n"
  371. " that's not required. Swap devices containing swap files can be\n"
  372. " specified like this. Also, a file which doesn't contain a\n"
  373. " filesystem can have its contents read in the virtual \n"
  374. " machine by running 'dd' on the device. <n> must be in the range\n"
  375. " 0 to 7. Appending an 'r' to the number will cause that device\n"
  376. " to be mounted read-only. For example ubd1r=./ext_fs. Appending\n"
  377. " an 's' will cause data to be written to disk on the host immediately.\n\n"
  378. );
  379. static int udb_setup(char *str)
  380. {
  381. printk("udb%s specified on command line is almost certainly a ubd -> "
  382. "udb TYPO\n", str);
  383. return(1);
  384. }
  385. __setup("udb", udb_setup);
  386. __uml_help(udb_setup,
  387. "udb\n"
  388. " This option is here solely to catch ubd -> udb typos, which can be\n"
  389. " to impossible to catch visually unless you specifically look for\n"
  390. " them. The only result of any option starting with 'udb' is an error\n"
  391. " in the boot output.\n\n"
  392. );
  393. static int fakehd_set = 0;
  394. static int fakehd(char *str)
  395. {
  396. printk(KERN_INFO "fakehd : Changing ubd name to \"hd\".\n");
  397. fakehd_set = 1;
  398. return 1;
  399. }
  400. __setup("fakehd", fakehd);
  401. __uml_help(fakehd,
  402. "fakehd\n"
  403. " Change the ubd device name to \"hd\".\n\n"
  404. );
  405. static void do_ubd_request(request_queue_t * q);
  406. /* Only changed by ubd_init, which is an initcall. */
  407. int thread_fd = -1;
  408. /* Changed by ubd_handler, which is serialized because interrupts only
  409. * happen on CPU 0.
  410. */
  411. int intr_count = 0;
  412. /* call ubd_finish if you need to serialize */
  413. static void __ubd_finish(struct request *req, int error)
  414. {
  415. int nsect;
  416. if(error){
  417. end_request(req, 0);
  418. return;
  419. }
  420. nsect = req->current_nr_sectors;
  421. req->sector += nsect;
  422. req->buffer += nsect << 9;
  423. req->errors = 0;
  424. req->nr_sectors -= nsect;
  425. req->current_nr_sectors = 0;
  426. end_request(req, 1);
  427. }
  428. static inline void ubd_finish(struct request *req, int error)
  429. {
  430. spin_lock(&ubd_io_lock);
  431. __ubd_finish(req, error);
  432. spin_unlock(&ubd_io_lock);
  433. }
  434. /* Called without ubd_io_lock held */
  435. static void ubd_handler(void)
  436. {
  437. struct io_thread_req req;
  438. struct request *rq = elv_next_request(ubd_queue);
  439. int n;
  440. do_ubd = NULL;
  441. intr_count++;
  442. n = os_read_file(thread_fd, &req, sizeof(req));
  443. if(n != sizeof(req)){
  444. printk(KERN_ERR "Pid %d - spurious interrupt in ubd_handler, "
  445. "err = %d\n", os_getpid(), -n);
  446. spin_lock(&ubd_io_lock);
  447. end_request(rq, 0);
  448. spin_unlock(&ubd_io_lock);
  449. return;
  450. }
  451. ubd_finish(rq, req.error);
  452. reactivate_fd(thread_fd, UBD_IRQ);
  453. do_ubd_request(ubd_queue);
  454. }
  455. static irqreturn_t ubd_intr(int irq, void *dev, struct pt_regs *unused)
  456. {
  457. ubd_handler();
  458. return(IRQ_HANDLED);
  459. }
  460. /* Only changed by ubd_init, which is an initcall. */
  461. static int io_pid = -1;
  462. void kill_io_thread(void)
  463. {
  464. if(io_pid != -1)
  465. os_kill_process(io_pid, 1);
  466. }
  467. __uml_exitcall(kill_io_thread);
  468. static int ubd_file_size(struct ubd *dev, __u64 *size_out)
  469. {
  470. char *file;
  471. file = dev->cow.file ? dev->cow.file : dev->file;
  472. return(os_file_size(file, size_out));
  473. }
  474. static void ubd_close(struct ubd *dev)
  475. {
  476. os_close_file(dev->fd);
  477. if(dev->cow.file == NULL)
  478. return;
  479. os_close_file(dev->cow.fd);
  480. vfree(dev->cow.bitmap);
  481. dev->cow.bitmap = NULL;
  482. }
  483. static int ubd_open_dev(struct ubd *dev)
  484. {
  485. struct openflags flags;
  486. char **back_ptr;
  487. int err, create_cow, *create_ptr;
  488. dev->openflags = dev->boot_openflags;
  489. create_cow = 0;
  490. create_ptr = (dev->cow.file != NULL) ? &create_cow : NULL;
  491. back_ptr = dev->no_cow ? NULL : &dev->cow.file;
  492. dev->fd = open_ubd_file(dev->file, &dev->openflags, dev->shared,
  493. back_ptr, &dev->cow.bitmap_offset,
  494. &dev->cow.bitmap_len, &dev->cow.data_offset,
  495. create_ptr);
  496. if((dev->fd == -ENOENT) && create_cow){
  497. dev->fd = create_cow_file(dev->file, dev->cow.file,
  498. dev->openflags, 1 << 9, PAGE_SIZE,
  499. &dev->cow.bitmap_offset,
  500. &dev->cow.bitmap_len,
  501. &dev->cow.data_offset);
  502. if(dev->fd >= 0){
  503. printk(KERN_INFO "Creating \"%s\" as COW file for "
  504. "\"%s\"\n", dev->file, dev->cow.file);
  505. }
  506. }
  507. if(dev->fd < 0){
  508. printk("Failed to open '%s', errno = %d\n", dev->file,
  509. -dev->fd);
  510. return(dev->fd);
  511. }
  512. if(dev->cow.file != NULL){
  513. err = -ENOMEM;
  514. dev->cow.bitmap = (void *) vmalloc(dev->cow.bitmap_len);
  515. if(dev->cow.bitmap == NULL){
  516. printk(KERN_ERR "Failed to vmalloc COW bitmap\n");
  517. goto error;
  518. }
  519. flush_tlb_kernel_vm();
  520. err = read_cow_bitmap(dev->fd, dev->cow.bitmap,
  521. dev->cow.bitmap_offset,
  522. dev->cow.bitmap_len);
  523. if(err < 0)
  524. goto error;
  525. flags = dev->openflags;
  526. flags.w = 0;
  527. err = open_ubd_file(dev->cow.file, &flags, dev->shared, NULL,
  528. NULL, NULL, NULL, NULL);
  529. if(err < 0) goto error;
  530. dev->cow.fd = err;
  531. }
  532. return(0);
  533. error:
  534. os_close_file(dev->fd);
  535. return(err);
  536. }
  537. static int ubd_new_disk(int major, u64 size, int unit,
  538. struct gendisk **disk_out)
  539. {
  540. struct gendisk *disk;
  541. disk = alloc_disk(1 << UBD_SHIFT);
  542. if(disk == NULL)
  543. return(-ENOMEM);
  544. disk->major = major;
  545. disk->first_minor = unit << UBD_SHIFT;
  546. disk->fops = &ubd_blops;
  547. set_capacity(disk, size / 512);
  548. if(major == MAJOR_NR)
  549. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  550. else
  551. sprintf(disk->disk_name, "ubd_fake%d", unit);
  552. /* sysfs register (not for ide fake devices) */
  553. if (major == MAJOR_NR) {
  554. ubd_dev[unit].pdev.id = unit;
  555. ubd_dev[unit].pdev.name = DRIVER_NAME;
  556. platform_device_register(&ubd_dev[unit].pdev);
  557. disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
  558. }
  559. disk->private_data = &ubd_dev[unit];
  560. disk->queue = ubd_queue;
  561. add_disk(disk);
  562. *disk_out = disk;
  563. return 0;
  564. }
  565. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  566. static int ubd_add(int n)
  567. {
  568. struct ubd *dev = &ubd_dev[n];
  569. int err;
  570. err = -ENODEV;
  571. if(dev->file == NULL)
  572. goto out;
  573. if (ubd_open_dev(dev))
  574. goto out;
  575. err = ubd_file_size(dev, &dev->size);
  576. if(err < 0)
  577. goto out_close;
  578. dev->size = ROUND_BLOCK(dev->size);
  579. err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
  580. if(err)
  581. goto out_close;
  582. if(fake_major != MAJOR_NR)
  583. ubd_new_disk(fake_major, dev->size, n,
  584. &fake_gendisk[n]);
  585. /* perhaps this should also be under the "if (fake_major)" above */
  586. /* using the fake_disk->disk_name and also the fakehd_set name */
  587. if (fake_ide)
  588. make_ide_entries(ubd_gendisk[n]->disk_name);
  589. err = 0;
  590. out_close:
  591. ubd_close(dev);
  592. out:
  593. return err;
  594. }
  595. static int ubd_config(char *str)
  596. {
  597. int n, err;
  598. str = kstrdup(str, GFP_KERNEL);
  599. if(str == NULL){
  600. printk(KERN_ERR "ubd_config failed to strdup string\n");
  601. return(1);
  602. }
  603. err = ubd_setup_common(str, &n);
  604. if(err){
  605. kfree(str);
  606. return(-1);
  607. }
  608. if(n == -1) return(0);
  609. spin_lock(&ubd_lock);
  610. err = ubd_add(n);
  611. if(err)
  612. ubd_dev[n].file = NULL;
  613. spin_unlock(&ubd_lock);
  614. return(err);
  615. }
  616. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  617. {
  618. struct ubd *dev;
  619. int n, len = 0;
  620. n = parse_unit(&name);
  621. if((n >= MAX_DEV) || (n < 0)){
  622. *error_out = "ubd_get_config : device number out of range";
  623. return(-1);
  624. }
  625. dev = &ubd_dev[n];
  626. spin_lock(&ubd_lock);
  627. if(dev->file == NULL){
  628. CONFIG_CHUNK(str, size, len, "", 1);
  629. goto out;
  630. }
  631. CONFIG_CHUNK(str, size, len, dev->file, 0);
  632. if(dev->cow.file != NULL){
  633. CONFIG_CHUNK(str, size, len, ",", 0);
  634. CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
  635. }
  636. else CONFIG_CHUNK(str, size, len, "", 1);
  637. out:
  638. spin_unlock(&ubd_lock);
  639. return(len);
  640. }
  641. static int ubd_id(char **str, int *start_out, int *end_out)
  642. {
  643. int n;
  644. n = parse_unit(str);
  645. *start_out = 0;
  646. *end_out = MAX_DEV - 1;
  647. return n;
  648. }
  649. static int ubd_remove(int n)
  650. {
  651. struct ubd *dev;
  652. int err = -ENODEV;
  653. spin_lock(&ubd_lock);
  654. if(ubd_gendisk[n] == NULL)
  655. goto out;
  656. dev = &ubd_dev[n];
  657. if(dev->file == NULL)
  658. goto out;
  659. /* you cannot remove a open disk */
  660. err = -EBUSY;
  661. if(dev->count > 0)
  662. goto out;
  663. del_gendisk(ubd_gendisk[n]);
  664. put_disk(ubd_gendisk[n]);
  665. ubd_gendisk[n] = NULL;
  666. if(fake_gendisk[n] != NULL){
  667. del_gendisk(fake_gendisk[n]);
  668. put_disk(fake_gendisk[n]);
  669. fake_gendisk[n] = NULL;
  670. }
  671. platform_device_unregister(&dev->pdev);
  672. *dev = ((struct ubd) DEFAULT_UBD);
  673. err = 0;
  674. out:
  675. spin_unlock(&ubd_lock);
  676. return err;
  677. }
  678. static struct mc_device ubd_mc = {
  679. .name = "ubd",
  680. .config = ubd_config,
  681. .get_config = ubd_get_config,
  682. .id = ubd_id,
  683. .remove = ubd_remove,
  684. };
  685. static int ubd_mc_init(void)
  686. {
  687. mconsole_register_dev(&ubd_mc);
  688. return 0;
  689. }
  690. __initcall(ubd_mc_init);
  691. static struct platform_driver ubd_driver = {
  692. .driver = {
  693. .name = DRIVER_NAME,
  694. },
  695. };
  696. int ubd_init(void)
  697. {
  698. int i;
  699. if (register_blkdev(MAJOR_NR, "ubd"))
  700. return -1;
  701. ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
  702. if (!ubd_queue) {
  703. unregister_blkdev(MAJOR_NR, "ubd");
  704. return -1;
  705. }
  706. if (fake_major != MAJOR_NR) {
  707. char name[sizeof("ubd_nnn\0")];
  708. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  709. if (register_blkdev(fake_major, "ubd"))
  710. return -1;
  711. }
  712. platform_driver_register(&ubd_driver);
  713. for (i = 0; i < MAX_DEV; i++)
  714. ubd_add(i);
  715. return 0;
  716. }
  717. late_initcall(ubd_init);
  718. int ubd_driver_init(void){
  719. unsigned long stack;
  720. int err;
  721. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  722. if(global_openflags.s){
  723. printk(KERN_INFO "ubd: Synchronous mode\n");
  724. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  725. * enough. So use anyway the io thread. */
  726. }
  727. stack = alloc_stack(0, 0);
  728. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  729. &thread_fd);
  730. if(io_pid < 0){
  731. printk(KERN_ERR
  732. "ubd : Failed to start I/O thread (errno = %d) - "
  733. "falling back to synchronous I/O\n", -io_pid);
  734. io_pid = -1;
  735. return(0);
  736. }
  737. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  738. IRQF_DISABLED, "ubd", ubd_dev);
  739. if(err != 0)
  740. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  741. return 0;
  742. }
  743. device_initcall(ubd_driver_init);
  744. static int ubd_open(struct inode *inode, struct file *filp)
  745. {
  746. struct gendisk *disk = inode->i_bdev->bd_disk;
  747. struct ubd *dev = disk->private_data;
  748. int err = 0;
  749. if(dev->count == 0){
  750. err = ubd_open_dev(dev);
  751. if(err){
  752. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  753. disk->disk_name, dev->file, -err);
  754. goto out;
  755. }
  756. }
  757. dev->count++;
  758. set_disk_ro(disk, !dev->openflags.w);
  759. /* This should no more be needed. And it didn't work anyway to exclude
  760. * read-write remounting of filesystems.*/
  761. /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
  762. if(--dev->count == 0) ubd_close(dev);
  763. err = -EROFS;
  764. }*/
  765. out:
  766. return(err);
  767. }
  768. static int ubd_release(struct inode * inode, struct file * file)
  769. {
  770. struct gendisk *disk = inode->i_bdev->bd_disk;
  771. struct ubd *dev = disk->private_data;
  772. if(--dev->count == 0)
  773. ubd_close(dev);
  774. return(0);
  775. }
  776. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  777. __u64 *cow_offset, unsigned long *bitmap,
  778. __u64 bitmap_offset, unsigned long *bitmap_words,
  779. __u64 bitmap_len)
  780. {
  781. __u64 sector = io_offset >> 9;
  782. int i, update_bitmap = 0;
  783. for(i = 0; i < length >> 9; i++){
  784. if(cow_mask != NULL)
  785. ubd_set_bit(i, (unsigned char *) cow_mask);
  786. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  787. continue;
  788. update_bitmap = 1;
  789. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  790. }
  791. if(!update_bitmap)
  792. return;
  793. *cow_offset = sector / (sizeof(unsigned long) * 8);
  794. /* This takes care of the case where we're exactly at the end of the
  795. * device, and *cow_offset + 1 is off the end. So, just back it up
  796. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  797. * for the original diagnosis.
  798. */
  799. if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
  800. sizeof(unsigned long) - 1))
  801. (*cow_offset)--;
  802. bitmap_words[0] = bitmap[*cow_offset];
  803. bitmap_words[1] = bitmap[*cow_offset + 1];
  804. *cow_offset *= sizeof(unsigned long);
  805. *cow_offset += bitmap_offset;
  806. }
  807. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  808. __u64 bitmap_offset, __u64 bitmap_len)
  809. {
  810. __u64 sector = req->offset >> 9;
  811. int i;
  812. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  813. panic("Operation too long");
  814. if(req->op == UBD_READ) {
  815. for(i = 0; i < req->length >> 9; i++){
  816. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  817. ubd_set_bit(i, (unsigned char *)
  818. &req->sector_mask);
  819. }
  820. }
  821. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  822. &req->cow_offset, bitmap, bitmap_offset,
  823. req->bitmap_words, bitmap_len);
  824. }
  825. /* Called with ubd_io_lock held */
  826. static int prepare_request(struct request *req, struct io_thread_req *io_req)
  827. {
  828. struct gendisk *disk = req->rq_disk;
  829. struct ubd *dev = disk->private_data;
  830. __u64 offset;
  831. int len;
  832. if(req->rq_status == RQ_INACTIVE) return(1);
  833. /* This should be impossible now */
  834. if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
  835. printk("Write attempted on readonly ubd device %s\n",
  836. disk->disk_name);
  837. end_request(req, 0);
  838. return(1);
  839. }
  840. offset = ((__u64) req->sector) << 9;
  841. len = req->current_nr_sectors << 9;
  842. io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
  843. io_req->fds[1] = dev->fd;
  844. io_req->cow_offset = -1;
  845. io_req->offset = offset;
  846. io_req->length = len;
  847. io_req->error = 0;
  848. io_req->sector_mask = 0;
  849. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  850. io_req->offsets[0] = 0;
  851. io_req->offsets[1] = dev->cow.data_offset;
  852. io_req->buffer = req->buffer;
  853. io_req->sectorsize = 1 << 9;
  854. if(dev->cow.file != NULL)
  855. cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
  856. dev->cow.bitmap_len);
  857. return(0);
  858. }
  859. /* Called with ubd_io_lock held */
  860. static void do_ubd_request(request_queue_t *q)
  861. {
  862. struct io_thread_req io_req;
  863. struct request *req;
  864. int err, n;
  865. if(thread_fd == -1){
  866. while((req = elv_next_request(q)) != NULL){
  867. err = prepare_request(req, &io_req);
  868. if(!err){
  869. do_io(&io_req);
  870. __ubd_finish(req, io_req.error);
  871. }
  872. }
  873. }
  874. else {
  875. if(do_ubd || (req = elv_next_request(q)) == NULL)
  876. return;
  877. err = prepare_request(req, &io_req);
  878. if(!err){
  879. do_ubd = ubd_handler;
  880. n = os_write_file(thread_fd, (char *) &io_req,
  881. sizeof(io_req));
  882. if(n != sizeof(io_req))
  883. printk("write to io thread failed, "
  884. "errno = %d\n", -n);
  885. }
  886. }
  887. }
  888. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  889. {
  890. struct ubd *dev = bdev->bd_disk->private_data;
  891. geo->heads = 128;
  892. geo->sectors = 32;
  893. geo->cylinders = dev->size / (128 * 32 * 512);
  894. return 0;
  895. }
  896. static int ubd_ioctl(struct inode * inode, struct file * file,
  897. unsigned int cmd, unsigned long arg)
  898. {
  899. struct ubd *dev = inode->i_bdev->bd_disk->private_data;
  900. struct hd_driveid ubd_id = {
  901. .cyls = 0,
  902. .heads = 128,
  903. .sectors = 32,
  904. };
  905. switch (cmd) {
  906. struct cdrom_volctrl volume;
  907. case HDIO_GET_IDENTITY:
  908. ubd_id.cyls = dev->size / (128 * 32 * 512);
  909. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  910. sizeof(ubd_id)))
  911. return(-EFAULT);
  912. return(0);
  913. case CDROMVOLREAD:
  914. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  915. return(-EFAULT);
  916. volume.channel0 = 255;
  917. volume.channel1 = 255;
  918. volume.channel2 = 255;
  919. volume.channel3 = 255;
  920. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  921. return(-EFAULT);
  922. return(0);
  923. }
  924. return(-EINVAL);
  925. }
  926. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  927. {
  928. struct uml_stat buf1, buf2;
  929. int err;
  930. if(from_cmdline == NULL)
  931. return 0;
  932. if(!strcmp(from_cmdline, from_cow))
  933. return 0;
  934. err = os_stat_file(from_cmdline, &buf1);
  935. if(err < 0){
  936. printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
  937. return 0;
  938. }
  939. err = os_stat_file(from_cow, &buf2);
  940. if(err < 0){
  941. printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
  942. return 1;
  943. }
  944. if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  945. return 0;
  946. printk("Backing file mismatch - \"%s\" requested,\n"
  947. "\"%s\" specified in COW header of \"%s\"\n",
  948. from_cmdline, from_cow, cow);
  949. return 1;
  950. }
  951. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  952. {
  953. unsigned long modtime;
  954. unsigned long long actual;
  955. int err;
  956. err = os_file_modtime(file, &modtime);
  957. if(err < 0){
  958. printk("Failed to get modification time of backing file "
  959. "\"%s\", err = %d\n", file, -err);
  960. return(err);
  961. }
  962. err = os_file_size(file, &actual);
  963. if(err < 0){
  964. printk("Failed to get size of backing file \"%s\", "
  965. "err = %d\n", file, -err);
  966. return(err);
  967. }
  968. if(actual != size){
  969. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  970. * the typecast.*/
  971. printk("Size mismatch (%llu vs %llu) of COW header vs backing "
  972. "file\n", (unsigned long long) size, actual);
  973. return(-EINVAL);
  974. }
  975. if(modtime != mtime){
  976. printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
  977. "file\n", mtime, modtime);
  978. return(-EINVAL);
  979. }
  980. return(0);
  981. }
  982. int read_cow_bitmap(int fd, void *buf, int offset, int len)
  983. {
  984. int err;
  985. err = os_seek_file(fd, offset);
  986. if(err < 0)
  987. return(err);
  988. err = os_read_file(fd, buf, len);
  989. if(err < 0)
  990. return(err);
  991. return(0);
  992. }
  993. int open_ubd_file(char *file, struct openflags *openflags, int shared,
  994. char **backing_file_out, int *bitmap_offset_out,
  995. unsigned long *bitmap_len_out, int *data_offset_out,
  996. int *create_cow_out)
  997. {
  998. time_t mtime;
  999. unsigned long long size;
  1000. __u32 version, align;
  1001. char *backing_file;
  1002. int fd, err, sectorsize, asked_switch, mode = 0644;
  1003. fd = os_open_file(file, *openflags, mode);
  1004. if (fd < 0) {
  1005. if ((fd == -ENOENT) && (create_cow_out != NULL))
  1006. *create_cow_out = 1;
  1007. if (!openflags->w ||
  1008. ((fd != -EROFS) && (fd != -EACCES)))
  1009. return fd;
  1010. openflags->w = 0;
  1011. fd = os_open_file(file, *openflags, mode);
  1012. if (fd < 0)
  1013. return fd;
  1014. }
  1015. if(shared)
  1016. printk("Not locking \"%s\" on the host\n", file);
  1017. else {
  1018. err = os_lock_file(fd, openflags->w);
  1019. if(err < 0){
  1020. printk("Failed to lock '%s', err = %d\n", file, -err);
  1021. goto out_close;
  1022. }
  1023. }
  1024. /* Successful return case! */
  1025. if(backing_file_out == NULL)
  1026. return(fd);
  1027. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  1028. &size, &sectorsize, &align, bitmap_offset_out);
  1029. if(err && (*backing_file_out != NULL)){
  1030. printk("Failed to read COW header from COW file \"%s\", "
  1031. "errno = %d\n", file, -err);
  1032. goto out_close;
  1033. }
  1034. if(err)
  1035. return(fd);
  1036. asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
  1037. /* Allow switching only if no mismatch. */
  1038. if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
  1039. printk("Switching backing file to '%s'\n", *backing_file_out);
  1040. err = write_cow_header(file, fd, *backing_file_out,
  1041. sectorsize, align, &size);
  1042. if (err) {
  1043. printk("Switch failed, errno = %d\n", -err);
  1044. goto out_close;
  1045. }
  1046. } else {
  1047. *backing_file_out = backing_file;
  1048. err = backing_file_mismatch(*backing_file_out, size, mtime);
  1049. if (err)
  1050. goto out_close;
  1051. }
  1052. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  1053. bitmap_len_out, data_offset_out);
  1054. return fd;
  1055. out_close:
  1056. os_close_file(fd);
  1057. return err;
  1058. }
  1059. int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
  1060. int sectorsize, int alignment, int *bitmap_offset_out,
  1061. unsigned long *bitmap_len_out, int *data_offset_out)
  1062. {
  1063. int err, fd;
  1064. flags.c = 1;
  1065. fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
  1066. if(fd < 0){
  1067. err = fd;
  1068. printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
  1069. -err);
  1070. goto out;
  1071. }
  1072. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  1073. bitmap_offset_out, bitmap_len_out,
  1074. data_offset_out);
  1075. if(!err)
  1076. return(fd);
  1077. os_close_file(fd);
  1078. out:
  1079. return(err);
  1080. }
  1081. static int update_bitmap(struct io_thread_req *req)
  1082. {
  1083. int n;
  1084. if(req->cow_offset == -1)
  1085. return(0);
  1086. n = os_seek_file(req->fds[1], req->cow_offset);
  1087. if(n < 0){
  1088. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1089. return(1);
  1090. }
  1091. n = os_write_file(req->fds[1], &req->bitmap_words,
  1092. sizeof(req->bitmap_words));
  1093. if(n != sizeof(req->bitmap_words)){
  1094. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1095. req->fds[1]);
  1096. return(1);
  1097. }
  1098. return(0);
  1099. }
  1100. void do_io(struct io_thread_req *req)
  1101. {
  1102. char *buf;
  1103. unsigned long len;
  1104. int n, nsectors, start, end, bit;
  1105. int err;
  1106. __u64 off;
  1107. nsectors = req->length / req->sectorsize;
  1108. start = 0;
  1109. do {
  1110. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1111. end = start;
  1112. while((end < nsectors) &&
  1113. (ubd_test_bit(end, (unsigned char *)
  1114. &req->sector_mask) == bit))
  1115. end++;
  1116. off = req->offset + req->offsets[bit] +
  1117. start * req->sectorsize;
  1118. len = (end - start) * req->sectorsize;
  1119. buf = &req->buffer[start * req->sectorsize];
  1120. err = os_seek_file(req->fds[bit], off);
  1121. if(err < 0){
  1122. printk("do_io - lseek failed : err = %d\n", -err);
  1123. req->error = 1;
  1124. return;
  1125. }
  1126. if(req->op == UBD_READ){
  1127. n = 0;
  1128. do {
  1129. buf = &buf[n];
  1130. len -= n;
  1131. n = os_read_file(req->fds[bit], buf, len);
  1132. if (n < 0) {
  1133. printk("do_io - read failed, err = %d "
  1134. "fd = %d\n", -n, req->fds[bit]);
  1135. req->error = 1;
  1136. return;
  1137. }
  1138. } while((n < len) && (n != 0));
  1139. if (n < len) memset(&buf[n], 0, len - n);
  1140. } else {
  1141. n = os_write_file(req->fds[bit], buf, len);
  1142. if(n != len){
  1143. printk("do_io - write failed err = %d "
  1144. "fd = %d\n", -n, req->fds[bit]);
  1145. req->error = 1;
  1146. return;
  1147. }
  1148. }
  1149. start = end;
  1150. } while(start < nsectors);
  1151. req->error = update_bitmap(req);
  1152. }
  1153. /* Changed in start_io_thread, which is serialized by being called only
  1154. * from ubd_init, which is an initcall.
  1155. */
  1156. int kernel_fd = -1;
  1157. /* Only changed by the io thread */
  1158. int io_count = 0;
  1159. int io_thread(void *arg)
  1160. {
  1161. struct io_thread_req req;
  1162. int n;
  1163. ignore_sigwinch_sig();
  1164. while(1){
  1165. n = os_read_file(kernel_fd, &req, sizeof(req));
  1166. if(n != sizeof(req)){
  1167. if(n < 0)
  1168. printk("io_thread - read failed, fd = %d, "
  1169. "err = %d\n", kernel_fd, -n);
  1170. else {
  1171. printk("io_thread - short read, fd = %d, "
  1172. "length = %d\n", kernel_fd, n);
  1173. }
  1174. continue;
  1175. }
  1176. io_count++;
  1177. do_io(&req);
  1178. n = os_write_file(kernel_fd, &req, sizeof(req));
  1179. if(n != sizeof(req))
  1180. printk("io_thread - write failed, fd = %d, err = %d\n",
  1181. kernel_fd, -n);
  1182. }
  1183. return 0;
  1184. }