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