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