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