ioctl failed: Operation not permitted - block layout of my files
<p>I want to view the contents of a file at block level.
Given a file, i want to know how many blocks it has and what is the size if each one.</p>

<p>my problem is when i run it i get this error: </p>

<pre><code>FIBMAP ioctl failed: Operation not permitted
</code></pre>

<p>Also when i'm comliling i get the following warning:</p>

<pre><code>warning: implicit declaration of function ‘ioctl’ [-Wimplicit-function-declaration]
  if (ioctl(fd, FIGETBSZ, &amp;blocksize)) {
</code></pre>

<p>I am using the following code.</p>

<p>Code:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;unistd.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;linux/fs.h&gt;
#include &lt;assert.h&gt;
#include &lt;linux/fs.h&gt;



int main(int argc, char **argv)
{

    int fd, i, block, blocksize, blkcnt;
    struct stat st;

    assert(argv[1] != NULL);

    fd = open(argv[1], O_RDONLY);
    if (fd &lt;= 0) {
        perror("error opening file");
        exit(EXIT_FAILURE);
    }

    if (ioctl(fd, FIGETBSZ, &amp;blocksize)) {
        perror("FIBMAP ioctl failed");
        exit(EXIT_FAILURE);
    }

    if (fstat(fd, &amp;st)) {
        perror("fstat error");
        exit(EXIT_FAILURE);
    }

    blkcnt = (st.st_size + blocksize - 1) / blocksize;

    for (i = 0; i &lt; blkcnt; i++) {
        block = i;
        if (ioctl(fd, FIBMAP, &amp;block)) {
            perror("FIBMAP ioctl failed");
        }
        printf("%3d %10d\n", i, block);
    }

    close(fd);
    return 0;
}
</code></pre>

<p>Can anyone please explain me what is the problem in this code.</p>