e2fsck: use root dir for lost+found when really desperate

If we're totally unable to allocate a lost+found directory, ask the
user if he would like to dump orphaned files in the root directory.
Hopefully this enables the user to delete enough files so that a
subsequent run of e2fsck will make more progress.  Better to cram lost
files in the rootdir than the current behavior, which is to fail at
linking them in, thereby leaving them as lost files.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
diff --git a/e2fsck/pass3.c b/e2fsck/pass3.c
index 31131ab..e6142ad 100644
--- a/e2fsck/pass3.c
+++ b/e2fsck/pass3.c
@@ -450,6 +450,12 @@
 		goto skip_new_block;
 	}
 	retval = ext2fs_new_block2(fs, 0, ctx->block_found_map, &blk);
+	if (retval == EXT2_ET_BLOCK_ALLOC_FAIL &&
+	    fix_problem(ctx, PR_3_LPF_NO_SPACE, &pctx)) {
+		printf("Delete some files and re-run e2fsck.\n\n");
+		ctx->lost_and_found = EXT2_ROOT_INO;
+		return 0;
+	}
 	if (retval) {
 		pctx.errcode = retval;
 		fix_problem(ctx, PR_3_ERR_LPF_NEW_BLOCK, &pctx);
@@ -464,6 +470,12 @@
 	 */
 	retval = ext2fs_new_inode(fs, EXT2_ROOT_INO, 040700,
 				  ctx->inode_used_map, &ino);
+	if (retval == EXT2_ET_INODE_ALLOC_FAIL &&
+	    fix_problem(ctx, PR_3_LPF_NO_SPACE, &pctx)) {
+		printf("Delete some files and re-run e2fsck.\n\n");
+		ctx->lost_and_found = EXT2_ROOT_INO;
+		return 0;
+	}
 	if (retval) {
 		pctx.errcode = retval;
 		fix_problem(ctx, PR_3_ERR_LPF_NEW_INODE, &pctx);
diff --git a/e2fsck/problem.c b/e2fsck/problem.c
index 2c9386f..e68433b 100644
--- a/e2fsck/problem.c
+++ b/e2fsck/problem.c
@@ -1630,6 +1630,11 @@
 	  N_("/@l has inline data\n"),
 	  PROMPT_CLEAR, 0 },
 
+	/* Cannot allocate /lost+found. */
+	{ PR_3_LPF_NO_SPACE,
+	  N_("Cannot allocate space for /@l.\nPlace lost files in root directory instead"),
+	  PROMPT_NULL, 0 },
+
 	/* Pass 3A Directory Optimization	*/
 
 	/* Pass 3A: Optimizing directories */
diff --git a/e2fsck/problem.h b/e2fsck/problem.h
index 80ef4a2..496e873 100644
--- a/e2fsck/problem.h
+++ b/e2fsck/problem.h
@@ -973,6 +973,9 @@
 /* Lost+found has inline data */
 #define PR_3_LPF_INLINE_DATA		0x030018
 
+/* Cannot allocate lost+found */
+#define PR_3_LPF_NO_SPACE		0x030019
+
 /*
  * Pass 3a --- rehashing diretories
  */
diff --git a/tests/f_nospc_create_lnf/expect.1 b/tests/f_nospc_create_lnf/expect.1
new file mode 100644
index 0000000..fc20628
--- /dev/null
+++ b/tests/f_nospc_create_lnf/expect.1
@@ -0,0 +1,28 @@
+Pass 1: Checking inodes, blocks, and sizes
+Pass 2: Checking directory structure
+Pass 3: Checking directory connectivity
+/lost+found not found.  Create? yes
+
+Cannot allocate space for /lost+found.
+Place lost files in root directory instead? yes
+
+Delete some files and re-run e2fsck.
+
+Pass 3A: Optimizing directories
+Pass 4: Checking reference counts
+Unattached inode 125
+Connect to /lost+found? yes
+
+Inode 125 ref count is 2, should be 1.  Fix? yes
+
+Pass 5: Checking group summary information
+Free blocks count wrong for group #0 (496, counted=495).
+Fix? yes
+
+Free blocks count wrong (496, counted=495).
+Fix? yes
+
+
+test_filesys: ***** FILE SYSTEM WAS MODIFIED *****
+test_filesys: 128/128 files (0.0% non-contiguous), 17/512 blocks
+Exit status is 1
diff --git a/tests/f_nospc_create_lnf/expect.2 b/tests/f_nospc_create_lnf/expect.2
new file mode 100644
index 0000000..5a44649
--- /dev/null
+++ b/tests/f_nospc_create_lnf/expect.2
@@ -0,0 +1,25 @@
+Pass 1: Checking inodes, blocks, and sizes
+Pass 2: Checking directory structure
+Pass 3: Checking directory connectivity
+/lost+found not found.  Create? yes
+
+Cannot allocate space for /lost+found.
+Place lost files in root directory instead? yes
+
+Delete some files and re-run e2fsck.
+
+Pass 4: Checking reference counts
+Pass 5: Checking group summary information
+Block bitmap differences:  -9
+Fix? yes
+
+Free blocks count wrong for group #0 (494, counted=495).
+Fix? yes
+
+Free blocks count wrong (494, counted=495).
+Fix? yes
+
+
+test_filesys: ***** FILE SYSTEM WAS MODIFIED *****
+test_filesys: 128/128 files (0.0% non-contiguous), 17/512 blocks
+Exit status is 1
diff --git a/tests/f_nospc_create_lnf/image.gz b/tests/f_nospc_create_lnf/image.gz
new file mode 100644
index 0000000..dc71b61
--- /dev/null
+++ b/tests/f_nospc_create_lnf/image.gz
Binary files differ
diff --git a/tests/f_nospc_create_lnf/name b/tests/f_nospc_create_lnf/name
new file mode 100644
index 0000000..df6c932
--- /dev/null
+++ b/tests/f_nospc_create_lnf/name
@@ -0,0 +1 @@
+no space to create lost+found