~aleteoryx/muditaos

607b4299d452ca0491da52a4a5402577eebe7d93 — Lucjan Bryndza 4 years ago 1a66e73
[EGD-7883] Close all opened files when umount

Force closing all opened files when unmount
filesystem is called.

Signed-off-by: Lucjan Bryndza <lucjan.bryndza@mudita.com>
M module-vfs/drivers/include/purefs/fs/drivers/file_handle_ext4.hpp => module-vfs/drivers/include/purefs/fs/drivers/file_handle_ext4.hpp +1 -1
@@ 19,7 19,7 @@ namespace purefs::fs::drivers
        {
            return &m_file;
        }
        [[nodiscard]] auto open_path() const noexcept -> const std::string &
        [[nodiscard]] auto open_path() const noexcept -> std::string override
        {
            return m_path;
        }

M module-vfs/drivers/include/purefs/fs/drivers/file_handle_littlefs.hpp => module-vfs/drivers/include/purefs/fs/drivers/file_handle_littlefs.hpp +1 -1
@@ 19,7 19,7 @@ namespace purefs::fs::drivers
        {
            return &file;
        }
        [[nodiscard]] auto open_path() const noexcept -> const std::string &
        [[nodiscard]] auto open_path() const noexcept -> std::string override
        {
            return m_path;
        }

M module-vfs/drivers/include/purefs/fs/drivers/file_handle_vfat.hpp => module-vfs/drivers/include/purefs/fs/drivers/file_handle_vfat.hpp +1 -1
@@ 20,7 20,7 @@ namespace purefs::fs::drivers
        {
            return &m_fil;
        }
        auto open_path() const noexcept -> const std::string &
        auto open_path() const noexcept -> std::string override
        {
            return m_path;
        }

M module-vfs/drivers/src/purefs/fs/filesystem_ext4.cpp => module-vfs/drivers/src/purefs/fs/filesystem_ext4.cpp +3 -2
@@ 1,6 1,7 @@
// Copyright (c) 2017-2021, Mudita Sp. z.o.o. All rights reserved.
// For licensing, see https://github.com/mudita/MuditaOS/LICENSE.md

#include <memory>
#include <purefs/fs/drivers/filesystem_ext4.hpp>
#include <purefs/fs/drivers/mount_point_ext4.hpp>
#include <purefs/blkdev/disk_manager.hpp>


@@ 45,7 46,7 @@ namespace purefs::fs::drivers
        template <typename T>
        auto invoke_efs(filesystem_ext4::fsmount mnt, T efs_fun, std::string_view oldpath, std::string_view newpath)
        {
            auto mntp = std::static_pointer_cast<mount_point_ext4>(mnt);
            auto mntp = std::dynamic_pointer_cast<mount_point_ext4>(mnt);
            if (!mntp) {
                LOG_ERROR("Non ext4 mount point");
                return -EBADF;


@@ 59,7 60,7 @@ namespace purefs::fs::drivers
        template <typename T, typename... Args>
        auto invoke_efs(filesystem_ext4::fsmount fmnt, T efs_fun, std::string_view path, Args &&...args)
        {
            auto mntp = std::static_pointer_cast<mount_point_ext4>(fmnt);
            auto mntp = std::dynamic_pointer_cast<mount_point_ext4>(fmnt);
            if (!mntp) {
                LOG_ERROR("Non ext4 mount point");
                return -EBADF;

M module-vfs/include/user/purefs/fs/file_handle.hpp => module-vfs/include/user/purefs/fs/file_handle.hpp +5 -0
@@ 4,6 4,7 @@
#pragma once

#include <memory>
#include <string>

namespace purefs::fs::internal
{


@@ 33,6 34,10 @@ namespace purefs::fs::internal
        {
            return m_mount_point.lock();
        }
        [[nodiscard]] virtual auto open_path() const noexcept -> std::string
        {
            return {};
        }

      private:
        const std::weak_ptr<mount_point> m_mount_point;

M module-vfs/include/user/purefs/fs/filesystem.hpp => module-vfs/include/user/purefs/fs/filesystem.hpp +2 -0
@@ 282,6 282,8 @@ namespace purefs::fs
                }
            }
        }
        auto cleanup_opened_files(std::string_view mount_point) -> void;

      private:
        std::weak_ptr<blkdev::disk_manager> m_diskmm;
        std::unordered_map<std::string, std::shared_ptr<filesystem_operations>> m_fstypes;

M module-vfs/include/user/purefs/fs/handle_mapper.hpp => module-vfs/include/user/purefs/fs/handle_mapper.hpp +1 -0
@@ 21,6 21,7 @@ namespace purefs::fs::internal
        void remove(std::size_t index)
        {
            unused.push_back(index);
            data[index] = {};
        }
        bool exists(std::size_t index) const
        {

M module-vfs/src/purefs/fs/filesystem.cpp => module-vfs/src/purefs/fs/filesystem.cpp +33 -0
@@ 19,6 19,19 @@ namespace purefs::fs
    {
        constexpr std::pair<short, std::string_view> part_types_to_vfs[] = {
            {0x0b, "vfat"}, {0x9e, "littlefs"}, {0x83, "ext4"}};

        auto compare_mount_points(std::string_view path1, std::string_view path2)
        {
            std::string spath1{path1};
            std::string spath2{path2};
            if (spath1.back() == '/') {
                spath1.pop_back();
            }
            if (spath2.back() == '/') {
                spath2.pop_back();
            }
            return spath1 == spath2;
        }
    }
    filesystem::filesystem(std::shared_ptr<blkdev::disk_manager> diskmm)
        : m_diskmm(diskmm), m_lock(std::make_unique<cpp_freertos::MutexRecursive>()),


@@ 159,6 172,9 @@ namespace purefs::fs
            LOG_ERROR("Unable to lock filesystem operation");
            return -EIO;
        }

        cleanup_opened_files(mount_point);

        const auto diskh    = mnti->second->disk();
        const auto umnt_ret = fsops->umount(mnti->second);
        if (umnt_ret) {


@@ 315,4 331,21 @@ namespace purefs::fs
        return std::make_shared<inotify>(svc, m_notifier);
    }

    auto filesystem::cleanup_opened_files(std::string_view mount_point) -> void
    {
        LOG_INFO("Closing opened files on mntpoint: %s before umount.", std::string(mount_point).c_str());
        for (auto fd = 0U; fd < m_fds.size(); ++fd) {
            const auto filp = m_fds[fd];
            if (!filp)
                continue;
            const auto mp = filp->mntpoint();
            if (!mp)
                continue;
            if (compare_mount_points(mp->mount_path(), mount_point)) {
                const auto err = close(fd + first_file_descriptor);
                LOG_WARN("Emergency closing: %s result: %i.", filp->open_path().c_str(), err);
            }
        }
    }

} // namespace purefs::fs