blob: 8c854d8cb0cd2ce1d5434d859a7ac2754ade9dc7 [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/*
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07003 * 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>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -07007 */
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>
Al Viro914e2632006-10-18 13:55:46 -040015#include <linux/sched.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070016#include <linux/inet.h>
17#include <linux/idr.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Al Viroe1200fe62015-04-01 23:42:28 -040019#include <linux/uio.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050020#include <net/9p/9p.h>
21#include <net/9p/client.h>
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070022
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070023#include "v9fs.h"
Latchesar Ionkov531b1092006-01-08 01:05:00 -080024#include "v9fs_vfs.h"
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070025#include "fid.h"
26
27/**
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060028 * struct p9_rdir - readdir accounting
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060029 * @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
37struct p9_rdir {
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060038 int head;
39 int tail;
Al Viro7ffdea72013-01-26 00:11:36 +000040 uint8_t buf[];
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060041};
42
43/**
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070044 * dt_type - return file type
45 * @mistat: mistat structure
46 *
47 */
48
Eric Van Hensbergen02da3982008-10-16 08:29:30 -050049static inline int dt_type(struct p9_wstat *mistat)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070050{
51 unsigned long perm = mistat->mode;
52 int rettype = DT_REG;
53
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050054 if (perm & P9_DMDIR)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070055 rettype = DT_DIR;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050056 if (perm & P9_DMSYMLINK)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070057 rettype = DT_LNK;
58
59 return rettype;
60}
61
62/**
Sripathi Kodi7751bdb2010-06-04 13:41:26 +000063 * 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
Al Viro7ffdea72013-01-26 00:11:36 +000069static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
Sripathi Kodi7751bdb2010-06-04 13:41:26 +000070{
Al Viro7ffdea72013-01-26 00:11:36 +000071 struct p9_fid *fid = filp->private_data;
Sohaib Mohamed6d66ffc2021-10-01 01:55:03 +020072
Al Viro7ffdea72013-01-26 00:11:36 +000073 if (!fid->rdir)
74 fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
75 return fid->rdir;
Sripathi Kodi7751bdb2010-06-04 13:41:26 +000076}
77
78/**
Al Viro8f298432013-05-17 17:51:41 -040079 * v9fs_dir_readdir - iterate through a directory
80 * @file: opened file structure
81 * @ctx: actor we feed the entries to
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070082 *
83 */
84
Al Viro8f298432013-05-17 17:51:41 -040085static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070086{
Al Viro8f298432013-05-17 17:51:41 -040087 bool over;
Eric Van Hensbergen02da3982008-10-16 08:29:30 -050088 struct p9_wstat st;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060089 int err = 0;
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050090 struct p9_fid *fid;
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -050091 int buflen;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -060092 struct p9_rdir *rdir;
Al Viroe1200fe62015-04-01 23:42:28 -040093 struct kvec kvec;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070094
Al Viro4b8e9922014-08-19 20:17:38 -040095 p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
Al Viro8f298432013-05-17 17:51:41 -040096 fid = file->private_data;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070097
Eric Van Hensbergen06b55b42008-10-13 20:36:15 -050098 buflen = fid->clnt->msize - P9_IOHDRSZ;
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -070099
Al Viro8f298432013-05-17 17:51:41 -0400100 rdir = v9fs_alloc_rdir_buf(file, buflen);
Al Viro7ffdea72013-01-26 00:11:36 +0000101 if (!rdir)
102 return -ENOMEM;
Al Viroe1200fe62015-04-01 23:42:28 -0400103 kvec.iov_base = rdir->buf;
104 kvec.iov_len = buflen;
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600105
Al Viro7ffdea72013-01-26 00:11:36 +0000106 while (1) {
Eric Van Hensbergen3e2796a2009-11-02 08:39:28 -0600107 if (rdir->tail == rdir->head) {
Al Viroe1200fe62015-04-01 23:42:28 -0400108 struct iov_iter to;
109 int n;
Sohaib Mohamed6d66ffc2021-10-01 01:55:03 +0200110
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);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800213 if (fid) {
214 spin_lock(&inode->i_lock);
215 hlist_del(&fid->ilist);
216 spin_unlock(&inode->i_lock);
jvrao62726a72010-08-25 16:26:21 +0000217 p9_client_clunk(fid);
Jianyong Wu6636b6d2020-09-23 22:11:46 +0800218 }
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700219 return 0;
220}
221
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -0800222const struct file_operations v9fs_dir_operations = {
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700223 .read = generic_read_dir,
Al Viro59af1582008-08-24 07:24:41 -0400224 .llseek = generic_file_llseek,
Al Viro5963ded2016-05-01 00:08:03 -0400225 .iterate_shared = v9fs_dir_readdir,
Eric Van Hensbergene69e7fe2005-09-09 13:04:18 -0700226 .open = v9fs_file_open,
227 .release = v9fs_dir_release,
228};
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000229
230const struct file_operations v9fs_dir_operations_dotl = {
231 .read = generic_read_dir,
232 .llseek = generic_file_llseek,
Al Viro5963ded2016-05-01 00:08:03 -0400233 .iterate_shared = v9fs_dir_readdir_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000234 .open = v9fs_file_open,
235 .release = v9fs_dir_release,
Sohaib Mohamed6d66ffc2021-10-01 01:55:03 +0200236 .fsync = v9fs_file_fsync_dotl,
Sripathi Kodi9b6533c2010-03-25 12:41:54 +0000237};