blob: b6df2e83809f4ac81ed5b3d02f944ac7f32d9de8 [file] [log] [blame]
David Howells08e0e7c2007-04-26 15:55:03 -07001/* AFS volume location management
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David Howells08e0e7c2007-04-26 15:55:03 -07003 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040016#include <linux/sched.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "internal.h"
18
Adrian Bunkc1206a22007-10-16 23:26:41 -070019static unsigned afs_vlocation_timeout = 10; /* volume location timeout in seconds */
20static unsigned afs_vlocation_update_timeout = 10 * 60;
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
David Howells08e0e7c2007-04-26 15:55:03 -070022static void afs_vlocation_reaper(struct work_struct *);
23static void afs_vlocation_updater(struct work_struct *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
David Howells08e0e7c2007-04-26 15:55:03 -070025static LIST_HEAD(afs_vlocation_updates);
26static LIST_HEAD(afs_vlocation_graveyard);
27static DEFINE_SPINLOCK(afs_vlocation_updates_lock);
28static DEFINE_SPINLOCK(afs_vlocation_graveyard_lock);
29static DECLARE_DELAYED_WORK(afs_vlocation_reap, afs_vlocation_reaper);
30static DECLARE_DELAYED_WORK(afs_vlocation_update, afs_vlocation_updater);
31static struct workqueue_struct *afs_vlocation_update_worker;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * iterate through the VL servers in a cell until one of them admits knowing
35 * about the volume in question
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 */
David Howells08e0e7c2007-04-26 15:55:03 -070037static int afs_vlocation_access_vl_by_name(struct afs_vlocation *vl,
David Howells00d3b7a2007-04-26 15:57:07 -070038 struct key *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 struct afs_cache_vlocation *vldb)
40{
David Howells08e0e7c2007-04-26 15:55:03 -070041 struct afs_cell *cell = vl->cell;
42 struct in_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 int count, ret;
44
David Howells08e0e7c2007-04-26 15:55:03 -070045 _enter("%s,%s", cell->name, vl->vldb.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
David Howells08e0e7c2007-04-26 15:55:03 -070047 down_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 ret = -ENOMEDIUM;
49 for (count = cell->vl_naddrs; count > 0; count--) {
David Howells08e0e7c2007-04-26 15:55:03 -070050 addr = cell->vl_addrs[cell->vl_curr_svix];
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Howells08e0e7c2007-04-26 15:55:03 -070052 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54 /* attempt to access the VL server */
David Howells00d3b7a2007-04-26 15:57:07 -070055 ret = afs_vl_get_entry_by_name(&addr, key, vl->vldb.name, vldb,
David Howells08e0e7c2007-04-26 15:55:03 -070056 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 switch (ret) {
58 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 goto out;
60 case -ENOMEM:
61 case -ENONET:
62 case -ENETUNREACH:
63 case -EHOSTUNREACH:
64 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 if (ret == -ENOMEM || ret == -ENONET)
66 goto out;
67 goto rotate;
68 case -ENOMEDIUM:
David Howells005411c2009-06-16 21:36:49 +010069 case -EKEYREJECTED:
70 case -EKEYEXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 goto out;
72 default:
David Howells08e0e7c2007-04-26 15:55:03 -070073 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 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
David Howellsec268152007-04-26 15:49:28 -070083out:
David Howells08e0e7c2007-04-26 15:55:03 -070084 up_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 _leave(" = %d", ret);
86 return ret;
David Howellsec268152007-04-26 15:49:28 -070087}
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * iterate through the VL servers in a cell until one of them admits knowing
91 * about the volume in question
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 */
David Howells08e0e7c2007-04-26 15:55:03 -070093static int afs_vlocation_access_vl_by_id(struct afs_vlocation *vl,
David Howells00d3b7a2007-04-26 15:57:07 -070094 struct key *key,
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 afs_volid_t volid,
96 afs_voltype_t voltype,
97 struct afs_cache_vlocation *vldb)
98{
David Howells08e0e7c2007-04-26 15:55:03 -070099 struct afs_cell *cell = vl->cell;
100 struct in_addr addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 int count, ret;
102
103 _enter("%s,%x,%d,", cell->name, volid, voltype);
104
David Howells08e0e7c2007-04-26 15:55:03 -0700105 down_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ret = -ENOMEDIUM;
107 for (count = cell->vl_naddrs; count > 0; count--) {
David Howells08e0e7c2007-04-26 15:55:03 -0700108 addr = cell->vl_addrs[cell->vl_curr_svix];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
David Howells08e0e7c2007-04-26 15:55:03 -0700110 _debug("CellServ[%hu]: %08x", cell->vl_curr_svix, addr.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112 /* attempt to access the VL server */
David Howells00d3b7a2007-04-26 15:57:07 -0700113 ret = afs_vl_get_entry_by_id(&addr, key, volid, voltype, vldb,
David Howells08e0e7c2007-04-26 15:55:03 -0700114 &afs_sync_call);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 switch (ret) {
116 case 0:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 goto out;
118 case -ENOMEM:
119 case -ENONET:
120 case -ENETUNREACH:
121 case -EHOSTUNREACH:
122 case -ECONNREFUSED:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 if (ret == -ENOMEM || ret == -ENONET)
124 goto out;
125 goto rotate;
David Howells08e0e7c2007-04-26 15:55:03 -0700126 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);
133 __set_current_state(TASK_RUNNING);
134 }
135 continue;
136 }
137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 case -ENOMEDIUM:
David Howells08e0e7c2007-04-26 15:55:03 -0700139 vl->upd_rej_cnt++;
140 goto rotate;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 default:
David Howells08e0e7c2007-04-26 15:55:03 -0700142 ret = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 goto rotate;
144 }
145
146 /* rotate the server records upon lookup failure */
147 rotate:
148 cell->vl_curr_svix++;
149 cell->vl_curr_svix %= cell->vl_naddrs;
David Howells08e0e7c2007-04-26 15:55:03 -0700150 vl->upd_busy_cnt = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
David Howellsec268152007-04-26 15:49:28 -0700153out:
David Howells08e0e7c2007-04-26 15:55:03 -0700154 if (ret < 0 && vl->upd_rej_cnt > 0) {
155 printk(KERN_NOTICE "kAFS:"
156 " Active volume no longer valid '%s'\n",
157 vl->vldb.name);
158 vl->valid = 0;
159 ret = -ENOMEDIUM;
160 }
161
162 up_write(&vl->cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 _leave(" = %d", ret);
164 return ret;
David Howellsec268152007-04-26 15:49:28 -0700165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167/*
David Howells08e0e7c2007-04-26 15:55:03 -0700168 * allocate a volume location record
169 */
170static struct afs_vlocation *afs_vlocation_alloc(struct afs_cell *cell,
171 const char *name,
172 size_t namesz)
173{
174 struct afs_vlocation *vl;
175
176 vl = kzalloc(sizeof(struct afs_vlocation), GFP_KERNEL);
177 if (vl) {
178 vl->cell = cell;
179 vl->state = AFS_VL_NEW;
180 atomic_set(&vl->usage, 1);
181 INIT_LIST_HEAD(&vl->link);
182 INIT_LIST_HEAD(&vl->grave);
183 INIT_LIST_HEAD(&vl->update);
184 init_waitqueue_head(&vl->waitq);
David S. Miller39bf0942007-04-26 20:39:14 -0700185 spin_lock_init(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700186 memcpy(vl->vldb.name, name, namesz);
187 }
188
189 _leave(" = %p", vl);
190 return vl;
191}
192
193/*
194 * update record if we found it in the cache
195 */
196static int afs_vlocation_update_record(struct afs_vlocation *vl,
David Howells00d3b7a2007-04-26 15:57:07 -0700197 struct key *key,
David Howells08e0e7c2007-04-26 15:55:03 -0700198 struct afs_cache_vlocation *vldb)
199{
200 afs_voltype_t voltype;
201 afs_volid_t vid;
202 int ret;
203
204 /* try to look up a cached volume in the cell VL databases by ID */
205 _debug("Locally Cached: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
206 vl->vldb.name,
207 vl->vldb.vidmask,
208 ntohl(vl->vldb.servers[0].s_addr),
209 vl->vldb.srvtmask[0],
210 ntohl(vl->vldb.servers[1].s_addr),
211 vl->vldb.srvtmask[1],
212 ntohl(vl->vldb.servers[2].s_addr),
213 vl->vldb.srvtmask[2]);
214
215 _debug("Vids: %08x %08x %08x",
216 vl->vldb.vid[0],
217 vl->vldb.vid[1],
218 vl->vldb.vid[2]);
219
220 if (vl->vldb.vidmask & AFS_VOL_VTM_RW) {
221 vid = vl->vldb.vid[0];
222 voltype = AFSVL_RWVOL;
223 } else if (vl->vldb.vidmask & AFS_VOL_VTM_RO) {
224 vid = vl->vldb.vid[1];
225 voltype = AFSVL_ROVOL;
226 } else if (vl->vldb.vidmask & AFS_VOL_VTM_BAK) {
227 vid = vl->vldb.vid[2];
228 voltype = AFSVL_BACKVOL;
229 } else {
230 BUG();
231 vid = 0;
232 voltype = 0;
233 }
234
235 /* contact the server to make sure the volume is still available
236 * - TODO: need to handle disconnected operation here
237 */
David Howells00d3b7a2007-04-26 15:57:07 -0700238 ret = afs_vlocation_access_vl_by_id(vl, key, vid, voltype, vldb);
David Howells08e0e7c2007-04-26 15:55:03 -0700239 switch (ret) {
240 /* net error */
241 default:
242 printk(KERN_WARNING "kAFS:"
243 " failed to update volume '%s' (%x) up in '%s': %d\n",
244 vl->vldb.name, vid, vl->cell->name, ret);
245 _leave(" = %d", ret);
246 return ret;
247
248 /* pulled from local cache into memory */
249 case 0:
250 _leave(" = 0");
251 return 0;
252
253 /* uh oh... looks like the volume got deleted */
254 case -ENOMEDIUM:
255 printk(KERN_ERR "kAFS:"
256 " volume '%s' (%x) does not exist '%s'\n",
257 vl->vldb.name, vid, vl->cell->name);
258
259 /* TODO: make existing record unavailable */
260 _leave(" = %d", ret);
261 return ret;
262 }
263}
264
265/*
266 * apply the update to a VL record
267 */
268static void afs_vlocation_apply_update(struct afs_vlocation *vl,
269 struct afs_cache_vlocation *vldb)
270{
271 _debug("Done VL Lookup: %s %02x { %08x(%x) %08x(%x) %08x(%x) }",
272 vldb->name, vldb->vidmask,
273 ntohl(vldb->servers[0].s_addr), vldb->srvtmask[0],
274 ntohl(vldb->servers[1].s_addr), vldb->srvtmask[1],
275 ntohl(vldb->servers[2].s_addr), vldb->srvtmask[2]);
276
277 _debug("Vids: %08x %08x %08x",
278 vldb->vid[0], vldb->vid[1], vldb->vid[2]);
279
280 if (strcmp(vldb->name, vl->vldb.name) != 0)
281 printk(KERN_NOTICE "kAFS:"
282 " name of volume '%s' changed to '%s' on server\n",
283 vl->vldb.name, vldb->name);
284
285 vl->vldb = *vldb;
286
David Howells9b3f26c2009-04-03 16:42:41 +0100287#ifdef CONFIG_AFS_FSCACHE
288 fscache_update_cookie(vl->cache);
David Howells08e0e7c2007-04-26 15:55:03 -0700289#endif
290}
291
292/*
293 * fill in a volume location record, consulting the cache and the VL server
294 * both
295 */
David Howells00d3b7a2007-04-26 15:57:07 -0700296static int afs_vlocation_fill_in_record(struct afs_vlocation *vl,
297 struct key *key)
David Howells08e0e7c2007-04-26 15:55:03 -0700298{
299 struct afs_cache_vlocation vldb;
300 int ret;
301
302 _enter("");
303
304 ASSERTCMP(vl->valid, ==, 0);
305
306 memset(&vldb, 0, sizeof(vldb));
307
308 /* see if we have an in-cache copy (will set vl->valid if there is) */
David Howells9b3f26c2009-04-03 16:42:41 +0100309#ifdef CONFIG_AFS_FSCACHE
310 vl->cache = fscache_acquire_cookie(vl->cell->cache,
David Howells94d30ae2013-09-21 00:09:31 +0100311 &afs_vlocation_cache_index_def, vl,
312 true);
David Howells08e0e7c2007-04-26 15:55:03 -0700313#endif
314
315 if (vl->valid) {
316 /* try to update a known volume in the cell VL databases by
317 * ID as the name may have changed */
318 _debug("found in cache");
David Howells00d3b7a2007-04-26 15:57:07 -0700319 ret = afs_vlocation_update_record(vl, key, &vldb);
David Howells08e0e7c2007-04-26 15:55:03 -0700320 } else {
321 /* try to look up an unknown volume in the cell VL databases by
322 * name */
David Howells00d3b7a2007-04-26 15:57:07 -0700323 ret = afs_vlocation_access_vl_by_name(vl, key, &vldb);
David Howells08e0e7c2007-04-26 15:55:03 -0700324 if (ret < 0) {
325 printk("kAFS: failed to locate '%s' in cell '%s'\n",
326 vl->vldb.name, vl->cell->name);
327 return ret;
328 }
329 }
330
331 afs_vlocation_apply_update(vl, &vldb);
332 _leave(" = 0");
333 return 0;
334}
335
336/*
337 * queue a vlocation record for updates
338 */
Adrian Bunkc1206a22007-10-16 23:26:41 -0700339static void afs_vlocation_queue_for_updates(struct afs_vlocation *vl)
David Howells08e0e7c2007-04-26 15:55:03 -0700340{
341 struct afs_vlocation *xvl;
342
343 /* wait at least 10 minutes before updating... */
344 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
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);
361 }
362
363 list_add_tail(&vl->update, &afs_vlocation_updates);
364 spin_unlock(&afs_vlocation_updates_lock);
365}
366
367/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 * lookup volume location
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 * - 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
373 */
David Howells08e0e7c2007-04-26 15:55:03 -0700374struct afs_vlocation *afs_vlocation_lookup(struct afs_cell *cell,
David Howells00d3b7a2007-04-26 15:57:07 -0700375 struct key *key,
David Howells08e0e7c2007-04-26 15:55:03 -0700376 const char *name,
377 size_t namesz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
David Howells08e0e7c2007-04-26 15:55:03 -0700379 struct afs_vlocation *vl;
380 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
David Howells00d3b7a2007-04-26 15:57:07 -0700382 _enter("{%s},{%x},%*.*s,%zu",
383 cell->name, key_serial(key),
384 (int) namesz, (int) namesz, name, namesz);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
Adrian Bunk65511982007-11-05 14:50:57 -0800386 if (namesz >= sizeof(vl->vldb.name)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 _leave(" = -ENAMETOOLONG");
David Howells08e0e7c2007-04-26 15:55:03 -0700388 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
390
David Howells08e0e7c2007-04-26 15:55:03 -0700391 /* 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')
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 continue;
David Howells08e0e7c2007-04-26 15:55:03 -0700397 if (memcmp(vl->vldb.name, name, namesz) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 goto found_in_memory;
399 }
David Howells08e0e7c2007-04-26 15:55:03 -0700400 spin_unlock(&cell->vl_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* not in the cell's in-memory lists - create a new record */
David Howells08e0e7c2007-04-26 15:55:03 -0700403 vl = afs_vlocation_alloc(cell, name, namesz);
404 if (!vl) {
405 up_write(&cell->vl_sem);
406 return ERR_PTR(-ENOMEM);
407 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 afs_get_cell(cell);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
David Howells08e0e7c2007-04-26 15:55:03 -0700411 list_add_tail(&vl->link, &cell->vl_list);
412 vl->state = AFS_VL_CREATING;
413 up_write(&cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
David Howells08e0e7c2007-04-26 15:55:03 -0700415fill_in_record:
David Howells00d3b7a2007-04-26 15:57:07 -0700416 ret = afs_vlocation_fill_in_record(vl, key);
David Howells08e0e7c2007-04-26 15:55:03 -0700417 if (ret < 0)
418 goto error_abandon;
David S. Miller39bf0942007-04-26 20:39:14 -0700419 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700420 vl->state = AFS_VL_VALID;
David S. Miller39bf0942007-04-26 20:39:14 -0700421 spin_unlock(&vl->lock);
David Howells47051a22007-04-27 15:26:30 -0700422 wake_up(&vl->waitq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423
David Howells9b3f26c2009-04-03 16:42:41 +0100424 /* update volume entry in local cache */
425#ifdef CONFIG_AFS_FSCACHE
426 fscache_update_cookie(vl->cache);
427#endif
428
David Howells08e0e7c2007-04-26 15:55:03 -0700429 /* schedule for regular updates */
430 afs_vlocation_queue_for_updates(vl);
431 goto success;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
David Howellsec268152007-04-26 15:49:28 -0700433found_in_memory:
David Howells08e0e7c2007-04-26 15:55:03 -0700434 /* found in memory */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 _debug("found in memory");
David Howells08e0e7c2007-04-26 15:55:03 -0700436 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
David Howells08e0e7c2007-04-26 15:55:03 -0700443 up_write(&cell->vl_sem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
David Howells08e0e7c2007-04-26 15:55:03 -0700445 /* see if it was an abandoned record that we might try filling in */
David S. Miller39bf0942007-04-26 20:39:14 -0700446 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700447 while (vl->state != AFS_VL_VALID) {
448 afs_vlocation_state_t state = vl->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
David Howells08e0e7c2007-04-26 15:55:03 -0700450 _debug("invalid [state %d]", state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
David Howells47051a22007-04-27 15:26:30 -0700452 if (state == AFS_VL_NEW || state == AFS_VL_NO_VOLUME) {
David S. Miller39bf0942007-04-26 20:39:14 -0700453 vl->state = AFS_VL_CREATING;
454 spin_unlock(&vl->lock);
455 goto fill_in_record;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
David Howells08e0e7c2007-04-26 15:55:03 -0700458 /* must now wait for creation or update by someone else to
459 * complete */
460 _debug("wait");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461
David S. Miller39bf0942007-04-26 20:39:14 -0700462 spin_unlock(&vl->lock);
David Howells47051a22007-04-27 15:26:30 -0700463 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (ret < 0)
David Howells08e0e7c2007-04-26 15:55:03 -0700468 goto error;
David S. Miller39bf0942007-04-26 20:39:14 -0700469 spin_lock(&vl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
David S. Miller39bf0942007-04-26 20:39:14 -0700471 spin_unlock(&vl->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
David Howells08e0e7c2007-04-26 15:55:03 -0700473success:
David Howells9b3f26c2009-04-03 16:42:41 +0100474 _leave(" = %p", vl);
David Howells08e0e7c2007-04-26 15:55:03 -0700475 return vl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
David Howells08e0e7c2007-04-26 15:55:03 -0700477error_abandon:
David S. Miller39bf0942007-04-26 20:39:14 -0700478 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700479 vl->state = AFS_VL_NEW;
David S. Miller39bf0942007-04-26 20:39:14 -0700480 spin_unlock(&vl->lock);
David Howells47051a22007-04-27 15:26:30 -0700481 wake_up(&vl->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700482error:
483 ASSERT(vl != NULL);
484 afs_put_vlocation(vl);
485 _leave(" = %d", ret);
486 return ERR_PTR(ret);
David Howellsec268152007-04-26 15:49:28 -0700487}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489/*
David Howells08e0e7c2007-04-26 15:55:03 -0700490 * finish using a volume location record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 */
David Howells08e0e7c2007-04-26 15:55:03 -0700492void afs_put_vlocation(struct afs_vlocation *vl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
David Howells08e0e7c2007-04-26 15:55:03 -0700494 if (!vl)
495 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
David Howells08e0e7c2007-04-26 15:55:03 -0700497 _enter("%s", vl->vldb.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
David Howells08e0e7c2007-04-26 15:55:03 -0700499 ASSERTCMP(atomic_read(&vl->usage), >, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
David Howells08e0e7c2007-04-26 15:55:03 -0700501 if (likely(!atomic_dec_and_test(&vl->usage))) {
502 _leave("");
503 return;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505
David Howells08e0e7c2007-04-26 15:55:03 -0700506 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);
510 vl->time_of_death = get_seconds();
Tejun Heo0ad53ee2011-01-14 15:56:37 +0000511 queue_delayed_work(afs_wq, &afs_vlocation_reap,
512 afs_vlocation_timeout * HZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
David Howells08e0e7c2007-04-26 15:55:03 -0700514 /* 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 }
David Howells08e0e7c2007-04-26 15:55:03 -0700521 spin_unlock(&afs_vlocation_graveyard_lock);
522 _leave(" [killed?]");
David Howellsec268152007-04-26 15:49:28 -0700523}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525/*
David Howells08e0e7c2007-04-26 15:55:03 -0700526 * destroy a dead volume location record
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 */
David Howells08e0e7c2007-04-26 15:55:03 -0700528static void afs_vlocation_destroy(struct afs_vlocation *vl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529{
David Howells08e0e7c2007-04-26 15:55:03 -0700530 _enter("%p", vl);
531
David Howells9b3f26c2009-04-03 16:42:41 +0100532#ifdef CONFIG_AFS_FSCACHE
533 fscache_relinquish_cookie(vl->cache, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700534#endif
David Howells08e0e7c2007-04-26 15:55:03 -0700535 afs_put_cell(vl->cell);
536 kfree(vl);
537}
538
539/*
540 * reap dead volume location records
541 */
542static void afs_vlocation_reaper(struct work_struct *work)
543{
544 LIST_HEAD(corpses);
545 struct afs_vlocation *vl;
546 unsigned long delay, expiry;
547 time_t now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
549 _enter("");
550
David Howells08e0e7c2007-04-26 15:55:03 -0700551 now = get_seconds();
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);
Tejun Heo41f63c52012-08-03 10:30:47 -0700565 mod_delayed_work(afs_wq, &afs_vlocation_reap, delay);
David Howells08e0e7c2007-04-26 15:55:03 -0700566 break;
567 }
568
569 spin_lock(&vl->cell->vl_lock);
570 if (atomic_read(&vl->usage) > 0) {
571 _debug("no reap");
572 list_del_init(&vl->grave);
573 } else {
574 _debug("reap");
575 list_move_tail(&vl->grave, &corpses);
576 list_del_init(&vl->link);
577 }
578 spin_unlock(&vl->cell->vl_lock);
579 }
580
581 spin_unlock(&afs_vlocation_graveyard_lock);
582
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);
588 }
589
590 _leave("");
David Howellsec268152007-04-26 15:49:28 -0700591}
David Howells08e0e7c2007-04-26 15:55:03 -0700592
593/*
594 * initialise the VL update process
595 */
596int __init afs_vlocation_update_init(void)
597{
598 afs_vlocation_update_worker =
599 create_singlethread_workqueue("kafs_vlupdated");
600 return afs_vlocation_update_worker ? 0 : -ENOMEM;
601}
602
603/*
604 * discard all the volume location records for rmmod
605 */
David Howellsfbb3fcb2007-05-03 03:12:46 -0700606void afs_vlocation_purge(void)
David Howells08e0e7c2007-04-26 15:55:03 -0700607{
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);
Tejun Heo41f63c52012-08-03 10:30:47 -0700613 mod_delayed_work(afs_vlocation_update_worker, &afs_vlocation_update, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700614 destroy_workqueue(afs_vlocation_update_worker);
615
Tejun Heo41f63c52012-08-03 10:30:47 -0700616 mod_delayed_work(afs_wq, &afs_vlocation_reap, 0);
David Howells08e0e7c2007-04-26 15:55:03 -0700617}
618
619/*
620 * update a volume location
621 */
622static void afs_vlocation_updater(struct work_struct *work)
623{
624 struct afs_cache_vlocation vldb;
625 struct afs_vlocation *vl, *xvl;
626 time_t now;
627 long timeout;
628 int ret;
629
630 _enter("");
631
632 now = get_seconds();
633
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 }
642
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);
648 }
649
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]");
656 return;
657 }
658
659 list_del_init(&vl->update);
660 atomic_inc(&vl->usage);
661 spin_unlock(&afs_vlocation_updates_lock);
662
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;
668
David Howells00d3b7a2007-04-26 15:57:07 -0700669 ret = afs_vlocation_update_record(vl, NULL, &vldb);
David S. Miller39bf0942007-04-26 20:39:14 -0700670 spin_lock(&vl->lock);
David Howells08e0e7c2007-04-26 15:55:03 -0700671 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 }
David S. Miller39bf0942007-04-26 20:39:14 -0700683 spin_unlock(&vl->lock);
David Howells47051a22007-04-27 15:26:30 -0700684 wake_up(&vl->waitq);
David Howells08e0e7c2007-04-26 15:55:03 -0700685
686 /* and then reschedule */
687 _debug("reschedule");
688 vl->update_at = get_seconds() + afs_vlocation_update_timeout;
689
690 spin_lock(&afs_vlocation_updates_lock);
691
692 if (!list_empty(&afs_vlocation_updates)) {
693 /* next update in 10 minutes, but wait at least 1 second more
694 * than the newest record already queued so that we don't spam
695 * the VL server suddenly with lots of requests
696 */
697 xvl = list_entry(afs_vlocation_updates.prev,
698 struct afs_vlocation, update);
699 if (vl->update_at <= xvl->update_at)
700 vl->update_at = xvl->update_at + 1;
701 xvl = list_entry(afs_vlocation_updates.next,
702 struct afs_vlocation, update);
703 timeout = xvl->update_at - now;
704 if (timeout < 0)
705 timeout = 0;
706 } else {
707 timeout = afs_vlocation_update_timeout;
708 }
709
710 ASSERT(list_empty(&vl->update));
711
712 list_add_tail(&vl->update, &afs_vlocation_updates);
713
714 _debug("timeout %ld", timeout);
715 queue_delayed_work(afs_vlocation_update_worker,
716 &afs_vlocation_update, timeout * HZ);
717 spin_unlock(&afs_vlocation_updates_lock);
718 afs_put_vlocation(vl);
719}