blob: 09332945d3222876ca7260999621e65f8dec76a9 [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/*
24 * Set up an interest-in-callbacks record for a volume on a server and
25 * register it with the server.
David Howellsd4a96be2018-05-10 08:43:04 +010026 * - Called with vnode->io_lock held.
David Howellsc435ee32017-11-02 15:27:49 +000027 */
28int afs_register_server_cb_interest(struct afs_vnode *vnode,
David Howellsd4a96be2018-05-10 08:43:04 +010029 struct afs_server_list *slist,
30 unsigned int index)
David Howellsc435ee32017-11-02 15:27:49 +000031{
David Howellsd4a96be2018-05-10 08:43:04 +010032 struct afs_server_entry *entry = &slist->servers[index];
33 struct afs_cb_interest *cbi, *vcbi, *new, *old;
David Howellsd2ddc772017-11-02 15:27:50 +000034 struct afs_server *server = entry->server;
David Howells08e0e7c2007-04-26 15:55:03 -070035
David Howellsc435ee32017-11-02 15:27:49 +000036again:
David Howellsd4a96be2018-05-10 08:43:04 +010037 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 Howellsc435ee32017-11-02 15:27:49 +000045 vcbi = vnode->cb_interest;
46 if (vcbi) {
David Howellsd4a96be2018-05-10 08:43:04 +010047 if (vcbi == cbi) {
David Howellsc435ee32017-11-02 15:27:49 +000048 afs_put_cb_interest(afs_v2net(vnode), cbi);
49 return 0;
50 }
51
David Howellsd4a96be2018-05-10 08:43:04 +010052 /* 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 Howellsc435ee32017-11-02 15:27:49 +000065 if (!cbi && vcbi->server == server) {
David Howellsd4a96be2018-05-10 08:43:04 +010066 write_lock(&slist->lock);
67 if (entry->cb_interest) {
68 write_unlock(&slist->lock);
69 afs_put_cb_interest(afs_v2net(vnode), cbi);
David Howellsc435ee32017-11-02 15:27:49 +000070 goto again;
71 }
David Howellsd4a96be2018-05-10 08:43:04 +010072
73 entry->cb_interest = cbi;
74 write_unlock(&slist->lock);
David Howellsc435ee32017-11-02 15:27:49 +000075 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 Howellsd4a96be2018-05-10 08:43:04 +010094 write_lock(&slist->lock);
95 if (!entry->cb_interest) {
96 entry->cb_interest = afs_get_cb_interest(new);
David Howellsc435ee32017-11-02 15:27:49 +000097 cbi = new;
David Howellsd4a96be2018-05-10 08:43:04 +010098 new = NULL;
David Howellsc435ee32017-11-02 15:27:49 +000099 } else {
David Howellsd4a96be2018-05-10 08:43:04 +0100100 cbi = afs_get_cb_interest(entry->cb_interest);
David Howellsc435ee32017-11-02 15:27:49 +0000101 }
David Howellsd4a96be2018-05-10 08:43:04 +0100102 write_unlock(&slist->lock);
103 afs_put_cb_interest(afs_v2net(vnode), new);
David Howellsc435ee32017-11-02 15:27:49 +0000104 }
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 Howellsd4a96be2018-05-10 08:43:04 +0100113 old = vnode->cb_interest;
114 vnode->cb_interest = cbi;
David Howellsc435ee32017-11-02 15:27:49 +0000115 vnode->cb_s_break = cbi->server->cb_s_break;
116 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
117
118 write_sequnlock(&vnode->cb_lock);
David Howellsd4a96be2018-05-10 08:43:04 +0100119 afs_put_cb_interest(afs_v2net(vnode), old);
David Howellsc435ee32017-11-02 15:27:49 +0000120 return 0;
121}
122
123/*
David Howellsc435ee32017-11-02 15:27:49 +0000124 * Remove an interest on a server.
125 */
126void afs_put_cb_interest(struct afs_net *net, struct afs_cb_interest *cbi)
127{
128 if (cbi && refcount_dec_and_test(&cbi->usage)) {
129 if (!list_empty(&cbi->cb_link)) {
130 write_lock(&cbi->server->cb_break_lock);
131 list_del_init(&cbi->cb_link);
132 write_unlock(&cbi->server->cb_break_lock);
133 afs_put_server(net, cbi->server);
134 }
135 kfree(cbi);
136 }
137}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139/*
140 * allow the fileserver to request callback state (re-)initialisation
141 */
David Howells08e0e7c2007-04-26 15:55:03 -0700142void afs_init_callback_state(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
David Howellsd2ddc772017-11-02 15:27:50 +0000144 if (!test_and_clear_bit(AFS_SERVER_FL_NEW, &server->flags))
David Howellsc435ee32017-11-02 15:27:49 +0000145 server->cb_s_break++;
David Howells08e0e7c2007-04-26 15:55:03 -0700146}
147
148/*
149 * actually break a callback
150 */
David Howellsc435ee32017-11-02 15:27:49 +0000151void afs_break_callback(struct afs_vnode *vnode)
David Howells08e0e7c2007-04-26 15:55:03 -0700152{
153 _enter("");
154
David Howellsc435ee32017-11-02 15:27:49 +0000155 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700156
David Howells5a813272018-04-06 14:17:26 +0100157 clear_bit(AFS_VNODE_NEW_CONTENT, &vnode->flags);
David Howellsc435ee32017-11-02 15:27:49 +0000158 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
159 vnode->cb_break++;
160 afs_clear_permits(vnode);
161
David Howells08e0e7c2007-04-26 15:55:03 -0700162 spin_lock(&vnode->lock);
163
164 _debug("break callback");
165
David Howellse8d6c552007-07-15 23:40:12 -0700166 if (list_empty(&vnode->granted_locks) &&
167 !list_empty(&vnode->pending_locks))
168 afs_lock_may_be_available(vnode);
David Howells08e0e7c2007-04-26 15:55:03 -0700169 spin_unlock(&vnode->lock);
170 }
David Howellsc435ee32017-11-02 15:27:49 +0000171
172 write_sequnlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700173}
174
175/*
176 * allow the fileserver to explicitly break one callback
177 * - happens when
178 * - the backing file is changed
179 * - a lock is released
180 */
181static void afs_break_one_callback(struct afs_server *server,
182 struct afs_fid *fid)
183{
David Howellsc435ee32017-11-02 15:27:49 +0000184 struct afs_cb_interest *cbi;
185 struct afs_iget_data data;
David Howells08e0e7c2007-04-26 15:55:03 -0700186 struct afs_vnode *vnode;
David Howellsc435ee32017-11-02 15:27:49 +0000187 struct inode *inode;
David Howells08e0e7c2007-04-26 15:55:03 -0700188
David Howellsc435ee32017-11-02 15:27:49 +0000189 read_lock(&server->cb_break_lock);
190
191 /* Step through all interested superblocks. There may be more than one
192 * because of cell aliasing.
193 */
194 list_for_each_entry(cbi, &server->cb_interests, cb_link) {
195 if (cbi->vid != fid->vid)
196 continue;
197
198 data.volume = NULL;
199 data.fid = *fid;
200 inode = ilookup5_nowait(cbi->sb, fid->vnode, afs_iget5_test, &data);
201 if (inode) {
202 vnode = AFS_FS_I(inode);
203 afs_break_callback(vnode);
204 iput(inode);
205 }
David Howells08e0e7c2007-04-26 15:55:03 -0700206 }
207
David Howellsc435ee32017-11-02 15:27:49 +0000208 read_unlock(&server->cb_break_lock);
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);
220 ASSERTCMP(count, <=, AFSCBMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 for (; count > 0; callbacks++, count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
224 callbacks->fid.vid,
225 callbacks->fid.vnode,
226 callbacks->fid.unique,
David Howells5cf9dd52018-04-09 21:12:31 +0100227 callbacks->cb.version,
228 callbacks->cb.expiry,
229 callbacks->cb.type
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 );
David Howells08e0e7c2007-04-26 15:55:03 -0700231 afs_break_one_callback(server, &callbacks->fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
David Howells08e0e7c2007-04-26 15:55:03 -0700234 _leave("");
235 return;
David Howellsec268152007-04-26 15:49:28 -0700236}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238/*
David Howellsc435ee32017-11-02 15:27:49 +0000239 * Clear the callback interests in a server list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 */
David Howellsd2ddc772017-11-02 15:27:50 +0000241void afs_clear_callback_interests(struct afs_net *net, struct afs_server_list *slist)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242{
David Howellsc435ee32017-11-02 15:27:49 +0000243 int i;
David Howells08e0e7c2007-04-26 15:55:03 -0700244
David Howellsd2ddc772017-11-02 15:27:50 +0000245 for (i = 0; i < slist->nr_servers; i++) {
246 afs_put_cb_interest(net, slist->servers[i].cb_interest);
247 slist->servers[i].cb_interest = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700248 }
David Howells08e0e7c2007-04-26 15:55:03 -0700249}