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