Whamcloud - gitweb
b=22235 llapi_uuid_match() prints bogus error message on upgraded filesystem
[fs/lustre-release.git] / lustre / utils / ll_recover_lost_found_objs.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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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/ll_recover_lost_found_objs.c
37  *
38  * Tool for recovering objects from lost+found that might result from a
39  * Lustre OST with a corrupted directory. Running e2fsck will fix the
40  * directory, but puts all of the objects into lost+found, where they are
41  * inaccessible to Lustre.
42  *
43  * Author: Kalpak Shah <kalpak.shah@sun.com>
44  */
45
46 #ifndef _GNU_SOURCE
47 #define _GNU_SOURCE
48 #endif
49
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include <errno.h>
55 #include <dirent.h>
56 #include <sys/types.h>
57 #include <sys/xattr.h>
58 #include <sys/stat.h>
59
60 #include <liblustre.h>
61
62 #define MAX_GROUPS 64
63
64 int verbose = 0;
65
66 struct obd_group_info {
67         int dir_exists;
68 };
69 struct obd_group_info grp_info[MAX_GROUPS];
70
71 static void usage(char *progname)
72 {
73         fprintf(stderr, "Usage: %s [-hv] -d lost+found_directory\n", progname);
74         fprintf(stderr, "You need to mount the corrupted OST filesystem and "
75                 "provide the path for the lost+found directory as the -d "
76                 "option, for example:\n"
77                 "ll_recover_lost_found_objs -d /mnt/ost/lost+found\n");
78         exit(1);
79 }
80
81 static int _ll_sprintf(char *buf, size_t size, const char *func, int line,
82                       const char *format, ...)
83 {
84         int rc;
85         va_list ap;
86
87         va_start(ap, format);
88         rc = vsnprintf(buf, size, format, ap);
89         if (!(rc > -1 && rc < size)) {
90                 fprintf(stderr,
91                         "error: %s:%d: path \"", func, line);
92                 vfprintf(stderr, format, ap);
93                 va_end(ap);
94                 fprintf(stderr, "\" is too long\n");
95                 return 1;
96         }
97         va_end(ap);
98         return 0;
99 }
100
101 #define ll_sprintf(buf, size, format, ...) \
102         _ll_sprintf(buf, size, __FUNCTION__, __LINE__, format, ## __VA_ARGS__)
103
104 static int mkdir_p(const char *dest_path, const char *mount, __u64 ff_group)
105 {
106         struct stat stat_buf;
107         int retval;
108         mode_t mode = 0700;
109         char tmp_path[PATH_MAX];
110
111         if (stat(dest_path, &stat_buf) == 0)
112                 return 0;
113
114         if (grp_info[ff_group].dir_exists == 0) {
115                 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/"LPU64,
116                                mount, ff_group))
117                         return 1;
118
119                 if (stat(tmp_path, &stat_buf) != 0) {
120                         retval = mkdir(tmp_path, mode);
121                         if (retval < 0) {
122                                 fprintf(stderr, "error: creating directory %s: "
123                                         "%s\n", tmp_path, strerror(errno));
124                                 return 1;
125                         }
126                         grp_info[ff_group].dir_exists = 1;
127                 }
128         }
129
130         retval = mkdir(dest_path, mode);
131         if (retval < 0) {
132                 fprintf(stderr, "error: creating directory %s: "
133                         "%s\n", dest_path, strerror(errno));
134                 return 1;
135         }
136
137         return 0;
138 }
139
140 /* This is returning 0 for an error */
141 static __u64 read_last_id(char *file_path)
142 {
143         __u64 last_id;
144         int fd;
145         int count;
146
147         fd = open(file_path, O_RDONLY);
148         if (fd < 0) {
149                 if (errno != ENOENT)
150                         fprintf(stderr, "error: opening %s: %s\n",
151                                         file_path, strerror(errno));
152                 return 0;
153         }
154
155         count = read(fd, &last_id, sizeof(last_id));
156         if (count < 0) {
157                 fprintf(stderr, "error: reading file %s: %s\n", file_path,
158                         strerror(errno));
159                 close(fd);
160                 return 0;
161         }
162         if (count != sizeof(last_id)) {
163                 fprintf(stderr, "error: Could not read full last_id from %s\n",
164                         file_path);
165                 close(fd);
166                 return 0;
167         }
168
169         close(fd);
170         return le64_to_cpu(last_id);
171 }
172
173 static unsigned filetype_dir_table[] = {
174         [0]= DT_UNKNOWN,
175         [S_IFIFO]= DT_FIFO,
176         [S_IFCHR] = DT_CHR,
177         [S_IFDIR] = DT_DIR,
178         [S_IFBLK] = DT_BLK,
179         [S_IFREG] = DT_REG,
180         [S_IFLNK] = DT_LNK,
181         [S_IFSOCK]= DT_SOCK,
182 #if defined(DT_DOOR) && defined(S_IFDOOR)
183         [S_IFDOOR]= DT_DOOR,
184 #endif
185 };
186
187 static int traverse_lost_found(char *src_dir, const char *mount_path)
188 {
189         DIR *dir_ptr;
190         struct filter_fid trusted_fid;
191         struct dirent64 *dirent;
192         __u64 ff_group, ff_objid;
193         char *file_path;
194         char dest_path[PATH_MAX];
195         char last_id_file[PATH_MAX];
196         __u64 last_id[MAX_GROUPS] = {0};
197         __u64 tmp_last_id;
198         struct stat st;
199         int obj_exists, xattr_len;
200         int len, ret = 0, error = 0;
201
202         len = strlen(src_dir);
203
204         dir_ptr = opendir(src_dir);
205         if (!dir_ptr) {
206                 fprintf(stderr, "error: opening directory: %s\n",
207                         strerror(errno));
208                 return 1;
209         }
210
211         while ((dirent = readdir64(dir_ptr)) != NULL) {
212                 if (!strcmp(dirent->d_name, ".") ||
213                     !strcmp(dirent->d_name, ".."))
214                         continue;
215
216                 src_dir[len] = 0;
217                 if ((len + strlen(dirent->d_name) + 2) > PATH_MAX) {
218                         fprintf(stderr, "error: %s/%s: path too long\n",
219                                 src_dir, dirent->d_name);
220                         break;
221                 }
222                 strcat(src_dir, "/");
223                 strcat(src_dir, dirent->d_name);
224
225                 if (dirent->d_type == DT_UNKNOWN) {
226                         ret = stat(src_dir, &st);
227                         if (ret == -1) {
228                                 fprintf(stderr,
229                                         "error: stating %s: %s\n",
230                                         src_dir, strerror(errno));
231                                 continue;
232                         }
233                         dirent->d_type = filetype_dir_table[st.st_mode &
234                                                             S_IFMT];
235                         if (dirent->d_type == DT_UNKNOWN) {
236                                 fprintf(stderr,
237                                         "error: %s of unknown type 0%o\n",
238                                         src_dir, st.st_mode);
239                                 continue;
240                         }
241                 }
242
243                 switch(dirent->d_type) {
244                 case DT_DIR:
245                 ret = traverse_lost_found(src_dir, mount_path);
246                 if (ret) {
247                         closedir(dir_ptr);
248                         return ret;
249                 }
250                 break;
251
252                 case DT_REG:
253                 file_path = src_dir;
254                 xattr_len = getxattr(file_path, "trusted.fid",
255                                      (void *)&trusted_fid,
256                                      sizeof(trusted_fid));
257
258                 if (xattr_len == -1 || xattr_len < sizeof(trusted_fid))
259                         /*
260                          * Its very much possible that we dont find fid
261                          * on precreated files, LAST_ID
262                          */
263                         continue;
264
265                 ff_group = le64_to_cpu(trusted_fid.ff_group);
266                 ff_objid = le64_to_cpu(trusted_fid.ff_objid);
267
268                 if (ff_group >= MAX_GROUPS) {
269                         fprintf(stderr, "error: invalid group "LPU64" likely"
270                                 "indicates a corrupt xattr for file %s.\n",
271                                 ff_group, file_path);
272                         continue;
273                 }
274
275                 /* might need to create the parent directories for
276                    this object */
277                 if (ll_sprintf(dest_path, PATH_MAX, "%s/O/"LPU64"/d"LPU64,
278                                mount_path, ff_group, ff_objid % 32)) {
279                         closedir(dir_ptr);
280                         return 1;
281                 }
282
283                 ret = mkdir_p(dest_path, mount_path, ff_group);
284                 if (ret) {
285                         closedir(dir_ptr);
286                         return ret;
287                 }
288
289                 /*
290                  * Object ID needs to be verified against last_id.
291                  * LAST_ID file may not be present in the group directory
292                  * due to corruption. In case of any error tyr to recover
293                  * as many objects as possible by setting last_id to ~0ULL.
294                  */
295                 if (last_id[ff_group] == 0) {
296                         if (ll_sprintf(last_id_file, PATH_MAX,
297                                        "%s/O/"LPU64"/LAST_ID",
298                                        mount_path, ff_group)) {
299                                 closedir(dir_ptr);
300                                 return 1;
301                         }
302
303                         tmp_last_id = read_last_id(last_id_file);
304                         if (tmp_last_id == 0)
305                                 tmp_last_id = ~0ULL;
306                         last_id[ff_group] = tmp_last_id;
307                 }
308
309                 if (ff_objid > last_id[ff_group]) {
310                         fprintf(stderr, "error: file skipped because object ID "
311                                 "greater than LAST_ID\nFilename: %s\n"
312                                 "Group: "LPU64"\nObjectid: "LPU64"\n"
313                                 "LAST_ID: "LPU64, file_path, ff_group, ff_objid,
314                                 last_id[ff_group]);
315                         continue;
316                 }
317
318                 /* move file from lost+found to proper object
319                    directory */
320                 if (ll_sprintf(dest_path, PATH_MAX,
321                                "%s/O/"LPU64"/d"LPU64"/"LPU64, mount_path,
322                                ff_group, ff_objid % 32, ff_objid)) {
323                         closedir(dir_ptr);
324                         return 1;
325                 }
326
327                 obj_exists = 1;
328                 ret = stat(dest_path, &st);
329                 if (ret == 0) {
330                         if (st.st_size == 0)
331                                 obj_exists = 0;
332                 } else {
333                         if (errno != ENOENT)
334                                 fprintf(stderr,
335                                         "warning: stat for %s: %s\n",
336                                         dest_path, strerror(errno));
337                         obj_exists = 0;
338                 }
339
340                 if (obj_exists) {
341                         fprintf(stderr, "error: target object %s already "
342                                 "exists and will not be replaced.\n",dest_path);
343                         continue;
344                 }
345
346                 if (rename(file_path, dest_path) < 0) {
347                         fprintf(stderr, "error: rename failed for file %s: %s\n",
348                                 file_path, strerror(errno));
349                         error++;
350                         continue;
351                 }
352
353                 printf("Object %s restored.\n", dest_path);
354                 break;
355                 }
356         }
357
358         closedir(dir_ptr);
359
360         return error;
361 }
362
363 /*
364  * If LAST_ID file is not present in some group then restore it with the highest
365  * object ID found in that group. By the time we come here all possible objects
366  * have been restored.
367  */
368 static int check_last_id(const char *mount_path)
369 {
370         char lastid_path[PATH_MAX];
371         char dirname[PATH_MAX], subdirname[PATH_MAX];
372         DIR *groupdir, *subdir;
373         struct stat st;
374         struct dirent *dirent;
375         __u64 group;
376         __u64 max_objid;
377         int fd;
378         int ret;
379
380         for (group = 0; group < MAX_GROUPS; group++) {
381                 max_objid = 0;
382
383                 if (ll_sprintf(dirname, PATH_MAX, "%s/O/"LPU64,
384                                mount_path, group))
385                         return 1;
386                 if (ll_sprintf(lastid_path, PATH_MAX, "%s/LAST_ID", dirname))
387                         return 1;
388
389                 if (stat(lastid_path, &st) == 0)
390                         continue;
391
392                 groupdir = opendir(dirname);
393                 if (groupdir == NULL) {
394                         if (errno != ENOENT)
395                                 fprintf(stderr, "error: opening %s: %s\n",
396                                         dirname, strerror(errno));
397                         continue;
398                 }
399
400                 while ((dirent = readdir(groupdir)) != NULL) {
401                         if (!strcmp(dirent->d_name, ".") ||
402                             !strcmp(dirent->d_name, ".."))
403                                 continue;
404
405                         if (ll_sprintf(subdirname, PATH_MAX, "%s/%s",
406                                        dirname, dirent->d_name)) {
407                                 closedir(groupdir);
408                                 return 1;
409                         }
410                         subdir = opendir(subdirname);
411                         if (subdir == NULL) {
412                                 fprintf(stderr, "error: opening %s: %s\n",
413                                         subdirname, strerror(errno));
414                                 continue;
415                         }
416
417                         while ((dirent = readdir(subdir)) != NULL) {
418                                 __u64 objid;
419                                 char *end;
420
421                                 if (!strcmp(dirent->d_name, ".") ||
422                                     !strcmp(dirent->d_name, ".."))
423                                         continue;
424
425                                 objid = strtoull(dirent->d_name, &end, 0);
426                                 if (end == dirent->d_name || *end != 0) {
427                                         fprintf(stderr, "error: unknown object"
428                                                 "ID %s/%s\n", subdirname,
429                                                 dirent->d_name);
430                                         continue;
431                                 }
432                                 if (objid > max_objid)
433                                        max_objid = objid;
434                         }
435                         closedir(subdir);
436                 }
437                 closedir(groupdir);
438
439                 fd = open(lastid_path, O_RDWR | O_CREAT, 0700);
440                 if (fd < 0) {
441                         fprintf(stderr, "error: open \"%s\" failed: %s\n",
442                                 lastid_path, strerror(errno));
443                         return 1;
444                 }
445
446                 max_objid = cpu_to_le64(max_objid);
447                 ret = write(fd, &max_objid, sizeof(__u64));
448                 if (ret < sizeof(__u64)) {
449                         fprintf(stderr, "error: write \"%s\" failed: %s\n",
450                                 lastid_path, strerror(errno));
451                         close(fd);
452                         return 1;
453                 }
454
455                 close(fd);
456         }
457
458         return 0;
459 }
460
461 int main(int argc, char **argv)
462 {
463         char *progname;
464         struct stat stat_buf;
465         char src_dir[PATH_MAX] = "";
466         char mount_path[PATH_MAX];
467         char tmp_path[PATH_MAX];
468         int c;
469         int retval;
470
471         progname = argv[0];
472
473         while ((c = getopt(argc, argv, "d:hv")) != EOF) {
474                 switch (c) {
475                 case 'd':
476                         if (chdir(optarg)) {
477                                 fprintf(stderr, "error: chdir to %s: %s\n",
478                                         optarg, strerror(errno));
479                                 return 1;
480                         }
481                         if (getcwd(src_dir, PATH_MAX) == NULL) {
482                                 fprintf(stderr,
483                                         "error: getcwd of lost+found: %s\n",
484                                         strerror(errno));
485                                 return 1;
486                         }
487                         if (chdir("..")) {
488                                 fprintf(stderr, "error: chdir to \"..\": %s\n",
489                                         strerror(errno));
490                                 return 1;
491                         }
492                         if (getcwd(mount_path, PATH_MAX) == NULL) {
493                                 fprintf(stderr,
494                                         "error: getcwd of mount point: %s\n",
495                                         strerror(errno));
496                                 return 1;
497                         }
498                         if (!strcmp(src_dir, mount_path)) {
499                                 fprintf(stderr,
500                                         "error: root directory is detected\n");
501                                 return 1;
502                         }
503                         fprintf(stdout, "\"lost+found\" directory path: %s\n",
504                                 src_dir);
505                         break;
506                 case 'v':
507                         verbose = 1;
508                         break;
509                 case 'h':
510                         usage(progname);
511                 default:
512                         fprintf(stderr, "%s: bad option '%c'\n",
513                                 progname, c);
514                         usage(progname);
515                 }
516         }
517
518         if (src_dir[0] == 0)
519                 usage(progname);
520
521         /* Check if 'O' directory exists and create it if needed */
522         if (ll_sprintf(tmp_path, PATH_MAX, "%s/O",  mount_path))
523                 return 1;
524
525         if (stat(tmp_path, &stat_buf) != 0) {
526                 retval = mkdir(tmp_path, 0700);
527                 if (retval == -1) {
528                         fprintf(stderr, "error: creating objects directory %s:"
529                                 " %s\n", tmp_path, strerror(errno));
530                         return 1;
531                 }
532         }
533
534         retval = traverse_lost_found(src_dir, mount_path);
535         if (retval) {
536                 fprintf(stderr, "error: traversing lost+found looking for "
537                         "orphan objects.\n");
538                 return retval;
539         }
540
541         retval = check_last_id(mount_path);
542         if (retval)
543                 fprintf(stderr, "error: while checking/restoring LAST_ID.\n");
544
545         return retval;
546 }