Whamcloud - gitweb
LU-12043 llite: extend readahead locks for striped file
[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, unsigned long min)
86 {
87         struct ll_ra_info *ra = &sbi->ll_ra_info;
88         long ret;
89         ENTRY;
90
91         /* If read-ahead pages left are less than 1M, do not do read-ahead,
92          * otherwise it will form small read RPC(< 1M), which hurt server
93          * performance a lot. */
94         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages),
95                   pages);
96         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
97                 GOTO(out, ret = 0);
98
99         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
100                 atomic_sub(ret, &ra->ra_cur_pages);
101                 ret = 0;
102         }
103
104 out:
105         if (ret < min) {
106                 /* override ra limit for maximum performance */
107                 atomic_add(min - ret, &ra->ra_cur_pages);
108                 ret = min;
109         }
110         RETURN(ret);
111 }
112
113 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
114 {
115         struct ll_ra_info *ra = &sbi->ll_ra_info;
116         atomic_sub(len, &ra->ra_cur_pages);
117 }
118
119 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
120 {
121         LASSERTF(which < _NR_RA_STAT, "which: %u\n", which);
122         lprocfs_counter_incr(sbi->ll_ra_stats, which);
123 }
124
125 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
126 {
127         struct ll_sb_info *sbi = ll_i2sbi(inode);
128         ll_ra_stats_inc_sbi(sbi, which);
129 }
130
131 #define RAS_CDEBUG(ras) \
132         CDEBUG(D_READA,                                                      \
133                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu rpc %lu "        \
134                "r %lu ri %lu csr %lu sf %lu sp %lu sl %lu lr %lu\n", \
135                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
136                ras->ras_consecutive_pages, ras->ras_window_start,            \
137                ras->ras_window_len, ras->ras_next_readahead,                 \
138                ras->ras_rpc_size,                                            \
139                ras->ras_requests, ras->ras_request_index,                    \
140                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
141                ras->ras_stride_pages, ras->ras_stride_length,                \
142                ras->ras_async_last_readpage)
143
144 static int index_in_window(unsigned long index, unsigned long point,
145                            unsigned long before, unsigned long after)
146 {
147         unsigned long start = point - before, end = point + after;
148
149         if (start > point)
150                start = 0;
151         if (end < point)
152                end = ~0;
153
154         return start <= index && index <= end;
155 }
156
157 void ll_ras_enter(struct file *f)
158 {
159         struct ll_file_data *fd = LUSTRE_FPRIVATE(f);
160         struct ll_readahead_state *ras = &fd->fd_ras;
161
162         spin_lock(&ras->ras_lock);
163         ras->ras_requests++;
164         ras->ras_request_index = 0;
165         ras->ras_consecutive_requests++;
166         spin_unlock(&ras->ras_lock);
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 {
180         struct cl_object *clob  = io->ci_obj;
181         struct inode     *inode = vvp_object_inode(clob);
182         struct page      *vmpage;
183         struct cl_page   *page;
184         struct vvp_page  *vpg;
185         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
186         int               rc    = 0;
187         const char       *msg   = NULL;
188         ENTRY;
189
190         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
191         if (vmpage == NULL) {
192                 which = RA_STAT_FAILED_GRAB_PAGE;
193                 msg   = "g_c_p_n failed";
194                 GOTO(out, rc = -EBUSY);
195         }
196
197         /* Check if vmpage was truncated or reclaimed */
198         if (vmpage->mapping != inode->i_mapping) {
199                 which = RA_STAT_WRONG_GRAB_PAGE;
200                 msg   = "g_c_p_n returned invalid page";
201                 GOTO(out, rc = -EBUSY);
202         }
203
204         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
205         if (IS_ERR(page)) {
206                 which = RA_STAT_FAILED_GRAB_PAGE;
207                 msg   = "cl_page_find failed";
208                 GOTO(out, rc = PTR_ERR(page));
209         }
210
211         lu_ref_add(&page->cp_reference, "ra", current);
212         cl_page_assume(env, io, page);
213         vpg = cl2vvp_page(cl_object_page_slice(clob, page));
214         if (!vpg->vpg_defer_uptodate && !PageUptodate(vmpage)) {
215                 vpg->vpg_defer_uptodate = 1;
216                 vpg->vpg_ra_used = 0;
217                 cl_page_list_add(queue, page);
218         } else {
219                 /* skip completed pages */
220                 cl_page_unassume(env, io, page);
221                 /* This page is already uptodate, returning a positive number
222                  * to tell the callers about this */
223                 rc = 1;
224         }
225
226         lu_ref_del(&page->cp_reference, "ra", current);
227         cl_page_put(env, page);
228
229 out:
230         if (vmpage != NULL) {
231                 if (rc != 0)
232                         unlock_page(vmpage);
233                 put_page(vmpage);
234         }
235         if (msg != NULL) {
236                 ll_ra_stats_inc(inode, which);
237                 CDEBUG(D_READA, "%s\n", msg);
238
239         }
240
241         RETURN(rc);
242 }
243
244 #define RIA_DEBUG(ria)                                                       \
245         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
246         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
247         ria->ria_pages)
248
249 static inline int stride_io_mode(struct ll_readahead_state *ras)
250 {
251         return ras->ras_consecutive_stride_requests > 1;
252 }
253
254 /* The function calculates how much pages will be read in
255  * [off, off + length], in such stride IO area,
256  * stride_offset = st_off, stride_lengh = st_len,
257  * stride_pages = st_pgs
258  *
259  *   |------------------|*****|------------------|*****|------------|*****|....
260  * st_off
261  *   |--- st_pgs     ---|
262  *   |-----     st_len   -----|
263  *
264  *              How many pages it should read in such pattern
265  *              |-------------------------------------------------------------|
266  *              off
267  *              |<------                  length                      ------->|
268  *
269  *          =   |<----->|  +  |-------------------------------------| +   |---|
270  *             start_left                 st_pgs * i                    end_left
271  */
272 static unsigned long
273 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
274                 unsigned long off, unsigned long length)
275 {
276         __u64 start = off > st_off ? off - st_off : 0;
277         __u64 end = off + length > st_off ? off + length - st_off : 0;
278         unsigned long start_left = 0;
279         unsigned long end_left = 0;
280         unsigned long pg_count;
281
282         if (st_len == 0 || length == 0 || end == 0)
283                 return length;
284
285         start_left = do_div(start, st_len);
286         if (start_left < st_pgs)
287                 start_left = st_pgs - start_left;
288         else
289                 start_left = 0;
290
291         end_left = do_div(end, st_len);
292         if (end_left > st_pgs)
293                 end_left = st_pgs;
294
295         CDEBUG(D_READA, "start %llu, end %llu start_left %lu end_left %lu\n",
296                start, end, start_left, end_left);
297
298         if (start == end)
299                 pg_count = end_left - (st_pgs - start_left);
300         else
301                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
302
303         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
304                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
305
306         return pg_count;
307 }
308
309 static int ria_page_count(struct ra_io_arg *ria)
310 {
311         __u64 length = ria->ria_end >= ria->ria_start ?
312                        ria->ria_end - ria->ria_start + 1 : 0;
313
314         return stride_pg_count(ria->ria_stoff, ria->ria_length,
315                                ria->ria_pages, ria->ria_start,
316                                length);
317 }
318
319 static unsigned long ras_align(struct ll_readahead_state *ras,
320                                unsigned long index,
321                                unsigned long *remainder)
322 {
323         unsigned long rem = index % ras->ras_rpc_size;
324         if (remainder != NULL)
325                 *remainder = rem;
326         return index - rem;
327 }
328
329 /*Check whether the index is in the defined ra-window */
330 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
331 {
332         /* If ria_length == ria_pages, it means non-stride I/O mode,
333          * idx should always inside read-ahead window in this case
334          * For stride I/O mode, just check whether the idx is inside
335          * the ria_pages. */
336         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
337                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
338                 ria->ria_length < ria->ria_pages);
339 }
340
341 static unsigned long
342 ll_read_ahead_pages(const struct lu_env *env, struct cl_io *io,
343                     struct cl_page_list *queue, struct ll_readahead_state *ras,
344                     struct ra_io_arg *ria, pgoff_t *ra_end)
345 {
346         struct cl_read_ahead ra = { 0 };
347         int rc = 0, count = 0;
348         bool stride_ria;
349         pgoff_t page_idx;
350
351         LASSERT(ria != NULL);
352         RIA_DEBUG(ria);
353
354         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
355         for (page_idx = ria->ria_start;
356              page_idx <= ria->ria_end && ria->ria_reserved > 0; page_idx++) {
357                 if (ras_inside_ra_window(page_idx, ria)) {
358                         if (ra.cra_end == 0 || ra.cra_end < page_idx) {
359                                 unsigned long end;
360
361                                 cl_read_ahead_release(env, &ra);
362
363                                 rc = cl_io_read_ahead(env, io, page_idx, &ra);
364                                 if (rc < 0)
365                                         break;
366
367                                 /* Do not shrink the ria_end at any case until
368                                  * the minimum end of current read is covered.
369                                  * And only shrink the ria_end if the matched
370                                  * LDLM lock doesn't cover more. */
371                                 if (page_idx > ra.cra_end ||
372                                     (ra.cra_contention &&
373                                      page_idx > ria->ria_end_min)) {
374                                         ria->ria_end = ra.cra_end;
375                                         break;
376                                 }
377
378                                 CDEBUG(D_READA, "idx: %lu, ra: %lu, rpc: %lu\n",
379                                        page_idx, ra.cra_end, ra.cra_rpc_size);
380                                 LASSERTF(ra.cra_end >= page_idx,
381                                          "object: %p, indcies %lu / %lu\n",
382                                          io->ci_obj, ra.cra_end, page_idx);
383                                 /* update read ahead RPC size.
384                                  * NB: it's racy but doesn't matter */
385                                 if (ras->ras_rpc_size != ra.cra_rpc_size &&
386                                     ra.cra_rpc_size > 0)
387                                         ras->ras_rpc_size = ra.cra_rpc_size;
388                                 /* trim it to align with optimal RPC size */
389                                 end = ras_align(ras, ria->ria_end + 1, NULL);
390                                 if (end > 0 && !ria->ria_eof)
391                                         ria->ria_end = end - 1;
392                                 if (ria->ria_end < ria->ria_end_min)
393                                         ria->ria_end = ria->ria_end_min;
394                         }
395                         if (page_idx > ria->ria_end)
396                                 break;
397
398                         /* If the page is inside the read-ahead window */
399                         rc = ll_read_ahead_page(env, io, queue, page_idx);
400                         if (rc < 0)
401                                 break;
402
403                         *ra_end = page_idx;
404                         /* Only subtract from reserve & count the page if we
405                          * really did readahead on that page. */
406                         if (rc == 0) {
407                                 ria->ria_reserved--;
408                                 count++;
409                         }
410                 } else if (stride_ria) {
411                         /* If it is not in the read-ahead window, and it is
412                          * read-ahead mode, then check whether it should skip
413                          * the stride gap.
414                          */
415                         pgoff_t offset;
416                         /* NOTE: This assertion only is valid when it is for
417                          * forward read-ahead, must adjust if backward
418                          * readahead is implemented.
419                          */
420                         LASSERTF(page_idx >= ria->ria_stoff,
421                                 "Invalid page_idx %lu rs %lu re %lu ro %lu "
422                                 "rl %lu rp %lu\n", page_idx,
423                                 ria->ria_start, ria->ria_end, ria->ria_stoff,
424                                 ria->ria_length, ria->ria_pages);
425                         offset = page_idx - ria->ria_stoff;
426                         offset = offset % (ria->ria_length);
427                         if (offset >= ria->ria_pages) {
428                                 page_idx += ria->ria_length - offset - 1;
429                                 CDEBUG(D_READA,
430                                        "Stride: jump %lu pages to %lu\n",
431                                        ria->ria_length - offset, page_idx);
432                                 continue;
433                         }
434                 }
435         }
436
437         cl_read_ahead_release(env, &ra);
438
439         return count;
440 }
441
442 static void ll_readahead_work_free(struct ll_readahead_work *work)
443 {
444         fput(work->lrw_file);
445         OBD_FREE_PTR(work);
446 }
447
448 static void ll_readahead_handle_work(struct work_struct *wq);
449 static void ll_readahead_work_add(struct inode *inode,
450                                   struct ll_readahead_work *work)
451 {
452         INIT_WORK(&work->lrw_readahead_work, ll_readahead_handle_work);
453         queue_work(ll_i2sbi(inode)->ll_ra_info.ll_readahead_wq,
454                    &work->lrw_readahead_work);
455 }
456
457 static int ll_readahead_file_kms(const struct lu_env *env,
458                                 struct cl_io *io, __u64 *kms)
459 {
460         struct cl_object *clob;
461         struct inode *inode;
462         struct cl_attr *attr = vvp_env_thread_attr(env);
463         int ret;
464
465         clob = io->ci_obj;
466         inode = vvp_object_inode(clob);
467
468         cl_object_attr_lock(clob);
469         ret = cl_object_attr_get(env, clob, attr);
470         cl_object_attr_unlock(clob);
471
472         if (ret != 0)
473                 RETURN(ret);
474
475         *kms = attr->cat_kms;
476         return 0;
477 }
478
479 static void ll_readahead_handle_work(struct work_struct *wq)
480 {
481         struct ll_readahead_work *work;
482         struct lu_env *env;
483         __u16 refcheck;
484         struct ra_io_arg *ria;
485         struct inode *inode;
486         struct ll_file_data *fd;
487         struct ll_readahead_state *ras;
488         struct cl_io *io;
489         struct cl_2queue *queue;
490         pgoff_t ra_end = 0;
491         unsigned long len, mlen = 0;
492         struct file *file;
493         __u64 kms;
494         int rc;
495         unsigned long end_index;
496
497         work = container_of(wq, struct ll_readahead_work,
498                             lrw_readahead_work);
499         fd = LUSTRE_FPRIVATE(work->lrw_file);
500         ras = &fd->fd_ras;
501         file = work->lrw_file;
502         inode = file_inode(file);
503
504         env = cl_env_alloc(&refcheck, LCT_NOREF);
505         if (IS_ERR(env))
506                 GOTO(out_free_work, rc = PTR_ERR(env));
507
508         io = vvp_env_thread_io(env);
509         ll_io_init(io, file, CIT_READ, NULL);
510
511         rc = ll_readahead_file_kms(env, io, &kms);
512         if (rc != 0)
513                 GOTO(out_put_env, rc);
514
515         if (kms == 0) {
516                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
517                 GOTO(out_put_env, rc = 0);
518         }
519
520         ria = &ll_env_info(env)->lti_ria;
521         memset(ria, 0, sizeof(*ria));
522
523         ria->ria_start = work->lrw_start;
524         /* Truncate RA window to end of file */
525         end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
526         if (end_index <= work->lrw_end) {
527                 work->lrw_end = end_index;
528                 ria->ria_eof = true;
529         }
530         if (work->lrw_end <= work->lrw_start)
531                 GOTO(out_put_env, rc = 0);
532
533         ria->ria_end = work->lrw_end;
534         len = ria->ria_end - ria->ria_start + 1;
535         ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria,
536                                             ria_page_count(ria), mlen);
537
538         CDEBUG(D_READA,
539                "async reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
540                ria->ria_reserved, len, mlen,
541                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
542                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
543
544         if (ria->ria_reserved < len) {
545                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
546                 if (PAGES_TO_MiB(ria->ria_reserved) < 1) {
547                         ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
548                         GOTO(out_put_env, rc = 0);
549                 }
550         }
551
552         rc = cl_io_rw_init(env, io, CIT_READ, ria->ria_start, len);
553         if (rc)
554                 GOTO(out_put_env, rc);
555
556         vvp_env_io(env)->vui_io_subtype = IO_NORMAL;
557         vvp_env_io(env)->vui_fd = fd;
558         io->ci_state = CIS_LOCKED;
559         io->ci_async_readahead = true;
560         rc = cl_io_start(env, io);
561         if (rc)
562                 GOTO(out_io_fini, rc);
563
564         queue = &io->ci_queue;
565         cl_2queue_init(queue);
566
567         rc = ll_read_ahead_pages(env, io, &queue->c2_qin, ras, ria, &ra_end);
568         if (ria->ria_reserved != 0)
569                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
570         if (queue->c2_qin.pl_nr > 0) {
571                 int count = queue->c2_qin.pl_nr;
572
573                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
574                 if (rc == 0)
575                         task_io_account_read(PAGE_SIZE * count);
576         }
577         if (ria->ria_end == ra_end && ra_end == (kms >> PAGE_SHIFT))
578                 ll_ra_stats_inc(inode, RA_STAT_EOF);
579
580         if (ra_end != ria->ria_end)
581                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
582
583         /* TODO: discard all pages until page reinit route is implemented */
584         cl_page_list_discard(env, io, &queue->c2_qin);
585
586         /* Unlock unsent read pages in case of error. */
587         cl_page_list_disown(env, io, &queue->c2_qin);
588
589         cl_2queue_fini(env, queue);
590 out_io_fini:
591         cl_io_end(env, io);
592         cl_io_fini(env, io);
593 out_put_env:
594         cl_env_put(env, &refcheck);
595 out_free_work:
596         if (ra_end > 0)
597                 ll_ra_stats_inc_sbi(ll_i2sbi(inode), RA_STAT_ASYNC);
598         ll_readahead_work_free(work);
599 }
600
601 static int ll_readahead(const struct lu_env *env, struct cl_io *io,
602                         struct cl_page_list *queue,
603                         struct ll_readahead_state *ras, bool hit,
604                         struct file *file)
605 {
606         struct vvp_io *vio = vvp_env_io(env);
607         struct ll_thread_info *lti = ll_env_info(env);
608         unsigned long len, mlen = 0;
609         pgoff_t ra_end = 0, start = 0, end = 0;
610         struct inode *inode;
611         struct ra_io_arg *ria = &lti->lti_ria;
612         struct cl_object *clob;
613         int ret = 0;
614         __u64 kms;
615         ENTRY;
616
617         clob = io->ci_obj;
618         inode = vvp_object_inode(clob);
619
620         memset(ria, 0, sizeof *ria);
621         ret = ll_readahead_file_kms(env, io, &kms);
622         if (ret != 0)
623                 RETURN(ret);
624
625         if (kms == 0) {
626                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
627                 RETURN(0);
628         }
629
630         spin_lock(&ras->ras_lock);
631
632         /**
633          * Note: other thread might rollback the ras_next_readahead,
634          * if it can not get the full size of prepared pages, see the
635          * end of this function. For stride read ahead, it needs to
636          * make sure the offset is no less than ras_stride_offset,
637          * so that stride read ahead can work correctly.
638          */
639         if (stride_io_mode(ras))
640                 start = max(ras->ras_next_readahead, ras->ras_stride_offset);
641         else
642                 start = ras->ras_next_readahead;
643
644         if (ras->ras_window_len > 0)
645                 end = ras->ras_window_start + ras->ras_window_len - 1;
646
647         /* Enlarge the RA window to encompass the full read */
648         if (vio->vui_ra_valid &&
649             end < vio->vui_ra_start + vio->vui_ra_count - 1)
650                 end = vio->vui_ra_start + vio->vui_ra_count - 1;
651
652         if (end != 0) {
653                 unsigned long end_index;
654
655                 /* Truncate RA window to end of file */
656                 end_index = (unsigned long)((kms - 1) >> PAGE_SHIFT);
657                 if (end_index <= end) {
658                         end = end_index;
659                         ria->ria_eof = true;
660                 }
661         }
662         ria->ria_start = start;
663         ria->ria_end = end;
664         /* If stride I/O mode is detected, get stride window*/
665         if (stride_io_mode(ras)) {
666                 ria->ria_stoff = ras->ras_stride_offset;
667                 ria->ria_length = ras->ras_stride_length;
668                 ria->ria_pages = ras->ras_stride_pages;
669         }
670         spin_unlock(&ras->ras_lock);
671
672         if (end == 0) {
673                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
674                 RETURN(0);
675         }
676         len = ria_page_count(ria);
677         if (len == 0) {
678                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
679                 RETURN(0);
680         }
681
682         RAS_CDEBUG(ras);
683         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
684                PFID(lu_object_fid(&clob->co_lu)),
685                ria->ria_start, ria->ria_end,
686                vio->vui_ra_valid ? vio->vui_ra_start : 0,
687                vio->vui_ra_valid ? vio->vui_ra_count : 0,
688                hit);
689
690         /* at least to extend the readahead window to cover current read */
691         if (!hit && vio->vui_ra_valid &&
692             vio->vui_ra_start + vio->vui_ra_count > ria->ria_start)
693                 ria->ria_end_min = vio->vui_ra_start + vio->vui_ra_count - 1;
694
695         ria->ria_reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
696         if (ria->ria_reserved < len)
697                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
698
699         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
700                ria->ria_reserved, len, mlen,
701                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
702                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
703
704         ret = ll_read_ahead_pages(env, io, queue, ras, ria, &ra_end);
705
706         if (ria->ria_reserved != 0)
707                 ll_ra_count_put(ll_i2sbi(inode), ria->ria_reserved);
708
709         if (ra_end == end && ra_end == (kms >> PAGE_SHIFT))
710                 ll_ra_stats_inc(inode, RA_STAT_EOF);
711
712         CDEBUG(D_READA, "ra_end = %lu end = %lu stride end = %lu pages = %d\n",
713                ra_end, end, ria->ria_end, ret);
714
715         if (ra_end != end)
716                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
717         if (ra_end > 0) {
718                 /* update the ras so that the next read-ahead tries from
719                  * where we left off. */
720                 spin_lock(&ras->ras_lock);
721                 ras->ras_next_readahead = ra_end + 1;
722                 spin_unlock(&ras->ras_lock);
723                 RAS_CDEBUG(ras);
724         }
725
726         RETURN(ret);
727 }
728
729 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
730                           unsigned long index)
731 {
732         ras->ras_window_start = ras_align(ras, index, NULL);
733 }
734
735 /* called with the ras_lock held or from places where it doesn't matter */
736 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
737                       unsigned long index)
738 {
739         ras->ras_last_readpage = index;
740         ras->ras_consecutive_requests = 0;
741         ras->ras_consecutive_pages = 0;
742         ras->ras_window_len = 0;
743         ras_set_start(inode, ras, index);
744         ras->ras_next_readahead = max(ras->ras_window_start, index + 1);
745
746         RAS_CDEBUG(ras);
747 }
748
749 /* called with the ras_lock held or from places where it doesn't matter */
750 static void ras_stride_reset(struct ll_readahead_state *ras)
751 {
752         ras->ras_consecutive_stride_requests = 0;
753         ras->ras_stride_length = 0;
754         ras->ras_stride_pages = 0;
755         RAS_CDEBUG(ras);
756 }
757
758 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
759 {
760         spin_lock_init(&ras->ras_lock);
761         ras->ras_rpc_size = PTLRPC_MAX_BRW_PAGES;
762         ras_reset(inode, ras, 0);
763         ras->ras_requests = 0;
764 }
765
766 /*
767  * Check whether the read request is in the stride window.
768  * If it is in the stride window, return 1, otherwise return 0.
769  */
770 static int index_in_stride_window(struct ll_readahead_state *ras,
771                                   unsigned long index)
772 {
773         unsigned long stride_gap;
774
775         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
776             ras->ras_stride_pages == ras->ras_stride_length)
777                 return 0;
778
779         stride_gap = index - ras->ras_last_readpage - 1;
780
781         /* If it is contiguous read */
782         if (stride_gap == 0)
783                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
784
785         /* Otherwise check the stride by itself */
786         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
787                 ras->ras_consecutive_pages == ras->ras_stride_pages;
788 }
789
790 static void ras_update_stride_detector(struct ll_readahead_state *ras,
791                                        unsigned long index)
792 {
793         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
794
795         if (!stride_io_mode(ras) && (stride_gap != 0 ||
796              ras->ras_consecutive_stride_requests == 0)) {
797                 ras->ras_stride_pages = ras->ras_consecutive_pages;
798                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
799         }
800         LASSERT(ras->ras_request_index == 0);
801         LASSERT(ras->ras_consecutive_stride_requests == 0);
802
803         if (index <= ras->ras_last_readpage) {
804                 /*Reset stride window for forward read*/
805                 ras_stride_reset(ras);
806                 return;
807         }
808
809         ras->ras_stride_pages = ras->ras_consecutive_pages;
810         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
811
812         RAS_CDEBUG(ras);
813         return;
814 }
815
816 static unsigned long
817 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
818 {
819         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
820                                ras->ras_stride_pages, ras->ras_stride_offset,
821                                len);
822 }
823
824 /* Stride Read-ahead window will be increased inc_len according to
825  * stride I/O pattern */
826 static void ras_stride_increase_window(struct ll_readahead_state *ras,
827                                        struct ll_ra_info *ra,
828                                        unsigned long inc_len)
829 {
830         unsigned long left, step, window_len;
831         unsigned long stride_len;
832
833         LASSERT(ras->ras_stride_length > 0);
834         LASSERTF(ras->ras_window_start + ras->ras_window_len
835                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
836                  " stride_offset %lu\n", ras->ras_window_start,
837                  ras->ras_window_len, ras->ras_stride_offset);
838
839         stride_len = ras->ras_window_start + ras->ras_window_len -
840                      ras->ras_stride_offset;
841
842         left = stride_len % ras->ras_stride_length;
843         window_len = ras->ras_window_len - left;
844
845         if (left < ras->ras_stride_pages)
846                 left += inc_len;
847         else
848                 left = ras->ras_stride_pages + inc_len;
849
850         LASSERT(ras->ras_stride_pages != 0);
851
852         step = left / ras->ras_stride_pages;
853         left %= ras->ras_stride_pages;
854
855         window_len += step * ras->ras_stride_length + left;
856
857         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
858                 ras->ras_window_len = window_len;
859
860         RAS_CDEBUG(ras);
861 }
862
863 static void ras_increase_window(struct inode *inode,
864                                 struct ll_readahead_state *ras,
865                                 struct ll_ra_info *ra)
866 {
867         /* The stretch of ra-window should be aligned with max rpc_size
868          * but current clio architecture does not support retrieve such
869          * information from lower layer. FIXME later
870          */
871         if (stride_io_mode(ras)) {
872                 ras_stride_increase_window(ras, ra, ras->ras_rpc_size);
873         } else {
874                 unsigned long wlen;
875
876                 wlen = min(ras->ras_window_len + ras->ras_rpc_size,
877                            ra->ra_max_pages_per_file);
878                 if (wlen < ras->ras_rpc_size)
879                         ras->ras_window_len = wlen;
880                 else
881                         ras->ras_window_len = ras_align(ras, wlen, NULL);
882         }
883 }
884
885 static void ras_update(struct ll_sb_info *sbi, struct inode *inode,
886                        struct ll_readahead_state *ras, unsigned long index,
887                        enum ras_update_flags flags)
888 {
889         struct ll_ra_info *ra = &sbi->ll_ra_info;
890         bool hit = flags & LL_RAS_HIT;
891         int zero = 0, stride_detect = 0, ra_miss = 0;
892         ENTRY;
893
894         spin_lock(&ras->ras_lock);
895
896         if (!hit)
897                 CDEBUG(D_READA, DFID " pages at %lu miss.\n",
898                        PFID(ll_inode2fid(inode)), index);
899         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
900
901         /* reset the read-ahead window in two cases.  First when the app seeks
902          * or reads to some other part of the file.  Secondly if we get a
903          * read-ahead miss that we think we've previously issued.  This can
904          * be a symptom of there being so many read-ahead pages that the VM is
905          * reclaiming it before we get to it. */
906         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
907                 zero = 1;
908                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
909         } else if (!hit && ras->ras_window_len &&
910                    index < ras->ras_next_readahead &&
911                    index_in_window(index, ras->ras_window_start, 0,
912                                    ras->ras_window_len)) {
913                 ra_miss = 1;
914                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
915         }
916
917         /* On the second access to a file smaller than the tunable
918          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
919          * file up to ra_max_pages_per_file.  This is simply a best effort
920          * and only occurs once per open file.  Normal RA behavior is reverted
921          * to for subsequent IO.  The mmap case does not increment
922          * ras_requests and thus can never trigger this behavior. */
923         if (ras->ras_requests >= 2 && !ras->ras_request_index) {
924                 __u64 kms_pages;
925
926                 kms_pages = (i_size_read(inode) + PAGE_SIZE - 1) >>
927                             PAGE_SHIFT;
928
929                 CDEBUG(D_READA, "kmsp %llu mwp %lu mp %lu\n", kms_pages,
930                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
931
932                 if (kms_pages &&
933                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
934                         ras->ras_window_start = 0;
935                         ras->ras_next_readahead = index + 1;
936                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
937                                 ra->ra_max_read_ahead_whole_pages);
938                         GOTO(out_unlock, 0);
939                 }
940         }
941         if (zero) {
942                 /* check whether it is in stride I/O mode*/
943                 if (!index_in_stride_window(ras, index)) {
944                         if (ras->ras_consecutive_stride_requests == 0 &&
945                             ras->ras_request_index == 0) {
946                                 ras_update_stride_detector(ras, index);
947                                 ras->ras_consecutive_stride_requests++;
948                         } else {
949                                 ras_stride_reset(ras);
950                         }
951                         ras_reset(inode, ras, index);
952                         ras->ras_consecutive_pages++;
953                         GOTO(out_unlock, 0);
954                 } else {
955                         ras->ras_consecutive_pages = 0;
956                         ras->ras_consecutive_requests = 0;
957                         if (++ras->ras_consecutive_stride_requests > 1)
958                                 stride_detect = 1;
959                         RAS_CDEBUG(ras);
960                 }
961         } else {
962                 if (ra_miss) {
963                         if (index_in_stride_window(ras, index) &&
964                             stride_io_mode(ras)) {
965                                 if (index != ras->ras_last_readpage + 1)
966                                         ras->ras_consecutive_pages = 0;
967                                 ras_reset(inode, ras, index);
968
969                                 /* If stride-RA hit cache miss, the stride
970                                  * detector will not be reset to avoid the
971                                  * overhead of redetecting read-ahead mode,
972                                  * but on the condition that the stride window
973                                  * is still intersect with normal sequential
974                                  * read-ahead window. */
975                                 if (ras->ras_window_start <
976                                     ras->ras_stride_offset)
977                                         ras_stride_reset(ras);
978                                 RAS_CDEBUG(ras);
979                         } else {
980                                 /* Reset both stride window and normal RA
981                                  * window */
982                                 ras_reset(inode, ras, index);
983                                 ras->ras_consecutive_pages++;
984                                 ras_stride_reset(ras);
985                                 GOTO(out_unlock, 0);
986                         }
987                 } else if (stride_io_mode(ras)) {
988                         /* If this is contiguous read but in stride I/O mode
989                          * currently, check whether stride step still is valid,
990                          * if invalid, it will reset the stride ra window*/
991                         if (!index_in_stride_window(ras, index)) {
992                                 /* Shrink stride read-ahead window to be zero */
993                                 ras_stride_reset(ras);
994                                 ras->ras_window_len = 0;
995                                 ras->ras_next_readahead = index;
996                         }
997                 }
998         }
999         ras->ras_consecutive_pages++;
1000         ras->ras_last_readpage = index;
1001         ras_set_start(inode, ras, index);
1002
1003         if (stride_io_mode(ras)) {
1004                 /* Since stride readahead is sentivite to the offset
1005                  * of read-ahead, so we use original offset here,
1006                  * instead of ras_window_start, which is RPC aligned */
1007                 ras->ras_next_readahead = max(index + 1,
1008                                               ras->ras_next_readahead);
1009                 ras->ras_window_start = max(ras->ras_stride_offset,
1010                                             ras->ras_window_start);
1011         } else {
1012                 if (ras->ras_next_readahead < ras->ras_window_start)
1013                         ras->ras_next_readahead = ras->ras_window_start;
1014                 if (!hit)
1015                         ras->ras_next_readahead = index + 1;
1016         }
1017         RAS_CDEBUG(ras);
1018
1019         /* Trigger RA in the mmap case where ras_consecutive_requests
1020          * is not incremented and thus can't be used to trigger RA */
1021         if (ras->ras_consecutive_pages >= 4 && flags & LL_RAS_MMAP) {
1022                 ras_increase_window(inode, ras, ra);
1023                 /* reset consecutive pages so that the readahead window can
1024                  * grow gradually. */
1025                 ras->ras_consecutive_pages = 0;
1026                 GOTO(out_unlock, 0);
1027         }
1028
1029         /* Initially reset the stride window offset to next_readahead*/
1030         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1031                 /**
1032                  * Once stride IO mode is detected, next_readahead should be
1033                  * reset to make sure next_readahead > stride offset
1034                  */
1035                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1036                 ras->ras_stride_offset = index;
1037                 ras->ras_window_start = max(index, ras->ras_window_start);
1038         }
1039
1040         /* The initial ras_window_len is set to the request size.  To avoid
1041          * uselessly reading and discarding pages for random IO the window is
1042          * only increased once per consecutive request received. */
1043         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1044             !ras->ras_request_index)
1045                 ras_increase_window(inode, ras, ra);
1046         EXIT;
1047 out_unlock:
1048         RAS_CDEBUG(ras);
1049         ras->ras_request_index++;
1050         spin_unlock(&ras->ras_lock);
1051         return;
1052 }
1053
1054 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1055 {
1056         struct inode           *inode = vmpage->mapping->host;
1057         struct ll_inode_info   *lli   = ll_i2info(inode);
1058         struct lu_env          *env;
1059         struct cl_io           *io;
1060         struct cl_page         *page;
1061         struct cl_object       *clob;
1062         bool redirtied = false;
1063         bool unlocked = false;
1064         int result;
1065         __u16 refcheck;
1066         ENTRY;
1067
1068         LASSERT(PageLocked(vmpage));
1069         LASSERT(!PageWriteback(vmpage));
1070
1071         LASSERT(ll_i2dtexp(inode) != NULL);
1072
1073         env = cl_env_get(&refcheck);
1074         if (IS_ERR(env))
1075                 GOTO(out, result = PTR_ERR(env));
1076
1077         clob  = ll_i2info(inode)->lli_clob;
1078         LASSERT(clob != NULL);
1079
1080         io = vvp_env_thread_io(env);
1081         io->ci_obj = clob;
1082         io->ci_ignore_layout = 1;
1083         result = cl_io_init(env, io, CIT_MISC, clob);
1084         if (result == 0) {
1085                 page = cl_page_find(env, clob, vmpage->index,
1086                                     vmpage, CPT_CACHEABLE);
1087                 if (!IS_ERR(page)) {
1088                         lu_ref_add(&page->cp_reference, "writepage",
1089                                    current);
1090                         cl_page_assume(env, io, page);
1091                         result = cl_page_flush(env, io, page);
1092                         if (result != 0) {
1093                                 /*
1094                                  * Re-dirty page on error so it retries write,
1095                                  * but not in case when IO has actually
1096                                  * occurred and completed with an error.
1097                                  */
1098                                 if (!PageError(vmpage)) {
1099                                         redirty_page_for_writepage(wbc, vmpage);
1100                                         result = 0;
1101                                         redirtied = true;
1102                                 }
1103                         }
1104                         cl_page_disown(env, io, page);
1105                         unlocked = true;
1106                         lu_ref_del(&page->cp_reference,
1107                                    "writepage", current);
1108                         cl_page_put(env, page);
1109                 } else {
1110                         result = PTR_ERR(page);
1111                 }
1112         }
1113         cl_io_fini(env, io);
1114
1115         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1116                 loff_t offset = cl_offset(clob, vmpage->index);
1117
1118                 /* Flush page failed because the extent is being written out.
1119                  * Wait for the write of extent to be finished to avoid
1120                  * breaking kernel which assumes ->writepage should mark
1121                  * PageWriteback or clean the page. */
1122                 result = cl_sync_file_range(inode, offset,
1123                                             offset + PAGE_SIZE - 1,
1124                                             CL_FSYNC_LOCAL, 1);
1125                 if (result > 0) {
1126                         /* actually we may have written more than one page.
1127                          * decreasing this page because the caller will count
1128                          * it. */
1129                         wbc->nr_to_write -= result - 1;
1130                         result = 0;
1131                 }
1132         }
1133
1134         cl_env_put(env, &refcheck);
1135         GOTO(out, result);
1136
1137 out:
1138         if (result < 0) {
1139                 if (!lli->lli_async_rc)
1140                         lli->lli_async_rc = result;
1141                 SetPageError(vmpage);
1142                 if (!unlocked)
1143                         unlock_page(vmpage);
1144         }
1145         return result;
1146 }
1147
1148 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1149 {
1150         struct inode *inode = mapping->host;
1151         loff_t start;
1152         loff_t end;
1153         enum cl_fsync_mode mode;
1154         int range_whole = 0;
1155         int result;
1156         ENTRY;
1157
1158         if (wbc->range_cyclic) {
1159                 start = mapping->writeback_index << PAGE_SHIFT;
1160                 end = OBD_OBJECT_EOF;
1161         } else {
1162                 start = wbc->range_start;
1163                 end = wbc->range_end;
1164                 if (end == LLONG_MAX) {
1165                         end = OBD_OBJECT_EOF;
1166                         range_whole = start == 0;
1167                 }
1168         }
1169
1170         mode = CL_FSYNC_NONE;
1171         if (wbc->sync_mode == WB_SYNC_ALL)
1172                 mode = CL_FSYNC_LOCAL;
1173
1174         if (ll_i2info(inode)->lli_clob == NULL)
1175                 RETURN(0);
1176
1177         /* for directio, it would call writepages() to evict cached pages
1178          * inside the IO context of write, which will cause deadlock at
1179          * layout_conf since it waits for active IOs to complete. */
1180         result = cl_sync_file_range(inode, start, end, mode, 1);
1181         if (result > 0) {
1182                 wbc->nr_to_write -= result;
1183                 result = 0;
1184          }
1185
1186         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1187                 if (end == OBD_OBJECT_EOF)
1188                         mapping->writeback_index = 0;
1189                 else
1190                         mapping->writeback_index = (end >> PAGE_SHIFT) + 1;
1191         }
1192         RETURN(result);
1193 }
1194
1195 struct ll_cl_context *ll_cl_find(struct file *file)
1196 {
1197         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1198         struct ll_cl_context *lcc;
1199         struct ll_cl_context *found = NULL;
1200
1201         read_lock(&fd->fd_lock);
1202         list_for_each_entry(lcc, &fd->fd_lccs, lcc_list) {
1203                 if (lcc->lcc_cookie == current) {
1204                         found = lcc;
1205                         break;
1206                 }
1207         }
1208         read_unlock(&fd->fd_lock);
1209
1210         return found;
1211 }
1212
1213 void ll_cl_add(struct file *file, const struct lu_env *env, struct cl_io *io,
1214                enum lcc_type type)
1215 {
1216         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1217         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1218
1219         memset(lcc, 0, sizeof(*lcc));
1220         INIT_LIST_HEAD(&lcc->lcc_list);
1221         lcc->lcc_cookie = current;
1222         lcc->lcc_env = env;
1223         lcc->lcc_io = io;
1224         lcc->lcc_type = type;
1225
1226         write_lock(&fd->fd_lock);
1227         list_add(&lcc->lcc_list, &fd->fd_lccs);
1228         write_unlock(&fd->fd_lock);
1229 }
1230
1231 void ll_cl_remove(struct file *file, const struct lu_env *env)
1232 {
1233         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1234         struct ll_cl_context *lcc = &ll_env_info(env)->lti_io_ctx;
1235
1236         write_lock(&fd->fd_lock);
1237         list_del_init(&lcc->lcc_list);
1238         write_unlock(&fd->fd_lock);
1239 }
1240
1241 int ll_io_read_page(const struct lu_env *env, struct cl_io *io,
1242                            struct cl_page *page, struct file *file)
1243 {
1244         struct inode              *inode  = vvp_object_inode(page->cp_obj);
1245         struct ll_sb_info         *sbi    = ll_i2sbi(inode);
1246         struct ll_file_data       *fd     = LUSTRE_FPRIVATE(file);
1247         struct ll_readahead_state *ras    = &fd->fd_ras;
1248         struct cl_2queue          *queue  = &io->ci_queue;
1249         struct cl_sync_io         *anchor = NULL;
1250         struct vvp_page           *vpg;
1251         int                        rc = 0;
1252         bool                       uptodate;
1253         ENTRY;
1254
1255         vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1256         uptodate = vpg->vpg_defer_uptodate;
1257
1258         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1259             sbi->ll_ra_info.ra_max_pages > 0 &&
1260             !vpg->vpg_ra_updated) {
1261                 struct vvp_io *vio = vvp_env_io(env);
1262                 enum ras_update_flags flags = 0;
1263
1264                 if (uptodate)
1265                         flags |= LL_RAS_HIT;
1266                 if (!vio->vui_ra_valid)
1267                         flags |= LL_RAS_MMAP;
1268                 ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1269         }
1270
1271         cl_2queue_init(queue);
1272         if (uptodate) {
1273                 vpg->vpg_ra_used = 1;
1274                 cl_page_export(env, page, 1);
1275                 cl_page_disown(env, io, page);
1276         } else {
1277                 anchor = &vvp_env_info(env)->vti_anchor;
1278                 cl_sync_io_init(anchor, 1);
1279                 page->cp_sync_io = anchor;
1280
1281                 cl_2queue_add(queue, page);
1282         }
1283
1284         if (sbi->ll_ra_info.ra_max_pages_per_file > 0 &&
1285             sbi->ll_ra_info.ra_max_pages > 0) {
1286                 int rc2;
1287
1288                 rc2 = ll_readahead(env, io, &queue->c2_qin, ras,
1289                                    uptodate, file);
1290                 CDEBUG(D_READA, DFID "%d pages read ahead at %lu\n",
1291                        PFID(ll_inode2fid(inode)), rc2, vvp_index(vpg));
1292         }
1293
1294         if (queue->c2_qin.pl_nr > 0) {
1295                 int count = queue->c2_qin.pl_nr;
1296                 rc = cl_io_submit_rw(env, io, CRT_READ, queue);
1297                 if (rc == 0)
1298                         task_io_account_read(PAGE_SIZE * count);
1299         }
1300
1301
1302         if (anchor != NULL && !cl_page_is_owned(page, io)) { /* have sent */
1303                 rc = cl_sync_io_wait(env, anchor, 0);
1304
1305                 cl_page_assume(env, io, page);
1306                 cl_page_list_del(env, &queue->c2_qout, page);
1307
1308                 if (!PageUptodate(cl_page_vmpage(page))) {
1309                         /* Failed to read a mirror, discard this page so that
1310                          * new page can be created with new mirror.
1311                          *
1312                          * TODO: this is not needed after page reinit
1313                          * route is implemented */
1314                         cl_page_discard(env, io, page);
1315                 }
1316                 cl_page_disown(env, io, page);
1317         }
1318
1319         /* TODO: discard all pages until page reinit route is implemented */
1320         cl_page_list_discard(env, io, &queue->c2_qin);
1321
1322         /* Unlock unsent read pages in case of error. */
1323         cl_page_list_disown(env, io, &queue->c2_qin);
1324
1325         cl_2queue_fini(env, queue);
1326
1327         RETURN(rc);
1328 }
1329
1330 /*
1331  * Possible return value:
1332  * 0 no async readahead triggered and fast read could not be used.
1333  * 1 no async readahead, but fast read could be used.
1334  * 2 async readahead triggered and fast read could be used too.
1335  * < 0 on error.
1336  */
1337 static int kickoff_async_readahead(struct file *file, unsigned long pages)
1338 {
1339         struct ll_readahead_work *lrw;
1340         struct inode *inode = file_inode(file);
1341         struct ll_sb_info *sbi = ll_i2sbi(inode);
1342         struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1343         struct ll_readahead_state *ras = &fd->fd_ras;
1344         struct ll_ra_info *ra = &sbi->ll_ra_info;
1345         unsigned long throttle;
1346         unsigned long start = ras_align(ras, ras->ras_next_readahead, NULL);
1347         unsigned long end = start + pages - 1;
1348
1349         throttle = min(ra->ra_async_pages_per_file_threshold,
1350                        ra->ra_max_pages_per_file);
1351         /*
1352          * If this is strided i/o or the window is smaller than the
1353          * throttle limit, we do not do async readahead. Otherwise,
1354          * we do async readahead, allowing the user thread to do fast i/o.
1355          */
1356         if (stride_io_mode(ras) || !throttle ||
1357             ras->ras_window_len < throttle)
1358                 return 0;
1359
1360         if ((atomic_read(&ra->ra_cur_pages) + pages) > ra->ra_max_pages)
1361                 return 0;
1362
1363         if (ras->ras_async_last_readpage == start)
1364                 return 1;
1365
1366         /* ll_readahead_work_free() free it */
1367         OBD_ALLOC_PTR(lrw);
1368         if (lrw) {
1369                 lrw->lrw_file = get_file(file);
1370                 lrw->lrw_start = start;
1371                 lrw->lrw_end = end;
1372                 spin_lock(&ras->ras_lock);
1373                 ras->ras_next_readahead = end + 1;
1374                 ras->ras_async_last_readpage = start;
1375                 spin_unlock(&ras->ras_lock);
1376                 ll_readahead_work_add(inode, lrw);
1377         } else {
1378                 return -ENOMEM;
1379         }
1380
1381         return 2;
1382 }
1383
1384 int ll_readpage(struct file *file, struct page *vmpage)
1385 {
1386         struct inode *inode = file_inode(file);
1387         struct cl_object *clob = ll_i2info(inode)->lli_clob;
1388         struct ll_cl_context *lcc;
1389         const struct lu_env  *env = NULL;
1390         struct cl_io   *io = NULL;
1391         struct cl_page *page;
1392         struct ll_sb_info *sbi = ll_i2sbi(inode);
1393         int result;
1394         ENTRY;
1395
1396         lcc = ll_cl_find(file);
1397         if (lcc != NULL) {
1398                 env = lcc->lcc_env;
1399                 io  = lcc->lcc_io;
1400         }
1401
1402         if (io == NULL) { /* fast read */
1403                 struct inode *inode = file_inode(file);
1404                 struct ll_file_data *fd = LUSTRE_FPRIVATE(file);
1405                 struct ll_readahead_state *ras = &fd->fd_ras;
1406                 struct lu_env  *local_env = NULL;
1407                 unsigned long fast_read_pages =
1408                         max(RA_REMAIN_WINDOW_MIN, ras->ras_rpc_size);
1409                 struct vvp_page *vpg;
1410
1411                 result = -ENODATA;
1412
1413                 /* TODO: need to verify the layout version to make sure
1414                  * the page is not invalid due to layout change. */
1415                 page = cl_vmpage_page(vmpage, clob);
1416                 if (page == NULL) {
1417                         unlock_page(vmpage);
1418                         ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1419                         RETURN(result);
1420                 }
1421
1422                 vpg = cl2vvp_page(cl_object_page_slice(page->cp_obj, page));
1423                 if (vpg->vpg_defer_uptodate) {
1424                         enum ras_update_flags flags = LL_RAS_HIT;
1425
1426                         if (lcc && lcc->lcc_type == LCC_MMAP)
1427                                 flags |= LL_RAS_MMAP;
1428
1429                         /* For fast read, it updates read ahead state only
1430                          * if the page is hit in cache because non cache page
1431                          * case will be handled by slow read later. */
1432                         ras_update(sbi, inode, ras, vvp_index(vpg), flags);
1433                         /* avoid duplicate ras_update() call */
1434                         vpg->vpg_ra_updated = 1;
1435
1436                         /* Check if we can issue a readahead RPC, if that is
1437                          * the case, we can't do fast IO because we will need
1438                          * a cl_io to issue the RPC. */
1439                         if (ras->ras_window_start + ras->ras_window_len <
1440                             ras->ras_next_readahead + fast_read_pages ||
1441                             kickoff_async_readahead(file, fast_read_pages) > 0)
1442                                 result = 0;
1443                 }
1444
1445                 if (!env) {
1446                         local_env = cl_env_percpu_get();
1447                         env = local_env;
1448                 }
1449
1450                 /* export the page and skip io stack */
1451                 if (result == 0) {
1452                         vpg->vpg_ra_used = 1;
1453                         cl_page_export(env, page, 1);
1454                 } else {
1455                         ll_ra_stats_inc_sbi(sbi, RA_STAT_FAILED_FAST_READ);
1456                 }
1457                 /* release page refcount before unlocking the page to ensure
1458                  * the object won't be destroyed in the calling path of
1459                  * cl_page_put(). Please see comment in ll_releasepage(). */
1460                 cl_page_put(env, page);
1461                 unlock_page(vmpage);
1462                 if (local_env)
1463                         cl_env_percpu_put(local_env);
1464
1465                 RETURN(result);
1466         }
1467
1468         LASSERT(io->ci_state == CIS_IO_GOING);
1469         page = cl_page_find(env, clob, vmpage->index, vmpage, CPT_CACHEABLE);
1470         if (!IS_ERR(page)) {
1471                 LASSERT(page->cp_type == CPT_CACHEABLE);
1472                 if (likely(!PageUptodate(vmpage))) {
1473                         cl_page_assume(env, io, page);
1474
1475                         result = ll_io_read_page(env, io, page, file);
1476                 } else {
1477                         /* Page from a non-object file. */
1478                         unlock_page(vmpage);
1479                         result = 0;
1480                 }
1481                 cl_page_put(env, page);
1482         } else {
1483                 unlock_page(vmpage);
1484                 result = PTR_ERR(page);
1485         }
1486         RETURN(result);
1487 }