summaryrefslogtreecommitdiffstats
path: root/source3/utils/smbget.c
blob: 28d56478d260b9cb31ffdb323b9bdfc1a7add7d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
   smbget: a wget-like utility with support for recursive downloading and 
   	smb:// urls
   Copyright (C) 2003-2004 Jelmer Vernooij <jelmer@samba.org>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "includes.h"
#include "system/filesys.h"
#include "popt_common.h"
#include "libsmbclient.h"

#if _FILE_OFFSET_BITS==64
#define OFF_T_FORMAT "%lld"
#define OFF_T_FORMAT_CAST long long
#else
#define OFF_T_FORMAT "%ld"
#define OFF_T_FORMAT_CAST long
#endif

static int columns = 0;

static int debuglevel, update;
static char *outputfile;


static time_t total_start_time = 0;
static off_t total_bytes = 0;

#define SMB_MAXPATHLEN MAXPATHLEN

/* Number of bytes to read when checking whether local and remote file are really the same file */
#define RESUME_CHECK_SIZE 				512
#define RESUME_DOWNLOAD_OFFSET			1024
#define RESUME_CHECK_OFFSET				RESUME_DOWNLOAD_OFFSET+RESUME_CHECK_SIZE
/* Number of bytes to read at once */
#define SMB_DEFAULT_BLOCKSIZE 					64000

static const char *username = NULL, *password = NULL, *workgroup = NULL;
static int nonprompt = 0, quiet = 0, dots = 0, keep_permissions = 0, verbose = 0, send_stdout = 0;
static int blocksize = SMB_DEFAULT_BLOCKSIZE;

static int smb_download_file(const char *base, const char *name, int recursive,
			     int resume, int toplevel, char *outfile);

static int get_num_cols(void)
{
#ifdef TIOCGWINSZ
	struct winsize ws;
	if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) < 0) {
		return 0;
	}
	return ws.ws_col;
#else
#warning No support for TIOCGWINSZ
	char *cols = getenv("COLUMNS");
	if(!cols) return 0;
	return atoi(cols);
#endif
}

static void change_columns(int sig)
{
	columns = get_num_cols();
}

static void human_readable(off_t s, char *buffer, int l)
{
	if (s > 1024 * 1024 * 1024) {
		snprintf(buffer, l, "%.2fGB", 1.0 * s / (1024 * 1024 * 1024));
	} else if (s > 1024 * 1024) {
		snprintf(buffer, l, "%.2fMB", 1.0 * s / (1024 * 1024));
	} else if (s > 1024) {
		snprintf(buffer, l, "%.2fkB", 1.0 * s / 1024);
	} else {
		snprintf(buffer, l, OFF_T_FORMAT"b", (OFF_T_FORMAT_CAST)s);
	}
}

static void get_auth_data(const char *srv, const char *shr, char *wg, int wglen, char *un, int unlen, char *pw, int pwlen)
{
	static char hasasked = 0;
	char *wgtmp, *usertmp;
	char tmp[128];

	if(hasasked) return;
	hasasked = 1;

	if(!nonprompt && !username) {
		printf("Username for %s at %s [guest] ", shr, srv);
		if (fgets(tmp, sizeof(tmp), stdin) == NULL) {
			return;
		}
		if ((strlen(tmp) > 0) && (tmp[strlen(tmp)-1] == '\n')) {
			tmp[strlen(tmp)-1] = '\0';
		}
		strncpy(un, tmp, unlen-1);
	} else if(username) strncpy(un, username, unlen-1);

	if(!nonprompt && !password) {
		char *prompt;
		if (asprintf(&prompt, "Password for %s at %s: ", shr, srv) == -1) {
			return;
		}
		(void) samba_getpass(prompt, pw, pwlen, false, false);
		free(prompt);
	} else if(password) strncpy(pw, password, pwlen-1);

	if(workgroup)strncpy(wg, workgroup, wglen-1);

	wgtmp = SMB_STRNDUP(wg, wglen); 
	usertmp = SMB_STRNDUP(un, unlen);
	if(!quiet)printf("Using workgroup %s, %s%s\n", wgtmp, *usertmp?"user ":"guest user", usertmp);
	free(wgtmp); free(usertmp);
}

/* Return 1 on error, 0 on success. */

static int smb_download_dir(const char *base, const char *name, int resume)
{
	char path[SMB_MAXPATHLEN];
	int dirhandle;
	struct smbc_dirent *dirent;
	const char *relname = name;
	char *tmpname;
	struct stat remotestat;
	int ret = 0;

	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (base[0] && name[0] && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);

	/* List files in directory and call smb_download_file on them */
	dirhandle = smbc_opendir(path);
	if(dirhandle < 1) {
		if (errno == ENOTDIR) {
			return smb_download_file(base, name, 1, resume,
						 0, NULL);
		}
		fprintf(stderr, "Can't open directory %s: %s\n", path, strerror(errno));
		return 1;
	}

	while(*relname == '/')relname++;
	mkdir(relname, 0755);

	tmpname = SMB_STRDUP(name);

	while((dirent = smbc_readdir(dirhandle))) {
		char *newname;
		if(!strcmp(dirent->name, ".") || !strcmp(dirent->name, ".."))continue;
		if (asprintf(&newname, "%s/%s", tmpname, dirent->name) == -1) {
			free(tmpname);
			return 1;
		}
		switch(dirent->smbc_type) {
		case SMBC_DIR:
			ret = smb_download_dir(base, newname, resume);
			break;

		case SMBC_WORKGROUP:
			ret = smb_download_dir("smb://", dirent->name, resume);
			break;

		case SMBC_SERVER:
			ret = smb_download_dir("smb://", dirent->name, resume);
			break;

		case SMBC_FILE:
			ret = smb_download_file(base, newname, 1, resume, 0,
						NULL);
			break;

		case SMBC_FILE_SHARE:
			ret = smb_download_dir(base, newname, resume);
			break;

		case SMBC_PRINTER_SHARE:
			if(!quiet)printf("Ignoring printer share %s\n", dirent->name);
			break;

		case SMBC_COMMS_SHARE:
			if(!quiet)printf("Ignoring comms share %s\n", dirent->name);
			break;

		case SMBC_IPC_SHARE:
			if(!quiet)printf("Ignoring ipc$ share %s\n", dirent->name);
			break;

		default:
			fprintf(stderr, "Ignoring file '%s' of type '%d'\n", newname, dirent->smbc_type);
			break;
		}
		free(newname);
	}
	free(tmpname);

	if(keep_permissions) {
		if(smbc_fstat(dirhandle, &remotestat) < 0) {
			fprintf(stderr, "Unable to get stats on %s on remote server\n", path);
			smbc_closedir(dirhandle);
			return 1;
		}

		if(chmod(relname, remotestat.st_mode) < 0) {
			fprintf(stderr, "Unable to change mode of local dir %s to %o\n", relname,
				(unsigned int)remotestat.st_mode);
			smbc_closedir(dirhandle);
			return 1;
		}
	}

	smbc_closedir(dirhandle);
	return ret;
}

static char *print_time(long t)
{
	static char buffer[100];
	int secs, mins, hours;
	if(t < -1) {
		strncpy(buffer, "Unknown", sizeof(buffer));
		return buffer;
	}

	secs = (int)t % 60;
	mins = (int)t / 60 % 60;
	hours = (int)t / (60 * 60);
	snprintf(buffer, sizeof(buffer)-1, "%02d:%02d:%02d", hours, mins, secs);
	return buffer;
}

static void print_progress(const char *name, time_t start, time_t now, off_t start_pos, off_t pos, off_t total)
{
	double avg = 0.0;
	long  eta = -1; 
	double prcnt = 0.0;
	char hpos[20], htotal[20], havg[20];
	char *status, *filename;
	int len;
	if(now - start)avg = 1.0 * (pos - start_pos) / (now - start);
	eta = (total - pos) / avg;
	if(total)prcnt = 100.0 * pos / total;

	human_readable(pos, hpos, sizeof(hpos));
	human_readable(total, htotal, sizeof(htotal));
	human_readable(avg, havg, sizeof(havg));

	len = asprintf(&status, "%s of %s (%.2f%%) at %s/s ETA: %s", hpos, htotal, prcnt, havg, print_time(eta));
	if (len == -1) {
		return;
	}

	if(columns) {
		int required = strlen(name), available = columns - len - strlen("[] ");
		if(required > available) {
			if (asprintf(&filename, "...%s", name + required - available + 3) == -1) {
				return;
			}
		} else {
			filename = SMB_STRNDUP(name, available);
		}
	} else filename = SMB_STRDUP(name);

	fprintf(stderr, "\r[%s] %s", filename, status);

	free(filename); free(status);
}

/* Return 1 on error, 0 on success. */

static int smb_download_file(const char *base, const char *name, int recursive,
			     int resume, int toplevel, char *outfile)
{
	int remotehandle, localhandle;
	time_t start_time = time_mono(NULL);
	const char *newpath;
	char path[SMB_MAXPATHLEN];
	char checkbuf[2][RESUME_CHECK_SIZE];
	char *readbuf = NULL;
	off_t offset_download = 0, offset_check = 0, curpos = 0, start_offset = 0;
	struct stat localstat, remotestat;

	snprintf(path, SMB_MAXPATHLEN-1, "%s%s%s", base, (*base && *name && name[0] != '/' && base[strlen(base)-1] != '/')?"/":"", name);

	remotehandle = smbc_open(path, O_RDONLY, 0755);

	if(remotehandle < 0) {
		switch(errno) {
		case EISDIR: 
			if(!recursive) {
				fprintf(stderr, "%s is a directory. Specify -R to download recursively\n", path);
				return 1;
			}
			return smb_download_dir(base, name, resume);

		case ENOENT:
			fprintf(stderr, "%s can't be found on the remote server\n", path);
			return 1;

		case ENOMEM:
			fprintf(stderr, "Not enough memory\n");
			return 1;

		case ENODEV:
			fprintf(stderr, "The share name used in %s does not exist\n", path);
			return 1;

		case EACCES:
			fprintf(stderr, "You don't have enough permissions to access %s\n", path);
			return 1;

		default:
			perror("smbc_open");
			return 1;
		}
	} 

	if(smbc_fstat(remotehandle, &remotestat) < 0) {
		fprintf(stderr, "Can't stat %s: %s\n", path, strerror(errno));
		return 1;
	}

	if(outfile) newpath = outfile;
	else if(!name[0]) {
		newpath = strrchr(base, '/');
		if(newpath)newpath++; else newpath = base;
	} else newpath = name;

	if (!toplevel && (newpath[0] == '/')) {
		newpath++;
	}

	/* Open local file according to the mode */
	if(update) {
		/* if it is up-to-date, skip */
		if(stat(newpath, &localstat) == 0 &&
				localstat.st_mtime >= remotestat.st_mtime) {
			if(verbose)
				printf("%s is up-to-date, skipping\n", newpath);
			smbc_close(remotehandle);
			return 0;
		}
		/* else open it for writing and truncate if it exists */
		localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | O_TRUNC, 0775);
		if(localhandle < 0) {
			fprintf(stderr, "Can't open %s : %s\n", newpath,
					strerror(errno));
			smbc_close(remotehandle);
			return 1;
		}
		/* no offset */
	} else if(!send_stdout) {
		localhandle = open(newpath, O_CREAT | O_NONBLOCK | O_RDWR | (!resume?O_EXCL:0), 0755);
		if(localhandle < 0) {
			fprintf(stderr, "Can't open %s: %s\n", newpath, strerror(errno));
			smbc_close(remotehandle);
			return 1;
		}

		if (fstat(localhandle, &localstat) != 0) {
			fprintf(stderr, "Can't fstat %s: %s\n", newpath, strerror(errno));
			smbc_close(remotehandle);
			close(localhandle);
			return 1;
		}

		start_offset = localstat.st_size;

		if(localstat.st_size && localstat.st_size == remotestat.st_size) {
			if(verbose)fprintf(stderr, "%s is already downloaded completely.\n", path);
			else if(!quiet)fprintf(stderr, "%s\n", path);
			smbc_close(remotehandle);
			close(localhandle);
			return 0;
		}

		if(localstat.st_size > RESUME_CHECK_OFFSET && remotestat.st_size > RESUME_CHECK_OFFSET) {
			offset_download = localstat.st_size - RESUME_DOWNLOAD_OFFSET;
			offset_check = localstat.st_size - RESUME_CHECK_OFFSET;
			if(verbose)printf("Trying to start resume of %s at "OFF_T_FORMAT"\n"
				   "At the moment "OFF_T_FORMAT" of "OFF_T_FORMAT" bytes have been retrieved\n",
				newpath, (OFF_T_FORMAT_CAST)offset_check, 
				(OFF_T_FORMAT_CAST)localstat.st_size,
				(OFF_T_FORMAT_CAST)remotestat.st_size);
		}

		if(offset_check) { 
			off_t off1, off2;
			/* First, check all bytes from offset_check to offset_download */
			off1 = lseek(localhandle, offset_check, SEEK_SET);
			if(off1 < 0) {
				fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in local file %s\n",
					(OFF_T_FORMAT_CAST)offset_check, newpath);
				smbc_close(remotehandle); close(localhandle);
				return 1;
			}

			off2 = smbc_lseek(remotehandle, offset_check, SEEK_SET); 
			if(off2 < 0) {
				fprintf(stderr, "Can't seek to "OFF_T_FORMAT" in remote file %s\n",
					(OFF_T_FORMAT_CAST)offset_check, newpath);
				smbc_close(remotehandle); close(localhandle);
				return 1;
			}

			if(off1 != off2) {
				fprintf(stderr, "Offset in local and remote files is different (local: "OFF_T_FORMAT", remote: "OFF_T_FORMAT")\n",
					(OFF_T_FORMAT_CAST)off1,
					(OFF_T_FORMAT_CAST)off2);
				smbc_close(remotehandle); close(localhandle);
				return 1;
			}

			if(smbc_read(remotehandle, checkbuf[0], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
				fprintf(stderr, "Can't read %d bytes from remote file %s\n", RESUME_CHECK_SIZE, path);
				smbc_close(remotehandle); close(localhandle);
				return 1;
			}

			if(read(localhandle, checkbuf[1], RESUME_CHECK_SIZE) != RESUME_CHECK_SIZE) {
				fprintf(stderr, "Can't read %d bytes from local file %s\n", RESUME_CHECK_SIZE, name);
				smbc_close(remotehandle); close(localhandle);
				return 1;
			}

			if(memcmp(checkbuf[0], checkbuf[1], RESUME_CHECK_SIZE) == 0) {
				if(verbose)printf("Current local and remote file appear to be the same. Starting download from offset "OFF_T_FORMAT"\n", (OFF_T_FORMAT_CAST)offset_download);
			} else {
				fprintf(stderr, "Local and remote file appear to be different, not doing resume for %s\n", path);
				smbc_close(remotehandle); close(localhandle);
				return 1;
			}
		}
	} else {
		localhandle = STDOUT_FILENO;
		start_offset = 0;
		offset_download = 0;
		offset_check = 0;
	}

	readbuf = (char *)SMB_MALLOC(blocksize);
	if (!readbuf) {
		if (localhandle != STDOUT_FILENO) {
			close(localhandle);
		}
		return 1;
	}

	/* Now, download all bytes from offset_download to the end */
	for(curpos = offset_download; curpos < remotestat.st_size; curpos+=blocksize) {
		ssize_t bytesread = smbc_read(remotehandle, readbuf, blocksize);
		if(bytesread < 0) {
			fprintf(stderr, "Can't read %u bytes at offset "OFF_T_FORMAT", file %s\n", (unsigned int)blocksize, (OFF_T_FORMAT_CAST)curpos, path);
			smbc_close(remotehandle);
			if (localhandle != STDOUT_FILENO) close(localhandle);
			free(readbuf);
			return 1;
		}

		total_bytes += bytesread;

		if(write(localhandle, readbuf, bytesread) < 0) {
			fprintf(stderr, "Can't write %u bytes to local file %s at offset "OFF_T_FORMAT"\n", (unsigned int)bytesread, path, (OFF_T_FORMAT_CAST)curpos);
			free(readbuf);
			smbc_close(remotehandle);
			if (localhandle != STDOUT_FILENO) close(localhandle);
			return 1;
		}

		if(dots)fputc('.', stderr);
		else if(!quiet) {
			print_progress(newpath, start_time, time_mono(NULL),
					start_offset, curpos, remotestat.st_size);
		}
	}

	free(readbuf);

	if(dots){
		fputc('\n', stderr);
		printf("%s downloaded\n", path);
	} else if(!quiet) {
		int i;
		fprintf(stderr, "\r%s", path);
		if(columns) {
			for(i = strlen(path); i < columns; i++) {
				fputc(' ', stderr);
			}
		}
		fputc('\n', stderr);
	}

	if(keep_permissions && !send_stdout) {
		if(fchmod(localhandle, remotestat.st_mode) < 0) {
			fprintf(stderr, "Unable to change mode of local file %s to %o\n", path,
				(unsigned int)remotestat.st_mode);
			smbc_close(remotehandle);
			close(localhandle);
			return 1;
		}
	}

	smbc_close(remotehandle);
	if (localhandle != STDOUT_FILENO) close(localhandle);
	return 0;
}

static void clean_exit(void)
{
	char bs[100];
	human_readable(total_bytes, bs, sizeof(bs));
	if(!quiet)fprintf(stderr, "Downloaded %s in %lu seconds\n", bs,
		(unsigned long)(time_mono(NULL) - total_start_time));
	exit(0);
}

static void signal_quit(int v)
{
	clean_exit();
}

static int readrcfile(const char *name, const struct poptOption long_options[])
{
	FILE *fd = fopen(name, "r");
	int lineno = 0, i;
	char var[101], val[101];
	char found;
	int *intdata; char **stringdata;
	if(!fd) {
		fprintf(stderr, "Can't open RC file %s\n", name);
		return 1;
	}

	while(!feof(fd)) {
		lineno++;
		if(fscanf(fd, "%100s %100s\n", var, val) < 2) {
			fprintf(stderr, "Can't parse line %d of %s, ignoring.\n", lineno, name);
			continue;
		}

		found = 0;

		for(i = 0; long_options[i].shortName; i++) {
			if(!long_options[i].longName)continue;
			if(strcmp(long_options[i].longName, var)) continue;
			if(!long_options[i].arg)continue;

			switch(long_options[i].argInfo) {
			case POPT_ARG_NONE:
				intdata = (int *)long_options[i].arg;
				if(!strcmp(val, "on")) *intdata = 1;
				else if(!strcmp(val, "off")) *intdata = 0;
				else fprintf(stderr, "Illegal value %s for %s at line %d in %s\n", val, var, lineno, name);
				break;
			case POPT_ARG_INT:
				intdata = (int *)long_options[i].arg;
				*intdata = atoi(val);
				break;
			case POPT_ARG_STRING:
				stringdata = (char **)long_options[i].arg;
				*stringdata = SMB_STRDUP(val);
				break;
			default:
				fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
				break;
			}

			found = 1;
		}
		if(!found) {
			fprintf(stderr, "Invalid variable %s at line %d in %s\n", var, lineno, name);
		}
	}

	fclose(fd);
	return 0;
}

int main(int argc, const char **argv)
{
	int c = 0;
	const char *file = NULL;
	char *rcfile = NULL;
	bool smb_encrypt = false;
	int resume = 0, recursive = 0;
	TALLOC_CTX *frame = talloc_stackframe();
	int ret = 0;
	struct poptOption long_options[] = {
		{"guest", 'a', POPT_ARG_NONE, NULL, 'a', "Work as user guest" },	
		{"encrypt", 'e', POPT_ARG_NONE, NULL, 'e', "Encrypt SMB transport (UNIX extended servers only)" },	
		{"resume", 'r', POPT_ARG_NONE, &resume, 0, "Automatically resume aborted files" },
		{"update", 'U',  POPT_ARG_NONE, &update, 0, "Download only when remote file is newer than local file or local file is missing"},
		{"recursive", 'R',  POPT_ARG_NONE, &recursive, 0, "Recursively download files" },
		{"username", 'u', POPT_ARG_STRING, &username, 'u', "Username to use" },
		{"password", 'p', POPT_ARG_STRING, &password, 'p', "Password to use" },
		{"workgroup", 'w', POPT_ARG_STRING, &workgroup, 'w', "Workgroup to use (optional)" },
		{"nonprompt", 'n', POPT_ARG_NONE, &nonprompt, 'n', "Don't ask anything (non-interactive)" },
		{"debuglevel", 'd', POPT_ARG_INT, &debuglevel, 'd', "Debuglevel to use" },
		{"outputfile", 'o', POPT_ARG_STRING, &outputfile, 'o', "Write downloaded data to specified file" },
		{"stdout", 'O', POPT_ARG_NONE, &send_stdout, 'O', "Write data to stdout" },
		{"dots", 'D', POPT_ARG_NONE, &dots, 'D', "Show dots as progress indication" },
		{"quiet", 'q', POPT_ARG_NONE, &quiet, 'q', "Be quiet" },
		{"verbose", 'v', POPT_ARG_NONE, &verbose, 'v', "Be verbose" },
		{"keep-permissions", 'P', POPT_ARG_NONE, &keep_permissions, 'P', "Keep permissions" },
		{"blocksize", 'b', POPT_ARG_INT, &blocksize, 'b', "Change number of bytes in a block"},
		{"rcfile", 'f', POPT_ARG_STRING, NULL, 'f', "Use specified rc file"},
		POPT_AUTOHELP
		POPT_TABLEEND
	};
	poptContext pc;

	load_case_tables();

	/* only read rcfile if it exists */
	if (asprintf(&rcfile, "%s/.smbgetrc", getenv("HOME")) == -1) {
		return 1;
	}
	if(access(rcfile, F_OK) == 0) 
		readrcfile(rcfile, long_options);
	free(rcfile);

#ifdef SIGWINCH
	signal(SIGWINCH, change_columns);
#endif
	signal(SIGINT, signal_quit);
	signal(SIGTERM, signal_quit);

	pc = poptGetContext(argv[0], argc, argv, long_options, 0);

	while((c = poptGetNextOpt(pc)) >= 0) {
		switch(c) {
		case 'f':
			readrcfile(poptGetOptArg(pc), long_options);
			break;
		case 'a':
			username = ""; password = "";
			break;
		case 'e':
			smb_encrypt = true;
			break;
		}
	}

	if((send_stdout || resume || outputfile) && update) {
		fprintf(stderr, "The -o, -R or -O and -U options can not be used together.\n");
		return 1;
	}
	if((send_stdout || outputfile) && recursive) {
		fprintf(stderr, "The -o or -O and -R options can not be used together.\n");
		return 1;
	}

	if(outputfile && send_stdout) {
		fprintf(stderr, "The -o and -O options cannot be used together.\n");
		return 1;
	}

	if(smbc_init(get_auth_data, debuglevel) < 0) {
		fprintf(stderr, "Unable to initialize libsmbclient\n");
		return 1;
	}

	if (smb_encrypt) {
		SMBCCTX *smb_ctx = smbc_set_context(NULL);
		smbc_option_set(smb_ctx,
			discard_const_p(char, "smb_encrypt_level"),
			"require");
	}

	columns = get_num_cols();

	total_start_time = time_mono(NULL);

	while ( (file = poptGetArg(pc)) ) {
		if (!recursive) 
			ret = smb_download_file(file, "", recursive, resume,
						1, outputfile);
		else 
			ret = smb_download_dir(file, "", resume);
	}

	TALLOC_FREE(frame);
	if ( ret == 0){
		clean_exit();
	}
	return ret;
}
3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830
/*
   Copyright (c) 2010-2012 Red Hat, Inc. <http://www.redhat.com>
   This file is part of GlusterFS.

   This file is licensed to you under your choice of the GNU Lesser
   General Public License, version 3 or any later version (LGPLv3 or
   later), or the GNU General Public License, version 2 (GPLv2), in all
   cases as published by the Free Software Foundation.
*/
#include <stdlib.h>
#include "cli.h"
#include "cli1-xdr.h"
#include <glusterfs/run.h>
#include <glusterfs/compat.h>
#include <glusterfs/syscall.h>
#include <glusterfs/upcall-utils.h>

enum gf_task_types { GF_TASK_TYPE_REBALANCE, GF_TASK_TYPE_REMOVE_BRICK };

/*
 * IMPORTANT NOTE:
 * All exported functions in this file which use libxml need use a
 * #if (HAVE_LIB_XML), #else, #endif
 * For eg,
 *      int exported_func () {
 *              #if (HAVE_LIB_XML)
 *                      <Stuff using libxml>
 *              #else
 *                      return 0;
 *              #endif
 *      }
 *
 *  All other functions, which are called internally within this file need to be
 *  within #if (HAVE_LIB_XML), #endif statements
 *  For eg,
 *      #if (HAVE_LIB_XML)
 *      int internal_func ()
 *      {
 *      }
 *      #endif
 *
 *  Following the above format ensures that all xml related code is compiled
 *  only when libxml2 is present, and also keeps the rest of the codebase free
 *  of #if (HAVE_LIB_XML)
 */

#if (HAVE_LIB_XML)

#include <libxml/encoding.h>
#include <libxml/xmlwriter.h>

#define XML_RET_CHECK_AND_GOTO(ret, label)                                     \
    do {                                                                       \
        if (ret < 0) {                                                         \
            ret = -1;                                                          \
            goto label;                                                        \
        } else                                                                 \
            ret = 0;                                                           \
    } while (0)

int
cli_begin_xml_output(xmlTextWriterPtr *writer, xmlDocPtr *doc)
{
    int ret = -1;

    *writer = xmlNewTextWriterDoc(doc, 0);
    if (*writer == NULL) {
        ret = -1;
        goto out;
    }

    ret = xmlTextWriterStartDocument(*writer, "1.0", "UTF-8", "yes");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <cliOutput> */
    ret = xmlTextWriterStartElement(*writer, (xmlChar *)"cliOutput");
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_end_xml_output(xmlTextWriterPtr writer, xmlDocPtr doc)
{
    int ret = -1;

    /* </cliOutput> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterEndDocument(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* Dump xml document to stdout and pretty format it */
    xmlSaveFormatFileEnc("-", doc, "UTF-8", 1);

    xmlFreeTextWriter(writer);
    xmlFreeDoc(doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_common(xmlTextWriterPtr writer, int op_ret, int op_errno,
                      char *op_errstr)
{
    int ret = -1;

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opRet", "%d",
                                          op_ret);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opErrno", "%d",
                                          op_errno);
    XML_RET_CHECK_AND_GOTO(ret, out);

    if (op_errstr)
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opErrstr",
                                              "%s", op_errstr);
    else
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opErrstr",
                                              "%s", "");

    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_str(char *op, char *str, int op_ret, int op_errno,
                   char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    if (op) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"cliOp", "%s",
                                              op);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    if (str) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"output", "%s",
                                              str);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
cli_xml_output_data_pair(dict_t *this, char *key, data_t *value, void *data)
{
    int ret = -1;
    xmlTextWriterPtr *writer = NULL;

    writer = (xmlTextWriterPtr *)data;

    ret = xmlTextWriterWriteFormatElement(*writer, (xmlChar *)key, "%s",
                                          value->data);

    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    return ret;
}
#endif

int
cli_xml_output_dict(char *op, dict_t *dict, int op_ret, int op_errno,
                    char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <"op"> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)op);
    XML_RET_CHECK_AND_GOTO(ret, out);

    if (dict)
        dict_foreach(dict, cli_xml_output_data_pair, &writer);

    /* </"op"> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
cli_xml_output_vol_status_common(xmlTextWriterPtr writer, dict_t *dict,
                                 int brick_index, int *online,
                                 gf_boolean_t *node_present)
{
    int ret = -1;
    char *hostname = NULL;
    char *path = NULL;
    char *uuid = NULL;
    int port = 0;
    int rdma_port = 0;
    int status = 0;
    int pid = 0;
    char key[1024] = {
        0,
    };

    snprintf(key, sizeof(key), "brick%d.hostname", brick_index);
    ret = dict_get_str(dict, key, &hostname);
    if (ret) {
        *node_present = _gf_false;
        goto out;
    }
    *node_present = _gf_true;

    /* <node>
     * will be closed in the calling function cli_xml_output_vol_status()*/
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"node");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname", "%s",
                                          hostname);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.path", brick_index);
    ret = dict_get_str(dict, key, &path);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"path", "%s",
                                          path);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.peerid", brick_index);
    ret = dict_get_str(dict, key, &uuid);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"peerid", "%s",
                                          uuid);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.status", brick_index);
    ret = dict_get_int32(dict, key, &status);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%d",
                                          status);
    XML_RET_CHECK_AND_GOTO(ret, out);
    *online = status;

    snprintf(key, sizeof(key), "brick%d.port", brick_index);
    ret = dict_get_int32(dict, key, &port);
    if (ret)
        goto out;

    snprintf(key, sizeof(key), "brick%d.rdma_port", brick_index);
    ret = dict_get_int32(dict, key, &rdma_port);

    /* If the process is either offline or doesn't provide a port (shd)
     * port = "N/A"
     * else print the port number of the process.
     */

    /*
     * Tag 'port' can be removed once console management is started
     * to support new tag ports.
     */

    if (*online == 1 && port != 0)
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"port", "%d",
                                              port);
    else
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"port", "%s",
                                              "N/A");
    XML_RET_CHECK_AND_GOTO(ret, out);
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"ports");
    if (*online == 1 && (port != 0 || rdma_port != 0)) {
        if (port) {
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"tcp",
                                                  "%d", port);
        } else {
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"tcp",
                                                  "%s", "N/A");
        }
        XML_RET_CHECK_AND_GOTO(ret, out);
        if (rdma_port) {
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"rdma",
                                                  "%d", rdma_port);
        } else {
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"rdma",
                                                  "%s", "N/A");
        }
        XML_RET_CHECK_AND_GOTO(ret, out);
    } else {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"tcp", "%s",
                                              "N/A");
        XML_RET_CHECK_AND_GOTO(ret, out);
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"rdma", "%s",
                                              "N/A");
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.pid", brick_index);
    ret = dict_get_int32(dict, key, &pid);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", "%d", pid);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_detail(xmlTextWriterPtr writer, dict_t *dict,
                                 int brick_index)
{
    int ret = -1;
    uint64_t size_total = 0;
    uint64_t size_free = 0;
    char *device = NULL;
    uint64_t block_size = 0;
    char *mnt_options = NULL;
    char *fs_name = NULL;
    char *inode_size = NULL;
    uint64_t inodes_total = 0;
    uint64_t inodes_free = 0;
    char key[1024] = {
        0,
    };

    snprintf(key, sizeof(key), "brick%d.total", brick_index);
    ret = dict_get_uint64(dict, key, &size_total);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"sizeTotal",
                                              "%" PRIu64, size_total);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.free", brick_index);
    ret = dict_get_uint64(dict, key, &size_free);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"sizeFree",
                                              "%" PRIu64, size_free);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.device", brick_index);
    ret = dict_get_str(dict, key, &device);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"device", "%s",
                                              device);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.block_size", brick_index);
    ret = dict_get_uint64(dict, key, &block_size);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"blockSize",
                                              "%" PRIu64, block_size);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.mnt_options", brick_index);
    ret = dict_get_str(dict, key, &mnt_options);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"mntOptions",
                                              "%s", mnt_options);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.fs_name", brick_index);
    ret = dict_get_str(dict, key, &fs_name);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"fsName", "%s",
                                              fs_name);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.inode_size", brick_index);
    ret = dict_get_str(dict, key, &inode_size);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"inodeSize",
                                              "%s", fs_name);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.total_inodes", brick_index);
    ret = dict_get_uint64(dict, key, &inodes_total);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"inodesTotal",
                                              "%" PRIu64, inodes_total);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    snprintf(key, sizeof(key), "brick%d.free_inodes", brick_index);
    ret = dict_get_uint64(dict, key, &inodes_free);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"inodesFree",
                                              "%" PRIu64, inodes_free);
        XML_RET_CHECK_AND_GOTO(ret, out);
    } else {
        ret = 0;
    }

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_mempool(xmlTextWriterPtr writer, dict_t *dict,
                                  char *prefix)
{
    int ret = -1;
    int mempool_count = 0;
    char *name = NULL;
    int hotcount = 0;
    int coldcount = 0;
    uint64_t paddedsizeof = 0;
    uint64_t alloccount = 0;
    int maxalloc = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <mempool> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"mempool");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.mempool-count", prefix);
    ret = dict_get_int32(dict, key, &mempool_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                          mempool_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < mempool_count; i++) {
        /* <pool> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"pool");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.name", prefix, i);
        ret = dict_get_str(dict, key, &name);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              name);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.hotcount", prefix, i);
        ret = dict_get_int32(dict, key, &hotcount);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hotCount",
                                              "%d", hotcount);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.coldcount", prefix, i);
        ret = dict_get_int32(dict, key, &coldcount);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"coldCount",
                                              "%d", coldcount);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.paddedsizeof", prefix, i);
        ret = dict_get_uint64(dict, key, &paddedsizeof);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            writer, (xmlChar *)"padddedSizeOf", "%" PRIu64, paddedsizeof);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.alloccount", prefix, i);
        ret = dict_get_uint64(dict, key, &alloccount);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"allocCount",
                                              "%" PRIu64, alloccount);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.max_alloc", prefix, i);
        ret = dict_get_int32(dict, key, &maxalloc);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxAlloc",
                                              "%d", maxalloc);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.pool-misses", prefix, i);
        ret = dict_get_uint64(dict, key, &alloccount);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"poolMisses",
                                              "%" PRIu64, alloccount);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.pool%d.max-stdalloc", prefix, i);
        ret = dict_get_int32(dict, key, &maxalloc);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxStdAlloc",
                                              "%d", maxalloc);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </pool> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </mempool> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_mem(xmlTextWriterPtr writer, dict_t *dict,
                              int brick_index)
{
    int ret = -1;
    int arena = 0;
    int ordblks = 0;
    int smblks = 0;
    int hblks = 0;
    int hblkhd = 0;
    int usmblks = 0;
    int fsmblks = 0;
    int uordblks = 0;
    int fordblks = 0;
    int keepcost = 0;
    char key[1024] = {
        0,
    };

    /* <memStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"memStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <mallinfo> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"mallinfo");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.arena", brick_index);
    ret = dict_get_int32(dict, key, &arena);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"arena", "%d",
                                          arena);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.ordblks", brick_index);
    ret = dict_get_int32(dict, key, &ordblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"ordblks", "%d",
                                          ordblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.smblks", brick_index);
    ret = dict_get_int32(dict, key, &smblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"smblks", "%d",
                                          smblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.hblks", brick_index);
    ret = dict_get_int32(dict, key, &hblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hblks", "%d",
                                          hblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.hblkhd", brick_index);
    ret = dict_get_int32(dict, key, &hblkhd);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hblkhd", "%d",
                                          hblkhd);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.usmblks", brick_index);
    ret = dict_get_int32(dict, key, &usmblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"usmblks", "%d",
                                          usmblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.fsmblks", brick_index);
    ret = dict_get_int32(dict, key, &fsmblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"fsmblks", "%d",
                                          fsmblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.uordblks", brick_index);
    ret = dict_get_int32(dict, key, &uordblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uordblks", "%d",
                                          uordblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.fordblks", brick_index);
    ret = dict_get_int32(dict, key, &fordblks);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"fordblks", "%d",
                                          fordblks);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.mallinfo.keepcost", brick_index);
    ret = dict_get_int32(dict, key, &keepcost);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"keepcost", "%d",
                                          keepcost);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </mallinfo> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d", brick_index);
    ret = cli_xml_output_vol_status_mempool(writer, dict, key);
    if (ret)
        goto out;

    /* </memStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_clients(xmlTextWriterPtr writer, dict_t *dict,
                                  int brick_index)
{
    int ret = -1;
    int client_count = 0;
    char *hostname = NULL;
    uint64_t bytes_read = 0;
    uint64_t bytes_write = 0;
    uint32_t opversion = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <clientsStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"clientsStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.clientcount", brick_index);
    ret = dict_get_int32(dict, key, &client_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"clientCount",
                                          "%d", client_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < client_count; i++) {
        /* <client> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"client");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "brick%d.client%d.hostname", brick_index, i);
        ret = dict_get_str(dict, key, &hostname);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname",
                                              "%s", hostname);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "brick%d.client%d.bytesread", brick_index,
                 i);
        ret = dict_get_uint64(dict, key, &bytes_read);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"bytesRead",
                                              "%" PRIu64, bytes_read);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "brick%d.client%d.byteswrite", brick_index,
                 i);
        ret = dict_get_uint64(dict, key, &bytes_write);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"bytesWrite",
                                              "%" PRIu64, bytes_write);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "brick%d.client%d.opversion", brick_index,
                 i);
        ret = dict_get_uint32(dict, key, &opversion);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"opVersion",
                                              "%" PRIu32, opversion);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </client> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </clientsStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_inode_entry(xmlTextWriterPtr writer, dict_t *dict,
                                      char *prefix)
{
    int ret = -1;
    char *gfid = NULL;
    uint64_t nlookup = 0;
    uint32_t ref = 0;
    int ia_type = 0;
    char key[1024] = {
        0,
    };

    /* <inode> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"inode");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.gfid", prefix);
    ret = dict_get_str(dict, key, &gfid);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"gfid", "%s",
                                          gfid);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.nlookup", prefix);
    ret = dict_get_uint64(dict, key, &nlookup);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"nLookup",
                                          "%" PRIu64, nlookup);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.ref", prefix);
    ret = dict_get_uint32(dict, key, &ref);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"ref", "%" PRIu32,
                                          ref);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.ia_type", prefix);
    ret = dict_get_int32(dict, key, &ia_type);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"iaType", "%d",
                                          ia_type);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </inode> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_itable(xmlTextWriterPtr writer, dict_t *dict,
                                 char *prefix)
{
    int ret = -1;
    uint32_t active_size = 0;
    uint32_t lru_size = 0;
    uint32_t purge_size = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    snprintf(key, sizeof(key), "%s.active_size", prefix);
    ret = dict_get_uint32(dict, key, &active_size);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"activeSize",
                                          "%" PRIu32, active_size);
    XML_RET_CHECK_AND_GOTO(ret, out);
    if (active_size != 0) {
        /* <active> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"active");
        XML_RET_CHECK_AND_GOTO(ret, out);

        for (i = 0; i < active_size; i++) {
            snprintf(key, sizeof(key), "%s.active%d", prefix, i);
            ret = cli_xml_output_vol_status_inode_entry(writer, dict, key);
            if (ret)
                goto out;
        }
        /* </active> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    snprintf(key, sizeof(key), "%s.lru_size", prefix);
    ret = dict_get_uint32(dict, key, &lru_size);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lruSize",
                                          "%" PRIu32, lru_size);
    XML_RET_CHECK_AND_GOTO(ret, out);
    if (lru_size != 0) {
        /* <lru> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"lru");
        XML_RET_CHECK_AND_GOTO(ret, out);

        for (i = 0; i < lru_size; i++) {
            snprintf(key, sizeof(key), "%s.lru%d", prefix, i);
            ret = cli_xml_output_vol_status_inode_entry(writer, dict, key);
            if (ret)
                goto out;
        }
        /* </lru> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    snprintf(key, sizeof(key), "%s.purge_size", prefix);
    ret = dict_get_uint32(dict, key, &purge_size);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"purgeSize",
                                          "%" PRIu32, purge_size);
    XML_RET_CHECK_AND_GOTO(ret, out);
    if (purge_size != 0) {
        /* <purge> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"purge");
        XML_RET_CHECK_AND_GOTO(ret, out);

        for (i = 0; i < purge_size; i++) {
            snprintf(key, sizeof(key), "%s.purge%d", prefix, i);
            ret = cli_xml_output_vol_status_inode_entry(writer, dict, key);
            if (ret)
                goto out;
        }
        /* </purge> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_inode(xmlTextWriterPtr writer, dict_t *dict,
                                int brick_index)
{
    int ret = -1;
    int conn_count = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <inodeStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"inodeStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.conncount", brick_index);
    ret = dict_get_int32(dict, key, &conn_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"connections",
                                          "%d", conn_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < conn_count; i++) {
        /* <connection> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"connection");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "brick%d.conn%d.itable", brick_index, i);
        ret = cli_xml_output_vol_status_itable(writer, dict, key);
        if (ret)
            goto out;

        /* </connection> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </inodeStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_fdtable(xmlTextWriterPtr writer, dict_t *dict,
                                  char *prefix)
{
    int ret = -1;
    int refcount = 0;
    uint32_t maxfds = 0;
    int firstfree = 0;
    int openfds = 0;
    int fd_pid = 0;
    int fd_refcount = 0;
    int fd_flags = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <fdTable> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"fdTable");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.refcount", prefix);
    ret = dict_get_int32(dict, key, &refcount);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"refCount", "%d",
                                          refcount);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.maxfds", prefix);
    ret = dict_get_uint32(dict, key, &maxfds);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxFds",
                                          "%" PRIu32, maxfds);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.firstfree", prefix);
    ret = dict_get_int32(dict, key, &firstfree);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"firstFree", "%d",
                                          firstfree);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.openfds", prefix);
    ret = dict_get_int32(dict, key, &openfds);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"openFds", "%d",
                                          openfds);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < maxfds; i++) {
        snprintf(key, sizeof(key), "%s.fdentry%d.pid", prefix, i);
        ret = dict_get_int32(dict, key, &fd_pid);
        if (ret)
            continue;

        snprintf(key, sizeof(key), "%s.fdentry%d.refcount", prefix, i);
        ret = dict_get_int32(dict, key, &fd_refcount);
        if (ret)
            continue;

        snprintf(key, sizeof(key), "%s.fdentry%d.flags", prefix, i);
        ret = dict_get_int32(dict, key, &fd_flags);
        if (ret)
            continue;

        /* <fd> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"fd");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"entry", "%d",
                                              i + 1);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", "%d",
                                              fd_pid);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"refCount",
                                              "%d", fd_refcount);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"flags", "%d",
                                              fd_flags);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </fd> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </fdTable> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_fd(xmlTextWriterPtr writer, dict_t *dict,
                             int brick_index)
{
    int ret = -1;
    int conn_count = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <fdStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"fdStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.conncount", brick_index);
    ret = dict_get_int32(dict, key, &conn_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"connections",
                                          "%d", conn_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < conn_count; i++) {
        /* <connection> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"connection");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "brick%d.conn%d.fdtable", brick_index, i);
        ret = cli_xml_output_vol_status_fdtable(writer, dict, key);
        if (ret)
            goto out;

        /* </connection> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </fdStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_callframe(xmlTextWriterPtr writer, dict_t *dict,
                                    char *prefix)
{
    int ret = -1;
    int ref_count = 0;
    char *translator = NULL;
    int complete = 0;
    char *parent = NULL;
    char *wind_from = NULL;
    char *wind_to = NULL;
    char *unwind_from = NULL;
    char *unwind_to = NULL;
    char key[1024] = {
        0,
    };

    /* <callFrame> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"callFrame");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.refcount", prefix);
    ret = dict_get_int32(dict, key, &ref_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"refCount", "%d",
                                          ref_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.translator", prefix);
    ret = dict_get_str(dict, key, &translator);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"translator", "%s",
                                          translator);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.complete", prefix);
    ret = dict_get_int32(dict, key, &complete);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"complete", "%d",
                                          complete);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.parent", prefix);
    ret = dict_get_str(dict, key, &parent);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"parent", "%s",
                                              parent);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    snprintf(key, sizeof(key), "%s.windfrom", prefix);
    ret = dict_get_str(dict, key, &wind_from);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"windFrom",
                                              "%s", wind_from);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    snprintf(key, sizeof(key), "%s.windto", prefix);
    ret = dict_get_str(dict, key, &wind_to);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"windTo", "%s",
                                              wind_to);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    snprintf(key, sizeof(key), "%s.unwindfrom", prefix);
    ret = dict_get_str(dict, key, &unwind_from);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"unwindFrom",
                                              "%s", unwind_from);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    snprintf(key, sizeof(key), "%s.unwindto", prefix);
    ret = dict_get_str(dict, key, &unwind_to);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"unwindTo",
                                              "%s", unwind_to);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </callFrame> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_callstack(xmlTextWriterPtr writer, dict_t *dict,
                                    char *prefix)
{
    int ret = -1;
    int uid = 0;
    int gid = 0;
    int pid = 0;
    uint64_t unique = 0;
    int frame_count = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <callStack> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"callStack");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.uid", prefix);
    ret = dict_get_int32(dict, key, &uid);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uid", "%d", uid);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.gid", prefix);
    ret = dict_get_int32(dict, key, &gid);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"gid", "%d", gid);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.pid", prefix);
    ret = dict_get_int32(dict, key, &pid);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid", "%d", pid);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.unique", prefix);
    ret = dict_get_uint64(dict, key, &unique);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"unique",
                                          "%" PRIu64, unique);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.count", prefix);
    ret = dict_get_int32(dict, key, &frame_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"frameCount", "%d",
                                          frame_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < frame_count; i++) {
        snprintf(key, sizeof(key), "%s.frame%d", prefix, i);
        ret = cli_xml_output_vol_status_callframe(writer, dict, key);
        if (ret)
            goto out;
    }

    /* </callStack> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_callpool(xmlTextWriterPtr writer, dict_t *dict,
                                   int brick_index)
{
    int ret = -1;
    int call_count = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <callpoolStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"callpoolStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "brick%d.callpool.count", brick_index);
    ret = dict_get_int32(dict, key, &call_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                          call_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < call_count; i++) {
        snprintf(key, sizeof(key), "brick%d.callpool.stack%d", brick_index, i);
        ret = cli_xml_output_vol_status_callstack(writer, dict, key);
        if (ret)
            goto out;
    }

    /* </callpoolStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_vol_status_begin(cli_local_t *local, int op_ret, int op_errno,
                                char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    ret = cli_begin_xml_output(&(local->writer), &(local->doc));
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <volStatus> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <volumes> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volumes");
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_status_end(cli_local_t *local)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    /* </volumes> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </volStatus> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(local->writer, local->doc);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
cli_xml_output_remove_brick_task_params(xmlTextWriterPtr writer, dict_t *dict,
                                        char *prefix)
{
    int ret = -1;
    char key[1024] = {
        0,
    };
    int count = 0;
    int i = 0;
    char *brick = NULL;

    /* <params> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"params");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.count", prefix);
    ret = dict_get_int32(dict, key, &count);
    if (ret)
        goto out;

    for (i = 1; i <= count; i++) {
        snprintf(key, sizeof(key), "%s.brick%d", prefix, i);
        ret = dict_get_str(dict, key, &brick);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brick", "%s",
                                              brick);
        XML_RET_CHECK_AND_GOTO(ret, out);
        brick = NULL;
    }

    /* </param> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_status_tasks(cli_local_t *local, dict_t *dict)
{
    int ret = -1;
    char *task_type = NULL;
    char *task_id_str = NULL;
    int status = 0;
    int tasks = 0;
    char key[1024] = {
        0,
    };
    int i = 0;

    /* <tasks> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"tasks");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "tasks", &tasks);
    if (ret)
        goto out;

    for (i = 0; i < tasks; i++) {
        /* <task> */
        ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"task");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "task%d.type", i);
        ret = dict_get_str(dict, key, &task_type);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"type",
                                              "%s", task_type);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "task%d.id", i);
        ret = dict_get_str(dict, key, &task_id_str);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"id",
                                              "%s", task_id_str);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "task%d.status", i);
        ret = dict_get_int32(dict, key, &status);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"status", "%d", status);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(local->writer,
                                              (xmlChar *)"statusStr", "%s",
                                              cli_vol_task_status_str[status]);

        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "task%d", i);
        if (!strcmp(task_type, "Remove brick")) {
            ret = cli_xml_output_remove_brick_task_params(local->writer, dict,
                                                          key);
            if (ret)
                goto out;
        }

        /* </task> */
        ret = xmlTextWriterEndElement(local->writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </tasks> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

#endif

int
cli_xml_output_vol_status_tasks_detail(cli_local_t *local, dict_t *dict)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    char *volname = NULL;

    /*<volume>*/
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volume");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "volname", &volname);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"volName",
                                          "%s", volname);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_xml_output_vol_status_tasks(local, dict);
    if (ret)
        goto out;

    /* </volume> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_status(cli_local_t *local, dict_t *dict)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    char *volname = NULL;
    int brick_count = 0;
    int brick_index_max = -1;
    int other_count = 0;
    int index_max = 0;
    uint32_t cmd = GF_CLI_STATUS_NONE;
    int online = 0;
    gf_boolean_t node_present = _gf_true;
    int i;
    int type = -1;

    /* <volume> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volume");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "volname", &volname);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"volName",
                                          "%s", volname);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "count", &brick_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"nodeCount",
                                          "%d", brick_count);

    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_uint32(dict, "cmd", &cmd);
    if (ret)
        goto out;

    ret = dict_get_int32(dict, "brick-index-max", &brick_index_max);
    if (ret)
        goto out;
    ret = dict_get_int32(dict, "other-count", &other_count);
    if (ret)
        goto out;

    index_max = brick_index_max + other_count;

    ret = dict_get_int32(dict, "type", &type);
    if (ret)
        goto out;

    for (i = 0; i <= index_max; i++) {
        ret = cli_xml_output_vol_status_common(local->writer, dict, i, &online,
                                               &node_present);
        if (ret) {
            if (node_present)
                goto out;
            else
                continue;
        }

        switch (cmd & GF_CLI_STATUS_MASK) {
            case GF_CLI_STATUS_DETAIL:
                ret = cli_xml_output_vol_status_detail(local->writer, dict, i);
                if (ret)
                    goto out;
                break;

            case GF_CLI_STATUS_MEM:
                if (online) {
                    ret = cli_xml_output_vol_status_mem(local->writer, dict, i);
                    if (ret)
                        goto out;
                }
                break;

            case GF_CLI_STATUS_CLIENTS:
                if (online) {
                    ret = cli_xml_output_vol_status_clients(local->writer, dict,
                                                            i);
                    if (ret)
                        goto out;
                }
                break;

            case GF_CLI_STATUS_INODE:
                if (online) {
                    ret = cli_xml_output_vol_status_inode(local->writer, dict,
                                                          i);
                    if (ret)
                        goto out;
                }
                break;

            case GF_CLI_STATUS_FD:
                if (online) {
                    ret = cli_xml_output_vol_status_fd(local->writer, dict, i);
                    if (ret)
                        goto out;
                }
                break;

            case GF_CLI_STATUS_CALLPOOL:
                if (online) {
                    ret = cli_xml_output_vol_status_callpool(local->writer,
                                                             dict, i);
                    if (ret)
                        goto out;
                }
                break;
            default:
                break;
        }

        /* </node>  was opened in cli_xml_output_vol_status_common()*/
        ret = xmlTextWriterEndElement(local->writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* Tasks are only present when a normal volume status call is done on a
     * single volume or on all volumes
     */
    if (((cmd & GF_CLI_STATUS_MASK) == GF_CLI_STATUS_NONE) &&
        (cmd & (GF_CLI_STATUS_VOL | GF_CLI_STATUS_ALL))) {
        ret = cli_xml_output_vol_status_tasks(local, dict);
        if (ret)
            goto out;
    }

    /* </volume> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
cli_xml_output_vol_top_rw_perf(xmlTextWriterPtr writer, dict_t *dict,
                               int brick_index, int member_index)
{
    int ret = -1;
    char *filename = NULL;
    uint64_t throughput = 0;
    struct timeval tv = {
        0,
    };
    char timestr[GF_TIMESTR_SIZE] = {
        0,
    };
    char key[1024] = {
        0,
    };

    /* <file> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"file");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-filename-%d", brick_index, member_index);
    ret = dict_get_str(dict, key, &filename);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"filename", "%s",
                                          filename);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-value-%d", brick_index, member_index);
    ret = dict_get_uint64(dict, key, &throughput);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count",
                                          "%" PRIu64, throughput);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-time-sec-%d", brick_index, member_index);
    ret = dict_get_int32(dict, key, (int32_t *)&tv.tv_sec);
    if (ret)
        goto out;

    snprintf(key, sizeof(key), "%d-time-usec-%d", brick_index, member_index);
    ret = dict_get_int32(dict, key, (int32_t *)&tv.tv_usec);
    if (ret)
        goto out;

    gf_time_fmt_tv(timestr, sizeof timestr, &tv, gf_timefmt_FT);
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"time", "%s",
                                          timestr);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </file> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

int
cli_xml_output_vol_top_other(xmlTextWriterPtr writer, dict_t *dict,
                             int brick_index, int member_index)
{
    int ret = -1;
    char *filename = NULL;
    uint64_t count = 0;
    char key[1024] = {
        0,
    };

    /* <file> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"file");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-filename-%d", brick_index, member_index);
    ret = dict_get_str(dict, key, &filename);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"filename", "%s",
                                          filename);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-value-%d", brick_index, member_index);
    ret = dict_get_uint64(dict, key, &count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count",
                                          "%" PRIu64, count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </file> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_vol_top(dict_t *dict, int op_ret, int op_errno, char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    int brick_count = 0;
    int top_op = GF_CLI_TOP_NONE;
    char *brick_name = NULL;
    int members = 0;
    uint64_t current_open = 0;
    uint64_t max_open = 0;
    char *max_open_time = NULL;
    double throughput = 0.0;
    double time_taken = 0.0;
    char key[1024] = {
        0,
    };
    int i = 0;
    int j = 0;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <volTop> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volTop");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "count", &brick_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickCount", "%d",
                                          brick_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "1-top-op", &top_op);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"topOp", "%d",
                                          top_op);
    XML_RET_CHECK_AND_GOTO(ret, out);

    while (i < brick_count) {
        i++;

        /* <brick> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"brick");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%d-brick", i);
        ret = dict_get_str(dict, key, &brick_name);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              brick_name);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%d-members", i);
        ret = dict_get_int32(dict, key, &members);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"members",
                                              "%d", members);
        XML_RET_CHECK_AND_GOTO(ret, out);

        switch (top_op) {
            case GF_CLI_TOP_OPEN:
                snprintf(key, sizeof(key), "%d-current-open", i);
                ret = dict_get_uint64(dict, key, &current_open);
                if (ret)
                    goto out;
                ret = xmlTextWriterWriteFormatElement(
                    writer, (xmlChar *)"currentOpen", "%" PRIu64, current_open);
                XML_RET_CHECK_AND_GOTO(ret, out);

                snprintf(key, sizeof(key), "%d-max-open", i);
                ret = dict_get_uint64(dict, key, &max_open);
                if (ret)
                    goto out;
                ret = xmlTextWriterWriteFormatElement(
                    writer, (xmlChar *)"maxOpen", "%" PRIu64, max_open);
                XML_RET_CHECK_AND_GOTO(ret, out);

                snprintf(key, sizeof(key), "%d-max-openfd-time", i);
                ret = dict_get_str(dict, key, &max_open_time);
                if (ret)
                    goto out;
                ret = xmlTextWriterWriteFormatElement(
                    writer, (xmlChar *)"maxOpenTime", "%s", max_open_time);
                XML_RET_CHECK_AND_GOTO(ret, out);

            case GF_CLI_TOP_READ:
            case GF_CLI_TOP_WRITE:
            case GF_CLI_TOP_OPENDIR:
            case GF_CLI_TOP_READDIR:

                break;

            case GF_CLI_TOP_READ_PERF:
            case GF_CLI_TOP_WRITE_PERF:
                snprintf(key, sizeof(key), "%d-throughput", i);
                ret = dict_get_double(dict, key, &throughput);
                if (!ret) {
                    snprintf(key, sizeof(key), "%d-time", i);
                    ret = dict_get_double(dict, key, &time_taken);
                }

                if (!ret) {
                    ret = xmlTextWriterWriteFormatElement(
                        writer, (xmlChar *)"throughput", "%f", throughput);
                    XML_RET_CHECK_AND_GOTO(ret, out);

                    ret = xmlTextWriterWriteFormatElement(
                        writer, (xmlChar *)"timeTaken", "%f", time_taken);
                    XML_RET_CHECK_AND_GOTO(ret, out);
                }

                break;

            default:
                ret = -1;
                goto out;
        }

        for (j = 1; j <= members; j++) {
            if (top_op == GF_CLI_TOP_READ_PERF ||
                top_op == GF_CLI_TOP_WRITE_PERF) {
                ret = cli_xml_output_vol_top_rw_perf(writer, dict, i, j);
            } else {
                ret = cli_xml_output_vol_top_other(writer, dict, i, j);
            }
            if (ret)
                goto out;
        }

        /* </brick> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </volTop> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
cli_xml_output_vol_profile_stats(xmlTextWriterPtr writer, dict_t *dict,
                                 int brick_index, int interval)
{
    int ret = -1;
    uint64_t read_count = 0;
    uint64_t write_count = 0;
    uint64_t hits = 0;
    double avg_latency = 0.0;
    double max_latency = 0.0;
    double min_latency = 0.0;
    uint64_t duration = 0;
    uint64_t total_read = 0;
    uint64_t total_write = 0;
    char key[1024] = {0};
    int i = 0;

    /* <cumulativeStats> || <intervalStats> */
    if (interval == -1)
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"cumulativeStats");
    else
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"intervalStats");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <blockStats> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"blockStats");
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < 32; i++) {
        /* <block> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"block");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"size",
                                              "%" PRIu32, (1U << i));
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%d-%d-read-%" PRIu32, brick_index, interval,
                 (1U << i));
        ret = dict_get_uint64(dict, key, &read_count);
        if (ret)
            read_count = 0;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"reads",
                                              "%" PRIu64, read_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%d-%d-write-%" PRIu32, brick_index,
                 interval, (1U << i));
        ret = dict_get_uint64(dict, key, &write_count);
        if (ret)
            write_count = 0;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"writes",
                                              "%" PRIu64, write_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </block> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </blockStats> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <fopStats> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"fopStats");
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < GF_FOP_MAXVALUE; i++) {
        snprintf(key, sizeof(key), "%d-%d-%d-hits", brick_index, interval, i);
        ret = dict_get_uint64(dict, key, &hits);
        if (ret)
            goto cont;

        snprintf(key, sizeof(key), "%d-%d-%d-avglatency", brick_index, interval,
                 i);
        ret = dict_get_double(dict, key, &avg_latency);
        if (ret)
            goto cont;

        snprintf(key, sizeof(key), "%d-%d-%d-minlatency", brick_index, interval,
                 i);
        ret = dict_get_double(dict, key, &min_latency);
        if (ret)
            goto cont;

        snprintf(key, sizeof(key), "%d-%d-%d-maxlatency", brick_index, interval,
                 i);
        ret = dict_get_double(dict, key, &max_latency);
        if (ret)
            goto cont;

        /* <fop> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"fop");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              gf_fop_list[i]);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hits",
                                              "%" PRIu64, hits);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"avgLatency",
                                              "%f", avg_latency);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"minLatency",
                                              "%f", min_latency);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxLatency",
                                              "%f", max_latency);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </fop> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);

    cont:
        hits = 0;
        avg_latency = 0.0;
        min_latency = 0.0;
        max_latency = 0.0;
    }

    for (i = 0; i < GF_UPCALL_FLAGS_MAXVALUE; i++) {
        hits = 0;
        avg_latency = 0.0;
        min_latency = 0.0;
        max_latency = 0.0;

        snprintf(key, sizeof(key), "%d-%d-%d-upcall-hits", brick_index,
                 interval, i);
        ret = dict_get_uint64(dict, key, &hits);
        if (ret)
            continue;

        /* <fop> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"fop");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              gf_fop_list[i]);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hits",
                                              "%" PRIu64, hits);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"avgLatency",
                                              "%f", avg_latency);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"minLatency",
                                              "%f", min_latency);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"maxLatency",
                                              "%f", max_latency);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </fop> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </fopStats> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-%d-duration", brick_index, interval);
    ret = dict_get_uint64(dict, key, &duration);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"duration",
                                          "%" PRIu64, duration);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-%d-total-read", brick_index, interval);
    ret = dict_get_uint64(dict, key, &total_read);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"totalRead",
                                          "%" PRIu64, total_read);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%d-%d-total-write", brick_index, interval);
    ret = dict_get_uint64(dict, key, &total_write);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"totalWrite",
                                          "%" PRIu64, total_write);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </cumulativeStats> || </intervalStats> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_vol_profile(dict_t *dict, int op_ret, int op_errno,
                           char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *volname = NULL;
    int op = GF_CLI_STATS_NONE;
    int info_op = GF_CLI_INFO_NONE;
    int brick_count = 0;
    char *brick_name = NULL;
    int interval = 0;
    char key[1024] = {
        0,
    };
    int i = 0;
    int stats_cleared = 0;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <volProfile> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volProfile");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "volname", &volname);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volname", "%s",
                                          volname);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "op", &op);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"profileOp", "%d",
                                          op);
    XML_RET_CHECK_AND_GOTO(ret, out);

    if (GF_CLI_STATS_INFO != op)
        goto cont;

    ret = dict_get_int32(dict, "count", &brick_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickCount", "%d",
                                          brick_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "info-op", &info_op);
    if (ret)
        goto out;

    while (i < brick_count) {
        i++;

        /* <brick> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"brick");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%d-brick", i);
        ret = dict_get_str(dict, key, &brick_name);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickName",
                                              "%s", brick_name);
        XML_RET_CHECK_AND_GOTO(ret, out);

        if (GF_CLI_INFO_CLEAR == info_op) {
            snprintf(key, sizeof(key), "%d-stats-cleared", i);
            ret = dict_get_int32(dict, key, &stats_cleared);
            if (ret)
                goto out;

            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"clearStats", "%s",
                stats_cleared ? "Cleared stats." : "Failed to clear stats.");
            XML_RET_CHECK_AND_GOTO(ret, out);
        } else {
            snprintf(key, sizeof(key), "%d-cumulative", i);
            ret = dict_get_int32(dict, key, &interval);
            if (ret == 0) {
                ret = cli_xml_output_vol_profile_stats(writer, dict, i,
                                                       interval);
                if (ret)
                    goto out;
            }

            snprintf(key, sizeof(key), "%d-interval", i);
            ret = dict_get_int32(dict, key, &interval);
            if (ret == 0) {
                ret = cli_xml_output_vol_profile_stats(writer, dict, i,
                                                       interval);
                if (ret)
                    goto out;
            }
        }

        /* </brick> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

cont:
    /* </volProfile> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_list(dict_t *dict, int op_ret, int op_errno, char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    int count = 0;
    char *volname = NULL;
    char key[1024] = {
        0,
    };
    int i = 0;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <volList> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volList");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "count", &count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                          count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < count; i++) {
        snprintf(key, sizeof(key), "volume%d", i);
        ret = dict_get_str(dict, key, &volname);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volume", "%s",
                                              volname);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </volList> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
cli_xml_output_vol_info_option(xmlTextWriterPtr writer, char *substr,
                               char *optstr, char *valstr)
{
    int ret = -1;
    char *ptr1 = NULL;
    char *ptr2 = NULL;

    ptr1 = substr;
    ptr2 = optstr;

    while (ptr1) {
        if (*ptr1 != *ptr2)
            break;
        ptr1++;
        ptr2++;
        if (!*ptr1)
            break;
        if (!*ptr2)
            break;
    }
    if (*ptr2 == '\0')
        goto out;

    /* <option> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"option");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          ptr2);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"value", "%s",
                                          valstr);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </option> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}

struct tmp_xml_option_logger {
    char *key;
    xmlTextWriterPtr writer;
};

static int
_output_vol_info_option(dict_t *d, char *k, data_t *v, void *data)
{
    int ret = 0;
    char *ptr = NULL;
    struct tmp_xml_option_logger *tmp = NULL;

    tmp = data;

    ptr = strstr(k, "option.");
    if (!ptr)
        goto out;

    if (!v) {
        ret = -1;
        goto out;
    }
    ret = cli_xml_output_vol_info_option(tmp->writer, tmp->key, k, v->data);

out:
    return ret;
}

int
cli_xml_output_vol_info_options(xmlTextWriterPtr writer, dict_t *dict,
                                char *prefix)
{
    int ret = -1;
    int opt_count = 0;
    char key[1024] = {
        0,
    };
    struct tmp_xml_option_logger tmp = {
        0,
    };

    snprintf(key, sizeof(key), "%s.opt_count", prefix);
    ret = dict_get_int32(dict, key, &opt_count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"optCount", "%d",
                                          opt_count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <options> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"options");
    XML_RET_CHECK_AND_GOTO(ret, out);
    snprintf(key, sizeof(key), "%s.option.", prefix);

    tmp.key = key;
    tmp.writer = writer;
    ret = dict_foreach(dict, _output_vol_info_option, &tmp);
    if (ret)
        goto out;

    /* </options> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_vol_info(cli_local_t *local, dict_t *dict)
{
#if (HAVE_LIB_XML)
    int ret = 0;
    int count = 0;
    char *volname = NULL;
    char *volume_id = NULL;
    char *uuid = NULL;
    int type = 0;
    int status = 0;
    int brick_count = 0;
    int dist_count = 0;
    int replica_count = 0;
    int arbiter_count = 0;
    int snap_count = 0;
    int isArbiter = 0;
    int disperse_count = 0;
    int redundancy_count = 0;
    int transport = 0;
    char *brick = NULL;
    char key[1024] = {
        0,
    };
    int i = 0;
    int j = 1;
    char *caps __attribute__((unused)) = NULL;
    int k __attribute__((unused)) = 0;

    ret = dict_get_int32(dict, "count", &count);
    if (ret)
        goto out;

    for (i = 0; i < count; i++) {
        /* <volume> */
        ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volume");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.name", i);
        ret = dict_get_str(dict, key, &volname);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"name",
                                              "%s", volname);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.volume_id", i);
        ret = dict_get_str(dict, key, &volume_id);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"id",
                                              "%s", volume_id);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.status", i);
        ret = dict_get_int32(dict, key, &status);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"status", "%d", status);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(local->writer,
                                              (xmlChar *)"statusStr", "%s",
                                              cli_vol_status_str[status]);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.snap_count", i);
        ret = dict_get_int32(dict, key, &snap_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"snapshotCount", "%d", snap_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.brick_count", i);
        ret = dict_get_int32(dict, key, &brick_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"brickCount", "%d", brick_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.dist_count", i);
        ret = dict_get_int32(dict, key, &dist_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(local->writer,
                                              (xmlChar *)"distCount", "%d",
                                              (brick_count / dist_count));
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.replica_count", i);
        ret = dict_get_int32(dict, key, &replica_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"replicaCount", "%d", replica_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.arbiter_count", i);
        ret = dict_get_int32(dict, key, &arbiter_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"arbiterCount", "%d", arbiter_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.disperse_count", i);
        ret = dict_get_int32(dict, key, &disperse_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"disperseCount", "%d", disperse_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.redundancy_count", i);
        ret = dict_get_int32(dict, key, &redundancy_count);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(local->writer,
                                              (xmlChar *)"redundancyCount",
                                              "%d", redundancy_count);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.type", i);
        ret = dict_get_int32(dict, key, &type);
        if (ret)
            goto out;
        /* For Distributed-(replicate,disperse)
           types
         */
        type = get_vol_type(type, dist_count, brick_count);

        ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"type",
                                              "%d", type);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"typeStr", "%s", vol_type_str[type]);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d.transport", i);
        ret = dict_get_int32(dict, key, &transport);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(
            local->writer, (xmlChar *)"transport", "%d", transport);
        XML_RET_CHECK_AND_GOTO(ret, out);

        j = 1;

        /* <bricks> */
        ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"bricks");
        XML_RET_CHECK_AND_GOTO(ret, out);

        while (j <= brick_count) {
            ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"brick");
            XML_RET_CHECK_AND_GOTO(ret, out);

            snprintf(key, sizeof(key), "volume%d.brick%d.uuid", i, j);
            ret = dict_get_str(dict, key, &uuid);
            if (ret)
                goto out;
            ret = xmlTextWriterWriteFormatAttribute(
                local->writer, (xmlChar *)"uuid", "%s", uuid);
            XML_RET_CHECK_AND_GOTO(ret, out);

            snprintf(key, sizeof(key), "volume%d.brick%d", i, j);
            ret = dict_get_str(dict, key, &brick);
            if (ret)
                goto out;
            ret = xmlTextWriterWriteFormatString(local->writer, "%s", brick);
            XML_RET_CHECK_AND_GOTO(ret, out);

            ret = xmlTextWriterWriteFormatElement(
                local->writer, (xmlChar *)"name", "%s", brick);
            XML_RET_CHECK_AND_GOTO(ret, out);

            ret = xmlTextWriterWriteFormatElement(
                local->writer, (xmlChar *)"hostUuid", "%s", uuid);
            XML_RET_CHECK_AND_GOTO(ret, out);

            snprintf(key, sizeof(key), "volume%d.brick%d.isArbiter", i, j);
            if (dict_get(dict, key))
                isArbiter = 1;
            else
                isArbiter = 0;
            ret = xmlTextWriterWriteFormatElement(
                local->writer, (xmlChar *)"isArbiter", "%d", isArbiter);
            XML_RET_CHECK_AND_GOTO(ret, out);

            /* </brick> */
            ret = xmlTextWriterEndElement(local->writer);
            XML_RET_CHECK_AND_GOTO(ret, out);

            j++;
        }
        /* </bricks> */
        ret = xmlTextWriterEndElement(local->writer);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "volume%d", i);
        ret = cli_xml_output_vol_info_options(local->writer, dict, key);
        if (ret)
            goto out;

        /* </volume> */
        ret = xmlTextWriterEndElement(local->writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    if (volname) {
        GF_FREE(local->get_vol.volname);
        local->get_vol.volname = gf_strdup(volname);
        local->vol_count += count;
    }
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_info_begin(cli_local_t *local, int op_ret, int op_errno,
                              char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    GF_ASSERT(local);

    ret = cli_begin_xml_output(&(local->writer), &(local->doc));
    if (ret)
        goto out;

    ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <volInfo> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volInfo");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <volumes> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volumes");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* Init vol count */
    local->vol_count = 0;

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_info_end(cli_local_t *local)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    GF_ASSERT(local);

    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"count",
                                          "%d", local->vol_count);
    XML_RET_CHECK_AND_GOTO(ret, out);
    /* </volumes> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </volInfo> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(local->writer, local->doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_quota_limit_list_end(cli_local_t *local)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(local->writer, local->doc);

out:
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_quota_limit_list_begin(cli_local_t *local, int op_ret,
                                          int op_errno, char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    ret = cli_begin_xml_output(&(local->writer), &(local->doc));
    if (ret)
        goto out;

    ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <volQuota> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"volQuota");
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
static int
cli_xml_output_peer_hostnames(xmlTextWriterPtr writer, dict_t *dict,
                              const char *prefix, int count)
{
    int ret = -1;
    int i = 0;
    char *hostname = NULL;
    char key[1024] = {
        0,
    };

    /* <hostnames> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"hostnames");
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 0; i < count; i++) {
        ret = snprintf(key, sizeof(key), "%s.hostname%d", prefix, i);
        if (ret < 0)
            goto out;
        ret = dict_get_str(dict, key, &hostname);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname",
                                              "%s", hostname);
        XML_RET_CHECK_AND_GOTO(ret, out);
        hostname = NULL;
    }

    /* </hostnames> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_peer_status(dict_t *dict, int op_ret, int op_errno,
                           char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    int count = 0;
    char *uuid = NULL;
    char *hostname = NULL;
    int connected = 0;
    int state_id = 0;
    char *state_str = NULL;
    int hostname_count = 0;
    int i = 1;
    char key[1024] = {
        0,
    };

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <peerStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"peerStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    if (!dict)
        goto cont;

    ret = dict_get_int32(dict, "count", &count);
    if (ret)
        goto out;

    while (i <= count) {
        /* <peer> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"peer");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "friend%d.uuid", i);
        ret = dict_get_str(dict, key, &uuid);
        if (ret)
            goto out;

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                              uuid);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "friend%d.hostname", i);
        ret = dict_get_str(dict, key, &hostname);
        if (ret)
            goto out;

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hostname",
                                              "%s", hostname);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "friend%d.hostname_count", i);
        ret = dict_get_int32(dict, key, &hostname_count);
        if ((ret == 0) && (hostname_count > 0)) {
            snprintf(key, sizeof(key), "friend%d", i);
            ret = cli_xml_output_peer_hostnames(writer, dict, key,
                                                hostname_count);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }

        snprintf(key, sizeof(key), "friend%d.connected", i);
        ret = dict_get_int32(dict, key, &connected);
        if (ret)
            goto out;

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"connected",
                                              "%d", connected);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "friend%d.stateId", i);
        ret = dict_get_int32(dict, key, &state_id);
        if (!ret) {
            /* ignore */

            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"state",
                                                  "%d", state_id);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }

        snprintf(key, sizeof(key), "friend%d.state", i);
        ret = dict_get_str(dict, key, &state_str);
        if (!ret) {
            /* ignore */

            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"stateStr",
                                                  "%s", state_str);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }

        /* </peer> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);

        i++;
    }

cont:
    /* </peerStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
/* Used for rebalance stop/status, remove-brick status */
int
cli_xml_output_vol_rebalance_status(xmlTextWriterPtr writer, dict_t *dict,
                                    enum gf_task_types task_type)
{
    int ret = -1;
    int count = 0;
    char *node_name = NULL;
    char *node_uuid = NULL;
    uint64_t files = 0;
    uint64_t size = 0;
    uint64_t lookups = 0;
    int status_rcd = 0;
    uint64_t failures = 0;
    uint64_t skipped = 0;
    uint64_t total_files = 0;
    uint64_t total_size = 0;
    uint64_t total_lookups = 0;
    uint64_t total_failures = 0;
    uint64_t total_skipped = 0;
    char key[1024] = {
        0,
    };
    int i = 0;
    int overall_status = -1;
    double elapsed = 0;
    double overall_elapsed = 0;

    if (!dict) {
        ret = 0;
        goto out;
    }

    ret = dict_get_int32(dict, "count", &count);
    if (ret)
        goto out;
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"nodeCount", "%d",
                                          count);
    XML_RET_CHECK_AND_GOTO(ret, out);

    while (i < count) {
        i++;
        /* Getting status early, to skip nodes that don't have the
         * rebalance process started
         */
        snprintf(key, sizeof(key), "status-%d", i);
        ret = dict_get_int32(dict, key, &status_rcd);

        /* If glusterd is down it fails to get the status, try
         getting status from other nodes */
        if (ret)
            continue;
        if (GF_DEFRAG_STATUS_NOT_STARTED == status_rcd)
            continue;

        /* <node> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"node");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "node-name-%d", i);
        ret = dict_get_str(dict, key, &node_name);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"nodeName",
                                              "%s", node_name);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "node-uuid-%d", i);
        ret = dict_get_str(dict, key, &node_uuid);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"id", "%s",
                                              node_uuid);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "files-%d", i);
        ret = dict_get_uint64(dict, key, &files);
        if (ret)
            goto out;
        total_files += files;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"files",
                                              "%" PRIu64, files);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "size-%d", i);
        ret = dict_get_uint64(dict, key, &size);
        if (ret)
            goto out;
        total_size += size;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"size",
                                              "%" PRIu64, size);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "lookups-%d", i);
        ret = dict_get_uint64(dict, key, &lookups);
        if (ret)
            goto out;
        total_lookups += lookups;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lookups",
                                              "%" PRIu64, lookups);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "failures-%d", i);
        ret = dict_get_uint64(dict, key, &failures);
        if (ret)
            goto out;

        snprintf(key, sizeof(key), "skipped-%d", i);

        ret = dict_get_uint64(dict, key, &skipped);
        if (ret)
            goto out;

        if (task_type == GF_TASK_TYPE_REMOVE_BRICK) {
            failures += skipped;
            skipped = 0;
        }

        total_failures += failures;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"failures",
                                              "%" PRIu64, failures);
        XML_RET_CHECK_AND_GOTO(ret, out);

        total_skipped += skipped;

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"skipped",
                                              "%" PRIu64, skipped);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%d",
                                              status_rcd);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(
            writer, (xmlChar *)"statusStr", "%s",
            cli_vol_task_status_str[status_rcd]);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "run-time-%d", i);
        ret = dict_get_double(dict, key, &elapsed);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"runtime",
                                              "%.2f", elapsed);
        XML_RET_CHECK_AND_GOTO(ret, out);

        if (elapsed > overall_elapsed) {
            overall_elapsed = elapsed;
        }

        /* Rebalance has 5 states,
         * NOT_STARTED, STARTED, STOPPED, COMPLETE, FAILED
         * The precedence used to determine the aggregate status is as
         * below,
         * STARTED > FAILED > STOPPED > COMPLETE > NOT_STARTED
         */
        /* TODO: Move this to a common place utilities that both CLI and
         * glusterd need.
         * Till then if the below algorithm is changed, change it in
         * glusterd_volume_status_aggregate_tasks_status in
         * glusterd-utils.c
         */

        if (-1 == overall_status)
            overall_status = status_rcd;
        int rank[] = {[GF_DEFRAG_STATUS_STARTED] = 1,
                      [GF_DEFRAG_STATUS_FAILED] = 2,
                      [GF_DEFRAG_STATUS_STOPPED] = 3,
                      [GF_DEFRAG_STATUS_COMPLETE] = 4,
                      [GF_DEFRAG_STATUS_NOT_STARTED] = 5};
        if (rank[status_rcd] <= rank[overall_status])
            overall_status = status_rcd;

        /* </node> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* Aggregate status */
    /* <aggregate> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"aggregate");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"files",
                                          "%" PRIu64, total_files);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"size", "%" PRIu64,
                                          total_size);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lookups",
                                          "%" PRIu64, total_lookups);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"failures",
                                          "%" PRIu64, total_failures);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"skipped",
                                          "%" PRIu64, total_skipped);
    XML_RET_CHECK_AND_GOTO(ret, out);

    if (overall_status == -1) {
        overall_status = status_rcd;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%d",
                                          overall_status);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        writer, (xmlChar *)"statusStr", "%s",
        cli_vol_task_status_str[overall_status]);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"runtime", "%.2f",
                                          overall_elapsed);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </aggregate> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_vol_rebalance(gf_cli_defrag_type op, dict_t *dict, int op_ret,
                             int op_errno, char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *task_id_str = NULL;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <volRebalance> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volRebalance");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, GF_REBALANCE_TID_KEY, &task_id_str);
    if (ret == 0) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"task-id",
                                              "%s", task_id_str);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"op", "%d", op);
    XML_RET_CHECK_AND_GOTO(ret, out);

    if ((GF_DEFRAG_CMD_STOP == op) || (GF_DEFRAG_CMD_STATUS == op)) {
        ret = cli_xml_output_vol_rebalance_status(writer, dict,
                                                  GF_TASK_TYPE_REBALANCE);
        if (ret)
            goto out;
    }

    /* </volRebalance> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_remove_brick(gf_boolean_t status_op, dict_t *dict,
                                int op_ret, int op_errno, char *op_errstr,
                                const char *op)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *task_id_str = NULL;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    ret = xmlTextWriterStartElement(writer, (xmlChar *)op);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, GF_REMOVE_BRICK_TID_KEY, &task_id_str);
    if (ret == 0) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"task-id",
                                              "%s", task_id_str);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    if (status_op) {
        ret = cli_xml_output_vol_rebalance_status(writer, dict,
                                                  GF_TASK_TYPE_REMOVE_BRICK);
        if (ret)
            goto out;
    }

    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_replace_brick(dict_t *dict, int op_ret, int op_errno,
                                 char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_vol_create(dict_t *dict, int op_ret, int op_errno,
                          char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *volname = NULL;
    char *volid = NULL;

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    if (dict) {
        /* <volCreate> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"volCreate");
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* <volume> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = dict_get_str(dict, "volname", &volname);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              volname);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = dict_get_str(dict, "volume-id", &volid);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"id", "%s",
                                              volid);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </volume> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </volCreate> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_generic_volume(char *op, dict_t *dict, int op_ret, int op_errno,
                              char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *volname = NULL;
    char *volid = NULL;

    GF_ASSERT(op);

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    if (dict) {
        /* <"op"> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)op);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* <volume> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = dict_get_str(dict, "volname", &volname);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              volname);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = dict_get_str(dict, "vol-id", &volid);
        if (ret)
            goto out;
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"id", "%s",
                                              volid);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </volume> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </"op"> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
int
_output_gsync_config(FILE *fp, xmlTextWriterPtr writer, char *op_name)
{
    char resbuf[256 + PATH_MAX] = {
        0,
    };
    char *ptr = NULL;
    char *v = NULL;
    int blen = sizeof(resbuf);
    int ret = 0;

    for (;;) {
        ptr = fgets(resbuf, blen, fp);
        if (!ptr)
            break;

        v = resbuf + strlen(resbuf) - 1;
        while (isspace(*v)) {
            /* strip trailing space */
            *v-- = '\0';
        }
        if (v == resbuf) {
            /* skip empty line */
            continue;
        }

        if (op_name != NULL) {
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)op_name,
                                                  "%s", resbuf);
            XML_RET_CHECK_AND_GOTO(ret, out);
            goto out;
        }

        v = strchr(resbuf, ':');
        if (!v) {
            ret = -1;
            goto out;
        }
        *v++ = '\0';
        while (isspace(*v))
            v++;
        v = gf_strdup(v);
        if (!v) {
            ret = -1;
            goto out;
        }

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)resbuf, "%s",
                                              v);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

#if (HAVE_LIB_XML)
int
get_gsync_config(runner_t *runner,
                 int (*op_conf)(FILE *fp, xmlTextWriterPtr writer,
                                char *op_name),
                 xmlTextWriterPtr writer, char *op_name)
{
    int ret = 0;

    runner_redir(runner, STDOUT_FILENO, RUN_PIPE);
    if (runner_start(runner) != 0) {
        gf_log("cli", GF_LOG_ERROR, "spawning child failed");
        return -1;
    }

    ret = op_conf(runner_chio(runner, STDOUT_FILENO), writer, op_name);

    ret |= runner_end(runner);
    if (ret)
        gf_log("cli", GF_LOG_ERROR, "reading data from child failed");

    return ret ? -1 : 0;
}
#endif

#if (HAVE_LIB_XML)
int
cli_xml_generate_gsync_config(dict_t *dict, xmlTextWriterPtr writer)
{
    runner_t runner = {
        0,
    };
    char *subop = NULL;
    char *gwd = NULL;
    char *secondary = NULL;
    char *confpath = NULL;
    char *primary = NULL;
    char *op_name = NULL;
    int ret = -1;
    char conf_path[PATH_MAX] = "";

    if (dict_get_str(dict, "subop", &subop) != 0) {
        ret = -1;
        goto out;
    }

    if (strcmp(subop, "get") != 0 && strcmp(subop, "get-all") != 0) {
        ret = xmlTextWriterWriteFormatElement(
            writer, (xmlChar *)"message", "%s",
            GEOREP " config updated successfully");
        XML_RET_CHECK_AND_GOTO(ret, out);
        ret = 0;
        goto out;
    }

    if (dict_get_str(dict, "glusterd_workdir", &gwd) != 0 ||
        dict_get_str(dict, "secondary", &secondary) != 0) {
        ret = -1;
        goto out;
    }

    if (dict_get_str(dict, "primary", &primary) != 0)
        primary = NULL;

    if (dict_get_str(dict, "op_name", &op_name) != 0)
        op_name = NULL;

    ret = dict_get_str(dict, "conf_path", &confpath);
    if (!confpath) {
        ret = snprintf(conf_path, sizeof(conf_path) - 1,
                       "%s/" GEOREP "/gsyncd_template.conf", gwd);
        conf_path[ret] = '\0';
        confpath = conf_path;
    }

    runinit(&runner);
    runner_add_args(&runner, GSYNCD_PREFIX "/gsyncd", "-c", NULL);
    runner_argprintf(&runner, "%s", confpath);
    runner_argprintf(&runner, "--iprefix=%s", DATADIR);

    if (primary)
        runner_argprintf(&runner, ":%s", primary);

    runner_add_arg(&runner, secondary);
    runner_argprintf(&runner, "--config-%s", subop);

    if (op_name)
        runner_add_arg(&runner, op_name);

    ret = get_gsync_config(&runner, _output_gsync_config, writer, op_name);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

#if (HAVE_LIB_XML)
int
cli_xml_output_vol_gsync_status(dict_t *dict, xmlTextWriterPtr writer)
{
    int ret = -1;
    int i = 1;
    int j = 0;
    int count = 0;
    const int number_of_fields = 20;
    int closed = 1;
    int session_closed = 1;
    gf_gsync_status_t **status_values = NULL;
    char status_value_name[PATH_MAX] = "";
    char *tmp = NULL;
    char *volume = NULL;
    char *volume_next = NULL;
    char *secondary = NULL;
    char *secondary_next = NULL;
    static const char *title_values[] = {
        "primary_node", "", "primary_brick", "secondary_user", "secondary",
        "secondary_node", "status", "crawl_status",
        /* last_synced */
        "", "entry", "data", "meta", "failures",
        /* checkpoint_time */
        "", "checkpoint_completed",
        /* checkpoint_completion_time */
        "", "primary_node_uuid",
        /* last_synced_utc */
        "last_synced",
        /* checkpoint_time_utc */
        "checkpoint_time",
        /* checkpoint_completion_time_utc */
        "checkpoint_completion_time"};

    GF_ASSERT(dict);

    ret = dict_get_int32(dict, "gsync-count", &count);
    if (ret)
        goto out;

    status_values = GF_MALLOC(count * sizeof(gf_gsync_status_t *),
                              gf_common_mt_char);
    if (!status_values) {
        ret = -1;
        goto out;
    }

    for (i = 0; i < count; i++) {
        status_values[i] = GF_CALLOC(1, sizeof(gf_gsync_status_t),
                                     gf_common_mt_char);
        if (!status_values[i]) {
            ret = -1;
            goto out;
        }

        snprintf(status_value_name, sizeof(status_value_name), "status_value%d",
                 i);

        ret = dict_get_bin(dict, status_value_name,
                           (void **)&(status_values[i]));
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "struct member empty.");
            goto out;
        }
    }

    qsort(status_values, count, sizeof(gf_gsync_status_t *),
          gf_gsync_status_t_comparator);

    for (i = 0; i < count; i++) {
        if (closed) {
            ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
            XML_RET_CHECK_AND_GOTO(ret, out);

            tmp = get_struct_variable(1, status_values[i]);
            if (!tmp) {
                gf_log("cli", GF_LOG_ERROR, "struct member empty.");
                ret = -1;
                goto out;
            }

            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name",
                                                  "%s", tmp);
            XML_RET_CHECK_AND_GOTO(ret, out);

            ret = xmlTextWriterStartElement(writer, (xmlChar *)"sessions");
            XML_RET_CHECK_AND_GOTO(ret, out);

            closed = 0;
        }

        if (session_closed) {
            ret = xmlTextWriterStartElement(writer, (xmlChar *)"session");
            XML_RET_CHECK_AND_GOTO(ret, out);

            session_closed = 0;

            tmp = get_struct_variable(21, status_values[i]);
            if (!tmp) {
                gf_log("cli", GF_LOG_ERROR, "struct member empty.");
                ret = -1;
                goto out;
            }

            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"session_secondary", "%s", tmp);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }

        ret = xmlTextWriterStartElement(writer, (xmlChar *)"pair");
        XML_RET_CHECK_AND_GOTO(ret, out);

        for (j = 0; j < number_of_fields; j++) {
            /* XML ignore fields */
            if (strcmp(title_values[j], "") == 0)
                continue;

            tmp = get_struct_variable(j, status_values[i]);
            if (!tmp) {
                gf_log("cli", GF_LOG_ERROR, "struct member empty.");
                ret = -1;
                goto out;
            }

            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)title_values[j], "%s", tmp);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }

        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);

        if (i + 1 < count) {
            secondary = get_struct_variable(20, status_values[i]);
            secondary_next = get_struct_variable(20, status_values[i + 1]);
            volume = get_struct_variable(1, status_values[i]);
            volume_next = get_struct_variable(1, status_values[i + 1]);
            if (!secondary || !secondary_next || !volume || !volume_next) {
                gf_log("cli", GF_LOG_ERROR, "struct member empty.");
                ret = -1;
                goto out;
            }

            if (strcmp(volume, volume_next) != 0) {
                closed = 1;
                session_closed = 1;

                ret = xmlTextWriterEndElement(writer);
                XML_RET_CHECK_AND_GOTO(ret, out);

                ret = xmlTextWriterEndElement(writer);
                XML_RET_CHECK_AND_GOTO(ret, out);

                ret = xmlTextWriterEndElement(writer);
                XML_RET_CHECK_AND_GOTO(ret, out);
            } else if (strcmp(secondary, secondary_next) != 0) {
                session_closed = 1;

                ret = xmlTextWriterEndElement(writer);
                XML_RET_CHECK_AND_GOTO(ret, out);
            }
        } else {
            ret = xmlTextWriterEndElement(writer);
            XML_RET_CHECK_AND_GOTO(ret, out);

            ret = xmlTextWriterEndElement(writer);
            XML_RET_CHECK_AND_GOTO(ret, out);

            ret = xmlTextWriterEndElement(writer);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }
    }
out:
    if (status_values)
        GF_FREE(status_values);
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
}
#endif

int
cli_xml_output_vol_gsync(dict_t *dict, int op_ret, int op_errno,
                         char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *primary = NULL;
    char *secondary = NULL;
    int type = 0;

    GF_ASSERT(dict);

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    /* <geoRep> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"geoRep");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "type", &type);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get type");
        goto out;
    }

    switch (type) {
        case GF_GSYNC_OPTION_TYPE_START:
        case GF_GSYNC_OPTION_TYPE_STOP:
        case GF_GSYNC_OPTION_TYPE_PAUSE:
        case GF_GSYNC_OPTION_TYPE_RESUME:
        case GF_GSYNC_OPTION_TYPE_CREATE:
        case GF_GSYNC_OPTION_TYPE_DELETE:
            if (dict_get_str(dict, "primary", &primary) != 0)
                primary = "???";
            if (dict_get_str(dict, "secondary", &secondary) != 0)
                secondary = "???";

            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"primary",
                                                  "%s", primary);
            XML_RET_CHECK_AND_GOTO(ret, out);

            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"secondary", "%s", secondary);
            XML_RET_CHECK_AND_GOTO(ret, out);

            break;

        case GF_GSYNC_OPTION_TYPE_CONFIG:
            if (op_ret == 0) {
                ret = xmlTextWriterStartElement(writer, (xmlChar *)"config");
                XML_RET_CHECK_AND_GOTO(ret, out);

                ret = cli_xml_generate_gsync_config(dict, writer);
                if (ret)
                    goto out;

                ret = xmlTextWriterEndElement(writer);
                XML_RET_CHECK_AND_GOTO(ret, out);
            }

            break;
        case GF_GSYNC_OPTION_TYPE_STATUS:
            ret = cli_xml_output_vol_gsync_status(dict, writer);
            if (ret) {
                gf_log("cli", GF_LOG_DEBUG, "Failed to get gsync status");
                goto out;
            }
            break;
        default:
            ret = 0;
            break;
    }

    /* </geoRep> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(writer, doc);
out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

#if (HAVE_LIB_XML)
/* This function will generate snapshot create output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing create output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_create(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    char *str_value = NULL;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapCreate> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapCreate");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapshot> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapname", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap name");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapuuid", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapshot> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapCreate> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate snapshot clone output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing create output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_clone(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    char *str_value = NULL;

    GF_VALIDATE_OR_GOTO("cli", writer, out);
    GF_VALIDATE_OR_GOTO("cli", doc, out);
    GF_VALIDATE_OR_GOTO("cli", dict, out);

    /* <CloneCreate> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"CloneCreate");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <volume> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "clonename", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get clone name");
        goto out;
    }
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapuuid", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get clone uuid");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </volume> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </CloneCreate> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate snapshot restore output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing restore output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_restore(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    char *str_value = NULL;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapRestore> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapRestore");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <volume> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "volname", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get vol name");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "volid", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get volume id");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </volume> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapshot> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapname", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap name");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapuuid", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapshot> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapRestore> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:

    return ret;
}

/* This function will generate snapshot list output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing list output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_list(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    int i = 0;
    int snapcount = 0;
    char *str_value = NULL;
    char key[PATH_MAX] = "";

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapList> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapList");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "snapcount", &snapcount);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snapcount");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                          snapcount);
    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 1; i <= snapcount; ++i) {
        ret = snprintf(key, sizeof(key), "snapname%d", i);
        if (ret < 0) {
            goto out;
        }

        ret = dict_get_str(dict, key, &str_value);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not get %s ", key);
            goto out;
        } else {
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"snapshot",
                                                  "%s", str_value);
            XML_RET_CHECK_AND_GOTO(ret, out);
        }
    }

    /* </snapList> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:

    return ret;
}

/* This function will generate xml output for origin volume
 * of the given snapshot.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing info output
 * @param keyprefix     prefix for dictionary key
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_info_orig_vol(xmlTextWriterPtr writer, xmlDocPtr doc,
                               dict_t *dict, char *keyprefix)
{
    int ret = -1;
    int value = 0;
    char *buffer = NULL;
    char key[PATH_MAX] = "";

    GF_ASSERT(dict);
    GF_ASSERT(keyprefix);
    GF_ASSERT(writer);
    GF_ASSERT(doc);

    /* <originVolume> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"originVolume");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%sorigin-volname", keyprefix);

    ret = dict_get_str(dict, key, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_WARNING, "Failed to get %s", key);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%ssnapcount", keyprefix);

    ret = dict_get_int32(dict, key, &value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"snapCount", "%d",
                                          value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%ssnaps-available", keyprefix);

    ret = dict_get_int32(dict, key, &value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"snapRemaining",
                                          "%d", value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </originVolume> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate xml output of snapshot volume info.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing info output
 * @param keyprefix     key prefix for dictionary
 * @param snap_driven   boolean to check if output is based of volume
 *                      or snapshot
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_info_snap_vol(xmlTextWriterPtr writer, xmlDocPtr doc,
                               dict_t *dict, char *keyprefix,
                               gf_boolean_t snap_driven)
{
    char key[PATH_MAX] = "";
    char *buffer = NULL;
    int ret = -1;

    GF_ASSERT(dict);
    GF_ASSERT(keyprefix);
    GF_ASSERT(writer);
    GF_ASSERT(doc);

    /* <snapVolume> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapVolume");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key, sizeof(key), "%s.volname", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_str(dict, key, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key, sizeof(key), "%s.vol-status", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_str(dict, key, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get %s", key);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* If the command is snap_driven then we need to show origin volume
     * info. Else this is shown in the start of info display.*/
    if (snap_driven) {
        ret = snprintf(key, sizeof(key), "%s.", keyprefix);
        if (ret < 0)
            goto out;

        ret = cli_xml_snapshot_info_orig_vol(writer, doc, dict, key);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR,
                   "Failed to create "
                   "xml output for snapshot's origin volume");
            goto out;
        }
    }

    /* </snapVolume> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate snapshot info of individual snapshot
 * in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing info output
 * @param keyprefix     key prefix for dictionary
 * @param snap_driven   boolean to check if output is based of volume
 *                      or snapshot
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_info_per_snap(xmlTextWriterPtr writer, xmlDocPtr doc,
                               dict_t *dict, char *keyprefix,
                               gf_boolean_t snap_driven)
{
    char key_buffer[PATH_MAX] = "";
    char *buffer = NULL;
    int volcount = 0;
    int ret = -1;
    int i = 0;

    GF_ASSERT(dict);
    GF_ASSERT(keyprefix);
    GF_ASSERT(writer);
    GF_ASSERT(doc);

    /* <snapshot> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snapname", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_str(dict, key_buffer, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Unable to fetch snapname %s ", key_buffer);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-id", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_str(dict, key_buffer, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Unable to fetch snap-id %s ", key_buffer);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-desc", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_str(dict, key_buffer, &buffer);
    if (!ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"description",
                                              "%s", buffer);
    } else {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"description",
                                              "%s", "");
    }
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key_buffer, sizeof(key_buffer), "%s.snap-time", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_str(dict, key_buffer, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Unable to fetch snap-time %s ", keyprefix);
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"createTime", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = snprintf(key_buffer, sizeof(key_buffer), "%s.vol-count", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_int32(dict, key_buffer, &volcount);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Fail to get snap vol count");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volCount", "%d",
                                          volcount);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, key_buffer, &volcount);
    /* Display info of each snapshot volume */
    for (i = 1; i <= volcount; i++) {
        ret = snprintf(key_buffer, sizeof(key_buffer), "%s.vol%d", keyprefix,
                       i);
        if (ret < 0)
            goto out;

        ret = cli_xml_snapshot_info_snap_vol(writer, doc, dict, key_buffer,
                                             snap_driven);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR,
                   "Could not list "
                   "details of volume in a snap");
            goto out;
        }
    }

    /* </snapshot> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);
out:
    return ret;
}

/* This function will generate snapshot info output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing info output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_info(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    int i = 0;
    int snapcount = 0;
    char key[PATH_MAX] = "";
    gf_boolean_t snap_driven = _gf_false;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapInfo> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapInfo");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snap_driven = dict_get_str_boolean(dict, "snap-driven", _gf_false);

    /* If the approach is volume based then we should display origin volume
     * information first followed by per snap info*/
    if (!snap_driven) {
        ret = cli_xml_snapshot_info_orig_vol(writer, doc, dict, "");
        if (ret) {
            gf_log("cli", GF_LOG_ERROR,
                   "Failed to create "
                   "xml output for snapshot's origin volume");
            goto out;
        }
    }

    ret = dict_get_int32(dict, "snapcount", &snapcount);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snapcount");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                          snapcount);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapshots> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshots");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* Get snapshot info of individual snapshots */
    for (i = 1; i <= snapcount; ++i) {
        snprintf(key, sizeof(key), "snap%d", i);

        ret = cli_xml_snapshot_info_per_snap(writer, doc, dict, key,
                                             snap_driven);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not get %s ", key);
            goto out;
        }
    }

    /* </snapshots> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapInfo> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:

    return ret;
}

/* This function will generate snapshot status of individual
 * snapshot volume in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing status output
 * @param keyprefix     key prefix for dictionary
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_volume_status(xmlTextWriterPtr writer, xmlDocPtr doc,
                               dict_t *dict, const char *keyprefix)
{
    int ret = -1;
    int brickcount = 0;
    int i = 0;
    int pid = 0;
    char *buffer = NULL;
    char key[PATH_MAX] = "";

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);
    GF_ASSERT(keyprefix);

    ret = snprintf(key, sizeof(key), "%s.brickcount", keyprefix);
    if (ret < 0)
        goto out;

    ret = dict_get_int32(dict, key, &brickcount);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to fetch brickcount");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"brickCount", "%d",
                                          brickcount);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* Get status of every brick belonging to the snapshot volume */
    for (i = 0; i < brickcount; i++) {
        /* <snapInfo> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"brick");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = snprintf(key, sizeof(key), "%s.brick%d.path", keyprefix, i);
        if (ret < 0)
            goto out;

        ret = dict_get_str(dict, key, &buffer);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Unable to get Brick Path");
            /*
             * If path itself is not present, then end *
             * this brick's status and continue to the *
             * brick                                   *
             */
            ret = xmlTextWriterEndElement(writer);
            XML_RET_CHECK_AND_GOTO(ret, out);
            continue;
        }

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"path", "%s",
                                              buffer);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = snprintf(key, sizeof(key), "%s.brick%d.vgname", keyprefix, i);
        if (ret < 0)
            goto out;

        ret = dict_get_str(dict, key, &buffer);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Unable to get Volume Group");
            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"volumeGroup", "N/A");
        } else
            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"volumeGroup", "%s", buffer);

        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = snprintf(key, sizeof(key), "%s.brick%d.status", keyprefix, i);
        if (ret < 0)
            goto out;

        ret = dict_get_str(dict, key, &buffer);
        if (ret) {
            gf_log("cli", GF_LOG_INFO, "Unable to get Brick Running");
            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"brick_running", "N/A");
        } else
            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"brick_running", "%s", buffer);

        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = snprintf(key, sizeof(key), "%s.brick%d.pid", keyprefix, i);
        if (ret < 0)
            goto out;

        ret = dict_get_int32(dict, key, &pid);
        if (ret) {
            gf_log("cli", GF_LOG_INFO, "Unable to get pid");
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid",
                                                  "N/A");
        } else
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"pid",
                                                  "%d", pid);

        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = snprintf(key, sizeof(key), "%s.brick%d.data", keyprefix, i);
        if (ret < 0)
            goto out;

        ret = dict_get_str(dict, key, &buffer);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Unable to get Data Percent");
            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"data_percentage", "N/A");
        } else
            ret = xmlTextWriterWriteFormatElement(
                writer, (xmlChar *)"data_percentage", "%s", buffer);

        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = snprintf(key, sizeof(key), "%s.brick%d.lvsize", keyprefix, i);
        if (ret < 0)
            goto out;

        ret = dict_get_str(dict, key, &buffer);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Unable to get LV Size");
            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lvSize",
                                                  "N/A");
        } else {
            /* Truncate any newline character */
            buffer = strtok(buffer, "\n");

            ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"lvSize",
                                                  "%s", buffer);
        }

        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </brick> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

out:
    return ret;
}

/* This function will generate snapshot status of individual
 * snapshot in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing status output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_status_per_snap(xmlTextWriterPtr writer, xmlDocPtr doc,
                                 dict_t *dict, const char *keyprefix)
{
    int ret = -1;
    int volcount = 0;
    int i = 0;
    char *buffer = NULL;
    char key[PATH_MAX] = "";

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);
    GF_ASSERT(keyprefix);

    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot");
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.snapname", keyprefix);

    ret = dict_get_str(dict, key, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Unable to get snapname");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.uuid", keyprefix);

    ret = dict_get_str(dict, key, &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Unable to get snap UUID");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    snprintf(key, sizeof(key), "%s.volcount", keyprefix);

    ret = dict_get_int32(dict, key, &volcount);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Unable to get volume count");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"volCount", "%d",
                                          volcount);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* Get snapshot status of individual snapshot volume */
    for (i = 0; i < volcount; i++) {
        /* <volume> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(key, sizeof(key), "%s.vol%d", keyprefix, i);

        ret = cli_xml_snapshot_volume_status(writer, doc, dict, key);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not get snap volume status");
            goto out;
        }

        /* </volume> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </snapshot> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate snapshot status output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing status output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_status(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    int snapcount = 0;
    int i = 0;
    int status_cmd = 0;
    char key[PATH_MAX] = "";

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapStatus> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "sub-cmd", &status_cmd);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Could not fetch status type");
        goto out;
    }

    if ((GF_SNAP_STATUS_TYPE_SNAP == status_cmd) ||
        (GF_SNAP_STATUS_TYPE_ITER == status_cmd)) {
        snapcount = 1;
    } else {
        ret = dict_get_int32(dict, "status.snapcount", &snapcount);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not get snapcount");
            goto out;
        }

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                              snapcount);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    for (i = 0; i < snapcount; i++) {
        snprintf(key, sizeof(key), "status.snap%d", i);

        ret = cli_xml_snapshot_status_per_snap(writer, doc, dict, key);
        if (ret < 0) {
            gf_log("cli", GF_LOG_ERROR,
                   "failed to create xml "
                   "output for snapshot status");
            goto out;
        }
    }

    /* </snapStatus> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:

    return ret;
}

/* This function will generate snapshot config show output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing status output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_config_show(xmlTextWriterPtr writer, xmlDocPtr doc,
                             dict_t *dict)
{
    int ret = -1;
    uint64_t i = 0;
    uint64_t value = 0;
    uint64_t volcount = 0;
    char buf[PATH_MAX] = "";
    char *str_value = NULL;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <systemConfig> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"systemConfig");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_uint64(dict, "snap-max-hard-limit", &value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to get "
               "snap-max-hard-limit");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hardLimit",
                                          "%" PRIu64, value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_uint64(dict, "snap-max-soft-limit", &value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to get "
               "snap-max-soft-limit");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"softLimit",
                                          "%" PRIu64 "%%", value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "auto-delete", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Could not fetch auto-delete");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"autoDelete", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snap-activate-on-create", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Could not fetch snap-activate-on-create-delete");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"activateOnCreate",
                                          "%s", str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </systemConfig> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <volumeConfig> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volumeConfig");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_uint64(dict, "voldisplaycount", &volcount);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Could not fetch volcount");
        goto out;
    }

    /* Get config of all the volumes */
    for (i = 0; i < volcount; i++) {
        /* <volume> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"volume");
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(buf, sizeof(buf), "volume%" PRIu64 "-volname", i);
        ret = dict_get_str(dict, buf, &str_value);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf);
            goto out;
        }

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              str_value);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(buf, sizeof(buf), "volume%" PRIu64 "-snap-max-hard-limit", i);
        ret = dict_get_uint64(dict, buf, &value);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf);
            goto out;
        }

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"hardLimit",
                                              "%" PRIu64, value);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(buf, sizeof(buf), "volume%" PRIu64 "-active-hard-limit", i);
        ret = dict_get_uint64(dict, buf, &value);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR,
                   "Could not fetch"
                   " effective snap_max_hard_limit for "
                   "%s",
                   str_value);
            goto out;
        }

        ret = xmlTextWriterWriteFormatElement(
            writer, (xmlChar *)"effectiveHardLimit", "%" PRIu64, value);
        XML_RET_CHECK_AND_GOTO(ret, out);

        snprintf(buf, sizeof(buf), "volume%" PRIu64 "-snap-max-soft-limit", i);
        ret = dict_get_uint64(dict, buf, &value);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR, "Could not fetch %s", buf);
            goto out;
        }

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"softLimit",
                                              "%" PRIu64, value);
        XML_RET_CHECK_AND_GOTO(ret, out);

        /* </volume> */
        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </volume> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate snapshot config set output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing status output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_config_set(xmlTextWriterPtr writer, xmlDocPtr doc,
                            dict_t *dict)
{
    int ret = -1;
    uint64_t hard_limit = 0;
    uint64_t soft_limit = 0;
    char *volname = NULL;
    char *auto_delete = NULL;
    char *snap_activate = NULL;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* This is optional parameter therefore ignore the error */
    ret = dict_get_uint64(dict, "snap-max-hard-limit", &hard_limit);
    /* This is optional parameter therefore ignore the error */
    ret = dict_get_uint64(dict, "snap-max-soft-limit", &soft_limit);
    ret = dict_get_str(dict, "auto-delete", &auto_delete);
    ret = dict_get_str(dict, "snap-activate-on-create", &snap_activate);

    if (!hard_limit && !soft_limit && !auto_delete && !snap_activate) {
        ret = -1;
        gf_log("cli", GF_LOG_ERROR,
               "At least one option from "
               "snap-max-hard-limit, snap-max-soft-limit, auto-delete"
               " and snap-activate-on-create should be set");
        goto out;
    }

    /* Ignore the error, as volname is optional */
    ret = dict_get_str(dict, "volname", &volname);

    if (NULL == volname) {
        /* <systemConfig> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"systemConfig");
    } else {
        /* <volumeConfig> */
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"volumeConfig");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                              volname);
    }

    XML_RET_CHECK_AND_GOTO(ret, out);

    if (hard_limit) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"newHardLimit",
                                              "%" PRIu64, hard_limit);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    if (soft_limit) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"newSoftLimit",
                                              "%" PRIu64, soft_limit);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    if (auto_delete) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"autoDelete",
                                              "%s", auto_delete);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    if (snap_activate) {
        ret = xmlTextWriterWriteFormatElement(
            writer, (xmlChar *)"activateOnCreate", "%s", snap_activate);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }

    /* </volumeConfig> or </systemConfig> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:
    return ret;
}

/* This function will generate snapshot config output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing config output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_config(xmlTextWriterPtr writer, xmlDocPtr doc, dict_t *dict)
{
    int ret = -1;
    int config_command = 0;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapConfig> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapConfig");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "config-command", &config_command);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Could not fetch config type");
        goto out;
    }

    switch (config_command) {
        case GF_SNAP_CONFIG_TYPE_SET:
            ret = cli_xml_snapshot_config_set(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create xml "
                       "output for snapshot config set command");
                goto out;
            }

            break;
        case GF_SNAP_CONFIG_DISPLAY:
            ret = cli_xml_snapshot_config_show(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create xml "
                       "output for snapshot config show command");
                goto out;
            }
            break;
        default:
            gf_log("cli", GF_LOG_ERROR, "Unknown config command :%d",
                   config_command);
            ret = -1;
            goto out;
    }

    /* </snapConfig> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:

    return ret;
}

/* This function will generate snapshot activate or
 * deactivate output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing activate or deactivate output
 *
 * @return 0 on success and -1 on failure
 */
static int
cli_xml_snapshot_activate_deactivate(xmlTextWriterPtr writer, xmlDocPtr doc,
                                     dict_t *dict, int cmd)
{
    int ret = -1;
    char *buffer = NULL;
    char *tag = NULL;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    if (GF_SNAP_OPTION_TYPE_ACTIVATE == cmd) {
        tag = "snapActivate";
    } else if (GF_SNAP_OPTION_TYPE_DEACTIVATE == cmd) {
        tag = "snapDeactivate";
    } else {
        gf_log("cli", GF_LOG_ERROR, "invalid command %d", cmd);
        goto out;
    }

    /* <snapActivate> or <snapDeactivate> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)tag);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapshot> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapname", &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap name");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapuuid", &buffer);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid");
        goto out;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          buffer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapshot> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapActivate> or </snapDeactivate> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = 0;
out:

    return ret;
}
#endif /* HAVE_LIB_XML */

/* This function will generate snapshot delete output in xml format.
 *
 * @param writer        xmlTextWriterPtr
 * @param doc           xmlDocPtr
 * @param dict          dict containing delete output
 * @param rsp           cli response
 *
 * @return 0 on success and -1 on failure
 */
int
cli_xml_snapshot_delete(cli_local_t *local, dict_t *dict, gf_cli_rsp *rsp)
{
    int ret = -1;
#ifdef HAVE_LIB_XML
    char *str_value = NULL;
    xmlTextWriterPtr writer = local->writer;
    xmlDocPtr doc = local->doc;

    GF_ASSERT(writer);
    GF_ASSERT(doc);
    GF_ASSERT(dict);

    /* <snapshot> */
    ret = xmlTextWriterStartElement(writer, (xmlChar *)"snapshot");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_str(dict, "snapname", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap name");
        goto xmlend;
    }

    if (!rsp->op_ret) {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status",
                                              "Success");
    } else {
        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"status",
                                              "Failure");
        XML_RET_CHECK_AND_GOTO(ret, xmlend);

        ret = cli_xml_output_common(writer, rsp->op_ret, rsp->op_errno,
                                    rsp->op_errstr);
    }
    XML_RET_CHECK_AND_GOTO(ret, xmlend);

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"name", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, xmlend);

    ret = dict_get_str(dict, "snapuuid", &str_value);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get snap uuid");
        goto xmlend;
    }

    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"uuid", "%s",
                                          str_value);
    XML_RET_CHECK_AND_GOTO(ret, out);

xmlend:
    /* </snapshot> */
    ret = xmlTextWriterEndElement(writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

#endif /* HAVE_LIB_XML */
    ret = 0;
out:

    return ret;
}

int
cli_xml_output_snap_status_begin(cli_local_t *local, int op_ret, int op_errno,
                                 char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    GF_ASSERT(local);

    ret = cli_begin_xml_output(&(local->writer), &(local->doc));
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapStatus> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapStatus");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapshots> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapshots");
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_TRACE, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_snap_status_end(cli_local_t *local)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    GF_ASSERT(local);

    /* </snapshots> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapStatus> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(local->writer, local->doc);
out:
    gf_log("cli", GF_LOG_TRACE, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_snap_delete_begin(cli_local_t *local, int op_ret, int op_errno,
                                 char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    int delete_cmd = -1;

    GF_ASSERT(local);

    ret = cli_begin_xml_output(&(local->writer), &(local->doc));
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(local->dict, "sub-cmd", &delete_cmd);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Failed to get sub-cmd");
        goto out;
    }

    ret = cli_xml_output_common(local->writer, op_ret, op_errno, op_errstr);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapStatus> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapDelete");
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* <snapshots> */
    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"snapshots");
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    gf_log("cli", GF_LOG_TRACE, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}

int
cli_xml_output_snap_delete_end(cli_local_t *local)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    GF_ASSERT(local);

    /* </snapshots> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    /* </snapDelete> */
    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = cli_end_xml_output(local->writer, local->doc);
out:
    gf_log("cli", GF_LOG_TRACE, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif
}
/* This function will generate xml output for all the snapshot commands
 *
 * @param cmd_type      command type
 * @param dict          dict containing snapshot command output
 * @param op_ret        return value of the snapshot command
 * @param op_errno      errno for the snapshot command
 * @param op_errstr     error string for the snapshot command
 *
 * @return 0 on success and -1 on failure
 */
int
cli_xml_output_snapshot(int cmd_type, dict_t *dict, int op_ret, int op_errno,
                        char *op_errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;

    GF_ASSERT(dict);

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to output "
               "xml begin block");
        goto out;
    }

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to output "
               "xml common block");
        goto out;
    }

    /* In case of command failure just printing the error message is good
     * enough */
    if (0 != op_ret) {
        goto end;
    }

    switch (cmd_type) {
        case GF_SNAP_OPTION_TYPE_CREATE:
            ret = cli_xml_snapshot_create(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot create command");
                goto out;
            }
            break;
        case GF_SNAP_OPTION_TYPE_CLONE:
            ret = cli_xml_snapshot_clone(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot clone command");
                goto out;
            }
            break;
        case GF_SNAP_OPTION_TYPE_RESTORE:
            ret = cli_xml_snapshot_restore(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot restore command");
                goto out;
            }
            break;
        case GF_SNAP_OPTION_TYPE_LIST:
            ret = cli_xml_snapshot_list(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot list command");
                goto out;
            }
            break;
        case GF_SNAP_OPTION_TYPE_STATUS:
            ret = cli_xml_snapshot_status(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create"
                       "xml output for snapshot status command");
                goto out;
            }
            break;
        case GF_SNAP_OPTION_TYPE_INFO:
            ret = cli_xml_snapshot_info(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot info command");
                goto out;
            }
            break;
        case GF_SNAP_OPTION_TYPE_ACTIVATE:
        case GF_SNAP_OPTION_TYPE_DEACTIVATE:
            ret = cli_xml_snapshot_activate_deactivate(writer, doc, dict,
                                                       cmd_type);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot config command");
            }
            break;
        case GF_SNAP_OPTION_TYPE_CONFIG:
            ret = cli_xml_snapshot_config(writer, doc, dict);
            if (ret) {
                gf_log("cli", GF_LOG_ERROR,
                       "Failed to create "
                       "xml output for snapshot config command");
            }
            break;
        default:
            gf_log("cli", GF_LOG_ERROR, "Unexpected snapshot command: %d",
                   cmd_type);
            goto out;
    }

end:
    ret = cli_end_xml_output(writer, doc);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to output "
               "xml end block");
        goto out;
    }

    ret = 0;
out:
    return ret;
#else
    return 0;
#endif /* HAVE_LIB_XML */
}

int
cli_xml_snapshot_begin_composite_op(cli_local_t *local)
{
    int ret = -1;
#ifdef HAVE_LIB_XML
    int cmd = -1;
    int type = -1;

    ret = dict_get_int32(local->dict, "sub-cmd", &cmd);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to get "
               "sub-cmd");
        ret = 0;
        goto out;
    }

    if (cmd == GF_SNAP_STATUS_TYPE_ITER || cmd == GF_SNAP_DELETE_TYPE_SNAP) {
        ret = 0;
        goto out;
    }

    ret = dict_get_int32(local->dict, "type", &type);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to get snapshot "
               "command type from dictionary");
        goto out;
    }

    if (GF_SNAP_OPTION_TYPE_STATUS == type)
        ret = cli_xml_output_snap_status_begin(local, 0, 0, NULL);
    else if (GF_SNAP_OPTION_TYPE_DELETE == type)
        ret = cli_xml_output_snap_delete_begin(local, 0, 0, NULL);

    if (ret) {
        gf_log("cli", GF_LOG_ERROR, "Error creating xml output");
        goto out;
    }

#endif /* HAVE_LIB_XML */
    ret = 0;
out:
    return ret;
}

int
cli_xml_snapshot_end_composite_op(cli_local_t *local)
{
    int ret = -1;
#ifdef HAVE_LIB_XML
    int cmd = -1;
    int type = -1;

    ret = dict_get_int32(local->dict, "sub-cmd", &cmd);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to get "
               "sub-cmd");
        ret = 0;
        goto out;
    }

    if (cmd == GF_SNAP_STATUS_TYPE_ITER || cmd == GF_SNAP_DELETE_TYPE_SNAP) {
        ret = 0;
        goto out;
    }

    ret = dict_get_int32(local->dict, "type", &type);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to get snapshot "
               "command type from dictionary");
        goto out;
    }

    if (GF_SNAP_OPTION_TYPE_STATUS == type)
        ret = cli_xml_output_snap_status_end(local);
    else if (GF_SNAP_OPTION_TYPE_DELETE == type)
        ret = cli_xml_output_snap_delete_end(local);

    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Error creating xml "
               "output");
        goto out;
    }
#endif /* HAVE_LIB_XML */
    ret = 0;
out:
    return ret;
}

int
cli_xml_snapshot_status_single_snap(cli_local_t *local, dict_t *dict, char *key)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    GF_VALIDATE_OR_GOTO("cli", (local != NULL), out);
    GF_VALIDATE_OR_GOTO("cli", (dict != NULL), out);
    GF_VALIDATE_OR_GOTO("cli", (key != NULL), out);

    ret = cli_xml_snapshot_status_per_snap(local->writer, local->doc, dict,
                                           key);
out:
    return ret;
#else
    return 0;
#endif /* HAVE_LIB_XML */
}

int
cli_xml_output_vol_getopts(dict_t *dict, int op_ret, int op_errno,
                           char *op_errstr)
{
#if (HAVE_LIB_XML)
    int i = 0;
    int ret = -1;
    int count = 0;
    xmlTextWriterPtr writer = NULL;
    xmlDocPtr doc = NULL;
    char *key = NULL;
    char *value = NULL;
    char dict_key[50] = {
        0,
    };

    ret = cli_begin_xml_output(&writer, &doc);
    if (ret)
        goto out;

    ret = cli_xml_output_common(writer, op_ret, op_errno, op_errstr);
    if (ret)
        goto out;

    ret = xmlTextWriterStartElement(writer, (xmlChar *)"volGetopts");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = dict_get_int32(dict, "count", &count);
    if (ret) {
        gf_log("cli", GF_LOG_ERROR,
               "Failed to retrieve count "
               "from the dictionary");
        goto out;
    }
    if (count <= 0) {
        gf_log("cli", GF_LOG_ERROR,
               "Value of count :%d is "
               "invalid",
               count);
        ret = -1;
        goto out;
    }
    ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"count", "%d",
                                          count);

    XML_RET_CHECK_AND_GOTO(ret, out);

    for (i = 1; i <= count; i++) {
        sprintf(dict_key, "key%d", i);
        ret = dict_get_str(dict, dict_key, &key);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR,
                   "Failed to"
                   " retrieve %s from the "
                   "dictionary",
                   dict_key);
            goto out;
        }
        sprintf(dict_key, "value%d", i);
        ret = dict_get_str(dict, dict_key, &value);
        if (ret) {
            gf_log("cli", GF_LOG_ERROR,
                   "Failed to "
                   "retrieve key value for %s from"
                   "the dictionary",
                   dict_key);
            goto out;
        }
        ret = xmlTextWriterStartElement(writer, (xmlChar *)"Opt");
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"Option", "%s",
                                              key);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterWriteFormatElement(writer, (xmlChar *)"Value", "%s",
                                              value);
        XML_RET_CHECK_AND_GOTO(ret, out);

        ret = xmlTextWriterEndElement(writer);
        XML_RET_CHECK_AND_GOTO(ret, out);
    }
    ret = cli_end_xml_output(writer, doc);

out:
    gf_log("cli", GF_LOG_DEBUG, "Returning %d", ret);
    return ret;
#else
    return 0;
#endif /* HAVE_LIB_XML */
}

int
cli_quota_list_xml_error(cli_local_t *local, char *path, char *errstr)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"limit");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"path",
                                          "%s", path);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"errstr",
                                          "%s", errstr);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterEndElement(local->writer);
    XML_RET_CHECK_AND_GOTO(ret, out);

out:
    return ret;
#else
    return 0;
#endif
}

int
cli_quota_xml_output(cli_local_t *local, char *path, int64_t hl_str,
                     char *sl_final, int64_t sl_num, int64_t used,
                     int64_t avail, char *sl, char *hl, gf_boolean_t limit_set)
{
#if (HAVE_LIB_XML)
    int ret = -1;

    ret = xmlTextWriterStartElement(local->writer, (xmlChar *)"limit");
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(local->writer, (xmlChar *)"path",
                                          "%s", path);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        local->writer, (xmlChar *)"hard_limit", !limit_set ? "N/A" : "%" PRId64,
        hl_str);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(local->writer,
                                          (xmlChar *)"soft_limit_percent",
                                          !limit_set ? "N/A" : "%s", sl_final);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        local->writer, (xmlChar *)"soft_limit_value",
        !limit_set ? "N/A" : "%" PRId64, sl_num);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        local->writer, (xmlChar *)"used_space", "%" PRId64, used);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        local->writer, (xmlChar *)"avail_space",
        !limit_set ? "N/A" : "%" PRId64, avail);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        local->writer, (xmlChar *)"sl_exceeded", !limit_set ? "N/A" : "%s", sl);
    XML_RET_CHECK_AND_GOTO(ret, out);

    ret = xmlTextWriterWriteFormatElement(
        local->writer, (xmlChar *)"hl_exceeded", !limit_set ? "N/A" : "%s", hl);