linux/fs/cachefiles/interface.c
<<
>>
Prefs
   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
  16struct cachefiles_lookup_data {
  17        struct cachefiles_xattr *auxdata;       /* auxiliary data */
  18        char                    *key;           /* key path */
  19};
  20
  21static 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 */
  26static 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
 100nomem_key:
 101        kfree(buffer);
 102nomem_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);
 106nomem_object:
 107        kfree(lookup_data);
 108nomem_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 */
 117static 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                        pr_warn("Lookup failed error %d\n", ret);
 150                fscache_object_lookup_error(&object->fscache);
 151        }
 152
 153        _leave(" [%d]", ret);
 154        return ret;
 155}
 156
 157/*
 158 * indication of lookup completion
 159 */
 160static void cachefiles_lookup_complete(struct fscache_object *_object)
 161{
 162        struct cachefiles_object *object;
 163
 164        object = container_of(_object, struct cachefiles_object, fscache);
 165
 166        _enter("{OBJ%x,%p}", object->fscache.debug_id, object->lookup_data);
 167
 168        if (object->lookup_data) {
 169                kfree(object->lookup_data->key);
 170                kfree(object->lookup_data->auxdata);
 171                kfree(object->lookup_data);
 172                object->lookup_data = NULL;
 173        }
 174}
 175
 176/*
 177 * increment the usage count on an inode object (may fail if unmounting)
 178 */
 179static
 180struct fscache_object *cachefiles_grab_object(struct fscache_object *_object)
 181{
 182        struct cachefiles_object *object =
 183                container_of(_object, struct cachefiles_object, fscache);
 184
 185        _enter("{OBJ%x,%d}", _object->debug_id, atomic_read(&object->usage));
 186
 187#ifdef CACHEFILES_DEBUG_SLAB
 188        ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
 189#endif
 190
 191        atomic_inc(&object->usage);
 192        return &object->fscache;
 193}
 194
 195/*
 196 * update the auxiliary data for an object object on disk
 197 */
 198static void cachefiles_update_object(struct fscache_object *_object)
 199{
 200        struct cachefiles_object *object;
 201        struct cachefiles_xattr *auxdata;
 202        struct cachefiles_cache *cache;
 203        struct fscache_cookie *cookie;
 204        const struct cred *saved_cred;
 205        unsigned auxlen;
 206
 207        _enter("{OBJ%x}", _object->debug_id);
 208
 209        object = container_of(_object, struct cachefiles_object, fscache);
 210        cache = container_of(object->fscache.cache, struct cachefiles_cache,
 211                             cache);
 212
 213        if (!fscache_use_cookie(_object)) {
 214                _leave(" [relinq]");
 215                return;
 216        }
 217
 218        cookie = object->fscache.cookie;
 219
 220        if (!cookie->def->get_aux) {
 221                fscache_unuse_cookie(_object);
 222                _leave(" [no aux]");
 223                return;
 224        }
 225
 226        auxdata = kmalloc(2 + 512 + 3, cachefiles_gfp);
 227        if (!auxdata) {
 228                fscache_unuse_cookie(_object);
 229                _leave(" [nomem]");
 230                return;
 231        }
 232
 233        auxlen = cookie->def->get_aux(cookie->netfs_data, auxdata->data, 511);
 234        fscache_unuse_cookie(_object);
 235        ASSERTCMP(auxlen, <, 511);
 236
 237        auxdata->len = auxlen + 1;
 238        auxdata->type = cookie->def->type;
 239
 240        cachefiles_begin_secure(cache, &saved_cred);
 241        cachefiles_update_object_xattr(object, auxdata);
 242        cachefiles_end_secure(cache, saved_cred);
 243        kfree(auxdata);
 244        _leave("");
 245}
 246
 247/*
 248 * discard the resources pinned by an object and effect retirement if
 249 * requested
 250 */
 251static void cachefiles_drop_object(struct fscache_object *_object)
 252{
 253        struct cachefiles_object *object;
 254        struct cachefiles_cache *cache;
 255        const struct cred *saved_cred;
 256        struct inode *inode;
 257        blkcnt_t i_blocks = 0;
 258
 259        ASSERT(_object);
 260
 261        object = container_of(_object, struct cachefiles_object, fscache);
 262
 263        _enter("{OBJ%x,%d}",
 264               object->fscache.debug_id, atomic_read(&object->usage));
 265
 266        cache = container_of(object->fscache.cache,
 267                             struct cachefiles_cache, cache);
 268
 269#ifdef CACHEFILES_DEBUG_SLAB
 270        ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
 271#endif
 272
 273        /* We need to tidy the object up if we did in fact manage to open it.
 274         * It's possible for us to get here before the object is fully
 275         * initialised if the parent goes away or the object gets retired
 276         * before we set it up.
 277         */
 278        if (object->dentry) {
 279                /* delete retired objects */
 280                if (test_bit(FSCACHE_OBJECT_RETIRED, &object->fscache.flags) &&
 281                    _object != cache->cache.fsdef
 282                    ) {
 283                        _debug("- retire object OBJ%x", object->fscache.debug_id);
 284                        inode = d_backing_inode(object->dentry);
 285                        if (inode)
 286                                i_blocks = inode->i_blocks;
 287
 288                        cachefiles_begin_secure(cache, &saved_cred);
 289                        cachefiles_delete_object(cache, object);
 290                        cachefiles_end_secure(cache, saved_cred);
 291                }
 292
 293                /* close the filesystem stuff attached to the object */
 294                if (object->backer != object->dentry)
 295                        dput(object->backer);
 296                object->backer = NULL;
 297        }
 298
 299        /* note that the object is now inactive */
 300        if (test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags))
 301                cachefiles_mark_object_inactive(cache, object, i_blocks);
 302
 303        dput(object->dentry);
 304        object->dentry = NULL;
 305
 306        _leave("");
 307}
 308
 309/*
 310 * dispose of a reference to an object
 311 */
 312static void cachefiles_put_object(struct fscache_object *_object)
 313{
 314        struct cachefiles_object *object;
 315        struct fscache_cache *cache;
 316
 317        ASSERT(_object);
 318
 319        object = container_of(_object, struct cachefiles_object, fscache);
 320
 321        _enter("{OBJ%x,%d}",
 322               object->fscache.debug_id, atomic_read(&object->usage));
 323
 324#ifdef CACHEFILES_DEBUG_SLAB
 325        ASSERT((atomic_read(&object->usage) & 0xffff0000) != 0x6b6b0000);
 326#endif
 327
 328        ASSERTIFCMP(object->fscache.parent,
 329                    object->fscache.parent->n_children, >, 0);
 330
 331        if (atomic_dec_and_test(&object->usage)) {
 332                _debug("- kill object OBJ%x", object->fscache.debug_id);
 333
 334                ASSERT(!test_bit(CACHEFILES_OBJECT_ACTIVE, &object->flags));
 335                ASSERTCMP(object->fscache.parent, ==, NULL);
 336                ASSERTCMP(object->backer, ==, NULL);
 337                ASSERTCMP(object->dentry, ==, NULL);
 338                ASSERTCMP(object->fscache.n_ops, ==, 0);
 339                ASSERTCMP(object->fscache.n_children, ==, 0);
 340
 341                if (object->lookup_data) {
 342                        kfree(object->lookup_data->key);
 343                        kfree(object->lookup_data->auxdata);
 344                        kfree(object->lookup_data);
 345                        object->lookup_data = NULL;
 346                }
 347
 348                cache = object->fscache.cache;
 349                fscache_object_destroy(&object->fscache);
 350                kmem_cache_free(cachefiles_object_jar, object);
 351                fscache_object_destroyed(cache);
 352        }
 353
 354        _leave("");
 355}
 356
 357/*
 358 * sync a cache
 359 */
 360static void cachefiles_sync_cache(struct fscache_cache *_cache)
 361{
 362        struct cachefiles_cache *cache;
 363        const struct cred *saved_cred;
 364        int ret;
 365
 366        _enter("%p", _cache);
 367
 368        cache = container_of(_cache, struct cachefiles_cache, cache);
 369
 370        /* make sure all pages pinned by operations on behalf of the netfs are
 371         * written to disc */
 372        cachefiles_begin_secure(cache, &saved_cred);
 373        down_read(&cache->mnt->mnt_sb->s_umount);
 374        ret = sync_filesystem(cache->mnt->mnt_sb);
 375        up_read(&cache->mnt->mnt_sb->s_umount);
 376        cachefiles_end_secure(cache, saved_cred);
 377
 378        if (ret == -EIO)
 379                cachefiles_io_error(cache,
 380                                    "Attempt to sync backing fs superblock"
 381                                    " returned error %d",
 382                                    ret);
 383}
 384
 385/*
 386 * check if the backing cache is updated to FS-Cache
 387 * - called by FS-Cache when evaluates if need to invalidate the cache
 388 */
 389static int cachefiles_check_consistency(struct fscache_operation *op)
 390{
 391        struct cachefiles_object *object;
 392        struct cachefiles_cache *cache;
 393        const struct cred *saved_cred;
 394        int ret;
 395
 396        _enter("{OBJ%x}", op->object->debug_id);
 397
 398        object = container_of(op->object, struct cachefiles_object, fscache);
 399        cache = container_of(object->fscache.cache,
 400                             struct cachefiles_cache, cache);
 401
 402        cachefiles_begin_secure(cache, &saved_cred);
 403        ret = cachefiles_check_auxdata(object);
 404        cachefiles_end_secure(cache, saved_cred);
 405
 406        _leave(" = %d", ret);
 407        return ret;
 408}
 409
 410/*
 411 * notification the attributes on an object have changed
 412 * - called with reads/writes excluded by FS-Cache
 413 */
 414static int cachefiles_attr_changed(struct fscache_object *_object)
 415{
 416        struct cachefiles_object *object;
 417        struct cachefiles_cache *cache;
 418        const struct cred *saved_cred;
 419        struct iattr newattrs;
 420        uint64_t ni_size;
 421        loff_t oi_size;
 422        int ret;
 423
 424        _object->cookie->def->get_attr(_object->cookie->netfs_data, &ni_size);
 425
 426        _enter("{OBJ%x},[%llu]",
 427               _object->debug_id, (unsigned long long) ni_size);
 428
 429        object = container_of(_object, struct cachefiles_object, fscache);
 430        cache = container_of(object->fscache.cache,
 431                             struct cachefiles_cache, cache);
 432
 433        if (ni_size == object->i_size)
 434                return 0;
 435
 436        if (!object->backer)
 437                return -ENOBUFS;
 438
 439        ASSERT(d_is_reg(object->backer));
 440
 441        fscache_set_store_limit(&object->fscache, ni_size);
 442
 443        oi_size = i_size_read(d_backing_inode(object->backer));
 444        if (oi_size == ni_size)
 445                return 0;
 446
 447        cachefiles_begin_secure(cache, &saved_cred);
 448        inode_lock(d_inode(object->backer));
 449
 450        /* if there's an extension to a partial page at the end of the backing
 451         * file, we need to discard the partial page so that we pick up new
 452         * data after it */
 453        if (oi_size & ~PAGE_MASK && ni_size > oi_size) {
 454                _debug("discard tail %llx", oi_size);
 455                newattrs.ia_valid = ATTR_SIZE;
 456                newattrs.ia_size = oi_size & PAGE_MASK;
 457                ret = notify_change(object->backer, &newattrs, NULL);
 458                if (ret < 0)
 459                        goto truncate_failed;
 460        }
 461
 462        newattrs.ia_valid = ATTR_SIZE;
 463        newattrs.ia_size = ni_size;
 464        ret = notify_change(object->backer, &newattrs, NULL);
 465
 466truncate_failed:
 467        inode_unlock(d_inode(object->backer));
 468        cachefiles_end_secure(cache, saved_cred);
 469
 470        if (ret == -EIO) {
 471                fscache_set_store_limit(&object->fscache, 0);
 472                cachefiles_io_error_obj(object, "Size set failed");
 473                ret = -ENOBUFS;
 474        }
 475
 476        _leave(" = %d", ret);
 477        return ret;
 478}
 479
 480/*
 481 * Invalidate an object
 482 */
 483static void cachefiles_invalidate_object(struct fscache_operation *op)
 484{
 485        struct cachefiles_object *object;
 486        struct cachefiles_cache *cache;
 487        const struct cred *saved_cred;
 488        struct path path;
 489        uint64_t ni_size;
 490        int ret;
 491
 492        object = container_of(op->object, struct cachefiles_object, fscache);
 493        cache = container_of(object->fscache.cache,
 494                             struct cachefiles_cache, cache);
 495
 496        op->object->cookie->def->get_attr(op->object->cookie->netfs_data,
 497                                          &ni_size);
 498
 499        _enter("{OBJ%x},[%llu]",
 500               op->object->debug_id, (unsigned long long)ni_size);
 501
 502        if (object->backer) {
 503                ASSERT(d_is_reg(object->backer));
 504
 505                fscache_set_store_limit(&object->fscache, ni_size);
 506
 507                path.dentry = object->backer;
 508                path.mnt = cache->mnt;
 509
 510                cachefiles_begin_secure(cache, &saved_cred);
 511                ret = vfs_truncate(&path, 0);
 512                if (ret == 0)
 513                        ret = vfs_truncate(&path, ni_size);
 514                cachefiles_end_secure(cache, saved_cred);
 515
 516                if (ret != 0) {
 517                        fscache_set_store_limit(&object->fscache, 0);
 518                        if (ret == -EIO)
 519                                cachefiles_io_error_obj(object,
 520                                                        "Invalidate failed");
 521                }
 522        }
 523
 524        fscache_op_complete(op, true);
 525        _leave("");
 526}
 527
 528/*
 529 * dissociate a cache from all the pages it was backing
 530 */
 531static void cachefiles_dissociate_pages(struct fscache_cache *cache)
 532{
 533        _enter("");
 534}
 535
 536const struct fscache_cache_ops cachefiles_cache_ops = {
 537        .name                   = "cachefiles",
 538        .alloc_object           = cachefiles_alloc_object,
 539        .lookup_object          = cachefiles_lookup_object,
 540        .lookup_complete        = cachefiles_lookup_complete,
 541        .grab_object            = cachefiles_grab_object,
 542        .update_object          = cachefiles_update_object,
 543        .invalidate_object      = cachefiles_invalidate_object,
 544        .drop_object            = cachefiles_drop_object,
 545        .put_object             = cachefiles_put_object,
 546        .sync_cache             = cachefiles_sync_cache,
 547        .attr_changed           = cachefiles_attr_changed,
 548        .read_or_alloc_page     = cachefiles_read_or_alloc_page,
 549        .read_or_alloc_pages    = cachefiles_read_or_alloc_pages,
 550        .allocate_page          = cachefiles_allocate_page,
 551        .allocate_pages         = cachefiles_allocate_pages,
 552        .write_page             = cachefiles_write_page,
 553        .uncache_page           = cachefiles_uncache_page,
 554        .dissociate_pages       = cachefiles_dissociate_pages,
 555        .check_consistency      = cachefiles_check_consistency,
 556};
 557