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. |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 26 | * - Called with vnode->io_lock held. |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 27 | */ |
| 28 | int afs_register_server_cb_interest(struct afs_vnode *vnode, |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 29 | struct afs_server_list *slist, |
| 30 | unsigned int index) |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 31 | { |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 32 | struct afs_server_entry *entry = &slist->servers[index]; |
| 33 | struct afs_cb_interest *cbi, *vcbi, *new, *old; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 34 | struct afs_server *server = entry->server; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 35 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 36 | again: |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 37 | if (vnode->cb_interest && |
| 38 | likely(vnode->cb_interest == entry->cb_interest)) |
| 39 | return 0; |
| 40 | |
| 41 | read_lock(&slist->lock); |
| 42 | cbi = afs_get_cb_interest(entry->cb_interest); |
| 43 | read_unlock(&slist->lock); |
| 44 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 45 | vcbi = vnode->cb_interest; |
| 46 | if (vcbi) { |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 47 | if (vcbi == cbi) { |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 48 | afs_put_cb_interest(afs_v2net(vnode), cbi); |
| 49 | return 0; |
| 50 | } |
| 51 | |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 52 | /* Use a new interest in the server list for the same server |
| 53 | * rather than an old one that's still attached to a vnode. |
| 54 | */ |
| 55 | if (cbi && vcbi->server == cbi->server) { |
| 56 | write_seqlock(&vnode->cb_lock); |
| 57 | old = vnode->cb_interest; |
| 58 | vnode->cb_interest = cbi; |
| 59 | write_sequnlock(&vnode->cb_lock); |
| 60 | afs_put_cb_interest(afs_v2net(vnode), old); |
| 61 | return 0; |
| 62 | } |
| 63 | |
| 64 | /* Re-use the one attached to the vnode. */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 65 | if (!cbi && vcbi->server == server) { |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 66 | write_lock(&slist->lock); |
| 67 | if (entry->cb_interest) { |
| 68 | write_unlock(&slist->lock); |
| 69 | afs_put_cb_interest(afs_v2net(vnode), cbi); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 70 | goto again; |
| 71 | } |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 72 | |
| 73 | entry->cb_interest = cbi; |
| 74 | write_unlock(&slist->lock); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | if (!cbi) { |
| 80 | new = kzalloc(sizeof(struct afs_cb_interest), GFP_KERNEL); |
| 81 | if (!new) |
| 82 | return -ENOMEM; |
| 83 | |
| 84 | refcount_set(&new->usage, 1); |
| 85 | new->sb = vnode->vfs_inode.i_sb; |
| 86 | new->vid = vnode->volume->vid; |
| 87 | new->server = afs_get_server(server); |
| 88 | INIT_LIST_HEAD(&new->cb_link); |
| 89 | |
| 90 | write_lock(&server->cb_break_lock); |
| 91 | list_add_tail(&new->cb_link, &server->cb_interests); |
| 92 | write_unlock(&server->cb_break_lock); |
| 93 | |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 94 | write_lock(&slist->lock); |
| 95 | if (!entry->cb_interest) { |
| 96 | entry->cb_interest = afs_get_cb_interest(new); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 97 | cbi = new; |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 98 | new = NULL; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 99 | } else { |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 100 | cbi = afs_get_cb_interest(entry->cb_interest); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 101 | } |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 102 | write_unlock(&slist->lock); |
| 103 | afs_put_cb_interest(afs_v2net(vnode), new); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | ASSERT(cbi); |
| 107 | |
| 108 | /* Change the server the vnode is using. This entails scrubbing any |
| 109 | * interest the vnode had in the previous server it was using. |
| 110 | */ |
| 111 | write_seqlock(&vnode->cb_lock); |
| 112 | |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 113 | old = vnode->cb_interest; |
| 114 | vnode->cb_interest = cbi; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 115 | vnode->cb_s_break = cbi->server->cb_s_break; |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame^] | 116 | vnode->cb_v_break = vnode->volume->cb_v_break; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 117 | clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
| 118 | |
| 119 | write_sequnlock(&vnode->cb_lock); |
David Howells | d4a96bec | 2018-05-10 08:43:04 +0100 | [diff] [blame] | 120 | afs_put_cb_interest(afs_v2net(vnode), old); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 125 | * Remove an interest on a server. |
| 126 | */ |
| 127 | void afs_put_cb_interest(struct afs_net *net, struct afs_cb_interest *cbi) |
| 128 | { |
| 129 | if (cbi && refcount_dec_and_test(&cbi->usage)) { |
| 130 | if (!list_empty(&cbi->cb_link)) { |
| 131 | write_lock(&cbi->server->cb_break_lock); |
| 132 | list_del_init(&cbi->cb_link); |
| 133 | write_unlock(&cbi->server->cb_break_lock); |
| 134 | afs_put_server(net, cbi->server); |
| 135 | } |
| 136 | kfree(cbi); |
| 137 | } |
| 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
| 141 | * allow the fileserver to request callback state (re-)initialisation |
| 142 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 143 | void afs_init_callback_state(struct afs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 145 | if (!test_and_clear_bit(AFS_SERVER_FL_NEW, &server->flags)) |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 146 | server->cb_s_break++; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /* |
| 150 | * actually break a callback |
| 151 | */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 152 | void afs_break_callback(struct afs_vnode *vnode) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 153 | { |
| 154 | _enter(""); |
| 155 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 156 | write_seqlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 157 | |
David Howells | 5a81327 | 2018-04-06 14:17:26 +0100 | [diff] [blame] | 158 | clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 159 | if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) { |
| 160 | vnode->cb_break++; |
| 161 | afs_clear_permits(vnode); |
| 162 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 163 | spin_lock(&vnode->lock); |
| 164 | |
| 165 | _debug("break callback"); |
| 166 | |
David Howells | e8d6c55 | 2007-07-15 23:40:12 -0700 | [diff] [blame] | 167 | if (list_empty(&vnode->granted_locks) && |
| 168 | !list_empty(&vnode->pending_locks)) |
| 169 | afs_lock_may_be_available(vnode); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 170 | spin_unlock(&vnode->lock); |
| 171 | } |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 172 | |
| 173 | write_sequnlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /* |
| 177 | * allow the fileserver to explicitly break one callback |
| 178 | * - happens when |
| 179 | * - the backing file is changed |
| 180 | * - a lock is released |
| 181 | */ |
| 182 | static void afs_break_one_callback(struct afs_server *server, |
| 183 | struct afs_fid *fid) |
| 184 | { |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 185 | struct afs_cb_interest *cbi; |
| 186 | struct afs_iget_data data; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 187 | struct afs_vnode *vnode; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 188 | struct inode *inode; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 189 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 190 | read_lock(&server->cb_break_lock); |
| 191 | |
| 192 | /* Step through all interested superblocks. There may be more than one |
| 193 | * because of cell aliasing. |
| 194 | */ |
| 195 | list_for_each_entry(cbi, &server->cb_interests, cb_link) { |
| 196 | if (cbi->vid != fid->vid) |
| 197 | continue; |
| 198 | |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame^] | 199 | if (fid->vnode == 0 && fid->unique == 0) { |
| 200 | /* The callback break applies to an entire volume. */ |
| 201 | struct afs_super_info *as = AFS_FS_S(cbi->sb); |
| 202 | struct afs_volume *volume = as->volume; |
| 203 | |
| 204 | write_lock(&volume->cb_break_lock); |
| 205 | volume->cb_v_break++; |
| 206 | write_unlock(&volume->cb_break_lock); |
| 207 | } else { |
| 208 | data.volume = NULL; |
| 209 | data.fid = *fid; |
| 210 | inode = ilookup5_nowait(cbi->sb, fid->vnode, |
| 211 | afs_iget5_test, &data); |
| 212 | if (inode) { |
| 213 | vnode = AFS_FS_I(inode); |
| 214 | afs_break_callback(vnode); |
| 215 | iput(inode); |
| 216 | } |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 217 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 218 | } |
| 219 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 220 | read_unlock(&server->cb_break_lock); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 221 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | /* |
| 224 | * allow the fileserver to break callback promises |
| 225 | */ |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 226 | void afs_break_callbacks(struct afs_server *server, size_t count, |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 227 | struct afs_callback_break *callbacks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 228 | { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 229 | _enter("%p,%zu,", server, count); |
| 230 | |
| 231 | ASSERT(server != NULL); |
| 232 | ASSERTCMP(count, <=, AFSCBMAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame^] | 234 | /* TODO: Sort the callback break list by volume ID */ |
| 235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | for (; count > 0; callbacks++, count--) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }", |
| 238 | callbacks->fid.vid, |
| 239 | callbacks->fid.vnode, |
| 240 | callbacks->fid.unique, |
David Howells | 5cf9dd5 | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 241 | callbacks->cb.version, |
| 242 | callbacks->cb.expiry, |
| 243 | callbacks->cb.type |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | ); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 245 | afs_break_one_callback(server, &callbacks->fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 246 | } |
| 247 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 248 | _leave(""); |
| 249 | return; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 250 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | /* |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 253 | * Clear the callback interests in a server list. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | */ |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 255 | 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] | 256 | { |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 257 | int i; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 258 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 259 | for (i = 0; i < slist->nr_servers; i++) { |
| 260 | afs_put_cb_interest(net, slist->servers[i].cb_interest); |
| 261 | slist->servers[i].cb_interest = NULL; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 262 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 263 | } |