xref: /openbmc/linux/fs/9p/vfs_dir.c (revision a266ef69)
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/slab.h>
18 #include <linux/uio.h>
19 #include <linux/fscache.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, ITER_DEST, &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 v9fs_inode *v9inode = V9FS_I(inode);
209 	struct p9_fid *fid;
210 	__le32 version;
211 	loff_t i_size;
212 
213 	fid = filp->private_data;
214 	p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
215 		 inode, filp, fid ? fid->fid : -1);
216 	if (fid) {
217 		spin_lock(&inode->i_lock);
218 		hlist_del(&fid->ilist);
219 		spin_unlock(&inode->i_lock);
220 		p9_fid_put(fid);
221 	}
222 
223 	if ((filp->f_mode & FMODE_WRITE)) {
224 		version = cpu_to_le32(v9inode->qid.version);
225 		i_size = i_size_read(inode);
226 		fscache_unuse_cookie(v9fs_inode_cookie(v9inode),
227 				     &version, &i_size);
228 	} else {
229 		fscache_unuse_cookie(v9fs_inode_cookie(v9inode), NULL, NULL);
230 	}
231 	return 0;
232 }
233 
234 const struct file_operations v9fs_dir_operations = {
235 	.read = generic_read_dir,
236 	.llseek = generic_file_llseek,
237 	.iterate_shared = v9fs_dir_readdir,
238 	.open = v9fs_file_open,
239 	.release = v9fs_dir_release,
240 };
241 
242 const struct file_operations v9fs_dir_operations_dotl = {
243 	.read = generic_read_dir,
244 	.llseek = generic_file_llseek,
245 	.iterate_shared = v9fs_dir_readdir_dotl,
246 	.open = v9fs_file_open,
247 	.release = v9fs_dir_release,
248 	.fsync = v9fs_file_fsync_dotl,
249 };
250