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 | /* |
| 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 Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 9 | */ |
| 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 Viro | 914e263 | 2006-10-18 13:55:46 -0400 | [diff] [blame] | 17 | #include <linux/sched.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 18 | #include <linux/inet.h> |
| 19 | #include <linux/idr.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 20 | #include <linux/slab.h> |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 21 | #include <linux/uio.h> |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 22 | #include <net/9p/9p.h> |
| 23 | #include <net/9p/client.h> |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 24 | |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 25 | #include "v9fs.h" |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 26 | #include "v9fs_vfs.h" |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 27 | #include "fid.h" |
| 28 | |
| 29 | /** |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 30 | * struct p9_rdir - readdir accounting |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 31 | * @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 | |
| 39 | struct p9_rdir { |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 40 | int head; |
| 41 | int tail; |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 42 | uint8_t buf[]; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | /** |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 46 | * dt_type - return file type |
| 47 | * @mistat: mistat structure |
| 48 | * |
| 49 | */ |
| 50 | |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 51 | static inline int dt_type(struct p9_wstat *mistat) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 52 | { |
| 53 | unsigned long perm = mistat->mode; |
| 54 | int rettype = DT_REG; |
| 55 | |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 56 | if (perm & P9_DMDIR) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 57 | rettype = DT_DIR; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 58 | if (perm & P9_DMSYMLINK) |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 59 | rettype = DT_LNK; |
| 60 | |
| 61 | return rettype; |
| 62 | } |
| 63 | |
| 64 | /** |
Sripathi Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 65 | * 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 Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 71 | 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] | 72 | { |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 73 | 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 Kodi | 7751bdb | 2010-06-04 13:41:26 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 80 | * v9fs_dir_readdir - iterate through a directory |
| 81 | * @file: opened file structure |
| 82 | * @ctx: actor we feed the entries to |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 83 | * |
| 84 | */ |
| 85 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 86 | 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] | 87 | { |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 88 | bool over; |
Eric Van Hensbergen | 02da398 | 2008-10-16 08:29:30 -0500 | [diff] [blame] | 89 | struct p9_wstat st; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 90 | int err = 0; |
Latchesar Ionkov | bd238fb | 2007-07-10 17:57:28 -0500 | [diff] [blame] | 91 | struct p9_fid *fid; |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 92 | int buflen; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 93 | struct p9_rdir *rdir; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 94 | struct kvec kvec; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 95 | |
Al Viro | 4b8e992 | 2014-08-19 20:17:38 -0400 | [diff] [blame] | 96 | p9_debug(P9_DEBUG_VFS, "name %pD\n", file); |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 97 | fid = file->private_data; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 98 | |
Eric Van Hensbergen | 06b55b4 | 2008-10-13 20:36:15 -0500 | [diff] [blame] | 99 | buflen = fid->clnt->msize - P9_IOHDRSZ; |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 100 | |
Al Viro | 8f29843 | 2013-05-17 17:51:41 -0400 | [diff] [blame] | 101 | rdir = v9fs_alloc_rdir_buf(file, buflen); |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 102 | if (!rdir) |
| 103 | return -ENOMEM; |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 104 | kvec.iov_base = rdir->buf; |
| 105 | kvec.iov_len = buflen; |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 106 | |
Al Viro | 7ffdea7 | 2013-01-26 00:11:36 +0000 | [diff] [blame] | 107 | while (1) { |
Eric Van Hensbergen | 3e2796a | 2009-11-02 08:39:28 -0600 | [diff] [blame] | 108 | if (rdir->tail == rdir->head) { |
Al Viro | e1200fe6 | 2015-04-01 23:42:28 -0400 | [diff] [blame] | 109 | struct iov_iter to; |
| 110 | int n; |
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); |
jvrao | 62726a7 | 2010-08-25 16:26:21 +0000 | [diff] [blame] | 213 | if (fid) |
| 214 | p9_client_clunk(fid); |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 215 | return 0; |
| 216 | } |
| 217 | |
Arjan van de Ven | 4b6f5d2 | 2006-03-28 01:56:42 -0800 | [diff] [blame] | 218 | const struct file_operations v9fs_dir_operations = { |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 219 | .read = generic_read_dir, |
Al Viro | 59af158 | 2008-08-24 07:24:41 -0400 | [diff] [blame] | 220 | .llseek = generic_file_llseek, |
Al Viro | 5963ded | 2016-05-01 00:08:03 -0400 | [diff] [blame] | 221 | .iterate_shared = v9fs_dir_readdir, |
Eric Van Hensbergen | e69e7fe | 2005-09-09 13:04:18 -0700 | [diff] [blame] | 222 | .open = v9fs_file_open, |
| 223 | .release = v9fs_dir_release, |
| 224 | }; |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 225 | |
| 226 | const struct file_operations v9fs_dir_operations_dotl = { |
| 227 | .read = generic_read_dir, |
| 228 | .llseek = generic_file_llseek, |
Al Viro | 5963ded | 2016-05-01 00:08:03 -0400 | [diff] [blame] | 229 | .iterate_shared = v9fs_dir_readdir_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 230 | .open = v9fs_file_open, |
| 231 | .release = v9fs_dir_release, |
Venkateswararao Jujjuri (JV) | b165d60 | 2010-10-22 10:13:12 -0700 | [diff] [blame] | 232 | .fsync = v9fs_file_fsync_dotl, |
Sripathi Kodi | 9b6533c | 2010-03-25 12:41:54 +0000 | [diff] [blame] | 233 | }; |