xref: /openbmc/linux/fs/afs/volume.c (revision bf070bb0)
1 /* AFS volume management
2  *
3  * Copyright (C) 2002, 2007 Red Hat, Inc. All Rights Reserved.
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/slab.h>
14 #include "internal.h"
15 
16 unsigned __read_mostly afs_volume_gc_delay = 10;
17 unsigned __read_mostly afs_volume_record_life = 60 * 60;
18 
19 static const char *const afs_voltypes[] = { "R/W", "R/O", "BAK" };
20 
21 /*
22  * Allocate a volume record and load it up from a vldb record.
23  */
24 static struct afs_volume *afs_alloc_volume(struct afs_mount_params *params,
25 					   struct afs_vldb_entry *vldb,
26 					   unsigned long type_mask)
27 {
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;
32 
33 	for (i = 0; i < vldb->nr_servers; i++)
34 		if (vldb->fs_mask[i] & type_mask)
35 			nr_servers++;
36 
37 	volume = kzalloc(sizeof(struct afs_volume), GFP_KERNEL);
38 	if (!volume)
39 		goto error_0;
40 
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;
47 
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);
52 
53 	slist = afs_alloc_server_list(params->cell, params->key, vldb, type_mask);
54 	if (IS_ERR(slist)) {
55 		ret = PTR_ERR(slist);
56 		goto error_1;
57 	}
58 
59 	refcount_set(&slist->usage, 1);
60 	volume->servers = slist;
61 
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))
65 			continue;
66 
67 		server = afs_lookup_server(params->cell, params->key,
68 					   &vldb->fs_server[i]);
69 		if (IS_ERR(server)) {
70 			ret = PTR_ERR(server);
71 			if (ret == -ENOENT)
72 				continue;
73 			goto error_2;
74 		}
75 
76 		/* Insertion-sort by server pointer */
77 		for (j = 0; j < slist->nr_servers; j++)
78 			if (slist->servers[j].server >= server)
79 				break;
80 		if (j < slist->nr_servers) {
81 			if (slist->servers[j].server == server) {
82 				afs_put_server(params->net, server);
83 				continue;
84 			}
85 
86 			memmove(slist->servers + j + 1,
87 				slist->servers + j,
88 				(slist->nr_servers - j) * sizeof(struct afs_server_entry));
89 		}
90 
91 		slist->servers[j].server = server;
92 		slist->nr_servers++;
93 	}
94 
95 	if (slist->nr_servers == 0) {
96 		ret = -EDESTADDRREQ;
97 		goto error_2;
98 	}
99 
100 	return volume;
101 
102 error_2:
103 	afs_put_serverlist(params->net, slist);
104 error_1:
105 	kfree(volume);
106 error_0:
107 	return ERR_PTR(ret);
108 }
109 
110 /*
111  * Look up a VLDB record for a volume.
112  */
113 static struct afs_vldb_entry *afs_vl_lookup_vldb(struct afs_cell *cell,
114 						 struct key *key,
115 						 const char *volname,
116 						 size_t volnamesz)
117 {
118 	struct afs_addr_cursor ac;
119 	struct afs_vldb_entry *vldb;
120 	int ret;
121 
122 	ret = afs_set_vl_cursor(&ac, cell);
123 	if (ret < 0)
124 		return ERR_PTR(ret);
125 
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);
129 			switch (ret) {
130 			case VL_SERVICE:
131 				clear_bit(ac.index, &ac.alist->yfs);
132 				set_bit(ac.index, &ac.alist->probed);
133 				ac.addr->srx_service = ret;
134 				break;
135 			case YFS_VL_SERVICE:
136 				set_bit(ac.index, &ac.alist->yfs);
137 				set_bit(ac.index, &ac.alist->probed);
138 				ac.addr->srx_service = ret;
139 				break;
140 			}
141 		}
142 
143 		vldb = afs_vl_get_entry_by_name_u(cell->net, &ac, key,
144 						  volname, volnamesz);
145 		switch (ac.error) {
146 		case 0:
147 			afs_end_cursor(&ac);
148 			return vldb;
149 		case -ECONNABORTED:
150 			ac.error = afs_abort_to_error(ac.abort_code);
151 			goto error;
152 		case -ENOMEM:
153 		case -ENONET:
154 			goto error;
155 		case -ENETUNREACH:
156 		case -EHOSTUNREACH:
157 		case -ECONNREFUSED:
158 			break;
159 		default:
160 			ac.error = -EIO;
161 			goto error;
162 		}
163 	}
164 
165 error:
166 	return ERR_PTR(afs_end_cursor(&ac));
167 }
168 
169 /*
170  * Look up a volume in the VL server and create a candidate volume record for
171  * it.
172  *
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
181  *
182  * The cell name is optional, and defaults to the current cell.
183  *
184  * See "The Rules of Mount Point Traversal" in Chapter 5 of the AFS SysAdmin
185  * Guide
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
189  *           if not available
190  * - Rule 3: If parent volume is R/W, then only mount R/W volume unless
191  *           explicitly told otherwise
192  */
193 struct afs_volume *afs_create_volume(struct afs_mount_params *params)
194 {
195 	struct afs_vldb_entry *vldb;
196 	struct afs_volume *volume;
197 	unsigned long type_mask = 1UL << params->type;
198 
199 	vldb = afs_vl_lookup_vldb(params->cell, params->key,
200 				  params->volname, params->volnamesz);
201 	if (IS_ERR(vldb))
202 		return ERR_CAST(vldb);
203 
204 	if (test_bit(AFS_VLDB_QUERY_ERROR, &vldb->flags)) {
205 		volume = ERR_PTR(vldb->error);
206 		goto error;
207 	}
208 
209 	/* Make the final decision on the type we want */
210 	volume = ERR_PTR(-ENOMEDIUM);
211 	if (params->force) {
212 		if (!(vldb->flags & type_mask))
213 			goto error;
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;
218 	} else {
219 		goto error;
220 	}
221 
222 	type_mask = 1UL << params->type;
223 	volume = afs_alloc_volume(params, vldb, type_mask);
224 
225 error:
226 	kfree(vldb);
227 	return volume;
228 }
229 
230 /*
231  * Destroy a volume record
232  */
233 static void afs_destroy_volume(struct afs_net *net, struct afs_volume *volume)
234 {
235 	_enter("%p", volume);
236 
237 #ifdef CONFIG_AFS_FSCACHE
238 	ASSERTCMP(volume->cache, ==, NULL);
239 #endif
240 
241 	afs_put_serverlist(net, volume->servers);
242 	afs_put_cell(net, volume->cell);
243 	kfree(volume);
244 
245 	_leave(" [destroyed]");
246 }
247 
248 /*
249  * Drop a reference on a volume record.
250  */
251 void afs_put_volume(struct afs_cell *cell, struct afs_volume *volume)
252 {
253 	if (volume) {
254 		_enter("%s", volume->name);
255 
256 		if (atomic_dec_and_test(&volume->usage))
257 			afs_destroy_volume(cell->net, volume);
258 	}
259 }
260 
261 /*
262  * Activate a volume.
263  */
264 void afs_activate_volume(struct afs_volume *volume)
265 {
266 #ifdef CONFIG_AFS_FSCACHE
267 	volume->cache = fscache_acquire_cookie(volume->cell->cache,
268 					       &afs_volume_cache_index_def,
269 					       volume, true);
270 #endif
271 
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);
275 }
276 
277 /*
278  * Deactivate a volume.
279  */
280 void afs_deactivate_volume(struct afs_volume *volume)
281 {
282 	_enter("%s", volume->name);
283 
284 	write_lock(&volume->cell->proc_lock);
285 	list_del_init(&volume->proc_link);
286 	write_unlock(&volume->cell->proc_lock);
287 
288 #ifdef CONFIG_AFS_FSCACHE
289 	fscache_relinquish_cookie(volume->cache,
290 				  test_bit(AFS_VOLUME_DELETED, &volume->flags));
291 	volume->cache = NULL;
292 #endif
293 
294 	_leave("");
295 }
296 
297 /*
298  * Query the VL service to update the volume status.
299  */
300 static int afs_update_volume_status(struct afs_volume *volume, struct key *key)
301 {
302 	struct afs_server_list *new, *old, *discard;
303 	struct afs_vldb_entry *vldb;
304 	char idbuf[16];
305 	int ret, idsz;
306 
307 	_enter("");
308 
309 	/* We look up an ID by passing it as a decimal string in the
310 	 * operation's name parameter.
311 	 */
312 	idsz = sprintf(idbuf, "%u", volume->vid);
313 
314 	vldb = afs_vl_lookup_vldb(volume->cell, key, idbuf, idsz);
315 	if (IS_ERR(vldb)) {
316 		ret = PTR_ERR(vldb);
317 		goto error;
318 	}
319 
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;
326 	}
327 
328 	/* See if the volume's server list got updated. */
329 	new = afs_alloc_server_list(volume->cell, key,
330 				      vldb, (1 << volume->type));
331 	if (IS_ERR(new)) {
332 		ret = PTR_ERR(new);
333 		goto error_vldb;
334 	}
335 
336 	write_lock(&volume->servers_lock);
337 
338 	discard = new;
339 	old = volume->servers;
340 	if (afs_annotate_server_list(new, old)) {
341 		new->seq = volume->servers_seq + 1;
342 		volume->servers = new;
343 		smp_wmb();
344 		volume->servers_seq++;
345 		discard = old;
346 	}
347 
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);
351 	ret = 0;
352 
353 	afs_put_serverlist(volume->cell->net, discard);
354 error_vldb:
355 	kfree(vldb);
356 error:
357 	_leave(" = %d", ret);
358 	return ret;
359 }
360 
361 /*
362  * Make sure the volume record is up to date.
363  */
364 int afs_check_volume_status(struct afs_volume *volume, struct key *key)
365 {
366 	time64_t now = ktime_get_real_seconds();
367 	int ret, retries = 0;
368 
369 	_enter("");
370 
371 	if (volume->update_at <= now)
372 		set_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags);
373 
374 retry:
375 	if (!test_bit(AFS_VOLUME_NEEDS_UPDATE, &volume->flags) &&
376 	    !test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
377 		_leave(" = 0");
378 		return 0;
379 	}
380 
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);
387 		return ret;
388 	}
389 
390 	if (!test_bit(AFS_VOLUME_WAIT, &volume->flags)) {
391 		_leave(" = 0 [no wait]");
392 		return 0;
393 	}
394 
395 	ret = wait_on_bit(&volume->flags, AFS_VOLUME_WAIT, TASK_INTERRUPTIBLE);
396 	if (ret == -ERESTARTSYS) {
397 		_leave(" = %d", ret);
398 		return ret;
399 	}
400 
401 	retries++;
402 	if (retries == 4) {
403 		_leave(" = -ESTALE");
404 		return -ESTALE;
405 	}
406 	goto retry;
407 }
408