Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/fs/9p/mux.c |
| 3 | * |
| 4 | * Protocol Multiplexer |
| 5 | * |
| 6 | * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com> |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 7 | * Copyright (C) 2004-2005 by Latchesar Ionkov <lucho@ionkov.net> |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 8 | * |
| 9 | * This program is free software; you can redistribute it and/or modify |
| 10 | * it under the terms of the GNU General Public License as published by |
| 11 | * the Free Software Foundation; either version 2 of the License, or |
| 12 | * (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to: |
| 21 | * Free Software Foundation |
| 22 | * 51 Franklin Street, Fifth Floor |
| 23 | * Boston, MA 02111-1301 USA |
| 24 | * |
| 25 | */ |
| 26 | |
| 27 | #include <linux/config.h> |
| 28 | #include <linux/module.h> |
| 29 | #include <linux/errno.h> |
| 30 | #include <linux/fs.h> |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 31 | #include <linux/poll.h> |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 32 | #include <linux/kthread.h> |
| 33 | #include <linux/idr.h> |
Ingo Molnar | 4f7a07b | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 34 | #include <linux/mutex.h> |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 35 | |
| 36 | #include "debug.h" |
| 37 | #include "v9fs.h" |
| 38 | #include "9p.h" |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 39 | #include "conv.h" |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 40 | #include "transport.h" |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 41 | #include "mux.h" |
| 42 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 43 | #define ERREQFLUSH 1 |
| 44 | #define SCHED_TIMEOUT 10 |
| 45 | #define MAXPOLLWADDR 2 |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 46 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 47 | enum { |
| 48 | Rworksched = 1, /* read work scheduled or running */ |
| 49 | Rpending = 2, /* can read */ |
| 50 | Wworksched = 4, /* write work scheduled or running */ |
| 51 | Wpending = 8, /* can write */ |
| 52 | }; |
| 53 | |
| 54 | struct v9fs_mux_poll_task; |
| 55 | |
| 56 | struct v9fs_req { |
| 57 | int tag; |
| 58 | struct v9fs_fcall *tcall; |
| 59 | struct v9fs_fcall *rcall; |
| 60 | int err; |
| 61 | v9fs_mux_req_callback cb; |
| 62 | void *cba; |
| 63 | struct list_head req_list; |
| 64 | }; |
| 65 | |
| 66 | struct v9fs_mux_data { |
| 67 | spinlock_t lock; |
| 68 | struct list_head mux_list; |
| 69 | struct v9fs_mux_poll_task *poll_task; |
| 70 | int msize; |
| 71 | unsigned char *extended; |
| 72 | struct v9fs_transport *trans; |
Russ Cox | 4a26c24 | 2006-03-25 03:07:24 -0800 | [diff] [blame] | 73 | struct v9fs_idpool tagpool; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 74 | int err; |
| 75 | wait_queue_head_t equeue; |
| 76 | struct list_head req_list; |
| 77 | struct list_head unsent_req_list; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 78 | struct v9fs_fcall *rcall; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 79 | int rpos; |
| 80 | char *rbuf; |
| 81 | int wpos; |
| 82 | int wsize; |
| 83 | char *wbuf; |
| 84 | wait_queue_t poll_wait[MAXPOLLWADDR]; |
| 85 | wait_queue_head_t *poll_waddr[MAXPOLLWADDR]; |
| 86 | poll_table pt; |
| 87 | struct work_struct rq; |
| 88 | struct work_struct wq; |
| 89 | unsigned long wsched; |
| 90 | }; |
| 91 | |
| 92 | struct v9fs_mux_poll_task { |
| 93 | struct task_struct *task; |
| 94 | struct list_head mux_list; |
| 95 | int muxnum; |
| 96 | }; |
| 97 | |
| 98 | struct v9fs_mux_rpc { |
| 99 | struct v9fs_mux_data *m; |
| 100 | struct v9fs_req *req; |
| 101 | int err; |
| 102 | struct v9fs_fcall *rcall; |
| 103 | wait_queue_head_t wqueue; |
| 104 | }; |
| 105 | |
| 106 | static int v9fs_poll_proc(void *); |
| 107 | static void v9fs_read_work(void *); |
| 108 | static void v9fs_write_work(void *); |
| 109 | static void v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address, |
| 110 | poll_table * p); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 111 | static u16 v9fs_mux_get_tag(struct v9fs_mux_data *); |
| 112 | static void v9fs_mux_put_tag(struct v9fs_mux_data *, u16); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 113 | |
Ingo Molnar | 4f7a07b | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 114 | static DEFINE_MUTEX(v9fs_mux_task_lock); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 115 | static struct workqueue_struct *v9fs_mux_wq; |
| 116 | |
| 117 | static int v9fs_mux_num; |
| 118 | static int v9fs_mux_poll_task_num; |
| 119 | static struct v9fs_mux_poll_task v9fs_mux_poll_tasks[100]; |
| 120 | |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 121 | int v9fs_mux_global_init(void) |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 122 | { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) |
| 126 | v9fs_mux_poll_tasks[i].task = NULL; |
| 127 | |
| 128 | v9fs_mux_wq = create_workqueue("v9fs"); |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 129 | if (!v9fs_mux_wq) |
| 130 | return -ENOMEM; |
| 131 | |
| 132 | return 0; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void v9fs_mux_global_exit(void) |
| 136 | { |
| 137 | destroy_workqueue(v9fs_mux_wq); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /** |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 141 | * v9fs_mux_calc_poll_procs - calculates the number of polling procs |
| 142 | * based on the number of mounted v9fs filesystems. |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 143 | * |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 144 | * The current implementation returns sqrt of the number of mounts. |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 145 | */ |
Adrian Bunk | 29c6e48 | 2006-03-24 03:15:52 -0800 | [diff] [blame] | 146 | static int v9fs_mux_calc_poll_procs(int muxnum) |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 147 | { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 148 | int n; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 149 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 150 | if (v9fs_mux_poll_task_num) |
| 151 | n = muxnum / v9fs_mux_poll_task_num + |
| 152 | (muxnum % v9fs_mux_poll_task_num ? 1 : 0); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 153 | else |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 154 | n = 1; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 155 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 156 | if (n > ARRAY_SIZE(v9fs_mux_poll_tasks)) |
| 157 | n = ARRAY_SIZE(v9fs_mux_poll_tasks); |
| 158 | |
| 159 | return n; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 162 | static int v9fs_mux_poll_start(struct v9fs_mux_data *m) |
Eric Van Hensbergen | 322b329 | 2005-09-09 13:04:23 -0700 | [diff] [blame] | 163 | { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 164 | int i, n; |
| 165 | struct v9fs_mux_poll_task *vpt, *vptlast; |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 166 | struct task_struct *pproc; |
Eric Van Hensbergen | 322b329 | 2005-09-09 13:04:23 -0700 | [diff] [blame] | 167 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 168 | dprintk(DEBUG_MUX, "mux %p muxnum %d procnum %d\n", m, v9fs_mux_num, |
| 169 | v9fs_mux_poll_task_num); |
Ingo Molnar | 4f7a07b | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 170 | mutex_lock(&v9fs_mux_task_lock); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 171 | |
| 172 | n = v9fs_mux_calc_poll_procs(v9fs_mux_num + 1); |
| 173 | if (n > v9fs_mux_poll_task_num) { |
| 174 | for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) { |
| 175 | if (v9fs_mux_poll_tasks[i].task == NULL) { |
| 176 | vpt = &v9fs_mux_poll_tasks[i]; |
| 177 | dprintk(DEBUG_MUX, "create proc %p\n", vpt); |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 178 | pproc = kthread_create(v9fs_poll_proc, vpt, |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 179 | "v9fs-poll"); |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 180 | |
| 181 | if (!IS_ERR(pproc)) { |
| 182 | vpt->task = pproc; |
| 183 | INIT_LIST_HEAD(&vpt->mux_list); |
| 184 | vpt->muxnum = 0; |
| 185 | v9fs_mux_poll_task_num++; |
| 186 | wake_up_process(vpt->task); |
| 187 | } |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 188 | break; |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks)) |
| 193 | dprintk(DEBUG_ERROR, "warning: no free poll slots\n"); |
Eric Van Hensbergen | 322b329 | 2005-09-09 13:04:23 -0700 | [diff] [blame] | 194 | } |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 195 | |
| 196 | n = (v9fs_mux_num + 1) / v9fs_mux_poll_task_num + |
| 197 | ((v9fs_mux_num + 1) % v9fs_mux_poll_task_num ? 1 : 0); |
| 198 | |
| 199 | vptlast = NULL; |
| 200 | for (i = 0; i < ARRAY_SIZE(v9fs_mux_poll_tasks); i++) { |
| 201 | vpt = &v9fs_mux_poll_tasks[i]; |
| 202 | if (vpt->task != NULL) { |
| 203 | vptlast = vpt; |
| 204 | if (vpt->muxnum < n) { |
| 205 | dprintk(DEBUG_MUX, "put in proc %d\n", i); |
| 206 | list_add(&m->mux_list, &vpt->mux_list); |
| 207 | vpt->muxnum++; |
| 208 | m->poll_task = vpt; |
| 209 | memset(&m->poll_waddr, 0, sizeof(m->poll_waddr)); |
| 210 | init_poll_funcptr(&m->pt, v9fs_pollwait); |
| 211 | break; |
| 212 | } |
| 213 | } |
| 214 | } |
| 215 | |
| 216 | if (i >= ARRAY_SIZE(v9fs_mux_poll_tasks)) { |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 217 | if (vptlast == NULL) |
| 218 | return -ENOMEM; |
| 219 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 220 | dprintk(DEBUG_MUX, "put in proc %d\n", i); |
| 221 | list_add(&m->mux_list, &vptlast->mux_list); |
| 222 | vptlast->muxnum++; |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 223 | m->poll_task = vptlast; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 224 | memset(&m->poll_waddr, 0, sizeof(m->poll_waddr)); |
| 225 | init_poll_funcptr(&m->pt, v9fs_pollwait); |
| 226 | } |
| 227 | |
| 228 | v9fs_mux_num++; |
Ingo Molnar | 4f7a07b | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 229 | mutex_unlock(&v9fs_mux_task_lock); |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 230 | |
| 231 | return 0; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | static void v9fs_mux_poll_stop(struct v9fs_mux_data *m) |
| 235 | { |
| 236 | int i; |
| 237 | struct v9fs_mux_poll_task *vpt; |
| 238 | |
Ingo Molnar | 4f7a07b | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 239 | mutex_lock(&v9fs_mux_task_lock); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 240 | vpt = m->poll_task; |
| 241 | list_del(&m->mux_list); |
| 242 | for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) { |
| 243 | if (m->poll_waddr[i] != NULL) { |
| 244 | remove_wait_queue(m->poll_waddr[i], &m->poll_wait[i]); |
| 245 | m->poll_waddr[i] = NULL; |
| 246 | } |
| 247 | } |
| 248 | vpt->muxnum--; |
| 249 | if (!vpt->muxnum) { |
| 250 | dprintk(DEBUG_MUX, "destroy proc %p\n", vpt); |
| 251 | send_sig(SIGKILL, vpt->task, 1); |
| 252 | vpt->task = NULL; |
| 253 | v9fs_mux_poll_task_num--; |
| 254 | } |
| 255 | v9fs_mux_num--; |
Ingo Molnar | 4f7a07b | 2006-03-23 03:00:19 -0800 | [diff] [blame] | 256 | mutex_unlock(&v9fs_mux_task_lock); |
Eric Van Hensbergen | 322b329 | 2005-09-09 13:04:23 -0700 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | /** |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 260 | * v9fs_mux_init - allocate and initialize the per-session mux data |
| 261 | * Creates the polling task if this is the first session. |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 262 | * |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 263 | * @trans - transport structure |
| 264 | * @msize - maximum message size |
| 265 | * @extended - pointer to the extended flag |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 266 | */ |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 267 | struct v9fs_mux_data *v9fs_mux_init(struct v9fs_transport *trans, int msize, |
| 268 | unsigned char *extended) |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 269 | { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 270 | int i, n; |
| 271 | struct v9fs_mux_data *m, *mtmp; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 272 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 273 | dprintk(DEBUG_MUX, "transport %p msize %d\n", trans, msize); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 274 | m = kmalloc(sizeof(struct v9fs_mux_data), GFP_KERNEL); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 275 | if (!m) |
| 276 | return ERR_PTR(-ENOMEM); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 277 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 278 | spin_lock_init(&m->lock); |
| 279 | INIT_LIST_HEAD(&m->mux_list); |
| 280 | m->msize = msize; |
| 281 | m->extended = extended; |
| 282 | m->trans = trans; |
Russ Cox | 4a26c24 | 2006-03-25 03:07:24 -0800 | [diff] [blame] | 283 | idr_init(&m->tagpool.pool); |
| 284 | init_MUTEX(&m->tagpool.lock); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 285 | m->err = 0; |
| 286 | init_waitqueue_head(&m->equeue); |
| 287 | INIT_LIST_HEAD(&m->req_list); |
| 288 | INIT_LIST_HEAD(&m->unsent_req_list); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 289 | m->rcall = NULL; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 290 | m->rpos = 0; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 291 | m->rbuf = NULL; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 292 | m->wpos = m->wsize = 0; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 293 | m->wbuf = NULL; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 294 | INIT_WORK(&m->rq, v9fs_read_work, m); |
| 295 | INIT_WORK(&m->wq, v9fs_write_work, m); |
| 296 | m->wsched = 0; |
| 297 | memset(&m->poll_waddr, 0, sizeof(m->poll_waddr)); |
Latchesar Ionkov | 1dac06b | 2006-01-08 01:05:02 -0800 | [diff] [blame] | 298 | m->poll_task = NULL; |
| 299 | n = v9fs_mux_poll_start(m); |
| 300 | if (n) |
| 301 | return ERR_PTR(n); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 302 | |
| 303 | n = trans->poll(trans, &m->pt); |
| 304 | if (n & POLLIN) { |
| 305 | dprintk(DEBUG_MUX, "mux %p can read\n", m); |
| 306 | set_bit(Rpending, &m->wsched); |
| 307 | } |
| 308 | |
| 309 | if (n & POLLOUT) { |
| 310 | dprintk(DEBUG_MUX, "mux %p can write\n", m); |
| 311 | set_bit(Wpending, &m->wsched); |
| 312 | } |
| 313 | |
| 314 | for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) { |
| 315 | if (IS_ERR(m->poll_waddr[i])) { |
| 316 | v9fs_mux_poll_stop(m); |
| 317 | mtmp = (void *)m->poll_waddr; /* the error code */ |
| 318 | kfree(m); |
| 319 | m = mtmp; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 320 | break; |
| 321 | } |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 322 | } |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 323 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 324 | return m; |
| 325 | } |
| 326 | |
| 327 | /** |
| 328 | * v9fs_mux_destroy - cancels all pending requests and frees mux resources |
| 329 | */ |
| 330 | void v9fs_mux_destroy(struct v9fs_mux_data *m) |
| 331 | { |
| 332 | dprintk(DEBUG_MUX, "mux %p prev %p next %p\n", m, |
| 333 | m->mux_list.prev, m->mux_list.next); |
| 334 | v9fs_mux_cancel(m, -ECONNRESET); |
| 335 | |
| 336 | if (!list_empty(&m->req_list)) { |
| 337 | /* wait until all processes waiting on this session exit */ |
| 338 | dprintk(DEBUG_MUX, "mux %p waiting for empty request queue\n", |
| 339 | m); |
| 340 | wait_event_timeout(m->equeue, (list_empty(&m->req_list)), 5000); |
| 341 | dprintk(DEBUG_MUX, "mux %p request queue empty: %d\n", m, |
| 342 | list_empty(&m->req_list)); |
| 343 | } |
| 344 | |
| 345 | v9fs_mux_poll_stop(m); |
| 346 | m->trans = NULL; |
| 347 | |
| 348 | kfree(m); |
| 349 | } |
| 350 | |
| 351 | /** |
| 352 | * v9fs_pollwait - called by files poll operation to add v9fs-poll task |
| 353 | * to files wait queue |
| 354 | */ |
| 355 | static void |
| 356 | v9fs_pollwait(struct file *filp, wait_queue_head_t * wait_address, |
| 357 | poll_table * p) |
| 358 | { |
| 359 | int i; |
| 360 | struct v9fs_mux_data *m; |
| 361 | |
| 362 | m = container_of(p, struct v9fs_mux_data, pt); |
| 363 | for(i = 0; i < ARRAY_SIZE(m->poll_waddr); i++) |
| 364 | if (m->poll_waddr[i] == NULL) |
| 365 | break; |
| 366 | |
| 367 | if (i >= ARRAY_SIZE(m->poll_waddr)) { |
| 368 | dprintk(DEBUG_ERROR, "not enough wait_address slots\n"); |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | m->poll_waddr[i] = wait_address; |
| 373 | |
| 374 | if (!wait_address) { |
| 375 | dprintk(DEBUG_ERROR, "no wait_address\n"); |
| 376 | m->poll_waddr[i] = ERR_PTR(-EIO); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | init_waitqueue_entry(&m->poll_wait[i], m->poll_task->task); |
| 381 | add_wait_queue(wait_address, &m->poll_wait[i]); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * v9fs_poll_mux - polls a mux and schedules read or write works if necessary |
| 386 | */ |
Adrian Bunk | 29c6e48 | 2006-03-24 03:15:52 -0800 | [diff] [blame] | 387 | static void v9fs_poll_mux(struct v9fs_mux_data *m) |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 388 | { |
| 389 | int n; |
| 390 | |
| 391 | if (m->err < 0) |
| 392 | return; |
| 393 | |
| 394 | n = m->trans->poll(m->trans, NULL); |
| 395 | if (n < 0 || n & (POLLERR | POLLHUP | POLLNVAL)) { |
| 396 | dprintk(DEBUG_MUX, "error mux %p err %d\n", m, n); |
| 397 | if (n >= 0) |
| 398 | n = -ECONNRESET; |
| 399 | v9fs_mux_cancel(m, n); |
| 400 | } |
| 401 | |
| 402 | if (n & POLLIN) { |
| 403 | set_bit(Rpending, &m->wsched); |
| 404 | dprintk(DEBUG_MUX, "mux %p can read\n", m); |
| 405 | if (!test_and_set_bit(Rworksched, &m->wsched)) { |
| 406 | dprintk(DEBUG_MUX, "schedule read work mux %p\n", m); |
| 407 | queue_work(v9fs_mux_wq, &m->rq); |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | if (n & POLLOUT) { |
| 412 | set_bit(Wpending, &m->wsched); |
| 413 | dprintk(DEBUG_MUX, "mux %p can write\n", m); |
| 414 | if ((m->wsize || !list_empty(&m->unsent_req_list)) |
| 415 | && !test_and_set_bit(Wworksched, &m->wsched)) { |
| 416 | dprintk(DEBUG_MUX, "schedule write work mux %p\n", m); |
| 417 | queue_work(v9fs_mux_wq, &m->wq); |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /** |
| 423 | * v9fs_poll_proc - polls all v9fs transports for new events and queues |
| 424 | * the appropriate work to the work queue |
| 425 | */ |
| 426 | static int v9fs_poll_proc(void *a) |
| 427 | { |
| 428 | struct v9fs_mux_data *m, *mtmp; |
| 429 | struct v9fs_mux_poll_task *vpt; |
| 430 | |
| 431 | vpt = a; |
| 432 | dprintk(DEBUG_MUX, "start %p %p\n", current, vpt); |
| 433 | allow_signal(SIGKILL); |
| 434 | while (!kthread_should_stop()) { |
| 435 | set_current_state(TASK_INTERRUPTIBLE); |
| 436 | if (signal_pending(current)) |
| 437 | break; |
| 438 | |
| 439 | list_for_each_entry_safe(m, mtmp, &vpt->mux_list, mux_list) { |
| 440 | v9fs_poll_mux(m); |
| 441 | } |
| 442 | |
| 443 | dprintk(DEBUG_MUX, "sleeping...\n"); |
| 444 | schedule_timeout(SCHED_TIMEOUT * HZ); |
| 445 | } |
| 446 | |
| 447 | __set_current_state(TASK_RUNNING); |
| 448 | dprintk(DEBUG_MUX, "finish\n"); |
| 449 | return 0; |
| 450 | } |
| 451 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 452 | /** |
| 453 | * v9fs_write_work - called when a transport can send some data |
| 454 | */ |
| 455 | static void v9fs_write_work(void *a) |
| 456 | { |
| 457 | int n, err; |
| 458 | struct v9fs_mux_data *m; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 459 | struct v9fs_req *req; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 460 | |
| 461 | m = a; |
| 462 | |
| 463 | if (m->err < 0) { |
| 464 | clear_bit(Wworksched, &m->wsched); |
| 465 | return; |
| 466 | } |
| 467 | |
| 468 | if (!m->wsize) { |
| 469 | if (list_empty(&m->unsent_req_list)) { |
| 470 | clear_bit(Wworksched, &m->wsched); |
| 471 | return; |
| 472 | } |
| 473 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 474 | spin_lock(&m->lock); |
Latchesar Ionkov | 034b91a | 2006-02-03 03:04:20 -0800 | [diff] [blame] | 475 | again: |
| 476 | req = list_entry(m->unsent_req_list.next, struct v9fs_req, |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 477 | req_list); |
| 478 | list_move_tail(&req->req_list, &m->req_list); |
Latchesar Ionkov | 034b91a | 2006-02-03 03:04:20 -0800 | [diff] [blame] | 479 | if (req->err == ERREQFLUSH) |
| 480 | goto again; |
| 481 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 482 | m->wbuf = req->tcall->sdata; |
| 483 | m->wsize = req->tcall->size; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 484 | m->wpos = 0; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 485 | dump_data(m->wbuf, m->wsize); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 486 | spin_unlock(&m->lock); |
| 487 | } |
| 488 | |
| 489 | dprintk(DEBUG_MUX, "mux %p pos %d size %d\n", m, m->wpos, m->wsize); |
| 490 | clear_bit(Wpending, &m->wsched); |
| 491 | err = m->trans->write(m->trans, m->wbuf + m->wpos, m->wsize - m->wpos); |
| 492 | dprintk(DEBUG_MUX, "mux %p sent %d bytes\n", m, err); |
| 493 | if (err == -EAGAIN) { |
| 494 | clear_bit(Wworksched, &m->wsched); |
| 495 | return; |
| 496 | } |
| 497 | |
| 498 | if (err <= 0) |
| 499 | goto error; |
| 500 | |
| 501 | m->wpos += err; |
| 502 | if (m->wpos == m->wsize) |
| 503 | m->wpos = m->wsize = 0; |
| 504 | |
| 505 | if (m->wsize == 0 && !list_empty(&m->unsent_req_list)) { |
| 506 | if (test_and_clear_bit(Wpending, &m->wsched)) |
| 507 | n = POLLOUT; |
| 508 | else |
| 509 | n = m->trans->poll(m->trans, NULL); |
| 510 | |
| 511 | if (n & POLLOUT) { |
| 512 | dprintk(DEBUG_MUX, "schedule write work mux %p\n", m); |
| 513 | queue_work(v9fs_mux_wq, &m->wq); |
| 514 | } else |
| 515 | clear_bit(Wworksched, &m->wsched); |
| 516 | } else |
| 517 | clear_bit(Wworksched, &m->wsched); |
| 518 | |
| 519 | return; |
| 520 | |
| 521 | error: |
| 522 | v9fs_mux_cancel(m, err); |
| 523 | clear_bit(Wworksched, &m->wsched); |
| 524 | } |
| 525 | |
| 526 | static void process_request(struct v9fs_mux_data *m, struct v9fs_req *req) |
| 527 | { |
| 528 | int ecode, tag; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 529 | struct v9fs_str *ename; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 530 | |
| 531 | tag = req->tag; |
Latchesar Ionkov | 034b91a | 2006-02-03 03:04:20 -0800 | [diff] [blame] | 532 | if (!req->err && req->rcall->id == RERROR) { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 533 | ecode = req->rcall->params.rerror.errno; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 534 | ename = &req->rcall->params.rerror.error; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 535 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 536 | dprintk(DEBUG_MUX, "Rerror %.*s\n", ename->len, ename->str); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 537 | |
| 538 | if (*m->extended) |
| 539 | req->err = -ecode; |
| 540 | |
| 541 | if (!req->err) { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 542 | req->err = v9fs_errstr2errno(ename->str, ename->len); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 543 | |
| 544 | if (!req->err) { /* string match failed */ |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 545 | PRINT_FCALL_ERROR("unknown error", req->rcall); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | if (!req->err) |
| 549 | req->err = -ESERVERFAULT; |
| 550 | } |
| 551 | } else if (req->tcall && req->rcall->id != req->tcall->id + 1) { |
| 552 | dprintk(DEBUG_ERROR, "fcall mismatch: expected %d, got %d\n", |
| 553 | req->tcall->id + 1, req->rcall->id); |
| 554 | if (!req->err) |
| 555 | req->err = -EIO; |
| 556 | } |
| 557 | |
Latchesar Ionkov | 034b91a | 2006-02-03 03:04:20 -0800 | [diff] [blame] | 558 | if (req->err == ERREQFLUSH) |
| 559 | return; |
| 560 | |
| 561 | if (req->cb) { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 562 | dprintk(DEBUG_MUX, "calling callback tcall %p rcall %p\n", |
| 563 | req->tcall, req->rcall); |
| 564 | |
| 565 | (*req->cb) (req->cba, req->tcall, req->rcall, req->err); |
| 566 | req->cb = NULL; |
| 567 | } else |
| 568 | kfree(req->rcall); |
| 569 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 570 | v9fs_mux_put_tag(m, tag); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 571 | |
| 572 | wake_up(&m->equeue); |
| 573 | kfree(req); |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * v9fs_read_work - called when there is some data to be read from a transport |
| 578 | */ |
| 579 | static void v9fs_read_work(void *a) |
| 580 | { |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 581 | int n, err; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 582 | struct v9fs_mux_data *m; |
| 583 | struct v9fs_req *req, *rptr, *rreq; |
| 584 | struct v9fs_fcall *rcall; |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 585 | char *rbuf; |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 586 | |
| 587 | m = a; |
| 588 | |
| 589 | if (m->err < 0) |
| 590 | return; |
| 591 | |
| 592 | rcall = NULL; |
| 593 | dprintk(DEBUG_MUX, "start mux %p pos %d\n", m, m->rpos); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 594 | |
| 595 | if (!m->rcall) { |
| 596 | m->rcall = |
| 597 | kmalloc(sizeof(struct v9fs_fcall) + m->msize, GFP_KERNEL); |
| 598 | if (!m->rcall) { |
| 599 | err = -ENOMEM; |
| 600 | goto error; |
| 601 | } |
| 602 | |
| 603 | m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall); |
| 604 | m->rpos = 0; |
| 605 | } |
| 606 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 607 | clear_bit(Rpending, &m->wsched); |
| 608 | err = m->trans->read(m->trans, m->rbuf + m->rpos, m->msize - m->rpos); |
| 609 | dprintk(DEBUG_MUX, "mux %p got %d bytes\n", m, err); |
| 610 | if (err == -EAGAIN) { |
| 611 | clear_bit(Rworksched, &m->wsched); |
| 612 | return; |
| 613 | } |
| 614 | |
| 615 | if (err <= 0) |
| 616 | goto error; |
| 617 | |
| 618 | m->rpos += err; |
| 619 | while (m->rpos > 4) { |
| 620 | n = le32_to_cpu(*(__le32 *) m->rbuf); |
| 621 | if (n >= m->msize) { |
| 622 | dprintk(DEBUG_ERROR, |
| 623 | "requested packet size too big: %d\n", n); |
| 624 | err = -EIO; |
| 625 | goto error; |
| 626 | } |
| 627 | |
| 628 | if (m->rpos < n) |
| 629 | break; |
| 630 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 631 | dump_data(m->rbuf, n); |
| 632 | err = |
| 633 | v9fs_deserialize_fcall(m->rbuf, n, m->rcall, *m->extended); |
| 634 | if (err < 0) { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 635 | goto error; |
| 636 | } |
| 637 | |
Latchesar Ionkov | 5174fda | 2006-03-25 03:07:25 -0800 | [diff] [blame^] | 638 | if ((v9fs_debug_level&DEBUG_FCALL) == DEBUG_FCALL) { |
| 639 | char buf[150]; |
| 640 | |
| 641 | v9fs_printfcall(buf, sizeof(buf), m->rcall, |
| 642 | *m->extended); |
| 643 | printk(KERN_NOTICE ">>> %p %s\n", m, buf); |
| 644 | } |
| 645 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 646 | rcall = m->rcall; |
| 647 | rbuf = m->rbuf; |
| 648 | if (m->rpos > n) { |
| 649 | m->rcall = kmalloc(sizeof(struct v9fs_fcall) + m->msize, |
| 650 | GFP_KERNEL); |
| 651 | if (!m->rcall) { |
| 652 | err = -ENOMEM; |
| 653 | goto error; |
| 654 | } |
| 655 | |
| 656 | m->rbuf = (char *)m->rcall + sizeof(struct v9fs_fcall); |
| 657 | memmove(m->rbuf, rbuf + n, m->rpos - n); |
| 658 | m->rpos -= n; |
| 659 | } else { |
| 660 | m->rcall = NULL; |
| 661 | m->rbuf = NULL; |
| 662 | m->rpos = 0; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 663 | } |
| 664 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 665 | dprintk(DEBUG_MUX, "mux %p fcall id %d tag %d\n", m, rcall->id, |
| 666 | rcall->tag); |
| 667 | |
| 668 | req = NULL; |
| 669 | spin_lock(&m->lock); |
| 670 | list_for_each_entry_safe(rreq, rptr, &m->req_list, req_list) { |
| 671 | if (rreq->tag == rcall->tag) { |
| 672 | req = rreq; |
| 673 | req->rcall = rcall; |
| 674 | list_del(&req->req_list); |
| 675 | spin_unlock(&m->lock); |
| 676 | process_request(m, req); |
| 677 | break; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 678 | } |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 679 | |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 680 | } |
| 681 | |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 682 | if (!req) { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 683 | spin_unlock(&m->lock); |
| 684 | if (err >= 0 && rcall->id != RFLUSH) |
Eric Van Hensbergen | cb2e87a | 2005-09-09 13:04:28 -0700 | [diff] [blame] | 685 | dprintk(DEBUG_ERROR, |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 686 | "unexpected response mux %p id %d tag %d\n", |
| 687 | m, rcall->id, rcall->tag); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 688 | kfree(rcall); |
| 689 | } |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 692 | if (!list_empty(&m->req_list)) { |
| 693 | if (test_and_clear_bit(Rpending, &m->wsched)) |
| 694 | n = POLLIN; |
| 695 | else |
| 696 | n = m->trans->poll(m->trans, NULL); |
Eric Van Hensbergen | cb2e87a | 2005-09-09 13:04:28 -0700 | [diff] [blame] | 697 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 698 | if (n & POLLIN) { |
| 699 | dprintk(DEBUG_MUX, "schedule read work mux %p\n", m); |
| 700 | queue_work(v9fs_mux_wq, &m->rq); |
| 701 | } else |
| 702 | clear_bit(Rworksched, &m->wsched); |
| 703 | } else |
| 704 | clear_bit(Rworksched, &m->wsched); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 705 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 706 | return; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 707 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 708 | error: |
| 709 | v9fs_mux_cancel(m, err); |
| 710 | clear_bit(Rworksched, &m->wsched); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 714 | * v9fs_send_request - send 9P request |
| 715 | * The function can sleep until the request is scheduled for sending. |
| 716 | * The function can be interrupted. Return from the function is not |
| 717 | * a guarantee that the request is sent succesfully. Can return errors |
| 718 | * that can be retrieved by PTR_ERR macros. |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 719 | * |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 720 | * @m: mux data |
| 721 | * @tc: request to be sent |
| 722 | * @cb: callback function to call when response is received |
| 723 | * @cba: parameter to pass to the callback function |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 724 | */ |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 725 | static struct v9fs_req *v9fs_send_request(struct v9fs_mux_data *m, |
| 726 | struct v9fs_fcall *tc, |
| 727 | v9fs_mux_req_callback cb, void *cba) |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 728 | { |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 729 | int n; |
| 730 | struct v9fs_req *req; |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 731 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 732 | dprintk(DEBUG_MUX, "mux %p task %p tcall %p id %d\n", m, current, |
| 733 | tc, tc->id); |
| 734 | if (m->err < 0) |
| 735 | return ERR_PTR(m->err); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 736 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 737 | req = kmalloc(sizeof(struct v9fs_req), GFP_KERNEL); |
| 738 | if (!req) |
| 739 | return ERR_PTR(-ENOMEM); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 740 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 741 | if (tc->id == TVERSION) |
| 742 | n = V9FS_NOTAG; |
| 743 | else |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 744 | n = v9fs_mux_get_tag(m); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 745 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 746 | if (n < 0) |
| 747 | return ERR_PTR(-ENOMEM); |
| 748 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 749 | v9fs_set_tag(tc, n); |
| 750 | |
Latchesar Ionkov | 5174fda | 2006-03-25 03:07:25 -0800 | [diff] [blame^] | 751 | if ((v9fs_debug_level&DEBUG_FCALL) == DEBUG_FCALL) { |
| 752 | char buf[150]; |
| 753 | |
| 754 | v9fs_printfcall(buf, sizeof(buf), tc, *m->extended); |
| 755 | printk(KERN_NOTICE "<<< %p %s\n", m, buf); |
| 756 | } |
| 757 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 758 | req->tag = n; |
| 759 | req->tcall = tc; |
| 760 | req->rcall = NULL; |
| 761 | req->err = 0; |
| 762 | req->cb = cb; |
| 763 | req->cba = cba; |
| 764 | |
| 765 | spin_lock(&m->lock); |
| 766 | list_add_tail(&req->req_list, &m->unsent_req_list); |
| 767 | spin_unlock(&m->lock); |
| 768 | |
| 769 | if (test_and_clear_bit(Wpending, &m->wsched)) |
| 770 | n = POLLOUT; |
| 771 | else |
| 772 | n = m->trans->poll(m->trans, NULL); |
| 773 | |
| 774 | if (n & POLLOUT && !test_and_set_bit(Wworksched, &m->wsched)) |
| 775 | queue_work(v9fs_mux_wq, &m->wq); |
| 776 | |
| 777 | return req; |
| 778 | } |
| 779 | |
Adrian Bunk | 29c6e48 | 2006-03-24 03:15:52 -0800 | [diff] [blame] | 780 | static void v9fs_mux_flush_cb(void *a, struct v9fs_fcall *tc, |
| 781 | struct v9fs_fcall *rc, int err) |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 782 | { |
| 783 | v9fs_mux_req_callback cb; |
| 784 | int tag; |
| 785 | struct v9fs_mux_data *m; |
| 786 | struct v9fs_req *req, *rptr; |
| 787 | |
| 788 | m = a; |
| 789 | dprintk(DEBUG_MUX, "mux %p tc %p rc %p err %d oldtag %d\n", m, tc, |
| 790 | rc, err, tc->params.tflush.oldtag); |
| 791 | |
| 792 | spin_lock(&m->lock); |
| 793 | cb = NULL; |
| 794 | tag = tc->params.tflush.oldtag; |
| 795 | list_for_each_entry_safe(req, rptr, &m->req_list, req_list) { |
| 796 | if (req->tag == tag) { |
| 797 | list_del(&req->req_list); |
| 798 | if (req->cb) { |
| 799 | cb = req->cb; |
| 800 | req->cb = NULL; |
| 801 | spin_unlock(&m->lock); |
| 802 | (*cb) (req->cba, req->tcall, req->rcall, |
| 803 | req->err); |
| 804 | } |
| 805 | kfree(req); |
| 806 | wake_up(&m->equeue); |
| 807 | break; |
| 808 | } |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 809 | } |
| 810 | |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 811 | if (!cb) |
| 812 | spin_unlock(&m->lock); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 813 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 814 | v9fs_mux_put_tag(m, tag); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 815 | kfree(tc); |
| 816 | kfree(rc); |
| 817 | } |
| 818 | |
| 819 | static void |
| 820 | v9fs_mux_flush_request(struct v9fs_mux_data *m, struct v9fs_req *req) |
| 821 | { |
| 822 | struct v9fs_fcall *fc; |
| 823 | |
| 824 | dprintk(DEBUG_MUX, "mux %p req %p tag %d\n", m, req, req->tag); |
| 825 | |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 826 | fc = v9fs_create_tflush(req->tag); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 827 | v9fs_send_request(m, fc, v9fs_mux_flush_cb, m); |
| 828 | } |
| 829 | |
| 830 | static void |
| 831 | v9fs_mux_rpc_cb(void *a, struct v9fs_fcall *tc, struct v9fs_fcall *rc, int err) |
| 832 | { |
| 833 | struct v9fs_mux_rpc *r; |
| 834 | |
| 835 | if (err == ERREQFLUSH) { |
Latchesar Ionkov | 034b91a | 2006-02-03 03:04:20 -0800 | [diff] [blame] | 836 | kfree(rc); |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 837 | dprintk(DEBUG_MUX, "err req flush\n"); |
| 838 | return; |
| 839 | } |
| 840 | |
| 841 | r = a; |
| 842 | dprintk(DEBUG_MUX, "mux %p req %p tc %p rc %p err %d\n", r->m, r->req, |
| 843 | tc, rc, err); |
| 844 | r->rcall = rc; |
| 845 | r->err = err; |
| 846 | wake_up(&r->wqueue); |
| 847 | } |
| 848 | |
| 849 | /** |
| 850 | * v9fs_mux_rpc - sends 9P request and waits until a response is available. |
| 851 | * The function can be interrupted. |
| 852 | * @m: mux data |
| 853 | * @tc: request to be sent |
| 854 | * @rc: pointer where a pointer to the response is stored |
| 855 | */ |
| 856 | int |
| 857 | v9fs_mux_rpc(struct v9fs_mux_data *m, struct v9fs_fcall *tc, |
| 858 | struct v9fs_fcall **rc) |
| 859 | { |
| 860 | int err; |
| 861 | unsigned long flags; |
| 862 | struct v9fs_req *req; |
| 863 | struct v9fs_mux_rpc r; |
| 864 | |
| 865 | r.err = 0; |
| 866 | r.rcall = NULL; |
| 867 | r.m = m; |
| 868 | init_waitqueue_head(&r.wqueue); |
| 869 | |
| 870 | if (rc) |
| 871 | *rc = NULL; |
| 872 | |
| 873 | req = v9fs_send_request(m, tc, v9fs_mux_rpc_cb, &r); |
| 874 | if (IS_ERR(req)) { |
| 875 | err = PTR_ERR(req); |
| 876 | dprintk(DEBUG_MUX, "error %d\n", err); |
| 877 | return PTR_ERR(req); |
| 878 | } |
| 879 | |
| 880 | r.req = req; |
| 881 | dprintk(DEBUG_MUX, "mux %p tc %p tag %d rpc %p req %p\n", m, tc, |
| 882 | req->tag, &r, req); |
| 883 | err = wait_event_interruptible(r.wqueue, r.rcall != NULL || r.err < 0); |
| 884 | if (r.err < 0) |
| 885 | err = r.err; |
| 886 | |
| 887 | if (err == -ERESTARTSYS && m->trans->status == Connected && m->err == 0) { |
| 888 | spin_lock(&m->lock); |
| 889 | req->tcall = NULL; |
| 890 | req->err = ERREQFLUSH; |
| 891 | spin_unlock(&m->lock); |
| 892 | |
| 893 | clear_thread_flag(TIF_SIGPENDING); |
| 894 | v9fs_mux_flush_request(m, req); |
| 895 | spin_lock_irqsave(¤t->sighand->siglock, flags); |
| 896 | recalc_sigpending(); |
| 897 | spin_unlock_irqrestore(¤t->sighand->siglock, flags); |
| 898 | } |
| 899 | |
| 900 | if (!err) { |
| 901 | if (r.rcall) |
| 902 | dprintk(DEBUG_MUX, "got response id %d tag %d\n", |
| 903 | r.rcall->id, r.rcall->tag); |
| 904 | |
| 905 | if (rc) |
| 906 | *rc = r.rcall; |
| 907 | else |
| 908 | kfree(r.rcall); |
| 909 | } else { |
| 910 | kfree(r.rcall); |
| 911 | dprintk(DEBUG_MUX, "got error %d\n", err); |
| 912 | if (err > 0) |
| 913 | err = -EIO; |
| 914 | } |
| 915 | |
| 916 | return err; |
| 917 | } |
| 918 | |
Adrian Bunk | 29c6e48 | 2006-03-24 03:15:52 -0800 | [diff] [blame] | 919 | #if 0 |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 920 | /** |
| 921 | * v9fs_mux_rpcnb - sends 9P request without waiting for response. |
| 922 | * @m: mux data |
| 923 | * @tc: request to be sent |
| 924 | * @cb: callback function to be called when response arrives |
| 925 | * @cba: value to pass to the callback function |
| 926 | */ |
| 927 | int v9fs_mux_rpcnb(struct v9fs_mux_data *m, struct v9fs_fcall *tc, |
| 928 | v9fs_mux_req_callback cb, void *a) |
| 929 | { |
| 930 | int err; |
| 931 | struct v9fs_req *req; |
| 932 | |
| 933 | req = v9fs_send_request(m, tc, cb, a); |
| 934 | if (IS_ERR(req)) { |
| 935 | err = PTR_ERR(req); |
| 936 | dprintk(DEBUG_MUX, "error %d\n", err); |
| 937 | return PTR_ERR(req); |
| 938 | } |
| 939 | |
| 940 | dprintk(DEBUG_MUX, "mux %p tc %p tag %d\n", m, tc, req->tag); |
Eric Van Hensbergen | 426cc91 | 2005-09-09 13:04:22 -0700 | [diff] [blame] | 941 | return 0; |
| 942 | } |
Adrian Bunk | 29c6e48 | 2006-03-24 03:15:52 -0800 | [diff] [blame] | 943 | #endif /* 0 */ |
Latchesar Ionkov | 3cf6429 | 2006-01-08 01:04:58 -0800 | [diff] [blame] | 944 | |
| 945 | /** |
| 946 | * v9fs_mux_cancel - cancel all pending requests with error |
| 947 | * @m: mux data |
| 948 | * @err: error code |
| 949 | */ |
| 950 | void v9fs_mux_cancel(struct v9fs_mux_data *m, int err) |
| 951 | { |
| 952 | struct v9fs_req *req, *rtmp; |
| 953 | LIST_HEAD(cancel_list); |
| 954 | |
| 955 | dprintk(DEBUG_MUX, "mux %p err %d\n", m, err); |
| 956 | m->err = err; |
| 957 | spin_lock(&m->lock); |
| 958 | list_for_each_entry_safe(req, rtmp, &m->req_list, req_list) { |
| 959 | list_move(&req->req_list, &cancel_list); |
| 960 | } |
| 961 | spin_unlock(&m->lock); |
| 962 | |
| 963 | list_for_each_entry_safe(req, rtmp, &cancel_list, req_list) { |
| 964 | list_del(&req->req_list); |
| 965 | if (!req->err) |
| 966 | req->err = err; |
| 967 | |
| 968 | if (req->cb) |
| 969 | (*req->cb) (req->cba, req->tcall, req->rcall, req->err); |
| 970 | else |
| 971 | kfree(req->rcall); |
| 972 | |
| 973 | kfree(req); |
| 974 | } |
| 975 | |
| 976 | wake_up(&m->equeue); |
| 977 | } |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 978 | |
| 979 | static u16 v9fs_mux_get_tag(struct v9fs_mux_data *m) |
| 980 | { |
| 981 | int tag; |
| 982 | |
Russ Cox | 4a26c24 | 2006-03-25 03:07:24 -0800 | [diff] [blame] | 983 | tag = v9fs_get_idpool(&m->tagpool); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 984 | if (tag < 0) |
| 985 | return V9FS_NOTAG; |
| 986 | else |
| 987 | return (u16) tag; |
| 988 | } |
| 989 | |
| 990 | static void v9fs_mux_put_tag(struct v9fs_mux_data *m, u16 tag) |
| 991 | { |
Russ Cox | 4a26c24 | 2006-03-25 03:07:24 -0800 | [diff] [blame] | 992 | if (tag != V9FS_NOTAG && v9fs_check_idpool(tag, &m->tagpool)) |
| 993 | v9fs_put_idpool(tag, &m->tagpool); |
Latchesar Ionkov | 531b109 | 2006-01-08 01:05:00 -0800 | [diff] [blame] | 994 | } |