Merge branch 'mm-pkeys-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[cascardo/linux.git] / drivers / staging / lustre / 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, 2015, 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
40 #include "osc_cl_internal.h"
41
42 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg);
43 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg);
44 static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
45                            struct osc_page *opg);
46
47 /** \addtogroup osc
48  *  @{
49  */
50
51 /*****************************************************************************
52  *
53  * Page operations.
54  *
55  */
56 static void osc_page_transfer_get(struct osc_page *opg, const char *label)
57 {
58         struct cl_page *page = opg->ops_cl.cpl_page;
59
60         LASSERT(!opg->ops_transfer_pinned);
61         cl_page_get(page);
62         lu_ref_add_atomic(&page->cp_reference, label, page);
63         opg->ops_transfer_pinned = 1;
64 }
65
66 static void osc_page_transfer_put(const struct lu_env *env,
67                                   struct osc_page *opg)
68 {
69         struct cl_page *page = opg->ops_cl.cpl_page;
70
71         if (opg->ops_transfer_pinned) {
72                 opg->ops_transfer_pinned = 0;
73                 lu_ref_del(&page->cp_reference, "transfer", page);
74                 cl_page_put(env, page);
75         }
76 }
77
78 /**
79  * This is called once for every page when it is submitted for a transfer
80  * either opportunistic (osc_page_cache_add()), or immediate
81  * (osc_page_submit()).
82  */
83 static void osc_page_transfer_add(const struct lu_env *env,
84                                   struct osc_page *opg, enum cl_req_type crt)
85 {
86         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
87
88         osc_lru_use(osc_cli(obj), opg);
89
90         spin_lock(&obj->oo_seatbelt);
91         list_add(&opg->ops_inflight, &obj->oo_inflight[crt]);
92         opg->ops_submitter = current;
93         spin_unlock(&obj->oo_seatbelt);
94 }
95
96 int osc_page_cache_add(const struct lu_env *env,
97                        const struct cl_page_slice *slice, struct cl_io *io)
98 {
99         struct osc_page *opg = cl2osc_page(slice);
100         int result;
101
102         osc_page_transfer_get(opg, "transfer\0cache");
103         result = osc_queue_async_io(env, io, opg);
104         if (result != 0)
105                 osc_page_transfer_put(env, opg);
106         else
107                 osc_page_transfer_add(env, opg, CRT_WRITE);
108
109         return result;
110 }
111
112 void osc_index2policy(ldlm_policy_data_t *policy, const struct cl_object *obj,
113                       pgoff_t start, pgoff_t end)
114 {
115         memset(policy, 0, sizeof(*policy));
116         policy->l_extent.start = cl_offset(obj, start);
117         policy->l_extent.end = cl_offset(obj, end + 1) - 1;
118 }
119
120 static int osc_page_is_under_lock(const struct lu_env *env,
121                                   const struct cl_page_slice *slice,
122                                   struct cl_io *unused, pgoff_t *max_index)
123 {
124         struct osc_page *opg = cl2osc_page(slice);
125         struct ldlm_lock *dlmlock;
126         int result = -ENODATA;
127
128         dlmlock = osc_dlmlock_at_pgoff(env, cl2osc(slice->cpl_obj),
129                                        osc_index(opg), 1, 0);
130         if (dlmlock) {
131                 *max_index = cl_index(slice->cpl_obj,
132                                       dlmlock->l_policy_data.l_extent.end);
133                 LDLM_LOCK_PUT(dlmlock);
134                 result = 0;
135         }
136         return result;
137 }
138
139 static const char *osc_list(struct list_head *head)
140 {
141         return list_empty(head) ? "-" : "+";
142 }
143
144 static inline unsigned long osc_submit_duration(struct osc_page *opg)
145 {
146         if (opg->ops_submit_time == 0)
147                 return 0;
148
149         return (cfs_time_current() - opg->ops_submit_time);
150 }
151
152 static int osc_page_print(const struct lu_env *env,
153                           const struct cl_page_slice *slice,
154                           void *cookie, lu_printer_t printer)
155 {
156         struct osc_page *opg = cl2osc_page(slice);
157         struct osc_async_page *oap = &opg->ops_oap;
158         struct osc_object *obj = cl2osc(slice->cpl_obj);
159         struct client_obd *cli = &osc_export(obj)->exp_obd->u.cli;
160
161         return (*printer)(env, cookie, LUSTRE_OSC_NAME "-page@%p %lu: 1< %#x %d %u %s %s > 2< %llu %u %u %#x %#x | %p %p %p > 3< %s %p %d %lu %d > 4< %d %d %d %lu %s | %s %s %s %s > 5< %s %s %s %s | %d %s | %d %s %s>\n",
162                           opg, osc_index(opg),
163                           /* 1 */
164                           oap->oap_magic, oap->oap_cmd,
165                           oap->oap_interrupted,
166                           osc_list(&oap->oap_pending_item),
167                           osc_list(&oap->oap_rpc_item),
168                           /* 2 */
169                           oap->oap_obj_off, oap->oap_page_off, oap->oap_count,
170                           oap->oap_async_flags, oap->oap_brw_flags,
171                           oap->oap_request, oap->oap_cli, obj,
172                           /* 3 */
173                           osc_list(&opg->ops_inflight),
174                           opg->ops_submitter, opg->ops_transfer_pinned,
175                           osc_submit_duration(opg), opg->ops_srvlock,
176                           /* 4 */
177                           cli->cl_r_in_flight, cli->cl_w_in_flight,
178                           cli->cl_max_rpcs_in_flight,
179                           cli->cl_avail_grant,
180                           osc_list(&cli->cl_cache_waiters),
181                           osc_list(&cli->cl_loi_ready_list),
182                           osc_list(&cli->cl_loi_hp_ready_list),
183                           osc_list(&cli->cl_loi_write_list),
184                           osc_list(&cli->cl_loi_read_list),
185                           /* 5 */
186                           osc_list(&obj->oo_ready_item),
187                           osc_list(&obj->oo_hp_ready_item),
188                           osc_list(&obj->oo_write_item),
189                           osc_list(&obj->oo_read_item),
190                           atomic_read(&obj->oo_nr_reads),
191                           osc_list(&obj->oo_reading_exts),
192                           atomic_read(&obj->oo_nr_writes),
193                           osc_list(&obj->oo_hp_exts),
194                           osc_list(&obj->oo_urgent_exts));
195 }
196
197 static void osc_page_delete(const struct lu_env *env,
198                             const struct cl_page_slice *slice)
199 {
200         struct osc_page *opg = cl2osc_page(slice);
201         struct osc_object *obj = cl2osc(opg->ops_cl.cpl_obj);
202         int rc;
203
204         CDEBUG(D_TRACE, "%p\n", opg);
205         osc_page_transfer_put(env, opg);
206         rc = osc_teardown_async_page(env, obj, opg);
207         if (rc) {
208                 CL_PAGE_DEBUG(D_ERROR, env, slice->cpl_page,
209                               "Trying to teardown failed: %d\n", rc);
210                 LASSERT(0);
211         }
212
213         spin_lock(&obj->oo_seatbelt);
214         if (opg->ops_submitter) {
215                 LASSERT(!list_empty(&opg->ops_inflight));
216                 list_del_init(&opg->ops_inflight);
217                 opg->ops_submitter = NULL;
218         }
219         spin_unlock(&obj->oo_seatbelt);
220
221         osc_lru_del(osc_cli(obj), opg);
222
223         if (slice->cpl_page->cp_type == CPT_CACHEABLE) {
224                 void *value;
225
226                 spin_lock(&obj->oo_tree_lock);
227                 value = radix_tree_delete(&obj->oo_tree, osc_index(opg));
228                 if (value)
229                         --obj->oo_npages;
230                 spin_unlock(&obj->oo_tree_lock);
231
232                 LASSERT(ergo(value, value == opg));
233         }
234 }
235
236 static void osc_page_clip(const struct lu_env *env,
237                           const struct cl_page_slice *slice, int from, int to)
238 {
239         struct osc_page *opg = cl2osc_page(slice);
240         struct osc_async_page *oap = &opg->ops_oap;
241
242         opg->ops_from = from;
243         opg->ops_to = to;
244         spin_lock(&oap->oap_lock);
245         oap->oap_async_flags |= ASYNC_COUNT_STABLE;
246         spin_unlock(&oap->oap_lock);
247 }
248
249 static int osc_page_cancel(const struct lu_env *env,
250                            const struct cl_page_slice *slice)
251 {
252         struct osc_page *opg = cl2osc_page(slice);
253         int rc = 0;
254
255         /* Check if the transferring against this page
256          * is completed, or not even queued.
257          */
258         if (opg->ops_transfer_pinned)
259                 /* FIXME: may not be interrupted.. */
260                 rc = osc_cancel_async_page(env, opg);
261         LASSERT(ergo(rc == 0, opg->ops_transfer_pinned == 0));
262         return rc;
263 }
264
265 static int osc_page_flush(const struct lu_env *env,
266                           const struct cl_page_slice *slice,
267                           struct cl_io *io)
268 {
269         struct osc_page *opg = cl2osc_page(slice);
270         int rc;
271
272         rc = osc_flush_async_page(env, io, opg);
273         return rc;
274 }
275
276 static const struct cl_page_operations osc_page_ops = {
277         .cpo_print       = osc_page_print,
278         .cpo_delete     = osc_page_delete,
279         .cpo_is_under_lock = osc_page_is_under_lock,
280         .cpo_clip          = osc_page_clip,
281         .cpo_cancel      = osc_page_cancel,
282         .cpo_flush        = osc_page_flush
283 };
284
285 int osc_page_init(const struct lu_env *env, struct cl_object *obj,
286                   struct cl_page *page, pgoff_t index)
287 {
288         struct osc_object *osc = cl2osc(obj);
289         struct osc_page *opg = cl_object_page_slice(obj, page);
290         int result;
291
292         opg->ops_from = 0;
293         opg->ops_to = PAGE_SIZE;
294
295         result = osc_prep_async_page(osc, opg, page->cp_vmpage,
296                                      cl_offset(obj, index));
297         if (result == 0) {
298                 struct osc_io *oio = osc_env_io(env);
299
300                 opg->ops_srvlock = osc_io_srvlock(oio);
301                 cl_page_slice_add(page, &opg->ops_cl, obj, index,
302                                   &osc_page_ops);
303         }
304         /* ops_inflight and ops_lru are the same field, but it doesn't
305          * hurt to initialize it twice :-)
306          */
307         INIT_LIST_HEAD(&opg->ops_inflight);
308         INIT_LIST_HEAD(&opg->ops_lru);
309
310         /* reserve an LRU space for this page */
311         if (page->cp_type == CPT_CACHEABLE && result == 0) {
312                 result = osc_lru_reserve(env, osc, opg);
313                 if (result == 0) {
314                         spin_lock(&osc->oo_tree_lock);
315                         result = radix_tree_insert(&osc->oo_tree, index, opg);
316                         if (result == 0)
317                                 ++osc->oo_npages;
318                         spin_unlock(&osc->oo_tree_lock);
319                         LASSERT(result == 0);
320                 }
321         }
322
323         return result;
324 }
325
326 /**
327  * Helper function called by osc_io_submit() for every page in an immediate
328  * transfer (i.e., transferred synchronously).
329  */
330 void osc_page_submit(const struct lu_env *env, struct osc_page *opg,
331                      enum cl_req_type crt, int brw_flags)
332 {
333         struct osc_async_page *oap = &opg->ops_oap;
334
335         LASSERTF(oap->oap_magic == OAP_MAGIC, "Bad oap magic: oap %p, magic 0x%x\n",
336                  oap, oap->oap_magic);
337         LASSERT(oap->oap_async_flags & ASYNC_READY);
338         LASSERT(oap->oap_async_flags & ASYNC_COUNT_STABLE);
339
340         oap->oap_cmd = crt == CRT_WRITE ? OBD_BRW_WRITE : OBD_BRW_READ;
341         oap->oap_page_off = opg->ops_from;
342         oap->oap_count = opg->ops_to - opg->ops_from;
343         oap->oap_brw_flags = brw_flags | OBD_BRW_SYNC;
344
345         if (capable(CFS_CAP_SYS_RESOURCE)) {
346                 oap->oap_brw_flags |= OBD_BRW_NOQUOTA;
347                 oap->oap_cmd |= OBD_BRW_NOQUOTA;
348         }
349
350         opg->ops_submit_time = cfs_time_current();
351         osc_page_transfer_get(opg, "transfer\0imm");
352         osc_page_transfer_add(env, opg, crt);
353 }
354
355 /* --------------- LRU page management ------------------ */
356
357 /* OSC is a natural place to manage LRU pages as applications are specialized
358  * to write OSC by OSC. Ideally, if one OSC is used more frequently it should
359  * occupy more LRU slots. On the other hand, we should avoid using up all LRU
360  * slots (client_obd::cl_lru_left) otherwise process has to be put into sleep
361  * for free LRU slots - this will be very bad so the algorithm requires each
362  * OSC to free slots voluntarily to maintain a reasonable number of free slots
363  * at any time.
364  */
365
366 static DECLARE_WAIT_QUEUE_HEAD(osc_lru_waitq);
367 /* LRU pages are freed in batch mode. OSC should at least free this
368  * number of pages to avoid running out of LRU budget, and..
369  */
370 static const int lru_shrink_min = 2 << (20 - PAGE_SHIFT);  /* 2M */
371 /* free this number at most otherwise it will take too long time to finish. */
372 static const int lru_shrink_max = 8 << (20 - PAGE_SHIFT); /* 8M */
373
374 /* Check if we can free LRU slots from this OSC. If there exists LRU waiters,
375  * we should free slots aggressively. In this way, slots are freed in a steady
376  * step to maintain fairness among OSCs.
377  *
378  * Return how many LRU pages should be freed.
379  */
380 static int osc_cache_too_much(struct client_obd *cli)
381 {
382         struct cl_client_cache *cache = cli->cl_cache;
383         long pages = atomic_long_read(&cli->cl_lru_in_list);
384         unsigned long budget;
385
386         budget = cache->ccc_lru_max / (atomic_read(&cache->ccc_users) - 2);
387
388         /* if it's going to run out LRU slots, we should free some, but not
389          * too much to maintain fairness among OSCs.
390          */
391         if (atomic_long_read(cli->cl_lru_left) < cache->ccc_lru_max >> 4) {
392                 if (pages >= budget)
393                         return lru_shrink_max;
394                 else if (pages >= budget / 2)
395                         return lru_shrink_min;
396         } else if (pages >= budget * 2) {
397                 return lru_shrink_min;
398         }
399         return 0;
400 }
401
402 int lru_queue_work(const struct lu_env *env, void *data)
403 {
404         struct client_obd *cli = data;
405
406         CDEBUG(D_CACHE, "Run LRU work for client obd %p.\n", cli);
407
408         if (osc_cache_too_much(cli))
409                 osc_lru_shrink(env, cli, lru_shrink_max, true);
410
411         return 0;
412 }
413
414 void osc_lru_add_batch(struct client_obd *cli, struct list_head *plist)
415 {
416         LIST_HEAD(lru);
417         struct osc_async_page *oap;
418         long npages = 0;
419
420         list_for_each_entry(oap, plist, oap_pending_item) {
421                 struct osc_page *opg = oap2osc_page(oap);
422
423                 if (!opg->ops_in_lru)
424                         continue;
425
426                 ++npages;
427                 LASSERT(list_empty(&opg->ops_lru));
428                 list_add(&opg->ops_lru, &lru);
429         }
430
431         if (npages > 0) {
432                 spin_lock(&cli->cl_lru_list_lock);
433                 list_splice_tail(&lru, &cli->cl_lru_list);
434                 atomic_long_sub(npages, &cli->cl_lru_busy);
435                 atomic_long_add(npages, &cli->cl_lru_in_list);
436                 spin_unlock(&cli->cl_lru_list_lock);
437
438                 /* XXX: May set force to be true for better performance */
439                 if (osc_cache_too_much(cli))
440                         (void)ptlrpcd_queue_work(cli->cl_lru_work);
441         }
442 }
443
444 static void __osc_lru_del(struct client_obd *cli, struct osc_page *opg)
445 {
446         LASSERT(atomic_long_read(&cli->cl_lru_in_list) > 0);
447         list_del_init(&opg->ops_lru);
448         atomic_long_dec(&cli->cl_lru_in_list);
449 }
450
451 /**
452  * Page is being destroyed. The page may be not in LRU list, if the transfer
453  * has never finished(error occurred).
454  */
455 static void osc_lru_del(struct client_obd *cli, struct osc_page *opg)
456 {
457         if (opg->ops_in_lru) {
458                 spin_lock(&cli->cl_lru_list_lock);
459                 if (!list_empty(&opg->ops_lru)) {
460                         __osc_lru_del(cli, opg);
461                 } else {
462                         LASSERT(atomic_long_read(&cli->cl_lru_busy) > 0);
463                         atomic_long_dec(&cli->cl_lru_busy);
464                 }
465                 spin_unlock(&cli->cl_lru_list_lock);
466
467                 atomic_long_inc(cli->cl_lru_left);
468                 /* this is a great place to release more LRU pages if
469                  * this osc occupies too many LRU pages and kernel is
470                  * stealing one of them.
471                  */
472                 if (!memory_pressure_get())
473                         (void)ptlrpcd_queue_work(cli->cl_lru_work);
474                 wake_up(&osc_lru_waitq);
475         } else {
476                 LASSERT(list_empty(&opg->ops_lru));
477         }
478 }
479
480 /**
481  * Delete page from LRUlist for redirty.
482  */
483 static void osc_lru_use(struct client_obd *cli, struct osc_page *opg)
484 {
485         /* If page is being transferred for the first time,
486          * ops_lru should be empty
487          */
488         if (opg->ops_in_lru && !list_empty(&opg->ops_lru)) {
489                 spin_lock(&cli->cl_lru_list_lock);
490                 __osc_lru_del(cli, opg);
491                 spin_unlock(&cli->cl_lru_list_lock);
492                 atomic_long_inc(&cli->cl_lru_busy);
493         }
494 }
495
496 static void discard_pagevec(const struct lu_env *env, struct cl_io *io,
497                             struct cl_page **pvec, int max_index)
498 {
499         int i;
500
501         for (i = 0; i < max_index; i++) {
502                 struct cl_page *page = pvec[i];
503
504                 LASSERT(cl_page_is_owned(page, io));
505                 cl_page_discard(env, io, page);
506                 cl_page_disown(env, io, page);
507                 cl_page_put(env, page);
508
509                 pvec[i] = NULL;
510         }
511 }
512
513 /**
514  * Check if a cl_page can be released, i.e, it's not being used.
515  *
516  * If unstable account is turned on, bulk transfer may hold one refcount
517  * for recovery so we need to check vmpage refcount as well; otherwise,
518  * even we can destroy cl_page but the corresponding vmpage can't be reused.
519  */
520 static inline bool lru_page_busy(struct client_obd *cli, struct cl_page *page)
521 {
522         if (cl_page_in_use_noref(page))
523                 return true;
524
525         if (cli->cl_cache->ccc_unstable_check) {
526                 struct page *vmpage = cl_page_vmpage(page);
527
528                 /* vmpage have two known users: cl_page and VM page cache */
529                 if (page_count(vmpage) - page_mapcount(vmpage) > 2)
530                         return true;
531         }
532         return false;
533 }
534
535 /**
536  * Drop @target of pages from LRU at most.
537  */
538 long osc_lru_shrink(const struct lu_env *env, struct client_obd *cli,
539                     long target, bool force)
540 {
541         struct cl_io *io;
542         struct cl_object *clobj = NULL;
543         struct cl_page **pvec;
544         struct osc_page *opg;
545         struct osc_page *temp;
546         int maxscan = 0;
547         long count = 0;
548         int index = 0;
549         int rc = 0;
550
551         LASSERT(atomic_long_read(&cli->cl_lru_in_list) >= 0);
552         if (atomic_long_read(&cli->cl_lru_in_list) == 0 || target <= 0)
553                 return 0;
554
555         if (!force) {
556                 if (atomic_read(&cli->cl_lru_shrinkers) > 0)
557                         return -EBUSY;
558
559                 if (atomic_inc_return(&cli->cl_lru_shrinkers) > 1) {
560                         atomic_dec(&cli->cl_lru_shrinkers);
561                         return -EBUSY;
562                 }
563         } else {
564                 atomic_inc(&cli->cl_lru_shrinkers);
565         }
566
567         pvec = (struct cl_page **)osc_env_info(env)->oti_pvec;
568         io = &osc_env_info(env)->oti_io;
569
570         spin_lock(&cli->cl_lru_list_lock);
571         maxscan = min(target << 1, atomic_long_read(&cli->cl_lru_in_list));
572         list_for_each_entry_safe(opg, temp, &cli->cl_lru_list, ops_lru) {
573                 struct cl_page *page;
574                 bool will_free = false;
575
576                 if (--maxscan < 0)
577                         break;
578
579                 page = opg->ops_cl.cpl_page;
580                 if (lru_page_busy(cli, page)) {
581                         list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
582                         continue;
583                 }
584
585                 LASSERT(page->cp_obj);
586                 if (clobj != page->cp_obj) {
587                         struct cl_object *tmp = page->cp_obj;
588
589                         cl_object_get(tmp);
590                         spin_unlock(&cli->cl_lru_list_lock);
591
592                         if (clobj) {
593                                 discard_pagevec(env, io, pvec, index);
594                                 index = 0;
595
596                                 cl_io_fini(env, io);
597                                 cl_object_put(env, clobj);
598                                 clobj = NULL;
599                         }
600
601                         clobj = tmp;
602                         io->ci_obj = clobj;
603                         io->ci_ignore_layout = 1;
604                         rc = cl_io_init(env, io, CIT_MISC, clobj);
605
606                         spin_lock(&cli->cl_lru_list_lock);
607
608                         if (rc != 0)
609                                 break;
610
611                         ++maxscan;
612                         continue;
613                 }
614
615                 if (cl_page_own_try(env, io, page) == 0) {
616                         if (!lru_page_busy(cli, page)) {
617                                 /* remove it from lru list earlier to avoid
618                                  * lock contention
619                                  */
620                                 __osc_lru_del(cli, opg);
621                                 opg->ops_in_lru = 0; /* will be discarded */
622
623                                 cl_page_get(page);
624                                 will_free = true;
625                         } else {
626                                 cl_page_disown(env, io, page);
627                         }
628                 }
629
630                 if (!will_free) {
631                         list_move_tail(&opg->ops_lru, &cli->cl_lru_list);
632                         continue;
633                 }
634
635                 /* Don't discard and free the page with cl_lru_list held */
636                 pvec[index++] = page;
637                 if (unlikely(index == OTI_PVEC_SIZE)) {
638                         spin_unlock(&cli->cl_lru_list_lock);
639                         discard_pagevec(env, io, pvec, index);
640                         index = 0;
641
642                         spin_lock(&cli->cl_lru_list_lock);
643                 }
644
645                 if (++count >= target)
646                         break;
647         }
648         spin_unlock(&cli->cl_lru_list_lock);
649
650         if (clobj) {
651                 discard_pagevec(env, io, pvec, index);
652
653                 cl_io_fini(env, io);
654                 cl_object_put(env, clobj);
655         }
656
657         atomic_dec(&cli->cl_lru_shrinkers);
658         if (count > 0) {
659                 atomic_long_add(count, cli->cl_lru_left);
660                 wake_up_all(&osc_lru_waitq);
661         }
662         return count > 0 ? count : rc;
663 }
664
665 long osc_lru_reclaim(struct client_obd *cli)
666 {
667         struct cl_env_nest nest;
668         struct lu_env *env;
669         struct cl_client_cache *cache = cli->cl_cache;
670         int max_scans;
671         long rc = 0;
672
673         LASSERT(cache);
674
675         env = cl_env_nested_get(&nest);
676         if (IS_ERR(env))
677                 return 0;
678
679         rc = osc_lru_shrink(env, cli, osc_cache_too_much(cli), false);
680         if (rc != 0) {
681                 if (rc == -EBUSY)
682                         rc = 0;
683
684                 CDEBUG(D_CACHE, "%s: Free %ld pages from own LRU: %p.\n",
685                        cli->cl_import->imp_obd->obd_name, rc, cli);
686                 goto out;
687         }
688
689         CDEBUG(D_CACHE, "%s: cli %p no free slots, pages: %ld, busy: %ld.\n",
690                cli->cl_import->imp_obd->obd_name, cli,
691                atomic_long_read(&cli->cl_lru_in_list),
692                atomic_long_read(&cli->cl_lru_busy));
693
694         /* Reclaim LRU slots from other client_obd as it can't free enough
695          * from its own. This should rarely happen.
696          */
697         spin_lock(&cache->ccc_lru_lock);
698         LASSERT(!list_empty(&cache->ccc_lru));
699
700         cache->ccc_lru_shrinkers++;
701         list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
702
703         max_scans = atomic_read(&cache->ccc_users) - 2;
704         while (--max_scans > 0 && !list_empty(&cache->ccc_lru)) {
705                 cli = list_entry(cache->ccc_lru.next, struct client_obd,
706                                  cl_lru_osc);
707
708                 CDEBUG(D_CACHE, "%s: cli %p LRU pages: %ld, busy: %ld.\n",
709                        cli->cl_import->imp_obd->obd_name, cli,
710                        atomic_long_read(&cli->cl_lru_in_list),
711                        atomic_long_read(&cli->cl_lru_busy));
712
713                 list_move_tail(&cli->cl_lru_osc, &cache->ccc_lru);
714                 if (osc_cache_too_much(cli) > 0) {
715                         spin_unlock(&cache->ccc_lru_lock);
716
717                         rc = osc_lru_shrink(env, cli, osc_cache_too_much(cli),
718                                             true);
719                         spin_lock(&cache->ccc_lru_lock);
720                         if (rc != 0)
721                                 break;
722                 }
723         }
724         spin_unlock(&cache->ccc_lru_lock);
725
726 out:
727         cl_env_nested_put(&nest, env);
728         CDEBUG(D_CACHE, "%s: cli %p freed %ld pages.\n",
729                cli->cl_import->imp_obd->obd_name, cli, rc);
730         return rc;
731 }
732
733 /**
734  * osc_lru_reserve() is called to reserve an LRU slot for a cl_page.
735  *
736  * Usually the LRU slots are reserved in osc_io_iter_rw_init().
737  * Only in the case that the LRU slots are in extreme shortage, it should
738  * have reserved enough slots for an IO.
739  */
740 static int osc_lru_reserve(const struct lu_env *env, struct osc_object *obj,
741                            struct osc_page *opg)
742 {
743         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
744         struct osc_io *oio = osc_env_io(env);
745         struct client_obd *cli = osc_cli(obj);
746         int rc = 0;
747
748         if (!cli->cl_cache) /* shall not be in LRU */
749                 return 0;
750
751         if (oio->oi_lru_reserved > 0) {
752                 --oio->oi_lru_reserved;
753                 goto out;
754         }
755
756         LASSERT(atomic_long_read(cli->cl_lru_left) >= 0);
757         while (!atomic_long_add_unless(cli->cl_lru_left, -1, 0)) {
758                 /* run out of LRU spaces, try to drop some by itself */
759                 rc = osc_lru_reclaim(cli);
760                 if (rc < 0)
761                         break;
762                 if (rc > 0)
763                         continue;
764
765                 cond_resched();
766
767                 rc = l_wait_event(osc_lru_waitq,
768                                   atomic_long_read(cli->cl_lru_left) > 0,
769                                   &lwi);
770
771                 if (rc < 0)
772                         break;
773         }
774
775 out:
776         if (rc >= 0) {
777                 atomic_long_inc(&cli->cl_lru_busy);
778                 opg->ops_in_lru = 1;
779                 rc = 0;
780         }
781
782         return rc;
783 }
784
785 /**
786  * Atomic operations are expensive. We accumulate the accounting for the
787  * same page pgdat to get better performance.
788  * In practice this can work pretty good because the pages in the same RPC
789  * are likely from the same page zone.
790  */
791 static inline void unstable_page_accounting(struct ptlrpc_bulk_desc *desc,
792                                             int factor)
793 {
794         int page_count = desc->bd_iov_count;
795         pg_data_t *last = NULL;
796         int count = 0;
797         int i;
798
799         for (i = 0; i < page_count; i++) {
800                 pg_data_t *pgdat = page_pgdat(desc->bd_iov[i].bv_page);
801
802                 if (likely(pgdat == last)) {
803                         ++count;
804                         continue;
805                 }
806
807                 if (count > 0) {
808                         mod_node_page_state(pgdat, NR_UNSTABLE_NFS,
809                                             factor * count);
810                         count = 0;
811                 }
812                 last = pgdat;
813                 ++count;
814         }
815         if (count > 0)
816                 mod_node_page_state(last, NR_UNSTABLE_NFS, factor * count);
817 }
818
819 static inline void add_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
820 {
821         unstable_page_accounting(desc, 1);
822 }
823
824 static inline void dec_unstable_page_accounting(struct ptlrpc_bulk_desc *desc)
825 {
826         unstable_page_accounting(desc, -1);
827 }
828
829 /**
830  * Performs "unstable" page accounting. This function balances the
831  * increment operations performed in osc_inc_unstable_pages. It is
832  * registered as the RPC request callback, and is executed when the
833  * bulk RPC is committed on the server. Thus at this point, the pages
834  * involved in the bulk transfer are no longer considered unstable.
835  *
836  * If this function is called, the request should have been committed
837  * or req:rq_unstable must have been set; it implies that the unstable
838  * statistic have been added.
839  */
840 void osc_dec_unstable_pages(struct ptlrpc_request *req)
841 {
842         struct client_obd *cli = &req->rq_import->imp_obd->u.cli;
843         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
844         int page_count = desc->bd_iov_count;
845         long unstable_count;
846
847         LASSERT(page_count >= 0);
848         dec_unstable_page_accounting(desc);
849
850         unstable_count = atomic_long_sub_return(page_count,
851                                                 &cli->cl_unstable_count);
852         LASSERT(unstable_count >= 0);
853
854         unstable_count = atomic_long_sub_return(page_count,
855                                                 &cli->cl_cache->ccc_unstable_nr);
856         LASSERT(unstable_count >= 0);
857         if (!unstable_count)
858                 wake_up_all(&cli->cl_cache->ccc_unstable_waitq);
859
860         if (osc_cache_too_much(cli))
861                 (void)ptlrpcd_queue_work(cli->cl_lru_work);
862 }
863
864 /**
865  * "unstable" page accounting. See: osc_dec_unstable_pages.
866  */
867 void osc_inc_unstable_pages(struct ptlrpc_request *req)
868 {
869         struct client_obd *cli  = &req->rq_import->imp_obd->u.cli;
870         struct ptlrpc_bulk_desc *desc = req->rq_bulk;
871         long page_count = desc->bd_iov_count;
872
873         /* No unstable page tracking */
874         if (!cli->cl_cache || !cli->cl_cache->ccc_unstable_check)
875                 return;
876
877         add_unstable_page_accounting(desc);
878         atomic_long_add(page_count, &cli->cl_unstable_count);
879         atomic_long_add(page_count, &cli->cl_cache->ccc_unstable_nr);
880
881         /*
882          * If the request has already been committed (i.e. brw_commit
883          * called via rq_commit_cb), we need to undo the unstable page
884          * increments we just performed because rq_commit_cb wont be
885          * called again.
886          */
887         spin_lock(&req->rq_lock);
888         if (unlikely(req->rq_committed)) {
889                 spin_unlock(&req->rq_lock);
890
891                 osc_dec_unstable_pages(req);
892         } else {
893                 req->rq_unstable = 1;
894                 spin_unlock(&req->rq_lock);
895         }
896 }
897
898 /**
899  * Check if it piggybacks SOFT_SYNC flag to OST from this OSC.
900  * This function will be called by every BRW RPC so it's critical
901  * to make this function fast.
902  */
903 bool osc_over_unstable_soft_limit(struct client_obd *cli)
904 {
905         long unstable_nr, osc_unstable_count;
906
907         /* Can't check cli->cl_unstable_count, therefore, no soft limit */
908         if (!cli->cl_cache || !cli->cl_cache->ccc_unstable_check)
909                 return false;
910
911         osc_unstable_count = atomic_long_read(&cli->cl_unstable_count);
912         unstable_nr = atomic_long_read(&cli->cl_cache->ccc_unstable_nr);
913
914         CDEBUG(D_CACHE,
915                "%s: cli: %p unstable pages: %lu, osc unstable pages: %lu\n",
916                cli->cl_import->imp_obd->obd_name, cli,
917                unstable_nr, osc_unstable_count);
918
919         /*
920          * If the LRU slots are in shortage - 25% remaining AND this OSC
921          * has one full RPC window of unstable pages, it's a good chance
922          * to piggyback a SOFT_SYNC flag.
923          * Please notice that the OST won't take immediate response for the
924          * SOFT_SYNC request so active OSCs will have more chance to carry
925          * the flag, this is reasonable.
926          */
927         return unstable_nr > cli->cl_cache->ccc_lru_max >> 2 &&
928                osc_unstable_count > cli->cl_max_pages_per_rpc *
929                                     cli->cl_max_rpcs_in_flight;
930 }
931
932 /** @} osc */