Where does ext4 store directory sizes? Are they stored in the directory inode?For example, when I run du -h, it returns directories' size instantly, so I don't believe it calculates it at that time.I'm using ext4 on Linux.
Where does ext4 store directory sizes?
directory;ext4
Using strace would seem to indicate that the file sizes are indeed calculated by querying the files within the directory.ExampleSay I fill a directory with 3 1MB files.$ mkdir adir$ fallocate -l 1M adir/afile1.txt$ fallocate -l 1M adir/afile2.txt$ fallocate -l 1M adir/afile3.txtNow when we trace the du -h command:$ strace -s 2000 -o du.log du -h adir/3.1M    adir/Looking at the resulting strace log file du.log:...newfstatat(AT_FDCWD, adir/, {st_mode=S_IFDIR|0775, st_size=4096, ...}, AT_SYMLINK_NOFOLLOW) = 0fcntl(3, F_DUPFD, 3)                    = 4fcntl(4, F_GETFD)                       = 0fcntl(4, F_SETFD, FD_CLOEXEC)           = 0getdents(3, /* 5 entries */, 32768)     = 144getdents(3, /* 0 entries */, 32768)     = 0close(3)                                = 0newfstatat(4, afile2.txt, {st_mode=S_IFREG|0644, st_size=1048576, ...}, AT_SYMLINK_NOFOLLOW) = 0newfstatat(4, afile3.txt, {st_mode=S_IFREG|0644, st_size=1048576, ...}, AT_SYMLINK_NOFOLLOW) = 0newfstatat(4, afile1.txt, {st_mode=S_IFREG|0644, st_size=1048576, ...}, AT_SYMLINK_NOFOLLOW) = 0brk(0)                                  = 0x231a000...Notice the newfstatat system calls? These are getting the size of each file in turn.Additional BackgroundIf you're interested here's a bit more on the subject. This behavior has nothing to do with EXT4. This is just how filesystems work in Unix.The stat command provides no facility for querying anything other then the size of a filesystem object (directory or file).$ stat adir/  File: adir/  Size: 4096        Blocks: 8          IO Block: 4096   directoryDevice: fd02h/64770d    Inode: 11539929    Links: 2Access: (0775/drwxrwxr-x)  Uid: ( 1000/    saml)   Gid: ( 1000/    saml)Context: unconfined_u:object_r:user_home_t:s0Access: 2014-04-15 22:29:25.289639888 -0400Modify: 2014-04-15 22:29:44.977638542 -0400Change: 2014-04-15 22:29:44.977638542 -0400 Birth: -Notice it's 4096 bytes. That's the actual size of the directory itself, not what it contains.ReferencesSome new system calls