All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Brian J. Watson" <Brian.J.Watson@compaq.com>
To: mike@bangstate.com, Linux Kernel <linux-kernel@vger.kernel.org>
Subject: Re: kernel space getcwd()? (using current() to find out cwd)
Date: Tue, 17 Apr 2001 16:28:07 -0700	[thread overview]
Message-ID: <3ADCD187.366993BE@compaq.com> (raw)
In-Reply-To: <3ADCC707.449F7B05@compaq.com>

"Brian J. Watson" wrote:
>         path = __d_path(pwd, pwdmnt, NULL, NULL, path, PAGE_SIZE);


Oops! That's no good. Here's the new and improved version:

char *
kgetcwd(char **bufp)
{
        char *path, *buf = (char *) __get_free_page(GFP_USER);
        struct vfsmnt *pwdmnt;
        struct dentry *pwd;

        *bufp = NULL;
        if (!buf)
                return ERR_PTR(-ENOMEM);

        read_lock(&current->fs->lock);
        pwdmnt = mntget(current->fs->pwdmnt);
        pwd = dget(current->fs->pwd);
        read_unlock(&current->fs->lock);

        spin_lock(&dcache_lock);
        path = __d_path(pwd, pwdmnt, NULL, NULL, buf, PAGE_SIZE);
        spin_unlock(&dcache_lock);

        mntput(pwdmnt);
        dput(pwd);

        *bufp = buf;
        return path;
}


The returned pointer is for the beginning of the path name. The pointer filled
into bufp is for the beginning of the allocated space. To deallocate, call
free_page() on the value in bufp.

The reason for the distinction is that __d_path builds the pathname from the end
of the buffer, working its way back toward the beginning. Rarely will the string
begin at the same address as the allocated buffer.


--
Brian Watson
Compaq Computer

  reply	other threads:[~2001-04-17 23:29 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2001-04-17 22:43 kernel space getcwd()? (using current() to find out cwd) Brian J. Watson
2001-04-17 23:28 ` Brian J. Watson [this message]
  -- strict thread matches above, loose matches on Subject: below --
2001-04-16 22:42 Michael L. Welles
2001-04-17 12:30 ` Christoph Hellwig

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=3ADCD187.366993BE@compaq.com \
    --to=brian.j.watson@compaq.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mike@bangstate.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.