write() issue during a low level hdd access


 
Thread Tools Search this Thread
Top Forums Programming write() issue during a low level hdd access
# 1  
Old 02-21-2010
write() issue during a low level hdd access

Hi,

I am trying to write zeroes to the hdd using a c program. I don't want to use the dd or ddrescue or any such inbuilt program because of reasons like real time progress, writing custom patterns. (my program is more like an erasure application, but does only zero fill).

here are the steps which i have followed. (I am executing under root).

Linux version used is ubuntu with (Intrepid) 8.10, with 2.6.27-7-generic.

opened the device /dev/sda using open call.

Code:
fd = open("/dev/sda", O_WRONLY);

and called a write call with
Code:
write(fd,buff,512);

The write call never fails for some strange reasons. It always returns 512 even in the case of a bad hdd connected to system. (The dmesg shows either the hdd is bad or not responding the write requests, or has I/O errors, or has sectors errors, or has failed the hard / soft reset operations).

I have tried using errno to get the error value, but nothing returns the error condition. Is that something i am missing?

Following is the code for the write function.

Code:
int writesector(const uint64_t offset, const int fd, const uint8_t *mybuff, const uint16_t len)
{
 if(!fd)
  {
    return 4;
  }
  errno = 0;
  if(lseek64(fd, offset, SEEK_SET) != -1)
  {
    if (errno!=0)
    {
      return 1;
    }
    
    errno = 0;
    
    if( (write(fd, mybuff, len)) != len)
    {
      return 1;
    }
    
    if (errno!=0)
    {
      return 1;
    }
  }
  else
  {
    return 2;
  }
  return 0;
}

Has anyone tried a similar approach in writing to hdd's at low level?

What else I can check for? I am currently out of options to identify the issue. I have tried implementing a write timeout, but it bombs badly! Smilie

Any inputs / help on this issue please?

Last edited by Franklin52; 02-21-2010 at 01:57 PM.. Reason: Please use code tags!
# 2  
Old 02-21-2010
You're not doing low-level I/O here, your write is probably going straight into cache. Try opening with O_DIRECT.

Please edit your post to put code in code tags like the thread posting page suggests. [ code ] stuff [ /code ] without the spaces in the tags. Preferably before a mod needs to do so for you. Smilie

Surely you could write custom patterns with either dd or dd_rescue though! dd_rescue in particular shows lots of progress info. Just feed them custom patterns on stdin and you'll have your custom patterns.
# 3  
Old 02-21-2010
thanks for the reply.

I know O_DIRECT might work, and I tried with O_DIRECT, but it fails for some reason. I have tried with aligned the write buffer after getting the block size from the HDD. But it fails miserably and I don't know how to debug it. I stopped digging into it because it might branch my work. Smilie

gdb is not of much help here. I've tried jumping directly to a known bad sector location in the code for testing., but I could not get it to fail the write call. (btw, dmesg starts spewing errors after a certain write attempts on the bad sector). So, the libata is catching those errors and kernel is aware of it. But the program does not receive the error.

I have tried mapping raw devices for the hdds and executed the same operations without success.

Quote:
Has any body has any example code of writing to a hdd using O_DIRECT? Like atleast for a single sector or a file?
I wrote a small program which is working currently. Doing the testing.


I have been trying to get this to work for a long time with multiple failed options! Having sleepless nights too. Smilie

Last edited by sponnusa; 02-21-2010 at 03:06 PM.. Reason: updated the code for O_DIRECT
# 4  
Old 02-21-2010
Quote:
Originally Posted by sponnusa
thanks for the reply.

I know O_DIRECT might work, and I tried with O_DIRECT, but it fails for some reason.
Try printing the error message with perror() to find out what "some reason" is. As a stubborn programmer would say, don't give up until you know why it's not working. Smilie
# 5  
Old 02-21-2010
I wanted the write to fail only for the bad sectors, but it was failing for all the sectors in the O_DIRECT mode, but I got a working program. (Well, working means, I was able to write to the sectors, but still could not get it to fail for the bad sectors).

As i had updated in my previous post, i had created a small program which writes to the sectors with O_DIRECT. I guess i might have made some mistake in my original O_DIRECT try. (lets leave it apart! Smilie)

But, even with the O_DIRECT and O_SYNC flags in place, the write passes for all the bad sectors.

Is there anything that has be enabled / checked?

Thank you for the replies.
# 6  
Old 02-21-2010
Hmm. Try fsync(fd) after the write. If it returns anything other than zero an error happened while syncing the data to disk.
# 7  
Old 02-21-2010
Here is the code which I am using.

Code:
int writesector(const uint64_t offset, const int fd, const uint8_t *mybuff, const uint16_t len)
{
	if(!fd)
  {
    return 4;
  }
  errno = 0;
  if(lseek64(fd, offset, SEEK_SET) != -1)
  {
    if (errno!=0)
    {
      return 1;
    }
    
    errno = 0;
    
    if( (write(fd, mybuff, len)) != len)
    {
      return 1;
    }
    
    if (errno!=0)
    {
      return 1;
    }
    
    if(fsync(fd)!=0)
    {
      return 1;
    }
    //sync();
  }
  else
  {
    return 2;
  }

  return 0;
}

The buffer I am using is aligned. PATTERN_LEN len is currently set to 8192 bytes. (i.e., 16 sectors).

The code is:
Code:
      diskdata = memalign(512,PATTERN_LEN);

    	if(diskdata == NULL)
    	{
    		printf("FATAL ERROR: Unable to allocate memory for write buffer!\n");
    		exit (0);
    	}

      memset(diskdata,0,PATTERN_LEN);

The file is opened using:

Code:
handle = open64(devicename, O_WRONLY | O_DIRECT | O_SYNC);

I don't know what causes the write call to pass always! Smilie

And yes, I am using the
Code:
#define _LARGEFILE64_SOURCE
#define _GNU_SOURCE

flags in the code for compilation of O_DIRECT and *64() functions.
Thank you.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Low level X11 programming

How to use X11 without Xlib not XCB? How draw window directly on low level? I must use anyway window manager like Motif? I have ridden that X11 has server-client architecture, client send via TCP/IP to port 6000 request for primitives and get replies. Where is detailed description of it? In X11... (0 Replies)
Discussion started by: AndrzejB
0 Replies

2. Programming

Why is C/C++ considered low-level languages???

Hi friends, I hope everyone is doing well and fine. I have always been hearing that C/C++ are relatively low-level as compared to Java/C# etc. Could you please tell me some low-level qualities of C/C++? And I think disk deframenters are written in C/C++, please correct me if I am wrong. And please... (5 Replies)
Discussion started by: gabam
5 Replies

3. Programming

System calls and C language low-level qualities???

Hi friends, I hope everyone is fine and doing well. I queried in my previous thread about the low-level qualities of C/C++ languages.I really thank you people for explaining, it was really helpful. One more ambiquity that I have in my mind is regarding the unix system calls like open, creat,... (1 Reply)
Discussion started by: gabam
1 Replies

4. AIX

High Runqueue (R) LOW CPU LOW I/O Low Network Low memory usage

Hello All I have a system running AIX 61 shared uncapped partition (with 11 physical processors, 24 Virtual 72GB of Memory) . The output from NMON, vmstat show a high run queue (60+) for continous periods of time intervals, but NO paging, relatively low I/o (6000) , CPU % is 40, Low network.... (9 Replies)
Discussion started by: IL-Malti
9 Replies

5. IP Networking

Best reference for understanding low level info on nic cards drivers and functionality

Hi, What is the best reference that gives in detail on nic cards configuration , assigning multiple ip addresses to a single interface, netlink library etc and all basic stuff at this level..? Thanks (2 Replies)
Discussion started by: Gopi Krishna P
2 Replies

6. HP-UX

Access to a second HDD

Hello How to access to a second hard disk on a HP-UX system? Thanks (3 Replies)
Discussion started by: ouniss
3 Replies

7. UNIX for Dummies Questions & Answers

Low level format?

I want to do a low level format like in windows (C:\format c:) but I don't know how it works in unix or linux.. Can somebody help me ? thnx :) (3 Replies)
Discussion started by: day
3 Replies
Login or Register to Ask a Question