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