使用块特殊文件/设备来实现文件系统

发布于 2024-09-18 04:04:59 字数 717 浏览 9 评论 0 原文

我已经使用 FUSE 实现了基本文件系统,并实现了所有可预见的 POSIX 功能[自然地,我甚至还没有简介;)]。目前我可以在常规文件 (st_mode & S_IFREG) 上运行文件系统,但开发的下一步是将其托管在实际的块设备上。按原样运行我的代码,在调用 st_size设备上的 >fstat。当然,我不希望问题就此停止:

  • 与常规文件相比,在块设备上操作需要进行哪些更改?
  • 关于性能、限制、特殊功能等,我需要考虑哪些特殊因素?
  • 是否有处理块特殊文件的教程和参考?谷歌搜索并没有什么用处。我只有背景知识(讽刺的是,在我黑暗的过去,来自 MSDN)和联机帮助页中的一些很少的信息。

Update0

  • 我已经指出了“常规文件”的含义。
  • 我不想专注于获取设备大小,我想要关于常规文件和设备文件在性能和使用方面的差异的一般准则。

I've implemented a basic filesystem using FUSE, with all foreseeable POSIX functionality implemented [naturally I haven't even profiled yet ;)]. Currently I'm able to run the filesystem on a regular file (st_mode & S_IFREG), but the next step in development is to host it on an actual block device. Running my code as is, immediately fails on reading st_size after calling fstat on the device. Of course I don't expect the problems to stop there so:

  • What changes are required to operate on block devices as opposed to regular files?
  • What are some special considerations I need to make with regard to performance, limitations, special features and the like?
  • Are there any tutorials and references with dealing with block special files? Googling has turned up very little useful; I only have background knowledge (ironically from MSDN in my dark past) and some scanty information in the manpages.

Update0

  • I've pointed out what I mean by "regular file".
  • I don't want to concentrate on getting the device size, I want general guidelines for differences between regular files and device files with respect to performance and usage.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

苯莒 2024-09-25 04:04:59

目前我可以运行
文件系统位于常规文件上,但是
下一步的发展是
将其托管在实际的块设备上

我不完全理解您的意思 - 我假设您是说“您当前将文件系统数据保存到正常安装的文件系统上的纯文件中 - 但现在希望使用原始块设备您的数据存储”。

如果是这样 - 已经这样做了几次 - 我建议如下:

  • 永远不要为您的文件系统使用“实际”块设备。始终使用分区。有几种很少使用的分区类型,您可以使用它们来表示这样的文件系统可能是您的文件系统类型,并且如果是这样的话,您的文件系统可以检查并挂载它。因此,您永远不会在诸如“/dev/sdb”之类的东西上运行,而是将数据存储在诸如/dev/sdb1之类的东西上,并为其分配某种分区类型。这具有明显的优点,例如允许您的文件系统与另一个文件系统共存于单个物理磁盘上,等等。
  • 如果您要在文件系统中实现任何缓存(如 Linux 中的页面缓存),请将所有 I/O 操作到具有 O_DIRECT 的块设备。这要求您传递页对齐的内存来执行所有 I/O,并要求请求是扇区/块对齐的 - 但会删除数据副本,否则当数据从块设备移动到页缓存时需要该数据副本,然后从页面缓存到用户空间[文件系统]读取器。

fstat“失败”是什么意思?这是一个试图确定块设备长度的 fstat?您收到错误吗?它是什么?

Currently I'm able to run the
filesystem on a regularly file, but
the next step in development is to
host it on an actual block device

I don't completely understand what you mean - I assume you are saying that "you currently save your filesystem data to a plain file on a normally mounted filesystem - but now wish to use a raw block device for your data storage".

If so - having done this a few times - I'd advise the following:

  • Never use an "actual" block device for you filesystem. Always use a partition. There are several rarely-used partition-types that you can use to denote that such a filesystem may be your filesystem type, and that your filesystem can check and mount it if it is such. Thus, you will never be running on something like "/dev/sdb", but rather you will store you data on one such as /dev/sdb1, and assign it some partition type. This has obvious advantages, like allowing your filesystem to be co-resident on a single phyiscal disk as another, etc.
  • If you are implementing any caching in your filesystem (like Linux does with the Page Cache), do all I/Os to the block devices with O_DIRECT. This requires you to pass page-alligned memory to do all I/O, and requires that the requests be sector/block aligned - but will remove a data copy which would otherwise be required when data is moved from the block device to the page cache, then from the page-cache to your user-space [filesystem] reader.

What do you mean that the fstat "fails"? This is an fstat trying to determing the length of the block device? Do you receive an error? What is it?

も星光 2024-09-25 04:04:59

块设备的行为与文件非常相似 - 像 dd 这样的工具可以对它们进行操作,而无需任何特殊处理。不过,fstat 返回有关特殊文件节点的信息,而不是它引用的 blockdev。您可能想使用 BLKGETSIZE64 ioctl 来读取大小。

不过,没有特别的理由在原始设备上使用分区 - blockdev 就是 blockdev。假设您的工作负载不会产生重复访问,O_DIRECT 也很好。不过,不要将它与确保文件系统的持久性和原子性的真正协议(fsync、障碍等)混淆。

block devices behave very much like files - tools like dd can operate on them without any special handling. fstat, though, returns information about the special-file node, not the blockdev it refers to. you probably want to use the BLKGETSIZE64 ioctl to read the size.

there's no particular reason to use a partition over a raw device, though - a blockdev is a blockdev. O_DIRECT is good, as well, assuming your workload won't generate repeated accesses. don't confuse it with a real protocol for ensuring the permanence and atomicity of your filesystem, though (fsync, barriers, etc).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文