9p: fix readdir corner cases
[linux-2.6.git] / fs / 9p / vfs_dir.c
index 88e3787..15cce53 100644 (file)
 #include "fid.h"
 
 /**
+ * struct p9_rdir - readdir accounting
+ * @mutex: mutex protecting readdir
+ * @head: start offset of current dirread buffer
+ * @tail: end offset of current dirread buffer
+ * @buf: dirread buffer
+ *
+ * private structure for keeping track of readdir
+ * allocated on demand
+ */
+
+struct p9_rdir {
+       struct mutex mutex;
+       int head;
+       int tail;
+       uint8_t *buf;
+};
+
+/**
  * dt_type - return file type
  * @mistat: mistat structure
  *
  */
 
-static inline int dt_type(struct p9_stat *mistat)
+static inline int dt_type(struct p9_wstat *mistat)
 {
        unsigned long perm = mistat->mode;
        int rettype = DT_REG;
@@ -69,32 +87,81 @@ static inline int dt_type(struct p9_stat *mistat)
 static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
 {
        int over;
+       struct p9_wstat st;
+       int err = 0;
        struct p9_fid *fid;
-       struct v9fs_session_info *v9ses;
-       struct inode *inode;
-       struct p9_stat *st;
+       int buflen;
+       int reclen = 0;
+       struct p9_rdir *rdir;
 
        P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
-       inode = filp->f_path.dentry->d_inode;
-       v9ses = v9fs_inode2v9ses(inode);
        fid = filp->private_data;
-       while ((st = p9_client_dirread(fid, filp->f_pos)) != NULL) {
-               if (IS_ERR(st))
-                       return PTR_ERR(st);
-
-               over = filldir(dirent, st->name.str, st->name.len, filp->f_pos,
-                       v9fs_qid2ino(&st->qid), dt_type(st));
-
-               if (over)
-                       break;
 
-               filp->f_pos += st->size;
-               kfree(st);
-               st = NULL;
+       buflen = fid->clnt->msize - P9_IOHDRSZ;
+
+       /* allocate rdir on demand */
+       if (!fid->rdir) {
+               rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
+
+               if (rdir == NULL) {
+                       err = -ENOMEM;
+                       goto exit;
+               }
+               spin_lock(&filp->f_dentry->d_lock);
+               if (!fid->rdir) {
+                       rdir->buf = (uint8_t *)rdir + sizeof(struct p9_rdir);
+                       mutex_init(&rdir->mutex);
+                       rdir->head = rdir->tail = 0;
+                       fid->rdir = (void *) rdir;
+                       rdir = NULL;
+               }
+               spin_unlock(&filp->f_dentry->d_lock);
+               kfree(rdir);
+       }
+       rdir = (struct p9_rdir *) fid->rdir;
+
+       err = mutex_lock_interruptible(&rdir->mutex);
+       while (err == 0) {
+               if (rdir->tail == rdir->head) {
+                       err = v9fs_file_readn(filp, rdir->buf, NULL,
+                                                       buflen, filp->f_pos);
+                       if (err <= 0)
+                               goto unlock_and_exit;
+
+                       rdir->head = 0;
+                       rdir->tail = err;
+               }
+
+               while (rdir->head < rdir->tail) {
+                       err = p9stat_read(rdir->buf + rdir->head,
+                                               buflen - rdir->head, &st,
+                                               fid->clnt->dotu);
+                       if (err) {
+                               P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
+                               err = -EIO;
+                               p9stat_free(&st);
+                               goto unlock_and_exit;
+                       }
+                       reclen = st.size+2;
+
+                       over = filldir(dirent, st.name, strlen(st.name),
+                           filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
+
+                       p9stat_free(&st);
+
+                       if (over) {
+                               err = 0;
+                               goto unlock_and_exit;
+                       }
+                       rdir->head += reclen;
+                       filp->f_pos += reclen;
+               }
        }
 
-       kfree(st);
-       return 0;
+unlock_and_exit:
+       mutex_unlock(&rdir->mutex);
+exit:
+       return err;
 }
 
 
@@ -119,6 +186,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,
        .readdir = v9fs_dir_readdir,
        .open = v9fs_file_open,
        .release = v9fs_dir_release,