Whamcloud - gitweb
LU-2061 hsm: send HSM resquest to CDT
[fs/lustre-release.git] / lustre / tests / copytool.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * Author: Nathan Rutman <nathan.rutman@sun.com>
35  *
36  */
37
38 /* HSM copytool example program.
39  * The copytool acts on action requests from Lustre to copy files to and from
40  * an HSM archive system.
41  *
42  * Note: under Linux, until llapi_hsm_copytool_fini is called (or the program is
43  * killed), the libcfs module will be referenced and unremovable,
44  * even after Lustre services stop.
45  */
46
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <getopt.h>
50 #include <signal.h>
51 #include <errno.h>
52 #include <sys/types.h>
53 #include <unistd.h>
54
55 #include <libcfs/libcfs.h>
56 #include <lustre/lustreapi.h>
57
58 void *ctdata;
59
60 void handler(int signal ) {
61         psignal(signal, "exiting");
62         /* If we don't clean up upon interrupt, umount thinks there's a ref
63          * and doesn't remove us from mtab (EINPROGRESS). The lustre client
64          * does successfully unmount and the mount is actually gone, but the
65          * mtab entry remains. So this just makes mtab happier. */
66         llapi_hsm_copytool_fini(&ctdata);
67         exit(1);
68 }
69
70 int main(int argc, char **argv) {
71         int c, test = 0;
72         struct option long_opts[] = {
73                 {"test", no_argument, 0, 't'},
74                 {0, 0, 0, 0}
75         };
76         int archives[] = {1}; /* which archives we care about */
77         int rc;
78
79         optind = 0;
80         while ((c = getopt_long(argc, argv, "t", long_opts, NULL)) != -1) {
81                 switch (c) {
82                 case 't':
83                         test++;
84                         break;
85                 default:
86                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
87                                 argv[0], argv[optind - 1]);
88                         return EINVAL;
89                 }
90         }
91
92         if (optind != argc - 1) {
93                 fprintf(stderr, "Usage: %s <fsname>\n", argv[0]);
94                 return -EINVAL;
95         }
96
97         rc = llapi_hsm_copytool_start(&ctdata, argv[optind], 0,
98                                   ARRAY_SIZE(archives), archives);
99         if (rc < 0) {
100                 fprintf(stderr, "Can't start copytool interface: %s\n",
101                         strerror(-rc));
102                 return -rc;
103         }
104
105         if (test)
106                 return -llapi_hsm_copytool_fini(&ctdata);
107
108         printf("Waiting for message from kernel (pid=%d)\n", getpid());
109
110         signal(SIGINT, handler);
111
112         while(1) {
113                 struct hsm_action_list *hal;
114                 struct hsm_action_item *hai;
115                 int msgsize, i = 0;
116
117                 rc = llapi_hsm_copytool_recv(ctdata, &hal, &msgsize);
118                 if (rc == -ESHUTDOWN) {
119                         fprintf(stderr, "shutting down");
120                         break;
121                 }
122                 if (rc < 0) {
123                         fprintf(stderr, "Message receive: %s", strerror(-rc));
124                         break;
125                 }
126                 if (msgsize == 0)
127                         continue; /* msg not for us */
128
129                 printf("Copytool fs=%s archive#=%d item_count=%d\n",
130                         hal->hal_fsname, hal->hal_archive_id, hal->hal_count);
131
132                 hai = hai_zero(hal);
133                 while (++i <= hal->hal_count) {
134                         printf("Item %d: action %d reclen %d\n", i,
135                                 hai->hai_action, hai->hai_len);
136                         printf(" "DFID" gid="LPU64" cookie="LPU64"\n",
137                                 PFID(&hai->hai_fid), hai->hai_gid,
138                                 hai->hai_cookie);
139                         hai = hai_next(hai);
140                 }
141
142                 llapi_hsm_copytool_free(&hal);
143         }
144
145         llapi_hsm_copytool_fini(&ctdata);
146
147         return -rc;
148 }
149
150
151