Whamcloud - gitweb
LU-3789 mgs: Add deprecation warning for "lctl conf_param"
[fs/lustre-release.git] / lustre / utils / lr_reader.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) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2013, Intel Corporation.
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 #if HAVE_CONFIG_H
43 #  include "config.h"
44 #endif /* HAVE_CONFIG_H */
45
46 #ifndef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #endif
49 #include <stdlib.h>
50 #include <stdio.h>
51 #include <unistd.h>
52 #include <fcntl.h>
53 #include <stdarg.h>
54 #include <mntent.h>
55
56 #include <sys/types.h>
57 #include <sys/stat.h>
58 #include <sys/mount.h>
59
60 #include <string.h>
61 #include <getopt.h>
62
63 #include <lustre_disk.h>
64 #include <lustre_ver.h>
65
66 int run_command(char *cmd)
67 {
68         char log[] = "/tmp/mkfs_logXXXXXX";
69         int fd, rc;
70         
71         
72         if ((fd = mkstemp(log)) >= 0) {
73                 close(fd);
74                 strcat(cmd, " >");
75                 strcat(cmd, log);
76         }
77         strcat(cmd, " 2>&1");
78
79         /* Can't use popen because we need the rv of the command */
80         rc = system(cmd);
81         if (rc && fd >= 0) {
82                 char buf[128];
83                 FILE *fp;
84                 fp = fopen(log, "r");
85                 if (fp) {
86                         while (fgets(buf, sizeof(buf), fp) != NULL) {
87                                 if (rc) 
88                                         printf("   %s", buf);
89                         }
90                         fclose(fp);
91                 }
92         }
93         if (fd >= 0) 
94                 remove(log);
95         return rc;
96 }                                                       
97
98
99
100 int main(int argc, char *const argv[])
101 {
102         char tmpdir[] = "/tmp/dirXXXXXX";
103         char cmd[128];
104         char filepnm[128];
105         char *progname, *dev;
106         struct lr_server_data lsd;
107         FILE *filep;
108         int ret;
109
110         if ((argc < 2) || (argv[argc - 1][0] == '-')) {
111                 printf("Usage: %s devicename\n", argv[0]);
112                 printf("Read and print the last_rcvd file from a device\n");
113                 printf("(safe for mounted devices)\n");
114                 return EINVAL;
115         }
116
117         progname = argv[0];
118         dev = argv[argc - 1];
119
120         /* Make a temporary directory to hold Lustre data files. */
121         if (!mkdtemp(tmpdir)) {
122                 fprintf(stderr, "%s: Can't create temporary directory %s: %s\n",
123                         progname, tmpdir, strerror(errno));
124                 return errno;
125         }
126
127         memset(cmd, 0, sizeof(cmd));
128         snprintf(cmd, sizeof(cmd),
129                 "%s -c -R 'dump /%s %s/%s' %s",
130                 DEBUGFS, LAST_RCVD, tmpdir, LAST_RCVD, dev);
131
132         ret = run_command(cmd);
133         if (ret) {
134                 fprintf(stderr, "%s: Unable to dump %s file\n",
135                         progname, LAST_RCVD);
136                 goto out_rmdir;
137         }
138
139         sprintf(filepnm, "%s/%s", tmpdir, LAST_RCVD);
140         filep = fopen(filepnm, "r");
141         if (!filep) {
142                 fprintf(stderr, "%s: Unable to read old data\n",
143                         progname);
144                 ret = -errno;
145                 goto out_rmdir;
146         }
147
148         printf("Reading %s\n", LAST_RCVD);
149         ret = fread(&lsd, 1, sizeof(lsd), filep);
150         if (ret < sizeof(lsd)) {
151                 fprintf(stderr, "%s: Short read (%d of %d)\n",
152                         progname, ret, (int)sizeof(lsd));
153                 ret = -ferror(filep);
154                 if (ret) 
155                         goto out_close;
156         }
157
158 #if 0
159         __u8  lsd_uuid[40];        /* server UUID */
160         __u64 lsd_last_transno;    /* last completed transaction ID */
161         __u64 lsd_compat14;        /* reserved - compat with old last_rcvd */
162         __u64 lsd_mount_count;     /* incarnation number */
163         __u32 lsd_feature_compat;  /* compatible feature flags */
164         __u32 lsd_feature_rocompat;/* read-only compatible feature flags */
165         __u32 lsd_feature_incompat;/* incompatible feature flags */
166         __u32 lsd_server_size;     /* size of server data area */
167         __u32 lsd_client_start;    /* start of per-client data area */
168         __u16 lsd_client_size;     /* size of per-client data area */
169         __u16 lsd_subdir_count;    /* number of subdirectories for objects */
170         __u64 lsd_catalog_oid;     /* recovery catalog object id */
171         __u32 lsd_catalog_ogen;    /* recovery catalog inode generation */
172         __u8  lsd_peeruuid[40];    /* UUID of MDS associated with this OST */
173         __u32 lsd_osd_index;       /* index number of OST/MDT in LOV/LMV */
174         __u8  lsd_padding[LR_SERVER_SIZE - 148];
175 #endif
176
177         printf("UUID %s\n", lsd.lsd_uuid);
178         printf("Feature compat=%#x\n", lsd.lsd_feature_compat);
179         printf("Feature incompat=%#x\n", lsd.lsd_feature_incompat);
180         printf("Feature rocompat=%#x\n", lsd.lsd_feature_rocompat);
181         printf("Last transaction %llu\n", (long long)lsd.lsd_last_transno);
182         printf("target index %u\n", lsd.lsd_osd_index);
183
184         if ((lsd.lsd_feature_compat & OBD_COMPAT_OST) ||
185             (lsd.lsd_feature_incompat & OBD_INCOMPAT_OST)) {
186                 printf("OST, index %d\n", lsd.lsd_osd_index);
187         } else if ((lsd.lsd_feature_compat & OBD_COMPAT_MDT) ||
188                    (lsd.lsd_feature_incompat & OBD_INCOMPAT_MDT)) {
189                 /* We must co-locate so mgs can see old logs.
190                    If user doesn't want this, they can copy the old
191                    logs manually and re-tunefs. */
192                 printf("MDS, index %d\n", lsd.lsd_osd_index);
193         } else  {
194                 /* If neither is set, we're pre-1.4.6, make a guess. */
195                 /* Construct debugfs command line. */
196                 memset(cmd, 0, sizeof(cmd));
197                 snprintf(cmd, sizeof(cmd), "%s -c -R 'rdump /%s %s' %s",
198                          DEBUGFS, MDT_LOGS_DIR, tmpdir, dev);
199
200                 run_command(cmd);
201
202                 sprintf(filepnm, "%s/%s", tmpdir, MDT_LOGS_DIR);
203                 if (lsd.lsd_osd_index > 0) {
204                         printf("non-flagged OST, index %d\n",
205                                lsd.lsd_osd_index);
206                 } else {
207                         /* If there's a LOGS dir, it's an MDT */
208                         if ((ret = access(filepnm, F_OK)) == 0) {
209                                 /* Old MDT's are always index 0
210                                    (pre CMD) */
211                                 printf("non-flagged MDS, index 0\n");
212                         } else {
213                                 printf("non-flagged OST, index unknown\n");
214                         }
215                 }
216         }
217
218 out_close:        
219         fclose(filep);
220
221 out_rmdir:
222         memset(cmd, 0, sizeof(cmd));
223         sprintf(cmd, "rm -rf %s", tmpdir);
224         run_command(cmd);
225         return ret;
226 }