aboutsummaryrefslogtreecommitdiff
path: root/ubifs-utils/fsck.ubifs/handle_disconnected.c
diff options
context:
space:
mode:
Diffstat (limited to 'ubifs-utils/fsck.ubifs/handle_disconnected.c')
-rw-r--r--ubifs-utils/fsck.ubifs/handle_disconnected.c108
1 files changed, 108 insertions, 0 deletions
diff --git a/ubifs-utils/fsck.ubifs/handle_disconnected.c b/ubifs-utils/fsck.ubifs/handle_disconnected.c
index b9a380f..be62522 100644
--- a/ubifs-utils/fsck.ubifs/handle_disconnected.c
+++ b/ubifs-utils/fsck.ubifs/handle_disconnected.c
@@ -19,6 +19,7 @@
#include "fsck.ubifs.h"
#define LOST_FOUND_DIR_NAME "lost+found"
+#define MAX_REPEAT_NAME_RETRY_TIMES 10000000
/**
* check_and_create_lost_found - Check and create the lost+found directory.
@@ -87,3 +88,110 @@ free_root:
kfree(root_ui);
return err;
}
+
+static int handle_disonnected_file(struct ubifs_info *c,
+ struct scanned_file *file)
+{
+ int err = 0;
+
+ if (FSCK(c)->lost_and_found) {
+ unsigned int index = 0;
+ char file_name[UBIFS_MAX_NLEN + 1];
+ struct fscrypt_name nm;
+ struct ubifs_inode *ui = NULL, *lost_found_ui = NULL;
+
+ lost_found_ui = ubifs_lookup_by_inum(c, FSCK(c)->lost_and_found);
+ if (IS_ERR(lost_found_ui)) {
+ err = PTR_ERR(lost_found_ui);
+ ubifs_assert(c, err != -ENOENT);
+ return err;
+ }
+ ui = ubifs_lookup_by_inum(c, file->inum);
+ if (IS_ERR(ui)) {
+ err = PTR_ERR(ui);
+ ubifs_assert(c, err != -ENOENT);
+ goto free_lost_found_ui;
+ }
+
+ while (index < MAX_REPEAT_NAME_RETRY_TIMES) {
+ struct ubifs_inode *target_ui;
+
+ err = snprintf(file_name, sizeof(file_name),
+ "INO_%lu_%u", file->inum, index);
+ if (err < 0)
+ goto free_ui;
+ fname_name(&nm) = file_name;
+ fname_len(&nm) = strlen(file_name);
+ target_ui = ubifs_lookup(c, lost_found_ui, &nm);
+ if (IS_ERR(target_ui)) {
+ err = PTR_ERR(target_ui);
+ if (err == -ENOENT)
+ break;
+ goto free_ui;
+ }
+ kfree(target_ui);
+ index++;
+ }
+
+ if (err != -ENOENT) {
+ err = 0;
+ kfree(ui);
+ kfree(lost_found_ui);
+ log_out(c, "Too many duplicated names(%u) in lost+found for inum %lu",
+ index, file->inum);
+ goto delete_file;
+ }
+
+ /* Try to recover disconnected file into lost+found. */
+ err = ubifs_link_recovery(c, lost_found_ui, ui, &nm);
+ if (err && err == -ENOSPC) {
+ err = 0;
+ log_out(c, "No free space to recover disconnected file");
+ goto delete_file;
+ }
+ dbg_fsck("recover disconnected file %lu, in %s",
+ file->inum, c->dev_name);
+
+free_ui:
+ kfree(ui);
+free_lost_found_ui:
+ kfree(lost_found_ui);
+ return err;
+ }
+
+ log_out(c, "No valid lost+found");
+
+delete_file:
+ if (fix_problem(c, DISCONNECTED_FILE_CANNOT_BE_RECOVERED, file))
+ err = delete_file(c, file);
+ return err;
+}
+
+/**
+ * handle_disonnected_files - Handle disconnected files.
+ * @c: UBIFS file-system description object
+ *
+ * This function tries to recover disonnected files into lost+found directory.
+ * If there is no free space left to recover the disconnected files, fsck may
+ * delete the files to make filesystem be consistent. Returns zero in case of
+ * success, a negative error code in case of failure.
+ */
+int handle_disonnected_files(struct ubifs_info *c)
+{
+ int err, ret = 0;
+ struct scanned_file *file;
+
+ while (!list_empty(&FSCK(c)->disconnected_files)) {
+ file = list_entry(FSCK(c)->disconnected_files.next,
+ struct scanned_file, list);
+
+ list_del(&file->list);
+ err = handle_disonnected_file(c, file);
+ if (err)
+ ret = ret ? ret : err;
+ destroy_file_content(c, file);
+ kfree(file);
+ }
+
+ return ret;
+}