2 * Copyright (c) 2002, 2007 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
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.
11 * Authors: David Woodhouse <dwmw2@infradead.org>
12 * David Howells <dhowells@redhat.com>
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>
24 * Create volume and callback interests on a server.
26 static struct afs_cb_interest
*afs_create_interest(struct afs_server
*server
,
27 struct afs_vnode
*vnode
)
29 struct afs_vol_interest
*new_vi
, *vi
;
30 struct afs_cb_interest
*new;
31 struct hlist_node
**pp
;
33 new_vi
= kzalloc(sizeof(struct afs_vol_interest
), GFP_KERNEL
);
37 new = kzalloc(sizeof(struct afs_cb_interest
), GFP_KERNEL
);
44 new_vi
->vid
= vnode
->volume
->vid
;
45 INIT_HLIST_NODE(&new_vi
->srv_link
);
46 INIT_HLIST_HEAD(&new_vi
->cb_interests
);
48 refcount_set(&new->usage
, 1);
49 new->sb
= vnode
->vfs_inode
.i_sb
;
50 new->vid
= vnode
->volume
->vid
;
51 new->server
= afs_get_server(server
, afs_server_trace_get_new_cbi
);
52 INIT_HLIST_NODE(&new->cb_vlink
);
54 write_lock(&server
->cb_break_lock
);
56 for (pp
= &server
->cb_volumes
.first
; *pp
; pp
= &(*pp
)->next
) {
57 vi
= hlist_entry(*pp
, struct afs_vol_interest
, srv_link
);
58 if (vi
->vid
< new_vi
->vid
)
60 if (vi
->vid
> new_vi
->vid
)
66 new_vi
->srv_link
.pprev
= pp
;
67 new_vi
->srv_link
.next
= *pp
;
69 (*pp
)->pprev
= &new_vi
->srv_link
.next
;
70 *pp
= &new_vi
->srv_link
;
75 new->vol_interest
= vi
;
76 hlist_add_head(&new->cb_vlink
, &vi
->cb_interests
);
78 write_unlock(&server
->cb_break_lock
);
84 * Set up an interest-in-callbacks record for a volume on a server and
85 * register it with the server.
86 * - Called with vnode->io_lock held.
88 int afs_register_server_cb_interest(struct afs_vnode
*vnode
,
89 struct afs_server_list
*slist
,
92 struct afs_server_entry
*entry
= &slist
->servers
[index
];
93 struct afs_cb_interest
*cbi
, *vcbi
, *new, *old
;
94 struct afs_server
*server
= entry
->server
;
97 vcbi
= rcu_dereference_protected(vnode
->cb_interest
,
98 lockdep_is_held(&vnode
->io_lock
));
99 if (vcbi
&& likely(vcbi
== entry
->cb_interest
))
102 read_lock(&slist
->lock
);
103 cbi
= afs_get_cb_interest(entry
->cb_interest
);
104 read_unlock(&slist
->lock
);
108 afs_put_cb_interest(afs_v2net(vnode
), cbi
);
112 /* Use a new interest in the server list for the same server
113 * rather than an old one that's still attached to a vnode.
115 if (cbi
&& vcbi
->server
== cbi
->server
) {
116 write_seqlock(&vnode
->cb_lock
);
117 old
= rcu_dereference_protected(vnode
->cb_interest
,
118 lockdep_is_held(&vnode
->cb_lock
.lock
));
119 rcu_assign_pointer(vnode
->cb_interest
, cbi
);
120 write_sequnlock(&vnode
->cb_lock
);
121 afs_put_cb_interest(afs_v2net(vnode
), old
);
125 /* Re-use the one attached to the vnode. */
126 if (!cbi
&& vcbi
->server
== server
) {
127 write_lock(&slist
->lock
);
128 if (entry
->cb_interest
) {
129 write_unlock(&slist
->lock
);
130 afs_put_cb_interest(afs_v2net(vnode
), cbi
);
134 entry
->cb_interest
= cbi
;
135 write_unlock(&slist
->lock
);
141 new = afs_create_interest(server
, vnode
);
145 write_lock(&slist
->lock
);
146 if (!entry
->cb_interest
) {
147 entry
->cb_interest
= afs_get_cb_interest(new);
151 cbi
= afs_get_cb_interest(entry
->cb_interest
);
153 write_unlock(&slist
->lock
);
154 afs_put_cb_interest(afs_v2net(vnode
), new);
159 /* Change the server the vnode is using. This entails scrubbing any
160 * interest the vnode had in the previous server it was using.
162 write_seqlock(&vnode
->cb_lock
);
164 old
= rcu_dereference_protected(vnode
->cb_interest
,
165 lockdep_is_held(&vnode
->cb_lock
.lock
));
166 rcu_assign_pointer(vnode
->cb_interest
, cbi
);
167 vnode
->cb_s_break
= cbi
->server
->cb_s_break
;
168 vnode
->cb_v_break
= vnode
->volume
->cb_v_break
;
169 clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
);
171 write_sequnlock(&vnode
->cb_lock
);
172 afs_put_cb_interest(afs_v2net(vnode
), old
);
177 * Remove an interest on a server.
179 void afs_put_cb_interest(struct afs_net
*net
, struct afs_cb_interest
*cbi
)
181 struct afs_vol_interest
*vi
;
183 if (cbi
&& refcount_dec_and_test(&cbi
->usage
)) {
184 if (!hlist_unhashed(&cbi
->cb_vlink
)) {
185 write_lock(&cbi
->server
->cb_break_lock
);
187 hlist_del_init(&cbi
->cb_vlink
);
188 vi
= cbi
->vol_interest
;
189 cbi
->vol_interest
= NULL
;
190 if (--vi
->usage
== 0)
191 hlist_del(&vi
->srv_link
);
195 write_unlock(&cbi
->server
->cb_break_lock
);
198 afs_put_server(net
, cbi
->server
, afs_server_trace_put_cbi
);
205 * allow the fileserver to request callback state (re-)initialisation
207 void afs_init_callback_state(struct afs_server
*server
)
209 server
->cb_s_break
++;
213 * actually break a callback
215 void __afs_break_callback(struct afs_vnode
*vnode
, enum afs_cb_break_reason reason
)
219 clear_bit(AFS_VNODE_NEW_CONTENT
, &vnode
->flags
);
220 if (test_and_clear_bit(AFS_VNODE_CB_PROMISED
, &vnode
->flags
)) {
222 afs_clear_permits(vnode
);
224 if (vnode
->lock_state
== AFS_VNODE_LOCK_WAITING_FOR_CB
)
225 afs_lock_may_be_available(vnode
);
227 trace_afs_cb_break(&vnode
->fid
, vnode
->cb_break
, reason
, true);
229 trace_afs_cb_break(&vnode
->fid
, vnode
->cb_break
, reason
, false);
233 void afs_break_callback(struct afs_vnode
*vnode
, enum afs_cb_break_reason reason
)
235 write_seqlock(&vnode
->cb_lock
);
236 __afs_break_callback(vnode
, reason
);
237 write_sequnlock(&vnode
->cb_lock
);
241 * allow the fileserver to explicitly break one callback
243 * - the backing file is changed
244 * - a lock is released
246 static void afs_break_one_callback(struct afs_server
*server
,
249 struct afs_vol_interest
*vi
;
250 struct afs_cb_interest
*cbi
;
251 struct afs_iget_data data
;
252 struct afs_vnode
*vnode
;
255 read_lock(&server
->cb_break_lock
);
256 hlist_for_each_entry(vi
, &server
->cb_volumes
, srv_link
) {
257 if (vi
->vid
< fid
->vid
)
259 if (vi
->vid
> fid
->vid
) {
263 //atomic_inc(&vi->usage);
267 /* TODO: Find all matching volumes if we couldn't match the server and
273 /* Step through all interested superblocks. There may be more than one
274 * because of cell aliasing.
276 hlist_for_each_entry(cbi
, &vi
->cb_interests
, cb_vlink
) {
277 if (fid
->vnode
== 0 && fid
->unique
== 0) {
278 /* The callback break applies to an entire volume. */
279 struct afs_super_info
*as
= AFS_FS_S(cbi
->sb
);
280 struct afs_volume
*volume
= as
->volume
;
282 write_lock(&volume
->cb_v_break_lock
);
283 volume
->cb_v_break
++;
284 trace_afs_cb_break(fid
, volume
->cb_v_break
,
285 afs_cb_break_for_volume_callback
, false);
286 write_unlock(&volume
->cb_v_break_lock
);
290 inode
= ilookup5_nowait(cbi
->sb
, fid
->vnode
,
291 afs_iget5_test
, &data
);
293 vnode
= AFS_FS_I(inode
);
294 afs_break_callback(vnode
, afs_cb_break_for_callback
);
297 trace_afs_cb_miss(fid
, afs_cb_break_for_callback
);
303 read_unlock(&server
->cb_break_lock
);
307 * allow the fileserver to break callback promises
309 void afs_break_callbacks(struct afs_server
*server
, size_t count
,
310 struct afs_callback_break
*callbacks
)
312 _enter("%p,%zu,", server
, count
);
314 ASSERT(server
!= NULL
);
316 /* TODO: Sort the callback break list by volume ID */
318 for (; count
> 0; callbacks
++, count
--) {
319 _debug("- Fid { vl=%08llx n=%llu u=%u }",
321 callbacks
->fid
.vnode
,
322 callbacks
->fid
.unique
);
323 afs_break_one_callback(server
, &callbacks
->fid
);
331 * Clear the callback interests in a server list.
333 void afs_clear_callback_interests(struct afs_net
*net
, struct afs_server_list
*slist
)
337 for (i
= 0; i
< slist
->nr_servers
; i
++) {
338 afs_put_cb_interest(net
, slist
->servers
[i
].cb_interest
);
339 slist
->servers
[i
].cb_interest
= NULL
;