【问题标题】:More explanation on `statfs64`关于 `statfs64` 的更多解释
【发布时间】:2019-12-04 16:29:45
【问题描述】:

根据文档,结构字段解释如下:

struct statfs {
__SWORD_TYPE f_type; /* type of file system (see below) */
__SWORD_TYPE f_bsize; /* optimal transfer block size */
fsblkcnt_t f_blocks; /* total data blocks in file system */
fsblkcnt_t f_bfree; /* free blocks in fs */
fsblkcnt_t f_bavail; /* free blocks available to
unprivileged user */
fsfilcnt_t f_files; /* total file nodes in file system */
fsfilcnt_t f_ffree; /* free file nodes in fs */
fsid_t f_fsid; /* file system id */
__SWORD_TYPE f_namelen; /* maximum length of filenames */
__SWORD_TYPE f_frsize; /* fragment size (since Linux 2.6) */
__SWORD_TYPE f_spare[5];
};

文件系统中的文件节点总数”是否意味着我们有多少现有文件?是否包含目录和链接?

fs 中的空闲文件节点”是什么意思?

什么是f_spare

在一些 Linux 分支中(例如,在 Android 中)我看到 f_spare 的大小为 4,并且定义了附加字段 f_flags。为f_flags 定义了哪些标志?

f_fsid 是唯一标识文件系统的随机数,还是它?

【问题讨论】:

  • 注意:一个文件可以有多个名称 - 硬链接。

标签: c++ linux system-calls


【解决方案1】:

“文件系统中的文件节点总数”是指我们有多少现有文件?是否包含目录和链接?

几乎。是的,它包括目录和软链接,但是两个文件可以共享同一个 inode。在这种情况下,它们是硬链接的并共享硬盘上的相同空间,但在文件系统中被视为不同的文件。举例说明:

% echo Hello > test1.txt
% ln test1.txt test2.txt
% ls -i test1.txt test2.txt
14946320 test1.txt  14946320 test2.txt

您将在文件名左侧看到的数字是索引节点(您将拥有与我的示例不同的数字)。如您所见,它们具有相同的 inode。如果您对一个文件进行更改,相同的更改将通过另一个文件可见。

“fs 中的空闲文件节点”是什么意思?

文件系统通常有一个可以跟踪的 inode 上限。实际类型 fsfilcnt_t 设置了一个限制(在我的系统上为 18446744073709551615),但它很可能更低。除非您以非常特殊的方式使用文件系统,否则此限制通常不是问题。

什么是 f_spare?在某些 Linux 分支中(例如,在 Android 中),我看到 f_spare 大小为 4,并且定义了附加字段 f_flags。

f_spare 只是填充结构本身的备用字节。填充字节保留供将来使用。如果将来将一个__fsword_t 的信息添加到结构中,他们将从f_spare 中删除一个备用__fsword_t。例如,我的系统只有 4 个备用 __fsword_ts(32 字节)。

为 f_flags 定义了哪些标志?

为您的系统定义的挂载标志可能不同,但我的man statfs64 页面显示这些:

   ST_MANDLOCK
          Mandatory locking is permitted on the filesystem (see fcntl(2)).

   ST_NOATIME
          Do not update access times; see mount(2).

   ST_NODEV
          Disallow access to device special files on this filesystem.

   ST_NODIRATIME
          Do not update directory access times; see mount(2).

   ST_NOEXEC
          Execution of programs is disallowed on this filesystem.

   ST_NOSUID
          The set-user-ID and set-group-ID bits are ignored by exec(3) for executable files on this filesystem

   ST_RDONLY
          This filesystem is mounted read-only.

   ST_RELATIME
          Update atime relative to mtime/ctime; see mount(2).

   ST_SYNCHRONOUS
          Writes are synched to the filesystem immediately (see the description of O_SYNC in open(2)).
   ST_MANDLOCK
          Mandatory locking is permitted on the filesystem (see fcntl(2)).

   ST_NOATIME
          Do not update access times; see mount(2).

   ST_NODEV
          Disallow access to device special files on this filesystem.

   ST_NODIRATIME
          Do not update directory access times; see mount(2).

   ST_NOEXEC
          Execution of programs is disallowed on this filesystem.

   ST_NOSUID
          The set-user-ID and set-group-ID bits are ignored by exec(3) for executable files on this filesystem

   ST_RDONLY
          This filesystem is mounted read-only.

   ST_RELATIME
          Update atime relative to mtime/ctime; see mount(2).

   ST_SYNCHRONOUS
          Writes are synched to the filesystem immediately (see the description of O_SYNC in open(2)).

f_fsid 是唯一标识文件系统的随机数,还是什么?

直接来自man statfs64 页面:“没有人知道 f_fsid 应该包含什么(但请参见下文)” 以及下文:

f_fsid 字段

Solaris、Irix 和 POSIX 有一个系统调用 statvfs(2),它返回一个包含无符号长 f_fsid 的结构 statvfs(定义在 中)。 Linux、SunOS、HP-UX、4.4BSD 有一个系统调用 statfs(),它返回一个包含 fsid_t f_fsid 的 struct statfs(定义在 中),其中 fsid_t 定义为 struct { int val[2]; }。 FreeBSD 也是如此,除了它使用包含文件。

一般的想法是 f_fsid 包含一些随机的东西,这样一对 (f_fsid,ino) 唯一地确定了一个文件。一些操作系统使用(变体)设备号,或设备号与文件系统类型相结合。一些操作系统限制仅将 f_fsid 字段提供给超级用户(对非特权用户将其归零),因为当 NFS 导出时,该字段用于文件系统的文件句柄中,并且提供它是一个安全问题。

在某些操作系统下,fsid 可以用作 sysfs(2) 系统调用的第二个参数。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 2010-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多