| 1 | /* |
| 2 | * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved. |
| 3 | * |
| 4 | * This code is derived from software contributed to The DragonFly Project |
| 5 | * by Matthew Dillon <dillon@dragonflybsd.org> |
| 6 | * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in |
| 16 | * the documentation and/or other materials provided with the |
| 17 | * distribution. |
| 18 | * 3. Neither the name of The DragonFly Project nor the names of its |
| 19 | * contributors may be used to endorse or promote products derived |
| 20 | * from this software without specific, prior written permission. |
| 21 | * |
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 25 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 26 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 27 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 28 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 29 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 30 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 31 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 32 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 33 | * SUCH DAMAGE. |
| 34 | */ |
| 35 | |
| 36 | #include "hammer2.h" |
| 37 | |
| 38 | #define SHOW_TAB 2 |
| 39 | |
| 40 | static void shell_rcvmsg(dmsg_msg_t *msg); |
| 41 | static void shell_ttymsg(dmsg_iocom_t *iocom); |
| 42 | |
| 43 | /************************************************************************ |
| 44 | * SHELL * |
| 45 | ************************************************************************/ |
| 46 | |
| 47 | int |
| 48 | cmd_shell(const char *hostname) |
| 49 | { |
| 50 | struct dmsg_iocom iocom; |
| 51 | dmsg_msg_t *msg; |
| 52 | int fd; |
| 53 | |
| 54 | /* |
| 55 | * Connect to the target |
| 56 | */ |
| 57 | fd = dmsg_connect(hostname); |
| 58 | if (fd < 0) |
| 59 | return 1; |
| 60 | |
| 61 | /* |
| 62 | * Run the session. The remote end transmits our prompt. |
| 63 | */ |
| 64 | dmsg_iocom_init(&iocom, fd, 0, |
| 65 | NULL, |
| 66 | shell_rcvmsg, |
| 67 | hammer2_shell_parse, |
| 68 | shell_ttymsg); |
| 69 | fcntl(0, F_SETFL, O_NONBLOCK); |
| 70 | printf("debug: connected\n"); |
| 71 | |
| 72 | msg = dmsg_msg_alloc(iocom.router, 0, DMSG_DBG_SHELL, NULL, NULL); |
| 73 | dmsg_msg_write(msg); |
| 74 | dmsg_iocom_core(&iocom); |
| 75 | fprintf(stderr, "debug: disconnected\n"); |
| 76 | close(fd); |
| 77 | return 0; |
| 78 | } |
| 79 | |
| 80 | /* |
| 81 | * Callback from dmsg_iocom_core() when messages might be present |
| 82 | * on the socket. |
| 83 | */ |
| 84 | static |
| 85 | void |
| 86 | shell_rcvmsg(dmsg_msg_t *msg) |
| 87 | { |
| 88 | switch(msg->any.head.cmd & DMSGF_TRANSMASK) { |
| 89 | case DMSG_LNK_ERROR: |
| 90 | case DMSG_LNK_ERROR | DMSGF_REPLY: |
| 91 | /* |
| 92 | * One-way non-transactional LNK_ERROR messages typically |
| 93 | * indicate a connection failure. Error code 0 is used by |
| 94 | * the debug shell to indicate no more results from last cmd. |
| 95 | */ |
| 96 | if (msg->any.head.error) { |
| 97 | fprintf(stderr, "Stream failure: %s\n", |
| 98 | dmsg_msg_str(msg)); |
| 99 | } else { |
| 100 | write(1, "debug> ", 7); |
| 101 | } |
| 102 | break; |
| 103 | case DMSG_LNK_ERROR | DMSGF_DELETE: |
| 104 | /* ignore termination of LNK_CONN */ |
| 105 | break; |
| 106 | case DMSG_DBG_SHELL: |
| 107 | /* |
| 108 | * We send the commands, not accept them. |
| 109 | * (one-way message, not transactional) |
| 110 | */ |
| 111 | dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); |
| 112 | break; |
| 113 | case DMSG_DBG_SHELL | DMSGF_REPLY: |
| 114 | /* |
| 115 | * A reply from the remote is data we copy to stdout. |
| 116 | * (one-way message, not transactional) |
| 117 | */ |
| 118 | if (msg->aux_size) { |
| 119 | msg->aux_data[msg->aux_size - 1] = 0; |
| 120 | write(1, msg->aux_data, strlen(msg->aux_data)); |
| 121 | } |
| 122 | break; |
| 123 | case DMSG_LNK_CONN | DMSGF_CREATE: |
| 124 | fprintf(stderr, "Debug Shell is ignoring received LNK_CONN\n"); |
| 125 | dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); |
| 126 | break; |
| 127 | case DMSG_LNK_CONN | DMSGF_DELETE: |
| 128 | break; |
| 129 | default: |
| 130 | /* |
| 131 | * Ignore any unknown messages, Terminate any unknown |
| 132 | * transactions with an error. |
| 133 | */ |
| 134 | fprintf(stderr, "Unknown message: %s\n", dmsg_msg_str(msg)); |
| 135 | if (msg->any.head.cmd & DMSGF_CREATE) |
| 136 | dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); |
| 137 | if (msg->any.head.cmd & DMSGF_DELETE) |
| 138 | dmsg_msg_reply(msg, DMSG_ERR_NOSUPP); |
| 139 | break; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static |
| 144 | void |
| 145 | shell_ttymsg(dmsg_iocom_t *iocom) |
| 146 | { |
| 147 | dmsg_msg_t *msg; |
| 148 | char buf[256]; |
| 149 | size_t len; |
| 150 | |
| 151 | if (fgets(buf, sizeof(buf), stdin) != NULL) { |
| 152 | len = strlen(buf); |
| 153 | if (len && buf[len - 1] == '\n') |
| 154 | buf[--len] = 0; |
| 155 | ++len; |
| 156 | msg = dmsg_msg_alloc(iocom->router, len, DMSG_DBG_SHELL, |
| 157 | NULL, NULL); |
| 158 | bcopy(buf, msg->aux_data, len); |
| 159 | dmsg_msg_write(msg); |
| 160 | } else if (feof(stdin)) { |
| 161 | /* |
| 162 | * Set EOF flag without setting any error code for normal |
| 163 | * EOF. |
| 164 | */ |
| 165 | iocom->flags |= DMSG_IOCOMF_EOF; |
| 166 | } else { |
| 167 | clearerr(stdin); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void shell_span(dmsg_router_t *router, char *cmdbuf); |
| 172 | |
| 173 | void |
| 174 | hammer2_shell_parse(dmsg_msg_t *msg) |
| 175 | { |
| 176 | dmsg_router_t *router = msg->router; |
| 177 | char *cmdbuf = msg->aux_data; |
| 178 | char *cmd = strsep(&cmdbuf, " \t"); |
| 179 | |
| 180 | if (cmd == NULL || *cmd == 0) { |
| 181 | ; |
| 182 | } else if (strcmp(cmd, "span") == 0) { |
| 183 | shell_span(router, cmdbuf); |
| 184 | } else if (strcmp(cmd, "tree") == 0) { |
| 185 | dmsg_shell_tree(router, cmdbuf); /* dump spanning tree */ |
| 186 | } else if (strcmp(cmd, "help") == 0 || strcmp(cmd, "?") == 0) { |
| 187 | dmsg_router_printf(router, "help Command help\n"); |
| 188 | dmsg_router_printf(router, "span <host> Span to target host\n"); |
| 189 | dmsg_router_printf(router, "tree Dump spanning tree\n"); |
| 190 | } else { |
| 191 | dmsg_router_printf(router, "Unrecognized command: %s\n", cmd); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | static void |
| 196 | shell_span(dmsg_router_t *router, char *cmdbuf) |
| 197 | { |
| 198 | dmsg_master_service_info_t *info; |
| 199 | const char *hostname = strsep(&cmdbuf, " \t"); |
| 200 | pthread_t thread; |
| 201 | int fd; |
| 202 | |
| 203 | /* |
| 204 | * Connect to the target |
| 205 | */ |
| 206 | if (hostname == NULL) { |
| 207 | fd = -1; |
| 208 | } else { |
| 209 | fd = dmsg_connect(hostname); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Start master service |
| 214 | */ |
| 215 | if (fd < 0) { |
| 216 | dmsg_router_printf(router, "Connection to %s failed\n", hostname); |
| 217 | } else { |
| 218 | dmsg_router_printf(router, "Connected to %s\n", hostname); |
| 219 | |
| 220 | info = malloc(sizeof(*info)); |
| 221 | bzero(info, sizeof(*info)); |
| 222 | info->fd = fd; |
| 223 | info->detachme = 1; |
| 224 | info->dbgmsg_callback = hammer2_shell_parse; |
| 225 | |
| 226 | pthread_create(&thread, NULL, dmsg_master_service, info); |
| 227 | /*pthread_join(thread, &res);*/ |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /************************************************************************ |
| 232 | * DEBUGSPAN * |
| 233 | ************************************************************************ |
| 234 | * |
| 235 | * Connect to the target manually (not via the cluster list embedded in |
| 236 | * a hammer2 filesystem) and initiate the SPAN protocol. |
| 237 | */ |
| 238 | int |
| 239 | cmd_debugspan(const char *hostname) |
| 240 | { |
| 241 | pthread_t thread; |
| 242 | int fd; |
| 243 | void *res; |
| 244 | |
| 245 | /* |
| 246 | * Connect to the target |
| 247 | */ |
| 248 | fd = dmsg_connect(hostname); |
| 249 | if (fd < 0) |
| 250 | return 1; |
| 251 | |
| 252 | printf("debugspan: connected to %s, starting CONN/SPAN\n", hostname); |
| 253 | pthread_create(&thread, NULL, |
| 254 | dmsg_master_service, (void *)(intptr_t)fd); |
| 255 | pthread_join(thread, &res); |
| 256 | return(0); |
| 257 | } |
| 258 | |
| 259 | /************************************************************************ |
| 260 | * SHOW * |
| 261 | ************************************************************************/ |
| 262 | |
| 263 | static void show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref); |
| 264 | static void tabprintf(int tab, const char *ctl, ...); |
| 265 | |
| 266 | int |
| 267 | cmd_show(const char *devpath) |
| 268 | { |
| 269 | hammer2_blockref_t broot; |
| 270 | int fd; |
| 271 | |
| 272 | fd = open(devpath, O_RDONLY); |
| 273 | if (fd < 0) { |
| 274 | perror("open"); |
| 275 | return 1; |
| 276 | } |
| 277 | bzero(&broot, sizeof(broot)); |
| 278 | broot.type = HAMMER2_BREF_TYPE_VOLUME; |
| 279 | broot.data_off = 0 | HAMMER2_PBUFRADIX; |
| 280 | show_bref(fd, 0, 0, &broot); |
| 281 | close(fd); |
| 282 | |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static void |
| 287 | show_bref(int fd, int tab, int bi, hammer2_blockref_t *bref) |
| 288 | { |
| 289 | hammer2_media_data_t media; |
| 290 | hammer2_blockref_t *bscan; |
| 291 | int bcount; |
| 292 | int i; |
| 293 | int didnl; |
| 294 | int namelen; |
| 295 | int obrace = 1; |
| 296 | size_t bytes; |
| 297 | const char *type_str; |
| 298 | char *str = NULL; |
| 299 | |
| 300 | switch(bref->type) { |
| 301 | case HAMMER2_BREF_TYPE_EMPTY: |
| 302 | type_str = "empty"; |
| 303 | break; |
| 304 | case HAMMER2_BREF_TYPE_INODE: |
| 305 | type_str = "inode"; |
| 306 | break; |
| 307 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 308 | type_str = "indblk"; |
| 309 | break; |
| 310 | case HAMMER2_BREF_TYPE_DATA: |
| 311 | type_str = "data"; |
| 312 | break; |
| 313 | case HAMMER2_BREF_TYPE_VOLUME: |
| 314 | type_str = "volume"; |
| 315 | break; |
| 316 | default: |
| 317 | type_str = "unknown"; |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | |
| 322 | tabprintf(tab, "%s.%-3d %016jx %016jx/%-2d mir=%016jx mod=%016jx ", |
| 323 | type_str, bi, (intmax_t)bref->data_off, |
| 324 | (intmax_t)bref->key, (intmax_t)bref->keybits, |
| 325 | (intmax_t)bref->mirror_tid, (intmax_t)bref->modify_tid); |
| 326 | tab += SHOW_TAB; |
| 327 | |
| 328 | bytes = (size_t)1 << (bref->data_off & HAMMER2_OFF_MASK_RADIX); |
| 329 | if (bytes < HAMMER2_MINIOSIZE || bytes > sizeof(media)) { |
| 330 | printf("(bad block size %zd)\n", bytes); |
| 331 | return; |
| 332 | } |
| 333 | if (bref->type != HAMMER2_BREF_TYPE_DATA || VerboseOpt >= 1) { |
| 334 | lseek(fd, bref->data_off & ~HAMMER2_OFF_MASK_RADIX, 0); |
| 335 | if (read(fd, &media, bytes) != (ssize_t)bytes) { |
| 336 | printf("(media read failed)\n"); |
| 337 | return; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | bscan = NULL; |
| 342 | bcount = 0; |
| 343 | didnl = 0; |
| 344 | |
| 345 | switch(bref->type) { |
| 346 | case HAMMER2_BREF_TYPE_EMPTY: |
| 347 | obrace = 0; |
| 348 | break; |
| 349 | case HAMMER2_BREF_TYPE_INODE: |
| 350 | printf("{\n"); |
| 351 | if (media.ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) { |
| 352 | /* no blockrefs */ |
| 353 | } else { |
| 354 | bscan = &media.ipdata.u.blockset.blockref[0]; |
| 355 | bcount = HAMMER2_SET_COUNT; |
| 356 | } |
| 357 | namelen = media.ipdata.name_len; |
| 358 | if (namelen > HAMMER2_INODE_MAXNAME) |
| 359 | namelen = 0; |
| 360 | tabprintf(tab, "filename \"%*.*s\"\n", |
| 361 | namelen, namelen, media.ipdata.filename); |
| 362 | tabprintf(tab, "version %d\n", media.ipdata.version); |
| 363 | tabprintf(tab, "uflags 0x%08x\n", |
| 364 | media.ipdata.uflags); |
| 365 | if (media.ipdata.rmajor || media.ipdata.rminor) { |
| 366 | tabprintf(tab, "rmajor %d\n", |
| 367 | media.ipdata.rmajor); |
| 368 | tabprintf(tab, "rminor %d\n", |
| 369 | media.ipdata.rminor); |
| 370 | } |
| 371 | tabprintf(tab, "ctime %s\n", |
| 372 | hammer2_time64_to_str(media.ipdata.ctime, &str)); |
| 373 | tabprintf(tab, "mtime %s\n", |
| 374 | hammer2_time64_to_str(media.ipdata.mtime, &str)); |
| 375 | tabprintf(tab, "atime %s\n", |
| 376 | hammer2_time64_to_str(media.ipdata.atime, &str)); |
| 377 | tabprintf(tab, "btime %s\n", |
| 378 | hammer2_time64_to_str(media.ipdata.btime, &str)); |
| 379 | tabprintf(tab, "uid %s\n", |
| 380 | hammer2_uuid_to_str(&media.ipdata.uid, &str)); |
| 381 | tabprintf(tab, "gid %s\n", |
| 382 | hammer2_uuid_to_str(&media.ipdata.gid, &str)); |
| 383 | tabprintf(tab, "type %s\n", |
| 384 | hammer2_iptype_to_str(media.ipdata.type)); |
| 385 | tabprintf(tab, "opflgs 0x%02x\n", |
| 386 | media.ipdata.op_flags); |
| 387 | tabprintf(tab, "capflgs 0x%04x\n", |
| 388 | media.ipdata.cap_flags); |
| 389 | tabprintf(tab, "mode %-7o\n", |
| 390 | media.ipdata.mode); |
| 391 | tabprintf(tab, "inum 0x%016jx\n", |
| 392 | media.ipdata.inum); |
| 393 | tabprintf(tab, "size %ju\n", |
| 394 | (uintmax_t)media.ipdata.size); |
| 395 | tabprintf(tab, "nlinks %ju\n", |
| 396 | (uintmax_t)media.ipdata.nlinks); |
| 397 | tabprintf(tab, "iparent 0x%016jx\n", |
| 398 | (uintmax_t)media.ipdata.iparent); |
| 399 | tabprintf(tab, "name_key 0x%016jx\n", |
| 400 | (uintmax_t)media.ipdata.name_key); |
| 401 | tabprintf(tab, "name_len %u\n", |
| 402 | media.ipdata.name_len); |
| 403 | tabprintf(tab, "ncopies %u\n", |
| 404 | media.ipdata.ncopies); |
| 405 | tabprintf(tab, "compalg %u\n", |
| 406 | media.ipdata.comp_algo); |
| 407 | if (media.ipdata.op_flags & HAMMER2_OPFLAG_PFSROOT) { |
| 408 | tabprintf(tab, "pfs_type %u (%s)\n", |
| 409 | media.ipdata.pfs_type, |
| 410 | hammer2_pfstype_to_str(media.ipdata.pfs_type)); |
| 411 | tabprintf(tab, "pfs_inum 0x%016jx\n", |
| 412 | (uintmax_t)media.ipdata.pfs_inum); |
| 413 | tabprintf(tab, "pfs_clid %s\n", |
| 414 | hammer2_uuid_to_str(&media.ipdata.pfs_clid, |
| 415 | &str)); |
| 416 | tabprintf(tab, "pfs_fsid %s\n", |
| 417 | hammer2_uuid_to_str(&media.ipdata.pfs_fsid, |
| 418 | &str)); |
| 419 | } |
| 420 | tabprintf(tab, "data_quota %ju\n", |
| 421 | (uintmax_t)media.ipdata.data_quota); |
| 422 | tabprintf(tab, "data_count %ju\n", |
| 423 | (uintmax_t)media.ipdata.data_count); |
| 424 | tabprintf(tab, "inode_quota %ju\n", |
| 425 | (uintmax_t)media.ipdata.inode_quota); |
| 426 | tabprintf(tab, "inode_count %ju\n", |
| 427 | (uintmax_t)media.ipdata.inode_count); |
| 428 | tabprintf(tab, "attr_tid 0x%016jx\n", |
| 429 | (uintmax_t)media.ipdata.attr_tid); |
| 430 | if (media.ipdata.type == HAMMER2_OBJTYPE_DIRECTORY) { |
| 431 | tabprintf(tab, "dirent_tid %016jx\n", |
| 432 | (uintmax_t)media.ipdata.dirent_tid); |
| 433 | } |
| 434 | break; |
| 435 | case HAMMER2_BREF_TYPE_INDIRECT: |
| 436 | bscan = &media.npdata.blockref[0]; |
| 437 | bcount = bytes / sizeof(hammer2_blockref_t); |
| 438 | didnl = 1; |
| 439 | printf("{\n"); |
| 440 | break; |
| 441 | case HAMMER2_BREF_TYPE_DATA: |
| 442 | if (VerboseOpt >= 2) { |
| 443 | printf("{\n"); |
| 444 | } else { |
| 445 | printf("\n"); |
| 446 | obrace = 0; |
| 447 | } |
| 448 | break; |
| 449 | case HAMMER2_BREF_TYPE_VOLUME: |
| 450 | bscan = &media.voldata.sroot_blockset.blockref[0]; |
| 451 | bcount = HAMMER2_SET_COUNT; |
| 452 | printf("{\n"); |
| 453 | break; |
| 454 | default: |
| 455 | break; |
| 456 | } |
| 457 | if (str) |
| 458 | free(str); |
| 459 | for (i = 0; i < bcount; ++i) { |
| 460 | if (bscan[i].type != HAMMER2_BREF_TYPE_EMPTY) { |
| 461 | if (didnl == 0) { |
| 462 | printf("\n"); |
| 463 | didnl = 1; |
| 464 | } |
| 465 | show_bref(fd, tab, i, &bscan[i]); |
| 466 | } |
| 467 | } |
| 468 | tab -= SHOW_TAB; |
| 469 | if (obrace) { |
| 470 | if (bref->type == HAMMER2_BREF_TYPE_INODE) |
| 471 | tabprintf(tab, "} (%s.%d, \"%s\")\n", |
| 472 | type_str, bi, media.ipdata.filename); |
| 473 | else |
| 474 | tabprintf(tab, "} (%s.%d)\n", type_str,bi); |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | static |
| 479 | void |
| 480 | tabprintf(int tab, const char *ctl, ...) |
| 481 | { |
| 482 | va_list va; |
| 483 | |
| 484 | printf("%*.*s", tab, tab, ""); |
| 485 | va_start(va, ctl); |
| 486 | vprintf(ctl, va); |
| 487 | va_end(va); |
| 488 | } |