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