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