dm-log-userspace-base.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  1. /*
  2. * Copyright (C) 2006-2009 Red Hat, Inc.
  3. *
  4. * This file is released under the LGPL.
  5. */
  6. #include <linux/bio.h>
  7. #include <linux/slab.h>
  8. #include <linux/dm-dirty-log.h>
  9. #include <linux/device-mapper.h>
  10. #include <linux/dm-log-userspace.h>
  11. #include "dm-log-userspace-transfer.h"
  12. #define DM_LOG_USERSPACE_VSN "1.1.0"
  13. struct flush_entry {
  14. int type;
  15. region_t region;
  16. struct list_head list;
  17. };
  18. /*
  19. * This limit on the number of mark and clear request is, to a degree,
  20. * arbitrary. However, there is some basis for the choice in the limits
  21. * imposed on the size of data payload by dm-log-userspace-transfer.c:
  22. * dm_consult_userspace().
  23. */
  24. #define MAX_FLUSH_GROUP_COUNT 32
  25. struct log_c {
  26. struct dm_target *ti;
  27. struct dm_dev *log_dev;
  28. uint32_t region_size;
  29. region_t region_count;
  30. uint64_t luid;
  31. char uuid[DM_UUID_LEN];
  32. char *usr_argv_str;
  33. uint32_t usr_argc;
  34. /*
  35. * in_sync_hint gets set when doing is_remote_recovering. It
  36. * represents the first region that needs recovery. IOW, the
  37. * first zero bit of sync_bits. This can be useful for to limit
  38. * traffic for calls like is_remote_recovering and get_resync_work,
  39. * but be take care in its use for anything else.
  40. */
  41. uint64_t in_sync_hint;
  42. /*
  43. * Mark and clear requests are held until a flush is issued
  44. * so that we can group, and thereby limit, the amount of
  45. * network traffic between kernel and userspace. The 'flush_lock'
  46. * is used to protect these lists.
  47. */
  48. spinlock_t flush_lock;
  49. struct list_head mark_list;
  50. struct list_head clear_list;
  51. };
  52. static mempool_t *flush_entry_pool;
  53. static void *flush_entry_alloc(gfp_t gfp_mask, void *pool_data)
  54. {
  55. return kmalloc(sizeof(struct flush_entry), gfp_mask);
  56. }
  57. static void flush_entry_free(void *element, void *pool_data)
  58. {
  59. kfree(element);
  60. }
  61. static int userspace_do_request(struct log_c *lc, const char *uuid,
  62. int request_type, char *data, size_t data_size,
  63. char *rdata, size_t *rdata_size)
  64. {
  65. int r;
  66. /*
  67. * If the server isn't there, -ESRCH is returned,
  68. * and we must keep trying until the server is
  69. * restored.
  70. */
  71. retry:
  72. r = dm_consult_userspace(uuid, lc->luid, request_type, data,
  73. data_size, rdata, rdata_size);
  74. if (r != -ESRCH)
  75. return r;
  76. DMERR(" Userspace log server not found.");
  77. while (1) {
  78. set_current_state(TASK_INTERRUPTIBLE);
  79. schedule_timeout(2*HZ);
  80. DMWARN("Attempting to contact userspace log server...");
  81. r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
  82. lc->usr_argv_str,
  83. strlen(lc->usr_argv_str) + 1,
  84. NULL, NULL);
  85. if (!r)
  86. break;
  87. }
  88. DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
  89. r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
  90. 0, NULL, NULL);
  91. if (!r)
  92. goto retry;
  93. DMERR("Error trying to resume userspace log: %d", r);
  94. return -ESRCH;
  95. }
  96. static int build_constructor_string(struct dm_target *ti,
  97. unsigned argc, char **argv,
  98. char **ctr_str)
  99. {
  100. int i, str_size;
  101. char *str = NULL;
  102. *ctr_str = NULL;
  103. for (i = 0, str_size = 0; i < argc; i++)
  104. str_size += strlen(argv[i]) + 1; /* +1 for space between args */
  105. str_size += 20; /* Max number of chars in a printed u64 number */
  106. str = kzalloc(str_size, GFP_KERNEL);
  107. if (!str) {
  108. DMWARN("Unable to allocate memory for constructor string");
  109. return -ENOMEM;
  110. }
  111. str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
  112. for (i = 0; i < argc; i++)
  113. str_size += sprintf(str + str_size, " %s", argv[i]);
  114. *ctr_str = str;
  115. return str_size;
  116. }
  117. /*
  118. * userspace_ctr
  119. *
  120. * argv contains:
  121. * <UUID> <other args>
  122. * Where 'other args' is the userspace implementation specific log
  123. * arguments. An example might be:
  124. * <UUID> clustered-disk <arg count> <log dev> <region_size> [[no]sync]
  125. *
  126. * So, this module will strip off the <UUID> for identification purposes
  127. * when communicating with userspace about a log; but will pass on everything
  128. * else.
  129. */
  130. static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
  131. unsigned argc, char **argv)
  132. {
  133. int r = 0;
  134. int str_size;
  135. char *ctr_str = NULL;
  136. struct log_c *lc = NULL;
  137. uint64_t rdata;
  138. size_t rdata_size = sizeof(rdata);
  139. char *devices_rdata = NULL;
  140. size_t devices_rdata_size = DM_NAME_LEN;
  141. if (argc < 3) {
  142. DMWARN("Too few arguments to userspace dirty log");
  143. return -EINVAL;
  144. }
  145. lc = kzalloc(sizeof(*lc), GFP_KERNEL);
  146. if (!lc) {
  147. DMWARN("Unable to allocate userspace log context.");
  148. return -ENOMEM;
  149. }
  150. /* The ptr value is sufficient for local unique id */
  151. lc->luid = (unsigned long)lc;
  152. lc->ti = ti;
  153. if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
  154. DMWARN("UUID argument too long.");
  155. kfree(lc);
  156. return -EINVAL;
  157. }
  158. strncpy(lc->uuid, argv[0], DM_UUID_LEN);
  159. spin_lock_init(&lc->flush_lock);
  160. INIT_LIST_HEAD(&lc->mark_list);
  161. INIT_LIST_HEAD(&lc->clear_list);
  162. str_size = build_constructor_string(ti, argc - 1, argv + 1, &ctr_str);
  163. if (str_size < 0) {
  164. kfree(lc);
  165. return str_size;
  166. }
  167. devices_rdata = kzalloc(devices_rdata_size, GFP_KERNEL);
  168. if (!devices_rdata) {
  169. DMERR("Failed to allocate memory for device information");
  170. r = -ENOMEM;
  171. goto out;
  172. }
  173. /*
  174. * Send table string and get back any opened device.
  175. */
  176. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
  177. ctr_str, str_size,
  178. devices_rdata, &devices_rdata_size);
  179. if (r < 0) {
  180. if (r == -ESRCH)
  181. DMERR("Userspace log server not found");
  182. else
  183. DMERR("Userspace log server failed to create log");
  184. goto out;
  185. }
  186. /* Since the region size does not change, get it now */
  187. rdata_size = sizeof(rdata);
  188. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
  189. NULL, 0, (char *)&rdata, &rdata_size);
  190. if (r) {
  191. DMERR("Failed to get region size of dirty log");
  192. goto out;
  193. }
  194. lc->region_size = (uint32_t)rdata;
  195. lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
  196. if (devices_rdata_size) {
  197. if (devices_rdata[devices_rdata_size - 1] != '\0') {
  198. DMERR("DM_ULOG_CTR device return string not properly terminated");
  199. r = -EINVAL;
  200. goto out;
  201. }
  202. r = dm_get_device(ti, devices_rdata,
  203. dm_table_get_mode(ti->table), &lc->log_dev);
  204. if (r)
  205. DMERR("Failed to register %s with device-mapper",
  206. devices_rdata);
  207. }
  208. out:
  209. kfree(devices_rdata);
  210. if (r) {
  211. kfree(lc);
  212. kfree(ctr_str);
  213. } else {
  214. lc->usr_argv_str = ctr_str;
  215. lc->usr_argc = argc;
  216. log->context = lc;
  217. }
  218. return r;
  219. }
  220. static void userspace_dtr(struct dm_dirty_log *log)
  221. {
  222. struct log_c *lc = log->context;
  223. (void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
  224. NULL, 0,
  225. NULL, NULL);
  226. if (lc->log_dev)
  227. dm_put_device(lc->ti, lc->log_dev);
  228. kfree(lc->usr_argv_str);
  229. kfree(lc);
  230. return;
  231. }
  232. static int userspace_presuspend(struct dm_dirty_log *log)
  233. {
  234. int r;
  235. struct log_c *lc = log->context;
  236. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
  237. NULL, 0,
  238. NULL, NULL);
  239. return r;
  240. }
  241. static int userspace_postsuspend(struct dm_dirty_log *log)
  242. {
  243. int r;
  244. struct log_c *lc = log->context;
  245. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
  246. NULL, 0,
  247. NULL, NULL);
  248. return r;
  249. }
  250. static int userspace_resume(struct dm_dirty_log *log)
  251. {
  252. int r;
  253. struct log_c *lc = log->context;
  254. lc->in_sync_hint = 0;
  255. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
  256. NULL, 0,
  257. NULL, NULL);
  258. return r;
  259. }
  260. static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
  261. {
  262. struct log_c *lc = log->context;
  263. return lc->region_size;
  264. }
  265. /*
  266. * userspace_is_clean
  267. *
  268. * Check whether a region is clean. If there is any sort of
  269. * failure when consulting the server, we return not clean.
  270. *
  271. * Returns: 1 if clean, 0 otherwise
  272. */
  273. static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
  274. {
  275. int r;
  276. uint64_t region64 = (uint64_t)region;
  277. int64_t is_clean;
  278. size_t rdata_size;
  279. struct log_c *lc = log->context;
  280. rdata_size = sizeof(is_clean);
  281. r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
  282. (char *)&region64, sizeof(region64),
  283. (char *)&is_clean, &rdata_size);
  284. return (r) ? 0 : (int)is_clean;
  285. }
  286. /*
  287. * userspace_in_sync
  288. *
  289. * Check if the region is in-sync. If there is any sort
  290. * of failure when consulting the server, we assume that
  291. * the region is not in sync.
  292. *
  293. * If 'can_block' is set, return immediately
  294. *
  295. * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
  296. */
  297. static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
  298. int can_block)
  299. {
  300. int r;
  301. uint64_t region64 = region;
  302. int64_t in_sync;
  303. size_t rdata_size;
  304. struct log_c *lc = log->context;
  305. /*
  306. * We can never respond directly - even if in_sync_hint is
  307. * set. This is because another machine could see a device
  308. * failure and mark the region out-of-sync. If we don't go
  309. * to userspace to ask, we might think the region is in-sync
  310. * and allow a read to pick up data that is stale. (This is
  311. * very unlikely if a device actually fails; but it is very
  312. * likely if a connection to one device from one machine fails.)
  313. *
  314. * There still might be a problem if the mirror caches the region
  315. * state as in-sync... but then this call would not be made. So,
  316. * that is a mirror problem.
  317. */
  318. if (!can_block)
  319. return -EWOULDBLOCK;
  320. rdata_size = sizeof(in_sync);
  321. r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
  322. (char *)&region64, sizeof(region64),
  323. (char *)&in_sync, &rdata_size);
  324. return (r) ? 0 : (int)in_sync;
  325. }
  326. static int flush_one_by_one(struct log_c *lc, struct list_head *flush_list)
  327. {
  328. int r = 0;
  329. struct flush_entry *fe;
  330. list_for_each_entry(fe, flush_list, list) {
  331. r = userspace_do_request(lc, lc->uuid, fe->type,
  332. (char *)&fe->region,
  333. sizeof(fe->region),
  334. NULL, NULL);
  335. if (r)
  336. break;
  337. }
  338. return r;
  339. }
  340. static int flush_by_group(struct log_c *lc, struct list_head *flush_list)
  341. {
  342. int r = 0;
  343. int count;
  344. uint32_t type = 0;
  345. struct flush_entry *fe, *tmp_fe;
  346. LIST_HEAD(tmp_list);
  347. uint64_t group[MAX_FLUSH_GROUP_COUNT];
  348. /*
  349. * Group process the requests
  350. */
  351. while (!list_empty(flush_list)) {
  352. count = 0;
  353. list_for_each_entry_safe(fe, tmp_fe, flush_list, list) {
  354. group[count] = fe->region;
  355. count++;
  356. list_move(&fe->list, &tmp_list);
  357. type = fe->type;
  358. if (count >= MAX_FLUSH_GROUP_COUNT)
  359. break;
  360. }
  361. r = userspace_do_request(lc, lc->uuid, type,
  362. (char *)(group),
  363. count * sizeof(uint64_t),
  364. NULL, NULL);
  365. if (r) {
  366. /* Group send failed. Attempt one-by-one. */
  367. list_splice_init(&tmp_list, flush_list);
  368. r = flush_one_by_one(lc, flush_list);
  369. break;
  370. }
  371. }
  372. /*
  373. * Must collect flush_entrys that were successfully processed
  374. * as a group so that they will be free'd by the caller.
  375. */
  376. list_splice_init(&tmp_list, flush_list);
  377. return r;
  378. }
  379. /*
  380. * userspace_flush
  381. *
  382. * This function is ok to block.
  383. * The flush happens in two stages. First, it sends all
  384. * clear/mark requests that are on the list. Then it
  385. * tells the server to commit them. This gives the
  386. * server a chance to optimise the commit, instead of
  387. * doing it for every request.
  388. *
  389. * Additionally, we could implement another thread that
  390. * sends the requests up to the server - reducing the
  391. * load on flush. Then the flush would have less in
  392. * the list and be responsible for the finishing commit.
  393. *
  394. * Returns: 0 on success, < 0 on failure
  395. */
  396. static int userspace_flush(struct dm_dirty_log *log)
  397. {
  398. int r = 0;
  399. unsigned long flags;
  400. struct log_c *lc = log->context;
  401. LIST_HEAD(mark_list);
  402. LIST_HEAD(clear_list);
  403. struct flush_entry *fe, *tmp_fe;
  404. spin_lock_irqsave(&lc->flush_lock, flags);
  405. list_splice_init(&lc->mark_list, &mark_list);
  406. list_splice_init(&lc->clear_list, &clear_list);
  407. spin_unlock_irqrestore(&lc->flush_lock, flags);
  408. if (list_empty(&mark_list) && list_empty(&clear_list))
  409. return 0;
  410. r = flush_by_group(lc, &mark_list);
  411. if (r)
  412. goto fail;
  413. r = flush_by_group(lc, &clear_list);
  414. if (r)
  415. goto fail;
  416. r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
  417. NULL, 0, NULL, NULL);
  418. fail:
  419. /*
  420. * We can safely remove these entries, even if failure.
  421. * Calling code will receive an error and will know that
  422. * the log facility has failed.
  423. */
  424. list_for_each_entry_safe(fe, tmp_fe, &mark_list, list) {
  425. list_del(&fe->list);
  426. mempool_free(fe, flush_entry_pool);
  427. }
  428. list_for_each_entry_safe(fe, tmp_fe, &clear_list, list) {
  429. list_del(&fe->list);
  430. mempool_free(fe, flush_entry_pool);
  431. }
  432. if (r)
  433. dm_table_event(lc->ti->table);
  434. return r;
  435. }
  436. /*
  437. * userspace_mark_region
  438. *
  439. * This function should avoid blocking unless absolutely required.
  440. * (Memory allocation is valid for blocking.)
  441. */
  442. static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
  443. {
  444. unsigned long flags;
  445. struct log_c *lc = log->context;
  446. struct flush_entry *fe;
  447. /* Wait for an allocation, but _never_ fail */
  448. fe = mempool_alloc(flush_entry_pool, GFP_NOIO);
  449. BUG_ON(!fe);
  450. spin_lock_irqsave(&lc->flush_lock, flags);
  451. fe->type = DM_ULOG_MARK_REGION;
  452. fe->region = region;
  453. list_add(&fe->list, &lc->mark_list);
  454. spin_unlock_irqrestore(&lc->flush_lock, flags);
  455. return;
  456. }
  457. /*
  458. * userspace_clear_region
  459. *
  460. * This function must not block.
  461. * So, the alloc can't block. In the worst case, it is ok to
  462. * fail. It would simply mean we can't clear the region.
  463. * Does nothing to current sync context, but does mean
  464. * the region will be re-sync'ed on a reload of the mirror
  465. * even though it is in-sync.
  466. */
  467. static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
  468. {
  469. unsigned long flags;
  470. struct log_c *lc = log->context;
  471. struct flush_entry *fe;
  472. /*
  473. * If we fail to allocate, we skip the clearing of
  474. * the region. This doesn't hurt us in any way, except
  475. * to cause the region to be resync'ed when the
  476. * device is activated next time.
  477. */
  478. fe = mempool_alloc(flush_entry_pool, GFP_ATOMIC);
  479. if (!fe) {
  480. DMERR("Failed to allocate memory to clear region.");
  481. return;
  482. }
  483. spin_lock_irqsave(&lc->flush_lock, flags);
  484. fe->type = DM_ULOG_CLEAR_REGION;
  485. fe->region = region;
  486. list_add(&fe->list, &lc->clear_list);
  487. spin_unlock_irqrestore(&lc->flush_lock, flags);
  488. return;
  489. }
  490. /*
  491. * userspace_get_resync_work
  492. *
  493. * Get a region that needs recovery. It is valid to return
  494. * an error for this function.
  495. *
  496. * Returns: 1 if region filled, 0 if no work, <0 on error
  497. */
  498. static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
  499. {
  500. int r;
  501. size_t rdata_size;
  502. struct log_c *lc = log->context;
  503. struct {
  504. int64_t i; /* 64-bit for mix arch compatibility */
  505. region_t r;
  506. } pkg;
  507. if (lc->in_sync_hint >= lc->region_count)
  508. return 0;
  509. rdata_size = sizeof(pkg);
  510. r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
  511. NULL, 0,
  512. (char *)&pkg, &rdata_size);
  513. *region = pkg.r;
  514. return (r) ? r : (int)pkg.i;
  515. }
  516. /*
  517. * userspace_set_region_sync
  518. *
  519. * Set the sync status of a given region. This function
  520. * must not fail.
  521. */
  522. static void userspace_set_region_sync(struct dm_dirty_log *log,
  523. region_t region, int in_sync)
  524. {
  525. int r;
  526. struct log_c *lc = log->context;
  527. struct {
  528. region_t r;
  529. int64_t i;
  530. } pkg;
  531. pkg.r = region;
  532. pkg.i = (int64_t)in_sync;
  533. r = userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
  534. (char *)&pkg, sizeof(pkg),
  535. NULL, NULL);
  536. /*
  537. * It would be nice to be able to report failures.
  538. * However, it is easy emough to detect and resolve.
  539. */
  540. return;
  541. }
  542. /*
  543. * userspace_get_sync_count
  544. *
  545. * If there is any sort of failure when consulting the server,
  546. * we assume that the sync count is zero.
  547. *
  548. * Returns: sync count on success, 0 on failure
  549. */
  550. static region_t userspace_get_sync_count(struct dm_dirty_log *log)
  551. {
  552. int r;
  553. size_t rdata_size;
  554. uint64_t sync_count;
  555. struct log_c *lc = log->context;
  556. rdata_size = sizeof(sync_count);
  557. r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
  558. NULL, 0,
  559. (char *)&sync_count, &rdata_size);
  560. if (r)
  561. return 0;
  562. if (sync_count >= lc->region_count)
  563. lc->in_sync_hint = lc->region_count;
  564. return (region_t)sync_count;
  565. }
  566. /*
  567. * userspace_status
  568. *
  569. * Returns: amount of space consumed
  570. */
  571. static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
  572. char *result, unsigned maxlen)
  573. {
  574. int r = 0;
  575. char *table_args;
  576. size_t sz = (size_t)maxlen;
  577. struct log_c *lc = log->context;
  578. switch (status_type) {
  579. case STATUSTYPE_INFO:
  580. r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
  581. NULL, 0,
  582. result, &sz);
  583. if (r) {
  584. sz = 0;
  585. DMEMIT("%s 1 COM_FAILURE", log->type->name);
  586. }
  587. break;
  588. case STATUSTYPE_TABLE:
  589. sz = 0;
  590. table_args = strchr(lc->usr_argv_str, ' ');
  591. BUG_ON(!table_args); /* There will always be a ' ' */
  592. table_args++;
  593. DMEMIT("%s %u %s %s ", log->type->name, lc->usr_argc,
  594. lc->uuid, table_args);
  595. break;
  596. }
  597. return (r) ? 0 : (int)sz;
  598. }
  599. /*
  600. * userspace_is_remote_recovering
  601. *
  602. * Returns: 1 if region recovering, 0 otherwise
  603. */
  604. static int userspace_is_remote_recovering(struct dm_dirty_log *log,
  605. region_t region)
  606. {
  607. int r;
  608. uint64_t region64 = region;
  609. struct log_c *lc = log->context;
  610. static unsigned long long limit;
  611. struct {
  612. int64_t is_recovering;
  613. uint64_t in_sync_hint;
  614. } pkg;
  615. size_t rdata_size = sizeof(pkg);
  616. /*
  617. * Once the mirror has been reported to be in-sync,
  618. * it will never again ask for recovery work. So,
  619. * we can safely say there is not a remote machine
  620. * recovering if the device is in-sync. (in_sync_hint
  621. * must be reset at resume time.)
  622. */
  623. if (region < lc->in_sync_hint)
  624. return 0;
  625. else if (jiffies < limit)
  626. return 1;
  627. limit = jiffies + (HZ / 4);
  628. r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
  629. (char *)&region64, sizeof(region64),
  630. (char *)&pkg, &rdata_size);
  631. if (r)
  632. return 1;
  633. lc->in_sync_hint = pkg.in_sync_hint;
  634. return (int)pkg.is_recovering;
  635. }
  636. static struct dm_dirty_log_type _userspace_type = {
  637. .name = "userspace",
  638. .module = THIS_MODULE,
  639. .ctr = userspace_ctr,
  640. .dtr = userspace_dtr,
  641. .presuspend = userspace_presuspend,
  642. .postsuspend = userspace_postsuspend,
  643. .resume = userspace_resume,
  644. .get_region_size = userspace_get_region_size,
  645. .is_clean = userspace_is_clean,
  646. .in_sync = userspace_in_sync,
  647. .flush = userspace_flush,
  648. .mark_region = userspace_mark_region,
  649. .clear_region = userspace_clear_region,
  650. .get_resync_work = userspace_get_resync_work,
  651. .set_region_sync = userspace_set_region_sync,
  652. .get_sync_count = userspace_get_sync_count,
  653. .status = userspace_status,
  654. .is_remote_recovering = userspace_is_remote_recovering,
  655. };
  656. static int __init userspace_dirty_log_init(void)
  657. {
  658. int r = 0;
  659. flush_entry_pool = mempool_create(100, flush_entry_alloc,
  660. flush_entry_free, NULL);
  661. if (!flush_entry_pool) {
  662. DMWARN("Unable to create flush_entry_pool: No memory.");
  663. return -ENOMEM;
  664. }
  665. r = dm_ulog_tfr_init();
  666. if (r) {
  667. DMWARN("Unable to initialize userspace log communications");
  668. mempool_destroy(flush_entry_pool);
  669. return r;
  670. }
  671. r = dm_dirty_log_type_register(&_userspace_type);
  672. if (r) {
  673. DMWARN("Couldn't register userspace dirty log type");
  674. dm_ulog_tfr_exit();
  675. mempool_destroy(flush_entry_pool);
  676. return r;
  677. }
  678. DMINFO("version " DM_LOG_USERSPACE_VSN " loaded");
  679. return 0;
  680. }
  681. static void __exit userspace_dirty_log_exit(void)
  682. {
  683. dm_dirty_log_type_unregister(&_userspace_type);
  684. dm_ulog_tfr_exit();
  685. mempool_destroy(flush_entry_pool);
  686. DMINFO("version " DM_LOG_USERSPACE_VSN " unloaded");
  687. return;
  688. }
  689. module_init(userspace_dirty_log_init);
  690. module_exit(userspace_dirty_log_exit);
  691. MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
  692. MODULE_AUTHOR("Jonathan Brassow <dm-devel@redhat.com>");
  693. MODULE_LICENSE("GPL");