Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 2 | * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * This software may be freely redistributed under the terms of the |
| 5 | * GNU General Public License. |
| 6 | * |
| 7 | * You should have received a copy of the GNU General Public License |
| 8 | * along with this program; if not, write to the Free Software |
| 9 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 10 | * |
David Woodhouse | 44d1b98 | 2008-06-05 22:46:18 -0700 | [diff] [blame] | 11 | * Authors: David Woodhouse <dwmw2@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * David Howells <dhowells@redhat.com> |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 19 | #include <linux/circ_buf.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include "internal.h" |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 22 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 23 | /* |
| 24 | * Set up an interest-in-callbacks record for a volume on a server and |
| 25 | * register it with the server. |
| 26 | * - Called with volume->server_sem held. |
| 27 | */ |
| 28 | int afs_register_server_cb_interest(struct afs_vnode *vnode, |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 29 | struct afs_server_entry *entry) |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 30 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 31 | struct afs_cb_interest *cbi = entry->cb_interest, *vcbi, *new, *x; |
| 32 | struct afs_server *server = entry->server; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 33 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 34 | again: |
| 35 | vcbi = vnode->cb_interest; |
| 36 | if (vcbi) { |
| 37 | if (vcbi == cbi) |
| 38 | return 0; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 39 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 40 | if (cbi && vcbi->server == cbi->server) { |
| 41 | write_seqlock(&vnode->cb_lock); |
| 42 | vnode->cb_interest = afs_get_cb_interest(cbi); |
| 43 | write_sequnlock(&vnode->cb_lock); |
| 44 | afs_put_cb_interest(afs_v2net(vnode), cbi); |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | if (!cbi && vcbi->server == server) { |
| 49 | afs_get_cb_interest(vcbi); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 50 | x = cmpxchg(&entry->cb_interest, cbi, vcbi); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 51 | if (x != cbi) { |
| 52 | cbi = x; |
| 53 | afs_put_cb_interest(afs_v2net(vnode), vcbi); |
| 54 | goto again; |
| 55 | } |
| 56 | return 0; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | if (!cbi) { |
| 61 | new = kzalloc(sizeof(struct afs_cb_interest), GFP_KERNEL); |
| 62 | if (!new) |
| 63 | return -ENOMEM; |
| 64 | |
| 65 | refcount_set(&new->usage, 1); |
| 66 | new->sb = vnode->vfs_inode.i_sb; |
| 67 | new->vid = vnode->volume->vid; |
| 68 | new->server = afs_get_server(server); |
| 69 | INIT_LIST_HEAD(&new->cb_link); |
| 70 | |
| 71 | write_lock(&server->cb_break_lock); |
| 72 | list_add_tail(&new->cb_link, &server->cb_interests); |
| 73 | write_unlock(&server->cb_break_lock); |
| 74 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 75 | x = cmpxchg(&entry->cb_interest, cbi, new); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 76 | if (x == cbi) { |
| 77 | cbi = new; |
| 78 | } else { |
| 79 | cbi = x; |
| 80 | afs_put_cb_interest(afs_v2net(vnode), new); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ASSERT(cbi); |
| 85 | |
| 86 | /* Change the server the vnode is using. This entails scrubbing any |
| 87 | * interest the vnode had in the previous server it was using. |
| 88 | */ |
| 89 | write_seqlock(&vnode->cb_lock); |
| 90 | |
| 91 | vnode->cb_interest = afs_get_cb_interest(cbi); |
| 92 | vnode->cb_s_break = cbi->server->cb_s_break; |
| 93 | clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
| 94 | |
| 95 | write_sequnlock(&vnode->cb_lock); |
| 96 | return 0; |
| 97 | } |
| 98 | |
| 99 | /* |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 100 | * Remove an interest on a server. |
| 101 | */ |
| 102 | void afs_put_cb_interest(struct afs_net *net, struct afs_cb_interest *cbi) |
| 103 | { |
| 104 | if (cbi && refcount_dec_and_test(&cbi->usage)) { |
| 105 | if (!list_empty(&cbi->cb_link)) { |
| 106 | write_lock(&cbi->server->cb_break_lock); |
| 107 | list_del_init(&cbi->cb_link); |
| 108 | write_unlock(&cbi->server->cb_break_lock); |
| 109 | afs_put_server(net, cbi->server); |
| 110 | } |
| 111 | kfree(cbi); |
| 112 | } |
| 113 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | /* |
| 116 | * allow the fileserver to request callback state (re-)initialisation |
| 117 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 118 | void afs_init_callback_state(struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 120 | if (!test_and_clear_bit(AFS_SERVER_FL_NEW, &server->flags)) |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 121 | server->cb_s_break++; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | /* |
| 125 | * actually break a callback |
| 126 | */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 127 | void afs_break_callback(struct afs_vnode *vnode) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 128 | { |
| 129 | _enter(""); |
| 130 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 131 | write_seqlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 132 | |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 133 | clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 134 | if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) { |
| 135 | vnode->cb_break++; |
| 136 | afs_clear_permits(vnode); |
| 137 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 138 | spin_lock(&vnode->lock); |
| 139 | |
| 140 | _debug("break callback"); |
| 141 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 142 | if (list_empty(&vnode->granted_locks) && |
| 143 | !list_empty(&vnode->pending_locks)) |
| 144 | afs_lock_may_be_available(vnode); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 145 | spin_unlock(&vnode->lock); |
| 146 | } |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 147 | |
| 148 | write_sequnlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * allow the fileserver to explicitly break one callback |
| 153 | * - happens when |
| 154 | * - the backing file is changed |
| 155 | * - a lock is released |
| 156 | */ |
| 157 | static void afs_break_one_callback(struct afs_server *server, |
| 158 | struct afs_fid *fid) |
| 159 | { |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 160 | struct afs_cb_interest *cbi; |
| 161 | struct afs_iget_data data; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 162 | struct afs_vnode *vnode; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 163 | struct inode *inode; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 164 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 165 | read_lock(&server->cb_break_lock); |
| 166 | |
| 167 | /* Step through all interested superblocks. There may be more than one |
| 168 | * because of cell aliasing. |
| 169 | */ |
| 170 | list_for_each_entry(cbi, &server->cb_interests, cb_link) { |
| 171 | if (cbi->vid != fid->vid) |
| 172 | continue; |
| 173 | |
| 174 | data.volume = NULL; |
| 175 | data.fid = *fid; |
| 176 | inode = ilookup5_nowait(cbi->sb, fid->vnode, afs_iget5_test, &data); |
| 177 | if (inode) { |
| 178 | vnode = AFS_FS_I(inode); |
| 179 | afs_break_callback(vnode); |
| 180 | iput(inode); |
| 181 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 182 | } |
| 183 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 184 | read_unlock(&server->cb_break_lock); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 185 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /* |
| 188 | * allow the fileserver to break callback promises |
| 189 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 190 | void afs_break_callbacks(struct afs_server *server, size_t count, |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 191 | struct afs_callback_break *callbacks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 193 | _enter("%p,%zu,", server, count); |
| 194 | |
| 195 | ASSERT(server != NULL); |
| 196 | ASSERTCMP(count, <=, AFSCBMAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | |
| 198 | for (; count > 0; callbacks++, count--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }", |
| 200 | callbacks->fid.vid, |
| 201 | callbacks->fid.vnode, |
| 202 | callbacks->fid.unique, |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 203 | callbacks->cb.version, |
| 204 | callbacks->cb.expiry, |
| 205 | callbacks->cb.type |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 206 | ); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 207 | afs_break_one_callback(server, &callbacks->fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | } |
| 209 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 210 | _leave(""); |
| 211 | return; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 212 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 213 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | /* |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 215 | * Clear the callback interests in a server list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 217 | void afs_clear_callback_interests(struct afs_net *net, struct afs_server_list *slist) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | { |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 219 | int i; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 220 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 221 | for (i = 0; i < slist->nr_servers; i++) { |
| 222 | afs_put_cb_interest(net, slist->servers[i].cb_interest); |
| 223 | slist->servers[i].cb_interest = NULL; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 224 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 225 | } |