blob: 674d22bf4f6f75465ad8274c3dde5ba1673cbc3e [file] [log] [blame]
Thomas Gleixner1f327612019-05-28 09:57:16 -07001// SPDX-License-Identifier: GPL-2.0-only
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07002/*
3 * linux/fs/9p/vfs_dir.c
4 *
5 * This file contains vfs directory ops for the 9P2000 protocol.
6 *
7 * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
8 * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07009 */
10
11#include <linux/module.h>
12#include <linux/errno.h>
13#include <linux/fs.h>
14#include <linux/file.h>
15#include <linux/stat.h>
16#include <linux/string.h>
Al Viro914e2632006-10-18 13:55:46 -040017#include <linux/sched.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070018#include <linux/inet.h>
19#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Al Viroe1200fe62015-04-01 23:42:28 -040021#include <linux/uio.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050022#include <net/9p/9p.h>
23#include <net/9p/client.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070024
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070025#include "v9fs.h"
Latchesar Ionkov531b1092006-01-08 01:05:00 -080026#include "v9fs_vfs.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070027#include "fid.h"
28
29/**
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060030 * struct p9_rdir - readdir accounting
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060031 * @head: start offset of current dirread buffer
32 * @tail: end offset of current dirread buffer
33 * @buf: dirread buffer
34 *
35 * private structure for keeping track of readdir
36 * allocated on demand
37 */
38
39struct p9_rdir {
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060040 int head;
41 int tail;
Al Viro7ffdea72013-01-26 00:11:36 +000042 uint8_t buf[];
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060043};
44
45/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070046 * dt_type - return file type
47 * @mistat: mistat structure
48 *
49 */
50
Eric Van Hensbergen02da3982008-10-16 08:29:30 -050051static inline int dt_type(struct p9_wstat *mistat)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070052{
53 unsigned long perm = mistat->mode;
54 int rettype = DT_REG;
55
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050056 if (perm & P9_DMDIR)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070057 rettype = DT_DIR;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050058 if (perm & P9_DMSYMLINK)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070059 rettype = DT_LNK;
60
61 return rettype;
62}
63
64/**
Sripathi Kodi7751bdb2010-06-04 13:41:26 +000065 * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
66 * @filp: opened file structure
67 * @buflen: Length in bytes of buffer to allocate
68 *
69 */
70
Al Viro7ffdea72013-01-26 00:11:36 +000071static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +000072{
Al Viro7ffdea72013-01-26 00:11:36 +000073 struct p9_fid *fid = filp->private_data;
74 if (!fid->rdir)
75 fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
76 return fid->rdir;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +000077}
78
79/**
Al Viro8f298432013-05-17 17:51:41 -040080 * v9fs_dir_readdir - iterate through a directory
81 * @file: opened file structure
82 * @ctx: actor we feed the entries to
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070083 *
84 */
85
Al Viro8f298432013-05-17 17:51:41 -040086static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070087{
Al Viro8f298432013-05-17 17:51:41 -040088 bool over;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -050089 struct p9_wstat st;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060090 int err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050091 struct p9_fid *fid;
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -050092 int buflen;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060093 struct p9_rdir *rdir;
Al Viroe1200fe62015-04-01 23:42:28 -040094 struct kvec kvec;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070095
Al Viro4b8e9922014-08-19 20:17:38 -040096 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
Al Viro8f298432013-05-17 17:51:41 -040097 fid = file->private_data;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070098
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -050099 buflen = fid->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700100
Al Viro8f298432013-05-17 17:51:41 -0400101 rdir = v9fs_alloc_rdir_buf(file, buflen);
Al Viro7ffdea72013-01-26 00:11:36 +0000102 if (!rdir)
103 return -ENOMEM;
Al Viroe1200fe62015-04-01 23:42:28 -0400104 kvec.iov_base = rdir->buf;
105 kvec.iov_len = buflen;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600106
Al Viro7ffdea72013-01-26 00:11:36 +0000107 while (1) {
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600108 if (rdir->tail == rdir->head) {
Al Viroe1200fe62015-04-01 23:42:28 -0400109 struct iov_iter to;
110 int n;
David Howellsaa563d72018-10-20 00:57:56 +0100111 iov_iter_kvec(&to, READ, &kvec, 1, buflen);
Al Viroe1200fe62015-04-01 23:42:28 -0400112 n = p9_client_read(file->private_data, ctx->pos, &to,
113 &err);
114 if (err)
Al Viro7ffdea72013-01-26 00:11:36 +0000115 return err;
Johannes Berg8e3c5002015-04-22 11:55:14 +0200116 if (n == 0)
117 return 0;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600118
119 rdir->head = 0;
Al Viroe1200fe62015-04-01 23:42:28 -0400120 rdir->tail = n;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600121 }
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600122 while (rdir->head < rdir->tail) {
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530123 err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
124 rdir->tail - rdir->head, &st);
Gertjan Halkes2803cf42018-09-05 15:41:29 +0900125 if (err <= 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800126 p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
Al Viro7ffdea72013-01-26 00:11:36 +0000127 return -EIO;
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -0500128 }
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -0500129
Al Viro8f298432013-05-17 17:51:41 -0400130 over = !dir_emit(ctx, st.name, strlen(st.name),
131 v9fs_qid2ino(&st.qid), dt_type(&st));
Eric Van Hensbergen02da3982008-10-16 08:29:30 -0500132 p9stat_free(&st);
Al Viro7ffdea72013-01-26 00:11:36 +0000133 if (over)
134 return 0;
135
Gertjan Halkes2803cf42018-09-05 15:41:29 +0900136 rdir->head += err;
137 ctx->pos += err;
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -0500138 }
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700139 }
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700140}
141
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000142/**
Al Viro8f298432013-05-17 17:51:41 -0400143 * v9fs_dir_readdir_dotl - iterate through a directory
144 * @file: opened file structure
145 * @ctx: actor we feed the entries to
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000146 *
147 */
Al Viro8f298432013-05-17 17:51:41 -0400148static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000149{
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000150 int err = 0;
151 struct p9_fid *fid;
152 int buflen;
153 struct p9_rdir *rdir;
154 struct p9_dirent curdirent;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000155
Al Viro4b8e9922014-08-19 20:17:38 -0400156 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
Al Viro8f298432013-05-17 17:51:41 -0400157 fid = file->private_data;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000158
159 buflen = fid->clnt->msize - P9_READDIRHDRSZ;
160
Al Viro8f298432013-05-17 17:51:41 -0400161 rdir = v9fs_alloc_rdir_buf(file, buflen);
Al Viro7ffdea72013-01-26 00:11:36 +0000162 if (!rdir)
163 return -ENOMEM;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000164
Al Viro7ffdea72013-01-26 00:11:36 +0000165 while (1) {
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000166 if (rdir->tail == rdir->head) {
167 err = p9_client_readdir(fid, rdir->buf, buflen,
Al Viro8f298432013-05-17 17:51:41 -0400168 ctx->pos);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000169 if (err <= 0)
Al Viro7ffdea72013-01-26 00:11:36 +0000170 return err;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000171
172 rdir->head = 0;
173 rdir->tail = err;
174 }
175
176 while (rdir->head < rdir->tail) {
177
Aneesh Kumar K.V348b5902011-08-07 00:46:59 +0530178 err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
179 rdir->tail - rdir->head,
180 &curdirent);
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000181 if (err < 0) {
Joe Perches5d385152011-11-28 10:40:46 -0800182 p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
Al Viro7ffdea72013-01-26 00:11:36 +0000183 return -EIO;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000184 }
185
Al Viro8f298432013-05-17 17:51:41 -0400186 if (!dir_emit(ctx, curdirent.d_name,
187 strlen(curdirent.d_name),
188 v9fs_qid2ino(&curdirent.qid),
189 curdirent.d_type))
Al Viro7ffdea72013-01-26 00:11:36 +0000190 return 0;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000191
Al Viro8f298432013-05-17 17:51:41 -0400192 ctx->pos = curdirent.d_off;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000193 rdir->head += err;
194 }
195 }
Sripathi Kodi7751bdb2010-06-04 13:41:26 +0000196}
197
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500198
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700199/**
200 * v9fs_dir_release - close a directory
201 * @inode: inode of the directory
202 * @filp: file pointer to a directory
203 *
204 */
205
206int v9fs_dir_release(struct inode *inode, struct file *filp)
207{
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500208 struct p9_fid *fid;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700209
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500210 fid = filp->private_data;
Joe Perches5d385152011-11-28 10:40:46 -0800211 p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
212 inode, filp, fid ? fid->fid : -1);
jvrao62726a72010-08-25 16:26:21 +0000213 if (fid)
214 p9_client_clunk(fid);
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700215 return 0;
216}
217
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800218const struct file_operations v9fs_dir_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700219 .read = generic_read_dir,
Al Viro59af1582008-08-24 07:24:41 -0400220 .llseek = generic_file_llseek,
Al Viro5963ded2016-05-01 00:08:03 -0400221 .iterate_shared = v9fs_dir_readdir,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700222 .open = v9fs_file_open,
223 .release = v9fs_dir_release,
224};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000225
226const struct file_operations v9fs_dir_operations_dotl = {
227 .read = generic_read_dir,
228 .llseek = generic_file_llseek,
Al Viro5963ded2016-05-01 00:08:03 -0400229 .iterate_shared = v9fs_dir_readdir_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000230 .open = v9fs_file_open,
231 .release = v9fs_dir_release,
Venkateswararao Jujjuri (JV)b165d602010-10-22 10:13:12 -0700232 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000233};