Whamcloud - gitweb
ChangeLog, Makefile.in, e2fsck.h, journal.c, unix.c:
[tools/e2fsprogs.git] / e2fsck / journal.c
1 /*
2  * journal.c --- code for handling the "ext3" journal
3  */
4
5 #include <errno.h>
6
7 #include "e2fsck.h"
8
9 /*
10  * This is a list of directories to try.  The first element may get
11  * replaced by a mktemp'ed generated temp directory if possible.
12  */
13 static char *dirlist[] = { "/mnt", "/tmp", "/root", "/boot", 0 };
14
15 /*
16  * This function attempts to mount and unmount an ext3 filesystem,
17  * which is a cheap way to force the kernel to run the journal and
18  * handle the recovery for us.
19  */
20 int e2fsck_run_ext3_journal(const char *device)
21 {
22         int     ret = 0;
23         char    **cpp, *dir;
24         char    template[] = "/tmp/ext3.XXXXXX";
25         char    *tmpdir;
26
27         /*
28          * First try to make a temporary directory.  This may fail if
29          * the root partition is still mounted read-only.
30          */
31         tmpdir = mktemp(template);
32         if (tmpdir) {
33                 ret = mkdir(template, 0700);
34                 if (ret)
35                         tmpdir = 0;
36         }
37         if (tmpdir) {
38                 ret = mount(device, tmpdir, "ext3", 0xC0ED, NULL);
39                 if (ret) {
40                         ret = errno;
41                         rmdir(tmpdir);
42                         return (ret);
43                 }
44         } else {
45                 /*
46                  * OK, creating a temporary directory didn't work.
47                  * Let's try a list of possible temporary mountpoints.
48                  */
49                 for (cpp = dirlist; dir = *cpp; cpp++) {
50                         ret = mount(device, dir, "ext3", 0xC0ED, NULL);
51                         if (ret == 0)
52                                 break;
53                 }
54                 if (!dir)
55                         return errno;
56         }
57
58         /*
59          * Now that it mounted cleanly, the filesystem will have been
60          * recovered, so we can now unmount it.
61          */
62         ret = umount(device);
63         if (ret)
64                 return errno;
65         /*
66          * Remove the temporary directory, if it was created.
67          */
68         if (tmpdir)
69                 rmdir(tmpdir);
70         return 0;
71 }
72