1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Filesystem index definition
3  *
4  * Copyright (C) 2004-2007 Red Hat, Inc. All Rights Reserved.
5  * Written by David Howells (dhowells@redhat.com)
6  */
7 
8 #define FSCACHE_DEBUG_LEVEL CACHE
9 #include <linux/module.h>
10 #include "internal.h"
11 
12 static
13 enum fscache_checkaux fscache_fsdef_netfs_check_aux(void *cookie_netfs_data,
14 						    const void *data,
15 						    uint16_t datalen,
16 						    loff_t object_size);
17 
18 /*
19  * The root index is owned by FS-Cache itself.
20  *
21  * When a netfs requests caching facilities, FS-Cache will, if one doesn't
22  * already exist, create an entry in the root index with the key being the name
23  * of the netfs ("AFS" for example), and the auxiliary data holding the index
24  * structure version supplied by the netfs:
25  *
26  *				     FSDEF
27  *				       |
28  *				 +-----------+
29  *				 |           |
30  *				NFS         AFS
31  *			       [v=1]       [v=1]
32  *
33  * If an entry with the appropriate name does already exist, the version is
34  * compared.  If the version is different, the entire subtree from that entry
35  * will be discarded and a new entry created.
36  *
37  * The new entry will be an index, and a cookie referring to it will be passed
38  * to the netfs.  This is then the root handle by which the netfs accesses the
39  * cache.  It can create whatever objects it likes in that index, including
40  * further indices.
41  */
42 static struct fscache_cookie_def fscache_fsdef_index_def = {
43 	.name		= ".FS-Cache",
44 	.type		= FSCACHE_COOKIE_TYPE_INDEX,
45 };
46 
47 struct fscache_cookie fscache_fsdef_index = {
48 	.debug_id	= 1,
49 	.ref		= REFCOUNT_INIT(1),
50 	.n_active	= ATOMIC_INIT(1),
51 	.lock		= __SPIN_LOCK_UNLOCKED(fscache_fsdef_index.lock),
52 	.backing_objects = HLIST_HEAD_INIT,
53 	.def		= &fscache_fsdef_index_def,
54 	.flags		= 1 << FSCACHE_COOKIE_ENABLED,
55 	.type		= FSCACHE_COOKIE_TYPE_INDEX,
56 };
57 EXPORT_SYMBOL(fscache_fsdef_index);
58 
59 /*
60  * Definition of an entry in the root index.  Each entry is an index, keyed to
61  * a specific netfs and only applicable to a particular version of the index
62  * structure used by that netfs.
63  */
64 struct fscache_cookie_def fscache_fsdef_netfs_def = {
65 	.name		= "FSDEF.netfs",
66 	.type		= FSCACHE_COOKIE_TYPE_INDEX,
67 	.check_aux	= fscache_fsdef_netfs_check_aux,
68 };
69 
70 /*
71  * check that the index structure version number stored in the auxiliary data
72  * matches the one the netfs gave us
73  */
fscache_fsdef_netfs_check_aux(void * cookie_netfs_data,const void * data,uint16_t datalen,loff_t object_size)74 static enum fscache_checkaux fscache_fsdef_netfs_check_aux(
75 	void *cookie_netfs_data,
76 	const void *data,
77 	uint16_t datalen,
78 	loff_t object_size)
79 {
80 	struct fscache_netfs *netfs = cookie_netfs_data;
81 	uint32_t version;
82 
83 	_enter("{%s},,%hu", netfs->name, datalen);
84 
85 	if (datalen != sizeof(version)) {
86 		_leave(" = OBSOLETE [dl=%d v=%zu]", datalen, sizeof(version));
87 		return FSCACHE_CHECKAUX_OBSOLETE;
88 	}
89 
90 	memcpy(&version, data, sizeof(version));
91 	if (version != netfs->version) {
92 		_leave(" = OBSOLETE [ver=%x net=%x]", version, netfs->version);
93 		return FSCACHE_CHECKAUX_OBSOLETE;
94 	}
95 
96 	_leave(" = OKAY");
97 	return FSCACHE_CHECKAUX_OKAY;
98 }
99