Whamcloud - gitweb
c0aa4be23912fd318717f21e4f779047ae1178fa
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_page for OSC layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  *   Author: Jinshan Xiong <jinshan.xiong@intel.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSC
39 #include <lustre_osc.h>
40
41 #include "osc_internal.h"
42
43 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg);
44 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg);
45 static int osc_lru_alloc(const struct lu_env *env, struct client_obd *cli,
46                          struct osc_page *opg);
47
48 /** \addtogroup osc
49  *  @{
50  */
51
52 /*
53  * Page operations.
54  */
55 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
56 {
57         struct cl_page *page = opg->ops_cl.cpl_page;
58
59         LASSERT(!opg->ops_transfer_pinned);
60         cl_page_get(page);
61         lu_ref_add_atomic(&page->cp_reference, label, page);
62         opg->ops_transfer_pinned = 1;
63 }
64
65 static void osc_page_transfer_put(const struct lu_env *env,
66                                   struct osc_page *opg)
67 {
68         struct cl_page *page = opg->ops_cl.cpl_page;
69
70         if (opg->ops_transfer_pinned) {
71                 opg->ops_transfer_pinned = 0;
72                 lu_ref_del(&page->cp_reference, "transfer", page);
73                 cl_page_put(env, page);
74         }
75 }
76
77 /**
78  * This is called once for every page when it is submitted for a transfer
79  * either opportunistic (osc_page_cache_add()), or immediate
80  * (osc_page_submit()).
81  */
82 static void osc_page_transfer_add(const struct lu_env *env,
83                                   struct osc_page *opg, enum cl_req_type crt)
84 {
85         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
86
87         /* ops_lru and ops_inflight share the same field, so take it from LRU
88          * first and then use it as inflight. */
89         osc_lru_use(osc_cli(obj), opg);
90 }
91
92 int osc_page_cache_add(const struct lu_env *env,
93                         const struct cl_page_slice *slice, struct cl_io *io)
94 {
95         struct osc_page *opg = cl2osc_page(slice);
96         int result;
97         ENTRY;
98
99         osc_page_transfer_get(opg, "transfer\0cache");
100         result = osc_queue_async_io(env, io, opg);
101         if (result != 0)
102                 osc_page_transfer_put(env, opg);
103         else
104                 osc_page_transfer_add(env, opg, CRT_WRITE);
105
106         RETURN(result);
107 }
108
109 void osc_index2policy(union ldlm_policy_data *policy,
110                       const struct cl_object *obj, pgoff_t start, pgoff_t end)
111 {
112         memset(policy, 0, sizeof *policy);
113         policy->l_extent.start = cl_offset(obj, start);
114         policy->l_extent.end   = cl_offset(obj, end + 1) - 1;
115 }
116
117 static const char *osc_list(struct list_head *head)
118 {
119         return list_empty(head) ? "-" : "+";
120 }
121
122 static inline cfs_time_t osc_submit_duration(struct osc_page *opg)
123 {
124         if (opg->ops_submit_time == 0)
125                 return 0;
126
127         return (cfs_time_current() - opg->ops_submit_time);
128 }
129
130 static int osc_page_print(const struct lu_env *env,
131                           const struct cl_page_slice *slice,
132                           void *cookie, lu_printer_t printer)
133 {
134         struct osc_page       *opg = cl2osc_page(slice);
135         struct osc_async_page *oap = &opg->ops_oap;
136         struct osc_object     *obj = cl2osc(slice->cpl_obj);
137         struct client_obd     *cli = &osc_export(obj)->exp_obd->u.cli;
138
139         return (*printer)(env, cookie, LUSTRE_OSC_NAME"-page@%p %lu: "
140                           "1< %#x %d %u %s %s > "
141                           "2< %lld %u %u %#x %#x | %p %p %p > "
142                           "3< %d %lu %d > "
143                           "4< %d %d %d %lu %s | %s %s %s %s > "
144                           "5< %s %s %s %s | %d %s | %d %s %s>\n",
145                           opg, osc_index(opg),
146                           /* 1 */
147                           oap->oap_magic, oap->oap_cmd,
148                           oap->oap_interrupted,
149                           osc_list(&oap->oap_pending_item),
150                           osc_list(&oap->oap_rpc_item),
151                           /* 2 */
152                           oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
153                           oap->oap_async_flags, oap->oap_brw_flags,
154                           oap->oap_request, oap->oap_cli, obj,
155                           /* 3 */
156                           opg->ops_transfer_pinned,
157                           osc_submit_duration(opg), opg->ops_srvlock,
158                           /* 4 */
159                           cli->cl_r_in_flight, cli->cl_w_in_flight,
160                           cli->cl_max_rpcs_in_flight,
161                           cli->cl_avail_grant,
162                           osc_list(&cli->cl_cache_waiters),
163                           osc_list(&cli->cl_loi_ready_list),
164                           osc_list(&cli->cl_loi_hp_ready_list),
165                           osc_list(&cli->cl_loi_write_list),
166                           osc_list(&cli->cl_loi_read_list),
167                           /* 5 */
168                           osc_list(&obj->oo_ready_item),
169                           osc_list(&obj->oo_hp_ready_item),
170                           osc_list(&obj->oo_write_item),
171                           osc_list(&obj->oo_read_item),
172                           atomic_read(&obj->oo_nr_reads),
173                           osc_list(&obj->oo_reading_exts),
174                           atomic_read(&obj->oo_nr_writes),
175                           osc_list(&obj->oo_hp_exts),
176                           osc_list(&obj->oo_urgent_exts));
177 }
178
179 static void osc_page_delete(const struct lu_env *env,
180                             const struct cl_page_slice *slice)
181 {
182         struct osc_page   *opg = cl2osc_page(slice);
183         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
184         int rc;
185
186         ENTRY;
187         CDEBUG(D_TRACE, "%p\n", opg);
188         osc_page_transfer_put(env, opg);
189         rc = osc_teardown_async_page(env, obj, opg);
190         if (rc) {
191                 CL_PAGE_DEBUG(D_ERROR, env, slice->cpl_page,
192                               "Trying to teardown failed: %d\n", rc);
193                 LASSERT(0);
194         }
195
196         osc_lru_del(osc_cli(obj), opg);
197
198         if (slice->cpl_page->cp_type == CPT_CACHEABLE) {
199                 void *value;
200
201                 spin_lock(&obj->oo_tree_lock);
202                 value = radix_tree_delete(&obj->oo_tree, osc_index(opg));
203                 if (value != NULL)
204                         --obj->oo_npages;
205                 spin_unlock(&obj->oo_tree_lock);
206
207                 LASSERT(ergo(value != NULL, value == opg));
208         }
209
210         EXIT;
211 }
212
213 static void osc_page_clip(const struct lu_env *env,
214                           const struct cl_page_slice *slice,
215                           int from, int to)
216 {
217         struct osc_page       *opg = cl2osc_page(slice);
218         struct osc_async_page *oap = &opg->ops_oap;
219
220         opg->ops_from = from;
221         opg->ops_to   = to;
222         spin_lock(&oap->oap_lock);
223         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
224         spin_unlock(&oap->oap_lock);
225 }
226
227 static int osc_page_cancel(const struct lu_env *env,
228                            const struct cl_page_slice *slice)
229 {
230         struct osc_page *opg = cl2osc_page(slice);
231         int rc = 0;
232
233         /* Check if the transferring against this page
234          * is completed, or not even queued. */
235         if (opg->ops_transfer_pinned)
236                 /* FIXME: may not be interrupted.. */
237                 rc = osc_cancel_async_page(env, opg);
238         LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
239         return rc;
240 }
241
242 static int osc_page_flush(const struct lu_env *env,
243                           const struct cl_page_slice *slice,
244                           struct cl_io *io)
245 {
246         struct osc_page *opg = cl2osc_page(slice);
247         int rc = 0;
248         ENTRY;
249         rc = osc_flush_async_page(env, io, opg);
250         RETURN(rc);
251 }
252
253 static const struct cl_page_operations osc_page_ops = {
254         .cpo_print         = osc_page_print,
255         .cpo_delete        = osc_page_delete,
256         .cpo_clip           = osc_page_clip,
257         .cpo_cancel         = osc_page_cancel,
258         .cpo_flush          = osc_page_flush
259 };
260
261 int osc_page_init(const struct lu_env *env, struct cl_object *obj,
262                   struct cl_page *page, pgoff_t index)
263 {
264         struct osc_object *osc = cl2osc(obj);
265         struct osc_page   *opg = cl_object_page_slice(obj, page);
266         struct osc_io *oio = osc_env_io(env);
267         int result;
268
269         opg->ops_from = 0;
270         opg->ops_to   = PAGE_SIZE;
271
272         INIT_LIST_HEAD(&opg->ops_lru);
273
274         result = osc_prep_async_page(osc, opg, page->cp_vmpage,
275                                      cl_offset(obj, index));
276         if (result != 0)
277                 return result;
278
279         opg->ops_srvlock = osc_io_srvlock(oio);
280         cl_page_slice_add(page, &opg->ops_cl, obj, index,
281                           &osc_page_ops);
282
283
284         /* reserve an LRU space for this page */
285         if (page->cp_type == CPT_CACHEABLE) {
286                 result = osc_lru_alloc(env, osc_cli(osc), opg);
287                 if (result == 0) {
288                         result = radix_tree_preload(GFP_NOFS);
289                         if (result == 0) {
290                                 spin_lock(&osc->oo_tree_lock);
291                                 result = radix_tree_insert(&osc->oo_tree,
292                                                            index, opg);
293                                 if (result == 0)
294                                         ++osc->oo_npages;
295                                 spin_unlock(&osc->oo_tree_lock);
296
297                                 radix_tree_preload_end();
298                         }
299                 }
300         }
301
302         return result;
303 }
304
305 /**
306  * Helper function called by osc_io_submit() for every page in an immediate
307  * transfer (i.e., transferred synchronously).
308  */
309 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
310                      enum cl_req_type crt, int brw_flags)
311 {
312         struct osc_async_page *oap = &opg->ops_oap;
313
314         LASSERTF(oap->oap_magic == OAP_MAGIC, "Bad oap magic: oap %p, "
315                  "magic 0x%x\n", oap, oap->oap_magic);
316         LASSERT(oap->oap_async_flags & ASYNC_READY);
317         LASSERT(oap->oap_async_flags & ASYNC_COUNT_STABLE);
318
319         oap->oap_cmd       = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
320         oap->oap_page_off  = opg->ops_from;
321         oap->oap_count     = opg->ops_to - opg->ops_from;
322         oap->oap_brw_flags = OBD_BRW_SYNC | brw_flags;
323
324         if (cfs_capable(CFS_CAP_SYS_RESOURCE)) {
325                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
326                 oap->oap_cmd |= OBD_BRW_NOQUOTA;
327         }
328
329         opg->ops_submit_time = cfs_time_current();
330         osc_page_transfer_get(opg, "transfer\0imm");
331         osc_page_transfer_add(env, opg, crt);
332 }
333
334 /* --------------- LRU page management ------------------ */
335
336 /* OSC is a natural place to manage LRU pages as applications are specialized
337  * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
338  * occupy more LRU slots. On the other hand, we should avoid using up all LRU
339  * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
340  * for free LRU slots - this will be very bad so the algorithm requires each
341  * OSC to free slots voluntarily to maintain a reasonable number of free slots
342  * at any time.
343  */
344
345 static DECLARE_WAIT_QUEUE_HEAD(osc_lru_waitq);
346
347 /**
348  * LRU pages are freed in batch mode. OSC should at least free this
349  * number of pages to avoid running out of LRU slots.
350  */
351 static inline int lru_shrink_min(struct client_obd *cli)
352 {
353         return cli->cl_max_pages_per_rpc * 2;
354 }
355
356 /**
357  * free this number at most otherwise it will take too long time to finsih.
358  */
359 static inline int lru_shrink_max(struct client_obd *cli)
360 {
361         return cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
362 }
363
364 /**
365  * Check if we can free LRU slots from this OSC. If there exists LRU waiters,
366  * we should free slots aggressively. In this way, slots are freed in a steady
367  * step to maintain fairness among OSCs.
368  *
369  * Return how many LRU pages should be freed.
370  */
371 static int osc_cache_too_much(struct client_obd *cli)
372 {
373         struct cl_client_cache *cache = cli->cl_cache;
374         long pages = atomic_long_read(&cli->cl_lru_in_list);
375         unsigned long budget;
376
377         LASSERT(cache != NULL);
378         budget = cache->ccc_lru_max / (atomic_read(&cache->ccc_users) - 2);
379
380         /* if it's going to run out LRU slots, we should free some, but not
381          * too much to maintain faireness among OSCs. */
382         if (atomic_long_read(cli->cl_lru_left) < cache->ccc_lru_max >> 2) {
383                 if (pages >= budget)
384                         return lru_shrink_max(cli);
385                 else if (pages >= budget / 2)
386                         return lru_shrink_min(cli);
387         } else {
388                 time64_t duration = ktime_get_real_seconds();
389                 long timediff;
390
391                 /* knock out pages by duration of no IO activity */
392                 duration -= cli->cl_lru_last_used;
393                 /*
394                  * The difference shouldn't be more than 70 years
395                  * so we can safely case to a long. Round to
396                  * approximately 1 minute.
397                  */
398                 timediff = (long)(duration >> 6);
399                 if (timediff > 0 && pages >= budget / timediff)
400                         return lru_shrink_min(cli);
401         }
402         return 0;
403 }
404
405 int lru_queue_work(const struct lu_env *env, void *data)
406 {
407         struct client_obd *cli = data;
408         int count;
409
410         CDEBUG(D_CACHE, "%s: run LRU work for client obd\n", cli_name(cli));
411         count = osc_cache_too_much(cli);
412         if (count > 0) {
413                 int rc = osc_lru_shrink(env, cli, count, false);
414
415                 CDEBUG(D_CACHE, "%s: shrank %d/%d pages from client obd\n",
416                        cli_name(cli), rc, count);
417                 if (rc >= count) {
418                         CDEBUG(D_CACHE, "%s: queue again\n", cli_name(cli));
419                         ptlrpcd_queue_work(cli->cl_lru_work);
420                 }
421         }
422
423         RETURN(0);
424 }
425
426 void osc_lru_add_batch(struct client_obd *cli, struct list_head *plist)
427 {
428         struct list_head lru = LIST_HEAD_INIT(lru);
429         struct osc_async_page *oap;
430         long npages = 0;
431
432         list_for_each_entry(oap, plist, oap_pending_item) {
433                 struct osc_page *opg = oap2osc_page(oap);
434
435                 if (!opg->ops_in_lru)
436                         continue;
437
438                 ++npages;
439                 LASSERT(list_empty(&opg->ops_lru));
440                 list_add(&opg->ops_lru, &lru);
441         }
442
443         if (npages > 0) {
444                 spin_lock(&cli->cl_lru_list_lock);
445                 list_splice_tail(&lru, &cli->cl_lru_list);
446                 atomic_long_sub(npages, &cli->cl_lru_busy);
447                 atomic_long_add(npages, &cli->cl_lru_in_list);
448                 cli->cl_lru_last_used = ktime_get_real_seconds();
449                 spin_unlock(&cli->cl_lru_list_lock);
450
451                 if (waitqueue_active(&osc_lru_waitq))
452                         (void)ptlrpcd_queue_work(cli->cl_lru_work);
453         }
454 }
455
456 static void __osc_lru_del(struct client_obd *cli, struct osc_page *opg)
457 {
458         LASSERT(atomic_long_read(&cli->cl_lru_in_list) > 0);
459         list_del_init(&opg->ops_lru);
460         atomic_long_dec(&cli->cl_lru_in_list);
461 }
462
463 /**
464  * Page is being destroyed. The page may be not in LRU list, if the transfer
465  * has never finished(error occurred).
466  */
467 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg)
468 {
469         if (opg->ops_in_lru) {
470                 spin_lock(&cli->cl_lru_list_lock);
471                 if (!list_empty(&opg->ops_lru)) {
472                         __osc_lru_del(cli, opg);
473                 } else {
474                         LASSERT(atomic_long_read(&cli->cl_lru_busy) > 0);
475                         atomic_long_dec(&cli->cl_lru_busy);
476                 }
477                 spin_unlock(&cli->cl_lru_list_lock);
478
479                 atomic_long_inc(cli->cl_lru_left);
480                 /* this is a great place to release more LRU pages if
481                  * this osc occupies too many LRU pages and kernel is
482                  * stealing one of them. */
483                 if (osc_cache_too_much(cli)) {
484                         CDEBUG(D_CACHE, "%s: queue LRU work\n", cli_name(cli));
485                         (void)ptlrpcd_queue_work(cli->cl_lru_work);
486                 }
487                 wake_up(&osc_lru_waitq);
488         } else {
489                 LASSERT(list_empty(&opg->ops_lru));
490         }
491 }
492
493 /**
494  * Delete page from LRU list for redirty.
495  */
496 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg)
497 {
498         /* If page is being transferred for the first time,
499          * ops_lru should be empty */
500         if (opg->ops_in_lru) {
501                 spin_lock(&cli->cl_lru_list_lock);
502                 if (!list_empty(&opg->ops_lru)) {
503                         __osc_lru_del(cli, opg);
504                         atomic_long_inc(&cli->cl_lru_busy);
505                 }
506                 spin_unlock(&cli->cl_lru_list_lock);
507         }
508 }
509
510 static void discard_pagevec(const struct lu_env *env, struct cl_io *io,
511                                 struct cl_page **pvec, int max_index)
512 {
513         int i;
514
515         for (i = 0; i < max_index; i++) {
516                 struct cl_page *page = pvec[i];
517
518                 LASSERT(cl_page_is_owned(page, io));
519                 cl_page_delete(env, page);
520                 cl_page_discard(env, io, page);
521                 cl_page_disown(env, io, page);
522                 cl_page_put(env, page);
523
524                 pvec[i] = NULL;
525         }
526 }
527
528 /**
529  * Check if a cl_page can be released, i.e, it's not being used.
530  *
531  * If unstable account is turned on, bulk transfer may hold one refcount
532  * for recovery so we need to check vmpage refcount as well; otherwise,
533  * even we can destroy cl_page but the corresponding vmpage can't be reused.
534  */
535 static inline bool lru_page_busy(struct client_obd *cli, struct cl_page *page)
536 {
537         if (cl_page_in_use_noref(page))
538                 return true;
539
540         if (cli->cl_cache->ccc_unstable_check) {
541                 struct page *vmpage = cl_page_vmpage(page);
542
543                 /* vmpage have two known users: cl_page and VM page cache */
544                 if (page_count(vmpage) - page_mapcount(vmpage) > 2)
545                         return true;
546         }
547         return false;
548 }
549
550 /**
551  * Drop @target of pages from LRU at most.
552  */
553 long osc_lru_shrink(const struct lu_env *env, struct client_obd *cli,
554                    long target, bool force)
555 {
556         struct cl_io *io;
557         struct cl_object *clobj = NULL;
558         struct cl_page **pvec;
559         struct osc_page *opg;
560         long count = 0;
561         int maxscan = 0;
562         int index = 0;
563         int rc = 0;
564         ENTRY;
565
566         LASSERT(atomic_long_read(&cli->cl_lru_in_list) >= 0);
567         if (atomic_long_read(&cli->cl_lru_in_list) == 0 || target <= 0)
568                 RETURN(0);
569
570         CDEBUG(D_CACHE, "%s: shrinkers: %d, force: %d\n",
571                cli_name(cli), atomic_read(&cli->cl_lru_shrinkers), force);
572         if (!force) {
573                 if (atomic_read(&cli->cl_lru_shrinkers) > 0)
574                         RETURN(-EBUSY);
575
576                 if (atomic_inc_return(&cli->cl_lru_shrinkers) > 1) {
577                         atomic_dec(&cli->cl_lru_shrinkers);
578                         RETURN(-EBUSY);
579                 }
580         } else {
581                 atomic_inc(&cli->cl_lru_shrinkers);
582         }
583
584         pvec = (struct cl_page **)osc_env_info(env)->oti_pvec;
585         io = &osc_env_info(env)->oti_io;
586
587         spin_lock(&cli->cl_lru_list_lock);
588         if (force)
589                 cli->cl_lru_reclaim++;
590         maxscan = min(target << 1, atomic_long_read(&cli->cl_lru_in_list));
591         while (!list_empty(&cli->cl_lru_list)) {
592                 struct cl_page *page;
593                 bool will_free = false;
594
595                 if (!force && atomic_read(&cli->cl_lru_shrinkers) > 1)
596                         break;
597
598                 if (--maxscan < 0)
599                         break;
600
601                 opg = list_entry(cli->cl_lru_list.next, struct osc_page,
602                                  ops_lru);
603                 page = opg->ops_cl.cpl_page;
604                 if (lru_page_busy(cli, page)) {
605                         list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
606                         continue;
607                 }
608
609                 LASSERT(page->cp_obj != NULL);
610                 if (clobj != page->cp_obj) {
611                         struct cl_object *tmp = page->cp_obj;
612
613                         cl_object_get(tmp);
614                         spin_unlock(&cli->cl_lru_list_lock);
615
616                         if (clobj != NULL) {
617                                 discard_pagevec(env, io, pvec, index);
618                                 index = 0;
619
620                                 cl_io_fini(env, io);
621                                 cl_object_put(env, clobj);
622                                 clobj = NULL;
623                         }
624
625                         clobj = tmp;
626                         io->ci_obj = clobj;
627                         io->ci_ignore_layout = 1;
628                         rc = cl_io_init(env, io, CIT_MISC, clobj);
629
630                         spin_lock(&cli->cl_lru_list_lock);
631
632                         if (rc != 0)
633                                 break;
634
635                         ++maxscan;
636                         continue;
637                 }
638
639                 if (cl_page_own_try(env, io, page) == 0) {
640                         if (!lru_page_busy(cli, page)) {
641                                 /* remove it from lru list earlier to avoid
642                                  * lock contention */
643                                 __osc_lru_del(cli, opg);
644                                 opg->ops_in_lru = 0; /* will be discarded */
645
646                                 cl_page_get(page);
647                                 will_free = true;
648                         } else {
649                                 cl_page_disown(env, io, page);
650                         }
651                 }
652
653                 if (!will_free) {
654                         list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
655                         continue;
656                 }
657
658                 /* Don't discard and free the page with cl_lru_list held */
659                 pvec[index++] = page;
660                 if (unlikely(index == OTI_PVEC_SIZE)) {
661                         spin_unlock(&cli->cl_lru_list_lock);
662                         discard_pagevec(env, io, pvec, index);
663                         index = 0;
664
665                         spin_lock(&cli->cl_lru_list_lock);
666                 }
667
668                 if (++count >= target)
669                         break;
670         }
671         spin_unlock(&cli->cl_lru_list_lock);
672
673         if (clobj != NULL) {
674                 discard_pagevec(env, io, pvec, index);
675
676                 cl_io_fini(env, io);
677                 cl_object_put(env, clobj);
678         }
679
680         atomic_dec(&cli->cl_lru_shrinkers);
681         if (count > 0) {
682                 atomic_long_add(count, cli->cl_lru_left);
683                 wake_up_all(&osc_lru_waitq);
684         }
685         RETURN(count > 0 ? count : rc);
686 }
687
688 /**
689  * Reclaim LRU pages by an IO thread. The caller wants to reclaim at least
690  * \@npages of LRU slots. For performance consideration, it's better to drop
691  * LRU pages in batch. Therefore, the actual number is adjusted at least
692  * max_pages_per_rpc.
693  */
694 static long osc_lru_reclaim(struct client_obd *cli, unsigned long npages)
695 {
696         struct lu_env *env;
697         struct cl_client_cache *cache = cli->cl_cache;
698         int max_scans;
699         __u16 refcheck;
700         long rc = 0;
701         ENTRY;
702
703         LASSERT(cache != NULL);
704
705         env = cl_env_get(&refcheck);
706         if (IS_ERR(env))
707                 RETURN(rc);
708
709         npages = max_t(int, npages, cli->cl_max_pages_per_rpc);
710         CDEBUG(D_CACHE, "%s: start to reclaim %ld pages from LRU\n",
711                cli_name(cli), npages);
712         rc = osc_lru_shrink(env, cli, npages, true);
713         if (rc >= npages) {
714                 CDEBUG(D_CACHE, "%s: reclaimed %ld/%ld pages from LRU\n",
715                        cli_name(cli), rc, npages);
716                 if (osc_cache_too_much(cli) > 0)
717                         ptlrpcd_queue_work(cli->cl_lru_work);
718                 GOTO(out, rc);
719         } else if (rc > 0) {
720                 npages -= rc;
721         }
722
723         CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %ld/%ld, want: %ld\n",
724                 cli_name(cli), cli, atomic_long_read(&cli->cl_lru_in_list),
725                 atomic_long_read(&cli->cl_lru_busy), npages);
726
727         /* Reclaim LRU slots from other client_obd as it can't free enough
728          * from its own. This should rarely happen. */
729         spin_lock(&cache->ccc_lru_lock);
730         LASSERT(!list_empty(&cache->ccc_lru));
731
732         cache->ccc_lru_shrinkers++;
733         list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
734
735         max_scans = atomic_read(&cache->ccc_users) - 2;
736         while (--max_scans > 0 && !list_empty(&cache->ccc_lru)) {
737                 cli = list_entry(cache->ccc_lru.next, struct client_obd,
738                                  cl_lru_osc);
739
740                 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %ld, busy: %ld.\n",
741                         cli_name(cli), cli,
742                         atomic_long_read(&cli->cl_lru_in_list),
743                         atomic_long_read(&cli->cl_lru_busy));
744
745                 list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
746                 if (osc_cache_too_much(cli) > 0) {
747                         spin_unlock(&cache->ccc_lru_lock);
748
749                         rc = osc_lru_shrink(env, cli, npages, true);
750                         spin_lock(&cache->ccc_lru_lock);
751                         if (rc >= npages)
752                                 break;
753                         if (rc > 0)
754                                 npages -= rc;
755                 }
756         }
757         spin_unlock(&cache->ccc_lru_lock);
758
759 out:
760         cl_env_put(env, &refcheck);
761         CDEBUG(D_CACHE, "%s: cli %p freed %ld pages.\n",
762                 cli_name(cli), cli, rc);
763         return rc;
764 }
765
766 /**
767  * osc_lru_alloc() is called to allocate an LRU slot for a cl_page.
768  *
769  * Usually the LRU slots are reserved in osc_io_iter_rw_init().
770  * Only in the case that the LRU slots are in extreme shortage, it should
771  * have reserved enough slots for an IO.
772  */
773 static int osc_lru_alloc(const struct lu_env *env, struct client_obd *cli,
774                          struct osc_page *opg)
775 {
776         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
777         struct osc_io *oio = osc_env_io(env);
778         int rc = 0;
779         ENTRY;
780
781         if (cli->cl_cache == NULL) /* shall not be in LRU */
782                 RETURN(0);
783
784         if (oio->oi_lru_reserved > 0) {
785                 --oio->oi_lru_reserved;
786                 goto out;
787         }
788
789         LASSERT(atomic_long_read(cli->cl_lru_left) >= 0);
790         while (!atomic_long_add_unless(cli->cl_lru_left, -1, 0)) {
791                 /* run out of LRU spaces, try to drop some by itself */
792                 rc = osc_lru_reclaim(cli, 1);
793                 if (rc < 0)
794                         break;
795                 if (rc > 0)
796                         continue;
797
798                 cond_resched();
799                 rc = l_wait_event(osc_lru_waitq,
800                                 atomic_long_read(cli->cl_lru_left) > 0,
801                                 &lwi);
802                 if (rc < 0)
803                         break;
804         }
805
806 out:
807         if (rc >= 0) {
808                 atomic_long_inc(&cli->cl_lru_busy);
809                 opg->ops_in_lru = 1;
810                 rc = 0;
811         }
812
813         RETURN(rc);
814 }
815
816 /**
817  * osc_lru_reserve() is called to reserve enough LRU slots for I/O.
818  *
819  * The benefit of doing this is to reduce contention against atomic counter
820  * cl_lru_left by changing it from per-page access to per-IO access.
821  */
822 unsigned long osc_lru_reserve(struct client_obd *cli, unsigned long npages)
823 {
824         unsigned long reserved = 0;
825         unsigned long max_pages;
826         unsigned long c;
827
828         /* reserve a full RPC window at most to avoid that a thread accidentally
829          * consumes too many LRU slots */
830         max_pages = cli->cl_max_pages_per_rpc * cli->cl_max_rpcs_in_flight;
831         if (npages > max_pages)
832                 npages = max_pages;
833
834         c = atomic_long_read(cli->cl_lru_left);
835         if (c < npages && osc_lru_reclaim(cli, npages) > 0)
836                 c = atomic_long_read(cli->cl_lru_left);
837         while (c >= npages) {
838                 if (c == atomic_long_cmpxchg(cli->cl_lru_left, c, c - npages)) {
839                         reserved = npages;
840                         break;
841                 }
842                 c = atomic_long_read(cli->cl_lru_left);
843         }
844         if (atomic_long_read(cli->cl_lru_left) < max_pages) {
845                 /* If there aren't enough pages in the per-OSC LRU then
846                  * wake up the LRU thread to try and clear out space, so
847                  * we don't block if pages are being dirtied quickly. */
848                 CDEBUG(D_CACHE, "%s: queue LRU, left: %lu/%ld.\n",
849                        cli_name(cli), atomic_long_read(cli->cl_lru_left),
850                        max_pages);
851                 (void)ptlrpcd_queue_work(cli->cl_lru_work);
852         }
853
854         return reserved;
855 }
856
857 /**
858  * osc_lru_unreserve() is called to unreserve LRU slots.
859  *
860  * LRU slots reserved by osc_lru_reserve() may have entries left due to several
861  * reasons such as page already existing or I/O error. Those reserved slots
862  * should be freed by calling this function.
863  */
864 void osc_lru_unreserve(struct client_obd *cli, unsigned long npages)
865 {
866         atomic_long_add(npages, cli->cl_lru_left);
867         wake_up_all(&osc_lru_waitq);
868 }
869
870 /**
871  * Atomic operations are expensive. We accumulate the accounting for the
872  * same page zone to get better performance.
873  * In practice this can work pretty good because the pages in the same RPC
874  * are likely from the same page zone.
875  */
876 static inline void unstable_page_accounting(struct ptlrpc_bulk_desc *desc,
877                                             int factor)
878 {
879         int page_count = desc->bd_iov_count;
880         void *zone = NULL;
881         int count = 0;
882         int i;
883
884         LASSERT(ptlrpc_is_bulk_desc_kiov(desc->bd_type));
885
886         for (i = 0; i < page_count; i++) {
887                 void *pz = page_zone(BD_GET_KIOV(desc, i).kiov_page);
888
889                 if (likely(pz == zone)) {
890                         ++count;
891                         continue;
892                 }
893
894                 if (count > 0) {
895                         mod_zone_page_state(zone, NR_UNSTABLE_NFS,
896                                             factor * count);
897                         count = 0;
898                 }
899                 zone = pz;
900                 ++count;
901         }
902         if (count > 0)
903                 mod_zone_page_state(zone, NR_UNSTABLE_NFS, factor * count);
904 }
905
906 static inline void add_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
907 {
908         unstable_page_accounting(desc, 1);
909 }
910
911 static inline void dec_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
912 {
913         unstable_page_accounting(desc, -1);
914 }
915
916 /**
917  * Performs "unstable" page accounting. This function balances the
918  * increment operations performed in osc_inc_unstable_pages. It is
919  * registered as the RPC request callback, and is executed when the
920  * bulk RPC is committed on the server. Thus at this point, the pages
921  * involved in the bulk transfer are no longer considered unstable.
922  *
923  * If this function is called, the request should have been committed
924  * or req:rq_unstable must have been set; it implies that the unstable
925  * statistic have been added.
926  */
927 void osc_dec_unstable_pages(struct ptlrpc_request *req)
928 {
929         struct ptlrpc_bulk_desc *desc       = req->rq_bulk;
930         struct client_obd       *cli        = &req->rq_import->imp_obd->u.cli;
931         int                      page_count = desc->bd_iov_count;
932         long                     unstable_count;
933
934         LASSERT(page_count >= 0);
935         dec_unstable_page_accounting(desc);
936
937         unstable_count = atomic_long_sub_return(page_count,
938                                                 &cli->cl_unstable_count);
939         LASSERT(unstable_count >= 0);
940
941         unstable_count = atomic_long_sub_return(page_count,
942                                            &cli->cl_cache->ccc_unstable_nr);
943         LASSERT(unstable_count >= 0);
944         if (unstable_count == 0)
945                 wake_up_all(&cli->cl_cache->ccc_unstable_waitq);
946
947         if (waitqueue_active(&osc_lru_waitq))
948                 (void)ptlrpcd_queue_work(cli->cl_lru_work);
949 }
950
951 /**
952  * "unstable" page accounting. See: osc_dec_unstable_pages.
953  */
954 void osc_inc_unstable_pages(struct ptlrpc_request *req)
955 {
956         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
957         struct client_obd       *cli  = &req->rq_import->imp_obd->u.cli;
958         long                     page_count = desc->bd_iov_count;
959
960         /* No unstable page tracking */
961         if (cli->cl_cache == NULL || !cli->cl_cache->ccc_unstable_check)
962                 return;
963
964         add_unstable_page_accounting(desc);
965         atomic_long_add(page_count, &cli->cl_unstable_count);
966         atomic_long_add(page_count, &cli->cl_cache->ccc_unstable_nr);
967
968         /* If the request has already been committed (i.e. brw_commit
969          * called via rq_commit_cb), we need to undo the unstable page
970          * increments we just performed because rq_commit_cb wont be
971          * called again. */
972         spin_lock(&req->rq_lock);
973         if (unlikely(req->rq_committed)) {
974                 spin_unlock(&req->rq_lock);
975
976                 osc_dec_unstable_pages(req);
977         } else {
978                 req->rq_unstable = 1;
979                 spin_unlock(&req->rq_lock);
980         }
981 }
982
983 /**
984  * Check if it piggybacks SOFT_SYNC flag to OST from this OSC.
985  * This function will be called by every BRW RPC so it's critical
986  * to make this function fast.
987  */
988 bool osc_over_unstable_soft_limit(struct client_obd *cli)
989 {
990         long unstable_nr, osc_unstable_count;
991
992         /* Can't check cli->cl_unstable_count, therefore, no soft limit */
993         if (cli->cl_cache == NULL || !cli->cl_cache->ccc_unstable_check)
994                 return false;
995
996         osc_unstable_count = atomic_long_read(&cli->cl_unstable_count);
997         unstable_nr = atomic_long_read(&cli->cl_cache->ccc_unstable_nr);
998
999         CDEBUG(D_CACHE,
1000                "%s: cli: %p unstable pages: %lu, osc unstable pages: %lu\n",
1001                cli_name(cli), cli, unstable_nr, osc_unstable_count);
1002
1003         /* If the LRU slots are in shortage - 25% remaining AND this OSC
1004          * has one full RPC window of unstable pages, it's a good chance
1005          * to piggyback a SOFT_SYNC flag.
1006          * Please notice that the OST won't take immediate response for the
1007          * SOFT_SYNC request so active OSCs will have more chance to carry
1008          * the flag, this is reasonable. */
1009         return unstable_nr > cli->cl_cache->ccc_lru_max >> 2 &&
1010                osc_unstable_count > cli->cl_max_pages_per_rpc *
1011                                     cli->cl_max_rpcs_in_flight;
1012 }
1013
1014 /**
1015  * Return how many LRU pages in the cache of all OSC devices
1016  *
1017  * \retval      return # of cached LRU pages times reclaimation tendency
1018  * \retval      SHRINK_STOP if it cannot do any scanning in this time
1019  */
1020 unsigned long osc_cache_shrink_count(struct shrinker *sk,
1021                                      struct shrink_control *sc)
1022 {
1023         struct client_obd *cli;
1024         unsigned long cached = 0;
1025
1026         spin_lock(&osc_shrink_lock);
1027         list_for_each_entry(cli, &osc_shrink_list, cl_shrink_list)
1028                 cached += atomic_long_read(&cli->cl_lru_in_list);
1029         spin_unlock(&osc_shrink_lock);
1030
1031         return (cached  * sysctl_vfs_cache_pressure) / 100;
1032 }
1033
1034 /**
1035  * Scan and try to reclaim sc->nr_to_scan cached LRU pages
1036  *
1037  * \retval      number of cached LRU pages reclaimed
1038  * \retval      SHRINK_STOP if it cannot do any scanning in this time
1039  *
1040  * Linux kernel will loop calling this shrinker scan routine with
1041  * sc->nr_to_scan = SHRINK_BATCH(128 for now) until kernel got enough memory.
1042  *
1043  * If sc->nr_to_scan is 0, the VM is querying the cache size, we don't need
1044  * to scan and try to reclaim LRU pages, just return 0 and
1045  * osc_cache_shrink_count() will report the LRU page number.
1046  */
1047 unsigned long osc_cache_shrink_scan(struct shrinker *sk,
1048                                     struct shrink_control *sc)
1049 {
1050         struct client_obd *cli;
1051         struct client_obd *stop_anchor = NULL;
1052         struct lu_env *env;
1053         long shrank = 0;
1054         int rc;
1055         __u16 refcheck;
1056
1057         if (sc->nr_to_scan == 0)
1058                 return 0;
1059
1060         if (!(sc->gfp_mask & __GFP_FS))
1061                 return SHRINK_STOP;
1062
1063         env = cl_env_get(&refcheck);
1064         if (IS_ERR(env))
1065                 return SHRINK_STOP;
1066
1067         spin_lock(&osc_shrink_lock);
1068         while (!list_empty(&osc_shrink_list)) {
1069                 cli = list_entry(osc_shrink_list.next, struct client_obd,
1070                                  cl_shrink_list);
1071
1072                 if (stop_anchor == NULL)
1073                         stop_anchor = cli;
1074                 else if (cli == stop_anchor)
1075                         break;
1076
1077                 list_move_tail(&cli->cl_shrink_list, &osc_shrink_list);
1078                 spin_unlock(&osc_shrink_lock);
1079
1080                 /* shrink no more than max_pages_per_rpc for an OSC */
1081                 rc = osc_lru_shrink(env, cli, (sc->nr_to_scan - shrank) >
1082                                     cli->cl_max_pages_per_rpc ?
1083                                     cli->cl_max_pages_per_rpc :
1084                                     sc->nr_to_scan - shrank, true);
1085                 if (rc > 0)
1086                         shrank += rc;
1087
1088                 if (shrank >= sc->nr_to_scan)
1089                         goto out;
1090
1091                 spin_lock(&osc_shrink_lock);
1092         }
1093         spin_unlock(&osc_shrink_lock);
1094
1095 out:
1096         cl_env_put(env, &refcheck);
1097
1098         return shrank;
1099 }
1100
1101 /** @} osc */