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