* O_SYNC on /dev/sda1 (usb-storage) do not work?
@ 2006-06-22 23:43 bash
0 siblings, 0 replies; only message in thread
From: bash @ 2006-06-22 23:43 UTC (permalink / raw)
To: linux-kernel
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2006-06-23 0:08 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-22 23:43 O_SYNC on /dev/sda1 (usb-storage) do not work? bash
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Powered by JetHome