diff --git a/fs/ntfs/BUILD.gn b/fs/ntfs/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..db4ce44367a7c7b81a2d54ad5f62752d304d667e --- /dev/null +++ b/fs/ntfs/BUILD.gn @@ -0,0 +1,4 @@ +kernel_module("ntfs_driver") { + sources = [ "ntfs_kernel.c", "ntfs_super.c" ] + include_dirs = [ "//third_party/ntfs-3g/include" ] +} diff --git a/fs/ntfs/ntfs_inode.c b/fs/ntfs/ntfs_inode.c new file mode 100644 index 0000000000000000000000000000000000000000..d5781d57466a68825c8392bf3bfd5a500202605c --- /dev/null +++ b/fs/ntfs/ntfs_inode.c @@ -0,0 +1,36 @@ +#include +#include +#include "ntfs_kernel.h" + +static struct inode *ntfs_alloc_inode(struct super_block *sb) { + struct ntfs_inode_info *ni; + + ni = kmem_cache_alloc(ntfs_inode_cachep, GFP_NOFS); + if (!ni) + return NULL; + + memset(ni, 0, sizeof(*ni)); + return &ni->vfs_inode; +} + +static void ntfs_destroy_inode(struct inode *inode) { + struct ntfs_inode_info *ni = NTFS_I(inode); + kmem_cache_free(ntfs_inode_cachep, ni); +} + +static const struct address_space_operations ntfs_aops = { + .readpage = ntfs_readpage, + .writepage = ntfs_writepage, +}; + +static const struct inode_operations ntfs_file_inode_ops = { + .getattr = ntfs_getattr, + .setattr = ntfs_setattr, +}; + +static const struct file_operations ntfs_file_operations = { + .llseek = generic_file_llseek, + .read_iter = generic_file_read_iter, + .write_iter = generic_file_write_iter, + .mmap = generic_file_mmap, +}; diff --git a/fs/ntfs/ntfs_kernel.c b/fs/ntfs/ntfs_kernel.c new file mode 100644 index 0000000000000000000000000000000000000000..cf6d442d8d8f36bffeba04d567f228efbc3d23a4 --- /dev/null +++ b/fs/ntfs/ntfs_kernel.c @@ -0,0 +1,69 @@ +#include +#include +#include "ntfs_kernel.h" + +static int ntfs_fill_super(struct super_block *sb, void *data, int silent) { + struct ntfs_volume *vol; + struct buffer_head *bh; + int flags = SB_RDONLY; + + /* 获取设备路径 */ + const char *dev_name = data; + sb->s_bdev = blkdev_get_by_path(dev_name, FMODE_READ | FMODE_WRITE, NULL); + if (IS_ERR(sb->s_bdev)) { + printk(KERN_ERR "NTFS: Failed to open block device\n"); + return PTR_ERR(sb->s_bdev); + } + + /* 初始化NTFS卷 */ + vol = ntfs_mount(sb->s_bdev->bd_dev, NTFS_DEFAULT_OPTIONS); + if (IS_ERR(vol)) { + printk(KERN_ERR "NTFS: Volume mount failed\n"); + return PTR_ERR(vol); + } + + /* 检查脏标志 */ + if (vol->flags & VOLUME_IS_DIRTY) { + printk(KERN_WARNING "NTFS: Dirty volume detected, force readonly\n"); + flags |= MS_RDONLY; + } + + sb->s_flags = flags; + sb->s_fs_info = vol; + sb->s_op = &ntfs_super_ops; + return 0; +} + +static struct dentry *ntfs_mount(struct file_system_type *fs_type, + int flags, const char *dev_name, void *data) { + return mount_bdev(fs_type, flags, dev_name, data, ntfs_fill_super); +} + +static void ntfs_kill_sb(struct super_block *sb) { + struct ntfs_volume *vol = sb->s_fs_info; + if (vol) { + ntfs_umount(vol, 1); + sb->s_fs_info = NULL; + } + kill_block_super(sb); +} + +/* VFS注册结构体 */ +static struct file_system_type ntfs_fs_type = { + .owner = THIS_MODULE, + .name = "ntfs", + .mount = ntfs_mount, + .kill_sb = ntfs_kill_sb, + .fs_flags = FS_REQUIRES_DEV, +}; + +/* 内核模块初始化 */ +static int __init ntfs_init(void) { + int ret = register_filesystem(&ntfs_fs_type); + if (ret) + printk(KERN_ERR "NTFS registration failed: %d\n", ret); + return ret; +} + +LOS_MODULE_INIT(ntfs_init, LOS_INIT_LEVEL_KMOD_EXTENDED); + diff --git a/fs/ntfs/ntfs_super.c b/fs/ntfs/ntfs_super.c new file mode 100644 index 0000000000000000000000000000000000000000..5b221b6f93dbec4ccf5e6bd1c1e72e1cc711d380 --- /dev/null +++ b/fs/ntfs/ntfs_super.c @@ -0,0 +1,24 @@ +#include +#include "ntfs_kernel.h" + +static void ntfs_put_super(struct super_block *sb) { + /* 同步元数据 */ + sync_blockdev(sb->s_bdev); +} + +static int ntfs_statfs(struct dentry *dentry, struct kstatfs *buf) { + struct super_block *sb = dentry->d_sb; + struct ntfs_volume *vol = NTFS_SB(sb)->vol; + + buf->f_type = NTFS_SUPER_MAGIC; + buf->f_bsize = vol->cluster_size; + buf->f_blocks = vol->nr_clusters; + buf->f_bfree = vol->free_clusters; + buf->f_bavail = vol->free_clusters; + return 0; +} + +static struct super_operations ntfs_super_ops = { + .put_super = ntfs_put_super, + .statfs = ntfs_statfs, +};