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 static 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 static int _ll_sprintf(char *buf, size_t size, const char *func, int line,
82 const char *format, ...)
88 rc = vsnprintf(buf, size, format, ap);
89 if (!(rc > -1 && rc < size)) {
91 "error: %s:%d: path \"", func, line);
92 vfprintf(stderr, format, ap);
94 fprintf(stderr, "\" is too long\n");
101 #define ll_sprintf(buf, size, format, ...) \
102 _ll_sprintf(buf, size, __FUNCTION__, __LINE__, format, ## __VA_ARGS__)
104 static int mkdir_p(const char *dest_path, const char *mount, __u64 ff_group)
106 struct stat stat_buf;
109 char tmp_path[PATH_MAX];
111 if (stat(dest_path, &stat_buf) == 0)
114 if (grp_info[ff_group].dir_exists == 0) {
115 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O/"LPU64,
119 if (stat(tmp_path, &stat_buf) != 0) {
120 retval = mkdir(tmp_path, mode);
122 fprintf(stderr, "error: creating directory %s: "
123 "%s\n", tmp_path, strerror(errno));
126 grp_info[ff_group].dir_exists = 1;
130 retval = mkdir(dest_path, mode);
132 fprintf(stderr, "error: creating directory %s: "
133 "%s\n", dest_path, strerror(errno));
140 /* This is returning 0 for an error */
141 static __u64 read_last_id(char *file_path)
147 fd = open(file_path, O_RDONLY);
150 fprintf(stderr, "error: opening %s: %s\n",
151 file_path, strerror(errno));
155 count = read(fd, &last_id, sizeof(last_id));
157 fprintf(stderr, "error: reading file %s: %s\n", file_path,
162 if (count != sizeof(last_id)) {
163 fprintf(stderr, "error: Could not read full last_id from %s\n",
170 return le64_to_cpu(last_id);
173 static unsigned filetype_dir_table[] = {
182 #if defined(DT_DOOR) && defined(S_IFDOOR)
187 static int traverse_lost_found(char *src_dir, const char *mount_path)
190 struct filter_fid trusted_fid;
191 struct dirent64 *dirent;
192 __u64 ff_group, ff_objid;
194 char dest_path[PATH_MAX];
195 char last_id_file[PATH_MAX];
196 __u64 last_id[MAX_GROUPS] = {0};
199 int obj_exists, xattr_len;
200 int len, ret = 0, error = 0;
202 len = strlen(src_dir);
204 dir_ptr = opendir(src_dir);
206 fprintf(stderr, "error: opening directory: %s\n",
211 while ((dirent = readdir64(dir_ptr)) != NULL) {
212 if (!strcmp(dirent->d_name, ".") ||
213 !strcmp(dirent->d_name, ".."))
217 if ((len + strlen(dirent->d_name) + 2) > PATH_MAX) {
218 fprintf(stderr, "error: %s/%s: path too long\n",
219 src_dir, dirent->d_name);
222 strcat(src_dir, "/");
223 strcat(src_dir, dirent->d_name);
225 if (dirent->d_type == DT_UNKNOWN) {
226 ret = stat(src_dir, &st);
229 "error: stating %s: %s\n",
230 src_dir, strerror(errno));
233 dirent->d_type = filetype_dir_table[st.st_mode &
235 if (dirent->d_type == DT_UNKNOWN) {
237 "error: %s of unknown type 0%o\n",
238 src_dir, st.st_mode);
243 switch(dirent->d_type) {
245 ret = traverse_lost_found(src_dir, mount_path);
254 xattr_len = getxattr(file_path, "trusted.fid",
255 (void *)&trusted_fid,
256 sizeof(trusted_fid));
258 if (xattr_len == -1 || xattr_len < sizeof(trusted_fid))
260 * Its very much possible that we dont find fid
261 * on precreated files, LAST_ID
265 ff_group = le64_to_cpu(trusted_fid.ff_group);
266 ff_objid = le64_to_cpu(trusted_fid.ff_objid);
268 if (ff_group >= MAX_GROUPS) {
269 fprintf(stderr, "error: invalid group "LPU64" likely"
270 "indicates a corrupt xattr for file %s.\n",
271 ff_group, file_path);
275 /* might need to create the parent directories for
277 if (ll_sprintf(dest_path, PATH_MAX, "%s/O/"LPU64"/d"LPU64,
278 mount_path, ff_group, ff_objid % 32)) {
283 ret = mkdir_p(dest_path, mount_path, ff_group);
290 * Object ID needs to be verified against last_id.
291 * LAST_ID file may not be present in the group directory
292 * due to corruption. In case of any error tyr to recover
293 * as many objects as possible by setting last_id to ~0ULL.
295 if (last_id[ff_group] == 0) {
296 if (ll_sprintf(last_id_file, PATH_MAX,
297 "%s/O/"LPU64"/LAST_ID",
298 mount_path, ff_group)) {
303 tmp_last_id = read_last_id(last_id_file);
304 if (tmp_last_id == 0)
306 last_id[ff_group] = tmp_last_id;
309 if (ff_objid > last_id[ff_group]) {
310 fprintf(stderr, "error: file skipped because object ID "
311 "greater than LAST_ID\nFilename: %s\n"
312 "Group: "LPU64"\nObjectid: "LPU64"\n"
313 "LAST_ID: "LPU64, file_path, ff_group, ff_objid,
318 /* move file from lost+found to proper object
320 if (ll_sprintf(dest_path, PATH_MAX,
321 "%s/O/"LPU64"/d"LPU64"/"LPU64, mount_path,
322 ff_group, ff_objid % 32, ff_objid)) {
328 ret = stat(dest_path, &st);
335 "warning: stat for %s: %s\n",
336 dest_path, strerror(errno));
341 fprintf(stderr, "error: target object %s already "
342 "exists and will not be replaced.\n",dest_path);
346 if (rename(file_path, dest_path) < 0) {
347 fprintf(stderr, "error: rename failed for file %s: %s\n",
348 file_path, strerror(errno));
353 printf("Object %s restored.\n", dest_path);
364 * If LAST_ID file is not present in some group then restore it with the highest
365 * object ID found in that group. By the time we come here all possible objects
366 * have been restored.
368 static int check_last_id(const char *mount_path)
370 char lastid_path[PATH_MAX];
371 char dirname[PATH_MAX], subdirname[PATH_MAX];
372 DIR *groupdir, *subdir;
374 struct dirent *dirent;
380 for (group = 0; group < MAX_GROUPS; group++) {
383 if (ll_sprintf(dirname, PATH_MAX, "%s/O/"LPU64,
386 if (ll_sprintf(lastid_path, PATH_MAX, "%s/LAST_ID", dirname))
389 if (stat(lastid_path, &st) == 0)
392 groupdir = opendir(dirname);
393 if (groupdir == NULL) {
395 fprintf(stderr, "error: opening %s: %s\n",
396 dirname, strerror(errno));
400 while ((dirent = readdir(groupdir)) != NULL) {
401 if (!strcmp(dirent->d_name, ".") ||
402 !strcmp(dirent->d_name, ".."))
405 if (ll_sprintf(subdirname, PATH_MAX, "%s/%s",
406 dirname, dirent->d_name)) {
410 subdir = opendir(subdirname);
411 if (subdir == NULL) {
412 fprintf(stderr, "error: opening %s: %s\n",
413 subdirname, strerror(errno));
417 while ((dirent = readdir(subdir)) != NULL) {
421 if (!strcmp(dirent->d_name, ".") ||
422 !strcmp(dirent->d_name, ".."))
425 objid = strtoull(dirent->d_name, &end, 0);
426 if (end == dirent->d_name || *end != 0) {
427 fprintf(stderr, "error: unknown object"
428 "ID %s/%s\n", subdirname,
432 if (objid > max_objid)
439 fd = open(lastid_path, O_RDWR | O_CREAT, 0700);
441 fprintf(stderr, "error: open \"%s\" failed: %s\n",
442 lastid_path, strerror(errno));
446 max_objid = cpu_to_le64(max_objid);
447 ret = write(fd, &max_objid, sizeof(__u64));
448 if (ret < sizeof(__u64)) {
449 fprintf(stderr, "error: write \"%s\" failed: %s\n",
450 lastid_path, strerror(errno));
461 int main(int argc, char **argv)
464 struct stat stat_buf;
465 char src_dir[PATH_MAX] = "";
466 char mount_path[PATH_MAX];
467 char tmp_path[PATH_MAX];
473 while ((c = getopt(argc, argv, "d:hv")) != EOF) {
477 fprintf(stderr, "error: chdir to %s: %s\n",
478 optarg, strerror(errno));
481 if (getcwd(src_dir, PATH_MAX) == NULL) {
483 "error: getcwd of lost+found: %s\n",
488 fprintf(stderr, "error: chdir to \"..\": %s\n",
492 if (getcwd(mount_path, PATH_MAX) == NULL) {
494 "error: getcwd of mount point: %s\n",
498 if (!strcmp(src_dir, mount_path)) {
500 "error: root directory is detected\n");
503 fprintf(stdout, "\"lost+found\" directory path: %s\n",
512 fprintf(stderr, "%s: bad option '%c'\n",
521 /* Check if 'O' directory exists and create it if needed */
522 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O", mount_path))
525 if (stat(tmp_path, &stat_buf) != 0) {
526 retval = mkdir(tmp_path, 0700);
528 fprintf(stderr, "error: creating objects directory %s:"
529 " %s\n", tmp_path, strerror(errno));
534 retval = traverse_lost_found(src_dir, mount_path);
536 fprintf(stderr, "error: traversing lost+found looking for "
537 "orphan objects.\n");
541 retval = check_last_id(mount_path);
543 fprintf(stderr, "error: while checking/restoring LAST_ID.\n");