]>
Commit | Line | Data |
---|---|---|
08e0e7c8 | 1 | /* AFS volume location management |
1da177e4 | 2 | * |
08e0e7c8 | 3 | * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved. |
1da177e4 LT |
4 | * Written by David Howells (dhowells@redhat.com) |
5 | * | |
6 | * This program is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU General Public License | |
8 | * as published by the Free Software Foundation; either version | |
9 | * 2 of the License, or (at your option) any later version. | |
10 | */ | |
11 | ||
12 | #include <linux/kernel.h> | |
13 | #include <linux/module.h> | |
14 | #include <linux/init.h> | |
1da177e4 LT |
15 | #include "internal.h" |
16 | ||
08e0e7c8 DH |
17 | unsigned afs_vlocation_timeout = 10; /* volume location timeout in seconds */ |
18 | unsigned afs_vlocation_update_timeout = 10 * 60; | |
1da177e4 | 19 | |
08e0e7c8 DH |
20 | static void afs_vlocation_reaper(struct work_struct *); |
21 | static void afs_vlocation_updater(struct work_struct *); | |
1da177e4 | 22 | |
08e0e7c8 DH |
23 | static LIST_HEAD(afs_vlocation_updates); |
24 | static LIST_HEAD(afs_vlocation_graveyard); | |
25 | static DEFINE_SPINLOCK(afs_vlocation_updates_lock); | |
26 | static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock); | |
27 | static DECLARE_DELAYED_WORK(afs_vlocation_reap, afs_vlocation_reaper); | |
28 | static DECLARE_DELAYED_WORK(afs_vlocation_update, afs_vlocation_updater); | |
29 | static struct workqueue_struct *afs_vlocation_update_worker; | |
1da177e4 | 30 | |
1da177e4 LT |
31 | /* |
32 | * iterate through the VL servers in a cell until one of them admits knowing | |
33 | * about the volume in question | |
1da177e4 | 34 | */ |
08e0e7c8 | 35 | static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vl, |
00d3b7a4 | 36 | struct key *key, |
1da177e4 LT |
37 | struct afs_cache_vlocation *vldb) |
38 | { | |
08e0e7c8 DH |
39 | struct afs_cell *cell = vl->cell; |
40 | struct in_addr addr; | |
1da177e4 LT |
41 | int count, ret; |
42 | ||
08e0e7c8 | 43 | _enter("%s,%s", cell->name, vl->vldb.name); |
1da177e4 | 44 | |
08e0e7c8 | 45 | down_write(&vl->cell->vl_sem); |
1da177e4 LT |
46 | ret = -ENOMEDIUM; |
47 | for (count = cell->vl_naddrs; count > 0; count--) { | |
08e0e7c8 DH |
48 | addr = cell->vl_addrs[cell->vl_curr_svix]; |
49 | ||
50 | _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr); | |
1da177e4 LT |
51 | |
52 | /* attempt to access the VL server */ | |
00d3b7a4 | 53 | ret = afs_vl_get_entry_by_name(&addr, key, vl->vldb.name, vldb, |
08e0e7c8 | 54 | &afs_sync_call); |
1da177e4 LT |
55 | switch (ret) { |
56 | case 0: | |
1da177e4 LT |
57 | goto out; |
58 | case -ENOMEM: | |
59 | case -ENONET: | |
60 | case -ENETUNREACH: | |
61 | case -EHOSTUNREACH: | |
62 | case -ECONNREFUSED: | |
1da177e4 LT |
63 | if (ret == -ENOMEM || ret == -ENONET) |
64 | goto out; | |
65 | goto rotate; | |
66 | case -ENOMEDIUM: | |
1da177e4 LT |
67 | goto out; |
68 | default: | |
08e0e7c8 | 69 | ret = -EIO; |
1da177e4 LT |
70 | goto rotate; |
71 | } | |
72 | ||
73 | /* rotate the server records upon lookup failure */ | |
74 | rotate: | |
75 | cell->vl_curr_svix++; | |
76 | cell->vl_curr_svix %= cell->vl_naddrs; | |
77 | } | |
78 | ||
ec26815a | 79 | out: |
08e0e7c8 | 80 | up_write(&vl->cell->vl_sem); |
1da177e4 LT |
81 | _leave(" = %d", ret); |
82 | return ret; | |
ec26815a | 83 | } |
1da177e4 | 84 | |
1da177e4 LT |
85 | /* |
86 | * iterate through the VL servers in a cell until one of them admits knowing | |
87 | * about the volume in question | |
1da177e4 | 88 | */ |
08e0e7c8 | 89 | static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vl, |
00d3b7a4 | 90 | struct key *key, |
1da177e4 LT |
91 | afs_volid_t volid, |
92 | afs_voltype_t voltype, | |
93 | struct afs_cache_vlocation *vldb) | |
94 | { | |
08e0e7c8 DH |
95 | struct afs_cell *cell = vl->cell; |
96 | struct in_addr addr; | |
1da177e4 LT |
97 | int count, ret; |
98 | ||
99 | _enter("%s,%x,%d,", cell->name, volid, voltype); | |
100 | ||
08e0e7c8 | 101 | down_write(&vl->cell->vl_sem); |
1da177e4 LT |
102 | ret = -ENOMEDIUM; |
103 | for (count = cell->vl_naddrs; count > 0; count--) { | |
08e0e7c8 DH |
104 | addr = cell->vl_addrs[cell->vl_curr_svix]; |
105 | ||
106 | _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr); | |
1da177e4 LT |
107 | |
108 | /* attempt to access the VL server */ | |
00d3b7a4 | 109 | ret = afs_vl_get_entry_by_id(&addr, key, volid, voltype, vldb, |
08e0e7c8 | 110 | &afs_sync_call); |
1da177e4 LT |
111 | switch (ret) { |
112 | case 0: | |
1da177e4 LT |
113 | goto out; |
114 | case -ENOMEM: | |
115 | case -ENONET: | |
116 | case -ENETUNREACH: | |
117 | case -EHOSTUNREACH: | |
118 | case -ECONNREFUSED: | |
1da177e4 LT |
119 | if (ret == -ENOMEM || ret == -ENONET) |
120 | goto out; | |
121 | goto rotate; | |
08e0e7c8 DH |
122 | case -EBUSY: |
123 | vl->upd_busy_cnt++; | |
124 | if (vl->upd_busy_cnt <= 3) { | |
125 | if (vl->upd_busy_cnt > 1) { | |
126 | /* second+ BUSY - sleep a little bit */ | |
127 | set_current_state(TASK_UNINTERRUPTIBLE); | |
128 | schedule_timeout(1); | |
129 | __set_current_state(TASK_RUNNING); | |
130 | } | |
131 | continue; | |
132 | } | |
133 | break; | |
1da177e4 | 134 | case -ENOMEDIUM: |
08e0e7c8 DH |
135 | vl->upd_rej_cnt++; |
136 | goto rotate; | |
1da177e4 | 137 | default: |
08e0e7c8 | 138 | ret = -EIO; |
1da177e4 LT |
139 | goto rotate; |
140 | } | |
141 | ||
142 | /* rotate the server records upon lookup failure */ | |
143 | rotate: | |
144 | cell->vl_curr_svix++; | |
145 | cell->vl_curr_svix %= cell->vl_naddrs; | |
08e0e7c8 | 146 | vl->upd_busy_cnt = 0; |
1da177e4 LT |
147 | } |
148 | ||
ec26815a | 149 | out: |
08e0e7c8 DH |
150 | if (ret < 0 && vl->upd_rej_cnt > 0) { |
151 | printk(KERN_NOTICE "kAFS:" | |
152 | " Active volume no longer valid '%s'\n", | |
153 | vl->vldb.name); | |
154 | vl->valid = 0; | |
155 | ret = -ENOMEDIUM; | |
156 | } | |
157 | ||
158 | up_write(&vl->cell->vl_sem); | |
1da177e4 LT |
159 | _leave(" = %d", ret); |
160 | return ret; | |
ec26815a | 161 | } |
1da177e4 | 162 | |
1da177e4 | 163 | /* |
08e0e7c8 | 164 | * allocate a volume location record |
1da177e4 | 165 | */ |
08e0e7c8 DH |
166 | static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell, |
167 | const char *name, | |
168 | size_t namesz) | |
1da177e4 | 169 | { |
08e0e7c8 DH |
170 | struct afs_vlocation *vl; |
171 | ||
172 | vl = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL); | |
173 | if (vl) { | |
174 | vl->cell = cell; | |
175 | vl->state = AFS_VL_NEW; | |
176 | atomic_set(&vl->usage, 1); | |
177 | INIT_LIST_HEAD(&vl->link); | |
178 | INIT_LIST_HEAD(&vl->grave); | |
179 | INIT_LIST_HEAD(&vl->update); | |
180 | init_waitqueue_head(&vl->waitq); | |
181 | rwlock_init(&vl->lock); | |
182 | memcpy(vl->vldb.name, name, namesz); | |
1da177e4 LT |
183 | } |
184 | ||
08e0e7c8 DH |
185 | _leave(" = %p", vl); |
186 | return vl; | |
187 | } | |
1da177e4 | 188 | |
08e0e7c8 DH |
189 | /* |
190 | * update record if we found it in the cache | |
191 | */ | |
192 | static int afs_vlocation_update_record(struct afs_vlocation *vl, | |
00d3b7a4 | 193 | struct key *key, |
08e0e7c8 DH |
194 | struct afs_cache_vlocation *vldb) |
195 | { | |
196 | afs_voltype_t voltype; | |
197 | afs_volid_t vid; | |
198 | int ret; | |
1da177e4 | 199 | |
1da177e4 | 200 | /* try to look up a cached volume in the cell VL databases by ID */ |
1da177e4 | 201 | _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }", |
08e0e7c8 DH |
202 | vl->vldb.name, |
203 | vl->vldb.vidmask, | |
204 | ntohl(vl->vldb.servers[0].s_addr), | |
205 | vl->vldb.srvtmask[0], | |
206 | ntohl(vl->vldb.servers[1].s_addr), | |
207 | vl->vldb.srvtmask[1], | |
208 | ntohl(vl->vldb.servers[2].s_addr), | |
209 | vl->vldb.srvtmask[2]); | |
1da177e4 LT |
210 | |
211 | _debug("Vids: %08x %08x %08x", | |
08e0e7c8 DH |
212 | vl->vldb.vid[0], |
213 | vl->vldb.vid[1], | |
214 | vl->vldb.vid[2]); | |
1da177e4 | 215 | |
08e0e7c8 DH |
216 | if (vl->vldb.vidmask & AFS_VOL_VTM_RW) { |
217 | vid = vl->vldb.vid[0]; | |
1da177e4 | 218 | voltype = AFSVL_RWVOL; |
08e0e7c8 DH |
219 | } else if (vl->vldb.vidmask & AFS_VOL_VTM_RO) { |
220 | vid = vl->vldb.vid[1]; | |
1da177e4 | 221 | voltype = AFSVL_ROVOL; |
08e0e7c8 DH |
222 | } else if (vl->vldb.vidmask & AFS_VOL_VTM_BAK) { |
223 | vid = vl->vldb.vid[2]; | |
1da177e4 | 224 | voltype = AFSVL_BACKVOL; |
ec26815a | 225 | } else { |
1da177e4 LT |
226 | BUG(); |
227 | vid = 0; | |
228 | voltype = 0; | |
229 | } | |
230 | ||
08e0e7c8 DH |
231 | /* contact the server to make sure the volume is still available |
232 | * - TODO: need to handle disconnected operation here | |
233 | */ | |
00d3b7a4 | 234 | ret = afs_vlocation_access_vl_by_id(vl, key, vid, voltype, vldb); |
1da177e4 LT |
235 | switch (ret) { |
236 | /* net error */ | |
237 | default: | |
08e0e7c8 DH |
238 | printk(KERN_WARNING "kAFS:" |
239 | " failed to update volume '%s' (%x) up in '%s': %d\n", | |
240 | vl->vldb.name, vid, vl->cell->name, ret); | |
241 | _leave(" = %d", ret); | |
242 | return ret; | |
1da177e4 LT |
243 | |
244 | /* pulled from local cache into memory */ | |
245 | case 0: | |
08e0e7c8 DH |
246 | _leave(" = 0"); |
247 | return 0; | |
1da177e4 LT |
248 | |
249 | /* uh oh... looks like the volume got deleted */ | |
250 | case -ENOMEDIUM: | |
08e0e7c8 DH |
251 | printk(KERN_ERR "kAFS:" |
252 | " volume '%s' (%x) does not exist '%s'\n", | |
253 | vl->vldb.name, vid, vl->cell->name); | |
1da177e4 LT |
254 | |
255 | /* TODO: make existing record unavailable */ | |
08e0e7c8 DH |
256 | _leave(" = %d", ret); |
257 | return ret; | |
1da177e4 | 258 | } |
08e0e7c8 | 259 | } |
1da177e4 | 260 | |
08e0e7c8 DH |
261 | /* |
262 | * apply the update to a VL record | |
263 | */ | |
264 | static void afs_vlocation_apply_update(struct afs_vlocation *vl, | |
265 | struct afs_cache_vlocation *vldb) | |
266 | { | |
267 | _debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }", | |
268 | vldb->name, vldb->vidmask, | |
269 | ntohl(vldb->servers[0].s_addr), vldb->srvtmask[0], | |
270 | ntohl(vldb->servers[1].s_addr), vldb->srvtmask[1], | |
271 | ntohl(vldb->servers[2].s_addr), vldb->srvtmask[2]); | |
1da177e4 | 272 | |
08e0e7c8 DH |
273 | _debug("Vids: %08x %08x %08x", |
274 | vldb->vid[0], vldb->vid[1], vldb->vid[2]); | |
1da177e4 | 275 | |
08e0e7c8 DH |
276 | if (strcmp(vldb->name, vl->vldb.name) != 0) |
277 | printk(KERN_NOTICE "kAFS:" | |
278 | " name of volume '%s' changed to '%s' on server\n", | |
279 | vl->vldb.name, vldb->name); | |
1da177e4 | 280 | |
08e0e7c8 | 281 | vl->vldb = *vldb; |
1da177e4 LT |
282 | |
283 | #ifdef AFS_CACHING_SUPPORT | |
284 | /* update volume entry in local cache */ | |
08e0e7c8 | 285 | cachefs_update_cookie(vl->cache); |
1da177e4 | 286 | #endif |
ec26815a | 287 | } |
1da177e4 | 288 | |
1da177e4 | 289 | /* |
08e0e7c8 DH |
290 | * fill in a volume location record, consulting the cache and the VL server |
291 | * both | |
1da177e4 | 292 | */ |
00d3b7a4 DH |
293 | static int afs_vlocation_fill_in_record(struct afs_vlocation *vl, |
294 | struct key *key) | |
1da177e4 | 295 | { |
08e0e7c8 DH |
296 | struct afs_cache_vlocation vldb; |
297 | int ret; | |
1da177e4 | 298 | |
08e0e7c8 | 299 | _enter(""); |
1da177e4 | 300 | |
08e0e7c8 | 301 | ASSERTCMP(vl->valid, ==, 0); |
1da177e4 | 302 | |
08e0e7c8 | 303 | memset(&vldb, 0, sizeof(vldb)); |
1da177e4 | 304 | |
08e0e7c8 DH |
305 | /* see if we have an in-cache copy (will set vl->valid if there is) */ |
306 | #ifdef AFS_CACHING_SUPPORT | |
307 | cachefs_acquire_cookie(cell->cache, | |
308 | &afs_volume_cache_index_def, | |
309 | vlocation, | |
310 | &vl->cache); | |
311 | #endif | |
1da177e4 | 312 | |
08e0e7c8 DH |
313 | if (vl->valid) { |
314 | /* try to update a known volume in the cell VL databases by | |
315 | * ID as the name may have changed */ | |
316 | _debug("found in cache"); | |
00d3b7a4 | 317 | ret = afs_vlocation_update_record(vl, key, &vldb); |
08e0e7c8 DH |
318 | } else { |
319 | /* try to look up an unknown volume in the cell VL databases by | |
320 | * name */ | |
00d3b7a4 | 321 | ret = afs_vlocation_access_vl_by_name(vl, key, &vldb); |
08e0e7c8 DH |
322 | if (ret < 0) { |
323 | printk("kAFS: failed to locate '%s' in cell '%s'\n", | |
324 | vl->vldb.name, vl->cell->name); | |
325 | return ret; | |
326 | } | |
1da177e4 LT |
327 | } |
328 | ||
08e0e7c8 DH |
329 | afs_vlocation_apply_update(vl, &vldb); |
330 | _leave(" = 0"); | |
331 | return 0; | |
ec26815a | 332 | } |
1da177e4 | 333 | |
1da177e4 | 334 | /* |
08e0e7c8 | 335 | * queue a vlocation record for updates |
1da177e4 | 336 | */ |
08e0e7c8 | 337 | void afs_vlocation_queue_for_updates(struct afs_vlocation *vl) |
1da177e4 | 338 | { |
08e0e7c8 | 339 | struct afs_vlocation *xvl; |
1da177e4 | 340 | |
08e0e7c8 DH |
341 | /* wait at least 10 minutes before updating... */ |
342 | vl->update_at = get_seconds() + afs_vlocation_update_timeout; | |
343 | ||
344 | spin_lock(&afs_vlocation_updates_lock); | |
345 | ||
346 | if (!list_empty(&afs_vlocation_updates)) { | |
347 | /* ... but wait at least 1 second more than the newest record | |
348 | * already queued so that we don't spam the VL server suddenly | |
349 | * with lots of requests | |
350 | */ | |
351 | xvl = list_entry(afs_vlocation_updates.prev, | |
352 | struct afs_vlocation, update); | |
353 | if (vl->update_at <= xvl->update_at) | |
354 | vl->update_at = xvl->update_at + 1; | |
355 | } else { | |
356 | queue_delayed_work(afs_vlocation_update_worker, | |
357 | &afs_vlocation_update, | |
358 | afs_vlocation_update_timeout * HZ); | |
1da177e4 | 359 | } |
08e0e7c8 DH |
360 | |
361 | list_add_tail(&vl->update, &afs_vlocation_updates); | |
362 | spin_unlock(&afs_vlocation_updates_lock); | |
ec26815a | 363 | } |
1da177e4 | 364 | |
1da177e4 | 365 | /* |
08e0e7c8 DH |
366 | * lookup volume location |
367 | * - iterate through the VL servers in a cell until one of them admits knowing | |
368 | * about the volume in question | |
369 | * - lookup in the local cache if not able to find on the VL server | |
370 | * - insert/update in the local cache if did get a VL response | |
1da177e4 | 371 | */ |
08e0e7c8 | 372 | struct afs_vlocation *afs_vlocation_lookup(struct afs_cell *cell, |
00d3b7a4 | 373 | struct key *key, |
08e0e7c8 DH |
374 | const char *name, |
375 | size_t namesz) | |
1da177e4 | 376 | { |
08e0e7c8 DH |
377 | struct afs_vlocation *vl; |
378 | int ret; | |
1da177e4 | 379 | |
00d3b7a4 DH |
380 | _enter("{%s},{%x},%*.*s,%zu", |
381 | cell->name, key_serial(key), | |
382 | (int) namesz, (int) namesz, name, namesz); | |
1da177e4 | 383 | |
08e0e7c8 DH |
384 | if (namesz > sizeof(vl->vldb.name)) { |
385 | _leave(" = -ENAMETOOLONG"); | |
386 | return ERR_PTR(-ENAMETOOLONG); | |
387 | } | |
1da177e4 | 388 | |
08e0e7c8 DH |
389 | /* see if we have an in-memory copy first */ |
390 | down_write(&cell->vl_sem); | |
391 | spin_lock(&cell->vl_lock); | |
392 | list_for_each_entry(vl, &cell->vl_list, link) { | |
393 | if (vl->vldb.name[namesz] != '\0') | |
394 | continue; | |
395 | if (memcmp(vl->vldb.name, name, namesz) == 0) | |
396 | goto found_in_memory; | |
1da177e4 | 397 | } |
08e0e7c8 | 398 | spin_unlock(&cell->vl_lock); |
1da177e4 | 399 | |
08e0e7c8 DH |
400 | /* not in the cell's in-memory lists - create a new record */ |
401 | vl = afs_vlocation_alloc(cell, name, namesz); | |
402 | if (!vl) { | |
403 | up_write(&cell->vl_sem); | |
404 | return ERR_PTR(-ENOMEM); | |
405 | } | |
1da177e4 | 406 | |
08e0e7c8 | 407 | afs_get_cell(cell); |
1da177e4 | 408 | |
08e0e7c8 DH |
409 | list_add_tail(&vl->link, &cell->vl_list); |
410 | vl->state = AFS_VL_CREATING; | |
411 | up_write(&cell->vl_sem); | |
1da177e4 | 412 | |
08e0e7c8 | 413 | fill_in_record: |
00d3b7a4 | 414 | ret = afs_vlocation_fill_in_record(vl, key); |
08e0e7c8 DH |
415 | if (ret < 0) |
416 | goto error_abandon; | |
417 | vl->state = AFS_VL_VALID; | |
418 | wake_up(&vl->waitq); | |
1da177e4 | 419 | |
08e0e7c8 DH |
420 | /* schedule for regular updates */ |
421 | afs_vlocation_queue_for_updates(vl); | |
422 | goto success; | |
1da177e4 | 423 | |
08e0e7c8 DH |
424 | found_in_memory: |
425 | /* found in memory */ | |
426 | _debug("found in memory"); | |
427 | atomic_inc(&vl->usage); | |
428 | spin_unlock(&cell->vl_lock); | |
429 | if (!list_empty(&vl->grave)) { | |
430 | spin_lock(&afs_vlocation_graveyard_lock); | |
431 | list_del_init(&vl->grave); | |
432 | spin_unlock(&afs_vlocation_graveyard_lock); | |
1da177e4 | 433 | } |
08e0e7c8 | 434 | up_write(&cell->vl_sem); |
1da177e4 | 435 | |
08e0e7c8 DH |
436 | /* see if it was an abandoned record that we might try filling in */ |
437 | while (vl->state != AFS_VL_VALID) { | |
438 | afs_vlocation_state_t state = vl->state; | |
1da177e4 | 439 | |
08e0e7c8 | 440 | _debug("invalid [state %d]", state); |
1da177e4 | 441 | |
08e0e7c8 DH |
442 | if ((state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME)) { |
443 | if (cmpxchg(&vl->state, state, AFS_VL_CREATING) == | |
444 | state) | |
445 | goto fill_in_record; | |
446 | continue; | |
447 | } | |
448 | ||
449 | /* must now wait for creation or update by someone else to | |
450 | * complete */ | |
451 | _debug("wait"); | |
452 | ||
453 | ret = wait_event_interruptible( | |
454 | vl->waitq, | |
455 | vl->state == AFS_VL_NEW || | |
456 | vl->state == AFS_VL_VALID || | |
457 | vl->state == AFS_VL_NO_VOLUME); | |
458 | if (ret < 0) | |
459 | goto error; | |
1da177e4 LT |
460 | } |
461 | ||
08e0e7c8 DH |
462 | success: |
463 | _leave(" = %p",vl); | |
464 | return vl; | |
465 | ||
466 | error_abandon: | |
467 | vl->state = AFS_VL_NEW; | |
468 | wake_up(&vl->waitq); | |
469 | error: | |
470 | ASSERT(vl != NULL); | |
471 | afs_put_vlocation(vl); | |
1da177e4 | 472 | _leave(" = %d", ret); |
08e0e7c8 | 473 | return ERR_PTR(ret); |
ec26815a | 474 | } |
1da177e4 | 475 | |
1da177e4 | 476 | /* |
08e0e7c8 | 477 | * finish using a volume location record |
1da177e4 | 478 | */ |
08e0e7c8 | 479 | void afs_put_vlocation(struct afs_vlocation *vl) |
1da177e4 | 480 | { |
08e0e7c8 DH |
481 | if (!vl) |
482 | return; | |
1da177e4 | 483 | |
08e0e7c8 | 484 | _enter("%s", vl->vldb.name); |
1da177e4 | 485 | |
08e0e7c8 | 486 | ASSERTCMP(atomic_read(&vl->usage), >, 0); |
1da177e4 | 487 | |
08e0e7c8 DH |
488 | if (likely(!atomic_dec_and_test(&vl->usage))) { |
489 | _leave(""); | |
490 | return; | |
491 | } | |
1da177e4 | 492 | |
08e0e7c8 DH |
493 | spin_lock(&afs_vlocation_graveyard_lock); |
494 | if (atomic_read(&vl->usage) == 0) { | |
495 | _debug("buried"); | |
496 | list_move_tail(&vl->grave, &afs_vlocation_graveyard); | |
497 | vl->time_of_death = get_seconds(); | |
498 | schedule_delayed_work(&afs_vlocation_reap, | |
499 | afs_vlocation_timeout * HZ); | |
500 | ||
501 | /* suspend updates on this record */ | |
502 | if (!list_empty(&vl->update)) { | |
503 | spin_lock(&afs_vlocation_updates_lock); | |
504 | list_del_init(&vl->update); | |
505 | spin_unlock(&afs_vlocation_updates_lock); | |
506 | } | |
507 | } | |
508 | spin_unlock(&afs_vlocation_graveyard_lock); | |
509 | _leave(" [killed?]"); | |
ec26815a | 510 | } |
1da177e4 | 511 | |
1da177e4 | 512 | /* |
08e0e7c8 | 513 | * destroy a dead volume location record |
1da177e4 | 514 | */ |
08e0e7c8 | 515 | static void afs_vlocation_destroy(struct afs_vlocation *vl) |
1da177e4 | 516 | { |
08e0e7c8 | 517 | _enter("%p", vl); |
1da177e4 | 518 | |
08e0e7c8 DH |
519 | #ifdef AFS_CACHING_SUPPORT |
520 | cachefs_relinquish_cookie(vl->cache, 0); | |
521 | #endif | |
1da177e4 | 522 | |
08e0e7c8 DH |
523 | afs_put_cell(vl->cell); |
524 | kfree(vl); | |
525 | } | |
526 | ||
527 | /* | |
528 | * reap dead volume location records | |
529 | */ | |
530 | static void afs_vlocation_reaper(struct work_struct *work) | |
531 | { | |
532 | LIST_HEAD(corpses); | |
533 | struct afs_vlocation *vl; | |
534 | unsigned long delay, expiry; | |
535 | time_t now; | |
1da177e4 | 536 | |
08e0e7c8 | 537 | _enter(""); |
1da177e4 | 538 | |
08e0e7c8 DH |
539 | now = get_seconds(); |
540 | spin_lock(&afs_vlocation_graveyard_lock); | |
541 | ||
542 | while (!list_empty(&afs_vlocation_graveyard)) { | |
543 | vl = list_entry(afs_vlocation_graveyard.next, | |
544 | struct afs_vlocation, grave); | |
545 | ||
546 | _debug("check %p", vl); | |
547 | ||
548 | /* the queue is ordered most dead first */ | |
549 | expiry = vl->time_of_death + afs_vlocation_timeout; | |
550 | if (expiry > now) { | |
551 | delay = (expiry - now) * HZ; | |
552 | _debug("delay %lu", delay); | |
553 | if (!schedule_delayed_work(&afs_vlocation_reap, | |
554 | delay)) { | |
555 | cancel_delayed_work(&afs_vlocation_reap); | |
556 | schedule_delayed_work(&afs_vlocation_reap, | |
557 | delay); | |
558 | } | |
559 | break; | |
560 | } | |
1da177e4 | 561 | |
08e0e7c8 DH |
562 | spin_lock(&vl->cell->vl_lock); |
563 | if (atomic_read(&vl->usage) > 0) { | |
564 | _debug("no reap"); | |
565 | list_del_init(&vl->grave); | |
ec26815a | 566 | } else { |
08e0e7c8 DH |
567 | _debug("reap"); |
568 | list_move_tail(&vl->grave, &corpses); | |
569 | list_del_init(&vl->link); | |
1da177e4 | 570 | } |
08e0e7c8 | 571 | spin_unlock(&vl->cell->vl_lock); |
1da177e4 LT |
572 | } |
573 | ||
08e0e7c8 | 574 | spin_unlock(&afs_vlocation_graveyard_lock); |
1da177e4 | 575 | |
08e0e7c8 DH |
576 | /* now reap the corpses we've extracted */ |
577 | while (!list_empty(&corpses)) { | |
578 | vl = list_entry(corpses.next, struct afs_vlocation, grave); | |
579 | list_del(&vl->grave); | |
580 | afs_vlocation_destroy(vl); | |
1da177e4 LT |
581 | } |
582 | ||
583 | _leave(""); | |
08e0e7c8 | 584 | } |
1da177e4 | 585 | |
08e0e7c8 DH |
586 | /* |
587 | * initialise the VL update process | |
588 | */ | |
589 | int __init afs_vlocation_update_init(void) | |
590 | { | |
591 | afs_vlocation_update_worker = | |
592 | create_singlethread_workqueue("kafs_vlupdated"); | |
593 | return afs_vlocation_update_worker ? 0 : -ENOMEM; | |
594 | } | |
595 | ||
596 | /* | |
597 | * discard all the volume location records for rmmod | |
598 | */ | |
599 | void __exit afs_vlocation_purge(void) | |
600 | { | |
601 | afs_vlocation_timeout = 0; | |
602 | ||
603 | spin_lock(&afs_vlocation_updates_lock); | |
604 | list_del_init(&afs_vlocation_updates); | |
605 | spin_unlock(&afs_vlocation_updates_lock); | |
606 | cancel_delayed_work(&afs_vlocation_update); | |
607 | queue_delayed_work(afs_vlocation_update_worker, | |
608 | &afs_vlocation_update, 0); | |
609 | destroy_workqueue(afs_vlocation_update_worker); | |
610 | ||
611 | cancel_delayed_work(&afs_vlocation_reap); | |
612 | schedule_delayed_work(&afs_vlocation_reap, 0); | |
ec26815a | 613 | } |
1da177e4 | 614 | |
1da177e4 | 615 | /* |
08e0e7c8 | 616 | * update a volume location |
1da177e4 | 617 | */ |
08e0e7c8 | 618 | static void afs_vlocation_updater(struct work_struct *work) |
1da177e4 LT |
619 | { |
620 | struct afs_cache_vlocation vldb; | |
08e0e7c8 DH |
621 | struct afs_vlocation *vl, *xvl; |
622 | time_t now; | |
623 | long timeout; | |
1da177e4 LT |
624 | int ret; |
625 | ||
08e0e7c8 | 626 | _enter(""); |
1da177e4 | 627 | |
08e0e7c8 | 628 | now = get_seconds(); |
1da177e4 | 629 | |
08e0e7c8 DH |
630 | /* find a record to update */ |
631 | spin_lock(&afs_vlocation_updates_lock); | |
632 | for (;;) { | |
633 | if (list_empty(&afs_vlocation_updates)) { | |
634 | spin_unlock(&afs_vlocation_updates_lock); | |
635 | _leave(" [nothing]"); | |
636 | return; | |
637 | } | |
1da177e4 | 638 | |
08e0e7c8 DH |
639 | vl = list_entry(afs_vlocation_updates.next, |
640 | struct afs_vlocation, update); | |
641 | if (atomic_read(&vl->usage) > 0) | |
642 | break; | |
643 | list_del_init(&vl->update); | |
1da177e4 LT |
644 | } |
645 | ||
08e0e7c8 DH |
646 | timeout = vl->update_at - now; |
647 | if (timeout > 0) { | |
648 | queue_delayed_work(afs_vlocation_update_worker, | |
649 | &afs_vlocation_update, timeout * HZ); | |
650 | spin_unlock(&afs_vlocation_updates_lock); | |
651 | _leave(" [nothing]"); | |
1da177e4 LT |
652 | return; |
653 | } | |
654 | ||
08e0e7c8 DH |
655 | list_del_init(&vl->update); |
656 | atomic_inc(&vl->usage); | |
657 | spin_unlock(&afs_vlocation_updates_lock); | |
1da177e4 | 658 | |
08e0e7c8 DH |
659 | /* we can now perform the update */ |
660 | _debug("update %s", vl->vldb.name); | |
661 | vl->state = AFS_VL_UPDATING; | |
662 | vl->upd_rej_cnt = 0; | |
663 | vl->upd_busy_cnt = 0; | |
1da177e4 | 664 | |
00d3b7a4 | 665 | ret = afs_vlocation_update_record(vl, NULL, &vldb); |
08e0e7c8 DH |
666 | switch (ret) { |
667 | case 0: | |
668 | afs_vlocation_apply_update(vl, &vldb); | |
669 | vl->state = AFS_VL_VALID; | |
670 | break; | |
671 | case -ENOMEDIUM: | |
672 | vl->state = AFS_VL_VOLUME_DELETED; | |
673 | break; | |
674 | default: | |
675 | vl->state = AFS_VL_UNCERTAIN; | |
676 | break; | |
677 | } | |
1da177e4 | 678 | |
08e0e7c8 DH |
679 | /* and then reschedule */ |
680 | _debug("reschedule"); | |
681 | vl->update_at = get_seconds() + afs_vlocation_update_timeout; | |
1da177e4 | 682 | |
08e0e7c8 | 683 | spin_lock(&afs_vlocation_updates_lock); |
1da177e4 | 684 | |
08e0e7c8 DH |
685 | if (!list_empty(&afs_vlocation_updates)) { |
686 | /* next update in 10 minutes, but wait at least 1 second more | |
687 | * than the newest record already queued so that we don't spam | |
688 | * the VL server suddenly with lots of requests | |
689 | */ | |
690 | xvl = list_entry(afs_vlocation_updates.prev, | |
691 | struct afs_vlocation, update); | |
692 | if (vl->update_at <= xvl->update_at) | |
693 | vl->update_at = xvl->update_at + 1; | |
694 | xvl = list_entry(afs_vlocation_updates.next, | |
695 | struct afs_vlocation, update); | |
696 | timeout = xvl->update_at - now; | |
697 | if (timeout < 0) | |
698 | timeout = 0; | |
699 | } else { | |
700 | timeout = afs_vlocation_update_timeout; | |
1da177e4 LT |
701 | } |
702 | ||
08e0e7c8 | 703 | ASSERT(list_empty(&vl->update)); |
1da177e4 | 704 | |
08e0e7c8 | 705 | list_add_tail(&vl->update, &afs_vlocation_updates); |
1da177e4 | 706 | |
08e0e7c8 DH |
707 | _debug("timeout %ld", timeout); |
708 | queue_delayed_work(afs_vlocation_update_worker, | |
709 | &afs_vlocation_update, timeout * HZ); | |
710 | spin_unlock(&afs_vlocation_updates_lock); | |
711 | afs_put_vlocation(vl); | |
ec26815a | 712 | } |