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 if (!test_bit(ac
.index
, &ac
.alist
->probed
)) {
128 ret
= afs_vl_get_capabilities(cell
->net
, &ac
, key
);
131 clear_bit(ac
.index
, &ac
.alist
->yfs
);
132 set_bit(ac
.index
, &ac
.alist
->probed
);
133 ac
.addr
->srx_service
= ret
;
136 set_bit(ac
.index
, &ac
.alist
->yfs
);
137 set_bit(ac
.index
, &ac
.alist
->probed
);
138 ac
.addr
->srx_service
= ret
;
143 vldb
= afs_vl_get_entry_by_name_u(cell
->net
, &ac
, key
,
150 ac
.error
= afs_abort_to_error(ac
.abort_code
);
166 return ERR_PTR(afs_end_cursor(&ac
));
170 * Look up a volume in the VL server and create a candidate volume record for
173 * The volume name can be one of the following:
174 * "%[cell:]volume[.]" R/W volume
175 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
176 * or R/W (rwparent=1) volume
177 * "%[cell:]volume.readonly" R/O volume
178 * "#[cell:]volume.readonly" R/O volume
179 * "%[cell:]volume.backup" Backup volume
180 * "#[cell:]volume.backup" Backup volume
182 * The cell name is optional, and defaults to the current cell.
184 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
186 * - Rule 1: Explicit type suffix forces access of that type or nothing
187 * (no suffix, then use Rule 2 & 3)
188 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
190 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
191 * explicitly told otherwise
193 struct afs_volume
*afs_create_volume(struct afs_mount_params
*params
)
195 struct afs_vldb_entry
*vldb
;
196 struct afs_volume
*volume
;
197 unsigned long type_mask
= 1UL << params
->type
;
199 vldb
= afs_vl_lookup_vldb(params
->cell
, params
->key
,
200 params
->volname
, params
->volnamesz
);
202 return ERR_CAST(vldb
);
204 if (test_bit(AFS_VLDB_QUERY_ERROR
, &vldb
->flags
)) {
205 volume
= ERR_PTR(vldb
->error
);
209 /* Make the final decision on the type we want */
210 volume
= ERR_PTR(-ENOMEDIUM
);
212 if (!(vldb
->flags
& type_mask
))
214 } else if (test_bit(AFS_VLDB_HAS_RO
, &vldb
->flags
)) {
215 params
->type
= AFSVL_ROVOL
;
216 } else if (test_bit(AFS_VLDB_HAS_RW
, &vldb
->flags
)) {
217 params
->type
= AFSVL_RWVOL
;
222 type_mask
= 1UL << params
->type
;
223 volume
= afs_alloc_volume(params
, vldb
, type_mask
);
231 * Destroy a volume record
233 static void afs_destroy_volume(struct afs_net
*net
, struct afs_volume
*volume
)
235 _enter("%p", volume
);
237 #ifdef CONFIG_AFS_FSCACHE
238 ASSERTCMP(volume
->cache
, ==, NULL
);
241 afs_put_serverlist(net
, volume
->servers
);
242 afs_put_cell(net
, volume
->cell
);
245 _leave(" [destroyed]");
249 * Drop a reference on a volume record.
251 void afs_put_volume(struct afs_cell
*cell
, struct afs_volume
*volume
)
254 _enter("%s", volume
->name
);
256 if (atomic_dec_and_test(&volume
->usage
))
257 afs_destroy_volume(cell
->net
, volume
);
264 void afs_activate_volume(struct afs_volume
*volume
)
266 #ifdef CONFIG_AFS_FSCACHE
267 volume
->cache
= fscache_acquire_cookie(volume
->cell
->cache
,
268 &afs_volume_cache_index_def
,
272 write_lock(&volume
->cell
->proc_lock
);
273 list_add_tail(&volume
->proc_link
, &volume
->cell
->proc_volumes
);
274 write_unlock(&volume
->cell
->proc_lock
);
278 * Deactivate a volume.
280 void afs_deactivate_volume(struct afs_volume
*volume
)
282 _enter("%s", volume
->name
);
284 write_lock(&volume
->cell
->proc_lock
);
285 list_del_init(&volume
->proc_link
);
286 write_unlock(&volume
->cell
->proc_lock
);
288 #ifdef CONFIG_AFS_FSCACHE
289 fscache_relinquish_cookie(volume
->cache
,
290 test_bit(AFS_VOLUME_DELETED
, &volume
->flags
));
291 volume
->cache
= NULL
;
298 * Query the VL service to update the volume status.
300 static int afs_update_volume_status(struct afs_volume
*volume
, struct key
*key
)
302 struct afs_server_list
*new, *old
, *discard
;
303 struct afs_vldb_entry
*vldb
;
309 /* We look up an ID by passing it as a decimal string in the
310 * operation's name parameter.
312 idsz
= sprintf(idbuf
, "%u", volume
->vid
);
314 vldb
= afs_vl_lookup_vldb(volume
->cell
, key
, idbuf
, idsz
);
320 /* See if the volume got renamed. */
321 if (vldb
->name_len
!= volume
->name_len
||
322 memcmp(vldb
->name
, volume
->name
, vldb
->name_len
) != 0) {
323 /* TODO: Use RCU'd string. */
324 memcpy(volume
->name
, vldb
->name
, AFS_MAXVOLNAME
);
325 volume
->name_len
= vldb
->name_len
;
328 /* See if the volume's server list got updated. */
329 new = afs_alloc_server_list(volume
->cell
, key
,
330 vldb
, (1 << volume
->type
));
336 write_lock(&volume
->servers_lock
);
339 old
= volume
->servers
;
340 if (afs_annotate_server_list(new, old
)) {
341 new->seq
= volume
->servers_seq
+ 1;
342 volume
->servers
= new;
344 volume
->servers_seq
++;
348 volume
->update_at
= ktime_get_real_seconds() + afs_volume_record_life
;
349 clear_bit(AFS_VOLUME_NEEDS_UPDATE
, &volume
->flags
);
350 write_unlock(&volume
->servers_lock
);
353 afs_put_serverlist(volume
->cell
->net
, discard
);
357 _leave(" = %d", ret
);
362 * Make sure the volume record is up to date.
364 int afs_check_volume_status(struct afs_volume
*volume
, struct key
*key
)
366 time64_t now
= ktime_get_real_seconds();
367 int ret
, retries
= 0;
371 if (volume
->update_at
<= now
)
372 set_bit(AFS_VOLUME_NEEDS_UPDATE
, &volume
->flags
);
375 if (!test_bit(AFS_VOLUME_NEEDS_UPDATE
, &volume
->flags
) &&
376 !test_bit(AFS_VOLUME_WAIT
, &volume
->flags
)) {
381 if (!test_and_set_bit_lock(AFS_VOLUME_UPDATING
, &volume
->flags
)) {
382 ret
= afs_update_volume_status(volume
, key
);
383 clear_bit_unlock(AFS_VOLUME_WAIT
, &volume
->flags
);
384 clear_bit_unlock(AFS_VOLUME_UPDATING
, &volume
->flags
);
385 wake_up_bit(&volume
->flags
, AFS_VOLUME_WAIT
);
386 _leave(" = %d", ret
);
390 if (!test_bit(AFS_VOLUME_WAIT
, &volume
->flags
)) {
391 _leave(" = 0 [no wait]");
395 ret
= wait_on_bit(&volume
->flags
, AFS_VOLUME_WAIT
, TASK_INTERRUPTIBLE
);
396 if (ret
== -ERESTARTSYS
) {
397 _leave(" = %d", ret
);
403 _leave(" = -ESTALE");