Skip to content

Commit 2a3dcbc

Browse files
tyhicksmartinetd
authored andcommitted
9p: Fix refcounting during full path walks for fid lookups
Decrement the refcount of the parent dentry's fid after walking each path component during a full path walk for a lookup. Failure to do so can lead to fids that are not clunked until the filesystem is unmounted, as indicated by this warning: 9pnet: found fid 3 not clunked The improper refcounting after walking resulted in open(2) returning -EIO on any directories underneath the mount point when using the virtio transport. When using the fd transport, there's no apparent issue until the filesytem is unmounted and the warning above is emitted to the logs. In some cases, the user may not yet be attached to the filesystem and a new root fid, associated with the user, is created and attached to the root dentry before the full path walk is performed. Increment the new root fid's refcount to two in that situation so that it can be safely decremented to one after it is used for the walk operation. The new fid will still be attached to the root dentry when v9fs_fid_lookup_with_uid() returns so a final refcount of one is correct/expected. Link: https://lkml.kernel.org/r/[email protected] Link: https://lkml.kernel.org/r/[email protected] Fixes: 6636b6d ("9p: add refcount to p9_fid struct") Cc: [email protected] Signed-off-by: Tyler Hicks <[email protected]> Reviewed-by: Christian Schoenebeck <[email protected]> [Dominique: fix clunking fid multiple times discussed in second link] Signed-off-by: Dominique Martinet <[email protected]>
1 parent e5690f2 commit 2a3dcbc

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

fs/9p/fid.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
152152
const unsigned char **wnames, *uname;
153153
int i, n, l, clone, access;
154154
struct v9fs_session_info *v9ses;
155-
struct p9_fid *fid, *old_fid = NULL;
155+
struct p9_fid *fid, *old_fid;
156156

157157
v9ses = v9fs_dentry2v9ses(dentry);
158158
access = v9ses->flags & V9FS_ACCESS_MASK;
@@ -194,13 +194,12 @@ static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
194194
if (IS_ERR(fid))
195195
return fid;
196196

197+
refcount_inc(&fid->count);
197198
v9fs_fid_add(dentry->d_sb->s_root, fid);
198199
}
199200
/* If we are root ourself just return that */
200-
if (dentry->d_sb->s_root == dentry) {
201-
refcount_inc(&fid->count);
201+
if (dentry->d_sb->s_root == dentry)
202202
return fid;
203-
}
204203
/*
205204
* Do a multipath walk with attached root.
206205
* When walking parent we need to make sure we
@@ -212,6 +211,7 @@ static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
212211
fid = ERR_PTR(n);
213212
goto err_out;
214213
}
214+
old_fid = fid;
215215
clone = 1;
216216
i = 0;
217217
while (i < n) {
@@ -221,19 +221,15 @@ static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
221221
* walk to ensure none of the patch component change
222222
*/
223223
fid = p9_client_walk(fid, l, &wnames[i], clone);
224+
/* non-cloning walk will return the same fid */
225+
if (fid != old_fid) {
226+
p9_client_clunk(old_fid);
227+
old_fid = fid;
228+
}
224229
if (IS_ERR(fid)) {
225-
if (old_fid) {
226-
/*
227-
* If we fail, clunk fid which are mapping
228-
* to path component and not the last component
229-
* of the path.
230-
*/
231-
p9_client_clunk(old_fid);
232-
}
233230
kfree(wnames);
234231
goto err_out;
235232
}
236-
old_fid = fid;
237233
i += l;
238234
clone = 0;
239235
}

0 commit comments

Comments
 (0)