8

The limit placed on disk quota in Linux is counted in blocks. However, I found no reliable way to determine the block size. Tutorials I found refer to block size as 512 bytes, and sometimes as 1024 bytes.

I got confused reading a post on LinuxForum.org for what a block size really means. So I tried to find that meaning in the context of quota.

I found a "Determine the block size on hard disk filesystem for disk quota" tip on NixCraft, that suggested the command:

dumpe2fs /dev/sdXN | grep -i 'Block size'

or

blockdev --getbsz /dev/sdXN

But on my system those commands returned 4096, and when I checked the real quota block size on the same system, I got a block size of 1024 bytes.

Is there a scriptable way to determine the quota block size on a device, short of creating a known sized file, and checking it's quota usage?

Mohsen Nosratinia
  • 9,844
  • 1
  • 27
  • 52
Chen Levy
  • 15,438
  • 17
  • 74
  • 92

1 Answers1

7

The filesystem blocksize and the quota blocksize are potentially different. The quota blocksize is given by the BLOCK_SIZE macro defined in <sys/mount.h> (/usr/include/sys/mount.h):

#ifndef _SYS_MOUNT_H
#define _SYS_MOUNT_H    1

#include <features.h>
#include <sys/ioctl.h>

#define BLOCK_SIZE      1024
#define BLOCK_SIZE_BITS 10
...

The filesystem blocksize for a given filesystem is returned by the statvfs call:

#include <stdio.h>
#include <sys/statvfs.h>

int main(int argc, char *argv[])
{
    char *fn;
    struct statvfs vfs;

    if (argc > 1)
        fn = argv[1];
    else
        fn = argv[0];

    if (statvfs(fn, &vfs))
    {
        perror("statvfs");
        return 1;
    }

    printf("(%s) bsize: %lu\n", fn, vfs.f_bsize);

    return 0;
}

The <sys/quota.h> header includes a convenience macro to convert filesystem blocks to disk quota blocks:

/*
 * Convert count of filesystem blocks to diskquota blocks, meant
 * for filesystems where i_blksize != BLOCK_SIZE
 */
#define fs_to_dq_blocks(num, blksize) (((num) * (blksize)) / BLOCK_SIZE)
Chris Magnuson
  • 5,780
  • 7
  • 34
  • 37
caf
  • 233,326
  • 40
  • 323
  • 462
  • So you say that the quota-block-size is a system constant, and is a feature of the current glibc (/usr/include/sys/mount.h) and/or the kernel (/usr/include/linux/fs.h) version? Is there a way to get at this value via `/proc` or `/sys`, or should I simply know thy-installed glibc/kernel? – Chen Levy Mar 24 '10 at 09:52
  • It's a part of the kernel ABI to userspace - so it cannot be changed. – caf Mar 24 '10 at 10:49
  • 1
    "The filesystem blocksize and the quota blocksize are potentially different. The quota blocksize is given by the BLOCK_SIZE macro defined in " -> Extremely useful, thank! Where did you get this from? I'd like to see the original reference... – Dr. Jan-Philip Gehrcke Aug 05 '13 at 13:31