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