David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 1 | /* AFS caching stuff |
| 2 | * |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 3 | * Copyright (C) 2008 Red Hat, Inc. All Rights Reserved. |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 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 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 12 | #include <linux/sched.h> |
| 13 | #include "internal.h" |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 14 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 15 | static uint16_t afs_cell_cache_get_key(const void *cookie_netfs_data, |
| 16 | void *buffer, uint16_t buflen); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 17 | static uint16_t afs_volume_cache_get_key(const void *cookie_netfs_data, |
| 18 | void *buffer, uint16_t buflen); |
| 19 | |
| 20 | static uint16_t afs_vnode_cache_get_key(const void *cookie_netfs_data, |
| 21 | void *buffer, uint16_t buflen); |
| 22 | static void afs_vnode_cache_get_attr(const void *cookie_netfs_data, |
| 23 | uint64_t *size); |
| 24 | static uint16_t afs_vnode_cache_get_aux(const void *cookie_netfs_data, |
| 25 | void *buffer, uint16_t buflen); |
| 26 | static enum fscache_checkaux afs_vnode_cache_check_aux(void *cookie_netfs_data, |
| 27 | const void *buffer, |
| 28 | uint16_t buflen); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 29 | |
| 30 | struct fscache_netfs afs_cache_netfs = { |
| 31 | .name = "afs", |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 32 | .version = 1, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 33 | }; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 34 | |
| 35 | struct fscache_cookie_def afs_cell_cache_index_def = { |
| 36 | .name = "AFS.cell", |
| 37 | .type = FSCACHE_COOKIE_TYPE_INDEX, |
| 38 | .get_key = afs_cell_cache_get_key, |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | struct fscache_cookie_def afs_volume_cache_index_def = { |
| 42 | .name = "AFS.volume", |
| 43 | .type = FSCACHE_COOKIE_TYPE_INDEX, |
| 44 | .get_key = afs_volume_cache_get_key, |
| 45 | }; |
| 46 | |
| 47 | struct fscache_cookie_def afs_vnode_cache_index_def = { |
| 48 | .name = "AFS.vnode", |
| 49 | .type = FSCACHE_COOKIE_TYPE_DATAFILE, |
| 50 | .get_key = afs_vnode_cache_get_key, |
| 51 | .get_attr = afs_vnode_cache_get_attr, |
| 52 | .get_aux = afs_vnode_cache_get_aux, |
| 53 | .check_aux = afs_vnode_cache_check_aux, |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 54 | }; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 55 | |
| 56 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 57 | * set the key for the index entry |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 58 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 59 | static uint16_t afs_cell_cache_get_key(const void *cookie_netfs_data, |
| 60 | void *buffer, uint16_t bufmax) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 61 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 62 | const struct afs_cell *cell = cookie_netfs_data; |
| 63 | uint16_t klen; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 64 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 65 | _enter("%p,%p,%u", cell, buffer, bufmax); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 66 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 67 | klen = strlen(cell->name); |
| 68 | if (klen > bufmax) |
| 69 | return 0; |
| 70 | |
| 71 | memcpy(buffer, cell->name, klen); |
| 72 | return klen; |
| 73 | } |
| 74 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 75 | /*****************************************************************************/ |
| 76 | /* |
| 77 | * set the key for the volume index entry |
| 78 | */ |
| 79 | static uint16_t afs_volume_cache_get_key(const void *cookie_netfs_data, |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 80 | void *buffer, uint16_t bufmax) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 81 | { |
| 82 | const struct afs_volume *volume = cookie_netfs_data; |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 83 | struct { |
| 84 | u64 volid; |
| 85 | } __packed key; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 86 | |
| 87 | _enter("{%u},%p,%u", volume->type, buffer, bufmax); |
| 88 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 89 | if (bufmax < sizeof(key)) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 90 | return 0; |
| 91 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 92 | key.volid = volume->vid; |
| 93 | memcpy(buffer, &key, sizeof(key)); |
| 94 | return sizeof(key); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | /*****************************************************************************/ |
| 98 | /* |
| 99 | * set the key for the index entry |
| 100 | */ |
| 101 | static uint16_t afs_vnode_cache_get_key(const void *cookie_netfs_data, |
| 102 | void *buffer, uint16_t bufmax) |
| 103 | { |
| 104 | const struct afs_vnode *vnode = cookie_netfs_data; |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 105 | struct { |
| 106 | u32 vnode_id[3]; |
| 107 | } __packed key; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 108 | |
| 109 | _enter("{%x,%x,%llx},%p,%u", |
| 110 | vnode->fid.vnode, vnode->fid.unique, vnode->status.data_version, |
| 111 | buffer, bufmax); |
| 112 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 113 | /* Allow for a 96-bit key */ |
| 114 | memset(&key, 0, sizeof(key)); |
| 115 | key.vnode_id[0] = vnode->fid.vnode; |
| 116 | key.vnode_id[1] = 0; |
| 117 | key.vnode_id[2] = 0; |
| 118 | |
| 119 | if (sizeof(key) > bufmax) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 120 | return 0; |
| 121 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 122 | memcpy(buffer, &key, sizeof(key)); |
| 123 | return sizeof(key); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 124 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 125 | |
| 126 | /* |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 127 | * provide updated file attributes |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 128 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 129 | static void afs_vnode_cache_get_attr(const void *cookie_netfs_data, |
| 130 | uint64_t *size) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 131 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 132 | const struct afs_vnode *vnode = cookie_netfs_data; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 133 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 134 | _enter("{%x,%x,%llx},", |
| 135 | vnode->fid.vnode, vnode->fid.unique, |
| 136 | vnode->status.data_version); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 137 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 138 | *size = vnode->status.size; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 139 | } |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 140 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 141 | struct afs_vnode_cache_aux { |
| 142 | u64 data_version; |
| 143 | u32 fid_unique; |
| 144 | } __packed; |
| 145 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 146 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 147 | * provide new auxiliary cache data |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 148 | */ |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 149 | static uint16_t afs_vnode_cache_get_aux(const void *cookie_netfs_data, |
| 150 | void *buffer, uint16_t bufmax) |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 151 | { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 152 | const struct afs_vnode *vnode = cookie_netfs_data; |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 153 | struct afs_vnode_cache_aux aux; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 154 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 155 | _enter("{%x,%x,%Lx},%p,%u", |
| 156 | vnode->fid.vnode, vnode->fid.unique, vnode->status.data_version, |
| 157 | buffer, bufmax); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 158 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 159 | memset(&aux, 0, sizeof(aux)); |
| 160 | aux.data_version = vnode->status.data_version; |
| 161 | aux.fid_unique = vnode->fid.unique; |
| 162 | |
| 163 | if (bufmax < sizeof(aux)) |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 164 | return 0; |
| 165 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 166 | memcpy(buffer, &aux, sizeof(aux)); |
| 167 | return sizeof(aux); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 171 | * check that the auxiliary data indicates that the entry is still valid |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 172 | */ |
| 173 | static enum fscache_checkaux afs_vnode_cache_check_aux(void *cookie_netfs_data, |
| 174 | const void *buffer, |
| 175 | uint16_t buflen) |
| 176 | { |
| 177 | struct afs_vnode *vnode = cookie_netfs_data; |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 178 | struct afs_vnode_cache_aux aux; |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 179 | |
| 180 | _enter("{%x,%x,%llx},%p,%u", |
| 181 | vnode->fid.vnode, vnode->fid.unique, vnode->status.data_version, |
| 182 | buffer, buflen); |
| 183 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 184 | memcpy(&aux, buffer, sizeof(aux)); |
| 185 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 186 | /* check the size of the data is what we're expecting */ |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 187 | if (buflen != sizeof(aux)) { |
| 188 | _leave(" = OBSOLETE [len %hx != %zx]", buflen, sizeof(aux)); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 189 | return FSCACHE_CHECKAUX_OBSOLETE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 190 | } |
| 191 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 192 | if (vnode->fid.unique != aux.fid_unique) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 193 | _leave(" = OBSOLETE [uniq %x != %x]", |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 194 | aux.fid_unique, vnode->fid.unique); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 195 | return FSCACHE_CHECKAUX_OBSOLETE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 196 | } |
| 197 | |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 198 | if (vnode->status.data_version != aux.data_version) { |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 199 | _leave(" = OBSOLETE [vers %llx != %llx]", |
David Howells | ad6a942 | 2017-11-02 15:27:47 +0000 | [diff] [blame] | 200 | aux.data_version, vnode->status.data_version); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 201 | return FSCACHE_CHECKAUX_OBSOLETE; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | _leave(" = SUCCESS"); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 205 | return FSCACHE_CHECKAUX_OKAY; |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 206 | } |