1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains vfs directory ops for the 9P2000 protocol.
5 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
9 #include <linux/module.h>
10 #include <linux/errno.h>
12 #include <linux/file.h>
13 #include <linux/stat.h>
14 #include <linux/string.h>
15 #include <linux/sched.h>
16 #include <linux/inet.h>
17 #include <linux/idr.h>
18 #include <linux/slab.h>
19 #include <linux/uio.h>
20 #include <linux/fscache.h>
21 #include <net/9p/9p.h>
22 #include <net/9p/client.h>
29 * struct p9_rdir - readdir accounting
30 * @head: start offset of current dirread buffer
31 * @tail: end offset of current dirread buffer
32 * @buf: dirread buffer
34 * private structure for keeping track of readdir
45 * dt_type - return file type
46 * @mistat: mistat structure
50 static inline int dt_type(struct p9_wstat *mistat)
52 unsigned long perm = mistat->mode;
57 if (perm & P9_DMSYMLINK)
64 * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
65 * @filp: opened file structure
66 * @buflen: Length in bytes of buffer to allocate
70 static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
72 struct p9_fid *fid = filp->private_data;
75 fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
80 * v9fs_dir_readdir - iterate through a directory
81 * @file: opened file structure
82 * @ctx: actor we feed the entries to
86 static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
96 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
97 fid = file->private_data;
99 buflen = fid->clnt->msize - P9_IOHDRSZ;
101 rdir = v9fs_alloc_rdir_buf(file, buflen);
104 kvec.iov_base = rdir->buf;
105 kvec.iov_len = buflen;
108 if (rdir->tail == rdir->head) {
112 iov_iter_kvec(&to, READ, &kvec, 1, buflen);
113 n = p9_client_read(file->private_data, ctx->pos, &to,
123 while (rdir->head < rdir->tail) {
124 err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
125 rdir->tail - rdir->head, &st);
127 p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
131 over = !dir_emit(ctx, st.name, strlen(st.name),
132 v9fs_qid2ino(&st.qid), dt_type(&st));
144 * v9fs_dir_readdir_dotl - iterate through a directory
145 * @file: opened file structure
146 * @ctx: actor we feed the entries to
149 static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
154 struct p9_rdir *rdir;
155 struct p9_dirent curdirent;
157 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
158 fid = file->private_data;
160 buflen = fid->clnt->msize - P9_READDIRHDRSZ;
162 rdir = v9fs_alloc_rdir_buf(file, buflen);
167 if (rdir->tail == rdir->head) {
168 err = p9_client_readdir(fid, rdir->buf, buflen,
177 while (rdir->head < rdir->tail) {
179 err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
180 rdir->tail - rdir->head,
183 p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
187 if (!dir_emit(ctx, curdirent.d_name,
188 strlen(curdirent.d_name),
189 v9fs_qid2ino(&curdirent.qid),
193 ctx->pos = curdirent.d_off;
201 * v9fs_dir_release - close a directory
202 * @inode: inode of the directory
203 * @filp: file pointer to a directory
207 int v9fs_dir_release(struct inode *inode, struct file *filp)
209 struct v9fs_inode *v9inode = V9FS_I(inode);
214 fid = filp->private_data;
215 p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
216 inode, filp, fid ? fid->fid : -1);
218 spin_lock(&inode->i_lock);
219 hlist_del(&fid->ilist);
220 spin_unlock(&inode->i_lock);
224 if ((filp->f_mode & FMODE_WRITE)) {
225 version = cpu_to_le32(v9inode->qid.version);
226 i_size = i_size_read(inode);
227 fscache_unuse_cookie(v9fs_inode_cookie(v9inode),
230 fscache_unuse_cookie(v9fs_inode_cookie(v9inode), NULL, NULL);
235 const struct file_operations v9fs_dir_operations = {
236 .read = generic_read_dir,
237 .llseek = generic_file_llseek,
238 .iterate_shared = v9fs_dir_readdir,
239 .open = v9fs_file_open,
240 .release = v9fs_dir_release,
243 const struct file_operations v9fs_dir_operations_dotl = {
244 .read = generic_read_dir,
245 .llseek = generic_file_llseek,
246 .iterate_shared = v9fs_dir_readdir_dotl,
247 .open = v9fs_file_open,
248 .release = v9fs_dir_release,
249 .fsync = v9fs_file_fsync_dotl,