PageRenderTime 60ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/xlators/mgmt/glusterd/src/glusterd-brick-ops.c

https://github.com/xiushu2012/glusterfs
C | 1679 lines | 1365 code | 221 blank | 93 comment | 252 complexity | 1d61c86ac6385898f35a2d9d3bfd20fe MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, LGPL-2.0, Apache-2.0, BSD-3-Clause

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. Copyright (c) 2011-2012 Red Hat, Inc. <http://www.redhat.com>
  3. This file is part of GlusterFS.
  4. This file is licensed to you under your choice of the GNU Lesser
  5. General Public License, version 3 or any later version (LGPLv3 or
  6. later), or the GNU General Public License, version 2 (GPLv2), in all
  7. cases as published by the Free Software Foundation.
  8. */
  9. #ifndef _CONFIG_H
  10. #define _CONFIG_H
  11. #include "config.h"
  12. #endif
  13. #include "common-utils.h"
  14. #include "cli1-xdr.h"
  15. #include "xdr-generic.h"
  16. #include "glusterd.h"
  17. #include "glusterd-op-sm.h"
  18. #include "glusterd-store.h"
  19. #include "glusterd-utils.h"
  20. #include "glusterd-volgen.h"
  21. #include "run.h"
  22. #include <sys/signal.h>
  23. /* misc */
  24. /* In this function, we decide, based on the 'count' of the brick,
  25. where to add it in the current volume. 'count' tells us already
  26. how many of the given bricks are added. other argument are self-
  27. descriptive. */
  28. int
  29. add_brick_at_right_order (glusterd_brickinfo_t *brickinfo,
  30. glusterd_volinfo_t *volinfo, int count,
  31. int32_t stripe_cnt, int32_t replica_cnt)
  32. {
  33. int idx = 0;
  34. int i = 0;
  35. int sub_cnt = 0;
  36. glusterd_brickinfo_t *brick = NULL;
  37. /* The complexity of the function is in deciding at which index
  38. to add new brick. Even though it can be defined with a complex
  39. single formula for all volume, it is seperated out to make it
  40. more readable */
  41. if (stripe_cnt) {
  42. /* common formula when 'stripe_count' is set */
  43. /* idx = ((count / ((stripe_cnt * volinfo->replica_count) -
  44. volinfo->dist_leaf_count)) * volinfo->dist_leaf_count) +
  45. (count + volinfo->dist_leaf_count);
  46. */
  47. sub_cnt = volinfo->dist_leaf_count;
  48. idx = ((count / ((stripe_cnt * volinfo->replica_count) -
  49. sub_cnt)) * sub_cnt) +
  50. (count + sub_cnt);
  51. goto insert_brick;
  52. }
  53. /* replica count is set */
  54. /* common formula when 'replica_count' is set */
  55. /* idx = ((count / (replica_cnt - existing_replica_count)) *
  56. existing_replica_count) +
  57. (count + existing_replica_count);
  58. */
  59. sub_cnt = volinfo->replica_count;
  60. idx = (count / (replica_cnt - sub_cnt) * sub_cnt) +
  61. (count + sub_cnt);
  62. insert_brick:
  63. i = 0;
  64. list_for_each_entry (brick, &volinfo->bricks, brick_list) {
  65. i++;
  66. if (i < idx)
  67. continue;
  68. gf_log (THIS->name, GF_LOG_DEBUG, "brick:%s index=%d, count=%d",
  69. brick->path, idx, count);
  70. list_add (&brickinfo->brick_list, &brick->brick_list);
  71. break;
  72. }
  73. return 0;
  74. }
  75. static int
  76. gd_addbr_validate_stripe_count (glusterd_volinfo_t *volinfo, int stripe_count,
  77. int total_bricks, int *type, char *err_str,
  78. size_t err_len)
  79. {
  80. int ret = -1;
  81. switch (volinfo->type) {
  82. case GF_CLUSTER_TYPE_NONE:
  83. if ((volinfo->brick_count * stripe_count) == total_bricks) {
  84. /* Change the volume type */
  85. *type = GF_CLUSTER_TYPE_STRIPE;
  86. gf_log (THIS->name, GF_LOG_INFO,
  87. "Changing the type of volume %s from "
  88. "'distribute' to 'stripe'", volinfo->volname);
  89. ret = 0;
  90. goto out;
  91. } else {
  92. snprintf (err_str, err_len, "Incorrect number of "
  93. "bricks (%d) supplied for stripe count (%d).",
  94. (total_bricks - volinfo->brick_count),
  95. stripe_count);
  96. gf_log (THIS->name, GF_LOG_ERROR, "%s", err_str);
  97. goto out;
  98. }
  99. break;
  100. case GF_CLUSTER_TYPE_REPLICATE:
  101. if (!(total_bricks % (volinfo->replica_count * stripe_count))) {
  102. /* Change the volume type */
  103. *type = GF_CLUSTER_TYPE_STRIPE_REPLICATE;
  104. gf_log (THIS->name, GF_LOG_INFO,
  105. "Changing the type of volume %s from "
  106. "'replicate' to 'replicate-stripe'",
  107. volinfo->volname);
  108. ret = 0;
  109. goto out;
  110. } else {
  111. snprintf (err_str, err_len, "Incorrect number of "
  112. "bricks (%d) supplied for changing volume's "
  113. "stripe count to %d, need at least %d bricks",
  114. (total_bricks - volinfo->brick_count),
  115. stripe_count,
  116. (volinfo->replica_count * stripe_count));
  117. gf_log (THIS->name, GF_LOG_ERROR, "%s", err_str);
  118. goto out;
  119. }
  120. break;
  121. case GF_CLUSTER_TYPE_STRIPE:
  122. case GF_CLUSTER_TYPE_STRIPE_REPLICATE:
  123. if (stripe_count < volinfo->stripe_count) {
  124. snprintf (err_str, err_len,
  125. "Incorrect stripe count (%d) supplied. "
  126. "Volume already has stripe count (%d)",
  127. stripe_count, volinfo->stripe_count);
  128. gf_log (THIS->name, GF_LOG_ERROR, "%s", err_str);
  129. goto out;
  130. }
  131. if (stripe_count == volinfo->stripe_count) {
  132. if (!(total_bricks % volinfo->dist_leaf_count)) {
  133. /* its same as the one which exists */
  134. ret = 1;
  135. goto out;
  136. }
  137. }
  138. if (stripe_count > volinfo->stripe_count) {
  139. /* We have to make sure before and after 'add-brick',
  140. the number or subvolumes for distribute will remain
  141. same, when stripe count is given */
  142. if ((volinfo->brick_count * (stripe_count *
  143. volinfo->replica_count)) ==
  144. (total_bricks * volinfo->dist_leaf_count)) {
  145. /* Change the dist_leaf_count */
  146. gf_log (THIS->name, GF_LOG_INFO,
  147. "Changing the stripe count of "
  148. "volume %s from %d to %d",
  149. volinfo->volname,
  150. volinfo->stripe_count, stripe_count);
  151. ret = 0;
  152. goto out;
  153. }
  154. }
  155. break;
  156. }
  157. out:
  158. return ret;
  159. }
  160. static int
  161. gd_addbr_validate_replica_count (glusterd_volinfo_t *volinfo, int replica_count,
  162. int total_bricks, int *type, char *err_str,
  163. int err_len)
  164. {
  165. int ret = -1;
  166. /* replica count is set */
  167. switch (volinfo->type) {
  168. case GF_CLUSTER_TYPE_NONE:
  169. if ((volinfo->brick_count * replica_count) == total_bricks) {
  170. /* Change the volume type */
  171. *type = GF_CLUSTER_TYPE_REPLICATE;
  172. gf_log (THIS->name, GF_LOG_INFO,
  173. "Changing the type of volume %s from "
  174. "'distribute' to 'replica'", volinfo->volname);
  175. ret = 0;
  176. goto out;
  177. } else {
  178. snprintf (err_str, err_len, "Incorrect number of "
  179. "bricks (%d) supplied for replica count (%d).",
  180. (total_bricks - volinfo->brick_count),
  181. replica_count);
  182. gf_log (THIS->name, GF_LOG_ERROR, "%s", err_str);
  183. goto out;
  184. }
  185. break;
  186. case GF_CLUSTER_TYPE_STRIPE:
  187. if (!(total_bricks % (volinfo->dist_leaf_count * replica_count))) {
  188. /* Change the volume type */
  189. *type = GF_CLUSTER_TYPE_STRIPE_REPLICATE;
  190. gf_log (THIS->name, GF_LOG_INFO,
  191. "Changing the type of volume %s from "
  192. "'stripe' to 'replicate-stripe'",
  193. volinfo->volname);
  194. ret = 0;
  195. goto out;
  196. } else {
  197. snprintf (err_str, err_len, "Incorrect number of "
  198. "bricks (%d) supplied for changing volume's "
  199. "replica count to %d, need at least %d "
  200. "bricks",
  201. (total_bricks - volinfo->brick_count),
  202. replica_count, (volinfo->dist_leaf_count *
  203. replica_count));
  204. gf_log (THIS->name, GF_LOG_ERROR, "%s", err_str);
  205. goto out;
  206. }
  207. break;
  208. case GF_CLUSTER_TYPE_REPLICATE:
  209. case GF_CLUSTER_TYPE_STRIPE_REPLICATE:
  210. if (replica_count < volinfo->replica_count) {
  211. snprintf (err_str, err_len,
  212. "Incorrect replica count (%d) supplied. "
  213. "Volume already has (%d)",
  214. replica_count, volinfo->replica_count);
  215. gf_log (THIS->name, GF_LOG_ERROR, "%s", err_str);
  216. goto out;
  217. }
  218. if (replica_count == volinfo->replica_count) {
  219. if (!(total_bricks % volinfo->dist_leaf_count)) {
  220. ret = 1;
  221. goto out;
  222. }
  223. }
  224. if (replica_count > volinfo->replica_count) {
  225. /* We have to make sure before and after 'add-brick',
  226. the number or subvolumes for distribute will remain
  227. same, when replica count is given */
  228. if ((total_bricks * volinfo->dist_leaf_count) ==
  229. (volinfo->brick_count * (replica_count *
  230. volinfo->stripe_count))) {
  231. /* Change the dist_leaf_count */
  232. gf_log (THIS->name, GF_LOG_INFO,
  233. "Changing the replica count of "
  234. "volume %s from %d to %d",
  235. volinfo->volname, volinfo->replica_count,
  236. replica_count);
  237. ret = 0;
  238. goto out;
  239. }
  240. }
  241. break;
  242. }
  243. out:
  244. return ret;
  245. }
  246. static int
  247. gd_rmbr_validate_replica_count (glusterd_volinfo_t *volinfo,
  248. int32_t replica_count,
  249. int32_t brick_count, char *err_str,
  250. size_t err_len)
  251. {
  252. int ret = -1;
  253. int replica_nodes = 0;
  254. switch (volinfo->type) {
  255. case GF_CLUSTER_TYPE_NONE:
  256. case GF_CLUSTER_TYPE_STRIPE:
  257. snprintf (err_str, err_len,
  258. "replica count (%d) option given for non replicate "
  259. "volume %s", replica_count, volinfo->volname);
  260. gf_log (THIS->name, GF_LOG_WARNING, "%s", err_str);
  261. goto out;
  262. case GF_CLUSTER_TYPE_REPLICATE:
  263. case GF_CLUSTER_TYPE_STRIPE_REPLICATE:
  264. /* in remove brick, you can only reduce the replica count */
  265. if (replica_count > volinfo->replica_count) {
  266. snprintf (err_str, err_len,
  267. "given replica count (%d) option is more "
  268. "than volume %s's replica count (%d)",
  269. replica_count, volinfo->volname,
  270. volinfo->replica_count);
  271. gf_log (THIS->name, GF_LOG_WARNING, "%s", err_str);
  272. goto out;
  273. }
  274. if (replica_count == volinfo->replica_count) {
  275. /* This means the 'replica N' option on CLI was
  276. redundant. Check if the total number of bricks given
  277. for removal is same as 'dist_leaf_count' */
  278. if (brick_count % volinfo->dist_leaf_count) {
  279. snprintf (err_str, err_len,
  280. "number of bricks provided (%d) is "
  281. "not valid. need at least %d "
  282. "(or %dxN)", brick_count,
  283. volinfo->dist_leaf_count,
  284. volinfo->dist_leaf_count);
  285. gf_log (THIS->name, GF_LOG_WARNING, "%s",
  286. err_str);
  287. goto out;
  288. }
  289. ret = 1;
  290. goto out;
  291. }
  292. replica_nodes = ((volinfo->brick_count /
  293. volinfo->replica_count) *
  294. (volinfo->replica_count - replica_count));
  295. if (brick_count % replica_nodes) {
  296. snprintf (err_str, err_len,
  297. "need %d(xN) bricks for reducing replica "
  298. "count of the volume from %d to %d",
  299. replica_nodes, volinfo->replica_count,
  300. replica_count);
  301. goto out;
  302. }
  303. break;
  304. }
  305. ret = 0;
  306. out:
  307. return ret;
  308. }
  309. /* Handler functions */
  310. int
  311. glusterd_handle_add_brick (rpcsvc_request_t *req)
  312. {
  313. int32_t ret = -1;
  314. gf_cli_req cli_req = {{0,}};
  315. dict_t *dict = NULL;
  316. char *bricks = NULL;
  317. char *volname = NULL;
  318. int brick_count = 0;
  319. void *cli_rsp = NULL;
  320. char err_str[2048] = {0,};
  321. gf_cli_rsp rsp = {0,};
  322. glusterd_volinfo_t *volinfo = NULL;
  323. xlator_t *this = NULL;
  324. int total_bricks = 0;
  325. int32_t replica_count = 0;
  326. int32_t stripe_count = 0;
  327. int type = 0;
  328. this = THIS;
  329. GF_ASSERT(this);
  330. GF_ASSERT (req);
  331. ret = xdr_to_generic (req->msg[0], &cli_req,
  332. (xdrproc_t)xdr_gf_cli_req);
  333. if (ret < 0) {
  334. //failed to decode msg;
  335. req->rpc_err = GARBAGE_ARGS;
  336. snprintf (err_str, sizeof (err_str), "Garbage args received");
  337. goto out;
  338. }
  339. gf_log (this->name, GF_LOG_INFO, "Received add brick req");
  340. if (cli_req.dict.dict_len) {
  341. /* Unserialize the dictionary */
  342. dict = dict_new ();
  343. ret = dict_unserialize (cli_req.dict.dict_val,
  344. cli_req.dict.dict_len,
  345. &dict);
  346. if (ret < 0) {
  347. gf_log (this->name, GF_LOG_ERROR,
  348. "failed to "
  349. "unserialize req-buffer to dictionary");
  350. snprintf (err_str, sizeof (err_str), "Unable to decode "
  351. "the command");
  352. goto out;
  353. }
  354. }
  355. ret = dict_get_str (dict, "volname", &volname);
  356. if (ret) {
  357. snprintf (err_str, sizeof (err_str), "Unable to get volume "
  358. "name");
  359. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  360. goto out;
  361. }
  362. if (!(ret = glusterd_check_volume_exists (volname))) {
  363. ret = -1;
  364. snprintf (err_str, sizeof (err_str), "Volume %s does not exist",
  365. volname);
  366. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  367. goto out;
  368. }
  369. ret = dict_get_int32 (dict, "count", &brick_count);
  370. if (ret) {
  371. snprintf (err_str, sizeof (err_str), "Unable to get volume "
  372. "brick count");
  373. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  374. goto out;
  375. }
  376. ret = dict_get_int32 (dict, "replica-count", &replica_count);
  377. if (!ret) {
  378. gf_log (this->name, GF_LOG_INFO, "replica-count is %d",
  379. replica_count);
  380. }
  381. ret = dict_get_int32 (dict, "stripe-count", &stripe_count);
  382. if (!ret) {
  383. gf_log (this->name, GF_LOG_INFO, "stripe-count is %d",
  384. stripe_count);
  385. }
  386. ret = glusterd_volinfo_find (volname, &volinfo);
  387. if (ret) {
  388. snprintf (err_str, sizeof (err_str), "Unable to get volinfo "
  389. "for volume name %s", volname);
  390. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  391. goto out;
  392. }
  393. total_bricks = volinfo->brick_count + brick_count;
  394. if (!stripe_count && !replica_count) {
  395. if (volinfo->type == GF_CLUSTER_TYPE_NONE)
  396. goto brick_val;
  397. if ((volinfo->brick_count < volinfo->dist_leaf_count) &&
  398. (total_bricks <= volinfo->dist_leaf_count))
  399. goto brick_val;
  400. if ((brick_count % volinfo->dist_leaf_count) != 0) {
  401. snprintf (err_str, sizeof (err_str), "Incorrect number "
  402. "of bricks supplied %d with count %d",
  403. brick_count, volinfo->dist_leaf_count);
  404. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  405. ret = -1;
  406. goto out;
  407. }
  408. goto brick_val;
  409. /* done with validation.. below section is if stripe|replica
  410. count is given */
  411. }
  412. /* These bricks needs to be added one per a replica or stripe volume */
  413. if (stripe_count) {
  414. ret = gd_addbr_validate_stripe_count (volinfo, stripe_count,
  415. total_bricks, &type,
  416. err_str,
  417. sizeof (err_str));
  418. if (ret == -1) {
  419. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  420. goto out;
  421. }
  422. /* if stripe count is same as earlier, set it back to 0 */
  423. if (ret == 1)
  424. stripe_count = 0;
  425. ret = dict_set_int32 (dict, "stripe-count", stripe_count);
  426. if (ret) {
  427. gf_log (this->name, GF_LOG_ERROR,
  428. "failed to set the stripe-count in dict");
  429. goto out;
  430. }
  431. goto brick_val;
  432. }
  433. ret = gd_addbr_validate_replica_count (volinfo, replica_count,
  434. total_bricks,
  435. &type, err_str,
  436. sizeof (err_str));
  437. if (ret == -1) {
  438. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  439. goto out;
  440. }
  441. /* if replica count is same as earlier, set it back to 0 */
  442. if (ret == 1)
  443. replica_count = 0;
  444. ret = dict_set_int32 (dict, "replica-count", replica_count);
  445. if (ret) {
  446. gf_log (this->name, GF_LOG_ERROR,
  447. "failed to set the replica-count in dict");
  448. goto out;
  449. }
  450. brick_val:
  451. ret = dict_get_str (dict, "bricks", &bricks);
  452. if (ret) {
  453. snprintf (err_str, sizeof (err_str), "Unable to get volume "
  454. "bricks");
  455. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  456. goto out;
  457. }
  458. if (type != volinfo->type) {
  459. ret = dict_set_int32 (dict, "type", type);
  460. if (ret)
  461. gf_log (this->name, GF_LOG_ERROR,
  462. "failed to set the new type in dict");
  463. }
  464. ret = glusterd_op_begin_synctask (req, GD_OP_ADD_BRICK, dict);
  465. out:
  466. if (ret) {
  467. rsp.op_ret = -1;
  468. rsp.op_errno = 0;
  469. if (err_str[0] == '\0')
  470. snprintf (err_str, sizeof (err_str), "Operation failed");
  471. rsp.op_errstr = err_str;
  472. cli_rsp = &rsp;
  473. glusterd_to_cli (req, cli_rsp, NULL, 0, NULL,
  474. (xdrproc_t)xdr_gf_cli_rsp, dict);
  475. if (dict)
  476. dict_unref (dict);
  477. ret = 0; //sent error to cli, prevent second reply
  478. }
  479. free (cli_req.dict.dict_val); //its malloced by xdr
  480. return ret;
  481. }
  482. int
  483. glusterd_handle_remove_brick (rpcsvc_request_t *req)
  484. {
  485. int32_t ret = -1;
  486. gf_cli_req cli_req = {{0,}};
  487. dict_t *dict = NULL;
  488. int32_t count = 0;
  489. char *brick = NULL;
  490. char key[256] = {0,};
  491. char *brick_list = NULL;
  492. int i = 1;
  493. glusterd_volinfo_t *volinfo = NULL;
  494. glusterd_brickinfo_t *brickinfo = NULL;
  495. int32_t pos = 0;
  496. int32_t sub_volume = 0;
  497. int32_t sub_volume_start = 0;
  498. int32_t sub_volume_end = 0;
  499. glusterd_brickinfo_t *tmp = NULL;
  500. char err_str[2048] = {0};
  501. gf_cli_rsp rsp = {0,};
  502. void *cli_rsp = NULL;
  503. char vol_type[256] = {0,};
  504. int32_t replica_count = 0;
  505. int32_t brick_index = 0;
  506. int32_t tmp_brick_idx = 0;
  507. int found = 0;
  508. int diff_count = 0;
  509. char *volname = 0;
  510. xlator_t *this = NULL;
  511. GF_ASSERT (req);
  512. this = THIS;
  513. ret = xdr_to_generic (req->msg[0], &cli_req,
  514. (xdrproc_t)xdr_gf_cli_req);
  515. if (ret < 0) {
  516. //failed to decode msg;
  517. req->rpc_err = GARBAGE_ARGS;
  518. snprintf (err_str, sizeof (err_str), "Received garbage args");
  519. goto out;
  520. }
  521. gf_log (this->name, GF_LOG_INFO, "Received rem brick req");
  522. if (cli_req.dict.dict_len) {
  523. /* Unserialize the dictionary */
  524. dict = dict_new ();
  525. ret = dict_unserialize (cli_req.dict.dict_val,
  526. cli_req.dict.dict_len,
  527. &dict);
  528. if (ret < 0) {
  529. gf_log (this->name, GF_LOG_ERROR,
  530. "failed to "
  531. "unserialize req-buffer to dictionary");
  532. snprintf (err_str, sizeof (err_str), "Unable to decode "
  533. "the command");
  534. goto out;
  535. }
  536. }
  537. ret = dict_get_str (dict, "volname", &volname);
  538. if (ret) {
  539. snprintf (err_str, sizeof (err_str), "Unable to get volume "
  540. "name");
  541. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  542. goto out;
  543. }
  544. ret = dict_get_int32 (dict, "count", &count);
  545. if (ret) {
  546. snprintf (err_str, sizeof (err_str), "Unable to get brick "
  547. "count");
  548. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  549. goto out;
  550. }
  551. ret = glusterd_volinfo_find (volname, &volinfo);
  552. if (ret) {
  553. snprintf (err_str, sizeof (err_str),"Volume %s does not exist",
  554. volname);
  555. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  556. goto out;
  557. }
  558. ret = dict_get_int32 (dict, "replica-count", &replica_count);
  559. if (!ret) {
  560. gf_log (this->name, GF_LOG_INFO,
  561. "request to change replica-count to %d", replica_count);
  562. ret = gd_rmbr_validate_replica_count (volinfo, replica_count,
  563. count, err_str,
  564. sizeof (err_str));
  565. if (ret < 0) {
  566. /* logging and error msg are done in above function
  567. itself */
  568. goto out;
  569. }
  570. dict_del (dict, "replica-count");
  571. if (ret) {
  572. replica_count = 0;
  573. } else {
  574. ret = dict_set_int32 (dict, "replica-count",
  575. replica_count);
  576. if (ret) {
  577. gf_log (this->name, GF_LOG_WARNING,
  578. "failed to set the replica_count "
  579. "in dict");
  580. goto out;
  581. }
  582. }
  583. }
  584. /* 'vol_type' is used for giving the meaning full error msg for user */
  585. if (volinfo->type == GF_CLUSTER_TYPE_REPLICATE) {
  586. strcpy (vol_type, "replica");
  587. } else if (volinfo->type == GF_CLUSTER_TYPE_STRIPE) {
  588. strcpy (vol_type, "stripe");
  589. } else if (volinfo->type == GF_CLUSTER_TYPE_STRIPE_REPLICATE) {
  590. strcpy (vol_type, "stripe-replicate");
  591. } else {
  592. strcpy (vol_type, "distribute");
  593. }
  594. /* Do not allow remove-brick if the volume is plain stripe */
  595. if ((volinfo->type == GF_CLUSTER_TYPE_STRIPE) &&
  596. (volinfo->brick_count == volinfo->stripe_count)) {
  597. snprintf (err_str, sizeof (err_str),
  598. "Removing brick from a plain stripe is not allowed");
  599. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  600. ret = -1;
  601. goto out;
  602. }
  603. if (!replica_count &&
  604. (volinfo->type == GF_CLUSTER_TYPE_STRIPE_REPLICATE) &&
  605. (volinfo->brick_count == volinfo->dist_leaf_count)) {
  606. snprintf (err_str, sizeof(err_str),
  607. "Removing bricks from stripe-replicate"
  608. " configuration is not allowed without reducing "
  609. "replica or stripe count explicitly.");
  610. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  611. ret = -1;
  612. goto out;
  613. }
  614. if (!replica_count &&
  615. (volinfo->type == GF_CLUSTER_TYPE_REPLICATE) &&
  616. (volinfo->brick_count == volinfo->dist_leaf_count)) {
  617. snprintf (err_str, sizeof (err_str),
  618. "Removing bricks from replicate configuration "
  619. "is not allowed without reducing replica count "
  620. "explicitly.");
  621. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  622. ret = -1;
  623. goto out;
  624. }
  625. /* Do not allow remove-brick if the bricks given is less than
  626. the replica count or stripe count */
  627. if (!replica_count && (volinfo->type != GF_CLUSTER_TYPE_NONE)) {
  628. if (volinfo->dist_leaf_count &&
  629. (count % volinfo->dist_leaf_count)) {
  630. snprintf (err_str, sizeof (err_str), "Remove brick "
  631. "incorrect brick count of %d for %s %d",
  632. count, vol_type, volinfo->dist_leaf_count);
  633. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  634. ret = -1;
  635. goto out;
  636. }
  637. }
  638. brick_list = GF_MALLOC (120000 * sizeof(*brick_list),gf_common_mt_char);
  639. if (!brick_list) {
  640. ret = -1;
  641. goto out;
  642. }
  643. strcpy (brick_list, " ");
  644. while ( i <= count) {
  645. snprintf (key, sizeof (key), "brick%d", i);
  646. ret = dict_get_str (dict, key, &brick);
  647. if (ret) {
  648. snprintf (err_str, sizeof (err_str), "Unable to get %s",
  649. key);
  650. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  651. goto out;
  652. }
  653. gf_log (this->name, GF_LOG_DEBUG, "Remove brick count %d brick:"
  654. " %s", i, brick);
  655. ret = glusterd_volume_brickinfo_get_by_brick(brick, volinfo,
  656. &brickinfo);
  657. if (ret) {
  658. snprintf (err_str, sizeof (err_str), "Incorrect brick "
  659. "%s for volume %s", brick, volname);
  660. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  661. goto out;
  662. }
  663. strcat(brick_list, brick);
  664. strcat(brick_list, " ");
  665. i++;
  666. if ((volinfo->type == GF_CLUSTER_TYPE_NONE) ||
  667. (volinfo->brick_count <= volinfo->dist_leaf_count))
  668. continue;
  669. if (replica_count) {
  670. /* do the validation of bricks here */
  671. /* -2 because i++ is already done, and i starts with 1,
  672. instead of 0 */
  673. diff_count = (volinfo->replica_count - replica_count);
  674. brick_index = (((i -2) / diff_count) * volinfo->replica_count);
  675. tmp_brick_idx = 0;
  676. found = 0;
  677. list_for_each_entry (tmp, &volinfo->bricks, brick_list) {
  678. tmp_brick_idx++;
  679. gf_log (this->name, GF_LOG_TRACE,
  680. "validate brick %s:%s (%d %d %d)",
  681. tmp->hostname, tmp->path, tmp_brick_idx,
  682. brick_index, volinfo->replica_count);
  683. if (tmp_brick_idx <= brick_index)
  684. continue;
  685. if (tmp_brick_idx >
  686. (brick_index + volinfo->replica_count))
  687. break;
  688. if ((!strcmp (tmp->hostname,brickinfo->hostname)) &&
  689. !strcmp (tmp->path, brickinfo->path)) {
  690. found = 1;
  691. break;
  692. }
  693. }
  694. if (found)
  695. continue;
  696. snprintf (err_str, sizeof (err_str), "Bricks are from "
  697. "same subvol");
  698. gf_log (this->name, GF_LOG_INFO,
  699. "failed to validate brick %s:%s (%d %d %d)",
  700. tmp->hostname, tmp->path, tmp_brick_idx,
  701. brick_index, volinfo->replica_count);
  702. ret = -1;
  703. /* brick order is not valid */
  704. goto out;
  705. }
  706. pos = 0;
  707. list_for_each_entry (tmp, &volinfo->bricks, brick_list) {
  708. if (strcmp (tmp->hostname,brickinfo->hostname) ||
  709. strcmp (tmp->path, brickinfo->path)) {
  710. pos++;
  711. continue;
  712. }
  713. gf_log (this->name, GF_LOG_INFO, "Found brick");
  714. if (!sub_volume && (volinfo->dist_leaf_count > 1)) {
  715. sub_volume = (pos / volinfo->dist_leaf_count) + 1;
  716. sub_volume_start = (volinfo->dist_leaf_count *
  717. (sub_volume - 1));
  718. sub_volume_end = (volinfo->dist_leaf_count *
  719. sub_volume) - 1;
  720. } else {
  721. if (pos < sub_volume_start ||
  722. pos >sub_volume_end) {
  723. ret = -1;
  724. snprintf (err_str, sizeof (err_str),
  725. "Bricks not from same subvol "
  726. "for %s", vol_type);
  727. gf_log (this->name, GF_LOG_ERROR,
  728. "%s", err_str);
  729. goto out;
  730. }
  731. }
  732. break;
  733. }
  734. }
  735. ret = glusterd_op_begin_synctask (req, GD_OP_REMOVE_BRICK, dict);
  736. out:
  737. if (ret) {
  738. rsp.op_ret = -1;
  739. rsp.op_errno = 0;
  740. if (err_str[0] == '\0')
  741. snprintf (err_str, sizeof (err_str),
  742. "Operation failed");
  743. gf_log (this->name, GF_LOG_ERROR, "%s", err_str);
  744. rsp.op_errstr = err_str;
  745. cli_rsp = &rsp;
  746. glusterd_to_cli (req, cli_rsp, NULL, 0, NULL,
  747. (xdrproc_t)xdr_gf_cli_rsp, dict);
  748. if (dict)
  749. dict_unref (dict);
  750. ret = 0; //sent error to cli, prevent second reply
  751. }
  752. GF_FREE (brick_list);
  753. free (cli_req.dict.dict_val); //its malloced by xdr
  754. return ret;
  755. }
  756. /* op-sm */
  757. int
  758. glusterd_op_perform_add_bricks (glusterd_volinfo_t *volinfo, int32_t count,
  759. char *bricks, dict_t *dict)
  760. {
  761. glusterd_brickinfo_t *brickinfo = NULL;
  762. char *brick = NULL;
  763. int32_t i = 1;
  764. char *brick_list = NULL;
  765. char *free_ptr1 = NULL;
  766. char *free_ptr2 = NULL;
  767. char *saveptr = NULL;
  768. int32_t ret = -1;
  769. int32_t stripe_count = 0;
  770. int32_t replica_count = 0;
  771. int32_t type = 0;
  772. GF_ASSERT (volinfo);
  773. if (bricks) {
  774. brick_list = gf_strdup (bricks);
  775. free_ptr1 = brick_list;
  776. }
  777. if (count)
  778. brick = strtok_r (brick_list+1, " \n", &saveptr);
  779. if (dict) {
  780. ret = dict_get_int32 (dict, "stripe-count", &stripe_count);
  781. if (!ret)
  782. gf_log (THIS->name, GF_LOG_INFO,
  783. "stripe-count is set %d", stripe_count);
  784. ret = dict_get_int32 (dict, "replica-count", &replica_count);
  785. if (!ret)
  786. gf_log (THIS->name, GF_LOG_INFO,
  787. "replica-count is set %d", replica_count);
  788. ret = dict_get_int32 (dict, "type", &type);
  789. if (!ret)
  790. gf_log (THIS->name, GF_LOG_INFO,
  791. "type is set %d, need to change it", type);
  792. }
  793. while ( i <= count) {
  794. ret = glusterd_brickinfo_new_from_brick (brick, &brickinfo);
  795. if (ret)
  796. goto out;
  797. ret = glusterd_resolve_brick (brickinfo);
  798. if (ret)
  799. goto out;
  800. if (stripe_count || replica_count) {
  801. add_brick_at_right_order (brickinfo, volinfo, (i - 1),
  802. stripe_count, replica_count);
  803. } else {
  804. list_add_tail (&brickinfo->brick_list, &volinfo->bricks);
  805. }
  806. brick = strtok_r (NULL, " \n", &saveptr);
  807. i++;
  808. volinfo->brick_count++;
  809. }
  810. /* Gets changed only if the options are given in add-brick cli */
  811. if (type)
  812. volinfo->type = type;
  813. if (replica_count) {
  814. volinfo->replica_count = replica_count;
  815. }
  816. if (stripe_count) {
  817. volinfo->stripe_count = stripe_count;
  818. }
  819. volinfo->dist_leaf_count = (volinfo->stripe_count *
  820. volinfo->replica_count);
  821. /* backward compatibility */
  822. volinfo->sub_count = ((volinfo->dist_leaf_count == 1) ? 0:
  823. volinfo->dist_leaf_count);
  824. ret = glusterd_create_volfiles_and_notify_services (volinfo);
  825. if (ret)
  826. goto out;
  827. ret = 0;
  828. if (GLUSTERD_STATUS_STARTED != volinfo->status)
  829. goto out;
  830. brick_list = gf_strdup (bricks);
  831. free_ptr2 = brick_list;
  832. i = 1;
  833. if (count)
  834. brick = strtok_r (brick_list+1, " \n", &saveptr);
  835. while (i <= count) {
  836. ret = glusterd_volume_brickinfo_get_by_brick (brick, volinfo,
  837. &brickinfo);
  838. if (ret)
  839. goto out;
  840. ret = glusterd_brick_start (volinfo, brickinfo,
  841. _gf_true);
  842. if (ret)
  843. goto out;
  844. i++;
  845. brick = strtok_r (NULL, " \n", &saveptr);
  846. }
  847. out:
  848. GF_FREE (free_ptr1);
  849. GF_FREE (free_ptr2);
  850. gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
  851. return ret;
  852. }
  853. int
  854. glusterd_op_perform_remove_brick (glusterd_volinfo_t *volinfo, char *brick,
  855. int force, int *need_migrate)
  856. {
  857. glusterd_brickinfo_t *brickinfo = NULL;
  858. int32_t ret = -1;
  859. glusterd_conf_t *priv = NULL;
  860. GF_ASSERT (volinfo);
  861. GF_ASSERT (brick);
  862. priv = THIS->private;
  863. GF_ASSERT (priv);
  864. ret = glusterd_volume_brickinfo_get_by_brick (brick, volinfo,
  865. &brickinfo);
  866. if (ret)
  867. goto out;
  868. ret = glusterd_resolve_brick (brickinfo);
  869. if (ret)
  870. goto out;
  871. glusterd_volinfo_reset_defrag_stats (volinfo);
  872. if (!uuid_compare (brickinfo->uuid, MY_UUID)) {
  873. /* Only if the brick is in this glusterd, do the rebalance */
  874. if (need_migrate)
  875. *need_migrate = 1;
  876. }
  877. if (force) {
  878. ret = glusterd_brick_stop (volinfo, brickinfo,
  879. _gf_true);
  880. if (ret) {
  881. gf_log (THIS->name, GF_LOG_ERROR, "Unable to stop "
  882. "glusterfs, ret: %d", ret);
  883. }
  884. goto out;
  885. }
  886. brickinfo->decommissioned = 1;
  887. ret = 0;
  888. out:
  889. gf_log ("", GF_LOG_DEBUG, "Returning %d", ret);
  890. return ret;
  891. }
  892. int
  893. glusterd_op_stage_add_brick (dict_t *dict, char **op_errstr)
  894. {
  895. int ret = 0;
  896. char *volname = NULL;
  897. int count = 0;
  898. int i = 0;
  899. char *bricks = NULL;
  900. char *brick_list = NULL;
  901. char *saveptr = NULL;
  902. char *free_ptr = NULL;
  903. char *brick = NULL;
  904. glusterd_brickinfo_t *brickinfo = NULL;
  905. glusterd_volinfo_t *volinfo = NULL;
  906. glusterd_conf_t *priv = NULL;
  907. char msg[2048] = {0,};
  908. gf_boolean_t brick_alloc = _gf_false;
  909. char *all_bricks = NULL;
  910. char *str_ret = NULL;
  911. priv = THIS->private;
  912. if (!priv)
  913. goto out;
  914. ret = dict_get_str (dict, "volname", &volname);
  915. if (ret) {
  916. gf_log (THIS->name, GF_LOG_ERROR,
  917. "Unable to get volume name");
  918. goto out;
  919. }
  920. ret = glusterd_volinfo_find (volname, &volinfo);
  921. if (ret) {
  922. gf_log (THIS->name, GF_LOG_ERROR,
  923. "Unable to find volume: %s", volname);
  924. goto out;
  925. }
  926. if (volinfo->backend == GD_VOL_BK_BD) {
  927. snprintf (msg, sizeof (msg), "Add brick is not supported for "
  928. "Block backend volume %s.", volname);
  929. gf_log (THIS->name, GF_LOG_ERROR, "%s", msg);
  930. *op_errstr = gf_strdup (msg);
  931. ret = -1;
  932. goto out;
  933. }
  934. ret = glusterd_validate_volume_id (dict, volinfo);
  935. if (ret)
  936. goto out;
  937. if (glusterd_is_rb_ongoing (volinfo)) {
  938. snprintf (msg, sizeof (msg), "Replace brick is in progress on "
  939. "volume %s. Please retry after replace-brick "
  940. "operation is committed or aborted", volname);
  941. gf_log (THIS->name, GF_LOG_ERROR, "%s", msg);
  942. *op_errstr = gf_strdup (msg);
  943. ret = -1;
  944. goto out;
  945. }
  946. if (glusterd_is_defrag_on(volinfo)) {
  947. snprintf (msg, sizeof(msg), "Volume name %s rebalance is in "
  948. "progress. Please retry after completion", volname);
  949. gf_log (THIS->name, GF_LOG_ERROR, "%s", msg);
  950. *op_errstr = gf_strdup (msg);
  951. ret = -1;
  952. goto out;
  953. }
  954. ret = dict_get_int32 (dict, "count", &count);
  955. if (ret) {
  956. gf_log ("", GF_LOG_ERROR, "Unable to get count");
  957. goto out;
  958. }
  959. ret = dict_get_str (dict, "bricks", &bricks);
  960. if (ret) {
  961. gf_log (THIS->name, GF_LOG_ERROR, "Unable to get bricks");
  962. goto out;
  963. }
  964. if (bricks) {
  965. brick_list = gf_strdup (bricks);
  966. all_bricks = gf_strdup (bricks);
  967. free_ptr = brick_list;
  968. }
  969. if (count)
  970. brick = strtok_r (brick_list+1, " \n", &saveptr);
  971. while ( i < count) {
  972. if (!glusterd_store_is_valid_brickpath (volname, brick) ||
  973. !glusterd_is_valid_volfpath (volname, brick)) {
  974. snprintf (msg, sizeof (msg), "brick path %s is "
  975. "too long", brick);
  976. gf_log (THIS->name, GF_LOG_ERROR, "%s", msg);
  977. *op_errstr = gf_strdup (msg);
  978. ret = -1;
  979. goto out;
  980. }
  981. ret = glusterd_brickinfo_new_from_brick (brick, &brickinfo);
  982. if (ret) {
  983. gf_log (THIS->name, GF_LOG_ERROR,
  984. "Add-brick: Unable"
  985. " to get brickinfo");
  986. goto out;
  987. }
  988. brick_alloc = _gf_true;
  989. ret = glusterd_new_brick_validate (brick, brickinfo, msg,
  990. sizeof (msg));
  991. if (ret) {
  992. *op_errstr = gf_strdup (msg);
  993. ret = -1;
  994. goto out;
  995. }
  996. if (!uuid_compare (brickinfo->uuid, MY_UUID)) {
  997. ret = glusterd_brick_create_path (brickinfo->hostname,
  998. brickinfo->path,
  999. volinfo->volume_id,
  1000. op_errstr);
  1001. if (ret)
  1002. goto out;
  1003. }
  1004. glusterd_brickinfo_delete (brickinfo);
  1005. brick_alloc = _gf_false;
  1006. brickinfo = NULL;
  1007. brick = strtok_r (NULL, " \n", &saveptr);
  1008. i++;
  1009. }
  1010. out:
  1011. GF_FREE (free_ptr);
  1012. if (brick_alloc && brickinfo)
  1013. glusterd_brickinfo_delete (brickinfo);
  1014. GF_FREE (str_ret);
  1015. GF_FREE (all_bricks);
  1016. gf_log (THIS->name, GF_LOG_DEBUG, "Returning %d", ret);
  1017. return ret;
  1018. }
  1019. int
  1020. glusterd_op_stage_remove_brick (dict_t *dict, char **op_errstr)
  1021. {
  1022. int ret = -1;
  1023. char *volname = NULL;
  1024. glusterd_volinfo_t *volinfo = NULL;
  1025. char *errstr = NULL;
  1026. int32_t brick_count = 0;
  1027. char msg[2048] = {0,};
  1028. int32_t flag = 0;
  1029. gf1_op_commands cmd = GF_OP_CMD_NONE;
  1030. char *task_id_str = NULL;
  1031. xlator_t *this = NULL;
  1032. this = THIS;
  1033. GF_ASSERT (this);
  1034. ret = dict_get_str (dict, "volname", &volname);
  1035. if (ret) {
  1036. gf_log (this->name, GF_LOG_ERROR, "Unable to get volume name");
  1037. goto out;
  1038. }
  1039. ret = glusterd_volinfo_find (volname, &volinfo);
  1040. if (ret) {
  1041. gf_log (this->name, GF_LOG_ERROR, "Volume %s does not exist", volname);
  1042. goto out;
  1043. }
  1044. ret = glusterd_validate_volume_id (dict, volinfo);
  1045. if (ret)
  1046. goto out;
  1047. if (glusterd_is_rb_ongoing (volinfo)) {
  1048. snprintf (msg, sizeof (msg), "Replace brick is in progress on "
  1049. "volume %s. Please retry after replace-brick "
  1050. "operation is committed or aborted", volname);
  1051. gf_log (this->name, GF_LOG_ERROR, "%s", msg);
  1052. *op_errstr = gf_strdup (msg);
  1053. ret = -1;
  1054. goto out;
  1055. }
  1056. ret = dict_get_int32 (dict, "command", &flag);
  1057. if (ret) {
  1058. gf_log (this->name, GF_LOG_ERROR, "Unable to get brick count");
  1059. goto out;
  1060. }
  1061. cmd = flag;
  1062. ret = -1;
  1063. switch (cmd) {
  1064. case GF_OP_CMD_NONE:
  1065. errstr = gf_strdup ("no remove-brick command issued");
  1066. goto out;
  1067. case GF_OP_CMD_STATUS:
  1068. ret = 0;
  1069. goto out;
  1070. case GF_OP_CMD_START:
  1071. {
  1072. if (GLUSTERD_STATUS_STARTED != volinfo->status) {
  1073. snprintf (msg, sizeof (msg), "Volume %s needs to be "
  1074. "started before remove-brick (you can use "
  1075. "'force' or 'commit' to override this "
  1076. "behavior)", volinfo->volname);
  1077. errstr = gf_strdup (msg);
  1078. gf_log (this->name, GF_LOG_ERROR, "%s", errstr);
  1079. goto out;
  1080. }

Large files files are truncated, but you can click here to view the full file