mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: bash <0x62ash@gmail.com>
To: linux-kernel@vger.kernel.org
Subject: O_SYNC on /dev/sda1 (usb-storage) do not work?
Date: Fri, 23 Jun 2006 03:43:50 +0400	[thread overview]
Message-ID: <20060623034350.d15614d3.0x62ash@gmail.com> (raw)

  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

                 reply	other threads:[~2006-06-23  0:08 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20060623034350.d15614d3.0x62ash@gmail.com \
    --to=0x62ash@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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