David Howells | 9cc6fc5 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 1 | /* Handle fileserver selection and rotation. |
| 2 | * |
| 3 | * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved. |
| 4 | * Written by David Howells (dhowells@redhat.com) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public Licence |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the Licence, or (at your option) any later version. |
| 10 | */ |
| 11 | |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/slab.h> |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 14 | #include <linux/fs.h> |
| 15 | #include <linux/sched.h> |
| 16 | #include <linux/delay.h> |
| 17 | #include <linux/sched/signal.h> |
David Howells | 9cc6fc5 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 18 | #include "internal.h" |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 19 | #include "afs_fs.h" |
David Howells | 9cc6fc5 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 20 | |
| 21 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 22 | * Begin an operation on the fileserver. |
| 23 | * |
| 24 | * Fileserver operations are serialised on the server by vnode, so we serialise |
| 25 | * them here also using the io_lock. |
| 26 | */ |
| 27 | bool afs_begin_vnode_operation(struct afs_fs_cursor *fc, struct afs_vnode *vnode, |
| 28 | struct key *key) |
| 29 | { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 30 | memset(fc, 0, sizeof(*fc)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 31 | fc->vnode = vnode; |
| 32 | fc->key = key; |
| 33 | fc->ac.error = SHRT_MAX; |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 34 | fc->error = -EDESTADDRREQ; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 35 | |
| 36 | if (mutex_lock_interruptible(&vnode->io_lock) < 0) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 37 | fc->error = -EINTR; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 38 | fc->flags |= AFS_FS_CURSOR_STOP; |
| 39 | return false; |
| 40 | } |
| 41 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 42 | if (vnode->lock_state != AFS_VNODE_LOCK_NONE) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 43 | fc->flags |= AFS_FS_CURSOR_CUR_ONLY; |
| 44 | return true; |
| 45 | } |
| 46 | |
| 47 | /* |
| 48 | * Begin iteration through a server list, starting with the vnode's last used |
| 49 | * server if possible, or the last recorded good server if not. |
| 50 | */ |
| 51 | static bool afs_start_fs_iteration(struct afs_fs_cursor *fc, |
| 52 | struct afs_vnode *vnode) |
| 53 | { |
| 54 | struct afs_cb_interest *cbi; |
| 55 | int i; |
| 56 | |
| 57 | read_lock(&vnode->volume->servers_lock); |
| 58 | fc->server_list = afs_get_serverlist(vnode->volume->servers); |
| 59 | read_unlock(&vnode->volume->servers_lock); |
| 60 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 61 | fc->untried = (1UL << fc->server_list->nr_servers) - 1; |
| 62 | fc->index = READ_ONCE(fc->server_list->preferred); |
| 63 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 64 | cbi = vnode->cb_interest; |
| 65 | if (cbi) { |
| 66 | /* See if the vnode's preferred record is still available */ |
| 67 | for (i = 0; i < fc->server_list->nr_servers; i++) { |
| 68 | if (fc->server_list->servers[i].cb_interest == cbi) { |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 69 | fc->index = i; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 70 | goto found_interest; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /* If we have a lock outstanding on a server that's no longer |
| 75 | * serving this vnode, then we can't switch to another server |
| 76 | * and have to return an error. |
| 77 | */ |
| 78 | if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 79 | fc->error = -ESTALE; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | /* Note that the callback promise is effectively broken */ |
| 84 | write_seqlock(&vnode->cb_lock); |
| 85 | ASSERTCMP(cbi, ==, vnode->cb_interest); |
| 86 | vnode->cb_interest = NULL; |
| 87 | if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) |
| 88 | vnode->cb_break++; |
| 89 | write_sequnlock(&vnode->cb_lock); |
| 90 | |
| 91 | afs_put_cb_interest(afs_v2net(vnode), cbi); |
| 92 | cbi = NULL; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | found_interest: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 96 | return true; |
| 97 | } |
| 98 | |
| 99 | /* |
| 100 | * Post volume busy note. |
| 101 | */ |
| 102 | static void afs_busy(struct afs_volume *volume, u32 abort_code) |
| 103 | { |
| 104 | const char *m; |
| 105 | |
| 106 | switch (abort_code) { |
| 107 | case VOFFLINE: m = "offline"; break; |
| 108 | case VRESTARTING: m = "restarting"; break; |
| 109 | case VSALVAGING: m = "being salvaged"; break; |
| 110 | default: m = "busy"; break; |
| 111 | } |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 112 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 113 | pr_notice("kAFS: Volume %llu '%s' is %s\n", volume->vid, volume->name, m); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /* |
| 117 | * Sleep and retry the operation to the same fileserver. |
| 118 | */ |
| 119 | static bool afs_sleep_and_retry(struct afs_fs_cursor *fc) |
| 120 | { |
| 121 | msleep_interruptible(1000); |
| 122 | if (signal_pending(current)) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 123 | fc->error = -ERESTARTSYS; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 124 | return false; |
| 125 | } |
| 126 | |
| 127 | return true; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * Select the fileserver to use. May be called multiple times to rotate |
| 132 | * through the fileservers. |
| 133 | */ |
| 134 | bool afs_select_fileserver(struct afs_fs_cursor *fc) |
| 135 | { |
| 136 | struct afs_addr_list *alist; |
| 137 | struct afs_server *server; |
| 138 | struct afs_vnode *vnode = fc->vnode; |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 139 | struct afs_error e; |
| 140 | u32 rtt; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 141 | int error = fc->ac.error, i; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 142 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 143 | _enter("%lx[%d],%lx[%d],%d,%d", |
| 144 | fc->untried, fc->index, |
| 145 | fc->ac.tried, fc->ac.index, |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 146 | error, fc->ac.abort_code); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 147 | |
| 148 | if (fc->flags & AFS_FS_CURSOR_STOP) { |
| 149 | _leave(" = f [stopped]"); |
| 150 | return false; |
| 151 | } |
| 152 | |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 153 | fc->nr_iterations++; |
| 154 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 155 | /* Evaluate the result of the previous operation, if there was one. */ |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 156 | switch (error) { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 157 | case SHRT_MAX: |
| 158 | goto start; |
| 159 | |
| 160 | case 0: |
| 161 | default: |
| 162 | /* Success or local failure. Stop. */ |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 163 | fc->error = error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 164 | fc->flags |= AFS_FS_CURSOR_STOP; |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 165 | _leave(" = f [okay/local %d]", error); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 166 | return false; |
| 167 | |
| 168 | case -ECONNABORTED: |
| 169 | /* The far side rejected the operation on some grounds. This |
| 170 | * might involve the server being busy or the volume having been moved. |
| 171 | */ |
| 172 | switch (fc->ac.abort_code) { |
| 173 | case VNOVOL: |
| 174 | /* This fileserver doesn't know about the volume. |
| 175 | * - May indicate that the VL is wrong - retry once and compare |
| 176 | * the results. |
| 177 | * - May indicate that the fileserver couldn't attach to the vol. |
| 178 | */ |
| 179 | if (fc->flags & AFS_FS_CURSOR_VNOVOL) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 180 | fc->error = -EREMOTEIO; |
David Howells | 3d9fa91 | 2018-05-11 22:55:59 +0100 | [diff] [blame] | 181 | goto next_server; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | write_lock(&vnode->volume->servers_lock); |
| 185 | fc->server_list->vnovol_mask |= 1 << fc->index; |
| 186 | write_unlock(&vnode->volume->servers_lock); |
| 187 | |
| 188 | set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 189 | error = afs_check_volume_status(vnode->volume, fc->key); |
| 190 | if (error < 0) |
| 191 | goto failed_set_error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 192 | |
| 193 | if (test_bit(AFS_VOLUME_DELETED, &vnode->volume->flags)) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 194 | fc->error = -ENOMEDIUM; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 195 | goto failed; |
| 196 | } |
| 197 | |
| 198 | /* If the server list didn't change, then assume that |
| 199 | * it's the fileserver having trouble. |
| 200 | */ |
| 201 | if (vnode->volume->servers == fc->server_list) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 202 | fc->error = -EREMOTEIO; |
David Howells | 3d9fa91 | 2018-05-11 22:55:59 +0100 | [diff] [blame] | 203 | goto next_server; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Try again */ |
| 207 | fc->flags |= AFS_FS_CURSOR_VNOVOL; |
| 208 | _leave(" = t [vnovol]"); |
| 209 | return true; |
| 210 | |
| 211 | case VSALVAGE: /* TODO: Should this return an error or iterate? */ |
| 212 | case VVOLEXISTS: |
| 213 | case VNOSERVICE: |
| 214 | case VONLINE: |
| 215 | case VDISKFULL: |
| 216 | case VOVERQUOTA: |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 217 | fc->error = afs_abort_to_error(fc->ac.abort_code); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 218 | goto next_server; |
| 219 | |
| 220 | case VOFFLINE: |
| 221 | if (!test_and_set_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags)) { |
| 222 | afs_busy(vnode->volume, fc->ac.abort_code); |
| 223 | clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags); |
| 224 | } |
| 225 | if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 226 | fc->error = -EADV; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 227 | goto failed; |
| 228 | } |
| 229 | if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 230 | fc->error = -ESTALE; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 231 | goto failed; |
| 232 | } |
| 233 | goto busy; |
| 234 | |
| 235 | case VSALVAGING: |
| 236 | case VRESTARTING: |
| 237 | case VBUSY: |
| 238 | /* Retry after going round all the servers unless we |
| 239 | * have a file lock we need to maintain. |
| 240 | */ |
| 241 | if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 242 | fc->error = -EBUSY; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 243 | goto failed; |
| 244 | } |
| 245 | if (!test_and_set_bit(AFS_VOLUME_BUSY, &vnode->volume->flags)) { |
| 246 | afs_busy(vnode->volume, fc->ac.abort_code); |
| 247 | clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags); |
| 248 | } |
| 249 | busy: |
| 250 | if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) { |
| 251 | if (!afs_sleep_and_retry(fc)) |
| 252 | goto failed; |
| 253 | |
| 254 | /* Retry with same server & address */ |
| 255 | _leave(" = t [vbusy]"); |
| 256 | return true; |
| 257 | } |
| 258 | |
| 259 | fc->flags |= AFS_FS_CURSOR_VBUSY; |
| 260 | goto next_server; |
| 261 | |
| 262 | case VMOVED: |
| 263 | /* The volume migrated to another server. We consider |
| 264 | * consider all locks and callbacks broken and request |
| 265 | * an update from the VLDB. |
| 266 | * |
| 267 | * We also limit the number of VMOVED hops we will |
| 268 | * honour, just in case someone sets up a loop. |
| 269 | */ |
| 270 | if (fc->flags & AFS_FS_CURSOR_VMOVED) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 271 | fc->error = -EREMOTEIO; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 272 | goto failed; |
| 273 | } |
| 274 | fc->flags |= AFS_FS_CURSOR_VMOVED; |
| 275 | |
| 276 | set_bit(AFS_VOLUME_WAIT, &vnode->volume->flags); |
| 277 | set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 278 | error = afs_check_volume_status(vnode->volume, fc->key); |
| 279 | if (error < 0) |
| 280 | goto failed_set_error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 281 | |
| 282 | /* If the server list didn't change, then the VLDB is |
| 283 | * out of sync with the fileservers. This is hopefully |
| 284 | * a temporary condition, however, so we don't want to |
| 285 | * permanently block access to the file. |
| 286 | * |
| 287 | * TODO: Try other fileservers if we can. |
| 288 | * |
| 289 | * TODO: Retry a few times with sleeps. |
| 290 | */ |
| 291 | if (vnode->volume->servers == fc->server_list) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 292 | fc->error = -ENOMEDIUM; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 293 | goto failed; |
| 294 | } |
| 295 | |
| 296 | goto restart_from_beginning; |
| 297 | |
| 298 | default: |
| 299 | clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags); |
| 300 | clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 301 | fc->error = afs_abort_to_error(fc->ac.abort_code); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 302 | goto failed; |
| 303 | } |
| 304 | |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 305 | case -ETIMEDOUT: |
| 306 | case -ETIME: |
| 307 | if (fc->error != -EDESTADDRREQ) |
| 308 | goto iterate_address; |
| 309 | /* Fall through */ |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 310 | case -ERFKILL: |
| 311 | case -EADDRNOTAVAIL: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 312 | case -ENETUNREACH: |
| 313 | case -EHOSTUNREACH: |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 314 | case -EHOSTDOWN: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 315 | case -ECONNREFUSED: |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 316 | _debug("no conn"); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 317 | fc->error = error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 318 | goto iterate_address; |
David Howells | 1a02502 | 2018-06-03 02:17:39 +0100 | [diff] [blame] | 319 | |
| 320 | case -ECONNRESET: |
| 321 | _debug("call reset"); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 322 | fc->error = error; |
David Howells | 1a02502 | 2018-06-03 02:17:39 +0100 | [diff] [blame] | 323 | goto failed; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | restart_from_beginning: |
| 327 | _debug("restart"); |
| 328 | afs_end_cursor(&fc->ac); |
| 329 | afs_put_cb_interest(afs_v2net(vnode), fc->cbi); |
| 330 | fc->cbi = NULL; |
| 331 | afs_put_serverlist(afs_v2net(vnode), fc->server_list); |
| 332 | fc->server_list = NULL; |
| 333 | start: |
| 334 | _debug("start"); |
| 335 | /* See if we need to do an update of the volume record. Note that the |
| 336 | * volume may have moved or even have been deleted. |
| 337 | */ |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 338 | error = afs_check_volume_status(vnode->volume, fc->key); |
| 339 | if (error < 0) |
| 340 | goto failed_set_error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 341 | |
| 342 | if (!afs_start_fs_iteration(fc, vnode)) |
| 343 | goto failed; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 344 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 345 | _debug("__ VOL %llx __", vnode->volume->vid); |
| 346 | error = afs_probe_fileservers(afs_v2net(vnode), fc->key, fc->server_list); |
| 347 | if (error < 0) |
| 348 | goto failed_set_error; |
| 349 | |
| 350 | pick_server: |
| 351 | _debug("pick [%lx]", fc->untried); |
| 352 | |
| 353 | error = afs_wait_for_fs_probes(fc->server_list, fc->untried); |
| 354 | if (error < 0) |
| 355 | goto failed_set_error; |
| 356 | |
| 357 | /* Pick the untried server with the lowest RTT. If we have outstanding |
| 358 | * callbacks, we stick with the server we're already using if we can. |
| 359 | */ |
| 360 | if (fc->cbi) { |
| 361 | _debug("cbi %u", fc->index); |
| 362 | if (test_bit(fc->index, &fc->untried)) |
| 363 | goto selected_server; |
| 364 | afs_put_cb_interest(afs_v2net(vnode), fc->cbi); |
| 365 | fc->cbi = NULL; |
| 366 | _debug("nocbi"); |
| 367 | } |
| 368 | |
| 369 | fc->index = -1; |
| 370 | rtt = U32_MAX; |
| 371 | for (i = 0; i < fc->server_list->nr_servers; i++) { |
| 372 | struct afs_server *s = fc->server_list->servers[i].server; |
| 373 | |
| 374 | if (!test_bit(i, &fc->untried) || !s->probe.responded) |
| 375 | continue; |
| 376 | if (s->probe.rtt < rtt) { |
| 377 | fc->index = i; |
| 378 | rtt = s->probe.rtt; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | if (fc->index == -1) |
| 383 | goto no_more_servers; |
| 384 | |
| 385 | selected_server: |
| 386 | _debug("use %d", fc->index); |
| 387 | __clear_bit(fc->index, &fc->untried); |
| 388 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 389 | /* We're starting on a different fileserver from the list. We need to |
| 390 | * check it, create a callback intercept, find its address list and |
| 391 | * probe its capabilities before we use it. |
| 392 | */ |
| 393 | ASSERTCMP(fc->ac.alist, ==, NULL); |
| 394 | server = fc->server_list->servers[fc->index].server; |
| 395 | |
| 396 | if (!afs_check_server_record(fc, server)) |
| 397 | goto failed; |
| 398 | |
| 399 | _debug("USING SERVER: %pU", &server->uuid); |
| 400 | |
| 401 | /* Make sure we've got a callback interest record for this server. We |
| 402 | * have to link it in before we send the request as we can be sent a |
| 403 | * break request before we've finished decoding the reply and |
| 404 | * installing the vnode. |
| 405 | */ |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 406 | error = afs_register_server_cb_interest(vnode, fc->server_list, |
| 407 | fc->index); |
| 408 | if (error < 0) |
| 409 | goto failed_set_error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 410 | |
| 411 | fc->cbi = afs_get_cb_interest(vnode->cb_interest); |
| 412 | |
| 413 | read_lock(&server->fs_lock); |
| 414 | alist = rcu_dereference_protected(server->addresses, |
| 415 | lockdep_is_held(&server->fs_lock)); |
| 416 | afs_get_addrlist(alist); |
| 417 | read_unlock(&server->fs_lock); |
| 418 | |
David Howells | 8305e57 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 419 | memset(&fc->ac, 0, sizeof(fc->ac)); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 420 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 421 | if (!fc->ac.alist) |
| 422 | fc->ac.alist = alist; |
| 423 | else |
| 424 | afs_put_addrlist(alist); |
| 425 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 426 | fc->ac.index = -1; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 427 | |
| 428 | iterate_address: |
| 429 | ASSERT(fc->ac.alist); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 430 | /* Iterate over the current server's address list to try and find an |
| 431 | * address on which it will respond to us. |
| 432 | */ |
David Howells | fe4d774 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 433 | if (!afs_iterate_addresses(&fc->ac)) |
| 434 | goto next_server; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 435 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 436 | _debug("address [%u] %u/%u", fc->index, fc->ac.index, fc->ac.alist->nr_addrs); |
| 437 | |
David Howells | fe4d774 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 438 | _leave(" = t"); |
| 439 | return true; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 440 | |
David Howells | 16280a1 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 441 | next_server: |
| 442 | _debug("next"); |
| 443 | afs_end_cursor(&fc->ac); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 444 | goto pick_server; |
David Howells | 16280a1 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 445 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 446 | no_more_servers: |
David Howells | 16280a1 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 447 | /* That's all the servers poked to no good effect. Try again if some |
| 448 | * of them were busy. |
| 449 | */ |
| 450 | if (fc->flags & AFS_FS_CURSOR_VBUSY) |
| 451 | goto restart_from_beginning; |
| 452 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 453 | e.error = -EDESTADDRREQ; |
| 454 | e.responded = false; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 455 | for (i = 0; i < fc->server_list->nr_servers; i++) { |
| 456 | struct afs_server *s = fc->server_list->servers[i].server; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 457 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 458 | afs_prioritise_error(&e, READ_ONCE(s->probe.error), |
| 459 | s->probe.abort_code); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 460 | } |
| 461 | |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 462 | failed_set_error: |
| 463 | fc->error = error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 464 | failed: |
| 465 | fc->flags |= AFS_FS_CURSOR_STOP; |
David Howells | fe4d774 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 466 | afs_end_cursor(&fc->ac); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 467 | _leave(" = f [failed %d]", fc->error); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 468 | return false; |
| 469 | } |
| 470 | |
| 471 | /* |
| 472 | * Select the same fileserver we used for a vnode before and only that |
| 473 | * fileserver. We use this when we have a lock on that file, which is backed |
| 474 | * only by the fileserver we obtained it from. |
| 475 | */ |
| 476 | bool afs_select_current_fileserver(struct afs_fs_cursor *fc) |
| 477 | { |
| 478 | struct afs_vnode *vnode = fc->vnode; |
| 479 | struct afs_cb_interest *cbi = vnode->cb_interest; |
| 480 | struct afs_addr_list *alist; |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 481 | int error = fc->ac.error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 482 | |
| 483 | _enter(""); |
| 484 | |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 485 | switch (error) { |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 486 | case SHRT_MAX: |
| 487 | if (!cbi) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 488 | fc->error = -ESTALE; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 489 | fc->flags |= AFS_FS_CURSOR_STOP; |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | fc->cbi = afs_get_cb_interest(vnode->cb_interest); |
| 494 | |
| 495 | read_lock(&cbi->server->fs_lock); |
| 496 | alist = rcu_dereference_protected(cbi->server->addresses, |
| 497 | lockdep_is_held(&cbi->server->fs_lock)); |
| 498 | afs_get_addrlist(alist); |
| 499 | read_unlock(&cbi->server->fs_lock); |
| 500 | if (!alist) { |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 501 | fc->error = -ESTALE; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 502 | fc->flags |= AFS_FS_CURSOR_STOP; |
| 503 | return false; |
| 504 | } |
| 505 | |
David Howells | 8305e57 | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 506 | memset(&fc->ac, 0, sizeof(fc->ac)); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 507 | fc->ac.alist = alist; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 508 | fc->ac.index = -1; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 509 | goto iterate_address; |
| 510 | |
| 511 | case 0: |
| 512 | default: |
| 513 | /* Success or local failure. Stop. */ |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 514 | fc->error = error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 515 | fc->flags |= AFS_FS_CURSOR_STOP; |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 516 | _leave(" = f [okay/local %d]", error); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 517 | return false; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 518 | |
| 519 | case -ECONNABORTED: |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 520 | fc->error = afs_abort_to_error(fc->ac.abort_code); |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 521 | fc->flags |= AFS_FS_CURSOR_STOP; |
| 522 | _leave(" = f [abort]"); |
| 523 | return false; |
| 524 | |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 525 | case -ERFKILL: |
| 526 | case -EADDRNOTAVAIL: |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 527 | case -ENETUNREACH: |
| 528 | case -EHOSTUNREACH: |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 529 | case -EHOSTDOWN: |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 530 | case -ECONNREFUSED: |
| 531 | case -ETIMEDOUT: |
| 532 | case -ETIME: |
| 533 | _debug("no conn"); |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 534 | fc->error = error; |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 535 | goto iterate_address; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 536 | } |
| 537 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 538 | iterate_address: |
| 539 | /* Iterate over the current server's address list to try and find an |
| 540 | * address on which it will respond to us. |
| 541 | */ |
| 542 | if (afs_iterate_addresses(&fc->ac)) { |
| 543 | _leave(" = t"); |
| 544 | return true; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 545 | } |
| 546 | |
David Howells | 0fafdc9 | 2017-11-13 16:59:50 +0000 | [diff] [blame] | 547 | afs_end_cursor(&fc->ac); |
| 548 | return false; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 549 | } |
| 550 | |
| 551 | /* |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 552 | * Dump cursor state in the case of the error being EDESTADDRREQ. |
| 553 | */ |
| 554 | static void afs_dump_edestaddrreq(const struct afs_fs_cursor *fc) |
| 555 | { |
| 556 | static int count; |
| 557 | int i; |
| 558 | |
| 559 | if (!IS_ENABLED(CONFIG_AFS_DEBUG_CURSOR) || count > 3) |
| 560 | return; |
| 561 | count++; |
| 562 | |
| 563 | rcu_read_lock(); |
| 564 | |
| 565 | pr_notice("EDESTADDR occurred\n"); |
| 566 | pr_notice("FC: cbb=%x cbb2=%x fl=%hx err=%hd\n", |
| 567 | fc->cb_break, fc->cb_break_2, fc->flags, fc->error); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 568 | pr_notice("FC: ut=%lx ix=%d ni=%u\n", |
| 569 | fc->untried, fc->index, fc->nr_iterations); |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 570 | |
| 571 | if (fc->server_list) { |
| 572 | const struct afs_server_list *sl = fc->server_list; |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 573 | pr_notice("FC: SL nr=%u pr=%u vnov=%hx\n", |
| 574 | sl->nr_servers, sl->preferred, sl->vnovol_mask); |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 575 | for (i = 0; i < sl->nr_servers; i++) { |
| 576 | const struct afs_server *s = sl->servers[i].server; |
| 577 | pr_notice("FC: server fl=%lx av=%u %pU\n", |
| 578 | s->flags, s->addr_version, &s->uuid); |
| 579 | if (s->addresses) { |
| 580 | const struct afs_addr_list *a = |
| 581 | rcu_dereference(s->addresses); |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 582 | pr_notice("FC: - av=%u nr=%u/%u/%u pr=%u\n", |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 583 | a->version, |
| 584 | a->nr_ipv4, a->nr_addrs, a->max_addrs, |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 585 | a->preferred); |
| 586 | pr_notice("FC: - pr=%lx R=%lx F=%lx\n", |
| 587 | a->probed, a->responded, a->failed); |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 588 | if (a == fc->ac.alist) |
| 589 | pr_notice("FC: - current\n"); |
| 590 | } |
| 591 | } |
| 592 | } |
| 593 | |
David Howells | 3bf0fb6 | 2018-10-20 00:57:59 +0100 | [diff] [blame] | 594 | pr_notice("AC: t=%lx ax=%u ac=%d er=%d r=%u ni=%u\n", |
| 595 | fc->ac.tried, fc->ac.index, fc->ac.abort_code, fc->ac.error, |
| 596 | fc->ac.responded, fc->ac.nr_iterations); |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 597 | rcu_read_unlock(); |
| 598 | } |
| 599 | |
| 600 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 601 | * Tidy up a filesystem cursor and unlock the vnode. |
| 602 | */ |
| 603 | int afs_end_vnode_operation(struct afs_fs_cursor *fc) |
| 604 | { |
| 605 | struct afs_net *net = afs_v2net(fc->vnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 606 | |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 607 | if (fc->error == -EDESTADDRREQ || |
David Howells | 4584ae9 | 2018-11-13 23:20:28 +0000 | [diff] [blame] | 608 | fc->error == -EADDRNOTAVAIL || |
David Howells | 744bcd7 | 2018-10-20 00:57:58 +0100 | [diff] [blame] | 609 | fc->error == -ENETUNREACH || |
| 610 | fc->error == -EHOSTUNREACH) |
| 611 | afs_dump_edestaddrreq(fc); |
| 612 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 613 | mutex_unlock(&fc->vnode->io_lock); |
| 614 | |
| 615 | afs_end_cursor(&fc->ac); |
| 616 | afs_put_cb_interest(net, fc->cbi); |
| 617 | afs_put_serverlist(net, fc->server_list); |
| 618 | |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 619 | if (fc->error == -ECONNABORTED) |
| 620 | fc->error = afs_abort_to_error(fc->ac.abort_code); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 621 | |
David Howells | e7f680f | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 622 | return fc->error; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 623 | } |