linux/drivers/staging/lustre/lustre/obdclass/cl_object.c
<<
>>
Prefs
   1/*
   2 * GPL HEADER START
   3 *
   4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   5 *
   6 * This program is free software; you can redistribute it and/or modify
   7 * it under the terms of the GNU General Public License version 2 only,
   8 * as published by the Free Software Foundation.
   9 *
  10 * This program is distributed in the hope that it will be useful, but
  11 * WITHOUT ANY WARRANTY; without even the implied warranty of
  12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13 * General Public License version 2 for more details (a copy is included
  14 * in the LICENSE file that accompanied this code).
  15 *
  16 * You should have received a copy of the GNU General Public License
  17 * version 2 along with this program; If not, see
  18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
  19 *
  20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  21 * CA 95054 USA or visit www.sun.com if you need additional information or
  22 * have any questions.
  23 *
  24 * GPL HEADER END
  25 */
  26/*
  27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  28 * Use is subject to license terms.
  29 *
  30 * Copyright (c) 2011, 2015, Intel Corporation.
  31 */
  32/*
  33 * This file is part of Lustre, http://www.lustre.org/
  34 * Lustre is a trademark of Sun Microsystems, Inc.
  35 *
  36 * Client Lustre Object.
  37 *
  38 *   Author: Nikita Danilov <nikita.danilov@sun.com>
  39 */
  40
  41/*
  42 * Locking.
  43 *
  44 *  i_mutex
  45 *      PG_locked
  46 *        ->coh_page_guard
  47 *        ->coh_lock_guard
  48 *        ->coh_attr_guard
  49 *        ->ls_guard
  50 */
  51
  52#define DEBUG_SUBSYSTEM S_CLASS
  53
  54#include "../../include/linux/libcfs/libcfs.h"
  55/* class_put_type() */
  56#include "../include/obd_class.h"
  57#include "../include/obd_support.h"
  58#include "../include/lustre_fid.h"
  59#include <linux/list.h>
  60#include "../../include/linux/libcfs/libcfs_hash.h"     /* for cfs_hash stuff */
  61#include "../include/cl_object.h"
  62#include "cl_internal.h"
  63
  64static struct kmem_cache *cl_env_kmem;
  65
  66/** Lock class of cl_object_header::coh_page_guard */
  67static struct lock_class_key cl_page_guard_class;
  68/** Lock class of cl_object_header::coh_lock_guard */
  69static struct lock_class_key cl_lock_guard_class;
  70/** Lock class of cl_object_header::coh_attr_guard */
  71static struct lock_class_key cl_attr_guard_class;
  72
  73extern __u32 lu_context_tags_default;
  74extern __u32 lu_session_tags_default;
  75/**
  76 * Initialize cl_object_header.
  77 */
  78int cl_object_header_init(struct cl_object_header *h)
  79{
  80        int result;
  81
  82        result = lu_object_header_init(&h->coh_lu);
  83        if (result == 0) {
  84                spin_lock_init(&h->coh_page_guard);
  85                spin_lock_init(&h->coh_lock_guard);
  86                spin_lock_init(&h->coh_attr_guard);
  87                lockdep_set_class(&h->coh_page_guard, &cl_page_guard_class);
  88                lockdep_set_class(&h->coh_lock_guard, &cl_lock_guard_class);
  89                lockdep_set_class(&h->coh_attr_guard, &cl_attr_guard_class);
  90                h->coh_pages = 0;
  91                /* XXX hard coded GFP_* mask. */
  92                INIT_RADIX_TREE(&h->coh_tree, GFP_ATOMIC);
  93                INIT_LIST_HEAD(&h->coh_locks);
  94                h->coh_page_bufsize = ALIGN(sizeof(struct cl_page), 8);
  95        }
  96        return result;
  97}
  98EXPORT_SYMBOL(cl_object_header_init);
  99
 100/**
 101 * Returns a cl_object with a given \a fid.
 102 *
 103 * Returns either cached or newly created object. Additional reference on the
 104 * returned object is acquired.
 105 *
 106 * \see lu_object_find(), cl_page_find(), cl_lock_find()
 107 */
 108struct cl_object *cl_object_find(const struct lu_env *env,
 109                                 struct cl_device *cd, const struct lu_fid *fid,
 110                                 const struct cl_object_conf *c)
 111{
 112        might_sleep();
 113        return lu2cl(lu_object_find_slice(env, cl2lu_dev(cd), fid, &c->coc_lu));
 114}
 115EXPORT_SYMBOL(cl_object_find);
 116
 117/**
 118 * Releases a reference on \a o.
 119 *
 120 * When last reference is released object is returned to the cache, unless
 121 * lu_object_header_flags::LU_OBJECT_HEARD_BANSHEE bit is set in its header.
 122 *
 123 * \see cl_page_put(), cl_lock_put().
 124 */
 125void cl_object_put(const struct lu_env *env, struct cl_object *o)
 126{
 127        lu_object_put(env, &o->co_lu);
 128}
 129EXPORT_SYMBOL(cl_object_put);
 130
 131/**
 132 * Acquire an additional reference to the object \a o.
 133 *
 134 * This can only be used to acquire _additional_ reference, i.e., caller
 135 * already has to possess at least one reference to \a o before calling this.
 136 *
 137 * \see cl_page_get(), cl_lock_get().
 138 */
 139void cl_object_get(struct cl_object *o)
 140{
 141        lu_object_get(&o->co_lu);
 142}
 143EXPORT_SYMBOL(cl_object_get);
 144
 145/**
 146 * Returns the top-object for a given \a o.
 147 *
 148 * \see cl_page_top(), cl_io_top()
 149 */
 150struct cl_object *cl_object_top(struct cl_object *o)
 151{
 152        struct cl_object_header *hdr = cl_object_header(o);
 153        struct cl_object *top;
 154
 155        while (hdr->coh_parent)
 156                hdr = hdr->coh_parent;
 157
 158        top = lu2cl(lu_object_top(&hdr->coh_lu));
 159        CDEBUG(D_TRACE, "%p -> %p\n", o, top);
 160        return top;
 161}
 162EXPORT_SYMBOL(cl_object_top);
 163
 164/**
 165 * Returns pointer to the lock protecting data-attributes for the given object
 166 * \a o.
 167 *
 168 * Data-attributes are protected by the cl_object_header::coh_attr_guard
 169 * spin-lock in the top-object.
 170 *
 171 * \see cl_attr, cl_object_attr_lock(), cl_object_operations::coo_attr_get().
 172 */
 173static spinlock_t *cl_object_attr_guard(struct cl_object *o)
 174{
 175        return &cl_object_header(cl_object_top(o))->coh_attr_guard;
 176}
 177
 178/**
 179 * Locks data-attributes.
 180 *
 181 * Prevents data-attributes from changing, until lock is released by
 182 * cl_object_attr_unlock(). This has to be called before calls to
 183 * cl_object_attr_get(), cl_object_attr_set().
 184 */
 185void cl_object_attr_lock(struct cl_object *o)
 186        __acquires(cl_object_attr_guard(o))
 187{
 188        spin_lock(cl_object_attr_guard(o));
 189}
 190EXPORT_SYMBOL(cl_object_attr_lock);
 191
 192/**
 193 * Releases data-attributes lock, acquired by cl_object_attr_lock().
 194 */
 195void cl_object_attr_unlock(struct cl_object *o)
 196        __releases(cl_object_attr_guard(o))
 197{
 198        spin_unlock(cl_object_attr_guard(o));
 199}
 200EXPORT_SYMBOL(cl_object_attr_unlock);
 201
 202/**
 203 * Returns data-attributes of an object \a obj.
 204 *
 205 * Every layer is asked (by calling cl_object_operations::coo_attr_get())
 206 * top-to-bottom to fill in parts of \a attr that this layer is responsible
 207 * for.
 208 */
 209int cl_object_attr_get(const struct lu_env *env, struct cl_object *obj,
 210                       struct cl_attr *attr)
 211{
 212        struct lu_object_header *top;
 213        int result;
 214
 215        assert_spin_locked(cl_object_attr_guard(obj));
 216
 217        top = obj->co_lu.lo_header;
 218        result = 0;
 219        list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
 220                if (obj->co_ops->coo_attr_get) {
 221                        result = obj->co_ops->coo_attr_get(env, obj, attr);
 222                        if (result != 0) {
 223                                if (result > 0)
 224                                        result = 0;
 225                                break;
 226                        }
 227                }
 228        }
 229        return result;
 230}
 231EXPORT_SYMBOL(cl_object_attr_get);
 232
 233/**
 234 * Updates data-attributes of an object \a obj.
 235 *
 236 * Only attributes, mentioned in a validness bit-mask \a v are
 237 * updated. Calls cl_object_operations::coo_attr_set() on every layer, bottom
 238 * to top.
 239 */
 240int cl_object_attr_set(const struct lu_env *env, struct cl_object *obj,
 241                       const struct cl_attr *attr, unsigned v)
 242{
 243        struct lu_object_header *top;
 244        int result;
 245
 246        assert_spin_locked(cl_object_attr_guard(obj));
 247
 248        top = obj->co_lu.lo_header;
 249        result = 0;
 250        list_for_each_entry_reverse(obj, &top->loh_layers, co_lu.lo_linkage) {
 251                if (obj->co_ops->coo_attr_set) {
 252                        result = obj->co_ops->coo_attr_set(env, obj, attr, v);
 253                        if (result != 0) {
 254                                if (result > 0)
 255                                        result = 0;
 256                                break;
 257                        }
 258                }
 259        }
 260        return result;
 261}
 262EXPORT_SYMBOL(cl_object_attr_set);
 263
 264/**
 265 * Notifies layers (bottom-to-top) that glimpse AST was received.
 266 *
 267 * Layers have to fill \a lvb fields with information that will be shipped
 268 * back to glimpse issuer.
 269 *
 270 * \see cl_lock_operations::clo_glimpse()
 271 */
 272int cl_object_glimpse(const struct lu_env *env, struct cl_object *obj,
 273                      struct ost_lvb *lvb)
 274{
 275        struct lu_object_header *top;
 276        int result;
 277
 278        top = obj->co_lu.lo_header;
 279        result = 0;
 280        list_for_each_entry_reverse(obj, &top->loh_layers, co_lu.lo_linkage) {
 281                if (obj->co_ops->coo_glimpse) {
 282                        result = obj->co_ops->coo_glimpse(env, obj, lvb);
 283                        if (result != 0)
 284                                break;
 285                }
 286        }
 287        LU_OBJECT_HEADER(D_DLMTRACE, env, lu_object_top(top),
 288                         "size: %llu mtime: %llu atime: %llu ctime: %llu blocks: %llu\n",
 289                         lvb->lvb_size, lvb->lvb_mtime, lvb->lvb_atime,
 290                         lvb->lvb_ctime, lvb->lvb_blocks);
 291        return result;
 292}
 293EXPORT_SYMBOL(cl_object_glimpse);
 294
 295/**
 296 * Updates a configuration of an object \a obj.
 297 */
 298int cl_conf_set(const struct lu_env *env, struct cl_object *obj,
 299                const struct cl_object_conf *conf)
 300{
 301        struct lu_object_header *top;
 302        int result;
 303
 304        top = obj->co_lu.lo_header;
 305        result = 0;
 306        list_for_each_entry(obj, &top->loh_layers, co_lu.lo_linkage) {
 307                if (obj->co_ops->coo_conf_set) {
 308                        result = obj->co_ops->coo_conf_set(env, obj, conf);
 309                        if (result != 0)
 310                                break;
 311                }
 312        }
 313        return result;
 314}
 315EXPORT_SYMBOL(cl_conf_set);
 316
 317/**
 318 * Helper function removing all object locks, and marking object for
 319 * deletion. All object pages must have been deleted at this point.
 320 *
 321 * This is called by cl_inode_fini() and lov_object_delete() to destroy top-
 322 * and sub- objects respectively.
 323 */
 324void cl_object_kill(const struct lu_env *env, struct cl_object *obj)
 325{
 326        struct cl_object_header *hdr;
 327
 328        hdr = cl_object_header(obj);
 329        LASSERT(!hdr->coh_tree.rnode);
 330        LASSERT(hdr->coh_pages == 0);
 331
 332        set_bit(LU_OBJECT_HEARD_BANSHEE, &hdr->coh_lu.loh_flags);
 333        /*
 334         * Destroy all locks. Object destruction (including cl_inode_fini())
 335         * cannot cancel the locks, because in the case of a local client,
 336         * where client and server share the same thread running
 337         * prune_icache(), this can dead-lock with ldlm_cancel_handler()
 338         * waiting on __wait_on_freeing_inode().
 339         */
 340        cl_locks_prune(env, obj, 0);
 341}
 342EXPORT_SYMBOL(cl_object_kill);
 343
 344/**
 345 * Prunes caches of pages and locks for this object.
 346 */
 347void cl_object_prune(const struct lu_env *env, struct cl_object *obj)
 348{
 349        cl_pages_prune(env, obj);
 350        cl_locks_prune(env, obj, 1);
 351}
 352EXPORT_SYMBOL(cl_object_prune);
 353
 354void cache_stats_init(struct cache_stats *cs, const char *name)
 355{
 356        int i;
 357
 358        cs->cs_name = name;
 359        for (i = 0; i < CS_NR; i++)
 360                atomic_set(&cs->cs_stats[i], 0);
 361}
 362
 363static int cache_stats_print(const struct cache_stats *cs,
 364                             struct seq_file *m, int h)
 365{
 366        int i;
 367        /*
 368         *   lookup    hit    total  cached create
 369         * env: ...... ...... ...... ...... ......
 370         */
 371        if (h) {
 372                const char *names[CS_NR] = CS_NAMES;
 373
 374                seq_printf(m, "%6s", " ");
 375                for (i = 0; i < CS_NR; i++)
 376                        seq_printf(m, "%8s", names[i]);
 377                seq_printf(m, "\n");
 378        }
 379
 380        seq_printf(m, "%5.5s:", cs->cs_name);
 381        for (i = 0; i < CS_NR; i++)
 382                seq_printf(m, "%8u", atomic_read(&cs->cs_stats[i]));
 383        return 0;
 384}
 385
 386/**
 387 * Initialize client site.
 388 *
 389 * Perform common initialization (lu_site_init()), and initialize statistical
 390 * counters. Also perform global initializations on the first call.
 391 */
 392int cl_site_init(struct cl_site *s, struct cl_device *d)
 393{
 394        int i;
 395        int result;
 396
 397        result = lu_site_init(&s->cs_lu, &d->cd_lu_dev);
 398        if (result == 0) {
 399                cache_stats_init(&s->cs_pages, "pages");
 400                cache_stats_init(&s->cs_locks, "locks");
 401                for (i = 0; i < ARRAY_SIZE(s->cs_pages_state); ++i)
 402                        atomic_set(&s->cs_pages_state[0], 0);
 403                for (i = 0; i < ARRAY_SIZE(s->cs_locks_state); ++i)
 404                        atomic_set(&s->cs_locks_state[i], 0);
 405        }
 406        return result;
 407}
 408EXPORT_SYMBOL(cl_site_init);
 409
 410/**
 411 * Finalize client site. Dual to cl_site_init().
 412 */
 413void cl_site_fini(struct cl_site *s)
 414{
 415        lu_site_fini(&s->cs_lu);
 416}
 417EXPORT_SYMBOL(cl_site_fini);
 418
 419static struct cache_stats cl_env_stats = {
 420        .cs_name    = "envs",
 421        .cs_stats = { ATOMIC_INIT(0), }
 422};
 423
 424/**
 425 * Outputs client site statistical counters into a buffer. Suitable for
 426 * ll_rd_*()-style functions.
 427 */
 428int cl_site_stats_print(const struct cl_site *site, struct seq_file *m)
 429{
 430        int i;
 431        static const char *pstate[] = {
 432                [CPS_CACHED]  = "c",
 433                [CPS_OWNED]   = "o",
 434                [CPS_PAGEOUT] = "w",
 435                [CPS_PAGEIN]  = "r",
 436                [CPS_FREEING] = "f"
 437        };
 438        static const char *lstate[] = {
 439                [CLS_NEW]       = "n",
 440                [CLS_QUEUING]   = "q",
 441                [CLS_ENQUEUED]  = "e",
 442                [CLS_HELD]      = "h",
 443                [CLS_INTRANSIT] = "t",
 444                [CLS_CACHED]    = "c",
 445                [CLS_FREEING]   = "f"
 446        };
 447/*
 448       lookup    hit  total   busy create
 449pages: ...... ...... ...... ...... ...... [...... ...... ...... ......]
 450locks: ...... ...... ...... ...... ...... [...... ...... ...... ...... ......]
 451  env: ...... ...... ...... ...... ......
 452 */
 453        lu_site_stats_print(&site->cs_lu, m);
 454        cache_stats_print(&site->cs_pages, m, 1);
 455        seq_printf(m, " [");
 456        for (i = 0; i < ARRAY_SIZE(site->cs_pages_state); ++i)
 457                seq_printf(m, "%s: %u ", pstate[i],
 458                           atomic_read(&site->cs_pages_state[i]));
 459        seq_printf(m, "]\n");
 460        cache_stats_print(&site->cs_locks, m, 0);
 461        seq_printf(m, " [");
 462        for (i = 0; i < ARRAY_SIZE(site->cs_locks_state); ++i)
 463                seq_printf(m, "%s: %u ", lstate[i],
 464                           atomic_read(&site->cs_locks_state[i]));
 465        seq_printf(m, "]\n");
 466        cache_stats_print(&cl_env_stats, m, 0);
 467        seq_printf(m, "\n");
 468        return 0;
 469}
 470EXPORT_SYMBOL(cl_site_stats_print);
 471
 472/*****************************************************************************
 473 *
 474 * lu_env handling on client.
 475 *
 476 */
 477
 478/**
 479 * The most efficient way is to store cl_env pointer in task specific
 480 * structures. On Linux, it wont' be easy to use task_struct->journal_info
 481 * because Lustre code may call into other fs which has certain assumptions
 482 * about journal_info. Currently following fields in task_struct are identified
 483 * can be used for this purpose:
 484 *  - tux_info: only on RedHat kernel.
 485 *  - ...
 486 * \note As long as we use task_struct to store cl_env, we assume that once
 487 * called into Lustre, we'll never call into the other part of the kernel
 488 * which will use those fields in task_struct without explicitly exiting
 489 * Lustre.
 490 *
 491 * If there's no space in task_struct is available, hash will be used.
 492 * bz20044, bz22683.
 493 */
 494
 495struct cl_env {
 496        void         *ce_magic;
 497        struct lu_env     ce_lu;
 498        struct lu_context ce_ses;
 499
 500        /**
 501         * This allows cl_env to be entered into cl_env_hash which implements
 502         * the current thread -> client environment lookup.
 503         */
 504        struct hlist_node  ce_node;
 505        /**
 506         * Owner for the current cl_env.
 507         *
 508         * If LL_TASK_CL_ENV is defined, this point to the owning current,
 509         * only for debugging purpose ;
 510         * Otherwise hash is used, and this is the key for cfs_hash.
 511         * Now current thread pid is stored. Note using thread pointer would
 512         * lead to unbalanced hash because of its specific allocation locality
 513         * and could be varied for different platforms and OSes, even different
 514         * OS versions.
 515         */
 516        void         *ce_owner;
 517
 518        /*
 519         * Linkage into global list of all client environments. Used for
 520         * garbage collection.
 521         */
 522        struct list_head        ce_linkage;
 523        /*
 524         *
 525         */
 526        int            ce_ref;
 527        /*
 528         * Debugging field: address of the caller who made original
 529         * allocation.
 530         */
 531        void         *ce_debug;
 532};
 533
 534#define CL_ENV_INC(counter)
 535#define CL_ENV_DEC(counter)
 536
 537static void cl_env_init0(struct cl_env *cle, void *debug)
 538{
 539        LASSERT(cle->ce_ref == 0);
 540        LASSERT(cle->ce_magic == &cl_env_init0);
 541        LASSERT(!cle->ce_debug && !cle->ce_owner);
 542
 543        cle->ce_ref = 1;
 544        cle->ce_debug = debug;
 545        CL_ENV_INC(busy);
 546}
 547
 548/*
 549 * The implementation of using hash table to connect cl_env and thread
 550 */
 551
 552static struct cfs_hash *cl_env_hash;
 553
 554static unsigned cl_env_hops_hash(struct cfs_hash *lh,
 555                                 const void *key, unsigned mask)
 556{
 557#if BITS_PER_LONG == 64
 558        return cfs_hash_u64_hash((__u64)key, mask);
 559#else
 560        return cfs_hash_u32_hash((__u32)key, mask);
 561#endif
 562}
 563
 564static void *cl_env_hops_obj(struct hlist_node *hn)
 565{
 566        struct cl_env *cle = hlist_entry(hn, struct cl_env, ce_node);
 567
 568        LASSERT(cle->ce_magic == &cl_env_init0);
 569        return (void *)cle;
 570}
 571
 572static int cl_env_hops_keycmp(const void *key, struct hlist_node *hn)
 573{
 574        struct cl_env *cle = cl_env_hops_obj(hn);
 575
 576        LASSERT(cle->ce_owner);
 577        return (key == cle->ce_owner);
 578}
 579
 580static void cl_env_hops_noop(struct cfs_hash *hs, struct hlist_node *hn)
 581{
 582        struct cl_env *cle = hlist_entry(hn, struct cl_env, ce_node);
 583
 584        LASSERT(cle->ce_magic == &cl_env_init0);
 585}
 586
 587static struct cfs_hash_ops cl_env_hops = {
 588        .hs_hash        = cl_env_hops_hash,
 589        .hs_key         = cl_env_hops_obj,
 590        .hs_keycmp      = cl_env_hops_keycmp,
 591        .hs_object      = cl_env_hops_obj,
 592        .hs_get         = cl_env_hops_noop,
 593        .hs_put_locked  = cl_env_hops_noop,
 594};
 595
 596static inline struct cl_env *cl_env_fetch(void)
 597{
 598        struct cl_env *cle;
 599
 600        cle = cfs_hash_lookup(cl_env_hash, (void *) (long) current->pid);
 601        LASSERT(ergo(cle, cle->ce_magic == &cl_env_init0));
 602        return cle;
 603}
 604
 605static inline void cl_env_attach(struct cl_env *cle)
 606{
 607        if (cle) {
 608                int rc;
 609
 610                LASSERT(!cle->ce_owner);
 611                cle->ce_owner = (void *) (long) current->pid;
 612                rc = cfs_hash_add_unique(cl_env_hash, cle->ce_owner,
 613                                         &cle->ce_node);
 614                LASSERT(rc == 0);
 615        }
 616}
 617
 618static inline void cl_env_do_detach(struct cl_env *cle)
 619{
 620        void *cookie;
 621
 622        LASSERT(cle->ce_owner == (void *) (long) current->pid);
 623        cookie = cfs_hash_del(cl_env_hash, cle->ce_owner,
 624                              &cle->ce_node);
 625        LASSERT(cookie == cle);
 626        cle->ce_owner = NULL;
 627}
 628
 629static int cl_env_store_init(void)
 630{
 631        cl_env_hash = cfs_hash_create("cl_env",
 632                                      HASH_CL_ENV_BITS, HASH_CL_ENV_BITS,
 633                                      HASH_CL_ENV_BKT_BITS, 0,
 634                                      CFS_HASH_MIN_THETA,
 635                                      CFS_HASH_MAX_THETA,
 636                                      &cl_env_hops,
 637                                      CFS_HASH_RW_BKTLOCK);
 638        return cl_env_hash ? 0 : -ENOMEM;
 639}
 640
 641static void cl_env_store_fini(void)
 642{
 643        cfs_hash_putref(cl_env_hash);
 644}
 645
 646static inline struct cl_env *cl_env_detach(struct cl_env *cle)
 647{
 648        if (!cle)
 649                cle = cl_env_fetch();
 650
 651        if (cle && cle->ce_owner)
 652                cl_env_do_detach(cle);
 653
 654        return cle;
 655}
 656
 657static struct lu_env *cl_env_new(__u32 ctx_tags, __u32 ses_tags, void *debug)
 658{
 659        struct lu_env *env;
 660        struct cl_env *cle;
 661
 662        cle = kmem_cache_zalloc(cl_env_kmem, GFP_NOFS);
 663        if (cle) {
 664                int rc;
 665
 666                INIT_LIST_HEAD(&cle->ce_linkage);
 667                cle->ce_magic = &cl_env_init0;
 668                env = &cle->ce_lu;
 669                rc = lu_env_init(env, ctx_tags | LCT_CL_THREAD);
 670                if (rc == 0) {
 671                        rc = lu_context_init(&cle->ce_ses,
 672                                             ses_tags | LCT_SESSION);
 673                        if (rc == 0) {
 674                                lu_context_enter(&cle->ce_ses);
 675                                env->le_ses = &cle->ce_ses;
 676                                cl_env_init0(cle, debug);
 677                        } else
 678                                lu_env_fini(env);
 679                }
 680                if (rc != 0) {
 681                        kmem_cache_free(cl_env_kmem, cle);
 682                        env = ERR_PTR(rc);
 683                } else {
 684                        CL_ENV_INC(create);
 685                        CL_ENV_INC(total);
 686                }
 687        } else
 688                env = ERR_PTR(-ENOMEM);
 689        return env;
 690}
 691
 692static void cl_env_fini(struct cl_env *cle)
 693{
 694        CL_ENV_DEC(total);
 695        lu_context_fini(&cle->ce_lu.le_ctx);
 696        lu_context_fini(&cle->ce_ses);
 697        kmem_cache_free(cl_env_kmem, cle);
 698}
 699
 700static inline struct cl_env *cl_env_container(struct lu_env *env)
 701{
 702        return container_of(env, struct cl_env, ce_lu);
 703}
 704
 705static struct lu_env *cl_env_peek(int *refcheck)
 706{
 707        struct lu_env *env;
 708        struct cl_env *cle;
 709
 710        CL_ENV_INC(lookup);
 711
 712        /* check that we don't go far from untrusted pointer */
 713        CLASSERT(offsetof(struct cl_env, ce_magic) == 0);
 714
 715        env = NULL;
 716        cle = cl_env_fetch();
 717        if (cle) {
 718                CL_ENV_INC(hit);
 719                env = &cle->ce_lu;
 720                *refcheck = ++cle->ce_ref;
 721        }
 722        CDEBUG(D_OTHER, "%d@%p\n", cle ? cle->ce_ref : 0, cle);
 723        return env;
 724}
 725
 726/**
 727 * Returns lu_env: if there already is an environment associated with the
 728 * current thread, it is returned, otherwise, new environment is allocated.
 729 *
 730 * \param refcheck pointer to a counter used to detect environment leaks. In
 731 * the usual case cl_env_get() and cl_env_put() are called in the same lexical
 732 * scope and pointer to the same integer is passed as \a refcheck. This is
 733 * used to detect missed cl_env_put().
 734 *
 735 * \see cl_env_put()
 736 */
 737struct lu_env *cl_env_get(int *refcheck)
 738{
 739        struct lu_env *env;
 740
 741        env = cl_env_peek(refcheck);
 742        if (!env) {
 743                env = cl_env_new(lu_context_tags_default,
 744                                 lu_session_tags_default,
 745                                 __builtin_return_address(0));
 746
 747                if (!IS_ERR(env)) {
 748                        struct cl_env *cle;
 749
 750                        cle = cl_env_container(env);
 751                        cl_env_attach(cle);
 752                        *refcheck = cle->ce_ref;
 753                        CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
 754                }
 755        }
 756        return env;
 757}
 758EXPORT_SYMBOL(cl_env_get);
 759
 760/**
 761 * Forces an allocation of a fresh environment with given tags.
 762 *
 763 * \see cl_env_get()
 764 */
 765struct lu_env *cl_env_alloc(int *refcheck, __u32 tags)
 766{
 767        struct lu_env *env;
 768
 769        LASSERT(!cl_env_peek(refcheck));
 770        env = cl_env_new(tags, tags, __builtin_return_address(0));
 771        if (!IS_ERR(env)) {
 772                struct cl_env *cle;
 773
 774                cle = cl_env_container(env);
 775                *refcheck = cle->ce_ref;
 776                CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
 777        }
 778        return env;
 779}
 780EXPORT_SYMBOL(cl_env_alloc);
 781
 782static void cl_env_exit(struct cl_env *cle)
 783{
 784        LASSERT(!cle->ce_owner);
 785        lu_context_exit(&cle->ce_lu.le_ctx);
 786        lu_context_exit(&cle->ce_ses);
 787}
 788
 789/**
 790 * Release an environment.
 791 *
 792 * Decrement \a env reference counter. When counter drops to 0, nothing in
 793 * this thread is using environment and it is returned to the allocation
 794 * cache, or freed straight away, if cache is large enough.
 795 */
 796void cl_env_put(struct lu_env *env, int *refcheck)
 797{
 798        struct cl_env *cle;
 799
 800        cle = cl_env_container(env);
 801
 802        LASSERT(cle->ce_ref > 0);
 803        LASSERT(ergo(refcheck, cle->ce_ref == *refcheck));
 804
 805        CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
 806        if (--cle->ce_ref == 0) {
 807                CL_ENV_DEC(busy);
 808                cl_env_detach(cle);
 809                cle->ce_debug = NULL;
 810                cl_env_exit(cle);
 811                cl_env_fini(cle);
 812        }
 813}
 814EXPORT_SYMBOL(cl_env_put);
 815
 816/**
 817 * Declares a point of re-entrancy.
 818 *
 819 * \see cl_env_reexit()
 820 */
 821void *cl_env_reenter(void)
 822{
 823        return cl_env_detach(NULL);
 824}
 825EXPORT_SYMBOL(cl_env_reenter);
 826
 827/**
 828 * Exits re-entrancy.
 829 */
 830void cl_env_reexit(void *cookie)
 831{
 832        cl_env_detach(NULL);
 833        cl_env_attach(cookie);
 834}
 835EXPORT_SYMBOL(cl_env_reexit);
 836
 837/**
 838 * Setup user-supplied \a env as a current environment. This is to be used to
 839 * guaranteed that environment exists even when cl_env_get() fails. It is up
 840 * to user to ensure proper concurrency control.
 841 *
 842 * \see cl_env_unplant()
 843 */
 844void cl_env_implant(struct lu_env *env, int *refcheck)
 845{
 846        struct cl_env *cle = cl_env_container(env);
 847
 848        LASSERT(cle->ce_ref > 0);
 849
 850        cl_env_attach(cle);
 851        cl_env_get(refcheck);
 852        CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
 853}
 854EXPORT_SYMBOL(cl_env_implant);
 855
 856/**
 857 * Detach environment installed earlier by cl_env_implant().
 858 */
 859void cl_env_unplant(struct lu_env *env, int *refcheck)
 860{
 861        struct cl_env *cle = cl_env_container(env);
 862
 863        LASSERT(cle->ce_ref > 1);
 864
 865        CDEBUG(D_OTHER, "%d@%p\n", cle->ce_ref, cle);
 866
 867        cl_env_detach(cle);
 868        cl_env_put(env, refcheck);
 869}
 870EXPORT_SYMBOL(cl_env_unplant);
 871
 872struct lu_env *cl_env_nested_get(struct cl_env_nest *nest)
 873{
 874        struct lu_env *env;
 875
 876        nest->cen_cookie = NULL;
 877        env = cl_env_peek(&nest->cen_refcheck);
 878        if (env) {
 879                if (!cl_io_is_going(env))
 880                        return env;
 881                cl_env_put(env, &nest->cen_refcheck);
 882                nest->cen_cookie = cl_env_reenter();
 883        }
 884        env = cl_env_get(&nest->cen_refcheck);
 885        if (IS_ERR(env)) {
 886                cl_env_reexit(nest->cen_cookie);
 887                return env;
 888        }
 889
 890        LASSERT(!cl_io_is_going(env));
 891        return env;
 892}
 893EXPORT_SYMBOL(cl_env_nested_get);
 894
 895void cl_env_nested_put(struct cl_env_nest *nest, struct lu_env *env)
 896{
 897        cl_env_put(env, &nest->cen_refcheck);
 898        cl_env_reexit(nest->cen_cookie);
 899}
 900EXPORT_SYMBOL(cl_env_nested_put);
 901
 902/**
 903 * Converts struct ost_lvb to struct cl_attr.
 904 *
 905 * \see cl_attr2lvb
 906 */
 907void cl_lvb2attr(struct cl_attr *attr, const struct ost_lvb *lvb)
 908{
 909        attr->cat_size   = lvb->lvb_size;
 910        attr->cat_mtime  = lvb->lvb_mtime;
 911        attr->cat_atime  = lvb->lvb_atime;
 912        attr->cat_ctime  = lvb->lvb_ctime;
 913        attr->cat_blocks = lvb->lvb_blocks;
 914}
 915EXPORT_SYMBOL(cl_lvb2attr);
 916
 917/*****************************************************************************
 918 *
 919 * Temporary prototype thing: mirror obd-devices into cl devices.
 920 *
 921 */
 922
 923struct cl_device *cl_type_setup(const struct lu_env *env, struct lu_site *site,
 924                                struct lu_device_type *ldt,
 925                                struct lu_device *next)
 926{
 927        const char       *typename;
 928        struct lu_device *d;
 929
 930        typename = ldt->ldt_name;
 931        d = ldt->ldt_ops->ldto_device_alloc(env, ldt, NULL);
 932        if (!IS_ERR(d)) {
 933                int rc;
 934
 935                if (site)
 936                        d->ld_site = site;
 937                rc = ldt->ldt_ops->ldto_device_init(env, d, typename, next);
 938                if (rc == 0) {
 939                        lu_device_get(d);
 940                        lu_ref_add(&d->ld_reference,
 941                                   "lu-stack", &lu_site_init);
 942                } else {
 943                        ldt->ldt_ops->ldto_device_free(env, d);
 944                        CERROR("can't init device '%s', %d\n", typename, rc);
 945                        d = ERR_PTR(rc);
 946                }
 947        } else
 948                CERROR("Cannot allocate device: '%s'\n", typename);
 949        return lu2cl_dev(d);
 950}
 951EXPORT_SYMBOL(cl_type_setup);
 952
 953/**
 954 * Finalize device stack by calling lu_stack_fini().
 955 */
 956void cl_stack_fini(const struct lu_env *env, struct cl_device *cl)
 957{
 958        lu_stack_fini(env, cl2lu_dev(cl));
 959}
 960EXPORT_SYMBOL(cl_stack_fini);
 961
 962int  cl_lock_init(void);
 963void cl_lock_fini(void);
 964
 965int  cl_page_init(void);
 966void cl_page_fini(void);
 967
 968static struct lu_context_key cl_key;
 969
 970struct cl_thread_info *cl_env_info(const struct lu_env *env)
 971{
 972        return lu_context_key_get(&env->le_ctx, &cl_key);
 973}
 974
 975/* defines cl0_key_{init,fini}() */
 976LU_KEY_INIT_FINI(cl0, struct cl_thread_info);
 977
 978static void *cl_key_init(const struct lu_context *ctx,
 979                         struct lu_context_key *key)
 980{
 981        struct cl_thread_info *info;
 982
 983        info = cl0_key_init(ctx, key);
 984        if (!IS_ERR(info)) {
 985                int i;
 986
 987                for (i = 0; i < ARRAY_SIZE(info->clt_counters); ++i)
 988                        lu_ref_init(&info->clt_counters[i].ctc_locks_locked);
 989        }
 990        return info;
 991}
 992
 993static void cl_key_fini(const struct lu_context *ctx,
 994                        struct lu_context_key *key, void *data)
 995{
 996        struct cl_thread_info *info;
 997        int i;
 998
 999        info = data;
1000        for (i = 0; i < ARRAY_SIZE(info->clt_counters); ++i)
1001                lu_ref_fini(&info->clt_counters[i].ctc_locks_locked);
1002        cl0_key_fini(ctx, key, data);
1003}
1004
1005static void cl_key_exit(const struct lu_context *ctx,
1006                        struct lu_context_key *key, void *data)
1007{
1008        struct cl_thread_info *info = data;
1009        int i;
1010
1011        for (i = 0; i < ARRAY_SIZE(info->clt_counters); ++i) {
1012                LASSERT(info->clt_counters[i].ctc_nr_held == 0);
1013                LASSERT(info->clt_counters[i].ctc_nr_used == 0);
1014                LASSERT(info->clt_counters[i].ctc_nr_locks_acquired == 0);
1015                LASSERT(info->clt_counters[i].ctc_nr_locks_locked == 0);
1016                lu_ref_fini(&info->clt_counters[i].ctc_locks_locked);
1017                lu_ref_init(&info->clt_counters[i].ctc_locks_locked);
1018        }
1019}
1020
1021static struct lu_context_key cl_key = {
1022        .lct_tags = LCT_CL_THREAD,
1023        .lct_init = cl_key_init,
1024        .lct_fini = cl_key_fini,
1025        .lct_exit = cl_key_exit
1026};
1027
1028static struct lu_kmem_descr cl_object_caches[] = {
1029        {
1030                .ckd_cache = &cl_env_kmem,
1031                .ckd_name  = "cl_env_kmem",
1032                .ckd_size  = sizeof(struct cl_env)
1033        },
1034        {
1035                .ckd_cache = NULL
1036        }
1037};
1038
1039/**
1040 * Global initialization of cl-data. Create kmem caches, register
1041 * lu_context_key's, etc.
1042 *
1043 * \see cl_global_fini()
1044 */
1045int cl_global_init(void)
1046{
1047        int result;
1048
1049        result = cl_env_store_init();
1050        if (result)
1051                return result;
1052
1053        result = lu_kmem_init(cl_object_caches);
1054        if (result)
1055                goto out_store;
1056
1057        LU_CONTEXT_KEY_INIT(&cl_key);
1058        result = lu_context_key_register(&cl_key);
1059        if (result)
1060                goto out_kmem;
1061
1062        result = cl_lock_init();
1063        if (result)
1064                goto out_context;
1065
1066        result = cl_page_init();
1067        if (result)
1068                goto out_lock;
1069
1070        return 0;
1071out_lock:
1072        cl_lock_fini();
1073out_context:
1074        lu_context_key_degister(&cl_key);
1075out_kmem:
1076        lu_kmem_fini(cl_object_caches);
1077out_store:
1078        cl_env_store_fini();
1079        return result;
1080}
1081
1082/**
1083 * Finalization of global cl-data. Dual to cl_global_init().
1084 */
1085void cl_global_fini(void)
1086{
1087        cl_lock_fini();
1088        cl_page_fini();
1089        lu_context_key_degister(&cl_key);
1090        lu_kmem_fini(cl_object_caches);
1091        cl_env_store_fini();
1092}
1093