Skip to content

Commit 1836d95

Browse files
committed
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh/v9fs
* 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ericvh/v9fs: 9p: fix readdir corner cases 9p: fix readlink 9p: fix a small bug in readdir for long directories
2 parents 333a074 + 3e2796a commit 1836d95

4 files changed

Lines changed: 74 additions & 36 deletions

File tree

‎fs/9p/vfs_dir.c‎

Lines changed: 67 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,24 @@
3939
#include "v9fs_vfs.h"
4040
#include "fid.h"
4141

42+
/**
43+
* struct p9_rdir - readdir accounting
44+
* @mutex: mutex protecting readdir
45+
* @head: start offset of current dirread buffer
46+
* @tail: end offset of current dirread buffer
47+
* @buf: dirread buffer
48+
*
49+
* private structure for keeping track of readdir
50+
* allocated on demand
51+
*/
52+
53+
struct p9_rdir {
54+
struct mutex mutex;
55+
int head;
56+
int tail;
57+
uint8_t *buf;
58+
};
59+
4260
/**
4361
* dt_type - return file type
4462
* @mistat: mistat structure
@@ -70,56 +88,79 @@ static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
7088
{
7189
int over;
7290
struct p9_wstat st;
73-
int err;
91+
int err = 0;
7492
struct p9_fid *fid;
7593
int buflen;
76-
char *statbuf;
77-
int n, i = 0;
94+
int reclen = 0;
95+
struct p9_rdir *rdir;
7896

7997
P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
8098
fid = filp->private_data;
8199

82100
buflen = fid->clnt->msize - P9_IOHDRSZ;
83-
statbuf = kmalloc(buflen, GFP_KERNEL);
84-
if (!statbuf)
85-
return -ENOMEM;
86-
87-
while (1) {
88-
err = v9fs_file_readn(filp, statbuf, NULL, buflen,
89-
fid->rdir_fpos);
90-
if (err <= 0)
91-
break;
92-
93-
n = err;
94-
while (i < n) {
95-
err = p9stat_read(statbuf + i, buflen-i, &st,
96-
fid->clnt->dotu);
101+
102+
/* allocate rdir on demand */
103+
if (!fid->rdir) {
104+
rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
105+
106+
if (rdir == NULL) {
107+
err = -ENOMEM;
108+
goto exit;
109+
}
110+
spin_lock(&filp->f_dentry->d_lock);
111+
if (!fid->rdir) {
112+
rdir->buf = (uint8_t *)rdir + sizeof(struct p9_rdir);
113+
mutex_init(&rdir->mutex);
114+
rdir->head = rdir->tail = 0;
115+
fid->rdir = (void *) rdir;
116+
rdir = NULL;
117+
}
118+
spin_unlock(&filp->f_dentry->d_lock);
119+
kfree(rdir);
120+
}
121+
rdir = (struct p9_rdir *) fid->rdir;
122+
123+
err = mutex_lock_interruptible(&rdir->mutex);
124+
while (err == 0) {
125+
if (rdir->tail == rdir->head) {
126+
err = v9fs_file_readn(filp, rdir->buf, NULL,
127+
buflen, filp->f_pos);
128+
if (err <= 0)
129+
goto unlock_and_exit;
130+
131+
rdir->head = 0;
132+
rdir->tail = err;
133+
}
134+
135+
while (rdir->head < rdir->tail) {
136+
err = p9stat_read(rdir->buf + rdir->head,
137+
buflen - rdir->head, &st,
138+
fid->clnt->dotu);
97139
if (err) {
98140
P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
99141
err = -EIO;
100142
p9stat_free(&st);
101-
goto free_and_exit;
143+
goto unlock_and_exit;
102144
}
103-
104-
i += st.size+2;
105-
fid->rdir_fpos += st.size+2;
145+
reclen = st.size+2;
106146

107147
over = filldir(dirent, st.name, strlen(st.name),
108148
filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
109149

110-
filp->f_pos += st.size+2;
111-
112150
p9stat_free(&st);
113151

114152
if (over) {
115153
err = 0;
116-
goto free_and_exit;
154+
goto unlock_and_exit;
117155
}
156+
rdir->head += reclen;
157+
filp->f_pos += reclen;
118158
}
119159
}
120160

121-
free_and_exit:
122-
kfree(statbuf);
161+
unlock_and_exit:
162+
mutex_unlock(&rdir->mutex);
163+
exit:
123164
return err;
124165
}
125166

‎fs/9p/vfs_inode.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -994,8 +994,7 @@ static int v9fs_readlink(struct dentry *dentry, char *buffer, int buflen)
994994
P9_DPRINTK(P9_DEBUG_VFS,
995995
"%s -> %s (%s)\n", dentry->d_name.name, st->extension, buffer);
996996

997-
retval = buflen;
998-
997+
retval = strnlen(buffer, buflen);
999998
done:
1000999
kfree(st);
10011000
return retval;
@@ -1062,7 +1061,7 @@ static void *v9fs_vfs_follow_link(struct dentry *dentry, struct nameidata *nd)
10621061
__putname(link);
10631062
link = ERR_PTR(len);
10641063
} else
1065-
link[len] = 0;
1064+
link[min(len, PATH_MAX-1)] = 0;
10661065
}
10671066
nd_set_link(nd, link);
10681067

‎include/net/9p/client.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,7 @@ struct p9_client {
159159
* @qid: the &p9_qid server identifier this handle points to
160160
* @iounit: the server reported maximum transaction size for this file
161161
* @uid: the numeric uid of the local user who owns this handle
162-
* @aux: transport specific information (unused?)
163-
* @rdir_fpos: tracks offset of file position when reading directory contents
162+
* @rdir: readdir accounting structure (allocated on demand)
164163
* @flist: per-client-instance fid tracking
165164
* @dlist: per-dentry fid tracking
166165
*
@@ -174,9 +173,9 @@ struct p9_fid {
174173
struct p9_qid qid;
175174
u32 iounit;
176175
uid_t uid;
177-
void *aux;
178176

179-
int rdir_fpos;
177+
void *rdir;
178+
180179
struct list_head flist;
181180
struct list_head dlist; /* list of all fids attached to a dentry */
182181
};

‎net/9p/client.c‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,9 @@ static struct p9_fid *p9_fid_create(struct p9_client *clnt)
582582

583583
memset(&fid->qid, 0, sizeof(struct p9_qid));
584584
fid->mode = -1;
585-
fid->rdir_fpos = 0;
586585
fid->uid = current_fsuid();
587586
fid->clnt = clnt;
588-
fid->aux = NULL;
589-
587+
fid->rdir = NULL;
590588
spin_lock_irqsave(&clnt->lock, flags);
591589
list_add(&fid->flist, &clnt->fidlist);
592590
spin_unlock_irqrestore(&clnt->lock, flags);
@@ -609,6 +607,7 @@ static void p9_fid_destroy(struct p9_fid *fid)
609607
spin_lock_irqsave(&clnt->lock, flags);
610608
list_del(&fid->flist);
611609
spin_unlock_irqrestore(&clnt->lock, flags);
610+
kfree(fid->rdir);
612611
kfree(fid);
613612
}
614613

0 commit comments

Comments
 (0)