I want store some data on the disk in linux. I want this data stored in continuous disk block in physical disk. If i in order write this data to a normal file, maybe the block that the file occupies is not continuous in physical disk. Is there any way to do this job?
It will mount each one onto a directory on the root partition. For example, if you have a Unix directory called /usr, it is probably a mount point to a partition that contains many programs installed with your Unix but not required during initial boot.
The optimal method of storing a file on a disk is in a contiguous series, i.e. all data in a stream stored end-to-end in a single line. As many files are larger than 512 bytes, it is up to the file system to allocate sectors to store the file's data.
Most file systems allocate space as it's needed, more or less sequentially, but they can't guess future behaviour — so if you write 200MiB to a file, then add a further 100MiB, there's a non-zero chance that both sets of data will be stored in different areas of the disk (basically, any other write needing more space ...
Directories are stored on disk just like regular files (i.e. inode with 14 pointers, etc.) except inode has special flag bit set to indicate that it's a directory. Each directory contains <name, i-number> pairs in no particular order. The file pointed to by the i-number may be another directory.
Disk partitions are continuous regions of a disk.
So one way to do what you want to do is to resize your disk partitions and create a new one with gparted (gnome) or partitionmanager (kde) or similiar - of an appropriate size for your file.
You can then write directly to your new partition (not using and bypassing a filesystem) by using the file:
/dev/sdxn
Where sdxn = {sda1, sda2, ..., sdb1, ... ...} etc. is the letter/number of the partition.
Alternatively you can set aside an entire disk by writing directly to it (bypassing partition table alltogether) using the file:
/dev/sdx
Where sdx = {sda, sdb, sdc, ...} etc. is the letter of the disk.
Warning: Don't make a typo and write to the wrong one (that has a filesystem on it) or you will corrupt it. Best to make a symbolic link ln -s /dev/sdxn /home/fred/mydata, and then always write to mydata file.
The filesystem code (inside the kernel, e.g. in linux-3.1.6/fs/ext4/
for ext4 file systems inside the linux-3.1.6 kernel source) is managing the disk blocks used for a given file. So you cannot organize the disk blocks of some of your files by yourself. However, you might give some hints to the kernel using some weird system calls.
If you don't like that, you could avoid the filesystem all together by writing directly to an unmounted partition, e.g. by doing write(2) syscalls to a file descriptor obtained by open(2)-ing for example /dev/sda2
; but unless you really know what you are doing (and the formulation of your question makes me feel you don't understand the exact role of file systems), I won't recommend doing that.
Kernel file system code is quite good, and kernel file system cache is very efficient.
If you want to speed-up your reads, consider perhaps using readahead(2) or fadvise(2) or madvise(2) system calls.
You could also, when creating your filesystem, tune it for your particular purposes. For instance, if you know you'll have mostly quite big files in it, you could use a bigger than standard block size (e.g. mke2fs -b 8192
), etc...
But don't think that software tricks would speed-up significantly your application; if you do a lot of disk IO, the real bottleneck is the hardware (so using SSD instead of hard disks might be easier).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With