xref: /openbmc/linux/fs/9p/vfs_dir.c (revision a1117495)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * This file contains vfs directory ops for the 9P2000 protocol.
4  *
5  *  Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
6  *  Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
7  */
8 
9 #include <linux/module.h>
10 #include <linux/errno.h>
11 #include <linux/fs.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 <net/9p/9p.h>
21 #include <net/9p/client.h>
22 
23 #include "v9fs.h"
24 #include "v9fs_vfs.h"
25 #include "fid.h"
26 
27 /**
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
32  *
33  * private structure for keeping track of readdir
34  * allocated on demand
35  */
36 
37 struct p9_rdir {
38 	int head;
39 	int tail;
40 	uint8_t buf[];
41 };
42 
43 /**
44  * dt_type - return file type
45  * @mistat: mistat structure
46  *
47  */
48 
49 static inline int dt_type(struct p9_wstat *mistat)
50 {
51 	unsigned long perm = mistat->mode;
52 	int rettype = DT_REG;
53 
54 	if (perm & P9_DMDIR)
55 		rettype = DT_DIR;
56 	if (perm & P9_DMSYMLINK)
57 		rettype = DT_LNK;
58 
59 	return rettype;
60 }
61 
62 /**
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
66  *
67  */
68 
69 static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
70 {
71 	struct p9_fid *fid = filp->private_data;
72 
73 	if (!fid->rdir)
74 		fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
75 	return fid->rdir;
76 }
77 
78 /**
79  * v9fs_dir_readdir - iterate through a directory
80  * @file: opened file structure
81  * @ctx: actor we feed the entries to
82  *
83  */
84 
85 static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
86 {
87 	bool over;
88 	struct p9_wstat st;
89 	int err = 0;
90 	struct p9_fid *fid;
91 	int buflen;
92 	struct p9_rdir *rdir;
93 	struct kvec kvec;
94 
95 	p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
96 	fid = file->private_data;
97 
98 	buflen = fid->clnt->msize - P9_IOHDRSZ;
99 
100 	rdir = v9fs_alloc_rdir_buf(file, buflen);
101 	if (!rdir)
102 		return -ENOMEM;
103 	kvec.iov_base = rdir->buf;
104 	kvec.iov_len = buflen;
105 
106 	while (1) {
107 		if (rdir->tail == rdir->head) {
108 			struct iov_iter to;
109 			int n;
110 
111 			iov_iter_kvec(&to, READ, &kvec, 1, buflen);
112 			n = p9_client_read(file->private_data, ctx->pos, &to,
113 					   &err);
114 			if (err)
115 				return err;
116 			if (n == 0)
117 				return 0;
118 
119 			rdir->head = 0;
120 			rdir->tail = n;
121 		}
122 		while (rdir->head < rdir->tail) {
123 			err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
124 					  rdir->tail - rdir->head, &st);
125 			if (err <= 0) {
126 				p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
127 				return -EIO;
128 			}
129 
130 			over = !dir_emit(ctx, st.name, strlen(st.name),
131 					 v9fs_qid2ino(&st.qid), dt_type(&st));
132 			p9stat_free(&st);
133 			if (over)
134 				return 0;
135 
136 			rdir->head += err;
137 			ctx->pos += err;
138 		}
139 	}
140 }
141 
142 /**
143  * v9fs_dir_readdir_dotl - iterate through a directory
144  * @file: opened file structure
145  * @ctx: actor we feed the entries to
146  *
147  */
148 static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
149 {
150 	int err = 0;
151 	struct p9_fid *fid;
152 	int buflen;
153 	struct p9_rdir *rdir;
154 	struct p9_dirent curdirent;
155 
156 	p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
157 	fid = file->private_data;
158 
159 	buflen = fid->clnt->msize - P9_READDIRHDRSZ;
160 
161 	rdir = v9fs_alloc_rdir_buf(file, buflen);
162 	if (!rdir)
163 		return -ENOMEM;
164 
165 	while (1) {
166 		if (rdir->tail == rdir->head) {
167 			err = p9_client_readdir(fid, rdir->buf, buflen,
168 						ctx->pos);
169 			if (err <= 0)
170 				return err;
171 
172 			rdir->head = 0;
173 			rdir->tail = err;
174 		}
175 
176 		while (rdir->head < rdir->tail) {
177 
178 			err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
179 					    rdir->tail - rdir->head,
180 					    &curdirent);
181 			if (err < 0) {
182 				p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
183 				return -EIO;
184 			}
185 
186 			if (!dir_emit(ctx, curdirent.d_name,
187 				      strlen(curdirent.d_name),
188 				      v9fs_qid2ino(&curdirent.qid),
189 				      curdirent.d_type))
190 				return 0;
191 
192 			ctx->pos = curdirent.d_off;
193 			rdir->head += err;
194 		}
195 	}
196 }
197 
198 
199 /**
200  * v9fs_dir_release - close a directory
201  * @inode: inode of the directory
202  * @filp: file pointer to a directory
203  *
204  */
205 
206 int v9fs_dir_release(struct inode *inode, struct file *filp)
207 {
208 	struct p9_fid *fid;
209 
210 	fid = filp->private_data;
211 	p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
212 		 inode, filp, fid ? fid->fid : -1);
213 	if (fid) {
214 		spin_lock(&inode->i_lock);
215 		hlist_del(&fid->ilist);
216 		spin_unlock(&inode->i_lock);
217 		p9_client_clunk(fid);
218 	}
219 	return 0;
220 }
221 
222 const struct file_operations v9fs_dir_operations = {
223 	.read = generic_read_dir,
224 	.llseek = generic_file_llseek,
225 	.iterate_shared = v9fs_dir_readdir,
226 	.open = v9fs_file_open,
227 	.release = v9fs_dir_release,
228 };
229 
230 const struct file_operations v9fs_dir_operations_dotl = {
231 	.read = generic_read_dir,
232 	.llseek = generic_file_llseek,
233 	.iterate_shared = v9fs_dir_readdir_dotl,
234 	.open = v9fs_file_open,
235 	.release = v9fs_dir_release,
236 	.fsync = v9fs_file_fsync_dotl,
237 };
238