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