LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
* How can I create or read/write a file in linux device driver?
@ 2007-01-12  7:47 congwen
  2007-01-12 10:27 ` Jesper Juhl
  0 siblings, 1 reply; 10+ messages in thread
From: congwen @ 2007-01-12  7:47 UTC (permalink / raw)
  To: linux-kernel

Hello everyone, I want to create and read/write a file in Linux kernel or device driver, I have write some code, the file could be created successfully, but it can not be wrote in anything, the function "write" always return a negative number, the content of the file is still empty. The following code is what I wrote in my device driver, the code reference the function sys_open() in open.c and sys_write() in read_write.c, please give me a help, thanks!

struct file *filp;
char testbuf[100] = {0};
char *ptestbuf = NULL;
ssize_t count = 0;
ssize_t ret = 0;
int ii = 0;
   
filp = filp_open("/logfile", O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (IS_ERR(filp))
   printk("<0>Create record file error!\n\r");
for(ii = 0; ii < 100; ii++)
   testbuf[ii] = 1;
ptestbuf = testbuf;

if (filp->f_mode & FMODE_WRITE) 
{
   struct inode *inode = filp->f_dentry->d_inode;
   ret = locks_verify_area(FLOCK_VERIFY_WRITE, inode, filp, filp->f_pos, count);
   if (!ret) 
   {
      ssize_t (*write)(struct file *, const char *, size_t, loff_t *);
      if (filp->f_op && (filp->f_op->write) != NULL) 
      {
         write = filp->f_op->write;
         count = 99;
         ret = write(filp, (const char *)ptestbuf, count, &filp->f_pos);
         printk("<0>After write, ret = %d   \n\r");
      }
   }
}


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2007-01-19 10:37 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-01-12  7:47 How can I create or read/write a file in linux device driver? congwen
2007-01-12 10:27 ` Jesper Juhl
2007-01-12 10:43   ` Jan Engelhardt
2007-01-12 10:54     ` Jesper Juhl
2007-01-12 10:59       ` Jan Engelhardt
2007-01-12 13:24   ` Erik Mouw
2007-01-12 14:27     ` linux-os (Dick Johnson)
2007-01-12 14:42       ` Erik Mouw
2007-01-12 22:31       ` Jan Engelhardt
2007-01-19 10:35       ` Helge Hafting

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).