1 /* FS-Cache interface to CacheFiles 2 * 3 * Copyright (C) 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 Licence 8 * as published by the Free Software Foundation; either version 9 * 2 of the Licence, or (at your option) any later version. 10 */ 11 12 #include <linux/slab.h> 13 #include <linux/mount.h> 14 #include "internal.h" 15 16 struct cachefiles_lookup_data { 17 struct cachefiles_xattr *auxdata; /* auxiliary data */ 18 char *key; /* key path */ 19 }; 20 21 static int cachefiles_attr_changed(struct fscache_object *_object); 22 23 /* 24 * allocate an object record for a cookie lookup and prepare the lookup data 25 */ 26 static struct fscache_object *cachefiles_alloc_object( 27 struct fscache_cache *_cache, 28 struct fscache_cookie *cookie) 29 { 30 struct cachefiles_lookup_data *lookup_data; 31 struct cachefiles_object *object; 32 struct cachefiles_cache *cache; 33 struct cachefiles_xattr *auxdata; 34 unsigned keylen, auxlen; 35 void *buffer; 36 char *key; 37 38 cache = container_of(_cache, struct cachefiles_cache, cache); 39 40 _enter("{%s},%p,", cache->cache.identifier, cookie); 41 42 lookup_data = kmalloc(sizeof(*lookup_data), cachefiles_gfp); 43 if (!lookup_data) 44 goto nomem_lookup_data; 45 46 /* create a new object record and a temporary leaf image */ 47 object = kmem_cache_alloc(cachefiles_object_jar, cachefiles_gfp); 48 if (!object) 49 goto nomem_object; 50 51 ASSERTCMP(object->backer, ==, NULL); 52 53 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)); 54 atomic_set(&object->usage, 1); 55 56 fscache_object_init(&object->fscache, cookie, &cache->cache); 57 58 object->type = cookie->def->type; 59 60 /* get hold of the raw key 61 * - stick the length on the front and leave space on the back for the 62 * encoder 63 */ 64 buffer = kmalloc((2 + 512) + 3, cachefiles_gfp); 65 if (!buffer) 66 goto nomem_buffer; 67 68 keylen = cookie->def->get_key(cookie->netfs_data, buffer + 2, 512); 69 ASSERTCMP(keylen, <, 512); 70 71 *(uint16_t *)buffer = keylen; 72 ((char *)buffer)[keylen + 2] = 0; 73 ((char *)buffer)[keylen + 3] = 0; 74 ((char *)buffer)[keylen + 4] = 0; 75 76 /* turn the raw key into something that can work with as a filename */ 77 key = cachefiles_cook_key(buffer, keylen + 2, object->type); 78 if (!key) 79 goto nomem_key; 80 81 /* get hold of the auxiliary data and prepend the object type */ 82 auxdata = buffer; 83 auxlen = 0; 84 if (cookie->def->get_aux) { 85 auxlen = cookie->def->get_aux(cookie->netfs_data, 86 auxdata->data, 511); 87 ASSERTCMP(auxlen, <, 511); 88 } 89 90 auxdata->len = auxlen + 1; 91 auxdata->type = cookie->def->type; 92 93 lookup_data->auxdata = auxdata; 94 lookup_data->key = key; 95 object->lookup_data = lookup_data; 96 97 _leave(" = %p [%p]", &object->fscache, lookup_data); 98 return &object->fscache; 99 100 nomem_key: 101 kfree(buffer); 102 nomem_buffer: 103 BUG_ON(test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)); 104 kmem_cache_free(cachefiles_object_jar, object); 105 fscache_object_destroyed(&cache->cache); 106 nomem_object: 107 kfree(lookup_data); 108 nomem_lookup_data: 109 _leave(" = -ENOMEM"); 110 return ERR_PTR(-ENOMEM); 111 } 112 113 /* 114 * attempt to look up the nominated node in this cache 115 * - return -ETIMEDOUT to be scheduled again 116 */ 117 static int cachefiles_lookup_object(struct fscache_object *_object) 118 { 119 struct cachefiles_lookup_data *lookup_data; 120 struct cachefiles_object *parent, *object; 121 struct cachefiles_cache *cache; 122 const struct cred *saved_cred; 123 int ret; 124 125 _enter("{OBJ%x}", _object->debug_id); 126 127 cache = container_of(_object->cache, struct cachefiles_cache, cache); 128 parent = container_of(_object->parent, 129 struct cachefiles_object, fscache); 130 object = container_of(_object, struct cachefiles_object, fscache); 131 lookup_data = object->lookup_data; 132 133 ASSERTCMP(lookup_data, !=, NULL); 134 135 /* look up the key, creating any missing bits */ 136 cachefiles_begin_secure(cache, &saved_cred); 137 ret = cachefiles_walk_to_object(parent, object, 138 lookup_data->key, 139 lookup_data->auxdata); 140 cachefiles_end_secure(cache, saved_cred); 141 142 /* polish off by setting the attributes of non-index files */ 143 if (ret == 0 && 144 object->fscache.cookie->def->type != FSCACHE_COOKIE_TYPE_INDEX) 145 cachefiles_attr_changed(&object->fscache); 146 147 if (ret < 0 && ret != -ETIMEDOUT) { 148 if (ret != -ENOBUFS) 149 printk(KERN_WARNING 150 "CacheFiles: Lookup failed error %d\n", ret); 151 fscache_object_lookup_error(&object->fscache); 152 } 153 154 _leave(" [%d]", ret); 155 return ret; 156 } 157 158 /* 159 * indication of lookup completion 160 */ 161 static void cachefiles_lookup_complete(struct fscache_object *_object) 162 { 163 struct cachefiles_object *object; 164 165 object = container_of(_object, struct cachefiles_object, fscache); 166 167 _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data); 168 169 if (object->lookup_data) { 170 kfree(object->lookup_data->key); 171 kfree(object->lookup_data->auxdata); 172 kfree(object->lookup_data); 173 object->lookup_data = NULL; 174 } 175 } 176 177 /* 178 * increment the usage count on an inode object (may fail if unmounting) 179 */ 180 static 181 struct fscache_object *cachefiles_grab_object(struct fscache_object *_object) 182 { 183 struct cachefiles_object *object = 184 container_of(_object, struct cachefiles_object, fscache); 185 186 _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage)); 187 188 #ifdef CACHEFILES_DEBUG_SLAB 189 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000); 190 #endif 191 192 atomic_inc(&object->usage); 193 return &object->fscache; 194 } 195 196 /* 197 * update the auxiliary data for an object object on disk 198 */ 199 static void cachefiles_update_object(struct fscache_object *_object) 200 { 201 struct cachefiles_object *object; 202 struct cachefiles_xattr *auxdata; 203 struct cachefiles_cache *cache; 204 struct fscache_cookie *cookie; 205 const struct cred *saved_cred; 206 unsigned auxlen; 207 208 _enter("{OBJ%x}", _object->debug_id); 209 210 object = container_of(_object, struct cachefiles_object, fscache); 211 cache = container_of(object->fscache.cache, struct cachefiles_cache, 212 cache); 213 214 if (!fscache_use_cookie(_object)) { 215 _leave(" [relinq]"); 216 return; 217 } 218 219 cookie = object->fscache.cookie; 220 221 if (!cookie->def->get_aux) { 222 fscache_unuse_cookie(_object); 223 _leave(" [no aux]"); 224 return; 225 } 226 227 auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp); 228 if (!auxdata) { 229 fscache_unuse_cookie(_object); 230 _leave(" [nomem]"); 231 return; 232 } 233 234 auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511); 235 fscache_unuse_cookie(_object); 236 ASSERTCMP(auxlen, <, 511); 237 238 auxdata->len = auxlen + 1; 239 auxdata->type = cookie->def->type; 240 241 cachefiles_begin_secure(cache, &saved_cred); 242 cachefiles_update_object_xattr(object, auxdata); 243 cachefiles_end_secure(cache, saved_cred); 244 kfree(auxdata); 245 _leave(""); 246 } 247 248 /* 249 * discard the resources pinned by an object and effect retirement if 250 * requested 251 */ 252 static void cachefiles_drop_object(struct fscache_object *_object) 253 { 254 struct cachefiles_object *object; 255 struct cachefiles_cache *cache; 256 const struct cred *saved_cred; 257 258 ASSERT(_object); 259 260 object = container_of(_object, struct cachefiles_object, fscache); 261 262 _enter("{OBJ%x,%d}", 263 object->fscache.debug_id, atomic_read(&object->usage)); 264 265 cache = container_of(object->fscache.cache, 266 struct cachefiles_cache, cache); 267 268 #ifdef CACHEFILES_DEBUG_SLAB 269 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000); 270 #endif 271 272 /* delete retired objects */ 273 if (test_bit(FSCACHE_COOKIE_RETIRED, &object->fscache.cookie->flags) && 274 _object != cache->cache.fsdef 275 ) { 276 _debug("- retire object OBJ%x", object->fscache.debug_id); 277 cachefiles_begin_secure(cache, &saved_cred); 278 cachefiles_delete_object(cache, object); 279 cachefiles_end_secure(cache, saved_cred); 280 } 281 282 /* close the filesystem stuff attached to the object */ 283 if (object->backer != object->dentry) 284 dput(object->backer); 285 object->backer = NULL; 286 287 /* note that the object is now inactive */ 288 if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)) { 289 write_lock(&cache->active_lock); 290 if (!test_and_clear_bit(CACHEFILES_OBJECT_ACTIVE, 291 &object->flags)) 292 BUG(); 293 rb_erase(&object->active_node, &cache->active_nodes); 294 wake_up_bit(&object->flags, CACHEFILES_OBJECT_ACTIVE); 295 write_unlock(&cache->active_lock); 296 } 297 298 dput(object->dentry); 299 object->dentry = NULL; 300 301 _leave(""); 302 } 303 304 /* 305 * dispose of a reference to an object 306 */ 307 static void cachefiles_put_object(struct fscache_object *_object) 308 { 309 struct cachefiles_object *object; 310 struct fscache_cache *cache; 311 312 ASSERT(_object); 313 314 object = container_of(_object, struct cachefiles_object, fscache); 315 316 _enter("{OBJ%x,%d}", 317 object->fscache.debug_id, atomic_read(&object->usage)); 318 319 #ifdef CACHEFILES_DEBUG_SLAB 320 ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000); 321 #endif 322 323 ASSERTIFCMP(object->fscache.parent, 324 object->fscache.parent->n_children, >, 0); 325 326 if (atomic_dec_and_test(&object->usage)) { 327 _debug("- kill object OBJ%x", object->fscache.debug_id); 328 329 ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags)); 330 ASSERTCMP(object->fscache.parent, ==, NULL); 331 ASSERTCMP(object->backer, ==, NULL); 332 ASSERTCMP(object->dentry, ==, NULL); 333 ASSERTCMP(object->fscache.n_ops, ==, 0); 334 ASSERTCMP(object->fscache.n_children, ==, 0); 335 336 if (object->lookup_data) { 337 kfree(object->lookup_data->key); 338 kfree(object->lookup_data->auxdata); 339 kfree(object->lookup_data); 340 object->lookup_data = NULL; 341 } 342 343 cache = object->fscache.cache; 344 fscache_object_destroy(&object->fscache); 345 kmem_cache_free(cachefiles_object_jar, object); 346 fscache_object_destroyed(cache); 347 } 348 349 _leave(""); 350 } 351 352 /* 353 * sync a cache 354 */ 355 static void cachefiles_sync_cache(struct fscache_cache *_cache) 356 { 357 struct cachefiles_cache *cache; 358 const struct cred *saved_cred; 359 int ret; 360 361 _enter("%p", _cache); 362 363 cache = container_of(_cache, struct cachefiles_cache, cache); 364 365 /* make sure all pages pinned by operations on behalf of the netfs are 366 * written to disc */ 367 cachefiles_begin_secure(cache, &saved_cred); 368 down_read(&cache->mnt->mnt_sb->s_umount); 369 ret = sync_filesystem(cache->mnt->mnt_sb); 370 up_read(&cache->mnt->mnt_sb->s_umount); 371 cachefiles_end_secure(cache, saved_cred); 372 373 if (ret == -EIO) 374 cachefiles_io_error(cache, 375 "Attempt to sync backing fs superblock" 376 " returned error %d", 377 ret); 378 } 379 380 /* 381 * notification the attributes on an object have changed 382 * - called with reads/writes excluded by FS-Cache 383 */ 384 static int cachefiles_attr_changed(struct fscache_object *_object) 385 { 386 struct cachefiles_object *object; 387 struct cachefiles_cache *cache; 388 const struct cred *saved_cred; 389 struct iattr newattrs; 390 uint64_t ni_size; 391 loff_t oi_size; 392 int ret; 393 394 _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size); 395 396 _enter("{OBJ%x},[%llu]", 397 _object->debug_id, (unsigned long long) ni_size); 398 399 object = container_of(_object, struct cachefiles_object, fscache); 400 cache = container_of(object->fscache.cache, 401 struct cachefiles_cache, cache); 402 403 if (ni_size == object->i_size) 404 return 0; 405 406 if (!object->backer) 407 return -ENOBUFS; 408 409 ASSERT(S_ISREG(object->backer->d_inode->i_mode)); 410 411 fscache_set_store_limit(&object->fscache, ni_size); 412 413 oi_size = i_size_read(object->backer->d_inode); 414 if (oi_size == ni_size) 415 return 0; 416 417 cachefiles_begin_secure(cache, &saved_cred); 418 mutex_lock(&object->backer->d_inode->i_mutex); 419 420 /* if there's an extension to a partial page at the end of the backing 421 * file, we need to discard the partial page so that we pick up new 422 * data after it */ 423 if (oi_size & ~PAGE_MASK && ni_size > oi_size) { 424 _debug("discard tail %llx", oi_size); 425 newattrs.ia_valid = ATTR_SIZE; 426 newattrs.ia_size = oi_size & PAGE_MASK; 427 ret = notify_change(object->backer, &newattrs); 428 if (ret < 0) 429 goto truncate_failed; 430 } 431 432 newattrs.ia_valid = ATTR_SIZE; 433 newattrs.ia_size = ni_size; 434 ret = notify_change(object->backer, &newattrs); 435 436 truncate_failed: 437 mutex_unlock(&object->backer->d_inode->i_mutex); 438 cachefiles_end_secure(cache, saved_cred); 439 440 if (ret == -EIO) { 441 fscache_set_store_limit(&object->fscache, 0); 442 cachefiles_io_error_obj(object, "Size set failed"); 443 ret = -ENOBUFS; 444 } 445 446 _leave(" = %d", ret); 447 return ret; 448 } 449 450 /* 451 * Invalidate an object 452 */ 453 static void cachefiles_invalidate_object(struct fscache_operation *op) 454 { 455 struct cachefiles_object *object; 456 struct cachefiles_cache *cache; 457 const struct cred *saved_cred; 458 struct path path; 459 uint64_t ni_size; 460 int ret; 461 462 object = container_of(op->object, struct cachefiles_object, fscache); 463 cache = container_of(object->fscache.cache, 464 struct cachefiles_cache, cache); 465 466 op->object->cookie->def->get_attr(op->object->cookie->netfs_data, 467 &ni_size); 468 469 _enter("{OBJ%x},[%llu]", 470 op->object->debug_id, (unsigned long long)ni_size); 471 472 if (object->backer) { 473 ASSERT(S_ISREG(object->backer->d_inode->i_mode)); 474 475 fscache_set_store_limit(&object->fscache, ni_size); 476 477 path.dentry = object->backer; 478 path.mnt = cache->mnt; 479 480 cachefiles_begin_secure(cache, &saved_cred); 481 ret = vfs_truncate(&path, 0); 482 if (ret == 0) 483 ret = vfs_truncate(&path, ni_size); 484 cachefiles_end_secure(cache, saved_cred); 485 486 if (ret != 0) { 487 fscache_set_store_limit(&object->fscache, 0); 488 if (ret == -EIO) 489 cachefiles_io_error_obj(object, 490 "Invalidate failed"); 491 } 492 } 493 494 fscache_op_complete(op, true); 495 _leave(""); 496 } 497 498 /* 499 * dissociate a cache from all the pages it was backing 500 */ 501 static void cachefiles_dissociate_pages(struct fscache_cache *cache) 502 { 503 _enter(""); 504 } 505 506 const struct fscache_cache_ops cachefiles_cache_ops = { 507 .name = "cachefiles", 508 .alloc_object = cachefiles_alloc_object, 509 .lookup_object = cachefiles_lookup_object, 510 .lookup_complete = cachefiles_lookup_complete, 511 .grab_object = cachefiles_grab_object, 512 .update_object = cachefiles_update_object, 513 .invalidate_object = cachefiles_invalidate_object, 514 .drop_object = cachefiles_drop_object, 515 .put_object = cachefiles_put_object, 516 .sync_cache = cachefiles_sync_cache, 517 .attr_changed = cachefiles_attr_changed, 518 .read_or_alloc_page = cachefiles_read_or_alloc_page, 519 .read_or_alloc_pages = cachefiles_read_or_alloc_pages, 520 .allocate_page = cachefiles_allocate_page, 521 .allocate_pages = cachefiles_allocate_pages, 522 .write_page = cachefiles_write_page, 523 .uncache_page = cachefiles_uncache_page, 524 .dissociate_pages = cachefiles_dissociate_pages, 525 }; 526