Skip to contentSkip to conversation

set blksize of block device

DISCUSSION Bijoy Thomas 3 participants 3 messages Oct 28, 2004 – Nov 1, 2004

lore
Share & tools
lore
#0 lore raw

You set the blocksize for block device when you format it. For e.g, when you format a device with mkfs.ext2, you specify what block size you wish to use. This gets recorded in the superblock of the device. You can see what blocksize a device is using by running the tune2fs command with the deivce as an argument.

Reading and writing a block on a device in userspace is as simple as opening the device, lseeking to the block in question and doing a read or write. Keep in mind that the filesystem blocksize has nothing to do with the blocksize for the device. The sector size for most block devices is 512 bytes. This means that the unit in which we can communicate with the device is 512bytes. However, the filesystem driver will have it own unit i.e, the blocksize. Hence, usually many sectors will fall in a block. The blocks are held in the buffer cache.The filesystem block size should be a power of 2 and less than the pagesize.

In kernel space, reads and writes to blocks on the device are done through the function bread and block_read. Both functions are used to read blocks from a device. If you modify a block, you can set the buffer as dirty and the kernel will later write it to disk.

Regards,
Bijoy.

----- Original Message -----
From: Lei Yang <[email protected]>
Date: Wednesday, October 27, 2004 10:25 pm
Subject: Re: set blksize of block device

Quoted 72 lines

Or in other words, is there generic routines for block devices such that
we could:

get (set) block size of a block device;
read an existing block (e.g. block 4);
write an existing block (e.g. block 5);

Please help!!!!

TIA
Lei

Quoted 1 line · Lei Yang

If nobody could answer this question, what about another one? Is

there

Quoted 1 line

a system call or a kernel interface that would allow me to write

a

Quoted 16 lines

block of data to block 1 of a certain block device?

Thanks for your reply in advance!

Lei

Quoted 8 lines · Lei Yang

Please cc me if you have answers to this, I am not on the list. Thanks a lot!

Quoted 3 lines · Lei Yang

Hello,

I am learning block device drivers and have a newbie question.

Given

Quoted 1 line
Quoted 1 line
Quoted 1 line

a block device, is there anyway that I could set its block

size? For

Quoted 1 line
Quoted 1 line
Quoted 1 line

example, I want to write a block device driver that will work

on an

Quoted 1 line
Quoted 1 line
Quoted 1 line

existing block device. In this driver, I want block size

smaller.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

(The idea looks confusing but I could explain if anybody is interested :- ) However, typically the block size is 1KB, now

I

Quoted 17 lines
Quoted 8 lines
Quoted 5 lines

want to set it to 512 or 256. Can I do it?

TIA
Lei

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

--

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

Thank you very much for your reply!

The problem is, I want to do all these stuff in kernel space. More specifically, I want to use them in a newbie kernel module. In this module, I wanna do something with a raw block device (with no filesystem). Below are some more questions regarding to your comments. Really appreciate your help!

Lei

Quoted 3 lines · Bijoy Thomas

You set the blocksize for block device when you format it. For e.g, when you format a device with mkfs.ext2, you specify what block size you wish to use. This gets recorded in the superblock of the device. You can see what blocksize a device is using by running the tune2fs command with the deivce as an argument.

What if I don'w want to format the device with a filesystem?

Quoted 5 lines

Reading and writing a block on a device in userspace is as simple as opening the device, lseeking to the block in question and doing a read or write. Keep in mind that the filesystem blocksize has nothing to do with the blocksize for the device. The sector size for most block devices is 512 bytes. This means that the unit in which we can communicate with the device is 512bytes. However, the filesystem driver will have it own unit i.e, the blocksize. Hence, usually many sectors will fall in a block. The blocks are held in the buffer cache.The filesystem block size should be a power of 2 and less than the pagesize.

In kernel space, reads and writes to blocks on the device are done through the function bread and block_read. Both functions are used to read blocks from a device. If you modify a block, you can set the buffer as dirty and the kernel will later write it to disk.

Isn't there a bwrite similar to bread?

Quoted 23 lines

Regards,
Bijoy.

----- Original Message -----
From: Lei Yang <[email protected]>
Date: Wednesday, October 27, 2004 10:25 pm
Subject: Re: set blksize of block device

Quoted 12 lines

Or in other words, is there generic routines for block devices such that we could:

get (set) block size of a block device;
read an existing block (e.g. block 4);
write an existing block (e.g. block 5);

Please help!!!!

TIA
Lei

#2 lore raw
In reply to Bijoy Thomas
Quoted 1 line · Bijoy Thomas

You set the blocksize for block device when you format it.

Just to clear this up for the archives, it's worth explaining the difference between the various block sizes mentioned - the poster was referring to hardware block size (e.g. sector size on a hard disk) and not to the size used within a particular filesystem.

Jon.