blob: 2dca8df1a18d963722fdeb5b5105d455c1914bc1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
David Howells08e0e7c2007-04-26 15:55:03 -07002 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
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 Woodhouse44d1b982008-06-05 22:46:18 -070011 * Authors: David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * David Howells <dhowells@redhat.com>
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
David Howells08e0e7c2007-04-26 15:55:03 -070019#include <linux/circ_buf.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040020#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
David Howells08e0e7c2007-04-26 15:55:03 -070022
David Howellsc435ee32017-11-02 15:27:49 +000023/*
David Howells47ea0f22018-06-15 15:24:50 +010024 * Create volume and callback interests on a server.
25 */
26static struct afs_cb_interest *afs_create_interest(struct afs_server *server,
27 struct afs_vnode *vnode)
28{
29 struct afs_vol_interest *new_vi, *vi;
30 struct afs_cb_interest *new;
31 struct hlist_node **pp;
32
33 new_vi = kzalloc(sizeof(struct afs_vol_interest), GFP_KERNEL);
34 if (!new_vi)
35 return NULL;
36
37 new = kzalloc(sizeof(struct afs_cb_interest), GFP_KERNEL);
38 if (!new) {
39 kfree(new_vi);
40 return NULL;
41 }
42
43 new_vi->usage = 1;
44 new_vi->vid = vnode->volume->vid;
45 INIT_HLIST_NODE(&new_vi->srv_link);
46 INIT_HLIST_HEAD(&new_vi->cb_interests);
47
48 refcount_set(&new->usage, 1);
49 new->sb = vnode->vfs_inode.i_sb;
50 new->vid = vnode->volume->vid;
David Howells45218192019-06-20 18:12:17 +010051 new->server = afs_get_server(server, afs_server_trace_get_new_cbi);
David Howells47ea0f22018-06-15 15:24:50 +010052 INIT_HLIST_NODE(&new->cb_vlink);
53
54 write_lock(&server->cb_break_lock);
55
56 for (pp = &server->cb_volumes.first; *pp; pp = &(*pp)->next) {
57 vi = hlist_entry(*pp, struct afs_vol_interest, srv_link);
58 if (vi->vid < new_vi->vid)
59 continue;
60 if (vi->vid > new_vi->vid)
61 break;
62 vi->usage++;
63 goto found_vi;
64 }
65
66 new_vi->srv_link.pprev = pp;
67 new_vi->srv_link.next = *pp;
68 if (*pp)
69 (*pp)->pprev = &new_vi->srv_link.next;
70 *pp = &new_vi->srv_link;
71 vi = new_vi;
72 new_vi = NULL;
73found_vi:
74
75 new->vol_interest = vi;
76 hlist_add_head(&new->cb_vlink, &vi->cb_interests);
77
78 write_unlock(&server->cb_break_lock);
79 kfree(new_vi);
80 return new;
81}
82
83/*
David Howellsc435ee32017-11-02 15:27:49 +000084 * Set up an interest-in-callbacks record for a volume on a server and
85 * register it with the server.
David Howellsd4a96bec2018-05-10 08:43:04 +010086 * - Called with vnode->io_lock held.
David Howellsc435ee32017-11-02 15:27:49 +000087 */
88int afs_register_server_cb_interest(struct afs_vnode *vnode,
David Howellsd4a96bec2018-05-10 08:43:04 +010089 struct afs_server_list *slist,
90 unsigned int index)
David Howellsc435ee32017-11-02 15:27:49 +000091{
David Howellsd4a96bec2018-05-10 08:43:04 +010092 struct afs_server_entry *entry = &slist->servers[index];
93 struct afs_cb_interest *cbi, *vcbi, *new, *old;
David Howellsd2ddc772017-11-02 15:27:50 +000094 struct afs_server *server = entry->server;
David Howells08e0e7c2007-04-26 15:55:03 -070095
David Howellsc435ee32017-11-02 15:27:49 +000096again:
David Howellsf6424042019-05-13 16:14:32 +010097 vcbi = rcu_dereference_protected(vnode->cb_interest,
98 lockdep_is_held(&vnode->io_lock));
99 if (vcbi && likely(vcbi == entry->cb_interest))
David Howellsd4a96bec2018-05-10 08:43:04 +0100100 return 0;
101
102 read_lock(&slist->lock);
103 cbi = afs_get_cb_interest(entry->cb_interest);
104 read_unlock(&slist->lock);
105
David Howellsc435ee32017-11-02 15:27:49 +0000106 if (vcbi) {
David Howellsd4a96bec2018-05-10 08:43:04 +0100107 if (vcbi == cbi) {
David Howellsc435ee32017-11-02 15:27:49 +0000108 afs_put_cb_interest(afs_v2net(vnode), cbi);
109 return 0;
110 }
111
David Howellsd4a96bec2018-05-10 08:43:04 +0100112 /* Use a new interest in the server list for the same server
113 * rather than an old one that's still attached to a vnode.
114 */
115 if (cbi && vcbi->server == cbi->server) {
116 write_seqlock(&vnode->cb_lock);
David Howellsf6424042019-05-13 16:14:32 +0100117 old = rcu_dereference_protected(vnode->cb_interest,
118 lockdep_is_held(&vnode->cb_lock.lock));
119 rcu_assign_pointer(vnode->cb_interest, cbi);
David Howellsd4a96bec2018-05-10 08:43:04 +0100120 write_sequnlock(&vnode->cb_lock);
121 afs_put_cb_interest(afs_v2net(vnode), old);
122 return 0;
123 }
124
125 /* Re-use the one attached to the vnode. */
David Howellsc435ee32017-11-02 15:27:49 +0000126 if (!cbi && vcbi->server == server) {
David Howellsd4a96bec2018-05-10 08:43:04 +0100127 write_lock(&slist->lock);
128 if (entry->cb_interest) {
129 write_unlock(&slist->lock);
130 afs_put_cb_interest(afs_v2net(vnode), cbi);
David Howellsc435ee32017-11-02 15:27:49 +0000131 goto again;
132 }
David Howellsd4a96bec2018-05-10 08:43:04 +0100133
134 entry->cb_interest = cbi;
135 write_unlock(&slist->lock);
David Howellsc435ee32017-11-02 15:27:49 +0000136 return 0;
137 }
138 }
139
140 if (!cbi) {
David Howells47ea0f22018-06-15 15:24:50 +0100141 new = afs_create_interest(server, vnode);
David Howellsc435ee32017-11-02 15:27:49 +0000142 if (!new)
143 return -ENOMEM;
144
David Howellsd4a96bec2018-05-10 08:43:04 +0100145 write_lock(&slist->lock);
146 if (!entry->cb_interest) {
147 entry->cb_interest = afs_get_cb_interest(new);
David Howellsc435ee32017-11-02 15:27:49 +0000148 cbi = new;
David Howellsd4a96bec2018-05-10 08:43:04 +0100149 new = NULL;
David Howellsc435ee32017-11-02 15:27:49 +0000150 } else {
David Howellsd4a96bec2018-05-10 08:43:04 +0100151 cbi = afs_get_cb_interest(entry->cb_interest);
David Howellsc435ee32017-11-02 15:27:49 +0000152 }
David Howellsd4a96bec2018-05-10 08:43:04 +0100153 write_unlock(&slist->lock);
154 afs_put_cb_interest(afs_v2net(vnode), new);
David Howellsc435ee32017-11-02 15:27:49 +0000155 }
156
157 ASSERT(cbi);
158
159 /* Change the server the vnode is using. This entails scrubbing any
160 * interest the vnode had in the previous server it was using.
161 */
162 write_seqlock(&vnode->cb_lock);
163
David Howellsf6424042019-05-13 16:14:32 +0100164 old = rcu_dereference_protected(vnode->cb_interest,
165 lockdep_is_held(&vnode->cb_lock.lock));
166 rcu_assign_pointer(vnode->cb_interest, cbi);
David Howellsc435ee32017-11-02 15:27:49 +0000167 vnode->cb_s_break = cbi->server->cb_s_break;
David Howells68251f02018-05-12 22:31:33 +0100168 vnode->cb_v_break = vnode->volume->cb_v_break;
David Howellsc435ee32017-11-02 15:27:49 +0000169 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
170
171 write_sequnlock(&vnode->cb_lock);
David Howellsd4a96bec2018-05-10 08:43:04 +0100172 afs_put_cb_interest(afs_v2net(vnode), old);
David Howellsc435ee32017-11-02 15:27:49 +0000173 return 0;
174}
175
176/*
David Howellsc435ee32017-11-02 15:27:49 +0000177 * Remove an interest on a server.
178 */
179void afs_put_cb_interest(struct afs_net *net, struct afs_cb_interest *cbi)
180{
David Howells47ea0f22018-06-15 15:24:50 +0100181 struct afs_vol_interest *vi;
182
David Howellsc435ee32017-11-02 15:27:49 +0000183 if (cbi && refcount_dec_and_test(&cbi->usage)) {
David Howells47ea0f22018-06-15 15:24:50 +0100184 if (!hlist_unhashed(&cbi->cb_vlink)) {
David Howellsc435ee32017-11-02 15:27:49 +0000185 write_lock(&cbi->server->cb_break_lock);
David Howells47ea0f22018-06-15 15:24:50 +0100186
187 hlist_del_init(&cbi->cb_vlink);
188 vi = cbi->vol_interest;
189 cbi->vol_interest = NULL;
190 if (--vi->usage == 0)
191 hlist_del(&vi->srv_link);
192 else
193 vi = NULL;
194
David Howellsc435ee32017-11-02 15:27:49 +0000195 write_unlock(&cbi->server->cb_break_lock);
David Howellsf6424042019-05-13 16:14:32 +0100196 if (vi)
197 kfree_rcu(vi, rcu);
David Howells45218192019-06-20 18:12:17 +0100198 afs_put_server(net, cbi->server, afs_server_trace_put_cbi);
David Howellsc435ee32017-11-02 15:27:49 +0000199 }
David Howellsf6424042019-05-13 16:14:32 +0100200 kfree_rcu(cbi, rcu);
David Howellsc435ee32017-11-02 15:27:49 +0000201 }
202}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204/*
205 * allow the fileserver to request callback state (re-)initialisation
206 */
David Howells08e0e7c2007-04-26 15:55:03 -0700207void afs_init_callback_state(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
David Howellseeba1e92019-04-13 08:37:37 +0100209 server->cb_s_break++;
David Howells08e0e7c2007-04-26 15:55:03 -0700210}
211
212/*
213 * actually break a callback
214 */
David Howells051d2522019-06-20 18:12:16 +0100215void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
David Howells08e0e7c2007-04-26 15:55:03 -0700216{
217 _enter("");
218
David Howells5a813272018-04-06 14:17:26 +0100219 clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000220 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
221 vnode->cb_break++;
222 afs_clear_permits(vnode);
223
David Howellsc7226e42019-05-10 23:03:31 +0100224 if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
David Howellse8d6c552007-07-15 23:40:12 -0700225 afs_lock_may_be_available(vnode);
David Howells051d2522019-06-20 18:12:16 +0100226
227 trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
228 } else {
229 trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
David Howells08e0e7c2007-04-26 15:55:03 -0700230 }
David Howells30062bd2018-10-20 00:57:58 +0100231}
David Howellsc435ee32017-11-02 15:27:49 +0000232
David Howells051d2522019-06-20 18:12:16 +0100233void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
David Howells30062bd2018-10-20 00:57:58 +0100234{
235 write_seqlock(&vnode->cb_lock);
David Howells051d2522019-06-20 18:12:16 +0100236 __afs_break_callback(vnode, reason);
David Howellsc435ee32017-11-02 15:27:49 +0000237 write_sequnlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700238}
239
240/*
241 * allow the fileserver to explicitly break one callback
242 * - happens when
243 * - the backing file is changed
244 * - a lock is released
245 */
246static void afs_break_one_callback(struct afs_server *server,
247 struct afs_fid *fid)
248{
David Howells47ea0f22018-06-15 15:24:50 +0100249 struct afs_vol_interest *vi;
David Howellsc435ee32017-11-02 15:27:49 +0000250 struct afs_cb_interest *cbi;
251 struct afs_iget_data data;
David Howells08e0e7c2007-04-26 15:55:03 -0700252 struct afs_vnode *vnode;
David Howellsc435ee32017-11-02 15:27:49 +0000253 struct inode *inode;
David Howells08e0e7c2007-04-26 15:55:03 -0700254
David Howellsc435ee32017-11-02 15:27:49 +0000255 read_lock(&server->cb_break_lock);
David Howells47ea0f22018-06-15 15:24:50 +0100256 hlist_for_each_entry(vi, &server->cb_volumes, srv_link) {
257 if (vi->vid < fid->vid)
258 continue;
259 if (vi->vid > fid->vid) {
260 vi = NULL;
261 break;
262 }
263 //atomic_inc(&vi->usage);
264 break;
265 }
266
267 /* TODO: Find all matching volumes if we couldn't match the server and
268 * break them anyway.
269 */
270 if (!vi)
271 goto out;
David Howellsc435ee32017-11-02 15:27:49 +0000272
273 /* Step through all interested superblocks. There may be more than one
274 * because of cell aliasing.
275 */
David Howells47ea0f22018-06-15 15:24:50 +0100276 hlist_for_each_entry(cbi, &vi->cb_interests, cb_vlink) {
David Howells68251f02018-05-12 22:31:33 +0100277 if (fid->vnode == 0 && fid->unique == 0) {
278 /* The callback break applies to an entire volume. */
279 struct afs_super_info *as = AFS_FS_S(cbi->sb);
280 struct afs_volume *volume = as->volume;
281
David Howells90fa9b62019-06-20 16:49:35 +0100282 write_lock(&volume->cb_v_break_lock);
David Howells68251f02018-05-12 22:31:33 +0100283 volume->cb_v_break++;
David Howells051d2522019-06-20 18:12:16 +0100284 trace_afs_cb_break(fid, volume->cb_v_break,
285 afs_cb_break_for_volume_callback, false);
David Howells90fa9b62019-06-20 16:49:35 +0100286 write_unlock(&volume->cb_v_break_lock);
David Howells68251f02018-05-12 22:31:33 +0100287 } else {
288 data.volume = NULL;
289 data.fid = *fid;
290 inode = ilookup5_nowait(cbi->sb, fid->vnode,
291 afs_iget5_test, &data);
292 if (inode) {
293 vnode = AFS_FS_I(inode);
David Howells051d2522019-06-20 18:12:16 +0100294 afs_break_callback(vnode, afs_cb_break_for_callback);
David Howells68251f02018-05-12 22:31:33 +0100295 iput(inode);
David Howells051d2522019-06-20 18:12:16 +0100296 } else {
297 trace_afs_cb_miss(fid, afs_cb_break_for_callback);
David Howells68251f02018-05-12 22:31:33 +0100298 }
David Howellsc435ee32017-11-02 15:27:49 +0000299 }
David Howells08e0e7c2007-04-26 15:55:03 -0700300 }
301
David Howells47ea0f22018-06-15 15:24:50 +0100302out:
David Howellsc435ee32017-11-02 15:27:49 +0000303 read_unlock(&server->cb_break_lock);
David Howellsec268152007-04-26 15:49:28 -0700304}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306/*
307 * allow the fileserver to break callback promises
308 */
David Howells08e0e7c2007-04-26 15:55:03 -0700309void afs_break_callbacks(struct afs_server *server, size_t count,
David Howells5cf9dd52018-04-09 21:12:31 +0100310 struct afs_callback_break *callbacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
David Howells08e0e7c2007-04-26 15:55:03 -0700312 _enter("%p,%zu,", server, count);
313
314 ASSERT(server != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
David Howells68251f02018-05-12 22:31:33 +0100316 /* TODO: Sort the callback break list by volume ID */
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 for (; count > 0; callbacks++, count--) {
David Howells06aeb292018-10-20 00:57:58 +0100319 _debug("- Fid { vl=%08llx n=%llu u=%u }",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 callbacks->fid.vid,
321 callbacks->fid.vnode,
David Howells06aeb292018-10-20 00:57:58 +0100322 callbacks->fid.unique);
David Howells08e0e7c2007-04-26 15:55:03 -0700323 afs_break_one_callback(server, &callbacks->fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325
David Howells08e0e7c2007-04-26 15:55:03 -0700326 _leave("");
327 return;
David Howellsec268152007-04-26 15:49:28 -0700328}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*
David Howellsc435ee32017-11-02 15:27:49 +0000331 * Clear the callback interests in a server list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 */
David Howellsd2ddc772017-11-02 15:27:50 +0000333void afs_clear_callback_interests(struct afs_net *net, struct afs_server_list *slist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
David Howellsc435ee32017-11-02 15:27:49 +0000335 int i;
David Howells08e0e7c2007-04-26 15:55:03 -0700336
David Howellsd2ddc772017-11-02 15:27:50 +0000337 for (i = 0; i < slist->nr_servers; i++) {
338 afs_put_cb_interest(net, slist->servers[i].cb_interest);
339 slist->servers[i].cb_interest = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700340 }
David Howells08e0e7c2007-04-26 15:55:03 -0700341}