1 // SPDX-License-Identifier: GPL-2.0-only
3 * This file contains vfs directory ops for the 9P2000 protocol.
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/slab.h>
18 #include <linux/uio.h>
19 #include <linux/fscache.h>
20 #include <net/9p/9p.h>
21 #include <net/9p/client.h>
28 * struct p9_rdir - readdir accounting
29 * @head: start offset of current dirread buffer
30 * @tail: end offset of current dirread buffer
31 * @buf: dirread buffer
33 * private structure for keeping track of readdir
44 * dt_type - return file type
45 * @mistat: mistat structure
49 static inline int dt_type(struct p9_wstat *mistat)
51 unsigned long perm = mistat->mode;
56 if (perm & P9_DMSYMLINK)
63 * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
64 * @filp: opened file structure
65 * @buflen: Length in bytes of buffer to allocate
69 static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
71 struct p9_fid *fid = filp->private_data;
74 fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
79 * v9fs_dir_readdir - iterate through a directory
80 * @file: opened file structure
81 * @ctx: actor we feed the entries to
85 static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
95 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
96 fid = file->private_data;
98 buflen = fid->clnt->msize - P9_IOHDRSZ;
100 rdir = v9fs_alloc_rdir_buf(file, buflen);
103 kvec.iov_base = rdir->buf;
104 kvec.iov_len = buflen;
107 if (rdir->tail == rdir->head) {
111 iov_iter_kvec(&to, ITER_DEST, &kvec, 1, buflen);
112 n = p9_client_read(file->private_data, ctx->pos, &to,
122 while (rdir->head < rdir->tail) {
123 err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
124 rdir->tail - rdir->head, &st);
126 p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
130 over = !dir_emit(ctx, st.name, strlen(st.name),
131 v9fs_qid2ino(&st.qid), dt_type(&st));
143 * v9fs_dir_readdir_dotl - iterate through a directory
144 * @file: opened file structure
145 * @ctx: actor we feed the entries to
148 static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
153 struct p9_rdir *rdir;
154 struct p9_dirent curdirent;
156 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
157 fid = file->private_data;
159 buflen = fid->clnt->msize - P9_READDIRHDRSZ;
161 rdir = v9fs_alloc_rdir_buf(file, buflen);
166 if (rdir->tail == rdir->head) {
167 err = p9_client_readdir(fid, rdir->buf, buflen,
176 while (rdir->head < rdir->tail) {
178 err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
179 rdir->tail - rdir->head,
182 p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
186 if (!dir_emit(ctx, curdirent.d_name,
187 strlen(curdirent.d_name),
188 v9fs_qid2ino(&curdirent.qid),
192 ctx->pos = curdirent.d_off;
200 * v9fs_dir_release - called on a close of a file or directory
201 * @inode: inode of the directory
202 * @filp: file pointer to a directory
206 int v9fs_dir_release(struct inode *inode, struct file *filp)
208 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);
221 retval = p9_fid_put(fid);
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,