]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/9p/vfs_dir.c
[PATCH] v9fs: new multiplexer implementation
[net-next-2.6.git] / fs / 9p / vfs_dir.c
CommitLineData
e69e7fe5
EVH
1/*
2 * linux/fs/9p/vfs_dir.c
3 *
4 * This file contains vfs directory ops for the 9P2000 protocol.
5 *
6 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
7 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to:
21 * Free Software Foundation
22 * 51 Franklin Street, Fifth Floor
23 * Boston, MA 02111-1301 USA
24 *
25 */
26
27#include <linux/module.h>
28#include <linux/errno.h>
29#include <linux/fs.h>
30#include <linux/file.h>
31#include <linux/stat.h>
32#include <linux/string.h>
33#include <linux/smp_lock.h>
34#include <linux/inet.h>
35#include <linux/idr.h>
36
37#include "debug.h"
38#include "v9fs.h"
39#include "9p.h"
40#include "v9fs_vfs.h"
41#include "conv.h"
42#include "fid.h"
43
44/**
45 * dt_type - return file type
46 * @mistat: mistat structure
47 *
48 */
49
50static inline int dt_type(struct v9fs_stat *mistat)
51{
52 unsigned long perm = mistat->mode;
53 int rettype = DT_REG;
54
55 if (perm & V9FS_DMDIR)
56 rettype = DT_DIR;
57 if (perm & V9FS_DMSYMLINK)
58 rettype = DT_LNK;
59
60 return rettype;
61}
62
63/**
64 * v9fs_dir_readdir - read a directory
65 * @filep: opened file structure
66 * @dirent: directory structure ???
67 * @filldir: function to populate directory structure ???
68 *
69 */
70
71static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
72{
73 struct v9fs_fcall *fcall = NULL;
74 struct inode *inode = filp->f_dentry->d_inode;
75 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
76 struct v9fs_fid *file = filp->private_data;
3cf6429a 77 unsigned int i, n, s;
e69e7fe5
EVH
78 int fid = -1;
79 int ret = 0;
80 struct v9fs_stat *mi = NULL;
81 int over = 0;
82
83 dprintk(DEBUG_VFS, "name %s\n", filp->f_dentry->d_name.name);
84
85 fid = file->fid;
86
87 mi = kmalloc(v9ses->maxdata, GFP_KERNEL);
88 if (!mi)
89 return -ENOMEM;
90
91 if (file->rdir_fcall && (filp->f_pos != file->rdir_pos)) {
92 kfree(file->rdir_fcall);
93 file->rdir_fcall = NULL;
94 }
95
96 if (file->rdir_fcall) {
97 n = file->rdir_fcall->params.rread.count;
98 i = file->rdir_fpos;
99 while (i < n) {
3cf6429a
LI
100 s = v9fs_deserialize_stat(
101 file->rdir_fcall->params.rread.data + i,
102 n - i, mi, v9ses->maxdata, v9ses->extended);
e69e7fe5
EVH
103
104 if (s == 0) {
105 dprintk(DEBUG_ERROR,
106 "error while deserializing mistat\n");
107 ret = -EIO;
108 goto FreeStructs;
109 }
110
111 over = filldir(dirent, mi->name, strlen(mi->name),
112 filp->f_pos, v9fs_qid2ino(&mi->qid),
113 dt_type(mi));
114
115 if (over) {
116 file->rdir_fpos = i;
117 file->rdir_pos = filp->f_pos;
118 break;
119 }
120
121 i += s;
122 filp->f_pos += s;
123 }
124
125 if (!over) {
126 kfree(file->rdir_fcall);
127 file->rdir_fcall = NULL;
128 }
129 }
130
131 while (!over) {
132 ret = v9fs_t_read(v9ses, fid, filp->f_pos,
133 v9ses->maxdata-V9FS_IOHDRSZ, &fcall);
134 if (ret < 0) {
135 dprintk(DEBUG_ERROR, "error while reading: %d: %p\n",
136 ret, fcall);
137 goto FreeStructs;
138 } else if (ret == 0)
139 break;
140
141 n = ret;
142 i = 0;
143 while (i < n) {
3cf6429a
LI
144 s = v9fs_deserialize_stat(fcall->params.rread.data + i,
145 n - i, mi, v9ses->maxdata, v9ses->extended);
e69e7fe5
EVH
146
147 if (s == 0) {
148 dprintk(DEBUG_ERROR,
149 "error while deserializing mistat\n");
150 return -EIO;
151 }
152
153 over = filldir(dirent, mi->name, strlen(mi->name),
154 filp->f_pos, v9fs_qid2ino(&mi->qid),
155 dt_type(mi));
156
157 if (over) {
158 file->rdir_fcall = fcall;
159 file->rdir_fpos = i;
160 file->rdir_pos = filp->f_pos;
161 fcall = NULL;
162 break;
163 }
164
165 i += s;
166 filp->f_pos += s;
167 }
168
169 kfree(fcall);
170 }
171
172 FreeStructs:
173 kfree(fcall);
174 kfree(mi);
175 return ret;
176}
177
178/**
179 * v9fs_dir_release - close a directory
180 * @inode: inode of the directory
181 * @filp: file pointer to a directory
182 *
183 */
184
185int v9fs_dir_release(struct inode *inode, struct file *filp)
186{
187 struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
188 struct v9fs_fid *fid = filp->private_data;
189 int fidnum = -1;
190
191 dprintk(DEBUG_VFS, "inode: %p filp: %p fid: %d\n", inode, filp,
192 fid->fid);
193 fidnum = fid->fid;
194
28fd1298 195 filemap_write_and_wait(inode->i_mapping);
e69e7fe5
EVH
196
197 if (fidnum >= 0) {
e69e7fe5
EVH
198 dprintk(DEBUG_VFS, "fidopen: %d v9f->fid: %d\n", fid->fidopen,
199 fid->fid);
200
3cf6429a 201 if (v9fs_t_clunk(v9ses, fidnum))
0b8dd177 202 dprintk(DEBUG_ERROR, "clunk failed\n");
e69e7fe5 203
e69e7fe5 204 kfree(fid->rdir_fcall);
0b8dd177 205 kfree(fid);
e69e7fe5
EVH
206
207 filp->private_data = NULL;
e69e7fe5
EVH
208 }
209
210 d_drop(filp->f_dentry);
211 return 0;
212}
213
214struct file_operations v9fs_dir_operations = {
215 .read = generic_read_dir,
216 .readdir = v9fs_dir_readdir,
217 .open = v9fs_file_open,
218 .release = v9fs_dir_release,
219};