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 (c) 2009, 2010, Oracle and/or its affiliates. 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 parent_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",
258 if (xattr_len == -1 || xattr_len < sizeof(parent_fid))
260 * Its very much possible that we dont find fid
261 * on precreated files, LAST_ID
265 ff_group = le64_to_cpu(parent_fid.ff_seq);
266 if (ff_group >= FID_SEQ_OST_MAX) {
267 fprintf(stderr, "error: invalid group "LPU64" likely"
268 "indicates a corrupt xattr for file %s.\n",
269 ff_group, file_path);
272 ff_objid = le64_to_cpu(parent_fid.ff_objid);
274 /* might need to create the parent directories for
276 if (ll_sprintf(dest_path, PATH_MAX, "%s/O/"LPU64"/d"LPU64,
277 mount_path, ff_group, ff_objid % 32)) {
282 ret = mkdir_p(dest_path, mount_path, ff_group);
289 * Object ID needs to be verified against last_id.
290 * LAST_ID file may not be present in the group directory
291 * due to corruption. In case of any error tyr to recover
292 * as many objects as possible by setting last_id to ~0ULL.
294 if (last_id[ff_group] == 0) {
295 if (ll_sprintf(last_id_file, PATH_MAX,
296 "%s/O/"LPU64"/LAST_ID",
297 mount_path, ff_group)) {
302 tmp_last_id = read_last_id(last_id_file);
303 if (tmp_last_id == 0)
305 last_id[ff_group] = tmp_last_id;
308 if (ff_objid > last_id[ff_group]) {
309 fprintf(stderr, "error: file skipped because object ID "
310 "greater than LAST_ID\nFilename: %s\n"
311 "Group: "LPU64"\nObjectid: "LPU64"\n"
312 "LAST_ID: "LPU64, file_path, ff_group, ff_objid,
317 /* move file from lost+found to proper object
319 if (ll_sprintf(dest_path, PATH_MAX,
320 "%s/O/"LPU64"/d"LPU64"/"LPU64, mount_path,
321 ff_group, ff_objid % 32, ff_objid)) {
327 ret = stat(dest_path, &st);
334 "warning: stat for %s: %s\n",
335 dest_path, strerror(errno));
340 fprintf(stderr, "error: target object %s already "
341 "exists and will not be replaced.\n",dest_path);
345 if (rename(file_path, dest_path) < 0) {
346 fprintf(stderr, "error: rename failed for file %s: %s\n",
347 file_path, strerror(errno));
352 printf("Object %s restored.\n", dest_path);
363 * If LAST_ID file is not present in some group then restore it with the highest
364 * object ID found in that group. By the time we come here all possible objects
365 * have been restored.
367 static int check_last_id(const char *mount_path)
369 char lastid_path[PATH_MAX];
370 char dirname[PATH_MAX], subdirname[PATH_MAX];
371 DIR *groupdir, *subdir;
373 struct dirent *dirent;
379 for (group = 0; group < MAX_GROUPS; group++) {
382 if (ll_sprintf(dirname, PATH_MAX, "%s/O/"LPU64,
385 if (ll_sprintf(lastid_path, PATH_MAX, "%s/LAST_ID", dirname))
388 if (stat(lastid_path, &st) == 0)
391 groupdir = opendir(dirname);
392 if (groupdir == NULL) {
394 fprintf(stderr, "error: opening %s: %s\n",
395 dirname, strerror(errno));
399 while ((dirent = readdir(groupdir)) != NULL) {
400 if (!strcmp(dirent->d_name, ".") ||
401 !strcmp(dirent->d_name, ".."))
404 if (ll_sprintf(subdirname, PATH_MAX, "%s/%s",
405 dirname, dirent->d_name)) {
409 subdir = opendir(subdirname);
410 if (subdir == NULL) {
411 fprintf(stderr, "error: opening %s: %s\n",
412 subdirname, strerror(errno));
416 while ((dirent = readdir(subdir)) != NULL) {
420 if (!strcmp(dirent->d_name, ".") ||
421 !strcmp(dirent->d_name, ".."))
424 objid = strtoull(dirent->d_name, &end, 0);
425 if (end == dirent->d_name || *end != 0) {
426 fprintf(stderr, "error: unknown object"
427 "ID %s/%s\n", subdirname,
431 if (objid > max_objid)
438 fd = open(lastid_path, O_RDWR | O_CREAT, 0700);
440 fprintf(stderr, "error: open \"%s\" failed: %s\n",
441 lastid_path, strerror(errno));
445 max_objid = cpu_to_le64(max_objid);
446 ret = write(fd, &max_objid, sizeof(__u64));
447 if (ret < sizeof(__u64)) {
448 fprintf(stderr, "error: write \"%s\" failed: %s\n",
449 lastid_path, strerror(errno));
460 int main(int argc, char **argv)
463 struct stat stat_buf;
464 char src_dir[PATH_MAX] = "";
465 char mount_path[PATH_MAX];
466 char tmp_path[PATH_MAX];
472 while ((c = getopt(argc, argv, "d:hv")) != EOF) {
476 fprintf(stderr, "error: chdir to %s: %s\n",
477 optarg, strerror(errno));
480 if (getcwd(src_dir, PATH_MAX) == NULL) {
482 "error: getcwd of lost+found: %s\n",
487 fprintf(stderr, "error: chdir to \"..\": %s\n",
491 if (getcwd(mount_path, PATH_MAX) == NULL) {
493 "error: getcwd of mount point: %s\n",
497 if (!strcmp(src_dir, mount_path)) {
499 "error: root directory is detected\n");
502 fprintf(stdout, "\"lost+found\" directory path: %s\n",
511 fprintf(stderr, "%s: bad option '%c'\n",
520 /* Check if 'O' directory exists and create it if needed */
521 if (ll_sprintf(tmp_path, PATH_MAX, "%s/O", mount_path))
524 if (stat(tmp_path, &stat_buf) != 0) {
525 retval = mkdir(tmp_path, 0700);
527 fprintf(stderr, "error: creating objects directory %s:"
528 " %s\n", tmp_path, strerror(errno));
533 retval = traverse_lost_found(src_dir, mount_path);
535 fprintf(stderr, "error: traversing lost+found looking for "
536 "orphan objects.\n");
540 retval = check_last_id(mount_path);
542 fprintf(stderr, "error: while checking/restoring LAST_ID.\n");