Hi,

 Maybe you should verify  you don't have an alignment problem when using O_DIRECT.

When you try , from user space , to use O_DIRECT for reading
from a file, you must use page granularity for achieving page alignemnent.

according to the man (2) read , regarding O_DIRECT flag:
"...
 Transfer  sizes,  and the alignment of user buffer and
              file offset must all be multiples of the logical block  size  of
              the file system.
..."


If you look at the following code exmaple , a little app from user space,
you will see that it will not work probably if you try to use ptr1 for reading, which is (probably)
not page aligned. ptr2 is changed  to the next page boundary, so it will
do the job.
 To be more accurate I shouldn't have use 4096 but get the page
size by a syscall (there are cases when it is 8k); and maybe there is something
which can be  written better which demonstrates the alignment issue regarding
O_DIRECT, but this seems to illustrate the point.

Also the size parameter of the read() must be a multiple of a page size, as far as
I understand.

// readFile.c

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <asm/types.h>
#include <asm/fcntl.h>

int main()
    {
    ssize_t size;   
    char* ptr1;
    char* ptr2;
   
    int fd = open("/work/input.txt",O_DIRECT);
    ptr1 = (void*)malloc(4096+4096-1);
    if (((int)ptr1 % 4096)!=0)
        printf("ptr1 % 4096)!=0)\n");
    else
        printf("ptr1 % 4096)==0)\n");
       
    ptr2 = (void*)((int)ptr1 - (int)ptr1 % 4096 + 4096);
    printf("%p %p\n",ptr1,ptr2);
    if (((int)ptr2 % 4096)!=0)
        printf("ptr2 % 4096)!=0)\n");
    else
        printf("ptr2 % 4096)==0)\n");

    if (fd > 0)
        {
        while (1)
            {
               
            size = read(fd,ptr2,4096);
            if (size <= 0)
                {
                printf("\n");
                   
                break;
                }
            printf("%s",ptr2);
            }
        }
    }

Regards,
Rami Rosen


 

On 8/30/05, Gilboa Davara <[EMAIL PROTECTED]> wrote:
On Tue, 2005-08-30 at 14:40 +0300, guy keren wrote:
> On Tue, 30 Aug 2005, Gilboa Davara wrote:
>
> > > Anyway, if you don't know how to use O_DIRECT from inside the kernel,
> > > why don't you take a look at the part in teh kernel that implments
> > > O_DIRECT from user space and do the same thing?
> >
> > Already on it; However, this code is fairly complex. It involves the
> > file management, vmm, etc. It's very easy to get lost in there... :/
> > If I can save the next 3 days of ctags drilling (or at least get some
> > pointers as for where to look) I'll be one happy man.
>
> you can save the next 3 days by deciding this is a very good idea to learn
> how this code works, without taking short-cuts, since you'll need this
> knowledge later on when you'll have bugs, when you'll need to make it
> behave a little differently, etc.

.. Let me correct myself.
I'm not looking for a code sample, I am looking for info.
Book, URL, etc.
Seems that as you go deeper inside the kernel, the less 'printed'
documentation exists.

Gilboa


=================================================================
To unsubscribe, send mail to [EMAIL PROTECTED] with
the word "unsubscribe" in the message body, e.g., run the command
echo unsubscribe | mail [EMAIL PROTECTED]


Reply via email to