Whamcloud - gitweb
b93ed5f5471b60945546ffbada65b90dd77151a1
[fs/lustre-release.git] / lustre / llite / rw.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) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/llite/rw.c
32  *
33  * Lustre Lite I/O page cache routines shared by different kernel revs
34  */
35
36 #include <linux/kernel.h>
37 #include <linux/mm.h>
38 #include <linux/string.h>
39 #include <linux/stat.h>
40 #include <linux/errno.h>
41 #include <linux/unistd.h>
42 #include <linux/writeback.h>
43 #include <asm/uaccess.h>
44
45 #include <linux/fs.h>
46 #include <linux/file.h>
47 #include <linux/stat.h>
48 #include <asm/uaccess.h>
49 #include <linux/mm.h>
50 #include <linux/pagemap.h>
51 /* current_is_kswapd() */
52 #include <linux/swap.h>
53 #include <linux/task_io_accounting_ops.h>
54
55 #define DEBUG_SUBSYSTEM S_LLITE
56
57 #include <obd_cksum.h>
58 #include "llite_internal.h"
59 #include <lustre_compat.h>
60
61 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
62
63 /**
64  * Get readahead pages from the filesystem readahead pool of the client for a
65  * thread.
66  *
67  * /param sbi superblock for filesystem readahead state ll_ra_info
68  * /param ria per-thread readahead state
69  * /param pages number of pages requested for readahead for the thread.
70  *
71  * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
72  * It should work well if the ra_max_pages is much greater than the single
73  * file's read-ahead window, and not too many threads contending for
74  * these readahead pages.
75  *
76  * TODO: There may be a 'global sync problem' if many threads are trying
77  * to get an ra budget that is larger than the remaining readahead pages
78  * and reach here at exactly the same time. They will compute /a ret to
79  * consume the remaining pages, but will fail at atomic_add_return() and
80  * get a zero ra window, although there is still ra space remaining. - Jay */
81
82 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
83                                      struct ra_io_arg *ria,
84                                      unsigned long pages,
85                                      unsigned long pages_min)
86 {
87         struct ll_ra_info *ra = &sbi->ll_ra_info;
88         long ret;
89
90         ENTRY;
91
92         WARN_ON_ONCE(pages_min > pages);
93         /**
94          * Don't try readahead aggresively if we are limited
95          * LRU pages, otherwise, it could cause deadlock.
96          */
97         pages = min(sbi->ll_cache->ccc_lru_max >> 2, pages);
98         /**
99          * if this happen, we reserve more pages than needed,
100          * this will make us leak @ra_cur_pages, because
101          * ll_ra_count_put() acutally freed @pages.
102          */
103         if (unlikely(pages_min > pages))
104                 pages_min = pages;
105
106         /*
107          * If read-ahead pages left are less than 1M, do not do read-ahead,
108          * otherwise it will form small read RPC(< 1M), which hurt server
109          * performance a lot.
110          */
111         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
112                   pages);
113         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
114                 GOTO(out, ret = 0);
115
116         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
117                 atomic_sub(ret, &ra->ra_cur_pages);
118                 ret = 0;
119         }
120
121 out:
122         if (ret < pages_min) {
123                 /* override ra limit for maximum performance */
124                 atomic_add(pages_min - ret, &ra->ra_cur_pages);
125                 ret = pages_min;
126         }
127         RETURN(ret);
128 }
129
130 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long pages)
131 {
132         struct ll_ra_info *ra = &sbi->ll_ra_info;
133         atomic_sub(pages, &ra->ra_cur_pages);
134 }
135
136 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
137 {
138         LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
139         lprocfs_counter_incr(sbi->ll_ra_stats, which);
140 }
141
142 static inline bool ll_readahead_enabled(struct ll_sb_info *sbi)
143 {
144         return sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
145                 sbi->ll_ra_info.ra_max_pages > 0;
146 }
147
148 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
149 {
150         struct ll_sb_info *sbi = ll_i2sbi(inode);
151         ll_ra_stats_inc_sbi(sbi, which);
152 }
153
154 #define RAS_CDEBUG(ras) \
155         CDEBUG(D_READA,                                                      \
156                "lre %llu cr %lu cb %llu wsi %lu wp %lu nra %lu rpc %lu "     \
157                "r %lu csr %lu so %llu sb %llu sl %llu lr %lu\n",             \
158                ras->ras_last_read_end_bytes, ras->ras_consecutive_requests,  \
159                ras->ras_consecutive_bytes, ras->ras_window_start_idx,        \
160                ras->ras_window_pages, ras->ras_next_readahead_idx,           \
161                ras->ras_rpc_pages, ras->ras_requests,                        \
162                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
163                ras->ras_stride_bytes, ras->ras_stride_length,                \
164                ras->ras_async_last_readpage_idx)
165
166 static bool pos_in_window(loff_t pos, loff_t point,
167                           unsigned long before, unsigned long after)
168 {
169         loff_t start = point - before;
170         loff_t end = point + after;
171
172         if (start > point)
173                 start = 0;
174         if (end < point)
175                 end = ~0;
176
177         return start <= pos && pos <= end;
178 }
179
180 enum ll_ra_page_hint {
181         MAYNEED = 0, /* this page possibly accessed soon */
182         WILLNEED /* this page is gurateed to be needed */
183 };
184
185 /**
186  * Initiates read-ahead of a page with given index.
187  *
188  * \retval +ve: page was already uptodate so it will be skipped
189  *              from being added;
190  * \retval -ve: page wasn't added to \a queue for error;
191  * \retval   0: page was added into \a queue for read ahead.
192  */
193 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
194                               struct cl_page_list *queue, pgoff_t index,
195                               enum ll_ra_page_hint hint)
196 {
197         struct cl_object *clob  = io->ci_obj;
198         struct inode     *inode = vvp_object_inode(clob);
199         struct page      *vmpage = NULL;
200         struct cl_page   *cp;
201         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
202         int               rc    = 0;
203         const char       *msg   = NULL;
204
205         ENTRY;
206
207         switch (hint) {
208         case MAYNEED:
209                 vmpage = grab_cache_page_nowait(inode->i_mapping, index);
210                 if (vmpage == NULL) {
211                         which = RA_STAT_FAILED_GRAB_PAGE;
212                         msg   = "g_c_p_n failed";
213                         GOTO(out, rc = -EBUSY);
214                 }
215                 break;
216         case WILLNEED:
217                 vmpage = find_or_create_page(inode->i_mapping, index,
218                                              GFP_NOFS);
219                 if (vmpage == NULL)
220                         GOTO(out, rc = -ENOMEM);
221                 break;
222         default:
223                 /* should not come here */
224                 GOTO(out, rc = -EINVAL);
225         }
226
227         /* Check if vmpage was truncated or reclaimed */
228         if (vmpage->mapping != inode->i_mapping) {
229                 which = RA_STAT_WRONG_GRAB_PAGE;
230                 msg   = "g_c_p_n returned invalid page";
231                 GOTO(out, rc = -EBUSY);
232         }
233
234         cp = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
235         if (IS_ERR(cp)) {
236                 which = RA_STAT_FAILED_GRAB_PAGE;
237                 msg   = "cl_page_find failed";
238                 GOTO(out, rc = PTR_ERR(cp));
239         }
240
241         lu_ref_add(&cp->cp_reference, "ra", current);
242         cl_page_assume(env, io, cp);
243
244         if (!cp->cp_defer_uptodate && !PageUptodate(vmpage)) {
245                 if (hint == MAYNEED) {
246                         cp->cp_defer_uptodate = 1;
247                         cp->cp_ra_used = 0;
248                 }
249
250                 cl_page_list_add(queue, cp, true);
251         } else {
252                 /* skip completed pages */
253                 cl_page_unassume(env, io, cp);
254                 /* This page is already uptodate, returning a positive number
255                  * to tell the callers about this */
256                 rc = 1;
257         }
258
259         lu_ref_del(&cp->cp_reference, "ra", current);
260         cl_page_put(env, cp);
261
262 out:
263         if (vmpage != NULL) {
264                 if (rc != 0)
265                         unlock_page(vmpage);
266                 put_page(vmpage);
267         }
268         if (msg != NULL && hint == MAYNEED) {
269                 ll_ra_stats_inc(inode, which);
270                 CDEBUG(D_READA, "%s\n", msg);
271
272         }
273
274         RETURN(rc);
275 }
276
277 #define RIA_DEBUG(ria)                                                  \
278         CDEBUG(D_READA, "rs %lu re %lu ro %llu rl %llu rb %llu\n",      \
279                ria->ria_start_idx, ria->ria_end_idx, ria->ria_stoff,    \
280                ria->ria_length, ria->ria_bytes)
281
282 static inline int stride_io_mode(struct ll_readahead_state *ras)
283 {
284         return ras->ras_consecutive_stride_requests > 1;
285 }
286
287 /* The function calculates how many bytes will be read in
288  * [off, off + length], in such stride IO area,
289  * stride_offset = st_off, stride_lengh = st_len,
290  * stride_bytes = st_bytes
291  *
292  *   |------------------|*****|------------------|*****|------------|*****|....
293  * st_off
294  *   |--- st_bytes     ---|
295  *   |-----     st_len   -----|
296  *
297  *              How many bytes it should read in such pattern
298  *              |-------------------------------------------------------------|
299  *              off
300  *              |<------                  length                      ------->|
301  *
302  *          =   |<----->|  +  |-------------------------------------| +   |---|
303  *             start_left                 st_bytes * i                 end_left
304  */
305 static loff_t stride_byte_count(loff_t st_off, loff_t st_len, loff_t st_bytes,
306                                 loff_t off, loff_t length)
307 {
308         u64 start = off > st_off ? off - st_off : 0;
309         u64 end = off + length > st_off ? off + length - st_off : 0;
310         u64 start_left;
311         u64 end_left;
312         u64 bytes_count;
313
314         if (st_len == 0 || length == 0 || end == 0)
315                 return length;
316
317         start = div64_u64_rem(start, st_len, &start_left);
318         if (start_left < st_bytes)
319                 start_left = st_bytes - start_left;
320         else
321                 start_left = 0;
322
323         end = div64_u64_rem(end, st_len, &end_left);
324         if (end_left > st_bytes)
325                 end_left = st_bytes;
326
327         CDEBUG(D_READA, "start %llu, end %llu start_left %llu end_left %llu\n",
328                start, end, start_left, end_left);
329
330         if (start == end)
331                 bytes_count = end_left - (st_bytes - start_left);
332         else
333                 bytes_count = start_left +
334                         st_bytes * (end - start - 1) + end_left;
335
336         CDEBUG(D_READA,
337                "st_off %llu, st_len %llu st_bytes %llu off %llu length %llu bytescount %llu\n",
338                st_off, st_len, st_bytes, off, length, bytes_count);
339
340         return bytes_count;
341 }
342
343 static unsigned long ria_page_count(struct ra_io_arg *ria)
344 {
345         loff_t length_bytes = ria->ria_end_idx >= ria->ria_start_idx ?
346                 (loff_t)(ria->ria_end_idx -
347                          ria->ria_start_idx + 1) << PAGE_SHIFT : 0;
348         loff_t bytes_count;
349
350         if (ria->ria_length > ria->ria_bytes && ria->ria_bytes &&
351             (ria->ria_length & ~PAGE_MASK || ria->ria_bytes & ~PAGE_MASK ||
352              ria->ria_stoff & ~PAGE_MASK)) {
353                 /* Over-estimate un-aligned page stride read */
354                 unsigned long pg_count = ((ria->ria_bytes +
355                                            PAGE_SIZE - 1) >> PAGE_SHIFT) + 1;
356                 pg_count *= length_bytes / ria->ria_length + 1;
357
358                 return pg_count;
359         }
360         bytes_count = stride_byte_count(ria->ria_stoff, ria->ria_length,
361                                         ria->ria_bytes,
362                                         (loff_t)ria->ria_start_idx<<PAGE_SHIFT,
363                                         length_bytes);
364         return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
365 }
366
367 static pgoff_t ras_align(struct ll_readahead_state *ras, pgoff_t index)
368 {
369         unsigned opt_size = min(ras->ras_window_pages, ras->ras_rpc_pages);
370
371         if (opt_size == 0)
372                 opt_size = 1;
373         return index - (index % opt_size);
374 }
375
376 /* Check whether the index is in the defined ra-window */
377 static bool ras_inside_ra_window(pgoff_t idx, struct ra_io_arg *ria)
378 {
379         loff_t pos = (loff_t)idx << PAGE_SHIFT;
380
381         /* If ria_length == ria_bytes, it means non-stride I/O mode,
382          * idx should always inside read-ahead window in this case
383          * For stride I/O mode, just check whether the idx is inside
384          * the ria_bytes.
385          */
386         if (ria->ria_length == 0 || ria->ria_length == ria->ria_bytes)
387                 return true;
388
389         if (pos >= ria->ria_stoff) {
390                 u64 offset;
391
392                 div64_u64_rem(pos - ria->ria_stoff, ria->ria_length, &offset);
393
394                 if (offset < ria->ria_bytes ||
395                     (ria->ria_length - offset) < PAGE_SIZE)
396                         return true;
397         } else if (pos + PAGE_SIZE > ria->ria_stoff) {
398                 return true;
399         }
400
401         return false;
402 }
403
404 static unsigned long
405 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
406                     struct cl_page_list *queue, struct ll_readahead_state *ras,
407                     struct ra_io_arg *ria, pgoff_t *ra_end, pgoff_t skip_index)
408 {
409         struct cl_read_ahead ra = { 0 };
410         /* busy page count is per stride */
411         int rc = 0, count = 0, busy_page_count = 0;
412         pgoff_t page_idx;
413
414         LASSERT(ria != NULL);
415         RIA_DEBUG(ria);
416
417         for (page_idx = ria->ria_start_idx;
418              page_idx <= ria->ria_end_idx && ria->ria_reserved > 0;
419              page_idx++) {
420                 if (skip_index && page_idx == skip_index)
421                         continue;
422                 if (ras_inside_ra_window(page_idx, ria)) {
423                         if (ra.cra_end_idx == 0 || ra.cra_end_idx < page_idx) {
424                                 pgoff_t end_idx;
425
426                                 /*
427                                  * Do not shrink ria_end_idx at any case until
428                                  * the minimum end of current read is covered.
429                                  *
430                                  * Do not extend read lock accross stripe if
431                                  * lock contention detected.
432                                  */
433                                 if (ra.cra_contention &&
434                                     page_idx > ria->ria_end_idx_min) {
435                                         ria->ria_end_idx = *ra_end;
436                                         break;
437                                 }
438
439                                 cl_read_ahead_release(env, &ra);
440
441                                 rc = cl_io_read_ahead(env, io, page_idx, &ra);
442                                 if (rc < 0)
443                                         break;
444
445                                  /*
446                                   * Only shrink ria_end_idx if the matched
447                                   * LDLM lock doesn't cover more.
448                                   */
449                                 if (page_idx > ra.cra_end_idx) {
450                                         ria->ria_end_idx = ra.cra_end_idx;
451                                         break;
452                                 }
453
454                                 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
455                                        page_idx, ra.cra_end_idx,
456                                        ra.cra_rpc_pages);
457                                 LASSERTF(ra.cra_end_idx >= page_idx,
458                                          "object: %p, indcies %lu / %lu\n",
459                                          io->ci_obj, ra.cra_end_idx, page_idx);
460                                 /* update read ahead RPC size.
461                                  * NB: it's racy but doesn't matter */
462                                 if (ras->ras_rpc_pages != ra.cra_rpc_pages &&
463                                     ra.cra_rpc_pages > 0)
464                                         ras->ras_rpc_pages = ra.cra_rpc_pages;
465                                 if (!skip_index) {
466                                         /* trim it to align with optimal RPC size */
467                                         end_idx = ras_align(ras, ria->ria_end_idx + 1);
468                                         if (end_idx > 0 && !ria->ria_eof)
469                                                 ria->ria_end_idx = end_idx - 1;
470                                 }
471                                 if (ria->ria_end_idx < ria->ria_end_idx_min)
472                                         ria->ria_end_idx = ria->ria_end_idx_min;
473                         }
474                         if (page_idx > ria->ria_end_idx)
475                                 break;
476
477                         /* If the page is inside the read-ahead window */
478                         rc = ll_read_ahead_page(env, io, queue, page_idx,
479                                                 MAYNEED);
480                         if (rc < 0 && rc != -EBUSY)
481                                 break;
482                         if (rc == -EBUSY) {
483                                 busy_page_count++;
484                                 CDEBUG(D_READA,
485                                        "skip busy page: %lu\n", page_idx);
486                                 /* For page unaligned readahead the first
487                                  * last pages of each region can be read by
488                                  * another reader on the same node, and so
489                                  * may be busy. So only stop for > 2 busy
490                                  * pages. */
491                                 if (busy_page_count > 2)
492                                         break;
493                         }
494
495                         *ra_end = page_idx;
496                         /* Only subtract from reserve & count the page if we
497                          * really did readahead on that page. */
498                         if (rc == 0) {
499                                 ria->ria_reserved--;
500                                 count++;
501                         }
502                 } else if (stride_io_mode(ras)) {
503                         /* If it is not in the read-ahead window, and it is
504                          * read-ahead mode, then check whether it should skip
505                          * the stride gap.
506                          */
507                         loff_t pos = (loff_t)page_idx << PAGE_SHIFT;
508                         u64 offset;
509
510                         div64_u64_rem(pos - ria->ria_stoff, ria->ria_length,
511                                       &offset);
512                         if (offset >= ria->ria_bytes) {
513                                 pos += (ria->ria_length - offset);
514                                 if ((pos >> PAGE_SHIFT) >= page_idx + 1)
515                                         page_idx = (pos >> PAGE_SHIFT) - 1;
516                                 busy_page_count = 0;
517                                 CDEBUG(D_READA,
518                                        "Stride: jump %llu pages to %lu\n",
519                                        ria->ria_length - offset, page_idx);
520                                 continue;
521                         }
522                 }
523         }
524
525         cl_read_ahead_release(env, &ra);
526
527         return count;
528 }
529
530 static void ll_readahead_work_free(struct ll_readahead_work *work)
531 {
532         fput(work->lrw_file);
533         OBD_FREE_PTR(work);
534 }
535
536 static void ll_readahead_handle_work(struct work_struct *wq);
537 static void ll_readahead_work_add(struct inode *inode,
538                                   struct ll_readahead_work *work)
539 {
540         INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
541         queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
542                    &work->lrw_readahead_work);
543 }
544
545 static int ll_readahead_file_kms(const struct lu_env *env,
546                                 struct cl_io *io, __u64 *kms)
547 {
548         struct cl_object *clob;
549         struct inode *inode;
550         struct cl_attr *attr = vvp_env_thread_attr(env);
551         int ret;
552
553         clob = io->ci_obj;
554         inode = vvp_object_inode(clob);
555
556         cl_object_attr_lock(clob);
557         ret = cl_object_attr_get(env, clob, attr);
558         cl_object_attr_unlock(clob);
559
560         if (ret != 0)
561                 RETURN(ret);
562
563         *kms = attr->cat_kms;
564         return 0;
565 }
566
567 static void ll_readahead_handle_work(struct work_struct *wq)
568 {
569         struct ll_readahead_work *work;
570         struct lu_env *env;
571         __u16 refcheck;
572         struct ra_io_arg *ria;
573         struct inode *inode;
574         struct ll_file_data *fd;
575         struct ll_readahead_state *ras;
576         struct cl_io *io;
577         struct cl_2queue *queue;
578         pgoff_t ra_end_idx = 0;
579         unsigned long pages, pages_min = 0;
580         struct file *file;
581         __u64 kms;
582         int rc;
583         pgoff_t eof_index;
584         struct ll_sb_info *sbi;
585
586         work = container_of(wq, struct ll_readahead_work,
587                             lrw_readahead_work);
588         fd = work->lrw_file->private_data;
589         ras = &fd->fd_ras;
590         file = work->lrw_file;
591         inode = file_inode(file);
592         sbi = ll_i2sbi(inode);
593
594         CDEBUG(D_READA|D_IOTRACE,
595                "%s:"DFID": async ra from %lu to %lu triggered by user pid %d\n",
596                file_dentry(file)->d_name.name, PFID(ll_inode2fid(inode)),
597                work->lrw_start_idx, work->lrw_end_idx, work->lrw_user_pid);
598
599         env = cl_env_alloc(&refcheck, LCT_NOREF);
600         if (IS_ERR(env))
601                 GOTO(out_free_work, rc = PTR_ERR(env));
602
603         io = vvp_env_thread_io(env);
604         ll_io_init(io, file, CIT_READ, NULL);
605
606         rc = ll_readahead_file_kms(env, io, &kms);
607         if (rc != 0)
608                 GOTO(out_put_env, rc);
609
610         if (kms == 0) {
611                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
612                 GOTO(out_put_env, rc = 0);
613         }
614
615         ria = &ll_env_info(env)->lti_ria;
616         memset(ria, 0, sizeof(*ria));
617
618         ria->ria_start_idx = work->lrw_start_idx;
619         /* Truncate RA window to end of file */
620         eof_index = (pgoff_t)(kms - 1) >> PAGE_SHIFT;
621         if (eof_index <= work->lrw_end_idx) {
622                 work->lrw_end_idx = eof_index;
623                 ria->ria_eof = true;
624         }
625         if (work->lrw_end_idx <= work->lrw_start_idx)
626                 GOTO(out_put_env, rc = 0);
627
628         ria->ria_end_idx = work->lrw_end_idx;
629         pages = ria->ria_end_idx - ria->ria_start_idx + 1;
630         ria->ria_reserved = ll_ra_count_get(sbi, ria,
631                                             ria_page_count(ria), pages_min);
632
633         CDEBUG(D_READA,
634                "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
635                ria->ria_reserved, pages, pages_min,
636                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
637                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
638
639         if (ria->ria_reserved < pages) {
640                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
641                 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
642                         ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
643                         GOTO(out_put_env, rc = 0);
644                 }
645         }
646
647         rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start_idx, pages);
648         if (rc)
649                 GOTO(out_put_env, rc);
650
651         /* overwrite jobid inited in vvp_io_init() */
652         if (strncmp(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
653                     sizeof(work->lrw_jobid)))
654                 memcpy(ll_i2info(inode)->lli_jobid, work->lrw_jobid,
655                        sizeof(work->lrw_jobid));
656
657         vvp_env_io(env)->vui_fd = fd;
658         io->ci_state = CIS_LOCKED;
659         io->ci_async_readahead = true;
660         rc = cl_io_start(env, io);
661         if (rc)
662                 GOTO(out_io_fini, rc);
663
664         queue = &io->ci_queue;
665         cl_2queue_init(queue);
666
667         rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria,
668                                  &ra_end_idx, 0);
669         if (ria->ria_reserved != 0)
670                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
671         if (queue->c2_qin.pl_nr > 0) {
672                 int count = queue->c2_qin.pl_nr;
673
674                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
675                 if (rc == 0)
676                         task_io_account_read(PAGE_SIZE * count);
677         }
678         if (ria->ria_end_idx == ra_end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
679                 ll_ra_stats_inc(inode, RA_STAT_EOF);
680
681         if (ra_end_idx != ria->ria_end_idx)
682                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
683
684         /* TODO: discard all pages until page reinit route is implemented */
685         cl_page_list_discard(env, io, &queue->c2_qin);
686
687         /* Unlock unsent read pages in case of error. */
688         cl_page_list_disown(env, &queue->c2_qin);
689
690         cl_2queue_fini(env, queue);
691 out_io_fini:
692         cl_io_end(env, io);
693         cl_io_fini(env, io);
694 out_put_env:
695         cl_env_put(env, &refcheck);
696 out_free_work:
697         if (ra_end_idx > 0)
698                 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
699         atomic_dec(&sbi->ll_ra_info.ra_async_inflight);
700         ll_readahead_work_free(work);
701 }
702
703 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
704                         struct cl_page_list *queue,
705                         struct ll_readahead_state *ras, bool hit,
706                         struct file *file, pgoff_t skip_index,
707                         pgoff_t *start_idx)
708 {
709         struct vvp_io *vio = vvp_env_io(env);
710         struct ll_thread_info *lti = ll_env_info(env);
711         unsigned long pages, pages_min = 0;
712         pgoff_t ra_end_idx = 0, end_idx = 0;
713         struct inode *inode;
714         struct ra_io_arg *ria = &lti->lti_ria;
715         struct cl_object *clob;
716         int ret = 0;
717         __u64 kms;
718         struct ll_sb_info *sbi;
719         struct ll_ra_info *ra;
720
721         ENTRY;
722
723         clob = io->ci_obj;
724         inode = vvp_object_inode(clob);
725         sbi = ll_i2sbi(inode);
726         ra = &sbi->ll_ra_info;
727
728         /**
729          * In case we have a limited max_cached_mb, readahead
730          * should be stopped if it have run out of all LRU slots.
731          */
732         if (atomic_read(&ra->ra_cur_pages) >= sbi->ll_cache->ccc_lru_max) {
733                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
734                 RETURN(0);
735         }
736
737         memset(ria, 0, sizeof(*ria));
738         ret = ll_readahead_file_kms(env, io, &kms);
739         if (ret != 0)
740                 RETURN(ret);
741
742         if (kms == 0) {
743                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
744                 RETURN(0);
745         }
746
747         spin_lock(&ras->ras_lock);
748
749         /**
750          * Note: other thread might rollback the ras_next_readahead_idx,
751          * if it can not get the full size of prepared pages, see the
752          * end of this function. For stride read ahead, it needs to
753          * make sure the offset is no less than ras_stride_offset,
754          * so that stride read ahead can work correctly.
755          */
756         if (stride_io_mode(ras))
757                 *start_idx = max_t(pgoff_t, ras->ras_next_readahead_idx,
758                                   ras->ras_stride_offset >> PAGE_SHIFT);
759         else
760                 *start_idx = ras->ras_next_readahead_idx;
761
762         if (ras->ras_window_pages > 0)
763                 end_idx = ras->ras_window_start_idx + ras->ras_window_pages - 1;
764
765         if (skip_index)
766                 end_idx = *start_idx + ras->ras_window_pages - 1;
767
768         /* Enlarge the RA window to encompass the full read */
769         if (vio->vui_ra_valid &&
770             end_idx < vio->vui_ra_start_idx + vio->vui_ra_pages - 1)
771                 end_idx = vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
772
773         if (end_idx != 0) {
774                 pgoff_t eof_index;
775
776                 /* Truncate RA window to end of file */
777                 eof_index = (pgoff_t)((kms - 1) >> PAGE_SHIFT);
778                 if (eof_index <= end_idx) {
779                         end_idx = eof_index;
780                         ria->ria_eof = true;
781                 }
782         }
783         ria->ria_start_idx = *start_idx;
784         ria->ria_end_idx = end_idx;
785         /* If stride I/O mode is detected, get stride window*/
786         if (stride_io_mode(ras)) {
787                 ria->ria_stoff = ras->ras_stride_offset;
788                 ria->ria_length = ras->ras_stride_length;
789                 ria->ria_bytes = ras->ras_stride_bytes;
790         }
791         spin_unlock(&ras->ras_lock);
792
793         if (end_idx == 0) {
794                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
795                 RETURN(0);
796         }
797         pages = ria_page_count(ria);
798         if (pages == 0) {
799                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
800                 RETURN(0);
801         }
802
803         RAS_CDEBUG(ras);
804         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
805                PFID(lu_object_fid(&clob->co_lu)),
806                ria->ria_start_idx, ria->ria_end_idx,
807                vio->vui_ra_valid ? vio->vui_ra_start_idx : 0,
808                vio->vui_ra_valid ? vio->vui_ra_pages : 0,
809                hit);
810
811         /* at least to extend the readahead window to cover current read */
812         if (!hit && vio->vui_ra_valid &&
813             vio->vui_ra_start_idx + vio->vui_ra_pages > ria->ria_start_idx) {
814                 ria->ria_end_idx_min =
815                         vio->vui_ra_start_idx + vio->vui_ra_pages - 1;
816                 pages_min = vio->vui_ra_start_idx + vio->vui_ra_pages -
817                                 ria->ria_start_idx;
818                  /**
819                   * For performance reason, exceeding @ra_max_pages
820                   * are allowed, but this should be limited with RPC
821                   * size in case a large block size read issued. Trim
822                   * to RPC boundary.
823                   */
824                 pages_min = min(pages_min, ras->ras_rpc_pages -
825                                 (ria->ria_start_idx % ras->ras_rpc_pages));
826         }
827
828         /* don't over reserved for mmap range read */
829         if (skip_index)
830                 pages_min = 0;
831         if (pages_min > pages)
832                 pages = pages_min;
833         ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, pages,
834                                             pages_min);
835         if (ria->ria_reserved < pages)
836                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
837
838         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
839                ria->ria_reserved, pages, pages_min,
840                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
841                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
842
843         ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end_idx,
844                                   skip_index);
845         if (ria->ria_reserved != 0)
846                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
847
848         if (ra_end_idx == end_idx && ra_end_idx == (kms >> PAGE_SHIFT))
849                 ll_ra_stats_inc(inode, RA_STAT_EOF);
850
851         CDEBUG(D_READA,
852                "ra_end_idx = %lu end_idx = %lu stride end = %lu pages = %d\n",
853                ra_end_idx, end_idx, ria->ria_end_idx, ret);
854
855         if (ra_end_idx != end_idx)
856                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
857         if (ra_end_idx > 0) {
858                 /* update the ras so that the next read-ahead tries from
859                  * where we left off. */
860                 spin_lock(&ras->ras_lock);
861                 ras->ras_next_readahead_idx = ra_end_idx + 1;
862                 spin_unlock(&ras->ras_lock);
863                 RAS_CDEBUG(ras);
864         }
865
866         RETURN(ret);
867 }
868
869 static int ll_readpages(const struct lu_env *env, struct cl_io *io,
870                         struct cl_page_list *queue,
871                         pgoff_t start, pgoff_t end)
872 {
873         int ret = 0;
874         __u64 kms;
875         pgoff_t page_idx;
876         int count = 0;
877
878         ENTRY;
879
880         ret = ll_readahead_file_kms(env, io, &kms);
881         if (ret != 0)
882                 RETURN(ret);
883
884         if (kms == 0)
885                 RETURN(0);
886
887         if (end != 0) {
888                 unsigned long end_index;
889
890                 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
891                 if (end_index <= end)
892                         end = end_index;
893         }
894
895         for (page_idx = start; page_idx <= end; page_idx++) {
896                 ret= ll_read_ahead_page(env, io, queue, page_idx,
897                                         WILLNEED);
898                 if (ret < 0)
899                         break;
900                 else if (ret == 0) /* ret 1 is already uptodate */
901                         count++;
902         }
903
904         RETURN(count > 0 ? count : ret);
905 }
906
907 static void ras_set_start(struct ll_readahead_state *ras, pgoff_t index)
908 {
909         ras->ras_window_start_idx = ras_align(ras, index);
910 }
911
912 /* called with the ras_lock held or from places where it doesn't matter */
913 static void ras_reset(struct ll_readahead_state *ras, pgoff_t index)
914 {
915         ras->ras_consecutive_requests = 0;
916         ras->ras_consecutive_bytes = 0;
917         ras->ras_window_pages = 0;
918         ras_set_start(ras, index);
919         ras->ras_next_readahead_idx = max(ras->ras_window_start_idx, index + 1);
920
921         RAS_CDEBUG(ras);
922 }
923
924 /* called with the ras_lock held or from places where it doesn't matter */
925 static void ras_stride_reset(struct ll_readahead_state *ras)
926 {
927         ras->ras_consecutive_stride_requests = 0;
928         ras->ras_stride_length = 0;
929         ras->ras_stride_bytes = 0;
930         RAS_CDEBUG(ras);
931 }
932
933 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
934 {
935         spin_lock_init(&ras->ras_lock);
936         ras->ras_rpc_pages = PTLRPC_MAX_BRW_PAGES;
937         ras_reset(ras, 0);
938         ras->ras_last_read_end_bytes = 0;
939         ras->ras_requests = 0;
940         ras->ras_range_min_start_idx = 0;
941         ras->ras_range_max_end_idx = 0;
942         ras->ras_range_requests = 0;
943         ras->ras_last_range_pages = 0;
944 }
945
946 /*
947  * Check whether the read request is in the stride window.
948  * If it is in the stride window, return true, otherwise return false.
949  */
950 static bool read_in_stride_window(struct ll_readahead_state *ras,
951                                   loff_t pos, loff_t count)
952 {
953         loff_t stride_gap;
954
955         if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
956             ras->ras_stride_bytes == ras->ras_stride_length)
957                 return false;
958
959         stride_gap = pos - ras->ras_last_read_end_bytes - 1;
960
961         /* If it is contiguous read */
962         if (stride_gap == 0)
963                 return ras->ras_consecutive_bytes + count <=
964                         ras->ras_stride_bytes;
965
966         /* Otherwise check the stride by itself */
967         return (ras->ras_stride_length - ras->ras_stride_bytes) == stride_gap &&
968                 ras->ras_consecutive_bytes == ras->ras_stride_bytes &&
969                 count <= ras->ras_stride_bytes;
970 }
971
972 static void ras_init_stride_detector(struct ll_readahead_state *ras,
973                                      loff_t pos, loff_t count)
974 {
975         loff_t stride_gap = pos - ras->ras_last_read_end_bytes - 1;
976
977         LASSERT(ras->ras_consecutive_stride_requests == 0);
978
979         if (pos <= ras->ras_last_read_end_bytes) {
980                 /*Reset stride window for forward read*/
981                 ras_stride_reset(ras);
982                 return;
983         }
984
985         ras->ras_stride_bytes = ras->ras_consecutive_bytes;
986         ras->ras_stride_length = stride_gap + ras->ras_consecutive_bytes;
987         ras->ras_consecutive_stride_requests++;
988         ras->ras_stride_offset = pos;
989
990         RAS_CDEBUG(ras);
991 }
992
993 static unsigned long
994 stride_page_count(struct ll_readahead_state *ras, loff_t len)
995 {
996         loff_t bytes_count =
997                 stride_byte_count(ras->ras_stride_offset,
998                                   ras->ras_stride_length, ras->ras_stride_bytes,
999                                   ras->ras_window_start_idx << PAGE_SHIFT, len);
1000
1001         return (bytes_count + PAGE_SIZE - 1) >> PAGE_SHIFT;
1002 }
1003
1004 /* Stride Read-ahead window will be increased inc_len according to
1005  * stride I/O pattern */
1006 static void ras_stride_increase_window(struct ll_readahead_state *ras,
1007                                        struct ll_ra_info *ra, loff_t inc_bytes)
1008 {
1009         loff_t window_bytes, stride_bytes;
1010         u64 left_bytes;
1011         u64 step;
1012         loff_t end;
1013
1014         /* temporarily store in page units to reduce LASSERT() cost below */
1015         end = ras->ras_window_start_idx + ras->ras_window_pages;
1016
1017         LASSERT(ras->ras_stride_length > 0);
1018         LASSERTF(end >= (ras->ras_stride_offset >> PAGE_SHIFT),
1019                  "window_start_idx %lu, window_pages %lu stride_offset %llu\n",
1020                  ras->ras_window_start_idx, ras->ras_window_pages,
1021                  ras->ras_stride_offset);
1022
1023         end <<= PAGE_SHIFT;
1024         if (end <= ras->ras_stride_offset)
1025                 stride_bytes = 0;
1026         else
1027                 stride_bytes = end - ras->ras_stride_offset;
1028
1029         div64_u64_rem(stride_bytes, ras->ras_stride_length, &left_bytes);
1030         window_bytes = (ras->ras_window_pages << PAGE_SHIFT);
1031         if (left_bytes < ras->ras_stride_bytes) {
1032                 if (ras->ras_stride_bytes - left_bytes >= inc_bytes) {
1033                         window_bytes += inc_bytes;
1034                         goto out;
1035                 } else {
1036                         window_bytes += (ras->ras_stride_bytes - left_bytes);
1037                         inc_bytes -= (ras->ras_stride_bytes - left_bytes);
1038                 }
1039         } else {
1040                 window_bytes += (ras->ras_stride_length - left_bytes);
1041         }
1042
1043         LASSERT(ras->ras_stride_bytes != 0);
1044
1045         step = div64_u64_rem(inc_bytes, ras->ras_stride_bytes, &left_bytes);
1046
1047         window_bytes += step * ras->ras_stride_length + left_bytes;
1048         LASSERT(window_bytes > 0);
1049
1050 out:
1051         if (stride_page_count(ras, window_bytes) <=
1052             ra->ra_max_pages_per_file || ras->ras_window_pages == 0)
1053                 ras->ras_window_pages = (window_bytes >> PAGE_SHIFT);
1054
1055         LASSERT(ras->ras_window_pages > 0);
1056
1057         RAS_CDEBUG(ras);
1058 }
1059
1060 static void ras_increase_window(struct inode *inode,
1061                                 struct ll_readahead_state *ras,
1062                                 struct ll_ra_info *ra)
1063 {
1064         /* The stretch of ra-window should be aligned with max rpc_size
1065          * but current clio architecture does not support retrieve such
1066          * information from lower layer. FIXME later
1067          */
1068         if (stride_io_mode(ras)) {
1069                 ras_stride_increase_window(ras, ra,
1070                                       (loff_t)ras->ras_rpc_pages << PAGE_SHIFT);
1071         } else {
1072                 pgoff_t window_pages;
1073
1074                 window_pages = min(ras->ras_window_pages + ras->ras_rpc_pages,
1075                                    ra->ra_max_pages_per_file);
1076                 if (window_pages < ras->ras_rpc_pages)
1077                         ras->ras_window_pages = window_pages;
1078                 else
1079                         ras->ras_window_pages = ras_align(ras, window_pages);
1080         }
1081 }
1082
1083 /**
1084  * Seek within 8 pages are considered as sequential read for now.
1085  */
1086 static inline bool is_loose_seq_read(struct ll_readahead_state *ras, loff_t pos)
1087 {
1088         return pos_in_window(pos, ras->ras_last_read_end_bytes,
1089                              8UL << PAGE_SHIFT, 8UL << PAGE_SHIFT);
1090 }
1091
1092 static inline bool is_loose_mmap_read(struct ll_sb_info *sbi,
1093                                       struct ll_readahead_state *ras,
1094                                       unsigned long pos)
1095 {
1096         unsigned long range_pages = sbi->ll_ra_info.ra_range_pages;
1097
1098         return pos_in_window(pos, ras->ras_last_read_end_bytes,
1099                              range_pages << PAGE_SHIFT,
1100                              range_pages << PAGE_SHIFT);
1101 }
1102
1103 /**
1104  * We have observed slow mmap read performances for some
1105  * applications. The problem is if access pattern is neither
1106  * sequential nor stride, but could be still adjacent in a
1107  * small range and then seek a random position.
1108  *
1109  * So the pattern could be something like this:
1110  *
1111  * [1M data] [hole] [0.5M data] [hole] [0.7M data] [1M data]
1112  *
1113  *
1114  * Every time an application reads mmap data, it may not only
1115  * read a single 4KB page, but aslo a cluster of nearby pages in
1116  * a range(e.g. 1MB) of the first page after a cache miss.
1117  *
1118  * The readahead engine is modified to track the range size of
1119  * a cluster of mmap reads, so that after a seek and/or cache miss,
1120  * the range size is used to efficiently prefetch multiple pages
1121  * in a single RPC rather than many small RPCs.
1122  */
1123 static void ras_detect_cluster_range(struct ll_readahead_state *ras,
1124                                      struct ll_sb_info *sbi,
1125                                      unsigned long pos, unsigned long count)
1126 {
1127         pgoff_t last_pages, pages;
1128         pgoff_t end_idx = (pos + count - 1) >> PAGE_SHIFT;
1129
1130         last_pages = ras->ras_range_max_end_idx -
1131                         ras->ras_range_min_start_idx + 1;
1132         /* First time come here */
1133         if (!ras->ras_range_max_end_idx)
1134                 goto out;
1135
1136         /* Random or Stride read */
1137         if (!is_loose_mmap_read(sbi, ras, pos))
1138                 goto out;
1139
1140         ras->ras_range_requests++;
1141         if (ras->ras_range_max_end_idx < end_idx)
1142                 ras->ras_range_max_end_idx = end_idx;
1143
1144         if (ras->ras_range_min_start_idx > (pos >> PAGE_SHIFT))
1145                 ras->ras_range_min_start_idx = pos >> PAGE_SHIFT;
1146
1147         /* Out of range, consider it as random or stride */
1148         pages = ras->ras_range_max_end_idx -
1149                         ras->ras_range_min_start_idx + 1;
1150         if (pages <= sbi->ll_ra_info.ra_range_pages)
1151                 return;
1152 out:
1153         ras->ras_last_range_pages = last_pages;
1154         ras->ras_range_requests = 0;
1155         ras->ras_range_min_start_idx = pos >> PAGE_SHIFT;
1156         ras->ras_range_max_end_idx = end_idx;
1157 }
1158
1159 static void ras_detect_read_pattern(struct ll_readahead_state *ras,
1160                                     struct ll_sb_info *sbi,
1161                                     loff_t pos, size_t count, bool mmap)
1162 {
1163         bool stride_detect = false;
1164         pgoff_t index = pos >> PAGE_SHIFT;
1165
1166         /*
1167          * Reset the read-ahead window in two cases. First when the app seeks
1168          * or reads to some other part of the file. Secondly if we get a
1169          * read-ahead miss that we think we've previously issued. This can
1170          * be a symptom of there being so many read-ahead pages that the VM
1171          * is reclaiming it before we get to it.
1172          */
1173         if (!is_loose_seq_read(ras, pos)) {
1174                 /* Check whether it is in stride I/O mode */
1175                 if (!read_in_stride_window(ras, pos, count)) {
1176                         if (ras->ras_consecutive_stride_requests == 0)
1177                                 ras_init_stride_detector(ras, pos, count);
1178                         else
1179                                 ras_stride_reset(ras);
1180                         ras->ras_consecutive_bytes = 0;
1181                         ras_reset(ras, index);
1182                 } else {
1183                         ras->ras_consecutive_bytes = 0;
1184                         ras->ras_consecutive_requests = 0;
1185                         if (++ras->ras_consecutive_stride_requests > 1)
1186                                 stride_detect = true;
1187                         RAS_CDEBUG(ras);
1188                 }
1189                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
1190         } else if (stride_io_mode(ras)) {
1191                 /*
1192                  * If this is contiguous read but in stride I/O mode
1193                  * currently, check whether stride step still is valid,
1194                  * if invalid, it will reset the stride ra window to
1195                  * be zero.
1196                  */
1197                 if (!read_in_stride_window(ras, pos, count)) {
1198                         ras_stride_reset(ras);
1199                         ras->ras_window_pages = 0;
1200                         ras->ras_next_readahead_idx = index;
1201                 }
1202         }
1203
1204         ras->ras_consecutive_bytes += count;
1205         if (mmap) {
1206                 pgoff_t idx = ras->ras_consecutive_bytes >> PAGE_SHIFT;
1207                 unsigned long ra_range_pages =
1208                                 max_t(unsigned long, RA_MIN_MMAP_RANGE_PAGES,
1209                                       sbi->ll_ra_info.ra_range_pages);
1210
1211                 if ((idx >= ra_range_pages &&
1212                      idx % ra_range_pages == 0) || stride_detect)
1213                         ras->ras_need_increase_window = true;
1214         } else if ((ras->ras_consecutive_requests > 1 || stride_detect)) {
1215                 ras->ras_need_increase_window = true;
1216         }
1217
1218         ras->ras_last_read_end_bytes = pos + count - 1;
1219 }
1220
1221 void ll_ras_enter(struct file *f, loff_t pos, size_t count)
1222 {
1223         struct ll_file_data *fd = f->private_data;
1224         struct ll_readahead_state *ras = &fd->fd_ras;
1225         struct inode *inode = file_inode(f);
1226         unsigned long index = pos >> PAGE_SHIFT;
1227         struct ll_sb_info *sbi = ll_i2sbi(inode);
1228
1229         spin_lock(&ras->ras_lock);
1230         ras->ras_requests++;
1231         ras->ras_consecutive_requests++;
1232         ras->ras_need_increase_window = false;
1233         ras->ras_no_miss_check = false;
1234         /*
1235          * On the second access to a file smaller than the tunable
1236          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
1237          * file up to ra_max_pages_per_file.  This is simply a best effort
1238          * and only occurs once per open file. Normal RA behavior is reverted
1239          * to for subsequent IO.
1240          */
1241         if (ras->ras_requests >= 2) {
1242                 __u64 kms_pages;
1243                 struct ll_ra_info *ra = &sbi->ll_ra_info;
1244
1245                 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
1246                             PAGE_SHIFT;
1247
1248                 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
1249                        ra->ra_max_read_ahead_whole_pages,
1250                        ra->ra_max_pages_per_file);
1251
1252                 if (kms_pages &&
1253                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
1254                         ras->ras_window_start_idx = 0;
1255                         ras->ras_next_readahead_idx = index + 1;
1256                         ras->ras_window_pages = min(ra->ra_max_pages_per_file,
1257                                             ra->ra_max_read_ahead_whole_pages);
1258                         ras->ras_no_miss_check = true;
1259                         GOTO(out_unlock, 0);
1260                 }
1261         }
1262         ras_detect_read_pattern(ras, sbi, pos, count, false);
1263 out_unlock:
1264         spin_unlock(&ras->ras_lock);
1265 }
1266
1267 static bool index_in_stride_window(struct ll_readahead_state *ras,
1268                                    pgoff_t index)
1269 {
1270         loff_t pos = (loff_t)index << PAGE_SHIFT;
1271
1272         if (ras->ras_stride_length == 0 || ras->ras_stride_bytes == 0 ||
1273             ras->ras_stride_bytes == ras->ras_stride_length)
1274                 return false;
1275
1276         if (pos >= ras->ras_stride_offset) {
1277                 u64 offset;
1278
1279                 div64_u64_rem(pos - ras->ras_stride_offset,
1280                               ras->ras_stride_length, &offset);
1281                 if (offset < ras->ras_stride_bytes ||
1282                     ras->ras_stride_length - offset < PAGE_SIZE)
1283                         return true;
1284         } else if (ras->ras_stride_offset - pos < PAGE_SIZE) {
1285                 return true;
1286         }
1287
1288         return false;
1289 }
1290
1291 /*
1292  * ll_ras_enter() is used to detect read pattern according to pos and count.
1293  *
1294  * ras_update() is used to detect cache miss and
1295  * reset window or increase window accordingly
1296  */
1297 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
1298                        struct ll_readahead_state *ras, pgoff_t index,
1299                        enum ras_update_flags flags, struct cl_io *io)
1300 {
1301         struct ll_ra_info *ra = &sbi->ll_ra_info;
1302         bool hit = flags & LL_RAS_HIT;
1303
1304         ENTRY;
1305         spin_lock(&ras->ras_lock);
1306
1307         if (!hit)
1308                 CDEBUG(D_READA|D_IOTRACE, DFID " pages at %lu miss.\n",
1309                        PFID(ll_inode2fid(inode)), index);
1310         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
1311
1312         /*
1313          * The readahead window has been expanded to cover whole
1314          * file size, we don't care whether ra miss happen or not.
1315          * Because we will read whole file to page cache even if
1316          * some pages missed.
1317          */
1318         if (ras->ras_no_miss_check)
1319                 GOTO(out_unlock, 0);
1320
1321         if (io && io->ci_rand_read)
1322                 GOTO(out_unlock, 0);
1323
1324         if (io && io->ci_seq_read) {
1325                 if (!hit) {
1326                         /* to avoid many small read RPC here */
1327                         ras->ras_window_pages = sbi->ll_ra_info.ra_range_pages;
1328                         ll_ra_stats_inc_sbi(sbi, RA_STAT_MMAP_RANGE_READ);
1329                 }
1330                 goto skip;
1331         }
1332
1333         if (flags & LL_RAS_MMAP) {
1334                 unsigned long ra_pages;
1335
1336                 ras_detect_cluster_range(ras, sbi, index << PAGE_SHIFT,
1337                                          PAGE_SIZE);
1338                 ras_detect_read_pattern(ras, sbi, (loff_t)index << PAGE_SHIFT,
1339                                         PAGE_SIZE, true);
1340
1341                 /* we did not detect anything but we could prefetch */
1342                 if (!ras->ras_need_increase_window &&
1343                     ras->ras_window_pages <= sbi->ll_ra_info.ra_range_pages &&
1344                     ras->ras_range_requests >= 2) {
1345                         if (!hit) {
1346                                 ra_pages = max_t(unsigned long,
1347                                         RA_MIN_MMAP_RANGE_PAGES,
1348                                         ras->ras_last_range_pages);
1349                                 if (index < ra_pages / 2)
1350                                         index = 0;
1351                                 else
1352                                         index -= ra_pages / 2;
1353                                 ras->ras_window_pages = ra_pages;
1354                                 ll_ra_stats_inc_sbi(sbi,
1355                                         RA_STAT_MMAP_RANGE_READ);
1356                         } else {
1357                                 ras->ras_window_pages = 0;
1358                         }
1359                         goto skip;
1360                 }
1361         }
1362
1363         if (!hit && ras->ras_window_pages &&
1364             index < ras->ras_next_readahead_idx &&
1365             pos_in_window(index, ras->ras_window_start_idx, 0,
1366                           ras->ras_window_pages)) {
1367                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
1368                 ras->ras_need_increase_window = false;
1369
1370                 if (index_in_stride_window(ras, index) &&
1371                     stride_io_mode(ras)) {
1372                         /*
1373                          * if (index != ras->ras_last_readpage + 1)
1374                          *      ras->ras_consecutive_pages = 0;
1375                          */
1376                         ras_reset(ras, index);
1377
1378                         /*
1379                          * If stride-RA hit cache miss, the stride
1380                          * detector will not be reset to avoid the
1381                          * overhead of redetecting read-ahead mode,
1382                          * but on the condition that the stride window
1383                          * is still intersect with normal sequential
1384                          * read-ahead window.
1385                          */
1386                         if (ras->ras_window_start_idx < ras->ras_stride_offset)
1387                                 ras_stride_reset(ras);
1388                         RAS_CDEBUG(ras);
1389                 } else {
1390                         /*
1391                          * Reset both stride window and normal RA
1392                          * window.
1393                          */
1394                         ras_reset(ras, index);
1395                         /* ras->ras_consecutive_pages++; */
1396                         ras->ras_consecutive_bytes = 0;
1397                         ras_stride_reset(ras);
1398                         GOTO(out_unlock, 0);
1399                 }
1400         }
1401
1402 skip:
1403         ras_set_start(ras, index);
1404
1405         if (stride_io_mode(ras)) {
1406                 /* Since stride readahead is sentivite to the offset
1407                  * of read-ahead, so we use original offset here,
1408                  * instead of ras_window_start_idx, which is RPC aligned.
1409                  */
1410                 ras->ras_next_readahead_idx = max(index + 1,
1411                                                   ras->ras_next_readahead_idx);
1412                 ras->ras_window_start_idx =
1413                                 max_t(pgoff_t, ras->ras_window_start_idx,
1414                                       ras->ras_stride_offset >> PAGE_SHIFT);
1415         } else {
1416                 if (ras->ras_next_readahead_idx < ras->ras_window_start_idx)
1417                         ras->ras_next_readahead_idx = ras->ras_window_start_idx;
1418                 if (!hit)
1419                         ras->ras_next_readahead_idx = index + 1;
1420         }
1421
1422         if (ras->ras_need_increase_window) {
1423                 ras_increase_window(inode, ras, ra);
1424                 ras->ras_need_increase_window = false;
1425         }
1426
1427         EXIT;
1428 out_unlock:
1429         spin_unlock(&ras->ras_lock);
1430 }
1431
1432 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1433 {
1434         struct inode           *inode = vmpage->mapping->host;
1435         struct ll_inode_info   *lli   = ll_i2info(inode);
1436         struct lu_env          *env;
1437         struct cl_io           *io;
1438         struct cl_page         *page;
1439         struct cl_object       *clob;
1440         bool redirtied = false;
1441         bool unlocked = false;
1442         int result;
1443         __u16 refcheck;
1444         ENTRY;
1445
1446         LASSERT(PageLocked(vmpage));
1447         LASSERT(!PageWriteback(vmpage));
1448
1449         LASSERT(ll_i2dtexp(inode) != NULL);
1450
1451         env = cl_env_get(&refcheck);
1452         if (IS_ERR(env))
1453                 GOTO(out, result = PTR_ERR(env));
1454
1455         clob  = ll_i2info(inode)->lli_clob;
1456         LASSERT(clob != NULL);
1457
1458         io = vvp_env_thread_io(env);
1459         io->ci_obj = clob;
1460         io->ci_ignore_layout = 1;
1461         result = cl_io_init(env, io, CIT_MISC, clob);
1462         if (result == 0) {
1463                 page = cl_page_find(env, clob, vmpage->index,
1464                                     vmpage, CPT_CACHEABLE);
1465                 if (!IS_ERR(page)) {
1466                         lu_ref_add(&page->cp_reference, "writepage",
1467                                    current);
1468                         cl_page_assume(env, io, page);
1469                         result = cl_page_flush(env, io, page);
1470                         if (result != 0) {
1471                                 /*
1472                                  * Re-dirty page on error so it retries write,
1473                                  * but not in case when IO has actually
1474                                  * occurred and completed with an error.
1475                                  */
1476                                 if (!PageError(vmpage)) {
1477                                         redirty_page_for_writepage(wbc, vmpage);
1478                                         result = 0;
1479                                         redirtied = true;
1480                                 }
1481                         }
1482                         cl_page_disown(env, io, page);
1483                         unlocked = true;
1484                         lu_ref_del(&page->cp_reference,
1485                                    "writepage", current);
1486                         cl_page_put(env, page);
1487                 } else {
1488                         result = PTR_ERR(page);
1489                 }
1490         }
1491         cl_io_fini(env, io);
1492
1493         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1494                 loff_t offset = cl_offset(clob, vmpage->index);
1495
1496                 /* Flush page failed because the extent is being written out.
1497                  * Wait for the write of extent to be finished to avoid
1498                  * breaking kernel which assumes ->writepage should mark
1499                  * PageWriteback or clean the page. */
1500                 result = cl_sync_file_range(inode, offset,
1501                                             offset + PAGE_SIZE - 1,
1502                                             CL_FSYNC_LOCAL, 1);
1503                 if (result > 0) {
1504                         /* actually we may have written more than one page.
1505                          * decreasing this page because the caller will count
1506                          * it. */
1507                         wbc->nr_to_write -= result - 1;
1508                         result = 0;
1509                 }
1510         }
1511
1512         cl_env_put(env, &refcheck);
1513         GOTO(out, result);
1514
1515 out:
1516         if (result < 0) {
1517                 if (!lli->lli_async_rc)
1518                         lli->lli_async_rc = result;
1519                 SetPageError(vmpage);
1520                 if (!unlocked)
1521                         unlock_page(vmpage);
1522         }
1523         return result;
1524 }
1525
1526 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1527 {
1528         struct inode *inode = mapping->host;
1529         loff_t start;
1530         loff_t end;
1531         enum cl_fsync_mode mode;
1532         int range_whole = 0;
1533         int result;
1534         ENTRY;
1535
1536         if (wbc->range_cyclic) {
1537                 start = (loff_t)mapping->writeback_index << PAGE_SHIFT;
1538                 end = OBD_OBJECT_EOF;
1539         } else {
1540                 start = wbc->range_start;
1541                 end = wbc->range_end;
1542                 if (end == LLONG_MAX) {
1543                         end = OBD_OBJECT_EOF;
1544                         range_whole = start == 0;
1545                 }
1546         }
1547
1548         mode = CL_FSYNC_NONE;
1549         if (wbc->sync_mode == WB_SYNC_ALL)
1550                 mode = CL_FSYNC_LOCAL;
1551
1552         if (ll_i2info(inode)->lli_clob == NULL)
1553                 RETURN(0);
1554
1555         /* for directio, it would call writepages() to evict cached pages
1556          * inside the IO context of write, which will cause deadlock at
1557          * layout_conf since it waits for active IOs to complete. */
1558         result = cl_sync_file_range(inode, start, end, mode, 1);
1559         if (result > 0) {
1560                 wbc->nr_to_write -= result;
1561                 result = 0;
1562          }
1563
1564         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1565                 if (end == OBD_OBJECT_EOF)
1566                         mapping->writeback_index = 0;
1567                 else
1568                         mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1569         }
1570         RETURN(result);
1571 }
1572
1573 struct ll_cl_context *ll_cl_find(struct inode *inode)
1574 {
1575         struct ll_inode_info *lli = ll_i2info(inode);
1576         struct ll_cl_context *lcc;
1577         struct ll_cl_context *found = NULL;
1578
1579         read_lock(&lli->lli_lock);
1580         list_for_each_entry(lcc, &lli->lli_lccs, lcc_list) {
1581                 if (lcc->lcc_cookie == current) {
1582                         found = lcc;
1583                         break;
1584                 }
1585         }
1586         read_unlock(&lli->lli_lock);
1587
1588         return found;
1589 }
1590
1591 void ll_cl_add(struct inode *inode, const struct lu_env *env, struct cl_io *io,
1592                enum lcc_type type)
1593 {
1594         struct ll_inode_info *lli = ll_i2info(inode);
1595         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1596
1597         memset(lcc, 0, sizeof(*lcc));
1598         INIT_LIST_HEAD(&lcc->lcc_list);
1599         lcc->lcc_cookie = current;
1600         lcc->lcc_env = env;
1601         lcc->lcc_io = io;
1602         lcc->lcc_type = type;
1603
1604         write_lock(&lli->lli_lock);
1605         list_add(&lcc->lcc_list, &lli->lli_lccs);
1606         write_unlock(&lli->lli_lock);
1607 }
1608
1609 void ll_cl_remove(struct inode *inode, const struct lu_env *env)
1610 {
1611         struct ll_inode_info *lli = ll_i2info(inode);
1612         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1613
1614         write_lock(&lli->lli_lock);
1615         list_del_init(&lcc->lcc_list);
1616         write_unlock(&lli->lli_lock);
1617 }
1618
1619 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1620                            struct cl_page *page, struct file *file)
1621 {
1622         struct inode              *inode  = vvp_object_inode(page->cp_obj);
1623         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1624         struct ll_file_data       *fd     = NULL;
1625         struct ll_readahead_state *ras    = NULL;
1626         struct cl_2queue          *queue  = &io->ci_queue;
1627         struct cl_sync_io         *anchor = NULL;
1628         struct vvp_page           *vpg;
1629         int                        rc = 0, rc2 = 0;
1630         bool                       uptodate;
1631         struct vvp_io *vio = vvp_env_io(env);
1632         bool mmap = !vio->vui_ra_valid;
1633         pgoff_t ra_start_index = 0;
1634         pgoff_t io_start_index;
1635         pgoff_t io_end_index;
1636         bool unlockpage = true;
1637         ENTRY;
1638
1639         if (file) {
1640                 fd = file->private_data;
1641                 ras = &fd->fd_ras;
1642         }
1643
1644         /* PagePrivate2 is set in ll_io_zero_page() to tell us the vmpage
1645          * must not be unlocked after processing.
1646          */
1647         if (page->cp_vmpage && PagePrivate2(page->cp_vmpage))
1648                 unlockpage = false;
1649
1650         vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1651         uptodate = page->cp_defer_uptodate;
1652
1653         if (ll_readahead_enabled(sbi) && !page->cp_ra_updated && ras) {
1654                 enum ras_update_flags flags = 0;
1655
1656                 if (uptodate)
1657                         flags |= LL_RAS_HIT;
1658                 if (mmap)
1659                         flags |= LL_RAS_MMAP;
1660                 ras_update(sbi, inode, ras, vvp_index(vpg), flags, io);
1661         }
1662
1663         cl_2queue_init(queue);
1664         if (uptodate) {
1665                 page->cp_ra_used = 1;
1666                 SetPageUptodate(page->cp_vmpage);
1667                 cl_page_disown(env, io, page);
1668         } else {
1669                 anchor = &vvp_env_info(env)->vti_anchor;
1670                 cl_sync_io_init(anchor, 1);
1671                 page->cp_sync_io = anchor;
1672
1673                 cl_2queue_add(queue, page, true);
1674         }
1675
1676         /* mmap does not set the ci_rw fields */
1677         if (!mmap) {
1678                 io_start_index = cl_index(io->ci_obj, io->u.ci_rw.crw_pos);
1679                 io_end_index = cl_index(io->ci_obj, io->u.ci_rw.crw_pos +
1680                                         io->u.ci_rw.crw_count - 1);
1681         } else {
1682                 io_start_index = vvp_index(vpg);
1683                 io_end_index = vvp_index(vpg);
1684         }
1685
1686         if (ll_readahead_enabled(sbi) && ras && !io->ci_rand_read) {
1687                 pgoff_t skip_index = 0;
1688
1689                 if (ras->ras_next_readahead_idx < vvp_index(vpg))
1690                         skip_index = vvp_index(vpg);
1691                 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1692                                    uptodate, file, skip_index,
1693                                    &ra_start_index);
1694                 /* to keep iotrace clean, we only print here if we actually
1695                  * read pages
1696                  */
1697                 CDEBUG(D_READA | (rc2 ? D_IOTRACE : 0),
1698                        DFID " %d pages read ahead at %lu, triggered by user read at %lu, stride offset %lld, stride length %lld, stride bytes %lld\n",
1699                        PFID(ll_inode2fid(inode)), rc2, ra_start_index,
1700                        vvp_index(vpg), ras->ras_stride_offset,
1701                        ras->ras_stride_length, ras->ras_stride_bytes);
1702
1703         } else if (vvp_index(vpg) == io_start_index &&
1704                    io_end_index - io_start_index > 0) {
1705                 rc2 = ll_readpages(env, io, &queue->c2_qin, io_start_index + 1,
1706                                    io_end_index);
1707                 CDEBUG(D_READA, DFID " %d pages read at %lu\n",
1708                        PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1709         }
1710
1711         if (queue->c2_qin.pl_nr > 0) {
1712                 int count = queue->c2_qin.pl_nr;
1713                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1714                 if (rc == 0)
1715                         task_io_account_read(PAGE_SIZE * count);
1716         }
1717
1718
1719         if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1720                 rc = cl_sync_io_wait(env, anchor, 0);
1721
1722                 cl_page_assume(env, io, page);
1723                 cl_page_list_del(env, &queue->c2_qout, page);
1724
1725                 if (!PageUptodate(cl_page_vmpage(page))) {
1726                         /* Failed to read a mirror, discard this page so that
1727                          * new page can be created with new mirror.
1728                          *
1729                          * TODO: this is not needed after page reinit
1730                          * route is implemented */
1731                         cl_page_discard(env, io, page);
1732                 }
1733                 if (unlockpage)
1734                         cl_page_disown(env, io, page);
1735         }
1736
1737         /* TODO: discard all pages until page reinit route is implemented */
1738         cl_page_list_discard(env, io, &queue->c2_qin);
1739
1740         /* Unlock unsent read pages in case of error. */
1741         cl_page_list_disown(env, &queue->c2_qin);
1742
1743         cl_2queue_fini(env, queue);
1744
1745         RETURN(rc);
1746 }
1747
1748 /*
1749  * Possible return value:
1750  * 0 no async readahead triggered and fast read could not be used.
1751  * 1 no async readahead, but fast read could be used.
1752  * 2 async readahead triggered and fast read could be used too.
1753  * < 0 on error.
1754  */
1755 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1756 {
1757         struct ll_readahead_work *lrw;
1758         struct inode *inode = file_inode(file);
1759         struct ll_sb_info *sbi = ll_i2sbi(inode);
1760         struct ll_file_data *fd = file->private_data;
1761         struct ll_readahead_state *ras = &fd->fd_ras;
1762         struct ll_ra_info *ra = &sbi->ll_ra_info;
1763         unsigned long throttle;
1764         pgoff_t start_idx = ras_align(ras, ras->ras_next_readahead_idx);
1765         pgoff_t end_idx = start_idx + pages - 1;
1766
1767         /**
1768          * In case we have a limited max_cached_mb, readahead
1769          * should be stopped if it have run out of all LRU slots.
1770          */
1771         if (atomic_read(&ra->ra_cur_pages) >= sbi->ll_cache->ccc_lru_max) {
1772                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
1773                 return 0;
1774         }
1775
1776         throttle = min(ra->ra_async_pages_per_file_threshold,
1777                        ra->ra_max_pages_per_file);
1778         /*
1779          * If this is strided i/o or the window is smaller than the
1780          * throttle limit, we do not do async readahead. Otherwise,
1781          * we do async readahead, allowing the user thread to do fast i/o.
1782          */
1783         if (stride_io_mode(ras) || !throttle ||
1784             ras->ras_window_pages < throttle ||
1785             atomic_read(&ra->ra_async_inflight) > ra->ra_async_max_active)
1786                 return 0;
1787
1788         if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1789                 return 0;
1790
1791         if (ras->ras_async_last_readpage_idx == start_idx)
1792                 return 1;
1793
1794         /* ll_readahead_work_free() free it */
1795         OBD_ALLOC_PTR(lrw);
1796         if (lrw) {
1797                 atomic_inc(&sbi->ll_ra_info.ra_async_inflight);
1798                 lrw->lrw_file = get_file(file);
1799                 lrw->lrw_start_idx = start_idx;
1800                 lrw->lrw_end_idx = end_idx;
1801                 lrw->lrw_user_pid = current->pid;
1802                 spin_lock(&ras->ras_lock);
1803                 ras->ras_next_readahead_idx = end_idx + 1;
1804                 ras->ras_async_last_readpage_idx = start_idx;
1805                 spin_unlock(&ras->ras_lock);
1806                 memcpy(lrw->lrw_jobid, ll_i2info(inode)->lli_jobid,
1807                        sizeof(lrw->lrw_jobid));
1808                 ll_readahead_work_add(inode, lrw);
1809         } else {
1810                 return -ENOMEM;
1811         }
1812
1813         return 2;
1814 }
1815
1816 /*
1817  * Check if we can issue a readahead RPC, if that is
1818  * the case, we can't do fast IO because we will need
1819  * a cl_io to issue the RPC.
1820  */
1821 static bool ll_use_fast_io(struct file *file,
1822                            struct ll_readahead_state *ras, pgoff_t index)
1823 {
1824         unsigned long fast_read_pages =
1825                 max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_pages);
1826         loff_t skip_pages;
1827         loff_t stride_bytes = ras->ras_stride_bytes;
1828
1829         if (stride_io_mode(ras) && stride_bytes) {
1830                 skip_pages = (ras->ras_stride_length +
1831                         ras->ras_stride_bytes - 1) / stride_bytes;
1832                 skip_pages *= fast_read_pages;
1833         } else {
1834                 skip_pages = fast_read_pages;
1835         }
1836
1837         if (ras->ras_window_start_idx + ras->ras_window_pages <
1838             ras->ras_next_readahead_idx + skip_pages ||
1839             kickoff_async_readahead(file, fast_read_pages) > 0)
1840                 return true;
1841
1842         return false;
1843 }
1844
1845 int ll_readpage(struct file *file, struct page *vmpage)
1846 {
1847         struct inode *inode = file_inode(file);
1848         struct cl_object *clob = ll_i2info(inode)->lli_clob;
1849         struct ll_cl_context *lcc;
1850         const struct lu_env  *env = NULL;
1851         struct cl_io   *io = NULL;
1852         struct cl_page *page;
1853         struct ll_sb_info *sbi = ll_i2sbi(inode);
1854         int result;
1855         ENTRY;
1856
1857         lcc = ll_cl_find(inode);
1858         if (lcc != NULL) {
1859                 env = lcc->lcc_env;
1860                 io  = lcc->lcc_io;
1861         }
1862
1863         if (io == NULL) { /* fast read */
1864                 struct inode *inode = file_inode(file);
1865                 struct ll_file_data *fd = file->private_data;
1866                 struct ll_readahead_state *ras = &fd->fd_ras;
1867                 struct lu_env  *local_env = NULL;
1868                 struct vvp_page *vpg;
1869
1870                 result = -ENODATA;
1871
1872                 /* TODO: need to verify the layout version to make sure
1873                  * the page is not invalid due to layout change. */
1874                 page = cl_vmpage_page(vmpage, clob);
1875                 if (page == NULL) {
1876                         unlock_page(vmpage);
1877                         ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1878                         RETURN(result);
1879                 }
1880
1881                 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1882                 if (page->cp_defer_uptodate) {
1883                         enum ras_update_flags flags = LL_RAS_HIT;
1884
1885                         if (lcc && lcc->lcc_type == LCC_MMAP)
1886                                 flags |= LL_RAS_MMAP;
1887
1888                         /* For fast read, it updates read ahead state only
1889                          * if the page is hit in cache because non cache page
1890                          * case will be handled by slow read later. */
1891                         ras_update(sbi, inode, ras, vvp_index(vpg), flags, io);
1892                         /* avoid duplicate ras_update() call */
1893                         page->cp_ra_updated = 1;
1894
1895                         if (ll_use_fast_io(file, ras, vvp_index(vpg)))
1896                                 result = 0;
1897                 }
1898
1899                 if (!env) {
1900                         local_env = cl_env_percpu_get();
1901                         env = local_env;
1902                 }
1903
1904                 /* export the page and skip io stack */
1905                 if (result == 0) {
1906                         page->cp_ra_used = 1;
1907                         SetPageUptodate(vmpage);
1908                 } else {
1909                         ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1910                 }
1911
1912                 /* release page refcount before unlocking the page to ensure
1913                  * the object won't be destroyed in the calling path of
1914                  * cl_page_put(). Please see comment in ll_releasepage(). */
1915                 cl_page_put(env, page);
1916                 unlock_page(vmpage);
1917                 if (local_env)
1918                         cl_env_percpu_put(local_env);
1919
1920                 RETURN(result);
1921         }
1922
1923         /**
1924          * Direct read can fall back to buffered read, but DIO is done
1925          * with lockless i/o, and buffered requires LDLM locking, so in
1926          * this case we must restart without lockless.
1927          */
1928         if (file->f_flags & O_DIRECT &&
1929             lcc && lcc->lcc_type == LCC_RW &&
1930             !io->ci_dio_lock) {
1931                 unlock_page(vmpage);
1932                 io->ci_dio_lock = 1;
1933                 io->ci_need_restart = 1;
1934                 RETURN(-ENOLCK);
1935         }
1936
1937         LASSERT(io->ci_state == CIS_IO_GOING);
1938         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1939         if (!IS_ERR(page)) {
1940                 LASSERT(page->cp_type == CPT_CACHEABLE);
1941                 if (likely(!PageUptodate(vmpage))) {
1942                         cl_page_assume(env, io, page);
1943
1944                         result = ll_io_read_page(env, io, page, file);
1945                 } else {
1946                         /* Page from a non-object file. */
1947                         unlock_page(vmpage);
1948                         result = 0;
1949                 }
1950                 cl_page_put(env, page);
1951         } else {
1952                 unlock_page(vmpage);
1953                 result = PTR_ERR(page);
1954         }
1955         RETURN(result);
1956 }