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