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, 2013, 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 <lustre/lustre_idl.h>
61 #include <libcfs/list.h>
68 struct obd_group_info {
71 struct list_head grp_list;
74 struct list_head grp_info_list;
76 static void grp_info_list_destroy(struct list_head *list)
78 struct obd_group_info *grp, *tmp;
80 list_for_each_entry_safe(grp, tmp, list, grp_list) {
81 list_del_init(&grp->grp_list);
86 static void usage(char *progname)
88 fprintf(stderr, "Usage: %s [-hnv] -d directory\n"
89 "Recover Lustre OST objects put in lost+found by e2fsck.\n"
90 "\t-d: Check directory, usually '/lost+found' (required).\n"
91 "\t Alternately, verify object directories under '/O'.\n"
92 "\t-n: Do not modify filesystem, only report changes.\n"
93 "\t-h: Print help message.\n"
94 "\t-v: Print changes verbosely.\n",
99 static int _ll_sprintf(char *buf, size_t size, const char *func, int line,
100 const char *format, ...)
105 va_start(ap, format);
106 rc = vsnprintf(buf, size, format, ap);
107 if (!(rc > -1 && rc < size)) {
109 "error: %s:%d: path \"", func, line);
110 vfprintf(stderr, format, ap);
112 fprintf(stderr, "\" is too long\n");
119 #define ll_sprintf(buf, size, format, ...) \
120 _ll_sprintf(buf, size, __FUNCTION__, __LINE__, format, ## __VA_ARGS__)
122 static int mkdir_p(const char *dest_path, mode_t mode)
124 struct stat stat_buf;
127 rc = stat(dest_path, &stat_buf);
129 if (S_ISDIR(stat_buf.st_mode))
131 if (!S_ISDIR(stat_buf.st_mode)) {
132 fprintf(stderr, "error: '%s' is not a directory (%o)\n",
133 dest_path, stat_buf.st_mode);
137 } else if (errno != ENOENT) {
139 fprintf(stderr, "error: error checking directory '%s': %s\n",
140 dest_path, strerror(errno));
145 fprintf(stderr, "dry_run: not creating directory '%s'\n",
151 rc = mkdir(dest_path, mode);
153 fprintf(stderr, "error: creating directory '%s': %s\n",
154 dest_path, strerror(errno));
159 /* This is returning 0 for an error */
160 static __u64 read_last_id(char *file_path)
166 fd = open(file_path, O_RDONLY);
169 fprintf(stderr, "error: opening '%s': %s\n",
170 file_path, strerror(errno));
174 count = read(fd, &last_id, sizeof(last_id));
176 fprintf(stderr, "error: reading file '%s': %s\n",
177 file_path, strerror(errno));
181 if (count != sizeof(last_id)) {
182 fprintf(stderr, "error: only read %d bytes from '%s'\n",
189 return le64_to_cpu(last_id);
192 struct obd_group_info *find_or_create_grp(struct list_head *list, __u64 seq,
195 struct obd_group_info *grp;
196 struct list_head *entry;
197 char tmp_path[PATH_MAX];
202 list_for_each(entry, list) {
203 grp = (struct obd_group_info *)list_entry(entry,
204 struct obd_group_info,
206 if (grp->grp_seq == seq)
210 grp = malloc(sizeof(struct obd_group_info));
214 sprintf(seq_name, (fid_seq_is_rsvd(seq) ||
215 fid_seq_is_mdt0(seq)) ? LPU64 : LPX64i,
216 fid_seq_is_idif(seq) ? 0 : seq);
218 /* Check whether the obj dir has been created */
219 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/%s", mount, seq_name)) {
224 retval = mkdir_p(tmp_path, 0700);
227 fprintf(stderr, "error: creating directory %s: %s\n",
228 tmp_path, strerror(errno));
232 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/%s/LAST_ID",
239 * Object ID needs to be verified against last_id.
240 * LAST_ID file may not be present in the group directory
241 * due to corruption. In case of any error try to recover
242 * as many objects as possible by setting last_id to ~0ULL.
244 tmp_last_id = read_last_id(tmp_path);
245 if (tmp_last_id == 0)
247 grp->grp_last_id = tmp_last_id;
250 list_add(&grp->grp_list, list);
254 static unsigned filetype_dir_table[] = {
263 #if defined(DT_DOOR) && defined(S_IFDOOR)
268 static int traverse_lost_found(char *src_dir, const char *mount_path)
271 struct lustre_mdt_attrs lma;
272 struct dirent64 *dirent;
273 __u64 ff_seq, ff_objid;
275 char dest_path[PATH_MAX];
277 int obj_exists, xattr_len;
278 int len, ret = 0, error = 0;
281 struct obd_group_info *grp_info;
283 len = strlen(src_dir);
285 dir_ptr = opendir(src_dir);
287 fprintf(stderr, "error: opening directory: %s\n",
292 while ((dirent = readdir64(dir_ptr)) != NULL) {
293 if (!strcmp(dirent->d_name, ".") ||
294 !strcmp(dirent->d_name, ".."))
298 if ((len + strlen(dirent->d_name) + 2) > PATH_MAX) {
299 fprintf(stderr, "error: %s/%s: path too long\n",
300 src_dir, dirent->d_name);
303 strcat(src_dir, "/");
304 strcat(src_dir, dirent->d_name);
306 if (dirent->d_type == DT_UNKNOWN) {
307 ret = stat(src_dir, &st);
310 "error: stating %s: %s\n",
311 src_dir, strerror(errno));
314 dirent->d_type = filetype_dir_table[st.st_mode &
316 if (dirent->d_type == DT_UNKNOWN) {
318 "error: %s of unknown type 0%o\n",
319 src_dir, st.st_mode);
324 switch(dirent->d_type) {
326 ret = traverse_lost_found(src_dir, mount_path);
335 xattr_len = getxattr(file_path, "trusted.lma",
336 (void *)&lma, sizeof(lma));
337 if (xattr_len == -1 || xattr_len < sizeof(lma)) {
338 struct filter_fid_old ff;
340 /* try old filter_fid EA */
341 xattr_len = getxattr(file_path, "trusted.fid",
342 (void *)&ff, sizeof(ff));
343 /* It's very much possible that we don't find any
344 * FID on precreated or unused objects or LAST_ID.
345 * The xattr needs to hold the full filter_fid_old
346 * with the OID/parent to be useful. */
347 if (xattr_len == -1 || xattr_len < sizeof(ff))
350 ff_seq = le64_to_cpu(ff.ff_seq);
351 ff_objid = le64_to_cpu(ff.ff_objid);
353 printf(DOSTID": ", ff_seq, ff_objid);
356 printf(DFID": ", PFID(&lma.lma_self_fid));
357 ff_seq = le64_to_cpu(lma.lma_self_fid.f_seq);
358 ff_objid = le32_to_cpu(lma.lma_self_fid.f_oid);
361 sprintf(seq_name, (fid_seq_is_rsvd(ff_seq) ||
362 fid_seq_is_mdt0(ff_seq)) ? LPU64 : LPX64i,
363 fid_seq_is_idif(ff_seq) ? 0 : ff_seq);
365 /* LAST_ID uses OID = 0. It will be regenerated later. */
368 printf("'%s': LAST_ID\n", file_path);
372 sprintf(obj_name, (fid_seq_is_rsvd(ff_seq) ||
373 fid_seq_is_mdt0(ff_seq) ||
374 fid_seq_is_idif(ff_seq)) ?
375 LPU64 : LPX64i, ff_objid);
377 grp_info = find_or_create_grp(&grp_info_list, ff_seq,
379 if (grp_info == NULL) {
384 /* Might need to create the parent directory for this object */
385 if (ll_sprintf(dest_path, PATH_MAX, "%s/O/%s/d"LPU64,
386 mount_path, seq_name, ff_objid % 32)) {
391 /* The O/{seq} directory was created in find_or_create_grp() */
392 ret = mkdir_p(dest_path, 0700);
398 if (ff_objid > grp_info->grp_last_id) {
399 fprintf(stderr, "error: file skipped because object ID "
400 "greater than LAST_ID\nFilename: %s\n"
401 "Group: "LPU64"\nObjectid: "LPU64"\n"
402 "LAST_ID: "LPU64, file_path, ff_seq, ff_objid,
403 grp_info->grp_last_id);
407 /* move file from lost+found to proper object directory */
408 if (ll_sprintf(dest_path, PATH_MAX,
409 "%s/O/%s/d"LPU64"/%s", mount_path,
410 seq_name, ff_objid % 32, obj_name)) {
415 /* Source and destination are the same file, do nothing. */
416 if (strcmp(file_path, dest_path) == 0) {
418 printf("'%s': OK\n", file_path);
423 ret = stat(dest_path, &st);
429 fprintf(stderr, "warning: stat for %s: %s\n",
430 dest_path, strerror(errno));
435 fprintf(dry_run ? stdout : stderr,
436 "%s: '%s' exists, will not replace with '%s'\n",
437 dry_run ? "dry_run" : "error",
438 dest_path, file_path);
442 printf("dry_run: not renaming '%s' to '%s'\n",
443 file_path, dest_path);
446 if (rename(file_path, dest_path) < 0) {
447 fprintf(stderr, "error: rename failed for '%s': %s\n",
448 file_path, strerror(errno));
453 printf("object '%s' restored.\n", dest_path);
464 * If LAST_ID file is not present in some group then restore it with the highest
465 * object ID found in that group. By the time we come here all possible objects
466 * have been restored.
468 static int check_last_id(const char *mount_path)
470 char lastid_path[PATH_MAX];
471 char dirname[PATH_MAX], subdirname[PATH_MAX];
472 DIR *groupdir, *subdir;
474 struct dirent *dirent;
480 for (group = 0; group < MAX_GROUPS; group++) {
483 if (ll_sprintf(dirname, PATH_MAX, "%s/O/"LPU64,
486 if (ll_sprintf(lastid_path, PATH_MAX, "%s/LAST_ID", dirname))
489 if (stat(lastid_path, &st) == 0)
492 groupdir = opendir(dirname);
493 if (groupdir == NULL) {
495 fprintf(stderr, "error: opening %s: %s\n",
496 dirname, strerror(errno));
500 while ((dirent = readdir(groupdir)) != NULL) {
501 if (!strcmp(dirent->d_name, ".") ||
502 !strcmp(dirent->d_name, ".."))
505 if (ll_sprintf(subdirname, PATH_MAX, "%s/%s",
506 dirname, dirent->d_name)) {
510 subdir = opendir(subdirname);
511 if (subdir == NULL) {
512 fprintf(stderr, "error: opening %s: %s\n",
513 subdirname, strerror(errno));
517 while ((dirent = readdir(subdir)) != NULL) {
521 if (!strcmp(dirent->d_name, ".") ||
522 !strcmp(dirent->d_name, ".."))
525 objid = strtoull(dirent->d_name, &end, 0);
526 if (end == dirent->d_name || *end != 0) {
527 fprintf(stderr, "error: unknown object"
528 "ID %s/%s\n", subdirname,
532 if (objid > max_objid)
540 fprintf(stderr, "dry_run: not updating '%s' to "
541 LPU64"\n", lastid_path, max_objid);
544 fd = open(lastid_path, O_RDWR | O_CREAT, 0700);
546 fprintf(stderr, "error: open '%s' failed: %s\n",
547 lastid_path, strerror(errno));
551 max_objid = cpu_to_le64(max_objid);
552 ret = write(fd, &max_objid, sizeof(__u64));
553 if (ret < sizeof(__u64)) {
554 fprintf(stderr, "error: write '%s' failed: %s\n",
555 lastid_path, strerror(errno));
566 int main(int argc, char **argv)
569 char src_dir[PATH_MAX] = "";
570 char mount_path[PATH_MAX];
571 char tmp_path[PATH_MAX];
575 progname = strrchr(argv[0], '/');
576 if (progname++ == NULL)
579 while ((c = getopt(argc, argv, "d:hnv")) != EOF) {
583 fprintf(stderr, "error: chdir to %s: %s\n",
584 optarg, strerror(errno));
587 if (getcwd(src_dir, PATH_MAX) == NULL) {
589 "error: getcwd of lost+found: %s\n",
594 fprintf(stderr, "error: chdir to \"..\": %s\n",
598 if (getcwd(mount_path, PATH_MAX) == NULL) {
600 "error: getcwd of mount point: %s\n",
604 if (!strcmp(src_dir, mount_path)) {
606 "error: root directory is detected\n");
609 fprintf(stdout, "%s: %sscan directory path: %s\n",
610 progname, dry_run ? "read_only " : "", src_dir);
621 fprintf(stderr, "%s: bad option '%c'\n",
630 /* Check if 'O' directory exists and create it if needed */
631 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O", mount_path))
634 rc = mkdir_p(tmp_path, 0700);
636 fprintf(stderr, "error: creating objects directory %s:"
637 " %s\n", tmp_path, strerror(errno));
641 INIT_LIST_HEAD(&grp_info_list);
642 rc = traverse_lost_found(src_dir, mount_path);
644 fprintf(stderr, "error: traversing lost+found looking for "
645 "orphan objects.\n");
649 rc = check_last_id(mount_path);
651 fprintf(stderr, "error: while checking/restoring LAST_ID.\n");
654 grp_info_list_destroy(&grp_info_list);
656 printf("%s: scan finished: rc = %d\n", progname, rc);