blob: 82f4c7a3b7b69706b6af77b724703244fe4680d1 [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.
26 * - Called with volume->server_sem held.
27 */
28int afs_register_server_cb_interest(struct afs_vnode *vnode,
29 struct afs_cb_interest **ppcbi,
30 struct afs_server *server)
31{
32 struct afs_cb_interest *cbi = *ppcbi, *vcbi, *new, *x;
David Howells08e0e7c2007-04-26 15:55:03 -070033
David Howellsc435ee32017-11-02 15:27:49 +000034again:
35 vcbi = vnode->cb_interest;
36 if (vcbi) {
37 if (vcbi == cbi)
38 return 0;
David Howells08e0e7c2007-04-26 15:55:03 -070039
David Howellsc435ee32017-11-02 15:27:49 +000040 if (cbi && vcbi->server == cbi->server) {
41 write_seqlock(&vnode->cb_lock);
42 vnode->cb_interest = afs_get_cb_interest(cbi);
43 write_sequnlock(&vnode->cb_lock);
44 afs_put_cb_interest(afs_v2net(vnode), cbi);
45 return 0;
46 }
47
48 if (!cbi && vcbi->server == server) {
49 afs_get_cb_interest(vcbi);
50 x = cmpxchg(ppcbi, cbi, vcbi);
51 if (x != cbi) {
52 cbi = x;
53 afs_put_cb_interest(afs_v2net(vnode), vcbi);
54 goto again;
55 }
56 return 0;
57 }
58 }
59
60 if (!cbi) {
61 new = kzalloc(sizeof(struct afs_cb_interest), GFP_KERNEL);
62 if (!new)
63 return -ENOMEM;
64
65 refcount_set(&new->usage, 1);
66 new->sb = vnode->vfs_inode.i_sb;
67 new->vid = vnode->volume->vid;
68 new->server = afs_get_server(server);
69 INIT_LIST_HEAD(&new->cb_link);
70
71 write_lock(&server->cb_break_lock);
72 list_add_tail(&new->cb_link, &server->cb_interests);
73 write_unlock(&server->cb_break_lock);
74
75 x = cmpxchg(ppcbi, cbi, new);
76 if (x == cbi) {
77 cbi = new;
78 } else {
79 cbi = x;
80 afs_put_cb_interest(afs_v2net(vnode), new);
81 }
82 }
83
84 ASSERT(cbi);
85
86 /* Change the server the vnode is using. This entails scrubbing any
87 * interest the vnode had in the previous server it was using.
88 */
89 write_seqlock(&vnode->cb_lock);
90
91 vnode->cb_interest = afs_get_cb_interest(cbi);
92 vnode->cb_s_break = cbi->server->cb_s_break;
93 clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
94
95 write_sequnlock(&vnode->cb_lock);
96 return 0;
97}
98
99/*
100 * Set a vnode's interest on a server.
101 */
102void afs_set_cb_interest(struct afs_vnode *vnode, struct afs_cb_interest *cbi)
103{
104 struct afs_cb_interest *old_cbi = NULL;
105
106 if (vnode->cb_interest == cbi)
107 return;
108
109 write_seqlock(&vnode->cb_lock);
110 if (vnode->cb_interest != cbi) {
111 afs_get_cb_interest(cbi);
112 old_cbi = vnode->cb_interest;
113 vnode->cb_interest = cbi;
114 }
115 write_sequnlock(&vnode->cb_lock);
116 afs_put_cb_interest(afs_v2net(vnode), cbi);
117}
118
119/*
120 * Remove an interest on a server.
121 */
122void afs_put_cb_interest(struct afs_net *net, struct afs_cb_interest *cbi)
123{
124 if (cbi && refcount_dec_and_test(&cbi->usage)) {
125 if (!list_empty(&cbi->cb_link)) {
126 write_lock(&cbi->server->cb_break_lock);
127 list_del_init(&cbi->cb_link);
128 write_unlock(&cbi->server->cb_break_lock);
129 afs_put_server(net, cbi->server);
130 }
131 kfree(cbi);
132 }
133}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135/*
136 * allow the fileserver to request callback state (re-)initialisation
137 */
David Howells08e0e7c2007-04-26 15:55:03 -0700138void afs_init_callback_state(struct afs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
David Howellsc435ee32017-11-02 15:27:49 +0000140 if (!test_and_clear_bit(AFS_SERVER_NEW, &server->flags))
141 server->cb_s_break++;
David Howells08e0e7c2007-04-26 15:55:03 -0700142}
143
144/*
145 * actually break a callback
146 */
David Howellsc435ee32017-11-02 15:27:49 +0000147void afs_break_callback(struct afs_vnode *vnode)
David Howells08e0e7c2007-04-26 15:55:03 -0700148{
149 _enter("");
150
David Howellsc435ee32017-11-02 15:27:49 +0000151 write_seqlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700152
David Howellsc435ee32017-11-02 15:27:49 +0000153 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
154 vnode->cb_break++;
155 afs_clear_permits(vnode);
156
David Howells08e0e7c2007-04-26 15:55:03 -0700157 spin_lock(&vnode->lock);
158
159 _debug("break callback");
160
David Howellse8d6c552007-07-15 23:40:12 -0700161 if (list_empty(&vnode->granted_locks) &&
162 !list_empty(&vnode->pending_locks))
163 afs_lock_may_be_available(vnode);
David Howells08e0e7c2007-04-26 15:55:03 -0700164 spin_unlock(&vnode->lock);
165 }
David Howellsc435ee32017-11-02 15:27:49 +0000166
167 write_sequnlock(&vnode->cb_lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700168}
169
170/*
171 * allow the fileserver to explicitly break one callback
172 * - happens when
173 * - the backing file is changed
174 * - a lock is released
175 */
176static void afs_break_one_callback(struct afs_server *server,
177 struct afs_fid *fid)
178{
David Howellsc435ee32017-11-02 15:27:49 +0000179 struct afs_cb_interest *cbi;
180 struct afs_iget_data data;
David Howells08e0e7c2007-04-26 15:55:03 -0700181 struct afs_vnode *vnode;
David Howellsc435ee32017-11-02 15:27:49 +0000182 struct inode *inode;
David Howells08e0e7c2007-04-26 15:55:03 -0700183
David Howellsc435ee32017-11-02 15:27:49 +0000184 read_lock(&server->cb_break_lock);
185
186 /* Step through all interested superblocks. There may be more than one
187 * because of cell aliasing.
188 */
189 list_for_each_entry(cbi, &server->cb_interests, cb_link) {
190 if (cbi->vid != fid->vid)
191 continue;
192
193 data.volume = NULL;
194 data.fid = *fid;
195 inode = ilookup5_nowait(cbi->sb, fid->vnode, afs_iget5_test, &data);
196 if (inode) {
197 vnode = AFS_FS_I(inode);
198 afs_break_callback(vnode);
199 iput(inode);
200 }
David Howells08e0e7c2007-04-26 15:55:03 -0700201 }
202
David Howellsc435ee32017-11-02 15:27:49 +0000203 read_unlock(&server->cb_break_lock);
David Howellsec268152007-04-26 15:49:28 -0700204}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206/*
207 * allow the fileserver to break callback promises
208 */
David Howells08e0e7c2007-04-26 15:55:03 -0700209void afs_break_callbacks(struct afs_server *server, size_t count,
210 struct afs_callback callbacks[])
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
David Howells08e0e7c2007-04-26 15:55:03 -0700212 _enter("%p,%zu,", server, count);
213
214 ASSERT(server != NULL);
215 ASSERTCMP(count, <=, AFSCBMAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 for (; count > 0; callbacks++, count--) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 _debug("- Fid { vl=%08x n=%u u=%u } CB { v=%u x=%u t=%u }",
219 callbacks->fid.vid,
220 callbacks->fid.vnode,
221 callbacks->fid.unique,
222 callbacks->version,
223 callbacks->expiry,
224 callbacks->type
225 );
David Howells08e0e7c2007-04-26 15:55:03 -0700226 afs_break_one_callback(server, &callbacks->fid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 }
228
David Howells08e0e7c2007-04-26 15:55:03 -0700229 _leave("");
230 return;
David Howellsec268152007-04-26 15:49:28 -0700231}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/*
David Howellsc435ee32017-11-02 15:27:49 +0000234 * Clear the callback interests in a server list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 */
David Howellsc435ee32017-11-02 15:27:49 +0000236void afs_clear_callback_interests(struct afs_net *net, struct afs_volume *volume)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
David Howellsc435ee32017-11-02 15:27:49 +0000238 int i;
David Howells08e0e7c2007-04-26 15:55:03 -0700239
David Howellsc435ee32017-11-02 15:27:49 +0000240 for (i = 0; i < ARRAY_SIZE(volume->cb_interests); i++) {
241 afs_put_cb_interest(net, volume->cb_interests[i]);
242 volume->cb_interests[i] = NULL;
David Howells08e0e7c2007-04-26 15:55:03 -0700243 }
David Howells08e0e7c2007-04-26 15:55:03 -0700244}