O_SYNC on /dev/sda1 (usb-storage) do not work?



Hello All,
Im wanna to write to flash drive synchronously, but i found that this
is not work for me :/

Test example:

#define DEVICE "/dev/sda1"
#define BLOCK_SIZE 512 // size of block in bytes
#define FIRST_BLOCK (190*1024*2+1) // offset in blocks (190 Mb)
#define DEVICE_SIZE (122312) // size in blocks (79,98 Mb)

int blk_write(void *buf, size_t count, size_t offset)
{
int blk_size = (count / BLOCK_SIZE) + ((count % BLOCK_SIZE > 0) ?
1 : 0); int f;
f = open(DEVICE, O_WRONLY|O_SYNC);
if (f < 0) {
fprintf(stderr, "write(): Cannot open file %s: %s\n", DEVICE,
strerror(errno)); return -1;
}
offset += FIRST_BLOCK;

if (lseek(f, offset * BLOCK_SIZE, SEEK_SET) != offset * BLOCK_SIZE)
{ fprintf(stderr, "write(): Cannot lseek at file %s: %s\n", DEVICE,
strerror(errno)); close(f);
return -1;
}
if (write(f, buf, BLOCK_SIZE * blk_size) != BLOCK_SIZE * blk_size) {
fprintf(stderr, "write(): Cannot write to file %s: %s\n",
DEVICE, strerror(errno)); close(f);
return -1;
}

#if 1
if (fsync(f) == -1)
fprintf(stderr, "write(): Can't fsync file %s: %s\n", DEVICE,
strerror(errno)); sync();
#endif

if (close(f)) {
fprintf(stderr, "write(): Cannot close file %s: %s\n", DEVICE,
strerror(errno));
}
return 0;
}


#define BUF_SIZE_IN_BLOCKS 10
#define SIGNATURE "this is %d iteration of write() with buf filled by %c\n"

int main()
{
size_t buf_size = BLOCK_SIZE * BUF_SIZE_IN_BLOCKS;
char *buf = malloc(buf_size);
char signature[1024];
int i;

srandom(time(NULL));
char fill_char = random () % 27 + 65; // random char [A-Z]

printf("fill char is %c\n", fill_char);

for (i = 0; i < 500; i += BUF_SIZE_IN_BLOCKS) {
sprintf(signature, SIGNATURE, i, fill_char);
memset(buf, fill_char , buf_size);
memcpy(buf, signature, strlen(signature));

if (i % 30 == 0)
printf("Iteration %d\n", i);

if (blk_write(buf, buf_size, i) == -1) {
printf("Cant write at this iteration: ");
printf(signature);
return -1;
}
}

free(buf);
return 0;
}


So.... im starting this program ....
$ sudo ./test1
fill char is D
Iteration 0
Iteration 30
Iteration 60
Iteration 90
write(): Cannot write to file /dev/sda1: Input/output error
Cant write at this iteration: this is 110 iteration of write() with buf
filled by D


After 90 I unpluged flash-drive from system.

So, if all operations of write() is synchronously then write() for
"100" iteration was successfull and completed. But when Im dump flash
(by ``dd'' and ``xxd'' programs) i see that buf for 100 iteration was
no written to drive :/


--
Biomechanica Artificial Sabotage Humanoid
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/



Relevant Pages

  • Re: making bytes out of bits
    ... int charget; ... In each first iteration of the inner loop, ... unsigned char, and add the value returned by charget. ...
    (comp.lang.c)
  • How to catch this?
    ... a char* type. ... To show result I used printf and I noticed that if I use ... last result is shown at each iteration. ... char* function (int in1, int in2) ...
    (comp.lang.c)
  • Re: Why does the instance change?
    ... public PositionVector(int _x, int _y, int _z) ... myClass myObject = new Object; ... on first iteration: ... I presume that a new memory allocation isn't being made and so on ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Local variables within looping constructs
    ... > the iteration. ... variable like an int there may well be no cost at all. ... That isn't within the loop, though, that is exactly equivalent to ... variables local to where they are used inproves clarity and reduces the ...
    (comp.lang.c)
  • Re: Unsure about Something
    ... I don't think there is any other way to do this (declare a local ... >>In each iteration you have declared a new variable with the name "a". ... > int n = i; ... > is re-created on each iteration of the loop. ...
    (microsoft.public.dotnet.languages.csharp)