6

I have a special need for block data storage. My data are formatted data blocks in size of 4096. For high efficiency, I want to directly manipulate the block on hard disk sector and do not want to treat the data block as file. I think one way is to treat the device as a file such as /dev/sda1 and to use lseek() read() and write() to read and write data. However I do not know the whether the head of the file is the first sector of hard disk. I also suspect the efficiency of this method.

I am working on Linux OS and C programming language.

What is the most efficient way to handle the sector of hard disk? Should I write an block device module of linux. However, I do not know much about it. What kernel functions should I use to read and write on block device?

chenatu
  • 827
  • 2
  • 10
  • 22
  • Is this: http://stackoverflow.com/questions/3520459/linux-hard-disk-direct-access-without-any-fs-from-c-program Relevant to you? Seems to answer most of the "how" questions though you may have to do basic research for the exact functions. – Avery Nov 23 '13 at 10:43
  • It is relevant, but I don't know which method has good performance of I/O speed for random access to hard disk. – chenatu Nov 23 '13 at 10:51
  • That question may be one you have to answer for yourself as it is very subjective and not very well suited to this format. – Avery Nov 23 '13 at 10:52
  • 4
    **Why do you want to avoid a file system?** Given that hard disks are about a million time slower than RAM, and SSDs are many thousand times slower than RAM, the file system overhead is so tiny that it is usually not perceptible. And the disk controller (inside the disk itself, so not avoidable by the any software) is doing a lot of things these days (sector reorganizations, etc...). You could for example use (from a user-land application) ordinary `read(2)` access (with data size multiple of 4Kbytes, and 4Kbyte aligned). Also, file systems are cached! – Basile Starynkevitch Nov 23 '13 at 13:17
  • What is your *"special need for block data storage"*; explain much more your motivations and overall goals. – Basile Starynkevitch Nov 23 '13 at 13:24
  • i don't understand why people need to justify their questions. he asked a question. maybe the answer is that it's not possible. fine. but why demand that he justify his reasons? it is not necessary to answer the question. – andrew cooke Nov 23 '13 at 13:47
  • 5
    @andrewcooke: people sometimes ask for more detail about questions because of the "X-Y problem": http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem - this is a pretty common thing when the question seems more specific about its solution than its original problem. – John Zwinck Nov 23 '13 at 14:35
  • 1
    @andrewcooke: because if I see someone asking how to operate a gun to cut his nails, I think it's much more useful to explain him that nail clippers are more than adequate. – Matteo Italia Nov 23 '13 at 16:56
  • Possible duplicate of [Read a single sector from a disk](https://stackoverflow.com/q/1753067/608639) – jww Dec 09 '18 at 09:59

2 Answers2

7

"Blocks in size of 4096" is not a special need, and you have not mentioned any access patterns that would break the kernel's built-in caching mechanisms.

The most efficient way to read and write your data is to use a file.

CL.
  • 173,858
  • 17
  • 217
  • 259
3
int ReadSector(int numSector,char* buf)
{
    int retCode = 0;
    BYTE sector[512];
    DWORD bytesRead;
    HANDLE device = NULL;

    device = CreateFile("\\\\.\\H:",    // Drive to open
        GENERIC_READ,           // Access mode
        FILE_SHARE_READ,        // Share Mode
        NULL,                   // Security Descriptor
        OPEN_EXISTING,          // How to create
        0,                      // File attributes
        NULL);                  // Handle to template

    if(device != NULL)
    {
        // Read one sector
        SetFilePointer (device, numSector*512, NULL, FILE_BEGIN) ;

        if (!ReadFile(device, sector, 512, &bytesRead, NULL))
        {
            Print("Error in reading1 floppy disk\n",numligne++);
        }
        else
        {
            // Copy boot sector into buffer and set retCode
            memcpy(buf,sector, 512);retCode=1;
        }

        CloseHandle(device);
        // Close the handle
    }

    return retCode;
}

This my function to read sectors and it is same maner to write. Sector zero will be first sector of a partition

Daniel
  • 49
  • 2