Whamcloud - gitweb
b=16098
[fs/lustre-release.git] / lustre / utils / lr_reader.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see [sun.com URL with a
20  * copy of GPLv2].
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/utils/lr_reader.c
37  *
38  * Author: Nathan Rutman <nathan@clusterfs.com>
39  */
40  /* Safely read the last_rcvd file from a device */
41
42 #ifndef _GNU_SOURCE
43 #define _GNU_SOURCE
44 #endif
45 #include <stdlib.h>
46 #include <stdio.h>
47 #include <unistd.h>
48 #include <fcntl.h>
49 #include <stdarg.h>
50 #include <mntent.h>
51
52 #include <sys/types.h>
53 #include <sys/stat.h>
54 #include <sys/mount.h>
55
56 #include <string.h>
57 #include <getopt.h>
58
59 #include <lustre_disk.h>
60 #include <lustre_ver.h>
61
62 int run_command(char *cmd)
63 {
64         char log[] = "/tmp/mkfs_logXXXXXX";
65         int fd, rc;
66         
67         
68         if ((fd = mkstemp(log)) >= 0) {
69                 close(fd);
70                 strcat(cmd, " >");
71                 strcat(cmd, log);
72         }
73         strcat(cmd, " 2>&1");
74
75         /* Can't use popen because we need the rv of the command */
76         rc = system(cmd);
77         if (rc && fd >= 0) {
78                 char buf[128];
79                 FILE *fp;
80                 fp = fopen(log, "r");
81                 if (fp) {
82                         while (fgets(buf, sizeof(buf), fp) != NULL) {
83                                 if (rc) 
84                                         printf("   %s", buf);
85                         }
86                         fclose(fp);
87                 }
88         }
89         if (fd >= 0) 
90                 remove(log);
91         return rc;
92 }                                                       
93
94
95
96 int main(int argc, char *const argv[])
97 {
98         char tmpdir[] = "/tmp/dirXXXXXX";
99         char cmd[128];
100         char filepnm[128];
101         char *progname, *dev;
102         struct lr_server_data lsd;
103         FILE *filep;
104         int ret;
105
106         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
107                 printf("Usage: %s devicename\n", argv[0]);
108                 printf("Read and print the last_rcvd file from a device\n");
109                 printf("(safe for mounted devices)\n");
110                 return EINVAL;
111         }
112
113         progname = argv[0];
114         dev = argv[argc - 1];
115
116         /* Make a temporary directory to hold Lustre data files. */
117         if (!mkdtemp(tmpdir)) {
118                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
119                         progname, tmpdir, strerror(errno));
120                 return errno;
121         }
122
123         memset(cmd, 0, sizeof(cmd));
124         sprintf(cmd,
125                 "debugfs -c -R 'dump /%s %s/%s' %s",
126                 LAST_RCVD, tmpdir, LAST_RCVD, dev);
127
128         ret = run_command(cmd);
129         if (ret) {
130                 fprintf(stderr, "%s: Unable to dump %s file\n",
131                         progname, LAST_RCVD);
132                 goto out_rmdir;
133         }
134
135         sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
136         filep = fopen(filepnm, "r");
137         if (!filep) {
138                 fprintf(stderr, "%s: Unable to read old data\n",
139                         progname);
140                 ret = -errno;
141                 goto out_rmdir;
142         }
143
144         printf("Reading %s\n", LAST_RCVD);
145         ret = fread(&lsd, 1, sizeof(lsd), filep);
146         if (ret < sizeof(lsd)) {
147                 fprintf(stderr, "%s: Short read (%d of %d)\n",
148                         progname, ret, (int)sizeof(lsd));
149                 ret = -ferror(filep);
150                 if (ret) 
151                         goto out_close;
152         }
153
154         #if 0
155         __u8  lsd_uuid[40];        /* server UUID */
156         __u64 lsd_last_transno;    /* last completed transaction ID */
157         __u64 lsd_compat14;        /* reserved - compat with old last_rcvd */
158         __u64 lsd_mount_count;     /* incarnation number */
159         __u32 lsd_feature_compat;  /* compatible feature flags */
160         __u32 lsd_feature_rocompat;/* read-only compatible feature flags */
161         __u32 lsd_feature_incompat;/* incompatible feature flags */
162         __u32 lsd_server_size;     /* size of server data area */
163         __u32 lsd_client_start;    /* start of per-client data area */
164         __u16 lsd_client_size;     /* size of per-client data area */
165         __u16 lsd_subdir_count;    /* number of subdirectories for objects */
166         __u64 lsd_catalog_oid;     /* recovery catalog object id */
167         __u32 lsd_catalog_ogen;    /* recovery catalog inode generation */
168         __u8  lsd_peeruuid[40];    /* UUID of MDS associated with this OST */
169         __u32 lsd_ost_index;       /* index number of OST in LOV */
170         __u32 lsd_mdt_index;       /* index number of MDT in LMV */
171         __u8  lsd_padding[LR_SERVER_SIZE - 148];
172         #endif
173
174         printf("UUID %s\n", lsd.lsd_uuid);
175         printf("Feature compat=%#x\n", lsd.lsd_feature_compat);
176         printf("Feature incompat=%#x\n", lsd.lsd_feature_incompat);
177         printf("Feature rocompat=%#x\n", lsd.lsd_feature_rocompat);
178         printf("Last transaction %llu\n", (long long)lsd.lsd_last_transno);
179         printf("ost index %u\n", lsd.lsd_ost_index);
180         printf("mdt index %u\n", lsd.lsd_mdt_index);
181
182         if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
183             (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
184                 printf("OST, index %d\n", lsd.lsd_ost_index);
185         } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
186                    (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
187                 /* We must co-locate so mgs can see old logs.
188                    If user doesn't want this, they can copy the old
189                    logs manually and re-tunefs. */
190                 printf("MDS, index %d\n", lsd.lsd_mdt_index);
191         } else  {
192                 /* If neither is set, we're pre-1.4.6, make a guess. */
193                 /* Construct debugfs command line. */
194                 memset(cmd, 0, sizeof(cmd));
195                 sprintf(cmd,
196                         "debugfs -c -R 'rdump /%s %s' %s",
197                         MDT_LOGS_DIR, tmpdir, dev);
198
199                 run_command(cmd);
200
201                 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
202                 if (lsd.lsd_ost_index > 0) {
203                         printf("non-flagged OST, index %d\n", 
204                                lsd.lsd_ost_index);
205                 } else {
206                         /* If there's a LOGS dir, it's an MDT */
207                         if ((ret = access(filepnm, F_OK)) == 0) {
208                                 /* Old MDT's are always index 0 
209                                    (pre CMD) */
210                                 printf("non-flagged MDS, index 0\n");
211                         } else {
212                                 printf("non-flagged OST, index unknown\n");
213                         }
214                 }
215         }
216         
217 out_close:        
218         fclose(filep);
219
220 out_rmdir:
221         memset(cmd, 0, sizeof(cmd));
222         sprintf(cmd, "rm -rf %s", tmpdir);
223         run_command(cmd);
224         return ret;
225 }