4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/utils/ll_recover_lost_found_objs.c
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.
43 * Author: Kalpak Shah <kalpak.shah@sun.com>
56 #include <sys/types.h>
57 #include <sys/xattr.h>
60 #include <liblustre.h>
61 #include <libcfs/list.h>
67 struct obd_group_info {
73 cfs_list_t grp_info_list;
75 static void grp_info_list_destroy(cfs_list_t *list)
77 struct obd_group_info *grp, *tmp;
79 cfs_list_for_each_entry_safe(grp, tmp, list, grp_list) {
80 cfs_list_del_init(&grp->grp_list);
85 static void usage(char *progname)
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");
95 static int _ll_sprintf(char *buf, size_t size, const char *func, int line,
96 const char *format, ...)
101 va_start(ap, format);
102 rc = vsnprintf(buf, size, format, ap);
103 if (!(rc > -1 && rc < size)) {
105 "error: %s:%d: path \"", func, line);
106 vfprintf(stderr, format, ap);
108 fprintf(stderr, "\" is too long\n");
115 #define ll_sprintf(buf, size, format, ...) \
116 _ll_sprintf(buf, size, __FUNCTION__, __LINE__, format, ## __VA_ARGS__)
118 static int mkdir_p(const char *dest_path, const char *mount)
120 struct stat stat_buf;
124 if (stat(dest_path, &stat_buf) == 0)
127 retval = mkdir(dest_path, mode);
129 fprintf(stderr, "error: creating directory %s: "
130 "%s\n", dest_path, strerror(errno));
137 /* This is returning 0 for an error */
138 static __u64 read_last_id(char *file_path)
144 fd = open(file_path, O_RDONLY);
147 fprintf(stderr, "error: opening %s: %s\n",
148 file_path, strerror(errno));
152 count = read(fd, &last_id, sizeof(last_id));
154 fprintf(stderr, "error: reading file %s: %s\n", file_path,
159 if (count != sizeof(last_id)) {
160 fprintf(stderr, "error: Could not read full last_id from %s\n",
167 return le64_to_cpu(last_id);
170 struct obd_group_info *find_or_create_grp(cfs_list_t *list, __u64 seq,
173 struct obd_group_info *grp;
175 char tmp_path[PATH_MAX];
177 struct stat stat_buf;
181 cfs_list_for_each(entry, list) {
182 grp = (struct obd_group_info *)cfs_list_entry(entry,
183 struct obd_group_info,
185 if (grp->grp_seq == seq)
189 grp = malloc(sizeof(struct obd_group_info));
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);
197 /* Check whether the obj dir has been created */
198 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/%s", mount, seq_name)) {
203 if (stat(tmp_path, &stat_buf) != 0) {
204 retval = mkdir(tmp_path, 0700);
207 fprintf(stderr, "error: creating directory %s: "
208 "%s\n", tmp_path, strerror(errno));
213 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/%s/LAST_ID",
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.
225 tmp_last_id = read_last_id(tmp_path);
226 if (tmp_last_id == 0)
228 grp->grp_last_id = tmp_last_id;
231 cfs_list_add(&grp->grp_list, list);
235 static unsigned filetype_dir_table[] = {
244 #if defined(DT_DOOR) && defined(S_IFDOOR)
249 static int traverse_lost_found(char *src_dir, const char *mount_path)
252 struct filter_fid parent_fid;
253 struct dirent64 *dirent;
254 __u64 ff_seq, ff_objid;
256 char dest_path[PATH_MAX];
258 int obj_exists, xattr_len;
259 int len, ret = 0, error = 0;
262 struct obd_group_info *grp_info;
264 len = strlen(src_dir);
266 dir_ptr = opendir(src_dir);
268 fprintf(stderr, "error: opening directory: %s\n",
273 while ((dirent = readdir64(dir_ptr)) != NULL) {
274 if (!strcmp(dirent->d_name, ".") ||
275 !strcmp(dirent->d_name, ".."))
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);
284 strcat(src_dir, "/");
285 strcat(src_dir, dirent->d_name);
287 if (dirent->d_type == DT_UNKNOWN) {
288 ret = stat(src_dir, &st);
291 "error: stating %s: %s\n",
292 src_dir, strerror(errno));
295 dirent->d_type = filetype_dir_table[st.st_mode &
297 if (dirent->d_type == DT_UNKNOWN) {
299 "error: %s of unknown type 0%o\n",
300 src_dir, st.st_mode);
305 switch(dirent->d_type) {
307 ret = traverse_lost_found(src_dir, mount_path);
316 xattr_len = getxattr(file_path, "trusted.fid",
320 if (xattr_len == -1 || xattr_len < sizeof(parent_fid))
322 * Its very much possible that we dont find fid
323 * on precreated files, LAST_ID
327 ff_seq = le64_to_cpu(parent_fid.ff_seq);
328 sprintf(seq_name, (fid_seq_is_rsvd(ff_seq) ||
329 fid_seq_is_mdt0(ff_seq)) ? LPU64 : LPX64i,
330 fid_seq_is_idif(ff_seq) ? 0 : ff_seq);
333 ff_objid = le64_to_cpu(parent_fid.ff_objid);
334 sprintf(obj_name, (fid_seq_is_rsvd(parent_fid.ff_seq) ||
335 fid_seq_is_mdt0(parent_fid.ff_seq) ||
336 fid_seq_is_idif(parent_fid.ff_seq)) ?
337 LPU64 : LPX64i, ff_objid);
339 grp_info = find_or_create_grp(&grp_info_list, ff_seq,
341 if (grp_info == NULL) {
346 /* might need to create the parent directories for
348 if (ll_sprintf(dest_path, PATH_MAX, "%s/O/%s/d"LPU64,
349 mount_path, seq_name, ff_objid % 32)) {
354 ret = mkdir_p(dest_path, mount_path);
360 if (ff_objid > grp_info->grp_last_id) {
361 fprintf(stderr, "error: file skipped because object ID "
362 "greater than LAST_ID\nFilename: %s\n"
363 "Group: "LPU64"\nObjectid: "LPU64"\n"
364 "LAST_ID: "LPU64, file_path, ff_seq, ff_objid,
365 grp_info->grp_last_id);
369 /* move file from lost+found to proper object
371 if (ll_sprintf(dest_path, PATH_MAX,
372 "%s/O/%s/d"LPU64"/%s", mount_path,
373 seq_name, ff_objid % 32, obj_name)) {
379 ret = stat(dest_path, &st);
386 "warning: stat for %s: %s\n",
387 dest_path, strerror(errno));
392 fprintf(stderr, "error: target object %s already "
393 "exists and will not be replaced.\n",dest_path);
397 if (rename(file_path, dest_path) < 0) {
398 fprintf(stderr, "error: rename failed for file %s: %s\n",
399 file_path, strerror(errno));
404 printf("Object %s restored.\n", dest_path);
415 * If LAST_ID file is not present in some group then restore it with the highest
416 * object ID found in that group. By the time we come here all possible objects
417 * have been restored.
419 static int check_last_id(const char *mount_path)
421 char lastid_path[PATH_MAX];
422 char dirname[PATH_MAX], subdirname[PATH_MAX];
423 DIR *groupdir, *subdir;
425 struct dirent *dirent;
431 for (group = 0; group < MAX_GROUPS; group++) {
434 if (ll_sprintf(dirname, PATH_MAX, "%s/O/"LPU64,
437 if (ll_sprintf(lastid_path, PATH_MAX, "%s/LAST_ID", dirname))
440 if (stat(lastid_path, &st) == 0)
443 groupdir = opendir(dirname);
444 if (groupdir == NULL) {
446 fprintf(stderr, "error: opening %s: %s\n",
447 dirname, strerror(errno));
451 while ((dirent = readdir(groupdir)) != NULL) {
452 if (!strcmp(dirent->d_name, ".") ||
453 !strcmp(dirent->d_name, ".."))
456 if (ll_sprintf(subdirname, PATH_MAX, "%s/%s",
457 dirname, dirent->d_name)) {
461 subdir = opendir(subdirname);
462 if (subdir == NULL) {
463 fprintf(stderr, "error: opening %s: %s\n",
464 subdirname, strerror(errno));
468 while ((dirent = readdir(subdir)) != NULL) {
472 if (!strcmp(dirent->d_name, ".") ||
473 !strcmp(dirent->d_name, ".."))
476 objid = strtoull(dirent->d_name, &end, 0);
477 if (end == dirent->d_name || *end != 0) {
478 fprintf(stderr, "error: unknown object"
479 "ID %s/%s\n", subdirname,
483 if (objid > max_objid)
490 fd = open(lastid_path, O_RDWR | O_CREAT, 0700);
492 fprintf(stderr, "error: open \"%s\" failed: %s\n",
493 lastid_path, strerror(errno));
497 max_objid = cpu_to_le64(max_objid);
498 ret = write(fd, &max_objid, sizeof(__u64));
499 if (ret < sizeof(__u64)) {
500 fprintf(stderr, "error: write \"%s\" failed: %s\n",
501 lastid_path, strerror(errno));
512 int main(int argc, char **argv)
515 struct stat stat_buf;
516 char src_dir[PATH_MAX] = "";
517 char mount_path[PATH_MAX];
518 char tmp_path[PATH_MAX];
524 while ((c = getopt(argc, argv, "d:hv")) != EOF) {
528 fprintf(stderr, "error: chdir to %s: %s\n",
529 optarg, strerror(errno));
532 if (getcwd(src_dir, PATH_MAX) == NULL) {
534 "error: getcwd of lost+found: %s\n",
539 fprintf(stderr, "error: chdir to \"..\": %s\n",
543 if (getcwd(mount_path, PATH_MAX) == NULL) {
545 "error: getcwd of mount point: %s\n",
549 if (!strcmp(src_dir, mount_path)) {
551 "error: root directory is detected\n");
554 fprintf(stdout, "\"lost+found\" directory path: %s\n",
563 fprintf(stderr, "%s: bad option '%c'\n",
572 /* Check if 'O' directory exists and create it if needed */
573 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O", mount_path))
576 if (stat(tmp_path, &stat_buf) != 0) {
577 retval = mkdir(tmp_path, 0700);
579 fprintf(stderr, "error: creating objects directory %s:"
580 " %s\n", tmp_path, strerror(errno));
585 CFS_INIT_LIST_HEAD(&grp_info_list);
586 retval = traverse_lost_found(src_dir, mount_path);
588 fprintf(stderr, "error: traversing lost+found looking for "
589 "orphan objects.\n");
593 retval = check_last_id(mount_path);
595 fprintf(stderr, "error: while checking/restoring LAST_ID.\n");
598 grp_info_list_destroy(&grp_info_list);