ubd_kern.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395
  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. int err;
  542. disk = alloc_disk(1 << UBD_SHIFT);
  543. if(disk == NULL)
  544. return(-ENOMEM);
  545. disk->major = major;
  546. disk->first_minor = unit << UBD_SHIFT;
  547. disk->fops = &ubd_blops;
  548. set_capacity(disk, size / 512);
  549. if(major == MAJOR_NR)
  550. sprintf(disk->disk_name, "ubd%c", 'a' + unit);
  551. else
  552. sprintf(disk->disk_name, "ubd_fake%d", unit);
  553. /* sysfs register (not for ide fake devices) */
  554. if (major == MAJOR_NR) {
  555. ubd_dev[unit].pdev.id = unit;
  556. ubd_dev[unit].pdev.name = DRIVER_NAME;
  557. platform_device_register(&ubd_dev[unit].pdev);
  558. disk->driverfs_dev = &ubd_dev[unit].pdev.dev;
  559. }
  560. disk->private_data = &ubd_dev[unit];
  561. disk->queue = ubd_queue;
  562. add_disk(disk);
  563. *disk_out = disk;
  564. return 0;
  565. }
  566. #define ROUND_BLOCK(n) ((n + ((1 << 9) - 1)) & (-1 << 9))
  567. static int ubd_add(int n)
  568. {
  569. struct ubd *dev = &ubd_dev[n];
  570. int err;
  571. err = -ENODEV;
  572. if(dev->file == NULL)
  573. goto out;
  574. if (ubd_open_dev(dev))
  575. goto out;
  576. err = ubd_file_size(dev, &dev->size);
  577. if(err < 0)
  578. goto out_close;
  579. dev->size = ROUND_BLOCK(dev->size);
  580. err = ubd_new_disk(MAJOR_NR, dev->size, n, &ubd_gendisk[n]);
  581. if(err)
  582. goto out_close;
  583. if(fake_major != MAJOR_NR)
  584. ubd_new_disk(fake_major, dev->size, n,
  585. &fake_gendisk[n]);
  586. /* perhaps this should also be under the "if (fake_major)" above */
  587. /* using the fake_disk->disk_name and also the fakehd_set name */
  588. if (fake_ide)
  589. make_ide_entries(ubd_gendisk[n]->disk_name);
  590. err = 0;
  591. out_close:
  592. ubd_close(dev);
  593. out:
  594. return err;
  595. }
  596. static int ubd_config(char *str)
  597. {
  598. int n, err;
  599. str = kstrdup(str, GFP_KERNEL);
  600. if(str == NULL){
  601. printk(KERN_ERR "ubd_config failed to strdup string\n");
  602. return(1);
  603. }
  604. err = ubd_setup_common(str, &n);
  605. if(err){
  606. kfree(str);
  607. return(-1);
  608. }
  609. if(n == -1) return(0);
  610. spin_lock(&ubd_lock);
  611. err = ubd_add(n);
  612. if(err)
  613. ubd_dev[n].file = NULL;
  614. spin_unlock(&ubd_lock);
  615. return(err);
  616. }
  617. static int ubd_get_config(char *name, char *str, int size, char **error_out)
  618. {
  619. struct ubd *dev;
  620. int n, len = 0;
  621. n = parse_unit(&name);
  622. if((n >= MAX_DEV) || (n < 0)){
  623. *error_out = "ubd_get_config : device number out of range";
  624. return(-1);
  625. }
  626. dev = &ubd_dev[n];
  627. spin_lock(&ubd_lock);
  628. if(dev->file == NULL){
  629. CONFIG_CHUNK(str, size, len, "", 1);
  630. goto out;
  631. }
  632. CONFIG_CHUNK(str, size, len, dev->file, 0);
  633. if(dev->cow.file != NULL){
  634. CONFIG_CHUNK(str, size, len, ",", 0);
  635. CONFIG_CHUNK(str, size, len, dev->cow.file, 1);
  636. }
  637. else CONFIG_CHUNK(str, size, len, "", 1);
  638. out:
  639. spin_unlock(&ubd_lock);
  640. return(len);
  641. }
  642. static int ubd_id(char **str, int *start_out, int *end_out)
  643. {
  644. int n;
  645. n = parse_unit(str);
  646. *start_out = 0;
  647. *end_out = MAX_DEV - 1;
  648. return n;
  649. }
  650. static int ubd_remove(int n)
  651. {
  652. struct ubd *dev;
  653. int err = -ENODEV;
  654. spin_lock(&ubd_lock);
  655. if(ubd_gendisk[n] == NULL)
  656. goto out;
  657. dev = &ubd_dev[n];
  658. if(dev->file == NULL)
  659. goto out;
  660. /* you cannot remove a open disk */
  661. err = -EBUSY;
  662. if(dev->count > 0)
  663. goto out;
  664. del_gendisk(ubd_gendisk[n]);
  665. put_disk(ubd_gendisk[n]);
  666. ubd_gendisk[n] = NULL;
  667. if(fake_gendisk[n] != NULL){
  668. del_gendisk(fake_gendisk[n]);
  669. put_disk(fake_gendisk[n]);
  670. fake_gendisk[n] = NULL;
  671. }
  672. platform_device_unregister(&dev->pdev);
  673. *dev = ((struct ubd) DEFAULT_UBD);
  674. err = 0;
  675. out:
  676. spin_unlock(&ubd_lock);
  677. return err;
  678. }
  679. static struct mc_device ubd_mc = {
  680. .name = "ubd",
  681. .config = ubd_config,
  682. .get_config = ubd_get_config,
  683. .id = ubd_id,
  684. .remove = ubd_remove,
  685. };
  686. static int ubd_mc_init(void)
  687. {
  688. mconsole_register_dev(&ubd_mc);
  689. return 0;
  690. }
  691. __initcall(ubd_mc_init);
  692. static struct platform_driver ubd_driver = {
  693. .driver = {
  694. .name = DRIVER_NAME,
  695. },
  696. };
  697. int ubd_init(void)
  698. {
  699. int i;
  700. if (register_blkdev(MAJOR_NR, "ubd"))
  701. return -1;
  702. ubd_queue = blk_init_queue(do_ubd_request, &ubd_io_lock);
  703. if (!ubd_queue) {
  704. unregister_blkdev(MAJOR_NR, "ubd");
  705. return -1;
  706. }
  707. if (fake_major != MAJOR_NR) {
  708. char name[sizeof("ubd_nnn\0")];
  709. snprintf(name, sizeof(name), "ubd_%d", fake_major);
  710. if (register_blkdev(fake_major, "ubd"))
  711. return -1;
  712. }
  713. platform_driver_register(&ubd_driver);
  714. for (i = 0; i < MAX_DEV; i++)
  715. ubd_add(i);
  716. return 0;
  717. }
  718. late_initcall(ubd_init);
  719. int ubd_driver_init(void){
  720. unsigned long stack;
  721. int err;
  722. /* Set by CONFIG_BLK_DEV_UBD_SYNC or ubd=sync.*/
  723. if(global_openflags.s){
  724. printk(KERN_INFO "ubd: Synchronous mode\n");
  725. /* Letting ubd=sync be like using ubd#s= instead of ubd#= is
  726. * enough. So use anyway the io thread. */
  727. }
  728. stack = alloc_stack(0, 0);
  729. io_pid = start_io_thread(stack + PAGE_SIZE - sizeof(void *),
  730. &thread_fd);
  731. if(io_pid < 0){
  732. printk(KERN_ERR
  733. "ubd : Failed to start I/O thread (errno = %d) - "
  734. "falling back to synchronous I/O\n", -io_pid);
  735. io_pid = -1;
  736. return(0);
  737. }
  738. err = um_request_irq(UBD_IRQ, thread_fd, IRQ_READ, ubd_intr,
  739. IRQF_DISABLED, "ubd", ubd_dev);
  740. if(err != 0)
  741. printk(KERN_ERR "um_request_irq failed - errno = %d\n", -err);
  742. return 0;
  743. }
  744. device_initcall(ubd_driver_init);
  745. static int ubd_open(struct inode *inode, struct file *filp)
  746. {
  747. struct gendisk *disk = inode->i_bdev->bd_disk;
  748. struct ubd *dev = disk->private_data;
  749. int err = 0;
  750. if(dev->count == 0){
  751. err = ubd_open_dev(dev);
  752. if(err){
  753. printk(KERN_ERR "%s: Can't open \"%s\": errno = %d\n",
  754. disk->disk_name, dev->file, -err);
  755. goto out;
  756. }
  757. }
  758. dev->count++;
  759. set_disk_ro(disk, !dev->openflags.w);
  760. /* This should no more be needed. And it didn't work anyway to exclude
  761. * read-write remounting of filesystems.*/
  762. /*if((filp->f_mode & FMODE_WRITE) && !dev->openflags.w){
  763. if(--dev->count == 0) ubd_close(dev);
  764. err = -EROFS;
  765. }*/
  766. out:
  767. return(err);
  768. }
  769. static int ubd_release(struct inode * inode, struct file * file)
  770. {
  771. struct gendisk *disk = inode->i_bdev->bd_disk;
  772. struct ubd *dev = disk->private_data;
  773. if(--dev->count == 0)
  774. ubd_close(dev);
  775. return(0);
  776. }
  777. static void cowify_bitmap(__u64 io_offset, int length, unsigned long *cow_mask,
  778. __u64 *cow_offset, unsigned long *bitmap,
  779. __u64 bitmap_offset, unsigned long *bitmap_words,
  780. __u64 bitmap_len)
  781. {
  782. __u64 sector = io_offset >> 9;
  783. int i, update_bitmap = 0;
  784. for(i = 0; i < length >> 9; i++){
  785. if(cow_mask != NULL)
  786. ubd_set_bit(i, (unsigned char *) cow_mask);
  787. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  788. continue;
  789. update_bitmap = 1;
  790. ubd_set_bit(sector + i, (unsigned char *) bitmap);
  791. }
  792. if(!update_bitmap)
  793. return;
  794. *cow_offset = sector / (sizeof(unsigned long) * 8);
  795. /* This takes care of the case where we're exactly at the end of the
  796. * device, and *cow_offset + 1 is off the end. So, just back it up
  797. * by one word. Thanks to Lynn Kerby for the fix and James McMechan
  798. * for the original diagnosis.
  799. */
  800. if(*cow_offset == ((bitmap_len + sizeof(unsigned long) - 1) /
  801. sizeof(unsigned long) - 1))
  802. (*cow_offset)--;
  803. bitmap_words[0] = bitmap[*cow_offset];
  804. bitmap_words[1] = bitmap[*cow_offset + 1];
  805. *cow_offset *= sizeof(unsigned long);
  806. *cow_offset += bitmap_offset;
  807. }
  808. static void cowify_req(struct io_thread_req *req, unsigned long *bitmap,
  809. __u64 bitmap_offset, __u64 bitmap_len)
  810. {
  811. __u64 sector = req->offset >> 9;
  812. int i;
  813. if(req->length > (sizeof(req->sector_mask) * 8) << 9)
  814. panic("Operation too long");
  815. if(req->op == UBD_READ) {
  816. for(i = 0; i < req->length >> 9; i++){
  817. if(ubd_test_bit(sector + i, (unsigned char *) bitmap))
  818. ubd_set_bit(i, (unsigned char *)
  819. &req->sector_mask);
  820. }
  821. }
  822. else cowify_bitmap(req->offset, req->length, &req->sector_mask,
  823. &req->cow_offset, bitmap, bitmap_offset,
  824. req->bitmap_words, bitmap_len);
  825. }
  826. /* Called with ubd_io_lock held */
  827. static int prepare_request(struct request *req, struct io_thread_req *io_req)
  828. {
  829. struct gendisk *disk = req->rq_disk;
  830. struct ubd *dev = disk->private_data;
  831. __u64 offset;
  832. int len;
  833. if(req->rq_status == RQ_INACTIVE) return(1);
  834. /* This should be impossible now */
  835. if((rq_data_dir(req) == WRITE) && !dev->openflags.w){
  836. printk("Write attempted on readonly ubd device %s\n",
  837. disk->disk_name);
  838. end_request(req, 0);
  839. return(1);
  840. }
  841. offset = ((__u64) req->sector) << 9;
  842. len = req->current_nr_sectors << 9;
  843. io_req->fds[0] = (dev->cow.file != NULL) ? dev->cow.fd : dev->fd;
  844. io_req->fds[1] = dev->fd;
  845. io_req->cow_offset = -1;
  846. io_req->offset = offset;
  847. io_req->length = len;
  848. io_req->error = 0;
  849. io_req->sector_mask = 0;
  850. io_req->op = (rq_data_dir(req) == READ) ? UBD_READ : UBD_WRITE;
  851. io_req->offsets[0] = 0;
  852. io_req->offsets[1] = dev->cow.data_offset;
  853. io_req->buffer = req->buffer;
  854. io_req->sectorsize = 1 << 9;
  855. if(dev->cow.file != NULL)
  856. cowify_req(io_req, dev->cow.bitmap, dev->cow.bitmap_offset,
  857. dev->cow.bitmap_len);
  858. return(0);
  859. }
  860. /* Called with ubd_io_lock held */
  861. static void do_ubd_request(request_queue_t *q)
  862. {
  863. struct io_thread_req io_req;
  864. struct request *req;
  865. int err, n;
  866. if(thread_fd == -1){
  867. while((req = elv_next_request(q)) != NULL){
  868. err = prepare_request(req, &io_req);
  869. if(!err){
  870. do_io(&io_req);
  871. __ubd_finish(req, io_req.error);
  872. }
  873. }
  874. }
  875. else {
  876. if(do_ubd || (req = elv_next_request(q)) == NULL)
  877. return;
  878. err = prepare_request(req, &io_req);
  879. if(!err){
  880. do_ubd = ubd_handler;
  881. n = os_write_file(thread_fd, (char *) &io_req,
  882. sizeof(io_req));
  883. if(n != sizeof(io_req))
  884. printk("write to io thread failed, "
  885. "errno = %d\n", -n);
  886. }
  887. }
  888. }
  889. static int ubd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
  890. {
  891. struct ubd *dev = bdev->bd_disk->private_data;
  892. geo->heads = 128;
  893. geo->sectors = 32;
  894. geo->cylinders = dev->size / (128 * 32 * 512);
  895. return 0;
  896. }
  897. static int ubd_ioctl(struct inode * inode, struct file * file,
  898. unsigned int cmd, unsigned long arg)
  899. {
  900. struct ubd *dev = inode->i_bdev->bd_disk->private_data;
  901. struct hd_driveid ubd_id = {
  902. .cyls = 0,
  903. .heads = 128,
  904. .sectors = 32,
  905. };
  906. switch (cmd) {
  907. struct cdrom_volctrl volume;
  908. case HDIO_GET_IDENTITY:
  909. ubd_id.cyls = dev->size / (128 * 32 * 512);
  910. if(copy_to_user((char __user *) arg, (char *) &ubd_id,
  911. sizeof(ubd_id)))
  912. return(-EFAULT);
  913. return(0);
  914. case CDROMVOLREAD:
  915. if(copy_from_user(&volume, (char __user *) arg, sizeof(volume)))
  916. return(-EFAULT);
  917. volume.channel0 = 255;
  918. volume.channel1 = 255;
  919. volume.channel2 = 255;
  920. volume.channel3 = 255;
  921. if(copy_to_user((char __user *) arg, &volume, sizeof(volume)))
  922. return(-EFAULT);
  923. return(0);
  924. }
  925. return(-EINVAL);
  926. }
  927. static int path_requires_switch(char *from_cmdline, char *from_cow, char *cow)
  928. {
  929. struct uml_stat buf1, buf2;
  930. int err;
  931. if(from_cmdline == NULL)
  932. return 0;
  933. if(!strcmp(from_cmdline, from_cow))
  934. return 0;
  935. err = os_stat_file(from_cmdline, &buf1);
  936. if(err < 0){
  937. printk("Couldn't stat '%s', err = %d\n", from_cmdline, -err);
  938. return 0;
  939. }
  940. err = os_stat_file(from_cow, &buf2);
  941. if(err < 0){
  942. printk("Couldn't stat '%s', err = %d\n", from_cow, -err);
  943. return 1;
  944. }
  945. if((buf1.ust_dev == buf2.ust_dev) && (buf1.ust_ino == buf2.ust_ino))
  946. return 0;
  947. printk("Backing file mismatch - \"%s\" requested,\n"
  948. "\"%s\" specified in COW header of \"%s\"\n",
  949. from_cmdline, from_cow, cow);
  950. return 1;
  951. }
  952. static int backing_file_mismatch(char *file, __u64 size, time_t mtime)
  953. {
  954. unsigned long modtime;
  955. unsigned long long actual;
  956. int err;
  957. err = os_file_modtime(file, &modtime);
  958. if(err < 0){
  959. printk("Failed to get modification time of backing file "
  960. "\"%s\", err = %d\n", file, -err);
  961. return(err);
  962. }
  963. err = os_file_size(file, &actual);
  964. if(err < 0){
  965. printk("Failed to get size of backing file \"%s\", "
  966. "err = %d\n", file, -err);
  967. return(err);
  968. }
  969. if(actual != size){
  970. /*__u64 can be a long on AMD64 and with %lu GCC complains; so
  971. * the typecast.*/
  972. printk("Size mismatch (%llu vs %llu) of COW header vs backing "
  973. "file\n", (unsigned long long) size, actual);
  974. return(-EINVAL);
  975. }
  976. if(modtime != mtime){
  977. printk("mtime mismatch (%ld vs %ld) of COW header vs backing "
  978. "file\n", mtime, modtime);
  979. return(-EINVAL);
  980. }
  981. return(0);
  982. }
  983. int read_cow_bitmap(int fd, void *buf, int offset, int len)
  984. {
  985. int err;
  986. err = os_seek_file(fd, offset);
  987. if(err < 0)
  988. return(err);
  989. err = os_read_file(fd, buf, len);
  990. if(err < 0)
  991. return(err);
  992. return(0);
  993. }
  994. int open_ubd_file(char *file, struct openflags *openflags, int shared,
  995. char **backing_file_out, int *bitmap_offset_out,
  996. unsigned long *bitmap_len_out, int *data_offset_out,
  997. int *create_cow_out)
  998. {
  999. time_t mtime;
  1000. unsigned long long size;
  1001. __u32 version, align;
  1002. char *backing_file;
  1003. int fd, err, sectorsize, asked_switch, mode = 0644;
  1004. fd = os_open_file(file, *openflags, mode);
  1005. if (fd < 0) {
  1006. if ((fd == -ENOENT) && (create_cow_out != NULL))
  1007. *create_cow_out = 1;
  1008. if (!openflags->w ||
  1009. ((fd != -EROFS) && (fd != -EACCES)))
  1010. return fd;
  1011. openflags->w = 0;
  1012. fd = os_open_file(file, *openflags, mode);
  1013. if (fd < 0)
  1014. return fd;
  1015. }
  1016. if(shared)
  1017. printk("Not locking \"%s\" on the host\n", file);
  1018. else {
  1019. err = os_lock_file(fd, openflags->w);
  1020. if(err < 0){
  1021. printk("Failed to lock '%s', err = %d\n", file, -err);
  1022. goto out_close;
  1023. }
  1024. }
  1025. /* Successful return case! */
  1026. if(backing_file_out == NULL)
  1027. return(fd);
  1028. err = read_cow_header(file_reader, &fd, &version, &backing_file, &mtime,
  1029. &size, &sectorsize, &align, bitmap_offset_out);
  1030. if(err && (*backing_file_out != NULL)){
  1031. printk("Failed to read COW header from COW file \"%s\", "
  1032. "errno = %d\n", file, -err);
  1033. goto out_close;
  1034. }
  1035. if(err)
  1036. return(fd);
  1037. asked_switch = path_requires_switch(*backing_file_out, backing_file, file);
  1038. /* Allow switching only if no mismatch. */
  1039. if (asked_switch && !backing_file_mismatch(*backing_file_out, size, mtime)) {
  1040. printk("Switching backing file to '%s'\n", *backing_file_out);
  1041. err = write_cow_header(file, fd, *backing_file_out,
  1042. sectorsize, align, &size);
  1043. if (err) {
  1044. printk("Switch failed, errno = %d\n", -err);
  1045. goto out_close;
  1046. }
  1047. } else {
  1048. *backing_file_out = backing_file;
  1049. err = backing_file_mismatch(*backing_file_out, size, mtime);
  1050. if (err)
  1051. goto out_close;
  1052. }
  1053. cow_sizes(version, size, sectorsize, align, *bitmap_offset_out,
  1054. bitmap_len_out, data_offset_out);
  1055. return fd;
  1056. out_close:
  1057. os_close_file(fd);
  1058. return err;
  1059. }
  1060. int create_cow_file(char *cow_file, char *backing_file, struct openflags flags,
  1061. int sectorsize, int alignment, int *bitmap_offset_out,
  1062. unsigned long *bitmap_len_out, int *data_offset_out)
  1063. {
  1064. int err, fd;
  1065. flags.c = 1;
  1066. fd = open_ubd_file(cow_file, &flags, 0, NULL, NULL, NULL, NULL, NULL);
  1067. if(fd < 0){
  1068. err = fd;
  1069. printk("Open of COW file '%s' failed, errno = %d\n", cow_file,
  1070. -err);
  1071. goto out;
  1072. }
  1073. err = init_cow_file(fd, cow_file, backing_file, sectorsize, alignment,
  1074. bitmap_offset_out, bitmap_len_out,
  1075. data_offset_out);
  1076. if(!err)
  1077. return(fd);
  1078. os_close_file(fd);
  1079. out:
  1080. return(err);
  1081. }
  1082. static int update_bitmap(struct io_thread_req *req)
  1083. {
  1084. int n;
  1085. if(req->cow_offset == -1)
  1086. return(0);
  1087. n = os_seek_file(req->fds[1], req->cow_offset);
  1088. if(n < 0){
  1089. printk("do_io - bitmap lseek failed : err = %d\n", -n);
  1090. return(1);
  1091. }
  1092. n = os_write_file(req->fds[1], &req->bitmap_words,
  1093. sizeof(req->bitmap_words));
  1094. if(n != sizeof(req->bitmap_words)){
  1095. printk("do_io - bitmap update failed, err = %d fd = %d\n", -n,
  1096. req->fds[1]);
  1097. return(1);
  1098. }
  1099. return(0);
  1100. }
  1101. void do_io(struct io_thread_req *req)
  1102. {
  1103. char *buf;
  1104. unsigned long len;
  1105. int n, nsectors, start, end, bit;
  1106. int err;
  1107. __u64 off;
  1108. nsectors = req->length / req->sectorsize;
  1109. start = 0;
  1110. do {
  1111. bit = ubd_test_bit(start, (unsigned char *) &req->sector_mask);
  1112. end = start;
  1113. while((end < nsectors) &&
  1114. (ubd_test_bit(end, (unsigned char *)
  1115. &req->sector_mask) == bit))
  1116. end++;
  1117. off = req->offset + req->offsets[bit] +
  1118. start * req->sectorsize;
  1119. len = (end - start) * req->sectorsize;
  1120. buf = &req->buffer[start * req->sectorsize];
  1121. err = os_seek_file(req->fds[bit], off);
  1122. if(err < 0){
  1123. printk("do_io - lseek failed : err = %d\n", -err);
  1124. req->error = 1;
  1125. return;
  1126. }
  1127. if(req->op == UBD_READ){
  1128. n = 0;
  1129. do {
  1130. buf = &buf[n];
  1131. len -= n;
  1132. n = os_read_file(req->fds[bit], buf, len);
  1133. if (n < 0) {
  1134. printk("do_io - read failed, err = %d "
  1135. "fd = %d\n", -n, req->fds[bit]);
  1136. req->error = 1;
  1137. return;
  1138. }
  1139. } while((n < len) && (n != 0));
  1140. if (n < len) memset(&buf[n], 0, len - n);
  1141. } else {
  1142. n = os_write_file(req->fds[bit], buf, len);
  1143. if(n != len){
  1144. printk("do_io - write failed err = %d "
  1145. "fd = %d\n", -n, req->fds[bit]);
  1146. req->error = 1;
  1147. return;
  1148. }
  1149. }
  1150. start = end;
  1151. } while(start < nsectors);
  1152. req->error = update_bitmap(req);
  1153. }
  1154. /* Changed in start_io_thread, which is serialized by being called only
  1155. * from ubd_init, which is an initcall.
  1156. */
  1157. int kernel_fd = -1;
  1158. /* Only changed by the io thread */
  1159. int io_count = 0;
  1160. int io_thread(void *arg)
  1161. {
  1162. struct io_thread_req req;
  1163. int n;
  1164. ignore_sigwinch_sig();
  1165. while(1){
  1166. n = os_read_file(kernel_fd, &req, sizeof(req));
  1167. if(n != sizeof(req)){
  1168. if(n < 0)
  1169. printk("io_thread - read failed, fd = %d, "
  1170. "err = %d\n", kernel_fd, -n);
  1171. else {
  1172. printk("io_thread - short read, fd = %d, "
  1173. "length = %d\n", kernel_fd, n);
  1174. }
  1175. continue;
  1176. }
  1177. io_count++;
  1178. do_io(&req);
  1179. n = os_write_file(kernel_fd, &req, sizeof(req));
  1180. if(n != sizeof(req))
  1181. printk("io_thread - write failed, fd = %d, err = %d\n",
  1182. kernel_fd, -n);
  1183. }
  1184. return 0;
  1185. }