+
+/*
+ * Check to see if a filesystem is in /proc/filesystems.
+ * Returns 1 if found, 0 if not
+ */
+int fs_proc_check(const char *fs_name)
+{
+ FILE *f;
+ char buf[80], *cp, *t;
+
+ f = fopen("/proc/filesystems", "r");
+ if (!f)
+ return (0);
+ while (!feof(f)) {
+ if (!fgets(buf, sizeof(buf), f))
+ break;
+ cp = buf;
+ if (!isspace(*cp)) {
+ while (*cp && !isspace(*cp))
+ cp++;
+ }
+ while (*cp && isspace(*cp))
+ cp++;
+ if ((t = strchr(cp, '\n')) != NULL)
+ *t = 0;
+ if ((t = strchr(cp, '\t')) != NULL)
+ *t = 0;
+ if ((t = strchr(cp, ' ')) != NULL)
+ *t = 0;
+ if (!strcmp(fs_name, cp)) {
+ fclose(f);
+ return (1);
+ }
+ }
+ fclose(f);
+ return (0);
+}
+
+/*
+ * Check to see if a filesystem is available as a module
+ * Returns 1 if found, 0 if not
+ */
+int check_for_modules(const char *fs_name)
+{
+#ifdef __linux__
+ struct utsname uts;
+ FILE *f;
+ char buf[1024], *cp, *t;
+ int i;
+
+ if (uname(&uts))
+ return (0);
+ snprintf(buf, sizeof(buf), "/lib/modules/%s/modules.dep", uts.release);
+
+ f = fopen(buf, "r");
+ if (!f)
+ return (0);
+ while (!feof(f)) {
+ if (!fgets(buf, sizeof(buf), f))
+ break;
+ if ((cp = strchr(buf, ':')) != NULL)
+ *cp = 0;
+ else
+ continue;
+ if ((cp = strrchr(buf, '/')) != NULL)
+ cp++;
+ else
+ cp = buf;
+ i = strlen(cp);
+ if (i > 3) {
+ t = cp + i - 3;
+ if (!strcmp(t, ".ko"))
+ *t = 0;
+ }
+ if (!strcmp(cp, fs_name)) {
+ fclose(f);
+ return (1);
+ }
+ }
+ fclose(f);
+#endif /* __linux__ */
+ return (0);
+}
+
+/*
+ * Helper function that does the right thing if write returns a
+ * partial write, or an EGAIN/EINTR error.
+ */
+int write_all(int fd, char *buf, size_t count)
+{
+ ssize_t ret;
+ int c = 0;
+
+ while (count > 0) {
+ ret = write(fd, buf, count);
+ if (ret < 0) {
+ if ((errno == EAGAIN) || (errno == EINTR))
+ continue;
+ return -1;
+ }
+ count -= ret;
+ buf += ret;
+ c += ret;
+ }
+ return c;
+}
+
+void dump_mmp_msg(struct mmp_struct *mmp, const char *msg)
+{
+
+ if (msg)
+ printf("MMP check failed: %s\n", msg);
+ if (mmp) {
+ time_t t = mmp->mmp_time;
+
+ printf("MMP error info: last update: %s node: %s device: %s\n",
+ ctime(&t), mmp->mmp_nodename, mmp->mmp_bdevname);
+ }
+}
+
+errcode_t e2fsck_mmp_update(ext2_filsys fs)
+{
+ errcode_t retval;
+
+ retval = ext2fs_mmp_update(fs);
+ if (retval == EXT2_ET_MMP_CHANGE_ABORT)
+ dump_mmp_msg(fs->mmp_cmp,
+ _("UNEXPECTED INCONSISTENCY: the filesystem is "
+ "being modified while fsck is running.\n"));
+
+ return retval;
+}
+
+void e2fsck_set_bitmap_type(ext2_filsys fs, unsigned int default_type,
+ const char *profile_name, unsigned int *old_type)
+{
+ unsigned type;
+ errcode_t retval;
+
+ if (old_type)
+ *old_type = fs->default_bitmap_type;
+ profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
+ profile_name, 0, default_type, &type);
+ profile_get_uint(e2fsck_global_ctx->profile, "bitmaps",
+ "all", 0, type, &type);
+ fs->default_bitmap_type = type ? type : default_type;
+}
+
+errcode_t e2fsck_allocate_inode_bitmap(ext2_filsys fs, const char *descr,
+ int deftype,
+ const char *name,
+ ext2fs_inode_bitmap *ret)
+{
+ errcode_t retval;
+ unsigned int save_type;
+
+ e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
+ retval = ext2fs_allocate_inode_bitmap(fs, descr, ret);
+ fs->default_bitmap_type = save_type;
+ return retval;
+}
+
+errcode_t e2fsck_allocate_block_bitmap(ext2_filsys fs, const char *descr,
+ int deftype,
+ const char *name,
+ ext2fs_block_bitmap *ret)
+{
+ errcode_t retval;
+ unsigned int save_type;
+
+ e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
+ retval = ext2fs_allocate_block_bitmap(fs, descr, ret);
+ fs->default_bitmap_type = save_type;
+ return retval;
+}
+
+errcode_t e2fsck_allocate_subcluster_bitmap(ext2_filsys fs, const char *descr,
+ int deftype,
+ const char *name,
+ ext2fs_block_bitmap *ret)
+{
+ errcode_t retval;
+ unsigned int save_type;
+
+ e2fsck_set_bitmap_type(fs, deftype, name, &save_type);
+ retval = ext2fs_allocate_subcluster_bitmap(fs, descr, ret);
+ fs->default_bitmap_type = save_type;
+ return retval;
+}