Re: [PATCH v2 1/1] dcache: Translating dentry into pathname withouttaking rename_lock

From: Al Viro
Date: Thu Sep 05 2013 - 16:46:20 EST


On Thu, Sep 05, 2013 at 04:29:06PM -0400, Waiman Long wrote:

> It is not as simple as doing a strncpy(). The pathname was built
> from the leaf up to the root, and from the end of buffer toward the
> beginning. As it goes through the while loop, the buffer will look
> like:
>
> " /c"
> " /b/c"
> "/a/b/c"
>
> If the content of the string is unreliable, I have to do at least 2 passes:
> 1) Locate the end of the string and determine the actual length
> 2) Copy the whole string or byte-by-byte backward

No, you do not need anything of that kind. All you need is
a) don't step out of the array (which will contain NUL at the end
at all times, no matter what) and
b) generate correct output *IF* no d_move() happens while you
do that.

Nothing else matters at all. You trust the length to be correct in absense
of d_move(). You can not trust it to match the size of ->d_name.name when
d_move() is happening, but you can trust everything up to ->d_name.len *or*
the first NUL, whichever happens first, to be safe to access.

Again, the contents copied into the buffer needs to be valid only if d_move()
hasn't happened; if it has, we don't give a fuck - read_seqretry() will take
care of that. All you need to care about in that case is not oopsing the
damn thing.

static int prepend_name(char **buffer, int *buflen, struct qstr *name)
{
const char *s = ACCESS_ONCE(name->name);
unsigned len = ACCESS_ONCE(name->len);
char *p;

*buflen -= len;
if (*buflen < 0)
return -ENAMETOOLONG;
p = *buffer -= len;
while (len--) {
c = *s++;
if (!c)
break;
*p++ = c;
}
return 0;
}

And that's *all* - just call that under rcu_read_lock() and within
seq = read_seqbegin(&rename_lock)/read_seqretry(&rename_lock, seq)
loop over the whole prepend_path/path_with_deleted/__dentry_path
thing.
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/