Whamcloud - gitweb
LU-2139 osc: Use SOFT_SYNC to urge server commit
[fs/lustre-release.git] / lustre / osc / osc_page.c
index bc234e2..affd0d2 100644 (file)
@@ -27,7 +27,7 @@
  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
  * Use is subject to license terms.
  *
- * Copyright (c) 2011, Whamcloud, Inc.
+ * Copyright (c) 2011, 2013, Intel Corporation.
  */
 /*
  * This file is part of Lustre, http://www.lustre.org/
 
 #include "osc_cl_internal.h"
 
-/** \addtogroup osc 
- *  @{ 
+static void osc_lru_del(struct client_obd *cli, struct osc_page *opg, bool del);
+static void osc_lru_add(struct client_obd *cli, struct osc_page *opg);
+static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
+                          struct osc_page *opg);
+
+/** \addtogroup osc
+ *  @{
  */
 
-/* 
+/*
  * Comment out osc_page_protected because it may sleep inside the
  * the client_obd_list_lock.
  * client_obd_list_lock -> osc_ap_completion -> osc_completion ->
@@ -58,23 +63,23 @@ static int osc_page_is_dlocked(const struct lu_env *env,
                                const struct osc_page *opg,
                                enum cl_lock_mode mode, int pending, int unref)
 {
-        struct cl_page         *page;
-        struct osc_object      *obj;
-        struct osc_thread_info *info;
-        struct ldlm_res_id     *resname;
-        struct lustre_handle   *lockh;
-        ldlm_policy_data_t     *policy;
-        ldlm_mode_t             dlmmode;
-        int                     flags;
-
-        cfs_might_sleep();
-
-        info = osc_env_info(env);
-        resname = &info->oti_resname;
-        policy = &info->oti_policy;
-        lockh = &info->oti_handle;
-        page = opg->ops_cl.cpl_page;
-        obj = cl2osc(opg->ops_cl.cpl_obj);
+       struct cl_page         *page;
+       struct osc_object      *obj;
+       struct osc_thread_info *info;
+       struct ldlm_res_id     *resname;
+       struct lustre_handle   *lockh;
+       ldlm_policy_data_t     *policy;
+       ldlm_mode_t             dlmmode;
+       __u64                   flags;
+
+       might_sleep();
+
+       info = osc_env_info(env);
+       resname = &info->oti_resname;
+       policy = &info->oti_policy;
+       lockh = &info->oti_handle;
+       page = opg->ops_cl.cpl_page;
+       obj = cl2osc(opg->ops_cl.cpl_obj);
 
         flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
         if (pending)
@@ -121,7 +126,7 @@ static int osc_page_protected(const struct lu_env *env,
                 descr->cld_mode = mode;
                 descr->cld_start = page->cp_index;
                 descr->cld_end   = page->cp_index;
-                cfs_spin_lock(&hdr->coh_lock_guard);
+               spin_lock(&hdr->coh_lock_guard);
                 cfs_list_for_each_entry(scan, &hdr->coh_locks, cll_linkage) {
                         /*
                          * Lock-less sub-lock has to be either in HELD state
@@ -139,7 +144,7 @@ static int osc_page_protected(const struct lu_env *env,
                                 break;
                         }
                 }
-                cfs_spin_unlock(&hdr->coh_lock_guard);
+               spin_unlock(&hdr->coh_lock_guard);
         }
         return result;
 }
@@ -163,7 +168,6 @@ static void osc_page_fini(const struct lu_env *env,
         struct osc_page *opg = cl2osc_page(slice);
         CDEBUG(D_TRACE, "%p\n", opg);
         LASSERT(opg->ops_lock == NULL);
-        OBD_SLAB_FREE_PTR(opg, osc_page_kmem);
 }
 
 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
@@ -196,13 +200,16 @@ static void osc_page_transfer_put(const struct lu_env *env,
 static void osc_page_transfer_add(const struct lu_env *env,
                                   struct osc_page *opg, enum cl_req_type crt)
 {
-        struct osc_object *obj;
+       struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
 
-        obj = cl2osc(opg->ops_cl.cpl_obj);
-        cfs_spin_lock(&obj->oo_seatbelt);
-        cfs_list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
-        opg->ops_submitter = cfs_current();
-        cfs_spin_unlock(&obj->oo_seatbelt);
+       /* ops_lru and ops_inflight share the same field, so take it from LRU
+        * first and then use it as inflight. */
+       osc_lru_del(osc_cli(obj), opg, false);
+
+       spin_lock(&obj->oo_seatbelt);
+       cfs_list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
+       opg->ops_submitter = current;
+       spin_unlock(&obj->oo_seatbelt);
 }
 
 static int osc_page_cache_add(const struct lu_env *env,
@@ -256,9 +263,9 @@ static int osc_page_addref_lock(const struct lu_env *env,
         olock = osc_lock_at(lock);
         if (cfs_atomic_inc_return(&olock->ols_pageref) <= 0) {
                 cfs_atomic_dec(&olock->ols_pageref);
-                cl_lock_put(env, lock);
-                rc = 1;
+                rc = -ENODATA;
         } else {
+               cl_lock_get(lock);
                 opg->ops_lock = lock;
                 rc = 0;
         }
@@ -285,16 +292,16 @@ static int osc_page_is_under_lock(const struct lu_env *env,
                                   struct cl_io *unused)
 {
         struct cl_lock *lock;
-        int             result;
+        int             result = -ENODATA;
 
         ENTRY;
         lock = cl_lock_at_page(env, slice->cpl_obj, slice->cpl_page,
                                NULL, 1, 0);
-        if (lock != NULL &&
-            osc_page_addref_lock(env, cl2osc_page(slice), lock) == 0)
-                result = -EBUSY;
-        else
-                result = -ENODATA;
+        if (lock != NULL) {
+               if (osc_page_addref_lock(env, cl2osc_page(slice), lock) == 0)
+                       result = -EBUSY;
+               cl_lock_put(env, lock);
+       }
         RETURN(result);
 }
 
@@ -312,10 +319,22 @@ static void osc_page_completion_read(const struct lu_env *env,
                                      const struct cl_page_slice *slice,
                                      int ioret)
 {
-        struct osc_page *opg = cl2osc_page(slice);
+       struct osc_page   *opg = cl2osc_page(slice);
+       struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
 
-        if (likely(opg->ops_lock))
-                osc_page_putref_lock(env, opg);
+       if (likely(opg->ops_lock))
+               osc_page_putref_lock(env, opg);
+       osc_lru_add(osc_cli(obj), opg);
+}
+
+static void osc_page_completion_write(const struct lu_env *env,
+                                     const struct cl_page_slice *slice,
+                                     int ioret)
+{
+       struct osc_page   *opg = cl2osc_page(slice);
+       struct osc_object *obj = cl2osc(slice->cpl_obj);
+
+       osc_lru_add(osc_cli(obj), opg);
 }
 
 static int osc_page_fail(const struct lu_env *env,
@@ -411,10 +430,17 @@ static void osc_page_delete(const struct lu_env *env,
                               "Trying to teardown failed: %d\n", rc);
                 LASSERT(0);
         }
-        cfs_spin_lock(&obj->oo_seatbelt);
-        cfs_list_del_init(&opg->ops_inflight);
-        cfs_spin_unlock(&obj->oo_seatbelt);
-        EXIT;
+
+       spin_lock(&obj->oo_seatbelt);
+       if (opg->ops_submitter != NULL) {
+               LASSERT(!cfs_list_empty(&opg->ops_inflight));
+               cfs_list_del_init(&opg->ops_inflight);
+               opg->ops_submitter = NULL;
+       }
+       spin_unlock(&obj->oo_seatbelt);
+
+       osc_lru_del(osc_cli(obj), opg, true);
+       EXIT;
 }
 
 void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
@@ -427,9 +453,9 @@ void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
 
         opg->ops_from = from;
         opg->ops_to   = to;
-        cfs_spin_lock(&oap->oap_lock);
-        oap->oap_async_flags |= ASYNC_COUNT_STABLE;
-        cfs_spin_unlock(&oap->oap_lock);
+       spin_lock(&oap->oap_lock);
+       oap->oap_async_flags |= ASYNC_COUNT_STABLE;
+       spin_unlock(&oap->oap_lock);
 }
 
 static int osc_page_cancel(const struct lu_env *env,
@@ -472,46 +498,74 @@ static const struct cl_page_operations osc_page_ops = {
                         .cpo_completion = osc_page_completion_read
                 },
                 [CRT_WRITE] = {
-                        .cpo_cache_add  = osc_page_cache_add
-                }
-        },
-        .cpo_clip           = osc_page_clip,
+                       .cpo_cache_add  = osc_page_cache_add,
+                       .cpo_completion = osc_page_completion_write
+               }
+       },
+       .cpo_clip           = osc_page_clip,
        .cpo_cancel         = osc_page_cancel,
        .cpo_flush          = osc_page_flush
 };
 
-struct cl_page *osc_page_init(const struct lu_env *env,
-                              struct cl_object *obj,
-                              struct cl_page *page, cfs_page_t *vmpage)
+int osc_page_init(const struct lu_env *env, struct cl_object *obj,
+               struct cl_page *page, struct page *vmpage)
 {
-        struct osc_object *osc = cl2osc(obj);
-        struct osc_page   *opg;
-        int result;
+       struct osc_object *osc = cl2osc(obj);
+       struct osc_page   *opg = cl_object_page_slice(obj, page);
+       int result;
 
-        OBD_SLAB_ALLOC_PTR_GFP(opg, osc_page_kmem, CFS_ALLOC_IO);
-        if (opg != NULL) {
-                opg->ops_from = 0;
-                opg->ops_to   = CFS_PAGE_SIZE;
-
-               result = osc_prep_async_page(osc, opg, vmpage,
-                                            cl_offset(obj, page->cp_index));
-                if (result == 0) {
-                        struct osc_io *oio = osc_env_io(env);
-                        opg->ops_srvlock = osc_io_srvlock(oio);
-                        cl_page_slice_add(page, &opg->ops_cl, obj,
-                                          &osc_page_ops);
-                }
-                /*
-                 * Cannot assert osc_page_protected() here as read-ahead
-                 * creates temporary pages outside of a lock.
-                 */
-#ifdef INVARIANT_CHECK
-                opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
+       opg->ops_from = 0;
+       opg->ops_to   = PAGE_CACHE_SIZE;
+
+       result = osc_prep_async_page(osc, opg, vmpage,
+                                       cl_offset(obj, page->cp_index));
+       if (result == 0) {
+               struct osc_io *oio = osc_env_io(env);
+               opg->ops_srvlock = osc_io_srvlock(oio);
+               cl_page_slice_add(page, &opg->ops_cl, obj,
+                               &osc_page_ops);
+       }
+       /*
+        * Cannot assert osc_page_protected() here as read-ahead
+        * creates temporary pages outside of a lock.
+        */
+#ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
+       opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
 #endif
-                CFS_INIT_LIST_HEAD(&opg->ops_inflight);
-        } else
-                result = -ENOMEM;
-        return ERR_PTR(result);
+       /* ops_inflight and ops_lru are the same field, but it doesn't
+        * hurt to initialize it twice :-) */
+       CFS_INIT_LIST_HEAD(&opg->ops_inflight);
+       CFS_INIT_LIST_HEAD(&opg->ops_lru);
+
+       /* reserve an LRU space for this page */
+       if (page->cp_type == CPT_CACHEABLE && result == 0)
+               result = osc_lru_reserve(env, osc, opg);
+
+       return result;
+}
+
+int osc_over_unstable_soft_limit(struct client_obd *cli)
+{
+       long obd_upages, obd_dpages, osc_upages;
+
+       /* Can't check cli->cl_unstable_count, therefore, no soft limit */
+       if (cli == NULL)
+               return 0;
+
+       obd_upages = cfs_atomic_read(&obd_unstable_pages);
+       obd_dpages = cfs_atomic_read(&obd_dirty_pages);
+
+       osc_upages = cfs_atomic_read(&cli->cl_unstable_count);
+
+       /* obd_max_dirty_pages is the max number of (dirty + unstable)
+        * pages allowed at any given time. To simulate an unstable page
+        * only limit, we subtract the current number of dirty pages
+        * from this max. This difference is roughly the amount of pages
+        * currently available for unstable pages. Thus, the soft limit
+        * is half of that difference. Check osc_upages to ensure we don't
+        * set SOFT_SYNC for OSCs without any outstanding unstable pages. */
+       return osc_upages != 0 &&
+              obd_upages >= (obd_max_dirty_pages - obd_dpages) / 2;
 }
 
 /**
@@ -537,6 +591,9 @@ void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
        oap->oap_count     = opg->ops_to - opg->ops_from;
        oap->oap_brw_flags = OBD_BRW_SYNC | brw_flags;
 
+       if (osc_over_unstable_soft_limit(oap->oap_cli))
+               oap->oap_brw_flags |= OBD_BRW_SOFT_SYNC;
+
        if (!client_is_remote(osc_export(obj)) &&
                        cfs_capable(CFS_CAP_SYS_RESOURCE)) {
                oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
@@ -548,4 +605,353 @@ void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
        osc_page_transfer_add(env, opg, crt);
 }
 
+/* --------------- LRU page management ------------------ */
+
+/* OSC is a natural place to manage LRU pages as applications are specialized
+ * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
+ * occupy more LRU slots. On the other hand, we should avoid using up all LRU
+ * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
+ * for free LRU slots - this will be very bad so the algorithm requires each
+ * OSC to free slots voluntarily to maintain a reasonable number of free slots
+ * at any time.
+ */
+
+static CFS_DECL_WAITQ(osc_lru_waitq);
+static cfs_atomic_t osc_lru_waiters = CFS_ATOMIC_INIT(0);
+/* LRU pages are freed in batch mode. OSC should at least free this
+ * number of pages to avoid running out of LRU budget, and.. */
+static const int lru_shrink_min = 2 << (20 - PAGE_CACHE_SHIFT);  /* 2M */
+/* free this number at most otherwise it will take too long time to finsih. */
+static const int lru_shrink_max = 32 << (20 - PAGE_CACHE_SHIFT); /* 32M */
+
+/* Check if we can free LRU slots from this OSC. If there exists LRU waiters,
+ * we should free slots aggressively. In this way, slots are freed in a steady
+ * step to maintain fairness among OSCs.
+ *
+ * Return how many LRU pages should be freed. */
+static int osc_cache_too_much(struct client_obd *cli)
+{
+       struct cl_client_cache *cache = cli->cl_cache;
+       int pages = cfs_atomic_read(&cli->cl_lru_in_list) >> 1;
+
+       if (cfs_atomic_read(&osc_lru_waiters) > 0 &&
+           cfs_atomic_read(cli->cl_lru_left) < lru_shrink_max)
+               /* drop lru pages aggressively */
+               return min(pages, lru_shrink_max);
+
+       /* if it's going to run out LRU slots, we should free some, but not
+        * too much to maintain faireness among OSCs. */
+       if (cfs_atomic_read(cli->cl_lru_left) < cache->ccc_lru_max >> 4) {
+               unsigned long tmp;
+
+               tmp = cache->ccc_lru_max / cfs_atomic_read(&cache->ccc_users);
+               if (pages > tmp)
+                       return min(pages, lru_shrink_max);
+
+               return pages > lru_shrink_min ? lru_shrink_min : 0;
+       }
+
+       return 0;
+}
+
+/* Return how many pages are not discarded in @pvec. */
+static int discard_pagevec(const struct lu_env *env, struct cl_io *io,
+                          struct cl_page **pvec, int max_index)
+{
+       int count;
+       int i;
+
+       for (count = 0, i = 0; i < max_index; i++) {
+               struct cl_page *page = pvec[i];
+               if (cl_page_own_try(env, io, page) == 0) {
+                       /* free LRU page only if nobody is using it.
+                        * This check is necessary to avoid freeing the pages
+                        * having already been removed from LRU and pinned
+                        * for IO. */
+                       if (!cl_page_in_use(page)) {
+                               cl_page_unmap(env, io, page);
+                               cl_page_discard(env, io, page);
+                               ++count;
+                       }
+                       cl_page_disown(env, io, page);
+               }
+               cl_page_put(env, page);
+               pvec[i] = NULL;
+       }
+       return max_index - count;
+}
+
+/**
+ * Drop @target of pages from LRU at most.
+ */
+int osc_lru_shrink(struct client_obd *cli, int target)
+{
+       struct cl_env_nest nest;
+       struct lu_env *env;
+       struct cl_io *io;
+       struct cl_object *clobj = NULL;
+       struct cl_page **pvec;
+       struct osc_page *opg;
+       int maxscan = 0;
+       int count = 0;
+       int index = 0;
+       int rc = 0;
+       ENTRY;
+
+       LASSERT(cfs_atomic_read(&cli->cl_lru_in_list) >= 0);
+       if (cfs_atomic_read(&cli->cl_lru_in_list) == 0 || target <= 0)
+               RETURN(0);
+
+       env = cl_env_nested_get(&nest);
+       if (IS_ERR(env))
+               RETURN(PTR_ERR(env));
+
+       pvec = osc_env_info(env)->oti_pvec;
+       io = &osc_env_info(env)->oti_io;
+
+       client_obd_list_lock(&cli->cl_lru_list_lock);
+       cfs_atomic_inc(&cli->cl_lru_shrinkers);
+       maxscan = min(target << 1, cfs_atomic_read(&cli->cl_lru_in_list));
+       while (!cfs_list_empty(&cli->cl_lru_list)) {
+               struct cl_page *page;
+
+               if (--maxscan < 0)
+                       break;
+
+               opg = cfs_list_entry(cli->cl_lru_list.next, struct osc_page,
+                                    ops_lru);
+               page = cl_page_top(opg->ops_cl.cpl_page);
+               if (cl_page_in_use_noref(page)) {
+                       cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
+                       continue;
+               }
+
+               LASSERT(page->cp_obj != NULL);
+               if (clobj != page->cp_obj) {
+                       struct cl_object *tmp = page->cp_obj;
+
+                       cl_object_get(tmp);
+                       client_obd_list_unlock(&cli->cl_lru_list_lock);
+
+                       if (clobj != NULL) {
+                               count -= discard_pagevec(env, io, pvec, index);
+                               index = 0;
+
+                               cl_io_fini(env, io);
+                               cl_object_put(env, clobj);
+                               clobj = NULL;
+                       }
+
+                       clobj = tmp;
+                       io->ci_obj = clobj;
+                       io->ci_ignore_layout = 1;
+                       rc = cl_io_init(env, io, CIT_MISC, clobj);
+
+                       client_obd_list_lock(&cli->cl_lru_list_lock);
+
+                       if (rc != 0)
+                               break;
+
+                       ++maxscan;
+                       continue;
+               }
+
+               /* move this page to the end of list as it will be discarded
+                * soon. The page will be finally removed from LRU list in
+                * osc_page_delete().  */
+               cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
+
+               /* it's okay to grab a refcount here w/o holding lock because
+                * it has to grab cl_lru_list_lock to delete the page. */
+               cl_page_get(page);
+               pvec[index++] = page;
+               if (++count >= target)
+                       break;
+
+               if (unlikely(index == OTI_PVEC_SIZE)) {
+                       client_obd_list_unlock(&cli->cl_lru_list_lock);
+                       count -= discard_pagevec(env, io, pvec, index);
+                       index = 0;
+
+                       client_obd_list_lock(&cli->cl_lru_list_lock);
+               }
+       }
+       client_obd_list_unlock(&cli->cl_lru_list_lock);
+
+       if (clobj != NULL) {
+               count -= discard_pagevec(env, io, pvec, index);
+
+               cl_io_fini(env, io);
+               cl_object_put(env, clobj);
+       }
+       cl_env_nested_put(&nest, env);
+
+       cfs_atomic_dec(&cli->cl_lru_shrinkers);
+       RETURN(count > 0 ? count : rc);
+}
+
+static void osc_lru_add(struct client_obd *cli, struct osc_page *opg)
+{
+       bool wakeup = false;
+
+       if (!opg->ops_in_lru)
+               return;
+
+       cfs_atomic_dec(&cli->cl_lru_busy);
+       client_obd_list_lock(&cli->cl_lru_list_lock);
+       if (cfs_list_empty(&opg->ops_lru)) {
+               cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
+               cfs_atomic_inc_return(&cli->cl_lru_in_list);
+               wakeup = cfs_atomic_read(&osc_lru_waiters) > 0;
+       }
+       client_obd_list_unlock(&cli->cl_lru_list_lock);
+
+       if (wakeup) {
+               osc_lru_shrink(cli, osc_cache_too_much(cli));
+               wake_up_all(&osc_lru_waitq);
+       }
+}
+
+/* delete page from LRUlist. The page can be deleted from LRUlist for two
+ * reasons: redirtied or deleted from page cache. */
+static void osc_lru_del(struct client_obd *cli, struct osc_page *opg, bool del)
+{
+       if (opg->ops_in_lru) {
+               client_obd_list_lock(&cli->cl_lru_list_lock);
+               if (!cfs_list_empty(&opg->ops_lru)) {
+                       LASSERT(cfs_atomic_read(&cli->cl_lru_in_list) > 0);
+                       cfs_list_del_init(&opg->ops_lru);
+                       cfs_atomic_dec(&cli->cl_lru_in_list);
+                       if (!del)
+                               cfs_atomic_inc(&cli->cl_lru_busy);
+               } else if (del) {
+                       LASSERT(cfs_atomic_read(&cli->cl_lru_busy) > 0);
+                       cfs_atomic_dec(&cli->cl_lru_busy);
+               }
+               client_obd_list_unlock(&cli->cl_lru_list_lock);
+               if (del) {
+                       cfs_atomic_inc(cli->cl_lru_left);
+                       /* this is a great place to release more LRU pages if
+                        * this osc occupies too many LRU pages and kernel is
+                        * stealing one of them.
+                        * cl_lru_shrinkers is to avoid recursive call in case
+                        * we're already in the context of osc_lru_shrink(). */
+                       if (cfs_atomic_read(&cli->cl_lru_shrinkers) == 0 &&
+                           !memory_pressure_get())
+                               osc_lru_shrink(cli, osc_cache_too_much(cli));
+                       wake_up(&osc_lru_waitq);
+               }
+       } else {
+               LASSERT(cfs_list_empty(&opg->ops_lru));
+       }
+}
+
+static inline int max_to_shrink(struct client_obd *cli)
+{
+       return min(cfs_atomic_read(&cli->cl_lru_in_list) >> 1, lru_shrink_max);
+}
+
+static int osc_lru_reclaim(struct client_obd *cli)
+{
+       struct cl_client_cache *cache = cli->cl_cache;
+       int max_scans;
+       int rc;
+
+       LASSERT(cache != NULL);
+       LASSERT(!cfs_list_empty(&cache->ccc_lru));
+
+       rc = osc_lru_shrink(cli, lru_shrink_min);
+       if (rc != 0) {
+               CDEBUG(D_CACHE, "%s: Free %d pages from own LRU: %p.\n",
+                       cli->cl_import->imp_obd->obd_name, rc, cli);
+               return rc;
+       }
+
+       CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %d, busy: %d.\n",
+               cli->cl_import->imp_obd->obd_name, cli,
+               cfs_atomic_read(&cli->cl_lru_in_list),
+               cfs_atomic_read(&cli->cl_lru_busy));
+
+       /* Reclaim LRU slots from other client_obd as it can't free enough
+        * from its own. This should rarely happen. */
+       spin_lock(&cache->ccc_lru_lock);
+       cache->ccc_lru_shrinkers++;
+       cfs_list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
+
+       max_scans = cfs_atomic_read(&cache->ccc_users);
+       while (--max_scans > 0 && !cfs_list_empty(&cache->ccc_lru)) {
+               cli = cfs_list_entry(cache->ccc_lru.next, struct client_obd,
+                                       cl_lru_osc);
+
+               CDEBUG(D_CACHE, "%s: cli %p LRU pages: %d, busy: %d.\n",
+                       cli->cl_import->imp_obd->obd_name, cli,
+                       cfs_atomic_read(&cli->cl_lru_in_list),
+                       cfs_atomic_read(&cli->cl_lru_busy));
+
+               cfs_list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
+               if (cfs_atomic_read(&cli->cl_lru_in_list) > 0) {
+                       spin_unlock(&cache->ccc_lru_lock);
+
+                       rc = osc_lru_shrink(cli, max_to_shrink(cli));
+                       spin_lock(&cache->ccc_lru_lock);
+                       if (rc != 0)
+                               break;
+               }
+       }
+       spin_unlock(&cache->ccc_lru_lock);
+
+       CDEBUG(D_CACHE, "%s: cli %p freed %d pages.\n",
+               cli->cl_import->imp_obd->obd_name, cli, rc);
+       return rc;
+}
+
+static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
+                          struct osc_page *opg)
+{
+       struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
+       struct client_obd *cli = osc_cli(obj);
+       int rc = 0;
+       ENTRY;
+
+       if (cli->cl_cache == NULL) /* shall not be in LRU */
+               RETURN(0);
+
+       LASSERT(cfs_atomic_read(cli->cl_lru_left) >= 0);
+       while (!cfs_atomic_add_unless(cli->cl_lru_left, -1, 0)) {
+               int gen;
+
+               /* run out of LRU spaces, try to drop some by itself */
+               rc = osc_lru_reclaim(cli);
+               if (rc < 0)
+                       break;
+               if (rc > 0)
+                       continue;
+
+               cond_resched();
+
+               /* slowest case, all of caching pages are busy, notifying
+                * other OSCs that we're lack of LRU slots. */
+               cfs_atomic_inc(&osc_lru_waiters);
+
+               gen = cfs_atomic_read(&cli->cl_lru_in_list);
+               rc = l_wait_event(osc_lru_waitq,
+                               cfs_atomic_read(cli->cl_lru_left) > 0 ||
+                               (cfs_atomic_read(&cli->cl_lru_in_list) > 0 &&
+                                gen != cfs_atomic_read(&cli->cl_lru_in_list)),
+                               &lwi);
+
+               cfs_atomic_dec(&osc_lru_waiters);
+               if (rc < 0)
+                       break;
+       }
+
+       if (rc >= 0) {
+               cfs_atomic_inc(&cli->cl_lru_busy);
+               opg->ops_in_lru = 1;
+               rc = 0;
+       }
+
+       RETURN(rc);
+}
+
 /** @} osc */