Whamcloud - gitweb
LU-7243 misc: update Intel copyright messages 2015
[fs/lustre-release.git] / lustre / tests / llapi_hsm_test.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22
23 /*
24  * Copyright 2014, 2015 Cray Inc, all rights reserved.
25  *
26  * Copyright (c) 2015, Intel Corporation.
27  */
28 /* Some portions are extracted from llapi_layout_test.c */
29
30 /* The purpose of this test is to check some HSM functions. HSM must
31  * be enabled before running it:
32  *   echo enabled > /proc/fs/lustre/mdt/lustre-MDT0000/hsm_control
33  */
34
35 /* All tests return 0 on success and non zero on error. The program will
36  * exit as soon a non zero error is returned. */
37
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <getopt.h>
41 #include <fcntl.h>
42 #include <unistd.h>
43 #include <poll.h>
44 #include <time.h>
45
46 #include <lustre/lustreapi.h>
47
48 static char fsmountdir[PATH_MAX];      /* Lustre mountpoint */
49 static char *lustre_dir;               /* Test directory inside Lustre */
50
51 #define ERROR(fmt, ...)                                                 \
52         fprintf(stderr, "%s: %s:%d: %s: " fmt "\n",                     \
53                 program_invocation_short_name, __FILE__, __LINE__,      \
54                 __func__, ## __VA_ARGS__);
55
56 #define DIE(fmt, ...)                           \
57         do {                                    \
58                 ERROR(fmt, ## __VA_ARGS__);     \
59                 exit(EXIT_FAILURE);             \
60         } while (0)
61
62 #define ASSERTF(cond, fmt, ...)                                         \
63         do {                                                            \
64                 if (!(cond))                                            \
65                         DIE("assertion '%s' failed: "fmt,               \
66                             #cond, ## __VA_ARGS__);                     \
67         } while (0)                                                     \
68
69 #define PERFORM(testfn) \
70         do {                                                            \
71                 fprintf(stderr, "Starting test " #testfn " at %llu\n",  \
72                         (unsigned long long)time(NULL));                \
73                 testfn();                                               \
74                 fprintf(stderr, "Finishing test " #testfn " at %llu\n", \
75                        (unsigned long long)time(NULL));                 \
76         } while (0)
77
78 /* Register and unregister 2000 times. Ensures there is no fd leak
79  * since there is usually 1024 fd per process. */
80 int test1(void)
81 {
82         int i;
83         int rc;
84         struct hsm_copytool_private *ctdata;
85
86         for (i = 0; i < 2000; i++) {
87                 rc = llapi_hsm_copytool_register(&ctdata, fsmountdir,
88                                                  0, NULL, 0);
89                 ASSERTF(rc == 0,
90                         "llapi_hsm_copytool_register failed: %s, loop=%d",
91                         strerror(-rc), i);
92
93                 rc = llapi_hsm_copytool_unregister(&ctdata);
94                 ASSERTF(rc == 0,
95                         "llapi_hsm_copytool_unregister failed: %s, loop=%d",
96                         strerror(-rc), i);
97         }
98
99         return 0;
100 }
101
102 /* Re-register */
103 int test2(void)
104 {
105         int rc;
106         struct hsm_copytool_private *ctdata1;
107         struct hsm_copytool_private *ctdata2;
108
109         rc = llapi_hsm_copytool_register(&ctdata1, fsmountdir, 0, NULL, 0);
110         ASSERTF(rc == 0, "llapi_hsm_copytool_register failed: %s",
111                 strerror(-rc));
112
113         rc = llapi_hsm_copytool_register(&ctdata2, fsmountdir, 0, NULL, 0);
114         ASSERTF(rc == 0, "llapi_hsm_copytool_register failed: %s",
115                 strerror(-rc));
116
117         rc = llapi_hsm_copytool_unregister(&ctdata2);
118         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
119                 strerror(-rc));
120
121         rc = llapi_hsm_copytool_unregister(&ctdata1);
122         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
123                 strerror(-rc));
124
125         return 0;
126 }
127
128 /* Bad parameters to llapi_hsm_copytool_register(). */
129 int test3(void)
130 {
131         int rc;
132         struct hsm_copytool_private *ctdata;
133         int archives[33];
134
135         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 1, NULL, 0);
136         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_register error: %s",
137                 strerror(-rc));
138
139         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 33, NULL, 0);
140         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_register error: %s",
141                 strerror(-rc));
142
143         memset(archives, 1, sizeof(archives));
144         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 34, archives, 0);
145         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_register error: %s",
146                 strerror(-rc));
147
148 #if 0
149         /* BUG? Should that fail or not? */
150         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, -1, NULL, 0);
151         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_register error: %s",
152                 strerror(-rc));
153 #endif
154
155         memset(archives, -1, sizeof(archives));
156         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 1, archives, 0);
157         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_register error: %s",
158                 strerror(-rc));
159
160         rc = llapi_hsm_copytool_register(&ctdata, "/tmp", 0, NULL, 0);
161         ASSERTF(rc == -ENOENT, "llapi_hsm_copytool_register error: %s",
162                 strerror(-rc));
163
164         return 0;
165 }
166
167 /* Bad parameters to llapi_hsm_copytool_unregister(). */
168 int test4(void)
169 {
170         int rc;
171
172         rc = llapi_hsm_copytool_unregister(NULL);
173         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_unregister error: %s",
174                 strerror(-rc));
175
176         return 0;
177 }
178
179 /* Test llapi_hsm_copytool_recv in non blocking mode */
180 int test5(void)
181 {
182         int rc;
183         int i;
184         struct hsm_copytool_private *ctdata;
185         struct hsm_action_list  *hal;
186         int msgsize;
187
188         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir,
189                                          0, NULL, O_NONBLOCK);
190         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
191                 strerror(-rc));
192
193         /* Hopefully there is nothing lingering */
194         for (i = 0; i < 1000; i++) {
195                 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
196                 ASSERTF(rc == -EWOULDBLOCK, "llapi_hsm_copytool_recv error: %s",
197                         strerror(-rc));
198         }
199
200         rc = llapi_hsm_copytool_unregister(&ctdata);
201         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
202                 strerror(-rc));
203
204         return 0;
205 }
206
207 /* Test llapi_hsm_copytool_recv with bogus parameters */
208 int test6(void)
209 {
210         struct hsm_copytool_private *ctdata;
211         struct hsm_action_list *hal;
212         int rc;
213         int msgsize;
214
215         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir, 0, NULL, 0);
216         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
217                 strerror(-rc));
218
219         rc = llapi_hsm_copytool_recv(NULL, &hal, &msgsize);
220         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s",
221                 strerror(-rc));
222
223         rc = llapi_hsm_copytool_recv(ctdata, NULL, &msgsize);
224         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s",
225                 strerror(-rc));
226
227         rc = llapi_hsm_copytool_recv(ctdata, &hal, NULL);
228         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s",
229                 strerror(-rc));
230
231         rc = llapi_hsm_copytool_recv(ctdata, NULL, NULL);
232         ASSERTF(rc == -EINVAL, "llapi_hsm_copytool_recv error: %s",
233                 strerror(-rc));
234
235         rc = llapi_hsm_copytool_unregister(&ctdata);
236         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
237                 strerror(-rc));
238
239         return 0;
240 }
241
242 /* Test polling (without actual traffic) */
243 int test7(void)
244 {
245         int rc;
246         struct hsm_copytool_private *ctdata;
247         struct hsm_action_list  *hal;
248         int msgsize;
249         int fd;
250         struct pollfd fds[1];
251
252         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir,
253                                          0, NULL, O_NONBLOCK);
254         ASSERTF(rc == 0, "llapi_hsm_copytool_register failed: %s",
255                 strerror(-rc));
256
257         fd = llapi_hsm_copytool_get_fd(ctdata);
258         ASSERTF(fd >= 0, "llapi_hsm_copytool_get_fd failed: %s",
259                 strerror(-rc));
260
261         /* Ensure it's read-only */
262         rc = write(fd, &rc, 1);
263         ASSERTF(rc == -1 && errno == EBADF, "write error: %d, %s",
264                 rc, strerror(errno));
265
266         rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
267         ASSERTF(rc == -EWOULDBLOCK, "llapi_hsm_copytool_recv error: %s",
268                 strerror(-rc));
269
270         fds[0].fd = fd;
271         fds[0].events = POLLIN;
272         rc = poll(fds, 1, 10);
273         ASSERTF(rc == 0, "poll failed: %d, %s",
274                 rc, strerror(errno)); /* no event */
275
276         rc = llapi_hsm_copytool_unregister(&ctdata);
277         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
278                 strerror(-rc));
279
280         return 0;
281 }
282
283 /* Create the testfile of a given length. It returns a valid file
284  * descriptor. */
285 static char testfile[PATH_MAX];
286 static int create_testfile(size_t length)
287 {
288         int rc;
289         int fd;
290
291         rc = snprintf(testfile, sizeof(testfile), "%s/hsm_check_test",
292                       lustre_dir);
293         ASSERTF((rc > 0 && rc < sizeof(testfile)), "invalid name for testfile");
294
295         /* Remove old test file, if any. */
296         unlink(testfile);
297
298         /* Use truncate so we can create a file (almost) as big as we
299          * want, while taking 0 bytes of data. */
300         fd = creat(testfile, S_IRWXU);
301         ASSERTF(fd >= 0, "create failed for '%s': %s",
302                 testfile, strerror(errno));
303
304         rc = ftruncate(fd, length);
305         ASSERTF(rc == 0, "ftruncate failed for '%s': %s",
306                 testfile, strerror(errno));
307
308         return fd;
309 }
310
311 /* Test llapi_hsm_state_get. */
312 void test50(void)
313 {
314         struct hsm_user_state hus;
315         int rc;
316         int fd;
317
318         fd = create_testfile(100);
319
320         /* With fd variant */
321         rc = llapi_hsm_state_get_fd(fd, &hus);
322         ASSERTF(rc == 0, "llapi_hsm_state_get_fd failed: %s", strerror(-rc));
323         ASSERTF(hus.hus_states == 0, "state=%u", hus.hus_states);
324
325         rc = llapi_hsm_state_get_fd(fd, NULL);
326         ASSERTF(rc == -EFAULT, "llapi_hsm_state_get_fd error: %s",
327                 strerror(-rc));
328
329         rc = close(fd);
330         ASSERTF(rc == 0, "close failed: %s", strerror(errno));
331
332         /* Without fd */
333         rc = llapi_hsm_state_get(testfile, &hus);
334         ASSERTF(rc == 0, "llapi_hsm_state_get failed: %s", strerror(-rc));
335         ASSERTF(hus.hus_states == 0, "state=%u", hus.hus_states);
336
337         rc = llapi_hsm_state_get(testfile, NULL);
338         ASSERTF(rc == -EFAULT, "llapi_hsm_state_get error: %s",
339                 strerror(-rc));
340
341         memset(&hus, 0xaa, sizeof(hus));
342         rc = llapi_hsm_state_get(testfile, &hus);
343         ASSERTF(rc == 0, "llapi_hsm_state_get failed: %s", strerror(-rc));
344         ASSERTF(hus.hus_states == 0, "state=%u", hus.hus_states);
345         ASSERTF(hus.hus_archive_id == 0, "archive_id=%u", hus.hus_archive_id);
346         ASSERTF(hus.hus_in_progress_state == 0, "hus_in_progress_state=%u",
347                 hus.hus_in_progress_state);
348         ASSERTF(hus.hus_in_progress_action == 0, "hus_in_progress_action=%u",
349                 hus.hus_in_progress_action);
350 }
351
352 /* Test llapi_hsm_state_set. */
353 void test51(void)
354 {
355         int rc;
356         int fd;
357         int i;
358         struct hsm_user_state hus;
359
360         fd = create_testfile(100);
361
362         rc = llapi_hsm_state_set_fd(fd, 0, 0, 0);
363         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
364
365         /* Set archive id */
366         for (i = 0; i <= 32; i++) {
367                 rc = llapi_hsm_state_set_fd(fd, HS_EXISTS, 0, i);
368                 ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s",
369                         strerror(-rc));
370
371                 rc = llapi_hsm_state_get_fd(fd, &hus);
372                 ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s",
373                         strerror(-rc));
374                 ASSERTF(hus.hus_states == HS_EXISTS, "state=%u",
375                         hus.hus_states);
376                 ASSERTF(hus.hus_archive_id == i, "archive_id=%u, i=%d",
377                         hus.hus_archive_id, i);
378         }
379
380         /* Invalid archive numbers */
381         rc = llapi_hsm_state_set_fd(fd, HS_EXISTS, 0, 33);
382         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd: %s", strerror(-rc));
383
384         rc = llapi_hsm_state_set_fd(fd, HS_EXISTS, 0, 151);
385         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd: %s", strerror(-rc));
386
387         rc = llapi_hsm_state_set_fd(fd, HS_EXISTS, 0, -1789);
388         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd: %s", strerror(-rc));
389
390         /* Settable flags, with respect of the HSM file state transition rules:
391          *      DIRTY without EXISTS: no dirty if no archive was created
392          *      DIRTY and RELEASED: a dirty file could not be released
393          *      RELEASED without ARCHIVED: do not release a non-archived file
394          *      LOST without ARCHIVED: cannot lost a non-archived file.
395          */
396         rc = llapi_hsm_state_set_fd(fd, HS_DIRTY, 0, 0);
397         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
398
399         rc = llapi_hsm_state_set_fd(fd, 0, HS_EXISTS, 0);
400         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd failed: %s",
401                 strerror(-rc));
402
403         rc = llapi_hsm_state_set_fd(fd, 0, HS_DIRTY, 0);
404         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
405
406         rc = llapi_hsm_state_set_fd(fd, 0, HS_EXISTS, 0);
407         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
408
409         rc = llapi_hsm_state_set_fd(fd, HS_DIRTY, 0, 0);
410         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd failed: %s",
411                 strerror(-rc));
412
413         rc = llapi_hsm_state_set_fd(fd, HS_RELEASED, 0, 0);
414         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd failed: %s",
415                 strerror(-rc));
416
417         rc = llapi_hsm_state_set_fd(fd, HS_LOST, 0, 0);
418         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd failed: %s",
419                 strerror(-rc));
420
421         rc = llapi_hsm_state_set_fd(fd, HS_ARCHIVED, 0, 0);
422         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
423
424         rc = llapi_hsm_state_set_fd(fd, HS_RELEASED, 0, 0);
425         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
426
427         rc = llapi_hsm_state_set_fd(fd, HS_LOST, 0, 0);
428         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
429
430         rc = llapi_hsm_state_set_fd(fd, HS_DIRTY|HS_EXISTS, 0, 0);
431         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd failed: %s",
432                 strerror(-rc));
433
434         rc = llapi_hsm_state_set_fd(fd, 0, HS_RELEASED, 0);
435         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
436
437         rc = llapi_hsm_state_set_fd(fd, HS_DIRTY|HS_EXISTS, 0, 0);
438         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
439
440         rc = llapi_hsm_state_set_fd(fd, 0, HS_ARCHIVED, 0);
441         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd failed: %s",
442                 strerror(-rc));
443
444         rc = llapi_hsm_state_set_fd(fd, 0, HS_LOST, 0);
445         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
446
447         rc = llapi_hsm_state_set_fd(fd, 0, HS_ARCHIVED, 0);
448         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
449
450         rc = llapi_hsm_state_set_fd(fd, HS_NORELEASE, 0, 0);
451         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
452
453         rc = llapi_hsm_state_set_fd(fd, 0, HS_NORELEASE, 0);
454         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
455
456         rc = llapi_hsm_state_set_fd(fd, HS_NOARCHIVE, 0, 0);
457         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
458
459         rc = llapi_hsm_state_set_fd(fd, 0, HS_NOARCHIVE, 0);
460         ASSERTF(rc == 0, "llapi_hsm_state_set_fd failed: %s", strerror(-rc));
461
462         /* Bogus flags for good measure. */
463         rc = llapi_hsm_state_set_fd(fd, 0x00080000, 0, 0);
464         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd: %s", strerror(-rc));
465
466         rc = llapi_hsm_state_set_fd(fd, 0x80000000, 0, 0);
467         ASSERTF(rc == -EINVAL, "llapi_hsm_state_set_fd: %s", strerror(-rc));
468
469         close(fd);
470 }
471
472 /* Test llapi_hsm_current_action */
473 void test52(void)
474 {
475         int rc;
476         int fd;
477         struct hsm_current_action hca;
478
479         /* No fd equivalent, so close it. */
480         fd = create_testfile(100);
481         close(fd);
482
483         rc = llapi_hsm_current_action(testfile, &hca);
484         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s", strerror(-rc));
485         ASSERTF(hca.hca_state, "hca_state=%u", hca.hca_state);
486         ASSERTF(hca.hca_action, "hca_state=%u", hca.hca_action);
487
488         rc = llapi_hsm_current_action(testfile, NULL);
489         ASSERTF(rc == -EFAULT, "llapi_hsm_current_action failed: %s",
490                 strerror(-rc));
491 }
492
493 /* Helper to simulate archiving a file. No actual data movement
494  * happens. */
495 void helper_archiving(void (*progress)
496                       (struct hsm_copyaction_private *hcp, size_t length),
497                       const size_t length)
498 {
499         int rc;
500         int fd;
501         struct hsm_copytool_private *ctdata;
502         struct hsm_user_request *hur;
503         struct hsm_action_list  *hal;
504         struct hsm_action_item  *hai;
505         int                      msgsize;
506         struct hsm_copyaction_private *hcp;
507         struct hsm_user_state hus;
508
509         fd = create_testfile(length);
510
511         rc = llapi_hsm_copytool_register(&ctdata, fsmountdir,
512                                          0, NULL, 0);
513         ASSERTF(rc == 0, "llapi_hsm_copytool_register failed: %s",
514                 strerror(-rc));
515
516         /* Create and send the archive request. */
517         hur = llapi_hsm_user_request_alloc(1, 0);
518         ASSERTF(hur != NULL, "llapi_hsm_user_request_alloc returned NULL");
519
520         hur->hur_request.hr_action = HUA_ARCHIVE;
521         hur->hur_request.hr_archive_id = 1;
522         hur->hur_request.hr_flags = 0;
523         hur->hur_request.hr_itemcount = 1;
524         hur->hur_request.hr_data_len = 0;
525         hur->hur_user_item[0].hui_extent.length = -1;
526
527         rc = llapi_fd2fid(fd, &hur->hur_user_item[0].hui_fid);
528         ASSERTF(rc == 0, "llapi_fd2fid failed: %s", strerror(-rc));
529
530         close(fd);
531
532         rc = llapi_hsm_request(testfile, hur);
533         ASSERTF(rc == 0, "llapi_hsm_request failed: %s", strerror(-rc));
534
535         free(hur);
536
537         /* Read the request */
538         rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
539         ASSERTF(rc == 0, "llapi_hsm_copytool_recv failed: %s", strerror(-rc));
540         ASSERTF(hal->hal_count == 1, "hal_count=%d", hal->hal_count);
541
542         hai = hai_first(hal);
543         ASSERTF(hai != NULL, "hai_first returned NULL");
544         ASSERTF(hai->hai_action == HSMA_ARCHIVE,
545                 "hai_action=%d", hai->hai_action);
546
547         /* "Begin" archiving */
548         hcp = NULL;
549         rc = llapi_hsm_action_begin(&hcp, ctdata, hai, -1, 0, false);
550         ASSERTF(rc == 0, "llapi_hsm_action_begin failed: %s", strerror(-rc));
551         ASSERTF(hcp != NULL, "hcp is NULL");
552
553         if (progress)
554                 progress(hcp, length);
555
556         /* Done archiving */
557         rc = llapi_hsm_action_end(&hcp, &hai->hai_extent, 0, 0);
558         ASSERTF(rc == 0, "llapi_hsm_action_end failed: %s", strerror(-rc));
559         ASSERTF(hcp == NULL, "hcp is NULL");
560
561         /* Close HSM client */
562         rc = llapi_hsm_copytool_unregister(&ctdata);
563         ASSERTF(rc == 0, "llapi_hsm_copytool_unregister failed: %s",
564                 strerror(-rc));
565
566         /* Final check */
567         rc = llapi_hsm_state_get(testfile, &hus);
568         ASSERTF(rc == 0, "llapi_hsm_state_get failed: %s", strerror(-rc));
569         ASSERTF(hus.hus_states == (HS_EXISTS | HS_ARCHIVED),
570                 "state=%u", hus.hus_states);
571 }
572
573 /* Simple archive. No progress. */
574 void test100(void)
575 {
576         const size_t length = 100;
577
578         helper_archiving(NULL, length);
579 }
580
581 /* Archive, with a report every byte. */
582 static void test101_progress(struct hsm_copyaction_private *hcp, size_t length)
583 {
584         int i;
585         int rc;
586         struct hsm_extent he;
587         struct hsm_current_action hca;
588
589         /* Report progress. 1 byte at a time :) */
590         for (i = 0; i < length; i++) {
591                 he.offset = i;
592                 he.length = 1;
593                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
594                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
595                         strerror(-rc));
596         }
597
598         rc = llapi_hsm_current_action(testfile, &hca);
599         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
600                 strerror(-rc));
601         ASSERTF(hca.hca_state == HPS_RUNNING,
602                 "hca_state=%u", hca.hca_state);
603         ASSERTF(hca.hca_action == HUA_ARCHIVE,
604                 "hca_state=%u", hca.hca_action);
605         ASSERTF(hca.hca_location.length == length,
606                 "length=%llu", (unsigned long long)hca.hca_location.length);
607 }
608
609 void test101(void)
610 {
611         const size_t length = 1000;
612
613         helper_archiving(test101_progress, length);
614 }
615
616 /* Archive, with a report every byte, backwards. */
617 static void test102_progress(struct hsm_copyaction_private *hcp, size_t length)
618 {
619         int i;
620         int rc;
621         struct hsm_extent he;
622         struct hsm_current_action hca;
623
624         /* Report progress. 1 byte at a time :) */
625         for (i = length-1; i >= 0; i--) {
626                 he.offset = i;
627                 he.length = 1;
628                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
629                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
630                         strerror(-rc));
631         }
632
633         rc = llapi_hsm_current_action(testfile, &hca);
634         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
635                 strerror(-rc));
636         ASSERTF(hca.hca_state == HPS_RUNNING,
637                 "hca_state=%u", hca.hca_state);
638         ASSERTF(hca.hca_action == HUA_ARCHIVE,
639                 "hca_state=%u", hca.hca_action);
640         ASSERTF(hca.hca_location.length == length,
641                 "length=%llu", (unsigned long long)hca.hca_location.length);
642 }
643
644 void test102(void)
645 {
646         const size_t length = 1000;
647
648         helper_archiving(test102_progress, length);
649 }
650
651 /* Archive, with a single report. */
652 static void test103_progress(struct hsm_copyaction_private *hcp, size_t length)
653 {
654         int rc;
655         struct hsm_extent he;
656         struct hsm_current_action hca;
657
658         he.offset = 0;
659         he.length = length;
660         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
661         ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
662                 strerror(-rc));
663
664         rc = llapi_hsm_current_action(testfile, &hca);
665         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
666                 strerror(-rc));
667         ASSERTF(hca.hca_state == HPS_RUNNING,
668                 "hca_state=%u", hca.hca_state);
669         ASSERTF(hca.hca_action == HUA_ARCHIVE,
670                 "hca_state=%u", hca.hca_action);
671         ASSERTF(hca.hca_location.length == length,
672                 "length=%llu", (unsigned long long)hca.hca_location.length);
673 }
674
675 void test103(void)
676 {
677         const size_t length = 1000;
678
679         helper_archiving(test103_progress, length);
680 }
681
682 /* Archive, with 2 reports. */
683 static void test104_progress(struct hsm_copyaction_private *hcp, size_t length)
684 {
685         int rc;
686         struct hsm_extent he;
687         struct hsm_current_action hca;
688
689         he.offset = 0;
690         he.length = length/2;
691         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
692         ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
693                 strerror(-rc));
694
695         he.offset = length/2;
696         he.length = length/2;
697         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
698         ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
699                 strerror(-rc));
700
701         rc = llapi_hsm_current_action(testfile, &hca);
702         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
703                 strerror(-rc));
704         ASSERTF(hca.hca_state == HPS_RUNNING,
705                 "hca_state=%u", hca.hca_state);
706         ASSERTF(hca.hca_action == HUA_ARCHIVE,
707                 "hca_state=%u", hca.hca_action);
708         ASSERTF(hca.hca_location.length == length,
709                 "length=%llu", (unsigned long long)hca.hca_location.length);
710 }
711
712 void test104(void)
713 {
714         const size_t length = 1000;
715
716         helper_archiving(test104_progress, length);
717 }
718
719 /* Archive, with 1 bogus report. */
720 static void test105_progress(struct hsm_copyaction_private *hcp, size_t length)
721 {
722         int rc;
723         struct hsm_extent he;
724         struct hsm_current_action hca;
725
726         he.offset = 2*length;
727         he.length = 10*length;
728         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
729         ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
730                 strerror(-rc));
731
732         rc = llapi_hsm_current_action(testfile, &hca);
733         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
734                 strerror(-rc));
735         ASSERTF(hca.hca_state == HPS_RUNNING,
736                 "hca_state=%u", hca.hca_state);
737         ASSERTF(hca.hca_action == HUA_ARCHIVE,
738                 "hca_state=%u", hca.hca_action);
739
740         /* BUG - offset should be 2*length, or length should
741          * be 8*length */
742         ASSERTF(hca.hca_location.length == 10*length,
743                 "length=%llu", (unsigned long long)hca.hca_location.length);
744 }
745
746 void test105(void)
747 {
748         const size_t length = 1000;
749
750         helper_archiving(test105_progress, length);
751 }
752
753 /* Archive, with 1 empty report. */
754 static void test106_progress(struct hsm_copyaction_private *hcp, size_t length)
755 {
756         int rc;
757         struct hsm_extent he;
758         struct hsm_current_action hca;
759
760         he.offset = 0;
761         he.length = 0;
762         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
763         ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
764                 strerror(-rc));
765
766         rc = llapi_hsm_current_action(testfile, &hca);
767         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
768                 strerror(-rc));
769         ASSERTF(hca.hca_state == HPS_RUNNING,
770                 "hca_state=%u", hca.hca_state);
771         ASSERTF(hca.hca_action == HUA_ARCHIVE,
772                 "hca_state=%u", hca.hca_action);
773         ASSERTF(hca.hca_location.length == 0,
774                 "length=%llu", (unsigned long long)hca.hca_location.length);
775 }
776
777 void test106(void)
778 {
779         const size_t length = 1000;
780
781         helper_archiving(test106_progress, length);
782 }
783
784 /* Archive, with 1 bogus report. */
785 static void test107_progress(struct hsm_copyaction_private *hcp, size_t length)
786 {
787         int rc;
788         struct hsm_extent he;
789         struct hsm_current_action hca;
790
791         he.offset = -1;
792         he.length = 10;
793         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
794         ASSERTF(rc == -EINVAL, "llapi_hsm_action_progress error: %s",
795                 strerror(-rc));
796
797         rc = llapi_hsm_current_action(testfile, &hca);
798         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
799                 strerror(-rc));
800         ASSERTF(hca.hca_state == HPS_RUNNING,
801                 "hca_state=%u", hca.hca_state);
802         ASSERTF(hca.hca_action == HUA_ARCHIVE,
803                 "hca_state=%u", hca.hca_action);
804         ASSERTF(hca.hca_location.length == 0,
805                 "length=%llu", (unsigned long long)hca.hca_location.length);
806 }
807
808 void test107(void)
809 {
810         const size_t length = 1000;
811
812         helper_archiving(test107_progress, length);
813 }
814
815 /* Archive, with same report, many times. */
816 static void test108_progress(struct hsm_copyaction_private *hcp, size_t length)
817 {
818         int rc;
819         struct hsm_extent he;
820         int i;
821         struct hsm_current_action hca;
822
823         for (i = 0; i < 1000; i++) {
824                 he.offset = 0;
825                 he.length = length;
826                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
827                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
828                         strerror(-rc));
829         }
830
831         rc = llapi_hsm_current_action(testfile, &hca);
832         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
833                 strerror(-rc));
834         ASSERTF(hca.hca_state == HPS_RUNNING,
835                 "hca_state=%u", hca.hca_state);
836         ASSERTF(hca.hca_action == HUA_ARCHIVE,
837                 "hca_state=%u", hca.hca_action);
838         ASSERTF(hca.hca_location.length == length,
839                 "length=%llu", (unsigned long long)hca.hca_location.length);
840 }
841
842 void test108(void)
843 {
844         const size_t length = 1000;
845
846         helper_archiving(test108_progress, length);
847 }
848
849 /* Archive, 1 report, with large number. */
850 static void test109_progress(struct hsm_copyaction_private *hcp, size_t length)
851 {
852         int rc;
853         struct hsm_extent he;
854         struct hsm_current_action hca;
855
856         he.offset = 0;
857         he.length = 0xffffffffffffffffULL;
858         rc = llapi_hsm_action_progress(hcp, &he, length, 0);
859         ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
860                 strerror(-rc));
861
862         rc = llapi_hsm_current_action(testfile, &hca);
863         ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
864                 strerror(-rc));
865         ASSERTF(hca.hca_state == HPS_RUNNING,
866                 "hca_state=%u", hca.hca_state);
867         ASSERTF(hca.hca_action == HUA_ARCHIVE,
868                 "hca_state=%u", hca.hca_action);
869         ASSERTF(hca.hca_location.length == 0xffffffffffffffffULL,
870                 "length=%llu", (unsigned long long)hca.hca_location.length);
871 }
872
873 void test109(void)
874 {
875         const size_t length = 1000;
876
877         helper_archiving(test109_progress, length);
878 }
879
880 /* Archive, with 10 reports, checking progress. */
881 static void test110_progress(struct hsm_copyaction_private *hcp, size_t length)
882 {
883         int rc;
884         int i;
885         struct hsm_extent he;
886         struct hsm_current_action hca;
887
888         for (i = 0; i < 10; i++) {
889                 he.offset = i*length/10;
890                 he.length = length/10;
891                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
892                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
893                         strerror(-rc));
894
895                 rc = llapi_hsm_current_action(testfile, &hca);
896                 ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
897                         strerror(-rc));
898                 ASSERTF(hca.hca_state == HPS_RUNNING,
899                         "hca_state=%u", hca.hca_state);
900                 ASSERTF(hca.hca_action == HUA_ARCHIVE,
901                         "hca_state=%u", hca.hca_action);
902                 ASSERTF(hca.hca_location.length == (i+1)*length/10,
903                         "i=%d, length=%llu",
904                         i, (unsigned long long)hca.hca_location.length);
905         }
906 }
907
908 void test110(void)
909 {
910         const size_t length = 1000;
911
912         helper_archiving(test110_progress, length);
913 }
914
915 /* Archive, with 10 reports in reverse order, checking progress. */
916 static void test111_progress(struct hsm_copyaction_private *hcp, size_t length)
917 {
918         int rc;
919         int i;
920         struct hsm_extent he;
921         struct hsm_current_action hca;
922
923         for (i = 0; i < 10; i++) {
924                 he.offset = (9-i)*length/10;
925                 he.length = length/10;
926                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
927                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
928                         strerror(-rc));
929
930                 rc = llapi_hsm_current_action(testfile, &hca);
931                 ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
932                         strerror(-rc));
933                 ASSERTF(hca.hca_state == HPS_RUNNING,
934                         "hca_state=%u", hca.hca_state);
935                 ASSERTF(hca.hca_action == HUA_ARCHIVE,
936                         "hca_state=%u", hca.hca_action);
937                 ASSERTF(hca.hca_location.length == (i+1)*length/10,
938                         "i=%d, length=%llu",
939                         i, (unsigned long long)hca.hca_location.length);
940         }
941 }
942
943 void test111(void)
944 {
945         const size_t length = 1000;
946
947         helper_archiving(test111_progress, length);
948 }
949
950 /* Archive, with 10 reports, and duplicating them, checking
951  * progress. */
952 static void test112_progress(struct hsm_copyaction_private *hcp, size_t length)
953 {
954         int rc;
955         int i;
956         struct hsm_extent he;
957         struct hsm_current_action hca;
958
959         for (i = 0; i < 10; i++) {
960                 he.offset = i*length/10;
961                 he.length = length/10;
962                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
963                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
964                         strerror(-rc));
965
966                 rc = llapi_hsm_current_action(testfile, &hca);
967                 ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
968                         strerror(-rc));
969                 ASSERTF(hca.hca_state == HPS_RUNNING,
970                         "hca_state=%u", hca.hca_state);
971                 ASSERTF(hca.hca_action == HUA_ARCHIVE,
972                         "hca_state=%u", hca.hca_action);
973                 ASSERTF(hca.hca_location.length == (i+1)*length/10,
974                         "i=%d, length=%llu",
975                         i, (unsigned long long)hca.hca_location.length);
976         }
977
978         for (i = 0; i < 10; i++) {
979                 he.offset = i*length/10;
980                 he.length = length/10;
981                 rc = llapi_hsm_action_progress(hcp, &he, length, 0);
982                 ASSERTF(rc == 0, "llapi_hsm_action_progress failed: %s",
983                         strerror(-rc));
984
985                 rc = llapi_hsm_current_action(testfile, &hca);
986                 ASSERTF(rc == 0, "llapi_hsm_current_action failed: %s",
987                         strerror(-rc));
988                 ASSERTF(hca.hca_state == HPS_RUNNING,
989                         "hca_state=%u", hca.hca_state);
990                 ASSERTF(hca.hca_action == HUA_ARCHIVE,
991                         "hca_state=%u", hca.hca_action);
992                 ASSERTF(hca.hca_location.length == length,
993                         "i=%d, length=%llu",
994                         i, (unsigned long long)hca.hca_location.length);
995         }
996 }
997
998 void test112(void)
999 {
1000         const size_t length = 1000;
1001
1002         helper_archiving(test112_progress, length);
1003 }
1004
1005 static void usage(char *prog)
1006 {
1007         fprintf(stderr, "Usage: %s [-d lustre_dir]\n", prog);
1008         exit(EXIT_FAILURE);
1009 }
1010
1011 static void process_args(int argc, char *argv[])
1012 {
1013         int c;
1014
1015         while ((c = getopt(argc, argv, "d:")) != -1) {
1016                 switch (c) {
1017                 case 'd':
1018                         lustre_dir = optarg;
1019                         break;
1020                 case '?':
1021                 default:
1022                         fprintf(stderr, "Unknown option '%c'\n", optopt);
1023                         usage(argv[0]);
1024                         break;
1025                 }
1026         }
1027 }
1028
1029 int main(int argc, char *argv[])
1030 {
1031         char fsname[8 + 1];
1032         int rc;
1033
1034         process_args(argc, argv);
1035         if (lustre_dir == NULL)
1036                 lustre_dir = "/mnt/lustre";
1037
1038         rc = llapi_search_mounts(lustre_dir, 0, fsmountdir, fsname);
1039         if (rc != 0) {
1040                 fprintf(stderr, "Error: %s: not a Lustre filesystem\n",
1041                         lustre_dir);
1042                 return EXIT_FAILURE;
1043         }
1044
1045         /* Play nice with Lustre test scripts. Non-line buffered output
1046          * stream under I/O redirection may appear incorrectly. */
1047         setvbuf(stdout, NULL, _IOLBF, 0);
1048
1049         PERFORM(test1);
1050         PERFORM(test2);
1051         PERFORM(test3);
1052         PERFORM(test4);
1053         PERFORM(test5);
1054         PERFORM(test6);
1055         PERFORM(test7);
1056         PERFORM(test50);
1057         PERFORM(test51);
1058         PERFORM(test52);
1059         PERFORM(test100);
1060         PERFORM(test101);
1061         PERFORM(test102);
1062         PERFORM(test103);
1063         PERFORM(test104);
1064         PERFORM(test105);
1065         PERFORM(test106);
1066         PERFORM(test107);
1067         PERFORM(test108);
1068         PERFORM(test109);
1069         PERFORM(test110);
1070         PERFORM(test111);
1071         PERFORM(test112);
1072
1073         return EXIT_SUCCESS;
1074 }