9p: use kvzalloc for readdir buffer
PATCH 1/2 9p: use kvzalloc for readdir buffer
The readdir buffer is sized to msize, so kzalloc() can fail under fragmentation with a page allocation failure in v9fs_alloc_rdir_buf() / v9fs_dir_readdir_dotl().
The buffer is only a response sink and is never pack_sg_list()'d, so kvzalloc() is safe for all transports, unlike the fcall buffers fixed in e21d451a82f3.
--- fs/9p/vfs_dir.c | 2 +- net/9p/client.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
fs/9p/vfs_dir.c +1 -1
@@ -70,7 +70,7 @@ static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)struct p9_fid *fid = filp->private_data;if (!fid->rdir) - fid->rdir =kzalloc (sizeof (struct p9_rdir ) + buflen,GFP_KERNEL ); + fid->rdir =kvzalloc (sizeof (struct p9_rdir ) + buflen,GFP_KERNEL );return fid->rdir; }
net/9p/client.c +1 -1
@@ -765,7 +765,7 @@ static void p9_fid_destroy(struct p9_fid *fid)spin_lock_irqsave (&clnt->lock, flags);idr_remove (&clnt->fids, fid->fid);spin_unlock_irqrestore (&clnt->lock, flags); -kfree (fid->rdir); +kvfree (fid->rdir);kfree (fid); }
Signature · 1 line
2.51.0
PATCH 2/2 9p: invalidate readdir buffer on llseek
v9fs_dir_operations{,_dotl} use generic_file_llseek, which updates f_pos but does not invalidate fid->rdir. After an lseek on a directory fd, the next getdents64 continues draining stale cached entries instead of refetching from the server at the new offset. This causes xfstests generic/637 to fail.
Replace generic_file_llseek with a custom v9fs_dir_llseek that resets rdir->head and rdir->tail when the position changes, following the same f_lock pattern used by nfs_llseek_dir.
Also change the readdir refetch condition from == to >= to prevent an infinite loop if lseek races with an in-progress readdir.
--- fs/9p/vfs_dir.c | 53 +++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 4 deletions(-)
fs/9p/vfs_dir.c +49 -4
@@ -103,7 +103,7 @@ static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) kvec.iov_len = buflen;while (1 ) { -if (rdir->tail == rdir->head) { +if (rdir->head >= rdir->tail) {struct iov_iter to;int n;
@@ -162,7 +162,7 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)return -ENOMEM ;while (1 ) { -if (rdir->tail == rdir->head) { +if (rdir->head >= rdir->tail) { err =p9_client_readdir (fid, rdir->buf, buflen, ctx->pos);if (err <=0 )
@@ -195,6 +195,51 @@ static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx) } +/** +* v9fs_dir_llseek - seek on a directory +* @filp: file structure +* @offset: new position +* @whence: SEEK_SET or SEEK_CUR +* +* Invalidate the readdir buffer when the position changes so the next +* getdents call refetches from the server at the new offset. +*/ + +static loff_t v9fs_dir_llseek (struct file *filp,loff_t offset,int whence) +{ +struct p9_fid *fid = filp->private_data; + +switch (whence) { +default : +return -EINVAL ; +case SEEK_SET : +if (offset <0 ) +return -EINVAL ; +spin_lock (&filp->f_lock); +break ; +case SEEK_CUR : +if (offset ==0 ) +return filp->f_pos; +spin_lock (&filp->f_lock); + offset += filp->f_pos; +if (offset <0 ) { +spin_unlock (&filp->f_lock); +return -EINVAL ; + } + } +if (offset != filp->f_pos) { +struct p9_rdir *rdir = fid->rdir; + + filp->f_pos = offset; +if (rdir) { + rdir->head =0 ; + rdir->tail =0 ; + } + } +spin_unlock (&filp->f_lock); +return offset; +} +/** * v9fs_dir_release - close a directory or a file * @inode: inode of the directory or file
@@ -238,7 +283,7 @@ int v9fs_dir_release(struct inode *inode, struct file *filp)const struct file_operations v9fs_dir_operations = { .read = generic_read_dir, - .llseek = generic_file_llseek, + .llseek = v9fs_dir_llseek, .iterate_shared = v9fs_dir_readdir, .open = v9fs_file_open, .release = v9fs_dir_release,
@@ -247,7 +292,7 @@ const struct file_operations v9fs_dir_operations = {const struct file_operations v9fs_dir_operations_dotl = { .read = generic_read_dir, - .llseek = generic_file_llseek, + .llseek = v9fs_dir_llseek, .iterate_shared = v9fs_dir_readdir_dotl, .open = v9fs_file_open, .release = v9fs_dir_release,
Signature · 1 line
2.51.0