dm-log-userspace-base.c 16 KB

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