Whamcloud - gitweb
LU-2335 kernel: add CONFIG_ prefix to config values
[fs/lustre-release.git] / lustre / osc / osc_page.c
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, 2013, 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  * Implementation of cl_page for OSC layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_OSC
42
43 #include "osc_cl_internal.h"
44
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);
49
50 /** \addtogroup osc
51  *  @{
52  */
53
54 /*
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.
60  */
61 #if 0
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)
65 {
66         struct cl_page         *page;
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;
72         ldlm_mode_t             dlmmode;
73         int                     flags;
74
75         cfs_might_sleep();
76
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);
83
84         flags = LDLM_FL_TEST_LOCK | LDLM_FL_BLOCK_GRANTED;
85         if (pending)
86                 flags |= LDLM_FL_CBPENDING;
87
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);
93 }
94
95 /**
96  * Checks an invariant that a page in the cache is covered by a lock, as
97  * needed.
98  */
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)
102 {
103         struct cl_object_header *hdr;
104         struct cl_lock          *scan;
105         struct cl_page          *page;
106         struct cl_lock_descr    *descr;
107         int result;
108
109         LINVRNT(!opg->ops_temp);
110
111         page = opg->ops_cl.cpl_page;
112         if (page->cp_owner != NULL &&
113             cl_io_top(page->cp_owner)->ci_lockreq == CILR_NEVER)
114                 /*
115                  * If IO is done without locks (liblustre, or lloop), lock is
116                  * not required.
117                  */
118                 result = 1;
119         else
120                 /* otherwise check for a DLM lock */
121         result = osc_page_is_dlocked(env, opg, mode, 1, unref);
122         if (result == 0) {
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) {
131                         /*
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().
136                          */
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;
141
142                                 olck = osc_lock_at(scan);
143                                 result = osc_lock_is_lockless(olck);
144                                 break;
145                         }
146                 }
147                 spin_unlock(&hdr->coh_lock_guard);
148         }
149         return result;
150 }
151 #else
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)
155 {
156         return 1;
157 }
158 #endif
159
160 /*****************************************************************************
161  *
162  * Page operations.
163  *
164  */
165 static void osc_page_fini(const struct lu_env *env,
166                           struct cl_page_slice *slice)
167 {
168         struct osc_page *opg = cl2osc_page(slice);
169         CDEBUG(D_TRACE, "%p\n", opg);
170         LASSERT(opg->ops_lock == NULL);
171 }
172
173 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
174 {
175         struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
176
177         LASSERT(!opg->ops_transfer_pinned);
178         cl_page_get(page);
179         lu_ref_add_atomic(&page->cp_reference, label, page);
180         opg->ops_transfer_pinned = 1;
181 }
182
183 static void osc_page_transfer_put(const struct lu_env *env,
184                                   struct osc_page *opg)
185 {
186         struct cl_page *page = cl_page_top(opg->ops_cl.cpl_page);
187
188         if (opg->ops_transfer_pinned) {
189                 lu_ref_del(&page->cp_reference, "transfer", page);
190                 opg->ops_transfer_pinned = 0;
191                 cl_page_put(env, page);
192         }
193 }
194
195 /**
196  * This is called once for every page when it is submitted for a transfer
197  * either opportunistic (osc_page_cache_add()), or immediate
198  * (osc_page_submit()).
199  */
200 static void osc_page_transfer_add(const struct lu_env *env,
201                                   struct osc_page *opg, enum cl_req_type crt)
202 {
203         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
204
205         /* ops_lru and ops_inflight share the same field, so take it from LRU
206          * first and then use it as inflight. */
207         osc_lru_del(osc_cli(obj), opg, false);
208
209         spin_lock(&obj->oo_seatbelt);
210         cfs_list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
211         opg->ops_submitter = cfs_current();
212         spin_unlock(&obj->oo_seatbelt);
213 }
214
215 static int osc_page_cache_add(const struct lu_env *env,
216                               const struct cl_page_slice *slice,
217                               struct cl_io *io)
218 {
219         struct osc_io   *oio = osc_env_io(env);
220         struct osc_page *opg = cl2osc_page(slice);
221         int result;
222         ENTRY;
223
224         LINVRNT(osc_page_protected(env, opg, CLM_WRITE, 0));
225
226         osc_page_transfer_get(opg, "transfer\0cache");
227         result = osc_queue_async_io(env, io, opg);
228         if (result != 0)
229                 osc_page_transfer_put(env, opg);
230         else
231                 osc_page_transfer_add(env, opg, CRT_WRITE);
232
233         /* for sync write, kernel will wait for this page to be flushed before
234          * osc_io_end() is called, so release it earlier.
235          * for mkwrite(), it's known there is no further pages. */
236         if (cl_io_is_sync_write(io) || cl_io_is_mkwrite(io)) {
237                 if (oio->oi_active != NULL) {
238                         osc_extent_release(env, oio->oi_active);
239                         oio->oi_active = NULL;
240                 }
241         }
242
243         RETURN(result);
244 }
245
246 void osc_index2policy(ldlm_policy_data_t *policy, const struct cl_object *obj,
247                       pgoff_t start, pgoff_t end)
248 {
249         memset(policy, 0, sizeof *policy);
250         policy->l_extent.start = cl_offset(obj, start);
251         policy->l_extent.end   = cl_offset(obj, end + 1) - 1;
252 }
253
254 static int osc_page_addref_lock(const struct lu_env *env,
255                                 struct osc_page *opg,
256                                 struct cl_lock *lock)
257 {
258         struct osc_lock *olock;
259         int              rc;
260
261         LASSERT(opg->ops_lock == NULL);
262
263         olock = osc_lock_at(lock);
264         if (cfs_atomic_inc_return(&olock->ols_pageref) <= 0) {
265                 cfs_atomic_dec(&olock->ols_pageref);
266                 rc = -ENODATA;
267         } else {
268                 cl_lock_get(lock);
269                 opg->ops_lock = lock;
270                 rc = 0;
271         }
272         return rc;
273 }
274
275 static void osc_page_putref_lock(const struct lu_env *env,
276                                  struct osc_page *opg)
277 {
278         struct cl_lock  *lock = opg->ops_lock;
279         struct osc_lock *olock;
280
281         LASSERT(lock != NULL);
282         olock = osc_lock_at(lock);
283
284         cfs_atomic_dec(&olock->ols_pageref);
285         opg->ops_lock = NULL;
286
287         cl_lock_put(env, lock);
288 }
289
290 static int osc_page_is_under_lock(const struct lu_env *env,
291                                   const struct cl_page_slice *slice,
292                                   struct cl_io *unused)
293 {
294         struct cl_lock *lock;
295         int             result = -ENODATA;
296
297         ENTRY;
298         lock = cl_lock_at_page(env, slice->cpl_obj, slice->cpl_page,
299                                NULL, 1, 0);
300         if (lock != NULL) {
301                 if (osc_page_addref_lock(env, cl2osc_page(slice), lock) == 0)
302                         result = -EBUSY;
303                 cl_lock_put(env, lock);
304         }
305         RETURN(result);
306 }
307
308 static void osc_page_disown(const struct lu_env *env,
309                             const struct cl_page_slice *slice,
310                             struct cl_io *io)
311 {
312         struct osc_page *opg = cl2osc_page(slice);
313
314         if (unlikely(opg->ops_lock))
315                 osc_page_putref_lock(env, opg);
316 }
317
318 static void osc_page_completion_read(const struct lu_env *env,
319                                      const struct cl_page_slice *slice,
320                                      int ioret)
321 {
322         struct osc_page   *opg = cl2osc_page(slice);
323         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
324
325         if (likely(opg->ops_lock))
326                 osc_page_putref_lock(env, opg);
327         osc_lru_add(osc_cli(obj), opg);
328 }
329
330 static void osc_page_completion_write(const struct lu_env *env,
331                                       const struct cl_page_slice *slice,
332                                       int ioret)
333 {
334         struct osc_page   *opg = cl2osc_page(slice);
335         struct osc_object *obj = cl2osc(slice->cpl_obj);
336
337         osc_lru_add(osc_cli(obj), opg);
338 }
339
340 static int osc_page_fail(const struct lu_env *env,
341                          const struct cl_page_slice *slice,
342                          struct cl_io *unused)
343 {
344         /*
345          * Cached read?
346          */
347         LBUG();
348         return 0;
349 }
350
351
352 static const char *osc_list(cfs_list_t *head)
353 {
354         return cfs_list_empty(head) ? "-" : "+";
355 }
356
357 static inline cfs_time_t osc_submit_duration(struct osc_page *opg)
358 {
359         if (opg->ops_submit_time == 0)
360                 return 0;
361
362         return (cfs_time_current() - opg->ops_submit_time);
363 }
364
365 static int osc_page_print(const struct lu_env *env,
366                           const struct cl_page_slice *slice,
367                           void *cookie, lu_printer_t printer)
368 {
369         struct osc_page       *opg = cl2osc_page(slice);
370         struct osc_async_page *oap = &opg->ops_oap;
371         struct osc_object     *obj = cl2osc(slice->cpl_obj);
372         struct client_obd     *cli = &osc_export(obj)->exp_obd->u.cli;
373
374         return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p: "
375                           "1< %#x %d %u %s %s > "
376                           "2< "LPU64" %u %u %#x %#x | %p %p %p > "
377                           "3< %s %p %d %lu %d > "
378                           "4< %d %d %d %lu %s | %s %s %s %s > "
379                           "5< %s %s %s %s | %d %s | %d %s %s>\n",
380                           opg,
381                           /* 1 */
382                           oap->oap_magic, oap->oap_cmd,
383                           oap->oap_interrupted,
384                           osc_list(&oap->oap_pending_item),
385                           osc_list(&oap->oap_rpc_item),
386                           /* 2 */
387                           oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
388                           oap->oap_async_flags, oap->oap_brw_flags,
389                           oap->oap_request, oap->oap_cli, obj,
390                           /* 3 */
391                           osc_list(&opg->ops_inflight),
392                           opg->ops_submitter, opg->ops_transfer_pinned,
393                           osc_submit_duration(opg), opg->ops_srvlock,
394                           /* 4 */
395                           cli->cl_r_in_flight, cli->cl_w_in_flight,
396                           cli->cl_max_rpcs_in_flight,
397                           cli->cl_avail_grant,
398                           osc_list(&cli->cl_cache_waiters),
399                           osc_list(&cli->cl_loi_ready_list),
400                           osc_list(&cli->cl_loi_hp_ready_list),
401                           osc_list(&cli->cl_loi_write_list),
402                           osc_list(&cli->cl_loi_read_list),
403                           /* 5 */
404                           osc_list(&obj->oo_ready_item),
405                           osc_list(&obj->oo_hp_ready_item),
406                           osc_list(&obj->oo_write_item),
407                           osc_list(&obj->oo_read_item),
408                           cfs_atomic_read(&obj->oo_nr_reads),
409                           osc_list(&obj->oo_reading_exts),
410                           cfs_atomic_read(&obj->oo_nr_writes),
411                           osc_list(&obj->oo_hp_exts),
412                           osc_list(&obj->oo_urgent_exts));
413 }
414
415 static void osc_page_delete(const struct lu_env *env,
416                             const struct cl_page_slice *slice)
417 {
418         struct osc_page   *opg = cl2osc_page(slice);
419         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
420         int rc;
421
422         LINVRNT(opg->ops_temp || osc_page_protected(env, opg, CLM_READ, 1));
423
424         ENTRY;
425         CDEBUG(D_TRACE, "%p\n", opg);
426         osc_page_transfer_put(env, opg);
427         rc = osc_teardown_async_page(env, obj, opg);
428         if (rc) {
429                 CL_PAGE_DEBUG(D_ERROR, env, cl_page_top(slice->cpl_page),
430                               "Trying to teardown failed: %d\n", rc);
431                 LASSERT(0);
432         }
433
434         spin_lock(&obj->oo_seatbelt);
435         if (opg->ops_submitter != NULL) {
436                 LASSERT(!cfs_list_empty(&opg->ops_inflight));
437                 cfs_list_del_init(&opg->ops_inflight);
438                 opg->ops_submitter = NULL;
439         }
440         spin_unlock(&obj->oo_seatbelt);
441
442         osc_lru_del(osc_cli(obj), opg, true);
443         EXIT;
444 }
445
446 void osc_page_clip(const struct lu_env *env, const struct cl_page_slice *slice,
447                    int from, int to)
448 {
449         struct osc_page       *opg = cl2osc_page(slice);
450         struct osc_async_page *oap = &opg->ops_oap;
451
452         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
453
454         opg->ops_from = from;
455         opg->ops_to   = to;
456         spin_lock(&oap->oap_lock);
457         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
458         spin_unlock(&oap->oap_lock);
459 }
460
461 static int osc_page_cancel(const struct lu_env *env,
462                            const struct cl_page_slice *slice)
463 {
464         struct osc_page *opg = cl2osc_page(slice);
465         int rc = 0;
466
467         LINVRNT(osc_page_protected(env, opg, CLM_READ, 0));
468
469         /* Check if the transferring against this page
470          * is completed, or not even queued. */
471         if (opg->ops_transfer_pinned)
472                 /* FIXME: may not be interrupted.. */
473                 rc = osc_cancel_async_page(env, opg);
474         LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
475         return rc;
476 }
477
478 static int osc_page_flush(const struct lu_env *env,
479                           const struct cl_page_slice *slice,
480                           struct cl_io *io)
481 {
482         struct osc_page *opg = cl2osc_page(slice);
483         int rc = 0;
484         ENTRY;
485         rc = osc_flush_async_page(env, io, opg);
486         RETURN(rc);
487 }
488
489 static const struct cl_page_operations osc_page_ops = {
490         .cpo_fini          = osc_page_fini,
491         .cpo_print         = osc_page_print,
492         .cpo_delete        = osc_page_delete,
493         .cpo_is_under_lock = osc_page_is_under_lock,
494         .cpo_disown        = osc_page_disown,
495         .io = {
496                 [CRT_READ] = {
497                         .cpo_cache_add  = osc_page_fail,
498                         .cpo_completion = osc_page_completion_read
499                 },
500                 [CRT_WRITE] = {
501                         .cpo_cache_add  = osc_page_cache_add,
502                         .cpo_completion = osc_page_completion_write
503                 }
504         },
505         .cpo_clip           = osc_page_clip,
506         .cpo_cancel         = osc_page_cancel,
507         .cpo_flush          = osc_page_flush
508 };
509
510 int osc_page_init(const struct lu_env *env, struct cl_object *obj,
511                 struct cl_page *page, cfs_page_t *vmpage)
512 {
513         struct osc_object *osc = cl2osc(obj);
514         struct osc_page   *opg = cl_object_page_slice(obj, page);
515         int result;
516
517         opg->ops_from = 0;
518         opg->ops_to   = CFS_PAGE_SIZE;
519
520         result = osc_prep_async_page(osc, opg, vmpage,
521                                         cl_offset(obj, page->cp_index));
522         if (result == 0) {
523                 struct osc_io *oio = osc_env_io(env);
524                 opg->ops_srvlock = osc_io_srvlock(oio);
525                 cl_page_slice_add(page, &opg->ops_cl, obj,
526                                 &osc_page_ops);
527         }
528         /*
529          * Cannot assert osc_page_protected() here as read-ahead
530          * creates temporary pages outside of a lock.
531          */
532 #ifdef CONFIG_LUSTRE_DEBUG_EXPENSIVE_CHECK
533         opg->ops_temp = !osc_page_protected(env, opg, CLM_READ, 1);
534 #endif
535         /* ops_inflight and ops_lru are the same field, but it doesn't
536          * hurt to initialize it twice :-) */
537         CFS_INIT_LIST_HEAD(&opg->ops_inflight);
538         CFS_INIT_LIST_HEAD(&opg->ops_lru);
539
540         /* reserve an LRU space for this page */
541         if (page->cp_type == CPT_CACHEABLE && result == 0)
542                 result = osc_lru_reserve(env, osc, opg);
543
544         return result;
545 }
546
547 /**
548  * Helper function called by osc_io_submit() for every page in an immediate
549  * transfer (i.e., transferred synchronously).
550  */
551 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
552                      enum cl_req_type crt, int brw_flags)
553 {
554         struct osc_async_page *oap = &opg->ops_oap;
555         struct osc_object     *obj = oap->oap_obj;
556
557         LINVRNT(osc_page_protected(env, opg,
558                                    crt == CRT_WRITE ? CLM_WRITE : CLM_READ, 1));
559
560         LASSERTF(oap->oap_magic == OAP_MAGIC, "Bad oap magic: oap %p, "
561                  "magic 0x%x\n", oap, oap->oap_magic);
562         LASSERT(oap->oap_async_flags & ASYNC_READY);
563         LASSERT(oap->oap_async_flags & ASYNC_COUNT_STABLE);
564
565         oap->oap_cmd       = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
566         oap->oap_page_off  = opg->ops_from;
567         oap->oap_count     = opg->ops_to - opg->ops_from;
568         oap->oap_brw_flags = OBD_BRW_SYNC | brw_flags;
569
570         if (!client_is_remote(osc_export(obj)) &&
571                         cfs_capable(CFS_CAP_SYS_RESOURCE)) {
572                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
573                 oap->oap_cmd |= OBD_BRW_NOQUOTA;
574         }
575
576         opg->ops_submit_time = cfs_time_current();
577         osc_page_transfer_get(opg, "transfer\0imm");
578         osc_page_transfer_add(env, opg, crt);
579 }
580
581 /* --------------- LRU page management ------------------ */
582
583 /* OSC is a natural place to manage LRU pages as applications are specialized
584  * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
585  * occupy more LRU slots. On the other hand, we should avoid using up all LRU
586  * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
587  * for free LRU slots - this will be very bad so the algorithm requires each
588  * OSC to free slots voluntarily to maintain a reasonable number of free slots
589  * at any time.
590  */
591
592 static CFS_DECL_WAITQ(osc_lru_waitq);
593 static cfs_atomic_t osc_lru_waiters = CFS_ATOMIC_INIT(0);
594 /* LRU pages are freed in batch mode. OSC should at least free this
595  * number of pages to avoid running out of LRU budget, and.. */
596 static const int lru_shrink_min = 2 << (20 - CFS_PAGE_SHIFT);  /* 2M */
597 /* free this number at most otherwise it will take too long time to finsih. */
598 static const int lru_shrink_max = 32 << (20 - CFS_PAGE_SHIFT); /* 32M */
599
600 /* Check if we can free LRU slots from this OSC. If there exists LRU waiters,
601  * we should free slots aggressively. In this way, slots are freed in a steady
602  * step to maintain fairness among OSCs.
603  *
604  * Return how many LRU pages should be freed. */
605 static int osc_cache_too_much(struct client_obd *cli)
606 {
607         struct cl_client_cache *cache = cli->cl_cache;
608         int pages = cfs_atomic_read(&cli->cl_lru_in_list) >> 1;
609
610         if (cfs_atomic_read(&osc_lru_waiters) > 0 &&
611             cfs_atomic_read(cli->cl_lru_left) < lru_shrink_max)
612                 /* drop lru pages aggressively */
613                 return min(pages, lru_shrink_max);
614
615         /* if it's going to run out LRU slots, we should free some, but not
616          * too much to maintain faireness among OSCs. */
617         if (cfs_atomic_read(cli->cl_lru_left) < cache->ccc_lru_max >> 4) {
618                 unsigned long tmp;
619
620                 tmp = cache->ccc_lru_max / cfs_atomic_read(&cache->ccc_users);
621                 if (pages > tmp)
622                         return min(pages, lru_shrink_max);
623
624                 return pages > lru_shrink_min ? lru_shrink_min : 0;
625         }
626
627         return 0;
628 }
629
630 /* Return how many pages are not discarded in @pvec. */
631 static int discard_pagevec(const struct lu_env *env, struct cl_io *io,
632                            struct cl_page **pvec, int max_index)
633 {
634         int count;
635         int i;
636
637         for (count = 0, i = 0; i < max_index; i++) {
638                 struct cl_page *page = pvec[i];
639                 if (cl_page_own_try(env, io, page) == 0) {
640                         /* free LRU page only if nobody is using it.
641                          * This check is necessary to avoid freeing the pages
642                          * having already been removed from LRU and pinned
643                          * for IO. */
644                         if (!cl_page_in_use(page)) {
645                                 cl_page_unmap(env, io, page);
646                                 cl_page_discard(env, io, page);
647                                 ++count;
648                         }
649                         cl_page_disown(env, io, page);
650                 }
651                 cl_page_put(env, page);
652                 pvec[i] = NULL;
653         }
654         return max_index - count;
655 }
656
657 /**
658  * Drop @target of pages from LRU at most.
659  */
660 int osc_lru_shrink(struct client_obd *cli, int target)
661 {
662         struct cl_env_nest nest;
663         struct lu_env *env;
664         struct cl_io *io;
665         struct cl_object *clobj = NULL;
666         struct cl_page **pvec;
667         struct osc_page *opg;
668         int maxscan = 0;
669         int count = 0;
670         int index = 0;
671         int rc = 0;
672         ENTRY;
673
674         LASSERT(cfs_atomic_read(&cli->cl_lru_in_list) >= 0);
675         if (cfs_atomic_read(&cli->cl_lru_in_list) == 0 || target <= 0)
676                 RETURN(0);
677
678         env = cl_env_nested_get(&nest);
679         if (IS_ERR(env))
680                 RETURN(PTR_ERR(env));
681
682         pvec = osc_env_info(env)->oti_pvec;
683         io = &osc_env_info(env)->oti_io;
684
685         client_obd_list_lock(&cli->cl_lru_list_lock);
686         cfs_atomic_inc(&cli->cl_lru_shrinkers);
687         maxscan = min(target << 1, cfs_atomic_read(&cli->cl_lru_in_list));
688         while (!cfs_list_empty(&cli->cl_lru_list)) {
689                 struct cl_page *page;
690
691                 if (--maxscan < 0)
692                         break;
693
694                 opg = cfs_list_entry(cli->cl_lru_list.next, struct osc_page,
695                                      ops_lru);
696                 page = cl_page_top(opg->ops_cl.cpl_page);
697                 if (cl_page_in_use_noref(page)) {
698                         cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
699                         continue;
700                 }
701
702                 LASSERT(page->cp_obj != NULL);
703                 if (clobj != page->cp_obj) {
704                         struct cl_object *tmp = page->cp_obj;
705
706                         cl_object_get(tmp);
707                         client_obd_list_unlock(&cli->cl_lru_list_lock);
708
709                         if (clobj != NULL) {
710                                 count -= discard_pagevec(env, io, pvec, index);
711                                 index = 0;
712
713                                 cl_io_fini(env, io);
714                                 cl_object_put(env, clobj);
715                                 clobj = NULL;
716                         }
717
718                         clobj = tmp;
719                         io->ci_obj = clobj;
720                         io->ci_ignore_layout = 1;
721                         rc = cl_io_init(env, io, CIT_MISC, clobj);
722
723                         client_obd_list_lock(&cli->cl_lru_list_lock);
724
725                         if (rc != 0)
726                                 break;
727
728                         ++maxscan;
729                         continue;
730                 }
731
732                 /* move this page to the end of list as it will be discarded
733                  * soon. The page will be finally removed from LRU list in
734                  * osc_page_delete().  */
735                 cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
736
737                 /* it's okay to grab a refcount here w/o holding lock because
738                  * it has to grab cl_lru_list_lock to delete the page. */
739                 cl_page_get(page);
740                 pvec[index++] = page;
741                 if (++count >= target)
742                         break;
743
744                 if (unlikely(index == OTI_PVEC_SIZE)) {
745                         client_obd_list_unlock(&cli->cl_lru_list_lock);
746                         count -= discard_pagevec(env, io, pvec, index);
747                         index = 0;
748
749                         client_obd_list_lock(&cli->cl_lru_list_lock);
750                 }
751         }
752         client_obd_list_unlock(&cli->cl_lru_list_lock);
753
754         if (clobj != NULL) {
755                 count -= discard_pagevec(env, io, pvec, index);
756
757                 cl_io_fini(env, io);
758                 cl_object_put(env, clobj);
759         }
760         cl_env_nested_put(&nest, env);
761
762         cfs_atomic_dec(&cli->cl_lru_shrinkers);
763         RETURN(count > 0 ? count : rc);
764 }
765
766 static void osc_lru_add(struct client_obd *cli, struct osc_page *opg)
767 {
768         bool wakeup = false;
769
770         if (!opg->ops_in_lru)
771                 return;
772
773         cfs_atomic_dec(&cli->cl_lru_busy);
774         client_obd_list_lock(&cli->cl_lru_list_lock);
775         if (cfs_list_empty(&opg->ops_lru)) {
776                 cfs_list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
777                 cfs_atomic_inc_return(&cli->cl_lru_in_list);
778                 wakeup = cfs_atomic_read(&osc_lru_waiters) > 0;
779         }
780         client_obd_list_unlock(&cli->cl_lru_list_lock);
781
782         if (wakeup) {
783                 osc_lru_shrink(cli, osc_cache_too_much(cli));
784                 cfs_waitq_broadcast(&osc_lru_waitq);
785         }
786 }
787
788 /* delete page from LRUlist. The page can be deleted from LRUlist for two
789  * reasons: redirtied or deleted from page cache. */
790 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg, bool del)
791 {
792         if (opg->ops_in_lru) {
793                 client_obd_list_lock(&cli->cl_lru_list_lock);
794                 if (!cfs_list_empty(&opg->ops_lru)) {
795                         LASSERT(cfs_atomic_read(&cli->cl_lru_in_list) > 0);
796                         cfs_list_del_init(&opg->ops_lru);
797                         cfs_atomic_dec(&cli->cl_lru_in_list);
798                         if (!del)
799                                 cfs_atomic_inc(&cli->cl_lru_busy);
800                 } else if (del) {
801                         LASSERT(cfs_atomic_read(&cli->cl_lru_busy) > 0);
802                         cfs_atomic_dec(&cli->cl_lru_busy);
803                 }
804                 client_obd_list_unlock(&cli->cl_lru_list_lock);
805                 if (del) {
806                         cfs_atomic_inc(cli->cl_lru_left);
807                         /* this is a great place to release more LRU pages if
808                          * this osc occupies too many LRU pages and kernel is
809                          * stealing one of them.
810                          * cl_lru_shrinkers is to avoid recursive call in case
811                          * we're already in the context of osc_lru_shrink(). */
812                         if (cfs_atomic_read(&cli->cl_lru_shrinkers) == 0 &&
813                             !cfs_memory_pressure_get())
814                                 osc_lru_shrink(cli, osc_cache_too_much(cli));
815                         cfs_waitq_signal(&osc_lru_waitq);
816                 }
817         } else {
818                 LASSERT(cfs_list_empty(&opg->ops_lru));
819         }
820 }
821
822 static inline int max_to_shrink(struct client_obd *cli)
823 {
824         return min(cfs_atomic_read(&cli->cl_lru_in_list) >> 1, lru_shrink_max);
825 }
826
827 static int osc_lru_reclaim(struct client_obd *cli)
828 {
829         struct cl_client_cache *cache = cli->cl_cache;
830         int max_scans;
831         int rc;
832
833         LASSERT(cache != NULL);
834         LASSERT(!cfs_list_empty(&cache->ccc_lru));
835
836         rc = osc_lru_shrink(cli, lru_shrink_min);
837         if (rc != 0) {
838                 CDEBUG(D_CACHE, "%s: Free %d pages from own LRU: %p.\n",
839                         cli->cl_import->imp_obd->obd_name, rc, cli);
840                 return rc;
841         }
842
843         CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %d, busy: %d.\n",
844                 cli->cl_import->imp_obd->obd_name, cli,
845                 cfs_atomic_read(&cli->cl_lru_in_list),
846                 cfs_atomic_read(&cli->cl_lru_busy));
847
848         /* Reclaim LRU slots from other client_obd as it can't free enough
849          * from its own. This should rarely happen. */
850         spin_lock(&cache->ccc_lru_lock);
851         cache->ccc_lru_shrinkers++;
852         cfs_list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
853
854         max_scans = cfs_atomic_read(&cache->ccc_users);
855         while (--max_scans > 0 && !cfs_list_empty(&cache->ccc_lru)) {
856                 cli = cfs_list_entry(cache->ccc_lru.next, struct client_obd,
857                                         cl_lru_osc);
858
859                 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %d, busy: %d.\n",
860                         cli->cl_import->imp_obd->obd_name, cli,
861                         cfs_atomic_read(&cli->cl_lru_in_list),
862                         cfs_atomic_read(&cli->cl_lru_busy));
863
864                 cfs_list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
865                 if (cfs_atomic_read(&cli->cl_lru_in_list) > 0) {
866                         spin_unlock(&cache->ccc_lru_lock);
867
868                         rc = osc_lru_shrink(cli, max_to_shrink(cli));
869                         spin_lock(&cache->ccc_lru_lock);
870                         if (rc != 0)
871                                 break;
872                 }
873         }
874         spin_unlock(&cache->ccc_lru_lock);
875
876         CDEBUG(D_CACHE, "%s: cli %p freed %d pages.\n",
877                 cli->cl_import->imp_obd->obd_name, cli, rc);
878         return rc;
879 }
880
881 static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
882                            struct osc_page *opg)
883 {
884         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
885         struct client_obd *cli = osc_cli(obj);
886         int rc = 0;
887         ENTRY;
888
889         if (cli->cl_cache == NULL) /* shall not be in LRU */
890                 RETURN(0);
891
892         LASSERT(cfs_atomic_read(cli->cl_lru_left) >= 0);
893         while (!cfs_atomic_add_unless(cli->cl_lru_left, -1, 0)) {
894                 int gen;
895
896                 /* run out of LRU spaces, try to drop some by itself */
897                 rc = osc_lru_reclaim(cli);
898                 if (rc < 0)
899                         break;
900                 if (rc > 0)
901                         continue;
902
903                 cfs_cond_resched();
904
905                 /* slowest case, all of caching pages are busy, notifying
906                  * other OSCs that we're lack of LRU slots. */
907                 cfs_atomic_inc(&osc_lru_waiters);
908
909                 gen = cfs_atomic_read(&cli->cl_lru_in_list);
910                 rc = l_wait_event(osc_lru_waitq,
911                                 cfs_atomic_read(cli->cl_lru_left) > 0 ||
912                                 (cfs_atomic_read(&cli->cl_lru_in_list) > 0 &&
913                                  gen != cfs_atomic_read(&cli->cl_lru_in_list)),
914                                 &lwi);
915
916                 cfs_atomic_dec(&osc_lru_waiters);
917                 if (rc < 0)
918                         break;
919         }
920
921         if (rc >= 0) {
922                 cfs_atomic_inc(&cli->cl_lru_busy);
923                 opg->ops_in_lru = 1;
924                 rc = 0;
925         }
926
927         RETURN(rc);
928 }
929
930 /** @} osc */