Thomas Gleixner | 1f32761 | 2019-05-28 09:57:16 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 2 | /* |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 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> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 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> |
Al Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 15 | #include <linux/sched.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 16 | #include <linux/inet.h> |
| 17 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 18 | #include <linux/slab.h> |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 19 | #include <linux/uio.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 20 | #include <net/9p/9p.h> |
| 21 | #include <net/9p/client.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 22 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 23 | #include "v9fs.h" |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 24 | #include "v9fs_vfs.h" |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 25 | #include "fid.h" |
| 26 | |
| 27 | /** |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 28 | * struct p9_rdir - readdir accounting |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 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 { |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 38 | int head; |
| 39 | int tail; |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 40 | uint8_t buf[]; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /** |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 44 | * dt_type - return file type |
| 45 | * @mistat: mistat structure |
| 46 | * |
| 47 | */ |
| 48 | |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 49 | static inline int dt_type(struct p9_wstat *mistat) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 50 | { |
| 51 | unsigned long perm = mistat->mode; |
| 52 | int rettype = DT_REG; |
| 53 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 54 | if (perm & P9_DMDIR) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 55 | rettype = DT_DIR; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 56 | if (perm & P9_DMSYMLINK) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 57 | rettype = DT_LNK; |
| 58 | |
| 59 | return rettype; |
| 60 | } |
| 61 | |
| 62 | /** |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 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 | |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 69 | static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen) |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 70 | { |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 71 | struct p9_fid *fid = filp->private_data; |
Sohaib Mohamed | 6d66ffc | 2021-10-01 01:55:03 +0200 | [diff] [blame] | 72 | |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 73 | if (!fid->rdir) |
| 74 | fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL); |
| 75 | return fid->rdir; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | /** |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 79 | * v9fs_dir_readdir - iterate through a directory |
| 80 | * @file: opened file structure |
| 81 | * @ctx: actor we feed the entries to |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 82 | * |
| 83 | */ |
| 84 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 85 | static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 86 | { |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 87 | bool over; |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 88 | struct p9_wstat st; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 89 | int err = 0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 90 | struct p9_fid *fid; |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 91 | int buflen; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 92 | struct p9_rdir *rdir; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 93 | struct kvec kvec; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 94 | |
Al Viro | 4b8e992 | 2014-08-19 20:17:38 -0400 | [diff] [blame] | 95 | p9_debug(P9_DEBUG_VFS, "name %pD\n", file); |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 96 | fid = file->private_data; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 97 | |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 98 | buflen = fid->clnt->msize - P9_IOHDRSZ; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 99 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 100 | rdir = v9fs_alloc_rdir_buf(file, buflen); |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 101 | if (!rdir) |
| 102 | return -ENOMEM; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 103 | kvec.iov_base = rdir->buf; |
| 104 | kvec.iov_len = buflen; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 105 | |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 106 | while (1) { |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 107 | if (rdir->tail == rdir->head) { |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 108 | struct iov_iter to; |
| 109 | int n; |
Sohaib Mohamed | 6d66ffc | 2021-10-01 01:55:03 +0200 | [diff] [blame] | 110 | |
David Howells | aa563d7 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 111 | iov_iter_kvec(&to, READ, &kvec, 1, buflen); |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 112 | n = p9_client_read(file->private_data, ctx->pos, &to, |
| 113 | &err); |
| 114 | if (err) |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 115 | return err; |
Johannes Berg | 8e3c500 | 2015-04-22 11:55:14 +0200 | [diff] [blame] | 116 | if (n == 0) |
| 117 | return 0; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 118 | |
| 119 | rdir->head = 0; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 120 | rdir->tail = n; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 121 | } |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 122 | while (rdir->head < rdir->tail) { |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 123 | err = p9stat_read(fid->clnt, rdir->buf + rdir->head, |
| 124 | rdir->tail - rdir->head, &st); |
Gertjan Halkes | 2803cf4 | 2018-09-05 15:41:29 +0900 | [diff] [blame] | 125 | if (err <= 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 126 | p9_debug(P9_DEBUG_VFS, "returned %d\n", err); |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 127 | return -EIO; |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 128 | } |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 129 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 130 | over = !dir_emit(ctx, st.name, strlen(st.name), |
| 131 | v9fs_qid2ino(&st.qid), dt_type(&st)); |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 132 | p9stat_free(&st); |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 133 | if (over) |
| 134 | return 0; |
| 135 | |
Gertjan Halkes | 2803cf4 | 2018-09-05 15:41:29 +0900 | [diff] [blame] | 136 | rdir->head += err; |
| 137 | ctx->pos += err; |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 138 | } |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 139 | } |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 142 | /** |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 143 | * v9fs_dir_readdir_dotl - iterate through a directory |
| 144 | * @file: opened file structure |
| 145 | * @ctx: actor we feed the entries to |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 146 | * |
| 147 | */ |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 148 | static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx) |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 149 | { |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 150 | int err = 0; |
| 151 | struct p9_fid *fid; |
| 152 | int buflen; |
| 153 | struct p9_rdir *rdir; |
| 154 | struct p9_dirent curdirent; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 155 | |
Al Viro | 4b8e992 | 2014-08-19 20:17:38 -0400 | [diff] [blame] | 156 | p9_debug(P9_DEBUG_VFS, "name %pD\n", file); |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 157 | fid = file->private_data; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 158 | |
| 159 | buflen = fid->clnt->msize - P9_READDIRHDRSZ; |
| 160 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 161 | rdir = v9fs_alloc_rdir_buf(file, buflen); |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 162 | if (!rdir) |
| 163 | return -ENOMEM; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 164 | |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 165 | while (1) { |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 166 | if (rdir->tail == rdir->head) { |
| 167 | err = p9_client_readdir(fid, rdir->buf, buflen, |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 168 | ctx->pos); |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 169 | if (err <= 0) |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 170 | return err; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 171 | |
| 172 | rdir->head = 0; |
| 173 | rdir->tail = err; |
| 174 | } |
| 175 | |
| 176 | while (rdir->head < rdir->tail) { |
| 177 | |
Aneesh Kumar K.V | 348b590 | 2011-08-07 00:46:59 +0530 | [diff] [blame] | 178 | err = p9dirent_read(fid->clnt, rdir->buf + rdir->head, |
| 179 | rdir->tail - rdir->head, |
| 180 | &curdirent); |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 181 | if (err < 0) { |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 182 | p9_debug(P9_DEBUG_VFS, "returned %d\n", err); |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 183 | return -EIO; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 186 | if (!dir_emit(ctx, curdirent.d_name, |
| 187 | strlen(curdirent.d_name), |
| 188 | v9fs_qid2ino(&curdirent.qid), |
| 189 | curdirent.d_type)) |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 190 | return 0; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 191 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 192 | ctx->pos = curdirent.d_off; |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 193 | rdir->head += err; |
| 194 | } |
| 195 | } |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 198 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 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 | { |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 208 | struct p9_fid *fid; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 209 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 210 | fid = filp->private_data; |
Joe Perches | 5d38515 | 2011-11-28 10:40:46 -0800 | [diff] [blame] | 211 | p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n", |
| 212 | inode, filp, fid ? fid->fid : -1); |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame] | 213 | if (fid) { |
| 214 | spin_lock(&inode->i_lock); |
| 215 | hlist_del(&fid->ilist); |
| 216 | spin_unlock(&inode->i_lock); |
jvrao | 62726a7 | 2010-08-25 16:26:21 +0000 | [diff] [blame] | 217 | p9_client_clunk(fid); |
Jianyong Wu | 6636b6d | 2020-09-23 22:11:46 +0800 | [diff] [blame] | 218 | } |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 219 | return 0; |
| 220 | } |
| 221 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 222 | const struct file_operations v9fs_dir_operations = { |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 223 | .read = generic_read_dir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 224 | .llseek = generic_file_llseek, |
Al Viro | 5963ded | 2016-05-01 00:08:03 -0400 | [diff] [blame] | 225 | .iterate_shared = v9fs_dir_readdir, |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 226 | .open = v9fs_file_open, |
| 227 | .release = v9fs_dir_release, |
| 228 | }; |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 229 | |
| 230 | const struct file_operations v9fs_dir_operations_dotl = { |
| 231 | .read = generic_read_dir, |
| 232 | .llseek = generic_file_llseek, |
Al Viro | 5963ded | 2016-05-01 00:08:03 -0400 | [diff] [blame] | 233 | .iterate_shared = v9fs_dir_readdir_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 234 | .open = v9fs_file_open, |
| 235 | .release = v9fs_dir_release, |
Sohaib Mohamed | 6d66ffc | 2021-10-01 01:55:03 +0200 | [diff] [blame] | 236 | .fsync = v9fs_file_fsync_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 237 | }; |