Possibly OT: no. of blocks allocated to a file

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Folks,

Not sure this is the appropriate forum for this question, but I'll ask
anyway (not sure that it isn't the appropriate forum either!). If I
should post this elsewhere, please do let me know, and sorry for the
trouble.

I'm running linux with an ext3 filesystem, and I wrote a small program
to stat a file and display a few things like the file size, the no. of
512 - byte blocks that was allocated to it, etc., and I noticed this:

1. The preferred filesystem I/O blocksize is 4096 bytes. (on my box).
2. The no. of 512-byte blocks allocated to a file corresponds to a
size in bytes that is the nearest multiple of 4096 greater than or
equal to the filesize + an extra 4096 bytes. For example, 4096 bytes
are allocated to a zero byte file, 8192 bytes for a file with any size
between 1 and 4096 bytes, and so on.

Question: Why should any file be allocated an extra 4096-byte block?

Here's the code, if you would like to know what I wrote :-):

============= begin code =========
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>

int main(int argc, char *argv[])
{
	int retval = 1;
	struct stat statbuf;

	if (argc == 2) {
		if ( !stat(argv[1], &statbuf) ) {
			printf("size: %d\n", (int) statbuf.st_size);
			printf("pref blksize: %d\n", (int) statbuf.st_blksize);
			printf("blocks: %d\n", (int) statbuf.st_blocks);
			printf("blocks * 512 = %d\n", (int) (statbuf.st_blocks * 512));

			retval = 0;
		} else {
			printf("%s\n", (char *) strerror(errno));
			retval = 1;
		}
	} else {
		printf ("usage: blocks <filename>\n");
		retval = 1;
	}

	return retval;
}
============= end code =================

Thanks a ton!
Karthik.


-- 
There are things known and things unknown, in between lie the Doors

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux