xref: /kernel/linux/linux-5.10/fs/afs/callback.c (revision 8c2ecf20)
1/*
2 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
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 *
11 * Authors: David Woodhouse <dwmw2@infradead.org>
12 *          David Howells <dhowells@redhat.com>
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/circ_buf.h>
20#include <linux/sched.h>
21#include "internal.h"
22
23/*
24 * Allow the fileserver to request callback state (re-)initialisation.
25 * Unfortunately, UUIDs are not guaranteed unique.
26 */
27void afs_init_callback_state(struct afs_server *server)
28{
29	rcu_read_lock();
30	do {
31		server->cb_s_break++;
32		server = rcu_dereference(server->uuid_next);
33	} while (0);
34	rcu_read_unlock();
35}
36
37/*
38 * actually break a callback
39 */
40void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
41{
42	_enter("");
43
44	clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
45	if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
46		vnode->cb_break++;
47		afs_clear_permits(vnode);
48
49		if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
50			afs_lock_may_be_available(vnode);
51
52		trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
53	} else {
54		trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
55	}
56}
57
58void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
59{
60	write_seqlock(&vnode->cb_lock);
61	__afs_break_callback(vnode, reason);
62	write_sequnlock(&vnode->cb_lock);
63}
64
65/*
66 * Look up a volume by volume ID under RCU conditions.
67 */
68static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
69						afs_volid_t vid)
70{
71	struct afs_volume *volume = NULL;
72	struct rb_node *p;
73	int seq = 1;
74
75	do {
76		/* Unfortunately, rbtree walking doesn't give reliable results
77		 * under just the RCU read lock, so we have to check for
78		 * changes.
79		 */
80		seq++; /* 2 on the 1st/lockless path, otherwise odd */
81		read_seqbegin_or_lock(&cell->volume_lock, &seq);
82
83		p = rcu_dereference_raw(cell->volumes.rb_node);
84		while (p) {
85			volume = rb_entry(p, struct afs_volume, cell_node);
86
87			if (volume->vid < vid)
88				p = rcu_dereference_raw(p->rb_left);
89			else if (volume->vid > vid)
90				p = rcu_dereference_raw(p->rb_right);
91			else
92				break;
93			volume = NULL;
94		}
95
96	} while (need_seqretry(&cell->volume_lock, seq));
97
98	done_seqretry(&cell->volume_lock, seq);
99	return volume;
100}
101
102/*
103 * allow the fileserver to explicitly break one callback
104 * - happens when
105 *   - the backing file is changed
106 *   - a lock is released
107 */
108static void afs_break_one_callback(struct afs_volume *volume,
109				   struct afs_fid *fid)
110{
111	struct super_block *sb;
112	struct afs_vnode *vnode;
113	struct inode *inode;
114
115	if (fid->vnode == 0 && fid->unique == 0) {
116		/* The callback break applies to an entire volume. */
117		write_lock(&volume->cb_v_break_lock);
118		volume->cb_v_break++;
119		trace_afs_cb_break(fid, volume->cb_v_break,
120				   afs_cb_break_for_volume_callback, false);
121		write_unlock(&volume->cb_v_break_lock);
122		return;
123	}
124
125	/* See if we can find a matching inode - even an I_NEW inode needs to
126	 * be marked as it can have its callback broken before we finish
127	 * setting up the local inode.
128	 */
129	sb = rcu_dereference(volume->sb);
130	if (!sb)
131		return;
132
133	inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
134	if (inode) {
135		vnode = AFS_FS_I(inode);
136		afs_break_callback(vnode, afs_cb_break_for_callback);
137	} else {
138		trace_afs_cb_miss(fid, afs_cb_break_for_callback);
139	}
140}
141
142static void afs_break_some_callbacks(struct afs_server *server,
143				     struct afs_callback_break *cbb,
144				     size_t *_count)
145{
146	struct afs_callback_break *residue = cbb;
147	struct afs_volume *volume;
148	afs_volid_t vid = cbb->fid.vid;
149	size_t i;
150
151	volume = afs_lookup_volume_rcu(server->cell, vid);
152
153	/* TODO: Find all matching volumes if we couldn't match the server and
154	 * break them anyway.
155	 */
156
157	for (i = *_count; i > 0; cbb++, i--) {
158		if (cbb->fid.vid == vid) {
159			_debug("- Fid { vl=%08llx n=%llu u=%u }",
160			       cbb->fid.vid,
161			       cbb->fid.vnode,
162			       cbb->fid.unique);
163			--*_count;
164			if (volume)
165				afs_break_one_callback(volume, &cbb->fid);
166		} else {
167			*residue++ = *cbb;
168		}
169	}
170}
171
172/*
173 * allow the fileserver to break callback promises
174 */
175void afs_break_callbacks(struct afs_server *server, size_t count,
176			 struct afs_callback_break *callbacks)
177{
178	_enter("%p,%zu,", server, count);
179
180	ASSERT(server != NULL);
181
182	rcu_read_lock();
183
184	while (count > 0)
185		afs_break_some_callbacks(server, callbacks, &count);
186
187	rcu_read_unlock();
188	return;
189}
190