dm-log-userspace-base.c 22 KB

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