]> git.ipfire.org Git - people/ms/linux.git/blame - fs/afs/volume.c
Merge branch 'for-6.0/dax' into libnvdimm-fixes
[people/ms/linux.git] / fs / afs / volume.c
CommitLineData
2874c5fd 1// SPDX-License-Identifier: GPL-2.0-or-later
ec26815a 2/* AFS volume management
1da177e4 3 *
08e0e7c8 4 * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
1da177e4 5 * Written by David Howells (dhowells@redhat.com)
1da177e4
LT
6 */
7
8#include <linux/kernel.h>
1da177e4 9#include <linux/slab.h>
1da177e4
LT
10#include "internal.h"
11
102d8410 12static unsigned __read_mostly afs_volume_record_life = 60 * 60;
d2ddc776 13
20325960
DH
14/*
15 * Insert a volume into a cell. If there's an existing volume record, that is
16 * returned instead with a ref held.
17 */
18static struct afs_volume *afs_insert_volume_into_cell(struct afs_cell *cell,
19 struct afs_volume *volume)
20{
21 struct afs_volume *p;
22 struct rb_node *parent = NULL, **pp;
23
24 write_seqlock(&cell->volume_lock);
25
26 pp = &cell->volumes.rb_node;
27 while (*pp) {
28 parent = *pp;
29 p = rb_entry(parent, struct afs_volume, cell_node);
30 if (p->vid < volume->vid) {
31 pp = &(*pp)->rb_left;
32 } else if (p->vid > volume->vid) {
33 pp = &(*pp)->rb_right;
34 } else {
35 volume = afs_get_volume(p, afs_volume_trace_get_cell_insert);
36 goto found;
37 }
38 }
39
40 rb_link_node_rcu(&volume->cell_node, parent, pp);
41 rb_insert_color(&volume->cell_node, &cell->volumes);
42 hlist_add_head_rcu(&volume->proc_link, &cell->proc_volumes);
43
44found:
45 write_sequnlock(&cell->volume_lock);
46 return volume;
47
48}
49
50static void afs_remove_volume_from_cell(struct afs_volume *volume)
51{
52 struct afs_cell *cell = volume->cell;
53
54 if (!hlist_unhashed(&volume->proc_link)) {
c56f9ec8 55 trace_afs_volume(volume->vid, refcount_read(&cell->ref),
20325960
DH
56 afs_volume_trace_remove);
57 write_seqlock(&cell->volume_lock);
58 hlist_del_rcu(&volume->proc_link);
59 rb_erase(&volume->cell_node, &cell->volumes);
60 write_sequnlock(&cell->volume_lock);
61 }
62}
63
1da177e4 64/*
d2ddc776
DH
65 * Allocate a volume record and load it up from a vldb record.
66 */
13fcc683 67static struct afs_volume *afs_alloc_volume(struct afs_fs_context *params,
d2ddc776
DH
68 struct afs_vldb_entry *vldb,
69 unsigned long type_mask)
70{
71 struct afs_server_list *slist;
d2ddc776 72 struct afs_volume *volume;
45df8462 73 int ret = -ENOMEM, nr_servers = 0, i;
d2ddc776
DH
74
75 for (i = 0; i < vldb->nr_servers; i++)
76 if (vldb->fs_mask[i] & type_mask)
77 nr_servers++;
78
79 volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
80 if (!volume)
81 goto error_0;
82
83 volume->vid = vldb->vid[params->type];
84 volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
dca54a7b 85 volume->cell = afs_get_cell(params->cell, afs_cell_trace_get_vol);
d2ddc776
DH
86 volume->type = params->type;
87 volume->type_force = params->force;
88 volume->name_len = vldb->name_len;
89
c56f9ec8 90 refcount_set(&volume->ref, 1);
20325960 91 INIT_HLIST_NODE(&volume->proc_link);
d2ddc776 92 rwlock_init(&volume->servers_lock);
90fa9b64 93 rwlock_init(&volume->cb_v_break_lock);
d2ddc776
DH
94 memcpy(volume->name, vldb->name, vldb->name_len + 1);
95
96 slist = afs_alloc_server_list(params->cell, params->key, vldb, type_mask);
97 if (IS_ERR(slist)) {
98 ret = PTR_ERR(slist);
99 goto error_1;
100 }
101
102 refcount_set(&slist->usage, 1);
8a070a96 103 rcu_assign_pointer(volume->servers, slist);
cca37d45 104 trace_afs_volume(volume->vid, 1, afs_volume_trace_alloc);
d2ddc776
DH
105 return volume;
106
d2ddc776 107error_1:
dca54a7b 108 afs_put_cell(volume->cell, afs_cell_trace_put_vol);
d2ddc776
DH
109 kfree(volume);
110error_0:
111 return ERR_PTR(ret);
112}
113
20325960
DH
114/*
115 * Look up or allocate a volume record.
116 */
117static struct afs_volume *afs_lookup_volume(struct afs_fs_context *params,
118 struct afs_vldb_entry *vldb,
119 unsigned long type_mask)
120{
121 struct afs_volume *candidate, *volume;
122
123 candidate = afs_alloc_volume(params, vldb, type_mask);
124 if (IS_ERR(candidate))
125 return candidate;
126
127 volume = afs_insert_volume_into_cell(params->cell, candidate);
128 if (volume != candidate)
129 afs_put_volume(params->net, candidate, afs_volume_trace_put_cell_dup);
130 return volume;
131}
132
d2ddc776
DH
133/*
134 * Look up a VLDB record for a volume.
135 */
136static struct afs_vldb_entry *afs_vl_lookup_vldb(struct afs_cell *cell,
137 struct key *key,
138 const char *volname,
139 size_t volnamesz)
140{
0a5143f2
DH
141 struct afs_vldb_entry *vldb = ERR_PTR(-EDESTADDRREQ);
142 struct afs_vl_cursor vc;
d2ddc776
DH
143 int ret;
144
0a5143f2
DH
145 if (!afs_begin_vlserver_operation(&vc, cell, key))
146 return ERR_PTR(-ERESTARTSYS);
d2ddc776 147
0a5143f2 148 while (afs_select_vlserver(&vc)) {
0a5143f2 149 vldb = afs_vl_get_entry_by_name_u(&vc, volname, volnamesz);
d2ddc776
DH
150 }
151
0a5143f2
DH
152 ret = afs_end_vlserver_operation(&vc);
153 return ret < 0 ? ERR_PTR(ret) : vldb;
d2ddc776
DH
154}
155
156/*
157 * Look up a volume in the VL server and create a candidate volume record for
158 * it.
159 *
160 * The volume name can be one of the following:
1da177e4
LT
161 * "%[cell:]volume[.]" R/W volume
162 * "#[cell:]volume[.]" R/O or R/W volume (rwparent=0),
163 * or R/W (rwparent=1) volume
164 * "%[cell:]volume.readonly" R/O volume
165 * "#[cell:]volume.readonly" R/O volume
166 * "%[cell:]volume.backup" Backup volume
167 * "#[cell:]volume.backup" Backup volume
168 *
169 * The cell name is optional, and defaults to the current cell.
170 *
171 * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
172 * Guide
173 * - Rule 1: Explicit type suffix forces access of that type or nothing
174 * (no suffix, then use Rule 2 & 3)
175 * - Rule 2: If parent volume is R/O, then mount R/O volume by preference, R/W
176 * if not available
177 * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
178 * explicitly told otherwise
179 */
13fcc683 180struct afs_volume *afs_create_volume(struct afs_fs_context *params)
1da177e4 181{
d2ddc776
DH
182 struct afs_vldb_entry *vldb;
183 struct afs_volume *volume;
184 unsigned long type_mask = 1UL << params->type;
1da177e4 185
d2ddc776
DH
186 vldb = afs_vl_lookup_vldb(params->cell, params->key,
187 params->volname, params->volnamesz);
188 if (IS_ERR(vldb))
189 return ERR_CAST(vldb);
1da177e4 190
d2ddc776
DH
191 if (test_bit(AFS_VLDB_QUERY_ERROR, &vldb->flags)) {
192 volume = ERR_PTR(vldb->error);
193 goto error;
194 }
1da177e4 195
d2ddc776
DH
196 /* Make the final decision on the type we want */
197 volume = ERR_PTR(-ENOMEDIUM);
00d3b7a4 198 if (params->force) {
d2ddc776 199 if (!(vldb->flags & type_mask))
1da177e4 200 goto error;
d2ddc776 201 } else if (test_bit(AFS_VLDB_HAS_RO, &vldb->flags)) {
00d3b7a4 202 params->type = AFSVL_ROVOL;
d2ddc776 203 } else if (test_bit(AFS_VLDB_HAS_RW, &vldb->flags)) {
00d3b7a4 204 params->type = AFSVL_RWVOL;
ec26815a 205 } else {
1da177e4
LT
206 goto error;
207 }
208
d2ddc776 209 type_mask = 1UL << params->type;
20325960 210 volume = afs_lookup_volume(params, vldb, type_mask);
1da177e4 211
d2ddc776
DH
212error:
213 kfree(vldb);
214 return volume;
215}
1da177e4 216
d2ddc776
DH
217/*
218 * Destroy a volume record
219 */
220static void afs_destroy_volume(struct afs_net *net, struct afs_volume *volume)
221{
222 _enter("%p", volume);
1da177e4 223
d2ddc776
DH
224#ifdef CONFIG_AFS_FSCACHE
225 ASSERTCMP(volume->cache, ==, NULL);
226#endif
1da177e4 227
20325960 228 afs_remove_volume_from_cell(volume);
8a070a96 229 afs_put_serverlist(net, rcu_access_pointer(volume->servers));
dca54a7b 230 afs_put_cell(volume->cell, afs_cell_trace_put_vol);
c56f9ec8 231 trace_afs_volume(volume->vid, refcount_read(&volume->ref),
cca37d45 232 afs_volume_trace_free);
20325960 233 kfree_rcu(volume, rcu);
1da177e4 234
d2ddc776
DH
235 _leave(" [destroyed]");
236}
237
238/*
cca37d45 239 * Get a reference on a volume record.
d2ddc776 240 */
cca37d45
DH
241struct afs_volume *afs_get_volume(struct afs_volume *volume,
242 enum afs_volume_trace reason)
d2ddc776
DH
243{
244 if (volume) {
c56f9ec8
DH
245 int r;
246
247 __refcount_inc(&volume->ref, &r);
248 trace_afs_volume(volume->vid, r + 1, reason);
cca37d45
DH
249 }
250 return volume;
251}
252
d2ddc776 253
cca37d45
DH
254/*
255 * Drop a reference on a volume record.
256 */
257void afs_put_volume(struct afs_net *net, struct afs_volume *volume,
258 enum afs_volume_trace reason)
259{
260 if (volume) {
261 afs_volid_t vid = volume->vid;
c56f9ec8
DH
262 bool zero;
263 int r;
264
265 zero = __refcount_dec_and_test(&volume->ref, &r);
266 trace_afs_volume(vid, r - 1, reason);
267 if (zero)
e49c7b2f 268 afs_destroy_volume(net, volume);
1da177e4 269 }
d2ddc776 270}
1da177e4 271
d2ddc776
DH
272/*
273 * Activate a volume.
274 */
523d27cd 275int afs_activate_volume(struct afs_volume *volume)
d2ddc776 276{
9b3f26c9 277#ifdef CONFIG_AFS_FSCACHE
523d27cd
DH
278 struct fscache_volume *vcookie;
279 char *name;
280
281 name = kasprintf(GFP_KERNEL, "afs,%s,%llx",
282 volume->cell->name, volume->vid);
283 if (!name)
284 return -ENOMEM;
285
286 vcookie = fscache_acquire_volume(name, NULL, NULL, 0);
287 if (IS_ERR(vcookie)) {
288 if (vcookie != ERR_PTR(-EBUSY)) {
289 kfree(name);
290 return PTR_ERR(vcookie);
291 }
292 pr_err("AFS: Cache volume key already in use (%s)\n", name);
293 vcookie = NULL;
294 }
295 volume->cache = vcookie;
296 kfree(name);
1da177e4 297#endif
523d27cd 298 return 0;
d2ddc776 299}
1da177e4 300
d2ddc776
DH
301/*
302 * Deactivate a volume.
303 */
304void afs_deactivate_volume(struct afs_volume *volume)
305{
306 _enter("%s", volume->name);
1da177e4 307
d2ddc776 308#ifdef CONFIG_AFS_FSCACHE
523d27cd 309 fscache_relinquish_volume(volume->cache, NULL,
d2ddc776
DH
310 test_bit(AFS_VOLUME_DELETED, &volume->flags));
311 volume->cache = NULL;
312#endif
1da177e4 313
d2ddc776 314 _leave("");
ec26815a 315}
1da177e4 316
1da177e4 317/*
d2ddc776 318 * Query the VL service to update the volume status.
1da177e4 319 */
d2ddc776 320static int afs_update_volume_status(struct afs_volume *volume, struct key *key)
1da177e4 321{
d2ddc776
DH
322 struct afs_server_list *new, *old, *discard;
323 struct afs_vldb_entry *vldb;
324 char idbuf[16];
325 int ret, idsz;
1da177e4 326
d2ddc776 327 _enter("");
1da177e4 328
d2ddc776
DH
329 /* We look up an ID by passing it as a decimal string in the
330 * operation's name parameter.
331 */
3b6492df 332 idsz = sprintf(idbuf, "%llu", volume->vid);
1da177e4 333
d2ddc776
DH
334 vldb = afs_vl_lookup_vldb(volume->cell, key, idbuf, idsz);
335 if (IS_ERR(vldb)) {
336 ret = PTR_ERR(vldb);
337 goto error;
338 }
1da177e4 339
d2ddc776
DH
340 /* See if the volume got renamed. */
341 if (vldb->name_len != volume->name_len ||
342 memcmp(vldb->name, volume->name, vldb->name_len) != 0) {
343 /* TODO: Use RCU'd string. */
344 memcpy(volume->name, vldb->name, AFS_MAXVOLNAME);
345 volume->name_len = vldb->name_len;
346 }
347
348 /* See if the volume's server list got updated. */
349 new = afs_alloc_server_list(volume->cell, key,
45df8462 350 vldb, (1 << volume->type));
d2ddc776
DH
351 if (IS_ERR(new)) {
352 ret = PTR_ERR(new);
353 goto error_vldb;
354 }
1da177e4 355
d2ddc776 356 write_lock(&volume->servers_lock);
1da177e4 357
d2ddc776 358 discard = new;
8a070a96
DH
359 old = rcu_dereference_protected(volume->servers,
360 lockdep_is_held(&volume->servers_lock));
d2ddc776
DH
361 if (afs_annotate_server_list(new, old)) {
362 new->seq = volume->servers_seq + 1;
8a070a96 363 rcu_assign_pointer(volume->servers, new);
d2ddc776
DH
364 smp_wmb();
365 volume->servers_seq++;
366 discard = old;
1da177e4
LT
367 }
368
d2ddc776 369 volume->update_at = ktime_get_real_seconds() + afs_volume_record_life;
d2ddc776
DH
370 write_unlock(&volume->servers_lock);
371 ret = 0;
1da177e4 372
d2ddc776
DH
373 afs_put_serverlist(volume->cell->net, discard);
374error_vldb:
375 kfree(vldb);
376error:
377 _leave(" = %d", ret);
378 return ret;
379}
1da177e4 380
d2ddc776
DH
381/*
382 * Make sure the volume record is up to date.
383 */
e49c7b2f 384int afs_check_volume_status(struct afs_volume *volume, struct afs_operation *op)
d2ddc776 385{
d2ddc776 386 int ret, retries = 0;
1da177e4 387
d2ddc776
DH
388 _enter("");
389
d2ddc776 390retry:
f6cbb368
DH
391 if (test_bit(AFS_VOLUME_WAIT, &volume->flags))
392 goto wait;
393 if (volume->update_at <= ktime_get_real_seconds() ||
394 test_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags))
395 goto update;
396 _leave(" = 0");
397 return 0;
398
399update:
d2ddc776 400 if (!test_and_set_bit_lock(AFS_VOLUME_UPDATING, &volume->flags)) {
f6cbb368 401 clear_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
e49c7b2f 402 ret = afs_update_volume_status(volume, op->key);
f6cbb368
DH
403 if (ret < 0)
404 set_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
d2ddc776
DH
405 clear_bit_unlock(AFS_VOLUME_WAIT, &volume->flags);
406 clear_bit_unlock(AFS_VOLUME_UPDATING, &volume->flags);
407 wake_up_bit(&volume->flags, AFS_VOLUME_WAIT);
408 _leave(" = %d", ret);
409 return ret;
410 }
1da177e4 411
f6cbb368 412wait:
d2ddc776
DH
413 if (!test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
414 _leave(" = 0 [no wait]");
415 return 0;
416 }
417
c4bfda16 418 ret = wait_on_bit(&volume->flags, AFS_VOLUME_WAIT,
e49c7b2f
DH
419 (op->flags & AFS_OPERATION_UNINTR) ?
420 TASK_UNINTERRUPTIBLE : TASK_INTERRUPTIBLE);
d2ddc776
DH
421 if (ret == -ERESTARTSYS) {
422 _leave(" = %d", ret);
423 return ret;
424 }
425
426 retries++;
427 if (retries == 4) {
428 _leave(" = -ESTALE");
429 return -ESTALE;
430 }
431 goto retry;
ec26815a 432}