Skip to main content
added 142 characters in body
Source Link
Random832
  • 11.3k
  • 2
  • 39
  • 42

Is /sys/block/sda/size in block size? If so which one?

The ioctl BLKGETSIZE has the same problem as it is in units of 512 rather than BLKSSZGET. BLKGETSIZE64 solves this ambiguity. The real block count is BLKGETSIZE64/BLKSSZGET.

/*BINFMTC:
http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <assert.h>

int main(int argc, char **argv)
{
        int fd;
        long blk=0L;
        long ssz=0L;
        long long oct=0LL;

        assertif((fd=open(argv[1],O_RDONLY))>-<0) { perror(argv[1]); exit(1); }
        assertif(!ioctl(fd,BLKGETSIZE,&blk)<0) { perror("BLKGETSIZE"); exit(1); }
        assertif(!ioctl(fd,BLKSSZGET,&ssz)<0) { perror("BLKSSZGET"); exit(1); }
        assertif(!ioctl(fd,BLKGETSIZE64,&oct)<0) { perror("BLKGETSIZE64"); exit(1); }
        assertif(!close(fd)<0) { perror("close"); exit(1); }
        printf("BLKGETSIZE=%ld BLKSSZGET=%ld BLKGETSIZE64=%lld BLKGETSIZE64/BLKSSZGET=%ld SIZEGB=%f #%f\240GiB\n\n",\
blk,ssz,oct,(long)(oct/(long long)ssz),(double)oct/1000000000.0,(double)oct/1073741824.0);

        fflush(stdout); /* before exec */

        execl("/bin/bash","bash","-c",\
"for i in \
/sys/block/?d?/{size,alignment_offset,?d??/size,?d??/alignment_offset,queue/*block*,queue/*sector*}; \
do test -f \"$i\" && echo \"$i: $(<$i)\"; done"\
,NULL);

        exit(127);
        return 127; /* not reached */
}

See http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html

Is /sys/block/sda/size in block size? If so which one?

The ioctl BLKGETSIZE has the same problem as it is in units of 512 rather than BLKSSZGET. BLKGETSIZE64 solves this ambiguity. The real block count is BLKGETSIZE64/BLKSSZGET.

/*BINFMTC:
http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <assert.h>

int main(int argc, char **argv)
{
        int fd;
        long blk=0L;
        long ssz=0L;
        long long oct=0LL;

        assert((fd=open(argv[1],O_RDONLY))>-1);
        assert(!ioctl(fd,BLKGETSIZE,&blk));
        assert(!ioctl(fd,BLKSSZGET,&ssz));
        assert(!ioctl(fd,BLKGETSIZE64,&oct));
        assert(!close(fd));
        printf("BLKGETSIZE=%ld BLKSSZGET=%ld BLKGETSIZE64=%lld BLKGETSIZE64/BLKSSZGET=%ld SIZEGB=%f #%f\240GiB\n\n",\
blk,ssz,oct,(long)(oct/(long long)ssz),(double)oct/1000000000.0,(double)oct/1073741824.0);

        fflush(stdout); /* before exec */

        execl("/bin/bash","bash","-c",\
"for i in \
/sys/block/?d?/{size,alignment_offset,?d??/size,?d??/alignment_offset,queue/*block*,queue/*sector*}; \
do test -f \"$i\" && echo \"$i: $(<$i)\"; done"\
,NULL);

        exit(127);
        return 127; /* not reached */
}

See http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html

Is /sys/block/sda/size in block size? If so which one?

The ioctl BLKGETSIZE has the same problem as it is in units of 512 rather than BLKSSZGET. BLKGETSIZE64 solves this ambiguity. The real block count is BLKGETSIZE64/BLKSSZGET.

/*BINFMTC:
http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <assert.h>

int main(int argc, char **argv)
{
        int fd;
        long blk=0L;
        long ssz=0L;
        long long oct=0LL;

        if((fd=open(argv[1],O_RDONLY))<0) { perror(argv[1]); exit(1); }
        if(ioctl(fd,BLKGETSIZE,&blk)<0) { perror("BLKGETSIZE"); exit(1); }
        if(ioctl(fd,BLKSSZGET,&ssz)<0) { perror("BLKSSZGET"); exit(1); }
        if(ioctl(fd,BLKGETSIZE64,&oct)<0) { perror("BLKGETSIZE64"); exit(1); }
        if(close(fd)<0) { perror("close"); exit(1); }
        printf("BLKGETSIZE=%ld BLKSSZGET=%ld BLKGETSIZE64=%lld BLKGETSIZE64/BLKSSZGET=%ld SIZEGB=%f #%f\240GiB\n\n",\
blk,ssz,oct,(long)(oct/(long long)ssz),(double)oct/1000000000.0,(double)oct/1073741824.0);

        fflush(stdout); /* before exec */

        execl("/bin/bash","bash","-c",\
"for i in \
/sys/block/?d?/{size,alignment_offset,?d??/size,?d??/alignment_offset,queue/*block*,queue/*sector*}; \
do test -f \"$i\" && echo \"$i: $(<$i)\"; done"\
,NULL);

        exit(127);
        return 127; /* not reached */
}

See http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html

Post Migrated Here from stackoverflow.com (revisions)
Source Link
Andrew Buckeridge
Andrew Buckeridge

Is /sys/block/sda/size in block size? If so which one?

The ioctl BLKGETSIZE has the same problem as it is in units of 512 rather than BLKSSZGET. BLKGETSIZE64 solves this ambiguity. The real block count is BLKGETSIZE64/BLKSSZGET.

/*BINFMTC:
http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
#include <assert.h>

int main(int argc, char **argv)
{
        int fd;
        long blk=0L;
        long ssz=0L;
        long long oct=0LL;

        assert((fd=open(argv[1],O_RDONLY))>-1);
        assert(!ioctl(fd,BLKGETSIZE,&blk));
        assert(!ioctl(fd,BLKSSZGET,&ssz));
        assert(!ioctl(fd,BLKGETSIZE64,&oct));
        assert(!close(fd));
        printf("BLKGETSIZE=%ld BLKSSZGET=%ld BLKGETSIZE64=%lld BLKGETSIZE64/BLKSSZGET=%ld SIZEGB=%f #%f\240GiB\n\n",\
blk,ssz,oct,(long)(oct/(long long)ssz),(double)oct/1000000000.0,(double)oct/1073741824.0);

        fflush(stdout); /* before exec */

        execl("/bin/bash","bash","-c",\
"for i in \
/sys/block/?d?/{size,alignment_offset,?d??/size,?d??/alignment_offset,queue/*block*,queue/*sector*}; \
do test -f \"$i\" && echo \"$i: $(<$i)\"; done"\
,NULL);

        exit(127);
        return 127; /* not reached */
}

See http://lkml.indiana.edu/hypermail/linux/kernel/0105.2/0744.html