LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: "congwen" <congwen@gmail.com>
To: "linux-kernel" <linux-kernel@vger.kernel.org>
Subject: How can I create or read/write a file in linux device driver?
Date: Fri, 12 Jan 2007 15:47:32 +0800	[thread overview]
Message-ID: <200701121547221465420@gmail.com> (raw)

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");
      }
   }
}


             reply	other threads:[~2007-01-12  7:47 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-12  7:47 congwen [this message]
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

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=200701121547221465420@gmail.com \
    --to=congwen@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --subject='Re: How can I create or read/write a file in linux device driver?' \
    /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

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).