Problem using O_DIRECT

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi,

	I have successfully written to a file through a kernel module. Now
what I want is to avoid buffer cache while read/write to a file
through a kernel module. For that I set O_DIRECT flag like shown in
the code below. But it the write returns error that invalid argument.
Anybody knows what might the problem n how do I get around with it? (I
am using kernel 2.6.9).

Regards,
Linux Prodigy.

//File od1.c
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/config.h>
#include <asm/uaccess.h>
#include <linux/file.h>
#include <linux/mm.h>
#include <linux/slab.h>

static int __init modinit(void)
{
	struct file *f;
	mm_segment_t orig_fs;
	int lenwrite = 0,i;
	char buff[512],fwrite[] = "/root/programs/odirect/try.txt";

	printk(KERN_ALERT "\nHello World 1\n ");
	for(i=0; i<512 ; i++)
		buff[i] = 'x';

	f = filp_open(fwrite,O_WRONLY, 0600);
	f->f_flags |= O_DIRECT;


	orig_fs = get_fs();
	set_fs(KERNEL_DS);
	
	lenwrite = f->f_op->write(f,buff,512,&f->f_pos);

	set_fs(orig_fs);
	
	printk(KERN_ALERT "\nHello World 1 after writing %d \n ",lenwrite);
	
	fput(f);

	return 0;
}

static void __exit modexit(void)
{
	printk(KERN_ALERT "\nBye World 1 :\n");
}

module_init(modinit);
module_exit(modexit);

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux