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