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