1 /* AFS volume management
3 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
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.
12 #include <linux/kernel.h>
13 #include <linux/slab.h>
16 unsigned __read_mostly afs_volume_gc_delay = 10;
17 unsigned __read_mostly afs_volume_record_life = 60 * 60;
19 static const char *const afs_voltypes[] = { "R/W", "R/O", "BAK" };
22 * Allocate a volume record and load it up from a vldb record.
24 static struct afs_volume *afs_alloc_volume(struct afs_mount_params *params,
25 struct afs_vldb_entry *vldb,
26 unsigned long type_mask)
28 struct afs_server_list *slist;
29 struct afs_server *server;
30 struct afs_volume *volume;
31 int ret = -ENOMEM, nr_servers = 0, i, j;
33 for (i = 0; i < vldb->nr_servers; i++)
34 if (vldb->fs_mask[i] & type_mask)
37 volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
41 volume->vid = vldb->vid[params->type];
42 volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
43 volume->cell = afs_get_cell(params->cell);
44 volume->type = params->type;
45 volume->type_force = params->force;
46 volume->name_len = vldb->name_len;
48 atomic_set(&volume->usage, 1);
49 INIT_LIST_HEAD(&volume->proc_link);
50 rwlock_init(&volume->servers_lock);
51 memcpy(volume->name, vldb->name, vldb->name_len + 1);
53 slist = afs_alloc_server_list(params->cell, params->key, vldb, type_mask);
59 refcount_set(&slist->usage, 1);
60 volume->servers = slist;
62 /* Make sure a records exists for each server this volume occupies. */
63 for (i = 0; i < nr_servers; i++) {
64 if (!(vldb->fs_mask[i] & type_mask))
67 server = afs_lookup_server(params->cell, params->key,
70 ret = PTR_ERR(server);
76 /* Insertion-sort by server pointer */
77 for (j = 0; j < slist->nr_servers; j++)
78 if (slist->servers[j].server >= server)
80 if (j < slist->nr_servers) {
81 if (slist->servers[j].server == server) {
82 afs_put_server(params->net, server);
86 memmove(slist->servers + j + 1,
88 (slist->nr_servers - j) * sizeof(struct afs_server_entry));
91 slist->servers[j].server = server;
95 if (slist->nr_servers == 0) {
103 afs_put_serverlist(params->net, slist);
111 * Look up a VLDB record for a volume.
113 static struct afs_vldb_entry *afs_vl_lookup_vldb(struct afs_cell *cell,
118 struct afs_addr_cursor ac;
119 struct afs_vldb_entry *vldb;
122 ret = afs_set_vl_cursor(&ac, cell);
126 while (afs_iterate_addresses(&ac)) {
127 vldb = afs_vl_get_entry_by_name_u(cell->net, &ac, key,
134 ac.error = afs_abort_to_error(ac.abort_code);
150 return ERR_PTR(afs_end_cursor(&ac));
154 * Look up a volume in the VL server and create a candidate volume record for
157 * The volume name can be one of the following:
158 * "%[cell:]volume[.]" R/W volume
159 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
160 * or R/W (rwparent=1) volume
161 * "%[cell:]volume.readonly" R/O volume
162 * "#[cell:]volume.readonly" R/O volume
163 * "%[cell:]volume.backup" Backup volume
164 * "#[cell:]volume.backup" Backup volume
166 * The cell name is optional, and defaults to the current cell.
168 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
170 * - Rule 1: Explicit type suffix forces access of that type or nothing
171 * (no suffix, then use Rule 2 & 3)
172 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
174 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
175 * explicitly told otherwise
177 struct afs_volume *afs_create_volume(struct afs_mount_params *params)
179 struct afs_vldb_entry *vldb;
180 struct afs_volume *volume;
181 unsigned long type_mask = 1UL << params->type;
183 vldb = afs_vl_lookup_vldb(params->cell, params->key,
184 params->volname, params->volnamesz);
186 return ERR_CAST(vldb);
188 if (test_bit(AFS_VLDB_QUERY_ERROR, &vldb->flags)) {
189 volume = ERR_PTR(vldb->error);
193 /* Make the final decision on the type we want */
194 volume = ERR_PTR(-ENOMEDIUM);
196 if (!(vldb->flags & type_mask))
198 } else if (test_bit(AFS_VLDB_HAS_RO, &vldb->flags)) {
199 params->type = AFSVL_ROVOL;
200 } else if (test_bit(AFS_VLDB_HAS_RW, &vldb->flags)) {
201 params->type = AFSVL_RWVOL;
206 type_mask = 1UL << params->type;
207 volume = afs_alloc_volume(params, vldb, type_mask);
215 * Destroy a volume record
217 static void afs_destroy_volume(struct afs_net *net, struct afs_volume *volume)
219 _enter("%p", volume);
221 #ifdef CONFIG_AFS_FSCACHE
222 ASSERTCMP(volume->cache, ==, NULL);
225 afs_put_serverlist(net, volume->servers);
226 afs_put_cell(net, volume->cell);
229 _leave(" [destroyed]");
233 * Drop a reference on a volume record.
235 void afs_put_volume(struct afs_cell *cell, struct afs_volume *volume)
238 _enter("%s", volume->name);
240 if (atomic_dec_and_test(&volume->usage))
241 afs_destroy_volume(cell->net, volume);
248 void afs_activate_volume(struct afs_volume *volume)
250 #ifdef CONFIG_AFS_FSCACHE
251 volume->cache = fscache_acquire_cookie(volume->cell->cache,
252 &afs_volume_cache_index_def,
256 write_lock(&volume->cell->proc_lock);
257 list_add_tail(&volume->proc_link, &volume->cell->proc_volumes);
258 write_unlock(&volume->cell->proc_lock);
262 * Deactivate a volume.
264 void afs_deactivate_volume(struct afs_volume *volume)
266 _enter("%s", volume->name);
268 write_lock(&volume->cell->proc_lock);
269 list_del_init(&volume->proc_link);
270 write_unlock(&volume->cell->proc_lock);
272 #ifdef CONFIG_AFS_FSCACHE
273 fscache_relinquish_cookie(volume->cache,
274 test_bit(AFS_VOLUME_DELETED, &volume->flags));
275 volume->cache = NULL;
282 * Query the VL service to update the volume status.
284 static int afs_update_volume_status(struct afs_volume *volume, struct key *key)
286 struct afs_server_list *new, *old, *discard;
287 struct afs_vldb_entry *vldb;
293 /* We look up an ID by passing it as a decimal string in the
294 * operation's name parameter.
296 idsz = sprintf(idbuf, "%u", volume->vid);
298 vldb = afs_vl_lookup_vldb(volume->cell, key, idbuf, idsz);
304 /* See if the volume got renamed. */
305 if (vldb->name_len != volume->name_len ||
306 memcmp(vldb->name, volume->name, vldb->name_len) != 0) {
307 /* TODO: Use RCU'd string. */
308 memcpy(volume->name, vldb->name, AFS_MAXVOLNAME);
309 volume->name_len = vldb->name_len;
312 /* See if the volume's server list got updated. */
313 new = afs_alloc_server_list(volume->cell, key,
314 vldb, (1 << volume->type));
320 write_lock(&volume->servers_lock);
323 old = volume->servers;
324 if (afs_annotate_server_list(new, old)) {
325 new->seq = volume->servers_seq + 1;
326 volume->servers = new;
328 volume->servers_seq++;
332 volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
333 clear_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
334 write_unlock(&volume->servers_lock);
337 afs_put_serverlist(volume->cell->net, discard);
341 _leave(" = %d", ret);
346 * Make sure the volume record is up to date.
348 int afs_check_volume_status(struct afs_volume *volume, struct key *key)
350 time64_t now = ktime_get_real_seconds();
351 int ret, retries = 0;
355 if (volume->update_at <= now)
356 set_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
359 if (!test_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags) &&
360 !test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
365 if (!test_and_set_bit_lock(AFS_VOLUME_UPDATING, &volume->flags)) {
366 ret = afs_update_volume_status(volume, key);
367 clear_bit_unlock(AFS_VOLUME_WAIT, &volume->flags);
368 clear_bit_unlock(AFS_VOLUME_UPDATING, &volume->flags);
369 wake_up_bit(&volume->flags, AFS_VOLUME_WAIT);
370 _leave(" = %d", ret);
374 if (!test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
375 _leave(" = 0 [no wait]");
379 ret = wait_on_bit(&volume->flags, AFS_VOLUME_WAIT, TASK_INTERRUPTIBLE);
380 if (ret == -ERESTARTSYS) {
381 _leave(" = %d", ret);
387 _leave(" = -ESTALE");