vfs_dir.c 5.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * linux/fs/9p/vfs_dir.c
 *
 * This file contains vfs directory ops for the 9P2000 protocol.
 *
 *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
 *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
 *
 *  This program is free software; you can redistribute it and/or modify
10 11
 *  it under the terms of the GNU General Public License version 2
 *  as published by the Free Software Foundation.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to:
 *  Free Software Foundation
 *  51 Franklin Street, Fifth Floor
 *  Boston, MA  02111-1301  USA
 *
 */

#include <linux/module.h>
#include <linux/errno.h>
#include <linux/fs.h>
#include <linux/file.h>
#include <linux/stat.h>
#include <linux/string.h>
32
#include <linux/sched.h>
33 34
#include <linux/inet.h>
#include <linux/idr.h>
35
#include <linux/slab.h>
36
#include <linux/uio.h>
37 38
#include <net/9p/9p.h>
#include <net/9p/client.h>
39 40

#include "v9fs.h"
41
#include "v9fs_vfs.h"
42 43
#include "fid.h"

E
Eric Van Hensbergen 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56
/**
 * struct p9_rdir - readdir accounting
 * @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 {
	int head;
	int tail;
A
Al Viro 已提交
57
	uint8_t buf[];
E
Eric Van Hensbergen 已提交
58 59
};

60 61 62 63 64 65
/**
 * dt_type - return file type
 * @mistat: mistat structure
 *
 */

66
static inline int dt_type(struct p9_wstat *mistat)
67 68 69 70
{
	unsigned long perm = mistat->mode;
	int rettype = DT_REG;

71
	if (perm & P9_DMDIR)
72
		rettype = DT_DIR;
73
	if (perm & P9_DMSYMLINK)
74 75 76 77 78 79
		rettype = DT_LNK;

	return rettype;
}

/**
80
 * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
E
Eric Van Hensbergen 已提交
81
 * @filp: opened file structure
82
 * @buflen: Length in bytes of buffer to allocate
83 84 85
 *
 */

A
Al Viro 已提交
86
static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
87
{
A
Al Viro 已提交
88 89 90 91
	struct p9_fid *fid = filp->private_data;
	if (!fid->rdir)
		fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
	return fid->rdir;
92 93 94
}

/**
A
Al Viro 已提交
95 96 97
 * v9fs_dir_readdir - iterate through a directory
 * @file: opened file structure
 * @ctx: actor we feed the entries to
98 99 100
 *
 */

A
Al Viro 已提交
101
static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
102
{
A
Al Viro 已提交
103
	bool over;
104 105 106 107 108 109
	struct p9_wstat st;
	int err = 0;
	struct p9_fid *fid;
	int buflen;
	int reclen = 0;
	struct p9_rdir *rdir;
110
	struct kvec kvec;
111

A
Al Viro 已提交
112
	p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
A
Al Viro 已提交
113
	fid = file->private_data;
114 115 116

	buflen = fid->clnt->msize - P9_IOHDRSZ;

A
Al Viro 已提交
117
	rdir = v9fs_alloc_rdir_buf(file, buflen);
A
Al Viro 已提交
118 119
	if (!rdir)
		return -ENOMEM;
120 121
	kvec.iov_base = rdir->buf;
	kvec.iov_len = buflen;
E
Eric Van Hensbergen 已提交
122

A
Al Viro 已提交
123
	while (1) {
E
Eric Van Hensbergen 已提交
124
		if (rdir->tail == rdir->head) {
125 126 127 128 129 130
			struct iov_iter to;
			int n;
			iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buflen);
			n = p9_client_read(file->private_data, ctx->pos, &to,
					   &err);
			if (err)
A
Al Viro 已提交
131
				return err;
J
Johannes Berg 已提交
132 133
			if (n == 0)
				return 0;
E
Eric Van Hensbergen 已提交
134 135

			rdir->head = 0;
136
			rdir->tail = n;
E
Eric Van Hensbergen 已提交
137 138
		}
		while (rdir->head < rdir->tail) {
139 140
			err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
					  rdir->tail - rdir->head, &st);
141
			if (err) {
142
				p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
A
Al Viro 已提交
143
				return -EIO;
E
Eric Van Hensbergen 已提交
144
			}
E
Eric Van Hensbergen 已提交
145
			reclen = st.size+2;
E
Eric Van Hensbergen 已提交
146

A
Al Viro 已提交
147 148
			over = !dir_emit(ctx, st.name, strlen(st.name),
					 v9fs_qid2ino(&st.qid), dt_type(&st));
149
			p9stat_free(&st);
A
Al Viro 已提交
150 151 152
			if (over)
				return 0;

E
Eric Van Hensbergen 已提交
153
			rdir->head += reclen;
A
Al Viro 已提交
154
			ctx->pos += reclen;
E
Eric Van Hensbergen 已提交
155
		}
156 157 158
	}
}

159
/**
A
Al Viro 已提交
160 161 162
 * v9fs_dir_readdir_dotl - iterate through a directory
 * @file: opened file structure
 * @ctx: actor we feed the entries to
163 164
 *
 */
A
Al Viro 已提交
165
static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
166 167 168 169 170 171 172
{
	int err = 0;
	struct p9_fid *fid;
	int buflen;
	struct p9_rdir *rdir;
	struct p9_dirent curdirent;

A
Al Viro 已提交
173
	p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
A
Al Viro 已提交
174
	fid = file->private_data;
175 176 177

	buflen = fid->clnt->msize - P9_READDIRHDRSZ;

A
Al Viro 已提交
178
	rdir = v9fs_alloc_rdir_buf(file, buflen);
A
Al Viro 已提交
179 180
	if (!rdir)
		return -ENOMEM;
181

A
Al Viro 已提交
182
	while (1) {
183 184
		if (rdir->tail == rdir->head) {
			err = p9_client_readdir(fid, rdir->buf, buflen,
A
Al Viro 已提交
185
						ctx->pos);
186
			if (err <= 0)
A
Al Viro 已提交
187
				return err;
188 189 190 191 192 193 194

			rdir->head = 0;
			rdir->tail = err;
		}

		while (rdir->head < rdir->tail) {

195 196 197
			err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
					    rdir->tail - rdir->head,
					    &curdirent);
198
			if (err < 0) {
199
				p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
A
Al Viro 已提交
200
				return -EIO;
201 202
			}

A
Al Viro 已提交
203 204 205 206
			if (!dir_emit(ctx, curdirent.d_name,
				      strlen(curdirent.d_name),
				      v9fs_qid2ino(&curdirent.qid),
				      curdirent.d_type))
A
Al Viro 已提交
207
				return 0;
208

A
Al Viro 已提交
209
			ctx->pos = curdirent.d_off;
210 211 212 213 214
			rdir->head += err;
		}
	}
}

215

216 217 218 219 220 221 222 223 224
/**
 * v9fs_dir_release - close a directory
 * @inode: inode of the directory
 * @filp: file pointer to a directory
 *
 */

int v9fs_dir_release(struct inode *inode, struct file *filp)
{
225
	struct p9_fid *fid;
226

227
	fid = filp->private_data;
228 229
	p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
		 inode, filp, fid ? fid->fid : -1);
230 231
	if (fid)
		p9_client_clunk(fid);
232 233 234
	return 0;
}

235
const struct file_operations v9fs_dir_operations = {
236
	.read = generic_read_dir,
237
	.llseek = generic_file_llseek,
A
Al Viro 已提交
238
	.iterate_shared = v9fs_dir_readdir,
239 240 241
	.open = v9fs_file_open,
	.release = v9fs_dir_release,
};
242 243 244 245

const struct file_operations v9fs_dir_operations_dotl = {
	.read = generic_read_dir,
	.llseek = generic_file_llseek,
A
Al Viro 已提交
246
	.iterate_shared = v9fs_dir_readdir_dotl,
247 248
	.open = v9fs_file_open,
	.release = v9fs_dir_release,
249
        .fsync = v9fs_file_fsync_dotl,
250
};