1

Im trying to use O_DIRECT to open a file and we're using jffs2

fd = open(filename, O_RDONLY|O_DIRECT|O_SYNC);

But i only get an "invalid argument" error. I have also tried the solutions in this SO link but to no avail. Ofcourse i have also put in "-D_GNU_SOURCE" in building the source.

I've read that O_DIRECT is not supported on tmpfs, but couldn't find support or un-support for jffs2. If someone could give me proof or un-proof for jffs2 support and/or any other thing i'm missing to make O_DIRECT work.

note: This all happened because Linux seem to cache memory data on its own when i use fread()/read(). And using system("echo 3 > /proc/sys/vm/drop_caches"); would be too late because I don't want the FS allocating cache memory in the first place.

Thanks, Naze

Community
  • 1
  • 1
Naze Kimi
  • 303
  • 1
  • 3
  • 23

1 Answers1

2

jffs2 does not support O_DIRECT.

You can see in __dentry_open() that in order to support O_DIRECT, a filesystem must supply the direct_IO and/or get_xip_mem members of the address_space_operations struct. You can likewise see that jffs2 does not supply these members.

It is not clear why you are so keen to avoid the page cache. Pages in the page cache are effectively "free" - they will be reclaimed when more memory is required for other uses.

caf
  • 233,326
  • 40
  • 323
  • 462
  • Thank you for your immediate reply. Now I've learned some new things about Linux filesystems. We're actually using this for an embedded system, and have only 32mb of ram space. With the runtime files, system files and cached runtime files, leaving less than 20mb, i would like to minimize memory used. so as to maximize incoming additional projects files. with Linux caching files i can't load anything more than 10mb. – Naze Kimi May 18 '11 at 00:45
  • @Naze Kimi: Pages in the page cache won't starve other things of memory - those pages will be evicted if more memory is required, which is why they are essentially equivalent to free memory. Have you tried mapping file(s) with `mmap()` rather than using `read()`? That will map the page cache pages directly into your processes address space. – caf May 18 '11 at 02:16
  • "Pages in the page cache won't starve other things of memory" - i know it says that, but it does gobbles up memory. plus there is still the problem of fread()/read() returning an error when the file is more than half of the free memory. i did try your suggestion on mmap(), though the files are loaded, i get errors on other processes of the program. anyway, mmap() might just what i needed. thanks again. – Naze Kimi May 19 '11 at 04:40