1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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>
66 struct obd_group_info {
69 struct obd_group_info grp_info[MAX_GROUPS];
71 void usage(char *progname)
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");
81 int mkdir_p(char *dest_path, char *mount, __u64 ff_group)
84 char tmp_path[PATH_MAX];
88 if (stat(dest_path, &stat_buf) == 0)
91 if (grp_info[ff_group].dir_exists == 0) {
92 sprintf(tmp_path, "%s/O/"LPU64, mount, ff_group);
93 if (stat(tmp_path, &stat_buf) != 0) {
94 retval = mkdir(tmp_path, 0700);
96 fprintf(stderr, "error: creating directory %s: "
97 "%s\n", tmp_path, strerror(errno));
100 grp_info[ff_group].dir_exists = 1;
104 retval = mkdir(dest_path, mode);
111 /* This is returning 0 for an error */
112 __u64 read_last_id(char *file_path)
118 fd = open(file_path, O_RDONLY);
122 count = read(fd, &last_id, sizeof(last_id));
124 fprintf(stderr, "error: reading file %s: %s\n", file_path,
129 if (count != sizeof(last_id)) {
130 fprintf(stderr, "error: Could not read full last_id from %s\n",
137 return le64_to_cpu(last_id);
140 static unsigned filetype_dir_table[] = {
149 #if defined(DT_DOOR) && defined(S_IFDOOR)
154 static int traverse_lost_found(char *src_dir, char *mount_path)
157 struct filter_fid trusted_fid;
158 struct dirent64 *dirent;
159 __u64 ff_group, ff_objid;
160 char file_path[PATH_MAX];
161 char dest_path[PATH_MAX];
162 char last_id_file[PATH_MAX];
163 __u64 last_id[MAX_GROUPS] = {0};
166 int obj_exists, xattr_len;
167 int len, ret = 0, error = 0;
169 len = strlen(src_dir);
171 dir_ptr = opendir(src_dir);
173 fprintf(stderr, "error: opening directory: %s\n",
178 while ((dirent = readdir64(dir_ptr)) != NULL) {
179 if (!strcmp(dirent->d_name, ".") ||
180 !strcmp(dirent->d_name, ".."))
184 if ((len + dirent->d_reclen + 2) > PATH_MAX) {
185 fprintf(stderr, "error: %s: string buffer is too small",
189 strcat(src_dir, "/");
190 strcat(src_dir, dirent->d_name);
192 if (dirent->d_type == DT_UNKNOWN) {
195 ret = stat(src_dir, &st);
197 dirent->d_type = filetype_dir_table[st.st_mode &
201 switch(dirent->d_type) {
203 ret = traverse_lost_found(src_dir, mount_path);
209 sprintf(file_path, "%s", src_dir);
210 xattr_len = getxattr(file_path, "trusted.fid", (void *)&trusted_fid,
211 sizeof(trusted_fid));
213 if (xattr_len < 0 || xattr_len < sizeof(trusted_fid)) {
215 * Its very much possible that we dont find fid
216 * on precreated files, LAST_ID
221 ff_group = le64_to_cpu(trusted_fid.ff_group);
222 ff_objid = le64_to_cpu(trusted_fid.ff_objid);
224 if (ff_group >= MAX_GROUPS) {
225 fprintf(stderr, "error: invalid group "LPU64" likely"
226 "indicates a corrupt xattr for file %s.\n",
227 ff_group, file_path);
231 /* might need to create the parent directories for this object */
232 sprintf(dest_path, "%s/O/"LPU64"/d"LPU64, mount_path, ff_group,
235 ret = mkdir_p(dest_path, mount_path, ff_group);
237 fprintf(stderr, "error: creating directory %s : %s\n",
238 dest_path, strerror(errno));
243 * Object ID needs to be verified against last_id.
244 * LAST_ID file may not be present in the group directory
245 * due to corruption. In case of any error try to recover
246 * as many objects as possible by setting last_id to ~0ULL.
248 if (last_id[ff_group] == 0) {
249 sprintf(last_id_file, "%s/O/"LPU64"/LAST_ID",
250 mount_path, ff_group);
251 tmp_last_id = read_last_id(last_id_file);
253 if (tmp_last_id == 0)
255 last_id[ff_group] = tmp_last_id;
258 if (ff_objid > last_id[ff_group]) {
259 fprintf(stderr, "error: file skipped because object ID "
260 "greater than LAST_ID\nFilename: %s\n"
261 "Group: "LPU64"\nObjectid: "LPU64"\n"
262 "LAST_ID: "LPU64, file_path, ff_group, ff_objid,
267 /* move file from lost+found to proper object directory */
268 sprintf(dest_path, "%s/O/"LPU64"/d"LPU64"/"LPU64, mount_path,
269 ff_group, ff_objid % 32, ff_objid);
272 ret = stat(dest_path, &st);
276 } else if (ret < 0 && errno == ENOENT) {
281 fprintf(stderr, "error: target object %s already "
282 "exists and will not be replaced.\n",dest_path);
286 if (rename(file_path, dest_path) < 0) {
287 fprintf(stderr, "error: rename failed for file %s: %s\n",
288 file_path, strerror(errno));
293 printf("Object %s restored.\n", dest_path);
308 * If LAST_ID file is not present in some group then restore it with the highest
309 * object ID found in that group. By the time we come here all possible objects
310 * have been restored.
312 int check_last_id(char *mount_path)
314 char lastid_path[PATH_MAX];
315 char dirname[PATH_MAX], subdirname[PATH_MAX];
316 DIR *groupdir, *subdir;
318 struct dirent *dirent;
319 unsigned long long group;
324 for (group = 0; group < MAX_GROUPS; group++) {
326 sprintf(dirname, "%s/O/%llu", mount_path, group);
328 strcpy(lastid_path, dirname);
329 strcat(lastid_path, "/LAST_ID");
330 if (stat(lastid_path, &st) == 0)
333 groupdir = opendir(dirname);
334 if (groupdir == NULL) {
336 fprintf(stderr, "error: opening %s: %s\n",
337 dirname, strerror(errno));
341 while ((dirent = readdir(groupdir)) != NULL) {
342 if (!strcmp(dirent->d_name, ".") ||
343 !strcmp(dirent->d_name, ".."))
346 sprintf(subdirname, "%s/%s", dirname, dirent->d_name);
348 subdir = opendir(subdirname);
349 if (subdir == NULL) {
350 fprintf(stderr, "error: opening %s: %s\n",
351 subdirname, strerror(errno));
355 while ((dirent = readdir(subdir)) != NULL) {
359 if (!strcmp(dirent->d_name, ".") ||
360 !strcmp(dirent->d_name, ".."))
363 objid = strtoull(dirent->d_name, &end, 0);
364 if (end == dirent->d_name || *end != 0) {
365 fprintf(stderr, "error: unknown object"
366 "ID %s/%s\n", subdirname,
370 if (objid > max_objid)
377 fd = open(lastid_path, O_RDWR | O_CREAT, 0700);
379 fprintf(stderr, "error: open \"%s\" failed: %s\n",
380 lastid_path, strerror(errno));
385 ret = write(fd, &max_objid, sizeof(__u64));
386 if (ret < sizeof(__u64)) {
397 int main(int argc, char **argv)
400 char *src_dir = NULL, *last_dir = NULL;
401 struct stat stat_buf;
402 char tmp_path[PATH_MAX];
403 char mount_path[PATH_MAX] = {0};
409 while ((c = getopt(argc, argv, "d:hv")) != EOF) {
414 last_dir = strrchr(src_dir, '/');
415 if (last_dir != strchr(src_dir, '/')) {
416 if (last_dir != NULL && (*(last_dir + 1) == '\0'))
419 fprintf(stdout, "\"lost+found\" directory path: %s\n",
428 fprintf(stderr, "%s: bad option '%c'\n",
437 last_dir = strrchr(src_dir, '/');
438 if (last_dir == NULL) {
439 /* Current directory */
440 strcpy(mount_path, src_dir);
441 strcat(mount_path, "/..");
443 strncpy(mount_path, src_dir, (int)(last_dir - src_dir));
446 /* Check if 'O' directory exists and create it if needed */
447 sprintf(tmp_path, "%s/O", mount_path);
448 if (stat(tmp_path, &stat_buf) != 0) {
449 retval = mkdir(tmp_path, 0700);
451 fprintf(stderr, "error: creating objects directory %s:"
452 " %s\n", tmp_path, strerror(errno));
456 memset(grp_info, 0, MAX_GROUPS * sizeof(struct obd_group_info));
458 retval = traverse_lost_found(src_dir, mount_path);
460 fprintf(stderr, "error: traversing lost+found looking for "
461 "orphan objects.\n");
465 retval = check_last_id(mount_path);
467 fprintf(stderr, "error: while checking/restoring LAST_ID.\n");