blob: 1b4d5809808d0d232d6a66d1c4a2b74d8bd980f7 [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 Howells6e0e99d2021-09-02 16:43:10 +010024 * 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 */
29void 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->vfs_inode.i_mapping, 0, 0, false);
34}
35
36void 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/*
David Howells3c4c4072020-05-27 15:51:30 +010055 * Allow the fileserver to request callback state (re-)initialisation.
56 * Unfortunately, UUIDs are not guaranteed unique.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
David Howells08e0e7c2007-04-26 15:55:03 -070058void afs_init_callback_state(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
David Howells3c4c4072020-05-27 15:51:30 +010060 rcu_read_lock();
61 do {
62 server->cb_s_break++;
David Howells4fe6a942021-09-02 21:51:01 +010063 atomic_inc(&server->cell->fs_s_break);
David Howells6e0e99d2021-09-02 16:43:10 +010064 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)));
David Howells3c4c4072020-05-27 15:51:30 +010068 rcu_read_unlock();
David Howells08e0e7c2007-04-26 15:55:03 -070069}
70
71/*
72 * actually break a callback
73 */
David Howells051d2522019-06-20 18:12:16 +010074void __afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
David Howells08e0e7c2007-04-26 15:55:03 -070075{
76 _enter("");
77
David Howells5a813272018-04-06 14:17:26 +010078 clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +000079 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
80 vnode->cb_break++;
David Howells4fe6a942021-09-02 21:51:01 +010081 vnode->cb_v_break = vnode->volume->cb_v_break;
David Howellsc435ee32017-11-02 15:27:49 +000082 afs_clear_permits(vnode);
83
David Howellsc7226e42019-05-10 23:03:31 +010084 if (vnode->lock_state == AFS_VNODE_LOCK_WAITING_FOR_CB)
David Howellse8d6c552007-07-15 23:40:12 -070085 afs_lock_may_be_available(vnode);
David Howells051d2522019-06-20 18:12:16 +010086
David Howells6e0e99d2021-09-02 16:43:10 +010087 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
David Howells051d2522019-06-20 18:12:16 +010092 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);
David Howells08e0e7c2007-04-26 15:55:03 -070095 }
David Howells30062bd2018-10-20 00:57:58 +010096}
David Howellsc435ee32017-11-02 15:27:49 +000097
David Howells051d2522019-06-20 18:12:16 +010098void afs_break_callback(struct afs_vnode *vnode, enum afs_cb_break_reason reason)
David Howells30062bd2018-10-20 00:57:58 +010099{
100 write_seqlock(&vnode->cb_lock);
David Howells051d2522019-06-20 18:12:16 +0100101 __afs_break_callback(vnode, reason);
David Howellsc435ee32017-11-02 15:27:49 +0000102 write_sequnlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700103}
104
105/*
David Howells20325962020-04-30 01:03:49 +0100106 * Look up a volume by volume ID under RCU conditions.
David Howells8230fd82020-03-27 15:02:44 +0000107 */
David Howells20325962020-04-30 01:03:49 +0100108static struct afs_volume *afs_lookup_volume_rcu(struct afs_cell *cell,
109 afs_volid_t vid)
David Howells8230fd82020-03-27 15:02:44 +0000110{
David Howells20325962020-04-30 01:03:49 +0100111 struct afs_volume *volume = NULL;
David Howells8230fd82020-03-27 15:02:44 +0000112 struct rb_node *p;
113 int seq = 0;
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 */
David Howells20325962020-04-30 01:03:49 +0100120 read_seqbegin_or_lock(&cell->volume_lock, &seq);
David Howells8230fd82020-03-27 15:02:44 +0000121
David Howells20325962020-04-30 01:03:49 +0100122 p = rcu_dereference_raw(cell->volumes.rb_node);
David Howells8230fd82020-03-27 15:02:44 +0000123 while (p) {
David Howells20325962020-04-30 01:03:49 +0100124 volume = rb_entry(p, struct afs_volume, cell_node);
David Howells8230fd82020-03-27 15:02:44 +0000125
David Howells20325962020-04-30 01:03:49 +0100126 if (volume->vid < vid)
David Howells8230fd82020-03-27 15:02:44 +0000127 p = rcu_dereference_raw(p->rb_left);
David Howells20325962020-04-30 01:03:49 +0100128 else if (volume->vid > vid)
David Howells8230fd82020-03-27 15:02:44 +0000129 p = rcu_dereference_raw(p->rb_right);
130 else
131 break;
David Howells20325962020-04-30 01:03:49 +0100132 volume = NULL;
David Howells8230fd82020-03-27 15:02:44 +0000133 }
134
David Howells20325962020-04-30 01:03:49 +0100135 } while (need_seqretry(&cell->volume_lock, seq));
David Howells8230fd82020-03-27 15:02:44 +0000136
David Howells20325962020-04-30 01:03:49 +0100137 done_seqretry(&cell->volume_lock, seq);
138 return volume;
David Howells8230fd82020-03-27 15:02:44 +0000139}
140
141/*
David Howells08e0e7c2007-04-26 15:55:03 -0700142 * allow the fileserver to explicitly break one callback
143 * - happens when
144 * - the backing file is changed
145 * - a lock is released
146 */
David Howells20325962020-04-30 01:03:49 +0100147static void afs_break_one_callback(struct afs_volume *volume,
148 struct afs_fid *fid)
David Howells08e0e7c2007-04-26 15:55:03 -0700149{
David Howells20325962020-04-30 01:03:49 +0100150 struct super_block *sb;
David Howells08e0e7c2007-04-26 15:55:03 -0700151 struct afs_vnode *vnode;
David Howellsc435ee32017-11-02 15:27:49 +0000152 struct inode *inode;
David Howells08e0e7c2007-04-26 15:55:03 -0700153
David Howells20325962020-04-30 01:03:49 +0100154 if (fid->vnode == 0 && fid->unique == 0) {
155 /* The callback break applies to an entire volume. */
156 write_lock(&volume->cb_v_break_lock);
157 volume->cb_v_break++;
158 trace_afs_cb_break(fid, volume->cb_v_break,
159 afs_cb_break_for_volume_callback, false);
160 write_unlock(&volume->cb_v_break_lock);
161 return;
162 }
David Howells68251f02018-05-12 22:31:33 +0100163
David Howells20325962020-04-30 01:03:49 +0100164 /* See if we can find a matching inode - even an I_NEW inode needs to
165 * be marked as it can have its callback broken before we finish
166 * setting up the local inode.
167 */
168 sb = rcu_dereference(volume->sb);
169 if (!sb)
170 return;
171
172 inode = find_inode_rcu(sb, fid->vnode, afs_ilookup5_test_by_fid, fid);
173 if (inode) {
174 vnode = AFS_FS_I(inode);
175 afs_break_callback(vnode, afs_cb_break_for_callback);
176 } else {
177 trace_afs_cb_miss(fid, afs_cb_break_for_callback);
David Howells08e0e7c2007-04-26 15:55:03 -0700178 }
David Howells8230fd82020-03-27 15:02:44 +0000179}
David Howells08e0e7c2007-04-26 15:55:03 -0700180
David Howells8230fd82020-03-27 15:02:44 +0000181static void afs_break_some_callbacks(struct afs_server *server,
182 struct afs_callback_break *cbb,
183 size_t *_count)
184{
185 struct afs_callback_break *residue = cbb;
David Howells20325962020-04-30 01:03:49 +0100186 struct afs_volume *volume;
David Howells8230fd82020-03-27 15:02:44 +0000187 afs_volid_t vid = cbb->fid.vid;
188 size_t i;
189
David Howells20325962020-04-30 01:03:49 +0100190 volume = afs_lookup_volume_rcu(server->cell, vid);
David Howells8230fd82020-03-27 15:02:44 +0000191
192 /* TODO: Find all matching volumes if we couldn't match the server and
193 * break them anyway.
194 */
195
196 for (i = *_count; i > 0; cbb++, i--) {
197 if (cbb->fid.vid == vid) {
198 _debug("- Fid { vl=%08llx n=%llu u=%u }",
199 cbb->fid.vid,
200 cbb->fid.vnode,
201 cbb->fid.unique);
202 --*_count;
David Howells20325962020-04-30 01:03:49 +0100203 if (volume)
204 afs_break_one_callback(volume, &cbb->fid);
David Howells8230fd82020-03-27 15:02:44 +0000205 } else {
206 *residue++ = *cbb;
207 }
208 }
David Howellsec268152007-04-26 15:49:28 -0700209}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * allow the fileserver to break callback promises
213 */
David Howells08e0e7c2007-04-26 15:55:03 -0700214void afs_break_callbacks(struct afs_server *server, size_t count,
David Howells5cf9dd52018-04-09 21:12:31 +0100215 struct afs_callback_break *callbacks)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
David Howells08e0e7c2007-04-26 15:55:03 -0700217 _enter("%p,%zu,", server, count);
218
219 ASSERT(server != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
David Howells8230fd82020-03-27 15:02:44 +0000221 rcu_read_lock();
David Howells68251f02018-05-12 22:31:33 +0100222
David Howells8230fd82020-03-27 15:02:44 +0000223 while (count > 0)
224 afs_break_some_callbacks(server, callbacks, &count);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
David Howells8230fd82020-03-27 15:02:44 +0000226 rcu_read_unlock();
David Howells08e0e7c2007-04-26 15:55:03 -0700227 return;
David Howellsec268152007-04-26 15:49:28 -0700228}