linux fallocate FALLOC_FL_KEEP_SIZE 在关闭文件后如何影响文件?
How does linux fallocate FALLOC_FL_KEEP_SIZE affect a file after it is closed?
linux调用
fallocate(fd, FALLOC_FL_KEEP_SIZE, offset, len);
可以用来为文件末尾后的文件预分配space而不增加文件长度。 (对吗?)
问题:当 fd 关闭时,这个磁盘 space 会发生什么?额外的 space 是否已释放,还是与文件保持关联?
如果它仍然与文件相关联,我该如何释放它 space?截断(或 open() 和 ftruncate())是否释放 space?
Question: what happens to this disk space when fd is closed? Is the extra space released, or does it remain associated with the file?
没有任何反应。分配的space与fd无关。
If it remains associated with the file, how do I free that space? Does truncate (or open() and ftruncate()) free the space?
truncate() 是回收 space 的主要方法。另一种方法是删除文件。您也可以尝试在文件的“过度分配”部分使用 FALLOC_FL_PUNCH_HOLE 和 FALLOC_FL_KEEP_SIZE 调用 fallocate(),尽管这看起来很乱并且可能会留下工件。
在幕后,使用 FALLOC_FL_KEEP_SIZE 扩展文件将在文件系统中分配块,但将它们标记为未初始化。并非所有文件系统都支持这一点。到目前为止,这与不带保持大小标志的调用 fallocate() 相同。与 keep size 标志的不同之处在于它会保持 inode 中的 size 字段(独立于块分配)不变。所有这些都发生在磁盘上;在 shell 你可以使用
ls -s
或
stat
观察真正的分配块计数,独立于 process/fd 用于进行 fallocate() 调用的任何一个。
linux调用
fallocate(fd, FALLOC_FL_KEEP_SIZE, offset, len);
可以用来为文件末尾后的文件预分配space而不增加文件长度。 (对吗?)
问题:当 fd 关闭时,这个磁盘 space 会发生什么?额外的 space 是否已释放,还是与文件保持关联?
如果它仍然与文件相关联,我该如何释放它 space?截断(或 open() 和 ftruncate())是否释放 space?
Question: what happens to this disk space when fd is closed? Is the extra space released, or does it remain associated with the file?
没有任何反应。分配的space与fd无关。
If it remains associated with the file, how do I free that space? Does truncate (or open() and ftruncate()) free the space?
truncate() 是回收 space 的主要方法。另一种方法是删除文件。您也可以尝试在文件的“过度分配”部分使用 FALLOC_FL_PUNCH_HOLE 和 FALLOC_FL_KEEP_SIZE 调用 fallocate(),尽管这看起来很乱并且可能会留下工件。
在幕后,使用 FALLOC_FL_KEEP_SIZE 扩展文件将在文件系统中分配块,但将它们标记为未初始化。并非所有文件系统都支持这一点。到目前为止,这与不带保持大小标志的调用 fallocate() 相同。与 keep size 标志的不同之处在于它会保持 inode 中的 size 字段(独立于块分配)不变。所有这些都发生在磁盘上;在 shell 你可以使用
ls -s
或
stat
观察真正的分配块计数,独立于 process/fd 用于进行 fallocate() 调用的任何一个。