]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/afs/rotate.c
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 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
;
43 if (mutex_lock_interruptible(&vnode
->io_lock
) < 0) {
44 fc
->ac
.error
= -EINTR
;
45 fc
->flags
|= AFS_FS_CURSOR_STOP
;
49 if (test_bit(AFS_VNODE_READLOCKED
, &vnode
->flags
) ||
50 test_bit(AFS_VNODE_WRITELOCKED
, &vnode
->flags
))
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
) {
84 fc
->ac
.error
= -ESTALE
;
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
->ac
.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
;
148 _enter("%u/%u,%u/%u,%d,%d",
149 fc
->index
, fc
->start
,
150 fc
->ac
.index
, fc
->ac
.start
,
151 fc
->ac
.error
, fc
->ac
.abort_code
);
153 if (fc
->flags
& AFS_FS_CURSOR_STOP
) {
154 _leave(" = f [stopped]");
158 /* Evaluate the result of the previous operation, if there was one. */
159 switch (fc
->ac
.error
) {
165 /* Success or local failure. Stop. */
166 fc
->flags
|= AFS_FS_CURSOR_STOP
;
167 _leave(" = f [okay/local %d]", fc
->ac
.error
);
171 /* The far side rejected the operation on some grounds. This
172 * might involve the server being busy or the volume having been moved.
174 switch (fc
->ac
.abort_code
) {
176 /* This fileserver doesn't know about the volume.
177 * - May indicate that the VL is wrong - retry once and compare
179 * - May indicate that the fileserver couldn't attach to the vol.
181 if (fc
->flags
& AFS_FS_CURSOR_VNOVOL
) {
182 fc
->ac
.error
= -EREMOTEIO
;
186 write_lock(&vnode
->volume
->servers_lock
);
187 fc
->server_list
->vnovol_mask
|= 1 << fc
->index
;
188 write_unlock(&vnode
->volume
->servers_lock
);
190 set_bit(AFS_VOLUME_NEEDS_UPDATE
, &vnode
->volume
->flags
);
191 fc
->ac
.error
= afs_check_volume_status(vnode
->volume
, fc
->key
);
192 if (fc
->ac
.error
< 0)
195 if (test_bit(AFS_VOLUME_DELETED
, &vnode
->volume
->flags
)) {
196 fc
->ac
.error
= -ENOMEDIUM
;
200 /* If the server list didn't change, then assume that
201 * it's the fileserver having trouble.
203 if (vnode
->volume
->servers
== fc
->server_list
) {
204 fc
->ac
.error
= -EREMOTEIO
;
209 fc
->flags
|= AFS_FS_CURSOR_VNOVOL
;
210 _leave(" = t [vnovol]");
213 case VSALVAGE
: /* TODO: Should this return an error or iterate? */
219 fc
->ac
.error
= afs_abort_to_error(fc
->ac
.abort_code
);
223 if (!test_and_set_bit(AFS_VOLUME_OFFLINE
, &vnode
->volume
->flags
)) {
224 afs_busy(vnode
->volume
, fc
->ac
.abort_code
);
225 clear_bit(AFS_VOLUME_BUSY
, &vnode
->volume
->flags
);
227 if (fc
->flags
& AFS_FS_CURSOR_NO_VSLEEP
) {
228 fc
->ac
.error
= -EADV
;
231 if (fc
->flags
& AFS_FS_CURSOR_CUR_ONLY
) {
232 fc
->ac
.error
= -ESTALE
;
240 /* Retry after going round all the servers unless we
241 * have a file lock we need to maintain.
243 if (fc
->flags
& AFS_FS_CURSOR_NO_VSLEEP
) {
244 fc
->ac
.error
= -EBUSY
;
247 if (!test_and_set_bit(AFS_VOLUME_BUSY
, &vnode
->volume
->flags
)) {
248 afs_busy(vnode
->volume
, fc
->ac
.abort_code
);
249 clear_bit(AFS_VOLUME_OFFLINE
, &vnode
->volume
->flags
);
252 if (fc
->flags
& AFS_FS_CURSOR_CUR_ONLY
) {
253 if (!afs_sleep_and_retry(fc
))
256 /* Retry with same server & address */
257 _leave(" = t [vbusy]");
261 fc
->flags
|= AFS_FS_CURSOR_VBUSY
;
265 /* The volume migrated to another server. We consider
266 * consider all locks and callbacks broken and request
267 * an update from the VLDB.
269 * We also limit the number of VMOVED hops we will
270 * honour, just in case someone sets up a loop.
272 if (fc
->flags
& AFS_FS_CURSOR_VMOVED
) {
273 fc
->ac
.error
= -EREMOTEIO
;
276 fc
->flags
|= AFS_FS_CURSOR_VMOVED
;
278 set_bit(AFS_VOLUME_WAIT
, &vnode
->volume
->flags
);
279 set_bit(AFS_VOLUME_NEEDS_UPDATE
, &vnode
->volume
->flags
);
280 fc
->ac
.error
= afs_check_volume_status(vnode
->volume
, fc
->key
);
281 if (fc
->ac
.error
< 0)
284 /* If the server list didn't change, then the VLDB is
285 * out of sync with the fileservers. This is hopefully
286 * a temporary condition, however, so we don't want to
287 * permanently block access to the file.
289 * TODO: Try other fileservers if we can.
291 * TODO: Retry a few times with sleeps.
293 if (vnode
->volume
->servers
== fc
->server_list
) {
294 fc
->ac
.error
= -ENOMEDIUM
;
298 goto restart_from_beginning
;
301 clear_bit(AFS_VOLUME_OFFLINE
, &vnode
->volume
->flags
);
302 clear_bit(AFS_VOLUME_BUSY
, &vnode
->volume
->flags
);
303 fc
->ac
.error
= afs_abort_to_error(fc
->ac
.abort_code
);
313 goto iterate_address
;
316 restart_from_beginning
:
318 afs_end_cursor(&fc
->ac
);
319 afs_put_cb_interest(afs_v2net(vnode
), fc
->cbi
);
321 afs_put_serverlist(afs_v2net(vnode
), fc
->server_list
);
322 fc
->server_list
= NULL
;
325 /* See if we need to do an update of the volume record. Note that the
326 * volume may have moved or even have been deleted.
328 fc
->ac
.error
= afs_check_volume_status(vnode
->volume
, fc
->key
);
329 if (fc
->ac
.error
< 0)
332 if (!afs_start_fs_iteration(fc
, vnode
))
338 afs_put_cb_interest(afs_v2net(vnode
), fc
->cbi
);
341 if (fc
->index
>= fc
->server_list
->nr_servers
)
343 if (fc
->index
!= fc
->start
)
346 /* That's all the servers poked to no good effect. Try again if some
349 if (fc
->flags
& AFS_FS_CURSOR_VBUSY
)
350 goto restart_from_beginning
;
352 fc
->ac
.error
= -EDESTADDRREQ
;
357 /* We're starting on a different fileserver from the list. We need to
358 * check it, create a callback intercept, find its address list and
359 * probe its capabilities before we use it.
361 ASSERTCMP(fc
->ac
.alist
, ==, NULL
);
362 server
= fc
->server_list
->servers
[fc
->index
].server
;
364 if (!afs_check_server_record(fc
, server
))
367 _debug("USING SERVER: %pU", &server
->uuid
);
369 /* Make sure we've got a callback interest record for this server. We
370 * have to link it in before we send the request as we can be sent a
371 * break request before we've finished decoding the reply and
372 * installing the vnode.
374 fc
->ac
.error
= afs_register_server_cb_interest(
375 vnode
, &fc
->server_list
->servers
[fc
->index
]);
376 if (fc
->ac
.error
< 0)
379 fc
->cbi
= afs_get_cb_interest(vnode
->cb_interest
);
381 read_lock(&server
->fs_lock
);
382 alist
= rcu_dereference_protected(server
->addresses
,
383 lockdep_is_held(&server
->fs_lock
));
384 afs_get_addrlist(alist
);
385 read_unlock(&server
->fs_lock
);
388 /* Probe the current fileserver if we haven't done so yet. */
389 if (!test_bit(AFS_SERVER_FL_PROBED
, &server
->flags
)) {
390 fc
->ac
.alist
= afs_get_addrlist(alist
);
392 if (!afs_probe_fileserver(fc
))
397 fc
->ac
.alist
= alist
;
399 afs_put_addrlist(alist
);
402 fc
->ac
.start
= READ_ONCE(alist
->index
);
403 fc
->ac
.index
= fc
->ac
.start
;
405 fc
->ac
.begun
= false;
406 goto iterate_address
;
409 ASSERT(fc
->ac
.alist
);
410 _debug("iterate %d/%d", fc
->ac
.index
, fc
->ac
.alist
->nr_addrs
);
411 /* Iterate over the current server's address list to try and find an
412 * address on which it will respond to us.
414 if (afs_iterate_addresses(&fc
->ac
)) {
419 afs_end_cursor(&fc
->ac
);
423 fc
->flags
|= AFS_FS_CURSOR_STOP
;
424 _leave(" = f [failed %d]", fc
->ac
.error
);
429 * Select the same fileserver we used for a vnode before and only that
430 * fileserver. We use this when we have a lock on that file, which is backed
431 * only by the fileserver we obtained it from.
433 bool afs_select_current_fileserver(struct afs_fs_cursor
*fc
)
435 struct afs_vnode
*vnode
= fc
->vnode
;
436 struct afs_cb_interest
*cbi
= vnode
->cb_interest
;
437 struct afs_addr_list
*alist
;
442 fc
->ac
.error
= -ESTALE
;
443 fc
->flags
|= AFS_FS_CURSOR_STOP
;
447 read_lock(&cbi
->server
->fs_lock
);
448 alist
= afs_get_addrlist(cbi
->server
->addresses
);
449 read_unlock(&cbi
->server
->fs_lock
);
451 fc
->ac
.error
= -ESTALE
;
452 fc
->flags
|= AFS_FS_CURSOR_STOP
;
456 fc
->ac
.alist
= alist
;
462 * Tidy up a filesystem cursor and unlock the vnode.
464 int afs_end_vnode_operation(struct afs_fs_cursor
*fc
)
466 struct afs_net
*net
= afs_v2net(fc
->vnode
);
469 mutex_unlock(&fc
->vnode
->io_lock
);
471 afs_end_cursor(&fc
->ac
);
472 afs_put_cb_interest(net
, fc
->cbi
);
473 afs_put_serverlist(net
, fc
->server_list
);
476 if (ret
== -ECONNABORTED
)
477 afs_abort_to_error(fc
->ac
.abort_code
);
484 * Set a filesystem server cursor for using a specific FS server.
486 int afs_set_fs_cursor(struct afs_fs_cursor
*fc
, struct afs_vnode
*vnode
)
488 afs_init_fs_cursor(fc
, vnode
);
490 read_seqlock_excl(&vnode
->cb_lock
);
491 if (vnode
->cb_interest
) {
492 if (vnode
->cb_interest
->server
->fs_state
== 0)
493 fc
->server
= afs_get_server(vnode
->cb_interest
->server
);
495 fc
->ac
.error
= vnode
->cb_interest
->server
->fs_state
;
497 fc
->ac
.error
= -ESTALE
;
499 read_sequnlock_excl(&vnode
->cb_lock
);
505 * pick a server to use to try accessing this volume
506 * - returns with an elevated usage count on the server chosen
508 bool afs_volume_pick_fileserver(struct afs_fs_cursor
*fc
, struct afs_vnode
*vnode
)
510 struct afs_volume
*volume
= vnode
->volume
;
511 struct afs_server
*server
;
512 int ret
, state
, loop
;
514 _enter("%s", volume
->vlocation
->vldb
.name
);
516 /* stick with the server we're already using if we can */
517 if (vnode
->cb_interest
&& vnode
->cb_interest
->server
->fs_state
== 0) {
518 fc
->server
= afs_get_server(vnode
->cb_interest
->server
);
522 down_read(&volume
->server_sem
);
524 /* handle the no-server case */
525 if (volume
->nservers
== 0) {
526 fc
->ac
.error
= volume
->rjservers
? -ENOMEDIUM
: -ESTALE
;
527 up_read(&volume
->server_sem
);
528 _leave(" = f [no servers %d]", fc
->ac
.error
);
532 /* basically, just search the list for the first live server and use
535 for (loop
= 0; loop
< volume
->nservers
; loop
++) {
536 server
= volume
->servers
[loop
];
537 state
= server
->fs_state
;
539 _debug("consider %d [%d]", loop
, state
);
558 ret
== -ENETUNREACH
||
559 ret
== -EHOSTUNREACH
)
566 ret
== -ENETUNREACH
||
567 ret
== -EHOSTUNREACH
||
568 ret
== -ECONNREFUSED
)
577 /* no available servers
578 * - TODO: handle the no active servers case better
580 up_read(&volume
->server_sem
);
581 _leave(" = f [%d]", fc
->ac
.error
);
585 /* Found an apparently healthy server. We need to register an interest
586 * in receiving callbacks before we talk to it.
588 ret
= afs_register_server_cb_interest(vnode
,
589 &volume
->cb_interests
[loop
], server
);
593 fc
->server
= afs_get_server(server
);
594 up_read(&volume
->server_sem
);
596 fc
->ac
.alist
= afs_get_addrlist(fc
->server
->addrs
);
597 fc
->ac
.addr
= &fc
->ac
.alist
->addrs
[0];
598 _debug("USING SERVER: %pIS\n", &fc
->ac
.addr
->transport
);
599 _leave(" = t (picked %pIS)", &fc
->ac
.addr
->transport
);
604 * release a server after use
605 * - releases the ref on the server struct that was acquired by picking
606 * - records result of using a particular server to access a volume
607 * - return true to try again, false if okay or to issue error
608 * - the caller must release the server struct if result was false
610 bool afs_iterate_fs_cursor(struct afs_fs_cursor
*fc
,
611 struct afs_vnode
*vnode
)
613 struct afs_volume
*volume
= vnode
->volume
;
614 struct afs_server
*server
= fc
->server
;
618 volume
->vlocation
->vldb
.name
, &fc
->ac
.addr
->transport
,
621 switch (fc
->ac
.error
) {
624 server
->fs_state
= 0;
628 /* the fileserver denied all knowledge of the volume */
630 down_write(&volume
->server_sem
);
632 /* firstly, find where the server is in the active list (if it
634 for (loop
= 0; loop
< volume
->nservers
; loop
++)
635 if (volume
->servers
[loop
] == server
)
638 /* no longer there - may have been discarded by another op */
639 goto try_next_server_upw
;
643 memmove(&volume
->servers
[loop
],
644 &volume
->servers
[loop
+ 1],
645 sizeof(volume
->servers
[loop
]) *
646 (volume
->nservers
- loop
));
647 volume
->servers
[volume
->nservers
] = NULL
;
648 afs_put_server(afs_v2net(vnode
), server
);
651 if (volume
->nservers
> 0)
652 /* another server might acknowledge its existence */
653 goto try_next_server_upw
;
655 /* handle the case where all the fileservers have rejected the
657 * - TODO: try asking the fileservers for volume information
658 * - TODO: contact the VL server again to see if the volume is
659 * no longer registered
661 up_write(&volume
->server_sem
);
662 afs_put_server(afs_v2net(vnode
), server
);
664 _leave(" = f [completely rejected]");
667 /* problem reaching the server */
674 /* mark the server as dead
675 * TODO: vary dead timeout depending on error
677 spin_lock(&server
->fs_lock
);
678 if (!server
->fs_state
) {
679 server
->fs_state
= fc
->ac
.error
;
680 printk("kAFS: SERVER DEAD state=%d\n", fc
->ac
.error
);
682 spin_unlock(&server
->fs_lock
);
683 goto try_next_server
;
685 /* miscellaneous error */
689 /* tell the caller to accept the result */
690 afs_put_server(afs_v2net(vnode
), server
);
692 _leave(" = f [local failure]");
696 /* tell the caller to loop around and try the next server */
698 up_write(&volume
->server_sem
);
700 afs_put_server(afs_v2net(vnode
), server
);
701 _leave(" = t [try next server]");
706 * Clean up a fileserver cursor.
708 int afs_end_fs_cursor(struct afs_fs_cursor
*fc
, struct afs_net
*net
)
710 afs_end_cursor(&fc
->ac
);
711 afs_put_server(net
, fc
->server
);