dm-log-userspace-base.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  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. r = mempool_init_slab_pool(&lc->flush_entry_pool, FLUSH_ENTRY_POOL_SIZE,
  209. _flush_entry_cache);
  210. if (r) {
  211. DMERR("Failed to create flush_entry_pool");
  212. goto out;
  213. }
  214. /*
  215. * Send table string and get back any opened device.
  216. */
  217. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_CTR,
  218. ctr_str, str_size,
  219. devices_rdata, &devices_rdata_size);
  220. if (r < 0) {
  221. if (r == -ESRCH)
  222. DMERR("Userspace log server not found");
  223. else
  224. DMERR("Userspace log server failed to create log");
  225. goto out;
  226. }
  227. /* Since the region size does not change, get it now */
  228. rdata_size = sizeof(rdata);
  229. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_GET_REGION_SIZE,
  230. NULL, 0, (char *)&rdata, &rdata_size);
  231. if (r) {
  232. DMERR("Failed to get region size of dirty log");
  233. goto out;
  234. }
  235. lc->region_size = (uint32_t)rdata;
  236. lc->region_count = dm_sector_div_up(ti->len, lc->region_size);
  237. if (devices_rdata_size) {
  238. if (devices_rdata[devices_rdata_size - 1] != '\0') {
  239. DMERR("DM_ULOG_CTR device return string not properly terminated");
  240. r = -EINVAL;
  241. goto out;
  242. }
  243. r = dm_get_device(ti, devices_rdata,
  244. dm_table_get_mode(ti->table), &lc->log_dev);
  245. if (r)
  246. DMERR("Failed to register %s with device-mapper",
  247. devices_rdata);
  248. }
  249. if (lc->integrated_flush) {
  250. lc->dmlog_wq = alloc_workqueue("dmlogd", WQ_MEM_RECLAIM, 0);
  251. if (!lc->dmlog_wq) {
  252. DMERR("couldn't start dmlogd");
  253. r = -ENOMEM;
  254. goto out;
  255. }
  256. INIT_DELAYED_WORK(&lc->flush_log_work, do_flush);
  257. atomic_set(&lc->sched_flush, 0);
  258. }
  259. out:
  260. kfree(devices_rdata);
  261. if (r) {
  262. mempool_exit(&lc->flush_entry_pool);
  263. kfree(lc);
  264. kfree(ctr_str);
  265. } else {
  266. lc->usr_argv_str = ctr_str;
  267. log->context = lc;
  268. }
  269. return r;
  270. }
  271. static void userspace_dtr(struct dm_dirty_log *log)
  272. {
  273. struct log_c *lc = log->context;
  274. if (lc->integrated_flush) {
  275. /* flush workqueue */
  276. if (atomic_read(&lc->sched_flush))
  277. flush_delayed_work(&lc->flush_log_work);
  278. destroy_workqueue(lc->dmlog_wq);
  279. }
  280. (void) dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_DTR,
  281. NULL, 0, NULL, NULL);
  282. if (lc->log_dev)
  283. dm_put_device(lc->ti, lc->log_dev);
  284. mempool_exit(&lc->flush_entry_pool);
  285. kfree(lc->usr_argv_str);
  286. kfree(lc);
  287. return;
  288. }
  289. static int userspace_presuspend(struct dm_dirty_log *log)
  290. {
  291. int r;
  292. struct log_c *lc = log->context;
  293. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_PRESUSPEND,
  294. NULL, 0, NULL, NULL);
  295. return r;
  296. }
  297. static int userspace_postsuspend(struct dm_dirty_log *log)
  298. {
  299. int r;
  300. struct log_c *lc = log->context;
  301. /*
  302. * Run planned flush earlier.
  303. */
  304. if (lc->integrated_flush && atomic_read(&lc->sched_flush))
  305. flush_delayed_work(&lc->flush_log_work);
  306. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_POSTSUSPEND,
  307. NULL, 0, NULL, NULL);
  308. return r;
  309. }
  310. static int userspace_resume(struct dm_dirty_log *log)
  311. {
  312. int r;
  313. struct log_c *lc = log->context;
  314. lc->in_sync_hint = 0;
  315. r = dm_consult_userspace(lc->uuid, lc->luid, DM_ULOG_RESUME,
  316. NULL, 0, NULL, NULL);
  317. return r;
  318. }
  319. static uint32_t userspace_get_region_size(struct dm_dirty_log *log)
  320. {
  321. struct log_c *lc = log->context;
  322. return lc->region_size;
  323. }
  324. /*
  325. * userspace_is_clean
  326. *
  327. * Check whether a region is clean. If there is any sort of
  328. * failure when consulting the server, we return not clean.
  329. *
  330. * Returns: 1 if clean, 0 otherwise
  331. */
  332. static int userspace_is_clean(struct dm_dirty_log *log, region_t region)
  333. {
  334. int r;
  335. uint64_t region64 = (uint64_t)region;
  336. int64_t is_clean;
  337. size_t rdata_size;
  338. struct log_c *lc = log->context;
  339. rdata_size = sizeof(is_clean);
  340. r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_CLEAN,
  341. (char *)&region64, sizeof(region64),
  342. (char *)&is_clean, &rdata_size);
  343. return (r) ? 0 : (int)is_clean;
  344. }
  345. /*
  346. * userspace_in_sync
  347. *
  348. * Check if the region is in-sync. If there is any sort
  349. * of failure when consulting the server, we assume that
  350. * the region is not in sync.
  351. *
  352. * If 'can_block' is set, return immediately
  353. *
  354. * Returns: 1 if in-sync, 0 if not-in-sync, -EWOULDBLOCK
  355. */
  356. static int userspace_in_sync(struct dm_dirty_log *log, region_t region,
  357. int can_block)
  358. {
  359. int r;
  360. uint64_t region64 = region;
  361. int64_t in_sync;
  362. size_t rdata_size;
  363. struct log_c *lc = log->context;
  364. /*
  365. * We can never respond directly - even if in_sync_hint is
  366. * set. This is because another machine could see a device
  367. * failure and mark the region out-of-sync. If we don't go
  368. * to userspace to ask, we might think the region is in-sync
  369. * and allow a read to pick up data that is stale. (This is
  370. * very unlikely if a device actually fails; but it is very
  371. * likely if a connection to one device from one machine fails.)
  372. *
  373. * There still might be a problem if the mirror caches the region
  374. * state as in-sync... but then this call would not be made. So,
  375. * that is a mirror problem.
  376. */
  377. if (!can_block)
  378. return -EWOULDBLOCK;
  379. rdata_size = sizeof(in_sync);
  380. r = userspace_do_request(lc, lc->uuid, DM_ULOG_IN_SYNC,
  381. (char *)&region64, sizeof(region64),
  382. (char *)&in_sync, &rdata_size);
  383. return (r) ? 0 : (int)in_sync;
  384. }
  385. static int flush_one_by_one(struct log_c *lc, struct list_head *flush_list)
  386. {
  387. int r = 0;
  388. struct dm_dirty_log_flush_entry *fe;
  389. list_for_each_entry(fe, flush_list, list) {
  390. r = userspace_do_request(lc, lc->uuid, fe->type,
  391. (char *)&fe->region,
  392. sizeof(fe->region),
  393. NULL, NULL);
  394. if (r)
  395. break;
  396. }
  397. return r;
  398. }
  399. static int flush_by_group(struct log_c *lc, struct list_head *flush_list,
  400. int flush_with_payload)
  401. {
  402. int r = 0;
  403. int count;
  404. uint32_t type = 0;
  405. struct dm_dirty_log_flush_entry *fe, *tmp_fe;
  406. LIST_HEAD(tmp_list);
  407. uint64_t group[MAX_FLUSH_GROUP_COUNT];
  408. /*
  409. * Group process the requests
  410. */
  411. while (!list_empty(flush_list)) {
  412. count = 0;
  413. list_for_each_entry_safe(fe, tmp_fe, flush_list, list) {
  414. group[count] = fe->region;
  415. count++;
  416. list_move(&fe->list, &tmp_list);
  417. type = fe->type;
  418. if (count >= MAX_FLUSH_GROUP_COUNT)
  419. break;
  420. }
  421. if (flush_with_payload) {
  422. r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
  423. (char *)(group),
  424. count * sizeof(uint64_t),
  425. NULL, NULL);
  426. /*
  427. * Integrated flush failed.
  428. */
  429. if (r)
  430. break;
  431. } else {
  432. r = userspace_do_request(lc, lc->uuid, type,
  433. (char *)(group),
  434. count * sizeof(uint64_t),
  435. NULL, NULL);
  436. if (r) {
  437. /*
  438. * Group send failed. Attempt one-by-one.
  439. */
  440. list_splice_init(&tmp_list, flush_list);
  441. r = flush_one_by_one(lc, flush_list);
  442. break;
  443. }
  444. }
  445. }
  446. /*
  447. * Must collect flush_entrys that were successfully processed
  448. * as a group so that they will be free'd by the caller.
  449. */
  450. list_splice_init(&tmp_list, flush_list);
  451. return r;
  452. }
  453. /*
  454. * userspace_flush
  455. *
  456. * This function is ok to block.
  457. * The flush happens in two stages. First, it sends all
  458. * clear/mark requests that are on the list. Then it
  459. * tells the server to commit them. This gives the
  460. * server a chance to optimise the commit, instead of
  461. * doing it for every request.
  462. *
  463. * Additionally, we could implement another thread that
  464. * sends the requests up to the server - reducing the
  465. * load on flush. Then the flush would have less in
  466. * the list and be responsible for the finishing commit.
  467. *
  468. * Returns: 0 on success, < 0 on failure
  469. */
  470. static int userspace_flush(struct dm_dirty_log *log)
  471. {
  472. int r = 0;
  473. unsigned long flags;
  474. struct log_c *lc = log->context;
  475. LIST_HEAD(mark_list);
  476. LIST_HEAD(clear_list);
  477. int mark_list_is_empty;
  478. int clear_list_is_empty;
  479. struct dm_dirty_log_flush_entry *fe, *tmp_fe;
  480. mempool_t *flush_entry_pool = &lc->flush_entry_pool;
  481. spin_lock_irqsave(&lc->flush_lock, flags);
  482. list_splice_init(&lc->mark_list, &mark_list);
  483. list_splice_init(&lc->clear_list, &clear_list);
  484. spin_unlock_irqrestore(&lc->flush_lock, flags);
  485. mark_list_is_empty = list_empty(&mark_list);
  486. clear_list_is_empty = list_empty(&clear_list);
  487. if (mark_list_is_empty && clear_list_is_empty)
  488. return 0;
  489. r = flush_by_group(lc, &clear_list, 0);
  490. if (r)
  491. goto out;
  492. if (!lc->integrated_flush) {
  493. r = flush_by_group(lc, &mark_list, 0);
  494. if (r)
  495. goto out;
  496. r = userspace_do_request(lc, lc->uuid, DM_ULOG_FLUSH,
  497. NULL, 0, NULL, NULL);
  498. goto out;
  499. }
  500. /*
  501. * Send integrated flush request with mark_list as payload.
  502. */
  503. r = flush_by_group(lc, &mark_list, 1);
  504. if (r)
  505. goto out;
  506. if (mark_list_is_empty && !atomic_read(&lc->sched_flush)) {
  507. /*
  508. * When there are only clear region requests,
  509. * we schedule a flush in the future.
  510. */
  511. queue_delayed_work(lc->dmlog_wq, &lc->flush_log_work, 3 * HZ);
  512. atomic_set(&lc->sched_flush, 1);
  513. } else {
  514. /*
  515. * Cancel pending flush because we
  516. * have already flushed in mark_region.
  517. */
  518. cancel_delayed_work(&lc->flush_log_work);
  519. atomic_set(&lc->sched_flush, 0);
  520. }
  521. out:
  522. /*
  523. * We can safely remove these entries, even after failure.
  524. * Calling code will receive an error and will know that
  525. * the log facility has failed.
  526. */
  527. list_for_each_entry_safe(fe, tmp_fe, &mark_list, list) {
  528. list_del(&fe->list);
  529. mempool_free(fe, flush_entry_pool);
  530. }
  531. list_for_each_entry_safe(fe, tmp_fe, &clear_list, list) {
  532. list_del(&fe->list);
  533. mempool_free(fe, flush_entry_pool);
  534. }
  535. if (r)
  536. dm_table_event(lc->ti->table);
  537. return r;
  538. }
  539. /*
  540. * userspace_mark_region
  541. *
  542. * This function should avoid blocking unless absolutely required.
  543. * (Memory allocation is valid for blocking.)
  544. */
  545. static void userspace_mark_region(struct dm_dirty_log *log, region_t region)
  546. {
  547. unsigned long flags;
  548. struct log_c *lc = log->context;
  549. struct dm_dirty_log_flush_entry *fe;
  550. /* Wait for an allocation, but _never_ fail */
  551. fe = mempool_alloc(&lc->flush_entry_pool, GFP_NOIO);
  552. BUG_ON(!fe);
  553. spin_lock_irqsave(&lc->flush_lock, flags);
  554. fe->type = DM_ULOG_MARK_REGION;
  555. fe->region = region;
  556. list_add(&fe->list, &lc->mark_list);
  557. spin_unlock_irqrestore(&lc->flush_lock, flags);
  558. return;
  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. return;
  592. }
  593. /*
  594. * userspace_get_resync_work
  595. *
  596. * Get a region that needs recovery. It is valid to return
  597. * an error for this function.
  598. *
  599. * Returns: 1 if region filled, 0 if no work, <0 on error
  600. */
  601. static int userspace_get_resync_work(struct dm_dirty_log *log, region_t *region)
  602. {
  603. int r;
  604. size_t rdata_size;
  605. struct log_c *lc = log->context;
  606. struct {
  607. int64_t i; /* 64-bit for mix arch compatibility */
  608. region_t r;
  609. } pkg;
  610. if (lc->in_sync_hint >= lc->region_count)
  611. return 0;
  612. rdata_size = sizeof(pkg);
  613. r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_RESYNC_WORK,
  614. NULL, 0, (char *)&pkg, &rdata_size);
  615. *region = pkg.r;
  616. return (r) ? r : (int)pkg.i;
  617. }
  618. /*
  619. * userspace_set_region_sync
  620. *
  621. * Set the sync status of a given region. This function
  622. * must not fail.
  623. */
  624. static void userspace_set_region_sync(struct dm_dirty_log *log,
  625. region_t region, int in_sync)
  626. {
  627. struct log_c *lc = log->context;
  628. struct {
  629. region_t r;
  630. int64_t i;
  631. } pkg;
  632. pkg.r = region;
  633. pkg.i = (int64_t)in_sync;
  634. (void) userspace_do_request(lc, lc->uuid, DM_ULOG_SET_REGION_SYNC,
  635. (char *)&pkg, sizeof(pkg), NULL, NULL);
  636. /*
  637. * It would be nice to be able to report failures.
  638. * However, it is easy enough to detect and resolve.
  639. */
  640. return;
  641. }
  642. /*
  643. * userspace_get_sync_count
  644. *
  645. * If there is any sort of failure when consulting the server,
  646. * we assume that the sync count is zero.
  647. *
  648. * Returns: sync count on success, 0 on failure
  649. */
  650. static region_t userspace_get_sync_count(struct dm_dirty_log *log)
  651. {
  652. int r;
  653. size_t rdata_size;
  654. uint64_t sync_count;
  655. struct log_c *lc = log->context;
  656. rdata_size = sizeof(sync_count);
  657. r = userspace_do_request(lc, lc->uuid, DM_ULOG_GET_SYNC_COUNT,
  658. NULL, 0, (char *)&sync_count, &rdata_size);
  659. if (r)
  660. return 0;
  661. if (sync_count >= lc->region_count)
  662. lc->in_sync_hint = lc->region_count;
  663. return (region_t)sync_count;
  664. }
  665. /*
  666. * userspace_status
  667. *
  668. * Returns: amount of space consumed
  669. */
  670. static int userspace_status(struct dm_dirty_log *log, status_type_t status_type,
  671. char *result, unsigned maxlen)
  672. {
  673. int r = 0;
  674. char *table_args;
  675. size_t sz = (size_t)maxlen;
  676. struct log_c *lc = log->context;
  677. switch (status_type) {
  678. case STATUSTYPE_INFO:
  679. r = userspace_do_request(lc, lc->uuid, DM_ULOG_STATUS_INFO,
  680. NULL, 0, result, &sz);
  681. if (r) {
  682. sz = 0;
  683. DMEMIT("%s 1 COM_FAILURE", log->type->name);
  684. }
  685. break;
  686. case STATUSTYPE_TABLE:
  687. sz = 0;
  688. table_args = strchr(lc->usr_argv_str, ' ');
  689. BUG_ON(!table_args); /* There will always be a ' ' */
  690. table_args++;
  691. DMEMIT("%s %u %s ", log->type->name, lc->usr_argc, lc->uuid);
  692. if (lc->integrated_flush)
  693. DMEMIT("integrated_flush ");
  694. DMEMIT("%s ", table_args);
  695. break;
  696. }
  697. return (r) ? 0 : (int)sz;
  698. }
  699. /*
  700. * userspace_is_remote_recovering
  701. *
  702. * Returns: 1 if region recovering, 0 otherwise
  703. */
  704. static int userspace_is_remote_recovering(struct dm_dirty_log *log,
  705. region_t region)
  706. {
  707. int r;
  708. uint64_t region64 = region;
  709. struct log_c *lc = log->context;
  710. static unsigned long limit;
  711. struct {
  712. int64_t is_recovering;
  713. uint64_t in_sync_hint;
  714. } pkg;
  715. size_t rdata_size = sizeof(pkg);
  716. /*
  717. * Once the mirror has been reported to be in-sync,
  718. * it will never again ask for recovery work. So,
  719. * we can safely say there is not a remote machine
  720. * recovering if the device is in-sync. (in_sync_hint
  721. * must be reset at resume time.)
  722. */
  723. if (region < lc->in_sync_hint)
  724. return 0;
  725. else if (time_after(limit, jiffies))
  726. return 1;
  727. limit = jiffies + (HZ / 4);
  728. r = userspace_do_request(lc, lc->uuid, DM_ULOG_IS_REMOTE_RECOVERING,
  729. (char *)&region64, sizeof(region64),
  730. (char *)&pkg, &rdata_size);
  731. if (r)
  732. return 1;
  733. lc->in_sync_hint = pkg.in_sync_hint;
  734. return (int)pkg.is_recovering;
  735. }
  736. static struct dm_dirty_log_type _userspace_type = {
  737. .name = "userspace",
  738. .module = THIS_MODULE,
  739. .ctr = userspace_ctr,
  740. .dtr = userspace_dtr,
  741. .presuspend = userspace_presuspend,
  742. .postsuspend = userspace_postsuspend,
  743. .resume = userspace_resume,
  744. .get_region_size = userspace_get_region_size,
  745. .is_clean = userspace_is_clean,
  746. .in_sync = userspace_in_sync,
  747. .flush = userspace_flush,
  748. .mark_region = userspace_mark_region,
  749. .clear_region = userspace_clear_region,
  750. .get_resync_work = userspace_get_resync_work,
  751. .set_region_sync = userspace_set_region_sync,
  752. .get_sync_count = userspace_get_sync_count,
  753. .status = userspace_status,
  754. .is_remote_recovering = userspace_is_remote_recovering,
  755. };
  756. static int __init userspace_dirty_log_init(void)
  757. {
  758. int r = 0;
  759. _flush_entry_cache = KMEM_CACHE(dm_dirty_log_flush_entry, 0);
  760. if (!_flush_entry_cache) {
  761. DMWARN("Unable to create flush_entry_cache: No memory.");
  762. return -ENOMEM;
  763. }
  764. r = dm_ulog_tfr_init();
  765. if (r) {
  766. DMWARN("Unable to initialize userspace log communications");
  767. kmem_cache_destroy(_flush_entry_cache);
  768. return r;
  769. }
  770. r = dm_dirty_log_type_register(&_userspace_type);
  771. if (r) {
  772. DMWARN("Couldn't register userspace dirty log type");
  773. dm_ulog_tfr_exit();
  774. kmem_cache_destroy(_flush_entry_cache);
  775. return r;
  776. }
  777. DMINFO("version " DM_LOG_USERSPACE_VSN " loaded");
  778. return 0;
  779. }
  780. static void __exit userspace_dirty_log_exit(void)
  781. {
  782. dm_dirty_log_type_unregister(&_userspace_type);
  783. dm_ulog_tfr_exit();
  784. kmem_cache_destroy(_flush_entry_cache);
  785. DMINFO("version " DM_LOG_USERSPACE_VSN " unloaded");
  786. return;
  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@redhat.com>");
  792. MODULE_LICENSE("GPL");