I have a disk with some filesystem known by the Linux kernel. I need to get the physical block numbers for all blocks that are currently free on this filesystem. How can I do that?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Getting a free-block count:
In-kernel method:
Look at the
.statfssuperblock hook that filesystems implement. They return akstatfsobject which has af_bfreeparameter, a count of the free blocks. You should be able to call into this.From userspace:
The
dfcommand is the simplest (which of course causes the kernel to callstatfs– where else would it get the information? 🙂Edit: Your original question conveyed getting just the free block count.
Getting block locations:
Getting the location of every free block and not just the total number is impossible in a filesystem-independent way with the current structure of the kernel (and it will probably remain this way). The VFS doesn’t impose any common structure on how the filesystem maintains its bitmap; bitmaps are kept track of in the filesystem-dependent part of the superblock.
So, if you want a solution that works across all filesystems, you are out of luck. If you know what the filesystems might be and its a small number, you can look at userspace
fsckcode for each of those and add functions to write out a bitmap in a format you expect, or you can modify the.statfshooks to write out the bitmap.Your method to allocate a large file that allocates all remaining free blocks doesn’t sound like a very good idea at all, especially if there are journals involved. It also won’t work for an active filesystem with files being written and deleted as you do this.