xref: /openbmc/linux/fs/afs/callback.c (revision 83146efc)
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  * Handle invalidation of an mmap'd file.  We invalidate all the PTEs referring
25  * to the pages in this file's pagecache, forcing the kernel to go through
26  * ->fault() or ->page_mkwrite() - at which point we can handle invalidation
27  * more fully.
28  */
29 void afs_invalidate_mmap_work(struct work_struct *work)
30 {
31 	struct afs_vnode *vnode = container_of(work, struct afs_vnode, cb_work);
32 
33 	unmap_mapping_pages(vnode->netfs.inode.i_mapping, 0, 0, false);
34 }
35 
36 void afs_server_init_callback_work(struct work_struct *work)
37 {
38 	struct afs_server *server = container_of(work, struct afs_server, initcb_work);
39 	struct afs_vnode *vnode;
40 	struct afs_cell *cell = server->cell;
41 
42 	down_read(&cell->fs_open_mmaps_lock);
43 
44 	list_for_each_entry(vnode, &cell->fs_open_mmaps, cb_mmap_link) {
45 		if (vnode->cb_server == server) {
46 			clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
47 			queue_work(system_unbound_wq, &vnode->cb_work);
48 		}
49 	}
50 
51 	up_read(&cell->fs_open_mmaps_lock);
52 }
53 
54 /*
55  * Allow the fileserver to request callback state (re-)initialisation.
56  * Unfortunately, UUIDs are not guaranteed unique.
57  */
58 void afs_init_callback_state(struct afs_server *server)
59 {
60 	rcu_read_lock();
61 	do {
62 		server->cb_s_break++;
63 		atomic_inc(&server->cell->fs_s_break);
64 		if (!list_empty(&server->cell->fs_open_mmaps))
65 			queue_work(system_unbound_wq, &server->initcb_work);
66 
67 	} while ((server = rcu_dereference(server->uuid_next)));
68 	rcu_read_unlock();
69 }
70 
71 /*
72  * actually break a callback
73  */
74 void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
75 {
76 	_enter("");
77 
78 	clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
79 	if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
80 		vnode->cb_break++;
81 		vnode->cb_v_break = vnode->volume->cb_v_break;
82 		afs_clear_permits(vnode);
83 
84 		if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
85 			afs_lock_may_be_available(vnode);
86 
87 		if (reason != afs_cb_break_for_deleted &&
88 		    vnode->status.type == AFS_FTYPE_FILE &&
89 		    atomic_read(&vnode->cb_nr_mmap))
90 			queue_work(system_unbound_wq, &vnode->cb_work);
91 
92 		trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, true);
93 	} else {
94 		trace_afs_cb_break(&vnode->fid, vnode->cb_break, reason, false);
95 	}
96 }
97 
98 void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
99 {
100 	write_seqlock(&vnode->cb_lock);
101 	__afs_break_callback(vnode, reason);
102 	write_sequnlock(&vnode->cb_lock);
103 }
104 
105 /*
106  * Look up a volume by volume ID under RCU conditions.
107  */
108 static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
109 						afs_volid_t vid)
110 {
111 	struct afs_volume *volume = NULL;
112 	struct rb_node *p;
113 	int seq = 1;
114 
115 	do {
116 		/* Unfortunately, rbtree walking doesn't give reliable results
117 		 * under just the RCU read lock, so we have to check for
118 		 * changes.
119 		 */
120 		seq++; /* 2 on the 1st/lockless path, otherwise odd */
121 		read_seqbegin_or_lock(&cell->volume_lock, &seq);
122 
123 		p = rcu_dereference_raw(cell->volumes.rb_node);
124 		while (p) {
125 			volume = rb_entry(p, struct afs_volume, cell_node);
126 
127 			if (volume->vid < vid)
128 				p = rcu_dereference_raw(p->rb_left);
129 			else if (volume->vid > vid)
130 				p = rcu_dereference_raw(p->rb_right);
131 			else
132 				break;
133 			volume = NULL;
134 		}
135 
136 	} while (need_seqretry(&cell->volume_lock, seq));
137 
138 	done_seqretry(&cell->volume_lock, seq);
139 	return volume;
140 }
141 
142 /*
143  * allow the fileserver to explicitly break one callback
144  * - happens when
145  *   - the backing file is changed
146  *   - a lock is released
147  */
148 static void afs_break_one_callback(struct afs_volume *volume,
149 				   struct afs_fid *fid)
150 {
151 	struct super_block *sb;
152 	struct afs_vnode *vnode;
153 	struct inode *inode;
154 
155 	if (fid->vnode == 0 && fid->unique == 0) {
156 		/* The callback break applies to an entire volume. */
157 		write_lock(&volume->cb_v_break_lock);
158 		volume->cb_v_break++;
159 		trace_afs_cb_break(fid, volume->cb_v_break,
160 				   afs_cb_break_for_volume_callback, false);
161 		write_unlock(&volume->cb_v_break_lock);
162 		return;
163 	}
164 
165 	/* See if we can find a matching inode - even an I_NEW inode needs to
166 	 * be marked as it can have its callback broken before we finish
167 	 * setting up the local inode.
168 	 */
169 	sb = rcu_dereference(volume->sb);
170 	if (!sb)
171 		return;
172 
173 	inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
174 	if (inode) {
175 		vnode = AFS_FS_I(inode);
176 		afs_break_callback(vnode, afs_cb_break_for_callback);
177 	} else {
178 		trace_afs_cb_miss(fid, afs_cb_break_for_callback);
179 	}
180 }
181 
182 static void afs_break_some_callbacks(struct afs_server *server,
183 				     struct afs_callback_break *cbb,
184 				     size_t *_count)
185 {
186 	struct afs_callback_break *residue = cbb;
187 	struct afs_volume *volume;
188 	afs_volid_t vid = cbb->fid.vid;
189 	size_t i;
190 
191 	volume = afs_lookup_volume_rcu(server->cell, vid);
192 
193 	/* TODO: Find all matching volumes if we couldn't match the server and
194 	 * break them anyway.
195 	 */
196 
197 	for (i = *_count; i > 0; cbb++, i--) {
198 		if (cbb->fid.vid == vid) {
199 			_debug("- Fid { vl=%08llx n=%llu u=%u }",
200 			       cbb->fid.vid,
201 			       cbb->fid.vnode,
202 			       cbb->fid.unique);
203 			--*_count;
204 			if (volume)
205 				afs_break_one_callback(volume, &cbb->fid);
206 		} else {
207 			*residue++ = *cbb;
208 		}
209 	}
210 }
211 
212 /*
213  * allow the fileserver to break callback promises
214  */
215 void afs_break_callbacks(struct afs_server *server, size_t count,
216 			 struct afs_callback_break *callbacks)
217 {
218 	_enter("%p,%zu,", server, count);
219 
220 	ASSERT(server != NULL);
221 
222 	rcu_read_lock();
223 
224 	while (count > 0)
225 		afs_break_some_callbacks(server, callbacks, &count);
226 
227 	rcu_read_unlock();
228 	return;
229 }
230