/* Test llapi_hsm_copytool_recv with bogus parameters */ int test6(void) { struct hsm_copytool_private *ctdata; struct hsm_action_list *hal; int rc; int msgsize; rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 0, NULL, 0); ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s", strerror(-rc)); rc = llapi_hsm_copytool_recv(NULL, &hal, &msgsize); ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s", strerror(-rc)); rc = llapi_hsm_copytool_recv(ctdata, NULL, &msgsize); ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s", strerror(-rc)); rc = llapi_hsm_copytool_recv(ctdata, &hal, NULL); ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s", strerror(-rc)); rc = llapi_hsm_copytool_recv(ctdata, NULL, NULL); ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s", strerror(-rc)); rc = llapi_hsm_copytool_unregister(&ctdata); ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s", strerror(-rc)); return 0; }
/* Test llapi_hsm_copytool_recv in non blocking mode */ int test5(void) { int rc; int i; struct hsm_copytool_private *ctdata; struct hsm_action_list *hal; int msgsize; rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 0, NULL, O_NONBLOCK); ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s", strerror(-rc)); /* Hopefully there is nothing lingering */ for (i = 0; i < 1000; i++) { rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize); ASSERTF(rc == -EWOULDBLOCK, "llapi_hsm_copytool_recv error: %s", strerror(-rc)); } rc = llapi_hsm_copytool_unregister(&ctdata); ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s", strerror(-rc)); return 0; }
/* Test polling (without actual traffic) */ int test7(void) { int rc; struct hsm_copytool_private *ctdata; struct hsm_action_list *hal; int msgsize; int fd; struct pollfd fds[1]; rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 0, NULL, O_NONBLOCK); ASSERTF(rc == 0, "llapi_hsm_copytool_register failed: %s", strerror(-rc)); fd = llapi_hsm_copytool_get_fd(ctdata); ASSERTF(fd >= 0, "llapi_hsm_copytool_get_fd failed: %s", strerror(-rc)); /* Ensure it's read-only */ rc = write(fd, &rc, 1); ASSERTF(rc == -1 && errno == EBADF, "write error: %d, %s", rc, strerror(errno)); rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize); ASSERTF(rc == -EWOULDBLOCK, "llapi_hsm_copytool_recv error: %s", strerror(-rc)); fds[0].fd = fd; fds[0].events = POLLIN; rc = poll(fds, 1, 10); ASSERTF(rc == 0, "poll failed: %d, %s", rc, strerror(errno)); /* no event */ rc = llapi_hsm_copytool_unregister(&ctdata); ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s", strerror(-rc)); return 0; }
int main(int argc, char **argv) { int c, test = 0; struct option long_opts[] = { {"test", no_argument, 0, 't'}, {0, 0, 0, 0} }; int archives[] = {1}; /* which archives we care about */ int rc; optind = 0; while ((c = getopt_long(argc, argv, "t", long_opts, NULL)) != -1) { switch (c) { case 't': test++; break; default: fprintf(stderr, "error: %s: option '%s' unrecognized\n", argv[0], argv[optind - 1]); return EINVAL; } } if (optind != argc - 1) { fprintf(stderr, "Usage: %s <fsname>\n", argv[0]); return -EINVAL; } rc = llapi_hsm_copytool_start(&ctdata, argv[optind], 0, ARRAY_SIZE(archives), archives); if (rc < 0) { fprintf(stderr, "Can't start copytool interface: %s\n", strerror(-rc)); return -rc; } if (test) return -llapi_hsm_copytool_fini(&ctdata); printf("Waiting for message from kernel (pid=%d)\n", getpid()); signal(SIGINT, handler); while(1) { struct hsm_action_list *hal; struct hsm_action_item *hai; int msgsize, i = 0; rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize); if (rc == -ESHUTDOWN) { fprintf(stderr, "shutting down"); break; } if (rc < 0) { fprintf(stderr, "Message receive: %s", strerror(-rc)); break; } if (msgsize == 0) continue; /* msg not for us */ printf("Copytool fs=%s archive#=%d item_count=%d\n", hal->hal_fsname, hal->hal_archive_id, hal->hal_count); hai = hai_zero(hal); while (++i <= hal->hal_count) { printf("Item %d: action %d reclen %d\n", i, hai->hai_action, hai->hai_len); printf(" "DFID" gid="LPU64" cookie="LPU64"\n", PFID(&hai->hai_fid), hai->hai_gid, hai->hai_cookie); hai = hai_next(hai); } llapi_hsm_copytool_free(&hal); } llapi_hsm_copytool_fini(&ctdata); return -rc; }
/* Helper to simulate archiving a file. No actual data movement * happens. */ void helper_archiving(void (*progress) (struct hsm_copyaction_private *hcp, size_t length), const size_t length) { int rc; int fd; struct hsm_copytool_private *ctdata; struct hsm_user_request *hur; struct hsm_action_list *hal; struct hsm_action_item *hai; int msgsize; struct hsm_copyaction_private *hcp; struct hsm_user_state hus; fd = create_testfile(length); rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 0, NULL, 0); ASSERTF(rc == 0, "llapi_hsm_copytool_register failed: %s", strerror(-rc)); /* Create and send the archive request. */ hur = llapi_hsm_user_request_alloc(1, 0); ASSERTF(hur != NULL, "llapi_hsm_user_request_alloc returned NULL"); hur->hur_request.hr_action = HUA_ARCHIVE; hur->hur_request.hr_archive_id = 1; hur->hur_request.hr_flags = 0; hur->hur_request.hr_itemcount = 1; hur->hur_request.hr_data_len = 0; hur->hur_user_item[0].hui_extent.offset = 0; hur->hur_user_item[0].hui_extent.length = -1; rc = llapi_fd2fid(fd, &hur->hur_user_item[0].hui_fid); ASSERTF(rc == 0, "llapi_fd2fid failed: %s", strerror(-rc)); close(fd); rc = llapi_hsm_request(testfile, hur); ASSERTF(rc == 0, "llapi_hsm_request failed: %s", strerror(-rc)); free(hur); /* Read the request */ rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize); ASSERTF(rc == 0, "llapi_hsm_copytool_recv failed: %s", strerror(-rc)); ASSERTF(hal->hal_count == 1, "hal_count=%d", hal->hal_count); hai = hai_first(hal); ASSERTF(hai != NULL, "hai_first returned NULL"); ASSERTF(hai->hai_action == HSMA_ARCHIVE, "hai_action=%d", hai->hai_action); /* "Begin" archiving */ hcp = NULL; rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, false); ASSERTF(rc == 0, "llapi_hsm_action_begin failed: %s", strerror(-rc)); ASSERTF(hcp != NULL, "hcp is NULL"); if (progress) progress(hcp, length); /* Done archiving */ rc = llapi_hsm_action_end(&hcp, &hai->hai_extent, 0, 0); ASSERTF(rc == 0, "llapi_hsm_action_end failed: %s", strerror(-rc)); ASSERTF(hcp == NULL, "hcp is NULL"); /* Close HSM client */ rc = llapi_hsm_copytool_unregister(&ctdata); ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s", strerror(-rc)); /* Final check */ rc = llapi_hsm_state_get(testfile, &hus); ASSERTF(rc == 0, "llapi_hsm_state_get failed: %s", strerror(-rc)); ASSERTF(hus.hus_states == (HS_EXISTS | HS_ARCHIVED), "state=%u", hus.hus_states); }