dm-log-userspace-base.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2006-2009 Red Hat, Inc.
  4. *
  5. * This file is released under the LGPL.
  6. */
  7. #include <linux/bio.h>
  8. #include <linux/slab.h>
  9. #include <linux/jiffies.h>
  10. #include <linux/dm-dirty-log.h>
  11. #include <linux/device-mapper.h>
  12. #include <linux/dm-log-userspace.h>
  13. #include <linux/module.h>
  14. #include <linux/workqueue.h>
  15. #include "dm-log-userspace-transfer.h"
  16. #define DM_LOG_USERSPACE_VSN "1.3.0"
  17. #define FLUSH_ENTRY_POOL_SIZE 16
  18. struct dm_dirty_log_flush_entry {
  19. int type;
  20. region_t region;
  21. struct list_head list;
  22. };
  23. /*
  24. * This limit on the number of mark and clear request is, to a degree,
  25. * arbitrary. However, there is some basis for the choice in the limits
  26. * imposed on the size of data payload by dm-log-userspace-transfer.c:
  27. * dm_consult_userspace().
  28. */
  29. #define MAX_FLUSH_GROUP_COUNT 32
  30. struct log_c {
  31. struct dm_target *ti;
  32. struct dm_dev *log_dev;
  33. char *usr_argv_str;
  34. uint32_t usr_argc;
  35. uint32_t region_size;
  36. region_t region_count;
  37. uint64_t luid;
  38. char uuid[DM_UUID_LEN];
  39. /*
  40. * Mark and clear requests are held until a flush is issued
  41. * so that we can group, and thereby limit, the amount of
  42. * network traffic between kernel and userspace. The 'flush_lock'
  43. * is used to protect these lists.
  44. */
  45. spinlock_t flush_lock;
  46. struct list_head mark_list;
  47. struct list_head clear_list;
  48. /*
  49. * in_sync_hint gets set when doing is_remote_recovering. It
  50. * represents the first region that needs recovery. IOW, the
  51. * first zero bit of sync_bits. This can be useful for to limit
  52. * traffic for calls like is_remote_recovering and get_resync_work,
  53. * but be take care in its use for anything else.
  54. */
  55. uint64_t in_sync_hint;
  56. /*
  57. * Workqueue for flush of clear region requests.
  58. */
  59. struct workqueue_struct *dmlog_wq;
  60. struct delayed_work flush_log_work;
  61. atomic_t sched_flush;
  62. /*
  63. * Combine userspace flush and mark requests for efficiency.
  64. */
  65. uint32_t integrated_flush;
  66. mempool_t flush_entry_pool;
  67. };
  68. static struct kmem_cache *_flush_entry_cache;
  69. static int userspace_do_request(struct log_c *lc, const char *uuid,
  70. int request_type, char *data, size_t data_size,
  71. char *rdata, size_t *rdata_size)
  72. {
  73. int r;
  74. /*
  75. * If the server isn't there, -ESRCH is returned,
  76. * and we must keep trying until the server is
  77. * restored.
  78. */
  79. retry:
  80. r = dm_consult_userspace(uuid, lc->luid, request_type, data,
  81. data_size, rdata, rdata_size);
  82. if (r != -ESRCH)
  83. return r;
  84. DMERR(" Userspace log server not found.");
  85. while (1) {
  86. set_current_state(TASK_INTERRUPTIBLE);
  87. schedule_timeout(2*HZ);
  88. DMWARN("Attempting to contact userspace log server...");
  89. r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_CTR,
  90. lc->usr_argv_str,
  91. strlen(lc->usr_argv_str) + 1,
  92. NULL, NULL);
  93. if (!r)
  94. break;
  95. }
  96. DMINFO("Reconnected to userspace log server... DM_ULOG_CTR complete");
  97. r = dm_consult_userspace(uuid, lc->luid, DM_ULOG_RESUME, NULL,
  98. 0, NULL, NULL);
  99. if (!r)
  100. goto retry;
  101. DMERR("Error trying to resume userspace log: %d", r);
  102. return -ESRCH;
  103. }
  104. static int build_constructor_string(struct dm_target *ti,
  105. unsigned int argc, char **argv,
  106. char **ctr_str)
  107. {
  108. int i, str_size;
  109. char *str = NULL;
  110. *ctr_str = NULL;
  111. /*
  112. * Determine overall size of the string.
  113. */
  114. for (i = 0, str_size = 0; i < argc; i++)
  115. str_size += strlen(argv[i]) + 1; /* +1 for space between args */
  116. str_size += 20; /* Max number of chars in a printed u64 number */
  117. str = kzalloc(str_size, GFP_KERNEL);
  118. if (!str) {
  119. DMWARN("Unable to allocate memory for constructor string");
  120. return -ENOMEM;
  121. }
  122. str_size = sprintf(str, "%llu", (unsigned long long)ti->len);
  123. for (i = 0; i < argc; i++)
  124. str_size += sprintf(str + str_size, " %s", argv[i]);
  125. *ctr_str = str;
  126. return str_size;
  127. }
  128. static void do_flush(struct work_struct *work)
  129. {
  130. int r;
  131. struct log_c *lc = container_of(work, struct log_c, flush_log_work.work);
  132. atomic_set(&lc->sched_flush, 0);
  133. r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH, NULL, 0, NULL, NULL);
  134. if (r)
  135. dm_table_event(lc->ti->table);
  136. }
  137. /*
  138. * userspace_ctr
  139. *
  140. * argv contains:
  141. * <UUID> [integrated_flush] <other args>
  142. * Where 'other args' are the userspace implementation-specific log
  143. * arguments.
  144. *
  145. * Example:
  146. * <UUID> [integrated_flush] clustered-disk <arg count> <log dev>
  147. * <region_size> [[no]sync]
  148. *
  149. * This module strips off the <UUID> and uses it for identification
  150. * purposes when communicating with userspace about a log.
  151. *
  152. * If integrated_flush is defined, the kernel combines flush
  153. * and mark requests.
  154. *
  155. * The rest of the line, beginning with 'clustered-disk', is passed
  156. * to the userspace ctr function.
  157. */
  158. static int userspace_ctr(struct dm_dirty_log *log, struct dm_target *ti,
  159. unsigned int argc, char **argv)
  160. {
  161. int r = 0;
  162. int str_size;
  163. char *ctr_str = NULL;
  164. struct log_c *lc = NULL;
  165. uint64_t rdata;
  166. size_t rdata_size = sizeof(rdata);
  167. char *devices_rdata = NULL;
  168. size_t devices_rdata_size = DM_NAME_LEN;
  169. if (argc < 3) {
  170. DMWARN("Too few arguments to userspace dirty log");
  171. return -EINVAL;
  172. }
  173. lc = kzalloc_obj(*lc);
  174. if (!lc) {
  175. DMWARN("Unable to allocate userspace log context.");
  176. return -ENOMEM;
  177. }
  178. /* The ptr value is sufficient for local unique id */
  179. lc->luid = (unsigned long)lc;
  180. lc->ti = ti;
  181. if (strlen(argv[0]) > (DM_UUID_LEN - 1)) {
  182. DMWARN("UUID argument too long.");
  183. kfree(lc);
  184. return -EINVAL;
  185. }
  186. lc->usr_argc = argc;
  187. strscpy(lc->uuid, argv[0], sizeof(lc->uuid));
  188. argc--;
  189. argv++;
  190. spin_lock_init(&lc->flush_lock);
  191. INIT_LIST_HEAD(&lc->mark_list);
  192. INIT_LIST_HEAD(&lc->clear_list);
  193. if (!strcasecmp(argv[0], "integrated_flush")) {
  194. lc->integrated_flush = 1;
  195. argc--;
  196. argv++;
  197. }
  198. str_size = build_constructor_string(ti, argc, argv, &ctr_str);
  199. if (str_size < 0) {
  200. kfree(lc);
  201. return str_size;
  202. }
  203. devices_rdata = kzalloc(devices_rdata_size, GFP_KERNEL);
  204. if (!devices_rdata) {
  205. DMERR("Failed to allocate memory for device information");
  206. r = -ENOMEM;
  207. goto out;
  208. }
  209. r = mempool_init_slab_pool(&lc->flush_entry_pool, FLUSH_ENTRY_POOL_SIZE,
  210. _flush_entry_cache);
  211. if (r) {
  212. DMERR("Failed to create flush_entry_pool");
  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",
  252. WQ_MEM_RECLAIM | WQ_PERCPU, 0);
  253. if (!lc->dmlog_wq) {
  254. DMERR("couldn't start dmlogd");
  255. r = -ENOMEM;
  256. goto out;
  257. }
  258. INIT_DELAYED_WORK(&lc->flush_log_work, do_flush);
  259. atomic_set(&lc->sched_flush, 0);
  260. }
  261. out:
  262. kfree(devices_rdata);
  263. if (r) {
  264. mempool_exit(&lc->flush_entry_pool);
  265. kfree(lc);
  266. kfree(ctr_str);
  267. } else {
  268. lc->usr_argv_str = ctr_str;
  269. log->context = lc;
  270. }
  271. return r;
  272. }
  273. static void userspace_dtr(struct dm_dirty_log *log)
  274. {
  275. struct log_c *lc = log->context;
  276. if (lc->integrated_flush) {
  277. /* flush workqueue */
  278. if (atomic_read(&lc->sched_flush))
  279. flush_delayed_work(&lc->flush_log_work);
  280. destroy_workqueue(lc->dmlog_wq);
  281. }
  282. (void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
  283. NULL, 0, NULL, NULL);
  284. if (lc->log_dev)
  285. dm_put_device(lc->ti, lc->log_dev);
  286. mempool_exit(&lc->flush_entry_pool);
  287. kfree(lc->usr_argv_str);
  288. kfree(lc);
  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. }
  560. /*
  561. * userspace_clear_region
  562. *
  563. * This function must not block.
  564. * So, the alloc can't block. In the worst case, it is ok to
  565. * fail. It would simply mean we can't clear the region.
  566. * Does nothing to current sync context, but does mean
  567. * the region will be re-sync'ed on a reload of the mirror
  568. * even though it is in-sync.
  569. */
  570. static void userspace_clear_region(struct dm_dirty_log *log, region_t region)
  571. {
  572. unsigned long flags;
  573. struct log_c *lc = log->context;
  574. struct dm_dirty_log_flush_entry *fe;
  575. /*
  576. * If we fail to allocate, we skip the clearing of
  577. * the region. This doesn't hurt us in any way, except
  578. * to cause the region to be resync'ed when the
  579. * device is activated next time.
  580. */
  581. fe = mempool_alloc(&lc->flush_entry_pool, GFP_ATOMIC);
  582. if (!fe) {
  583. DMERR("Failed to allocate memory to clear region.");
  584. return;
  585. }
  586. spin_lock_irqsave(&lc->flush_lock, flags);
  587. fe->type = DM_ULOG_CLEAR_REGION;
  588. fe->region = region;
  589. list_add(&fe->list, &lc->clear_list);
  590. spin_unlock_irqrestore(&lc->flush_lock, flags);
  591. }
  592. /*
  593. * userspace_get_resync_work
  594. *
  595. * Get a region that needs recovery. It is valid to return
  596. * an error for this function.
  597. *
  598. * Returns: 1 if region filled, 0 if no work, <0 on error
  599. */
  600. static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
  601. {
  602. int r;
  603. size_t rdata_size;
  604. struct log_c *lc = log->context;
  605. struct {
  606. int64_t i; /* 64-bit for mix arch compatibility */
  607. region_t r;
  608. } pkg;
  609. if (lc->in_sync_hint >= lc->region_count)
  610. return 0;
  611. rdata_size = sizeof(pkg);
  612. r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
  613. NULL, 0, (char *)&pkg, &rdata_size);
  614. *region = pkg.r;
  615. return (r) ? r : (int)pkg.i;
  616. }
  617. /*
  618. * userspace_set_region_sync
  619. *
  620. * Set the sync status of a given region. This function
  621. * must not fail.
  622. */
  623. static void userspace_set_region_sync(struct dm_dirty_log *log,
  624. region_t region, int in_sync)
  625. {
  626. struct log_c *lc = log->context;
  627. struct {
  628. region_t r;
  629. int64_t i;
  630. } pkg;
  631. pkg.r = region;
  632. pkg.i = (int64_t)in_sync;
  633. (void) userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
  634. (char *)&pkg, sizeof(pkg), NULL, NULL);
  635. /*
  636. * It would be nice to be able to report failures.
  637. * However, it is easy enough to detect and resolve.
  638. */
  639. }
  640. /*
  641. * userspace_get_sync_count
  642. *
  643. * If there is any sort of failure when consulting the server,
  644. * we assume that the sync count is zero.
  645. *
  646. * Returns: sync count on success, 0 on failure
  647. */
  648. static region_t userspace_get_sync_count(struct dm_dirty_log *log)
  649. {
  650. int r;
  651. size_t rdata_size;
  652. uint64_t sync_count;
  653. struct log_c *lc = log->context;
  654. rdata_size = sizeof(sync_count);
  655. r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
  656. NULL, 0, (char *)&sync_count, &rdata_size);
  657. if (r)
  658. return 0;
  659. if (sync_count >= lc->region_count)
  660. lc->in_sync_hint = lc->region_count;
  661. return (region_t)sync_count;
  662. }
  663. /*
  664. * userspace_status
  665. *
  666. * Returns: amount of space consumed
  667. */
  668. static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
  669. char *result, unsigned int maxlen)
  670. {
  671. int r = 0;
  672. char *table_args;
  673. size_t sz = (size_t)maxlen;
  674. struct log_c *lc = log->context;
  675. switch (status_type) {
  676. case STATUSTYPE_INFO:
  677. r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
  678. NULL, 0, result, &sz);
  679. if (r) {
  680. sz = 0;
  681. DMEMIT("%s 1 COM_FAILURE", log->type->name);
  682. }
  683. break;
  684. case STATUSTYPE_TABLE:
  685. sz = 0;
  686. table_args = strchr(lc->usr_argv_str, ' ');
  687. BUG_ON(!table_args); /* There will always be a ' ' */
  688. table_args++;
  689. DMEMIT("%s %u %s ", log->type->name, lc->usr_argc, lc->uuid);
  690. if (lc->integrated_flush)
  691. DMEMIT("integrated_flush ");
  692. DMEMIT("%s ", table_args);
  693. break;
  694. case STATUSTYPE_IMA:
  695. *result = '\0';
  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. }
  788. module_init(userspace_dirty_log_init);
  789. module_exit(userspace_dirty_log_exit);
  790. MODULE_DESCRIPTION(DM_NAME " userspace dirty log link");
  791. MODULE_AUTHOR("Jonathan Brassow <dm-devel@lists.linux.dev>");
  792. MODULE_LICENSE("GPL");