4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * Implementation of cl_page for OSC layer.
38 * Author: Nikita Danilov <nikita.danilov@sun.com>
41 #define DEBUG_SUBSYSTEM S_OSC
43 #include "osc_cl_internal.h"
45 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg, bool del);
46 static void osc_lru_add(struct client_obd *cli, struct osc_page *opg);
47 static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
48 struct osc_page *opg);
55 * Comment out osc_page_protected because it may sleep inside the
56 * the client_obd_list_lock.
57 * client_obd_list_lock -> osc_ap_completion -> osc_completion ->
58 * -> osc_page_protected -> osc_page_is_dlocked -> osc_match_base
59 * -> ldlm_lock_match -> sptlrpc_import_check_ctx -> sleep.
62 static int osc_page_is_dlocked(const struct lu_env *env,
63 const struct osc_page *opg,
64 enum cl_lock_mode mode, int pending, int unref)
67 struct osc_object *obj;
68 struct osc_thread_info *info;
69 struct ldlm_res_id *resname;
70 struct lustre_handle *lockh;
71 ldlm_policy_data_t *policy;
77 info = osc_env_info(env);
78 resname = &info->oti_resname;
79 policy = &info->oti_policy;
80 lockh = &info->oti_handle;
81 page = opg->ops_cl.cpl_page;
82 obj = cl2osc(opg->ops_cl.cpl_obj);
84 flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
86 flags |= LDLM_FL_CBPENDING;
88 dlmmode = osc_cl_lock2ldlm(mode) | LCK_PW;
89 osc_lock_build_res(env, obj, resname);
90 osc_index2policy(policy, page->cp_obj, page->cp_index, page->cp_index);
91 return osc_match_base(osc_export(obj), resname, LDLM_EXTENT, policy,
92 dlmmode, &flags, NULL, lockh, unref);
96 * Checks an invariant that a page in the cache is covered by a lock, as
99 static int osc_page_protected(const struct lu_env *env,
100 const struct osc_page *opg,
101 enum cl_lock_mode mode, int unref)
103 struct cl_object_header *hdr;
104 struct cl_lock *scan;
105 struct cl_page *page;
106 struct cl_lock_descr *descr;
109 LINVRNT(!opg->ops_temp);
111 page = opg->ops_cl.cpl_page;
112 if (page->cp_owner != NULL &&
113 cl_io_top(page->cp_owner)->ci_lockreq == CILR_NEVER)
115 * If IO is done without locks (liblustre, or lloop), lock is
120 /* otherwise check for a DLM lock */
121 result = osc_page_is_dlocked(env, opg, mode, 1, unref);
123 /* maybe this page is a part of a lockless io? */
124 hdr = cl_object_header(opg->ops_cl.cpl_obj);
125 descr = &osc_env_info(env)->oti_descr;
126 descr->cld_mode = mode;
127 descr->cld_start = page->cp_index;
128 descr->cld_end = page->cp_index;
129 spin_lock(&hdr->coh_lock_guard);
130 cfs_list_for_each_entry(scan, &hdr->coh_locks, cll_linkage) {
132 * Lock-less sub-lock has to be either in HELD state
133 * (when io is actively going on), or in CACHED state,
134 * when top-lock is being unlocked:
135 * cl_io_unlock()->cl_unuse()->...->lov_lock_unuse().
137 if ((scan->cll_state == CLS_HELD ||
138 scan->cll_state == CLS_CACHED) &&
139 cl_lock_ext_match(&scan->cll_descr, descr)) {
140 struct osc_lock *olck;
142 olck = osc_lock_at(scan);
143 result = osc_lock_is_lockless(olck);
147 spin_unlock(&hdr->coh_lock_guard);
152 static int osc_page_protected(const struct lu_env *env,
153 const struct osc_page *opg,
154 enum cl_lock_mode mode, int unref)
160 /*****************************************************************************
165 static void osc_page_fini(const struct lu_env *env,
166 struct cl_page_slice *slice)
168 struct osc_page *opg = cl2osc_page(slice);
169 CDEBUG(D_TRACE, "%p\n", opg);
170 LASSERT(opg->ops_lock == NULL);
171 OBD_SLAB_FREE_PTR(opg, osc_page_kmem);
174 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
176 struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
178 LASSERT(!opg->ops_transfer_pinned);
180 lu_ref_add_atomic(&page->cp_reference, label, page);
181 opg->ops_transfer_pinned = 1;
184 static void osc_page_transfer_put(const struct lu_env *env,
185 struct osc_page *opg)
187 struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
189 if (opg->ops_transfer_pinned) {
190 lu_ref_del(&page->cp_reference, "transfer", page);
191 opg->ops_transfer_pinned = 0;
192 cl_page_put(env, page);
197 * This is called once for every page when it is submitted for a transfer
198 * either opportunistic (osc_page_cache_add()), or immediate
199 * (osc_page_submit()).
201 static void osc_page_transfer_add(const struct lu_env *env,
202 struct osc_page *opg, enum cl_req_type crt)
204 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
206 /* ops_lru and ops_inflight share the same field, so take it from LRU
207 * first and then use it as inflight. */
208 osc_lru_del(osc_cli(obj), opg, false);
210 spin_lock(&obj->oo_seatbelt);
211 cfs_list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
212 opg->ops_submitter = cfs_current();
213 spin_unlock(&obj->oo_seatbelt);
216 static int osc_page_cache_add(const struct lu_env *env,
217 const struct cl_page_slice *slice,
220 struct osc_io *oio = osc_env_io(env);
221 struct osc_page *opg = cl2osc_page(slice);
225 LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 0));
227 osc_page_transfer_get(opg, "transfer\0cache");
228 result = osc_queue_async_io(env, io, opg);
230 osc_page_transfer_put(env, opg);
232 osc_page_transfer_add(env, opg, CRT_WRITE);
234 /* for sync write, kernel will wait for this page to be flushed before
235 * osc_io_end() is called, so release it earlier.
236 * for mkwrite(), it's known there is no further pages. */
237 if (cl_io_is_sync_write(io) || cl_io_is_mkwrite(io)) {
238 if (oio->oi_active != NULL) {
239 osc_extent_release(env, oio->oi_active);
240 oio->oi_active = NULL;
247 void osc_index2policy(ldlm_policy_data_t *policy, const struct cl_object *obj,
248 pgoff_t start, pgoff_t end)
250 memset(policy, 0, sizeof *policy);
251 policy->l_extent.start = cl_offset(obj, start);
252 policy->l_extent.end = cl_offset(obj, end + 1) - 1;
255 static int osc_page_addref_lock(const struct lu_env *env,
256 struct osc_page *opg,
257 struct cl_lock *lock)
259 struct osc_lock *olock;
262 LASSERT(opg->ops_lock == NULL);
264 olock = osc_lock_at(lock);
265 if (cfs_atomic_inc_return(&olock->ols_pageref) <= 0) {
266 cfs_atomic_dec(&olock->ols_pageref);
270 opg->ops_lock = lock;
276 static void osc_page_putref_lock(const struct lu_env *env,
277 struct osc_page *opg)
279 struct cl_lock *lock = opg->ops_lock;
280 struct osc_lock *olock;
282 LASSERT(lock != NULL);
283 olock = osc_lock_at(lock);
285 cfs_atomic_dec(&olock->ols_pageref);
286 opg->ops_lock = NULL;
288 cl_lock_put(env, lock);
291 static int osc_page_is_under_lock(const struct lu_env *env,
292 const struct cl_page_slice *slice,
293 struct cl_io *unused)
295 struct cl_lock *lock;
296 int result = -ENODATA;
299 lock = cl_lock_at_page(env, slice->cpl_obj, slice->cpl_page,
302 if (osc_page_addref_lock(env, cl2osc_page(slice), lock) == 0)
304 cl_lock_put(env, lock);
309 static void osc_page_disown(const struct lu_env *env,
310 const struct cl_page_slice *slice,
313 struct osc_page *opg = cl2osc_page(slice);
315 if (unlikely(opg->ops_lock))
316 osc_page_putref_lock(env, opg);
319 static void osc_page_completion_read(const struct lu_env *env,
320 const struct cl_page_slice *slice,
323 struct osc_page *opg = cl2osc_page(slice);
324 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
326 if (likely(opg->ops_lock))
327 osc_page_putref_lock(env, opg);
328 osc_lru_add(osc_cli(obj), opg);
331 static void osc_page_completion_write(const struct lu_env *env,
332 const struct cl_page_slice *slice,
335 struct osc_page *opg = cl2osc_page(slice);
336 struct osc_object *obj = cl2osc(slice->cpl_obj);
338 osc_lru_add(osc_cli(obj), opg);
341 static int osc_page_fail(const struct lu_env *env,
342 const struct cl_page_slice *slice,
343 struct cl_io *unused)
353 static const char *osc_list(cfs_list_t *head)
355 return cfs_list_empty(head) ? "-" : "+";
358 static inline cfs_time_t osc_submit_duration(struct osc_page *opg)
360 if (opg->ops_submit_time == 0)
363 return (cfs_time_current() - opg->ops_submit_time);
366 static int osc_page_print(const struct lu_env *env,
367 const struct cl_page_slice *slice,
368 void *cookie, lu_printer_t printer)
370 struct osc_page *opg = cl2osc_page(slice);
371 struct osc_async_page *oap = &opg->ops_oap;
372 struct osc_object *obj = cl2osc(slice->cpl_obj);
373 struct client_obd *cli = &osc_export(obj)->exp_obd->u.cli;
375 return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p: "
376 "1< %#x %d %u %s %s > "
377 "2< "LPU64" %u %u %#x %#x | %p %p %p > "
378 "3< %s %p %d %lu %d > "
379 "4< %d %d %d %lu %s | %s %s %s %s > "
380 "5< %s %s %s %s | %d %s | %d %s %s>\n",
383 oap->oap_magic, oap->oap_cmd,
384 oap->oap_interrupted,
385 osc_list(&oap->oap_pending_item),
386 osc_list(&oap->oap_rpc_item),
388 oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
389 oap->oap_async_flags, oap->oap_brw_flags,
390 oap->oap_request, oap->oap_cli, obj,
392 osc_list(&opg->ops_inflight),
393 opg->ops_submitter, opg->ops_transfer_pinned,
394 osc_submit_duration(opg), opg->ops_srvlock,
396 cli->cl_r_in_flight, cli->cl_w_in_flight,
397 cli->cl_max_rpcs_in_flight,
399 osc_list(&cli->cl_cache_waiters),
400 osc_list(&cli->cl_loi_ready_list),
401 osc_list(&cli->cl_loi_hp_ready_list),
402 osc_list(&cli->cl_loi_write_list),
403 osc_list(&cli->cl_loi_read_list),
405 osc_list(&obj->oo_ready_item),
406 osc_list(&obj->oo_hp_ready_item),
407 osc_list(&obj->oo_write_item),
408 osc_list(&obj->oo_read_item),
409 cfs_atomic_read(&obj->oo_nr_reads),
410 osc_list(&obj->oo_reading_exts),
411 cfs_atomic_read(&obj->oo_nr_writes),
412 osc_list(&obj->oo_hp_exts),
413 osc_list(&obj->oo_urgent_exts));
416 static void osc_page_delete(const struct lu_env *env,
417 const struct cl_page_slice *slice)
419 struct osc_page *opg = cl2osc_page(slice);
420 struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
423 LINVRNT(opg->ops_temp || osc_page_protected(env, opg, CLM_READ, 1));
426 CDEBUG(D_TRACE, "%p\n", opg);
427 osc_page_transfer_put(env, opg);
428 rc = osc_teardown_async_page(env, obj, opg);
430 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(slice->cpl_page),
431 "Trying to teardown failed: %d\n", rc);
435 spin_lock(&obj->oo_seatbelt);
436 if (opg->ops_submitter != NULL) {
437 LASSERT(!cfs_list_empty(&opg->ops_inflight));
438 cfs_list_del_init(&opg->ops_inflight);
439 opg->ops_submitter = NULL;
441 spin_unlock(&obj->oo_seatbelt);
443 osc_lru_del(osc_cli(obj), opg, true);
447 void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
450 struct osc_page *opg = cl2osc_page(slice);
451 struct osc_async_page *oap = &opg->ops_oap;
453 LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
455 opg->ops_from = from;
457 spin_lock(&oap->oap_lock);
458 oap->oap_async_flags |= ASYNC_COUNT_STABLE;
459 spin_unlock(&oap->oap_lock);
462 static int osc_page_cancel(const struct lu_env *env,
463 const struct cl_page_slice *slice)
465 struct osc_page *opg = cl2osc_page(slice);
468 LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
470 /* Check if the transferring against this page
471 * is completed, or not even queued. */
472 if (opg->ops_transfer_pinned)
473 /* FIXME: may not be interrupted.. */
474 rc = osc_cancel_async_page(env, opg);
475 LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
479 static int osc_page_flush(const struct lu_env *env,
480 const struct cl_page_slice *slice,
483 struct osc_page *opg = cl2osc_page(slice);
486 rc = osc_flush_async_page(env, io, opg);
490 static const struct cl_page_operations osc_page_ops = {
491 .cpo_fini = osc_page_fini,
492 .cpo_print = osc_page_print,
493 .cpo_delete = osc_page_delete,
494 .cpo_is_under_lock = osc_page_is_under_lock,
495 .cpo_disown = osc_page_disown,
498 .cpo_cache_add = osc_page_fail,
499 .cpo_completion = osc_page_completion_read
502 .cpo_cache_add = osc_page_cache_add,
503 .cpo_completion = osc_page_completion_write
506 .cpo_clip = osc_page_clip,
507 .cpo_cancel = osc_page_cancel,
508 .cpo_flush = osc_page_flush
511 struct cl_page *osc_page_init(const struct lu_env *env,
512 struct cl_object *obj,
513 struct cl_page *page, cfs_page_t *vmpage)
515 struct osc_object *osc = cl2osc(obj);
516 struct osc_page *opg;
519 OBD_SLAB_ALLOC_PTR_GFP(opg, osc_page_kmem, CFS_ALLOC_IO);
522 opg->ops_to = CFS_PAGE_SIZE;
524 result = osc_prep_async_page(osc, opg, vmpage,
525 cl_offset(obj, page->cp_index));
527 struct osc_io *oio = osc_env_io(env);
528 opg->ops_srvlock = osc_io_srvlock(oio);
529 cl_page_slice_add(page, &opg->ops_cl, obj,
533 * Cannot assert osc_page_protected() here as read-ahead
534 * creates temporary pages outside of a lock.
536 #ifdef INVARIANT_CHECK
537 opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
539 /* ops_inflight and ops_lru are the same field, but it doesn't
540 * hurt to initialize it twice :-) */
541 CFS_INIT_LIST_HEAD(&opg->ops_inflight);
542 CFS_INIT_LIST_HEAD(&opg->ops_lru);
546 /* reserve an LRU space for this page */
547 if (page->cp_type == CPT_CACHEABLE && result == 0)
548 result = osc_lru_reserve(env, osc, opg);
550 return ERR_PTR(result);
554 * Helper function called by osc_io_submit() for every page in an immediate
555 * transfer (i.e., transferred synchronously).
557 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
558 enum cl_req_type crt, int brw_flags)
560 struct osc_async_page *oap = &opg->ops_oap;
561 struct osc_object *obj = oap->oap_obj;
563 LINVRNT(osc_page_protected(env, opg,
564 crt == CRT_WRITE ? CLM_WRITE : CLM_READ, 1));
566 LASSERTF(oap->oap_magic == OAP_MAGIC, "Bad oap magic: oap %p, "
567 "magic 0x%x\n", oap, oap->oap_magic);
568 LASSERT(oap->oap_async_flags & ASYNC_READY);
569 LASSERT(oap->oap_async_flags & ASYNC_COUNT_STABLE);
571 oap->oap_cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
572 oap->oap_page_off = opg->ops_from;
573 oap->oap_count = opg->ops_to - opg->ops_from;
574 oap->oap_brw_flags = OBD_BRW_SYNC | brw_flags;
576 if (!client_is_remote(osc_export(obj)) &&
577 cfs_capable(CFS_CAP_SYS_RESOURCE)) {
578 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
579 oap->oap_cmd |= OBD_BRW_NOQUOTA;
582 opg->ops_submit_time = cfs_time_current();
583 osc_page_transfer_get(opg, "transfer\0imm");
584 osc_page_transfer_add(env, opg, crt);
587 /* --------------- LRU page management ------------------ */
589 /* OSC is a natural place to manage LRU pages as applications are specialized
590 * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
591 * occupy more LRU slots. On the other hand, we should avoid using up all LRU
592 * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
593 * for free LRU slots - this will be very bad so the algorithm requires each
594 * OSC to free slots voluntarily to maintain a reasonable number of free slots
598 static CFS_DECL_WAITQ(osc_lru_waitq);
599 static cfs_atomic_t osc_lru_waiters = CFS_ATOMIC_INIT(0);
600 /* LRU pages are freed in batch mode. OSC should at least free this
601 * number of pages to avoid running out of LRU budget, and.. */
602 static const int lru_shrink_min = 2 << (20 - CFS_PAGE_SHIFT); /* 2M */
603 /* free this number at most otherwise it will take too long time to finsih. */
604 static const int lru_shrink_max = 32 << (20 - CFS_PAGE_SHIFT); /* 32M */
606 /* Check if we can free LRU slots from this OSC. If there exists LRU waiters,
607 * we should free slots aggressively. In this way, slots are freed in a steady
608 * step to maintain fairness among OSCs.
610 * Return how many LRU pages should be freed. */
611 static int osc_cache_too_much(struct client_obd *cli)
613 struct cl_client_cache *cache = cli->cl_cache;
614 int pages = cfs_atomic_read(&cli->cl_lru_in_list) >> 1;
616 if (cfs_atomic_read(&osc_lru_waiters) > 0 &&
617 cfs_atomic_read(cli->cl_lru_left) < lru_shrink_max)
618 /* drop lru pages aggressively */
619 return min(pages, lru_shrink_max);
621 /* if it's going to run out LRU slots, we should free some, but not
622 * too much to maintain faireness among OSCs. */
623 if (cfs_atomic_read(cli->cl_lru_left) < cache->ccc_lru_max >> 4) {
626 tmp = cache->ccc_lru_max / cfs_atomic_read(&cache->ccc_users);
628 return min(pages, lru_shrink_max);
630 return pages > lru_shrink_min ? lru_shrink_min : 0;
636 /* Return how many pages are not discarded in @pvec. */
637 static int discard_pagevec(const struct lu_env *env, struct cl_io *io,
638 struct cl_page **pvec, int max_index)
643 for (count = 0, i = 0; i < max_index; i++) {
644 struct cl_page *page = pvec[i];
645 if (cl_page_own_try(env, io, page) == 0) {
646 /* free LRU page only if nobody is using it.
647 * This check is necessary to avoid freeing the pages
648 * having already been removed from LRU and pinned
650 if (cfs_atomic_read(&page->cp_ref) == 1) {
651 cl_page_unmap(env, io, page);
652 cl_page_discard(env, io, page);
655 cl_page_disown(env, io, page);
657 cl_page_put(env, page);
660 return max_index - count;
664 * Drop @target of pages from LRU at most.
666 int osc_lru_shrink(struct client_obd *cli, int target)
668 struct cl_env_nest nest;
671 struct cl_object *clobj = NULL;
672 struct cl_page **pvec;
673 struct osc_page *opg;
680 LASSERT(cfs_atomic_read(&cli->cl_lru_in_list) >= 0);
681 if (cfs_atomic_read(&cli->cl_lru_in_list) == 0 || target <= 0)
684 env = cl_env_nested_get(&nest);
686 RETURN(PTR_ERR(env));
688 pvec = osc_env_info(env)->oti_pvec;
689 io = &osc_env_info(env)->oti_io;
691 client_obd_list_lock(&cli->cl_lru_list_lock);
692 cfs_atomic_inc(&cli->cl_lru_shrinkers);
693 maxscan = min(target << 1, cfs_atomic_read(&cli->cl_lru_in_list));
694 while (!cfs_list_empty(&cli->cl_lru_list)) {
695 struct cl_page *page;
700 opg = cfs_list_entry(cli->cl_lru_list.next, struct osc_page,
702 page = cl_page_top(opg->ops_cl.cpl_page);
703 if (page->cp_state == CPS_FREEING ||
704 cfs_atomic_read(&page->cp_ref) > 0) {
705 cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
709 LASSERT(page->cp_obj != NULL);
710 if (clobj != page->cp_obj) {
711 struct cl_object *tmp = page->cp_obj;
714 client_obd_list_unlock(&cli->cl_lru_list_lock);
717 count -= discard_pagevec(env, io, pvec, index);
721 cl_object_put(env, clobj);
727 rc = cl_io_init(env, io, CIT_MISC, clobj);
732 client_obd_list_lock(&cli->cl_lru_list_lock);
736 /* move this page to the end of list as it will be discarded
737 * soon. The page will be finally removed from LRU list in
738 * osc_page_delete(). */
739 cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
741 /* it's okay to grab a refcount here w/o holding lock because
742 * it has to grab cl_lru_list_lock to delete the page. */
744 pvec[index++] = page;
745 if (++count >= target)
748 if (unlikely(index == OTI_PVEC_SIZE)) {
749 client_obd_list_unlock(&cli->cl_lru_list_lock);
750 count -= discard_pagevec(env, io, pvec, index);
753 client_obd_list_lock(&cli->cl_lru_list_lock);
756 client_obd_list_unlock(&cli->cl_lru_list_lock);
759 count -= discard_pagevec(env, io, pvec, index);
762 cl_object_put(env, clobj);
764 cl_env_nested_put(&nest, env);
766 cfs_atomic_dec(&cli->cl_lru_shrinkers);
767 RETURN(count > 0 ? count : rc);
770 static void osc_lru_add(struct client_obd *cli, struct osc_page *opg)
774 if (!opg->ops_in_lru)
777 cfs_atomic_dec(&cli->cl_lru_busy);
778 client_obd_list_lock(&cli->cl_lru_list_lock);
779 if (cfs_list_empty(&opg->ops_lru)) {
780 cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
781 cfs_atomic_inc_return(&cli->cl_lru_in_list);
782 wakeup = cfs_atomic_read(&osc_lru_waiters) > 0;
784 client_obd_list_unlock(&cli->cl_lru_list_lock);
787 cfs_waitq_broadcast(&osc_lru_waitq);
790 /* delete page from LRUlist. The page can be deleted from LRUlist for two
791 * reasons: redirtied or deleted from page cache. */
792 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg, bool del)
794 if (opg->ops_in_lru) {
795 client_obd_list_lock(&cli->cl_lru_list_lock);
796 if (!cfs_list_empty(&opg->ops_lru)) {
797 LASSERT(cfs_atomic_read(&cli->cl_lru_in_list) > 0);
798 cfs_list_del_init(&opg->ops_lru);
799 cfs_atomic_dec(&cli->cl_lru_in_list);
801 cfs_atomic_inc(&cli->cl_lru_busy);
803 LASSERT(cfs_atomic_read(&cli->cl_lru_busy) > 0);
804 cfs_atomic_dec(&cli->cl_lru_busy);
806 client_obd_list_unlock(&cli->cl_lru_list_lock);
808 cfs_atomic_inc(cli->cl_lru_left);
809 /* this is a great place to release more LRU pages if
810 * this osc occupies too many LRU pages and kernel is
811 * stealing one of them.
812 * cl_lru_shrinkers is to avoid recursive call in case
813 * we're already in the context of osc_lru_shrink(). */
814 if (cfs_atomic_read(&cli->cl_lru_shrinkers) == 0)
815 osc_lru_shrink(cli, osc_cache_too_much(cli));
816 cfs_waitq_signal(&osc_lru_waitq);
819 LASSERT(cfs_list_empty(&opg->ops_lru));
823 static int osc_lru_reclaim(struct client_obd *cli)
825 struct cl_client_cache *cache = cli->cl_cache;
826 struct client_obd *victim;
827 struct client_obd *tmp;
830 LASSERT(cache != NULL);
831 LASSERT(!cfs_list_empty(&cache->ccc_lru));
833 rc = osc_lru_shrink(cli, lru_shrink_min);
835 CDEBUG(D_CACHE, "%s: Free %d pages from own LRU: %p.\n",
836 cli->cl_import->imp_obd->obd_name, rc, cli);
840 CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %d, busy: %d.\n",
841 cli->cl_import->imp_obd->obd_name, cli,
842 cfs_atomic_read(&cli->cl_lru_in_list),
843 cfs_atomic_read(&cli->cl_lru_busy));
845 /* Reclaim LRU slots from other client_obd as it can't free enough
846 * from its own. This should rarely happen. */
847 spin_lock(&cache->ccc_lru_lock);
848 cache->ccc_lru_shrinkers++;
849 cfs_list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
850 cfs_list_for_each_entry_safe(victim, tmp, &cache->ccc_lru, cl_lru_osc) {
854 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %d, busy: %d.\n",
855 victim->cl_import->imp_obd->obd_name, victim,
856 cfs_atomic_read(&victim->cl_lru_in_list),
857 cfs_atomic_read(&victim->cl_lru_busy));
859 cfs_list_move_tail(&victim->cl_lru_osc, &cache->ccc_lru);
860 if (cfs_atomic_read(&victim->cl_lru_in_list) > 0)
863 spin_unlock(&cache->ccc_lru_lock);
865 CDEBUG(D_CACHE, "%s: can't get any free LRU slots.\n",
866 cli->cl_import->imp_obd->obd_name);
870 rc = osc_lru_shrink(victim,
871 min(cfs_atomic_read(&victim->cl_lru_in_list) >> 1,
874 CDEBUG(D_CACHE, "%s: Free %d pages from other cli: %p.\n",
875 cli->cl_import->imp_obd->obd_name, rc, victim);
880 static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
881 struct osc_page *opg)
883 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
884 struct client_obd *cli = osc_cli(obj);
888 if (cli->cl_cache == NULL) /* shall not be in LRU */
891 LASSERT(cfs_atomic_read(cli->cl_lru_left) >= 0);
892 while (!cfs_atomic_add_unless(cli->cl_lru_left, -1, 0)) {
895 /* run out of LRU spaces, try to drop some by itself */
896 rc = osc_lru_reclaim(cli);
904 /* slowest case, all of caching pages are busy, notifying
905 * other OSCs that we're lack of LRU slots. */
906 cfs_atomic_inc(&osc_lru_waiters);
908 gen = cfs_atomic_read(&cli->cl_lru_in_list);
909 rc = l_wait_event(osc_lru_waitq,
910 cfs_atomic_read(cli->cl_lru_left) > 0 ||
911 (cfs_atomic_read(&cli->cl_lru_in_list) > 0 &&
912 gen != cfs_atomic_read(&cli->cl_lru_in_list)),
915 cfs_atomic_dec(&osc_lru_waiters);
921 cfs_atomic_inc(&cli->cl_lru_busy);