Whamcloud - gitweb
LU-5010 llite: disable readahead optimization
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/llite/rw.c
37  *
38  * Lustre Lite I/O page cache routines shared by different kernel revs
39  */
40
41 #include <linux/kernel.h>
42 #include <linux/mm.h>
43 #include <linux/string.h>
44 #include <linux/stat.h>
45 #include <linux/errno.h>
46 #include <linux/unistd.h>
47 #include <linux/writeback.h>
48 #include <asm/uaccess.h>
49
50 #include <linux/fs.h>
51 #include <linux/stat.h>
52 #include <asm/uaccess.h>
53 #include <linux/mm.h>
54 #include <linux/pagemap.h>
55 /* current_is_kswapd() */
56 #include <linux/swap.h>
57
58 #define DEBUG_SUBSYSTEM S_LLITE
59
60 #include <lustre_lite.h>
61 #include <obd_cksum.h>
62 #include "llite_internal.h"
63 #include <linux/lustre_compat25.h>
64
65 /**
66  * Finalizes cl-data before exiting typical address_space operation. Dual to
67  * ll_cl_init().
68  */
69 void ll_cl_fini(struct ll_cl_context *lcc)
70 {
71         struct lu_env  *env  = lcc->lcc_env;
72         struct cl_io   *io   = lcc->lcc_io;
73         struct cl_page *page = lcc->lcc_page;
74
75         LASSERT(lcc->lcc_cookie == current);
76         LASSERT(env != NULL);
77
78         if (page != NULL) {
79                 lu_ref_del(&page->cp_reference, "cl_io", io);
80                 cl_page_put(env, page);
81         }
82
83         cl_env_put(env, &lcc->lcc_refcheck);
84 }
85
86 /**
87  * Initializes common cl-data at the typical address_space operation entry
88  * point.
89  */
90 struct ll_cl_context *ll_cl_init(struct file *file, struct page *vmpage)
91 {
92         struct ll_cl_context *lcc;
93         struct lu_env    *env;
94         struct cl_io     *io;
95         struct cl_object *clob;
96         struct ccc_io    *cio;
97
98         int refcheck;
99         int result = 0;
100
101         clob = ll_i2info(file->f_dentry->d_inode)->lli_clob;
102         LASSERT(clob != NULL);
103
104         env = cl_env_get(&refcheck);
105         if (IS_ERR(env))
106                 return ERR_PTR(PTR_ERR(env));
107
108         lcc = &vvp_env_info(env)->vti_io_ctx;
109         memset(lcc, 0, sizeof(*lcc));
110         lcc->lcc_env = env;
111         lcc->lcc_refcheck = refcheck;
112         lcc->lcc_cookie = current;
113
114         cio = ccc_env_io(env);
115         io = cio->cui_cl.cis_io;
116         lcc->lcc_io = io;
117         if (io == NULL)
118                 result = -EIO;
119         if (result == 0 && vmpage != NULL) {
120                 struct cl_page   *page;
121
122                 LASSERT(io != NULL);
123                 LASSERT(io->ci_state == CIS_IO_GOING);
124                 LASSERT(cio->cui_fd == LUSTRE_FPRIVATE(file));
125                 page = cl_page_find(env, clob, vmpage->index, vmpage,
126                                     CPT_CACHEABLE);
127                 if (!IS_ERR(page)) {
128                         lcc->lcc_page = page;
129                         lu_ref_add(&page->cp_reference, "cl_io", io);
130                         result = 0;
131                 } else
132                         result = PTR_ERR(page);
133         }
134         if (result) {
135                 ll_cl_fini(lcc);
136                 lcc = ERR_PTR(result);
137         }
138
139         return lcc;
140 }
141
142 struct obd_capa *cl_capa_lookup(struct inode *inode, enum cl_req_type crt)
143 {
144         __u64 opc;
145
146         opc = crt == CRT_WRITE ? CAPA_OPC_OSS_WRITE : CAPA_OPC_OSS_RW;
147         return ll_osscapa_get(inode, opc);
148 }
149
150 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which);
151
152 /**
153  * Get readahead pages from the filesystem readahead pool of the client for a
154  * thread.
155  *
156  * /param sbi superblock for filesystem readahead state ll_ra_info
157  * /param ria per-thread readahead state
158  * /param pages number of pages requested for readahead for the thread.
159  *
160  * WARNING: This algorithm is used to reduce contention on sbi->ll_lock.
161  * It should work well if the ra_max_pages is much greater than the single
162  * file's read-ahead window, and not too many threads contending for
163  * these readahead pages.
164  *
165  * TODO: There may be a 'global sync problem' if many threads are trying
166  * to get an ra budget that is larger than the remaining readahead pages
167  * and reach here at exactly the same time. They will compute /a ret to
168  * consume the remaining pages, but will fail at atomic_add_return() and
169  * get a zero ra window, although there is still ra space remaining. - Jay */
170
171 static unsigned long ll_ra_count_get(struct ll_sb_info *sbi,
172                                      struct ra_io_arg *ria,
173                                      unsigned long pages, unsigned long min)
174 {
175         struct ll_ra_info *ra = &sbi->ll_ra_info;
176         long ret;
177         ENTRY;
178
179         /* If read-ahead pages left are less than 1M, do not do read-ahead,
180          * otherwise it will form small read RPC(< 1M), which hurt server
181          * performance a lot. */
182         ret = min(ra->ra_max_pages - atomic_read(&ra->ra_cur_pages), pages);
183         if (ret < 0 || ret < min_t(long, PTLRPC_MAX_BRW_PAGES, pages))
184                 GOTO(out, ret = 0);
185
186         /* If the non-strided (ria_pages == 0) readahead window
187          * (ria_start + ret) has grown across an RPC boundary, then trim
188          * readahead size by the amount beyond the RPC so it ends on an
189          * RPC boundary. If the readahead window is already ending on
190          * an RPC boundary (beyond_rpc == 0), or smaller than a full
191          * RPC (beyond_rpc < ret) the readahead size is unchanged.
192          * The (beyond_rpc != 0) check is skipped since the conditional
193          * branch is more expensive than subtracting zero from the result.
194          *
195          * Strided read is left unaligned to avoid small fragments beyond
196          * the RPC boundary from needing an extra read RPC. */
197         if (ria->ria_pages == 0) {
198                 long beyond_rpc = (ria->ria_start + ret) % PTLRPC_MAX_BRW_PAGES;
199                 if (/* beyond_rpc != 0 && */ beyond_rpc < ret)
200                         ret -= beyond_rpc;
201         }
202
203         if (atomic_add_return(ret, &ra->ra_cur_pages) > ra->ra_max_pages) {
204                 atomic_sub(ret, &ra->ra_cur_pages);
205                 ret = 0;
206         }
207
208 out:
209         if (ret < min) {
210                 /* override ra limit for maximum performance */
211                 atomic_add(min - ret, &ra->ra_cur_pages);
212                 ret = min;
213         }
214         RETURN(ret);
215 }
216
217 void ll_ra_count_put(struct ll_sb_info *sbi, unsigned long len)
218 {
219         struct ll_ra_info *ra = &sbi->ll_ra_info;
220         atomic_sub(len, &ra->ra_cur_pages);
221 }
222
223 static void ll_ra_stats_inc_sbi(struct ll_sb_info *sbi, enum ra_stat which)
224 {
225         LASSERTF(which >= 0 && which < _NR_RA_STAT, "which: %u\n", which);
226         lprocfs_counter_incr(sbi->ll_ra_stats, which);
227 }
228
229 void ll_ra_stats_inc(struct inode *inode, enum ra_stat which)
230 {
231         struct ll_sb_info *sbi = ll_i2sbi(inode);
232         ll_ra_stats_inc_sbi(sbi, which);
233 }
234
235 #define RAS_CDEBUG(ras) \
236         CDEBUG(D_READA,                                                      \
237                "lrp %lu cr %lu cp %lu ws %lu wl %lu nra %lu r %lu ri %lu"    \
238                "csr %lu sf %lu sp %lu sl %lu \n",                            \
239                ras->ras_last_readpage, ras->ras_consecutive_requests,        \
240                ras->ras_consecutive_pages, ras->ras_window_start,            \
241                ras->ras_window_len, ras->ras_next_readahead,                 \
242                ras->ras_requests, ras->ras_request_index,                    \
243                ras->ras_consecutive_stride_requests, ras->ras_stride_offset, \
244                ras->ras_stride_pages, ras->ras_stride_length)
245
246 static int index_in_window(unsigned long index, unsigned long point,
247                            unsigned long before, unsigned long after)
248 {
249         unsigned long start = point - before, end = point + after;
250
251         if (start > point)
252                start = 0;
253         if (end < point)
254                end = ~0;
255
256         return start <= index && index <= end;
257 }
258
259 static struct ll_readahead_state *ll_ras_get(struct file *f)
260 {
261         struct ll_file_data       *fd;
262
263         fd = LUSTRE_FPRIVATE(f);
264         return &fd->fd_ras;
265 }
266
267 void ll_ra_read_in(struct file *f, struct ll_ra_read *rar)
268 {
269         struct ll_readahead_state *ras;
270
271         ras = ll_ras_get(f);
272
273         spin_lock(&ras->ras_lock);
274         ras->ras_requests++;
275         ras->ras_request_index = 0;
276         ras->ras_consecutive_requests++;
277         rar->lrr_reader = current;
278
279         list_add(&rar->lrr_linkage, &ras->ras_read_beads);
280         spin_unlock(&ras->ras_lock);
281 }
282
283 void ll_ra_read_ex(struct file *f, struct ll_ra_read *rar)
284 {
285         struct ll_readahead_state *ras;
286
287         ras = ll_ras_get(f);
288
289         spin_lock(&ras->ras_lock);
290         list_del_init(&rar->lrr_linkage);
291         spin_unlock(&ras->ras_lock);
292 }
293
294 static struct ll_ra_read *ll_ra_read_get_locked(struct ll_readahead_state *ras)
295 {
296         struct ll_ra_read *scan;
297
298         list_for_each_entry(scan, &ras->ras_read_beads, lrr_linkage) {
299                 if (scan->lrr_reader == current)
300                         return scan;
301         }
302         return NULL;
303 }
304
305 struct ll_ra_read *ll_ra_read_get(struct file *f)
306 {
307         struct ll_readahead_state *ras;
308         struct ll_ra_read         *bead;
309
310         ras = ll_ras_get(f);
311
312         spin_lock(&ras->ras_lock);
313         bead = ll_ra_read_get_locked(ras);
314         spin_unlock(&ras->ras_lock);
315         return bead;
316 }
317
318 static int cl_read_ahead_page(const struct lu_env *env, struct cl_io *io,
319                               struct cl_page_list *queue, struct cl_page *page,
320                               struct cl_object *clob, pgoff_t *max_index)
321 {
322         struct page *vmpage = page->cp_vmpage;
323         struct ccc_page *cp;
324         int              rc;
325
326         ENTRY;
327
328         rc = 0;
329         cl_page_assume(env, io, page);
330         lu_ref_add(&page->cp_reference, "ra", current);
331         cp = cl2ccc_page(cl_object_page_slice(clob, page));
332         if (!cp->cpg_defer_uptodate && !PageUptodate(vmpage)) {
333                 CDEBUG(D_READA, "page index %lu, max_index: %lu\n",
334                        ccc_index(cp), *max_index);
335                 /* Disable the optimization on prefetching maximum readahead
336                  * index because there is a race with lock cancellation. This
337                  * optimization will be revived later.
338                  * if (*max_index == 0 || ccc_index(cp) > *max_index) */
339                 rc = cl_page_is_under_lock(env, io, page, max_index);
340                 if (rc == 0) {
341                         cp->cpg_defer_uptodate = 1;
342                         cp->cpg_ra_used = 0;
343                         cl_page_list_add(queue, page);
344                         rc = 1;
345                 } else {
346                         cl_page_discard(env, io, page);
347                         rc = -ENOLCK;
348                 }
349         } else {
350                 /* skip completed pages */
351                 cl_page_unassume(env, io, page);
352         }
353         lu_ref_del(&page->cp_reference, "ra", current);
354         cl_page_put(env, page);
355         RETURN(rc);
356 }
357
358 /**
359  * Initiates read-ahead of a page with given index.
360  *
361  * \retval     +ve: page was added to \a queue.
362  *
363  * \retval -ENOLCK: there is no extent lock for this part of a file, stop
364  *                  read-ahead.
365  *
366  * \retval  -ve, 0: page wasn't added to \a queue for other reason.
367  */
368 static int ll_read_ahead_page(const struct lu_env *env, struct cl_io *io,
369                               struct cl_page_list *queue,
370                               pgoff_t index, pgoff_t *max_index)
371 {
372         struct cl_object *clob  = io->ci_obj;
373         struct inode     *inode = ccc_object_inode(clob);
374         struct page      *vmpage;
375         struct cl_page   *page;
376         enum ra_stat      which = _NR_RA_STAT; /* keep gcc happy */
377         unsigned int      gfp_mask;
378         int               rc    = 0;
379         const char       *msg   = NULL;
380
381         ENTRY;
382
383         gfp_mask = GFP_HIGHUSER & ~__GFP_WAIT;
384 #ifdef __GFP_NOWARN
385         gfp_mask |= __GFP_NOWARN;
386 #endif
387         vmpage = grab_cache_page_nowait(inode->i_mapping, index);
388         if (vmpage != NULL) {
389                 /* Check if vmpage was truncated or reclaimed */
390                 if (vmpage->mapping == inode->i_mapping) {
391                         page = cl_page_find(env, clob, vmpage->index,
392                                             vmpage, CPT_CACHEABLE);
393                         if (!IS_ERR(page)) {
394                                 rc = cl_read_ahead_page(env, io, queue,
395                                                         page, clob, max_index);
396                                 if (rc == -ENOLCK) {
397                                         which = RA_STAT_FAILED_MATCH;
398                                         msg   = "lock match failed";
399                                 }
400                         } else {
401                                 which = RA_STAT_FAILED_GRAB_PAGE;
402                                 msg   = "cl_page_find failed";
403                         }
404                 } else {
405                         which = RA_STAT_WRONG_GRAB_PAGE;
406                         msg   = "g_c_p_n returned invalid page";
407                 }
408                 if (rc != 1)
409                         unlock_page(vmpage);
410                 page_cache_release(vmpage);
411         } else {
412                 which = RA_STAT_FAILED_GRAB_PAGE;
413                 msg   = "g_c_p_n failed";
414         }
415         if (msg != NULL) {
416                 ll_ra_stats_inc(inode, which);
417                 CDEBUG(D_READA, "%s\n", msg);
418         }
419         RETURN(rc);
420 }
421
422 #define RIA_DEBUG(ria)                                                       \
423         CDEBUG(D_READA, "rs %lu re %lu ro %lu rl %lu rp %lu\n",       \
424         ria->ria_start, ria->ria_end, ria->ria_stoff, ria->ria_length,\
425         ria->ria_pages)
426
427 /* Limit this to the blocksize instead of PTLRPC_BRW_MAX_SIZE, since we don't
428  * know what the actual RPC size is.  If this needs to change, it makes more
429  * sense to tune the i_blkbits value for the file based on the OSTs it is
430  * striped over, rather than having a constant value for all files here. */
431
432 /* RAS_INCREASE_STEP should be (1UL << (inode->i_blkbits - PAGE_CACHE_SHIFT)).
433  * Temprarily set RAS_INCREASE_STEP to 1MB. After 4MB RPC is enabled
434  * by default, this should be adjusted corresponding with max_read_ahead_mb
435  * and max_read_ahead_per_file_mb otherwise the readahead budget can be used
436  * up quickly which will affect read performance siginificantly. See LU-2816 */
437 #define RAS_INCREASE_STEP(inode) (ONE_MB_BRW_SIZE >> PAGE_CACHE_SHIFT)
438
439 static inline int stride_io_mode(struct ll_readahead_state *ras)
440 {
441         return ras->ras_consecutive_stride_requests > 1;
442 }
443 /* The function calculates how much pages will be read in
444  * [off, off + length], in such stride IO area,
445  * stride_offset = st_off, stride_lengh = st_len,
446  * stride_pages = st_pgs
447  *
448  *   |------------------|*****|------------------|*****|------------|*****|....
449  * st_off
450  *   |--- st_pgs     ---|
451  *   |-----     st_len   -----|
452  *
453  *              How many pages it should read in such pattern
454  *              |-------------------------------------------------------------|
455  *              off
456  *              |<------                  length                      ------->|
457  *
458  *          =   |<----->|  +  |-------------------------------------| +   |---|
459  *             start_left                 st_pgs * i                    end_left
460  */
461 static unsigned long
462 stride_pg_count(pgoff_t st_off, unsigned long st_len, unsigned long st_pgs,
463                 unsigned long off, unsigned long length)
464 {
465         __u64 start = off > st_off ? off - st_off : 0;
466         __u64 end = off + length > st_off ? off + length - st_off : 0;
467         unsigned long start_left = 0;
468         unsigned long end_left = 0;
469         unsigned long pg_count;
470
471         if (st_len == 0 || length == 0 || end == 0)
472                 return length;
473
474         start_left = do_div(start, st_len);
475         if (start_left < st_pgs)
476                 start_left = st_pgs - start_left;
477         else
478                 start_left = 0;
479
480         end_left = do_div(end, st_len);
481         if (end_left > st_pgs)
482                 end_left = st_pgs;
483
484         CDEBUG(D_READA, "start "LPU64", end "LPU64" start_left %lu end_left %lu \n",
485                start, end, start_left, end_left);
486
487         if (start == end)
488                 pg_count = end_left - (st_pgs - start_left);
489         else
490                 pg_count = start_left + st_pgs * (end - start - 1) + end_left;
491
492         CDEBUG(D_READA, "st_off %lu, st_len %lu st_pgs %lu off %lu length %lu"
493                "pgcount %lu\n", st_off, st_len, st_pgs, off, length, pg_count);
494
495         return pg_count;
496 }
497
498 static int ria_page_count(struct ra_io_arg *ria)
499 {
500         __u64 length = ria->ria_end >= ria->ria_start ?
501                        ria->ria_end - ria->ria_start + 1 : 0;
502
503         return stride_pg_count(ria->ria_stoff, ria->ria_length,
504                                ria->ria_pages, ria->ria_start,
505                                length);
506 }
507
508 /*Check whether the index is in the defined ra-window */
509 static int ras_inside_ra_window(unsigned long idx, struct ra_io_arg *ria)
510 {
511         /* If ria_length == ria_pages, it means non-stride I/O mode,
512          * idx should always inside read-ahead window in this case
513          * For stride I/O mode, just check whether the idx is inside
514          * the ria_pages. */
515         return ria->ria_length == 0 || ria->ria_length == ria->ria_pages ||
516                (idx >= ria->ria_stoff && (idx - ria->ria_stoff) %
517                 ria->ria_length < ria->ria_pages);
518 }
519
520 static int ll_read_ahead_pages(const struct lu_env *env,
521                                struct cl_io *io, struct cl_page_list *queue,
522                                struct ra_io_arg *ria,
523                                unsigned long *reserved_pages,
524                                unsigned long *ra_end)
525 {
526         int rc, count = 0;
527         bool stride_ria;
528         pgoff_t page_idx;
529         pgoff_t max_index = 0;
530
531         LASSERT(ria != NULL);
532         RIA_DEBUG(ria);
533
534         stride_ria = ria->ria_length > ria->ria_pages && ria->ria_pages > 0;
535         for (page_idx = ria->ria_start;
536              page_idx <= ria->ria_end && *reserved_pages > 0; page_idx++) {
537                 if (ras_inside_ra_window(page_idx, ria)) {
538                         /* If the page is inside the read-ahead window*/
539                         rc = ll_read_ahead_page(env, io, queue,
540                                                 page_idx, &max_index);
541                         if (rc == 1) {
542                                 (*reserved_pages)--;
543                                 count++;
544                         } else if (rc == -ENOLCK)
545                                 break;
546                 } else if (stride_ria) {
547                         /* If it is not in the read-ahead window, and it is
548                          * read-ahead mode, then check whether it should skip
549                          * the stride gap */
550                         pgoff_t offset;
551                         /* FIXME: This assertion only is valid when it is for
552                          * forward read-ahead, it will be fixed when backward
553                          * read-ahead is implemented */
554                         LASSERTF(page_idx > ria->ria_stoff, "Invalid page_idx %lu"
555                                 "rs %lu re %lu ro %lu rl %lu rp %lu\n", page_idx,
556                                 ria->ria_start, ria->ria_end, ria->ria_stoff,
557                                 ria->ria_length, ria->ria_pages);
558                         offset = page_idx - ria->ria_stoff;
559                         offset = offset % (ria->ria_length);
560                         if (offset > ria->ria_pages) {
561                                 page_idx += ria->ria_length - offset;
562                                 CDEBUG(D_READA, "i %lu skip %lu \n", page_idx,
563                                        ria->ria_length - offset);
564                                 continue;
565                         }
566                 }
567         }
568         *ra_end = page_idx;
569         return count;
570 }
571
572 int ll_readahead(const struct lu_env *env, struct cl_io *io,
573                  struct cl_page_list *queue, struct ll_readahead_state *ras,
574                  bool hit)
575 {
576         struct vvp_io *vio = vvp_env_io(env);
577         struct vvp_thread_info *vti = vvp_env_info(env);
578         struct cl_attr *attr = ccc_env_thread_attr(env);
579         unsigned long start = 0, end = 0, reserved;
580         unsigned long ra_end, len, mlen = 0;
581         struct inode *inode;
582         struct ll_ra_read *bead;
583         struct ra_io_arg *ria = &vti->vti_ria;
584         struct cl_object *clob;
585         int ret = 0;
586         __u64 kms;
587         ENTRY;
588
589         clob = io->ci_obj;
590         inode = ccc_object_inode(clob);
591
592         memset(ria, 0, sizeof *ria);
593
594         cl_object_attr_lock(clob);
595         ret = cl_object_attr_get(env, clob, attr);
596         cl_object_attr_unlock(clob);
597
598         if (ret != 0)
599                 RETURN(ret);
600         kms = attr->cat_kms;
601         if (kms == 0) {
602                 ll_ra_stats_inc(inode, RA_STAT_ZERO_LEN);
603                 RETURN(0);
604         }
605
606         spin_lock(&ras->ras_lock);
607         if (vio->cui_ra_window_set)
608                 bead = &vio->cui_bead;
609         else
610                 bead = NULL;
611
612         /* Enlarge the RA window to encompass the full read */
613         if (bead != NULL && ras->ras_window_start + ras->ras_window_len <
614             bead->lrr_start + bead->lrr_count) {
615                 ras->ras_window_len = bead->lrr_start + bead->lrr_count -
616                                       ras->ras_window_start;
617         }
618         /* Reserve a part of the read-ahead window that we'll be issuing */
619         if (ras->ras_window_len) {
620                 start = ras->ras_next_readahead;
621                 end = ras->ras_window_start + ras->ras_window_len - 1;
622         }
623         if (end != 0) {
624                 unsigned long rpc_boundary;
625                 /*
626                  * Align RA window to an optimal boundary.
627                  *
628                  * XXX This would be better to align to cl_max_pages_per_rpc
629                  * instead of PTLRPC_MAX_BRW_PAGES, because the RPC size may
630                  * be aligned to the RAID stripe size in the future and that
631                  * is more important than the RPC size.
632                  */
633                 /* Note: we only trim the RPC, instead of extending the RPC
634                  * to the boundary, so to avoid reading too much pages during
635                  * random reading. */
636                 rpc_boundary = ((end + 1) & (~(PTLRPC_MAX_BRW_PAGES - 1)));
637                 if (rpc_boundary > 0)
638                         rpc_boundary--;
639
640                 if (rpc_boundary  > start)
641                         end = rpc_boundary;
642
643                 /* Truncate RA window to end of file */
644                 end = min(end, (unsigned long)((kms - 1) >> PAGE_CACHE_SHIFT));
645
646                 ras->ras_next_readahead = max(end, end + 1);
647                 RAS_CDEBUG(ras);
648         }
649         ria->ria_start = start;
650         ria->ria_end = end;
651         /* If stride I/O mode is detected, get stride window*/
652         if (stride_io_mode(ras)) {
653                 ria->ria_stoff = ras->ras_stride_offset;
654                 ria->ria_length = ras->ras_stride_length;
655                 ria->ria_pages = ras->ras_stride_pages;
656         }
657         spin_unlock(&ras->ras_lock);
658
659         if (end == 0) {
660                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
661                 RETURN(0);
662         }
663         len = ria_page_count(ria);
664         if (len == 0) {
665                 ll_ra_stats_inc(inode, RA_STAT_ZERO_WINDOW);
666                 RETURN(0);
667         }
668
669         CDEBUG(D_READA, DFID": ria: %lu/%lu, bead: %lu/%lu, hit: %d\n",
670                PFID(lu_object_fid(&clob->co_lu)),
671                ria->ria_start, ria->ria_end,
672                bead == NULL ? 0 : bead->lrr_start,
673                bead == NULL ? 0 : bead->lrr_count,
674                hit);
675
676         /* at least to extend the readahead window to cover current read */
677         if (!hit && bead != NULL &&
678             bead->lrr_start + bead->lrr_count > ria->ria_start) {
679                 /* to the end of current read window. */
680                 mlen = bead->lrr_start + bead->lrr_count - ria->ria_start;
681                 /* trim to RPC boundary */
682                 start = ria->ria_start & (PTLRPC_MAX_BRW_PAGES - 1);
683                 mlen = min(mlen, PTLRPC_MAX_BRW_PAGES - start);
684         }
685
686         reserved = ll_ra_count_get(ll_i2sbi(inode), ria, len, mlen);
687         if (reserved < len)
688                 ll_ra_stats_inc(inode, RA_STAT_MAX_IN_FLIGHT);
689
690         CDEBUG(D_READA, "reserved pages: %lu/%lu/%lu, ra_cur %d, ra_max %lu\n",
691                reserved, len, mlen,
692                atomic_read(&ll_i2sbi(inode)->ll_ra_info.ra_cur_pages),
693                ll_i2sbi(inode)->ll_ra_info.ra_max_pages);
694
695         ret = ll_read_ahead_pages(env, io, queue, ria, &reserved, &ra_end);
696
697         if (reserved != 0)
698                 ll_ra_count_put(ll_i2sbi(inode), reserved);
699
700         if (ra_end == end + 1 && ra_end == (kms >> PAGE_CACHE_SHIFT))
701                 ll_ra_stats_inc(inode, RA_STAT_EOF);
702
703         /* if we didn't get to the end of the region we reserved from
704          * the ras we need to go back and update the ras so that the
705          * next read-ahead tries from where we left off.  we only do so
706          * if the region we failed to issue read-ahead on is still ahead
707          * of the app and behind the next index to start read-ahead from */
708         CDEBUG(D_READA, "ra_end %lu end %lu stride end %lu \n",
709                ra_end, end, ria->ria_end);
710
711         if (ra_end != end + 1) {
712                 ll_ra_stats_inc(inode, RA_STAT_FAILED_REACH_END);
713                 spin_lock(&ras->ras_lock);
714                 if (ra_end < ras->ras_next_readahead &&
715                     index_in_window(ra_end, ras->ras_window_start, 0,
716                                     ras->ras_window_len)) {
717                         ras->ras_next_readahead = ra_end;
718                         RAS_CDEBUG(ras);
719                 }
720                 spin_unlock(&ras->ras_lock);
721         }
722
723         RETURN(ret);
724 }
725
726 static void ras_set_start(struct inode *inode, struct ll_readahead_state *ras,
727                           unsigned long index)
728 {
729         ras->ras_window_start = index & (~(RAS_INCREASE_STEP(inode) - 1));
730 }
731
732 /* called with the ras_lock held or from places where it doesn't matter */
733 static void ras_reset(struct inode *inode, struct ll_readahead_state *ras,
734                       unsigned long index)
735 {
736         ras->ras_last_readpage = index;
737         ras->ras_consecutive_requests = 0;
738         ras->ras_consecutive_pages = 0;
739         ras->ras_window_len = 0;
740         ras_set_start(inode, ras, index);
741         ras->ras_next_readahead = max(ras->ras_window_start, index);
742
743         RAS_CDEBUG(ras);
744 }
745
746 /* called with the ras_lock held or from places where it doesn't matter */
747 static void ras_stride_reset(struct ll_readahead_state *ras)
748 {
749         ras->ras_consecutive_stride_requests = 0;
750         ras->ras_stride_length = 0;
751         ras->ras_stride_pages = 0;
752         RAS_CDEBUG(ras);
753 }
754
755 void ll_readahead_init(struct inode *inode, struct ll_readahead_state *ras)
756 {
757         spin_lock_init(&ras->ras_lock);
758         ras_reset(inode, ras, 0);
759         ras->ras_requests = 0;
760         INIT_LIST_HEAD(&ras->ras_read_beads);
761 }
762
763 /*
764  * Check whether the read request is in the stride window.
765  * If it is in the stride window, return 1, otherwise return 0.
766  */
767 static int index_in_stride_window(struct ll_readahead_state *ras,
768                                   unsigned long index)
769 {
770         unsigned long stride_gap;
771
772         if (ras->ras_stride_length == 0 || ras->ras_stride_pages == 0 ||
773             ras->ras_stride_pages == ras->ras_stride_length)
774                 return 0;
775
776         stride_gap = index - ras->ras_last_readpage - 1;
777
778         /* If it is contiguous read */
779         if (stride_gap == 0)
780                 return ras->ras_consecutive_pages + 1 <= ras->ras_stride_pages;
781
782         /* Otherwise check the stride by itself */
783         return (ras->ras_stride_length - ras->ras_stride_pages) == stride_gap &&
784                 ras->ras_consecutive_pages == ras->ras_stride_pages;
785 }
786
787 static void ras_update_stride_detector(struct ll_readahead_state *ras,
788                                        unsigned long index)
789 {
790         unsigned long stride_gap = index - ras->ras_last_readpage - 1;
791
792         if (!stride_io_mode(ras) && (stride_gap != 0 ||
793              ras->ras_consecutive_stride_requests == 0)) {
794                 ras->ras_stride_pages = ras->ras_consecutive_pages;
795                 ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
796         }
797         LASSERT(ras->ras_request_index == 0);
798         LASSERT(ras->ras_consecutive_stride_requests == 0);
799
800         if (index <= ras->ras_last_readpage) {
801                 /*Reset stride window for forward read*/
802                 ras_stride_reset(ras);
803                 return;
804         }
805
806         ras->ras_stride_pages = ras->ras_consecutive_pages;
807         ras->ras_stride_length = stride_gap +ras->ras_consecutive_pages;
808
809         RAS_CDEBUG(ras);
810         return;
811 }
812
813 static unsigned long
814 stride_page_count(struct ll_readahead_state *ras, unsigned long len)
815 {
816         return stride_pg_count(ras->ras_stride_offset, ras->ras_stride_length,
817                                ras->ras_stride_pages, ras->ras_stride_offset,
818                                len);
819 }
820
821 /* Stride Read-ahead window will be increased inc_len according to
822  * stride I/O pattern */
823 static void ras_stride_increase_window(struct ll_readahead_state *ras,
824                                        struct ll_ra_info *ra,
825                                        unsigned long inc_len)
826 {
827         unsigned long left, step, window_len;
828         unsigned long stride_len;
829
830         LASSERT(ras->ras_stride_length > 0);
831         LASSERTF(ras->ras_window_start + ras->ras_window_len
832                  >= ras->ras_stride_offset, "window_start %lu, window_len %lu"
833                  " stride_offset %lu\n", ras->ras_window_start,
834                  ras->ras_window_len, ras->ras_stride_offset);
835
836         stride_len = ras->ras_window_start + ras->ras_window_len -
837                      ras->ras_stride_offset;
838
839         left = stride_len % ras->ras_stride_length;
840         window_len = ras->ras_window_len - left;
841
842         if (left < ras->ras_stride_pages)
843                 left += inc_len;
844         else
845                 left = ras->ras_stride_pages + inc_len;
846
847         LASSERT(ras->ras_stride_pages != 0);
848
849         step = left / ras->ras_stride_pages;
850         left %= ras->ras_stride_pages;
851
852         window_len += step * ras->ras_stride_length + left;
853
854         if (stride_page_count(ras, window_len) <= ra->ra_max_pages_per_file)
855                 ras->ras_window_len = window_len;
856
857         RAS_CDEBUG(ras);
858 }
859
860 static void ras_increase_window(struct inode *inode,
861                                 struct ll_readahead_state *ras,
862                                 struct ll_ra_info *ra)
863 {
864         /* The stretch of ra-window should be aligned with max rpc_size
865          * but current clio architecture does not support retrieve such
866          * information from lower layer. FIXME later
867          */
868         if (stride_io_mode(ras))
869                 ras_stride_increase_window(ras, ra, RAS_INCREASE_STEP(inode));
870         else
871                 ras->ras_window_len = min(ras->ras_window_len +
872                                           RAS_INCREASE_STEP(inode),
873                                           ra->ra_max_pages_per_file);
874 }
875
876 void ras_update(struct ll_sb_info *sbi, struct inode *inode,
877                 struct ll_readahead_state *ras, unsigned long index,
878                 unsigned hit)
879 {
880         struct ll_ra_info *ra = &sbi->ll_ra_info;
881         int zero = 0, stride_detect = 0, ra_miss = 0;
882         ENTRY;
883
884         spin_lock(&ras->ras_lock);
885
886         ll_ra_stats_inc_sbi(sbi, hit ? RA_STAT_HIT : RA_STAT_MISS);
887
888         /* reset the read-ahead window in two cases.  First when the app seeks
889          * or reads to some other part of the file.  Secondly if we get a
890          * read-ahead miss that we think we've previously issued.  This can
891          * be a symptom of there being so many read-ahead pages that the VM is
892          * reclaiming it before we get to it. */
893         if (!index_in_window(index, ras->ras_last_readpage, 8, 8)) {
894                 zero = 1;
895                 ll_ra_stats_inc_sbi(sbi, RA_STAT_DISTANT_READPAGE);
896         } else if (!hit && ras->ras_window_len &&
897                    index < ras->ras_next_readahead &&
898                    index_in_window(index, ras->ras_window_start, 0,
899                                    ras->ras_window_len)) {
900                 ra_miss = 1;
901                 ll_ra_stats_inc_sbi(sbi, RA_STAT_MISS_IN_WINDOW);
902         }
903
904         /* On the second access to a file smaller than the tunable
905          * ra_max_read_ahead_whole_pages trigger RA on all pages in the
906          * file up to ra_max_pages_per_file.  This is simply a best effort
907          * and only occurs once per open file.  Normal RA behavior is reverted
908          * to for subsequent IO.  The mmap case does not increment
909          * ras_requests and thus can never trigger this behavior. */
910         if (ras->ras_requests == 2 && !ras->ras_request_index) {
911                 __u64 kms_pages;
912
913                 kms_pages = (i_size_read(inode) + PAGE_CACHE_SIZE - 1) >>
914                             PAGE_CACHE_SHIFT;
915
916                 CDEBUG(D_READA, "kmsp "LPU64" mwp %lu mp %lu\n", kms_pages,
917                        ra->ra_max_read_ahead_whole_pages, ra->ra_max_pages_per_file);
918
919                 if (kms_pages &&
920                     kms_pages <= ra->ra_max_read_ahead_whole_pages) {
921                         ras->ras_window_start = 0;
922                         ras->ras_last_readpage = 0;
923                         ras->ras_next_readahead = 0;
924                         ras->ras_window_len = min(ra->ra_max_pages_per_file,
925                                 ra->ra_max_read_ahead_whole_pages);
926                         GOTO(out_unlock, 0);
927                 }
928         }
929         if (zero) {
930                 /* check whether it is in stride I/O mode*/
931                 if (!index_in_stride_window(ras, index)) {
932                         if (ras->ras_consecutive_stride_requests == 0 &&
933                             ras->ras_request_index == 0) {
934                                 ras_update_stride_detector(ras, index);
935                                 ras->ras_consecutive_stride_requests++;
936                         } else {
937                                 ras_stride_reset(ras);
938                         }
939                         ras_reset(inode, ras, index);
940                         ras->ras_consecutive_pages++;
941                         GOTO(out_unlock, 0);
942                 } else {
943                         ras->ras_consecutive_pages = 0;
944                         ras->ras_consecutive_requests = 0;
945                         if (++ras->ras_consecutive_stride_requests > 1)
946                                 stride_detect = 1;
947                         RAS_CDEBUG(ras);
948                 }
949         } else {
950                 if (ra_miss) {
951                         if (index_in_stride_window(ras, index) &&
952                             stride_io_mode(ras)) {
953                                 /*If stride-RA hit cache miss, the stride dector
954                                  *will not be reset to avoid the overhead of
955                                  *redetecting read-ahead mode */
956                                 if (index != ras->ras_last_readpage + 1)
957                                         ras->ras_consecutive_pages = 0;
958                                 ras_reset(inode, ras, index);
959                                 RAS_CDEBUG(ras);
960                         } else {
961                                 /* Reset both stride window and normal RA
962                                  * window */
963                                 ras_reset(inode, ras, index);
964                                 ras->ras_consecutive_pages++;
965                                 ras_stride_reset(ras);
966                                 GOTO(out_unlock, 0);
967                         }
968                 } else if (stride_io_mode(ras)) {
969                         /* If this is contiguous read but in stride I/O mode
970                          * currently, check whether stride step still is valid,
971                          * if invalid, it will reset the stride ra window*/
972                         if (!index_in_stride_window(ras, index)) {
973                                 /* Shrink stride read-ahead window to be zero */
974                                 ras_stride_reset(ras);
975                                 ras->ras_window_len = 0;
976                                 ras->ras_next_readahead = index;
977                         }
978                 }
979         }
980         ras->ras_consecutive_pages++;
981         ras->ras_last_readpage = index;
982         ras_set_start(inode, ras, index);
983
984         if (stride_io_mode(ras)) {
985                 /* Since stride readahead is sentivite to the offset
986                  * of read-ahead, so we use original offset here,
987                  * instead of ras_window_start, which is RPC aligned */
988                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
989         } else {
990                 if (ras->ras_next_readahead < ras->ras_window_start)
991                         ras->ras_next_readahead = ras->ras_window_start;
992                 if (!hit)
993                         ras->ras_next_readahead = index + 1;
994         }
995         RAS_CDEBUG(ras);
996
997         /* Trigger RA in the mmap case where ras_consecutive_requests
998          * is not incremented and thus can't be used to trigger RA */
999         if (!ras->ras_window_len && ras->ras_consecutive_pages == 4) {
1000                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1001                 GOTO(out_unlock, 0);
1002         }
1003
1004         /* Initially reset the stride window offset to next_readahead*/
1005         if (ras->ras_consecutive_stride_requests == 2 && stride_detect) {
1006                 /**
1007                  * Once stride IO mode is detected, next_readahead should be
1008                  * reset to make sure next_readahead > stride offset
1009                  */
1010                 ras->ras_next_readahead = max(index, ras->ras_next_readahead);
1011                 ras->ras_stride_offset = index;
1012                 ras->ras_window_len = RAS_INCREASE_STEP(inode);
1013         }
1014
1015         /* The initial ras_window_len is set to the request size.  To avoid
1016          * uselessly reading and discarding pages for random IO the window is
1017          * only increased once per consecutive request received. */
1018         if ((ras->ras_consecutive_requests > 1 || stride_detect) &&
1019             !ras->ras_request_index)
1020                 ras_increase_window(inode, ras, ra);
1021         EXIT;
1022 out_unlock:
1023         RAS_CDEBUG(ras);
1024         ras->ras_request_index++;
1025         spin_unlock(&ras->ras_lock);
1026         return;
1027 }
1028
1029 int ll_writepage(struct page *vmpage, struct writeback_control *wbc)
1030 {
1031         struct inode           *inode = vmpage->mapping->host;
1032         struct ll_inode_info   *lli   = ll_i2info(inode);
1033         struct lu_env          *env;
1034         struct cl_io           *io;
1035         struct cl_page         *page;
1036         struct cl_object       *clob;
1037         struct cl_env_nest      nest;
1038         bool redirtied = false;
1039         bool unlocked = false;
1040         int result;
1041         ENTRY;
1042
1043         LASSERT(PageLocked(vmpage));
1044         LASSERT(!PageWriteback(vmpage));
1045
1046         LASSERT(ll_i2dtexp(inode) != NULL);
1047
1048         env = cl_env_nested_get(&nest);
1049         if (IS_ERR(env))
1050                 GOTO(out, result = PTR_ERR(env));
1051
1052         clob  = ll_i2info(inode)->lli_clob;
1053         LASSERT(clob != NULL);
1054
1055         io = ccc_env_thread_io(env);
1056         io->ci_obj = clob;
1057         io->ci_ignore_layout = 1;
1058         result = cl_io_init(env, io, CIT_MISC, clob);
1059         if (result == 0) {
1060                 page = cl_page_find(env, clob, vmpage->index,
1061                                     vmpage, CPT_CACHEABLE);
1062                 if (!IS_ERR(page)) {
1063                         lu_ref_add(&page->cp_reference, "writepage",
1064                                    current);
1065                         cl_page_assume(env, io, page);
1066                         result = cl_page_flush(env, io, page);
1067                         if (result != 0) {
1068                                 /*
1069                                  * Re-dirty page on error so it retries write,
1070                                  * but not in case when IO has actually
1071                                  * occurred and completed with an error.
1072                                  */
1073                                 if (!PageError(vmpage)) {
1074                                         redirty_page_for_writepage(wbc, vmpage);
1075                                         result = 0;
1076                                         redirtied = true;
1077                                 }
1078                         }
1079                         cl_page_disown(env, io, page);
1080                         unlocked = true;
1081                         lu_ref_del(&page->cp_reference,
1082                                    "writepage", current);
1083                         cl_page_put(env, page);
1084                 } else {
1085                         result = PTR_ERR(page);
1086                 }
1087         }
1088         cl_io_fini(env, io);
1089
1090         if (redirtied && wbc->sync_mode == WB_SYNC_ALL) {
1091                 loff_t offset = cl_offset(clob, vmpage->index);
1092
1093                 /* Flush page failed because the extent is being written out.
1094                  * Wait for the write of extent to be finished to avoid
1095                  * breaking kernel which assumes ->writepage should mark
1096                  * PageWriteback or clean the page. */
1097                 result = cl_sync_file_range(inode, offset,
1098                                             offset + PAGE_CACHE_SIZE - 1,
1099                                             CL_FSYNC_LOCAL, 1);
1100                 if (result > 0) {
1101                         /* actually we may have written more than one page.
1102                          * decreasing this page because the caller will count
1103                          * it. */
1104                         wbc->nr_to_write -= result - 1;
1105                         result = 0;
1106                 }
1107         }
1108
1109         cl_env_nested_put(&nest, env);
1110         GOTO(out, result);
1111
1112 out:
1113         if (result < 0) {
1114                 if (!lli->lli_async_rc)
1115                         lli->lli_async_rc = result;
1116                 SetPageError(vmpage);
1117                 if (!unlocked)
1118                         unlock_page(vmpage);
1119         }
1120         return result;
1121 }
1122
1123 int ll_writepages(struct address_space *mapping, struct writeback_control *wbc)
1124 {
1125         struct inode *inode = mapping->host;
1126         struct ll_sb_info *sbi = ll_i2sbi(inode);
1127         loff_t start;
1128         loff_t end;
1129         enum cl_fsync_mode mode;
1130         int range_whole = 0;
1131         int result;
1132         int ignore_layout = 0;
1133         ENTRY;
1134
1135         if (wbc->range_cyclic) {
1136                 start = mapping->writeback_index << PAGE_CACHE_SHIFT;
1137                 end = OBD_OBJECT_EOF;
1138         } else {
1139                 start = wbc->range_start;
1140                 end = wbc->range_end;
1141                 if (end == LLONG_MAX) {
1142                         end = OBD_OBJECT_EOF;
1143                         range_whole = start == 0;
1144                 }
1145         }
1146
1147         mode = CL_FSYNC_NONE;
1148         if (wbc->sync_mode == WB_SYNC_ALL)
1149                 mode = CL_FSYNC_LOCAL;
1150
1151         if (sbi->ll_umounting)
1152                 /* if the mountpoint is being umounted, all pages have to be
1153                  * evicted to avoid hitting LBUG when truncate_inode_pages()
1154                  * is called later on. */
1155                 ignore_layout = 1;
1156
1157         if (cl_i2info(inode)->lli_clob == NULL)
1158                 RETURN(0);
1159
1160         result = cl_sync_file_range(inode, start, end, mode, ignore_layout);
1161         if (result > 0) {
1162                 wbc->nr_to_write -= result;
1163                 result = 0;
1164          }
1165
1166         if (wbc->range_cyclic || (range_whole && wbc->nr_to_write > 0)) {
1167                 if (end == OBD_OBJECT_EOF)
1168                         mapping->writeback_index = 0;
1169                 else
1170                         mapping->writeback_index = (end >> PAGE_CACHE_SHIFT) +1;
1171         }
1172         RETURN(result);
1173 }
1174
1175 int ll_readpage(struct file *file, struct page *vmpage)
1176 {
1177         struct ll_cl_context *lcc;
1178         int result;
1179         ENTRY;
1180
1181         lcc = ll_cl_init(file, vmpage);
1182         if (!IS_ERR(lcc)) {
1183                 struct lu_env  *env  = lcc->lcc_env;
1184                 struct cl_io   *io   = lcc->lcc_io;
1185                 struct cl_page *page = lcc->lcc_page;
1186
1187                 LASSERT(page->cp_type == CPT_CACHEABLE);
1188                 if (likely(!PageUptodate(vmpage))) {
1189                         cl_page_assume(env, io, page);
1190                         result = cl_io_read_page(env, io, page);
1191                 } else {
1192                         /* Page from a non-object file. */
1193                         unlock_page(vmpage);
1194                         result = 0;
1195                 }
1196                 ll_cl_fini(lcc);
1197         } else {
1198                 unlock_page(vmpage);
1199                 result = PTR_ERR(lcc);
1200         }
1201         RETURN(result);
1202 }
1203
1204 int ll_page_sync_io(const struct lu_env *env, struct cl_io *io,
1205                     struct cl_page *page, enum cl_req_type crt)
1206 {
1207         struct cl_2queue  *queue;
1208         int result;
1209
1210         LASSERT(io->ci_type == CIT_READ || io->ci_type == CIT_WRITE);
1211
1212         queue = &io->ci_queue;
1213         cl_2queue_init_page(queue, page);
1214
1215         result = cl_io_submit_sync(env, io, crt, queue, 0);
1216         LASSERT(cl_page_is_owned(page, io));
1217
1218         if (crt == CRT_READ)
1219                 /*
1220                  * in CRT_WRITE case page is left locked even in case of
1221                  * error.
1222                  */
1223                 cl_page_list_disown(env, io, &queue->c2_qin);
1224         cl_2queue_fini(env, queue);
1225
1226         return result;
1227 }