Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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_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 <getopt.h>
49 #include <signal.h>
50 #include <libcfs/libcfs.h>
51 #include <lustre/lustre_user.h>
52 #include <lustre/liblustreapi.h>
53
54 void *ctdata;
55
56 void handler(int signal ) {
57         psignal(signal, "exiting");
58         /* If we don't clean up upon interrupt, umount thinks there's a ref
59          * and doesn't remove us from mtab (EINPROGRESS). The lustre client
60          * does successfully unmount and the mount is actually gone, but the
61          * mtab entry remains. So this just makes mtab happier. */
62         llapi_copytool_fini(&ctdata);
63         exit(1);
64 }
65
66 int main(int argc, char **argv) {
67         int c, test = 0;
68         struct option long_opts[] = {
69                 {"test", no_argument, 0, 't'},
70                 {0, 0, 0, 0}
71         };
72         int archives[] = {1}; /* which archives we care about */
73         int rc;
74
75         optind = 0;
76         while ((c = getopt_long(argc, argv, "t", long_opts, NULL)) != -1) {
77                 switch (c) {
78                 case 't':
79                         test++;
80                         break;
81                 default:
82                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
83                                 argv[0], argv[optind - 1]);
84                         return EINVAL;
85                 }
86         }
87
88         if (optind != argc - 1) {
89                 fprintf(stderr, "Usage: %s <fsname>\n", argv[0]);
90                 return -EINVAL;
91         }
92
93         rc = llapi_copytool_start(&ctdata, argv[optind], 0,
94                                   ARRAY_SIZE(archives), archives);
95         if (rc < 0) {
96                 fprintf(stderr, "Can't start copytool interface: %s\n",
97                         strerror(-rc));
98                 return -rc;
99         }
100
101         if (test)
102                 return -llapi_copytool_fini(&ctdata);
103
104         printf("Waiting for message from kernel (pid=%d)\n", getpid());
105
106         signal(SIGINT, handler);
107
108         while(1) {
109                 struct hsm_action_list *hal;
110                 struct hsm_action_item *hai;
111                 int msgsize, i = 0;
112
113                 rc = llapi_copytool_recv(ctdata, &hal, &msgsize);
114                 if (rc == -ESHUTDOWN) {
115                         fprintf(stderr, "shutting down");
116                         break;
117                 }
118                 if (rc < 0) {
119                         fprintf(stderr, "Message receive: %s", strerror(-rc));
120                         break;
121                 }
122                 if (msgsize == 0)
123                         continue; /* msg not for us */
124
125                 printf("Copytool fs=%s archive#=%d item_count=%d\n",
126                        hal->hal_fsname, hal->hal_archive_num, hal->hal_count);
127
128                 hai = hai_zero(hal);
129                 while (++i <= hal->hal_count) {
130                         printf("Item %d: action %d reclen %d\n", i,
131                                hai->hai_action, hai->hai_len);
132                         printf(" "DFID" gid="LPU64" cookie="LPU64"\n",
133                                PFID(&hai->hai_fid), hai->hai_gid,
134                                hai->hai_cookie);
135                         hai = hai_next(hai);
136                 }
137
138                 llapi_copytool_free(&hal);
139         }
140
141         llapi_copytool_fini(&ctdata);
142
143         return -rc;
144 }
145
146
147