1 /* Handle fileserver selection and rotation.
3 * Copyright (C) 2017 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
15 #include <linux/sched.h>
16 #include <linux/delay.h>
17 #include <linux/sched/signal.h>
22 * Initialise a filesystem server cursor for iterating over FS servers.
24 static void afs_init_fs_cursor(struct afs_fs_cursor *fc, struct afs_vnode *vnode)
26 memset(fc, 0, sizeof(*fc));
30 * Begin an operation on the fileserver.
32 * Fileserver operations are serialised on the server by vnode, so we serialise
33 * them here also using the io_lock.
35 bool afs_begin_vnode_operation(struct afs_fs_cursor *fc, struct afs_vnode *vnode,
38 afs_init_fs_cursor(fc, vnode);
41 fc->ac.error = SHRT_MAX;
42 fc->error = -EDESTADDRREQ;
44 if (mutex_lock_interruptible(&vnode->io_lock) < 0) {
46 fc->flags |= AFS_FS_CURSOR_STOP;
50 if (vnode->lock_state != AFS_VNODE_LOCK_NONE)
51 fc->flags |= AFS_FS_CURSOR_CUR_ONLY;
56 * Begin iteration through a server list, starting with the vnode's last used
57 * server if possible, or the last recorded good server if not.
59 static bool afs_start_fs_iteration(struct afs_fs_cursor *fc,
60 struct afs_vnode *vnode)
62 struct afs_cb_interest *cbi;
65 read_lock(&vnode->volume->servers_lock);
66 fc->server_list = afs_get_serverlist(vnode->volume->servers);
67 read_unlock(&vnode->volume->servers_lock);
69 cbi = vnode->cb_interest;
71 /* See if the vnode's preferred record is still available */
72 for (i = 0; i < fc->server_list->nr_servers; i++) {
73 if (fc->server_list->servers[i].cb_interest == cbi) {
79 /* If we have a lock outstanding on a server that's no longer
80 * serving this vnode, then we can't switch to another server
81 * and have to return an error.
83 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
88 /* Note that the callback promise is effectively broken */
89 write_seqlock(&vnode->cb_lock);
90 ASSERTCMP(cbi, ==, vnode->cb_interest);
91 vnode->cb_interest = NULL;
92 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
94 write_sequnlock(&vnode->cb_lock);
96 afs_put_cb_interest(afs_v2net(vnode), cbi);
99 fc->start = READ_ONCE(fc->server_list->index);
103 fc->index = fc->start;
108 * Post volume busy note.
110 static void afs_busy(struct afs_volume *volume, u32 abort_code)
114 switch (abort_code) {
115 case VOFFLINE: m = "offline"; break;
116 case VRESTARTING: m = "restarting"; break;
117 case VSALVAGING: m = "being salvaged"; break;
118 default: m = "busy"; break;
121 pr_notice("kAFS: Volume %u '%s' is %s\n", volume->vid, volume->name, m);
125 * Sleep and retry the operation to the same fileserver.
127 static bool afs_sleep_and_retry(struct afs_fs_cursor *fc)
129 msleep_interruptible(1000);
130 if (signal_pending(current)) {
131 fc->error = -ERESTARTSYS;
139 * Select the fileserver to use. May be called multiple times to rotate
140 * through the fileservers.
142 bool afs_select_fileserver(struct afs_fs_cursor *fc)
144 struct afs_addr_list *alist;
145 struct afs_server *server;
146 struct afs_vnode *vnode = fc->vnode;
147 int error = fc->ac.error;
149 _enter("%u/%u,%u/%u,%d,%d",
150 fc->index, fc->start,
151 fc->ac.index, fc->ac.start,
152 error, fc->ac.abort_code);
154 if (fc->flags & AFS_FS_CURSOR_STOP) {
155 _leave(" = f [stopped]");
159 /* Evaluate the result of the previous operation, if there was one. */
166 /* Success or local failure. Stop. */
168 fc->flags |= AFS_FS_CURSOR_STOP;
169 _leave(" = f [okay/local %d]", error);
173 /* The far side rejected the operation on some grounds. This
174 * might involve the server being busy or the volume having been moved.
176 switch (fc->ac.abort_code) {
178 /* This fileserver doesn't know about the volume.
179 * - May indicate that the VL is wrong - retry once and compare
181 * - May indicate that the fileserver couldn't attach to the vol.
183 if (fc->flags & AFS_FS_CURSOR_VNOVOL) {
184 fc->error = -EREMOTEIO;
188 write_lock(&vnode->volume->servers_lock);
189 fc->server_list->vnovol_mask |= 1 << fc->index;
190 write_unlock(&vnode->volume->servers_lock);
192 set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags);
193 error = afs_check_volume_status(vnode->volume, fc->key);
195 goto failed_set_error;
197 if (test_bit(AFS_VOLUME_DELETED, &vnode->volume->flags)) {
198 fc->error = -ENOMEDIUM;
202 /* If the server list didn't change, then assume that
203 * it's the fileserver having trouble.
205 if (vnode->volume->servers == fc->server_list) {
206 fc->error = -EREMOTEIO;
211 fc->flags |= AFS_FS_CURSOR_VNOVOL;
212 _leave(" = t [vnovol]");
215 case VSALVAGE: /* TODO: Should this return an error or iterate? */
221 fc->error = afs_abort_to_error(fc->ac.abort_code);
225 if (!test_and_set_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags)) {
226 afs_busy(vnode->volume, fc->ac.abort_code);
227 clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags);
229 if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) {
233 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
242 /* Retry after going round all the servers unless we
243 * have a file lock we need to maintain.
245 if (fc->flags & AFS_FS_CURSOR_NO_VSLEEP) {
249 if (!test_and_set_bit(AFS_VOLUME_BUSY, &vnode->volume->flags)) {
250 afs_busy(vnode->volume, fc->ac.abort_code);
251 clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags);
254 if (fc->flags & AFS_FS_CURSOR_CUR_ONLY) {
255 if (!afs_sleep_and_retry(fc))
258 /* Retry with same server & address */
259 _leave(" = t [vbusy]");
263 fc->flags |= AFS_FS_CURSOR_VBUSY;
267 /* The volume migrated to another server. We consider
268 * consider all locks and callbacks broken and request
269 * an update from the VLDB.
271 * We also limit the number of VMOVED hops we will
272 * honour, just in case someone sets up a loop.
274 if (fc->flags & AFS_FS_CURSOR_VMOVED) {
275 fc->error = -EREMOTEIO;
278 fc->flags |= AFS_FS_CURSOR_VMOVED;
280 set_bit(AFS_VOLUME_WAIT, &vnode->volume->flags);
281 set_bit(AFS_VOLUME_NEEDS_UPDATE, &vnode->volume->flags);
282 error = afs_check_volume_status(vnode->volume, fc->key);
284 goto failed_set_error;
286 /* If the server list didn't change, then the VLDB is
287 * out of sync with the fileservers. This is hopefully
288 * a temporary condition, however, so we don't want to
289 * permanently block access to the file.
291 * TODO: Try other fileservers if we can.
293 * TODO: Retry a few times with sleeps.
295 if (vnode->volume->servers == fc->server_list) {
296 fc->error = -ENOMEDIUM;
300 goto restart_from_beginning;
303 clear_bit(AFS_VOLUME_OFFLINE, &vnode->volume->flags);
304 clear_bit(AFS_VOLUME_BUSY, &vnode->volume->flags);
305 fc->error = afs_abort_to_error(fc->ac.abort_code);
311 if (fc->error != -EDESTADDRREQ)
312 goto iterate_address;
319 goto iterate_address;
322 _debug("call reset");
327 restart_from_beginning:
329 afs_end_cursor(&fc->ac);
330 afs_put_cb_interest(afs_v2net(vnode), fc->cbi);
332 afs_put_serverlist(afs_v2net(vnode), fc->server_list);
333 fc->server_list = NULL;
336 /* See if we need to do an update of the volume record. Note that the
337 * volume may have moved or even have been deleted.
339 error = afs_check_volume_status(vnode->volume, fc->key);
341 goto failed_set_error;
343 if (!afs_start_fs_iteration(fc, vnode))
348 /* We're starting on a different fileserver from the list. We need to
349 * check it, create a callback intercept, find its address list and
350 * probe its capabilities before we use it.
352 ASSERTCMP(fc->ac.alist, ==, NULL);
353 server = fc->server_list->servers[fc->index].server;
355 if (!afs_check_server_record(fc, server))
358 _debug("USING SERVER: %pU", &server->uuid);
360 /* Make sure we've got a callback interest record for this server. We
361 * have to link it in before we send the request as we can be sent a
362 * break request before we've finished decoding the reply and
363 * installing the vnode.
365 error = afs_register_server_cb_interest(vnode, fc->server_list,
368 goto failed_set_error;
370 fc->cbi = afs_get_cb_interest(vnode->cb_interest);
372 read_lock(&server->fs_lock);
373 alist = rcu_dereference_protected(server->addresses,
374 lockdep_is_held(&server->fs_lock));
375 afs_get_addrlist(alist);
376 read_unlock(&server->fs_lock);
378 memset(&fc->ac, 0, sizeof(fc->ac));
380 /* Probe the current fileserver if we haven't done so yet. */
381 if (!test_bit(AFS_SERVER_FL_PROBED, &server->flags)) {
382 fc->ac.alist = afs_get_addrlist(alist);
384 if (!afs_probe_fileserver(fc)) {
385 switch (fc->ac.error) {
397 fc->ac.alist = alist;
399 afs_put_addrlist(alist);
401 fc->ac.start = READ_ONCE(alist->index);
402 fc->ac.index = fc->ac.start;
405 ASSERT(fc->ac.alist);
406 _debug("iterate %d/%d", fc->ac.index, fc->ac.alist->nr_addrs);
407 /* Iterate over the current server's address list to try and find an
408 * address on which it will respond to us.
410 if (!afs_iterate_addresses(&fc->ac))
418 afs_end_cursor(&fc->ac);
419 afs_put_cb_interest(afs_v2net(vnode), fc->cbi);
422 if (fc->index >= fc->server_list->nr_servers)
424 if (fc->index != fc->start)
427 /* That's all the servers poked to no good effect. Try again if some
430 if (fc->flags & AFS_FS_CURSOR_VBUSY)
431 goto restart_from_beginning;
438 fc->flags |= AFS_FS_CURSOR_STOP;
439 afs_end_cursor(&fc->ac);
440 _leave(" = f [failed %d]", fc->error);
445 * Select the same fileserver we used for a vnode before and only that
446 * fileserver. We use this when we have a lock on that file, which is backed
447 * only by the fileserver we obtained it from.
449 bool afs_select_current_fileserver(struct afs_fs_cursor *fc)
451 struct afs_vnode *vnode = fc->vnode;
452 struct afs_cb_interest *cbi = vnode->cb_interest;
453 struct afs_addr_list *alist;
454 int error = fc->ac.error;
462 fc->flags |= AFS_FS_CURSOR_STOP;
466 fc->cbi = afs_get_cb_interest(vnode->cb_interest);
468 read_lock(&cbi->server->fs_lock);
469 alist = rcu_dereference_protected(cbi->server->addresses,
470 lockdep_is_held(&cbi->server->fs_lock));
471 afs_get_addrlist(alist);
472 read_unlock(&cbi->server->fs_lock);
475 fc->flags |= AFS_FS_CURSOR_STOP;
479 memset(&fc->ac, 0, sizeof(fc->ac));
480 fc->ac.alist = alist;
481 fc->ac.start = READ_ONCE(alist->index);
482 fc->ac.index = fc->ac.start;
483 goto iterate_address;
487 /* Success or local failure. Stop. */
489 fc->flags |= AFS_FS_CURSOR_STOP;
490 _leave(" = f [okay/local %d]", error);
494 fc->error = afs_abort_to_error(fc->ac.abort_code);
495 fc->flags |= AFS_FS_CURSOR_STOP;
496 _leave(" = f [abort]");
506 goto iterate_address;
510 /* Iterate over the current server's address list to try and find an
511 * address on which it will respond to us.
513 if (afs_iterate_addresses(&fc->ac)) {
518 afs_end_cursor(&fc->ac);
523 * Tidy up a filesystem cursor and unlock the vnode.
525 int afs_end_vnode_operation(struct afs_fs_cursor *fc)
527 struct afs_net *net = afs_v2net(fc->vnode);
529 mutex_unlock(&fc->vnode->io_lock);
531 afs_end_cursor(&fc->ac);
532 afs_put_cb_interest(net, fc->cbi);
533 afs_put_serverlist(net, fc->server_list);
535 if (fc->error == -ECONNABORTED)
536 fc->error = afs_abort_to_error(fc->ac.abort_code);